intel_display.c 453 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318
  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 <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. #include <linux/reservation.h>
  47. #include <linux/dma-buf.h>
  48. /* Primary plane formats for gen <= 3 */
  49. static const uint32_t i8xx_primary_formats[] = {
  50. DRM_FORMAT_C8,
  51. DRM_FORMAT_RGB565,
  52. DRM_FORMAT_XRGB1555,
  53. DRM_FORMAT_XRGB8888,
  54. };
  55. /* Primary plane formats for gen >= 4 */
  56. static const uint32_t i965_primary_formats[] = {
  57. DRM_FORMAT_C8,
  58. DRM_FORMAT_RGB565,
  59. DRM_FORMAT_XRGB8888,
  60. DRM_FORMAT_XBGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_XBGR2101010,
  63. };
  64. static const uint32_t skl_primary_formats[] = {
  65. DRM_FORMAT_C8,
  66. DRM_FORMAT_RGB565,
  67. DRM_FORMAT_XRGB8888,
  68. DRM_FORMAT_XBGR8888,
  69. DRM_FORMAT_ARGB8888,
  70. DRM_FORMAT_ABGR8888,
  71. DRM_FORMAT_XRGB2101010,
  72. DRM_FORMAT_XBGR2101010,
  73. DRM_FORMAT_YUYV,
  74. DRM_FORMAT_YVYU,
  75. DRM_FORMAT_UYVY,
  76. DRM_FORMAT_VYUY,
  77. };
  78. /* Cursor formats */
  79. static const uint32_t intel_cursor_formats[] = {
  80. DRM_FORMAT_ARGB8888,
  81. };
  82. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  83. struct intel_crtc_state *pipe_config);
  84. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  85. struct intel_crtc_state *pipe_config);
  86. static int intel_framebuffer_init(struct drm_device *dev,
  87. struct intel_framebuffer *ifb,
  88. struct drm_mode_fb_cmd2 *mode_cmd,
  89. struct drm_i915_gem_object *obj);
  90. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  91. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  92. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  93. struct intel_link_m_n *m_n,
  94. struct intel_link_m_n *m2_n2);
  95. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  96. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  97. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  98. static void vlv_prepare_pll(struct intel_crtc *crtc,
  99. const struct intel_crtc_state *pipe_config);
  100. static void chv_prepare_pll(struct intel_crtc *crtc,
  101. const struct intel_crtc_state *pipe_config);
  102. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  104. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  105. struct intel_crtc_state *crtc_state);
  106. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  107. int num_connectors);
  108. static void skylake_pfit_enable(struct intel_crtc *crtc);
  109. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  110. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  111. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  112. static void intel_pre_disable_primary(struct drm_crtc *crtc);
  113. typedef struct {
  114. int min, max;
  115. } intel_range_t;
  116. typedef struct {
  117. int dot_limit;
  118. int p2_slow, p2_fast;
  119. } intel_p2_t;
  120. typedef struct intel_limit intel_limit_t;
  121. struct intel_limit {
  122. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  123. intel_p2_t p2;
  124. };
  125. /* returns HPLL frequency in kHz */
  126. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  127. {
  128. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  129. /* Obtain SKU information */
  130. mutex_lock(&dev_priv->sb_lock);
  131. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  132. CCK_FUSE_HPLL_FREQ_MASK;
  133. mutex_unlock(&dev_priv->sb_lock);
  134. return vco_freq[hpll_freq] * 1000;
  135. }
  136. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  137. const char *name, u32 reg)
  138. {
  139. u32 val;
  140. int divider;
  141. if (dev_priv->hpll_freq == 0)
  142. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  143. mutex_lock(&dev_priv->sb_lock);
  144. val = vlv_cck_read(dev_priv, reg);
  145. mutex_unlock(&dev_priv->sb_lock);
  146. divider = val & CCK_FREQUENCY_VALUES;
  147. WARN((val & CCK_FREQUENCY_STATUS) !=
  148. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  149. "%s change in progress\n", name);
  150. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  151. }
  152. int
  153. intel_pch_rawclk(struct drm_device *dev)
  154. {
  155. struct drm_i915_private *dev_priv = dev->dev_private;
  156. WARN_ON(!HAS_PCH_SPLIT(dev));
  157. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  158. }
  159. /* hrawclock is 1/4 the FSB frequency */
  160. int intel_hrawclk(struct drm_device *dev)
  161. {
  162. struct drm_i915_private *dev_priv = dev->dev_private;
  163. uint32_t clkcfg;
  164. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  165. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  166. return 200;
  167. clkcfg = I915_READ(CLKCFG);
  168. switch (clkcfg & CLKCFG_FSB_MASK) {
  169. case CLKCFG_FSB_400:
  170. return 100;
  171. case CLKCFG_FSB_533:
  172. return 133;
  173. case CLKCFG_FSB_667:
  174. return 166;
  175. case CLKCFG_FSB_800:
  176. return 200;
  177. case CLKCFG_FSB_1067:
  178. return 266;
  179. case CLKCFG_FSB_1333:
  180. return 333;
  181. /* these two are just a guess; one of them might be right */
  182. case CLKCFG_FSB_1600:
  183. case CLKCFG_FSB_1600_ALT:
  184. return 400;
  185. default:
  186. return 133;
  187. }
  188. }
  189. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  190. {
  191. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  192. return;
  193. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  194. CCK_CZ_CLOCK_CONTROL);
  195. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  196. }
  197. static inline u32 /* units of 100MHz */
  198. intel_fdi_link_freq(struct drm_device *dev)
  199. {
  200. if (IS_GEN5(dev)) {
  201. struct drm_i915_private *dev_priv = dev->dev_private;
  202. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  203. } else
  204. return 27;
  205. }
  206. static const intel_limit_t intel_limits_i8xx_dac = {
  207. .dot = { .min = 25000, .max = 350000 },
  208. .vco = { .min = 908000, .max = 1512000 },
  209. .n = { .min = 2, .max = 16 },
  210. .m = { .min = 96, .max = 140 },
  211. .m1 = { .min = 18, .max = 26 },
  212. .m2 = { .min = 6, .max = 16 },
  213. .p = { .min = 4, .max = 128 },
  214. .p1 = { .min = 2, .max = 33 },
  215. .p2 = { .dot_limit = 165000,
  216. .p2_slow = 4, .p2_fast = 2 },
  217. };
  218. static const intel_limit_t intel_limits_i8xx_dvo = {
  219. .dot = { .min = 25000, .max = 350000 },
  220. .vco = { .min = 908000, .max = 1512000 },
  221. .n = { .min = 2, .max = 16 },
  222. .m = { .min = 96, .max = 140 },
  223. .m1 = { .min = 18, .max = 26 },
  224. .m2 = { .min = 6, .max = 16 },
  225. .p = { .min = 4, .max = 128 },
  226. .p1 = { .min = 2, .max = 33 },
  227. .p2 = { .dot_limit = 165000,
  228. .p2_slow = 4, .p2_fast = 4 },
  229. };
  230. static const intel_limit_t intel_limits_i8xx_lvds = {
  231. .dot = { .min = 25000, .max = 350000 },
  232. .vco = { .min = 908000, .max = 1512000 },
  233. .n = { .min = 2, .max = 16 },
  234. .m = { .min = 96, .max = 140 },
  235. .m1 = { .min = 18, .max = 26 },
  236. .m2 = { .min = 6, .max = 16 },
  237. .p = { .min = 4, .max = 128 },
  238. .p1 = { .min = 1, .max = 6 },
  239. .p2 = { .dot_limit = 165000,
  240. .p2_slow = 14, .p2_fast = 7 },
  241. };
  242. static const intel_limit_t intel_limits_i9xx_sdvo = {
  243. .dot = { .min = 20000, .max = 400000 },
  244. .vco = { .min = 1400000, .max = 2800000 },
  245. .n = { .min = 1, .max = 6 },
  246. .m = { .min = 70, .max = 120 },
  247. .m1 = { .min = 8, .max = 18 },
  248. .m2 = { .min = 3, .max = 7 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_i9xx_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1400000, .max = 2800000 },
  257. .n = { .min = 1, .max = 6 },
  258. .m = { .min = 70, .max = 120 },
  259. .m1 = { .min = 8, .max = 18 },
  260. .m2 = { .min = 3, .max = 7 },
  261. .p = { .min = 7, .max = 98 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 7 },
  265. };
  266. static const intel_limit_t intel_limits_g4x_sdvo = {
  267. .dot = { .min = 25000, .max = 270000 },
  268. .vco = { .min = 1750000, .max = 3500000},
  269. .n = { .min = 1, .max = 4 },
  270. .m = { .min = 104, .max = 138 },
  271. .m1 = { .min = 17, .max = 23 },
  272. .m2 = { .min = 5, .max = 11 },
  273. .p = { .min = 10, .max = 30 },
  274. .p1 = { .min = 1, .max = 3},
  275. .p2 = { .dot_limit = 270000,
  276. .p2_slow = 10,
  277. .p2_fast = 10
  278. },
  279. };
  280. static const intel_limit_t intel_limits_g4x_hdmi = {
  281. .dot = { .min = 22000, .max = 400000 },
  282. .vco = { .min = 1750000, .max = 3500000},
  283. .n = { .min = 1, .max = 4 },
  284. .m = { .min = 104, .max = 138 },
  285. .m1 = { .min = 16, .max = 23 },
  286. .m2 = { .min = 5, .max = 11 },
  287. .p = { .min = 5, .max = 80 },
  288. .p1 = { .min = 1, .max = 8},
  289. .p2 = { .dot_limit = 165000,
  290. .p2_slow = 10, .p2_fast = 5 },
  291. };
  292. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  293. .dot = { .min = 20000, .max = 115000 },
  294. .vco = { .min = 1750000, .max = 3500000 },
  295. .n = { .min = 1, .max = 3 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 28, .max = 112 },
  300. .p1 = { .min = 2, .max = 8 },
  301. .p2 = { .dot_limit = 0,
  302. .p2_slow = 14, .p2_fast = 14
  303. },
  304. };
  305. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  306. .dot = { .min = 80000, .max = 224000 },
  307. .vco = { .min = 1750000, .max = 3500000 },
  308. .n = { .min = 1, .max = 3 },
  309. .m = { .min = 104, .max = 138 },
  310. .m1 = { .min = 17, .max = 23 },
  311. .m2 = { .min = 5, .max = 11 },
  312. .p = { .min = 14, .max = 42 },
  313. .p1 = { .min = 2, .max = 6 },
  314. .p2 = { .dot_limit = 0,
  315. .p2_slow = 7, .p2_fast = 7
  316. },
  317. };
  318. static const intel_limit_t intel_limits_pineview_sdvo = {
  319. .dot = { .min = 20000, .max = 400000},
  320. .vco = { .min = 1700000, .max = 3500000 },
  321. /* Pineview's Ncounter is a ring counter */
  322. .n = { .min = 3, .max = 6 },
  323. .m = { .min = 2, .max = 256 },
  324. /* Pineview only has one combined m divider, which we treat as m2. */
  325. .m1 = { .min = 0, .max = 0 },
  326. .m2 = { .min = 0, .max = 254 },
  327. .p = { .min = 5, .max = 80 },
  328. .p1 = { .min = 1, .max = 8 },
  329. .p2 = { .dot_limit = 200000,
  330. .p2_slow = 10, .p2_fast = 5 },
  331. };
  332. static const intel_limit_t intel_limits_pineview_lvds = {
  333. .dot = { .min = 20000, .max = 400000 },
  334. .vco = { .min = 1700000, .max = 3500000 },
  335. .n = { .min = 3, .max = 6 },
  336. .m = { .min = 2, .max = 256 },
  337. .m1 = { .min = 0, .max = 0 },
  338. .m2 = { .min = 0, .max = 254 },
  339. .p = { .min = 7, .max = 112 },
  340. .p1 = { .min = 1, .max = 8 },
  341. .p2 = { .dot_limit = 112000,
  342. .p2_slow = 14, .p2_fast = 14 },
  343. };
  344. /* Ironlake / Sandybridge
  345. *
  346. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  347. * the range value for them is (actual_value - 2).
  348. */
  349. static const intel_limit_t intel_limits_ironlake_dac = {
  350. .dot = { .min = 25000, .max = 350000 },
  351. .vco = { .min = 1760000, .max = 3510000 },
  352. .n = { .min = 1, .max = 5 },
  353. .m = { .min = 79, .max = 127 },
  354. .m1 = { .min = 12, .max = 22 },
  355. .m2 = { .min = 5, .max = 9 },
  356. .p = { .min = 5, .max = 80 },
  357. .p1 = { .min = 1, .max = 8 },
  358. .p2 = { .dot_limit = 225000,
  359. .p2_slow = 10, .p2_fast = 5 },
  360. };
  361. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  362. .dot = { .min = 25000, .max = 350000 },
  363. .vco = { .min = 1760000, .max = 3510000 },
  364. .n = { .min = 1, .max = 3 },
  365. .m = { .min = 79, .max = 118 },
  366. .m1 = { .min = 12, .max = 22 },
  367. .m2 = { .min = 5, .max = 9 },
  368. .p = { .min = 28, .max = 112 },
  369. .p1 = { .min = 2, .max = 8 },
  370. .p2 = { .dot_limit = 225000,
  371. .p2_slow = 14, .p2_fast = 14 },
  372. };
  373. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  374. .dot = { .min = 25000, .max = 350000 },
  375. .vco = { .min = 1760000, .max = 3510000 },
  376. .n = { .min = 1, .max = 3 },
  377. .m = { .min = 79, .max = 127 },
  378. .m1 = { .min = 12, .max = 22 },
  379. .m2 = { .min = 5, .max = 9 },
  380. .p = { .min = 14, .max = 56 },
  381. .p1 = { .min = 2, .max = 8 },
  382. .p2 = { .dot_limit = 225000,
  383. .p2_slow = 7, .p2_fast = 7 },
  384. };
  385. /* LVDS 100mhz refclk limits. */
  386. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  387. .dot = { .min = 25000, .max = 350000 },
  388. .vco = { .min = 1760000, .max = 3510000 },
  389. .n = { .min = 1, .max = 2 },
  390. .m = { .min = 79, .max = 126 },
  391. .m1 = { .min = 12, .max = 22 },
  392. .m2 = { .min = 5, .max = 9 },
  393. .p = { .min = 28, .max = 112 },
  394. .p1 = { .min = 2, .max = 8 },
  395. .p2 = { .dot_limit = 225000,
  396. .p2_slow = 14, .p2_fast = 14 },
  397. };
  398. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  399. .dot = { .min = 25000, .max = 350000 },
  400. .vco = { .min = 1760000, .max = 3510000 },
  401. .n = { .min = 1, .max = 3 },
  402. .m = { .min = 79, .max = 126 },
  403. .m1 = { .min = 12, .max = 22 },
  404. .m2 = { .min = 5, .max = 9 },
  405. .p = { .min = 14, .max = 42 },
  406. .p1 = { .min = 2, .max = 6 },
  407. .p2 = { .dot_limit = 225000,
  408. .p2_slow = 7, .p2_fast = 7 },
  409. };
  410. static const intel_limit_t intel_limits_vlv = {
  411. /*
  412. * These are the data rate limits (measured in fast clocks)
  413. * since those are the strictest limits we have. The fast
  414. * clock and actual rate limits are more relaxed, so checking
  415. * them would make no difference.
  416. */
  417. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  418. .vco = { .min = 4000000, .max = 6000000 },
  419. .n = { .min = 1, .max = 7 },
  420. .m1 = { .min = 2, .max = 3 },
  421. .m2 = { .min = 11, .max = 156 },
  422. .p1 = { .min = 2, .max = 3 },
  423. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  424. };
  425. static const intel_limit_t intel_limits_chv = {
  426. /*
  427. * These are the data rate limits (measured in fast clocks)
  428. * since those are the strictest limits we have. The fast
  429. * clock and actual rate limits are more relaxed, so checking
  430. * them would make no difference.
  431. */
  432. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  433. .vco = { .min = 4800000, .max = 6480000 },
  434. .n = { .min = 1, .max = 1 },
  435. .m1 = { .min = 2, .max = 2 },
  436. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  437. .p1 = { .min = 2, .max = 4 },
  438. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  439. };
  440. static const intel_limit_t intel_limits_bxt = {
  441. /* FIXME: find real dot limits */
  442. .dot = { .min = 0, .max = INT_MAX },
  443. .vco = { .min = 4800000, .max = 6700000 },
  444. .n = { .min = 1, .max = 1 },
  445. .m1 = { .min = 2, .max = 2 },
  446. /* FIXME: find real m2 limits */
  447. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  448. .p1 = { .min = 2, .max = 4 },
  449. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  450. };
  451. static bool
  452. needs_modeset(struct drm_crtc_state *state)
  453. {
  454. return drm_atomic_crtc_needs_modeset(state);
  455. }
  456. /**
  457. * Returns whether any output on the specified pipe is of the specified type
  458. */
  459. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  460. {
  461. struct drm_device *dev = crtc->base.dev;
  462. struct intel_encoder *encoder;
  463. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  464. if (encoder->type == type)
  465. return true;
  466. return false;
  467. }
  468. /**
  469. * Returns whether any output on the specified pipe will have the specified
  470. * type after a staged modeset is complete, i.e., the same as
  471. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  472. * encoder->crtc.
  473. */
  474. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  475. int type)
  476. {
  477. struct drm_atomic_state *state = crtc_state->base.state;
  478. struct drm_connector *connector;
  479. struct drm_connector_state *connector_state;
  480. struct intel_encoder *encoder;
  481. int i, num_connectors = 0;
  482. for_each_connector_in_state(state, connector, connector_state, i) {
  483. if (connector_state->crtc != crtc_state->base.crtc)
  484. continue;
  485. num_connectors++;
  486. encoder = to_intel_encoder(connector_state->best_encoder);
  487. if (encoder->type == type)
  488. return true;
  489. }
  490. WARN_ON(num_connectors == 0);
  491. return false;
  492. }
  493. static const intel_limit_t *
  494. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  495. {
  496. struct drm_device *dev = crtc_state->base.crtc->dev;
  497. const intel_limit_t *limit;
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  499. if (intel_is_dual_link_lvds(dev)) {
  500. if (refclk == 100000)
  501. limit = &intel_limits_ironlake_dual_lvds_100m;
  502. else
  503. limit = &intel_limits_ironlake_dual_lvds;
  504. } else {
  505. if (refclk == 100000)
  506. limit = &intel_limits_ironlake_single_lvds_100m;
  507. else
  508. limit = &intel_limits_ironlake_single_lvds;
  509. }
  510. } else
  511. limit = &intel_limits_ironlake_dac;
  512. return limit;
  513. }
  514. static const intel_limit_t *
  515. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  516. {
  517. struct drm_device *dev = crtc_state->base.crtc->dev;
  518. const intel_limit_t *limit;
  519. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  520. if (intel_is_dual_link_lvds(dev))
  521. limit = &intel_limits_g4x_dual_channel_lvds;
  522. else
  523. limit = &intel_limits_g4x_single_channel_lvds;
  524. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  525. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  526. limit = &intel_limits_g4x_hdmi;
  527. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  528. limit = &intel_limits_g4x_sdvo;
  529. } else /* The option is for other outputs */
  530. limit = &intel_limits_i9xx_sdvo;
  531. return limit;
  532. }
  533. static const intel_limit_t *
  534. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  535. {
  536. struct drm_device *dev = crtc_state->base.crtc->dev;
  537. const intel_limit_t *limit;
  538. if (IS_BROXTON(dev))
  539. limit = &intel_limits_bxt;
  540. else if (HAS_PCH_SPLIT(dev))
  541. limit = intel_ironlake_limit(crtc_state, refclk);
  542. else if (IS_G4X(dev)) {
  543. limit = intel_g4x_limit(crtc_state);
  544. } else if (IS_PINEVIEW(dev)) {
  545. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  546. limit = &intel_limits_pineview_lvds;
  547. else
  548. limit = &intel_limits_pineview_sdvo;
  549. } else if (IS_CHERRYVIEW(dev)) {
  550. limit = &intel_limits_chv;
  551. } else if (IS_VALLEYVIEW(dev)) {
  552. limit = &intel_limits_vlv;
  553. } else if (!IS_GEN2(dev)) {
  554. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  555. limit = &intel_limits_i9xx_lvds;
  556. else
  557. limit = &intel_limits_i9xx_sdvo;
  558. } else {
  559. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  560. limit = &intel_limits_i8xx_lvds;
  561. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  562. limit = &intel_limits_i8xx_dvo;
  563. else
  564. limit = &intel_limits_i8xx_dac;
  565. }
  566. return limit;
  567. }
  568. /*
  569. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  570. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  571. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  572. * The helpers' return value is the rate of the clock that is fed to the
  573. * display engine's pipe which can be the above fast dot clock rate or a
  574. * divided-down version of it.
  575. */
  576. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  577. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  578. {
  579. clock->m = clock->m2 + 2;
  580. clock->p = clock->p1 * clock->p2;
  581. if (WARN_ON(clock->n == 0 || clock->p == 0))
  582. return 0;
  583. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  584. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  585. return clock->dot;
  586. }
  587. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  588. {
  589. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  590. }
  591. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  592. {
  593. clock->m = i9xx_dpll_compute_m(clock);
  594. clock->p = clock->p1 * clock->p2;
  595. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  596. return 0;
  597. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  598. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  599. return clock->dot;
  600. }
  601. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  602. {
  603. clock->m = clock->m1 * clock->m2;
  604. clock->p = clock->p1 * clock->p2;
  605. if (WARN_ON(clock->n == 0 || clock->p == 0))
  606. return 0;
  607. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  608. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  609. return clock->dot / 5;
  610. }
  611. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  612. {
  613. clock->m = clock->m1 * clock->m2;
  614. clock->p = clock->p1 * clock->p2;
  615. if (WARN_ON(clock->n == 0 || clock->p == 0))
  616. return 0;
  617. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  618. clock->n << 22);
  619. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  620. return clock->dot / 5;
  621. }
  622. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  623. /**
  624. * Returns whether the given set of divisors are valid for a given refclk with
  625. * the given connectors.
  626. */
  627. static bool intel_PLL_is_valid(struct drm_device *dev,
  628. const intel_limit_t *limit,
  629. const intel_clock_t *clock)
  630. {
  631. if (clock->n < limit->n.min || limit->n.max < clock->n)
  632. INTELPllInvalid("n out of range\n");
  633. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  634. INTELPllInvalid("p1 out of range\n");
  635. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  636. INTELPllInvalid("m2 out of range\n");
  637. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  638. INTELPllInvalid("m1 out of range\n");
  639. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  640. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  641. if (clock->m1 <= clock->m2)
  642. INTELPllInvalid("m1 <= m2\n");
  643. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  644. if (clock->p < limit->p.min || limit->p.max < clock->p)
  645. INTELPllInvalid("p out of range\n");
  646. if (clock->m < limit->m.min || limit->m.max < clock->m)
  647. INTELPllInvalid("m out of range\n");
  648. }
  649. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  650. INTELPllInvalid("vco out of range\n");
  651. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  652. * connector, etc., rather than just a single range.
  653. */
  654. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  655. INTELPllInvalid("dot out of range\n");
  656. return true;
  657. }
  658. static int
  659. i9xx_select_p2_div(const intel_limit_t *limit,
  660. const struct intel_crtc_state *crtc_state,
  661. int target)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  665. /*
  666. * For LVDS just rely on its current settings for dual-channel.
  667. * We haven't figured out how to reliably set up different
  668. * single/dual channel state, if we even can.
  669. */
  670. if (intel_is_dual_link_lvds(dev))
  671. return limit->p2.p2_fast;
  672. else
  673. return limit->p2.p2_slow;
  674. } else {
  675. if (target < limit->p2.dot_limit)
  676. return limit->p2.p2_slow;
  677. else
  678. return limit->p2.p2_fast;
  679. }
  680. }
  681. static bool
  682. i9xx_find_best_dpll(const intel_limit_t *limit,
  683. struct intel_crtc_state *crtc_state,
  684. int target, int refclk, intel_clock_t *match_clock,
  685. intel_clock_t *best_clock)
  686. {
  687. struct drm_device *dev = crtc_state->base.crtc->dev;
  688. intel_clock_t clock;
  689. int err = target;
  690. memset(best_clock, 0, sizeof(*best_clock));
  691. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  692. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  693. clock.m1++) {
  694. for (clock.m2 = limit->m2.min;
  695. clock.m2 <= limit->m2.max; clock.m2++) {
  696. if (clock.m2 >= clock.m1)
  697. break;
  698. for (clock.n = limit->n.min;
  699. clock.n <= limit->n.max; clock.n++) {
  700. for (clock.p1 = limit->p1.min;
  701. clock.p1 <= limit->p1.max; clock.p1++) {
  702. int this_err;
  703. i9xx_calc_dpll_params(refclk, &clock);
  704. if (!intel_PLL_is_valid(dev, limit,
  705. &clock))
  706. continue;
  707. if (match_clock &&
  708. clock.p != match_clock->p)
  709. continue;
  710. this_err = abs(clock.dot - target);
  711. if (this_err < err) {
  712. *best_clock = clock;
  713. err = this_err;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. return (err != target);
  720. }
  721. static bool
  722. pnv_find_best_dpll(const intel_limit_t *limit,
  723. struct intel_crtc_state *crtc_state,
  724. int target, int refclk, intel_clock_t *match_clock,
  725. intel_clock_t *best_clock)
  726. {
  727. struct drm_device *dev = crtc_state->base.crtc->dev;
  728. intel_clock_t clock;
  729. int err = target;
  730. memset(best_clock, 0, sizeof(*best_clock));
  731. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  732. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  733. clock.m1++) {
  734. for (clock.m2 = limit->m2.min;
  735. clock.m2 <= limit->m2.max; clock.m2++) {
  736. for (clock.n = limit->n.min;
  737. clock.n <= limit->n.max; clock.n++) {
  738. for (clock.p1 = limit->p1.min;
  739. clock.p1 <= limit->p1.max; clock.p1++) {
  740. int this_err;
  741. pnv_calc_dpll_params(refclk, &clock);
  742. if (!intel_PLL_is_valid(dev, limit,
  743. &clock))
  744. continue;
  745. if (match_clock &&
  746. clock.p != match_clock->p)
  747. continue;
  748. this_err = abs(clock.dot - target);
  749. if (this_err < err) {
  750. *best_clock = clock;
  751. err = this_err;
  752. }
  753. }
  754. }
  755. }
  756. }
  757. return (err != target);
  758. }
  759. static bool
  760. g4x_find_best_dpll(const intel_limit_t *limit,
  761. struct intel_crtc_state *crtc_state,
  762. int target, int refclk, intel_clock_t *match_clock,
  763. intel_clock_t *best_clock)
  764. {
  765. struct drm_device *dev = crtc_state->base.crtc->dev;
  766. intel_clock_t clock;
  767. int max_n;
  768. bool found = false;
  769. /* approximately equals target * 0.00585 */
  770. int err_most = (target >> 8) + (target >> 9);
  771. memset(best_clock, 0, sizeof(*best_clock));
  772. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  773. max_n = limit->n.max;
  774. /* based on hardware requirement, prefer smaller n to precision */
  775. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  776. /* based on hardware requirement, prefere larger m1,m2 */
  777. for (clock.m1 = limit->m1.max;
  778. clock.m1 >= limit->m1.min; clock.m1--) {
  779. for (clock.m2 = limit->m2.max;
  780. clock.m2 >= limit->m2.min; clock.m2--) {
  781. for (clock.p1 = limit->p1.max;
  782. clock.p1 >= limit->p1.min; clock.p1--) {
  783. int this_err;
  784. i9xx_calc_dpll_params(refclk, &clock);
  785. if (!intel_PLL_is_valid(dev, limit,
  786. &clock))
  787. continue;
  788. this_err = abs(clock.dot - target);
  789. if (this_err < err_most) {
  790. *best_clock = clock;
  791. err_most = this_err;
  792. max_n = clock.n;
  793. found = true;
  794. }
  795. }
  796. }
  797. }
  798. }
  799. return found;
  800. }
  801. /*
  802. * Check if the calculated PLL configuration is more optimal compared to the
  803. * best configuration and error found so far. Return the calculated error.
  804. */
  805. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  806. const intel_clock_t *calculated_clock,
  807. const intel_clock_t *best_clock,
  808. unsigned int best_error_ppm,
  809. unsigned int *error_ppm)
  810. {
  811. /*
  812. * For CHV ignore the error and consider only the P value.
  813. * Prefer a bigger P value based on HW requirements.
  814. */
  815. if (IS_CHERRYVIEW(dev)) {
  816. *error_ppm = 0;
  817. return calculated_clock->p > best_clock->p;
  818. }
  819. if (WARN_ON_ONCE(!target_freq))
  820. return false;
  821. *error_ppm = div_u64(1000000ULL *
  822. abs(target_freq - calculated_clock->dot),
  823. target_freq);
  824. /*
  825. * Prefer a better P value over a better (smaller) error if the error
  826. * is small. Ensure this preference for future configurations too by
  827. * setting the error to 0.
  828. */
  829. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  830. *error_ppm = 0;
  831. return true;
  832. }
  833. return *error_ppm + 10 < best_error_ppm;
  834. }
  835. static bool
  836. vlv_find_best_dpll(const intel_limit_t *limit,
  837. struct intel_crtc_state *crtc_state,
  838. int target, int refclk, intel_clock_t *match_clock,
  839. intel_clock_t *best_clock)
  840. {
  841. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  842. struct drm_device *dev = crtc->base.dev;
  843. intel_clock_t clock;
  844. unsigned int bestppm = 1000000;
  845. /* min update 19.2 MHz */
  846. int max_n = min(limit->n.max, refclk / 19200);
  847. bool found = false;
  848. target *= 5; /* fast clock */
  849. memset(best_clock, 0, sizeof(*best_clock));
  850. /* based on hardware requirement, prefer smaller n to precision */
  851. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  852. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  853. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  854. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  855. clock.p = clock.p1 * clock.p2;
  856. /* based on hardware requirement, prefer bigger m1,m2 values */
  857. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  858. unsigned int ppm;
  859. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  860. refclk * clock.m1);
  861. vlv_calc_dpll_params(refclk, &clock);
  862. if (!intel_PLL_is_valid(dev, limit,
  863. &clock))
  864. continue;
  865. if (!vlv_PLL_is_optimal(dev, target,
  866. &clock,
  867. best_clock,
  868. bestppm, &ppm))
  869. continue;
  870. *best_clock = clock;
  871. bestppm = ppm;
  872. found = true;
  873. }
  874. }
  875. }
  876. }
  877. return found;
  878. }
  879. static bool
  880. chv_find_best_dpll(const intel_limit_t *limit,
  881. struct intel_crtc_state *crtc_state,
  882. int target, int refclk, intel_clock_t *match_clock,
  883. intel_clock_t *best_clock)
  884. {
  885. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  886. struct drm_device *dev = crtc->base.dev;
  887. unsigned int best_error_ppm;
  888. intel_clock_t clock;
  889. uint64_t m2;
  890. int found = false;
  891. memset(best_clock, 0, sizeof(*best_clock));
  892. best_error_ppm = 1000000;
  893. /*
  894. * Based on hardware doc, the n always set to 1, and m1 always
  895. * set to 2. If requires to support 200Mhz refclk, we need to
  896. * revisit this because n may not 1 anymore.
  897. */
  898. clock.n = 1, clock.m1 = 2;
  899. target *= 5; /* fast clock */
  900. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  901. for (clock.p2 = limit->p2.p2_fast;
  902. clock.p2 >= limit->p2.p2_slow;
  903. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  904. unsigned int error_ppm;
  905. clock.p = clock.p1 * clock.p2;
  906. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  907. clock.n) << 22, refclk * clock.m1);
  908. if (m2 > INT_MAX/clock.m1)
  909. continue;
  910. clock.m2 = m2;
  911. chv_calc_dpll_params(refclk, &clock);
  912. if (!intel_PLL_is_valid(dev, limit, &clock))
  913. continue;
  914. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  915. best_error_ppm, &error_ppm))
  916. continue;
  917. *best_clock = clock;
  918. best_error_ppm = error_ppm;
  919. found = true;
  920. }
  921. }
  922. return found;
  923. }
  924. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  925. intel_clock_t *best_clock)
  926. {
  927. int refclk = i9xx_get_refclk(crtc_state, 0);
  928. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  929. target_clock, refclk, NULL, best_clock);
  930. }
  931. bool intel_crtc_active(struct drm_crtc *crtc)
  932. {
  933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  934. /* Be paranoid as we can arrive here with only partial
  935. * state retrieved from the hardware during setup.
  936. *
  937. * We can ditch the adjusted_mode.crtc_clock check as soon
  938. * as Haswell has gained clock readout/fastboot support.
  939. *
  940. * We can ditch the crtc->primary->fb check as soon as we can
  941. * properly reconstruct framebuffers.
  942. *
  943. * FIXME: The intel_crtc->active here should be switched to
  944. * crtc->state->active once we have proper CRTC states wired up
  945. * for atomic.
  946. */
  947. return intel_crtc->active && crtc->primary->state->fb &&
  948. intel_crtc->config->base.adjusted_mode.crtc_clock;
  949. }
  950. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  951. enum pipe pipe)
  952. {
  953. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  955. return intel_crtc->config->cpu_transcoder;
  956. }
  957. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  958. {
  959. struct drm_i915_private *dev_priv = dev->dev_private;
  960. i915_reg_t reg = PIPEDSL(pipe);
  961. u32 line1, line2;
  962. u32 line_mask;
  963. if (IS_GEN2(dev))
  964. line_mask = DSL_LINEMASK_GEN2;
  965. else
  966. line_mask = DSL_LINEMASK_GEN3;
  967. line1 = I915_READ(reg) & line_mask;
  968. msleep(5);
  969. line2 = I915_READ(reg) & line_mask;
  970. return line1 == line2;
  971. }
  972. /*
  973. * intel_wait_for_pipe_off - wait for pipe to turn off
  974. * @crtc: crtc whose pipe to wait for
  975. *
  976. * After disabling a pipe, we can't wait for vblank in the usual way,
  977. * spinning on the vblank interrupt status bit, since we won't actually
  978. * see an interrupt when the pipe is disabled.
  979. *
  980. * On Gen4 and above:
  981. * wait for the pipe register state bit to turn off
  982. *
  983. * Otherwise:
  984. * wait for the display line value to settle (it usually
  985. * ends up stopping at the start of the next frame).
  986. *
  987. */
  988. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  989. {
  990. struct drm_device *dev = crtc->base.dev;
  991. struct drm_i915_private *dev_priv = dev->dev_private;
  992. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  993. enum pipe pipe = crtc->pipe;
  994. if (INTEL_INFO(dev)->gen >= 4) {
  995. i915_reg_t reg = PIPECONF(cpu_transcoder);
  996. /* Wait for the Pipe State to go off */
  997. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  998. 100))
  999. WARN(1, "pipe_off wait timed out\n");
  1000. } else {
  1001. /* Wait for the display line to settle */
  1002. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  1003. WARN(1, "pipe_off wait timed out\n");
  1004. }
  1005. }
  1006. /* Only for pre-ILK configs */
  1007. void assert_pll(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. u32 val;
  1011. bool cur_state;
  1012. val = I915_READ(DPLL(pipe));
  1013. cur_state = !!(val & DPLL_VCO_ENABLE);
  1014. I915_STATE_WARN(cur_state != state,
  1015. "PLL state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1019. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1020. {
  1021. u32 val;
  1022. bool cur_state;
  1023. mutex_lock(&dev_priv->sb_lock);
  1024. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1025. mutex_unlock(&dev_priv->sb_lock);
  1026. cur_state = val & DSI_PLL_VCO_EN;
  1027. I915_STATE_WARN(cur_state != state,
  1028. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1029. onoff(state), onoff(cur_state));
  1030. }
  1031. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1032. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1033. struct intel_shared_dpll *
  1034. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1035. {
  1036. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1037. if (crtc->config->shared_dpll < 0)
  1038. return NULL;
  1039. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1040. }
  1041. /* For ILK+ */
  1042. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1043. struct intel_shared_dpll *pll,
  1044. bool state)
  1045. {
  1046. bool cur_state;
  1047. struct intel_dpll_hw_state hw_state;
  1048. if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
  1049. return;
  1050. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1051. I915_STATE_WARN(cur_state != state,
  1052. "%s assertion failure (expected %s, current %s)\n",
  1053. pll->name, onoff(state), onoff(cur_state));
  1054. }
  1055. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. bool cur_state;
  1059. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1060. pipe);
  1061. if (HAS_DDI(dev_priv->dev)) {
  1062. /* DDI does not have a specific FDI_TX register */
  1063. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1064. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1065. } else {
  1066. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1067. cur_state = !!(val & FDI_TX_ENABLE);
  1068. }
  1069. I915_STATE_WARN(cur_state != state,
  1070. "FDI TX state assertion failure (expected %s, current %s)\n",
  1071. onoff(state), onoff(cur_state));
  1072. }
  1073. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1074. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1075. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe, bool state)
  1077. {
  1078. u32 val;
  1079. bool cur_state;
  1080. val = I915_READ(FDI_RX_CTL(pipe));
  1081. cur_state = !!(val & FDI_RX_ENABLE);
  1082. I915_STATE_WARN(cur_state != state,
  1083. "FDI RX state assertion failure (expected %s, current %s)\n",
  1084. onoff(state), onoff(cur_state));
  1085. }
  1086. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1087. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1088. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. u32 val;
  1092. /* ILK FDI PLL is always enabled */
  1093. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1094. return;
  1095. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1096. if (HAS_DDI(dev_priv->dev))
  1097. return;
  1098. val = I915_READ(FDI_TX_CTL(pipe));
  1099. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1100. }
  1101. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. u32 val;
  1105. bool cur_state;
  1106. val = I915_READ(FDI_RX_CTL(pipe));
  1107. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1108. I915_STATE_WARN(cur_state != state,
  1109. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1110. onoff(state), onoff(cur_state));
  1111. }
  1112. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1113. enum pipe pipe)
  1114. {
  1115. struct drm_device *dev = dev_priv->dev;
  1116. i915_reg_t pp_reg;
  1117. u32 val;
  1118. enum pipe panel_pipe = PIPE_A;
  1119. bool locked = true;
  1120. if (WARN_ON(HAS_DDI(dev)))
  1121. return;
  1122. if (HAS_PCH_SPLIT(dev)) {
  1123. u32 port_sel;
  1124. pp_reg = PCH_PP_CONTROL;
  1125. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1126. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1127. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1128. panel_pipe = PIPE_B;
  1129. /* XXX: else fix for eDP */
  1130. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1131. /* presumably write lock depends on pipe, not port select */
  1132. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1133. panel_pipe = pipe;
  1134. } else {
  1135. pp_reg = PP_CONTROL;
  1136. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1137. panel_pipe = PIPE_B;
  1138. }
  1139. val = I915_READ(pp_reg);
  1140. if (!(val & PANEL_POWER_ON) ||
  1141. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1142. locked = false;
  1143. I915_STATE_WARN(panel_pipe == pipe && locked,
  1144. "panel assertion failure, pipe %c regs locked\n",
  1145. pipe_name(pipe));
  1146. }
  1147. static void assert_cursor(struct drm_i915_private *dev_priv,
  1148. enum pipe pipe, bool state)
  1149. {
  1150. struct drm_device *dev = dev_priv->dev;
  1151. bool cur_state;
  1152. if (IS_845G(dev) || IS_I865G(dev))
  1153. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1154. else
  1155. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1156. I915_STATE_WARN(cur_state != state,
  1157. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1158. pipe_name(pipe), onoff(state), onoff(cur_state));
  1159. }
  1160. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1161. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1162. void assert_pipe(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe, bool state)
  1164. {
  1165. bool cur_state;
  1166. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1167. pipe);
  1168. enum intel_display_power_domain power_domain;
  1169. /* if we need the pipe quirk it must be always on */
  1170. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1171. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1172. state = true;
  1173. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1174. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1175. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1176. cur_state = !!(val & PIPECONF_ENABLE);
  1177. intel_display_power_put(dev_priv, power_domain);
  1178. } else {
  1179. cur_state = false;
  1180. }
  1181. I915_STATE_WARN(cur_state != state,
  1182. "pipe %c assertion failure (expected %s, current %s)\n",
  1183. pipe_name(pipe), onoff(state), onoff(cur_state));
  1184. }
  1185. static void assert_plane(struct drm_i915_private *dev_priv,
  1186. enum plane plane, bool state)
  1187. {
  1188. u32 val;
  1189. bool cur_state;
  1190. val = I915_READ(DSPCNTR(plane));
  1191. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1192. I915_STATE_WARN(cur_state != state,
  1193. "plane %c assertion failure (expected %s, current %s)\n",
  1194. plane_name(plane), onoff(state), onoff(cur_state));
  1195. }
  1196. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1197. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1198. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe)
  1200. {
  1201. struct drm_device *dev = dev_priv->dev;
  1202. int i;
  1203. /* Primary planes are fixed to pipes on gen4+ */
  1204. if (INTEL_INFO(dev)->gen >= 4) {
  1205. u32 val = I915_READ(DSPCNTR(pipe));
  1206. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1207. "plane %c assertion failure, should be disabled but not\n",
  1208. plane_name(pipe));
  1209. return;
  1210. }
  1211. /* Need to check both planes against the pipe */
  1212. for_each_pipe(dev_priv, i) {
  1213. u32 val = I915_READ(DSPCNTR(i));
  1214. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1215. DISPPLANE_SEL_PIPE_SHIFT;
  1216. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1217. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1218. plane_name(i), pipe_name(pipe));
  1219. }
  1220. }
  1221. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe)
  1223. {
  1224. struct drm_device *dev = dev_priv->dev;
  1225. int sprite;
  1226. if (INTEL_INFO(dev)->gen >= 9) {
  1227. for_each_sprite(dev_priv, pipe, sprite) {
  1228. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1229. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1230. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1231. sprite, pipe_name(pipe));
  1232. }
  1233. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1234. for_each_sprite(dev_priv, pipe, sprite) {
  1235. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1236. I915_STATE_WARN(val & SP_ENABLE,
  1237. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1238. sprite_name(pipe, sprite), pipe_name(pipe));
  1239. }
  1240. } else if (INTEL_INFO(dev)->gen >= 7) {
  1241. u32 val = I915_READ(SPRCTL(pipe));
  1242. I915_STATE_WARN(val & SPRITE_ENABLE,
  1243. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1244. plane_name(pipe), pipe_name(pipe));
  1245. } else if (INTEL_INFO(dev)->gen >= 5) {
  1246. u32 val = I915_READ(DVSCNTR(pipe));
  1247. I915_STATE_WARN(val & DVS_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. plane_name(pipe), pipe_name(pipe));
  1250. }
  1251. }
  1252. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1253. {
  1254. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1255. drm_crtc_vblank_put(crtc);
  1256. }
  1257. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1258. {
  1259. u32 val;
  1260. bool enabled;
  1261. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1262. val = I915_READ(PCH_DREF_CONTROL);
  1263. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1264. DREF_SUPERSPREAD_SOURCE_MASK));
  1265. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1266. }
  1267. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe)
  1269. {
  1270. u32 val;
  1271. bool enabled;
  1272. val = I915_READ(PCH_TRANSCONF(pipe));
  1273. enabled = !!(val & TRANS_ENABLE);
  1274. I915_STATE_WARN(enabled,
  1275. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1276. pipe_name(pipe));
  1277. }
  1278. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, u32 port_sel, u32 val)
  1280. {
  1281. if ((val & DP_PORT_EN) == 0)
  1282. return false;
  1283. if (HAS_PCH_CPT(dev_priv->dev)) {
  1284. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1285. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1286. return false;
  1287. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1288. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1289. return false;
  1290. } else {
  1291. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1292. return false;
  1293. }
  1294. return true;
  1295. }
  1296. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1297. enum pipe pipe, u32 val)
  1298. {
  1299. if ((val & SDVO_ENABLE) == 0)
  1300. return false;
  1301. if (HAS_PCH_CPT(dev_priv->dev)) {
  1302. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1303. return false;
  1304. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1305. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1306. return false;
  1307. } else {
  1308. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1309. return false;
  1310. }
  1311. return true;
  1312. }
  1313. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1314. enum pipe pipe, u32 val)
  1315. {
  1316. if ((val & LVDS_PORT_EN) == 0)
  1317. return false;
  1318. if (HAS_PCH_CPT(dev_priv->dev)) {
  1319. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1320. return false;
  1321. } else {
  1322. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1323. return false;
  1324. }
  1325. return true;
  1326. }
  1327. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1328. enum pipe pipe, u32 val)
  1329. {
  1330. if ((val & ADPA_DAC_ENABLE) == 0)
  1331. return false;
  1332. if (HAS_PCH_CPT(dev_priv->dev)) {
  1333. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1334. return false;
  1335. } else {
  1336. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1337. return false;
  1338. }
  1339. return true;
  1340. }
  1341. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1342. enum pipe pipe, i915_reg_t reg,
  1343. u32 port_sel)
  1344. {
  1345. u32 val = I915_READ(reg);
  1346. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1347. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1348. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1349. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1350. && (val & DP_PIPEB_SELECT),
  1351. "IBX PCH dp port still using transcoder B\n");
  1352. }
  1353. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe, i915_reg_t reg)
  1355. {
  1356. u32 val = I915_READ(reg);
  1357. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1358. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1359. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1360. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1361. && (val & SDVO_PIPE_B_SELECT),
  1362. "IBX PCH hdmi port still using transcoder B\n");
  1363. }
  1364. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1365. enum pipe pipe)
  1366. {
  1367. u32 val;
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1369. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1370. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1371. val = I915_READ(PCH_ADPA);
  1372. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1374. pipe_name(pipe));
  1375. val = I915_READ(PCH_LVDS);
  1376. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1377. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1378. pipe_name(pipe));
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1380. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1381. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1382. }
  1383. static void vlv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_device *dev = crtc->base.dev;
  1387. struct drm_i915_private *dev_priv = dev->dev_private;
  1388. i915_reg_t reg = DPLL(crtc->pipe);
  1389. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1390. assert_pipe_disabled(dev_priv, crtc->pipe);
  1391. /* PLL is protected by panel, make sure we can write it */
  1392. if (IS_MOBILE(dev_priv->dev))
  1393. assert_panel_unlocked(dev_priv, crtc->pipe);
  1394. I915_WRITE(reg, dpll);
  1395. POSTING_READ(reg);
  1396. udelay(150);
  1397. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1398. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1399. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1400. POSTING_READ(DPLL_MD(crtc->pipe));
  1401. /* We do this three times for luck */
  1402. I915_WRITE(reg, dpll);
  1403. POSTING_READ(reg);
  1404. udelay(150); /* wait for warmup */
  1405. I915_WRITE(reg, dpll);
  1406. POSTING_READ(reg);
  1407. udelay(150); /* wait for warmup */
  1408. I915_WRITE(reg, dpll);
  1409. POSTING_READ(reg);
  1410. udelay(150); /* wait for warmup */
  1411. }
  1412. static void chv_enable_pll(struct intel_crtc *crtc,
  1413. const struct intel_crtc_state *pipe_config)
  1414. {
  1415. struct drm_device *dev = crtc->base.dev;
  1416. struct drm_i915_private *dev_priv = dev->dev_private;
  1417. int pipe = crtc->pipe;
  1418. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1419. u32 tmp;
  1420. assert_pipe_disabled(dev_priv, crtc->pipe);
  1421. mutex_lock(&dev_priv->sb_lock);
  1422. /* Enable back the 10bit clock to display controller */
  1423. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1424. tmp |= DPIO_DCLKP_EN;
  1425. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1426. mutex_unlock(&dev_priv->sb_lock);
  1427. /*
  1428. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1429. */
  1430. udelay(1);
  1431. /* Enable PLL */
  1432. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1433. /* Check PLL is locked */
  1434. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1435. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1436. /* not sure when this should be written */
  1437. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1438. POSTING_READ(DPLL_MD(pipe));
  1439. }
  1440. static int intel_num_dvo_pipes(struct drm_device *dev)
  1441. {
  1442. struct intel_crtc *crtc;
  1443. int count = 0;
  1444. for_each_intel_crtc(dev, crtc)
  1445. count += crtc->base.state->active &&
  1446. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1447. return count;
  1448. }
  1449. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1450. {
  1451. struct drm_device *dev = crtc->base.dev;
  1452. struct drm_i915_private *dev_priv = dev->dev_private;
  1453. i915_reg_t reg = DPLL(crtc->pipe);
  1454. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1455. assert_pipe_disabled(dev_priv, crtc->pipe);
  1456. /* No really, not for ILK+ */
  1457. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1458. /* PLL is protected by panel, make sure we can write it */
  1459. if (IS_MOBILE(dev) && !IS_I830(dev))
  1460. assert_panel_unlocked(dev_priv, crtc->pipe);
  1461. /* Enable DVO 2x clock on both PLLs if necessary */
  1462. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1463. /*
  1464. * It appears to be important that we don't enable this
  1465. * for the current pipe before otherwise configuring the
  1466. * PLL. No idea how this should be handled if multiple
  1467. * DVO outputs are enabled simultaneosly.
  1468. */
  1469. dpll |= DPLL_DVO_2X_MODE;
  1470. I915_WRITE(DPLL(!crtc->pipe),
  1471. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1472. }
  1473. /*
  1474. * Apparently we need to have VGA mode enabled prior to changing
  1475. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1476. * dividers, even though the register value does change.
  1477. */
  1478. I915_WRITE(reg, 0);
  1479. I915_WRITE(reg, dpll);
  1480. /* Wait for the clocks to stabilize. */
  1481. POSTING_READ(reg);
  1482. udelay(150);
  1483. if (INTEL_INFO(dev)->gen >= 4) {
  1484. I915_WRITE(DPLL_MD(crtc->pipe),
  1485. crtc->config->dpll_hw_state.dpll_md);
  1486. } else {
  1487. /* The pixel multiplier can only be updated once the
  1488. * DPLL is enabled and the clocks are stable.
  1489. *
  1490. * So write it again.
  1491. */
  1492. I915_WRITE(reg, dpll);
  1493. }
  1494. /* We do this three times for luck */
  1495. I915_WRITE(reg, dpll);
  1496. POSTING_READ(reg);
  1497. udelay(150); /* wait for warmup */
  1498. I915_WRITE(reg, dpll);
  1499. POSTING_READ(reg);
  1500. udelay(150); /* wait for warmup */
  1501. I915_WRITE(reg, dpll);
  1502. POSTING_READ(reg);
  1503. udelay(150); /* wait for warmup */
  1504. }
  1505. /**
  1506. * i9xx_disable_pll - disable a PLL
  1507. * @dev_priv: i915 private structure
  1508. * @pipe: pipe PLL to disable
  1509. *
  1510. * Disable the PLL for @pipe, making sure the pipe is off first.
  1511. *
  1512. * Note! This is for pre-ILK only.
  1513. */
  1514. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1515. {
  1516. struct drm_device *dev = crtc->base.dev;
  1517. struct drm_i915_private *dev_priv = dev->dev_private;
  1518. enum pipe pipe = crtc->pipe;
  1519. /* Disable DVO 2x clock on both PLLs if necessary */
  1520. if (IS_I830(dev) &&
  1521. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1522. !intel_num_dvo_pipes(dev)) {
  1523. I915_WRITE(DPLL(PIPE_B),
  1524. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1525. I915_WRITE(DPLL(PIPE_A),
  1526. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1527. }
  1528. /* Don't disable pipe or pipe PLLs if needed */
  1529. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1530. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1531. return;
  1532. /* Make sure the pipe isn't still relying on us */
  1533. assert_pipe_disabled(dev_priv, pipe);
  1534. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1535. POSTING_READ(DPLL(pipe));
  1536. }
  1537. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1538. {
  1539. u32 val;
  1540. /* Make sure the pipe isn't still relying on us */
  1541. assert_pipe_disabled(dev_priv, pipe);
  1542. /*
  1543. * Leave integrated clock source and reference clock enabled for pipe B.
  1544. * The latter is needed for VGA hotplug / manual detection.
  1545. */
  1546. val = DPLL_VGA_MODE_DIS;
  1547. if (pipe == PIPE_B)
  1548. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1549. I915_WRITE(DPLL(pipe), val);
  1550. POSTING_READ(DPLL(pipe));
  1551. }
  1552. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1553. {
  1554. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1555. u32 val;
  1556. /* Make sure the pipe isn't still relying on us */
  1557. assert_pipe_disabled(dev_priv, pipe);
  1558. /* Set PLL en = 0 */
  1559. val = DPLL_SSC_REF_CLK_CHV |
  1560. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1561. if (pipe != PIPE_A)
  1562. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1563. I915_WRITE(DPLL(pipe), val);
  1564. POSTING_READ(DPLL(pipe));
  1565. mutex_lock(&dev_priv->sb_lock);
  1566. /* Disable 10bit clock to display controller */
  1567. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1568. val &= ~DPIO_DCLKP_EN;
  1569. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1570. mutex_unlock(&dev_priv->sb_lock);
  1571. }
  1572. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1573. struct intel_digital_port *dport,
  1574. unsigned int expected_mask)
  1575. {
  1576. u32 port_mask;
  1577. i915_reg_t dpll_reg;
  1578. switch (dport->port) {
  1579. case PORT_B:
  1580. port_mask = DPLL_PORTB_READY_MASK;
  1581. dpll_reg = DPLL(0);
  1582. break;
  1583. case PORT_C:
  1584. port_mask = DPLL_PORTC_READY_MASK;
  1585. dpll_reg = DPLL(0);
  1586. expected_mask <<= 4;
  1587. break;
  1588. case PORT_D:
  1589. port_mask = DPLL_PORTD_READY_MASK;
  1590. dpll_reg = DPIO_PHY_STATUS;
  1591. break;
  1592. default:
  1593. BUG();
  1594. }
  1595. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1596. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1597. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1598. }
  1599. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1600. {
  1601. struct drm_device *dev = crtc->base.dev;
  1602. struct drm_i915_private *dev_priv = dev->dev_private;
  1603. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1604. if (WARN_ON(pll == NULL))
  1605. return;
  1606. WARN_ON(!pll->config.crtc_mask);
  1607. if (pll->active == 0) {
  1608. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1609. WARN_ON(pll->on);
  1610. assert_shared_dpll_disabled(dev_priv, pll);
  1611. pll->mode_set(dev_priv, pll);
  1612. }
  1613. }
  1614. /**
  1615. * intel_enable_shared_dpll - enable PCH PLL
  1616. * @dev_priv: i915 private structure
  1617. * @pipe: pipe PLL to enable
  1618. *
  1619. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1620. * drives the transcoder clock.
  1621. */
  1622. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1623. {
  1624. struct drm_device *dev = crtc->base.dev;
  1625. struct drm_i915_private *dev_priv = dev->dev_private;
  1626. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1627. if (WARN_ON(pll == NULL))
  1628. return;
  1629. if (WARN_ON(pll->config.crtc_mask == 0))
  1630. return;
  1631. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1632. pll->name, pll->active, pll->on,
  1633. crtc->base.base.id);
  1634. if (pll->active++) {
  1635. WARN_ON(!pll->on);
  1636. assert_shared_dpll_enabled(dev_priv, pll);
  1637. return;
  1638. }
  1639. WARN_ON(pll->on);
  1640. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1641. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1642. pll->enable(dev_priv, pll);
  1643. pll->on = true;
  1644. }
  1645. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1646. {
  1647. struct drm_device *dev = crtc->base.dev;
  1648. struct drm_i915_private *dev_priv = dev->dev_private;
  1649. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1650. /* PCH only available on ILK+ */
  1651. if (INTEL_INFO(dev)->gen < 5)
  1652. return;
  1653. if (pll == NULL)
  1654. return;
  1655. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1656. return;
  1657. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1658. pll->name, pll->active, pll->on,
  1659. crtc->base.base.id);
  1660. if (WARN_ON(pll->active == 0)) {
  1661. assert_shared_dpll_disabled(dev_priv, pll);
  1662. return;
  1663. }
  1664. assert_shared_dpll_enabled(dev_priv, pll);
  1665. WARN_ON(!pll->on);
  1666. if (--pll->active)
  1667. return;
  1668. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1669. pll->disable(dev_priv, pll);
  1670. pll->on = false;
  1671. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1672. }
  1673. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1674. enum pipe pipe)
  1675. {
  1676. struct drm_device *dev = dev_priv->dev;
  1677. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1678. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1679. i915_reg_t reg;
  1680. uint32_t val, pipeconf_val;
  1681. /* PCH only available on ILK+ */
  1682. BUG_ON(!HAS_PCH_SPLIT(dev));
  1683. /* Make sure PCH DPLL is enabled */
  1684. assert_shared_dpll_enabled(dev_priv,
  1685. intel_crtc_to_shared_dpll(intel_crtc));
  1686. /* FDI must be feeding us bits for PCH ports */
  1687. assert_fdi_tx_enabled(dev_priv, pipe);
  1688. assert_fdi_rx_enabled(dev_priv, pipe);
  1689. if (HAS_PCH_CPT(dev)) {
  1690. /* Workaround: Set the timing override bit before enabling the
  1691. * pch transcoder. */
  1692. reg = TRANS_CHICKEN2(pipe);
  1693. val = I915_READ(reg);
  1694. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1695. I915_WRITE(reg, val);
  1696. }
  1697. reg = PCH_TRANSCONF(pipe);
  1698. val = I915_READ(reg);
  1699. pipeconf_val = I915_READ(PIPECONF(pipe));
  1700. if (HAS_PCH_IBX(dev_priv->dev)) {
  1701. /*
  1702. * Make the BPC in transcoder be consistent with
  1703. * that in pipeconf reg. For HDMI we must use 8bpc
  1704. * here for both 8bpc and 12bpc.
  1705. */
  1706. val &= ~PIPECONF_BPC_MASK;
  1707. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1708. val |= PIPECONF_8BPC;
  1709. else
  1710. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1711. }
  1712. val &= ~TRANS_INTERLACE_MASK;
  1713. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1714. if (HAS_PCH_IBX(dev_priv->dev) &&
  1715. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1716. val |= TRANS_LEGACY_INTERLACED_ILK;
  1717. else
  1718. val |= TRANS_INTERLACED;
  1719. else
  1720. val |= TRANS_PROGRESSIVE;
  1721. I915_WRITE(reg, val | TRANS_ENABLE);
  1722. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1723. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1724. }
  1725. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1726. enum transcoder cpu_transcoder)
  1727. {
  1728. u32 val, pipeconf_val;
  1729. /* PCH only available on ILK+ */
  1730. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1731. /* FDI must be feeding us bits for PCH ports */
  1732. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1733. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1734. /* Workaround: set timing override bit. */
  1735. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1736. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1737. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1738. val = TRANS_ENABLE;
  1739. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1740. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1741. PIPECONF_INTERLACED_ILK)
  1742. val |= TRANS_INTERLACED;
  1743. else
  1744. val |= TRANS_PROGRESSIVE;
  1745. I915_WRITE(LPT_TRANSCONF, val);
  1746. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1747. DRM_ERROR("Failed to enable PCH transcoder\n");
  1748. }
  1749. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1750. enum pipe pipe)
  1751. {
  1752. struct drm_device *dev = dev_priv->dev;
  1753. i915_reg_t reg;
  1754. uint32_t val;
  1755. /* FDI relies on the transcoder */
  1756. assert_fdi_tx_disabled(dev_priv, pipe);
  1757. assert_fdi_rx_disabled(dev_priv, pipe);
  1758. /* Ports must be off as well */
  1759. assert_pch_ports_disabled(dev_priv, pipe);
  1760. reg = PCH_TRANSCONF(pipe);
  1761. val = I915_READ(reg);
  1762. val &= ~TRANS_ENABLE;
  1763. I915_WRITE(reg, val);
  1764. /* wait for PCH transcoder off, transcoder state */
  1765. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1766. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1767. if (HAS_PCH_CPT(dev)) {
  1768. /* Workaround: Clear the timing override chicken bit again. */
  1769. reg = TRANS_CHICKEN2(pipe);
  1770. val = I915_READ(reg);
  1771. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1772. I915_WRITE(reg, val);
  1773. }
  1774. }
  1775. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1776. {
  1777. u32 val;
  1778. val = I915_READ(LPT_TRANSCONF);
  1779. val &= ~TRANS_ENABLE;
  1780. I915_WRITE(LPT_TRANSCONF, val);
  1781. /* wait for PCH transcoder off, transcoder state */
  1782. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1783. DRM_ERROR("Failed to disable PCH transcoder\n");
  1784. /* Workaround: clear timing override bit. */
  1785. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1786. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1787. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1788. }
  1789. /**
  1790. * intel_enable_pipe - enable a pipe, asserting requirements
  1791. * @crtc: crtc responsible for the pipe
  1792. *
  1793. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1794. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1795. */
  1796. static void intel_enable_pipe(struct intel_crtc *crtc)
  1797. {
  1798. struct drm_device *dev = crtc->base.dev;
  1799. struct drm_i915_private *dev_priv = dev->dev_private;
  1800. enum pipe pipe = crtc->pipe;
  1801. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1802. enum pipe pch_transcoder;
  1803. i915_reg_t reg;
  1804. u32 val;
  1805. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1806. assert_planes_disabled(dev_priv, pipe);
  1807. assert_cursor_disabled(dev_priv, pipe);
  1808. assert_sprites_disabled(dev_priv, pipe);
  1809. if (HAS_PCH_LPT(dev_priv->dev))
  1810. pch_transcoder = TRANSCODER_A;
  1811. else
  1812. pch_transcoder = pipe;
  1813. /*
  1814. * A pipe without a PLL won't actually be able to drive bits from
  1815. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1816. * need the check.
  1817. */
  1818. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1819. if (crtc->config->has_dsi_encoder)
  1820. assert_dsi_pll_enabled(dev_priv);
  1821. else
  1822. assert_pll_enabled(dev_priv, pipe);
  1823. else {
  1824. if (crtc->config->has_pch_encoder) {
  1825. /* if driving the PCH, we need FDI enabled */
  1826. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1827. assert_fdi_tx_pll_enabled(dev_priv,
  1828. (enum pipe) cpu_transcoder);
  1829. }
  1830. /* FIXME: assert CPU port conditions for SNB+ */
  1831. }
  1832. reg = PIPECONF(cpu_transcoder);
  1833. val = I915_READ(reg);
  1834. if (val & PIPECONF_ENABLE) {
  1835. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1836. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1837. return;
  1838. }
  1839. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1840. POSTING_READ(reg);
  1841. /*
  1842. * Until the pipe starts DSL will read as 0, which would cause
  1843. * an apparent vblank timestamp jump, which messes up also the
  1844. * frame count when it's derived from the timestamps. So let's
  1845. * wait for the pipe to start properly before we call
  1846. * drm_crtc_vblank_on()
  1847. */
  1848. if (dev->max_vblank_count == 0 &&
  1849. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1850. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1851. }
  1852. /**
  1853. * intel_disable_pipe - disable a pipe, asserting requirements
  1854. * @crtc: crtc whose pipes is to be disabled
  1855. *
  1856. * Disable the pipe of @crtc, making sure that various hardware
  1857. * specific requirements are met, if applicable, e.g. plane
  1858. * disabled, panel fitter off, etc.
  1859. *
  1860. * Will wait until the pipe has shut down before returning.
  1861. */
  1862. static void intel_disable_pipe(struct intel_crtc *crtc)
  1863. {
  1864. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1865. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1866. enum pipe pipe = crtc->pipe;
  1867. i915_reg_t reg;
  1868. u32 val;
  1869. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1870. /*
  1871. * Make sure planes won't keep trying to pump pixels to us,
  1872. * or we might hang the display.
  1873. */
  1874. assert_planes_disabled(dev_priv, pipe);
  1875. assert_cursor_disabled(dev_priv, pipe);
  1876. assert_sprites_disabled(dev_priv, pipe);
  1877. reg = PIPECONF(cpu_transcoder);
  1878. val = I915_READ(reg);
  1879. if ((val & PIPECONF_ENABLE) == 0)
  1880. return;
  1881. /*
  1882. * Double wide has implications for planes
  1883. * so best keep it disabled when not needed.
  1884. */
  1885. if (crtc->config->double_wide)
  1886. val &= ~PIPECONF_DOUBLE_WIDE;
  1887. /* Don't disable pipe or pipe PLLs if needed */
  1888. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1889. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1890. val &= ~PIPECONF_ENABLE;
  1891. I915_WRITE(reg, val);
  1892. if ((val & PIPECONF_ENABLE) == 0)
  1893. intel_wait_for_pipe_off(crtc);
  1894. }
  1895. static bool need_vtd_wa(struct drm_device *dev)
  1896. {
  1897. #ifdef CONFIG_INTEL_IOMMU
  1898. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1899. return true;
  1900. #endif
  1901. return false;
  1902. }
  1903. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1904. {
  1905. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1906. }
  1907. static unsigned int intel_tile_width(const struct drm_i915_private *dev_priv,
  1908. uint64_t fb_modifier, unsigned int cpp)
  1909. {
  1910. switch (fb_modifier) {
  1911. case DRM_FORMAT_MOD_NONE:
  1912. return cpp;
  1913. case I915_FORMAT_MOD_X_TILED:
  1914. if (IS_GEN2(dev_priv))
  1915. return 128;
  1916. else
  1917. return 512;
  1918. case I915_FORMAT_MOD_Y_TILED:
  1919. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1920. return 128;
  1921. else
  1922. return 512;
  1923. case I915_FORMAT_MOD_Yf_TILED:
  1924. switch (cpp) {
  1925. case 1:
  1926. return 64;
  1927. case 2:
  1928. case 4:
  1929. return 128;
  1930. case 8:
  1931. case 16:
  1932. return 256;
  1933. default:
  1934. MISSING_CASE(cpp);
  1935. return cpp;
  1936. }
  1937. break;
  1938. default:
  1939. MISSING_CASE(fb_modifier);
  1940. return cpp;
  1941. }
  1942. }
  1943. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1944. uint64_t fb_modifier, unsigned int cpp)
  1945. {
  1946. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1947. return 1;
  1948. else
  1949. return intel_tile_size(dev_priv) /
  1950. intel_tile_width(dev_priv, fb_modifier, cpp);
  1951. }
  1952. unsigned int
  1953. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1954. uint32_t pixel_format, uint64_t fb_modifier)
  1955. {
  1956. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1957. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1958. return ALIGN(height, tile_height);
  1959. }
  1960. static void
  1961. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1962. const struct drm_plane_state *plane_state)
  1963. {
  1964. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1965. struct intel_rotation_info *info = &view->params.rotated;
  1966. unsigned int tile_size, tile_width, tile_height, cpp;
  1967. *view = i915_ggtt_view_normal;
  1968. if (!plane_state)
  1969. return;
  1970. if (!intel_rotation_90_or_270(plane_state->rotation))
  1971. return;
  1972. *view = i915_ggtt_view_rotated;
  1973. info->height = fb->height;
  1974. info->pixel_format = fb->pixel_format;
  1975. info->pitch = fb->pitches[0];
  1976. info->uv_offset = fb->offsets[1];
  1977. info->fb_modifier = fb->modifier[0];
  1978. tile_size = intel_tile_size(dev_priv);
  1979. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1980. tile_width = intel_tile_width(dev_priv, fb->modifier[0], cpp);
  1981. tile_height = tile_size / tile_width;
  1982. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_width);
  1983. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1984. info->size = info->width_pages * info->height_pages * tile_size;
  1985. if (info->pixel_format == DRM_FORMAT_NV12) {
  1986. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1987. tile_width = intel_tile_width(dev_priv, fb->modifier[1], cpp);
  1988. tile_height = tile_size / tile_width;
  1989. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[1], tile_width);
  1990. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2, tile_height);
  1991. info->size_uv = info->width_pages_uv * info->height_pages_uv * tile_size;
  1992. }
  1993. }
  1994. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1995. {
  1996. if (INTEL_INFO(dev_priv)->gen >= 9)
  1997. return 256 * 1024;
  1998. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1999. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  2000. return 128 * 1024;
  2001. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2002. return 4 * 1024;
  2003. else
  2004. return 0;
  2005. }
  2006. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  2007. uint64_t fb_modifier)
  2008. {
  2009. switch (fb_modifier) {
  2010. case DRM_FORMAT_MOD_NONE:
  2011. return intel_linear_alignment(dev_priv);
  2012. case I915_FORMAT_MOD_X_TILED:
  2013. if (INTEL_INFO(dev_priv)->gen >= 9)
  2014. return 256 * 1024;
  2015. return 0;
  2016. case I915_FORMAT_MOD_Y_TILED:
  2017. case I915_FORMAT_MOD_Yf_TILED:
  2018. return 1 * 1024 * 1024;
  2019. default:
  2020. MISSING_CASE(fb_modifier);
  2021. return 0;
  2022. }
  2023. }
  2024. int
  2025. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2026. struct drm_framebuffer *fb,
  2027. const struct drm_plane_state *plane_state)
  2028. {
  2029. struct drm_device *dev = fb->dev;
  2030. struct drm_i915_private *dev_priv = dev->dev_private;
  2031. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2032. struct i915_ggtt_view view;
  2033. u32 alignment;
  2034. int ret;
  2035. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2036. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2037. intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2038. /* Note that the w/a also requires 64 PTE of padding following the
  2039. * bo. We currently fill all unused PTE with the shadow page and so
  2040. * we should always have valid PTE following the scanout preventing
  2041. * the VT-d warning.
  2042. */
  2043. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2044. alignment = 256 * 1024;
  2045. /*
  2046. * Global gtt pte registers are special registers which actually forward
  2047. * writes to a chunk of system memory. Which means that there is no risk
  2048. * that the register values disappear as soon as we call
  2049. * intel_runtime_pm_put(), so it is correct to wrap only the
  2050. * pin/unpin/fence and not more.
  2051. */
  2052. intel_runtime_pm_get(dev_priv);
  2053. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  2054. &view);
  2055. if (ret)
  2056. goto err_pm;
  2057. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2058. * fence, whereas 965+ only requires a fence if using
  2059. * framebuffer compression. For simplicity, we always install
  2060. * a fence as the cost is not that onerous.
  2061. */
  2062. if (view.type == I915_GGTT_VIEW_NORMAL) {
  2063. ret = i915_gem_object_get_fence(obj);
  2064. if (ret == -EDEADLK) {
  2065. /*
  2066. * -EDEADLK means there are no free fences
  2067. * no pending flips.
  2068. *
  2069. * This is propagated to atomic, but it uses
  2070. * -EDEADLK to force a locking recovery, so
  2071. * change the returned error to -EBUSY.
  2072. */
  2073. ret = -EBUSY;
  2074. goto err_unpin;
  2075. } else if (ret)
  2076. goto err_unpin;
  2077. i915_gem_object_pin_fence(obj);
  2078. }
  2079. intel_runtime_pm_put(dev_priv);
  2080. return 0;
  2081. err_unpin:
  2082. i915_gem_object_unpin_from_display_plane(obj, &view);
  2083. err_pm:
  2084. intel_runtime_pm_put(dev_priv);
  2085. return ret;
  2086. }
  2087. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2088. const struct drm_plane_state *plane_state)
  2089. {
  2090. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2091. struct i915_ggtt_view view;
  2092. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2093. intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2094. if (view.type == I915_GGTT_VIEW_NORMAL)
  2095. i915_gem_object_unpin_fence(obj);
  2096. i915_gem_object_unpin_from_display_plane(obj, &view);
  2097. }
  2098. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2099. * is assumed to be a power-of-two. */
  2100. u32 intel_compute_tile_offset(struct drm_i915_private *dev_priv,
  2101. int *x, int *y,
  2102. uint64_t fb_modifier,
  2103. unsigned int cpp,
  2104. unsigned int pitch)
  2105. {
  2106. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2107. unsigned int tile_size, tile_width, tile_height;
  2108. unsigned int tile_rows, tiles;
  2109. tile_size = intel_tile_size(dev_priv);
  2110. tile_width = intel_tile_width(dev_priv, fb_modifier, cpp);
  2111. tile_height = tile_size / tile_width;
  2112. tile_rows = *y / tile_height;
  2113. *y %= tile_height;
  2114. tiles = *x / (tile_width/cpp);
  2115. *x %= tile_width/cpp;
  2116. return tile_rows * pitch * tile_height + tiles * tile_size;
  2117. } else {
  2118. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2119. unsigned int offset;
  2120. offset = *y * pitch + *x * cpp;
  2121. *y = (offset & alignment) / pitch;
  2122. *x = ((offset & alignment) - *y * pitch) / cpp;
  2123. return offset & ~alignment;
  2124. }
  2125. }
  2126. static int i9xx_format_to_fourcc(int format)
  2127. {
  2128. switch (format) {
  2129. case DISPPLANE_8BPP:
  2130. return DRM_FORMAT_C8;
  2131. case DISPPLANE_BGRX555:
  2132. return DRM_FORMAT_XRGB1555;
  2133. case DISPPLANE_BGRX565:
  2134. return DRM_FORMAT_RGB565;
  2135. default:
  2136. case DISPPLANE_BGRX888:
  2137. return DRM_FORMAT_XRGB8888;
  2138. case DISPPLANE_RGBX888:
  2139. return DRM_FORMAT_XBGR8888;
  2140. case DISPPLANE_BGRX101010:
  2141. return DRM_FORMAT_XRGB2101010;
  2142. case DISPPLANE_RGBX101010:
  2143. return DRM_FORMAT_XBGR2101010;
  2144. }
  2145. }
  2146. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2147. {
  2148. switch (format) {
  2149. case PLANE_CTL_FORMAT_RGB_565:
  2150. return DRM_FORMAT_RGB565;
  2151. default:
  2152. case PLANE_CTL_FORMAT_XRGB_8888:
  2153. if (rgb_order) {
  2154. if (alpha)
  2155. return DRM_FORMAT_ABGR8888;
  2156. else
  2157. return DRM_FORMAT_XBGR8888;
  2158. } else {
  2159. if (alpha)
  2160. return DRM_FORMAT_ARGB8888;
  2161. else
  2162. return DRM_FORMAT_XRGB8888;
  2163. }
  2164. case PLANE_CTL_FORMAT_XRGB_2101010:
  2165. if (rgb_order)
  2166. return DRM_FORMAT_XBGR2101010;
  2167. else
  2168. return DRM_FORMAT_XRGB2101010;
  2169. }
  2170. }
  2171. static bool
  2172. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2173. struct intel_initial_plane_config *plane_config)
  2174. {
  2175. struct drm_device *dev = crtc->base.dev;
  2176. struct drm_i915_private *dev_priv = to_i915(dev);
  2177. struct drm_i915_gem_object *obj = NULL;
  2178. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2179. struct drm_framebuffer *fb = &plane_config->fb->base;
  2180. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2181. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2182. PAGE_SIZE);
  2183. size_aligned -= base_aligned;
  2184. if (plane_config->size == 0)
  2185. return false;
  2186. /* If the FB is too big, just don't use it since fbdev is not very
  2187. * important and we should probably use that space with FBC or other
  2188. * features. */
  2189. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2190. return false;
  2191. mutex_lock(&dev->struct_mutex);
  2192. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2193. base_aligned,
  2194. base_aligned,
  2195. size_aligned);
  2196. if (!obj) {
  2197. mutex_unlock(&dev->struct_mutex);
  2198. return false;
  2199. }
  2200. obj->tiling_mode = plane_config->tiling;
  2201. if (obj->tiling_mode == I915_TILING_X)
  2202. obj->stride = fb->pitches[0];
  2203. mode_cmd.pixel_format = fb->pixel_format;
  2204. mode_cmd.width = fb->width;
  2205. mode_cmd.height = fb->height;
  2206. mode_cmd.pitches[0] = fb->pitches[0];
  2207. mode_cmd.modifier[0] = fb->modifier[0];
  2208. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2209. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2210. &mode_cmd, obj)) {
  2211. DRM_DEBUG_KMS("intel fb init failed\n");
  2212. goto out_unref_obj;
  2213. }
  2214. mutex_unlock(&dev->struct_mutex);
  2215. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2216. return true;
  2217. out_unref_obj:
  2218. drm_gem_object_unreference(&obj->base);
  2219. mutex_unlock(&dev->struct_mutex);
  2220. return false;
  2221. }
  2222. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2223. static void
  2224. update_state_fb(struct drm_plane *plane)
  2225. {
  2226. if (plane->fb == plane->state->fb)
  2227. return;
  2228. if (plane->state->fb)
  2229. drm_framebuffer_unreference(plane->state->fb);
  2230. plane->state->fb = plane->fb;
  2231. if (plane->state->fb)
  2232. drm_framebuffer_reference(plane->state->fb);
  2233. }
  2234. static void
  2235. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2236. struct intel_initial_plane_config *plane_config)
  2237. {
  2238. struct drm_device *dev = intel_crtc->base.dev;
  2239. struct drm_i915_private *dev_priv = dev->dev_private;
  2240. struct drm_crtc *c;
  2241. struct intel_crtc *i;
  2242. struct drm_i915_gem_object *obj;
  2243. struct drm_plane *primary = intel_crtc->base.primary;
  2244. struct drm_plane_state *plane_state = primary->state;
  2245. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2246. struct intel_plane *intel_plane = to_intel_plane(primary);
  2247. struct intel_plane_state *intel_state =
  2248. to_intel_plane_state(plane_state);
  2249. struct drm_framebuffer *fb;
  2250. if (!plane_config->fb)
  2251. return;
  2252. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2253. fb = &plane_config->fb->base;
  2254. goto valid_fb;
  2255. }
  2256. kfree(plane_config->fb);
  2257. /*
  2258. * Failed to alloc the obj, check to see if we should share
  2259. * an fb with another CRTC instead
  2260. */
  2261. for_each_crtc(dev, c) {
  2262. i = to_intel_crtc(c);
  2263. if (c == &intel_crtc->base)
  2264. continue;
  2265. if (!i->active)
  2266. continue;
  2267. fb = c->primary->fb;
  2268. if (!fb)
  2269. continue;
  2270. obj = intel_fb_obj(fb);
  2271. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2272. drm_framebuffer_reference(fb);
  2273. goto valid_fb;
  2274. }
  2275. }
  2276. /*
  2277. * We've failed to reconstruct the BIOS FB. Current display state
  2278. * indicates that the primary plane is visible, but has a NULL FB,
  2279. * which will lead to problems later if we don't fix it up. The
  2280. * simplest solution is to just disable the primary plane now and
  2281. * pretend the BIOS never had it enabled.
  2282. */
  2283. to_intel_plane_state(plane_state)->visible = false;
  2284. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2285. intel_pre_disable_primary(&intel_crtc->base);
  2286. intel_plane->disable_plane(primary, &intel_crtc->base);
  2287. return;
  2288. valid_fb:
  2289. plane_state->src_x = 0;
  2290. plane_state->src_y = 0;
  2291. plane_state->src_w = fb->width << 16;
  2292. plane_state->src_h = fb->height << 16;
  2293. plane_state->crtc_x = 0;
  2294. plane_state->crtc_y = 0;
  2295. plane_state->crtc_w = fb->width;
  2296. plane_state->crtc_h = fb->height;
  2297. intel_state->src.x1 = plane_state->src_x;
  2298. intel_state->src.y1 = plane_state->src_y;
  2299. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2300. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2301. intel_state->dst.x1 = plane_state->crtc_x;
  2302. intel_state->dst.y1 = plane_state->crtc_y;
  2303. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2304. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2305. obj = intel_fb_obj(fb);
  2306. if (obj->tiling_mode != I915_TILING_NONE)
  2307. dev_priv->preserve_bios_swizzle = true;
  2308. drm_framebuffer_reference(fb);
  2309. primary->fb = primary->state->fb = fb;
  2310. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2311. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2312. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2313. }
  2314. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2315. const struct intel_crtc_state *crtc_state,
  2316. const struct intel_plane_state *plane_state)
  2317. {
  2318. struct drm_device *dev = primary->dev;
  2319. struct drm_i915_private *dev_priv = dev->dev_private;
  2320. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2321. struct drm_framebuffer *fb = plane_state->base.fb;
  2322. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2323. int plane = intel_crtc->plane;
  2324. u32 linear_offset;
  2325. u32 dspcntr;
  2326. i915_reg_t reg = DSPCNTR(plane);
  2327. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2328. int x = plane_state->src.x1 >> 16;
  2329. int y = plane_state->src.y1 >> 16;
  2330. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2331. dspcntr |= DISPLAY_PLANE_ENABLE;
  2332. if (INTEL_INFO(dev)->gen < 4) {
  2333. if (intel_crtc->pipe == PIPE_B)
  2334. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2335. /* pipesrc and dspsize control the size that is scaled from,
  2336. * which should always be the user's requested size.
  2337. */
  2338. I915_WRITE(DSPSIZE(plane),
  2339. ((crtc_state->pipe_src_h - 1) << 16) |
  2340. (crtc_state->pipe_src_w - 1));
  2341. I915_WRITE(DSPPOS(plane), 0);
  2342. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2343. I915_WRITE(PRIMSIZE(plane),
  2344. ((crtc_state->pipe_src_h - 1) << 16) |
  2345. (crtc_state->pipe_src_w - 1));
  2346. I915_WRITE(PRIMPOS(plane), 0);
  2347. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2348. }
  2349. switch (fb->pixel_format) {
  2350. case DRM_FORMAT_C8:
  2351. dspcntr |= DISPPLANE_8BPP;
  2352. break;
  2353. case DRM_FORMAT_XRGB1555:
  2354. dspcntr |= DISPPLANE_BGRX555;
  2355. break;
  2356. case DRM_FORMAT_RGB565:
  2357. dspcntr |= DISPPLANE_BGRX565;
  2358. break;
  2359. case DRM_FORMAT_XRGB8888:
  2360. dspcntr |= DISPPLANE_BGRX888;
  2361. break;
  2362. case DRM_FORMAT_XBGR8888:
  2363. dspcntr |= DISPPLANE_RGBX888;
  2364. break;
  2365. case DRM_FORMAT_XRGB2101010:
  2366. dspcntr |= DISPPLANE_BGRX101010;
  2367. break;
  2368. case DRM_FORMAT_XBGR2101010:
  2369. dspcntr |= DISPPLANE_RGBX101010;
  2370. break;
  2371. default:
  2372. BUG();
  2373. }
  2374. if (INTEL_INFO(dev)->gen >= 4 &&
  2375. obj->tiling_mode != I915_TILING_NONE)
  2376. dspcntr |= DISPPLANE_TILED;
  2377. if (IS_G4X(dev))
  2378. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2379. linear_offset = y * fb->pitches[0] + x * cpp;
  2380. if (INTEL_INFO(dev)->gen >= 4) {
  2381. intel_crtc->dspaddr_offset =
  2382. intel_compute_tile_offset(dev_priv, &x, &y,
  2383. fb->modifier[0], cpp,
  2384. fb->pitches[0]);
  2385. linear_offset -= intel_crtc->dspaddr_offset;
  2386. } else {
  2387. intel_crtc->dspaddr_offset = linear_offset;
  2388. }
  2389. if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  2390. dspcntr |= DISPPLANE_ROTATE_180;
  2391. x += (crtc_state->pipe_src_w - 1);
  2392. y += (crtc_state->pipe_src_h - 1);
  2393. /* Finding the last pixel of the last line of the display
  2394. data and adding to linear_offset*/
  2395. linear_offset +=
  2396. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2397. (crtc_state->pipe_src_w - 1) * cpp;
  2398. }
  2399. intel_crtc->adjusted_x = x;
  2400. intel_crtc->adjusted_y = y;
  2401. I915_WRITE(reg, dspcntr);
  2402. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2403. if (INTEL_INFO(dev)->gen >= 4) {
  2404. I915_WRITE(DSPSURF(plane),
  2405. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2406. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2407. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2408. } else
  2409. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2410. POSTING_READ(reg);
  2411. }
  2412. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2413. struct drm_crtc *crtc)
  2414. {
  2415. struct drm_device *dev = crtc->dev;
  2416. struct drm_i915_private *dev_priv = dev->dev_private;
  2417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2418. int plane = intel_crtc->plane;
  2419. I915_WRITE(DSPCNTR(plane), 0);
  2420. if (INTEL_INFO(dev_priv)->gen >= 4)
  2421. I915_WRITE(DSPSURF(plane), 0);
  2422. else
  2423. I915_WRITE(DSPADDR(plane), 0);
  2424. POSTING_READ(DSPCNTR(plane));
  2425. }
  2426. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2427. const struct intel_crtc_state *crtc_state,
  2428. const struct intel_plane_state *plane_state)
  2429. {
  2430. struct drm_device *dev = primary->dev;
  2431. struct drm_i915_private *dev_priv = dev->dev_private;
  2432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2433. struct drm_framebuffer *fb = plane_state->base.fb;
  2434. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2435. int plane = intel_crtc->plane;
  2436. u32 linear_offset;
  2437. u32 dspcntr;
  2438. i915_reg_t reg = DSPCNTR(plane);
  2439. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2440. int x = plane_state->src.x1 >> 16;
  2441. int y = plane_state->src.y1 >> 16;
  2442. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2443. dspcntr |= DISPLAY_PLANE_ENABLE;
  2444. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2445. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2446. switch (fb->pixel_format) {
  2447. case DRM_FORMAT_C8:
  2448. dspcntr |= DISPPLANE_8BPP;
  2449. break;
  2450. case DRM_FORMAT_RGB565:
  2451. dspcntr |= DISPPLANE_BGRX565;
  2452. break;
  2453. case DRM_FORMAT_XRGB8888:
  2454. dspcntr |= DISPPLANE_BGRX888;
  2455. break;
  2456. case DRM_FORMAT_XBGR8888:
  2457. dspcntr |= DISPPLANE_RGBX888;
  2458. break;
  2459. case DRM_FORMAT_XRGB2101010:
  2460. dspcntr |= DISPPLANE_BGRX101010;
  2461. break;
  2462. case DRM_FORMAT_XBGR2101010:
  2463. dspcntr |= DISPPLANE_RGBX101010;
  2464. break;
  2465. default:
  2466. BUG();
  2467. }
  2468. if (obj->tiling_mode != I915_TILING_NONE)
  2469. dspcntr |= DISPPLANE_TILED;
  2470. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2471. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2472. linear_offset = y * fb->pitches[0] + x * cpp;
  2473. intel_crtc->dspaddr_offset =
  2474. intel_compute_tile_offset(dev_priv, &x, &y,
  2475. fb->modifier[0], cpp,
  2476. fb->pitches[0]);
  2477. linear_offset -= intel_crtc->dspaddr_offset;
  2478. if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  2479. dspcntr |= DISPPLANE_ROTATE_180;
  2480. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2481. x += (crtc_state->pipe_src_w - 1);
  2482. y += (crtc_state->pipe_src_h - 1);
  2483. /* Finding the last pixel of the last line of the display
  2484. data and adding to linear_offset*/
  2485. linear_offset +=
  2486. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2487. (crtc_state->pipe_src_w - 1) * cpp;
  2488. }
  2489. }
  2490. intel_crtc->adjusted_x = x;
  2491. intel_crtc->adjusted_y = y;
  2492. I915_WRITE(reg, dspcntr);
  2493. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2494. I915_WRITE(DSPSURF(plane),
  2495. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2496. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2497. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2498. } else {
  2499. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2500. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2501. }
  2502. POSTING_READ(reg);
  2503. }
  2504. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2505. uint64_t fb_modifier, uint32_t pixel_format)
  2506. {
  2507. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2508. return 64;
  2509. } else {
  2510. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2511. return intel_tile_width(dev_priv, fb_modifier, cpp);
  2512. }
  2513. }
  2514. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2515. struct drm_i915_gem_object *obj,
  2516. unsigned int plane)
  2517. {
  2518. struct i915_ggtt_view view;
  2519. struct i915_vma *vma;
  2520. u64 offset;
  2521. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2522. intel_plane->base.state);
  2523. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2524. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2525. view.type))
  2526. return -1;
  2527. offset = vma->node.start;
  2528. if (plane == 1) {
  2529. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2530. PAGE_SIZE;
  2531. }
  2532. WARN_ON(upper_32_bits(offset));
  2533. return lower_32_bits(offset);
  2534. }
  2535. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2536. {
  2537. struct drm_device *dev = intel_crtc->base.dev;
  2538. struct drm_i915_private *dev_priv = dev->dev_private;
  2539. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2540. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2541. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2542. }
  2543. /*
  2544. * This function detaches (aka. unbinds) unused scalers in hardware
  2545. */
  2546. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2547. {
  2548. struct intel_crtc_scaler_state *scaler_state;
  2549. int i;
  2550. scaler_state = &intel_crtc->config->scaler_state;
  2551. /* loop through and disable scalers that aren't in use */
  2552. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2553. if (!scaler_state->scalers[i].in_use)
  2554. skl_detach_scaler(intel_crtc, i);
  2555. }
  2556. }
  2557. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2558. {
  2559. switch (pixel_format) {
  2560. case DRM_FORMAT_C8:
  2561. return PLANE_CTL_FORMAT_INDEXED;
  2562. case DRM_FORMAT_RGB565:
  2563. return PLANE_CTL_FORMAT_RGB_565;
  2564. case DRM_FORMAT_XBGR8888:
  2565. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2566. case DRM_FORMAT_XRGB8888:
  2567. return PLANE_CTL_FORMAT_XRGB_8888;
  2568. /*
  2569. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2570. * to be already pre-multiplied. We need to add a knob (or a different
  2571. * DRM_FORMAT) for user-space to configure that.
  2572. */
  2573. case DRM_FORMAT_ABGR8888:
  2574. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2575. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2576. case DRM_FORMAT_ARGB8888:
  2577. return PLANE_CTL_FORMAT_XRGB_8888 |
  2578. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2579. case DRM_FORMAT_XRGB2101010:
  2580. return PLANE_CTL_FORMAT_XRGB_2101010;
  2581. case DRM_FORMAT_XBGR2101010:
  2582. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2583. case DRM_FORMAT_YUYV:
  2584. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2585. case DRM_FORMAT_YVYU:
  2586. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2587. case DRM_FORMAT_UYVY:
  2588. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2589. case DRM_FORMAT_VYUY:
  2590. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2591. default:
  2592. MISSING_CASE(pixel_format);
  2593. }
  2594. return 0;
  2595. }
  2596. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2597. {
  2598. switch (fb_modifier) {
  2599. case DRM_FORMAT_MOD_NONE:
  2600. break;
  2601. case I915_FORMAT_MOD_X_TILED:
  2602. return PLANE_CTL_TILED_X;
  2603. case I915_FORMAT_MOD_Y_TILED:
  2604. return PLANE_CTL_TILED_Y;
  2605. case I915_FORMAT_MOD_Yf_TILED:
  2606. return PLANE_CTL_TILED_YF;
  2607. default:
  2608. MISSING_CASE(fb_modifier);
  2609. }
  2610. return 0;
  2611. }
  2612. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2613. {
  2614. switch (rotation) {
  2615. case BIT(DRM_ROTATE_0):
  2616. break;
  2617. /*
  2618. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2619. * while i915 HW rotation is clockwise, thats why this swapping.
  2620. */
  2621. case BIT(DRM_ROTATE_90):
  2622. return PLANE_CTL_ROTATE_270;
  2623. case BIT(DRM_ROTATE_180):
  2624. return PLANE_CTL_ROTATE_180;
  2625. case BIT(DRM_ROTATE_270):
  2626. return PLANE_CTL_ROTATE_90;
  2627. default:
  2628. MISSING_CASE(rotation);
  2629. }
  2630. return 0;
  2631. }
  2632. static void skylake_update_primary_plane(struct drm_plane *plane,
  2633. const struct intel_crtc_state *crtc_state,
  2634. const struct intel_plane_state *plane_state)
  2635. {
  2636. struct drm_device *dev = plane->dev;
  2637. struct drm_i915_private *dev_priv = dev->dev_private;
  2638. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2639. struct drm_framebuffer *fb = plane_state->base.fb;
  2640. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2641. int pipe = intel_crtc->pipe;
  2642. u32 plane_ctl, stride_div, stride;
  2643. u32 tile_height, plane_offset, plane_size;
  2644. unsigned int rotation = plane_state->base.rotation;
  2645. int x_offset, y_offset;
  2646. u32 surf_addr;
  2647. int scaler_id = plane_state->scaler_id;
  2648. int src_x = plane_state->src.x1 >> 16;
  2649. int src_y = plane_state->src.y1 >> 16;
  2650. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2651. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2652. int dst_x = plane_state->dst.x1;
  2653. int dst_y = plane_state->dst.y1;
  2654. int dst_w = drm_rect_width(&plane_state->dst);
  2655. int dst_h = drm_rect_height(&plane_state->dst);
  2656. plane_ctl = PLANE_CTL_ENABLE |
  2657. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2658. PLANE_CTL_PIPE_CSC_ENABLE;
  2659. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2660. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2661. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2662. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2663. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2664. fb->pixel_format);
  2665. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2666. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2667. if (intel_rotation_90_or_270(rotation)) {
  2668. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2669. /* stride = Surface height in tiles */
  2670. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2671. stride = DIV_ROUND_UP(fb->height, tile_height);
  2672. x_offset = stride * tile_height - src_y - src_h;
  2673. y_offset = src_x;
  2674. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2675. } else {
  2676. stride = fb->pitches[0] / stride_div;
  2677. x_offset = src_x;
  2678. y_offset = src_y;
  2679. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2680. }
  2681. plane_offset = y_offset << 16 | x_offset;
  2682. intel_crtc->adjusted_x = x_offset;
  2683. intel_crtc->adjusted_y = y_offset;
  2684. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2685. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2686. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2687. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2688. if (scaler_id >= 0) {
  2689. uint32_t ps_ctrl = 0;
  2690. WARN_ON(!dst_w || !dst_h);
  2691. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2692. crtc_state->scaler_state.scalers[scaler_id].mode;
  2693. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2694. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2695. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2696. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2697. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2698. } else {
  2699. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2700. }
  2701. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2702. POSTING_READ(PLANE_SURF(pipe, 0));
  2703. }
  2704. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2705. struct drm_crtc *crtc)
  2706. {
  2707. struct drm_device *dev = crtc->dev;
  2708. struct drm_i915_private *dev_priv = dev->dev_private;
  2709. int pipe = to_intel_crtc(crtc)->pipe;
  2710. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2711. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2712. POSTING_READ(PLANE_SURF(pipe, 0));
  2713. }
  2714. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2715. static int
  2716. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2717. int x, int y, enum mode_set_atomic state)
  2718. {
  2719. /* Support for kgdboc is disabled, this needs a major rework. */
  2720. DRM_ERROR("legacy panic handler not supported any more.\n");
  2721. return -ENODEV;
  2722. }
  2723. static void intel_complete_page_flips(struct drm_device *dev)
  2724. {
  2725. struct drm_crtc *crtc;
  2726. for_each_crtc(dev, crtc) {
  2727. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2728. enum plane plane = intel_crtc->plane;
  2729. intel_prepare_page_flip(dev, plane);
  2730. intel_finish_page_flip_plane(dev, plane);
  2731. }
  2732. }
  2733. static void intel_update_primary_planes(struct drm_device *dev)
  2734. {
  2735. struct drm_crtc *crtc;
  2736. for_each_crtc(dev, crtc) {
  2737. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2738. struct intel_plane_state *plane_state;
  2739. drm_modeset_lock_crtc(crtc, &plane->base);
  2740. plane_state = to_intel_plane_state(plane->base.state);
  2741. if (plane_state->visible)
  2742. plane->update_plane(&plane->base,
  2743. to_intel_crtc_state(crtc->state),
  2744. plane_state);
  2745. drm_modeset_unlock_crtc(crtc);
  2746. }
  2747. }
  2748. void intel_prepare_reset(struct drm_device *dev)
  2749. {
  2750. /* no reset support for gen2 */
  2751. if (IS_GEN2(dev))
  2752. return;
  2753. /* reset doesn't touch the display */
  2754. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2755. return;
  2756. drm_modeset_lock_all(dev);
  2757. /*
  2758. * Disabling the crtcs gracefully seems nicer. Also the
  2759. * g33 docs say we should at least disable all the planes.
  2760. */
  2761. intel_display_suspend(dev);
  2762. }
  2763. void intel_finish_reset(struct drm_device *dev)
  2764. {
  2765. struct drm_i915_private *dev_priv = to_i915(dev);
  2766. /*
  2767. * Flips in the rings will be nuked by the reset,
  2768. * so complete all pending flips so that user space
  2769. * will get its events and not get stuck.
  2770. */
  2771. intel_complete_page_flips(dev);
  2772. /* no reset support for gen2 */
  2773. if (IS_GEN2(dev))
  2774. return;
  2775. /* reset doesn't touch the display */
  2776. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2777. /*
  2778. * Flips in the rings have been nuked by the reset,
  2779. * so update the base address of all primary
  2780. * planes to the the last fb to make sure we're
  2781. * showing the correct fb after a reset.
  2782. *
  2783. * FIXME: Atomic will make this obsolete since we won't schedule
  2784. * CS-based flips (which might get lost in gpu resets) any more.
  2785. */
  2786. intel_update_primary_planes(dev);
  2787. return;
  2788. }
  2789. /*
  2790. * The display has been reset as well,
  2791. * so need a full re-initialization.
  2792. */
  2793. intel_runtime_pm_disable_interrupts(dev_priv);
  2794. intel_runtime_pm_enable_interrupts(dev_priv);
  2795. intel_modeset_init_hw(dev);
  2796. spin_lock_irq(&dev_priv->irq_lock);
  2797. if (dev_priv->display.hpd_irq_setup)
  2798. dev_priv->display.hpd_irq_setup(dev);
  2799. spin_unlock_irq(&dev_priv->irq_lock);
  2800. intel_display_resume(dev);
  2801. intel_hpd_init(dev_priv);
  2802. drm_modeset_unlock_all(dev);
  2803. }
  2804. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2805. {
  2806. struct drm_device *dev = crtc->dev;
  2807. struct drm_i915_private *dev_priv = dev->dev_private;
  2808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2809. bool pending;
  2810. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2811. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2812. return false;
  2813. spin_lock_irq(&dev->event_lock);
  2814. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2815. spin_unlock_irq(&dev->event_lock);
  2816. return pending;
  2817. }
  2818. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2819. struct intel_crtc_state *old_crtc_state)
  2820. {
  2821. struct drm_device *dev = crtc->base.dev;
  2822. struct drm_i915_private *dev_priv = dev->dev_private;
  2823. struct intel_crtc_state *pipe_config =
  2824. to_intel_crtc_state(crtc->base.state);
  2825. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2826. crtc->base.mode = crtc->base.state->mode;
  2827. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2828. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2829. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2830. if (HAS_DDI(dev))
  2831. intel_set_pipe_csc(&crtc->base);
  2832. /*
  2833. * Update pipe size and adjust fitter if needed: the reason for this is
  2834. * that in compute_mode_changes we check the native mode (not the pfit
  2835. * mode) to see if we can flip rather than do a full mode set. In the
  2836. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2837. * pfit state, we'll end up with a big fb scanned out into the wrong
  2838. * sized surface.
  2839. */
  2840. I915_WRITE(PIPESRC(crtc->pipe),
  2841. ((pipe_config->pipe_src_w - 1) << 16) |
  2842. (pipe_config->pipe_src_h - 1));
  2843. /* on skylake this is done by detaching scalers */
  2844. if (INTEL_INFO(dev)->gen >= 9) {
  2845. skl_detach_scalers(crtc);
  2846. if (pipe_config->pch_pfit.enabled)
  2847. skylake_pfit_enable(crtc);
  2848. } else if (HAS_PCH_SPLIT(dev)) {
  2849. if (pipe_config->pch_pfit.enabled)
  2850. ironlake_pfit_enable(crtc);
  2851. else if (old_crtc_state->pch_pfit.enabled)
  2852. ironlake_pfit_disable(crtc, true);
  2853. }
  2854. }
  2855. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2856. {
  2857. struct drm_device *dev = crtc->dev;
  2858. struct drm_i915_private *dev_priv = dev->dev_private;
  2859. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2860. int pipe = intel_crtc->pipe;
  2861. i915_reg_t reg;
  2862. u32 temp;
  2863. /* enable normal train */
  2864. reg = FDI_TX_CTL(pipe);
  2865. temp = I915_READ(reg);
  2866. if (IS_IVYBRIDGE(dev)) {
  2867. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2868. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2869. } else {
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2872. }
  2873. I915_WRITE(reg, temp);
  2874. reg = FDI_RX_CTL(pipe);
  2875. temp = I915_READ(reg);
  2876. if (HAS_PCH_CPT(dev)) {
  2877. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2878. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2879. } else {
  2880. temp &= ~FDI_LINK_TRAIN_NONE;
  2881. temp |= FDI_LINK_TRAIN_NONE;
  2882. }
  2883. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2884. /* wait one idle pattern time */
  2885. POSTING_READ(reg);
  2886. udelay(1000);
  2887. /* IVB wants error correction enabled */
  2888. if (IS_IVYBRIDGE(dev))
  2889. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2890. FDI_FE_ERRC_ENABLE);
  2891. }
  2892. /* The FDI link training functions for ILK/Ibexpeak. */
  2893. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2894. {
  2895. struct drm_device *dev = crtc->dev;
  2896. struct drm_i915_private *dev_priv = dev->dev_private;
  2897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2898. int pipe = intel_crtc->pipe;
  2899. i915_reg_t reg;
  2900. u32 temp, tries;
  2901. /* FDI needs bits from pipe first */
  2902. assert_pipe_enabled(dev_priv, pipe);
  2903. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2904. for train result */
  2905. reg = FDI_RX_IMR(pipe);
  2906. temp = I915_READ(reg);
  2907. temp &= ~FDI_RX_SYMBOL_LOCK;
  2908. temp &= ~FDI_RX_BIT_LOCK;
  2909. I915_WRITE(reg, temp);
  2910. I915_READ(reg);
  2911. udelay(150);
  2912. /* enable CPU FDI TX and PCH FDI RX */
  2913. reg = FDI_TX_CTL(pipe);
  2914. temp = I915_READ(reg);
  2915. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2916. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2917. temp &= ~FDI_LINK_TRAIN_NONE;
  2918. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2919. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2920. reg = FDI_RX_CTL(pipe);
  2921. temp = I915_READ(reg);
  2922. temp &= ~FDI_LINK_TRAIN_NONE;
  2923. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2924. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2925. POSTING_READ(reg);
  2926. udelay(150);
  2927. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2928. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2929. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2930. FDI_RX_PHASE_SYNC_POINTER_EN);
  2931. reg = FDI_RX_IIR(pipe);
  2932. for (tries = 0; tries < 5; tries++) {
  2933. temp = I915_READ(reg);
  2934. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2935. if ((temp & FDI_RX_BIT_LOCK)) {
  2936. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2937. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2938. break;
  2939. }
  2940. }
  2941. if (tries == 5)
  2942. DRM_ERROR("FDI train 1 fail!\n");
  2943. /* Train 2 */
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_LINK_TRAIN_NONE;
  2947. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2948. I915_WRITE(reg, temp);
  2949. reg = FDI_RX_CTL(pipe);
  2950. temp = I915_READ(reg);
  2951. temp &= ~FDI_LINK_TRAIN_NONE;
  2952. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2953. I915_WRITE(reg, temp);
  2954. POSTING_READ(reg);
  2955. udelay(150);
  2956. reg = FDI_RX_IIR(pipe);
  2957. for (tries = 0; tries < 5; tries++) {
  2958. temp = I915_READ(reg);
  2959. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2960. if (temp & FDI_RX_SYMBOL_LOCK) {
  2961. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2962. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2963. break;
  2964. }
  2965. }
  2966. if (tries == 5)
  2967. DRM_ERROR("FDI train 2 fail!\n");
  2968. DRM_DEBUG_KMS("FDI train done\n");
  2969. }
  2970. static const int snb_b_fdi_train_param[] = {
  2971. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2972. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2973. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2974. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2975. };
  2976. /* The FDI link training functions for SNB/Cougarpoint. */
  2977. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2978. {
  2979. struct drm_device *dev = crtc->dev;
  2980. struct drm_i915_private *dev_priv = dev->dev_private;
  2981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2982. int pipe = intel_crtc->pipe;
  2983. i915_reg_t reg;
  2984. u32 temp, i, retry;
  2985. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2986. for train result */
  2987. reg = FDI_RX_IMR(pipe);
  2988. temp = I915_READ(reg);
  2989. temp &= ~FDI_RX_SYMBOL_LOCK;
  2990. temp &= ~FDI_RX_BIT_LOCK;
  2991. I915_WRITE(reg, temp);
  2992. POSTING_READ(reg);
  2993. udelay(150);
  2994. /* enable CPU FDI TX and PCH FDI RX */
  2995. reg = FDI_TX_CTL(pipe);
  2996. temp = I915_READ(reg);
  2997. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2998. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2999. temp &= ~FDI_LINK_TRAIN_NONE;
  3000. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3001. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3002. /* SNB-B */
  3003. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3004. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3005. I915_WRITE(FDI_RX_MISC(pipe),
  3006. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3007. reg = FDI_RX_CTL(pipe);
  3008. temp = I915_READ(reg);
  3009. if (HAS_PCH_CPT(dev)) {
  3010. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3011. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3012. } else {
  3013. temp &= ~FDI_LINK_TRAIN_NONE;
  3014. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3015. }
  3016. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3017. POSTING_READ(reg);
  3018. udelay(150);
  3019. for (i = 0; i < 4; i++) {
  3020. reg = FDI_TX_CTL(pipe);
  3021. temp = I915_READ(reg);
  3022. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3023. temp |= snb_b_fdi_train_param[i];
  3024. I915_WRITE(reg, temp);
  3025. POSTING_READ(reg);
  3026. udelay(500);
  3027. for (retry = 0; retry < 5; retry++) {
  3028. reg = FDI_RX_IIR(pipe);
  3029. temp = I915_READ(reg);
  3030. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3031. if (temp & FDI_RX_BIT_LOCK) {
  3032. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3033. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3034. break;
  3035. }
  3036. udelay(50);
  3037. }
  3038. if (retry < 5)
  3039. break;
  3040. }
  3041. if (i == 4)
  3042. DRM_ERROR("FDI train 1 fail!\n");
  3043. /* Train 2 */
  3044. reg = FDI_TX_CTL(pipe);
  3045. temp = I915_READ(reg);
  3046. temp &= ~FDI_LINK_TRAIN_NONE;
  3047. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3048. if (IS_GEN6(dev)) {
  3049. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3050. /* SNB-B */
  3051. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3052. }
  3053. I915_WRITE(reg, temp);
  3054. reg = FDI_RX_CTL(pipe);
  3055. temp = I915_READ(reg);
  3056. if (HAS_PCH_CPT(dev)) {
  3057. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3058. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3059. } else {
  3060. temp &= ~FDI_LINK_TRAIN_NONE;
  3061. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3062. }
  3063. I915_WRITE(reg, temp);
  3064. POSTING_READ(reg);
  3065. udelay(150);
  3066. for (i = 0; i < 4; i++) {
  3067. reg = FDI_TX_CTL(pipe);
  3068. temp = I915_READ(reg);
  3069. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3070. temp |= snb_b_fdi_train_param[i];
  3071. I915_WRITE(reg, temp);
  3072. POSTING_READ(reg);
  3073. udelay(500);
  3074. for (retry = 0; retry < 5; retry++) {
  3075. reg = FDI_RX_IIR(pipe);
  3076. temp = I915_READ(reg);
  3077. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3078. if (temp & FDI_RX_SYMBOL_LOCK) {
  3079. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3080. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3081. break;
  3082. }
  3083. udelay(50);
  3084. }
  3085. if (retry < 5)
  3086. break;
  3087. }
  3088. if (i == 4)
  3089. DRM_ERROR("FDI train 2 fail!\n");
  3090. DRM_DEBUG_KMS("FDI train done.\n");
  3091. }
  3092. /* Manual link training for Ivy Bridge A0 parts */
  3093. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3094. {
  3095. struct drm_device *dev = crtc->dev;
  3096. struct drm_i915_private *dev_priv = dev->dev_private;
  3097. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3098. int pipe = intel_crtc->pipe;
  3099. i915_reg_t reg;
  3100. u32 temp, i, j;
  3101. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3102. for train result */
  3103. reg = FDI_RX_IMR(pipe);
  3104. temp = I915_READ(reg);
  3105. temp &= ~FDI_RX_SYMBOL_LOCK;
  3106. temp &= ~FDI_RX_BIT_LOCK;
  3107. I915_WRITE(reg, temp);
  3108. POSTING_READ(reg);
  3109. udelay(150);
  3110. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3111. I915_READ(FDI_RX_IIR(pipe)));
  3112. /* Try each vswing and preemphasis setting twice before moving on */
  3113. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3114. /* disable first in case we need to retry */
  3115. reg = FDI_TX_CTL(pipe);
  3116. temp = I915_READ(reg);
  3117. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3118. temp &= ~FDI_TX_ENABLE;
  3119. I915_WRITE(reg, temp);
  3120. reg = FDI_RX_CTL(pipe);
  3121. temp = I915_READ(reg);
  3122. temp &= ~FDI_LINK_TRAIN_AUTO;
  3123. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3124. temp &= ~FDI_RX_ENABLE;
  3125. I915_WRITE(reg, temp);
  3126. /* enable CPU FDI TX and PCH FDI RX */
  3127. reg = FDI_TX_CTL(pipe);
  3128. temp = I915_READ(reg);
  3129. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3130. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3131. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3132. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3133. temp |= snb_b_fdi_train_param[j/2];
  3134. temp |= FDI_COMPOSITE_SYNC;
  3135. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3136. I915_WRITE(FDI_RX_MISC(pipe),
  3137. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3138. reg = FDI_RX_CTL(pipe);
  3139. temp = I915_READ(reg);
  3140. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3141. temp |= FDI_COMPOSITE_SYNC;
  3142. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3143. POSTING_READ(reg);
  3144. udelay(1); /* should be 0.5us */
  3145. for (i = 0; i < 4; i++) {
  3146. reg = FDI_RX_IIR(pipe);
  3147. temp = I915_READ(reg);
  3148. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3149. if (temp & FDI_RX_BIT_LOCK ||
  3150. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3151. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3152. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3153. i);
  3154. break;
  3155. }
  3156. udelay(1); /* should be 0.5us */
  3157. }
  3158. if (i == 4) {
  3159. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3160. continue;
  3161. }
  3162. /* Train 2 */
  3163. reg = FDI_TX_CTL(pipe);
  3164. temp = I915_READ(reg);
  3165. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3166. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3167. I915_WRITE(reg, temp);
  3168. reg = FDI_RX_CTL(pipe);
  3169. temp = I915_READ(reg);
  3170. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3171. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3172. I915_WRITE(reg, temp);
  3173. POSTING_READ(reg);
  3174. udelay(2); /* should be 1.5us */
  3175. for (i = 0; i < 4; i++) {
  3176. reg = FDI_RX_IIR(pipe);
  3177. temp = I915_READ(reg);
  3178. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3179. if (temp & FDI_RX_SYMBOL_LOCK ||
  3180. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3181. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3182. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3183. i);
  3184. goto train_done;
  3185. }
  3186. udelay(2); /* should be 1.5us */
  3187. }
  3188. if (i == 4)
  3189. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3190. }
  3191. train_done:
  3192. DRM_DEBUG_KMS("FDI train done.\n");
  3193. }
  3194. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3195. {
  3196. struct drm_device *dev = intel_crtc->base.dev;
  3197. struct drm_i915_private *dev_priv = dev->dev_private;
  3198. int pipe = intel_crtc->pipe;
  3199. i915_reg_t reg;
  3200. u32 temp;
  3201. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3202. reg = FDI_RX_CTL(pipe);
  3203. temp = I915_READ(reg);
  3204. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3205. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3206. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3207. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3208. POSTING_READ(reg);
  3209. udelay(200);
  3210. /* Switch from Rawclk to PCDclk */
  3211. temp = I915_READ(reg);
  3212. I915_WRITE(reg, temp | FDI_PCDCLK);
  3213. POSTING_READ(reg);
  3214. udelay(200);
  3215. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3216. reg = FDI_TX_CTL(pipe);
  3217. temp = I915_READ(reg);
  3218. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3219. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3220. POSTING_READ(reg);
  3221. udelay(100);
  3222. }
  3223. }
  3224. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3225. {
  3226. struct drm_device *dev = intel_crtc->base.dev;
  3227. struct drm_i915_private *dev_priv = dev->dev_private;
  3228. int pipe = intel_crtc->pipe;
  3229. i915_reg_t reg;
  3230. u32 temp;
  3231. /* Switch from PCDclk to Rawclk */
  3232. reg = FDI_RX_CTL(pipe);
  3233. temp = I915_READ(reg);
  3234. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3235. /* Disable CPU FDI TX PLL */
  3236. reg = FDI_TX_CTL(pipe);
  3237. temp = I915_READ(reg);
  3238. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3239. POSTING_READ(reg);
  3240. udelay(100);
  3241. reg = FDI_RX_CTL(pipe);
  3242. temp = I915_READ(reg);
  3243. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3244. /* Wait for the clocks to turn off. */
  3245. POSTING_READ(reg);
  3246. udelay(100);
  3247. }
  3248. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3249. {
  3250. struct drm_device *dev = crtc->dev;
  3251. struct drm_i915_private *dev_priv = dev->dev_private;
  3252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3253. int pipe = intel_crtc->pipe;
  3254. i915_reg_t reg;
  3255. u32 temp;
  3256. /* disable CPU FDI tx and PCH FDI rx */
  3257. reg = FDI_TX_CTL(pipe);
  3258. temp = I915_READ(reg);
  3259. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3260. POSTING_READ(reg);
  3261. reg = FDI_RX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. temp &= ~(0x7 << 16);
  3264. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3265. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3266. POSTING_READ(reg);
  3267. udelay(100);
  3268. /* Ironlake workaround, disable clock pointer after downing FDI */
  3269. if (HAS_PCH_IBX(dev))
  3270. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3271. /* still set train pattern 1 */
  3272. reg = FDI_TX_CTL(pipe);
  3273. temp = I915_READ(reg);
  3274. temp &= ~FDI_LINK_TRAIN_NONE;
  3275. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3276. I915_WRITE(reg, temp);
  3277. reg = FDI_RX_CTL(pipe);
  3278. temp = I915_READ(reg);
  3279. if (HAS_PCH_CPT(dev)) {
  3280. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3281. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3282. } else {
  3283. temp &= ~FDI_LINK_TRAIN_NONE;
  3284. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3285. }
  3286. /* BPC in FDI rx is consistent with that in PIPECONF */
  3287. temp &= ~(0x07 << 16);
  3288. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3289. I915_WRITE(reg, temp);
  3290. POSTING_READ(reg);
  3291. udelay(100);
  3292. }
  3293. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3294. {
  3295. struct intel_crtc *crtc;
  3296. /* Note that we don't need to be called with mode_config.lock here
  3297. * as our list of CRTC objects is static for the lifetime of the
  3298. * device and so cannot disappear as we iterate. Similarly, we can
  3299. * happily treat the predicates as racy, atomic checks as userspace
  3300. * cannot claim and pin a new fb without at least acquring the
  3301. * struct_mutex and so serialising with us.
  3302. */
  3303. for_each_intel_crtc(dev, crtc) {
  3304. if (atomic_read(&crtc->unpin_work_count) == 0)
  3305. continue;
  3306. if (crtc->unpin_work)
  3307. intel_wait_for_vblank(dev, crtc->pipe);
  3308. return true;
  3309. }
  3310. return false;
  3311. }
  3312. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3313. {
  3314. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3315. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3316. /* ensure that the unpin work is consistent wrt ->pending. */
  3317. smp_rmb();
  3318. intel_crtc->unpin_work = NULL;
  3319. if (work->event)
  3320. drm_send_vblank_event(intel_crtc->base.dev,
  3321. intel_crtc->pipe,
  3322. work->event);
  3323. drm_crtc_vblank_put(&intel_crtc->base);
  3324. wake_up_all(&dev_priv->pending_flip_queue);
  3325. queue_work(dev_priv->wq, &work->work);
  3326. trace_i915_flip_complete(intel_crtc->plane,
  3327. work->pending_flip_obj);
  3328. }
  3329. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3330. {
  3331. struct drm_device *dev = crtc->dev;
  3332. struct drm_i915_private *dev_priv = dev->dev_private;
  3333. long ret;
  3334. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3335. ret = wait_event_interruptible_timeout(
  3336. dev_priv->pending_flip_queue,
  3337. !intel_crtc_has_pending_flip(crtc),
  3338. 60*HZ);
  3339. if (ret < 0)
  3340. return ret;
  3341. if (ret == 0) {
  3342. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3343. spin_lock_irq(&dev->event_lock);
  3344. if (intel_crtc->unpin_work) {
  3345. WARN_ONCE(1, "Removing stuck page flip\n");
  3346. page_flip_completed(intel_crtc);
  3347. }
  3348. spin_unlock_irq(&dev->event_lock);
  3349. }
  3350. return 0;
  3351. }
  3352. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3353. {
  3354. u32 temp;
  3355. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3356. mutex_lock(&dev_priv->sb_lock);
  3357. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3358. temp |= SBI_SSCCTL_DISABLE;
  3359. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3360. mutex_unlock(&dev_priv->sb_lock);
  3361. }
  3362. /* Program iCLKIP clock to the desired frequency */
  3363. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3364. {
  3365. struct drm_device *dev = crtc->dev;
  3366. struct drm_i915_private *dev_priv = dev->dev_private;
  3367. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3368. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3369. u32 temp;
  3370. lpt_disable_iclkip(dev_priv);
  3371. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3372. if (clock == 20000) {
  3373. auxdiv = 1;
  3374. divsel = 0x41;
  3375. phaseinc = 0x20;
  3376. } else {
  3377. /* The iCLK virtual clock root frequency is in MHz,
  3378. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3379. * divisors, it is necessary to divide one by another, so we
  3380. * convert the virtual clock precision to KHz here for higher
  3381. * precision.
  3382. */
  3383. u32 iclk_virtual_root_freq = 172800 * 1000;
  3384. u32 iclk_pi_range = 64;
  3385. u32 desired_divisor, msb_divisor_value, pi_value;
  3386. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
  3387. msb_divisor_value = desired_divisor / iclk_pi_range;
  3388. pi_value = desired_divisor % iclk_pi_range;
  3389. auxdiv = 0;
  3390. divsel = msb_divisor_value - 2;
  3391. phaseinc = pi_value;
  3392. }
  3393. /* This should not happen with any sane values */
  3394. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3395. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3396. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3397. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3398. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3399. clock,
  3400. auxdiv,
  3401. divsel,
  3402. phasedir,
  3403. phaseinc);
  3404. mutex_lock(&dev_priv->sb_lock);
  3405. /* Program SSCDIVINTPHASE6 */
  3406. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3407. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3408. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3409. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3410. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3411. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3412. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3413. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3414. /* Program SSCAUXDIV */
  3415. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3416. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3417. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3418. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3419. /* Enable modulator and associated divider */
  3420. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3421. temp &= ~SBI_SSCCTL_DISABLE;
  3422. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3423. mutex_unlock(&dev_priv->sb_lock);
  3424. /* Wait for initialization time */
  3425. udelay(24);
  3426. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3427. }
  3428. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3429. enum pipe pch_transcoder)
  3430. {
  3431. struct drm_device *dev = crtc->base.dev;
  3432. struct drm_i915_private *dev_priv = dev->dev_private;
  3433. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3434. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3435. I915_READ(HTOTAL(cpu_transcoder)));
  3436. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3437. I915_READ(HBLANK(cpu_transcoder)));
  3438. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3439. I915_READ(HSYNC(cpu_transcoder)));
  3440. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3441. I915_READ(VTOTAL(cpu_transcoder)));
  3442. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3443. I915_READ(VBLANK(cpu_transcoder)));
  3444. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3445. I915_READ(VSYNC(cpu_transcoder)));
  3446. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3447. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3448. }
  3449. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3450. {
  3451. struct drm_i915_private *dev_priv = dev->dev_private;
  3452. uint32_t temp;
  3453. temp = I915_READ(SOUTH_CHICKEN1);
  3454. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3455. return;
  3456. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3457. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3458. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3459. if (enable)
  3460. temp |= FDI_BC_BIFURCATION_SELECT;
  3461. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3462. I915_WRITE(SOUTH_CHICKEN1, temp);
  3463. POSTING_READ(SOUTH_CHICKEN1);
  3464. }
  3465. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3466. {
  3467. struct drm_device *dev = intel_crtc->base.dev;
  3468. switch (intel_crtc->pipe) {
  3469. case PIPE_A:
  3470. break;
  3471. case PIPE_B:
  3472. if (intel_crtc->config->fdi_lanes > 2)
  3473. cpt_set_fdi_bc_bifurcation(dev, false);
  3474. else
  3475. cpt_set_fdi_bc_bifurcation(dev, true);
  3476. break;
  3477. case PIPE_C:
  3478. cpt_set_fdi_bc_bifurcation(dev, true);
  3479. break;
  3480. default:
  3481. BUG();
  3482. }
  3483. }
  3484. /* Return which DP Port should be selected for Transcoder DP control */
  3485. static enum port
  3486. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3487. {
  3488. struct drm_device *dev = crtc->dev;
  3489. struct intel_encoder *encoder;
  3490. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3491. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3492. encoder->type == INTEL_OUTPUT_EDP)
  3493. return enc_to_dig_port(&encoder->base)->port;
  3494. }
  3495. return -1;
  3496. }
  3497. /*
  3498. * Enable PCH resources required for PCH ports:
  3499. * - PCH PLLs
  3500. * - FDI training & RX/TX
  3501. * - update transcoder timings
  3502. * - DP transcoding bits
  3503. * - transcoder
  3504. */
  3505. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3506. {
  3507. struct drm_device *dev = crtc->dev;
  3508. struct drm_i915_private *dev_priv = dev->dev_private;
  3509. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3510. int pipe = intel_crtc->pipe;
  3511. u32 temp;
  3512. assert_pch_transcoder_disabled(dev_priv, pipe);
  3513. if (IS_IVYBRIDGE(dev))
  3514. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3515. /* Write the TU size bits before fdi link training, so that error
  3516. * detection works. */
  3517. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3518. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3519. /*
  3520. * Sometimes spurious CPU pipe underruns happen during FDI
  3521. * training, at least with VGA+HDMI cloning. Suppress them.
  3522. */
  3523. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3524. /* For PCH output, training FDI link */
  3525. dev_priv->display.fdi_link_train(crtc);
  3526. /* We need to program the right clock selection before writing the pixel
  3527. * mutliplier into the DPLL. */
  3528. if (HAS_PCH_CPT(dev)) {
  3529. u32 sel;
  3530. temp = I915_READ(PCH_DPLL_SEL);
  3531. temp |= TRANS_DPLL_ENABLE(pipe);
  3532. sel = TRANS_DPLLB_SEL(pipe);
  3533. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3534. temp |= sel;
  3535. else
  3536. temp &= ~sel;
  3537. I915_WRITE(PCH_DPLL_SEL, temp);
  3538. }
  3539. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3540. * transcoder, and we actually should do this to not upset any PCH
  3541. * transcoder that already use the clock when we share it.
  3542. *
  3543. * Note that enable_shared_dpll tries to do the right thing, but
  3544. * get_shared_dpll unconditionally resets the pll - we need that to have
  3545. * the right LVDS enable sequence. */
  3546. intel_enable_shared_dpll(intel_crtc);
  3547. /* set transcoder timing, panel must allow it */
  3548. assert_panel_unlocked(dev_priv, pipe);
  3549. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3550. intel_fdi_normal_train(crtc);
  3551. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3552. /* For PCH DP, enable TRANS_DP_CTL */
  3553. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3554. const struct drm_display_mode *adjusted_mode =
  3555. &intel_crtc->config->base.adjusted_mode;
  3556. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3557. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3558. temp = I915_READ(reg);
  3559. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3560. TRANS_DP_SYNC_MASK |
  3561. TRANS_DP_BPC_MASK);
  3562. temp |= TRANS_DP_OUTPUT_ENABLE;
  3563. temp |= bpc << 9; /* same format but at 11:9 */
  3564. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3565. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3566. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3567. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3568. switch (intel_trans_dp_port_sel(crtc)) {
  3569. case PORT_B:
  3570. temp |= TRANS_DP_PORT_SEL_B;
  3571. break;
  3572. case PORT_C:
  3573. temp |= TRANS_DP_PORT_SEL_C;
  3574. break;
  3575. case PORT_D:
  3576. temp |= TRANS_DP_PORT_SEL_D;
  3577. break;
  3578. default:
  3579. BUG();
  3580. }
  3581. I915_WRITE(reg, temp);
  3582. }
  3583. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3584. }
  3585. static void lpt_pch_enable(struct drm_crtc *crtc)
  3586. {
  3587. struct drm_device *dev = crtc->dev;
  3588. struct drm_i915_private *dev_priv = dev->dev_private;
  3589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3590. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3591. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3592. lpt_program_iclkip(crtc);
  3593. /* Set transcoder timing. */
  3594. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3595. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3596. }
  3597. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3598. struct intel_crtc_state *crtc_state)
  3599. {
  3600. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3601. struct intel_shared_dpll *pll;
  3602. struct intel_shared_dpll_config *shared_dpll;
  3603. enum intel_dpll_id i;
  3604. int max = dev_priv->num_shared_dpll;
  3605. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3606. if (HAS_PCH_IBX(dev_priv->dev)) {
  3607. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3608. i = (enum intel_dpll_id) crtc->pipe;
  3609. pll = &dev_priv->shared_dplls[i];
  3610. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3611. crtc->base.base.id, pll->name);
  3612. WARN_ON(shared_dpll[i].crtc_mask);
  3613. goto found;
  3614. }
  3615. if (IS_BROXTON(dev_priv->dev)) {
  3616. /* PLL is attached to port in bxt */
  3617. struct intel_encoder *encoder;
  3618. struct intel_digital_port *intel_dig_port;
  3619. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3620. if (WARN_ON(!encoder))
  3621. return NULL;
  3622. intel_dig_port = enc_to_dig_port(&encoder->base);
  3623. /* 1:1 mapping between ports and PLLs */
  3624. i = (enum intel_dpll_id)intel_dig_port->port;
  3625. pll = &dev_priv->shared_dplls[i];
  3626. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3627. crtc->base.base.id, pll->name);
  3628. WARN_ON(shared_dpll[i].crtc_mask);
  3629. goto found;
  3630. } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
  3631. /* Do not consider SPLL */
  3632. max = 2;
  3633. for (i = 0; i < max; i++) {
  3634. pll = &dev_priv->shared_dplls[i];
  3635. /* Only want to check enabled timings first */
  3636. if (shared_dpll[i].crtc_mask == 0)
  3637. continue;
  3638. if (memcmp(&crtc_state->dpll_hw_state,
  3639. &shared_dpll[i].hw_state,
  3640. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3641. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3642. crtc->base.base.id, pll->name,
  3643. shared_dpll[i].crtc_mask,
  3644. pll->active);
  3645. goto found;
  3646. }
  3647. }
  3648. /* Ok no matching timings, maybe there's a free one? */
  3649. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3650. pll = &dev_priv->shared_dplls[i];
  3651. if (shared_dpll[i].crtc_mask == 0) {
  3652. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3653. crtc->base.base.id, pll->name);
  3654. goto found;
  3655. }
  3656. }
  3657. return NULL;
  3658. found:
  3659. if (shared_dpll[i].crtc_mask == 0)
  3660. shared_dpll[i].hw_state =
  3661. crtc_state->dpll_hw_state;
  3662. crtc_state->shared_dpll = i;
  3663. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3664. pipe_name(crtc->pipe));
  3665. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3666. return pll;
  3667. }
  3668. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3669. {
  3670. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3671. struct intel_shared_dpll_config *shared_dpll;
  3672. struct intel_shared_dpll *pll;
  3673. enum intel_dpll_id i;
  3674. if (!to_intel_atomic_state(state)->dpll_set)
  3675. return;
  3676. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3677. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3678. pll = &dev_priv->shared_dplls[i];
  3679. pll->config = shared_dpll[i];
  3680. }
  3681. }
  3682. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3683. {
  3684. struct drm_i915_private *dev_priv = dev->dev_private;
  3685. i915_reg_t dslreg = PIPEDSL(pipe);
  3686. u32 temp;
  3687. temp = I915_READ(dslreg);
  3688. udelay(500);
  3689. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3690. if (wait_for(I915_READ(dslreg) != temp, 5))
  3691. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3692. }
  3693. }
  3694. static int
  3695. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3696. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3697. int src_w, int src_h, int dst_w, int dst_h)
  3698. {
  3699. struct intel_crtc_scaler_state *scaler_state =
  3700. &crtc_state->scaler_state;
  3701. struct intel_crtc *intel_crtc =
  3702. to_intel_crtc(crtc_state->base.crtc);
  3703. int need_scaling;
  3704. need_scaling = intel_rotation_90_or_270(rotation) ?
  3705. (src_h != dst_w || src_w != dst_h):
  3706. (src_w != dst_w || src_h != dst_h);
  3707. /*
  3708. * if plane is being disabled or scaler is no more required or force detach
  3709. * - free scaler binded to this plane/crtc
  3710. * - in order to do this, update crtc->scaler_usage
  3711. *
  3712. * Here scaler state in crtc_state is set free so that
  3713. * scaler can be assigned to other user. Actual register
  3714. * update to free the scaler is done in plane/panel-fit programming.
  3715. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3716. */
  3717. if (force_detach || !need_scaling) {
  3718. if (*scaler_id >= 0) {
  3719. scaler_state->scaler_users &= ~(1 << scaler_user);
  3720. scaler_state->scalers[*scaler_id].in_use = 0;
  3721. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3722. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3723. intel_crtc->pipe, scaler_user, *scaler_id,
  3724. scaler_state->scaler_users);
  3725. *scaler_id = -1;
  3726. }
  3727. return 0;
  3728. }
  3729. /* range checks */
  3730. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3731. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3732. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3733. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3734. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3735. "size is out of scaler range\n",
  3736. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3737. return -EINVAL;
  3738. }
  3739. /* mark this plane as a scaler user in crtc_state */
  3740. scaler_state->scaler_users |= (1 << scaler_user);
  3741. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3742. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3743. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3744. scaler_state->scaler_users);
  3745. return 0;
  3746. }
  3747. /**
  3748. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3749. *
  3750. * @state: crtc's scaler state
  3751. *
  3752. * Return
  3753. * 0 - scaler_usage updated successfully
  3754. * error - requested scaling cannot be supported or other error condition
  3755. */
  3756. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3757. {
  3758. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3759. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3760. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3761. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3762. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3763. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3764. state->pipe_src_w, state->pipe_src_h,
  3765. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3766. }
  3767. /**
  3768. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3769. *
  3770. * @state: crtc's scaler state
  3771. * @plane_state: atomic plane state to update
  3772. *
  3773. * Return
  3774. * 0 - scaler_usage updated successfully
  3775. * error - requested scaling cannot be supported or other error condition
  3776. */
  3777. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3778. struct intel_plane_state *plane_state)
  3779. {
  3780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3781. struct intel_plane *intel_plane =
  3782. to_intel_plane(plane_state->base.plane);
  3783. struct drm_framebuffer *fb = plane_state->base.fb;
  3784. int ret;
  3785. bool force_detach = !fb || !plane_state->visible;
  3786. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3787. intel_plane->base.base.id, intel_crtc->pipe,
  3788. drm_plane_index(&intel_plane->base));
  3789. ret = skl_update_scaler(crtc_state, force_detach,
  3790. drm_plane_index(&intel_plane->base),
  3791. &plane_state->scaler_id,
  3792. plane_state->base.rotation,
  3793. drm_rect_width(&plane_state->src) >> 16,
  3794. drm_rect_height(&plane_state->src) >> 16,
  3795. drm_rect_width(&plane_state->dst),
  3796. drm_rect_height(&plane_state->dst));
  3797. if (ret || plane_state->scaler_id < 0)
  3798. return ret;
  3799. /* check colorkey */
  3800. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3801. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3802. intel_plane->base.base.id);
  3803. return -EINVAL;
  3804. }
  3805. /* Check src format */
  3806. switch (fb->pixel_format) {
  3807. case DRM_FORMAT_RGB565:
  3808. case DRM_FORMAT_XBGR8888:
  3809. case DRM_FORMAT_XRGB8888:
  3810. case DRM_FORMAT_ABGR8888:
  3811. case DRM_FORMAT_ARGB8888:
  3812. case DRM_FORMAT_XRGB2101010:
  3813. case DRM_FORMAT_XBGR2101010:
  3814. case DRM_FORMAT_YUYV:
  3815. case DRM_FORMAT_YVYU:
  3816. case DRM_FORMAT_UYVY:
  3817. case DRM_FORMAT_VYUY:
  3818. break;
  3819. default:
  3820. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3821. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3822. return -EINVAL;
  3823. }
  3824. return 0;
  3825. }
  3826. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3827. {
  3828. int i;
  3829. for (i = 0; i < crtc->num_scalers; i++)
  3830. skl_detach_scaler(crtc, i);
  3831. }
  3832. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3833. {
  3834. struct drm_device *dev = crtc->base.dev;
  3835. struct drm_i915_private *dev_priv = dev->dev_private;
  3836. int pipe = crtc->pipe;
  3837. struct intel_crtc_scaler_state *scaler_state =
  3838. &crtc->config->scaler_state;
  3839. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3840. if (crtc->config->pch_pfit.enabled) {
  3841. int id;
  3842. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3843. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3844. return;
  3845. }
  3846. id = scaler_state->scaler_id;
  3847. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3848. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3849. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3850. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3851. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3852. }
  3853. }
  3854. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3855. {
  3856. struct drm_device *dev = crtc->base.dev;
  3857. struct drm_i915_private *dev_priv = dev->dev_private;
  3858. int pipe = crtc->pipe;
  3859. if (crtc->config->pch_pfit.enabled) {
  3860. /* Force use of hard-coded filter coefficients
  3861. * as some pre-programmed values are broken,
  3862. * e.g. x201.
  3863. */
  3864. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3865. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3866. PF_PIPE_SEL_IVB(pipe));
  3867. else
  3868. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3869. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3870. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3871. }
  3872. }
  3873. void hsw_enable_ips(struct intel_crtc *crtc)
  3874. {
  3875. struct drm_device *dev = crtc->base.dev;
  3876. struct drm_i915_private *dev_priv = dev->dev_private;
  3877. if (!crtc->config->ips_enabled)
  3878. return;
  3879. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3880. intel_wait_for_vblank(dev, crtc->pipe);
  3881. assert_plane_enabled(dev_priv, crtc->plane);
  3882. if (IS_BROADWELL(dev)) {
  3883. mutex_lock(&dev_priv->rps.hw_lock);
  3884. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3885. mutex_unlock(&dev_priv->rps.hw_lock);
  3886. /* Quoting Art Runyan: "its not safe to expect any particular
  3887. * value in IPS_CTL bit 31 after enabling IPS through the
  3888. * mailbox." Moreover, the mailbox may return a bogus state,
  3889. * so we need to just enable it and continue on.
  3890. */
  3891. } else {
  3892. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3893. /* The bit only becomes 1 in the next vblank, so this wait here
  3894. * is essentially intel_wait_for_vblank. If we don't have this
  3895. * and don't wait for vblanks until the end of crtc_enable, then
  3896. * the HW state readout code will complain that the expected
  3897. * IPS_CTL value is not the one we read. */
  3898. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3899. DRM_ERROR("Timed out waiting for IPS enable\n");
  3900. }
  3901. }
  3902. void hsw_disable_ips(struct intel_crtc *crtc)
  3903. {
  3904. struct drm_device *dev = crtc->base.dev;
  3905. struct drm_i915_private *dev_priv = dev->dev_private;
  3906. if (!crtc->config->ips_enabled)
  3907. return;
  3908. assert_plane_enabled(dev_priv, crtc->plane);
  3909. if (IS_BROADWELL(dev)) {
  3910. mutex_lock(&dev_priv->rps.hw_lock);
  3911. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3912. mutex_unlock(&dev_priv->rps.hw_lock);
  3913. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3914. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3915. DRM_ERROR("Timed out waiting for IPS disable\n");
  3916. } else {
  3917. I915_WRITE(IPS_CTL, 0);
  3918. POSTING_READ(IPS_CTL);
  3919. }
  3920. /* We need to wait for a vblank before we can disable the plane. */
  3921. intel_wait_for_vblank(dev, crtc->pipe);
  3922. }
  3923. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3924. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3925. {
  3926. struct drm_device *dev = crtc->dev;
  3927. struct drm_i915_private *dev_priv = dev->dev_private;
  3928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3929. enum pipe pipe = intel_crtc->pipe;
  3930. int i;
  3931. bool reenable_ips = false;
  3932. /* The clocks have to be on to load the palette. */
  3933. if (!crtc->state->active)
  3934. return;
  3935. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3936. if (intel_crtc->config->has_dsi_encoder)
  3937. assert_dsi_pll_enabled(dev_priv);
  3938. else
  3939. assert_pll_enabled(dev_priv, pipe);
  3940. }
  3941. /* Workaround : Do not read or write the pipe palette/gamma data while
  3942. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3943. */
  3944. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3945. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3946. GAMMA_MODE_MODE_SPLIT)) {
  3947. hsw_disable_ips(intel_crtc);
  3948. reenable_ips = true;
  3949. }
  3950. for (i = 0; i < 256; i++) {
  3951. i915_reg_t palreg;
  3952. if (HAS_GMCH_DISPLAY(dev))
  3953. palreg = PALETTE(pipe, i);
  3954. else
  3955. palreg = LGC_PALETTE(pipe, i);
  3956. I915_WRITE(palreg,
  3957. (intel_crtc->lut_r[i] << 16) |
  3958. (intel_crtc->lut_g[i] << 8) |
  3959. intel_crtc->lut_b[i]);
  3960. }
  3961. if (reenable_ips)
  3962. hsw_enable_ips(intel_crtc);
  3963. }
  3964. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3965. {
  3966. if (intel_crtc->overlay) {
  3967. struct drm_device *dev = intel_crtc->base.dev;
  3968. struct drm_i915_private *dev_priv = dev->dev_private;
  3969. mutex_lock(&dev->struct_mutex);
  3970. dev_priv->mm.interruptible = false;
  3971. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3972. dev_priv->mm.interruptible = true;
  3973. mutex_unlock(&dev->struct_mutex);
  3974. }
  3975. /* Let userspace switch the overlay on again. In most cases userspace
  3976. * has to recompute where to put it anyway.
  3977. */
  3978. }
  3979. /**
  3980. * intel_post_enable_primary - Perform operations after enabling primary plane
  3981. * @crtc: the CRTC whose primary plane was just enabled
  3982. *
  3983. * Performs potentially sleeping operations that must be done after the primary
  3984. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3985. * called due to an explicit primary plane update, or due to an implicit
  3986. * re-enable that is caused when a sprite plane is updated to no longer
  3987. * completely hide the primary plane.
  3988. */
  3989. static void
  3990. intel_post_enable_primary(struct drm_crtc *crtc)
  3991. {
  3992. struct drm_device *dev = crtc->dev;
  3993. struct drm_i915_private *dev_priv = dev->dev_private;
  3994. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3995. int pipe = intel_crtc->pipe;
  3996. /*
  3997. * FIXME IPS should be fine as long as one plane is
  3998. * enabled, but in practice it seems to have problems
  3999. * when going from primary only to sprite only and vice
  4000. * versa.
  4001. */
  4002. hsw_enable_ips(intel_crtc);
  4003. /*
  4004. * Gen2 reports pipe underruns whenever all planes are disabled.
  4005. * So don't enable underrun reporting before at least some planes
  4006. * are enabled.
  4007. * FIXME: Need to fix the logic to work when we turn off all planes
  4008. * but leave the pipe running.
  4009. */
  4010. if (IS_GEN2(dev))
  4011. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4012. /* Underruns don't always raise interrupts, so check manually. */
  4013. intel_check_cpu_fifo_underruns(dev_priv);
  4014. intel_check_pch_fifo_underruns(dev_priv);
  4015. }
  4016. /**
  4017. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4018. * @crtc: the CRTC whose primary plane is to be disabled
  4019. *
  4020. * Performs potentially sleeping operations that must be done before the
  4021. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4022. * be called due to an explicit primary plane update, or due to an implicit
  4023. * disable that is caused when a sprite plane completely hides the primary
  4024. * plane.
  4025. */
  4026. static void
  4027. intel_pre_disable_primary(struct drm_crtc *crtc)
  4028. {
  4029. struct drm_device *dev = crtc->dev;
  4030. struct drm_i915_private *dev_priv = dev->dev_private;
  4031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4032. int pipe = intel_crtc->pipe;
  4033. /*
  4034. * Gen2 reports pipe underruns whenever all planes are disabled.
  4035. * So diasble underrun reporting before all the planes get disabled.
  4036. * FIXME: Need to fix the logic to work when we turn off all planes
  4037. * but leave the pipe running.
  4038. */
  4039. if (IS_GEN2(dev))
  4040. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4041. /*
  4042. * Vblank time updates from the shadow to live plane control register
  4043. * are blocked if the memory self-refresh mode is active at that
  4044. * moment. So to make sure the plane gets truly disabled, disable
  4045. * first the self-refresh mode. The self-refresh enable bit in turn
  4046. * will be checked/applied by the HW only at the next frame start
  4047. * event which is after the vblank start event, so we need to have a
  4048. * wait-for-vblank between disabling the plane and the pipe.
  4049. */
  4050. if (HAS_GMCH_DISPLAY(dev)) {
  4051. intel_set_memory_cxsr(dev_priv, false);
  4052. dev_priv->wm.vlv.cxsr = false;
  4053. intel_wait_for_vblank(dev, pipe);
  4054. }
  4055. /*
  4056. * FIXME IPS should be fine as long as one plane is
  4057. * enabled, but in practice it seems to have problems
  4058. * when going from primary only to sprite only and vice
  4059. * versa.
  4060. */
  4061. hsw_disable_ips(intel_crtc);
  4062. }
  4063. static void intel_post_plane_update(struct intel_crtc *crtc)
  4064. {
  4065. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4066. struct intel_crtc_state *pipe_config =
  4067. to_intel_crtc_state(crtc->base.state);
  4068. struct drm_device *dev = crtc->base.dev;
  4069. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4070. crtc->wm.cxsr_allowed = true;
  4071. if (pipe_config->wm_changed && pipe_config->base.active)
  4072. intel_update_watermarks(&crtc->base);
  4073. if (atomic->update_fbc)
  4074. intel_fbc_post_update(crtc);
  4075. if (atomic->post_enable_primary)
  4076. intel_post_enable_primary(&crtc->base);
  4077. memset(atomic, 0, sizeof(*atomic));
  4078. }
  4079. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4080. {
  4081. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4082. struct drm_device *dev = crtc->base.dev;
  4083. struct drm_i915_private *dev_priv = dev->dev_private;
  4084. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4085. struct intel_crtc_state *pipe_config =
  4086. to_intel_crtc_state(crtc->base.state);
  4087. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4088. struct drm_plane *primary = crtc->base.primary;
  4089. struct drm_plane_state *old_pri_state =
  4090. drm_atomic_get_existing_plane_state(old_state, primary);
  4091. bool modeset = needs_modeset(&pipe_config->base);
  4092. if (atomic->update_fbc)
  4093. intel_fbc_pre_update(crtc);
  4094. if (old_pri_state) {
  4095. struct intel_plane_state *primary_state =
  4096. to_intel_plane_state(primary->state);
  4097. struct intel_plane_state *old_primary_state =
  4098. to_intel_plane_state(old_pri_state);
  4099. if (old_primary_state->visible &&
  4100. (modeset || !primary_state->visible))
  4101. intel_pre_disable_primary(&crtc->base);
  4102. }
  4103. if (pipe_config->disable_cxsr) {
  4104. crtc->wm.cxsr_allowed = false;
  4105. if (old_crtc_state->base.active)
  4106. intel_set_memory_cxsr(dev_priv, false);
  4107. }
  4108. if (!needs_modeset(&pipe_config->base) && pipe_config->wm_changed)
  4109. intel_update_watermarks(&crtc->base);
  4110. }
  4111. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4112. {
  4113. struct drm_device *dev = crtc->dev;
  4114. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4115. struct drm_plane *p;
  4116. int pipe = intel_crtc->pipe;
  4117. intel_crtc_dpms_overlay_disable(intel_crtc);
  4118. drm_for_each_plane_mask(p, dev, plane_mask)
  4119. to_intel_plane(p)->disable_plane(p, crtc);
  4120. /*
  4121. * FIXME: Once we grow proper nuclear flip support out of this we need
  4122. * to compute the mask of flip planes precisely. For the time being
  4123. * consider this a flip to a NULL plane.
  4124. */
  4125. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4126. }
  4127. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4128. {
  4129. struct drm_device *dev = crtc->dev;
  4130. struct drm_i915_private *dev_priv = dev->dev_private;
  4131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4132. struct intel_encoder *encoder;
  4133. int pipe = intel_crtc->pipe;
  4134. if (WARN_ON(intel_crtc->active))
  4135. return;
  4136. if (intel_crtc->config->has_pch_encoder)
  4137. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4138. if (intel_crtc->config->has_pch_encoder)
  4139. intel_prepare_shared_dpll(intel_crtc);
  4140. if (intel_crtc->config->has_dp_encoder)
  4141. intel_dp_set_m_n(intel_crtc, M1_N1);
  4142. intel_set_pipe_timings(intel_crtc);
  4143. if (intel_crtc->config->has_pch_encoder) {
  4144. intel_cpu_transcoder_set_m_n(intel_crtc,
  4145. &intel_crtc->config->fdi_m_n, NULL);
  4146. }
  4147. ironlake_set_pipeconf(crtc);
  4148. intel_crtc->active = true;
  4149. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4150. for_each_encoder_on_crtc(dev, crtc, encoder)
  4151. if (encoder->pre_enable)
  4152. encoder->pre_enable(encoder);
  4153. if (intel_crtc->config->has_pch_encoder) {
  4154. /* Note: FDI PLL enabling _must_ be done before we enable the
  4155. * cpu pipes, hence this is separate from all the other fdi/pch
  4156. * enabling. */
  4157. ironlake_fdi_pll_enable(intel_crtc);
  4158. } else {
  4159. assert_fdi_tx_disabled(dev_priv, pipe);
  4160. assert_fdi_rx_disabled(dev_priv, pipe);
  4161. }
  4162. ironlake_pfit_enable(intel_crtc);
  4163. /*
  4164. * On ILK+ LUT must be loaded before the pipe is running but with
  4165. * clocks enabled
  4166. */
  4167. intel_crtc_load_lut(crtc);
  4168. intel_update_watermarks(crtc);
  4169. intel_enable_pipe(intel_crtc);
  4170. if (intel_crtc->config->has_pch_encoder)
  4171. ironlake_pch_enable(crtc);
  4172. assert_vblank_disabled(crtc);
  4173. drm_crtc_vblank_on(crtc);
  4174. for_each_encoder_on_crtc(dev, crtc, encoder)
  4175. encoder->enable(encoder);
  4176. if (HAS_PCH_CPT(dev))
  4177. cpt_verify_modeset(dev, intel_crtc->pipe);
  4178. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4179. if (intel_crtc->config->has_pch_encoder)
  4180. intel_wait_for_vblank(dev, pipe);
  4181. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4182. }
  4183. /* IPS only exists on ULT machines and is tied to pipe A. */
  4184. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4185. {
  4186. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4187. }
  4188. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4189. {
  4190. struct drm_device *dev = crtc->dev;
  4191. struct drm_i915_private *dev_priv = dev->dev_private;
  4192. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4193. struct intel_encoder *encoder;
  4194. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4195. struct intel_crtc_state *pipe_config =
  4196. to_intel_crtc_state(crtc->state);
  4197. if (WARN_ON(intel_crtc->active))
  4198. return;
  4199. if (intel_crtc->config->has_pch_encoder)
  4200. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4201. false);
  4202. if (intel_crtc_to_shared_dpll(intel_crtc))
  4203. intel_enable_shared_dpll(intel_crtc);
  4204. if (intel_crtc->config->has_dp_encoder)
  4205. intel_dp_set_m_n(intel_crtc, M1_N1);
  4206. intel_set_pipe_timings(intel_crtc);
  4207. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4208. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4209. intel_crtc->config->pixel_multiplier - 1);
  4210. }
  4211. if (intel_crtc->config->has_pch_encoder) {
  4212. intel_cpu_transcoder_set_m_n(intel_crtc,
  4213. &intel_crtc->config->fdi_m_n, NULL);
  4214. }
  4215. haswell_set_pipeconf(crtc);
  4216. intel_set_pipe_csc(crtc);
  4217. intel_crtc->active = true;
  4218. if (intel_crtc->config->has_pch_encoder)
  4219. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4220. else
  4221. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4222. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4223. if (encoder->pre_enable)
  4224. encoder->pre_enable(encoder);
  4225. }
  4226. if (intel_crtc->config->has_pch_encoder)
  4227. dev_priv->display.fdi_link_train(crtc);
  4228. if (!intel_crtc->config->has_dsi_encoder)
  4229. intel_ddi_enable_pipe_clock(intel_crtc);
  4230. if (INTEL_INFO(dev)->gen >= 9)
  4231. skylake_pfit_enable(intel_crtc);
  4232. else
  4233. ironlake_pfit_enable(intel_crtc);
  4234. /*
  4235. * On ILK+ LUT must be loaded before the pipe is running but with
  4236. * clocks enabled
  4237. */
  4238. intel_crtc_load_lut(crtc);
  4239. intel_ddi_set_pipe_settings(crtc);
  4240. if (!intel_crtc->config->has_dsi_encoder)
  4241. intel_ddi_enable_transcoder_func(crtc);
  4242. intel_update_watermarks(crtc);
  4243. intel_enable_pipe(intel_crtc);
  4244. if (intel_crtc->config->has_pch_encoder)
  4245. lpt_pch_enable(crtc);
  4246. if (intel_crtc->config->dp_encoder_is_mst)
  4247. intel_ddi_set_vc_payload_alloc(crtc, true);
  4248. assert_vblank_disabled(crtc);
  4249. drm_crtc_vblank_on(crtc);
  4250. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4251. encoder->enable(encoder);
  4252. intel_opregion_notify_encoder(encoder, true);
  4253. }
  4254. if (intel_crtc->config->has_pch_encoder) {
  4255. intel_wait_for_vblank(dev, pipe);
  4256. intel_wait_for_vblank(dev, pipe);
  4257. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4258. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4259. true);
  4260. }
  4261. /* If we change the relative order between pipe/planes enabling, we need
  4262. * to change the workaround. */
  4263. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4264. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4265. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4266. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4267. }
  4268. }
  4269. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4270. {
  4271. struct drm_device *dev = crtc->base.dev;
  4272. struct drm_i915_private *dev_priv = dev->dev_private;
  4273. int pipe = crtc->pipe;
  4274. /* To avoid upsetting the power well on haswell only disable the pfit if
  4275. * it's in use. The hw state code will make sure we get this right. */
  4276. if (force || crtc->config->pch_pfit.enabled) {
  4277. I915_WRITE(PF_CTL(pipe), 0);
  4278. I915_WRITE(PF_WIN_POS(pipe), 0);
  4279. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4280. }
  4281. }
  4282. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4283. {
  4284. struct drm_device *dev = crtc->dev;
  4285. struct drm_i915_private *dev_priv = dev->dev_private;
  4286. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4287. struct intel_encoder *encoder;
  4288. int pipe = intel_crtc->pipe;
  4289. if (intel_crtc->config->has_pch_encoder)
  4290. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4291. for_each_encoder_on_crtc(dev, crtc, encoder)
  4292. encoder->disable(encoder);
  4293. drm_crtc_vblank_off(crtc);
  4294. assert_vblank_disabled(crtc);
  4295. /*
  4296. * Sometimes spurious CPU pipe underruns happen when the
  4297. * pipe is already disabled, but FDI RX/TX is still enabled.
  4298. * Happens at least with VGA+HDMI cloning. Suppress them.
  4299. */
  4300. if (intel_crtc->config->has_pch_encoder)
  4301. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4302. intel_disable_pipe(intel_crtc);
  4303. ironlake_pfit_disable(intel_crtc, false);
  4304. if (intel_crtc->config->has_pch_encoder) {
  4305. ironlake_fdi_disable(crtc);
  4306. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4307. }
  4308. for_each_encoder_on_crtc(dev, crtc, encoder)
  4309. if (encoder->post_disable)
  4310. encoder->post_disable(encoder);
  4311. if (intel_crtc->config->has_pch_encoder) {
  4312. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4313. if (HAS_PCH_CPT(dev)) {
  4314. i915_reg_t reg;
  4315. u32 temp;
  4316. /* disable TRANS_DP_CTL */
  4317. reg = TRANS_DP_CTL(pipe);
  4318. temp = I915_READ(reg);
  4319. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4320. TRANS_DP_PORT_SEL_MASK);
  4321. temp |= TRANS_DP_PORT_SEL_NONE;
  4322. I915_WRITE(reg, temp);
  4323. /* disable DPLL_SEL */
  4324. temp = I915_READ(PCH_DPLL_SEL);
  4325. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4326. I915_WRITE(PCH_DPLL_SEL, temp);
  4327. }
  4328. ironlake_fdi_pll_disable(intel_crtc);
  4329. }
  4330. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4331. }
  4332. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4333. {
  4334. struct drm_device *dev = crtc->dev;
  4335. struct drm_i915_private *dev_priv = dev->dev_private;
  4336. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4337. struct intel_encoder *encoder;
  4338. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4339. if (intel_crtc->config->has_pch_encoder)
  4340. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4341. false);
  4342. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4343. intel_opregion_notify_encoder(encoder, false);
  4344. encoder->disable(encoder);
  4345. }
  4346. drm_crtc_vblank_off(crtc);
  4347. assert_vblank_disabled(crtc);
  4348. intel_disable_pipe(intel_crtc);
  4349. if (intel_crtc->config->dp_encoder_is_mst)
  4350. intel_ddi_set_vc_payload_alloc(crtc, false);
  4351. if (!intel_crtc->config->has_dsi_encoder)
  4352. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4353. if (INTEL_INFO(dev)->gen >= 9)
  4354. skylake_scaler_disable(intel_crtc);
  4355. else
  4356. ironlake_pfit_disable(intel_crtc, false);
  4357. if (!intel_crtc->config->has_dsi_encoder)
  4358. intel_ddi_disable_pipe_clock(intel_crtc);
  4359. for_each_encoder_on_crtc(dev, crtc, encoder)
  4360. if (encoder->post_disable)
  4361. encoder->post_disable(encoder);
  4362. if (intel_crtc->config->has_pch_encoder) {
  4363. lpt_disable_pch_transcoder(dev_priv);
  4364. lpt_disable_iclkip(dev_priv);
  4365. intel_ddi_fdi_disable(crtc);
  4366. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4367. true);
  4368. }
  4369. }
  4370. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4371. {
  4372. struct drm_device *dev = crtc->base.dev;
  4373. struct drm_i915_private *dev_priv = dev->dev_private;
  4374. struct intel_crtc_state *pipe_config = crtc->config;
  4375. if (!pipe_config->gmch_pfit.control)
  4376. return;
  4377. /*
  4378. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4379. * according to register description and PRM.
  4380. */
  4381. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4382. assert_pipe_disabled(dev_priv, crtc->pipe);
  4383. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4384. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4385. /* Border color in case we don't scale up to the full screen. Black by
  4386. * default, change to something else for debugging. */
  4387. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4388. }
  4389. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4390. {
  4391. switch (port) {
  4392. case PORT_A:
  4393. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4394. case PORT_B:
  4395. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4396. case PORT_C:
  4397. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4398. case PORT_D:
  4399. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4400. case PORT_E:
  4401. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4402. default:
  4403. MISSING_CASE(port);
  4404. return POWER_DOMAIN_PORT_OTHER;
  4405. }
  4406. }
  4407. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4408. {
  4409. switch (port) {
  4410. case PORT_A:
  4411. return POWER_DOMAIN_AUX_A;
  4412. case PORT_B:
  4413. return POWER_DOMAIN_AUX_B;
  4414. case PORT_C:
  4415. return POWER_DOMAIN_AUX_C;
  4416. case PORT_D:
  4417. return POWER_DOMAIN_AUX_D;
  4418. case PORT_E:
  4419. /* FIXME: Check VBT for actual wiring of PORT E */
  4420. return POWER_DOMAIN_AUX_D;
  4421. default:
  4422. MISSING_CASE(port);
  4423. return POWER_DOMAIN_AUX_A;
  4424. }
  4425. }
  4426. enum intel_display_power_domain
  4427. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4428. {
  4429. struct drm_device *dev = intel_encoder->base.dev;
  4430. struct intel_digital_port *intel_dig_port;
  4431. switch (intel_encoder->type) {
  4432. case INTEL_OUTPUT_UNKNOWN:
  4433. /* Only DDI platforms should ever use this output type */
  4434. WARN_ON_ONCE(!HAS_DDI(dev));
  4435. case INTEL_OUTPUT_DISPLAYPORT:
  4436. case INTEL_OUTPUT_HDMI:
  4437. case INTEL_OUTPUT_EDP:
  4438. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4439. return port_to_power_domain(intel_dig_port->port);
  4440. case INTEL_OUTPUT_DP_MST:
  4441. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4442. return port_to_power_domain(intel_dig_port->port);
  4443. case INTEL_OUTPUT_ANALOG:
  4444. return POWER_DOMAIN_PORT_CRT;
  4445. case INTEL_OUTPUT_DSI:
  4446. return POWER_DOMAIN_PORT_DSI;
  4447. default:
  4448. return POWER_DOMAIN_PORT_OTHER;
  4449. }
  4450. }
  4451. enum intel_display_power_domain
  4452. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4453. {
  4454. struct drm_device *dev = intel_encoder->base.dev;
  4455. struct intel_digital_port *intel_dig_port;
  4456. switch (intel_encoder->type) {
  4457. case INTEL_OUTPUT_UNKNOWN:
  4458. case INTEL_OUTPUT_HDMI:
  4459. /*
  4460. * Only DDI platforms should ever use these output types.
  4461. * We can get here after the HDMI detect code has already set
  4462. * the type of the shared encoder. Since we can't be sure
  4463. * what's the status of the given connectors, play safe and
  4464. * run the DP detection too.
  4465. */
  4466. WARN_ON_ONCE(!HAS_DDI(dev));
  4467. case INTEL_OUTPUT_DISPLAYPORT:
  4468. case INTEL_OUTPUT_EDP:
  4469. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4470. return port_to_aux_power_domain(intel_dig_port->port);
  4471. case INTEL_OUTPUT_DP_MST:
  4472. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4473. return port_to_aux_power_domain(intel_dig_port->port);
  4474. default:
  4475. MISSING_CASE(intel_encoder->type);
  4476. return POWER_DOMAIN_AUX_A;
  4477. }
  4478. }
  4479. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4480. struct intel_crtc_state *crtc_state)
  4481. {
  4482. struct drm_device *dev = crtc->dev;
  4483. struct drm_encoder *encoder;
  4484. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4485. enum pipe pipe = intel_crtc->pipe;
  4486. unsigned long mask;
  4487. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4488. if (!crtc_state->base.active)
  4489. return 0;
  4490. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4491. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4492. if (crtc_state->pch_pfit.enabled ||
  4493. crtc_state->pch_pfit.force_thru)
  4494. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4495. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4496. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4497. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4498. }
  4499. return mask;
  4500. }
  4501. static unsigned long
  4502. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4503. struct intel_crtc_state *crtc_state)
  4504. {
  4505. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4506. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4507. enum intel_display_power_domain domain;
  4508. unsigned long domains, new_domains, old_domains;
  4509. old_domains = intel_crtc->enabled_power_domains;
  4510. intel_crtc->enabled_power_domains = new_domains =
  4511. get_crtc_power_domains(crtc, crtc_state);
  4512. domains = new_domains & ~old_domains;
  4513. for_each_power_domain(domain, domains)
  4514. intel_display_power_get(dev_priv, domain);
  4515. return old_domains & ~new_domains;
  4516. }
  4517. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4518. unsigned long domains)
  4519. {
  4520. enum intel_display_power_domain domain;
  4521. for_each_power_domain(domain, domains)
  4522. intel_display_power_put(dev_priv, domain);
  4523. }
  4524. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4525. {
  4526. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4527. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4528. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4529. return max_cdclk_freq;
  4530. else if (IS_CHERRYVIEW(dev_priv))
  4531. return max_cdclk_freq*95/100;
  4532. else if (INTEL_INFO(dev_priv)->gen < 4)
  4533. return 2*max_cdclk_freq*90/100;
  4534. else
  4535. return max_cdclk_freq*90/100;
  4536. }
  4537. static void intel_update_max_cdclk(struct drm_device *dev)
  4538. {
  4539. struct drm_i915_private *dev_priv = dev->dev_private;
  4540. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4541. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4542. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4543. dev_priv->max_cdclk_freq = 675000;
  4544. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4545. dev_priv->max_cdclk_freq = 540000;
  4546. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4547. dev_priv->max_cdclk_freq = 450000;
  4548. else
  4549. dev_priv->max_cdclk_freq = 337500;
  4550. } else if (IS_BROADWELL(dev)) {
  4551. /*
  4552. * FIXME with extra cooling we can allow
  4553. * 540 MHz for ULX and 675 Mhz for ULT.
  4554. * How can we know if extra cooling is
  4555. * available? PCI ID, VTB, something else?
  4556. */
  4557. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4558. dev_priv->max_cdclk_freq = 450000;
  4559. else if (IS_BDW_ULX(dev))
  4560. dev_priv->max_cdclk_freq = 450000;
  4561. else if (IS_BDW_ULT(dev))
  4562. dev_priv->max_cdclk_freq = 540000;
  4563. else
  4564. dev_priv->max_cdclk_freq = 675000;
  4565. } else if (IS_CHERRYVIEW(dev)) {
  4566. dev_priv->max_cdclk_freq = 320000;
  4567. } else if (IS_VALLEYVIEW(dev)) {
  4568. dev_priv->max_cdclk_freq = 400000;
  4569. } else {
  4570. /* otherwise assume cdclk is fixed */
  4571. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4572. }
  4573. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4574. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4575. dev_priv->max_cdclk_freq);
  4576. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4577. dev_priv->max_dotclk_freq);
  4578. }
  4579. static void intel_update_cdclk(struct drm_device *dev)
  4580. {
  4581. struct drm_i915_private *dev_priv = dev->dev_private;
  4582. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4583. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4584. dev_priv->cdclk_freq);
  4585. /*
  4586. * Program the gmbus_freq based on the cdclk frequency.
  4587. * BSpec erroneously claims we should aim for 4MHz, but
  4588. * in fact 1MHz is the correct frequency.
  4589. */
  4590. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  4591. /*
  4592. * Program the gmbus_freq based on the cdclk frequency.
  4593. * BSpec erroneously claims we should aim for 4MHz, but
  4594. * in fact 1MHz is the correct frequency.
  4595. */
  4596. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4597. }
  4598. if (dev_priv->max_cdclk_freq == 0)
  4599. intel_update_max_cdclk(dev);
  4600. }
  4601. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4602. {
  4603. struct drm_i915_private *dev_priv = dev->dev_private;
  4604. uint32_t divider;
  4605. uint32_t ratio;
  4606. uint32_t current_freq;
  4607. int ret;
  4608. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4609. switch (frequency) {
  4610. case 144000:
  4611. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4612. ratio = BXT_DE_PLL_RATIO(60);
  4613. break;
  4614. case 288000:
  4615. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4616. ratio = BXT_DE_PLL_RATIO(60);
  4617. break;
  4618. case 384000:
  4619. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4620. ratio = BXT_DE_PLL_RATIO(60);
  4621. break;
  4622. case 576000:
  4623. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4624. ratio = BXT_DE_PLL_RATIO(60);
  4625. break;
  4626. case 624000:
  4627. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4628. ratio = BXT_DE_PLL_RATIO(65);
  4629. break;
  4630. case 19200:
  4631. /*
  4632. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4633. * to suppress GCC warning.
  4634. */
  4635. ratio = 0;
  4636. divider = 0;
  4637. break;
  4638. default:
  4639. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4640. return;
  4641. }
  4642. mutex_lock(&dev_priv->rps.hw_lock);
  4643. /* Inform power controller of upcoming frequency change */
  4644. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4645. 0x80000000);
  4646. mutex_unlock(&dev_priv->rps.hw_lock);
  4647. if (ret) {
  4648. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4649. ret, frequency);
  4650. return;
  4651. }
  4652. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4653. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4654. current_freq = current_freq * 500 + 1000;
  4655. /*
  4656. * DE PLL has to be disabled when
  4657. * - setting to 19.2MHz (bypass, PLL isn't used)
  4658. * - before setting to 624MHz (PLL needs toggling)
  4659. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4660. */
  4661. if (frequency == 19200 || frequency == 624000 ||
  4662. current_freq == 624000) {
  4663. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4664. /* Timeout 200us */
  4665. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4666. 1))
  4667. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4668. }
  4669. if (frequency != 19200) {
  4670. uint32_t val;
  4671. val = I915_READ(BXT_DE_PLL_CTL);
  4672. val &= ~BXT_DE_PLL_RATIO_MASK;
  4673. val |= ratio;
  4674. I915_WRITE(BXT_DE_PLL_CTL, val);
  4675. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4676. /* Timeout 200us */
  4677. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4678. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4679. val = I915_READ(CDCLK_CTL);
  4680. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4681. val |= divider;
  4682. /*
  4683. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4684. * enable otherwise.
  4685. */
  4686. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4687. if (frequency >= 500000)
  4688. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4689. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4690. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4691. val |= (frequency - 1000) / 500;
  4692. I915_WRITE(CDCLK_CTL, val);
  4693. }
  4694. mutex_lock(&dev_priv->rps.hw_lock);
  4695. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4696. DIV_ROUND_UP(frequency, 25000));
  4697. mutex_unlock(&dev_priv->rps.hw_lock);
  4698. if (ret) {
  4699. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4700. ret, frequency);
  4701. return;
  4702. }
  4703. intel_update_cdclk(dev);
  4704. }
  4705. void broxton_init_cdclk(struct drm_device *dev)
  4706. {
  4707. struct drm_i915_private *dev_priv = dev->dev_private;
  4708. uint32_t val;
  4709. /*
  4710. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4711. * or else the reset will hang because there is no PCH to respond.
  4712. * Move the handshake programming to initialization sequence.
  4713. * Previously was left up to BIOS.
  4714. */
  4715. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4716. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4717. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4718. /* Enable PG1 for cdclk */
  4719. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4720. /* check if cd clock is enabled */
  4721. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4722. DRM_DEBUG_KMS("Display already initialized\n");
  4723. return;
  4724. }
  4725. /*
  4726. * FIXME:
  4727. * - The initial CDCLK needs to be read from VBT.
  4728. * Need to make this change after VBT has changes for BXT.
  4729. * - check if setting the max (or any) cdclk freq is really necessary
  4730. * here, it belongs to modeset time
  4731. */
  4732. broxton_set_cdclk(dev, 624000);
  4733. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4734. POSTING_READ(DBUF_CTL);
  4735. udelay(10);
  4736. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4737. DRM_ERROR("DBuf power enable timeout!\n");
  4738. }
  4739. void broxton_uninit_cdclk(struct drm_device *dev)
  4740. {
  4741. struct drm_i915_private *dev_priv = dev->dev_private;
  4742. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4743. POSTING_READ(DBUF_CTL);
  4744. udelay(10);
  4745. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4746. DRM_ERROR("DBuf power disable timeout!\n");
  4747. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4748. broxton_set_cdclk(dev, 19200);
  4749. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4750. }
  4751. static const struct skl_cdclk_entry {
  4752. unsigned int freq;
  4753. unsigned int vco;
  4754. } skl_cdclk_frequencies[] = {
  4755. { .freq = 308570, .vco = 8640 },
  4756. { .freq = 337500, .vco = 8100 },
  4757. { .freq = 432000, .vco = 8640 },
  4758. { .freq = 450000, .vco = 8100 },
  4759. { .freq = 540000, .vco = 8100 },
  4760. { .freq = 617140, .vco = 8640 },
  4761. { .freq = 675000, .vco = 8100 },
  4762. };
  4763. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4764. {
  4765. return (freq - 1000) / 500;
  4766. }
  4767. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4768. {
  4769. unsigned int i;
  4770. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4771. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4772. if (e->freq == freq)
  4773. return e->vco;
  4774. }
  4775. return 8100;
  4776. }
  4777. static void
  4778. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4779. {
  4780. unsigned int min_freq;
  4781. u32 val;
  4782. /* select the minimum CDCLK before enabling DPLL 0 */
  4783. val = I915_READ(CDCLK_CTL);
  4784. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4785. val |= CDCLK_FREQ_337_308;
  4786. if (required_vco == 8640)
  4787. min_freq = 308570;
  4788. else
  4789. min_freq = 337500;
  4790. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4791. I915_WRITE(CDCLK_CTL, val);
  4792. POSTING_READ(CDCLK_CTL);
  4793. /*
  4794. * We always enable DPLL0 with the lowest link rate possible, but still
  4795. * taking into account the VCO required to operate the eDP panel at the
  4796. * desired frequency. The usual DP link rates operate with a VCO of
  4797. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4798. * The modeset code is responsible for the selection of the exact link
  4799. * rate later on, with the constraint of choosing a frequency that
  4800. * works with required_vco.
  4801. */
  4802. val = I915_READ(DPLL_CTRL1);
  4803. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4804. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4805. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4806. if (required_vco == 8640)
  4807. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4808. SKL_DPLL0);
  4809. else
  4810. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4811. SKL_DPLL0);
  4812. I915_WRITE(DPLL_CTRL1, val);
  4813. POSTING_READ(DPLL_CTRL1);
  4814. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4815. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4816. DRM_ERROR("DPLL0 not locked\n");
  4817. }
  4818. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4819. {
  4820. int ret;
  4821. u32 val;
  4822. /* inform PCU we want to change CDCLK */
  4823. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4824. mutex_lock(&dev_priv->rps.hw_lock);
  4825. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4826. mutex_unlock(&dev_priv->rps.hw_lock);
  4827. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4828. }
  4829. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4830. {
  4831. unsigned int i;
  4832. for (i = 0; i < 15; i++) {
  4833. if (skl_cdclk_pcu_ready(dev_priv))
  4834. return true;
  4835. udelay(10);
  4836. }
  4837. return false;
  4838. }
  4839. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4840. {
  4841. struct drm_device *dev = dev_priv->dev;
  4842. u32 freq_select, pcu_ack;
  4843. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4844. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4845. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4846. return;
  4847. }
  4848. /* set CDCLK_CTL */
  4849. switch(freq) {
  4850. case 450000:
  4851. case 432000:
  4852. freq_select = CDCLK_FREQ_450_432;
  4853. pcu_ack = 1;
  4854. break;
  4855. case 540000:
  4856. freq_select = CDCLK_FREQ_540;
  4857. pcu_ack = 2;
  4858. break;
  4859. case 308570:
  4860. case 337500:
  4861. default:
  4862. freq_select = CDCLK_FREQ_337_308;
  4863. pcu_ack = 0;
  4864. break;
  4865. case 617140:
  4866. case 675000:
  4867. freq_select = CDCLK_FREQ_675_617;
  4868. pcu_ack = 3;
  4869. break;
  4870. }
  4871. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4872. POSTING_READ(CDCLK_CTL);
  4873. /* inform PCU of the change */
  4874. mutex_lock(&dev_priv->rps.hw_lock);
  4875. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4876. mutex_unlock(&dev_priv->rps.hw_lock);
  4877. intel_update_cdclk(dev);
  4878. }
  4879. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4880. {
  4881. /* disable DBUF power */
  4882. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4883. POSTING_READ(DBUF_CTL);
  4884. udelay(10);
  4885. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4886. DRM_ERROR("DBuf power disable timeout\n");
  4887. /* disable DPLL0 */
  4888. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4889. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4890. DRM_ERROR("Couldn't disable DPLL0\n");
  4891. }
  4892. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4893. {
  4894. unsigned int required_vco;
  4895. /* DPLL0 not enabled (happens on early BIOS versions) */
  4896. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4897. /* enable DPLL0 */
  4898. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4899. skl_dpll0_enable(dev_priv, required_vco);
  4900. }
  4901. /* set CDCLK to the frequency the BIOS chose */
  4902. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4903. /* enable DBUF power */
  4904. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4905. POSTING_READ(DBUF_CTL);
  4906. udelay(10);
  4907. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4908. DRM_ERROR("DBuf power enable timeout\n");
  4909. }
  4910. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4911. {
  4912. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4913. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4914. int freq = dev_priv->skl_boot_cdclk;
  4915. /*
  4916. * check if the pre-os intialized the display
  4917. * There is SWF18 scratchpad register defined which is set by the
  4918. * pre-os which can be used by the OS drivers to check the status
  4919. */
  4920. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4921. goto sanitize;
  4922. /* Is PLL enabled and locked ? */
  4923. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4924. goto sanitize;
  4925. /* DPLL okay; verify the cdclock
  4926. *
  4927. * Noticed in some instances that the freq selection is correct but
  4928. * decimal part is programmed wrong from BIOS where pre-os does not
  4929. * enable display. Verify the same as well.
  4930. */
  4931. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4932. /* All well; nothing to sanitize */
  4933. return false;
  4934. sanitize:
  4935. /*
  4936. * As of now initialize with max cdclk till
  4937. * we get dynamic cdclk support
  4938. * */
  4939. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4940. skl_init_cdclk(dev_priv);
  4941. /* we did have to sanitize */
  4942. return true;
  4943. }
  4944. /* Adjust CDclk dividers to allow high res or save power if possible */
  4945. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4946. {
  4947. struct drm_i915_private *dev_priv = dev->dev_private;
  4948. u32 val, cmd;
  4949. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4950. != dev_priv->cdclk_freq);
  4951. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4952. cmd = 2;
  4953. else if (cdclk == 266667)
  4954. cmd = 1;
  4955. else
  4956. cmd = 0;
  4957. mutex_lock(&dev_priv->rps.hw_lock);
  4958. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4959. val &= ~DSPFREQGUAR_MASK;
  4960. val |= (cmd << DSPFREQGUAR_SHIFT);
  4961. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4962. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4963. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4964. 50)) {
  4965. DRM_ERROR("timed out waiting for CDclk change\n");
  4966. }
  4967. mutex_unlock(&dev_priv->rps.hw_lock);
  4968. mutex_lock(&dev_priv->sb_lock);
  4969. if (cdclk == 400000) {
  4970. u32 divider;
  4971. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4972. /* adjust cdclk divider */
  4973. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4974. val &= ~CCK_FREQUENCY_VALUES;
  4975. val |= divider;
  4976. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4977. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4978. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4979. 50))
  4980. DRM_ERROR("timed out waiting for CDclk change\n");
  4981. }
  4982. /* adjust self-refresh exit latency value */
  4983. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4984. val &= ~0x7f;
  4985. /*
  4986. * For high bandwidth configs, we set a higher latency in the bunit
  4987. * so that the core display fetch happens in time to avoid underruns.
  4988. */
  4989. if (cdclk == 400000)
  4990. val |= 4500 / 250; /* 4.5 usec */
  4991. else
  4992. val |= 3000 / 250; /* 3.0 usec */
  4993. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4994. mutex_unlock(&dev_priv->sb_lock);
  4995. intel_update_cdclk(dev);
  4996. }
  4997. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4998. {
  4999. struct drm_i915_private *dev_priv = dev->dev_private;
  5000. u32 val, cmd;
  5001. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5002. != dev_priv->cdclk_freq);
  5003. switch (cdclk) {
  5004. case 333333:
  5005. case 320000:
  5006. case 266667:
  5007. case 200000:
  5008. break;
  5009. default:
  5010. MISSING_CASE(cdclk);
  5011. return;
  5012. }
  5013. /*
  5014. * Specs are full of misinformation, but testing on actual
  5015. * hardware has shown that we just need to write the desired
  5016. * CCK divider into the Punit register.
  5017. */
  5018. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5019. mutex_lock(&dev_priv->rps.hw_lock);
  5020. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5021. val &= ~DSPFREQGUAR_MASK_CHV;
  5022. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5023. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5024. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5025. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5026. 50)) {
  5027. DRM_ERROR("timed out waiting for CDclk change\n");
  5028. }
  5029. mutex_unlock(&dev_priv->rps.hw_lock);
  5030. intel_update_cdclk(dev);
  5031. }
  5032. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5033. int max_pixclk)
  5034. {
  5035. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5036. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5037. /*
  5038. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5039. * 200MHz
  5040. * 267MHz
  5041. * 320/333MHz (depends on HPLL freq)
  5042. * 400MHz (VLV only)
  5043. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5044. * of the lower bin and adjust if needed.
  5045. *
  5046. * We seem to get an unstable or solid color picture at 200MHz.
  5047. * Not sure what's wrong. For now use 200MHz only when all pipes
  5048. * are off.
  5049. */
  5050. if (!IS_CHERRYVIEW(dev_priv) &&
  5051. max_pixclk > freq_320*limit/100)
  5052. return 400000;
  5053. else if (max_pixclk > 266667*limit/100)
  5054. return freq_320;
  5055. else if (max_pixclk > 0)
  5056. return 266667;
  5057. else
  5058. return 200000;
  5059. }
  5060. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5061. int max_pixclk)
  5062. {
  5063. /*
  5064. * FIXME:
  5065. * - remove the guardband, it's not needed on BXT
  5066. * - set 19.2MHz bypass frequency if there are no active pipes
  5067. */
  5068. if (max_pixclk > 576000*9/10)
  5069. return 624000;
  5070. else if (max_pixclk > 384000*9/10)
  5071. return 576000;
  5072. else if (max_pixclk > 288000*9/10)
  5073. return 384000;
  5074. else if (max_pixclk > 144000*9/10)
  5075. return 288000;
  5076. else
  5077. return 144000;
  5078. }
  5079. /* Compute the max pixel clock for new configuration. */
  5080. static int intel_mode_max_pixclk(struct drm_device *dev,
  5081. struct drm_atomic_state *state)
  5082. {
  5083. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5084. struct drm_i915_private *dev_priv = dev->dev_private;
  5085. struct drm_crtc *crtc;
  5086. struct drm_crtc_state *crtc_state;
  5087. unsigned max_pixclk = 0, i;
  5088. enum pipe pipe;
  5089. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5090. sizeof(intel_state->min_pixclk));
  5091. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5092. int pixclk = 0;
  5093. if (crtc_state->enable)
  5094. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5095. intel_state->min_pixclk[i] = pixclk;
  5096. }
  5097. for_each_pipe(dev_priv, pipe)
  5098. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5099. return max_pixclk;
  5100. }
  5101. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5102. {
  5103. struct drm_device *dev = state->dev;
  5104. struct drm_i915_private *dev_priv = dev->dev_private;
  5105. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5106. struct intel_atomic_state *intel_state =
  5107. to_intel_atomic_state(state);
  5108. if (max_pixclk < 0)
  5109. return max_pixclk;
  5110. intel_state->cdclk = intel_state->dev_cdclk =
  5111. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5112. if (!intel_state->active_crtcs)
  5113. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5114. return 0;
  5115. }
  5116. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5117. {
  5118. struct drm_device *dev = state->dev;
  5119. struct drm_i915_private *dev_priv = dev->dev_private;
  5120. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5121. struct intel_atomic_state *intel_state =
  5122. to_intel_atomic_state(state);
  5123. if (max_pixclk < 0)
  5124. return max_pixclk;
  5125. intel_state->cdclk = intel_state->dev_cdclk =
  5126. broxton_calc_cdclk(dev_priv, max_pixclk);
  5127. if (!intel_state->active_crtcs)
  5128. intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
  5129. return 0;
  5130. }
  5131. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5132. {
  5133. unsigned int credits, default_credits;
  5134. if (IS_CHERRYVIEW(dev_priv))
  5135. default_credits = PFI_CREDIT(12);
  5136. else
  5137. default_credits = PFI_CREDIT(8);
  5138. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5139. /* CHV suggested value is 31 or 63 */
  5140. if (IS_CHERRYVIEW(dev_priv))
  5141. credits = PFI_CREDIT_63;
  5142. else
  5143. credits = PFI_CREDIT(15);
  5144. } else {
  5145. credits = default_credits;
  5146. }
  5147. /*
  5148. * WA - write default credits before re-programming
  5149. * FIXME: should we also set the resend bit here?
  5150. */
  5151. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5152. default_credits);
  5153. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5154. credits | PFI_CREDIT_RESEND);
  5155. /*
  5156. * FIXME is this guaranteed to clear
  5157. * immediately or should we poll for it?
  5158. */
  5159. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5160. }
  5161. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5162. {
  5163. struct drm_device *dev = old_state->dev;
  5164. struct drm_i915_private *dev_priv = dev->dev_private;
  5165. struct intel_atomic_state *old_intel_state =
  5166. to_intel_atomic_state(old_state);
  5167. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5168. /*
  5169. * FIXME: We can end up here with all power domains off, yet
  5170. * with a CDCLK frequency other than the minimum. To account
  5171. * for this take the PIPE-A power domain, which covers the HW
  5172. * blocks needed for the following programming. This can be
  5173. * removed once it's guaranteed that we get here either with
  5174. * the minimum CDCLK set, or the required power domains
  5175. * enabled.
  5176. */
  5177. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5178. if (IS_CHERRYVIEW(dev))
  5179. cherryview_set_cdclk(dev, req_cdclk);
  5180. else
  5181. valleyview_set_cdclk(dev, req_cdclk);
  5182. vlv_program_pfi_credits(dev_priv);
  5183. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5184. }
  5185. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5186. {
  5187. struct drm_device *dev = crtc->dev;
  5188. struct drm_i915_private *dev_priv = to_i915(dev);
  5189. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5190. struct intel_encoder *encoder;
  5191. int pipe = intel_crtc->pipe;
  5192. if (WARN_ON(intel_crtc->active))
  5193. return;
  5194. if (intel_crtc->config->has_dp_encoder)
  5195. intel_dp_set_m_n(intel_crtc, M1_N1);
  5196. intel_set_pipe_timings(intel_crtc);
  5197. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5198. struct drm_i915_private *dev_priv = dev->dev_private;
  5199. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5200. I915_WRITE(CHV_CANVAS(pipe), 0);
  5201. }
  5202. i9xx_set_pipeconf(intel_crtc);
  5203. intel_crtc->active = true;
  5204. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5205. for_each_encoder_on_crtc(dev, crtc, encoder)
  5206. if (encoder->pre_pll_enable)
  5207. encoder->pre_pll_enable(encoder);
  5208. if (!intel_crtc->config->has_dsi_encoder) {
  5209. if (IS_CHERRYVIEW(dev)) {
  5210. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5211. chv_enable_pll(intel_crtc, intel_crtc->config);
  5212. } else {
  5213. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5214. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5215. }
  5216. }
  5217. for_each_encoder_on_crtc(dev, crtc, encoder)
  5218. if (encoder->pre_enable)
  5219. encoder->pre_enable(encoder);
  5220. i9xx_pfit_enable(intel_crtc);
  5221. intel_crtc_load_lut(crtc);
  5222. intel_enable_pipe(intel_crtc);
  5223. assert_vblank_disabled(crtc);
  5224. drm_crtc_vblank_on(crtc);
  5225. for_each_encoder_on_crtc(dev, crtc, encoder)
  5226. encoder->enable(encoder);
  5227. }
  5228. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5229. {
  5230. struct drm_device *dev = crtc->base.dev;
  5231. struct drm_i915_private *dev_priv = dev->dev_private;
  5232. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5233. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5234. }
  5235. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5236. {
  5237. struct drm_device *dev = crtc->dev;
  5238. struct drm_i915_private *dev_priv = to_i915(dev);
  5239. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5240. struct intel_encoder *encoder;
  5241. int pipe = intel_crtc->pipe;
  5242. if (WARN_ON(intel_crtc->active))
  5243. return;
  5244. i9xx_set_pll_dividers(intel_crtc);
  5245. if (intel_crtc->config->has_dp_encoder)
  5246. intel_dp_set_m_n(intel_crtc, M1_N1);
  5247. intel_set_pipe_timings(intel_crtc);
  5248. i9xx_set_pipeconf(intel_crtc);
  5249. intel_crtc->active = true;
  5250. if (!IS_GEN2(dev))
  5251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5252. for_each_encoder_on_crtc(dev, crtc, encoder)
  5253. if (encoder->pre_enable)
  5254. encoder->pre_enable(encoder);
  5255. i9xx_enable_pll(intel_crtc);
  5256. i9xx_pfit_enable(intel_crtc);
  5257. intel_crtc_load_lut(crtc);
  5258. intel_update_watermarks(crtc);
  5259. intel_enable_pipe(intel_crtc);
  5260. assert_vblank_disabled(crtc);
  5261. drm_crtc_vblank_on(crtc);
  5262. for_each_encoder_on_crtc(dev, crtc, encoder)
  5263. encoder->enable(encoder);
  5264. }
  5265. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5266. {
  5267. struct drm_device *dev = crtc->base.dev;
  5268. struct drm_i915_private *dev_priv = dev->dev_private;
  5269. if (!crtc->config->gmch_pfit.control)
  5270. return;
  5271. assert_pipe_disabled(dev_priv, crtc->pipe);
  5272. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5273. I915_READ(PFIT_CONTROL));
  5274. I915_WRITE(PFIT_CONTROL, 0);
  5275. }
  5276. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5277. {
  5278. struct drm_device *dev = crtc->dev;
  5279. struct drm_i915_private *dev_priv = dev->dev_private;
  5280. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5281. struct intel_encoder *encoder;
  5282. int pipe = intel_crtc->pipe;
  5283. /*
  5284. * On gen2 planes are double buffered but the pipe isn't, so we must
  5285. * wait for planes to fully turn off before disabling the pipe.
  5286. * We also need to wait on all gmch platforms because of the
  5287. * self-refresh mode constraint explained above.
  5288. */
  5289. intel_wait_for_vblank(dev, pipe);
  5290. for_each_encoder_on_crtc(dev, crtc, encoder)
  5291. encoder->disable(encoder);
  5292. drm_crtc_vblank_off(crtc);
  5293. assert_vblank_disabled(crtc);
  5294. intel_disable_pipe(intel_crtc);
  5295. i9xx_pfit_disable(intel_crtc);
  5296. for_each_encoder_on_crtc(dev, crtc, encoder)
  5297. if (encoder->post_disable)
  5298. encoder->post_disable(encoder);
  5299. if (!intel_crtc->config->has_dsi_encoder) {
  5300. if (IS_CHERRYVIEW(dev))
  5301. chv_disable_pll(dev_priv, pipe);
  5302. else if (IS_VALLEYVIEW(dev))
  5303. vlv_disable_pll(dev_priv, pipe);
  5304. else
  5305. i9xx_disable_pll(intel_crtc);
  5306. }
  5307. for_each_encoder_on_crtc(dev, crtc, encoder)
  5308. if (encoder->post_pll_disable)
  5309. encoder->post_pll_disable(encoder);
  5310. if (!IS_GEN2(dev))
  5311. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5312. }
  5313. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5314. {
  5315. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5316. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5317. enum intel_display_power_domain domain;
  5318. unsigned long domains;
  5319. if (!intel_crtc->active)
  5320. return;
  5321. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5322. WARN_ON(intel_crtc->unpin_work);
  5323. intel_pre_disable_primary(crtc);
  5324. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5325. to_intel_plane_state(crtc->primary->state)->visible = false;
  5326. }
  5327. dev_priv->display.crtc_disable(crtc);
  5328. intel_crtc->active = false;
  5329. intel_fbc_disable(intel_crtc);
  5330. intel_update_watermarks(crtc);
  5331. intel_disable_shared_dpll(intel_crtc);
  5332. domains = intel_crtc->enabled_power_domains;
  5333. for_each_power_domain(domain, domains)
  5334. intel_display_power_put(dev_priv, domain);
  5335. intel_crtc->enabled_power_domains = 0;
  5336. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5337. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5338. }
  5339. /*
  5340. * turn all crtc's off, but do not adjust state
  5341. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5342. */
  5343. int intel_display_suspend(struct drm_device *dev)
  5344. {
  5345. struct drm_i915_private *dev_priv = to_i915(dev);
  5346. struct drm_atomic_state *state;
  5347. int ret;
  5348. state = drm_atomic_helper_suspend(dev);
  5349. ret = PTR_ERR_OR_ZERO(state);
  5350. if (ret)
  5351. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5352. else
  5353. dev_priv->modeset_restore_state = state;
  5354. return ret;
  5355. }
  5356. void intel_encoder_destroy(struct drm_encoder *encoder)
  5357. {
  5358. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5359. drm_encoder_cleanup(encoder);
  5360. kfree(intel_encoder);
  5361. }
  5362. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5363. * internal consistency). */
  5364. static void intel_connector_check_state(struct intel_connector *connector)
  5365. {
  5366. struct drm_crtc *crtc = connector->base.state->crtc;
  5367. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5368. connector->base.base.id,
  5369. connector->base.name);
  5370. if (connector->get_hw_state(connector)) {
  5371. struct intel_encoder *encoder = connector->encoder;
  5372. struct drm_connector_state *conn_state = connector->base.state;
  5373. I915_STATE_WARN(!crtc,
  5374. "connector enabled without attached crtc\n");
  5375. if (!crtc)
  5376. return;
  5377. I915_STATE_WARN(!crtc->state->active,
  5378. "connector is active, but attached crtc isn't\n");
  5379. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5380. return;
  5381. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5382. "atomic encoder doesn't match attached encoder\n");
  5383. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5384. "attached encoder crtc differs from connector crtc\n");
  5385. } else {
  5386. I915_STATE_WARN(crtc && crtc->state->active,
  5387. "attached crtc is active, but connector isn't\n");
  5388. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5389. "best encoder set without crtc!\n");
  5390. }
  5391. }
  5392. int intel_connector_init(struct intel_connector *connector)
  5393. {
  5394. drm_atomic_helper_connector_reset(&connector->base);
  5395. if (!connector->base.state)
  5396. return -ENOMEM;
  5397. return 0;
  5398. }
  5399. struct intel_connector *intel_connector_alloc(void)
  5400. {
  5401. struct intel_connector *connector;
  5402. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5403. if (!connector)
  5404. return NULL;
  5405. if (intel_connector_init(connector) < 0) {
  5406. kfree(connector);
  5407. return NULL;
  5408. }
  5409. return connector;
  5410. }
  5411. /* Simple connector->get_hw_state implementation for encoders that support only
  5412. * one connector and no cloning and hence the encoder state determines the state
  5413. * of the connector. */
  5414. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5415. {
  5416. enum pipe pipe = 0;
  5417. struct intel_encoder *encoder = connector->encoder;
  5418. return encoder->get_hw_state(encoder, &pipe);
  5419. }
  5420. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5421. {
  5422. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5423. return crtc_state->fdi_lanes;
  5424. return 0;
  5425. }
  5426. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5427. struct intel_crtc_state *pipe_config)
  5428. {
  5429. struct drm_atomic_state *state = pipe_config->base.state;
  5430. struct intel_crtc *other_crtc;
  5431. struct intel_crtc_state *other_crtc_state;
  5432. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5433. pipe_name(pipe), pipe_config->fdi_lanes);
  5434. if (pipe_config->fdi_lanes > 4) {
  5435. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5436. pipe_name(pipe), pipe_config->fdi_lanes);
  5437. return -EINVAL;
  5438. }
  5439. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5440. if (pipe_config->fdi_lanes > 2) {
  5441. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5442. pipe_config->fdi_lanes);
  5443. return -EINVAL;
  5444. } else {
  5445. return 0;
  5446. }
  5447. }
  5448. if (INTEL_INFO(dev)->num_pipes == 2)
  5449. return 0;
  5450. /* Ivybridge 3 pipe is really complicated */
  5451. switch (pipe) {
  5452. case PIPE_A:
  5453. return 0;
  5454. case PIPE_B:
  5455. if (pipe_config->fdi_lanes <= 2)
  5456. return 0;
  5457. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5458. other_crtc_state =
  5459. intel_atomic_get_crtc_state(state, other_crtc);
  5460. if (IS_ERR(other_crtc_state))
  5461. return PTR_ERR(other_crtc_state);
  5462. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5463. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5464. pipe_name(pipe), pipe_config->fdi_lanes);
  5465. return -EINVAL;
  5466. }
  5467. return 0;
  5468. case PIPE_C:
  5469. if (pipe_config->fdi_lanes > 2) {
  5470. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5471. pipe_name(pipe), pipe_config->fdi_lanes);
  5472. return -EINVAL;
  5473. }
  5474. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5475. other_crtc_state =
  5476. intel_atomic_get_crtc_state(state, other_crtc);
  5477. if (IS_ERR(other_crtc_state))
  5478. return PTR_ERR(other_crtc_state);
  5479. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5480. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5481. return -EINVAL;
  5482. }
  5483. return 0;
  5484. default:
  5485. BUG();
  5486. }
  5487. }
  5488. #define RETRY 1
  5489. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5490. struct intel_crtc_state *pipe_config)
  5491. {
  5492. struct drm_device *dev = intel_crtc->base.dev;
  5493. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5494. int lane, link_bw, fdi_dotclock, ret;
  5495. bool needs_recompute = false;
  5496. retry:
  5497. /* FDI is a binary signal running at ~2.7GHz, encoding
  5498. * each output octet as 10 bits. The actual frequency
  5499. * is stored as a divider into a 100MHz clock, and the
  5500. * mode pixel clock is stored in units of 1KHz.
  5501. * Hence the bw of each lane in terms of the mode signal
  5502. * is:
  5503. */
  5504. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5505. fdi_dotclock = adjusted_mode->crtc_clock;
  5506. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5507. pipe_config->pipe_bpp);
  5508. pipe_config->fdi_lanes = lane;
  5509. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5510. link_bw, &pipe_config->fdi_m_n);
  5511. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5512. intel_crtc->pipe, pipe_config);
  5513. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5514. pipe_config->pipe_bpp -= 2*3;
  5515. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5516. pipe_config->pipe_bpp);
  5517. needs_recompute = true;
  5518. pipe_config->bw_constrained = true;
  5519. goto retry;
  5520. }
  5521. if (needs_recompute)
  5522. return RETRY;
  5523. return ret;
  5524. }
  5525. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5526. struct intel_crtc_state *pipe_config)
  5527. {
  5528. if (pipe_config->pipe_bpp > 24)
  5529. return false;
  5530. /* HSW can handle pixel rate up to cdclk? */
  5531. if (IS_HASWELL(dev_priv->dev))
  5532. return true;
  5533. /*
  5534. * We compare against max which means we must take
  5535. * the increased cdclk requirement into account when
  5536. * calculating the new cdclk.
  5537. *
  5538. * Should measure whether using a lower cdclk w/o IPS
  5539. */
  5540. return ilk_pipe_pixel_rate(pipe_config) <=
  5541. dev_priv->max_cdclk_freq * 95 / 100;
  5542. }
  5543. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5544. struct intel_crtc_state *pipe_config)
  5545. {
  5546. struct drm_device *dev = crtc->base.dev;
  5547. struct drm_i915_private *dev_priv = dev->dev_private;
  5548. pipe_config->ips_enabled = i915.enable_ips &&
  5549. hsw_crtc_supports_ips(crtc) &&
  5550. pipe_config_supports_ips(dev_priv, pipe_config);
  5551. }
  5552. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5553. {
  5554. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5555. /* GDG double wide on either pipe, otherwise pipe A only */
  5556. return INTEL_INFO(dev_priv)->gen < 4 &&
  5557. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5558. }
  5559. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5560. struct intel_crtc_state *pipe_config)
  5561. {
  5562. struct drm_device *dev = crtc->base.dev;
  5563. struct drm_i915_private *dev_priv = dev->dev_private;
  5564. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5565. /* FIXME should check pixel clock limits on all platforms */
  5566. if (INTEL_INFO(dev)->gen < 4) {
  5567. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5568. /*
  5569. * Enable double wide mode when the dot clock
  5570. * is > 90% of the (display) core speed.
  5571. */
  5572. if (intel_crtc_supports_double_wide(crtc) &&
  5573. adjusted_mode->crtc_clock > clock_limit) {
  5574. clock_limit *= 2;
  5575. pipe_config->double_wide = true;
  5576. }
  5577. if (adjusted_mode->crtc_clock > clock_limit) {
  5578. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5579. adjusted_mode->crtc_clock, clock_limit,
  5580. yesno(pipe_config->double_wide));
  5581. return -EINVAL;
  5582. }
  5583. }
  5584. /*
  5585. * Pipe horizontal size must be even in:
  5586. * - DVO ganged mode
  5587. * - LVDS dual channel mode
  5588. * - Double wide pipe
  5589. */
  5590. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5591. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5592. pipe_config->pipe_src_w &= ~1;
  5593. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5594. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5595. */
  5596. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5597. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5598. return -EINVAL;
  5599. if (HAS_IPS(dev))
  5600. hsw_compute_ips_config(crtc, pipe_config);
  5601. if (pipe_config->has_pch_encoder)
  5602. return ironlake_fdi_compute_config(crtc, pipe_config);
  5603. return 0;
  5604. }
  5605. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5606. {
  5607. struct drm_i915_private *dev_priv = to_i915(dev);
  5608. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5609. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5610. uint32_t linkrate;
  5611. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5612. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5613. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5614. return 540000;
  5615. linkrate = (I915_READ(DPLL_CTRL1) &
  5616. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5617. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5618. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5619. /* vco 8640 */
  5620. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5621. case CDCLK_FREQ_450_432:
  5622. return 432000;
  5623. case CDCLK_FREQ_337_308:
  5624. return 308570;
  5625. case CDCLK_FREQ_675_617:
  5626. return 617140;
  5627. default:
  5628. WARN(1, "Unknown cd freq selection\n");
  5629. }
  5630. } else {
  5631. /* vco 8100 */
  5632. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5633. case CDCLK_FREQ_450_432:
  5634. return 450000;
  5635. case CDCLK_FREQ_337_308:
  5636. return 337500;
  5637. case CDCLK_FREQ_675_617:
  5638. return 675000;
  5639. default:
  5640. WARN(1, "Unknown cd freq selection\n");
  5641. }
  5642. }
  5643. /* error case, do as if DPLL0 isn't enabled */
  5644. return 24000;
  5645. }
  5646. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5647. {
  5648. struct drm_i915_private *dev_priv = to_i915(dev);
  5649. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5650. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5651. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5652. int cdclk;
  5653. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5654. return 19200;
  5655. cdclk = 19200 * pll_ratio / 2;
  5656. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5657. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5658. return cdclk; /* 576MHz or 624MHz */
  5659. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5660. return cdclk * 2 / 3; /* 384MHz */
  5661. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5662. return cdclk / 2; /* 288MHz */
  5663. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5664. return cdclk / 4; /* 144MHz */
  5665. }
  5666. /* error case, do as if DE PLL isn't enabled */
  5667. return 19200;
  5668. }
  5669. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5670. {
  5671. struct drm_i915_private *dev_priv = dev->dev_private;
  5672. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5673. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5674. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5675. return 800000;
  5676. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5677. return 450000;
  5678. else if (freq == LCPLL_CLK_FREQ_450)
  5679. return 450000;
  5680. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5681. return 540000;
  5682. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5683. return 337500;
  5684. else
  5685. return 675000;
  5686. }
  5687. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5688. {
  5689. struct drm_i915_private *dev_priv = dev->dev_private;
  5690. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5691. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5692. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5693. return 800000;
  5694. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5695. return 450000;
  5696. else if (freq == LCPLL_CLK_FREQ_450)
  5697. return 450000;
  5698. else if (IS_HSW_ULT(dev))
  5699. return 337500;
  5700. else
  5701. return 540000;
  5702. }
  5703. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5704. {
  5705. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5706. CCK_DISPLAY_CLOCK_CONTROL);
  5707. }
  5708. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5709. {
  5710. return 450000;
  5711. }
  5712. static int i945_get_display_clock_speed(struct drm_device *dev)
  5713. {
  5714. return 400000;
  5715. }
  5716. static int i915_get_display_clock_speed(struct drm_device *dev)
  5717. {
  5718. return 333333;
  5719. }
  5720. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5721. {
  5722. return 200000;
  5723. }
  5724. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5725. {
  5726. u16 gcfgc = 0;
  5727. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5728. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5729. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5730. return 266667;
  5731. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5732. return 333333;
  5733. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5734. return 444444;
  5735. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5736. return 200000;
  5737. default:
  5738. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5739. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5740. return 133333;
  5741. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5742. return 166667;
  5743. }
  5744. }
  5745. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5746. {
  5747. u16 gcfgc = 0;
  5748. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5749. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5750. return 133333;
  5751. else {
  5752. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5753. case GC_DISPLAY_CLOCK_333_MHZ:
  5754. return 333333;
  5755. default:
  5756. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5757. return 190000;
  5758. }
  5759. }
  5760. }
  5761. static int i865_get_display_clock_speed(struct drm_device *dev)
  5762. {
  5763. return 266667;
  5764. }
  5765. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5766. {
  5767. u16 hpllcc = 0;
  5768. /*
  5769. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5770. * encoding is different :(
  5771. * FIXME is this the right way to detect 852GM/852GMV?
  5772. */
  5773. if (dev->pdev->revision == 0x1)
  5774. return 133333;
  5775. pci_bus_read_config_word(dev->pdev->bus,
  5776. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5777. /* Assume that the hardware is in the high speed state. This
  5778. * should be the default.
  5779. */
  5780. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5781. case GC_CLOCK_133_200:
  5782. case GC_CLOCK_133_200_2:
  5783. case GC_CLOCK_100_200:
  5784. return 200000;
  5785. case GC_CLOCK_166_250:
  5786. return 250000;
  5787. case GC_CLOCK_100_133:
  5788. return 133333;
  5789. case GC_CLOCK_133_266:
  5790. case GC_CLOCK_133_266_2:
  5791. case GC_CLOCK_166_266:
  5792. return 266667;
  5793. }
  5794. /* Shouldn't happen */
  5795. return 0;
  5796. }
  5797. static int i830_get_display_clock_speed(struct drm_device *dev)
  5798. {
  5799. return 133333;
  5800. }
  5801. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5802. {
  5803. struct drm_i915_private *dev_priv = dev->dev_private;
  5804. static const unsigned int blb_vco[8] = {
  5805. [0] = 3200000,
  5806. [1] = 4000000,
  5807. [2] = 5333333,
  5808. [3] = 4800000,
  5809. [4] = 6400000,
  5810. };
  5811. static const unsigned int pnv_vco[8] = {
  5812. [0] = 3200000,
  5813. [1] = 4000000,
  5814. [2] = 5333333,
  5815. [3] = 4800000,
  5816. [4] = 2666667,
  5817. };
  5818. static const unsigned int cl_vco[8] = {
  5819. [0] = 3200000,
  5820. [1] = 4000000,
  5821. [2] = 5333333,
  5822. [3] = 6400000,
  5823. [4] = 3333333,
  5824. [5] = 3566667,
  5825. [6] = 4266667,
  5826. };
  5827. static const unsigned int elk_vco[8] = {
  5828. [0] = 3200000,
  5829. [1] = 4000000,
  5830. [2] = 5333333,
  5831. [3] = 4800000,
  5832. };
  5833. static const unsigned int ctg_vco[8] = {
  5834. [0] = 3200000,
  5835. [1] = 4000000,
  5836. [2] = 5333333,
  5837. [3] = 6400000,
  5838. [4] = 2666667,
  5839. [5] = 4266667,
  5840. };
  5841. const unsigned int *vco_table;
  5842. unsigned int vco;
  5843. uint8_t tmp = 0;
  5844. /* FIXME other chipsets? */
  5845. if (IS_GM45(dev))
  5846. vco_table = ctg_vco;
  5847. else if (IS_G4X(dev))
  5848. vco_table = elk_vco;
  5849. else if (IS_CRESTLINE(dev))
  5850. vco_table = cl_vco;
  5851. else if (IS_PINEVIEW(dev))
  5852. vco_table = pnv_vco;
  5853. else if (IS_G33(dev))
  5854. vco_table = blb_vco;
  5855. else
  5856. return 0;
  5857. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5858. vco = vco_table[tmp & 0x7];
  5859. if (vco == 0)
  5860. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5861. else
  5862. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5863. return vco;
  5864. }
  5865. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5866. {
  5867. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5868. uint16_t tmp = 0;
  5869. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5870. cdclk_sel = (tmp >> 12) & 0x1;
  5871. switch (vco) {
  5872. case 2666667:
  5873. case 4000000:
  5874. case 5333333:
  5875. return cdclk_sel ? 333333 : 222222;
  5876. case 3200000:
  5877. return cdclk_sel ? 320000 : 228571;
  5878. default:
  5879. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5880. return 222222;
  5881. }
  5882. }
  5883. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5884. {
  5885. static const uint8_t div_3200[] = { 16, 10, 8 };
  5886. static const uint8_t div_4000[] = { 20, 12, 10 };
  5887. static const uint8_t div_5333[] = { 24, 16, 14 };
  5888. const uint8_t *div_table;
  5889. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5890. uint16_t tmp = 0;
  5891. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5892. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5893. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5894. goto fail;
  5895. switch (vco) {
  5896. case 3200000:
  5897. div_table = div_3200;
  5898. break;
  5899. case 4000000:
  5900. div_table = div_4000;
  5901. break;
  5902. case 5333333:
  5903. div_table = div_5333;
  5904. break;
  5905. default:
  5906. goto fail;
  5907. }
  5908. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5909. fail:
  5910. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5911. return 200000;
  5912. }
  5913. static int g33_get_display_clock_speed(struct drm_device *dev)
  5914. {
  5915. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5916. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5917. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5918. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5919. const uint8_t *div_table;
  5920. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5921. uint16_t tmp = 0;
  5922. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5923. cdclk_sel = (tmp >> 4) & 0x7;
  5924. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5925. goto fail;
  5926. switch (vco) {
  5927. case 3200000:
  5928. div_table = div_3200;
  5929. break;
  5930. case 4000000:
  5931. div_table = div_4000;
  5932. break;
  5933. case 4800000:
  5934. div_table = div_4800;
  5935. break;
  5936. case 5333333:
  5937. div_table = div_5333;
  5938. break;
  5939. default:
  5940. goto fail;
  5941. }
  5942. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5943. fail:
  5944. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5945. return 190476;
  5946. }
  5947. static void
  5948. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5949. {
  5950. while (*num > DATA_LINK_M_N_MASK ||
  5951. *den > DATA_LINK_M_N_MASK) {
  5952. *num >>= 1;
  5953. *den >>= 1;
  5954. }
  5955. }
  5956. static void compute_m_n(unsigned int m, unsigned int n,
  5957. uint32_t *ret_m, uint32_t *ret_n)
  5958. {
  5959. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5960. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5961. intel_reduce_m_n_ratio(ret_m, ret_n);
  5962. }
  5963. void
  5964. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5965. int pixel_clock, int link_clock,
  5966. struct intel_link_m_n *m_n)
  5967. {
  5968. m_n->tu = 64;
  5969. compute_m_n(bits_per_pixel * pixel_clock,
  5970. link_clock * nlanes * 8,
  5971. &m_n->gmch_m, &m_n->gmch_n);
  5972. compute_m_n(pixel_clock, link_clock,
  5973. &m_n->link_m, &m_n->link_n);
  5974. }
  5975. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5976. {
  5977. if (i915.panel_use_ssc >= 0)
  5978. return i915.panel_use_ssc != 0;
  5979. return dev_priv->vbt.lvds_use_ssc
  5980. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5981. }
  5982. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5983. int num_connectors)
  5984. {
  5985. struct drm_device *dev = crtc_state->base.crtc->dev;
  5986. struct drm_i915_private *dev_priv = dev->dev_private;
  5987. int refclk;
  5988. WARN_ON(!crtc_state->base.state);
  5989. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
  5990. refclk = 100000;
  5991. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5992. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5993. refclk = dev_priv->vbt.lvds_ssc_freq;
  5994. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5995. } else if (!IS_GEN2(dev)) {
  5996. refclk = 96000;
  5997. } else {
  5998. refclk = 48000;
  5999. }
  6000. return refclk;
  6001. }
  6002. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6003. {
  6004. return (1 << dpll->n) << 16 | dpll->m2;
  6005. }
  6006. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6007. {
  6008. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6009. }
  6010. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6011. struct intel_crtc_state *crtc_state,
  6012. intel_clock_t *reduced_clock)
  6013. {
  6014. struct drm_device *dev = crtc->base.dev;
  6015. u32 fp, fp2 = 0;
  6016. if (IS_PINEVIEW(dev)) {
  6017. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6018. if (reduced_clock)
  6019. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6020. } else {
  6021. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6022. if (reduced_clock)
  6023. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6024. }
  6025. crtc_state->dpll_hw_state.fp0 = fp;
  6026. crtc->lowfreq_avail = false;
  6027. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6028. reduced_clock) {
  6029. crtc_state->dpll_hw_state.fp1 = fp2;
  6030. crtc->lowfreq_avail = true;
  6031. } else {
  6032. crtc_state->dpll_hw_state.fp1 = fp;
  6033. }
  6034. }
  6035. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6036. pipe)
  6037. {
  6038. u32 reg_val;
  6039. /*
  6040. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6041. * and set it to a reasonable value instead.
  6042. */
  6043. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6044. reg_val &= 0xffffff00;
  6045. reg_val |= 0x00000030;
  6046. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6047. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6048. reg_val &= 0x8cffffff;
  6049. reg_val = 0x8c000000;
  6050. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6051. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6052. reg_val &= 0xffffff00;
  6053. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6054. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6055. reg_val &= 0x00ffffff;
  6056. reg_val |= 0xb0000000;
  6057. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6058. }
  6059. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6060. struct intel_link_m_n *m_n)
  6061. {
  6062. struct drm_device *dev = crtc->base.dev;
  6063. struct drm_i915_private *dev_priv = dev->dev_private;
  6064. int pipe = crtc->pipe;
  6065. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6066. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6067. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6068. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6069. }
  6070. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6071. struct intel_link_m_n *m_n,
  6072. struct intel_link_m_n *m2_n2)
  6073. {
  6074. struct drm_device *dev = crtc->base.dev;
  6075. struct drm_i915_private *dev_priv = dev->dev_private;
  6076. int pipe = crtc->pipe;
  6077. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6078. if (INTEL_INFO(dev)->gen >= 5) {
  6079. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6080. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6081. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6082. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6083. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6084. * for gen < 8) and if DRRS is supported (to make sure the
  6085. * registers are not unnecessarily accessed).
  6086. */
  6087. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6088. crtc->config->has_drrs) {
  6089. I915_WRITE(PIPE_DATA_M2(transcoder),
  6090. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6091. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6092. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6093. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6094. }
  6095. } else {
  6096. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6097. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6098. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6099. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6100. }
  6101. }
  6102. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6103. {
  6104. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6105. if (m_n == M1_N1) {
  6106. dp_m_n = &crtc->config->dp_m_n;
  6107. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6108. } else if (m_n == M2_N2) {
  6109. /*
  6110. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6111. * needs to be programmed into M1_N1.
  6112. */
  6113. dp_m_n = &crtc->config->dp_m2_n2;
  6114. } else {
  6115. DRM_ERROR("Unsupported divider value\n");
  6116. return;
  6117. }
  6118. if (crtc->config->has_pch_encoder)
  6119. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6120. else
  6121. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6122. }
  6123. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6124. struct intel_crtc_state *pipe_config)
  6125. {
  6126. u32 dpll, dpll_md;
  6127. /*
  6128. * Enable DPIO clock input. We should never disable the reference
  6129. * clock for pipe B, since VGA hotplug / manual detection depends
  6130. * on it.
  6131. */
  6132. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6133. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6134. /* We should never disable this, set it here for state tracking */
  6135. if (crtc->pipe == PIPE_B)
  6136. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6137. dpll |= DPLL_VCO_ENABLE;
  6138. pipe_config->dpll_hw_state.dpll = dpll;
  6139. dpll_md = (pipe_config->pixel_multiplier - 1)
  6140. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6141. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6142. }
  6143. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6144. const struct intel_crtc_state *pipe_config)
  6145. {
  6146. struct drm_device *dev = crtc->base.dev;
  6147. struct drm_i915_private *dev_priv = dev->dev_private;
  6148. int pipe = crtc->pipe;
  6149. u32 mdiv;
  6150. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6151. u32 coreclk, reg_val;
  6152. mutex_lock(&dev_priv->sb_lock);
  6153. bestn = pipe_config->dpll.n;
  6154. bestm1 = pipe_config->dpll.m1;
  6155. bestm2 = pipe_config->dpll.m2;
  6156. bestp1 = pipe_config->dpll.p1;
  6157. bestp2 = pipe_config->dpll.p2;
  6158. /* See eDP HDMI DPIO driver vbios notes doc */
  6159. /* PLL B needs special handling */
  6160. if (pipe == PIPE_B)
  6161. vlv_pllb_recal_opamp(dev_priv, pipe);
  6162. /* Set up Tx target for periodic Rcomp update */
  6163. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6164. /* Disable target IRef on PLL */
  6165. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6166. reg_val &= 0x00ffffff;
  6167. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6168. /* Disable fast lock */
  6169. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6170. /* Set idtafcrecal before PLL is enabled */
  6171. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6172. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6173. mdiv |= ((bestn << DPIO_N_SHIFT));
  6174. mdiv |= (1 << DPIO_K_SHIFT);
  6175. /*
  6176. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6177. * but we don't support that).
  6178. * Note: don't use the DAC post divider as it seems unstable.
  6179. */
  6180. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6181. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6182. mdiv |= DPIO_ENABLE_CALIBRATION;
  6183. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6184. /* Set HBR and RBR LPF coefficients */
  6185. if (pipe_config->port_clock == 162000 ||
  6186. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6187. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6188. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6189. 0x009f0003);
  6190. else
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6192. 0x00d0000f);
  6193. if (pipe_config->has_dp_encoder) {
  6194. /* Use SSC source */
  6195. if (pipe == PIPE_A)
  6196. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6197. 0x0df40000);
  6198. else
  6199. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6200. 0x0df70000);
  6201. } else { /* HDMI or VGA */
  6202. /* Use bend source */
  6203. if (pipe == PIPE_A)
  6204. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6205. 0x0df70000);
  6206. else
  6207. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6208. 0x0df40000);
  6209. }
  6210. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6211. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6212. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6213. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6214. coreclk |= 0x01000000;
  6215. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6216. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6217. mutex_unlock(&dev_priv->sb_lock);
  6218. }
  6219. static void chv_compute_dpll(struct intel_crtc *crtc,
  6220. struct intel_crtc_state *pipe_config)
  6221. {
  6222. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6223. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6224. DPLL_VCO_ENABLE;
  6225. if (crtc->pipe != PIPE_A)
  6226. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6227. pipe_config->dpll_hw_state.dpll_md =
  6228. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6229. }
  6230. static void chv_prepare_pll(struct intel_crtc *crtc,
  6231. const struct intel_crtc_state *pipe_config)
  6232. {
  6233. struct drm_device *dev = crtc->base.dev;
  6234. struct drm_i915_private *dev_priv = dev->dev_private;
  6235. int pipe = crtc->pipe;
  6236. i915_reg_t dpll_reg = DPLL(crtc->pipe);
  6237. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6238. u32 loopfilter, tribuf_calcntr;
  6239. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6240. u32 dpio_val;
  6241. int vco;
  6242. bestn = pipe_config->dpll.n;
  6243. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6244. bestm1 = pipe_config->dpll.m1;
  6245. bestm2 = pipe_config->dpll.m2 >> 22;
  6246. bestp1 = pipe_config->dpll.p1;
  6247. bestp2 = pipe_config->dpll.p2;
  6248. vco = pipe_config->dpll.vco;
  6249. dpio_val = 0;
  6250. loopfilter = 0;
  6251. /*
  6252. * Enable Refclk and SSC
  6253. */
  6254. I915_WRITE(dpll_reg,
  6255. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6256. mutex_lock(&dev_priv->sb_lock);
  6257. /* p1 and p2 divider */
  6258. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6259. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6260. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6261. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6262. 1 << DPIO_CHV_K_DIV_SHIFT);
  6263. /* Feedback post-divider - m2 */
  6264. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6265. /* Feedback refclk divider - n and m1 */
  6266. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6267. DPIO_CHV_M1_DIV_BY_2 |
  6268. 1 << DPIO_CHV_N_DIV_SHIFT);
  6269. /* M2 fraction division */
  6270. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6271. /* M2 fraction division enable */
  6272. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6273. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6274. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6275. if (bestm2_frac)
  6276. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6277. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6278. /* Program digital lock detect threshold */
  6279. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6280. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6281. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6282. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6283. if (!bestm2_frac)
  6284. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6285. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6286. /* Loop filter */
  6287. if (vco == 5400000) {
  6288. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6289. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6290. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6291. tribuf_calcntr = 0x9;
  6292. } else if (vco <= 6200000) {
  6293. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6294. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6295. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6296. tribuf_calcntr = 0x9;
  6297. } else if (vco <= 6480000) {
  6298. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6299. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6300. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6301. tribuf_calcntr = 0x8;
  6302. } else {
  6303. /* Not supported. Apply the same limits as in the max case */
  6304. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6305. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6306. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6307. tribuf_calcntr = 0;
  6308. }
  6309. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6310. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6311. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6312. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6313. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6314. /* AFC Recal */
  6315. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6316. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6317. DPIO_AFC_RECAL);
  6318. mutex_unlock(&dev_priv->sb_lock);
  6319. }
  6320. /**
  6321. * vlv_force_pll_on - forcibly enable just the PLL
  6322. * @dev_priv: i915 private structure
  6323. * @pipe: pipe PLL to enable
  6324. * @dpll: PLL configuration
  6325. *
  6326. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6327. * in cases where we need the PLL enabled even when @pipe is not going to
  6328. * be enabled.
  6329. */
  6330. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6331. const struct dpll *dpll)
  6332. {
  6333. struct intel_crtc *crtc =
  6334. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6335. struct intel_crtc_state *pipe_config;
  6336. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6337. if (!pipe_config)
  6338. return -ENOMEM;
  6339. pipe_config->base.crtc = &crtc->base;
  6340. pipe_config->pixel_multiplier = 1;
  6341. pipe_config->dpll = *dpll;
  6342. if (IS_CHERRYVIEW(dev)) {
  6343. chv_compute_dpll(crtc, pipe_config);
  6344. chv_prepare_pll(crtc, pipe_config);
  6345. chv_enable_pll(crtc, pipe_config);
  6346. } else {
  6347. vlv_compute_dpll(crtc, pipe_config);
  6348. vlv_prepare_pll(crtc, pipe_config);
  6349. vlv_enable_pll(crtc, pipe_config);
  6350. }
  6351. kfree(pipe_config);
  6352. return 0;
  6353. }
  6354. /**
  6355. * vlv_force_pll_off - forcibly disable just the PLL
  6356. * @dev_priv: i915 private structure
  6357. * @pipe: pipe PLL to disable
  6358. *
  6359. * Disable the PLL for @pipe. To be used in cases where we need
  6360. * the PLL enabled even when @pipe is not going to be enabled.
  6361. */
  6362. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6363. {
  6364. if (IS_CHERRYVIEW(dev))
  6365. chv_disable_pll(to_i915(dev), pipe);
  6366. else
  6367. vlv_disable_pll(to_i915(dev), pipe);
  6368. }
  6369. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6370. struct intel_crtc_state *crtc_state,
  6371. intel_clock_t *reduced_clock,
  6372. int num_connectors)
  6373. {
  6374. struct drm_device *dev = crtc->base.dev;
  6375. struct drm_i915_private *dev_priv = dev->dev_private;
  6376. u32 dpll;
  6377. bool is_sdvo;
  6378. struct dpll *clock = &crtc_state->dpll;
  6379. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6380. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6381. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6382. dpll = DPLL_VGA_MODE_DIS;
  6383. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6384. dpll |= DPLLB_MODE_LVDS;
  6385. else
  6386. dpll |= DPLLB_MODE_DAC_SERIAL;
  6387. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6388. dpll |= (crtc_state->pixel_multiplier - 1)
  6389. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6390. }
  6391. if (is_sdvo)
  6392. dpll |= DPLL_SDVO_HIGH_SPEED;
  6393. if (crtc_state->has_dp_encoder)
  6394. dpll |= DPLL_SDVO_HIGH_SPEED;
  6395. /* compute bitmask from p1 value */
  6396. if (IS_PINEVIEW(dev))
  6397. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6398. else {
  6399. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6400. if (IS_G4X(dev) && reduced_clock)
  6401. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6402. }
  6403. switch (clock->p2) {
  6404. case 5:
  6405. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6406. break;
  6407. case 7:
  6408. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6409. break;
  6410. case 10:
  6411. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6412. break;
  6413. case 14:
  6414. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6415. break;
  6416. }
  6417. if (INTEL_INFO(dev)->gen >= 4)
  6418. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6419. if (crtc_state->sdvo_tv_clock)
  6420. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6421. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6422. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6423. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6424. else
  6425. dpll |= PLL_REF_INPUT_DREFCLK;
  6426. dpll |= DPLL_VCO_ENABLE;
  6427. crtc_state->dpll_hw_state.dpll = dpll;
  6428. if (INTEL_INFO(dev)->gen >= 4) {
  6429. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6430. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6431. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6432. }
  6433. }
  6434. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6435. struct intel_crtc_state *crtc_state,
  6436. intel_clock_t *reduced_clock,
  6437. int num_connectors)
  6438. {
  6439. struct drm_device *dev = crtc->base.dev;
  6440. struct drm_i915_private *dev_priv = dev->dev_private;
  6441. u32 dpll;
  6442. struct dpll *clock = &crtc_state->dpll;
  6443. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6444. dpll = DPLL_VGA_MODE_DIS;
  6445. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6446. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6447. } else {
  6448. if (clock->p1 == 2)
  6449. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6450. else
  6451. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6452. if (clock->p2 == 4)
  6453. dpll |= PLL_P2_DIVIDE_BY_4;
  6454. }
  6455. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6456. dpll |= DPLL_DVO_2X_MODE;
  6457. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6458. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6459. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6460. else
  6461. dpll |= PLL_REF_INPUT_DREFCLK;
  6462. dpll |= DPLL_VCO_ENABLE;
  6463. crtc_state->dpll_hw_state.dpll = dpll;
  6464. }
  6465. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6466. {
  6467. struct drm_device *dev = intel_crtc->base.dev;
  6468. struct drm_i915_private *dev_priv = dev->dev_private;
  6469. enum pipe pipe = intel_crtc->pipe;
  6470. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6471. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6472. uint32_t crtc_vtotal, crtc_vblank_end;
  6473. int vsyncshift = 0;
  6474. /* We need to be careful not to changed the adjusted mode, for otherwise
  6475. * the hw state checker will get angry at the mismatch. */
  6476. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6477. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6478. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6479. /* the chip adds 2 halflines automatically */
  6480. crtc_vtotal -= 1;
  6481. crtc_vblank_end -= 1;
  6482. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6483. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6484. else
  6485. vsyncshift = adjusted_mode->crtc_hsync_start -
  6486. adjusted_mode->crtc_htotal / 2;
  6487. if (vsyncshift < 0)
  6488. vsyncshift += adjusted_mode->crtc_htotal;
  6489. }
  6490. if (INTEL_INFO(dev)->gen > 3)
  6491. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6492. I915_WRITE(HTOTAL(cpu_transcoder),
  6493. (adjusted_mode->crtc_hdisplay - 1) |
  6494. ((adjusted_mode->crtc_htotal - 1) << 16));
  6495. I915_WRITE(HBLANK(cpu_transcoder),
  6496. (adjusted_mode->crtc_hblank_start - 1) |
  6497. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6498. I915_WRITE(HSYNC(cpu_transcoder),
  6499. (adjusted_mode->crtc_hsync_start - 1) |
  6500. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6501. I915_WRITE(VTOTAL(cpu_transcoder),
  6502. (adjusted_mode->crtc_vdisplay - 1) |
  6503. ((crtc_vtotal - 1) << 16));
  6504. I915_WRITE(VBLANK(cpu_transcoder),
  6505. (adjusted_mode->crtc_vblank_start - 1) |
  6506. ((crtc_vblank_end - 1) << 16));
  6507. I915_WRITE(VSYNC(cpu_transcoder),
  6508. (adjusted_mode->crtc_vsync_start - 1) |
  6509. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6510. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6511. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6512. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6513. * bits. */
  6514. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6515. (pipe == PIPE_B || pipe == PIPE_C))
  6516. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6517. /* pipesrc controls the size that is scaled from, which should
  6518. * always be the user's requested size.
  6519. */
  6520. I915_WRITE(PIPESRC(pipe),
  6521. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6522. (intel_crtc->config->pipe_src_h - 1));
  6523. }
  6524. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6525. struct intel_crtc_state *pipe_config)
  6526. {
  6527. struct drm_device *dev = crtc->base.dev;
  6528. struct drm_i915_private *dev_priv = dev->dev_private;
  6529. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6530. uint32_t tmp;
  6531. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6532. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6533. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6534. tmp = I915_READ(HBLANK(cpu_transcoder));
  6535. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6536. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6537. tmp = I915_READ(HSYNC(cpu_transcoder));
  6538. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6539. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6540. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6541. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6542. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6543. tmp = I915_READ(VBLANK(cpu_transcoder));
  6544. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6545. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6546. tmp = I915_READ(VSYNC(cpu_transcoder));
  6547. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6548. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6549. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6550. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6551. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6552. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6553. }
  6554. tmp = I915_READ(PIPESRC(crtc->pipe));
  6555. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6556. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6557. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6558. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6559. }
  6560. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6561. struct intel_crtc_state *pipe_config)
  6562. {
  6563. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6564. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6565. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6566. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6567. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6568. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6569. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6570. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6571. mode->flags = pipe_config->base.adjusted_mode.flags;
  6572. mode->type = DRM_MODE_TYPE_DRIVER;
  6573. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6574. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6575. mode->hsync = drm_mode_hsync(mode);
  6576. mode->vrefresh = drm_mode_vrefresh(mode);
  6577. drm_mode_set_name(mode);
  6578. }
  6579. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6580. {
  6581. struct drm_device *dev = intel_crtc->base.dev;
  6582. struct drm_i915_private *dev_priv = dev->dev_private;
  6583. uint32_t pipeconf;
  6584. pipeconf = 0;
  6585. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6586. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6587. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6588. if (intel_crtc->config->double_wide)
  6589. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6590. /* only g4x and later have fancy bpc/dither controls */
  6591. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6592. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6593. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6594. pipeconf |= PIPECONF_DITHER_EN |
  6595. PIPECONF_DITHER_TYPE_SP;
  6596. switch (intel_crtc->config->pipe_bpp) {
  6597. case 18:
  6598. pipeconf |= PIPECONF_6BPC;
  6599. break;
  6600. case 24:
  6601. pipeconf |= PIPECONF_8BPC;
  6602. break;
  6603. case 30:
  6604. pipeconf |= PIPECONF_10BPC;
  6605. break;
  6606. default:
  6607. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6608. BUG();
  6609. }
  6610. }
  6611. if (HAS_PIPE_CXSR(dev)) {
  6612. if (intel_crtc->lowfreq_avail) {
  6613. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6614. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6615. } else {
  6616. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6617. }
  6618. }
  6619. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6620. if (INTEL_INFO(dev)->gen < 4 ||
  6621. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6622. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6623. else
  6624. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6625. } else
  6626. pipeconf |= PIPECONF_PROGRESSIVE;
  6627. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6628. intel_crtc->config->limited_color_range)
  6629. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6630. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6631. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6632. }
  6633. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6634. struct intel_crtc_state *crtc_state)
  6635. {
  6636. struct drm_device *dev = crtc->base.dev;
  6637. struct drm_i915_private *dev_priv = dev->dev_private;
  6638. int refclk, num_connectors = 0;
  6639. intel_clock_t clock;
  6640. bool ok;
  6641. const intel_limit_t *limit;
  6642. struct drm_atomic_state *state = crtc_state->base.state;
  6643. struct drm_connector *connector;
  6644. struct drm_connector_state *connector_state;
  6645. int i;
  6646. memset(&crtc_state->dpll_hw_state, 0,
  6647. sizeof(crtc_state->dpll_hw_state));
  6648. if (crtc_state->has_dsi_encoder)
  6649. return 0;
  6650. for_each_connector_in_state(state, connector, connector_state, i) {
  6651. if (connector_state->crtc == &crtc->base)
  6652. num_connectors++;
  6653. }
  6654. if (!crtc_state->clock_set) {
  6655. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6656. /*
  6657. * Returns a set of divisors for the desired target clock with
  6658. * the given refclk, or FALSE. The returned values represent
  6659. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6660. * 2) / p1 / p2.
  6661. */
  6662. limit = intel_limit(crtc_state, refclk);
  6663. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6664. crtc_state->port_clock,
  6665. refclk, NULL, &clock);
  6666. if (!ok) {
  6667. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6668. return -EINVAL;
  6669. }
  6670. /* Compat-code for transition, will disappear. */
  6671. crtc_state->dpll.n = clock.n;
  6672. crtc_state->dpll.m1 = clock.m1;
  6673. crtc_state->dpll.m2 = clock.m2;
  6674. crtc_state->dpll.p1 = clock.p1;
  6675. crtc_state->dpll.p2 = clock.p2;
  6676. }
  6677. if (IS_GEN2(dev)) {
  6678. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6679. num_connectors);
  6680. } else if (IS_CHERRYVIEW(dev)) {
  6681. chv_compute_dpll(crtc, crtc_state);
  6682. } else if (IS_VALLEYVIEW(dev)) {
  6683. vlv_compute_dpll(crtc, crtc_state);
  6684. } else {
  6685. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6686. num_connectors);
  6687. }
  6688. return 0;
  6689. }
  6690. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6691. struct intel_crtc_state *pipe_config)
  6692. {
  6693. struct drm_device *dev = crtc->base.dev;
  6694. struct drm_i915_private *dev_priv = dev->dev_private;
  6695. uint32_t tmp;
  6696. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6697. return;
  6698. tmp = I915_READ(PFIT_CONTROL);
  6699. if (!(tmp & PFIT_ENABLE))
  6700. return;
  6701. /* Check whether the pfit is attached to our pipe. */
  6702. if (INTEL_INFO(dev)->gen < 4) {
  6703. if (crtc->pipe != PIPE_B)
  6704. return;
  6705. } else {
  6706. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6707. return;
  6708. }
  6709. pipe_config->gmch_pfit.control = tmp;
  6710. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6711. if (INTEL_INFO(dev)->gen < 5)
  6712. pipe_config->gmch_pfit.lvds_border_bits =
  6713. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6714. }
  6715. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6716. struct intel_crtc_state *pipe_config)
  6717. {
  6718. struct drm_device *dev = crtc->base.dev;
  6719. struct drm_i915_private *dev_priv = dev->dev_private;
  6720. int pipe = pipe_config->cpu_transcoder;
  6721. intel_clock_t clock;
  6722. u32 mdiv;
  6723. int refclk = 100000;
  6724. /* In case of MIPI DPLL will not even be used */
  6725. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6726. return;
  6727. mutex_lock(&dev_priv->sb_lock);
  6728. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6729. mutex_unlock(&dev_priv->sb_lock);
  6730. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6731. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6732. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6733. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6734. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6735. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6736. }
  6737. static void
  6738. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6739. struct intel_initial_plane_config *plane_config)
  6740. {
  6741. struct drm_device *dev = crtc->base.dev;
  6742. struct drm_i915_private *dev_priv = dev->dev_private;
  6743. u32 val, base, offset;
  6744. int pipe = crtc->pipe, plane = crtc->plane;
  6745. int fourcc, pixel_format;
  6746. unsigned int aligned_height;
  6747. struct drm_framebuffer *fb;
  6748. struct intel_framebuffer *intel_fb;
  6749. val = I915_READ(DSPCNTR(plane));
  6750. if (!(val & DISPLAY_PLANE_ENABLE))
  6751. return;
  6752. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6753. if (!intel_fb) {
  6754. DRM_DEBUG_KMS("failed to alloc fb\n");
  6755. return;
  6756. }
  6757. fb = &intel_fb->base;
  6758. if (INTEL_INFO(dev)->gen >= 4) {
  6759. if (val & DISPPLANE_TILED) {
  6760. plane_config->tiling = I915_TILING_X;
  6761. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6762. }
  6763. }
  6764. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6765. fourcc = i9xx_format_to_fourcc(pixel_format);
  6766. fb->pixel_format = fourcc;
  6767. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6768. if (INTEL_INFO(dev)->gen >= 4) {
  6769. if (plane_config->tiling)
  6770. offset = I915_READ(DSPTILEOFF(plane));
  6771. else
  6772. offset = I915_READ(DSPLINOFF(plane));
  6773. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6774. } else {
  6775. base = I915_READ(DSPADDR(plane));
  6776. }
  6777. plane_config->base = base;
  6778. val = I915_READ(PIPESRC(pipe));
  6779. fb->width = ((val >> 16) & 0xfff) + 1;
  6780. fb->height = ((val >> 0) & 0xfff) + 1;
  6781. val = I915_READ(DSPSTRIDE(pipe));
  6782. fb->pitches[0] = val & 0xffffffc0;
  6783. aligned_height = intel_fb_align_height(dev, fb->height,
  6784. fb->pixel_format,
  6785. fb->modifier[0]);
  6786. plane_config->size = fb->pitches[0] * aligned_height;
  6787. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6788. pipe_name(pipe), plane, fb->width, fb->height,
  6789. fb->bits_per_pixel, base, fb->pitches[0],
  6790. plane_config->size);
  6791. plane_config->fb = intel_fb;
  6792. }
  6793. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6794. struct intel_crtc_state *pipe_config)
  6795. {
  6796. struct drm_device *dev = crtc->base.dev;
  6797. struct drm_i915_private *dev_priv = dev->dev_private;
  6798. int pipe = pipe_config->cpu_transcoder;
  6799. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6800. intel_clock_t clock;
  6801. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6802. int refclk = 100000;
  6803. mutex_lock(&dev_priv->sb_lock);
  6804. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6805. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6806. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6807. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6808. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6809. mutex_unlock(&dev_priv->sb_lock);
  6810. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6811. clock.m2 = (pll_dw0 & 0xff) << 22;
  6812. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6813. clock.m2 |= pll_dw2 & 0x3fffff;
  6814. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6815. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6816. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6817. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6818. }
  6819. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6820. struct intel_crtc_state *pipe_config)
  6821. {
  6822. struct drm_device *dev = crtc->base.dev;
  6823. struct drm_i915_private *dev_priv = dev->dev_private;
  6824. enum intel_display_power_domain power_domain;
  6825. uint32_t tmp;
  6826. bool ret;
  6827. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6828. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6829. return false;
  6830. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6831. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6832. ret = false;
  6833. tmp = I915_READ(PIPECONF(crtc->pipe));
  6834. if (!(tmp & PIPECONF_ENABLE))
  6835. goto out;
  6836. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6837. switch (tmp & PIPECONF_BPC_MASK) {
  6838. case PIPECONF_6BPC:
  6839. pipe_config->pipe_bpp = 18;
  6840. break;
  6841. case PIPECONF_8BPC:
  6842. pipe_config->pipe_bpp = 24;
  6843. break;
  6844. case PIPECONF_10BPC:
  6845. pipe_config->pipe_bpp = 30;
  6846. break;
  6847. default:
  6848. break;
  6849. }
  6850. }
  6851. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6852. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6853. pipe_config->limited_color_range = true;
  6854. if (INTEL_INFO(dev)->gen < 4)
  6855. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6856. intel_get_pipe_timings(crtc, pipe_config);
  6857. i9xx_get_pfit_config(crtc, pipe_config);
  6858. if (INTEL_INFO(dev)->gen >= 4) {
  6859. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6860. pipe_config->pixel_multiplier =
  6861. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6862. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6863. pipe_config->dpll_hw_state.dpll_md = tmp;
  6864. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6865. tmp = I915_READ(DPLL(crtc->pipe));
  6866. pipe_config->pixel_multiplier =
  6867. ((tmp & SDVO_MULTIPLIER_MASK)
  6868. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6869. } else {
  6870. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6871. * port and will be fixed up in the encoder->get_config
  6872. * function. */
  6873. pipe_config->pixel_multiplier = 1;
  6874. }
  6875. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6876. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6877. /*
  6878. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6879. * on 830. Filter it out here so that we don't
  6880. * report errors due to that.
  6881. */
  6882. if (IS_I830(dev))
  6883. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6884. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6885. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6886. } else {
  6887. /* Mask out read-only status bits. */
  6888. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6889. DPLL_PORTC_READY_MASK |
  6890. DPLL_PORTB_READY_MASK);
  6891. }
  6892. if (IS_CHERRYVIEW(dev))
  6893. chv_crtc_clock_get(crtc, pipe_config);
  6894. else if (IS_VALLEYVIEW(dev))
  6895. vlv_crtc_clock_get(crtc, pipe_config);
  6896. else
  6897. i9xx_crtc_clock_get(crtc, pipe_config);
  6898. /*
  6899. * Normally the dotclock is filled in by the encoder .get_config()
  6900. * but in case the pipe is enabled w/o any ports we need a sane
  6901. * default.
  6902. */
  6903. pipe_config->base.adjusted_mode.crtc_clock =
  6904. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6905. ret = true;
  6906. out:
  6907. intel_display_power_put(dev_priv, power_domain);
  6908. return ret;
  6909. }
  6910. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6911. {
  6912. struct drm_i915_private *dev_priv = dev->dev_private;
  6913. struct intel_encoder *encoder;
  6914. u32 val, final;
  6915. bool has_lvds = false;
  6916. bool has_cpu_edp = false;
  6917. bool has_panel = false;
  6918. bool has_ck505 = false;
  6919. bool can_ssc = false;
  6920. /* We need to take the global config into account */
  6921. for_each_intel_encoder(dev, encoder) {
  6922. switch (encoder->type) {
  6923. case INTEL_OUTPUT_LVDS:
  6924. has_panel = true;
  6925. has_lvds = true;
  6926. break;
  6927. case INTEL_OUTPUT_EDP:
  6928. has_panel = true;
  6929. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6930. has_cpu_edp = true;
  6931. break;
  6932. default:
  6933. break;
  6934. }
  6935. }
  6936. if (HAS_PCH_IBX(dev)) {
  6937. has_ck505 = dev_priv->vbt.display_clock_mode;
  6938. can_ssc = has_ck505;
  6939. } else {
  6940. has_ck505 = false;
  6941. can_ssc = true;
  6942. }
  6943. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6944. has_panel, has_lvds, has_ck505);
  6945. /* Ironlake: try to setup display ref clock before DPLL
  6946. * enabling. This is only under driver's control after
  6947. * PCH B stepping, previous chipset stepping should be
  6948. * ignoring this setting.
  6949. */
  6950. val = I915_READ(PCH_DREF_CONTROL);
  6951. /* As we must carefully and slowly disable/enable each source in turn,
  6952. * compute the final state we want first and check if we need to
  6953. * make any changes at all.
  6954. */
  6955. final = val;
  6956. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6957. if (has_ck505)
  6958. final |= DREF_NONSPREAD_CK505_ENABLE;
  6959. else
  6960. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6961. final &= ~DREF_SSC_SOURCE_MASK;
  6962. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6963. final &= ~DREF_SSC1_ENABLE;
  6964. if (has_panel) {
  6965. final |= DREF_SSC_SOURCE_ENABLE;
  6966. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6967. final |= DREF_SSC1_ENABLE;
  6968. if (has_cpu_edp) {
  6969. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6970. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6971. else
  6972. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6973. } else
  6974. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6975. } else {
  6976. final |= DREF_SSC_SOURCE_DISABLE;
  6977. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6978. }
  6979. if (final == val)
  6980. return;
  6981. /* Always enable nonspread source */
  6982. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6983. if (has_ck505)
  6984. val |= DREF_NONSPREAD_CK505_ENABLE;
  6985. else
  6986. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6987. if (has_panel) {
  6988. val &= ~DREF_SSC_SOURCE_MASK;
  6989. val |= DREF_SSC_SOURCE_ENABLE;
  6990. /* SSC must be turned on before enabling the CPU output */
  6991. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6992. DRM_DEBUG_KMS("Using SSC on panel\n");
  6993. val |= DREF_SSC1_ENABLE;
  6994. } else
  6995. val &= ~DREF_SSC1_ENABLE;
  6996. /* Get SSC going before enabling the outputs */
  6997. I915_WRITE(PCH_DREF_CONTROL, val);
  6998. POSTING_READ(PCH_DREF_CONTROL);
  6999. udelay(200);
  7000. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7001. /* Enable CPU source on CPU attached eDP */
  7002. if (has_cpu_edp) {
  7003. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7004. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7005. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7006. } else
  7007. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7008. } else
  7009. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7010. I915_WRITE(PCH_DREF_CONTROL, val);
  7011. POSTING_READ(PCH_DREF_CONTROL);
  7012. udelay(200);
  7013. } else {
  7014. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7015. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7016. /* Turn off CPU output */
  7017. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7018. I915_WRITE(PCH_DREF_CONTROL, val);
  7019. POSTING_READ(PCH_DREF_CONTROL);
  7020. udelay(200);
  7021. /* Turn off the SSC source */
  7022. val &= ~DREF_SSC_SOURCE_MASK;
  7023. val |= DREF_SSC_SOURCE_DISABLE;
  7024. /* Turn off SSC1 */
  7025. val &= ~DREF_SSC1_ENABLE;
  7026. I915_WRITE(PCH_DREF_CONTROL, val);
  7027. POSTING_READ(PCH_DREF_CONTROL);
  7028. udelay(200);
  7029. }
  7030. BUG_ON(val != final);
  7031. }
  7032. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7033. {
  7034. uint32_t tmp;
  7035. tmp = I915_READ(SOUTH_CHICKEN2);
  7036. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7037. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7038. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7039. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7040. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7041. tmp = I915_READ(SOUTH_CHICKEN2);
  7042. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7043. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7044. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7045. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7046. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7047. }
  7048. /* WaMPhyProgramming:hsw */
  7049. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7050. {
  7051. uint32_t tmp;
  7052. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7053. tmp &= ~(0xFF << 24);
  7054. tmp |= (0x12 << 24);
  7055. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7056. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7057. tmp |= (1 << 11);
  7058. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7059. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7060. tmp |= (1 << 11);
  7061. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7062. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7063. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7064. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7065. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7066. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7067. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7068. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7069. tmp &= ~(7 << 13);
  7070. tmp |= (5 << 13);
  7071. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7072. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7073. tmp &= ~(7 << 13);
  7074. tmp |= (5 << 13);
  7075. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7076. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7077. tmp &= ~0xFF;
  7078. tmp |= 0x1C;
  7079. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7080. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7081. tmp &= ~0xFF;
  7082. tmp |= 0x1C;
  7083. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7084. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7085. tmp &= ~(0xFF << 16);
  7086. tmp |= (0x1C << 16);
  7087. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7088. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7089. tmp &= ~(0xFF << 16);
  7090. tmp |= (0x1C << 16);
  7091. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7092. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7093. tmp |= (1 << 27);
  7094. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7095. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7096. tmp |= (1 << 27);
  7097. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7098. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7099. tmp &= ~(0xF << 28);
  7100. tmp |= (4 << 28);
  7101. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7102. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7103. tmp &= ~(0xF << 28);
  7104. tmp |= (4 << 28);
  7105. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7106. }
  7107. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7108. * Programming" based on the parameters passed:
  7109. * - Sequence to enable CLKOUT_DP
  7110. * - Sequence to enable CLKOUT_DP without spread
  7111. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7112. */
  7113. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7114. bool with_fdi)
  7115. {
  7116. struct drm_i915_private *dev_priv = dev->dev_private;
  7117. uint32_t reg, tmp;
  7118. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7119. with_spread = true;
  7120. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7121. with_fdi = false;
  7122. mutex_lock(&dev_priv->sb_lock);
  7123. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7124. tmp &= ~SBI_SSCCTL_DISABLE;
  7125. tmp |= SBI_SSCCTL_PATHALT;
  7126. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7127. udelay(24);
  7128. if (with_spread) {
  7129. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7130. tmp &= ~SBI_SSCCTL_PATHALT;
  7131. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7132. if (with_fdi) {
  7133. lpt_reset_fdi_mphy(dev_priv);
  7134. lpt_program_fdi_mphy(dev_priv);
  7135. }
  7136. }
  7137. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7138. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7139. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7140. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7141. mutex_unlock(&dev_priv->sb_lock);
  7142. }
  7143. /* Sequence to disable CLKOUT_DP */
  7144. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7145. {
  7146. struct drm_i915_private *dev_priv = dev->dev_private;
  7147. uint32_t reg, tmp;
  7148. mutex_lock(&dev_priv->sb_lock);
  7149. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7150. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7151. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7152. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7153. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7154. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7155. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7156. tmp |= SBI_SSCCTL_PATHALT;
  7157. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7158. udelay(32);
  7159. }
  7160. tmp |= SBI_SSCCTL_DISABLE;
  7161. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7162. }
  7163. mutex_unlock(&dev_priv->sb_lock);
  7164. }
  7165. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7166. static const uint16_t sscdivintphase[] = {
  7167. [BEND_IDX( 50)] = 0x3B23,
  7168. [BEND_IDX( 45)] = 0x3B23,
  7169. [BEND_IDX( 40)] = 0x3C23,
  7170. [BEND_IDX( 35)] = 0x3C23,
  7171. [BEND_IDX( 30)] = 0x3D23,
  7172. [BEND_IDX( 25)] = 0x3D23,
  7173. [BEND_IDX( 20)] = 0x3E23,
  7174. [BEND_IDX( 15)] = 0x3E23,
  7175. [BEND_IDX( 10)] = 0x3F23,
  7176. [BEND_IDX( 5)] = 0x3F23,
  7177. [BEND_IDX( 0)] = 0x0025,
  7178. [BEND_IDX( -5)] = 0x0025,
  7179. [BEND_IDX(-10)] = 0x0125,
  7180. [BEND_IDX(-15)] = 0x0125,
  7181. [BEND_IDX(-20)] = 0x0225,
  7182. [BEND_IDX(-25)] = 0x0225,
  7183. [BEND_IDX(-30)] = 0x0325,
  7184. [BEND_IDX(-35)] = 0x0325,
  7185. [BEND_IDX(-40)] = 0x0425,
  7186. [BEND_IDX(-45)] = 0x0425,
  7187. [BEND_IDX(-50)] = 0x0525,
  7188. };
  7189. /*
  7190. * Bend CLKOUT_DP
  7191. * steps -50 to 50 inclusive, in steps of 5
  7192. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7193. * change in clock period = -(steps / 10) * 5.787 ps
  7194. */
  7195. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7196. {
  7197. uint32_t tmp;
  7198. int idx = BEND_IDX(steps);
  7199. if (WARN_ON(steps % 5 != 0))
  7200. return;
  7201. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7202. return;
  7203. mutex_lock(&dev_priv->sb_lock);
  7204. if (steps % 10 != 0)
  7205. tmp = 0xAAAAAAAB;
  7206. else
  7207. tmp = 0x00000000;
  7208. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7209. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7210. tmp &= 0xffff0000;
  7211. tmp |= sscdivintphase[idx];
  7212. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7213. mutex_unlock(&dev_priv->sb_lock);
  7214. }
  7215. #undef BEND_IDX
  7216. static void lpt_init_pch_refclk(struct drm_device *dev)
  7217. {
  7218. struct intel_encoder *encoder;
  7219. bool has_vga = false;
  7220. for_each_intel_encoder(dev, encoder) {
  7221. switch (encoder->type) {
  7222. case INTEL_OUTPUT_ANALOG:
  7223. has_vga = true;
  7224. break;
  7225. default:
  7226. break;
  7227. }
  7228. }
  7229. if (has_vga) {
  7230. lpt_bend_clkout_dp(to_i915(dev), 0);
  7231. lpt_enable_clkout_dp(dev, true, true);
  7232. } else {
  7233. lpt_disable_clkout_dp(dev);
  7234. }
  7235. }
  7236. /*
  7237. * Initialize reference clocks when the driver loads
  7238. */
  7239. void intel_init_pch_refclk(struct drm_device *dev)
  7240. {
  7241. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7242. ironlake_init_pch_refclk(dev);
  7243. else if (HAS_PCH_LPT(dev))
  7244. lpt_init_pch_refclk(dev);
  7245. }
  7246. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7247. {
  7248. struct drm_device *dev = crtc_state->base.crtc->dev;
  7249. struct drm_i915_private *dev_priv = dev->dev_private;
  7250. struct drm_atomic_state *state = crtc_state->base.state;
  7251. struct drm_connector *connector;
  7252. struct drm_connector_state *connector_state;
  7253. struct intel_encoder *encoder;
  7254. int num_connectors = 0, i;
  7255. bool is_lvds = false;
  7256. for_each_connector_in_state(state, connector, connector_state, i) {
  7257. if (connector_state->crtc != crtc_state->base.crtc)
  7258. continue;
  7259. encoder = to_intel_encoder(connector_state->best_encoder);
  7260. switch (encoder->type) {
  7261. case INTEL_OUTPUT_LVDS:
  7262. is_lvds = true;
  7263. break;
  7264. default:
  7265. break;
  7266. }
  7267. num_connectors++;
  7268. }
  7269. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7270. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7271. dev_priv->vbt.lvds_ssc_freq);
  7272. return dev_priv->vbt.lvds_ssc_freq;
  7273. }
  7274. return 120000;
  7275. }
  7276. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7277. {
  7278. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7280. int pipe = intel_crtc->pipe;
  7281. uint32_t val;
  7282. val = 0;
  7283. switch (intel_crtc->config->pipe_bpp) {
  7284. case 18:
  7285. val |= PIPECONF_6BPC;
  7286. break;
  7287. case 24:
  7288. val |= PIPECONF_8BPC;
  7289. break;
  7290. case 30:
  7291. val |= PIPECONF_10BPC;
  7292. break;
  7293. case 36:
  7294. val |= PIPECONF_12BPC;
  7295. break;
  7296. default:
  7297. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7298. BUG();
  7299. }
  7300. if (intel_crtc->config->dither)
  7301. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7302. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7303. val |= PIPECONF_INTERLACED_ILK;
  7304. else
  7305. val |= PIPECONF_PROGRESSIVE;
  7306. if (intel_crtc->config->limited_color_range)
  7307. val |= PIPECONF_COLOR_RANGE_SELECT;
  7308. I915_WRITE(PIPECONF(pipe), val);
  7309. POSTING_READ(PIPECONF(pipe));
  7310. }
  7311. /*
  7312. * Set up the pipe CSC unit.
  7313. *
  7314. * Currently only full range RGB to limited range RGB conversion
  7315. * is supported, but eventually this should handle various
  7316. * RGB<->YCbCr scenarios as well.
  7317. */
  7318. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7319. {
  7320. struct drm_device *dev = crtc->dev;
  7321. struct drm_i915_private *dev_priv = dev->dev_private;
  7322. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7323. int pipe = intel_crtc->pipe;
  7324. uint16_t coeff = 0x7800; /* 1.0 */
  7325. /*
  7326. * TODO: Check what kind of values actually come out of the pipe
  7327. * with these coeff/postoff values and adjust to get the best
  7328. * accuracy. Perhaps we even need to take the bpc value into
  7329. * consideration.
  7330. */
  7331. if (intel_crtc->config->limited_color_range)
  7332. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7333. /*
  7334. * GY/GU and RY/RU should be the other way around according
  7335. * to BSpec, but reality doesn't agree. Just set them up in
  7336. * a way that results in the correct picture.
  7337. */
  7338. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7339. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7340. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7341. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7342. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7343. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7344. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7345. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7346. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7347. if (INTEL_INFO(dev)->gen > 6) {
  7348. uint16_t postoff = 0;
  7349. if (intel_crtc->config->limited_color_range)
  7350. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7351. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7352. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7353. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7354. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7355. } else {
  7356. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7357. if (intel_crtc->config->limited_color_range)
  7358. mode |= CSC_BLACK_SCREEN_OFFSET;
  7359. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7360. }
  7361. }
  7362. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7363. {
  7364. struct drm_device *dev = crtc->dev;
  7365. struct drm_i915_private *dev_priv = dev->dev_private;
  7366. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7367. enum pipe pipe = intel_crtc->pipe;
  7368. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7369. uint32_t val;
  7370. val = 0;
  7371. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7372. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7373. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7374. val |= PIPECONF_INTERLACED_ILK;
  7375. else
  7376. val |= PIPECONF_PROGRESSIVE;
  7377. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7378. POSTING_READ(PIPECONF(cpu_transcoder));
  7379. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7380. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7381. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7382. val = 0;
  7383. switch (intel_crtc->config->pipe_bpp) {
  7384. case 18:
  7385. val |= PIPEMISC_DITHER_6_BPC;
  7386. break;
  7387. case 24:
  7388. val |= PIPEMISC_DITHER_8_BPC;
  7389. break;
  7390. case 30:
  7391. val |= PIPEMISC_DITHER_10_BPC;
  7392. break;
  7393. case 36:
  7394. val |= PIPEMISC_DITHER_12_BPC;
  7395. break;
  7396. default:
  7397. /* Case prevented by pipe_config_set_bpp. */
  7398. BUG();
  7399. }
  7400. if (intel_crtc->config->dither)
  7401. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7402. I915_WRITE(PIPEMISC(pipe), val);
  7403. }
  7404. }
  7405. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7406. struct intel_crtc_state *crtc_state,
  7407. intel_clock_t *clock,
  7408. bool *has_reduced_clock,
  7409. intel_clock_t *reduced_clock)
  7410. {
  7411. struct drm_device *dev = crtc->dev;
  7412. struct drm_i915_private *dev_priv = dev->dev_private;
  7413. int refclk;
  7414. const intel_limit_t *limit;
  7415. bool ret;
  7416. refclk = ironlake_get_refclk(crtc_state);
  7417. /*
  7418. * Returns a set of divisors for the desired target clock with the given
  7419. * refclk, or FALSE. The returned values represent the clock equation:
  7420. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7421. */
  7422. limit = intel_limit(crtc_state, refclk);
  7423. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7424. crtc_state->port_clock,
  7425. refclk, NULL, clock);
  7426. if (!ret)
  7427. return false;
  7428. return true;
  7429. }
  7430. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7431. {
  7432. /*
  7433. * Account for spread spectrum to avoid
  7434. * oversubscribing the link. Max center spread
  7435. * is 2.5%; use 5% for safety's sake.
  7436. */
  7437. u32 bps = target_clock * bpp * 21 / 20;
  7438. return DIV_ROUND_UP(bps, link_bw * 8);
  7439. }
  7440. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7441. {
  7442. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7443. }
  7444. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7445. struct intel_crtc_state *crtc_state,
  7446. u32 *fp,
  7447. intel_clock_t *reduced_clock, u32 *fp2)
  7448. {
  7449. struct drm_crtc *crtc = &intel_crtc->base;
  7450. struct drm_device *dev = crtc->dev;
  7451. struct drm_i915_private *dev_priv = dev->dev_private;
  7452. struct drm_atomic_state *state = crtc_state->base.state;
  7453. struct drm_connector *connector;
  7454. struct drm_connector_state *connector_state;
  7455. struct intel_encoder *encoder;
  7456. uint32_t dpll;
  7457. int factor, num_connectors = 0, i;
  7458. bool is_lvds = false, is_sdvo = false;
  7459. for_each_connector_in_state(state, connector, connector_state, i) {
  7460. if (connector_state->crtc != crtc_state->base.crtc)
  7461. continue;
  7462. encoder = to_intel_encoder(connector_state->best_encoder);
  7463. switch (encoder->type) {
  7464. case INTEL_OUTPUT_LVDS:
  7465. is_lvds = true;
  7466. break;
  7467. case INTEL_OUTPUT_SDVO:
  7468. case INTEL_OUTPUT_HDMI:
  7469. is_sdvo = true;
  7470. break;
  7471. default:
  7472. break;
  7473. }
  7474. num_connectors++;
  7475. }
  7476. /* Enable autotuning of the PLL clock (if permissible) */
  7477. factor = 21;
  7478. if (is_lvds) {
  7479. if ((intel_panel_use_ssc(dev_priv) &&
  7480. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7481. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7482. factor = 25;
  7483. } else if (crtc_state->sdvo_tv_clock)
  7484. factor = 20;
  7485. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7486. *fp |= FP_CB_TUNE;
  7487. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7488. *fp2 |= FP_CB_TUNE;
  7489. dpll = 0;
  7490. if (is_lvds)
  7491. dpll |= DPLLB_MODE_LVDS;
  7492. else
  7493. dpll |= DPLLB_MODE_DAC_SERIAL;
  7494. dpll |= (crtc_state->pixel_multiplier - 1)
  7495. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7496. if (is_sdvo)
  7497. dpll |= DPLL_SDVO_HIGH_SPEED;
  7498. if (crtc_state->has_dp_encoder)
  7499. dpll |= DPLL_SDVO_HIGH_SPEED;
  7500. /* compute bitmask from p1 value */
  7501. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7502. /* also FPA1 */
  7503. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7504. switch (crtc_state->dpll.p2) {
  7505. case 5:
  7506. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7507. break;
  7508. case 7:
  7509. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7510. break;
  7511. case 10:
  7512. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7513. break;
  7514. case 14:
  7515. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7516. break;
  7517. }
  7518. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7519. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7520. else
  7521. dpll |= PLL_REF_INPUT_DREFCLK;
  7522. return dpll | DPLL_VCO_ENABLE;
  7523. }
  7524. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7525. struct intel_crtc_state *crtc_state)
  7526. {
  7527. struct drm_device *dev = crtc->base.dev;
  7528. intel_clock_t clock, reduced_clock;
  7529. u32 dpll = 0, fp = 0, fp2 = 0;
  7530. bool ok, has_reduced_clock = false;
  7531. bool is_lvds = false;
  7532. struct intel_shared_dpll *pll;
  7533. memset(&crtc_state->dpll_hw_state, 0,
  7534. sizeof(crtc_state->dpll_hw_state));
  7535. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7536. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7537. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7538. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7539. &has_reduced_clock, &reduced_clock);
  7540. if (!ok && !crtc_state->clock_set) {
  7541. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7542. return -EINVAL;
  7543. }
  7544. /* Compat-code for transition, will disappear. */
  7545. if (!crtc_state->clock_set) {
  7546. crtc_state->dpll.n = clock.n;
  7547. crtc_state->dpll.m1 = clock.m1;
  7548. crtc_state->dpll.m2 = clock.m2;
  7549. crtc_state->dpll.p1 = clock.p1;
  7550. crtc_state->dpll.p2 = clock.p2;
  7551. }
  7552. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7553. if (crtc_state->has_pch_encoder) {
  7554. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7555. if (has_reduced_clock)
  7556. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7557. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7558. &fp, &reduced_clock,
  7559. has_reduced_clock ? &fp2 : NULL);
  7560. crtc_state->dpll_hw_state.dpll = dpll;
  7561. crtc_state->dpll_hw_state.fp0 = fp;
  7562. if (has_reduced_clock)
  7563. crtc_state->dpll_hw_state.fp1 = fp2;
  7564. else
  7565. crtc_state->dpll_hw_state.fp1 = fp;
  7566. pll = intel_get_shared_dpll(crtc, crtc_state);
  7567. if (pll == NULL) {
  7568. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7569. pipe_name(crtc->pipe));
  7570. return -EINVAL;
  7571. }
  7572. }
  7573. if (is_lvds && has_reduced_clock)
  7574. crtc->lowfreq_avail = true;
  7575. else
  7576. crtc->lowfreq_avail = false;
  7577. return 0;
  7578. }
  7579. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7580. struct intel_link_m_n *m_n)
  7581. {
  7582. struct drm_device *dev = crtc->base.dev;
  7583. struct drm_i915_private *dev_priv = dev->dev_private;
  7584. enum pipe pipe = crtc->pipe;
  7585. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7586. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7587. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7588. & ~TU_SIZE_MASK;
  7589. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7590. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7591. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7592. }
  7593. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7594. enum transcoder transcoder,
  7595. struct intel_link_m_n *m_n,
  7596. struct intel_link_m_n *m2_n2)
  7597. {
  7598. struct drm_device *dev = crtc->base.dev;
  7599. struct drm_i915_private *dev_priv = dev->dev_private;
  7600. enum pipe pipe = crtc->pipe;
  7601. if (INTEL_INFO(dev)->gen >= 5) {
  7602. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7603. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7604. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7605. & ~TU_SIZE_MASK;
  7606. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7607. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7608. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7609. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7610. * gen < 8) and if DRRS is supported (to make sure the
  7611. * registers are not unnecessarily read).
  7612. */
  7613. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7614. crtc->config->has_drrs) {
  7615. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7616. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7617. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7618. & ~TU_SIZE_MASK;
  7619. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7620. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7621. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7622. }
  7623. } else {
  7624. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7625. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7626. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7627. & ~TU_SIZE_MASK;
  7628. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7629. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7630. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7631. }
  7632. }
  7633. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7634. struct intel_crtc_state *pipe_config)
  7635. {
  7636. if (pipe_config->has_pch_encoder)
  7637. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7638. else
  7639. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7640. &pipe_config->dp_m_n,
  7641. &pipe_config->dp_m2_n2);
  7642. }
  7643. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7644. struct intel_crtc_state *pipe_config)
  7645. {
  7646. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7647. &pipe_config->fdi_m_n, NULL);
  7648. }
  7649. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7650. struct intel_crtc_state *pipe_config)
  7651. {
  7652. struct drm_device *dev = crtc->base.dev;
  7653. struct drm_i915_private *dev_priv = dev->dev_private;
  7654. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7655. uint32_t ps_ctrl = 0;
  7656. int id = -1;
  7657. int i;
  7658. /* find scaler attached to this pipe */
  7659. for (i = 0; i < crtc->num_scalers; i++) {
  7660. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7661. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7662. id = i;
  7663. pipe_config->pch_pfit.enabled = true;
  7664. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7665. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7666. break;
  7667. }
  7668. }
  7669. scaler_state->scaler_id = id;
  7670. if (id >= 0) {
  7671. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7672. } else {
  7673. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7674. }
  7675. }
  7676. static void
  7677. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7678. struct intel_initial_plane_config *plane_config)
  7679. {
  7680. struct drm_device *dev = crtc->base.dev;
  7681. struct drm_i915_private *dev_priv = dev->dev_private;
  7682. u32 val, base, offset, stride_mult, tiling;
  7683. int pipe = crtc->pipe;
  7684. int fourcc, pixel_format;
  7685. unsigned int aligned_height;
  7686. struct drm_framebuffer *fb;
  7687. struct intel_framebuffer *intel_fb;
  7688. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7689. if (!intel_fb) {
  7690. DRM_DEBUG_KMS("failed to alloc fb\n");
  7691. return;
  7692. }
  7693. fb = &intel_fb->base;
  7694. val = I915_READ(PLANE_CTL(pipe, 0));
  7695. if (!(val & PLANE_CTL_ENABLE))
  7696. goto error;
  7697. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7698. fourcc = skl_format_to_fourcc(pixel_format,
  7699. val & PLANE_CTL_ORDER_RGBX,
  7700. val & PLANE_CTL_ALPHA_MASK);
  7701. fb->pixel_format = fourcc;
  7702. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7703. tiling = val & PLANE_CTL_TILED_MASK;
  7704. switch (tiling) {
  7705. case PLANE_CTL_TILED_LINEAR:
  7706. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7707. break;
  7708. case PLANE_CTL_TILED_X:
  7709. plane_config->tiling = I915_TILING_X;
  7710. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7711. break;
  7712. case PLANE_CTL_TILED_Y:
  7713. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7714. break;
  7715. case PLANE_CTL_TILED_YF:
  7716. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7717. break;
  7718. default:
  7719. MISSING_CASE(tiling);
  7720. goto error;
  7721. }
  7722. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7723. plane_config->base = base;
  7724. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7725. val = I915_READ(PLANE_SIZE(pipe, 0));
  7726. fb->height = ((val >> 16) & 0xfff) + 1;
  7727. fb->width = ((val >> 0) & 0x1fff) + 1;
  7728. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7729. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7730. fb->pixel_format);
  7731. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7732. aligned_height = intel_fb_align_height(dev, fb->height,
  7733. fb->pixel_format,
  7734. fb->modifier[0]);
  7735. plane_config->size = fb->pitches[0] * aligned_height;
  7736. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7737. pipe_name(pipe), fb->width, fb->height,
  7738. fb->bits_per_pixel, base, fb->pitches[0],
  7739. plane_config->size);
  7740. plane_config->fb = intel_fb;
  7741. return;
  7742. error:
  7743. kfree(fb);
  7744. }
  7745. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7746. struct intel_crtc_state *pipe_config)
  7747. {
  7748. struct drm_device *dev = crtc->base.dev;
  7749. struct drm_i915_private *dev_priv = dev->dev_private;
  7750. uint32_t tmp;
  7751. tmp = I915_READ(PF_CTL(crtc->pipe));
  7752. if (tmp & PF_ENABLE) {
  7753. pipe_config->pch_pfit.enabled = true;
  7754. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7755. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7756. /* We currently do not free assignements of panel fitters on
  7757. * ivb/hsw (since we don't use the higher upscaling modes which
  7758. * differentiates them) so just WARN about this case for now. */
  7759. if (IS_GEN7(dev)) {
  7760. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7761. PF_PIPE_SEL_IVB(crtc->pipe));
  7762. }
  7763. }
  7764. }
  7765. static void
  7766. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7767. struct intel_initial_plane_config *plane_config)
  7768. {
  7769. struct drm_device *dev = crtc->base.dev;
  7770. struct drm_i915_private *dev_priv = dev->dev_private;
  7771. u32 val, base, offset;
  7772. int pipe = crtc->pipe;
  7773. int fourcc, pixel_format;
  7774. unsigned int aligned_height;
  7775. struct drm_framebuffer *fb;
  7776. struct intel_framebuffer *intel_fb;
  7777. val = I915_READ(DSPCNTR(pipe));
  7778. if (!(val & DISPLAY_PLANE_ENABLE))
  7779. return;
  7780. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7781. if (!intel_fb) {
  7782. DRM_DEBUG_KMS("failed to alloc fb\n");
  7783. return;
  7784. }
  7785. fb = &intel_fb->base;
  7786. if (INTEL_INFO(dev)->gen >= 4) {
  7787. if (val & DISPPLANE_TILED) {
  7788. plane_config->tiling = I915_TILING_X;
  7789. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7790. }
  7791. }
  7792. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7793. fourcc = i9xx_format_to_fourcc(pixel_format);
  7794. fb->pixel_format = fourcc;
  7795. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7796. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7797. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7798. offset = I915_READ(DSPOFFSET(pipe));
  7799. } else {
  7800. if (plane_config->tiling)
  7801. offset = I915_READ(DSPTILEOFF(pipe));
  7802. else
  7803. offset = I915_READ(DSPLINOFF(pipe));
  7804. }
  7805. plane_config->base = base;
  7806. val = I915_READ(PIPESRC(pipe));
  7807. fb->width = ((val >> 16) & 0xfff) + 1;
  7808. fb->height = ((val >> 0) & 0xfff) + 1;
  7809. val = I915_READ(DSPSTRIDE(pipe));
  7810. fb->pitches[0] = val & 0xffffffc0;
  7811. aligned_height = intel_fb_align_height(dev, fb->height,
  7812. fb->pixel_format,
  7813. fb->modifier[0]);
  7814. plane_config->size = fb->pitches[0] * aligned_height;
  7815. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7816. pipe_name(pipe), fb->width, fb->height,
  7817. fb->bits_per_pixel, base, fb->pitches[0],
  7818. plane_config->size);
  7819. plane_config->fb = intel_fb;
  7820. }
  7821. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7822. struct intel_crtc_state *pipe_config)
  7823. {
  7824. struct drm_device *dev = crtc->base.dev;
  7825. struct drm_i915_private *dev_priv = dev->dev_private;
  7826. enum intel_display_power_domain power_domain;
  7827. uint32_t tmp;
  7828. bool ret;
  7829. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7830. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7831. return false;
  7832. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7833. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7834. ret = false;
  7835. tmp = I915_READ(PIPECONF(crtc->pipe));
  7836. if (!(tmp & PIPECONF_ENABLE))
  7837. goto out;
  7838. switch (tmp & PIPECONF_BPC_MASK) {
  7839. case PIPECONF_6BPC:
  7840. pipe_config->pipe_bpp = 18;
  7841. break;
  7842. case PIPECONF_8BPC:
  7843. pipe_config->pipe_bpp = 24;
  7844. break;
  7845. case PIPECONF_10BPC:
  7846. pipe_config->pipe_bpp = 30;
  7847. break;
  7848. case PIPECONF_12BPC:
  7849. pipe_config->pipe_bpp = 36;
  7850. break;
  7851. default:
  7852. break;
  7853. }
  7854. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7855. pipe_config->limited_color_range = true;
  7856. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7857. struct intel_shared_dpll *pll;
  7858. pipe_config->has_pch_encoder = true;
  7859. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7860. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7861. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7862. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7863. if (HAS_PCH_IBX(dev_priv->dev)) {
  7864. pipe_config->shared_dpll =
  7865. (enum intel_dpll_id) crtc->pipe;
  7866. } else {
  7867. tmp = I915_READ(PCH_DPLL_SEL);
  7868. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7869. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7870. else
  7871. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7872. }
  7873. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7874. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7875. &pipe_config->dpll_hw_state));
  7876. tmp = pipe_config->dpll_hw_state.dpll;
  7877. pipe_config->pixel_multiplier =
  7878. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7879. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7880. ironlake_pch_clock_get(crtc, pipe_config);
  7881. } else {
  7882. pipe_config->pixel_multiplier = 1;
  7883. }
  7884. intel_get_pipe_timings(crtc, pipe_config);
  7885. ironlake_get_pfit_config(crtc, pipe_config);
  7886. ret = true;
  7887. out:
  7888. intel_display_power_put(dev_priv, power_domain);
  7889. return ret;
  7890. }
  7891. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7892. {
  7893. struct drm_device *dev = dev_priv->dev;
  7894. struct intel_crtc *crtc;
  7895. for_each_intel_crtc(dev, crtc)
  7896. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7897. pipe_name(crtc->pipe));
  7898. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7899. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7900. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7901. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7902. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7903. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7904. "CPU PWM1 enabled\n");
  7905. if (IS_HASWELL(dev))
  7906. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7907. "CPU PWM2 enabled\n");
  7908. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7909. "PCH PWM1 enabled\n");
  7910. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7911. "Utility pin enabled\n");
  7912. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7913. /*
  7914. * In theory we can still leave IRQs enabled, as long as only the HPD
  7915. * interrupts remain enabled. We used to check for that, but since it's
  7916. * gen-specific and since we only disable LCPLL after we fully disable
  7917. * the interrupts, the check below should be enough.
  7918. */
  7919. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7920. }
  7921. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7922. {
  7923. struct drm_device *dev = dev_priv->dev;
  7924. if (IS_HASWELL(dev))
  7925. return I915_READ(D_COMP_HSW);
  7926. else
  7927. return I915_READ(D_COMP_BDW);
  7928. }
  7929. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7930. {
  7931. struct drm_device *dev = dev_priv->dev;
  7932. if (IS_HASWELL(dev)) {
  7933. mutex_lock(&dev_priv->rps.hw_lock);
  7934. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7935. val))
  7936. DRM_ERROR("Failed to write to D_COMP\n");
  7937. mutex_unlock(&dev_priv->rps.hw_lock);
  7938. } else {
  7939. I915_WRITE(D_COMP_BDW, val);
  7940. POSTING_READ(D_COMP_BDW);
  7941. }
  7942. }
  7943. /*
  7944. * This function implements pieces of two sequences from BSpec:
  7945. * - Sequence for display software to disable LCPLL
  7946. * - Sequence for display software to allow package C8+
  7947. * The steps implemented here are just the steps that actually touch the LCPLL
  7948. * register. Callers should take care of disabling all the display engine
  7949. * functions, doing the mode unset, fixing interrupts, etc.
  7950. */
  7951. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7952. bool switch_to_fclk, bool allow_power_down)
  7953. {
  7954. uint32_t val;
  7955. assert_can_disable_lcpll(dev_priv);
  7956. val = I915_READ(LCPLL_CTL);
  7957. if (switch_to_fclk) {
  7958. val |= LCPLL_CD_SOURCE_FCLK;
  7959. I915_WRITE(LCPLL_CTL, val);
  7960. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7961. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7962. DRM_ERROR("Switching to FCLK failed\n");
  7963. val = I915_READ(LCPLL_CTL);
  7964. }
  7965. val |= LCPLL_PLL_DISABLE;
  7966. I915_WRITE(LCPLL_CTL, val);
  7967. POSTING_READ(LCPLL_CTL);
  7968. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7969. DRM_ERROR("LCPLL still locked\n");
  7970. val = hsw_read_dcomp(dev_priv);
  7971. val |= D_COMP_COMP_DISABLE;
  7972. hsw_write_dcomp(dev_priv, val);
  7973. ndelay(100);
  7974. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7975. 1))
  7976. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7977. if (allow_power_down) {
  7978. val = I915_READ(LCPLL_CTL);
  7979. val |= LCPLL_POWER_DOWN_ALLOW;
  7980. I915_WRITE(LCPLL_CTL, val);
  7981. POSTING_READ(LCPLL_CTL);
  7982. }
  7983. }
  7984. /*
  7985. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7986. * source.
  7987. */
  7988. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7989. {
  7990. uint32_t val;
  7991. val = I915_READ(LCPLL_CTL);
  7992. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7993. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7994. return;
  7995. /*
  7996. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7997. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7998. */
  7999. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8000. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8001. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8002. I915_WRITE(LCPLL_CTL, val);
  8003. POSTING_READ(LCPLL_CTL);
  8004. }
  8005. val = hsw_read_dcomp(dev_priv);
  8006. val |= D_COMP_COMP_FORCE;
  8007. val &= ~D_COMP_COMP_DISABLE;
  8008. hsw_write_dcomp(dev_priv, val);
  8009. val = I915_READ(LCPLL_CTL);
  8010. val &= ~LCPLL_PLL_DISABLE;
  8011. I915_WRITE(LCPLL_CTL, val);
  8012. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  8013. DRM_ERROR("LCPLL not locked yet\n");
  8014. if (val & LCPLL_CD_SOURCE_FCLK) {
  8015. val = I915_READ(LCPLL_CTL);
  8016. val &= ~LCPLL_CD_SOURCE_FCLK;
  8017. I915_WRITE(LCPLL_CTL, val);
  8018. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8019. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8020. DRM_ERROR("Switching back to LCPLL failed\n");
  8021. }
  8022. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8023. intel_update_cdclk(dev_priv->dev);
  8024. }
  8025. /*
  8026. * Package states C8 and deeper are really deep PC states that can only be
  8027. * reached when all the devices on the system allow it, so even if the graphics
  8028. * device allows PC8+, it doesn't mean the system will actually get to these
  8029. * states. Our driver only allows PC8+ when going into runtime PM.
  8030. *
  8031. * The requirements for PC8+ are that all the outputs are disabled, the power
  8032. * well is disabled and most interrupts are disabled, and these are also
  8033. * requirements for runtime PM. When these conditions are met, we manually do
  8034. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8035. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8036. * hang the machine.
  8037. *
  8038. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8039. * the state of some registers, so when we come back from PC8+ we need to
  8040. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8041. * need to take care of the registers kept by RC6. Notice that this happens even
  8042. * if we don't put the device in PCI D3 state (which is what currently happens
  8043. * because of the runtime PM support).
  8044. *
  8045. * For more, read "Display Sequences for Package C8" on the hardware
  8046. * documentation.
  8047. */
  8048. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8049. {
  8050. struct drm_device *dev = dev_priv->dev;
  8051. uint32_t val;
  8052. DRM_DEBUG_KMS("Enabling package C8+\n");
  8053. if (HAS_PCH_LPT_LP(dev)) {
  8054. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8055. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8056. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8057. }
  8058. lpt_disable_clkout_dp(dev);
  8059. hsw_disable_lcpll(dev_priv, true, true);
  8060. }
  8061. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8062. {
  8063. struct drm_device *dev = dev_priv->dev;
  8064. uint32_t val;
  8065. DRM_DEBUG_KMS("Disabling package C8+\n");
  8066. hsw_restore_lcpll(dev_priv);
  8067. lpt_init_pch_refclk(dev);
  8068. if (HAS_PCH_LPT_LP(dev)) {
  8069. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8070. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8071. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8072. }
  8073. }
  8074. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8075. {
  8076. struct drm_device *dev = old_state->dev;
  8077. struct intel_atomic_state *old_intel_state =
  8078. to_intel_atomic_state(old_state);
  8079. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8080. broxton_set_cdclk(dev, req_cdclk);
  8081. }
  8082. /* compute the max rate for new configuration */
  8083. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8084. {
  8085. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8086. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8087. struct drm_crtc *crtc;
  8088. struct drm_crtc_state *cstate;
  8089. struct intel_crtc_state *crtc_state;
  8090. unsigned max_pixel_rate = 0, i;
  8091. enum pipe pipe;
  8092. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8093. sizeof(intel_state->min_pixclk));
  8094. for_each_crtc_in_state(state, crtc, cstate, i) {
  8095. int pixel_rate;
  8096. crtc_state = to_intel_crtc_state(cstate);
  8097. if (!crtc_state->base.enable) {
  8098. intel_state->min_pixclk[i] = 0;
  8099. continue;
  8100. }
  8101. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8102. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8103. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8104. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8105. intel_state->min_pixclk[i] = pixel_rate;
  8106. }
  8107. for_each_pipe(dev_priv, pipe)
  8108. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8109. return max_pixel_rate;
  8110. }
  8111. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8112. {
  8113. struct drm_i915_private *dev_priv = dev->dev_private;
  8114. uint32_t val, data;
  8115. int ret;
  8116. if (WARN((I915_READ(LCPLL_CTL) &
  8117. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8118. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8119. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8120. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8121. "trying to change cdclk frequency with cdclk not enabled\n"))
  8122. return;
  8123. mutex_lock(&dev_priv->rps.hw_lock);
  8124. ret = sandybridge_pcode_write(dev_priv,
  8125. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8126. mutex_unlock(&dev_priv->rps.hw_lock);
  8127. if (ret) {
  8128. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8129. return;
  8130. }
  8131. val = I915_READ(LCPLL_CTL);
  8132. val |= LCPLL_CD_SOURCE_FCLK;
  8133. I915_WRITE(LCPLL_CTL, val);
  8134. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8135. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8136. DRM_ERROR("Switching to FCLK failed\n");
  8137. val = I915_READ(LCPLL_CTL);
  8138. val &= ~LCPLL_CLK_FREQ_MASK;
  8139. switch (cdclk) {
  8140. case 450000:
  8141. val |= LCPLL_CLK_FREQ_450;
  8142. data = 0;
  8143. break;
  8144. case 540000:
  8145. val |= LCPLL_CLK_FREQ_54O_BDW;
  8146. data = 1;
  8147. break;
  8148. case 337500:
  8149. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8150. data = 2;
  8151. break;
  8152. case 675000:
  8153. val |= LCPLL_CLK_FREQ_675_BDW;
  8154. data = 3;
  8155. break;
  8156. default:
  8157. WARN(1, "invalid cdclk frequency\n");
  8158. return;
  8159. }
  8160. I915_WRITE(LCPLL_CTL, val);
  8161. val = I915_READ(LCPLL_CTL);
  8162. val &= ~LCPLL_CD_SOURCE_FCLK;
  8163. I915_WRITE(LCPLL_CTL, val);
  8164. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8165. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8166. DRM_ERROR("Switching back to LCPLL failed\n");
  8167. mutex_lock(&dev_priv->rps.hw_lock);
  8168. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8169. mutex_unlock(&dev_priv->rps.hw_lock);
  8170. intel_update_cdclk(dev);
  8171. WARN(cdclk != dev_priv->cdclk_freq,
  8172. "cdclk requested %d kHz but got %d kHz\n",
  8173. cdclk, dev_priv->cdclk_freq);
  8174. }
  8175. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8176. {
  8177. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8178. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8179. int max_pixclk = ilk_max_pixel_rate(state);
  8180. int cdclk;
  8181. /*
  8182. * FIXME should also account for plane ratio
  8183. * once 64bpp pixel formats are supported.
  8184. */
  8185. if (max_pixclk > 540000)
  8186. cdclk = 675000;
  8187. else if (max_pixclk > 450000)
  8188. cdclk = 540000;
  8189. else if (max_pixclk > 337500)
  8190. cdclk = 450000;
  8191. else
  8192. cdclk = 337500;
  8193. if (cdclk > dev_priv->max_cdclk_freq) {
  8194. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8195. cdclk, dev_priv->max_cdclk_freq);
  8196. return -EINVAL;
  8197. }
  8198. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8199. if (!intel_state->active_crtcs)
  8200. intel_state->dev_cdclk = 337500;
  8201. return 0;
  8202. }
  8203. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8204. {
  8205. struct drm_device *dev = old_state->dev;
  8206. struct intel_atomic_state *old_intel_state =
  8207. to_intel_atomic_state(old_state);
  8208. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8209. broadwell_set_cdclk(dev, req_cdclk);
  8210. }
  8211. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8212. struct intel_crtc_state *crtc_state)
  8213. {
  8214. struct intel_encoder *intel_encoder =
  8215. intel_ddi_get_crtc_new_encoder(crtc_state);
  8216. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8217. if (!intel_ddi_pll_select(crtc, crtc_state))
  8218. return -EINVAL;
  8219. }
  8220. crtc->lowfreq_avail = false;
  8221. return 0;
  8222. }
  8223. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8224. enum port port,
  8225. struct intel_crtc_state *pipe_config)
  8226. {
  8227. switch (port) {
  8228. case PORT_A:
  8229. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8230. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8231. break;
  8232. case PORT_B:
  8233. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8234. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8235. break;
  8236. case PORT_C:
  8237. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8238. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8239. break;
  8240. default:
  8241. DRM_ERROR("Incorrect port type\n");
  8242. }
  8243. }
  8244. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8245. enum port port,
  8246. struct intel_crtc_state *pipe_config)
  8247. {
  8248. u32 temp, dpll_ctl1;
  8249. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8250. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8251. switch (pipe_config->ddi_pll_sel) {
  8252. case SKL_DPLL0:
  8253. /*
  8254. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8255. * of the shared DPLL framework and thus needs to be read out
  8256. * separately
  8257. */
  8258. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8259. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8260. break;
  8261. case SKL_DPLL1:
  8262. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8263. break;
  8264. case SKL_DPLL2:
  8265. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8266. break;
  8267. case SKL_DPLL3:
  8268. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8269. break;
  8270. }
  8271. }
  8272. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8273. enum port port,
  8274. struct intel_crtc_state *pipe_config)
  8275. {
  8276. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8277. switch (pipe_config->ddi_pll_sel) {
  8278. case PORT_CLK_SEL_WRPLL1:
  8279. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8280. break;
  8281. case PORT_CLK_SEL_WRPLL2:
  8282. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8283. break;
  8284. case PORT_CLK_SEL_SPLL:
  8285. pipe_config->shared_dpll = DPLL_ID_SPLL;
  8286. break;
  8287. }
  8288. }
  8289. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8290. struct intel_crtc_state *pipe_config)
  8291. {
  8292. struct drm_device *dev = crtc->base.dev;
  8293. struct drm_i915_private *dev_priv = dev->dev_private;
  8294. struct intel_shared_dpll *pll;
  8295. enum port port;
  8296. uint32_t tmp;
  8297. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8298. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8299. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8300. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8301. else if (IS_BROXTON(dev))
  8302. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8303. else
  8304. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8305. if (pipe_config->shared_dpll >= 0) {
  8306. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8307. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8308. &pipe_config->dpll_hw_state));
  8309. }
  8310. /*
  8311. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8312. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8313. * the PCH transcoder is on.
  8314. */
  8315. if (INTEL_INFO(dev)->gen < 9 &&
  8316. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8317. pipe_config->has_pch_encoder = true;
  8318. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8319. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8320. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8321. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8322. }
  8323. }
  8324. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8325. struct intel_crtc_state *pipe_config)
  8326. {
  8327. struct drm_device *dev = crtc->base.dev;
  8328. struct drm_i915_private *dev_priv = dev->dev_private;
  8329. enum intel_display_power_domain power_domain;
  8330. unsigned long power_domain_mask;
  8331. uint32_t tmp;
  8332. bool ret;
  8333. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8334. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8335. return false;
  8336. power_domain_mask = BIT(power_domain);
  8337. ret = false;
  8338. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8339. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8340. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8341. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8342. enum pipe trans_edp_pipe;
  8343. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8344. default:
  8345. WARN(1, "unknown pipe linked to edp transcoder\n");
  8346. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8347. case TRANS_DDI_EDP_INPUT_A_ON:
  8348. trans_edp_pipe = PIPE_A;
  8349. break;
  8350. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8351. trans_edp_pipe = PIPE_B;
  8352. break;
  8353. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8354. trans_edp_pipe = PIPE_C;
  8355. break;
  8356. }
  8357. if (trans_edp_pipe == crtc->pipe)
  8358. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8359. }
  8360. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8361. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8362. goto out;
  8363. power_domain_mask |= BIT(power_domain);
  8364. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8365. if (!(tmp & PIPECONF_ENABLE))
  8366. goto out;
  8367. haswell_get_ddi_port_state(crtc, pipe_config);
  8368. intel_get_pipe_timings(crtc, pipe_config);
  8369. if (INTEL_INFO(dev)->gen >= 9) {
  8370. skl_init_scalers(dev, crtc, pipe_config);
  8371. }
  8372. if (INTEL_INFO(dev)->gen >= 9) {
  8373. pipe_config->scaler_state.scaler_id = -1;
  8374. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8375. }
  8376. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8377. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8378. power_domain_mask |= BIT(power_domain);
  8379. if (INTEL_INFO(dev)->gen >= 9)
  8380. skylake_get_pfit_config(crtc, pipe_config);
  8381. else
  8382. ironlake_get_pfit_config(crtc, pipe_config);
  8383. }
  8384. if (IS_HASWELL(dev))
  8385. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8386. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8387. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8388. pipe_config->pixel_multiplier =
  8389. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8390. } else {
  8391. pipe_config->pixel_multiplier = 1;
  8392. }
  8393. ret = true;
  8394. out:
  8395. for_each_power_domain(power_domain, power_domain_mask)
  8396. intel_display_power_put(dev_priv, power_domain);
  8397. return ret;
  8398. }
  8399. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8400. const struct intel_plane_state *plane_state)
  8401. {
  8402. struct drm_device *dev = crtc->dev;
  8403. struct drm_i915_private *dev_priv = dev->dev_private;
  8404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8405. uint32_t cntl = 0, size = 0;
  8406. if (plane_state && plane_state->visible) {
  8407. unsigned int width = plane_state->base.crtc_w;
  8408. unsigned int height = plane_state->base.crtc_h;
  8409. unsigned int stride = roundup_pow_of_two(width) * 4;
  8410. switch (stride) {
  8411. default:
  8412. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8413. width, stride);
  8414. stride = 256;
  8415. /* fallthrough */
  8416. case 256:
  8417. case 512:
  8418. case 1024:
  8419. case 2048:
  8420. break;
  8421. }
  8422. cntl |= CURSOR_ENABLE |
  8423. CURSOR_GAMMA_ENABLE |
  8424. CURSOR_FORMAT_ARGB |
  8425. CURSOR_STRIDE(stride);
  8426. size = (height << 12) | width;
  8427. }
  8428. if (intel_crtc->cursor_cntl != 0 &&
  8429. (intel_crtc->cursor_base != base ||
  8430. intel_crtc->cursor_size != size ||
  8431. intel_crtc->cursor_cntl != cntl)) {
  8432. /* On these chipsets we can only modify the base/size/stride
  8433. * whilst the cursor is disabled.
  8434. */
  8435. I915_WRITE(CURCNTR(PIPE_A), 0);
  8436. POSTING_READ(CURCNTR(PIPE_A));
  8437. intel_crtc->cursor_cntl = 0;
  8438. }
  8439. if (intel_crtc->cursor_base != base) {
  8440. I915_WRITE(CURBASE(PIPE_A), base);
  8441. intel_crtc->cursor_base = base;
  8442. }
  8443. if (intel_crtc->cursor_size != size) {
  8444. I915_WRITE(CURSIZE, size);
  8445. intel_crtc->cursor_size = size;
  8446. }
  8447. if (intel_crtc->cursor_cntl != cntl) {
  8448. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8449. POSTING_READ(CURCNTR(PIPE_A));
  8450. intel_crtc->cursor_cntl = cntl;
  8451. }
  8452. }
  8453. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8454. const struct intel_plane_state *plane_state)
  8455. {
  8456. struct drm_device *dev = crtc->dev;
  8457. struct drm_i915_private *dev_priv = dev->dev_private;
  8458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8459. int pipe = intel_crtc->pipe;
  8460. uint32_t cntl = 0;
  8461. if (plane_state && plane_state->visible) {
  8462. cntl = MCURSOR_GAMMA_ENABLE;
  8463. switch (plane_state->base.crtc_w) {
  8464. case 64:
  8465. cntl |= CURSOR_MODE_64_ARGB_AX;
  8466. break;
  8467. case 128:
  8468. cntl |= CURSOR_MODE_128_ARGB_AX;
  8469. break;
  8470. case 256:
  8471. cntl |= CURSOR_MODE_256_ARGB_AX;
  8472. break;
  8473. default:
  8474. MISSING_CASE(plane_state->base.crtc_w);
  8475. return;
  8476. }
  8477. cntl |= pipe << 28; /* Connect to correct pipe */
  8478. if (HAS_DDI(dev))
  8479. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8480. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8481. cntl |= CURSOR_ROTATE_180;
  8482. }
  8483. if (intel_crtc->cursor_cntl != cntl) {
  8484. I915_WRITE(CURCNTR(pipe), cntl);
  8485. POSTING_READ(CURCNTR(pipe));
  8486. intel_crtc->cursor_cntl = cntl;
  8487. }
  8488. /* and commit changes on next vblank */
  8489. I915_WRITE(CURBASE(pipe), base);
  8490. POSTING_READ(CURBASE(pipe));
  8491. intel_crtc->cursor_base = base;
  8492. }
  8493. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8494. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8495. const struct intel_plane_state *plane_state)
  8496. {
  8497. struct drm_device *dev = crtc->dev;
  8498. struct drm_i915_private *dev_priv = dev->dev_private;
  8499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8500. int pipe = intel_crtc->pipe;
  8501. u32 base = intel_crtc->cursor_addr;
  8502. u32 pos = 0;
  8503. if (plane_state) {
  8504. int x = plane_state->base.crtc_x;
  8505. int y = plane_state->base.crtc_y;
  8506. if (x < 0) {
  8507. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8508. x = -x;
  8509. }
  8510. pos |= x << CURSOR_X_SHIFT;
  8511. if (y < 0) {
  8512. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8513. y = -y;
  8514. }
  8515. pos |= y << CURSOR_Y_SHIFT;
  8516. /* ILK+ do this automagically */
  8517. if (HAS_GMCH_DISPLAY(dev) &&
  8518. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8519. base += (plane_state->base.crtc_h *
  8520. plane_state->base.crtc_w - 1) * 4;
  8521. }
  8522. }
  8523. I915_WRITE(CURPOS(pipe), pos);
  8524. if (IS_845G(dev) || IS_I865G(dev))
  8525. i845_update_cursor(crtc, base, plane_state);
  8526. else
  8527. i9xx_update_cursor(crtc, base, plane_state);
  8528. }
  8529. static bool cursor_size_ok(struct drm_device *dev,
  8530. uint32_t width, uint32_t height)
  8531. {
  8532. if (width == 0 || height == 0)
  8533. return false;
  8534. /*
  8535. * 845g/865g are special in that they are only limited by
  8536. * the width of their cursors, the height is arbitrary up to
  8537. * the precision of the register. Everything else requires
  8538. * square cursors, limited to a few power-of-two sizes.
  8539. */
  8540. if (IS_845G(dev) || IS_I865G(dev)) {
  8541. if ((width & 63) != 0)
  8542. return false;
  8543. if (width > (IS_845G(dev) ? 64 : 512))
  8544. return false;
  8545. if (height > 1023)
  8546. return false;
  8547. } else {
  8548. switch (width | height) {
  8549. case 256:
  8550. case 128:
  8551. if (IS_GEN2(dev))
  8552. return false;
  8553. case 64:
  8554. break;
  8555. default:
  8556. return false;
  8557. }
  8558. }
  8559. return true;
  8560. }
  8561. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8562. u16 *blue, uint32_t start, uint32_t size)
  8563. {
  8564. int end = (start + size > 256) ? 256 : start + size, i;
  8565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8566. for (i = start; i < end; i++) {
  8567. intel_crtc->lut_r[i] = red[i] >> 8;
  8568. intel_crtc->lut_g[i] = green[i] >> 8;
  8569. intel_crtc->lut_b[i] = blue[i] >> 8;
  8570. }
  8571. intel_crtc_load_lut(crtc);
  8572. }
  8573. /* VESA 640x480x72Hz mode to set on the pipe */
  8574. static struct drm_display_mode load_detect_mode = {
  8575. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8576. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8577. };
  8578. struct drm_framebuffer *
  8579. __intel_framebuffer_create(struct drm_device *dev,
  8580. struct drm_mode_fb_cmd2 *mode_cmd,
  8581. struct drm_i915_gem_object *obj)
  8582. {
  8583. struct intel_framebuffer *intel_fb;
  8584. int ret;
  8585. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8586. if (!intel_fb)
  8587. return ERR_PTR(-ENOMEM);
  8588. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8589. if (ret)
  8590. goto err;
  8591. return &intel_fb->base;
  8592. err:
  8593. kfree(intel_fb);
  8594. return ERR_PTR(ret);
  8595. }
  8596. static struct drm_framebuffer *
  8597. intel_framebuffer_create(struct drm_device *dev,
  8598. struct drm_mode_fb_cmd2 *mode_cmd,
  8599. struct drm_i915_gem_object *obj)
  8600. {
  8601. struct drm_framebuffer *fb;
  8602. int ret;
  8603. ret = i915_mutex_lock_interruptible(dev);
  8604. if (ret)
  8605. return ERR_PTR(ret);
  8606. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8607. mutex_unlock(&dev->struct_mutex);
  8608. return fb;
  8609. }
  8610. static u32
  8611. intel_framebuffer_pitch_for_width(int width, int bpp)
  8612. {
  8613. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8614. return ALIGN(pitch, 64);
  8615. }
  8616. static u32
  8617. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8618. {
  8619. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8620. return PAGE_ALIGN(pitch * mode->vdisplay);
  8621. }
  8622. static struct drm_framebuffer *
  8623. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8624. struct drm_display_mode *mode,
  8625. int depth, int bpp)
  8626. {
  8627. struct drm_framebuffer *fb;
  8628. struct drm_i915_gem_object *obj;
  8629. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8630. obj = i915_gem_alloc_object(dev,
  8631. intel_framebuffer_size_for_mode(mode, bpp));
  8632. if (obj == NULL)
  8633. return ERR_PTR(-ENOMEM);
  8634. mode_cmd.width = mode->hdisplay;
  8635. mode_cmd.height = mode->vdisplay;
  8636. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8637. bpp);
  8638. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8639. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8640. if (IS_ERR(fb))
  8641. drm_gem_object_unreference_unlocked(&obj->base);
  8642. return fb;
  8643. }
  8644. static struct drm_framebuffer *
  8645. mode_fits_in_fbdev(struct drm_device *dev,
  8646. struct drm_display_mode *mode)
  8647. {
  8648. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8649. struct drm_i915_private *dev_priv = dev->dev_private;
  8650. struct drm_i915_gem_object *obj;
  8651. struct drm_framebuffer *fb;
  8652. if (!dev_priv->fbdev)
  8653. return NULL;
  8654. if (!dev_priv->fbdev->fb)
  8655. return NULL;
  8656. obj = dev_priv->fbdev->fb->obj;
  8657. BUG_ON(!obj);
  8658. fb = &dev_priv->fbdev->fb->base;
  8659. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8660. fb->bits_per_pixel))
  8661. return NULL;
  8662. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8663. return NULL;
  8664. drm_framebuffer_reference(fb);
  8665. return fb;
  8666. #else
  8667. return NULL;
  8668. #endif
  8669. }
  8670. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8671. struct drm_crtc *crtc,
  8672. struct drm_display_mode *mode,
  8673. struct drm_framebuffer *fb,
  8674. int x, int y)
  8675. {
  8676. struct drm_plane_state *plane_state;
  8677. int hdisplay, vdisplay;
  8678. int ret;
  8679. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8680. if (IS_ERR(plane_state))
  8681. return PTR_ERR(plane_state);
  8682. if (mode)
  8683. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8684. else
  8685. hdisplay = vdisplay = 0;
  8686. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8687. if (ret)
  8688. return ret;
  8689. drm_atomic_set_fb_for_plane(plane_state, fb);
  8690. plane_state->crtc_x = 0;
  8691. plane_state->crtc_y = 0;
  8692. plane_state->crtc_w = hdisplay;
  8693. plane_state->crtc_h = vdisplay;
  8694. plane_state->src_x = x << 16;
  8695. plane_state->src_y = y << 16;
  8696. plane_state->src_w = hdisplay << 16;
  8697. plane_state->src_h = vdisplay << 16;
  8698. return 0;
  8699. }
  8700. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8701. struct drm_display_mode *mode,
  8702. struct intel_load_detect_pipe *old,
  8703. struct drm_modeset_acquire_ctx *ctx)
  8704. {
  8705. struct intel_crtc *intel_crtc;
  8706. struct intel_encoder *intel_encoder =
  8707. intel_attached_encoder(connector);
  8708. struct drm_crtc *possible_crtc;
  8709. struct drm_encoder *encoder = &intel_encoder->base;
  8710. struct drm_crtc *crtc = NULL;
  8711. struct drm_device *dev = encoder->dev;
  8712. struct drm_framebuffer *fb;
  8713. struct drm_mode_config *config = &dev->mode_config;
  8714. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8715. struct drm_connector_state *connector_state;
  8716. struct intel_crtc_state *crtc_state;
  8717. int ret, i = -1;
  8718. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8719. connector->base.id, connector->name,
  8720. encoder->base.id, encoder->name);
  8721. old->restore_state = NULL;
  8722. retry:
  8723. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8724. if (ret)
  8725. goto fail;
  8726. /*
  8727. * Algorithm gets a little messy:
  8728. *
  8729. * - if the connector already has an assigned crtc, use it (but make
  8730. * sure it's on first)
  8731. *
  8732. * - try to find the first unused crtc that can drive this connector,
  8733. * and use that if we find one
  8734. */
  8735. /* See if we already have a CRTC for this connector */
  8736. if (connector->state->crtc) {
  8737. crtc = connector->state->crtc;
  8738. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8739. if (ret)
  8740. goto fail;
  8741. /* Make sure the crtc and connector are running */
  8742. goto found;
  8743. }
  8744. /* Find an unused one (if possible) */
  8745. for_each_crtc(dev, possible_crtc) {
  8746. i++;
  8747. if (!(encoder->possible_crtcs & (1 << i)))
  8748. continue;
  8749. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8750. if (ret)
  8751. goto fail;
  8752. if (possible_crtc->state->enable) {
  8753. drm_modeset_unlock(&possible_crtc->mutex);
  8754. continue;
  8755. }
  8756. crtc = possible_crtc;
  8757. break;
  8758. }
  8759. /*
  8760. * If we didn't find an unused CRTC, don't use any.
  8761. */
  8762. if (!crtc) {
  8763. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8764. goto fail;
  8765. }
  8766. found:
  8767. intel_crtc = to_intel_crtc(crtc);
  8768. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8769. if (ret)
  8770. goto fail;
  8771. state = drm_atomic_state_alloc(dev);
  8772. restore_state = drm_atomic_state_alloc(dev);
  8773. if (!state || !restore_state) {
  8774. ret = -ENOMEM;
  8775. goto fail;
  8776. }
  8777. state->acquire_ctx = ctx;
  8778. restore_state->acquire_ctx = ctx;
  8779. connector_state = drm_atomic_get_connector_state(state, connector);
  8780. if (IS_ERR(connector_state)) {
  8781. ret = PTR_ERR(connector_state);
  8782. goto fail;
  8783. }
  8784. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8785. if (ret)
  8786. goto fail;
  8787. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8788. if (IS_ERR(crtc_state)) {
  8789. ret = PTR_ERR(crtc_state);
  8790. goto fail;
  8791. }
  8792. crtc_state->base.active = crtc_state->base.enable = true;
  8793. if (!mode)
  8794. mode = &load_detect_mode;
  8795. /* We need a framebuffer large enough to accommodate all accesses
  8796. * that the plane may generate whilst we perform load detection.
  8797. * We can not rely on the fbcon either being present (we get called
  8798. * during its initialisation to detect all boot displays, or it may
  8799. * not even exist) or that it is large enough to satisfy the
  8800. * requested mode.
  8801. */
  8802. fb = mode_fits_in_fbdev(dev, mode);
  8803. if (fb == NULL) {
  8804. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8805. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8806. } else
  8807. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8808. if (IS_ERR(fb)) {
  8809. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8810. goto fail;
  8811. }
  8812. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8813. if (ret)
  8814. goto fail;
  8815. drm_framebuffer_unreference(fb);
  8816. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8817. if (ret)
  8818. goto fail;
  8819. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8820. if (!ret)
  8821. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8822. if (!ret)
  8823. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8824. if (ret) {
  8825. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8826. goto fail;
  8827. }
  8828. ret = drm_atomic_commit(state);
  8829. if (ret) {
  8830. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8831. goto fail;
  8832. }
  8833. old->restore_state = restore_state;
  8834. /* let the connector get through one full cycle before testing */
  8835. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8836. return true;
  8837. fail:
  8838. drm_atomic_state_free(state);
  8839. drm_atomic_state_free(restore_state);
  8840. restore_state = state = NULL;
  8841. if (ret == -EDEADLK) {
  8842. drm_modeset_backoff(ctx);
  8843. goto retry;
  8844. }
  8845. return false;
  8846. }
  8847. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8848. struct intel_load_detect_pipe *old,
  8849. struct drm_modeset_acquire_ctx *ctx)
  8850. {
  8851. struct intel_encoder *intel_encoder =
  8852. intel_attached_encoder(connector);
  8853. struct drm_encoder *encoder = &intel_encoder->base;
  8854. struct drm_atomic_state *state = old->restore_state;
  8855. int ret;
  8856. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8857. connector->base.id, connector->name,
  8858. encoder->base.id, encoder->name);
  8859. if (!state)
  8860. return;
  8861. ret = drm_atomic_commit(state);
  8862. if (ret) {
  8863. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8864. drm_atomic_state_free(state);
  8865. }
  8866. }
  8867. static int i9xx_pll_refclk(struct drm_device *dev,
  8868. const struct intel_crtc_state *pipe_config)
  8869. {
  8870. struct drm_i915_private *dev_priv = dev->dev_private;
  8871. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8872. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8873. return dev_priv->vbt.lvds_ssc_freq;
  8874. else if (HAS_PCH_SPLIT(dev))
  8875. return 120000;
  8876. else if (!IS_GEN2(dev))
  8877. return 96000;
  8878. else
  8879. return 48000;
  8880. }
  8881. /* Returns the clock of the currently programmed mode of the given pipe. */
  8882. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8883. struct intel_crtc_state *pipe_config)
  8884. {
  8885. struct drm_device *dev = crtc->base.dev;
  8886. struct drm_i915_private *dev_priv = dev->dev_private;
  8887. int pipe = pipe_config->cpu_transcoder;
  8888. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8889. u32 fp;
  8890. intel_clock_t clock;
  8891. int port_clock;
  8892. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8893. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8894. fp = pipe_config->dpll_hw_state.fp0;
  8895. else
  8896. fp = pipe_config->dpll_hw_state.fp1;
  8897. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8898. if (IS_PINEVIEW(dev)) {
  8899. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8900. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8901. } else {
  8902. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8903. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8904. }
  8905. if (!IS_GEN2(dev)) {
  8906. if (IS_PINEVIEW(dev))
  8907. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8908. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8909. else
  8910. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8911. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8912. switch (dpll & DPLL_MODE_MASK) {
  8913. case DPLLB_MODE_DAC_SERIAL:
  8914. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8915. 5 : 10;
  8916. break;
  8917. case DPLLB_MODE_LVDS:
  8918. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8919. 7 : 14;
  8920. break;
  8921. default:
  8922. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8923. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8924. return;
  8925. }
  8926. if (IS_PINEVIEW(dev))
  8927. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8928. else
  8929. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8930. } else {
  8931. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8932. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8933. if (is_lvds) {
  8934. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8935. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8936. if (lvds & LVDS_CLKB_POWER_UP)
  8937. clock.p2 = 7;
  8938. else
  8939. clock.p2 = 14;
  8940. } else {
  8941. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8942. clock.p1 = 2;
  8943. else {
  8944. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8945. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8946. }
  8947. if (dpll & PLL_P2_DIVIDE_BY_4)
  8948. clock.p2 = 4;
  8949. else
  8950. clock.p2 = 2;
  8951. }
  8952. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8953. }
  8954. /*
  8955. * This value includes pixel_multiplier. We will use
  8956. * port_clock to compute adjusted_mode.crtc_clock in the
  8957. * encoder's get_config() function.
  8958. */
  8959. pipe_config->port_clock = port_clock;
  8960. }
  8961. int intel_dotclock_calculate(int link_freq,
  8962. const struct intel_link_m_n *m_n)
  8963. {
  8964. /*
  8965. * The calculation for the data clock is:
  8966. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8967. * But we want to avoid losing precison if possible, so:
  8968. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8969. *
  8970. * and the link clock is simpler:
  8971. * link_clock = (m * link_clock) / n
  8972. */
  8973. if (!m_n->link_n)
  8974. return 0;
  8975. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8976. }
  8977. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8978. struct intel_crtc_state *pipe_config)
  8979. {
  8980. struct drm_device *dev = crtc->base.dev;
  8981. /* read out port_clock from the DPLL */
  8982. i9xx_crtc_clock_get(crtc, pipe_config);
  8983. /*
  8984. * This value does not include pixel_multiplier.
  8985. * We will check that port_clock and adjusted_mode.crtc_clock
  8986. * agree once we know their relationship in the encoder's
  8987. * get_config() function.
  8988. */
  8989. pipe_config->base.adjusted_mode.crtc_clock =
  8990. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8991. &pipe_config->fdi_m_n);
  8992. }
  8993. /** Returns the currently programmed mode of the given pipe. */
  8994. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8995. struct drm_crtc *crtc)
  8996. {
  8997. struct drm_i915_private *dev_priv = dev->dev_private;
  8998. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8999. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9000. struct drm_display_mode *mode;
  9001. struct intel_crtc_state *pipe_config;
  9002. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9003. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9004. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9005. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9006. enum pipe pipe = intel_crtc->pipe;
  9007. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9008. if (!mode)
  9009. return NULL;
  9010. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9011. if (!pipe_config) {
  9012. kfree(mode);
  9013. return NULL;
  9014. }
  9015. /*
  9016. * Construct a pipe_config sufficient for getting the clock info
  9017. * back out of crtc_clock_get.
  9018. *
  9019. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9020. * to use a real value here instead.
  9021. */
  9022. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9023. pipe_config->pixel_multiplier = 1;
  9024. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9025. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9026. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9027. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9028. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9029. mode->hdisplay = (htot & 0xffff) + 1;
  9030. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9031. mode->hsync_start = (hsync & 0xffff) + 1;
  9032. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9033. mode->vdisplay = (vtot & 0xffff) + 1;
  9034. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9035. mode->vsync_start = (vsync & 0xffff) + 1;
  9036. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9037. drm_mode_set_name(mode);
  9038. kfree(pipe_config);
  9039. return mode;
  9040. }
  9041. void intel_mark_busy(struct drm_device *dev)
  9042. {
  9043. struct drm_i915_private *dev_priv = dev->dev_private;
  9044. if (dev_priv->mm.busy)
  9045. return;
  9046. intel_runtime_pm_get(dev_priv);
  9047. i915_update_gfx_val(dev_priv);
  9048. if (INTEL_INFO(dev)->gen >= 6)
  9049. gen6_rps_busy(dev_priv);
  9050. dev_priv->mm.busy = true;
  9051. }
  9052. void intel_mark_idle(struct drm_device *dev)
  9053. {
  9054. struct drm_i915_private *dev_priv = dev->dev_private;
  9055. if (!dev_priv->mm.busy)
  9056. return;
  9057. dev_priv->mm.busy = false;
  9058. if (INTEL_INFO(dev)->gen >= 6)
  9059. gen6_rps_idle(dev->dev_private);
  9060. intel_runtime_pm_put(dev_priv);
  9061. }
  9062. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9063. {
  9064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9065. struct drm_device *dev = crtc->dev;
  9066. struct intel_unpin_work *work;
  9067. spin_lock_irq(&dev->event_lock);
  9068. work = intel_crtc->unpin_work;
  9069. intel_crtc->unpin_work = NULL;
  9070. spin_unlock_irq(&dev->event_lock);
  9071. if (work) {
  9072. cancel_work_sync(&work->work);
  9073. kfree(work);
  9074. }
  9075. drm_crtc_cleanup(crtc);
  9076. kfree(intel_crtc);
  9077. }
  9078. static void intel_unpin_work_fn(struct work_struct *__work)
  9079. {
  9080. struct intel_unpin_work *work =
  9081. container_of(__work, struct intel_unpin_work, work);
  9082. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9083. struct drm_device *dev = crtc->base.dev;
  9084. struct drm_plane *primary = crtc->base.primary;
  9085. mutex_lock(&dev->struct_mutex);
  9086. intel_unpin_fb_obj(work->old_fb, primary->state);
  9087. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9088. if (work->flip_queued_req)
  9089. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9090. mutex_unlock(&dev->struct_mutex);
  9091. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9092. intel_fbc_post_update(crtc);
  9093. drm_framebuffer_unreference(work->old_fb);
  9094. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9095. atomic_dec(&crtc->unpin_work_count);
  9096. kfree(work);
  9097. }
  9098. static void do_intel_finish_page_flip(struct drm_device *dev,
  9099. struct drm_crtc *crtc)
  9100. {
  9101. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9102. struct intel_unpin_work *work;
  9103. unsigned long flags;
  9104. /* Ignore early vblank irqs */
  9105. if (intel_crtc == NULL)
  9106. return;
  9107. /*
  9108. * This is called both by irq handlers and the reset code (to complete
  9109. * lost pageflips) so needs the full irqsave spinlocks.
  9110. */
  9111. spin_lock_irqsave(&dev->event_lock, flags);
  9112. work = intel_crtc->unpin_work;
  9113. /* Ensure we don't miss a work->pending update ... */
  9114. smp_rmb();
  9115. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9116. spin_unlock_irqrestore(&dev->event_lock, flags);
  9117. return;
  9118. }
  9119. page_flip_completed(intel_crtc);
  9120. spin_unlock_irqrestore(&dev->event_lock, flags);
  9121. }
  9122. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9123. {
  9124. struct drm_i915_private *dev_priv = dev->dev_private;
  9125. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9126. do_intel_finish_page_flip(dev, crtc);
  9127. }
  9128. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9129. {
  9130. struct drm_i915_private *dev_priv = dev->dev_private;
  9131. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9132. do_intel_finish_page_flip(dev, crtc);
  9133. }
  9134. /* Is 'a' after or equal to 'b'? */
  9135. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9136. {
  9137. return !((a - b) & 0x80000000);
  9138. }
  9139. static bool page_flip_finished(struct intel_crtc *crtc)
  9140. {
  9141. struct drm_device *dev = crtc->base.dev;
  9142. struct drm_i915_private *dev_priv = dev->dev_private;
  9143. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9144. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9145. return true;
  9146. /*
  9147. * The relevant registers doen't exist on pre-ctg.
  9148. * As the flip done interrupt doesn't trigger for mmio
  9149. * flips on gmch platforms, a flip count check isn't
  9150. * really needed there. But since ctg has the registers,
  9151. * include it in the check anyway.
  9152. */
  9153. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9154. return true;
  9155. /*
  9156. * BDW signals flip done immediately if the plane
  9157. * is disabled, even if the plane enable is already
  9158. * armed to occur at the next vblank :(
  9159. */
  9160. /*
  9161. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9162. * used the same base address. In that case the mmio flip might
  9163. * have completed, but the CS hasn't even executed the flip yet.
  9164. *
  9165. * A flip count check isn't enough as the CS might have updated
  9166. * the base address just after start of vblank, but before we
  9167. * managed to process the interrupt. This means we'd complete the
  9168. * CS flip too soon.
  9169. *
  9170. * Combining both checks should get us a good enough result. It may
  9171. * still happen that the CS flip has been executed, but has not
  9172. * yet actually completed. But in case the base address is the same
  9173. * anyway, we don't really care.
  9174. */
  9175. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9176. crtc->unpin_work->gtt_offset &&
  9177. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9178. crtc->unpin_work->flip_count);
  9179. }
  9180. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9181. {
  9182. struct drm_i915_private *dev_priv = dev->dev_private;
  9183. struct intel_crtc *intel_crtc =
  9184. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9185. unsigned long flags;
  9186. /*
  9187. * This is called both by irq handlers and the reset code (to complete
  9188. * lost pageflips) so needs the full irqsave spinlocks.
  9189. *
  9190. * NB: An MMIO update of the plane base pointer will also
  9191. * generate a page-flip completion irq, i.e. every modeset
  9192. * is also accompanied by a spurious intel_prepare_page_flip().
  9193. */
  9194. spin_lock_irqsave(&dev->event_lock, flags);
  9195. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9196. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9197. spin_unlock_irqrestore(&dev->event_lock, flags);
  9198. }
  9199. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9200. {
  9201. /* Ensure that the work item is consistent when activating it ... */
  9202. smp_wmb();
  9203. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9204. /* and that it is marked active as soon as the irq could fire. */
  9205. smp_wmb();
  9206. }
  9207. static int intel_gen2_queue_flip(struct drm_device *dev,
  9208. struct drm_crtc *crtc,
  9209. struct drm_framebuffer *fb,
  9210. struct drm_i915_gem_object *obj,
  9211. struct drm_i915_gem_request *req,
  9212. uint32_t flags)
  9213. {
  9214. struct intel_engine_cs *ring = req->ring;
  9215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9216. u32 flip_mask;
  9217. int ret;
  9218. ret = intel_ring_begin(req, 6);
  9219. if (ret)
  9220. return ret;
  9221. /* Can't queue multiple flips, so wait for the previous
  9222. * one to finish before executing the next.
  9223. */
  9224. if (intel_crtc->plane)
  9225. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9226. else
  9227. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9228. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9229. intel_ring_emit(ring, MI_NOOP);
  9230. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9231. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9232. intel_ring_emit(ring, fb->pitches[0]);
  9233. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9234. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9235. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9236. return 0;
  9237. }
  9238. static int intel_gen3_queue_flip(struct drm_device *dev,
  9239. struct drm_crtc *crtc,
  9240. struct drm_framebuffer *fb,
  9241. struct drm_i915_gem_object *obj,
  9242. struct drm_i915_gem_request *req,
  9243. uint32_t flags)
  9244. {
  9245. struct intel_engine_cs *ring = req->ring;
  9246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9247. u32 flip_mask;
  9248. int ret;
  9249. ret = intel_ring_begin(req, 6);
  9250. if (ret)
  9251. return ret;
  9252. if (intel_crtc->plane)
  9253. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9254. else
  9255. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9256. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9257. intel_ring_emit(ring, MI_NOOP);
  9258. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9259. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9260. intel_ring_emit(ring, fb->pitches[0]);
  9261. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9262. intel_ring_emit(ring, MI_NOOP);
  9263. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9264. return 0;
  9265. }
  9266. static int intel_gen4_queue_flip(struct drm_device *dev,
  9267. struct drm_crtc *crtc,
  9268. struct drm_framebuffer *fb,
  9269. struct drm_i915_gem_object *obj,
  9270. struct drm_i915_gem_request *req,
  9271. uint32_t flags)
  9272. {
  9273. struct intel_engine_cs *ring = req->ring;
  9274. struct drm_i915_private *dev_priv = dev->dev_private;
  9275. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9276. uint32_t pf, pipesrc;
  9277. int ret;
  9278. ret = intel_ring_begin(req, 4);
  9279. if (ret)
  9280. return ret;
  9281. /* i965+ uses the linear or tiled offsets from the
  9282. * Display Registers (which do not change across a page-flip)
  9283. * so we need only reprogram the base address.
  9284. */
  9285. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9286. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9287. intel_ring_emit(ring, fb->pitches[0]);
  9288. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9289. obj->tiling_mode);
  9290. /* XXX Enabling the panel-fitter across page-flip is so far
  9291. * untested on non-native modes, so ignore it for now.
  9292. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9293. */
  9294. pf = 0;
  9295. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9296. intel_ring_emit(ring, pf | pipesrc);
  9297. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9298. return 0;
  9299. }
  9300. static int intel_gen6_queue_flip(struct drm_device *dev,
  9301. struct drm_crtc *crtc,
  9302. struct drm_framebuffer *fb,
  9303. struct drm_i915_gem_object *obj,
  9304. struct drm_i915_gem_request *req,
  9305. uint32_t flags)
  9306. {
  9307. struct intel_engine_cs *ring = req->ring;
  9308. struct drm_i915_private *dev_priv = dev->dev_private;
  9309. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9310. uint32_t pf, pipesrc;
  9311. int ret;
  9312. ret = intel_ring_begin(req, 4);
  9313. if (ret)
  9314. return ret;
  9315. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9316. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9317. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9318. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9319. /* Contrary to the suggestions in the documentation,
  9320. * "Enable Panel Fitter" does not seem to be required when page
  9321. * flipping with a non-native mode, and worse causes a normal
  9322. * modeset to fail.
  9323. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9324. */
  9325. pf = 0;
  9326. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9327. intel_ring_emit(ring, pf | pipesrc);
  9328. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9329. return 0;
  9330. }
  9331. static int intel_gen7_queue_flip(struct drm_device *dev,
  9332. struct drm_crtc *crtc,
  9333. struct drm_framebuffer *fb,
  9334. struct drm_i915_gem_object *obj,
  9335. struct drm_i915_gem_request *req,
  9336. uint32_t flags)
  9337. {
  9338. struct intel_engine_cs *ring = req->ring;
  9339. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9340. uint32_t plane_bit = 0;
  9341. int len, ret;
  9342. switch (intel_crtc->plane) {
  9343. case PLANE_A:
  9344. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9345. break;
  9346. case PLANE_B:
  9347. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9348. break;
  9349. case PLANE_C:
  9350. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9351. break;
  9352. default:
  9353. WARN_ONCE(1, "unknown plane in flip command\n");
  9354. return -ENODEV;
  9355. }
  9356. len = 4;
  9357. if (ring->id == RCS) {
  9358. len += 6;
  9359. /*
  9360. * On Gen 8, SRM is now taking an extra dword to accommodate
  9361. * 48bits addresses, and we need a NOOP for the batch size to
  9362. * stay even.
  9363. */
  9364. if (IS_GEN8(dev))
  9365. len += 2;
  9366. }
  9367. /*
  9368. * BSpec MI_DISPLAY_FLIP for IVB:
  9369. * "The full packet must be contained within the same cache line."
  9370. *
  9371. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9372. * cacheline, if we ever start emitting more commands before
  9373. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9374. * then do the cacheline alignment, and finally emit the
  9375. * MI_DISPLAY_FLIP.
  9376. */
  9377. ret = intel_ring_cacheline_align(req);
  9378. if (ret)
  9379. return ret;
  9380. ret = intel_ring_begin(req, len);
  9381. if (ret)
  9382. return ret;
  9383. /* Unmask the flip-done completion message. Note that the bspec says that
  9384. * we should do this for both the BCS and RCS, and that we must not unmask
  9385. * more than one flip event at any time (or ensure that one flip message
  9386. * can be sent by waiting for flip-done prior to queueing new flips).
  9387. * Experimentation says that BCS works despite DERRMR masking all
  9388. * flip-done completion events and that unmasking all planes at once
  9389. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9390. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9391. */
  9392. if (ring->id == RCS) {
  9393. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9394. intel_ring_emit_reg(ring, DERRMR);
  9395. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9396. DERRMR_PIPEB_PRI_FLIP_DONE |
  9397. DERRMR_PIPEC_PRI_FLIP_DONE));
  9398. if (IS_GEN8(dev))
  9399. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9400. MI_SRM_LRM_GLOBAL_GTT);
  9401. else
  9402. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9403. MI_SRM_LRM_GLOBAL_GTT);
  9404. intel_ring_emit_reg(ring, DERRMR);
  9405. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9406. if (IS_GEN8(dev)) {
  9407. intel_ring_emit(ring, 0);
  9408. intel_ring_emit(ring, MI_NOOP);
  9409. }
  9410. }
  9411. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9412. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9413. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9414. intel_ring_emit(ring, (MI_NOOP));
  9415. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9416. return 0;
  9417. }
  9418. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9419. struct drm_i915_gem_object *obj)
  9420. {
  9421. /*
  9422. * This is not being used for older platforms, because
  9423. * non-availability of flip done interrupt forces us to use
  9424. * CS flips. Older platforms derive flip done using some clever
  9425. * tricks involving the flip_pending status bits and vblank irqs.
  9426. * So using MMIO flips there would disrupt this mechanism.
  9427. */
  9428. if (ring == NULL)
  9429. return true;
  9430. if (INTEL_INFO(ring->dev)->gen < 5)
  9431. return false;
  9432. if (i915.use_mmio_flip < 0)
  9433. return false;
  9434. else if (i915.use_mmio_flip > 0)
  9435. return true;
  9436. else if (i915.enable_execlists)
  9437. return true;
  9438. else if (obj->base.dma_buf &&
  9439. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9440. false))
  9441. return true;
  9442. else
  9443. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9444. }
  9445. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9446. unsigned int rotation,
  9447. struct intel_unpin_work *work)
  9448. {
  9449. struct drm_device *dev = intel_crtc->base.dev;
  9450. struct drm_i915_private *dev_priv = dev->dev_private;
  9451. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9452. const enum pipe pipe = intel_crtc->pipe;
  9453. u32 ctl, stride, tile_height;
  9454. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9455. ctl &= ~PLANE_CTL_TILED_MASK;
  9456. switch (fb->modifier[0]) {
  9457. case DRM_FORMAT_MOD_NONE:
  9458. break;
  9459. case I915_FORMAT_MOD_X_TILED:
  9460. ctl |= PLANE_CTL_TILED_X;
  9461. break;
  9462. case I915_FORMAT_MOD_Y_TILED:
  9463. ctl |= PLANE_CTL_TILED_Y;
  9464. break;
  9465. case I915_FORMAT_MOD_Yf_TILED:
  9466. ctl |= PLANE_CTL_TILED_YF;
  9467. break;
  9468. default:
  9469. MISSING_CASE(fb->modifier[0]);
  9470. }
  9471. /*
  9472. * The stride is either expressed as a multiple of 64 bytes chunks for
  9473. * linear buffers or in number of tiles for tiled buffers.
  9474. */
  9475. if (intel_rotation_90_or_270(rotation)) {
  9476. /* stride = Surface height in tiles */
  9477. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9478. stride = DIV_ROUND_UP(fb->height, tile_height);
  9479. } else {
  9480. stride = fb->pitches[0] /
  9481. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9482. fb->pixel_format);
  9483. }
  9484. /*
  9485. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9486. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9487. */
  9488. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9489. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9490. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9491. POSTING_READ(PLANE_SURF(pipe, 0));
  9492. }
  9493. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9494. struct intel_unpin_work *work)
  9495. {
  9496. struct drm_device *dev = intel_crtc->base.dev;
  9497. struct drm_i915_private *dev_priv = dev->dev_private;
  9498. struct intel_framebuffer *intel_fb =
  9499. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9500. struct drm_i915_gem_object *obj = intel_fb->obj;
  9501. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9502. u32 dspcntr;
  9503. dspcntr = I915_READ(reg);
  9504. if (obj->tiling_mode != I915_TILING_NONE)
  9505. dspcntr |= DISPPLANE_TILED;
  9506. else
  9507. dspcntr &= ~DISPPLANE_TILED;
  9508. I915_WRITE(reg, dspcntr);
  9509. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9510. POSTING_READ(DSPSURF(intel_crtc->plane));
  9511. }
  9512. /*
  9513. * XXX: This is the temporary way to update the plane registers until we get
  9514. * around to using the usual plane update functions for MMIO flips
  9515. */
  9516. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9517. {
  9518. struct intel_crtc *crtc = mmio_flip->crtc;
  9519. struct intel_unpin_work *work;
  9520. spin_lock_irq(&crtc->base.dev->event_lock);
  9521. work = crtc->unpin_work;
  9522. spin_unlock_irq(&crtc->base.dev->event_lock);
  9523. if (work == NULL)
  9524. return;
  9525. intel_mark_page_flip_active(work);
  9526. intel_pipe_update_start(crtc);
  9527. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9528. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9529. else
  9530. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9531. ilk_do_mmio_flip(crtc, work);
  9532. intel_pipe_update_end(crtc);
  9533. }
  9534. static void intel_mmio_flip_work_func(struct work_struct *work)
  9535. {
  9536. struct intel_mmio_flip *mmio_flip =
  9537. container_of(work, struct intel_mmio_flip, work);
  9538. struct intel_framebuffer *intel_fb =
  9539. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9540. struct drm_i915_gem_object *obj = intel_fb->obj;
  9541. if (mmio_flip->req) {
  9542. WARN_ON(__i915_wait_request(mmio_flip->req,
  9543. mmio_flip->crtc->reset_counter,
  9544. false, NULL,
  9545. &mmio_flip->i915->rps.mmioflips));
  9546. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9547. }
  9548. /* For framebuffer backed by dmabuf, wait for fence */
  9549. if (obj->base.dma_buf)
  9550. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9551. false, false,
  9552. MAX_SCHEDULE_TIMEOUT) < 0);
  9553. intel_do_mmio_flip(mmio_flip);
  9554. kfree(mmio_flip);
  9555. }
  9556. static int intel_queue_mmio_flip(struct drm_device *dev,
  9557. struct drm_crtc *crtc,
  9558. struct drm_i915_gem_object *obj)
  9559. {
  9560. struct intel_mmio_flip *mmio_flip;
  9561. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9562. if (mmio_flip == NULL)
  9563. return -ENOMEM;
  9564. mmio_flip->i915 = to_i915(dev);
  9565. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9566. mmio_flip->crtc = to_intel_crtc(crtc);
  9567. mmio_flip->rotation = crtc->primary->state->rotation;
  9568. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9569. schedule_work(&mmio_flip->work);
  9570. return 0;
  9571. }
  9572. static int intel_default_queue_flip(struct drm_device *dev,
  9573. struct drm_crtc *crtc,
  9574. struct drm_framebuffer *fb,
  9575. struct drm_i915_gem_object *obj,
  9576. struct drm_i915_gem_request *req,
  9577. uint32_t flags)
  9578. {
  9579. return -ENODEV;
  9580. }
  9581. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9582. struct drm_crtc *crtc)
  9583. {
  9584. struct drm_i915_private *dev_priv = dev->dev_private;
  9585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9586. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9587. u32 addr;
  9588. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9589. return true;
  9590. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9591. return false;
  9592. if (!work->enable_stall_check)
  9593. return false;
  9594. if (work->flip_ready_vblank == 0) {
  9595. if (work->flip_queued_req &&
  9596. !i915_gem_request_completed(work->flip_queued_req, true))
  9597. return false;
  9598. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9599. }
  9600. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9601. return false;
  9602. /* Potential stall - if we see that the flip has happened,
  9603. * assume a missed interrupt. */
  9604. if (INTEL_INFO(dev)->gen >= 4)
  9605. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9606. else
  9607. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9608. /* There is a potential issue here with a false positive after a flip
  9609. * to the same address. We could address this by checking for a
  9610. * non-incrementing frame counter.
  9611. */
  9612. return addr == work->gtt_offset;
  9613. }
  9614. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9615. {
  9616. struct drm_i915_private *dev_priv = dev->dev_private;
  9617. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9618. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9619. struct intel_unpin_work *work;
  9620. WARN_ON(!in_interrupt());
  9621. if (crtc == NULL)
  9622. return;
  9623. spin_lock(&dev->event_lock);
  9624. work = intel_crtc->unpin_work;
  9625. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9626. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9627. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9628. page_flip_completed(intel_crtc);
  9629. work = NULL;
  9630. }
  9631. if (work != NULL &&
  9632. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9633. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9634. spin_unlock(&dev->event_lock);
  9635. }
  9636. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9637. struct drm_framebuffer *fb,
  9638. struct drm_pending_vblank_event *event,
  9639. uint32_t page_flip_flags)
  9640. {
  9641. struct drm_device *dev = crtc->dev;
  9642. struct drm_i915_private *dev_priv = dev->dev_private;
  9643. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9644. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9645. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9646. struct drm_plane *primary = crtc->primary;
  9647. enum pipe pipe = intel_crtc->pipe;
  9648. struct intel_unpin_work *work;
  9649. struct intel_engine_cs *ring;
  9650. bool mmio_flip;
  9651. struct drm_i915_gem_request *request = NULL;
  9652. int ret;
  9653. /*
  9654. * drm_mode_page_flip_ioctl() should already catch this, but double
  9655. * check to be safe. In the future we may enable pageflipping from
  9656. * a disabled primary plane.
  9657. */
  9658. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9659. return -EBUSY;
  9660. /* Can't change pixel format via MI display flips. */
  9661. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9662. return -EINVAL;
  9663. /*
  9664. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9665. * Note that pitch changes could also affect these register.
  9666. */
  9667. if (INTEL_INFO(dev)->gen > 3 &&
  9668. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9669. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9670. return -EINVAL;
  9671. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9672. goto out_hang;
  9673. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9674. if (work == NULL)
  9675. return -ENOMEM;
  9676. work->event = event;
  9677. work->crtc = crtc;
  9678. work->old_fb = old_fb;
  9679. INIT_WORK(&work->work, intel_unpin_work_fn);
  9680. ret = drm_crtc_vblank_get(crtc);
  9681. if (ret)
  9682. goto free_work;
  9683. /* We borrow the event spin lock for protecting unpin_work */
  9684. spin_lock_irq(&dev->event_lock);
  9685. if (intel_crtc->unpin_work) {
  9686. /* Before declaring the flip queue wedged, check if
  9687. * the hardware completed the operation behind our backs.
  9688. */
  9689. if (__intel_pageflip_stall_check(dev, crtc)) {
  9690. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9691. page_flip_completed(intel_crtc);
  9692. } else {
  9693. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9694. spin_unlock_irq(&dev->event_lock);
  9695. drm_crtc_vblank_put(crtc);
  9696. kfree(work);
  9697. return -EBUSY;
  9698. }
  9699. }
  9700. intel_crtc->unpin_work = work;
  9701. spin_unlock_irq(&dev->event_lock);
  9702. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9703. flush_workqueue(dev_priv->wq);
  9704. /* Reference the objects for the scheduled work. */
  9705. drm_framebuffer_reference(work->old_fb);
  9706. drm_gem_object_reference(&obj->base);
  9707. crtc->primary->fb = fb;
  9708. update_state_fb(crtc->primary);
  9709. intel_fbc_pre_update(intel_crtc);
  9710. work->pending_flip_obj = obj;
  9711. ret = i915_mutex_lock_interruptible(dev);
  9712. if (ret)
  9713. goto cleanup;
  9714. atomic_inc(&intel_crtc->unpin_work_count);
  9715. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9716. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9717. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9718. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9719. ring = &dev_priv->ring[BCS];
  9720. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9721. /* vlv: DISPLAY_FLIP fails to change tiling */
  9722. ring = NULL;
  9723. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9724. ring = &dev_priv->ring[BCS];
  9725. } else if (INTEL_INFO(dev)->gen >= 7) {
  9726. ring = i915_gem_request_get_ring(obj->last_write_req);
  9727. if (ring == NULL || ring->id != RCS)
  9728. ring = &dev_priv->ring[BCS];
  9729. } else {
  9730. ring = &dev_priv->ring[RCS];
  9731. }
  9732. mmio_flip = use_mmio_flip(ring, obj);
  9733. /* When using CS flips, we want to emit semaphores between rings.
  9734. * However, when using mmio flips we will create a task to do the
  9735. * synchronisation, so all we want here is to pin the framebuffer
  9736. * into the display plane and skip any waits.
  9737. */
  9738. if (!mmio_flip) {
  9739. ret = i915_gem_object_sync(obj, ring, &request);
  9740. if (ret)
  9741. goto cleanup_pending;
  9742. }
  9743. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9744. crtc->primary->state);
  9745. if (ret)
  9746. goto cleanup_pending;
  9747. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9748. obj, 0);
  9749. work->gtt_offset += intel_crtc->dspaddr_offset;
  9750. if (mmio_flip) {
  9751. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9752. if (ret)
  9753. goto cleanup_unpin;
  9754. i915_gem_request_assign(&work->flip_queued_req,
  9755. obj->last_write_req);
  9756. } else {
  9757. if (!request) {
  9758. request = i915_gem_request_alloc(ring, NULL);
  9759. if (IS_ERR(request)) {
  9760. ret = PTR_ERR(request);
  9761. goto cleanup_unpin;
  9762. }
  9763. }
  9764. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9765. page_flip_flags);
  9766. if (ret)
  9767. goto cleanup_unpin;
  9768. i915_gem_request_assign(&work->flip_queued_req, request);
  9769. }
  9770. if (request)
  9771. i915_add_request_no_flush(request);
  9772. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9773. work->enable_stall_check = true;
  9774. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9775. to_intel_plane(primary)->frontbuffer_bit);
  9776. mutex_unlock(&dev->struct_mutex);
  9777. intel_frontbuffer_flip_prepare(dev,
  9778. to_intel_plane(primary)->frontbuffer_bit);
  9779. trace_i915_flip_request(intel_crtc->plane, obj);
  9780. return 0;
  9781. cleanup_unpin:
  9782. intel_unpin_fb_obj(fb, crtc->primary->state);
  9783. cleanup_pending:
  9784. if (!IS_ERR_OR_NULL(request))
  9785. i915_gem_request_cancel(request);
  9786. atomic_dec(&intel_crtc->unpin_work_count);
  9787. mutex_unlock(&dev->struct_mutex);
  9788. cleanup:
  9789. crtc->primary->fb = old_fb;
  9790. update_state_fb(crtc->primary);
  9791. drm_gem_object_unreference_unlocked(&obj->base);
  9792. drm_framebuffer_unreference(work->old_fb);
  9793. spin_lock_irq(&dev->event_lock);
  9794. intel_crtc->unpin_work = NULL;
  9795. spin_unlock_irq(&dev->event_lock);
  9796. drm_crtc_vblank_put(crtc);
  9797. free_work:
  9798. kfree(work);
  9799. if (ret == -EIO) {
  9800. struct drm_atomic_state *state;
  9801. struct drm_plane_state *plane_state;
  9802. out_hang:
  9803. state = drm_atomic_state_alloc(dev);
  9804. if (!state)
  9805. return -ENOMEM;
  9806. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9807. retry:
  9808. plane_state = drm_atomic_get_plane_state(state, primary);
  9809. ret = PTR_ERR_OR_ZERO(plane_state);
  9810. if (!ret) {
  9811. drm_atomic_set_fb_for_plane(plane_state, fb);
  9812. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9813. if (!ret)
  9814. ret = drm_atomic_commit(state);
  9815. }
  9816. if (ret == -EDEADLK) {
  9817. drm_modeset_backoff(state->acquire_ctx);
  9818. drm_atomic_state_clear(state);
  9819. goto retry;
  9820. }
  9821. if (ret)
  9822. drm_atomic_state_free(state);
  9823. if (ret == 0 && event) {
  9824. spin_lock_irq(&dev->event_lock);
  9825. drm_send_vblank_event(dev, pipe, event);
  9826. spin_unlock_irq(&dev->event_lock);
  9827. }
  9828. }
  9829. return ret;
  9830. }
  9831. /**
  9832. * intel_wm_need_update - Check whether watermarks need updating
  9833. * @plane: drm plane
  9834. * @state: new plane state
  9835. *
  9836. * Check current plane state versus the new one to determine whether
  9837. * watermarks need to be recalculated.
  9838. *
  9839. * Returns true or false.
  9840. */
  9841. static bool intel_wm_need_update(struct drm_plane *plane,
  9842. struct drm_plane_state *state)
  9843. {
  9844. struct intel_plane_state *new = to_intel_plane_state(state);
  9845. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9846. /* Update watermarks on tiling or size changes. */
  9847. if (new->visible != cur->visible)
  9848. return true;
  9849. if (!cur->base.fb || !new->base.fb)
  9850. return false;
  9851. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9852. cur->base.rotation != new->base.rotation ||
  9853. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9854. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9855. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9856. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9857. return true;
  9858. return false;
  9859. }
  9860. static bool needs_scaling(struct intel_plane_state *state)
  9861. {
  9862. int src_w = drm_rect_width(&state->src) >> 16;
  9863. int src_h = drm_rect_height(&state->src) >> 16;
  9864. int dst_w = drm_rect_width(&state->dst);
  9865. int dst_h = drm_rect_height(&state->dst);
  9866. return (src_w != dst_w || src_h != dst_h);
  9867. }
  9868. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9869. struct drm_plane_state *plane_state)
  9870. {
  9871. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9872. struct drm_crtc *crtc = crtc_state->crtc;
  9873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9874. struct drm_plane *plane = plane_state->plane;
  9875. struct drm_device *dev = crtc->dev;
  9876. struct intel_plane_state *old_plane_state =
  9877. to_intel_plane_state(plane->state);
  9878. int idx = intel_crtc->base.base.id, ret;
  9879. bool mode_changed = needs_modeset(crtc_state);
  9880. bool was_crtc_enabled = crtc->state->active;
  9881. bool is_crtc_enabled = crtc_state->active;
  9882. bool turn_off, turn_on, visible, was_visible;
  9883. struct drm_framebuffer *fb = plane_state->fb;
  9884. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9885. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9886. ret = skl_update_scaler_plane(
  9887. to_intel_crtc_state(crtc_state),
  9888. to_intel_plane_state(plane_state));
  9889. if (ret)
  9890. return ret;
  9891. }
  9892. was_visible = old_plane_state->visible;
  9893. visible = to_intel_plane_state(plane_state)->visible;
  9894. if (!was_crtc_enabled && WARN_ON(was_visible))
  9895. was_visible = false;
  9896. /*
  9897. * Visibility is calculated as if the crtc was on, but
  9898. * after scaler setup everything depends on it being off
  9899. * when the crtc isn't active.
  9900. */
  9901. if (!is_crtc_enabled)
  9902. to_intel_plane_state(plane_state)->visible = visible = false;
  9903. if (!was_visible && !visible)
  9904. return 0;
  9905. if (fb != old_plane_state->base.fb)
  9906. pipe_config->fb_changed = true;
  9907. turn_off = was_visible && (!visible || mode_changed);
  9908. turn_on = visible && (!was_visible || mode_changed);
  9909. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9910. plane->base.id, fb ? fb->base.id : -1);
  9911. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9912. plane->base.id, was_visible, visible,
  9913. turn_off, turn_on, mode_changed);
  9914. if (turn_on || turn_off) {
  9915. pipe_config->wm_changed = true;
  9916. /* must disable cxsr around plane enable/disable */
  9917. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9918. pipe_config->disable_cxsr = true;
  9919. } else if (intel_wm_need_update(plane, plane_state)) {
  9920. pipe_config->wm_changed = true;
  9921. }
  9922. if (visible || was_visible)
  9923. intel_crtc->atomic.fb_bits |=
  9924. to_intel_plane(plane)->frontbuffer_bit;
  9925. switch (plane->type) {
  9926. case DRM_PLANE_TYPE_PRIMARY:
  9927. intel_crtc->atomic.post_enable_primary = turn_on;
  9928. intel_crtc->atomic.update_fbc = true;
  9929. break;
  9930. case DRM_PLANE_TYPE_CURSOR:
  9931. break;
  9932. case DRM_PLANE_TYPE_OVERLAY:
  9933. /*
  9934. * WaCxSRDisabledForSpriteScaling:ivb
  9935. *
  9936. * cstate->update_wm was already set above, so this flag will
  9937. * take effect when we commit and program watermarks.
  9938. */
  9939. if (IS_IVYBRIDGE(dev) &&
  9940. needs_scaling(to_intel_plane_state(plane_state)) &&
  9941. !needs_scaling(old_plane_state))
  9942. pipe_config->disable_lp_wm = true;
  9943. break;
  9944. }
  9945. return 0;
  9946. }
  9947. static bool encoders_cloneable(const struct intel_encoder *a,
  9948. const struct intel_encoder *b)
  9949. {
  9950. /* masks could be asymmetric, so check both ways */
  9951. return a == b || (a->cloneable & (1 << b->type) &&
  9952. b->cloneable & (1 << a->type));
  9953. }
  9954. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9955. struct intel_crtc *crtc,
  9956. struct intel_encoder *encoder)
  9957. {
  9958. struct intel_encoder *source_encoder;
  9959. struct drm_connector *connector;
  9960. struct drm_connector_state *connector_state;
  9961. int i;
  9962. for_each_connector_in_state(state, connector, connector_state, i) {
  9963. if (connector_state->crtc != &crtc->base)
  9964. continue;
  9965. source_encoder =
  9966. to_intel_encoder(connector_state->best_encoder);
  9967. if (!encoders_cloneable(encoder, source_encoder))
  9968. return false;
  9969. }
  9970. return true;
  9971. }
  9972. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9973. struct intel_crtc *crtc)
  9974. {
  9975. struct intel_encoder *encoder;
  9976. struct drm_connector *connector;
  9977. struct drm_connector_state *connector_state;
  9978. int i;
  9979. for_each_connector_in_state(state, connector, connector_state, i) {
  9980. if (connector_state->crtc != &crtc->base)
  9981. continue;
  9982. encoder = to_intel_encoder(connector_state->best_encoder);
  9983. if (!check_single_encoder_cloning(state, crtc, encoder))
  9984. return false;
  9985. }
  9986. return true;
  9987. }
  9988. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9989. struct drm_crtc_state *crtc_state)
  9990. {
  9991. struct drm_device *dev = crtc->dev;
  9992. struct drm_i915_private *dev_priv = dev->dev_private;
  9993. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9994. struct intel_crtc_state *pipe_config =
  9995. to_intel_crtc_state(crtc_state);
  9996. struct drm_atomic_state *state = crtc_state->state;
  9997. int ret;
  9998. bool mode_changed = needs_modeset(crtc_state);
  9999. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10000. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10001. return -EINVAL;
  10002. }
  10003. if (mode_changed && !crtc_state->active)
  10004. pipe_config->wm_changed = true;
  10005. if (mode_changed && crtc_state->enable &&
  10006. dev_priv->display.crtc_compute_clock &&
  10007. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  10008. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10009. pipe_config);
  10010. if (ret)
  10011. return ret;
  10012. }
  10013. ret = 0;
  10014. if (dev_priv->display.compute_pipe_wm) {
  10015. ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
  10016. if (ret)
  10017. return ret;
  10018. }
  10019. if (INTEL_INFO(dev)->gen >= 9) {
  10020. if (mode_changed)
  10021. ret = skl_update_scaler_crtc(pipe_config);
  10022. if (!ret)
  10023. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10024. pipe_config);
  10025. }
  10026. return ret;
  10027. }
  10028. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10029. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10030. .load_lut = intel_crtc_load_lut,
  10031. .atomic_begin = intel_begin_crtc_commit,
  10032. .atomic_flush = intel_finish_crtc_commit,
  10033. .atomic_check = intel_crtc_atomic_check,
  10034. };
  10035. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10036. {
  10037. struct intel_connector *connector;
  10038. for_each_intel_connector(dev, connector) {
  10039. if (connector->base.encoder) {
  10040. connector->base.state->best_encoder =
  10041. connector->base.encoder;
  10042. connector->base.state->crtc =
  10043. connector->base.encoder->crtc;
  10044. } else {
  10045. connector->base.state->best_encoder = NULL;
  10046. connector->base.state->crtc = NULL;
  10047. }
  10048. }
  10049. }
  10050. static void
  10051. connected_sink_compute_bpp(struct intel_connector *connector,
  10052. struct intel_crtc_state *pipe_config)
  10053. {
  10054. int bpp = pipe_config->pipe_bpp;
  10055. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10056. connector->base.base.id,
  10057. connector->base.name);
  10058. /* Don't use an invalid EDID bpc value */
  10059. if (connector->base.display_info.bpc &&
  10060. connector->base.display_info.bpc * 3 < bpp) {
  10061. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10062. bpp, connector->base.display_info.bpc*3);
  10063. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10064. }
  10065. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10066. if (connector->base.display_info.bpc == 0) {
  10067. int type = connector->base.connector_type;
  10068. int clamp_bpp = 24;
  10069. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10070. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10071. type == DRM_MODE_CONNECTOR_eDP)
  10072. clamp_bpp = 18;
  10073. if (bpp > clamp_bpp) {
  10074. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10075. bpp, clamp_bpp);
  10076. pipe_config->pipe_bpp = clamp_bpp;
  10077. }
  10078. }
  10079. }
  10080. static int
  10081. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10082. struct intel_crtc_state *pipe_config)
  10083. {
  10084. struct drm_device *dev = crtc->base.dev;
  10085. struct drm_atomic_state *state;
  10086. struct drm_connector *connector;
  10087. struct drm_connector_state *connector_state;
  10088. int bpp, i;
  10089. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10090. bpp = 10*3;
  10091. else if (INTEL_INFO(dev)->gen >= 5)
  10092. bpp = 12*3;
  10093. else
  10094. bpp = 8*3;
  10095. pipe_config->pipe_bpp = bpp;
  10096. state = pipe_config->base.state;
  10097. /* Clamp display bpp to EDID value */
  10098. for_each_connector_in_state(state, connector, connector_state, i) {
  10099. if (connector_state->crtc != &crtc->base)
  10100. continue;
  10101. connected_sink_compute_bpp(to_intel_connector(connector),
  10102. pipe_config);
  10103. }
  10104. return bpp;
  10105. }
  10106. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10107. {
  10108. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10109. "type: 0x%x flags: 0x%x\n",
  10110. mode->crtc_clock,
  10111. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10112. mode->crtc_hsync_end, mode->crtc_htotal,
  10113. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10114. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10115. }
  10116. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10117. struct intel_crtc_state *pipe_config,
  10118. const char *context)
  10119. {
  10120. struct drm_device *dev = crtc->base.dev;
  10121. struct drm_plane *plane;
  10122. struct intel_plane *intel_plane;
  10123. struct intel_plane_state *state;
  10124. struct drm_framebuffer *fb;
  10125. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10126. context, pipe_config, pipe_name(crtc->pipe));
  10127. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10128. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10129. pipe_config->pipe_bpp, pipe_config->dither);
  10130. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10131. pipe_config->has_pch_encoder,
  10132. pipe_config->fdi_lanes,
  10133. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10134. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10135. pipe_config->fdi_m_n.tu);
  10136. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10137. pipe_config->has_dp_encoder,
  10138. pipe_config->lane_count,
  10139. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10140. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10141. pipe_config->dp_m_n.tu);
  10142. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10143. pipe_config->has_dp_encoder,
  10144. pipe_config->lane_count,
  10145. pipe_config->dp_m2_n2.gmch_m,
  10146. pipe_config->dp_m2_n2.gmch_n,
  10147. pipe_config->dp_m2_n2.link_m,
  10148. pipe_config->dp_m2_n2.link_n,
  10149. pipe_config->dp_m2_n2.tu);
  10150. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10151. pipe_config->has_audio,
  10152. pipe_config->has_infoframe);
  10153. DRM_DEBUG_KMS("requested mode:\n");
  10154. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10155. DRM_DEBUG_KMS("adjusted mode:\n");
  10156. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10157. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10158. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10159. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10160. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10161. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10162. crtc->num_scalers,
  10163. pipe_config->scaler_state.scaler_users,
  10164. pipe_config->scaler_state.scaler_id);
  10165. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10166. pipe_config->gmch_pfit.control,
  10167. pipe_config->gmch_pfit.pgm_ratios,
  10168. pipe_config->gmch_pfit.lvds_border_bits);
  10169. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10170. pipe_config->pch_pfit.pos,
  10171. pipe_config->pch_pfit.size,
  10172. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10173. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10174. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10175. if (IS_BROXTON(dev)) {
  10176. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10177. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10178. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10179. pipe_config->ddi_pll_sel,
  10180. pipe_config->dpll_hw_state.ebb0,
  10181. pipe_config->dpll_hw_state.ebb4,
  10182. pipe_config->dpll_hw_state.pll0,
  10183. pipe_config->dpll_hw_state.pll1,
  10184. pipe_config->dpll_hw_state.pll2,
  10185. pipe_config->dpll_hw_state.pll3,
  10186. pipe_config->dpll_hw_state.pll6,
  10187. pipe_config->dpll_hw_state.pll8,
  10188. pipe_config->dpll_hw_state.pll9,
  10189. pipe_config->dpll_hw_state.pll10,
  10190. pipe_config->dpll_hw_state.pcsdw12);
  10191. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10192. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10193. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10194. pipe_config->ddi_pll_sel,
  10195. pipe_config->dpll_hw_state.ctrl1,
  10196. pipe_config->dpll_hw_state.cfgcr1,
  10197. pipe_config->dpll_hw_state.cfgcr2);
  10198. } else if (HAS_DDI(dev)) {
  10199. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10200. pipe_config->ddi_pll_sel,
  10201. pipe_config->dpll_hw_state.wrpll,
  10202. pipe_config->dpll_hw_state.spll);
  10203. } else {
  10204. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10205. "fp0: 0x%x, fp1: 0x%x\n",
  10206. pipe_config->dpll_hw_state.dpll,
  10207. pipe_config->dpll_hw_state.dpll_md,
  10208. pipe_config->dpll_hw_state.fp0,
  10209. pipe_config->dpll_hw_state.fp1);
  10210. }
  10211. DRM_DEBUG_KMS("planes on this crtc\n");
  10212. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10213. intel_plane = to_intel_plane(plane);
  10214. if (intel_plane->pipe != crtc->pipe)
  10215. continue;
  10216. state = to_intel_plane_state(plane->state);
  10217. fb = state->base.fb;
  10218. if (!fb) {
  10219. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10220. "disabled, scaler_id = %d\n",
  10221. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10222. plane->base.id, intel_plane->pipe,
  10223. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10224. drm_plane_index(plane), state->scaler_id);
  10225. continue;
  10226. }
  10227. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10228. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10229. plane->base.id, intel_plane->pipe,
  10230. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10231. drm_plane_index(plane));
  10232. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10233. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10234. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10235. state->scaler_id,
  10236. state->src.x1 >> 16, state->src.y1 >> 16,
  10237. drm_rect_width(&state->src) >> 16,
  10238. drm_rect_height(&state->src) >> 16,
  10239. state->dst.x1, state->dst.y1,
  10240. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10241. }
  10242. }
  10243. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10244. {
  10245. struct drm_device *dev = state->dev;
  10246. struct drm_connector *connector;
  10247. unsigned int used_ports = 0;
  10248. /*
  10249. * Walk the connector list instead of the encoder
  10250. * list to detect the problem on ddi platforms
  10251. * where there's just one encoder per digital port.
  10252. */
  10253. drm_for_each_connector(connector, dev) {
  10254. struct drm_connector_state *connector_state;
  10255. struct intel_encoder *encoder;
  10256. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10257. if (!connector_state)
  10258. connector_state = connector->state;
  10259. if (!connector_state->best_encoder)
  10260. continue;
  10261. encoder = to_intel_encoder(connector_state->best_encoder);
  10262. WARN_ON(!connector_state->crtc);
  10263. switch (encoder->type) {
  10264. unsigned int port_mask;
  10265. case INTEL_OUTPUT_UNKNOWN:
  10266. if (WARN_ON(!HAS_DDI(dev)))
  10267. break;
  10268. case INTEL_OUTPUT_DISPLAYPORT:
  10269. case INTEL_OUTPUT_HDMI:
  10270. case INTEL_OUTPUT_EDP:
  10271. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10272. /* the same port mustn't appear more than once */
  10273. if (used_ports & port_mask)
  10274. return false;
  10275. used_ports |= port_mask;
  10276. default:
  10277. break;
  10278. }
  10279. }
  10280. return true;
  10281. }
  10282. static void
  10283. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10284. {
  10285. struct drm_crtc_state tmp_state;
  10286. struct intel_crtc_scaler_state scaler_state;
  10287. struct intel_dpll_hw_state dpll_hw_state;
  10288. enum intel_dpll_id shared_dpll;
  10289. uint32_t ddi_pll_sel;
  10290. bool force_thru;
  10291. /* FIXME: before the switch to atomic started, a new pipe_config was
  10292. * kzalloc'd. Code that depends on any field being zero should be
  10293. * fixed, so that the crtc_state can be safely duplicated. For now,
  10294. * only fields that are know to not cause problems are preserved. */
  10295. tmp_state = crtc_state->base;
  10296. scaler_state = crtc_state->scaler_state;
  10297. shared_dpll = crtc_state->shared_dpll;
  10298. dpll_hw_state = crtc_state->dpll_hw_state;
  10299. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10300. force_thru = crtc_state->pch_pfit.force_thru;
  10301. memset(crtc_state, 0, sizeof *crtc_state);
  10302. crtc_state->base = tmp_state;
  10303. crtc_state->scaler_state = scaler_state;
  10304. crtc_state->shared_dpll = shared_dpll;
  10305. crtc_state->dpll_hw_state = dpll_hw_state;
  10306. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10307. crtc_state->pch_pfit.force_thru = force_thru;
  10308. }
  10309. static int
  10310. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10311. struct intel_crtc_state *pipe_config)
  10312. {
  10313. struct drm_atomic_state *state = pipe_config->base.state;
  10314. struct intel_encoder *encoder;
  10315. struct drm_connector *connector;
  10316. struct drm_connector_state *connector_state;
  10317. int base_bpp, ret = -EINVAL;
  10318. int i;
  10319. bool retry = true;
  10320. clear_intel_crtc_state(pipe_config);
  10321. pipe_config->cpu_transcoder =
  10322. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10323. /*
  10324. * Sanitize sync polarity flags based on requested ones. If neither
  10325. * positive or negative polarity is requested, treat this as meaning
  10326. * negative polarity.
  10327. */
  10328. if (!(pipe_config->base.adjusted_mode.flags &
  10329. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10330. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10331. if (!(pipe_config->base.adjusted_mode.flags &
  10332. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10333. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10334. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10335. pipe_config);
  10336. if (base_bpp < 0)
  10337. goto fail;
  10338. /*
  10339. * Determine the real pipe dimensions. Note that stereo modes can
  10340. * increase the actual pipe size due to the frame doubling and
  10341. * insertion of additional space for blanks between the frame. This
  10342. * is stored in the crtc timings. We use the requested mode to do this
  10343. * computation to clearly distinguish it from the adjusted mode, which
  10344. * can be changed by the connectors in the below retry loop.
  10345. */
  10346. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10347. &pipe_config->pipe_src_w,
  10348. &pipe_config->pipe_src_h);
  10349. encoder_retry:
  10350. /* Ensure the port clock defaults are reset when retrying. */
  10351. pipe_config->port_clock = 0;
  10352. pipe_config->pixel_multiplier = 1;
  10353. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10354. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10355. CRTC_STEREO_DOUBLE);
  10356. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10357. * adjust it according to limitations or connector properties, and also
  10358. * a chance to reject the mode entirely.
  10359. */
  10360. for_each_connector_in_state(state, connector, connector_state, i) {
  10361. if (connector_state->crtc != crtc)
  10362. continue;
  10363. encoder = to_intel_encoder(connector_state->best_encoder);
  10364. if (!(encoder->compute_config(encoder, pipe_config))) {
  10365. DRM_DEBUG_KMS("Encoder config failure\n");
  10366. goto fail;
  10367. }
  10368. }
  10369. /* Set default port clock if not overwritten by the encoder. Needs to be
  10370. * done afterwards in case the encoder adjusts the mode. */
  10371. if (!pipe_config->port_clock)
  10372. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10373. * pipe_config->pixel_multiplier;
  10374. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10375. if (ret < 0) {
  10376. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10377. goto fail;
  10378. }
  10379. if (ret == RETRY) {
  10380. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10381. ret = -EINVAL;
  10382. goto fail;
  10383. }
  10384. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10385. retry = false;
  10386. goto encoder_retry;
  10387. }
  10388. /* Dithering seems to not pass-through bits correctly when it should, so
  10389. * only enable it on 6bpc panels. */
  10390. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10391. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10392. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10393. fail:
  10394. return ret;
  10395. }
  10396. static void
  10397. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10398. {
  10399. struct drm_crtc *crtc;
  10400. struct drm_crtc_state *crtc_state;
  10401. int i;
  10402. /* Double check state. */
  10403. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10404. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10405. /* Update hwmode for vblank functions */
  10406. if (crtc->state->active)
  10407. crtc->hwmode = crtc->state->adjusted_mode;
  10408. else
  10409. crtc->hwmode.crtc_clock = 0;
  10410. /*
  10411. * Update legacy state to satisfy fbc code. This can
  10412. * be removed when fbc uses the atomic state.
  10413. */
  10414. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10415. struct drm_plane_state *plane_state = crtc->primary->state;
  10416. crtc->primary->fb = plane_state->fb;
  10417. crtc->x = plane_state->src_x >> 16;
  10418. crtc->y = plane_state->src_y >> 16;
  10419. }
  10420. }
  10421. }
  10422. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10423. {
  10424. int diff;
  10425. if (clock1 == clock2)
  10426. return true;
  10427. if (!clock1 || !clock2)
  10428. return false;
  10429. diff = abs(clock1 - clock2);
  10430. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10431. return true;
  10432. return false;
  10433. }
  10434. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10435. list_for_each_entry((intel_crtc), \
  10436. &(dev)->mode_config.crtc_list, \
  10437. base.head) \
  10438. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10439. static bool
  10440. intel_compare_m_n(unsigned int m, unsigned int n,
  10441. unsigned int m2, unsigned int n2,
  10442. bool exact)
  10443. {
  10444. if (m == m2 && n == n2)
  10445. return true;
  10446. if (exact || !m || !n || !m2 || !n2)
  10447. return false;
  10448. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10449. if (n > n2) {
  10450. while (n > n2) {
  10451. m2 <<= 1;
  10452. n2 <<= 1;
  10453. }
  10454. } else if (n < n2) {
  10455. while (n < n2) {
  10456. m <<= 1;
  10457. n <<= 1;
  10458. }
  10459. }
  10460. if (n != n2)
  10461. return false;
  10462. return intel_fuzzy_clock_check(m, m2);
  10463. }
  10464. static bool
  10465. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10466. struct intel_link_m_n *m2_n2,
  10467. bool adjust)
  10468. {
  10469. if (m_n->tu == m2_n2->tu &&
  10470. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10471. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10472. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10473. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10474. if (adjust)
  10475. *m2_n2 = *m_n;
  10476. return true;
  10477. }
  10478. return false;
  10479. }
  10480. static bool
  10481. intel_pipe_config_compare(struct drm_device *dev,
  10482. struct intel_crtc_state *current_config,
  10483. struct intel_crtc_state *pipe_config,
  10484. bool adjust)
  10485. {
  10486. bool ret = true;
  10487. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10488. do { \
  10489. if (!adjust) \
  10490. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10491. else \
  10492. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10493. } while (0)
  10494. #define PIPE_CONF_CHECK_X(name) \
  10495. if (current_config->name != pipe_config->name) { \
  10496. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10497. "(expected 0x%08x, found 0x%08x)\n", \
  10498. current_config->name, \
  10499. pipe_config->name); \
  10500. ret = false; \
  10501. }
  10502. #define PIPE_CONF_CHECK_I(name) \
  10503. if (current_config->name != pipe_config->name) { \
  10504. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10505. "(expected %i, found %i)\n", \
  10506. current_config->name, \
  10507. pipe_config->name); \
  10508. ret = false; \
  10509. }
  10510. #define PIPE_CONF_CHECK_M_N(name) \
  10511. if (!intel_compare_link_m_n(&current_config->name, \
  10512. &pipe_config->name,\
  10513. adjust)) { \
  10514. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10515. "(expected tu %i gmch %i/%i link %i/%i, " \
  10516. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10517. current_config->name.tu, \
  10518. current_config->name.gmch_m, \
  10519. current_config->name.gmch_n, \
  10520. current_config->name.link_m, \
  10521. current_config->name.link_n, \
  10522. pipe_config->name.tu, \
  10523. pipe_config->name.gmch_m, \
  10524. pipe_config->name.gmch_n, \
  10525. pipe_config->name.link_m, \
  10526. pipe_config->name.link_n); \
  10527. ret = false; \
  10528. }
  10529. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10530. if (!intel_compare_link_m_n(&current_config->name, \
  10531. &pipe_config->name, adjust) && \
  10532. !intel_compare_link_m_n(&current_config->alt_name, \
  10533. &pipe_config->name, adjust)) { \
  10534. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10535. "(expected tu %i gmch %i/%i link %i/%i, " \
  10536. "or tu %i gmch %i/%i link %i/%i, " \
  10537. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10538. current_config->name.tu, \
  10539. current_config->name.gmch_m, \
  10540. current_config->name.gmch_n, \
  10541. current_config->name.link_m, \
  10542. current_config->name.link_n, \
  10543. current_config->alt_name.tu, \
  10544. current_config->alt_name.gmch_m, \
  10545. current_config->alt_name.gmch_n, \
  10546. current_config->alt_name.link_m, \
  10547. current_config->alt_name.link_n, \
  10548. pipe_config->name.tu, \
  10549. pipe_config->name.gmch_m, \
  10550. pipe_config->name.gmch_n, \
  10551. pipe_config->name.link_m, \
  10552. pipe_config->name.link_n); \
  10553. ret = false; \
  10554. }
  10555. /* This is required for BDW+ where there is only one set of registers for
  10556. * switching between high and low RR.
  10557. * This macro can be used whenever a comparison has to be made between one
  10558. * hw state and multiple sw state variables.
  10559. */
  10560. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10561. if ((current_config->name != pipe_config->name) && \
  10562. (current_config->alt_name != pipe_config->name)) { \
  10563. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10564. "(expected %i or %i, found %i)\n", \
  10565. current_config->name, \
  10566. current_config->alt_name, \
  10567. pipe_config->name); \
  10568. ret = false; \
  10569. }
  10570. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10571. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10572. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10573. "(expected %i, found %i)\n", \
  10574. current_config->name & (mask), \
  10575. pipe_config->name & (mask)); \
  10576. ret = false; \
  10577. }
  10578. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10579. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10580. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10581. "(expected %i, found %i)\n", \
  10582. current_config->name, \
  10583. pipe_config->name); \
  10584. ret = false; \
  10585. }
  10586. #define PIPE_CONF_QUIRK(quirk) \
  10587. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10588. PIPE_CONF_CHECK_I(cpu_transcoder);
  10589. PIPE_CONF_CHECK_I(has_pch_encoder);
  10590. PIPE_CONF_CHECK_I(fdi_lanes);
  10591. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10592. PIPE_CONF_CHECK_I(has_dp_encoder);
  10593. PIPE_CONF_CHECK_I(lane_count);
  10594. if (INTEL_INFO(dev)->gen < 8) {
  10595. PIPE_CONF_CHECK_M_N(dp_m_n);
  10596. if (current_config->has_drrs)
  10597. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10598. } else
  10599. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10600. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10601. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10602. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10603. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10604. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10605. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10606. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10607. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10608. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10609. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10610. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10611. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10612. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10613. PIPE_CONF_CHECK_I(pixel_multiplier);
  10614. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10615. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10616. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10617. PIPE_CONF_CHECK_I(limited_color_range);
  10618. PIPE_CONF_CHECK_I(has_infoframe);
  10619. PIPE_CONF_CHECK_I(has_audio);
  10620. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10621. DRM_MODE_FLAG_INTERLACE);
  10622. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10623. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10624. DRM_MODE_FLAG_PHSYNC);
  10625. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10626. DRM_MODE_FLAG_NHSYNC);
  10627. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10628. DRM_MODE_FLAG_PVSYNC);
  10629. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10630. DRM_MODE_FLAG_NVSYNC);
  10631. }
  10632. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10633. /* pfit ratios are autocomputed by the hw on gen4+ */
  10634. if (INTEL_INFO(dev)->gen < 4)
  10635. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10636. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10637. if (!adjust) {
  10638. PIPE_CONF_CHECK_I(pipe_src_w);
  10639. PIPE_CONF_CHECK_I(pipe_src_h);
  10640. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10641. if (current_config->pch_pfit.enabled) {
  10642. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10643. PIPE_CONF_CHECK_X(pch_pfit.size);
  10644. }
  10645. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10646. }
  10647. /* BDW+ don't expose a synchronous way to read the state */
  10648. if (IS_HASWELL(dev))
  10649. PIPE_CONF_CHECK_I(ips_enabled);
  10650. PIPE_CONF_CHECK_I(double_wide);
  10651. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10652. PIPE_CONF_CHECK_I(shared_dpll);
  10653. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10654. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10655. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10656. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10657. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10658. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10659. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10660. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10661. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10662. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10663. PIPE_CONF_CHECK_I(pipe_bpp);
  10664. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10665. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10666. #undef PIPE_CONF_CHECK_X
  10667. #undef PIPE_CONF_CHECK_I
  10668. #undef PIPE_CONF_CHECK_I_ALT
  10669. #undef PIPE_CONF_CHECK_FLAGS
  10670. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10671. #undef PIPE_CONF_QUIRK
  10672. #undef INTEL_ERR_OR_DBG_KMS
  10673. return ret;
  10674. }
  10675. static void check_wm_state(struct drm_device *dev)
  10676. {
  10677. struct drm_i915_private *dev_priv = dev->dev_private;
  10678. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10679. struct intel_crtc *intel_crtc;
  10680. int plane;
  10681. if (INTEL_INFO(dev)->gen < 9)
  10682. return;
  10683. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10684. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10685. for_each_intel_crtc(dev, intel_crtc) {
  10686. struct skl_ddb_entry *hw_entry, *sw_entry;
  10687. const enum pipe pipe = intel_crtc->pipe;
  10688. if (!intel_crtc->active)
  10689. continue;
  10690. /* planes */
  10691. for_each_plane(dev_priv, pipe, plane) {
  10692. hw_entry = &hw_ddb.plane[pipe][plane];
  10693. sw_entry = &sw_ddb->plane[pipe][plane];
  10694. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10695. continue;
  10696. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10697. "(expected (%u,%u), found (%u,%u))\n",
  10698. pipe_name(pipe), plane + 1,
  10699. sw_entry->start, sw_entry->end,
  10700. hw_entry->start, hw_entry->end);
  10701. }
  10702. /* cursor */
  10703. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10704. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10705. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10706. continue;
  10707. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10708. "(expected (%u,%u), found (%u,%u))\n",
  10709. pipe_name(pipe),
  10710. sw_entry->start, sw_entry->end,
  10711. hw_entry->start, hw_entry->end);
  10712. }
  10713. }
  10714. static void
  10715. check_connector_state(struct drm_device *dev,
  10716. struct drm_atomic_state *old_state)
  10717. {
  10718. struct drm_connector_state *old_conn_state;
  10719. struct drm_connector *connector;
  10720. int i;
  10721. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10722. struct drm_encoder *encoder = connector->encoder;
  10723. struct drm_connector_state *state = connector->state;
  10724. /* This also checks the encoder/connector hw state with the
  10725. * ->get_hw_state callbacks. */
  10726. intel_connector_check_state(to_intel_connector(connector));
  10727. I915_STATE_WARN(state->best_encoder != encoder,
  10728. "connector's atomic encoder doesn't match legacy encoder\n");
  10729. }
  10730. }
  10731. static void
  10732. check_encoder_state(struct drm_device *dev)
  10733. {
  10734. struct intel_encoder *encoder;
  10735. struct intel_connector *connector;
  10736. for_each_intel_encoder(dev, encoder) {
  10737. bool enabled = false;
  10738. enum pipe pipe;
  10739. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10740. encoder->base.base.id,
  10741. encoder->base.name);
  10742. for_each_intel_connector(dev, connector) {
  10743. if (connector->base.state->best_encoder != &encoder->base)
  10744. continue;
  10745. enabled = true;
  10746. I915_STATE_WARN(connector->base.state->crtc !=
  10747. encoder->base.crtc,
  10748. "connector's crtc doesn't match encoder crtc\n");
  10749. }
  10750. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10751. "encoder's enabled state mismatch "
  10752. "(expected %i, found %i)\n",
  10753. !!encoder->base.crtc, enabled);
  10754. if (!encoder->base.crtc) {
  10755. bool active;
  10756. active = encoder->get_hw_state(encoder, &pipe);
  10757. I915_STATE_WARN(active,
  10758. "encoder detached but still enabled on pipe %c.\n",
  10759. pipe_name(pipe));
  10760. }
  10761. }
  10762. }
  10763. static void
  10764. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10765. {
  10766. struct drm_i915_private *dev_priv = dev->dev_private;
  10767. struct intel_encoder *encoder;
  10768. struct drm_crtc_state *old_crtc_state;
  10769. struct drm_crtc *crtc;
  10770. int i;
  10771. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10772. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10773. struct intel_crtc_state *pipe_config, *sw_config;
  10774. bool active;
  10775. if (!needs_modeset(crtc->state) &&
  10776. !to_intel_crtc_state(crtc->state)->update_pipe)
  10777. continue;
  10778. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10779. pipe_config = to_intel_crtc_state(old_crtc_state);
  10780. memset(pipe_config, 0, sizeof(*pipe_config));
  10781. pipe_config->base.crtc = crtc;
  10782. pipe_config->base.state = old_state;
  10783. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10784. crtc->base.id);
  10785. active = dev_priv->display.get_pipe_config(intel_crtc,
  10786. pipe_config);
  10787. /* hw state is inconsistent with the pipe quirk */
  10788. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10789. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10790. active = crtc->state->active;
  10791. I915_STATE_WARN(crtc->state->active != active,
  10792. "crtc active state doesn't match with hw state "
  10793. "(expected %i, found %i)\n", crtc->state->active, active);
  10794. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10795. "transitional active state does not match atomic hw state "
  10796. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10797. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10798. enum pipe pipe;
  10799. active = encoder->get_hw_state(encoder, &pipe);
  10800. I915_STATE_WARN(active != crtc->state->active,
  10801. "[ENCODER:%i] active %i with crtc active %i\n",
  10802. encoder->base.base.id, active, crtc->state->active);
  10803. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10804. "Encoder connected to wrong pipe %c\n",
  10805. pipe_name(pipe));
  10806. if (active)
  10807. encoder->get_config(encoder, pipe_config);
  10808. }
  10809. if (!crtc->state->active)
  10810. continue;
  10811. sw_config = to_intel_crtc_state(crtc->state);
  10812. if (!intel_pipe_config_compare(dev, sw_config,
  10813. pipe_config, false)) {
  10814. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10815. intel_dump_pipe_config(intel_crtc, pipe_config,
  10816. "[hw state]");
  10817. intel_dump_pipe_config(intel_crtc, sw_config,
  10818. "[sw state]");
  10819. }
  10820. }
  10821. }
  10822. static void
  10823. check_shared_dpll_state(struct drm_device *dev)
  10824. {
  10825. struct drm_i915_private *dev_priv = dev->dev_private;
  10826. struct intel_crtc *crtc;
  10827. struct intel_dpll_hw_state dpll_hw_state;
  10828. int i;
  10829. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10830. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10831. int enabled_crtcs = 0, active_crtcs = 0;
  10832. bool active;
  10833. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10834. DRM_DEBUG_KMS("%s\n", pll->name);
  10835. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10836. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10837. "more active pll users than references: %i vs %i\n",
  10838. pll->active, hweight32(pll->config.crtc_mask));
  10839. I915_STATE_WARN(pll->active && !pll->on,
  10840. "pll in active use but not on in sw tracking\n");
  10841. I915_STATE_WARN(pll->on && !pll->active,
  10842. "pll in on but not on in use in sw tracking\n");
  10843. I915_STATE_WARN(pll->on != active,
  10844. "pll on state mismatch (expected %i, found %i)\n",
  10845. pll->on, active);
  10846. for_each_intel_crtc(dev, crtc) {
  10847. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10848. enabled_crtcs++;
  10849. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10850. active_crtcs++;
  10851. }
  10852. I915_STATE_WARN(pll->active != active_crtcs,
  10853. "pll active crtcs mismatch (expected %i, found %i)\n",
  10854. pll->active, active_crtcs);
  10855. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10856. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10857. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10858. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10859. sizeof(dpll_hw_state)),
  10860. "pll hw state mismatch\n");
  10861. }
  10862. }
  10863. static void
  10864. intel_modeset_check_state(struct drm_device *dev,
  10865. struct drm_atomic_state *old_state)
  10866. {
  10867. check_wm_state(dev);
  10868. check_connector_state(dev, old_state);
  10869. check_encoder_state(dev);
  10870. check_crtc_state(dev, old_state);
  10871. check_shared_dpll_state(dev);
  10872. }
  10873. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10874. int dotclock)
  10875. {
  10876. /*
  10877. * FDI already provided one idea for the dotclock.
  10878. * Yell if the encoder disagrees.
  10879. */
  10880. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10881. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10882. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10883. }
  10884. static void update_scanline_offset(struct intel_crtc *crtc)
  10885. {
  10886. struct drm_device *dev = crtc->base.dev;
  10887. /*
  10888. * The scanline counter increments at the leading edge of hsync.
  10889. *
  10890. * On most platforms it starts counting from vtotal-1 on the
  10891. * first active line. That means the scanline counter value is
  10892. * always one less than what we would expect. Ie. just after
  10893. * start of vblank, which also occurs at start of hsync (on the
  10894. * last active line), the scanline counter will read vblank_start-1.
  10895. *
  10896. * On gen2 the scanline counter starts counting from 1 instead
  10897. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10898. * to keep the value positive), instead of adding one.
  10899. *
  10900. * On HSW+ the behaviour of the scanline counter depends on the output
  10901. * type. For DP ports it behaves like most other platforms, but on HDMI
  10902. * there's an extra 1 line difference. So we need to add two instead of
  10903. * one to the value.
  10904. */
  10905. if (IS_GEN2(dev)) {
  10906. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10907. int vtotal;
  10908. vtotal = adjusted_mode->crtc_vtotal;
  10909. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10910. vtotal /= 2;
  10911. crtc->scanline_offset = vtotal - 1;
  10912. } else if (HAS_DDI(dev) &&
  10913. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10914. crtc->scanline_offset = 2;
  10915. } else
  10916. crtc->scanline_offset = 1;
  10917. }
  10918. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10919. {
  10920. struct drm_device *dev = state->dev;
  10921. struct drm_i915_private *dev_priv = to_i915(dev);
  10922. struct intel_shared_dpll_config *shared_dpll = NULL;
  10923. struct drm_crtc *crtc;
  10924. struct drm_crtc_state *crtc_state;
  10925. int i;
  10926. if (!dev_priv->display.crtc_compute_clock)
  10927. return;
  10928. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10929. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10930. int old_dpll = to_intel_crtc_state(crtc->state)->shared_dpll;
  10931. if (!needs_modeset(crtc_state))
  10932. continue;
  10933. to_intel_crtc_state(crtc_state)->shared_dpll = DPLL_ID_PRIVATE;
  10934. if (old_dpll == DPLL_ID_PRIVATE)
  10935. continue;
  10936. if (!shared_dpll)
  10937. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10938. shared_dpll[old_dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10939. }
  10940. }
  10941. /*
  10942. * This implements the workaround described in the "notes" section of the mode
  10943. * set sequence documentation. When going from no pipes or single pipe to
  10944. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10945. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10946. */
  10947. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10948. {
  10949. struct drm_crtc_state *crtc_state;
  10950. struct intel_crtc *intel_crtc;
  10951. struct drm_crtc *crtc;
  10952. struct intel_crtc_state *first_crtc_state = NULL;
  10953. struct intel_crtc_state *other_crtc_state = NULL;
  10954. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10955. int i;
  10956. /* look at all crtc's that are going to be enabled in during modeset */
  10957. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10958. intel_crtc = to_intel_crtc(crtc);
  10959. if (!crtc_state->active || !needs_modeset(crtc_state))
  10960. continue;
  10961. if (first_crtc_state) {
  10962. other_crtc_state = to_intel_crtc_state(crtc_state);
  10963. break;
  10964. } else {
  10965. first_crtc_state = to_intel_crtc_state(crtc_state);
  10966. first_pipe = intel_crtc->pipe;
  10967. }
  10968. }
  10969. /* No workaround needed? */
  10970. if (!first_crtc_state)
  10971. return 0;
  10972. /* w/a possibly needed, check how many crtc's are already enabled. */
  10973. for_each_intel_crtc(state->dev, intel_crtc) {
  10974. struct intel_crtc_state *pipe_config;
  10975. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10976. if (IS_ERR(pipe_config))
  10977. return PTR_ERR(pipe_config);
  10978. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10979. if (!pipe_config->base.active ||
  10980. needs_modeset(&pipe_config->base))
  10981. continue;
  10982. /* 2 or more enabled crtcs means no need for w/a */
  10983. if (enabled_pipe != INVALID_PIPE)
  10984. return 0;
  10985. enabled_pipe = intel_crtc->pipe;
  10986. }
  10987. if (enabled_pipe != INVALID_PIPE)
  10988. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10989. else if (other_crtc_state)
  10990. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10991. return 0;
  10992. }
  10993. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10994. {
  10995. struct drm_crtc *crtc;
  10996. struct drm_crtc_state *crtc_state;
  10997. int ret = 0;
  10998. /* add all active pipes to the state */
  10999. for_each_crtc(state->dev, crtc) {
  11000. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11001. if (IS_ERR(crtc_state))
  11002. return PTR_ERR(crtc_state);
  11003. if (!crtc_state->active || needs_modeset(crtc_state))
  11004. continue;
  11005. crtc_state->mode_changed = true;
  11006. ret = drm_atomic_add_affected_connectors(state, crtc);
  11007. if (ret)
  11008. break;
  11009. ret = drm_atomic_add_affected_planes(state, crtc);
  11010. if (ret)
  11011. break;
  11012. }
  11013. return ret;
  11014. }
  11015. static int intel_modeset_checks(struct drm_atomic_state *state)
  11016. {
  11017. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11018. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11019. struct drm_crtc *crtc;
  11020. struct drm_crtc_state *crtc_state;
  11021. int ret = 0, i;
  11022. if (!check_digital_port_conflicts(state)) {
  11023. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11024. return -EINVAL;
  11025. }
  11026. intel_state->modeset = true;
  11027. intel_state->active_crtcs = dev_priv->active_crtcs;
  11028. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11029. if (crtc_state->active)
  11030. intel_state->active_crtcs |= 1 << i;
  11031. else
  11032. intel_state->active_crtcs &= ~(1 << i);
  11033. }
  11034. /*
  11035. * See if the config requires any additional preparation, e.g.
  11036. * to adjust global state with pipes off. We need to do this
  11037. * here so we can get the modeset_pipe updated config for the new
  11038. * mode set on this crtc. For other crtcs we need to use the
  11039. * adjusted_mode bits in the crtc directly.
  11040. */
  11041. if (dev_priv->display.modeset_calc_cdclk) {
  11042. ret = dev_priv->display.modeset_calc_cdclk(state);
  11043. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11044. ret = intel_modeset_all_pipes(state);
  11045. if (ret < 0)
  11046. return ret;
  11047. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11048. intel_state->cdclk, intel_state->dev_cdclk);
  11049. } else
  11050. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11051. intel_modeset_clear_plls(state);
  11052. if (IS_HASWELL(dev_priv))
  11053. return haswell_mode_set_planes_workaround(state);
  11054. return 0;
  11055. }
  11056. /*
  11057. * Handle calculation of various watermark data at the end of the atomic check
  11058. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11059. * handlers to ensure that all derived state has been updated.
  11060. */
  11061. static void calc_watermark_data(struct drm_atomic_state *state)
  11062. {
  11063. struct drm_device *dev = state->dev;
  11064. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11065. struct drm_crtc *crtc;
  11066. struct drm_crtc_state *cstate;
  11067. struct drm_plane *plane;
  11068. struct drm_plane_state *pstate;
  11069. /*
  11070. * Calculate watermark configuration details now that derived
  11071. * plane/crtc state is all properly updated.
  11072. */
  11073. drm_for_each_crtc(crtc, dev) {
  11074. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  11075. crtc->state;
  11076. if (cstate->active)
  11077. intel_state->wm_config.num_pipes_active++;
  11078. }
  11079. drm_for_each_legacy_plane(plane, dev) {
  11080. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11081. plane->state;
  11082. if (!to_intel_plane_state(pstate)->visible)
  11083. continue;
  11084. intel_state->wm_config.sprites_enabled = true;
  11085. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11086. pstate->crtc_h != pstate->src_h >> 16)
  11087. intel_state->wm_config.sprites_scaled = true;
  11088. }
  11089. }
  11090. /**
  11091. * intel_atomic_check - validate state object
  11092. * @dev: drm device
  11093. * @state: state to validate
  11094. */
  11095. static int intel_atomic_check(struct drm_device *dev,
  11096. struct drm_atomic_state *state)
  11097. {
  11098. struct drm_i915_private *dev_priv = to_i915(dev);
  11099. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11100. struct drm_crtc *crtc;
  11101. struct drm_crtc_state *crtc_state;
  11102. int ret, i;
  11103. bool any_ms = false;
  11104. ret = drm_atomic_helper_check_modeset(dev, state);
  11105. if (ret)
  11106. return ret;
  11107. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11108. struct intel_crtc_state *pipe_config =
  11109. to_intel_crtc_state(crtc_state);
  11110. memset(&to_intel_crtc(crtc)->atomic, 0,
  11111. sizeof(struct intel_crtc_atomic_commit));
  11112. /* Catch I915_MODE_FLAG_INHERITED */
  11113. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11114. crtc_state->mode_changed = true;
  11115. if (!crtc_state->enable) {
  11116. if (needs_modeset(crtc_state))
  11117. any_ms = true;
  11118. continue;
  11119. }
  11120. if (!needs_modeset(crtc_state))
  11121. continue;
  11122. /* FIXME: For only active_changed we shouldn't need to do any
  11123. * state recomputation at all. */
  11124. ret = drm_atomic_add_affected_connectors(state, crtc);
  11125. if (ret)
  11126. return ret;
  11127. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11128. if (ret)
  11129. return ret;
  11130. if (i915.fastboot &&
  11131. intel_pipe_config_compare(dev,
  11132. to_intel_crtc_state(crtc->state),
  11133. pipe_config, true)) {
  11134. crtc_state->mode_changed = false;
  11135. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11136. }
  11137. if (needs_modeset(crtc_state)) {
  11138. any_ms = true;
  11139. ret = drm_atomic_add_affected_planes(state, crtc);
  11140. if (ret)
  11141. return ret;
  11142. }
  11143. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11144. needs_modeset(crtc_state) ?
  11145. "[modeset]" : "[fastset]");
  11146. }
  11147. if (any_ms) {
  11148. ret = intel_modeset_checks(state);
  11149. if (ret)
  11150. return ret;
  11151. } else
  11152. intel_state->cdclk = dev_priv->cdclk_freq;
  11153. ret = drm_atomic_helper_check_planes(dev, state);
  11154. if (ret)
  11155. return ret;
  11156. intel_fbc_choose_crtc(dev_priv, state);
  11157. calc_watermark_data(state);
  11158. return 0;
  11159. }
  11160. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11161. struct drm_atomic_state *state,
  11162. bool async)
  11163. {
  11164. struct drm_i915_private *dev_priv = dev->dev_private;
  11165. struct drm_plane_state *plane_state;
  11166. struct drm_crtc_state *crtc_state;
  11167. struct drm_plane *plane;
  11168. struct drm_crtc *crtc;
  11169. int i, ret;
  11170. if (async) {
  11171. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11172. return -EINVAL;
  11173. }
  11174. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11175. ret = intel_crtc_wait_for_pending_flips(crtc);
  11176. if (ret)
  11177. return ret;
  11178. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11179. flush_workqueue(dev_priv->wq);
  11180. }
  11181. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11182. if (ret)
  11183. return ret;
  11184. ret = drm_atomic_helper_prepare_planes(dev, state);
  11185. if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
  11186. u32 reset_counter;
  11187. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  11188. mutex_unlock(&dev->struct_mutex);
  11189. for_each_plane_in_state(state, plane, plane_state, i) {
  11190. struct intel_plane_state *intel_plane_state =
  11191. to_intel_plane_state(plane_state);
  11192. if (!intel_plane_state->wait_req)
  11193. continue;
  11194. ret = __i915_wait_request(intel_plane_state->wait_req,
  11195. reset_counter, true,
  11196. NULL, NULL);
  11197. /* Swallow -EIO errors to allow updates during hw lockup. */
  11198. if (ret == -EIO)
  11199. ret = 0;
  11200. if (ret)
  11201. break;
  11202. }
  11203. if (!ret)
  11204. return 0;
  11205. mutex_lock(&dev->struct_mutex);
  11206. drm_atomic_helper_cleanup_planes(dev, state);
  11207. }
  11208. mutex_unlock(&dev->struct_mutex);
  11209. return ret;
  11210. }
  11211. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11212. struct drm_i915_private *dev_priv,
  11213. unsigned crtc_mask)
  11214. {
  11215. unsigned last_vblank_count[I915_MAX_PIPES];
  11216. enum pipe pipe;
  11217. int ret;
  11218. if (!crtc_mask)
  11219. return;
  11220. for_each_pipe(dev_priv, pipe) {
  11221. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11222. if (!((1 << pipe) & crtc_mask))
  11223. continue;
  11224. ret = drm_crtc_vblank_get(crtc);
  11225. if (WARN_ON(ret != 0)) {
  11226. crtc_mask &= ~(1 << pipe);
  11227. continue;
  11228. }
  11229. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11230. }
  11231. for_each_pipe(dev_priv, pipe) {
  11232. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11233. long lret;
  11234. if (!((1 << pipe) & crtc_mask))
  11235. continue;
  11236. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11237. last_vblank_count[pipe] !=
  11238. drm_crtc_vblank_count(crtc),
  11239. msecs_to_jiffies(50));
  11240. WARN_ON(!lret);
  11241. drm_crtc_vblank_put(crtc);
  11242. }
  11243. }
  11244. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11245. {
  11246. /* fb updated, need to unpin old fb */
  11247. if (crtc_state->fb_changed)
  11248. return true;
  11249. /* wm changes, need vblank before final wm's */
  11250. if (crtc_state->wm_changed)
  11251. return true;
  11252. /*
  11253. * cxsr is re-enabled after vblank.
  11254. * This is already handled by crtc_state->wm_changed,
  11255. * but added for clarity.
  11256. */
  11257. if (crtc_state->disable_cxsr)
  11258. return true;
  11259. return false;
  11260. }
  11261. /**
  11262. * intel_atomic_commit - commit validated state object
  11263. * @dev: DRM device
  11264. * @state: the top-level driver state object
  11265. * @async: asynchronous commit
  11266. *
  11267. * This function commits a top-level state object that has been validated
  11268. * with drm_atomic_helper_check().
  11269. *
  11270. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11271. * we can only handle plane-related operations and do not yet support
  11272. * asynchronous commit.
  11273. *
  11274. * RETURNS
  11275. * Zero for success or -errno.
  11276. */
  11277. static int intel_atomic_commit(struct drm_device *dev,
  11278. struct drm_atomic_state *state,
  11279. bool async)
  11280. {
  11281. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11282. struct drm_i915_private *dev_priv = dev->dev_private;
  11283. struct drm_crtc_state *crtc_state;
  11284. struct drm_crtc *crtc;
  11285. int ret = 0, i;
  11286. bool hw_check = intel_state->modeset;
  11287. unsigned long put_domains[I915_MAX_PIPES] = {};
  11288. unsigned crtc_vblank_mask = 0;
  11289. ret = intel_atomic_prepare_commit(dev, state, async);
  11290. if (ret) {
  11291. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11292. return ret;
  11293. }
  11294. drm_atomic_helper_swap_state(dev, state);
  11295. dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
  11296. if (intel_state->modeset) {
  11297. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11298. sizeof(intel_state->min_pixclk));
  11299. dev_priv->active_crtcs = intel_state->active_crtcs;
  11300. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11301. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11302. }
  11303. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11304. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11305. if (needs_modeset(crtc->state) ||
  11306. to_intel_crtc_state(crtc->state)->update_pipe) {
  11307. hw_check = true;
  11308. put_domains[to_intel_crtc(crtc)->pipe] =
  11309. modeset_get_crtc_power_domains(crtc,
  11310. to_intel_crtc_state(crtc->state));
  11311. }
  11312. if (!needs_modeset(crtc->state))
  11313. continue;
  11314. intel_pre_plane_update(to_intel_crtc_state(crtc_state));
  11315. if (crtc_state->active) {
  11316. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11317. dev_priv->display.crtc_disable(crtc);
  11318. intel_crtc->active = false;
  11319. intel_fbc_disable(intel_crtc);
  11320. intel_disable_shared_dpll(intel_crtc);
  11321. /*
  11322. * Underruns don't always raise
  11323. * interrupts, so check manually.
  11324. */
  11325. intel_check_cpu_fifo_underruns(dev_priv);
  11326. intel_check_pch_fifo_underruns(dev_priv);
  11327. if (!crtc->state->active)
  11328. intel_update_watermarks(crtc);
  11329. }
  11330. }
  11331. /* Only after disabling all output pipelines that will be changed can we
  11332. * update the the output configuration. */
  11333. intel_modeset_update_crtc_state(state);
  11334. if (intel_state->modeset) {
  11335. intel_shared_dpll_commit(state);
  11336. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11337. if (dev_priv->display.modeset_commit_cdclk &&
  11338. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11339. dev_priv->display.modeset_commit_cdclk(state);
  11340. }
  11341. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11342. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11343. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11344. bool modeset = needs_modeset(crtc->state);
  11345. struct intel_crtc_state *pipe_config =
  11346. to_intel_crtc_state(crtc->state);
  11347. bool update_pipe = !modeset && pipe_config->update_pipe;
  11348. if (modeset && crtc->state->active) {
  11349. update_scanline_offset(to_intel_crtc(crtc));
  11350. dev_priv->display.crtc_enable(crtc);
  11351. }
  11352. if (!modeset)
  11353. intel_pre_plane_update(to_intel_crtc_state(crtc_state));
  11354. if (crtc->state->active && intel_crtc->atomic.update_fbc)
  11355. intel_fbc_enable(intel_crtc);
  11356. if (crtc->state->active &&
  11357. (crtc->state->planes_changed || update_pipe))
  11358. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11359. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11360. crtc_vblank_mask |= 1 << i;
  11361. }
  11362. /* FIXME: add subpixel order */
  11363. if (!state->legacy_cursor_update)
  11364. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11365. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11366. intel_post_plane_update(to_intel_crtc(crtc));
  11367. if (put_domains[i])
  11368. modeset_put_power_domains(dev_priv, put_domains[i]);
  11369. }
  11370. if (intel_state->modeset)
  11371. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11372. mutex_lock(&dev->struct_mutex);
  11373. drm_atomic_helper_cleanup_planes(dev, state);
  11374. mutex_unlock(&dev->struct_mutex);
  11375. if (hw_check)
  11376. intel_modeset_check_state(dev, state);
  11377. drm_atomic_state_free(state);
  11378. /* As one of the primary mmio accessors, KMS has a high likelihood
  11379. * of triggering bugs in unclaimed access. After we finish
  11380. * modesetting, see if an error has been flagged, and if so
  11381. * enable debugging for the next modeset - and hope we catch
  11382. * the culprit.
  11383. *
  11384. * XXX note that we assume display power is on at this point.
  11385. * This might hold true now but we need to add pm helper to check
  11386. * unclaimed only when the hardware is on, as atomic commits
  11387. * can happen also when the device is completely off.
  11388. */
  11389. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11390. return 0;
  11391. }
  11392. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11393. {
  11394. struct drm_device *dev = crtc->dev;
  11395. struct drm_atomic_state *state;
  11396. struct drm_crtc_state *crtc_state;
  11397. int ret;
  11398. state = drm_atomic_state_alloc(dev);
  11399. if (!state) {
  11400. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11401. crtc->base.id);
  11402. return;
  11403. }
  11404. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11405. retry:
  11406. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11407. ret = PTR_ERR_OR_ZERO(crtc_state);
  11408. if (!ret) {
  11409. if (!crtc_state->active)
  11410. goto out;
  11411. crtc_state->mode_changed = true;
  11412. ret = drm_atomic_commit(state);
  11413. }
  11414. if (ret == -EDEADLK) {
  11415. drm_atomic_state_clear(state);
  11416. drm_modeset_backoff(state->acquire_ctx);
  11417. goto retry;
  11418. }
  11419. if (ret)
  11420. out:
  11421. drm_atomic_state_free(state);
  11422. }
  11423. #undef for_each_intel_crtc_masked
  11424. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11425. .gamma_set = intel_crtc_gamma_set,
  11426. .set_config = drm_atomic_helper_set_config,
  11427. .destroy = intel_crtc_destroy,
  11428. .page_flip = intel_crtc_page_flip,
  11429. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11430. .atomic_destroy_state = intel_crtc_destroy_state,
  11431. };
  11432. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11433. struct intel_shared_dpll *pll,
  11434. struct intel_dpll_hw_state *hw_state)
  11435. {
  11436. uint32_t val;
  11437. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11438. return false;
  11439. val = I915_READ(PCH_DPLL(pll->id));
  11440. hw_state->dpll = val;
  11441. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11442. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11443. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  11444. return val & DPLL_VCO_ENABLE;
  11445. }
  11446. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11447. struct intel_shared_dpll *pll)
  11448. {
  11449. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11450. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11451. }
  11452. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11453. struct intel_shared_dpll *pll)
  11454. {
  11455. /* PCH refclock must be enabled first */
  11456. ibx_assert_pch_refclk_enabled(dev_priv);
  11457. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11458. /* Wait for the clocks to stabilize. */
  11459. POSTING_READ(PCH_DPLL(pll->id));
  11460. udelay(150);
  11461. /* The pixel multiplier can only be updated once the
  11462. * DPLL is enabled and the clocks are stable.
  11463. *
  11464. * So write it again.
  11465. */
  11466. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11467. POSTING_READ(PCH_DPLL(pll->id));
  11468. udelay(200);
  11469. }
  11470. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11471. struct intel_shared_dpll *pll)
  11472. {
  11473. struct drm_device *dev = dev_priv->dev;
  11474. struct intel_crtc *crtc;
  11475. /* Make sure no transcoder isn't still depending on us. */
  11476. for_each_intel_crtc(dev, crtc) {
  11477. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11478. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11479. }
  11480. I915_WRITE(PCH_DPLL(pll->id), 0);
  11481. POSTING_READ(PCH_DPLL(pll->id));
  11482. udelay(200);
  11483. }
  11484. static char *ibx_pch_dpll_names[] = {
  11485. "PCH DPLL A",
  11486. "PCH DPLL B",
  11487. };
  11488. static void ibx_pch_dpll_init(struct drm_device *dev)
  11489. {
  11490. struct drm_i915_private *dev_priv = dev->dev_private;
  11491. int i;
  11492. dev_priv->num_shared_dpll = 2;
  11493. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11494. dev_priv->shared_dplls[i].id = i;
  11495. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11496. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11497. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11498. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11499. dev_priv->shared_dplls[i].get_hw_state =
  11500. ibx_pch_dpll_get_hw_state;
  11501. }
  11502. }
  11503. static void intel_shared_dpll_init(struct drm_device *dev)
  11504. {
  11505. struct drm_i915_private *dev_priv = dev->dev_private;
  11506. if (HAS_DDI(dev))
  11507. intel_ddi_pll_init(dev);
  11508. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11509. ibx_pch_dpll_init(dev);
  11510. else
  11511. dev_priv->num_shared_dpll = 0;
  11512. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11513. }
  11514. /**
  11515. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11516. * @plane: drm plane to prepare for
  11517. * @fb: framebuffer to prepare for presentation
  11518. *
  11519. * Prepares a framebuffer for usage on a display plane. Generally this
  11520. * involves pinning the underlying object and updating the frontbuffer tracking
  11521. * bits. Some older platforms need special physical address handling for
  11522. * cursor planes.
  11523. *
  11524. * Must be called with struct_mutex held.
  11525. *
  11526. * Returns 0 on success, negative error code on failure.
  11527. */
  11528. int
  11529. intel_prepare_plane_fb(struct drm_plane *plane,
  11530. const struct drm_plane_state *new_state)
  11531. {
  11532. struct drm_device *dev = plane->dev;
  11533. struct drm_framebuffer *fb = new_state->fb;
  11534. struct intel_plane *intel_plane = to_intel_plane(plane);
  11535. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11536. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11537. int ret = 0;
  11538. if (!obj && !old_obj)
  11539. return 0;
  11540. if (old_obj) {
  11541. struct drm_crtc_state *crtc_state =
  11542. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11543. /* Big Hammer, we also need to ensure that any pending
  11544. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11545. * current scanout is retired before unpinning the old
  11546. * framebuffer. Note that we rely on userspace rendering
  11547. * into the buffer attached to the pipe they are waiting
  11548. * on. If not, userspace generates a GPU hang with IPEHR
  11549. * point to the MI_WAIT_FOR_EVENT.
  11550. *
  11551. * This should only fail upon a hung GPU, in which case we
  11552. * can safely continue.
  11553. */
  11554. if (needs_modeset(crtc_state))
  11555. ret = i915_gem_object_wait_rendering(old_obj, true);
  11556. /* Swallow -EIO errors to allow updates during hw lockup. */
  11557. if (ret && ret != -EIO)
  11558. return ret;
  11559. }
  11560. /* For framebuffer backed by dmabuf, wait for fence */
  11561. if (obj && obj->base.dma_buf) {
  11562. long lret;
  11563. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11564. false, true,
  11565. MAX_SCHEDULE_TIMEOUT);
  11566. if (lret == -ERESTARTSYS)
  11567. return lret;
  11568. WARN(lret < 0, "waiting returns %li\n", lret);
  11569. }
  11570. if (!obj) {
  11571. ret = 0;
  11572. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11573. INTEL_INFO(dev)->cursor_needs_physical) {
  11574. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11575. ret = i915_gem_object_attach_phys(obj, align);
  11576. if (ret)
  11577. DRM_DEBUG_KMS("failed to attach phys object\n");
  11578. } else {
  11579. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
  11580. }
  11581. if (ret == 0) {
  11582. if (obj) {
  11583. struct intel_plane_state *plane_state =
  11584. to_intel_plane_state(new_state);
  11585. i915_gem_request_assign(&plane_state->wait_req,
  11586. obj->last_write_req);
  11587. }
  11588. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11589. }
  11590. return ret;
  11591. }
  11592. /**
  11593. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11594. * @plane: drm plane to clean up for
  11595. * @fb: old framebuffer that was on plane
  11596. *
  11597. * Cleans up a framebuffer that has just been removed from a plane.
  11598. *
  11599. * Must be called with struct_mutex held.
  11600. */
  11601. void
  11602. intel_cleanup_plane_fb(struct drm_plane *plane,
  11603. const struct drm_plane_state *old_state)
  11604. {
  11605. struct drm_device *dev = plane->dev;
  11606. struct intel_plane *intel_plane = to_intel_plane(plane);
  11607. struct intel_plane_state *old_intel_state;
  11608. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11609. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11610. old_intel_state = to_intel_plane_state(old_state);
  11611. if (!obj && !old_obj)
  11612. return;
  11613. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11614. !INTEL_INFO(dev)->cursor_needs_physical))
  11615. intel_unpin_fb_obj(old_state->fb, old_state);
  11616. /* prepare_fb aborted? */
  11617. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11618. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11619. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11620. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11621. }
  11622. int
  11623. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11624. {
  11625. int max_scale;
  11626. struct drm_device *dev;
  11627. struct drm_i915_private *dev_priv;
  11628. int crtc_clock, cdclk;
  11629. if (!intel_crtc || !crtc_state->base.enable)
  11630. return DRM_PLANE_HELPER_NO_SCALING;
  11631. dev = intel_crtc->base.dev;
  11632. dev_priv = dev->dev_private;
  11633. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11634. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11635. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11636. return DRM_PLANE_HELPER_NO_SCALING;
  11637. /*
  11638. * skl max scale is lower of:
  11639. * close to 3 but not 3, -1 is for that purpose
  11640. * or
  11641. * cdclk/crtc_clock
  11642. */
  11643. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11644. return max_scale;
  11645. }
  11646. static int
  11647. intel_check_primary_plane(struct drm_plane *plane,
  11648. struct intel_crtc_state *crtc_state,
  11649. struct intel_plane_state *state)
  11650. {
  11651. struct drm_crtc *crtc = state->base.crtc;
  11652. struct drm_framebuffer *fb = state->base.fb;
  11653. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11654. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11655. bool can_position = false;
  11656. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11657. /* use scaler when colorkey is not required */
  11658. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11659. min_scale = 1;
  11660. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11661. }
  11662. can_position = true;
  11663. }
  11664. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11665. &state->dst, &state->clip,
  11666. min_scale, max_scale,
  11667. can_position, true,
  11668. &state->visible);
  11669. }
  11670. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11671. struct drm_crtc_state *old_crtc_state)
  11672. {
  11673. struct drm_device *dev = crtc->dev;
  11674. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11675. struct intel_crtc_state *old_intel_state =
  11676. to_intel_crtc_state(old_crtc_state);
  11677. bool modeset = needs_modeset(crtc->state);
  11678. /* Perform vblank evasion around commit operation */
  11679. intel_pipe_update_start(intel_crtc);
  11680. if (modeset)
  11681. return;
  11682. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11683. intel_update_pipe_config(intel_crtc, old_intel_state);
  11684. else if (INTEL_INFO(dev)->gen >= 9)
  11685. skl_detach_scalers(intel_crtc);
  11686. }
  11687. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11688. struct drm_crtc_state *old_crtc_state)
  11689. {
  11690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11691. intel_pipe_update_end(intel_crtc);
  11692. }
  11693. /**
  11694. * intel_plane_destroy - destroy a plane
  11695. * @plane: plane to destroy
  11696. *
  11697. * Common destruction function for all types of planes (primary, cursor,
  11698. * sprite).
  11699. */
  11700. void intel_plane_destroy(struct drm_plane *plane)
  11701. {
  11702. struct intel_plane *intel_plane = to_intel_plane(plane);
  11703. drm_plane_cleanup(plane);
  11704. kfree(intel_plane);
  11705. }
  11706. const struct drm_plane_funcs intel_plane_funcs = {
  11707. .update_plane = drm_atomic_helper_update_plane,
  11708. .disable_plane = drm_atomic_helper_disable_plane,
  11709. .destroy = intel_plane_destroy,
  11710. .set_property = drm_atomic_helper_plane_set_property,
  11711. .atomic_get_property = intel_plane_atomic_get_property,
  11712. .atomic_set_property = intel_plane_atomic_set_property,
  11713. .atomic_duplicate_state = intel_plane_duplicate_state,
  11714. .atomic_destroy_state = intel_plane_destroy_state,
  11715. };
  11716. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11717. int pipe)
  11718. {
  11719. struct intel_plane *primary;
  11720. struct intel_plane_state *state;
  11721. const uint32_t *intel_primary_formats;
  11722. unsigned int num_formats;
  11723. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11724. if (primary == NULL)
  11725. return NULL;
  11726. state = intel_create_plane_state(&primary->base);
  11727. if (!state) {
  11728. kfree(primary);
  11729. return NULL;
  11730. }
  11731. primary->base.state = &state->base;
  11732. primary->can_scale = false;
  11733. primary->max_downscale = 1;
  11734. if (INTEL_INFO(dev)->gen >= 9) {
  11735. primary->can_scale = true;
  11736. state->scaler_id = -1;
  11737. }
  11738. primary->pipe = pipe;
  11739. primary->plane = pipe;
  11740. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11741. primary->check_plane = intel_check_primary_plane;
  11742. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11743. primary->plane = !pipe;
  11744. if (INTEL_INFO(dev)->gen >= 9) {
  11745. intel_primary_formats = skl_primary_formats;
  11746. num_formats = ARRAY_SIZE(skl_primary_formats);
  11747. primary->update_plane = skylake_update_primary_plane;
  11748. primary->disable_plane = skylake_disable_primary_plane;
  11749. } else if (HAS_PCH_SPLIT(dev)) {
  11750. intel_primary_formats = i965_primary_formats;
  11751. num_formats = ARRAY_SIZE(i965_primary_formats);
  11752. primary->update_plane = ironlake_update_primary_plane;
  11753. primary->disable_plane = i9xx_disable_primary_plane;
  11754. } else if (INTEL_INFO(dev)->gen >= 4) {
  11755. intel_primary_formats = i965_primary_formats;
  11756. num_formats = ARRAY_SIZE(i965_primary_formats);
  11757. primary->update_plane = i9xx_update_primary_plane;
  11758. primary->disable_plane = i9xx_disable_primary_plane;
  11759. } else {
  11760. intel_primary_formats = i8xx_primary_formats;
  11761. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11762. primary->update_plane = i9xx_update_primary_plane;
  11763. primary->disable_plane = i9xx_disable_primary_plane;
  11764. }
  11765. drm_universal_plane_init(dev, &primary->base, 0,
  11766. &intel_plane_funcs,
  11767. intel_primary_formats, num_formats,
  11768. DRM_PLANE_TYPE_PRIMARY, NULL);
  11769. if (INTEL_INFO(dev)->gen >= 4)
  11770. intel_create_rotation_property(dev, primary);
  11771. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11772. return &primary->base;
  11773. }
  11774. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11775. {
  11776. if (!dev->mode_config.rotation_property) {
  11777. unsigned long flags = BIT(DRM_ROTATE_0) |
  11778. BIT(DRM_ROTATE_180);
  11779. if (INTEL_INFO(dev)->gen >= 9)
  11780. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11781. dev->mode_config.rotation_property =
  11782. drm_mode_create_rotation_property(dev, flags);
  11783. }
  11784. if (dev->mode_config.rotation_property)
  11785. drm_object_attach_property(&plane->base.base,
  11786. dev->mode_config.rotation_property,
  11787. plane->base.state->rotation);
  11788. }
  11789. static int
  11790. intel_check_cursor_plane(struct drm_plane *plane,
  11791. struct intel_crtc_state *crtc_state,
  11792. struct intel_plane_state *state)
  11793. {
  11794. struct drm_crtc *crtc = crtc_state->base.crtc;
  11795. struct drm_framebuffer *fb = state->base.fb;
  11796. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11797. enum pipe pipe = to_intel_plane(plane)->pipe;
  11798. unsigned stride;
  11799. int ret;
  11800. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11801. &state->dst, &state->clip,
  11802. DRM_PLANE_HELPER_NO_SCALING,
  11803. DRM_PLANE_HELPER_NO_SCALING,
  11804. true, true, &state->visible);
  11805. if (ret)
  11806. return ret;
  11807. /* if we want to turn off the cursor ignore width and height */
  11808. if (!obj)
  11809. return 0;
  11810. /* Check for which cursor types we support */
  11811. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11812. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11813. state->base.crtc_w, state->base.crtc_h);
  11814. return -EINVAL;
  11815. }
  11816. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11817. if (obj->base.size < stride * state->base.crtc_h) {
  11818. DRM_DEBUG_KMS("buffer is too small\n");
  11819. return -ENOMEM;
  11820. }
  11821. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11822. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11823. return -EINVAL;
  11824. }
  11825. /*
  11826. * There's something wrong with the cursor on CHV pipe C.
  11827. * If it straddles the left edge of the screen then
  11828. * moving it away from the edge or disabling it often
  11829. * results in a pipe underrun, and often that can lead to
  11830. * dead pipe (constant underrun reported, and it scans
  11831. * out just a solid color). To recover from that, the
  11832. * display power well must be turned off and on again.
  11833. * Refuse the put the cursor into that compromised position.
  11834. */
  11835. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11836. state->visible && state->base.crtc_x < 0) {
  11837. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11838. return -EINVAL;
  11839. }
  11840. return 0;
  11841. }
  11842. static void
  11843. intel_disable_cursor_plane(struct drm_plane *plane,
  11844. struct drm_crtc *crtc)
  11845. {
  11846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11847. intel_crtc->cursor_addr = 0;
  11848. intel_crtc_update_cursor(crtc, NULL);
  11849. }
  11850. static void
  11851. intel_update_cursor_plane(struct drm_plane *plane,
  11852. const struct intel_crtc_state *crtc_state,
  11853. const struct intel_plane_state *state)
  11854. {
  11855. struct drm_crtc *crtc = crtc_state->base.crtc;
  11856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11857. struct drm_device *dev = plane->dev;
  11858. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11859. uint32_t addr;
  11860. if (!obj)
  11861. addr = 0;
  11862. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11863. addr = i915_gem_obj_ggtt_offset(obj);
  11864. else
  11865. addr = obj->phys_handle->busaddr;
  11866. intel_crtc->cursor_addr = addr;
  11867. intel_crtc_update_cursor(crtc, state);
  11868. }
  11869. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11870. int pipe)
  11871. {
  11872. struct intel_plane *cursor;
  11873. struct intel_plane_state *state;
  11874. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11875. if (cursor == NULL)
  11876. return NULL;
  11877. state = intel_create_plane_state(&cursor->base);
  11878. if (!state) {
  11879. kfree(cursor);
  11880. return NULL;
  11881. }
  11882. cursor->base.state = &state->base;
  11883. cursor->can_scale = false;
  11884. cursor->max_downscale = 1;
  11885. cursor->pipe = pipe;
  11886. cursor->plane = pipe;
  11887. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11888. cursor->check_plane = intel_check_cursor_plane;
  11889. cursor->update_plane = intel_update_cursor_plane;
  11890. cursor->disable_plane = intel_disable_cursor_plane;
  11891. drm_universal_plane_init(dev, &cursor->base, 0,
  11892. &intel_plane_funcs,
  11893. intel_cursor_formats,
  11894. ARRAY_SIZE(intel_cursor_formats),
  11895. DRM_PLANE_TYPE_CURSOR, NULL);
  11896. if (INTEL_INFO(dev)->gen >= 4) {
  11897. if (!dev->mode_config.rotation_property)
  11898. dev->mode_config.rotation_property =
  11899. drm_mode_create_rotation_property(dev,
  11900. BIT(DRM_ROTATE_0) |
  11901. BIT(DRM_ROTATE_180));
  11902. if (dev->mode_config.rotation_property)
  11903. drm_object_attach_property(&cursor->base.base,
  11904. dev->mode_config.rotation_property,
  11905. state->base.rotation);
  11906. }
  11907. if (INTEL_INFO(dev)->gen >=9)
  11908. state->scaler_id = -1;
  11909. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11910. return &cursor->base;
  11911. }
  11912. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11913. struct intel_crtc_state *crtc_state)
  11914. {
  11915. int i;
  11916. struct intel_scaler *intel_scaler;
  11917. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11918. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11919. intel_scaler = &scaler_state->scalers[i];
  11920. intel_scaler->in_use = 0;
  11921. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11922. }
  11923. scaler_state->scaler_id = -1;
  11924. }
  11925. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11926. {
  11927. struct drm_i915_private *dev_priv = dev->dev_private;
  11928. struct intel_crtc *intel_crtc;
  11929. struct intel_crtc_state *crtc_state = NULL;
  11930. struct drm_plane *primary = NULL;
  11931. struct drm_plane *cursor = NULL;
  11932. int i, ret;
  11933. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11934. if (intel_crtc == NULL)
  11935. return;
  11936. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11937. if (!crtc_state)
  11938. goto fail;
  11939. intel_crtc->config = crtc_state;
  11940. intel_crtc->base.state = &crtc_state->base;
  11941. crtc_state->base.crtc = &intel_crtc->base;
  11942. /* initialize shared scalers */
  11943. if (INTEL_INFO(dev)->gen >= 9) {
  11944. if (pipe == PIPE_C)
  11945. intel_crtc->num_scalers = 1;
  11946. else
  11947. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11948. skl_init_scalers(dev, intel_crtc, crtc_state);
  11949. }
  11950. primary = intel_primary_plane_create(dev, pipe);
  11951. if (!primary)
  11952. goto fail;
  11953. cursor = intel_cursor_plane_create(dev, pipe);
  11954. if (!cursor)
  11955. goto fail;
  11956. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11957. cursor, &intel_crtc_funcs, NULL);
  11958. if (ret)
  11959. goto fail;
  11960. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11961. for (i = 0; i < 256; i++) {
  11962. intel_crtc->lut_r[i] = i;
  11963. intel_crtc->lut_g[i] = i;
  11964. intel_crtc->lut_b[i] = i;
  11965. }
  11966. /*
  11967. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11968. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11969. */
  11970. intel_crtc->pipe = pipe;
  11971. intel_crtc->plane = pipe;
  11972. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11973. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11974. intel_crtc->plane = !pipe;
  11975. }
  11976. intel_crtc->cursor_base = ~0;
  11977. intel_crtc->cursor_cntl = ~0;
  11978. intel_crtc->cursor_size = ~0;
  11979. intel_crtc->wm.cxsr_allowed = true;
  11980. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11981. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11982. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11983. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11984. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11985. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11986. return;
  11987. fail:
  11988. if (primary)
  11989. drm_plane_cleanup(primary);
  11990. if (cursor)
  11991. drm_plane_cleanup(cursor);
  11992. kfree(crtc_state);
  11993. kfree(intel_crtc);
  11994. }
  11995. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11996. {
  11997. struct drm_encoder *encoder = connector->base.encoder;
  11998. struct drm_device *dev = connector->base.dev;
  11999. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12000. if (!encoder || WARN_ON(!encoder->crtc))
  12001. return INVALID_PIPE;
  12002. return to_intel_crtc(encoder->crtc)->pipe;
  12003. }
  12004. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12005. struct drm_file *file)
  12006. {
  12007. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12008. struct drm_crtc *drmmode_crtc;
  12009. struct intel_crtc *crtc;
  12010. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12011. if (!drmmode_crtc) {
  12012. DRM_ERROR("no such CRTC id\n");
  12013. return -ENOENT;
  12014. }
  12015. crtc = to_intel_crtc(drmmode_crtc);
  12016. pipe_from_crtc_id->pipe = crtc->pipe;
  12017. return 0;
  12018. }
  12019. static int intel_encoder_clones(struct intel_encoder *encoder)
  12020. {
  12021. struct drm_device *dev = encoder->base.dev;
  12022. struct intel_encoder *source_encoder;
  12023. int index_mask = 0;
  12024. int entry = 0;
  12025. for_each_intel_encoder(dev, source_encoder) {
  12026. if (encoders_cloneable(encoder, source_encoder))
  12027. index_mask |= (1 << entry);
  12028. entry++;
  12029. }
  12030. return index_mask;
  12031. }
  12032. static bool has_edp_a(struct drm_device *dev)
  12033. {
  12034. struct drm_i915_private *dev_priv = dev->dev_private;
  12035. if (!IS_MOBILE(dev))
  12036. return false;
  12037. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12038. return false;
  12039. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12040. return false;
  12041. return true;
  12042. }
  12043. static bool intel_crt_present(struct drm_device *dev)
  12044. {
  12045. struct drm_i915_private *dev_priv = dev->dev_private;
  12046. if (INTEL_INFO(dev)->gen >= 9)
  12047. return false;
  12048. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12049. return false;
  12050. if (IS_CHERRYVIEW(dev))
  12051. return false;
  12052. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12053. return false;
  12054. /* DDI E can't be used if DDI A requires 4 lanes */
  12055. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12056. return false;
  12057. if (!dev_priv->vbt.int_crt_support)
  12058. return false;
  12059. return true;
  12060. }
  12061. static void intel_setup_outputs(struct drm_device *dev)
  12062. {
  12063. struct drm_i915_private *dev_priv = dev->dev_private;
  12064. struct intel_encoder *encoder;
  12065. bool dpd_is_edp = false;
  12066. intel_lvds_init(dev);
  12067. if (intel_crt_present(dev))
  12068. intel_crt_init(dev);
  12069. if (IS_BROXTON(dev)) {
  12070. /*
  12071. * FIXME: Broxton doesn't support port detection via the
  12072. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12073. * detect the ports.
  12074. */
  12075. intel_ddi_init(dev, PORT_A);
  12076. intel_ddi_init(dev, PORT_B);
  12077. intel_ddi_init(dev, PORT_C);
  12078. } else if (HAS_DDI(dev)) {
  12079. int found;
  12080. /*
  12081. * Haswell uses DDI functions to detect digital outputs.
  12082. * On SKL pre-D0 the strap isn't connected, so we assume
  12083. * it's there.
  12084. */
  12085. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12086. /* WaIgnoreDDIAStrap: skl */
  12087. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12088. intel_ddi_init(dev, PORT_A);
  12089. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12090. * register */
  12091. found = I915_READ(SFUSE_STRAP);
  12092. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12093. intel_ddi_init(dev, PORT_B);
  12094. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12095. intel_ddi_init(dev, PORT_C);
  12096. if (found & SFUSE_STRAP_DDID_DETECTED)
  12097. intel_ddi_init(dev, PORT_D);
  12098. /*
  12099. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12100. */
  12101. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12102. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12103. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12104. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12105. intel_ddi_init(dev, PORT_E);
  12106. } else if (HAS_PCH_SPLIT(dev)) {
  12107. int found;
  12108. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12109. if (has_edp_a(dev))
  12110. intel_dp_init(dev, DP_A, PORT_A);
  12111. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12112. /* PCH SDVOB multiplex with HDMIB */
  12113. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12114. if (!found)
  12115. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12116. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12117. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12118. }
  12119. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12120. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12121. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12122. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12123. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12124. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12125. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12126. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12127. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12128. /*
  12129. * The DP_DETECTED bit is the latched state of the DDC
  12130. * SDA pin at boot. However since eDP doesn't require DDC
  12131. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12132. * eDP ports may have been muxed to an alternate function.
  12133. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12134. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12135. * detect eDP ports.
  12136. */
  12137. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12138. !intel_dp_is_edp(dev, PORT_B))
  12139. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12140. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12141. intel_dp_is_edp(dev, PORT_B))
  12142. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12143. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12144. !intel_dp_is_edp(dev, PORT_C))
  12145. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12146. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12147. intel_dp_is_edp(dev, PORT_C))
  12148. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12149. if (IS_CHERRYVIEW(dev)) {
  12150. /* eDP not supported on port D, so don't check VBT */
  12151. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12152. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12153. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12154. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12155. }
  12156. intel_dsi_init(dev);
  12157. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12158. bool found = false;
  12159. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12160. DRM_DEBUG_KMS("probing SDVOB\n");
  12161. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12162. if (!found && IS_G4X(dev)) {
  12163. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12164. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12165. }
  12166. if (!found && IS_G4X(dev))
  12167. intel_dp_init(dev, DP_B, PORT_B);
  12168. }
  12169. /* Before G4X SDVOC doesn't have its own detect register */
  12170. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12171. DRM_DEBUG_KMS("probing SDVOC\n");
  12172. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12173. }
  12174. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12175. if (IS_G4X(dev)) {
  12176. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12177. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12178. }
  12179. if (IS_G4X(dev))
  12180. intel_dp_init(dev, DP_C, PORT_C);
  12181. }
  12182. if (IS_G4X(dev) &&
  12183. (I915_READ(DP_D) & DP_DETECTED))
  12184. intel_dp_init(dev, DP_D, PORT_D);
  12185. } else if (IS_GEN2(dev))
  12186. intel_dvo_init(dev);
  12187. if (SUPPORTS_TV(dev))
  12188. intel_tv_init(dev);
  12189. intel_psr_init(dev);
  12190. for_each_intel_encoder(dev, encoder) {
  12191. encoder->base.possible_crtcs = encoder->crtc_mask;
  12192. encoder->base.possible_clones =
  12193. intel_encoder_clones(encoder);
  12194. }
  12195. intel_init_pch_refclk(dev);
  12196. drm_helper_move_panel_connectors_to_head(dev);
  12197. }
  12198. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12199. {
  12200. struct drm_device *dev = fb->dev;
  12201. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12202. drm_framebuffer_cleanup(fb);
  12203. mutex_lock(&dev->struct_mutex);
  12204. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12205. drm_gem_object_unreference(&intel_fb->obj->base);
  12206. mutex_unlock(&dev->struct_mutex);
  12207. kfree(intel_fb);
  12208. }
  12209. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12210. struct drm_file *file,
  12211. unsigned int *handle)
  12212. {
  12213. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12214. struct drm_i915_gem_object *obj = intel_fb->obj;
  12215. if (obj->userptr.mm) {
  12216. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12217. return -EINVAL;
  12218. }
  12219. return drm_gem_handle_create(file, &obj->base, handle);
  12220. }
  12221. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12222. struct drm_file *file,
  12223. unsigned flags, unsigned color,
  12224. struct drm_clip_rect *clips,
  12225. unsigned num_clips)
  12226. {
  12227. struct drm_device *dev = fb->dev;
  12228. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12229. struct drm_i915_gem_object *obj = intel_fb->obj;
  12230. mutex_lock(&dev->struct_mutex);
  12231. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12232. mutex_unlock(&dev->struct_mutex);
  12233. return 0;
  12234. }
  12235. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12236. .destroy = intel_user_framebuffer_destroy,
  12237. .create_handle = intel_user_framebuffer_create_handle,
  12238. .dirty = intel_user_framebuffer_dirty,
  12239. };
  12240. static
  12241. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12242. uint32_t pixel_format)
  12243. {
  12244. u32 gen = INTEL_INFO(dev)->gen;
  12245. if (gen >= 9) {
  12246. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12247. /* "The stride in bytes must not exceed the of the size of 8K
  12248. * pixels and 32K bytes."
  12249. */
  12250. return min(8192 * cpp, 32768);
  12251. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12252. return 32*1024;
  12253. } else if (gen >= 4) {
  12254. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12255. return 16*1024;
  12256. else
  12257. return 32*1024;
  12258. } else if (gen >= 3) {
  12259. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12260. return 8*1024;
  12261. else
  12262. return 16*1024;
  12263. } else {
  12264. /* XXX DSPC is limited to 4k tiled */
  12265. return 8*1024;
  12266. }
  12267. }
  12268. static int intel_framebuffer_init(struct drm_device *dev,
  12269. struct intel_framebuffer *intel_fb,
  12270. struct drm_mode_fb_cmd2 *mode_cmd,
  12271. struct drm_i915_gem_object *obj)
  12272. {
  12273. struct drm_i915_private *dev_priv = to_i915(dev);
  12274. unsigned int aligned_height;
  12275. int ret;
  12276. u32 pitch_limit, stride_alignment;
  12277. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12278. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12279. /* Enforce that fb modifier and tiling mode match, but only for
  12280. * X-tiled. This is needed for FBC. */
  12281. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12282. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12283. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12284. return -EINVAL;
  12285. }
  12286. } else {
  12287. if (obj->tiling_mode == I915_TILING_X)
  12288. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12289. else if (obj->tiling_mode == I915_TILING_Y) {
  12290. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12291. return -EINVAL;
  12292. }
  12293. }
  12294. /* Passed in modifier sanity checking. */
  12295. switch (mode_cmd->modifier[0]) {
  12296. case I915_FORMAT_MOD_Y_TILED:
  12297. case I915_FORMAT_MOD_Yf_TILED:
  12298. if (INTEL_INFO(dev)->gen < 9) {
  12299. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12300. mode_cmd->modifier[0]);
  12301. return -EINVAL;
  12302. }
  12303. case DRM_FORMAT_MOD_NONE:
  12304. case I915_FORMAT_MOD_X_TILED:
  12305. break;
  12306. default:
  12307. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12308. mode_cmd->modifier[0]);
  12309. return -EINVAL;
  12310. }
  12311. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12312. mode_cmd->modifier[0],
  12313. mode_cmd->pixel_format);
  12314. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12315. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12316. mode_cmd->pitches[0], stride_alignment);
  12317. return -EINVAL;
  12318. }
  12319. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12320. mode_cmd->pixel_format);
  12321. if (mode_cmd->pitches[0] > pitch_limit) {
  12322. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12323. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12324. "tiled" : "linear",
  12325. mode_cmd->pitches[0], pitch_limit);
  12326. return -EINVAL;
  12327. }
  12328. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12329. mode_cmd->pitches[0] != obj->stride) {
  12330. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12331. mode_cmd->pitches[0], obj->stride);
  12332. return -EINVAL;
  12333. }
  12334. /* Reject formats not supported by any plane early. */
  12335. switch (mode_cmd->pixel_format) {
  12336. case DRM_FORMAT_C8:
  12337. case DRM_FORMAT_RGB565:
  12338. case DRM_FORMAT_XRGB8888:
  12339. case DRM_FORMAT_ARGB8888:
  12340. break;
  12341. case DRM_FORMAT_XRGB1555:
  12342. if (INTEL_INFO(dev)->gen > 3) {
  12343. DRM_DEBUG("unsupported pixel format: %s\n",
  12344. drm_get_format_name(mode_cmd->pixel_format));
  12345. return -EINVAL;
  12346. }
  12347. break;
  12348. case DRM_FORMAT_ABGR8888:
  12349. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12350. INTEL_INFO(dev)->gen < 9) {
  12351. DRM_DEBUG("unsupported pixel format: %s\n",
  12352. drm_get_format_name(mode_cmd->pixel_format));
  12353. return -EINVAL;
  12354. }
  12355. break;
  12356. case DRM_FORMAT_XBGR8888:
  12357. case DRM_FORMAT_XRGB2101010:
  12358. case DRM_FORMAT_XBGR2101010:
  12359. if (INTEL_INFO(dev)->gen < 4) {
  12360. DRM_DEBUG("unsupported pixel format: %s\n",
  12361. drm_get_format_name(mode_cmd->pixel_format));
  12362. return -EINVAL;
  12363. }
  12364. break;
  12365. case DRM_FORMAT_ABGR2101010:
  12366. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12367. DRM_DEBUG("unsupported pixel format: %s\n",
  12368. drm_get_format_name(mode_cmd->pixel_format));
  12369. return -EINVAL;
  12370. }
  12371. break;
  12372. case DRM_FORMAT_YUYV:
  12373. case DRM_FORMAT_UYVY:
  12374. case DRM_FORMAT_YVYU:
  12375. case DRM_FORMAT_VYUY:
  12376. if (INTEL_INFO(dev)->gen < 5) {
  12377. DRM_DEBUG("unsupported pixel format: %s\n",
  12378. drm_get_format_name(mode_cmd->pixel_format));
  12379. return -EINVAL;
  12380. }
  12381. break;
  12382. default:
  12383. DRM_DEBUG("unsupported pixel format: %s\n",
  12384. drm_get_format_name(mode_cmd->pixel_format));
  12385. return -EINVAL;
  12386. }
  12387. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12388. if (mode_cmd->offsets[0] != 0)
  12389. return -EINVAL;
  12390. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12391. mode_cmd->pixel_format,
  12392. mode_cmd->modifier[0]);
  12393. /* FIXME drm helper for size checks (especially planar formats)? */
  12394. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12395. return -EINVAL;
  12396. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12397. intel_fb->obj = obj;
  12398. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12399. if (ret) {
  12400. DRM_ERROR("framebuffer init failed %d\n", ret);
  12401. return ret;
  12402. }
  12403. intel_fb->obj->framebuffer_references++;
  12404. return 0;
  12405. }
  12406. static struct drm_framebuffer *
  12407. intel_user_framebuffer_create(struct drm_device *dev,
  12408. struct drm_file *filp,
  12409. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12410. {
  12411. struct drm_framebuffer *fb;
  12412. struct drm_i915_gem_object *obj;
  12413. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12414. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12415. mode_cmd.handles[0]));
  12416. if (&obj->base == NULL)
  12417. return ERR_PTR(-ENOENT);
  12418. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12419. if (IS_ERR(fb))
  12420. drm_gem_object_unreference_unlocked(&obj->base);
  12421. return fb;
  12422. }
  12423. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12424. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12425. {
  12426. }
  12427. #endif
  12428. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12429. .fb_create = intel_user_framebuffer_create,
  12430. .output_poll_changed = intel_fbdev_output_poll_changed,
  12431. .atomic_check = intel_atomic_check,
  12432. .atomic_commit = intel_atomic_commit,
  12433. .atomic_state_alloc = intel_atomic_state_alloc,
  12434. .atomic_state_clear = intel_atomic_state_clear,
  12435. };
  12436. /* Set up chip specific display functions */
  12437. static void intel_init_display(struct drm_device *dev)
  12438. {
  12439. struct drm_i915_private *dev_priv = dev->dev_private;
  12440. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12441. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12442. else if (IS_CHERRYVIEW(dev))
  12443. dev_priv->display.find_dpll = chv_find_best_dpll;
  12444. else if (IS_VALLEYVIEW(dev))
  12445. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12446. else if (IS_PINEVIEW(dev))
  12447. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12448. else
  12449. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12450. if (INTEL_INFO(dev)->gen >= 9) {
  12451. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12452. dev_priv->display.get_initial_plane_config =
  12453. skylake_get_initial_plane_config;
  12454. dev_priv->display.crtc_compute_clock =
  12455. haswell_crtc_compute_clock;
  12456. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12457. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12458. } else if (HAS_DDI(dev)) {
  12459. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12460. dev_priv->display.get_initial_plane_config =
  12461. ironlake_get_initial_plane_config;
  12462. dev_priv->display.crtc_compute_clock =
  12463. haswell_crtc_compute_clock;
  12464. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12465. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12466. } else if (HAS_PCH_SPLIT(dev)) {
  12467. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12468. dev_priv->display.get_initial_plane_config =
  12469. ironlake_get_initial_plane_config;
  12470. dev_priv->display.crtc_compute_clock =
  12471. ironlake_crtc_compute_clock;
  12472. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12473. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12474. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12475. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12476. dev_priv->display.get_initial_plane_config =
  12477. i9xx_get_initial_plane_config;
  12478. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12479. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12480. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12481. } else {
  12482. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12483. dev_priv->display.get_initial_plane_config =
  12484. i9xx_get_initial_plane_config;
  12485. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12486. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12487. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12488. }
  12489. /* Returns the core display clock speed */
  12490. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12491. dev_priv->display.get_display_clock_speed =
  12492. skylake_get_display_clock_speed;
  12493. else if (IS_BROXTON(dev))
  12494. dev_priv->display.get_display_clock_speed =
  12495. broxton_get_display_clock_speed;
  12496. else if (IS_BROADWELL(dev))
  12497. dev_priv->display.get_display_clock_speed =
  12498. broadwell_get_display_clock_speed;
  12499. else if (IS_HASWELL(dev))
  12500. dev_priv->display.get_display_clock_speed =
  12501. haswell_get_display_clock_speed;
  12502. else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  12503. dev_priv->display.get_display_clock_speed =
  12504. valleyview_get_display_clock_speed;
  12505. else if (IS_GEN5(dev))
  12506. dev_priv->display.get_display_clock_speed =
  12507. ilk_get_display_clock_speed;
  12508. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12509. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12510. dev_priv->display.get_display_clock_speed =
  12511. i945_get_display_clock_speed;
  12512. else if (IS_GM45(dev))
  12513. dev_priv->display.get_display_clock_speed =
  12514. gm45_get_display_clock_speed;
  12515. else if (IS_CRESTLINE(dev))
  12516. dev_priv->display.get_display_clock_speed =
  12517. i965gm_get_display_clock_speed;
  12518. else if (IS_PINEVIEW(dev))
  12519. dev_priv->display.get_display_clock_speed =
  12520. pnv_get_display_clock_speed;
  12521. else if (IS_G33(dev) || IS_G4X(dev))
  12522. dev_priv->display.get_display_clock_speed =
  12523. g33_get_display_clock_speed;
  12524. else if (IS_I915G(dev))
  12525. dev_priv->display.get_display_clock_speed =
  12526. i915_get_display_clock_speed;
  12527. else if (IS_I945GM(dev) || IS_845G(dev))
  12528. dev_priv->display.get_display_clock_speed =
  12529. i9xx_misc_get_display_clock_speed;
  12530. else if (IS_I915GM(dev))
  12531. dev_priv->display.get_display_clock_speed =
  12532. i915gm_get_display_clock_speed;
  12533. else if (IS_I865G(dev))
  12534. dev_priv->display.get_display_clock_speed =
  12535. i865_get_display_clock_speed;
  12536. else if (IS_I85X(dev))
  12537. dev_priv->display.get_display_clock_speed =
  12538. i85x_get_display_clock_speed;
  12539. else { /* 830 */
  12540. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12541. dev_priv->display.get_display_clock_speed =
  12542. i830_get_display_clock_speed;
  12543. }
  12544. if (IS_GEN5(dev)) {
  12545. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12546. } else if (IS_GEN6(dev)) {
  12547. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12548. } else if (IS_IVYBRIDGE(dev)) {
  12549. /* FIXME: detect B0+ stepping and use auto training */
  12550. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12551. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12552. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12553. if (IS_BROADWELL(dev)) {
  12554. dev_priv->display.modeset_commit_cdclk =
  12555. broadwell_modeset_commit_cdclk;
  12556. dev_priv->display.modeset_calc_cdclk =
  12557. broadwell_modeset_calc_cdclk;
  12558. }
  12559. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12560. dev_priv->display.modeset_commit_cdclk =
  12561. valleyview_modeset_commit_cdclk;
  12562. dev_priv->display.modeset_calc_cdclk =
  12563. valleyview_modeset_calc_cdclk;
  12564. } else if (IS_BROXTON(dev)) {
  12565. dev_priv->display.modeset_commit_cdclk =
  12566. broxton_modeset_commit_cdclk;
  12567. dev_priv->display.modeset_calc_cdclk =
  12568. broxton_modeset_calc_cdclk;
  12569. }
  12570. switch (INTEL_INFO(dev)->gen) {
  12571. case 2:
  12572. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12573. break;
  12574. case 3:
  12575. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12576. break;
  12577. case 4:
  12578. case 5:
  12579. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12580. break;
  12581. case 6:
  12582. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12583. break;
  12584. case 7:
  12585. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12586. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12587. break;
  12588. case 9:
  12589. /* Drop through - unsupported since execlist only. */
  12590. default:
  12591. /* Default just returns -ENODEV to indicate unsupported */
  12592. dev_priv->display.queue_flip = intel_default_queue_flip;
  12593. }
  12594. mutex_init(&dev_priv->pps_mutex);
  12595. }
  12596. /*
  12597. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12598. * resume, or other times. This quirk makes sure that's the case for
  12599. * affected systems.
  12600. */
  12601. static void quirk_pipea_force(struct drm_device *dev)
  12602. {
  12603. struct drm_i915_private *dev_priv = dev->dev_private;
  12604. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12605. DRM_INFO("applying pipe a force quirk\n");
  12606. }
  12607. static void quirk_pipeb_force(struct drm_device *dev)
  12608. {
  12609. struct drm_i915_private *dev_priv = dev->dev_private;
  12610. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12611. DRM_INFO("applying pipe b force quirk\n");
  12612. }
  12613. /*
  12614. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12615. */
  12616. static void quirk_ssc_force_disable(struct drm_device *dev)
  12617. {
  12618. struct drm_i915_private *dev_priv = dev->dev_private;
  12619. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12620. DRM_INFO("applying lvds SSC disable quirk\n");
  12621. }
  12622. /*
  12623. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12624. * brightness value
  12625. */
  12626. static void quirk_invert_brightness(struct drm_device *dev)
  12627. {
  12628. struct drm_i915_private *dev_priv = dev->dev_private;
  12629. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12630. DRM_INFO("applying inverted panel brightness quirk\n");
  12631. }
  12632. /* Some VBT's incorrectly indicate no backlight is present */
  12633. static void quirk_backlight_present(struct drm_device *dev)
  12634. {
  12635. struct drm_i915_private *dev_priv = dev->dev_private;
  12636. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12637. DRM_INFO("applying backlight present quirk\n");
  12638. }
  12639. struct intel_quirk {
  12640. int device;
  12641. int subsystem_vendor;
  12642. int subsystem_device;
  12643. void (*hook)(struct drm_device *dev);
  12644. };
  12645. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12646. struct intel_dmi_quirk {
  12647. void (*hook)(struct drm_device *dev);
  12648. const struct dmi_system_id (*dmi_id_list)[];
  12649. };
  12650. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12651. {
  12652. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12653. return 1;
  12654. }
  12655. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12656. {
  12657. .dmi_id_list = &(const struct dmi_system_id[]) {
  12658. {
  12659. .callback = intel_dmi_reverse_brightness,
  12660. .ident = "NCR Corporation",
  12661. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12662. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12663. },
  12664. },
  12665. { } /* terminating entry */
  12666. },
  12667. .hook = quirk_invert_brightness,
  12668. },
  12669. };
  12670. static struct intel_quirk intel_quirks[] = {
  12671. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12672. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12673. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12674. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12675. /* 830 needs to leave pipe A & dpll A up */
  12676. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12677. /* 830 needs to leave pipe B & dpll B up */
  12678. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12679. /* Lenovo U160 cannot use SSC on LVDS */
  12680. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12681. /* Sony Vaio Y cannot use SSC on LVDS */
  12682. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12683. /* Acer Aspire 5734Z must invert backlight brightness */
  12684. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12685. /* Acer/eMachines G725 */
  12686. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12687. /* Acer/eMachines e725 */
  12688. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12689. /* Acer/Packard Bell NCL20 */
  12690. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12691. /* Acer Aspire 4736Z */
  12692. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12693. /* Acer Aspire 5336 */
  12694. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12695. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12696. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12697. /* Acer C720 Chromebook (Core i3 4005U) */
  12698. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12699. /* Apple Macbook 2,1 (Core 2 T7400) */
  12700. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12701. /* Apple Macbook 4,1 */
  12702. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12703. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12704. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12705. /* HP Chromebook 14 (Celeron 2955U) */
  12706. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12707. /* Dell Chromebook 11 */
  12708. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12709. /* Dell Chromebook 11 (2015 version) */
  12710. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12711. };
  12712. static void intel_init_quirks(struct drm_device *dev)
  12713. {
  12714. struct pci_dev *d = dev->pdev;
  12715. int i;
  12716. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12717. struct intel_quirk *q = &intel_quirks[i];
  12718. if (d->device == q->device &&
  12719. (d->subsystem_vendor == q->subsystem_vendor ||
  12720. q->subsystem_vendor == PCI_ANY_ID) &&
  12721. (d->subsystem_device == q->subsystem_device ||
  12722. q->subsystem_device == PCI_ANY_ID))
  12723. q->hook(dev);
  12724. }
  12725. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12726. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12727. intel_dmi_quirks[i].hook(dev);
  12728. }
  12729. }
  12730. /* Disable the VGA plane that we never use */
  12731. static void i915_disable_vga(struct drm_device *dev)
  12732. {
  12733. struct drm_i915_private *dev_priv = dev->dev_private;
  12734. u8 sr1;
  12735. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12736. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12737. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12738. outb(SR01, VGA_SR_INDEX);
  12739. sr1 = inb(VGA_SR_DATA);
  12740. outb(sr1 | 1<<5, VGA_SR_DATA);
  12741. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12742. udelay(300);
  12743. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12744. POSTING_READ(vga_reg);
  12745. }
  12746. void intel_modeset_init_hw(struct drm_device *dev)
  12747. {
  12748. struct drm_i915_private *dev_priv = dev->dev_private;
  12749. intel_update_cdclk(dev);
  12750. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12751. intel_init_clock_gating(dev);
  12752. intel_enable_gt_powersave(dev);
  12753. }
  12754. /*
  12755. * Calculate what we think the watermarks should be for the state we've read
  12756. * out of the hardware and then immediately program those watermarks so that
  12757. * we ensure the hardware settings match our internal state.
  12758. *
  12759. * We can calculate what we think WM's should be by creating a duplicate of the
  12760. * current state (which was constructed during hardware readout) and running it
  12761. * through the atomic check code to calculate new watermark values in the
  12762. * state object.
  12763. */
  12764. static void sanitize_watermarks(struct drm_device *dev)
  12765. {
  12766. struct drm_i915_private *dev_priv = to_i915(dev);
  12767. struct drm_atomic_state *state;
  12768. struct drm_crtc *crtc;
  12769. struct drm_crtc_state *cstate;
  12770. struct drm_modeset_acquire_ctx ctx;
  12771. int ret;
  12772. int i;
  12773. /* Only supported on platforms that use atomic watermark design */
  12774. if (!dev_priv->display.program_watermarks)
  12775. return;
  12776. /*
  12777. * We need to hold connection_mutex before calling duplicate_state so
  12778. * that the connector loop is protected.
  12779. */
  12780. drm_modeset_acquire_init(&ctx, 0);
  12781. retry:
  12782. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12783. if (ret == -EDEADLK) {
  12784. drm_modeset_backoff(&ctx);
  12785. goto retry;
  12786. } else if (WARN_ON(ret)) {
  12787. goto fail;
  12788. }
  12789. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12790. if (WARN_ON(IS_ERR(state)))
  12791. goto fail;
  12792. ret = intel_atomic_check(dev, state);
  12793. if (ret) {
  12794. /*
  12795. * If we fail here, it means that the hardware appears to be
  12796. * programmed in a way that shouldn't be possible, given our
  12797. * understanding of watermark requirements. This might mean a
  12798. * mistake in the hardware readout code or a mistake in the
  12799. * watermark calculations for a given platform. Raise a WARN
  12800. * so that this is noticeable.
  12801. *
  12802. * If this actually happens, we'll have to just leave the
  12803. * BIOS-programmed watermarks untouched and hope for the best.
  12804. */
  12805. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12806. goto fail;
  12807. }
  12808. /* Write calculated watermark values back */
  12809. to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
  12810. for_each_crtc_in_state(state, crtc, cstate, i) {
  12811. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12812. dev_priv->display.program_watermarks(cs);
  12813. }
  12814. drm_atomic_state_free(state);
  12815. fail:
  12816. drm_modeset_drop_locks(&ctx);
  12817. drm_modeset_acquire_fini(&ctx);
  12818. }
  12819. void intel_modeset_init(struct drm_device *dev)
  12820. {
  12821. struct drm_i915_private *dev_priv = dev->dev_private;
  12822. int sprite, ret;
  12823. enum pipe pipe;
  12824. struct intel_crtc *crtc;
  12825. drm_mode_config_init(dev);
  12826. dev->mode_config.min_width = 0;
  12827. dev->mode_config.min_height = 0;
  12828. dev->mode_config.preferred_depth = 24;
  12829. dev->mode_config.prefer_shadow = 1;
  12830. dev->mode_config.allow_fb_modifiers = true;
  12831. dev->mode_config.funcs = &intel_mode_funcs;
  12832. intel_init_quirks(dev);
  12833. intel_init_pm(dev);
  12834. if (INTEL_INFO(dev)->num_pipes == 0)
  12835. return;
  12836. /*
  12837. * There may be no VBT; and if the BIOS enabled SSC we can
  12838. * just keep using it to avoid unnecessary flicker. Whereas if the
  12839. * BIOS isn't using it, don't assume it will work even if the VBT
  12840. * indicates as much.
  12841. */
  12842. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12843. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12844. DREF_SSC1_ENABLE);
  12845. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12846. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12847. bios_lvds_use_ssc ? "en" : "dis",
  12848. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12849. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12850. }
  12851. }
  12852. intel_init_display(dev);
  12853. intel_init_audio(dev);
  12854. if (IS_GEN2(dev)) {
  12855. dev->mode_config.max_width = 2048;
  12856. dev->mode_config.max_height = 2048;
  12857. } else if (IS_GEN3(dev)) {
  12858. dev->mode_config.max_width = 4096;
  12859. dev->mode_config.max_height = 4096;
  12860. } else {
  12861. dev->mode_config.max_width = 8192;
  12862. dev->mode_config.max_height = 8192;
  12863. }
  12864. if (IS_845G(dev) || IS_I865G(dev)) {
  12865. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12866. dev->mode_config.cursor_height = 1023;
  12867. } else if (IS_GEN2(dev)) {
  12868. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12869. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12870. } else {
  12871. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12872. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12873. }
  12874. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12875. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12876. INTEL_INFO(dev)->num_pipes,
  12877. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12878. for_each_pipe(dev_priv, pipe) {
  12879. intel_crtc_init(dev, pipe);
  12880. for_each_sprite(dev_priv, pipe, sprite) {
  12881. ret = intel_plane_init(dev, pipe, sprite);
  12882. if (ret)
  12883. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12884. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12885. }
  12886. }
  12887. intel_update_czclk(dev_priv);
  12888. intel_update_cdclk(dev);
  12889. intel_shared_dpll_init(dev);
  12890. /* Just disable it once at startup */
  12891. i915_disable_vga(dev);
  12892. intel_setup_outputs(dev);
  12893. drm_modeset_lock_all(dev);
  12894. intel_modeset_setup_hw_state(dev);
  12895. drm_modeset_unlock_all(dev);
  12896. for_each_intel_crtc(dev, crtc) {
  12897. struct intel_initial_plane_config plane_config = {};
  12898. if (!crtc->active)
  12899. continue;
  12900. /*
  12901. * Note that reserving the BIOS fb up front prevents us
  12902. * from stuffing other stolen allocations like the ring
  12903. * on top. This prevents some ugliness at boot time, and
  12904. * can even allow for smooth boot transitions if the BIOS
  12905. * fb is large enough for the active pipe configuration.
  12906. */
  12907. dev_priv->display.get_initial_plane_config(crtc,
  12908. &plane_config);
  12909. /*
  12910. * If the fb is shared between multiple heads, we'll
  12911. * just get the first one.
  12912. */
  12913. intel_find_initial_plane_obj(crtc, &plane_config);
  12914. }
  12915. /*
  12916. * Make sure hardware watermarks really match the state we read out.
  12917. * Note that we need to do this after reconstructing the BIOS fb's
  12918. * since the watermark calculation done here will use pstate->fb.
  12919. */
  12920. sanitize_watermarks(dev);
  12921. }
  12922. static void intel_enable_pipe_a(struct drm_device *dev)
  12923. {
  12924. struct intel_connector *connector;
  12925. struct drm_connector *crt = NULL;
  12926. struct intel_load_detect_pipe load_detect_temp;
  12927. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12928. /* We can't just switch on the pipe A, we need to set things up with a
  12929. * proper mode and output configuration. As a gross hack, enable pipe A
  12930. * by enabling the load detect pipe once. */
  12931. for_each_intel_connector(dev, connector) {
  12932. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12933. crt = &connector->base;
  12934. break;
  12935. }
  12936. }
  12937. if (!crt)
  12938. return;
  12939. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12940. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12941. }
  12942. static bool
  12943. intel_check_plane_mapping(struct intel_crtc *crtc)
  12944. {
  12945. struct drm_device *dev = crtc->base.dev;
  12946. struct drm_i915_private *dev_priv = dev->dev_private;
  12947. u32 val;
  12948. if (INTEL_INFO(dev)->num_pipes == 1)
  12949. return true;
  12950. val = I915_READ(DSPCNTR(!crtc->plane));
  12951. if ((val & DISPLAY_PLANE_ENABLE) &&
  12952. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12953. return false;
  12954. return true;
  12955. }
  12956. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12957. {
  12958. struct drm_device *dev = crtc->base.dev;
  12959. struct intel_encoder *encoder;
  12960. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12961. return true;
  12962. return false;
  12963. }
  12964. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  12965. {
  12966. struct drm_device *dev = encoder->base.dev;
  12967. struct intel_connector *connector;
  12968. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12969. return true;
  12970. return false;
  12971. }
  12972. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12973. {
  12974. struct drm_device *dev = crtc->base.dev;
  12975. struct drm_i915_private *dev_priv = dev->dev_private;
  12976. i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
  12977. /* Clear any frame start delays used for debugging left by the BIOS */
  12978. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12979. /* restore vblank interrupts to correct state */
  12980. drm_crtc_vblank_reset(&crtc->base);
  12981. if (crtc->active) {
  12982. struct intel_plane *plane;
  12983. drm_crtc_vblank_on(&crtc->base);
  12984. /* Disable everything but the primary plane */
  12985. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12986. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12987. continue;
  12988. plane->disable_plane(&plane->base, &crtc->base);
  12989. }
  12990. }
  12991. /* We need to sanitize the plane -> pipe mapping first because this will
  12992. * disable the crtc (and hence change the state) if it is wrong. Note
  12993. * that gen4+ has a fixed plane -> pipe mapping. */
  12994. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12995. bool plane;
  12996. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12997. crtc->base.base.id);
  12998. /* Pipe has the wrong plane attached and the plane is active.
  12999. * Temporarily change the plane mapping and disable everything
  13000. * ... */
  13001. plane = crtc->plane;
  13002. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13003. crtc->plane = !plane;
  13004. intel_crtc_disable_noatomic(&crtc->base);
  13005. crtc->plane = plane;
  13006. }
  13007. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13008. crtc->pipe == PIPE_A && !crtc->active) {
  13009. /* BIOS forgot to enable pipe A, this mostly happens after
  13010. * resume. Force-enable the pipe to fix this, the update_dpms
  13011. * call below we restore the pipe to the right state, but leave
  13012. * the required bits on. */
  13013. intel_enable_pipe_a(dev);
  13014. }
  13015. /* Adjust the state of the output pipe according to whether we
  13016. * have active connectors/encoders. */
  13017. if (!intel_crtc_has_encoders(crtc))
  13018. intel_crtc_disable_noatomic(&crtc->base);
  13019. if (crtc->active != crtc->base.state->active) {
  13020. struct intel_encoder *encoder;
  13021. /* This can happen either due to bugs in the get_hw_state
  13022. * functions or because of calls to intel_crtc_disable_noatomic,
  13023. * or because the pipe is force-enabled due to the
  13024. * pipe A quirk. */
  13025. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  13026. crtc->base.base.id,
  13027. crtc->base.state->enable ? "enabled" : "disabled",
  13028. crtc->active ? "enabled" : "disabled");
  13029. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  13030. crtc->base.state->active = crtc->active;
  13031. crtc->base.enabled = crtc->active;
  13032. crtc->base.state->connector_mask = 0;
  13033. crtc->base.state->encoder_mask = 0;
  13034. /* Because we only establish the connector -> encoder ->
  13035. * crtc links if something is active, this means the
  13036. * crtc is now deactivated. Break the links. connector
  13037. * -> encoder links are only establish when things are
  13038. * actually up, hence no need to break them. */
  13039. WARN_ON(crtc->active);
  13040. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13041. encoder->base.crtc = NULL;
  13042. }
  13043. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13044. /*
  13045. * We start out with underrun reporting disabled to avoid races.
  13046. * For correct bookkeeping mark this on active crtcs.
  13047. *
  13048. * Also on gmch platforms we dont have any hardware bits to
  13049. * disable the underrun reporting. Which means we need to start
  13050. * out with underrun reporting disabled also on inactive pipes,
  13051. * since otherwise we'll complain about the garbage we read when
  13052. * e.g. coming up after runtime pm.
  13053. *
  13054. * No protection against concurrent access is required - at
  13055. * worst a fifo underrun happens which also sets this to false.
  13056. */
  13057. crtc->cpu_fifo_underrun_disabled = true;
  13058. crtc->pch_fifo_underrun_disabled = true;
  13059. }
  13060. }
  13061. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13062. {
  13063. struct intel_connector *connector;
  13064. struct drm_device *dev = encoder->base.dev;
  13065. /* We need to check both for a crtc link (meaning that the
  13066. * encoder is active and trying to read from a pipe) and the
  13067. * pipe itself being active. */
  13068. bool has_active_crtc = encoder->base.crtc &&
  13069. to_intel_crtc(encoder->base.crtc)->active;
  13070. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13071. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13072. encoder->base.base.id,
  13073. encoder->base.name);
  13074. /* Connector is active, but has no active pipe. This is
  13075. * fallout from our resume register restoring. Disable
  13076. * the encoder manually again. */
  13077. if (encoder->base.crtc) {
  13078. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13079. encoder->base.base.id,
  13080. encoder->base.name);
  13081. encoder->disable(encoder);
  13082. if (encoder->post_disable)
  13083. encoder->post_disable(encoder);
  13084. }
  13085. encoder->base.crtc = NULL;
  13086. /* Inconsistent output/port/pipe state happens presumably due to
  13087. * a bug in one of the get_hw_state functions. Or someplace else
  13088. * in our code, like the register restore mess on resume. Clamp
  13089. * things to off as a safer default. */
  13090. for_each_intel_connector(dev, connector) {
  13091. if (connector->encoder != encoder)
  13092. continue;
  13093. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13094. connector->base.encoder = NULL;
  13095. }
  13096. }
  13097. /* Enabled encoders without active connectors will be fixed in
  13098. * the crtc fixup. */
  13099. }
  13100. void i915_redisable_vga_power_on(struct drm_device *dev)
  13101. {
  13102. struct drm_i915_private *dev_priv = dev->dev_private;
  13103. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13104. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13105. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13106. i915_disable_vga(dev);
  13107. }
  13108. }
  13109. void i915_redisable_vga(struct drm_device *dev)
  13110. {
  13111. struct drm_i915_private *dev_priv = dev->dev_private;
  13112. /* This function can be called both from intel_modeset_setup_hw_state or
  13113. * at a very early point in our resume sequence, where the power well
  13114. * structures are not yet restored. Since this function is at a very
  13115. * paranoid "someone might have enabled VGA while we were not looking"
  13116. * level, just check if the power well is enabled instead of trying to
  13117. * follow the "don't touch the power well if we don't need it" policy
  13118. * the rest of the driver uses. */
  13119. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13120. return;
  13121. i915_redisable_vga_power_on(dev);
  13122. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13123. }
  13124. static bool primary_get_hw_state(struct intel_plane *plane)
  13125. {
  13126. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13127. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13128. }
  13129. /* FIXME read out full plane state for all planes */
  13130. static void readout_plane_state(struct intel_crtc *crtc)
  13131. {
  13132. struct drm_plane *primary = crtc->base.primary;
  13133. struct intel_plane_state *plane_state =
  13134. to_intel_plane_state(primary->state);
  13135. plane_state->visible = crtc->active &&
  13136. primary_get_hw_state(to_intel_plane(primary));
  13137. if (plane_state->visible)
  13138. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13139. }
  13140. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13141. {
  13142. struct drm_i915_private *dev_priv = dev->dev_private;
  13143. enum pipe pipe;
  13144. struct intel_crtc *crtc;
  13145. struct intel_encoder *encoder;
  13146. struct intel_connector *connector;
  13147. int i;
  13148. dev_priv->active_crtcs = 0;
  13149. for_each_intel_crtc(dev, crtc) {
  13150. struct intel_crtc_state *crtc_state = crtc->config;
  13151. int pixclk = 0;
  13152. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  13153. memset(crtc_state, 0, sizeof(*crtc_state));
  13154. crtc_state->base.crtc = &crtc->base;
  13155. crtc_state->base.active = crtc_state->base.enable =
  13156. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13157. crtc->base.enabled = crtc_state->base.enable;
  13158. crtc->active = crtc_state->base.active;
  13159. if (crtc_state->base.active) {
  13160. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13161. if (IS_BROADWELL(dev_priv)) {
  13162. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13163. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13164. if (crtc_state->ips_enabled)
  13165. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13166. } else if (IS_VALLEYVIEW(dev_priv) ||
  13167. IS_CHERRYVIEW(dev_priv) ||
  13168. IS_BROXTON(dev_priv))
  13169. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13170. else
  13171. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13172. }
  13173. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13174. readout_plane_state(crtc);
  13175. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13176. crtc->base.base.id,
  13177. crtc->active ? "enabled" : "disabled");
  13178. }
  13179. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13180. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13181. pll->on = pll->get_hw_state(dev_priv, pll,
  13182. &pll->config.hw_state);
  13183. pll->active = 0;
  13184. pll->config.crtc_mask = 0;
  13185. for_each_intel_crtc(dev, crtc) {
  13186. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  13187. pll->active++;
  13188. pll->config.crtc_mask |= 1 << crtc->pipe;
  13189. }
  13190. }
  13191. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13192. pll->name, pll->config.crtc_mask, pll->on);
  13193. if (pll->config.crtc_mask)
  13194. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  13195. }
  13196. for_each_intel_encoder(dev, encoder) {
  13197. pipe = 0;
  13198. if (encoder->get_hw_state(encoder, &pipe)) {
  13199. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13200. encoder->base.crtc = &crtc->base;
  13201. encoder->get_config(encoder, crtc->config);
  13202. } else {
  13203. encoder->base.crtc = NULL;
  13204. }
  13205. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13206. encoder->base.base.id,
  13207. encoder->base.name,
  13208. encoder->base.crtc ? "enabled" : "disabled",
  13209. pipe_name(pipe));
  13210. }
  13211. for_each_intel_connector(dev, connector) {
  13212. if (connector->get_hw_state(connector)) {
  13213. connector->base.dpms = DRM_MODE_DPMS_ON;
  13214. encoder = connector->encoder;
  13215. connector->base.encoder = &encoder->base;
  13216. if (encoder->base.crtc &&
  13217. encoder->base.crtc->state->active) {
  13218. /*
  13219. * This has to be done during hardware readout
  13220. * because anything calling .crtc_disable may
  13221. * rely on the connector_mask being accurate.
  13222. */
  13223. encoder->base.crtc->state->connector_mask |=
  13224. 1 << drm_connector_index(&connector->base);
  13225. encoder->base.crtc->state->encoder_mask |=
  13226. 1 << drm_encoder_index(&encoder->base);
  13227. }
  13228. } else {
  13229. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13230. connector->base.encoder = NULL;
  13231. }
  13232. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13233. connector->base.base.id,
  13234. connector->base.name,
  13235. connector->base.encoder ? "enabled" : "disabled");
  13236. }
  13237. for_each_intel_crtc(dev, crtc) {
  13238. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13239. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13240. if (crtc->base.state->active) {
  13241. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13242. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13243. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13244. /*
  13245. * The initial mode needs to be set in order to keep
  13246. * the atomic core happy. It wants a valid mode if the
  13247. * crtc's enabled, so we do the above call.
  13248. *
  13249. * At this point some state updated by the connectors
  13250. * in their ->detect() callback has not run yet, so
  13251. * no recalculation can be done yet.
  13252. *
  13253. * Even if we could do a recalculation and modeset
  13254. * right now it would cause a double modeset if
  13255. * fbdev or userspace chooses a different initial mode.
  13256. *
  13257. * If that happens, someone indicated they wanted a
  13258. * mode change, which means it's safe to do a full
  13259. * recalculation.
  13260. */
  13261. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13262. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13263. update_scanline_offset(crtc);
  13264. }
  13265. }
  13266. }
  13267. /* Scan out the current hw modeset state,
  13268. * and sanitizes it to the current state
  13269. */
  13270. static void
  13271. intel_modeset_setup_hw_state(struct drm_device *dev)
  13272. {
  13273. struct drm_i915_private *dev_priv = dev->dev_private;
  13274. enum pipe pipe;
  13275. struct intel_crtc *crtc;
  13276. struct intel_encoder *encoder;
  13277. int i;
  13278. intel_modeset_readout_hw_state(dev);
  13279. /* HW state is read out, now we need to sanitize this mess. */
  13280. for_each_intel_encoder(dev, encoder) {
  13281. intel_sanitize_encoder(encoder);
  13282. }
  13283. for_each_pipe(dev_priv, pipe) {
  13284. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13285. intel_sanitize_crtc(crtc);
  13286. intel_dump_pipe_config(crtc, crtc->config,
  13287. "[setup_hw_state]");
  13288. }
  13289. intel_modeset_update_connector_atomic_state(dev);
  13290. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13291. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13292. if (!pll->on || pll->active)
  13293. continue;
  13294. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13295. pll->disable(dev_priv, pll);
  13296. pll->on = false;
  13297. }
  13298. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13299. vlv_wm_get_hw_state(dev);
  13300. else if (IS_GEN9(dev))
  13301. skl_wm_get_hw_state(dev);
  13302. else if (HAS_PCH_SPLIT(dev))
  13303. ilk_wm_get_hw_state(dev);
  13304. for_each_intel_crtc(dev, crtc) {
  13305. unsigned long put_domains;
  13306. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13307. if (WARN_ON(put_domains))
  13308. modeset_put_power_domains(dev_priv, put_domains);
  13309. }
  13310. intel_display_set_init_power(dev_priv, false);
  13311. intel_fbc_init_pipe_state(dev_priv);
  13312. }
  13313. void intel_display_resume(struct drm_device *dev)
  13314. {
  13315. struct drm_i915_private *dev_priv = to_i915(dev);
  13316. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13317. struct drm_modeset_acquire_ctx ctx;
  13318. int ret;
  13319. bool setup = false;
  13320. dev_priv->modeset_restore_state = NULL;
  13321. /*
  13322. * This is a cludge because with real atomic modeset mode_config.mutex
  13323. * won't be taken. Unfortunately some probed state like
  13324. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13325. * it here for now.
  13326. */
  13327. mutex_lock(&dev->mode_config.mutex);
  13328. drm_modeset_acquire_init(&ctx, 0);
  13329. retry:
  13330. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13331. if (ret == 0 && !setup) {
  13332. setup = true;
  13333. intel_modeset_setup_hw_state(dev);
  13334. i915_redisable_vga(dev);
  13335. }
  13336. if (ret == 0 && state) {
  13337. struct drm_crtc_state *crtc_state;
  13338. struct drm_crtc *crtc;
  13339. int i;
  13340. state->acquire_ctx = &ctx;
  13341. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13342. /*
  13343. * Force recalculation even if we restore
  13344. * current state. With fast modeset this may not result
  13345. * in a modeset when the state is compatible.
  13346. */
  13347. crtc_state->mode_changed = true;
  13348. }
  13349. ret = drm_atomic_commit(state);
  13350. }
  13351. if (ret == -EDEADLK) {
  13352. drm_modeset_backoff(&ctx);
  13353. goto retry;
  13354. }
  13355. drm_modeset_drop_locks(&ctx);
  13356. drm_modeset_acquire_fini(&ctx);
  13357. mutex_unlock(&dev->mode_config.mutex);
  13358. if (ret) {
  13359. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13360. drm_atomic_state_free(state);
  13361. }
  13362. }
  13363. void intel_modeset_gem_init(struct drm_device *dev)
  13364. {
  13365. struct drm_crtc *c;
  13366. struct drm_i915_gem_object *obj;
  13367. int ret;
  13368. intel_init_gt_powersave(dev);
  13369. intel_modeset_init_hw(dev);
  13370. intel_setup_overlay(dev);
  13371. /*
  13372. * Make sure any fbs we allocated at startup are properly
  13373. * pinned & fenced. When we do the allocation it's too early
  13374. * for this.
  13375. */
  13376. for_each_crtc(dev, c) {
  13377. obj = intel_fb_obj(c->primary->fb);
  13378. if (obj == NULL)
  13379. continue;
  13380. mutex_lock(&dev->struct_mutex);
  13381. ret = intel_pin_and_fence_fb_obj(c->primary,
  13382. c->primary->fb,
  13383. c->primary->state);
  13384. mutex_unlock(&dev->struct_mutex);
  13385. if (ret) {
  13386. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13387. to_intel_crtc(c)->pipe);
  13388. drm_framebuffer_unreference(c->primary->fb);
  13389. c->primary->fb = NULL;
  13390. c->primary->crtc = c->primary->state->crtc = NULL;
  13391. update_state_fb(c->primary);
  13392. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13393. }
  13394. }
  13395. intel_backlight_register(dev);
  13396. }
  13397. void intel_connector_unregister(struct intel_connector *intel_connector)
  13398. {
  13399. struct drm_connector *connector = &intel_connector->base;
  13400. intel_panel_destroy_backlight(connector);
  13401. drm_connector_unregister(connector);
  13402. }
  13403. void intel_modeset_cleanup(struct drm_device *dev)
  13404. {
  13405. struct drm_i915_private *dev_priv = dev->dev_private;
  13406. struct intel_connector *connector;
  13407. intel_disable_gt_powersave(dev);
  13408. intel_backlight_unregister(dev);
  13409. /*
  13410. * Interrupts and polling as the first thing to avoid creating havoc.
  13411. * Too much stuff here (turning of connectors, ...) would
  13412. * experience fancy races otherwise.
  13413. */
  13414. intel_irq_uninstall(dev_priv);
  13415. /*
  13416. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13417. * poll handlers. Hence disable polling after hpd handling is shut down.
  13418. */
  13419. drm_kms_helper_poll_fini(dev);
  13420. intel_unregister_dsm_handler();
  13421. intel_fbc_global_disable(dev_priv);
  13422. /* flush any delayed tasks or pending work */
  13423. flush_scheduled_work();
  13424. /* destroy the backlight and sysfs files before encoders/connectors */
  13425. for_each_intel_connector(dev, connector)
  13426. connector->unregister(connector);
  13427. drm_mode_config_cleanup(dev);
  13428. intel_cleanup_overlay(dev);
  13429. intel_cleanup_gt_powersave(dev);
  13430. intel_teardown_gmbus(dev);
  13431. }
  13432. /*
  13433. * Return which encoder is currently attached for connector.
  13434. */
  13435. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13436. {
  13437. return &intel_attached_encoder(connector)->base;
  13438. }
  13439. void intel_connector_attach_encoder(struct intel_connector *connector,
  13440. struct intel_encoder *encoder)
  13441. {
  13442. connector->encoder = encoder;
  13443. drm_mode_connector_attach_encoder(&connector->base,
  13444. &encoder->base);
  13445. }
  13446. /*
  13447. * set vga decode state - true == enable VGA decode
  13448. */
  13449. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13450. {
  13451. struct drm_i915_private *dev_priv = dev->dev_private;
  13452. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13453. u16 gmch_ctrl;
  13454. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13455. DRM_ERROR("failed to read control word\n");
  13456. return -EIO;
  13457. }
  13458. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13459. return 0;
  13460. if (state)
  13461. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13462. else
  13463. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13464. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13465. DRM_ERROR("failed to write control word\n");
  13466. return -EIO;
  13467. }
  13468. return 0;
  13469. }
  13470. struct intel_display_error_state {
  13471. u32 power_well_driver;
  13472. int num_transcoders;
  13473. struct intel_cursor_error_state {
  13474. u32 control;
  13475. u32 position;
  13476. u32 base;
  13477. u32 size;
  13478. } cursor[I915_MAX_PIPES];
  13479. struct intel_pipe_error_state {
  13480. bool power_domain_on;
  13481. u32 source;
  13482. u32 stat;
  13483. } pipe[I915_MAX_PIPES];
  13484. struct intel_plane_error_state {
  13485. u32 control;
  13486. u32 stride;
  13487. u32 size;
  13488. u32 pos;
  13489. u32 addr;
  13490. u32 surface;
  13491. u32 tile_offset;
  13492. } plane[I915_MAX_PIPES];
  13493. struct intel_transcoder_error_state {
  13494. bool power_domain_on;
  13495. enum transcoder cpu_transcoder;
  13496. u32 conf;
  13497. u32 htotal;
  13498. u32 hblank;
  13499. u32 hsync;
  13500. u32 vtotal;
  13501. u32 vblank;
  13502. u32 vsync;
  13503. } transcoder[4];
  13504. };
  13505. struct intel_display_error_state *
  13506. intel_display_capture_error_state(struct drm_device *dev)
  13507. {
  13508. struct drm_i915_private *dev_priv = dev->dev_private;
  13509. struct intel_display_error_state *error;
  13510. int transcoders[] = {
  13511. TRANSCODER_A,
  13512. TRANSCODER_B,
  13513. TRANSCODER_C,
  13514. TRANSCODER_EDP,
  13515. };
  13516. int i;
  13517. if (INTEL_INFO(dev)->num_pipes == 0)
  13518. return NULL;
  13519. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13520. if (error == NULL)
  13521. return NULL;
  13522. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13523. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13524. for_each_pipe(dev_priv, i) {
  13525. error->pipe[i].power_domain_on =
  13526. __intel_display_power_is_enabled(dev_priv,
  13527. POWER_DOMAIN_PIPE(i));
  13528. if (!error->pipe[i].power_domain_on)
  13529. continue;
  13530. error->cursor[i].control = I915_READ(CURCNTR(i));
  13531. error->cursor[i].position = I915_READ(CURPOS(i));
  13532. error->cursor[i].base = I915_READ(CURBASE(i));
  13533. error->plane[i].control = I915_READ(DSPCNTR(i));
  13534. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13535. if (INTEL_INFO(dev)->gen <= 3) {
  13536. error->plane[i].size = I915_READ(DSPSIZE(i));
  13537. error->plane[i].pos = I915_READ(DSPPOS(i));
  13538. }
  13539. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13540. error->plane[i].addr = I915_READ(DSPADDR(i));
  13541. if (INTEL_INFO(dev)->gen >= 4) {
  13542. error->plane[i].surface = I915_READ(DSPSURF(i));
  13543. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13544. }
  13545. error->pipe[i].source = I915_READ(PIPESRC(i));
  13546. if (HAS_GMCH_DISPLAY(dev))
  13547. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13548. }
  13549. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13550. if (HAS_DDI(dev_priv->dev))
  13551. error->num_transcoders++; /* Account for eDP. */
  13552. for (i = 0; i < error->num_transcoders; i++) {
  13553. enum transcoder cpu_transcoder = transcoders[i];
  13554. error->transcoder[i].power_domain_on =
  13555. __intel_display_power_is_enabled(dev_priv,
  13556. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13557. if (!error->transcoder[i].power_domain_on)
  13558. continue;
  13559. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13560. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13561. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13562. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13563. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13564. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13565. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13566. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13567. }
  13568. return error;
  13569. }
  13570. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13571. void
  13572. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13573. struct drm_device *dev,
  13574. struct intel_display_error_state *error)
  13575. {
  13576. struct drm_i915_private *dev_priv = dev->dev_private;
  13577. int i;
  13578. if (!error)
  13579. return;
  13580. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13581. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13582. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13583. error->power_well_driver);
  13584. for_each_pipe(dev_priv, i) {
  13585. err_printf(m, "Pipe [%d]:\n", i);
  13586. err_printf(m, " Power: %s\n",
  13587. onoff(error->pipe[i].power_domain_on));
  13588. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13589. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13590. err_printf(m, "Plane [%d]:\n", i);
  13591. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13592. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13593. if (INTEL_INFO(dev)->gen <= 3) {
  13594. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13595. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13596. }
  13597. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13598. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13599. if (INTEL_INFO(dev)->gen >= 4) {
  13600. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13601. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13602. }
  13603. err_printf(m, "Cursor [%d]:\n", i);
  13604. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13605. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13606. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13607. }
  13608. for (i = 0; i < error->num_transcoders; i++) {
  13609. err_printf(m, "CPU transcoder: %c\n",
  13610. transcoder_name(error->transcoder[i].cpu_transcoder));
  13611. err_printf(m, " Power: %s\n",
  13612. onoff(error->transcoder[i].power_domain_on));
  13613. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13614. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13615. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13616. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13617. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13618. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13619. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13620. }
  13621. }