intel_display.c 456 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344
  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 "intel_dsi.h"
  39. #include "i915_trace.h"
  40. #include <drm/drm_atomic.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include <drm/drm_dp_helper.h>
  43. #include <drm/drm_crtc_helper.h>
  44. #include <drm/drm_plane_helper.h>
  45. #include <drm/drm_rect.h>
  46. #include <linux/dma_remapping.h>
  47. #include <linux/reservation.h>
  48. #include <linux/dma-buf.h>
  49. /* Primary plane formats for gen <= 3 */
  50. static const uint32_t i8xx_primary_formats[] = {
  51. DRM_FORMAT_C8,
  52. DRM_FORMAT_RGB565,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_XRGB8888,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t i965_primary_formats[] = {
  58. DRM_FORMAT_C8,
  59. DRM_FORMAT_RGB565,
  60. DRM_FORMAT_XRGB8888,
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_XRGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. };
  65. static const uint32_t skl_primary_formats[] = {
  66. DRM_FORMAT_C8,
  67. DRM_FORMAT_RGB565,
  68. DRM_FORMAT_XRGB8888,
  69. DRM_FORMAT_XBGR8888,
  70. DRM_FORMAT_ARGB8888,
  71. DRM_FORMAT_ABGR8888,
  72. DRM_FORMAT_XRGB2101010,
  73. DRM_FORMAT_XBGR2101010,
  74. DRM_FORMAT_YUYV,
  75. DRM_FORMAT_YVYU,
  76. DRM_FORMAT_UYVY,
  77. DRM_FORMAT_VYUY,
  78. };
  79. /* Cursor formats */
  80. static const uint32_t intel_cursor_formats[] = {
  81. DRM_FORMAT_ARGB8888,
  82. };
  83. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  86. struct intel_crtc_state *pipe_config);
  87. static int intel_framebuffer_init(struct drm_device *dev,
  88. struct intel_framebuffer *ifb,
  89. struct drm_mode_fb_cmd2 *mode_cmd,
  90. struct drm_i915_gem_object *obj);
  91. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  92. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  93. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  94. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  95. struct intel_link_m_n *m_n,
  96. struct intel_link_m_n *m2_n2);
  97. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  98. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  99. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  100. static void vlv_prepare_pll(struct intel_crtc *crtc,
  101. const struct intel_crtc_state *pipe_config);
  102. static void chv_prepare_pll(struct intel_crtc *crtc,
  103. const struct intel_crtc_state *pipe_config);
  104. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  105. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  106. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  107. struct intel_crtc_state *crtc_state);
  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_noatomic(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. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  137. const char *name, u32 reg, int ref_freq)
  138. {
  139. u32 val;
  140. int divider;
  141. mutex_lock(&dev_priv->sb_lock);
  142. val = vlv_cck_read(dev_priv, reg);
  143. mutex_unlock(&dev_priv->sb_lock);
  144. divider = val & CCK_FREQUENCY_VALUES;
  145. WARN((val & CCK_FREQUENCY_STATUS) !=
  146. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  147. "%s change in progress\n", name);
  148. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  149. }
  150. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  151. const char *name, u32 reg)
  152. {
  153. if (dev_priv->hpll_freq == 0)
  154. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  155. return vlv_get_cck_clock(dev_priv, name, reg,
  156. dev_priv->hpll_freq);
  157. }
  158. static int
  159. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  160. {
  161. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  162. }
  163. static int
  164. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  165. {
  166. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  167. CCK_DISPLAY_REF_CLOCK_CONTROL);
  168. }
  169. static int
  170. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  171. {
  172. uint32_t clkcfg;
  173. /* hrawclock is 1/4 the FSB frequency */
  174. clkcfg = I915_READ(CLKCFG);
  175. switch (clkcfg & CLKCFG_FSB_MASK) {
  176. case CLKCFG_FSB_400:
  177. return 100000;
  178. case CLKCFG_FSB_533:
  179. return 133333;
  180. case CLKCFG_FSB_667:
  181. return 166667;
  182. case CLKCFG_FSB_800:
  183. return 200000;
  184. case CLKCFG_FSB_1067:
  185. return 266667;
  186. case CLKCFG_FSB_1333:
  187. return 333333;
  188. /* these two are just a guess; one of them might be right */
  189. case CLKCFG_FSB_1600:
  190. case CLKCFG_FSB_1600_ALT:
  191. return 400000;
  192. default:
  193. return 133333;
  194. }
  195. }
  196. static void intel_update_rawclk(struct drm_i915_private *dev_priv)
  197. {
  198. if (HAS_PCH_SPLIT(dev_priv))
  199. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  200. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  201. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  202. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  203. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  204. else
  205. return; /* no rawclk on other platforms, or no need to know it */
  206. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  207. }
  208. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  209. {
  210. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  211. return;
  212. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  213. CCK_CZ_CLOCK_CONTROL);
  214. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  215. }
  216. static inline u32 /* units of 100MHz */
  217. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  218. const struct intel_crtc_state *pipe_config)
  219. {
  220. if (HAS_DDI(dev_priv))
  221. return pipe_config->port_clock; /* SPLL */
  222. else if (IS_GEN5(dev_priv))
  223. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  224. else
  225. return 270000;
  226. }
  227. static const intel_limit_t intel_limits_i8xx_dac = {
  228. .dot = { .min = 25000, .max = 350000 },
  229. .vco = { .min = 908000, .max = 1512000 },
  230. .n = { .min = 2, .max = 16 },
  231. .m = { .min = 96, .max = 140 },
  232. .m1 = { .min = 18, .max = 26 },
  233. .m2 = { .min = 6, .max = 16 },
  234. .p = { .min = 4, .max = 128 },
  235. .p1 = { .min = 2, .max = 33 },
  236. .p2 = { .dot_limit = 165000,
  237. .p2_slow = 4, .p2_fast = 2 },
  238. };
  239. static const intel_limit_t intel_limits_i8xx_dvo = {
  240. .dot = { .min = 25000, .max = 350000 },
  241. .vco = { .min = 908000, .max = 1512000 },
  242. .n = { .min = 2, .max = 16 },
  243. .m = { .min = 96, .max = 140 },
  244. .m1 = { .min = 18, .max = 26 },
  245. .m2 = { .min = 6, .max = 16 },
  246. .p = { .min = 4, .max = 128 },
  247. .p1 = { .min = 2, .max = 33 },
  248. .p2 = { .dot_limit = 165000,
  249. .p2_slow = 4, .p2_fast = 4 },
  250. };
  251. static const intel_limit_t intel_limits_i8xx_lvds = {
  252. .dot = { .min = 25000, .max = 350000 },
  253. .vco = { .min = 908000, .max = 1512000 },
  254. .n = { .min = 2, .max = 16 },
  255. .m = { .min = 96, .max = 140 },
  256. .m1 = { .min = 18, .max = 26 },
  257. .m2 = { .min = 6, .max = 16 },
  258. .p = { .min = 4, .max = 128 },
  259. .p1 = { .min = 1, .max = 6 },
  260. .p2 = { .dot_limit = 165000,
  261. .p2_slow = 14, .p2_fast = 7 },
  262. };
  263. static const intel_limit_t intel_limits_i9xx_sdvo = {
  264. .dot = { .min = 20000, .max = 400000 },
  265. .vco = { .min = 1400000, .max = 2800000 },
  266. .n = { .min = 1, .max = 6 },
  267. .m = { .min = 70, .max = 120 },
  268. .m1 = { .min = 8, .max = 18 },
  269. .m2 = { .min = 3, .max = 7 },
  270. .p = { .min = 5, .max = 80 },
  271. .p1 = { .min = 1, .max = 8 },
  272. .p2 = { .dot_limit = 200000,
  273. .p2_slow = 10, .p2_fast = 5 },
  274. };
  275. static const intel_limit_t intel_limits_i9xx_lvds = {
  276. .dot = { .min = 20000, .max = 400000 },
  277. .vco = { .min = 1400000, .max = 2800000 },
  278. .n = { .min = 1, .max = 6 },
  279. .m = { .min = 70, .max = 120 },
  280. .m1 = { .min = 8, .max = 18 },
  281. .m2 = { .min = 3, .max = 7 },
  282. .p = { .min = 7, .max = 98 },
  283. .p1 = { .min = 1, .max = 8 },
  284. .p2 = { .dot_limit = 112000,
  285. .p2_slow = 14, .p2_fast = 7 },
  286. };
  287. static const intel_limit_t intel_limits_g4x_sdvo = {
  288. .dot = { .min = 25000, .max = 270000 },
  289. .vco = { .min = 1750000, .max = 3500000},
  290. .n = { .min = 1, .max = 4 },
  291. .m = { .min = 104, .max = 138 },
  292. .m1 = { .min = 17, .max = 23 },
  293. .m2 = { .min = 5, .max = 11 },
  294. .p = { .min = 10, .max = 30 },
  295. .p1 = { .min = 1, .max = 3},
  296. .p2 = { .dot_limit = 270000,
  297. .p2_slow = 10,
  298. .p2_fast = 10
  299. },
  300. };
  301. static const intel_limit_t intel_limits_g4x_hdmi = {
  302. .dot = { .min = 22000, .max = 400000 },
  303. .vco = { .min = 1750000, .max = 3500000},
  304. .n = { .min = 1, .max = 4 },
  305. .m = { .min = 104, .max = 138 },
  306. .m1 = { .min = 16, .max = 23 },
  307. .m2 = { .min = 5, .max = 11 },
  308. .p = { .min = 5, .max = 80 },
  309. .p1 = { .min = 1, .max = 8},
  310. .p2 = { .dot_limit = 165000,
  311. .p2_slow = 10, .p2_fast = 5 },
  312. };
  313. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  314. .dot = { .min = 20000, .max = 115000 },
  315. .vco = { .min = 1750000, .max = 3500000 },
  316. .n = { .min = 1, .max = 3 },
  317. .m = { .min = 104, .max = 138 },
  318. .m1 = { .min = 17, .max = 23 },
  319. .m2 = { .min = 5, .max = 11 },
  320. .p = { .min = 28, .max = 112 },
  321. .p1 = { .min = 2, .max = 8 },
  322. .p2 = { .dot_limit = 0,
  323. .p2_slow = 14, .p2_fast = 14
  324. },
  325. };
  326. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  327. .dot = { .min = 80000, .max = 224000 },
  328. .vco = { .min = 1750000, .max = 3500000 },
  329. .n = { .min = 1, .max = 3 },
  330. .m = { .min = 104, .max = 138 },
  331. .m1 = { .min = 17, .max = 23 },
  332. .m2 = { .min = 5, .max = 11 },
  333. .p = { .min = 14, .max = 42 },
  334. .p1 = { .min = 2, .max = 6 },
  335. .p2 = { .dot_limit = 0,
  336. .p2_slow = 7, .p2_fast = 7
  337. },
  338. };
  339. static const intel_limit_t intel_limits_pineview_sdvo = {
  340. .dot = { .min = 20000, .max = 400000},
  341. .vco = { .min = 1700000, .max = 3500000 },
  342. /* Pineview's Ncounter is a ring counter */
  343. .n = { .min = 3, .max = 6 },
  344. .m = { .min = 2, .max = 256 },
  345. /* Pineview only has one combined m divider, which we treat as m2. */
  346. .m1 = { .min = 0, .max = 0 },
  347. .m2 = { .min = 0, .max = 254 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 200000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const intel_limit_t intel_limits_pineview_lvds = {
  354. .dot = { .min = 20000, .max = 400000 },
  355. .vco = { .min = 1700000, .max = 3500000 },
  356. .n = { .min = 3, .max = 6 },
  357. .m = { .min = 2, .max = 256 },
  358. .m1 = { .min = 0, .max = 0 },
  359. .m2 = { .min = 0, .max = 254 },
  360. .p = { .min = 7, .max = 112 },
  361. .p1 = { .min = 1, .max = 8 },
  362. .p2 = { .dot_limit = 112000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. /* Ironlake / Sandybridge
  366. *
  367. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  368. * the range value for them is (actual_value - 2).
  369. */
  370. static const intel_limit_t intel_limits_ironlake_dac = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 5 },
  374. .m = { .min = 79, .max = 127 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 5, .max = 80 },
  378. .p1 = { .min = 1, .max = 8 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 10, .p2_fast = 5 },
  381. };
  382. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  383. .dot = { .min = 25000, .max = 350000 },
  384. .vco = { .min = 1760000, .max = 3510000 },
  385. .n = { .min = 1, .max = 3 },
  386. .m = { .min = 79, .max = 118 },
  387. .m1 = { .min = 12, .max = 22 },
  388. .m2 = { .min = 5, .max = 9 },
  389. .p = { .min = 28, .max = 112 },
  390. .p1 = { .min = 2, .max = 8 },
  391. .p2 = { .dot_limit = 225000,
  392. .p2_slow = 14, .p2_fast = 14 },
  393. };
  394. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  395. .dot = { .min = 25000, .max = 350000 },
  396. .vco = { .min = 1760000, .max = 3510000 },
  397. .n = { .min = 1, .max = 3 },
  398. .m = { .min = 79, .max = 127 },
  399. .m1 = { .min = 12, .max = 22 },
  400. .m2 = { .min = 5, .max = 9 },
  401. .p = { .min = 14, .max = 56 },
  402. .p1 = { .min = 2, .max = 8 },
  403. .p2 = { .dot_limit = 225000,
  404. .p2_slow = 7, .p2_fast = 7 },
  405. };
  406. /* LVDS 100mhz refclk limits. */
  407. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  408. .dot = { .min = 25000, .max = 350000 },
  409. .vco = { .min = 1760000, .max = 3510000 },
  410. .n = { .min = 1, .max = 2 },
  411. .m = { .min = 79, .max = 126 },
  412. .m1 = { .min = 12, .max = 22 },
  413. .m2 = { .min = 5, .max = 9 },
  414. .p = { .min = 28, .max = 112 },
  415. .p1 = { .min = 2, .max = 8 },
  416. .p2 = { .dot_limit = 225000,
  417. .p2_slow = 14, .p2_fast = 14 },
  418. };
  419. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  420. .dot = { .min = 25000, .max = 350000 },
  421. .vco = { .min = 1760000, .max = 3510000 },
  422. .n = { .min = 1, .max = 3 },
  423. .m = { .min = 79, .max = 126 },
  424. .m1 = { .min = 12, .max = 22 },
  425. .m2 = { .min = 5, .max = 9 },
  426. .p = { .min = 14, .max = 42 },
  427. .p1 = { .min = 2, .max = 6 },
  428. .p2 = { .dot_limit = 225000,
  429. .p2_slow = 7, .p2_fast = 7 },
  430. };
  431. static const intel_limit_t intel_limits_vlv = {
  432. /*
  433. * These are the data rate limits (measured in fast clocks)
  434. * since those are the strictest limits we have. The fast
  435. * clock and actual rate limits are more relaxed, so checking
  436. * them would make no difference.
  437. */
  438. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  439. .vco = { .min = 4000000, .max = 6000000 },
  440. .n = { .min = 1, .max = 7 },
  441. .m1 = { .min = 2, .max = 3 },
  442. .m2 = { .min = 11, .max = 156 },
  443. .p1 = { .min = 2, .max = 3 },
  444. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  445. };
  446. static const intel_limit_t intel_limits_chv = {
  447. /*
  448. * These are the data rate limits (measured in fast clocks)
  449. * since those are the strictest limits we have. The fast
  450. * clock and actual rate limits are more relaxed, so checking
  451. * them would make no difference.
  452. */
  453. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  454. .vco = { .min = 4800000, .max = 6480000 },
  455. .n = { .min = 1, .max = 1 },
  456. .m1 = { .min = 2, .max = 2 },
  457. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  458. .p1 = { .min = 2, .max = 4 },
  459. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  460. };
  461. static const intel_limit_t intel_limits_bxt = {
  462. /* FIXME: find real dot limits */
  463. .dot = { .min = 0, .max = INT_MAX },
  464. .vco = { .min = 4800000, .max = 6700000 },
  465. .n = { .min = 1, .max = 1 },
  466. .m1 = { .min = 2, .max = 2 },
  467. /* FIXME: find real m2 limits */
  468. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  469. .p1 = { .min = 2, .max = 4 },
  470. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  471. };
  472. static bool
  473. needs_modeset(struct drm_crtc_state *state)
  474. {
  475. return drm_atomic_crtc_needs_modeset(state);
  476. }
  477. /**
  478. * Returns whether any output on the specified pipe is of the specified type
  479. */
  480. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  481. {
  482. struct drm_device *dev = crtc->base.dev;
  483. struct intel_encoder *encoder;
  484. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  485. if (encoder->type == type)
  486. return true;
  487. return false;
  488. }
  489. /**
  490. * Returns whether any output on the specified pipe will have the specified
  491. * type after a staged modeset is complete, i.e., the same as
  492. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  493. * encoder->crtc.
  494. */
  495. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  496. int type)
  497. {
  498. struct drm_atomic_state *state = crtc_state->base.state;
  499. struct drm_connector *connector;
  500. struct drm_connector_state *connector_state;
  501. struct intel_encoder *encoder;
  502. int i, num_connectors = 0;
  503. for_each_connector_in_state(state, connector, connector_state, i) {
  504. if (connector_state->crtc != crtc_state->base.crtc)
  505. continue;
  506. num_connectors++;
  507. encoder = to_intel_encoder(connector_state->best_encoder);
  508. if (encoder->type == type)
  509. return true;
  510. }
  511. WARN_ON(num_connectors == 0);
  512. return false;
  513. }
  514. /*
  515. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  516. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  517. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  518. * The helpers' return value is the rate of the clock that is fed to the
  519. * display engine's pipe which can be the above fast dot clock rate or a
  520. * divided-down version of it.
  521. */
  522. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  523. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  524. {
  525. clock->m = clock->m2 + 2;
  526. clock->p = clock->p1 * clock->p2;
  527. if (WARN_ON(clock->n == 0 || clock->p == 0))
  528. return 0;
  529. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  530. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  531. return clock->dot;
  532. }
  533. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  534. {
  535. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  536. }
  537. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  538. {
  539. clock->m = i9xx_dpll_compute_m(clock);
  540. clock->p = clock->p1 * clock->p2;
  541. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  542. return 0;
  543. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  544. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  545. return clock->dot;
  546. }
  547. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  548. {
  549. clock->m = clock->m1 * clock->m2;
  550. clock->p = clock->p1 * clock->p2;
  551. if (WARN_ON(clock->n == 0 || clock->p == 0))
  552. return 0;
  553. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  554. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  555. return clock->dot / 5;
  556. }
  557. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  558. {
  559. clock->m = clock->m1 * clock->m2;
  560. clock->p = clock->p1 * clock->p2;
  561. if (WARN_ON(clock->n == 0 || clock->p == 0))
  562. return 0;
  563. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  564. clock->n << 22);
  565. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  566. return clock->dot / 5;
  567. }
  568. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  569. /**
  570. * Returns whether the given set of divisors are valid for a given refclk with
  571. * the given connectors.
  572. */
  573. static bool intel_PLL_is_valid(struct drm_device *dev,
  574. const intel_limit_t *limit,
  575. const intel_clock_t *clock)
  576. {
  577. if (clock->n < limit->n.min || limit->n.max < clock->n)
  578. INTELPllInvalid("n out of range\n");
  579. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  580. INTELPllInvalid("p1 out of range\n");
  581. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  582. INTELPllInvalid("m2 out of range\n");
  583. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  584. INTELPllInvalid("m1 out of range\n");
  585. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  586. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  587. if (clock->m1 <= clock->m2)
  588. INTELPllInvalid("m1 <= m2\n");
  589. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  590. if (clock->p < limit->p.min || limit->p.max < clock->p)
  591. INTELPllInvalid("p out of range\n");
  592. if (clock->m < limit->m.min || limit->m.max < clock->m)
  593. INTELPllInvalid("m out of range\n");
  594. }
  595. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  596. INTELPllInvalid("vco out of range\n");
  597. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  598. * connector, etc., rather than just a single range.
  599. */
  600. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  601. INTELPllInvalid("dot out of range\n");
  602. return true;
  603. }
  604. static int
  605. i9xx_select_p2_div(const intel_limit_t *limit,
  606. const struct intel_crtc_state *crtc_state,
  607. int target)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  611. /*
  612. * For LVDS just rely on its current settings for dual-channel.
  613. * We haven't figured out how to reliably set up different
  614. * single/dual channel state, if we even can.
  615. */
  616. if (intel_is_dual_link_lvds(dev))
  617. return limit->p2.p2_fast;
  618. else
  619. return limit->p2.p2_slow;
  620. } else {
  621. if (target < limit->p2.dot_limit)
  622. return limit->p2.p2_slow;
  623. else
  624. return limit->p2.p2_fast;
  625. }
  626. }
  627. /*
  628. * Returns a set of divisors for the desired target clock with the given
  629. * refclk, or FALSE. The returned values represent the clock equation:
  630. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  631. *
  632. * Target and reference clocks are specified in kHz.
  633. *
  634. * If match_clock is provided, then best_clock P divider must match the P
  635. * divider from @match_clock used for LVDS downclocking.
  636. */
  637. static bool
  638. i9xx_find_best_dpll(const intel_limit_t *limit,
  639. struct intel_crtc_state *crtc_state,
  640. int target, int refclk, intel_clock_t *match_clock,
  641. intel_clock_t *best_clock)
  642. {
  643. struct drm_device *dev = crtc_state->base.crtc->dev;
  644. intel_clock_t clock;
  645. int err = target;
  646. memset(best_clock, 0, sizeof(*best_clock));
  647. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  648. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  649. clock.m1++) {
  650. for (clock.m2 = limit->m2.min;
  651. clock.m2 <= limit->m2.max; clock.m2++) {
  652. if (clock.m2 >= clock.m1)
  653. break;
  654. for (clock.n = limit->n.min;
  655. clock.n <= limit->n.max; clock.n++) {
  656. for (clock.p1 = limit->p1.min;
  657. clock.p1 <= limit->p1.max; clock.p1++) {
  658. int this_err;
  659. i9xx_calc_dpll_params(refclk, &clock);
  660. if (!intel_PLL_is_valid(dev, limit,
  661. &clock))
  662. continue;
  663. if (match_clock &&
  664. clock.p != match_clock->p)
  665. continue;
  666. this_err = abs(clock.dot - target);
  667. if (this_err < err) {
  668. *best_clock = clock;
  669. err = this_err;
  670. }
  671. }
  672. }
  673. }
  674. }
  675. return (err != target);
  676. }
  677. /*
  678. * Returns a set of divisors for the desired target clock with the given
  679. * refclk, or FALSE. The returned values represent the clock equation:
  680. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  681. *
  682. * Target and reference clocks are specified in kHz.
  683. *
  684. * If match_clock is provided, then best_clock P divider must match the P
  685. * divider from @match_clock used for LVDS downclocking.
  686. */
  687. static bool
  688. pnv_find_best_dpll(const intel_limit_t *limit,
  689. struct intel_crtc_state *crtc_state,
  690. int target, int refclk, intel_clock_t *match_clock,
  691. intel_clock_t *best_clock)
  692. {
  693. struct drm_device *dev = crtc_state->base.crtc->dev;
  694. intel_clock_t clock;
  695. int err = target;
  696. memset(best_clock, 0, sizeof(*best_clock));
  697. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  698. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  699. clock.m1++) {
  700. for (clock.m2 = limit->m2.min;
  701. clock.m2 <= limit->m2.max; clock.m2++) {
  702. for (clock.n = limit->n.min;
  703. clock.n <= limit->n.max; clock.n++) {
  704. for (clock.p1 = limit->p1.min;
  705. clock.p1 <= limit->p1.max; clock.p1++) {
  706. int this_err;
  707. pnv_calc_dpll_params(refclk, &clock);
  708. if (!intel_PLL_is_valid(dev, limit,
  709. &clock))
  710. continue;
  711. if (match_clock &&
  712. clock.p != match_clock->p)
  713. continue;
  714. this_err = abs(clock.dot - target);
  715. if (this_err < err) {
  716. *best_clock = clock;
  717. err = this_err;
  718. }
  719. }
  720. }
  721. }
  722. }
  723. return (err != target);
  724. }
  725. /*
  726. * Returns a set of divisors for the desired target clock with the given
  727. * refclk, or FALSE. The returned values represent the clock equation:
  728. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  729. *
  730. * Target and reference clocks are specified in kHz.
  731. *
  732. * If match_clock is provided, then best_clock P divider must match the P
  733. * divider from @match_clock used for LVDS downclocking.
  734. */
  735. static bool
  736. g4x_find_best_dpll(const intel_limit_t *limit,
  737. struct intel_crtc_state *crtc_state,
  738. int target, int refclk, intel_clock_t *match_clock,
  739. intel_clock_t *best_clock)
  740. {
  741. struct drm_device *dev = crtc_state->base.crtc->dev;
  742. intel_clock_t clock;
  743. int max_n;
  744. bool found = false;
  745. /* approximately equals target * 0.00585 */
  746. int err_most = (target >> 8) + (target >> 9);
  747. memset(best_clock, 0, sizeof(*best_clock));
  748. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  749. max_n = limit->n.max;
  750. /* based on hardware requirement, prefer smaller n to precision */
  751. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  752. /* based on hardware requirement, prefere larger m1,m2 */
  753. for (clock.m1 = limit->m1.max;
  754. clock.m1 >= limit->m1.min; clock.m1--) {
  755. for (clock.m2 = limit->m2.max;
  756. clock.m2 >= limit->m2.min; clock.m2--) {
  757. for (clock.p1 = limit->p1.max;
  758. clock.p1 >= limit->p1.min; clock.p1--) {
  759. int this_err;
  760. i9xx_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(dev, limit,
  762. &clock))
  763. continue;
  764. this_err = abs(clock.dot - target);
  765. if (this_err < err_most) {
  766. *best_clock = clock;
  767. err_most = this_err;
  768. max_n = clock.n;
  769. found = true;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. return found;
  776. }
  777. /*
  778. * Check if the calculated PLL configuration is more optimal compared to the
  779. * best configuration and error found so far. Return the calculated error.
  780. */
  781. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  782. const intel_clock_t *calculated_clock,
  783. const intel_clock_t *best_clock,
  784. unsigned int best_error_ppm,
  785. unsigned int *error_ppm)
  786. {
  787. /*
  788. * For CHV ignore the error and consider only the P value.
  789. * Prefer a bigger P value based on HW requirements.
  790. */
  791. if (IS_CHERRYVIEW(dev)) {
  792. *error_ppm = 0;
  793. return calculated_clock->p > best_clock->p;
  794. }
  795. if (WARN_ON_ONCE(!target_freq))
  796. return false;
  797. *error_ppm = div_u64(1000000ULL *
  798. abs(target_freq - calculated_clock->dot),
  799. target_freq);
  800. /*
  801. * Prefer a better P value over a better (smaller) error if the error
  802. * is small. Ensure this preference for future configurations too by
  803. * setting the error to 0.
  804. */
  805. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  806. *error_ppm = 0;
  807. return true;
  808. }
  809. return *error_ppm + 10 < best_error_ppm;
  810. }
  811. /*
  812. * Returns a set of divisors for the desired target clock with the given
  813. * refclk, or FALSE. The returned values represent the clock equation:
  814. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  815. */
  816. static bool
  817. vlv_find_best_dpll(const intel_limit_t *limit,
  818. struct intel_crtc_state *crtc_state,
  819. int target, int refclk, intel_clock_t *match_clock,
  820. intel_clock_t *best_clock)
  821. {
  822. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  823. struct drm_device *dev = crtc->base.dev;
  824. intel_clock_t clock;
  825. unsigned int bestppm = 1000000;
  826. /* min update 19.2 MHz */
  827. int max_n = min(limit->n.max, refclk / 19200);
  828. bool found = false;
  829. target *= 5; /* fast clock */
  830. memset(best_clock, 0, sizeof(*best_clock));
  831. /* based on hardware requirement, prefer smaller n to precision */
  832. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  833. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  834. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  835. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  836. clock.p = clock.p1 * clock.p2;
  837. /* based on hardware requirement, prefer bigger m1,m2 values */
  838. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  839. unsigned int ppm;
  840. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  841. refclk * clock.m1);
  842. vlv_calc_dpll_params(refclk, &clock);
  843. if (!intel_PLL_is_valid(dev, limit,
  844. &clock))
  845. continue;
  846. if (!vlv_PLL_is_optimal(dev, target,
  847. &clock,
  848. best_clock,
  849. bestppm, &ppm))
  850. continue;
  851. *best_clock = clock;
  852. bestppm = ppm;
  853. found = true;
  854. }
  855. }
  856. }
  857. }
  858. return found;
  859. }
  860. /*
  861. * Returns a set of divisors for the desired target clock with the given
  862. * refclk, or FALSE. The returned values represent the clock equation:
  863. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  864. */
  865. static bool
  866. chv_find_best_dpll(const intel_limit_t *limit,
  867. struct intel_crtc_state *crtc_state,
  868. int target, int refclk, intel_clock_t *match_clock,
  869. intel_clock_t *best_clock)
  870. {
  871. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  872. struct drm_device *dev = crtc->base.dev;
  873. unsigned int best_error_ppm;
  874. intel_clock_t clock;
  875. uint64_t m2;
  876. int found = false;
  877. memset(best_clock, 0, sizeof(*best_clock));
  878. best_error_ppm = 1000000;
  879. /*
  880. * Based on hardware doc, the n always set to 1, and m1 always
  881. * set to 2. If requires to support 200Mhz refclk, we need to
  882. * revisit this because n may not 1 anymore.
  883. */
  884. clock.n = 1, clock.m1 = 2;
  885. target *= 5; /* fast clock */
  886. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  887. for (clock.p2 = limit->p2.p2_fast;
  888. clock.p2 >= limit->p2.p2_slow;
  889. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  890. unsigned int error_ppm;
  891. clock.p = clock.p1 * clock.p2;
  892. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  893. clock.n) << 22, refclk * clock.m1);
  894. if (m2 > INT_MAX/clock.m1)
  895. continue;
  896. clock.m2 = m2;
  897. chv_calc_dpll_params(refclk, &clock);
  898. if (!intel_PLL_is_valid(dev, limit, &clock))
  899. continue;
  900. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  901. best_error_ppm, &error_ppm))
  902. continue;
  903. *best_clock = clock;
  904. best_error_ppm = error_ppm;
  905. found = true;
  906. }
  907. }
  908. return found;
  909. }
  910. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  911. intel_clock_t *best_clock)
  912. {
  913. int refclk = 100000;
  914. const intel_limit_t *limit = &intel_limits_bxt;
  915. return chv_find_best_dpll(limit, crtc_state,
  916. target_clock, refclk, NULL, best_clock);
  917. }
  918. bool intel_crtc_active(struct drm_crtc *crtc)
  919. {
  920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  921. /* Be paranoid as we can arrive here with only partial
  922. * state retrieved from the hardware during setup.
  923. *
  924. * We can ditch the adjusted_mode.crtc_clock check as soon
  925. * as Haswell has gained clock readout/fastboot support.
  926. *
  927. * We can ditch the crtc->primary->fb check as soon as we can
  928. * properly reconstruct framebuffers.
  929. *
  930. * FIXME: The intel_crtc->active here should be switched to
  931. * crtc->state->active once we have proper CRTC states wired up
  932. * for atomic.
  933. */
  934. return intel_crtc->active && crtc->primary->state->fb &&
  935. intel_crtc->config->base.adjusted_mode.crtc_clock;
  936. }
  937. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  938. enum pipe pipe)
  939. {
  940. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  942. return intel_crtc->config->cpu_transcoder;
  943. }
  944. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  945. {
  946. struct drm_i915_private *dev_priv = dev->dev_private;
  947. i915_reg_t reg = PIPEDSL(pipe);
  948. u32 line1, line2;
  949. u32 line_mask;
  950. if (IS_GEN2(dev))
  951. line_mask = DSL_LINEMASK_GEN2;
  952. else
  953. line_mask = DSL_LINEMASK_GEN3;
  954. line1 = I915_READ(reg) & line_mask;
  955. msleep(5);
  956. line2 = I915_READ(reg) & line_mask;
  957. return line1 == line2;
  958. }
  959. /*
  960. * intel_wait_for_pipe_off - wait for pipe to turn off
  961. * @crtc: crtc whose pipe to wait for
  962. *
  963. * After disabling a pipe, we can't wait for vblank in the usual way,
  964. * spinning on the vblank interrupt status bit, since we won't actually
  965. * see an interrupt when the pipe is disabled.
  966. *
  967. * On Gen4 and above:
  968. * wait for the pipe register state bit to turn off
  969. *
  970. * Otherwise:
  971. * wait for the display line value to settle (it usually
  972. * ends up stopping at the start of the next frame).
  973. *
  974. */
  975. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  976. {
  977. struct drm_device *dev = crtc->base.dev;
  978. struct drm_i915_private *dev_priv = dev->dev_private;
  979. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  980. enum pipe pipe = crtc->pipe;
  981. if (INTEL_INFO(dev)->gen >= 4) {
  982. i915_reg_t reg = PIPECONF(cpu_transcoder);
  983. /* Wait for the Pipe State to go off */
  984. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  985. 100))
  986. WARN(1, "pipe_off wait timed out\n");
  987. } else {
  988. /* Wait for the display line to settle */
  989. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  990. WARN(1, "pipe_off wait timed out\n");
  991. }
  992. }
  993. /* Only for pre-ILK configs */
  994. void assert_pll(struct drm_i915_private *dev_priv,
  995. enum pipe pipe, bool state)
  996. {
  997. u32 val;
  998. bool cur_state;
  999. val = I915_READ(DPLL(pipe));
  1000. cur_state = !!(val & DPLL_VCO_ENABLE);
  1001. I915_STATE_WARN(cur_state != state,
  1002. "PLL state assertion failure (expected %s, current %s)\n",
  1003. onoff(state), onoff(cur_state));
  1004. }
  1005. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1006. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1007. {
  1008. u32 val;
  1009. bool cur_state;
  1010. mutex_lock(&dev_priv->sb_lock);
  1011. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1012. mutex_unlock(&dev_priv->sb_lock);
  1013. cur_state = val & DSI_PLL_VCO_EN;
  1014. I915_STATE_WARN(cur_state != state,
  1015. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1019. enum pipe pipe, bool state)
  1020. {
  1021. bool cur_state;
  1022. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1023. pipe);
  1024. if (HAS_DDI(dev_priv)) {
  1025. /* DDI does not have a specific FDI_TX register */
  1026. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1027. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1028. } else {
  1029. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1030. cur_state = !!(val & FDI_TX_ENABLE);
  1031. }
  1032. I915_STATE_WARN(cur_state != state,
  1033. "FDI TX state assertion failure (expected %s, current %s)\n",
  1034. onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1037. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1038. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. u32 val;
  1042. bool cur_state;
  1043. val = I915_READ(FDI_RX_CTL(pipe));
  1044. cur_state = !!(val & FDI_RX_ENABLE);
  1045. I915_STATE_WARN(cur_state != state,
  1046. "FDI RX state assertion failure (expected %s, current %s)\n",
  1047. onoff(state), onoff(cur_state));
  1048. }
  1049. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1050. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1051. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1052. enum pipe pipe)
  1053. {
  1054. u32 val;
  1055. /* ILK FDI PLL is always enabled */
  1056. if (INTEL_INFO(dev_priv)->gen == 5)
  1057. return;
  1058. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1059. if (HAS_DDI(dev_priv))
  1060. return;
  1061. val = I915_READ(FDI_TX_CTL(pipe));
  1062. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1063. }
  1064. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1065. enum pipe pipe, bool state)
  1066. {
  1067. u32 val;
  1068. bool cur_state;
  1069. val = I915_READ(FDI_RX_CTL(pipe));
  1070. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1071. I915_STATE_WARN(cur_state != state,
  1072. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1073. onoff(state), onoff(cur_state));
  1074. }
  1075. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe)
  1077. {
  1078. struct drm_device *dev = dev_priv->dev;
  1079. i915_reg_t pp_reg;
  1080. u32 val;
  1081. enum pipe panel_pipe = PIPE_A;
  1082. bool locked = true;
  1083. if (WARN_ON(HAS_DDI(dev)))
  1084. return;
  1085. if (HAS_PCH_SPLIT(dev)) {
  1086. u32 port_sel;
  1087. pp_reg = PCH_PP_CONTROL;
  1088. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1089. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1090. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1091. panel_pipe = PIPE_B;
  1092. /* XXX: else fix for eDP */
  1093. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1094. /* presumably write lock depends on pipe, not port select */
  1095. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1096. panel_pipe = pipe;
  1097. } else {
  1098. pp_reg = PP_CONTROL;
  1099. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1100. panel_pipe = PIPE_B;
  1101. }
  1102. val = I915_READ(pp_reg);
  1103. if (!(val & PANEL_POWER_ON) ||
  1104. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1105. locked = false;
  1106. I915_STATE_WARN(panel_pipe == pipe && locked,
  1107. "panel assertion failure, pipe %c regs locked\n",
  1108. pipe_name(pipe));
  1109. }
  1110. static void assert_cursor(struct drm_i915_private *dev_priv,
  1111. enum pipe pipe, bool state)
  1112. {
  1113. struct drm_device *dev = dev_priv->dev;
  1114. bool cur_state;
  1115. if (IS_845G(dev) || IS_I865G(dev))
  1116. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1117. else
  1118. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1119. I915_STATE_WARN(cur_state != state,
  1120. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1121. pipe_name(pipe), onoff(state), onoff(cur_state));
  1122. }
  1123. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1124. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1125. void assert_pipe(struct drm_i915_private *dev_priv,
  1126. enum pipe pipe, bool state)
  1127. {
  1128. bool cur_state;
  1129. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1130. pipe);
  1131. enum intel_display_power_domain power_domain;
  1132. /* if we need the pipe quirk it must be always on */
  1133. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1134. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1135. state = true;
  1136. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1137. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1138. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1139. cur_state = !!(val & PIPECONF_ENABLE);
  1140. intel_display_power_put(dev_priv, power_domain);
  1141. } else {
  1142. cur_state = false;
  1143. }
  1144. I915_STATE_WARN(cur_state != state,
  1145. "pipe %c assertion failure (expected %s, current %s)\n",
  1146. pipe_name(pipe), onoff(state), onoff(cur_state));
  1147. }
  1148. static void assert_plane(struct drm_i915_private *dev_priv,
  1149. enum plane plane, bool state)
  1150. {
  1151. u32 val;
  1152. bool cur_state;
  1153. val = I915_READ(DSPCNTR(plane));
  1154. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1155. I915_STATE_WARN(cur_state != state,
  1156. "plane %c assertion failure (expected %s, current %s)\n",
  1157. plane_name(plane), onoff(state), onoff(cur_state));
  1158. }
  1159. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1160. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1161. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1162. enum pipe pipe)
  1163. {
  1164. struct drm_device *dev = dev_priv->dev;
  1165. int i;
  1166. /* Primary planes are fixed to pipes on gen4+ */
  1167. if (INTEL_INFO(dev)->gen >= 4) {
  1168. u32 val = I915_READ(DSPCNTR(pipe));
  1169. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1170. "plane %c assertion failure, should be disabled but not\n",
  1171. plane_name(pipe));
  1172. return;
  1173. }
  1174. /* Need to check both planes against the pipe */
  1175. for_each_pipe(dev_priv, i) {
  1176. u32 val = I915_READ(DSPCNTR(i));
  1177. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1178. DISPPLANE_SEL_PIPE_SHIFT;
  1179. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1180. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(i), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1185. enum pipe pipe)
  1186. {
  1187. struct drm_device *dev = dev_priv->dev;
  1188. int sprite;
  1189. if (INTEL_INFO(dev)->gen >= 9) {
  1190. for_each_sprite(dev_priv, pipe, sprite) {
  1191. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1192. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1193. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1194. sprite, pipe_name(pipe));
  1195. }
  1196. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1197. for_each_sprite(dev_priv, pipe, sprite) {
  1198. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1199. I915_STATE_WARN(val & SP_ENABLE,
  1200. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1201. sprite_name(pipe, sprite), pipe_name(pipe));
  1202. }
  1203. } else if (INTEL_INFO(dev)->gen >= 7) {
  1204. u32 val = I915_READ(SPRCTL(pipe));
  1205. I915_STATE_WARN(val & SPRITE_ENABLE,
  1206. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1207. plane_name(pipe), pipe_name(pipe));
  1208. } else if (INTEL_INFO(dev)->gen >= 5) {
  1209. u32 val = I915_READ(DVSCNTR(pipe));
  1210. I915_STATE_WARN(val & DVS_ENABLE,
  1211. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1212. plane_name(pipe), pipe_name(pipe));
  1213. }
  1214. }
  1215. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1216. {
  1217. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1218. drm_crtc_vblank_put(crtc);
  1219. }
  1220. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1221. enum pipe pipe)
  1222. {
  1223. u32 val;
  1224. bool enabled;
  1225. val = I915_READ(PCH_TRANSCONF(pipe));
  1226. enabled = !!(val & TRANS_ENABLE);
  1227. I915_STATE_WARN(enabled,
  1228. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1229. pipe_name(pipe));
  1230. }
  1231. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 port_sel, u32 val)
  1233. {
  1234. if ((val & DP_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1238. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1239. return false;
  1240. } else if (IS_CHERRYVIEW(dev_priv)) {
  1241. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & SDVO_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1256. return false;
  1257. } else if (IS_CHERRYVIEW(dev_priv)) {
  1258. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1259. return false;
  1260. } else {
  1261. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1262. return false;
  1263. }
  1264. return true;
  1265. }
  1266. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1267. enum pipe pipe, u32 val)
  1268. {
  1269. if ((val & LVDS_PORT_EN) == 0)
  1270. return false;
  1271. if (HAS_PCH_CPT(dev_priv)) {
  1272. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1273. return false;
  1274. } else {
  1275. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1276. return false;
  1277. }
  1278. return true;
  1279. }
  1280. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1281. enum pipe pipe, u32 val)
  1282. {
  1283. if ((val & ADPA_DAC_ENABLE) == 0)
  1284. return false;
  1285. if (HAS_PCH_CPT(dev_priv)) {
  1286. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1287. return false;
  1288. } else {
  1289. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1295. enum pipe pipe, i915_reg_t reg,
  1296. u32 port_sel)
  1297. {
  1298. u32 val = I915_READ(reg);
  1299. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1300. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1301. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1302. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1303. && (val & DP_PIPEB_SELECT),
  1304. "IBX PCH dp port still using transcoder B\n");
  1305. }
  1306. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1307. enum pipe pipe, i915_reg_t reg)
  1308. {
  1309. u32 val = I915_READ(reg);
  1310. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1311. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1312. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1313. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1314. && (val & SDVO_PIPE_B_SELECT),
  1315. "IBX PCH hdmi port still using transcoder B\n");
  1316. }
  1317. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1318. enum pipe pipe)
  1319. {
  1320. u32 val;
  1321. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1322. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1323. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1324. val = I915_READ(PCH_ADPA);
  1325. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1326. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1327. pipe_name(pipe));
  1328. val = I915_READ(PCH_LVDS);
  1329. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1330. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1331. pipe_name(pipe));
  1332. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1333. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1334. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1335. }
  1336. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1337. const struct intel_crtc_state *pipe_config)
  1338. {
  1339. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1340. enum pipe pipe = crtc->pipe;
  1341. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1342. POSTING_READ(DPLL(pipe));
  1343. udelay(150);
  1344. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1345. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1346. }
  1347. static void vlv_enable_pll(struct intel_crtc *crtc,
  1348. const struct intel_crtc_state *pipe_config)
  1349. {
  1350. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1351. enum pipe pipe = crtc->pipe;
  1352. assert_pipe_disabled(dev_priv, pipe);
  1353. /* PLL is protected by panel, make sure we can write it */
  1354. assert_panel_unlocked(dev_priv, pipe);
  1355. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1356. _vlv_enable_pll(crtc, pipe_config);
  1357. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1358. POSTING_READ(DPLL_MD(pipe));
  1359. }
  1360. static void _chv_enable_pll(struct intel_crtc *crtc,
  1361. const struct intel_crtc_state *pipe_config)
  1362. {
  1363. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1364. enum pipe pipe = crtc->pipe;
  1365. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1366. u32 tmp;
  1367. mutex_lock(&dev_priv->sb_lock);
  1368. /* Enable back the 10bit clock to display controller */
  1369. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1370. tmp |= DPIO_DCLKP_EN;
  1371. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1372. mutex_unlock(&dev_priv->sb_lock);
  1373. /*
  1374. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1375. */
  1376. udelay(1);
  1377. /* Enable PLL */
  1378. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1379. /* Check PLL is locked */
  1380. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1381. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1382. }
  1383. static void chv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1387. enum pipe pipe = crtc->pipe;
  1388. assert_pipe_disabled(dev_priv, pipe);
  1389. /* PLL is protected by panel, make sure we can write it */
  1390. assert_panel_unlocked(dev_priv, pipe);
  1391. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1392. _chv_enable_pll(crtc, pipe_config);
  1393. if (pipe != PIPE_A) {
  1394. /*
  1395. * WaPixelRepeatModeFixForC0:chv
  1396. *
  1397. * DPLLCMD is AWOL. Use chicken bits to propagate
  1398. * the value from DPLLBMD to either pipe B or C.
  1399. */
  1400. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1401. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1402. I915_WRITE(CBR4_VLV, 0);
  1403. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1404. /*
  1405. * DPLLB VGA mode also seems to cause problems.
  1406. * We should always have it disabled.
  1407. */
  1408. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1409. } else {
  1410. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1411. POSTING_READ(DPLL_MD(pipe));
  1412. }
  1413. }
  1414. static int intel_num_dvo_pipes(struct drm_device *dev)
  1415. {
  1416. struct intel_crtc *crtc;
  1417. int count = 0;
  1418. for_each_intel_crtc(dev, crtc)
  1419. count += crtc->base.state->active &&
  1420. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1421. return count;
  1422. }
  1423. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1424. {
  1425. struct drm_device *dev = crtc->base.dev;
  1426. struct drm_i915_private *dev_priv = dev->dev_private;
  1427. i915_reg_t reg = DPLL(crtc->pipe);
  1428. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1429. assert_pipe_disabled(dev_priv, crtc->pipe);
  1430. /* PLL is protected by panel, make sure we can write it */
  1431. if (IS_MOBILE(dev) && !IS_I830(dev))
  1432. assert_panel_unlocked(dev_priv, crtc->pipe);
  1433. /* Enable DVO 2x clock on both PLLs if necessary */
  1434. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1435. /*
  1436. * It appears to be important that we don't enable this
  1437. * for the current pipe before otherwise configuring the
  1438. * PLL. No idea how this should be handled if multiple
  1439. * DVO outputs are enabled simultaneosly.
  1440. */
  1441. dpll |= DPLL_DVO_2X_MODE;
  1442. I915_WRITE(DPLL(!crtc->pipe),
  1443. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1444. }
  1445. /*
  1446. * Apparently we need to have VGA mode enabled prior to changing
  1447. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1448. * dividers, even though the register value does change.
  1449. */
  1450. I915_WRITE(reg, 0);
  1451. I915_WRITE(reg, dpll);
  1452. /* Wait for the clocks to stabilize. */
  1453. POSTING_READ(reg);
  1454. udelay(150);
  1455. if (INTEL_INFO(dev)->gen >= 4) {
  1456. I915_WRITE(DPLL_MD(crtc->pipe),
  1457. crtc->config->dpll_hw_state.dpll_md);
  1458. } else {
  1459. /* The pixel multiplier can only be updated once the
  1460. * DPLL is enabled and the clocks are stable.
  1461. *
  1462. * So write it again.
  1463. */
  1464. I915_WRITE(reg, dpll);
  1465. }
  1466. /* We do this three times for luck */
  1467. I915_WRITE(reg, dpll);
  1468. POSTING_READ(reg);
  1469. udelay(150); /* wait for warmup */
  1470. I915_WRITE(reg, dpll);
  1471. POSTING_READ(reg);
  1472. udelay(150); /* wait for warmup */
  1473. I915_WRITE(reg, dpll);
  1474. POSTING_READ(reg);
  1475. udelay(150); /* wait for warmup */
  1476. }
  1477. /**
  1478. * i9xx_disable_pll - disable a PLL
  1479. * @dev_priv: i915 private structure
  1480. * @pipe: pipe PLL to disable
  1481. *
  1482. * Disable the PLL for @pipe, making sure the pipe is off first.
  1483. *
  1484. * Note! This is for pre-ILK only.
  1485. */
  1486. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1487. {
  1488. struct drm_device *dev = crtc->base.dev;
  1489. struct drm_i915_private *dev_priv = dev->dev_private;
  1490. enum pipe pipe = crtc->pipe;
  1491. /* Disable DVO 2x clock on both PLLs if necessary */
  1492. if (IS_I830(dev) &&
  1493. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1494. !intel_num_dvo_pipes(dev)) {
  1495. I915_WRITE(DPLL(PIPE_B),
  1496. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1497. I915_WRITE(DPLL(PIPE_A),
  1498. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1499. }
  1500. /* Don't disable pipe or pipe PLLs if needed */
  1501. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1502. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1503. return;
  1504. /* Make sure the pipe isn't still relying on us */
  1505. assert_pipe_disabled(dev_priv, pipe);
  1506. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1507. POSTING_READ(DPLL(pipe));
  1508. }
  1509. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1510. {
  1511. u32 val;
  1512. /* Make sure the pipe isn't still relying on us */
  1513. assert_pipe_disabled(dev_priv, pipe);
  1514. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1515. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1516. if (pipe != PIPE_A)
  1517. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1518. I915_WRITE(DPLL(pipe), val);
  1519. POSTING_READ(DPLL(pipe));
  1520. }
  1521. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1522. {
  1523. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1524. u32 val;
  1525. /* Make sure the pipe isn't still relying on us */
  1526. assert_pipe_disabled(dev_priv, pipe);
  1527. val = DPLL_SSC_REF_CLK_CHV |
  1528. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1529. if (pipe != PIPE_A)
  1530. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1531. I915_WRITE(DPLL(pipe), val);
  1532. POSTING_READ(DPLL(pipe));
  1533. mutex_lock(&dev_priv->sb_lock);
  1534. /* Disable 10bit clock to display controller */
  1535. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1536. val &= ~DPIO_DCLKP_EN;
  1537. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1538. mutex_unlock(&dev_priv->sb_lock);
  1539. }
  1540. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1541. struct intel_digital_port *dport,
  1542. unsigned int expected_mask)
  1543. {
  1544. u32 port_mask;
  1545. i915_reg_t dpll_reg;
  1546. switch (dport->port) {
  1547. case PORT_B:
  1548. port_mask = DPLL_PORTB_READY_MASK;
  1549. dpll_reg = DPLL(0);
  1550. break;
  1551. case PORT_C:
  1552. port_mask = DPLL_PORTC_READY_MASK;
  1553. dpll_reg = DPLL(0);
  1554. expected_mask <<= 4;
  1555. break;
  1556. case PORT_D:
  1557. port_mask = DPLL_PORTD_READY_MASK;
  1558. dpll_reg = DPIO_PHY_STATUS;
  1559. break;
  1560. default:
  1561. BUG();
  1562. }
  1563. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1564. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1565. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1566. }
  1567. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1568. enum pipe pipe)
  1569. {
  1570. struct drm_device *dev = dev_priv->dev;
  1571. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1572. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1573. i915_reg_t reg;
  1574. uint32_t val, pipeconf_val;
  1575. /* Make sure PCH DPLL is enabled */
  1576. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1577. /* FDI must be feeding us bits for PCH ports */
  1578. assert_fdi_tx_enabled(dev_priv, pipe);
  1579. assert_fdi_rx_enabled(dev_priv, pipe);
  1580. if (HAS_PCH_CPT(dev)) {
  1581. /* Workaround: Set the timing override bit before enabling the
  1582. * pch transcoder. */
  1583. reg = TRANS_CHICKEN2(pipe);
  1584. val = I915_READ(reg);
  1585. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1586. I915_WRITE(reg, val);
  1587. }
  1588. reg = PCH_TRANSCONF(pipe);
  1589. val = I915_READ(reg);
  1590. pipeconf_val = I915_READ(PIPECONF(pipe));
  1591. if (HAS_PCH_IBX(dev_priv)) {
  1592. /*
  1593. * Make the BPC in transcoder be consistent with
  1594. * that in pipeconf reg. For HDMI we must use 8bpc
  1595. * here for both 8bpc and 12bpc.
  1596. */
  1597. val &= ~PIPECONF_BPC_MASK;
  1598. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1599. val |= PIPECONF_8BPC;
  1600. else
  1601. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1602. }
  1603. val &= ~TRANS_INTERLACE_MASK;
  1604. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1605. if (HAS_PCH_IBX(dev_priv) &&
  1606. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1607. val |= TRANS_LEGACY_INTERLACED_ILK;
  1608. else
  1609. val |= TRANS_INTERLACED;
  1610. else
  1611. val |= TRANS_PROGRESSIVE;
  1612. I915_WRITE(reg, val | TRANS_ENABLE);
  1613. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1614. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1615. }
  1616. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1617. enum transcoder cpu_transcoder)
  1618. {
  1619. u32 val, pipeconf_val;
  1620. /* FDI must be feeding us bits for PCH ports */
  1621. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1622. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1623. /* Workaround: set timing override bit. */
  1624. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1625. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1626. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1627. val = TRANS_ENABLE;
  1628. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1629. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1630. PIPECONF_INTERLACED_ILK)
  1631. val |= TRANS_INTERLACED;
  1632. else
  1633. val |= TRANS_PROGRESSIVE;
  1634. I915_WRITE(LPT_TRANSCONF, val);
  1635. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1636. DRM_ERROR("Failed to enable PCH transcoder\n");
  1637. }
  1638. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1639. enum pipe pipe)
  1640. {
  1641. struct drm_device *dev = dev_priv->dev;
  1642. i915_reg_t reg;
  1643. uint32_t val;
  1644. /* FDI relies on the transcoder */
  1645. assert_fdi_tx_disabled(dev_priv, pipe);
  1646. assert_fdi_rx_disabled(dev_priv, pipe);
  1647. /* Ports must be off as well */
  1648. assert_pch_ports_disabled(dev_priv, pipe);
  1649. reg = PCH_TRANSCONF(pipe);
  1650. val = I915_READ(reg);
  1651. val &= ~TRANS_ENABLE;
  1652. I915_WRITE(reg, val);
  1653. /* wait for PCH transcoder off, transcoder state */
  1654. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1655. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1656. if (HAS_PCH_CPT(dev)) {
  1657. /* Workaround: Clear the timing override chicken bit again. */
  1658. reg = TRANS_CHICKEN2(pipe);
  1659. val = I915_READ(reg);
  1660. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1661. I915_WRITE(reg, val);
  1662. }
  1663. }
  1664. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1665. {
  1666. u32 val;
  1667. val = I915_READ(LPT_TRANSCONF);
  1668. val &= ~TRANS_ENABLE;
  1669. I915_WRITE(LPT_TRANSCONF, val);
  1670. /* wait for PCH transcoder off, transcoder state */
  1671. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1672. DRM_ERROR("Failed to disable PCH transcoder\n");
  1673. /* Workaround: clear timing override bit. */
  1674. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1675. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1676. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1677. }
  1678. /**
  1679. * intel_enable_pipe - enable a pipe, asserting requirements
  1680. * @crtc: crtc responsible for the pipe
  1681. *
  1682. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1683. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1684. */
  1685. static void intel_enable_pipe(struct intel_crtc *crtc)
  1686. {
  1687. struct drm_device *dev = crtc->base.dev;
  1688. struct drm_i915_private *dev_priv = dev->dev_private;
  1689. enum pipe pipe = crtc->pipe;
  1690. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1691. enum pipe pch_transcoder;
  1692. i915_reg_t reg;
  1693. u32 val;
  1694. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1695. assert_planes_disabled(dev_priv, pipe);
  1696. assert_cursor_disabled(dev_priv, pipe);
  1697. assert_sprites_disabled(dev_priv, pipe);
  1698. if (HAS_PCH_LPT(dev_priv))
  1699. pch_transcoder = TRANSCODER_A;
  1700. else
  1701. pch_transcoder = pipe;
  1702. /*
  1703. * A pipe without a PLL won't actually be able to drive bits from
  1704. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1705. * need the check.
  1706. */
  1707. if (HAS_GMCH_DISPLAY(dev_priv))
  1708. if (crtc->config->has_dsi_encoder)
  1709. assert_dsi_pll_enabled(dev_priv);
  1710. else
  1711. assert_pll_enabled(dev_priv, pipe);
  1712. else {
  1713. if (crtc->config->has_pch_encoder) {
  1714. /* if driving the PCH, we need FDI enabled */
  1715. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1716. assert_fdi_tx_pll_enabled(dev_priv,
  1717. (enum pipe) cpu_transcoder);
  1718. }
  1719. /* FIXME: assert CPU port conditions for SNB+ */
  1720. }
  1721. reg = PIPECONF(cpu_transcoder);
  1722. val = I915_READ(reg);
  1723. if (val & PIPECONF_ENABLE) {
  1724. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1725. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1726. return;
  1727. }
  1728. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1729. POSTING_READ(reg);
  1730. /*
  1731. * Until the pipe starts DSL will read as 0, which would cause
  1732. * an apparent vblank timestamp jump, which messes up also the
  1733. * frame count when it's derived from the timestamps. So let's
  1734. * wait for the pipe to start properly before we call
  1735. * drm_crtc_vblank_on()
  1736. */
  1737. if (dev->max_vblank_count == 0 &&
  1738. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1739. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1740. }
  1741. /**
  1742. * intel_disable_pipe - disable a pipe, asserting requirements
  1743. * @crtc: crtc whose pipes is to be disabled
  1744. *
  1745. * Disable the pipe of @crtc, making sure that various hardware
  1746. * specific requirements are met, if applicable, e.g. plane
  1747. * disabled, panel fitter off, etc.
  1748. *
  1749. * Will wait until the pipe has shut down before returning.
  1750. */
  1751. static void intel_disable_pipe(struct intel_crtc *crtc)
  1752. {
  1753. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1754. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1755. enum pipe pipe = crtc->pipe;
  1756. i915_reg_t reg;
  1757. u32 val;
  1758. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1759. /*
  1760. * Make sure planes won't keep trying to pump pixels to us,
  1761. * or we might hang the display.
  1762. */
  1763. assert_planes_disabled(dev_priv, pipe);
  1764. assert_cursor_disabled(dev_priv, pipe);
  1765. assert_sprites_disabled(dev_priv, pipe);
  1766. reg = PIPECONF(cpu_transcoder);
  1767. val = I915_READ(reg);
  1768. if ((val & PIPECONF_ENABLE) == 0)
  1769. return;
  1770. /*
  1771. * Double wide has implications for planes
  1772. * so best keep it disabled when not needed.
  1773. */
  1774. if (crtc->config->double_wide)
  1775. val &= ~PIPECONF_DOUBLE_WIDE;
  1776. /* Don't disable pipe or pipe PLLs if needed */
  1777. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1778. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1779. val &= ~PIPECONF_ENABLE;
  1780. I915_WRITE(reg, val);
  1781. if ((val & PIPECONF_ENABLE) == 0)
  1782. intel_wait_for_pipe_off(crtc);
  1783. }
  1784. static bool need_vtd_wa(struct drm_device *dev)
  1785. {
  1786. #ifdef CONFIG_INTEL_IOMMU
  1787. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1788. return true;
  1789. #endif
  1790. return false;
  1791. }
  1792. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1793. {
  1794. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1795. }
  1796. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1797. uint64_t fb_modifier, unsigned int cpp)
  1798. {
  1799. switch (fb_modifier) {
  1800. case DRM_FORMAT_MOD_NONE:
  1801. return cpp;
  1802. case I915_FORMAT_MOD_X_TILED:
  1803. if (IS_GEN2(dev_priv))
  1804. return 128;
  1805. else
  1806. return 512;
  1807. case I915_FORMAT_MOD_Y_TILED:
  1808. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1809. return 128;
  1810. else
  1811. return 512;
  1812. case I915_FORMAT_MOD_Yf_TILED:
  1813. switch (cpp) {
  1814. case 1:
  1815. return 64;
  1816. case 2:
  1817. case 4:
  1818. return 128;
  1819. case 8:
  1820. case 16:
  1821. return 256;
  1822. default:
  1823. MISSING_CASE(cpp);
  1824. return cpp;
  1825. }
  1826. break;
  1827. default:
  1828. MISSING_CASE(fb_modifier);
  1829. return cpp;
  1830. }
  1831. }
  1832. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1833. uint64_t fb_modifier, unsigned int cpp)
  1834. {
  1835. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1836. return 1;
  1837. else
  1838. return intel_tile_size(dev_priv) /
  1839. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1840. }
  1841. /* Return the tile dimensions in pixel units */
  1842. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1843. unsigned int *tile_width,
  1844. unsigned int *tile_height,
  1845. uint64_t fb_modifier,
  1846. unsigned int cpp)
  1847. {
  1848. unsigned int tile_width_bytes =
  1849. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1850. *tile_width = tile_width_bytes / cpp;
  1851. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1852. }
  1853. unsigned int
  1854. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1855. uint32_t pixel_format, uint64_t fb_modifier)
  1856. {
  1857. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1858. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1859. return ALIGN(height, tile_height);
  1860. }
  1861. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1862. {
  1863. unsigned int size = 0;
  1864. int i;
  1865. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1866. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1867. return size;
  1868. }
  1869. static void
  1870. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1871. const struct drm_framebuffer *fb,
  1872. unsigned int rotation)
  1873. {
  1874. if (intel_rotation_90_or_270(rotation)) {
  1875. *view = i915_ggtt_view_rotated;
  1876. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1877. } else {
  1878. *view = i915_ggtt_view_normal;
  1879. }
  1880. }
  1881. static void
  1882. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1883. struct drm_framebuffer *fb)
  1884. {
  1885. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1886. unsigned int tile_size, tile_width, tile_height, cpp;
  1887. tile_size = intel_tile_size(dev_priv);
  1888. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1889. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1890. fb->modifier[0], cpp);
  1891. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1892. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1893. if (info->pixel_format == DRM_FORMAT_NV12) {
  1894. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1895. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1896. fb->modifier[1], cpp);
  1897. info->uv_offset = fb->offsets[1];
  1898. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1899. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1900. }
  1901. }
  1902. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1903. {
  1904. if (INTEL_INFO(dev_priv)->gen >= 9)
  1905. return 256 * 1024;
  1906. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1907. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1908. return 128 * 1024;
  1909. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1910. return 4 * 1024;
  1911. else
  1912. return 0;
  1913. }
  1914. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1915. uint64_t fb_modifier)
  1916. {
  1917. switch (fb_modifier) {
  1918. case DRM_FORMAT_MOD_NONE:
  1919. return intel_linear_alignment(dev_priv);
  1920. case I915_FORMAT_MOD_X_TILED:
  1921. if (INTEL_INFO(dev_priv)->gen >= 9)
  1922. return 256 * 1024;
  1923. return 0;
  1924. case I915_FORMAT_MOD_Y_TILED:
  1925. case I915_FORMAT_MOD_Yf_TILED:
  1926. return 1 * 1024 * 1024;
  1927. default:
  1928. MISSING_CASE(fb_modifier);
  1929. return 0;
  1930. }
  1931. }
  1932. int
  1933. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1934. unsigned int rotation)
  1935. {
  1936. struct drm_device *dev = fb->dev;
  1937. struct drm_i915_private *dev_priv = dev->dev_private;
  1938. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1939. struct i915_ggtt_view view;
  1940. u32 alignment;
  1941. int ret;
  1942. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1943. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1944. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1945. /* Note that the w/a also requires 64 PTE of padding following the
  1946. * bo. We currently fill all unused PTE with the shadow page and so
  1947. * we should always have valid PTE following the scanout preventing
  1948. * the VT-d warning.
  1949. */
  1950. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1951. alignment = 256 * 1024;
  1952. /*
  1953. * Global gtt pte registers are special registers which actually forward
  1954. * writes to a chunk of system memory. Which means that there is no risk
  1955. * that the register values disappear as soon as we call
  1956. * intel_runtime_pm_put(), so it is correct to wrap only the
  1957. * pin/unpin/fence and not more.
  1958. */
  1959. intel_runtime_pm_get(dev_priv);
  1960. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1961. &view);
  1962. if (ret)
  1963. goto err_pm;
  1964. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1965. * fence, whereas 965+ only requires a fence if using
  1966. * framebuffer compression. For simplicity, we always install
  1967. * a fence as the cost is not that onerous.
  1968. */
  1969. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1970. ret = i915_gem_object_get_fence(obj);
  1971. if (ret == -EDEADLK) {
  1972. /*
  1973. * -EDEADLK means there are no free fences
  1974. * no pending flips.
  1975. *
  1976. * This is propagated to atomic, but it uses
  1977. * -EDEADLK to force a locking recovery, so
  1978. * change the returned error to -EBUSY.
  1979. */
  1980. ret = -EBUSY;
  1981. goto err_unpin;
  1982. } else if (ret)
  1983. goto err_unpin;
  1984. i915_gem_object_pin_fence(obj);
  1985. }
  1986. intel_runtime_pm_put(dev_priv);
  1987. return 0;
  1988. err_unpin:
  1989. i915_gem_object_unpin_from_display_plane(obj, &view);
  1990. err_pm:
  1991. intel_runtime_pm_put(dev_priv);
  1992. return ret;
  1993. }
  1994. static void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1995. {
  1996. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1997. struct i915_ggtt_view view;
  1998. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1999. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2000. if (view.type == I915_GGTT_VIEW_NORMAL)
  2001. i915_gem_object_unpin_fence(obj);
  2002. i915_gem_object_unpin_from_display_plane(obj, &view);
  2003. }
  2004. /*
  2005. * Adjust the tile offset by moving the difference into
  2006. * the x/y offsets.
  2007. *
  2008. * Input tile dimensions and pitch must already be
  2009. * rotated to match x and y, and in pixel units.
  2010. */
  2011. static u32 intel_adjust_tile_offset(int *x, int *y,
  2012. unsigned int tile_width,
  2013. unsigned int tile_height,
  2014. unsigned int tile_size,
  2015. unsigned int pitch_tiles,
  2016. u32 old_offset,
  2017. u32 new_offset)
  2018. {
  2019. unsigned int tiles;
  2020. WARN_ON(old_offset & (tile_size - 1));
  2021. WARN_ON(new_offset & (tile_size - 1));
  2022. WARN_ON(new_offset > old_offset);
  2023. tiles = (old_offset - new_offset) / tile_size;
  2024. *y += tiles / pitch_tiles * tile_height;
  2025. *x += tiles % pitch_tiles * tile_width;
  2026. return new_offset;
  2027. }
  2028. /*
  2029. * Computes the linear offset to the base tile and adjusts
  2030. * x, y. bytes per pixel is assumed to be a power-of-two.
  2031. *
  2032. * In the 90/270 rotated case, x and y are assumed
  2033. * to be already rotated to match the rotated GTT view, and
  2034. * pitch is the tile_height aligned framebuffer height.
  2035. */
  2036. u32 intel_compute_tile_offset(int *x, int *y,
  2037. const struct drm_framebuffer *fb, int plane,
  2038. unsigned int pitch,
  2039. unsigned int rotation)
  2040. {
  2041. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2042. uint64_t fb_modifier = fb->modifier[plane];
  2043. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2044. u32 offset, offset_aligned, alignment;
  2045. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2046. if (alignment)
  2047. alignment--;
  2048. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2049. unsigned int tile_size, tile_width, tile_height;
  2050. unsigned int tile_rows, tiles, pitch_tiles;
  2051. tile_size = intel_tile_size(dev_priv);
  2052. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2053. fb_modifier, cpp);
  2054. if (intel_rotation_90_or_270(rotation)) {
  2055. pitch_tiles = pitch / tile_height;
  2056. swap(tile_width, tile_height);
  2057. } else {
  2058. pitch_tiles = pitch / (tile_width * cpp);
  2059. }
  2060. tile_rows = *y / tile_height;
  2061. *y %= tile_height;
  2062. tiles = *x / tile_width;
  2063. *x %= tile_width;
  2064. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2065. offset_aligned = offset & ~alignment;
  2066. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2067. tile_size, pitch_tiles,
  2068. offset, offset_aligned);
  2069. } else {
  2070. offset = *y * pitch + *x * cpp;
  2071. offset_aligned = offset & ~alignment;
  2072. *y = (offset & alignment) / pitch;
  2073. *x = ((offset & alignment) - *y * pitch) / cpp;
  2074. }
  2075. return offset_aligned;
  2076. }
  2077. static int i9xx_format_to_fourcc(int format)
  2078. {
  2079. switch (format) {
  2080. case DISPPLANE_8BPP:
  2081. return DRM_FORMAT_C8;
  2082. case DISPPLANE_BGRX555:
  2083. return DRM_FORMAT_XRGB1555;
  2084. case DISPPLANE_BGRX565:
  2085. return DRM_FORMAT_RGB565;
  2086. default:
  2087. case DISPPLANE_BGRX888:
  2088. return DRM_FORMAT_XRGB8888;
  2089. case DISPPLANE_RGBX888:
  2090. return DRM_FORMAT_XBGR8888;
  2091. case DISPPLANE_BGRX101010:
  2092. return DRM_FORMAT_XRGB2101010;
  2093. case DISPPLANE_RGBX101010:
  2094. return DRM_FORMAT_XBGR2101010;
  2095. }
  2096. }
  2097. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2098. {
  2099. switch (format) {
  2100. case PLANE_CTL_FORMAT_RGB_565:
  2101. return DRM_FORMAT_RGB565;
  2102. default:
  2103. case PLANE_CTL_FORMAT_XRGB_8888:
  2104. if (rgb_order) {
  2105. if (alpha)
  2106. return DRM_FORMAT_ABGR8888;
  2107. else
  2108. return DRM_FORMAT_XBGR8888;
  2109. } else {
  2110. if (alpha)
  2111. return DRM_FORMAT_ARGB8888;
  2112. else
  2113. return DRM_FORMAT_XRGB8888;
  2114. }
  2115. case PLANE_CTL_FORMAT_XRGB_2101010:
  2116. if (rgb_order)
  2117. return DRM_FORMAT_XBGR2101010;
  2118. else
  2119. return DRM_FORMAT_XRGB2101010;
  2120. }
  2121. }
  2122. static bool
  2123. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2124. struct intel_initial_plane_config *plane_config)
  2125. {
  2126. struct drm_device *dev = crtc->base.dev;
  2127. struct drm_i915_private *dev_priv = to_i915(dev);
  2128. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2129. struct drm_i915_gem_object *obj = NULL;
  2130. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2131. struct drm_framebuffer *fb = &plane_config->fb->base;
  2132. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2133. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2134. PAGE_SIZE);
  2135. size_aligned -= base_aligned;
  2136. if (plane_config->size == 0)
  2137. return false;
  2138. /* If the FB is too big, just don't use it since fbdev is not very
  2139. * important and we should probably use that space with FBC or other
  2140. * features. */
  2141. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2142. return false;
  2143. mutex_lock(&dev->struct_mutex);
  2144. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2145. base_aligned,
  2146. base_aligned,
  2147. size_aligned);
  2148. if (!obj) {
  2149. mutex_unlock(&dev->struct_mutex);
  2150. return false;
  2151. }
  2152. obj->tiling_mode = plane_config->tiling;
  2153. if (obj->tiling_mode == I915_TILING_X)
  2154. obj->stride = fb->pitches[0];
  2155. mode_cmd.pixel_format = fb->pixel_format;
  2156. mode_cmd.width = fb->width;
  2157. mode_cmd.height = fb->height;
  2158. mode_cmd.pitches[0] = fb->pitches[0];
  2159. mode_cmd.modifier[0] = fb->modifier[0];
  2160. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2161. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2162. &mode_cmd, obj)) {
  2163. DRM_DEBUG_KMS("intel fb init failed\n");
  2164. goto out_unref_obj;
  2165. }
  2166. mutex_unlock(&dev->struct_mutex);
  2167. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2168. return true;
  2169. out_unref_obj:
  2170. drm_gem_object_unreference(&obj->base);
  2171. mutex_unlock(&dev->struct_mutex);
  2172. return false;
  2173. }
  2174. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2175. static void
  2176. update_state_fb(struct drm_plane *plane)
  2177. {
  2178. if (plane->fb == plane->state->fb)
  2179. return;
  2180. if (plane->state->fb)
  2181. drm_framebuffer_unreference(plane->state->fb);
  2182. plane->state->fb = plane->fb;
  2183. if (plane->state->fb)
  2184. drm_framebuffer_reference(plane->state->fb);
  2185. }
  2186. static void
  2187. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2188. struct intel_initial_plane_config *plane_config)
  2189. {
  2190. struct drm_device *dev = intel_crtc->base.dev;
  2191. struct drm_i915_private *dev_priv = dev->dev_private;
  2192. struct drm_crtc *c;
  2193. struct intel_crtc *i;
  2194. struct drm_i915_gem_object *obj;
  2195. struct drm_plane *primary = intel_crtc->base.primary;
  2196. struct drm_plane_state *plane_state = primary->state;
  2197. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2198. struct intel_plane *intel_plane = to_intel_plane(primary);
  2199. struct intel_plane_state *intel_state =
  2200. to_intel_plane_state(plane_state);
  2201. struct drm_framebuffer *fb;
  2202. if (!plane_config->fb)
  2203. return;
  2204. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2205. fb = &plane_config->fb->base;
  2206. goto valid_fb;
  2207. }
  2208. kfree(plane_config->fb);
  2209. /*
  2210. * Failed to alloc the obj, check to see if we should share
  2211. * an fb with another CRTC instead
  2212. */
  2213. for_each_crtc(dev, c) {
  2214. i = to_intel_crtc(c);
  2215. if (c == &intel_crtc->base)
  2216. continue;
  2217. if (!i->active)
  2218. continue;
  2219. fb = c->primary->fb;
  2220. if (!fb)
  2221. continue;
  2222. obj = intel_fb_obj(fb);
  2223. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2224. drm_framebuffer_reference(fb);
  2225. goto valid_fb;
  2226. }
  2227. }
  2228. /*
  2229. * We've failed to reconstruct the BIOS FB. Current display state
  2230. * indicates that the primary plane is visible, but has a NULL FB,
  2231. * which will lead to problems later if we don't fix it up. The
  2232. * simplest solution is to just disable the primary plane now and
  2233. * pretend the BIOS never had it enabled.
  2234. */
  2235. to_intel_plane_state(plane_state)->visible = false;
  2236. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2237. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2238. intel_plane->disable_plane(primary, &intel_crtc->base);
  2239. return;
  2240. valid_fb:
  2241. plane_state->src_x = 0;
  2242. plane_state->src_y = 0;
  2243. plane_state->src_w = fb->width << 16;
  2244. plane_state->src_h = fb->height << 16;
  2245. plane_state->crtc_x = 0;
  2246. plane_state->crtc_y = 0;
  2247. plane_state->crtc_w = fb->width;
  2248. plane_state->crtc_h = fb->height;
  2249. intel_state->src.x1 = plane_state->src_x;
  2250. intel_state->src.y1 = plane_state->src_y;
  2251. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2252. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2253. intel_state->dst.x1 = plane_state->crtc_x;
  2254. intel_state->dst.y1 = plane_state->crtc_y;
  2255. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2256. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2257. obj = intel_fb_obj(fb);
  2258. if (obj->tiling_mode != I915_TILING_NONE)
  2259. dev_priv->preserve_bios_swizzle = true;
  2260. drm_framebuffer_reference(fb);
  2261. primary->fb = primary->state->fb = fb;
  2262. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2263. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2264. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2265. }
  2266. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2267. const struct intel_crtc_state *crtc_state,
  2268. const struct intel_plane_state *plane_state)
  2269. {
  2270. struct drm_device *dev = primary->dev;
  2271. struct drm_i915_private *dev_priv = dev->dev_private;
  2272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2273. struct drm_framebuffer *fb = plane_state->base.fb;
  2274. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2275. int plane = intel_crtc->plane;
  2276. u32 linear_offset;
  2277. u32 dspcntr;
  2278. i915_reg_t reg = DSPCNTR(plane);
  2279. unsigned int rotation = plane_state->base.rotation;
  2280. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2281. int x = plane_state->src.x1 >> 16;
  2282. int y = plane_state->src.y1 >> 16;
  2283. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2284. dspcntr |= DISPLAY_PLANE_ENABLE;
  2285. if (INTEL_INFO(dev)->gen < 4) {
  2286. if (intel_crtc->pipe == PIPE_B)
  2287. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2288. /* pipesrc and dspsize control the size that is scaled from,
  2289. * which should always be the user's requested size.
  2290. */
  2291. I915_WRITE(DSPSIZE(plane),
  2292. ((crtc_state->pipe_src_h - 1) << 16) |
  2293. (crtc_state->pipe_src_w - 1));
  2294. I915_WRITE(DSPPOS(plane), 0);
  2295. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2296. I915_WRITE(PRIMSIZE(plane),
  2297. ((crtc_state->pipe_src_h - 1) << 16) |
  2298. (crtc_state->pipe_src_w - 1));
  2299. I915_WRITE(PRIMPOS(plane), 0);
  2300. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2301. }
  2302. switch (fb->pixel_format) {
  2303. case DRM_FORMAT_C8:
  2304. dspcntr |= DISPPLANE_8BPP;
  2305. break;
  2306. case DRM_FORMAT_XRGB1555:
  2307. dspcntr |= DISPPLANE_BGRX555;
  2308. break;
  2309. case DRM_FORMAT_RGB565:
  2310. dspcntr |= DISPPLANE_BGRX565;
  2311. break;
  2312. case DRM_FORMAT_XRGB8888:
  2313. dspcntr |= DISPPLANE_BGRX888;
  2314. break;
  2315. case DRM_FORMAT_XBGR8888:
  2316. dspcntr |= DISPPLANE_RGBX888;
  2317. break;
  2318. case DRM_FORMAT_XRGB2101010:
  2319. dspcntr |= DISPPLANE_BGRX101010;
  2320. break;
  2321. case DRM_FORMAT_XBGR2101010:
  2322. dspcntr |= DISPPLANE_RGBX101010;
  2323. break;
  2324. default:
  2325. BUG();
  2326. }
  2327. if (INTEL_INFO(dev)->gen >= 4 &&
  2328. obj->tiling_mode != I915_TILING_NONE)
  2329. dspcntr |= DISPPLANE_TILED;
  2330. if (IS_G4X(dev))
  2331. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2332. linear_offset = y * fb->pitches[0] + x * cpp;
  2333. if (INTEL_INFO(dev)->gen >= 4) {
  2334. intel_crtc->dspaddr_offset =
  2335. intel_compute_tile_offset(&x, &y, fb, 0,
  2336. fb->pitches[0], rotation);
  2337. linear_offset -= intel_crtc->dspaddr_offset;
  2338. } else {
  2339. intel_crtc->dspaddr_offset = linear_offset;
  2340. }
  2341. if (rotation == BIT(DRM_ROTATE_180)) {
  2342. dspcntr |= DISPPLANE_ROTATE_180;
  2343. x += (crtc_state->pipe_src_w - 1);
  2344. y += (crtc_state->pipe_src_h - 1);
  2345. /* Finding the last pixel of the last line of the display
  2346. data and adding to linear_offset*/
  2347. linear_offset +=
  2348. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2349. (crtc_state->pipe_src_w - 1) * cpp;
  2350. }
  2351. intel_crtc->adjusted_x = x;
  2352. intel_crtc->adjusted_y = y;
  2353. I915_WRITE(reg, dspcntr);
  2354. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2355. if (INTEL_INFO(dev)->gen >= 4) {
  2356. I915_WRITE(DSPSURF(plane),
  2357. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2358. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2359. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2360. } else
  2361. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2362. POSTING_READ(reg);
  2363. }
  2364. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2365. struct drm_crtc *crtc)
  2366. {
  2367. struct drm_device *dev = crtc->dev;
  2368. struct drm_i915_private *dev_priv = dev->dev_private;
  2369. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2370. int plane = intel_crtc->plane;
  2371. I915_WRITE(DSPCNTR(plane), 0);
  2372. if (INTEL_INFO(dev_priv)->gen >= 4)
  2373. I915_WRITE(DSPSURF(plane), 0);
  2374. else
  2375. I915_WRITE(DSPADDR(plane), 0);
  2376. POSTING_READ(DSPCNTR(plane));
  2377. }
  2378. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2379. const struct intel_crtc_state *crtc_state,
  2380. const struct intel_plane_state *plane_state)
  2381. {
  2382. struct drm_device *dev = primary->dev;
  2383. struct drm_i915_private *dev_priv = dev->dev_private;
  2384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2385. struct drm_framebuffer *fb = plane_state->base.fb;
  2386. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2387. int plane = intel_crtc->plane;
  2388. u32 linear_offset;
  2389. u32 dspcntr;
  2390. i915_reg_t reg = DSPCNTR(plane);
  2391. unsigned int rotation = plane_state->base.rotation;
  2392. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2393. int x = plane_state->src.x1 >> 16;
  2394. int y = plane_state->src.y1 >> 16;
  2395. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2396. dspcntr |= DISPLAY_PLANE_ENABLE;
  2397. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2398. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2399. switch (fb->pixel_format) {
  2400. case DRM_FORMAT_C8:
  2401. dspcntr |= DISPPLANE_8BPP;
  2402. break;
  2403. case DRM_FORMAT_RGB565:
  2404. dspcntr |= DISPPLANE_BGRX565;
  2405. break;
  2406. case DRM_FORMAT_XRGB8888:
  2407. dspcntr |= DISPPLANE_BGRX888;
  2408. break;
  2409. case DRM_FORMAT_XBGR8888:
  2410. dspcntr |= DISPPLANE_RGBX888;
  2411. break;
  2412. case DRM_FORMAT_XRGB2101010:
  2413. dspcntr |= DISPPLANE_BGRX101010;
  2414. break;
  2415. case DRM_FORMAT_XBGR2101010:
  2416. dspcntr |= DISPPLANE_RGBX101010;
  2417. break;
  2418. default:
  2419. BUG();
  2420. }
  2421. if (obj->tiling_mode != I915_TILING_NONE)
  2422. dspcntr |= DISPPLANE_TILED;
  2423. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2424. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2425. linear_offset = y * fb->pitches[0] + x * cpp;
  2426. intel_crtc->dspaddr_offset =
  2427. intel_compute_tile_offset(&x, &y, fb, 0,
  2428. fb->pitches[0], rotation);
  2429. linear_offset -= intel_crtc->dspaddr_offset;
  2430. if (rotation == BIT(DRM_ROTATE_180)) {
  2431. dspcntr |= DISPPLANE_ROTATE_180;
  2432. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2433. x += (crtc_state->pipe_src_w - 1);
  2434. y += (crtc_state->pipe_src_h - 1);
  2435. /* Finding the last pixel of the last line of the display
  2436. data and adding to linear_offset*/
  2437. linear_offset +=
  2438. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2439. (crtc_state->pipe_src_w - 1) * cpp;
  2440. }
  2441. }
  2442. intel_crtc->adjusted_x = x;
  2443. intel_crtc->adjusted_y = y;
  2444. I915_WRITE(reg, dspcntr);
  2445. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2446. I915_WRITE(DSPSURF(plane),
  2447. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2448. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2449. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2450. } else {
  2451. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2452. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2453. }
  2454. POSTING_READ(reg);
  2455. }
  2456. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2457. uint64_t fb_modifier, uint32_t pixel_format)
  2458. {
  2459. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2460. return 64;
  2461. } else {
  2462. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2463. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2464. }
  2465. }
  2466. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2467. struct drm_i915_gem_object *obj,
  2468. unsigned int plane)
  2469. {
  2470. struct i915_ggtt_view view;
  2471. struct i915_vma *vma;
  2472. u64 offset;
  2473. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2474. intel_plane->base.state->rotation);
  2475. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2476. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2477. view.type))
  2478. return -1;
  2479. offset = vma->node.start;
  2480. if (plane == 1) {
  2481. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2482. PAGE_SIZE;
  2483. }
  2484. WARN_ON(upper_32_bits(offset));
  2485. return lower_32_bits(offset);
  2486. }
  2487. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2488. {
  2489. struct drm_device *dev = intel_crtc->base.dev;
  2490. struct drm_i915_private *dev_priv = dev->dev_private;
  2491. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2492. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2493. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2494. }
  2495. /*
  2496. * This function detaches (aka. unbinds) unused scalers in hardware
  2497. */
  2498. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2499. {
  2500. struct intel_crtc_scaler_state *scaler_state;
  2501. int i;
  2502. scaler_state = &intel_crtc->config->scaler_state;
  2503. /* loop through and disable scalers that aren't in use */
  2504. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2505. if (!scaler_state->scalers[i].in_use)
  2506. skl_detach_scaler(intel_crtc, i);
  2507. }
  2508. }
  2509. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2510. {
  2511. switch (pixel_format) {
  2512. case DRM_FORMAT_C8:
  2513. return PLANE_CTL_FORMAT_INDEXED;
  2514. case DRM_FORMAT_RGB565:
  2515. return PLANE_CTL_FORMAT_RGB_565;
  2516. case DRM_FORMAT_XBGR8888:
  2517. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2518. case DRM_FORMAT_XRGB8888:
  2519. return PLANE_CTL_FORMAT_XRGB_8888;
  2520. /*
  2521. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2522. * to be already pre-multiplied. We need to add a knob (or a different
  2523. * DRM_FORMAT) for user-space to configure that.
  2524. */
  2525. case DRM_FORMAT_ABGR8888:
  2526. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2527. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2528. case DRM_FORMAT_ARGB8888:
  2529. return PLANE_CTL_FORMAT_XRGB_8888 |
  2530. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2531. case DRM_FORMAT_XRGB2101010:
  2532. return PLANE_CTL_FORMAT_XRGB_2101010;
  2533. case DRM_FORMAT_XBGR2101010:
  2534. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2535. case DRM_FORMAT_YUYV:
  2536. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2537. case DRM_FORMAT_YVYU:
  2538. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2539. case DRM_FORMAT_UYVY:
  2540. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2541. case DRM_FORMAT_VYUY:
  2542. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2543. default:
  2544. MISSING_CASE(pixel_format);
  2545. }
  2546. return 0;
  2547. }
  2548. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2549. {
  2550. switch (fb_modifier) {
  2551. case DRM_FORMAT_MOD_NONE:
  2552. break;
  2553. case I915_FORMAT_MOD_X_TILED:
  2554. return PLANE_CTL_TILED_X;
  2555. case I915_FORMAT_MOD_Y_TILED:
  2556. return PLANE_CTL_TILED_Y;
  2557. case I915_FORMAT_MOD_Yf_TILED:
  2558. return PLANE_CTL_TILED_YF;
  2559. default:
  2560. MISSING_CASE(fb_modifier);
  2561. }
  2562. return 0;
  2563. }
  2564. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2565. {
  2566. switch (rotation) {
  2567. case BIT(DRM_ROTATE_0):
  2568. break;
  2569. /*
  2570. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2571. * while i915 HW rotation is clockwise, thats why this swapping.
  2572. */
  2573. case BIT(DRM_ROTATE_90):
  2574. return PLANE_CTL_ROTATE_270;
  2575. case BIT(DRM_ROTATE_180):
  2576. return PLANE_CTL_ROTATE_180;
  2577. case BIT(DRM_ROTATE_270):
  2578. return PLANE_CTL_ROTATE_90;
  2579. default:
  2580. MISSING_CASE(rotation);
  2581. }
  2582. return 0;
  2583. }
  2584. static void skylake_update_primary_plane(struct drm_plane *plane,
  2585. const struct intel_crtc_state *crtc_state,
  2586. const struct intel_plane_state *plane_state)
  2587. {
  2588. struct drm_device *dev = plane->dev;
  2589. struct drm_i915_private *dev_priv = dev->dev_private;
  2590. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2591. struct drm_framebuffer *fb = plane_state->base.fb;
  2592. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2593. int pipe = intel_crtc->pipe;
  2594. u32 plane_ctl, stride_div, stride;
  2595. u32 tile_height, plane_offset, plane_size;
  2596. unsigned int rotation = plane_state->base.rotation;
  2597. int x_offset, y_offset;
  2598. u32 surf_addr;
  2599. int scaler_id = plane_state->scaler_id;
  2600. int src_x = plane_state->src.x1 >> 16;
  2601. int src_y = plane_state->src.y1 >> 16;
  2602. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2603. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2604. int dst_x = plane_state->dst.x1;
  2605. int dst_y = plane_state->dst.y1;
  2606. int dst_w = drm_rect_width(&plane_state->dst);
  2607. int dst_h = drm_rect_height(&plane_state->dst);
  2608. plane_ctl = PLANE_CTL_ENABLE |
  2609. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2610. PLANE_CTL_PIPE_CSC_ENABLE;
  2611. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2612. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2613. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2614. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2615. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2616. fb->pixel_format);
  2617. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2618. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2619. if (intel_rotation_90_or_270(rotation)) {
  2620. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2621. /* stride = Surface height in tiles */
  2622. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2623. stride = DIV_ROUND_UP(fb->height, tile_height);
  2624. x_offset = stride * tile_height - src_y - src_h;
  2625. y_offset = src_x;
  2626. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2627. } else {
  2628. stride = fb->pitches[0] / stride_div;
  2629. x_offset = src_x;
  2630. y_offset = src_y;
  2631. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2632. }
  2633. plane_offset = y_offset << 16 | x_offset;
  2634. intel_crtc->adjusted_x = x_offset;
  2635. intel_crtc->adjusted_y = y_offset;
  2636. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2637. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2638. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2639. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2640. if (scaler_id >= 0) {
  2641. uint32_t ps_ctrl = 0;
  2642. WARN_ON(!dst_w || !dst_h);
  2643. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2644. crtc_state->scaler_state.scalers[scaler_id].mode;
  2645. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2646. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2647. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2648. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2649. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2650. } else {
  2651. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2652. }
  2653. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2654. POSTING_READ(PLANE_SURF(pipe, 0));
  2655. }
  2656. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2657. struct drm_crtc *crtc)
  2658. {
  2659. struct drm_device *dev = crtc->dev;
  2660. struct drm_i915_private *dev_priv = dev->dev_private;
  2661. int pipe = to_intel_crtc(crtc)->pipe;
  2662. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2663. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2664. POSTING_READ(PLANE_SURF(pipe, 0));
  2665. }
  2666. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2667. static int
  2668. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2669. int x, int y, enum mode_set_atomic state)
  2670. {
  2671. /* Support for kgdboc is disabled, this needs a major rework. */
  2672. DRM_ERROR("legacy panic handler not supported any more.\n");
  2673. return -ENODEV;
  2674. }
  2675. static void intel_complete_page_flips(struct drm_device *dev)
  2676. {
  2677. struct drm_crtc *crtc;
  2678. for_each_crtc(dev, crtc) {
  2679. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2680. enum plane plane = intel_crtc->plane;
  2681. intel_prepare_page_flip(dev, plane);
  2682. intel_finish_page_flip_plane(dev, plane);
  2683. }
  2684. }
  2685. static void intel_update_primary_planes(struct drm_device *dev)
  2686. {
  2687. struct drm_crtc *crtc;
  2688. for_each_crtc(dev, crtc) {
  2689. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2690. struct intel_plane_state *plane_state;
  2691. drm_modeset_lock_crtc(crtc, &plane->base);
  2692. plane_state = to_intel_plane_state(plane->base.state);
  2693. if (plane_state->visible)
  2694. plane->update_plane(&plane->base,
  2695. to_intel_crtc_state(crtc->state),
  2696. plane_state);
  2697. drm_modeset_unlock_crtc(crtc);
  2698. }
  2699. }
  2700. void intel_prepare_reset(struct drm_device *dev)
  2701. {
  2702. /* no reset support for gen2 */
  2703. if (IS_GEN2(dev))
  2704. return;
  2705. /* reset doesn't touch the display */
  2706. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2707. return;
  2708. drm_modeset_lock_all(dev);
  2709. /*
  2710. * Disabling the crtcs gracefully seems nicer. Also the
  2711. * g33 docs say we should at least disable all the planes.
  2712. */
  2713. intel_display_suspend(dev);
  2714. }
  2715. void intel_finish_reset(struct drm_device *dev)
  2716. {
  2717. struct drm_i915_private *dev_priv = to_i915(dev);
  2718. /*
  2719. * Flips in the rings will be nuked by the reset,
  2720. * so complete all pending flips so that user space
  2721. * will get its events and not get stuck.
  2722. */
  2723. intel_complete_page_flips(dev);
  2724. /* no reset support for gen2 */
  2725. if (IS_GEN2(dev))
  2726. return;
  2727. /* reset doesn't touch the display */
  2728. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2729. /*
  2730. * Flips in the rings have been nuked by the reset,
  2731. * so update the base address of all primary
  2732. * planes to the the last fb to make sure we're
  2733. * showing the correct fb after a reset.
  2734. *
  2735. * FIXME: Atomic will make this obsolete since we won't schedule
  2736. * CS-based flips (which might get lost in gpu resets) any more.
  2737. */
  2738. intel_update_primary_planes(dev);
  2739. return;
  2740. }
  2741. /*
  2742. * The display has been reset as well,
  2743. * so need a full re-initialization.
  2744. */
  2745. intel_runtime_pm_disable_interrupts(dev_priv);
  2746. intel_runtime_pm_enable_interrupts(dev_priv);
  2747. intel_modeset_init_hw(dev);
  2748. spin_lock_irq(&dev_priv->irq_lock);
  2749. if (dev_priv->display.hpd_irq_setup)
  2750. dev_priv->display.hpd_irq_setup(dev);
  2751. spin_unlock_irq(&dev_priv->irq_lock);
  2752. intel_display_resume(dev);
  2753. intel_hpd_init(dev_priv);
  2754. drm_modeset_unlock_all(dev);
  2755. }
  2756. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2757. {
  2758. struct drm_device *dev = crtc->dev;
  2759. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2760. unsigned reset_counter;
  2761. bool pending;
  2762. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2763. if (intel_crtc->reset_counter != reset_counter)
  2764. return false;
  2765. spin_lock_irq(&dev->event_lock);
  2766. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2767. spin_unlock_irq(&dev->event_lock);
  2768. return pending;
  2769. }
  2770. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2771. struct intel_crtc_state *old_crtc_state)
  2772. {
  2773. struct drm_device *dev = crtc->base.dev;
  2774. struct drm_i915_private *dev_priv = dev->dev_private;
  2775. struct intel_crtc_state *pipe_config =
  2776. to_intel_crtc_state(crtc->base.state);
  2777. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2778. crtc->base.mode = crtc->base.state->mode;
  2779. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2780. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2781. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2782. /*
  2783. * Update pipe size and adjust fitter if needed: the reason for this is
  2784. * that in compute_mode_changes we check the native mode (not the pfit
  2785. * mode) to see if we can flip rather than do a full mode set. In the
  2786. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2787. * pfit state, we'll end up with a big fb scanned out into the wrong
  2788. * sized surface.
  2789. */
  2790. I915_WRITE(PIPESRC(crtc->pipe),
  2791. ((pipe_config->pipe_src_w - 1) << 16) |
  2792. (pipe_config->pipe_src_h - 1));
  2793. /* on skylake this is done by detaching scalers */
  2794. if (INTEL_INFO(dev)->gen >= 9) {
  2795. skl_detach_scalers(crtc);
  2796. if (pipe_config->pch_pfit.enabled)
  2797. skylake_pfit_enable(crtc);
  2798. } else if (HAS_PCH_SPLIT(dev)) {
  2799. if (pipe_config->pch_pfit.enabled)
  2800. ironlake_pfit_enable(crtc);
  2801. else if (old_crtc_state->pch_pfit.enabled)
  2802. ironlake_pfit_disable(crtc, true);
  2803. }
  2804. }
  2805. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2806. {
  2807. struct drm_device *dev = crtc->dev;
  2808. struct drm_i915_private *dev_priv = dev->dev_private;
  2809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2810. int pipe = intel_crtc->pipe;
  2811. i915_reg_t reg;
  2812. u32 temp;
  2813. /* enable normal train */
  2814. reg = FDI_TX_CTL(pipe);
  2815. temp = I915_READ(reg);
  2816. if (IS_IVYBRIDGE(dev)) {
  2817. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2818. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2819. } else {
  2820. temp &= ~FDI_LINK_TRAIN_NONE;
  2821. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2822. }
  2823. I915_WRITE(reg, temp);
  2824. reg = FDI_RX_CTL(pipe);
  2825. temp = I915_READ(reg);
  2826. if (HAS_PCH_CPT(dev)) {
  2827. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2828. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2829. } else {
  2830. temp &= ~FDI_LINK_TRAIN_NONE;
  2831. temp |= FDI_LINK_TRAIN_NONE;
  2832. }
  2833. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2834. /* wait one idle pattern time */
  2835. POSTING_READ(reg);
  2836. udelay(1000);
  2837. /* IVB wants error correction enabled */
  2838. if (IS_IVYBRIDGE(dev))
  2839. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2840. FDI_FE_ERRC_ENABLE);
  2841. }
  2842. /* The FDI link training functions for ILK/Ibexpeak. */
  2843. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2844. {
  2845. struct drm_device *dev = crtc->dev;
  2846. struct drm_i915_private *dev_priv = dev->dev_private;
  2847. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2848. int pipe = intel_crtc->pipe;
  2849. i915_reg_t reg;
  2850. u32 temp, tries;
  2851. /* FDI needs bits from pipe first */
  2852. assert_pipe_enabled(dev_priv, pipe);
  2853. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2854. for train result */
  2855. reg = FDI_RX_IMR(pipe);
  2856. temp = I915_READ(reg);
  2857. temp &= ~FDI_RX_SYMBOL_LOCK;
  2858. temp &= ~FDI_RX_BIT_LOCK;
  2859. I915_WRITE(reg, temp);
  2860. I915_READ(reg);
  2861. udelay(150);
  2862. /* enable CPU FDI TX and PCH FDI RX */
  2863. reg = FDI_TX_CTL(pipe);
  2864. temp = I915_READ(reg);
  2865. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2866. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2867. temp &= ~FDI_LINK_TRAIN_NONE;
  2868. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2869. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2870. reg = FDI_RX_CTL(pipe);
  2871. temp = I915_READ(reg);
  2872. temp &= ~FDI_LINK_TRAIN_NONE;
  2873. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2874. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2875. POSTING_READ(reg);
  2876. udelay(150);
  2877. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2878. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2879. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2880. FDI_RX_PHASE_SYNC_POINTER_EN);
  2881. reg = FDI_RX_IIR(pipe);
  2882. for (tries = 0; tries < 5; tries++) {
  2883. temp = I915_READ(reg);
  2884. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2885. if ((temp & FDI_RX_BIT_LOCK)) {
  2886. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2887. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2888. break;
  2889. }
  2890. }
  2891. if (tries == 5)
  2892. DRM_ERROR("FDI train 1 fail!\n");
  2893. /* Train 2 */
  2894. reg = FDI_TX_CTL(pipe);
  2895. temp = I915_READ(reg);
  2896. temp &= ~FDI_LINK_TRAIN_NONE;
  2897. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2898. I915_WRITE(reg, temp);
  2899. reg = FDI_RX_CTL(pipe);
  2900. temp = I915_READ(reg);
  2901. temp &= ~FDI_LINK_TRAIN_NONE;
  2902. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2903. I915_WRITE(reg, temp);
  2904. POSTING_READ(reg);
  2905. udelay(150);
  2906. reg = FDI_RX_IIR(pipe);
  2907. for (tries = 0; tries < 5; tries++) {
  2908. temp = I915_READ(reg);
  2909. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2910. if (temp & FDI_RX_SYMBOL_LOCK) {
  2911. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2912. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2913. break;
  2914. }
  2915. }
  2916. if (tries == 5)
  2917. DRM_ERROR("FDI train 2 fail!\n");
  2918. DRM_DEBUG_KMS("FDI train done\n");
  2919. }
  2920. static const int snb_b_fdi_train_param[] = {
  2921. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2922. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2923. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2924. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2925. };
  2926. /* The FDI link training functions for SNB/Cougarpoint. */
  2927. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2928. {
  2929. struct drm_device *dev = crtc->dev;
  2930. struct drm_i915_private *dev_priv = dev->dev_private;
  2931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2932. int pipe = intel_crtc->pipe;
  2933. i915_reg_t reg;
  2934. u32 temp, i, retry;
  2935. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2936. for train result */
  2937. reg = FDI_RX_IMR(pipe);
  2938. temp = I915_READ(reg);
  2939. temp &= ~FDI_RX_SYMBOL_LOCK;
  2940. temp &= ~FDI_RX_BIT_LOCK;
  2941. I915_WRITE(reg, temp);
  2942. POSTING_READ(reg);
  2943. udelay(150);
  2944. /* enable CPU FDI TX and PCH FDI RX */
  2945. reg = FDI_TX_CTL(pipe);
  2946. temp = I915_READ(reg);
  2947. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2948. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2949. temp &= ~FDI_LINK_TRAIN_NONE;
  2950. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2951. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2952. /* SNB-B */
  2953. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2954. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2955. I915_WRITE(FDI_RX_MISC(pipe),
  2956. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2957. reg = FDI_RX_CTL(pipe);
  2958. temp = I915_READ(reg);
  2959. if (HAS_PCH_CPT(dev)) {
  2960. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2961. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2962. } else {
  2963. temp &= ~FDI_LINK_TRAIN_NONE;
  2964. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2965. }
  2966. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2967. POSTING_READ(reg);
  2968. udelay(150);
  2969. for (i = 0; i < 4; i++) {
  2970. reg = FDI_TX_CTL(pipe);
  2971. temp = I915_READ(reg);
  2972. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2973. temp |= snb_b_fdi_train_param[i];
  2974. I915_WRITE(reg, temp);
  2975. POSTING_READ(reg);
  2976. udelay(500);
  2977. for (retry = 0; retry < 5; retry++) {
  2978. reg = FDI_RX_IIR(pipe);
  2979. temp = I915_READ(reg);
  2980. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2981. if (temp & FDI_RX_BIT_LOCK) {
  2982. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2983. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2984. break;
  2985. }
  2986. udelay(50);
  2987. }
  2988. if (retry < 5)
  2989. break;
  2990. }
  2991. if (i == 4)
  2992. DRM_ERROR("FDI train 1 fail!\n");
  2993. /* Train 2 */
  2994. reg = FDI_TX_CTL(pipe);
  2995. temp = I915_READ(reg);
  2996. temp &= ~FDI_LINK_TRAIN_NONE;
  2997. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2998. if (IS_GEN6(dev)) {
  2999. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3000. /* SNB-B */
  3001. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3002. }
  3003. I915_WRITE(reg, temp);
  3004. reg = FDI_RX_CTL(pipe);
  3005. temp = I915_READ(reg);
  3006. if (HAS_PCH_CPT(dev)) {
  3007. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3008. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3009. } else {
  3010. temp &= ~FDI_LINK_TRAIN_NONE;
  3011. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3012. }
  3013. I915_WRITE(reg, temp);
  3014. POSTING_READ(reg);
  3015. udelay(150);
  3016. for (i = 0; i < 4; i++) {
  3017. reg = FDI_TX_CTL(pipe);
  3018. temp = I915_READ(reg);
  3019. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3020. temp |= snb_b_fdi_train_param[i];
  3021. I915_WRITE(reg, temp);
  3022. POSTING_READ(reg);
  3023. udelay(500);
  3024. for (retry = 0; retry < 5; retry++) {
  3025. reg = FDI_RX_IIR(pipe);
  3026. temp = I915_READ(reg);
  3027. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3028. if (temp & FDI_RX_SYMBOL_LOCK) {
  3029. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3030. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3031. break;
  3032. }
  3033. udelay(50);
  3034. }
  3035. if (retry < 5)
  3036. break;
  3037. }
  3038. if (i == 4)
  3039. DRM_ERROR("FDI train 2 fail!\n");
  3040. DRM_DEBUG_KMS("FDI train done.\n");
  3041. }
  3042. /* Manual link training for Ivy Bridge A0 parts */
  3043. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3044. {
  3045. struct drm_device *dev = crtc->dev;
  3046. struct drm_i915_private *dev_priv = dev->dev_private;
  3047. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3048. int pipe = intel_crtc->pipe;
  3049. i915_reg_t reg;
  3050. u32 temp, i, j;
  3051. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3052. for train result */
  3053. reg = FDI_RX_IMR(pipe);
  3054. temp = I915_READ(reg);
  3055. temp &= ~FDI_RX_SYMBOL_LOCK;
  3056. temp &= ~FDI_RX_BIT_LOCK;
  3057. I915_WRITE(reg, temp);
  3058. POSTING_READ(reg);
  3059. udelay(150);
  3060. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3061. I915_READ(FDI_RX_IIR(pipe)));
  3062. /* Try each vswing and preemphasis setting twice before moving on */
  3063. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3064. /* disable first in case we need to retry */
  3065. reg = FDI_TX_CTL(pipe);
  3066. temp = I915_READ(reg);
  3067. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3068. temp &= ~FDI_TX_ENABLE;
  3069. I915_WRITE(reg, temp);
  3070. reg = FDI_RX_CTL(pipe);
  3071. temp = I915_READ(reg);
  3072. temp &= ~FDI_LINK_TRAIN_AUTO;
  3073. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3074. temp &= ~FDI_RX_ENABLE;
  3075. I915_WRITE(reg, temp);
  3076. /* enable CPU FDI TX and PCH FDI RX */
  3077. reg = FDI_TX_CTL(pipe);
  3078. temp = I915_READ(reg);
  3079. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3080. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3081. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3082. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3083. temp |= snb_b_fdi_train_param[j/2];
  3084. temp |= FDI_COMPOSITE_SYNC;
  3085. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3086. I915_WRITE(FDI_RX_MISC(pipe),
  3087. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3088. reg = FDI_RX_CTL(pipe);
  3089. temp = I915_READ(reg);
  3090. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3091. temp |= FDI_COMPOSITE_SYNC;
  3092. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3093. POSTING_READ(reg);
  3094. udelay(1); /* should be 0.5us */
  3095. for (i = 0; i < 4; i++) {
  3096. reg = FDI_RX_IIR(pipe);
  3097. temp = I915_READ(reg);
  3098. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3099. if (temp & FDI_RX_BIT_LOCK ||
  3100. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3101. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3102. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3103. i);
  3104. break;
  3105. }
  3106. udelay(1); /* should be 0.5us */
  3107. }
  3108. if (i == 4) {
  3109. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3110. continue;
  3111. }
  3112. /* Train 2 */
  3113. reg = FDI_TX_CTL(pipe);
  3114. temp = I915_READ(reg);
  3115. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3116. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3117. I915_WRITE(reg, temp);
  3118. reg = FDI_RX_CTL(pipe);
  3119. temp = I915_READ(reg);
  3120. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3121. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3122. I915_WRITE(reg, temp);
  3123. POSTING_READ(reg);
  3124. udelay(2); /* should be 1.5us */
  3125. for (i = 0; i < 4; i++) {
  3126. reg = FDI_RX_IIR(pipe);
  3127. temp = I915_READ(reg);
  3128. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3129. if (temp & FDI_RX_SYMBOL_LOCK ||
  3130. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3131. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3132. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3133. i);
  3134. goto train_done;
  3135. }
  3136. udelay(2); /* should be 1.5us */
  3137. }
  3138. if (i == 4)
  3139. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3140. }
  3141. train_done:
  3142. DRM_DEBUG_KMS("FDI train done.\n");
  3143. }
  3144. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3145. {
  3146. struct drm_device *dev = intel_crtc->base.dev;
  3147. struct drm_i915_private *dev_priv = dev->dev_private;
  3148. int pipe = intel_crtc->pipe;
  3149. i915_reg_t reg;
  3150. u32 temp;
  3151. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3152. reg = FDI_RX_CTL(pipe);
  3153. temp = I915_READ(reg);
  3154. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3155. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3156. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3157. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3158. POSTING_READ(reg);
  3159. udelay(200);
  3160. /* Switch from Rawclk to PCDclk */
  3161. temp = I915_READ(reg);
  3162. I915_WRITE(reg, temp | FDI_PCDCLK);
  3163. POSTING_READ(reg);
  3164. udelay(200);
  3165. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3166. reg = FDI_TX_CTL(pipe);
  3167. temp = I915_READ(reg);
  3168. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3169. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3170. POSTING_READ(reg);
  3171. udelay(100);
  3172. }
  3173. }
  3174. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3175. {
  3176. struct drm_device *dev = intel_crtc->base.dev;
  3177. struct drm_i915_private *dev_priv = dev->dev_private;
  3178. int pipe = intel_crtc->pipe;
  3179. i915_reg_t reg;
  3180. u32 temp;
  3181. /* Switch from PCDclk to Rawclk */
  3182. reg = FDI_RX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3185. /* Disable CPU FDI TX PLL */
  3186. reg = FDI_TX_CTL(pipe);
  3187. temp = I915_READ(reg);
  3188. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3189. POSTING_READ(reg);
  3190. udelay(100);
  3191. reg = FDI_RX_CTL(pipe);
  3192. temp = I915_READ(reg);
  3193. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3194. /* Wait for the clocks to turn off. */
  3195. POSTING_READ(reg);
  3196. udelay(100);
  3197. }
  3198. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3199. {
  3200. struct drm_device *dev = crtc->dev;
  3201. struct drm_i915_private *dev_priv = dev->dev_private;
  3202. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3203. int pipe = intel_crtc->pipe;
  3204. i915_reg_t reg;
  3205. u32 temp;
  3206. /* disable CPU FDI tx and PCH FDI rx */
  3207. reg = FDI_TX_CTL(pipe);
  3208. temp = I915_READ(reg);
  3209. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3210. POSTING_READ(reg);
  3211. reg = FDI_RX_CTL(pipe);
  3212. temp = I915_READ(reg);
  3213. temp &= ~(0x7 << 16);
  3214. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3215. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3216. POSTING_READ(reg);
  3217. udelay(100);
  3218. /* Ironlake workaround, disable clock pointer after downing FDI */
  3219. if (HAS_PCH_IBX(dev))
  3220. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3221. /* still set train pattern 1 */
  3222. reg = FDI_TX_CTL(pipe);
  3223. temp = I915_READ(reg);
  3224. temp &= ~FDI_LINK_TRAIN_NONE;
  3225. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3226. I915_WRITE(reg, temp);
  3227. reg = FDI_RX_CTL(pipe);
  3228. temp = I915_READ(reg);
  3229. if (HAS_PCH_CPT(dev)) {
  3230. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3231. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3232. } else {
  3233. temp &= ~FDI_LINK_TRAIN_NONE;
  3234. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3235. }
  3236. /* BPC in FDI rx is consistent with that in PIPECONF */
  3237. temp &= ~(0x07 << 16);
  3238. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3239. I915_WRITE(reg, temp);
  3240. POSTING_READ(reg);
  3241. udelay(100);
  3242. }
  3243. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3244. {
  3245. struct intel_crtc *crtc;
  3246. /* Note that we don't need to be called with mode_config.lock here
  3247. * as our list of CRTC objects is static for the lifetime of the
  3248. * device and so cannot disappear as we iterate. Similarly, we can
  3249. * happily treat the predicates as racy, atomic checks as userspace
  3250. * cannot claim and pin a new fb without at least acquring the
  3251. * struct_mutex and so serialising with us.
  3252. */
  3253. for_each_intel_crtc(dev, crtc) {
  3254. if (atomic_read(&crtc->unpin_work_count) == 0)
  3255. continue;
  3256. if (crtc->unpin_work)
  3257. intel_wait_for_vblank(dev, crtc->pipe);
  3258. return true;
  3259. }
  3260. return false;
  3261. }
  3262. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3263. {
  3264. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3265. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3266. /* ensure that the unpin work is consistent wrt ->pending. */
  3267. smp_rmb();
  3268. intel_crtc->unpin_work = NULL;
  3269. if (work->event)
  3270. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3271. drm_crtc_vblank_put(&intel_crtc->base);
  3272. wake_up_all(&dev_priv->pending_flip_queue);
  3273. queue_work(dev_priv->wq, &work->work);
  3274. trace_i915_flip_complete(intel_crtc->plane,
  3275. work->pending_flip_obj);
  3276. }
  3277. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3278. {
  3279. struct drm_device *dev = crtc->dev;
  3280. struct drm_i915_private *dev_priv = dev->dev_private;
  3281. long ret;
  3282. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3283. ret = wait_event_interruptible_timeout(
  3284. dev_priv->pending_flip_queue,
  3285. !intel_crtc_has_pending_flip(crtc),
  3286. 60*HZ);
  3287. if (ret < 0)
  3288. return ret;
  3289. if (ret == 0) {
  3290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3291. spin_lock_irq(&dev->event_lock);
  3292. if (intel_crtc->unpin_work) {
  3293. WARN_ONCE(1, "Removing stuck page flip\n");
  3294. page_flip_completed(intel_crtc);
  3295. }
  3296. spin_unlock_irq(&dev->event_lock);
  3297. }
  3298. return 0;
  3299. }
  3300. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3301. {
  3302. u32 temp;
  3303. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3304. mutex_lock(&dev_priv->sb_lock);
  3305. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3306. temp |= SBI_SSCCTL_DISABLE;
  3307. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3308. mutex_unlock(&dev_priv->sb_lock);
  3309. }
  3310. /* Program iCLKIP clock to the desired frequency */
  3311. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3312. {
  3313. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3314. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3315. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3316. u32 temp;
  3317. lpt_disable_iclkip(dev_priv);
  3318. /* The iCLK virtual clock root frequency is in MHz,
  3319. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3320. * divisors, it is necessary to divide one by another, so we
  3321. * convert the virtual clock precision to KHz here for higher
  3322. * precision.
  3323. */
  3324. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3325. u32 iclk_virtual_root_freq = 172800 * 1000;
  3326. u32 iclk_pi_range = 64;
  3327. u32 desired_divisor;
  3328. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3329. clock << auxdiv);
  3330. divsel = (desired_divisor / iclk_pi_range) - 2;
  3331. phaseinc = desired_divisor % iclk_pi_range;
  3332. /*
  3333. * Near 20MHz is a corner case which is
  3334. * out of range for the 7-bit divisor
  3335. */
  3336. if (divsel <= 0x7f)
  3337. break;
  3338. }
  3339. /* This should not happen with any sane values */
  3340. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3341. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3342. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3343. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3344. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3345. clock,
  3346. auxdiv,
  3347. divsel,
  3348. phasedir,
  3349. phaseinc);
  3350. mutex_lock(&dev_priv->sb_lock);
  3351. /* Program SSCDIVINTPHASE6 */
  3352. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3353. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3354. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3355. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3356. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3357. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3358. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3359. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3360. /* Program SSCAUXDIV */
  3361. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3362. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3363. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3364. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3365. /* Enable modulator and associated divider */
  3366. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3367. temp &= ~SBI_SSCCTL_DISABLE;
  3368. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3369. mutex_unlock(&dev_priv->sb_lock);
  3370. /* Wait for initialization time */
  3371. udelay(24);
  3372. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3373. }
  3374. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3375. {
  3376. u32 divsel, phaseinc, auxdiv;
  3377. u32 iclk_virtual_root_freq = 172800 * 1000;
  3378. u32 iclk_pi_range = 64;
  3379. u32 desired_divisor;
  3380. u32 temp;
  3381. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3382. return 0;
  3383. mutex_lock(&dev_priv->sb_lock);
  3384. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3385. if (temp & SBI_SSCCTL_DISABLE) {
  3386. mutex_unlock(&dev_priv->sb_lock);
  3387. return 0;
  3388. }
  3389. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3390. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3391. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3392. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3393. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3394. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3395. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3396. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3397. mutex_unlock(&dev_priv->sb_lock);
  3398. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3399. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3400. desired_divisor << auxdiv);
  3401. }
  3402. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3403. enum pipe pch_transcoder)
  3404. {
  3405. struct drm_device *dev = crtc->base.dev;
  3406. struct drm_i915_private *dev_priv = dev->dev_private;
  3407. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3408. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3409. I915_READ(HTOTAL(cpu_transcoder)));
  3410. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3411. I915_READ(HBLANK(cpu_transcoder)));
  3412. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3413. I915_READ(HSYNC(cpu_transcoder)));
  3414. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3415. I915_READ(VTOTAL(cpu_transcoder)));
  3416. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3417. I915_READ(VBLANK(cpu_transcoder)));
  3418. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3419. I915_READ(VSYNC(cpu_transcoder)));
  3420. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3421. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3422. }
  3423. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3424. {
  3425. struct drm_i915_private *dev_priv = dev->dev_private;
  3426. uint32_t temp;
  3427. temp = I915_READ(SOUTH_CHICKEN1);
  3428. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3429. return;
  3430. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3431. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3432. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3433. if (enable)
  3434. temp |= FDI_BC_BIFURCATION_SELECT;
  3435. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3436. I915_WRITE(SOUTH_CHICKEN1, temp);
  3437. POSTING_READ(SOUTH_CHICKEN1);
  3438. }
  3439. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3440. {
  3441. struct drm_device *dev = intel_crtc->base.dev;
  3442. switch (intel_crtc->pipe) {
  3443. case PIPE_A:
  3444. break;
  3445. case PIPE_B:
  3446. if (intel_crtc->config->fdi_lanes > 2)
  3447. cpt_set_fdi_bc_bifurcation(dev, false);
  3448. else
  3449. cpt_set_fdi_bc_bifurcation(dev, true);
  3450. break;
  3451. case PIPE_C:
  3452. cpt_set_fdi_bc_bifurcation(dev, true);
  3453. break;
  3454. default:
  3455. BUG();
  3456. }
  3457. }
  3458. /* Return which DP Port should be selected for Transcoder DP control */
  3459. static enum port
  3460. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3461. {
  3462. struct drm_device *dev = crtc->dev;
  3463. struct intel_encoder *encoder;
  3464. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3465. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3466. encoder->type == INTEL_OUTPUT_EDP)
  3467. return enc_to_dig_port(&encoder->base)->port;
  3468. }
  3469. return -1;
  3470. }
  3471. /*
  3472. * Enable PCH resources required for PCH ports:
  3473. * - PCH PLLs
  3474. * - FDI training & RX/TX
  3475. * - update transcoder timings
  3476. * - DP transcoding bits
  3477. * - transcoder
  3478. */
  3479. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3480. {
  3481. struct drm_device *dev = crtc->dev;
  3482. struct drm_i915_private *dev_priv = dev->dev_private;
  3483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3484. int pipe = intel_crtc->pipe;
  3485. u32 temp;
  3486. assert_pch_transcoder_disabled(dev_priv, pipe);
  3487. if (IS_IVYBRIDGE(dev))
  3488. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3489. /* Write the TU size bits before fdi link training, so that error
  3490. * detection works. */
  3491. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3492. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3493. /* For PCH output, training FDI link */
  3494. dev_priv->display.fdi_link_train(crtc);
  3495. /* We need to program the right clock selection before writing the pixel
  3496. * mutliplier into the DPLL. */
  3497. if (HAS_PCH_CPT(dev)) {
  3498. u32 sel;
  3499. temp = I915_READ(PCH_DPLL_SEL);
  3500. temp |= TRANS_DPLL_ENABLE(pipe);
  3501. sel = TRANS_DPLLB_SEL(pipe);
  3502. if (intel_crtc->config->shared_dpll ==
  3503. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3504. temp |= sel;
  3505. else
  3506. temp &= ~sel;
  3507. I915_WRITE(PCH_DPLL_SEL, temp);
  3508. }
  3509. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3510. * transcoder, and we actually should do this to not upset any PCH
  3511. * transcoder that already use the clock when we share it.
  3512. *
  3513. * Note that enable_shared_dpll tries to do the right thing, but
  3514. * get_shared_dpll unconditionally resets the pll - we need that to have
  3515. * the right LVDS enable sequence. */
  3516. intel_enable_shared_dpll(intel_crtc);
  3517. /* set transcoder timing, panel must allow it */
  3518. assert_panel_unlocked(dev_priv, pipe);
  3519. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3520. intel_fdi_normal_train(crtc);
  3521. /* For PCH DP, enable TRANS_DP_CTL */
  3522. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3523. const struct drm_display_mode *adjusted_mode =
  3524. &intel_crtc->config->base.adjusted_mode;
  3525. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3526. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3527. temp = I915_READ(reg);
  3528. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3529. TRANS_DP_SYNC_MASK |
  3530. TRANS_DP_BPC_MASK);
  3531. temp |= TRANS_DP_OUTPUT_ENABLE;
  3532. temp |= bpc << 9; /* same format but at 11:9 */
  3533. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3534. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3535. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3536. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3537. switch (intel_trans_dp_port_sel(crtc)) {
  3538. case PORT_B:
  3539. temp |= TRANS_DP_PORT_SEL_B;
  3540. break;
  3541. case PORT_C:
  3542. temp |= TRANS_DP_PORT_SEL_C;
  3543. break;
  3544. case PORT_D:
  3545. temp |= TRANS_DP_PORT_SEL_D;
  3546. break;
  3547. default:
  3548. BUG();
  3549. }
  3550. I915_WRITE(reg, temp);
  3551. }
  3552. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3553. }
  3554. static void lpt_pch_enable(struct drm_crtc *crtc)
  3555. {
  3556. struct drm_device *dev = crtc->dev;
  3557. struct drm_i915_private *dev_priv = dev->dev_private;
  3558. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3559. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3560. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3561. lpt_program_iclkip(crtc);
  3562. /* Set transcoder timing. */
  3563. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3564. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3565. }
  3566. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3567. {
  3568. struct drm_i915_private *dev_priv = dev->dev_private;
  3569. i915_reg_t dslreg = PIPEDSL(pipe);
  3570. u32 temp;
  3571. temp = I915_READ(dslreg);
  3572. udelay(500);
  3573. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3574. if (wait_for(I915_READ(dslreg) != temp, 5))
  3575. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3576. }
  3577. }
  3578. static int
  3579. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3580. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3581. int src_w, int src_h, int dst_w, int dst_h)
  3582. {
  3583. struct intel_crtc_scaler_state *scaler_state =
  3584. &crtc_state->scaler_state;
  3585. struct intel_crtc *intel_crtc =
  3586. to_intel_crtc(crtc_state->base.crtc);
  3587. int need_scaling;
  3588. need_scaling = intel_rotation_90_or_270(rotation) ?
  3589. (src_h != dst_w || src_w != dst_h):
  3590. (src_w != dst_w || src_h != dst_h);
  3591. /*
  3592. * if plane is being disabled or scaler is no more required or force detach
  3593. * - free scaler binded to this plane/crtc
  3594. * - in order to do this, update crtc->scaler_usage
  3595. *
  3596. * Here scaler state in crtc_state is set free so that
  3597. * scaler can be assigned to other user. Actual register
  3598. * update to free the scaler is done in plane/panel-fit programming.
  3599. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3600. */
  3601. if (force_detach || !need_scaling) {
  3602. if (*scaler_id >= 0) {
  3603. scaler_state->scaler_users &= ~(1 << scaler_user);
  3604. scaler_state->scalers[*scaler_id].in_use = 0;
  3605. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3606. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3607. intel_crtc->pipe, scaler_user, *scaler_id,
  3608. scaler_state->scaler_users);
  3609. *scaler_id = -1;
  3610. }
  3611. return 0;
  3612. }
  3613. /* range checks */
  3614. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3615. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3616. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3617. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3618. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3619. "size is out of scaler range\n",
  3620. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3621. return -EINVAL;
  3622. }
  3623. /* mark this plane as a scaler user in crtc_state */
  3624. scaler_state->scaler_users |= (1 << scaler_user);
  3625. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3626. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3627. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3628. scaler_state->scaler_users);
  3629. return 0;
  3630. }
  3631. /**
  3632. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3633. *
  3634. * @state: crtc's scaler state
  3635. *
  3636. * Return
  3637. * 0 - scaler_usage updated successfully
  3638. * error - requested scaling cannot be supported or other error condition
  3639. */
  3640. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3641. {
  3642. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3643. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3644. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3645. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3646. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3647. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3648. state->pipe_src_w, state->pipe_src_h,
  3649. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3650. }
  3651. /**
  3652. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3653. *
  3654. * @state: crtc's scaler state
  3655. * @plane_state: atomic plane state to update
  3656. *
  3657. * Return
  3658. * 0 - scaler_usage updated successfully
  3659. * error - requested scaling cannot be supported or other error condition
  3660. */
  3661. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3662. struct intel_plane_state *plane_state)
  3663. {
  3664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3665. struct intel_plane *intel_plane =
  3666. to_intel_plane(plane_state->base.plane);
  3667. struct drm_framebuffer *fb = plane_state->base.fb;
  3668. int ret;
  3669. bool force_detach = !fb || !plane_state->visible;
  3670. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3671. intel_plane->base.base.id, intel_crtc->pipe,
  3672. drm_plane_index(&intel_plane->base));
  3673. ret = skl_update_scaler(crtc_state, force_detach,
  3674. drm_plane_index(&intel_plane->base),
  3675. &plane_state->scaler_id,
  3676. plane_state->base.rotation,
  3677. drm_rect_width(&plane_state->src) >> 16,
  3678. drm_rect_height(&plane_state->src) >> 16,
  3679. drm_rect_width(&plane_state->dst),
  3680. drm_rect_height(&plane_state->dst));
  3681. if (ret || plane_state->scaler_id < 0)
  3682. return ret;
  3683. /* check colorkey */
  3684. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3685. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3686. intel_plane->base.base.id);
  3687. return -EINVAL;
  3688. }
  3689. /* Check src format */
  3690. switch (fb->pixel_format) {
  3691. case DRM_FORMAT_RGB565:
  3692. case DRM_FORMAT_XBGR8888:
  3693. case DRM_FORMAT_XRGB8888:
  3694. case DRM_FORMAT_ABGR8888:
  3695. case DRM_FORMAT_ARGB8888:
  3696. case DRM_FORMAT_XRGB2101010:
  3697. case DRM_FORMAT_XBGR2101010:
  3698. case DRM_FORMAT_YUYV:
  3699. case DRM_FORMAT_YVYU:
  3700. case DRM_FORMAT_UYVY:
  3701. case DRM_FORMAT_VYUY:
  3702. break;
  3703. default:
  3704. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3705. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3706. return -EINVAL;
  3707. }
  3708. return 0;
  3709. }
  3710. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3711. {
  3712. int i;
  3713. for (i = 0; i < crtc->num_scalers; i++)
  3714. skl_detach_scaler(crtc, i);
  3715. }
  3716. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3717. {
  3718. struct drm_device *dev = crtc->base.dev;
  3719. struct drm_i915_private *dev_priv = dev->dev_private;
  3720. int pipe = crtc->pipe;
  3721. struct intel_crtc_scaler_state *scaler_state =
  3722. &crtc->config->scaler_state;
  3723. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3724. if (crtc->config->pch_pfit.enabled) {
  3725. int id;
  3726. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3727. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3728. return;
  3729. }
  3730. id = scaler_state->scaler_id;
  3731. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3732. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3733. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3734. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3735. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3736. }
  3737. }
  3738. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3739. {
  3740. struct drm_device *dev = crtc->base.dev;
  3741. struct drm_i915_private *dev_priv = dev->dev_private;
  3742. int pipe = crtc->pipe;
  3743. if (crtc->config->pch_pfit.enabled) {
  3744. /* Force use of hard-coded filter coefficients
  3745. * as some pre-programmed values are broken,
  3746. * e.g. x201.
  3747. */
  3748. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3749. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3750. PF_PIPE_SEL_IVB(pipe));
  3751. else
  3752. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3753. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3754. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3755. }
  3756. }
  3757. void hsw_enable_ips(struct intel_crtc *crtc)
  3758. {
  3759. struct drm_device *dev = crtc->base.dev;
  3760. struct drm_i915_private *dev_priv = dev->dev_private;
  3761. if (!crtc->config->ips_enabled)
  3762. return;
  3763. /*
  3764. * We can only enable IPS after we enable a plane and wait for a vblank
  3765. * This function is called from post_plane_update, which is run after
  3766. * a vblank wait.
  3767. */
  3768. assert_plane_enabled(dev_priv, crtc->plane);
  3769. if (IS_BROADWELL(dev)) {
  3770. mutex_lock(&dev_priv->rps.hw_lock);
  3771. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3772. mutex_unlock(&dev_priv->rps.hw_lock);
  3773. /* Quoting Art Runyan: "its not safe to expect any particular
  3774. * value in IPS_CTL bit 31 after enabling IPS through the
  3775. * mailbox." Moreover, the mailbox may return a bogus state,
  3776. * so we need to just enable it and continue on.
  3777. */
  3778. } else {
  3779. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3780. /* The bit only becomes 1 in the next vblank, so this wait here
  3781. * is essentially intel_wait_for_vblank. If we don't have this
  3782. * and don't wait for vblanks until the end of crtc_enable, then
  3783. * the HW state readout code will complain that the expected
  3784. * IPS_CTL value is not the one we read. */
  3785. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3786. DRM_ERROR("Timed out waiting for IPS enable\n");
  3787. }
  3788. }
  3789. void hsw_disable_ips(struct intel_crtc *crtc)
  3790. {
  3791. struct drm_device *dev = crtc->base.dev;
  3792. struct drm_i915_private *dev_priv = dev->dev_private;
  3793. if (!crtc->config->ips_enabled)
  3794. return;
  3795. assert_plane_enabled(dev_priv, crtc->plane);
  3796. if (IS_BROADWELL(dev)) {
  3797. mutex_lock(&dev_priv->rps.hw_lock);
  3798. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3799. mutex_unlock(&dev_priv->rps.hw_lock);
  3800. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3801. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3802. DRM_ERROR("Timed out waiting for IPS disable\n");
  3803. } else {
  3804. I915_WRITE(IPS_CTL, 0);
  3805. POSTING_READ(IPS_CTL);
  3806. }
  3807. /* We need to wait for a vblank before we can disable the plane. */
  3808. intel_wait_for_vblank(dev, crtc->pipe);
  3809. }
  3810. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3811. {
  3812. if (intel_crtc->overlay) {
  3813. struct drm_device *dev = intel_crtc->base.dev;
  3814. struct drm_i915_private *dev_priv = dev->dev_private;
  3815. mutex_lock(&dev->struct_mutex);
  3816. dev_priv->mm.interruptible = false;
  3817. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3818. dev_priv->mm.interruptible = true;
  3819. mutex_unlock(&dev->struct_mutex);
  3820. }
  3821. /* Let userspace switch the overlay on again. In most cases userspace
  3822. * has to recompute where to put it anyway.
  3823. */
  3824. }
  3825. /**
  3826. * intel_post_enable_primary - Perform operations after enabling primary plane
  3827. * @crtc: the CRTC whose primary plane was just enabled
  3828. *
  3829. * Performs potentially sleeping operations that must be done after the primary
  3830. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3831. * called due to an explicit primary plane update, or due to an implicit
  3832. * re-enable that is caused when a sprite plane is updated to no longer
  3833. * completely hide the primary plane.
  3834. */
  3835. static void
  3836. intel_post_enable_primary(struct drm_crtc *crtc)
  3837. {
  3838. struct drm_device *dev = crtc->dev;
  3839. struct drm_i915_private *dev_priv = dev->dev_private;
  3840. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3841. int pipe = intel_crtc->pipe;
  3842. /*
  3843. * FIXME IPS should be fine as long as one plane is
  3844. * enabled, but in practice it seems to have problems
  3845. * when going from primary only to sprite only and vice
  3846. * versa.
  3847. */
  3848. hsw_enable_ips(intel_crtc);
  3849. /*
  3850. * Gen2 reports pipe underruns whenever all planes are disabled.
  3851. * So don't enable underrun reporting before at least some planes
  3852. * are enabled.
  3853. * FIXME: Need to fix the logic to work when we turn off all planes
  3854. * but leave the pipe running.
  3855. */
  3856. if (IS_GEN2(dev))
  3857. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3858. /* Underruns don't always raise interrupts, so check manually. */
  3859. intel_check_cpu_fifo_underruns(dev_priv);
  3860. intel_check_pch_fifo_underruns(dev_priv);
  3861. }
  3862. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3863. static void
  3864. intel_pre_disable_primary(struct drm_crtc *crtc)
  3865. {
  3866. struct drm_device *dev = crtc->dev;
  3867. struct drm_i915_private *dev_priv = dev->dev_private;
  3868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3869. int pipe = intel_crtc->pipe;
  3870. /*
  3871. * Gen2 reports pipe underruns whenever all planes are disabled.
  3872. * So diasble underrun reporting before all the planes get disabled.
  3873. * FIXME: Need to fix the logic to work when we turn off all planes
  3874. * but leave the pipe running.
  3875. */
  3876. if (IS_GEN2(dev))
  3877. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3878. /*
  3879. * FIXME IPS should be fine as long as one plane is
  3880. * enabled, but in practice it seems to have problems
  3881. * when going from primary only to sprite only and vice
  3882. * versa.
  3883. */
  3884. hsw_disable_ips(intel_crtc);
  3885. }
  3886. /* FIXME get rid of this and use pre_plane_update */
  3887. static void
  3888. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3889. {
  3890. struct drm_device *dev = crtc->dev;
  3891. struct drm_i915_private *dev_priv = dev->dev_private;
  3892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3893. int pipe = intel_crtc->pipe;
  3894. intel_pre_disable_primary(crtc);
  3895. /*
  3896. * Vblank time updates from the shadow to live plane control register
  3897. * are blocked if the memory self-refresh mode is active at that
  3898. * moment. So to make sure the plane gets truly disabled, disable
  3899. * first the self-refresh mode. The self-refresh enable bit in turn
  3900. * will be checked/applied by the HW only at the next frame start
  3901. * event which is after the vblank start event, so we need to have a
  3902. * wait-for-vblank between disabling the plane and the pipe.
  3903. */
  3904. if (HAS_GMCH_DISPLAY(dev)) {
  3905. intel_set_memory_cxsr(dev_priv, false);
  3906. dev_priv->wm.vlv.cxsr = false;
  3907. intel_wait_for_vblank(dev, pipe);
  3908. }
  3909. }
  3910. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3911. {
  3912. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3913. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3914. struct intel_crtc_state *pipe_config =
  3915. to_intel_crtc_state(crtc->base.state);
  3916. struct drm_device *dev = crtc->base.dev;
  3917. struct drm_plane *primary = crtc->base.primary;
  3918. struct drm_plane_state *old_pri_state =
  3919. drm_atomic_get_existing_plane_state(old_state, primary);
  3920. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3921. crtc->wm.cxsr_allowed = true;
  3922. if (pipe_config->update_wm_post && pipe_config->base.active)
  3923. intel_update_watermarks(&crtc->base);
  3924. if (old_pri_state) {
  3925. struct intel_plane_state *primary_state =
  3926. to_intel_plane_state(primary->state);
  3927. struct intel_plane_state *old_primary_state =
  3928. to_intel_plane_state(old_pri_state);
  3929. intel_fbc_post_update(crtc);
  3930. if (primary_state->visible &&
  3931. (needs_modeset(&pipe_config->base) ||
  3932. !old_primary_state->visible))
  3933. intel_post_enable_primary(&crtc->base);
  3934. }
  3935. }
  3936. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3937. {
  3938. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3939. struct drm_device *dev = crtc->base.dev;
  3940. struct drm_i915_private *dev_priv = dev->dev_private;
  3941. struct intel_crtc_state *pipe_config =
  3942. to_intel_crtc_state(crtc->base.state);
  3943. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3944. struct drm_plane *primary = crtc->base.primary;
  3945. struct drm_plane_state *old_pri_state =
  3946. drm_atomic_get_existing_plane_state(old_state, primary);
  3947. bool modeset = needs_modeset(&pipe_config->base);
  3948. if (old_pri_state) {
  3949. struct intel_plane_state *primary_state =
  3950. to_intel_plane_state(primary->state);
  3951. struct intel_plane_state *old_primary_state =
  3952. to_intel_plane_state(old_pri_state);
  3953. intel_fbc_pre_update(crtc);
  3954. if (old_primary_state->visible &&
  3955. (modeset || !primary_state->visible))
  3956. intel_pre_disable_primary(&crtc->base);
  3957. }
  3958. if (pipe_config->disable_cxsr) {
  3959. crtc->wm.cxsr_allowed = false;
  3960. /*
  3961. * Vblank time updates from the shadow to live plane control register
  3962. * are blocked if the memory self-refresh mode is active at that
  3963. * moment. So to make sure the plane gets truly disabled, disable
  3964. * first the self-refresh mode. The self-refresh enable bit in turn
  3965. * will be checked/applied by the HW only at the next frame start
  3966. * event which is after the vblank start event, so we need to have a
  3967. * wait-for-vblank between disabling the plane and the pipe.
  3968. */
  3969. if (old_crtc_state->base.active) {
  3970. intel_set_memory_cxsr(dev_priv, false);
  3971. dev_priv->wm.vlv.cxsr = false;
  3972. intel_wait_for_vblank(dev, crtc->pipe);
  3973. }
  3974. }
  3975. /*
  3976. * IVB workaround: must disable low power watermarks for at least
  3977. * one frame before enabling scaling. LP watermarks can be re-enabled
  3978. * when scaling is disabled.
  3979. *
  3980. * WaCxSRDisabledForSpriteScaling:ivb
  3981. */
  3982. if (pipe_config->disable_lp_wm) {
  3983. ilk_disable_lp_wm(dev);
  3984. intel_wait_for_vblank(dev, crtc->pipe);
  3985. }
  3986. /*
  3987. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3988. * watermark programming here.
  3989. */
  3990. if (needs_modeset(&pipe_config->base))
  3991. return;
  3992. /*
  3993. * For platforms that support atomic watermarks, program the
  3994. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3995. * will be the intermediate values that are safe for both pre- and
  3996. * post- vblank; when vblank happens, the 'active' values will be set
  3997. * to the final 'target' values and we'll do this again to get the
  3998. * optimal watermarks. For gen9+ platforms, the values we program here
  3999. * will be the final target values which will get automatically latched
  4000. * at vblank time; no further programming will be necessary.
  4001. *
  4002. * If a platform hasn't been transitioned to atomic watermarks yet,
  4003. * we'll continue to update watermarks the old way, if flags tell
  4004. * us to.
  4005. */
  4006. if (dev_priv->display.initial_watermarks != NULL)
  4007. dev_priv->display.initial_watermarks(pipe_config);
  4008. else if (pipe_config->update_wm_pre)
  4009. intel_update_watermarks(&crtc->base);
  4010. }
  4011. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4012. {
  4013. struct drm_device *dev = crtc->dev;
  4014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4015. struct drm_plane *p;
  4016. int pipe = intel_crtc->pipe;
  4017. intel_crtc_dpms_overlay_disable(intel_crtc);
  4018. drm_for_each_plane_mask(p, dev, plane_mask)
  4019. to_intel_plane(p)->disable_plane(p, crtc);
  4020. /*
  4021. * FIXME: Once we grow proper nuclear flip support out of this we need
  4022. * to compute the mask of flip planes precisely. For the time being
  4023. * consider this a flip to a NULL plane.
  4024. */
  4025. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4026. }
  4027. static void ironlake_crtc_enable(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. struct intel_encoder *encoder;
  4033. int pipe = intel_crtc->pipe;
  4034. struct intel_crtc_state *pipe_config =
  4035. to_intel_crtc_state(crtc->state);
  4036. if (WARN_ON(intel_crtc->active))
  4037. return;
  4038. /*
  4039. * Sometimes spurious CPU pipe underruns happen during FDI
  4040. * training, at least with VGA+HDMI cloning. Suppress them.
  4041. *
  4042. * On ILK we get an occasional spurious CPU pipe underruns
  4043. * between eDP port A enable and vdd enable. Also PCH port
  4044. * enable seems to result in the occasional CPU pipe underrun.
  4045. *
  4046. * Spurious PCH underruns also occur during PCH enabling.
  4047. */
  4048. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4049. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4050. if (intel_crtc->config->has_pch_encoder)
  4051. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4052. if (intel_crtc->config->has_pch_encoder)
  4053. intel_prepare_shared_dpll(intel_crtc);
  4054. if (intel_crtc->config->has_dp_encoder)
  4055. intel_dp_set_m_n(intel_crtc, M1_N1);
  4056. intel_set_pipe_timings(intel_crtc);
  4057. intel_set_pipe_src_size(intel_crtc);
  4058. if (intel_crtc->config->has_pch_encoder) {
  4059. intel_cpu_transcoder_set_m_n(intel_crtc,
  4060. &intel_crtc->config->fdi_m_n, NULL);
  4061. }
  4062. ironlake_set_pipeconf(crtc);
  4063. intel_crtc->active = true;
  4064. for_each_encoder_on_crtc(dev, crtc, encoder)
  4065. if (encoder->pre_enable)
  4066. encoder->pre_enable(encoder);
  4067. if (intel_crtc->config->has_pch_encoder) {
  4068. /* Note: FDI PLL enabling _must_ be done before we enable the
  4069. * cpu pipes, hence this is separate from all the other fdi/pch
  4070. * enabling. */
  4071. ironlake_fdi_pll_enable(intel_crtc);
  4072. } else {
  4073. assert_fdi_tx_disabled(dev_priv, pipe);
  4074. assert_fdi_rx_disabled(dev_priv, pipe);
  4075. }
  4076. ironlake_pfit_enable(intel_crtc);
  4077. /*
  4078. * On ILK+ LUT must be loaded before the pipe is running but with
  4079. * clocks enabled
  4080. */
  4081. intel_color_load_luts(&pipe_config->base);
  4082. if (dev_priv->display.initial_watermarks != NULL)
  4083. dev_priv->display.initial_watermarks(intel_crtc->config);
  4084. intel_enable_pipe(intel_crtc);
  4085. if (intel_crtc->config->has_pch_encoder)
  4086. ironlake_pch_enable(crtc);
  4087. assert_vblank_disabled(crtc);
  4088. drm_crtc_vblank_on(crtc);
  4089. for_each_encoder_on_crtc(dev, crtc, encoder)
  4090. encoder->enable(encoder);
  4091. if (HAS_PCH_CPT(dev))
  4092. cpt_verify_modeset(dev, intel_crtc->pipe);
  4093. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4094. if (intel_crtc->config->has_pch_encoder)
  4095. intel_wait_for_vblank(dev, pipe);
  4096. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4097. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4098. }
  4099. /* IPS only exists on ULT machines and is tied to pipe A. */
  4100. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4101. {
  4102. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4103. }
  4104. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4105. {
  4106. struct drm_device *dev = crtc->dev;
  4107. struct drm_i915_private *dev_priv = dev->dev_private;
  4108. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4109. struct intel_encoder *encoder;
  4110. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4111. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4112. struct intel_crtc_state *pipe_config =
  4113. to_intel_crtc_state(crtc->state);
  4114. if (WARN_ON(intel_crtc->active))
  4115. return;
  4116. if (intel_crtc->config->has_pch_encoder)
  4117. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4118. false);
  4119. if (intel_crtc->config->shared_dpll)
  4120. intel_enable_shared_dpll(intel_crtc);
  4121. if (intel_crtc->config->has_dp_encoder)
  4122. intel_dp_set_m_n(intel_crtc, M1_N1);
  4123. if (!intel_crtc->config->has_dsi_encoder)
  4124. intel_set_pipe_timings(intel_crtc);
  4125. intel_set_pipe_src_size(intel_crtc);
  4126. if (cpu_transcoder != TRANSCODER_EDP &&
  4127. !transcoder_is_dsi(cpu_transcoder)) {
  4128. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4129. intel_crtc->config->pixel_multiplier - 1);
  4130. }
  4131. if (intel_crtc->config->has_pch_encoder) {
  4132. intel_cpu_transcoder_set_m_n(intel_crtc,
  4133. &intel_crtc->config->fdi_m_n, NULL);
  4134. }
  4135. if (!intel_crtc->config->has_dsi_encoder)
  4136. haswell_set_pipeconf(crtc);
  4137. haswell_set_pipemisc(crtc);
  4138. intel_color_set_csc(&pipe_config->base);
  4139. intel_crtc->active = true;
  4140. if (intel_crtc->config->has_pch_encoder)
  4141. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4142. else
  4143. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4144. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4145. if (encoder->pre_enable)
  4146. encoder->pre_enable(encoder);
  4147. }
  4148. if (intel_crtc->config->has_pch_encoder)
  4149. dev_priv->display.fdi_link_train(crtc);
  4150. if (!intel_crtc->config->has_dsi_encoder)
  4151. intel_ddi_enable_pipe_clock(intel_crtc);
  4152. if (INTEL_INFO(dev)->gen >= 9)
  4153. skylake_pfit_enable(intel_crtc);
  4154. else
  4155. ironlake_pfit_enable(intel_crtc);
  4156. /*
  4157. * On ILK+ LUT must be loaded before the pipe is running but with
  4158. * clocks enabled
  4159. */
  4160. intel_color_load_luts(&pipe_config->base);
  4161. intel_ddi_set_pipe_settings(crtc);
  4162. if (!intel_crtc->config->has_dsi_encoder)
  4163. intel_ddi_enable_transcoder_func(crtc);
  4164. if (dev_priv->display.initial_watermarks != NULL)
  4165. dev_priv->display.initial_watermarks(pipe_config);
  4166. else
  4167. intel_update_watermarks(crtc);
  4168. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4169. if (!intel_crtc->config->has_dsi_encoder)
  4170. intel_enable_pipe(intel_crtc);
  4171. if (intel_crtc->config->has_pch_encoder)
  4172. lpt_pch_enable(crtc);
  4173. if (intel_crtc->config->dp_encoder_is_mst)
  4174. intel_ddi_set_vc_payload_alloc(crtc, true);
  4175. assert_vblank_disabled(crtc);
  4176. drm_crtc_vblank_on(crtc);
  4177. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4178. encoder->enable(encoder);
  4179. intel_opregion_notify_encoder(encoder, true);
  4180. }
  4181. if (intel_crtc->config->has_pch_encoder) {
  4182. intel_wait_for_vblank(dev, pipe);
  4183. intel_wait_for_vblank(dev, pipe);
  4184. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4185. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4186. true);
  4187. }
  4188. /* If we change the relative order between pipe/planes enabling, we need
  4189. * to change the workaround. */
  4190. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4191. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4192. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4193. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4194. }
  4195. }
  4196. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4197. {
  4198. struct drm_device *dev = crtc->base.dev;
  4199. struct drm_i915_private *dev_priv = dev->dev_private;
  4200. int pipe = crtc->pipe;
  4201. /* To avoid upsetting the power well on haswell only disable the pfit if
  4202. * it's in use. The hw state code will make sure we get this right. */
  4203. if (force || crtc->config->pch_pfit.enabled) {
  4204. I915_WRITE(PF_CTL(pipe), 0);
  4205. I915_WRITE(PF_WIN_POS(pipe), 0);
  4206. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4207. }
  4208. }
  4209. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4210. {
  4211. struct drm_device *dev = crtc->dev;
  4212. struct drm_i915_private *dev_priv = dev->dev_private;
  4213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4214. struct intel_encoder *encoder;
  4215. int pipe = intel_crtc->pipe;
  4216. /*
  4217. * Sometimes spurious CPU pipe underruns happen when the
  4218. * pipe is already disabled, but FDI RX/TX is still enabled.
  4219. * Happens at least with VGA+HDMI cloning. Suppress them.
  4220. */
  4221. if (intel_crtc->config->has_pch_encoder) {
  4222. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4223. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4224. }
  4225. for_each_encoder_on_crtc(dev, crtc, encoder)
  4226. encoder->disable(encoder);
  4227. drm_crtc_vblank_off(crtc);
  4228. assert_vblank_disabled(crtc);
  4229. intel_disable_pipe(intel_crtc);
  4230. ironlake_pfit_disable(intel_crtc, false);
  4231. if (intel_crtc->config->has_pch_encoder)
  4232. ironlake_fdi_disable(crtc);
  4233. for_each_encoder_on_crtc(dev, crtc, encoder)
  4234. if (encoder->post_disable)
  4235. encoder->post_disable(encoder);
  4236. if (intel_crtc->config->has_pch_encoder) {
  4237. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4238. if (HAS_PCH_CPT(dev)) {
  4239. i915_reg_t reg;
  4240. u32 temp;
  4241. /* disable TRANS_DP_CTL */
  4242. reg = TRANS_DP_CTL(pipe);
  4243. temp = I915_READ(reg);
  4244. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4245. TRANS_DP_PORT_SEL_MASK);
  4246. temp |= TRANS_DP_PORT_SEL_NONE;
  4247. I915_WRITE(reg, temp);
  4248. /* disable DPLL_SEL */
  4249. temp = I915_READ(PCH_DPLL_SEL);
  4250. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4251. I915_WRITE(PCH_DPLL_SEL, temp);
  4252. }
  4253. ironlake_fdi_pll_disable(intel_crtc);
  4254. }
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4256. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4257. }
  4258. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4259. {
  4260. struct drm_device *dev = crtc->dev;
  4261. struct drm_i915_private *dev_priv = dev->dev_private;
  4262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4263. struct intel_encoder *encoder;
  4264. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4265. if (intel_crtc->config->has_pch_encoder)
  4266. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4267. false);
  4268. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4269. intel_opregion_notify_encoder(encoder, false);
  4270. encoder->disable(encoder);
  4271. }
  4272. drm_crtc_vblank_off(crtc);
  4273. assert_vblank_disabled(crtc);
  4274. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4275. if (!intel_crtc->config->has_dsi_encoder)
  4276. intel_disable_pipe(intel_crtc);
  4277. if (intel_crtc->config->dp_encoder_is_mst)
  4278. intel_ddi_set_vc_payload_alloc(crtc, false);
  4279. if (!intel_crtc->config->has_dsi_encoder)
  4280. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4281. if (INTEL_INFO(dev)->gen >= 9)
  4282. skylake_scaler_disable(intel_crtc);
  4283. else
  4284. ironlake_pfit_disable(intel_crtc, false);
  4285. if (!intel_crtc->config->has_dsi_encoder)
  4286. intel_ddi_disable_pipe_clock(intel_crtc);
  4287. for_each_encoder_on_crtc(dev, crtc, encoder)
  4288. if (encoder->post_disable)
  4289. encoder->post_disable(encoder);
  4290. if (intel_crtc->config->has_pch_encoder) {
  4291. lpt_disable_pch_transcoder(dev_priv);
  4292. lpt_disable_iclkip(dev_priv);
  4293. intel_ddi_fdi_disable(crtc);
  4294. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4295. true);
  4296. }
  4297. }
  4298. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4299. {
  4300. struct drm_device *dev = crtc->base.dev;
  4301. struct drm_i915_private *dev_priv = dev->dev_private;
  4302. struct intel_crtc_state *pipe_config = crtc->config;
  4303. if (!pipe_config->gmch_pfit.control)
  4304. return;
  4305. /*
  4306. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4307. * according to register description and PRM.
  4308. */
  4309. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4310. assert_pipe_disabled(dev_priv, crtc->pipe);
  4311. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4312. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4313. /* Border color in case we don't scale up to the full screen. Black by
  4314. * default, change to something else for debugging. */
  4315. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4316. }
  4317. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4318. {
  4319. switch (port) {
  4320. case PORT_A:
  4321. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4322. case PORT_B:
  4323. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4324. case PORT_C:
  4325. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4326. case PORT_D:
  4327. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4328. case PORT_E:
  4329. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4330. default:
  4331. MISSING_CASE(port);
  4332. return POWER_DOMAIN_PORT_OTHER;
  4333. }
  4334. }
  4335. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4336. {
  4337. switch (port) {
  4338. case PORT_A:
  4339. return POWER_DOMAIN_AUX_A;
  4340. case PORT_B:
  4341. return POWER_DOMAIN_AUX_B;
  4342. case PORT_C:
  4343. return POWER_DOMAIN_AUX_C;
  4344. case PORT_D:
  4345. return POWER_DOMAIN_AUX_D;
  4346. case PORT_E:
  4347. /* FIXME: Check VBT for actual wiring of PORT E */
  4348. return POWER_DOMAIN_AUX_D;
  4349. default:
  4350. MISSING_CASE(port);
  4351. return POWER_DOMAIN_AUX_A;
  4352. }
  4353. }
  4354. enum intel_display_power_domain
  4355. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4356. {
  4357. struct drm_device *dev = intel_encoder->base.dev;
  4358. struct intel_digital_port *intel_dig_port;
  4359. switch (intel_encoder->type) {
  4360. case INTEL_OUTPUT_UNKNOWN:
  4361. /* Only DDI platforms should ever use this output type */
  4362. WARN_ON_ONCE(!HAS_DDI(dev));
  4363. case INTEL_OUTPUT_DISPLAYPORT:
  4364. case INTEL_OUTPUT_HDMI:
  4365. case INTEL_OUTPUT_EDP:
  4366. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4367. return port_to_power_domain(intel_dig_port->port);
  4368. case INTEL_OUTPUT_DP_MST:
  4369. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4370. return port_to_power_domain(intel_dig_port->port);
  4371. case INTEL_OUTPUT_ANALOG:
  4372. return POWER_DOMAIN_PORT_CRT;
  4373. case INTEL_OUTPUT_DSI:
  4374. return POWER_DOMAIN_PORT_DSI;
  4375. default:
  4376. return POWER_DOMAIN_PORT_OTHER;
  4377. }
  4378. }
  4379. enum intel_display_power_domain
  4380. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4381. {
  4382. struct drm_device *dev = intel_encoder->base.dev;
  4383. struct intel_digital_port *intel_dig_port;
  4384. switch (intel_encoder->type) {
  4385. case INTEL_OUTPUT_UNKNOWN:
  4386. case INTEL_OUTPUT_HDMI:
  4387. /*
  4388. * Only DDI platforms should ever use these output types.
  4389. * We can get here after the HDMI detect code has already set
  4390. * the type of the shared encoder. Since we can't be sure
  4391. * what's the status of the given connectors, play safe and
  4392. * run the DP detection too.
  4393. */
  4394. WARN_ON_ONCE(!HAS_DDI(dev));
  4395. case INTEL_OUTPUT_DISPLAYPORT:
  4396. case INTEL_OUTPUT_EDP:
  4397. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4398. return port_to_aux_power_domain(intel_dig_port->port);
  4399. case INTEL_OUTPUT_DP_MST:
  4400. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4401. return port_to_aux_power_domain(intel_dig_port->port);
  4402. default:
  4403. MISSING_CASE(intel_encoder->type);
  4404. return POWER_DOMAIN_AUX_A;
  4405. }
  4406. }
  4407. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4408. struct intel_crtc_state *crtc_state)
  4409. {
  4410. struct drm_device *dev = crtc->dev;
  4411. struct drm_encoder *encoder;
  4412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4413. enum pipe pipe = intel_crtc->pipe;
  4414. unsigned long mask;
  4415. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4416. if (!crtc_state->base.active)
  4417. return 0;
  4418. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4419. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4420. if (crtc_state->pch_pfit.enabled ||
  4421. crtc_state->pch_pfit.force_thru)
  4422. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4423. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4424. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4425. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4426. }
  4427. if (crtc_state->shared_dpll)
  4428. mask |= BIT(POWER_DOMAIN_PLLS);
  4429. return mask;
  4430. }
  4431. static unsigned long
  4432. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4433. struct intel_crtc_state *crtc_state)
  4434. {
  4435. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4436. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4437. enum intel_display_power_domain domain;
  4438. unsigned long domains, new_domains, old_domains;
  4439. old_domains = intel_crtc->enabled_power_domains;
  4440. intel_crtc->enabled_power_domains = new_domains =
  4441. get_crtc_power_domains(crtc, crtc_state);
  4442. domains = new_domains & ~old_domains;
  4443. for_each_power_domain(domain, domains)
  4444. intel_display_power_get(dev_priv, domain);
  4445. return old_domains & ~new_domains;
  4446. }
  4447. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4448. unsigned long domains)
  4449. {
  4450. enum intel_display_power_domain domain;
  4451. for_each_power_domain(domain, domains)
  4452. intel_display_power_put(dev_priv, domain);
  4453. }
  4454. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4455. {
  4456. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4457. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4458. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4459. return max_cdclk_freq;
  4460. else if (IS_CHERRYVIEW(dev_priv))
  4461. return max_cdclk_freq*95/100;
  4462. else if (INTEL_INFO(dev_priv)->gen < 4)
  4463. return 2*max_cdclk_freq*90/100;
  4464. else
  4465. return max_cdclk_freq*90/100;
  4466. }
  4467. static void intel_update_max_cdclk(struct drm_device *dev)
  4468. {
  4469. struct drm_i915_private *dev_priv = dev->dev_private;
  4470. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4471. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4472. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4473. dev_priv->max_cdclk_freq = 675000;
  4474. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4475. dev_priv->max_cdclk_freq = 540000;
  4476. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4477. dev_priv->max_cdclk_freq = 450000;
  4478. else
  4479. dev_priv->max_cdclk_freq = 337500;
  4480. } else if (IS_BROXTON(dev)) {
  4481. dev_priv->max_cdclk_freq = 624000;
  4482. } else if (IS_BROADWELL(dev)) {
  4483. /*
  4484. * FIXME with extra cooling we can allow
  4485. * 540 MHz for ULX and 675 Mhz for ULT.
  4486. * How can we know if extra cooling is
  4487. * available? PCI ID, VTB, something else?
  4488. */
  4489. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4490. dev_priv->max_cdclk_freq = 450000;
  4491. else if (IS_BDW_ULX(dev))
  4492. dev_priv->max_cdclk_freq = 450000;
  4493. else if (IS_BDW_ULT(dev))
  4494. dev_priv->max_cdclk_freq = 540000;
  4495. else
  4496. dev_priv->max_cdclk_freq = 675000;
  4497. } else if (IS_CHERRYVIEW(dev)) {
  4498. dev_priv->max_cdclk_freq = 320000;
  4499. } else if (IS_VALLEYVIEW(dev)) {
  4500. dev_priv->max_cdclk_freq = 400000;
  4501. } else {
  4502. /* otherwise assume cdclk is fixed */
  4503. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4504. }
  4505. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4506. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4507. dev_priv->max_cdclk_freq);
  4508. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4509. dev_priv->max_dotclk_freq);
  4510. }
  4511. static void intel_update_cdclk(struct drm_device *dev)
  4512. {
  4513. struct drm_i915_private *dev_priv = dev->dev_private;
  4514. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4515. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4516. dev_priv->cdclk_freq);
  4517. /*
  4518. * Program the gmbus_freq based on the cdclk frequency.
  4519. * BSpec erroneously claims we should aim for 4MHz, but
  4520. * in fact 1MHz is the correct frequency.
  4521. */
  4522. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  4523. /*
  4524. * Program the gmbus_freq based on the cdclk frequency.
  4525. * BSpec erroneously claims we should aim for 4MHz, but
  4526. * in fact 1MHz is the correct frequency.
  4527. */
  4528. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4529. }
  4530. if (dev_priv->max_cdclk_freq == 0)
  4531. intel_update_max_cdclk(dev);
  4532. }
  4533. static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int frequency)
  4534. {
  4535. uint32_t divider;
  4536. uint32_t ratio;
  4537. uint32_t current_freq;
  4538. int ret;
  4539. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4540. switch (frequency) {
  4541. case 144000:
  4542. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4543. ratio = BXT_DE_PLL_RATIO(60);
  4544. break;
  4545. case 288000:
  4546. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4547. ratio = BXT_DE_PLL_RATIO(60);
  4548. break;
  4549. case 384000:
  4550. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4551. ratio = BXT_DE_PLL_RATIO(60);
  4552. break;
  4553. case 576000:
  4554. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4555. ratio = BXT_DE_PLL_RATIO(60);
  4556. break;
  4557. case 624000:
  4558. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4559. ratio = BXT_DE_PLL_RATIO(65);
  4560. break;
  4561. case 19200:
  4562. /*
  4563. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4564. * to suppress GCC warning.
  4565. */
  4566. ratio = 0;
  4567. divider = 0;
  4568. break;
  4569. default:
  4570. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4571. return;
  4572. }
  4573. mutex_lock(&dev_priv->rps.hw_lock);
  4574. /* Inform power controller of upcoming frequency change */
  4575. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4576. 0x80000000);
  4577. mutex_unlock(&dev_priv->rps.hw_lock);
  4578. if (ret) {
  4579. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4580. ret, frequency);
  4581. return;
  4582. }
  4583. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4584. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4585. current_freq = current_freq * 500 + 1000;
  4586. /*
  4587. * DE PLL has to be disabled when
  4588. * - setting to 19.2MHz (bypass, PLL isn't used)
  4589. * - before setting to 624MHz (PLL needs toggling)
  4590. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4591. */
  4592. if (frequency == 19200 || frequency == 624000 ||
  4593. current_freq == 624000) {
  4594. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4595. /* Timeout 200us */
  4596. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4597. 1))
  4598. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4599. }
  4600. if (frequency != 19200) {
  4601. uint32_t val;
  4602. val = I915_READ(BXT_DE_PLL_CTL);
  4603. val &= ~BXT_DE_PLL_RATIO_MASK;
  4604. val |= ratio;
  4605. I915_WRITE(BXT_DE_PLL_CTL, val);
  4606. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4607. /* Timeout 200us */
  4608. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4609. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4610. val = I915_READ(CDCLK_CTL);
  4611. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4612. val |= divider;
  4613. /*
  4614. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4615. * enable otherwise.
  4616. */
  4617. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4618. if (frequency >= 500000)
  4619. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4620. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4621. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4622. val |= (frequency - 1000) / 500;
  4623. I915_WRITE(CDCLK_CTL, val);
  4624. }
  4625. mutex_lock(&dev_priv->rps.hw_lock);
  4626. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4627. DIV_ROUND_UP(frequency, 25000));
  4628. mutex_unlock(&dev_priv->rps.hw_lock);
  4629. if (ret) {
  4630. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4631. ret, frequency);
  4632. return;
  4633. }
  4634. intel_update_cdclk(dev_priv->dev);
  4635. }
  4636. static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
  4637. {
  4638. if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
  4639. return false;
  4640. /* TODO: Check for a valid CDCLK rate */
  4641. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
  4642. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
  4643. return false;
  4644. }
  4645. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
  4646. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
  4647. return false;
  4648. }
  4649. return true;
  4650. }
  4651. bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
  4652. {
  4653. return broxton_cdclk_is_enabled(dev_priv);
  4654. }
  4655. void broxton_init_cdclk(struct drm_i915_private *dev_priv)
  4656. {
  4657. /* check if cd clock is enabled */
  4658. if (broxton_cdclk_is_enabled(dev_priv)) {
  4659. DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
  4660. return;
  4661. }
  4662. DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
  4663. /*
  4664. * FIXME:
  4665. * - The initial CDCLK needs to be read from VBT.
  4666. * Need to make this change after VBT has changes for BXT.
  4667. * - check if setting the max (or any) cdclk freq is really necessary
  4668. * here, it belongs to modeset time
  4669. */
  4670. broxton_set_cdclk(dev_priv, 624000);
  4671. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4672. POSTING_READ(DBUF_CTL);
  4673. udelay(10);
  4674. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4675. DRM_ERROR("DBuf power enable timeout!\n");
  4676. }
  4677. void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
  4678. {
  4679. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4680. POSTING_READ(DBUF_CTL);
  4681. udelay(10);
  4682. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4683. DRM_ERROR("DBuf power disable timeout!\n");
  4684. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4685. broxton_set_cdclk(dev_priv, 19200);
  4686. }
  4687. static const struct skl_cdclk_entry {
  4688. unsigned int freq;
  4689. unsigned int vco;
  4690. } skl_cdclk_frequencies[] = {
  4691. { .freq = 308570, .vco = 8640 },
  4692. { .freq = 337500, .vco = 8100 },
  4693. { .freq = 432000, .vco = 8640 },
  4694. { .freq = 450000, .vco = 8100 },
  4695. { .freq = 540000, .vco = 8100 },
  4696. { .freq = 617140, .vco = 8640 },
  4697. { .freq = 675000, .vco = 8100 },
  4698. };
  4699. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4700. {
  4701. return (freq - 1000) / 500;
  4702. }
  4703. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4704. {
  4705. unsigned int i;
  4706. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4707. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4708. if (e->freq == freq)
  4709. return e->vco;
  4710. }
  4711. return 8100;
  4712. }
  4713. static void
  4714. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4715. {
  4716. unsigned int min_freq;
  4717. u32 val;
  4718. /* select the minimum CDCLK before enabling DPLL 0 */
  4719. val = I915_READ(CDCLK_CTL);
  4720. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4721. val |= CDCLK_FREQ_337_308;
  4722. if (required_vco == 8640)
  4723. min_freq = 308570;
  4724. else
  4725. min_freq = 337500;
  4726. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4727. I915_WRITE(CDCLK_CTL, val);
  4728. POSTING_READ(CDCLK_CTL);
  4729. /*
  4730. * We always enable DPLL0 with the lowest link rate possible, but still
  4731. * taking into account the VCO required to operate the eDP panel at the
  4732. * desired frequency. The usual DP link rates operate with a VCO of
  4733. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4734. * The modeset code is responsible for the selection of the exact link
  4735. * rate later on, with the constraint of choosing a frequency that
  4736. * works with required_vco.
  4737. */
  4738. val = I915_READ(DPLL_CTRL1);
  4739. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4740. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4741. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4742. if (required_vco == 8640)
  4743. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4744. SKL_DPLL0);
  4745. else
  4746. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4747. SKL_DPLL0);
  4748. I915_WRITE(DPLL_CTRL1, val);
  4749. POSTING_READ(DPLL_CTRL1);
  4750. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4751. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4752. DRM_ERROR("DPLL0 not locked\n");
  4753. }
  4754. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4755. {
  4756. int ret;
  4757. u32 val;
  4758. /* inform PCU we want to change CDCLK */
  4759. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4760. mutex_lock(&dev_priv->rps.hw_lock);
  4761. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4762. mutex_unlock(&dev_priv->rps.hw_lock);
  4763. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4764. }
  4765. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4766. {
  4767. unsigned int i;
  4768. for (i = 0; i < 15; i++) {
  4769. if (skl_cdclk_pcu_ready(dev_priv))
  4770. return true;
  4771. udelay(10);
  4772. }
  4773. return false;
  4774. }
  4775. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4776. {
  4777. struct drm_device *dev = dev_priv->dev;
  4778. u32 freq_select, pcu_ack;
  4779. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4780. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4781. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4782. return;
  4783. }
  4784. /* set CDCLK_CTL */
  4785. switch(freq) {
  4786. case 450000:
  4787. case 432000:
  4788. freq_select = CDCLK_FREQ_450_432;
  4789. pcu_ack = 1;
  4790. break;
  4791. case 540000:
  4792. freq_select = CDCLK_FREQ_540;
  4793. pcu_ack = 2;
  4794. break;
  4795. case 308570:
  4796. case 337500:
  4797. default:
  4798. freq_select = CDCLK_FREQ_337_308;
  4799. pcu_ack = 0;
  4800. break;
  4801. case 617140:
  4802. case 675000:
  4803. freq_select = CDCLK_FREQ_675_617;
  4804. pcu_ack = 3;
  4805. break;
  4806. }
  4807. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4808. POSTING_READ(CDCLK_CTL);
  4809. /* inform PCU of the change */
  4810. mutex_lock(&dev_priv->rps.hw_lock);
  4811. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4812. mutex_unlock(&dev_priv->rps.hw_lock);
  4813. intel_update_cdclk(dev);
  4814. }
  4815. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4816. {
  4817. /* disable DBUF power */
  4818. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4819. POSTING_READ(DBUF_CTL);
  4820. udelay(10);
  4821. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4822. DRM_ERROR("DBuf power disable timeout\n");
  4823. /* disable DPLL0 */
  4824. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4825. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4826. DRM_ERROR("Couldn't disable DPLL0\n");
  4827. }
  4828. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4829. {
  4830. unsigned int required_vco;
  4831. /* DPLL0 not enabled (happens on early BIOS versions) */
  4832. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4833. /* enable DPLL0 */
  4834. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4835. skl_dpll0_enable(dev_priv, required_vco);
  4836. }
  4837. /* set CDCLK to the frequency the BIOS chose */
  4838. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4839. /* enable DBUF power */
  4840. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4841. POSTING_READ(DBUF_CTL);
  4842. udelay(10);
  4843. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4844. DRM_ERROR("DBuf power enable timeout\n");
  4845. }
  4846. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4847. {
  4848. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4849. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4850. int freq = dev_priv->skl_boot_cdclk;
  4851. /*
  4852. * check if the pre-os intialized the display
  4853. * There is SWF18 scratchpad register defined which is set by the
  4854. * pre-os which can be used by the OS drivers to check the status
  4855. */
  4856. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4857. goto sanitize;
  4858. /* Is PLL enabled and locked ? */
  4859. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4860. goto sanitize;
  4861. /* DPLL okay; verify the cdclock
  4862. *
  4863. * Noticed in some instances that the freq selection is correct but
  4864. * decimal part is programmed wrong from BIOS where pre-os does not
  4865. * enable display. Verify the same as well.
  4866. */
  4867. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4868. /* All well; nothing to sanitize */
  4869. return false;
  4870. sanitize:
  4871. /*
  4872. * As of now initialize with max cdclk till
  4873. * we get dynamic cdclk support
  4874. * */
  4875. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4876. skl_init_cdclk(dev_priv);
  4877. /* we did have to sanitize */
  4878. return true;
  4879. }
  4880. /* Adjust CDclk dividers to allow high res or save power if possible */
  4881. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4882. {
  4883. struct drm_i915_private *dev_priv = dev->dev_private;
  4884. u32 val, cmd;
  4885. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4886. != dev_priv->cdclk_freq);
  4887. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4888. cmd = 2;
  4889. else if (cdclk == 266667)
  4890. cmd = 1;
  4891. else
  4892. cmd = 0;
  4893. mutex_lock(&dev_priv->rps.hw_lock);
  4894. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4895. val &= ~DSPFREQGUAR_MASK;
  4896. val |= (cmd << DSPFREQGUAR_SHIFT);
  4897. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4898. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4899. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4900. 50)) {
  4901. DRM_ERROR("timed out waiting for CDclk change\n");
  4902. }
  4903. mutex_unlock(&dev_priv->rps.hw_lock);
  4904. mutex_lock(&dev_priv->sb_lock);
  4905. if (cdclk == 400000) {
  4906. u32 divider;
  4907. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4908. /* adjust cdclk divider */
  4909. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4910. val &= ~CCK_FREQUENCY_VALUES;
  4911. val |= divider;
  4912. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4913. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4914. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4915. 50))
  4916. DRM_ERROR("timed out waiting for CDclk change\n");
  4917. }
  4918. /* adjust self-refresh exit latency value */
  4919. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4920. val &= ~0x7f;
  4921. /*
  4922. * For high bandwidth configs, we set a higher latency in the bunit
  4923. * so that the core display fetch happens in time to avoid underruns.
  4924. */
  4925. if (cdclk == 400000)
  4926. val |= 4500 / 250; /* 4.5 usec */
  4927. else
  4928. val |= 3000 / 250; /* 3.0 usec */
  4929. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4930. mutex_unlock(&dev_priv->sb_lock);
  4931. intel_update_cdclk(dev);
  4932. }
  4933. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4934. {
  4935. struct drm_i915_private *dev_priv = dev->dev_private;
  4936. u32 val, cmd;
  4937. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4938. != dev_priv->cdclk_freq);
  4939. switch (cdclk) {
  4940. case 333333:
  4941. case 320000:
  4942. case 266667:
  4943. case 200000:
  4944. break;
  4945. default:
  4946. MISSING_CASE(cdclk);
  4947. return;
  4948. }
  4949. /*
  4950. * Specs are full of misinformation, but testing on actual
  4951. * hardware has shown that we just need to write the desired
  4952. * CCK divider into the Punit register.
  4953. */
  4954. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4955. mutex_lock(&dev_priv->rps.hw_lock);
  4956. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4957. val &= ~DSPFREQGUAR_MASK_CHV;
  4958. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4959. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4960. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4961. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4962. 50)) {
  4963. DRM_ERROR("timed out waiting for CDclk change\n");
  4964. }
  4965. mutex_unlock(&dev_priv->rps.hw_lock);
  4966. intel_update_cdclk(dev);
  4967. }
  4968. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4969. int max_pixclk)
  4970. {
  4971. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4972. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4973. /*
  4974. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4975. * 200MHz
  4976. * 267MHz
  4977. * 320/333MHz (depends on HPLL freq)
  4978. * 400MHz (VLV only)
  4979. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4980. * of the lower bin and adjust if needed.
  4981. *
  4982. * We seem to get an unstable or solid color picture at 200MHz.
  4983. * Not sure what's wrong. For now use 200MHz only when all pipes
  4984. * are off.
  4985. */
  4986. if (!IS_CHERRYVIEW(dev_priv) &&
  4987. max_pixclk > freq_320*limit/100)
  4988. return 400000;
  4989. else if (max_pixclk > 266667*limit/100)
  4990. return freq_320;
  4991. else if (max_pixclk > 0)
  4992. return 266667;
  4993. else
  4994. return 200000;
  4995. }
  4996. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4997. int max_pixclk)
  4998. {
  4999. /*
  5000. * FIXME:
  5001. * - remove the guardband, it's not needed on BXT
  5002. * - set 19.2MHz bypass frequency if there are no active pipes
  5003. */
  5004. if (max_pixclk > 576000*9/10)
  5005. return 624000;
  5006. else if (max_pixclk > 384000*9/10)
  5007. return 576000;
  5008. else if (max_pixclk > 288000*9/10)
  5009. return 384000;
  5010. else if (max_pixclk > 144000*9/10)
  5011. return 288000;
  5012. else
  5013. return 144000;
  5014. }
  5015. /* Compute the max pixel clock for new configuration. */
  5016. static int intel_mode_max_pixclk(struct drm_device *dev,
  5017. struct drm_atomic_state *state)
  5018. {
  5019. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5020. struct drm_i915_private *dev_priv = dev->dev_private;
  5021. struct drm_crtc *crtc;
  5022. struct drm_crtc_state *crtc_state;
  5023. unsigned max_pixclk = 0, i;
  5024. enum pipe pipe;
  5025. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5026. sizeof(intel_state->min_pixclk));
  5027. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5028. int pixclk = 0;
  5029. if (crtc_state->enable)
  5030. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5031. intel_state->min_pixclk[i] = pixclk;
  5032. }
  5033. for_each_pipe(dev_priv, pipe)
  5034. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5035. return max_pixclk;
  5036. }
  5037. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5038. {
  5039. struct drm_device *dev = state->dev;
  5040. struct drm_i915_private *dev_priv = dev->dev_private;
  5041. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5042. struct intel_atomic_state *intel_state =
  5043. to_intel_atomic_state(state);
  5044. if (max_pixclk < 0)
  5045. return max_pixclk;
  5046. intel_state->cdclk = intel_state->dev_cdclk =
  5047. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5048. if (!intel_state->active_crtcs)
  5049. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5050. return 0;
  5051. }
  5052. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5053. {
  5054. struct drm_device *dev = state->dev;
  5055. struct drm_i915_private *dev_priv = dev->dev_private;
  5056. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5057. struct intel_atomic_state *intel_state =
  5058. to_intel_atomic_state(state);
  5059. if (max_pixclk < 0)
  5060. return max_pixclk;
  5061. intel_state->cdclk = intel_state->dev_cdclk =
  5062. broxton_calc_cdclk(dev_priv, max_pixclk);
  5063. if (!intel_state->active_crtcs)
  5064. intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
  5065. return 0;
  5066. }
  5067. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5068. {
  5069. unsigned int credits, default_credits;
  5070. if (IS_CHERRYVIEW(dev_priv))
  5071. default_credits = PFI_CREDIT(12);
  5072. else
  5073. default_credits = PFI_CREDIT(8);
  5074. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5075. /* CHV suggested value is 31 or 63 */
  5076. if (IS_CHERRYVIEW(dev_priv))
  5077. credits = PFI_CREDIT_63;
  5078. else
  5079. credits = PFI_CREDIT(15);
  5080. } else {
  5081. credits = default_credits;
  5082. }
  5083. /*
  5084. * WA - write default credits before re-programming
  5085. * FIXME: should we also set the resend bit here?
  5086. */
  5087. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5088. default_credits);
  5089. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5090. credits | PFI_CREDIT_RESEND);
  5091. /*
  5092. * FIXME is this guaranteed to clear
  5093. * immediately or should we poll for it?
  5094. */
  5095. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5096. }
  5097. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5098. {
  5099. struct drm_device *dev = old_state->dev;
  5100. struct drm_i915_private *dev_priv = dev->dev_private;
  5101. struct intel_atomic_state *old_intel_state =
  5102. to_intel_atomic_state(old_state);
  5103. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5104. /*
  5105. * FIXME: We can end up here with all power domains off, yet
  5106. * with a CDCLK frequency other than the minimum. To account
  5107. * for this take the PIPE-A power domain, which covers the HW
  5108. * blocks needed for the following programming. This can be
  5109. * removed once it's guaranteed that we get here either with
  5110. * the minimum CDCLK set, or the required power domains
  5111. * enabled.
  5112. */
  5113. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5114. if (IS_CHERRYVIEW(dev))
  5115. cherryview_set_cdclk(dev, req_cdclk);
  5116. else
  5117. valleyview_set_cdclk(dev, req_cdclk);
  5118. vlv_program_pfi_credits(dev_priv);
  5119. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5120. }
  5121. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5122. {
  5123. struct drm_device *dev = crtc->dev;
  5124. struct drm_i915_private *dev_priv = to_i915(dev);
  5125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5126. struct intel_encoder *encoder;
  5127. struct intel_crtc_state *pipe_config =
  5128. to_intel_crtc_state(crtc->state);
  5129. int pipe = intel_crtc->pipe;
  5130. if (WARN_ON(intel_crtc->active))
  5131. return;
  5132. if (intel_crtc->config->has_dp_encoder)
  5133. intel_dp_set_m_n(intel_crtc, M1_N1);
  5134. intel_set_pipe_timings(intel_crtc);
  5135. intel_set_pipe_src_size(intel_crtc);
  5136. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5137. struct drm_i915_private *dev_priv = dev->dev_private;
  5138. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5139. I915_WRITE(CHV_CANVAS(pipe), 0);
  5140. }
  5141. i9xx_set_pipeconf(intel_crtc);
  5142. intel_crtc->active = true;
  5143. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5144. for_each_encoder_on_crtc(dev, crtc, encoder)
  5145. if (encoder->pre_pll_enable)
  5146. encoder->pre_pll_enable(encoder);
  5147. if (IS_CHERRYVIEW(dev)) {
  5148. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5149. chv_enable_pll(intel_crtc, intel_crtc->config);
  5150. } else {
  5151. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5152. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5153. }
  5154. for_each_encoder_on_crtc(dev, crtc, encoder)
  5155. if (encoder->pre_enable)
  5156. encoder->pre_enable(encoder);
  5157. i9xx_pfit_enable(intel_crtc);
  5158. intel_color_load_luts(&pipe_config->base);
  5159. intel_update_watermarks(crtc);
  5160. intel_enable_pipe(intel_crtc);
  5161. assert_vblank_disabled(crtc);
  5162. drm_crtc_vblank_on(crtc);
  5163. for_each_encoder_on_crtc(dev, crtc, encoder)
  5164. encoder->enable(encoder);
  5165. }
  5166. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5167. {
  5168. struct drm_device *dev = crtc->base.dev;
  5169. struct drm_i915_private *dev_priv = dev->dev_private;
  5170. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5171. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5172. }
  5173. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5174. {
  5175. struct drm_device *dev = crtc->dev;
  5176. struct drm_i915_private *dev_priv = to_i915(dev);
  5177. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5178. struct intel_encoder *encoder;
  5179. struct intel_crtc_state *pipe_config =
  5180. to_intel_crtc_state(crtc->state);
  5181. enum pipe pipe = intel_crtc->pipe;
  5182. if (WARN_ON(intel_crtc->active))
  5183. return;
  5184. i9xx_set_pll_dividers(intel_crtc);
  5185. if (intel_crtc->config->has_dp_encoder)
  5186. intel_dp_set_m_n(intel_crtc, M1_N1);
  5187. intel_set_pipe_timings(intel_crtc);
  5188. intel_set_pipe_src_size(intel_crtc);
  5189. i9xx_set_pipeconf(intel_crtc);
  5190. intel_crtc->active = true;
  5191. if (!IS_GEN2(dev))
  5192. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5193. for_each_encoder_on_crtc(dev, crtc, encoder)
  5194. if (encoder->pre_enable)
  5195. encoder->pre_enable(encoder);
  5196. i9xx_enable_pll(intel_crtc);
  5197. i9xx_pfit_enable(intel_crtc);
  5198. intel_color_load_luts(&pipe_config->base);
  5199. intel_update_watermarks(crtc);
  5200. intel_enable_pipe(intel_crtc);
  5201. assert_vblank_disabled(crtc);
  5202. drm_crtc_vblank_on(crtc);
  5203. for_each_encoder_on_crtc(dev, crtc, encoder)
  5204. encoder->enable(encoder);
  5205. }
  5206. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5207. {
  5208. struct drm_device *dev = crtc->base.dev;
  5209. struct drm_i915_private *dev_priv = dev->dev_private;
  5210. if (!crtc->config->gmch_pfit.control)
  5211. return;
  5212. assert_pipe_disabled(dev_priv, crtc->pipe);
  5213. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5214. I915_READ(PFIT_CONTROL));
  5215. I915_WRITE(PFIT_CONTROL, 0);
  5216. }
  5217. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5218. {
  5219. struct drm_device *dev = crtc->dev;
  5220. struct drm_i915_private *dev_priv = dev->dev_private;
  5221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5222. struct intel_encoder *encoder;
  5223. int pipe = intel_crtc->pipe;
  5224. /*
  5225. * On gen2 planes are double buffered but the pipe isn't, so we must
  5226. * wait for planes to fully turn off before disabling the pipe.
  5227. */
  5228. if (IS_GEN2(dev))
  5229. intel_wait_for_vblank(dev, pipe);
  5230. for_each_encoder_on_crtc(dev, crtc, encoder)
  5231. encoder->disable(encoder);
  5232. drm_crtc_vblank_off(crtc);
  5233. assert_vblank_disabled(crtc);
  5234. intel_disable_pipe(intel_crtc);
  5235. i9xx_pfit_disable(intel_crtc);
  5236. for_each_encoder_on_crtc(dev, crtc, encoder)
  5237. if (encoder->post_disable)
  5238. encoder->post_disable(encoder);
  5239. if (!intel_crtc->config->has_dsi_encoder) {
  5240. if (IS_CHERRYVIEW(dev))
  5241. chv_disable_pll(dev_priv, pipe);
  5242. else if (IS_VALLEYVIEW(dev))
  5243. vlv_disable_pll(dev_priv, pipe);
  5244. else
  5245. i9xx_disable_pll(intel_crtc);
  5246. }
  5247. for_each_encoder_on_crtc(dev, crtc, encoder)
  5248. if (encoder->post_pll_disable)
  5249. encoder->post_pll_disable(encoder);
  5250. if (!IS_GEN2(dev))
  5251. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5252. }
  5253. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5254. {
  5255. struct intel_encoder *encoder;
  5256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5257. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5258. enum intel_display_power_domain domain;
  5259. unsigned long domains;
  5260. if (!intel_crtc->active)
  5261. return;
  5262. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5263. WARN_ON(intel_crtc->unpin_work);
  5264. intel_pre_disable_primary_noatomic(crtc);
  5265. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5266. to_intel_plane_state(crtc->primary->state)->visible = false;
  5267. }
  5268. dev_priv->display.crtc_disable(crtc);
  5269. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
  5270. crtc->base.id);
  5271. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5272. crtc->state->active = false;
  5273. intel_crtc->active = false;
  5274. crtc->enabled = false;
  5275. crtc->state->connector_mask = 0;
  5276. crtc->state->encoder_mask = 0;
  5277. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5278. encoder->base.crtc = NULL;
  5279. intel_fbc_disable(intel_crtc);
  5280. intel_update_watermarks(crtc);
  5281. intel_disable_shared_dpll(intel_crtc);
  5282. domains = intel_crtc->enabled_power_domains;
  5283. for_each_power_domain(domain, domains)
  5284. intel_display_power_put(dev_priv, domain);
  5285. intel_crtc->enabled_power_domains = 0;
  5286. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5287. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5288. }
  5289. /*
  5290. * turn all crtc's off, but do not adjust state
  5291. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5292. */
  5293. int intel_display_suspend(struct drm_device *dev)
  5294. {
  5295. struct drm_i915_private *dev_priv = to_i915(dev);
  5296. struct drm_atomic_state *state;
  5297. int ret;
  5298. state = drm_atomic_helper_suspend(dev);
  5299. ret = PTR_ERR_OR_ZERO(state);
  5300. if (ret)
  5301. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5302. else
  5303. dev_priv->modeset_restore_state = state;
  5304. return ret;
  5305. }
  5306. void intel_encoder_destroy(struct drm_encoder *encoder)
  5307. {
  5308. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5309. drm_encoder_cleanup(encoder);
  5310. kfree(intel_encoder);
  5311. }
  5312. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5313. * internal consistency). */
  5314. static void intel_connector_verify_state(struct intel_connector *connector)
  5315. {
  5316. struct drm_crtc *crtc = connector->base.state->crtc;
  5317. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5318. connector->base.base.id,
  5319. connector->base.name);
  5320. if (connector->get_hw_state(connector)) {
  5321. struct intel_encoder *encoder = connector->encoder;
  5322. struct drm_connector_state *conn_state = connector->base.state;
  5323. I915_STATE_WARN(!crtc,
  5324. "connector enabled without attached crtc\n");
  5325. if (!crtc)
  5326. return;
  5327. I915_STATE_WARN(!crtc->state->active,
  5328. "connector is active, but attached crtc isn't\n");
  5329. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5330. return;
  5331. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5332. "atomic encoder doesn't match attached encoder\n");
  5333. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5334. "attached encoder crtc differs from connector crtc\n");
  5335. } else {
  5336. I915_STATE_WARN(crtc && crtc->state->active,
  5337. "attached crtc is active, but connector isn't\n");
  5338. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5339. "best encoder set without crtc!\n");
  5340. }
  5341. }
  5342. int intel_connector_init(struct intel_connector *connector)
  5343. {
  5344. drm_atomic_helper_connector_reset(&connector->base);
  5345. if (!connector->base.state)
  5346. return -ENOMEM;
  5347. return 0;
  5348. }
  5349. struct intel_connector *intel_connector_alloc(void)
  5350. {
  5351. struct intel_connector *connector;
  5352. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5353. if (!connector)
  5354. return NULL;
  5355. if (intel_connector_init(connector) < 0) {
  5356. kfree(connector);
  5357. return NULL;
  5358. }
  5359. return connector;
  5360. }
  5361. /* Simple connector->get_hw_state implementation for encoders that support only
  5362. * one connector and no cloning and hence the encoder state determines the state
  5363. * of the connector. */
  5364. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5365. {
  5366. enum pipe pipe = 0;
  5367. struct intel_encoder *encoder = connector->encoder;
  5368. return encoder->get_hw_state(encoder, &pipe);
  5369. }
  5370. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5371. {
  5372. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5373. return crtc_state->fdi_lanes;
  5374. return 0;
  5375. }
  5376. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5377. struct intel_crtc_state *pipe_config)
  5378. {
  5379. struct drm_atomic_state *state = pipe_config->base.state;
  5380. struct intel_crtc *other_crtc;
  5381. struct intel_crtc_state *other_crtc_state;
  5382. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5383. pipe_name(pipe), pipe_config->fdi_lanes);
  5384. if (pipe_config->fdi_lanes > 4) {
  5385. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5386. pipe_name(pipe), pipe_config->fdi_lanes);
  5387. return -EINVAL;
  5388. }
  5389. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5390. if (pipe_config->fdi_lanes > 2) {
  5391. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5392. pipe_config->fdi_lanes);
  5393. return -EINVAL;
  5394. } else {
  5395. return 0;
  5396. }
  5397. }
  5398. if (INTEL_INFO(dev)->num_pipes == 2)
  5399. return 0;
  5400. /* Ivybridge 3 pipe is really complicated */
  5401. switch (pipe) {
  5402. case PIPE_A:
  5403. return 0;
  5404. case PIPE_B:
  5405. if (pipe_config->fdi_lanes <= 2)
  5406. return 0;
  5407. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5408. other_crtc_state =
  5409. intel_atomic_get_crtc_state(state, other_crtc);
  5410. if (IS_ERR(other_crtc_state))
  5411. return PTR_ERR(other_crtc_state);
  5412. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5413. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5414. pipe_name(pipe), pipe_config->fdi_lanes);
  5415. return -EINVAL;
  5416. }
  5417. return 0;
  5418. case PIPE_C:
  5419. if (pipe_config->fdi_lanes > 2) {
  5420. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5421. pipe_name(pipe), pipe_config->fdi_lanes);
  5422. return -EINVAL;
  5423. }
  5424. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5425. other_crtc_state =
  5426. intel_atomic_get_crtc_state(state, other_crtc);
  5427. if (IS_ERR(other_crtc_state))
  5428. return PTR_ERR(other_crtc_state);
  5429. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5430. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5431. return -EINVAL;
  5432. }
  5433. return 0;
  5434. default:
  5435. BUG();
  5436. }
  5437. }
  5438. #define RETRY 1
  5439. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5440. struct intel_crtc_state *pipe_config)
  5441. {
  5442. struct drm_device *dev = intel_crtc->base.dev;
  5443. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5444. int lane, link_bw, fdi_dotclock, ret;
  5445. bool needs_recompute = false;
  5446. retry:
  5447. /* FDI is a binary signal running at ~2.7GHz, encoding
  5448. * each output octet as 10 bits. The actual frequency
  5449. * is stored as a divider into a 100MHz clock, and the
  5450. * mode pixel clock is stored in units of 1KHz.
  5451. * Hence the bw of each lane in terms of the mode signal
  5452. * is:
  5453. */
  5454. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5455. fdi_dotclock = adjusted_mode->crtc_clock;
  5456. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5457. pipe_config->pipe_bpp);
  5458. pipe_config->fdi_lanes = lane;
  5459. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5460. link_bw, &pipe_config->fdi_m_n);
  5461. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5462. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5463. pipe_config->pipe_bpp -= 2*3;
  5464. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5465. pipe_config->pipe_bpp);
  5466. needs_recompute = true;
  5467. pipe_config->bw_constrained = true;
  5468. goto retry;
  5469. }
  5470. if (needs_recompute)
  5471. return RETRY;
  5472. return ret;
  5473. }
  5474. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5475. struct intel_crtc_state *pipe_config)
  5476. {
  5477. if (pipe_config->pipe_bpp > 24)
  5478. return false;
  5479. /* HSW can handle pixel rate up to cdclk? */
  5480. if (IS_HASWELL(dev_priv))
  5481. return true;
  5482. /*
  5483. * We compare against max which means we must take
  5484. * the increased cdclk requirement into account when
  5485. * calculating the new cdclk.
  5486. *
  5487. * Should measure whether using a lower cdclk w/o IPS
  5488. */
  5489. return ilk_pipe_pixel_rate(pipe_config) <=
  5490. dev_priv->max_cdclk_freq * 95 / 100;
  5491. }
  5492. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5493. struct intel_crtc_state *pipe_config)
  5494. {
  5495. struct drm_device *dev = crtc->base.dev;
  5496. struct drm_i915_private *dev_priv = dev->dev_private;
  5497. pipe_config->ips_enabled = i915.enable_ips &&
  5498. hsw_crtc_supports_ips(crtc) &&
  5499. pipe_config_supports_ips(dev_priv, pipe_config);
  5500. }
  5501. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5502. {
  5503. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5504. /* GDG double wide on either pipe, otherwise pipe A only */
  5505. return INTEL_INFO(dev_priv)->gen < 4 &&
  5506. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5507. }
  5508. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5509. struct intel_crtc_state *pipe_config)
  5510. {
  5511. struct drm_device *dev = crtc->base.dev;
  5512. struct drm_i915_private *dev_priv = dev->dev_private;
  5513. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5514. /* FIXME should check pixel clock limits on all platforms */
  5515. if (INTEL_INFO(dev)->gen < 4) {
  5516. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5517. /*
  5518. * Enable double wide mode when the dot clock
  5519. * is > 90% of the (display) core speed.
  5520. */
  5521. if (intel_crtc_supports_double_wide(crtc) &&
  5522. adjusted_mode->crtc_clock > clock_limit) {
  5523. clock_limit *= 2;
  5524. pipe_config->double_wide = true;
  5525. }
  5526. if (adjusted_mode->crtc_clock > clock_limit) {
  5527. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5528. adjusted_mode->crtc_clock, clock_limit,
  5529. yesno(pipe_config->double_wide));
  5530. return -EINVAL;
  5531. }
  5532. }
  5533. /*
  5534. * Pipe horizontal size must be even in:
  5535. * - DVO ganged mode
  5536. * - LVDS dual channel mode
  5537. * - Double wide pipe
  5538. */
  5539. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5540. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5541. pipe_config->pipe_src_w &= ~1;
  5542. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5543. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5544. */
  5545. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5546. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5547. return -EINVAL;
  5548. if (HAS_IPS(dev))
  5549. hsw_compute_ips_config(crtc, pipe_config);
  5550. if (pipe_config->has_pch_encoder)
  5551. return ironlake_fdi_compute_config(crtc, pipe_config);
  5552. return 0;
  5553. }
  5554. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5555. {
  5556. struct drm_i915_private *dev_priv = to_i915(dev);
  5557. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5558. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5559. uint32_t linkrate;
  5560. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5561. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5562. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5563. return 540000;
  5564. linkrate = (I915_READ(DPLL_CTRL1) &
  5565. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5566. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5567. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5568. /* vco 8640 */
  5569. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5570. case CDCLK_FREQ_450_432:
  5571. return 432000;
  5572. case CDCLK_FREQ_337_308:
  5573. return 308570;
  5574. case CDCLK_FREQ_675_617:
  5575. return 617140;
  5576. default:
  5577. WARN(1, "Unknown cd freq selection\n");
  5578. }
  5579. } else {
  5580. /* vco 8100 */
  5581. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5582. case CDCLK_FREQ_450_432:
  5583. return 450000;
  5584. case CDCLK_FREQ_337_308:
  5585. return 337500;
  5586. case CDCLK_FREQ_675_617:
  5587. return 675000;
  5588. default:
  5589. WARN(1, "Unknown cd freq selection\n");
  5590. }
  5591. }
  5592. /* error case, do as if DPLL0 isn't enabled */
  5593. return 24000;
  5594. }
  5595. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5596. {
  5597. struct drm_i915_private *dev_priv = to_i915(dev);
  5598. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5599. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5600. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5601. int cdclk;
  5602. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5603. return 19200;
  5604. cdclk = 19200 * pll_ratio / 2;
  5605. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5606. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5607. return cdclk; /* 576MHz or 624MHz */
  5608. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5609. return cdclk * 2 / 3; /* 384MHz */
  5610. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5611. return cdclk / 2; /* 288MHz */
  5612. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5613. return cdclk / 4; /* 144MHz */
  5614. }
  5615. /* error case, do as if DE PLL isn't enabled */
  5616. return 19200;
  5617. }
  5618. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5619. {
  5620. struct drm_i915_private *dev_priv = dev->dev_private;
  5621. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5622. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5623. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5624. return 800000;
  5625. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5626. return 450000;
  5627. else if (freq == LCPLL_CLK_FREQ_450)
  5628. return 450000;
  5629. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5630. return 540000;
  5631. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5632. return 337500;
  5633. else
  5634. return 675000;
  5635. }
  5636. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5637. {
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5640. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5641. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5642. return 800000;
  5643. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5644. return 450000;
  5645. else if (freq == LCPLL_CLK_FREQ_450)
  5646. return 450000;
  5647. else if (IS_HSW_ULT(dev))
  5648. return 337500;
  5649. else
  5650. return 540000;
  5651. }
  5652. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5653. {
  5654. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5655. CCK_DISPLAY_CLOCK_CONTROL);
  5656. }
  5657. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. return 450000;
  5660. }
  5661. static int i945_get_display_clock_speed(struct drm_device *dev)
  5662. {
  5663. return 400000;
  5664. }
  5665. static int i915_get_display_clock_speed(struct drm_device *dev)
  5666. {
  5667. return 333333;
  5668. }
  5669. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5670. {
  5671. return 200000;
  5672. }
  5673. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5674. {
  5675. u16 gcfgc = 0;
  5676. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5677. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5678. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5679. return 266667;
  5680. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5681. return 333333;
  5682. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5683. return 444444;
  5684. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5685. return 200000;
  5686. default:
  5687. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5688. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5689. return 133333;
  5690. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5691. return 166667;
  5692. }
  5693. }
  5694. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5695. {
  5696. u16 gcfgc = 0;
  5697. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5698. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5699. return 133333;
  5700. else {
  5701. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5702. case GC_DISPLAY_CLOCK_333_MHZ:
  5703. return 333333;
  5704. default:
  5705. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5706. return 190000;
  5707. }
  5708. }
  5709. }
  5710. static int i865_get_display_clock_speed(struct drm_device *dev)
  5711. {
  5712. return 266667;
  5713. }
  5714. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5715. {
  5716. u16 hpllcc = 0;
  5717. /*
  5718. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5719. * encoding is different :(
  5720. * FIXME is this the right way to detect 852GM/852GMV?
  5721. */
  5722. if (dev->pdev->revision == 0x1)
  5723. return 133333;
  5724. pci_bus_read_config_word(dev->pdev->bus,
  5725. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5726. /* Assume that the hardware is in the high speed state. This
  5727. * should be the default.
  5728. */
  5729. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5730. case GC_CLOCK_133_200:
  5731. case GC_CLOCK_133_200_2:
  5732. case GC_CLOCK_100_200:
  5733. return 200000;
  5734. case GC_CLOCK_166_250:
  5735. return 250000;
  5736. case GC_CLOCK_100_133:
  5737. return 133333;
  5738. case GC_CLOCK_133_266:
  5739. case GC_CLOCK_133_266_2:
  5740. case GC_CLOCK_166_266:
  5741. return 266667;
  5742. }
  5743. /* Shouldn't happen */
  5744. return 0;
  5745. }
  5746. static int i830_get_display_clock_speed(struct drm_device *dev)
  5747. {
  5748. return 133333;
  5749. }
  5750. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5751. {
  5752. struct drm_i915_private *dev_priv = dev->dev_private;
  5753. static const unsigned int blb_vco[8] = {
  5754. [0] = 3200000,
  5755. [1] = 4000000,
  5756. [2] = 5333333,
  5757. [3] = 4800000,
  5758. [4] = 6400000,
  5759. };
  5760. static const unsigned int pnv_vco[8] = {
  5761. [0] = 3200000,
  5762. [1] = 4000000,
  5763. [2] = 5333333,
  5764. [3] = 4800000,
  5765. [4] = 2666667,
  5766. };
  5767. static const unsigned int cl_vco[8] = {
  5768. [0] = 3200000,
  5769. [1] = 4000000,
  5770. [2] = 5333333,
  5771. [3] = 6400000,
  5772. [4] = 3333333,
  5773. [5] = 3566667,
  5774. [6] = 4266667,
  5775. };
  5776. static const unsigned int elk_vco[8] = {
  5777. [0] = 3200000,
  5778. [1] = 4000000,
  5779. [2] = 5333333,
  5780. [3] = 4800000,
  5781. };
  5782. static const unsigned int ctg_vco[8] = {
  5783. [0] = 3200000,
  5784. [1] = 4000000,
  5785. [2] = 5333333,
  5786. [3] = 6400000,
  5787. [4] = 2666667,
  5788. [5] = 4266667,
  5789. };
  5790. const unsigned int *vco_table;
  5791. unsigned int vco;
  5792. uint8_t tmp = 0;
  5793. /* FIXME other chipsets? */
  5794. if (IS_GM45(dev))
  5795. vco_table = ctg_vco;
  5796. else if (IS_G4X(dev))
  5797. vco_table = elk_vco;
  5798. else if (IS_CRESTLINE(dev))
  5799. vco_table = cl_vco;
  5800. else if (IS_PINEVIEW(dev))
  5801. vco_table = pnv_vco;
  5802. else if (IS_G33(dev))
  5803. vco_table = blb_vco;
  5804. else
  5805. return 0;
  5806. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5807. vco = vco_table[tmp & 0x7];
  5808. if (vco == 0)
  5809. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5810. else
  5811. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5812. return vco;
  5813. }
  5814. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5815. {
  5816. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5817. uint16_t tmp = 0;
  5818. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5819. cdclk_sel = (tmp >> 12) & 0x1;
  5820. switch (vco) {
  5821. case 2666667:
  5822. case 4000000:
  5823. case 5333333:
  5824. return cdclk_sel ? 333333 : 222222;
  5825. case 3200000:
  5826. return cdclk_sel ? 320000 : 228571;
  5827. default:
  5828. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5829. return 222222;
  5830. }
  5831. }
  5832. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5833. {
  5834. static const uint8_t div_3200[] = { 16, 10, 8 };
  5835. static const uint8_t div_4000[] = { 20, 12, 10 };
  5836. static const uint8_t div_5333[] = { 24, 16, 14 };
  5837. const uint8_t *div_table;
  5838. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5839. uint16_t tmp = 0;
  5840. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5841. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5842. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5843. goto fail;
  5844. switch (vco) {
  5845. case 3200000:
  5846. div_table = div_3200;
  5847. break;
  5848. case 4000000:
  5849. div_table = div_4000;
  5850. break;
  5851. case 5333333:
  5852. div_table = div_5333;
  5853. break;
  5854. default:
  5855. goto fail;
  5856. }
  5857. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5858. fail:
  5859. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5860. return 200000;
  5861. }
  5862. static int g33_get_display_clock_speed(struct drm_device *dev)
  5863. {
  5864. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5865. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5866. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5867. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5868. const uint8_t *div_table;
  5869. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5870. uint16_t tmp = 0;
  5871. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5872. cdclk_sel = (tmp >> 4) & 0x7;
  5873. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5874. goto fail;
  5875. switch (vco) {
  5876. case 3200000:
  5877. div_table = div_3200;
  5878. break;
  5879. case 4000000:
  5880. div_table = div_4000;
  5881. break;
  5882. case 4800000:
  5883. div_table = div_4800;
  5884. break;
  5885. case 5333333:
  5886. div_table = div_5333;
  5887. break;
  5888. default:
  5889. goto fail;
  5890. }
  5891. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5892. fail:
  5893. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5894. return 190476;
  5895. }
  5896. static void
  5897. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5898. {
  5899. while (*num > DATA_LINK_M_N_MASK ||
  5900. *den > DATA_LINK_M_N_MASK) {
  5901. *num >>= 1;
  5902. *den >>= 1;
  5903. }
  5904. }
  5905. static void compute_m_n(unsigned int m, unsigned int n,
  5906. uint32_t *ret_m, uint32_t *ret_n)
  5907. {
  5908. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5909. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5910. intel_reduce_m_n_ratio(ret_m, ret_n);
  5911. }
  5912. void
  5913. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5914. int pixel_clock, int link_clock,
  5915. struct intel_link_m_n *m_n)
  5916. {
  5917. m_n->tu = 64;
  5918. compute_m_n(bits_per_pixel * pixel_clock,
  5919. link_clock * nlanes * 8,
  5920. &m_n->gmch_m, &m_n->gmch_n);
  5921. compute_m_n(pixel_clock, link_clock,
  5922. &m_n->link_m, &m_n->link_n);
  5923. }
  5924. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5925. {
  5926. if (i915.panel_use_ssc >= 0)
  5927. return i915.panel_use_ssc != 0;
  5928. return dev_priv->vbt.lvds_use_ssc
  5929. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5930. }
  5931. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5932. {
  5933. return (1 << dpll->n) << 16 | dpll->m2;
  5934. }
  5935. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5936. {
  5937. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5938. }
  5939. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5940. struct intel_crtc_state *crtc_state,
  5941. intel_clock_t *reduced_clock)
  5942. {
  5943. struct drm_device *dev = crtc->base.dev;
  5944. u32 fp, fp2 = 0;
  5945. if (IS_PINEVIEW(dev)) {
  5946. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5947. if (reduced_clock)
  5948. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5949. } else {
  5950. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5951. if (reduced_clock)
  5952. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5953. }
  5954. crtc_state->dpll_hw_state.fp0 = fp;
  5955. crtc->lowfreq_avail = false;
  5956. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5957. reduced_clock) {
  5958. crtc_state->dpll_hw_state.fp1 = fp2;
  5959. crtc->lowfreq_avail = true;
  5960. } else {
  5961. crtc_state->dpll_hw_state.fp1 = fp;
  5962. }
  5963. }
  5964. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5965. pipe)
  5966. {
  5967. u32 reg_val;
  5968. /*
  5969. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5970. * and set it to a reasonable value instead.
  5971. */
  5972. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5973. reg_val &= 0xffffff00;
  5974. reg_val |= 0x00000030;
  5975. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5976. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5977. reg_val &= 0x8cffffff;
  5978. reg_val = 0x8c000000;
  5979. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5980. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5981. reg_val &= 0xffffff00;
  5982. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5983. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5984. reg_val &= 0x00ffffff;
  5985. reg_val |= 0xb0000000;
  5986. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5987. }
  5988. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5989. struct intel_link_m_n *m_n)
  5990. {
  5991. struct drm_device *dev = crtc->base.dev;
  5992. struct drm_i915_private *dev_priv = dev->dev_private;
  5993. int pipe = crtc->pipe;
  5994. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5995. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5996. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5997. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5998. }
  5999. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6000. struct intel_link_m_n *m_n,
  6001. struct intel_link_m_n *m2_n2)
  6002. {
  6003. struct drm_device *dev = crtc->base.dev;
  6004. struct drm_i915_private *dev_priv = dev->dev_private;
  6005. int pipe = crtc->pipe;
  6006. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6007. if (INTEL_INFO(dev)->gen >= 5) {
  6008. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6009. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6010. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6011. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6012. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6013. * for gen < 8) and if DRRS is supported (to make sure the
  6014. * registers are not unnecessarily accessed).
  6015. */
  6016. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6017. crtc->config->has_drrs) {
  6018. I915_WRITE(PIPE_DATA_M2(transcoder),
  6019. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6020. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6021. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6022. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6023. }
  6024. } else {
  6025. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6026. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6027. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6028. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6029. }
  6030. }
  6031. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6032. {
  6033. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6034. if (m_n == M1_N1) {
  6035. dp_m_n = &crtc->config->dp_m_n;
  6036. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6037. } else if (m_n == M2_N2) {
  6038. /*
  6039. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6040. * needs to be programmed into M1_N1.
  6041. */
  6042. dp_m_n = &crtc->config->dp_m2_n2;
  6043. } else {
  6044. DRM_ERROR("Unsupported divider value\n");
  6045. return;
  6046. }
  6047. if (crtc->config->has_pch_encoder)
  6048. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6049. else
  6050. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6051. }
  6052. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6053. struct intel_crtc_state *pipe_config)
  6054. {
  6055. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6056. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6057. if (crtc->pipe != PIPE_A)
  6058. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6059. /* DPLL not used with DSI, but still need the rest set up */
  6060. if (!pipe_config->has_dsi_encoder)
  6061. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6062. DPLL_EXT_BUFFER_ENABLE_VLV;
  6063. pipe_config->dpll_hw_state.dpll_md =
  6064. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6065. }
  6066. static void chv_compute_dpll(struct intel_crtc *crtc,
  6067. struct intel_crtc_state *pipe_config)
  6068. {
  6069. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6070. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6071. if (crtc->pipe != PIPE_A)
  6072. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6073. /* DPLL not used with DSI, but still need the rest set up */
  6074. if (!pipe_config->has_dsi_encoder)
  6075. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6076. pipe_config->dpll_hw_state.dpll_md =
  6077. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6078. }
  6079. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6080. const struct intel_crtc_state *pipe_config)
  6081. {
  6082. struct drm_device *dev = crtc->base.dev;
  6083. struct drm_i915_private *dev_priv = dev->dev_private;
  6084. enum pipe pipe = crtc->pipe;
  6085. u32 mdiv;
  6086. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6087. u32 coreclk, reg_val;
  6088. /* Enable Refclk */
  6089. I915_WRITE(DPLL(pipe),
  6090. pipe_config->dpll_hw_state.dpll &
  6091. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6092. /* No need to actually set up the DPLL with DSI */
  6093. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6094. return;
  6095. mutex_lock(&dev_priv->sb_lock);
  6096. bestn = pipe_config->dpll.n;
  6097. bestm1 = pipe_config->dpll.m1;
  6098. bestm2 = pipe_config->dpll.m2;
  6099. bestp1 = pipe_config->dpll.p1;
  6100. bestp2 = pipe_config->dpll.p2;
  6101. /* See eDP HDMI DPIO driver vbios notes doc */
  6102. /* PLL B needs special handling */
  6103. if (pipe == PIPE_B)
  6104. vlv_pllb_recal_opamp(dev_priv, pipe);
  6105. /* Set up Tx target for periodic Rcomp update */
  6106. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6107. /* Disable target IRef on PLL */
  6108. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6109. reg_val &= 0x00ffffff;
  6110. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6111. /* Disable fast lock */
  6112. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6113. /* Set idtafcrecal before PLL is enabled */
  6114. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6115. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6116. mdiv |= ((bestn << DPIO_N_SHIFT));
  6117. mdiv |= (1 << DPIO_K_SHIFT);
  6118. /*
  6119. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6120. * but we don't support that).
  6121. * Note: don't use the DAC post divider as it seems unstable.
  6122. */
  6123. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6124. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6125. mdiv |= DPIO_ENABLE_CALIBRATION;
  6126. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6127. /* Set HBR and RBR LPF coefficients */
  6128. if (pipe_config->port_clock == 162000 ||
  6129. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6130. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6131. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6132. 0x009f0003);
  6133. else
  6134. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6135. 0x00d0000f);
  6136. if (pipe_config->has_dp_encoder) {
  6137. /* Use SSC source */
  6138. if (pipe == PIPE_A)
  6139. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6140. 0x0df40000);
  6141. else
  6142. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6143. 0x0df70000);
  6144. } else { /* HDMI or VGA */
  6145. /* Use bend source */
  6146. if (pipe == PIPE_A)
  6147. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6148. 0x0df70000);
  6149. else
  6150. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6151. 0x0df40000);
  6152. }
  6153. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6154. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6155. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6156. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6157. coreclk |= 0x01000000;
  6158. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6159. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6160. mutex_unlock(&dev_priv->sb_lock);
  6161. }
  6162. static void chv_prepare_pll(struct intel_crtc *crtc,
  6163. const struct intel_crtc_state *pipe_config)
  6164. {
  6165. struct drm_device *dev = crtc->base.dev;
  6166. struct drm_i915_private *dev_priv = dev->dev_private;
  6167. enum pipe pipe = crtc->pipe;
  6168. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6169. u32 loopfilter, tribuf_calcntr;
  6170. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6171. u32 dpio_val;
  6172. int vco;
  6173. /* Enable Refclk and SSC */
  6174. I915_WRITE(DPLL(pipe),
  6175. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6176. /* No need to actually set up the DPLL with DSI */
  6177. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6178. return;
  6179. bestn = pipe_config->dpll.n;
  6180. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6181. bestm1 = pipe_config->dpll.m1;
  6182. bestm2 = pipe_config->dpll.m2 >> 22;
  6183. bestp1 = pipe_config->dpll.p1;
  6184. bestp2 = pipe_config->dpll.p2;
  6185. vco = pipe_config->dpll.vco;
  6186. dpio_val = 0;
  6187. loopfilter = 0;
  6188. mutex_lock(&dev_priv->sb_lock);
  6189. /* p1 and p2 divider */
  6190. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6191. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6192. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6193. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6194. 1 << DPIO_CHV_K_DIV_SHIFT);
  6195. /* Feedback post-divider - m2 */
  6196. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6197. /* Feedback refclk divider - n and m1 */
  6198. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6199. DPIO_CHV_M1_DIV_BY_2 |
  6200. 1 << DPIO_CHV_N_DIV_SHIFT);
  6201. /* M2 fraction division */
  6202. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6203. /* M2 fraction division enable */
  6204. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6205. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6206. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6207. if (bestm2_frac)
  6208. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6209. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6210. /* Program digital lock detect threshold */
  6211. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6212. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6213. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6214. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6215. if (!bestm2_frac)
  6216. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6217. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6218. /* Loop filter */
  6219. if (vco == 5400000) {
  6220. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6221. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6222. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6223. tribuf_calcntr = 0x9;
  6224. } else if (vco <= 6200000) {
  6225. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6226. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6227. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6228. tribuf_calcntr = 0x9;
  6229. } else if (vco <= 6480000) {
  6230. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6231. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6232. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6233. tribuf_calcntr = 0x8;
  6234. } else {
  6235. /* Not supported. Apply the same limits as in the max case */
  6236. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6237. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6238. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6239. tribuf_calcntr = 0;
  6240. }
  6241. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6242. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6243. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6244. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6246. /* AFC Recal */
  6247. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6248. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6249. DPIO_AFC_RECAL);
  6250. mutex_unlock(&dev_priv->sb_lock);
  6251. }
  6252. /**
  6253. * vlv_force_pll_on - forcibly enable just the PLL
  6254. * @dev_priv: i915 private structure
  6255. * @pipe: pipe PLL to enable
  6256. * @dpll: PLL configuration
  6257. *
  6258. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6259. * in cases where we need the PLL enabled even when @pipe is not going to
  6260. * be enabled.
  6261. */
  6262. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6263. const struct dpll *dpll)
  6264. {
  6265. struct intel_crtc *crtc =
  6266. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6267. struct intel_crtc_state *pipe_config;
  6268. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6269. if (!pipe_config)
  6270. return -ENOMEM;
  6271. pipe_config->base.crtc = &crtc->base;
  6272. pipe_config->pixel_multiplier = 1;
  6273. pipe_config->dpll = *dpll;
  6274. if (IS_CHERRYVIEW(dev)) {
  6275. chv_compute_dpll(crtc, pipe_config);
  6276. chv_prepare_pll(crtc, pipe_config);
  6277. chv_enable_pll(crtc, pipe_config);
  6278. } else {
  6279. vlv_compute_dpll(crtc, pipe_config);
  6280. vlv_prepare_pll(crtc, pipe_config);
  6281. vlv_enable_pll(crtc, pipe_config);
  6282. }
  6283. kfree(pipe_config);
  6284. return 0;
  6285. }
  6286. /**
  6287. * vlv_force_pll_off - forcibly disable just the PLL
  6288. * @dev_priv: i915 private structure
  6289. * @pipe: pipe PLL to disable
  6290. *
  6291. * Disable the PLL for @pipe. To be used in cases where we need
  6292. * the PLL enabled even when @pipe is not going to be enabled.
  6293. */
  6294. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6295. {
  6296. if (IS_CHERRYVIEW(dev))
  6297. chv_disable_pll(to_i915(dev), pipe);
  6298. else
  6299. vlv_disable_pll(to_i915(dev), pipe);
  6300. }
  6301. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6302. struct intel_crtc_state *crtc_state,
  6303. intel_clock_t *reduced_clock)
  6304. {
  6305. struct drm_device *dev = crtc->base.dev;
  6306. struct drm_i915_private *dev_priv = dev->dev_private;
  6307. u32 dpll;
  6308. bool is_sdvo;
  6309. struct dpll *clock = &crtc_state->dpll;
  6310. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6311. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6312. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6313. dpll = DPLL_VGA_MODE_DIS;
  6314. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6315. dpll |= DPLLB_MODE_LVDS;
  6316. else
  6317. dpll |= DPLLB_MODE_DAC_SERIAL;
  6318. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6319. dpll |= (crtc_state->pixel_multiplier - 1)
  6320. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6321. }
  6322. if (is_sdvo)
  6323. dpll |= DPLL_SDVO_HIGH_SPEED;
  6324. if (crtc_state->has_dp_encoder)
  6325. dpll |= DPLL_SDVO_HIGH_SPEED;
  6326. /* compute bitmask from p1 value */
  6327. if (IS_PINEVIEW(dev))
  6328. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6329. else {
  6330. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6331. if (IS_G4X(dev) && reduced_clock)
  6332. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6333. }
  6334. switch (clock->p2) {
  6335. case 5:
  6336. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6337. break;
  6338. case 7:
  6339. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6340. break;
  6341. case 10:
  6342. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6343. break;
  6344. case 14:
  6345. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6346. break;
  6347. }
  6348. if (INTEL_INFO(dev)->gen >= 4)
  6349. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6350. if (crtc_state->sdvo_tv_clock)
  6351. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6352. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6353. intel_panel_use_ssc(dev_priv))
  6354. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6355. else
  6356. dpll |= PLL_REF_INPUT_DREFCLK;
  6357. dpll |= DPLL_VCO_ENABLE;
  6358. crtc_state->dpll_hw_state.dpll = dpll;
  6359. if (INTEL_INFO(dev)->gen >= 4) {
  6360. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6361. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6362. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6363. }
  6364. }
  6365. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6366. struct intel_crtc_state *crtc_state,
  6367. intel_clock_t *reduced_clock)
  6368. {
  6369. struct drm_device *dev = crtc->base.dev;
  6370. struct drm_i915_private *dev_priv = dev->dev_private;
  6371. u32 dpll;
  6372. struct dpll *clock = &crtc_state->dpll;
  6373. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6374. dpll = DPLL_VGA_MODE_DIS;
  6375. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6376. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6377. } else {
  6378. if (clock->p1 == 2)
  6379. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6380. else
  6381. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6382. if (clock->p2 == 4)
  6383. dpll |= PLL_P2_DIVIDE_BY_4;
  6384. }
  6385. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6386. dpll |= DPLL_DVO_2X_MODE;
  6387. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6388. intel_panel_use_ssc(dev_priv))
  6389. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6390. else
  6391. dpll |= PLL_REF_INPUT_DREFCLK;
  6392. dpll |= DPLL_VCO_ENABLE;
  6393. crtc_state->dpll_hw_state.dpll = dpll;
  6394. }
  6395. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6396. {
  6397. struct drm_device *dev = intel_crtc->base.dev;
  6398. struct drm_i915_private *dev_priv = dev->dev_private;
  6399. enum pipe pipe = intel_crtc->pipe;
  6400. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6401. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6402. uint32_t crtc_vtotal, crtc_vblank_end;
  6403. int vsyncshift = 0;
  6404. /* We need to be careful not to changed the adjusted mode, for otherwise
  6405. * the hw state checker will get angry at the mismatch. */
  6406. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6407. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6408. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6409. /* the chip adds 2 halflines automatically */
  6410. crtc_vtotal -= 1;
  6411. crtc_vblank_end -= 1;
  6412. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6413. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6414. else
  6415. vsyncshift = adjusted_mode->crtc_hsync_start -
  6416. adjusted_mode->crtc_htotal / 2;
  6417. if (vsyncshift < 0)
  6418. vsyncshift += adjusted_mode->crtc_htotal;
  6419. }
  6420. if (INTEL_INFO(dev)->gen > 3)
  6421. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6422. I915_WRITE(HTOTAL(cpu_transcoder),
  6423. (adjusted_mode->crtc_hdisplay - 1) |
  6424. ((adjusted_mode->crtc_htotal - 1) << 16));
  6425. I915_WRITE(HBLANK(cpu_transcoder),
  6426. (adjusted_mode->crtc_hblank_start - 1) |
  6427. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6428. I915_WRITE(HSYNC(cpu_transcoder),
  6429. (adjusted_mode->crtc_hsync_start - 1) |
  6430. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6431. I915_WRITE(VTOTAL(cpu_transcoder),
  6432. (adjusted_mode->crtc_vdisplay - 1) |
  6433. ((crtc_vtotal - 1) << 16));
  6434. I915_WRITE(VBLANK(cpu_transcoder),
  6435. (adjusted_mode->crtc_vblank_start - 1) |
  6436. ((crtc_vblank_end - 1) << 16));
  6437. I915_WRITE(VSYNC(cpu_transcoder),
  6438. (adjusted_mode->crtc_vsync_start - 1) |
  6439. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6440. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6441. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6442. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6443. * bits. */
  6444. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6445. (pipe == PIPE_B || pipe == PIPE_C))
  6446. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6447. }
  6448. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6449. {
  6450. struct drm_device *dev = intel_crtc->base.dev;
  6451. struct drm_i915_private *dev_priv = dev->dev_private;
  6452. enum pipe pipe = intel_crtc->pipe;
  6453. /* pipesrc controls the size that is scaled from, which should
  6454. * always be the user's requested size.
  6455. */
  6456. I915_WRITE(PIPESRC(pipe),
  6457. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6458. (intel_crtc->config->pipe_src_h - 1));
  6459. }
  6460. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6461. struct intel_crtc_state *pipe_config)
  6462. {
  6463. struct drm_device *dev = crtc->base.dev;
  6464. struct drm_i915_private *dev_priv = dev->dev_private;
  6465. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6466. uint32_t tmp;
  6467. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6468. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6469. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6470. tmp = I915_READ(HBLANK(cpu_transcoder));
  6471. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6472. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6473. tmp = I915_READ(HSYNC(cpu_transcoder));
  6474. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6475. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6476. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6477. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6478. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6479. tmp = I915_READ(VBLANK(cpu_transcoder));
  6480. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6481. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6482. tmp = I915_READ(VSYNC(cpu_transcoder));
  6483. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6484. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6485. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6486. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6487. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6488. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6489. }
  6490. }
  6491. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6492. struct intel_crtc_state *pipe_config)
  6493. {
  6494. struct drm_device *dev = crtc->base.dev;
  6495. struct drm_i915_private *dev_priv = dev->dev_private;
  6496. u32 tmp;
  6497. tmp = I915_READ(PIPESRC(crtc->pipe));
  6498. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6499. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6500. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6501. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6502. }
  6503. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6504. struct intel_crtc_state *pipe_config)
  6505. {
  6506. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6507. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6508. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6509. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6510. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6511. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6512. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6513. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6514. mode->flags = pipe_config->base.adjusted_mode.flags;
  6515. mode->type = DRM_MODE_TYPE_DRIVER;
  6516. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6517. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6518. mode->hsync = drm_mode_hsync(mode);
  6519. mode->vrefresh = drm_mode_vrefresh(mode);
  6520. drm_mode_set_name(mode);
  6521. }
  6522. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6523. {
  6524. struct drm_device *dev = intel_crtc->base.dev;
  6525. struct drm_i915_private *dev_priv = dev->dev_private;
  6526. uint32_t pipeconf;
  6527. pipeconf = 0;
  6528. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6529. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6530. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6531. if (intel_crtc->config->double_wide)
  6532. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6533. /* only g4x and later have fancy bpc/dither controls */
  6534. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6535. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6536. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6537. pipeconf |= PIPECONF_DITHER_EN |
  6538. PIPECONF_DITHER_TYPE_SP;
  6539. switch (intel_crtc->config->pipe_bpp) {
  6540. case 18:
  6541. pipeconf |= PIPECONF_6BPC;
  6542. break;
  6543. case 24:
  6544. pipeconf |= PIPECONF_8BPC;
  6545. break;
  6546. case 30:
  6547. pipeconf |= PIPECONF_10BPC;
  6548. break;
  6549. default:
  6550. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6551. BUG();
  6552. }
  6553. }
  6554. if (HAS_PIPE_CXSR(dev)) {
  6555. if (intel_crtc->lowfreq_avail) {
  6556. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6557. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6558. } else {
  6559. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6560. }
  6561. }
  6562. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6563. if (INTEL_INFO(dev)->gen < 4 ||
  6564. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6565. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6566. else
  6567. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6568. } else
  6569. pipeconf |= PIPECONF_PROGRESSIVE;
  6570. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6571. intel_crtc->config->limited_color_range)
  6572. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6573. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6574. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6575. }
  6576. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6577. struct intel_crtc_state *crtc_state)
  6578. {
  6579. struct drm_device *dev = crtc->base.dev;
  6580. struct drm_i915_private *dev_priv = dev->dev_private;
  6581. const intel_limit_t *limit;
  6582. int refclk = 48000;
  6583. memset(&crtc_state->dpll_hw_state, 0,
  6584. sizeof(crtc_state->dpll_hw_state));
  6585. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6586. if (intel_panel_use_ssc(dev_priv)) {
  6587. refclk = dev_priv->vbt.lvds_ssc_freq;
  6588. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6589. }
  6590. limit = &intel_limits_i8xx_lvds;
  6591. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6592. limit = &intel_limits_i8xx_dvo;
  6593. } else {
  6594. limit = &intel_limits_i8xx_dac;
  6595. }
  6596. if (!crtc_state->clock_set &&
  6597. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6598. refclk, NULL, &crtc_state->dpll)) {
  6599. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6600. return -EINVAL;
  6601. }
  6602. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6603. return 0;
  6604. }
  6605. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6606. struct intel_crtc_state *crtc_state)
  6607. {
  6608. struct drm_device *dev = crtc->base.dev;
  6609. struct drm_i915_private *dev_priv = dev->dev_private;
  6610. const intel_limit_t *limit;
  6611. int refclk = 96000;
  6612. memset(&crtc_state->dpll_hw_state, 0,
  6613. sizeof(crtc_state->dpll_hw_state));
  6614. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6615. if (intel_panel_use_ssc(dev_priv)) {
  6616. refclk = dev_priv->vbt.lvds_ssc_freq;
  6617. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6618. }
  6619. if (intel_is_dual_link_lvds(dev))
  6620. limit = &intel_limits_g4x_dual_channel_lvds;
  6621. else
  6622. limit = &intel_limits_g4x_single_channel_lvds;
  6623. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6624. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6625. limit = &intel_limits_g4x_hdmi;
  6626. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6627. limit = &intel_limits_g4x_sdvo;
  6628. } else {
  6629. /* The option is for other outputs */
  6630. limit = &intel_limits_i9xx_sdvo;
  6631. }
  6632. if (!crtc_state->clock_set &&
  6633. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6634. refclk, NULL, &crtc_state->dpll)) {
  6635. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6636. return -EINVAL;
  6637. }
  6638. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6639. return 0;
  6640. }
  6641. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6642. struct intel_crtc_state *crtc_state)
  6643. {
  6644. struct drm_device *dev = crtc->base.dev;
  6645. struct drm_i915_private *dev_priv = dev->dev_private;
  6646. const intel_limit_t *limit;
  6647. int refclk = 96000;
  6648. memset(&crtc_state->dpll_hw_state, 0,
  6649. sizeof(crtc_state->dpll_hw_state));
  6650. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6651. if (intel_panel_use_ssc(dev_priv)) {
  6652. refclk = dev_priv->vbt.lvds_ssc_freq;
  6653. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6654. }
  6655. limit = &intel_limits_pineview_lvds;
  6656. } else {
  6657. limit = &intel_limits_pineview_sdvo;
  6658. }
  6659. if (!crtc_state->clock_set &&
  6660. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6661. refclk, NULL, &crtc_state->dpll)) {
  6662. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6663. return -EINVAL;
  6664. }
  6665. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6666. return 0;
  6667. }
  6668. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6669. struct intel_crtc_state *crtc_state)
  6670. {
  6671. struct drm_device *dev = crtc->base.dev;
  6672. struct drm_i915_private *dev_priv = dev->dev_private;
  6673. const intel_limit_t *limit;
  6674. int refclk = 96000;
  6675. memset(&crtc_state->dpll_hw_state, 0,
  6676. sizeof(crtc_state->dpll_hw_state));
  6677. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6678. if (intel_panel_use_ssc(dev_priv)) {
  6679. refclk = dev_priv->vbt.lvds_ssc_freq;
  6680. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6681. }
  6682. limit = &intel_limits_i9xx_lvds;
  6683. } else {
  6684. limit = &intel_limits_i9xx_sdvo;
  6685. }
  6686. if (!crtc_state->clock_set &&
  6687. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6688. refclk, NULL, &crtc_state->dpll)) {
  6689. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6690. return -EINVAL;
  6691. }
  6692. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6693. return 0;
  6694. }
  6695. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6696. struct intel_crtc_state *crtc_state)
  6697. {
  6698. int refclk = 100000;
  6699. const intel_limit_t *limit = &intel_limits_chv;
  6700. memset(&crtc_state->dpll_hw_state, 0,
  6701. sizeof(crtc_state->dpll_hw_state));
  6702. if (!crtc_state->clock_set &&
  6703. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6704. refclk, NULL, &crtc_state->dpll)) {
  6705. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6706. return -EINVAL;
  6707. }
  6708. chv_compute_dpll(crtc, crtc_state);
  6709. return 0;
  6710. }
  6711. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6712. struct intel_crtc_state *crtc_state)
  6713. {
  6714. int refclk = 100000;
  6715. const intel_limit_t *limit = &intel_limits_vlv;
  6716. memset(&crtc_state->dpll_hw_state, 0,
  6717. sizeof(crtc_state->dpll_hw_state));
  6718. if (!crtc_state->clock_set &&
  6719. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6720. refclk, NULL, &crtc_state->dpll)) {
  6721. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6722. return -EINVAL;
  6723. }
  6724. vlv_compute_dpll(crtc, crtc_state);
  6725. return 0;
  6726. }
  6727. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6728. struct intel_crtc_state *pipe_config)
  6729. {
  6730. struct drm_device *dev = crtc->base.dev;
  6731. struct drm_i915_private *dev_priv = dev->dev_private;
  6732. uint32_t tmp;
  6733. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6734. return;
  6735. tmp = I915_READ(PFIT_CONTROL);
  6736. if (!(tmp & PFIT_ENABLE))
  6737. return;
  6738. /* Check whether the pfit is attached to our pipe. */
  6739. if (INTEL_INFO(dev)->gen < 4) {
  6740. if (crtc->pipe != PIPE_B)
  6741. return;
  6742. } else {
  6743. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6744. return;
  6745. }
  6746. pipe_config->gmch_pfit.control = tmp;
  6747. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6748. }
  6749. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6750. struct intel_crtc_state *pipe_config)
  6751. {
  6752. struct drm_device *dev = crtc->base.dev;
  6753. struct drm_i915_private *dev_priv = dev->dev_private;
  6754. int pipe = pipe_config->cpu_transcoder;
  6755. intel_clock_t clock;
  6756. u32 mdiv;
  6757. int refclk = 100000;
  6758. /* In case of DSI, DPLL will not be used */
  6759. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6760. return;
  6761. mutex_lock(&dev_priv->sb_lock);
  6762. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6763. mutex_unlock(&dev_priv->sb_lock);
  6764. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6765. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6766. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6767. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6768. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6769. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6770. }
  6771. static void
  6772. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6773. struct intel_initial_plane_config *plane_config)
  6774. {
  6775. struct drm_device *dev = crtc->base.dev;
  6776. struct drm_i915_private *dev_priv = dev->dev_private;
  6777. u32 val, base, offset;
  6778. int pipe = crtc->pipe, plane = crtc->plane;
  6779. int fourcc, pixel_format;
  6780. unsigned int aligned_height;
  6781. struct drm_framebuffer *fb;
  6782. struct intel_framebuffer *intel_fb;
  6783. val = I915_READ(DSPCNTR(plane));
  6784. if (!(val & DISPLAY_PLANE_ENABLE))
  6785. return;
  6786. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6787. if (!intel_fb) {
  6788. DRM_DEBUG_KMS("failed to alloc fb\n");
  6789. return;
  6790. }
  6791. fb = &intel_fb->base;
  6792. if (INTEL_INFO(dev)->gen >= 4) {
  6793. if (val & DISPPLANE_TILED) {
  6794. plane_config->tiling = I915_TILING_X;
  6795. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6796. }
  6797. }
  6798. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6799. fourcc = i9xx_format_to_fourcc(pixel_format);
  6800. fb->pixel_format = fourcc;
  6801. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6802. if (INTEL_INFO(dev)->gen >= 4) {
  6803. if (plane_config->tiling)
  6804. offset = I915_READ(DSPTILEOFF(plane));
  6805. else
  6806. offset = I915_READ(DSPLINOFF(plane));
  6807. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6808. } else {
  6809. base = I915_READ(DSPADDR(plane));
  6810. }
  6811. plane_config->base = base;
  6812. val = I915_READ(PIPESRC(pipe));
  6813. fb->width = ((val >> 16) & 0xfff) + 1;
  6814. fb->height = ((val >> 0) & 0xfff) + 1;
  6815. val = I915_READ(DSPSTRIDE(pipe));
  6816. fb->pitches[0] = val & 0xffffffc0;
  6817. aligned_height = intel_fb_align_height(dev, fb->height,
  6818. fb->pixel_format,
  6819. fb->modifier[0]);
  6820. plane_config->size = fb->pitches[0] * aligned_height;
  6821. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6822. pipe_name(pipe), plane, fb->width, fb->height,
  6823. fb->bits_per_pixel, base, fb->pitches[0],
  6824. plane_config->size);
  6825. plane_config->fb = intel_fb;
  6826. }
  6827. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6828. struct intel_crtc_state *pipe_config)
  6829. {
  6830. struct drm_device *dev = crtc->base.dev;
  6831. struct drm_i915_private *dev_priv = dev->dev_private;
  6832. int pipe = pipe_config->cpu_transcoder;
  6833. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6834. intel_clock_t clock;
  6835. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6836. int refclk = 100000;
  6837. /* In case of DSI, DPLL will not be used */
  6838. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6839. return;
  6840. mutex_lock(&dev_priv->sb_lock);
  6841. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6842. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6843. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6844. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6845. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6846. mutex_unlock(&dev_priv->sb_lock);
  6847. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6848. clock.m2 = (pll_dw0 & 0xff) << 22;
  6849. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6850. clock.m2 |= pll_dw2 & 0x3fffff;
  6851. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6852. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6853. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6854. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6855. }
  6856. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6857. struct intel_crtc_state *pipe_config)
  6858. {
  6859. struct drm_device *dev = crtc->base.dev;
  6860. struct drm_i915_private *dev_priv = dev->dev_private;
  6861. enum intel_display_power_domain power_domain;
  6862. uint32_t tmp;
  6863. bool ret;
  6864. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6865. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6866. return false;
  6867. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6868. pipe_config->shared_dpll = NULL;
  6869. ret = false;
  6870. tmp = I915_READ(PIPECONF(crtc->pipe));
  6871. if (!(tmp & PIPECONF_ENABLE))
  6872. goto out;
  6873. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6874. switch (tmp & PIPECONF_BPC_MASK) {
  6875. case PIPECONF_6BPC:
  6876. pipe_config->pipe_bpp = 18;
  6877. break;
  6878. case PIPECONF_8BPC:
  6879. pipe_config->pipe_bpp = 24;
  6880. break;
  6881. case PIPECONF_10BPC:
  6882. pipe_config->pipe_bpp = 30;
  6883. break;
  6884. default:
  6885. break;
  6886. }
  6887. }
  6888. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6889. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6890. pipe_config->limited_color_range = true;
  6891. if (INTEL_INFO(dev)->gen < 4)
  6892. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6893. intel_get_pipe_timings(crtc, pipe_config);
  6894. intel_get_pipe_src_size(crtc, pipe_config);
  6895. i9xx_get_pfit_config(crtc, pipe_config);
  6896. if (INTEL_INFO(dev)->gen >= 4) {
  6897. /* No way to read it out on pipes B and C */
  6898. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6899. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6900. else
  6901. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6902. pipe_config->pixel_multiplier =
  6903. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6904. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6905. pipe_config->dpll_hw_state.dpll_md = tmp;
  6906. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6907. tmp = I915_READ(DPLL(crtc->pipe));
  6908. pipe_config->pixel_multiplier =
  6909. ((tmp & SDVO_MULTIPLIER_MASK)
  6910. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6911. } else {
  6912. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6913. * port and will be fixed up in the encoder->get_config
  6914. * function. */
  6915. pipe_config->pixel_multiplier = 1;
  6916. }
  6917. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6918. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6919. /*
  6920. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6921. * on 830. Filter it out here so that we don't
  6922. * report errors due to that.
  6923. */
  6924. if (IS_I830(dev))
  6925. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6926. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6927. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6928. } else {
  6929. /* Mask out read-only status bits. */
  6930. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6931. DPLL_PORTC_READY_MASK |
  6932. DPLL_PORTB_READY_MASK);
  6933. }
  6934. if (IS_CHERRYVIEW(dev))
  6935. chv_crtc_clock_get(crtc, pipe_config);
  6936. else if (IS_VALLEYVIEW(dev))
  6937. vlv_crtc_clock_get(crtc, pipe_config);
  6938. else
  6939. i9xx_crtc_clock_get(crtc, pipe_config);
  6940. /*
  6941. * Normally the dotclock is filled in by the encoder .get_config()
  6942. * but in case the pipe is enabled w/o any ports we need a sane
  6943. * default.
  6944. */
  6945. pipe_config->base.adjusted_mode.crtc_clock =
  6946. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6947. ret = true;
  6948. out:
  6949. intel_display_power_put(dev_priv, power_domain);
  6950. return ret;
  6951. }
  6952. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6953. {
  6954. struct drm_i915_private *dev_priv = dev->dev_private;
  6955. struct intel_encoder *encoder;
  6956. u32 val, final;
  6957. bool has_lvds = false;
  6958. bool has_cpu_edp = false;
  6959. bool has_panel = false;
  6960. bool has_ck505 = false;
  6961. bool can_ssc = false;
  6962. /* We need to take the global config into account */
  6963. for_each_intel_encoder(dev, encoder) {
  6964. switch (encoder->type) {
  6965. case INTEL_OUTPUT_LVDS:
  6966. has_panel = true;
  6967. has_lvds = true;
  6968. break;
  6969. case INTEL_OUTPUT_EDP:
  6970. has_panel = true;
  6971. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6972. has_cpu_edp = true;
  6973. break;
  6974. default:
  6975. break;
  6976. }
  6977. }
  6978. if (HAS_PCH_IBX(dev)) {
  6979. has_ck505 = dev_priv->vbt.display_clock_mode;
  6980. can_ssc = has_ck505;
  6981. } else {
  6982. has_ck505 = false;
  6983. can_ssc = true;
  6984. }
  6985. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6986. has_panel, has_lvds, has_ck505);
  6987. /* Ironlake: try to setup display ref clock before DPLL
  6988. * enabling. This is only under driver's control after
  6989. * PCH B stepping, previous chipset stepping should be
  6990. * ignoring this setting.
  6991. */
  6992. val = I915_READ(PCH_DREF_CONTROL);
  6993. /* As we must carefully and slowly disable/enable each source in turn,
  6994. * compute the final state we want first and check if we need to
  6995. * make any changes at all.
  6996. */
  6997. final = val;
  6998. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6999. if (has_ck505)
  7000. final |= DREF_NONSPREAD_CK505_ENABLE;
  7001. else
  7002. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7003. final &= ~DREF_SSC_SOURCE_MASK;
  7004. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7005. final &= ~DREF_SSC1_ENABLE;
  7006. if (has_panel) {
  7007. final |= DREF_SSC_SOURCE_ENABLE;
  7008. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7009. final |= DREF_SSC1_ENABLE;
  7010. if (has_cpu_edp) {
  7011. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7012. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7013. else
  7014. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7015. } else
  7016. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7017. } else {
  7018. final |= DREF_SSC_SOURCE_DISABLE;
  7019. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7020. }
  7021. if (final == val)
  7022. return;
  7023. /* Always enable nonspread source */
  7024. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7025. if (has_ck505)
  7026. val |= DREF_NONSPREAD_CK505_ENABLE;
  7027. else
  7028. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7029. if (has_panel) {
  7030. val &= ~DREF_SSC_SOURCE_MASK;
  7031. val |= DREF_SSC_SOURCE_ENABLE;
  7032. /* SSC must be turned on before enabling the CPU output */
  7033. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7034. DRM_DEBUG_KMS("Using SSC on panel\n");
  7035. val |= DREF_SSC1_ENABLE;
  7036. } else
  7037. val &= ~DREF_SSC1_ENABLE;
  7038. /* Get SSC going before enabling the outputs */
  7039. I915_WRITE(PCH_DREF_CONTROL, val);
  7040. POSTING_READ(PCH_DREF_CONTROL);
  7041. udelay(200);
  7042. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7043. /* Enable CPU source on CPU attached eDP */
  7044. if (has_cpu_edp) {
  7045. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7046. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7047. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7048. } else
  7049. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7050. } else
  7051. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7052. I915_WRITE(PCH_DREF_CONTROL, val);
  7053. POSTING_READ(PCH_DREF_CONTROL);
  7054. udelay(200);
  7055. } else {
  7056. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7057. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7058. /* Turn off CPU output */
  7059. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7060. I915_WRITE(PCH_DREF_CONTROL, val);
  7061. POSTING_READ(PCH_DREF_CONTROL);
  7062. udelay(200);
  7063. /* Turn off the SSC source */
  7064. val &= ~DREF_SSC_SOURCE_MASK;
  7065. val |= DREF_SSC_SOURCE_DISABLE;
  7066. /* Turn off SSC1 */
  7067. val &= ~DREF_SSC1_ENABLE;
  7068. I915_WRITE(PCH_DREF_CONTROL, val);
  7069. POSTING_READ(PCH_DREF_CONTROL);
  7070. udelay(200);
  7071. }
  7072. BUG_ON(val != final);
  7073. }
  7074. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7075. {
  7076. uint32_t tmp;
  7077. tmp = I915_READ(SOUTH_CHICKEN2);
  7078. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7079. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7080. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7081. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7082. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7083. tmp = I915_READ(SOUTH_CHICKEN2);
  7084. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7085. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7086. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7087. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7088. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7089. }
  7090. /* WaMPhyProgramming:hsw */
  7091. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7092. {
  7093. uint32_t tmp;
  7094. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7095. tmp &= ~(0xFF << 24);
  7096. tmp |= (0x12 << 24);
  7097. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7098. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7099. tmp |= (1 << 11);
  7100. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7101. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7102. tmp |= (1 << 11);
  7103. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7104. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7105. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7106. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7107. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7108. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7109. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7110. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7111. tmp &= ~(7 << 13);
  7112. tmp |= (5 << 13);
  7113. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7114. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7115. tmp &= ~(7 << 13);
  7116. tmp |= (5 << 13);
  7117. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7118. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7119. tmp &= ~0xFF;
  7120. tmp |= 0x1C;
  7121. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7122. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7123. tmp &= ~0xFF;
  7124. tmp |= 0x1C;
  7125. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7126. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7127. tmp &= ~(0xFF << 16);
  7128. tmp |= (0x1C << 16);
  7129. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7130. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7131. tmp &= ~(0xFF << 16);
  7132. tmp |= (0x1C << 16);
  7133. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7134. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7135. tmp |= (1 << 27);
  7136. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7137. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7138. tmp |= (1 << 27);
  7139. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7140. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7141. tmp &= ~(0xF << 28);
  7142. tmp |= (4 << 28);
  7143. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7144. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7145. tmp &= ~(0xF << 28);
  7146. tmp |= (4 << 28);
  7147. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7148. }
  7149. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7150. * Programming" based on the parameters passed:
  7151. * - Sequence to enable CLKOUT_DP
  7152. * - Sequence to enable CLKOUT_DP without spread
  7153. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7154. */
  7155. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7156. bool with_fdi)
  7157. {
  7158. struct drm_i915_private *dev_priv = dev->dev_private;
  7159. uint32_t reg, tmp;
  7160. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7161. with_spread = true;
  7162. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7163. with_fdi = false;
  7164. mutex_lock(&dev_priv->sb_lock);
  7165. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7166. tmp &= ~SBI_SSCCTL_DISABLE;
  7167. tmp |= SBI_SSCCTL_PATHALT;
  7168. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7169. udelay(24);
  7170. if (with_spread) {
  7171. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7172. tmp &= ~SBI_SSCCTL_PATHALT;
  7173. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7174. if (with_fdi) {
  7175. lpt_reset_fdi_mphy(dev_priv);
  7176. lpt_program_fdi_mphy(dev_priv);
  7177. }
  7178. }
  7179. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7180. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7181. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7182. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7183. mutex_unlock(&dev_priv->sb_lock);
  7184. }
  7185. /* Sequence to disable CLKOUT_DP */
  7186. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7187. {
  7188. struct drm_i915_private *dev_priv = dev->dev_private;
  7189. uint32_t reg, tmp;
  7190. mutex_lock(&dev_priv->sb_lock);
  7191. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7192. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7193. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7194. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7195. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7196. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7197. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7198. tmp |= SBI_SSCCTL_PATHALT;
  7199. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7200. udelay(32);
  7201. }
  7202. tmp |= SBI_SSCCTL_DISABLE;
  7203. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7204. }
  7205. mutex_unlock(&dev_priv->sb_lock);
  7206. }
  7207. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7208. static const uint16_t sscdivintphase[] = {
  7209. [BEND_IDX( 50)] = 0x3B23,
  7210. [BEND_IDX( 45)] = 0x3B23,
  7211. [BEND_IDX( 40)] = 0x3C23,
  7212. [BEND_IDX( 35)] = 0x3C23,
  7213. [BEND_IDX( 30)] = 0x3D23,
  7214. [BEND_IDX( 25)] = 0x3D23,
  7215. [BEND_IDX( 20)] = 0x3E23,
  7216. [BEND_IDX( 15)] = 0x3E23,
  7217. [BEND_IDX( 10)] = 0x3F23,
  7218. [BEND_IDX( 5)] = 0x3F23,
  7219. [BEND_IDX( 0)] = 0x0025,
  7220. [BEND_IDX( -5)] = 0x0025,
  7221. [BEND_IDX(-10)] = 0x0125,
  7222. [BEND_IDX(-15)] = 0x0125,
  7223. [BEND_IDX(-20)] = 0x0225,
  7224. [BEND_IDX(-25)] = 0x0225,
  7225. [BEND_IDX(-30)] = 0x0325,
  7226. [BEND_IDX(-35)] = 0x0325,
  7227. [BEND_IDX(-40)] = 0x0425,
  7228. [BEND_IDX(-45)] = 0x0425,
  7229. [BEND_IDX(-50)] = 0x0525,
  7230. };
  7231. /*
  7232. * Bend CLKOUT_DP
  7233. * steps -50 to 50 inclusive, in steps of 5
  7234. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7235. * change in clock period = -(steps / 10) * 5.787 ps
  7236. */
  7237. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7238. {
  7239. uint32_t tmp;
  7240. int idx = BEND_IDX(steps);
  7241. if (WARN_ON(steps % 5 != 0))
  7242. return;
  7243. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7244. return;
  7245. mutex_lock(&dev_priv->sb_lock);
  7246. if (steps % 10 != 0)
  7247. tmp = 0xAAAAAAAB;
  7248. else
  7249. tmp = 0x00000000;
  7250. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7251. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7252. tmp &= 0xffff0000;
  7253. tmp |= sscdivintphase[idx];
  7254. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7255. mutex_unlock(&dev_priv->sb_lock);
  7256. }
  7257. #undef BEND_IDX
  7258. static void lpt_init_pch_refclk(struct drm_device *dev)
  7259. {
  7260. struct intel_encoder *encoder;
  7261. bool has_vga = false;
  7262. for_each_intel_encoder(dev, encoder) {
  7263. switch (encoder->type) {
  7264. case INTEL_OUTPUT_ANALOG:
  7265. has_vga = true;
  7266. break;
  7267. default:
  7268. break;
  7269. }
  7270. }
  7271. if (has_vga) {
  7272. lpt_bend_clkout_dp(to_i915(dev), 0);
  7273. lpt_enable_clkout_dp(dev, true, true);
  7274. } else {
  7275. lpt_disable_clkout_dp(dev);
  7276. }
  7277. }
  7278. /*
  7279. * Initialize reference clocks when the driver loads
  7280. */
  7281. void intel_init_pch_refclk(struct drm_device *dev)
  7282. {
  7283. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7284. ironlake_init_pch_refclk(dev);
  7285. else if (HAS_PCH_LPT(dev))
  7286. lpt_init_pch_refclk(dev);
  7287. }
  7288. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7289. {
  7290. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7292. int pipe = intel_crtc->pipe;
  7293. uint32_t val;
  7294. val = 0;
  7295. switch (intel_crtc->config->pipe_bpp) {
  7296. case 18:
  7297. val |= PIPECONF_6BPC;
  7298. break;
  7299. case 24:
  7300. val |= PIPECONF_8BPC;
  7301. break;
  7302. case 30:
  7303. val |= PIPECONF_10BPC;
  7304. break;
  7305. case 36:
  7306. val |= PIPECONF_12BPC;
  7307. break;
  7308. default:
  7309. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7310. BUG();
  7311. }
  7312. if (intel_crtc->config->dither)
  7313. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7314. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7315. val |= PIPECONF_INTERLACED_ILK;
  7316. else
  7317. val |= PIPECONF_PROGRESSIVE;
  7318. if (intel_crtc->config->limited_color_range)
  7319. val |= PIPECONF_COLOR_RANGE_SELECT;
  7320. I915_WRITE(PIPECONF(pipe), val);
  7321. POSTING_READ(PIPECONF(pipe));
  7322. }
  7323. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7324. {
  7325. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7327. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7328. u32 val = 0;
  7329. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7330. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7331. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7332. val |= PIPECONF_INTERLACED_ILK;
  7333. else
  7334. val |= PIPECONF_PROGRESSIVE;
  7335. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7336. POSTING_READ(PIPECONF(cpu_transcoder));
  7337. }
  7338. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7339. {
  7340. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7342. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7343. u32 val = 0;
  7344. switch (intel_crtc->config->pipe_bpp) {
  7345. case 18:
  7346. val |= PIPEMISC_DITHER_6_BPC;
  7347. break;
  7348. case 24:
  7349. val |= PIPEMISC_DITHER_8_BPC;
  7350. break;
  7351. case 30:
  7352. val |= PIPEMISC_DITHER_10_BPC;
  7353. break;
  7354. case 36:
  7355. val |= PIPEMISC_DITHER_12_BPC;
  7356. break;
  7357. default:
  7358. /* Case prevented by pipe_config_set_bpp. */
  7359. BUG();
  7360. }
  7361. if (intel_crtc->config->dither)
  7362. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7363. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7364. }
  7365. }
  7366. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7367. {
  7368. /*
  7369. * Account for spread spectrum to avoid
  7370. * oversubscribing the link. Max center spread
  7371. * is 2.5%; use 5% for safety's sake.
  7372. */
  7373. u32 bps = target_clock * bpp * 21 / 20;
  7374. return DIV_ROUND_UP(bps, link_bw * 8);
  7375. }
  7376. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7377. {
  7378. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7379. }
  7380. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7381. struct intel_crtc_state *crtc_state,
  7382. intel_clock_t *reduced_clock)
  7383. {
  7384. struct drm_crtc *crtc = &intel_crtc->base;
  7385. struct drm_device *dev = crtc->dev;
  7386. struct drm_i915_private *dev_priv = dev->dev_private;
  7387. struct drm_atomic_state *state = crtc_state->base.state;
  7388. struct drm_connector *connector;
  7389. struct drm_connector_state *connector_state;
  7390. struct intel_encoder *encoder;
  7391. u32 dpll, fp, fp2;
  7392. int factor, i;
  7393. bool is_lvds = false, is_sdvo = false;
  7394. for_each_connector_in_state(state, connector, connector_state, i) {
  7395. if (connector_state->crtc != crtc_state->base.crtc)
  7396. continue;
  7397. encoder = to_intel_encoder(connector_state->best_encoder);
  7398. switch (encoder->type) {
  7399. case INTEL_OUTPUT_LVDS:
  7400. is_lvds = true;
  7401. break;
  7402. case INTEL_OUTPUT_SDVO:
  7403. case INTEL_OUTPUT_HDMI:
  7404. is_sdvo = true;
  7405. break;
  7406. default:
  7407. break;
  7408. }
  7409. }
  7410. /* Enable autotuning of the PLL clock (if permissible) */
  7411. factor = 21;
  7412. if (is_lvds) {
  7413. if ((intel_panel_use_ssc(dev_priv) &&
  7414. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7415. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7416. factor = 25;
  7417. } else if (crtc_state->sdvo_tv_clock)
  7418. factor = 20;
  7419. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7420. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7421. fp |= FP_CB_TUNE;
  7422. if (reduced_clock) {
  7423. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7424. if (reduced_clock->m < factor * reduced_clock->n)
  7425. fp2 |= FP_CB_TUNE;
  7426. } else {
  7427. fp2 = fp;
  7428. }
  7429. dpll = 0;
  7430. if (is_lvds)
  7431. dpll |= DPLLB_MODE_LVDS;
  7432. else
  7433. dpll |= DPLLB_MODE_DAC_SERIAL;
  7434. dpll |= (crtc_state->pixel_multiplier - 1)
  7435. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7436. if (is_sdvo)
  7437. dpll |= DPLL_SDVO_HIGH_SPEED;
  7438. if (crtc_state->has_dp_encoder)
  7439. dpll |= DPLL_SDVO_HIGH_SPEED;
  7440. /* compute bitmask from p1 value */
  7441. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7442. /* also FPA1 */
  7443. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7444. switch (crtc_state->dpll.p2) {
  7445. case 5:
  7446. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7447. break;
  7448. case 7:
  7449. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7450. break;
  7451. case 10:
  7452. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7453. break;
  7454. case 14:
  7455. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7456. break;
  7457. }
  7458. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7459. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7460. else
  7461. dpll |= PLL_REF_INPUT_DREFCLK;
  7462. dpll |= DPLL_VCO_ENABLE;
  7463. crtc_state->dpll_hw_state.dpll = dpll;
  7464. crtc_state->dpll_hw_state.fp0 = fp;
  7465. crtc_state->dpll_hw_state.fp1 = fp2;
  7466. }
  7467. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7468. struct intel_crtc_state *crtc_state)
  7469. {
  7470. struct drm_device *dev = crtc->base.dev;
  7471. struct drm_i915_private *dev_priv = dev->dev_private;
  7472. intel_clock_t reduced_clock;
  7473. bool has_reduced_clock = false;
  7474. struct intel_shared_dpll *pll;
  7475. const intel_limit_t *limit;
  7476. int refclk = 120000;
  7477. memset(&crtc_state->dpll_hw_state, 0,
  7478. sizeof(crtc_state->dpll_hw_state));
  7479. crtc->lowfreq_avail = false;
  7480. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7481. if (!crtc_state->has_pch_encoder)
  7482. return 0;
  7483. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7484. if (intel_panel_use_ssc(dev_priv)) {
  7485. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7486. dev_priv->vbt.lvds_ssc_freq);
  7487. refclk = dev_priv->vbt.lvds_ssc_freq;
  7488. }
  7489. if (intel_is_dual_link_lvds(dev)) {
  7490. if (refclk == 100000)
  7491. limit = &intel_limits_ironlake_dual_lvds_100m;
  7492. else
  7493. limit = &intel_limits_ironlake_dual_lvds;
  7494. } else {
  7495. if (refclk == 100000)
  7496. limit = &intel_limits_ironlake_single_lvds_100m;
  7497. else
  7498. limit = &intel_limits_ironlake_single_lvds;
  7499. }
  7500. } else {
  7501. limit = &intel_limits_ironlake_dac;
  7502. }
  7503. if (!crtc_state->clock_set &&
  7504. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7505. refclk, NULL, &crtc_state->dpll)) {
  7506. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7507. return -EINVAL;
  7508. }
  7509. ironlake_compute_dpll(crtc, crtc_state,
  7510. has_reduced_clock ? &reduced_clock : NULL);
  7511. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7512. if (pll == NULL) {
  7513. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7514. pipe_name(crtc->pipe));
  7515. return -EINVAL;
  7516. }
  7517. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7518. has_reduced_clock)
  7519. crtc->lowfreq_avail = true;
  7520. return 0;
  7521. }
  7522. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7523. struct intel_link_m_n *m_n)
  7524. {
  7525. struct drm_device *dev = crtc->base.dev;
  7526. struct drm_i915_private *dev_priv = dev->dev_private;
  7527. enum pipe pipe = crtc->pipe;
  7528. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7529. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7530. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7531. & ~TU_SIZE_MASK;
  7532. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7533. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7534. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7535. }
  7536. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7537. enum transcoder transcoder,
  7538. struct intel_link_m_n *m_n,
  7539. struct intel_link_m_n *m2_n2)
  7540. {
  7541. struct drm_device *dev = crtc->base.dev;
  7542. struct drm_i915_private *dev_priv = dev->dev_private;
  7543. enum pipe pipe = crtc->pipe;
  7544. if (INTEL_INFO(dev)->gen >= 5) {
  7545. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7546. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7547. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7548. & ~TU_SIZE_MASK;
  7549. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7550. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7551. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7552. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7553. * gen < 8) and if DRRS is supported (to make sure the
  7554. * registers are not unnecessarily read).
  7555. */
  7556. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7557. crtc->config->has_drrs) {
  7558. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7559. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7560. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7561. & ~TU_SIZE_MASK;
  7562. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7563. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7564. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7565. }
  7566. } else {
  7567. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7568. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7569. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7570. & ~TU_SIZE_MASK;
  7571. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7572. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7573. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7574. }
  7575. }
  7576. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7577. struct intel_crtc_state *pipe_config)
  7578. {
  7579. if (pipe_config->has_pch_encoder)
  7580. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7581. else
  7582. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7583. &pipe_config->dp_m_n,
  7584. &pipe_config->dp_m2_n2);
  7585. }
  7586. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7587. struct intel_crtc_state *pipe_config)
  7588. {
  7589. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7590. &pipe_config->fdi_m_n, NULL);
  7591. }
  7592. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7593. struct intel_crtc_state *pipe_config)
  7594. {
  7595. struct drm_device *dev = crtc->base.dev;
  7596. struct drm_i915_private *dev_priv = dev->dev_private;
  7597. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7598. uint32_t ps_ctrl = 0;
  7599. int id = -1;
  7600. int i;
  7601. /* find scaler attached to this pipe */
  7602. for (i = 0; i < crtc->num_scalers; i++) {
  7603. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7604. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7605. id = i;
  7606. pipe_config->pch_pfit.enabled = true;
  7607. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7608. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7609. break;
  7610. }
  7611. }
  7612. scaler_state->scaler_id = id;
  7613. if (id >= 0) {
  7614. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7615. } else {
  7616. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7617. }
  7618. }
  7619. static void
  7620. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7621. struct intel_initial_plane_config *plane_config)
  7622. {
  7623. struct drm_device *dev = crtc->base.dev;
  7624. struct drm_i915_private *dev_priv = dev->dev_private;
  7625. u32 val, base, offset, stride_mult, tiling;
  7626. int pipe = crtc->pipe;
  7627. int fourcc, pixel_format;
  7628. unsigned int aligned_height;
  7629. struct drm_framebuffer *fb;
  7630. struct intel_framebuffer *intel_fb;
  7631. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7632. if (!intel_fb) {
  7633. DRM_DEBUG_KMS("failed to alloc fb\n");
  7634. return;
  7635. }
  7636. fb = &intel_fb->base;
  7637. val = I915_READ(PLANE_CTL(pipe, 0));
  7638. if (!(val & PLANE_CTL_ENABLE))
  7639. goto error;
  7640. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7641. fourcc = skl_format_to_fourcc(pixel_format,
  7642. val & PLANE_CTL_ORDER_RGBX,
  7643. val & PLANE_CTL_ALPHA_MASK);
  7644. fb->pixel_format = fourcc;
  7645. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7646. tiling = val & PLANE_CTL_TILED_MASK;
  7647. switch (tiling) {
  7648. case PLANE_CTL_TILED_LINEAR:
  7649. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7650. break;
  7651. case PLANE_CTL_TILED_X:
  7652. plane_config->tiling = I915_TILING_X;
  7653. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7654. break;
  7655. case PLANE_CTL_TILED_Y:
  7656. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7657. break;
  7658. case PLANE_CTL_TILED_YF:
  7659. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7660. break;
  7661. default:
  7662. MISSING_CASE(tiling);
  7663. goto error;
  7664. }
  7665. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7666. plane_config->base = base;
  7667. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7668. val = I915_READ(PLANE_SIZE(pipe, 0));
  7669. fb->height = ((val >> 16) & 0xfff) + 1;
  7670. fb->width = ((val >> 0) & 0x1fff) + 1;
  7671. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7672. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7673. fb->pixel_format);
  7674. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7675. aligned_height = intel_fb_align_height(dev, fb->height,
  7676. fb->pixel_format,
  7677. fb->modifier[0]);
  7678. plane_config->size = fb->pitches[0] * aligned_height;
  7679. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7680. pipe_name(pipe), fb->width, fb->height,
  7681. fb->bits_per_pixel, base, fb->pitches[0],
  7682. plane_config->size);
  7683. plane_config->fb = intel_fb;
  7684. return;
  7685. error:
  7686. kfree(fb);
  7687. }
  7688. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7689. struct intel_crtc_state *pipe_config)
  7690. {
  7691. struct drm_device *dev = crtc->base.dev;
  7692. struct drm_i915_private *dev_priv = dev->dev_private;
  7693. uint32_t tmp;
  7694. tmp = I915_READ(PF_CTL(crtc->pipe));
  7695. if (tmp & PF_ENABLE) {
  7696. pipe_config->pch_pfit.enabled = true;
  7697. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7698. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7699. /* We currently do not free assignements of panel fitters on
  7700. * ivb/hsw (since we don't use the higher upscaling modes which
  7701. * differentiates them) so just WARN about this case for now. */
  7702. if (IS_GEN7(dev)) {
  7703. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7704. PF_PIPE_SEL_IVB(crtc->pipe));
  7705. }
  7706. }
  7707. }
  7708. static void
  7709. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7710. struct intel_initial_plane_config *plane_config)
  7711. {
  7712. struct drm_device *dev = crtc->base.dev;
  7713. struct drm_i915_private *dev_priv = dev->dev_private;
  7714. u32 val, base, offset;
  7715. int pipe = crtc->pipe;
  7716. int fourcc, pixel_format;
  7717. unsigned int aligned_height;
  7718. struct drm_framebuffer *fb;
  7719. struct intel_framebuffer *intel_fb;
  7720. val = I915_READ(DSPCNTR(pipe));
  7721. if (!(val & DISPLAY_PLANE_ENABLE))
  7722. return;
  7723. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7724. if (!intel_fb) {
  7725. DRM_DEBUG_KMS("failed to alloc fb\n");
  7726. return;
  7727. }
  7728. fb = &intel_fb->base;
  7729. if (INTEL_INFO(dev)->gen >= 4) {
  7730. if (val & DISPPLANE_TILED) {
  7731. plane_config->tiling = I915_TILING_X;
  7732. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7733. }
  7734. }
  7735. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7736. fourcc = i9xx_format_to_fourcc(pixel_format);
  7737. fb->pixel_format = fourcc;
  7738. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7739. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7740. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7741. offset = I915_READ(DSPOFFSET(pipe));
  7742. } else {
  7743. if (plane_config->tiling)
  7744. offset = I915_READ(DSPTILEOFF(pipe));
  7745. else
  7746. offset = I915_READ(DSPLINOFF(pipe));
  7747. }
  7748. plane_config->base = base;
  7749. val = I915_READ(PIPESRC(pipe));
  7750. fb->width = ((val >> 16) & 0xfff) + 1;
  7751. fb->height = ((val >> 0) & 0xfff) + 1;
  7752. val = I915_READ(DSPSTRIDE(pipe));
  7753. fb->pitches[0] = val & 0xffffffc0;
  7754. aligned_height = intel_fb_align_height(dev, fb->height,
  7755. fb->pixel_format,
  7756. fb->modifier[0]);
  7757. plane_config->size = fb->pitches[0] * aligned_height;
  7758. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7759. pipe_name(pipe), fb->width, fb->height,
  7760. fb->bits_per_pixel, base, fb->pitches[0],
  7761. plane_config->size);
  7762. plane_config->fb = intel_fb;
  7763. }
  7764. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7765. struct intel_crtc_state *pipe_config)
  7766. {
  7767. struct drm_device *dev = crtc->base.dev;
  7768. struct drm_i915_private *dev_priv = dev->dev_private;
  7769. enum intel_display_power_domain power_domain;
  7770. uint32_t tmp;
  7771. bool ret;
  7772. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7773. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7774. return false;
  7775. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7776. pipe_config->shared_dpll = NULL;
  7777. ret = false;
  7778. tmp = I915_READ(PIPECONF(crtc->pipe));
  7779. if (!(tmp & PIPECONF_ENABLE))
  7780. goto out;
  7781. switch (tmp & PIPECONF_BPC_MASK) {
  7782. case PIPECONF_6BPC:
  7783. pipe_config->pipe_bpp = 18;
  7784. break;
  7785. case PIPECONF_8BPC:
  7786. pipe_config->pipe_bpp = 24;
  7787. break;
  7788. case PIPECONF_10BPC:
  7789. pipe_config->pipe_bpp = 30;
  7790. break;
  7791. case PIPECONF_12BPC:
  7792. pipe_config->pipe_bpp = 36;
  7793. break;
  7794. default:
  7795. break;
  7796. }
  7797. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7798. pipe_config->limited_color_range = true;
  7799. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7800. struct intel_shared_dpll *pll;
  7801. enum intel_dpll_id pll_id;
  7802. pipe_config->has_pch_encoder = true;
  7803. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7804. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7805. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7806. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7807. if (HAS_PCH_IBX(dev_priv)) {
  7808. pll_id = (enum intel_dpll_id) crtc->pipe;
  7809. } else {
  7810. tmp = I915_READ(PCH_DPLL_SEL);
  7811. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7812. pll_id = DPLL_ID_PCH_PLL_B;
  7813. else
  7814. pll_id= DPLL_ID_PCH_PLL_A;
  7815. }
  7816. pipe_config->shared_dpll =
  7817. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7818. pll = pipe_config->shared_dpll;
  7819. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7820. &pipe_config->dpll_hw_state));
  7821. tmp = pipe_config->dpll_hw_state.dpll;
  7822. pipe_config->pixel_multiplier =
  7823. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7824. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7825. ironlake_pch_clock_get(crtc, pipe_config);
  7826. } else {
  7827. pipe_config->pixel_multiplier = 1;
  7828. }
  7829. intel_get_pipe_timings(crtc, pipe_config);
  7830. intel_get_pipe_src_size(crtc, pipe_config);
  7831. ironlake_get_pfit_config(crtc, pipe_config);
  7832. ret = true;
  7833. out:
  7834. intel_display_power_put(dev_priv, power_domain);
  7835. return ret;
  7836. }
  7837. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7838. {
  7839. struct drm_device *dev = dev_priv->dev;
  7840. struct intel_crtc *crtc;
  7841. for_each_intel_crtc(dev, crtc)
  7842. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7843. pipe_name(crtc->pipe));
  7844. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7845. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7846. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7847. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7848. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7849. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7850. "CPU PWM1 enabled\n");
  7851. if (IS_HASWELL(dev))
  7852. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7853. "CPU PWM2 enabled\n");
  7854. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7855. "PCH PWM1 enabled\n");
  7856. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7857. "Utility pin enabled\n");
  7858. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7859. /*
  7860. * In theory we can still leave IRQs enabled, as long as only the HPD
  7861. * interrupts remain enabled. We used to check for that, but since it's
  7862. * gen-specific and since we only disable LCPLL after we fully disable
  7863. * the interrupts, the check below should be enough.
  7864. */
  7865. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7866. }
  7867. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7868. {
  7869. struct drm_device *dev = dev_priv->dev;
  7870. if (IS_HASWELL(dev))
  7871. return I915_READ(D_COMP_HSW);
  7872. else
  7873. return I915_READ(D_COMP_BDW);
  7874. }
  7875. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7876. {
  7877. struct drm_device *dev = dev_priv->dev;
  7878. if (IS_HASWELL(dev)) {
  7879. mutex_lock(&dev_priv->rps.hw_lock);
  7880. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7881. val))
  7882. DRM_ERROR("Failed to write to D_COMP\n");
  7883. mutex_unlock(&dev_priv->rps.hw_lock);
  7884. } else {
  7885. I915_WRITE(D_COMP_BDW, val);
  7886. POSTING_READ(D_COMP_BDW);
  7887. }
  7888. }
  7889. /*
  7890. * This function implements pieces of two sequences from BSpec:
  7891. * - Sequence for display software to disable LCPLL
  7892. * - Sequence for display software to allow package C8+
  7893. * The steps implemented here are just the steps that actually touch the LCPLL
  7894. * register. Callers should take care of disabling all the display engine
  7895. * functions, doing the mode unset, fixing interrupts, etc.
  7896. */
  7897. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7898. bool switch_to_fclk, bool allow_power_down)
  7899. {
  7900. uint32_t val;
  7901. assert_can_disable_lcpll(dev_priv);
  7902. val = I915_READ(LCPLL_CTL);
  7903. if (switch_to_fclk) {
  7904. val |= LCPLL_CD_SOURCE_FCLK;
  7905. I915_WRITE(LCPLL_CTL, val);
  7906. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7907. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7908. DRM_ERROR("Switching to FCLK failed\n");
  7909. val = I915_READ(LCPLL_CTL);
  7910. }
  7911. val |= LCPLL_PLL_DISABLE;
  7912. I915_WRITE(LCPLL_CTL, val);
  7913. POSTING_READ(LCPLL_CTL);
  7914. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7915. DRM_ERROR("LCPLL still locked\n");
  7916. val = hsw_read_dcomp(dev_priv);
  7917. val |= D_COMP_COMP_DISABLE;
  7918. hsw_write_dcomp(dev_priv, val);
  7919. ndelay(100);
  7920. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7921. 1))
  7922. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7923. if (allow_power_down) {
  7924. val = I915_READ(LCPLL_CTL);
  7925. val |= LCPLL_POWER_DOWN_ALLOW;
  7926. I915_WRITE(LCPLL_CTL, val);
  7927. POSTING_READ(LCPLL_CTL);
  7928. }
  7929. }
  7930. /*
  7931. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7932. * source.
  7933. */
  7934. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7935. {
  7936. uint32_t val;
  7937. val = I915_READ(LCPLL_CTL);
  7938. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7939. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7940. return;
  7941. /*
  7942. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7943. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7944. */
  7945. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7946. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7947. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7948. I915_WRITE(LCPLL_CTL, val);
  7949. POSTING_READ(LCPLL_CTL);
  7950. }
  7951. val = hsw_read_dcomp(dev_priv);
  7952. val |= D_COMP_COMP_FORCE;
  7953. val &= ~D_COMP_COMP_DISABLE;
  7954. hsw_write_dcomp(dev_priv, val);
  7955. val = I915_READ(LCPLL_CTL);
  7956. val &= ~LCPLL_PLL_DISABLE;
  7957. I915_WRITE(LCPLL_CTL, val);
  7958. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7959. DRM_ERROR("LCPLL not locked yet\n");
  7960. if (val & LCPLL_CD_SOURCE_FCLK) {
  7961. val = I915_READ(LCPLL_CTL);
  7962. val &= ~LCPLL_CD_SOURCE_FCLK;
  7963. I915_WRITE(LCPLL_CTL, val);
  7964. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7965. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7966. DRM_ERROR("Switching back to LCPLL failed\n");
  7967. }
  7968. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7969. intel_update_cdclk(dev_priv->dev);
  7970. }
  7971. /*
  7972. * Package states C8 and deeper are really deep PC states that can only be
  7973. * reached when all the devices on the system allow it, so even if the graphics
  7974. * device allows PC8+, it doesn't mean the system will actually get to these
  7975. * states. Our driver only allows PC8+ when going into runtime PM.
  7976. *
  7977. * The requirements for PC8+ are that all the outputs are disabled, the power
  7978. * well is disabled and most interrupts are disabled, and these are also
  7979. * requirements for runtime PM. When these conditions are met, we manually do
  7980. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7981. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7982. * hang the machine.
  7983. *
  7984. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7985. * the state of some registers, so when we come back from PC8+ we need to
  7986. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7987. * need to take care of the registers kept by RC6. Notice that this happens even
  7988. * if we don't put the device in PCI D3 state (which is what currently happens
  7989. * because of the runtime PM support).
  7990. *
  7991. * For more, read "Display Sequences for Package C8" on the hardware
  7992. * documentation.
  7993. */
  7994. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7995. {
  7996. struct drm_device *dev = dev_priv->dev;
  7997. uint32_t val;
  7998. DRM_DEBUG_KMS("Enabling package C8+\n");
  7999. if (HAS_PCH_LPT_LP(dev)) {
  8000. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8001. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8002. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8003. }
  8004. lpt_disable_clkout_dp(dev);
  8005. hsw_disable_lcpll(dev_priv, true, true);
  8006. }
  8007. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8008. {
  8009. struct drm_device *dev = dev_priv->dev;
  8010. uint32_t val;
  8011. DRM_DEBUG_KMS("Disabling package C8+\n");
  8012. hsw_restore_lcpll(dev_priv);
  8013. lpt_init_pch_refclk(dev);
  8014. if (HAS_PCH_LPT_LP(dev)) {
  8015. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8016. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8017. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8018. }
  8019. }
  8020. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8021. {
  8022. struct drm_device *dev = old_state->dev;
  8023. struct intel_atomic_state *old_intel_state =
  8024. to_intel_atomic_state(old_state);
  8025. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8026. broxton_set_cdclk(to_i915(dev), req_cdclk);
  8027. }
  8028. /* compute the max rate for new configuration */
  8029. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8030. {
  8031. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8032. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8033. struct drm_crtc *crtc;
  8034. struct drm_crtc_state *cstate;
  8035. struct intel_crtc_state *crtc_state;
  8036. unsigned max_pixel_rate = 0, i;
  8037. enum pipe pipe;
  8038. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8039. sizeof(intel_state->min_pixclk));
  8040. for_each_crtc_in_state(state, crtc, cstate, i) {
  8041. int pixel_rate;
  8042. crtc_state = to_intel_crtc_state(cstate);
  8043. if (!crtc_state->base.enable) {
  8044. intel_state->min_pixclk[i] = 0;
  8045. continue;
  8046. }
  8047. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8048. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8049. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8050. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8051. intel_state->min_pixclk[i] = pixel_rate;
  8052. }
  8053. for_each_pipe(dev_priv, pipe)
  8054. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8055. return max_pixel_rate;
  8056. }
  8057. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8058. {
  8059. struct drm_i915_private *dev_priv = dev->dev_private;
  8060. uint32_t val, data;
  8061. int ret;
  8062. if (WARN((I915_READ(LCPLL_CTL) &
  8063. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8064. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8065. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8066. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8067. "trying to change cdclk frequency with cdclk not enabled\n"))
  8068. return;
  8069. mutex_lock(&dev_priv->rps.hw_lock);
  8070. ret = sandybridge_pcode_write(dev_priv,
  8071. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8072. mutex_unlock(&dev_priv->rps.hw_lock);
  8073. if (ret) {
  8074. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8075. return;
  8076. }
  8077. val = I915_READ(LCPLL_CTL);
  8078. val |= LCPLL_CD_SOURCE_FCLK;
  8079. I915_WRITE(LCPLL_CTL, val);
  8080. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8081. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8082. DRM_ERROR("Switching to FCLK failed\n");
  8083. val = I915_READ(LCPLL_CTL);
  8084. val &= ~LCPLL_CLK_FREQ_MASK;
  8085. switch (cdclk) {
  8086. case 450000:
  8087. val |= LCPLL_CLK_FREQ_450;
  8088. data = 0;
  8089. break;
  8090. case 540000:
  8091. val |= LCPLL_CLK_FREQ_54O_BDW;
  8092. data = 1;
  8093. break;
  8094. case 337500:
  8095. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8096. data = 2;
  8097. break;
  8098. case 675000:
  8099. val |= LCPLL_CLK_FREQ_675_BDW;
  8100. data = 3;
  8101. break;
  8102. default:
  8103. WARN(1, "invalid cdclk frequency\n");
  8104. return;
  8105. }
  8106. I915_WRITE(LCPLL_CTL, val);
  8107. val = I915_READ(LCPLL_CTL);
  8108. val &= ~LCPLL_CD_SOURCE_FCLK;
  8109. I915_WRITE(LCPLL_CTL, val);
  8110. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8111. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8112. DRM_ERROR("Switching back to LCPLL failed\n");
  8113. mutex_lock(&dev_priv->rps.hw_lock);
  8114. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8115. mutex_unlock(&dev_priv->rps.hw_lock);
  8116. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8117. intel_update_cdclk(dev);
  8118. WARN(cdclk != dev_priv->cdclk_freq,
  8119. "cdclk requested %d kHz but got %d kHz\n",
  8120. cdclk, dev_priv->cdclk_freq);
  8121. }
  8122. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8123. {
  8124. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8125. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8126. int max_pixclk = ilk_max_pixel_rate(state);
  8127. int cdclk;
  8128. /*
  8129. * FIXME should also account for plane ratio
  8130. * once 64bpp pixel formats are supported.
  8131. */
  8132. if (max_pixclk > 540000)
  8133. cdclk = 675000;
  8134. else if (max_pixclk > 450000)
  8135. cdclk = 540000;
  8136. else if (max_pixclk > 337500)
  8137. cdclk = 450000;
  8138. else
  8139. cdclk = 337500;
  8140. if (cdclk > dev_priv->max_cdclk_freq) {
  8141. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8142. cdclk, dev_priv->max_cdclk_freq);
  8143. return -EINVAL;
  8144. }
  8145. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8146. if (!intel_state->active_crtcs)
  8147. intel_state->dev_cdclk = 337500;
  8148. return 0;
  8149. }
  8150. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8151. {
  8152. struct drm_device *dev = old_state->dev;
  8153. struct intel_atomic_state *old_intel_state =
  8154. to_intel_atomic_state(old_state);
  8155. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8156. broadwell_set_cdclk(dev, req_cdclk);
  8157. }
  8158. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8159. struct intel_crtc_state *crtc_state)
  8160. {
  8161. struct intel_encoder *intel_encoder =
  8162. intel_ddi_get_crtc_new_encoder(crtc_state);
  8163. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8164. if (!intel_ddi_pll_select(crtc, crtc_state))
  8165. return -EINVAL;
  8166. }
  8167. crtc->lowfreq_avail = false;
  8168. return 0;
  8169. }
  8170. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8171. enum port port,
  8172. struct intel_crtc_state *pipe_config)
  8173. {
  8174. enum intel_dpll_id id;
  8175. switch (port) {
  8176. case PORT_A:
  8177. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8178. id = DPLL_ID_SKL_DPLL0;
  8179. break;
  8180. case PORT_B:
  8181. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8182. id = DPLL_ID_SKL_DPLL1;
  8183. break;
  8184. case PORT_C:
  8185. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8186. id = DPLL_ID_SKL_DPLL2;
  8187. break;
  8188. default:
  8189. DRM_ERROR("Incorrect port type\n");
  8190. return;
  8191. }
  8192. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8193. }
  8194. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8195. enum port port,
  8196. struct intel_crtc_state *pipe_config)
  8197. {
  8198. enum intel_dpll_id id;
  8199. u32 temp;
  8200. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8201. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8202. switch (pipe_config->ddi_pll_sel) {
  8203. case SKL_DPLL0:
  8204. id = DPLL_ID_SKL_DPLL0;
  8205. break;
  8206. case SKL_DPLL1:
  8207. id = DPLL_ID_SKL_DPLL1;
  8208. break;
  8209. case SKL_DPLL2:
  8210. id = DPLL_ID_SKL_DPLL2;
  8211. break;
  8212. case SKL_DPLL3:
  8213. id = DPLL_ID_SKL_DPLL3;
  8214. break;
  8215. default:
  8216. MISSING_CASE(pipe_config->ddi_pll_sel);
  8217. return;
  8218. }
  8219. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8220. }
  8221. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8222. enum port port,
  8223. struct intel_crtc_state *pipe_config)
  8224. {
  8225. enum intel_dpll_id id;
  8226. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8227. switch (pipe_config->ddi_pll_sel) {
  8228. case PORT_CLK_SEL_WRPLL1:
  8229. id = DPLL_ID_WRPLL1;
  8230. break;
  8231. case PORT_CLK_SEL_WRPLL2:
  8232. id = DPLL_ID_WRPLL2;
  8233. break;
  8234. case PORT_CLK_SEL_SPLL:
  8235. id = DPLL_ID_SPLL;
  8236. break;
  8237. case PORT_CLK_SEL_LCPLL_810:
  8238. id = DPLL_ID_LCPLL_810;
  8239. break;
  8240. case PORT_CLK_SEL_LCPLL_1350:
  8241. id = DPLL_ID_LCPLL_1350;
  8242. break;
  8243. case PORT_CLK_SEL_LCPLL_2700:
  8244. id = DPLL_ID_LCPLL_2700;
  8245. break;
  8246. default:
  8247. MISSING_CASE(pipe_config->ddi_pll_sel);
  8248. /* fall through */
  8249. case PORT_CLK_SEL_NONE:
  8250. return;
  8251. }
  8252. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8253. }
  8254. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8255. struct intel_crtc_state *pipe_config,
  8256. unsigned long *power_domain_mask)
  8257. {
  8258. struct drm_device *dev = crtc->base.dev;
  8259. struct drm_i915_private *dev_priv = dev->dev_private;
  8260. enum intel_display_power_domain power_domain;
  8261. u32 tmp;
  8262. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8263. /*
  8264. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8265. * consistency and less surprising code; it's in always on power).
  8266. */
  8267. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8268. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8269. enum pipe trans_edp_pipe;
  8270. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8271. default:
  8272. WARN(1, "unknown pipe linked to edp transcoder\n");
  8273. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8274. case TRANS_DDI_EDP_INPUT_A_ON:
  8275. trans_edp_pipe = PIPE_A;
  8276. break;
  8277. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8278. trans_edp_pipe = PIPE_B;
  8279. break;
  8280. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8281. trans_edp_pipe = PIPE_C;
  8282. break;
  8283. }
  8284. if (trans_edp_pipe == crtc->pipe)
  8285. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8286. }
  8287. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8288. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8289. return false;
  8290. *power_domain_mask |= BIT(power_domain);
  8291. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8292. return tmp & PIPECONF_ENABLE;
  8293. }
  8294. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8295. struct intel_crtc_state *pipe_config,
  8296. unsigned long *power_domain_mask)
  8297. {
  8298. struct drm_device *dev = crtc->base.dev;
  8299. struct drm_i915_private *dev_priv = dev->dev_private;
  8300. enum intel_display_power_domain power_domain;
  8301. enum port port;
  8302. enum transcoder cpu_transcoder;
  8303. u32 tmp;
  8304. pipe_config->has_dsi_encoder = false;
  8305. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8306. if (port == PORT_A)
  8307. cpu_transcoder = TRANSCODER_DSI_A;
  8308. else
  8309. cpu_transcoder = TRANSCODER_DSI_C;
  8310. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8311. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8312. continue;
  8313. *power_domain_mask |= BIT(power_domain);
  8314. /*
  8315. * The PLL needs to be enabled with a valid divider
  8316. * configuration, otherwise accessing DSI registers will hang
  8317. * the machine. See BSpec North Display Engine
  8318. * registers/MIPI[BXT]. We can break out here early, since we
  8319. * need the same DSI PLL to be enabled for both DSI ports.
  8320. */
  8321. if (!intel_dsi_pll_is_enabled(dev_priv))
  8322. break;
  8323. /* XXX: this works for video mode only */
  8324. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8325. if (!(tmp & DPI_ENABLE))
  8326. continue;
  8327. tmp = I915_READ(MIPI_CTRL(port));
  8328. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8329. continue;
  8330. pipe_config->cpu_transcoder = cpu_transcoder;
  8331. pipe_config->has_dsi_encoder = true;
  8332. break;
  8333. }
  8334. return pipe_config->has_dsi_encoder;
  8335. }
  8336. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8337. struct intel_crtc_state *pipe_config)
  8338. {
  8339. struct drm_device *dev = crtc->base.dev;
  8340. struct drm_i915_private *dev_priv = dev->dev_private;
  8341. struct intel_shared_dpll *pll;
  8342. enum port port;
  8343. uint32_t tmp;
  8344. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8345. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8346. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8347. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8348. else if (IS_BROXTON(dev))
  8349. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8350. else
  8351. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8352. pll = pipe_config->shared_dpll;
  8353. if (pll) {
  8354. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8355. &pipe_config->dpll_hw_state));
  8356. }
  8357. /*
  8358. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8359. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8360. * the PCH transcoder is on.
  8361. */
  8362. if (INTEL_INFO(dev)->gen < 9 &&
  8363. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8364. pipe_config->has_pch_encoder = true;
  8365. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8366. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8367. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8368. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8369. }
  8370. }
  8371. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8372. struct intel_crtc_state *pipe_config)
  8373. {
  8374. struct drm_device *dev = crtc->base.dev;
  8375. struct drm_i915_private *dev_priv = dev->dev_private;
  8376. enum intel_display_power_domain power_domain;
  8377. unsigned long power_domain_mask;
  8378. bool active;
  8379. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8380. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8381. return false;
  8382. power_domain_mask = BIT(power_domain);
  8383. pipe_config->shared_dpll = NULL;
  8384. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8385. if (IS_BROXTON(dev_priv)) {
  8386. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8387. &power_domain_mask);
  8388. WARN_ON(active && pipe_config->has_dsi_encoder);
  8389. if (pipe_config->has_dsi_encoder)
  8390. active = true;
  8391. }
  8392. if (!active)
  8393. goto out;
  8394. if (!pipe_config->has_dsi_encoder) {
  8395. haswell_get_ddi_port_state(crtc, pipe_config);
  8396. intel_get_pipe_timings(crtc, pipe_config);
  8397. }
  8398. intel_get_pipe_src_size(crtc, pipe_config);
  8399. pipe_config->gamma_mode =
  8400. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8401. if (INTEL_INFO(dev)->gen >= 9) {
  8402. skl_init_scalers(dev, crtc, pipe_config);
  8403. }
  8404. if (INTEL_INFO(dev)->gen >= 9) {
  8405. pipe_config->scaler_state.scaler_id = -1;
  8406. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8407. }
  8408. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8409. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8410. power_domain_mask |= BIT(power_domain);
  8411. if (INTEL_INFO(dev)->gen >= 9)
  8412. skylake_get_pfit_config(crtc, pipe_config);
  8413. else
  8414. ironlake_get_pfit_config(crtc, pipe_config);
  8415. }
  8416. if (IS_HASWELL(dev))
  8417. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8418. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8419. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8420. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8421. pipe_config->pixel_multiplier =
  8422. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8423. } else {
  8424. pipe_config->pixel_multiplier = 1;
  8425. }
  8426. out:
  8427. for_each_power_domain(power_domain, power_domain_mask)
  8428. intel_display_power_put(dev_priv, power_domain);
  8429. return active;
  8430. }
  8431. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8432. const struct intel_plane_state *plane_state)
  8433. {
  8434. struct drm_device *dev = crtc->dev;
  8435. struct drm_i915_private *dev_priv = dev->dev_private;
  8436. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8437. uint32_t cntl = 0, size = 0;
  8438. if (plane_state && plane_state->visible) {
  8439. unsigned int width = plane_state->base.crtc_w;
  8440. unsigned int height = plane_state->base.crtc_h;
  8441. unsigned int stride = roundup_pow_of_two(width) * 4;
  8442. switch (stride) {
  8443. default:
  8444. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8445. width, stride);
  8446. stride = 256;
  8447. /* fallthrough */
  8448. case 256:
  8449. case 512:
  8450. case 1024:
  8451. case 2048:
  8452. break;
  8453. }
  8454. cntl |= CURSOR_ENABLE |
  8455. CURSOR_GAMMA_ENABLE |
  8456. CURSOR_FORMAT_ARGB |
  8457. CURSOR_STRIDE(stride);
  8458. size = (height << 12) | width;
  8459. }
  8460. if (intel_crtc->cursor_cntl != 0 &&
  8461. (intel_crtc->cursor_base != base ||
  8462. intel_crtc->cursor_size != size ||
  8463. intel_crtc->cursor_cntl != cntl)) {
  8464. /* On these chipsets we can only modify the base/size/stride
  8465. * whilst the cursor is disabled.
  8466. */
  8467. I915_WRITE(CURCNTR(PIPE_A), 0);
  8468. POSTING_READ(CURCNTR(PIPE_A));
  8469. intel_crtc->cursor_cntl = 0;
  8470. }
  8471. if (intel_crtc->cursor_base != base) {
  8472. I915_WRITE(CURBASE(PIPE_A), base);
  8473. intel_crtc->cursor_base = base;
  8474. }
  8475. if (intel_crtc->cursor_size != size) {
  8476. I915_WRITE(CURSIZE, size);
  8477. intel_crtc->cursor_size = size;
  8478. }
  8479. if (intel_crtc->cursor_cntl != cntl) {
  8480. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8481. POSTING_READ(CURCNTR(PIPE_A));
  8482. intel_crtc->cursor_cntl = cntl;
  8483. }
  8484. }
  8485. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8486. const struct intel_plane_state *plane_state)
  8487. {
  8488. struct drm_device *dev = crtc->dev;
  8489. struct drm_i915_private *dev_priv = dev->dev_private;
  8490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8491. int pipe = intel_crtc->pipe;
  8492. uint32_t cntl = 0;
  8493. if (plane_state && plane_state->visible) {
  8494. cntl = MCURSOR_GAMMA_ENABLE;
  8495. switch (plane_state->base.crtc_w) {
  8496. case 64:
  8497. cntl |= CURSOR_MODE_64_ARGB_AX;
  8498. break;
  8499. case 128:
  8500. cntl |= CURSOR_MODE_128_ARGB_AX;
  8501. break;
  8502. case 256:
  8503. cntl |= CURSOR_MODE_256_ARGB_AX;
  8504. break;
  8505. default:
  8506. MISSING_CASE(plane_state->base.crtc_w);
  8507. return;
  8508. }
  8509. cntl |= pipe << 28; /* Connect to correct pipe */
  8510. if (HAS_DDI(dev))
  8511. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8512. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8513. cntl |= CURSOR_ROTATE_180;
  8514. }
  8515. if (intel_crtc->cursor_cntl != cntl) {
  8516. I915_WRITE(CURCNTR(pipe), cntl);
  8517. POSTING_READ(CURCNTR(pipe));
  8518. intel_crtc->cursor_cntl = cntl;
  8519. }
  8520. /* and commit changes on next vblank */
  8521. I915_WRITE(CURBASE(pipe), base);
  8522. POSTING_READ(CURBASE(pipe));
  8523. intel_crtc->cursor_base = base;
  8524. }
  8525. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8526. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8527. const struct intel_plane_state *plane_state)
  8528. {
  8529. struct drm_device *dev = crtc->dev;
  8530. struct drm_i915_private *dev_priv = dev->dev_private;
  8531. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8532. int pipe = intel_crtc->pipe;
  8533. u32 base = intel_crtc->cursor_addr;
  8534. u32 pos = 0;
  8535. if (plane_state) {
  8536. int x = plane_state->base.crtc_x;
  8537. int y = plane_state->base.crtc_y;
  8538. if (x < 0) {
  8539. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8540. x = -x;
  8541. }
  8542. pos |= x << CURSOR_X_SHIFT;
  8543. if (y < 0) {
  8544. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8545. y = -y;
  8546. }
  8547. pos |= y << CURSOR_Y_SHIFT;
  8548. /* ILK+ do this automagically */
  8549. if (HAS_GMCH_DISPLAY(dev) &&
  8550. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8551. base += (plane_state->base.crtc_h *
  8552. plane_state->base.crtc_w - 1) * 4;
  8553. }
  8554. }
  8555. I915_WRITE(CURPOS(pipe), pos);
  8556. if (IS_845G(dev) || IS_I865G(dev))
  8557. i845_update_cursor(crtc, base, plane_state);
  8558. else
  8559. i9xx_update_cursor(crtc, base, plane_state);
  8560. }
  8561. static bool cursor_size_ok(struct drm_device *dev,
  8562. uint32_t width, uint32_t height)
  8563. {
  8564. if (width == 0 || height == 0)
  8565. return false;
  8566. /*
  8567. * 845g/865g are special in that they are only limited by
  8568. * the width of their cursors, the height is arbitrary up to
  8569. * the precision of the register. Everything else requires
  8570. * square cursors, limited to a few power-of-two sizes.
  8571. */
  8572. if (IS_845G(dev) || IS_I865G(dev)) {
  8573. if ((width & 63) != 0)
  8574. return false;
  8575. if (width > (IS_845G(dev) ? 64 : 512))
  8576. return false;
  8577. if (height > 1023)
  8578. return false;
  8579. } else {
  8580. switch (width | height) {
  8581. case 256:
  8582. case 128:
  8583. if (IS_GEN2(dev))
  8584. return false;
  8585. case 64:
  8586. break;
  8587. default:
  8588. return false;
  8589. }
  8590. }
  8591. return true;
  8592. }
  8593. /* VESA 640x480x72Hz mode to set on the pipe */
  8594. static struct drm_display_mode load_detect_mode = {
  8595. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8596. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8597. };
  8598. struct drm_framebuffer *
  8599. __intel_framebuffer_create(struct drm_device *dev,
  8600. struct drm_mode_fb_cmd2 *mode_cmd,
  8601. struct drm_i915_gem_object *obj)
  8602. {
  8603. struct intel_framebuffer *intel_fb;
  8604. int ret;
  8605. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8606. if (!intel_fb)
  8607. return ERR_PTR(-ENOMEM);
  8608. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8609. if (ret)
  8610. goto err;
  8611. return &intel_fb->base;
  8612. err:
  8613. kfree(intel_fb);
  8614. return ERR_PTR(ret);
  8615. }
  8616. static struct drm_framebuffer *
  8617. intel_framebuffer_create(struct drm_device *dev,
  8618. struct drm_mode_fb_cmd2 *mode_cmd,
  8619. struct drm_i915_gem_object *obj)
  8620. {
  8621. struct drm_framebuffer *fb;
  8622. int ret;
  8623. ret = i915_mutex_lock_interruptible(dev);
  8624. if (ret)
  8625. return ERR_PTR(ret);
  8626. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8627. mutex_unlock(&dev->struct_mutex);
  8628. return fb;
  8629. }
  8630. static u32
  8631. intel_framebuffer_pitch_for_width(int width, int bpp)
  8632. {
  8633. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8634. return ALIGN(pitch, 64);
  8635. }
  8636. static u32
  8637. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8638. {
  8639. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8640. return PAGE_ALIGN(pitch * mode->vdisplay);
  8641. }
  8642. static struct drm_framebuffer *
  8643. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8644. struct drm_display_mode *mode,
  8645. int depth, int bpp)
  8646. {
  8647. struct drm_framebuffer *fb;
  8648. struct drm_i915_gem_object *obj;
  8649. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8650. obj = i915_gem_alloc_object(dev,
  8651. intel_framebuffer_size_for_mode(mode, bpp));
  8652. if (obj == NULL)
  8653. return ERR_PTR(-ENOMEM);
  8654. mode_cmd.width = mode->hdisplay;
  8655. mode_cmd.height = mode->vdisplay;
  8656. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8657. bpp);
  8658. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8659. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8660. if (IS_ERR(fb))
  8661. drm_gem_object_unreference_unlocked(&obj->base);
  8662. return fb;
  8663. }
  8664. static struct drm_framebuffer *
  8665. mode_fits_in_fbdev(struct drm_device *dev,
  8666. struct drm_display_mode *mode)
  8667. {
  8668. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8669. struct drm_i915_private *dev_priv = dev->dev_private;
  8670. struct drm_i915_gem_object *obj;
  8671. struct drm_framebuffer *fb;
  8672. if (!dev_priv->fbdev)
  8673. return NULL;
  8674. if (!dev_priv->fbdev->fb)
  8675. return NULL;
  8676. obj = dev_priv->fbdev->fb->obj;
  8677. BUG_ON(!obj);
  8678. fb = &dev_priv->fbdev->fb->base;
  8679. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8680. fb->bits_per_pixel))
  8681. return NULL;
  8682. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8683. return NULL;
  8684. drm_framebuffer_reference(fb);
  8685. return fb;
  8686. #else
  8687. return NULL;
  8688. #endif
  8689. }
  8690. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8691. struct drm_crtc *crtc,
  8692. struct drm_display_mode *mode,
  8693. struct drm_framebuffer *fb,
  8694. int x, int y)
  8695. {
  8696. struct drm_plane_state *plane_state;
  8697. int hdisplay, vdisplay;
  8698. int ret;
  8699. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8700. if (IS_ERR(plane_state))
  8701. return PTR_ERR(plane_state);
  8702. if (mode)
  8703. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8704. else
  8705. hdisplay = vdisplay = 0;
  8706. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8707. if (ret)
  8708. return ret;
  8709. drm_atomic_set_fb_for_plane(plane_state, fb);
  8710. plane_state->crtc_x = 0;
  8711. plane_state->crtc_y = 0;
  8712. plane_state->crtc_w = hdisplay;
  8713. plane_state->crtc_h = vdisplay;
  8714. plane_state->src_x = x << 16;
  8715. plane_state->src_y = y << 16;
  8716. plane_state->src_w = hdisplay << 16;
  8717. plane_state->src_h = vdisplay << 16;
  8718. return 0;
  8719. }
  8720. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8721. struct drm_display_mode *mode,
  8722. struct intel_load_detect_pipe *old,
  8723. struct drm_modeset_acquire_ctx *ctx)
  8724. {
  8725. struct intel_crtc *intel_crtc;
  8726. struct intel_encoder *intel_encoder =
  8727. intel_attached_encoder(connector);
  8728. struct drm_crtc *possible_crtc;
  8729. struct drm_encoder *encoder = &intel_encoder->base;
  8730. struct drm_crtc *crtc = NULL;
  8731. struct drm_device *dev = encoder->dev;
  8732. struct drm_framebuffer *fb;
  8733. struct drm_mode_config *config = &dev->mode_config;
  8734. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8735. struct drm_connector_state *connector_state;
  8736. struct intel_crtc_state *crtc_state;
  8737. int ret, i = -1;
  8738. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8739. connector->base.id, connector->name,
  8740. encoder->base.id, encoder->name);
  8741. old->restore_state = NULL;
  8742. retry:
  8743. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8744. if (ret)
  8745. goto fail;
  8746. /*
  8747. * Algorithm gets a little messy:
  8748. *
  8749. * - if the connector already has an assigned crtc, use it (but make
  8750. * sure it's on first)
  8751. *
  8752. * - try to find the first unused crtc that can drive this connector,
  8753. * and use that if we find one
  8754. */
  8755. /* See if we already have a CRTC for this connector */
  8756. if (connector->state->crtc) {
  8757. crtc = connector->state->crtc;
  8758. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8759. if (ret)
  8760. goto fail;
  8761. /* Make sure the crtc and connector are running */
  8762. goto found;
  8763. }
  8764. /* Find an unused one (if possible) */
  8765. for_each_crtc(dev, possible_crtc) {
  8766. i++;
  8767. if (!(encoder->possible_crtcs & (1 << i)))
  8768. continue;
  8769. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8770. if (ret)
  8771. goto fail;
  8772. if (possible_crtc->state->enable) {
  8773. drm_modeset_unlock(&possible_crtc->mutex);
  8774. continue;
  8775. }
  8776. crtc = possible_crtc;
  8777. break;
  8778. }
  8779. /*
  8780. * If we didn't find an unused CRTC, don't use any.
  8781. */
  8782. if (!crtc) {
  8783. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8784. goto fail;
  8785. }
  8786. found:
  8787. intel_crtc = to_intel_crtc(crtc);
  8788. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8789. if (ret)
  8790. goto fail;
  8791. state = drm_atomic_state_alloc(dev);
  8792. restore_state = drm_atomic_state_alloc(dev);
  8793. if (!state || !restore_state) {
  8794. ret = -ENOMEM;
  8795. goto fail;
  8796. }
  8797. state->acquire_ctx = ctx;
  8798. restore_state->acquire_ctx = ctx;
  8799. connector_state = drm_atomic_get_connector_state(state, connector);
  8800. if (IS_ERR(connector_state)) {
  8801. ret = PTR_ERR(connector_state);
  8802. goto fail;
  8803. }
  8804. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8805. if (ret)
  8806. goto fail;
  8807. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8808. if (IS_ERR(crtc_state)) {
  8809. ret = PTR_ERR(crtc_state);
  8810. goto fail;
  8811. }
  8812. crtc_state->base.active = crtc_state->base.enable = true;
  8813. if (!mode)
  8814. mode = &load_detect_mode;
  8815. /* We need a framebuffer large enough to accommodate all accesses
  8816. * that the plane may generate whilst we perform load detection.
  8817. * We can not rely on the fbcon either being present (we get called
  8818. * during its initialisation to detect all boot displays, or it may
  8819. * not even exist) or that it is large enough to satisfy the
  8820. * requested mode.
  8821. */
  8822. fb = mode_fits_in_fbdev(dev, mode);
  8823. if (fb == NULL) {
  8824. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8825. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8826. } else
  8827. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8828. if (IS_ERR(fb)) {
  8829. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8830. goto fail;
  8831. }
  8832. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8833. if (ret)
  8834. goto fail;
  8835. drm_framebuffer_unreference(fb);
  8836. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8837. if (ret)
  8838. goto fail;
  8839. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8840. if (!ret)
  8841. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8842. if (!ret)
  8843. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8844. if (ret) {
  8845. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8846. goto fail;
  8847. }
  8848. ret = drm_atomic_commit(state);
  8849. if (ret) {
  8850. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8851. goto fail;
  8852. }
  8853. old->restore_state = restore_state;
  8854. /* let the connector get through one full cycle before testing */
  8855. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8856. return true;
  8857. fail:
  8858. drm_atomic_state_free(state);
  8859. drm_atomic_state_free(restore_state);
  8860. restore_state = state = NULL;
  8861. if (ret == -EDEADLK) {
  8862. drm_modeset_backoff(ctx);
  8863. goto retry;
  8864. }
  8865. return false;
  8866. }
  8867. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8868. struct intel_load_detect_pipe *old,
  8869. struct drm_modeset_acquire_ctx *ctx)
  8870. {
  8871. struct intel_encoder *intel_encoder =
  8872. intel_attached_encoder(connector);
  8873. struct drm_encoder *encoder = &intel_encoder->base;
  8874. struct drm_atomic_state *state = old->restore_state;
  8875. int ret;
  8876. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8877. connector->base.id, connector->name,
  8878. encoder->base.id, encoder->name);
  8879. if (!state)
  8880. return;
  8881. ret = drm_atomic_commit(state);
  8882. if (ret) {
  8883. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8884. drm_atomic_state_free(state);
  8885. }
  8886. }
  8887. static int i9xx_pll_refclk(struct drm_device *dev,
  8888. const struct intel_crtc_state *pipe_config)
  8889. {
  8890. struct drm_i915_private *dev_priv = dev->dev_private;
  8891. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8892. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8893. return dev_priv->vbt.lvds_ssc_freq;
  8894. else if (HAS_PCH_SPLIT(dev))
  8895. return 120000;
  8896. else if (!IS_GEN2(dev))
  8897. return 96000;
  8898. else
  8899. return 48000;
  8900. }
  8901. /* Returns the clock of the currently programmed mode of the given pipe. */
  8902. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8903. struct intel_crtc_state *pipe_config)
  8904. {
  8905. struct drm_device *dev = crtc->base.dev;
  8906. struct drm_i915_private *dev_priv = dev->dev_private;
  8907. int pipe = pipe_config->cpu_transcoder;
  8908. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8909. u32 fp;
  8910. intel_clock_t clock;
  8911. int port_clock;
  8912. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8913. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8914. fp = pipe_config->dpll_hw_state.fp0;
  8915. else
  8916. fp = pipe_config->dpll_hw_state.fp1;
  8917. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8918. if (IS_PINEVIEW(dev)) {
  8919. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8920. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8921. } else {
  8922. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8923. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8924. }
  8925. if (!IS_GEN2(dev)) {
  8926. if (IS_PINEVIEW(dev))
  8927. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8928. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8929. else
  8930. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8931. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8932. switch (dpll & DPLL_MODE_MASK) {
  8933. case DPLLB_MODE_DAC_SERIAL:
  8934. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8935. 5 : 10;
  8936. break;
  8937. case DPLLB_MODE_LVDS:
  8938. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8939. 7 : 14;
  8940. break;
  8941. default:
  8942. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8943. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8944. return;
  8945. }
  8946. if (IS_PINEVIEW(dev))
  8947. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8948. else
  8949. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8950. } else {
  8951. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8952. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8953. if (is_lvds) {
  8954. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8955. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8956. if (lvds & LVDS_CLKB_POWER_UP)
  8957. clock.p2 = 7;
  8958. else
  8959. clock.p2 = 14;
  8960. } else {
  8961. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8962. clock.p1 = 2;
  8963. else {
  8964. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8965. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8966. }
  8967. if (dpll & PLL_P2_DIVIDE_BY_4)
  8968. clock.p2 = 4;
  8969. else
  8970. clock.p2 = 2;
  8971. }
  8972. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8973. }
  8974. /*
  8975. * This value includes pixel_multiplier. We will use
  8976. * port_clock to compute adjusted_mode.crtc_clock in the
  8977. * encoder's get_config() function.
  8978. */
  8979. pipe_config->port_clock = port_clock;
  8980. }
  8981. int intel_dotclock_calculate(int link_freq,
  8982. const struct intel_link_m_n *m_n)
  8983. {
  8984. /*
  8985. * The calculation for the data clock is:
  8986. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8987. * But we want to avoid losing precison if possible, so:
  8988. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8989. *
  8990. * and the link clock is simpler:
  8991. * link_clock = (m * link_clock) / n
  8992. */
  8993. if (!m_n->link_n)
  8994. return 0;
  8995. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8996. }
  8997. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8998. struct intel_crtc_state *pipe_config)
  8999. {
  9000. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9001. /* read out port_clock from the DPLL */
  9002. i9xx_crtc_clock_get(crtc, pipe_config);
  9003. /*
  9004. * In case there is an active pipe without active ports,
  9005. * we may need some idea for the dotclock anyway.
  9006. * Calculate one based on the FDI configuration.
  9007. */
  9008. pipe_config->base.adjusted_mode.crtc_clock =
  9009. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9010. &pipe_config->fdi_m_n);
  9011. }
  9012. /** Returns the currently programmed mode of the given pipe. */
  9013. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9014. struct drm_crtc *crtc)
  9015. {
  9016. struct drm_i915_private *dev_priv = dev->dev_private;
  9017. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9018. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9019. struct drm_display_mode *mode;
  9020. struct intel_crtc_state *pipe_config;
  9021. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9022. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9023. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9024. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9025. enum pipe pipe = intel_crtc->pipe;
  9026. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9027. if (!mode)
  9028. return NULL;
  9029. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9030. if (!pipe_config) {
  9031. kfree(mode);
  9032. return NULL;
  9033. }
  9034. /*
  9035. * Construct a pipe_config sufficient for getting the clock info
  9036. * back out of crtc_clock_get.
  9037. *
  9038. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9039. * to use a real value here instead.
  9040. */
  9041. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9042. pipe_config->pixel_multiplier = 1;
  9043. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9044. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9045. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9046. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9047. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9048. mode->hdisplay = (htot & 0xffff) + 1;
  9049. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9050. mode->hsync_start = (hsync & 0xffff) + 1;
  9051. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9052. mode->vdisplay = (vtot & 0xffff) + 1;
  9053. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9054. mode->vsync_start = (vsync & 0xffff) + 1;
  9055. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9056. drm_mode_set_name(mode);
  9057. kfree(pipe_config);
  9058. return mode;
  9059. }
  9060. void intel_mark_busy(struct drm_device *dev)
  9061. {
  9062. struct drm_i915_private *dev_priv = dev->dev_private;
  9063. if (dev_priv->mm.busy)
  9064. return;
  9065. intel_runtime_pm_get(dev_priv);
  9066. i915_update_gfx_val(dev_priv);
  9067. if (INTEL_INFO(dev)->gen >= 6)
  9068. gen6_rps_busy(dev_priv);
  9069. dev_priv->mm.busy = true;
  9070. }
  9071. void intel_mark_idle(struct drm_device *dev)
  9072. {
  9073. struct drm_i915_private *dev_priv = dev->dev_private;
  9074. if (!dev_priv->mm.busy)
  9075. return;
  9076. dev_priv->mm.busy = false;
  9077. if (INTEL_INFO(dev)->gen >= 6)
  9078. gen6_rps_idle(dev->dev_private);
  9079. intel_runtime_pm_put(dev_priv);
  9080. }
  9081. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9082. {
  9083. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9084. struct drm_device *dev = crtc->dev;
  9085. struct intel_unpin_work *work;
  9086. spin_lock_irq(&dev->event_lock);
  9087. work = intel_crtc->unpin_work;
  9088. intel_crtc->unpin_work = NULL;
  9089. spin_unlock_irq(&dev->event_lock);
  9090. if (work) {
  9091. cancel_work_sync(&work->work);
  9092. kfree(work);
  9093. }
  9094. drm_crtc_cleanup(crtc);
  9095. kfree(intel_crtc);
  9096. }
  9097. static void intel_unpin_work_fn(struct work_struct *__work)
  9098. {
  9099. struct intel_unpin_work *work =
  9100. container_of(__work, struct intel_unpin_work, work);
  9101. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9102. struct drm_device *dev = crtc->base.dev;
  9103. struct drm_plane *primary = crtc->base.primary;
  9104. mutex_lock(&dev->struct_mutex);
  9105. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9106. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9107. if (work->flip_queued_req)
  9108. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9109. mutex_unlock(&dev->struct_mutex);
  9110. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9111. intel_fbc_post_update(crtc);
  9112. drm_framebuffer_unreference(work->old_fb);
  9113. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9114. atomic_dec(&crtc->unpin_work_count);
  9115. kfree(work);
  9116. }
  9117. static void do_intel_finish_page_flip(struct drm_device *dev,
  9118. struct drm_crtc *crtc)
  9119. {
  9120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9121. struct intel_unpin_work *work;
  9122. unsigned long flags;
  9123. /* Ignore early vblank irqs */
  9124. if (intel_crtc == NULL)
  9125. return;
  9126. /*
  9127. * This is called both by irq handlers and the reset code (to complete
  9128. * lost pageflips) so needs the full irqsave spinlocks.
  9129. */
  9130. spin_lock_irqsave(&dev->event_lock, flags);
  9131. work = intel_crtc->unpin_work;
  9132. /* Ensure we don't miss a work->pending update ... */
  9133. smp_rmb();
  9134. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9135. spin_unlock_irqrestore(&dev->event_lock, flags);
  9136. return;
  9137. }
  9138. page_flip_completed(intel_crtc);
  9139. spin_unlock_irqrestore(&dev->event_lock, flags);
  9140. }
  9141. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9142. {
  9143. struct drm_i915_private *dev_priv = dev->dev_private;
  9144. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9145. do_intel_finish_page_flip(dev, crtc);
  9146. }
  9147. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9148. {
  9149. struct drm_i915_private *dev_priv = dev->dev_private;
  9150. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9151. do_intel_finish_page_flip(dev, crtc);
  9152. }
  9153. /* Is 'a' after or equal to 'b'? */
  9154. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9155. {
  9156. return !((a - b) & 0x80000000);
  9157. }
  9158. static bool page_flip_finished(struct intel_crtc *crtc)
  9159. {
  9160. struct drm_device *dev = crtc->base.dev;
  9161. struct drm_i915_private *dev_priv = dev->dev_private;
  9162. unsigned reset_counter;
  9163. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9164. if (crtc->reset_counter != reset_counter)
  9165. return true;
  9166. /*
  9167. * The relevant registers doen't exist on pre-ctg.
  9168. * As the flip done interrupt doesn't trigger for mmio
  9169. * flips on gmch platforms, a flip count check isn't
  9170. * really needed there. But since ctg has the registers,
  9171. * include it in the check anyway.
  9172. */
  9173. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9174. return true;
  9175. /*
  9176. * BDW signals flip done immediately if the plane
  9177. * is disabled, even if the plane enable is already
  9178. * armed to occur at the next vblank :(
  9179. */
  9180. /*
  9181. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9182. * used the same base address. In that case the mmio flip might
  9183. * have completed, but the CS hasn't even executed the flip yet.
  9184. *
  9185. * A flip count check isn't enough as the CS might have updated
  9186. * the base address just after start of vblank, but before we
  9187. * managed to process the interrupt. This means we'd complete the
  9188. * CS flip too soon.
  9189. *
  9190. * Combining both checks should get us a good enough result. It may
  9191. * still happen that the CS flip has been executed, but has not
  9192. * yet actually completed. But in case the base address is the same
  9193. * anyway, we don't really care.
  9194. */
  9195. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9196. crtc->unpin_work->gtt_offset &&
  9197. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9198. crtc->unpin_work->flip_count);
  9199. }
  9200. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9201. {
  9202. struct drm_i915_private *dev_priv = dev->dev_private;
  9203. struct intel_crtc *intel_crtc =
  9204. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9205. unsigned long flags;
  9206. /*
  9207. * This is called both by irq handlers and the reset code (to complete
  9208. * lost pageflips) so needs the full irqsave spinlocks.
  9209. *
  9210. * NB: An MMIO update of the plane base pointer will also
  9211. * generate a page-flip completion irq, i.e. every modeset
  9212. * is also accompanied by a spurious intel_prepare_page_flip().
  9213. */
  9214. spin_lock_irqsave(&dev->event_lock, flags);
  9215. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9216. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9217. spin_unlock_irqrestore(&dev->event_lock, flags);
  9218. }
  9219. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9220. {
  9221. /* Ensure that the work item is consistent when activating it ... */
  9222. smp_wmb();
  9223. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9224. /* and that it is marked active as soon as the irq could fire. */
  9225. smp_wmb();
  9226. }
  9227. static int intel_gen2_queue_flip(struct drm_device *dev,
  9228. struct drm_crtc *crtc,
  9229. struct drm_framebuffer *fb,
  9230. struct drm_i915_gem_object *obj,
  9231. struct drm_i915_gem_request *req,
  9232. uint32_t flags)
  9233. {
  9234. struct intel_engine_cs *engine = req->engine;
  9235. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9236. u32 flip_mask;
  9237. int ret;
  9238. ret = intel_ring_begin(req, 6);
  9239. if (ret)
  9240. return ret;
  9241. /* Can't queue multiple flips, so wait for the previous
  9242. * one to finish before executing the next.
  9243. */
  9244. if (intel_crtc->plane)
  9245. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9246. else
  9247. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9248. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9249. intel_ring_emit(engine, MI_NOOP);
  9250. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9251. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9252. intel_ring_emit(engine, fb->pitches[0]);
  9253. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9254. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9255. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9256. return 0;
  9257. }
  9258. static int intel_gen3_queue_flip(struct drm_device *dev,
  9259. struct drm_crtc *crtc,
  9260. struct drm_framebuffer *fb,
  9261. struct drm_i915_gem_object *obj,
  9262. struct drm_i915_gem_request *req,
  9263. uint32_t flags)
  9264. {
  9265. struct intel_engine_cs *engine = req->engine;
  9266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9267. u32 flip_mask;
  9268. int ret;
  9269. ret = intel_ring_begin(req, 6);
  9270. if (ret)
  9271. return ret;
  9272. if (intel_crtc->plane)
  9273. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9274. else
  9275. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9276. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9277. intel_ring_emit(engine, MI_NOOP);
  9278. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9279. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9280. intel_ring_emit(engine, fb->pitches[0]);
  9281. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9282. intel_ring_emit(engine, MI_NOOP);
  9283. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9284. return 0;
  9285. }
  9286. static int intel_gen4_queue_flip(struct drm_device *dev,
  9287. struct drm_crtc *crtc,
  9288. struct drm_framebuffer *fb,
  9289. struct drm_i915_gem_object *obj,
  9290. struct drm_i915_gem_request *req,
  9291. uint32_t flags)
  9292. {
  9293. struct intel_engine_cs *engine = req->engine;
  9294. struct drm_i915_private *dev_priv = dev->dev_private;
  9295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9296. uint32_t pf, pipesrc;
  9297. int ret;
  9298. ret = intel_ring_begin(req, 4);
  9299. if (ret)
  9300. return ret;
  9301. /* i965+ uses the linear or tiled offsets from the
  9302. * Display Registers (which do not change across a page-flip)
  9303. * so we need only reprogram the base address.
  9304. */
  9305. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9306. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9307. intel_ring_emit(engine, fb->pitches[0]);
  9308. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
  9309. obj->tiling_mode);
  9310. /* XXX Enabling the panel-fitter across page-flip is so far
  9311. * untested on non-native modes, so ignore it for now.
  9312. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9313. */
  9314. pf = 0;
  9315. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9316. intel_ring_emit(engine, pf | pipesrc);
  9317. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9318. return 0;
  9319. }
  9320. static int intel_gen6_queue_flip(struct drm_device *dev,
  9321. struct drm_crtc *crtc,
  9322. struct drm_framebuffer *fb,
  9323. struct drm_i915_gem_object *obj,
  9324. struct drm_i915_gem_request *req,
  9325. uint32_t flags)
  9326. {
  9327. struct intel_engine_cs *engine = req->engine;
  9328. struct drm_i915_private *dev_priv = dev->dev_private;
  9329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9330. uint32_t pf, pipesrc;
  9331. int ret;
  9332. ret = intel_ring_begin(req, 4);
  9333. if (ret)
  9334. return ret;
  9335. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9336. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9337. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9338. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9339. /* Contrary to the suggestions in the documentation,
  9340. * "Enable Panel Fitter" does not seem to be required when page
  9341. * flipping with a non-native mode, and worse causes a normal
  9342. * modeset to fail.
  9343. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9344. */
  9345. pf = 0;
  9346. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9347. intel_ring_emit(engine, pf | pipesrc);
  9348. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9349. return 0;
  9350. }
  9351. static int intel_gen7_queue_flip(struct drm_device *dev,
  9352. struct drm_crtc *crtc,
  9353. struct drm_framebuffer *fb,
  9354. struct drm_i915_gem_object *obj,
  9355. struct drm_i915_gem_request *req,
  9356. uint32_t flags)
  9357. {
  9358. struct intel_engine_cs *engine = req->engine;
  9359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9360. uint32_t plane_bit = 0;
  9361. int len, ret;
  9362. switch (intel_crtc->plane) {
  9363. case PLANE_A:
  9364. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9365. break;
  9366. case PLANE_B:
  9367. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9368. break;
  9369. case PLANE_C:
  9370. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9371. break;
  9372. default:
  9373. WARN_ONCE(1, "unknown plane in flip command\n");
  9374. return -ENODEV;
  9375. }
  9376. len = 4;
  9377. if (engine->id == RCS) {
  9378. len += 6;
  9379. /*
  9380. * On Gen 8, SRM is now taking an extra dword to accommodate
  9381. * 48bits addresses, and we need a NOOP for the batch size to
  9382. * stay even.
  9383. */
  9384. if (IS_GEN8(dev))
  9385. len += 2;
  9386. }
  9387. /*
  9388. * BSpec MI_DISPLAY_FLIP for IVB:
  9389. * "The full packet must be contained within the same cache line."
  9390. *
  9391. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9392. * cacheline, if we ever start emitting more commands before
  9393. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9394. * then do the cacheline alignment, and finally emit the
  9395. * MI_DISPLAY_FLIP.
  9396. */
  9397. ret = intel_ring_cacheline_align(req);
  9398. if (ret)
  9399. return ret;
  9400. ret = intel_ring_begin(req, len);
  9401. if (ret)
  9402. return ret;
  9403. /* Unmask the flip-done completion message. Note that the bspec says that
  9404. * we should do this for both the BCS and RCS, and that we must not unmask
  9405. * more than one flip event at any time (or ensure that one flip message
  9406. * can be sent by waiting for flip-done prior to queueing new flips).
  9407. * Experimentation says that BCS works despite DERRMR masking all
  9408. * flip-done completion events and that unmasking all planes at once
  9409. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9410. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9411. */
  9412. if (engine->id == RCS) {
  9413. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9414. intel_ring_emit_reg(engine, DERRMR);
  9415. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9416. DERRMR_PIPEB_PRI_FLIP_DONE |
  9417. DERRMR_PIPEC_PRI_FLIP_DONE));
  9418. if (IS_GEN8(dev))
  9419. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9420. MI_SRM_LRM_GLOBAL_GTT);
  9421. else
  9422. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9423. MI_SRM_LRM_GLOBAL_GTT);
  9424. intel_ring_emit_reg(engine, DERRMR);
  9425. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9426. if (IS_GEN8(dev)) {
  9427. intel_ring_emit(engine, 0);
  9428. intel_ring_emit(engine, MI_NOOP);
  9429. }
  9430. }
  9431. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9432. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9433. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9434. intel_ring_emit(engine, (MI_NOOP));
  9435. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9436. return 0;
  9437. }
  9438. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9439. struct drm_i915_gem_object *obj)
  9440. {
  9441. /*
  9442. * This is not being used for older platforms, because
  9443. * non-availability of flip done interrupt forces us to use
  9444. * CS flips. Older platforms derive flip done using some clever
  9445. * tricks involving the flip_pending status bits and vblank irqs.
  9446. * So using MMIO flips there would disrupt this mechanism.
  9447. */
  9448. if (engine == NULL)
  9449. return true;
  9450. if (INTEL_INFO(engine->dev)->gen < 5)
  9451. return false;
  9452. if (i915.use_mmio_flip < 0)
  9453. return false;
  9454. else if (i915.use_mmio_flip > 0)
  9455. return true;
  9456. else if (i915.enable_execlists)
  9457. return true;
  9458. else if (obj->base.dma_buf &&
  9459. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9460. false))
  9461. return true;
  9462. else
  9463. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9464. }
  9465. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9466. unsigned int rotation,
  9467. struct intel_unpin_work *work)
  9468. {
  9469. struct drm_device *dev = intel_crtc->base.dev;
  9470. struct drm_i915_private *dev_priv = dev->dev_private;
  9471. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9472. const enum pipe pipe = intel_crtc->pipe;
  9473. u32 ctl, stride, tile_height;
  9474. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9475. ctl &= ~PLANE_CTL_TILED_MASK;
  9476. switch (fb->modifier[0]) {
  9477. case DRM_FORMAT_MOD_NONE:
  9478. break;
  9479. case I915_FORMAT_MOD_X_TILED:
  9480. ctl |= PLANE_CTL_TILED_X;
  9481. break;
  9482. case I915_FORMAT_MOD_Y_TILED:
  9483. ctl |= PLANE_CTL_TILED_Y;
  9484. break;
  9485. case I915_FORMAT_MOD_Yf_TILED:
  9486. ctl |= PLANE_CTL_TILED_YF;
  9487. break;
  9488. default:
  9489. MISSING_CASE(fb->modifier[0]);
  9490. }
  9491. /*
  9492. * The stride is either expressed as a multiple of 64 bytes chunks for
  9493. * linear buffers or in number of tiles for tiled buffers.
  9494. */
  9495. if (intel_rotation_90_or_270(rotation)) {
  9496. /* stride = Surface height in tiles */
  9497. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9498. stride = DIV_ROUND_UP(fb->height, tile_height);
  9499. } else {
  9500. stride = fb->pitches[0] /
  9501. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9502. fb->pixel_format);
  9503. }
  9504. /*
  9505. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9506. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9507. */
  9508. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9509. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9510. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9511. POSTING_READ(PLANE_SURF(pipe, 0));
  9512. }
  9513. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9514. struct intel_unpin_work *work)
  9515. {
  9516. struct drm_device *dev = intel_crtc->base.dev;
  9517. struct drm_i915_private *dev_priv = dev->dev_private;
  9518. struct intel_framebuffer *intel_fb =
  9519. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9520. struct drm_i915_gem_object *obj = intel_fb->obj;
  9521. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9522. u32 dspcntr;
  9523. dspcntr = I915_READ(reg);
  9524. if (obj->tiling_mode != I915_TILING_NONE)
  9525. dspcntr |= DISPPLANE_TILED;
  9526. else
  9527. dspcntr &= ~DISPPLANE_TILED;
  9528. I915_WRITE(reg, dspcntr);
  9529. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9530. POSTING_READ(DSPSURF(intel_crtc->plane));
  9531. }
  9532. /*
  9533. * XXX: This is the temporary way to update the plane registers until we get
  9534. * around to using the usual plane update functions for MMIO flips
  9535. */
  9536. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9537. {
  9538. struct intel_crtc *crtc = mmio_flip->crtc;
  9539. struct intel_unpin_work *work;
  9540. spin_lock_irq(&crtc->base.dev->event_lock);
  9541. work = crtc->unpin_work;
  9542. spin_unlock_irq(&crtc->base.dev->event_lock);
  9543. if (work == NULL)
  9544. return;
  9545. intel_mark_page_flip_active(work);
  9546. intel_pipe_update_start(crtc);
  9547. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9548. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9549. else
  9550. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9551. ilk_do_mmio_flip(crtc, work);
  9552. intel_pipe_update_end(crtc);
  9553. }
  9554. static void intel_mmio_flip_work_func(struct work_struct *work)
  9555. {
  9556. struct intel_mmio_flip *mmio_flip =
  9557. container_of(work, struct intel_mmio_flip, work);
  9558. struct intel_framebuffer *intel_fb =
  9559. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9560. struct drm_i915_gem_object *obj = intel_fb->obj;
  9561. if (mmio_flip->req) {
  9562. WARN_ON(__i915_wait_request(mmio_flip->req,
  9563. false, NULL,
  9564. &mmio_flip->i915->rps.mmioflips));
  9565. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9566. }
  9567. /* For framebuffer backed by dmabuf, wait for fence */
  9568. if (obj->base.dma_buf)
  9569. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9570. false, false,
  9571. MAX_SCHEDULE_TIMEOUT) < 0);
  9572. intel_do_mmio_flip(mmio_flip);
  9573. kfree(mmio_flip);
  9574. }
  9575. static int intel_queue_mmio_flip(struct drm_device *dev,
  9576. struct drm_crtc *crtc,
  9577. struct drm_i915_gem_object *obj)
  9578. {
  9579. struct intel_mmio_flip *mmio_flip;
  9580. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9581. if (mmio_flip == NULL)
  9582. return -ENOMEM;
  9583. mmio_flip->i915 = to_i915(dev);
  9584. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9585. mmio_flip->crtc = to_intel_crtc(crtc);
  9586. mmio_flip->rotation = crtc->primary->state->rotation;
  9587. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9588. schedule_work(&mmio_flip->work);
  9589. return 0;
  9590. }
  9591. static int intel_default_queue_flip(struct drm_device *dev,
  9592. struct drm_crtc *crtc,
  9593. struct drm_framebuffer *fb,
  9594. struct drm_i915_gem_object *obj,
  9595. struct drm_i915_gem_request *req,
  9596. uint32_t flags)
  9597. {
  9598. return -ENODEV;
  9599. }
  9600. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9601. struct drm_crtc *crtc)
  9602. {
  9603. struct drm_i915_private *dev_priv = dev->dev_private;
  9604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9605. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9606. u32 addr;
  9607. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9608. return true;
  9609. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9610. return false;
  9611. if (!work->enable_stall_check)
  9612. return false;
  9613. if (work->flip_ready_vblank == 0) {
  9614. if (work->flip_queued_req &&
  9615. !i915_gem_request_completed(work->flip_queued_req, true))
  9616. return false;
  9617. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9618. }
  9619. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9620. return false;
  9621. /* Potential stall - if we see that the flip has happened,
  9622. * assume a missed interrupt. */
  9623. if (INTEL_INFO(dev)->gen >= 4)
  9624. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9625. else
  9626. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9627. /* There is a potential issue here with a false positive after a flip
  9628. * to the same address. We could address this by checking for a
  9629. * non-incrementing frame counter.
  9630. */
  9631. return addr == work->gtt_offset;
  9632. }
  9633. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9634. {
  9635. struct drm_i915_private *dev_priv = dev->dev_private;
  9636. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9637. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9638. struct intel_unpin_work *work;
  9639. WARN_ON(!in_interrupt());
  9640. if (crtc == NULL)
  9641. return;
  9642. spin_lock(&dev->event_lock);
  9643. work = intel_crtc->unpin_work;
  9644. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9645. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9646. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9647. page_flip_completed(intel_crtc);
  9648. work = NULL;
  9649. }
  9650. if (work != NULL &&
  9651. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9652. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9653. spin_unlock(&dev->event_lock);
  9654. }
  9655. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9656. struct drm_framebuffer *fb,
  9657. struct drm_pending_vblank_event *event,
  9658. uint32_t page_flip_flags)
  9659. {
  9660. struct drm_device *dev = crtc->dev;
  9661. struct drm_i915_private *dev_priv = dev->dev_private;
  9662. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9663. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9665. struct drm_plane *primary = crtc->primary;
  9666. enum pipe pipe = intel_crtc->pipe;
  9667. struct intel_unpin_work *work;
  9668. struct intel_engine_cs *engine;
  9669. bool mmio_flip;
  9670. struct drm_i915_gem_request *request = NULL;
  9671. int ret;
  9672. /*
  9673. * drm_mode_page_flip_ioctl() should already catch this, but double
  9674. * check to be safe. In the future we may enable pageflipping from
  9675. * a disabled primary plane.
  9676. */
  9677. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9678. return -EBUSY;
  9679. /* Can't change pixel format via MI display flips. */
  9680. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9681. return -EINVAL;
  9682. /*
  9683. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9684. * Note that pitch changes could also affect these register.
  9685. */
  9686. if (INTEL_INFO(dev)->gen > 3 &&
  9687. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9688. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9689. return -EINVAL;
  9690. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9691. goto out_hang;
  9692. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9693. if (work == NULL)
  9694. return -ENOMEM;
  9695. work->event = event;
  9696. work->crtc = crtc;
  9697. work->old_fb = old_fb;
  9698. INIT_WORK(&work->work, intel_unpin_work_fn);
  9699. ret = drm_crtc_vblank_get(crtc);
  9700. if (ret)
  9701. goto free_work;
  9702. /* We borrow the event spin lock for protecting unpin_work */
  9703. spin_lock_irq(&dev->event_lock);
  9704. if (intel_crtc->unpin_work) {
  9705. /* Before declaring the flip queue wedged, check if
  9706. * the hardware completed the operation behind our backs.
  9707. */
  9708. if (__intel_pageflip_stall_check(dev, crtc)) {
  9709. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9710. page_flip_completed(intel_crtc);
  9711. } else {
  9712. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9713. spin_unlock_irq(&dev->event_lock);
  9714. drm_crtc_vblank_put(crtc);
  9715. kfree(work);
  9716. return -EBUSY;
  9717. }
  9718. }
  9719. intel_crtc->unpin_work = work;
  9720. spin_unlock_irq(&dev->event_lock);
  9721. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9722. flush_workqueue(dev_priv->wq);
  9723. /* Reference the objects for the scheduled work. */
  9724. drm_framebuffer_reference(work->old_fb);
  9725. drm_gem_object_reference(&obj->base);
  9726. crtc->primary->fb = fb;
  9727. update_state_fb(crtc->primary);
  9728. intel_fbc_pre_update(intel_crtc);
  9729. work->pending_flip_obj = obj;
  9730. ret = i915_mutex_lock_interruptible(dev);
  9731. if (ret)
  9732. goto cleanup;
  9733. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9734. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9735. ret = -EIO;
  9736. goto cleanup;
  9737. }
  9738. atomic_inc(&intel_crtc->unpin_work_count);
  9739. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9740. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9741. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9742. engine = &dev_priv->engine[BCS];
  9743. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9744. /* vlv: DISPLAY_FLIP fails to change tiling */
  9745. engine = NULL;
  9746. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9747. engine = &dev_priv->engine[BCS];
  9748. } else if (INTEL_INFO(dev)->gen >= 7) {
  9749. engine = i915_gem_request_get_engine(obj->last_write_req);
  9750. if (engine == NULL || engine->id != RCS)
  9751. engine = &dev_priv->engine[BCS];
  9752. } else {
  9753. engine = &dev_priv->engine[RCS];
  9754. }
  9755. mmio_flip = use_mmio_flip(engine, obj);
  9756. /* When using CS flips, we want to emit semaphores between rings.
  9757. * However, when using mmio flips we will create a task to do the
  9758. * synchronisation, so all we want here is to pin the framebuffer
  9759. * into the display plane and skip any waits.
  9760. */
  9761. if (!mmio_flip) {
  9762. ret = i915_gem_object_sync(obj, engine, &request);
  9763. if (ret)
  9764. goto cleanup_pending;
  9765. }
  9766. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9767. if (ret)
  9768. goto cleanup_pending;
  9769. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9770. obj, 0);
  9771. work->gtt_offset += intel_crtc->dspaddr_offset;
  9772. if (mmio_flip) {
  9773. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9774. if (ret)
  9775. goto cleanup_unpin;
  9776. i915_gem_request_assign(&work->flip_queued_req,
  9777. obj->last_write_req);
  9778. } else {
  9779. if (!request) {
  9780. request = i915_gem_request_alloc(engine, NULL);
  9781. if (IS_ERR(request)) {
  9782. ret = PTR_ERR(request);
  9783. goto cleanup_unpin;
  9784. }
  9785. }
  9786. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9787. page_flip_flags);
  9788. if (ret)
  9789. goto cleanup_unpin;
  9790. i915_gem_request_assign(&work->flip_queued_req, request);
  9791. }
  9792. if (request)
  9793. i915_add_request_no_flush(request);
  9794. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9795. work->enable_stall_check = true;
  9796. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9797. to_intel_plane(primary)->frontbuffer_bit);
  9798. mutex_unlock(&dev->struct_mutex);
  9799. intel_frontbuffer_flip_prepare(dev,
  9800. to_intel_plane(primary)->frontbuffer_bit);
  9801. trace_i915_flip_request(intel_crtc->plane, obj);
  9802. return 0;
  9803. cleanup_unpin:
  9804. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9805. cleanup_pending:
  9806. if (!IS_ERR_OR_NULL(request))
  9807. i915_add_request_no_flush(request);
  9808. atomic_dec(&intel_crtc->unpin_work_count);
  9809. mutex_unlock(&dev->struct_mutex);
  9810. cleanup:
  9811. crtc->primary->fb = old_fb;
  9812. update_state_fb(crtc->primary);
  9813. drm_gem_object_unreference_unlocked(&obj->base);
  9814. drm_framebuffer_unreference(work->old_fb);
  9815. spin_lock_irq(&dev->event_lock);
  9816. intel_crtc->unpin_work = NULL;
  9817. spin_unlock_irq(&dev->event_lock);
  9818. drm_crtc_vblank_put(crtc);
  9819. free_work:
  9820. kfree(work);
  9821. if (ret == -EIO) {
  9822. struct drm_atomic_state *state;
  9823. struct drm_plane_state *plane_state;
  9824. out_hang:
  9825. state = drm_atomic_state_alloc(dev);
  9826. if (!state)
  9827. return -ENOMEM;
  9828. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9829. retry:
  9830. plane_state = drm_atomic_get_plane_state(state, primary);
  9831. ret = PTR_ERR_OR_ZERO(plane_state);
  9832. if (!ret) {
  9833. drm_atomic_set_fb_for_plane(plane_state, fb);
  9834. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9835. if (!ret)
  9836. ret = drm_atomic_commit(state);
  9837. }
  9838. if (ret == -EDEADLK) {
  9839. drm_modeset_backoff(state->acquire_ctx);
  9840. drm_atomic_state_clear(state);
  9841. goto retry;
  9842. }
  9843. if (ret)
  9844. drm_atomic_state_free(state);
  9845. if (ret == 0 && event) {
  9846. spin_lock_irq(&dev->event_lock);
  9847. drm_crtc_send_vblank_event(crtc, event);
  9848. spin_unlock_irq(&dev->event_lock);
  9849. }
  9850. }
  9851. return ret;
  9852. }
  9853. /**
  9854. * intel_wm_need_update - Check whether watermarks need updating
  9855. * @plane: drm plane
  9856. * @state: new plane state
  9857. *
  9858. * Check current plane state versus the new one to determine whether
  9859. * watermarks need to be recalculated.
  9860. *
  9861. * Returns true or false.
  9862. */
  9863. static bool intel_wm_need_update(struct drm_plane *plane,
  9864. struct drm_plane_state *state)
  9865. {
  9866. struct intel_plane_state *new = to_intel_plane_state(state);
  9867. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9868. /* Update watermarks on tiling or size changes. */
  9869. if (new->visible != cur->visible)
  9870. return true;
  9871. if (!cur->base.fb || !new->base.fb)
  9872. return false;
  9873. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9874. cur->base.rotation != new->base.rotation ||
  9875. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9876. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9877. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9878. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9879. return true;
  9880. return false;
  9881. }
  9882. static bool needs_scaling(struct intel_plane_state *state)
  9883. {
  9884. int src_w = drm_rect_width(&state->src) >> 16;
  9885. int src_h = drm_rect_height(&state->src) >> 16;
  9886. int dst_w = drm_rect_width(&state->dst);
  9887. int dst_h = drm_rect_height(&state->dst);
  9888. return (src_w != dst_w || src_h != dst_h);
  9889. }
  9890. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9891. struct drm_plane_state *plane_state)
  9892. {
  9893. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9894. struct drm_crtc *crtc = crtc_state->crtc;
  9895. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9896. struct drm_plane *plane = plane_state->plane;
  9897. struct drm_device *dev = crtc->dev;
  9898. struct drm_i915_private *dev_priv = to_i915(dev);
  9899. struct intel_plane_state *old_plane_state =
  9900. to_intel_plane_state(plane->state);
  9901. int idx = intel_crtc->base.base.id, ret;
  9902. bool mode_changed = needs_modeset(crtc_state);
  9903. bool was_crtc_enabled = crtc->state->active;
  9904. bool is_crtc_enabled = crtc_state->active;
  9905. bool turn_off, turn_on, visible, was_visible;
  9906. struct drm_framebuffer *fb = plane_state->fb;
  9907. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9908. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9909. ret = skl_update_scaler_plane(
  9910. to_intel_crtc_state(crtc_state),
  9911. to_intel_plane_state(plane_state));
  9912. if (ret)
  9913. return ret;
  9914. }
  9915. was_visible = old_plane_state->visible;
  9916. visible = to_intel_plane_state(plane_state)->visible;
  9917. if (!was_crtc_enabled && WARN_ON(was_visible))
  9918. was_visible = false;
  9919. /*
  9920. * Visibility is calculated as if the crtc was on, but
  9921. * after scaler setup everything depends on it being off
  9922. * when the crtc isn't active.
  9923. */
  9924. if (!is_crtc_enabled)
  9925. to_intel_plane_state(plane_state)->visible = visible = false;
  9926. if (!was_visible && !visible)
  9927. return 0;
  9928. if (fb != old_plane_state->base.fb)
  9929. pipe_config->fb_changed = true;
  9930. turn_off = was_visible && (!visible || mode_changed);
  9931. turn_on = visible && (!was_visible || mode_changed);
  9932. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9933. plane->base.id, fb ? fb->base.id : -1);
  9934. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9935. plane->base.id, was_visible, visible,
  9936. turn_off, turn_on, mode_changed);
  9937. if (turn_on) {
  9938. pipe_config->update_wm_pre = true;
  9939. /* must disable cxsr around plane enable/disable */
  9940. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9941. pipe_config->disable_cxsr = true;
  9942. } else if (turn_off) {
  9943. pipe_config->update_wm_post = true;
  9944. /* must disable cxsr around plane enable/disable */
  9945. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9946. pipe_config->disable_cxsr = true;
  9947. } else if (intel_wm_need_update(plane, plane_state)) {
  9948. /* FIXME bollocks */
  9949. pipe_config->update_wm_pre = true;
  9950. pipe_config->update_wm_post = true;
  9951. }
  9952. /* Pre-gen9 platforms need two-step watermark updates */
  9953. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9954. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  9955. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  9956. if (visible || was_visible)
  9957. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  9958. /*
  9959. * WaCxSRDisabledForSpriteScaling:ivb
  9960. *
  9961. * cstate->update_wm was already set above, so this flag will
  9962. * take effect when we commit and program watermarks.
  9963. */
  9964. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  9965. needs_scaling(to_intel_plane_state(plane_state)) &&
  9966. !needs_scaling(old_plane_state))
  9967. pipe_config->disable_lp_wm = true;
  9968. return 0;
  9969. }
  9970. static bool encoders_cloneable(const struct intel_encoder *a,
  9971. const struct intel_encoder *b)
  9972. {
  9973. /* masks could be asymmetric, so check both ways */
  9974. return a == b || (a->cloneable & (1 << b->type) &&
  9975. b->cloneable & (1 << a->type));
  9976. }
  9977. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9978. struct intel_crtc *crtc,
  9979. struct intel_encoder *encoder)
  9980. {
  9981. struct intel_encoder *source_encoder;
  9982. struct drm_connector *connector;
  9983. struct drm_connector_state *connector_state;
  9984. int i;
  9985. for_each_connector_in_state(state, connector, connector_state, i) {
  9986. if (connector_state->crtc != &crtc->base)
  9987. continue;
  9988. source_encoder =
  9989. to_intel_encoder(connector_state->best_encoder);
  9990. if (!encoders_cloneable(encoder, source_encoder))
  9991. return false;
  9992. }
  9993. return true;
  9994. }
  9995. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9996. struct intel_crtc *crtc)
  9997. {
  9998. struct intel_encoder *encoder;
  9999. struct drm_connector *connector;
  10000. struct drm_connector_state *connector_state;
  10001. int i;
  10002. for_each_connector_in_state(state, connector, connector_state, i) {
  10003. if (connector_state->crtc != &crtc->base)
  10004. continue;
  10005. encoder = to_intel_encoder(connector_state->best_encoder);
  10006. if (!check_single_encoder_cloning(state, crtc, encoder))
  10007. return false;
  10008. }
  10009. return true;
  10010. }
  10011. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10012. struct drm_crtc_state *crtc_state)
  10013. {
  10014. struct drm_device *dev = crtc->dev;
  10015. struct drm_i915_private *dev_priv = dev->dev_private;
  10016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10017. struct intel_crtc_state *pipe_config =
  10018. to_intel_crtc_state(crtc_state);
  10019. struct drm_atomic_state *state = crtc_state->state;
  10020. int ret;
  10021. bool mode_changed = needs_modeset(crtc_state);
  10022. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10023. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10024. return -EINVAL;
  10025. }
  10026. if (mode_changed && !crtc_state->active)
  10027. pipe_config->update_wm_post = true;
  10028. if (mode_changed && crtc_state->enable &&
  10029. dev_priv->display.crtc_compute_clock &&
  10030. !WARN_ON(pipe_config->shared_dpll)) {
  10031. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10032. pipe_config);
  10033. if (ret)
  10034. return ret;
  10035. }
  10036. if (crtc_state->color_mgmt_changed) {
  10037. ret = intel_color_check(crtc, crtc_state);
  10038. if (ret)
  10039. return ret;
  10040. }
  10041. ret = 0;
  10042. if (dev_priv->display.compute_pipe_wm) {
  10043. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10044. if (ret) {
  10045. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10046. return ret;
  10047. }
  10048. }
  10049. if (dev_priv->display.compute_intermediate_wm &&
  10050. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10051. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10052. return 0;
  10053. /*
  10054. * Calculate 'intermediate' watermarks that satisfy both the
  10055. * old state and the new state. We can program these
  10056. * immediately.
  10057. */
  10058. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10059. intel_crtc,
  10060. pipe_config);
  10061. if (ret) {
  10062. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10063. return ret;
  10064. }
  10065. } else if (dev_priv->display.compute_intermediate_wm) {
  10066. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10067. pipe_config->wm.intermediate = pipe_config->wm.optimal.ilk;
  10068. }
  10069. if (INTEL_INFO(dev)->gen >= 9) {
  10070. if (mode_changed)
  10071. ret = skl_update_scaler_crtc(pipe_config);
  10072. if (!ret)
  10073. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10074. pipe_config);
  10075. }
  10076. return ret;
  10077. }
  10078. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10079. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10080. .atomic_begin = intel_begin_crtc_commit,
  10081. .atomic_flush = intel_finish_crtc_commit,
  10082. .atomic_check = intel_crtc_atomic_check,
  10083. };
  10084. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10085. {
  10086. struct intel_connector *connector;
  10087. for_each_intel_connector(dev, connector) {
  10088. if (connector->base.state->crtc)
  10089. drm_connector_unreference(&connector->base);
  10090. if (connector->base.encoder) {
  10091. connector->base.state->best_encoder =
  10092. connector->base.encoder;
  10093. connector->base.state->crtc =
  10094. connector->base.encoder->crtc;
  10095. drm_connector_reference(&connector->base);
  10096. } else {
  10097. connector->base.state->best_encoder = NULL;
  10098. connector->base.state->crtc = NULL;
  10099. }
  10100. }
  10101. }
  10102. static void
  10103. connected_sink_compute_bpp(struct intel_connector *connector,
  10104. struct intel_crtc_state *pipe_config)
  10105. {
  10106. int bpp = pipe_config->pipe_bpp;
  10107. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10108. connector->base.base.id,
  10109. connector->base.name);
  10110. /* Don't use an invalid EDID bpc value */
  10111. if (connector->base.display_info.bpc &&
  10112. connector->base.display_info.bpc * 3 < bpp) {
  10113. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10114. bpp, connector->base.display_info.bpc*3);
  10115. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10116. }
  10117. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10118. if (connector->base.display_info.bpc == 0) {
  10119. int type = connector->base.connector_type;
  10120. int clamp_bpp = 24;
  10121. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10122. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10123. type == DRM_MODE_CONNECTOR_eDP)
  10124. clamp_bpp = 18;
  10125. if (bpp > clamp_bpp) {
  10126. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10127. bpp, clamp_bpp);
  10128. pipe_config->pipe_bpp = clamp_bpp;
  10129. }
  10130. }
  10131. }
  10132. static int
  10133. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10134. struct intel_crtc_state *pipe_config)
  10135. {
  10136. struct drm_device *dev = crtc->base.dev;
  10137. struct drm_atomic_state *state;
  10138. struct drm_connector *connector;
  10139. struct drm_connector_state *connector_state;
  10140. int bpp, i;
  10141. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10142. bpp = 10*3;
  10143. else if (INTEL_INFO(dev)->gen >= 5)
  10144. bpp = 12*3;
  10145. else
  10146. bpp = 8*3;
  10147. pipe_config->pipe_bpp = bpp;
  10148. state = pipe_config->base.state;
  10149. /* Clamp display bpp to EDID value */
  10150. for_each_connector_in_state(state, connector, connector_state, i) {
  10151. if (connector_state->crtc != &crtc->base)
  10152. continue;
  10153. connected_sink_compute_bpp(to_intel_connector(connector),
  10154. pipe_config);
  10155. }
  10156. return bpp;
  10157. }
  10158. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10159. {
  10160. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10161. "type: 0x%x flags: 0x%x\n",
  10162. mode->crtc_clock,
  10163. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10164. mode->crtc_hsync_end, mode->crtc_htotal,
  10165. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10166. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10167. }
  10168. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10169. struct intel_crtc_state *pipe_config,
  10170. const char *context)
  10171. {
  10172. struct drm_device *dev = crtc->base.dev;
  10173. struct drm_plane *plane;
  10174. struct intel_plane *intel_plane;
  10175. struct intel_plane_state *state;
  10176. struct drm_framebuffer *fb;
  10177. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10178. context, pipe_config, pipe_name(crtc->pipe));
  10179. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10180. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10181. pipe_config->pipe_bpp, pipe_config->dither);
  10182. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10183. pipe_config->has_pch_encoder,
  10184. pipe_config->fdi_lanes,
  10185. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10186. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10187. pipe_config->fdi_m_n.tu);
  10188. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10189. pipe_config->has_dp_encoder,
  10190. pipe_config->lane_count,
  10191. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10192. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10193. pipe_config->dp_m_n.tu);
  10194. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10195. pipe_config->has_dp_encoder,
  10196. pipe_config->lane_count,
  10197. pipe_config->dp_m2_n2.gmch_m,
  10198. pipe_config->dp_m2_n2.gmch_n,
  10199. pipe_config->dp_m2_n2.link_m,
  10200. pipe_config->dp_m2_n2.link_n,
  10201. pipe_config->dp_m2_n2.tu);
  10202. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10203. pipe_config->has_audio,
  10204. pipe_config->has_infoframe);
  10205. DRM_DEBUG_KMS("requested mode:\n");
  10206. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10207. DRM_DEBUG_KMS("adjusted mode:\n");
  10208. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10209. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10210. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10211. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10212. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10213. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10214. crtc->num_scalers,
  10215. pipe_config->scaler_state.scaler_users,
  10216. pipe_config->scaler_state.scaler_id);
  10217. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10218. pipe_config->gmch_pfit.control,
  10219. pipe_config->gmch_pfit.pgm_ratios,
  10220. pipe_config->gmch_pfit.lvds_border_bits);
  10221. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10222. pipe_config->pch_pfit.pos,
  10223. pipe_config->pch_pfit.size,
  10224. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10225. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10226. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10227. if (IS_BROXTON(dev)) {
  10228. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10229. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10230. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10231. pipe_config->ddi_pll_sel,
  10232. pipe_config->dpll_hw_state.ebb0,
  10233. pipe_config->dpll_hw_state.ebb4,
  10234. pipe_config->dpll_hw_state.pll0,
  10235. pipe_config->dpll_hw_state.pll1,
  10236. pipe_config->dpll_hw_state.pll2,
  10237. pipe_config->dpll_hw_state.pll3,
  10238. pipe_config->dpll_hw_state.pll6,
  10239. pipe_config->dpll_hw_state.pll8,
  10240. pipe_config->dpll_hw_state.pll9,
  10241. pipe_config->dpll_hw_state.pll10,
  10242. pipe_config->dpll_hw_state.pcsdw12);
  10243. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10244. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10245. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10246. pipe_config->ddi_pll_sel,
  10247. pipe_config->dpll_hw_state.ctrl1,
  10248. pipe_config->dpll_hw_state.cfgcr1,
  10249. pipe_config->dpll_hw_state.cfgcr2);
  10250. } else if (HAS_DDI(dev)) {
  10251. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10252. pipe_config->ddi_pll_sel,
  10253. pipe_config->dpll_hw_state.wrpll,
  10254. pipe_config->dpll_hw_state.spll);
  10255. } else {
  10256. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10257. "fp0: 0x%x, fp1: 0x%x\n",
  10258. pipe_config->dpll_hw_state.dpll,
  10259. pipe_config->dpll_hw_state.dpll_md,
  10260. pipe_config->dpll_hw_state.fp0,
  10261. pipe_config->dpll_hw_state.fp1);
  10262. }
  10263. DRM_DEBUG_KMS("planes on this crtc\n");
  10264. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10265. intel_plane = to_intel_plane(plane);
  10266. if (intel_plane->pipe != crtc->pipe)
  10267. continue;
  10268. state = to_intel_plane_state(plane->state);
  10269. fb = state->base.fb;
  10270. if (!fb) {
  10271. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10272. "disabled, scaler_id = %d\n",
  10273. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10274. plane->base.id, intel_plane->pipe,
  10275. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10276. drm_plane_index(plane), state->scaler_id);
  10277. continue;
  10278. }
  10279. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10280. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10281. plane->base.id, intel_plane->pipe,
  10282. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10283. drm_plane_index(plane));
  10284. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10285. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10286. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10287. state->scaler_id,
  10288. state->src.x1 >> 16, state->src.y1 >> 16,
  10289. drm_rect_width(&state->src) >> 16,
  10290. drm_rect_height(&state->src) >> 16,
  10291. state->dst.x1, state->dst.y1,
  10292. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10293. }
  10294. }
  10295. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10296. {
  10297. struct drm_device *dev = state->dev;
  10298. struct drm_connector *connector;
  10299. unsigned int used_ports = 0;
  10300. /*
  10301. * Walk the connector list instead of the encoder
  10302. * list to detect the problem on ddi platforms
  10303. * where there's just one encoder per digital port.
  10304. */
  10305. drm_for_each_connector(connector, dev) {
  10306. struct drm_connector_state *connector_state;
  10307. struct intel_encoder *encoder;
  10308. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10309. if (!connector_state)
  10310. connector_state = connector->state;
  10311. if (!connector_state->best_encoder)
  10312. continue;
  10313. encoder = to_intel_encoder(connector_state->best_encoder);
  10314. WARN_ON(!connector_state->crtc);
  10315. switch (encoder->type) {
  10316. unsigned int port_mask;
  10317. case INTEL_OUTPUT_UNKNOWN:
  10318. if (WARN_ON(!HAS_DDI(dev)))
  10319. break;
  10320. case INTEL_OUTPUT_DISPLAYPORT:
  10321. case INTEL_OUTPUT_HDMI:
  10322. case INTEL_OUTPUT_EDP:
  10323. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10324. /* the same port mustn't appear more than once */
  10325. if (used_ports & port_mask)
  10326. return false;
  10327. used_ports |= port_mask;
  10328. default:
  10329. break;
  10330. }
  10331. }
  10332. return true;
  10333. }
  10334. static void
  10335. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10336. {
  10337. struct drm_crtc_state tmp_state;
  10338. struct intel_crtc_scaler_state scaler_state;
  10339. struct intel_dpll_hw_state dpll_hw_state;
  10340. struct intel_shared_dpll *shared_dpll;
  10341. uint32_t ddi_pll_sel;
  10342. bool force_thru;
  10343. /* FIXME: before the switch to atomic started, a new pipe_config was
  10344. * kzalloc'd. Code that depends on any field being zero should be
  10345. * fixed, so that the crtc_state can be safely duplicated. For now,
  10346. * only fields that are know to not cause problems are preserved. */
  10347. tmp_state = crtc_state->base;
  10348. scaler_state = crtc_state->scaler_state;
  10349. shared_dpll = crtc_state->shared_dpll;
  10350. dpll_hw_state = crtc_state->dpll_hw_state;
  10351. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10352. force_thru = crtc_state->pch_pfit.force_thru;
  10353. memset(crtc_state, 0, sizeof *crtc_state);
  10354. crtc_state->base = tmp_state;
  10355. crtc_state->scaler_state = scaler_state;
  10356. crtc_state->shared_dpll = shared_dpll;
  10357. crtc_state->dpll_hw_state = dpll_hw_state;
  10358. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10359. crtc_state->pch_pfit.force_thru = force_thru;
  10360. }
  10361. static int
  10362. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10363. struct intel_crtc_state *pipe_config)
  10364. {
  10365. struct drm_atomic_state *state = pipe_config->base.state;
  10366. struct intel_encoder *encoder;
  10367. struct drm_connector *connector;
  10368. struct drm_connector_state *connector_state;
  10369. int base_bpp, ret = -EINVAL;
  10370. int i;
  10371. bool retry = true;
  10372. clear_intel_crtc_state(pipe_config);
  10373. pipe_config->cpu_transcoder =
  10374. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10375. /*
  10376. * Sanitize sync polarity flags based on requested ones. If neither
  10377. * positive or negative polarity is requested, treat this as meaning
  10378. * negative polarity.
  10379. */
  10380. if (!(pipe_config->base.adjusted_mode.flags &
  10381. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10382. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10383. if (!(pipe_config->base.adjusted_mode.flags &
  10384. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10385. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10386. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10387. pipe_config);
  10388. if (base_bpp < 0)
  10389. goto fail;
  10390. /*
  10391. * Determine the real pipe dimensions. Note that stereo modes can
  10392. * increase the actual pipe size due to the frame doubling and
  10393. * insertion of additional space for blanks between the frame. This
  10394. * is stored in the crtc timings. We use the requested mode to do this
  10395. * computation to clearly distinguish it from the adjusted mode, which
  10396. * can be changed by the connectors in the below retry loop.
  10397. */
  10398. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10399. &pipe_config->pipe_src_w,
  10400. &pipe_config->pipe_src_h);
  10401. encoder_retry:
  10402. /* Ensure the port clock defaults are reset when retrying. */
  10403. pipe_config->port_clock = 0;
  10404. pipe_config->pixel_multiplier = 1;
  10405. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10406. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10407. CRTC_STEREO_DOUBLE);
  10408. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10409. * adjust it according to limitations or connector properties, and also
  10410. * a chance to reject the mode entirely.
  10411. */
  10412. for_each_connector_in_state(state, connector, connector_state, i) {
  10413. if (connector_state->crtc != crtc)
  10414. continue;
  10415. encoder = to_intel_encoder(connector_state->best_encoder);
  10416. if (!(encoder->compute_config(encoder, pipe_config))) {
  10417. DRM_DEBUG_KMS("Encoder config failure\n");
  10418. goto fail;
  10419. }
  10420. }
  10421. /* Set default port clock if not overwritten by the encoder. Needs to be
  10422. * done afterwards in case the encoder adjusts the mode. */
  10423. if (!pipe_config->port_clock)
  10424. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10425. * pipe_config->pixel_multiplier;
  10426. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10427. if (ret < 0) {
  10428. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10429. goto fail;
  10430. }
  10431. if (ret == RETRY) {
  10432. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10433. ret = -EINVAL;
  10434. goto fail;
  10435. }
  10436. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10437. retry = false;
  10438. goto encoder_retry;
  10439. }
  10440. /* Dithering seems to not pass-through bits correctly when it should, so
  10441. * only enable it on 6bpc panels. */
  10442. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10443. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10444. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10445. fail:
  10446. return ret;
  10447. }
  10448. static void
  10449. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10450. {
  10451. struct drm_crtc *crtc;
  10452. struct drm_crtc_state *crtc_state;
  10453. int i;
  10454. /* Double check state. */
  10455. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10456. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10457. /* Update hwmode for vblank functions */
  10458. if (crtc->state->active)
  10459. crtc->hwmode = crtc->state->adjusted_mode;
  10460. else
  10461. crtc->hwmode.crtc_clock = 0;
  10462. /*
  10463. * Update legacy state to satisfy fbc code. This can
  10464. * be removed when fbc uses the atomic state.
  10465. */
  10466. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10467. struct drm_plane_state *plane_state = crtc->primary->state;
  10468. crtc->primary->fb = plane_state->fb;
  10469. crtc->x = plane_state->src_x >> 16;
  10470. crtc->y = plane_state->src_y >> 16;
  10471. }
  10472. }
  10473. }
  10474. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10475. {
  10476. int diff;
  10477. if (clock1 == clock2)
  10478. return true;
  10479. if (!clock1 || !clock2)
  10480. return false;
  10481. diff = abs(clock1 - clock2);
  10482. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10483. return true;
  10484. return false;
  10485. }
  10486. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10487. list_for_each_entry((intel_crtc), \
  10488. &(dev)->mode_config.crtc_list, \
  10489. base.head) \
  10490. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10491. static bool
  10492. intel_compare_m_n(unsigned int m, unsigned int n,
  10493. unsigned int m2, unsigned int n2,
  10494. bool exact)
  10495. {
  10496. if (m == m2 && n == n2)
  10497. return true;
  10498. if (exact || !m || !n || !m2 || !n2)
  10499. return false;
  10500. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10501. if (n > n2) {
  10502. while (n > n2) {
  10503. m2 <<= 1;
  10504. n2 <<= 1;
  10505. }
  10506. } else if (n < n2) {
  10507. while (n < n2) {
  10508. m <<= 1;
  10509. n <<= 1;
  10510. }
  10511. }
  10512. if (n != n2)
  10513. return false;
  10514. return intel_fuzzy_clock_check(m, m2);
  10515. }
  10516. static bool
  10517. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10518. struct intel_link_m_n *m2_n2,
  10519. bool adjust)
  10520. {
  10521. if (m_n->tu == m2_n2->tu &&
  10522. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10523. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10524. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10525. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10526. if (adjust)
  10527. *m2_n2 = *m_n;
  10528. return true;
  10529. }
  10530. return false;
  10531. }
  10532. static bool
  10533. intel_pipe_config_compare(struct drm_device *dev,
  10534. struct intel_crtc_state *current_config,
  10535. struct intel_crtc_state *pipe_config,
  10536. bool adjust)
  10537. {
  10538. bool ret = true;
  10539. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10540. do { \
  10541. if (!adjust) \
  10542. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10543. else \
  10544. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10545. } while (0)
  10546. #define PIPE_CONF_CHECK_X(name) \
  10547. if (current_config->name != pipe_config->name) { \
  10548. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10549. "(expected 0x%08x, found 0x%08x)\n", \
  10550. current_config->name, \
  10551. pipe_config->name); \
  10552. ret = false; \
  10553. }
  10554. #define PIPE_CONF_CHECK_I(name) \
  10555. if (current_config->name != pipe_config->name) { \
  10556. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10557. "(expected %i, found %i)\n", \
  10558. current_config->name, \
  10559. pipe_config->name); \
  10560. ret = false; \
  10561. }
  10562. #define PIPE_CONF_CHECK_P(name) \
  10563. if (current_config->name != pipe_config->name) { \
  10564. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10565. "(expected %p, found %p)\n", \
  10566. current_config->name, \
  10567. pipe_config->name); \
  10568. ret = false; \
  10569. }
  10570. #define PIPE_CONF_CHECK_M_N(name) \
  10571. if (!intel_compare_link_m_n(&current_config->name, \
  10572. &pipe_config->name,\
  10573. adjust)) { \
  10574. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10575. "(expected tu %i gmch %i/%i link %i/%i, " \
  10576. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10577. current_config->name.tu, \
  10578. current_config->name.gmch_m, \
  10579. current_config->name.gmch_n, \
  10580. current_config->name.link_m, \
  10581. current_config->name.link_n, \
  10582. pipe_config->name.tu, \
  10583. pipe_config->name.gmch_m, \
  10584. pipe_config->name.gmch_n, \
  10585. pipe_config->name.link_m, \
  10586. pipe_config->name.link_n); \
  10587. ret = false; \
  10588. }
  10589. /* This is required for BDW+ where there is only one set of registers for
  10590. * switching between high and low RR.
  10591. * This macro can be used whenever a comparison has to be made between one
  10592. * hw state and multiple sw state variables.
  10593. */
  10594. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10595. if (!intel_compare_link_m_n(&current_config->name, \
  10596. &pipe_config->name, adjust) && \
  10597. !intel_compare_link_m_n(&current_config->alt_name, \
  10598. &pipe_config->name, adjust)) { \
  10599. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10600. "(expected tu %i gmch %i/%i link %i/%i, " \
  10601. "or tu %i gmch %i/%i link %i/%i, " \
  10602. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10603. current_config->name.tu, \
  10604. current_config->name.gmch_m, \
  10605. current_config->name.gmch_n, \
  10606. current_config->name.link_m, \
  10607. current_config->name.link_n, \
  10608. current_config->alt_name.tu, \
  10609. current_config->alt_name.gmch_m, \
  10610. current_config->alt_name.gmch_n, \
  10611. current_config->alt_name.link_m, \
  10612. current_config->alt_name.link_n, \
  10613. pipe_config->name.tu, \
  10614. pipe_config->name.gmch_m, \
  10615. pipe_config->name.gmch_n, \
  10616. pipe_config->name.link_m, \
  10617. pipe_config->name.link_n); \
  10618. ret = false; \
  10619. }
  10620. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10621. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10622. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10623. "(expected %i, found %i)\n", \
  10624. current_config->name & (mask), \
  10625. pipe_config->name & (mask)); \
  10626. ret = false; \
  10627. }
  10628. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10629. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10630. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10631. "(expected %i, found %i)\n", \
  10632. current_config->name, \
  10633. pipe_config->name); \
  10634. ret = false; \
  10635. }
  10636. #define PIPE_CONF_QUIRK(quirk) \
  10637. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10638. PIPE_CONF_CHECK_I(cpu_transcoder);
  10639. PIPE_CONF_CHECK_I(has_pch_encoder);
  10640. PIPE_CONF_CHECK_I(fdi_lanes);
  10641. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10642. PIPE_CONF_CHECK_I(has_dp_encoder);
  10643. PIPE_CONF_CHECK_I(lane_count);
  10644. if (INTEL_INFO(dev)->gen < 8) {
  10645. PIPE_CONF_CHECK_M_N(dp_m_n);
  10646. if (current_config->has_drrs)
  10647. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10648. } else
  10649. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10650. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10651. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10652. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10653. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10654. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10655. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10656. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10657. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10658. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10659. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10660. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10661. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10662. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10663. PIPE_CONF_CHECK_I(pixel_multiplier);
  10664. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10665. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10666. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10667. PIPE_CONF_CHECK_I(limited_color_range);
  10668. PIPE_CONF_CHECK_I(has_infoframe);
  10669. PIPE_CONF_CHECK_I(has_audio);
  10670. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10671. DRM_MODE_FLAG_INTERLACE);
  10672. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10673. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10674. DRM_MODE_FLAG_PHSYNC);
  10675. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10676. DRM_MODE_FLAG_NHSYNC);
  10677. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10678. DRM_MODE_FLAG_PVSYNC);
  10679. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10680. DRM_MODE_FLAG_NVSYNC);
  10681. }
  10682. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10683. /* pfit ratios are autocomputed by the hw on gen4+ */
  10684. if (INTEL_INFO(dev)->gen < 4)
  10685. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10686. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10687. if (!adjust) {
  10688. PIPE_CONF_CHECK_I(pipe_src_w);
  10689. PIPE_CONF_CHECK_I(pipe_src_h);
  10690. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10691. if (current_config->pch_pfit.enabled) {
  10692. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10693. PIPE_CONF_CHECK_X(pch_pfit.size);
  10694. }
  10695. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10696. }
  10697. /* BDW+ don't expose a synchronous way to read the state */
  10698. if (IS_HASWELL(dev))
  10699. PIPE_CONF_CHECK_I(ips_enabled);
  10700. PIPE_CONF_CHECK_I(double_wide);
  10701. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10702. PIPE_CONF_CHECK_P(shared_dpll);
  10703. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10704. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10705. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10706. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10707. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10708. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10709. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10710. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10711. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10712. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10713. PIPE_CONF_CHECK_X(dsi_pll.div);
  10714. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10715. PIPE_CONF_CHECK_I(pipe_bpp);
  10716. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10717. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10718. #undef PIPE_CONF_CHECK_X
  10719. #undef PIPE_CONF_CHECK_I
  10720. #undef PIPE_CONF_CHECK_P
  10721. #undef PIPE_CONF_CHECK_FLAGS
  10722. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10723. #undef PIPE_CONF_QUIRK
  10724. #undef INTEL_ERR_OR_DBG_KMS
  10725. return ret;
  10726. }
  10727. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10728. const struct intel_crtc_state *pipe_config)
  10729. {
  10730. if (pipe_config->has_pch_encoder) {
  10731. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10732. &pipe_config->fdi_m_n);
  10733. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10734. /*
  10735. * FDI already provided one idea for the dotclock.
  10736. * Yell if the encoder disagrees.
  10737. */
  10738. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10739. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10740. fdi_dotclock, dotclock);
  10741. }
  10742. }
  10743. static void verify_wm_state(struct drm_crtc *crtc,
  10744. struct drm_crtc_state *new_state)
  10745. {
  10746. struct drm_device *dev = crtc->dev;
  10747. struct drm_i915_private *dev_priv = dev->dev_private;
  10748. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10749. struct skl_ddb_entry *hw_entry, *sw_entry;
  10750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10751. const enum pipe pipe = intel_crtc->pipe;
  10752. int plane;
  10753. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10754. return;
  10755. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10756. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10757. /* planes */
  10758. for_each_plane(dev_priv, pipe, plane) {
  10759. hw_entry = &hw_ddb.plane[pipe][plane];
  10760. sw_entry = &sw_ddb->plane[pipe][plane];
  10761. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10762. continue;
  10763. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10764. "(expected (%u,%u), found (%u,%u))\n",
  10765. pipe_name(pipe), plane + 1,
  10766. sw_entry->start, sw_entry->end,
  10767. hw_entry->start, hw_entry->end);
  10768. }
  10769. /* cursor */
  10770. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10771. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10772. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10773. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10774. "(expected (%u,%u), found (%u,%u))\n",
  10775. pipe_name(pipe),
  10776. sw_entry->start, sw_entry->end,
  10777. hw_entry->start, hw_entry->end);
  10778. }
  10779. }
  10780. static void
  10781. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10782. {
  10783. struct drm_connector *connector;
  10784. drm_for_each_connector(connector, dev) {
  10785. struct drm_encoder *encoder = connector->encoder;
  10786. struct drm_connector_state *state = connector->state;
  10787. if (state->crtc != crtc)
  10788. continue;
  10789. intel_connector_verify_state(to_intel_connector(connector));
  10790. I915_STATE_WARN(state->best_encoder != encoder,
  10791. "connector's atomic encoder doesn't match legacy encoder\n");
  10792. }
  10793. }
  10794. static void
  10795. verify_encoder_state(struct drm_device *dev)
  10796. {
  10797. struct intel_encoder *encoder;
  10798. struct intel_connector *connector;
  10799. for_each_intel_encoder(dev, encoder) {
  10800. bool enabled = false;
  10801. enum pipe pipe;
  10802. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10803. encoder->base.base.id,
  10804. encoder->base.name);
  10805. for_each_intel_connector(dev, connector) {
  10806. if (connector->base.state->best_encoder != &encoder->base)
  10807. continue;
  10808. enabled = true;
  10809. I915_STATE_WARN(connector->base.state->crtc !=
  10810. encoder->base.crtc,
  10811. "connector's crtc doesn't match encoder crtc\n");
  10812. }
  10813. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10814. "encoder's enabled state mismatch "
  10815. "(expected %i, found %i)\n",
  10816. !!encoder->base.crtc, enabled);
  10817. if (!encoder->base.crtc) {
  10818. bool active;
  10819. active = encoder->get_hw_state(encoder, &pipe);
  10820. I915_STATE_WARN(active,
  10821. "encoder detached but still enabled on pipe %c.\n",
  10822. pipe_name(pipe));
  10823. }
  10824. }
  10825. }
  10826. static void
  10827. verify_crtc_state(struct drm_crtc *crtc,
  10828. struct drm_crtc_state *old_crtc_state,
  10829. struct drm_crtc_state *new_crtc_state)
  10830. {
  10831. struct drm_device *dev = crtc->dev;
  10832. struct drm_i915_private *dev_priv = dev->dev_private;
  10833. struct intel_encoder *encoder;
  10834. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10835. struct intel_crtc_state *pipe_config, *sw_config;
  10836. struct drm_atomic_state *old_state;
  10837. bool active;
  10838. old_state = old_crtc_state->state;
  10839. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10840. pipe_config = to_intel_crtc_state(old_crtc_state);
  10841. memset(pipe_config, 0, sizeof(*pipe_config));
  10842. pipe_config->base.crtc = crtc;
  10843. pipe_config->base.state = old_state;
  10844. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  10845. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10846. /* hw state is inconsistent with the pipe quirk */
  10847. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10848. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10849. active = new_crtc_state->active;
  10850. I915_STATE_WARN(new_crtc_state->active != active,
  10851. "crtc active state doesn't match with hw state "
  10852. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10853. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10854. "transitional active state does not match atomic hw state "
  10855. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10856. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10857. enum pipe pipe;
  10858. active = encoder->get_hw_state(encoder, &pipe);
  10859. I915_STATE_WARN(active != new_crtc_state->active,
  10860. "[ENCODER:%i] active %i with crtc active %i\n",
  10861. encoder->base.base.id, active, new_crtc_state->active);
  10862. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10863. "Encoder connected to wrong pipe %c\n",
  10864. pipe_name(pipe));
  10865. if (active)
  10866. encoder->get_config(encoder, pipe_config);
  10867. }
  10868. if (!new_crtc_state->active)
  10869. return;
  10870. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10871. sw_config = to_intel_crtc_state(crtc->state);
  10872. if (!intel_pipe_config_compare(dev, sw_config,
  10873. pipe_config, false)) {
  10874. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10875. intel_dump_pipe_config(intel_crtc, pipe_config,
  10876. "[hw state]");
  10877. intel_dump_pipe_config(intel_crtc, sw_config,
  10878. "[sw state]");
  10879. }
  10880. }
  10881. static void
  10882. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10883. struct intel_shared_dpll *pll,
  10884. struct drm_crtc *crtc,
  10885. struct drm_crtc_state *new_state)
  10886. {
  10887. struct intel_dpll_hw_state dpll_hw_state;
  10888. unsigned crtc_mask;
  10889. bool active;
  10890. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10891. DRM_DEBUG_KMS("%s\n", pll->name);
  10892. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10893. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10894. I915_STATE_WARN(!pll->on && pll->active_mask,
  10895. "pll in active use but not on in sw tracking\n");
  10896. I915_STATE_WARN(pll->on && !pll->active_mask,
  10897. "pll is on but not used by any active crtc\n");
  10898. I915_STATE_WARN(pll->on != active,
  10899. "pll on state mismatch (expected %i, found %i)\n",
  10900. pll->on, active);
  10901. }
  10902. if (!crtc) {
  10903. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10904. "more active pll users than references: %x vs %x\n",
  10905. pll->active_mask, pll->config.crtc_mask);
  10906. return;
  10907. }
  10908. crtc_mask = 1 << drm_crtc_index(crtc);
  10909. if (new_state->active)
  10910. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10911. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10912. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10913. else
  10914. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10915. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10916. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10917. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10918. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10919. crtc_mask, pll->config.crtc_mask);
  10920. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10921. &dpll_hw_state,
  10922. sizeof(dpll_hw_state)),
  10923. "pll hw state mismatch\n");
  10924. }
  10925. static void
  10926. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10927. struct drm_crtc_state *old_crtc_state,
  10928. struct drm_crtc_state *new_crtc_state)
  10929. {
  10930. struct drm_i915_private *dev_priv = dev->dev_private;
  10931. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10932. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10933. if (new_state->shared_dpll)
  10934. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10935. if (old_state->shared_dpll &&
  10936. old_state->shared_dpll != new_state->shared_dpll) {
  10937. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10938. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10939. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10940. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10941. pipe_name(drm_crtc_index(crtc)));
  10942. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10943. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10944. pipe_name(drm_crtc_index(crtc)));
  10945. }
  10946. }
  10947. static void
  10948. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10949. struct drm_crtc_state *old_state,
  10950. struct drm_crtc_state *new_state)
  10951. {
  10952. if (!needs_modeset(new_state) &&
  10953. !to_intel_crtc_state(new_state)->update_pipe)
  10954. return;
  10955. verify_wm_state(crtc, new_state);
  10956. verify_connector_state(crtc->dev, crtc);
  10957. verify_crtc_state(crtc, old_state, new_state);
  10958. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10959. }
  10960. static void
  10961. verify_disabled_dpll_state(struct drm_device *dev)
  10962. {
  10963. struct drm_i915_private *dev_priv = dev->dev_private;
  10964. int i;
  10965. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10966. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10967. }
  10968. static void
  10969. intel_modeset_verify_disabled(struct drm_device *dev)
  10970. {
  10971. verify_encoder_state(dev);
  10972. verify_connector_state(dev, NULL);
  10973. verify_disabled_dpll_state(dev);
  10974. }
  10975. static void update_scanline_offset(struct intel_crtc *crtc)
  10976. {
  10977. struct drm_device *dev = crtc->base.dev;
  10978. /*
  10979. * The scanline counter increments at the leading edge of hsync.
  10980. *
  10981. * On most platforms it starts counting from vtotal-1 on the
  10982. * first active line. That means the scanline counter value is
  10983. * always one less than what we would expect. Ie. just after
  10984. * start of vblank, which also occurs at start of hsync (on the
  10985. * last active line), the scanline counter will read vblank_start-1.
  10986. *
  10987. * On gen2 the scanline counter starts counting from 1 instead
  10988. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10989. * to keep the value positive), instead of adding one.
  10990. *
  10991. * On HSW+ the behaviour of the scanline counter depends on the output
  10992. * type. For DP ports it behaves like most other platforms, but on HDMI
  10993. * there's an extra 1 line difference. So we need to add two instead of
  10994. * one to the value.
  10995. */
  10996. if (IS_GEN2(dev)) {
  10997. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10998. int vtotal;
  10999. vtotal = adjusted_mode->crtc_vtotal;
  11000. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11001. vtotal /= 2;
  11002. crtc->scanline_offset = vtotal - 1;
  11003. } else if (HAS_DDI(dev) &&
  11004. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  11005. crtc->scanline_offset = 2;
  11006. } else
  11007. crtc->scanline_offset = 1;
  11008. }
  11009. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11010. {
  11011. struct drm_device *dev = state->dev;
  11012. struct drm_i915_private *dev_priv = to_i915(dev);
  11013. struct intel_shared_dpll_config *shared_dpll = NULL;
  11014. struct drm_crtc *crtc;
  11015. struct drm_crtc_state *crtc_state;
  11016. int i;
  11017. if (!dev_priv->display.crtc_compute_clock)
  11018. return;
  11019. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11021. struct intel_shared_dpll *old_dpll =
  11022. to_intel_crtc_state(crtc->state)->shared_dpll;
  11023. if (!needs_modeset(crtc_state))
  11024. continue;
  11025. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11026. if (!old_dpll)
  11027. continue;
  11028. if (!shared_dpll)
  11029. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11030. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11031. }
  11032. }
  11033. /*
  11034. * This implements the workaround described in the "notes" section of the mode
  11035. * set sequence documentation. When going from no pipes or single pipe to
  11036. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11037. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11038. */
  11039. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11040. {
  11041. struct drm_crtc_state *crtc_state;
  11042. struct intel_crtc *intel_crtc;
  11043. struct drm_crtc *crtc;
  11044. struct intel_crtc_state *first_crtc_state = NULL;
  11045. struct intel_crtc_state *other_crtc_state = NULL;
  11046. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11047. int i;
  11048. /* look at all crtc's that are going to be enabled in during modeset */
  11049. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11050. intel_crtc = to_intel_crtc(crtc);
  11051. if (!crtc_state->active || !needs_modeset(crtc_state))
  11052. continue;
  11053. if (first_crtc_state) {
  11054. other_crtc_state = to_intel_crtc_state(crtc_state);
  11055. break;
  11056. } else {
  11057. first_crtc_state = to_intel_crtc_state(crtc_state);
  11058. first_pipe = intel_crtc->pipe;
  11059. }
  11060. }
  11061. /* No workaround needed? */
  11062. if (!first_crtc_state)
  11063. return 0;
  11064. /* w/a possibly needed, check how many crtc's are already enabled. */
  11065. for_each_intel_crtc(state->dev, intel_crtc) {
  11066. struct intel_crtc_state *pipe_config;
  11067. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11068. if (IS_ERR(pipe_config))
  11069. return PTR_ERR(pipe_config);
  11070. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11071. if (!pipe_config->base.active ||
  11072. needs_modeset(&pipe_config->base))
  11073. continue;
  11074. /* 2 or more enabled crtcs means no need for w/a */
  11075. if (enabled_pipe != INVALID_PIPE)
  11076. return 0;
  11077. enabled_pipe = intel_crtc->pipe;
  11078. }
  11079. if (enabled_pipe != INVALID_PIPE)
  11080. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11081. else if (other_crtc_state)
  11082. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11083. return 0;
  11084. }
  11085. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11086. {
  11087. struct drm_crtc *crtc;
  11088. struct drm_crtc_state *crtc_state;
  11089. int ret = 0;
  11090. /* add all active pipes to the state */
  11091. for_each_crtc(state->dev, crtc) {
  11092. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11093. if (IS_ERR(crtc_state))
  11094. return PTR_ERR(crtc_state);
  11095. if (!crtc_state->active || needs_modeset(crtc_state))
  11096. continue;
  11097. crtc_state->mode_changed = true;
  11098. ret = drm_atomic_add_affected_connectors(state, crtc);
  11099. if (ret)
  11100. break;
  11101. ret = drm_atomic_add_affected_planes(state, crtc);
  11102. if (ret)
  11103. break;
  11104. }
  11105. return ret;
  11106. }
  11107. static int intel_modeset_checks(struct drm_atomic_state *state)
  11108. {
  11109. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11110. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11111. struct drm_crtc *crtc;
  11112. struct drm_crtc_state *crtc_state;
  11113. int ret = 0, i;
  11114. if (!check_digital_port_conflicts(state)) {
  11115. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11116. return -EINVAL;
  11117. }
  11118. intel_state->modeset = true;
  11119. intel_state->active_crtcs = dev_priv->active_crtcs;
  11120. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11121. if (crtc_state->active)
  11122. intel_state->active_crtcs |= 1 << i;
  11123. else
  11124. intel_state->active_crtcs &= ~(1 << i);
  11125. }
  11126. /*
  11127. * See if the config requires any additional preparation, e.g.
  11128. * to adjust global state with pipes off. We need to do this
  11129. * here so we can get the modeset_pipe updated config for the new
  11130. * mode set on this crtc. For other crtcs we need to use the
  11131. * adjusted_mode bits in the crtc directly.
  11132. */
  11133. if (dev_priv->display.modeset_calc_cdclk) {
  11134. ret = dev_priv->display.modeset_calc_cdclk(state);
  11135. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11136. ret = intel_modeset_all_pipes(state);
  11137. if (ret < 0)
  11138. return ret;
  11139. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11140. intel_state->cdclk, intel_state->dev_cdclk);
  11141. } else
  11142. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11143. intel_modeset_clear_plls(state);
  11144. if (IS_HASWELL(dev_priv))
  11145. return haswell_mode_set_planes_workaround(state);
  11146. return 0;
  11147. }
  11148. /*
  11149. * Handle calculation of various watermark data at the end of the atomic check
  11150. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11151. * handlers to ensure that all derived state has been updated.
  11152. */
  11153. static void calc_watermark_data(struct drm_atomic_state *state)
  11154. {
  11155. struct drm_device *dev = state->dev;
  11156. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11157. struct drm_crtc *crtc;
  11158. struct drm_crtc_state *cstate;
  11159. struct drm_plane *plane;
  11160. struct drm_plane_state *pstate;
  11161. /*
  11162. * Calculate watermark configuration details now that derived
  11163. * plane/crtc state is all properly updated.
  11164. */
  11165. drm_for_each_crtc(crtc, dev) {
  11166. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  11167. crtc->state;
  11168. if (cstate->active)
  11169. intel_state->wm_config.num_pipes_active++;
  11170. }
  11171. drm_for_each_legacy_plane(plane, dev) {
  11172. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11173. plane->state;
  11174. if (!to_intel_plane_state(pstate)->visible)
  11175. continue;
  11176. intel_state->wm_config.sprites_enabled = true;
  11177. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11178. pstate->crtc_h != pstate->src_h >> 16)
  11179. intel_state->wm_config.sprites_scaled = true;
  11180. }
  11181. }
  11182. /**
  11183. * intel_atomic_check - validate state object
  11184. * @dev: drm device
  11185. * @state: state to validate
  11186. */
  11187. static int intel_atomic_check(struct drm_device *dev,
  11188. struct drm_atomic_state *state)
  11189. {
  11190. struct drm_i915_private *dev_priv = to_i915(dev);
  11191. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11192. struct drm_crtc *crtc;
  11193. struct drm_crtc_state *crtc_state;
  11194. int ret, i;
  11195. bool any_ms = false;
  11196. ret = drm_atomic_helper_check_modeset(dev, state);
  11197. if (ret)
  11198. return ret;
  11199. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11200. struct intel_crtc_state *pipe_config =
  11201. to_intel_crtc_state(crtc_state);
  11202. /* Catch I915_MODE_FLAG_INHERITED */
  11203. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11204. crtc_state->mode_changed = true;
  11205. if (!crtc_state->enable) {
  11206. if (needs_modeset(crtc_state))
  11207. any_ms = true;
  11208. continue;
  11209. }
  11210. if (!needs_modeset(crtc_state))
  11211. continue;
  11212. /* FIXME: For only active_changed we shouldn't need to do any
  11213. * state recomputation at all. */
  11214. ret = drm_atomic_add_affected_connectors(state, crtc);
  11215. if (ret)
  11216. return ret;
  11217. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11218. if (ret)
  11219. return ret;
  11220. if (i915.fastboot &&
  11221. intel_pipe_config_compare(dev,
  11222. to_intel_crtc_state(crtc->state),
  11223. pipe_config, true)) {
  11224. crtc_state->mode_changed = false;
  11225. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11226. }
  11227. if (needs_modeset(crtc_state)) {
  11228. any_ms = true;
  11229. ret = drm_atomic_add_affected_planes(state, crtc);
  11230. if (ret)
  11231. return ret;
  11232. }
  11233. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11234. needs_modeset(crtc_state) ?
  11235. "[modeset]" : "[fastset]");
  11236. }
  11237. if (any_ms) {
  11238. ret = intel_modeset_checks(state);
  11239. if (ret)
  11240. return ret;
  11241. } else
  11242. intel_state->cdclk = dev_priv->cdclk_freq;
  11243. ret = drm_atomic_helper_check_planes(dev, state);
  11244. if (ret)
  11245. return ret;
  11246. intel_fbc_choose_crtc(dev_priv, state);
  11247. calc_watermark_data(state);
  11248. return 0;
  11249. }
  11250. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11251. struct drm_atomic_state *state,
  11252. bool nonblock)
  11253. {
  11254. struct drm_i915_private *dev_priv = dev->dev_private;
  11255. struct drm_plane_state *plane_state;
  11256. struct drm_crtc_state *crtc_state;
  11257. struct drm_plane *plane;
  11258. struct drm_crtc *crtc;
  11259. int i, ret;
  11260. if (nonblock) {
  11261. DRM_DEBUG_KMS("i915 does not yet support nonblocking commit\n");
  11262. return -EINVAL;
  11263. }
  11264. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11265. if (state->legacy_cursor_update)
  11266. continue;
  11267. ret = intel_crtc_wait_for_pending_flips(crtc);
  11268. if (ret)
  11269. return ret;
  11270. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11271. flush_workqueue(dev_priv->wq);
  11272. }
  11273. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11274. if (ret)
  11275. return ret;
  11276. ret = drm_atomic_helper_prepare_planes(dev, state);
  11277. mutex_unlock(&dev->struct_mutex);
  11278. if (!ret && !nonblock) {
  11279. for_each_plane_in_state(state, plane, plane_state, i) {
  11280. struct intel_plane_state *intel_plane_state =
  11281. to_intel_plane_state(plane_state);
  11282. if (!intel_plane_state->wait_req)
  11283. continue;
  11284. ret = __i915_wait_request(intel_plane_state->wait_req,
  11285. true, NULL, NULL);
  11286. if (ret) {
  11287. /* Any hang should be swallowed by the wait */
  11288. WARN_ON(ret == -EIO);
  11289. mutex_lock(&dev->struct_mutex);
  11290. drm_atomic_helper_cleanup_planes(dev, state);
  11291. mutex_unlock(&dev->struct_mutex);
  11292. break;
  11293. }
  11294. }
  11295. }
  11296. return ret;
  11297. }
  11298. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11299. struct drm_i915_private *dev_priv,
  11300. unsigned crtc_mask)
  11301. {
  11302. unsigned last_vblank_count[I915_MAX_PIPES];
  11303. enum pipe pipe;
  11304. int ret;
  11305. if (!crtc_mask)
  11306. return;
  11307. for_each_pipe(dev_priv, pipe) {
  11308. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11309. if (!((1 << pipe) & crtc_mask))
  11310. continue;
  11311. ret = drm_crtc_vblank_get(crtc);
  11312. if (WARN_ON(ret != 0)) {
  11313. crtc_mask &= ~(1 << pipe);
  11314. continue;
  11315. }
  11316. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11317. }
  11318. for_each_pipe(dev_priv, pipe) {
  11319. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11320. long lret;
  11321. if (!((1 << pipe) & crtc_mask))
  11322. continue;
  11323. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11324. last_vblank_count[pipe] !=
  11325. drm_crtc_vblank_count(crtc),
  11326. msecs_to_jiffies(50));
  11327. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11328. drm_crtc_vblank_put(crtc);
  11329. }
  11330. }
  11331. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11332. {
  11333. /* fb updated, need to unpin old fb */
  11334. if (crtc_state->fb_changed)
  11335. return true;
  11336. /* wm changes, need vblank before final wm's */
  11337. if (crtc_state->update_wm_post)
  11338. return true;
  11339. /*
  11340. * cxsr is re-enabled after vblank.
  11341. * This is already handled by crtc_state->update_wm_post,
  11342. * but added for clarity.
  11343. */
  11344. if (crtc_state->disable_cxsr)
  11345. return true;
  11346. return false;
  11347. }
  11348. /**
  11349. * intel_atomic_commit - commit validated state object
  11350. * @dev: DRM device
  11351. * @state: the top-level driver state object
  11352. * @nonblock: nonblocking commit
  11353. *
  11354. * This function commits a top-level state object that has been validated
  11355. * with drm_atomic_helper_check().
  11356. *
  11357. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11358. * we can only handle plane-related operations and do not yet support
  11359. * nonblocking commit.
  11360. *
  11361. * RETURNS
  11362. * Zero for success or -errno.
  11363. */
  11364. static int intel_atomic_commit(struct drm_device *dev,
  11365. struct drm_atomic_state *state,
  11366. bool nonblock)
  11367. {
  11368. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11369. struct drm_i915_private *dev_priv = dev->dev_private;
  11370. struct drm_crtc_state *old_crtc_state;
  11371. struct drm_crtc *crtc;
  11372. struct intel_crtc_state *intel_cstate;
  11373. int ret = 0, i;
  11374. bool hw_check = intel_state->modeset;
  11375. unsigned long put_domains[I915_MAX_PIPES] = {};
  11376. unsigned crtc_vblank_mask = 0;
  11377. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11378. if (ret) {
  11379. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11380. return ret;
  11381. }
  11382. drm_atomic_helper_swap_state(dev, state);
  11383. dev_priv->wm.config = intel_state->wm_config;
  11384. intel_shared_dpll_commit(state);
  11385. if (intel_state->modeset) {
  11386. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11387. sizeof(intel_state->min_pixclk));
  11388. dev_priv->active_crtcs = intel_state->active_crtcs;
  11389. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11390. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11391. }
  11392. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11394. if (needs_modeset(crtc->state) ||
  11395. to_intel_crtc_state(crtc->state)->update_pipe) {
  11396. hw_check = true;
  11397. put_domains[to_intel_crtc(crtc)->pipe] =
  11398. modeset_get_crtc_power_domains(crtc,
  11399. to_intel_crtc_state(crtc->state));
  11400. }
  11401. if (!needs_modeset(crtc->state))
  11402. continue;
  11403. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11404. if (old_crtc_state->active) {
  11405. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11406. dev_priv->display.crtc_disable(crtc);
  11407. intel_crtc->active = false;
  11408. intel_fbc_disable(intel_crtc);
  11409. intel_disable_shared_dpll(intel_crtc);
  11410. /*
  11411. * Underruns don't always raise
  11412. * interrupts, so check manually.
  11413. */
  11414. intel_check_cpu_fifo_underruns(dev_priv);
  11415. intel_check_pch_fifo_underruns(dev_priv);
  11416. if (!crtc->state->active)
  11417. intel_update_watermarks(crtc);
  11418. }
  11419. }
  11420. /* Only after disabling all output pipelines that will be changed can we
  11421. * update the the output configuration. */
  11422. intel_modeset_update_crtc_state(state);
  11423. if (intel_state->modeset) {
  11424. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11425. if (dev_priv->display.modeset_commit_cdclk &&
  11426. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11427. dev_priv->display.modeset_commit_cdclk(state);
  11428. intel_modeset_verify_disabled(dev);
  11429. }
  11430. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11431. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11433. bool modeset = needs_modeset(crtc->state);
  11434. struct intel_crtc_state *pipe_config =
  11435. to_intel_crtc_state(crtc->state);
  11436. bool update_pipe = !modeset && pipe_config->update_pipe;
  11437. if (modeset && crtc->state->active) {
  11438. update_scanline_offset(to_intel_crtc(crtc));
  11439. dev_priv->display.crtc_enable(crtc);
  11440. }
  11441. if (!modeset)
  11442. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11443. if (crtc->state->active &&
  11444. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11445. intel_fbc_enable(intel_crtc);
  11446. if (crtc->state->active &&
  11447. (crtc->state->planes_changed || update_pipe))
  11448. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11449. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11450. crtc_vblank_mask |= 1 << i;
  11451. }
  11452. /* FIXME: add subpixel order */
  11453. if (!state->legacy_cursor_update)
  11454. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11455. /*
  11456. * Now that the vblank has passed, we can go ahead and program the
  11457. * optimal watermarks on platforms that need two-step watermark
  11458. * programming.
  11459. *
  11460. * TODO: Move this (and other cleanup) to an async worker eventually.
  11461. */
  11462. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11463. intel_cstate = to_intel_crtc_state(crtc->state);
  11464. if (dev_priv->display.optimize_watermarks)
  11465. dev_priv->display.optimize_watermarks(intel_cstate);
  11466. }
  11467. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11468. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11469. if (put_domains[i])
  11470. modeset_put_power_domains(dev_priv, put_domains[i]);
  11471. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11472. }
  11473. if (intel_state->modeset)
  11474. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11475. mutex_lock(&dev->struct_mutex);
  11476. drm_atomic_helper_cleanup_planes(dev, state);
  11477. mutex_unlock(&dev->struct_mutex);
  11478. drm_atomic_state_free(state);
  11479. /* As one of the primary mmio accessors, KMS has a high likelihood
  11480. * of triggering bugs in unclaimed access. After we finish
  11481. * modesetting, see if an error has been flagged, and if so
  11482. * enable debugging for the next modeset - and hope we catch
  11483. * the culprit.
  11484. *
  11485. * XXX note that we assume display power is on at this point.
  11486. * This might hold true now but we need to add pm helper to check
  11487. * unclaimed only when the hardware is on, as atomic commits
  11488. * can happen also when the device is completely off.
  11489. */
  11490. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11491. return 0;
  11492. }
  11493. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11494. {
  11495. struct drm_device *dev = crtc->dev;
  11496. struct drm_atomic_state *state;
  11497. struct drm_crtc_state *crtc_state;
  11498. int ret;
  11499. state = drm_atomic_state_alloc(dev);
  11500. if (!state) {
  11501. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11502. crtc->base.id);
  11503. return;
  11504. }
  11505. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11506. retry:
  11507. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11508. ret = PTR_ERR_OR_ZERO(crtc_state);
  11509. if (!ret) {
  11510. if (!crtc_state->active)
  11511. goto out;
  11512. crtc_state->mode_changed = true;
  11513. ret = drm_atomic_commit(state);
  11514. }
  11515. if (ret == -EDEADLK) {
  11516. drm_atomic_state_clear(state);
  11517. drm_modeset_backoff(state->acquire_ctx);
  11518. goto retry;
  11519. }
  11520. if (ret)
  11521. out:
  11522. drm_atomic_state_free(state);
  11523. }
  11524. #undef for_each_intel_crtc_masked
  11525. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11526. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11527. .set_config = drm_atomic_helper_set_config,
  11528. .set_property = drm_atomic_helper_crtc_set_property,
  11529. .destroy = intel_crtc_destroy,
  11530. .page_flip = intel_crtc_page_flip,
  11531. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11532. .atomic_destroy_state = intel_crtc_destroy_state,
  11533. };
  11534. /**
  11535. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11536. * @plane: drm plane to prepare for
  11537. * @fb: framebuffer to prepare for presentation
  11538. *
  11539. * Prepares a framebuffer for usage on a display plane. Generally this
  11540. * involves pinning the underlying object and updating the frontbuffer tracking
  11541. * bits. Some older platforms need special physical address handling for
  11542. * cursor planes.
  11543. *
  11544. * Must be called with struct_mutex held.
  11545. *
  11546. * Returns 0 on success, negative error code on failure.
  11547. */
  11548. int
  11549. intel_prepare_plane_fb(struct drm_plane *plane,
  11550. const struct drm_plane_state *new_state)
  11551. {
  11552. struct drm_device *dev = plane->dev;
  11553. struct drm_framebuffer *fb = new_state->fb;
  11554. struct intel_plane *intel_plane = to_intel_plane(plane);
  11555. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11556. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11557. int ret = 0;
  11558. if (!obj && !old_obj)
  11559. return 0;
  11560. if (old_obj) {
  11561. struct drm_crtc_state *crtc_state =
  11562. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11563. /* Big Hammer, we also need to ensure that any pending
  11564. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11565. * current scanout is retired before unpinning the old
  11566. * framebuffer. Note that we rely on userspace rendering
  11567. * into the buffer attached to the pipe they are waiting
  11568. * on. If not, userspace generates a GPU hang with IPEHR
  11569. * point to the MI_WAIT_FOR_EVENT.
  11570. *
  11571. * This should only fail upon a hung GPU, in which case we
  11572. * can safely continue.
  11573. */
  11574. if (needs_modeset(crtc_state))
  11575. ret = i915_gem_object_wait_rendering(old_obj, true);
  11576. if (ret) {
  11577. /* GPU hangs should have been swallowed by the wait */
  11578. WARN_ON(ret == -EIO);
  11579. return ret;
  11580. }
  11581. }
  11582. /* For framebuffer backed by dmabuf, wait for fence */
  11583. if (obj && obj->base.dma_buf) {
  11584. long lret;
  11585. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11586. false, true,
  11587. MAX_SCHEDULE_TIMEOUT);
  11588. if (lret == -ERESTARTSYS)
  11589. return lret;
  11590. WARN(lret < 0, "waiting returns %li\n", lret);
  11591. }
  11592. if (!obj) {
  11593. ret = 0;
  11594. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11595. INTEL_INFO(dev)->cursor_needs_physical) {
  11596. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11597. ret = i915_gem_object_attach_phys(obj, align);
  11598. if (ret)
  11599. DRM_DEBUG_KMS("failed to attach phys object\n");
  11600. } else {
  11601. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11602. }
  11603. if (ret == 0) {
  11604. if (obj) {
  11605. struct intel_plane_state *plane_state =
  11606. to_intel_plane_state(new_state);
  11607. i915_gem_request_assign(&plane_state->wait_req,
  11608. obj->last_write_req);
  11609. }
  11610. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11611. }
  11612. return ret;
  11613. }
  11614. /**
  11615. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11616. * @plane: drm plane to clean up for
  11617. * @fb: old framebuffer that was on plane
  11618. *
  11619. * Cleans up a framebuffer that has just been removed from a plane.
  11620. *
  11621. * Must be called with struct_mutex held.
  11622. */
  11623. void
  11624. intel_cleanup_plane_fb(struct drm_plane *plane,
  11625. const struct drm_plane_state *old_state)
  11626. {
  11627. struct drm_device *dev = plane->dev;
  11628. struct intel_plane *intel_plane = to_intel_plane(plane);
  11629. struct intel_plane_state *old_intel_state;
  11630. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11631. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11632. old_intel_state = to_intel_plane_state(old_state);
  11633. if (!obj && !old_obj)
  11634. return;
  11635. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11636. !INTEL_INFO(dev)->cursor_needs_physical))
  11637. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11638. /* prepare_fb aborted? */
  11639. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11640. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11641. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11642. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11643. }
  11644. int
  11645. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11646. {
  11647. int max_scale;
  11648. struct drm_device *dev;
  11649. struct drm_i915_private *dev_priv;
  11650. int crtc_clock, cdclk;
  11651. if (!intel_crtc || !crtc_state->base.enable)
  11652. return DRM_PLANE_HELPER_NO_SCALING;
  11653. dev = intel_crtc->base.dev;
  11654. dev_priv = dev->dev_private;
  11655. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11656. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11657. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11658. return DRM_PLANE_HELPER_NO_SCALING;
  11659. /*
  11660. * skl max scale is lower of:
  11661. * close to 3 but not 3, -1 is for that purpose
  11662. * or
  11663. * cdclk/crtc_clock
  11664. */
  11665. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11666. return max_scale;
  11667. }
  11668. static int
  11669. intel_check_primary_plane(struct drm_plane *plane,
  11670. struct intel_crtc_state *crtc_state,
  11671. struct intel_plane_state *state)
  11672. {
  11673. struct drm_crtc *crtc = state->base.crtc;
  11674. struct drm_framebuffer *fb = state->base.fb;
  11675. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11676. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11677. bool can_position = false;
  11678. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11679. /* use scaler when colorkey is not required */
  11680. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11681. min_scale = 1;
  11682. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11683. }
  11684. can_position = true;
  11685. }
  11686. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11687. &state->dst, &state->clip,
  11688. min_scale, max_scale,
  11689. can_position, true,
  11690. &state->visible);
  11691. }
  11692. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11693. struct drm_crtc_state *old_crtc_state)
  11694. {
  11695. struct drm_device *dev = crtc->dev;
  11696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11697. struct intel_crtc_state *old_intel_state =
  11698. to_intel_crtc_state(old_crtc_state);
  11699. bool modeset = needs_modeset(crtc->state);
  11700. /* Perform vblank evasion around commit operation */
  11701. intel_pipe_update_start(intel_crtc);
  11702. if (modeset)
  11703. return;
  11704. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11705. intel_color_set_csc(crtc->state);
  11706. intel_color_load_luts(crtc->state);
  11707. }
  11708. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11709. intel_update_pipe_config(intel_crtc, old_intel_state);
  11710. else if (INTEL_INFO(dev)->gen >= 9)
  11711. skl_detach_scalers(intel_crtc);
  11712. }
  11713. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11714. struct drm_crtc_state *old_crtc_state)
  11715. {
  11716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11717. intel_pipe_update_end(intel_crtc);
  11718. }
  11719. /**
  11720. * intel_plane_destroy - destroy a plane
  11721. * @plane: plane to destroy
  11722. *
  11723. * Common destruction function for all types of planes (primary, cursor,
  11724. * sprite).
  11725. */
  11726. void intel_plane_destroy(struct drm_plane *plane)
  11727. {
  11728. struct intel_plane *intel_plane = to_intel_plane(plane);
  11729. drm_plane_cleanup(plane);
  11730. kfree(intel_plane);
  11731. }
  11732. const struct drm_plane_funcs intel_plane_funcs = {
  11733. .update_plane = drm_atomic_helper_update_plane,
  11734. .disable_plane = drm_atomic_helper_disable_plane,
  11735. .destroy = intel_plane_destroy,
  11736. .set_property = drm_atomic_helper_plane_set_property,
  11737. .atomic_get_property = intel_plane_atomic_get_property,
  11738. .atomic_set_property = intel_plane_atomic_set_property,
  11739. .atomic_duplicate_state = intel_plane_duplicate_state,
  11740. .atomic_destroy_state = intel_plane_destroy_state,
  11741. };
  11742. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11743. int pipe)
  11744. {
  11745. struct intel_plane *primary = NULL;
  11746. struct intel_plane_state *state = NULL;
  11747. const uint32_t *intel_primary_formats;
  11748. unsigned int num_formats;
  11749. int ret;
  11750. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11751. if (!primary)
  11752. goto fail;
  11753. state = intel_create_plane_state(&primary->base);
  11754. if (!state)
  11755. goto fail;
  11756. primary->base.state = &state->base;
  11757. primary->can_scale = false;
  11758. primary->max_downscale = 1;
  11759. if (INTEL_INFO(dev)->gen >= 9) {
  11760. primary->can_scale = true;
  11761. state->scaler_id = -1;
  11762. }
  11763. primary->pipe = pipe;
  11764. primary->plane = pipe;
  11765. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11766. primary->check_plane = intel_check_primary_plane;
  11767. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11768. primary->plane = !pipe;
  11769. if (INTEL_INFO(dev)->gen >= 9) {
  11770. intel_primary_formats = skl_primary_formats;
  11771. num_formats = ARRAY_SIZE(skl_primary_formats);
  11772. primary->update_plane = skylake_update_primary_plane;
  11773. primary->disable_plane = skylake_disable_primary_plane;
  11774. } else if (HAS_PCH_SPLIT(dev)) {
  11775. intel_primary_formats = i965_primary_formats;
  11776. num_formats = ARRAY_SIZE(i965_primary_formats);
  11777. primary->update_plane = ironlake_update_primary_plane;
  11778. primary->disable_plane = i9xx_disable_primary_plane;
  11779. } else if (INTEL_INFO(dev)->gen >= 4) {
  11780. intel_primary_formats = i965_primary_formats;
  11781. num_formats = ARRAY_SIZE(i965_primary_formats);
  11782. primary->update_plane = i9xx_update_primary_plane;
  11783. primary->disable_plane = i9xx_disable_primary_plane;
  11784. } else {
  11785. intel_primary_formats = i8xx_primary_formats;
  11786. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11787. primary->update_plane = i9xx_update_primary_plane;
  11788. primary->disable_plane = i9xx_disable_primary_plane;
  11789. }
  11790. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11791. &intel_plane_funcs,
  11792. intel_primary_formats, num_formats,
  11793. DRM_PLANE_TYPE_PRIMARY, NULL);
  11794. if (ret)
  11795. goto fail;
  11796. if (INTEL_INFO(dev)->gen >= 4)
  11797. intel_create_rotation_property(dev, primary);
  11798. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11799. return &primary->base;
  11800. fail:
  11801. kfree(state);
  11802. kfree(primary);
  11803. return NULL;
  11804. }
  11805. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11806. {
  11807. if (!dev->mode_config.rotation_property) {
  11808. unsigned long flags = BIT(DRM_ROTATE_0) |
  11809. BIT(DRM_ROTATE_180);
  11810. if (INTEL_INFO(dev)->gen >= 9)
  11811. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11812. dev->mode_config.rotation_property =
  11813. drm_mode_create_rotation_property(dev, flags);
  11814. }
  11815. if (dev->mode_config.rotation_property)
  11816. drm_object_attach_property(&plane->base.base,
  11817. dev->mode_config.rotation_property,
  11818. plane->base.state->rotation);
  11819. }
  11820. static int
  11821. intel_check_cursor_plane(struct drm_plane *plane,
  11822. struct intel_crtc_state *crtc_state,
  11823. struct intel_plane_state *state)
  11824. {
  11825. struct drm_crtc *crtc = crtc_state->base.crtc;
  11826. struct drm_framebuffer *fb = state->base.fb;
  11827. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11828. enum pipe pipe = to_intel_plane(plane)->pipe;
  11829. unsigned stride;
  11830. int ret;
  11831. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11832. &state->dst, &state->clip,
  11833. DRM_PLANE_HELPER_NO_SCALING,
  11834. DRM_PLANE_HELPER_NO_SCALING,
  11835. true, true, &state->visible);
  11836. if (ret)
  11837. return ret;
  11838. /* if we want to turn off the cursor ignore width and height */
  11839. if (!obj)
  11840. return 0;
  11841. /* Check for which cursor types we support */
  11842. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11843. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11844. state->base.crtc_w, state->base.crtc_h);
  11845. return -EINVAL;
  11846. }
  11847. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11848. if (obj->base.size < stride * state->base.crtc_h) {
  11849. DRM_DEBUG_KMS("buffer is too small\n");
  11850. return -ENOMEM;
  11851. }
  11852. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11853. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11854. return -EINVAL;
  11855. }
  11856. /*
  11857. * There's something wrong with the cursor on CHV pipe C.
  11858. * If it straddles the left edge of the screen then
  11859. * moving it away from the edge or disabling it often
  11860. * results in a pipe underrun, and often that can lead to
  11861. * dead pipe (constant underrun reported, and it scans
  11862. * out just a solid color). To recover from that, the
  11863. * display power well must be turned off and on again.
  11864. * Refuse the put the cursor into that compromised position.
  11865. */
  11866. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11867. state->visible && state->base.crtc_x < 0) {
  11868. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11869. return -EINVAL;
  11870. }
  11871. return 0;
  11872. }
  11873. static void
  11874. intel_disable_cursor_plane(struct drm_plane *plane,
  11875. struct drm_crtc *crtc)
  11876. {
  11877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11878. intel_crtc->cursor_addr = 0;
  11879. intel_crtc_update_cursor(crtc, NULL);
  11880. }
  11881. static void
  11882. intel_update_cursor_plane(struct drm_plane *plane,
  11883. const struct intel_crtc_state *crtc_state,
  11884. const struct intel_plane_state *state)
  11885. {
  11886. struct drm_crtc *crtc = crtc_state->base.crtc;
  11887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11888. struct drm_device *dev = plane->dev;
  11889. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11890. uint32_t addr;
  11891. if (!obj)
  11892. addr = 0;
  11893. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11894. addr = i915_gem_obj_ggtt_offset(obj);
  11895. else
  11896. addr = obj->phys_handle->busaddr;
  11897. intel_crtc->cursor_addr = addr;
  11898. intel_crtc_update_cursor(crtc, state);
  11899. }
  11900. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11901. int pipe)
  11902. {
  11903. struct intel_plane *cursor = NULL;
  11904. struct intel_plane_state *state = NULL;
  11905. int ret;
  11906. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11907. if (!cursor)
  11908. goto fail;
  11909. state = intel_create_plane_state(&cursor->base);
  11910. if (!state)
  11911. goto fail;
  11912. cursor->base.state = &state->base;
  11913. cursor->can_scale = false;
  11914. cursor->max_downscale = 1;
  11915. cursor->pipe = pipe;
  11916. cursor->plane = pipe;
  11917. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11918. cursor->check_plane = intel_check_cursor_plane;
  11919. cursor->update_plane = intel_update_cursor_plane;
  11920. cursor->disable_plane = intel_disable_cursor_plane;
  11921. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  11922. &intel_plane_funcs,
  11923. intel_cursor_formats,
  11924. ARRAY_SIZE(intel_cursor_formats),
  11925. DRM_PLANE_TYPE_CURSOR, NULL);
  11926. if (ret)
  11927. goto fail;
  11928. if (INTEL_INFO(dev)->gen >= 4) {
  11929. if (!dev->mode_config.rotation_property)
  11930. dev->mode_config.rotation_property =
  11931. drm_mode_create_rotation_property(dev,
  11932. BIT(DRM_ROTATE_0) |
  11933. BIT(DRM_ROTATE_180));
  11934. if (dev->mode_config.rotation_property)
  11935. drm_object_attach_property(&cursor->base.base,
  11936. dev->mode_config.rotation_property,
  11937. state->base.rotation);
  11938. }
  11939. if (INTEL_INFO(dev)->gen >=9)
  11940. state->scaler_id = -1;
  11941. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11942. return &cursor->base;
  11943. fail:
  11944. kfree(state);
  11945. kfree(cursor);
  11946. return NULL;
  11947. }
  11948. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11949. struct intel_crtc_state *crtc_state)
  11950. {
  11951. int i;
  11952. struct intel_scaler *intel_scaler;
  11953. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11954. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11955. intel_scaler = &scaler_state->scalers[i];
  11956. intel_scaler->in_use = 0;
  11957. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11958. }
  11959. scaler_state->scaler_id = -1;
  11960. }
  11961. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11962. {
  11963. struct drm_i915_private *dev_priv = dev->dev_private;
  11964. struct intel_crtc *intel_crtc;
  11965. struct intel_crtc_state *crtc_state = NULL;
  11966. struct drm_plane *primary = NULL;
  11967. struct drm_plane *cursor = NULL;
  11968. int ret;
  11969. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11970. if (intel_crtc == NULL)
  11971. return;
  11972. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11973. if (!crtc_state)
  11974. goto fail;
  11975. intel_crtc->config = crtc_state;
  11976. intel_crtc->base.state = &crtc_state->base;
  11977. crtc_state->base.crtc = &intel_crtc->base;
  11978. /* initialize shared scalers */
  11979. if (INTEL_INFO(dev)->gen >= 9) {
  11980. if (pipe == PIPE_C)
  11981. intel_crtc->num_scalers = 1;
  11982. else
  11983. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11984. skl_init_scalers(dev, intel_crtc, crtc_state);
  11985. }
  11986. primary = intel_primary_plane_create(dev, pipe);
  11987. if (!primary)
  11988. goto fail;
  11989. cursor = intel_cursor_plane_create(dev, pipe);
  11990. if (!cursor)
  11991. goto fail;
  11992. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11993. cursor, &intel_crtc_funcs, NULL);
  11994. if (ret)
  11995. goto fail;
  11996. /*
  11997. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11998. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11999. */
  12000. intel_crtc->pipe = pipe;
  12001. intel_crtc->plane = pipe;
  12002. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12003. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12004. intel_crtc->plane = !pipe;
  12005. }
  12006. intel_crtc->cursor_base = ~0;
  12007. intel_crtc->cursor_cntl = ~0;
  12008. intel_crtc->cursor_size = ~0;
  12009. intel_crtc->wm.cxsr_allowed = true;
  12010. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12011. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12012. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12013. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12014. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12015. intel_color_init(&intel_crtc->base);
  12016. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12017. return;
  12018. fail:
  12019. if (primary)
  12020. drm_plane_cleanup(primary);
  12021. if (cursor)
  12022. drm_plane_cleanup(cursor);
  12023. kfree(crtc_state);
  12024. kfree(intel_crtc);
  12025. }
  12026. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12027. {
  12028. struct drm_encoder *encoder = connector->base.encoder;
  12029. struct drm_device *dev = connector->base.dev;
  12030. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12031. if (!encoder || WARN_ON(!encoder->crtc))
  12032. return INVALID_PIPE;
  12033. return to_intel_crtc(encoder->crtc)->pipe;
  12034. }
  12035. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12036. struct drm_file *file)
  12037. {
  12038. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12039. struct drm_crtc *drmmode_crtc;
  12040. struct intel_crtc *crtc;
  12041. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12042. if (!drmmode_crtc) {
  12043. DRM_ERROR("no such CRTC id\n");
  12044. return -ENOENT;
  12045. }
  12046. crtc = to_intel_crtc(drmmode_crtc);
  12047. pipe_from_crtc_id->pipe = crtc->pipe;
  12048. return 0;
  12049. }
  12050. static int intel_encoder_clones(struct intel_encoder *encoder)
  12051. {
  12052. struct drm_device *dev = encoder->base.dev;
  12053. struct intel_encoder *source_encoder;
  12054. int index_mask = 0;
  12055. int entry = 0;
  12056. for_each_intel_encoder(dev, source_encoder) {
  12057. if (encoders_cloneable(encoder, source_encoder))
  12058. index_mask |= (1 << entry);
  12059. entry++;
  12060. }
  12061. return index_mask;
  12062. }
  12063. static bool has_edp_a(struct drm_device *dev)
  12064. {
  12065. struct drm_i915_private *dev_priv = dev->dev_private;
  12066. if (!IS_MOBILE(dev))
  12067. return false;
  12068. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12069. return false;
  12070. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12071. return false;
  12072. return true;
  12073. }
  12074. static bool intel_crt_present(struct drm_device *dev)
  12075. {
  12076. struct drm_i915_private *dev_priv = dev->dev_private;
  12077. if (INTEL_INFO(dev)->gen >= 9)
  12078. return false;
  12079. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12080. return false;
  12081. if (IS_CHERRYVIEW(dev))
  12082. return false;
  12083. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12084. return false;
  12085. /* DDI E can't be used if DDI A requires 4 lanes */
  12086. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12087. return false;
  12088. if (!dev_priv->vbt.int_crt_support)
  12089. return false;
  12090. return true;
  12091. }
  12092. static void intel_setup_outputs(struct drm_device *dev)
  12093. {
  12094. struct drm_i915_private *dev_priv = dev->dev_private;
  12095. struct intel_encoder *encoder;
  12096. bool dpd_is_edp = false;
  12097. intel_lvds_init(dev);
  12098. if (intel_crt_present(dev))
  12099. intel_crt_init(dev);
  12100. if (IS_BROXTON(dev)) {
  12101. /*
  12102. * FIXME: Broxton doesn't support port detection via the
  12103. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12104. * detect the ports.
  12105. */
  12106. intel_ddi_init(dev, PORT_A);
  12107. intel_ddi_init(dev, PORT_B);
  12108. intel_ddi_init(dev, PORT_C);
  12109. intel_dsi_init(dev);
  12110. } else if (HAS_DDI(dev)) {
  12111. int found;
  12112. /*
  12113. * Haswell uses DDI functions to detect digital outputs.
  12114. * On SKL pre-D0 the strap isn't connected, so we assume
  12115. * it's there.
  12116. */
  12117. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12118. /* WaIgnoreDDIAStrap: skl */
  12119. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12120. intel_ddi_init(dev, PORT_A);
  12121. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12122. * register */
  12123. found = I915_READ(SFUSE_STRAP);
  12124. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12125. intel_ddi_init(dev, PORT_B);
  12126. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12127. intel_ddi_init(dev, PORT_C);
  12128. if (found & SFUSE_STRAP_DDID_DETECTED)
  12129. intel_ddi_init(dev, PORT_D);
  12130. /*
  12131. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12132. */
  12133. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12134. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12135. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12136. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12137. intel_ddi_init(dev, PORT_E);
  12138. } else if (HAS_PCH_SPLIT(dev)) {
  12139. int found;
  12140. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12141. if (has_edp_a(dev))
  12142. intel_dp_init(dev, DP_A, PORT_A);
  12143. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12144. /* PCH SDVOB multiplex with HDMIB */
  12145. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12146. if (!found)
  12147. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12148. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12149. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12150. }
  12151. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12152. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12153. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12154. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12155. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12156. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12157. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12158. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12159. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12160. /*
  12161. * The DP_DETECTED bit is the latched state of the DDC
  12162. * SDA pin at boot. However since eDP doesn't require DDC
  12163. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12164. * eDP ports may have been muxed to an alternate function.
  12165. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12166. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12167. * detect eDP ports.
  12168. */
  12169. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12170. !intel_dp_is_edp(dev, PORT_B))
  12171. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12172. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12173. intel_dp_is_edp(dev, PORT_B))
  12174. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12175. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12176. !intel_dp_is_edp(dev, PORT_C))
  12177. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12178. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12179. intel_dp_is_edp(dev, PORT_C))
  12180. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12181. if (IS_CHERRYVIEW(dev)) {
  12182. /* eDP not supported on port D, so don't check VBT */
  12183. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12184. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12185. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12186. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12187. }
  12188. intel_dsi_init(dev);
  12189. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12190. bool found = false;
  12191. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12192. DRM_DEBUG_KMS("probing SDVOB\n");
  12193. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12194. if (!found && IS_G4X(dev)) {
  12195. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12196. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12197. }
  12198. if (!found && IS_G4X(dev))
  12199. intel_dp_init(dev, DP_B, PORT_B);
  12200. }
  12201. /* Before G4X SDVOC doesn't have its own detect register */
  12202. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12203. DRM_DEBUG_KMS("probing SDVOC\n");
  12204. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12205. }
  12206. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12207. if (IS_G4X(dev)) {
  12208. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12209. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12210. }
  12211. if (IS_G4X(dev))
  12212. intel_dp_init(dev, DP_C, PORT_C);
  12213. }
  12214. if (IS_G4X(dev) &&
  12215. (I915_READ(DP_D) & DP_DETECTED))
  12216. intel_dp_init(dev, DP_D, PORT_D);
  12217. } else if (IS_GEN2(dev))
  12218. intel_dvo_init(dev);
  12219. if (SUPPORTS_TV(dev))
  12220. intel_tv_init(dev);
  12221. intel_psr_init(dev);
  12222. for_each_intel_encoder(dev, encoder) {
  12223. encoder->base.possible_crtcs = encoder->crtc_mask;
  12224. encoder->base.possible_clones =
  12225. intel_encoder_clones(encoder);
  12226. }
  12227. intel_init_pch_refclk(dev);
  12228. drm_helper_move_panel_connectors_to_head(dev);
  12229. }
  12230. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12231. {
  12232. struct drm_device *dev = fb->dev;
  12233. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12234. drm_framebuffer_cleanup(fb);
  12235. mutex_lock(&dev->struct_mutex);
  12236. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12237. drm_gem_object_unreference(&intel_fb->obj->base);
  12238. mutex_unlock(&dev->struct_mutex);
  12239. kfree(intel_fb);
  12240. }
  12241. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12242. struct drm_file *file,
  12243. unsigned int *handle)
  12244. {
  12245. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12246. struct drm_i915_gem_object *obj = intel_fb->obj;
  12247. if (obj->userptr.mm) {
  12248. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12249. return -EINVAL;
  12250. }
  12251. return drm_gem_handle_create(file, &obj->base, handle);
  12252. }
  12253. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12254. struct drm_file *file,
  12255. unsigned flags, unsigned color,
  12256. struct drm_clip_rect *clips,
  12257. unsigned num_clips)
  12258. {
  12259. struct drm_device *dev = fb->dev;
  12260. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12261. struct drm_i915_gem_object *obj = intel_fb->obj;
  12262. mutex_lock(&dev->struct_mutex);
  12263. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12264. mutex_unlock(&dev->struct_mutex);
  12265. return 0;
  12266. }
  12267. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12268. .destroy = intel_user_framebuffer_destroy,
  12269. .create_handle = intel_user_framebuffer_create_handle,
  12270. .dirty = intel_user_framebuffer_dirty,
  12271. };
  12272. static
  12273. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12274. uint32_t pixel_format)
  12275. {
  12276. u32 gen = INTEL_INFO(dev)->gen;
  12277. if (gen >= 9) {
  12278. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12279. /* "The stride in bytes must not exceed the of the size of 8K
  12280. * pixels and 32K bytes."
  12281. */
  12282. return min(8192 * cpp, 32768);
  12283. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12284. return 32*1024;
  12285. } else if (gen >= 4) {
  12286. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12287. return 16*1024;
  12288. else
  12289. return 32*1024;
  12290. } else if (gen >= 3) {
  12291. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12292. return 8*1024;
  12293. else
  12294. return 16*1024;
  12295. } else {
  12296. /* XXX DSPC is limited to 4k tiled */
  12297. return 8*1024;
  12298. }
  12299. }
  12300. static int intel_framebuffer_init(struct drm_device *dev,
  12301. struct intel_framebuffer *intel_fb,
  12302. struct drm_mode_fb_cmd2 *mode_cmd,
  12303. struct drm_i915_gem_object *obj)
  12304. {
  12305. struct drm_i915_private *dev_priv = to_i915(dev);
  12306. unsigned int aligned_height;
  12307. int ret;
  12308. u32 pitch_limit, stride_alignment;
  12309. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12310. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12311. /* Enforce that fb modifier and tiling mode match, but only for
  12312. * X-tiled. This is needed for FBC. */
  12313. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12314. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12315. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12316. return -EINVAL;
  12317. }
  12318. } else {
  12319. if (obj->tiling_mode == I915_TILING_X)
  12320. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12321. else if (obj->tiling_mode == I915_TILING_Y) {
  12322. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12323. return -EINVAL;
  12324. }
  12325. }
  12326. /* Passed in modifier sanity checking. */
  12327. switch (mode_cmd->modifier[0]) {
  12328. case I915_FORMAT_MOD_Y_TILED:
  12329. case I915_FORMAT_MOD_Yf_TILED:
  12330. if (INTEL_INFO(dev)->gen < 9) {
  12331. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12332. mode_cmd->modifier[0]);
  12333. return -EINVAL;
  12334. }
  12335. case DRM_FORMAT_MOD_NONE:
  12336. case I915_FORMAT_MOD_X_TILED:
  12337. break;
  12338. default:
  12339. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12340. mode_cmd->modifier[0]);
  12341. return -EINVAL;
  12342. }
  12343. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12344. mode_cmd->modifier[0],
  12345. mode_cmd->pixel_format);
  12346. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12347. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12348. mode_cmd->pitches[0], stride_alignment);
  12349. return -EINVAL;
  12350. }
  12351. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12352. mode_cmd->pixel_format);
  12353. if (mode_cmd->pitches[0] > pitch_limit) {
  12354. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12355. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12356. "tiled" : "linear",
  12357. mode_cmd->pitches[0], pitch_limit);
  12358. return -EINVAL;
  12359. }
  12360. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12361. mode_cmd->pitches[0] != obj->stride) {
  12362. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12363. mode_cmd->pitches[0], obj->stride);
  12364. return -EINVAL;
  12365. }
  12366. /* Reject formats not supported by any plane early. */
  12367. switch (mode_cmd->pixel_format) {
  12368. case DRM_FORMAT_C8:
  12369. case DRM_FORMAT_RGB565:
  12370. case DRM_FORMAT_XRGB8888:
  12371. case DRM_FORMAT_ARGB8888:
  12372. break;
  12373. case DRM_FORMAT_XRGB1555:
  12374. if (INTEL_INFO(dev)->gen > 3) {
  12375. DRM_DEBUG("unsupported pixel format: %s\n",
  12376. drm_get_format_name(mode_cmd->pixel_format));
  12377. return -EINVAL;
  12378. }
  12379. break;
  12380. case DRM_FORMAT_ABGR8888:
  12381. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12382. INTEL_INFO(dev)->gen < 9) {
  12383. DRM_DEBUG("unsupported pixel format: %s\n",
  12384. drm_get_format_name(mode_cmd->pixel_format));
  12385. return -EINVAL;
  12386. }
  12387. break;
  12388. case DRM_FORMAT_XBGR8888:
  12389. case DRM_FORMAT_XRGB2101010:
  12390. case DRM_FORMAT_XBGR2101010:
  12391. if (INTEL_INFO(dev)->gen < 4) {
  12392. DRM_DEBUG("unsupported pixel format: %s\n",
  12393. drm_get_format_name(mode_cmd->pixel_format));
  12394. return -EINVAL;
  12395. }
  12396. break;
  12397. case DRM_FORMAT_ABGR2101010:
  12398. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12399. DRM_DEBUG("unsupported pixel format: %s\n",
  12400. drm_get_format_name(mode_cmd->pixel_format));
  12401. return -EINVAL;
  12402. }
  12403. break;
  12404. case DRM_FORMAT_YUYV:
  12405. case DRM_FORMAT_UYVY:
  12406. case DRM_FORMAT_YVYU:
  12407. case DRM_FORMAT_VYUY:
  12408. if (INTEL_INFO(dev)->gen < 5) {
  12409. DRM_DEBUG("unsupported pixel format: %s\n",
  12410. drm_get_format_name(mode_cmd->pixel_format));
  12411. return -EINVAL;
  12412. }
  12413. break;
  12414. default:
  12415. DRM_DEBUG("unsupported pixel format: %s\n",
  12416. drm_get_format_name(mode_cmd->pixel_format));
  12417. return -EINVAL;
  12418. }
  12419. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12420. if (mode_cmd->offsets[0] != 0)
  12421. return -EINVAL;
  12422. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12423. mode_cmd->pixel_format,
  12424. mode_cmd->modifier[0]);
  12425. /* FIXME drm helper for size checks (especially planar formats)? */
  12426. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12427. return -EINVAL;
  12428. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12429. intel_fb->obj = obj;
  12430. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12431. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12432. if (ret) {
  12433. DRM_ERROR("framebuffer init failed %d\n", ret);
  12434. return ret;
  12435. }
  12436. intel_fb->obj->framebuffer_references++;
  12437. return 0;
  12438. }
  12439. static struct drm_framebuffer *
  12440. intel_user_framebuffer_create(struct drm_device *dev,
  12441. struct drm_file *filp,
  12442. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12443. {
  12444. struct drm_framebuffer *fb;
  12445. struct drm_i915_gem_object *obj;
  12446. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12447. obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
  12448. if (&obj->base == NULL)
  12449. return ERR_PTR(-ENOENT);
  12450. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12451. if (IS_ERR(fb))
  12452. drm_gem_object_unreference_unlocked(&obj->base);
  12453. return fb;
  12454. }
  12455. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12456. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12457. {
  12458. }
  12459. #endif
  12460. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12461. .fb_create = intel_user_framebuffer_create,
  12462. .output_poll_changed = intel_fbdev_output_poll_changed,
  12463. .atomic_check = intel_atomic_check,
  12464. .atomic_commit = intel_atomic_commit,
  12465. .atomic_state_alloc = intel_atomic_state_alloc,
  12466. .atomic_state_clear = intel_atomic_state_clear,
  12467. };
  12468. /**
  12469. * intel_init_display_hooks - initialize the display modesetting hooks
  12470. * @dev_priv: device private
  12471. */
  12472. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12473. {
  12474. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12475. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12476. dev_priv->display.get_initial_plane_config =
  12477. skylake_get_initial_plane_config;
  12478. dev_priv->display.crtc_compute_clock =
  12479. haswell_crtc_compute_clock;
  12480. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12481. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12482. } else if (HAS_DDI(dev_priv)) {
  12483. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12484. dev_priv->display.get_initial_plane_config =
  12485. ironlake_get_initial_plane_config;
  12486. dev_priv->display.crtc_compute_clock =
  12487. haswell_crtc_compute_clock;
  12488. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12489. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12490. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12491. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12492. dev_priv->display.get_initial_plane_config =
  12493. ironlake_get_initial_plane_config;
  12494. dev_priv->display.crtc_compute_clock =
  12495. ironlake_crtc_compute_clock;
  12496. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12497. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12498. } else if (IS_CHERRYVIEW(dev_priv)) {
  12499. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12500. dev_priv->display.get_initial_plane_config =
  12501. i9xx_get_initial_plane_config;
  12502. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12503. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12504. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12505. } else if (IS_VALLEYVIEW(dev_priv)) {
  12506. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12507. dev_priv->display.get_initial_plane_config =
  12508. i9xx_get_initial_plane_config;
  12509. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12510. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12511. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12512. } else if (IS_G4X(dev_priv)) {
  12513. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12514. dev_priv->display.get_initial_plane_config =
  12515. i9xx_get_initial_plane_config;
  12516. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12517. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12518. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12519. } else if (IS_PINEVIEW(dev_priv)) {
  12520. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12521. dev_priv->display.get_initial_plane_config =
  12522. i9xx_get_initial_plane_config;
  12523. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12524. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12525. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12526. } else if (!IS_GEN2(dev_priv)) {
  12527. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12528. dev_priv->display.get_initial_plane_config =
  12529. i9xx_get_initial_plane_config;
  12530. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12531. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12532. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12533. } else {
  12534. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12535. dev_priv->display.get_initial_plane_config =
  12536. i9xx_get_initial_plane_config;
  12537. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12538. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12539. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12540. }
  12541. /* Returns the core display clock speed */
  12542. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12543. dev_priv->display.get_display_clock_speed =
  12544. skylake_get_display_clock_speed;
  12545. else if (IS_BROXTON(dev_priv))
  12546. dev_priv->display.get_display_clock_speed =
  12547. broxton_get_display_clock_speed;
  12548. else if (IS_BROADWELL(dev_priv))
  12549. dev_priv->display.get_display_clock_speed =
  12550. broadwell_get_display_clock_speed;
  12551. else if (IS_HASWELL(dev_priv))
  12552. dev_priv->display.get_display_clock_speed =
  12553. haswell_get_display_clock_speed;
  12554. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12555. dev_priv->display.get_display_clock_speed =
  12556. valleyview_get_display_clock_speed;
  12557. else if (IS_GEN5(dev_priv))
  12558. dev_priv->display.get_display_clock_speed =
  12559. ilk_get_display_clock_speed;
  12560. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12561. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12562. dev_priv->display.get_display_clock_speed =
  12563. i945_get_display_clock_speed;
  12564. else if (IS_GM45(dev_priv))
  12565. dev_priv->display.get_display_clock_speed =
  12566. gm45_get_display_clock_speed;
  12567. else if (IS_CRESTLINE(dev_priv))
  12568. dev_priv->display.get_display_clock_speed =
  12569. i965gm_get_display_clock_speed;
  12570. else if (IS_PINEVIEW(dev_priv))
  12571. dev_priv->display.get_display_clock_speed =
  12572. pnv_get_display_clock_speed;
  12573. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12574. dev_priv->display.get_display_clock_speed =
  12575. g33_get_display_clock_speed;
  12576. else if (IS_I915G(dev_priv))
  12577. dev_priv->display.get_display_clock_speed =
  12578. i915_get_display_clock_speed;
  12579. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12580. dev_priv->display.get_display_clock_speed =
  12581. i9xx_misc_get_display_clock_speed;
  12582. else if (IS_I915GM(dev_priv))
  12583. dev_priv->display.get_display_clock_speed =
  12584. i915gm_get_display_clock_speed;
  12585. else if (IS_I865G(dev_priv))
  12586. dev_priv->display.get_display_clock_speed =
  12587. i865_get_display_clock_speed;
  12588. else if (IS_I85X(dev_priv))
  12589. dev_priv->display.get_display_clock_speed =
  12590. i85x_get_display_clock_speed;
  12591. else { /* 830 */
  12592. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12593. dev_priv->display.get_display_clock_speed =
  12594. i830_get_display_clock_speed;
  12595. }
  12596. if (IS_GEN5(dev_priv)) {
  12597. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12598. } else if (IS_GEN6(dev_priv)) {
  12599. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12600. } else if (IS_IVYBRIDGE(dev_priv)) {
  12601. /* FIXME: detect B0+ stepping and use auto training */
  12602. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12603. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12604. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12605. if (IS_BROADWELL(dev_priv)) {
  12606. dev_priv->display.modeset_commit_cdclk =
  12607. broadwell_modeset_commit_cdclk;
  12608. dev_priv->display.modeset_calc_cdclk =
  12609. broadwell_modeset_calc_cdclk;
  12610. }
  12611. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12612. dev_priv->display.modeset_commit_cdclk =
  12613. valleyview_modeset_commit_cdclk;
  12614. dev_priv->display.modeset_calc_cdclk =
  12615. valleyview_modeset_calc_cdclk;
  12616. } else if (IS_BROXTON(dev_priv)) {
  12617. dev_priv->display.modeset_commit_cdclk =
  12618. broxton_modeset_commit_cdclk;
  12619. dev_priv->display.modeset_calc_cdclk =
  12620. broxton_modeset_calc_cdclk;
  12621. }
  12622. switch (INTEL_INFO(dev_priv)->gen) {
  12623. case 2:
  12624. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12625. break;
  12626. case 3:
  12627. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12628. break;
  12629. case 4:
  12630. case 5:
  12631. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12632. break;
  12633. case 6:
  12634. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12635. break;
  12636. case 7:
  12637. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12638. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12639. break;
  12640. case 9:
  12641. /* Drop through - unsupported since execlist only. */
  12642. default:
  12643. /* Default just returns -ENODEV to indicate unsupported */
  12644. dev_priv->display.queue_flip = intel_default_queue_flip;
  12645. }
  12646. }
  12647. /*
  12648. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12649. * resume, or other times. This quirk makes sure that's the case for
  12650. * affected systems.
  12651. */
  12652. static void quirk_pipea_force(struct drm_device *dev)
  12653. {
  12654. struct drm_i915_private *dev_priv = dev->dev_private;
  12655. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12656. DRM_INFO("applying pipe a force quirk\n");
  12657. }
  12658. static void quirk_pipeb_force(struct drm_device *dev)
  12659. {
  12660. struct drm_i915_private *dev_priv = dev->dev_private;
  12661. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12662. DRM_INFO("applying pipe b force quirk\n");
  12663. }
  12664. /*
  12665. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12666. */
  12667. static void quirk_ssc_force_disable(struct drm_device *dev)
  12668. {
  12669. struct drm_i915_private *dev_priv = dev->dev_private;
  12670. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12671. DRM_INFO("applying lvds SSC disable quirk\n");
  12672. }
  12673. /*
  12674. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12675. * brightness value
  12676. */
  12677. static void quirk_invert_brightness(struct drm_device *dev)
  12678. {
  12679. struct drm_i915_private *dev_priv = dev->dev_private;
  12680. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12681. DRM_INFO("applying inverted panel brightness quirk\n");
  12682. }
  12683. /* Some VBT's incorrectly indicate no backlight is present */
  12684. static void quirk_backlight_present(struct drm_device *dev)
  12685. {
  12686. struct drm_i915_private *dev_priv = dev->dev_private;
  12687. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12688. DRM_INFO("applying backlight present quirk\n");
  12689. }
  12690. struct intel_quirk {
  12691. int device;
  12692. int subsystem_vendor;
  12693. int subsystem_device;
  12694. void (*hook)(struct drm_device *dev);
  12695. };
  12696. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12697. struct intel_dmi_quirk {
  12698. void (*hook)(struct drm_device *dev);
  12699. const struct dmi_system_id (*dmi_id_list)[];
  12700. };
  12701. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12702. {
  12703. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12704. return 1;
  12705. }
  12706. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12707. {
  12708. .dmi_id_list = &(const struct dmi_system_id[]) {
  12709. {
  12710. .callback = intel_dmi_reverse_brightness,
  12711. .ident = "NCR Corporation",
  12712. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12713. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12714. },
  12715. },
  12716. { } /* terminating entry */
  12717. },
  12718. .hook = quirk_invert_brightness,
  12719. },
  12720. };
  12721. static struct intel_quirk intel_quirks[] = {
  12722. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12723. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12724. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12725. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12726. /* 830 needs to leave pipe A & dpll A up */
  12727. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12728. /* 830 needs to leave pipe B & dpll B up */
  12729. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12730. /* Lenovo U160 cannot use SSC on LVDS */
  12731. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12732. /* Sony Vaio Y cannot use SSC on LVDS */
  12733. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12734. /* Acer Aspire 5734Z must invert backlight brightness */
  12735. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12736. /* Acer/eMachines G725 */
  12737. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12738. /* Acer/eMachines e725 */
  12739. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12740. /* Acer/Packard Bell NCL20 */
  12741. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12742. /* Acer Aspire 4736Z */
  12743. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12744. /* Acer Aspire 5336 */
  12745. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12746. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12747. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12748. /* Acer C720 Chromebook (Core i3 4005U) */
  12749. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12750. /* Apple Macbook 2,1 (Core 2 T7400) */
  12751. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12752. /* Apple Macbook 4,1 */
  12753. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12754. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12755. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12756. /* HP Chromebook 14 (Celeron 2955U) */
  12757. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12758. /* Dell Chromebook 11 */
  12759. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12760. /* Dell Chromebook 11 (2015 version) */
  12761. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12762. };
  12763. static void intel_init_quirks(struct drm_device *dev)
  12764. {
  12765. struct pci_dev *d = dev->pdev;
  12766. int i;
  12767. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12768. struct intel_quirk *q = &intel_quirks[i];
  12769. if (d->device == q->device &&
  12770. (d->subsystem_vendor == q->subsystem_vendor ||
  12771. q->subsystem_vendor == PCI_ANY_ID) &&
  12772. (d->subsystem_device == q->subsystem_device ||
  12773. q->subsystem_device == PCI_ANY_ID))
  12774. q->hook(dev);
  12775. }
  12776. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12777. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12778. intel_dmi_quirks[i].hook(dev);
  12779. }
  12780. }
  12781. /* Disable the VGA plane that we never use */
  12782. static void i915_disable_vga(struct drm_device *dev)
  12783. {
  12784. struct drm_i915_private *dev_priv = dev->dev_private;
  12785. u8 sr1;
  12786. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12787. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12788. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12789. outb(SR01, VGA_SR_INDEX);
  12790. sr1 = inb(VGA_SR_DATA);
  12791. outb(sr1 | 1<<5, VGA_SR_DATA);
  12792. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12793. udelay(300);
  12794. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12795. POSTING_READ(vga_reg);
  12796. }
  12797. void intel_modeset_init_hw(struct drm_device *dev)
  12798. {
  12799. struct drm_i915_private *dev_priv = dev->dev_private;
  12800. intel_update_cdclk(dev);
  12801. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12802. intel_init_clock_gating(dev);
  12803. intel_enable_gt_powersave(dev);
  12804. }
  12805. /*
  12806. * Calculate what we think the watermarks should be for the state we've read
  12807. * out of the hardware and then immediately program those watermarks so that
  12808. * we ensure the hardware settings match our internal state.
  12809. *
  12810. * We can calculate what we think WM's should be by creating a duplicate of the
  12811. * current state (which was constructed during hardware readout) and running it
  12812. * through the atomic check code to calculate new watermark values in the
  12813. * state object.
  12814. */
  12815. static void sanitize_watermarks(struct drm_device *dev)
  12816. {
  12817. struct drm_i915_private *dev_priv = to_i915(dev);
  12818. struct drm_atomic_state *state;
  12819. struct drm_crtc *crtc;
  12820. struct drm_crtc_state *cstate;
  12821. struct drm_modeset_acquire_ctx ctx;
  12822. int ret;
  12823. int i;
  12824. /* Only supported on platforms that use atomic watermark design */
  12825. if (!dev_priv->display.optimize_watermarks)
  12826. return;
  12827. /*
  12828. * We need to hold connection_mutex before calling duplicate_state so
  12829. * that the connector loop is protected.
  12830. */
  12831. drm_modeset_acquire_init(&ctx, 0);
  12832. retry:
  12833. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12834. if (ret == -EDEADLK) {
  12835. drm_modeset_backoff(&ctx);
  12836. goto retry;
  12837. } else if (WARN_ON(ret)) {
  12838. goto fail;
  12839. }
  12840. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12841. if (WARN_ON(IS_ERR(state)))
  12842. goto fail;
  12843. /*
  12844. * Hardware readout is the only time we don't want to calculate
  12845. * intermediate watermarks (since we don't trust the current
  12846. * watermarks).
  12847. */
  12848. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12849. ret = intel_atomic_check(dev, state);
  12850. if (ret) {
  12851. /*
  12852. * If we fail here, it means that the hardware appears to be
  12853. * programmed in a way that shouldn't be possible, given our
  12854. * understanding of watermark requirements. This might mean a
  12855. * mistake in the hardware readout code or a mistake in the
  12856. * watermark calculations for a given platform. Raise a WARN
  12857. * so that this is noticeable.
  12858. *
  12859. * If this actually happens, we'll have to just leave the
  12860. * BIOS-programmed watermarks untouched and hope for the best.
  12861. */
  12862. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12863. goto fail;
  12864. }
  12865. /* Write calculated watermark values back */
  12866. to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
  12867. for_each_crtc_in_state(state, crtc, cstate, i) {
  12868. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12869. cs->wm.need_postvbl_update = true;
  12870. dev_priv->display.optimize_watermarks(cs);
  12871. }
  12872. drm_atomic_state_free(state);
  12873. fail:
  12874. drm_modeset_drop_locks(&ctx);
  12875. drm_modeset_acquire_fini(&ctx);
  12876. }
  12877. void intel_modeset_init(struct drm_device *dev)
  12878. {
  12879. struct drm_i915_private *dev_priv = to_i915(dev);
  12880. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12881. int sprite, ret;
  12882. enum pipe pipe;
  12883. struct intel_crtc *crtc;
  12884. drm_mode_config_init(dev);
  12885. dev->mode_config.min_width = 0;
  12886. dev->mode_config.min_height = 0;
  12887. dev->mode_config.preferred_depth = 24;
  12888. dev->mode_config.prefer_shadow = 1;
  12889. dev->mode_config.allow_fb_modifiers = true;
  12890. dev->mode_config.funcs = &intel_mode_funcs;
  12891. intel_init_quirks(dev);
  12892. intel_init_pm(dev);
  12893. if (INTEL_INFO(dev)->num_pipes == 0)
  12894. return;
  12895. /*
  12896. * There may be no VBT; and if the BIOS enabled SSC we can
  12897. * just keep using it to avoid unnecessary flicker. Whereas if the
  12898. * BIOS isn't using it, don't assume it will work even if the VBT
  12899. * indicates as much.
  12900. */
  12901. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12902. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12903. DREF_SSC1_ENABLE);
  12904. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12905. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12906. bios_lvds_use_ssc ? "en" : "dis",
  12907. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12908. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12909. }
  12910. }
  12911. if (IS_GEN2(dev)) {
  12912. dev->mode_config.max_width = 2048;
  12913. dev->mode_config.max_height = 2048;
  12914. } else if (IS_GEN3(dev)) {
  12915. dev->mode_config.max_width = 4096;
  12916. dev->mode_config.max_height = 4096;
  12917. } else {
  12918. dev->mode_config.max_width = 8192;
  12919. dev->mode_config.max_height = 8192;
  12920. }
  12921. if (IS_845G(dev) || IS_I865G(dev)) {
  12922. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12923. dev->mode_config.cursor_height = 1023;
  12924. } else if (IS_GEN2(dev)) {
  12925. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12926. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12927. } else {
  12928. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12929. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12930. }
  12931. dev->mode_config.fb_base = ggtt->mappable_base;
  12932. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12933. INTEL_INFO(dev)->num_pipes,
  12934. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12935. for_each_pipe(dev_priv, pipe) {
  12936. intel_crtc_init(dev, pipe);
  12937. for_each_sprite(dev_priv, pipe, sprite) {
  12938. ret = intel_plane_init(dev, pipe, sprite);
  12939. if (ret)
  12940. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12941. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12942. }
  12943. }
  12944. intel_update_czclk(dev_priv);
  12945. intel_update_rawclk(dev_priv);
  12946. intel_update_cdclk(dev);
  12947. intel_shared_dpll_init(dev);
  12948. /* Just disable it once at startup */
  12949. i915_disable_vga(dev);
  12950. intel_setup_outputs(dev);
  12951. drm_modeset_lock_all(dev);
  12952. intel_modeset_setup_hw_state(dev);
  12953. drm_modeset_unlock_all(dev);
  12954. for_each_intel_crtc(dev, crtc) {
  12955. struct intel_initial_plane_config plane_config = {};
  12956. if (!crtc->active)
  12957. continue;
  12958. /*
  12959. * Note that reserving the BIOS fb up front prevents us
  12960. * from stuffing other stolen allocations like the ring
  12961. * on top. This prevents some ugliness at boot time, and
  12962. * can even allow for smooth boot transitions if the BIOS
  12963. * fb is large enough for the active pipe configuration.
  12964. */
  12965. dev_priv->display.get_initial_plane_config(crtc,
  12966. &plane_config);
  12967. /*
  12968. * If the fb is shared between multiple heads, we'll
  12969. * just get the first one.
  12970. */
  12971. intel_find_initial_plane_obj(crtc, &plane_config);
  12972. }
  12973. /*
  12974. * Make sure hardware watermarks really match the state we read out.
  12975. * Note that we need to do this after reconstructing the BIOS fb's
  12976. * since the watermark calculation done here will use pstate->fb.
  12977. */
  12978. sanitize_watermarks(dev);
  12979. }
  12980. static void intel_enable_pipe_a(struct drm_device *dev)
  12981. {
  12982. struct intel_connector *connector;
  12983. struct drm_connector *crt = NULL;
  12984. struct intel_load_detect_pipe load_detect_temp;
  12985. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12986. /* We can't just switch on the pipe A, we need to set things up with a
  12987. * proper mode and output configuration. As a gross hack, enable pipe A
  12988. * by enabling the load detect pipe once. */
  12989. for_each_intel_connector(dev, connector) {
  12990. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12991. crt = &connector->base;
  12992. break;
  12993. }
  12994. }
  12995. if (!crt)
  12996. return;
  12997. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12998. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12999. }
  13000. static bool
  13001. intel_check_plane_mapping(struct intel_crtc *crtc)
  13002. {
  13003. struct drm_device *dev = crtc->base.dev;
  13004. struct drm_i915_private *dev_priv = dev->dev_private;
  13005. u32 val;
  13006. if (INTEL_INFO(dev)->num_pipes == 1)
  13007. return true;
  13008. val = I915_READ(DSPCNTR(!crtc->plane));
  13009. if ((val & DISPLAY_PLANE_ENABLE) &&
  13010. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13011. return false;
  13012. return true;
  13013. }
  13014. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13015. {
  13016. struct drm_device *dev = crtc->base.dev;
  13017. struct intel_encoder *encoder;
  13018. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13019. return true;
  13020. return false;
  13021. }
  13022. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13023. {
  13024. struct drm_device *dev = encoder->base.dev;
  13025. struct intel_connector *connector;
  13026. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13027. return true;
  13028. return false;
  13029. }
  13030. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13031. {
  13032. struct drm_device *dev = crtc->base.dev;
  13033. struct drm_i915_private *dev_priv = dev->dev_private;
  13034. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13035. /* Clear any frame start delays used for debugging left by the BIOS */
  13036. if (!transcoder_is_dsi(cpu_transcoder)) {
  13037. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13038. I915_WRITE(reg,
  13039. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13040. }
  13041. /* restore vblank interrupts to correct state */
  13042. drm_crtc_vblank_reset(&crtc->base);
  13043. if (crtc->active) {
  13044. struct intel_plane *plane;
  13045. drm_crtc_vblank_on(&crtc->base);
  13046. /* Disable everything but the primary plane */
  13047. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13048. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13049. continue;
  13050. plane->disable_plane(&plane->base, &crtc->base);
  13051. }
  13052. }
  13053. /* We need to sanitize the plane -> pipe mapping first because this will
  13054. * disable the crtc (and hence change the state) if it is wrong. Note
  13055. * that gen4+ has a fixed plane -> pipe mapping. */
  13056. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13057. bool plane;
  13058. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  13059. crtc->base.base.id);
  13060. /* Pipe has the wrong plane attached and the plane is active.
  13061. * Temporarily change the plane mapping and disable everything
  13062. * ... */
  13063. plane = crtc->plane;
  13064. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13065. crtc->plane = !plane;
  13066. intel_crtc_disable_noatomic(&crtc->base);
  13067. crtc->plane = plane;
  13068. }
  13069. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13070. crtc->pipe == PIPE_A && !crtc->active) {
  13071. /* BIOS forgot to enable pipe A, this mostly happens after
  13072. * resume. Force-enable the pipe to fix this, the update_dpms
  13073. * call below we restore the pipe to the right state, but leave
  13074. * the required bits on. */
  13075. intel_enable_pipe_a(dev);
  13076. }
  13077. /* Adjust the state of the output pipe according to whether we
  13078. * have active connectors/encoders. */
  13079. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13080. intel_crtc_disable_noatomic(&crtc->base);
  13081. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13082. /*
  13083. * We start out with underrun reporting disabled to avoid races.
  13084. * For correct bookkeeping mark this on active crtcs.
  13085. *
  13086. * Also on gmch platforms we dont have any hardware bits to
  13087. * disable the underrun reporting. Which means we need to start
  13088. * out with underrun reporting disabled also on inactive pipes,
  13089. * since otherwise we'll complain about the garbage we read when
  13090. * e.g. coming up after runtime pm.
  13091. *
  13092. * No protection against concurrent access is required - at
  13093. * worst a fifo underrun happens which also sets this to false.
  13094. */
  13095. crtc->cpu_fifo_underrun_disabled = true;
  13096. crtc->pch_fifo_underrun_disabled = true;
  13097. }
  13098. }
  13099. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13100. {
  13101. struct intel_connector *connector;
  13102. struct drm_device *dev = encoder->base.dev;
  13103. /* We need to check both for a crtc link (meaning that the
  13104. * encoder is active and trying to read from a pipe) and the
  13105. * pipe itself being active. */
  13106. bool has_active_crtc = encoder->base.crtc &&
  13107. to_intel_crtc(encoder->base.crtc)->active;
  13108. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13109. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13110. encoder->base.base.id,
  13111. encoder->base.name);
  13112. /* Connector is active, but has no active pipe. This is
  13113. * fallout from our resume register restoring. Disable
  13114. * the encoder manually again. */
  13115. if (encoder->base.crtc) {
  13116. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13117. encoder->base.base.id,
  13118. encoder->base.name);
  13119. encoder->disable(encoder);
  13120. if (encoder->post_disable)
  13121. encoder->post_disable(encoder);
  13122. }
  13123. encoder->base.crtc = NULL;
  13124. /* Inconsistent output/port/pipe state happens presumably due to
  13125. * a bug in one of the get_hw_state functions. Or someplace else
  13126. * in our code, like the register restore mess on resume. Clamp
  13127. * things to off as a safer default. */
  13128. for_each_intel_connector(dev, connector) {
  13129. if (connector->encoder != encoder)
  13130. continue;
  13131. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13132. connector->base.encoder = NULL;
  13133. }
  13134. }
  13135. /* Enabled encoders without active connectors will be fixed in
  13136. * the crtc fixup. */
  13137. }
  13138. void i915_redisable_vga_power_on(struct drm_device *dev)
  13139. {
  13140. struct drm_i915_private *dev_priv = dev->dev_private;
  13141. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13142. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13143. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13144. i915_disable_vga(dev);
  13145. }
  13146. }
  13147. void i915_redisable_vga(struct drm_device *dev)
  13148. {
  13149. struct drm_i915_private *dev_priv = dev->dev_private;
  13150. /* This function can be called both from intel_modeset_setup_hw_state or
  13151. * at a very early point in our resume sequence, where the power well
  13152. * structures are not yet restored. Since this function is at a very
  13153. * paranoid "someone might have enabled VGA while we were not looking"
  13154. * level, just check if the power well is enabled instead of trying to
  13155. * follow the "don't touch the power well if we don't need it" policy
  13156. * the rest of the driver uses. */
  13157. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13158. return;
  13159. i915_redisable_vga_power_on(dev);
  13160. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13161. }
  13162. static bool primary_get_hw_state(struct intel_plane *plane)
  13163. {
  13164. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13165. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13166. }
  13167. /* FIXME read out full plane state for all planes */
  13168. static void readout_plane_state(struct intel_crtc *crtc)
  13169. {
  13170. struct drm_plane *primary = crtc->base.primary;
  13171. struct intel_plane_state *plane_state =
  13172. to_intel_plane_state(primary->state);
  13173. plane_state->visible = crtc->active &&
  13174. primary_get_hw_state(to_intel_plane(primary));
  13175. if (plane_state->visible)
  13176. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13177. }
  13178. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13179. {
  13180. struct drm_i915_private *dev_priv = dev->dev_private;
  13181. enum pipe pipe;
  13182. struct intel_crtc *crtc;
  13183. struct intel_encoder *encoder;
  13184. struct intel_connector *connector;
  13185. int i;
  13186. dev_priv->active_crtcs = 0;
  13187. for_each_intel_crtc(dev, crtc) {
  13188. struct intel_crtc_state *crtc_state = crtc->config;
  13189. int pixclk = 0;
  13190. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13191. memset(crtc_state, 0, sizeof(*crtc_state));
  13192. crtc_state->base.crtc = &crtc->base;
  13193. crtc_state->base.active = crtc_state->base.enable =
  13194. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13195. crtc->base.enabled = crtc_state->base.enable;
  13196. crtc->active = crtc_state->base.active;
  13197. if (crtc_state->base.active) {
  13198. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13199. if (IS_BROADWELL(dev_priv)) {
  13200. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13201. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13202. if (crtc_state->ips_enabled)
  13203. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13204. } else if (IS_VALLEYVIEW(dev_priv) ||
  13205. IS_CHERRYVIEW(dev_priv) ||
  13206. IS_BROXTON(dev_priv))
  13207. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13208. else
  13209. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13210. }
  13211. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13212. readout_plane_state(crtc);
  13213. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13214. crtc->base.base.id,
  13215. crtc->active ? "enabled" : "disabled");
  13216. }
  13217. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13218. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13219. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13220. &pll->config.hw_state);
  13221. pll->config.crtc_mask = 0;
  13222. for_each_intel_crtc(dev, crtc) {
  13223. if (crtc->active && crtc->config->shared_dpll == pll)
  13224. pll->config.crtc_mask |= 1 << crtc->pipe;
  13225. }
  13226. pll->active_mask = pll->config.crtc_mask;
  13227. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13228. pll->name, pll->config.crtc_mask, pll->on);
  13229. }
  13230. for_each_intel_encoder(dev, encoder) {
  13231. pipe = 0;
  13232. if (encoder->get_hw_state(encoder, &pipe)) {
  13233. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13234. encoder->base.crtc = &crtc->base;
  13235. encoder->get_config(encoder, crtc->config);
  13236. } else {
  13237. encoder->base.crtc = NULL;
  13238. }
  13239. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13240. encoder->base.base.id,
  13241. encoder->base.name,
  13242. encoder->base.crtc ? "enabled" : "disabled",
  13243. pipe_name(pipe));
  13244. }
  13245. for_each_intel_connector(dev, connector) {
  13246. if (connector->get_hw_state(connector)) {
  13247. connector->base.dpms = DRM_MODE_DPMS_ON;
  13248. encoder = connector->encoder;
  13249. connector->base.encoder = &encoder->base;
  13250. if (encoder->base.crtc &&
  13251. encoder->base.crtc->state->active) {
  13252. /*
  13253. * This has to be done during hardware readout
  13254. * because anything calling .crtc_disable may
  13255. * rely on the connector_mask being accurate.
  13256. */
  13257. encoder->base.crtc->state->connector_mask |=
  13258. 1 << drm_connector_index(&connector->base);
  13259. encoder->base.crtc->state->encoder_mask |=
  13260. 1 << drm_encoder_index(&encoder->base);
  13261. }
  13262. } else {
  13263. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13264. connector->base.encoder = NULL;
  13265. }
  13266. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13267. connector->base.base.id,
  13268. connector->base.name,
  13269. connector->base.encoder ? "enabled" : "disabled");
  13270. }
  13271. for_each_intel_crtc(dev, crtc) {
  13272. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13273. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13274. if (crtc->base.state->active) {
  13275. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13276. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13277. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13278. /*
  13279. * The initial mode needs to be set in order to keep
  13280. * the atomic core happy. It wants a valid mode if the
  13281. * crtc's enabled, so we do the above call.
  13282. *
  13283. * At this point some state updated by the connectors
  13284. * in their ->detect() callback has not run yet, so
  13285. * no recalculation can be done yet.
  13286. *
  13287. * Even if we could do a recalculation and modeset
  13288. * right now it would cause a double modeset if
  13289. * fbdev or userspace chooses a different initial mode.
  13290. *
  13291. * If that happens, someone indicated they wanted a
  13292. * mode change, which means it's safe to do a full
  13293. * recalculation.
  13294. */
  13295. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13296. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13297. update_scanline_offset(crtc);
  13298. }
  13299. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13300. }
  13301. }
  13302. /* Scan out the current hw modeset state,
  13303. * and sanitizes it to the current state
  13304. */
  13305. static void
  13306. intel_modeset_setup_hw_state(struct drm_device *dev)
  13307. {
  13308. struct drm_i915_private *dev_priv = dev->dev_private;
  13309. enum pipe pipe;
  13310. struct intel_crtc *crtc;
  13311. struct intel_encoder *encoder;
  13312. int i;
  13313. intel_modeset_readout_hw_state(dev);
  13314. /* HW state is read out, now we need to sanitize this mess. */
  13315. for_each_intel_encoder(dev, encoder) {
  13316. intel_sanitize_encoder(encoder);
  13317. }
  13318. for_each_pipe(dev_priv, pipe) {
  13319. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13320. intel_sanitize_crtc(crtc);
  13321. intel_dump_pipe_config(crtc, crtc->config,
  13322. "[setup_hw_state]");
  13323. }
  13324. intel_modeset_update_connector_atomic_state(dev);
  13325. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13326. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13327. if (!pll->on || pll->active_mask)
  13328. continue;
  13329. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13330. pll->funcs.disable(dev_priv, pll);
  13331. pll->on = false;
  13332. }
  13333. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13334. vlv_wm_get_hw_state(dev);
  13335. else if (IS_GEN9(dev))
  13336. skl_wm_get_hw_state(dev);
  13337. else if (HAS_PCH_SPLIT(dev))
  13338. ilk_wm_get_hw_state(dev);
  13339. for_each_intel_crtc(dev, crtc) {
  13340. unsigned long put_domains;
  13341. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13342. if (WARN_ON(put_domains))
  13343. modeset_put_power_domains(dev_priv, put_domains);
  13344. }
  13345. intel_display_set_init_power(dev_priv, false);
  13346. intel_fbc_init_pipe_state(dev_priv);
  13347. }
  13348. void intel_display_resume(struct drm_device *dev)
  13349. {
  13350. struct drm_i915_private *dev_priv = to_i915(dev);
  13351. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13352. struct drm_modeset_acquire_ctx ctx;
  13353. int ret;
  13354. bool setup = false;
  13355. dev_priv->modeset_restore_state = NULL;
  13356. /*
  13357. * This is a cludge because with real atomic modeset mode_config.mutex
  13358. * won't be taken. Unfortunately some probed state like
  13359. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13360. * it here for now.
  13361. */
  13362. mutex_lock(&dev->mode_config.mutex);
  13363. drm_modeset_acquire_init(&ctx, 0);
  13364. retry:
  13365. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13366. if (ret == 0 && !setup) {
  13367. setup = true;
  13368. intel_modeset_setup_hw_state(dev);
  13369. i915_redisable_vga(dev);
  13370. }
  13371. if (ret == 0 && state) {
  13372. struct drm_crtc_state *crtc_state;
  13373. struct drm_crtc *crtc;
  13374. int i;
  13375. state->acquire_ctx = &ctx;
  13376. /* ignore any reset values/BIOS leftovers in the WM registers */
  13377. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13378. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13379. /*
  13380. * Force recalculation even if we restore
  13381. * current state. With fast modeset this may not result
  13382. * in a modeset when the state is compatible.
  13383. */
  13384. crtc_state->mode_changed = true;
  13385. }
  13386. ret = drm_atomic_commit(state);
  13387. }
  13388. if (ret == -EDEADLK) {
  13389. drm_modeset_backoff(&ctx);
  13390. goto retry;
  13391. }
  13392. drm_modeset_drop_locks(&ctx);
  13393. drm_modeset_acquire_fini(&ctx);
  13394. mutex_unlock(&dev->mode_config.mutex);
  13395. if (ret) {
  13396. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13397. drm_atomic_state_free(state);
  13398. }
  13399. }
  13400. void intel_modeset_gem_init(struct drm_device *dev)
  13401. {
  13402. struct drm_crtc *c;
  13403. struct drm_i915_gem_object *obj;
  13404. int ret;
  13405. intel_init_gt_powersave(dev);
  13406. intel_modeset_init_hw(dev);
  13407. intel_setup_overlay(dev);
  13408. /*
  13409. * Make sure any fbs we allocated at startup are properly
  13410. * pinned & fenced. When we do the allocation it's too early
  13411. * for this.
  13412. */
  13413. for_each_crtc(dev, c) {
  13414. obj = intel_fb_obj(c->primary->fb);
  13415. if (obj == NULL)
  13416. continue;
  13417. mutex_lock(&dev->struct_mutex);
  13418. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13419. c->primary->state->rotation);
  13420. mutex_unlock(&dev->struct_mutex);
  13421. if (ret) {
  13422. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13423. to_intel_crtc(c)->pipe);
  13424. drm_framebuffer_unreference(c->primary->fb);
  13425. c->primary->fb = NULL;
  13426. c->primary->crtc = c->primary->state->crtc = NULL;
  13427. update_state_fb(c->primary);
  13428. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13429. }
  13430. }
  13431. intel_backlight_register(dev);
  13432. }
  13433. void intel_connector_unregister(struct intel_connector *intel_connector)
  13434. {
  13435. struct drm_connector *connector = &intel_connector->base;
  13436. intel_panel_destroy_backlight(connector);
  13437. drm_connector_unregister(connector);
  13438. }
  13439. void intel_modeset_cleanup(struct drm_device *dev)
  13440. {
  13441. struct drm_i915_private *dev_priv = dev->dev_private;
  13442. struct intel_connector *connector;
  13443. intel_disable_gt_powersave(dev);
  13444. intel_backlight_unregister(dev);
  13445. /*
  13446. * Interrupts and polling as the first thing to avoid creating havoc.
  13447. * Too much stuff here (turning of connectors, ...) would
  13448. * experience fancy races otherwise.
  13449. */
  13450. intel_irq_uninstall(dev_priv);
  13451. /*
  13452. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13453. * poll handlers. Hence disable polling after hpd handling is shut down.
  13454. */
  13455. drm_kms_helper_poll_fini(dev);
  13456. intel_unregister_dsm_handler();
  13457. intel_fbc_global_disable(dev_priv);
  13458. /* flush any delayed tasks or pending work */
  13459. flush_scheduled_work();
  13460. /* destroy the backlight and sysfs files before encoders/connectors */
  13461. for_each_intel_connector(dev, connector)
  13462. connector->unregister(connector);
  13463. drm_mode_config_cleanup(dev);
  13464. intel_cleanup_overlay(dev);
  13465. intel_cleanup_gt_powersave(dev);
  13466. intel_teardown_gmbus(dev);
  13467. }
  13468. /*
  13469. * Return which encoder is currently attached for connector.
  13470. */
  13471. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13472. {
  13473. return &intel_attached_encoder(connector)->base;
  13474. }
  13475. void intel_connector_attach_encoder(struct intel_connector *connector,
  13476. struct intel_encoder *encoder)
  13477. {
  13478. connector->encoder = encoder;
  13479. drm_mode_connector_attach_encoder(&connector->base,
  13480. &encoder->base);
  13481. }
  13482. /*
  13483. * set vga decode state - true == enable VGA decode
  13484. */
  13485. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13486. {
  13487. struct drm_i915_private *dev_priv = dev->dev_private;
  13488. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13489. u16 gmch_ctrl;
  13490. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13491. DRM_ERROR("failed to read control word\n");
  13492. return -EIO;
  13493. }
  13494. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13495. return 0;
  13496. if (state)
  13497. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13498. else
  13499. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13500. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13501. DRM_ERROR("failed to write control word\n");
  13502. return -EIO;
  13503. }
  13504. return 0;
  13505. }
  13506. struct intel_display_error_state {
  13507. u32 power_well_driver;
  13508. int num_transcoders;
  13509. struct intel_cursor_error_state {
  13510. u32 control;
  13511. u32 position;
  13512. u32 base;
  13513. u32 size;
  13514. } cursor[I915_MAX_PIPES];
  13515. struct intel_pipe_error_state {
  13516. bool power_domain_on;
  13517. u32 source;
  13518. u32 stat;
  13519. } pipe[I915_MAX_PIPES];
  13520. struct intel_plane_error_state {
  13521. u32 control;
  13522. u32 stride;
  13523. u32 size;
  13524. u32 pos;
  13525. u32 addr;
  13526. u32 surface;
  13527. u32 tile_offset;
  13528. } plane[I915_MAX_PIPES];
  13529. struct intel_transcoder_error_state {
  13530. bool power_domain_on;
  13531. enum transcoder cpu_transcoder;
  13532. u32 conf;
  13533. u32 htotal;
  13534. u32 hblank;
  13535. u32 hsync;
  13536. u32 vtotal;
  13537. u32 vblank;
  13538. u32 vsync;
  13539. } transcoder[4];
  13540. };
  13541. struct intel_display_error_state *
  13542. intel_display_capture_error_state(struct drm_device *dev)
  13543. {
  13544. struct drm_i915_private *dev_priv = dev->dev_private;
  13545. struct intel_display_error_state *error;
  13546. int transcoders[] = {
  13547. TRANSCODER_A,
  13548. TRANSCODER_B,
  13549. TRANSCODER_C,
  13550. TRANSCODER_EDP,
  13551. };
  13552. int i;
  13553. if (INTEL_INFO(dev)->num_pipes == 0)
  13554. return NULL;
  13555. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13556. if (error == NULL)
  13557. return NULL;
  13558. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13559. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13560. for_each_pipe(dev_priv, i) {
  13561. error->pipe[i].power_domain_on =
  13562. __intel_display_power_is_enabled(dev_priv,
  13563. POWER_DOMAIN_PIPE(i));
  13564. if (!error->pipe[i].power_domain_on)
  13565. continue;
  13566. error->cursor[i].control = I915_READ(CURCNTR(i));
  13567. error->cursor[i].position = I915_READ(CURPOS(i));
  13568. error->cursor[i].base = I915_READ(CURBASE(i));
  13569. error->plane[i].control = I915_READ(DSPCNTR(i));
  13570. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13571. if (INTEL_INFO(dev)->gen <= 3) {
  13572. error->plane[i].size = I915_READ(DSPSIZE(i));
  13573. error->plane[i].pos = I915_READ(DSPPOS(i));
  13574. }
  13575. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13576. error->plane[i].addr = I915_READ(DSPADDR(i));
  13577. if (INTEL_INFO(dev)->gen >= 4) {
  13578. error->plane[i].surface = I915_READ(DSPSURF(i));
  13579. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13580. }
  13581. error->pipe[i].source = I915_READ(PIPESRC(i));
  13582. if (HAS_GMCH_DISPLAY(dev))
  13583. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13584. }
  13585. /* Note: this does not include DSI transcoders. */
  13586. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13587. if (HAS_DDI(dev_priv))
  13588. error->num_transcoders++; /* Account for eDP. */
  13589. for (i = 0; i < error->num_transcoders; i++) {
  13590. enum transcoder cpu_transcoder = transcoders[i];
  13591. error->transcoder[i].power_domain_on =
  13592. __intel_display_power_is_enabled(dev_priv,
  13593. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13594. if (!error->transcoder[i].power_domain_on)
  13595. continue;
  13596. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13597. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13598. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13599. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13600. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13601. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13602. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13603. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13604. }
  13605. return error;
  13606. }
  13607. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13608. void
  13609. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13610. struct drm_device *dev,
  13611. struct intel_display_error_state *error)
  13612. {
  13613. struct drm_i915_private *dev_priv = dev->dev_private;
  13614. int i;
  13615. if (!error)
  13616. return;
  13617. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13618. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13619. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13620. error->power_well_driver);
  13621. for_each_pipe(dev_priv, i) {
  13622. err_printf(m, "Pipe [%d]:\n", i);
  13623. err_printf(m, " Power: %s\n",
  13624. onoff(error->pipe[i].power_domain_on));
  13625. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13626. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13627. err_printf(m, "Plane [%d]:\n", i);
  13628. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13629. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13630. if (INTEL_INFO(dev)->gen <= 3) {
  13631. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13632. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13633. }
  13634. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13635. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13636. if (INTEL_INFO(dev)->gen >= 4) {
  13637. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13638. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13639. }
  13640. err_printf(m, "Cursor [%d]:\n", i);
  13641. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13642. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13643. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13644. }
  13645. for (i = 0; i < error->num_transcoders; i++) {
  13646. err_printf(m, "CPU transcoder: %s\n",
  13647. transcoder_name(error->transcoder[i].cpu_transcoder));
  13648. err_printf(m, " Power: %s\n",
  13649. onoff(error->transcoder[i].power_domain_on));
  13650. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13651. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13652. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13653. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13654. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13655. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13656. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13657. }
  13658. }