intel_display.c 455 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310
  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. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  114. struct intel_limit {
  115. struct {
  116. int min, max;
  117. } dot, vco, n, m, m1, m2, p, p1;
  118. struct {
  119. int dot_limit;
  120. int p2_slow, p2_fast;
  121. } p2;
  122. };
  123. /* returns HPLL frequency in kHz */
  124. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  125. {
  126. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  127. /* Obtain SKU information */
  128. mutex_lock(&dev_priv->sb_lock);
  129. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  130. CCK_FUSE_HPLL_FREQ_MASK;
  131. mutex_unlock(&dev_priv->sb_lock);
  132. return vco_freq[hpll_freq] * 1000;
  133. }
  134. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  135. const char *name, u32 reg, int ref_freq)
  136. {
  137. u32 val;
  138. int divider;
  139. mutex_lock(&dev_priv->sb_lock);
  140. val = vlv_cck_read(dev_priv, reg);
  141. mutex_unlock(&dev_priv->sb_lock);
  142. divider = val & CCK_FREQUENCY_VALUES;
  143. WARN((val & CCK_FREQUENCY_STATUS) !=
  144. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  145. "%s change in progress\n", name);
  146. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  147. }
  148. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  149. const char *name, u32 reg)
  150. {
  151. if (dev_priv->hpll_freq == 0)
  152. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  153. return vlv_get_cck_clock(dev_priv, name, reg,
  154. dev_priv->hpll_freq);
  155. }
  156. static int
  157. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  158. {
  159. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  160. }
  161. static int
  162. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  163. {
  164. /* RAWCLK_FREQ_VLV register updated from power well code */
  165. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  166. CCK_DISPLAY_REF_CLOCK_CONTROL);
  167. }
  168. static int
  169. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  170. {
  171. uint32_t clkcfg;
  172. /* hrawclock is 1/4 the FSB frequency */
  173. clkcfg = I915_READ(CLKCFG);
  174. switch (clkcfg & CLKCFG_FSB_MASK) {
  175. case CLKCFG_FSB_400:
  176. return 100000;
  177. case CLKCFG_FSB_533:
  178. return 133333;
  179. case CLKCFG_FSB_667:
  180. return 166667;
  181. case CLKCFG_FSB_800:
  182. return 200000;
  183. case CLKCFG_FSB_1067:
  184. return 266667;
  185. case CLKCFG_FSB_1333:
  186. return 333333;
  187. /* these two are just a guess; one of them might be right */
  188. case CLKCFG_FSB_1600:
  189. case CLKCFG_FSB_1600_ALT:
  190. return 400000;
  191. default:
  192. return 133333;
  193. }
  194. }
  195. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  196. {
  197. if (HAS_PCH_SPLIT(dev_priv))
  198. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  199. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  200. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  201. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  202. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  203. else
  204. return; /* no rawclk on other platforms, or no need to know it */
  205. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  206. }
  207. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  208. {
  209. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  210. return;
  211. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  212. CCK_CZ_CLOCK_CONTROL);
  213. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  214. }
  215. static inline u32 /* units of 100MHz */
  216. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  217. const struct intel_crtc_state *pipe_config)
  218. {
  219. if (HAS_DDI(dev_priv))
  220. return pipe_config->port_clock; /* SPLL */
  221. else if (IS_GEN5(dev_priv))
  222. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  223. else
  224. return 270000;
  225. }
  226. static const struct intel_limit intel_limits_i8xx_dac = {
  227. .dot = { .min = 25000, .max = 350000 },
  228. .vco = { .min = 908000, .max = 1512000 },
  229. .n = { .min = 2, .max = 16 },
  230. .m = { .min = 96, .max = 140 },
  231. .m1 = { .min = 18, .max = 26 },
  232. .m2 = { .min = 6, .max = 16 },
  233. .p = { .min = 4, .max = 128 },
  234. .p1 = { .min = 2, .max = 33 },
  235. .p2 = { .dot_limit = 165000,
  236. .p2_slow = 4, .p2_fast = 2 },
  237. };
  238. static const struct intel_limit intel_limits_i8xx_dvo = {
  239. .dot = { .min = 25000, .max = 350000 },
  240. .vco = { .min = 908000, .max = 1512000 },
  241. .n = { .min = 2, .max = 16 },
  242. .m = { .min = 96, .max = 140 },
  243. .m1 = { .min = 18, .max = 26 },
  244. .m2 = { .min = 6, .max = 16 },
  245. .p = { .min = 4, .max = 128 },
  246. .p1 = { .min = 2, .max = 33 },
  247. .p2 = { .dot_limit = 165000,
  248. .p2_slow = 4, .p2_fast = 4 },
  249. };
  250. static const struct intel_limit intel_limits_i8xx_lvds = {
  251. .dot = { .min = 25000, .max = 350000 },
  252. .vco = { .min = 908000, .max = 1512000 },
  253. .n = { .min = 2, .max = 16 },
  254. .m = { .min = 96, .max = 140 },
  255. .m1 = { .min = 18, .max = 26 },
  256. .m2 = { .min = 6, .max = 16 },
  257. .p = { .min = 4, .max = 128 },
  258. .p1 = { .min = 1, .max = 6 },
  259. .p2 = { .dot_limit = 165000,
  260. .p2_slow = 14, .p2_fast = 7 },
  261. };
  262. static const struct intel_limit intel_limits_i9xx_sdvo = {
  263. .dot = { .min = 20000, .max = 400000 },
  264. .vco = { .min = 1400000, .max = 2800000 },
  265. .n = { .min = 1, .max = 6 },
  266. .m = { .min = 70, .max = 120 },
  267. .m1 = { .min = 8, .max = 18 },
  268. .m2 = { .min = 3, .max = 7 },
  269. .p = { .min = 5, .max = 80 },
  270. .p1 = { .min = 1, .max = 8 },
  271. .p2 = { .dot_limit = 200000,
  272. .p2_slow = 10, .p2_fast = 5 },
  273. };
  274. static const struct intel_limit intel_limits_i9xx_lvds = {
  275. .dot = { .min = 20000, .max = 400000 },
  276. .vco = { .min = 1400000, .max = 2800000 },
  277. .n = { .min = 1, .max = 6 },
  278. .m = { .min = 70, .max = 120 },
  279. .m1 = { .min = 8, .max = 18 },
  280. .m2 = { .min = 3, .max = 7 },
  281. .p = { .min = 7, .max = 98 },
  282. .p1 = { .min = 1, .max = 8 },
  283. .p2 = { .dot_limit = 112000,
  284. .p2_slow = 14, .p2_fast = 7 },
  285. };
  286. static const struct intel_limit intel_limits_g4x_sdvo = {
  287. .dot = { .min = 25000, .max = 270000 },
  288. .vco = { .min = 1750000, .max = 3500000},
  289. .n = { .min = 1, .max = 4 },
  290. .m = { .min = 104, .max = 138 },
  291. .m1 = { .min = 17, .max = 23 },
  292. .m2 = { .min = 5, .max = 11 },
  293. .p = { .min = 10, .max = 30 },
  294. .p1 = { .min = 1, .max = 3},
  295. .p2 = { .dot_limit = 270000,
  296. .p2_slow = 10,
  297. .p2_fast = 10
  298. },
  299. };
  300. static const struct intel_limit intel_limits_g4x_hdmi = {
  301. .dot = { .min = 22000, .max = 400000 },
  302. .vco = { .min = 1750000, .max = 3500000},
  303. .n = { .min = 1, .max = 4 },
  304. .m = { .min = 104, .max = 138 },
  305. .m1 = { .min = 16, .max = 23 },
  306. .m2 = { .min = 5, .max = 11 },
  307. .p = { .min = 5, .max = 80 },
  308. .p1 = { .min = 1, .max = 8},
  309. .p2 = { .dot_limit = 165000,
  310. .p2_slow = 10, .p2_fast = 5 },
  311. };
  312. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  313. .dot = { .min = 20000, .max = 115000 },
  314. .vco = { .min = 1750000, .max = 3500000 },
  315. .n = { .min = 1, .max = 3 },
  316. .m = { .min = 104, .max = 138 },
  317. .m1 = { .min = 17, .max = 23 },
  318. .m2 = { .min = 5, .max = 11 },
  319. .p = { .min = 28, .max = 112 },
  320. .p1 = { .min = 2, .max = 8 },
  321. .p2 = { .dot_limit = 0,
  322. .p2_slow = 14, .p2_fast = 14
  323. },
  324. };
  325. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  326. .dot = { .min = 80000, .max = 224000 },
  327. .vco = { .min = 1750000, .max = 3500000 },
  328. .n = { .min = 1, .max = 3 },
  329. .m = { .min = 104, .max = 138 },
  330. .m1 = { .min = 17, .max = 23 },
  331. .m2 = { .min = 5, .max = 11 },
  332. .p = { .min = 14, .max = 42 },
  333. .p1 = { .min = 2, .max = 6 },
  334. .p2 = { .dot_limit = 0,
  335. .p2_slow = 7, .p2_fast = 7
  336. },
  337. };
  338. static const struct intel_limit intel_limits_pineview_sdvo = {
  339. .dot = { .min = 20000, .max = 400000},
  340. .vco = { .min = 1700000, .max = 3500000 },
  341. /* Pineview's Ncounter is a ring counter */
  342. .n = { .min = 3, .max = 6 },
  343. .m = { .min = 2, .max = 256 },
  344. /* Pineview only has one combined m divider, which we treat as m2. */
  345. .m1 = { .min = 0, .max = 0 },
  346. .m2 = { .min = 0, .max = 254 },
  347. .p = { .min = 5, .max = 80 },
  348. .p1 = { .min = 1, .max = 8 },
  349. .p2 = { .dot_limit = 200000,
  350. .p2_slow = 10, .p2_fast = 5 },
  351. };
  352. static const struct intel_limit intel_limits_pineview_lvds = {
  353. .dot = { .min = 20000, .max = 400000 },
  354. .vco = { .min = 1700000, .max = 3500000 },
  355. .n = { .min = 3, .max = 6 },
  356. .m = { .min = 2, .max = 256 },
  357. .m1 = { .min = 0, .max = 0 },
  358. .m2 = { .min = 0, .max = 254 },
  359. .p = { .min = 7, .max = 112 },
  360. .p1 = { .min = 1, .max = 8 },
  361. .p2 = { .dot_limit = 112000,
  362. .p2_slow = 14, .p2_fast = 14 },
  363. };
  364. /* Ironlake / Sandybridge
  365. *
  366. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  367. * the range value for them is (actual_value - 2).
  368. */
  369. static const struct intel_limit intel_limits_ironlake_dac = {
  370. .dot = { .min = 25000, .max = 350000 },
  371. .vco = { .min = 1760000, .max = 3510000 },
  372. .n = { .min = 1, .max = 5 },
  373. .m = { .min = 79, .max = 127 },
  374. .m1 = { .min = 12, .max = 22 },
  375. .m2 = { .min = 5, .max = 9 },
  376. .p = { .min = 5, .max = 80 },
  377. .p1 = { .min = 1, .max = 8 },
  378. .p2 = { .dot_limit = 225000,
  379. .p2_slow = 10, .p2_fast = 5 },
  380. };
  381. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  382. .dot = { .min = 25000, .max = 350000 },
  383. .vco = { .min = 1760000, .max = 3510000 },
  384. .n = { .min = 1, .max = 3 },
  385. .m = { .min = 79, .max = 118 },
  386. .m1 = { .min = 12, .max = 22 },
  387. .m2 = { .min = 5, .max = 9 },
  388. .p = { .min = 28, .max = 112 },
  389. .p1 = { .min = 2, .max = 8 },
  390. .p2 = { .dot_limit = 225000,
  391. .p2_slow = 14, .p2_fast = 14 },
  392. };
  393. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  394. .dot = { .min = 25000, .max = 350000 },
  395. .vco = { .min = 1760000, .max = 3510000 },
  396. .n = { .min = 1, .max = 3 },
  397. .m = { .min = 79, .max = 127 },
  398. .m1 = { .min = 12, .max = 22 },
  399. .m2 = { .min = 5, .max = 9 },
  400. .p = { .min = 14, .max = 56 },
  401. .p1 = { .min = 2, .max = 8 },
  402. .p2 = { .dot_limit = 225000,
  403. .p2_slow = 7, .p2_fast = 7 },
  404. };
  405. /* LVDS 100mhz refclk limits. */
  406. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  407. .dot = { .min = 25000, .max = 350000 },
  408. .vco = { .min = 1760000, .max = 3510000 },
  409. .n = { .min = 1, .max = 2 },
  410. .m = { .min = 79, .max = 126 },
  411. .m1 = { .min = 12, .max = 22 },
  412. .m2 = { .min = 5, .max = 9 },
  413. .p = { .min = 28, .max = 112 },
  414. .p1 = { .min = 2, .max = 8 },
  415. .p2 = { .dot_limit = 225000,
  416. .p2_slow = 14, .p2_fast = 14 },
  417. };
  418. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  419. .dot = { .min = 25000, .max = 350000 },
  420. .vco = { .min = 1760000, .max = 3510000 },
  421. .n = { .min = 1, .max = 3 },
  422. .m = { .min = 79, .max = 126 },
  423. .m1 = { .min = 12, .max = 22 },
  424. .m2 = { .min = 5, .max = 9 },
  425. .p = { .min = 14, .max = 42 },
  426. .p1 = { .min = 2, .max = 6 },
  427. .p2 = { .dot_limit = 225000,
  428. .p2_slow = 7, .p2_fast = 7 },
  429. };
  430. static const struct intel_limit intel_limits_vlv = {
  431. /*
  432. * These are the data rate limits (measured in fast clocks)
  433. * since those are the strictest limits we have. The fast
  434. * clock and actual rate limits are more relaxed, so checking
  435. * them would make no difference.
  436. */
  437. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  438. .vco = { .min = 4000000, .max = 6000000 },
  439. .n = { .min = 1, .max = 7 },
  440. .m1 = { .min = 2, .max = 3 },
  441. .m2 = { .min = 11, .max = 156 },
  442. .p1 = { .min = 2, .max = 3 },
  443. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  444. };
  445. static const struct intel_limit intel_limits_chv = {
  446. /*
  447. * These are the data rate limits (measured in fast clocks)
  448. * since those are the strictest limits we have. The fast
  449. * clock and actual rate limits are more relaxed, so checking
  450. * them would make no difference.
  451. */
  452. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  453. .vco = { .min = 4800000, .max = 6480000 },
  454. .n = { .min = 1, .max = 1 },
  455. .m1 = { .min = 2, .max = 2 },
  456. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  457. .p1 = { .min = 2, .max = 4 },
  458. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  459. };
  460. static const struct intel_limit intel_limits_bxt = {
  461. /* FIXME: find real dot limits */
  462. .dot = { .min = 0, .max = INT_MAX },
  463. .vco = { .min = 4800000, .max = 6700000 },
  464. .n = { .min = 1, .max = 1 },
  465. .m1 = { .min = 2, .max = 2 },
  466. /* FIXME: find real m2 limits */
  467. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  468. .p1 = { .min = 2, .max = 4 },
  469. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  470. };
  471. static bool
  472. needs_modeset(struct drm_crtc_state *state)
  473. {
  474. return drm_atomic_crtc_needs_modeset(state);
  475. }
  476. /**
  477. * Returns whether any output on the specified pipe is of the specified type
  478. */
  479. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  480. {
  481. struct drm_device *dev = crtc->base.dev;
  482. struct intel_encoder *encoder;
  483. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  484. if (encoder->type == type)
  485. return true;
  486. return false;
  487. }
  488. /**
  489. * Returns whether any output on the specified pipe will have the specified
  490. * type after a staged modeset is complete, i.e., the same as
  491. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  492. * encoder->crtc.
  493. */
  494. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  495. int type)
  496. {
  497. struct drm_atomic_state *state = crtc_state->base.state;
  498. struct drm_connector *connector;
  499. struct drm_connector_state *connector_state;
  500. struct intel_encoder *encoder;
  501. int i, num_connectors = 0;
  502. for_each_connector_in_state(state, connector, connector_state, i) {
  503. if (connector_state->crtc != crtc_state->base.crtc)
  504. continue;
  505. num_connectors++;
  506. encoder = to_intel_encoder(connector_state->best_encoder);
  507. if (encoder->type == type)
  508. return true;
  509. }
  510. WARN_ON(num_connectors == 0);
  511. return false;
  512. }
  513. /*
  514. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  515. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  516. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  517. * The helpers' return value is the rate of the clock that is fed to the
  518. * display engine's pipe which can be the above fast dot clock rate or a
  519. * divided-down version of it.
  520. */
  521. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  522. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  523. {
  524. clock->m = clock->m2 + 2;
  525. clock->p = clock->p1 * clock->p2;
  526. if (WARN_ON(clock->n == 0 || clock->p == 0))
  527. return 0;
  528. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  529. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  530. return clock->dot;
  531. }
  532. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  533. {
  534. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  535. }
  536. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  537. {
  538. clock->m = i9xx_dpll_compute_m(clock);
  539. clock->p = clock->p1 * clock->p2;
  540. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  541. return 0;
  542. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  543. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  544. return clock->dot;
  545. }
  546. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  547. {
  548. clock->m = clock->m1 * clock->m2;
  549. clock->p = clock->p1 * clock->p2;
  550. if (WARN_ON(clock->n == 0 || clock->p == 0))
  551. return 0;
  552. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  553. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  554. return clock->dot / 5;
  555. }
  556. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  557. {
  558. clock->m = clock->m1 * clock->m2;
  559. clock->p = clock->p1 * clock->p2;
  560. if (WARN_ON(clock->n == 0 || clock->p == 0))
  561. return 0;
  562. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  563. clock->n << 22);
  564. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  565. return clock->dot / 5;
  566. }
  567. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  568. /**
  569. * Returns whether the given set of divisors are valid for a given refclk with
  570. * the given connectors.
  571. */
  572. static bool intel_PLL_is_valid(struct drm_device *dev,
  573. const struct intel_limit *limit,
  574. const struct dpll *clock)
  575. {
  576. if (clock->n < limit->n.min || limit->n.max < clock->n)
  577. INTELPllInvalid("n out of range\n");
  578. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  579. INTELPllInvalid("p1 out of range\n");
  580. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  581. INTELPllInvalid("m2 out of range\n");
  582. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  583. INTELPllInvalid("m1 out of range\n");
  584. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  585. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  586. if (clock->m1 <= clock->m2)
  587. INTELPllInvalid("m1 <= m2\n");
  588. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  589. if (clock->p < limit->p.min || limit->p.max < clock->p)
  590. INTELPllInvalid("p out of range\n");
  591. if (clock->m < limit->m.min || limit->m.max < clock->m)
  592. INTELPllInvalid("m out of range\n");
  593. }
  594. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  595. INTELPllInvalid("vco out of range\n");
  596. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  597. * connector, etc., rather than just a single range.
  598. */
  599. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  600. INTELPllInvalid("dot out of range\n");
  601. return true;
  602. }
  603. static int
  604. i9xx_select_p2_div(const struct intel_limit *limit,
  605. const struct intel_crtc_state *crtc_state,
  606. int target)
  607. {
  608. struct drm_device *dev = crtc_state->base.crtc->dev;
  609. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  610. /*
  611. * For LVDS just rely on its current settings for dual-channel.
  612. * We haven't figured out how to reliably set up different
  613. * single/dual channel state, if we even can.
  614. */
  615. if (intel_is_dual_link_lvds(dev))
  616. return limit->p2.p2_fast;
  617. else
  618. return limit->p2.p2_slow;
  619. } else {
  620. if (target < limit->p2.dot_limit)
  621. return limit->p2.p2_slow;
  622. else
  623. return limit->p2.p2_fast;
  624. }
  625. }
  626. /*
  627. * Returns a set of divisors for the desired target clock with the given
  628. * refclk, or FALSE. The returned values represent the clock equation:
  629. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  630. *
  631. * Target and reference clocks are specified in kHz.
  632. *
  633. * If match_clock is provided, then best_clock P divider must match the P
  634. * divider from @match_clock used for LVDS downclocking.
  635. */
  636. static bool
  637. i9xx_find_best_dpll(const struct intel_limit *limit,
  638. struct intel_crtc_state *crtc_state,
  639. int target, int refclk, struct dpll *match_clock,
  640. struct dpll *best_clock)
  641. {
  642. struct drm_device *dev = crtc_state->base.crtc->dev;
  643. struct dpll clock;
  644. int err = target;
  645. memset(best_clock, 0, sizeof(*best_clock));
  646. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  647. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  648. clock.m1++) {
  649. for (clock.m2 = limit->m2.min;
  650. clock.m2 <= limit->m2.max; clock.m2++) {
  651. if (clock.m2 >= clock.m1)
  652. break;
  653. for (clock.n = limit->n.min;
  654. clock.n <= limit->n.max; clock.n++) {
  655. for (clock.p1 = limit->p1.min;
  656. clock.p1 <= limit->p1.max; clock.p1++) {
  657. int this_err;
  658. i9xx_calc_dpll_params(refclk, &clock);
  659. if (!intel_PLL_is_valid(dev, limit,
  660. &clock))
  661. continue;
  662. if (match_clock &&
  663. clock.p != match_clock->p)
  664. continue;
  665. this_err = abs(clock.dot - target);
  666. if (this_err < err) {
  667. *best_clock = clock;
  668. err = this_err;
  669. }
  670. }
  671. }
  672. }
  673. }
  674. return (err != target);
  675. }
  676. /*
  677. * Returns a set of divisors for the desired target clock with the given
  678. * refclk, or FALSE. The returned values represent the clock equation:
  679. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  680. *
  681. * Target and reference clocks are specified in kHz.
  682. *
  683. * If match_clock is provided, then best_clock P divider must match the P
  684. * divider from @match_clock used for LVDS downclocking.
  685. */
  686. static bool
  687. pnv_find_best_dpll(const struct intel_limit *limit,
  688. struct intel_crtc_state *crtc_state,
  689. int target, int refclk, struct dpll *match_clock,
  690. struct dpll *best_clock)
  691. {
  692. struct drm_device *dev = crtc_state->base.crtc->dev;
  693. struct dpll clock;
  694. int err = target;
  695. memset(best_clock, 0, sizeof(*best_clock));
  696. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  697. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  698. clock.m1++) {
  699. for (clock.m2 = limit->m2.min;
  700. clock.m2 <= limit->m2.max; clock.m2++) {
  701. for (clock.n = limit->n.min;
  702. clock.n <= limit->n.max; clock.n++) {
  703. for (clock.p1 = limit->p1.min;
  704. clock.p1 <= limit->p1.max; clock.p1++) {
  705. int this_err;
  706. pnv_calc_dpll_params(refclk, &clock);
  707. if (!intel_PLL_is_valid(dev, limit,
  708. &clock))
  709. continue;
  710. if (match_clock &&
  711. clock.p != match_clock->p)
  712. continue;
  713. this_err = abs(clock.dot - target);
  714. if (this_err < err) {
  715. *best_clock = clock;
  716. err = this_err;
  717. }
  718. }
  719. }
  720. }
  721. }
  722. return (err != target);
  723. }
  724. /*
  725. * Returns a set of divisors for the desired target clock with the given
  726. * refclk, or FALSE. The returned values represent the clock equation:
  727. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  728. *
  729. * Target and reference clocks are specified in kHz.
  730. *
  731. * If match_clock is provided, then best_clock P divider must match the P
  732. * divider from @match_clock used for LVDS downclocking.
  733. */
  734. static bool
  735. g4x_find_best_dpll(const struct intel_limit *limit,
  736. struct intel_crtc_state *crtc_state,
  737. int target, int refclk, struct dpll *match_clock,
  738. struct dpll *best_clock)
  739. {
  740. struct drm_device *dev = crtc_state->base.crtc->dev;
  741. struct dpll clock;
  742. int max_n;
  743. bool found = false;
  744. /* approximately equals target * 0.00585 */
  745. int err_most = (target >> 8) + (target >> 9);
  746. memset(best_clock, 0, sizeof(*best_clock));
  747. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  748. max_n = limit->n.max;
  749. /* based on hardware requirement, prefer smaller n to precision */
  750. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  751. /* based on hardware requirement, prefere larger m1,m2 */
  752. for (clock.m1 = limit->m1.max;
  753. clock.m1 >= limit->m1.min; clock.m1--) {
  754. for (clock.m2 = limit->m2.max;
  755. clock.m2 >= limit->m2.min; clock.m2--) {
  756. for (clock.p1 = limit->p1.max;
  757. clock.p1 >= limit->p1.min; clock.p1--) {
  758. int this_err;
  759. i9xx_calc_dpll_params(refclk, &clock);
  760. if (!intel_PLL_is_valid(dev, limit,
  761. &clock))
  762. continue;
  763. this_err = abs(clock.dot - target);
  764. if (this_err < err_most) {
  765. *best_clock = clock;
  766. err_most = this_err;
  767. max_n = clock.n;
  768. found = true;
  769. }
  770. }
  771. }
  772. }
  773. }
  774. return found;
  775. }
  776. /*
  777. * Check if the calculated PLL configuration is more optimal compared to the
  778. * best configuration and error found so far. Return the calculated error.
  779. */
  780. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  781. const struct dpll *calculated_clock,
  782. const struct dpll *best_clock,
  783. unsigned int best_error_ppm,
  784. unsigned int *error_ppm)
  785. {
  786. /*
  787. * For CHV ignore the error and consider only the P value.
  788. * Prefer a bigger P value based on HW requirements.
  789. */
  790. if (IS_CHERRYVIEW(dev)) {
  791. *error_ppm = 0;
  792. return calculated_clock->p > best_clock->p;
  793. }
  794. if (WARN_ON_ONCE(!target_freq))
  795. return false;
  796. *error_ppm = div_u64(1000000ULL *
  797. abs(target_freq - calculated_clock->dot),
  798. target_freq);
  799. /*
  800. * Prefer a better P value over a better (smaller) error if the error
  801. * is small. Ensure this preference for future configurations too by
  802. * setting the error to 0.
  803. */
  804. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  805. *error_ppm = 0;
  806. return true;
  807. }
  808. return *error_ppm + 10 < best_error_ppm;
  809. }
  810. /*
  811. * Returns a set of divisors for the desired target clock with the given
  812. * refclk, or FALSE. The returned values represent the clock equation:
  813. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  814. */
  815. static bool
  816. vlv_find_best_dpll(const struct intel_limit *limit,
  817. struct intel_crtc_state *crtc_state,
  818. int target, int refclk, struct dpll *match_clock,
  819. struct dpll *best_clock)
  820. {
  821. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  822. struct drm_device *dev = crtc->base.dev;
  823. struct dpll clock;
  824. unsigned int bestppm = 1000000;
  825. /* min update 19.2 MHz */
  826. int max_n = min(limit->n.max, refclk / 19200);
  827. bool found = false;
  828. target *= 5; /* fast clock */
  829. memset(best_clock, 0, sizeof(*best_clock));
  830. /* based on hardware requirement, prefer smaller n to precision */
  831. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  832. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  833. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  834. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  835. clock.p = clock.p1 * clock.p2;
  836. /* based on hardware requirement, prefer bigger m1,m2 values */
  837. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  838. unsigned int ppm;
  839. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  840. refclk * clock.m1);
  841. vlv_calc_dpll_params(refclk, &clock);
  842. if (!intel_PLL_is_valid(dev, limit,
  843. &clock))
  844. continue;
  845. if (!vlv_PLL_is_optimal(dev, target,
  846. &clock,
  847. best_clock,
  848. bestppm, &ppm))
  849. continue;
  850. *best_clock = clock;
  851. bestppm = ppm;
  852. found = true;
  853. }
  854. }
  855. }
  856. }
  857. return found;
  858. }
  859. /*
  860. * Returns a set of divisors for the desired target clock with the given
  861. * refclk, or FALSE. The returned values represent the clock equation:
  862. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  863. */
  864. static bool
  865. chv_find_best_dpll(const struct intel_limit *limit,
  866. struct intel_crtc_state *crtc_state,
  867. int target, int refclk, struct dpll *match_clock,
  868. struct dpll *best_clock)
  869. {
  870. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  871. struct drm_device *dev = crtc->base.dev;
  872. unsigned int best_error_ppm;
  873. struct dpll clock;
  874. uint64_t m2;
  875. int found = false;
  876. memset(best_clock, 0, sizeof(*best_clock));
  877. best_error_ppm = 1000000;
  878. /*
  879. * Based on hardware doc, the n always set to 1, and m1 always
  880. * set to 2. If requires to support 200Mhz refclk, we need to
  881. * revisit this because n may not 1 anymore.
  882. */
  883. clock.n = 1, clock.m1 = 2;
  884. target *= 5; /* fast clock */
  885. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  886. for (clock.p2 = limit->p2.p2_fast;
  887. clock.p2 >= limit->p2.p2_slow;
  888. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  889. unsigned int error_ppm;
  890. clock.p = clock.p1 * clock.p2;
  891. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  892. clock.n) << 22, refclk * clock.m1);
  893. if (m2 > INT_MAX/clock.m1)
  894. continue;
  895. clock.m2 = m2;
  896. chv_calc_dpll_params(refclk, &clock);
  897. if (!intel_PLL_is_valid(dev, limit, &clock))
  898. continue;
  899. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  900. best_error_ppm, &error_ppm))
  901. continue;
  902. *best_clock = clock;
  903. best_error_ppm = error_ppm;
  904. found = true;
  905. }
  906. }
  907. return found;
  908. }
  909. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  910. struct dpll *best_clock)
  911. {
  912. int refclk = 100000;
  913. const struct intel_limit *limit = &intel_limits_bxt;
  914. return chv_find_best_dpll(limit, crtc_state,
  915. target_clock, refclk, NULL, best_clock);
  916. }
  917. bool intel_crtc_active(struct drm_crtc *crtc)
  918. {
  919. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  920. /* Be paranoid as we can arrive here with only partial
  921. * state retrieved from the hardware during setup.
  922. *
  923. * We can ditch the adjusted_mode.crtc_clock check as soon
  924. * as Haswell has gained clock readout/fastboot support.
  925. *
  926. * We can ditch the crtc->primary->fb check as soon as we can
  927. * properly reconstruct framebuffers.
  928. *
  929. * FIXME: The intel_crtc->active here should be switched to
  930. * crtc->state->active once we have proper CRTC states wired up
  931. * for atomic.
  932. */
  933. return intel_crtc->active && crtc->primary->state->fb &&
  934. intel_crtc->config->base.adjusted_mode.crtc_clock;
  935. }
  936. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  937. enum pipe pipe)
  938. {
  939. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  941. return intel_crtc->config->cpu_transcoder;
  942. }
  943. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  944. {
  945. struct drm_i915_private *dev_priv = dev->dev_private;
  946. i915_reg_t reg = PIPEDSL(pipe);
  947. u32 line1, line2;
  948. u32 line_mask;
  949. if (IS_GEN2(dev))
  950. line_mask = DSL_LINEMASK_GEN2;
  951. else
  952. line_mask = DSL_LINEMASK_GEN3;
  953. line1 = I915_READ(reg) & line_mask;
  954. msleep(5);
  955. line2 = I915_READ(reg) & line_mask;
  956. return line1 == line2;
  957. }
  958. /*
  959. * intel_wait_for_pipe_off - wait for pipe to turn off
  960. * @crtc: crtc whose pipe to wait for
  961. *
  962. * After disabling a pipe, we can't wait for vblank in the usual way,
  963. * spinning on the vblank interrupt status bit, since we won't actually
  964. * see an interrupt when the pipe is disabled.
  965. *
  966. * On Gen4 and above:
  967. * wait for the pipe register state bit to turn off
  968. *
  969. * Otherwise:
  970. * wait for the display line value to settle (it usually
  971. * ends up stopping at the start of the next frame).
  972. *
  973. */
  974. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  975. {
  976. struct drm_device *dev = crtc->base.dev;
  977. struct drm_i915_private *dev_priv = dev->dev_private;
  978. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  979. enum pipe pipe = crtc->pipe;
  980. if (INTEL_INFO(dev)->gen >= 4) {
  981. i915_reg_t reg = PIPECONF(cpu_transcoder);
  982. /* Wait for the Pipe State to go off */
  983. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  984. 100))
  985. WARN(1, "pipe_off wait timed out\n");
  986. } else {
  987. /* Wait for the display line to settle */
  988. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  989. WARN(1, "pipe_off wait timed out\n");
  990. }
  991. }
  992. /* Only for pre-ILK configs */
  993. void assert_pll(struct drm_i915_private *dev_priv,
  994. enum pipe pipe, bool state)
  995. {
  996. u32 val;
  997. bool cur_state;
  998. val = I915_READ(DPLL(pipe));
  999. cur_state = !!(val & DPLL_VCO_ENABLE);
  1000. I915_STATE_WARN(cur_state != state,
  1001. "PLL state assertion failure (expected %s, current %s)\n",
  1002. onoff(state), onoff(cur_state));
  1003. }
  1004. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1005. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1006. {
  1007. u32 val;
  1008. bool cur_state;
  1009. mutex_lock(&dev_priv->sb_lock);
  1010. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1011. mutex_unlock(&dev_priv->sb_lock);
  1012. cur_state = val & DSI_PLL_VCO_EN;
  1013. I915_STATE_WARN(cur_state != state,
  1014. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1015. onoff(state), onoff(cur_state));
  1016. }
  1017. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1018. enum pipe pipe, bool state)
  1019. {
  1020. bool cur_state;
  1021. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1022. pipe);
  1023. if (HAS_DDI(dev_priv)) {
  1024. /* DDI does not have a specific FDI_TX register */
  1025. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1026. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1027. } else {
  1028. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1029. cur_state = !!(val & FDI_TX_ENABLE);
  1030. }
  1031. I915_STATE_WARN(cur_state != state,
  1032. "FDI TX state assertion failure (expected %s, current %s)\n",
  1033. onoff(state), onoff(cur_state));
  1034. }
  1035. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1036. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1037. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1038. enum pipe pipe, bool state)
  1039. {
  1040. u32 val;
  1041. bool cur_state;
  1042. val = I915_READ(FDI_RX_CTL(pipe));
  1043. cur_state = !!(val & FDI_RX_ENABLE);
  1044. I915_STATE_WARN(cur_state != state,
  1045. "FDI RX state assertion failure (expected %s, current %s)\n",
  1046. onoff(state), onoff(cur_state));
  1047. }
  1048. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1049. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1050. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1051. enum pipe pipe)
  1052. {
  1053. u32 val;
  1054. /* ILK FDI PLL is always enabled */
  1055. if (IS_GEN5(dev_priv))
  1056. return;
  1057. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1058. if (HAS_DDI(dev_priv))
  1059. return;
  1060. val = I915_READ(FDI_TX_CTL(pipe));
  1061. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1062. }
  1063. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1064. enum pipe pipe, bool state)
  1065. {
  1066. u32 val;
  1067. bool cur_state;
  1068. val = I915_READ(FDI_RX_CTL(pipe));
  1069. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1070. I915_STATE_WARN(cur_state != state,
  1071. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1072. onoff(state), onoff(cur_state));
  1073. }
  1074. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe)
  1076. {
  1077. struct drm_device *dev = dev_priv->dev;
  1078. i915_reg_t pp_reg;
  1079. u32 val;
  1080. enum pipe panel_pipe = PIPE_A;
  1081. bool locked = true;
  1082. if (WARN_ON(HAS_DDI(dev)))
  1083. return;
  1084. if (HAS_PCH_SPLIT(dev)) {
  1085. u32 port_sel;
  1086. pp_reg = PCH_PP_CONTROL;
  1087. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1088. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1089. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1090. panel_pipe = PIPE_B;
  1091. /* XXX: else fix for eDP */
  1092. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1093. /* presumably write lock depends on pipe, not port select */
  1094. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1095. panel_pipe = pipe;
  1096. } else {
  1097. pp_reg = PP_CONTROL;
  1098. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1099. panel_pipe = PIPE_B;
  1100. }
  1101. val = I915_READ(pp_reg);
  1102. if (!(val & PANEL_POWER_ON) ||
  1103. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1104. locked = false;
  1105. I915_STATE_WARN(panel_pipe == pipe && locked,
  1106. "panel assertion failure, pipe %c regs locked\n",
  1107. pipe_name(pipe));
  1108. }
  1109. static void assert_cursor(struct drm_i915_private *dev_priv,
  1110. enum pipe pipe, bool state)
  1111. {
  1112. struct drm_device *dev = dev_priv->dev;
  1113. bool cur_state;
  1114. if (IS_845G(dev) || IS_I865G(dev))
  1115. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1116. else
  1117. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1118. I915_STATE_WARN(cur_state != state,
  1119. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1120. pipe_name(pipe), onoff(state), onoff(cur_state));
  1121. }
  1122. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1123. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1124. void assert_pipe(struct drm_i915_private *dev_priv,
  1125. enum pipe pipe, bool state)
  1126. {
  1127. bool cur_state;
  1128. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1129. pipe);
  1130. enum intel_display_power_domain power_domain;
  1131. /* if we need the pipe quirk it must be always on */
  1132. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1133. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1134. state = true;
  1135. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1136. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1137. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1138. cur_state = !!(val & PIPECONF_ENABLE);
  1139. intel_display_power_put(dev_priv, power_domain);
  1140. } else {
  1141. cur_state = false;
  1142. }
  1143. I915_STATE_WARN(cur_state != state,
  1144. "pipe %c assertion failure (expected %s, current %s)\n",
  1145. pipe_name(pipe), onoff(state), onoff(cur_state));
  1146. }
  1147. static void assert_plane(struct drm_i915_private *dev_priv,
  1148. enum plane plane, bool state)
  1149. {
  1150. u32 val;
  1151. bool cur_state;
  1152. val = I915_READ(DSPCNTR(plane));
  1153. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1154. I915_STATE_WARN(cur_state != state,
  1155. "plane %c assertion failure (expected %s, current %s)\n",
  1156. plane_name(plane), onoff(state), onoff(cur_state));
  1157. }
  1158. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1159. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1160. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe)
  1162. {
  1163. struct drm_device *dev = dev_priv->dev;
  1164. int i;
  1165. /* Primary planes are fixed to pipes on gen4+ */
  1166. if (INTEL_INFO(dev)->gen >= 4) {
  1167. u32 val = I915_READ(DSPCNTR(pipe));
  1168. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1169. "plane %c assertion failure, should be disabled but not\n",
  1170. plane_name(pipe));
  1171. return;
  1172. }
  1173. /* Need to check both planes against the pipe */
  1174. for_each_pipe(dev_priv, i) {
  1175. u32 val = I915_READ(DSPCNTR(i));
  1176. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1177. DISPPLANE_SEL_PIPE_SHIFT;
  1178. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1179. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1180. plane_name(i), pipe_name(pipe));
  1181. }
  1182. }
  1183. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1184. enum pipe pipe)
  1185. {
  1186. struct drm_device *dev = dev_priv->dev;
  1187. int sprite;
  1188. if (INTEL_INFO(dev)->gen >= 9) {
  1189. for_each_sprite(dev_priv, pipe, sprite) {
  1190. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1191. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1192. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1193. sprite, pipe_name(pipe));
  1194. }
  1195. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1196. for_each_sprite(dev_priv, pipe, sprite) {
  1197. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1198. I915_STATE_WARN(val & SP_ENABLE,
  1199. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1200. sprite_name(pipe, sprite), pipe_name(pipe));
  1201. }
  1202. } else if (INTEL_INFO(dev)->gen >= 7) {
  1203. u32 val = I915_READ(SPRCTL(pipe));
  1204. I915_STATE_WARN(val & SPRITE_ENABLE,
  1205. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1206. plane_name(pipe), pipe_name(pipe));
  1207. } else if (INTEL_INFO(dev)->gen >= 5) {
  1208. u32 val = I915_READ(DVSCNTR(pipe));
  1209. I915_STATE_WARN(val & DVS_ENABLE,
  1210. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1211. plane_name(pipe), pipe_name(pipe));
  1212. }
  1213. }
  1214. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1215. {
  1216. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1217. drm_crtc_vblank_put(crtc);
  1218. }
  1219. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1220. enum pipe pipe)
  1221. {
  1222. u32 val;
  1223. bool enabled;
  1224. val = I915_READ(PCH_TRANSCONF(pipe));
  1225. enabled = !!(val & TRANS_ENABLE);
  1226. I915_STATE_WARN(enabled,
  1227. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1228. pipe_name(pipe));
  1229. }
  1230. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1231. enum pipe pipe, u32 port_sel, u32 val)
  1232. {
  1233. if ((val & DP_PORT_EN) == 0)
  1234. return false;
  1235. if (HAS_PCH_CPT(dev_priv)) {
  1236. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1237. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1238. return false;
  1239. } else if (IS_CHERRYVIEW(dev_priv)) {
  1240. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1241. return false;
  1242. } else {
  1243. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1244. return false;
  1245. }
  1246. return true;
  1247. }
  1248. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1249. enum pipe pipe, u32 val)
  1250. {
  1251. if ((val & SDVO_ENABLE) == 0)
  1252. return false;
  1253. if (HAS_PCH_CPT(dev_priv)) {
  1254. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1255. return false;
  1256. } else if (IS_CHERRYVIEW(dev_priv)) {
  1257. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1258. return false;
  1259. } else {
  1260. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1261. return false;
  1262. }
  1263. return true;
  1264. }
  1265. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe, u32 val)
  1267. {
  1268. if ((val & LVDS_PORT_EN) == 0)
  1269. return false;
  1270. if (HAS_PCH_CPT(dev_priv)) {
  1271. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1272. return false;
  1273. } else {
  1274. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1275. return false;
  1276. }
  1277. return true;
  1278. }
  1279. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1280. enum pipe pipe, u32 val)
  1281. {
  1282. if ((val & ADPA_DAC_ENABLE) == 0)
  1283. return false;
  1284. if (HAS_PCH_CPT(dev_priv)) {
  1285. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1286. return false;
  1287. } else {
  1288. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1289. return false;
  1290. }
  1291. return true;
  1292. }
  1293. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1294. enum pipe pipe, i915_reg_t reg,
  1295. u32 port_sel)
  1296. {
  1297. u32 val = I915_READ(reg);
  1298. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1299. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1300. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1301. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1302. && (val & DP_PIPEB_SELECT),
  1303. "IBX PCH dp port still using transcoder B\n");
  1304. }
  1305. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1306. enum pipe pipe, i915_reg_t reg)
  1307. {
  1308. u32 val = I915_READ(reg);
  1309. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1310. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1311. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1312. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1313. && (val & SDVO_PIPE_B_SELECT),
  1314. "IBX PCH hdmi port still using transcoder B\n");
  1315. }
  1316. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1317. enum pipe pipe)
  1318. {
  1319. u32 val;
  1320. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1321. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1322. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1323. val = I915_READ(PCH_ADPA);
  1324. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1325. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1326. pipe_name(pipe));
  1327. val = I915_READ(PCH_LVDS);
  1328. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1329. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1330. pipe_name(pipe));
  1331. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1332. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1333. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1334. }
  1335. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1336. const struct intel_crtc_state *pipe_config)
  1337. {
  1338. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1339. enum pipe pipe = crtc->pipe;
  1340. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1341. POSTING_READ(DPLL(pipe));
  1342. udelay(150);
  1343. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1344. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1345. }
  1346. static void vlv_enable_pll(struct intel_crtc *crtc,
  1347. const struct intel_crtc_state *pipe_config)
  1348. {
  1349. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1350. enum pipe pipe = crtc->pipe;
  1351. assert_pipe_disabled(dev_priv, pipe);
  1352. /* PLL is protected by panel, make sure we can write it */
  1353. assert_panel_unlocked(dev_priv, pipe);
  1354. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1355. _vlv_enable_pll(crtc, pipe_config);
  1356. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1357. POSTING_READ(DPLL_MD(pipe));
  1358. }
  1359. static void _chv_enable_pll(struct intel_crtc *crtc,
  1360. const struct intel_crtc_state *pipe_config)
  1361. {
  1362. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1363. enum pipe pipe = crtc->pipe;
  1364. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1365. u32 tmp;
  1366. mutex_lock(&dev_priv->sb_lock);
  1367. /* Enable back the 10bit clock to display controller */
  1368. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1369. tmp |= DPIO_DCLKP_EN;
  1370. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1371. mutex_unlock(&dev_priv->sb_lock);
  1372. /*
  1373. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1374. */
  1375. udelay(1);
  1376. /* Enable PLL */
  1377. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1378. /* Check PLL is locked */
  1379. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1380. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1381. }
  1382. static void chv_enable_pll(struct intel_crtc *crtc,
  1383. const struct intel_crtc_state *pipe_config)
  1384. {
  1385. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1386. enum pipe pipe = crtc->pipe;
  1387. assert_pipe_disabled(dev_priv, pipe);
  1388. /* PLL is protected by panel, make sure we can write it */
  1389. assert_panel_unlocked(dev_priv, pipe);
  1390. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1391. _chv_enable_pll(crtc, pipe_config);
  1392. if (pipe != PIPE_A) {
  1393. /*
  1394. * WaPixelRepeatModeFixForC0:chv
  1395. *
  1396. * DPLLCMD is AWOL. Use chicken bits to propagate
  1397. * the value from DPLLBMD to either pipe B or C.
  1398. */
  1399. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1400. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1401. I915_WRITE(CBR4_VLV, 0);
  1402. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1403. /*
  1404. * DPLLB VGA mode also seems to cause problems.
  1405. * We should always have it disabled.
  1406. */
  1407. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1408. } else {
  1409. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1410. POSTING_READ(DPLL_MD(pipe));
  1411. }
  1412. }
  1413. static int intel_num_dvo_pipes(struct drm_device *dev)
  1414. {
  1415. struct intel_crtc *crtc;
  1416. int count = 0;
  1417. for_each_intel_crtc(dev, crtc)
  1418. count += crtc->base.state->active &&
  1419. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1420. return count;
  1421. }
  1422. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1423. {
  1424. struct drm_device *dev = crtc->base.dev;
  1425. struct drm_i915_private *dev_priv = dev->dev_private;
  1426. i915_reg_t reg = DPLL(crtc->pipe);
  1427. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1428. assert_pipe_disabled(dev_priv, crtc->pipe);
  1429. /* PLL is protected by panel, make sure we can write it */
  1430. if (IS_MOBILE(dev) && !IS_I830(dev))
  1431. assert_panel_unlocked(dev_priv, crtc->pipe);
  1432. /* Enable DVO 2x clock on both PLLs if necessary */
  1433. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1434. /*
  1435. * It appears to be important that we don't enable this
  1436. * for the current pipe before otherwise configuring the
  1437. * PLL. No idea how this should be handled if multiple
  1438. * DVO outputs are enabled simultaneosly.
  1439. */
  1440. dpll |= DPLL_DVO_2X_MODE;
  1441. I915_WRITE(DPLL(!crtc->pipe),
  1442. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1443. }
  1444. /*
  1445. * Apparently we need to have VGA mode enabled prior to changing
  1446. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1447. * dividers, even though the register value does change.
  1448. */
  1449. I915_WRITE(reg, 0);
  1450. I915_WRITE(reg, dpll);
  1451. /* Wait for the clocks to stabilize. */
  1452. POSTING_READ(reg);
  1453. udelay(150);
  1454. if (INTEL_INFO(dev)->gen >= 4) {
  1455. I915_WRITE(DPLL_MD(crtc->pipe),
  1456. crtc->config->dpll_hw_state.dpll_md);
  1457. } else {
  1458. /* The pixel multiplier can only be updated once the
  1459. * DPLL is enabled and the clocks are stable.
  1460. *
  1461. * So write it again.
  1462. */
  1463. I915_WRITE(reg, dpll);
  1464. }
  1465. /* We do this three times for luck */
  1466. I915_WRITE(reg, dpll);
  1467. POSTING_READ(reg);
  1468. udelay(150); /* wait for warmup */
  1469. I915_WRITE(reg, dpll);
  1470. POSTING_READ(reg);
  1471. udelay(150); /* wait for warmup */
  1472. I915_WRITE(reg, dpll);
  1473. POSTING_READ(reg);
  1474. udelay(150); /* wait for warmup */
  1475. }
  1476. /**
  1477. * i9xx_disable_pll - disable a PLL
  1478. * @dev_priv: i915 private structure
  1479. * @pipe: pipe PLL to disable
  1480. *
  1481. * Disable the PLL for @pipe, making sure the pipe is off first.
  1482. *
  1483. * Note! This is for pre-ILK only.
  1484. */
  1485. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1486. {
  1487. struct drm_device *dev = crtc->base.dev;
  1488. struct drm_i915_private *dev_priv = dev->dev_private;
  1489. enum pipe pipe = crtc->pipe;
  1490. /* Disable DVO 2x clock on both PLLs if necessary */
  1491. if (IS_I830(dev) &&
  1492. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1493. !intel_num_dvo_pipes(dev)) {
  1494. I915_WRITE(DPLL(PIPE_B),
  1495. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1496. I915_WRITE(DPLL(PIPE_A),
  1497. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1498. }
  1499. /* Don't disable pipe or pipe PLLs if needed */
  1500. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1501. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1502. return;
  1503. /* Make sure the pipe isn't still relying on us */
  1504. assert_pipe_disabled(dev_priv, pipe);
  1505. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1506. POSTING_READ(DPLL(pipe));
  1507. }
  1508. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1509. {
  1510. u32 val;
  1511. /* Make sure the pipe isn't still relying on us */
  1512. assert_pipe_disabled(dev_priv, pipe);
  1513. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1514. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1515. if (pipe != PIPE_A)
  1516. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1517. I915_WRITE(DPLL(pipe), val);
  1518. POSTING_READ(DPLL(pipe));
  1519. }
  1520. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1521. {
  1522. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1523. u32 val;
  1524. /* Make sure the pipe isn't still relying on us */
  1525. assert_pipe_disabled(dev_priv, pipe);
  1526. val = DPLL_SSC_REF_CLK_CHV |
  1527. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1528. if (pipe != PIPE_A)
  1529. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1530. I915_WRITE(DPLL(pipe), val);
  1531. POSTING_READ(DPLL(pipe));
  1532. mutex_lock(&dev_priv->sb_lock);
  1533. /* Disable 10bit clock to display controller */
  1534. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1535. val &= ~DPIO_DCLKP_EN;
  1536. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1537. mutex_unlock(&dev_priv->sb_lock);
  1538. }
  1539. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1540. struct intel_digital_port *dport,
  1541. unsigned int expected_mask)
  1542. {
  1543. u32 port_mask;
  1544. i915_reg_t dpll_reg;
  1545. switch (dport->port) {
  1546. case PORT_B:
  1547. port_mask = DPLL_PORTB_READY_MASK;
  1548. dpll_reg = DPLL(0);
  1549. break;
  1550. case PORT_C:
  1551. port_mask = DPLL_PORTC_READY_MASK;
  1552. dpll_reg = DPLL(0);
  1553. expected_mask <<= 4;
  1554. break;
  1555. case PORT_D:
  1556. port_mask = DPLL_PORTD_READY_MASK;
  1557. dpll_reg = DPIO_PHY_STATUS;
  1558. break;
  1559. default:
  1560. BUG();
  1561. }
  1562. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1563. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1564. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1565. }
  1566. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1567. enum pipe pipe)
  1568. {
  1569. struct drm_device *dev = dev_priv->dev;
  1570. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1571. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1572. i915_reg_t reg;
  1573. uint32_t val, pipeconf_val;
  1574. /* Make sure PCH DPLL is enabled */
  1575. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1576. /* FDI must be feeding us bits for PCH ports */
  1577. assert_fdi_tx_enabled(dev_priv, pipe);
  1578. assert_fdi_rx_enabled(dev_priv, pipe);
  1579. if (HAS_PCH_CPT(dev)) {
  1580. /* Workaround: Set the timing override bit before enabling the
  1581. * pch transcoder. */
  1582. reg = TRANS_CHICKEN2(pipe);
  1583. val = I915_READ(reg);
  1584. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1585. I915_WRITE(reg, val);
  1586. }
  1587. reg = PCH_TRANSCONF(pipe);
  1588. val = I915_READ(reg);
  1589. pipeconf_val = I915_READ(PIPECONF(pipe));
  1590. if (HAS_PCH_IBX(dev_priv)) {
  1591. /*
  1592. * Make the BPC in transcoder be consistent with
  1593. * that in pipeconf reg. For HDMI we must use 8bpc
  1594. * here for both 8bpc and 12bpc.
  1595. */
  1596. val &= ~PIPECONF_BPC_MASK;
  1597. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1598. val |= PIPECONF_8BPC;
  1599. else
  1600. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1601. }
  1602. val &= ~TRANS_INTERLACE_MASK;
  1603. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1604. if (HAS_PCH_IBX(dev_priv) &&
  1605. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1606. val |= TRANS_LEGACY_INTERLACED_ILK;
  1607. else
  1608. val |= TRANS_INTERLACED;
  1609. else
  1610. val |= TRANS_PROGRESSIVE;
  1611. I915_WRITE(reg, val | TRANS_ENABLE);
  1612. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1613. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1614. }
  1615. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum transcoder cpu_transcoder)
  1617. {
  1618. u32 val, pipeconf_val;
  1619. /* FDI must be feeding us bits for PCH ports */
  1620. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1621. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1622. /* Workaround: set timing override bit. */
  1623. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1624. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1625. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1626. val = TRANS_ENABLE;
  1627. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1628. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1629. PIPECONF_INTERLACED_ILK)
  1630. val |= TRANS_INTERLACED;
  1631. else
  1632. val |= TRANS_PROGRESSIVE;
  1633. I915_WRITE(LPT_TRANSCONF, val);
  1634. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1635. DRM_ERROR("Failed to enable PCH transcoder\n");
  1636. }
  1637. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1638. enum pipe pipe)
  1639. {
  1640. struct drm_device *dev = dev_priv->dev;
  1641. i915_reg_t reg;
  1642. uint32_t val;
  1643. /* FDI relies on the transcoder */
  1644. assert_fdi_tx_disabled(dev_priv, pipe);
  1645. assert_fdi_rx_disabled(dev_priv, pipe);
  1646. /* Ports must be off as well */
  1647. assert_pch_ports_disabled(dev_priv, pipe);
  1648. reg = PCH_TRANSCONF(pipe);
  1649. val = I915_READ(reg);
  1650. val &= ~TRANS_ENABLE;
  1651. I915_WRITE(reg, val);
  1652. /* wait for PCH transcoder off, transcoder state */
  1653. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1654. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1655. if (HAS_PCH_CPT(dev)) {
  1656. /* Workaround: Clear the timing override chicken bit again. */
  1657. reg = TRANS_CHICKEN2(pipe);
  1658. val = I915_READ(reg);
  1659. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1660. I915_WRITE(reg, val);
  1661. }
  1662. }
  1663. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1664. {
  1665. u32 val;
  1666. val = I915_READ(LPT_TRANSCONF);
  1667. val &= ~TRANS_ENABLE;
  1668. I915_WRITE(LPT_TRANSCONF, val);
  1669. /* wait for PCH transcoder off, transcoder state */
  1670. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1671. DRM_ERROR("Failed to disable PCH transcoder\n");
  1672. /* Workaround: clear timing override bit. */
  1673. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1674. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1675. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1676. }
  1677. /**
  1678. * intel_enable_pipe - enable a pipe, asserting requirements
  1679. * @crtc: crtc responsible for the pipe
  1680. *
  1681. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1682. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1683. */
  1684. static void intel_enable_pipe(struct intel_crtc *crtc)
  1685. {
  1686. struct drm_device *dev = crtc->base.dev;
  1687. struct drm_i915_private *dev_priv = dev->dev_private;
  1688. enum pipe pipe = crtc->pipe;
  1689. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1690. enum pipe pch_transcoder;
  1691. i915_reg_t reg;
  1692. u32 val;
  1693. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1694. assert_planes_disabled(dev_priv, pipe);
  1695. assert_cursor_disabled(dev_priv, pipe);
  1696. assert_sprites_disabled(dev_priv, pipe);
  1697. if (HAS_PCH_LPT(dev_priv))
  1698. pch_transcoder = TRANSCODER_A;
  1699. else
  1700. pch_transcoder = pipe;
  1701. /*
  1702. * A pipe without a PLL won't actually be able to drive bits from
  1703. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1704. * need the check.
  1705. */
  1706. if (HAS_GMCH_DISPLAY(dev_priv))
  1707. if (crtc->config->has_dsi_encoder)
  1708. assert_dsi_pll_enabled(dev_priv);
  1709. else
  1710. assert_pll_enabled(dev_priv, pipe);
  1711. else {
  1712. if (crtc->config->has_pch_encoder) {
  1713. /* if driving the PCH, we need FDI enabled */
  1714. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1715. assert_fdi_tx_pll_enabled(dev_priv,
  1716. (enum pipe) cpu_transcoder);
  1717. }
  1718. /* FIXME: assert CPU port conditions for SNB+ */
  1719. }
  1720. reg = PIPECONF(cpu_transcoder);
  1721. val = I915_READ(reg);
  1722. if (val & PIPECONF_ENABLE) {
  1723. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1724. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1725. return;
  1726. }
  1727. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1728. POSTING_READ(reg);
  1729. /*
  1730. * Until the pipe starts DSL will read as 0, which would cause
  1731. * an apparent vblank timestamp jump, which messes up also the
  1732. * frame count when it's derived from the timestamps. So let's
  1733. * wait for the pipe to start properly before we call
  1734. * drm_crtc_vblank_on()
  1735. */
  1736. if (dev->max_vblank_count == 0 &&
  1737. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1738. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1739. }
  1740. /**
  1741. * intel_disable_pipe - disable a pipe, asserting requirements
  1742. * @crtc: crtc whose pipes is to be disabled
  1743. *
  1744. * Disable the pipe of @crtc, making sure that various hardware
  1745. * specific requirements are met, if applicable, e.g. plane
  1746. * disabled, panel fitter off, etc.
  1747. *
  1748. * Will wait until the pipe has shut down before returning.
  1749. */
  1750. static void intel_disable_pipe(struct intel_crtc *crtc)
  1751. {
  1752. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1753. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1754. enum pipe pipe = crtc->pipe;
  1755. i915_reg_t reg;
  1756. u32 val;
  1757. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1758. /*
  1759. * Make sure planes won't keep trying to pump pixels to us,
  1760. * or we might hang the display.
  1761. */
  1762. assert_planes_disabled(dev_priv, pipe);
  1763. assert_cursor_disabled(dev_priv, pipe);
  1764. assert_sprites_disabled(dev_priv, pipe);
  1765. reg = PIPECONF(cpu_transcoder);
  1766. val = I915_READ(reg);
  1767. if ((val & PIPECONF_ENABLE) == 0)
  1768. return;
  1769. /*
  1770. * Double wide has implications for planes
  1771. * so best keep it disabled when not needed.
  1772. */
  1773. if (crtc->config->double_wide)
  1774. val &= ~PIPECONF_DOUBLE_WIDE;
  1775. /* Don't disable pipe or pipe PLLs if needed */
  1776. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1777. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1778. val &= ~PIPECONF_ENABLE;
  1779. I915_WRITE(reg, val);
  1780. if ((val & PIPECONF_ENABLE) == 0)
  1781. intel_wait_for_pipe_off(crtc);
  1782. }
  1783. static bool need_vtd_wa(struct drm_device *dev)
  1784. {
  1785. #ifdef CONFIG_INTEL_IOMMU
  1786. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1787. return true;
  1788. #endif
  1789. return false;
  1790. }
  1791. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1792. {
  1793. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1794. }
  1795. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1796. uint64_t fb_modifier, unsigned int cpp)
  1797. {
  1798. switch (fb_modifier) {
  1799. case DRM_FORMAT_MOD_NONE:
  1800. return cpp;
  1801. case I915_FORMAT_MOD_X_TILED:
  1802. if (IS_GEN2(dev_priv))
  1803. return 128;
  1804. else
  1805. return 512;
  1806. case I915_FORMAT_MOD_Y_TILED:
  1807. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1808. return 128;
  1809. else
  1810. return 512;
  1811. case I915_FORMAT_MOD_Yf_TILED:
  1812. switch (cpp) {
  1813. case 1:
  1814. return 64;
  1815. case 2:
  1816. case 4:
  1817. return 128;
  1818. case 8:
  1819. case 16:
  1820. return 256;
  1821. default:
  1822. MISSING_CASE(cpp);
  1823. return cpp;
  1824. }
  1825. break;
  1826. default:
  1827. MISSING_CASE(fb_modifier);
  1828. return cpp;
  1829. }
  1830. }
  1831. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1832. uint64_t fb_modifier, unsigned int cpp)
  1833. {
  1834. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1835. return 1;
  1836. else
  1837. return intel_tile_size(dev_priv) /
  1838. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1839. }
  1840. /* Return the tile dimensions in pixel units */
  1841. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1842. unsigned int *tile_width,
  1843. unsigned int *tile_height,
  1844. uint64_t fb_modifier,
  1845. unsigned int cpp)
  1846. {
  1847. unsigned int tile_width_bytes =
  1848. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1849. *tile_width = tile_width_bytes / cpp;
  1850. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1851. }
  1852. unsigned int
  1853. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1854. uint32_t pixel_format, uint64_t fb_modifier)
  1855. {
  1856. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1857. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1858. return ALIGN(height, tile_height);
  1859. }
  1860. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1861. {
  1862. unsigned int size = 0;
  1863. int i;
  1864. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1865. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1866. return size;
  1867. }
  1868. static void
  1869. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1870. const struct drm_framebuffer *fb,
  1871. unsigned int rotation)
  1872. {
  1873. if (intel_rotation_90_or_270(rotation)) {
  1874. *view = i915_ggtt_view_rotated;
  1875. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1876. } else {
  1877. *view = i915_ggtt_view_normal;
  1878. }
  1879. }
  1880. static void
  1881. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1882. struct drm_framebuffer *fb)
  1883. {
  1884. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1885. unsigned int tile_size, tile_width, tile_height, cpp;
  1886. tile_size = intel_tile_size(dev_priv);
  1887. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1888. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1889. fb->modifier[0], cpp);
  1890. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1891. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1892. if (info->pixel_format == DRM_FORMAT_NV12) {
  1893. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1894. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1895. fb->modifier[1], cpp);
  1896. info->uv_offset = fb->offsets[1];
  1897. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1898. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1899. }
  1900. }
  1901. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1902. {
  1903. if (INTEL_INFO(dev_priv)->gen >= 9)
  1904. return 256 * 1024;
  1905. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1906. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1907. return 128 * 1024;
  1908. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1909. return 4 * 1024;
  1910. else
  1911. return 0;
  1912. }
  1913. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1914. uint64_t fb_modifier)
  1915. {
  1916. switch (fb_modifier) {
  1917. case DRM_FORMAT_MOD_NONE:
  1918. return intel_linear_alignment(dev_priv);
  1919. case I915_FORMAT_MOD_X_TILED:
  1920. if (INTEL_INFO(dev_priv)->gen >= 9)
  1921. return 256 * 1024;
  1922. return 0;
  1923. case I915_FORMAT_MOD_Y_TILED:
  1924. case I915_FORMAT_MOD_Yf_TILED:
  1925. return 1 * 1024 * 1024;
  1926. default:
  1927. MISSING_CASE(fb_modifier);
  1928. return 0;
  1929. }
  1930. }
  1931. int
  1932. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1933. unsigned int rotation)
  1934. {
  1935. struct drm_device *dev = fb->dev;
  1936. struct drm_i915_private *dev_priv = dev->dev_private;
  1937. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1938. struct i915_ggtt_view view;
  1939. u32 alignment;
  1940. int ret;
  1941. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1942. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1943. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1944. /* Note that the w/a also requires 64 PTE of padding following the
  1945. * bo. We currently fill all unused PTE with the shadow page and so
  1946. * we should always have valid PTE following the scanout preventing
  1947. * the VT-d warning.
  1948. */
  1949. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1950. alignment = 256 * 1024;
  1951. /*
  1952. * Global gtt pte registers are special registers which actually forward
  1953. * writes to a chunk of system memory. Which means that there is no risk
  1954. * that the register values disappear as soon as we call
  1955. * intel_runtime_pm_put(), so it is correct to wrap only the
  1956. * pin/unpin/fence and not more.
  1957. */
  1958. intel_runtime_pm_get(dev_priv);
  1959. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1960. &view);
  1961. if (ret)
  1962. goto err_pm;
  1963. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1964. * fence, whereas 965+ only requires a fence if using
  1965. * framebuffer compression. For simplicity, we always install
  1966. * a fence as the cost is not that onerous.
  1967. */
  1968. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1969. ret = i915_gem_object_get_fence(obj);
  1970. if (ret == -EDEADLK) {
  1971. /*
  1972. * -EDEADLK means there are no free fences
  1973. * no pending flips.
  1974. *
  1975. * This is propagated to atomic, but it uses
  1976. * -EDEADLK to force a locking recovery, so
  1977. * change the returned error to -EBUSY.
  1978. */
  1979. ret = -EBUSY;
  1980. goto err_unpin;
  1981. } else if (ret)
  1982. goto err_unpin;
  1983. i915_gem_object_pin_fence(obj);
  1984. }
  1985. intel_runtime_pm_put(dev_priv);
  1986. return 0;
  1987. err_unpin:
  1988. i915_gem_object_unpin_from_display_plane(obj, &view);
  1989. err_pm:
  1990. intel_runtime_pm_put(dev_priv);
  1991. return ret;
  1992. }
  1993. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1994. {
  1995. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1996. struct i915_ggtt_view view;
  1997. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1998. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1999. if (view.type == I915_GGTT_VIEW_NORMAL)
  2000. i915_gem_object_unpin_fence(obj);
  2001. i915_gem_object_unpin_from_display_plane(obj, &view);
  2002. }
  2003. /*
  2004. * Adjust the tile offset by moving the difference into
  2005. * the x/y offsets.
  2006. *
  2007. * Input tile dimensions and pitch must already be
  2008. * rotated to match x and y, and in pixel units.
  2009. */
  2010. static u32 intel_adjust_tile_offset(int *x, int *y,
  2011. unsigned int tile_width,
  2012. unsigned int tile_height,
  2013. unsigned int tile_size,
  2014. unsigned int pitch_tiles,
  2015. u32 old_offset,
  2016. u32 new_offset)
  2017. {
  2018. unsigned int tiles;
  2019. WARN_ON(old_offset & (tile_size - 1));
  2020. WARN_ON(new_offset & (tile_size - 1));
  2021. WARN_ON(new_offset > old_offset);
  2022. tiles = (old_offset - new_offset) / tile_size;
  2023. *y += tiles / pitch_tiles * tile_height;
  2024. *x += tiles % pitch_tiles * tile_width;
  2025. return new_offset;
  2026. }
  2027. /*
  2028. * Computes the linear offset to the base tile and adjusts
  2029. * x, y. bytes per pixel is assumed to be a power-of-two.
  2030. *
  2031. * In the 90/270 rotated case, x and y are assumed
  2032. * to be already rotated to match the rotated GTT view, and
  2033. * pitch is the tile_height aligned framebuffer height.
  2034. */
  2035. u32 intel_compute_tile_offset(int *x, int *y,
  2036. const struct drm_framebuffer *fb, int plane,
  2037. unsigned int pitch,
  2038. unsigned int rotation)
  2039. {
  2040. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2041. uint64_t fb_modifier = fb->modifier[plane];
  2042. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2043. u32 offset, offset_aligned, alignment;
  2044. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2045. if (alignment)
  2046. alignment--;
  2047. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2048. unsigned int tile_size, tile_width, tile_height;
  2049. unsigned int tile_rows, tiles, pitch_tiles;
  2050. tile_size = intel_tile_size(dev_priv);
  2051. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2052. fb_modifier, cpp);
  2053. if (intel_rotation_90_or_270(rotation)) {
  2054. pitch_tiles = pitch / tile_height;
  2055. swap(tile_width, tile_height);
  2056. } else {
  2057. pitch_tiles = pitch / (tile_width * cpp);
  2058. }
  2059. tile_rows = *y / tile_height;
  2060. *y %= tile_height;
  2061. tiles = *x / tile_width;
  2062. *x %= tile_width;
  2063. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2064. offset_aligned = offset & ~alignment;
  2065. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2066. tile_size, pitch_tiles,
  2067. offset, offset_aligned);
  2068. } else {
  2069. offset = *y * pitch + *x * cpp;
  2070. offset_aligned = offset & ~alignment;
  2071. *y = (offset & alignment) / pitch;
  2072. *x = ((offset & alignment) - *y * pitch) / cpp;
  2073. }
  2074. return offset_aligned;
  2075. }
  2076. static int i9xx_format_to_fourcc(int format)
  2077. {
  2078. switch (format) {
  2079. case DISPPLANE_8BPP:
  2080. return DRM_FORMAT_C8;
  2081. case DISPPLANE_BGRX555:
  2082. return DRM_FORMAT_XRGB1555;
  2083. case DISPPLANE_BGRX565:
  2084. return DRM_FORMAT_RGB565;
  2085. default:
  2086. case DISPPLANE_BGRX888:
  2087. return DRM_FORMAT_XRGB8888;
  2088. case DISPPLANE_RGBX888:
  2089. return DRM_FORMAT_XBGR8888;
  2090. case DISPPLANE_BGRX101010:
  2091. return DRM_FORMAT_XRGB2101010;
  2092. case DISPPLANE_RGBX101010:
  2093. return DRM_FORMAT_XBGR2101010;
  2094. }
  2095. }
  2096. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2097. {
  2098. switch (format) {
  2099. case PLANE_CTL_FORMAT_RGB_565:
  2100. return DRM_FORMAT_RGB565;
  2101. default:
  2102. case PLANE_CTL_FORMAT_XRGB_8888:
  2103. if (rgb_order) {
  2104. if (alpha)
  2105. return DRM_FORMAT_ABGR8888;
  2106. else
  2107. return DRM_FORMAT_XBGR8888;
  2108. } else {
  2109. if (alpha)
  2110. return DRM_FORMAT_ARGB8888;
  2111. else
  2112. return DRM_FORMAT_XRGB8888;
  2113. }
  2114. case PLANE_CTL_FORMAT_XRGB_2101010:
  2115. if (rgb_order)
  2116. return DRM_FORMAT_XBGR2101010;
  2117. else
  2118. return DRM_FORMAT_XRGB2101010;
  2119. }
  2120. }
  2121. static bool
  2122. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2123. struct intel_initial_plane_config *plane_config)
  2124. {
  2125. struct drm_device *dev = crtc->base.dev;
  2126. struct drm_i915_private *dev_priv = to_i915(dev);
  2127. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2128. struct drm_i915_gem_object *obj = NULL;
  2129. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2130. struct drm_framebuffer *fb = &plane_config->fb->base;
  2131. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2132. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2133. PAGE_SIZE);
  2134. size_aligned -= base_aligned;
  2135. if (plane_config->size == 0)
  2136. return false;
  2137. /* If the FB is too big, just don't use it since fbdev is not very
  2138. * important and we should probably use that space with FBC or other
  2139. * features. */
  2140. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2141. return false;
  2142. mutex_lock(&dev->struct_mutex);
  2143. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2144. base_aligned,
  2145. base_aligned,
  2146. size_aligned);
  2147. if (!obj) {
  2148. mutex_unlock(&dev->struct_mutex);
  2149. return false;
  2150. }
  2151. obj->tiling_mode = plane_config->tiling;
  2152. if (obj->tiling_mode == I915_TILING_X)
  2153. obj->stride = fb->pitches[0];
  2154. mode_cmd.pixel_format = fb->pixel_format;
  2155. mode_cmd.width = fb->width;
  2156. mode_cmd.height = fb->height;
  2157. mode_cmd.pitches[0] = fb->pitches[0];
  2158. mode_cmd.modifier[0] = fb->modifier[0];
  2159. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2160. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2161. &mode_cmd, obj)) {
  2162. DRM_DEBUG_KMS("intel fb init failed\n");
  2163. goto out_unref_obj;
  2164. }
  2165. mutex_unlock(&dev->struct_mutex);
  2166. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2167. return true;
  2168. out_unref_obj:
  2169. drm_gem_object_unreference(&obj->base);
  2170. mutex_unlock(&dev->struct_mutex);
  2171. return false;
  2172. }
  2173. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2174. static void
  2175. update_state_fb(struct drm_plane *plane)
  2176. {
  2177. if (plane->fb == plane->state->fb)
  2178. return;
  2179. if (plane->state->fb)
  2180. drm_framebuffer_unreference(plane->state->fb);
  2181. plane->state->fb = plane->fb;
  2182. if (plane->state->fb)
  2183. drm_framebuffer_reference(plane->state->fb);
  2184. }
  2185. static void
  2186. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2187. struct intel_initial_plane_config *plane_config)
  2188. {
  2189. struct drm_device *dev = intel_crtc->base.dev;
  2190. struct drm_i915_private *dev_priv = dev->dev_private;
  2191. struct drm_crtc *c;
  2192. struct intel_crtc *i;
  2193. struct drm_i915_gem_object *obj;
  2194. struct drm_plane *primary = intel_crtc->base.primary;
  2195. struct drm_plane_state *plane_state = primary->state;
  2196. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2197. struct intel_plane *intel_plane = to_intel_plane(primary);
  2198. struct intel_plane_state *intel_state =
  2199. to_intel_plane_state(plane_state);
  2200. struct drm_framebuffer *fb;
  2201. if (!plane_config->fb)
  2202. return;
  2203. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2204. fb = &plane_config->fb->base;
  2205. goto valid_fb;
  2206. }
  2207. kfree(plane_config->fb);
  2208. /*
  2209. * Failed to alloc the obj, check to see if we should share
  2210. * an fb with another CRTC instead
  2211. */
  2212. for_each_crtc(dev, c) {
  2213. i = to_intel_crtc(c);
  2214. if (c == &intel_crtc->base)
  2215. continue;
  2216. if (!i->active)
  2217. continue;
  2218. fb = c->primary->fb;
  2219. if (!fb)
  2220. continue;
  2221. obj = intel_fb_obj(fb);
  2222. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2223. drm_framebuffer_reference(fb);
  2224. goto valid_fb;
  2225. }
  2226. }
  2227. /*
  2228. * We've failed to reconstruct the BIOS FB. Current display state
  2229. * indicates that the primary plane is visible, but has a NULL FB,
  2230. * which will lead to problems later if we don't fix it up. The
  2231. * simplest solution is to just disable the primary plane now and
  2232. * pretend the BIOS never had it enabled.
  2233. */
  2234. to_intel_plane_state(plane_state)->visible = false;
  2235. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2236. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2237. intel_plane->disable_plane(primary, &intel_crtc->base);
  2238. return;
  2239. valid_fb:
  2240. plane_state->src_x = 0;
  2241. plane_state->src_y = 0;
  2242. plane_state->src_w = fb->width << 16;
  2243. plane_state->src_h = fb->height << 16;
  2244. plane_state->crtc_x = 0;
  2245. plane_state->crtc_y = 0;
  2246. plane_state->crtc_w = fb->width;
  2247. plane_state->crtc_h = fb->height;
  2248. intel_state->src.x1 = plane_state->src_x;
  2249. intel_state->src.y1 = plane_state->src_y;
  2250. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2251. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2252. intel_state->dst.x1 = plane_state->crtc_x;
  2253. intel_state->dst.y1 = plane_state->crtc_y;
  2254. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2255. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2256. obj = intel_fb_obj(fb);
  2257. if (obj->tiling_mode != I915_TILING_NONE)
  2258. dev_priv->preserve_bios_swizzle = true;
  2259. drm_framebuffer_reference(fb);
  2260. primary->fb = primary->state->fb = fb;
  2261. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2262. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2263. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2264. }
  2265. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2266. const struct intel_crtc_state *crtc_state,
  2267. const struct intel_plane_state *plane_state)
  2268. {
  2269. struct drm_device *dev = primary->dev;
  2270. struct drm_i915_private *dev_priv = dev->dev_private;
  2271. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2272. struct drm_framebuffer *fb = plane_state->base.fb;
  2273. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2274. int plane = intel_crtc->plane;
  2275. u32 linear_offset;
  2276. u32 dspcntr;
  2277. i915_reg_t reg = DSPCNTR(plane);
  2278. unsigned int rotation = plane_state->base.rotation;
  2279. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2280. int x = plane_state->src.x1 >> 16;
  2281. int y = plane_state->src.y1 >> 16;
  2282. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2283. dspcntr |= DISPLAY_PLANE_ENABLE;
  2284. if (INTEL_INFO(dev)->gen < 4) {
  2285. if (intel_crtc->pipe == PIPE_B)
  2286. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2287. /* pipesrc and dspsize control the size that is scaled from,
  2288. * which should always be the user's requested size.
  2289. */
  2290. I915_WRITE(DSPSIZE(plane),
  2291. ((crtc_state->pipe_src_h - 1) << 16) |
  2292. (crtc_state->pipe_src_w - 1));
  2293. I915_WRITE(DSPPOS(plane), 0);
  2294. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2295. I915_WRITE(PRIMSIZE(plane),
  2296. ((crtc_state->pipe_src_h - 1) << 16) |
  2297. (crtc_state->pipe_src_w - 1));
  2298. I915_WRITE(PRIMPOS(plane), 0);
  2299. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2300. }
  2301. switch (fb->pixel_format) {
  2302. case DRM_FORMAT_C8:
  2303. dspcntr |= DISPPLANE_8BPP;
  2304. break;
  2305. case DRM_FORMAT_XRGB1555:
  2306. dspcntr |= DISPPLANE_BGRX555;
  2307. break;
  2308. case DRM_FORMAT_RGB565:
  2309. dspcntr |= DISPPLANE_BGRX565;
  2310. break;
  2311. case DRM_FORMAT_XRGB8888:
  2312. dspcntr |= DISPPLANE_BGRX888;
  2313. break;
  2314. case DRM_FORMAT_XBGR8888:
  2315. dspcntr |= DISPPLANE_RGBX888;
  2316. break;
  2317. case DRM_FORMAT_XRGB2101010:
  2318. dspcntr |= DISPPLANE_BGRX101010;
  2319. break;
  2320. case DRM_FORMAT_XBGR2101010:
  2321. dspcntr |= DISPPLANE_RGBX101010;
  2322. break;
  2323. default:
  2324. BUG();
  2325. }
  2326. if (INTEL_INFO(dev)->gen >= 4 &&
  2327. obj->tiling_mode != I915_TILING_NONE)
  2328. dspcntr |= DISPPLANE_TILED;
  2329. if (IS_G4X(dev))
  2330. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2331. linear_offset = y * fb->pitches[0] + x * cpp;
  2332. if (INTEL_INFO(dev)->gen >= 4) {
  2333. intel_crtc->dspaddr_offset =
  2334. intel_compute_tile_offset(&x, &y, fb, 0,
  2335. fb->pitches[0], rotation);
  2336. linear_offset -= intel_crtc->dspaddr_offset;
  2337. } else {
  2338. intel_crtc->dspaddr_offset = linear_offset;
  2339. }
  2340. if (rotation == BIT(DRM_ROTATE_180)) {
  2341. dspcntr |= DISPPLANE_ROTATE_180;
  2342. x += (crtc_state->pipe_src_w - 1);
  2343. y += (crtc_state->pipe_src_h - 1);
  2344. /* Finding the last pixel of the last line of the display
  2345. data and adding to linear_offset*/
  2346. linear_offset +=
  2347. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2348. (crtc_state->pipe_src_w - 1) * cpp;
  2349. }
  2350. intel_crtc->adjusted_x = x;
  2351. intel_crtc->adjusted_y = y;
  2352. I915_WRITE(reg, dspcntr);
  2353. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2354. if (INTEL_INFO(dev)->gen >= 4) {
  2355. I915_WRITE(DSPSURF(plane),
  2356. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2357. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2358. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2359. } else
  2360. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2361. POSTING_READ(reg);
  2362. }
  2363. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2364. struct drm_crtc *crtc)
  2365. {
  2366. struct drm_device *dev = crtc->dev;
  2367. struct drm_i915_private *dev_priv = dev->dev_private;
  2368. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2369. int plane = intel_crtc->plane;
  2370. I915_WRITE(DSPCNTR(plane), 0);
  2371. if (INTEL_INFO(dev_priv)->gen >= 4)
  2372. I915_WRITE(DSPSURF(plane), 0);
  2373. else
  2374. I915_WRITE(DSPADDR(plane), 0);
  2375. POSTING_READ(DSPCNTR(plane));
  2376. }
  2377. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2378. const struct intel_crtc_state *crtc_state,
  2379. const struct intel_plane_state *plane_state)
  2380. {
  2381. struct drm_device *dev = primary->dev;
  2382. struct drm_i915_private *dev_priv = dev->dev_private;
  2383. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2384. struct drm_framebuffer *fb = plane_state->base.fb;
  2385. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2386. int plane = intel_crtc->plane;
  2387. u32 linear_offset;
  2388. u32 dspcntr;
  2389. i915_reg_t reg = DSPCNTR(plane);
  2390. unsigned int rotation = plane_state->base.rotation;
  2391. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2392. int x = plane_state->src.x1 >> 16;
  2393. int y = plane_state->src.y1 >> 16;
  2394. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2395. dspcntr |= DISPLAY_PLANE_ENABLE;
  2396. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2397. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2398. switch (fb->pixel_format) {
  2399. case DRM_FORMAT_C8:
  2400. dspcntr |= DISPPLANE_8BPP;
  2401. break;
  2402. case DRM_FORMAT_RGB565:
  2403. dspcntr |= DISPPLANE_BGRX565;
  2404. break;
  2405. case DRM_FORMAT_XRGB8888:
  2406. dspcntr |= DISPPLANE_BGRX888;
  2407. break;
  2408. case DRM_FORMAT_XBGR8888:
  2409. dspcntr |= DISPPLANE_RGBX888;
  2410. break;
  2411. case DRM_FORMAT_XRGB2101010:
  2412. dspcntr |= DISPPLANE_BGRX101010;
  2413. break;
  2414. case DRM_FORMAT_XBGR2101010:
  2415. dspcntr |= DISPPLANE_RGBX101010;
  2416. break;
  2417. default:
  2418. BUG();
  2419. }
  2420. if (obj->tiling_mode != I915_TILING_NONE)
  2421. dspcntr |= DISPPLANE_TILED;
  2422. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2423. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2424. linear_offset = y * fb->pitches[0] + x * cpp;
  2425. intel_crtc->dspaddr_offset =
  2426. intel_compute_tile_offset(&x, &y, fb, 0,
  2427. fb->pitches[0], rotation);
  2428. linear_offset -= intel_crtc->dspaddr_offset;
  2429. if (rotation == BIT(DRM_ROTATE_180)) {
  2430. dspcntr |= DISPPLANE_ROTATE_180;
  2431. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2432. x += (crtc_state->pipe_src_w - 1);
  2433. y += (crtc_state->pipe_src_h - 1);
  2434. /* Finding the last pixel of the last line of the display
  2435. data and adding to linear_offset*/
  2436. linear_offset +=
  2437. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2438. (crtc_state->pipe_src_w - 1) * cpp;
  2439. }
  2440. }
  2441. intel_crtc->adjusted_x = x;
  2442. intel_crtc->adjusted_y = y;
  2443. I915_WRITE(reg, dspcntr);
  2444. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2445. I915_WRITE(DSPSURF(plane),
  2446. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2447. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2448. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2449. } else {
  2450. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2451. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2452. }
  2453. POSTING_READ(reg);
  2454. }
  2455. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2456. uint64_t fb_modifier, uint32_t pixel_format)
  2457. {
  2458. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2459. return 64;
  2460. } else {
  2461. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2462. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2463. }
  2464. }
  2465. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2466. struct drm_i915_gem_object *obj,
  2467. unsigned int plane)
  2468. {
  2469. struct i915_ggtt_view view;
  2470. struct i915_vma *vma;
  2471. u64 offset;
  2472. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2473. intel_plane->base.state->rotation);
  2474. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2475. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2476. view.type))
  2477. return -1;
  2478. offset = vma->node.start;
  2479. if (plane == 1) {
  2480. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2481. PAGE_SIZE;
  2482. }
  2483. WARN_ON(upper_32_bits(offset));
  2484. return lower_32_bits(offset);
  2485. }
  2486. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2487. {
  2488. struct drm_device *dev = intel_crtc->base.dev;
  2489. struct drm_i915_private *dev_priv = dev->dev_private;
  2490. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2491. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2492. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2493. }
  2494. /*
  2495. * This function detaches (aka. unbinds) unused scalers in hardware
  2496. */
  2497. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2498. {
  2499. struct intel_crtc_scaler_state *scaler_state;
  2500. int i;
  2501. scaler_state = &intel_crtc->config->scaler_state;
  2502. /* loop through and disable scalers that aren't in use */
  2503. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2504. if (!scaler_state->scalers[i].in_use)
  2505. skl_detach_scaler(intel_crtc, i);
  2506. }
  2507. }
  2508. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2509. {
  2510. switch (pixel_format) {
  2511. case DRM_FORMAT_C8:
  2512. return PLANE_CTL_FORMAT_INDEXED;
  2513. case DRM_FORMAT_RGB565:
  2514. return PLANE_CTL_FORMAT_RGB_565;
  2515. case DRM_FORMAT_XBGR8888:
  2516. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2517. case DRM_FORMAT_XRGB8888:
  2518. return PLANE_CTL_FORMAT_XRGB_8888;
  2519. /*
  2520. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2521. * to be already pre-multiplied. We need to add a knob (or a different
  2522. * DRM_FORMAT) for user-space to configure that.
  2523. */
  2524. case DRM_FORMAT_ABGR8888:
  2525. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2526. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2527. case DRM_FORMAT_ARGB8888:
  2528. return PLANE_CTL_FORMAT_XRGB_8888 |
  2529. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2530. case DRM_FORMAT_XRGB2101010:
  2531. return PLANE_CTL_FORMAT_XRGB_2101010;
  2532. case DRM_FORMAT_XBGR2101010:
  2533. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2534. case DRM_FORMAT_YUYV:
  2535. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2536. case DRM_FORMAT_YVYU:
  2537. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2538. case DRM_FORMAT_UYVY:
  2539. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2540. case DRM_FORMAT_VYUY:
  2541. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2542. default:
  2543. MISSING_CASE(pixel_format);
  2544. }
  2545. return 0;
  2546. }
  2547. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2548. {
  2549. switch (fb_modifier) {
  2550. case DRM_FORMAT_MOD_NONE:
  2551. break;
  2552. case I915_FORMAT_MOD_X_TILED:
  2553. return PLANE_CTL_TILED_X;
  2554. case I915_FORMAT_MOD_Y_TILED:
  2555. return PLANE_CTL_TILED_Y;
  2556. case I915_FORMAT_MOD_Yf_TILED:
  2557. return PLANE_CTL_TILED_YF;
  2558. default:
  2559. MISSING_CASE(fb_modifier);
  2560. }
  2561. return 0;
  2562. }
  2563. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2564. {
  2565. switch (rotation) {
  2566. case BIT(DRM_ROTATE_0):
  2567. break;
  2568. /*
  2569. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2570. * while i915 HW rotation is clockwise, thats why this swapping.
  2571. */
  2572. case BIT(DRM_ROTATE_90):
  2573. return PLANE_CTL_ROTATE_270;
  2574. case BIT(DRM_ROTATE_180):
  2575. return PLANE_CTL_ROTATE_180;
  2576. case BIT(DRM_ROTATE_270):
  2577. return PLANE_CTL_ROTATE_90;
  2578. default:
  2579. MISSING_CASE(rotation);
  2580. }
  2581. return 0;
  2582. }
  2583. static void skylake_update_primary_plane(struct drm_plane *plane,
  2584. const struct intel_crtc_state *crtc_state,
  2585. const struct intel_plane_state *plane_state)
  2586. {
  2587. struct drm_device *dev = plane->dev;
  2588. struct drm_i915_private *dev_priv = dev->dev_private;
  2589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2590. struct drm_framebuffer *fb = plane_state->base.fb;
  2591. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2592. int pipe = intel_crtc->pipe;
  2593. u32 plane_ctl, stride_div, stride;
  2594. u32 tile_height, plane_offset, plane_size;
  2595. unsigned int rotation = plane_state->base.rotation;
  2596. int x_offset, y_offset;
  2597. u32 surf_addr;
  2598. int scaler_id = plane_state->scaler_id;
  2599. int src_x = plane_state->src.x1 >> 16;
  2600. int src_y = plane_state->src.y1 >> 16;
  2601. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2602. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2603. int dst_x = plane_state->dst.x1;
  2604. int dst_y = plane_state->dst.y1;
  2605. int dst_w = drm_rect_width(&plane_state->dst);
  2606. int dst_h = drm_rect_height(&plane_state->dst);
  2607. plane_ctl = PLANE_CTL_ENABLE |
  2608. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2609. PLANE_CTL_PIPE_CSC_ENABLE;
  2610. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2611. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2612. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2613. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2614. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2615. fb->pixel_format);
  2616. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2617. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2618. if (intel_rotation_90_or_270(rotation)) {
  2619. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2620. /* stride = Surface height in tiles */
  2621. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2622. stride = DIV_ROUND_UP(fb->height, tile_height);
  2623. x_offset = stride * tile_height - src_y - src_h;
  2624. y_offset = src_x;
  2625. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2626. } else {
  2627. stride = fb->pitches[0] / stride_div;
  2628. x_offset = src_x;
  2629. y_offset = src_y;
  2630. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2631. }
  2632. plane_offset = y_offset << 16 | x_offset;
  2633. intel_crtc->adjusted_x = x_offset;
  2634. intel_crtc->adjusted_y = y_offset;
  2635. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2636. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2637. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2638. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2639. if (scaler_id >= 0) {
  2640. uint32_t ps_ctrl = 0;
  2641. WARN_ON(!dst_w || !dst_h);
  2642. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2643. crtc_state->scaler_state.scalers[scaler_id].mode;
  2644. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2645. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2646. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2647. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2648. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2649. } else {
  2650. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2651. }
  2652. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2653. POSTING_READ(PLANE_SURF(pipe, 0));
  2654. }
  2655. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2656. struct drm_crtc *crtc)
  2657. {
  2658. struct drm_device *dev = crtc->dev;
  2659. struct drm_i915_private *dev_priv = dev->dev_private;
  2660. int pipe = to_intel_crtc(crtc)->pipe;
  2661. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2662. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2663. POSTING_READ(PLANE_SURF(pipe, 0));
  2664. }
  2665. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2666. static int
  2667. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2668. int x, int y, enum mode_set_atomic state)
  2669. {
  2670. /* Support for kgdboc is disabled, this needs a major rework. */
  2671. DRM_ERROR("legacy panic handler not supported any more.\n");
  2672. return -ENODEV;
  2673. }
  2674. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2675. {
  2676. struct drm_crtc *crtc;
  2677. for_each_crtc(dev_priv->dev, crtc) {
  2678. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2679. enum plane plane = intel_crtc->plane;
  2680. intel_prepare_page_flip(dev_priv, plane);
  2681. intel_finish_page_flip_plane(dev_priv, plane);
  2682. }
  2683. }
  2684. static void intel_update_primary_planes(struct drm_device *dev)
  2685. {
  2686. struct drm_crtc *crtc;
  2687. for_each_crtc(dev, crtc) {
  2688. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2689. struct intel_plane_state *plane_state;
  2690. drm_modeset_lock_crtc(crtc, &plane->base);
  2691. plane_state = to_intel_plane_state(plane->base.state);
  2692. if (plane_state->visible)
  2693. plane->update_plane(&plane->base,
  2694. to_intel_crtc_state(crtc->state),
  2695. plane_state);
  2696. drm_modeset_unlock_crtc(crtc);
  2697. }
  2698. }
  2699. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2700. {
  2701. /* no reset support for gen2 */
  2702. if (IS_GEN2(dev_priv))
  2703. return;
  2704. /* reset doesn't touch the display */
  2705. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2706. return;
  2707. drm_modeset_lock_all(dev_priv->dev);
  2708. /*
  2709. * Disabling the crtcs gracefully seems nicer. Also the
  2710. * g33 docs say we should at least disable all the planes.
  2711. */
  2712. intel_display_suspend(dev_priv->dev);
  2713. }
  2714. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2715. {
  2716. /*
  2717. * Flips in the rings will be nuked by the reset,
  2718. * so complete all pending flips so that user space
  2719. * will get its events and not get stuck.
  2720. */
  2721. intel_complete_page_flips(dev_priv);
  2722. /* no reset support for gen2 */
  2723. if (IS_GEN2(dev_priv))
  2724. return;
  2725. /* reset doesn't touch the display */
  2726. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2727. /*
  2728. * Flips in the rings have been nuked by the reset,
  2729. * so update the base address of all primary
  2730. * planes to the the last fb to make sure we're
  2731. * showing the correct fb after a reset.
  2732. *
  2733. * FIXME: Atomic will make this obsolete since we won't schedule
  2734. * CS-based flips (which might get lost in gpu resets) any more.
  2735. */
  2736. intel_update_primary_planes(dev_priv->dev);
  2737. return;
  2738. }
  2739. /*
  2740. * The display has been reset as well,
  2741. * so need a full re-initialization.
  2742. */
  2743. intel_runtime_pm_disable_interrupts(dev_priv);
  2744. intel_runtime_pm_enable_interrupts(dev_priv);
  2745. intel_modeset_init_hw(dev_priv->dev);
  2746. spin_lock_irq(&dev_priv->irq_lock);
  2747. if (dev_priv->display.hpd_irq_setup)
  2748. dev_priv->display.hpd_irq_setup(dev_priv);
  2749. spin_unlock_irq(&dev_priv->irq_lock);
  2750. intel_display_resume(dev_priv->dev);
  2751. intel_hpd_init(dev_priv);
  2752. drm_modeset_unlock_all(dev_priv->dev);
  2753. }
  2754. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2755. {
  2756. struct drm_device *dev = crtc->dev;
  2757. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2758. unsigned reset_counter;
  2759. bool pending;
  2760. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2761. if (intel_crtc->reset_counter != reset_counter)
  2762. return false;
  2763. spin_lock_irq(&dev->event_lock);
  2764. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2765. spin_unlock_irq(&dev->event_lock);
  2766. return pending;
  2767. }
  2768. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2769. struct intel_crtc_state *old_crtc_state)
  2770. {
  2771. struct drm_device *dev = crtc->base.dev;
  2772. struct drm_i915_private *dev_priv = dev->dev_private;
  2773. struct intel_crtc_state *pipe_config =
  2774. to_intel_crtc_state(crtc->base.state);
  2775. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2776. crtc->base.mode = crtc->base.state->mode;
  2777. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2778. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2779. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2780. /*
  2781. * Update pipe size and adjust fitter if needed: the reason for this is
  2782. * that in compute_mode_changes we check the native mode (not the pfit
  2783. * mode) to see if we can flip rather than do a full mode set. In the
  2784. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2785. * pfit state, we'll end up with a big fb scanned out into the wrong
  2786. * sized surface.
  2787. */
  2788. I915_WRITE(PIPESRC(crtc->pipe),
  2789. ((pipe_config->pipe_src_w - 1) << 16) |
  2790. (pipe_config->pipe_src_h - 1));
  2791. /* on skylake this is done by detaching scalers */
  2792. if (INTEL_INFO(dev)->gen >= 9) {
  2793. skl_detach_scalers(crtc);
  2794. if (pipe_config->pch_pfit.enabled)
  2795. skylake_pfit_enable(crtc);
  2796. } else if (HAS_PCH_SPLIT(dev)) {
  2797. if (pipe_config->pch_pfit.enabled)
  2798. ironlake_pfit_enable(crtc);
  2799. else if (old_crtc_state->pch_pfit.enabled)
  2800. ironlake_pfit_disable(crtc, true);
  2801. }
  2802. }
  2803. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2804. {
  2805. struct drm_device *dev = crtc->dev;
  2806. struct drm_i915_private *dev_priv = dev->dev_private;
  2807. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2808. int pipe = intel_crtc->pipe;
  2809. i915_reg_t reg;
  2810. u32 temp;
  2811. /* enable normal train */
  2812. reg = FDI_TX_CTL(pipe);
  2813. temp = I915_READ(reg);
  2814. if (IS_IVYBRIDGE(dev)) {
  2815. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2816. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2817. } else {
  2818. temp &= ~FDI_LINK_TRAIN_NONE;
  2819. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2820. }
  2821. I915_WRITE(reg, temp);
  2822. reg = FDI_RX_CTL(pipe);
  2823. temp = I915_READ(reg);
  2824. if (HAS_PCH_CPT(dev)) {
  2825. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2826. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2827. } else {
  2828. temp &= ~FDI_LINK_TRAIN_NONE;
  2829. temp |= FDI_LINK_TRAIN_NONE;
  2830. }
  2831. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2832. /* wait one idle pattern time */
  2833. POSTING_READ(reg);
  2834. udelay(1000);
  2835. /* IVB wants error correction enabled */
  2836. if (IS_IVYBRIDGE(dev))
  2837. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2838. FDI_FE_ERRC_ENABLE);
  2839. }
  2840. /* The FDI link training functions for ILK/Ibexpeak. */
  2841. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2842. {
  2843. struct drm_device *dev = crtc->dev;
  2844. struct drm_i915_private *dev_priv = dev->dev_private;
  2845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2846. int pipe = intel_crtc->pipe;
  2847. i915_reg_t reg;
  2848. u32 temp, tries;
  2849. /* FDI needs bits from pipe first */
  2850. assert_pipe_enabled(dev_priv, pipe);
  2851. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2852. for train result */
  2853. reg = FDI_RX_IMR(pipe);
  2854. temp = I915_READ(reg);
  2855. temp &= ~FDI_RX_SYMBOL_LOCK;
  2856. temp &= ~FDI_RX_BIT_LOCK;
  2857. I915_WRITE(reg, temp);
  2858. I915_READ(reg);
  2859. udelay(150);
  2860. /* enable CPU FDI TX and PCH FDI RX */
  2861. reg = FDI_TX_CTL(pipe);
  2862. temp = I915_READ(reg);
  2863. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2864. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2865. temp &= ~FDI_LINK_TRAIN_NONE;
  2866. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2867. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2868. reg = FDI_RX_CTL(pipe);
  2869. temp = I915_READ(reg);
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2872. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2873. POSTING_READ(reg);
  2874. udelay(150);
  2875. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2876. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2877. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2878. FDI_RX_PHASE_SYNC_POINTER_EN);
  2879. reg = FDI_RX_IIR(pipe);
  2880. for (tries = 0; tries < 5; tries++) {
  2881. temp = I915_READ(reg);
  2882. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2883. if ((temp & FDI_RX_BIT_LOCK)) {
  2884. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2885. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2886. break;
  2887. }
  2888. }
  2889. if (tries == 5)
  2890. DRM_ERROR("FDI train 1 fail!\n");
  2891. /* Train 2 */
  2892. reg = FDI_TX_CTL(pipe);
  2893. temp = I915_READ(reg);
  2894. temp &= ~FDI_LINK_TRAIN_NONE;
  2895. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2896. I915_WRITE(reg, temp);
  2897. reg = FDI_RX_CTL(pipe);
  2898. temp = I915_READ(reg);
  2899. temp &= ~FDI_LINK_TRAIN_NONE;
  2900. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2901. I915_WRITE(reg, temp);
  2902. POSTING_READ(reg);
  2903. udelay(150);
  2904. reg = FDI_RX_IIR(pipe);
  2905. for (tries = 0; tries < 5; tries++) {
  2906. temp = I915_READ(reg);
  2907. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2908. if (temp & FDI_RX_SYMBOL_LOCK) {
  2909. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2910. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2911. break;
  2912. }
  2913. }
  2914. if (tries == 5)
  2915. DRM_ERROR("FDI train 2 fail!\n");
  2916. DRM_DEBUG_KMS("FDI train done\n");
  2917. }
  2918. static const int snb_b_fdi_train_param[] = {
  2919. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2920. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2921. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2922. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2923. };
  2924. /* The FDI link training functions for SNB/Cougarpoint. */
  2925. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2926. {
  2927. struct drm_device *dev = crtc->dev;
  2928. struct drm_i915_private *dev_priv = dev->dev_private;
  2929. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2930. int pipe = intel_crtc->pipe;
  2931. i915_reg_t reg;
  2932. u32 temp, i, retry;
  2933. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2934. for train result */
  2935. reg = FDI_RX_IMR(pipe);
  2936. temp = I915_READ(reg);
  2937. temp &= ~FDI_RX_SYMBOL_LOCK;
  2938. temp &= ~FDI_RX_BIT_LOCK;
  2939. I915_WRITE(reg, temp);
  2940. POSTING_READ(reg);
  2941. udelay(150);
  2942. /* enable CPU FDI TX and PCH FDI RX */
  2943. reg = FDI_TX_CTL(pipe);
  2944. temp = I915_READ(reg);
  2945. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2946. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2947. temp &= ~FDI_LINK_TRAIN_NONE;
  2948. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2949. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2950. /* SNB-B */
  2951. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2952. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2953. I915_WRITE(FDI_RX_MISC(pipe),
  2954. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2955. reg = FDI_RX_CTL(pipe);
  2956. temp = I915_READ(reg);
  2957. if (HAS_PCH_CPT(dev)) {
  2958. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2959. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2960. } else {
  2961. temp &= ~FDI_LINK_TRAIN_NONE;
  2962. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2963. }
  2964. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2965. POSTING_READ(reg);
  2966. udelay(150);
  2967. for (i = 0; i < 4; i++) {
  2968. reg = FDI_TX_CTL(pipe);
  2969. temp = I915_READ(reg);
  2970. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2971. temp |= snb_b_fdi_train_param[i];
  2972. I915_WRITE(reg, temp);
  2973. POSTING_READ(reg);
  2974. udelay(500);
  2975. for (retry = 0; retry < 5; retry++) {
  2976. reg = FDI_RX_IIR(pipe);
  2977. temp = I915_READ(reg);
  2978. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2979. if (temp & FDI_RX_BIT_LOCK) {
  2980. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2981. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2982. break;
  2983. }
  2984. udelay(50);
  2985. }
  2986. if (retry < 5)
  2987. break;
  2988. }
  2989. if (i == 4)
  2990. DRM_ERROR("FDI train 1 fail!\n");
  2991. /* Train 2 */
  2992. reg = FDI_TX_CTL(pipe);
  2993. temp = I915_READ(reg);
  2994. temp &= ~FDI_LINK_TRAIN_NONE;
  2995. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2996. if (IS_GEN6(dev)) {
  2997. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2998. /* SNB-B */
  2999. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3000. }
  3001. I915_WRITE(reg, temp);
  3002. reg = FDI_RX_CTL(pipe);
  3003. temp = I915_READ(reg);
  3004. if (HAS_PCH_CPT(dev)) {
  3005. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3006. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3007. } else {
  3008. temp &= ~FDI_LINK_TRAIN_NONE;
  3009. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3010. }
  3011. I915_WRITE(reg, temp);
  3012. POSTING_READ(reg);
  3013. udelay(150);
  3014. for (i = 0; i < 4; i++) {
  3015. reg = FDI_TX_CTL(pipe);
  3016. temp = I915_READ(reg);
  3017. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3018. temp |= snb_b_fdi_train_param[i];
  3019. I915_WRITE(reg, temp);
  3020. POSTING_READ(reg);
  3021. udelay(500);
  3022. for (retry = 0; retry < 5; retry++) {
  3023. reg = FDI_RX_IIR(pipe);
  3024. temp = I915_READ(reg);
  3025. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3026. if (temp & FDI_RX_SYMBOL_LOCK) {
  3027. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3028. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3029. break;
  3030. }
  3031. udelay(50);
  3032. }
  3033. if (retry < 5)
  3034. break;
  3035. }
  3036. if (i == 4)
  3037. DRM_ERROR("FDI train 2 fail!\n");
  3038. DRM_DEBUG_KMS("FDI train done.\n");
  3039. }
  3040. /* Manual link training for Ivy Bridge A0 parts */
  3041. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3042. {
  3043. struct drm_device *dev = crtc->dev;
  3044. struct drm_i915_private *dev_priv = dev->dev_private;
  3045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3046. int pipe = intel_crtc->pipe;
  3047. i915_reg_t reg;
  3048. u32 temp, i, j;
  3049. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3050. for train result */
  3051. reg = FDI_RX_IMR(pipe);
  3052. temp = I915_READ(reg);
  3053. temp &= ~FDI_RX_SYMBOL_LOCK;
  3054. temp &= ~FDI_RX_BIT_LOCK;
  3055. I915_WRITE(reg, temp);
  3056. POSTING_READ(reg);
  3057. udelay(150);
  3058. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3059. I915_READ(FDI_RX_IIR(pipe)));
  3060. /* Try each vswing and preemphasis setting twice before moving on */
  3061. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3062. /* disable first in case we need to retry */
  3063. reg = FDI_TX_CTL(pipe);
  3064. temp = I915_READ(reg);
  3065. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3066. temp &= ~FDI_TX_ENABLE;
  3067. I915_WRITE(reg, temp);
  3068. reg = FDI_RX_CTL(pipe);
  3069. temp = I915_READ(reg);
  3070. temp &= ~FDI_LINK_TRAIN_AUTO;
  3071. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3072. temp &= ~FDI_RX_ENABLE;
  3073. I915_WRITE(reg, temp);
  3074. /* enable CPU FDI TX and PCH FDI RX */
  3075. reg = FDI_TX_CTL(pipe);
  3076. temp = I915_READ(reg);
  3077. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3078. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3079. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3080. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3081. temp |= snb_b_fdi_train_param[j/2];
  3082. temp |= FDI_COMPOSITE_SYNC;
  3083. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3084. I915_WRITE(FDI_RX_MISC(pipe),
  3085. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3086. reg = FDI_RX_CTL(pipe);
  3087. temp = I915_READ(reg);
  3088. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3089. temp |= FDI_COMPOSITE_SYNC;
  3090. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3091. POSTING_READ(reg);
  3092. udelay(1); /* should be 0.5us */
  3093. for (i = 0; i < 4; i++) {
  3094. reg = FDI_RX_IIR(pipe);
  3095. temp = I915_READ(reg);
  3096. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3097. if (temp & FDI_RX_BIT_LOCK ||
  3098. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3099. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3100. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3101. i);
  3102. break;
  3103. }
  3104. udelay(1); /* should be 0.5us */
  3105. }
  3106. if (i == 4) {
  3107. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3108. continue;
  3109. }
  3110. /* Train 2 */
  3111. reg = FDI_TX_CTL(pipe);
  3112. temp = I915_READ(reg);
  3113. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3114. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3115. I915_WRITE(reg, temp);
  3116. reg = FDI_RX_CTL(pipe);
  3117. temp = I915_READ(reg);
  3118. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3119. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3120. I915_WRITE(reg, temp);
  3121. POSTING_READ(reg);
  3122. udelay(2); /* should be 1.5us */
  3123. for (i = 0; i < 4; i++) {
  3124. reg = FDI_RX_IIR(pipe);
  3125. temp = I915_READ(reg);
  3126. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3127. if (temp & FDI_RX_SYMBOL_LOCK ||
  3128. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3129. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3130. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3131. i);
  3132. goto train_done;
  3133. }
  3134. udelay(2); /* should be 1.5us */
  3135. }
  3136. if (i == 4)
  3137. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3138. }
  3139. train_done:
  3140. DRM_DEBUG_KMS("FDI train done.\n");
  3141. }
  3142. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3143. {
  3144. struct drm_device *dev = intel_crtc->base.dev;
  3145. struct drm_i915_private *dev_priv = dev->dev_private;
  3146. int pipe = intel_crtc->pipe;
  3147. i915_reg_t reg;
  3148. u32 temp;
  3149. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3150. reg = FDI_RX_CTL(pipe);
  3151. temp = I915_READ(reg);
  3152. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3153. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3154. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3155. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3156. POSTING_READ(reg);
  3157. udelay(200);
  3158. /* Switch from Rawclk to PCDclk */
  3159. temp = I915_READ(reg);
  3160. I915_WRITE(reg, temp | FDI_PCDCLK);
  3161. POSTING_READ(reg);
  3162. udelay(200);
  3163. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3164. reg = FDI_TX_CTL(pipe);
  3165. temp = I915_READ(reg);
  3166. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3167. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3168. POSTING_READ(reg);
  3169. udelay(100);
  3170. }
  3171. }
  3172. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3173. {
  3174. struct drm_device *dev = intel_crtc->base.dev;
  3175. struct drm_i915_private *dev_priv = dev->dev_private;
  3176. int pipe = intel_crtc->pipe;
  3177. i915_reg_t reg;
  3178. u32 temp;
  3179. /* Switch from PCDclk to Rawclk */
  3180. reg = FDI_RX_CTL(pipe);
  3181. temp = I915_READ(reg);
  3182. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3183. /* Disable CPU FDI TX PLL */
  3184. reg = FDI_TX_CTL(pipe);
  3185. temp = I915_READ(reg);
  3186. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3187. POSTING_READ(reg);
  3188. udelay(100);
  3189. reg = FDI_RX_CTL(pipe);
  3190. temp = I915_READ(reg);
  3191. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3192. /* Wait for the clocks to turn off. */
  3193. POSTING_READ(reg);
  3194. udelay(100);
  3195. }
  3196. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3197. {
  3198. struct drm_device *dev = crtc->dev;
  3199. struct drm_i915_private *dev_priv = dev->dev_private;
  3200. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3201. int pipe = intel_crtc->pipe;
  3202. i915_reg_t reg;
  3203. u32 temp;
  3204. /* disable CPU FDI tx and PCH FDI rx */
  3205. reg = FDI_TX_CTL(pipe);
  3206. temp = I915_READ(reg);
  3207. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3208. POSTING_READ(reg);
  3209. reg = FDI_RX_CTL(pipe);
  3210. temp = I915_READ(reg);
  3211. temp &= ~(0x7 << 16);
  3212. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3213. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3214. POSTING_READ(reg);
  3215. udelay(100);
  3216. /* Ironlake workaround, disable clock pointer after downing FDI */
  3217. if (HAS_PCH_IBX(dev))
  3218. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3219. /* still set train pattern 1 */
  3220. reg = FDI_TX_CTL(pipe);
  3221. temp = I915_READ(reg);
  3222. temp &= ~FDI_LINK_TRAIN_NONE;
  3223. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3224. I915_WRITE(reg, temp);
  3225. reg = FDI_RX_CTL(pipe);
  3226. temp = I915_READ(reg);
  3227. if (HAS_PCH_CPT(dev)) {
  3228. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3229. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3230. } else {
  3231. temp &= ~FDI_LINK_TRAIN_NONE;
  3232. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3233. }
  3234. /* BPC in FDI rx is consistent with that in PIPECONF */
  3235. temp &= ~(0x07 << 16);
  3236. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3237. I915_WRITE(reg, temp);
  3238. POSTING_READ(reg);
  3239. udelay(100);
  3240. }
  3241. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3242. {
  3243. struct intel_crtc *crtc;
  3244. /* Note that we don't need to be called with mode_config.lock here
  3245. * as our list of CRTC objects is static for the lifetime of the
  3246. * device and so cannot disappear as we iterate. Similarly, we can
  3247. * happily treat the predicates as racy, atomic checks as userspace
  3248. * cannot claim and pin a new fb without at least acquring the
  3249. * struct_mutex and so serialising with us.
  3250. */
  3251. for_each_intel_crtc(dev, crtc) {
  3252. if (atomic_read(&crtc->unpin_work_count) == 0)
  3253. continue;
  3254. if (crtc->unpin_work)
  3255. intel_wait_for_vblank(dev, crtc->pipe);
  3256. return true;
  3257. }
  3258. return false;
  3259. }
  3260. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3261. {
  3262. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3263. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3264. /* ensure that the unpin work is consistent wrt ->pending. */
  3265. smp_rmb();
  3266. intel_crtc->unpin_work = NULL;
  3267. if (work->event)
  3268. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3269. drm_crtc_vblank_put(&intel_crtc->base);
  3270. wake_up_all(&dev_priv->pending_flip_queue);
  3271. queue_work(dev_priv->wq, &work->work);
  3272. trace_i915_flip_complete(intel_crtc->plane,
  3273. work->pending_flip_obj);
  3274. }
  3275. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3276. {
  3277. struct drm_device *dev = crtc->dev;
  3278. struct drm_i915_private *dev_priv = dev->dev_private;
  3279. long ret;
  3280. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3281. ret = wait_event_interruptible_timeout(
  3282. dev_priv->pending_flip_queue,
  3283. !intel_crtc_has_pending_flip(crtc),
  3284. 60*HZ);
  3285. if (ret < 0)
  3286. return ret;
  3287. if (ret == 0) {
  3288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3289. spin_lock_irq(&dev->event_lock);
  3290. if (intel_crtc->unpin_work) {
  3291. WARN_ONCE(1, "Removing stuck page flip\n");
  3292. page_flip_completed(intel_crtc);
  3293. }
  3294. spin_unlock_irq(&dev->event_lock);
  3295. }
  3296. return 0;
  3297. }
  3298. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3299. {
  3300. u32 temp;
  3301. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3302. mutex_lock(&dev_priv->sb_lock);
  3303. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3304. temp |= SBI_SSCCTL_DISABLE;
  3305. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3306. mutex_unlock(&dev_priv->sb_lock);
  3307. }
  3308. /* Program iCLKIP clock to the desired frequency */
  3309. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3310. {
  3311. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3312. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3313. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3314. u32 temp;
  3315. lpt_disable_iclkip(dev_priv);
  3316. /* The iCLK virtual clock root frequency is in MHz,
  3317. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3318. * divisors, it is necessary to divide one by another, so we
  3319. * convert the virtual clock precision to KHz here for higher
  3320. * precision.
  3321. */
  3322. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3323. u32 iclk_virtual_root_freq = 172800 * 1000;
  3324. u32 iclk_pi_range = 64;
  3325. u32 desired_divisor;
  3326. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3327. clock << auxdiv);
  3328. divsel = (desired_divisor / iclk_pi_range) - 2;
  3329. phaseinc = desired_divisor % iclk_pi_range;
  3330. /*
  3331. * Near 20MHz is a corner case which is
  3332. * out of range for the 7-bit divisor
  3333. */
  3334. if (divsel <= 0x7f)
  3335. break;
  3336. }
  3337. /* This should not happen with any sane values */
  3338. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3339. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3340. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3341. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3342. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3343. clock,
  3344. auxdiv,
  3345. divsel,
  3346. phasedir,
  3347. phaseinc);
  3348. mutex_lock(&dev_priv->sb_lock);
  3349. /* Program SSCDIVINTPHASE6 */
  3350. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3351. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3352. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3353. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3354. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3355. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3356. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3357. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3358. /* Program SSCAUXDIV */
  3359. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3360. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3361. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3362. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3363. /* Enable modulator and associated divider */
  3364. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3365. temp &= ~SBI_SSCCTL_DISABLE;
  3366. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3367. mutex_unlock(&dev_priv->sb_lock);
  3368. /* Wait for initialization time */
  3369. udelay(24);
  3370. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3371. }
  3372. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3373. {
  3374. u32 divsel, phaseinc, auxdiv;
  3375. u32 iclk_virtual_root_freq = 172800 * 1000;
  3376. u32 iclk_pi_range = 64;
  3377. u32 desired_divisor;
  3378. u32 temp;
  3379. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3380. return 0;
  3381. mutex_lock(&dev_priv->sb_lock);
  3382. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3383. if (temp & SBI_SSCCTL_DISABLE) {
  3384. mutex_unlock(&dev_priv->sb_lock);
  3385. return 0;
  3386. }
  3387. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3388. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3389. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3390. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3391. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3392. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3393. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3394. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3395. mutex_unlock(&dev_priv->sb_lock);
  3396. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3397. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3398. desired_divisor << auxdiv);
  3399. }
  3400. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3401. enum pipe pch_transcoder)
  3402. {
  3403. struct drm_device *dev = crtc->base.dev;
  3404. struct drm_i915_private *dev_priv = dev->dev_private;
  3405. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3406. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3407. I915_READ(HTOTAL(cpu_transcoder)));
  3408. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3409. I915_READ(HBLANK(cpu_transcoder)));
  3410. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3411. I915_READ(HSYNC(cpu_transcoder)));
  3412. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3413. I915_READ(VTOTAL(cpu_transcoder)));
  3414. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3415. I915_READ(VBLANK(cpu_transcoder)));
  3416. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3417. I915_READ(VSYNC(cpu_transcoder)));
  3418. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3419. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3420. }
  3421. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3422. {
  3423. struct drm_i915_private *dev_priv = dev->dev_private;
  3424. uint32_t temp;
  3425. temp = I915_READ(SOUTH_CHICKEN1);
  3426. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3427. return;
  3428. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3429. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3430. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3431. if (enable)
  3432. temp |= FDI_BC_BIFURCATION_SELECT;
  3433. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3434. I915_WRITE(SOUTH_CHICKEN1, temp);
  3435. POSTING_READ(SOUTH_CHICKEN1);
  3436. }
  3437. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3438. {
  3439. struct drm_device *dev = intel_crtc->base.dev;
  3440. switch (intel_crtc->pipe) {
  3441. case PIPE_A:
  3442. break;
  3443. case PIPE_B:
  3444. if (intel_crtc->config->fdi_lanes > 2)
  3445. cpt_set_fdi_bc_bifurcation(dev, false);
  3446. else
  3447. cpt_set_fdi_bc_bifurcation(dev, true);
  3448. break;
  3449. case PIPE_C:
  3450. cpt_set_fdi_bc_bifurcation(dev, true);
  3451. break;
  3452. default:
  3453. BUG();
  3454. }
  3455. }
  3456. /* Return which DP Port should be selected for Transcoder DP control */
  3457. static enum port
  3458. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3459. {
  3460. struct drm_device *dev = crtc->dev;
  3461. struct intel_encoder *encoder;
  3462. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3463. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3464. encoder->type == INTEL_OUTPUT_EDP)
  3465. return enc_to_dig_port(&encoder->base)->port;
  3466. }
  3467. return -1;
  3468. }
  3469. /*
  3470. * Enable PCH resources required for PCH ports:
  3471. * - PCH PLLs
  3472. * - FDI training & RX/TX
  3473. * - update transcoder timings
  3474. * - DP transcoding bits
  3475. * - transcoder
  3476. */
  3477. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3478. {
  3479. struct drm_device *dev = crtc->dev;
  3480. struct drm_i915_private *dev_priv = dev->dev_private;
  3481. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3482. int pipe = intel_crtc->pipe;
  3483. u32 temp;
  3484. assert_pch_transcoder_disabled(dev_priv, pipe);
  3485. if (IS_IVYBRIDGE(dev))
  3486. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3487. /* Write the TU size bits before fdi link training, so that error
  3488. * detection works. */
  3489. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3490. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3491. /* For PCH output, training FDI link */
  3492. dev_priv->display.fdi_link_train(crtc);
  3493. /* We need to program the right clock selection before writing the pixel
  3494. * mutliplier into the DPLL. */
  3495. if (HAS_PCH_CPT(dev)) {
  3496. u32 sel;
  3497. temp = I915_READ(PCH_DPLL_SEL);
  3498. temp |= TRANS_DPLL_ENABLE(pipe);
  3499. sel = TRANS_DPLLB_SEL(pipe);
  3500. if (intel_crtc->config->shared_dpll ==
  3501. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3502. temp |= sel;
  3503. else
  3504. temp &= ~sel;
  3505. I915_WRITE(PCH_DPLL_SEL, temp);
  3506. }
  3507. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3508. * transcoder, and we actually should do this to not upset any PCH
  3509. * transcoder that already use the clock when we share it.
  3510. *
  3511. * Note that enable_shared_dpll tries to do the right thing, but
  3512. * get_shared_dpll unconditionally resets the pll - we need that to have
  3513. * the right LVDS enable sequence. */
  3514. intel_enable_shared_dpll(intel_crtc);
  3515. /* set transcoder timing, panel must allow it */
  3516. assert_panel_unlocked(dev_priv, pipe);
  3517. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3518. intel_fdi_normal_train(crtc);
  3519. /* For PCH DP, enable TRANS_DP_CTL */
  3520. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3521. const struct drm_display_mode *adjusted_mode =
  3522. &intel_crtc->config->base.adjusted_mode;
  3523. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3524. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3525. temp = I915_READ(reg);
  3526. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3527. TRANS_DP_SYNC_MASK |
  3528. TRANS_DP_BPC_MASK);
  3529. temp |= TRANS_DP_OUTPUT_ENABLE;
  3530. temp |= bpc << 9; /* same format but at 11:9 */
  3531. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3532. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3533. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3534. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3535. switch (intel_trans_dp_port_sel(crtc)) {
  3536. case PORT_B:
  3537. temp |= TRANS_DP_PORT_SEL_B;
  3538. break;
  3539. case PORT_C:
  3540. temp |= TRANS_DP_PORT_SEL_C;
  3541. break;
  3542. case PORT_D:
  3543. temp |= TRANS_DP_PORT_SEL_D;
  3544. break;
  3545. default:
  3546. BUG();
  3547. }
  3548. I915_WRITE(reg, temp);
  3549. }
  3550. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3551. }
  3552. static void lpt_pch_enable(struct drm_crtc *crtc)
  3553. {
  3554. struct drm_device *dev = crtc->dev;
  3555. struct drm_i915_private *dev_priv = dev->dev_private;
  3556. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3557. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3558. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3559. lpt_program_iclkip(crtc);
  3560. /* Set transcoder timing. */
  3561. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3562. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3563. }
  3564. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3565. {
  3566. struct drm_i915_private *dev_priv = dev->dev_private;
  3567. i915_reg_t dslreg = PIPEDSL(pipe);
  3568. u32 temp;
  3569. temp = I915_READ(dslreg);
  3570. udelay(500);
  3571. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3572. if (wait_for(I915_READ(dslreg) != temp, 5))
  3573. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3574. }
  3575. }
  3576. static int
  3577. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3578. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3579. int src_w, int src_h, int dst_w, int dst_h)
  3580. {
  3581. struct intel_crtc_scaler_state *scaler_state =
  3582. &crtc_state->scaler_state;
  3583. struct intel_crtc *intel_crtc =
  3584. to_intel_crtc(crtc_state->base.crtc);
  3585. int need_scaling;
  3586. need_scaling = intel_rotation_90_or_270(rotation) ?
  3587. (src_h != dst_w || src_w != dst_h):
  3588. (src_w != dst_w || src_h != dst_h);
  3589. /*
  3590. * if plane is being disabled or scaler is no more required or force detach
  3591. * - free scaler binded to this plane/crtc
  3592. * - in order to do this, update crtc->scaler_usage
  3593. *
  3594. * Here scaler state in crtc_state is set free so that
  3595. * scaler can be assigned to other user. Actual register
  3596. * update to free the scaler is done in plane/panel-fit programming.
  3597. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3598. */
  3599. if (force_detach || !need_scaling) {
  3600. if (*scaler_id >= 0) {
  3601. scaler_state->scaler_users &= ~(1 << scaler_user);
  3602. scaler_state->scalers[*scaler_id].in_use = 0;
  3603. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3604. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3605. intel_crtc->pipe, scaler_user, *scaler_id,
  3606. scaler_state->scaler_users);
  3607. *scaler_id = -1;
  3608. }
  3609. return 0;
  3610. }
  3611. /* range checks */
  3612. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3613. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3614. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3615. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3616. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3617. "size is out of scaler range\n",
  3618. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3619. return -EINVAL;
  3620. }
  3621. /* mark this plane as a scaler user in crtc_state */
  3622. scaler_state->scaler_users |= (1 << scaler_user);
  3623. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3624. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3625. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3626. scaler_state->scaler_users);
  3627. return 0;
  3628. }
  3629. /**
  3630. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3631. *
  3632. * @state: crtc's scaler state
  3633. *
  3634. * Return
  3635. * 0 - scaler_usage updated successfully
  3636. * error - requested scaling cannot be supported or other error condition
  3637. */
  3638. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3639. {
  3640. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3641. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3642. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3643. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3644. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3645. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3646. state->pipe_src_w, state->pipe_src_h,
  3647. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3648. }
  3649. /**
  3650. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3651. *
  3652. * @state: crtc's scaler state
  3653. * @plane_state: atomic plane state to update
  3654. *
  3655. * Return
  3656. * 0 - scaler_usage updated successfully
  3657. * error - requested scaling cannot be supported or other error condition
  3658. */
  3659. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3660. struct intel_plane_state *plane_state)
  3661. {
  3662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3663. struct intel_plane *intel_plane =
  3664. to_intel_plane(plane_state->base.plane);
  3665. struct drm_framebuffer *fb = plane_state->base.fb;
  3666. int ret;
  3667. bool force_detach = !fb || !plane_state->visible;
  3668. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3669. intel_plane->base.base.id, intel_crtc->pipe,
  3670. drm_plane_index(&intel_plane->base));
  3671. ret = skl_update_scaler(crtc_state, force_detach,
  3672. drm_plane_index(&intel_plane->base),
  3673. &plane_state->scaler_id,
  3674. plane_state->base.rotation,
  3675. drm_rect_width(&plane_state->src) >> 16,
  3676. drm_rect_height(&plane_state->src) >> 16,
  3677. drm_rect_width(&plane_state->dst),
  3678. drm_rect_height(&plane_state->dst));
  3679. if (ret || plane_state->scaler_id < 0)
  3680. return ret;
  3681. /* check colorkey */
  3682. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3683. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3684. intel_plane->base.base.id);
  3685. return -EINVAL;
  3686. }
  3687. /* Check src format */
  3688. switch (fb->pixel_format) {
  3689. case DRM_FORMAT_RGB565:
  3690. case DRM_FORMAT_XBGR8888:
  3691. case DRM_FORMAT_XRGB8888:
  3692. case DRM_FORMAT_ABGR8888:
  3693. case DRM_FORMAT_ARGB8888:
  3694. case DRM_FORMAT_XRGB2101010:
  3695. case DRM_FORMAT_XBGR2101010:
  3696. case DRM_FORMAT_YUYV:
  3697. case DRM_FORMAT_YVYU:
  3698. case DRM_FORMAT_UYVY:
  3699. case DRM_FORMAT_VYUY:
  3700. break;
  3701. default:
  3702. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3703. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3704. return -EINVAL;
  3705. }
  3706. return 0;
  3707. }
  3708. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3709. {
  3710. int i;
  3711. for (i = 0; i < crtc->num_scalers; i++)
  3712. skl_detach_scaler(crtc, i);
  3713. }
  3714. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3715. {
  3716. struct drm_device *dev = crtc->base.dev;
  3717. struct drm_i915_private *dev_priv = dev->dev_private;
  3718. int pipe = crtc->pipe;
  3719. struct intel_crtc_scaler_state *scaler_state =
  3720. &crtc->config->scaler_state;
  3721. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3722. if (crtc->config->pch_pfit.enabled) {
  3723. int id;
  3724. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3725. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3726. return;
  3727. }
  3728. id = scaler_state->scaler_id;
  3729. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3730. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3731. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3732. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3733. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3734. }
  3735. }
  3736. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3737. {
  3738. struct drm_device *dev = crtc->base.dev;
  3739. struct drm_i915_private *dev_priv = dev->dev_private;
  3740. int pipe = crtc->pipe;
  3741. if (crtc->config->pch_pfit.enabled) {
  3742. /* Force use of hard-coded filter coefficients
  3743. * as some pre-programmed values are broken,
  3744. * e.g. x201.
  3745. */
  3746. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3747. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3748. PF_PIPE_SEL_IVB(pipe));
  3749. else
  3750. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3751. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3752. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3753. }
  3754. }
  3755. void hsw_enable_ips(struct intel_crtc *crtc)
  3756. {
  3757. struct drm_device *dev = crtc->base.dev;
  3758. struct drm_i915_private *dev_priv = dev->dev_private;
  3759. if (!crtc->config->ips_enabled)
  3760. return;
  3761. /*
  3762. * We can only enable IPS after we enable a plane and wait for a vblank
  3763. * This function is called from post_plane_update, which is run after
  3764. * a vblank wait.
  3765. */
  3766. assert_plane_enabled(dev_priv, crtc->plane);
  3767. if (IS_BROADWELL(dev)) {
  3768. mutex_lock(&dev_priv->rps.hw_lock);
  3769. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3770. mutex_unlock(&dev_priv->rps.hw_lock);
  3771. /* Quoting Art Runyan: "its not safe to expect any particular
  3772. * value in IPS_CTL bit 31 after enabling IPS through the
  3773. * mailbox." Moreover, the mailbox may return a bogus state,
  3774. * so we need to just enable it and continue on.
  3775. */
  3776. } else {
  3777. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3778. /* The bit only becomes 1 in the next vblank, so this wait here
  3779. * is essentially intel_wait_for_vblank. If we don't have this
  3780. * and don't wait for vblanks until the end of crtc_enable, then
  3781. * the HW state readout code will complain that the expected
  3782. * IPS_CTL value is not the one we read. */
  3783. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3784. DRM_ERROR("Timed out waiting for IPS enable\n");
  3785. }
  3786. }
  3787. void hsw_disable_ips(struct intel_crtc *crtc)
  3788. {
  3789. struct drm_device *dev = crtc->base.dev;
  3790. struct drm_i915_private *dev_priv = dev->dev_private;
  3791. if (!crtc->config->ips_enabled)
  3792. return;
  3793. assert_plane_enabled(dev_priv, crtc->plane);
  3794. if (IS_BROADWELL(dev)) {
  3795. mutex_lock(&dev_priv->rps.hw_lock);
  3796. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3797. mutex_unlock(&dev_priv->rps.hw_lock);
  3798. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3799. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3800. DRM_ERROR("Timed out waiting for IPS disable\n");
  3801. } else {
  3802. I915_WRITE(IPS_CTL, 0);
  3803. POSTING_READ(IPS_CTL);
  3804. }
  3805. /* We need to wait for a vblank before we can disable the plane. */
  3806. intel_wait_for_vblank(dev, crtc->pipe);
  3807. }
  3808. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3809. {
  3810. if (intel_crtc->overlay) {
  3811. struct drm_device *dev = intel_crtc->base.dev;
  3812. struct drm_i915_private *dev_priv = dev->dev_private;
  3813. mutex_lock(&dev->struct_mutex);
  3814. dev_priv->mm.interruptible = false;
  3815. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3816. dev_priv->mm.interruptible = true;
  3817. mutex_unlock(&dev->struct_mutex);
  3818. }
  3819. /* Let userspace switch the overlay on again. In most cases userspace
  3820. * has to recompute where to put it anyway.
  3821. */
  3822. }
  3823. /**
  3824. * intel_post_enable_primary - Perform operations after enabling primary plane
  3825. * @crtc: the CRTC whose primary plane was just enabled
  3826. *
  3827. * Performs potentially sleeping operations that must be done after the primary
  3828. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3829. * called due to an explicit primary plane update, or due to an implicit
  3830. * re-enable that is caused when a sprite plane is updated to no longer
  3831. * completely hide the primary plane.
  3832. */
  3833. static void
  3834. intel_post_enable_primary(struct drm_crtc *crtc)
  3835. {
  3836. struct drm_device *dev = crtc->dev;
  3837. struct drm_i915_private *dev_priv = dev->dev_private;
  3838. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3839. int pipe = intel_crtc->pipe;
  3840. /*
  3841. * FIXME IPS should be fine as long as one plane is
  3842. * enabled, but in practice it seems to have problems
  3843. * when going from primary only to sprite only and vice
  3844. * versa.
  3845. */
  3846. hsw_enable_ips(intel_crtc);
  3847. /*
  3848. * Gen2 reports pipe underruns whenever all planes are disabled.
  3849. * So don't enable underrun reporting before at least some planes
  3850. * are enabled.
  3851. * FIXME: Need to fix the logic to work when we turn off all planes
  3852. * but leave the pipe running.
  3853. */
  3854. if (IS_GEN2(dev))
  3855. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3856. /* Underruns don't always raise interrupts, so check manually. */
  3857. intel_check_cpu_fifo_underruns(dev_priv);
  3858. intel_check_pch_fifo_underruns(dev_priv);
  3859. }
  3860. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3861. static void
  3862. intel_pre_disable_primary(struct drm_crtc *crtc)
  3863. {
  3864. struct drm_device *dev = crtc->dev;
  3865. struct drm_i915_private *dev_priv = dev->dev_private;
  3866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3867. int pipe = intel_crtc->pipe;
  3868. /*
  3869. * Gen2 reports pipe underruns whenever all planes are disabled.
  3870. * So diasble underrun reporting before all the planes get disabled.
  3871. * FIXME: Need to fix the logic to work when we turn off all planes
  3872. * but leave the pipe running.
  3873. */
  3874. if (IS_GEN2(dev))
  3875. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3876. /*
  3877. * FIXME IPS should be fine as long as one plane is
  3878. * enabled, but in practice it seems to have problems
  3879. * when going from primary only to sprite only and vice
  3880. * versa.
  3881. */
  3882. hsw_disable_ips(intel_crtc);
  3883. }
  3884. /* FIXME get rid of this and use pre_plane_update */
  3885. static void
  3886. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3887. {
  3888. struct drm_device *dev = crtc->dev;
  3889. struct drm_i915_private *dev_priv = dev->dev_private;
  3890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3891. int pipe = intel_crtc->pipe;
  3892. intel_pre_disable_primary(crtc);
  3893. /*
  3894. * Vblank time updates from the shadow to live plane control register
  3895. * are blocked if the memory self-refresh mode is active at that
  3896. * moment. So to make sure the plane gets truly disabled, disable
  3897. * first the self-refresh mode. The self-refresh enable bit in turn
  3898. * will be checked/applied by the HW only at the next frame start
  3899. * event which is after the vblank start event, so we need to have a
  3900. * wait-for-vblank between disabling the plane and the pipe.
  3901. */
  3902. if (HAS_GMCH_DISPLAY(dev)) {
  3903. intel_set_memory_cxsr(dev_priv, false);
  3904. dev_priv->wm.vlv.cxsr = false;
  3905. intel_wait_for_vblank(dev, pipe);
  3906. }
  3907. }
  3908. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3909. {
  3910. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3911. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3912. struct intel_crtc_state *pipe_config =
  3913. to_intel_crtc_state(crtc->base.state);
  3914. struct drm_device *dev = crtc->base.dev;
  3915. struct drm_plane *primary = crtc->base.primary;
  3916. struct drm_plane_state *old_pri_state =
  3917. drm_atomic_get_existing_plane_state(old_state, primary);
  3918. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3919. crtc->wm.cxsr_allowed = true;
  3920. if (pipe_config->update_wm_post && pipe_config->base.active)
  3921. intel_update_watermarks(&crtc->base);
  3922. if (old_pri_state) {
  3923. struct intel_plane_state *primary_state =
  3924. to_intel_plane_state(primary->state);
  3925. struct intel_plane_state *old_primary_state =
  3926. to_intel_plane_state(old_pri_state);
  3927. intel_fbc_post_update(crtc);
  3928. if (primary_state->visible &&
  3929. (needs_modeset(&pipe_config->base) ||
  3930. !old_primary_state->visible))
  3931. intel_post_enable_primary(&crtc->base);
  3932. }
  3933. }
  3934. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3935. {
  3936. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3937. struct drm_device *dev = crtc->base.dev;
  3938. struct drm_i915_private *dev_priv = dev->dev_private;
  3939. struct intel_crtc_state *pipe_config =
  3940. to_intel_crtc_state(crtc->base.state);
  3941. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3942. struct drm_plane *primary = crtc->base.primary;
  3943. struct drm_plane_state *old_pri_state =
  3944. drm_atomic_get_existing_plane_state(old_state, primary);
  3945. bool modeset = needs_modeset(&pipe_config->base);
  3946. if (old_pri_state) {
  3947. struct intel_plane_state *primary_state =
  3948. to_intel_plane_state(primary->state);
  3949. struct intel_plane_state *old_primary_state =
  3950. to_intel_plane_state(old_pri_state);
  3951. intel_fbc_pre_update(crtc);
  3952. if (old_primary_state->visible &&
  3953. (modeset || !primary_state->visible))
  3954. intel_pre_disable_primary(&crtc->base);
  3955. }
  3956. if (pipe_config->disable_cxsr) {
  3957. crtc->wm.cxsr_allowed = false;
  3958. /*
  3959. * Vblank time updates from the shadow to live plane control register
  3960. * are blocked if the memory self-refresh mode is active at that
  3961. * moment. So to make sure the plane gets truly disabled, disable
  3962. * first the self-refresh mode. The self-refresh enable bit in turn
  3963. * will be checked/applied by the HW only at the next frame start
  3964. * event which is after the vblank start event, so we need to have a
  3965. * wait-for-vblank between disabling the plane and the pipe.
  3966. */
  3967. if (old_crtc_state->base.active) {
  3968. intel_set_memory_cxsr(dev_priv, false);
  3969. dev_priv->wm.vlv.cxsr = false;
  3970. intel_wait_for_vblank(dev, crtc->pipe);
  3971. }
  3972. }
  3973. /*
  3974. * IVB workaround: must disable low power watermarks for at least
  3975. * one frame before enabling scaling. LP watermarks can be re-enabled
  3976. * when scaling is disabled.
  3977. *
  3978. * WaCxSRDisabledForSpriteScaling:ivb
  3979. */
  3980. if (pipe_config->disable_lp_wm) {
  3981. ilk_disable_lp_wm(dev);
  3982. intel_wait_for_vblank(dev, crtc->pipe);
  3983. }
  3984. /*
  3985. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3986. * watermark programming here.
  3987. */
  3988. if (needs_modeset(&pipe_config->base))
  3989. return;
  3990. /*
  3991. * For platforms that support atomic watermarks, program the
  3992. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3993. * will be the intermediate values that are safe for both pre- and
  3994. * post- vblank; when vblank happens, the 'active' values will be set
  3995. * to the final 'target' values and we'll do this again to get the
  3996. * optimal watermarks. For gen9+ platforms, the values we program here
  3997. * will be the final target values which will get automatically latched
  3998. * at vblank time; no further programming will be necessary.
  3999. *
  4000. * If a platform hasn't been transitioned to atomic watermarks yet,
  4001. * we'll continue to update watermarks the old way, if flags tell
  4002. * us to.
  4003. */
  4004. if (dev_priv->display.initial_watermarks != NULL)
  4005. dev_priv->display.initial_watermarks(pipe_config);
  4006. else if (pipe_config->update_wm_pre)
  4007. intel_update_watermarks(&crtc->base);
  4008. }
  4009. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4010. {
  4011. struct drm_device *dev = crtc->dev;
  4012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4013. struct drm_plane *p;
  4014. int pipe = intel_crtc->pipe;
  4015. intel_crtc_dpms_overlay_disable(intel_crtc);
  4016. drm_for_each_plane_mask(p, dev, plane_mask)
  4017. to_intel_plane(p)->disable_plane(p, crtc);
  4018. /*
  4019. * FIXME: Once we grow proper nuclear flip support out of this we need
  4020. * to compute the mask of flip planes precisely. For the time being
  4021. * consider this a flip to a NULL plane.
  4022. */
  4023. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4024. }
  4025. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4026. {
  4027. struct drm_device *dev = crtc->dev;
  4028. struct drm_i915_private *dev_priv = dev->dev_private;
  4029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4030. struct intel_encoder *encoder;
  4031. int pipe = intel_crtc->pipe;
  4032. struct intel_crtc_state *pipe_config =
  4033. to_intel_crtc_state(crtc->state);
  4034. if (WARN_ON(intel_crtc->active))
  4035. return;
  4036. /*
  4037. * Sometimes spurious CPU pipe underruns happen during FDI
  4038. * training, at least with VGA+HDMI cloning. Suppress them.
  4039. *
  4040. * On ILK we get an occasional spurious CPU pipe underruns
  4041. * between eDP port A enable and vdd enable. Also PCH port
  4042. * enable seems to result in the occasional CPU pipe underrun.
  4043. *
  4044. * Spurious PCH underruns also occur during PCH enabling.
  4045. */
  4046. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4047. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4048. if (intel_crtc->config->has_pch_encoder)
  4049. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4050. if (intel_crtc->config->has_pch_encoder)
  4051. intel_prepare_shared_dpll(intel_crtc);
  4052. if (intel_crtc->config->has_dp_encoder)
  4053. intel_dp_set_m_n(intel_crtc, M1_N1);
  4054. intel_set_pipe_timings(intel_crtc);
  4055. intel_set_pipe_src_size(intel_crtc);
  4056. if (intel_crtc->config->has_pch_encoder) {
  4057. intel_cpu_transcoder_set_m_n(intel_crtc,
  4058. &intel_crtc->config->fdi_m_n, NULL);
  4059. }
  4060. ironlake_set_pipeconf(crtc);
  4061. intel_crtc->active = true;
  4062. for_each_encoder_on_crtc(dev, crtc, encoder)
  4063. if (encoder->pre_enable)
  4064. encoder->pre_enable(encoder);
  4065. if (intel_crtc->config->has_pch_encoder) {
  4066. /* Note: FDI PLL enabling _must_ be done before we enable the
  4067. * cpu pipes, hence this is separate from all the other fdi/pch
  4068. * enabling. */
  4069. ironlake_fdi_pll_enable(intel_crtc);
  4070. } else {
  4071. assert_fdi_tx_disabled(dev_priv, pipe);
  4072. assert_fdi_rx_disabled(dev_priv, pipe);
  4073. }
  4074. ironlake_pfit_enable(intel_crtc);
  4075. /*
  4076. * On ILK+ LUT must be loaded before the pipe is running but with
  4077. * clocks enabled
  4078. */
  4079. intel_color_load_luts(&pipe_config->base);
  4080. if (dev_priv->display.initial_watermarks != NULL)
  4081. dev_priv->display.initial_watermarks(intel_crtc->config);
  4082. intel_enable_pipe(intel_crtc);
  4083. if (intel_crtc->config->has_pch_encoder)
  4084. ironlake_pch_enable(crtc);
  4085. assert_vblank_disabled(crtc);
  4086. drm_crtc_vblank_on(crtc);
  4087. for_each_encoder_on_crtc(dev, crtc, encoder)
  4088. encoder->enable(encoder);
  4089. if (HAS_PCH_CPT(dev))
  4090. cpt_verify_modeset(dev, intel_crtc->pipe);
  4091. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4092. if (intel_crtc->config->has_pch_encoder)
  4093. intel_wait_for_vblank(dev, pipe);
  4094. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4095. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4096. }
  4097. /* IPS only exists on ULT machines and is tied to pipe A. */
  4098. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4099. {
  4100. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4101. }
  4102. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4103. {
  4104. struct drm_device *dev = crtc->dev;
  4105. struct drm_i915_private *dev_priv = dev->dev_private;
  4106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4107. struct intel_encoder *encoder;
  4108. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4109. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4110. struct intel_crtc_state *pipe_config =
  4111. to_intel_crtc_state(crtc->state);
  4112. if (WARN_ON(intel_crtc->active))
  4113. return;
  4114. if (intel_crtc->config->has_pch_encoder)
  4115. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4116. false);
  4117. if (intel_crtc->config->shared_dpll)
  4118. intel_enable_shared_dpll(intel_crtc);
  4119. if (intel_crtc->config->has_dp_encoder)
  4120. intel_dp_set_m_n(intel_crtc, M1_N1);
  4121. if (!intel_crtc->config->has_dsi_encoder)
  4122. intel_set_pipe_timings(intel_crtc);
  4123. intel_set_pipe_src_size(intel_crtc);
  4124. if (cpu_transcoder != TRANSCODER_EDP &&
  4125. !transcoder_is_dsi(cpu_transcoder)) {
  4126. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4127. intel_crtc->config->pixel_multiplier - 1);
  4128. }
  4129. if (intel_crtc->config->has_pch_encoder) {
  4130. intel_cpu_transcoder_set_m_n(intel_crtc,
  4131. &intel_crtc->config->fdi_m_n, NULL);
  4132. }
  4133. if (!intel_crtc->config->has_dsi_encoder)
  4134. haswell_set_pipeconf(crtc);
  4135. haswell_set_pipemisc(crtc);
  4136. intel_color_set_csc(&pipe_config->base);
  4137. intel_crtc->active = true;
  4138. if (intel_crtc->config->has_pch_encoder)
  4139. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4140. else
  4141. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4142. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4143. if (encoder->pre_enable)
  4144. encoder->pre_enable(encoder);
  4145. }
  4146. if (intel_crtc->config->has_pch_encoder)
  4147. dev_priv->display.fdi_link_train(crtc);
  4148. if (!intel_crtc->config->has_dsi_encoder)
  4149. intel_ddi_enable_pipe_clock(intel_crtc);
  4150. if (INTEL_INFO(dev)->gen >= 9)
  4151. skylake_pfit_enable(intel_crtc);
  4152. else
  4153. ironlake_pfit_enable(intel_crtc);
  4154. /*
  4155. * On ILK+ LUT must be loaded before the pipe is running but with
  4156. * clocks enabled
  4157. */
  4158. intel_color_load_luts(&pipe_config->base);
  4159. intel_ddi_set_pipe_settings(crtc);
  4160. if (!intel_crtc->config->has_dsi_encoder)
  4161. intel_ddi_enable_transcoder_func(crtc);
  4162. if (dev_priv->display.initial_watermarks != NULL)
  4163. dev_priv->display.initial_watermarks(pipe_config);
  4164. else
  4165. intel_update_watermarks(crtc);
  4166. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4167. if (!intel_crtc->config->has_dsi_encoder)
  4168. intel_enable_pipe(intel_crtc);
  4169. if (intel_crtc->config->has_pch_encoder)
  4170. lpt_pch_enable(crtc);
  4171. if (intel_crtc->config->dp_encoder_is_mst)
  4172. intel_ddi_set_vc_payload_alloc(crtc, true);
  4173. assert_vblank_disabled(crtc);
  4174. drm_crtc_vblank_on(crtc);
  4175. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4176. encoder->enable(encoder);
  4177. intel_opregion_notify_encoder(encoder, true);
  4178. }
  4179. if (intel_crtc->config->has_pch_encoder) {
  4180. intel_wait_for_vblank(dev, pipe);
  4181. intel_wait_for_vblank(dev, pipe);
  4182. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4183. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4184. true);
  4185. }
  4186. /* If we change the relative order between pipe/planes enabling, we need
  4187. * to change the workaround. */
  4188. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4189. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4190. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4191. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4192. }
  4193. }
  4194. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4195. {
  4196. struct drm_device *dev = crtc->base.dev;
  4197. struct drm_i915_private *dev_priv = dev->dev_private;
  4198. int pipe = crtc->pipe;
  4199. /* To avoid upsetting the power well on haswell only disable the pfit if
  4200. * it's in use. The hw state code will make sure we get this right. */
  4201. if (force || crtc->config->pch_pfit.enabled) {
  4202. I915_WRITE(PF_CTL(pipe), 0);
  4203. I915_WRITE(PF_WIN_POS(pipe), 0);
  4204. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4205. }
  4206. }
  4207. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4208. {
  4209. struct drm_device *dev = crtc->dev;
  4210. struct drm_i915_private *dev_priv = dev->dev_private;
  4211. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4212. struct intel_encoder *encoder;
  4213. int pipe = intel_crtc->pipe;
  4214. /*
  4215. * Sometimes spurious CPU pipe underruns happen when the
  4216. * pipe is already disabled, but FDI RX/TX is still enabled.
  4217. * Happens at least with VGA+HDMI cloning. Suppress them.
  4218. */
  4219. if (intel_crtc->config->has_pch_encoder) {
  4220. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4221. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4222. }
  4223. for_each_encoder_on_crtc(dev, crtc, encoder)
  4224. encoder->disable(encoder);
  4225. drm_crtc_vblank_off(crtc);
  4226. assert_vblank_disabled(crtc);
  4227. intel_disable_pipe(intel_crtc);
  4228. ironlake_pfit_disable(intel_crtc, false);
  4229. if (intel_crtc->config->has_pch_encoder)
  4230. ironlake_fdi_disable(crtc);
  4231. for_each_encoder_on_crtc(dev, crtc, encoder)
  4232. if (encoder->post_disable)
  4233. encoder->post_disable(encoder);
  4234. if (intel_crtc->config->has_pch_encoder) {
  4235. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4236. if (HAS_PCH_CPT(dev)) {
  4237. i915_reg_t reg;
  4238. u32 temp;
  4239. /* disable TRANS_DP_CTL */
  4240. reg = TRANS_DP_CTL(pipe);
  4241. temp = I915_READ(reg);
  4242. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4243. TRANS_DP_PORT_SEL_MASK);
  4244. temp |= TRANS_DP_PORT_SEL_NONE;
  4245. I915_WRITE(reg, temp);
  4246. /* disable DPLL_SEL */
  4247. temp = I915_READ(PCH_DPLL_SEL);
  4248. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4249. I915_WRITE(PCH_DPLL_SEL, temp);
  4250. }
  4251. ironlake_fdi_pll_disable(intel_crtc);
  4252. }
  4253. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4254. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4255. }
  4256. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4257. {
  4258. struct drm_device *dev = crtc->dev;
  4259. struct drm_i915_private *dev_priv = dev->dev_private;
  4260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4261. struct intel_encoder *encoder;
  4262. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4263. if (intel_crtc->config->has_pch_encoder)
  4264. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4265. false);
  4266. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4267. intel_opregion_notify_encoder(encoder, false);
  4268. encoder->disable(encoder);
  4269. }
  4270. drm_crtc_vblank_off(crtc);
  4271. assert_vblank_disabled(crtc);
  4272. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4273. if (!intel_crtc->config->has_dsi_encoder)
  4274. intel_disable_pipe(intel_crtc);
  4275. if (intel_crtc->config->dp_encoder_is_mst)
  4276. intel_ddi_set_vc_payload_alloc(crtc, false);
  4277. if (!intel_crtc->config->has_dsi_encoder)
  4278. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4279. if (INTEL_INFO(dev)->gen >= 9)
  4280. skylake_scaler_disable(intel_crtc);
  4281. else
  4282. ironlake_pfit_disable(intel_crtc, false);
  4283. if (!intel_crtc->config->has_dsi_encoder)
  4284. intel_ddi_disable_pipe_clock(intel_crtc);
  4285. for_each_encoder_on_crtc(dev, crtc, encoder)
  4286. if (encoder->post_disable)
  4287. encoder->post_disable(encoder);
  4288. if (intel_crtc->config->has_pch_encoder) {
  4289. lpt_disable_pch_transcoder(dev_priv);
  4290. lpt_disable_iclkip(dev_priv);
  4291. intel_ddi_fdi_disable(crtc);
  4292. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4293. true);
  4294. }
  4295. }
  4296. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4297. {
  4298. struct drm_device *dev = crtc->base.dev;
  4299. struct drm_i915_private *dev_priv = dev->dev_private;
  4300. struct intel_crtc_state *pipe_config = crtc->config;
  4301. if (!pipe_config->gmch_pfit.control)
  4302. return;
  4303. /*
  4304. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4305. * according to register description and PRM.
  4306. */
  4307. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4308. assert_pipe_disabled(dev_priv, crtc->pipe);
  4309. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4310. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4311. /* Border color in case we don't scale up to the full screen. Black by
  4312. * default, change to something else for debugging. */
  4313. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4314. }
  4315. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4316. {
  4317. switch (port) {
  4318. case PORT_A:
  4319. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4320. case PORT_B:
  4321. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4322. case PORT_C:
  4323. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4324. case PORT_D:
  4325. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4326. case PORT_E:
  4327. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4328. default:
  4329. MISSING_CASE(port);
  4330. return POWER_DOMAIN_PORT_OTHER;
  4331. }
  4332. }
  4333. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4334. {
  4335. switch (port) {
  4336. case PORT_A:
  4337. return POWER_DOMAIN_AUX_A;
  4338. case PORT_B:
  4339. return POWER_DOMAIN_AUX_B;
  4340. case PORT_C:
  4341. return POWER_DOMAIN_AUX_C;
  4342. case PORT_D:
  4343. return POWER_DOMAIN_AUX_D;
  4344. case PORT_E:
  4345. /* FIXME: Check VBT for actual wiring of PORT E */
  4346. return POWER_DOMAIN_AUX_D;
  4347. default:
  4348. MISSING_CASE(port);
  4349. return POWER_DOMAIN_AUX_A;
  4350. }
  4351. }
  4352. enum intel_display_power_domain
  4353. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4354. {
  4355. struct drm_device *dev = intel_encoder->base.dev;
  4356. struct intel_digital_port *intel_dig_port;
  4357. switch (intel_encoder->type) {
  4358. case INTEL_OUTPUT_UNKNOWN:
  4359. /* Only DDI platforms should ever use this output type */
  4360. WARN_ON_ONCE(!HAS_DDI(dev));
  4361. case INTEL_OUTPUT_DISPLAYPORT:
  4362. case INTEL_OUTPUT_HDMI:
  4363. case INTEL_OUTPUT_EDP:
  4364. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4365. return port_to_power_domain(intel_dig_port->port);
  4366. case INTEL_OUTPUT_DP_MST:
  4367. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4368. return port_to_power_domain(intel_dig_port->port);
  4369. case INTEL_OUTPUT_ANALOG:
  4370. return POWER_DOMAIN_PORT_CRT;
  4371. case INTEL_OUTPUT_DSI:
  4372. return POWER_DOMAIN_PORT_DSI;
  4373. default:
  4374. return POWER_DOMAIN_PORT_OTHER;
  4375. }
  4376. }
  4377. enum intel_display_power_domain
  4378. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4379. {
  4380. struct drm_device *dev = intel_encoder->base.dev;
  4381. struct intel_digital_port *intel_dig_port;
  4382. switch (intel_encoder->type) {
  4383. case INTEL_OUTPUT_UNKNOWN:
  4384. case INTEL_OUTPUT_HDMI:
  4385. /*
  4386. * Only DDI platforms should ever use these output types.
  4387. * We can get here after the HDMI detect code has already set
  4388. * the type of the shared encoder. Since we can't be sure
  4389. * what's the status of the given connectors, play safe and
  4390. * run the DP detection too.
  4391. */
  4392. WARN_ON_ONCE(!HAS_DDI(dev));
  4393. case INTEL_OUTPUT_DISPLAYPORT:
  4394. case INTEL_OUTPUT_EDP:
  4395. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4396. return port_to_aux_power_domain(intel_dig_port->port);
  4397. case INTEL_OUTPUT_DP_MST:
  4398. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4399. return port_to_aux_power_domain(intel_dig_port->port);
  4400. default:
  4401. MISSING_CASE(intel_encoder->type);
  4402. return POWER_DOMAIN_AUX_A;
  4403. }
  4404. }
  4405. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4406. struct intel_crtc_state *crtc_state)
  4407. {
  4408. struct drm_device *dev = crtc->dev;
  4409. struct drm_encoder *encoder;
  4410. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4411. enum pipe pipe = intel_crtc->pipe;
  4412. unsigned long mask;
  4413. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4414. if (!crtc_state->base.active)
  4415. return 0;
  4416. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4417. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4418. if (crtc_state->pch_pfit.enabled ||
  4419. crtc_state->pch_pfit.force_thru)
  4420. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4421. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4422. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4423. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4424. }
  4425. if (crtc_state->shared_dpll)
  4426. mask |= BIT(POWER_DOMAIN_PLLS);
  4427. return mask;
  4428. }
  4429. static unsigned long
  4430. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4431. struct intel_crtc_state *crtc_state)
  4432. {
  4433. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4435. enum intel_display_power_domain domain;
  4436. unsigned long domains, new_domains, old_domains;
  4437. old_domains = intel_crtc->enabled_power_domains;
  4438. intel_crtc->enabled_power_domains = new_domains =
  4439. get_crtc_power_domains(crtc, crtc_state);
  4440. domains = new_domains & ~old_domains;
  4441. for_each_power_domain(domain, domains)
  4442. intel_display_power_get(dev_priv, domain);
  4443. return old_domains & ~new_domains;
  4444. }
  4445. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4446. unsigned long domains)
  4447. {
  4448. enum intel_display_power_domain domain;
  4449. for_each_power_domain(domain, domains)
  4450. intel_display_power_put(dev_priv, domain);
  4451. }
  4452. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4453. {
  4454. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4455. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4456. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4457. return max_cdclk_freq;
  4458. else if (IS_CHERRYVIEW(dev_priv))
  4459. return max_cdclk_freq*95/100;
  4460. else if (INTEL_INFO(dev_priv)->gen < 4)
  4461. return 2*max_cdclk_freq*90/100;
  4462. else
  4463. return max_cdclk_freq*90/100;
  4464. }
  4465. static void intel_update_max_cdclk(struct drm_device *dev)
  4466. {
  4467. struct drm_i915_private *dev_priv = dev->dev_private;
  4468. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4469. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4470. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4471. dev_priv->max_cdclk_freq = 675000;
  4472. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4473. dev_priv->max_cdclk_freq = 540000;
  4474. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4475. dev_priv->max_cdclk_freq = 450000;
  4476. else
  4477. dev_priv->max_cdclk_freq = 337500;
  4478. } else if (IS_BROXTON(dev)) {
  4479. dev_priv->max_cdclk_freq = 624000;
  4480. } else if (IS_BROADWELL(dev)) {
  4481. /*
  4482. * FIXME with extra cooling we can allow
  4483. * 540 MHz for ULX and 675 Mhz for ULT.
  4484. * How can we know if extra cooling is
  4485. * available? PCI ID, VTB, something else?
  4486. */
  4487. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4488. dev_priv->max_cdclk_freq = 450000;
  4489. else if (IS_BDW_ULX(dev))
  4490. dev_priv->max_cdclk_freq = 450000;
  4491. else if (IS_BDW_ULT(dev))
  4492. dev_priv->max_cdclk_freq = 540000;
  4493. else
  4494. dev_priv->max_cdclk_freq = 675000;
  4495. } else if (IS_CHERRYVIEW(dev)) {
  4496. dev_priv->max_cdclk_freq = 320000;
  4497. } else if (IS_VALLEYVIEW(dev)) {
  4498. dev_priv->max_cdclk_freq = 400000;
  4499. } else {
  4500. /* otherwise assume cdclk is fixed */
  4501. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4502. }
  4503. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4504. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4505. dev_priv->max_cdclk_freq);
  4506. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4507. dev_priv->max_dotclk_freq);
  4508. }
  4509. static void intel_update_cdclk(struct drm_device *dev)
  4510. {
  4511. struct drm_i915_private *dev_priv = dev->dev_private;
  4512. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4513. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4514. dev_priv->cdclk_freq);
  4515. /*
  4516. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4517. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4518. * of cdclk that generates 4MHz reference clock freq which is used to
  4519. * generate GMBus clock. This will vary with the cdclk freq.
  4520. */
  4521. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4522. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4523. if (dev_priv->max_cdclk_freq == 0)
  4524. intel_update_max_cdclk(dev);
  4525. }
  4526. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4527. static int skl_cdclk_decimal(int cdclk)
  4528. {
  4529. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4530. }
  4531. static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4532. {
  4533. uint32_t divider;
  4534. uint32_t ratio;
  4535. uint32_t current_cdclk;
  4536. int ret;
  4537. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4538. switch (cdclk) {
  4539. case 144000:
  4540. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4541. ratio = BXT_DE_PLL_RATIO(60);
  4542. break;
  4543. case 288000:
  4544. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4545. ratio = BXT_DE_PLL_RATIO(60);
  4546. break;
  4547. case 384000:
  4548. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4549. ratio = BXT_DE_PLL_RATIO(60);
  4550. break;
  4551. case 576000:
  4552. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4553. ratio = BXT_DE_PLL_RATIO(60);
  4554. break;
  4555. case 624000:
  4556. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4557. ratio = BXT_DE_PLL_RATIO(65);
  4558. break;
  4559. case 19200:
  4560. /*
  4561. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4562. * to suppress GCC warning.
  4563. */
  4564. ratio = 0;
  4565. divider = 0;
  4566. break;
  4567. default:
  4568. DRM_ERROR("unsupported CDCLK freq %d", cdclk);
  4569. return;
  4570. }
  4571. mutex_lock(&dev_priv->rps.hw_lock);
  4572. /* Inform power controller of upcoming frequency change */
  4573. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4574. 0x80000000);
  4575. mutex_unlock(&dev_priv->rps.hw_lock);
  4576. if (ret) {
  4577. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4578. ret, cdclk);
  4579. return;
  4580. }
  4581. current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4582. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4583. current_cdclk = current_cdclk * 500 + 1000;
  4584. /*
  4585. * DE PLL has to be disabled when
  4586. * - setting to 19.2MHz (bypass, PLL isn't used)
  4587. * - before setting to 624MHz (PLL needs toggling)
  4588. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4589. */
  4590. if (cdclk == 19200 || cdclk == 624000 ||
  4591. current_cdclk == 624000) {
  4592. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4593. /* Timeout 200us */
  4594. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4595. 1))
  4596. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4597. }
  4598. if (cdclk != 19200) {
  4599. uint32_t val;
  4600. val = I915_READ(BXT_DE_PLL_CTL);
  4601. val &= ~BXT_DE_PLL_RATIO_MASK;
  4602. val |= ratio;
  4603. I915_WRITE(BXT_DE_PLL_CTL, val);
  4604. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4605. /* Timeout 200us */
  4606. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4607. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4608. val = I915_READ(CDCLK_CTL);
  4609. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4610. val |= divider;
  4611. /*
  4612. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4613. * enable otherwise.
  4614. */
  4615. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4616. if (cdclk >= 500000)
  4617. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4618. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4619. val |= skl_cdclk_decimal(cdclk);
  4620. I915_WRITE(CDCLK_CTL, val);
  4621. }
  4622. mutex_lock(&dev_priv->rps.hw_lock);
  4623. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4624. DIV_ROUND_UP(cdclk, 25000));
  4625. mutex_unlock(&dev_priv->rps.hw_lock);
  4626. if (ret) {
  4627. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4628. ret, cdclk);
  4629. return;
  4630. }
  4631. intel_update_cdclk(dev_priv->dev);
  4632. }
  4633. static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
  4634. {
  4635. if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
  4636. return false;
  4637. /* TODO: Check for a valid CDCLK rate */
  4638. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_REQUEST)) {
  4639. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power not requested\n");
  4640. return false;
  4641. }
  4642. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE)) {
  4643. DRM_DEBUG_DRIVER("CDCLK enabled, but DBUF power hasn't settled\n");
  4644. return false;
  4645. }
  4646. return true;
  4647. }
  4648. bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
  4649. {
  4650. return broxton_cdclk_is_enabled(dev_priv);
  4651. }
  4652. void broxton_init_cdclk(struct drm_i915_private *dev_priv)
  4653. {
  4654. /* check if cd clock is enabled */
  4655. if (broxton_cdclk_is_enabled(dev_priv)) {
  4656. DRM_DEBUG_KMS("CDCLK already enabled, won't reprogram it\n");
  4657. return;
  4658. }
  4659. DRM_DEBUG_KMS("CDCLK not enabled, enabling it\n");
  4660. /*
  4661. * FIXME:
  4662. * - The initial CDCLK needs to be read from VBT.
  4663. * Need to make this change after VBT has changes for BXT.
  4664. * - check if setting the max (or any) cdclk freq is really necessary
  4665. * here, it belongs to modeset time
  4666. */
  4667. broxton_set_cdclk(dev_priv, 624000);
  4668. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4669. POSTING_READ(DBUF_CTL);
  4670. udelay(10);
  4671. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4672. DRM_ERROR("DBuf power enable timeout!\n");
  4673. }
  4674. void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
  4675. {
  4676. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4677. POSTING_READ(DBUF_CTL);
  4678. udelay(10);
  4679. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4680. DRM_ERROR("DBuf power disable timeout!\n");
  4681. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4682. broxton_set_cdclk(dev_priv, 19200);
  4683. }
  4684. static const struct skl_cdclk_entry {
  4685. unsigned int freq;
  4686. unsigned int vco;
  4687. } skl_cdclk_frequencies[] = {
  4688. { .freq = 308570, .vco = 8640 },
  4689. { .freq = 337500, .vco = 8100 },
  4690. { .freq = 432000, .vco = 8640 },
  4691. { .freq = 450000, .vco = 8100 },
  4692. { .freq = 540000, .vco = 8100 },
  4693. { .freq = 617140, .vco = 8640 },
  4694. { .freq = 675000, .vco = 8100 },
  4695. };
  4696. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4697. {
  4698. unsigned int i;
  4699. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4700. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4701. if (e->freq == freq)
  4702. return e->vco;
  4703. }
  4704. return 8100;
  4705. }
  4706. static void
  4707. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4708. {
  4709. int min_cdclk;
  4710. u32 val;
  4711. /* select the minimum CDCLK before enabling DPLL 0 */
  4712. if (required_vco == 8640)
  4713. min_cdclk = 308570;
  4714. else
  4715. min_cdclk = 337500;
  4716. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4717. I915_WRITE(CDCLK_CTL, val);
  4718. POSTING_READ(CDCLK_CTL);
  4719. /*
  4720. * We always enable DPLL0 with the lowest link rate possible, but still
  4721. * taking into account the VCO required to operate the eDP panel at the
  4722. * desired frequency. The usual DP link rates operate with a VCO of
  4723. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4724. * The modeset code is responsible for the selection of the exact link
  4725. * rate later on, with the constraint of choosing a frequency that
  4726. * works with required_vco.
  4727. */
  4728. val = I915_READ(DPLL_CTRL1);
  4729. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4730. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4731. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4732. if (required_vco == 8640)
  4733. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4734. SKL_DPLL0);
  4735. else
  4736. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4737. SKL_DPLL0);
  4738. I915_WRITE(DPLL_CTRL1, val);
  4739. POSTING_READ(DPLL_CTRL1);
  4740. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4741. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4742. DRM_ERROR("DPLL0 not locked\n");
  4743. }
  4744. static void
  4745. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4746. {
  4747. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4748. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4749. DRM_ERROR("Couldn't disable DPLL0\n");
  4750. }
  4751. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4752. {
  4753. int ret;
  4754. u32 val;
  4755. /* inform PCU we want to change CDCLK */
  4756. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4757. mutex_lock(&dev_priv->rps.hw_lock);
  4758. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4759. mutex_unlock(&dev_priv->rps.hw_lock);
  4760. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4761. }
  4762. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4763. {
  4764. unsigned int i;
  4765. for (i = 0; i < 15; i++) {
  4766. if (skl_cdclk_pcu_ready(dev_priv))
  4767. return true;
  4768. udelay(10);
  4769. }
  4770. return false;
  4771. }
  4772. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4773. {
  4774. struct drm_device *dev = dev_priv->dev;
  4775. u32 freq_select, pcu_ack;
  4776. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", cdclk);
  4777. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4778. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4779. return;
  4780. }
  4781. /* set CDCLK_CTL */
  4782. switch (cdclk) {
  4783. case 450000:
  4784. case 432000:
  4785. freq_select = CDCLK_FREQ_450_432;
  4786. pcu_ack = 1;
  4787. break;
  4788. case 540000:
  4789. freq_select = CDCLK_FREQ_540;
  4790. pcu_ack = 2;
  4791. break;
  4792. case 308570:
  4793. case 337500:
  4794. default:
  4795. freq_select = CDCLK_FREQ_337_308;
  4796. pcu_ack = 0;
  4797. break;
  4798. case 617140:
  4799. case 675000:
  4800. freq_select = CDCLK_FREQ_675_617;
  4801. pcu_ack = 3;
  4802. break;
  4803. }
  4804. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4805. POSTING_READ(CDCLK_CTL);
  4806. /* inform PCU of the change */
  4807. mutex_lock(&dev_priv->rps.hw_lock);
  4808. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4809. mutex_unlock(&dev_priv->rps.hw_lock);
  4810. intel_update_cdclk(dev);
  4811. }
  4812. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4813. {
  4814. /* disable DBUF power */
  4815. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4816. POSTING_READ(DBUF_CTL);
  4817. udelay(10);
  4818. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4819. DRM_ERROR("DBuf power disable timeout\n");
  4820. skl_dpll0_disable(dev_priv);
  4821. }
  4822. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4823. {
  4824. unsigned int required_vco;
  4825. /* DPLL0 not enabled (happens on early BIOS versions) */
  4826. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4827. /* enable DPLL0 */
  4828. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4829. skl_dpll0_enable(dev_priv, required_vco);
  4830. }
  4831. /* set CDCLK to the frequency the BIOS chose */
  4832. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4833. /* enable DBUF power */
  4834. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4835. POSTING_READ(DBUF_CTL);
  4836. udelay(10);
  4837. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4838. DRM_ERROR("DBuf power enable timeout\n");
  4839. }
  4840. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4841. {
  4842. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4843. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4844. int freq = dev_priv->skl_boot_cdclk;
  4845. /*
  4846. * check if the pre-os intialized the display
  4847. * There is SWF18 scratchpad register defined which is set by the
  4848. * pre-os which can be used by the OS drivers to check the status
  4849. */
  4850. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4851. goto sanitize;
  4852. /* Is PLL enabled and locked ? */
  4853. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4854. goto sanitize;
  4855. /* DPLL okay; verify the cdclock
  4856. *
  4857. * Noticed in some instances that the freq selection is correct but
  4858. * decimal part is programmed wrong from BIOS where pre-os does not
  4859. * enable display. Verify the same as well.
  4860. */
  4861. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4862. /* All well; nothing to sanitize */
  4863. return false;
  4864. sanitize:
  4865. /*
  4866. * As of now initialize with max cdclk till
  4867. * we get dynamic cdclk support
  4868. * */
  4869. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4870. skl_init_cdclk(dev_priv);
  4871. /* we did have to sanitize */
  4872. return true;
  4873. }
  4874. /* Adjust CDclk dividers to allow high res or save power if possible */
  4875. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4876. {
  4877. struct drm_i915_private *dev_priv = dev->dev_private;
  4878. u32 val, cmd;
  4879. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4880. != dev_priv->cdclk_freq);
  4881. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4882. cmd = 2;
  4883. else if (cdclk == 266667)
  4884. cmd = 1;
  4885. else
  4886. cmd = 0;
  4887. mutex_lock(&dev_priv->rps.hw_lock);
  4888. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4889. val &= ~DSPFREQGUAR_MASK;
  4890. val |= (cmd << DSPFREQGUAR_SHIFT);
  4891. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4892. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4893. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4894. 50)) {
  4895. DRM_ERROR("timed out waiting for CDclk change\n");
  4896. }
  4897. mutex_unlock(&dev_priv->rps.hw_lock);
  4898. mutex_lock(&dev_priv->sb_lock);
  4899. if (cdclk == 400000) {
  4900. u32 divider;
  4901. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4902. /* adjust cdclk divider */
  4903. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4904. val &= ~CCK_FREQUENCY_VALUES;
  4905. val |= divider;
  4906. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4907. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4908. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4909. 50))
  4910. DRM_ERROR("timed out waiting for CDclk change\n");
  4911. }
  4912. /* adjust self-refresh exit latency value */
  4913. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4914. val &= ~0x7f;
  4915. /*
  4916. * For high bandwidth configs, we set a higher latency in the bunit
  4917. * so that the core display fetch happens in time to avoid underruns.
  4918. */
  4919. if (cdclk == 400000)
  4920. val |= 4500 / 250; /* 4.5 usec */
  4921. else
  4922. val |= 3000 / 250; /* 3.0 usec */
  4923. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4924. mutex_unlock(&dev_priv->sb_lock);
  4925. intel_update_cdclk(dev);
  4926. }
  4927. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4928. {
  4929. struct drm_i915_private *dev_priv = dev->dev_private;
  4930. u32 val, cmd;
  4931. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4932. != dev_priv->cdclk_freq);
  4933. switch (cdclk) {
  4934. case 333333:
  4935. case 320000:
  4936. case 266667:
  4937. case 200000:
  4938. break;
  4939. default:
  4940. MISSING_CASE(cdclk);
  4941. return;
  4942. }
  4943. /*
  4944. * Specs are full of misinformation, but testing on actual
  4945. * hardware has shown that we just need to write the desired
  4946. * CCK divider into the Punit register.
  4947. */
  4948. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4949. mutex_lock(&dev_priv->rps.hw_lock);
  4950. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4951. val &= ~DSPFREQGUAR_MASK_CHV;
  4952. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4953. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4954. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4955. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4956. 50)) {
  4957. DRM_ERROR("timed out waiting for CDclk change\n");
  4958. }
  4959. mutex_unlock(&dev_priv->rps.hw_lock);
  4960. intel_update_cdclk(dev);
  4961. }
  4962. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4963. int max_pixclk)
  4964. {
  4965. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4966. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4967. /*
  4968. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4969. * 200MHz
  4970. * 267MHz
  4971. * 320/333MHz (depends on HPLL freq)
  4972. * 400MHz (VLV only)
  4973. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4974. * of the lower bin and adjust if needed.
  4975. *
  4976. * We seem to get an unstable or solid color picture at 200MHz.
  4977. * Not sure what's wrong. For now use 200MHz only when all pipes
  4978. * are off.
  4979. */
  4980. if (!IS_CHERRYVIEW(dev_priv) &&
  4981. max_pixclk > freq_320*limit/100)
  4982. return 400000;
  4983. else if (max_pixclk > 266667*limit/100)
  4984. return freq_320;
  4985. else if (max_pixclk > 0)
  4986. return 266667;
  4987. else
  4988. return 200000;
  4989. }
  4990. static int broxton_calc_cdclk(int max_pixclk)
  4991. {
  4992. /*
  4993. * FIXME:
  4994. * - set 19.2MHz bypass frequency if there are no active pipes
  4995. */
  4996. if (max_pixclk > 576000)
  4997. return 624000;
  4998. else if (max_pixclk > 384000)
  4999. return 576000;
  5000. else if (max_pixclk > 288000)
  5001. return 384000;
  5002. else if (max_pixclk > 144000)
  5003. return 288000;
  5004. else
  5005. return 144000;
  5006. }
  5007. /* Compute the max pixel clock for new configuration. */
  5008. static int intel_mode_max_pixclk(struct drm_device *dev,
  5009. struct drm_atomic_state *state)
  5010. {
  5011. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5012. struct drm_i915_private *dev_priv = dev->dev_private;
  5013. struct drm_crtc *crtc;
  5014. struct drm_crtc_state *crtc_state;
  5015. unsigned max_pixclk = 0, i;
  5016. enum pipe pipe;
  5017. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5018. sizeof(intel_state->min_pixclk));
  5019. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5020. int pixclk = 0;
  5021. if (crtc_state->enable)
  5022. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5023. intel_state->min_pixclk[i] = pixclk;
  5024. }
  5025. for_each_pipe(dev_priv, pipe)
  5026. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5027. return max_pixclk;
  5028. }
  5029. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5030. {
  5031. struct drm_device *dev = state->dev;
  5032. struct drm_i915_private *dev_priv = dev->dev_private;
  5033. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5034. struct intel_atomic_state *intel_state =
  5035. to_intel_atomic_state(state);
  5036. intel_state->cdclk = intel_state->dev_cdclk =
  5037. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5038. if (!intel_state->active_crtcs)
  5039. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5040. return 0;
  5041. }
  5042. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5043. {
  5044. int max_pixclk = ilk_max_pixel_rate(state);
  5045. struct intel_atomic_state *intel_state =
  5046. to_intel_atomic_state(state);
  5047. intel_state->cdclk = intel_state->dev_cdclk =
  5048. broxton_calc_cdclk(max_pixclk);
  5049. if (!intel_state->active_crtcs)
  5050. intel_state->dev_cdclk = broxton_calc_cdclk(0);
  5051. return 0;
  5052. }
  5053. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5054. {
  5055. unsigned int credits, default_credits;
  5056. if (IS_CHERRYVIEW(dev_priv))
  5057. default_credits = PFI_CREDIT(12);
  5058. else
  5059. default_credits = PFI_CREDIT(8);
  5060. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5061. /* CHV suggested value is 31 or 63 */
  5062. if (IS_CHERRYVIEW(dev_priv))
  5063. credits = PFI_CREDIT_63;
  5064. else
  5065. credits = PFI_CREDIT(15);
  5066. } else {
  5067. credits = default_credits;
  5068. }
  5069. /*
  5070. * WA - write default credits before re-programming
  5071. * FIXME: should we also set the resend bit here?
  5072. */
  5073. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5074. default_credits);
  5075. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5076. credits | PFI_CREDIT_RESEND);
  5077. /*
  5078. * FIXME is this guaranteed to clear
  5079. * immediately or should we poll for it?
  5080. */
  5081. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5082. }
  5083. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5084. {
  5085. struct drm_device *dev = old_state->dev;
  5086. struct drm_i915_private *dev_priv = dev->dev_private;
  5087. struct intel_atomic_state *old_intel_state =
  5088. to_intel_atomic_state(old_state);
  5089. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5090. /*
  5091. * FIXME: We can end up here with all power domains off, yet
  5092. * with a CDCLK frequency other than the minimum. To account
  5093. * for this take the PIPE-A power domain, which covers the HW
  5094. * blocks needed for the following programming. This can be
  5095. * removed once it's guaranteed that we get here either with
  5096. * the minimum CDCLK set, or the required power domains
  5097. * enabled.
  5098. */
  5099. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5100. if (IS_CHERRYVIEW(dev))
  5101. cherryview_set_cdclk(dev, req_cdclk);
  5102. else
  5103. valleyview_set_cdclk(dev, req_cdclk);
  5104. vlv_program_pfi_credits(dev_priv);
  5105. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5106. }
  5107. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5108. {
  5109. struct drm_device *dev = crtc->dev;
  5110. struct drm_i915_private *dev_priv = to_i915(dev);
  5111. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5112. struct intel_encoder *encoder;
  5113. struct intel_crtc_state *pipe_config =
  5114. to_intel_crtc_state(crtc->state);
  5115. int pipe = intel_crtc->pipe;
  5116. if (WARN_ON(intel_crtc->active))
  5117. return;
  5118. if (intel_crtc->config->has_dp_encoder)
  5119. intel_dp_set_m_n(intel_crtc, M1_N1);
  5120. intel_set_pipe_timings(intel_crtc);
  5121. intel_set_pipe_src_size(intel_crtc);
  5122. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5123. struct drm_i915_private *dev_priv = dev->dev_private;
  5124. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5125. I915_WRITE(CHV_CANVAS(pipe), 0);
  5126. }
  5127. i9xx_set_pipeconf(intel_crtc);
  5128. intel_crtc->active = true;
  5129. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5130. for_each_encoder_on_crtc(dev, crtc, encoder)
  5131. if (encoder->pre_pll_enable)
  5132. encoder->pre_pll_enable(encoder);
  5133. if (IS_CHERRYVIEW(dev)) {
  5134. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5135. chv_enable_pll(intel_crtc, intel_crtc->config);
  5136. } else {
  5137. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5138. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5139. }
  5140. for_each_encoder_on_crtc(dev, crtc, encoder)
  5141. if (encoder->pre_enable)
  5142. encoder->pre_enable(encoder);
  5143. i9xx_pfit_enable(intel_crtc);
  5144. intel_color_load_luts(&pipe_config->base);
  5145. intel_update_watermarks(crtc);
  5146. intel_enable_pipe(intel_crtc);
  5147. assert_vblank_disabled(crtc);
  5148. drm_crtc_vblank_on(crtc);
  5149. for_each_encoder_on_crtc(dev, crtc, encoder)
  5150. encoder->enable(encoder);
  5151. }
  5152. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5153. {
  5154. struct drm_device *dev = crtc->base.dev;
  5155. struct drm_i915_private *dev_priv = dev->dev_private;
  5156. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5157. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5158. }
  5159. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5160. {
  5161. struct drm_device *dev = crtc->dev;
  5162. struct drm_i915_private *dev_priv = to_i915(dev);
  5163. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5164. struct intel_encoder *encoder;
  5165. struct intel_crtc_state *pipe_config =
  5166. to_intel_crtc_state(crtc->state);
  5167. enum pipe pipe = intel_crtc->pipe;
  5168. if (WARN_ON(intel_crtc->active))
  5169. return;
  5170. i9xx_set_pll_dividers(intel_crtc);
  5171. if (intel_crtc->config->has_dp_encoder)
  5172. intel_dp_set_m_n(intel_crtc, M1_N1);
  5173. intel_set_pipe_timings(intel_crtc);
  5174. intel_set_pipe_src_size(intel_crtc);
  5175. i9xx_set_pipeconf(intel_crtc);
  5176. intel_crtc->active = true;
  5177. if (!IS_GEN2(dev))
  5178. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5179. for_each_encoder_on_crtc(dev, crtc, encoder)
  5180. if (encoder->pre_enable)
  5181. encoder->pre_enable(encoder);
  5182. i9xx_enable_pll(intel_crtc);
  5183. i9xx_pfit_enable(intel_crtc);
  5184. intel_color_load_luts(&pipe_config->base);
  5185. intel_update_watermarks(crtc);
  5186. intel_enable_pipe(intel_crtc);
  5187. assert_vblank_disabled(crtc);
  5188. drm_crtc_vblank_on(crtc);
  5189. for_each_encoder_on_crtc(dev, crtc, encoder)
  5190. encoder->enable(encoder);
  5191. }
  5192. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5193. {
  5194. struct drm_device *dev = crtc->base.dev;
  5195. struct drm_i915_private *dev_priv = dev->dev_private;
  5196. if (!crtc->config->gmch_pfit.control)
  5197. return;
  5198. assert_pipe_disabled(dev_priv, crtc->pipe);
  5199. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5200. I915_READ(PFIT_CONTROL));
  5201. I915_WRITE(PFIT_CONTROL, 0);
  5202. }
  5203. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5204. {
  5205. struct drm_device *dev = crtc->dev;
  5206. struct drm_i915_private *dev_priv = dev->dev_private;
  5207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5208. struct intel_encoder *encoder;
  5209. int pipe = intel_crtc->pipe;
  5210. /*
  5211. * On gen2 planes are double buffered but the pipe isn't, so we must
  5212. * wait for planes to fully turn off before disabling the pipe.
  5213. */
  5214. if (IS_GEN2(dev))
  5215. intel_wait_for_vblank(dev, pipe);
  5216. for_each_encoder_on_crtc(dev, crtc, encoder)
  5217. encoder->disable(encoder);
  5218. drm_crtc_vblank_off(crtc);
  5219. assert_vblank_disabled(crtc);
  5220. intel_disable_pipe(intel_crtc);
  5221. i9xx_pfit_disable(intel_crtc);
  5222. for_each_encoder_on_crtc(dev, crtc, encoder)
  5223. if (encoder->post_disable)
  5224. encoder->post_disable(encoder);
  5225. if (!intel_crtc->config->has_dsi_encoder) {
  5226. if (IS_CHERRYVIEW(dev))
  5227. chv_disable_pll(dev_priv, pipe);
  5228. else if (IS_VALLEYVIEW(dev))
  5229. vlv_disable_pll(dev_priv, pipe);
  5230. else
  5231. i9xx_disable_pll(intel_crtc);
  5232. }
  5233. for_each_encoder_on_crtc(dev, crtc, encoder)
  5234. if (encoder->post_pll_disable)
  5235. encoder->post_pll_disable(encoder);
  5236. if (!IS_GEN2(dev))
  5237. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5238. }
  5239. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5240. {
  5241. struct intel_encoder *encoder;
  5242. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5243. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5244. enum intel_display_power_domain domain;
  5245. unsigned long domains;
  5246. if (!intel_crtc->active)
  5247. return;
  5248. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5249. WARN_ON(intel_crtc->unpin_work);
  5250. intel_pre_disable_primary_noatomic(crtc);
  5251. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5252. to_intel_plane_state(crtc->primary->state)->visible = false;
  5253. }
  5254. dev_priv->display.crtc_disable(crtc);
  5255. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
  5256. crtc->base.id);
  5257. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5258. crtc->state->active = false;
  5259. intel_crtc->active = false;
  5260. crtc->enabled = false;
  5261. crtc->state->connector_mask = 0;
  5262. crtc->state->encoder_mask = 0;
  5263. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5264. encoder->base.crtc = NULL;
  5265. intel_fbc_disable(intel_crtc);
  5266. intel_update_watermarks(crtc);
  5267. intel_disable_shared_dpll(intel_crtc);
  5268. domains = intel_crtc->enabled_power_domains;
  5269. for_each_power_domain(domain, domains)
  5270. intel_display_power_put(dev_priv, domain);
  5271. intel_crtc->enabled_power_domains = 0;
  5272. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5273. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5274. }
  5275. /*
  5276. * turn all crtc's off, but do not adjust state
  5277. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5278. */
  5279. int intel_display_suspend(struct drm_device *dev)
  5280. {
  5281. struct drm_i915_private *dev_priv = to_i915(dev);
  5282. struct drm_atomic_state *state;
  5283. int ret;
  5284. state = drm_atomic_helper_suspend(dev);
  5285. ret = PTR_ERR_OR_ZERO(state);
  5286. if (ret)
  5287. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5288. else
  5289. dev_priv->modeset_restore_state = state;
  5290. return ret;
  5291. }
  5292. void intel_encoder_destroy(struct drm_encoder *encoder)
  5293. {
  5294. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5295. drm_encoder_cleanup(encoder);
  5296. kfree(intel_encoder);
  5297. }
  5298. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5299. * internal consistency). */
  5300. static void intel_connector_verify_state(struct intel_connector *connector)
  5301. {
  5302. struct drm_crtc *crtc = connector->base.state->crtc;
  5303. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5304. connector->base.base.id,
  5305. connector->base.name);
  5306. if (connector->get_hw_state(connector)) {
  5307. struct intel_encoder *encoder = connector->encoder;
  5308. struct drm_connector_state *conn_state = connector->base.state;
  5309. I915_STATE_WARN(!crtc,
  5310. "connector enabled without attached crtc\n");
  5311. if (!crtc)
  5312. return;
  5313. I915_STATE_WARN(!crtc->state->active,
  5314. "connector is active, but attached crtc isn't\n");
  5315. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5316. return;
  5317. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5318. "atomic encoder doesn't match attached encoder\n");
  5319. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5320. "attached encoder crtc differs from connector crtc\n");
  5321. } else {
  5322. I915_STATE_WARN(crtc && crtc->state->active,
  5323. "attached crtc is active, but connector isn't\n");
  5324. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5325. "best encoder set without crtc!\n");
  5326. }
  5327. }
  5328. int intel_connector_init(struct intel_connector *connector)
  5329. {
  5330. drm_atomic_helper_connector_reset(&connector->base);
  5331. if (!connector->base.state)
  5332. return -ENOMEM;
  5333. return 0;
  5334. }
  5335. struct intel_connector *intel_connector_alloc(void)
  5336. {
  5337. struct intel_connector *connector;
  5338. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5339. if (!connector)
  5340. return NULL;
  5341. if (intel_connector_init(connector) < 0) {
  5342. kfree(connector);
  5343. return NULL;
  5344. }
  5345. return connector;
  5346. }
  5347. /* Simple connector->get_hw_state implementation for encoders that support only
  5348. * one connector and no cloning and hence the encoder state determines the state
  5349. * of the connector. */
  5350. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5351. {
  5352. enum pipe pipe = 0;
  5353. struct intel_encoder *encoder = connector->encoder;
  5354. return encoder->get_hw_state(encoder, &pipe);
  5355. }
  5356. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5357. {
  5358. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5359. return crtc_state->fdi_lanes;
  5360. return 0;
  5361. }
  5362. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5363. struct intel_crtc_state *pipe_config)
  5364. {
  5365. struct drm_atomic_state *state = pipe_config->base.state;
  5366. struct intel_crtc *other_crtc;
  5367. struct intel_crtc_state *other_crtc_state;
  5368. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5369. pipe_name(pipe), pipe_config->fdi_lanes);
  5370. if (pipe_config->fdi_lanes > 4) {
  5371. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5372. pipe_name(pipe), pipe_config->fdi_lanes);
  5373. return -EINVAL;
  5374. }
  5375. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5376. if (pipe_config->fdi_lanes > 2) {
  5377. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5378. pipe_config->fdi_lanes);
  5379. return -EINVAL;
  5380. } else {
  5381. return 0;
  5382. }
  5383. }
  5384. if (INTEL_INFO(dev)->num_pipes == 2)
  5385. return 0;
  5386. /* Ivybridge 3 pipe is really complicated */
  5387. switch (pipe) {
  5388. case PIPE_A:
  5389. return 0;
  5390. case PIPE_B:
  5391. if (pipe_config->fdi_lanes <= 2)
  5392. return 0;
  5393. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5394. other_crtc_state =
  5395. intel_atomic_get_crtc_state(state, other_crtc);
  5396. if (IS_ERR(other_crtc_state))
  5397. return PTR_ERR(other_crtc_state);
  5398. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5399. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5400. pipe_name(pipe), pipe_config->fdi_lanes);
  5401. return -EINVAL;
  5402. }
  5403. return 0;
  5404. case PIPE_C:
  5405. if (pipe_config->fdi_lanes > 2) {
  5406. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5407. pipe_name(pipe), pipe_config->fdi_lanes);
  5408. return -EINVAL;
  5409. }
  5410. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5411. other_crtc_state =
  5412. intel_atomic_get_crtc_state(state, other_crtc);
  5413. if (IS_ERR(other_crtc_state))
  5414. return PTR_ERR(other_crtc_state);
  5415. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5416. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5417. return -EINVAL;
  5418. }
  5419. return 0;
  5420. default:
  5421. BUG();
  5422. }
  5423. }
  5424. #define RETRY 1
  5425. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5426. struct intel_crtc_state *pipe_config)
  5427. {
  5428. struct drm_device *dev = intel_crtc->base.dev;
  5429. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5430. int lane, link_bw, fdi_dotclock, ret;
  5431. bool needs_recompute = false;
  5432. retry:
  5433. /* FDI is a binary signal running at ~2.7GHz, encoding
  5434. * each output octet as 10 bits. The actual frequency
  5435. * is stored as a divider into a 100MHz clock, and the
  5436. * mode pixel clock is stored in units of 1KHz.
  5437. * Hence the bw of each lane in terms of the mode signal
  5438. * is:
  5439. */
  5440. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5441. fdi_dotclock = adjusted_mode->crtc_clock;
  5442. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5443. pipe_config->pipe_bpp);
  5444. pipe_config->fdi_lanes = lane;
  5445. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5446. link_bw, &pipe_config->fdi_m_n);
  5447. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5448. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5449. pipe_config->pipe_bpp -= 2*3;
  5450. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5451. pipe_config->pipe_bpp);
  5452. needs_recompute = true;
  5453. pipe_config->bw_constrained = true;
  5454. goto retry;
  5455. }
  5456. if (needs_recompute)
  5457. return RETRY;
  5458. return ret;
  5459. }
  5460. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5461. struct intel_crtc_state *pipe_config)
  5462. {
  5463. if (pipe_config->pipe_bpp > 24)
  5464. return false;
  5465. /* HSW can handle pixel rate up to cdclk? */
  5466. if (IS_HASWELL(dev_priv))
  5467. return true;
  5468. /*
  5469. * We compare against max which means we must take
  5470. * the increased cdclk requirement into account when
  5471. * calculating the new cdclk.
  5472. *
  5473. * Should measure whether using a lower cdclk w/o IPS
  5474. */
  5475. return ilk_pipe_pixel_rate(pipe_config) <=
  5476. dev_priv->max_cdclk_freq * 95 / 100;
  5477. }
  5478. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5479. struct intel_crtc_state *pipe_config)
  5480. {
  5481. struct drm_device *dev = crtc->base.dev;
  5482. struct drm_i915_private *dev_priv = dev->dev_private;
  5483. pipe_config->ips_enabled = i915.enable_ips &&
  5484. hsw_crtc_supports_ips(crtc) &&
  5485. pipe_config_supports_ips(dev_priv, pipe_config);
  5486. }
  5487. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5488. {
  5489. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5490. /* GDG double wide on either pipe, otherwise pipe A only */
  5491. return INTEL_INFO(dev_priv)->gen < 4 &&
  5492. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5493. }
  5494. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5495. struct intel_crtc_state *pipe_config)
  5496. {
  5497. struct drm_device *dev = crtc->base.dev;
  5498. struct drm_i915_private *dev_priv = dev->dev_private;
  5499. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5500. /* FIXME should check pixel clock limits on all platforms */
  5501. if (INTEL_INFO(dev)->gen < 4) {
  5502. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5503. /*
  5504. * Enable double wide mode when the dot clock
  5505. * is > 90% of the (display) core speed.
  5506. */
  5507. if (intel_crtc_supports_double_wide(crtc) &&
  5508. adjusted_mode->crtc_clock > clock_limit) {
  5509. clock_limit *= 2;
  5510. pipe_config->double_wide = true;
  5511. }
  5512. if (adjusted_mode->crtc_clock > clock_limit) {
  5513. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5514. adjusted_mode->crtc_clock, clock_limit,
  5515. yesno(pipe_config->double_wide));
  5516. return -EINVAL;
  5517. }
  5518. }
  5519. /*
  5520. * Pipe horizontal size must be even in:
  5521. * - DVO ganged mode
  5522. * - LVDS dual channel mode
  5523. * - Double wide pipe
  5524. */
  5525. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5526. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5527. pipe_config->pipe_src_w &= ~1;
  5528. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5529. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5530. */
  5531. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5532. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5533. return -EINVAL;
  5534. if (HAS_IPS(dev))
  5535. hsw_compute_ips_config(crtc, pipe_config);
  5536. if (pipe_config->has_pch_encoder)
  5537. return ironlake_fdi_compute_config(crtc, pipe_config);
  5538. return 0;
  5539. }
  5540. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5541. {
  5542. struct drm_i915_private *dev_priv = to_i915(dev);
  5543. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5544. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5545. uint32_t linkrate;
  5546. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5547. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5548. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5549. return 540000;
  5550. linkrate = (I915_READ(DPLL_CTRL1) &
  5551. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5552. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5553. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5554. /* vco 8640 */
  5555. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5556. case CDCLK_FREQ_450_432:
  5557. return 432000;
  5558. case CDCLK_FREQ_337_308:
  5559. return 308570;
  5560. case CDCLK_FREQ_675_617:
  5561. return 617140;
  5562. default:
  5563. WARN(1, "Unknown cd freq selection\n");
  5564. }
  5565. } else {
  5566. /* vco 8100 */
  5567. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5568. case CDCLK_FREQ_450_432:
  5569. return 450000;
  5570. case CDCLK_FREQ_337_308:
  5571. return 337500;
  5572. case CDCLK_FREQ_675_617:
  5573. return 675000;
  5574. default:
  5575. WARN(1, "Unknown cd freq selection\n");
  5576. }
  5577. }
  5578. /* error case, do as if DPLL0 isn't enabled */
  5579. return 24000;
  5580. }
  5581. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5582. {
  5583. struct drm_i915_private *dev_priv = to_i915(dev);
  5584. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5585. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5586. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5587. int cdclk;
  5588. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5589. return 19200;
  5590. cdclk = 19200 * pll_ratio / 2;
  5591. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5592. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5593. return cdclk; /* 576MHz or 624MHz */
  5594. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5595. return cdclk * 2 / 3; /* 384MHz */
  5596. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5597. return cdclk / 2; /* 288MHz */
  5598. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5599. return cdclk / 4; /* 144MHz */
  5600. }
  5601. /* error case, do as if DE PLL isn't enabled */
  5602. return 19200;
  5603. }
  5604. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5605. {
  5606. struct drm_i915_private *dev_priv = dev->dev_private;
  5607. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5608. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5609. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5610. return 800000;
  5611. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5612. return 450000;
  5613. else if (freq == LCPLL_CLK_FREQ_450)
  5614. return 450000;
  5615. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5616. return 540000;
  5617. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5618. return 337500;
  5619. else
  5620. return 675000;
  5621. }
  5622. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5623. {
  5624. struct drm_i915_private *dev_priv = dev->dev_private;
  5625. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5626. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5627. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5628. return 800000;
  5629. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5630. return 450000;
  5631. else if (freq == LCPLL_CLK_FREQ_450)
  5632. return 450000;
  5633. else if (IS_HSW_ULT(dev))
  5634. return 337500;
  5635. else
  5636. return 540000;
  5637. }
  5638. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5639. {
  5640. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5641. CCK_DISPLAY_CLOCK_CONTROL);
  5642. }
  5643. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5644. {
  5645. return 450000;
  5646. }
  5647. static int i945_get_display_clock_speed(struct drm_device *dev)
  5648. {
  5649. return 400000;
  5650. }
  5651. static int i915_get_display_clock_speed(struct drm_device *dev)
  5652. {
  5653. return 333333;
  5654. }
  5655. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5656. {
  5657. return 200000;
  5658. }
  5659. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5660. {
  5661. u16 gcfgc = 0;
  5662. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5663. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5664. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5665. return 266667;
  5666. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5667. return 333333;
  5668. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5669. return 444444;
  5670. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5671. return 200000;
  5672. default:
  5673. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5674. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5675. return 133333;
  5676. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5677. return 166667;
  5678. }
  5679. }
  5680. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5681. {
  5682. u16 gcfgc = 0;
  5683. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5684. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5685. return 133333;
  5686. else {
  5687. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5688. case GC_DISPLAY_CLOCK_333_MHZ:
  5689. return 333333;
  5690. default:
  5691. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5692. return 190000;
  5693. }
  5694. }
  5695. }
  5696. static int i865_get_display_clock_speed(struct drm_device *dev)
  5697. {
  5698. return 266667;
  5699. }
  5700. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5701. {
  5702. u16 hpllcc = 0;
  5703. /*
  5704. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5705. * encoding is different :(
  5706. * FIXME is this the right way to detect 852GM/852GMV?
  5707. */
  5708. if (dev->pdev->revision == 0x1)
  5709. return 133333;
  5710. pci_bus_read_config_word(dev->pdev->bus,
  5711. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5712. /* Assume that the hardware is in the high speed state. This
  5713. * should be the default.
  5714. */
  5715. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5716. case GC_CLOCK_133_200:
  5717. case GC_CLOCK_133_200_2:
  5718. case GC_CLOCK_100_200:
  5719. return 200000;
  5720. case GC_CLOCK_166_250:
  5721. return 250000;
  5722. case GC_CLOCK_100_133:
  5723. return 133333;
  5724. case GC_CLOCK_133_266:
  5725. case GC_CLOCK_133_266_2:
  5726. case GC_CLOCK_166_266:
  5727. return 266667;
  5728. }
  5729. /* Shouldn't happen */
  5730. return 0;
  5731. }
  5732. static int i830_get_display_clock_speed(struct drm_device *dev)
  5733. {
  5734. return 133333;
  5735. }
  5736. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5737. {
  5738. struct drm_i915_private *dev_priv = dev->dev_private;
  5739. static const unsigned int blb_vco[8] = {
  5740. [0] = 3200000,
  5741. [1] = 4000000,
  5742. [2] = 5333333,
  5743. [3] = 4800000,
  5744. [4] = 6400000,
  5745. };
  5746. static const unsigned int pnv_vco[8] = {
  5747. [0] = 3200000,
  5748. [1] = 4000000,
  5749. [2] = 5333333,
  5750. [3] = 4800000,
  5751. [4] = 2666667,
  5752. };
  5753. static const unsigned int cl_vco[8] = {
  5754. [0] = 3200000,
  5755. [1] = 4000000,
  5756. [2] = 5333333,
  5757. [3] = 6400000,
  5758. [4] = 3333333,
  5759. [5] = 3566667,
  5760. [6] = 4266667,
  5761. };
  5762. static const unsigned int elk_vco[8] = {
  5763. [0] = 3200000,
  5764. [1] = 4000000,
  5765. [2] = 5333333,
  5766. [3] = 4800000,
  5767. };
  5768. static const unsigned int ctg_vco[8] = {
  5769. [0] = 3200000,
  5770. [1] = 4000000,
  5771. [2] = 5333333,
  5772. [3] = 6400000,
  5773. [4] = 2666667,
  5774. [5] = 4266667,
  5775. };
  5776. const unsigned int *vco_table;
  5777. unsigned int vco;
  5778. uint8_t tmp = 0;
  5779. /* FIXME other chipsets? */
  5780. if (IS_GM45(dev))
  5781. vco_table = ctg_vco;
  5782. else if (IS_G4X(dev))
  5783. vco_table = elk_vco;
  5784. else if (IS_CRESTLINE(dev))
  5785. vco_table = cl_vco;
  5786. else if (IS_PINEVIEW(dev))
  5787. vco_table = pnv_vco;
  5788. else if (IS_G33(dev))
  5789. vco_table = blb_vco;
  5790. else
  5791. return 0;
  5792. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5793. vco = vco_table[tmp & 0x7];
  5794. if (vco == 0)
  5795. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5796. else
  5797. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5798. return vco;
  5799. }
  5800. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5801. {
  5802. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5803. uint16_t tmp = 0;
  5804. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5805. cdclk_sel = (tmp >> 12) & 0x1;
  5806. switch (vco) {
  5807. case 2666667:
  5808. case 4000000:
  5809. case 5333333:
  5810. return cdclk_sel ? 333333 : 222222;
  5811. case 3200000:
  5812. return cdclk_sel ? 320000 : 228571;
  5813. default:
  5814. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5815. return 222222;
  5816. }
  5817. }
  5818. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5819. {
  5820. static const uint8_t div_3200[] = { 16, 10, 8 };
  5821. static const uint8_t div_4000[] = { 20, 12, 10 };
  5822. static const uint8_t div_5333[] = { 24, 16, 14 };
  5823. const uint8_t *div_table;
  5824. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5825. uint16_t tmp = 0;
  5826. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5827. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5828. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5829. goto fail;
  5830. switch (vco) {
  5831. case 3200000:
  5832. div_table = div_3200;
  5833. break;
  5834. case 4000000:
  5835. div_table = div_4000;
  5836. break;
  5837. case 5333333:
  5838. div_table = div_5333;
  5839. break;
  5840. default:
  5841. goto fail;
  5842. }
  5843. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5844. fail:
  5845. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5846. return 200000;
  5847. }
  5848. static int g33_get_display_clock_speed(struct drm_device *dev)
  5849. {
  5850. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5851. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5852. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5853. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5854. const uint8_t *div_table;
  5855. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5856. uint16_t tmp = 0;
  5857. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5858. cdclk_sel = (tmp >> 4) & 0x7;
  5859. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5860. goto fail;
  5861. switch (vco) {
  5862. case 3200000:
  5863. div_table = div_3200;
  5864. break;
  5865. case 4000000:
  5866. div_table = div_4000;
  5867. break;
  5868. case 4800000:
  5869. div_table = div_4800;
  5870. break;
  5871. case 5333333:
  5872. div_table = div_5333;
  5873. break;
  5874. default:
  5875. goto fail;
  5876. }
  5877. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5878. fail:
  5879. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5880. return 190476;
  5881. }
  5882. static void
  5883. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5884. {
  5885. while (*num > DATA_LINK_M_N_MASK ||
  5886. *den > DATA_LINK_M_N_MASK) {
  5887. *num >>= 1;
  5888. *den >>= 1;
  5889. }
  5890. }
  5891. static void compute_m_n(unsigned int m, unsigned int n,
  5892. uint32_t *ret_m, uint32_t *ret_n)
  5893. {
  5894. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5895. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5896. intel_reduce_m_n_ratio(ret_m, ret_n);
  5897. }
  5898. void
  5899. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5900. int pixel_clock, int link_clock,
  5901. struct intel_link_m_n *m_n)
  5902. {
  5903. m_n->tu = 64;
  5904. compute_m_n(bits_per_pixel * pixel_clock,
  5905. link_clock * nlanes * 8,
  5906. &m_n->gmch_m, &m_n->gmch_n);
  5907. compute_m_n(pixel_clock, link_clock,
  5908. &m_n->link_m, &m_n->link_n);
  5909. }
  5910. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5911. {
  5912. if (i915.panel_use_ssc >= 0)
  5913. return i915.panel_use_ssc != 0;
  5914. return dev_priv->vbt.lvds_use_ssc
  5915. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5916. }
  5917. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5918. {
  5919. return (1 << dpll->n) << 16 | dpll->m2;
  5920. }
  5921. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5922. {
  5923. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5924. }
  5925. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5926. struct intel_crtc_state *crtc_state,
  5927. struct dpll *reduced_clock)
  5928. {
  5929. struct drm_device *dev = crtc->base.dev;
  5930. u32 fp, fp2 = 0;
  5931. if (IS_PINEVIEW(dev)) {
  5932. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5933. if (reduced_clock)
  5934. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5935. } else {
  5936. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5937. if (reduced_clock)
  5938. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5939. }
  5940. crtc_state->dpll_hw_state.fp0 = fp;
  5941. crtc->lowfreq_avail = false;
  5942. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5943. reduced_clock) {
  5944. crtc_state->dpll_hw_state.fp1 = fp2;
  5945. crtc->lowfreq_avail = true;
  5946. } else {
  5947. crtc_state->dpll_hw_state.fp1 = fp;
  5948. }
  5949. }
  5950. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5951. pipe)
  5952. {
  5953. u32 reg_val;
  5954. /*
  5955. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5956. * and set it to a reasonable value instead.
  5957. */
  5958. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5959. reg_val &= 0xffffff00;
  5960. reg_val |= 0x00000030;
  5961. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5962. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5963. reg_val &= 0x8cffffff;
  5964. reg_val = 0x8c000000;
  5965. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5966. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5967. reg_val &= 0xffffff00;
  5968. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5969. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5970. reg_val &= 0x00ffffff;
  5971. reg_val |= 0xb0000000;
  5972. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5973. }
  5974. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5975. struct intel_link_m_n *m_n)
  5976. {
  5977. struct drm_device *dev = crtc->base.dev;
  5978. struct drm_i915_private *dev_priv = dev->dev_private;
  5979. int pipe = crtc->pipe;
  5980. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5981. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5982. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5983. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5984. }
  5985. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5986. struct intel_link_m_n *m_n,
  5987. struct intel_link_m_n *m2_n2)
  5988. {
  5989. struct drm_device *dev = crtc->base.dev;
  5990. struct drm_i915_private *dev_priv = dev->dev_private;
  5991. int pipe = crtc->pipe;
  5992. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5993. if (INTEL_INFO(dev)->gen >= 5) {
  5994. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5995. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5996. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5997. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5998. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5999. * for gen < 8) and if DRRS is supported (to make sure the
  6000. * registers are not unnecessarily accessed).
  6001. */
  6002. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6003. crtc->config->has_drrs) {
  6004. I915_WRITE(PIPE_DATA_M2(transcoder),
  6005. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6006. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6007. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6008. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6009. }
  6010. } else {
  6011. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6012. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6013. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6014. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6015. }
  6016. }
  6017. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6018. {
  6019. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6020. if (m_n == M1_N1) {
  6021. dp_m_n = &crtc->config->dp_m_n;
  6022. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6023. } else if (m_n == M2_N2) {
  6024. /*
  6025. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6026. * needs to be programmed into M1_N1.
  6027. */
  6028. dp_m_n = &crtc->config->dp_m2_n2;
  6029. } else {
  6030. DRM_ERROR("Unsupported divider value\n");
  6031. return;
  6032. }
  6033. if (crtc->config->has_pch_encoder)
  6034. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6035. else
  6036. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6037. }
  6038. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6039. struct intel_crtc_state *pipe_config)
  6040. {
  6041. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6042. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6043. if (crtc->pipe != PIPE_A)
  6044. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6045. /* DPLL not used with DSI, but still need the rest set up */
  6046. if (!pipe_config->has_dsi_encoder)
  6047. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6048. DPLL_EXT_BUFFER_ENABLE_VLV;
  6049. pipe_config->dpll_hw_state.dpll_md =
  6050. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6051. }
  6052. static void chv_compute_dpll(struct intel_crtc *crtc,
  6053. struct intel_crtc_state *pipe_config)
  6054. {
  6055. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  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. pipe_config->dpll_hw_state.dpll_md =
  6063. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6064. }
  6065. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6066. const struct intel_crtc_state *pipe_config)
  6067. {
  6068. struct drm_device *dev = crtc->base.dev;
  6069. struct drm_i915_private *dev_priv = dev->dev_private;
  6070. enum pipe pipe = crtc->pipe;
  6071. u32 mdiv;
  6072. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6073. u32 coreclk, reg_val;
  6074. /* Enable Refclk */
  6075. I915_WRITE(DPLL(pipe),
  6076. pipe_config->dpll_hw_state.dpll &
  6077. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6078. /* No need to actually set up the DPLL with DSI */
  6079. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6080. return;
  6081. mutex_lock(&dev_priv->sb_lock);
  6082. bestn = pipe_config->dpll.n;
  6083. bestm1 = pipe_config->dpll.m1;
  6084. bestm2 = pipe_config->dpll.m2;
  6085. bestp1 = pipe_config->dpll.p1;
  6086. bestp2 = pipe_config->dpll.p2;
  6087. /* See eDP HDMI DPIO driver vbios notes doc */
  6088. /* PLL B needs special handling */
  6089. if (pipe == PIPE_B)
  6090. vlv_pllb_recal_opamp(dev_priv, pipe);
  6091. /* Set up Tx target for periodic Rcomp update */
  6092. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6093. /* Disable target IRef on PLL */
  6094. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6095. reg_val &= 0x00ffffff;
  6096. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6097. /* Disable fast lock */
  6098. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6099. /* Set idtafcrecal before PLL is enabled */
  6100. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6101. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6102. mdiv |= ((bestn << DPIO_N_SHIFT));
  6103. mdiv |= (1 << DPIO_K_SHIFT);
  6104. /*
  6105. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6106. * but we don't support that).
  6107. * Note: don't use the DAC post divider as it seems unstable.
  6108. */
  6109. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6110. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6111. mdiv |= DPIO_ENABLE_CALIBRATION;
  6112. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6113. /* Set HBR and RBR LPF coefficients */
  6114. if (pipe_config->port_clock == 162000 ||
  6115. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6116. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6117. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6118. 0x009f0003);
  6119. else
  6120. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6121. 0x00d0000f);
  6122. if (pipe_config->has_dp_encoder) {
  6123. /* Use SSC source */
  6124. if (pipe == PIPE_A)
  6125. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6126. 0x0df40000);
  6127. else
  6128. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6129. 0x0df70000);
  6130. } else { /* HDMI or VGA */
  6131. /* Use bend source */
  6132. if (pipe == PIPE_A)
  6133. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6134. 0x0df70000);
  6135. else
  6136. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6137. 0x0df40000);
  6138. }
  6139. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6140. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6141. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6142. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6143. coreclk |= 0x01000000;
  6144. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6145. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6146. mutex_unlock(&dev_priv->sb_lock);
  6147. }
  6148. static void chv_prepare_pll(struct intel_crtc *crtc,
  6149. const struct intel_crtc_state *pipe_config)
  6150. {
  6151. struct drm_device *dev = crtc->base.dev;
  6152. struct drm_i915_private *dev_priv = dev->dev_private;
  6153. enum pipe pipe = crtc->pipe;
  6154. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6155. u32 loopfilter, tribuf_calcntr;
  6156. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6157. u32 dpio_val;
  6158. int vco;
  6159. /* Enable Refclk and SSC */
  6160. I915_WRITE(DPLL(pipe),
  6161. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6162. /* No need to actually set up the DPLL with DSI */
  6163. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6164. return;
  6165. bestn = pipe_config->dpll.n;
  6166. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6167. bestm1 = pipe_config->dpll.m1;
  6168. bestm2 = pipe_config->dpll.m2 >> 22;
  6169. bestp1 = pipe_config->dpll.p1;
  6170. bestp2 = pipe_config->dpll.p2;
  6171. vco = pipe_config->dpll.vco;
  6172. dpio_val = 0;
  6173. loopfilter = 0;
  6174. mutex_lock(&dev_priv->sb_lock);
  6175. /* p1 and p2 divider */
  6176. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6177. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6178. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6179. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6180. 1 << DPIO_CHV_K_DIV_SHIFT);
  6181. /* Feedback post-divider - m2 */
  6182. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6183. /* Feedback refclk divider - n and m1 */
  6184. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6185. DPIO_CHV_M1_DIV_BY_2 |
  6186. 1 << DPIO_CHV_N_DIV_SHIFT);
  6187. /* M2 fraction division */
  6188. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6189. /* M2 fraction division enable */
  6190. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6191. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6192. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6193. if (bestm2_frac)
  6194. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6195. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6196. /* Program digital lock detect threshold */
  6197. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6198. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6199. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6200. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6201. if (!bestm2_frac)
  6202. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6203. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6204. /* Loop filter */
  6205. if (vco == 5400000) {
  6206. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6207. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6208. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6209. tribuf_calcntr = 0x9;
  6210. } else if (vco <= 6200000) {
  6211. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6212. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6213. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6214. tribuf_calcntr = 0x9;
  6215. } else if (vco <= 6480000) {
  6216. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6217. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6218. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6219. tribuf_calcntr = 0x8;
  6220. } else {
  6221. /* Not supported. Apply the same limits as in the max case */
  6222. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6223. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6224. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6225. tribuf_calcntr = 0;
  6226. }
  6227. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6228. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6229. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6230. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6231. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6232. /* AFC Recal */
  6233. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6234. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6235. DPIO_AFC_RECAL);
  6236. mutex_unlock(&dev_priv->sb_lock);
  6237. }
  6238. /**
  6239. * vlv_force_pll_on - forcibly enable just the PLL
  6240. * @dev_priv: i915 private structure
  6241. * @pipe: pipe PLL to enable
  6242. * @dpll: PLL configuration
  6243. *
  6244. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6245. * in cases where we need the PLL enabled even when @pipe is not going to
  6246. * be enabled.
  6247. */
  6248. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6249. const struct dpll *dpll)
  6250. {
  6251. struct intel_crtc *crtc =
  6252. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6253. struct intel_crtc_state *pipe_config;
  6254. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6255. if (!pipe_config)
  6256. return -ENOMEM;
  6257. pipe_config->base.crtc = &crtc->base;
  6258. pipe_config->pixel_multiplier = 1;
  6259. pipe_config->dpll = *dpll;
  6260. if (IS_CHERRYVIEW(dev)) {
  6261. chv_compute_dpll(crtc, pipe_config);
  6262. chv_prepare_pll(crtc, pipe_config);
  6263. chv_enable_pll(crtc, pipe_config);
  6264. } else {
  6265. vlv_compute_dpll(crtc, pipe_config);
  6266. vlv_prepare_pll(crtc, pipe_config);
  6267. vlv_enable_pll(crtc, pipe_config);
  6268. }
  6269. kfree(pipe_config);
  6270. return 0;
  6271. }
  6272. /**
  6273. * vlv_force_pll_off - forcibly disable just the PLL
  6274. * @dev_priv: i915 private structure
  6275. * @pipe: pipe PLL to disable
  6276. *
  6277. * Disable the PLL for @pipe. To be used in cases where we need
  6278. * the PLL enabled even when @pipe is not going to be enabled.
  6279. */
  6280. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6281. {
  6282. if (IS_CHERRYVIEW(dev))
  6283. chv_disable_pll(to_i915(dev), pipe);
  6284. else
  6285. vlv_disable_pll(to_i915(dev), pipe);
  6286. }
  6287. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6288. struct intel_crtc_state *crtc_state,
  6289. struct dpll *reduced_clock)
  6290. {
  6291. struct drm_device *dev = crtc->base.dev;
  6292. struct drm_i915_private *dev_priv = dev->dev_private;
  6293. u32 dpll;
  6294. bool is_sdvo;
  6295. struct dpll *clock = &crtc_state->dpll;
  6296. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6297. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6298. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6299. dpll = DPLL_VGA_MODE_DIS;
  6300. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6301. dpll |= DPLLB_MODE_LVDS;
  6302. else
  6303. dpll |= DPLLB_MODE_DAC_SERIAL;
  6304. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6305. dpll |= (crtc_state->pixel_multiplier - 1)
  6306. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6307. }
  6308. if (is_sdvo)
  6309. dpll |= DPLL_SDVO_HIGH_SPEED;
  6310. if (crtc_state->has_dp_encoder)
  6311. dpll |= DPLL_SDVO_HIGH_SPEED;
  6312. /* compute bitmask from p1 value */
  6313. if (IS_PINEVIEW(dev))
  6314. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6315. else {
  6316. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6317. if (IS_G4X(dev) && reduced_clock)
  6318. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6319. }
  6320. switch (clock->p2) {
  6321. case 5:
  6322. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6323. break;
  6324. case 7:
  6325. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6326. break;
  6327. case 10:
  6328. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6329. break;
  6330. case 14:
  6331. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6332. break;
  6333. }
  6334. if (INTEL_INFO(dev)->gen >= 4)
  6335. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6336. if (crtc_state->sdvo_tv_clock)
  6337. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6338. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6339. intel_panel_use_ssc(dev_priv))
  6340. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6341. else
  6342. dpll |= PLL_REF_INPUT_DREFCLK;
  6343. dpll |= DPLL_VCO_ENABLE;
  6344. crtc_state->dpll_hw_state.dpll = dpll;
  6345. if (INTEL_INFO(dev)->gen >= 4) {
  6346. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6347. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6348. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6349. }
  6350. }
  6351. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6352. struct intel_crtc_state *crtc_state,
  6353. struct dpll *reduced_clock)
  6354. {
  6355. struct drm_device *dev = crtc->base.dev;
  6356. struct drm_i915_private *dev_priv = dev->dev_private;
  6357. u32 dpll;
  6358. struct dpll *clock = &crtc_state->dpll;
  6359. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6360. dpll = DPLL_VGA_MODE_DIS;
  6361. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6362. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6363. } else {
  6364. if (clock->p1 == 2)
  6365. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6366. else
  6367. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6368. if (clock->p2 == 4)
  6369. dpll |= PLL_P2_DIVIDE_BY_4;
  6370. }
  6371. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6372. dpll |= DPLL_DVO_2X_MODE;
  6373. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6374. intel_panel_use_ssc(dev_priv))
  6375. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6376. else
  6377. dpll |= PLL_REF_INPUT_DREFCLK;
  6378. dpll |= DPLL_VCO_ENABLE;
  6379. crtc_state->dpll_hw_state.dpll = dpll;
  6380. }
  6381. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6382. {
  6383. struct drm_device *dev = intel_crtc->base.dev;
  6384. struct drm_i915_private *dev_priv = dev->dev_private;
  6385. enum pipe pipe = intel_crtc->pipe;
  6386. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6387. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6388. uint32_t crtc_vtotal, crtc_vblank_end;
  6389. int vsyncshift = 0;
  6390. /* We need to be careful not to changed the adjusted mode, for otherwise
  6391. * the hw state checker will get angry at the mismatch. */
  6392. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6393. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6394. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6395. /* the chip adds 2 halflines automatically */
  6396. crtc_vtotal -= 1;
  6397. crtc_vblank_end -= 1;
  6398. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6399. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6400. else
  6401. vsyncshift = adjusted_mode->crtc_hsync_start -
  6402. adjusted_mode->crtc_htotal / 2;
  6403. if (vsyncshift < 0)
  6404. vsyncshift += adjusted_mode->crtc_htotal;
  6405. }
  6406. if (INTEL_INFO(dev)->gen > 3)
  6407. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6408. I915_WRITE(HTOTAL(cpu_transcoder),
  6409. (adjusted_mode->crtc_hdisplay - 1) |
  6410. ((adjusted_mode->crtc_htotal - 1) << 16));
  6411. I915_WRITE(HBLANK(cpu_transcoder),
  6412. (adjusted_mode->crtc_hblank_start - 1) |
  6413. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6414. I915_WRITE(HSYNC(cpu_transcoder),
  6415. (adjusted_mode->crtc_hsync_start - 1) |
  6416. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6417. I915_WRITE(VTOTAL(cpu_transcoder),
  6418. (adjusted_mode->crtc_vdisplay - 1) |
  6419. ((crtc_vtotal - 1) << 16));
  6420. I915_WRITE(VBLANK(cpu_transcoder),
  6421. (adjusted_mode->crtc_vblank_start - 1) |
  6422. ((crtc_vblank_end - 1) << 16));
  6423. I915_WRITE(VSYNC(cpu_transcoder),
  6424. (adjusted_mode->crtc_vsync_start - 1) |
  6425. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6426. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6427. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6428. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6429. * bits. */
  6430. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6431. (pipe == PIPE_B || pipe == PIPE_C))
  6432. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6433. }
  6434. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6435. {
  6436. struct drm_device *dev = intel_crtc->base.dev;
  6437. struct drm_i915_private *dev_priv = dev->dev_private;
  6438. enum pipe pipe = intel_crtc->pipe;
  6439. /* pipesrc controls the size that is scaled from, which should
  6440. * always be the user's requested size.
  6441. */
  6442. I915_WRITE(PIPESRC(pipe),
  6443. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6444. (intel_crtc->config->pipe_src_h - 1));
  6445. }
  6446. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6447. struct intel_crtc_state *pipe_config)
  6448. {
  6449. struct drm_device *dev = crtc->base.dev;
  6450. struct drm_i915_private *dev_priv = dev->dev_private;
  6451. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6452. uint32_t tmp;
  6453. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6454. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6455. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6456. tmp = I915_READ(HBLANK(cpu_transcoder));
  6457. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6458. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6459. tmp = I915_READ(HSYNC(cpu_transcoder));
  6460. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6461. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6462. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6463. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6464. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6465. tmp = I915_READ(VBLANK(cpu_transcoder));
  6466. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6467. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6468. tmp = I915_READ(VSYNC(cpu_transcoder));
  6469. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6470. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6471. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6472. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6473. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6474. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6475. }
  6476. }
  6477. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6478. struct intel_crtc_state *pipe_config)
  6479. {
  6480. struct drm_device *dev = crtc->base.dev;
  6481. struct drm_i915_private *dev_priv = dev->dev_private;
  6482. u32 tmp;
  6483. tmp = I915_READ(PIPESRC(crtc->pipe));
  6484. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6485. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6486. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6487. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6488. }
  6489. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6490. struct intel_crtc_state *pipe_config)
  6491. {
  6492. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6493. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6494. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6495. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6496. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6497. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6498. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6499. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6500. mode->flags = pipe_config->base.adjusted_mode.flags;
  6501. mode->type = DRM_MODE_TYPE_DRIVER;
  6502. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6503. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6504. mode->hsync = drm_mode_hsync(mode);
  6505. mode->vrefresh = drm_mode_vrefresh(mode);
  6506. drm_mode_set_name(mode);
  6507. }
  6508. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6509. {
  6510. struct drm_device *dev = intel_crtc->base.dev;
  6511. struct drm_i915_private *dev_priv = dev->dev_private;
  6512. uint32_t pipeconf;
  6513. pipeconf = 0;
  6514. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6515. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6516. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6517. if (intel_crtc->config->double_wide)
  6518. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6519. /* only g4x and later have fancy bpc/dither controls */
  6520. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6521. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6522. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6523. pipeconf |= PIPECONF_DITHER_EN |
  6524. PIPECONF_DITHER_TYPE_SP;
  6525. switch (intel_crtc->config->pipe_bpp) {
  6526. case 18:
  6527. pipeconf |= PIPECONF_6BPC;
  6528. break;
  6529. case 24:
  6530. pipeconf |= PIPECONF_8BPC;
  6531. break;
  6532. case 30:
  6533. pipeconf |= PIPECONF_10BPC;
  6534. break;
  6535. default:
  6536. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6537. BUG();
  6538. }
  6539. }
  6540. if (HAS_PIPE_CXSR(dev)) {
  6541. if (intel_crtc->lowfreq_avail) {
  6542. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6543. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6544. } else {
  6545. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6546. }
  6547. }
  6548. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6549. if (INTEL_INFO(dev)->gen < 4 ||
  6550. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6551. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6552. else
  6553. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6554. } else
  6555. pipeconf |= PIPECONF_PROGRESSIVE;
  6556. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6557. intel_crtc->config->limited_color_range)
  6558. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6559. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6560. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6561. }
  6562. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6563. struct intel_crtc_state *crtc_state)
  6564. {
  6565. struct drm_device *dev = crtc->base.dev;
  6566. struct drm_i915_private *dev_priv = dev->dev_private;
  6567. const struct intel_limit *limit;
  6568. int refclk = 48000;
  6569. memset(&crtc_state->dpll_hw_state, 0,
  6570. sizeof(crtc_state->dpll_hw_state));
  6571. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6572. if (intel_panel_use_ssc(dev_priv)) {
  6573. refclk = dev_priv->vbt.lvds_ssc_freq;
  6574. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6575. }
  6576. limit = &intel_limits_i8xx_lvds;
  6577. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6578. limit = &intel_limits_i8xx_dvo;
  6579. } else {
  6580. limit = &intel_limits_i8xx_dac;
  6581. }
  6582. if (!crtc_state->clock_set &&
  6583. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6584. refclk, NULL, &crtc_state->dpll)) {
  6585. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6586. return -EINVAL;
  6587. }
  6588. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6589. return 0;
  6590. }
  6591. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6592. struct intel_crtc_state *crtc_state)
  6593. {
  6594. struct drm_device *dev = crtc->base.dev;
  6595. struct drm_i915_private *dev_priv = dev->dev_private;
  6596. const struct intel_limit *limit;
  6597. int refclk = 96000;
  6598. memset(&crtc_state->dpll_hw_state, 0,
  6599. sizeof(crtc_state->dpll_hw_state));
  6600. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6601. if (intel_panel_use_ssc(dev_priv)) {
  6602. refclk = dev_priv->vbt.lvds_ssc_freq;
  6603. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6604. }
  6605. if (intel_is_dual_link_lvds(dev))
  6606. limit = &intel_limits_g4x_dual_channel_lvds;
  6607. else
  6608. limit = &intel_limits_g4x_single_channel_lvds;
  6609. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6610. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6611. limit = &intel_limits_g4x_hdmi;
  6612. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6613. limit = &intel_limits_g4x_sdvo;
  6614. } else {
  6615. /* The option is for other outputs */
  6616. limit = &intel_limits_i9xx_sdvo;
  6617. }
  6618. if (!crtc_state->clock_set &&
  6619. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6620. refclk, NULL, &crtc_state->dpll)) {
  6621. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6622. return -EINVAL;
  6623. }
  6624. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6625. return 0;
  6626. }
  6627. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6628. struct intel_crtc_state *crtc_state)
  6629. {
  6630. struct drm_device *dev = crtc->base.dev;
  6631. struct drm_i915_private *dev_priv = dev->dev_private;
  6632. const struct intel_limit *limit;
  6633. int refclk = 96000;
  6634. memset(&crtc_state->dpll_hw_state, 0,
  6635. sizeof(crtc_state->dpll_hw_state));
  6636. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6637. if (intel_panel_use_ssc(dev_priv)) {
  6638. refclk = dev_priv->vbt.lvds_ssc_freq;
  6639. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6640. }
  6641. limit = &intel_limits_pineview_lvds;
  6642. } else {
  6643. limit = &intel_limits_pineview_sdvo;
  6644. }
  6645. if (!crtc_state->clock_set &&
  6646. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6647. refclk, NULL, &crtc_state->dpll)) {
  6648. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6649. return -EINVAL;
  6650. }
  6651. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6652. return 0;
  6653. }
  6654. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6655. struct intel_crtc_state *crtc_state)
  6656. {
  6657. struct drm_device *dev = crtc->base.dev;
  6658. struct drm_i915_private *dev_priv = dev->dev_private;
  6659. const struct intel_limit *limit;
  6660. int refclk = 96000;
  6661. memset(&crtc_state->dpll_hw_state, 0,
  6662. sizeof(crtc_state->dpll_hw_state));
  6663. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6664. if (intel_panel_use_ssc(dev_priv)) {
  6665. refclk = dev_priv->vbt.lvds_ssc_freq;
  6666. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6667. }
  6668. limit = &intel_limits_i9xx_lvds;
  6669. } else {
  6670. limit = &intel_limits_i9xx_sdvo;
  6671. }
  6672. if (!crtc_state->clock_set &&
  6673. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6674. refclk, NULL, &crtc_state->dpll)) {
  6675. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6676. return -EINVAL;
  6677. }
  6678. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6679. return 0;
  6680. }
  6681. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6682. struct intel_crtc_state *crtc_state)
  6683. {
  6684. int refclk = 100000;
  6685. const struct intel_limit *limit = &intel_limits_chv;
  6686. memset(&crtc_state->dpll_hw_state, 0,
  6687. sizeof(crtc_state->dpll_hw_state));
  6688. if (!crtc_state->clock_set &&
  6689. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6690. refclk, NULL, &crtc_state->dpll)) {
  6691. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6692. return -EINVAL;
  6693. }
  6694. chv_compute_dpll(crtc, crtc_state);
  6695. return 0;
  6696. }
  6697. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6698. struct intel_crtc_state *crtc_state)
  6699. {
  6700. int refclk = 100000;
  6701. const struct intel_limit *limit = &intel_limits_vlv;
  6702. memset(&crtc_state->dpll_hw_state, 0,
  6703. sizeof(crtc_state->dpll_hw_state));
  6704. if (!crtc_state->clock_set &&
  6705. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6706. refclk, NULL, &crtc_state->dpll)) {
  6707. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6708. return -EINVAL;
  6709. }
  6710. vlv_compute_dpll(crtc, crtc_state);
  6711. return 0;
  6712. }
  6713. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6714. struct intel_crtc_state *pipe_config)
  6715. {
  6716. struct drm_device *dev = crtc->base.dev;
  6717. struct drm_i915_private *dev_priv = dev->dev_private;
  6718. uint32_t tmp;
  6719. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6720. return;
  6721. tmp = I915_READ(PFIT_CONTROL);
  6722. if (!(tmp & PFIT_ENABLE))
  6723. return;
  6724. /* Check whether the pfit is attached to our pipe. */
  6725. if (INTEL_INFO(dev)->gen < 4) {
  6726. if (crtc->pipe != PIPE_B)
  6727. return;
  6728. } else {
  6729. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6730. return;
  6731. }
  6732. pipe_config->gmch_pfit.control = tmp;
  6733. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6734. }
  6735. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6736. struct intel_crtc_state *pipe_config)
  6737. {
  6738. struct drm_device *dev = crtc->base.dev;
  6739. struct drm_i915_private *dev_priv = dev->dev_private;
  6740. int pipe = pipe_config->cpu_transcoder;
  6741. struct dpll clock;
  6742. u32 mdiv;
  6743. int refclk = 100000;
  6744. /* In case of DSI, DPLL will not be used */
  6745. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6746. return;
  6747. mutex_lock(&dev_priv->sb_lock);
  6748. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6749. mutex_unlock(&dev_priv->sb_lock);
  6750. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6751. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6752. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6753. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6754. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6755. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6756. }
  6757. static void
  6758. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6759. struct intel_initial_plane_config *plane_config)
  6760. {
  6761. struct drm_device *dev = crtc->base.dev;
  6762. struct drm_i915_private *dev_priv = dev->dev_private;
  6763. u32 val, base, offset;
  6764. int pipe = crtc->pipe, plane = crtc->plane;
  6765. int fourcc, pixel_format;
  6766. unsigned int aligned_height;
  6767. struct drm_framebuffer *fb;
  6768. struct intel_framebuffer *intel_fb;
  6769. val = I915_READ(DSPCNTR(plane));
  6770. if (!(val & DISPLAY_PLANE_ENABLE))
  6771. return;
  6772. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6773. if (!intel_fb) {
  6774. DRM_DEBUG_KMS("failed to alloc fb\n");
  6775. return;
  6776. }
  6777. fb = &intel_fb->base;
  6778. if (INTEL_INFO(dev)->gen >= 4) {
  6779. if (val & DISPPLANE_TILED) {
  6780. plane_config->tiling = I915_TILING_X;
  6781. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6782. }
  6783. }
  6784. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6785. fourcc = i9xx_format_to_fourcc(pixel_format);
  6786. fb->pixel_format = fourcc;
  6787. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6788. if (INTEL_INFO(dev)->gen >= 4) {
  6789. if (plane_config->tiling)
  6790. offset = I915_READ(DSPTILEOFF(plane));
  6791. else
  6792. offset = I915_READ(DSPLINOFF(plane));
  6793. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6794. } else {
  6795. base = I915_READ(DSPADDR(plane));
  6796. }
  6797. plane_config->base = base;
  6798. val = I915_READ(PIPESRC(pipe));
  6799. fb->width = ((val >> 16) & 0xfff) + 1;
  6800. fb->height = ((val >> 0) & 0xfff) + 1;
  6801. val = I915_READ(DSPSTRIDE(pipe));
  6802. fb->pitches[0] = val & 0xffffffc0;
  6803. aligned_height = intel_fb_align_height(dev, fb->height,
  6804. fb->pixel_format,
  6805. fb->modifier[0]);
  6806. plane_config->size = fb->pitches[0] * aligned_height;
  6807. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6808. pipe_name(pipe), plane, fb->width, fb->height,
  6809. fb->bits_per_pixel, base, fb->pitches[0],
  6810. plane_config->size);
  6811. plane_config->fb = intel_fb;
  6812. }
  6813. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6814. struct intel_crtc_state *pipe_config)
  6815. {
  6816. struct drm_device *dev = crtc->base.dev;
  6817. struct drm_i915_private *dev_priv = dev->dev_private;
  6818. int pipe = pipe_config->cpu_transcoder;
  6819. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6820. struct dpll clock;
  6821. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6822. int refclk = 100000;
  6823. /* In case of DSI, DPLL will not be used */
  6824. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6825. return;
  6826. mutex_lock(&dev_priv->sb_lock);
  6827. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6828. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6829. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6830. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6831. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6832. mutex_unlock(&dev_priv->sb_lock);
  6833. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6834. clock.m2 = (pll_dw0 & 0xff) << 22;
  6835. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6836. clock.m2 |= pll_dw2 & 0x3fffff;
  6837. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6838. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6839. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6840. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6841. }
  6842. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6843. struct intel_crtc_state *pipe_config)
  6844. {
  6845. struct drm_device *dev = crtc->base.dev;
  6846. struct drm_i915_private *dev_priv = dev->dev_private;
  6847. enum intel_display_power_domain power_domain;
  6848. uint32_t tmp;
  6849. bool ret;
  6850. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6851. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6852. return false;
  6853. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6854. pipe_config->shared_dpll = NULL;
  6855. ret = false;
  6856. tmp = I915_READ(PIPECONF(crtc->pipe));
  6857. if (!(tmp & PIPECONF_ENABLE))
  6858. goto out;
  6859. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6860. switch (tmp & PIPECONF_BPC_MASK) {
  6861. case PIPECONF_6BPC:
  6862. pipe_config->pipe_bpp = 18;
  6863. break;
  6864. case PIPECONF_8BPC:
  6865. pipe_config->pipe_bpp = 24;
  6866. break;
  6867. case PIPECONF_10BPC:
  6868. pipe_config->pipe_bpp = 30;
  6869. break;
  6870. default:
  6871. break;
  6872. }
  6873. }
  6874. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6875. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6876. pipe_config->limited_color_range = true;
  6877. if (INTEL_INFO(dev)->gen < 4)
  6878. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6879. intel_get_pipe_timings(crtc, pipe_config);
  6880. intel_get_pipe_src_size(crtc, pipe_config);
  6881. i9xx_get_pfit_config(crtc, pipe_config);
  6882. if (INTEL_INFO(dev)->gen >= 4) {
  6883. /* No way to read it out on pipes B and C */
  6884. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6885. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6886. else
  6887. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6888. pipe_config->pixel_multiplier =
  6889. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6890. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6891. pipe_config->dpll_hw_state.dpll_md = tmp;
  6892. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6893. tmp = I915_READ(DPLL(crtc->pipe));
  6894. pipe_config->pixel_multiplier =
  6895. ((tmp & SDVO_MULTIPLIER_MASK)
  6896. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6897. } else {
  6898. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6899. * port and will be fixed up in the encoder->get_config
  6900. * function. */
  6901. pipe_config->pixel_multiplier = 1;
  6902. }
  6903. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6904. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6905. /*
  6906. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6907. * on 830. Filter it out here so that we don't
  6908. * report errors due to that.
  6909. */
  6910. if (IS_I830(dev))
  6911. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6912. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6913. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6914. } else {
  6915. /* Mask out read-only status bits. */
  6916. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6917. DPLL_PORTC_READY_MASK |
  6918. DPLL_PORTB_READY_MASK);
  6919. }
  6920. if (IS_CHERRYVIEW(dev))
  6921. chv_crtc_clock_get(crtc, pipe_config);
  6922. else if (IS_VALLEYVIEW(dev))
  6923. vlv_crtc_clock_get(crtc, pipe_config);
  6924. else
  6925. i9xx_crtc_clock_get(crtc, pipe_config);
  6926. /*
  6927. * Normally the dotclock is filled in by the encoder .get_config()
  6928. * but in case the pipe is enabled w/o any ports we need a sane
  6929. * default.
  6930. */
  6931. pipe_config->base.adjusted_mode.crtc_clock =
  6932. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6933. ret = true;
  6934. out:
  6935. intel_display_power_put(dev_priv, power_domain);
  6936. return ret;
  6937. }
  6938. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6939. {
  6940. struct drm_i915_private *dev_priv = dev->dev_private;
  6941. struct intel_encoder *encoder;
  6942. u32 val, final;
  6943. bool has_lvds = false;
  6944. bool has_cpu_edp = false;
  6945. bool has_panel = false;
  6946. bool has_ck505 = false;
  6947. bool can_ssc = false;
  6948. /* We need to take the global config into account */
  6949. for_each_intel_encoder(dev, encoder) {
  6950. switch (encoder->type) {
  6951. case INTEL_OUTPUT_LVDS:
  6952. has_panel = true;
  6953. has_lvds = true;
  6954. break;
  6955. case INTEL_OUTPUT_EDP:
  6956. has_panel = true;
  6957. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6958. has_cpu_edp = true;
  6959. break;
  6960. default:
  6961. break;
  6962. }
  6963. }
  6964. if (HAS_PCH_IBX(dev)) {
  6965. has_ck505 = dev_priv->vbt.display_clock_mode;
  6966. can_ssc = has_ck505;
  6967. } else {
  6968. has_ck505 = false;
  6969. can_ssc = true;
  6970. }
  6971. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6972. has_panel, has_lvds, has_ck505);
  6973. /* Ironlake: try to setup display ref clock before DPLL
  6974. * enabling. This is only under driver's control after
  6975. * PCH B stepping, previous chipset stepping should be
  6976. * ignoring this setting.
  6977. */
  6978. val = I915_READ(PCH_DREF_CONTROL);
  6979. /* As we must carefully and slowly disable/enable each source in turn,
  6980. * compute the final state we want first and check if we need to
  6981. * make any changes at all.
  6982. */
  6983. final = val;
  6984. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6985. if (has_ck505)
  6986. final |= DREF_NONSPREAD_CK505_ENABLE;
  6987. else
  6988. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6989. final &= ~DREF_SSC_SOURCE_MASK;
  6990. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6991. final &= ~DREF_SSC1_ENABLE;
  6992. if (has_panel) {
  6993. final |= DREF_SSC_SOURCE_ENABLE;
  6994. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6995. final |= DREF_SSC1_ENABLE;
  6996. if (has_cpu_edp) {
  6997. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6998. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6999. else
  7000. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7001. } else
  7002. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7003. } else {
  7004. final |= DREF_SSC_SOURCE_DISABLE;
  7005. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7006. }
  7007. if (final == val)
  7008. return;
  7009. /* Always enable nonspread source */
  7010. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7011. if (has_ck505)
  7012. val |= DREF_NONSPREAD_CK505_ENABLE;
  7013. else
  7014. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7015. if (has_panel) {
  7016. val &= ~DREF_SSC_SOURCE_MASK;
  7017. val |= DREF_SSC_SOURCE_ENABLE;
  7018. /* SSC must be turned on before enabling the CPU output */
  7019. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7020. DRM_DEBUG_KMS("Using SSC on panel\n");
  7021. val |= DREF_SSC1_ENABLE;
  7022. } else
  7023. val &= ~DREF_SSC1_ENABLE;
  7024. /* Get SSC going before enabling the outputs */
  7025. I915_WRITE(PCH_DREF_CONTROL, val);
  7026. POSTING_READ(PCH_DREF_CONTROL);
  7027. udelay(200);
  7028. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7029. /* Enable CPU source on CPU attached eDP */
  7030. if (has_cpu_edp) {
  7031. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7032. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7033. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7034. } else
  7035. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7036. } else
  7037. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7038. I915_WRITE(PCH_DREF_CONTROL, val);
  7039. POSTING_READ(PCH_DREF_CONTROL);
  7040. udelay(200);
  7041. } else {
  7042. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7043. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7044. /* Turn off CPU output */
  7045. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7046. I915_WRITE(PCH_DREF_CONTROL, val);
  7047. POSTING_READ(PCH_DREF_CONTROL);
  7048. udelay(200);
  7049. /* Turn off the SSC source */
  7050. val &= ~DREF_SSC_SOURCE_MASK;
  7051. val |= DREF_SSC_SOURCE_DISABLE;
  7052. /* Turn off SSC1 */
  7053. val &= ~DREF_SSC1_ENABLE;
  7054. I915_WRITE(PCH_DREF_CONTROL, val);
  7055. POSTING_READ(PCH_DREF_CONTROL);
  7056. udelay(200);
  7057. }
  7058. BUG_ON(val != final);
  7059. }
  7060. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7061. {
  7062. uint32_t tmp;
  7063. tmp = I915_READ(SOUTH_CHICKEN2);
  7064. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7065. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7066. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7067. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7068. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7069. tmp = I915_READ(SOUTH_CHICKEN2);
  7070. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7071. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7072. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7073. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7074. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7075. }
  7076. /* WaMPhyProgramming:hsw */
  7077. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7078. {
  7079. uint32_t tmp;
  7080. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7081. tmp &= ~(0xFF << 24);
  7082. tmp |= (0x12 << 24);
  7083. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7084. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7085. tmp |= (1 << 11);
  7086. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7087. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7088. tmp |= (1 << 11);
  7089. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7090. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7091. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7092. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7093. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7094. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7095. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7096. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7097. tmp &= ~(7 << 13);
  7098. tmp |= (5 << 13);
  7099. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7100. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7101. tmp &= ~(7 << 13);
  7102. tmp |= (5 << 13);
  7103. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7104. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7105. tmp &= ~0xFF;
  7106. tmp |= 0x1C;
  7107. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7108. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7109. tmp &= ~0xFF;
  7110. tmp |= 0x1C;
  7111. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7112. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7113. tmp &= ~(0xFF << 16);
  7114. tmp |= (0x1C << 16);
  7115. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7116. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7117. tmp &= ~(0xFF << 16);
  7118. tmp |= (0x1C << 16);
  7119. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7120. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7121. tmp |= (1 << 27);
  7122. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7123. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7124. tmp |= (1 << 27);
  7125. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7126. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7127. tmp &= ~(0xF << 28);
  7128. tmp |= (4 << 28);
  7129. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7130. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7131. tmp &= ~(0xF << 28);
  7132. tmp |= (4 << 28);
  7133. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7134. }
  7135. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7136. * Programming" based on the parameters passed:
  7137. * - Sequence to enable CLKOUT_DP
  7138. * - Sequence to enable CLKOUT_DP without spread
  7139. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7140. */
  7141. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7142. bool with_fdi)
  7143. {
  7144. struct drm_i915_private *dev_priv = dev->dev_private;
  7145. uint32_t reg, tmp;
  7146. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7147. with_spread = true;
  7148. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7149. with_fdi = false;
  7150. mutex_lock(&dev_priv->sb_lock);
  7151. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7152. tmp &= ~SBI_SSCCTL_DISABLE;
  7153. tmp |= SBI_SSCCTL_PATHALT;
  7154. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7155. udelay(24);
  7156. if (with_spread) {
  7157. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7158. tmp &= ~SBI_SSCCTL_PATHALT;
  7159. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7160. if (with_fdi) {
  7161. lpt_reset_fdi_mphy(dev_priv);
  7162. lpt_program_fdi_mphy(dev_priv);
  7163. }
  7164. }
  7165. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7166. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7167. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7168. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7169. mutex_unlock(&dev_priv->sb_lock);
  7170. }
  7171. /* Sequence to disable CLKOUT_DP */
  7172. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7173. {
  7174. struct drm_i915_private *dev_priv = dev->dev_private;
  7175. uint32_t reg, tmp;
  7176. mutex_lock(&dev_priv->sb_lock);
  7177. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7178. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7179. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7180. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7181. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7182. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7183. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7184. tmp |= SBI_SSCCTL_PATHALT;
  7185. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7186. udelay(32);
  7187. }
  7188. tmp |= SBI_SSCCTL_DISABLE;
  7189. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7190. }
  7191. mutex_unlock(&dev_priv->sb_lock);
  7192. }
  7193. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7194. static const uint16_t sscdivintphase[] = {
  7195. [BEND_IDX( 50)] = 0x3B23,
  7196. [BEND_IDX( 45)] = 0x3B23,
  7197. [BEND_IDX( 40)] = 0x3C23,
  7198. [BEND_IDX( 35)] = 0x3C23,
  7199. [BEND_IDX( 30)] = 0x3D23,
  7200. [BEND_IDX( 25)] = 0x3D23,
  7201. [BEND_IDX( 20)] = 0x3E23,
  7202. [BEND_IDX( 15)] = 0x3E23,
  7203. [BEND_IDX( 10)] = 0x3F23,
  7204. [BEND_IDX( 5)] = 0x3F23,
  7205. [BEND_IDX( 0)] = 0x0025,
  7206. [BEND_IDX( -5)] = 0x0025,
  7207. [BEND_IDX(-10)] = 0x0125,
  7208. [BEND_IDX(-15)] = 0x0125,
  7209. [BEND_IDX(-20)] = 0x0225,
  7210. [BEND_IDX(-25)] = 0x0225,
  7211. [BEND_IDX(-30)] = 0x0325,
  7212. [BEND_IDX(-35)] = 0x0325,
  7213. [BEND_IDX(-40)] = 0x0425,
  7214. [BEND_IDX(-45)] = 0x0425,
  7215. [BEND_IDX(-50)] = 0x0525,
  7216. };
  7217. /*
  7218. * Bend CLKOUT_DP
  7219. * steps -50 to 50 inclusive, in steps of 5
  7220. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7221. * change in clock period = -(steps / 10) * 5.787 ps
  7222. */
  7223. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7224. {
  7225. uint32_t tmp;
  7226. int idx = BEND_IDX(steps);
  7227. if (WARN_ON(steps % 5 != 0))
  7228. return;
  7229. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7230. return;
  7231. mutex_lock(&dev_priv->sb_lock);
  7232. if (steps % 10 != 0)
  7233. tmp = 0xAAAAAAAB;
  7234. else
  7235. tmp = 0x00000000;
  7236. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7237. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7238. tmp &= 0xffff0000;
  7239. tmp |= sscdivintphase[idx];
  7240. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7241. mutex_unlock(&dev_priv->sb_lock);
  7242. }
  7243. #undef BEND_IDX
  7244. static void lpt_init_pch_refclk(struct drm_device *dev)
  7245. {
  7246. struct intel_encoder *encoder;
  7247. bool has_vga = false;
  7248. for_each_intel_encoder(dev, encoder) {
  7249. switch (encoder->type) {
  7250. case INTEL_OUTPUT_ANALOG:
  7251. has_vga = true;
  7252. break;
  7253. default:
  7254. break;
  7255. }
  7256. }
  7257. if (has_vga) {
  7258. lpt_bend_clkout_dp(to_i915(dev), 0);
  7259. lpt_enable_clkout_dp(dev, true, true);
  7260. } else {
  7261. lpt_disable_clkout_dp(dev);
  7262. }
  7263. }
  7264. /*
  7265. * Initialize reference clocks when the driver loads
  7266. */
  7267. void intel_init_pch_refclk(struct drm_device *dev)
  7268. {
  7269. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7270. ironlake_init_pch_refclk(dev);
  7271. else if (HAS_PCH_LPT(dev))
  7272. lpt_init_pch_refclk(dev);
  7273. }
  7274. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7275. {
  7276. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7277. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7278. int pipe = intel_crtc->pipe;
  7279. uint32_t val;
  7280. val = 0;
  7281. switch (intel_crtc->config->pipe_bpp) {
  7282. case 18:
  7283. val |= PIPECONF_6BPC;
  7284. break;
  7285. case 24:
  7286. val |= PIPECONF_8BPC;
  7287. break;
  7288. case 30:
  7289. val |= PIPECONF_10BPC;
  7290. break;
  7291. case 36:
  7292. val |= PIPECONF_12BPC;
  7293. break;
  7294. default:
  7295. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7296. BUG();
  7297. }
  7298. if (intel_crtc->config->dither)
  7299. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7300. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7301. val |= PIPECONF_INTERLACED_ILK;
  7302. else
  7303. val |= PIPECONF_PROGRESSIVE;
  7304. if (intel_crtc->config->limited_color_range)
  7305. val |= PIPECONF_COLOR_RANGE_SELECT;
  7306. I915_WRITE(PIPECONF(pipe), val);
  7307. POSTING_READ(PIPECONF(pipe));
  7308. }
  7309. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7310. {
  7311. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7312. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7313. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7314. u32 val = 0;
  7315. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7316. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7317. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7318. val |= PIPECONF_INTERLACED_ILK;
  7319. else
  7320. val |= PIPECONF_PROGRESSIVE;
  7321. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7322. POSTING_READ(PIPECONF(cpu_transcoder));
  7323. }
  7324. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7325. {
  7326. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7328. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7329. u32 val = 0;
  7330. switch (intel_crtc->config->pipe_bpp) {
  7331. case 18:
  7332. val |= PIPEMISC_DITHER_6_BPC;
  7333. break;
  7334. case 24:
  7335. val |= PIPEMISC_DITHER_8_BPC;
  7336. break;
  7337. case 30:
  7338. val |= PIPEMISC_DITHER_10_BPC;
  7339. break;
  7340. case 36:
  7341. val |= PIPEMISC_DITHER_12_BPC;
  7342. break;
  7343. default:
  7344. /* Case prevented by pipe_config_set_bpp. */
  7345. BUG();
  7346. }
  7347. if (intel_crtc->config->dither)
  7348. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7349. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7350. }
  7351. }
  7352. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7353. {
  7354. /*
  7355. * Account for spread spectrum to avoid
  7356. * oversubscribing the link. Max center spread
  7357. * is 2.5%; use 5% for safety's sake.
  7358. */
  7359. u32 bps = target_clock * bpp * 21 / 20;
  7360. return DIV_ROUND_UP(bps, link_bw * 8);
  7361. }
  7362. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7363. {
  7364. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7365. }
  7366. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7367. struct intel_crtc_state *crtc_state,
  7368. struct dpll *reduced_clock)
  7369. {
  7370. struct drm_crtc *crtc = &intel_crtc->base;
  7371. struct drm_device *dev = crtc->dev;
  7372. struct drm_i915_private *dev_priv = dev->dev_private;
  7373. struct drm_atomic_state *state = crtc_state->base.state;
  7374. struct drm_connector *connector;
  7375. struct drm_connector_state *connector_state;
  7376. struct intel_encoder *encoder;
  7377. u32 dpll, fp, fp2;
  7378. int factor, i;
  7379. bool is_lvds = false, is_sdvo = false;
  7380. for_each_connector_in_state(state, connector, connector_state, i) {
  7381. if (connector_state->crtc != crtc_state->base.crtc)
  7382. continue;
  7383. encoder = to_intel_encoder(connector_state->best_encoder);
  7384. switch (encoder->type) {
  7385. case INTEL_OUTPUT_LVDS:
  7386. is_lvds = true;
  7387. break;
  7388. case INTEL_OUTPUT_SDVO:
  7389. case INTEL_OUTPUT_HDMI:
  7390. is_sdvo = true;
  7391. break;
  7392. default:
  7393. break;
  7394. }
  7395. }
  7396. /* Enable autotuning of the PLL clock (if permissible) */
  7397. factor = 21;
  7398. if (is_lvds) {
  7399. if ((intel_panel_use_ssc(dev_priv) &&
  7400. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7401. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7402. factor = 25;
  7403. } else if (crtc_state->sdvo_tv_clock)
  7404. factor = 20;
  7405. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7406. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7407. fp |= FP_CB_TUNE;
  7408. if (reduced_clock) {
  7409. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7410. if (reduced_clock->m < factor * reduced_clock->n)
  7411. fp2 |= FP_CB_TUNE;
  7412. } else {
  7413. fp2 = fp;
  7414. }
  7415. dpll = 0;
  7416. if (is_lvds)
  7417. dpll |= DPLLB_MODE_LVDS;
  7418. else
  7419. dpll |= DPLLB_MODE_DAC_SERIAL;
  7420. dpll |= (crtc_state->pixel_multiplier - 1)
  7421. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7422. if (is_sdvo)
  7423. dpll |= DPLL_SDVO_HIGH_SPEED;
  7424. if (crtc_state->has_dp_encoder)
  7425. dpll |= DPLL_SDVO_HIGH_SPEED;
  7426. /* compute bitmask from p1 value */
  7427. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7428. /* also FPA1 */
  7429. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7430. switch (crtc_state->dpll.p2) {
  7431. case 5:
  7432. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7433. break;
  7434. case 7:
  7435. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7436. break;
  7437. case 10:
  7438. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7439. break;
  7440. case 14:
  7441. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7442. break;
  7443. }
  7444. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7445. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7446. else
  7447. dpll |= PLL_REF_INPUT_DREFCLK;
  7448. dpll |= DPLL_VCO_ENABLE;
  7449. crtc_state->dpll_hw_state.dpll = dpll;
  7450. crtc_state->dpll_hw_state.fp0 = fp;
  7451. crtc_state->dpll_hw_state.fp1 = fp2;
  7452. }
  7453. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7454. struct intel_crtc_state *crtc_state)
  7455. {
  7456. struct drm_device *dev = crtc->base.dev;
  7457. struct drm_i915_private *dev_priv = dev->dev_private;
  7458. struct dpll reduced_clock;
  7459. bool has_reduced_clock = false;
  7460. struct intel_shared_dpll *pll;
  7461. const struct intel_limit *limit;
  7462. int refclk = 120000;
  7463. memset(&crtc_state->dpll_hw_state, 0,
  7464. sizeof(crtc_state->dpll_hw_state));
  7465. crtc->lowfreq_avail = false;
  7466. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7467. if (!crtc_state->has_pch_encoder)
  7468. return 0;
  7469. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7470. if (intel_panel_use_ssc(dev_priv)) {
  7471. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7472. dev_priv->vbt.lvds_ssc_freq);
  7473. refclk = dev_priv->vbt.lvds_ssc_freq;
  7474. }
  7475. if (intel_is_dual_link_lvds(dev)) {
  7476. if (refclk == 100000)
  7477. limit = &intel_limits_ironlake_dual_lvds_100m;
  7478. else
  7479. limit = &intel_limits_ironlake_dual_lvds;
  7480. } else {
  7481. if (refclk == 100000)
  7482. limit = &intel_limits_ironlake_single_lvds_100m;
  7483. else
  7484. limit = &intel_limits_ironlake_single_lvds;
  7485. }
  7486. } else {
  7487. limit = &intel_limits_ironlake_dac;
  7488. }
  7489. if (!crtc_state->clock_set &&
  7490. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7491. refclk, NULL, &crtc_state->dpll)) {
  7492. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7493. return -EINVAL;
  7494. }
  7495. ironlake_compute_dpll(crtc, crtc_state,
  7496. has_reduced_clock ? &reduced_clock : NULL);
  7497. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7498. if (pll == NULL) {
  7499. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7500. pipe_name(crtc->pipe));
  7501. return -EINVAL;
  7502. }
  7503. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7504. has_reduced_clock)
  7505. crtc->lowfreq_avail = true;
  7506. return 0;
  7507. }
  7508. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7509. struct intel_link_m_n *m_n)
  7510. {
  7511. struct drm_device *dev = crtc->base.dev;
  7512. struct drm_i915_private *dev_priv = dev->dev_private;
  7513. enum pipe pipe = crtc->pipe;
  7514. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7515. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7516. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7517. & ~TU_SIZE_MASK;
  7518. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7519. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7520. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7521. }
  7522. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7523. enum transcoder transcoder,
  7524. struct intel_link_m_n *m_n,
  7525. struct intel_link_m_n *m2_n2)
  7526. {
  7527. struct drm_device *dev = crtc->base.dev;
  7528. struct drm_i915_private *dev_priv = dev->dev_private;
  7529. enum pipe pipe = crtc->pipe;
  7530. if (INTEL_INFO(dev)->gen >= 5) {
  7531. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7532. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7533. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7534. & ~TU_SIZE_MASK;
  7535. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7536. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7537. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7538. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7539. * gen < 8) and if DRRS is supported (to make sure the
  7540. * registers are not unnecessarily read).
  7541. */
  7542. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7543. crtc->config->has_drrs) {
  7544. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7545. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7546. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7547. & ~TU_SIZE_MASK;
  7548. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7549. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7550. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7551. }
  7552. } else {
  7553. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7554. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7555. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7556. & ~TU_SIZE_MASK;
  7557. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7558. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7559. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7560. }
  7561. }
  7562. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7563. struct intel_crtc_state *pipe_config)
  7564. {
  7565. if (pipe_config->has_pch_encoder)
  7566. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7567. else
  7568. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7569. &pipe_config->dp_m_n,
  7570. &pipe_config->dp_m2_n2);
  7571. }
  7572. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7573. struct intel_crtc_state *pipe_config)
  7574. {
  7575. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7576. &pipe_config->fdi_m_n, NULL);
  7577. }
  7578. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7579. struct intel_crtc_state *pipe_config)
  7580. {
  7581. struct drm_device *dev = crtc->base.dev;
  7582. struct drm_i915_private *dev_priv = dev->dev_private;
  7583. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7584. uint32_t ps_ctrl = 0;
  7585. int id = -1;
  7586. int i;
  7587. /* find scaler attached to this pipe */
  7588. for (i = 0; i < crtc->num_scalers; i++) {
  7589. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7590. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7591. id = i;
  7592. pipe_config->pch_pfit.enabled = true;
  7593. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7594. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7595. break;
  7596. }
  7597. }
  7598. scaler_state->scaler_id = id;
  7599. if (id >= 0) {
  7600. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7601. } else {
  7602. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7603. }
  7604. }
  7605. static void
  7606. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7607. struct intel_initial_plane_config *plane_config)
  7608. {
  7609. struct drm_device *dev = crtc->base.dev;
  7610. struct drm_i915_private *dev_priv = dev->dev_private;
  7611. u32 val, base, offset, stride_mult, tiling;
  7612. int pipe = crtc->pipe;
  7613. int fourcc, pixel_format;
  7614. unsigned int aligned_height;
  7615. struct drm_framebuffer *fb;
  7616. struct intel_framebuffer *intel_fb;
  7617. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7618. if (!intel_fb) {
  7619. DRM_DEBUG_KMS("failed to alloc fb\n");
  7620. return;
  7621. }
  7622. fb = &intel_fb->base;
  7623. val = I915_READ(PLANE_CTL(pipe, 0));
  7624. if (!(val & PLANE_CTL_ENABLE))
  7625. goto error;
  7626. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7627. fourcc = skl_format_to_fourcc(pixel_format,
  7628. val & PLANE_CTL_ORDER_RGBX,
  7629. val & PLANE_CTL_ALPHA_MASK);
  7630. fb->pixel_format = fourcc;
  7631. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7632. tiling = val & PLANE_CTL_TILED_MASK;
  7633. switch (tiling) {
  7634. case PLANE_CTL_TILED_LINEAR:
  7635. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7636. break;
  7637. case PLANE_CTL_TILED_X:
  7638. plane_config->tiling = I915_TILING_X;
  7639. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7640. break;
  7641. case PLANE_CTL_TILED_Y:
  7642. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7643. break;
  7644. case PLANE_CTL_TILED_YF:
  7645. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7646. break;
  7647. default:
  7648. MISSING_CASE(tiling);
  7649. goto error;
  7650. }
  7651. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7652. plane_config->base = base;
  7653. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7654. val = I915_READ(PLANE_SIZE(pipe, 0));
  7655. fb->height = ((val >> 16) & 0xfff) + 1;
  7656. fb->width = ((val >> 0) & 0x1fff) + 1;
  7657. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7658. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7659. fb->pixel_format);
  7660. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7661. aligned_height = intel_fb_align_height(dev, fb->height,
  7662. fb->pixel_format,
  7663. fb->modifier[0]);
  7664. plane_config->size = fb->pitches[0] * aligned_height;
  7665. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7666. pipe_name(pipe), fb->width, fb->height,
  7667. fb->bits_per_pixel, base, fb->pitches[0],
  7668. plane_config->size);
  7669. plane_config->fb = intel_fb;
  7670. return;
  7671. error:
  7672. kfree(fb);
  7673. }
  7674. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7675. struct intel_crtc_state *pipe_config)
  7676. {
  7677. struct drm_device *dev = crtc->base.dev;
  7678. struct drm_i915_private *dev_priv = dev->dev_private;
  7679. uint32_t tmp;
  7680. tmp = I915_READ(PF_CTL(crtc->pipe));
  7681. if (tmp & PF_ENABLE) {
  7682. pipe_config->pch_pfit.enabled = true;
  7683. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7684. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7685. /* We currently do not free assignements of panel fitters on
  7686. * ivb/hsw (since we don't use the higher upscaling modes which
  7687. * differentiates them) so just WARN about this case for now. */
  7688. if (IS_GEN7(dev)) {
  7689. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7690. PF_PIPE_SEL_IVB(crtc->pipe));
  7691. }
  7692. }
  7693. }
  7694. static void
  7695. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7696. struct intel_initial_plane_config *plane_config)
  7697. {
  7698. struct drm_device *dev = crtc->base.dev;
  7699. struct drm_i915_private *dev_priv = dev->dev_private;
  7700. u32 val, base, offset;
  7701. int pipe = crtc->pipe;
  7702. int fourcc, pixel_format;
  7703. unsigned int aligned_height;
  7704. struct drm_framebuffer *fb;
  7705. struct intel_framebuffer *intel_fb;
  7706. val = I915_READ(DSPCNTR(pipe));
  7707. if (!(val & DISPLAY_PLANE_ENABLE))
  7708. return;
  7709. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7710. if (!intel_fb) {
  7711. DRM_DEBUG_KMS("failed to alloc fb\n");
  7712. return;
  7713. }
  7714. fb = &intel_fb->base;
  7715. if (INTEL_INFO(dev)->gen >= 4) {
  7716. if (val & DISPPLANE_TILED) {
  7717. plane_config->tiling = I915_TILING_X;
  7718. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7719. }
  7720. }
  7721. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7722. fourcc = i9xx_format_to_fourcc(pixel_format);
  7723. fb->pixel_format = fourcc;
  7724. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7725. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7726. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7727. offset = I915_READ(DSPOFFSET(pipe));
  7728. } else {
  7729. if (plane_config->tiling)
  7730. offset = I915_READ(DSPTILEOFF(pipe));
  7731. else
  7732. offset = I915_READ(DSPLINOFF(pipe));
  7733. }
  7734. plane_config->base = base;
  7735. val = I915_READ(PIPESRC(pipe));
  7736. fb->width = ((val >> 16) & 0xfff) + 1;
  7737. fb->height = ((val >> 0) & 0xfff) + 1;
  7738. val = I915_READ(DSPSTRIDE(pipe));
  7739. fb->pitches[0] = val & 0xffffffc0;
  7740. aligned_height = intel_fb_align_height(dev, fb->height,
  7741. fb->pixel_format,
  7742. fb->modifier[0]);
  7743. plane_config->size = fb->pitches[0] * aligned_height;
  7744. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7745. pipe_name(pipe), fb->width, fb->height,
  7746. fb->bits_per_pixel, base, fb->pitches[0],
  7747. plane_config->size);
  7748. plane_config->fb = intel_fb;
  7749. }
  7750. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7751. struct intel_crtc_state *pipe_config)
  7752. {
  7753. struct drm_device *dev = crtc->base.dev;
  7754. struct drm_i915_private *dev_priv = dev->dev_private;
  7755. enum intel_display_power_domain power_domain;
  7756. uint32_t tmp;
  7757. bool ret;
  7758. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7759. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7760. return false;
  7761. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7762. pipe_config->shared_dpll = NULL;
  7763. ret = false;
  7764. tmp = I915_READ(PIPECONF(crtc->pipe));
  7765. if (!(tmp & PIPECONF_ENABLE))
  7766. goto out;
  7767. switch (tmp & PIPECONF_BPC_MASK) {
  7768. case PIPECONF_6BPC:
  7769. pipe_config->pipe_bpp = 18;
  7770. break;
  7771. case PIPECONF_8BPC:
  7772. pipe_config->pipe_bpp = 24;
  7773. break;
  7774. case PIPECONF_10BPC:
  7775. pipe_config->pipe_bpp = 30;
  7776. break;
  7777. case PIPECONF_12BPC:
  7778. pipe_config->pipe_bpp = 36;
  7779. break;
  7780. default:
  7781. break;
  7782. }
  7783. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7784. pipe_config->limited_color_range = true;
  7785. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7786. struct intel_shared_dpll *pll;
  7787. enum intel_dpll_id pll_id;
  7788. pipe_config->has_pch_encoder = true;
  7789. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7790. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7791. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7792. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7793. if (HAS_PCH_IBX(dev_priv)) {
  7794. /*
  7795. * The pipe->pch transcoder and pch transcoder->pll
  7796. * mapping is fixed.
  7797. */
  7798. pll_id = (enum intel_dpll_id) crtc->pipe;
  7799. } else {
  7800. tmp = I915_READ(PCH_DPLL_SEL);
  7801. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7802. pll_id = DPLL_ID_PCH_PLL_B;
  7803. else
  7804. pll_id= DPLL_ID_PCH_PLL_A;
  7805. }
  7806. pipe_config->shared_dpll =
  7807. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7808. pll = pipe_config->shared_dpll;
  7809. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7810. &pipe_config->dpll_hw_state));
  7811. tmp = pipe_config->dpll_hw_state.dpll;
  7812. pipe_config->pixel_multiplier =
  7813. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7814. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7815. ironlake_pch_clock_get(crtc, pipe_config);
  7816. } else {
  7817. pipe_config->pixel_multiplier = 1;
  7818. }
  7819. intel_get_pipe_timings(crtc, pipe_config);
  7820. intel_get_pipe_src_size(crtc, pipe_config);
  7821. ironlake_get_pfit_config(crtc, pipe_config);
  7822. ret = true;
  7823. out:
  7824. intel_display_power_put(dev_priv, power_domain);
  7825. return ret;
  7826. }
  7827. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7828. {
  7829. struct drm_device *dev = dev_priv->dev;
  7830. struct intel_crtc *crtc;
  7831. for_each_intel_crtc(dev, crtc)
  7832. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7833. pipe_name(crtc->pipe));
  7834. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7835. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7836. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7837. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7838. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7839. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7840. "CPU PWM1 enabled\n");
  7841. if (IS_HASWELL(dev))
  7842. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7843. "CPU PWM2 enabled\n");
  7844. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7845. "PCH PWM1 enabled\n");
  7846. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7847. "Utility pin enabled\n");
  7848. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7849. /*
  7850. * In theory we can still leave IRQs enabled, as long as only the HPD
  7851. * interrupts remain enabled. We used to check for that, but since it's
  7852. * gen-specific and since we only disable LCPLL after we fully disable
  7853. * the interrupts, the check below should be enough.
  7854. */
  7855. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7856. }
  7857. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7858. {
  7859. struct drm_device *dev = dev_priv->dev;
  7860. if (IS_HASWELL(dev))
  7861. return I915_READ(D_COMP_HSW);
  7862. else
  7863. return I915_READ(D_COMP_BDW);
  7864. }
  7865. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7866. {
  7867. struct drm_device *dev = dev_priv->dev;
  7868. if (IS_HASWELL(dev)) {
  7869. mutex_lock(&dev_priv->rps.hw_lock);
  7870. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7871. val))
  7872. DRM_ERROR("Failed to write to D_COMP\n");
  7873. mutex_unlock(&dev_priv->rps.hw_lock);
  7874. } else {
  7875. I915_WRITE(D_COMP_BDW, val);
  7876. POSTING_READ(D_COMP_BDW);
  7877. }
  7878. }
  7879. /*
  7880. * This function implements pieces of two sequences from BSpec:
  7881. * - Sequence for display software to disable LCPLL
  7882. * - Sequence for display software to allow package C8+
  7883. * The steps implemented here are just the steps that actually touch the LCPLL
  7884. * register. Callers should take care of disabling all the display engine
  7885. * functions, doing the mode unset, fixing interrupts, etc.
  7886. */
  7887. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7888. bool switch_to_fclk, bool allow_power_down)
  7889. {
  7890. uint32_t val;
  7891. assert_can_disable_lcpll(dev_priv);
  7892. val = I915_READ(LCPLL_CTL);
  7893. if (switch_to_fclk) {
  7894. val |= LCPLL_CD_SOURCE_FCLK;
  7895. I915_WRITE(LCPLL_CTL, val);
  7896. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7897. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7898. DRM_ERROR("Switching to FCLK failed\n");
  7899. val = I915_READ(LCPLL_CTL);
  7900. }
  7901. val |= LCPLL_PLL_DISABLE;
  7902. I915_WRITE(LCPLL_CTL, val);
  7903. POSTING_READ(LCPLL_CTL);
  7904. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7905. DRM_ERROR("LCPLL still locked\n");
  7906. val = hsw_read_dcomp(dev_priv);
  7907. val |= D_COMP_COMP_DISABLE;
  7908. hsw_write_dcomp(dev_priv, val);
  7909. ndelay(100);
  7910. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7911. 1))
  7912. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7913. if (allow_power_down) {
  7914. val = I915_READ(LCPLL_CTL);
  7915. val |= LCPLL_POWER_DOWN_ALLOW;
  7916. I915_WRITE(LCPLL_CTL, val);
  7917. POSTING_READ(LCPLL_CTL);
  7918. }
  7919. }
  7920. /*
  7921. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7922. * source.
  7923. */
  7924. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7925. {
  7926. uint32_t val;
  7927. val = I915_READ(LCPLL_CTL);
  7928. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7929. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7930. return;
  7931. /*
  7932. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7933. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7934. */
  7935. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7936. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7937. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7938. I915_WRITE(LCPLL_CTL, val);
  7939. POSTING_READ(LCPLL_CTL);
  7940. }
  7941. val = hsw_read_dcomp(dev_priv);
  7942. val |= D_COMP_COMP_FORCE;
  7943. val &= ~D_COMP_COMP_DISABLE;
  7944. hsw_write_dcomp(dev_priv, val);
  7945. val = I915_READ(LCPLL_CTL);
  7946. val &= ~LCPLL_PLL_DISABLE;
  7947. I915_WRITE(LCPLL_CTL, val);
  7948. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7949. DRM_ERROR("LCPLL not locked yet\n");
  7950. if (val & LCPLL_CD_SOURCE_FCLK) {
  7951. val = I915_READ(LCPLL_CTL);
  7952. val &= ~LCPLL_CD_SOURCE_FCLK;
  7953. I915_WRITE(LCPLL_CTL, val);
  7954. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7955. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7956. DRM_ERROR("Switching back to LCPLL failed\n");
  7957. }
  7958. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7959. intel_update_cdclk(dev_priv->dev);
  7960. }
  7961. /*
  7962. * Package states C8 and deeper are really deep PC states that can only be
  7963. * reached when all the devices on the system allow it, so even if the graphics
  7964. * device allows PC8+, it doesn't mean the system will actually get to these
  7965. * states. Our driver only allows PC8+ when going into runtime PM.
  7966. *
  7967. * The requirements for PC8+ are that all the outputs are disabled, the power
  7968. * well is disabled and most interrupts are disabled, and these are also
  7969. * requirements for runtime PM. When these conditions are met, we manually do
  7970. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7971. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7972. * hang the machine.
  7973. *
  7974. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7975. * the state of some registers, so when we come back from PC8+ we need to
  7976. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7977. * need to take care of the registers kept by RC6. Notice that this happens even
  7978. * if we don't put the device in PCI D3 state (which is what currently happens
  7979. * because of the runtime PM support).
  7980. *
  7981. * For more, read "Display Sequences for Package C8" on the hardware
  7982. * documentation.
  7983. */
  7984. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7985. {
  7986. struct drm_device *dev = dev_priv->dev;
  7987. uint32_t val;
  7988. DRM_DEBUG_KMS("Enabling package C8+\n");
  7989. if (HAS_PCH_LPT_LP(dev)) {
  7990. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7991. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7992. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7993. }
  7994. lpt_disable_clkout_dp(dev);
  7995. hsw_disable_lcpll(dev_priv, true, true);
  7996. }
  7997. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7998. {
  7999. struct drm_device *dev = dev_priv->dev;
  8000. uint32_t val;
  8001. DRM_DEBUG_KMS("Disabling package C8+\n");
  8002. hsw_restore_lcpll(dev_priv);
  8003. lpt_init_pch_refclk(dev);
  8004. if (HAS_PCH_LPT_LP(dev)) {
  8005. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8006. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8007. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8008. }
  8009. }
  8010. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8011. {
  8012. struct drm_device *dev = old_state->dev;
  8013. struct intel_atomic_state *old_intel_state =
  8014. to_intel_atomic_state(old_state);
  8015. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8016. broxton_set_cdclk(to_i915(dev), req_cdclk);
  8017. }
  8018. /* compute the max rate for new configuration */
  8019. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8020. {
  8021. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8022. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8023. struct drm_crtc *crtc;
  8024. struct drm_crtc_state *cstate;
  8025. struct intel_crtc_state *crtc_state;
  8026. unsigned max_pixel_rate = 0, i;
  8027. enum pipe pipe;
  8028. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8029. sizeof(intel_state->min_pixclk));
  8030. for_each_crtc_in_state(state, crtc, cstate, i) {
  8031. int pixel_rate;
  8032. crtc_state = to_intel_crtc_state(cstate);
  8033. if (!crtc_state->base.enable) {
  8034. intel_state->min_pixclk[i] = 0;
  8035. continue;
  8036. }
  8037. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8038. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8039. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8040. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8041. intel_state->min_pixclk[i] = pixel_rate;
  8042. }
  8043. for_each_pipe(dev_priv, pipe)
  8044. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8045. return max_pixel_rate;
  8046. }
  8047. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8048. {
  8049. struct drm_i915_private *dev_priv = dev->dev_private;
  8050. uint32_t val, data;
  8051. int ret;
  8052. if (WARN((I915_READ(LCPLL_CTL) &
  8053. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8054. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8055. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8056. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8057. "trying to change cdclk frequency with cdclk not enabled\n"))
  8058. return;
  8059. mutex_lock(&dev_priv->rps.hw_lock);
  8060. ret = sandybridge_pcode_write(dev_priv,
  8061. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8062. mutex_unlock(&dev_priv->rps.hw_lock);
  8063. if (ret) {
  8064. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8065. return;
  8066. }
  8067. val = I915_READ(LCPLL_CTL);
  8068. val |= LCPLL_CD_SOURCE_FCLK;
  8069. I915_WRITE(LCPLL_CTL, val);
  8070. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8071. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8072. DRM_ERROR("Switching to FCLK failed\n");
  8073. val = I915_READ(LCPLL_CTL);
  8074. val &= ~LCPLL_CLK_FREQ_MASK;
  8075. switch (cdclk) {
  8076. case 450000:
  8077. val |= LCPLL_CLK_FREQ_450;
  8078. data = 0;
  8079. break;
  8080. case 540000:
  8081. val |= LCPLL_CLK_FREQ_54O_BDW;
  8082. data = 1;
  8083. break;
  8084. case 337500:
  8085. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8086. data = 2;
  8087. break;
  8088. case 675000:
  8089. val |= LCPLL_CLK_FREQ_675_BDW;
  8090. data = 3;
  8091. break;
  8092. default:
  8093. WARN(1, "invalid cdclk frequency\n");
  8094. return;
  8095. }
  8096. I915_WRITE(LCPLL_CTL, val);
  8097. val = I915_READ(LCPLL_CTL);
  8098. val &= ~LCPLL_CD_SOURCE_FCLK;
  8099. I915_WRITE(LCPLL_CTL, val);
  8100. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8101. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8102. DRM_ERROR("Switching back to LCPLL failed\n");
  8103. mutex_lock(&dev_priv->rps.hw_lock);
  8104. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8105. mutex_unlock(&dev_priv->rps.hw_lock);
  8106. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8107. intel_update_cdclk(dev);
  8108. WARN(cdclk != dev_priv->cdclk_freq,
  8109. "cdclk requested %d kHz but got %d kHz\n",
  8110. cdclk, dev_priv->cdclk_freq);
  8111. }
  8112. static int broadwell_calc_cdclk(int max_pixclk)
  8113. {
  8114. if (max_pixclk > 540000)
  8115. return 675000;
  8116. else if (max_pixclk > 450000)
  8117. return 540000;
  8118. else if (max_pixclk > 337500)
  8119. return 450000;
  8120. else
  8121. return 337500;
  8122. }
  8123. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8124. {
  8125. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8126. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8127. int max_pixclk = ilk_max_pixel_rate(state);
  8128. int cdclk;
  8129. /*
  8130. * FIXME should also account for plane ratio
  8131. * once 64bpp pixel formats are supported.
  8132. */
  8133. cdclk = broadwell_calc_cdclk(max_pixclk);
  8134. if (cdclk > dev_priv->max_cdclk_freq) {
  8135. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8136. cdclk, dev_priv->max_cdclk_freq);
  8137. return -EINVAL;
  8138. }
  8139. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8140. if (!intel_state->active_crtcs)
  8141. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8142. return 0;
  8143. }
  8144. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8145. {
  8146. struct drm_device *dev = old_state->dev;
  8147. struct intel_atomic_state *old_intel_state =
  8148. to_intel_atomic_state(old_state);
  8149. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8150. broadwell_set_cdclk(dev, req_cdclk);
  8151. }
  8152. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8153. struct intel_crtc_state *crtc_state)
  8154. {
  8155. struct intel_encoder *intel_encoder =
  8156. intel_ddi_get_crtc_new_encoder(crtc_state);
  8157. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8158. if (!intel_ddi_pll_select(crtc, crtc_state))
  8159. return -EINVAL;
  8160. }
  8161. crtc->lowfreq_avail = false;
  8162. return 0;
  8163. }
  8164. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8165. enum port port,
  8166. struct intel_crtc_state *pipe_config)
  8167. {
  8168. enum intel_dpll_id id;
  8169. switch (port) {
  8170. case PORT_A:
  8171. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8172. id = DPLL_ID_SKL_DPLL0;
  8173. break;
  8174. case PORT_B:
  8175. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8176. id = DPLL_ID_SKL_DPLL1;
  8177. break;
  8178. case PORT_C:
  8179. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8180. id = DPLL_ID_SKL_DPLL2;
  8181. break;
  8182. default:
  8183. DRM_ERROR("Incorrect port type\n");
  8184. return;
  8185. }
  8186. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8187. }
  8188. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8189. enum port port,
  8190. struct intel_crtc_state *pipe_config)
  8191. {
  8192. enum intel_dpll_id id;
  8193. u32 temp;
  8194. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8195. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8196. switch (pipe_config->ddi_pll_sel) {
  8197. case SKL_DPLL0:
  8198. id = DPLL_ID_SKL_DPLL0;
  8199. break;
  8200. case SKL_DPLL1:
  8201. id = DPLL_ID_SKL_DPLL1;
  8202. break;
  8203. case SKL_DPLL2:
  8204. id = DPLL_ID_SKL_DPLL2;
  8205. break;
  8206. case SKL_DPLL3:
  8207. id = DPLL_ID_SKL_DPLL3;
  8208. break;
  8209. default:
  8210. MISSING_CASE(pipe_config->ddi_pll_sel);
  8211. return;
  8212. }
  8213. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8214. }
  8215. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8216. enum port port,
  8217. struct intel_crtc_state *pipe_config)
  8218. {
  8219. enum intel_dpll_id id;
  8220. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8221. switch (pipe_config->ddi_pll_sel) {
  8222. case PORT_CLK_SEL_WRPLL1:
  8223. id = DPLL_ID_WRPLL1;
  8224. break;
  8225. case PORT_CLK_SEL_WRPLL2:
  8226. id = DPLL_ID_WRPLL2;
  8227. break;
  8228. case PORT_CLK_SEL_SPLL:
  8229. id = DPLL_ID_SPLL;
  8230. break;
  8231. case PORT_CLK_SEL_LCPLL_810:
  8232. id = DPLL_ID_LCPLL_810;
  8233. break;
  8234. case PORT_CLK_SEL_LCPLL_1350:
  8235. id = DPLL_ID_LCPLL_1350;
  8236. break;
  8237. case PORT_CLK_SEL_LCPLL_2700:
  8238. id = DPLL_ID_LCPLL_2700;
  8239. break;
  8240. default:
  8241. MISSING_CASE(pipe_config->ddi_pll_sel);
  8242. /* fall through */
  8243. case PORT_CLK_SEL_NONE:
  8244. return;
  8245. }
  8246. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8247. }
  8248. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8249. struct intel_crtc_state *pipe_config,
  8250. unsigned long *power_domain_mask)
  8251. {
  8252. struct drm_device *dev = crtc->base.dev;
  8253. struct drm_i915_private *dev_priv = dev->dev_private;
  8254. enum intel_display_power_domain power_domain;
  8255. u32 tmp;
  8256. /*
  8257. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8258. * transcoder handled below.
  8259. */
  8260. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8261. /*
  8262. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8263. * consistency and less surprising code; it's in always on power).
  8264. */
  8265. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8266. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8267. enum pipe trans_edp_pipe;
  8268. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8269. default:
  8270. WARN(1, "unknown pipe linked to edp transcoder\n");
  8271. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8272. case TRANS_DDI_EDP_INPUT_A_ON:
  8273. trans_edp_pipe = PIPE_A;
  8274. break;
  8275. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8276. trans_edp_pipe = PIPE_B;
  8277. break;
  8278. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8279. trans_edp_pipe = PIPE_C;
  8280. break;
  8281. }
  8282. if (trans_edp_pipe == crtc->pipe)
  8283. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8284. }
  8285. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8286. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8287. return false;
  8288. *power_domain_mask |= BIT(power_domain);
  8289. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8290. return tmp & PIPECONF_ENABLE;
  8291. }
  8292. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8293. struct intel_crtc_state *pipe_config,
  8294. unsigned long *power_domain_mask)
  8295. {
  8296. struct drm_device *dev = crtc->base.dev;
  8297. struct drm_i915_private *dev_priv = dev->dev_private;
  8298. enum intel_display_power_domain power_domain;
  8299. enum port port;
  8300. enum transcoder cpu_transcoder;
  8301. u32 tmp;
  8302. pipe_config->has_dsi_encoder = false;
  8303. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8304. if (port == PORT_A)
  8305. cpu_transcoder = TRANSCODER_DSI_A;
  8306. else
  8307. cpu_transcoder = TRANSCODER_DSI_C;
  8308. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8309. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8310. continue;
  8311. *power_domain_mask |= BIT(power_domain);
  8312. /*
  8313. * The PLL needs to be enabled with a valid divider
  8314. * configuration, otherwise accessing DSI registers will hang
  8315. * the machine. See BSpec North Display Engine
  8316. * registers/MIPI[BXT]. We can break out here early, since we
  8317. * need the same DSI PLL to be enabled for both DSI ports.
  8318. */
  8319. if (!intel_dsi_pll_is_enabled(dev_priv))
  8320. break;
  8321. /* XXX: this works for video mode only */
  8322. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8323. if (!(tmp & DPI_ENABLE))
  8324. continue;
  8325. tmp = I915_READ(MIPI_CTRL(port));
  8326. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8327. continue;
  8328. pipe_config->cpu_transcoder = cpu_transcoder;
  8329. pipe_config->has_dsi_encoder = true;
  8330. break;
  8331. }
  8332. return pipe_config->has_dsi_encoder;
  8333. }
  8334. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8335. struct intel_crtc_state *pipe_config)
  8336. {
  8337. struct drm_device *dev = crtc->base.dev;
  8338. struct drm_i915_private *dev_priv = dev->dev_private;
  8339. struct intel_shared_dpll *pll;
  8340. enum port port;
  8341. uint32_t tmp;
  8342. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8343. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8344. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8345. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8346. else if (IS_BROXTON(dev))
  8347. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8348. else
  8349. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8350. pll = pipe_config->shared_dpll;
  8351. if (pll) {
  8352. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8353. &pipe_config->dpll_hw_state));
  8354. }
  8355. /*
  8356. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8357. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8358. * the PCH transcoder is on.
  8359. */
  8360. if (INTEL_INFO(dev)->gen < 9 &&
  8361. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8362. pipe_config->has_pch_encoder = true;
  8363. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8364. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8365. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8366. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8367. }
  8368. }
  8369. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8370. struct intel_crtc_state *pipe_config)
  8371. {
  8372. struct drm_device *dev = crtc->base.dev;
  8373. struct drm_i915_private *dev_priv = dev->dev_private;
  8374. enum intel_display_power_domain power_domain;
  8375. unsigned long power_domain_mask;
  8376. bool active;
  8377. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8378. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8379. return false;
  8380. power_domain_mask = BIT(power_domain);
  8381. pipe_config->shared_dpll = NULL;
  8382. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8383. if (IS_BROXTON(dev_priv)) {
  8384. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8385. &power_domain_mask);
  8386. WARN_ON(active && pipe_config->has_dsi_encoder);
  8387. if (pipe_config->has_dsi_encoder)
  8388. active = true;
  8389. }
  8390. if (!active)
  8391. goto out;
  8392. if (!pipe_config->has_dsi_encoder) {
  8393. haswell_get_ddi_port_state(crtc, pipe_config);
  8394. intel_get_pipe_timings(crtc, pipe_config);
  8395. }
  8396. intel_get_pipe_src_size(crtc, pipe_config);
  8397. pipe_config->gamma_mode =
  8398. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8399. if (INTEL_INFO(dev)->gen >= 9) {
  8400. skl_init_scalers(dev, crtc, pipe_config);
  8401. }
  8402. if (INTEL_INFO(dev)->gen >= 9) {
  8403. pipe_config->scaler_state.scaler_id = -1;
  8404. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8405. }
  8406. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8407. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8408. power_domain_mask |= BIT(power_domain);
  8409. if (INTEL_INFO(dev)->gen >= 9)
  8410. skylake_get_pfit_config(crtc, pipe_config);
  8411. else
  8412. ironlake_get_pfit_config(crtc, pipe_config);
  8413. }
  8414. if (IS_HASWELL(dev))
  8415. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8416. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8417. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8418. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8419. pipe_config->pixel_multiplier =
  8420. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8421. } else {
  8422. pipe_config->pixel_multiplier = 1;
  8423. }
  8424. out:
  8425. for_each_power_domain(power_domain, power_domain_mask)
  8426. intel_display_power_put(dev_priv, power_domain);
  8427. return active;
  8428. }
  8429. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8430. const struct intel_plane_state *plane_state)
  8431. {
  8432. struct drm_device *dev = crtc->dev;
  8433. struct drm_i915_private *dev_priv = dev->dev_private;
  8434. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8435. uint32_t cntl = 0, size = 0;
  8436. if (plane_state && plane_state->visible) {
  8437. unsigned int width = plane_state->base.crtc_w;
  8438. unsigned int height = plane_state->base.crtc_h;
  8439. unsigned int stride = roundup_pow_of_two(width) * 4;
  8440. switch (stride) {
  8441. default:
  8442. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8443. width, stride);
  8444. stride = 256;
  8445. /* fallthrough */
  8446. case 256:
  8447. case 512:
  8448. case 1024:
  8449. case 2048:
  8450. break;
  8451. }
  8452. cntl |= CURSOR_ENABLE |
  8453. CURSOR_GAMMA_ENABLE |
  8454. CURSOR_FORMAT_ARGB |
  8455. CURSOR_STRIDE(stride);
  8456. size = (height << 12) | width;
  8457. }
  8458. if (intel_crtc->cursor_cntl != 0 &&
  8459. (intel_crtc->cursor_base != base ||
  8460. intel_crtc->cursor_size != size ||
  8461. intel_crtc->cursor_cntl != cntl)) {
  8462. /* On these chipsets we can only modify the base/size/stride
  8463. * whilst the cursor is disabled.
  8464. */
  8465. I915_WRITE(CURCNTR(PIPE_A), 0);
  8466. POSTING_READ(CURCNTR(PIPE_A));
  8467. intel_crtc->cursor_cntl = 0;
  8468. }
  8469. if (intel_crtc->cursor_base != base) {
  8470. I915_WRITE(CURBASE(PIPE_A), base);
  8471. intel_crtc->cursor_base = base;
  8472. }
  8473. if (intel_crtc->cursor_size != size) {
  8474. I915_WRITE(CURSIZE, size);
  8475. intel_crtc->cursor_size = size;
  8476. }
  8477. if (intel_crtc->cursor_cntl != cntl) {
  8478. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8479. POSTING_READ(CURCNTR(PIPE_A));
  8480. intel_crtc->cursor_cntl = cntl;
  8481. }
  8482. }
  8483. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8484. const struct intel_plane_state *plane_state)
  8485. {
  8486. struct drm_device *dev = crtc->dev;
  8487. struct drm_i915_private *dev_priv = dev->dev_private;
  8488. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8489. int pipe = intel_crtc->pipe;
  8490. uint32_t cntl = 0;
  8491. if (plane_state && plane_state->visible) {
  8492. cntl = MCURSOR_GAMMA_ENABLE;
  8493. switch (plane_state->base.crtc_w) {
  8494. case 64:
  8495. cntl |= CURSOR_MODE_64_ARGB_AX;
  8496. break;
  8497. case 128:
  8498. cntl |= CURSOR_MODE_128_ARGB_AX;
  8499. break;
  8500. case 256:
  8501. cntl |= CURSOR_MODE_256_ARGB_AX;
  8502. break;
  8503. default:
  8504. MISSING_CASE(plane_state->base.crtc_w);
  8505. return;
  8506. }
  8507. cntl |= pipe << 28; /* Connect to correct pipe */
  8508. if (HAS_DDI(dev))
  8509. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8510. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8511. cntl |= CURSOR_ROTATE_180;
  8512. }
  8513. if (intel_crtc->cursor_cntl != cntl) {
  8514. I915_WRITE(CURCNTR(pipe), cntl);
  8515. POSTING_READ(CURCNTR(pipe));
  8516. intel_crtc->cursor_cntl = cntl;
  8517. }
  8518. /* and commit changes on next vblank */
  8519. I915_WRITE(CURBASE(pipe), base);
  8520. POSTING_READ(CURBASE(pipe));
  8521. intel_crtc->cursor_base = base;
  8522. }
  8523. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8524. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8525. const struct intel_plane_state *plane_state)
  8526. {
  8527. struct drm_device *dev = crtc->dev;
  8528. struct drm_i915_private *dev_priv = dev->dev_private;
  8529. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8530. int pipe = intel_crtc->pipe;
  8531. u32 base = intel_crtc->cursor_addr;
  8532. u32 pos = 0;
  8533. if (plane_state) {
  8534. int x = plane_state->base.crtc_x;
  8535. int y = plane_state->base.crtc_y;
  8536. if (x < 0) {
  8537. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8538. x = -x;
  8539. }
  8540. pos |= x << CURSOR_X_SHIFT;
  8541. if (y < 0) {
  8542. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8543. y = -y;
  8544. }
  8545. pos |= y << CURSOR_Y_SHIFT;
  8546. /* ILK+ do this automagically */
  8547. if (HAS_GMCH_DISPLAY(dev) &&
  8548. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8549. base += (plane_state->base.crtc_h *
  8550. plane_state->base.crtc_w - 1) * 4;
  8551. }
  8552. }
  8553. I915_WRITE(CURPOS(pipe), pos);
  8554. if (IS_845G(dev) || IS_I865G(dev))
  8555. i845_update_cursor(crtc, base, plane_state);
  8556. else
  8557. i9xx_update_cursor(crtc, base, plane_state);
  8558. }
  8559. static bool cursor_size_ok(struct drm_device *dev,
  8560. uint32_t width, uint32_t height)
  8561. {
  8562. if (width == 0 || height == 0)
  8563. return false;
  8564. /*
  8565. * 845g/865g are special in that they are only limited by
  8566. * the width of their cursors, the height is arbitrary up to
  8567. * the precision of the register. Everything else requires
  8568. * square cursors, limited to a few power-of-two sizes.
  8569. */
  8570. if (IS_845G(dev) || IS_I865G(dev)) {
  8571. if ((width & 63) != 0)
  8572. return false;
  8573. if (width > (IS_845G(dev) ? 64 : 512))
  8574. return false;
  8575. if (height > 1023)
  8576. return false;
  8577. } else {
  8578. switch (width | height) {
  8579. case 256:
  8580. case 128:
  8581. if (IS_GEN2(dev))
  8582. return false;
  8583. case 64:
  8584. break;
  8585. default:
  8586. return false;
  8587. }
  8588. }
  8589. return true;
  8590. }
  8591. /* VESA 640x480x72Hz mode to set on the pipe */
  8592. static struct drm_display_mode load_detect_mode = {
  8593. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8594. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8595. };
  8596. struct drm_framebuffer *
  8597. __intel_framebuffer_create(struct drm_device *dev,
  8598. struct drm_mode_fb_cmd2 *mode_cmd,
  8599. struct drm_i915_gem_object *obj)
  8600. {
  8601. struct intel_framebuffer *intel_fb;
  8602. int ret;
  8603. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8604. if (!intel_fb)
  8605. return ERR_PTR(-ENOMEM);
  8606. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8607. if (ret)
  8608. goto err;
  8609. return &intel_fb->base;
  8610. err:
  8611. kfree(intel_fb);
  8612. return ERR_PTR(ret);
  8613. }
  8614. static struct drm_framebuffer *
  8615. intel_framebuffer_create(struct drm_device *dev,
  8616. struct drm_mode_fb_cmd2 *mode_cmd,
  8617. struct drm_i915_gem_object *obj)
  8618. {
  8619. struct drm_framebuffer *fb;
  8620. int ret;
  8621. ret = i915_mutex_lock_interruptible(dev);
  8622. if (ret)
  8623. return ERR_PTR(ret);
  8624. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8625. mutex_unlock(&dev->struct_mutex);
  8626. return fb;
  8627. }
  8628. static u32
  8629. intel_framebuffer_pitch_for_width(int width, int bpp)
  8630. {
  8631. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8632. return ALIGN(pitch, 64);
  8633. }
  8634. static u32
  8635. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8636. {
  8637. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8638. return PAGE_ALIGN(pitch * mode->vdisplay);
  8639. }
  8640. static struct drm_framebuffer *
  8641. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8642. struct drm_display_mode *mode,
  8643. int depth, int bpp)
  8644. {
  8645. struct drm_framebuffer *fb;
  8646. struct drm_i915_gem_object *obj;
  8647. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8648. obj = i915_gem_object_create(dev,
  8649. intel_framebuffer_size_for_mode(mode, bpp));
  8650. if (IS_ERR(obj))
  8651. return ERR_CAST(obj);
  8652. mode_cmd.width = mode->hdisplay;
  8653. mode_cmd.height = mode->vdisplay;
  8654. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8655. bpp);
  8656. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8657. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8658. if (IS_ERR(fb))
  8659. drm_gem_object_unreference_unlocked(&obj->base);
  8660. return fb;
  8661. }
  8662. static struct drm_framebuffer *
  8663. mode_fits_in_fbdev(struct drm_device *dev,
  8664. struct drm_display_mode *mode)
  8665. {
  8666. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8667. struct drm_i915_private *dev_priv = dev->dev_private;
  8668. struct drm_i915_gem_object *obj;
  8669. struct drm_framebuffer *fb;
  8670. if (!dev_priv->fbdev)
  8671. return NULL;
  8672. if (!dev_priv->fbdev->fb)
  8673. return NULL;
  8674. obj = dev_priv->fbdev->fb->obj;
  8675. BUG_ON(!obj);
  8676. fb = &dev_priv->fbdev->fb->base;
  8677. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8678. fb->bits_per_pixel))
  8679. return NULL;
  8680. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8681. return NULL;
  8682. drm_framebuffer_reference(fb);
  8683. return fb;
  8684. #else
  8685. return NULL;
  8686. #endif
  8687. }
  8688. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8689. struct drm_crtc *crtc,
  8690. struct drm_display_mode *mode,
  8691. struct drm_framebuffer *fb,
  8692. int x, int y)
  8693. {
  8694. struct drm_plane_state *plane_state;
  8695. int hdisplay, vdisplay;
  8696. int ret;
  8697. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8698. if (IS_ERR(plane_state))
  8699. return PTR_ERR(plane_state);
  8700. if (mode)
  8701. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8702. else
  8703. hdisplay = vdisplay = 0;
  8704. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8705. if (ret)
  8706. return ret;
  8707. drm_atomic_set_fb_for_plane(plane_state, fb);
  8708. plane_state->crtc_x = 0;
  8709. plane_state->crtc_y = 0;
  8710. plane_state->crtc_w = hdisplay;
  8711. plane_state->crtc_h = vdisplay;
  8712. plane_state->src_x = x << 16;
  8713. plane_state->src_y = y << 16;
  8714. plane_state->src_w = hdisplay << 16;
  8715. plane_state->src_h = vdisplay << 16;
  8716. return 0;
  8717. }
  8718. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8719. struct drm_display_mode *mode,
  8720. struct intel_load_detect_pipe *old,
  8721. struct drm_modeset_acquire_ctx *ctx)
  8722. {
  8723. struct intel_crtc *intel_crtc;
  8724. struct intel_encoder *intel_encoder =
  8725. intel_attached_encoder(connector);
  8726. struct drm_crtc *possible_crtc;
  8727. struct drm_encoder *encoder = &intel_encoder->base;
  8728. struct drm_crtc *crtc = NULL;
  8729. struct drm_device *dev = encoder->dev;
  8730. struct drm_framebuffer *fb;
  8731. struct drm_mode_config *config = &dev->mode_config;
  8732. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8733. struct drm_connector_state *connector_state;
  8734. struct intel_crtc_state *crtc_state;
  8735. int ret, i = -1;
  8736. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8737. connector->base.id, connector->name,
  8738. encoder->base.id, encoder->name);
  8739. old->restore_state = NULL;
  8740. retry:
  8741. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8742. if (ret)
  8743. goto fail;
  8744. /*
  8745. * Algorithm gets a little messy:
  8746. *
  8747. * - if the connector already has an assigned crtc, use it (but make
  8748. * sure it's on first)
  8749. *
  8750. * - try to find the first unused crtc that can drive this connector,
  8751. * and use that if we find one
  8752. */
  8753. /* See if we already have a CRTC for this connector */
  8754. if (connector->state->crtc) {
  8755. crtc = connector->state->crtc;
  8756. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8757. if (ret)
  8758. goto fail;
  8759. /* Make sure the crtc and connector are running */
  8760. goto found;
  8761. }
  8762. /* Find an unused one (if possible) */
  8763. for_each_crtc(dev, possible_crtc) {
  8764. i++;
  8765. if (!(encoder->possible_crtcs & (1 << i)))
  8766. continue;
  8767. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8768. if (ret)
  8769. goto fail;
  8770. if (possible_crtc->state->enable) {
  8771. drm_modeset_unlock(&possible_crtc->mutex);
  8772. continue;
  8773. }
  8774. crtc = possible_crtc;
  8775. break;
  8776. }
  8777. /*
  8778. * If we didn't find an unused CRTC, don't use any.
  8779. */
  8780. if (!crtc) {
  8781. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8782. goto fail;
  8783. }
  8784. found:
  8785. intel_crtc = to_intel_crtc(crtc);
  8786. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8787. if (ret)
  8788. goto fail;
  8789. state = drm_atomic_state_alloc(dev);
  8790. restore_state = drm_atomic_state_alloc(dev);
  8791. if (!state || !restore_state) {
  8792. ret = -ENOMEM;
  8793. goto fail;
  8794. }
  8795. state->acquire_ctx = ctx;
  8796. restore_state->acquire_ctx = ctx;
  8797. connector_state = drm_atomic_get_connector_state(state, connector);
  8798. if (IS_ERR(connector_state)) {
  8799. ret = PTR_ERR(connector_state);
  8800. goto fail;
  8801. }
  8802. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8803. if (ret)
  8804. goto fail;
  8805. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8806. if (IS_ERR(crtc_state)) {
  8807. ret = PTR_ERR(crtc_state);
  8808. goto fail;
  8809. }
  8810. crtc_state->base.active = crtc_state->base.enable = true;
  8811. if (!mode)
  8812. mode = &load_detect_mode;
  8813. /* We need a framebuffer large enough to accommodate all accesses
  8814. * that the plane may generate whilst we perform load detection.
  8815. * We can not rely on the fbcon either being present (we get called
  8816. * during its initialisation to detect all boot displays, or it may
  8817. * not even exist) or that it is large enough to satisfy the
  8818. * requested mode.
  8819. */
  8820. fb = mode_fits_in_fbdev(dev, mode);
  8821. if (fb == NULL) {
  8822. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8823. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8824. } else
  8825. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8826. if (IS_ERR(fb)) {
  8827. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8828. goto fail;
  8829. }
  8830. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8831. if (ret)
  8832. goto fail;
  8833. drm_framebuffer_unreference(fb);
  8834. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8835. if (ret)
  8836. goto fail;
  8837. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8838. if (!ret)
  8839. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8840. if (!ret)
  8841. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8842. if (ret) {
  8843. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8844. goto fail;
  8845. }
  8846. ret = drm_atomic_commit(state);
  8847. if (ret) {
  8848. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8849. goto fail;
  8850. }
  8851. old->restore_state = restore_state;
  8852. /* let the connector get through one full cycle before testing */
  8853. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8854. return true;
  8855. fail:
  8856. drm_atomic_state_free(state);
  8857. drm_atomic_state_free(restore_state);
  8858. restore_state = state = NULL;
  8859. if (ret == -EDEADLK) {
  8860. drm_modeset_backoff(ctx);
  8861. goto retry;
  8862. }
  8863. return false;
  8864. }
  8865. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8866. struct intel_load_detect_pipe *old,
  8867. struct drm_modeset_acquire_ctx *ctx)
  8868. {
  8869. struct intel_encoder *intel_encoder =
  8870. intel_attached_encoder(connector);
  8871. struct drm_encoder *encoder = &intel_encoder->base;
  8872. struct drm_atomic_state *state = old->restore_state;
  8873. int ret;
  8874. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8875. connector->base.id, connector->name,
  8876. encoder->base.id, encoder->name);
  8877. if (!state)
  8878. return;
  8879. ret = drm_atomic_commit(state);
  8880. if (ret) {
  8881. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8882. drm_atomic_state_free(state);
  8883. }
  8884. }
  8885. static int i9xx_pll_refclk(struct drm_device *dev,
  8886. const struct intel_crtc_state *pipe_config)
  8887. {
  8888. struct drm_i915_private *dev_priv = dev->dev_private;
  8889. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8890. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8891. return dev_priv->vbt.lvds_ssc_freq;
  8892. else if (HAS_PCH_SPLIT(dev))
  8893. return 120000;
  8894. else if (!IS_GEN2(dev))
  8895. return 96000;
  8896. else
  8897. return 48000;
  8898. }
  8899. /* Returns the clock of the currently programmed mode of the given pipe. */
  8900. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8901. struct intel_crtc_state *pipe_config)
  8902. {
  8903. struct drm_device *dev = crtc->base.dev;
  8904. struct drm_i915_private *dev_priv = dev->dev_private;
  8905. int pipe = pipe_config->cpu_transcoder;
  8906. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8907. u32 fp;
  8908. struct dpll clock;
  8909. int port_clock;
  8910. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8911. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8912. fp = pipe_config->dpll_hw_state.fp0;
  8913. else
  8914. fp = pipe_config->dpll_hw_state.fp1;
  8915. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8916. if (IS_PINEVIEW(dev)) {
  8917. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8918. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8919. } else {
  8920. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8921. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8922. }
  8923. if (!IS_GEN2(dev)) {
  8924. if (IS_PINEVIEW(dev))
  8925. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8926. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8927. else
  8928. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8929. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8930. switch (dpll & DPLL_MODE_MASK) {
  8931. case DPLLB_MODE_DAC_SERIAL:
  8932. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8933. 5 : 10;
  8934. break;
  8935. case DPLLB_MODE_LVDS:
  8936. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8937. 7 : 14;
  8938. break;
  8939. default:
  8940. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8941. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8942. return;
  8943. }
  8944. if (IS_PINEVIEW(dev))
  8945. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8946. else
  8947. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8948. } else {
  8949. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8950. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8951. if (is_lvds) {
  8952. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8953. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8954. if (lvds & LVDS_CLKB_POWER_UP)
  8955. clock.p2 = 7;
  8956. else
  8957. clock.p2 = 14;
  8958. } else {
  8959. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8960. clock.p1 = 2;
  8961. else {
  8962. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8963. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8964. }
  8965. if (dpll & PLL_P2_DIVIDE_BY_4)
  8966. clock.p2 = 4;
  8967. else
  8968. clock.p2 = 2;
  8969. }
  8970. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8971. }
  8972. /*
  8973. * This value includes pixel_multiplier. We will use
  8974. * port_clock to compute adjusted_mode.crtc_clock in the
  8975. * encoder's get_config() function.
  8976. */
  8977. pipe_config->port_clock = port_clock;
  8978. }
  8979. int intel_dotclock_calculate(int link_freq,
  8980. const struct intel_link_m_n *m_n)
  8981. {
  8982. /*
  8983. * The calculation for the data clock is:
  8984. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8985. * But we want to avoid losing precison if possible, so:
  8986. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8987. *
  8988. * and the link clock is simpler:
  8989. * link_clock = (m * link_clock) / n
  8990. */
  8991. if (!m_n->link_n)
  8992. return 0;
  8993. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8994. }
  8995. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8996. struct intel_crtc_state *pipe_config)
  8997. {
  8998. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8999. /* read out port_clock from the DPLL */
  9000. i9xx_crtc_clock_get(crtc, pipe_config);
  9001. /*
  9002. * In case there is an active pipe without active ports,
  9003. * we may need some idea for the dotclock anyway.
  9004. * Calculate one based on the FDI configuration.
  9005. */
  9006. pipe_config->base.adjusted_mode.crtc_clock =
  9007. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9008. &pipe_config->fdi_m_n);
  9009. }
  9010. /** Returns the currently programmed mode of the given pipe. */
  9011. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9012. struct drm_crtc *crtc)
  9013. {
  9014. struct drm_i915_private *dev_priv = dev->dev_private;
  9015. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9016. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9017. struct drm_display_mode *mode;
  9018. struct intel_crtc_state *pipe_config;
  9019. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9020. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9021. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9022. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9023. enum pipe pipe = intel_crtc->pipe;
  9024. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9025. if (!mode)
  9026. return NULL;
  9027. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9028. if (!pipe_config) {
  9029. kfree(mode);
  9030. return NULL;
  9031. }
  9032. /*
  9033. * Construct a pipe_config sufficient for getting the clock info
  9034. * back out of crtc_clock_get.
  9035. *
  9036. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9037. * to use a real value here instead.
  9038. */
  9039. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9040. pipe_config->pixel_multiplier = 1;
  9041. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9042. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9043. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9044. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9045. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9046. mode->hdisplay = (htot & 0xffff) + 1;
  9047. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9048. mode->hsync_start = (hsync & 0xffff) + 1;
  9049. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9050. mode->vdisplay = (vtot & 0xffff) + 1;
  9051. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9052. mode->vsync_start = (vsync & 0xffff) + 1;
  9053. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9054. drm_mode_set_name(mode);
  9055. kfree(pipe_config);
  9056. return mode;
  9057. }
  9058. void intel_mark_busy(struct drm_i915_private *dev_priv)
  9059. {
  9060. if (dev_priv->mm.busy)
  9061. return;
  9062. intel_runtime_pm_get(dev_priv);
  9063. i915_update_gfx_val(dev_priv);
  9064. if (INTEL_GEN(dev_priv) >= 6)
  9065. gen6_rps_busy(dev_priv);
  9066. dev_priv->mm.busy = true;
  9067. }
  9068. void intel_mark_idle(struct drm_i915_private *dev_priv)
  9069. {
  9070. if (!dev_priv->mm.busy)
  9071. return;
  9072. dev_priv->mm.busy = false;
  9073. if (INTEL_GEN(dev_priv) >= 6)
  9074. gen6_rps_idle(dev_priv);
  9075. intel_runtime_pm_put(dev_priv);
  9076. }
  9077. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9078. {
  9079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9080. struct drm_device *dev = crtc->dev;
  9081. struct intel_unpin_work *work;
  9082. spin_lock_irq(&dev->event_lock);
  9083. work = intel_crtc->unpin_work;
  9084. intel_crtc->unpin_work = NULL;
  9085. spin_unlock_irq(&dev->event_lock);
  9086. if (work) {
  9087. cancel_work_sync(&work->work);
  9088. kfree(work);
  9089. }
  9090. drm_crtc_cleanup(crtc);
  9091. kfree(intel_crtc);
  9092. }
  9093. static void intel_unpin_work_fn(struct work_struct *__work)
  9094. {
  9095. struct intel_unpin_work *work =
  9096. container_of(__work, struct intel_unpin_work, work);
  9097. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9098. struct drm_device *dev = crtc->base.dev;
  9099. struct drm_plane *primary = crtc->base.primary;
  9100. mutex_lock(&dev->struct_mutex);
  9101. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9102. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9103. if (work->flip_queued_req)
  9104. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9105. mutex_unlock(&dev->struct_mutex);
  9106. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9107. intel_fbc_post_update(crtc);
  9108. drm_framebuffer_unreference(work->old_fb);
  9109. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9110. atomic_dec(&crtc->unpin_work_count);
  9111. kfree(work);
  9112. }
  9113. static void do_intel_finish_page_flip(struct drm_i915_private *dev_priv,
  9114. struct drm_crtc *crtc)
  9115. {
  9116. struct drm_device *dev = dev_priv->dev;
  9117. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9118. struct intel_unpin_work *work;
  9119. unsigned long flags;
  9120. /* Ignore early vblank irqs */
  9121. if (intel_crtc == NULL)
  9122. return;
  9123. /*
  9124. * This is called both by irq handlers and the reset code (to complete
  9125. * lost pageflips) so needs the full irqsave spinlocks.
  9126. */
  9127. spin_lock_irqsave(&dev->event_lock, flags);
  9128. work = intel_crtc->unpin_work;
  9129. /* Ensure we don't miss a work->pending update ... */
  9130. smp_rmb();
  9131. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9132. spin_unlock_irqrestore(&dev->event_lock, flags);
  9133. return;
  9134. }
  9135. page_flip_completed(intel_crtc);
  9136. spin_unlock_irqrestore(&dev->event_lock, flags);
  9137. }
  9138. void intel_finish_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9139. {
  9140. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9141. do_intel_finish_page_flip(dev_priv, crtc);
  9142. }
  9143. void intel_finish_page_flip_plane(struct drm_i915_private *dev_priv, int plane)
  9144. {
  9145. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9146. do_intel_finish_page_flip(dev_priv, crtc);
  9147. }
  9148. /* Is 'a' after or equal to 'b'? */
  9149. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9150. {
  9151. return !((a - b) & 0x80000000);
  9152. }
  9153. static bool page_flip_finished(struct intel_crtc *crtc)
  9154. {
  9155. struct drm_device *dev = crtc->base.dev;
  9156. struct drm_i915_private *dev_priv = dev->dev_private;
  9157. unsigned reset_counter;
  9158. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9159. if (crtc->reset_counter != reset_counter)
  9160. return true;
  9161. /*
  9162. * The relevant registers doen't exist on pre-ctg.
  9163. * As the flip done interrupt doesn't trigger for mmio
  9164. * flips on gmch platforms, a flip count check isn't
  9165. * really needed there. But since ctg has the registers,
  9166. * include it in the check anyway.
  9167. */
  9168. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9169. return true;
  9170. /*
  9171. * BDW signals flip done immediately if the plane
  9172. * is disabled, even if the plane enable is already
  9173. * armed to occur at the next vblank :(
  9174. */
  9175. /*
  9176. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9177. * used the same base address. In that case the mmio flip might
  9178. * have completed, but the CS hasn't even executed the flip yet.
  9179. *
  9180. * A flip count check isn't enough as the CS might have updated
  9181. * the base address just after start of vblank, but before we
  9182. * managed to process the interrupt. This means we'd complete the
  9183. * CS flip too soon.
  9184. *
  9185. * Combining both checks should get us a good enough result. It may
  9186. * still happen that the CS flip has been executed, but has not
  9187. * yet actually completed. But in case the base address is the same
  9188. * anyway, we don't really care.
  9189. */
  9190. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9191. crtc->unpin_work->gtt_offset &&
  9192. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9193. crtc->unpin_work->flip_count);
  9194. }
  9195. void intel_prepare_page_flip(struct drm_i915_private *dev_priv, int plane)
  9196. {
  9197. struct drm_device *dev = dev_priv->dev;
  9198. struct intel_crtc *intel_crtc =
  9199. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9200. unsigned long flags;
  9201. /*
  9202. * This is called both by irq handlers and the reset code (to complete
  9203. * lost pageflips) so needs the full irqsave spinlocks.
  9204. *
  9205. * NB: An MMIO update of the plane base pointer will also
  9206. * generate a page-flip completion irq, i.e. every modeset
  9207. * is also accompanied by a spurious intel_prepare_page_flip().
  9208. */
  9209. spin_lock_irqsave(&dev->event_lock, flags);
  9210. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9211. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9212. spin_unlock_irqrestore(&dev->event_lock, flags);
  9213. }
  9214. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9215. {
  9216. /* Ensure that the work item is consistent when activating it ... */
  9217. smp_wmb();
  9218. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9219. /* and that it is marked active as soon as the irq could fire. */
  9220. smp_wmb();
  9221. }
  9222. static int intel_gen2_queue_flip(struct drm_device *dev,
  9223. struct drm_crtc *crtc,
  9224. struct drm_framebuffer *fb,
  9225. struct drm_i915_gem_object *obj,
  9226. struct drm_i915_gem_request *req,
  9227. uint32_t flags)
  9228. {
  9229. struct intel_engine_cs *engine = req->engine;
  9230. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9231. u32 flip_mask;
  9232. int ret;
  9233. ret = intel_ring_begin(req, 6);
  9234. if (ret)
  9235. return ret;
  9236. /* Can't queue multiple flips, so wait for the previous
  9237. * one to finish before executing the next.
  9238. */
  9239. if (intel_crtc->plane)
  9240. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9241. else
  9242. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9243. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9244. intel_ring_emit(engine, MI_NOOP);
  9245. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9246. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9247. intel_ring_emit(engine, fb->pitches[0]);
  9248. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9249. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9250. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9251. return 0;
  9252. }
  9253. static int intel_gen3_queue_flip(struct drm_device *dev,
  9254. struct drm_crtc *crtc,
  9255. struct drm_framebuffer *fb,
  9256. struct drm_i915_gem_object *obj,
  9257. struct drm_i915_gem_request *req,
  9258. uint32_t flags)
  9259. {
  9260. struct intel_engine_cs *engine = req->engine;
  9261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9262. u32 flip_mask;
  9263. int ret;
  9264. ret = intel_ring_begin(req, 6);
  9265. if (ret)
  9266. return ret;
  9267. if (intel_crtc->plane)
  9268. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9269. else
  9270. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9271. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9272. intel_ring_emit(engine, MI_NOOP);
  9273. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9274. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9275. intel_ring_emit(engine, fb->pitches[0]);
  9276. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9277. intel_ring_emit(engine, MI_NOOP);
  9278. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9279. return 0;
  9280. }
  9281. static int intel_gen4_queue_flip(struct drm_device *dev,
  9282. struct drm_crtc *crtc,
  9283. struct drm_framebuffer *fb,
  9284. struct drm_i915_gem_object *obj,
  9285. struct drm_i915_gem_request *req,
  9286. uint32_t flags)
  9287. {
  9288. struct intel_engine_cs *engine = req->engine;
  9289. struct drm_i915_private *dev_priv = dev->dev_private;
  9290. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9291. uint32_t pf, pipesrc;
  9292. int ret;
  9293. ret = intel_ring_begin(req, 4);
  9294. if (ret)
  9295. return ret;
  9296. /* i965+ uses the linear or tiled offsets from the
  9297. * Display Registers (which do not change across a page-flip)
  9298. * so we need only reprogram the base address.
  9299. */
  9300. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9301. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9302. intel_ring_emit(engine, fb->pitches[0]);
  9303. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset |
  9304. obj->tiling_mode);
  9305. /* XXX Enabling the panel-fitter across page-flip is so far
  9306. * untested on non-native modes, so ignore it for now.
  9307. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9308. */
  9309. pf = 0;
  9310. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9311. intel_ring_emit(engine, pf | pipesrc);
  9312. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9313. return 0;
  9314. }
  9315. static int intel_gen6_queue_flip(struct drm_device *dev,
  9316. struct drm_crtc *crtc,
  9317. struct drm_framebuffer *fb,
  9318. struct drm_i915_gem_object *obj,
  9319. struct drm_i915_gem_request *req,
  9320. uint32_t flags)
  9321. {
  9322. struct intel_engine_cs *engine = req->engine;
  9323. struct drm_i915_private *dev_priv = dev->dev_private;
  9324. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9325. uint32_t pf, pipesrc;
  9326. int ret;
  9327. ret = intel_ring_begin(req, 4);
  9328. if (ret)
  9329. return ret;
  9330. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9331. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9332. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9333. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9334. /* Contrary to the suggestions in the documentation,
  9335. * "Enable Panel Fitter" does not seem to be required when page
  9336. * flipping with a non-native mode, and worse causes a normal
  9337. * modeset to fail.
  9338. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9339. */
  9340. pf = 0;
  9341. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9342. intel_ring_emit(engine, pf | pipesrc);
  9343. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9344. return 0;
  9345. }
  9346. static int intel_gen7_queue_flip(struct drm_device *dev,
  9347. struct drm_crtc *crtc,
  9348. struct drm_framebuffer *fb,
  9349. struct drm_i915_gem_object *obj,
  9350. struct drm_i915_gem_request *req,
  9351. uint32_t flags)
  9352. {
  9353. struct intel_engine_cs *engine = req->engine;
  9354. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9355. uint32_t plane_bit = 0;
  9356. int len, ret;
  9357. switch (intel_crtc->plane) {
  9358. case PLANE_A:
  9359. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9360. break;
  9361. case PLANE_B:
  9362. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9363. break;
  9364. case PLANE_C:
  9365. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9366. break;
  9367. default:
  9368. WARN_ONCE(1, "unknown plane in flip command\n");
  9369. return -ENODEV;
  9370. }
  9371. len = 4;
  9372. if (engine->id == RCS) {
  9373. len += 6;
  9374. /*
  9375. * On Gen 8, SRM is now taking an extra dword to accommodate
  9376. * 48bits addresses, and we need a NOOP for the batch size to
  9377. * stay even.
  9378. */
  9379. if (IS_GEN8(dev))
  9380. len += 2;
  9381. }
  9382. /*
  9383. * BSpec MI_DISPLAY_FLIP for IVB:
  9384. * "The full packet must be contained within the same cache line."
  9385. *
  9386. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9387. * cacheline, if we ever start emitting more commands before
  9388. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9389. * then do the cacheline alignment, and finally emit the
  9390. * MI_DISPLAY_FLIP.
  9391. */
  9392. ret = intel_ring_cacheline_align(req);
  9393. if (ret)
  9394. return ret;
  9395. ret = intel_ring_begin(req, len);
  9396. if (ret)
  9397. return ret;
  9398. /* Unmask the flip-done completion message. Note that the bspec says that
  9399. * we should do this for both the BCS and RCS, and that we must not unmask
  9400. * more than one flip event at any time (or ensure that one flip message
  9401. * can be sent by waiting for flip-done prior to queueing new flips).
  9402. * Experimentation says that BCS works despite DERRMR masking all
  9403. * flip-done completion events and that unmasking all planes at once
  9404. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9405. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9406. */
  9407. if (engine->id == RCS) {
  9408. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9409. intel_ring_emit_reg(engine, DERRMR);
  9410. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9411. DERRMR_PIPEB_PRI_FLIP_DONE |
  9412. DERRMR_PIPEC_PRI_FLIP_DONE));
  9413. if (IS_GEN8(dev))
  9414. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9415. MI_SRM_LRM_GLOBAL_GTT);
  9416. else
  9417. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9418. MI_SRM_LRM_GLOBAL_GTT);
  9419. intel_ring_emit_reg(engine, DERRMR);
  9420. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9421. if (IS_GEN8(dev)) {
  9422. intel_ring_emit(engine, 0);
  9423. intel_ring_emit(engine, MI_NOOP);
  9424. }
  9425. }
  9426. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9427. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9428. intel_ring_emit(engine, intel_crtc->unpin_work->gtt_offset);
  9429. intel_ring_emit(engine, (MI_NOOP));
  9430. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9431. return 0;
  9432. }
  9433. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9434. struct drm_i915_gem_object *obj)
  9435. {
  9436. /*
  9437. * This is not being used for older platforms, because
  9438. * non-availability of flip done interrupt forces us to use
  9439. * CS flips. Older platforms derive flip done using some clever
  9440. * tricks involving the flip_pending status bits and vblank irqs.
  9441. * So using MMIO flips there would disrupt this mechanism.
  9442. */
  9443. if (engine == NULL)
  9444. return true;
  9445. if (INTEL_GEN(engine->i915) < 5)
  9446. return false;
  9447. if (i915.use_mmio_flip < 0)
  9448. return false;
  9449. else if (i915.use_mmio_flip > 0)
  9450. return true;
  9451. else if (i915.enable_execlists)
  9452. return true;
  9453. else if (obj->base.dma_buf &&
  9454. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9455. false))
  9456. return true;
  9457. else
  9458. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9459. }
  9460. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9461. unsigned int rotation,
  9462. struct intel_unpin_work *work)
  9463. {
  9464. struct drm_device *dev = intel_crtc->base.dev;
  9465. struct drm_i915_private *dev_priv = dev->dev_private;
  9466. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9467. const enum pipe pipe = intel_crtc->pipe;
  9468. u32 ctl, stride, tile_height;
  9469. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9470. ctl &= ~PLANE_CTL_TILED_MASK;
  9471. switch (fb->modifier[0]) {
  9472. case DRM_FORMAT_MOD_NONE:
  9473. break;
  9474. case I915_FORMAT_MOD_X_TILED:
  9475. ctl |= PLANE_CTL_TILED_X;
  9476. break;
  9477. case I915_FORMAT_MOD_Y_TILED:
  9478. ctl |= PLANE_CTL_TILED_Y;
  9479. break;
  9480. case I915_FORMAT_MOD_Yf_TILED:
  9481. ctl |= PLANE_CTL_TILED_YF;
  9482. break;
  9483. default:
  9484. MISSING_CASE(fb->modifier[0]);
  9485. }
  9486. /*
  9487. * The stride is either expressed as a multiple of 64 bytes chunks for
  9488. * linear buffers or in number of tiles for tiled buffers.
  9489. */
  9490. if (intel_rotation_90_or_270(rotation)) {
  9491. /* stride = Surface height in tiles */
  9492. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9493. stride = DIV_ROUND_UP(fb->height, tile_height);
  9494. } else {
  9495. stride = fb->pitches[0] /
  9496. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9497. fb->pixel_format);
  9498. }
  9499. /*
  9500. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9501. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9502. */
  9503. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9504. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9505. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9506. POSTING_READ(PLANE_SURF(pipe, 0));
  9507. }
  9508. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9509. struct intel_unpin_work *work)
  9510. {
  9511. struct drm_device *dev = intel_crtc->base.dev;
  9512. struct drm_i915_private *dev_priv = dev->dev_private;
  9513. struct intel_framebuffer *intel_fb =
  9514. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9515. struct drm_i915_gem_object *obj = intel_fb->obj;
  9516. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9517. u32 dspcntr;
  9518. dspcntr = I915_READ(reg);
  9519. if (obj->tiling_mode != I915_TILING_NONE)
  9520. dspcntr |= DISPPLANE_TILED;
  9521. else
  9522. dspcntr &= ~DISPPLANE_TILED;
  9523. I915_WRITE(reg, dspcntr);
  9524. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9525. POSTING_READ(DSPSURF(intel_crtc->plane));
  9526. }
  9527. /*
  9528. * XXX: This is the temporary way to update the plane registers until we get
  9529. * around to using the usual plane update functions for MMIO flips
  9530. */
  9531. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9532. {
  9533. struct intel_crtc *crtc = mmio_flip->crtc;
  9534. struct intel_unpin_work *work;
  9535. spin_lock_irq(&crtc->base.dev->event_lock);
  9536. work = crtc->unpin_work;
  9537. spin_unlock_irq(&crtc->base.dev->event_lock);
  9538. if (work == NULL)
  9539. return;
  9540. intel_mark_page_flip_active(work);
  9541. intel_pipe_update_start(crtc);
  9542. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9543. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9544. else
  9545. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9546. ilk_do_mmio_flip(crtc, work);
  9547. intel_pipe_update_end(crtc);
  9548. }
  9549. static void intel_mmio_flip_work_func(struct work_struct *work)
  9550. {
  9551. struct intel_mmio_flip *mmio_flip =
  9552. container_of(work, struct intel_mmio_flip, work);
  9553. struct intel_framebuffer *intel_fb =
  9554. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9555. struct drm_i915_gem_object *obj = intel_fb->obj;
  9556. if (mmio_flip->req) {
  9557. WARN_ON(__i915_wait_request(mmio_flip->req,
  9558. false, NULL,
  9559. &mmio_flip->i915->rps.mmioflips));
  9560. i915_gem_request_unreference(mmio_flip->req);
  9561. }
  9562. /* For framebuffer backed by dmabuf, wait for fence */
  9563. if (obj->base.dma_buf)
  9564. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9565. false, false,
  9566. MAX_SCHEDULE_TIMEOUT) < 0);
  9567. intel_do_mmio_flip(mmio_flip);
  9568. kfree(mmio_flip);
  9569. }
  9570. static int intel_queue_mmio_flip(struct drm_device *dev,
  9571. struct drm_crtc *crtc,
  9572. struct drm_i915_gem_object *obj)
  9573. {
  9574. struct intel_mmio_flip *mmio_flip;
  9575. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9576. if (mmio_flip == NULL)
  9577. return -ENOMEM;
  9578. mmio_flip->i915 = to_i915(dev);
  9579. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9580. mmio_flip->crtc = to_intel_crtc(crtc);
  9581. mmio_flip->rotation = crtc->primary->state->rotation;
  9582. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9583. schedule_work(&mmio_flip->work);
  9584. return 0;
  9585. }
  9586. static int intel_default_queue_flip(struct drm_device *dev,
  9587. struct drm_crtc *crtc,
  9588. struct drm_framebuffer *fb,
  9589. struct drm_i915_gem_object *obj,
  9590. struct drm_i915_gem_request *req,
  9591. uint32_t flags)
  9592. {
  9593. return -ENODEV;
  9594. }
  9595. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9596. struct drm_crtc *crtc)
  9597. {
  9598. struct drm_i915_private *dev_priv = dev->dev_private;
  9599. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9600. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9601. u32 addr;
  9602. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9603. return true;
  9604. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9605. return false;
  9606. if (!work->enable_stall_check)
  9607. return false;
  9608. if (work->flip_ready_vblank == 0) {
  9609. if (work->flip_queued_req &&
  9610. !i915_gem_request_completed(work->flip_queued_req, true))
  9611. return false;
  9612. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9613. }
  9614. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9615. return false;
  9616. /* Potential stall - if we see that the flip has happened,
  9617. * assume a missed interrupt. */
  9618. if (INTEL_INFO(dev)->gen >= 4)
  9619. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9620. else
  9621. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9622. /* There is a potential issue here with a false positive after a flip
  9623. * to the same address. We could address this by checking for a
  9624. * non-incrementing frame counter.
  9625. */
  9626. return addr == work->gtt_offset;
  9627. }
  9628. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9629. {
  9630. struct drm_device *dev = dev_priv->dev;
  9631. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9632. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9633. struct intel_unpin_work *work;
  9634. WARN_ON(!in_interrupt());
  9635. if (crtc == NULL)
  9636. return;
  9637. spin_lock(&dev->event_lock);
  9638. work = intel_crtc->unpin_work;
  9639. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9640. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9641. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9642. page_flip_completed(intel_crtc);
  9643. work = NULL;
  9644. }
  9645. if (work != NULL &&
  9646. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9647. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9648. spin_unlock(&dev->event_lock);
  9649. }
  9650. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9651. struct drm_framebuffer *fb,
  9652. struct drm_pending_vblank_event *event,
  9653. uint32_t page_flip_flags)
  9654. {
  9655. struct drm_device *dev = crtc->dev;
  9656. struct drm_i915_private *dev_priv = dev->dev_private;
  9657. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9658. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9659. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9660. struct drm_plane *primary = crtc->primary;
  9661. enum pipe pipe = intel_crtc->pipe;
  9662. struct intel_unpin_work *work;
  9663. struct intel_engine_cs *engine;
  9664. bool mmio_flip;
  9665. struct drm_i915_gem_request *request = NULL;
  9666. int ret;
  9667. /*
  9668. * drm_mode_page_flip_ioctl() should already catch this, but double
  9669. * check to be safe. In the future we may enable pageflipping from
  9670. * a disabled primary plane.
  9671. */
  9672. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9673. return -EBUSY;
  9674. /* Can't change pixel format via MI display flips. */
  9675. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9676. return -EINVAL;
  9677. /*
  9678. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9679. * Note that pitch changes could also affect these register.
  9680. */
  9681. if (INTEL_INFO(dev)->gen > 3 &&
  9682. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9683. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9684. return -EINVAL;
  9685. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9686. goto out_hang;
  9687. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9688. if (work == NULL)
  9689. return -ENOMEM;
  9690. work->event = event;
  9691. work->crtc = crtc;
  9692. work->old_fb = old_fb;
  9693. INIT_WORK(&work->work, intel_unpin_work_fn);
  9694. ret = drm_crtc_vblank_get(crtc);
  9695. if (ret)
  9696. goto free_work;
  9697. /* We borrow the event spin lock for protecting unpin_work */
  9698. spin_lock_irq(&dev->event_lock);
  9699. if (intel_crtc->unpin_work) {
  9700. /* Before declaring the flip queue wedged, check if
  9701. * the hardware completed the operation behind our backs.
  9702. */
  9703. if (__intel_pageflip_stall_check(dev, crtc)) {
  9704. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9705. page_flip_completed(intel_crtc);
  9706. } else {
  9707. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9708. spin_unlock_irq(&dev->event_lock);
  9709. drm_crtc_vblank_put(crtc);
  9710. kfree(work);
  9711. return -EBUSY;
  9712. }
  9713. }
  9714. intel_crtc->unpin_work = work;
  9715. spin_unlock_irq(&dev->event_lock);
  9716. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9717. flush_workqueue(dev_priv->wq);
  9718. /* Reference the objects for the scheduled work. */
  9719. drm_framebuffer_reference(work->old_fb);
  9720. drm_gem_object_reference(&obj->base);
  9721. crtc->primary->fb = fb;
  9722. update_state_fb(crtc->primary);
  9723. intel_fbc_pre_update(intel_crtc);
  9724. work->pending_flip_obj = obj;
  9725. ret = i915_mutex_lock_interruptible(dev);
  9726. if (ret)
  9727. goto cleanup;
  9728. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9729. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9730. ret = -EIO;
  9731. goto cleanup;
  9732. }
  9733. atomic_inc(&intel_crtc->unpin_work_count);
  9734. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9735. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9736. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9737. engine = &dev_priv->engine[BCS];
  9738. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9739. /* vlv: DISPLAY_FLIP fails to change tiling */
  9740. engine = NULL;
  9741. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9742. engine = &dev_priv->engine[BCS];
  9743. } else if (INTEL_INFO(dev)->gen >= 7) {
  9744. engine = i915_gem_request_get_engine(obj->last_write_req);
  9745. if (engine == NULL || engine->id != RCS)
  9746. engine = &dev_priv->engine[BCS];
  9747. } else {
  9748. engine = &dev_priv->engine[RCS];
  9749. }
  9750. mmio_flip = use_mmio_flip(engine, obj);
  9751. /* When using CS flips, we want to emit semaphores between rings.
  9752. * However, when using mmio flips we will create a task to do the
  9753. * synchronisation, so all we want here is to pin the framebuffer
  9754. * into the display plane and skip any waits.
  9755. */
  9756. if (!mmio_flip) {
  9757. ret = i915_gem_object_sync(obj, engine, &request);
  9758. if (ret)
  9759. goto cleanup_pending;
  9760. }
  9761. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9762. if (ret)
  9763. goto cleanup_pending;
  9764. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9765. obj, 0);
  9766. work->gtt_offset += intel_crtc->dspaddr_offset;
  9767. if (mmio_flip) {
  9768. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9769. if (ret)
  9770. goto cleanup_unpin;
  9771. i915_gem_request_assign(&work->flip_queued_req,
  9772. obj->last_write_req);
  9773. } else {
  9774. if (!request) {
  9775. request = i915_gem_request_alloc(engine, NULL);
  9776. if (IS_ERR(request)) {
  9777. ret = PTR_ERR(request);
  9778. goto cleanup_unpin;
  9779. }
  9780. }
  9781. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9782. page_flip_flags);
  9783. if (ret)
  9784. goto cleanup_unpin;
  9785. i915_gem_request_assign(&work->flip_queued_req, request);
  9786. }
  9787. if (request)
  9788. i915_add_request_no_flush(request);
  9789. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9790. work->enable_stall_check = true;
  9791. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9792. to_intel_plane(primary)->frontbuffer_bit);
  9793. mutex_unlock(&dev->struct_mutex);
  9794. intel_frontbuffer_flip_prepare(dev,
  9795. to_intel_plane(primary)->frontbuffer_bit);
  9796. trace_i915_flip_request(intel_crtc->plane, obj);
  9797. return 0;
  9798. cleanup_unpin:
  9799. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9800. cleanup_pending:
  9801. if (!IS_ERR_OR_NULL(request))
  9802. i915_add_request_no_flush(request);
  9803. atomic_dec(&intel_crtc->unpin_work_count);
  9804. mutex_unlock(&dev->struct_mutex);
  9805. cleanup:
  9806. crtc->primary->fb = old_fb;
  9807. update_state_fb(crtc->primary);
  9808. drm_gem_object_unreference_unlocked(&obj->base);
  9809. drm_framebuffer_unreference(work->old_fb);
  9810. spin_lock_irq(&dev->event_lock);
  9811. intel_crtc->unpin_work = NULL;
  9812. spin_unlock_irq(&dev->event_lock);
  9813. drm_crtc_vblank_put(crtc);
  9814. free_work:
  9815. kfree(work);
  9816. if (ret == -EIO) {
  9817. struct drm_atomic_state *state;
  9818. struct drm_plane_state *plane_state;
  9819. out_hang:
  9820. state = drm_atomic_state_alloc(dev);
  9821. if (!state)
  9822. return -ENOMEM;
  9823. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9824. retry:
  9825. plane_state = drm_atomic_get_plane_state(state, primary);
  9826. ret = PTR_ERR_OR_ZERO(plane_state);
  9827. if (!ret) {
  9828. drm_atomic_set_fb_for_plane(plane_state, fb);
  9829. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9830. if (!ret)
  9831. ret = drm_atomic_commit(state);
  9832. }
  9833. if (ret == -EDEADLK) {
  9834. drm_modeset_backoff(state->acquire_ctx);
  9835. drm_atomic_state_clear(state);
  9836. goto retry;
  9837. }
  9838. if (ret)
  9839. drm_atomic_state_free(state);
  9840. if (ret == 0 && event) {
  9841. spin_lock_irq(&dev->event_lock);
  9842. drm_crtc_send_vblank_event(crtc, event);
  9843. spin_unlock_irq(&dev->event_lock);
  9844. }
  9845. }
  9846. return ret;
  9847. }
  9848. /**
  9849. * intel_wm_need_update - Check whether watermarks need updating
  9850. * @plane: drm plane
  9851. * @state: new plane state
  9852. *
  9853. * Check current plane state versus the new one to determine whether
  9854. * watermarks need to be recalculated.
  9855. *
  9856. * Returns true or false.
  9857. */
  9858. static bool intel_wm_need_update(struct drm_plane *plane,
  9859. struct drm_plane_state *state)
  9860. {
  9861. struct intel_plane_state *new = to_intel_plane_state(state);
  9862. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9863. /* Update watermarks on tiling or size changes. */
  9864. if (new->visible != cur->visible)
  9865. return true;
  9866. if (!cur->base.fb || !new->base.fb)
  9867. return false;
  9868. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9869. cur->base.rotation != new->base.rotation ||
  9870. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9871. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9872. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9873. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9874. return true;
  9875. return false;
  9876. }
  9877. static bool needs_scaling(struct intel_plane_state *state)
  9878. {
  9879. int src_w = drm_rect_width(&state->src) >> 16;
  9880. int src_h = drm_rect_height(&state->src) >> 16;
  9881. int dst_w = drm_rect_width(&state->dst);
  9882. int dst_h = drm_rect_height(&state->dst);
  9883. return (src_w != dst_w || src_h != dst_h);
  9884. }
  9885. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9886. struct drm_plane_state *plane_state)
  9887. {
  9888. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9889. struct drm_crtc *crtc = crtc_state->crtc;
  9890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9891. struct drm_plane *plane = plane_state->plane;
  9892. struct drm_device *dev = crtc->dev;
  9893. struct drm_i915_private *dev_priv = to_i915(dev);
  9894. struct intel_plane_state *old_plane_state =
  9895. to_intel_plane_state(plane->state);
  9896. int idx = intel_crtc->base.base.id, ret;
  9897. bool mode_changed = needs_modeset(crtc_state);
  9898. bool was_crtc_enabled = crtc->state->active;
  9899. bool is_crtc_enabled = crtc_state->active;
  9900. bool turn_off, turn_on, visible, was_visible;
  9901. struct drm_framebuffer *fb = plane_state->fb;
  9902. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9903. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9904. ret = skl_update_scaler_plane(
  9905. to_intel_crtc_state(crtc_state),
  9906. to_intel_plane_state(plane_state));
  9907. if (ret)
  9908. return ret;
  9909. }
  9910. was_visible = old_plane_state->visible;
  9911. visible = to_intel_plane_state(plane_state)->visible;
  9912. if (!was_crtc_enabled && WARN_ON(was_visible))
  9913. was_visible = false;
  9914. /*
  9915. * Visibility is calculated as if the crtc was on, but
  9916. * after scaler setup everything depends on it being off
  9917. * when the crtc isn't active.
  9918. *
  9919. * FIXME this is wrong for watermarks. Watermarks should also
  9920. * be computed as if the pipe would be active. Perhaps move
  9921. * per-plane wm computation to the .check_plane() hook, and
  9922. * only combine the results from all planes in the current place?
  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. }
  10066. if (INTEL_INFO(dev)->gen >= 9) {
  10067. if (mode_changed)
  10068. ret = skl_update_scaler_crtc(pipe_config);
  10069. if (!ret)
  10070. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10071. pipe_config);
  10072. }
  10073. return ret;
  10074. }
  10075. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10076. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10077. .atomic_begin = intel_begin_crtc_commit,
  10078. .atomic_flush = intel_finish_crtc_commit,
  10079. .atomic_check = intel_crtc_atomic_check,
  10080. };
  10081. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10082. {
  10083. struct intel_connector *connector;
  10084. for_each_intel_connector(dev, connector) {
  10085. if (connector->base.encoder) {
  10086. connector->base.state->best_encoder =
  10087. connector->base.encoder;
  10088. connector->base.state->crtc =
  10089. connector->base.encoder->crtc;
  10090. } else {
  10091. connector->base.state->best_encoder = NULL;
  10092. connector->base.state->crtc = NULL;
  10093. }
  10094. }
  10095. }
  10096. static void
  10097. connected_sink_compute_bpp(struct intel_connector *connector,
  10098. struct intel_crtc_state *pipe_config)
  10099. {
  10100. int bpp = pipe_config->pipe_bpp;
  10101. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10102. connector->base.base.id,
  10103. connector->base.name);
  10104. /* Don't use an invalid EDID bpc value */
  10105. if (connector->base.display_info.bpc &&
  10106. connector->base.display_info.bpc * 3 < bpp) {
  10107. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10108. bpp, connector->base.display_info.bpc*3);
  10109. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10110. }
  10111. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10112. if (connector->base.display_info.bpc == 0) {
  10113. int type = connector->base.connector_type;
  10114. int clamp_bpp = 24;
  10115. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10116. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10117. type == DRM_MODE_CONNECTOR_eDP)
  10118. clamp_bpp = 18;
  10119. if (bpp > clamp_bpp) {
  10120. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10121. bpp, clamp_bpp);
  10122. pipe_config->pipe_bpp = clamp_bpp;
  10123. }
  10124. }
  10125. }
  10126. static int
  10127. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10128. struct intel_crtc_state *pipe_config)
  10129. {
  10130. struct drm_device *dev = crtc->base.dev;
  10131. struct drm_atomic_state *state;
  10132. struct drm_connector *connector;
  10133. struct drm_connector_state *connector_state;
  10134. int bpp, i;
  10135. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10136. bpp = 10*3;
  10137. else if (INTEL_INFO(dev)->gen >= 5)
  10138. bpp = 12*3;
  10139. else
  10140. bpp = 8*3;
  10141. pipe_config->pipe_bpp = bpp;
  10142. state = pipe_config->base.state;
  10143. /* Clamp display bpp to EDID value */
  10144. for_each_connector_in_state(state, connector, connector_state, i) {
  10145. if (connector_state->crtc != &crtc->base)
  10146. continue;
  10147. connected_sink_compute_bpp(to_intel_connector(connector),
  10148. pipe_config);
  10149. }
  10150. return bpp;
  10151. }
  10152. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10153. {
  10154. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10155. "type: 0x%x flags: 0x%x\n",
  10156. mode->crtc_clock,
  10157. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10158. mode->crtc_hsync_end, mode->crtc_htotal,
  10159. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10160. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10161. }
  10162. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10163. struct intel_crtc_state *pipe_config,
  10164. const char *context)
  10165. {
  10166. struct drm_device *dev = crtc->base.dev;
  10167. struct drm_plane *plane;
  10168. struct intel_plane *intel_plane;
  10169. struct intel_plane_state *state;
  10170. struct drm_framebuffer *fb;
  10171. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10172. context, pipe_config, pipe_name(crtc->pipe));
  10173. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10174. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10175. pipe_config->pipe_bpp, pipe_config->dither);
  10176. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10177. pipe_config->has_pch_encoder,
  10178. pipe_config->fdi_lanes,
  10179. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10180. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10181. pipe_config->fdi_m_n.tu);
  10182. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10183. pipe_config->has_dp_encoder,
  10184. pipe_config->lane_count,
  10185. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10186. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10187. pipe_config->dp_m_n.tu);
  10188. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10189. pipe_config->has_dp_encoder,
  10190. pipe_config->lane_count,
  10191. pipe_config->dp_m2_n2.gmch_m,
  10192. pipe_config->dp_m2_n2.gmch_n,
  10193. pipe_config->dp_m2_n2.link_m,
  10194. pipe_config->dp_m2_n2.link_n,
  10195. pipe_config->dp_m2_n2.tu);
  10196. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10197. pipe_config->has_audio,
  10198. pipe_config->has_infoframe);
  10199. DRM_DEBUG_KMS("requested mode:\n");
  10200. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10201. DRM_DEBUG_KMS("adjusted mode:\n");
  10202. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10203. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10204. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10205. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10206. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10207. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10208. crtc->num_scalers,
  10209. pipe_config->scaler_state.scaler_users,
  10210. pipe_config->scaler_state.scaler_id);
  10211. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10212. pipe_config->gmch_pfit.control,
  10213. pipe_config->gmch_pfit.pgm_ratios,
  10214. pipe_config->gmch_pfit.lvds_border_bits);
  10215. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10216. pipe_config->pch_pfit.pos,
  10217. pipe_config->pch_pfit.size,
  10218. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10219. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10220. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10221. if (IS_BROXTON(dev)) {
  10222. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10223. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10224. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10225. pipe_config->ddi_pll_sel,
  10226. pipe_config->dpll_hw_state.ebb0,
  10227. pipe_config->dpll_hw_state.ebb4,
  10228. pipe_config->dpll_hw_state.pll0,
  10229. pipe_config->dpll_hw_state.pll1,
  10230. pipe_config->dpll_hw_state.pll2,
  10231. pipe_config->dpll_hw_state.pll3,
  10232. pipe_config->dpll_hw_state.pll6,
  10233. pipe_config->dpll_hw_state.pll8,
  10234. pipe_config->dpll_hw_state.pll9,
  10235. pipe_config->dpll_hw_state.pll10,
  10236. pipe_config->dpll_hw_state.pcsdw12);
  10237. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10238. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10239. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10240. pipe_config->ddi_pll_sel,
  10241. pipe_config->dpll_hw_state.ctrl1,
  10242. pipe_config->dpll_hw_state.cfgcr1,
  10243. pipe_config->dpll_hw_state.cfgcr2);
  10244. } else if (HAS_DDI(dev)) {
  10245. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10246. pipe_config->ddi_pll_sel,
  10247. pipe_config->dpll_hw_state.wrpll,
  10248. pipe_config->dpll_hw_state.spll);
  10249. } else {
  10250. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10251. "fp0: 0x%x, fp1: 0x%x\n",
  10252. pipe_config->dpll_hw_state.dpll,
  10253. pipe_config->dpll_hw_state.dpll_md,
  10254. pipe_config->dpll_hw_state.fp0,
  10255. pipe_config->dpll_hw_state.fp1);
  10256. }
  10257. DRM_DEBUG_KMS("planes on this crtc\n");
  10258. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10259. intel_plane = to_intel_plane(plane);
  10260. if (intel_plane->pipe != crtc->pipe)
  10261. continue;
  10262. state = to_intel_plane_state(plane->state);
  10263. fb = state->base.fb;
  10264. if (!fb) {
  10265. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10266. "disabled, scaler_id = %d\n",
  10267. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10268. plane->base.id, intel_plane->pipe,
  10269. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10270. drm_plane_index(plane), state->scaler_id);
  10271. continue;
  10272. }
  10273. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10274. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10275. plane->base.id, intel_plane->pipe,
  10276. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10277. drm_plane_index(plane));
  10278. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10279. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10280. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10281. state->scaler_id,
  10282. state->src.x1 >> 16, state->src.y1 >> 16,
  10283. drm_rect_width(&state->src) >> 16,
  10284. drm_rect_height(&state->src) >> 16,
  10285. state->dst.x1, state->dst.y1,
  10286. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10287. }
  10288. }
  10289. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10290. {
  10291. struct drm_device *dev = state->dev;
  10292. struct drm_connector *connector;
  10293. unsigned int used_ports = 0;
  10294. /*
  10295. * Walk the connector list instead of the encoder
  10296. * list to detect the problem on ddi platforms
  10297. * where there's just one encoder per digital port.
  10298. */
  10299. drm_for_each_connector(connector, dev) {
  10300. struct drm_connector_state *connector_state;
  10301. struct intel_encoder *encoder;
  10302. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10303. if (!connector_state)
  10304. connector_state = connector->state;
  10305. if (!connector_state->best_encoder)
  10306. continue;
  10307. encoder = to_intel_encoder(connector_state->best_encoder);
  10308. WARN_ON(!connector_state->crtc);
  10309. switch (encoder->type) {
  10310. unsigned int port_mask;
  10311. case INTEL_OUTPUT_UNKNOWN:
  10312. if (WARN_ON(!HAS_DDI(dev)))
  10313. break;
  10314. case INTEL_OUTPUT_DISPLAYPORT:
  10315. case INTEL_OUTPUT_HDMI:
  10316. case INTEL_OUTPUT_EDP:
  10317. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10318. /* the same port mustn't appear more than once */
  10319. if (used_ports & port_mask)
  10320. return false;
  10321. used_ports |= port_mask;
  10322. default:
  10323. break;
  10324. }
  10325. }
  10326. return true;
  10327. }
  10328. static void
  10329. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10330. {
  10331. struct drm_crtc_state tmp_state;
  10332. struct intel_crtc_scaler_state scaler_state;
  10333. struct intel_dpll_hw_state dpll_hw_state;
  10334. struct intel_shared_dpll *shared_dpll;
  10335. uint32_t ddi_pll_sel;
  10336. bool force_thru;
  10337. /* FIXME: before the switch to atomic started, a new pipe_config was
  10338. * kzalloc'd. Code that depends on any field being zero should be
  10339. * fixed, so that the crtc_state can be safely duplicated. For now,
  10340. * only fields that are know to not cause problems are preserved. */
  10341. tmp_state = crtc_state->base;
  10342. scaler_state = crtc_state->scaler_state;
  10343. shared_dpll = crtc_state->shared_dpll;
  10344. dpll_hw_state = crtc_state->dpll_hw_state;
  10345. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10346. force_thru = crtc_state->pch_pfit.force_thru;
  10347. memset(crtc_state, 0, sizeof *crtc_state);
  10348. crtc_state->base = tmp_state;
  10349. crtc_state->scaler_state = scaler_state;
  10350. crtc_state->shared_dpll = shared_dpll;
  10351. crtc_state->dpll_hw_state = dpll_hw_state;
  10352. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10353. crtc_state->pch_pfit.force_thru = force_thru;
  10354. }
  10355. static int
  10356. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10357. struct intel_crtc_state *pipe_config)
  10358. {
  10359. struct drm_atomic_state *state = pipe_config->base.state;
  10360. struct intel_encoder *encoder;
  10361. struct drm_connector *connector;
  10362. struct drm_connector_state *connector_state;
  10363. int base_bpp, ret = -EINVAL;
  10364. int i;
  10365. bool retry = true;
  10366. clear_intel_crtc_state(pipe_config);
  10367. pipe_config->cpu_transcoder =
  10368. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10369. /*
  10370. * Sanitize sync polarity flags based on requested ones. If neither
  10371. * positive or negative polarity is requested, treat this as meaning
  10372. * negative polarity.
  10373. */
  10374. if (!(pipe_config->base.adjusted_mode.flags &
  10375. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10376. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10377. if (!(pipe_config->base.adjusted_mode.flags &
  10378. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10379. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10380. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10381. pipe_config);
  10382. if (base_bpp < 0)
  10383. goto fail;
  10384. /*
  10385. * Determine the real pipe dimensions. Note that stereo modes can
  10386. * increase the actual pipe size due to the frame doubling and
  10387. * insertion of additional space for blanks between the frame. This
  10388. * is stored in the crtc timings. We use the requested mode to do this
  10389. * computation to clearly distinguish it from the adjusted mode, which
  10390. * can be changed by the connectors in the below retry loop.
  10391. */
  10392. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10393. &pipe_config->pipe_src_w,
  10394. &pipe_config->pipe_src_h);
  10395. encoder_retry:
  10396. /* Ensure the port clock defaults are reset when retrying. */
  10397. pipe_config->port_clock = 0;
  10398. pipe_config->pixel_multiplier = 1;
  10399. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10400. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10401. CRTC_STEREO_DOUBLE);
  10402. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10403. * adjust it according to limitations or connector properties, and also
  10404. * a chance to reject the mode entirely.
  10405. */
  10406. for_each_connector_in_state(state, connector, connector_state, i) {
  10407. if (connector_state->crtc != crtc)
  10408. continue;
  10409. encoder = to_intel_encoder(connector_state->best_encoder);
  10410. if (!(encoder->compute_config(encoder, pipe_config))) {
  10411. DRM_DEBUG_KMS("Encoder config failure\n");
  10412. goto fail;
  10413. }
  10414. }
  10415. /* Set default port clock if not overwritten by the encoder. Needs to be
  10416. * done afterwards in case the encoder adjusts the mode. */
  10417. if (!pipe_config->port_clock)
  10418. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10419. * pipe_config->pixel_multiplier;
  10420. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10421. if (ret < 0) {
  10422. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10423. goto fail;
  10424. }
  10425. if (ret == RETRY) {
  10426. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10427. ret = -EINVAL;
  10428. goto fail;
  10429. }
  10430. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10431. retry = false;
  10432. goto encoder_retry;
  10433. }
  10434. /* Dithering seems to not pass-through bits correctly when it should, so
  10435. * only enable it on 6bpc panels. */
  10436. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10437. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10438. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10439. fail:
  10440. return ret;
  10441. }
  10442. static void
  10443. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10444. {
  10445. struct drm_crtc *crtc;
  10446. struct drm_crtc_state *crtc_state;
  10447. int i;
  10448. /* Double check state. */
  10449. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10450. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10451. /* Update hwmode for vblank functions */
  10452. if (crtc->state->active)
  10453. crtc->hwmode = crtc->state->adjusted_mode;
  10454. else
  10455. crtc->hwmode.crtc_clock = 0;
  10456. /*
  10457. * Update legacy state to satisfy fbc code. This can
  10458. * be removed when fbc uses the atomic state.
  10459. */
  10460. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10461. struct drm_plane_state *plane_state = crtc->primary->state;
  10462. crtc->primary->fb = plane_state->fb;
  10463. crtc->x = plane_state->src_x >> 16;
  10464. crtc->y = plane_state->src_y >> 16;
  10465. }
  10466. }
  10467. }
  10468. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10469. {
  10470. int diff;
  10471. if (clock1 == clock2)
  10472. return true;
  10473. if (!clock1 || !clock2)
  10474. return false;
  10475. diff = abs(clock1 - clock2);
  10476. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10477. return true;
  10478. return false;
  10479. }
  10480. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10481. list_for_each_entry((intel_crtc), \
  10482. &(dev)->mode_config.crtc_list, \
  10483. base.head) \
  10484. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10485. static bool
  10486. intel_compare_m_n(unsigned int m, unsigned int n,
  10487. unsigned int m2, unsigned int n2,
  10488. bool exact)
  10489. {
  10490. if (m == m2 && n == n2)
  10491. return true;
  10492. if (exact || !m || !n || !m2 || !n2)
  10493. return false;
  10494. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10495. if (n > n2) {
  10496. while (n > n2) {
  10497. m2 <<= 1;
  10498. n2 <<= 1;
  10499. }
  10500. } else if (n < n2) {
  10501. while (n < n2) {
  10502. m <<= 1;
  10503. n <<= 1;
  10504. }
  10505. }
  10506. if (n != n2)
  10507. return false;
  10508. return intel_fuzzy_clock_check(m, m2);
  10509. }
  10510. static bool
  10511. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10512. struct intel_link_m_n *m2_n2,
  10513. bool adjust)
  10514. {
  10515. if (m_n->tu == m2_n2->tu &&
  10516. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10517. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10518. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10519. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10520. if (adjust)
  10521. *m2_n2 = *m_n;
  10522. return true;
  10523. }
  10524. return false;
  10525. }
  10526. static bool
  10527. intel_pipe_config_compare(struct drm_device *dev,
  10528. struct intel_crtc_state *current_config,
  10529. struct intel_crtc_state *pipe_config,
  10530. bool adjust)
  10531. {
  10532. bool ret = true;
  10533. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10534. do { \
  10535. if (!adjust) \
  10536. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10537. else \
  10538. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10539. } while (0)
  10540. #define PIPE_CONF_CHECK_X(name) \
  10541. if (current_config->name != pipe_config->name) { \
  10542. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10543. "(expected 0x%08x, found 0x%08x)\n", \
  10544. current_config->name, \
  10545. pipe_config->name); \
  10546. ret = false; \
  10547. }
  10548. #define PIPE_CONF_CHECK_I(name) \
  10549. if (current_config->name != pipe_config->name) { \
  10550. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10551. "(expected %i, found %i)\n", \
  10552. current_config->name, \
  10553. pipe_config->name); \
  10554. ret = false; \
  10555. }
  10556. #define PIPE_CONF_CHECK_P(name) \
  10557. if (current_config->name != pipe_config->name) { \
  10558. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10559. "(expected %p, found %p)\n", \
  10560. current_config->name, \
  10561. pipe_config->name); \
  10562. ret = false; \
  10563. }
  10564. #define PIPE_CONF_CHECK_M_N(name) \
  10565. if (!intel_compare_link_m_n(&current_config->name, \
  10566. &pipe_config->name,\
  10567. adjust)) { \
  10568. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10569. "(expected tu %i gmch %i/%i link %i/%i, " \
  10570. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10571. current_config->name.tu, \
  10572. current_config->name.gmch_m, \
  10573. current_config->name.gmch_n, \
  10574. current_config->name.link_m, \
  10575. current_config->name.link_n, \
  10576. pipe_config->name.tu, \
  10577. pipe_config->name.gmch_m, \
  10578. pipe_config->name.gmch_n, \
  10579. pipe_config->name.link_m, \
  10580. pipe_config->name.link_n); \
  10581. ret = false; \
  10582. }
  10583. /* This is required for BDW+ where there is only one set of registers for
  10584. * switching between high and low RR.
  10585. * This macro can be used whenever a comparison has to be made between one
  10586. * hw state and multiple sw state variables.
  10587. */
  10588. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10589. if (!intel_compare_link_m_n(&current_config->name, \
  10590. &pipe_config->name, adjust) && \
  10591. !intel_compare_link_m_n(&current_config->alt_name, \
  10592. &pipe_config->name, adjust)) { \
  10593. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10594. "(expected tu %i gmch %i/%i link %i/%i, " \
  10595. "or tu %i gmch %i/%i link %i/%i, " \
  10596. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10597. current_config->name.tu, \
  10598. current_config->name.gmch_m, \
  10599. current_config->name.gmch_n, \
  10600. current_config->name.link_m, \
  10601. current_config->name.link_n, \
  10602. current_config->alt_name.tu, \
  10603. current_config->alt_name.gmch_m, \
  10604. current_config->alt_name.gmch_n, \
  10605. current_config->alt_name.link_m, \
  10606. current_config->alt_name.link_n, \
  10607. pipe_config->name.tu, \
  10608. pipe_config->name.gmch_m, \
  10609. pipe_config->name.gmch_n, \
  10610. pipe_config->name.link_m, \
  10611. pipe_config->name.link_n); \
  10612. ret = false; \
  10613. }
  10614. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10615. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10616. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10617. "(expected %i, found %i)\n", \
  10618. current_config->name & (mask), \
  10619. pipe_config->name & (mask)); \
  10620. ret = false; \
  10621. }
  10622. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10623. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10624. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10625. "(expected %i, found %i)\n", \
  10626. current_config->name, \
  10627. pipe_config->name); \
  10628. ret = false; \
  10629. }
  10630. #define PIPE_CONF_QUIRK(quirk) \
  10631. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10632. PIPE_CONF_CHECK_I(cpu_transcoder);
  10633. PIPE_CONF_CHECK_I(has_pch_encoder);
  10634. PIPE_CONF_CHECK_I(fdi_lanes);
  10635. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10636. PIPE_CONF_CHECK_I(has_dp_encoder);
  10637. PIPE_CONF_CHECK_I(lane_count);
  10638. if (INTEL_INFO(dev)->gen < 8) {
  10639. PIPE_CONF_CHECK_M_N(dp_m_n);
  10640. if (current_config->has_drrs)
  10641. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10642. } else
  10643. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10644. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10645. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10646. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10647. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10648. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10649. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10650. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10651. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10652. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10653. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10654. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10655. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10656. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10657. PIPE_CONF_CHECK_I(pixel_multiplier);
  10658. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10659. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10660. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10661. PIPE_CONF_CHECK_I(limited_color_range);
  10662. PIPE_CONF_CHECK_I(has_infoframe);
  10663. PIPE_CONF_CHECK_I(has_audio);
  10664. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10665. DRM_MODE_FLAG_INTERLACE);
  10666. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10667. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10668. DRM_MODE_FLAG_PHSYNC);
  10669. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10670. DRM_MODE_FLAG_NHSYNC);
  10671. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10672. DRM_MODE_FLAG_PVSYNC);
  10673. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10674. DRM_MODE_FLAG_NVSYNC);
  10675. }
  10676. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10677. /* pfit ratios are autocomputed by the hw on gen4+ */
  10678. if (INTEL_INFO(dev)->gen < 4)
  10679. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10680. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10681. if (!adjust) {
  10682. PIPE_CONF_CHECK_I(pipe_src_w);
  10683. PIPE_CONF_CHECK_I(pipe_src_h);
  10684. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10685. if (current_config->pch_pfit.enabled) {
  10686. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10687. PIPE_CONF_CHECK_X(pch_pfit.size);
  10688. }
  10689. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10690. }
  10691. /* BDW+ don't expose a synchronous way to read the state */
  10692. if (IS_HASWELL(dev))
  10693. PIPE_CONF_CHECK_I(ips_enabled);
  10694. PIPE_CONF_CHECK_I(double_wide);
  10695. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10696. PIPE_CONF_CHECK_P(shared_dpll);
  10697. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10698. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10699. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10700. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10701. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10702. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10703. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10704. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10705. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10706. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10707. PIPE_CONF_CHECK_X(dsi_pll.div);
  10708. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10709. PIPE_CONF_CHECK_I(pipe_bpp);
  10710. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10711. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10712. #undef PIPE_CONF_CHECK_X
  10713. #undef PIPE_CONF_CHECK_I
  10714. #undef PIPE_CONF_CHECK_P
  10715. #undef PIPE_CONF_CHECK_FLAGS
  10716. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10717. #undef PIPE_CONF_QUIRK
  10718. #undef INTEL_ERR_OR_DBG_KMS
  10719. return ret;
  10720. }
  10721. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10722. const struct intel_crtc_state *pipe_config)
  10723. {
  10724. if (pipe_config->has_pch_encoder) {
  10725. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10726. &pipe_config->fdi_m_n);
  10727. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10728. /*
  10729. * FDI already provided one idea for the dotclock.
  10730. * Yell if the encoder disagrees.
  10731. */
  10732. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10733. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10734. fdi_dotclock, dotclock);
  10735. }
  10736. }
  10737. static void verify_wm_state(struct drm_crtc *crtc,
  10738. struct drm_crtc_state *new_state)
  10739. {
  10740. struct drm_device *dev = crtc->dev;
  10741. struct drm_i915_private *dev_priv = dev->dev_private;
  10742. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10743. struct skl_ddb_entry *hw_entry, *sw_entry;
  10744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10745. const enum pipe pipe = intel_crtc->pipe;
  10746. int plane;
  10747. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10748. return;
  10749. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10750. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10751. /* planes */
  10752. for_each_plane(dev_priv, pipe, plane) {
  10753. hw_entry = &hw_ddb.plane[pipe][plane];
  10754. sw_entry = &sw_ddb->plane[pipe][plane];
  10755. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10756. continue;
  10757. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10758. "(expected (%u,%u), found (%u,%u))\n",
  10759. pipe_name(pipe), plane + 1,
  10760. sw_entry->start, sw_entry->end,
  10761. hw_entry->start, hw_entry->end);
  10762. }
  10763. /* cursor */
  10764. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10765. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10766. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10767. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10768. "(expected (%u,%u), found (%u,%u))\n",
  10769. pipe_name(pipe),
  10770. sw_entry->start, sw_entry->end,
  10771. hw_entry->start, hw_entry->end);
  10772. }
  10773. }
  10774. static void
  10775. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10776. {
  10777. struct drm_connector *connector;
  10778. drm_for_each_connector(connector, dev) {
  10779. struct drm_encoder *encoder = connector->encoder;
  10780. struct drm_connector_state *state = connector->state;
  10781. if (state->crtc != crtc)
  10782. continue;
  10783. intel_connector_verify_state(to_intel_connector(connector));
  10784. I915_STATE_WARN(state->best_encoder != encoder,
  10785. "connector's atomic encoder doesn't match legacy encoder\n");
  10786. }
  10787. }
  10788. static void
  10789. verify_encoder_state(struct drm_device *dev)
  10790. {
  10791. struct intel_encoder *encoder;
  10792. struct intel_connector *connector;
  10793. for_each_intel_encoder(dev, encoder) {
  10794. bool enabled = false;
  10795. enum pipe pipe;
  10796. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10797. encoder->base.base.id,
  10798. encoder->base.name);
  10799. for_each_intel_connector(dev, connector) {
  10800. if (connector->base.state->best_encoder != &encoder->base)
  10801. continue;
  10802. enabled = true;
  10803. I915_STATE_WARN(connector->base.state->crtc !=
  10804. encoder->base.crtc,
  10805. "connector's crtc doesn't match encoder crtc\n");
  10806. }
  10807. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10808. "encoder's enabled state mismatch "
  10809. "(expected %i, found %i)\n",
  10810. !!encoder->base.crtc, enabled);
  10811. if (!encoder->base.crtc) {
  10812. bool active;
  10813. active = encoder->get_hw_state(encoder, &pipe);
  10814. I915_STATE_WARN(active,
  10815. "encoder detached but still enabled on pipe %c.\n",
  10816. pipe_name(pipe));
  10817. }
  10818. }
  10819. }
  10820. static void
  10821. verify_crtc_state(struct drm_crtc *crtc,
  10822. struct drm_crtc_state *old_crtc_state,
  10823. struct drm_crtc_state *new_crtc_state)
  10824. {
  10825. struct drm_device *dev = crtc->dev;
  10826. struct drm_i915_private *dev_priv = dev->dev_private;
  10827. struct intel_encoder *encoder;
  10828. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10829. struct intel_crtc_state *pipe_config, *sw_config;
  10830. struct drm_atomic_state *old_state;
  10831. bool active;
  10832. old_state = old_crtc_state->state;
  10833. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10834. pipe_config = to_intel_crtc_state(old_crtc_state);
  10835. memset(pipe_config, 0, sizeof(*pipe_config));
  10836. pipe_config->base.crtc = crtc;
  10837. pipe_config->base.state = old_state;
  10838. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  10839. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10840. /* hw state is inconsistent with the pipe quirk */
  10841. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10842. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10843. active = new_crtc_state->active;
  10844. I915_STATE_WARN(new_crtc_state->active != active,
  10845. "crtc active state doesn't match with hw state "
  10846. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10847. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10848. "transitional active state does not match atomic hw state "
  10849. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10850. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10851. enum pipe pipe;
  10852. active = encoder->get_hw_state(encoder, &pipe);
  10853. I915_STATE_WARN(active != new_crtc_state->active,
  10854. "[ENCODER:%i] active %i with crtc active %i\n",
  10855. encoder->base.base.id, active, new_crtc_state->active);
  10856. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10857. "Encoder connected to wrong pipe %c\n",
  10858. pipe_name(pipe));
  10859. if (active)
  10860. encoder->get_config(encoder, pipe_config);
  10861. }
  10862. if (!new_crtc_state->active)
  10863. return;
  10864. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10865. sw_config = to_intel_crtc_state(crtc->state);
  10866. if (!intel_pipe_config_compare(dev, sw_config,
  10867. pipe_config, false)) {
  10868. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10869. intel_dump_pipe_config(intel_crtc, pipe_config,
  10870. "[hw state]");
  10871. intel_dump_pipe_config(intel_crtc, sw_config,
  10872. "[sw state]");
  10873. }
  10874. }
  10875. static void
  10876. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10877. struct intel_shared_dpll *pll,
  10878. struct drm_crtc *crtc,
  10879. struct drm_crtc_state *new_state)
  10880. {
  10881. struct intel_dpll_hw_state dpll_hw_state;
  10882. unsigned crtc_mask;
  10883. bool active;
  10884. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10885. DRM_DEBUG_KMS("%s\n", pll->name);
  10886. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10887. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10888. I915_STATE_WARN(!pll->on && pll->active_mask,
  10889. "pll in active use but not on in sw tracking\n");
  10890. I915_STATE_WARN(pll->on && !pll->active_mask,
  10891. "pll is on but not used by any active crtc\n");
  10892. I915_STATE_WARN(pll->on != active,
  10893. "pll on state mismatch (expected %i, found %i)\n",
  10894. pll->on, active);
  10895. }
  10896. if (!crtc) {
  10897. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10898. "more active pll users than references: %x vs %x\n",
  10899. pll->active_mask, pll->config.crtc_mask);
  10900. return;
  10901. }
  10902. crtc_mask = 1 << drm_crtc_index(crtc);
  10903. if (new_state->active)
  10904. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10905. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10906. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10907. else
  10908. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10909. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10910. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10911. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10912. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10913. crtc_mask, pll->config.crtc_mask);
  10914. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10915. &dpll_hw_state,
  10916. sizeof(dpll_hw_state)),
  10917. "pll hw state mismatch\n");
  10918. }
  10919. static void
  10920. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10921. struct drm_crtc_state *old_crtc_state,
  10922. struct drm_crtc_state *new_crtc_state)
  10923. {
  10924. struct drm_i915_private *dev_priv = dev->dev_private;
  10925. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10926. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10927. if (new_state->shared_dpll)
  10928. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10929. if (old_state->shared_dpll &&
  10930. old_state->shared_dpll != new_state->shared_dpll) {
  10931. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10932. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10933. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10934. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10935. pipe_name(drm_crtc_index(crtc)));
  10936. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10937. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10938. pipe_name(drm_crtc_index(crtc)));
  10939. }
  10940. }
  10941. static void
  10942. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10943. struct drm_crtc_state *old_state,
  10944. struct drm_crtc_state *new_state)
  10945. {
  10946. if (!needs_modeset(new_state) &&
  10947. !to_intel_crtc_state(new_state)->update_pipe)
  10948. return;
  10949. verify_wm_state(crtc, new_state);
  10950. verify_connector_state(crtc->dev, crtc);
  10951. verify_crtc_state(crtc, old_state, new_state);
  10952. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10953. }
  10954. static void
  10955. verify_disabled_dpll_state(struct drm_device *dev)
  10956. {
  10957. struct drm_i915_private *dev_priv = dev->dev_private;
  10958. int i;
  10959. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10960. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10961. }
  10962. static void
  10963. intel_modeset_verify_disabled(struct drm_device *dev)
  10964. {
  10965. verify_encoder_state(dev);
  10966. verify_connector_state(dev, NULL);
  10967. verify_disabled_dpll_state(dev);
  10968. }
  10969. static void update_scanline_offset(struct intel_crtc *crtc)
  10970. {
  10971. struct drm_device *dev = crtc->base.dev;
  10972. /*
  10973. * The scanline counter increments at the leading edge of hsync.
  10974. *
  10975. * On most platforms it starts counting from vtotal-1 on the
  10976. * first active line. That means the scanline counter value is
  10977. * always one less than what we would expect. Ie. just after
  10978. * start of vblank, which also occurs at start of hsync (on the
  10979. * last active line), the scanline counter will read vblank_start-1.
  10980. *
  10981. * On gen2 the scanline counter starts counting from 1 instead
  10982. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10983. * to keep the value positive), instead of adding one.
  10984. *
  10985. * On HSW+ the behaviour of the scanline counter depends on the output
  10986. * type. For DP ports it behaves like most other platforms, but on HDMI
  10987. * there's an extra 1 line difference. So we need to add two instead of
  10988. * one to the value.
  10989. */
  10990. if (IS_GEN2(dev)) {
  10991. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10992. int vtotal;
  10993. vtotal = adjusted_mode->crtc_vtotal;
  10994. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10995. vtotal /= 2;
  10996. crtc->scanline_offset = vtotal - 1;
  10997. } else if (HAS_DDI(dev) &&
  10998. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10999. crtc->scanline_offset = 2;
  11000. } else
  11001. crtc->scanline_offset = 1;
  11002. }
  11003. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11004. {
  11005. struct drm_device *dev = state->dev;
  11006. struct drm_i915_private *dev_priv = to_i915(dev);
  11007. struct intel_shared_dpll_config *shared_dpll = NULL;
  11008. struct drm_crtc *crtc;
  11009. struct drm_crtc_state *crtc_state;
  11010. int i;
  11011. if (!dev_priv->display.crtc_compute_clock)
  11012. return;
  11013. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11014. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11015. struct intel_shared_dpll *old_dpll =
  11016. to_intel_crtc_state(crtc->state)->shared_dpll;
  11017. if (!needs_modeset(crtc_state))
  11018. continue;
  11019. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11020. if (!old_dpll)
  11021. continue;
  11022. if (!shared_dpll)
  11023. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11024. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11025. }
  11026. }
  11027. /*
  11028. * This implements the workaround described in the "notes" section of the mode
  11029. * set sequence documentation. When going from no pipes or single pipe to
  11030. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11031. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11032. */
  11033. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11034. {
  11035. struct drm_crtc_state *crtc_state;
  11036. struct intel_crtc *intel_crtc;
  11037. struct drm_crtc *crtc;
  11038. struct intel_crtc_state *first_crtc_state = NULL;
  11039. struct intel_crtc_state *other_crtc_state = NULL;
  11040. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11041. int i;
  11042. /* look at all crtc's that are going to be enabled in during modeset */
  11043. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11044. intel_crtc = to_intel_crtc(crtc);
  11045. if (!crtc_state->active || !needs_modeset(crtc_state))
  11046. continue;
  11047. if (first_crtc_state) {
  11048. other_crtc_state = to_intel_crtc_state(crtc_state);
  11049. break;
  11050. } else {
  11051. first_crtc_state = to_intel_crtc_state(crtc_state);
  11052. first_pipe = intel_crtc->pipe;
  11053. }
  11054. }
  11055. /* No workaround needed? */
  11056. if (!first_crtc_state)
  11057. return 0;
  11058. /* w/a possibly needed, check how many crtc's are already enabled. */
  11059. for_each_intel_crtc(state->dev, intel_crtc) {
  11060. struct intel_crtc_state *pipe_config;
  11061. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11062. if (IS_ERR(pipe_config))
  11063. return PTR_ERR(pipe_config);
  11064. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11065. if (!pipe_config->base.active ||
  11066. needs_modeset(&pipe_config->base))
  11067. continue;
  11068. /* 2 or more enabled crtcs means no need for w/a */
  11069. if (enabled_pipe != INVALID_PIPE)
  11070. return 0;
  11071. enabled_pipe = intel_crtc->pipe;
  11072. }
  11073. if (enabled_pipe != INVALID_PIPE)
  11074. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11075. else if (other_crtc_state)
  11076. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11077. return 0;
  11078. }
  11079. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11080. {
  11081. struct drm_crtc *crtc;
  11082. struct drm_crtc_state *crtc_state;
  11083. int ret = 0;
  11084. /* add all active pipes to the state */
  11085. for_each_crtc(state->dev, crtc) {
  11086. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11087. if (IS_ERR(crtc_state))
  11088. return PTR_ERR(crtc_state);
  11089. if (!crtc_state->active || needs_modeset(crtc_state))
  11090. continue;
  11091. crtc_state->mode_changed = true;
  11092. ret = drm_atomic_add_affected_connectors(state, crtc);
  11093. if (ret)
  11094. break;
  11095. ret = drm_atomic_add_affected_planes(state, crtc);
  11096. if (ret)
  11097. break;
  11098. }
  11099. return ret;
  11100. }
  11101. static int intel_modeset_checks(struct drm_atomic_state *state)
  11102. {
  11103. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11104. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11105. struct drm_crtc *crtc;
  11106. struct drm_crtc_state *crtc_state;
  11107. int ret = 0, i;
  11108. if (!check_digital_port_conflicts(state)) {
  11109. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11110. return -EINVAL;
  11111. }
  11112. intel_state->modeset = true;
  11113. intel_state->active_crtcs = dev_priv->active_crtcs;
  11114. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11115. if (crtc_state->active)
  11116. intel_state->active_crtcs |= 1 << i;
  11117. else
  11118. intel_state->active_crtcs &= ~(1 << i);
  11119. if (crtc_state->active != crtc->state->active)
  11120. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11121. }
  11122. /*
  11123. * See if the config requires any additional preparation, e.g.
  11124. * to adjust global state with pipes off. We need to do this
  11125. * here so we can get the modeset_pipe updated config for the new
  11126. * mode set on this crtc. For other crtcs we need to use the
  11127. * adjusted_mode bits in the crtc directly.
  11128. */
  11129. if (dev_priv->display.modeset_calc_cdclk) {
  11130. ret = dev_priv->display.modeset_calc_cdclk(state);
  11131. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11132. ret = intel_modeset_all_pipes(state);
  11133. if (ret < 0)
  11134. return ret;
  11135. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11136. intel_state->cdclk, intel_state->dev_cdclk);
  11137. } else
  11138. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11139. intel_modeset_clear_plls(state);
  11140. if (IS_HASWELL(dev_priv))
  11141. return haswell_mode_set_planes_workaround(state);
  11142. return 0;
  11143. }
  11144. /*
  11145. * Handle calculation of various watermark data at the end of the atomic check
  11146. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11147. * handlers to ensure that all derived state has been updated.
  11148. */
  11149. static int calc_watermark_data(struct drm_atomic_state *state)
  11150. {
  11151. struct drm_device *dev = state->dev;
  11152. struct drm_i915_private *dev_priv = to_i915(dev);
  11153. /* Is there platform-specific watermark information to calculate? */
  11154. if (dev_priv->display.compute_global_watermarks)
  11155. return dev_priv->display.compute_global_watermarks(state);
  11156. return 0;
  11157. }
  11158. /**
  11159. * intel_atomic_check - validate state object
  11160. * @dev: drm device
  11161. * @state: state to validate
  11162. */
  11163. static int intel_atomic_check(struct drm_device *dev,
  11164. struct drm_atomic_state *state)
  11165. {
  11166. struct drm_i915_private *dev_priv = to_i915(dev);
  11167. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11168. struct drm_crtc *crtc;
  11169. struct drm_crtc_state *crtc_state;
  11170. int ret, i;
  11171. bool any_ms = false;
  11172. ret = drm_atomic_helper_check_modeset(dev, state);
  11173. if (ret)
  11174. return ret;
  11175. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11176. struct intel_crtc_state *pipe_config =
  11177. to_intel_crtc_state(crtc_state);
  11178. /* Catch I915_MODE_FLAG_INHERITED */
  11179. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11180. crtc_state->mode_changed = true;
  11181. if (!crtc_state->enable) {
  11182. if (needs_modeset(crtc_state))
  11183. any_ms = true;
  11184. continue;
  11185. }
  11186. if (!needs_modeset(crtc_state))
  11187. continue;
  11188. /* FIXME: For only active_changed we shouldn't need to do any
  11189. * state recomputation at all. */
  11190. ret = drm_atomic_add_affected_connectors(state, crtc);
  11191. if (ret)
  11192. return ret;
  11193. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11194. if (ret) {
  11195. intel_dump_pipe_config(to_intel_crtc(crtc),
  11196. pipe_config, "[failed]");
  11197. return ret;
  11198. }
  11199. if (i915.fastboot &&
  11200. intel_pipe_config_compare(dev,
  11201. to_intel_crtc_state(crtc->state),
  11202. pipe_config, true)) {
  11203. crtc_state->mode_changed = false;
  11204. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11205. }
  11206. if (needs_modeset(crtc_state)) {
  11207. any_ms = true;
  11208. ret = drm_atomic_add_affected_planes(state, crtc);
  11209. if (ret)
  11210. return ret;
  11211. }
  11212. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11213. needs_modeset(crtc_state) ?
  11214. "[modeset]" : "[fastset]");
  11215. }
  11216. if (any_ms) {
  11217. ret = intel_modeset_checks(state);
  11218. if (ret)
  11219. return ret;
  11220. } else
  11221. intel_state->cdclk = dev_priv->cdclk_freq;
  11222. ret = drm_atomic_helper_check_planes(dev, state);
  11223. if (ret)
  11224. return ret;
  11225. intel_fbc_choose_crtc(dev_priv, state);
  11226. return calc_watermark_data(state);
  11227. }
  11228. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11229. struct drm_atomic_state *state,
  11230. bool async)
  11231. {
  11232. struct drm_i915_private *dev_priv = dev->dev_private;
  11233. struct drm_plane_state *plane_state;
  11234. struct drm_crtc_state *crtc_state;
  11235. struct drm_plane *plane;
  11236. struct drm_crtc *crtc;
  11237. int i, ret;
  11238. if (async) {
  11239. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11240. return -EINVAL;
  11241. }
  11242. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11243. if (state->legacy_cursor_update)
  11244. continue;
  11245. ret = intel_crtc_wait_for_pending_flips(crtc);
  11246. if (ret)
  11247. return ret;
  11248. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11249. flush_workqueue(dev_priv->wq);
  11250. }
  11251. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11252. if (ret)
  11253. return ret;
  11254. ret = drm_atomic_helper_prepare_planes(dev, state);
  11255. mutex_unlock(&dev->struct_mutex);
  11256. if (!ret && !async) {
  11257. for_each_plane_in_state(state, plane, plane_state, i) {
  11258. struct intel_plane_state *intel_plane_state =
  11259. to_intel_plane_state(plane_state);
  11260. if (!intel_plane_state->wait_req)
  11261. continue;
  11262. ret = __i915_wait_request(intel_plane_state->wait_req,
  11263. true, NULL, NULL);
  11264. if (ret) {
  11265. /* Any hang should be swallowed by the wait */
  11266. WARN_ON(ret == -EIO);
  11267. mutex_lock(&dev->struct_mutex);
  11268. drm_atomic_helper_cleanup_planes(dev, state);
  11269. mutex_unlock(&dev->struct_mutex);
  11270. break;
  11271. }
  11272. }
  11273. }
  11274. return ret;
  11275. }
  11276. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11277. struct drm_i915_private *dev_priv,
  11278. unsigned crtc_mask)
  11279. {
  11280. unsigned last_vblank_count[I915_MAX_PIPES];
  11281. enum pipe pipe;
  11282. int ret;
  11283. if (!crtc_mask)
  11284. return;
  11285. for_each_pipe(dev_priv, pipe) {
  11286. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11287. if (!((1 << pipe) & crtc_mask))
  11288. continue;
  11289. ret = drm_crtc_vblank_get(crtc);
  11290. if (WARN_ON(ret != 0)) {
  11291. crtc_mask &= ~(1 << pipe);
  11292. continue;
  11293. }
  11294. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11295. }
  11296. for_each_pipe(dev_priv, pipe) {
  11297. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11298. long lret;
  11299. if (!((1 << pipe) & crtc_mask))
  11300. continue;
  11301. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11302. last_vblank_count[pipe] !=
  11303. drm_crtc_vblank_count(crtc),
  11304. msecs_to_jiffies(50));
  11305. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11306. drm_crtc_vblank_put(crtc);
  11307. }
  11308. }
  11309. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11310. {
  11311. /* fb updated, need to unpin old fb */
  11312. if (crtc_state->fb_changed)
  11313. return true;
  11314. /* wm changes, need vblank before final wm's */
  11315. if (crtc_state->update_wm_post)
  11316. return true;
  11317. /*
  11318. * cxsr is re-enabled after vblank.
  11319. * This is already handled by crtc_state->update_wm_post,
  11320. * but added for clarity.
  11321. */
  11322. if (crtc_state->disable_cxsr)
  11323. return true;
  11324. return false;
  11325. }
  11326. /**
  11327. * intel_atomic_commit - commit validated state object
  11328. * @dev: DRM device
  11329. * @state: the top-level driver state object
  11330. * @async: asynchronous commit
  11331. *
  11332. * This function commits a top-level state object that has been validated
  11333. * with drm_atomic_helper_check().
  11334. *
  11335. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11336. * we can only handle plane-related operations and do not yet support
  11337. * asynchronous commit.
  11338. *
  11339. * RETURNS
  11340. * Zero for success or -errno.
  11341. */
  11342. static int intel_atomic_commit(struct drm_device *dev,
  11343. struct drm_atomic_state *state,
  11344. bool async)
  11345. {
  11346. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11347. struct drm_i915_private *dev_priv = dev->dev_private;
  11348. struct drm_crtc_state *old_crtc_state;
  11349. struct drm_crtc *crtc;
  11350. struct intel_crtc_state *intel_cstate;
  11351. int ret = 0, i;
  11352. bool hw_check = intel_state->modeset;
  11353. unsigned long put_domains[I915_MAX_PIPES] = {};
  11354. unsigned crtc_vblank_mask = 0;
  11355. ret = intel_atomic_prepare_commit(dev, state, async);
  11356. if (ret) {
  11357. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11358. return ret;
  11359. }
  11360. drm_atomic_helper_swap_state(dev, state);
  11361. dev_priv->wm.distrust_bios_wm = false;
  11362. dev_priv->wm.skl_results = intel_state->wm_results;
  11363. intel_shared_dpll_commit(state);
  11364. if (intel_state->modeset) {
  11365. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11366. sizeof(intel_state->min_pixclk));
  11367. dev_priv->active_crtcs = intel_state->active_crtcs;
  11368. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11369. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11370. }
  11371. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11373. if (needs_modeset(crtc->state) ||
  11374. to_intel_crtc_state(crtc->state)->update_pipe) {
  11375. hw_check = true;
  11376. put_domains[to_intel_crtc(crtc)->pipe] =
  11377. modeset_get_crtc_power_domains(crtc,
  11378. to_intel_crtc_state(crtc->state));
  11379. }
  11380. if (!needs_modeset(crtc->state))
  11381. continue;
  11382. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11383. if (old_crtc_state->active) {
  11384. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11385. dev_priv->display.crtc_disable(crtc);
  11386. intel_crtc->active = false;
  11387. intel_fbc_disable(intel_crtc);
  11388. intel_disable_shared_dpll(intel_crtc);
  11389. /*
  11390. * Underruns don't always raise
  11391. * interrupts, so check manually.
  11392. */
  11393. intel_check_cpu_fifo_underruns(dev_priv);
  11394. intel_check_pch_fifo_underruns(dev_priv);
  11395. if (!crtc->state->active)
  11396. intel_update_watermarks(crtc);
  11397. }
  11398. }
  11399. /* Only after disabling all output pipelines that will be changed can we
  11400. * update the the output configuration. */
  11401. intel_modeset_update_crtc_state(state);
  11402. if (intel_state->modeset) {
  11403. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11404. if (dev_priv->display.modeset_commit_cdclk &&
  11405. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11406. dev_priv->display.modeset_commit_cdclk(state);
  11407. intel_modeset_verify_disabled(dev);
  11408. }
  11409. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11410. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11412. bool modeset = needs_modeset(crtc->state);
  11413. struct intel_crtc_state *pipe_config =
  11414. to_intel_crtc_state(crtc->state);
  11415. bool update_pipe = !modeset && pipe_config->update_pipe;
  11416. if (modeset && crtc->state->active) {
  11417. update_scanline_offset(to_intel_crtc(crtc));
  11418. dev_priv->display.crtc_enable(crtc);
  11419. }
  11420. if (!modeset)
  11421. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11422. if (crtc->state->active &&
  11423. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11424. intel_fbc_enable(intel_crtc);
  11425. if (crtc->state->active &&
  11426. (crtc->state->planes_changed || update_pipe))
  11427. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11428. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11429. crtc_vblank_mask |= 1 << i;
  11430. }
  11431. /* FIXME: add subpixel order */
  11432. if (!state->legacy_cursor_update)
  11433. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11434. /*
  11435. * Now that the vblank has passed, we can go ahead and program the
  11436. * optimal watermarks on platforms that need two-step watermark
  11437. * programming.
  11438. *
  11439. * TODO: Move this (and other cleanup) to an async worker eventually.
  11440. */
  11441. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11442. intel_cstate = to_intel_crtc_state(crtc->state);
  11443. if (dev_priv->display.optimize_watermarks)
  11444. dev_priv->display.optimize_watermarks(intel_cstate);
  11445. }
  11446. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11447. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11448. if (put_domains[i])
  11449. modeset_put_power_domains(dev_priv, put_domains[i]);
  11450. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11451. }
  11452. if (intel_state->modeset)
  11453. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11454. mutex_lock(&dev->struct_mutex);
  11455. drm_atomic_helper_cleanup_planes(dev, state);
  11456. mutex_unlock(&dev->struct_mutex);
  11457. drm_atomic_state_free(state);
  11458. /* As one of the primary mmio accessors, KMS has a high likelihood
  11459. * of triggering bugs in unclaimed access. After we finish
  11460. * modesetting, see if an error has been flagged, and if so
  11461. * enable debugging for the next modeset - and hope we catch
  11462. * the culprit.
  11463. *
  11464. * XXX note that we assume display power is on at this point.
  11465. * This might hold true now but we need to add pm helper to check
  11466. * unclaimed only when the hardware is on, as atomic commits
  11467. * can happen also when the device is completely off.
  11468. */
  11469. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11470. return 0;
  11471. }
  11472. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11473. {
  11474. struct drm_device *dev = crtc->dev;
  11475. struct drm_atomic_state *state;
  11476. struct drm_crtc_state *crtc_state;
  11477. int ret;
  11478. state = drm_atomic_state_alloc(dev);
  11479. if (!state) {
  11480. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11481. crtc->base.id);
  11482. return;
  11483. }
  11484. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11485. retry:
  11486. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11487. ret = PTR_ERR_OR_ZERO(crtc_state);
  11488. if (!ret) {
  11489. if (!crtc_state->active)
  11490. goto out;
  11491. crtc_state->mode_changed = true;
  11492. ret = drm_atomic_commit(state);
  11493. }
  11494. if (ret == -EDEADLK) {
  11495. drm_atomic_state_clear(state);
  11496. drm_modeset_backoff(state->acquire_ctx);
  11497. goto retry;
  11498. }
  11499. if (ret)
  11500. out:
  11501. drm_atomic_state_free(state);
  11502. }
  11503. #undef for_each_intel_crtc_masked
  11504. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11505. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11506. .set_config = drm_atomic_helper_set_config,
  11507. .set_property = drm_atomic_helper_crtc_set_property,
  11508. .destroy = intel_crtc_destroy,
  11509. .page_flip = intel_crtc_page_flip,
  11510. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11511. .atomic_destroy_state = intel_crtc_destroy_state,
  11512. };
  11513. /**
  11514. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11515. * @plane: drm plane to prepare for
  11516. * @fb: framebuffer to prepare for presentation
  11517. *
  11518. * Prepares a framebuffer for usage on a display plane. Generally this
  11519. * involves pinning the underlying object and updating the frontbuffer tracking
  11520. * bits. Some older platforms need special physical address handling for
  11521. * cursor planes.
  11522. *
  11523. * Must be called with struct_mutex held.
  11524. *
  11525. * Returns 0 on success, negative error code on failure.
  11526. */
  11527. int
  11528. intel_prepare_plane_fb(struct drm_plane *plane,
  11529. const struct drm_plane_state *new_state)
  11530. {
  11531. struct drm_device *dev = plane->dev;
  11532. struct drm_framebuffer *fb = new_state->fb;
  11533. struct intel_plane *intel_plane = to_intel_plane(plane);
  11534. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11535. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11536. int ret = 0;
  11537. if (!obj && !old_obj)
  11538. return 0;
  11539. if (old_obj) {
  11540. struct drm_crtc_state *crtc_state =
  11541. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11542. /* Big Hammer, we also need to ensure that any pending
  11543. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11544. * current scanout is retired before unpinning the old
  11545. * framebuffer. Note that we rely on userspace rendering
  11546. * into the buffer attached to the pipe they are waiting
  11547. * on. If not, userspace generates a GPU hang with IPEHR
  11548. * point to the MI_WAIT_FOR_EVENT.
  11549. *
  11550. * This should only fail upon a hung GPU, in which case we
  11551. * can safely continue.
  11552. */
  11553. if (needs_modeset(crtc_state))
  11554. ret = i915_gem_object_wait_rendering(old_obj, true);
  11555. if (ret) {
  11556. /* GPU hangs should have been swallowed by the wait */
  11557. WARN_ON(ret == -EIO);
  11558. return ret;
  11559. }
  11560. }
  11561. /* For framebuffer backed by dmabuf, wait for fence */
  11562. if (obj && obj->base.dma_buf) {
  11563. long lret;
  11564. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11565. false, true,
  11566. MAX_SCHEDULE_TIMEOUT);
  11567. if (lret == -ERESTARTSYS)
  11568. return lret;
  11569. WARN(lret < 0, "waiting returns %li\n", lret);
  11570. }
  11571. if (!obj) {
  11572. ret = 0;
  11573. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11574. INTEL_INFO(dev)->cursor_needs_physical) {
  11575. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11576. ret = i915_gem_object_attach_phys(obj, align);
  11577. if (ret)
  11578. DRM_DEBUG_KMS("failed to attach phys object\n");
  11579. } else {
  11580. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11581. }
  11582. if (ret == 0) {
  11583. if (obj) {
  11584. struct intel_plane_state *plane_state =
  11585. to_intel_plane_state(new_state);
  11586. i915_gem_request_assign(&plane_state->wait_req,
  11587. obj->last_write_req);
  11588. }
  11589. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11590. }
  11591. return ret;
  11592. }
  11593. /**
  11594. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11595. * @plane: drm plane to clean up for
  11596. * @fb: old framebuffer that was on plane
  11597. *
  11598. * Cleans up a framebuffer that has just been removed from a plane.
  11599. *
  11600. * Must be called with struct_mutex held.
  11601. */
  11602. void
  11603. intel_cleanup_plane_fb(struct drm_plane *plane,
  11604. const struct drm_plane_state *old_state)
  11605. {
  11606. struct drm_device *dev = plane->dev;
  11607. struct intel_plane *intel_plane = to_intel_plane(plane);
  11608. struct intel_plane_state *old_intel_state;
  11609. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11610. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11611. old_intel_state = to_intel_plane_state(old_state);
  11612. if (!obj && !old_obj)
  11613. return;
  11614. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11615. !INTEL_INFO(dev)->cursor_needs_physical))
  11616. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11617. /* prepare_fb aborted? */
  11618. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11619. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11620. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11621. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11622. }
  11623. int
  11624. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11625. {
  11626. int max_scale;
  11627. struct drm_device *dev;
  11628. struct drm_i915_private *dev_priv;
  11629. int crtc_clock, cdclk;
  11630. if (!intel_crtc || !crtc_state->base.enable)
  11631. return DRM_PLANE_HELPER_NO_SCALING;
  11632. dev = intel_crtc->base.dev;
  11633. dev_priv = dev->dev_private;
  11634. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11635. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11636. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11637. return DRM_PLANE_HELPER_NO_SCALING;
  11638. /*
  11639. * skl max scale is lower of:
  11640. * close to 3 but not 3, -1 is for that purpose
  11641. * or
  11642. * cdclk/crtc_clock
  11643. */
  11644. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11645. return max_scale;
  11646. }
  11647. static int
  11648. intel_check_primary_plane(struct drm_plane *plane,
  11649. struct intel_crtc_state *crtc_state,
  11650. struct intel_plane_state *state)
  11651. {
  11652. struct drm_crtc *crtc = state->base.crtc;
  11653. struct drm_framebuffer *fb = state->base.fb;
  11654. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11655. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11656. bool can_position = false;
  11657. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11658. /* use scaler when colorkey is not required */
  11659. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11660. min_scale = 1;
  11661. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11662. }
  11663. can_position = true;
  11664. }
  11665. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11666. &state->dst, &state->clip,
  11667. min_scale, max_scale,
  11668. can_position, true,
  11669. &state->visible);
  11670. }
  11671. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11672. struct drm_crtc_state *old_crtc_state)
  11673. {
  11674. struct drm_device *dev = crtc->dev;
  11675. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11676. struct intel_crtc_state *old_intel_state =
  11677. to_intel_crtc_state(old_crtc_state);
  11678. bool modeset = needs_modeset(crtc->state);
  11679. /* Perform vblank evasion around commit operation */
  11680. intel_pipe_update_start(intel_crtc);
  11681. if (modeset)
  11682. return;
  11683. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11684. intel_color_set_csc(crtc->state);
  11685. intel_color_load_luts(crtc->state);
  11686. }
  11687. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11688. intel_update_pipe_config(intel_crtc, old_intel_state);
  11689. else if (INTEL_INFO(dev)->gen >= 9)
  11690. skl_detach_scalers(intel_crtc);
  11691. }
  11692. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11693. struct drm_crtc_state *old_crtc_state)
  11694. {
  11695. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11696. intel_pipe_update_end(intel_crtc);
  11697. }
  11698. /**
  11699. * intel_plane_destroy - destroy a plane
  11700. * @plane: plane to destroy
  11701. *
  11702. * Common destruction function for all types of planes (primary, cursor,
  11703. * sprite).
  11704. */
  11705. void intel_plane_destroy(struct drm_plane *plane)
  11706. {
  11707. struct intel_plane *intel_plane = to_intel_plane(plane);
  11708. drm_plane_cleanup(plane);
  11709. kfree(intel_plane);
  11710. }
  11711. const struct drm_plane_funcs intel_plane_funcs = {
  11712. .update_plane = drm_atomic_helper_update_plane,
  11713. .disable_plane = drm_atomic_helper_disable_plane,
  11714. .destroy = intel_plane_destroy,
  11715. .set_property = drm_atomic_helper_plane_set_property,
  11716. .atomic_get_property = intel_plane_atomic_get_property,
  11717. .atomic_set_property = intel_plane_atomic_set_property,
  11718. .atomic_duplicate_state = intel_plane_duplicate_state,
  11719. .atomic_destroy_state = intel_plane_destroy_state,
  11720. };
  11721. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11722. int pipe)
  11723. {
  11724. struct intel_plane *primary = NULL;
  11725. struct intel_plane_state *state = NULL;
  11726. const uint32_t *intel_primary_formats;
  11727. unsigned int num_formats;
  11728. int ret;
  11729. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11730. if (!primary)
  11731. goto fail;
  11732. state = intel_create_plane_state(&primary->base);
  11733. if (!state)
  11734. goto fail;
  11735. primary->base.state = &state->base;
  11736. primary->can_scale = false;
  11737. primary->max_downscale = 1;
  11738. if (INTEL_INFO(dev)->gen >= 9) {
  11739. primary->can_scale = true;
  11740. state->scaler_id = -1;
  11741. }
  11742. primary->pipe = pipe;
  11743. primary->plane = pipe;
  11744. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11745. primary->check_plane = intel_check_primary_plane;
  11746. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11747. primary->plane = !pipe;
  11748. if (INTEL_INFO(dev)->gen >= 9) {
  11749. intel_primary_formats = skl_primary_formats;
  11750. num_formats = ARRAY_SIZE(skl_primary_formats);
  11751. primary->update_plane = skylake_update_primary_plane;
  11752. primary->disable_plane = skylake_disable_primary_plane;
  11753. } else if (HAS_PCH_SPLIT(dev)) {
  11754. intel_primary_formats = i965_primary_formats;
  11755. num_formats = ARRAY_SIZE(i965_primary_formats);
  11756. primary->update_plane = ironlake_update_primary_plane;
  11757. primary->disable_plane = i9xx_disable_primary_plane;
  11758. } else if (INTEL_INFO(dev)->gen >= 4) {
  11759. intel_primary_formats = i965_primary_formats;
  11760. num_formats = ARRAY_SIZE(i965_primary_formats);
  11761. primary->update_plane = i9xx_update_primary_plane;
  11762. primary->disable_plane = i9xx_disable_primary_plane;
  11763. } else {
  11764. intel_primary_formats = i8xx_primary_formats;
  11765. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11766. primary->update_plane = i9xx_update_primary_plane;
  11767. primary->disable_plane = i9xx_disable_primary_plane;
  11768. }
  11769. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11770. &intel_plane_funcs,
  11771. intel_primary_formats, num_formats,
  11772. DRM_PLANE_TYPE_PRIMARY, NULL);
  11773. if (ret)
  11774. goto fail;
  11775. if (INTEL_INFO(dev)->gen >= 4)
  11776. intel_create_rotation_property(dev, primary);
  11777. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11778. return &primary->base;
  11779. fail:
  11780. kfree(state);
  11781. kfree(primary);
  11782. return NULL;
  11783. }
  11784. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11785. {
  11786. if (!dev->mode_config.rotation_property) {
  11787. unsigned long flags = BIT(DRM_ROTATE_0) |
  11788. BIT(DRM_ROTATE_180);
  11789. if (INTEL_INFO(dev)->gen >= 9)
  11790. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11791. dev->mode_config.rotation_property =
  11792. drm_mode_create_rotation_property(dev, flags);
  11793. }
  11794. if (dev->mode_config.rotation_property)
  11795. drm_object_attach_property(&plane->base.base,
  11796. dev->mode_config.rotation_property,
  11797. plane->base.state->rotation);
  11798. }
  11799. static int
  11800. intel_check_cursor_plane(struct drm_plane *plane,
  11801. struct intel_crtc_state *crtc_state,
  11802. struct intel_plane_state *state)
  11803. {
  11804. struct drm_crtc *crtc = crtc_state->base.crtc;
  11805. struct drm_framebuffer *fb = state->base.fb;
  11806. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11807. enum pipe pipe = to_intel_plane(plane)->pipe;
  11808. unsigned stride;
  11809. int ret;
  11810. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11811. &state->dst, &state->clip,
  11812. DRM_PLANE_HELPER_NO_SCALING,
  11813. DRM_PLANE_HELPER_NO_SCALING,
  11814. true, true, &state->visible);
  11815. if (ret)
  11816. return ret;
  11817. /* if we want to turn off the cursor ignore width and height */
  11818. if (!obj)
  11819. return 0;
  11820. /* Check for which cursor types we support */
  11821. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11822. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11823. state->base.crtc_w, state->base.crtc_h);
  11824. return -EINVAL;
  11825. }
  11826. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11827. if (obj->base.size < stride * state->base.crtc_h) {
  11828. DRM_DEBUG_KMS("buffer is too small\n");
  11829. return -ENOMEM;
  11830. }
  11831. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11832. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11833. return -EINVAL;
  11834. }
  11835. /*
  11836. * There's something wrong with the cursor on CHV pipe C.
  11837. * If it straddles the left edge of the screen then
  11838. * moving it away from the edge or disabling it often
  11839. * results in a pipe underrun, and often that can lead to
  11840. * dead pipe (constant underrun reported, and it scans
  11841. * out just a solid color). To recover from that, the
  11842. * display power well must be turned off and on again.
  11843. * Refuse the put the cursor into that compromised position.
  11844. */
  11845. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11846. state->visible && state->base.crtc_x < 0) {
  11847. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11848. return -EINVAL;
  11849. }
  11850. return 0;
  11851. }
  11852. static void
  11853. intel_disable_cursor_plane(struct drm_plane *plane,
  11854. struct drm_crtc *crtc)
  11855. {
  11856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11857. intel_crtc->cursor_addr = 0;
  11858. intel_crtc_update_cursor(crtc, NULL);
  11859. }
  11860. static void
  11861. intel_update_cursor_plane(struct drm_plane *plane,
  11862. const struct intel_crtc_state *crtc_state,
  11863. const struct intel_plane_state *state)
  11864. {
  11865. struct drm_crtc *crtc = crtc_state->base.crtc;
  11866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11867. struct drm_device *dev = plane->dev;
  11868. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11869. uint32_t addr;
  11870. if (!obj)
  11871. addr = 0;
  11872. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11873. addr = i915_gem_obj_ggtt_offset(obj);
  11874. else
  11875. addr = obj->phys_handle->busaddr;
  11876. intel_crtc->cursor_addr = addr;
  11877. intel_crtc_update_cursor(crtc, state);
  11878. }
  11879. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11880. int pipe)
  11881. {
  11882. struct intel_plane *cursor = NULL;
  11883. struct intel_plane_state *state = NULL;
  11884. int ret;
  11885. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11886. if (!cursor)
  11887. goto fail;
  11888. state = intel_create_plane_state(&cursor->base);
  11889. if (!state)
  11890. goto fail;
  11891. cursor->base.state = &state->base;
  11892. cursor->can_scale = false;
  11893. cursor->max_downscale = 1;
  11894. cursor->pipe = pipe;
  11895. cursor->plane = pipe;
  11896. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11897. cursor->check_plane = intel_check_cursor_plane;
  11898. cursor->update_plane = intel_update_cursor_plane;
  11899. cursor->disable_plane = intel_disable_cursor_plane;
  11900. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  11901. &intel_plane_funcs,
  11902. intel_cursor_formats,
  11903. ARRAY_SIZE(intel_cursor_formats),
  11904. DRM_PLANE_TYPE_CURSOR, NULL);
  11905. if (ret)
  11906. goto fail;
  11907. if (INTEL_INFO(dev)->gen >= 4) {
  11908. if (!dev->mode_config.rotation_property)
  11909. dev->mode_config.rotation_property =
  11910. drm_mode_create_rotation_property(dev,
  11911. BIT(DRM_ROTATE_0) |
  11912. BIT(DRM_ROTATE_180));
  11913. if (dev->mode_config.rotation_property)
  11914. drm_object_attach_property(&cursor->base.base,
  11915. dev->mode_config.rotation_property,
  11916. state->base.rotation);
  11917. }
  11918. if (INTEL_INFO(dev)->gen >=9)
  11919. state->scaler_id = -1;
  11920. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11921. return &cursor->base;
  11922. fail:
  11923. kfree(state);
  11924. kfree(cursor);
  11925. return NULL;
  11926. }
  11927. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11928. struct intel_crtc_state *crtc_state)
  11929. {
  11930. int i;
  11931. struct intel_scaler *intel_scaler;
  11932. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11933. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11934. intel_scaler = &scaler_state->scalers[i];
  11935. intel_scaler->in_use = 0;
  11936. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11937. }
  11938. scaler_state->scaler_id = -1;
  11939. }
  11940. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11941. {
  11942. struct drm_i915_private *dev_priv = dev->dev_private;
  11943. struct intel_crtc *intel_crtc;
  11944. struct intel_crtc_state *crtc_state = NULL;
  11945. struct drm_plane *primary = NULL;
  11946. struct drm_plane *cursor = NULL;
  11947. int ret;
  11948. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11949. if (intel_crtc == NULL)
  11950. return;
  11951. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11952. if (!crtc_state)
  11953. goto fail;
  11954. intel_crtc->config = crtc_state;
  11955. intel_crtc->base.state = &crtc_state->base;
  11956. crtc_state->base.crtc = &intel_crtc->base;
  11957. /* initialize shared scalers */
  11958. if (INTEL_INFO(dev)->gen >= 9) {
  11959. if (pipe == PIPE_C)
  11960. intel_crtc->num_scalers = 1;
  11961. else
  11962. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11963. skl_init_scalers(dev, intel_crtc, crtc_state);
  11964. }
  11965. primary = intel_primary_plane_create(dev, pipe);
  11966. if (!primary)
  11967. goto fail;
  11968. cursor = intel_cursor_plane_create(dev, pipe);
  11969. if (!cursor)
  11970. goto fail;
  11971. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11972. cursor, &intel_crtc_funcs, NULL);
  11973. if (ret)
  11974. goto fail;
  11975. /*
  11976. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11977. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11978. */
  11979. intel_crtc->pipe = pipe;
  11980. intel_crtc->plane = pipe;
  11981. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11982. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11983. intel_crtc->plane = !pipe;
  11984. }
  11985. intel_crtc->cursor_base = ~0;
  11986. intel_crtc->cursor_cntl = ~0;
  11987. intel_crtc->cursor_size = ~0;
  11988. intel_crtc->wm.cxsr_allowed = true;
  11989. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11990. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11991. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11992. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11993. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11994. intel_color_init(&intel_crtc->base);
  11995. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11996. return;
  11997. fail:
  11998. if (primary)
  11999. drm_plane_cleanup(primary);
  12000. if (cursor)
  12001. drm_plane_cleanup(cursor);
  12002. kfree(crtc_state);
  12003. kfree(intel_crtc);
  12004. }
  12005. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12006. {
  12007. struct drm_encoder *encoder = connector->base.encoder;
  12008. struct drm_device *dev = connector->base.dev;
  12009. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12010. if (!encoder || WARN_ON(!encoder->crtc))
  12011. return INVALID_PIPE;
  12012. return to_intel_crtc(encoder->crtc)->pipe;
  12013. }
  12014. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12015. struct drm_file *file)
  12016. {
  12017. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12018. struct drm_crtc *drmmode_crtc;
  12019. struct intel_crtc *crtc;
  12020. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12021. if (!drmmode_crtc) {
  12022. DRM_ERROR("no such CRTC id\n");
  12023. return -ENOENT;
  12024. }
  12025. crtc = to_intel_crtc(drmmode_crtc);
  12026. pipe_from_crtc_id->pipe = crtc->pipe;
  12027. return 0;
  12028. }
  12029. static int intel_encoder_clones(struct intel_encoder *encoder)
  12030. {
  12031. struct drm_device *dev = encoder->base.dev;
  12032. struct intel_encoder *source_encoder;
  12033. int index_mask = 0;
  12034. int entry = 0;
  12035. for_each_intel_encoder(dev, source_encoder) {
  12036. if (encoders_cloneable(encoder, source_encoder))
  12037. index_mask |= (1 << entry);
  12038. entry++;
  12039. }
  12040. return index_mask;
  12041. }
  12042. static bool has_edp_a(struct drm_device *dev)
  12043. {
  12044. struct drm_i915_private *dev_priv = dev->dev_private;
  12045. if (!IS_MOBILE(dev))
  12046. return false;
  12047. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12048. return false;
  12049. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12050. return false;
  12051. return true;
  12052. }
  12053. static bool intel_crt_present(struct drm_device *dev)
  12054. {
  12055. struct drm_i915_private *dev_priv = dev->dev_private;
  12056. if (INTEL_INFO(dev)->gen >= 9)
  12057. return false;
  12058. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12059. return false;
  12060. if (IS_CHERRYVIEW(dev))
  12061. return false;
  12062. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12063. return false;
  12064. /* DDI E can't be used if DDI A requires 4 lanes */
  12065. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12066. return false;
  12067. if (!dev_priv->vbt.int_crt_support)
  12068. return false;
  12069. return true;
  12070. }
  12071. static void intel_setup_outputs(struct drm_device *dev)
  12072. {
  12073. struct drm_i915_private *dev_priv = dev->dev_private;
  12074. struct intel_encoder *encoder;
  12075. bool dpd_is_edp = false;
  12076. intel_lvds_init(dev);
  12077. if (intel_crt_present(dev))
  12078. intel_crt_init(dev);
  12079. if (IS_BROXTON(dev)) {
  12080. /*
  12081. * FIXME: Broxton doesn't support port detection via the
  12082. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12083. * detect the ports.
  12084. */
  12085. intel_ddi_init(dev, PORT_A);
  12086. intel_ddi_init(dev, PORT_B);
  12087. intel_ddi_init(dev, PORT_C);
  12088. intel_dsi_init(dev);
  12089. } else if (HAS_DDI(dev)) {
  12090. int found;
  12091. /*
  12092. * Haswell uses DDI functions to detect digital outputs.
  12093. * On SKL pre-D0 the strap isn't connected, so we assume
  12094. * it's there.
  12095. */
  12096. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12097. /* WaIgnoreDDIAStrap: skl */
  12098. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12099. intel_ddi_init(dev, PORT_A);
  12100. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12101. * register */
  12102. found = I915_READ(SFUSE_STRAP);
  12103. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12104. intel_ddi_init(dev, PORT_B);
  12105. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12106. intel_ddi_init(dev, PORT_C);
  12107. if (found & SFUSE_STRAP_DDID_DETECTED)
  12108. intel_ddi_init(dev, PORT_D);
  12109. /*
  12110. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12111. */
  12112. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12113. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12114. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12115. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12116. intel_ddi_init(dev, PORT_E);
  12117. } else if (HAS_PCH_SPLIT(dev)) {
  12118. int found;
  12119. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12120. if (has_edp_a(dev))
  12121. intel_dp_init(dev, DP_A, PORT_A);
  12122. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12123. /* PCH SDVOB multiplex with HDMIB */
  12124. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12125. if (!found)
  12126. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12127. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12128. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12129. }
  12130. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12131. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12132. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12133. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12134. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12135. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12136. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12137. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12138. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12139. /*
  12140. * The DP_DETECTED bit is the latched state of the DDC
  12141. * SDA pin at boot. However since eDP doesn't require DDC
  12142. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12143. * eDP ports may have been muxed to an alternate function.
  12144. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12145. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12146. * detect eDP ports.
  12147. */
  12148. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12149. !intel_dp_is_edp(dev, PORT_B))
  12150. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12151. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12152. intel_dp_is_edp(dev, PORT_B))
  12153. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12154. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12155. !intel_dp_is_edp(dev, PORT_C))
  12156. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12157. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12158. intel_dp_is_edp(dev, PORT_C))
  12159. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12160. if (IS_CHERRYVIEW(dev)) {
  12161. /* eDP not supported on port D, so don't check VBT */
  12162. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12163. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12164. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12165. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12166. }
  12167. intel_dsi_init(dev);
  12168. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12169. bool found = false;
  12170. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12171. DRM_DEBUG_KMS("probing SDVOB\n");
  12172. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12173. if (!found && IS_G4X(dev)) {
  12174. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12175. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12176. }
  12177. if (!found && IS_G4X(dev))
  12178. intel_dp_init(dev, DP_B, PORT_B);
  12179. }
  12180. /* Before G4X SDVOC doesn't have its own detect register */
  12181. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12182. DRM_DEBUG_KMS("probing SDVOC\n");
  12183. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12184. }
  12185. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12186. if (IS_G4X(dev)) {
  12187. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12188. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12189. }
  12190. if (IS_G4X(dev))
  12191. intel_dp_init(dev, DP_C, PORT_C);
  12192. }
  12193. if (IS_G4X(dev) &&
  12194. (I915_READ(DP_D) & DP_DETECTED))
  12195. intel_dp_init(dev, DP_D, PORT_D);
  12196. } else if (IS_GEN2(dev))
  12197. intel_dvo_init(dev);
  12198. if (SUPPORTS_TV(dev))
  12199. intel_tv_init(dev);
  12200. intel_psr_init(dev);
  12201. for_each_intel_encoder(dev, encoder) {
  12202. encoder->base.possible_crtcs = encoder->crtc_mask;
  12203. encoder->base.possible_clones =
  12204. intel_encoder_clones(encoder);
  12205. }
  12206. intel_init_pch_refclk(dev);
  12207. drm_helper_move_panel_connectors_to_head(dev);
  12208. }
  12209. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12210. {
  12211. struct drm_device *dev = fb->dev;
  12212. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12213. drm_framebuffer_cleanup(fb);
  12214. mutex_lock(&dev->struct_mutex);
  12215. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12216. drm_gem_object_unreference(&intel_fb->obj->base);
  12217. mutex_unlock(&dev->struct_mutex);
  12218. kfree(intel_fb);
  12219. }
  12220. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12221. struct drm_file *file,
  12222. unsigned int *handle)
  12223. {
  12224. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12225. struct drm_i915_gem_object *obj = intel_fb->obj;
  12226. if (obj->userptr.mm) {
  12227. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12228. return -EINVAL;
  12229. }
  12230. return drm_gem_handle_create(file, &obj->base, handle);
  12231. }
  12232. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12233. struct drm_file *file,
  12234. unsigned flags, unsigned color,
  12235. struct drm_clip_rect *clips,
  12236. unsigned num_clips)
  12237. {
  12238. struct drm_device *dev = fb->dev;
  12239. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12240. struct drm_i915_gem_object *obj = intel_fb->obj;
  12241. mutex_lock(&dev->struct_mutex);
  12242. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12243. mutex_unlock(&dev->struct_mutex);
  12244. return 0;
  12245. }
  12246. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12247. .destroy = intel_user_framebuffer_destroy,
  12248. .create_handle = intel_user_framebuffer_create_handle,
  12249. .dirty = intel_user_framebuffer_dirty,
  12250. };
  12251. static
  12252. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12253. uint32_t pixel_format)
  12254. {
  12255. u32 gen = INTEL_INFO(dev)->gen;
  12256. if (gen >= 9) {
  12257. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12258. /* "The stride in bytes must not exceed the of the size of 8K
  12259. * pixels and 32K bytes."
  12260. */
  12261. return min(8192 * cpp, 32768);
  12262. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12263. return 32*1024;
  12264. } else if (gen >= 4) {
  12265. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12266. return 16*1024;
  12267. else
  12268. return 32*1024;
  12269. } else if (gen >= 3) {
  12270. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12271. return 8*1024;
  12272. else
  12273. return 16*1024;
  12274. } else {
  12275. /* XXX DSPC is limited to 4k tiled */
  12276. return 8*1024;
  12277. }
  12278. }
  12279. static int intel_framebuffer_init(struct drm_device *dev,
  12280. struct intel_framebuffer *intel_fb,
  12281. struct drm_mode_fb_cmd2 *mode_cmd,
  12282. struct drm_i915_gem_object *obj)
  12283. {
  12284. struct drm_i915_private *dev_priv = to_i915(dev);
  12285. unsigned int aligned_height;
  12286. int ret;
  12287. u32 pitch_limit, stride_alignment;
  12288. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12289. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12290. /* Enforce that fb modifier and tiling mode match, but only for
  12291. * X-tiled. This is needed for FBC. */
  12292. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12293. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12294. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12295. return -EINVAL;
  12296. }
  12297. } else {
  12298. if (obj->tiling_mode == I915_TILING_X)
  12299. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12300. else if (obj->tiling_mode == I915_TILING_Y) {
  12301. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12302. return -EINVAL;
  12303. }
  12304. }
  12305. /* Passed in modifier sanity checking. */
  12306. switch (mode_cmd->modifier[0]) {
  12307. case I915_FORMAT_MOD_Y_TILED:
  12308. case I915_FORMAT_MOD_Yf_TILED:
  12309. if (INTEL_INFO(dev)->gen < 9) {
  12310. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12311. mode_cmd->modifier[0]);
  12312. return -EINVAL;
  12313. }
  12314. case DRM_FORMAT_MOD_NONE:
  12315. case I915_FORMAT_MOD_X_TILED:
  12316. break;
  12317. default:
  12318. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12319. mode_cmd->modifier[0]);
  12320. return -EINVAL;
  12321. }
  12322. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12323. mode_cmd->modifier[0],
  12324. mode_cmd->pixel_format);
  12325. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12326. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12327. mode_cmd->pitches[0], stride_alignment);
  12328. return -EINVAL;
  12329. }
  12330. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12331. mode_cmd->pixel_format);
  12332. if (mode_cmd->pitches[0] > pitch_limit) {
  12333. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12334. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12335. "tiled" : "linear",
  12336. mode_cmd->pitches[0], pitch_limit);
  12337. return -EINVAL;
  12338. }
  12339. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12340. mode_cmd->pitches[0] != obj->stride) {
  12341. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12342. mode_cmd->pitches[0], obj->stride);
  12343. return -EINVAL;
  12344. }
  12345. /* Reject formats not supported by any plane early. */
  12346. switch (mode_cmd->pixel_format) {
  12347. case DRM_FORMAT_C8:
  12348. case DRM_FORMAT_RGB565:
  12349. case DRM_FORMAT_XRGB8888:
  12350. case DRM_FORMAT_ARGB8888:
  12351. break;
  12352. case DRM_FORMAT_XRGB1555:
  12353. if (INTEL_INFO(dev)->gen > 3) {
  12354. DRM_DEBUG("unsupported pixel format: %s\n",
  12355. drm_get_format_name(mode_cmd->pixel_format));
  12356. return -EINVAL;
  12357. }
  12358. break;
  12359. case DRM_FORMAT_ABGR8888:
  12360. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12361. INTEL_INFO(dev)->gen < 9) {
  12362. DRM_DEBUG("unsupported pixel format: %s\n",
  12363. drm_get_format_name(mode_cmd->pixel_format));
  12364. return -EINVAL;
  12365. }
  12366. break;
  12367. case DRM_FORMAT_XBGR8888:
  12368. case DRM_FORMAT_XRGB2101010:
  12369. case DRM_FORMAT_XBGR2101010:
  12370. if (INTEL_INFO(dev)->gen < 4) {
  12371. DRM_DEBUG("unsupported pixel format: %s\n",
  12372. drm_get_format_name(mode_cmd->pixel_format));
  12373. return -EINVAL;
  12374. }
  12375. break;
  12376. case DRM_FORMAT_ABGR2101010:
  12377. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12378. DRM_DEBUG("unsupported pixel format: %s\n",
  12379. drm_get_format_name(mode_cmd->pixel_format));
  12380. return -EINVAL;
  12381. }
  12382. break;
  12383. case DRM_FORMAT_YUYV:
  12384. case DRM_FORMAT_UYVY:
  12385. case DRM_FORMAT_YVYU:
  12386. case DRM_FORMAT_VYUY:
  12387. if (INTEL_INFO(dev)->gen < 5) {
  12388. DRM_DEBUG("unsupported pixel format: %s\n",
  12389. drm_get_format_name(mode_cmd->pixel_format));
  12390. return -EINVAL;
  12391. }
  12392. break;
  12393. default:
  12394. DRM_DEBUG("unsupported pixel format: %s\n",
  12395. drm_get_format_name(mode_cmd->pixel_format));
  12396. return -EINVAL;
  12397. }
  12398. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12399. if (mode_cmd->offsets[0] != 0)
  12400. return -EINVAL;
  12401. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12402. mode_cmd->pixel_format,
  12403. mode_cmd->modifier[0]);
  12404. /* FIXME drm helper for size checks (especially planar formats)? */
  12405. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12406. return -EINVAL;
  12407. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12408. intel_fb->obj = obj;
  12409. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12410. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12411. if (ret) {
  12412. DRM_ERROR("framebuffer init failed %d\n", ret);
  12413. return ret;
  12414. }
  12415. intel_fb->obj->framebuffer_references++;
  12416. return 0;
  12417. }
  12418. static struct drm_framebuffer *
  12419. intel_user_framebuffer_create(struct drm_device *dev,
  12420. struct drm_file *filp,
  12421. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12422. {
  12423. struct drm_framebuffer *fb;
  12424. struct drm_i915_gem_object *obj;
  12425. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12426. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12427. mode_cmd.handles[0]));
  12428. if (&obj->base == NULL)
  12429. return ERR_PTR(-ENOENT);
  12430. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12431. if (IS_ERR(fb))
  12432. drm_gem_object_unreference_unlocked(&obj->base);
  12433. return fb;
  12434. }
  12435. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12436. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12437. {
  12438. }
  12439. #endif
  12440. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12441. .fb_create = intel_user_framebuffer_create,
  12442. .output_poll_changed = intel_fbdev_output_poll_changed,
  12443. .atomic_check = intel_atomic_check,
  12444. .atomic_commit = intel_atomic_commit,
  12445. .atomic_state_alloc = intel_atomic_state_alloc,
  12446. .atomic_state_clear = intel_atomic_state_clear,
  12447. };
  12448. /**
  12449. * intel_init_display_hooks - initialize the display modesetting hooks
  12450. * @dev_priv: device private
  12451. */
  12452. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12453. {
  12454. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12455. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12456. dev_priv->display.get_initial_plane_config =
  12457. skylake_get_initial_plane_config;
  12458. dev_priv->display.crtc_compute_clock =
  12459. haswell_crtc_compute_clock;
  12460. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12461. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12462. } else if (HAS_DDI(dev_priv)) {
  12463. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12464. dev_priv->display.get_initial_plane_config =
  12465. ironlake_get_initial_plane_config;
  12466. dev_priv->display.crtc_compute_clock =
  12467. haswell_crtc_compute_clock;
  12468. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12469. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12470. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12471. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12472. dev_priv->display.get_initial_plane_config =
  12473. ironlake_get_initial_plane_config;
  12474. dev_priv->display.crtc_compute_clock =
  12475. ironlake_crtc_compute_clock;
  12476. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12477. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12478. } else if (IS_CHERRYVIEW(dev_priv)) {
  12479. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12480. dev_priv->display.get_initial_plane_config =
  12481. i9xx_get_initial_plane_config;
  12482. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12483. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12484. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12485. } else if (IS_VALLEYVIEW(dev_priv)) {
  12486. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12487. dev_priv->display.get_initial_plane_config =
  12488. i9xx_get_initial_plane_config;
  12489. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12490. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12491. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12492. } else if (IS_G4X(dev_priv)) {
  12493. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12494. dev_priv->display.get_initial_plane_config =
  12495. i9xx_get_initial_plane_config;
  12496. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12497. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12498. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12499. } else if (IS_PINEVIEW(dev_priv)) {
  12500. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12501. dev_priv->display.get_initial_plane_config =
  12502. i9xx_get_initial_plane_config;
  12503. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12504. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12505. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12506. } else if (!IS_GEN2(dev_priv)) {
  12507. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12508. dev_priv->display.get_initial_plane_config =
  12509. i9xx_get_initial_plane_config;
  12510. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12511. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12512. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12513. } else {
  12514. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12515. dev_priv->display.get_initial_plane_config =
  12516. i9xx_get_initial_plane_config;
  12517. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12518. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12519. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12520. }
  12521. /* Returns the core display clock speed */
  12522. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12523. dev_priv->display.get_display_clock_speed =
  12524. skylake_get_display_clock_speed;
  12525. else if (IS_BROXTON(dev_priv))
  12526. dev_priv->display.get_display_clock_speed =
  12527. broxton_get_display_clock_speed;
  12528. else if (IS_BROADWELL(dev_priv))
  12529. dev_priv->display.get_display_clock_speed =
  12530. broadwell_get_display_clock_speed;
  12531. else if (IS_HASWELL(dev_priv))
  12532. dev_priv->display.get_display_clock_speed =
  12533. haswell_get_display_clock_speed;
  12534. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12535. dev_priv->display.get_display_clock_speed =
  12536. valleyview_get_display_clock_speed;
  12537. else if (IS_GEN5(dev_priv))
  12538. dev_priv->display.get_display_clock_speed =
  12539. ilk_get_display_clock_speed;
  12540. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12541. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12542. dev_priv->display.get_display_clock_speed =
  12543. i945_get_display_clock_speed;
  12544. else if (IS_GM45(dev_priv))
  12545. dev_priv->display.get_display_clock_speed =
  12546. gm45_get_display_clock_speed;
  12547. else if (IS_CRESTLINE(dev_priv))
  12548. dev_priv->display.get_display_clock_speed =
  12549. i965gm_get_display_clock_speed;
  12550. else if (IS_PINEVIEW(dev_priv))
  12551. dev_priv->display.get_display_clock_speed =
  12552. pnv_get_display_clock_speed;
  12553. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12554. dev_priv->display.get_display_clock_speed =
  12555. g33_get_display_clock_speed;
  12556. else if (IS_I915G(dev_priv))
  12557. dev_priv->display.get_display_clock_speed =
  12558. i915_get_display_clock_speed;
  12559. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12560. dev_priv->display.get_display_clock_speed =
  12561. i9xx_misc_get_display_clock_speed;
  12562. else if (IS_I915GM(dev_priv))
  12563. dev_priv->display.get_display_clock_speed =
  12564. i915gm_get_display_clock_speed;
  12565. else if (IS_I865G(dev_priv))
  12566. dev_priv->display.get_display_clock_speed =
  12567. i865_get_display_clock_speed;
  12568. else if (IS_I85X(dev_priv))
  12569. dev_priv->display.get_display_clock_speed =
  12570. i85x_get_display_clock_speed;
  12571. else { /* 830 */
  12572. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12573. dev_priv->display.get_display_clock_speed =
  12574. i830_get_display_clock_speed;
  12575. }
  12576. if (IS_GEN5(dev_priv)) {
  12577. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12578. } else if (IS_GEN6(dev_priv)) {
  12579. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12580. } else if (IS_IVYBRIDGE(dev_priv)) {
  12581. /* FIXME: detect B0+ stepping and use auto training */
  12582. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12583. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12584. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12585. }
  12586. if (IS_BROADWELL(dev_priv)) {
  12587. dev_priv->display.modeset_commit_cdclk =
  12588. broadwell_modeset_commit_cdclk;
  12589. dev_priv->display.modeset_calc_cdclk =
  12590. broadwell_modeset_calc_cdclk;
  12591. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12592. dev_priv->display.modeset_commit_cdclk =
  12593. valleyview_modeset_commit_cdclk;
  12594. dev_priv->display.modeset_calc_cdclk =
  12595. valleyview_modeset_calc_cdclk;
  12596. } else if (IS_BROXTON(dev_priv)) {
  12597. dev_priv->display.modeset_commit_cdclk =
  12598. broxton_modeset_commit_cdclk;
  12599. dev_priv->display.modeset_calc_cdclk =
  12600. broxton_modeset_calc_cdclk;
  12601. }
  12602. switch (INTEL_INFO(dev_priv)->gen) {
  12603. case 2:
  12604. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12605. break;
  12606. case 3:
  12607. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12608. break;
  12609. case 4:
  12610. case 5:
  12611. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12612. break;
  12613. case 6:
  12614. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12615. break;
  12616. case 7:
  12617. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12618. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12619. break;
  12620. case 9:
  12621. /* Drop through - unsupported since execlist only. */
  12622. default:
  12623. /* Default just returns -ENODEV to indicate unsupported */
  12624. dev_priv->display.queue_flip = intel_default_queue_flip;
  12625. }
  12626. }
  12627. /*
  12628. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12629. * resume, or other times. This quirk makes sure that's the case for
  12630. * affected systems.
  12631. */
  12632. static void quirk_pipea_force(struct drm_device *dev)
  12633. {
  12634. struct drm_i915_private *dev_priv = dev->dev_private;
  12635. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12636. DRM_INFO("applying pipe a force quirk\n");
  12637. }
  12638. static void quirk_pipeb_force(struct drm_device *dev)
  12639. {
  12640. struct drm_i915_private *dev_priv = dev->dev_private;
  12641. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12642. DRM_INFO("applying pipe b force quirk\n");
  12643. }
  12644. /*
  12645. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12646. */
  12647. static void quirk_ssc_force_disable(struct drm_device *dev)
  12648. {
  12649. struct drm_i915_private *dev_priv = dev->dev_private;
  12650. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12651. DRM_INFO("applying lvds SSC disable quirk\n");
  12652. }
  12653. /*
  12654. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12655. * brightness value
  12656. */
  12657. static void quirk_invert_brightness(struct drm_device *dev)
  12658. {
  12659. struct drm_i915_private *dev_priv = dev->dev_private;
  12660. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12661. DRM_INFO("applying inverted panel brightness quirk\n");
  12662. }
  12663. /* Some VBT's incorrectly indicate no backlight is present */
  12664. static void quirk_backlight_present(struct drm_device *dev)
  12665. {
  12666. struct drm_i915_private *dev_priv = dev->dev_private;
  12667. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12668. DRM_INFO("applying backlight present quirk\n");
  12669. }
  12670. struct intel_quirk {
  12671. int device;
  12672. int subsystem_vendor;
  12673. int subsystem_device;
  12674. void (*hook)(struct drm_device *dev);
  12675. };
  12676. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12677. struct intel_dmi_quirk {
  12678. void (*hook)(struct drm_device *dev);
  12679. const struct dmi_system_id (*dmi_id_list)[];
  12680. };
  12681. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12682. {
  12683. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12684. return 1;
  12685. }
  12686. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12687. {
  12688. .dmi_id_list = &(const struct dmi_system_id[]) {
  12689. {
  12690. .callback = intel_dmi_reverse_brightness,
  12691. .ident = "NCR Corporation",
  12692. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12693. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12694. },
  12695. },
  12696. { } /* terminating entry */
  12697. },
  12698. .hook = quirk_invert_brightness,
  12699. },
  12700. };
  12701. static struct intel_quirk intel_quirks[] = {
  12702. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12703. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12704. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12705. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12706. /* 830 needs to leave pipe A & dpll A up */
  12707. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12708. /* 830 needs to leave pipe B & dpll B up */
  12709. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12710. /* Lenovo U160 cannot use SSC on LVDS */
  12711. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12712. /* Sony Vaio Y cannot use SSC on LVDS */
  12713. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12714. /* Acer Aspire 5734Z must invert backlight brightness */
  12715. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12716. /* Acer/eMachines G725 */
  12717. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12718. /* Acer/eMachines e725 */
  12719. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12720. /* Acer/Packard Bell NCL20 */
  12721. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12722. /* Acer Aspire 4736Z */
  12723. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12724. /* Acer Aspire 5336 */
  12725. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12726. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12727. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12728. /* Acer C720 Chromebook (Core i3 4005U) */
  12729. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12730. /* Apple Macbook 2,1 (Core 2 T7400) */
  12731. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12732. /* Apple Macbook 4,1 */
  12733. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12734. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12735. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12736. /* HP Chromebook 14 (Celeron 2955U) */
  12737. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12738. /* Dell Chromebook 11 */
  12739. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12740. /* Dell Chromebook 11 (2015 version) */
  12741. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12742. };
  12743. static void intel_init_quirks(struct drm_device *dev)
  12744. {
  12745. struct pci_dev *d = dev->pdev;
  12746. int i;
  12747. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12748. struct intel_quirk *q = &intel_quirks[i];
  12749. if (d->device == q->device &&
  12750. (d->subsystem_vendor == q->subsystem_vendor ||
  12751. q->subsystem_vendor == PCI_ANY_ID) &&
  12752. (d->subsystem_device == q->subsystem_device ||
  12753. q->subsystem_device == PCI_ANY_ID))
  12754. q->hook(dev);
  12755. }
  12756. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12757. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12758. intel_dmi_quirks[i].hook(dev);
  12759. }
  12760. }
  12761. /* Disable the VGA plane that we never use */
  12762. static void i915_disable_vga(struct drm_device *dev)
  12763. {
  12764. struct drm_i915_private *dev_priv = dev->dev_private;
  12765. u8 sr1;
  12766. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12767. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12768. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12769. outb(SR01, VGA_SR_INDEX);
  12770. sr1 = inb(VGA_SR_DATA);
  12771. outb(sr1 | 1<<5, VGA_SR_DATA);
  12772. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12773. udelay(300);
  12774. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12775. POSTING_READ(vga_reg);
  12776. }
  12777. void intel_modeset_init_hw(struct drm_device *dev)
  12778. {
  12779. struct drm_i915_private *dev_priv = dev->dev_private;
  12780. intel_update_cdclk(dev);
  12781. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12782. intel_init_clock_gating(dev);
  12783. intel_enable_gt_powersave(dev_priv);
  12784. }
  12785. /*
  12786. * Calculate what we think the watermarks should be for the state we've read
  12787. * out of the hardware and then immediately program those watermarks so that
  12788. * we ensure the hardware settings match our internal state.
  12789. *
  12790. * We can calculate what we think WM's should be by creating a duplicate of the
  12791. * current state (which was constructed during hardware readout) and running it
  12792. * through the atomic check code to calculate new watermark values in the
  12793. * state object.
  12794. */
  12795. static void sanitize_watermarks(struct drm_device *dev)
  12796. {
  12797. struct drm_i915_private *dev_priv = to_i915(dev);
  12798. struct drm_atomic_state *state;
  12799. struct drm_crtc *crtc;
  12800. struct drm_crtc_state *cstate;
  12801. struct drm_modeset_acquire_ctx ctx;
  12802. int ret;
  12803. int i;
  12804. /* Only supported on platforms that use atomic watermark design */
  12805. if (!dev_priv->display.optimize_watermarks)
  12806. return;
  12807. /*
  12808. * We need to hold connection_mutex before calling duplicate_state so
  12809. * that the connector loop is protected.
  12810. */
  12811. drm_modeset_acquire_init(&ctx, 0);
  12812. retry:
  12813. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12814. if (ret == -EDEADLK) {
  12815. drm_modeset_backoff(&ctx);
  12816. goto retry;
  12817. } else if (WARN_ON(ret)) {
  12818. goto fail;
  12819. }
  12820. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12821. if (WARN_ON(IS_ERR(state)))
  12822. goto fail;
  12823. /*
  12824. * Hardware readout is the only time we don't want to calculate
  12825. * intermediate watermarks (since we don't trust the current
  12826. * watermarks).
  12827. */
  12828. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12829. ret = intel_atomic_check(dev, state);
  12830. if (ret) {
  12831. /*
  12832. * If we fail here, it means that the hardware appears to be
  12833. * programmed in a way that shouldn't be possible, given our
  12834. * understanding of watermark requirements. This might mean a
  12835. * mistake in the hardware readout code or a mistake in the
  12836. * watermark calculations for a given platform. Raise a WARN
  12837. * so that this is noticeable.
  12838. *
  12839. * If this actually happens, we'll have to just leave the
  12840. * BIOS-programmed watermarks untouched and hope for the best.
  12841. */
  12842. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12843. goto fail;
  12844. }
  12845. /* Write calculated watermark values back */
  12846. for_each_crtc_in_state(state, crtc, cstate, i) {
  12847. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12848. cs->wm.need_postvbl_update = true;
  12849. dev_priv->display.optimize_watermarks(cs);
  12850. }
  12851. drm_atomic_state_free(state);
  12852. fail:
  12853. drm_modeset_drop_locks(&ctx);
  12854. drm_modeset_acquire_fini(&ctx);
  12855. }
  12856. void intel_modeset_init(struct drm_device *dev)
  12857. {
  12858. struct drm_i915_private *dev_priv = to_i915(dev);
  12859. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12860. int sprite, ret;
  12861. enum pipe pipe;
  12862. struct intel_crtc *crtc;
  12863. drm_mode_config_init(dev);
  12864. dev->mode_config.min_width = 0;
  12865. dev->mode_config.min_height = 0;
  12866. dev->mode_config.preferred_depth = 24;
  12867. dev->mode_config.prefer_shadow = 1;
  12868. dev->mode_config.allow_fb_modifiers = true;
  12869. dev->mode_config.funcs = &intel_mode_funcs;
  12870. intel_init_quirks(dev);
  12871. intel_init_pm(dev);
  12872. if (INTEL_INFO(dev)->num_pipes == 0)
  12873. return;
  12874. /*
  12875. * There may be no VBT; and if the BIOS enabled SSC we can
  12876. * just keep using it to avoid unnecessary flicker. Whereas if the
  12877. * BIOS isn't using it, don't assume it will work even if the VBT
  12878. * indicates as much.
  12879. */
  12880. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12881. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12882. DREF_SSC1_ENABLE);
  12883. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12884. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12885. bios_lvds_use_ssc ? "en" : "dis",
  12886. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12887. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12888. }
  12889. }
  12890. if (IS_GEN2(dev)) {
  12891. dev->mode_config.max_width = 2048;
  12892. dev->mode_config.max_height = 2048;
  12893. } else if (IS_GEN3(dev)) {
  12894. dev->mode_config.max_width = 4096;
  12895. dev->mode_config.max_height = 4096;
  12896. } else {
  12897. dev->mode_config.max_width = 8192;
  12898. dev->mode_config.max_height = 8192;
  12899. }
  12900. if (IS_845G(dev) || IS_I865G(dev)) {
  12901. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12902. dev->mode_config.cursor_height = 1023;
  12903. } else if (IS_GEN2(dev)) {
  12904. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12905. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12906. } else {
  12907. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12908. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12909. }
  12910. dev->mode_config.fb_base = ggtt->mappable_base;
  12911. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12912. INTEL_INFO(dev)->num_pipes,
  12913. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12914. for_each_pipe(dev_priv, pipe) {
  12915. intel_crtc_init(dev, pipe);
  12916. for_each_sprite(dev_priv, pipe, sprite) {
  12917. ret = intel_plane_init(dev, pipe, sprite);
  12918. if (ret)
  12919. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12920. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12921. }
  12922. }
  12923. intel_update_czclk(dev_priv);
  12924. intel_update_cdclk(dev);
  12925. intel_shared_dpll_init(dev);
  12926. /* Just disable it once at startup */
  12927. i915_disable_vga(dev);
  12928. intel_setup_outputs(dev);
  12929. drm_modeset_lock_all(dev);
  12930. intel_modeset_setup_hw_state(dev);
  12931. drm_modeset_unlock_all(dev);
  12932. for_each_intel_crtc(dev, crtc) {
  12933. struct intel_initial_plane_config plane_config = {};
  12934. if (!crtc->active)
  12935. continue;
  12936. /*
  12937. * Note that reserving the BIOS fb up front prevents us
  12938. * from stuffing other stolen allocations like the ring
  12939. * on top. This prevents some ugliness at boot time, and
  12940. * can even allow for smooth boot transitions if the BIOS
  12941. * fb is large enough for the active pipe configuration.
  12942. */
  12943. dev_priv->display.get_initial_plane_config(crtc,
  12944. &plane_config);
  12945. /*
  12946. * If the fb is shared between multiple heads, we'll
  12947. * just get the first one.
  12948. */
  12949. intel_find_initial_plane_obj(crtc, &plane_config);
  12950. }
  12951. /*
  12952. * Make sure hardware watermarks really match the state we read out.
  12953. * Note that we need to do this after reconstructing the BIOS fb's
  12954. * since the watermark calculation done here will use pstate->fb.
  12955. */
  12956. sanitize_watermarks(dev);
  12957. }
  12958. static void intel_enable_pipe_a(struct drm_device *dev)
  12959. {
  12960. struct intel_connector *connector;
  12961. struct drm_connector *crt = NULL;
  12962. struct intel_load_detect_pipe load_detect_temp;
  12963. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12964. /* We can't just switch on the pipe A, we need to set things up with a
  12965. * proper mode and output configuration. As a gross hack, enable pipe A
  12966. * by enabling the load detect pipe once. */
  12967. for_each_intel_connector(dev, connector) {
  12968. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12969. crt = &connector->base;
  12970. break;
  12971. }
  12972. }
  12973. if (!crt)
  12974. return;
  12975. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12976. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12977. }
  12978. static bool
  12979. intel_check_plane_mapping(struct intel_crtc *crtc)
  12980. {
  12981. struct drm_device *dev = crtc->base.dev;
  12982. struct drm_i915_private *dev_priv = dev->dev_private;
  12983. u32 val;
  12984. if (INTEL_INFO(dev)->num_pipes == 1)
  12985. return true;
  12986. val = I915_READ(DSPCNTR(!crtc->plane));
  12987. if ((val & DISPLAY_PLANE_ENABLE) &&
  12988. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12989. return false;
  12990. return true;
  12991. }
  12992. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12993. {
  12994. struct drm_device *dev = crtc->base.dev;
  12995. struct intel_encoder *encoder;
  12996. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12997. return true;
  12998. return false;
  12999. }
  13000. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13001. {
  13002. struct drm_device *dev = encoder->base.dev;
  13003. struct intel_connector *connector;
  13004. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13005. return true;
  13006. return false;
  13007. }
  13008. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13009. {
  13010. struct drm_device *dev = crtc->base.dev;
  13011. struct drm_i915_private *dev_priv = dev->dev_private;
  13012. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13013. /* Clear any frame start delays used for debugging left by the BIOS */
  13014. if (!transcoder_is_dsi(cpu_transcoder)) {
  13015. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13016. I915_WRITE(reg,
  13017. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13018. }
  13019. /* restore vblank interrupts to correct state */
  13020. drm_crtc_vblank_reset(&crtc->base);
  13021. if (crtc->active) {
  13022. struct intel_plane *plane;
  13023. drm_crtc_vblank_on(&crtc->base);
  13024. /* Disable everything but the primary plane */
  13025. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13026. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13027. continue;
  13028. plane->disable_plane(&plane->base, &crtc->base);
  13029. }
  13030. }
  13031. /* We need to sanitize the plane -> pipe mapping first because this will
  13032. * disable the crtc (and hence change the state) if it is wrong. Note
  13033. * that gen4+ has a fixed plane -> pipe mapping. */
  13034. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13035. bool plane;
  13036. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  13037. crtc->base.base.id);
  13038. /* Pipe has the wrong plane attached and the plane is active.
  13039. * Temporarily change the plane mapping and disable everything
  13040. * ... */
  13041. plane = crtc->plane;
  13042. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13043. crtc->plane = !plane;
  13044. intel_crtc_disable_noatomic(&crtc->base);
  13045. crtc->plane = plane;
  13046. }
  13047. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13048. crtc->pipe == PIPE_A && !crtc->active) {
  13049. /* BIOS forgot to enable pipe A, this mostly happens after
  13050. * resume. Force-enable the pipe to fix this, the update_dpms
  13051. * call below we restore the pipe to the right state, but leave
  13052. * the required bits on. */
  13053. intel_enable_pipe_a(dev);
  13054. }
  13055. /* Adjust the state of the output pipe according to whether we
  13056. * have active connectors/encoders. */
  13057. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13058. intel_crtc_disable_noatomic(&crtc->base);
  13059. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13060. /*
  13061. * We start out with underrun reporting disabled to avoid races.
  13062. * For correct bookkeeping mark this on active crtcs.
  13063. *
  13064. * Also on gmch platforms we dont have any hardware bits to
  13065. * disable the underrun reporting. Which means we need to start
  13066. * out with underrun reporting disabled also on inactive pipes,
  13067. * since otherwise we'll complain about the garbage we read when
  13068. * e.g. coming up after runtime pm.
  13069. *
  13070. * No protection against concurrent access is required - at
  13071. * worst a fifo underrun happens which also sets this to false.
  13072. */
  13073. crtc->cpu_fifo_underrun_disabled = true;
  13074. crtc->pch_fifo_underrun_disabled = true;
  13075. }
  13076. }
  13077. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13078. {
  13079. struct intel_connector *connector;
  13080. struct drm_device *dev = encoder->base.dev;
  13081. /* We need to check both for a crtc link (meaning that the
  13082. * encoder is active and trying to read from a pipe) and the
  13083. * pipe itself being active. */
  13084. bool has_active_crtc = encoder->base.crtc &&
  13085. to_intel_crtc(encoder->base.crtc)->active;
  13086. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13087. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13088. encoder->base.base.id,
  13089. encoder->base.name);
  13090. /* Connector is active, but has no active pipe. This is
  13091. * fallout from our resume register restoring. Disable
  13092. * the encoder manually again. */
  13093. if (encoder->base.crtc) {
  13094. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13095. encoder->base.base.id,
  13096. encoder->base.name);
  13097. encoder->disable(encoder);
  13098. if (encoder->post_disable)
  13099. encoder->post_disable(encoder);
  13100. }
  13101. encoder->base.crtc = NULL;
  13102. /* Inconsistent output/port/pipe state happens presumably due to
  13103. * a bug in one of the get_hw_state functions. Or someplace else
  13104. * in our code, like the register restore mess on resume. Clamp
  13105. * things to off as a safer default. */
  13106. for_each_intel_connector(dev, connector) {
  13107. if (connector->encoder != encoder)
  13108. continue;
  13109. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13110. connector->base.encoder = NULL;
  13111. }
  13112. }
  13113. /* Enabled encoders without active connectors will be fixed in
  13114. * the crtc fixup. */
  13115. }
  13116. void i915_redisable_vga_power_on(struct drm_device *dev)
  13117. {
  13118. struct drm_i915_private *dev_priv = dev->dev_private;
  13119. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13120. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13121. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13122. i915_disable_vga(dev);
  13123. }
  13124. }
  13125. void i915_redisable_vga(struct drm_device *dev)
  13126. {
  13127. struct drm_i915_private *dev_priv = dev->dev_private;
  13128. /* This function can be called both from intel_modeset_setup_hw_state or
  13129. * at a very early point in our resume sequence, where the power well
  13130. * structures are not yet restored. Since this function is at a very
  13131. * paranoid "someone might have enabled VGA while we were not looking"
  13132. * level, just check if the power well is enabled instead of trying to
  13133. * follow the "don't touch the power well if we don't need it" policy
  13134. * the rest of the driver uses. */
  13135. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13136. return;
  13137. i915_redisable_vga_power_on(dev);
  13138. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13139. }
  13140. static bool primary_get_hw_state(struct intel_plane *plane)
  13141. {
  13142. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13143. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13144. }
  13145. /* FIXME read out full plane state for all planes */
  13146. static void readout_plane_state(struct intel_crtc *crtc)
  13147. {
  13148. struct drm_plane *primary = crtc->base.primary;
  13149. struct intel_plane_state *plane_state =
  13150. to_intel_plane_state(primary->state);
  13151. plane_state->visible = crtc->active &&
  13152. primary_get_hw_state(to_intel_plane(primary));
  13153. if (plane_state->visible)
  13154. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13155. }
  13156. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13157. {
  13158. struct drm_i915_private *dev_priv = dev->dev_private;
  13159. enum pipe pipe;
  13160. struct intel_crtc *crtc;
  13161. struct intel_encoder *encoder;
  13162. struct intel_connector *connector;
  13163. int i;
  13164. dev_priv->active_crtcs = 0;
  13165. for_each_intel_crtc(dev, crtc) {
  13166. struct intel_crtc_state *crtc_state = crtc->config;
  13167. int pixclk = 0;
  13168. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  13169. memset(crtc_state, 0, sizeof(*crtc_state));
  13170. crtc_state->base.crtc = &crtc->base;
  13171. crtc_state->base.active = crtc_state->base.enable =
  13172. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13173. crtc->base.enabled = crtc_state->base.enable;
  13174. crtc->active = crtc_state->base.active;
  13175. if (crtc_state->base.active) {
  13176. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13177. if (IS_BROADWELL(dev_priv)) {
  13178. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13179. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13180. if (crtc_state->ips_enabled)
  13181. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13182. } else if (IS_VALLEYVIEW(dev_priv) ||
  13183. IS_CHERRYVIEW(dev_priv) ||
  13184. IS_BROXTON(dev_priv))
  13185. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13186. else
  13187. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13188. }
  13189. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13190. readout_plane_state(crtc);
  13191. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13192. crtc->base.base.id,
  13193. crtc->active ? "enabled" : "disabled");
  13194. }
  13195. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13196. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13197. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13198. &pll->config.hw_state);
  13199. pll->config.crtc_mask = 0;
  13200. for_each_intel_crtc(dev, crtc) {
  13201. if (crtc->active && crtc->config->shared_dpll == pll)
  13202. pll->config.crtc_mask |= 1 << crtc->pipe;
  13203. }
  13204. pll->active_mask = pll->config.crtc_mask;
  13205. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13206. pll->name, pll->config.crtc_mask, pll->on);
  13207. }
  13208. for_each_intel_encoder(dev, encoder) {
  13209. pipe = 0;
  13210. if (encoder->get_hw_state(encoder, &pipe)) {
  13211. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13212. encoder->base.crtc = &crtc->base;
  13213. encoder->get_config(encoder, crtc->config);
  13214. } else {
  13215. encoder->base.crtc = NULL;
  13216. }
  13217. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13218. encoder->base.base.id,
  13219. encoder->base.name,
  13220. encoder->base.crtc ? "enabled" : "disabled",
  13221. pipe_name(pipe));
  13222. }
  13223. for_each_intel_connector(dev, connector) {
  13224. if (connector->get_hw_state(connector)) {
  13225. connector->base.dpms = DRM_MODE_DPMS_ON;
  13226. encoder = connector->encoder;
  13227. connector->base.encoder = &encoder->base;
  13228. if (encoder->base.crtc &&
  13229. encoder->base.crtc->state->active) {
  13230. /*
  13231. * This has to be done during hardware readout
  13232. * because anything calling .crtc_disable may
  13233. * rely on the connector_mask being accurate.
  13234. */
  13235. encoder->base.crtc->state->connector_mask |=
  13236. 1 << drm_connector_index(&connector->base);
  13237. encoder->base.crtc->state->encoder_mask |=
  13238. 1 << drm_encoder_index(&encoder->base);
  13239. }
  13240. } else {
  13241. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13242. connector->base.encoder = NULL;
  13243. }
  13244. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13245. connector->base.base.id,
  13246. connector->base.name,
  13247. connector->base.encoder ? "enabled" : "disabled");
  13248. }
  13249. for_each_intel_crtc(dev, crtc) {
  13250. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13251. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13252. if (crtc->base.state->active) {
  13253. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13254. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13255. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13256. /*
  13257. * The initial mode needs to be set in order to keep
  13258. * the atomic core happy. It wants a valid mode if the
  13259. * crtc's enabled, so we do the above call.
  13260. *
  13261. * At this point some state updated by the connectors
  13262. * in their ->detect() callback has not run yet, so
  13263. * no recalculation can be done yet.
  13264. *
  13265. * Even if we could do a recalculation and modeset
  13266. * right now it would cause a double modeset if
  13267. * fbdev or userspace chooses a different initial mode.
  13268. *
  13269. * If that happens, someone indicated they wanted a
  13270. * mode change, which means it's safe to do a full
  13271. * recalculation.
  13272. */
  13273. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13274. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13275. update_scanline_offset(crtc);
  13276. }
  13277. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13278. }
  13279. }
  13280. /* Scan out the current hw modeset state,
  13281. * and sanitizes it to the current state
  13282. */
  13283. static void
  13284. intel_modeset_setup_hw_state(struct drm_device *dev)
  13285. {
  13286. struct drm_i915_private *dev_priv = dev->dev_private;
  13287. enum pipe pipe;
  13288. struct intel_crtc *crtc;
  13289. struct intel_encoder *encoder;
  13290. int i;
  13291. intel_modeset_readout_hw_state(dev);
  13292. /* HW state is read out, now we need to sanitize this mess. */
  13293. for_each_intel_encoder(dev, encoder) {
  13294. intel_sanitize_encoder(encoder);
  13295. }
  13296. for_each_pipe(dev_priv, pipe) {
  13297. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13298. intel_sanitize_crtc(crtc);
  13299. intel_dump_pipe_config(crtc, crtc->config,
  13300. "[setup_hw_state]");
  13301. }
  13302. intel_modeset_update_connector_atomic_state(dev);
  13303. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13304. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13305. if (!pll->on || pll->active_mask)
  13306. continue;
  13307. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13308. pll->funcs.disable(dev_priv, pll);
  13309. pll->on = false;
  13310. }
  13311. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13312. vlv_wm_get_hw_state(dev);
  13313. else if (IS_GEN9(dev))
  13314. skl_wm_get_hw_state(dev);
  13315. else if (HAS_PCH_SPLIT(dev))
  13316. ilk_wm_get_hw_state(dev);
  13317. for_each_intel_crtc(dev, crtc) {
  13318. unsigned long put_domains;
  13319. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13320. if (WARN_ON(put_domains))
  13321. modeset_put_power_domains(dev_priv, put_domains);
  13322. }
  13323. intel_display_set_init_power(dev_priv, false);
  13324. intel_fbc_init_pipe_state(dev_priv);
  13325. }
  13326. void intel_display_resume(struct drm_device *dev)
  13327. {
  13328. struct drm_i915_private *dev_priv = to_i915(dev);
  13329. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13330. struct drm_modeset_acquire_ctx ctx;
  13331. int ret;
  13332. bool setup = false;
  13333. dev_priv->modeset_restore_state = NULL;
  13334. /*
  13335. * This is a cludge because with real atomic modeset mode_config.mutex
  13336. * won't be taken. Unfortunately some probed state like
  13337. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13338. * it here for now.
  13339. */
  13340. mutex_lock(&dev->mode_config.mutex);
  13341. drm_modeset_acquire_init(&ctx, 0);
  13342. retry:
  13343. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13344. if (ret == 0 && !setup) {
  13345. setup = true;
  13346. intel_modeset_setup_hw_state(dev);
  13347. i915_redisable_vga(dev);
  13348. }
  13349. if (ret == 0 && state) {
  13350. struct drm_crtc_state *crtc_state;
  13351. struct drm_crtc *crtc;
  13352. int i;
  13353. state->acquire_ctx = &ctx;
  13354. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13355. /*
  13356. * Force recalculation even if we restore
  13357. * current state. With fast modeset this may not result
  13358. * in a modeset when the state is compatible.
  13359. */
  13360. crtc_state->mode_changed = true;
  13361. }
  13362. ret = drm_atomic_commit(state);
  13363. }
  13364. if (ret == -EDEADLK) {
  13365. drm_modeset_backoff(&ctx);
  13366. goto retry;
  13367. }
  13368. drm_modeset_drop_locks(&ctx);
  13369. drm_modeset_acquire_fini(&ctx);
  13370. mutex_unlock(&dev->mode_config.mutex);
  13371. if (ret) {
  13372. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13373. drm_atomic_state_free(state);
  13374. }
  13375. }
  13376. void intel_modeset_gem_init(struct drm_device *dev)
  13377. {
  13378. struct drm_i915_private *dev_priv = to_i915(dev);
  13379. struct drm_crtc *c;
  13380. struct drm_i915_gem_object *obj;
  13381. int ret;
  13382. intel_init_gt_powersave(dev_priv);
  13383. intel_modeset_init_hw(dev);
  13384. intel_setup_overlay(dev_priv);
  13385. /*
  13386. * Make sure any fbs we allocated at startup are properly
  13387. * pinned & fenced. When we do the allocation it's too early
  13388. * for this.
  13389. */
  13390. for_each_crtc(dev, c) {
  13391. obj = intel_fb_obj(c->primary->fb);
  13392. if (obj == NULL)
  13393. continue;
  13394. mutex_lock(&dev->struct_mutex);
  13395. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13396. c->primary->state->rotation);
  13397. mutex_unlock(&dev->struct_mutex);
  13398. if (ret) {
  13399. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13400. to_intel_crtc(c)->pipe);
  13401. drm_framebuffer_unreference(c->primary->fb);
  13402. c->primary->fb = NULL;
  13403. c->primary->crtc = c->primary->state->crtc = NULL;
  13404. update_state_fb(c->primary);
  13405. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13406. }
  13407. }
  13408. intel_backlight_register(dev);
  13409. }
  13410. void intel_connector_unregister(struct intel_connector *intel_connector)
  13411. {
  13412. struct drm_connector *connector = &intel_connector->base;
  13413. intel_panel_destroy_backlight(connector);
  13414. drm_connector_unregister(connector);
  13415. }
  13416. void intel_modeset_cleanup(struct drm_device *dev)
  13417. {
  13418. struct drm_i915_private *dev_priv = dev->dev_private;
  13419. struct intel_connector *connector;
  13420. intel_disable_gt_powersave(dev_priv);
  13421. intel_backlight_unregister(dev);
  13422. /*
  13423. * Interrupts and polling as the first thing to avoid creating havoc.
  13424. * Too much stuff here (turning of connectors, ...) would
  13425. * experience fancy races otherwise.
  13426. */
  13427. intel_irq_uninstall(dev_priv);
  13428. /*
  13429. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13430. * poll handlers. Hence disable polling after hpd handling is shut down.
  13431. */
  13432. drm_kms_helper_poll_fini(dev);
  13433. intel_unregister_dsm_handler();
  13434. intel_fbc_global_disable(dev_priv);
  13435. /* flush any delayed tasks or pending work */
  13436. flush_scheduled_work();
  13437. /* destroy the backlight and sysfs files before encoders/connectors */
  13438. for_each_intel_connector(dev, connector)
  13439. connector->unregister(connector);
  13440. drm_mode_config_cleanup(dev);
  13441. intel_cleanup_overlay(dev_priv);
  13442. intel_cleanup_gt_powersave(dev_priv);
  13443. intel_teardown_gmbus(dev);
  13444. }
  13445. /*
  13446. * Return which encoder is currently attached for connector.
  13447. */
  13448. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13449. {
  13450. return &intel_attached_encoder(connector)->base;
  13451. }
  13452. void intel_connector_attach_encoder(struct intel_connector *connector,
  13453. struct intel_encoder *encoder)
  13454. {
  13455. connector->encoder = encoder;
  13456. drm_mode_connector_attach_encoder(&connector->base,
  13457. &encoder->base);
  13458. }
  13459. /*
  13460. * set vga decode state - true == enable VGA decode
  13461. */
  13462. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13463. {
  13464. struct drm_i915_private *dev_priv = dev->dev_private;
  13465. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13466. u16 gmch_ctrl;
  13467. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13468. DRM_ERROR("failed to read control word\n");
  13469. return -EIO;
  13470. }
  13471. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13472. return 0;
  13473. if (state)
  13474. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13475. else
  13476. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13477. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13478. DRM_ERROR("failed to write control word\n");
  13479. return -EIO;
  13480. }
  13481. return 0;
  13482. }
  13483. struct intel_display_error_state {
  13484. u32 power_well_driver;
  13485. int num_transcoders;
  13486. struct intel_cursor_error_state {
  13487. u32 control;
  13488. u32 position;
  13489. u32 base;
  13490. u32 size;
  13491. } cursor[I915_MAX_PIPES];
  13492. struct intel_pipe_error_state {
  13493. bool power_domain_on;
  13494. u32 source;
  13495. u32 stat;
  13496. } pipe[I915_MAX_PIPES];
  13497. struct intel_plane_error_state {
  13498. u32 control;
  13499. u32 stride;
  13500. u32 size;
  13501. u32 pos;
  13502. u32 addr;
  13503. u32 surface;
  13504. u32 tile_offset;
  13505. } plane[I915_MAX_PIPES];
  13506. struct intel_transcoder_error_state {
  13507. bool power_domain_on;
  13508. enum transcoder cpu_transcoder;
  13509. u32 conf;
  13510. u32 htotal;
  13511. u32 hblank;
  13512. u32 hsync;
  13513. u32 vtotal;
  13514. u32 vblank;
  13515. u32 vsync;
  13516. } transcoder[4];
  13517. };
  13518. struct intel_display_error_state *
  13519. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13520. {
  13521. struct intel_display_error_state *error;
  13522. int transcoders[] = {
  13523. TRANSCODER_A,
  13524. TRANSCODER_B,
  13525. TRANSCODER_C,
  13526. TRANSCODER_EDP,
  13527. };
  13528. int i;
  13529. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13530. return NULL;
  13531. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13532. if (error == NULL)
  13533. return NULL;
  13534. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13535. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13536. for_each_pipe(dev_priv, i) {
  13537. error->pipe[i].power_domain_on =
  13538. __intel_display_power_is_enabled(dev_priv,
  13539. POWER_DOMAIN_PIPE(i));
  13540. if (!error->pipe[i].power_domain_on)
  13541. continue;
  13542. error->cursor[i].control = I915_READ(CURCNTR(i));
  13543. error->cursor[i].position = I915_READ(CURPOS(i));
  13544. error->cursor[i].base = I915_READ(CURBASE(i));
  13545. error->plane[i].control = I915_READ(DSPCNTR(i));
  13546. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13547. if (INTEL_GEN(dev_priv) <= 3) {
  13548. error->plane[i].size = I915_READ(DSPSIZE(i));
  13549. error->plane[i].pos = I915_READ(DSPPOS(i));
  13550. }
  13551. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13552. error->plane[i].addr = I915_READ(DSPADDR(i));
  13553. if (INTEL_GEN(dev_priv) >= 4) {
  13554. error->plane[i].surface = I915_READ(DSPSURF(i));
  13555. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13556. }
  13557. error->pipe[i].source = I915_READ(PIPESRC(i));
  13558. if (HAS_GMCH_DISPLAY(dev_priv))
  13559. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13560. }
  13561. /* Note: this does not include DSI transcoders. */
  13562. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13563. if (HAS_DDI(dev_priv))
  13564. error->num_transcoders++; /* Account for eDP. */
  13565. for (i = 0; i < error->num_transcoders; i++) {
  13566. enum transcoder cpu_transcoder = transcoders[i];
  13567. error->transcoder[i].power_domain_on =
  13568. __intel_display_power_is_enabled(dev_priv,
  13569. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13570. if (!error->transcoder[i].power_domain_on)
  13571. continue;
  13572. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13573. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13574. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13575. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13576. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13577. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13578. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13579. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13580. }
  13581. return error;
  13582. }
  13583. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13584. void
  13585. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13586. struct drm_device *dev,
  13587. struct intel_display_error_state *error)
  13588. {
  13589. struct drm_i915_private *dev_priv = dev->dev_private;
  13590. int i;
  13591. if (!error)
  13592. return;
  13593. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13594. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13595. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13596. error->power_well_driver);
  13597. for_each_pipe(dev_priv, i) {
  13598. err_printf(m, "Pipe [%d]:\n", i);
  13599. err_printf(m, " Power: %s\n",
  13600. onoff(error->pipe[i].power_domain_on));
  13601. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13602. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13603. err_printf(m, "Plane [%d]:\n", i);
  13604. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13605. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13606. if (INTEL_INFO(dev)->gen <= 3) {
  13607. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13608. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13609. }
  13610. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13611. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13612. if (INTEL_INFO(dev)->gen >= 4) {
  13613. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13614. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13615. }
  13616. err_printf(m, "Cursor [%d]:\n", i);
  13617. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13618. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13619. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13620. }
  13621. for (i = 0; i < error->num_transcoders; i++) {
  13622. err_printf(m, "CPU transcoder: %s\n",
  13623. transcoder_name(error->transcoder[i].cpu_transcoder));
  13624. err_printf(m, " Power: %s\n",
  13625. onoff(error->transcoder[i].power_domain_on));
  13626. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13627. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13628. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13629. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13630. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13631. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13632. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13633. }
  13634. }