intel_display.c 462 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578
  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. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  118. static int bxt_calc_cdclk(int max_pixclk);
  119. struct intel_limit {
  120. struct {
  121. int min, max;
  122. } dot, vco, n, m, m1, m2, p, p1;
  123. struct {
  124. int dot_limit;
  125. int p2_slow, p2_fast;
  126. } p2;
  127. };
  128. /* returns HPLL frequency in kHz */
  129. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  130. {
  131. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  132. /* Obtain SKU information */
  133. mutex_lock(&dev_priv->sb_lock);
  134. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  135. CCK_FUSE_HPLL_FREQ_MASK;
  136. mutex_unlock(&dev_priv->sb_lock);
  137. return vco_freq[hpll_freq] * 1000;
  138. }
  139. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  140. const char *name, u32 reg, int ref_freq)
  141. {
  142. u32 val;
  143. int divider;
  144. mutex_lock(&dev_priv->sb_lock);
  145. val = vlv_cck_read(dev_priv, reg);
  146. mutex_unlock(&dev_priv->sb_lock);
  147. divider = val & CCK_FREQUENCY_VALUES;
  148. WARN((val & CCK_FREQUENCY_STATUS) !=
  149. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  150. "%s change in progress\n", name);
  151. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  152. }
  153. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  154. const char *name, u32 reg)
  155. {
  156. if (dev_priv->hpll_freq == 0)
  157. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  158. return vlv_get_cck_clock(dev_priv, name, reg,
  159. dev_priv->hpll_freq);
  160. }
  161. static int
  162. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  163. {
  164. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  165. }
  166. static int
  167. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  168. {
  169. /* RAWCLK_FREQ_VLV register updated from power well code */
  170. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  171. CCK_DISPLAY_REF_CLOCK_CONTROL);
  172. }
  173. static int
  174. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  175. {
  176. uint32_t clkcfg;
  177. /* hrawclock is 1/4 the FSB frequency */
  178. clkcfg = I915_READ(CLKCFG);
  179. switch (clkcfg & CLKCFG_FSB_MASK) {
  180. case CLKCFG_FSB_400:
  181. return 100000;
  182. case CLKCFG_FSB_533:
  183. return 133333;
  184. case CLKCFG_FSB_667:
  185. return 166667;
  186. case CLKCFG_FSB_800:
  187. return 200000;
  188. case CLKCFG_FSB_1067:
  189. return 266667;
  190. case CLKCFG_FSB_1333:
  191. return 333333;
  192. /* these two are just a guess; one of them might be right */
  193. case CLKCFG_FSB_1600:
  194. case CLKCFG_FSB_1600_ALT:
  195. return 400000;
  196. default:
  197. return 133333;
  198. }
  199. }
  200. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  201. {
  202. if (HAS_PCH_SPLIT(dev_priv))
  203. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  204. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  205. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  206. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  207. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  208. else
  209. return; /* no rawclk on other platforms, or no need to know it */
  210. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  211. }
  212. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  213. {
  214. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  215. return;
  216. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  217. CCK_CZ_CLOCK_CONTROL);
  218. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  219. }
  220. static inline u32 /* units of 100MHz */
  221. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  222. const struct intel_crtc_state *pipe_config)
  223. {
  224. if (HAS_DDI(dev_priv))
  225. return pipe_config->port_clock; /* SPLL */
  226. else if (IS_GEN5(dev_priv))
  227. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  228. else
  229. return 270000;
  230. }
  231. static const struct intel_limit intel_limits_i8xx_dac = {
  232. .dot = { .min = 25000, .max = 350000 },
  233. .vco = { .min = 908000, .max = 1512000 },
  234. .n = { .min = 2, .max = 16 },
  235. .m = { .min = 96, .max = 140 },
  236. .m1 = { .min = 18, .max = 26 },
  237. .m2 = { .min = 6, .max = 16 },
  238. .p = { .min = 4, .max = 128 },
  239. .p1 = { .min = 2, .max = 33 },
  240. .p2 = { .dot_limit = 165000,
  241. .p2_slow = 4, .p2_fast = 2 },
  242. };
  243. static const struct intel_limit intel_limits_i8xx_dvo = {
  244. .dot = { .min = 25000, .max = 350000 },
  245. .vco = { .min = 908000, .max = 1512000 },
  246. .n = { .min = 2, .max = 16 },
  247. .m = { .min = 96, .max = 140 },
  248. .m1 = { .min = 18, .max = 26 },
  249. .m2 = { .min = 6, .max = 16 },
  250. .p = { .min = 4, .max = 128 },
  251. .p1 = { .min = 2, .max = 33 },
  252. .p2 = { .dot_limit = 165000,
  253. .p2_slow = 4, .p2_fast = 4 },
  254. };
  255. static const struct intel_limit intel_limits_i8xx_lvds = {
  256. .dot = { .min = 25000, .max = 350000 },
  257. .vco = { .min = 908000, .max = 1512000 },
  258. .n = { .min = 2, .max = 16 },
  259. .m = { .min = 96, .max = 140 },
  260. .m1 = { .min = 18, .max = 26 },
  261. .m2 = { .min = 6, .max = 16 },
  262. .p = { .min = 4, .max = 128 },
  263. .p1 = { .min = 1, .max = 6 },
  264. .p2 = { .dot_limit = 165000,
  265. .p2_slow = 14, .p2_fast = 7 },
  266. };
  267. static const struct intel_limit intel_limits_i9xx_sdvo = {
  268. .dot = { .min = 20000, .max = 400000 },
  269. .vco = { .min = 1400000, .max = 2800000 },
  270. .n = { .min = 1, .max = 6 },
  271. .m = { .min = 70, .max = 120 },
  272. .m1 = { .min = 8, .max = 18 },
  273. .m2 = { .min = 3, .max = 7 },
  274. .p = { .min = 5, .max = 80 },
  275. .p1 = { .min = 1, .max = 8 },
  276. .p2 = { .dot_limit = 200000,
  277. .p2_slow = 10, .p2_fast = 5 },
  278. };
  279. static const struct intel_limit intel_limits_i9xx_lvds = {
  280. .dot = { .min = 20000, .max = 400000 },
  281. .vco = { .min = 1400000, .max = 2800000 },
  282. .n = { .min = 1, .max = 6 },
  283. .m = { .min = 70, .max = 120 },
  284. .m1 = { .min = 8, .max = 18 },
  285. .m2 = { .min = 3, .max = 7 },
  286. .p = { .min = 7, .max = 98 },
  287. .p1 = { .min = 1, .max = 8 },
  288. .p2 = { .dot_limit = 112000,
  289. .p2_slow = 14, .p2_fast = 7 },
  290. };
  291. static const struct intel_limit intel_limits_g4x_sdvo = {
  292. .dot = { .min = 25000, .max = 270000 },
  293. .vco = { .min = 1750000, .max = 3500000},
  294. .n = { .min = 1, .max = 4 },
  295. .m = { .min = 104, .max = 138 },
  296. .m1 = { .min = 17, .max = 23 },
  297. .m2 = { .min = 5, .max = 11 },
  298. .p = { .min = 10, .max = 30 },
  299. .p1 = { .min = 1, .max = 3},
  300. .p2 = { .dot_limit = 270000,
  301. .p2_slow = 10,
  302. .p2_fast = 10
  303. },
  304. };
  305. static const struct intel_limit intel_limits_g4x_hdmi = {
  306. .dot = { .min = 22000, .max = 400000 },
  307. .vco = { .min = 1750000, .max = 3500000},
  308. .n = { .min = 1, .max = 4 },
  309. .m = { .min = 104, .max = 138 },
  310. .m1 = { .min = 16, .max = 23 },
  311. .m2 = { .min = 5, .max = 11 },
  312. .p = { .min = 5, .max = 80 },
  313. .p1 = { .min = 1, .max = 8},
  314. .p2 = { .dot_limit = 165000,
  315. .p2_slow = 10, .p2_fast = 5 },
  316. };
  317. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  318. .dot = { .min = 20000, .max = 115000 },
  319. .vco = { .min = 1750000, .max = 3500000 },
  320. .n = { .min = 1, .max = 3 },
  321. .m = { .min = 104, .max = 138 },
  322. .m1 = { .min = 17, .max = 23 },
  323. .m2 = { .min = 5, .max = 11 },
  324. .p = { .min = 28, .max = 112 },
  325. .p1 = { .min = 2, .max = 8 },
  326. .p2 = { .dot_limit = 0,
  327. .p2_slow = 14, .p2_fast = 14
  328. },
  329. };
  330. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  331. .dot = { .min = 80000, .max = 224000 },
  332. .vco = { .min = 1750000, .max = 3500000 },
  333. .n = { .min = 1, .max = 3 },
  334. .m = { .min = 104, .max = 138 },
  335. .m1 = { .min = 17, .max = 23 },
  336. .m2 = { .min = 5, .max = 11 },
  337. .p = { .min = 14, .max = 42 },
  338. .p1 = { .min = 2, .max = 6 },
  339. .p2 = { .dot_limit = 0,
  340. .p2_slow = 7, .p2_fast = 7
  341. },
  342. };
  343. static const struct intel_limit intel_limits_pineview_sdvo = {
  344. .dot = { .min = 20000, .max = 400000},
  345. .vco = { .min = 1700000, .max = 3500000 },
  346. /* Pineview's Ncounter is a ring counter */
  347. .n = { .min = 3, .max = 6 },
  348. .m = { .min = 2, .max = 256 },
  349. /* Pineview only has one combined m divider, which we treat as m2. */
  350. .m1 = { .min = 0, .max = 0 },
  351. .m2 = { .min = 0, .max = 254 },
  352. .p = { .min = 5, .max = 80 },
  353. .p1 = { .min = 1, .max = 8 },
  354. .p2 = { .dot_limit = 200000,
  355. .p2_slow = 10, .p2_fast = 5 },
  356. };
  357. static const struct intel_limit intel_limits_pineview_lvds = {
  358. .dot = { .min = 20000, .max = 400000 },
  359. .vco = { .min = 1700000, .max = 3500000 },
  360. .n = { .min = 3, .max = 6 },
  361. .m = { .min = 2, .max = 256 },
  362. .m1 = { .min = 0, .max = 0 },
  363. .m2 = { .min = 0, .max = 254 },
  364. .p = { .min = 7, .max = 112 },
  365. .p1 = { .min = 1, .max = 8 },
  366. .p2 = { .dot_limit = 112000,
  367. .p2_slow = 14, .p2_fast = 14 },
  368. };
  369. /* Ironlake / Sandybridge
  370. *
  371. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  372. * the range value for them is (actual_value - 2).
  373. */
  374. static const struct intel_limit intel_limits_ironlake_dac = {
  375. .dot = { .min = 25000, .max = 350000 },
  376. .vco = { .min = 1760000, .max = 3510000 },
  377. .n = { .min = 1, .max = 5 },
  378. .m = { .min = 79, .max = 127 },
  379. .m1 = { .min = 12, .max = 22 },
  380. .m2 = { .min = 5, .max = 9 },
  381. .p = { .min = 5, .max = 80 },
  382. .p1 = { .min = 1, .max = 8 },
  383. .p2 = { .dot_limit = 225000,
  384. .p2_slow = 10, .p2_fast = 5 },
  385. };
  386. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  387. .dot = { .min = 25000, .max = 350000 },
  388. .vco = { .min = 1760000, .max = 3510000 },
  389. .n = { .min = 1, .max = 3 },
  390. .m = { .min = 79, .max = 118 },
  391. .m1 = { .min = 12, .max = 22 },
  392. .m2 = { .min = 5, .max = 9 },
  393. .p = { .min = 28, .max = 112 },
  394. .p1 = { .min = 2, .max = 8 },
  395. .p2 = { .dot_limit = 225000,
  396. .p2_slow = 14, .p2_fast = 14 },
  397. };
  398. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  399. .dot = { .min = 25000, .max = 350000 },
  400. .vco = { .min = 1760000, .max = 3510000 },
  401. .n = { .min = 1, .max = 3 },
  402. .m = { .min = 79, .max = 127 },
  403. .m1 = { .min = 12, .max = 22 },
  404. .m2 = { .min = 5, .max = 9 },
  405. .p = { .min = 14, .max = 56 },
  406. .p1 = { .min = 2, .max = 8 },
  407. .p2 = { .dot_limit = 225000,
  408. .p2_slow = 7, .p2_fast = 7 },
  409. };
  410. /* LVDS 100mhz refclk limits. */
  411. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  412. .dot = { .min = 25000, .max = 350000 },
  413. .vco = { .min = 1760000, .max = 3510000 },
  414. .n = { .min = 1, .max = 2 },
  415. .m = { .min = 79, .max = 126 },
  416. .m1 = { .min = 12, .max = 22 },
  417. .m2 = { .min = 5, .max = 9 },
  418. .p = { .min = 28, .max = 112 },
  419. .p1 = { .min = 2, .max = 8 },
  420. .p2 = { .dot_limit = 225000,
  421. .p2_slow = 14, .p2_fast = 14 },
  422. };
  423. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  424. .dot = { .min = 25000, .max = 350000 },
  425. .vco = { .min = 1760000, .max = 3510000 },
  426. .n = { .min = 1, .max = 3 },
  427. .m = { .min = 79, .max = 126 },
  428. .m1 = { .min = 12, .max = 22 },
  429. .m2 = { .min = 5, .max = 9 },
  430. .p = { .min = 14, .max = 42 },
  431. .p1 = { .min = 2, .max = 6 },
  432. .p2 = { .dot_limit = 225000,
  433. .p2_slow = 7, .p2_fast = 7 },
  434. };
  435. static const struct intel_limit intel_limits_vlv = {
  436. /*
  437. * These are the data rate limits (measured in fast clocks)
  438. * since those are the strictest limits we have. The fast
  439. * clock and actual rate limits are more relaxed, so checking
  440. * them would make no difference.
  441. */
  442. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  443. .vco = { .min = 4000000, .max = 6000000 },
  444. .n = { .min = 1, .max = 7 },
  445. .m1 = { .min = 2, .max = 3 },
  446. .m2 = { .min = 11, .max = 156 },
  447. .p1 = { .min = 2, .max = 3 },
  448. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  449. };
  450. static const struct intel_limit intel_limits_chv = {
  451. /*
  452. * These are the data rate limits (measured in fast clocks)
  453. * since those are the strictest limits we have. The fast
  454. * clock and actual rate limits are more relaxed, so checking
  455. * them would make no difference.
  456. */
  457. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  458. .vco = { .min = 4800000, .max = 6480000 },
  459. .n = { .min = 1, .max = 1 },
  460. .m1 = { .min = 2, .max = 2 },
  461. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  462. .p1 = { .min = 2, .max = 4 },
  463. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  464. };
  465. static const struct intel_limit intel_limits_bxt = {
  466. /* FIXME: find real dot limits */
  467. .dot = { .min = 0, .max = INT_MAX },
  468. .vco = { .min = 4800000, .max = 6700000 },
  469. .n = { .min = 1, .max = 1 },
  470. .m1 = { .min = 2, .max = 2 },
  471. /* FIXME: find real m2 limits */
  472. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  473. .p1 = { .min = 2, .max = 4 },
  474. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  475. };
  476. static bool
  477. needs_modeset(struct drm_crtc_state *state)
  478. {
  479. return drm_atomic_crtc_needs_modeset(state);
  480. }
  481. /**
  482. * Returns whether any output on the specified pipe is of the specified type
  483. */
  484. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  485. {
  486. struct drm_device *dev = crtc->base.dev;
  487. struct intel_encoder *encoder;
  488. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  489. if (encoder->type == type)
  490. return true;
  491. return false;
  492. }
  493. /**
  494. * Returns whether any output on the specified pipe will have the specified
  495. * type after a staged modeset is complete, i.e., the same as
  496. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  497. * encoder->crtc.
  498. */
  499. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  500. int type)
  501. {
  502. struct drm_atomic_state *state = crtc_state->base.state;
  503. struct drm_connector *connector;
  504. struct drm_connector_state *connector_state;
  505. struct intel_encoder *encoder;
  506. int i, num_connectors = 0;
  507. for_each_connector_in_state(state, connector, connector_state, i) {
  508. if (connector_state->crtc != crtc_state->base.crtc)
  509. continue;
  510. num_connectors++;
  511. encoder = to_intel_encoder(connector_state->best_encoder);
  512. if (encoder->type == type)
  513. return true;
  514. }
  515. WARN_ON(num_connectors == 0);
  516. return false;
  517. }
  518. /*
  519. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  520. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  521. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  522. * The helpers' return value is the rate of the clock that is fed to the
  523. * display engine's pipe which can be the above fast dot clock rate or a
  524. * divided-down version of it.
  525. */
  526. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  527. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  528. {
  529. clock->m = clock->m2 + 2;
  530. clock->p = clock->p1 * clock->p2;
  531. if (WARN_ON(clock->n == 0 || clock->p == 0))
  532. return 0;
  533. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  534. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  535. return clock->dot;
  536. }
  537. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  538. {
  539. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  540. }
  541. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  542. {
  543. clock->m = i9xx_dpll_compute_m(clock);
  544. clock->p = clock->p1 * clock->p2;
  545. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  546. return 0;
  547. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  548. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  549. return clock->dot;
  550. }
  551. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  552. {
  553. clock->m = clock->m1 * clock->m2;
  554. clock->p = clock->p1 * clock->p2;
  555. if (WARN_ON(clock->n == 0 || clock->p == 0))
  556. return 0;
  557. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  558. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  559. return clock->dot / 5;
  560. }
  561. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  562. {
  563. clock->m = clock->m1 * clock->m2;
  564. clock->p = clock->p1 * clock->p2;
  565. if (WARN_ON(clock->n == 0 || clock->p == 0))
  566. return 0;
  567. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  568. clock->n << 22);
  569. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  570. return clock->dot / 5;
  571. }
  572. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  573. /**
  574. * Returns whether the given set of divisors are valid for a given refclk with
  575. * the given connectors.
  576. */
  577. static bool intel_PLL_is_valid(struct drm_device *dev,
  578. const struct intel_limit *limit,
  579. const struct dpll *clock)
  580. {
  581. if (clock->n < limit->n.min || limit->n.max < clock->n)
  582. INTELPllInvalid("n out of range\n");
  583. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  584. INTELPllInvalid("p1 out of range\n");
  585. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  586. INTELPllInvalid("m2 out of range\n");
  587. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  588. INTELPllInvalid("m1 out of range\n");
  589. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  590. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  591. if (clock->m1 <= clock->m2)
  592. INTELPllInvalid("m1 <= m2\n");
  593. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  594. if (clock->p < limit->p.min || limit->p.max < clock->p)
  595. INTELPllInvalid("p out of range\n");
  596. if (clock->m < limit->m.min || limit->m.max < clock->m)
  597. INTELPllInvalid("m out of range\n");
  598. }
  599. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  600. INTELPllInvalid("vco out of range\n");
  601. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  602. * connector, etc., rather than just a single range.
  603. */
  604. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  605. INTELPllInvalid("dot out of range\n");
  606. return true;
  607. }
  608. static int
  609. i9xx_select_p2_div(const struct intel_limit *limit,
  610. const struct intel_crtc_state *crtc_state,
  611. int target)
  612. {
  613. struct drm_device *dev = crtc_state->base.crtc->dev;
  614. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  615. /*
  616. * For LVDS just rely on its current settings for dual-channel.
  617. * We haven't figured out how to reliably set up different
  618. * single/dual channel state, if we even can.
  619. */
  620. if (intel_is_dual_link_lvds(dev))
  621. return limit->p2.p2_fast;
  622. else
  623. return limit->p2.p2_slow;
  624. } else {
  625. if (target < limit->p2.dot_limit)
  626. return limit->p2.p2_slow;
  627. else
  628. return limit->p2.p2_fast;
  629. }
  630. }
  631. /*
  632. * Returns a set of divisors for the desired target clock with the given
  633. * refclk, or FALSE. The returned values represent the clock equation:
  634. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  635. *
  636. * Target and reference clocks are specified in kHz.
  637. *
  638. * If match_clock is provided, then best_clock P divider must match the P
  639. * divider from @match_clock used for LVDS downclocking.
  640. */
  641. static bool
  642. i9xx_find_best_dpll(const struct intel_limit *limit,
  643. struct intel_crtc_state *crtc_state,
  644. int target, int refclk, struct dpll *match_clock,
  645. struct dpll *best_clock)
  646. {
  647. struct drm_device *dev = crtc_state->base.crtc->dev;
  648. struct dpll clock;
  649. int err = target;
  650. memset(best_clock, 0, sizeof(*best_clock));
  651. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  652. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  653. clock.m1++) {
  654. for (clock.m2 = limit->m2.min;
  655. clock.m2 <= limit->m2.max; clock.m2++) {
  656. if (clock.m2 >= clock.m1)
  657. break;
  658. for (clock.n = limit->n.min;
  659. clock.n <= limit->n.max; clock.n++) {
  660. for (clock.p1 = limit->p1.min;
  661. clock.p1 <= limit->p1.max; clock.p1++) {
  662. int this_err;
  663. i9xx_calc_dpll_params(refclk, &clock);
  664. if (!intel_PLL_is_valid(dev, limit,
  665. &clock))
  666. continue;
  667. if (match_clock &&
  668. clock.p != match_clock->p)
  669. continue;
  670. this_err = abs(clock.dot - target);
  671. if (this_err < err) {
  672. *best_clock = clock;
  673. err = this_err;
  674. }
  675. }
  676. }
  677. }
  678. }
  679. return (err != target);
  680. }
  681. /*
  682. * Returns a set of divisors for the desired target clock with the given
  683. * refclk, or FALSE. The returned values represent the clock equation:
  684. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  685. *
  686. * Target and reference clocks are specified in kHz.
  687. *
  688. * If match_clock is provided, then best_clock P divider must match the P
  689. * divider from @match_clock used for LVDS downclocking.
  690. */
  691. static bool
  692. pnv_find_best_dpll(const struct intel_limit *limit,
  693. struct intel_crtc_state *crtc_state,
  694. int target, int refclk, struct dpll *match_clock,
  695. struct dpll *best_clock)
  696. {
  697. struct drm_device *dev = crtc_state->base.crtc->dev;
  698. struct dpll clock;
  699. int err = target;
  700. memset(best_clock, 0, sizeof(*best_clock));
  701. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  702. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  703. clock.m1++) {
  704. for (clock.m2 = limit->m2.min;
  705. clock.m2 <= limit->m2.max; clock.m2++) {
  706. for (clock.n = limit->n.min;
  707. clock.n <= limit->n.max; clock.n++) {
  708. for (clock.p1 = limit->p1.min;
  709. clock.p1 <= limit->p1.max; clock.p1++) {
  710. int this_err;
  711. pnv_calc_dpll_params(refclk, &clock);
  712. if (!intel_PLL_is_valid(dev, limit,
  713. &clock))
  714. continue;
  715. if (match_clock &&
  716. clock.p != match_clock->p)
  717. continue;
  718. this_err = abs(clock.dot - target);
  719. if (this_err < err) {
  720. *best_clock = clock;
  721. err = this_err;
  722. }
  723. }
  724. }
  725. }
  726. }
  727. return (err != target);
  728. }
  729. /*
  730. * Returns a set of divisors for the desired target clock with the given
  731. * refclk, or FALSE. The returned values represent the clock equation:
  732. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  733. *
  734. * Target and reference clocks are specified in kHz.
  735. *
  736. * If match_clock is provided, then best_clock P divider must match the P
  737. * divider from @match_clock used for LVDS downclocking.
  738. */
  739. static bool
  740. g4x_find_best_dpll(const struct intel_limit *limit,
  741. struct intel_crtc_state *crtc_state,
  742. int target, int refclk, struct dpll *match_clock,
  743. struct dpll *best_clock)
  744. {
  745. struct drm_device *dev = crtc_state->base.crtc->dev;
  746. struct dpll clock;
  747. int max_n;
  748. bool found = false;
  749. /* approximately equals target * 0.00585 */
  750. int err_most = (target >> 8) + (target >> 9);
  751. memset(best_clock, 0, sizeof(*best_clock));
  752. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  753. max_n = limit->n.max;
  754. /* based on hardware requirement, prefer smaller n to precision */
  755. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  756. /* based on hardware requirement, prefere larger m1,m2 */
  757. for (clock.m1 = limit->m1.max;
  758. clock.m1 >= limit->m1.min; clock.m1--) {
  759. for (clock.m2 = limit->m2.max;
  760. clock.m2 >= limit->m2.min; clock.m2--) {
  761. for (clock.p1 = limit->p1.max;
  762. clock.p1 >= limit->p1.min; clock.p1--) {
  763. int this_err;
  764. i9xx_calc_dpll_params(refclk, &clock);
  765. if (!intel_PLL_is_valid(dev, limit,
  766. &clock))
  767. continue;
  768. this_err = abs(clock.dot - target);
  769. if (this_err < err_most) {
  770. *best_clock = clock;
  771. err_most = this_err;
  772. max_n = clock.n;
  773. found = true;
  774. }
  775. }
  776. }
  777. }
  778. }
  779. return found;
  780. }
  781. /*
  782. * Check if the calculated PLL configuration is more optimal compared to the
  783. * best configuration and error found so far. Return the calculated error.
  784. */
  785. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  786. const struct dpll *calculated_clock,
  787. const struct dpll *best_clock,
  788. unsigned int best_error_ppm,
  789. unsigned int *error_ppm)
  790. {
  791. /*
  792. * For CHV ignore the error and consider only the P value.
  793. * Prefer a bigger P value based on HW requirements.
  794. */
  795. if (IS_CHERRYVIEW(dev)) {
  796. *error_ppm = 0;
  797. return calculated_clock->p > best_clock->p;
  798. }
  799. if (WARN_ON_ONCE(!target_freq))
  800. return false;
  801. *error_ppm = div_u64(1000000ULL *
  802. abs(target_freq - calculated_clock->dot),
  803. target_freq);
  804. /*
  805. * Prefer a better P value over a better (smaller) error if the error
  806. * is small. Ensure this preference for future configurations too by
  807. * setting the error to 0.
  808. */
  809. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  810. *error_ppm = 0;
  811. return true;
  812. }
  813. return *error_ppm + 10 < best_error_ppm;
  814. }
  815. /*
  816. * Returns a set of divisors for the desired target clock with the given
  817. * refclk, or FALSE. The returned values represent the clock equation:
  818. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  819. */
  820. static bool
  821. vlv_find_best_dpll(const struct intel_limit *limit,
  822. struct intel_crtc_state *crtc_state,
  823. int target, int refclk, struct dpll *match_clock,
  824. struct dpll *best_clock)
  825. {
  826. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  827. struct drm_device *dev = crtc->base.dev;
  828. struct dpll clock;
  829. unsigned int bestppm = 1000000;
  830. /* min update 19.2 MHz */
  831. int max_n = min(limit->n.max, refclk / 19200);
  832. bool found = false;
  833. target *= 5; /* fast clock */
  834. memset(best_clock, 0, sizeof(*best_clock));
  835. /* based on hardware requirement, prefer smaller n to precision */
  836. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  837. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  838. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  839. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  840. clock.p = clock.p1 * clock.p2;
  841. /* based on hardware requirement, prefer bigger m1,m2 values */
  842. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  843. unsigned int ppm;
  844. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  845. refclk * clock.m1);
  846. vlv_calc_dpll_params(refclk, &clock);
  847. if (!intel_PLL_is_valid(dev, limit,
  848. &clock))
  849. continue;
  850. if (!vlv_PLL_is_optimal(dev, target,
  851. &clock,
  852. best_clock,
  853. bestppm, &ppm))
  854. continue;
  855. *best_clock = clock;
  856. bestppm = ppm;
  857. found = true;
  858. }
  859. }
  860. }
  861. }
  862. return found;
  863. }
  864. /*
  865. * Returns a set of divisors for the desired target clock with the given
  866. * refclk, or FALSE. The returned values represent the clock equation:
  867. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  868. */
  869. static bool
  870. chv_find_best_dpll(const struct intel_limit *limit,
  871. struct intel_crtc_state *crtc_state,
  872. int target, int refclk, struct dpll *match_clock,
  873. struct dpll *best_clock)
  874. {
  875. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  876. struct drm_device *dev = crtc->base.dev;
  877. unsigned int best_error_ppm;
  878. struct dpll clock;
  879. uint64_t m2;
  880. int found = false;
  881. memset(best_clock, 0, sizeof(*best_clock));
  882. best_error_ppm = 1000000;
  883. /*
  884. * Based on hardware doc, the n always set to 1, and m1 always
  885. * set to 2. If requires to support 200Mhz refclk, we need to
  886. * revisit this because n may not 1 anymore.
  887. */
  888. clock.n = 1, clock.m1 = 2;
  889. target *= 5; /* fast clock */
  890. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  891. for (clock.p2 = limit->p2.p2_fast;
  892. clock.p2 >= limit->p2.p2_slow;
  893. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  894. unsigned int error_ppm;
  895. clock.p = clock.p1 * clock.p2;
  896. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  897. clock.n) << 22, refclk * clock.m1);
  898. if (m2 > INT_MAX/clock.m1)
  899. continue;
  900. clock.m2 = m2;
  901. chv_calc_dpll_params(refclk, &clock);
  902. if (!intel_PLL_is_valid(dev, limit, &clock))
  903. continue;
  904. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  905. best_error_ppm, &error_ppm))
  906. continue;
  907. *best_clock = clock;
  908. best_error_ppm = error_ppm;
  909. found = true;
  910. }
  911. }
  912. return found;
  913. }
  914. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  915. struct dpll *best_clock)
  916. {
  917. int refclk = 100000;
  918. const struct intel_limit *limit = &intel_limits_bxt;
  919. return chv_find_best_dpll(limit, crtc_state,
  920. target_clock, refclk, NULL, best_clock);
  921. }
  922. bool intel_crtc_active(struct drm_crtc *crtc)
  923. {
  924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  925. /* Be paranoid as we can arrive here with only partial
  926. * state retrieved from the hardware during setup.
  927. *
  928. * We can ditch the adjusted_mode.crtc_clock check as soon
  929. * as Haswell has gained clock readout/fastboot support.
  930. *
  931. * We can ditch the crtc->primary->fb check as soon as we can
  932. * properly reconstruct framebuffers.
  933. *
  934. * FIXME: The intel_crtc->active here should be switched to
  935. * crtc->state->active once we have proper CRTC states wired up
  936. * for atomic.
  937. */
  938. return intel_crtc->active && crtc->primary->state->fb &&
  939. intel_crtc->config->base.adjusted_mode.crtc_clock;
  940. }
  941. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  942. enum pipe pipe)
  943. {
  944. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  945. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  946. return intel_crtc->config->cpu_transcoder;
  947. }
  948. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  949. {
  950. struct drm_i915_private *dev_priv = dev->dev_private;
  951. i915_reg_t reg = PIPEDSL(pipe);
  952. u32 line1, line2;
  953. u32 line_mask;
  954. if (IS_GEN2(dev))
  955. line_mask = DSL_LINEMASK_GEN2;
  956. else
  957. line_mask = DSL_LINEMASK_GEN3;
  958. line1 = I915_READ(reg) & line_mask;
  959. msleep(5);
  960. line2 = I915_READ(reg) & line_mask;
  961. return line1 == line2;
  962. }
  963. /*
  964. * intel_wait_for_pipe_off - wait for pipe to turn off
  965. * @crtc: crtc whose pipe to wait for
  966. *
  967. * After disabling a pipe, we can't wait for vblank in the usual way,
  968. * spinning on the vblank interrupt status bit, since we won't actually
  969. * see an interrupt when the pipe is disabled.
  970. *
  971. * On Gen4 and above:
  972. * wait for the pipe register state bit to turn off
  973. *
  974. * Otherwise:
  975. * wait for the display line value to settle (it usually
  976. * ends up stopping at the start of the next frame).
  977. *
  978. */
  979. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  980. {
  981. struct drm_device *dev = crtc->base.dev;
  982. struct drm_i915_private *dev_priv = dev->dev_private;
  983. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  984. enum pipe pipe = crtc->pipe;
  985. if (INTEL_INFO(dev)->gen >= 4) {
  986. i915_reg_t reg = PIPECONF(cpu_transcoder);
  987. /* Wait for the Pipe State to go off */
  988. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  989. 100))
  990. WARN(1, "pipe_off wait timed out\n");
  991. } else {
  992. /* Wait for the display line to settle */
  993. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  994. WARN(1, "pipe_off wait timed out\n");
  995. }
  996. }
  997. /* Only for pre-ILK configs */
  998. void assert_pll(struct drm_i915_private *dev_priv,
  999. enum pipe pipe, bool state)
  1000. {
  1001. u32 val;
  1002. bool cur_state;
  1003. val = I915_READ(DPLL(pipe));
  1004. cur_state = !!(val & DPLL_VCO_ENABLE);
  1005. I915_STATE_WARN(cur_state != state,
  1006. "PLL state assertion failure (expected %s, current %s)\n",
  1007. onoff(state), onoff(cur_state));
  1008. }
  1009. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1010. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1011. {
  1012. u32 val;
  1013. bool cur_state;
  1014. mutex_lock(&dev_priv->sb_lock);
  1015. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1016. mutex_unlock(&dev_priv->sb_lock);
  1017. cur_state = val & DSI_PLL_VCO_EN;
  1018. I915_STATE_WARN(cur_state != state,
  1019. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1020. onoff(state), onoff(cur_state));
  1021. }
  1022. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1023. enum pipe pipe, bool state)
  1024. {
  1025. bool cur_state;
  1026. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1027. pipe);
  1028. if (HAS_DDI(dev_priv)) {
  1029. /* DDI does not have a specific FDI_TX register */
  1030. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1031. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1032. } else {
  1033. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1034. cur_state = !!(val & FDI_TX_ENABLE);
  1035. }
  1036. I915_STATE_WARN(cur_state != state,
  1037. "FDI TX state assertion failure (expected %s, current %s)\n",
  1038. onoff(state), onoff(cur_state));
  1039. }
  1040. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1041. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1042. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1043. enum pipe pipe, bool state)
  1044. {
  1045. u32 val;
  1046. bool cur_state;
  1047. val = I915_READ(FDI_RX_CTL(pipe));
  1048. cur_state = !!(val & FDI_RX_ENABLE);
  1049. I915_STATE_WARN(cur_state != state,
  1050. "FDI RX state assertion failure (expected %s, current %s)\n",
  1051. onoff(state), onoff(cur_state));
  1052. }
  1053. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1054. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1055. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe)
  1057. {
  1058. u32 val;
  1059. /* ILK FDI PLL is always enabled */
  1060. if (IS_GEN5(dev_priv))
  1061. return;
  1062. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1063. if (HAS_DDI(dev_priv))
  1064. return;
  1065. val = I915_READ(FDI_TX_CTL(pipe));
  1066. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1067. }
  1068. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1069. enum pipe pipe, bool state)
  1070. {
  1071. u32 val;
  1072. bool cur_state;
  1073. val = I915_READ(FDI_RX_CTL(pipe));
  1074. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1075. I915_STATE_WARN(cur_state != state,
  1076. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1077. onoff(state), onoff(cur_state));
  1078. }
  1079. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1080. enum pipe pipe)
  1081. {
  1082. struct drm_device *dev = dev_priv->dev;
  1083. i915_reg_t pp_reg;
  1084. u32 val;
  1085. enum pipe panel_pipe = PIPE_A;
  1086. bool locked = true;
  1087. if (WARN_ON(HAS_DDI(dev)))
  1088. return;
  1089. if (HAS_PCH_SPLIT(dev)) {
  1090. u32 port_sel;
  1091. pp_reg = PCH_PP_CONTROL;
  1092. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1093. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1094. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1095. panel_pipe = PIPE_B;
  1096. /* XXX: else fix for eDP */
  1097. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1098. /* presumably write lock depends on pipe, not port select */
  1099. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1100. panel_pipe = pipe;
  1101. } else {
  1102. pp_reg = PP_CONTROL;
  1103. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1104. panel_pipe = PIPE_B;
  1105. }
  1106. val = I915_READ(pp_reg);
  1107. if (!(val & PANEL_POWER_ON) ||
  1108. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1109. locked = false;
  1110. I915_STATE_WARN(panel_pipe == pipe && locked,
  1111. "panel assertion failure, pipe %c regs locked\n",
  1112. pipe_name(pipe));
  1113. }
  1114. static void assert_cursor(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe, bool state)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. bool cur_state;
  1119. if (IS_845G(dev) || IS_I865G(dev))
  1120. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1121. else
  1122. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1123. I915_STATE_WARN(cur_state != state,
  1124. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1125. pipe_name(pipe), onoff(state), onoff(cur_state));
  1126. }
  1127. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1128. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1129. void assert_pipe(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe, bool state)
  1131. {
  1132. bool cur_state;
  1133. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1134. pipe);
  1135. enum intel_display_power_domain power_domain;
  1136. /* if we need the pipe quirk it must be always on */
  1137. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1138. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1139. state = true;
  1140. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1141. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1142. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1143. cur_state = !!(val & PIPECONF_ENABLE);
  1144. intel_display_power_put(dev_priv, power_domain);
  1145. } else {
  1146. cur_state = false;
  1147. }
  1148. I915_STATE_WARN(cur_state != state,
  1149. "pipe %c assertion failure (expected %s, current %s)\n",
  1150. pipe_name(pipe), onoff(state), onoff(cur_state));
  1151. }
  1152. static void assert_plane(struct drm_i915_private *dev_priv,
  1153. enum plane plane, bool state)
  1154. {
  1155. u32 val;
  1156. bool cur_state;
  1157. val = I915_READ(DSPCNTR(plane));
  1158. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1159. I915_STATE_WARN(cur_state != state,
  1160. "plane %c assertion failure (expected %s, current %s)\n",
  1161. plane_name(plane), onoff(state), onoff(cur_state));
  1162. }
  1163. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1164. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1165. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1166. enum pipe pipe)
  1167. {
  1168. struct drm_device *dev = dev_priv->dev;
  1169. int i;
  1170. /* Primary planes are fixed to pipes on gen4+ */
  1171. if (INTEL_INFO(dev)->gen >= 4) {
  1172. u32 val = I915_READ(DSPCNTR(pipe));
  1173. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1174. "plane %c assertion failure, should be disabled but not\n",
  1175. plane_name(pipe));
  1176. return;
  1177. }
  1178. /* Need to check both planes against the pipe */
  1179. for_each_pipe(dev_priv, i) {
  1180. u32 val = I915_READ(DSPCNTR(i));
  1181. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1182. DISPPLANE_SEL_PIPE_SHIFT;
  1183. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1184. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1185. plane_name(i), pipe_name(pipe));
  1186. }
  1187. }
  1188. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1189. enum pipe pipe)
  1190. {
  1191. struct drm_device *dev = dev_priv->dev;
  1192. int sprite;
  1193. if (INTEL_INFO(dev)->gen >= 9) {
  1194. for_each_sprite(dev_priv, pipe, sprite) {
  1195. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1196. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1197. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1198. sprite, pipe_name(pipe));
  1199. }
  1200. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1201. for_each_sprite(dev_priv, pipe, sprite) {
  1202. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1203. I915_STATE_WARN(val & SP_ENABLE,
  1204. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1205. sprite_name(pipe, sprite), pipe_name(pipe));
  1206. }
  1207. } else if (INTEL_INFO(dev)->gen >= 7) {
  1208. u32 val = I915_READ(SPRCTL(pipe));
  1209. I915_STATE_WARN(val & SPRITE_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. } else if (INTEL_INFO(dev)->gen >= 5) {
  1213. u32 val = I915_READ(DVSCNTR(pipe));
  1214. I915_STATE_WARN(val & DVS_ENABLE,
  1215. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1216. plane_name(pipe), pipe_name(pipe));
  1217. }
  1218. }
  1219. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1220. {
  1221. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1222. drm_crtc_vblank_put(crtc);
  1223. }
  1224. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1225. enum pipe pipe)
  1226. {
  1227. u32 val;
  1228. bool enabled;
  1229. val = I915_READ(PCH_TRANSCONF(pipe));
  1230. enabled = !!(val & TRANS_ENABLE);
  1231. I915_STATE_WARN(enabled,
  1232. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1233. pipe_name(pipe));
  1234. }
  1235. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 port_sel, u32 val)
  1237. {
  1238. if ((val & DP_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv)) {
  1241. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1242. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1243. return false;
  1244. } else if (IS_CHERRYVIEW(dev_priv)) {
  1245. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1246. return false;
  1247. } else {
  1248. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1249. return false;
  1250. }
  1251. return true;
  1252. }
  1253. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1254. enum pipe pipe, u32 val)
  1255. {
  1256. if ((val & SDVO_ENABLE) == 0)
  1257. return false;
  1258. if (HAS_PCH_CPT(dev_priv)) {
  1259. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1260. return false;
  1261. } else if (IS_CHERRYVIEW(dev_priv)) {
  1262. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1263. return false;
  1264. } else {
  1265. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1266. return false;
  1267. }
  1268. return true;
  1269. }
  1270. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1271. enum pipe pipe, u32 val)
  1272. {
  1273. if ((val & LVDS_PORT_EN) == 0)
  1274. return false;
  1275. if (HAS_PCH_CPT(dev_priv)) {
  1276. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1277. return false;
  1278. } else {
  1279. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1280. return false;
  1281. }
  1282. return true;
  1283. }
  1284. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1285. enum pipe pipe, u32 val)
  1286. {
  1287. if ((val & ADPA_DAC_ENABLE) == 0)
  1288. return false;
  1289. if (HAS_PCH_CPT(dev_priv)) {
  1290. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1291. return false;
  1292. } else {
  1293. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1294. return false;
  1295. }
  1296. return true;
  1297. }
  1298. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1299. enum pipe pipe, i915_reg_t reg,
  1300. u32 port_sel)
  1301. {
  1302. u32 val = I915_READ(reg);
  1303. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1304. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1305. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1306. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1307. && (val & DP_PIPEB_SELECT),
  1308. "IBX PCH dp port still using transcoder B\n");
  1309. }
  1310. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1311. enum pipe pipe, i915_reg_t reg)
  1312. {
  1313. u32 val = I915_READ(reg);
  1314. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1315. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1316. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1317. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1318. && (val & SDVO_PIPE_B_SELECT),
  1319. "IBX PCH hdmi port still using transcoder B\n");
  1320. }
  1321. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1322. enum pipe pipe)
  1323. {
  1324. u32 val;
  1325. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1326. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1327. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1328. val = I915_READ(PCH_ADPA);
  1329. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1330. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1331. pipe_name(pipe));
  1332. val = I915_READ(PCH_LVDS);
  1333. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1334. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1335. pipe_name(pipe));
  1336. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1337. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1338. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1339. }
  1340. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1341. const struct intel_crtc_state *pipe_config)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. enum pipe pipe = crtc->pipe;
  1345. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1346. POSTING_READ(DPLL(pipe));
  1347. udelay(150);
  1348. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1349. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1350. }
  1351. static void vlv_enable_pll(struct intel_crtc *crtc,
  1352. const struct intel_crtc_state *pipe_config)
  1353. {
  1354. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1355. enum pipe pipe = crtc->pipe;
  1356. assert_pipe_disabled(dev_priv, pipe);
  1357. /* PLL is protected by panel, make sure we can write it */
  1358. assert_panel_unlocked(dev_priv, pipe);
  1359. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1360. _vlv_enable_pll(crtc, pipe_config);
  1361. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1362. POSTING_READ(DPLL_MD(pipe));
  1363. }
  1364. static void _chv_enable_pll(struct intel_crtc *crtc,
  1365. const struct intel_crtc_state *pipe_config)
  1366. {
  1367. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1368. enum pipe pipe = crtc->pipe;
  1369. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1370. u32 tmp;
  1371. mutex_lock(&dev_priv->sb_lock);
  1372. /* Enable back the 10bit clock to display controller */
  1373. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1374. tmp |= DPIO_DCLKP_EN;
  1375. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1376. mutex_unlock(&dev_priv->sb_lock);
  1377. /*
  1378. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1379. */
  1380. udelay(1);
  1381. /* Enable PLL */
  1382. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1383. /* Check PLL is locked */
  1384. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1385. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1386. }
  1387. static void chv_enable_pll(struct intel_crtc *crtc,
  1388. const struct intel_crtc_state *pipe_config)
  1389. {
  1390. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1391. enum pipe pipe = crtc->pipe;
  1392. assert_pipe_disabled(dev_priv, pipe);
  1393. /* PLL is protected by panel, make sure we can write it */
  1394. assert_panel_unlocked(dev_priv, pipe);
  1395. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1396. _chv_enable_pll(crtc, pipe_config);
  1397. if (pipe != PIPE_A) {
  1398. /*
  1399. * WaPixelRepeatModeFixForC0:chv
  1400. *
  1401. * DPLLCMD is AWOL. Use chicken bits to propagate
  1402. * the value from DPLLBMD to either pipe B or C.
  1403. */
  1404. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1405. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1406. I915_WRITE(CBR4_VLV, 0);
  1407. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1408. /*
  1409. * DPLLB VGA mode also seems to cause problems.
  1410. * We should always have it disabled.
  1411. */
  1412. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1413. } else {
  1414. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1415. POSTING_READ(DPLL_MD(pipe));
  1416. }
  1417. }
  1418. static int intel_num_dvo_pipes(struct drm_device *dev)
  1419. {
  1420. struct intel_crtc *crtc;
  1421. int count = 0;
  1422. for_each_intel_crtc(dev, crtc)
  1423. count += crtc->base.state->active &&
  1424. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1425. return count;
  1426. }
  1427. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1428. {
  1429. struct drm_device *dev = crtc->base.dev;
  1430. struct drm_i915_private *dev_priv = dev->dev_private;
  1431. i915_reg_t reg = DPLL(crtc->pipe);
  1432. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1433. assert_pipe_disabled(dev_priv, crtc->pipe);
  1434. /* PLL is protected by panel, make sure we can write it */
  1435. if (IS_MOBILE(dev) && !IS_I830(dev))
  1436. assert_panel_unlocked(dev_priv, crtc->pipe);
  1437. /* Enable DVO 2x clock on both PLLs if necessary */
  1438. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1439. /*
  1440. * It appears to be important that we don't enable this
  1441. * for the current pipe before otherwise configuring the
  1442. * PLL. No idea how this should be handled if multiple
  1443. * DVO outputs are enabled simultaneosly.
  1444. */
  1445. dpll |= DPLL_DVO_2X_MODE;
  1446. I915_WRITE(DPLL(!crtc->pipe),
  1447. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1448. }
  1449. /*
  1450. * Apparently we need to have VGA mode enabled prior to changing
  1451. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1452. * dividers, even though the register value does change.
  1453. */
  1454. I915_WRITE(reg, 0);
  1455. I915_WRITE(reg, dpll);
  1456. /* Wait for the clocks to stabilize. */
  1457. POSTING_READ(reg);
  1458. udelay(150);
  1459. if (INTEL_INFO(dev)->gen >= 4) {
  1460. I915_WRITE(DPLL_MD(crtc->pipe),
  1461. crtc->config->dpll_hw_state.dpll_md);
  1462. } else {
  1463. /* The pixel multiplier can only be updated once the
  1464. * DPLL is enabled and the clocks are stable.
  1465. *
  1466. * So write it again.
  1467. */
  1468. I915_WRITE(reg, dpll);
  1469. }
  1470. /* We do this three times for luck */
  1471. I915_WRITE(reg, dpll);
  1472. POSTING_READ(reg);
  1473. udelay(150); /* wait for warmup */
  1474. I915_WRITE(reg, dpll);
  1475. POSTING_READ(reg);
  1476. udelay(150); /* wait for warmup */
  1477. I915_WRITE(reg, dpll);
  1478. POSTING_READ(reg);
  1479. udelay(150); /* wait for warmup */
  1480. }
  1481. /**
  1482. * i9xx_disable_pll - disable a PLL
  1483. * @dev_priv: i915 private structure
  1484. * @pipe: pipe PLL to disable
  1485. *
  1486. * Disable the PLL for @pipe, making sure the pipe is off first.
  1487. *
  1488. * Note! This is for pre-ILK only.
  1489. */
  1490. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1491. {
  1492. struct drm_device *dev = crtc->base.dev;
  1493. struct drm_i915_private *dev_priv = dev->dev_private;
  1494. enum pipe pipe = crtc->pipe;
  1495. /* Disable DVO 2x clock on both PLLs if necessary */
  1496. if (IS_I830(dev) &&
  1497. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1498. !intel_num_dvo_pipes(dev)) {
  1499. I915_WRITE(DPLL(PIPE_B),
  1500. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1501. I915_WRITE(DPLL(PIPE_A),
  1502. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1503. }
  1504. /* Don't disable pipe or pipe PLLs if needed */
  1505. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1506. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1507. return;
  1508. /* Make sure the pipe isn't still relying on us */
  1509. assert_pipe_disabled(dev_priv, pipe);
  1510. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1511. POSTING_READ(DPLL(pipe));
  1512. }
  1513. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1514. {
  1515. u32 val;
  1516. /* Make sure the pipe isn't still relying on us */
  1517. assert_pipe_disabled(dev_priv, pipe);
  1518. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1519. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1520. if (pipe != PIPE_A)
  1521. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1522. I915_WRITE(DPLL(pipe), val);
  1523. POSTING_READ(DPLL(pipe));
  1524. }
  1525. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1526. {
  1527. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1528. u32 val;
  1529. /* Make sure the pipe isn't still relying on us */
  1530. assert_pipe_disabled(dev_priv, pipe);
  1531. val = DPLL_SSC_REF_CLK_CHV |
  1532. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1533. if (pipe != PIPE_A)
  1534. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1535. I915_WRITE(DPLL(pipe), val);
  1536. POSTING_READ(DPLL(pipe));
  1537. mutex_lock(&dev_priv->sb_lock);
  1538. /* Disable 10bit clock to display controller */
  1539. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1540. val &= ~DPIO_DCLKP_EN;
  1541. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1542. mutex_unlock(&dev_priv->sb_lock);
  1543. }
  1544. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1545. struct intel_digital_port *dport,
  1546. unsigned int expected_mask)
  1547. {
  1548. u32 port_mask;
  1549. i915_reg_t dpll_reg;
  1550. switch (dport->port) {
  1551. case PORT_B:
  1552. port_mask = DPLL_PORTB_READY_MASK;
  1553. dpll_reg = DPLL(0);
  1554. break;
  1555. case PORT_C:
  1556. port_mask = DPLL_PORTC_READY_MASK;
  1557. dpll_reg = DPLL(0);
  1558. expected_mask <<= 4;
  1559. break;
  1560. case PORT_D:
  1561. port_mask = DPLL_PORTD_READY_MASK;
  1562. dpll_reg = DPIO_PHY_STATUS;
  1563. break;
  1564. default:
  1565. BUG();
  1566. }
  1567. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1568. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1569. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1570. }
  1571. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1572. enum pipe pipe)
  1573. {
  1574. struct drm_device *dev = dev_priv->dev;
  1575. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1576. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1577. i915_reg_t reg;
  1578. uint32_t val, pipeconf_val;
  1579. /* Make sure PCH DPLL is enabled */
  1580. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1581. /* FDI must be feeding us bits for PCH ports */
  1582. assert_fdi_tx_enabled(dev_priv, pipe);
  1583. assert_fdi_rx_enabled(dev_priv, pipe);
  1584. if (HAS_PCH_CPT(dev)) {
  1585. /* Workaround: Set the timing override bit before enabling the
  1586. * pch transcoder. */
  1587. reg = TRANS_CHICKEN2(pipe);
  1588. val = I915_READ(reg);
  1589. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1590. I915_WRITE(reg, val);
  1591. }
  1592. reg = PCH_TRANSCONF(pipe);
  1593. val = I915_READ(reg);
  1594. pipeconf_val = I915_READ(PIPECONF(pipe));
  1595. if (HAS_PCH_IBX(dev_priv)) {
  1596. /*
  1597. * Make the BPC in transcoder be consistent with
  1598. * that in pipeconf reg. For HDMI we must use 8bpc
  1599. * here for both 8bpc and 12bpc.
  1600. */
  1601. val &= ~PIPECONF_BPC_MASK;
  1602. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1603. val |= PIPECONF_8BPC;
  1604. else
  1605. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1606. }
  1607. val &= ~TRANS_INTERLACE_MASK;
  1608. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1609. if (HAS_PCH_IBX(dev_priv) &&
  1610. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1611. val |= TRANS_LEGACY_INTERLACED_ILK;
  1612. else
  1613. val |= TRANS_INTERLACED;
  1614. else
  1615. val |= TRANS_PROGRESSIVE;
  1616. I915_WRITE(reg, val | TRANS_ENABLE);
  1617. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1618. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1619. }
  1620. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1621. enum transcoder cpu_transcoder)
  1622. {
  1623. u32 val, pipeconf_val;
  1624. /* FDI must be feeding us bits for PCH ports */
  1625. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1626. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1627. /* Workaround: set timing override bit. */
  1628. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1629. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1630. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1631. val = TRANS_ENABLE;
  1632. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1633. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1634. PIPECONF_INTERLACED_ILK)
  1635. val |= TRANS_INTERLACED;
  1636. else
  1637. val |= TRANS_PROGRESSIVE;
  1638. I915_WRITE(LPT_TRANSCONF, val);
  1639. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1640. DRM_ERROR("Failed to enable PCH transcoder\n");
  1641. }
  1642. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1643. enum pipe pipe)
  1644. {
  1645. struct drm_device *dev = dev_priv->dev;
  1646. i915_reg_t reg;
  1647. uint32_t val;
  1648. /* FDI relies on the transcoder */
  1649. assert_fdi_tx_disabled(dev_priv, pipe);
  1650. assert_fdi_rx_disabled(dev_priv, pipe);
  1651. /* Ports must be off as well */
  1652. assert_pch_ports_disabled(dev_priv, pipe);
  1653. reg = PCH_TRANSCONF(pipe);
  1654. val = I915_READ(reg);
  1655. val &= ~TRANS_ENABLE;
  1656. I915_WRITE(reg, val);
  1657. /* wait for PCH transcoder off, transcoder state */
  1658. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1659. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1660. if (HAS_PCH_CPT(dev)) {
  1661. /* Workaround: Clear the timing override chicken bit again. */
  1662. reg = TRANS_CHICKEN2(pipe);
  1663. val = I915_READ(reg);
  1664. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1665. I915_WRITE(reg, val);
  1666. }
  1667. }
  1668. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1669. {
  1670. u32 val;
  1671. val = I915_READ(LPT_TRANSCONF);
  1672. val &= ~TRANS_ENABLE;
  1673. I915_WRITE(LPT_TRANSCONF, val);
  1674. /* wait for PCH transcoder off, transcoder state */
  1675. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1676. DRM_ERROR("Failed to disable PCH transcoder\n");
  1677. /* Workaround: clear timing override bit. */
  1678. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1679. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1680. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1681. }
  1682. /**
  1683. * intel_enable_pipe - enable a pipe, asserting requirements
  1684. * @crtc: crtc responsible for the pipe
  1685. *
  1686. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1687. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1688. */
  1689. static void intel_enable_pipe(struct intel_crtc *crtc)
  1690. {
  1691. struct drm_device *dev = crtc->base.dev;
  1692. struct drm_i915_private *dev_priv = dev->dev_private;
  1693. enum pipe pipe = crtc->pipe;
  1694. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1695. enum pipe pch_transcoder;
  1696. i915_reg_t reg;
  1697. u32 val;
  1698. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1699. assert_planes_disabled(dev_priv, pipe);
  1700. assert_cursor_disabled(dev_priv, pipe);
  1701. assert_sprites_disabled(dev_priv, pipe);
  1702. if (HAS_PCH_LPT(dev_priv))
  1703. pch_transcoder = TRANSCODER_A;
  1704. else
  1705. pch_transcoder = pipe;
  1706. /*
  1707. * A pipe without a PLL won't actually be able to drive bits from
  1708. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1709. * need the check.
  1710. */
  1711. if (HAS_GMCH_DISPLAY(dev_priv))
  1712. if (crtc->config->has_dsi_encoder)
  1713. assert_dsi_pll_enabled(dev_priv);
  1714. else
  1715. assert_pll_enabled(dev_priv, pipe);
  1716. else {
  1717. if (crtc->config->has_pch_encoder) {
  1718. /* if driving the PCH, we need FDI enabled */
  1719. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1720. assert_fdi_tx_pll_enabled(dev_priv,
  1721. (enum pipe) cpu_transcoder);
  1722. }
  1723. /* FIXME: assert CPU port conditions for SNB+ */
  1724. }
  1725. reg = PIPECONF(cpu_transcoder);
  1726. val = I915_READ(reg);
  1727. if (val & PIPECONF_ENABLE) {
  1728. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1729. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1730. return;
  1731. }
  1732. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1733. POSTING_READ(reg);
  1734. /*
  1735. * Until the pipe starts DSL will read as 0, which would cause
  1736. * an apparent vblank timestamp jump, which messes up also the
  1737. * frame count when it's derived from the timestamps. So let's
  1738. * wait for the pipe to start properly before we call
  1739. * drm_crtc_vblank_on()
  1740. */
  1741. if (dev->max_vblank_count == 0 &&
  1742. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1743. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1744. }
  1745. /**
  1746. * intel_disable_pipe - disable a pipe, asserting requirements
  1747. * @crtc: crtc whose pipes is to be disabled
  1748. *
  1749. * Disable the pipe of @crtc, making sure that various hardware
  1750. * specific requirements are met, if applicable, e.g. plane
  1751. * disabled, panel fitter off, etc.
  1752. *
  1753. * Will wait until the pipe has shut down before returning.
  1754. */
  1755. static void intel_disable_pipe(struct intel_crtc *crtc)
  1756. {
  1757. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1758. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1759. enum pipe pipe = crtc->pipe;
  1760. i915_reg_t reg;
  1761. u32 val;
  1762. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1763. /*
  1764. * Make sure planes won't keep trying to pump pixels to us,
  1765. * or we might hang the display.
  1766. */
  1767. assert_planes_disabled(dev_priv, pipe);
  1768. assert_cursor_disabled(dev_priv, pipe);
  1769. assert_sprites_disabled(dev_priv, pipe);
  1770. reg = PIPECONF(cpu_transcoder);
  1771. val = I915_READ(reg);
  1772. if ((val & PIPECONF_ENABLE) == 0)
  1773. return;
  1774. /*
  1775. * Double wide has implications for planes
  1776. * so best keep it disabled when not needed.
  1777. */
  1778. if (crtc->config->double_wide)
  1779. val &= ~PIPECONF_DOUBLE_WIDE;
  1780. /* Don't disable pipe or pipe PLLs if needed */
  1781. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1782. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1783. val &= ~PIPECONF_ENABLE;
  1784. I915_WRITE(reg, val);
  1785. if ((val & PIPECONF_ENABLE) == 0)
  1786. intel_wait_for_pipe_off(crtc);
  1787. }
  1788. static bool need_vtd_wa(struct drm_device *dev)
  1789. {
  1790. #ifdef CONFIG_INTEL_IOMMU
  1791. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1792. return true;
  1793. #endif
  1794. return false;
  1795. }
  1796. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1797. {
  1798. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1799. }
  1800. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1801. uint64_t fb_modifier, unsigned int cpp)
  1802. {
  1803. switch (fb_modifier) {
  1804. case DRM_FORMAT_MOD_NONE:
  1805. return cpp;
  1806. case I915_FORMAT_MOD_X_TILED:
  1807. if (IS_GEN2(dev_priv))
  1808. return 128;
  1809. else
  1810. return 512;
  1811. case I915_FORMAT_MOD_Y_TILED:
  1812. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1813. return 128;
  1814. else
  1815. return 512;
  1816. case I915_FORMAT_MOD_Yf_TILED:
  1817. switch (cpp) {
  1818. case 1:
  1819. return 64;
  1820. case 2:
  1821. case 4:
  1822. return 128;
  1823. case 8:
  1824. case 16:
  1825. return 256;
  1826. default:
  1827. MISSING_CASE(cpp);
  1828. return cpp;
  1829. }
  1830. break;
  1831. default:
  1832. MISSING_CASE(fb_modifier);
  1833. return cpp;
  1834. }
  1835. }
  1836. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1837. uint64_t fb_modifier, unsigned int cpp)
  1838. {
  1839. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1840. return 1;
  1841. else
  1842. return intel_tile_size(dev_priv) /
  1843. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1844. }
  1845. /* Return the tile dimensions in pixel units */
  1846. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1847. unsigned int *tile_width,
  1848. unsigned int *tile_height,
  1849. uint64_t fb_modifier,
  1850. unsigned int cpp)
  1851. {
  1852. unsigned int tile_width_bytes =
  1853. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1854. *tile_width = tile_width_bytes / cpp;
  1855. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1856. }
  1857. unsigned int
  1858. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1859. uint32_t pixel_format, uint64_t fb_modifier)
  1860. {
  1861. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1862. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1863. return ALIGN(height, tile_height);
  1864. }
  1865. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1866. {
  1867. unsigned int size = 0;
  1868. int i;
  1869. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1870. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1871. return size;
  1872. }
  1873. static void
  1874. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1875. const struct drm_framebuffer *fb,
  1876. unsigned int rotation)
  1877. {
  1878. if (intel_rotation_90_or_270(rotation)) {
  1879. *view = i915_ggtt_view_rotated;
  1880. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1881. } else {
  1882. *view = i915_ggtt_view_normal;
  1883. }
  1884. }
  1885. static void
  1886. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1887. struct drm_framebuffer *fb)
  1888. {
  1889. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1890. unsigned int tile_size, tile_width, tile_height, cpp;
  1891. tile_size = intel_tile_size(dev_priv);
  1892. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1893. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1894. fb->modifier[0], cpp);
  1895. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1896. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1897. if (info->pixel_format == DRM_FORMAT_NV12) {
  1898. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1899. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1900. fb->modifier[1], cpp);
  1901. info->uv_offset = fb->offsets[1];
  1902. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1903. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1904. }
  1905. }
  1906. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1907. {
  1908. if (INTEL_INFO(dev_priv)->gen >= 9)
  1909. return 256 * 1024;
  1910. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1911. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1912. return 128 * 1024;
  1913. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1914. return 4 * 1024;
  1915. else
  1916. return 0;
  1917. }
  1918. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1919. uint64_t fb_modifier)
  1920. {
  1921. switch (fb_modifier) {
  1922. case DRM_FORMAT_MOD_NONE:
  1923. return intel_linear_alignment(dev_priv);
  1924. case I915_FORMAT_MOD_X_TILED:
  1925. if (INTEL_INFO(dev_priv)->gen >= 9)
  1926. return 256 * 1024;
  1927. return 0;
  1928. case I915_FORMAT_MOD_Y_TILED:
  1929. case I915_FORMAT_MOD_Yf_TILED:
  1930. return 1 * 1024 * 1024;
  1931. default:
  1932. MISSING_CASE(fb_modifier);
  1933. return 0;
  1934. }
  1935. }
  1936. int
  1937. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1938. unsigned int rotation)
  1939. {
  1940. struct drm_device *dev = fb->dev;
  1941. struct drm_i915_private *dev_priv = dev->dev_private;
  1942. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1943. struct i915_ggtt_view view;
  1944. u32 alignment;
  1945. int ret;
  1946. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1947. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1948. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1949. /* Note that the w/a also requires 64 PTE of padding following the
  1950. * bo. We currently fill all unused PTE with the shadow page and so
  1951. * we should always have valid PTE following the scanout preventing
  1952. * the VT-d warning.
  1953. */
  1954. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1955. alignment = 256 * 1024;
  1956. /*
  1957. * Global gtt pte registers are special registers which actually forward
  1958. * writes to a chunk of system memory. Which means that there is no risk
  1959. * that the register values disappear as soon as we call
  1960. * intel_runtime_pm_put(), so it is correct to wrap only the
  1961. * pin/unpin/fence and not more.
  1962. */
  1963. intel_runtime_pm_get(dev_priv);
  1964. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1965. &view);
  1966. if (ret)
  1967. goto err_pm;
  1968. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1969. * fence, whereas 965+ only requires a fence if using
  1970. * framebuffer compression. For simplicity, we always install
  1971. * a fence as the cost is not that onerous.
  1972. */
  1973. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1974. ret = i915_gem_object_get_fence(obj);
  1975. if (ret == -EDEADLK) {
  1976. /*
  1977. * -EDEADLK means there are no free fences
  1978. * no pending flips.
  1979. *
  1980. * This is propagated to atomic, but it uses
  1981. * -EDEADLK to force a locking recovery, so
  1982. * change the returned error to -EBUSY.
  1983. */
  1984. ret = -EBUSY;
  1985. goto err_unpin;
  1986. } else if (ret)
  1987. goto err_unpin;
  1988. i915_gem_object_pin_fence(obj);
  1989. }
  1990. intel_runtime_pm_put(dev_priv);
  1991. return 0;
  1992. err_unpin:
  1993. i915_gem_object_unpin_from_display_plane(obj, &view);
  1994. err_pm:
  1995. intel_runtime_pm_put(dev_priv);
  1996. return ret;
  1997. }
  1998. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1999. {
  2000. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2001. struct i915_ggtt_view view;
  2002. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2003. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2004. if (view.type == I915_GGTT_VIEW_NORMAL)
  2005. i915_gem_object_unpin_fence(obj);
  2006. i915_gem_object_unpin_from_display_plane(obj, &view);
  2007. }
  2008. /*
  2009. * Adjust the tile offset by moving the difference into
  2010. * the x/y offsets.
  2011. *
  2012. * Input tile dimensions and pitch must already be
  2013. * rotated to match x and y, and in pixel units.
  2014. */
  2015. static u32 intel_adjust_tile_offset(int *x, int *y,
  2016. unsigned int tile_width,
  2017. unsigned int tile_height,
  2018. unsigned int tile_size,
  2019. unsigned int pitch_tiles,
  2020. u32 old_offset,
  2021. u32 new_offset)
  2022. {
  2023. unsigned int tiles;
  2024. WARN_ON(old_offset & (tile_size - 1));
  2025. WARN_ON(new_offset & (tile_size - 1));
  2026. WARN_ON(new_offset > old_offset);
  2027. tiles = (old_offset - new_offset) / tile_size;
  2028. *y += tiles / pitch_tiles * tile_height;
  2029. *x += tiles % pitch_tiles * tile_width;
  2030. return new_offset;
  2031. }
  2032. /*
  2033. * Computes the linear offset to the base tile and adjusts
  2034. * x, y. bytes per pixel is assumed to be a power-of-two.
  2035. *
  2036. * In the 90/270 rotated case, x and y are assumed
  2037. * to be already rotated to match the rotated GTT view, and
  2038. * pitch is the tile_height aligned framebuffer height.
  2039. */
  2040. u32 intel_compute_tile_offset(int *x, int *y,
  2041. const struct drm_framebuffer *fb, int plane,
  2042. unsigned int pitch,
  2043. unsigned int rotation)
  2044. {
  2045. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2046. uint64_t fb_modifier = fb->modifier[plane];
  2047. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2048. u32 offset, offset_aligned, alignment;
  2049. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2050. if (alignment)
  2051. alignment--;
  2052. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2053. unsigned int tile_size, tile_width, tile_height;
  2054. unsigned int tile_rows, tiles, pitch_tiles;
  2055. tile_size = intel_tile_size(dev_priv);
  2056. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2057. fb_modifier, cpp);
  2058. if (intel_rotation_90_or_270(rotation)) {
  2059. pitch_tiles = pitch / tile_height;
  2060. swap(tile_width, tile_height);
  2061. } else {
  2062. pitch_tiles = pitch / (tile_width * cpp);
  2063. }
  2064. tile_rows = *y / tile_height;
  2065. *y %= tile_height;
  2066. tiles = *x / tile_width;
  2067. *x %= tile_width;
  2068. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2069. offset_aligned = offset & ~alignment;
  2070. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2071. tile_size, pitch_tiles,
  2072. offset, offset_aligned);
  2073. } else {
  2074. offset = *y * pitch + *x * cpp;
  2075. offset_aligned = offset & ~alignment;
  2076. *y = (offset & alignment) / pitch;
  2077. *x = ((offset & alignment) - *y * pitch) / cpp;
  2078. }
  2079. return offset_aligned;
  2080. }
  2081. static int i9xx_format_to_fourcc(int format)
  2082. {
  2083. switch (format) {
  2084. case DISPPLANE_8BPP:
  2085. return DRM_FORMAT_C8;
  2086. case DISPPLANE_BGRX555:
  2087. return DRM_FORMAT_XRGB1555;
  2088. case DISPPLANE_BGRX565:
  2089. return DRM_FORMAT_RGB565;
  2090. default:
  2091. case DISPPLANE_BGRX888:
  2092. return DRM_FORMAT_XRGB8888;
  2093. case DISPPLANE_RGBX888:
  2094. return DRM_FORMAT_XBGR8888;
  2095. case DISPPLANE_BGRX101010:
  2096. return DRM_FORMAT_XRGB2101010;
  2097. case DISPPLANE_RGBX101010:
  2098. return DRM_FORMAT_XBGR2101010;
  2099. }
  2100. }
  2101. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2102. {
  2103. switch (format) {
  2104. case PLANE_CTL_FORMAT_RGB_565:
  2105. return DRM_FORMAT_RGB565;
  2106. default:
  2107. case PLANE_CTL_FORMAT_XRGB_8888:
  2108. if (rgb_order) {
  2109. if (alpha)
  2110. return DRM_FORMAT_ABGR8888;
  2111. else
  2112. return DRM_FORMAT_XBGR8888;
  2113. } else {
  2114. if (alpha)
  2115. return DRM_FORMAT_ARGB8888;
  2116. else
  2117. return DRM_FORMAT_XRGB8888;
  2118. }
  2119. case PLANE_CTL_FORMAT_XRGB_2101010:
  2120. if (rgb_order)
  2121. return DRM_FORMAT_XBGR2101010;
  2122. else
  2123. return DRM_FORMAT_XRGB2101010;
  2124. }
  2125. }
  2126. static bool
  2127. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2128. struct intel_initial_plane_config *plane_config)
  2129. {
  2130. struct drm_device *dev = crtc->base.dev;
  2131. struct drm_i915_private *dev_priv = to_i915(dev);
  2132. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2133. struct drm_i915_gem_object *obj = NULL;
  2134. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2135. struct drm_framebuffer *fb = &plane_config->fb->base;
  2136. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2137. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2138. PAGE_SIZE);
  2139. size_aligned -= base_aligned;
  2140. if (plane_config->size == 0)
  2141. return false;
  2142. /* If the FB is too big, just don't use it since fbdev is not very
  2143. * important and we should probably use that space with FBC or other
  2144. * features. */
  2145. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2146. return false;
  2147. mutex_lock(&dev->struct_mutex);
  2148. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2149. base_aligned,
  2150. base_aligned,
  2151. size_aligned);
  2152. if (!obj) {
  2153. mutex_unlock(&dev->struct_mutex);
  2154. return false;
  2155. }
  2156. obj->tiling_mode = plane_config->tiling;
  2157. if (obj->tiling_mode == I915_TILING_X)
  2158. obj->stride = fb->pitches[0];
  2159. mode_cmd.pixel_format = fb->pixel_format;
  2160. mode_cmd.width = fb->width;
  2161. mode_cmd.height = fb->height;
  2162. mode_cmd.pitches[0] = fb->pitches[0];
  2163. mode_cmd.modifier[0] = fb->modifier[0];
  2164. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2165. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2166. &mode_cmd, obj)) {
  2167. DRM_DEBUG_KMS("intel fb init failed\n");
  2168. goto out_unref_obj;
  2169. }
  2170. mutex_unlock(&dev->struct_mutex);
  2171. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2172. return true;
  2173. out_unref_obj:
  2174. drm_gem_object_unreference(&obj->base);
  2175. mutex_unlock(&dev->struct_mutex);
  2176. return false;
  2177. }
  2178. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2179. static void
  2180. update_state_fb(struct drm_plane *plane)
  2181. {
  2182. if (plane->fb == plane->state->fb)
  2183. return;
  2184. if (plane->state->fb)
  2185. drm_framebuffer_unreference(plane->state->fb);
  2186. plane->state->fb = plane->fb;
  2187. if (plane->state->fb)
  2188. drm_framebuffer_reference(plane->state->fb);
  2189. }
  2190. static void
  2191. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2192. struct intel_initial_plane_config *plane_config)
  2193. {
  2194. struct drm_device *dev = intel_crtc->base.dev;
  2195. struct drm_i915_private *dev_priv = dev->dev_private;
  2196. struct drm_crtc *c;
  2197. struct intel_crtc *i;
  2198. struct drm_i915_gem_object *obj;
  2199. struct drm_plane *primary = intel_crtc->base.primary;
  2200. struct drm_plane_state *plane_state = primary->state;
  2201. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2202. struct intel_plane *intel_plane = to_intel_plane(primary);
  2203. struct intel_plane_state *intel_state =
  2204. to_intel_plane_state(plane_state);
  2205. struct drm_framebuffer *fb;
  2206. if (!plane_config->fb)
  2207. return;
  2208. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2209. fb = &plane_config->fb->base;
  2210. goto valid_fb;
  2211. }
  2212. kfree(plane_config->fb);
  2213. /*
  2214. * Failed to alloc the obj, check to see if we should share
  2215. * an fb with another CRTC instead
  2216. */
  2217. for_each_crtc(dev, c) {
  2218. i = to_intel_crtc(c);
  2219. if (c == &intel_crtc->base)
  2220. continue;
  2221. if (!i->active)
  2222. continue;
  2223. fb = c->primary->fb;
  2224. if (!fb)
  2225. continue;
  2226. obj = intel_fb_obj(fb);
  2227. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2228. drm_framebuffer_reference(fb);
  2229. goto valid_fb;
  2230. }
  2231. }
  2232. /*
  2233. * We've failed to reconstruct the BIOS FB. Current display state
  2234. * indicates that the primary plane is visible, but has a NULL FB,
  2235. * which will lead to problems later if we don't fix it up. The
  2236. * simplest solution is to just disable the primary plane now and
  2237. * pretend the BIOS never had it enabled.
  2238. */
  2239. to_intel_plane_state(plane_state)->visible = false;
  2240. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2241. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2242. intel_plane->disable_plane(primary, &intel_crtc->base);
  2243. return;
  2244. valid_fb:
  2245. plane_state->src_x = 0;
  2246. plane_state->src_y = 0;
  2247. plane_state->src_w = fb->width << 16;
  2248. plane_state->src_h = fb->height << 16;
  2249. plane_state->crtc_x = 0;
  2250. plane_state->crtc_y = 0;
  2251. plane_state->crtc_w = fb->width;
  2252. plane_state->crtc_h = fb->height;
  2253. intel_state->src.x1 = plane_state->src_x;
  2254. intel_state->src.y1 = plane_state->src_y;
  2255. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2256. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2257. intel_state->dst.x1 = plane_state->crtc_x;
  2258. intel_state->dst.y1 = plane_state->crtc_y;
  2259. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2260. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2261. obj = intel_fb_obj(fb);
  2262. if (obj->tiling_mode != I915_TILING_NONE)
  2263. dev_priv->preserve_bios_swizzle = true;
  2264. drm_framebuffer_reference(fb);
  2265. primary->fb = primary->state->fb = fb;
  2266. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2267. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2268. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2269. }
  2270. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2271. const struct intel_crtc_state *crtc_state,
  2272. const struct intel_plane_state *plane_state)
  2273. {
  2274. struct drm_device *dev = primary->dev;
  2275. struct drm_i915_private *dev_priv = dev->dev_private;
  2276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2277. struct drm_framebuffer *fb = plane_state->base.fb;
  2278. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2279. int plane = intel_crtc->plane;
  2280. u32 linear_offset;
  2281. u32 dspcntr;
  2282. i915_reg_t reg = DSPCNTR(plane);
  2283. unsigned int rotation = plane_state->base.rotation;
  2284. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2285. int x = plane_state->src.x1 >> 16;
  2286. int y = plane_state->src.y1 >> 16;
  2287. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2288. dspcntr |= DISPLAY_PLANE_ENABLE;
  2289. if (INTEL_INFO(dev)->gen < 4) {
  2290. if (intel_crtc->pipe == PIPE_B)
  2291. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2292. /* pipesrc and dspsize control the size that is scaled from,
  2293. * which should always be the user's requested size.
  2294. */
  2295. I915_WRITE(DSPSIZE(plane),
  2296. ((crtc_state->pipe_src_h - 1) << 16) |
  2297. (crtc_state->pipe_src_w - 1));
  2298. I915_WRITE(DSPPOS(plane), 0);
  2299. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2300. I915_WRITE(PRIMSIZE(plane),
  2301. ((crtc_state->pipe_src_h - 1) << 16) |
  2302. (crtc_state->pipe_src_w - 1));
  2303. I915_WRITE(PRIMPOS(plane), 0);
  2304. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2305. }
  2306. switch (fb->pixel_format) {
  2307. case DRM_FORMAT_C8:
  2308. dspcntr |= DISPPLANE_8BPP;
  2309. break;
  2310. case DRM_FORMAT_XRGB1555:
  2311. dspcntr |= DISPPLANE_BGRX555;
  2312. break;
  2313. case DRM_FORMAT_RGB565:
  2314. dspcntr |= DISPPLANE_BGRX565;
  2315. break;
  2316. case DRM_FORMAT_XRGB8888:
  2317. dspcntr |= DISPPLANE_BGRX888;
  2318. break;
  2319. case DRM_FORMAT_XBGR8888:
  2320. dspcntr |= DISPPLANE_RGBX888;
  2321. break;
  2322. case DRM_FORMAT_XRGB2101010:
  2323. dspcntr |= DISPPLANE_BGRX101010;
  2324. break;
  2325. case DRM_FORMAT_XBGR2101010:
  2326. dspcntr |= DISPPLANE_RGBX101010;
  2327. break;
  2328. default:
  2329. BUG();
  2330. }
  2331. if (INTEL_INFO(dev)->gen >= 4 &&
  2332. obj->tiling_mode != I915_TILING_NONE)
  2333. dspcntr |= DISPPLANE_TILED;
  2334. if (IS_G4X(dev))
  2335. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2336. linear_offset = y * fb->pitches[0] + x * cpp;
  2337. if (INTEL_INFO(dev)->gen >= 4) {
  2338. intel_crtc->dspaddr_offset =
  2339. intel_compute_tile_offset(&x, &y, fb, 0,
  2340. fb->pitches[0], rotation);
  2341. linear_offset -= intel_crtc->dspaddr_offset;
  2342. } else {
  2343. intel_crtc->dspaddr_offset = linear_offset;
  2344. }
  2345. if (rotation == BIT(DRM_ROTATE_180)) {
  2346. dspcntr |= DISPPLANE_ROTATE_180;
  2347. x += (crtc_state->pipe_src_w - 1);
  2348. y += (crtc_state->pipe_src_h - 1);
  2349. /* Finding the last pixel of the last line of the display
  2350. data and adding to linear_offset*/
  2351. linear_offset +=
  2352. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2353. (crtc_state->pipe_src_w - 1) * cpp;
  2354. }
  2355. intel_crtc->adjusted_x = x;
  2356. intel_crtc->adjusted_y = y;
  2357. I915_WRITE(reg, dspcntr);
  2358. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2359. if (INTEL_INFO(dev)->gen >= 4) {
  2360. I915_WRITE(DSPSURF(plane),
  2361. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2362. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2363. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2364. } else
  2365. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2366. POSTING_READ(reg);
  2367. }
  2368. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2369. struct drm_crtc *crtc)
  2370. {
  2371. struct drm_device *dev = crtc->dev;
  2372. struct drm_i915_private *dev_priv = dev->dev_private;
  2373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2374. int plane = intel_crtc->plane;
  2375. I915_WRITE(DSPCNTR(plane), 0);
  2376. if (INTEL_INFO(dev_priv)->gen >= 4)
  2377. I915_WRITE(DSPSURF(plane), 0);
  2378. else
  2379. I915_WRITE(DSPADDR(plane), 0);
  2380. POSTING_READ(DSPCNTR(plane));
  2381. }
  2382. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2383. const struct intel_crtc_state *crtc_state,
  2384. const struct intel_plane_state *plane_state)
  2385. {
  2386. struct drm_device *dev = primary->dev;
  2387. struct drm_i915_private *dev_priv = dev->dev_private;
  2388. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2389. struct drm_framebuffer *fb = plane_state->base.fb;
  2390. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2391. int plane = intel_crtc->plane;
  2392. u32 linear_offset;
  2393. u32 dspcntr;
  2394. i915_reg_t reg = DSPCNTR(plane);
  2395. unsigned int rotation = plane_state->base.rotation;
  2396. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2397. int x = plane_state->src.x1 >> 16;
  2398. int y = plane_state->src.y1 >> 16;
  2399. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2400. dspcntr |= DISPLAY_PLANE_ENABLE;
  2401. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2402. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2403. switch (fb->pixel_format) {
  2404. case DRM_FORMAT_C8:
  2405. dspcntr |= DISPPLANE_8BPP;
  2406. break;
  2407. case DRM_FORMAT_RGB565:
  2408. dspcntr |= DISPPLANE_BGRX565;
  2409. break;
  2410. case DRM_FORMAT_XRGB8888:
  2411. dspcntr |= DISPPLANE_BGRX888;
  2412. break;
  2413. case DRM_FORMAT_XBGR8888:
  2414. dspcntr |= DISPPLANE_RGBX888;
  2415. break;
  2416. case DRM_FORMAT_XRGB2101010:
  2417. dspcntr |= DISPPLANE_BGRX101010;
  2418. break;
  2419. case DRM_FORMAT_XBGR2101010:
  2420. dspcntr |= DISPPLANE_RGBX101010;
  2421. break;
  2422. default:
  2423. BUG();
  2424. }
  2425. if (obj->tiling_mode != I915_TILING_NONE)
  2426. dspcntr |= DISPPLANE_TILED;
  2427. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2428. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2429. linear_offset = y * fb->pitches[0] + x * cpp;
  2430. intel_crtc->dspaddr_offset =
  2431. intel_compute_tile_offset(&x, &y, fb, 0,
  2432. fb->pitches[0], rotation);
  2433. linear_offset -= intel_crtc->dspaddr_offset;
  2434. if (rotation == BIT(DRM_ROTATE_180)) {
  2435. dspcntr |= DISPPLANE_ROTATE_180;
  2436. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2437. x += (crtc_state->pipe_src_w - 1);
  2438. y += (crtc_state->pipe_src_h - 1);
  2439. /* Finding the last pixel of the last line of the display
  2440. data and adding to linear_offset*/
  2441. linear_offset +=
  2442. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2443. (crtc_state->pipe_src_w - 1) * cpp;
  2444. }
  2445. }
  2446. intel_crtc->adjusted_x = x;
  2447. intel_crtc->adjusted_y = y;
  2448. I915_WRITE(reg, dspcntr);
  2449. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2450. I915_WRITE(DSPSURF(plane),
  2451. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2452. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2453. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2454. } else {
  2455. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2456. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2457. }
  2458. POSTING_READ(reg);
  2459. }
  2460. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2461. uint64_t fb_modifier, uint32_t pixel_format)
  2462. {
  2463. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2464. return 64;
  2465. } else {
  2466. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2467. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2468. }
  2469. }
  2470. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2471. struct drm_i915_gem_object *obj,
  2472. unsigned int plane)
  2473. {
  2474. struct i915_ggtt_view view;
  2475. struct i915_vma *vma;
  2476. u64 offset;
  2477. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2478. intel_plane->base.state->rotation);
  2479. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2480. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2481. view.type))
  2482. return -1;
  2483. offset = vma->node.start;
  2484. if (plane == 1) {
  2485. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2486. PAGE_SIZE;
  2487. }
  2488. WARN_ON(upper_32_bits(offset));
  2489. return lower_32_bits(offset);
  2490. }
  2491. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2492. {
  2493. struct drm_device *dev = intel_crtc->base.dev;
  2494. struct drm_i915_private *dev_priv = dev->dev_private;
  2495. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2496. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2497. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2498. }
  2499. /*
  2500. * This function detaches (aka. unbinds) unused scalers in hardware
  2501. */
  2502. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2503. {
  2504. struct intel_crtc_scaler_state *scaler_state;
  2505. int i;
  2506. scaler_state = &intel_crtc->config->scaler_state;
  2507. /* loop through and disable scalers that aren't in use */
  2508. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2509. if (!scaler_state->scalers[i].in_use)
  2510. skl_detach_scaler(intel_crtc, i);
  2511. }
  2512. }
  2513. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2514. {
  2515. switch (pixel_format) {
  2516. case DRM_FORMAT_C8:
  2517. return PLANE_CTL_FORMAT_INDEXED;
  2518. case DRM_FORMAT_RGB565:
  2519. return PLANE_CTL_FORMAT_RGB_565;
  2520. case DRM_FORMAT_XBGR8888:
  2521. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2522. case DRM_FORMAT_XRGB8888:
  2523. return PLANE_CTL_FORMAT_XRGB_8888;
  2524. /*
  2525. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2526. * to be already pre-multiplied. We need to add a knob (or a different
  2527. * DRM_FORMAT) for user-space to configure that.
  2528. */
  2529. case DRM_FORMAT_ABGR8888:
  2530. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2531. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2532. case DRM_FORMAT_ARGB8888:
  2533. return PLANE_CTL_FORMAT_XRGB_8888 |
  2534. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2535. case DRM_FORMAT_XRGB2101010:
  2536. return PLANE_CTL_FORMAT_XRGB_2101010;
  2537. case DRM_FORMAT_XBGR2101010:
  2538. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2539. case DRM_FORMAT_YUYV:
  2540. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2541. case DRM_FORMAT_YVYU:
  2542. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2543. case DRM_FORMAT_UYVY:
  2544. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2545. case DRM_FORMAT_VYUY:
  2546. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2547. default:
  2548. MISSING_CASE(pixel_format);
  2549. }
  2550. return 0;
  2551. }
  2552. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2553. {
  2554. switch (fb_modifier) {
  2555. case DRM_FORMAT_MOD_NONE:
  2556. break;
  2557. case I915_FORMAT_MOD_X_TILED:
  2558. return PLANE_CTL_TILED_X;
  2559. case I915_FORMAT_MOD_Y_TILED:
  2560. return PLANE_CTL_TILED_Y;
  2561. case I915_FORMAT_MOD_Yf_TILED:
  2562. return PLANE_CTL_TILED_YF;
  2563. default:
  2564. MISSING_CASE(fb_modifier);
  2565. }
  2566. return 0;
  2567. }
  2568. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2569. {
  2570. switch (rotation) {
  2571. case BIT(DRM_ROTATE_0):
  2572. break;
  2573. /*
  2574. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2575. * while i915 HW rotation is clockwise, thats why this swapping.
  2576. */
  2577. case BIT(DRM_ROTATE_90):
  2578. return PLANE_CTL_ROTATE_270;
  2579. case BIT(DRM_ROTATE_180):
  2580. return PLANE_CTL_ROTATE_180;
  2581. case BIT(DRM_ROTATE_270):
  2582. return PLANE_CTL_ROTATE_90;
  2583. default:
  2584. MISSING_CASE(rotation);
  2585. }
  2586. return 0;
  2587. }
  2588. static void skylake_update_primary_plane(struct drm_plane *plane,
  2589. const struct intel_crtc_state *crtc_state,
  2590. const struct intel_plane_state *plane_state)
  2591. {
  2592. struct drm_device *dev = plane->dev;
  2593. struct drm_i915_private *dev_priv = dev->dev_private;
  2594. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2595. struct drm_framebuffer *fb = plane_state->base.fb;
  2596. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2597. int pipe = intel_crtc->pipe;
  2598. u32 plane_ctl, stride_div, stride;
  2599. u32 tile_height, plane_offset, plane_size;
  2600. unsigned int rotation = plane_state->base.rotation;
  2601. int x_offset, y_offset;
  2602. u32 surf_addr;
  2603. int scaler_id = plane_state->scaler_id;
  2604. int src_x = plane_state->src.x1 >> 16;
  2605. int src_y = plane_state->src.y1 >> 16;
  2606. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2607. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2608. int dst_x = plane_state->dst.x1;
  2609. int dst_y = plane_state->dst.y1;
  2610. int dst_w = drm_rect_width(&plane_state->dst);
  2611. int dst_h = drm_rect_height(&plane_state->dst);
  2612. plane_ctl = PLANE_CTL_ENABLE |
  2613. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2614. PLANE_CTL_PIPE_CSC_ENABLE;
  2615. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2616. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2617. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2618. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2619. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2620. fb->pixel_format);
  2621. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2622. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2623. if (intel_rotation_90_or_270(rotation)) {
  2624. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2625. /* stride = Surface height in tiles */
  2626. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2627. stride = DIV_ROUND_UP(fb->height, tile_height);
  2628. x_offset = stride * tile_height - src_y - src_h;
  2629. y_offset = src_x;
  2630. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2631. } else {
  2632. stride = fb->pitches[0] / stride_div;
  2633. x_offset = src_x;
  2634. y_offset = src_y;
  2635. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2636. }
  2637. plane_offset = y_offset << 16 | x_offset;
  2638. intel_crtc->adjusted_x = x_offset;
  2639. intel_crtc->adjusted_y = y_offset;
  2640. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2641. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2642. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2643. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2644. if (scaler_id >= 0) {
  2645. uint32_t ps_ctrl = 0;
  2646. WARN_ON(!dst_w || !dst_h);
  2647. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2648. crtc_state->scaler_state.scalers[scaler_id].mode;
  2649. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2650. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2651. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2652. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2653. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2654. } else {
  2655. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2656. }
  2657. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2658. POSTING_READ(PLANE_SURF(pipe, 0));
  2659. }
  2660. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2661. struct drm_crtc *crtc)
  2662. {
  2663. struct drm_device *dev = crtc->dev;
  2664. struct drm_i915_private *dev_priv = dev->dev_private;
  2665. int pipe = to_intel_crtc(crtc)->pipe;
  2666. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2667. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2668. POSTING_READ(PLANE_SURF(pipe, 0));
  2669. }
  2670. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2671. static int
  2672. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2673. int x, int y, enum mode_set_atomic state)
  2674. {
  2675. /* Support for kgdboc is disabled, this needs a major rework. */
  2676. DRM_ERROR("legacy panic handler not supported any more.\n");
  2677. return -ENODEV;
  2678. }
  2679. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2680. {
  2681. struct intel_crtc *crtc;
  2682. for_each_intel_crtc(dev_priv->dev, crtc)
  2683. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2684. }
  2685. static void intel_update_primary_planes(struct drm_device *dev)
  2686. {
  2687. struct drm_crtc *crtc;
  2688. for_each_crtc(dev, crtc) {
  2689. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2690. struct intel_plane_state *plane_state;
  2691. drm_modeset_lock_crtc(crtc, &plane->base);
  2692. plane_state = to_intel_plane_state(plane->base.state);
  2693. if (plane_state->visible)
  2694. plane->update_plane(&plane->base,
  2695. to_intel_crtc_state(crtc->state),
  2696. plane_state);
  2697. drm_modeset_unlock_crtc(crtc);
  2698. }
  2699. }
  2700. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2701. {
  2702. /* no reset support for gen2 */
  2703. if (IS_GEN2(dev_priv))
  2704. return;
  2705. /* reset doesn't touch the display */
  2706. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2707. return;
  2708. drm_modeset_lock_all(dev_priv->dev);
  2709. /*
  2710. * Disabling the crtcs gracefully seems nicer. Also the
  2711. * g33 docs say we should at least disable all the planes.
  2712. */
  2713. intel_display_suspend(dev_priv->dev);
  2714. }
  2715. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2716. {
  2717. /*
  2718. * Flips in the rings will be nuked by the reset,
  2719. * so complete all pending flips so that user space
  2720. * will get its events and not get stuck.
  2721. */
  2722. intel_complete_page_flips(dev_priv);
  2723. /* no reset support for gen2 */
  2724. if (IS_GEN2(dev_priv))
  2725. return;
  2726. /* reset doesn't touch the display */
  2727. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2728. /*
  2729. * Flips in the rings have been nuked by the reset,
  2730. * so update the base address of all primary
  2731. * planes to the the last fb to make sure we're
  2732. * showing the correct fb after a reset.
  2733. *
  2734. * FIXME: Atomic will make this obsolete since we won't schedule
  2735. * CS-based flips (which might get lost in gpu resets) any more.
  2736. */
  2737. intel_update_primary_planes(dev_priv->dev);
  2738. return;
  2739. }
  2740. /*
  2741. * The display has been reset as well,
  2742. * so need a full re-initialization.
  2743. */
  2744. intel_runtime_pm_disable_interrupts(dev_priv);
  2745. intel_runtime_pm_enable_interrupts(dev_priv);
  2746. intel_modeset_init_hw(dev_priv->dev);
  2747. spin_lock_irq(&dev_priv->irq_lock);
  2748. if (dev_priv->display.hpd_irq_setup)
  2749. dev_priv->display.hpd_irq_setup(dev_priv);
  2750. spin_unlock_irq(&dev_priv->irq_lock);
  2751. intel_display_resume(dev_priv->dev);
  2752. intel_hpd_init(dev_priv);
  2753. drm_modeset_unlock_all(dev_priv->dev);
  2754. }
  2755. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2756. {
  2757. struct drm_device *dev = crtc->dev;
  2758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2759. unsigned reset_counter;
  2760. bool pending;
  2761. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2762. if (intel_crtc->reset_counter != reset_counter)
  2763. return false;
  2764. spin_lock_irq(&dev->event_lock);
  2765. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2766. spin_unlock_irq(&dev->event_lock);
  2767. return pending;
  2768. }
  2769. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2770. struct intel_crtc_state *old_crtc_state)
  2771. {
  2772. struct drm_device *dev = crtc->base.dev;
  2773. struct drm_i915_private *dev_priv = dev->dev_private;
  2774. struct intel_crtc_state *pipe_config =
  2775. to_intel_crtc_state(crtc->base.state);
  2776. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2777. crtc->base.mode = crtc->base.state->mode;
  2778. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2779. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2780. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2781. /*
  2782. * Update pipe size and adjust fitter if needed: the reason for this is
  2783. * that in compute_mode_changes we check the native mode (not the pfit
  2784. * mode) to see if we can flip rather than do a full mode set. In the
  2785. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2786. * pfit state, we'll end up with a big fb scanned out into the wrong
  2787. * sized surface.
  2788. */
  2789. I915_WRITE(PIPESRC(crtc->pipe),
  2790. ((pipe_config->pipe_src_w - 1) << 16) |
  2791. (pipe_config->pipe_src_h - 1));
  2792. /* on skylake this is done by detaching scalers */
  2793. if (INTEL_INFO(dev)->gen >= 9) {
  2794. skl_detach_scalers(crtc);
  2795. if (pipe_config->pch_pfit.enabled)
  2796. skylake_pfit_enable(crtc);
  2797. } else if (HAS_PCH_SPLIT(dev)) {
  2798. if (pipe_config->pch_pfit.enabled)
  2799. ironlake_pfit_enable(crtc);
  2800. else if (old_crtc_state->pch_pfit.enabled)
  2801. ironlake_pfit_disable(crtc, true);
  2802. }
  2803. }
  2804. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2805. {
  2806. struct drm_device *dev = crtc->dev;
  2807. struct drm_i915_private *dev_priv = dev->dev_private;
  2808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2809. int pipe = intel_crtc->pipe;
  2810. i915_reg_t reg;
  2811. u32 temp;
  2812. /* enable normal train */
  2813. reg = FDI_TX_CTL(pipe);
  2814. temp = I915_READ(reg);
  2815. if (IS_IVYBRIDGE(dev)) {
  2816. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2817. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2818. } else {
  2819. temp &= ~FDI_LINK_TRAIN_NONE;
  2820. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2821. }
  2822. I915_WRITE(reg, temp);
  2823. reg = FDI_RX_CTL(pipe);
  2824. temp = I915_READ(reg);
  2825. if (HAS_PCH_CPT(dev)) {
  2826. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2827. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2828. } else {
  2829. temp &= ~FDI_LINK_TRAIN_NONE;
  2830. temp |= FDI_LINK_TRAIN_NONE;
  2831. }
  2832. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2833. /* wait one idle pattern time */
  2834. POSTING_READ(reg);
  2835. udelay(1000);
  2836. /* IVB wants error correction enabled */
  2837. if (IS_IVYBRIDGE(dev))
  2838. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2839. FDI_FE_ERRC_ENABLE);
  2840. }
  2841. /* The FDI link training functions for ILK/Ibexpeak. */
  2842. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2843. {
  2844. struct drm_device *dev = crtc->dev;
  2845. struct drm_i915_private *dev_priv = dev->dev_private;
  2846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2847. int pipe = intel_crtc->pipe;
  2848. i915_reg_t reg;
  2849. u32 temp, tries;
  2850. /* FDI needs bits from pipe first */
  2851. assert_pipe_enabled(dev_priv, pipe);
  2852. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2853. for train result */
  2854. reg = FDI_RX_IMR(pipe);
  2855. temp = I915_READ(reg);
  2856. temp &= ~FDI_RX_SYMBOL_LOCK;
  2857. temp &= ~FDI_RX_BIT_LOCK;
  2858. I915_WRITE(reg, temp);
  2859. I915_READ(reg);
  2860. udelay(150);
  2861. /* enable CPU FDI TX and PCH FDI RX */
  2862. reg = FDI_TX_CTL(pipe);
  2863. temp = I915_READ(reg);
  2864. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2865. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2866. temp &= ~FDI_LINK_TRAIN_NONE;
  2867. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2868. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2869. reg = FDI_RX_CTL(pipe);
  2870. temp = I915_READ(reg);
  2871. temp &= ~FDI_LINK_TRAIN_NONE;
  2872. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2873. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2874. POSTING_READ(reg);
  2875. udelay(150);
  2876. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2877. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2878. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2879. FDI_RX_PHASE_SYNC_POINTER_EN);
  2880. reg = FDI_RX_IIR(pipe);
  2881. for (tries = 0; tries < 5; tries++) {
  2882. temp = I915_READ(reg);
  2883. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2884. if ((temp & FDI_RX_BIT_LOCK)) {
  2885. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2886. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2887. break;
  2888. }
  2889. }
  2890. if (tries == 5)
  2891. DRM_ERROR("FDI train 1 fail!\n");
  2892. /* Train 2 */
  2893. reg = FDI_TX_CTL(pipe);
  2894. temp = I915_READ(reg);
  2895. temp &= ~FDI_LINK_TRAIN_NONE;
  2896. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2897. I915_WRITE(reg, temp);
  2898. reg = FDI_RX_CTL(pipe);
  2899. temp = I915_READ(reg);
  2900. temp &= ~FDI_LINK_TRAIN_NONE;
  2901. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2902. I915_WRITE(reg, temp);
  2903. POSTING_READ(reg);
  2904. udelay(150);
  2905. reg = FDI_RX_IIR(pipe);
  2906. for (tries = 0; tries < 5; tries++) {
  2907. temp = I915_READ(reg);
  2908. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2909. if (temp & FDI_RX_SYMBOL_LOCK) {
  2910. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2911. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2912. break;
  2913. }
  2914. }
  2915. if (tries == 5)
  2916. DRM_ERROR("FDI train 2 fail!\n");
  2917. DRM_DEBUG_KMS("FDI train done\n");
  2918. }
  2919. static const int snb_b_fdi_train_param[] = {
  2920. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2921. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2922. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2923. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2924. };
  2925. /* The FDI link training functions for SNB/Cougarpoint. */
  2926. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2927. {
  2928. struct drm_device *dev = crtc->dev;
  2929. struct drm_i915_private *dev_priv = dev->dev_private;
  2930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2931. int pipe = intel_crtc->pipe;
  2932. i915_reg_t reg;
  2933. u32 temp, i, retry;
  2934. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2935. for train result */
  2936. reg = FDI_RX_IMR(pipe);
  2937. temp = I915_READ(reg);
  2938. temp &= ~FDI_RX_SYMBOL_LOCK;
  2939. temp &= ~FDI_RX_BIT_LOCK;
  2940. I915_WRITE(reg, temp);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. /* enable CPU FDI TX and PCH FDI RX */
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2947. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2948. temp &= ~FDI_LINK_TRAIN_NONE;
  2949. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2950. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2951. /* SNB-B */
  2952. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2953. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2954. I915_WRITE(FDI_RX_MISC(pipe),
  2955. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2956. reg = FDI_RX_CTL(pipe);
  2957. temp = I915_READ(reg);
  2958. if (HAS_PCH_CPT(dev)) {
  2959. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2960. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2961. } else {
  2962. temp &= ~FDI_LINK_TRAIN_NONE;
  2963. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2964. }
  2965. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2966. POSTING_READ(reg);
  2967. udelay(150);
  2968. for (i = 0; i < 4; i++) {
  2969. reg = FDI_TX_CTL(pipe);
  2970. temp = I915_READ(reg);
  2971. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2972. temp |= snb_b_fdi_train_param[i];
  2973. I915_WRITE(reg, temp);
  2974. POSTING_READ(reg);
  2975. udelay(500);
  2976. for (retry = 0; retry < 5; retry++) {
  2977. reg = FDI_RX_IIR(pipe);
  2978. temp = I915_READ(reg);
  2979. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2980. if (temp & FDI_RX_BIT_LOCK) {
  2981. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2982. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2983. break;
  2984. }
  2985. udelay(50);
  2986. }
  2987. if (retry < 5)
  2988. break;
  2989. }
  2990. if (i == 4)
  2991. DRM_ERROR("FDI train 1 fail!\n");
  2992. /* Train 2 */
  2993. reg = FDI_TX_CTL(pipe);
  2994. temp = I915_READ(reg);
  2995. temp &= ~FDI_LINK_TRAIN_NONE;
  2996. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2997. if (IS_GEN6(dev)) {
  2998. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2999. /* SNB-B */
  3000. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3001. }
  3002. I915_WRITE(reg, temp);
  3003. reg = FDI_RX_CTL(pipe);
  3004. temp = I915_READ(reg);
  3005. if (HAS_PCH_CPT(dev)) {
  3006. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3007. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3008. } else {
  3009. temp &= ~FDI_LINK_TRAIN_NONE;
  3010. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3011. }
  3012. I915_WRITE(reg, temp);
  3013. POSTING_READ(reg);
  3014. udelay(150);
  3015. for (i = 0; i < 4; i++) {
  3016. reg = FDI_TX_CTL(pipe);
  3017. temp = I915_READ(reg);
  3018. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3019. temp |= snb_b_fdi_train_param[i];
  3020. I915_WRITE(reg, temp);
  3021. POSTING_READ(reg);
  3022. udelay(500);
  3023. for (retry = 0; retry < 5; retry++) {
  3024. reg = FDI_RX_IIR(pipe);
  3025. temp = I915_READ(reg);
  3026. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3027. if (temp & FDI_RX_SYMBOL_LOCK) {
  3028. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3029. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3030. break;
  3031. }
  3032. udelay(50);
  3033. }
  3034. if (retry < 5)
  3035. break;
  3036. }
  3037. if (i == 4)
  3038. DRM_ERROR("FDI train 2 fail!\n");
  3039. DRM_DEBUG_KMS("FDI train done.\n");
  3040. }
  3041. /* Manual link training for Ivy Bridge A0 parts */
  3042. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3043. {
  3044. struct drm_device *dev = crtc->dev;
  3045. struct drm_i915_private *dev_priv = dev->dev_private;
  3046. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3047. int pipe = intel_crtc->pipe;
  3048. i915_reg_t reg;
  3049. u32 temp, i, j;
  3050. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3051. for train result */
  3052. reg = FDI_RX_IMR(pipe);
  3053. temp = I915_READ(reg);
  3054. temp &= ~FDI_RX_SYMBOL_LOCK;
  3055. temp &= ~FDI_RX_BIT_LOCK;
  3056. I915_WRITE(reg, temp);
  3057. POSTING_READ(reg);
  3058. udelay(150);
  3059. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3060. I915_READ(FDI_RX_IIR(pipe)));
  3061. /* Try each vswing and preemphasis setting twice before moving on */
  3062. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3063. /* disable first in case we need to retry */
  3064. reg = FDI_TX_CTL(pipe);
  3065. temp = I915_READ(reg);
  3066. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3067. temp &= ~FDI_TX_ENABLE;
  3068. I915_WRITE(reg, temp);
  3069. reg = FDI_RX_CTL(pipe);
  3070. temp = I915_READ(reg);
  3071. temp &= ~FDI_LINK_TRAIN_AUTO;
  3072. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3073. temp &= ~FDI_RX_ENABLE;
  3074. I915_WRITE(reg, temp);
  3075. /* enable CPU FDI TX and PCH FDI RX */
  3076. reg = FDI_TX_CTL(pipe);
  3077. temp = I915_READ(reg);
  3078. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3079. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3080. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3081. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3082. temp |= snb_b_fdi_train_param[j/2];
  3083. temp |= FDI_COMPOSITE_SYNC;
  3084. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3085. I915_WRITE(FDI_RX_MISC(pipe),
  3086. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3087. reg = FDI_RX_CTL(pipe);
  3088. temp = I915_READ(reg);
  3089. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3090. temp |= FDI_COMPOSITE_SYNC;
  3091. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3092. POSTING_READ(reg);
  3093. udelay(1); /* should be 0.5us */
  3094. for (i = 0; i < 4; i++) {
  3095. reg = FDI_RX_IIR(pipe);
  3096. temp = I915_READ(reg);
  3097. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3098. if (temp & FDI_RX_BIT_LOCK ||
  3099. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3100. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3101. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3102. i);
  3103. break;
  3104. }
  3105. udelay(1); /* should be 0.5us */
  3106. }
  3107. if (i == 4) {
  3108. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3109. continue;
  3110. }
  3111. /* Train 2 */
  3112. reg = FDI_TX_CTL(pipe);
  3113. temp = I915_READ(reg);
  3114. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3115. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3116. I915_WRITE(reg, temp);
  3117. reg = FDI_RX_CTL(pipe);
  3118. temp = I915_READ(reg);
  3119. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3120. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3121. I915_WRITE(reg, temp);
  3122. POSTING_READ(reg);
  3123. udelay(2); /* should be 1.5us */
  3124. for (i = 0; i < 4; i++) {
  3125. reg = FDI_RX_IIR(pipe);
  3126. temp = I915_READ(reg);
  3127. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3128. if (temp & FDI_RX_SYMBOL_LOCK ||
  3129. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3130. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3131. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3132. i);
  3133. goto train_done;
  3134. }
  3135. udelay(2); /* should be 1.5us */
  3136. }
  3137. if (i == 4)
  3138. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3139. }
  3140. train_done:
  3141. DRM_DEBUG_KMS("FDI train done.\n");
  3142. }
  3143. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3144. {
  3145. struct drm_device *dev = intel_crtc->base.dev;
  3146. struct drm_i915_private *dev_priv = dev->dev_private;
  3147. int pipe = intel_crtc->pipe;
  3148. i915_reg_t reg;
  3149. u32 temp;
  3150. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3151. reg = FDI_RX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3154. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3155. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3156. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3157. POSTING_READ(reg);
  3158. udelay(200);
  3159. /* Switch from Rawclk to PCDclk */
  3160. temp = I915_READ(reg);
  3161. I915_WRITE(reg, temp | FDI_PCDCLK);
  3162. POSTING_READ(reg);
  3163. udelay(200);
  3164. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3165. reg = FDI_TX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3168. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3169. POSTING_READ(reg);
  3170. udelay(100);
  3171. }
  3172. }
  3173. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3174. {
  3175. struct drm_device *dev = intel_crtc->base.dev;
  3176. struct drm_i915_private *dev_priv = dev->dev_private;
  3177. int pipe = intel_crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp;
  3180. /* Switch from PCDclk to Rawclk */
  3181. reg = FDI_RX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3184. /* Disable CPU FDI TX PLL */
  3185. reg = FDI_TX_CTL(pipe);
  3186. temp = I915_READ(reg);
  3187. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3188. POSTING_READ(reg);
  3189. udelay(100);
  3190. reg = FDI_RX_CTL(pipe);
  3191. temp = I915_READ(reg);
  3192. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3193. /* Wait for the clocks to turn off. */
  3194. POSTING_READ(reg);
  3195. udelay(100);
  3196. }
  3197. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3198. {
  3199. struct drm_device *dev = crtc->dev;
  3200. struct drm_i915_private *dev_priv = dev->dev_private;
  3201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3202. int pipe = intel_crtc->pipe;
  3203. i915_reg_t reg;
  3204. u32 temp;
  3205. /* disable CPU FDI tx and PCH FDI rx */
  3206. reg = FDI_TX_CTL(pipe);
  3207. temp = I915_READ(reg);
  3208. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3209. POSTING_READ(reg);
  3210. reg = FDI_RX_CTL(pipe);
  3211. temp = I915_READ(reg);
  3212. temp &= ~(0x7 << 16);
  3213. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3214. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3215. POSTING_READ(reg);
  3216. udelay(100);
  3217. /* Ironlake workaround, disable clock pointer after downing FDI */
  3218. if (HAS_PCH_IBX(dev))
  3219. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3220. /* still set train pattern 1 */
  3221. reg = FDI_TX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. temp &= ~FDI_LINK_TRAIN_NONE;
  3224. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3225. I915_WRITE(reg, temp);
  3226. reg = FDI_RX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. if (HAS_PCH_CPT(dev)) {
  3229. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3230. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3231. } else {
  3232. temp &= ~FDI_LINK_TRAIN_NONE;
  3233. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3234. }
  3235. /* BPC in FDI rx is consistent with that in PIPECONF */
  3236. temp &= ~(0x07 << 16);
  3237. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3238. I915_WRITE(reg, temp);
  3239. POSTING_READ(reg);
  3240. udelay(100);
  3241. }
  3242. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3243. {
  3244. struct intel_crtc *crtc;
  3245. /* Note that we don't need to be called with mode_config.lock here
  3246. * as our list of CRTC objects is static for the lifetime of the
  3247. * device and so cannot disappear as we iterate. Similarly, we can
  3248. * happily treat the predicates as racy, atomic checks as userspace
  3249. * cannot claim and pin a new fb without at least acquring the
  3250. * struct_mutex and so serialising with us.
  3251. */
  3252. for_each_intel_crtc(dev, crtc) {
  3253. if (atomic_read(&crtc->unpin_work_count) == 0)
  3254. continue;
  3255. if (crtc->flip_work)
  3256. intel_wait_for_vblank(dev, crtc->pipe);
  3257. return true;
  3258. }
  3259. return false;
  3260. }
  3261. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3262. {
  3263. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3264. struct intel_flip_work *work = intel_crtc->flip_work;
  3265. intel_crtc->flip_work = NULL;
  3266. if (work->event)
  3267. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3268. drm_crtc_vblank_put(&intel_crtc->base);
  3269. wake_up_all(&dev_priv->pending_flip_queue);
  3270. queue_work(dev_priv->wq, &work->unpin_work);
  3271. trace_i915_flip_complete(intel_crtc->plane,
  3272. work->pending_flip_obj);
  3273. }
  3274. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3275. {
  3276. struct drm_device *dev = crtc->dev;
  3277. struct drm_i915_private *dev_priv = dev->dev_private;
  3278. long ret;
  3279. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3280. ret = wait_event_interruptible_timeout(
  3281. dev_priv->pending_flip_queue,
  3282. !intel_crtc_has_pending_flip(crtc),
  3283. 60*HZ);
  3284. if (ret < 0)
  3285. return ret;
  3286. if (ret == 0) {
  3287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3288. struct intel_flip_work *work;
  3289. spin_lock_irq(&dev->event_lock);
  3290. work = intel_crtc->flip_work;
  3291. if (work && !is_mmio_work(work)) {
  3292. WARN_ONCE(1, "Removing stuck page flip\n");
  3293. page_flip_completed(intel_crtc);
  3294. }
  3295. spin_unlock_irq(&dev->event_lock);
  3296. }
  3297. return 0;
  3298. }
  3299. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3300. {
  3301. u32 temp;
  3302. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3303. mutex_lock(&dev_priv->sb_lock);
  3304. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3305. temp |= SBI_SSCCTL_DISABLE;
  3306. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3307. mutex_unlock(&dev_priv->sb_lock);
  3308. }
  3309. /* Program iCLKIP clock to the desired frequency */
  3310. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3311. {
  3312. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3313. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3314. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3315. u32 temp;
  3316. lpt_disable_iclkip(dev_priv);
  3317. /* The iCLK virtual clock root frequency is in MHz,
  3318. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3319. * divisors, it is necessary to divide one by another, so we
  3320. * convert the virtual clock precision to KHz here for higher
  3321. * precision.
  3322. */
  3323. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3324. u32 iclk_virtual_root_freq = 172800 * 1000;
  3325. u32 iclk_pi_range = 64;
  3326. u32 desired_divisor;
  3327. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3328. clock << auxdiv);
  3329. divsel = (desired_divisor / iclk_pi_range) - 2;
  3330. phaseinc = desired_divisor % iclk_pi_range;
  3331. /*
  3332. * Near 20MHz is a corner case which is
  3333. * out of range for the 7-bit divisor
  3334. */
  3335. if (divsel <= 0x7f)
  3336. break;
  3337. }
  3338. /* This should not happen with any sane values */
  3339. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3340. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3341. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3342. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3343. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3344. clock,
  3345. auxdiv,
  3346. divsel,
  3347. phasedir,
  3348. phaseinc);
  3349. mutex_lock(&dev_priv->sb_lock);
  3350. /* Program SSCDIVINTPHASE6 */
  3351. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3352. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3353. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3354. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3355. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3356. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3357. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3358. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3359. /* Program SSCAUXDIV */
  3360. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3361. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3362. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3363. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3364. /* Enable modulator and associated divider */
  3365. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3366. temp &= ~SBI_SSCCTL_DISABLE;
  3367. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3368. mutex_unlock(&dev_priv->sb_lock);
  3369. /* Wait for initialization time */
  3370. udelay(24);
  3371. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3372. }
  3373. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3374. {
  3375. u32 divsel, phaseinc, auxdiv;
  3376. u32 iclk_virtual_root_freq = 172800 * 1000;
  3377. u32 iclk_pi_range = 64;
  3378. u32 desired_divisor;
  3379. u32 temp;
  3380. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3381. return 0;
  3382. mutex_lock(&dev_priv->sb_lock);
  3383. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3384. if (temp & SBI_SSCCTL_DISABLE) {
  3385. mutex_unlock(&dev_priv->sb_lock);
  3386. return 0;
  3387. }
  3388. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3389. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3390. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3391. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3392. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3393. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3394. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3395. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3396. mutex_unlock(&dev_priv->sb_lock);
  3397. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3398. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3399. desired_divisor << auxdiv);
  3400. }
  3401. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3402. enum pipe pch_transcoder)
  3403. {
  3404. struct drm_device *dev = crtc->base.dev;
  3405. struct drm_i915_private *dev_priv = dev->dev_private;
  3406. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3407. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3408. I915_READ(HTOTAL(cpu_transcoder)));
  3409. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3410. I915_READ(HBLANK(cpu_transcoder)));
  3411. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3412. I915_READ(HSYNC(cpu_transcoder)));
  3413. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3414. I915_READ(VTOTAL(cpu_transcoder)));
  3415. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3416. I915_READ(VBLANK(cpu_transcoder)));
  3417. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3418. I915_READ(VSYNC(cpu_transcoder)));
  3419. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3420. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3421. }
  3422. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3423. {
  3424. struct drm_i915_private *dev_priv = dev->dev_private;
  3425. uint32_t temp;
  3426. temp = I915_READ(SOUTH_CHICKEN1);
  3427. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3428. return;
  3429. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3430. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3431. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3432. if (enable)
  3433. temp |= FDI_BC_BIFURCATION_SELECT;
  3434. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3435. I915_WRITE(SOUTH_CHICKEN1, temp);
  3436. POSTING_READ(SOUTH_CHICKEN1);
  3437. }
  3438. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3439. {
  3440. struct drm_device *dev = intel_crtc->base.dev;
  3441. switch (intel_crtc->pipe) {
  3442. case PIPE_A:
  3443. break;
  3444. case PIPE_B:
  3445. if (intel_crtc->config->fdi_lanes > 2)
  3446. cpt_set_fdi_bc_bifurcation(dev, false);
  3447. else
  3448. cpt_set_fdi_bc_bifurcation(dev, true);
  3449. break;
  3450. case PIPE_C:
  3451. cpt_set_fdi_bc_bifurcation(dev, true);
  3452. break;
  3453. default:
  3454. BUG();
  3455. }
  3456. }
  3457. /* Return which DP Port should be selected for Transcoder DP control */
  3458. static enum port
  3459. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3460. {
  3461. struct drm_device *dev = crtc->dev;
  3462. struct intel_encoder *encoder;
  3463. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3464. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3465. encoder->type == INTEL_OUTPUT_EDP)
  3466. return enc_to_dig_port(&encoder->base)->port;
  3467. }
  3468. return -1;
  3469. }
  3470. /*
  3471. * Enable PCH resources required for PCH ports:
  3472. * - PCH PLLs
  3473. * - FDI training & RX/TX
  3474. * - update transcoder timings
  3475. * - DP transcoding bits
  3476. * - transcoder
  3477. */
  3478. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3479. {
  3480. struct drm_device *dev = crtc->dev;
  3481. struct drm_i915_private *dev_priv = dev->dev_private;
  3482. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3483. int pipe = intel_crtc->pipe;
  3484. u32 temp;
  3485. assert_pch_transcoder_disabled(dev_priv, pipe);
  3486. if (IS_IVYBRIDGE(dev))
  3487. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3488. /* Write the TU size bits before fdi link training, so that error
  3489. * detection works. */
  3490. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3491. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3492. /* For PCH output, training FDI link */
  3493. dev_priv->display.fdi_link_train(crtc);
  3494. /* We need to program the right clock selection before writing the pixel
  3495. * mutliplier into the DPLL. */
  3496. if (HAS_PCH_CPT(dev)) {
  3497. u32 sel;
  3498. temp = I915_READ(PCH_DPLL_SEL);
  3499. temp |= TRANS_DPLL_ENABLE(pipe);
  3500. sel = TRANS_DPLLB_SEL(pipe);
  3501. if (intel_crtc->config->shared_dpll ==
  3502. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3503. temp |= sel;
  3504. else
  3505. temp &= ~sel;
  3506. I915_WRITE(PCH_DPLL_SEL, temp);
  3507. }
  3508. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3509. * transcoder, and we actually should do this to not upset any PCH
  3510. * transcoder that already use the clock when we share it.
  3511. *
  3512. * Note that enable_shared_dpll tries to do the right thing, but
  3513. * get_shared_dpll unconditionally resets the pll - we need that to have
  3514. * the right LVDS enable sequence. */
  3515. intel_enable_shared_dpll(intel_crtc);
  3516. /* set transcoder timing, panel must allow it */
  3517. assert_panel_unlocked(dev_priv, pipe);
  3518. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3519. intel_fdi_normal_train(crtc);
  3520. /* For PCH DP, enable TRANS_DP_CTL */
  3521. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3522. const struct drm_display_mode *adjusted_mode =
  3523. &intel_crtc->config->base.adjusted_mode;
  3524. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3525. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3526. temp = I915_READ(reg);
  3527. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3528. TRANS_DP_SYNC_MASK |
  3529. TRANS_DP_BPC_MASK);
  3530. temp |= TRANS_DP_OUTPUT_ENABLE;
  3531. temp |= bpc << 9; /* same format but at 11:9 */
  3532. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3533. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3534. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3535. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3536. switch (intel_trans_dp_port_sel(crtc)) {
  3537. case PORT_B:
  3538. temp |= TRANS_DP_PORT_SEL_B;
  3539. break;
  3540. case PORT_C:
  3541. temp |= TRANS_DP_PORT_SEL_C;
  3542. break;
  3543. case PORT_D:
  3544. temp |= TRANS_DP_PORT_SEL_D;
  3545. break;
  3546. default:
  3547. BUG();
  3548. }
  3549. I915_WRITE(reg, temp);
  3550. }
  3551. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3552. }
  3553. static void lpt_pch_enable(struct drm_crtc *crtc)
  3554. {
  3555. struct drm_device *dev = crtc->dev;
  3556. struct drm_i915_private *dev_priv = dev->dev_private;
  3557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3558. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3559. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3560. lpt_program_iclkip(crtc);
  3561. /* Set transcoder timing. */
  3562. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3563. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3564. }
  3565. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3566. {
  3567. struct drm_i915_private *dev_priv = dev->dev_private;
  3568. i915_reg_t dslreg = PIPEDSL(pipe);
  3569. u32 temp;
  3570. temp = I915_READ(dslreg);
  3571. udelay(500);
  3572. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3573. if (wait_for(I915_READ(dslreg) != temp, 5))
  3574. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3575. }
  3576. }
  3577. static int
  3578. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3579. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3580. int src_w, int src_h, int dst_w, int dst_h)
  3581. {
  3582. struct intel_crtc_scaler_state *scaler_state =
  3583. &crtc_state->scaler_state;
  3584. struct intel_crtc *intel_crtc =
  3585. to_intel_crtc(crtc_state->base.crtc);
  3586. int need_scaling;
  3587. need_scaling = intel_rotation_90_or_270(rotation) ?
  3588. (src_h != dst_w || src_w != dst_h):
  3589. (src_w != dst_w || src_h != dst_h);
  3590. /*
  3591. * if plane is being disabled or scaler is no more required or force detach
  3592. * - free scaler binded to this plane/crtc
  3593. * - in order to do this, update crtc->scaler_usage
  3594. *
  3595. * Here scaler state in crtc_state is set free so that
  3596. * scaler can be assigned to other user. Actual register
  3597. * update to free the scaler is done in plane/panel-fit programming.
  3598. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3599. */
  3600. if (force_detach || !need_scaling) {
  3601. if (*scaler_id >= 0) {
  3602. scaler_state->scaler_users &= ~(1 << scaler_user);
  3603. scaler_state->scalers[*scaler_id].in_use = 0;
  3604. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3605. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3606. intel_crtc->pipe, scaler_user, *scaler_id,
  3607. scaler_state->scaler_users);
  3608. *scaler_id = -1;
  3609. }
  3610. return 0;
  3611. }
  3612. /* range checks */
  3613. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3614. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3615. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3616. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3617. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3618. "size is out of scaler range\n",
  3619. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3620. return -EINVAL;
  3621. }
  3622. /* mark this plane as a scaler user in crtc_state */
  3623. scaler_state->scaler_users |= (1 << scaler_user);
  3624. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3625. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3626. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3627. scaler_state->scaler_users);
  3628. return 0;
  3629. }
  3630. /**
  3631. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3632. *
  3633. * @state: crtc's scaler state
  3634. *
  3635. * Return
  3636. * 0 - scaler_usage updated successfully
  3637. * error - requested scaling cannot be supported or other error condition
  3638. */
  3639. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3640. {
  3641. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3642. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3643. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3644. intel_crtc->base.base.id, intel_crtc->base.name,
  3645. intel_crtc->pipe, SKL_CRTC_INDEX);
  3646. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3647. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3648. state->pipe_src_w, state->pipe_src_h,
  3649. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3650. }
  3651. /**
  3652. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3653. *
  3654. * @state: crtc's scaler state
  3655. * @plane_state: atomic plane state to update
  3656. *
  3657. * Return
  3658. * 0 - scaler_usage updated successfully
  3659. * error - requested scaling cannot be supported or other error condition
  3660. */
  3661. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3662. struct intel_plane_state *plane_state)
  3663. {
  3664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3665. struct intel_plane *intel_plane =
  3666. to_intel_plane(plane_state->base.plane);
  3667. struct drm_framebuffer *fb = plane_state->base.fb;
  3668. int ret;
  3669. bool force_detach = !fb || !plane_state->visible;
  3670. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3671. intel_plane->base.base.id, intel_plane->base.name,
  3672. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3673. ret = skl_update_scaler(crtc_state, force_detach,
  3674. drm_plane_index(&intel_plane->base),
  3675. &plane_state->scaler_id,
  3676. plane_state->base.rotation,
  3677. drm_rect_width(&plane_state->src) >> 16,
  3678. drm_rect_height(&plane_state->src) >> 16,
  3679. drm_rect_width(&plane_state->dst),
  3680. drm_rect_height(&plane_state->dst));
  3681. if (ret || plane_state->scaler_id < 0)
  3682. return ret;
  3683. /* check colorkey */
  3684. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3685. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3686. intel_plane->base.base.id,
  3687. intel_plane->base.name);
  3688. return -EINVAL;
  3689. }
  3690. /* Check src format */
  3691. switch (fb->pixel_format) {
  3692. case DRM_FORMAT_RGB565:
  3693. case DRM_FORMAT_XBGR8888:
  3694. case DRM_FORMAT_XRGB8888:
  3695. case DRM_FORMAT_ABGR8888:
  3696. case DRM_FORMAT_ARGB8888:
  3697. case DRM_FORMAT_XRGB2101010:
  3698. case DRM_FORMAT_XBGR2101010:
  3699. case DRM_FORMAT_YUYV:
  3700. case DRM_FORMAT_YVYU:
  3701. case DRM_FORMAT_UYVY:
  3702. case DRM_FORMAT_VYUY:
  3703. break;
  3704. default:
  3705. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3706. intel_plane->base.base.id, intel_plane->base.name,
  3707. fb->base.id, fb->pixel_format);
  3708. return -EINVAL;
  3709. }
  3710. return 0;
  3711. }
  3712. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3713. {
  3714. int i;
  3715. for (i = 0; i < crtc->num_scalers; i++)
  3716. skl_detach_scaler(crtc, i);
  3717. }
  3718. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3719. {
  3720. struct drm_device *dev = crtc->base.dev;
  3721. struct drm_i915_private *dev_priv = dev->dev_private;
  3722. int pipe = crtc->pipe;
  3723. struct intel_crtc_scaler_state *scaler_state =
  3724. &crtc->config->scaler_state;
  3725. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3726. if (crtc->config->pch_pfit.enabled) {
  3727. int id;
  3728. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3729. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3730. return;
  3731. }
  3732. id = scaler_state->scaler_id;
  3733. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3734. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3735. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3736. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3737. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3738. }
  3739. }
  3740. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3741. {
  3742. struct drm_device *dev = crtc->base.dev;
  3743. struct drm_i915_private *dev_priv = dev->dev_private;
  3744. int pipe = crtc->pipe;
  3745. if (crtc->config->pch_pfit.enabled) {
  3746. /* Force use of hard-coded filter coefficients
  3747. * as some pre-programmed values are broken,
  3748. * e.g. x201.
  3749. */
  3750. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3751. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3752. PF_PIPE_SEL_IVB(pipe));
  3753. else
  3754. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3755. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3756. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3757. }
  3758. }
  3759. void hsw_enable_ips(struct intel_crtc *crtc)
  3760. {
  3761. struct drm_device *dev = crtc->base.dev;
  3762. struct drm_i915_private *dev_priv = dev->dev_private;
  3763. if (!crtc->config->ips_enabled)
  3764. return;
  3765. /*
  3766. * We can only enable IPS after we enable a plane and wait for a vblank
  3767. * This function is called from post_plane_update, which is run after
  3768. * a vblank wait.
  3769. */
  3770. assert_plane_enabled(dev_priv, crtc->plane);
  3771. if (IS_BROADWELL(dev)) {
  3772. mutex_lock(&dev_priv->rps.hw_lock);
  3773. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3774. mutex_unlock(&dev_priv->rps.hw_lock);
  3775. /* Quoting Art Runyan: "its not safe to expect any particular
  3776. * value in IPS_CTL bit 31 after enabling IPS through the
  3777. * mailbox." Moreover, the mailbox may return a bogus state,
  3778. * so we need to just enable it and continue on.
  3779. */
  3780. } else {
  3781. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3782. /* The bit only becomes 1 in the next vblank, so this wait here
  3783. * is essentially intel_wait_for_vblank. If we don't have this
  3784. * and don't wait for vblanks until the end of crtc_enable, then
  3785. * the HW state readout code will complain that the expected
  3786. * IPS_CTL value is not the one we read. */
  3787. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3788. DRM_ERROR("Timed out waiting for IPS enable\n");
  3789. }
  3790. }
  3791. void hsw_disable_ips(struct intel_crtc *crtc)
  3792. {
  3793. struct drm_device *dev = crtc->base.dev;
  3794. struct drm_i915_private *dev_priv = dev->dev_private;
  3795. if (!crtc->config->ips_enabled)
  3796. return;
  3797. assert_plane_enabled(dev_priv, crtc->plane);
  3798. if (IS_BROADWELL(dev)) {
  3799. mutex_lock(&dev_priv->rps.hw_lock);
  3800. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3801. mutex_unlock(&dev_priv->rps.hw_lock);
  3802. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3803. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3804. DRM_ERROR("Timed out waiting for IPS disable\n");
  3805. } else {
  3806. I915_WRITE(IPS_CTL, 0);
  3807. POSTING_READ(IPS_CTL);
  3808. }
  3809. /* We need to wait for a vblank before we can disable the plane. */
  3810. intel_wait_for_vblank(dev, crtc->pipe);
  3811. }
  3812. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3813. {
  3814. if (intel_crtc->overlay) {
  3815. struct drm_device *dev = intel_crtc->base.dev;
  3816. struct drm_i915_private *dev_priv = dev->dev_private;
  3817. mutex_lock(&dev->struct_mutex);
  3818. dev_priv->mm.interruptible = false;
  3819. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3820. dev_priv->mm.interruptible = true;
  3821. mutex_unlock(&dev->struct_mutex);
  3822. }
  3823. /* Let userspace switch the overlay on again. In most cases userspace
  3824. * has to recompute where to put it anyway.
  3825. */
  3826. }
  3827. /**
  3828. * intel_post_enable_primary - Perform operations after enabling primary plane
  3829. * @crtc: the CRTC whose primary plane was just enabled
  3830. *
  3831. * Performs potentially sleeping operations that must be done after the primary
  3832. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3833. * called due to an explicit primary plane update, or due to an implicit
  3834. * re-enable that is caused when a sprite plane is updated to no longer
  3835. * completely hide the primary plane.
  3836. */
  3837. static void
  3838. intel_post_enable_primary(struct drm_crtc *crtc)
  3839. {
  3840. struct drm_device *dev = crtc->dev;
  3841. struct drm_i915_private *dev_priv = dev->dev_private;
  3842. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3843. int pipe = intel_crtc->pipe;
  3844. /*
  3845. * FIXME IPS should be fine as long as one plane is
  3846. * enabled, but in practice it seems to have problems
  3847. * when going from primary only to sprite only and vice
  3848. * versa.
  3849. */
  3850. hsw_enable_ips(intel_crtc);
  3851. /*
  3852. * Gen2 reports pipe underruns whenever all planes are disabled.
  3853. * So don't enable underrun reporting before at least some planes
  3854. * are enabled.
  3855. * FIXME: Need to fix the logic to work when we turn off all planes
  3856. * but leave the pipe running.
  3857. */
  3858. if (IS_GEN2(dev))
  3859. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3860. /* Underruns don't always raise interrupts, so check manually. */
  3861. intel_check_cpu_fifo_underruns(dev_priv);
  3862. intel_check_pch_fifo_underruns(dev_priv);
  3863. }
  3864. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3865. static void
  3866. intel_pre_disable_primary(struct drm_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->dev;
  3869. struct drm_i915_private *dev_priv = dev->dev_private;
  3870. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3871. int pipe = intel_crtc->pipe;
  3872. /*
  3873. * Gen2 reports pipe underruns whenever all planes are disabled.
  3874. * So diasble underrun reporting before all the planes get disabled.
  3875. * FIXME: Need to fix the logic to work when we turn off all planes
  3876. * but leave the pipe running.
  3877. */
  3878. if (IS_GEN2(dev))
  3879. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3880. /*
  3881. * FIXME IPS should be fine as long as one plane is
  3882. * enabled, but in practice it seems to have problems
  3883. * when going from primary only to sprite only and vice
  3884. * versa.
  3885. */
  3886. hsw_disable_ips(intel_crtc);
  3887. }
  3888. /* FIXME get rid of this and use pre_plane_update */
  3889. static void
  3890. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3891. {
  3892. struct drm_device *dev = crtc->dev;
  3893. struct drm_i915_private *dev_priv = dev->dev_private;
  3894. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3895. int pipe = intel_crtc->pipe;
  3896. intel_pre_disable_primary(crtc);
  3897. /*
  3898. * Vblank time updates from the shadow to live plane control register
  3899. * are blocked if the memory self-refresh mode is active at that
  3900. * moment. So to make sure the plane gets truly disabled, disable
  3901. * first the self-refresh mode. The self-refresh enable bit in turn
  3902. * will be checked/applied by the HW only at the next frame start
  3903. * event which is after the vblank start event, so we need to have a
  3904. * wait-for-vblank between disabling the plane and the pipe.
  3905. */
  3906. if (HAS_GMCH_DISPLAY(dev)) {
  3907. intel_set_memory_cxsr(dev_priv, false);
  3908. dev_priv->wm.vlv.cxsr = false;
  3909. intel_wait_for_vblank(dev, pipe);
  3910. }
  3911. }
  3912. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3913. {
  3914. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3915. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3916. struct intel_crtc_state *pipe_config =
  3917. to_intel_crtc_state(crtc->base.state);
  3918. struct drm_device *dev = crtc->base.dev;
  3919. struct drm_plane *primary = crtc->base.primary;
  3920. struct drm_plane_state *old_pri_state =
  3921. drm_atomic_get_existing_plane_state(old_state, primary);
  3922. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3923. crtc->wm.cxsr_allowed = true;
  3924. if (pipe_config->update_wm_post && pipe_config->base.active)
  3925. intel_update_watermarks(&crtc->base);
  3926. if (old_pri_state) {
  3927. struct intel_plane_state *primary_state =
  3928. to_intel_plane_state(primary->state);
  3929. struct intel_plane_state *old_primary_state =
  3930. to_intel_plane_state(old_pri_state);
  3931. intel_fbc_post_update(crtc);
  3932. if (primary_state->visible &&
  3933. (needs_modeset(&pipe_config->base) ||
  3934. !old_primary_state->visible))
  3935. intel_post_enable_primary(&crtc->base);
  3936. }
  3937. }
  3938. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3939. {
  3940. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3941. struct drm_device *dev = crtc->base.dev;
  3942. struct drm_i915_private *dev_priv = dev->dev_private;
  3943. struct intel_crtc_state *pipe_config =
  3944. to_intel_crtc_state(crtc->base.state);
  3945. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3946. struct drm_plane *primary = crtc->base.primary;
  3947. struct drm_plane_state *old_pri_state =
  3948. drm_atomic_get_existing_plane_state(old_state, primary);
  3949. bool modeset = needs_modeset(&pipe_config->base);
  3950. if (old_pri_state) {
  3951. struct intel_plane_state *primary_state =
  3952. to_intel_plane_state(primary->state);
  3953. struct intel_plane_state *old_primary_state =
  3954. to_intel_plane_state(old_pri_state);
  3955. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3956. if (old_primary_state->visible &&
  3957. (modeset || !primary_state->visible))
  3958. intel_pre_disable_primary(&crtc->base);
  3959. }
  3960. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  3961. crtc->wm.cxsr_allowed = false;
  3962. /*
  3963. * Vblank time updates from the shadow to live plane control register
  3964. * are blocked if the memory self-refresh mode is active at that
  3965. * moment. So to make sure the plane gets truly disabled, disable
  3966. * first the self-refresh mode. The self-refresh enable bit in turn
  3967. * will be checked/applied by the HW only at the next frame start
  3968. * event which is after the vblank start event, so we need to have a
  3969. * wait-for-vblank between disabling the plane and the pipe.
  3970. */
  3971. if (old_crtc_state->base.active) {
  3972. intel_set_memory_cxsr(dev_priv, false);
  3973. dev_priv->wm.vlv.cxsr = false;
  3974. intel_wait_for_vblank(dev, crtc->pipe);
  3975. }
  3976. }
  3977. /*
  3978. * IVB workaround: must disable low power watermarks for at least
  3979. * one frame before enabling scaling. LP watermarks can be re-enabled
  3980. * when scaling is disabled.
  3981. *
  3982. * WaCxSRDisabledForSpriteScaling:ivb
  3983. */
  3984. if (pipe_config->disable_lp_wm) {
  3985. ilk_disable_lp_wm(dev);
  3986. intel_wait_for_vblank(dev, crtc->pipe);
  3987. }
  3988. /*
  3989. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3990. * watermark programming here.
  3991. */
  3992. if (needs_modeset(&pipe_config->base))
  3993. return;
  3994. /*
  3995. * For platforms that support atomic watermarks, program the
  3996. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3997. * will be the intermediate values that are safe for both pre- and
  3998. * post- vblank; when vblank happens, the 'active' values will be set
  3999. * to the final 'target' values and we'll do this again to get the
  4000. * optimal watermarks. For gen9+ platforms, the values we program here
  4001. * will be the final target values which will get automatically latched
  4002. * at vblank time; no further programming will be necessary.
  4003. *
  4004. * If a platform hasn't been transitioned to atomic watermarks yet,
  4005. * we'll continue to update watermarks the old way, if flags tell
  4006. * us to.
  4007. */
  4008. if (dev_priv->display.initial_watermarks != NULL)
  4009. dev_priv->display.initial_watermarks(pipe_config);
  4010. else if (pipe_config->update_wm_pre)
  4011. intel_update_watermarks(&crtc->base);
  4012. }
  4013. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4014. {
  4015. struct drm_device *dev = crtc->dev;
  4016. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4017. struct drm_plane *p;
  4018. int pipe = intel_crtc->pipe;
  4019. intel_crtc_dpms_overlay_disable(intel_crtc);
  4020. drm_for_each_plane_mask(p, dev, plane_mask)
  4021. to_intel_plane(p)->disable_plane(p, crtc);
  4022. /*
  4023. * FIXME: Once we grow proper nuclear flip support out of this we need
  4024. * to compute the mask of flip planes precisely. For the time being
  4025. * consider this a flip to a NULL plane.
  4026. */
  4027. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4028. }
  4029. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4030. {
  4031. struct drm_device *dev = crtc->dev;
  4032. struct drm_i915_private *dev_priv = dev->dev_private;
  4033. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4034. struct intel_encoder *encoder;
  4035. int pipe = intel_crtc->pipe;
  4036. struct intel_crtc_state *pipe_config =
  4037. to_intel_crtc_state(crtc->state);
  4038. if (WARN_ON(intel_crtc->active))
  4039. return;
  4040. /*
  4041. * Sometimes spurious CPU pipe underruns happen during FDI
  4042. * training, at least with VGA+HDMI cloning. Suppress them.
  4043. *
  4044. * On ILK we get an occasional spurious CPU pipe underruns
  4045. * between eDP port A enable and vdd enable. Also PCH port
  4046. * enable seems to result in the occasional CPU pipe underrun.
  4047. *
  4048. * Spurious PCH underruns also occur during PCH enabling.
  4049. */
  4050. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4051. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4052. if (intel_crtc->config->has_pch_encoder)
  4053. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4054. if (intel_crtc->config->has_pch_encoder)
  4055. intel_prepare_shared_dpll(intel_crtc);
  4056. if (intel_crtc->config->has_dp_encoder)
  4057. intel_dp_set_m_n(intel_crtc, M1_N1);
  4058. intel_set_pipe_timings(intel_crtc);
  4059. intel_set_pipe_src_size(intel_crtc);
  4060. if (intel_crtc->config->has_pch_encoder) {
  4061. intel_cpu_transcoder_set_m_n(intel_crtc,
  4062. &intel_crtc->config->fdi_m_n, NULL);
  4063. }
  4064. ironlake_set_pipeconf(crtc);
  4065. intel_crtc->active = true;
  4066. for_each_encoder_on_crtc(dev, crtc, encoder)
  4067. if (encoder->pre_enable)
  4068. encoder->pre_enable(encoder);
  4069. if (intel_crtc->config->has_pch_encoder) {
  4070. /* Note: FDI PLL enabling _must_ be done before we enable the
  4071. * cpu pipes, hence this is separate from all the other fdi/pch
  4072. * enabling. */
  4073. ironlake_fdi_pll_enable(intel_crtc);
  4074. } else {
  4075. assert_fdi_tx_disabled(dev_priv, pipe);
  4076. assert_fdi_rx_disabled(dev_priv, pipe);
  4077. }
  4078. ironlake_pfit_enable(intel_crtc);
  4079. /*
  4080. * On ILK+ LUT must be loaded before the pipe is running but with
  4081. * clocks enabled
  4082. */
  4083. intel_color_load_luts(&pipe_config->base);
  4084. if (dev_priv->display.initial_watermarks != NULL)
  4085. dev_priv->display.initial_watermarks(intel_crtc->config);
  4086. intel_enable_pipe(intel_crtc);
  4087. if (intel_crtc->config->has_pch_encoder)
  4088. ironlake_pch_enable(crtc);
  4089. assert_vblank_disabled(crtc);
  4090. drm_crtc_vblank_on(crtc);
  4091. for_each_encoder_on_crtc(dev, crtc, encoder)
  4092. encoder->enable(encoder);
  4093. if (HAS_PCH_CPT(dev))
  4094. cpt_verify_modeset(dev, intel_crtc->pipe);
  4095. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4096. if (intel_crtc->config->has_pch_encoder)
  4097. intel_wait_for_vblank(dev, pipe);
  4098. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4099. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4100. }
  4101. /* IPS only exists on ULT machines and is tied to pipe A. */
  4102. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4103. {
  4104. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4105. }
  4106. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4107. {
  4108. struct drm_device *dev = crtc->dev;
  4109. struct drm_i915_private *dev_priv = dev->dev_private;
  4110. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4111. struct intel_encoder *encoder;
  4112. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4113. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4114. struct intel_crtc_state *pipe_config =
  4115. to_intel_crtc_state(crtc->state);
  4116. if (WARN_ON(intel_crtc->active))
  4117. return;
  4118. if (intel_crtc->config->has_pch_encoder)
  4119. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4120. false);
  4121. for_each_encoder_on_crtc(dev, crtc, encoder)
  4122. if (encoder->pre_pll_enable)
  4123. encoder->pre_pll_enable(encoder);
  4124. if (intel_crtc->config->shared_dpll)
  4125. intel_enable_shared_dpll(intel_crtc);
  4126. if (intel_crtc->config->has_dp_encoder)
  4127. intel_dp_set_m_n(intel_crtc, M1_N1);
  4128. if (!intel_crtc->config->has_dsi_encoder)
  4129. intel_set_pipe_timings(intel_crtc);
  4130. intel_set_pipe_src_size(intel_crtc);
  4131. if (cpu_transcoder != TRANSCODER_EDP &&
  4132. !transcoder_is_dsi(cpu_transcoder)) {
  4133. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4134. intel_crtc->config->pixel_multiplier - 1);
  4135. }
  4136. if (intel_crtc->config->has_pch_encoder) {
  4137. intel_cpu_transcoder_set_m_n(intel_crtc,
  4138. &intel_crtc->config->fdi_m_n, NULL);
  4139. }
  4140. if (!intel_crtc->config->has_dsi_encoder)
  4141. haswell_set_pipeconf(crtc);
  4142. haswell_set_pipemisc(crtc);
  4143. intel_color_set_csc(&pipe_config->base);
  4144. intel_crtc->active = true;
  4145. if (intel_crtc->config->has_pch_encoder)
  4146. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4147. else
  4148. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4149. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4150. if (encoder->pre_enable)
  4151. encoder->pre_enable(encoder);
  4152. }
  4153. if (intel_crtc->config->has_pch_encoder)
  4154. dev_priv->display.fdi_link_train(crtc);
  4155. if (!intel_crtc->config->has_dsi_encoder)
  4156. intel_ddi_enable_pipe_clock(intel_crtc);
  4157. if (INTEL_INFO(dev)->gen >= 9)
  4158. skylake_pfit_enable(intel_crtc);
  4159. else
  4160. ironlake_pfit_enable(intel_crtc);
  4161. /*
  4162. * On ILK+ LUT must be loaded before the pipe is running but with
  4163. * clocks enabled
  4164. */
  4165. intel_color_load_luts(&pipe_config->base);
  4166. intel_ddi_set_pipe_settings(crtc);
  4167. if (!intel_crtc->config->has_dsi_encoder)
  4168. intel_ddi_enable_transcoder_func(crtc);
  4169. if (dev_priv->display.initial_watermarks != NULL)
  4170. dev_priv->display.initial_watermarks(pipe_config);
  4171. else
  4172. intel_update_watermarks(crtc);
  4173. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4174. if (!intel_crtc->config->has_dsi_encoder)
  4175. intel_enable_pipe(intel_crtc);
  4176. if (intel_crtc->config->has_pch_encoder)
  4177. lpt_pch_enable(crtc);
  4178. if (intel_crtc->config->dp_encoder_is_mst)
  4179. intel_ddi_set_vc_payload_alloc(crtc, true);
  4180. assert_vblank_disabled(crtc);
  4181. drm_crtc_vblank_on(crtc);
  4182. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4183. encoder->enable(encoder);
  4184. intel_opregion_notify_encoder(encoder, true);
  4185. }
  4186. if (intel_crtc->config->has_pch_encoder) {
  4187. intel_wait_for_vblank(dev, pipe);
  4188. intel_wait_for_vblank(dev, pipe);
  4189. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4190. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4191. true);
  4192. }
  4193. /* If we change the relative order between pipe/planes enabling, we need
  4194. * to change the workaround. */
  4195. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4196. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4197. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4198. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4199. }
  4200. }
  4201. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4202. {
  4203. struct drm_device *dev = crtc->base.dev;
  4204. struct drm_i915_private *dev_priv = dev->dev_private;
  4205. int pipe = crtc->pipe;
  4206. /* To avoid upsetting the power well on haswell only disable the pfit if
  4207. * it's in use. The hw state code will make sure we get this right. */
  4208. if (force || crtc->config->pch_pfit.enabled) {
  4209. I915_WRITE(PF_CTL(pipe), 0);
  4210. I915_WRITE(PF_WIN_POS(pipe), 0);
  4211. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4212. }
  4213. }
  4214. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4215. {
  4216. struct drm_device *dev = crtc->dev;
  4217. struct drm_i915_private *dev_priv = dev->dev_private;
  4218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4219. struct intel_encoder *encoder;
  4220. int pipe = intel_crtc->pipe;
  4221. /*
  4222. * Sometimes spurious CPU pipe underruns happen when the
  4223. * pipe is already disabled, but FDI RX/TX is still enabled.
  4224. * Happens at least with VGA+HDMI cloning. Suppress them.
  4225. */
  4226. if (intel_crtc->config->has_pch_encoder) {
  4227. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4228. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4229. }
  4230. for_each_encoder_on_crtc(dev, crtc, encoder)
  4231. encoder->disable(encoder);
  4232. drm_crtc_vblank_off(crtc);
  4233. assert_vblank_disabled(crtc);
  4234. intel_disable_pipe(intel_crtc);
  4235. ironlake_pfit_disable(intel_crtc, false);
  4236. if (intel_crtc->config->has_pch_encoder)
  4237. ironlake_fdi_disable(crtc);
  4238. for_each_encoder_on_crtc(dev, crtc, encoder)
  4239. if (encoder->post_disable)
  4240. encoder->post_disable(encoder);
  4241. if (intel_crtc->config->has_pch_encoder) {
  4242. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4243. if (HAS_PCH_CPT(dev)) {
  4244. i915_reg_t reg;
  4245. u32 temp;
  4246. /* disable TRANS_DP_CTL */
  4247. reg = TRANS_DP_CTL(pipe);
  4248. temp = I915_READ(reg);
  4249. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4250. TRANS_DP_PORT_SEL_MASK);
  4251. temp |= TRANS_DP_PORT_SEL_NONE;
  4252. I915_WRITE(reg, temp);
  4253. /* disable DPLL_SEL */
  4254. temp = I915_READ(PCH_DPLL_SEL);
  4255. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4256. I915_WRITE(PCH_DPLL_SEL, temp);
  4257. }
  4258. ironlake_fdi_pll_disable(intel_crtc);
  4259. }
  4260. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4261. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4262. }
  4263. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4264. {
  4265. struct drm_device *dev = crtc->dev;
  4266. struct drm_i915_private *dev_priv = dev->dev_private;
  4267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4268. struct intel_encoder *encoder;
  4269. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4270. if (intel_crtc->config->has_pch_encoder)
  4271. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4272. false);
  4273. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4274. intel_opregion_notify_encoder(encoder, false);
  4275. encoder->disable(encoder);
  4276. }
  4277. drm_crtc_vblank_off(crtc);
  4278. assert_vblank_disabled(crtc);
  4279. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4280. if (!intel_crtc->config->has_dsi_encoder)
  4281. intel_disable_pipe(intel_crtc);
  4282. if (intel_crtc->config->dp_encoder_is_mst)
  4283. intel_ddi_set_vc_payload_alloc(crtc, false);
  4284. if (!intel_crtc->config->has_dsi_encoder)
  4285. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4286. if (INTEL_INFO(dev)->gen >= 9)
  4287. skylake_scaler_disable(intel_crtc);
  4288. else
  4289. ironlake_pfit_disable(intel_crtc, false);
  4290. if (!intel_crtc->config->has_dsi_encoder)
  4291. intel_ddi_disable_pipe_clock(intel_crtc);
  4292. for_each_encoder_on_crtc(dev, crtc, encoder)
  4293. if (encoder->post_disable)
  4294. encoder->post_disable(encoder);
  4295. if (intel_crtc->config->has_pch_encoder) {
  4296. lpt_disable_pch_transcoder(dev_priv);
  4297. lpt_disable_iclkip(dev_priv);
  4298. intel_ddi_fdi_disable(crtc);
  4299. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4300. true);
  4301. }
  4302. }
  4303. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4304. {
  4305. struct drm_device *dev = crtc->base.dev;
  4306. struct drm_i915_private *dev_priv = dev->dev_private;
  4307. struct intel_crtc_state *pipe_config = crtc->config;
  4308. if (!pipe_config->gmch_pfit.control)
  4309. return;
  4310. /*
  4311. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4312. * according to register description and PRM.
  4313. */
  4314. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4315. assert_pipe_disabled(dev_priv, crtc->pipe);
  4316. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4317. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4318. /* Border color in case we don't scale up to the full screen. Black by
  4319. * default, change to something else for debugging. */
  4320. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4321. }
  4322. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4323. {
  4324. switch (port) {
  4325. case PORT_A:
  4326. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4327. case PORT_B:
  4328. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4329. case PORT_C:
  4330. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4331. case PORT_D:
  4332. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4333. case PORT_E:
  4334. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4335. default:
  4336. MISSING_CASE(port);
  4337. return POWER_DOMAIN_PORT_OTHER;
  4338. }
  4339. }
  4340. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4341. {
  4342. switch (port) {
  4343. case PORT_A:
  4344. return POWER_DOMAIN_AUX_A;
  4345. case PORT_B:
  4346. return POWER_DOMAIN_AUX_B;
  4347. case PORT_C:
  4348. return POWER_DOMAIN_AUX_C;
  4349. case PORT_D:
  4350. return POWER_DOMAIN_AUX_D;
  4351. case PORT_E:
  4352. /* FIXME: Check VBT for actual wiring of PORT E */
  4353. return POWER_DOMAIN_AUX_D;
  4354. default:
  4355. MISSING_CASE(port);
  4356. return POWER_DOMAIN_AUX_A;
  4357. }
  4358. }
  4359. enum intel_display_power_domain
  4360. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4361. {
  4362. struct drm_device *dev = intel_encoder->base.dev;
  4363. struct intel_digital_port *intel_dig_port;
  4364. switch (intel_encoder->type) {
  4365. case INTEL_OUTPUT_UNKNOWN:
  4366. /* Only DDI platforms should ever use this output type */
  4367. WARN_ON_ONCE(!HAS_DDI(dev));
  4368. case INTEL_OUTPUT_DISPLAYPORT:
  4369. case INTEL_OUTPUT_HDMI:
  4370. case INTEL_OUTPUT_EDP:
  4371. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4372. return port_to_power_domain(intel_dig_port->port);
  4373. case INTEL_OUTPUT_DP_MST:
  4374. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4375. return port_to_power_domain(intel_dig_port->port);
  4376. case INTEL_OUTPUT_ANALOG:
  4377. return POWER_DOMAIN_PORT_CRT;
  4378. case INTEL_OUTPUT_DSI:
  4379. return POWER_DOMAIN_PORT_DSI;
  4380. default:
  4381. return POWER_DOMAIN_PORT_OTHER;
  4382. }
  4383. }
  4384. enum intel_display_power_domain
  4385. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4386. {
  4387. struct drm_device *dev = intel_encoder->base.dev;
  4388. struct intel_digital_port *intel_dig_port;
  4389. switch (intel_encoder->type) {
  4390. case INTEL_OUTPUT_UNKNOWN:
  4391. case INTEL_OUTPUT_HDMI:
  4392. /*
  4393. * Only DDI platforms should ever use these output types.
  4394. * We can get here after the HDMI detect code has already set
  4395. * the type of the shared encoder. Since we can't be sure
  4396. * what's the status of the given connectors, play safe and
  4397. * run the DP detection too.
  4398. */
  4399. WARN_ON_ONCE(!HAS_DDI(dev));
  4400. case INTEL_OUTPUT_DISPLAYPORT:
  4401. case INTEL_OUTPUT_EDP:
  4402. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4403. return port_to_aux_power_domain(intel_dig_port->port);
  4404. case INTEL_OUTPUT_DP_MST:
  4405. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4406. return port_to_aux_power_domain(intel_dig_port->port);
  4407. default:
  4408. MISSING_CASE(intel_encoder->type);
  4409. return POWER_DOMAIN_AUX_A;
  4410. }
  4411. }
  4412. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4413. struct intel_crtc_state *crtc_state)
  4414. {
  4415. struct drm_device *dev = crtc->dev;
  4416. struct drm_encoder *encoder;
  4417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4418. enum pipe pipe = intel_crtc->pipe;
  4419. unsigned long mask;
  4420. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4421. if (!crtc_state->base.active)
  4422. return 0;
  4423. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4424. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4425. if (crtc_state->pch_pfit.enabled ||
  4426. crtc_state->pch_pfit.force_thru)
  4427. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4428. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4429. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4430. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4431. }
  4432. if (crtc_state->shared_dpll)
  4433. mask |= BIT(POWER_DOMAIN_PLLS);
  4434. return mask;
  4435. }
  4436. static unsigned long
  4437. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4438. struct intel_crtc_state *crtc_state)
  4439. {
  4440. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4442. enum intel_display_power_domain domain;
  4443. unsigned long domains, new_domains, old_domains;
  4444. old_domains = intel_crtc->enabled_power_domains;
  4445. intel_crtc->enabled_power_domains = new_domains =
  4446. get_crtc_power_domains(crtc, crtc_state);
  4447. domains = new_domains & ~old_domains;
  4448. for_each_power_domain(domain, domains)
  4449. intel_display_power_get(dev_priv, domain);
  4450. return old_domains & ~new_domains;
  4451. }
  4452. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4453. unsigned long domains)
  4454. {
  4455. enum intel_display_power_domain domain;
  4456. for_each_power_domain(domain, domains)
  4457. intel_display_power_put(dev_priv, domain);
  4458. }
  4459. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4460. {
  4461. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4462. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4463. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4464. return max_cdclk_freq;
  4465. else if (IS_CHERRYVIEW(dev_priv))
  4466. return max_cdclk_freq*95/100;
  4467. else if (INTEL_INFO(dev_priv)->gen < 4)
  4468. return 2*max_cdclk_freq*90/100;
  4469. else
  4470. return max_cdclk_freq*90/100;
  4471. }
  4472. static int skl_calc_cdclk(int max_pixclk, int vco);
  4473. static void intel_update_max_cdclk(struct drm_device *dev)
  4474. {
  4475. struct drm_i915_private *dev_priv = dev->dev_private;
  4476. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4477. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4478. int max_cdclk, vco;
  4479. vco = dev_priv->skl_preferred_vco_freq;
  4480. WARN_ON(vco != 8100000 && vco != 8640000);
  4481. /*
  4482. * Use the lower (vco 8640) cdclk values as a
  4483. * first guess. skl_calc_cdclk() will correct it
  4484. * if the preferred vco is 8100 instead.
  4485. */
  4486. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4487. max_cdclk = 617143;
  4488. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4489. max_cdclk = 540000;
  4490. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4491. max_cdclk = 432000;
  4492. else
  4493. max_cdclk = 308571;
  4494. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4495. } else if (IS_BROXTON(dev)) {
  4496. dev_priv->max_cdclk_freq = 624000;
  4497. } else if (IS_BROADWELL(dev)) {
  4498. /*
  4499. * FIXME with extra cooling we can allow
  4500. * 540 MHz for ULX and 675 Mhz for ULT.
  4501. * How can we know if extra cooling is
  4502. * available? PCI ID, VTB, something else?
  4503. */
  4504. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4505. dev_priv->max_cdclk_freq = 450000;
  4506. else if (IS_BDW_ULX(dev))
  4507. dev_priv->max_cdclk_freq = 450000;
  4508. else if (IS_BDW_ULT(dev))
  4509. dev_priv->max_cdclk_freq = 540000;
  4510. else
  4511. dev_priv->max_cdclk_freq = 675000;
  4512. } else if (IS_CHERRYVIEW(dev)) {
  4513. dev_priv->max_cdclk_freq = 320000;
  4514. } else if (IS_VALLEYVIEW(dev)) {
  4515. dev_priv->max_cdclk_freq = 400000;
  4516. } else {
  4517. /* otherwise assume cdclk is fixed */
  4518. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4519. }
  4520. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4521. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4522. dev_priv->max_cdclk_freq);
  4523. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4524. dev_priv->max_dotclk_freq);
  4525. }
  4526. static void intel_update_cdclk(struct drm_device *dev)
  4527. {
  4528. struct drm_i915_private *dev_priv = dev->dev_private;
  4529. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4530. if (INTEL_GEN(dev_priv) >= 9)
  4531. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4532. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4533. dev_priv->cdclk_pll.ref);
  4534. else
  4535. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4536. dev_priv->cdclk_freq);
  4537. /*
  4538. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4539. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4540. * of cdclk that generates 4MHz reference clock freq which is used to
  4541. * generate GMBus clock. This will vary with the cdclk freq.
  4542. */
  4543. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4544. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4545. }
  4546. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4547. static int skl_cdclk_decimal(int cdclk)
  4548. {
  4549. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4550. }
  4551. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4552. {
  4553. int ratio;
  4554. if (cdclk == dev_priv->cdclk_pll.ref)
  4555. return 0;
  4556. switch (cdclk) {
  4557. default:
  4558. MISSING_CASE(cdclk);
  4559. case 144000:
  4560. case 288000:
  4561. case 384000:
  4562. case 576000:
  4563. ratio = 60;
  4564. break;
  4565. case 624000:
  4566. ratio = 65;
  4567. break;
  4568. }
  4569. return dev_priv->cdclk_pll.ref * ratio;
  4570. }
  4571. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4572. {
  4573. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4574. /* Timeout 200us */
  4575. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
  4576. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4577. dev_priv->cdclk_pll.vco = 0;
  4578. }
  4579. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4580. {
  4581. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4582. u32 val;
  4583. val = I915_READ(BXT_DE_PLL_CTL);
  4584. val &= ~BXT_DE_PLL_RATIO_MASK;
  4585. val |= BXT_DE_PLL_RATIO(ratio);
  4586. I915_WRITE(BXT_DE_PLL_CTL, val);
  4587. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4588. /* Timeout 200us */
  4589. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
  4590. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4591. dev_priv->cdclk_pll.vco = vco;
  4592. }
  4593. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4594. {
  4595. u32 val, divider;
  4596. int vco, ret;
  4597. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4598. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4599. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4600. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4601. case 8:
  4602. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4603. break;
  4604. case 4:
  4605. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4606. break;
  4607. case 3:
  4608. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4609. break;
  4610. case 2:
  4611. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4612. break;
  4613. default:
  4614. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4615. WARN_ON(vco != 0);
  4616. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4617. break;
  4618. }
  4619. /* Inform power controller of upcoming frequency change */
  4620. mutex_lock(&dev_priv->rps.hw_lock);
  4621. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4622. 0x80000000);
  4623. mutex_unlock(&dev_priv->rps.hw_lock);
  4624. if (ret) {
  4625. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4626. ret, cdclk);
  4627. return;
  4628. }
  4629. if (dev_priv->cdclk_pll.vco != 0 &&
  4630. dev_priv->cdclk_pll.vco != vco)
  4631. bxt_de_pll_disable(dev_priv);
  4632. if (dev_priv->cdclk_pll.vco != vco)
  4633. bxt_de_pll_enable(dev_priv, vco);
  4634. val = divider | skl_cdclk_decimal(cdclk);
  4635. /*
  4636. * FIXME if only the cd2x divider needs changing, it could be done
  4637. * without shutting off the pipe (if only one pipe is active).
  4638. */
  4639. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4640. /*
  4641. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4642. * enable otherwise.
  4643. */
  4644. if (cdclk >= 500000)
  4645. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4646. I915_WRITE(CDCLK_CTL, val);
  4647. mutex_lock(&dev_priv->rps.hw_lock);
  4648. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4649. DIV_ROUND_UP(cdclk, 25000));
  4650. mutex_unlock(&dev_priv->rps.hw_lock);
  4651. if (ret) {
  4652. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4653. ret, cdclk);
  4654. return;
  4655. }
  4656. intel_update_cdclk(dev_priv->dev);
  4657. }
  4658. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4659. {
  4660. u32 cdctl, expected;
  4661. intel_update_cdclk(dev_priv->dev);
  4662. if (dev_priv->cdclk_pll.vco == 0 ||
  4663. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4664. goto sanitize;
  4665. /* DPLL okay; verify the cdclock
  4666. *
  4667. * Some BIOS versions leave an incorrect decimal frequency value and
  4668. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4669. * so sanitize this register.
  4670. */
  4671. cdctl = I915_READ(CDCLK_CTL);
  4672. /*
  4673. * Let's ignore the pipe field, since BIOS could have configured the
  4674. * dividers both synching to an active pipe, or asynchronously
  4675. * (PIPE_NONE).
  4676. */
  4677. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4678. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4679. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4680. /*
  4681. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4682. * enable otherwise.
  4683. */
  4684. if (dev_priv->cdclk_freq >= 500000)
  4685. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4686. if (cdctl == expected)
  4687. /* All well; nothing to sanitize */
  4688. return;
  4689. sanitize:
  4690. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4691. /* force cdclk programming */
  4692. dev_priv->cdclk_freq = 0;
  4693. /* force full PLL disable + enable */
  4694. dev_priv->cdclk_pll.vco = -1;
  4695. }
  4696. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  4697. {
  4698. bxt_sanitize_cdclk(dev_priv);
  4699. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4700. return;
  4701. /*
  4702. * FIXME:
  4703. * - The initial CDCLK needs to be read from VBT.
  4704. * Need to make this change after VBT has changes for BXT.
  4705. */
  4706. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  4707. }
  4708. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  4709. {
  4710. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4711. }
  4712. static int skl_calc_cdclk(int max_pixclk, int vco)
  4713. {
  4714. if (vco == 8640000) {
  4715. if (max_pixclk > 540000)
  4716. return 617143;
  4717. else if (max_pixclk > 432000)
  4718. return 540000;
  4719. else if (max_pixclk > 308571)
  4720. return 432000;
  4721. else
  4722. return 308571;
  4723. } else {
  4724. if (max_pixclk > 540000)
  4725. return 675000;
  4726. else if (max_pixclk > 450000)
  4727. return 540000;
  4728. else if (max_pixclk > 337500)
  4729. return 450000;
  4730. else
  4731. return 337500;
  4732. }
  4733. }
  4734. static void
  4735. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4736. {
  4737. u32 val;
  4738. dev_priv->cdclk_pll.ref = 24000;
  4739. dev_priv->cdclk_pll.vco = 0;
  4740. val = I915_READ(LCPLL1_CTL);
  4741. if ((val & LCPLL_PLL_ENABLE) == 0)
  4742. return;
  4743. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4744. return;
  4745. val = I915_READ(DPLL_CTRL1);
  4746. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4747. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4748. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4749. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4750. return;
  4751. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4752. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4753. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4754. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4755. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4756. dev_priv->cdclk_pll.vco = 8100000;
  4757. break;
  4758. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4759. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4760. dev_priv->cdclk_pll.vco = 8640000;
  4761. break;
  4762. default:
  4763. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4764. break;
  4765. }
  4766. }
  4767. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4768. {
  4769. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4770. dev_priv->skl_preferred_vco_freq = vco;
  4771. if (changed)
  4772. intel_update_max_cdclk(dev_priv->dev);
  4773. }
  4774. static void
  4775. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4776. {
  4777. int min_cdclk = skl_calc_cdclk(0, vco);
  4778. u32 val;
  4779. WARN_ON(vco != 8100000 && vco != 8640000);
  4780. /* select the minimum CDCLK before enabling DPLL 0 */
  4781. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4782. I915_WRITE(CDCLK_CTL, val);
  4783. POSTING_READ(CDCLK_CTL);
  4784. /*
  4785. * We always enable DPLL0 with the lowest link rate possible, but still
  4786. * taking into account the VCO required to operate the eDP panel at the
  4787. * desired frequency. The usual DP link rates operate with a VCO of
  4788. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4789. * The modeset code is responsible for the selection of the exact link
  4790. * rate later on, with the constraint of choosing a frequency that
  4791. * works with vco.
  4792. */
  4793. val = I915_READ(DPLL_CTRL1);
  4794. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4795. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4796. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4797. if (vco == 8640000)
  4798. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4799. SKL_DPLL0);
  4800. else
  4801. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4802. SKL_DPLL0);
  4803. I915_WRITE(DPLL_CTRL1, val);
  4804. POSTING_READ(DPLL_CTRL1);
  4805. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4806. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4807. DRM_ERROR("DPLL0 not locked\n");
  4808. dev_priv->cdclk_pll.vco = vco;
  4809. /* We'll want to keep using the current vco from now on. */
  4810. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4811. }
  4812. static void
  4813. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4814. {
  4815. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4816. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4817. DRM_ERROR("Couldn't disable DPLL0\n");
  4818. dev_priv->cdclk_pll.vco = 0;
  4819. }
  4820. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4821. {
  4822. int ret;
  4823. u32 val;
  4824. /* inform PCU we want to change CDCLK */
  4825. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4826. mutex_lock(&dev_priv->rps.hw_lock);
  4827. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4828. mutex_unlock(&dev_priv->rps.hw_lock);
  4829. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4830. }
  4831. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4832. {
  4833. unsigned int i;
  4834. for (i = 0; i < 15; i++) {
  4835. if (skl_cdclk_pcu_ready(dev_priv))
  4836. return true;
  4837. udelay(10);
  4838. }
  4839. return false;
  4840. }
  4841. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4842. {
  4843. struct drm_device *dev = dev_priv->dev;
  4844. u32 freq_select, pcu_ack;
  4845. WARN_ON((cdclk == 24000) != (vco == 0));
  4846. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4847. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4848. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4849. return;
  4850. }
  4851. /* set CDCLK_CTL */
  4852. switch (cdclk) {
  4853. case 450000:
  4854. case 432000:
  4855. freq_select = CDCLK_FREQ_450_432;
  4856. pcu_ack = 1;
  4857. break;
  4858. case 540000:
  4859. freq_select = CDCLK_FREQ_540;
  4860. pcu_ack = 2;
  4861. break;
  4862. case 308571:
  4863. case 337500:
  4864. default:
  4865. freq_select = CDCLK_FREQ_337_308;
  4866. pcu_ack = 0;
  4867. break;
  4868. case 617143:
  4869. case 675000:
  4870. freq_select = CDCLK_FREQ_675_617;
  4871. pcu_ack = 3;
  4872. break;
  4873. }
  4874. if (dev_priv->cdclk_pll.vco != 0 &&
  4875. dev_priv->cdclk_pll.vco != vco)
  4876. skl_dpll0_disable(dev_priv);
  4877. if (dev_priv->cdclk_pll.vco != vco)
  4878. skl_dpll0_enable(dev_priv, vco);
  4879. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4880. POSTING_READ(CDCLK_CTL);
  4881. /* inform PCU of the change */
  4882. mutex_lock(&dev_priv->rps.hw_lock);
  4883. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4884. mutex_unlock(&dev_priv->rps.hw_lock);
  4885. intel_update_cdclk(dev);
  4886. }
  4887. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4888. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4889. {
  4890. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4891. }
  4892. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4893. {
  4894. int cdclk, vco;
  4895. skl_sanitize_cdclk(dev_priv);
  4896. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4897. /*
  4898. * Use the current vco as our initial
  4899. * guess as to what the preferred vco is.
  4900. */
  4901. if (dev_priv->skl_preferred_vco_freq == 0)
  4902. skl_set_preferred_cdclk_vco(dev_priv,
  4903. dev_priv->cdclk_pll.vco);
  4904. return;
  4905. }
  4906. vco = dev_priv->skl_preferred_vco_freq;
  4907. if (vco == 0)
  4908. vco = 8100000;
  4909. cdclk = skl_calc_cdclk(0, vco);
  4910. skl_set_cdclk(dev_priv, cdclk, vco);
  4911. }
  4912. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4913. {
  4914. uint32_t cdctl, expected;
  4915. /*
  4916. * check if the pre-os intialized the display
  4917. * There is SWF18 scratchpad register defined which is set by the
  4918. * pre-os which can be used by the OS drivers to check the status
  4919. */
  4920. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4921. goto sanitize;
  4922. intel_update_cdclk(dev_priv->dev);
  4923. /* Is PLL enabled and locked ? */
  4924. if (dev_priv->cdclk_pll.vco == 0 ||
  4925. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4926. goto sanitize;
  4927. /* DPLL okay; verify the cdclock
  4928. *
  4929. * Noticed in some instances that the freq selection is correct but
  4930. * decimal part is programmed wrong from BIOS where pre-os does not
  4931. * enable display. Verify the same as well.
  4932. */
  4933. cdctl = I915_READ(CDCLK_CTL);
  4934. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4935. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4936. if (cdctl == expected)
  4937. /* All well; nothing to sanitize */
  4938. return;
  4939. sanitize:
  4940. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4941. /* force cdclk programming */
  4942. dev_priv->cdclk_freq = 0;
  4943. /* force full PLL disable + enable */
  4944. dev_priv->cdclk_pll.vco = -1;
  4945. }
  4946. /* Adjust CDclk dividers to allow high res or save power if possible */
  4947. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4948. {
  4949. struct drm_i915_private *dev_priv = dev->dev_private;
  4950. u32 val, cmd;
  4951. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4952. != dev_priv->cdclk_freq);
  4953. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4954. cmd = 2;
  4955. else if (cdclk == 266667)
  4956. cmd = 1;
  4957. else
  4958. cmd = 0;
  4959. mutex_lock(&dev_priv->rps.hw_lock);
  4960. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4961. val &= ~DSPFREQGUAR_MASK;
  4962. val |= (cmd << DSPFREQGUAR_SHIFT);
  4963. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4964. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4965. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4966. 50)) {
  4967. DRM_ERROR("timed out waiting for CDclk change\n");
  4968. }
  4969. mutex_unlock(&dev_priv->rps.hw_lock);
  4970. mutex_lock(&dev_priv->sb_lock);
  4971. if (cdclk == 400000) {
  4972. u32 divider;
  4973. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4974. /* adjust cdclk divider */
  4975. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4976. val &= ~CCK_FREQUENCY_VALUES;
  4977. val |= divider;
  4978. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4979. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4980. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4981. 50))
  4982. DRM_ERROR("timed out waiting for CDclk change\n");
  4983. }
  4984. /* adjust self-refresh exit latency value */
  4985. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4986. val &= ~0x7f;
  4987. /*
  4988. * For high bandwidth configs, we set a higher latency in the bunit
  4989. * so that the core display fetch happens in time to avoid underruns.
  4990. */
  4991. if (cdclk == 400000)
  4992. val |= 4500 / 250; /* 4.5 usec */
  4993. else
  4994. val |= 3000 / 250; /* 3.0 usec */
  4995. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4996. mutex_unlock(&dev_priv->sb_lock);
  4997. intel_update_cdclk(dev);
  4998. }
  4999. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5000. {
  5001. struct drm_i915_private *dev_priv = dev->dev_private;
  5002. u32 val, cmd;
  5003. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5004. != dev_priv->cdclk_freq);
  5005. switch (cdclk) {
  5006. case 333333:
  5007. case 320000:
  5008. case 266667:
  5009. case 200000:
  5010. break;
  5011. default:
  5012. MISSING_CASE(cdclk);
  5013. return;
  5014. }
  5015. /*
  5016. * Specs are full of misinformation, but testing on actual
  5017. * hardware has shown that we just need to write the desired
  5018. * CCK divider into the Punit register.
  5019. */
  5020. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5021. mutex_lock(&dev_priv->rps.hw_lock);
  5022. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5023. val &= ~DSPFREQGUAR_MASK_CHV;
  5024. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5025. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5026. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5027. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5028. 50)) {
  5029. DRM_ERROR("timed out waiting for CDclk change\n");
  5030. }
  5031. mutex_unlock(&dev_priv->rps.hw_lock);
  5032. intel_update_cdclk(dev);
  5033. }
  5034. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5035. int max_pixclk)
  5036. {
  5037. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5038. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5039. /*
  5040. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5041. * 200MHz
  5042. * 267MHz
  5043. * 320/333MHz (depends on HPLL freq)
  5044. * 400MHz (VLV only)
  5045. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5046. * of the lower bin and adjust if needed.
  5047. *
  5048. * We seem to get an unstable or solid color picture at 200MHz.
  5049. * Not sure what's wrong. For now use 200MHz only when all pipes
  5050. * are off.
  5051. */
  5052. if (!IS_CHERRYVIEW(dev_priv) &&
  5053. max_pixclk > freq_320*limit/100)
  5054. return 400000;
  5055. else if (max_pixclk > 266667*limit/100)
  5056. return freq_320;
  5057. else if (max_pixclk > 0)
  5058. return 266667;
  5059. else
  5060. return 200000;
  5061. }
  5062. static int bxt_calc_cdclk(int max_pixclk)
  5063. {
  5064. if (max_pixclk > 576000)
  5065. return 624000;
  5066. else if (max_pixclk > 384000)
  5067. return 576000;
  5068. else if (max_pixclk > 288000)
  5069. return 384000;
  5070. else if (max_pixclk > 144000)
  5071. return 288000;
  5072. else
  5073. return 144000;
  5074. }
  5075. /* Compute the max pixel clock for new configuration. */
  5076. static int intel_mode_max_pixclk(struct drm_device *dev,
  5077. struct drm_atomic_state *state)
  5078. {
  5079. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5080. struct drm_i915_private *dev_priv = dev->dev_private;
  5081. struct drm_crtc *crtc;
  5082. struct drm_crtc_state *crtc_state;
  5083. unsigned max_pixclk = 0, i;
  5084. enum pipe pipe;
  5085. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5086. sizeof(intel_state->min_pixclk));
  5087. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5088. int pixclk = 0;
  5089. if (crtc_state->enable)
  5090. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5091. intel_state->min_pixclk[i] = pixclk;
  5092. }
  5093. for_each_pipe(dev_priv, pipe)
  5094. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5095. return max_pixclk;
  5096. }
  5097. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5098. {
  5099. struct drm_device *dev = state->dev;
  5100. struct drm_i915_private *dev_priv = dev->dev_private;
  5101. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5102. struct intel_atomic_state *intel_state =
  5103. to_intel_atomic_state(state);
  5104. intel_state->cdclk = intel_state->dev_cdclk =
  5105. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5106. if (!intel_state->active_crtcs)
  5107. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5108. return 0;
  5109. }
  5110. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5111. {
  5112. int max_pixclk = ilk_max_pixel_rate(state);
  5113. struct intel_atomic_state *intel_state =
  5114. to_intel_atomic_state(state);
  5115. intel_state->cdclk = intel_state->dev_cdclk =
  5116. bxt_calc_cdclk(max_pixclk);
  5117. if (!intel_state->active_crtcs)
  5118. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5119. return 0;
  5120. }
  5121. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5122. {
  5123. unsigned int credits, default_credits;
  5124. if (IS_CHERRYVIEW(dev_priv))
  5125. default_credits = PFI_CREDIT(12);
  5126. else
  5127. default_credits = PFI_CREDIT(8);
  5128. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5129. /* CHV suggested value is 31 or 63 */
  5130. if (IS_CHERRYVIEW(dev_priv))
  5131. credits = PFI_CREDIT_63;
  5132. else
  5133. credits = PFI_CREDIT(15);
  5134. } else {
  5135. credits = default_credits;
  5136. }
  5137. /*
  5138. * WA - write default credits before re-programming
  5139. * FIXME: should we also set the resend bit here?
  5140. */
  5141. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5142. default_credits);
  5143. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5144. credits | PFI_CREDIT_RESEND);
  5145. /*
  5146. * FIXME is this guaranteed to clear
  5147. * immediately or should we poll for it?
  5148. */
  5149. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5150. }
  5151. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5152. {
  5153. struct drm_device *dev = old_state->dev;
  5154. struct drm_i915_private *dev_priv = dev->dev_private;
  5155. struct intel_atomic_state *old_intel_state =
  5156. to_intel_atomic_state(old_state);
  5157. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5158. /*
  5159. * FIXME: We can end up here with all power domains off, yet
  5160. * with a CDCLK frequency other than the minimum. To account
  5161. * for this take the PIPE-A power domain, which covers the HW
  5162. * blocks needed for the following programming. This can be
  5163. * removed once it's guaranteed that we get here either with
  5164. * the minimum CDCLK set, or the required power domains
  5165. * enabled.
  5166. */
  5167. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5168. if (IS_CHERRYVIEW(dev))
  5169. cherryview_set_cdclk(dev, req_cdclk);
  5170. else
  5171. valleyview_set_cdclk(dev, req_cdclk);
  5172. vlv_program_pfi_credits(dev_priv);
  5173. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5174. }
  5175. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5176. {
  5177. struct drm_device *dev = crtc->dev;
  5178. struct drm_i915_private *dev_priv = to_i915(dev);
  5179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5180. struct intel_encoder *encoder;
  5181. struct intel_crtc_state *pipe_config =
  5182. to_intel_crtc_state(crtc->state);
  5183. int pipe = intel_crtc->pipe;
  5184. if (WARN_ON(intel_crtc->active))
  5185. return;
  5186. if (intel_crtc->config->has_dp_encoder)
  5187. intel_dp_set_m_n(intel_crtc, M1_N1);
  5188. intel_set_pipe_timings(intel_crtc);
  5189. intel_set_pipe_src_size(intel_crtc);
  5190. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5191. struct drm_i915_private *dev_priv = dev->dev_private;
  5192. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5193. I915_WRITE(CHV_CANVAS(pipe), 0);
  5194. }
  5195. i9xx_set_pipeconf(intel_crtc);
  5196. intel_crtc->active = true;
  5197. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5198. for_each_encoder_on_crtc(dev, crtc, encoder)
  5199. if (encoder->pre_pll_enable)
  5200. encoder->pre_pll_enable(encoder);
  5201. if (IS_CHERRYVIEW(dev)) {
  5202. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5203. chv_enable_pll(intel_crtc, intel_crtc->config);
  5204. } else {
  5205. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5206. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5207. }
  5208. for_each_encoder_on_crtc(dev, crtc, encoder)
  5209. if (encoder->pre_enable)
  5210. encoder->pre_enable(encoder);
  5211. i9xx_pfit_enable(intel_crtc);
  5212. intel_color_load_luts(&pipe_config->base);
  5213. intel_update_watermarks(crtc);
  5214. intel_enable_pipe(intel_crtc);
  5215. assert_vblank_disabled(crtc);
  5216. drm_crtc_vblank_on(crtc);
  5217. for_each_encoder_on_crtc(dev, crtc, encoder)
  5218. encoder->enable(encoder);
  5219. }
  5220. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5221. {
  5222. struct drm_device *dev = crtc->base.dev;
  5223. struct drm_i915_private *dev_priv = dev->dev_private;
  5224. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5225. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5226. }
  5227. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5228. {
  5229. struct drm_device *dev = crtc->dev;
  5230. struct drm_i915_private *dev_priv = to_i915(dev);
  5231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5232. struct intel_encoder *encoder;
  5233. struct intel_crtc_state *pipe_config =
  5234. to_intel_crtc_state(crtc->state);
  5235. enum pipe pipe = intel_crtc->pipe;
  5236. if (WARN_ON(intel_crtc->active))
  5237. return;
  5238. i9xx_set_pll_dividers(intel_crtc);
  5239. if (intel_crtc->config->has_dp_encoder)
  5240. intel_dp_set_m_n(intel_crtc, M1_N1);
  5241. intel_set_pipe_timings(intel_crtc);
  5242. intel_set_pipe_src_size(intel_crtc);
  5243. i9xx_set_pipeconf(intel_crtc);
  5244. intel_crtc->active = true;
  5245. if (!IS_GEN2(dev))
  5246. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5247. for_each_encoder_on_crtc(dev, crtc, encoder)
  5248. if (encoder->pre_enable)
  5249. encoder->pre_enable(encoder);
  5250. i9xx_enable_pll(intel_crtc);
  5251. i9xx_pfit_enable(intel_crtc);
  5252. intel_color_load_luts(&pipe_config->base);
  5253. intel_update_watermarks(crtc);
  5254. intel_enable_pipe(intel_crtc);
  5255. assert_vblank_disabled(crtc);
  5256. drm_crtc_vblank_on(crtc);
  5257. for_each_encoder_on_crtc(dev, crtc, encoder)
  5258. encoder->enable(encoder);
  5259. }
  5260. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5261. {
  5262. struct drm_device *dev = crtc->base.dev;
  5263. struct drm_i915_private *dev_priv = dev->dev_private;
  5264. if (!crtc->config->gmch_pfit.control)
  5265. return;
  5266. assert_pipe_disabled(dev_priv, crtc->pipe);
  5267. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5268. I915_READ(PFIT_CONTROL));
  5269. I915_WRITE(PFIT_CONTROL, 0);
  5270. }
  5271. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5272. {
  5273. struct drm_device *dev = crtc->dev;
  5274. struct drm_i915_private *dev_priv = dev->dev_private;
  5275. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5276. struct intel_encoder *encoder;
  5277. int pipe = intel_crtc->pipe;
  5278. /*
  5279. * On gen2 planes are double buffered but the pipe isn't, so we must
  5280. * wait for planes to fully turn off before disabling the pipe.
  5281. */
  5282. if (IS_GEN2(dev))
  5283. intel_wait_for_vblank(dev, pipe);
  5284. for_each_encoder_on_crtc(dev, crtc, encoder)
  5285. encoder->disable(encoder);
  5286. drm_crtc_vblank_off(crtc);
  5287. assert_vblank_disabled(crtc);
  5288. intel_disable_pipe(intel_crtc);
  5289. i9xx_pfit_disable(intel_crtc);
  5290. for_each_encoder_on_crtc(dev, crtc, encoder)
  5291. if (encoder->post_disable)
  5292. encoder->post_disable(encoder);
  5293. if (!intel_crtc->config->has_dsi_encoder) {
  5294. if (IS_CHERRYVIEW(dev))
  5295. chv_disable_pll(dev_priv, pipe);
  5296. else if (IS_VALLEYVIEW(dev))
  5297. vlv_disable_pll(dev_priv, pipe);
  5298. else
  5299. i9xx_disable_pll(intel_crtc);
  5300. }
  5301. for_each_encoder_on_crtc(dev, crtc, encoder)
  5302. if (encoder->post_pll_disable)
  5303. encoder->post_pll_disable(encoder);
  5304. if (!IS_GEN2(dev))
  5305. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5306. }
  5307. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5308. {
  5309. struct intel_encoder *encoder;
  5310. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5311. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5312. enum intel_display_power_domain domain;
  5313. unsigned long domains;
  5314. if (!intel_crtc->active)
  5315. return;
  5316. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5317. WARN_ON(intel_crtc->flip_work);
  5318. intel_pre_disable_primary_noatomic(crtc);
  5319. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5320. to_intel_plane_state(crtc->primary->state)->visible = false;
  5321. }
  5322. dev_priv->display.crtc_disable(crtc);
  5323. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5324. crtc->base.id, crtc->name);
  5325. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5326. crtc->state->active = false;
  5327. intel_crtc->active = false;
  5328. crtc->enabled = false;
  5329. crtc->state->connector_mask = 0;
  5330. crtc->state->encoder_mask = 0;
  5331. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5332. encoder->base.crtc = NULL;
  5333. intel_fbc_disable(intel_crtc);
  5334. intel_update_watermarks(crtc);
  5335. intel_disable_shared_dpll(intel_crtc);
  5336. domains = intel_crtc->enabled_power_domains;
  5337. for_each_power_domain(domain, domains)
  5338. intel_display_power_put(dev_priv, domain);
  5339. intel_crtc->enabled_power_domains = 0;
  5340. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5341. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5342. }
  5343. /*
  5344. * turn all crtc's off, but do not adjust state
  5345. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5346. */
  5347. int intel_display_suspend(struct drm_device *dev)
  5348. {
  5349. struct drm_i915_private *dev_priv = to_i915(dev);
  5350. struct drm_atomic_state *state;
  5351. int ret;
  5352. state = drm_atomic_helper_suspend(dev);
  5353. ret = PTR_ERR_OR_ZERO(state);
  5354. if (ret)
  5355. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5356. else
  5357. dev_priv->modeset_restore_state = state;
  5358. return ret;
  5359. }
  5360. void intel_encoder_destroy(struct drm_encoder *encoder)
  5361. {
  5362. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5363. drm_encoder_cleanup(encoder);
  5364. kfree(intel_encoder);
  5365. }
  5366. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5367. * internal consistency). */
  5368. static void intel_connector_verify_state(struct intel_connector *connector)
  5369. {
  5370. struct drm_crtc *crtc = connector->base.state->crtc;
  5371. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5372. connector->base.base.id,
  5373. connector->base.name);
  5374. if (connector->get_hw_state(connector)) {
  5375. struct intel_encoder *encoder = connector->encoder;
  5376. struct drm_connector_state *conn_state = connector->base.state;
  5377. I915_STATE_WARN(!crtc,
  5378. "connector enabled without attached crtc\n");
  5379. if (!crtc)
  5380. return;
  5381. I915_STATE_WARN(!crtc->state->active,
  5382. "connector is active, but attached crtc isn't\n");
  5383. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5384. return;
  5385. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5386. "atomic encoder doesn't match attached encoder\n");
  5387. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5388. "attached encoder crtc differs from connector crtc\n");
  5389. } else {
  5390. I915_STATE_WARN(crtc && crtc->state->active,
  5391. "attached crtc is active, but connector isn't\n");
  5392. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5393. "best encoder set without crtc!\n");
  5394. }
  5395. }
  5396. int intel_connector_init(struct intel_connector *connector)
  5397. {
  5398. drm_atomic_helper_connector_reset(&connector->base);
  5399. if (!connector->base.state)
  5400. return -ENOMEM;
  5401. return 0;
  5402. }
  5403. struct intel_connector *intel_connector_alloc(void)
  5404. {
  5405. struct intel_connector *connector;
  5406. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5407. if (!connector)
  5408. return NULL;
  5409. if (intel_connector_init(connector) < 0) {
  5410. kfree(connector);
  5411. return NULL;
  5412. }
  5413. return connector;
  5414. }
  5415. /* Simple connector->get_hw_state implementation for encoders that support only
  5416. * one connector and no cloning and hence the encoder state determines the state
  5417. * of the connector. */
  5418. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5419. {
  5420. enum pipe pipe = 0;
  5421. struct intel_encoder *encoder = connector->encoder;
  5422. return encoder->get_hw_state(encoder, &pipe);
  5423. }
  5424. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5425. {
  5426. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5427. return crtc_state->fdi_lanes;
  5428. return 0;
  5429. }
  5430. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5431. struct intel_crtc_state *pipe_config)
  5432. {
  5433. struct drm_atomic_state *state = pipe_config->base.state;
  5434. struct intel_crtc *other_crtc;
  5435. struct intel_crtc_state *other_crtc_state;
  5436. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5437. pipe_name(pipe), pipe_config->fdi_lanes);
  5438. if (pipe_config->fdi_lanes > 4) {
  5439. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5440. pipe_name(pipe), pipe_config->fdi_lanes);
  5441. return -EINVAL;
  5442. }
  5443. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5444. if (pipe_config->fdi_lanes > 2) {
  5445. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5446. pipe_config->fdi_lanes);
  5447. return -EINVAL;
  5448. } else {
  5449. return 0;
  5450. }
  5451. }
  5452. if (INTEL_INFO(dev)->num_pipes == 2)
  5453. return 0;
  5454. /* Ivybridge 3 pipe is really complicated */
  5455. switch (pipe) {
  5456. case PIPE_A:
  5457. return 0;
  5458. case PIPE_B:
  5459. if (pipe_config->fdi_lanes <= 2)
  5460. return 0;
  5461. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5462. other_crtc_state =
  5463. intel_atomic_get_crtc_state(state, other_crtc);
  5464. if (IS_ERR(other_crtc_state))
  5465. return PTR_ERR(other_crtc_state);
  5466. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5467. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5468. pipe_name(pipe), pipe_config->fdi_lanes);
  5469. return -EINVAL;
  5470. }
  5471. return 0;
  5472. case PIPE_C:
  5473. if (pipe_config->fdi_lanes > 2) {
  5474. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5475. pipe_name(pipe), pipe_config->fdi_lanes);
  5476. return -EINVAL;
  5477. }
  5478. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5479. other_crtc_state =
  5480. intel_atomic_get_crtc_state(state, other_crtc);
  5481. if (IS_ERR(other_crtc_state))
  5482. return PTR_ERR(other_crtc_state);
  5483. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5484. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5485. return -EINVAL;
  5486. }
  5487. return 0;
  5488. default:
  5489. BUG();
  5490. }
  5491. }
  5492. #define RETRY 1
  5493. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5494. struct intel_crtc_state *pipe_config)
  5495. {
  5496. struct drm_device *dev = intel_crtc->base.dev;
  5497. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5498. int lane, link_bw, fdi_dotclock, ret;
  5499. bool needs_recompute = false;
  5500. retry:
  5501. /* FDI is a binary signal running at ~2.7GHz, encoding
  5502. * each output octet as 10 bits. The actual frequency
  5503. * is stored as a divider into a 100MHz clock, and the
  5504. * mode pixel clock is stored in units of 1KHz.
  5505. * Hence the bw of each lane in terms of the mode signal
  5506. * is:
  5507. */
  5508. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5509. fdi_dotclock = adjusted_mode->crtc_clock;
  5510. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5511. pipe_config->pipe_bpp);
  5512. pipe_config->fdi_lanes = lane;
  5513. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5514. link_bw, &pipe_config->fdi_m_n);
  5515. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5516. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5517. pipe_config->pipe_bpp -= 2*3;
  5518. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5519. pipe_config->pipe_bpp);
  5520. needs_recompute = true;
  5521. pipe_config->bw_constrained = true;
  5522. goto retry;
  5523. }
  5524. if (needs_recompute)
  5525. return RETRY;
  5526. return ret;
  5527. }
  5528. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5529. struct intel_crtc_state *pipe_config)
  5530. {
  5531. if (pipe_config->pipe_bpp > 24)
  5532. return false;
  5533. /* HSW can handle pixel rate up to cdclk? */
  5534. if (IS_HASWELL(dev_priv))
  5535. return true;
  5536. /*
  5537. * We compare against max which means we must take
  5538. * the increased cdclk requirement into account when
  5539. * calculating the new cdclk.
  5540. *
  5541. * Should measure whether using a lower cdclk w/o IPS
  5542. */
  5543. return ilk_pipe_pixel_rate(pipe_config) <=
  5544. dev_priv->max_cdclk_freq * 95 / 100;
  5545. }
  5546. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5547. struct intel_crtc_state *pipe_config)
  5548. {
  5549. struct drm_device *dev = crtc->base.dev;
  5550. struct drm_i915_private *dev_priv = dev->dev_private;
  5551. pipe_config->ips_enabled = i915.enable_ips &&
  5552. hsw_crtc_supports_ips(crtc) &&
  5553. pipe_config_supports_ips(dev_priv, pipe_config);
  5554. }
  5555. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5556. {
  5557. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5558. /* GDG double wide on either pipe, otherwise pipe A only */
  5559. return INTEL_INFO(dev_priv)->gen < 4 &&
  5560. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5561. }
  5562. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5563. struct intel_crtc_state *pipe_config)
  5564. {
  5565. struct drm_device *dev = crtc->base.dev;
  5566. struct drm_i915_private *dev_priv = dev->dev_private;
  5567. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5568. int clock_limit = dev_priv->max_dotclk_freq;
  5569. if (INTEL_INFO(dev)->gen < 4) {
  5570. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5571. /*
  5572. * Enable double wide mode when the dot clock
  5573. * is > 90% of the (display) core speed.
  5574. */
  5575. if (intel_crtc_supports_double_wide(crtc) &&
  5576. adjusted_mode->crtc_clock > clock_limit) {
  5577. clock_limit = dev_priv->max_dotclk_freq;
  5578. pipe_config->double_wide = true;
  5579. }
  5580. }
  5581. if (adjusted_mode->crtc_clock > clock_limit) {
  5582. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5583. adjusted_mode->crtc_clock, clock_limit,
  5584. yesno(pipe_config->double_wide));
  5585. return -EINVAL;
  5586. }
  5587. /*
  5588. * Pipe horizontal size must be even in:
  5589. * - DVO ganged mode
  5590. * - LVDS dual channel mode
  5591. * - Double wide pipe
  5592. */
  5593. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5594. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5595. pipe_config->pipe_src_w &= ~1;
  5596. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5597. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5598. */
  5599. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5600. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5601. return -EINVAL;
  5602. if (HAS_IPS(dev))
  5603. hsw_compute_ips_config(crtc, pipe_config);
  5604. if (pipe_config->has_pch_encoder)
  5605. return ironlake_fdi_compute_config(crtc, pipe_config);
  5606. return 0;
  5607. }
  5608. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5609. {
  5610. struct drm_i915_private *dev_priv = to_i915(dev);
  5611. uint32_t cdctl;
  5612. skl_dpll0_update(dev_priv);
  5613. if (dev_priv->cdclk_pll.vco == 0)
  5614. return dev_priv->cdclk_pll.ref;
  5615. cdctl = I915_READ(CDCLK_CTL);
  5616. if (dev_priv->cdclk_pll.vco == 8640000) {
  5617. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5618. case CDCLK_FREQ_450_432:
  5619. return 432000;
  5620. case CDCLK_FREQ_337_308:
  5621. return 308571;
  5622. case CDCLK_FREQ_540:
  5623. return 540000;
  5624. case CDCLK_FREQ_675_617:
  5625. return 617143;
  5626. default:
  5627. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5628. }
  5629. } else {
  5630. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5631. case CDCLK_FREQ_450_432:
  5632. return 450000;
  5633. case CDCLK_FREQ_337_308:
  5634. return 337500;
  5635. case CDCLK_FREQ_540:
  5636. return 540000;
  5637. case CDCLK_FREQ_675_617:
  5638. return 675000;
  5639. default:
  5640. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5641. }
  5642. }
  5643. return dev_priv->cdclk_pll.ref;
  5644. }
  5645. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5646. {
  5647. u32 val;
  5648. dev_priv->cdclk_pll.ref = 19200;
  5649. dev_priv->cdclk_pll.vco = 0;
  5650. val = I915_READ(BXT_DE_PLL_ENABLE);
  5651. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5652. return;
  5653. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5654. return;
  5655. val = I915_READ(BXT_DE_PLL_CTL);
  5656. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5657. dev_priv->cdclk_pll.ref;
  5658. }
  5659. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5660. {
  5661. struct drm_i915_private *dev_priv = to_i915(dev);
  5662. u32 divider;
  5663. int div, vco;
  5664. bxt_de_pll_update(dev_priv);
  5665. vco = dev_priv->cdclk_pll.vco;
  5666. if (vco == 0)
  5667. return dev_priv->cdclk_pll.ref;
  5668. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5669. switch (divider) {
  5670. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5671. div = 2;
  5672. break;
  5673. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5674. div = 3;
  5675. break;
  5676. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5677. div = 4;
  5678. break;
  5679. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5680. div = 8;
  5681. break;
  5682. default:
  5683. MISSING_CASE(divider);
  5684. return dev_priv->cdclk_pll.ref;
  5685. }
  5686. return DIV_ROUND_CLOSEST(vco, div);
  5687. }
  5688. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5689. {
  5690. struct drm_i915_private *dev_priv = dev->dev_private;
  5691. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5692. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5693. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5694. return 800000;
  5695. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5696. return 450000;
  5697. else if (freq == LCPLL_CLK_FREQ_450)
  5698. return 450000;
  5699. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5700. return 540000;
  5701. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5702. return 337500;
  5703. else
  5704. return 675000;
  5705. }
  5706. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5707. {
  5708. struct drm_i915_private *dev_priv = dev->dev_private;
  5709. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5710. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5711. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5712. return 800000;
  5713. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5714. return 450000;
  5715. else if (freq == LCPLL_CLK_FREQ_450)
  5716. return 450000;
  5717. else if (IS_HSW_ULT(dev))
  5718. return 337500;
  5719. else
  5720. return 540000;
  5721. }
  5722. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5723. {
  5724. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5725. CCK_DISPLAY_CLOCK_CONTROL);
  5726. }
  5727. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5728. {
  5729. return 450000;
  5730. }
  5731. static int i945_get_display_clock_speed(struct drm_device *dev)
  5732. {
  5733. return 400000;
  5734. }
  5735. static int i915_get_display_clock_speed(struct drm_device *dev)
  5736. {
  5737. return 333333;
  5738. }
  5739. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5740. {
  5741. return 200000;
  5742. }
  5743. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5744. {
  5745. u16 gcfgc = 0;
  5746. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5747. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5748. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5749. return 266667;
  5750. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5751. return 333333;
  5752. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5753. return 444444;
  5754. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5755. return 200000;
  5756. default:
  5757. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5758. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5759. return 133333;
  5760. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5761. return 166667;
  5762. }
  5763. }
  5764. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5765. {
  5766. u16 gcfgc = 0;
  5767. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5768. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5769. return 133333;
  5770. else {
  5771. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5772. case GC_DISPLAY_CLOCK_333_MHZ:
  5773. return 333333;
  5774. default:
  5775. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5776. return 190000;
  5777. }
  5778. }
  5779. }
  5780. static int i865_get_display_clock_speed(struct drm_device *dev)
  5781. {
  5782. return 266667;
  5783. }
  5784. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5785. {
  5786. u16 hpllcc = 0;
  5787. /*
  5788. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5789. * encoding is different :(
  5790. * FIXME is this the right way to detect 852GM/852GMV?
  5791. */
  5792. if (dev->pdev->revision == 0x1)
  5793. return 133333;
  5794. pci_bus_read_config_word(dev->pdev->bus,
  5795. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5796. /* Assume that the hardware is in the high speed state. This
  5797. * should be the default.
  5798. */
  5799. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5800. case GC_CLOCK_133_200:
  5801. case GC_CLOCK_133_200_2:
  5802. case GC_CLOCK_100_200:
  5803. return 200000;
  5804. case GC_CLOCK_166_250:
  5805. return 250000;
  5806. case GC_CLOCK_100_133:
  5807. return 133333;
  5808. case GC_CLOCK_133_266:
  5809. case GC_CLOCK_133_266_2:
  5810. case GC_CLOCK_166_266:
  5811. return 266667;
  5812. }
  5813. /* Shouldn't happen */
  5814. return 0;
  5815. }
  5816. static int i830_get_display_clock_speed(struct drm_device *dev)
  5817. {
  5818. return 133333;
  5819. }
  5820. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5821. {
  5822. struct drm_i915_private *dev_priv = dev->dev_private;
  5823. static const unsigned int blb_vco[8] = {
  5824. [0] = 3200000,
  5825. [1] = 4000000,
  5826. [2] = 5333333,
  5827. [3] = 4800000,
  5828. [4] = 6400000,
  5829. };
  5830. static const unsigned int pnv_vco[8] = {
  5831. [0] = 3200000,
  5832. [1] = 4000000,
  5833. [2] = 5333333,
  5834. [3] = 4800000,
  5835. [4] = 2666667,
  5836. };
  5837. static const unsigned int cl_vco[8] = {
  5838. [0] = 3200000,
  5839. [1] = 4000000,
  5840. [2] = 5333333,
  5841. [3] = 6400000,
  5842. [4] = 3333333,
  5843. [5] = 3566667,
  5844. [6] = 4266667,
  5845. };
  5846. static const unsigned int elk_vco[8] = {
  5847. [0] = 3200000,
  5848. [1] = 4000000,
  5849. [2] = 5333333,
  5850. [3] = 4800000,
  5851. };
  5852. static const unsigned int ctg_vco[8] = {
  5853. [0] = 3200000,
  5854. [1] = 4000000,
  5855. [2] = 5333333,
  5856. [3] = 6400000,
  5857. [4] = 2666667,
  5858. [5] = 4266667,
  5859. };
  5860. const unsigned int *vco_table;
  5861. unsigned int vco;
  5862. uint8_t tmp = 0;
  5863. /* FIXME other chipsets? */
  5864. if (IS_GM45(dev))
  5865. vco_table = ctg_vco;
  5866. else if (IS_G4X(dev))
  5867. vco_table = elk_vco;
  5868. else if (IS_CRESTLINE(dev))
  5869. vco_table = cl_vco;
  5870. else if (IS_PINEVIEW(dev))
  5871. vco_table = pnv_vco;
  5872. else if (IS_G33(dev))
  5873. vco_table = blb_vco;
  5874. else
  5875. return 0;
  5876. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5877. vco = vco_table[tmp & 0x7];
  5878. if (vco == 0)
  5879. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5880. else
  5881. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5882. return vco;
  5883. }
  5884. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5885. {
  5886. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5887. uint16_t tmp = 0;
  5888. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5889. cdclk_sel = (tmp >> 12) & 0x1;
  5890. switch (vco) {
  5891. case 2666667:
  5892. case 4000000:
  5893. case 5333333:
  5894. return cdclk_sel ? 333333 : 222222;
  5895. case 3200000:
  5896. return cdclk_sel ? 320000 : 228571;
  5897. default:
  5898. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5899. return 222222;
  5900. }
  5901. }
  5902. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5903. {
  5904. static const uint8_t div_3200[] = { 16, 10, 8 };
  5905. static const uint8_t div_4000[] = { 20, 12, 10 };
  5906. static const uint8_t div_5333[] = { 24, 16, 14 };
  5907. const uint8_t *div_table;
  5908. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5909. uint16_t tmp = 0;
  5910. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5911. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5912. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5913. goto fail;
  5914. switch (vco) {
  5915. case 3200000:
  5916. div_table = div_3200;
  5917. break;
  5918. case 4000000:
  5919. div_table = div_4000;
  5920. break;
  5921. case 5333333:
  5922. div_table = div_5333;
  5923. break;
  5924. default:
  5925. goto fail;
  5926. }
  5927. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5928. fail:
  5929. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5930. return 200000;
  5931. }
  5932. static int g33_get_display_clock_speed(struct drm_device *dev)
  5933. {
  5934. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5935. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5936. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5937. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5938. const uint8_t *div_table;
  5939. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5940. uint16_t tmp = 0;
  5941. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5942. cdclk_sel = (tmp >> 4) & 0x7;
  5943. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5944. goto fail;
  5945. switch (vco) {
  5946. case 3200000:
  5947. div_table = div_3200;
  5948. break;
  5949. case 4000000:
  5950. div_table = div_4000;
  5951. break;
  5952. case 4800000:
  5953. div_table = div_4800;
  5954. break;
  5955. case 5333333:
  5956. div_table = div_5333;
  5957. break;
  5958. default:
  5959. goto fail;
  5960. }
  5961. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5962. fail:
  5963. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5964. return 190476;
  5965. }
  5966. static void
  5967. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5968. {
  5969. while (*num > DATA_LINK_M_N_MASK ||
  5970. *den > DATA_LINK_M_N_MASK) {
  5971. *num >>= 1;
  5972. *den >>= 1;
  5973. }
  5974. }
  5975. static void compute_m_n(unsigned int m, unsigned int n,
  5976. uint32_t *ret_m, uint32_t *ret_n)
  5977. {
  5978. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5979. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5980. intel_reduce_m_n_ratio(ret_m, ret_n);
  5981. }
  5982. void
  5983. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5984. int pixel_clock, int link_clock,
  5985. struct intel_link_m_n *m_n)
  5986. {
  5987. m_n->tu = 64;
  5988. compute_m_n(bits_per_pixel * pixel_clock,
  5989. link_clock * nlanes * 8,
  5990. &m_n->gmch_m, &m_n->gmch_n);
  5991. compute_m_n(pixel_clock, link_clock,
  5992. &m_n->link_m, &m_n->link_n);
  5993. }
  5994. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5995. {
  5996. if (i915.panel_use_ssc >= 0)
  5997. return i915.panel_use_ssc != 0;
  5998. return dev_priv->vbt.lvds_use_ssc
  5999. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6000. }
  6001. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6002. {
  6003. return (1 << dpll->n) << 16 | dpll->m2;
  6004. }
  6005. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6006. {
  6007. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6008. }
  6009. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6010. struct intel_crtc_state *crtc_state,
  6011. struct dpll *reduced_clock)
  6012. {
  6013. struct drm_device *dev = crtc->base.dev;
  6014. u32 fp, fp2 = 0;
  6015. if (IS_PINEVIEW(dev)) {
  6016. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6017. if (reduced_clock)
  6018. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6019. } else {
  6020. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6021. if (reduced_clock)
  6022. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6023. }
  6024. crtc_state->dpll_hw_state.fp0 = fp;
  6025. crtc->lowfreq_avail = false;
  6026. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6027. reduced_clock) {
  6028. crtc_state->dpll_hw_state.fp1 = fp2;
  6029. crtc->lowfreq_avail = true;
  6030. } else {
  6031. crtc_state->dpll_hw_state.fp1 = fp;
  6032. }
  6033. }
  6034. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6035. pipe)
  6036. {
  6037. u32 reg_val;
  6038. /*
  6039. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6040. * and set it to a reasonable value instead.
  6041. */
  6042. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6043. reg_val &= 0xffffff00;
  6044. reg_val |= 0x00000030;
  6045. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6046. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6047. reg_val &= 0x8cffffff;
  6048. reg_val = 0x8c000000;
  6049. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6050. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6051. reg_val &= 0xffffff00;
  6052. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6053. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6054. reg_val &= 0x00ffffff;
  6055. reg_val |= 0xb0000000;
  6056. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6057. }
  6058. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6059. struct intel_link_m_n *m_n)
  6060. {
  6061. struct drm_device *dev = crtc->base.dev;
  6062. struct drm_i915_private *dev_priv = dev->dev_private;
  6063. int pipe = crtc->pipe;
  6064. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6065. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6066. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6067. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6068. }
  6069. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6070. struct intel_link_m_n *m_n,
  6071. struct intel_link_m_n *m2_n2)
  6072. {
  6073. struct drm_device *dev = crtc->base.dev;
  6074. struct drm_i915_private *dev_priv = dev->dev_private;
  6075. int pipe = crtc->pipe;
  6076. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6077. if (INTEL_INFO(dev)->gen >= 5) {
  6078. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6079. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6080. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6081. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6082. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6083. * for gen < 8) and if DRRS is supported (to make sure the
  6084. * registers are not unnecessarily accessed).
  6085. */
  6086. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6087. crtc->config->has_drrs) {
  6088. I915_WRITE(PIPE_DATA_M2(transcoder),
  6089. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6090. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6091. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6092. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6093. }
  6094. } else {
  6095. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6096. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6097. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6098. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6099. }
  6100. }
  6101. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6102. {
  6103. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6104. if (m_n == M1_N1) {
  6105. dp_m_n = &crtc->config->dp_m_n;
  6106. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6107. } else if (m_n == M2_N2) {
  6108. /*
  6109. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6110. * needs to be programmed into M1_N1.
  6111. */
  6112. dp_m_n = &crtc->config->dp_m2_n2;
  6113. } else {
  6114. DRM_ERROR("Unsupported divider value\n");
  6115. return;
  6116. }
  6117. if (crtc->config->has_pch_encoder)
  6118. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6119. else
  6120. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6121. }
  6122. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6123. struct intel_crtc_state *pipe_config)
  6124. {
  6125. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6126. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6127. if (crtc->pipe != PIPE_A)
  6128. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6129. /* DPLL not used with DSI, but still need the rest set up */
  6130. if (!pipe_config->has_dsi_encoder)
  6131. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6132. DPLL_EXT_BUFFER_ENABLE_VLV;
  6133. pipe_config->dpll_hw_state.dpll_md =
  6134. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6135. }
  6136. static void chv_compute_dpll(struct intel_crtc *crtc,
  6137. struct intel_crtc_state *pipe_config)
  6138. {
  6139. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6140. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6141. if (crtc->pipe != PIPE_A)
  6142. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6143. /* DPLL not used with DSI, but still need the rest set up */
  6144. if (!pipe_config->has_dsi_encoder)
  6145. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6146. pipe_config->dpll_hw_state.dpll_md =
  6147. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6148. }
  6149. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6150. const struct intel_crtc_state *pipe_config)
  6151. {
  6152. struct drm_device *dev = crtc->base.dev;
  6153. struct drm_i915_private *dev_priv = dev->dev_private;
  6154. enum pipe pipe = crtc->pipe;
  6155. u32 mdiv;
  6156. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6157. u32 coreclk, reg_val;
  6158. /* Enable Refclk */
  6159. I915_WRITE(DPLL(pipe),
  6160. pipe_config->dpll_hw_state.dpll &
  6161. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  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. mutex_lock(&dev_priv->sb_lock);
  6166. bestn = pipe_config->dpll.n;
  6167. bestm1 = pipe_config->dpll.m1;
  6168. bestm2 = pipe_config->dpll.m2;
  6169. bestp1 = pipe_config->dpll.p1;
  6170. bestp2 = pipe_config->dpll.p2;
  6171. /* See eDP HDMI DPIO driver vbios notes doc */
  6172. /* PLL B needs special handling */
  6173. if (pipe == PIPE_B)
  6174. vlv_pllb_recal_opamp(dev_priv, pipe);
  6175. /* Set up Tx target for periodic Rcomp update */
  6176. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6177. /* Disable target IRef on PLL */
  6178. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6179. reg_val &= 0x00ffffff;
  6180. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6181. /* Disable fast lock */
  6182. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6183. /* Set idtafcrecal before PLL is enabled */
  6184. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6185. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6186. mdiv |= ((bestn << DPIO_N_SHIFT));
  6187. mdiv |= (1 << DPIO_K_SHIFT);
  6188. /*
  6189. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6190. * but we don't support that).
  6191. * Note: don't use the DAC post divider as it seems unstable.
  6192. */
  6193. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6194. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6195. mdiv |= DPIO_ENABLE_CALIBRATION;
  6196. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6197. /* Set HBR and RBR LPF coefficients */
  6198. if (pipe_config->port_clock == 162000 ||
  6199. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6200. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6201. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6202. 0x009f0003);
  6203. else
  6204. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6205. 0x00d0000f);
  6206. if (pipe_config->has_dp_encoder) {
  6207. /* Use SSC source */
  6208. if (pipe == PIPE_A)
  6209. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6210. 0x0df40000);
  6211. else
  6212. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6213. 0x0df70000);
  6214. } else { /* HDMI or VGA */
  6215. /* Use bend source */
  6216. if (pipe == PIPE_A)
  6217. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6218. 0x0df70000);
  6219. else
  6220. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6221. 0x0df40000);
  6222. }
  6223. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6224. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6225. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6226. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6227. coreclk |= 0x01000000;
  6228. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6229. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6230. mutex_unlock(&dev_priv->sb_lock);
  6231. }
  6232. static void chv_prepare_pll(struct intel_crtc *crtc,
  6233. const struct intel_crtc_state *pipe_config)
  6234. {
  6235. struct drm_device *dev = crtc->base.dev;
  6236. struct drm_i915_private *dev_priv = dev->dev_private;
  6237. enum pipe pipe = crtc->pipe;
  6238. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6239. u32 loopfilter, tribuf_calcntr;
  6240. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6241. u32 dpio_val;
  6242. int vco;
  6243. /* Enable Refclk and SSC */
  6244. I915_WRITE(DPLL(pipe),
  6245. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6246. /* No need to actually set up the DPLL with DSI */
  6247. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6248. return;
  6249. bestn = pipe_config->dpll.n;
  6250. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6251. bestm1 = pipe_config->dpll.m1;
  6252. bestm2 = pipe_config->dpll.m2 >> 22;
  6253. bestp1 = pipe_config->dpll.p1;
  6254. bestp2 = pipe_config->dpll.p2;
  6255. vco = pipe_config->dpll.vco;
  6256. dpio_val = 0;
  6257. loopfilter = 0;
  6258. mutex_lock(&dev_priv->sb_lock);
  6259. /* p1 and p2 divider */
  6260. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6261. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6262. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6263. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6264. 1 << DPIO_CHV_K_DIV_SHIFT);
  6265. /* Feedback post-divider - m2 */
  6266. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6267. /* Feedback refclk divider - n and m1 */
  6268. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6269. DPIO_CHV_M1_DIV_BY_2 |
  6270. 1 << DPIO_CHV_N_DIV_SHIFT);
  6271. /* M2 fraction division */
  6272. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6273. /* M2 fraction division enable */
  6274. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6275. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6276. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6277. if (bestm2_frac)
  6278. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6279. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6280. /* Program digital lock detect threshold */
  6281. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6282. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6283. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6284. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6285. if (!bestm2_frac)
  6286. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6287. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6288. /* Loop filter */
  6289. if (vco == 5400000) {
  6290. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6291. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6292. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6293. tribuf_calcntr = 0x9;
  6294. } else if (vco <= 6200000) {
  6295. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6296. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6297. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6298. tribuf_calcntr = 0x9;
  6299. } else if (vco <= 6480000) {
  6300. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6301. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6302. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6303. tribuf_calcntr = 0x8;
  6304. } else {
  6305. /* Not supported. Apply the same limits as in the max case */
  6306. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6307. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6308. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6309. tribuf_calcntr = 0;
  6310. }
  6311. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6312. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6313. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6314. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6315. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6316. /* AFC Recal */
  6317. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6318. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6319. DPIO_AFC_RECAL);
  6320. mutex_unlock(&dev_priv->sb_lock);
  6321. }
  6322. /**
  6323. * vlv_force_pll_on - forcibly enable just the PLL
  6324. * @dev_priv: i915 private structure
  6325. * @pipe: pipe PLL to enable
  6326. * @dpll: PLL configuration
  6327. *
  6328. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6329. * in cases where we need the PLL enabled even when @pipe is not going to
  6330. * be enabled.
  6331. */
  6332. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6333. const struct dpll *dpll)
  6334. {
  6335. struct intel_crtc *crtc =
  6336. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6337. struct intel_crtc_state *pipe_config;
  6338. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6339. if (!pipe_config)
  6340. return -ENOMEM;
  6341. pipe_config->base.crtc = &crtc->base;
  6342. pipe_config->pixel_multiplier = 1;
  6343. pipe_config->dpll = *dpll;
  6344. if (IS_CHERRYVIEW(dev)) {
  6345. chv_compute_dpll(crtc, pipe_config);
  6346. chv_prepare_pll(crtc, pipe_config);
  6347. chv_enable_pll(crtc, pipe_config);
  6348. } else {
  6349. vlv_compute_dpll(crtc, pipe_config);
  6350. vlv_prepare_pll(crtc, pipe_config);
  6351. vlv_enable_pll(crtc, pipe_config);
  6352. }
  6353. kfree(pipe_config);
  6354. return 0;
  6355. }
  6356. /**
  6357. * vlv_force_pll_off - forcibly disable just the PLL
  6358. * @dev_priv: i915 private structure
  6359. * @pipe: pipe PLL to disable
  6360. *
  6361. * Disable the PLL for @pipe. To be used in cases where we need
  6362. * the PLL enabled even when @pipe is not going to be enabled.
  6363. */
  6364. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6365. {
  6366. if (IS_CHERRYVIEW(dev))
  6367. chv_disable_pll(to_i915(dev), pipe);
  6368. else
  6369. vlv_disable_pll(to_i915(dev), pipe);
  6370. }
  6371. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6372. struct intel_crtc_state *crtc_state,
  6373. struct dpll *reduced_clock)
  6374. {
  6375. struct drm_device *dev = crtc->base.dev;
  6376. struct drm_i915_private *dev_priv = dev->dev_private;
  6377. u32 dpll;
  6378. bool is_sdvo;
  6379. struct dpll *clock = &crtc_state->dpll;
  6380. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6381. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6382. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6383. dpll = DPLL_VGA_MODE_DIS;
  6384. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6385. dpll |= DPLLB_MODE_LVDS;
  6386. else
  6387. dpll |= DPLLB_MODE_DAC_SERIAL;
  6388. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6389. dpll |= (crtc_state->pixel_multiplier - 1)
  6390. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6391. }
  6392. if (is_sdvo)
  6393. dpll |= DPLL_SDVO_HIGH_SPEED;
  6394. if (crtc_state->has_dp_encoder)
  6395. dpll |= DPLL_SDVO_HIGH_SPEED;
  6396. /* compute bitmask from p1 value */
  6397. if (IS_PINEVIEW(dev))
  6398. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6399. else {
  6400. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6401. if (IS_G4X(dev) && reduced_clock)
  6402. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6403. }
  6404. switch (clock->p2) {
  6405. case 5:
  6406. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6407. break;
  6408. case 7:
  6409. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6410. break;
  6411. case 10:
  6412. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6413. break;
  6414. case 14:
  6415. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6416. break;
  6417. }
  6418. if (INTEL_INFO(dev)->gen >= 4)
  6419. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6420. if (crtc_state->sdvo_tv_clock)
  6421. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6422. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6423. intel_panel_use_ssc(dev_priv))
  6424. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6425. else
  6426. dpll |= PLL_REF_INPUT_DREFCLK;
  6427. dpll |= DPLL_VCO_ENABLE;
  6428. crtc_state->dpll_hw_state.dpll = dpll;
  6429. if (INTEL_INFO(dev)->gen >= 4) {
  6430. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6431. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6432. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6433. }
  6434. }
  6435. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6436. struct intel_crtc_state *crtc_state,
  6437. struct dpll *reduced_clock)
  6438. {
  6439. struct drm_device *dev = crtc->base.dev;
  6440. struct drm_i915_private *dev_priv = dev->dev_private;
  6441. u32 dpll;
  6442. struct dpll *clock = &crtc_state->dpll;
  6443. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6444. dpll = DPLL_VGA_MODE_DIS;
  6445. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6446. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6447. } else {
  6448. if (clock->p1 == 2)
  6449. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6450. else
  6451. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6452. if (clock->p2 == 4)
  6453. dpll |= PLL_P2_DIVIDE_BY_4;
  6454. }
  6455. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6456. dpll |= DPLL_DVO_2X_MODE;
  6457. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6458. intel_panel_use_ssc(dev_priv))
  6459. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6460. else
  6461. dpll |= PLL_REF_INPUT_DREFCLK;
  6462. dpll |= DPLL_VCO_ENABLE;
  6463. crtc_state->dpll_hw_state.dpll = dpll;
  6464. }
  6465. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6466. {
  6467. struct drm_device *dev = intel_crtc->base.dev;
  6468. struct drm_i915_private *dev_priv = dev->dev_private;
  6469. enum pipe pipe = intel_crtc->pipe;
  6470. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6471. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6472. uint32_t crtc_vtotal, crtc_vblank_end;
  6473. int vsyncshift = 0;
  6474. /* We need to be careful not to changed the adjusted mode, for otherwise
  6475. * the hw state checker will get angry at the mismatch. */
  6476. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6477. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6478. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6479. /* the chip adds 2 halflines automatically */
  6480. crtc_vtotal -= 1;
  6481. crtc_vblank_end -= 1;
  6482. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6483. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6484. else
  6485. vsyncshift = adjusted_mode->crtc_hsync_start -
  6486. adjusted_mode->crtc_htotal / 2;
  6487. if (vsyncshift < 0)
  6488. vsyncshift += adjusted_mode->crtc_htotal;
  6489. }
  6490. if (INTEL_INFO(dev)->gen > 3)
  6491. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6492. I915_WRITE(HTOTAL(cpu_transcoder),
  6493. (adjusted_mode->crtc_hdisplay - 1) |
  6494. ((adjusted_mode->crtc_htotal - 1) << 16));
  6495. I915_WRITE(HBLANK(cpu_transcoder),
  6496. (adjusted_mode->crtc_hblank_start - 1) |
  6497. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6498. I915_WRITE(HSYNC(cpu_transcoder),
  6499. (adjusted_mode->crtc_hsync_start - 1) |
  6500. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6501. I915_WRITE(VTOTAL(cpu_transcoder),
  6502. (adjusted_mode->crtc_vdisplay - 1) |
  6503. ((crtc_vtotal - 1) << 16));
  6504. I915_WRITE(VBLANK(cpu_transcoder),
  6505. (adjusted_mode->crtc_vblank_start - 1) |
  6506. ((crtc_vblank_end - 1) << 16));
  6507. I915_WRITE(VSYNC(cpu_transcoder),
  6508. (adjusted_mode->crtc_vsync_start - 1) |
  6509. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6510. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6511. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6512. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6513. * bits. */
  6514. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6515. (pipe == PIPE_B || pipe == PIPE_C))
  6516. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6517. }
  6518. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6519. {
  6520. struct drm_device *dev = intel_crtc->base.dev;
  6521. struct drm_i915_private *dev_priv = dev->dev_private;
  6522. enum pipe pipe = intel_crtc->pipe;
  6523. /* pipesrc controls the size that is scaled from, which should
  6524. * always be the user's requested size.
  6525. */
  6526. I915_WRITE(PIPESRC(pipe),
  6527. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6528. (intel_crtc->config->pipe_src_h - 1));
  6529. }
  6530. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6531. struct intel_crtc_state *pipe_config)
  6532. {
  6533. struct drm_device *dev = crtc->base.dev;
  6534. struct drm_i915_private *dev_priv = dev->dev_private;
  6535. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6536. uint32_t tmp;
  6537. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6538. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6539. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6540. tmp = I915_READ(HBLANK(cpu_transcoder));
  6541. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6542. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6543. tmp = I915_READ(HSYNC(cpu_transcoder));
  6544. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6545. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6546. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6547. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6548. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6549. tmp = I915_READ(VBLANK(cpu_transcoder));
  6550. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6551. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6552. tmp = I915_READ(VSYNC(cpu_transcoder));
  6553. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6554. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6555. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6556. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6557. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6558. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6559. }
  6560. }
  6561. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6562. struct intel_crtc_state *pipe_config)
  6563. {
  6564. struct drm_device *dev = crtc->base.dev;
  6565. struct drm_i915_private *dev_priv = dev->dev_private;
  6566. u32 tmp;
  6567. tmp = I915_READ(PIPESRC(crtc->pipe));
  6568. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6569. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6570. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6571. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6572. }
  6573. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6574. struct intel_crtc_state *pipe_config)
  6575. {
  6576. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6577. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6578. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6579. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6580. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6581. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6582. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6583. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6584. mode->flags = pipe_config->base.adjusted_mode.flags;
  6585. mode->type = DRM_MODE_TYPE_DRIVER;
  6586. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6587. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6588. mode->hsync = drm_mode_hsync(mode);
  6589. mode->vrefresh = drm_mode_vrefresh(mode);
  6590. drm_mode_set_name(mode);
  6591. }
  6592. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6593. {
  6594. struct drm_device *dev = intel_crtc->base.dev;
  6595. struct drm_i915_private *dev_priv = dev->dev_private;
  6596. uint32_t pipeconf;
  6597. pipeconf = 0;
  6598. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6599. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6600. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6601. if (intel_crtc->config->double_wide)
  6602. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6603. /* only g4x and later have fancy bpc/dither controls */
  6604. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6605. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6606. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6607. pipeconf |= PIPECONF_DITHER_EN |
  6608. PIPECONF_DITHER_TYPE_SP;
  6609. switch (intel_crtc->config->pipe_bpp) {
  6610. case 18:
  6611. pipeconf |= PIPECONF_6BPC;
  6612. break;
  6613. case 24:
  6614. pipeconf |= PIPECONF_8BPC;
  6615. break;
  6616. case 30:
  6617. pipeconf |= PIPECONF_10BPC;
  6618. break;
  6619. default:
  6620. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6621. BUG();
  6622. }
  6623. }
  6624. if (HAS_PIPE_CXSR(dev)) {
  6625. if (intel_crtc->lowfreq_avail) {
  6626. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6627. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6628. } else {
  6629. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6630. }
  6631. }
  6632. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6633. if (INTEL_INFO(dev)->gen < 4 ||
  6634. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6635. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6636. else
  6637. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6638. } else
  6639. pipeconf |= PIPECONF_PROGRESSIVE;
  6640. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6641. intel_crtc->config->limited_color_range)
  6642. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6643. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6644. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6645. }
  6646. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6647. struct intel_crtc_state *crtc_state)
  6648. {
  6649. struct drm_device *dev = crtc->base.dev;
  6650. struct drm_i915_private *dev_priv = dev->dev_private;
  6651. const struct intel_limit *limit;
  6652. int refclk = 48000;
  6653. memset(&crtc_state->dpll_hw_state, 0,
  6654. sizeof(crtc_state->dpll_hw_state));
  6655. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6656. if (intel_panel_use_ssc(dev_priv)) {
  6657. refclk = dev_priv->vbt.lvds_ssc_freq;
  6658. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6659. }
  6660. limit = &intel_limits_i8xx_lvds;
  6661. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6662. limit = &intel_limits_i8xx_dvo;
  6663. } else {
  6664. limit = &intel_limits_i8xx_dac;
  6665. }
  6666. if (!crtc_state->clock_set &&
  6667. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6668. refclk, NULL, &crtc_state->dpll)) {
  6669. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6670. return -EINVAL;
  6671. }
  6672. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6673. return 0;
  6674. }
  6675. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6676. struct intel_crtc_state *crtc_state)
  6677. {
  6678. struct drm_device *dev = crtc->base.dev;
  6679. struct drm_i915_private *dev_priv = dev->dev_private;
  6680. const struct intel_limit *limit;
  6681. int refclk = 96000;
  6682. memset(&crtc_state->dpll_hw_state, 0,
  6683. sizeof(crtc_state->dpll_hw_state));
  6684. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6685. if (intel_panel_use_ssc(dev_priv)) {
  6686. refclk = dev_priv->vbt.lvds_ssc_freq;
  6687. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6688. }
  6689. if (intel_is_dual_link_lvds(dev))
  6690. limit = &intel_limits_g4x_dual_channel_lvds;
  6691. else
  6692. limit = &intel_limits_g4x_single_channel_lvds;
  6693. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6694. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6695. limit = &intel_limits_g4x_hdmi;
  6696. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6697. limit = &intel_limits_g4x_sdvo;
  6698. } else {
  6699. /* The option is for other outputs */
  6700. limit = &intel_limits_i9xx_sdvo;
  6701. }
  6702. if (!crtc_state->clock_set &&
  6703. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6704. refclk, NULL, &crtc_state->dpll)) {
  6705. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6706. return -EINVAL;
  6707. }
  6708. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6709. return 0;
  6710. }
  6711. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6712. struct intel_crtc_state *crtc_state)
  6713. {
  6714. struct drm_device *dev = crtc->base.dev;
  6715. struct drm_i915_private *dev_priv = dev->dev_private;
  6716. const struct intel_limit *limit;
  6717. int refclk = 96000;
  6718. memset(&crtc_state->dpll_hw_state, 0,
  6719. sizeof(crtc_state->dpll_hw_state));
  6720. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6721. if (intel_panel_use_ssc(dev_priv)) {
  6722. refclk = dev_priv->vbt.lvds_ssc_freq;
  6723. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6724. }
  6725. limit = &intel_limits_pineview_lvds;
  6726. } else {
  6727. limit = &intel_limits_pineview_sdvo;
  6728. }
  6729. if (!crtc_state->clock_set &&
  6730. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6731. refclk, NULL, &crtc_state->dpll)) {
  6732. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6733. return -EINVAL;
  6734. }
  6735. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6736. return 0;
  6737. }
  6738. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6739. struct intel_crtc_state *crtc_state)
  6740. {
  6741. struct drm_device *dev = crtc->base.dev;
  6742. struct drm_i915_private *dev_priv = dev->dev_private;
  6743. const struct intel_limit *limit;
  6744. int refclk = 96000;
  6745. memset(&crtc_state->dpll_hw_state, 0,
  6746. sizeof(crtc_state->dpll_hw_state));
  6747. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6748. if (intel_panel_use_ssc(dev_priv)) {
  6749. refclk = dev_priv->vbt.lvds_ssc_freq;
  6750. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6751. }
  6752. limit = &intel_limits_i9xx_lvds;
  6753. } else {
  6754. limit = &intel_limits_i9xx_sdvo;
  6755. }
  6756. if (!crtc_state->clock_set &&
  6757. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6758. refclk, NULL, &crtc_state->dpll)) {
  6759. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6760. return -EINVAL;
  6761. }
  6762. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6763. return 0;
  6764. }
  6765. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6766. struct intel_crtc_state *crtc_state)
  6767. {
  6768. int refclk = 100000;
  6769. const struct intel_limit *limit = &intel_limits_chv;
  6770. memset(&crtc_state->dpll_hw_state, 0,
  6771. sizeof(crtc_state->dpll_hw_state));
  6772. if (!crtc_state->clock_set &&
  6773. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6774. refclk, NULL, &crtc_state->dpll)) {
  6775. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6776. return -EINVAL;
  6777. }
  6778. chv_compute_dpll(crtc, crtc_state);
  6779. return 0;
  6780. }
  6781. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6782. struct intel_crtc_state *crtc_state)
  6783. {
  6784. int refclk = 100000;
  6785. const struct intel_limit *limit = &intel_limits_vlv;
  6786. memset(&crtc_state->dpll_hw_state, 0,
  6787. sizeof(crtc_state->dpll_hw_state));
  6788. if (!crtc_state->clock_set &&
  6789. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6790. refclk, NULL, &crtc_state->dpll)) {
  6791. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6792. return -EINVAL;
  6793. }
  6794. vlv_compute_dpll(crtc, crtc_state);
  6795. return 0;
  6796. }
  6797. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6798. struct intel_crtc_state *pipe_config)
  6799. {
  6800. struct drm_device *dev = crtc->base.dev;
  6801. struct drm_i915_private *dev_priv = dev->dev_private;
  6802. uint32_t tmp;
  6803. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6804. return;
  6805. tmp = I915_READ(PFIT_CONTROL);
  6806. if (!(tmp & PFIT_ENABLE))
  6807. return;
  6808. /* Check whether the pfit is attached to our pipe. */
  6809. if (INTEL_INFO(dev)->gen < 4) {
  6810. if (crtc->pipe != PIPE_B)
  6811. return;
  6812. } else {
  6813. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6814. return;
  6815. }
  6816. pipe_config->gmch_pfit.control = tmp;
  6817. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6818. }
  6819. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6820. struct intel_crtc_state *pipe_config)
  6821. {
  6822. struct drm_device *dev = crtc->base.dev;
  6823. struct drm_i915_private *dev_priv = dev->dev_private;
  6824. int pipe = pipe_config->cpu_transcoder;
  6825. struct dpll clock;
  6826. u32 mdiv;
  6827. int refclk = 100000;
  6828. /* In case of DSI, DPLL will not be used */
  6829. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6830. return;
  6831. mutex_lock(&dev_priv->sb_lock);
  6832. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6833. mutex_unlock(&dev_priv->sb_lock);
  6834. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6835. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6836. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6837. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6838. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6839. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6840. }
  6841. static void
  6842. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6843. struct intel_initial_plane_config *plane_config)
  6844. {
  6845. struct drm_device *dev = crtc->base.dev;
  6846. struct drm_i915_private *dev_priv = dev->dev_private;
  6847. u32 val, base, offset;
  6848. int pipe = crtc->pipe, plane = crtc->plane;
  6849. int fourcc, pixel_format;
  6850. unsigned int aligned_height;
  6851. struct drm_framebuffer *fb;
  6852. struct intel_framebuffer *intel_fb;
  6853. val = I915_READ(DSPCNTR(plane));
  6854. if (!(val & DISPLAY_PLANE_ENABLE))
  6855. return;
  6856. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6857. if (!intel_fb) {
  6858. DRM_DEBUG_KMS("failed to alloc fb\n");
  6859. return;
  6860. }
  6861. fb = &intel_fb->base;
  6862. if (INTEL_INFO(dev)->gen >= 4) {
  6863. if (val & DISPPLANE_TILED) {
  6864. plane_config->tiling = I915_TILING_X;
  6865. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6866. }
  6867. }
  6868. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6869. fourcc = i9xx_format_to_fourcc(pixel_format);
  6870. fb->pixel_format = fourcc;
  6871. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6872. if (INTEL_INFO(dev)->gen >= 4) {
  6873. if (plane_config->tiling)
  6874. offset = I915_READ(DSPTILEOFF(plane));
  6875. else
  6876. offset = I915_READ(DSPLINOFF(plane));
  6877. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6878. } else {
  6879. base = I915_READ(DSPADDR(plane));
  6880. }
  6881. plane_config->base = base;
  6882. val = I915_READ(PIPESRC(pipe));
  6883. fb->width = ((val >> 16) & 0xfff) + 1;
  6884. fb->height = ((val >> 0) & 0xfff) + 1;
  6885. val = I915_READ(DSPSTRIDE(pipe));
  6886. fb->pitches[0] = val & 0xffffffc0;
  6887. aligned_height = intel_fb_align_height(dev, fb->height,
  6888. fb->pixel_format,
  6889. fb->modifier[0]);
  6890. plane_config->size = fb->pitches[0] * aligned_height;
  6891. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6892. pipe_name(pipe), plane, fb->width, fb->height,
  6893. fb->bits_per_pixel, base, fb->pitches[0],
  6894. plane_config->size);
  6895. plane_config->fb = intel_fb;
  6896. }
  6897. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6898. struct intel_crtc_state *pipe_config)
  6899. {
  6900. struct drm_device *dev = crtc->base.dev;
  6901. struct drm_i915_private *dev_priv = dev->dev_private;
  6902. int pipe = pipe_config->cpu_transcoder;
  6903. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6904. struct dpll clock;
  6905. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6906. int refclk = 100000;
  6907. /* In case of DSI, DPLL will not be used */
  6908. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6909. return;
  6910. mutex_lock(&dev_priv->sb_lock);
  6911. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6912. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6913. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6914. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6915. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6916. mutex_unlock(&dev_priv->sb_lock);
  6917. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6918. clock.m2 = (pll_dw0 & 0xff) << 22;
  6919. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6920. clock.m2 |= pll_dw2 & 0x3fffff;
  6921. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6922. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6923. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6924. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6925. }
  6926. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6927. struct intel_crtc_state *pipe_config)
  6928. {
  6929. struct drm_device *dev = crtc->base.dev;
  6930. struct drm_i915_private *dev_priv = dev->dev_private;
  6931. enum intel_display_power_domain power_domain;
  6932. uint32_t tmp;
  6933. bool ret;
  6934. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6935. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6936. return false;
  6937. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6938. pipe_config->shared_dpll = NULL;
  6939. ret = false;
  6940. tmp = I915_READ(PIPECONF(crtc->pipe));
  6941. if (!(tmp & PIPECONF_ENABLE))
  6942. goto out;
  6943. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6944. switch (tmp & PIPECONF_BPC_MASK) {
  6945. case PIPECONF_6BPC:
  6946. pipe_config->pipe_bpp = 18;
  6947. break;
  6948. case PIPECONF_8BPC:
  6949. pipe_config->pipe_bpp = 24;
  6950. break;
  6951. case PIPECONF_10BPC:
  6952. pipe_config->pipe_bpp = 30;
  6953. break;
  6954. default:
  6955. break;
  6956. }
  6957. }
  6958. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6959. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6960. pipe_config->limited_color_range = true;
  6961. if (INTEL_INFO(dev)->gen < 4)
  6962. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6963. intel_get_pipe_timings(crtc, pipe_config);
  6964. intel_get_pipe_src_size(crtc, pipe_config);
  6965. i9xx_get_pfit_config(crtc, pipe_config);
  6966. if (INTEL_INFO(dev)->gen >= 4) {
  6967. /* No way to read it out on pipes B and C */
  6968. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6969. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6970. else
  6971. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6972. pipe_config->pixel_multiplier =
  6973. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6974. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6975. pipe_config->dpll_hw_state.dpll_md = tmp;
  6976. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6977. tmp = I915_READ(DPLL(crtc->pipe));
  6978. pipe_config->pixel_multiplier =
  6979. ((tmp & SDVO_MULTIPLIER_MASK)
  6980. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6981. } else {
  6982. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6983. * port and will be fixed up in the encoder->get_config
  6984. * function. */
  6985. pipe_config->pixel_multiplier = 1;
  6986. }
  6987. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6988. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6989. /*
  6990. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6991. * on 830. Filter it out here so that we don't
  6992. * report errors due to that.
  6993. */
  6994. if (IS_I830(dev))
  6995. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6996. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6997. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6998. } else {
  6999. /* Mask out read-only status bits. */
  7000. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7001. DPLL_PORTC_READY_MASK |
  7002. DPLL_PORTB_READY_MASK);
  7003. }
  7004. if (IS_CHERRYVIEW(dev))
  7005. chv_crtc_clock_get(crtc, pipe_config);
  7006. else if (IS_VALLEYVIEW(dev))
  7007. vlv_crtc_clock_get(crtc, pipe_config);
  7008. else
  7009. i9xx_crtc_clock_get(crtc, pipe_config);
  7010. /*
  7011. * Normally the dotclock is filled in by the encoder .get_config()
  7012. * but in case the pipe is enabled w/o any ports we need a sane
  7013. * default.
  7014. */
  7015. pipe_config->base.adjusted_mode.crtc_clock =
  7016. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7017. ret = true;
  7018. out:
  7019. intel_display_power_put(dev_priv, power_domain);
  7020. return ret;
  7021. }
  7022. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7023. {
  7024. struct drm_i915_private *dev_priv = dev->dev_private;
  7025. struct intel_encoder *encoder;
  7026. int i;
  7027. u32 val, final;
  7028. bool has_lvds = false;
  7029. bool has_cpu_edp = false;
  7030. bool has_panel = false;
  7031. bool has_ck505 = false;
  7032. bool can_ssc = false;
  7033. bool using_ssc_source = false;
  7034. /* We need to take the global config into account */
  7035. for_each_intel_encoder(dev, encoder) {
  7036. switch (encoder->type) {
  7037. case INTEL_OUTPUT_LVDS:
  7038. has_panel = true;
  7039. has_lvds = true;
  7040. break;
  7041. case INTEL_OUTPUT_EDP:
  7042. has_panel = true;
  7043. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7044. has_cpu_edp = true;
  7045. break;
  7046. default:
  7047. break;
  7048. }
  7049. }
  7050. if (HAS_PCH_IBX(dev)) {
  7051. has_ck505 = dev_priv->vbt.display_clock_mode;
  7052. can_ssc = has_ck505;
  7053. } else {
  7054. has_ck505 = false;
  7055. can_ssc = true;
  7056. }
  7057. /* Check if any DPLLs are using the SSC source */
  7058. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7059. u32 temp = I915_READ(PCH_DPLL(i));
  7060. if (!(temp & DPLL_VCO_ENABLE))
  7061. continue;
  7062. if ((temp & PLL_REF_INPUT_MASK) ==
  7063. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7064. using_ssc_source = true;
  7065. break;
  7066. }
  7067. }
  7068. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7069. has_panel, has_lvds, has_ck505, using_ssc_source);
  7070. /* Ironlake: try to setup display ref clock before DPLL
  7071. * enabling. This is only under driver's control after
  7072. * PCH B stepping, previous chipset stepping should be
  7073. * ignoring this setting.
  7074. */
  7075. val = I915_READ(PCH_DREF_CONTROL);
  7076. /* As we must carefully and slowly disable/enable each source in turn,
  7077. * compute the final state we want first and check if we need to
  7078. * make any changes at all.
  7079. */
  7080. final = val;
  7081. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7082. if (has_ck505)
  7083. final |= DREF_NONSPREAD_CK505_ENABLE;
  7084. else
  7085. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7086. final &= ~DREF_SSC_SOURCE_MASK;
  7087. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7088. final &= ~DREF_SSC1_ENABLE;
  7089. if (has_panel) {
  7090. final |= DREF_SSC_SOURCE_ENABLE;
  7091. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7092. final |= DREF_SSC1_ENABLE;
  7093. if (has_cpu_edp) {
  7094. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7095. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7096. else
  7097. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7098. } else
  7099. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7100. } else if (using_ssc_source) {
  7101. final |= DREF_SSC_SOURCE_ENABLE;
  7102. final |= DREF_SSC1_ENABLE;
  7103. }
  7104. if (final == val)
  7105. return;
  7106. /* Always enable nonspread source */
  7107. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7108. if (has_ck505)
  7109. val |= DREF_NONSPREAD_CK505_ENABLE;
  7110. else
  7111. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7112. if (has_panel) {
  7113. val &= ~DREF_SSC_SOURCE_MASK;
  7114. val |= DREF_SSC_SOURCE_ENABLE;
  7115. /* SSC must be turned on before enabling the CPU output */
  7116. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7117. DRM_DEBUG_KMS("Using SSC on panel\n");
  7118. val |= DREF_SSC1_ENABLE;
  7119. } else
  7120. val &= ~DREF_SSC1_ENABLE;
  7121. /* Get SSC going before enabling the outputs */
  7122. I915_WRITE(PCH_DREF_CONTROL, val);
  7123. POSTING_READ(PCH_DREF_CONTROL);
  7124. udelay(200);
  7125. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7126. /* Enable CPU source on CPU attached eDP */
  7127. if (has_cpu_edp) {
  7128. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7129. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7130. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7131. } else
  7132. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7133. } else
  7134. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7135. I915_WRITE(PCH_DREF_CONTROL, val);
  7136. POSTING_READ(PCH_DREF_CONTROL);
  7137. udelay(200);
  7138. } else {
  7139. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7140. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7141. /* Turn off CPU output */
  7142. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7143. I915_WRITE(PCH_DREF_CONTROL, val);
  7144. POSTING_READ(PCH_DREF_CONTROL);
  7145. udelay(200);
  7146. if (!using_ssc_source) {
  7147. DRM_DEBUG_KMS("Disabling SSC source\n");
  7148. /* Turn off the SSC source */
  7149. val &= ~DREF_SSC_SOURCE_MASK;
  7150. val |= DREF_SSC_SOURCE_DISABLE;
  7151. /* Turn off SSC1 */
  7152. val &= ~DREF_SSC1_ENABLE;
  7153. I915_WRITE(PCH_DREF_CONTROL, val);
  7154. POSTING_READ(PCH_DREF_CONTROL);
  7155. udelay(200);
  7156. }
  7157. }
  7158. BUG_ON(val != final);
  7159. }
  7160. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7161. {
  7162. uint32_t tmp;
  7163. tmp = I915_READ(SOUTH_CHICKEN2);
  7164. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7165. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7166. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7167. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7168. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7169. tmp = I915_READ(SOUTH_CHICKEN2);
  7170. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7171. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7172. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7173. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7174. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7175. }
  7176. /* WaMPhyProgramming:hsw */
  7177. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7178. {
  7179. uint32_t tmp;
  7180. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7181. tmp &= ~(0xFF << 24);
  7182. tmp |= (0x12 << 24);
  7183. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7184. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7185. tmp |= (1 << 11);
  7186. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7187. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7188. tmp |= (1 << 11);
  7189. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7190. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7191. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7192. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7193. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7194. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7195. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7196. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7197. tmp &= ~(7 << 13);
  7198. tmp |= (5 << 13);
  7199. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7200. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7201. tmp &= ~(7 << 13);
  7202. tmp |= (5 << 13);
  7203. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7204. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7205. tmp &= ~0xFF;
  7206. tmp |= 0x1C;
  7207. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7208. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7209. tmp &= ~0xFF;
  7210. tmp |= 0x1C;
  7211. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7212. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7213. tmp &= ~(0xFF << 16);
  7214. tmp |= (0x1C << 16);
  7215. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7216. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7217. tmp &= ~(0xFF << 16);
  7218. tmp |= (0x1C << 16);
  7219. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7220. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7221. tmp |= (1 << 27);
  7222. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7223. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7224. tmp |= (1 << 27);
  7225. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7226. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7227. tmp &= ~(0xF << 28);
  7228. tmp |= (4 << 28);
  7229. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7230. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7231. tmp &= ~(0xF << 28);
  7232. tmp |= (4 << 28);
  7233. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7234. }
  7235. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7236. * Programming" based on the parameters passed:
  7237. * - Sequence to enable CLKOUT_DP
  7238. * - Sequence to enable CLKOUT_DP without spread
  7239. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7240. */
  7241. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7242. bool with_fdi)
  7243. {
  7244. struct drm_i915_private *dev_priv = dev->dev_private;
  7245. uint32_t reg, tmp;
  7246. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7247. with_spread = true;
  7248. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7249. with_fdi = false;
  7250. mutex_lock(&dev_priv->sb_lock);
  7251. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7252. tmp &= ~SBI_SSCCTL_DISABLE;
  7253. tmp |= SBI_SSCCTL_PATHALT;
  7254. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7255. udelay(24);
  7256. if (with_spread) {
  7257. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7258. tmp &= ~SBI_SSCCTL_PATHALT;
  7259. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7260. if (with_fdi) {
  7261. lpt_reset_fdi_mphy(dev_priv);
  7262. lpt_program_fdi_mphy(dev_priv);
  7263. }
  7264. }
  7265. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7266. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7267. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7268. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7269. mutex_unlock(&dev_priv->sb_lock);
  7270. }
  7271. /* Sequence to disable CLKOUT_DP */
  7272. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7273. {
  7274. struct drm_i915_private *dev_priv = dev->dev_private;
  7275. uint32_t reg, tmp;
  7276. mutex_lock(&dev_priv->sb_lock);
  7277. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7278. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7279. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7280. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7281. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7282. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7283. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7284. tmp |= SBI_SSCCTL_PATHALT;
  7285. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7286. udelay(32);
  7287. }
  7288. tmp |= SBI_SSCCTL_DISABLE;
  7289. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7290. }
  7291. mutex_unlock(&dev_priv->sb_lock);
  7292. }
  7293. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7294. static const uint16_t sscdivintphase[] = {
  7295. [BEND_IDX( 50)] = 0x3B23,
  7296. [BEND_IDX( 45)] = 0x3B23,
  7297. [BEND_IDX( 40)] = 0x3C23,
  7298. [BEND_IDX( 35)] = 0x3C23,
  7299. [BEND_IDX( 30)] = 0x3D23,
  7300. [BEND_IDX( 25)] = 0x3D23,
  7301. [BEND_IDX( 20)] = 0x3E23,
  7302. [BEND_IDX( 15)] = 0x3E23,
  7303. [BEND_IDX( 10)] = 0x3F23,
  7304. [BEND_IDX( 5)] = 0x3F23,
  7305. [BEND_IDX( 0)] = 0x0025,
  7306. [BEND_IDX( -5)] = 0x0025,
  7307. [BEND_IDX(-10)] = 0x0125,
  7308. [BEND_IDX(-15)] = 0x0125,
  7309. [BEND_IDX(-20)] = 0x0225,
  7310. [BEND_IDX(-25)] = 0x0225,
  7311. [BEND_IDX(-30)] = 0x0325,
  7312. [BEND_IDX(-35)] = 0x0325,
  7313. [BEND_IDX(-40)] = 0x0425,
  7314. [BEND_IDX(-45)] = 0x0425,
  7315. [BEND_IDX(-50)] = 0x0525,
  7316. };
  7317. /*
  7318. * Bend CLKOUT_DP
  7319. * steps -50 to 50 inclusive, in steps of 5
  7320. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7321. * change in clock period = -(steps / 10) * 5.787 ps
  7322. */
  7323. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7324. {
  7325. uint32_t tmp;
  7326. int idx = BEND_IDX(steps);
  7327. if (WARN_ON(steps % 5 != 0))
  7328. return;
  7329. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7330. return;
  7331. mutex_lock(&dev_priv->sb_lock);
  7332. if (steps % 10 != 0)
  7333. tmp = 0xAAAAAAAB;
  7334. else
  7335. tmp = 0x00000000;
  7336. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7337. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7338. tmp &= 0xffff0000;
  7339. tmp |= sscdivintphase[idx];
  7340. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7341. mutex_unlock(&dev_priv->sb_lock);
  7342. }
  7343. #undef BEND_IDX
  7344. static void lpt_init_pch_refclk(struct drm_device *dev)
  7345. {
  7346. struct intel_encoder *encoder;
  7347. bool has_vga = false;
  7348. for_each_intel_encoder(dev, encoder) {
  7349. switch (encoder->type) {
  7350. case INTEL_OUTPUT_ANALOG:
  7351. has_vga = true;
  7352. break;
  7353. default:
  7354. break;
  7355. }
  7356. }
  7357. if (has_vga) {
  7358. lpt_bend_clkout_dp(to_i915(dev), 0);
  7359. lpt_enable_clkout_dp(dev, true, true);
  7360. } else {
  7361. lpt_disable_clkout_dp(dev);
  7362. }
  7363. }
  7364. /*
  7365. * Initialize reference clocks when the driver loads
  7366. */
  7367. void intel_init_pch_refclk(struct drm_device *dev)
  7368. {
  7369. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7370. ironlake_init_pch_refclk(dev);
  7371. else if (HAS_PCH_LPT(dev))
  7372. lpt_init_pch_refclk(dev);
  7373. }
  7374. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7375. {
  7376. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7377. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7378. int pipe = intel_crtc->pipe;
  7379. uint32_t val;
  7380. val = 0;
  7381. switch (intel_crtc->config->pipe_bpp) {
  7382. case 18:
  7383. val |= PIPECONF_6BPC;
  7384. break;
  7385. case 24:
  7386. val |= PIPECONF_8BPC;
  7387. break;
  7388. case 30:
  7389. val |= PIPECONF_10BPC;
  7390. break;
  7391. case 36:
  7392. val |= PIPECONF_12BPC;
  7393. break;
  7394. default:
  7395. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7396. BUG();
  7397. }
  7398. if (intel_crtc->config->dither)
  7399. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7400. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7401. val |= PIPECONF_INTERLACED_ILK;
  7402. else
  7403. val |= PIPECONF_PROGRESSIVE;
  7404. if (intel_crtc->config->limited_color_range)
  7405. val |= PIPECONF_COLOR_RANGE_SELECT;
  7406. I915_WRITE(PIPECONF(pipe), val);
  7407. POSTING_READ(PIPECONF(pipe));
  7408. }
  7409. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7410. {
  7411. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7413. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7414. u32 val = 0;
  7415. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7416. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7417. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7418. val |= PIPECONF_INTERLACED_ILK;
  7419. else
  7420. val |= PIPECONF_PROGRESSIVE;
  7421. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7422. POSTING_READ(PIPECONF(cpu_transcoder));
  7423. }
  7424. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7425. {
  7426. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7427. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7428. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7429. u32 val = 0;
  7430. switch (intel_crtc->config->pipe_bpp) {
  7431. case 18:
  7432. val |= PIPEMISC_DITHER_6_BPC;
  7433. break;
  7434. case 24:
  7435. val |= PIPEMISC_DITHER_8_BPC;
  7436. break;
  7437. case 30:
  7438. val |= PIPEMISC_DITHER_10_BPC;
  7439. break;
  7440. case 36:
  7441. val |= PIPEMISC_DITHER_12_BPC;
  7442. break;
  7443. default:
  7444. /* Case prevented by pipe_config_set_bpp. */
  7445. BUG();
  7446. }
  7447. if (intel_crtc->config->dither)
  7448. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7449. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7450. }
  7451. }
  7452. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7453. {
  7454. /*
  7455. * Account for spread spectrum to avoid
  7456. * oversubscribing the link. Max center spread
  7457. * is 2.5%; use 5% for safety's sake.
  7458. */
  7459. u32 bps = target_clock * bpp * 21 / 20;
  7460. return DIV_ROUND_UP(bps, link_bw * 8);
  7461. }
  7462. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7463. {
  7464. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7465. }
  7466. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7467. struct intel_crtc_state *crtc_state,
  7468. struct dpll *reduced_clock)
  7469. {
  7470. struct drm_crtc *crtc = &intel_crtc->base;
  7471. struct drm_device *dev = crtc->dev;
  7472. struct drm_i915_private *dev_priv = dev->dev_private;
  7473. struct drm_atomic_state *state = crtc_state->base.state;
  7474. struct drm_connector *connector;
  7475. struct drm_connector_state *connector_state;
  7476. struct intel_encoder *encoder;
  7477. u32 dpll, fp, fp2;
  7478. int factor, i;
  7479. bool is_lvds = false, is_sdvo = false;
  7480. for_each_connector_in_state(state, connector, connector_state, i) {
  7481. if (connector_state->crtc != crtc_state->base.crtc)
  7482. continue;
  7483. encoder = to_intel_encoder(connector_state->best_encoder);
  7484. switch (encoder->type) {
  7485. case INTEL_OUTPUT_LVDS:
  7486. is_lvds = true;
  7487. break;
  7488. case INTEL_OUTPUT_SDVO:
  7489. case INTEL_OUTPUT_HDMI:
  7490. is_sdvo = true;
  7491. break;
  7492. default:
  7493. break;
  7494. }
  7495. }
  7496. /* Enable autotuning of the PLL clock (if permissible) */
  7497. factor = 21;
  7498. if (is_lvds) {
  7499. if ((intel_panel_use_ssc(dev_priv) &&
  7500. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7501. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7502. factor = 25;
  7503. } else if (crtc_state->sdvo_tv_clock)
  7504. factor = 20;
  7505. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7506. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7507. fp |= FP_CB_TUNE;
  7508. if (reduced_clock) {
  7509. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7510. if (reduced_clock->m < factor * reduced_clock->n)
  7511. fp2 |= FP_CB_TUNE;
  7512. } else {
  7513. fp2 = fp;
  7514. }
  7515. dpll = 0;
  7516. if (is_lvds)
  7517. dpll |= DPLLB_MODE_LVDS;
  7518. else
  7519. dpll |= DPLLB_MODE_DAC_SERIAL;
  7520. dpll |= (crtc_state->pixel_multiplier - 1)
  7521. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7522. if (is_sdvo)
  7523. dpll |= DPLL_SDVO_HIGH_SPEED;
  7524. if (crtc_state->has_dp_encoder)
  7525. dpll |= DPLL_SDVO_HIGH_SPEED;
  7526. /* compute bitmask from p1 value */
  7527. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7528. /* also FPA1 */
  7529. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7530. switch (crtc_state->dpll.p2) {
  7531. case 5:
  7532. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7533. break;
  7534. case 7:
  7535. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7536. break;
  7537. case 10:
  7538. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7539. break;
  7540. case 14:
  7541. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7542. break;
  7543. }
  7544. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7545. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7546. else
  7547. dpll |= PLL_REF_INPUT_DREFCLK;
  7548. dpll |= DPLL_VCO_ENABLE;
  7549. crtc_state->dpll_hw_state.dpll = dpll;
  7550. crtc_state->dpll_hw_state.fp0 = fp;
  7551. crtc_state->dpll_hw_state.fp1 = fp2;
  7552. }
  7553. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7554. struct intel_crtc_state *crtc_state)
  7555. {
  7556. struct drm_device *dev = crtc->base.dev;
  7557. struct drm_i915_private *dev_priv = dev->dev_private;
  7558. struct dpll reduced_clock;
  7559. bool has_reduced_clock = false;
  7560. struct intel_shared_dpll *pll;
  7561. const struct intel_limit *limit;
  7562. int refclk = 120000;
  7563. memset(&crtc_state->dpll_hw_state, 0,
  7564. sizeof(crtc_state->dpll_hw_state));
  7565. crtc->lowfreq_avail = false;
  7566. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7567. if (!crtc_state->has_pch_encoder)
  7568. return 0;
  7569. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7570. if (intel_panel_use_ssc(dev_priv)) {
  7571. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7572. dev_priv->vbt.lvds_ssc_freq);
  7573. refclk = dev_priv->vbt.lvds_ssc_freq;
  7574. }
  7575. if (intel_is_dual_link_lvds(dev)) {
  7576. if (refclk == 100000)
  7577. limit = &intel_limits_ironlake_dual_lvds_100m;
  7578. else
  7579. limit = &intel_limits_ironlake_dual_lvds;
  7580. } else {
  7581. if (refclk == 100000)
  7582. limit = &intel_limits_ironlake_single_lvds_100m;
  7583. else
  7584. limit = &intel_limits_ironlake_single_lvds;
  7585. }
  7586. } else {
  7587. limit = &intel_limits_ironlake_dac;
  7588. }
  7589. if (!crtc_state->clock_set &&
  7590. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7591. refclk, NULL, &crtc_state->dpll)) {
  7592. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7593. return -EINVAL;
  7594. }
  7595. ironlake_compute_dpll(crtc, crtc_state,
  7596. has_reduced_clock ? &reduced_clock : NULL);
  7597. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7598. if (pll == NULL) {
  7599. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7600. pipe_name(crtc->pipe));
  7601. return -EINVAL;
  7602. }
  7603. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7604. has_reduced_clock)
  7605. crtc->lowfreq_avail = true;
  7606. return 0;
  7607. }
  7608. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7609. struct intel_link_m_n *m_n)
  7610. {
  7611. struct drm_device *dev = crtc->base.dev;
  7612. struct drm_i915_private *dev_priv = dev->dev_private;
  7613. enum pipe pipe = crtc->pipe;
  7614. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7615. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7616. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7617. & ~TU_SIZE_MASK;
  7618. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7619. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7620. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7621. }
  7622. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7623. enum transcoder transcoder,
  7624. struct intel_link_m_n *m_n,
  7625. struct intel_link_m_n *m2_n2)
  7626. {
  7627. struct drm_device *dev = crtc->base.dev;
  7628. struct drm_i915_private *dev_priv = dev->dev_private;
  7629. enum pipe pipe = crtc->pipe;
  7630. if (INTEL_INFO(dev)->gen >= 5) {
  7631. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7632. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7633. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7634. & ~TU_SIZE_MASK;
  7635. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7636. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7637. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7638. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7639. * gen < 8) and if DRRS is supported (to make sure the
  7640. * registers are not unnecessarily read).
  7641. */
  7642. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7643. crtc->config->has_drrs) {
  7644. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7645. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7646. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7647. & ~TU_SIZE_MASK;
  7648. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7649. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7650. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7651. }
  7652. } else {
  7653. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7654. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7655. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7656. & ~TU_SIZE_MASK;
  7657. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7658. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7659. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7660. }
  7661. }
  7662. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7663. struct intel_crtc_state *pipe_config)
  7664. {
  7665. if (pipe_config->has_pch_encoder)
  7666. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7667. else
  7668. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7669. &pipe_config->dp_m_n,
  7670. &pipe_config->dp_m2_n2);
  7671. }
  7672. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7673. struct intel_crtc_state *pipe_config)
  7674. {
  7675. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7676. &pipe_config->fdi_m_n, NULL);
  7677. }
  7678. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7679. struct intel_crtc_state *pipe_config)
  7680. {
  7681. struct drm_device *dev = crtc->base.dev;
  7682. struct drm_i915_private *dev_priv = dev->dev_private;
  7683. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7684. uint32_t ps_ctrl = 0;
  7685. int id = -1;
  7686. int i;
  7687. /* find scaler attached to this pipe */
  7688. for (i = 0; i < crtc->num_scalers; i++) {
  7689. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7690. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7691. id = i;
  7692. pipe_config->pch_pfit.enabled = true;
  7693. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7694. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7695. break;
  7696. }
  7697. }
  7698. scaler_state->scaler_id = id;
  7699. if (id >= 0) {
  7700. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7701. } else {
  7702. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7703. }
  7704. }
  7705. static void
  7706. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7707. struct intel_initial_plane_config *plane_config)
  7708. {
  7709. struct drm_device *dev = crtc->base.dev;
  7710. struct drm_i915_private *dev_priv = dev->dev_private;
  7711. u32 val, base, offset, stride_mult, tiling;
  7712. int pipe = crtc->pipe;
  7713. int fourcc, pixel_format;
  7714. unsigned int aligned_height;
  7715. struct drm_framebuffer *fb;
  7716. struct intel_framebuffer *intel_fb;
  7717. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7718. if (!intel_fb) {
  7719. DRM_DEBUG_KMS("failed to alloc fb\n");
  7720. return;
  7721. }
  7722. fb = &intel_fb->base;
  7723. val = I915_READ(PLANE_CTL(pipe, 0));
  7724. if (!(val & PLANE_CTL_ENABLE))
  7725. goto error;
  7726. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7727. fourcc = skl_format_to_fourcc(pixel_format,
  7728. val & PLANE_CTL_ORDER_RGBX,
  7729. val & PLANE_CTL_ALPHA_MASK);
  7730. fb->pixel_format = fourcc;
  7731. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7732. tiling = val & PLANE_CTL_TILED_MASK;
  7733. switch (tiling) {
  7734. case PLANE_CTL_TILED_LINEAR:
  7735. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7736. break;
  7737. case PLANE_CTL_TILED_X:
  7738. plane_config->tiling = I915_TILING_X;
  7739. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7740. break;
  7741. case PLANE_CTL_TILED_Y:
  7742. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7743. break;
  7744. case PLANE_CTL_TILED_YF:
  7745. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7746. break;
  7747. default:
  7748. MISSING_CASE(tiling);
  7749. goto error;
  7750. }
  7751. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7752. plane_config->base = base;
  7753. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7754. val = I915_READ(PLANE_SIZE(pipe, 0));
  7755. fb->height = ((val >> 16) & 0xfff) + 1;
  7756. fb->width = ((val >> 0) & 0x1fff) + 1;
  7757. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7758. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7759. fb->pixel_format);
  7760. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7761. aligned_height = intel_fb_align_height(dev, fb->height,
  7762. fb->pixel_format,
  7763. fb->modifier[0]);
  7764. plane_config->size = fb->pitches[0] * aligned_height;
  7765. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7766. pipe_name(pipe), fb->width, fb->height,
  7767. fb->bits_per_pixel, base, fb->pitches[0],
  7768. plane_config->size);
  7769. plane_config->fb = intel_fb;
  7770. return;
  7771. error:
  7772. kfree(fb);
  7773. }
  7774. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7775. struct intel_crtc_state *pipe_config)
  7776. {
  7777. struct drm_device *dev = crtc->base.dev;
  7778. struct drm_i915_private *dev_priv = dev->dev_private;
  7779. uint32_t tmp;
  7780. tmp = I915_READ(PF_CTL(crtc->pipe));
  7781. if (tmp & PF_ENABLE) {
  7782. pipe_config->pch_pfit.enabled = true;
  7783. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7784. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7785. /* We currently do not free assignements of panel fitters on
  7786. * ivb/hsw (since we don't use the higher upscaling modes which
  7787. * differentiates them) so just WARN about this case for now. */
  7788. if (IS_GEN7(dev)) {
  7789. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7790. PF_PIPE_SEL_IVB(crtc->pipe));
  7791. }
  7792. }
  7793. }
  7794. static void
  7795. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7796. struct intel_initial_plane_config *plane_config)
  7797. {
  7798. struct drm_device *dev = crtc->base.dev;
  7799. struct drm_i915_private *dev_priv = dev->dev_private;
  7800. u32 val, base, offset;
  7801. int pipe = crtc->pipe;
  7802. int fourcc, pixel_format;
  7803. unsigned int aligned_height;
  7804. struct drm_framebuffer *fb;
  7805. struct intel_framebuffer *intel_fb;
  7806. val = I915_READ(DSPCNTR(pipe));
  7807. if (!(val & DISPLAY_PLANE_ENABLE))
  7808. return;
  7809. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7810. if (!intel_fb) {
  7811. DRM_DEBUG_KMS("failed to alloc fb\n");
  7812. return;
  7813. }
  7814. fb = &intel_fb->base;
  7815. if (INTEL_INFO(dev)->gen >= 4) {
  7816. if (val & DISPPLANE_TILED) {
  7817. plane_config->tiling = I915_TILING_X;
  7818. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7819. }
  7820. }
  7821. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7822. fourcc = i9xx_format_to_fourcc(pixel_format);
  7823. fb->pixel_format = fourcc;
  7824. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7825. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7826. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7827. offset = I915_READ(DSPOFFSET(pipe));
  7828. } else {
  7829. if (plane_config->tiling)
  7830. offset = I915_READ(DSPTILEOFF(pipe));
  7831. else
  7832. offset = I915_READ(DSPLINOFF(pipe));
  7833. }
  7834. plane_config->base = base;
  7835. val = I915_READ(PIPESRC(pipe));
  7836. fb->width = ((val >> 16) & 0xfff) + 1;
  7837. fb->height = ((val >> 0) & 0xfff) + 1;
  7838. val = I915_READ(DSPSTRIDE(pipe));
  7839. fb->pitches[0] = val & 0xffffffc0;
  7840. aligned_height = intel_fb_align_height(dev, fb->height,
  7841. fb->pixel_format,
  7842. fb->modifier[0]);
  7843. plane_config->size = fb->pitches[0] * aligned_height;
  7844. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7845. pipe_name(pipe), fb->width, fb->height,
  7846. fb->bits_per_pixel, base, fb->pitches[0],
  7847. plane_config->size);
  7848. plane_config->fb = intel_fb;
  7849. }
  7850. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7851. struct intel_crtc_state *pipe_config)
  7852. {
  7853. struct drm_device *dev = crtc->base.dev;
  7854. struct drm_i915_private *dev_priv = dev->dev_private;
  7855. enum intel_display_power_domain power_domain;
  7856. uint32_t tmp;
  7857. bool ret;
  7858. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7859. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7860. return false;
  7861. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7862. pipe_config->shared_dpll = NULL;
  7863. ret = false;
  7864. tmp = I915_READ(PIPECONF(crtc->pipe));
  7865. if (!(tmp & PIPECONF_ENABLE))
  7866. goto out;
  7867. switch (tmp & PIPECONF_BPC_MASK) {
  7868. case PIPECONF_6BPC:
  7869. pipe_config->pipe_bpp = 18;
  7870. break;
  7871. case PIPECONF_8BPC:
  7872. pipe_config->pipe_bpp = 24;
  7873. break;
  7874. case PIPECONF_10BPC:
  7875. pipe_config->pipe_bpp = 30;
  7876. break;
  7877. case PIPECONF_12BPC:
  7878. pipe_config->pipe_bpp = 36;
  7879. break;
  7880. default:
  7881. break;
  7882. }
  7883. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7884. pipe_config->limited_color_range = true;
  7885. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7886. struct intel_shared_dpll *pll;
  7887. enum intel_dpll_id pll_id;
  7888. pipe_config->has_pch_encoder = true;
  7889. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7890. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7891. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7892. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7893. if (HAS_PCH_IBX(dev_priv)) {
  7894. /*
  7895. * The pipe->pch transcoder and pch transcoder->pll
  7896. * mapping is fixed.
  7897. */
  7898. pll_id = (enum intel_dpll_id) crtc->pipe;
  7899. } else {
  7900. tmp = I915_READ(PCH_DPLL_SEL);
  7901. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7902. pll_id = DPLL_ID_PCH_PLL_B;
  7903. else
  7904. pll_id= DPLL_ID_PCH_PLL_A;
  7905. }
  7906. pipe_config->shared_dpll =
  7907. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7908. pll = pipe_config->shared_dpll;
  7909. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7910. &pipe_config->dpll_hw_state));
  7911. tmp = pipe_config->dpll_hw_state.dpll;
  7912. pipe_config->pixel_multiplier =
  7913. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7914. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7915. ironlake_pch_clock_get(crtc, pipe_config);
  7916. } else {
  7917. pipe_config->pixel_multiplier = 1;
  7918. }
  7919. intel_get_pipe_timings(crtc, pipe_config);
  7920. intel_get_pipe_src_size(crtc, pipe_config);
  7921. ironlake_get_pfit_config(crtc, pipe_config);
  7922. ret = true;
  7923. out:
  7924. intel_display_power_put(dev_priv, power_domain);
  7925. return ret;
  7926. }
  7927. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7928. {
  7929. struct drm_device *dev = dev_priv->dev;
  7930. struct intel_crtc *crtc;
  7931. for_each_intel_crtc(dev, crtc)
  7932. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7933. pipe_name(crtc->pipe));
  7934. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7935. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7936. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7937. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7938. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7939. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7940. "CPU PWM1 enabled\n");
  7941. if (IS_HASWELL(dev))
  7942. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7943. "CPU PWM2 enabled\n");
  7944. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7945. "PCH PWM1 enabled\n");
  7946. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7947. "Utility pin enabled\n");
  7948. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7949. /*
  7950. * In theory we can still leave IRQs enabled, as long as only the HPD
  7951. * interrupts remain enabled. We used to check for that, but since it's
  7952. * gen-specific and since we only disable LCPLL after we fully disable
  7953. * the interrupts, the check below should be enough.
  7954. */
  7955. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7956. }
  7957. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7958. {
  7959. struct drm_device *dev = dev_priv->dev;
  7960. if (IS_HASWELL(dev))
  7961. return I915_READ(D_COMP_HSW);
  7962. else
  7963. return I915_READ(D_COMP_BDW);
  7964. }
  7965. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7966. {
  7967. struct drm_device *dev = dev_priv->dev;
  7968. if (IS_HASWELL(dev)) {
  7969. mutex_lock(&dev_priv->rps.hw_lock);
  7970. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7971. val))
  7972. DRM_ERROR("Failed to write to D_COMP\n");
  7973. mutex_unlock(&dev_priv->rps.hw_lock);
  7974. } else {
  7975. I915_WRITE(D_COMP_BDW, val);
  7976. POSTING_READ(D_COMP_BDW);
  7977. }
  7978. }
  7979. /*
  7980. * This function implements pieces of two sequences from BSpec:
  7981. * - Sequence for display software to disable LCPLL
  7982. * - Sequence for display software to allow package C8+
  7983. * The steps implemented here are just the steps that actually touch the LCPLL
  7984. * register. Callers should take care of disabling all the display engine
  7985. * functions, doing the mode unset, fixing interrupts, etc.
  7986. */
  7987. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7988. bool switch_to_fclk, bool allow_power_down)
  7989. {
  7990. uint32_t val;
  7991. assert_can_disable_lcpll(dev_priv);
  7992. val = I915_READ(LCPLL_CTL);
  7993. if (switch_to_fclk) {
  7994. val |= LCPLL_CD_SOURCE_FCLK;
  7995. I915_WRITE(LCPLL_CTL, val);
  7996. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7997. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7998. DRM_ERROR("Switching to FCLK failed\n");
  7999. val = I915_READ(LCPLL_CTL);
  8000. }
  8001. val |= LCPLL_PLL_DISABLE;
  8002. I915_WRITE(LCPLL_CTL, val);
  8003. POSTING_READ(LCPLL_CTL);
  8004. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  8005. DRM_ERROR("LCPLL still locked\n");
  8006. val = hsw_read_dcomp(dev_priv);
  8007. val |= D_COMP_COMP_DISABLE;
  8008. hsw_write_dcomp(dev_priv, val);
  8009. ndelay(100);
  8010. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8011. 1))
  8012. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8013. if (allow_power_down) {
  8014. val = I915_READ(LCPLL_CTL);
  8015. val |= LCPLL_POWER_DOWN_ALLOW;
  8016. I915_WRITE(LCPLL_CTL, val);
  8017. POSTING_READ(LCPLL_CTL);
  8018. }
  8019. }
  8020. /*
  8021. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8022. * source.
  8023. */
  8024. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8025. {
  8026. uint32_t val;
  8027. val = I915_READ(LCPLL_CTL);
  8028. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8029. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8030. return;
  8031. /*
  8032. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8033. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8034. */
  8035. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8036. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8037. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8038. I915_WRITE(LCPLL_CTL, val);
  8039. POSTING_READ(LCPLL_CTL);
  8040. }
  8041. val = hsw_read_dcomp(dev_priv);
  8042. val |= D_COMP_COMP_FORCE;
  8043. val &= ~D_COMP_COMP_DISABLE;
  8044. hsw_write_dcomp(dev_priv, val);
  8045. val = I915_READ(LCPLL_CTL);
  8046. val &= ~LCPLL_PLL_DISABLE;
  8047. I915_WRITE(LCPLL_CTL, val);
  8048. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  8049. DRM_ERROR("LCPLL not locked yet\n");
  8050. if (val & LCPLL_CD_SOURCE_FCLK) {
  8051. val = I915_READ(LCPLL_CTL);
  8052. val &= ~LCPLL_CD_SOURCE_FCLK;
  8053. I915_WRITE(LCPLL_CTL, val);
  8054. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8055. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8056. DRM_ERROR("Switching back to LCPLL failed\n");
  8057. }
  8058. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8059. intel_update_cdclk(dev_priv->dev);
  8060. }
  8061. /*
  8062. * Package states C8 and deeper are really deep PC states that can only be
  8063. * reached when all the devices on the system allow it, so even if the graphics
  8064. * device allows PC8+, it doesn't mean the system will actually get to these
  8065. * states. Our driver only allows PC8+ when going into runtime PM.
  8066. *
  8067. * The requirements for PC8+ are that all the outputs are disabled, the power
  8068. * well is disabled and most interrupts are disabled, and these are also
  8069. * requirements for runtime PM. When these conditions are met, we manually do
  8070. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8071. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8072. * hang the machine.
  8073. *
  8074. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8075. * the state of some registers, so when we come back from PC8+ we need to
  8076. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8077. * need to take care of the registers kept by RC6. Notice that this happens even
  8078. * if we don't put the device in PCI D3 state (which is what currently happens
  8079. * because of the runtime PM support).
  8080. *
  8081. * For more, read "Display Sequences for Package C8" on the hardware
  8082. * documentation.
  8083. */
  8084. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8085. {
  8086. struct drm_device *dev = dev_priv->dev;
  8087. uint32_t val;
  8088. DRM_DEBUG_KMS("Enabling package C8+\n");
  8089. if (HAS_PCH_LPT_LP(dev)) {
  8090. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8091. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8092. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8093. }
  8094. lpt_disable_clkout_dp(dev);
  8095. hsw_disable_lcpll(dev_priv, true, true);
  8096. }
  8097. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8098. {
  8099. struct drm_device *dev = dev_priv->dev;
  8100. uint32_t val;
  8101. DRM_DEBUG_KMS("Disabling package C8+\n");
  8102. hsw_restore_lcpll(dev_priv);
  8103. lpt_init_pch_refclk(dev);
  8104. if (HAS_PCH_LPT_LP(dev)) {
  8105. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8106. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8107. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8108. }
  8109. }
  8110. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8111. {
  8112. struct drm_device *dev = old_state->dev;
  8113. struct intel_atomic_state *old_intel_state =
  8114. to_intel_atomic_state(old_state);
  8115. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8116. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8117. }
  8118. /* compute the max rate for new configuration */
  8119. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8120. {
  8121. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8122. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8123. struct drm_crtc *crtc;
  8124. struct drm_crtc_state *cstate;
  8125. struct intel_crtc_state *crtc_state;
  8126. unsigned max_pixel_rate = 0, i;
  8127. enum pipe pipe;
  8128. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8129. sizeof(intel_state->min_pixclk));
  8130. for_each_crtc_in_state(state, crtc, cstate, i) {
  8131. int pixel_rate;
  8132. crtc_state = to_intel_crtc_state(cstate);
  8133. if (!crtc_state->base.enable) {
  8134. intel_state->min_pixclk[i] = 0;
  8135. continue;
  8136. }
  8137. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8138. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8139. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8140. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8141. intel_state->min_pixclk[i] = pixel_rate;
  8142. }
  8143. for_each_pipe(dev_priv, pipe)
  8144. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8145. return max_pixel_rate;
  8146. }
  8147. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8148. {
  8149. struct drm_i915_private *dev_priv = dev->dev_private;
  8150. uint32_t val, data;
  8151. int ret;
  8152. if (WARN((I915_READ(LCPLL_CTL) &
  8153. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8154. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8155. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8156. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8157. "trying to change cdclk frequency with cdclk not enabled\n"))
  8158. return;
  8159. mutex_lock(&dev_priv->rps.hw_lock);
  8160. ret = sandybridge_pcode_write(dev_priv,
  8161. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8162. mutex_unlock(&dev_priv->rps.hw_lock);
  8163. if (ret) {
  8164. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8165. return;
  8166. }
  8167. val = I915_READ(LCPLL_CTL);
  8168. val |= LCPLL_CD_SOURCE_FCLK;
  8169. I915_WRITE(LCPLL_CTL, val);
  8170. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8171. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8172. DRM_ERROR("Switching to FCLK failed\n");
  8173. val = I915_READ(LCPLL_CTL);
  8174. val &= ~LCPLL_CLK_FREQ_MASK;
  8175. switch (cdclk) {
  8176. case 450000:
  8177. val |= LCPLL_CLK_FREQ_450;
  8178. data = 0;
  8179. break;
  8180. case 540000:
  8181. val |= LCPLL_CLK_FREQ_54O_BDW;
  8182. data = 1;
  8183. break;
  8184. case 337500:
  8185. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8186. data = 2;
  8187. break;
  8188. case 675000:
  8189. val |= LCPLL_CLK_FREQ_675_BDW;
  8190. data = 3;
  8191. break;
  8192. default:
  8193. WARN(1, "invalid cdclk frequency\n");
  8194. return;
  8195. }
  8196. I915_WRITE(LCPLL_CTL, val);
  8197. val = I915_READ(LCPLL_CTL);
  8198. val &= ~LCPLL_CD_SOURCE_FCLK;
  8199. I915_WRITE(LCPLL_CTL, val);
  8200. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8201. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8202. DRM_ERROR("Switching back to LCPLL failed\n");
  8203. mutex_lock(&dev_priv->rps.hw_lock);
  8204. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8205. mutex_unlock(&dev_priv->rps.hw_lock);
  8206. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8207. intel_update_cdclk(dev);
  8208. WARN(cdclk != dev_priv->cdclk_freq,
  8209. "cdclk requested %d kHz but got %d kHz\n",
  8210. cdclk, dev_priv->cdclk_freq);
  8211. }
  8212. static int broadwell_calc_cdclk(int max_pixclk)
  8213. {
  8214. if (max_pixclk > 540000)
  8215. return 675000;
  8216. else if (max_pixclk > 450000)
  8217. return 540000;
  8218. else if (max_pixclk > 337500)
  8219. return 450000;
  8220. else
  8221. return 337500;
  8222. }
  8223. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8224. {
  8225. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8226. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8227. int max_pixclk = ilk_max_pixel_rate(state);
  8228. int cdclk;
  8229. /*
  8230. * FIXME should also account for plane ratio
  8231. * once 64bpp pixel formats are supported.
  8232. */
  8233. cdclk = broadwell_calc_cdclk(max_pixclk);
  8234. if (cdclk > dev_priv->max_cdclk_freq) {
  8235. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8236. cdclk, dev_priv->max_cdclk_freq);
  8237. return -EINVAL;
  8238. }
  8239. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8240. if (!intel_state->active_crtcs)
  8241. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8242. return 0;
  8243. }
  8244. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8245. {
  8246. struct drm_device *dev = old_state->dev;
  8247. struct intel_atomic_state *old_intel_state =
  8248. to_intel_atomic_state(old_state);
  8249. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8250. broadwell_set_cdclk(dev, req_cdclk);
  8251. }
  8252. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8253. {
  8254. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8255. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8256. const int max_pixclk = ilk_max_pixel_rate(state);
  8257. int vco = intel_state->cdclk_pll_vco;
  8258. int cdclk;
  8259. /*
  8260. * FIXME should also account for plane ratio
  8261. * once 64bpp pixel formats are supported.
  8262. */
  8263. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8264. /*
  8265. * FIXME move the cdclk caclulation to
  8266. * compute_config() so we can fail gracegully.
  8267. */
  8268. if (cdclk > dev_priv->max_cdclk_freq) {
  8269. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8270. cdclk, dev_priv->max_cdclk_freq);
  8271. cdclk = dev_priv->max_cdclk_freq;
  8272. }
  8273. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8274. if (!intel_state->active_crtcs)
  8275. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8276. return 0;
  8277. }
  8278. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8279. {
  8280. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8281. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8282. unsigned int req_cdclk = intel_state->dev_cdclk;
  8283. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8284. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8285. }
  8286. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8287. struct intel_crtc_state *crtc_state)
  8288. {
  8289. struct intel_encoder *intel_encoder =
  8290. intel_ddi_get_crtc_new_encoder(crtc_state);
  8291. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8292. if (!intel_ddi_pll_select(crtc, crtc_state))
  8293. return -EINVAL;
  8294. }
  8295. crtc->lowfreq_avail = false;
  8296. return 0;
  8297. }
  8298. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8299. enum port port,
  8300. struct intel_crtc_state *pipe_config)
  8301. {
  8302. enum intel_dpll_id id;
  8303. switch (port) {
  8304. case PORT_A:
  8305. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8306. id = DPLL_ID_SKL_DPLL0;
  8307. break;
  8308. case PORT_B:
  8309. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8310. id = DPLL_ID_SKL_DPLL1;
  8311. break;
  8312. case PORT_C:
  8313. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8314. id = DPLL_ID_SKL_DPLL2;
  8315. break;
  8316. default:
  8317. DRM_ERROR("Incorrect port type\n");
  8318. return;
  8319. }
  8320. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8321. }
  8322. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8323. enum port port,
  8324. struct intel_crtc_state *pipe_config)
  8325. {
  8326. enum intel_dpll_id id;
  8327. u32 temp;
  8328. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8329. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8330. switch (pipe_config->ddi_pll_sel) {
  8331. case SKL_DPLL0:
  8332. id = DPLL_ID_SKL_DPLL0;
  8333. break;
  8334. case SKL_DPLL1:
  8335. id = DPLL_ID_SKL_DPLL1;
  8336. break;
  8337. case SKL_DPLL2:
  8338. id = DPLL_ID_SKL_DPLL2;
  8339. break;
  8340. case SKL_DPLL3:
  8341. id = DPLL_ID_SKL_DPLL3;
  8342. break;
  8343. default:
  8344. MISSING_CASE(pipe_config->ddi_pll_sel);
  8345. return;
  8346. }
  8347. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8348. }
  8349. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8350. enum port port,
  8351. struct intel_crtc_state *pipe_config)
  8352. {
  8353. enum intel_dpll_id id;
  8354. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8355. switch (pipe_config->ddi_pll_sel) {
  8356. case PORT_CLK_SEL_WRPLL1:
  8357. id = DPLL_ID_WRPLL1;
  8358. break;
  8359. case PORT_CLK_SEL_WRPLL2:
  8360. id = DPLL_ID_WRPLL2;
  8361. break;
  8362. case PORT_CLK_SEL_SPLL:
  8363. id = DPLL_ID_SPLL;
  8364. break;
  8365. case PORT_CLK_SEL_LCPLL_810:
  8366. id = DPLL_ID_LCPLL_810;
  8367. break;
  8368. case PORT_CLK_SEL_LCPLL_1350:
  8369. id = DPLL_ID_LCPLL_1350;
  8370. break;
  8371. case PORT_CLK_SEL_LCPLL_2700:
  8372. id = DPLL_ID_LCPLL_2700;
  8373. break;
  8374. default:
  8375. MISSING_CASE(pipe_config->ddi_pll_sel);
  8376. /* fall through */
  8377. case PORT_CLK_SEL_NONE:
  8378. return;
  8379. }
  8380. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8381. }
  8382. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8383. struct intel_crtc_state *pipe_config,
  8384. unsigned long *power_domain_mask)
  8385. {
  8386. struct drm_device *dev = crtc->base.dev;
  8387. struct drm_i915_private *dev_priv = dev->dev_private;
  8388. enum intel_display_power_domain power_domain;
  8389. u32 tmp;
  8390. /*
  8391. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8392. * transcoder handled below.
  8393. */
  8394. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8395. /*
  8396. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8397. * consistency and less surprising code; it's in always on power).
  8398. */
  8399. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8400. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8401. enum pipe trans_edp_pipe;
  8402. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8403. default:
  8404. WARN(1, "unknown pipe linked to edp transcoder\n");
  8405. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8406. case TRANS_DDI_EDP_INPUT_A_ON:
  8407. trans_edp_pipe = PIPE_A;
  8408. break;
  8409. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8410. trans_edp_pipe = PIPE_B;
  8411. break;
  8412. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8413. trans_edp_pipe = PIPE_C;
  8414. break;
  8415. }
  8416. if (trans_edp_pipe == crtc->pipe)
  8417. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8418. }
  8419. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8420. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8421. return false;
  8422. *power_domain_mask |= BIT(power_domain);
  8423. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8424. return tmp & PIPECONF_ENABLE;
  8425. }
  8426. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8427. struct intel_crtc_state *pipe_config,
  8428. unsigned long *power_domain_mask)
  8429. {
  8430. struct drm_device *dev = crtc->base.dev;
  8431. struct drm_i915_private *dev_priv = dev->dev_private;
  8432. enum intel_display_power_domain power_domain;
  8433. enum port port;
  8434. enum transcoder cpu_transcoder;
  8435. u32 tmp;
  8436. pipe_config->has_dsi_encoder = false;
  8437. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8438. if (port == PORT_A)
  8439. cpu_transcoder = TRANSCODER_DSI_A;
  8440. else
  8441. cpu_transcoder = TRANSCODER_DSI_C;
  8442. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8443. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8444. continue;
  8445. *power_domain_mask |= BIT(power_domain);
  8446. /*
  8447. * The PLL needs to be enabled with a valid divider
  8448. * configuration, otherwise accessing DSI registers will hang
  8449. * the machine. See BSpec North Display Engine
  8450. * registers/MIPI[BXT]. We can break out here early, since we
  8451. * need the same DSI PLL to be enabled for both DSI ports.
  8452. */
  8453. if (!intel_dsi_pll_is_enabled(dev_priv))
  8454. break;
  8455. /* XXX: this works for video mode only */
  8456. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8457. if (!(tmp & DPI_ENABLE))
  8458. continue;
  8459. tmp = I915_READ(MIPI_CTRL(port));
  8460. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8461. continue;
  8462. pipe_config->cpu_transcoder = cpu_transcoder;
  8463. pipe_config->has_dsi_encoder = true;
  8464. break;
  8465. }
  8466. return pipe_config->has_dsi_encoder;
  8467. }
  8468. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8469. struct intel_crtc_state *pipe_config)
  8470. {
  8471. struct drm_device *dev = crtc->base.dev;
  8472. struct drm_i915_private *dev_priv = dev->dev_private;
  8473. struct intel_shared_dpll *pll;
  8474. enum port port;
  8475. uint32_t tmp;
  8476. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8477. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8478. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8479. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8480. else if (IS_BROXTON(dev))
  8481. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8482. else
  8483. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8484. pll = pipe_config->shared_dpll;
  8485. if (pll) {
  8486. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8487. &pipe_config->dpll_hw_state));
  8488. }
  8489. /*
  8490. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8491. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8492. * the PCH transcoder is on.
  8493. */
  8494. if (INTEL_INFO(dev)->gen < 9 &&
  8495. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8496. pipe_config->has_pch_encoder = true;
  8497. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8498. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8499. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8500. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8501. }
  8502. }
  8503. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8504. struct intel_crtc_state *pipe_config)
  8505. {
  8506. struct drm_device *dev = crtc->base.dev;
  8507. struct drm_i915_private *dev_priv = dev->dev_private;
  8508. enum intel_display_power_domain power_domain;
  8509. unsigned long power_domain_mask;
  8510. bool active;
  8511. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8512. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8513. return false;
  8514. power_domain_mask = BIT(power_domain);
  8515. pipe_config->shared_dpll = NULL;
  8516. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8517. if (IS_BROXTON(dev_priv)) {
  8518. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8519. &power_domain_mask);
  8520. WARN_ON(active && pipe_config->has_dsi_encoder);
  8521. if (pipe_config->has_dsi_encoder)
  8522. active = true;
  8523. }
  8524. if (!active)
  8525. goto out;
  8526. if (!pipe_config->has_dsi_encoder) {
  8527. haswell_get_ddi_port_state(crtc, pipe_config);
  8528. intel_get_pipe_timings(crtc, pipe_config);
  8529. }
  8530. intel_get_pipe_src_size(crtc, pipe_config);
  8531. pipe_config->gamma_mode =
  8532. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8533. if (INTEL_INFO(dev)->gen >= 9) {
  8534. skl_init_scalers(dev, crtc, pipe_config);
  8535. }
  8536. if (INTEL_INFO(dev)->gen >= 9) {
  8537. pipe_config->scaler_state.scaler_id = -1;
  8538. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8539. }
  8540. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8541. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8542. power_domain_mask |= BIT(power_domain);
  8543. if (INTEL_INFO(dev)->gen >= 9)
  8544. skylake_get_pfit_config(crtc, pipe_config);
  8545. else
  8546. ironlake_get_pfit_config(crtc, pipe_config);
  8547. }
  8548. if (IS_HASWELL(dev))
  8549. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8550. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8551. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8552. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8553. pipe_config->pixel_multiplier =
  8554. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8555. } else {
  8556. pipe_config->pixel_multiplier = 1;
  8557. }
  8558. out:
  8559. for_each_power_domain(power_domain, power_domain_mask)
  8560. intel_display_power_put(dev_priv, power_domain);
  8561. return active;
  8562. }
  8563. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8564. const struct intel_plane_state *plane_state)
  8565. {
  8566. struct drm_device *dev = crtc->dev;
  8567. struct drm_i915_private *dev_priv = dev->dev_private;
  8568. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8569. uint32_t cntl = 0, size = 0;
  8570. if (plane_state && plane_state->visible) {
  8571. unsigned int width = plane_state->base.crtc_w;
  8572. unsigned int height = plane_state->base.crtc_h;
  8573. unsigned int stride = roundup_pow_of_two(width) * 4;
  8574. switch (stride) {
  8575. default:
  8576. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8577. width, stride);
  8578. stride = 256;
  8579. /* fallthrough */
  8580. case 256:
  8581. case 512:
  8582. case 1024:
  8583. case 2048:
  8584. break;
  8585. }
  8586. cntl |= CURSOR_ENABLE |
  8587. CURSOR_GAMMA_ENABLE |
  8588. CURSOR_FORMAT_ARGB |
  8589. CURSOR_STRIDE(stride);
  8590. size = (height << 12) | width;
  8591. }
  8592. if (intel_crtc->cursor_cntl != 0 &&
  8593. (intel_crtc->cursor_base != base ||
  8594. intel_crtc->cursor_size != size ||
  8595. intel_crtc->cursor_cntl != cntl)) {
  8596. /* On these chipsets we can only modify the base/size/stride
  8597. * whilst the cursor is disabled.
  8598. */
  8599. I915_WRITE(CURCNTR(PIPE_A), 0);
  8600. POSTING_READ(CURCNTR(PIPE_A));
  8601. intel_crtc->cursor_cntl = 0;
  8602. }
  8603. if (intel_crtc->cursor_base != base) {
  8604. I915_WRITE(CURBASE(PIPE_A), base);
  8605. intel_crtc->cursor_base = base;
  8606. }
  8607. if (intel_crtc->cursor_size != size) {
  8608. I915_WRITE(CURSIZE, size);
  8609. intel_crtc->cursor_size = size;
  8610. }
  8611. if (intel_crtc->cursor_cntl != cntl) {
  8612. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8613. POSTING_READ(CURCNTR(PIPE_A));
  8614. intel_crtc->cursor_cntl = cntl;
  8615. }
  8616. }
  8617. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8618. const struct intel_plane_state *plane_state)
  8619. {
  8620. struct drm_device *dev = crtc->dev;
  8621. struct drm_i915_private *dev_priv = dev->dev_private;
  8622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8623. int pipe = intel_crtc->pipe;
  8624. uint32_t cntl = 0;
  8625. if (plane_state && plane_state->visible) {
  8626. cntl = MCURSOR_GAMMA_ENABLE;
  8627. switch (plane_state->base.crtc_w) {
  8628. case 64:
  8629. cntl |= CURSOR_MODE_64_ARGB_AX;
  8630. break;
  8631. case 128:
  8632. cntl |= CURSOR_MODE_128_ARGB_AX;
  8633. break;
  8634. case 256:
  8635. cntl |= CURSOR_MODE_256_ARGB_AX;
  8636. break;
  8637. default:
  8638. MISSING_CASE(plane_state->base.crtc_w);
  8639. return;
  8640. }
  8641. cntl |= pipe << 28; /* Connect to correct pipe */
  8642. if (HAS_DDI(dev))
  8643. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8644. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8645. cntl |= CURSOR_ROTATE_180;
  8646. }
  8647. if (intel_crtc->cursor_cntl != cntl) {
  8648. I915_WRITE(CURCNTR(pipe), cntl);
  8649. POSTING_READ(CURCNTR(pipe));
  8650. intel_crtc->cursor_cntl = cntl;
  8651. }
  8652. /* and commit changes on next vblank */
  8653. I915_WRITE(CURBASE(pipe), base);
  8654. POSTING_READ(CURBASE(pipe));
  8655. intel_crtc->cursor_base = base;
  8656. }
  8657. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8658. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8659. const struct intel_plane_state *plane_state)
  8660. {
  8661. struct drm_device *dev = crtc->dev;
  8662. struct drm_i915_private *dev_priv = dev->dev_private;
  8663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8664. int pipe = intel_crtc->pipe;
  8665. u32 base = intel_crtc->cursor_addr;
  8666. u32 pos = 0;
  8667. if (plane_state) {
  8668. int x = plane_state->base.crtc_x;
  8669. int y = plane_state->base.crtc_y;
  8670. if (x < 0) {
  8671. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8672. x = -x;
  8673. }
  8674. pos |= x << CURSOR_X_SHIFT;
  8675. if (y < 0) {
  8676. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8677. y = -y;
  8678. }
  8679. pos |= y << CURSOR_Y_SHIFT;
  8680. /* ILK+ do this automagically */
  8681. if (HAS_GMCH_DISPLAY(dev) &&
  8682. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8683. base += (plane_state->base.crtc_h *
  8684. plane_state->base.crtc_w - 1) * 4;
  8685. }
  8686. }
  8687. I915_WRITE(CURPOS(pipe), pos);
  8688. if (IS_845G(dev) || IS_I865G(dev))
  8689. i845_update_cursor(crtc, base, plane_state);
  8690. else
  8691. i9xx_update_cursor(crtc, base, plane_state);
  8692. }
  8693. static bool cursor_size_ok(struct drm_device *dev,
  8694. uint32_t width, uint32_t height)
  8695. {
  8696. if (width == 0 || height == 0)
  8697. return false;
  8698. /*
  8699. * 845g/865g are special in that they are only limited by
  8700. * the width of their cursors, the height is arbitrary up to
  8701. * the precision of the register. Everything else requires
  8702. * square cursors, limited to a few power-of-two sizes.
  8703. */
  8704. if (IS_845G(dev) || IS_I865G(dev)) {
  8705. if ((width & 63) != 0)
  8706. return false;
  8707. if (width > (IS_845G(dev) ? 64 : 512))
  8708. return false;
  8709. if (height > 1023)
  8710. return false;
  8711. } else {
  8712. switch (width | height) {
  8713. case 256:
  8714. case 128:
  8715. if (IS_GEN2(dev))
  8716. return false;
  8717. case 64:
  8718. break;
  8719. default:
  8720. return false;
  8721. }
  8722. }
  8723. return true;
  8724. }
  8725. /* VESA 640x480x72Hz mode to set on the pipe */
  8726. static struct drm_display_mode load_detect_mode = {
  8727. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8728. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8729. };
  8730. struct drm_framebuffer *
  8731. __intel_framebuffer_create(struct drm_device *dev,
  8732. struct drm_mode_fb_cmd2 *mode_cmd,
  8733. struct drm_i915_gem_object *obj)
  8734. {
  8735. struct intel_framebuffer *intel_fb;
  8736. int ret;
  8737. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8738. if (!intel_fb)
  8739. return ERR_PTR(-ENOMEM);
  8740. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8741. if (ret)
  8742. goto err;
  8743. return &intel_fb->base;
  8744. err:
  8745. kfree(intel_fb);
  8746. return ERR_PTR(ret);
  8747. }
  8748. static struct drm_framebuffer *
  8749. intel_framebuffer_create(struct drm_device *dev,
  8750. struct drm_mode_fb_cmd2 *mode_cmd,
  8751. struct drm_i915_gem_object *obj)
  8752. {
  8753. struct drm_framebuffer *fb;
  8754. int ret;
  8755. ret = i915_mutex_lock_interruptible(dev);
  8756. if (ret)
  8757. return ERR_PTR(ret);
  8758. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8759. mutex_unlock(&dev->struct_mutex);
  8760. return fb;
  8761. }
  8762. static u32
  8763. intel_framebuffer_pitch_for_width(int width, int bpp)
  8764. {
  8765. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8766. return ALIGN(pitch, 64);
  8767. }
  8768. static u32
  8769. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8770. {
  8771. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8772. return PAGE_ALIGN(pitch * mode->vdisplay);
  8773. }
  8774. static struct drm_framebuffer *
  8775. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8776. struct drm_display_mode *mode,
  8777. int depth, int bpp)
  8778. {
  8779. struct drm_framebuffer *fb;
  8780. struct drm_i915_gem_object *obj;
  8781. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8782. obj = i915_gem_object_create(dev,
  8783. intel_framebuffer_size_for_mode(mode, bpp));
  8784. if (IS_ERR(obj))
  8785. return ERR_CAST(obj);
  8786. mode_cmd.width = mode->hdisplay;
  8787. mode_cmd.height = mode->vdisplay;
  8788. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8789. bpp);
  8790. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8791. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8792. if (IS_ERR(fb))
  8793. drm_gem_object_unreference_unlocked(&obj->base);
  8794. return fb;
  8795. }
  8796. static struct drm_framebuffer *
  8797. mode_fits_in_fbdev(struct drm_device *dev,
  8798. struct drm_display_mode *mode)
  8799. {
  8800. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8801. struct drm_i915_private *dev_priv = dev->dev_private;
  8802. struct drm_i915_gem_object *obj;
  8803. struct drm_framebuffer *fb;
  8804. if (!dev_priv->fbdev)
  8805. return NULL;
  8806. if (!dev_priv->fbdev->fb)
  8807. return NULL;
  8808. obj = dev_priv->fbdev->fb->obj;
  8809. BUG_ON(!obj);
  8810. fb = &dev_priv->fbdev->fb->base;
  8811. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8812. fb->bits_per_pixel))
  8813. return NULL;
  8814. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8815. return NULL;
  8816. drm_framebuffer_reference(fb);
  8817. return fb;
  8818. #else
  8819. return NULL;
  8820. #endif
  8821. }
  8822. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8823. struct drm_crtc *crtc,
  8824. struct drm_display_mode *mode,
  8825. struct drm_framebuffer *fb,
  8826. int x, int y)
  8827. {
  8828. struct drm_plane_state *plane_state;
  8829. int hdisplay, vdisplay;
  8830. int ret;
  8831. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8832. if (IS_ERR(plane_state))
  8833. return PTR_ERR(plane_state);
  8834. if (mode)
  8835. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8836. else
  8837. hdisplay = vdisplay = 0;
  8838. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8839. if (ret)
  8840. return ret;
  8841. drm_atomic_set_fb_for_plane(plane_state, fb);
  8842. plane_state->crtc_x = 0;
  8843. plane_state->crtc_y = 0;
  8844. plane_state->crtc_w = hdisplay;
  8845. plane_state->crtc_h = vdisplay;
  8846. plane_state->src_x = x << 16;
  8847. plane_state->src_y = y << 16;
  8848. plane_state->src_w = hdisplay << 16;
  8849. plane_state->src_h = vdisplay << 16;
  8850. return 0;
  8851. }
  8852. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8853. struct drm_display_mode *mode,
  8854. struct intel_load_detect_pipe *old,
  8855. struct drm_modeset_acquire_ctx *ctx)
  8856. {
  8857. struct intel_crtc *intel_crtc;
  8858. struct intel_encoder *intel_encoder =
  8859. intel_attached_encoder(connector);
  8860. struct drm_crtc *possible_crtc;
  8861. struct drm_encoder *encoder = &intel_encoder->base;
  8862. struct drm_crtc *crtc = NULL;
  8863. struct drm_device *dev = encoder->dev;
  8864. struct drm_framebuffer *fb;
  8865. struct drm_mode_config *config = &dev->mode_config;
  8866. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8867. struct drm_connector_state *connector_state;
  8868. struct intel_crtc_state *crtc_state;
  8869. int ret, i = -1;
  8870. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8871. connector->base.id, connector->name,
  8872. encoder->base.id, encoder->name);
  8873. old->restore_state = NULL;
  8874. retry:
  8875. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8876. if (ret)
  8877. goto fail;
  8878. /*
  8879. * Algorithm gets a little messy:
  8880. *
  8881. * - if the connector already has an assigned crtc, use it (but make
  8882. * sure it's on first)
  8883. *
  8884. * - try to find the first unused crtc that can drive this connector,
  8885. * and use that if we find one
  8886. */
  8887. /* See if we already have a CRTC for this connector */
  8888. if (connector->state->crtc) {
  8889. crtc = connector->state->crtc;
  8890. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8891. if (ret)
  8892. goto fail;
  8893. /* Make sure the crtc and connector are running */
  8894. goto found;
  8895. }
  8896. /* Find an unused one (if possible) */
  8897. for_each_crtc(dev, possible_crtc) {
  8898. i++;
  8899. if (!(encoder->possible_crtcs & (1 << i)))
  8900. continue;
  8901. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8902. if (ret)
  8903. goto fail;
  8904. if (possible_crtc->state->enable) {
  8905. drm_modeset_unlock(&possible_crtc->mutex);
  8906. continue;
  8907. }
  8908. crtc = possible_crtc;
  8909. break;
  8910. }
  8911. /*
  8912. * If we didn't find an unused CRTC, don't use any.
  8913. */
  8914. if (!crtc) {
  8915. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8916. goto fail;
  8917. }
  8918. found:
  8919. intel_crtc = to_intel_crtc(crtc);
  8920. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8921. if (ret)
  8922. goto fail;
  8923. state = drm_atomic_state_alloc(dev);
  8924. restore_state = drm_atomic_state_alloc(dev);
  8925. if (!state || !restore_state) {
  8926. ret = -ENOMEM;
  8927. goto fail;
  8928. }
  8929. state->acquire_ctx = ctx;
  8930. restore_state->acquire_ctx = ctx;
  8931. connector_state = drm_atomic_get_connector_state(state, connector);
  8932. if (IS_ERR(connector_state)) {
  8933. ret = PTR_ERR(connector_state);
  8934. goto fail;
  8935. }
  8936. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8937. if (ret)
  8938. goto fail;
  8939. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8940. if (IS_ERR(crtc_state)) {
  8941. ret = PTR_ERR(crtc_state);
  8942. goto fail;
  8943. }
  8944. crtc_state->base.active = crtc_state->base.enable = true;
  8945. if (!mode)
  8946. mode = &load_detect_mode;
  8947. /* We need a framebuffer large enough to accommodate all accesses
  8948. * that the plane may generate whilst we perform load detection.
  8949. * We can not rely on the fbcon either being present (we get called
  8950. * during its initialisation to detect all boot displays, or it may
  8951. * not even exist) or that it is large enough to satisfy the
  8952. * requested mode.
  8953. */
  8954. fb = mode_fits_in_fbdev(dev, mode);
  8955. if (fb == NULL) {
  8956. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8957. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8958. } else
  8959. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8960. if (IS_ERR(fb)) {
  8961. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8962. goto fail;
  8963. }
  8964. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8965. if (ret)
  8966. goto fail;
  8967. drm_framebuffer_unreference(fb);
  8968. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8969. if (ret)
  8970. goto fail;
  8971. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8972. if (!ret)
  8973. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8974. if (!ret)
  8975. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8976. if (ret) {
  8977. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8978. goto fail;
  8979. }
  8980. ret = drm_atomic_commit(state);
  8981. if (ret) {
  8982. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8983. goto fail;
  8984. }
  8985. old->restore_state = restore_state;
  8986. /* let the connector get through one full cycle before testing */
  8987. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8988. return true;
  8989. fail:
  8990. drm_atomic_state_free(state);
  8991. drm_atomic_state_free(restore_state);
  8992. restore_state = state = NULL;
  8993. if (ret == -EDEADLK) {
  8994. drm_modeset_backoff(ctx);
  8995. goto retry;
  8996. }
  8997. return false;
  8998. }
  8999. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9000. struct intel_load_detect_pipe *old,
  9001. struct drm_modeset_acquire_ctx *ctx)
  9002. {
  9003. struct intel_encoder *intel_encoder =
  9004. intel_attached_encoder(connector);
  9005. struct drm_encoder *encoder = &intel_encoder->base;
  9006. struct drm_atomic_state *state = old->restore_state;
  9007. int ret;
  9008. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9009. connector->base.id, connector->name,
  9010. encoder->base.id, encoder->name);
  9011. if (!state)
  9012. return;
  9013. ret = drm_atomic_commit(state);
  9014. if (ret) {
  9015. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9016. drm_atomic_state_free(state);
  9017. }
  9018. }
  9019. static int i9xx_pll_refclk(struct drm_device *dev,
  9020. const struct intel_crtc_state *pipe_config)
  9021. {
  9022. struct drm_i915_private *dev_priv = dev->dev_private;
  9023. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9024. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9025. return dev_priv->vbt.lvds_ssc_freq;
  9026. else if (HAS_PCH_SPLIT(dev))
  9027. return 120000;
  9028. else if (!IS_GEN2(dev))
  9029. return 96000;
  9030. else
  9031. return 48000;
  9032. }
  9033. /* Returns the clock of the currently programmed mode of the given pipe. */
  9034. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9035. struct intel_crtc_state *pipe_config)
  9036. {
  9037. struct drm_device *dev = crtc->base.dev;
  9038. struct drm_i915_private *dev_priv = dev->dev_private;
  9039. int pipe = pipe_config->cpu_transcoder;
  9040. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9041. u32 fp;
  9042. struct dpll clock;
  9043. int port_clock;
  9044. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9045. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9046. fp = pipe_config->dpll_hw_state.fp0;
  9047. else
  9048. fp = pipe_config->dpll_hw_state.fp1;
  9049. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9050. if (IS_PINEVIEW(dev)) {
  9051. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9052. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9053. } else {
  9054. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9055. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9056. }
  9057. if (!IS_GEN2(dev)) {
  9058. if (IS_PINEVIEW(dev))
  9059. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9060. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9061. else
  9062. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9063. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9064. switch (dpll & DPLL_MODE_MASK) {
  9065. case DPLLB_MODE_DAC_SERIAL:
  9066. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9067. 5 : 10;
  9068. break;
  9069. case DPLLB_MODE_LVDS:
  9070. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9071. 7 : 14;
  9072. break;
  9073. default:
  9074. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9075. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9076. return;
  9077. }
  9078. if (IS_PINEVIEW(dev))
  9079. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9080. else
  9081. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9082. } else {
  9083. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9084. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9085. if (is_lvds) {
  9086. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9087. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9088. if (lvds & LVDS_CLKB_POWER_UP)
  9089. clock.p2 = 7;
  9090. else
  9091. clock.p2 = 14;
  9092. } else {
  9093. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9094. clock.p1 = 2;
  9095. else {
  9096. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9097. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9098. }
  9099. if (dpll & PLL_P2_DIVIDE_BY_4)
  9100. clock.p2 = 4;
  9101. else
  9102. clock.p2 = 2;
  9103. }
  9104. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9105. }
  9106. /*
  9107. * This value includes pixel_multiplier. We will use
  9108. * port_clock to compute adjusted_mode.crtc_clock in the
  9109. * encoder's get_config() function.
  9110. */
  9111. pipe_config->port_clock = port_clock;
  9112. }
  9113. int intel_dotclock_calculate(int link_freq,
  9114. const struct intel_link_m_n *m_n)
  9115. {
  9116. /*
  9117. * The calculation for the data clock is:
  9118. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9119. * But we want to avoid losing precison if possible, so:
  9120. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9121. *
  9122. * and the link clock is simpler:
  9123. * link_clock = (m * link_clock) / n
  9124. */
  9125. if (!m_n->link_n)
  9126. return 0;
  9127. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9128. }
  9129. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9130. struct intel_crtc_state *pipe_config)
  9131. {
  9132. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9133. /* read out port_clock from the DPLL */
  9134. i9xx_crtc_clock_get(crtc, pipe_config);
  9135. /*
  9136. * In case there is an active pipe without active ports,
  9137. * we may need some idea for the dotclock anyway.
  9138. * Calculate one based on the FDI configuration.
  9139. */
  9140. pipe_config->base.adjusted_mode.crtc_clock =
  9141. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9142. &pipe_config->fdi_m_n);
  9143. }
  9144. /** Returns the currently programmed mode of the given pipe. */
  9145. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9146. struct drm_crtc *crtc)
  9147. {
  9148. struct drm_i915_private *dev_priv = dev->dev_private;
  9149. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9150. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9151. struct drm_display_mode *mode;
  9152. struct intel_crtc_state *pipe_config;
  9153. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9154. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9155. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9156. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9157. enum pipe pipe = intel_crtc->pipe;
  9158. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9159. if (!mode)
  9160. return NULL;
  9161. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9162. if (!pipe_config) {
  9163. kfree(mode);
  9164. return NULL;
  9165. }
  9166. /*
  9167. * Construct a pipe_config sufficient for getting the clock info
  9168. * back out of crtc_clock_get.
  9169. *
  9170. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9171. * to use a real value here instead.
  9172. */
  9173. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9174. pipe_config->pixel_multiplier = 1;
  9175. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9176. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9177. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9178. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9179. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9180. mode->hdisplay = (htot & 0xffff) + 1;
  9181. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9182. mode->hsync_start = (hsync & 0xffff) + 1;
  9183. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9184. mode->vdisplay = (vtot & 0xffff) + 1;
  9185. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9186. mode->vsync_start = (vsync & 0xffff) + 1;
  9187. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9188. drm_mode_set_name(mode);
  9189. kfree(pipe_config);
  9190. return mode;
  9191. }
  9192. void intel_mark_busy(struct drm_i915_private *dev_priv)
  9193. {
  9194. if (dev_priv->mm.busy)
  9195. return;
  9196. intel_runtime_pm_get(dev_priv);
  9197. i915_update_gfx_val(dev_priv);
  9198. if (INTEL_GEN(dev_priv) >= 6)
  9199. gen6_rps_busy(dev_priv);
  9200. dev_priv->mm.busy = true;
  9201. }
  9202. void intel_mark_idle(struct drm_i915_private *dev_priv)
  9203. {
  9204. if (!dev_priv->mm.busy)
  9205. return;
  9206. dev_priv->mm.busy = false;
  9207. if (INTEL_GEN(dev_priv) >= 6)
  9208. gen6_rps_idle(dev_priv);
  9209. intel_runtime_pm_put(dev_priv);
  9210. }
  9211. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9212. {
  9213. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9214. struct drm_device *dev = crtc->dev;
  9215. struct intel_flip_work *work;
  9216. spin_lock_irq(&dev->event_lock);
  9217. work = intel_crtc->flip_work;
  9218. intel_crtc->flip_work = NULL;
  9219. spin_unlock_irq(&dev->event_lock);
  9220. if (work) {
  9221. cancel_work_sync(&work->mmio_work);
  9222. cancel_work_sync(&work->unpin_work);
  9223. kfree(work);
  9224. }
  9225. drm_crtc_cleanup(crtc);
  9226. kfree(intel_crtc);
  9227. }
  9228. static void intel_unpin_work_fn(struct work_struct *__work)
  9229. {
  9230. struct intel_flip_work *work =
  9231. container_of(__work, struct intel_flip_work, unpin_work);
  9232. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9233. struct drm_device *dev = crtc->base.dev;
  9234. struct drm_plane *primary = crtc->base.primary;
  9235. if (is_mmio_work(work))
  9236. flush_work(&work->mmio_work);
  9237. mutex_lock(&dev->struct_mutex);
  9238. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9239. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9240. if (work->flip_queued_req)
  9241. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9242. mutex_unlock(&dev->struct_mutex);
  9243. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9244. intel_fbc_post_update(crtc);
  9245. drm_framebuffer_unreference(work->old_fb);
  9246. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9247. atomic_dec(&crtc->unpin_work_count);
  9248. kfree(work);
  9249. }
  9250. /* Is 'a' after or equal to 'b'? */
  9251. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9252. {
  9253. return !((a - b) & 0x80000000);
  9254. }
  9255. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9256. struct intel_flip_work *work)
  9257. {
  9258. struct drm_device *dev = crtc->base.dev;
  9259. struct drm_i915_private *dev_priv = dev->dev_private;
  9260. unsigned reset_counter;
  9261. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9262. if (crtc->reset_counter != reset_counter)
  9263. return true;
  9264. /*
  9265. * The relevant registers doen't exist on pre-ctg.
  9266. * As the flip done interrupt doesn't trigger for mmio
  9267. * flips on gmch platforms, a flip count check isn't
  9268. * really needed there. But since ctg has the registers,
  9269. * include it in the check anyway.
  9270. */
  9271. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9272. return true;
  9273. /*
  9274. * BDW signals flip done immediately if the plane
  9275. * is disabled, even if the plane enable is already
  9276. * armed to occur at the next vblank :(
  9277. */
  9278. /*
  9279. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9280. * used the same base address. In that case the mmio flip might
  9281. * have completed, but the CS hasn't even executed the flip yet.
  9282. *
  9283. * A flip count check isn't enough as the CS might have updated
  9284. * the base address just after start of vblank, but before we
  9285. * managed to process the interrupt. This means we'd complete the
  9286. * CS flip too soon.
  9287. *
  9288. * Combining both checks should get us a good enough result. It may
  9289. * still happen that the CS flip has been executed, but has not
  9290. * yet actually completed. But in case the base address is the same
  9291. * anyway, we don't really care.
  9292. */
  9293. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9294. crtc->flip_work->gtt_offset &&
  9295. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9296. crtc->flip_work->flip_count);
  9297. }
  9298. static bool
  9299. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9300. struct intel_flip_work *work)
  9301. {
  9302. /*
  9303. * MMIO work completes when vblank is different from
  9304. * flip_queued_vblank.
  9305. *
  9306. * Reset counter value doesn't matter, this is handled by
  9307. * i915_wait_request finishing early, so no need to handle
  9308. * reset here.
  9309. */
  9310. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9311. }
  9312. static bool pageflip_finished(struct intel_crtc *crtc,
  9313. struct intel_flip_work *work)
  9314. {
  9315. if (!atomic_read(&work->pending))
  9316. return false;
  9317. smp_rmb();
  9318. if (is_mmio_work(work))
  9319. return __pageflip_finished_mmio(crtc, work);
  9320. else
  9321. return __pageflip_finished_cs(crtc, work);
  9322. }
  9323. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9324. {
  9325. struct drm_device *dev = dev_priv->dev;
  9326. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9328. struct intel_flip_work *work;
  9329. unsigned long flags;
  9330. /* Ignore early vblank irqs */
  9331. if (!crtc)
  9332. return;
  9333. /*
  9334. * This is called both by irq handlers and the reset code (to complete
  9335. * lost pageflips) so needs the full irqsave spinlocks.
  9336. */
  9337. spin_lock_irqsave(&dev->event_lock, flags);
  9338. work = intel_crtc->flip_work;
  9339. if (work != NULL &&
  9340. !is_mmio_work(work) &&
  9341. pageflip_finished(intel_crtc, work))
  9342. page_flip_completed(intel_crtc);
  9343. spin_unlock_irqrestore(&dev->event_lock, flags);
  9344. }
  9345. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9346. {
  9347. struct drm_device *dev = dev_priv->dev;
  9348. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9349. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9350. struct intel_flip_work *work;
  9351. unsigned long flags;
  9352. /* Ignore early vblank irqs */
  9353. if (!crtc)
  9354. return;
  9355. /*
  9356. * This is called both by irq handlers and the reset code (to complete
  9357. * lost pageflips) so needs the full irqsave spinlocks.
  9358. */
  9359. spin_lock_irqsave(&dev->event_lock, flags);
  9360. work = intel_crtc->flip_work;
  9361. if (work != NULL &&
  9362. is_mmio_work(work) &&
  9363. pageflip_finished(intel_crtc, work))
  9364. page_flip_completed(intel_crtc);
  9365. spin_unlock_irqrestore(&dev->event_lock, flags);
  9366. }
  9367. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9368. struct intel_flip_work *work)
  9369. {
  9370. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9371. /* Ensure that the work item is consistent when activating it ... */
  9372. smp_mb__before_atomic();
  9373. atomic_set(&work->pending, 1);
  9374. }
  9375. static int intel_gen2_queue_flip(struct drm_device *dev,
  9376. struct drm_crtc *crtc,
  9377. struct drm_framebuffer *fb,
  9378. struct drm_i915_gem_object *obj,
  9379. struct drm_i915_gem_request *req,
  9380. uint32_t flags)
  9381. {
  9382. struct intel_engine_cs *engine = req->engine;
  9383. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9384. u32 flip_mask;
  9385. int ret;
  9386. ret = intel_ring_begin(req, 6);
  9387. if (ret)
  9388. return ret;
  9389. /* Can't queue multiple flips, so wait for the previous
  9390. * one to finish before executing the next.
  9391. */
  9392. if (intel_crtc->plane)
  9393. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9394. else
  9395. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9396. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9397. intel_ring_emit(engine, MI_NOOP);
  9398. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9399. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9400. intel_ring_emit(engine, fb->pitches[0]);
  9401. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9402. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9403. return 0;
  9404. }
  9405. static int intel_gen3_queue_flip(struct drm_device *dev,
  9406. struct drm_crtc *crtc,
  9407. struct drm_framebuffer *fb,
  9408. struct drm_i915_gem_object *obj,
  9409. struct drm_i915_gem_request *req,
  9410. uint32_t flags)
  9411. {
  9412. struct intel_engine_cs *engine = req->engine;
  9413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9414. u32 flip_mask;
  9415. int ret;
  9416. ret = intel_ring_begin(req, 6);
  9417. if (ret)
  9418. return ret;
  9419. if (intel_crtc->plane)
  9420. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9421. else
  9422. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9423. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9424. intel_ring_emit(engine, MI_NOOP);
  9425. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9426. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9427. intel_ring_emit(engine, fb->pitches[0]);
  9428. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9429. intel_ring_emit(engine, MI_NOOP);
  9430. return 0;
  9431. }
  9432. static int intel_gen4_queue_flip(struct drm_device *dev,
  9433. struct drm_crtc *crtc,
  9434. struct drm_framebuffer *fb,
  9435. struct drm_i915_gem_object *obj,
  9436. struct drm_i915_gem_request *req,
  9437. uint32_t flags)
  9438. {
  9439. struct intel_engine_cs *engine = req->engine;
  9440. struct drm_i915_private *dev_priv = dev->dev_private;
  9441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9442. uint32_t pf, pipesrc;
  9443. int ret;
  9444. ret = intel_ring_begin(req, 4);
  9445. if (ret)
  9446. return ret;
  9447. /* i965+ uses the linear or tiled offsets from the
  9448. * Display Registers (which do not change across a page-flip)
  9449. * so we need only reprogram the base address.
  9450. */
  9451. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9452. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9453. intel_ring_emit(engine, fb->pitches[0]);
  9454. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
  9455. obj->tiling_mode);
  9456. /* XXX Enabling the panel-fitter across page-flip is so far
  9457. * untested on non-native modes, so ignore it for now.
  9458. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9459. */
  9460. pf = 0;
  9461. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9462. intel_ring_emit(engine, pf | pipesrc);
  9463. return 0;
  9464. }
  9465. static int intel_gen6_queue_flip(struct drm_device *dev,
  9466. struct drm_crtc *crtc,
  9467. struct drm_framebuffer *fb,
  9468. struct drm_i915_gem_object *obj,
  9469. struct drm_i915_gem_request *req,
  9470. uint32_t flags)
  9471. {
  9472. struct intel_engine_cs *engine = req->engine;
  9473. struct drm_i915_private *dev_priv = dev->dev_private;
  9474. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9475. uint32_t pf, pipesrc;
  9476. int ret;
  9477. ret = intel_ring_begin(req, 4);
  9478. if (ret)
  9479. return ret;
  9480. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9481. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9482. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9483. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9484. /* Contrary to the suggestions in the documentation,
  9485. * "Enable Panel Fitter" does not seem to be required when page
  9486. * flipping with a non-native mode, and worse causes a normal
  9487. * modeset to fail.
  9488. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9489. */
  9490. pf = 0;
  9491. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9492. intel_ring_emit(engine, pf | pipesrc);
  9493. return 0;
  9494. }
  9495. static int intel_gen7_queue_flip(struct drm_device *dev,
  9496. struct drm_crtc *crtc,
  9497. struct drm_framebuffer *fb,
  9498. struct drm_i915_gem_object *obj,
  9499. struct drm_i915_gem_request *req,
  9500. uint32_t flags)
  9501. {
  9502. struct intel_engine_cs *engine = req->engine;
  9503. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9504. uint32_t plane_bit = 0;
  9505. int len, ret;
  9506. switch (intel_crtc->plane) {
  9507. case PLANE_A:
  9508. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9509. break;
  9510. case PLANE_B:
  9511. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9512. break;
  9513. case PLANE_C:
  9514. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9515. break;
  9516. default:
  9517. WARN_ONCE(1, "unknown plane in flip command\n");
  9518. return -ENODEV;
  9519. }
  9520. len = 4;
  9521. if (engine->id == RCS) {
  9522. len += 6;
  9523. /*
  9524. * On Gen 8, SRM is now taking an extra dword to accommodate
  9525. * 48bits addresses, and we need a NOOP for the batch size to
  9526. * stay even.
  9527. */
  9528. if (IS_GEN8(dev))
  9529. len += 2;
  9530. }
  9531. /*
  9532. * BSpec MI_DISPLAY_FLIP for IVB:
  9533. * "The full packet must be contained within the same cache line."
  9534. *
  9535. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9536. * cacheline, if we ever start emitting more commands before
  9537. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9538. * then do the cacheline alignment, and finally emit the
  9539. * MI_DISPLAY_FLIP.
  9540. */
  9541. ret = intel_ring_cacheline_align(req);
  9542. if (ret)
  9543. return ret;
  9544. ret = intel_ring_begin(req, len);
  9545. if (ret)
  9546. return ret;
  9547. /* Unmask the flip-done completion message. Note that the bspec says that
  9548. * we should do this for both the BCS and RCS, and that we must not unmask
  9549. * more than one flip event at any time (or ensure that one flip message
  9550. * can be sent by waiting for flip-done prior to queueing new flips).
  9551. * Experimentation says that BCS works despite DERRMR masking all
  9552. * flip-done completion events and that unmasking all planes at once
  9553. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9554. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9555. */
  9556. if (engine->id == RCS) {
  9557. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9558. intel_ring_emit_reg(engine, DERRMR);
  9559. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9560. DERRMR_PIPEB_PRI_FLIP_DONE |
  9561. DERRMR_PIPEC_PRI_FLIP_DONE));
  9562. if (IS_GEN8(dev))
  9563. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9564. MI_SRM_LRM_GLOBAL_GTT);
  9565. else
  9566. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9567. MI_SRM_LRM_GLOBAL_GTT);
  9568. intel_ring_emit_reg(engine, DERRMR);
  9569. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9570. if (IS_GEN8(dev)) {
  9571. intel_ring_emit(engine, 0);
  9572. intel_ring_emit(engine, MI_NOOP);
  9573. }
  9574. }
  9575. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9576. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9577. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9578. intel_ring_emit(engine, (MI_NOOP));
  9579. return 0;
  9580. }
  9581. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9582. struct drm_i915_gem_object *obj)
  9583. {
  9584. /*
  9585. * This is not being used for older platforms, because
  9586. * non-availability of flip done interrupt forces us to use
  9587. * CS flips. Older platforms derive flip done using some clever
  9588. * tricks involving the flip_pending status bits and vblank irqs.
  9589. * So using MMIO flips there would disrupt this mechanism.
  9590. */
  9591. if (engine == NULL)
  9592. return true;
  9593. if (INTEL_GEN(engine->i915) < 5)
  9594. return false;
  9595. if (i915.use_mmio_flip < 0)
  9596. return false;
  9597. else if (i915.use_mmio_flip > 0)
  9598. return true;
  9599. else if (i915.enable_execlists)
  9600. return true;
  9601. else if (obj->base.dma_buf &&
  9602. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9603. false))
  9604. return true;
  9605. else
  9606. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9607. }
  9608. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9609. unsigned int rotation,
  9610. struct intel_flip_work *work)
  9611. {
  9612. struct drm_device *dev = intel_crtc->base.dev;
  9613. struct drm_i915_private *dev_priv = dev->dev_private;
  9614. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9615. const enum pipe pipe = intel_crtc->pipe;
  9616. u32 ctl, stride, tile_height;
  9617. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9618. ctl &= ~PLANE_CTL_TILED_MASK;
  9619. switch (fb->modifier[0]) {
  9620. case DRM_FORMAT_MOD_NONE:
  9621. break;
  9622. case I915_FORMAT_MOD_X_TILED:
  9623. ctl |= PLANE_CTL_TILED_X;
  9624. break;
  9625. case I915_FORMAT_MOD_Y_TILED:
  9626. ctl |= PLANE_CTL_TILED_Y;
  9627. break;
  9628. case I915_FORMAT_MOD_Yf_TILED:
  9629. ctl |= PLANE_CTL_TILED_YF;
  9630. break;
  9631. default:
  9632. MISSING_CASE(fb->modifier[0]);
  9633. }
  9634. /*
  9635. * The stride is either expressed as a multiple of 64 bytes chunks for
  9636. * linear buffers or in number of tiles for tiled buffers.
  9637. */
  9638. if (intel_rotation_90_or_270(rotation)) {
  9639. /* stride = Surface height in tiles */
  9640. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9641. stride = DIV_ROUND_UP(fb->height, tile_height);
  9642. } else {
  9643. stride = fb->pitches[0] /
  9644. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9645. fb->pixel_format);
  9646. }
  9647. /*
  9648. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9649. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9650. */
  9651. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9652. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9653. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9654. POSTING_READ(PLANE_SURF(pipe, 0));
  9655. }
  9656. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9657. struct intel_flip_work *work)
  9658. {
  9659. struct drm_device *dev = intel_crtc->base.dev;
  9660. struct drm_i915_private *dev_priv = dev->dev_private;
  9661. struct intel_framebuffer *intel_fb =
  9662. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9663. struct drm_i915_gem_object *obj = intel_fb->obj;
  9664. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9665. u32 dspcntr;
  9666. dspcntr = I915_READ(reg);
  9667. if (obj->tiling_mode != I915_TILING_NONE)
  9668. dspcntr |= DISPPLANE_TILED;
  9669. else
  9670. dspcntr &= ~DISPPLANE_TILED;
  9671. I915_WRITE(reg, dspcntr);
  9672. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9673. POSTING_READ(DSPSURF(intel_crtc->plane));
  9674. }
  9675. static void intel_mmio_flip_work_func(struct work_struct *w)
  9676. {
  9677. struct intel_flip_work *work =
  9678. container_of(w, struct intel_flip_work, mmio_work);
  9679. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9680. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9681. struct intel_framebuffer *intel_fb =
  9682. to_intel_framebuffer(crtc->base.primary->fb);
  9683. struct drm_i915_gem_object *obj = intel_fb->obj;
  9684. if (work->flip_queued_req)
  9685. WARN_ON(__i915_wait_request(work->flip_queued_req,
  9686. false, NULL,
  9687. &dev_priv->rps.mmioflips));
  9688. /* For framebuffer backed by dmabuf, wait for fence */
  9689. if (obj->base.dma_buf)
  9690. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9691. false, false,
  9692. MAX_SCHEDULE_TIMEOUT) < 0);
  9693. intel_pipe_update_start(crtc);
  9694. if (INTEL_GEN(dev_priv) >= 9)
  9695. skl_do_mmio_flip(crtc, work->rotation, work);
  9696. else
  9697. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9698. ilk_do_mmio_flip(crtc, work);
  9699. intel_pipe_update_end(crtc, work);
  9700. }
  9701. static int intel_default_queue_flip(struct drm_device *dev,
  9702. struct drm_crtc *crtc,
  9703. struct drm_framebuffer *fb,
  9704. struct drm_i915_gem_object *obj,
  9705. struct drm_i915_gem_request *req,
  9706. uint32_t flags)
  9707. {
  9708. return -ENODEV;
  9709. }
  9710. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9711. struct intel_crtc *intel_crtc,
  9712. struct intel_flip_work *work)
  9713. {
  9714. u32 addr, vblank;
  9715. if (!atomic_read(&work->pending))
  9716. return false;
  9717. smp_rmb();
  9718. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9719. if (work->flip_ready_vblank == 0) {
  9720. if (work->flip_queued_req &&
  9721. !i915_gem_request_completed(work->flip_queued_req, true))
  9722. return false;
  9723. work->flip_ready_vblank = vblank;
  9724. }
  9725. if (vblank - work->flip_ready_vblank < 3)
  9726. return false;
  9727. /* Potential stall - if we see that the flip has happened,
  9728. * assume a missed interrupt. */
  9729. if (INTEL_GEN(dev_priv) >= 4)
  9730. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9731. else
  9732. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9733. /* There is a potential issue here with a false positive after a flip
  9734. * to the same address. We could address this by checking for a
  9735. * non-incrementing frame counter.
  9736. */
  9737. return addr == work->gtt_offset;
  9738. }
  9739. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9740. {
  9741. struct drm_device *dev = dev_priv->dev;
  9742. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9743. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9744. struct intel_flip_work *work;
  9745. WARN_ON(!in_interrupt());
  9746. if (crtc == NULL)
  9747. return;
  9748. spin_lock(&dev->event_lock);
  9749. work = intel_crtc->flip_work;
  9750. if (work != NULL && !is_mmio_work(work) &&
  9751. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9752. WARN_ONCE(1,
  9753. "Kicking stuck page flip: queued at %d, now %d\n",
  9754. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9755. page_flip_completed(intel_crtc);
  9756. work = NULL;
  9757. }
  9758. if (work != NULL && !is_mmio_work(work) &&
  9759. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9760. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9761. spin_unlock(&dev->event_lock);
  9762. }
  9763. __maybe_unused
  9764. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9765. struct drm_framebuffer *fb,
  9766. struct drm_pending_vblank_event *event,
  9767. uint32_t page_flip_flags)
  9768. {
  9769. struct drm_device *dev = crtc->dev;
  9770. struct drm_i915_private *dev_priv = dev->dev_private;
  9771. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9772. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9773. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9774. struct drm_plane *primary = crtc->primary;
  9775. enum pipe pipe = intel_crtc->pipe;
  9776. struct intel_flip_work *work;
  9777. struct intel_engine_cs *engine;
  9778. bool mmio_flip;
  9779. struct drm_i915_gem_request *request = NULL;
  9780. int ret;
  9781. /*
  9782. * drm_mode_page_flip_ioctl() should already catch this, but double
  9783. * check to be safe. In the future we may enable pageflipping from
  9784. * a disabled primary plane.
  9785. */
  9786. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9787. return -EBUSY;
  9788. /* Can't change pixel format via MI display flips. */
  9789. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9790. return -EINVAL;
  9791. /*
  9792. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9793. * Note that pitch changes could also affect these register.
  9794. */
  9795. if (INTEL_INFO(dev)->gen > 3 &&
  9796. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9797. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9798. return -EINVAL;
  9799. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9800. goto out_hang;
  9801. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9802. if (work == NULL)
  9803. return -ENOMEM;
  9804. work->event = event;
  9805. work->crtc = crtc;
  9806. work->old_fb = old_fb;
  9807. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9808. ret = drm_crtc_vblank_get(crtc);
  9809. if (ret)
  9810. goto free_work;
  9811. /* We borrow the event spin lock for protecting flip_work */
  9812. spin_lock_irq(&dev->event_lock);
  9813. if (intel_crtc->flip_work) {
  9814. /* Before declaring the flip queue wedged, check if
  9815. * the hardware completed the operation behind our backs.
  9816. */
  9817. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9818. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9819. page_flip_completed(intel_crtc);
  9820. } else {
  9821. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9822. spin_unlock_irq(&dev->event_lock);
  9823. drm_crtc_vblank_put(crtc);
  9824. kfree(work);
  9825. return -EBUSY;
  9826. }
  9827. }
  9828. intel_crtc->flip_work = work;
  9829. spin_unlock_irq(&dev->event_lock);
  9830. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9831. flush_workqueue(dev_priv->wq);
  9832. /* Reference the objects for the scheduled work. */
  9833. drm_framebuffer_reference(work->old_fb);
  9834. drm_gem_object_reference(&obj->base);
  9835. crtc->primary->fb = fb;
  9836. update_state_fb(crtc->primary);
  9837. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9838. to_intel_plane_state(primary->state));
  9839. work->pending_flip_obj = obj;
  9840. ret = i915_mutex_lock_interruptible(dev);
  9841. if (ret)
  9842. goto cleanup;
  9843. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9844. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9845. ret = -EIO;
  9846. goto cleanup;
  9847. }
  9848. atomic_inc(&intel_crtc->unpin_work_count);
  9849. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9850. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9851. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9852. engine = &dev_priv->engine[BCS];
  9853. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9854. /* vlv: DISPLAY_FLIP fails to change tiling */
  9855. engine = NULL;
  9856. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9857. engine = &dev_priv->engine[BCS];
  9858. } else if (INTEL_INFO(dev)->gen >= 7) {
  9859. engine = i915_gem_request_get_engine(obj->last_write_req);
  9860. if (engine == NULL || engine->id != RCS)
  9861. engine = &dev_priv->engine[BCS];
  9862. } else {
  9863. engine = &dev_priv->engine[RCS];
  9864. }
  9865. mmio_flip = use_mmio_flip(engine, obj);
  9866. /* When using CS flips, we want to emit semaphores between rings.
  9867. * However, when using mmio flips we will create a task to do the
  9868. * synchronisation, so all we want here is to pin the framebuffer
  9869. * into the display plane and skip any waits.
  9870. */
  9871. if (!mmio_flip) {
  9872. ret = i915_gem_object_sync(obj, engine, &request);
  9873. if (!ret && !request) {
  9874. request = i915_gem_request_alloc(engine, NULL);
  9875. ret = PTR_ERR_OR_ZERO(request);
  9876. }
  9877. if (ret)
  9878. goto cleanup_pending;
  9879. }
  9880. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9881. if (ret)
  9882. goto cleanup_pending;
  9883. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9884. obj, 0);
  9885. work->gtt_offset += intel_crtc->dspaddr_offset;
  9886. work->rotation = crtc->primary->state->rotation;
  9887. if (mmio_flip) {
  9888. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9889. i915_gem_request_assign(&work->flip_queued_req,
  9890. obj->last_write_req);
  9891. schedule_work(&work->mmio_work);
  9892. } else {
  9893. i915_gem_request_assign(&work->flip_queued_req, request);
  9894. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9895. page_flip_flags);
  9896. if (ret)
  9897. goto cleanup_unpin;
  9898. intel_mark_page_flip_active(intel_crtc, work);
  9899. i915_add_request_no_flush(request);
  9900. }
  9901. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9902. to_intel_plane(primary)->frontbuffer_bit);
  9903. mutex_unlock(&dev->struct_mutex);
  9904. intel_frontbuffer_flip_prepare(dev,
  9905. to_intel_plane(primary)->frontbuffer_bit);
  9906. trace_i915_flip_request(intel_crtc->plane, obj);
  9907. return 0;
  9908. cleanup_unpin:
  9909. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9910. cleanup_pending:
  9911. if (!IS_ERR_OR_NULL(request))
  9912. i915_add_request_no_flush(request);
  9913. atomic_dec(&intel_crtc->unpin_work_count);
  9914. mutex_unlock(&dev->struct_mutex);
  9915. cleanup:
  9916. crtc->primary->fb = old_fb;
  9917. update_state_fb(crtc->primary);
  9918. drm_gem_object_unreference_unlocked(&obj->base);
  9919. drm_framebuffer_unreference(work->old_fb);
  9920. spin_lock_irq(&dev->event_lock);
  9921. intel_crtc->flip_work = NULL;
  9922. spin_unlock_irq(&dev->event_lock);
  9923. drm_crtc_vblank_put(crtc);
  9924. free_work:
  9925. kfree(work);
  9926. if (ret == -EIO) {
  9927. struct drm_atomic_state *state;
  9928. struct drm_plane_state *plane_state;
  9929. out_hang:
  9930. state = drm_atomic_state_alloc(dev);
  9931. if (!state)
  9932. return -ENOMEM;
  9933. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9934. retry:
  9935. plane_state = drm_atomic_get_plane_state(state, primary);
  9936. ret = PTR_ERR_OR_ZERO(plane_state);
  9937. if (!ret) {
  9938. drm_atomic_set_fb_for_plane(plane_state, fb);
  9939. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9940. if (!ret)
  9941. ret = drm_atomic_commit(state);
  9942. }
  9943. if (ret == -EDEADLK) {
  9944. drm_modeset_backoff(state->acquire_ctx);
  9945. drm_atomic_state_clear(state);
  9946. goto retry;
  9947. }
  9948. if (ret)
  9949. drm_atomic_state_free(state);
  9950. if (ret == 0 && event) {
  9951. spin_lock_irq(&dev->event_lock);
  9952. drm_crtc_send_vblank_event(crtc, event);
  9953. spin_unlock_irq(&dev->event_lock);
  9954. }
  9955. }
  9956. return ret;
  9957. }
  9958. /**
  9959. * intel_wm_need_update - Check whether watermarks need updating
  9960. * @plane: drm plane
  9961. * @state: new plane state
  9962. *
  9963. * Check current plane state versus the new one to determine whether
  9964. * watermarks need to be recalculated.
  9965. *
  9966. * Returns true or false.
  9967. */
  9968. static bool intel_wm_need_update(struct drm_plane *plane,
  9969. struct drm_plane_state *state)
  9970. {
  9971. struct intel_plane_state *new = to_intel_plane_state(state);
  9972. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9973. /* Update watermarks on tiling or size changes. */
  9974. if (new->visible != cur->visible)
  9975. return true;
  9976. if (!cur->base.fb || !new->base.fb)
  9977. return false;
  9978. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9979. cur->base.rotation != new->base.rotation ||
  9980. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9981. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9982. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9983. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9984. return true;
  9985. return false;
  9986. }
  9987. static bool needs_scaling(struct intel_plane_state *state)
  9988. {
  9989. int src_w = drm_rect_width(&state->src) >> 16;
  9990. int src_h = drm_rect_height(&state->src) >> 16;
  9991. int dst_w = drm_rect_width(&state->dst);
  9992. int dst_h = drm_rect_height(&state->dst);
  9993. return (src_w != dst_w || src_h != dst_h);
  9994. }
  9995. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9996. struct drm_plane_state *plane_state)
  9997. {
  9998. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9999. struct drm_crtc *crtc = crtc_state->crtc;
  10000. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10001. struct drm_plane *plane = plane_state->plane;
  10002. struct drm_device *dev = crtc->dev;
  10003. struct drm_i915_private *dev_priv = to_i915(dev);
  10004. struct intel_plane_state *old_plane_state =
  10005. to_intel_plane_state(plane->state);
  10006. bool mode_changed = needs_modeset(crtc_state);
  10007. bool was_crtc_enabled = crtc->state->active;
  10008. bool is_crtc_enabled = crtc_state->active;
  10009. bool turn_off, turn_on, visible, was_visible;
  10010. struct drm_framebuffer *fb = plane_state->fb;
  10011. int ret;
  10012. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  10013. plane->type != DRM_PLANE_TYPE_CURSOR) {
  10014. ret = skl_update_scaler_plane(
  10015. to_intel_crtc_state(crtc_state),
  10016. to_intel_plane_state(plane_state));
  10017. if (ret)
  10018. return ret;
  10019. }
  10020. was_visible = old_plane_state->visible;
  10021. visible = to_intel_plane_state(plane_state)->visible;
  10022. if (!was_crtc_enabled && WARN_ON(was_visible))
  10023. was_visible = false;
  10024. /*
  10025. * Visibility is calculated as if the crtc was on, but
  10026. * after scaler setup everything depends on it being off
  10027. * when the crtc isn't active.
  10028. *
  10029. * FIXME this is wrong for watermarks. Watermarks should also
  10030. * be computed as if the pipe would be active. Perhaps move
  10031. * per-plane wm computation to the .check_plane() hook, and
  10032. * only combine the results from all planes in the current place?
  10033. */
  10034. if (!is_crtc_enabled)
  10035. to_intel_plane_state(plane_state)->visible = visible = false;
  10036. if (!was_visible && !visible)
  10037. return 0;
  10038. if (fb != old_plane_state->base.fb)
  10039. pipe_config->fb_changed = true;
  10040. turn_off = was_visible && (!visible || mode_changed);
  10041. turn_on = visible && (!was_visible || mode_changed);
  10042. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10043. intel_crtc->base.base.id,
  10044. intel_crtc->base.name,
  10045. plane->base.id, plane->name,
  10046. fb ? fb->base.id : -1);
  10047. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10048. plane->base.id, plane->name,
  10049. was_visible, visible,
  10050. turn_off, turn_on, mode_changed);
  10051. if (turn_on) {
  10052. pipe_config->update_wm_pre = true;
  10053. /* must disable cxsr around plane enable/disable */
  10054. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10055. pipe_config->disable_cxsr = true;
  10056. } else if (turn_off) {
  10057. pipe_config->update_wm_post = true;
  10058. /* must disable cxsr around plane enable/disable */
  10059. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10060. pipe_config->disable_cxsr = true;
  10061. } else if (intel_wm_need_update(plane, plane_state)) {
  10062. /* FIXME bollocks */
  10063. pipe_config->update_wm_pre = true;
  10064. pipe_config->update_wm_post = true;
  10065. }
  10066. /* Pre-gen9 platforms need two-step watermark updates */
  10067. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10068. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10069. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10070. if (visible || was_visible)
  10071. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10072. /*
  10073. * WaCxSRDisabledForSpriteScaling:ivb
  10074. *
  10075. * cstate->update_wm was already set above, so this flag will
  10076. * take effect when we commit and program watermarks.
  10077. */
  10078. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10079. needs_scaling(to_intel_plane_state(plane_state)) &&
  10080. !needs_scaling(old_plane_state))
  10081. pipe_config->disable_lp_wm = true;
  10082. return 0;
  10083. }
  10084. static bool encoders_cloneable(const struct intel_encoder *a,
  10085. const struct intel_encoder *b)
  10086. {
  10087. /* masks could be asymmetric, so check both ways */
  10088. return a == b || (a->cloneable & (1 << b->type) &&
  10089. b->cloneable & (1 << a->type));
  10090. }
  10091. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10092. struct intel_crtc *crtc,
  10093. struct intel_encoder *encoder)
  10094. {
  10095. struct intel_encoder *source_encoder;
  10096. struct drm_connector *connector;
  10097. struct drm_connector_state *connector_state;
  10098. int i;
  10099. for_each_connector_in_state(state, connector, connector_state, i) {
  10100. if (connector_state->crtc != &crtc->base)
  10101. continue;
  10102. source_encoder =
  10103. to_intel_encoder(connector_state->best_encoder);
  10104. if (!encoders_cloneable(encoder, source_encoder))
  10105. return false;
  10106. }
  10107. return true;
  10108. }
  10109. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10110. struct intel_crtc *crtc)
  10111. {
  10112. struct intel_encoder *encoder;
  10113. struct drm_connector *connector;
  10114. struct drm_connector_state *connector_state;
  10115. int i;
  10116. for_each_connector_in_state(state, connector, connector_state, i) {
  10117. if (connector_state->crtc != &crtc->base)
  10118. continue;
  10119. encoder = to_intel_encoder(connector_state->best_encoder);
  10120. if (!check_single_encoder_cloning(state, crtc, encoder))
  10121. return false;
  10122. }
  10123. return true;
  10124. }
  10125. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10126. struct drm_crtc_state *crtc_state)
  10127. {
  10128. struct drm_device *dev = crtc->dev;
  10129. struct drm_i915_private *dev_priv = dev->dev_private;
  10130. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10131. struct intel_crtc_state *pipe_config =
  10132. to_intel_crtc_state(crtc_state);
  10133. struct drm_atomic_state *state = crtc_state->state;
  10134. int ret;
  10135. bool mode_changed = needs_modeset(crtc_state);
  10136. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10137. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10138. return -EINVAL;
  10139. }
  10140. if (mode_changed && !crtc_state->active)
  10141. pipe_config->update_wm_post = true;
  10142. if (mode_changed && crtc_state->enable &&
  10143. dev_priv->display.crtc_compute_clock &&
  10144. !WARN_ON(pipe_config->shared_dpll)) {
  10145. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10146. pipe_config);
  10147. if (ret)
  10148. return ret;
  10149. }
  10150. if (crtc_state->color_mgmt_changed) {
  10151. ret = intel_color_check(crtc, crtc_state);
  10152. if (ret)
  10153. return ret;
  10154. }
  10155. ret = 0;
  10156. if (dev_priv->display.compute_pipe_wm) {
  10157. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10158. if (ret) {
  10159. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10160. return ret;
  10161. }
  10162. }
  10163. if (dev_priv->display.compute_intermediate_wm &&
  10164. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10165. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10166. return 0;
  10167. /*
  10168. * Calculate 'intermediate' watermarks that satisfy both the
  10169. * old state and the new state. We can program these
  10170. * immediately.
  10171. */
  10172. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10173. intel_crtc,
  10174. pipe_config);
  10175. if (ret) {
  10176. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10177. return ret;
  10178. }
  10179. } else if (dev_priv->display.compute_intermediate_wm) {
  10180. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10181. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10182. }
  10183. if (INTEL_INFO(dev)->gen >= 9) {
  10184. if (mode_changed)
  10185. ret = skl_update_scaler_crtc(pipe_config);
  10186. if (!ret)
  10187. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10188. pipe_config);
  10189. }
  10190. return ret;
  10191. }
  10192. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10193. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10194. .atomic_begin = intel_begin_crtc_commit,
  10195. .atomic_flush = intel_finish_crtc_commit,
  10196. .atomic_check = intel_crtc_atomic_check,
  10197. };
  10198. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10199. {
  10200. struct intel_connector *connector;
  10201. for_each_intel_connector(dev, connector) {
  10202. if (connector->base.state->crtc)
  10203. drm_connector_unreference(&connector->base);
  10204. if (connector->base.encoder) {
  10205. connector->base.state->best_encoder =
  10206. connector->base.encoder;
  10207. connector->base.state->crtc =
  10208. connector->base.encoder->crtc;
  10209. drm_connector_reference(&connector->base);
  10210. } else {
  10211. connector->base.state->best_encoder = NULL;
  10212. connector->base.state->crtc = NULL;
  10213. }
  10214. }
  10215. }
  10216. static void
  10217. connected_sink_compute_bpp(struct intel_connector *connector,
  10218. struct intel_crtc_state *pipe_config)
  10219. {
  10220. int bpp = pipe_config->pipe_bpp;
  10221. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10222. connector->base.base.id,
  10223. connector->base.name);
  10224. /* Don't use an invalid EDID bpc value */
  10225. if (connector->base.display_info.bpc &&
  10226. connector->base.display_info.bpc * 3 < bpp) {
  10227. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10228. bpp, connector->base.display_info.bpc*3);
  10229. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10230. }
  10231. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10232. if (connector->base.display_info.bpc == 0) {
  10233. int type = connector->base.connector_type;
  10234. int clamp_bpp = 24;
  10235. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10236. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10237. type == DRM_MODE_CONNECTOR_eDP)
  10238. clamp_bpp = 18;
  10239. if (bpp > clamp_bpp) {
  10240. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10241. bpp, clamp_bpp);
  10242. pipe_config->pipe_bpp = clamp_bpp;
  10243. }
  10244. }
  10245. }
  10246. static int
  10247. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10248. struct intel_crtc_state *pipe_config)
  10249. {
  10250. struct drm_device *dev = crtc->base.dev;
  10251. struct drm_atomic_state *state;
  10252. struct drm_connector *connector;
  10253. struct drm_connector_state *connector_state;
  10254. int bpp, i;
  10255. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10256. bpp = 10*3;
  10257. else if (INTEL_INFO(dev)->gen >= 5)
  10258. bpp = 12*3;
  10259. else
  10260. bpp = 8*3;
  10261. pipe_config->pipe_bpp = bpp;
  10262. state = pipe_config->base.state;
  10263. /* Clamp display bpp to EDID value */
  10264. for_each_connector_in_state(state, connector, connector_state, i) {
  10265. if (connector_state->crtc != &crtc->base)
  10266. continue;
  10267. connected_sink_compute_bpp(to_intel_connector(connector),
  10268. pipe_config);
  10269. }
  10270. return bpp;
  10271. }
  10272. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10273. {
  10274. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10275. "type: 0x%x flags: 0x%x\n",
  10276. mode->crtc_clock,
  10277. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10278. mode->crtc_hsync_end, mode->crtc_htotal,
  10279. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10280. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10281. }
  10282. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10283. struct intel_crtc_state *pipe_config,
  10284. const char *context)
  10285. {
  10286. struct drm_device *dev = crtc->base.dev;
  10287. struct drm_plane *plane;
  10288. struct intel_plane *intel_plane;
  10289. struct intel_plane_state *state;
  10290. struct drm_framebuffer *fb;
  10291. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10292. crtc->base.base.id, crtc->base.name,
  10293. context, pipe_config, pipe_name(crtc->pipe));
  10294. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10295. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10296. pipe_config->pipe_bpp, pipe_config->dither);
  10297. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10298. pipe_config->has_pch_encoder,
  10299. pipe_config->fdi_lanes,
  10300. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10301. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10302. pipe_config->fdi_m_n.tu);
  10303. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10304. pipe_config->has_dp_encoder,
  10305. pipe_config->lane_count,
  10306. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10307. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10308. pipe_config->dp_m_n.tu);
  10309. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10310. pipe_config->has_dp_encoder,
  10311. pipe_config->lane_count,
  10312. pipe_config->dp_m2_n2.gmch_m,
  10313. pipe_config->dp_m2_n2.gmch_n,
  10314. pipe_config->dp_m2_n2.link_m,
  10315. pipe_config->dp_m2_n2.link_n,
  10316. pipe_config->dp_m2_n2.tu);
  10317. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10318. pipe_config->has_audio,
  10319. pipe_config->has_infoframe);
  10320. DRM_DEBUG_KMS("requested mode:\n");
  10321. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10322. DRM_DEBUG_KMS("adjusted mode:\n");
  10323. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10324. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10325. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10326. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10327. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10328. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10329. crtc->num_scalers,
  10330. pipe_config->scaler_state.scaler_users,
  10331. pipe_config->scaler_state.scaler_id);
  10332. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10333. pipe_config->gmch_pfit.control,
  10334. pipe_config->gmch_pfit.pgm_ratios,
  10335. pipe_config->gmch_pfit.lvds_border_bits);
  10336. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10337. pipe_config->pch_pfit.pos,
  10338. pipe_config->pch_pfit.size,
  10339. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10340. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10341. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10342. if (IS_BROXTON(dev)) {
  10343. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10344. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10345. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10346. pipe_config->ddi_pll_sel,
  10347. pipe_config->dpll_hw_state.ebb0,
  10348. pipe_config->dpll_hw_state.ebb4,
  10349. pipe_config->dpll_hw_state.pll0,
  10350. pipe_config->dpll_hw_state.pll1,
  10351. pipe_config->dpll_hw_state.pll2,
  10352. pipe_config->dpll_hw_state.pll3,
  10353. pipe_config->dpll_hw_state.pll6,
  10354. pipe_config->dpll_hw_state.pll8,
  10355. pipe_config->dpll_hw_state.pll9,
  10356. pipe_config->dpll_hw_state.pll10,
  10357. pipe_config->dpll_hw_state.pcsdw12);
  10358. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10359. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10360. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10361. pipe_config->ddi_pll_sel,
  10362. pipe_config->dpll_hw_state.ctrl1,
  10363. pipe_config->dpll_hw_state.cfgcr1,
  10364. pipe_config->dpll_hw_state.cfgcr2);
  10365. } else if (HAS_DDI(dev)) {
  10366. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10367. pipe_config->ddi_pll_sel,
  10368. pipe_config->dpll_hw_state.wrpll,
  10369. pipe_config->dpll_hw_state.spll);
  10370. } else {
  10371. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10372. "fp0: 0x%x, fp1: 0x%x\n",
  10373. pipe_config->dpll_hw_state.dpll,
  10374. pipe_config->dpll_hw_state.dpll_md,
  10375. pipe_config->dpll_hw_state.fp0,
  10376. pipe_config->dpll_hw_state.fp1);
  10377. }
  10378. DRM_DEBUG_KMS("planes on this crtc\n");
  10379. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10380. intel_plane = to_intel_plane(plane);
  10381. if (intel_plane->pipe != crtc->pipe)
  10382. continue;
  10383. state = to_intel_plane_state(plane->state);
  10384. fb = state->base.fb;
  10385. if (!fb) {
  10386. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10387. plane->base.id, plane->name, state->scaler_id);
  10388. continue;
  10389. }
  10390. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10391. plane->base.id, plane->name);
  10392. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10393. fb->base.id, fb->width, fb->height,
  10394. drm_get_format_name(fb->pixel_format));
  10395. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10396. state->scaler_id,
  10397. state->src.x1 >> 16, state->src.y1 >> 16,
  10398. drm_rect_width(&state->src) >> 16,
  10399. drm_rect_height(&state->src) >> 16,
  10400. state->dst.x1, state->dst.y1,
  10401. drm_rect_width(&state->dst),
  10402. drm_rect_height(&state->dst));
  10403. }
  10404. }
  10405. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10406. {
  10407. struct drm_device *dev = state->dev;
  10408. struct drm_connector *connector;
  10409. unsigned int used_ports = 0;
  10410. /*
  10411. * Walk the connector list instead of the encoder
  10412. * list to detect the problem on ddi platforms
  10413. * where there's just one encoder per digital port.
  10414. */
  10415. drm_for_each_connector(connector, dev) {
  10416. struct drm_connector_state *connector_state;
  10417. struct intel_encoder *encoder;
  10418. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10419. if (!connector_state)
  10420. connector_state = connector->state;
  10421. if (!connector_state->best_encoder)
  10422. continue;
  10423. encoder = to_intel_encoder(connector_state->best_encoder);
  10424. WARN_ON(!connector_state->crtc);
  10425. switch (encoder->type) {
  10426. unsigned int port_mask;
  10427. case INTEL_OUTPUT_UNKNOWN:
  10428. if (WARN_ON(!HAS_DDI(dev)))
  10429. break;
  10430. case INTEL_OUTPUT_DISPLAYPORT:
  10431. case INTEL_OUTPUT_HDMI:
  10432. case INTEL_OUTPUT_EDP:
  10433. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10434. /* the same port mustn't appear more than once */
  10435. if (used_ports & port_mask)
  10436. return false;
  10437. used_ports |= port_mask;
  10438. default:
  10439. break;
  10440. }
  10441. }
  10442. return true;
  10443. }
  10444. static void
  10445. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10446. {
  10447. struct drm_crtc_state tmp_state;
  10448. struct intel_crtc_scaler_state scaler_state;
  10449. struct intel_dpll_hw_state dpll_hw_state;
  10450. struct intel_shared_dpll *shared_dpll;
  10451. uint32_t ddi_pll_sel;
  10452. bool force_thru;
  10453. /* FIXME: before the switch to atomic started, a new pipe_config was
  10454. * kzalloc'd. Code that depends on any field being zero should be
  10455. * fixed, so that the crtc_state can be safely duplicated. For now,
  10456. * only fields that are know to not cause problems are preserved. */
  10457. tmp_state = crtc_state->base;
  10458. scaler_state = crtc_state->scaler_state;
  10459. shared_dpll = crtc_state->shared_dpll;
  10460. dpll_hw_state = crtc_state->dpll_hw_state;
  10461. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10462. force_thru = crtc_state->pch_pfit.force_thru;
  10463. memset(crtc_state, 0, sizeof *crtc_state);
  10464. crtc_state->base = tmp_state;
  10465. crtc_state->scaler_state = scaler_state;
  10466. crtc_state->shared_dpll = shared_dpll;
  10467. crtc_state->dpll_hw_state = dpll_hw_state;
  10468. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10469. crtc_state->pch_pfit.force_thru = force_thru;
  10470. }
  10471. static int
  10472. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10473. struct intel_crtc_state *pipe_config)
  10474. {
  10475. struct drm_atomic_state *state = pipe_config->base.state;
  10476. struct intel_encoder *encoder;
  10477. struct drm_connector *connector;
  10478. struct drm_connector_state *connector_state;
  10479. int base_bpp, ret = -EINVAL;
  10480. int i;
  10481. bool retry = true;
  10482. clear_intel_crtc_state(pipe_config);
  10483. pipe_config->cpu_transcoder =
  10484. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10485. /*
  10486. * Sanitize sync polarity flags based on requested ones. If neither
  10487. * positive or negative polarity is requested, treat this as meaning
  10488. * negative polarity.
  10489. */
  10490. if (!(pipe_config->base.adjusted_mode.flags &
  10491. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10492. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10493. if (!(pipe_config->base.adjusted_mode.flags &
  10494. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10495. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10496. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10497. pipe_config);
  10498. if (base_bpp < 0)
  10499. goto fail;
  10500. /*
  10501. * Determine the real pipe dimensions. Note that stereo modes can
  10502. * increase the actual pipe size due to the frame doubling and
  10503. * insertion of additional space for blanks between the frame. This
  10504. * is stored in the crtc timings. We use the requested mode to do this
  10505. * computation to clearly distinguish it from the adjusted mode, which
  10506. * can be changed by the connectors in the below retry loop.
  10507. */
  10508. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10509. &pipe_config->pipe_src_w,
  10510. &pipe_config->pipe_src_h);
  10511. encoder_retry:
  10512. /* Ensure the port clock defaults are reset when retrying. */
  10513. pipe_config->port_clock = 0;
  10514. pipe_config->pixel_multiplier = 1;
  10515. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10516. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10517. CRTC_STEREO_DOUBLE);
  10518. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10519. * adjust it according to limitations or connector properties, and also
  10520. * a chance to reject the mode entirely.
  10521. */
  10522. for_each_connector_in_state(state, connector, connector_state, i) {
  10523. if (connector_state->crtc != crtc)
  10524. continue;
  10525. encoder = to_intel_encoder(connector_state->best_encoder);
  10526. if (!(encoder->compute_config(encoder, pipe_config))) {
  10527. DRM_DEBUG_KMS("Encoder config failure\n");
  10528. goto fail;
  10529. }
  10530. }
  10531. /* Set default port clock if not overwritten by the encoder. Needs to be
  10532. * done afterwards in case the encoder adjusts the mode. */
  10533. if (!pipe_config->port_clock)
  10534. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10535. * pipe_config->pixel_multiplier;
  10536. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10537. if (ret < 0) {
  10538. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10539. goto fail;
  10540. }
  10541. if (ret == RETRY) {
  10542. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10543. ret = -EINVAL;
  10544. goto fail;
  10545. }
  10546. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10547. retry = false;
  10548. goto encoder_retry;
  10549. }
  10550. /* Dithering seems to not pass-through bits correctly when it should, so
  10551. * only enable it on 6bpc panels. */
  10552. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10553. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10554. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10555. fail:
  10556. return ret;
  10557. }
  10558. static void
  10559. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10560. {
  10561. struct drm_crtc *crtc;
  10562. struct drm_crtc_state *crtc_state;
  10563. int i;
  10564. /* Double check state. */
  10565. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10566. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10567. /* Update hwmode for vblank functions */
  10568. if (crtc->state->active)
  10569. crtc->hwmode = crtc->state->adjusted_mode;
  10570. else
  10571. crtc->hwmode.crtc_clock = 0;
  10572. /*
  10573. * Update legacy state to satisfy fbc code. This can
  10574. * be removed when fbc uses the atomic state.
  10575. */
  10576. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10577. struct drm_plane_state *plane_state = crtc->primary->state;
  10578. crtc->primary->fb = plane_state->fb;
  10579. crtc->x = plane_state->src_x >> 16;
  10580. crtc->y = plane_state->src_y >> 16;
  10581. }
  10582. }
  10583. }
  10584. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10585. {
  10586. int diff;
  10587. if (clock1 == clock2)
  10588. return true;
  10589. if (!clock1 || !clock2)
  10590. return false;
  10591. diff = abs(clock1 - clock2);
  10592. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10593. return true;
  10594. return false;
  10595. }
  10596. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10597. list_for_each_entry((intel_crtc), \
  10598. &(dev)->mode_config.crtc_list, \
  10599. base.head) \
  10600. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10601. static bool
  10602. intel_compare_m_n(unsigned int m, unsigned int n,
  10603. unsigned int m2, unsigned int n2,
  10604. bool exact)
  10605. {
  10606. if (m == m2 && n == n2)
  10607. return true;
  10608. if (exact || !m || !n || !m2 || !n2)
  10609. return false;
  10610. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10611. if (n > n2) {
  10612. while (n > n2) {
  10613. m2 <<= 1;
  10614. n2 <<= 1;
  10615. }
  10616. } else if (n < n2) {
  10617. while (n < n2) {
  10618. m <<= 1;
  10619. n <<= 1;
  10620. }
  10621. }
  10622. if (n != n2)
  10623. return false;
  10624. return intel_fuzzy_clock_check(m, m2);
  10625. }
  10626. static bool
  10627. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10628. struct intel_link_m_n *m2_n2,
  10629. bool adjust)
  10630. {
  10631. if (m_n->tu == m2_n2->tu &&
  10632. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10633. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10634. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10635. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10636. if (adjust)
  10637. *m2_n2 = *m_n;
  10638. return true;
  10639. }
  10640. return false;
  10641. }
  10642. static bool
  10643. intel_pipe_config_compare(struct drm_device *dev,
  10644. struct intel_crtc_state *current_config,
  10645. struct intel_crtc_state *pipe_config,
  10646. bool adjust)
  10647. {
  10648. bool ret = true;
  10649. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10650. do { \
  10651. if (!adjust) \
  10652. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10653. else \
  10654. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10655. } while (0)
  10656. #define PIPE_CONF_CHECK_X(name) \
  10657. if (current_config->name != pipe_config->name) { \
  10658. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10659. "(expected 0x%08x, found 0x%08x)\n", \
  10660. current_config->name, \
  10661. pipe_config->name); \
  10662. ret = false; \
  10663. }
  10664. #define PIPE_CONF_CHECK_I(name) \
  10665. if (current_config->name != pipe_config->name) { \
  10666. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10667. "(expected %i, found %i)\n", \
  10668. current_config->name, \
  10669. pipe_config->name); \
  10670. ret = false; \
  10671. }
  10672. #define PIPE_CONF_CHECK_P(name) \
  10673. if (current_config->name != pipe_config->name) { \
  10674. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10675. "(expected %p, found %p)\n", \
  10676. current_config->name, \
  10677. pipe_config->name); \
  10678. ret = false; \
  10679. }
  10680. #define PIPE_CONF_CHECK_M_N(name) \
  10681. if (!intel_compare_link_m_n(&current_config->name, \
  10682. &pipe_config->name,\
  10683. adjust)) { \
  10684. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10685. "(expected tu %i gmch %i/%i link %i/%i, " \
  10686. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10687. current_config->name.tu, \
  10688. current_config->name.gmch_m, \
  10689. current_config->name.gmch_n, \
  10690. current_config->name.link_m, \
  10691. current_config->name.link_n, \
  10692. pipe_config->name.tu, \
  10693. pipe_config->name.gmch_m, \
  10694. pipe_config->name.gmch_n, \
  10695. pipe_config->name.link_m, \
  10696. pipe_config->name.link_n); \
  10697. ret = false; \
  10698. }
  10699. /* This is required for BDW+ where there is only one set of registers for
  10700. * switching between high and low RR.
  10701. * This macro can be used whenever a comparison has to be made between one
  10702. * hw state and multiple sw state variables.
  10703. */
  10704. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10705. if (!intel_compare_link_m_n(&current_config->name, \
  10706. &pipe_config->name, adjust) && \
  10707. !intel_compare_link_m_n(&current_config->alt_name, \
  10708. &pipe_config->name, adjust)) { \
  10709. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10710. "(expected tu %i gmch %i/%i link %i/%i, " \
  10711. "or tu %i gmch %i/%i link %i/%i, " \
  10712. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10713. current_config->name.tu, \
  10714. current_config->name.gmch_m, \
  10715. current_config->name.gmch_n, \
  10716. current_config->name.link_m, \
  10717. current_config->name.link_n, \
  10718. current_config->alt_name.tu, \
  10719. current_config->alt_name.gmch_m, \
  10720. current_config->alt_name.gmch_n, \
  10721. current_config->alt_name.link_m, \
  10722. current_config->alt_name.link_n, \
  10723. pipe_config->name.tu, \
  10724. pipe_config->name.gmch_m, \
  10725. pipe_config->name.gmch_n, \
  10726. pipe_config->name.link_m, \
  10727. pipe_config->name.link_n); \
  10728. ret = false; \
  10729. }
  10730. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10731. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10732. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10733. "(expected %i, found %i)\n", \
  10734. current_config->name & (mask), \
  10735. pipe_config->name & (mask)); \
  10736. ret = false; \
  10737. }
  10738. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10739. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10740. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10741. "(expected %i, found %i)\n", \
  10742. current_config->name, \
  10743. pipe_config->name); \
  10744. ret = false; \
  10745. }
  10746. #define PIPE_CONF_QUIRK(quirk) \
  10747. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10748. PIPE_CONF_CHECK_I(cpu_transcoder);
  10749. PIPE_CONF_CHECK_I(has_pch_encoder);
  10750. PIPE_CONF_CHECK_I(fdi_lanes);
  10751. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10752. PIPE_CONF_CHECK_I(has_dp_encoder);
  10753. PIPE_CONF_CHECK_I(lane_count);
  10754. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  10755. if (INTEL_INFO(dev)->gen < 8) {
  10756. PIPE_CONF_CHECK_M_N(dp_m_n);
  10757. if (current_config->has_drrs)
  10758. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10759. } else
  10760. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10761. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10762. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10763. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10764. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10765. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10766. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10767. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10768. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10769. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10770. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10771. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10772. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10773. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10774. PIPE_CONF_CHECK_I(pixel_multiplier);
  10775. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10776. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10777. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10778. PIPE_CONF_CHECK_I(limited_color_range);
  10779. PIPE_CONF_CHECK_I(has_infoframe);
  10780. PIPE_CONF_CHECK_I(has_audio);
  10781. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10782. DRM_MODE_FLAG_INTERLACE);
  10783. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10784. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10785. DRM_MODE_FLAG_PHSYNC);
  10786. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10787. DRM_MODE_FLAG_NHSYNC);
  10788. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10789. DRM_MODE_FLAG_PVSYNC);
  10790. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10791. DRM_MODE_FLAG_NVSYNC);
  10792. }
  10793. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10794. /* pfit ratios are autocomputed by the hw on gen4+ */
  10795. if (INTEL_INFO(dev)->gen < 4)
  10796. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10797. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10798. if (!adjust) {
  10799. PIPE_CONF_CHECK_I(pipe_src_w);
  10800. PIPE_CONF_CHECK_I(pipe_src_h);
  10801. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10802. if (current_config->pch_pfit.enabled) {
  10803. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10804. PIPE_CONF_CHECK_X(pch_pfit.size);
  10805. }
  10806. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10807. }
  10808. /* BDW+ don't expose a synchronous way to read the state */
  10809. if (IS_HASWELL(dev))
  10810. PIPE_CONF_CHECK_I(ips_enabled);
  10811. PIPE_CONF_CHECK_I(double_wide);
  10812. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10813. PIPE_CONF_CHECK_P(shared_dpll);
  10814. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10815. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10816. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10817. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10818. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10819. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10820. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10821. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10822. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10823. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10824. PIPE_CONF_CHECK_X(dsi_pll.div);
  10825. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10826. PIPE_CONF_CHECK_I(pipe_bpp);
  10827. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10828. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10829. #undef PIPE_CONF_CHECK_X
  10830. #undef PIPE_CONF_CHECK_I
  10831. #undef PIPE_CONF_CHECK_P
  10832. #undef PIPE_CONF_CHECK_FLAGS
  10833. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10834. #undef PIPE_CONF_QUIRK
  10835. #undef INTEL_ERR_OR_DBG_KMS
  10836. return ret;
  10837. }
  10838. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10839. const struct intel_crtc_state *pipe_config)
  10840. {
  10841. if (pipe_config->has_pch_encoder) {
  10842. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10843. &pipe_config->fdi_m_n);
  10844. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10845. /*
  10846. * FDI already provided one idea for the dotclock.
  10847. * Yell if the encoder disagrees.
  10848. */
  10849. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10850. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10851. fdi_dotclock, dotclock);
  10852. }
  10853. }
  10854. static void verify_wm_state(struct drm_crtc *crtc,
  10855. struct drm_crtc_state *new_state)
  10856. {
  10857. struct drm_device *dev = crtc->dev;
  10858. struct drm_i915_private *dev_priv = dev->dev_private;
  10859. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10860. struct skl_ddb_entry *hw_entry, *sw_entry;
  10861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10862. const enum pipe pipe = intel_crtc->pipe;
  10863. int plane;
  10864. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10865. return;
  10866. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10867. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10868. /* planes */
  10869. for_each_plane(dev_priv, pipe, plane) {
  10870. hw_entry = &hw_ddb.plane[pipe][plane];
  10871. sw_entry = &sw_ddb->plane[pipe][plane];
  10872. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10873. continue;
  10874. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10875. "(expected (%u,%u), found (%u,%u))\n",
  10876. pipe_name(pipe), plane + 1,
  10877. sw_entry->start, sw_entry->end,
  10878. hw_entry->start, hw_entry->end);
  10879. }
  10880. /* cursor */
  10881. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10882. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10883. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10884. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10885. "(expected (%u,%u), found (%u,%u))\n",
  10886. pipe_name(pipe),
  10887. sw_entry->start, sw_entry->end,
  10888. hw_entry->start, hw_entry->end);
  10889. }
  10890. }
  10891. static void
  10892. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10893. {
  10894. struct drm_connector *connector;
  10895. drm_for_each_connector(connector, dev) {
  10896. struct drm_encoder *encoder = connector->encoder;
  10897. struct drm_connector_state *state = connector->state;
  10898. if (state->crtc != crtc)
  10899. continue;
  10900. intel_connector_verify_state(to_intel_connector(connector));
  10901. I915_STATE_WARN(state->best_encoder != encoder,
  10902. "connector's atomic encoder doesn't match legacy encoder\n");
  10903. }
  10904. }
  10905. static void
  10906. verify_encoder_state(struct drm_device *dev)
  10907. {
  10908. struct intel_encoder *encoder;
  10909. struct intel_connector *connector;
  10910. for_each_intel_encoder(dev, encoder) {
  10911. bool enabled = false;
  10912. enum pipe pipe;
  10913. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10914. encoder->base.base.id,
  10915. encoder->base.name);
  10916. for_each_intel_connector(dev, connector) {
  10917. if (connector->base.state->best_encoder != &encoder->base)
  10918. continue;
  10919. enabled = true;
  10920. I915_STATE_WARN(connector->base.state->crtc !=
  10921. encoder->base.crtc,
  10922. "connector's crtc doesn't match encoder crtc\n");
  10923. }
  10924. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10925. "encoder's enabled state mismatch "
  10926. "(expected %i, found %i)\n",
  10927. !!encoder->base.crtc, enabled);
  10928. if (!encoder->base.crtc) {
  10929. bool active;
  10930. active = encoder->get_hw_state(encoder, &pipe);
  10931. I915_STATE_WARN(active,
  10932. "encoder detached but still enabled on pipe %c.\n",
  10933. pipe_name(pipe));
  10934. }
  10935. }
  10936. }
  10937. static void
  10938. verify_crtc_state(struct drm_crtc *crtc,
  10939. struct drm_crtc_state *old_crtc_state,
  10940. struct drm_crtc_state *new_crtc_state)
  10941. {
  10942. struct drm_device *dev = crtc->dev;
  10943. struct drm_i915_private *dev_priv = dev->dev_private;
  10944. struct intel_encoder *encoder;
  10945. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10946. struct intel_crtc_state *pipe_config, *sw_config;
  10947. struct drm_atomic_state *old_state;
  10948. bool active;
  10949. old_state = old_crtc_state->state;
  10950. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10951. pipe_config = to_intel_crtc_state(old_crtc_state);
  10952. memset(pipe_config, 0, sizeof(*pipe_config));
  10953. pipe_config->base.crtc = crtc;
  10954. pipe_config->base.state = old_state;
  10955. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10956. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10957. /* hw state is inconsistent with the pipe quirk */
  10958. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10959. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10960. active = new_crtc_state->active;
  10961. I915_STATE_WARN(new_crtc_state->active != active,
  10962. "crtc active state doesn't match with hw state "
  10963. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10964. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10965. "transitional active state does not match atomic hw state "
  10966. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10967. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10968. enum pipe pipe;
  10969. active = encoder->get_hw_state(encoder, &pipe);
  10970. I915_STATE_WARN(active != new_crtc_state->active,
  10971. "[ENCODER:%i] active %i with crtc active %i\n",
  10972. encoder->base.base.id, active, new_crtc_state->active);
  10973. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10974. "Encoder connected to wrong pipe %c\n",
  10975. pipe_name(pipe));
  10976. if (active)
  10977. encoder->get_config(encoder, pipe_config);
  10978. }
  10979. if (!new_crtc_state->active)
  10980. return;
  10981. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10982. sw_config = to_intel_crtc_state(crtc->state);
  10983. if (!intel_pipe_config_compare(dev, sw_config,
  10984. pipe_config, false)) {
  10985. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10986. intel_dump_pipe_config(intel_crtc, pipe_config,
  10987. "[hw state]");
  10988. intel_dump_pipe_config(intel_crtc, sw_config,
  10989. "[sw state]");
  10990. }
  10991. }
  10992. static void
  10993. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10994. struct intel_shared_dpll *pll,
  10995. struct drm_crtc *crtc,
  10996. struct drm_crtc_state *new_state)
  10997. {
  10998. struct intel_dpll_hw_state dpll_hw_state;
  10999. unsigned crtc_mask;
  11000. bool active;
  11001. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11002. DRM_DEBUG_KMS("%s\n", pll->name);
  11003. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11004. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11005. I915_STATE_WARN(!pll->on && pll->active_mask,
  11006. "pll in active use but not on in sw tracking\n");
  11007. I915_STATE_WARN(pll->on && !pll->active_mask,
  11008. "pll is on but not used by any active crtc\n");
  11009. I915_STATE_WARN(pll->on != active,
  11010. "pll on state mismatch (expected %i, found %i)\n",
  11011. pll->on, active);
  11012. }
  11013. if (!crtc) {
  11014. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11015. "more active pll users than references: %x vs %x\n",
  11016. pll->active_mask, pll->config.crtc_mask);
  11017. return;
  11018. }
  11019. crtc_mask = 1 << drm_crtc_index(crtc);
  11020. if (new_state->active)
  11021. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11022. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11023. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11024. else
  11025. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11026. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11027. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11028. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11029. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11030. crtc_mask, pll->config.crtc_mask);
  11031. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11032. &dpll_hw_state,
  11033. sizeof(dpll_hw_state)),
  11034. "pll hw state mismatch\n");
  11035. }
  11036. static void
  11037. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11038. struct drm_crtc_state *old_crtc_state,
  11039. struct drm_crtc_state *new_crtc_state)
  11040. {
  11041. struct drm_i915_private *dev_priv = dev->dev_private;
  11042. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11043. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11044. if (new_state->shared_dpll)
  11045. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11046. if (old_state->shared_dpll &&
  11047. old_state->shared_dpll != new_state->shared_dpll) {
  11048. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11049. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11050. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11051. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11052. pipe_name(drm_crtc_index(crtc)));
  11053. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11054. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11055. pipe_name(drm_crtc_index(crtc)));
  11056. }
  11057. }
  11058. static void
  11059. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11060. struct drm_crtc_state *old_state,
  11061. struct drm_crtc_state *new_state)
  11062. {
  11063. if (!needs_modeset(new_state) &&
  11064. !to_intel_crtc_state(new_state)->update_pipe)
  11065. return;
  11066. verify_wm_state(crtc, new_state);
  11067. verify_connector_state(crtc->dev, crtc);
  11068. verify_crtc_state(crtc, old_state, new_state);
  11069. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11070. }
  11071. static void
  11072. verify_disabled_dpll_state(struct drm_device *dev)
  11073. {
  11074. struct drm_i915_private *dev_priv = dev->dev_private;
  11075. int i;
  11076. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11077. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11078. }
  11079. static void
  11080. intel_modeset_verify_disabled(struct drm_device *dev)
  11081. {
  11082. verify_encoder_state(dev);
  11083. verify_connector_state(dev, NULL);
  11084. verify_disabled_dpll_state(dev);
  11085. }
  11086. static void update_scanline_offset(struct intel_crtc *crtc)
  11087. {
  11088. struct drm_device *dev = crtc->base.dev;
  11089. /*
  11090. * The scanline counter increments at the leading edge of hsync.
  11091. *
  11092. * On most platforms it starts counting from vtotal-1 on the
  11093. * first active line. That means the scanline counter value is
  11094. * always one less than what we would expect. Ie. just after
  11095. * start of vblank, which also occurs at start of hsync (on the
  11096. * last active line), the scanline counter will read vblank_start-1.
  11097. *
  11098. * On gen2 the scanline counter starts counting from 1 instead
  11099. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11100. * to keep the value positive), instead of adding one.
  11101. *
  11102. * On HSW+ the behaviour of the scanline counter depends on the output
  11103. * type. For DP ports it behaves like most other platforms, but on HDMI
  11104. * there's an extra 1 line difference. So we need to add two instead of
  11105. * one to the value.
  11106. */
  11107. if (IS_GEN2(dev)) {
  11108. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11109. int vtotal;
  11110. vtotal = adjusted_mode->crtc_vtotal;
  11111. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11112. vtotal /= 2;
  11113. crtc->scanline_offset = vtotal - 1;
  11114. } else if (HAS_DDI(dev) &&
  11115. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  11116. crtc->scanline_offset = 2;
  11117. } else
  11118. crtc->scanline_offset = 1;
  11119. }
  11120. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11121. {
  11122. struct drm_device *dev = state->dev;
  11123. struct drm_i915_private *dev_priv = to_i915(dev);
  11124. struct intel_shared_dpll_config *shared_dpll = NULL;
  11125. struct drm_crtc *crtc;
  11126. struct drm_crtc_state *crtc_state;
  11127. int i;
  11128. if (!dev_priv->display.crtc_compute_clock)
  11129. return;
  11130. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11132. struct intel_shared_dpll *old_dpll =
  11133. to_intel_crtc_state(crtc->state)->shared_dpll;
  11134. if (!needs_modeset(crtc_state))
  11135. continue;
  11136. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11137. if (!old_dpll)
  11138. continue;
  11139. if (!shared_dpll)
  11140. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11141. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11142. }
  11143. }
  11144. /*
  11145. * This implements the workaround described in the "notes" section of the mode
  11146. * set sequence documentation. When going from no pipes or single pipe to
  11147. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11148. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11149. */
  11150. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11151. {
  11152. struct drm_crtc_state *crtc_state;
  11153. struct intel_crtc *intel_crtc;
  11154. struct drm_crtc *crtc;
  11155. struct intel_crtc_state *first_crtc_state = NULL;
  11156. struct intel_crtc_state *other_crtc_state = NULL;
  11157. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11158. int i;
  11159. /* look at all crtc's that are going to be enabled in during modeset */
  11160. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11161. intel_crtc = to_intel_crtc(crtc);
  11162. if (!crtc_state->active || !needs_modeset(crtc_state))
  11163. continue;
  11164. if (first_crtc_state) {
  11165. other_crtc_state = to_intel_crtc_state(crtc_state);
  11166. break;
  11167. } else {
  11168. first_crtc_state = to_intel_crtc_state(crtc_state);
  11169. first_pipe = intel_crtc->pipe;
  11170. }
  11171. }
  11172. /* No workaround needed? */
  11173. if (!first_crtc_state)
  11174. return 0;
  11175. /* w/a possibly needed, check how many crtc's are already enabled. */
  11176. for_each_intel_crtc(state->dev, intel_crtc) {
  11177. struct intel_crtc_state *pipe_config;
  11178. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11179. if (IS_ERR(pipe_config))
  11180. return PTR_ERR(pipe_config);
  11181. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11182. if (!pipe_config->base.active ||
  11183. needs_modeset(&pipe_config->base))
  11184. continue;
  11185. /* 2 or more enabled crtcs means no need for w/a */
  11186. if (enabled_pipe != INVALID_PIPE)
  11187. return 0;
  11188. enabled_pipe = intel_crtc->pipe;
  11189. }
  11190. if (enabled_pipe != INVALID_PIPE)
  11191. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11192. else if (other_crtc_state)
  11193. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11194. return 0;
  11195. }
  11196. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11197. {
  11198. struct drm_crtc *crtc;
  11199. struct drm_crtc_state *crtc_state;
  11200. int ret = 0;
  11201. /* add all active pipes to the state */
  11202. for_each_crtc(state->dev, crtc) {
  11203. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11204. if (IS_ERR(crtc_state))
  11205. return PTR_ERR(crtc_state);
  11206. if (!crtc_state->active || needs_modeset(crtc_state))
  11207. continue;
  11208. crtc_state->mode_changed = true;
  11209. ret = drm_atomic_add_affected_connectors(state, crtc);
  11210. if (ret)
  11211. break;
  11212. ret = drm_atomic_add_affected_planes(state, crtc);
  11213. if (ret)
  11214. break;
  11215. }
  11216. return ret;
  11217. }
  11218. static int intel_modeset_checks(struct drm_atomic_state *state)
  11219. {
  11220. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11221. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11222. struct drm_crtc *crtc;
  11223. struct drm_crtc_state *crtc_state;
  11224. int ret = 0, i;
  11225. if (!check_digital_port_conflicts(state)) {
  11226. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11227. return -EINVAL;
  11228. }
  11229. intel_state->modeset = true;
  11230. intel_state->active_crtcs = dev_priv->active_crtcs;
  11231. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11232. if (crtc_state->active)
  11233. intel_state->active_crtcs |= 1 << i;
  11234. else
  11235. intel_state->active_crtcs &= ~(1 << i);
  11236. if (crtc_state->active != crtc->state->active)
  11237. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11238. }
  11239. /*
  11240. * See if the config requires any additional preparation, e.g.
  11241. * to adjust global state with pipes off. We need to do this
  11242. * here so we can get the modeset_pipe updated config for the new
  11243. * mode set on this crtc. For other crtcs we need to use the
  11244. * adjusted_mode bits in the crtc directly.
  11245. */
  11246. if (dev_priv->display.modeset_calc_cdclk) {
  11247. if (!intel_state->cdclk_pll_vco)
  11248. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11249. if (!intel_state->cdclk_pll_vco)
  11250. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11251. ret = dev_priv->display.modeset_calc_cdclk(state);
  11252. if (ret < 0)
  11253. return ret;
  11254. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11255. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11256. ret = intel_modeset_all_pipes(state);
  11257. if (ret < 0)
  11258. return ret;
  11259. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11260. intel_state->cdclk, intel_state->dev_cdclk);
  11261. } else
  11262. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11263. intel_modeset_clear_plls(state);
  11264. if (IS_HASWELL(dev_priv))
  11265. return haswell_mode_set_planes_workaround(state);
  11266. return 0;
  11267. }
  11268. /*
  11269. * Handle calculation of various watermark data at the end of the atomic check
  11270. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11271. * handlers to ensure that all derived state has been updated.
  11272. */
  11273. static int calc_watermark_data(struct drm_atomic_state *state)
  11274. {
  11275. struct drm_device *dev = state->dev;
  11276. struct drm_i915_private *dev_priv = to_i915(dev);
  11277. /* Is there platform-specific watermark information to calculate? */
  11278. if (dev_priv->display.compute_global_watermarks)
  11279. return dev_priv->display.compute_global_watermarks(state);
  11280. return 0;
  11281. }
  11282. /**
  11283. * intel_atomic_check - validate state object
  11284. * @dev: drm device
  11285. * @state: state to validate
  11286. */
  11287. static int intel_atomic_check(struct drm_device *dev,
  11288. struct drm_atomic_state *state)
  11289. {
  11290. struct drm_i915_private *dev_priv = to_i915(dev);
  11291. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11292. struct drm_crtc *crtc;
  11293. struct drm_crtc_state *crtc_state;
  11294. int ret, i;
  11295. bool any_ms = false;
  11296. ret = drm_atomic_helper_check_modeset(dev, state);
  11297. if (ret)
  11298. return ret;
  11299. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11300. struct intel_crtc_state *pipe_config =
  11301. to_intel_crtc_state(crtc_state);
  11302. /* Catch I915_MODE_FLAG_INHERITED */
  11303. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11304. crtc_state->mode_changed = true;
  11305. if (!needs_modeset(crtc_state))
  11306. continue;
  11307. if (!crtc_state->enable) {
  11308. any_ms = true;
  11309. continue;
  11310. }
  11311. /* FIXME: For only active_changed we shouldn't need to do any
  11312. * state recomputation at all. */
  11313. ret = drm_atomic_add_affected_connectors(state, crtc);
  11314. if (ret)
  11315. return ret;
  11316. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11317. if (ret) {
  11318. intel_dump_pipe_config(to_intel_crtc(crtc),
  11319. pipe_config, "[failed]");
  11320. return ret;
  11321. }
  11322. if (i915.fastboot &&
  11323. intel_pipe_config_compare(dev,
  11324. to_intel_crtc_state(crtc->state),
  11325. pipe_config, true)) {
  11326. crtc_state->mode_changed = false;
  11327. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11328. }
  11329. if (needs_modeset(crtc_state))
  11330. any_ms = true;
  11331. ret = drm_atomic_add_affected_planes(state, crtc);
  11332. if (ret)
  11333. return ret;
  11334. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11335. needs_modeset(crtc_state) ?
  11336. "[modeset]" : "[fastset]");
  11337. }
  11338. if (any_ms) {
  11339. ret = intel_modeset_checks(state);
  11340. if (ret)
  11341. return ret;
  11342. } else
  11343. intel_state->cdclk = dev_priv->cdclk_freq;
  11344. ret = drm_atomic_helper_check_planes(dev, state);
  11345. if (ret)
  11346. return ret;
  11347. intel_fbc_choose_crtc(dev_priv, state);
  11348. return calc_watermark_data(state);
  11349. }
  11350. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11351. struct drm_atomic_state *state,
  11352. bool nonblock)
  11353. {
  11354. struct drm_i915_private *dev_priv = dev->dev_private;
  11355. struct drm_plane_state *plane_state;
  11356. struct drm_crtc_state *crtc_state;
  11357. struct drm_plane *plane;
  11358. struct drm_crtc *crtc;
  11359. int i, ret;
  11360. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11361. if (state->legacy_cursor_update)
  11362. continue;
  11363. ret = intel_crtc_wait_for_pending_flips(crtc);
  11364. if (ret)
  11365. return ret;
  11366. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11367. flush_workqueue(dev_priv->wq);
  11368. }
  11369. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11370. if (ret)
  11371. return ret;
  11372. ret = drm_atomic_helper_prepare_planes(dev, state);
  11373. mutex_unlock(&dev->struct_mutex);
  11374. if (!ret && !nonblock) {
  11375. for_each_plane_in_state(state, plane, plane_state, i) {
  11376. struct intel_plane_state *intel_plane_state =
  11377. to_intel_plane_state(plane_state);
  11378. if (!intel_plane_state->wait_req)
  11379. continue;
  11380. ret = __i915_wait_request(intel_plane_state->wait_req,
  11381. true, NULL, NULL);
  11382. if (ret) {
  11383. /* Any hang should be swallowed by the wait */
  11384. WARN_ON(ret == -EIO);
  11385. mutex_lock(&dev->struct_mutex);
  11386. drm_atomic_helper_cleanup_planes(dev, state);
  11387. mutex_unlock(&dev->struct_mutex);
  11388. break;
  11389. }
  11390. }
  11391. }
  11392. return ret;
  11393. }
  11394. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11395. {
  11396. struct drm_device *dev = crtc->base.dev;
  11397. if (!dev->max_vblank_count)
  11398. return drm_accurate_vblank_count(&crtc->base);
  11399. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11400. }
  11401. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11402. struct drm_i915_private *dev_priv,
  11403. unsigned crtc_mask)
  11404. {
  11405. unsigned last_vblank_count[I915_MAX_PIPES];
  11406. enum pipe pipe;
  11407. int ret;
  11408. if (!crtc_mask)
  11409. return;
  11410. for_each_pipe(dev_priv, pipe) {
  11411. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11412. if (!((1 << pipe) & crtc_mask))
  11413. continue;
  11414. ret = drm_crtc_vblank_get(crtc);
  11415. if (WARN_ON(ret != 0)) {
  11416. crtc_mask &= ~(1 << pipe);
  11417. continue;
  11418. }
  11419. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11420. }
  11421. for_each_pipe(dev_priv, pipe) {
  11422. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11423. long lret;
  11424. if (!((1 << pipe) & crtc_mask))
  11425. continue;
  11426. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11427. last_vblank_count[pipe] !=
  11428. drm_crtc_vblank_count(crtc),
  11429. msecs_to_jiffies(50));
  11430. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11431. drm_crtc_vblank_put(crtc);
  11432. }
  11433. }
  11434. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11435. {
  11436. /* fb updated, need to unpin old fb */
  11437. if (crtc_state->fb_changed)
  11438. return true;
  11439. /* wm changes, need vblank before final wm's */
  11440. if (crtc_state->update_wm_post)
  11441. return true;
  11442. /*
  11443. * cxsr is re-enabled after vblank.
  11444. * This is already handled by crtc_state->update_wm_post,
  11445. * but added for clarity.
  11446. */
  11447. if (crtc_state->disable_cxsr)
  11448. return true;
  11449. return false;
  11450. }
  11451. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11452. {
  11453. struct drm_device *dev = state->dev;
  11454. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11455. struct drm_i915_private *dev_priv = dev->dev_private;
  11456. struct drm_crtc_state *old_crtc_state;
  11457. struct drm_crtc *crtc;
  11458. struct intel_crtc_state *intel_cstate;
  11459. struct drm_plane *plane;
  11460. struct drm_plane_state *plane_state;
  11461. bool hw_check = intel_state->modeset;
  11462. unsigned long put_domains[I915_MAX_PIPES] = {};
  11463. unsigned crtc_vblank_mask = 0;
  11464. int i, ret;
  11465. for_each_plane_in_state(state, plane, plane_state, i) {
  11466. struct intel_plane_state *intel_plane_state =
  11467. to_intel_plane_state(plane_state);
  11468. if (!intel_plane_state->wait_req)
  11469. continue;
  11470. ret = __i915_wait_request(intel_plane_state->wait_req,
  11471. true, NULL, NULL);
  11472. /* EIO should be eaten, and we can't get interrupted in the
  11473. * worker, and blocking commits have waited already. */
  11474. WARN_ON(ret);
  11475. }
  11476. drm_atomic_helper_wait_for_dependencies(state);
  11477. if (intel_state->modeset) {
  11478. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11479. sizeof(intel_state->min_pixclk));
  11480. dev_priv->active_crtcs = intel_state->active_crtcs;
  11481. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11482. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11483. }
  11484. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11485. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11486. if (needs_modeset(crtc->state) ||
  11487. to_intel_crtc_state(crtc->state)->update_pipe) {
  11488. hw_check = true;
  11489. put_domains[to_intel_crtc(crtc)->pipe] =
  11490. modeset_get_crtc_power_domains(crtc,
  11491. to_intel_crtc_state(crtc->state));
  11492. }
  11493. if (!needs_modeset(crtc->state))
  11494. continue;
  11495. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11496. if (old_crtc_state->active) {
  11497. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11498. dev_priv->display.crtc_disable(crtc);
  11499. intel_crtc->active = false;
  11500. intel_fbc_disable(intel_crtc);
  11501. intel_disable_shared_dpll(intel_crtc);
  11502. /*
  11503. * Underruns don't always raise
  11504. * interrupts, so check manually.
  11505. */
  11506. intel_check_cpu_fifo_underruns(dev_priv);
  11507. intel_check_pch_fifo_underruns(dev_priv);
  11508. if (!crtc->state->active)
  11509. intel_update_watermarks(crtc);
  11510. }
  11511. }
  11512. /* Only after disabling all output pipelines that will be changed can we
  11513. * update the the output configuration. */
  11514. intel_modeset_update_crtc_state(state);
  11515. if (intel_state->modeset) {
  11516. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11517. if (dev_priv->display.modeset_commit_cdclk &&
  11518. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11519. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11520. dev_priv->display.modeset_commit_cdclk(state);
  11521. intel_modeset_verify_disabled(dev);
  11522. }
  11523. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11524. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11525. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11526. bool modeset = needs_modeset(crtc->state);
  11527. struct intel_crtc_state *pipe_config =
  11528. to_intel_crtc_state(crtc->state);
  11529. if (modeset && crtc->state->active) {
  11530. update_scanline_offset(to_intel_crtc(crtc));
  11531. dev_priv->display.crtc_enable(crtc);
  11532. }
  11533. /* Complete events for now disable pipes here. */
  11534. if (modeset && !crtc->state->active && crtc->state->event) {
  11535. spin_lock_irq(&dev->event_lock);
  11536. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  11537. spin_unlock_irq(&dev->event_lock);
  11538. crtc->state->event = NULL;
  11539. }
  11540. if (!modeset)
  11541. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11542. if (crtc->state->active &&
  11543. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11544. intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
  11545. if (crtc->state->active)
  11546. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11547. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11548. crtc_vblank_mask |= 1 << i;
  11549. }
  11550. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  11551. * already, but still need the state for the delayed optimization. To
  11552. * fix this:
  11553. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  11554. * - schedule that vblank worker _before_ calling hw_done
  11555. * - at the start of commit_tail, cancel it _synchrously
  11556. * - switch over to the vblank wait helper in the core after that since
  11557. * we don't need out special handling any more.
  11558. */
  11559. if (!state->legacy_cursor_update)
  11560. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11561. /*
  11562. * Now that the vblank has passed, we can go ahead and program the
  11563. * optimal watermarks on platforms that need two-step watermark
  11564. * programming.
  11565. *
  11566. * TODO: Move this (and other cleanup) to an async worker eventually.
  11567. */
  11568. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11569. intel_cstate = to_intel_crtc_state(crtc->state);
  11570. if (dev_priv->display.optimize_watermarks)
  11571. dev_priv->display.optimize_watermarks(intel_cstate);
  11572. }
  11573. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11574. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11575. if (put_domains[i])
  11576. modeset_put_power_domains(dev_priv, put_domains[i]);
  11577. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11578. }
  11579. drm_atomic_helper_commit_hw_done(state);
  11580. if (intel_state->modeset)
  11581. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11582. mutex_lock(&dev->struct_mutex);
  11583. drm_atomic_helper_cleanup_planes(dev, state);
  11584. mutex_unlock(&dev->struct_mutex);
  11585. drm_atomic_helper_commit_cleanup_done(state);
  11586. drm_atomic_state_free(state);
  11587. /* As one of the primary mmio accessors, KMS has a high likelihood
  11588. * of triggering bugs in unclaimed access. After we finish
  11589. * modesetting, see if an error has been flagged, and if so
  11590. * enable debugging for the next modeset - and hope we catch
  11591. * the culprit.
  11592. *
  11593. * XXX note that we assume display power is on at this point.
  11594. * This might hold true now but we need to add pm helper to check
  11595. * unclaimed only when the hardware is on, as atomic commits
  11596. * can happen also when the device is completely off.
  11597. */
  11598. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11599. }
  11600. static void intel_atomic_commit_work(struct work_struct *work)
  11601. {
  11602. struct drm_atomic_state *state = container_of(work,
  11603. struct drm_atomic_state,
  11604. commit_work);
  11605. intel_atomic_commit_tail(state);
  11606. }
  11607. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  11608. {
  11609. struct drm_plane_state *old_plane_state;
  11610. struct drm_plane *plane;
  11611. struct drm_i915_gem_object *obj, *old_obj;
  11612. struct intel_plane *intel_plane;
  11613. int i;
  11614. mutex_lock(&state->dev->struct_mutex);
  11615. for_each_plane_in_state(state, plane, old_plane_state, i) {
  11616. obj = intel_fb_obj(plane->state->fb);
  11617. old_obj = intel_fb_obj(old_plane_state->fb);
  11618. intel_plane = to_intel_plane(plane);
  11619. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11620. }
  11621. mutex_unlock(&state->dev->struct_mutex);
  11622. }
  11623. /**
  11624. * intel_atomic_commit - commit validated state object
  11625. * @dev: DRM device
  11626. * @state: the top-level driver state object
  11627. * @nonblock: nonblocking commit
  11628. *
  11629. * This function commits a top-level state object that has been validated
  11630. * with drm_atomic_helper_check().
  11631. *
  11632. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11633. * nonblocking commits are only safe for pure plane updates. Everything else
  11634. * should work though.
  11635. *
  11636. * RETURNS
  11637. * Zero for success or -errno.
  11638. */
  11639. static int intel_atomic_commit(struct drm_device *dev,
  11640. struct drm_atomic_state *state,
  11641. bool nonblock)
  11642. {
  11643. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11644. struct drm_i915_private *dev_priv = dev->dev_private;
  11645. int ret = 0;
  11646. if (intel_state->modeset && nonblock) {
  11647. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  11648. return -EINVAL;
  11649. }
  11650. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11651. if (ret)
  11652. return ret;
  11653. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  11654. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11655. if (ret) {
  11656. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11657. return ret;
  11658. }
  11659. drm_atomic_helper_swap_state(state, true);
  11660. dev_priv->wm.distrust_bios_wm = false;
  11661. dev_priv->wm.skl_results = intel_state->wm_results;
  11662. intel_shared_dpll_commit(state);
  11663. intel_atomic_track_fbs(state);
  11664. if (nonblock)
  11665. queue_work(system_unbound_wq, &state->commit_work);
  11666. else
  11667. intel_atomic_commit_tail(state);
  11668. return 0;
  11669. }
  11670. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11671. {
  11672. struct drm_device *dev = crtc->dev;
  11673. struct drm_atomic_state *state;
  11674. struct drm_crtc_state *crtc_state;
  11675. int ret;
  11676. state = drm_atomic_state_alloc(dev);
  11677. if (!state) {
  11678. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11679. crtc->base.id, crtc->name);
  11680. return;
  11681. }
  11682. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11683. retry:
  11684. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11685. ret = PTR_ERR_OR_ZERO(crtc_state);
  11686. if (!ret) {
  11687. if (!crtc_state->active)
  11688. goto out;
  11689. crtc_state->mode_changed = true;
  11690. ret = drm_atomic_commit(state);
  11691. }
  11692. if (ret == -EDEADLK) {
  11693. drm_atomic_state_clear(state);
  11694. drm_modeset_backoff(state->acquire_ctx);
  11695. goto retry;
  11696. }
  11697. if (ret)
  11698. out:
  11699. drm_atomic_state_free(state);
  11700. }
  11701. #undef for_each_intel_crtc_masked
  11702. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11703. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11704. .set_config = drm_atomic_helper_set_config,
  11705. .set_property = drm_atomic_helper_crtc_set_property,
  11706. .destroy = intel_crtc_destroy,
  11707. .page_flip = drm_atomic_helper_page_flip,
  11708. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11709. .atomic_destroy_state = intel_crtc_destroy_state,
  11710. };
  11711. /**
  11712. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11713. * @plane: drm plane to prepare for
  11714. * @fb: framebuffer to prepare for presentation
  11715. *
  11716. * Prepares a framebuffer for usage on a display plane. Generally this
  11717. * involves pinning the underlying object and updating the frontbuffer tracking
  11718. * bits. Some older platforms need special physical address handling for
  11719. * cursor planes.
  11720. *
  11721. * Must be called with struct_mutex held.
  11722. *
  11723. * Returns 0 on success, negative error code on failure.
  11724. */
  11725. int
  11726. intel_prepare_plane_fb(struct drm_plane *plane,
  11727. const struct drm_plane_state *new_state)
  11728. {
  11729. struct drm_device *dev = plane->dev;
  11730. struct drm_framebuffer *fb = new_state->fb;
  11731. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11732. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11733. int ret = 0;
  11734. if (!obj && !old_obj)
  11735. return 0;
  11736. if (old_obj) {
  11737. struct drm_crtc_state *crtc_state =
  11738. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11739. /* Big Hammer, we also need to ensure that any pending
  11740. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11741. * current scanout is retired before unpinning the old
  11742. * framebuffer. Note that we rely on userspace rendering
  11743. * into the buffer attached to the pipe they are waiting
  11744. * on. If not, userspace generates a GPU hang with IPEHR
  11745. * point to the MI_WAIT_FOR_EVENT.
  11746. *
  11747. * This should only fail upon a hung GPU, in which case we
  11748. * can safely continue.
  11749. */
  11750. if (needs_modeset(crtc_state))
  11751. ret = i915_gem_object_wait_rendering(old_obj, true);
  11752. if (ret) {
  11753. /* GPU hangs should have been swallowed by the wait */
  11754. WARN_ON(ret == -EIO);
  11755. return ret;
  11756. }
  11757. }
  11758. /* For framebuffer backed by dmabuf, wait for fence */
  11759. if (obj && obj->base.dma_buf) {
  11760. long lret;
  11761. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11762. false, true,
  11763. MAX_SCHEDULE_TIMEOUT);
  11764. if (lret == -ERESTARTSYS)
  11765. return lret;
  11766. WARN(lret < 0, "waiting returns %li\n", lret);
  11767. }
  11768. if (!obj) {
  11769. ret = 0;
  11770. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11771. INTEL_INFO(dev)->cursor_needs_physical) {
  11772. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11773. ret = i915_gem_object_attach_phys(obj, align);
  11774. if (ret)
  11775. DRM_DEBUG_KMS("failed to attach phys object\n");
  11776. } else {
  11777. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11778. }
  11779. if (ret == 0 && obj) {
  11780. struct intel_plane_state *plane_state =
  11781. to_intel_plane_state(new_state);
  11782. i915_gem_request_assign(&plane_state->wait_req,
  11783. obj->last_write_req);
  11784. }
  11785. return ret;
  11786. }
  11787. /**
  11788. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11789. * @plane: drm plane to clean up for
  11790. * @fb: old framebuffer that was on plane
  11791. *
  11792. * Cleans up a framebuffer that has just been removed from a plane.
  11793. *
  11794. * Must be called with struct_mutex held.
  11795. */
  11796. void
  11797. intel_cleanup_plane_fb(struct drm_plane *plane,
  11798. const struct drm_plane_state *old_state)
  11799. {
  11800. struct drm_device *dev = plane->dev;
  11801. struct intel_plane_state *old_intel_state;
  11802. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11803. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11804. old_intel_state = to_intel_plane_state(old_state);
  11805. if (!obj && !old_obj)
  11806. return;
  11807. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11808. !INTEL_INFO(dev)->cursor_needs_physical))
  11809. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11810. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11811. }
  11812. int
  11813. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11814. {
  11815. int max_scale;
  11816. struct drm_device *dev;
  11817. struct drm_i915_private *dev_priv;
  11818. int crtc_clock, cdclk;
  11819. if (!intel_crtc || !crtc_state->base.enable)
  11820. return DRM_PLANE_HELPER_NO_SCALING;
  11821. dev = intel_crtc->base.dev;
  11822. dev_priv = dev->dev_private;
  11823. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11824. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11825. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11826. return DRM_PLANE_HELPER_NO_SCALING;
  11827. /*
  11828. * skl max scale is lower of:
  11829. * close to 3 but not 3, -1 is for that purpose
  11830. * or
  11831. * cdclk/crtc_clock
  11832. */
  11833. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11834. return max_scale;
  11835. }
  11836. static int
  11837. intel_check_primary_plane(struct drm_plane *plane,
  11838. struct intel_crtc_state *crtc_state,
  11839. struct intel_plane_state *state)
  11840. {
  11841. struct drm_crtc *crtc = state->base.crtc;
  11842. struct drm_framebuffer *fb = state->base.fb;
  11843. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11844. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11845. bool can_position = false;
  11846. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11847. /* use scaler when colorkey is not required */
  11848. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11849. min_scale = 1;
  11850. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11851. }
  11852. can_position = true;
  11853. }
  11854. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11855. &state->dst, &state->clip,
  11856. min_scale, max_scale,
  11857. can_position, true,
  11858. &state->visible);
  11859. }
  11860. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11861. struct drm_crtc_state *old_crtc_state)
  11862. {
  11863. struct drm_device *dev = crtc->dev;
  11864. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11865. struct intel_crtc_state *old_intel_state =
  11866. to_intel_crtc_state(old_crtc_state);
  11867. bool modeset = needs_modeset(crtc->state);
  11868. /* Perform vblank evasion around commit operation */
  11869. intel_pipe_update_start(intel_crtc);
  11870. if (modeset)
  11871. return;
  11872. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11873. intel_color_set_csc(crtc->state);
  11874. intel_color_load_luts(crtc->state);
  11875. }
  11876. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11877. intel_update_pipe_config(intel_crtc, old_intel_state);
  11878. else if (INTEL_INFO(dev)->gen >= 9)
  11879. skl_detach_scalers(intel_crtc);
  11880. }
  11881. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11882. struct drm_crtc_state *old_crtc_state)
  11883. {
  11884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11885. intel_pipe_update_end(intel_crtc, NULL);
  11886. }
  11887. /**
  11888. * intel_plane_destroy - destroy a plane
  11889. * @plane: plane to destroy
  11890. *
  11891. * Common destruction function for all types of planes (primary, cursor,
  11892. * sprite).
  11893. */
  11894. void intel_plane_destroy(struct drm_plane *plane)
  11895. {
  11896. if (!plane)
  11897. return;
  11898. drm_plane_cleanup(plane);
  11899. kfree(to_intel_plane(plane));
  11900. }
  11901. const struct drm_plane_funcs intel_plane_funcs = {
  11902. .update_plane = drm_atomic_helper_update_plane,
  11903. .disable_plane = drm_atomic_helper_disable_plane,
  11904. .destroy = intel_plane_destroy,
  11905. .set_property = drm_atomic_helper_plane_set_property,
  11906. .atomic_get_property = intel_plane_atomic_get_property,
  11907. .atomic_set_property = intel_plane_atomic_set_property,
  11908. .atomic_duplicate_state = intel_plane_duplicate_state,
  11909. .atomic_destroy_state = intel_plane_destroy_state,
  11910. };
  11911. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11912. int pipe)
  11913. {
  11914. struct intel_plane *primary = NULL;
  11915. struct intel_plane_state *state = NULL;
  11916. const uint32_t *intel_primary_formats;
  11917. unsigned int num_formats;
  11918. int ret;
  11919. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11920. if (!primary)
  11921. goto fail;
  11922. state = intel_create_plane_state(&primary->base);
  11923. if (!state)
  11924. goto fail;
  11925. primary->base.state = &state->base;
  11926. primary->can_scale = false;
  11927. primary->max_downscale = 1;
  11928. if (INTEL_INFO(dev)->gen >= 9) {
  11929. primary->can_scale = true;
  11930. state->scaler_id = -1;
  11931. }
  11932. primary->pipe = pipe;
  11933. primary->plane = pipe;
  11934. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11935. primary->check_plane = intel_check_primary_plane;
  11936. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11937. primary->plane = !pipe;
  11938. if (INTEL_INFO(dev)->gen >= 9) {
  11939. intel_primary_formats = skl_primary_formats;
  11940. num_formats = ARRAY_SIZE(skl_primary_formats);
  11941. primary->update_plane = skylake_update_primary_plane;
  11942. primary->disable_plane = skylake_disable_primary_plane;
  11943. } else if (HAS_PCH_SPLIT(dev)) {
  11944. intel_primary_formats = i965_primary_formats;
  11945. num_formats = ARRAY_SIZE(i965_primary_formats);
  11946. primary->update_plane = ironlake_update_primary_plane;
  11947. primary->disable_plane = i9xx_disable_primary_plane;
  11948. } else if (INTEL_INFO(dev)->gen >= 4) {
  11949. intel_primary_formats = i965_primary_formats;
  11950. num_formats = ARRAY_SIZE(i965_primary_formats);
  11951. primary->update_plane = i9xx_update_primary_plane;
  11952. primary->disable_plane = i9xx_disable_primary_plane;
  11953. } else {
  11954. intel_primary_formats = i8xx_primary_formats;
  11955. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11956. primary->update_plane = i9xx_update_primary_plane;
  11957. primary->disable_plane = i9xx_disable_primary_plane;
  11958. }
  11959. if (INTEL_INFO(dev)->gen >= 9)
  11960. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11961. &intel_plane_funcs,
  11962. intel_primary_formats, num_formats,
  11963. DRM_PLANE_TYPE_PRIMARY,
  11964. "plane 1%c", pipe_name(pipe));
  11965. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11966. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11967. &intel_plane_funcs,
  11968. intel_primary_formats, num_formats,
  11969. DRM_PLANE_TYPE_PRIMARY,
  11970. "primary %c", pipe_name(pipe));
  11971. else
  11972. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11973. &intel_plane_funcs,
  11974. intel_primary_formats, num_formats,
  11975. DRM_PLANE_TYPE_PRIMARY,
  11976. "plane %c", plane_name(primary->plane));
  11977. if (ret)
  11978. goto fail;
  11979. if (INTEL_INFO(dev)->gen >= 4)
  11980. intel_create_rotation_property(dev, primary);
  11981. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11982. return &primary->base;
  11983. fail:
  11984. kfree(state);
  11985. kfree(primary);
  11986. return NULL;
  11987. }
  11988. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11989. {
  11990. if (!dev->mode_config.rotation_property) {
  11991. unsigned long flags = BIT(DRM_ROTATE_0) |
  11992. BIT(DRM_ROTATE_180);
  11993. if (INTEL_INFO(dev)->gen >= 9)
  11994. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11995. dev->mode_config.rotation_property =
  11996. drm_mode_create_rotation_property(dev, flags);
  11997. }
  11998. if (dev->mode_config.rotation_property)
  11999. drm_object_attach_property(&plane->base.base,
  12000. dev->mode_config.rotation_property,
  12001. plane->base.state->rotation);
  12002. }
  12003. static int
  12004. intel_check_cursor_plane(struct drm_plane *plane,
  12005. struct intel_crtc_state *crtc_state,
  12006. struct intel_plane_state *state)
  12007. {
  12008. struct drm_crtc *crtc = crtc_state->base.crtc;
  12009. struct drm_framebuffer *fb = state->base.fb;
  12010. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12011. enum pipe pipe = to_intel_plane(plane)->pipe;
  12012. unsigned stride;
  12013. int ret;
  12014. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  12015. &state->dst, &state->clip,
  12016. DRM_PLANE_HELPER_NO_SCALING,
  12017. DRM_PLANE_HELPER_NO_SCALING,
  12018. true, true, &state->visible);
  12019. if (ret)
  12020. return ret;
  12021. /* if we want to turn off the cursor ignore width and height */
  12022. if (!obj)
  12023. return 0;
  12024. /* Check for which cursor types we support */
  12025. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  12026. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12027. state->base.crtc_w, state->base.crtc_h);
  12028. return -EINVAL;
  12029. }
  12030. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12031. if (obj->base.size < stride * state->base.crtc_h) {
  12032. DRM_DEBUG_KMS("buffer is too small\n");
  12033. return -ENOMEM;
  12034. }
  12035. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12036. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12037. return -EINVAL;
  12038. }
  12039. /*
  12040. * There's something wrong with the cursor on CHV pipe C.
  12041. * If it straddles the left edge of the screen then
  12042. * moving it away from the edge or disabling it often
  12043. * results in a pipe underrun, and often that can lead to
  12044. * dead pipe (constant underrun reported, and it scans
  12045. * out just a solid color). To recover from that, the
  12046. * display power well must be turned off and on again.
  12047. * Refuse the put the cursor into that compromised position.
  12048. */
  12049. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12050. state->visible && state->base.crtc_x < 0) {
  12051. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12052. return -EINVAL;
  12053. }
  12054. return 0;
  12055. }
  12056. static void
  12057. intel_disable_cursor_plane(struct drm_plane *plane,
  12058. struct drm_crtc *crtc)
  12059. {
  12060. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12061. intel_crtc->cursor_addr = 0;
  12062. intel_crtc_update_cursor(crtc, NULL);
  12063. }
  12064. static void
  12065. intel_update_cursor_plane(struct drm_plane *plane,
  12066. const struct intel_crtc_state *crtc_state,
  12067. const struct intel_plane_state *state)
  12068. {
  12069. struct drm_crtc *crtc = crtc_state->base.crtc;
  12070. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12071. struct drm_device *dev = plane->dev;
  12072. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12073. uint32_t addr;
  12074. if (!obj)
  12075. addr = 0;
  12076. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12077. addr = i915_gem_obj_ggtt_offset(obj);
  12078. else
  12079. addr = obj->phys_handle->busaddr;
  12080. intel_crtc->cursor_addr = addr;
  12081. intel_crtc_update_cursor(crtc, state);
  12082. }
  12083. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12084. int pipe)
  12085. {
  12086. struct intel_plane *cursor = NULL;
  12087. struct intel_plane_state *state = NULL;
  12088. int ret;
  12089. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12090. if (!cursor)
  12091. goto fail;
  12092. state = intel_create_plane_state(&cursor->base);
  12093. if (!state)
  12094. goto fail;
  12095. cursor->base.state = &state->base;
  12096. cursor->can_scale = false;
  12097. cursor->max_downscale = 1;
  12098. cursor->pipe = pipe;
  12099. cursor->plane = pipe;
  12100. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12101. cursor->check_plane = intel_check_cursor_plane;
  12102. cursor->update_plane = intel_update_cursor_plane;
  12103. cursor->disable_plane = intel_disable_cursor_plane;
  12104. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12105. &intel_plane_funcs,
  12106. intel_cursor_formats,
  12107. ARRAY_SIZE(intel_cursor_formats),
  12108. DRM_PLANE_TYPE_CURSOR,
  12109. "cursor %c", pipe_name(pipe));
  12110. if (ret)
  12111. goto fail;
  12112. if (INTEL_INFO(dev)->gen >= 4) {
  12113. if (!dev->mode_config.rotation_property)
  12114. dev->mode_config.rotation_property =
  12115. drm_mode_create_rotation_property(dev,
  12116. BIT(DRM_ROTATE_0) |
  12117. BIT(DRM_ROTATE_180));
  12118. if (dev->mode_config.rotation_property)
  12119. drm_object_attach_property(&cursor->base.base,
  12120. dev->mode_config.rotation_property,
  12121. state->base.rotation);
  12122. }
  12123. if (INTEL_INFO(dev)->gen >=9)
  12124. state->scaler_id = -1;
  12125. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12126. return &cursor->base;
  12127. fail:
  12128. kfree(state);
  12129. kfree(cursor);
  12130. return NULL;
  12131. }
  12132. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12133. struct intel_crtc_state *crtc_state)
  12134. {
  12135. int i;
  12136. struct intel_scaler *intel_scaler;
  12137. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12138. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12139. intel_scaler = &scaler_state->scalers[i];
  12140. intel_scaler->in_use = 0;
  12141. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12142. }
  12143. scaler_state->scaler_id = -1;
  12144. }
  12145. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12146. {
  12147. struct drm_i915_private *dev_priv = dev->dev_private;
  12148. struct intel_crtc *intel_crtc;
  12149. struct intel_crtc_state *crtc_state = NULL;
  12150. struct drm_plane *primary = NULL;
  12151. struct drm_plane *cursor = NULL;
  12152. int ret;
  12153. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12154. if (intel_crtc == NULL)
  12155. return;
  12156. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12157. if (!crtc_state)
  12158. goto fail;
  12159. intel_crtc->config = crtc_state;
  12160. intel_crtc->base.state = &crtc_state->base;
  12161. crtc_state->base.crtc = &intel_crtc->base;
  12162. /* initialize shared scalers */
  12163. if (INTEL_INFO(dev)->gen >= 9) {
  12164. if (pipe == PIPE_C)
  12165. intel_crtc->num_scalers = 1;
  12166. else
  12167. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12168. skl_init_scalers(dev, intel_crtc, crtc_state);
  12169. }
  12170. primary = intel_primary_plane_create(dev, pipe);
  12171. if (!primary)
  12172. goto fail;
  12173. cursor = intel_cursor_plane_create(dev, pipe);
  12174. if (!cursor)
  12175. goto fail;
  12176. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12177. cursor, &intel_crtc_funcs,
  12178. "pipe %c", pipe_name(pipe));
  12179. if (ret)
  12180. goto fail;
  12181. /*
  12182. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12183. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12184. */
  12185. intel_crtc->pipe = pipe;
  12186. intel_crtc->plane = pipe;
  12187. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12188. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12189. intel_crtc->plane = !pipe;
  12190. }
  12191. intel_crtc->cursor_base = ~0;
  12192. intel_crtc->cursor_cntl = ~0;
  12193. intel_crtc->cursor_size = ~0;
  12194. intel_crtc->wm.cxsr_allowed = true;
  12195. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12196. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12197. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12198. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12199. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12200. intel_color_init(&intel_crtc->base);
  12201. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12202. return;
  12203. fail:
  12204. intel_plane_destroy(primary);
  12205. intel_plane_destroy(cursor);
  12206. kfree(crtc_state);
  12207. kfree(intel_crtc);
  12208. }
  12209. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12210. {
  12211. struct drm_encoder *encoder = connector->base.encoder;
  12212. struct drm_device *dev = connector->base.dev;
  12213. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12214. if (!encoder || WARN_ON(!encoder->crtc))
  12215. return INVALID_PIPE;
  12216. return to_intel_crtc(encoder->crtc)->pipe;
  12217. }
  12218. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12219. struct drm_file *file)
  12220. {
  12221. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12222. struct drm_crtc *drmmode_crtc;
  12223. struct intel_crtc *crtc;
  12224. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12225. if (!drmmode_crtc) {
  12226. DRM_ERROR("no such CRTC id\n");
  12227. return -ENOENT;
  12228. }
  12229. crtc = to_intel_crtc(drmmode_crtc);
  12230. pipe_from_crtc_id->pipe = crtc->pipe;
  12231. return 0;
  12232. }
  12233. static int intel_encoder_clones(struct intel_encoder *encoder)
  12234. {
  12235. struct drm_device *dev = encoder->base.dev;
  12236. struct intel_encoder *source_encoder;
  12237. int index_mask = 0;
  12238. int entry = 0;
  12239. for_each_intel_encoder(dev, source_encoder) {
  12240. if (encoders_cloneable(encoder, source_encoder))
  12241. index_mask |= (1 << entry);
  12242. entry++;
  12243. }
  12244. return index_mask;
  12245. }
  12246. static bool has_edp_a(struct drm_device *dev)
  12247. {
  12248. struct drm_i915_private *dev_priv = dev->dev_private;
  12249. if (!IS_MOBILE(dev))
  12250. return false;
  12251. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12252. return false;
  12253. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12254. return false;
  12255. return true;
  12256. }
  12257. static bool intel_crt_present(struct drm_device *dev)
  12258. {
  12259. struct drm_i915_private *dev_priv = dev->dev_private;
  12260. if (INTEL_INFO(dev)->gen >= 9)
  12261. return false;
  12262. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12263. return false;
  12264. if (IS_CHERRYVIEW(dev))
  12265. return false;
  12266. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12267. return false;
  12268. /* DDI E can't be used if DDI A requires 4 lanes */
  12269. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12270. return false;
  12271. if (!dev_priv->vbt.int_crt_support)
  12272. return false;
  12273. return true;
  12274. }
  12275. static void intel_setup_outputs(struct drm_device *dev)
  12276. {
  12277. struct drm_i915_private *dev_priv = dev->dev_private;
  12278. struct intel_encoder *encoder;
  12279. bool dpd_is_edp = false;
  12280. intel_lvds_init(dev);
  12281. if (intel_crt_present(dev))
  12282. intel_crt_init(dev);
  12283. if (IS_BROXTON(dev)) {
  12284. /*
  12285. * FIXME: Broxton doesn't support port detection via the
  12286. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12287. * detect the ports.
  12288. */
  12289. intel_ddi_init(dev, PORT_A);
  12290. intel_ddi_init(dev, PORT_B);
  12291. intel_ddi_init(dev, PORT_C);
  12292. intel_dsi_init(dev);
  12293. } else if (HAS_DDI(dev)) {
  12294. int found;
  12295. /*
  12296. * Haswell uses DDI functions to detect digital outputs.
  12297. * On SKL pre-D0 the strap isn't connected, so we assume
  12298. * it's there.
  12299. */
  12300. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12301. /* WaIgnoreDDIAStrap: skl */
  12302. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12303. intel_ddi_init(dev, PORT_A);
  12304. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12305. * register */
  12306. found = I915_READ(SFUSE_STRAP);
  12307. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12308. intel_ddi_init(dev, PORT_B);
  12309. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12310. intel_ddi_init(dev, PORT_C);
  12311. if (found & SFUSE_STRAP_DDID_DETECTED)
  12312. intel_ddi_init(dev, PORT_D);
  12313. /*
  12314. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12315. */
  12316. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12317. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12318. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12319. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12320. intel_ddi_init(dev, PORT_E);
  12321. } else if (HAS_PCH_SPLIT(dev)) {
  12322. int found;
  12323. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12324. if (has_edp_a(dev))
  12325. intel_dp_init(dev, DP_A, PORT_A);
  12326. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12327. /* PCH SDVOB multiplex with HDMIB */
  12328. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12329. if (!found)
  12330. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12331. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12332. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12333. }
  12334. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12335. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12336. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12337. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12338. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12339. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12340. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12341. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12342. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12343. bool has_edp, has_port;
  12344. /*
  12345. * The DP_DETECTED bit is the latched state of the DDC
  12346. * SDA pin at boot. However since eDP doesn't require DDC
  12347. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12348. * eDP ports may have been muxed to an alternate function.
  12349. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12350. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12351. * detect eDP ports.
  12352. *
  12353. * Sadly the straps seem to be missing sometimes even for HDMI
  12354. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12355. * and VBT for the presence of the port. Additionally we can't
  12356. * trust the port type the VBT declares as we've seen at least
  12357. * HDMI ports that the VBT claim are DP or eDP.
  12358. */
  12359. has_edp = intel_dp_is_edp(dev, PORT_B);
  12360. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12361. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12362. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12363. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12364. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12365. has_edp = intel_dp_is_edp(dev, PORT_C);
  12366. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12367. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12368. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12369. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12370. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12371. if (IS_CHERRYVIEW(dev)) {
  12372. /*
  12373. * eDP not supported on port D,
  12374. * so no need to worry about it
  12375. */
  12376. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12377. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12378. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12379. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12380. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12381. }
  12382. intel_dsi_init(dev);
  12383. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12384. bool found = false;
  12385. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12386. DRM_DEBUG_KMS("probing SDVOB\n");
  12387. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12388. if (!found && IS_G4X(dev)) {
  12389. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12390. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12391. }
  12392. if (!found && IS_G4X(dev))
  12393. intel_dp_init(dev, DP_B, PORT_B);
  12394. }
  12395. /* Before G4X SDVOC doesn't have its own detect register */
  12396. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12397. DRM_DEBUG_KMS("probing SDVOC\n");
  12398. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12399. }
  12400. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12401. if (IS_G4X(dev)) {
  12402. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12403. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12404. }
  12405. if (IS_G4X(dev))
  12406. intel_dp_init(dev, DP_C, PORT_C);
  12407. }
  12408. if (IS_G4X(dev) &&
  12409. (I915_READ(DP_D) & DP_DETECTED))
  12410. intel_dp_init(dev, DP_D, PORT_D);
  12411. } else if (IS_GEN2(dev))
  12412. intel_dvo_init(dev);
  12413. if (SUPPORTS_TV(dev))
  12414. intel_tv_init(dev);
  12415. intel_psr_init(dev);
  12416. for_each_intel_encoder(dev, encoder) {
  12417. encoder->base.possible_crtcs = encoder->crtc_mask;
  12418. encoder->base.possible_clones =
  12419. intel_encoder_clones(encoder);
  12420. }
  12421. intel_init_pch_refclk(dev);
  12422. drm_helper_move_panel_connectors_to_head(dev);
  12423. }
  12424. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12425. {
  12426. struct drm_device *dev = fb->dev;
  12427. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12428. drm_framebuffer_cleanup(fb);
  12429. mutex_lock(&dev->struct_mutex);
  12430. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12431. drm_gem_object_unreference(&intel_fb->obj->base);
  12432. mutex_unlock(&dev->struct_mutex);
  12433. kfree(intel_fb);
  12434. }
  12435. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12436. struct drm_file *file,
  12437. unsigned int *handle)
  12438. {
  12439. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12440. struct drm_i915_gem_object *obj = intel_fb->obj;
  12441. if (obj->userptr.mm) {
  12442. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12443. return -EINVAL;
  12444. }
  12445. return drm_gem_handle_create(file, &obj->base, handle);
  12446. }
  12447. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12448. struct drm_file *file,
  12449. unsigned flags, unsigned color,
  12450. struct drm_clip_rect *clips,
  12451. unsigned num_clips)
  12452. {
  12453. struct drm_device *dev = fb->dev;
  12454. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12455. struct drm_i915_gem_object *obj = intel_fb->obj;
  12456. mutex_lock(&dev->struct_mutex);
  12457. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12458. mutex_unlock(&dev->struct_mutex);
  12459. return 0;
  12460. }
  12461. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12462. .destroy = intel_user_framebuffer_destroy,
  12463. .create_handle = intel_user_framebuffer_create_handle,
  12464. .dirty = intel_user_framebuffer_dirty,
  12465. };
  12466. static
  12467. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12468. uint32_t pixel_format)
  12469. {
  12470. u32 gen = INTEL_INFO(dev)->gen;
  12471. if (gen >= 9) {
  12472. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12473. /* "The stride in bytes must not exceed the of the size of 8K
  12474. * pixels and 32K bytes."
  12475. */
  12476. return min(8192 * cpp, 32768);
  12477. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12478. return 32*1024;
  12479. } else if (gen >= 4) {
  12480. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12481. return 16*1024;
  12482. else
  12483. return 32*1024;
  12484. } else if (gen >= 3) {
  12485. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12486. return 8*1024;
  12487. else
  12488. return 16*1024;
  12489. } else {
  12490. /* XXX DSPC is limited to 4k tiled */
  12491. return 8*1024;
  12492. }
  12493. }
  12494. static int intel_framebuffer_init(struct drm_device *dev,
  12495. struct intel_framebuffer *intel_fb,
  12496. struct drm_mode_fb_cmd2 *mode_cmd,
  12497. struct drm_i915_gem_object *obj)
  12498. {
  12499. struct drm_i915_private *dev_priv = to_i915(dev);
  12500. unsigned int aligned_height;
  12501. int ret;
  12502. u32 pitch_limit, stride_alignment;
  12503. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12504. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12505. /* Enforce that fb modifier and tiling mode match, but only for
  12506. * X-tiled. This is needed for FBC. */
  12507. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12508. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12509. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12510. return -EINVAL;
  12511. }
  12512. } else {
  12513. if (obj->tiling_mode == I915_TILING_X)
  12514. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12515. else if (obj->tiling_mode == I915_TILING_Y) {
  12516. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12517. return -EINVAL;
  12518. }
  12519. }
  12520. /* Passed in modifier sanity checking. */
  12521. switch (mode_cmd->modifier[0]) {
  12522. case I915_FORMAT_MOD_Y_TILED:
  12523. case I915_FORMAT_MOD_Yf_TILED:
  12524. if (INTEL_INFO(dev)->gen < 9) {
  12525. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12526. mode_cmd->modifier[0]);
  12527. return -EINVAL;
  12528. }
  12529. case DRM_FORMAT_MOD_NONE:
  12530. case I915_FORMAT_MOD_X_TILED:
  12531. break;
  12532. default:
  12533. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12534. mode_cmd->modifier[0]);
  12535. return -EINVAL;
  12536. }
  12537. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12538. mode_cmd->modifier[0],
  12539. mode_cmd->pixel_format);
  12540. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12541. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12542. mode_cmd->pitches[0], stride_alignment);
  12543. return -EINVAL;
  12544. }
  12545. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12546. mode_cmd->pixel_format);
  12547. if (mode_cmd->pitches[0] > pitch_limit) {
  12548. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12549. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12550. "tiled" : "linear",
  12551. mode_cmd->pitches[0], pitch_limit);
  12552. return -EINVAL;
  12553. }
  12554. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12555. mode_cmd->pitches[0] != obj->stride) {
  12556. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12557. mode_cmd->pitches[0], obj->stride);
  12558. return -EINVAL;
  12559. }
  12560. /* Reject formats not supported by any plane early. */
  12561. switch (mode_cmd->pixel_format) {
  12562. case DRM_FORMAT_C8:
  12563. case DRM_FORMAT_RGB565:
  12564. case DRM_FORMAT_XRGB8888:
  12565. case DRM_FORMAT_ARGB8888:
  12566. break;
  12567. case DRM_FORMAT_XRGB1555:
  12568. if (INTEL_INFO(dev)->gen > 3) {
  12569. DRM_DEBUG("unsupported pixel format: %s\n",
  12570. drm_get_format_name(mode_cmd->pixel_format));
  12571. return -EINVAL;
  12572. }
  12573. break;
  12574. case DRM_FORMAT_ABGR8888:
  12575. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12576. INTEL_INFO(dev)->gen < 9) {
  12577. DRM_DEBUG("unsupported pixel format: %s\n",
  12578. drm_get_format_name(mode_cmd->pixel_format));
  12579. return -EINVAL;
  12580. }
  12581. break;
  12582. case DRM_FORMAT_XBGR8888:
  12583. case DRM_FORMAT_XRGB2101010:
  12584. case DRM_FORMAT_XBGR2101010:
  12585. if (INTEL_INFO(dev)->gen < 4) {
  12586. DRM_DEBUG("unsupported pixel format: %s\n",
  12587. drm_get_format_name(mode_cmd->pixel_format));
  12588. return -EINVAL;
  12589. }
  12590. break;
  12591. case DRM_FORMAT_ABGR2101010:
  12592. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12593. DRM_DEBUG("unsupported pixel format: %s\n",
  12594. drm_get_format_name(mode_cmd->pixel_format));
  12595. return -EINVAL;
  12596. }
  12597. break;
  12598. case DRM_FORMAT_YUYV:
  12599. case DRM_FORMAT_UYVY:
  12600. case DRM_FORMAT_YVYU:
  12601. case DRM_FORMAT_VYUY:
  12602. if (INTEL_INFO(dev)->gen < 5) {
  12603. DRM_DEBUG("unsupported pixel format: %s\n",
  12604. drm_get_format_name(mode_cmd->pixel_format));
  12605. return -EINVAL;
  12606. }
  12607. break;
  12608. default:
  12609. DRM_DEBUG("unsupported pixel format: %s\n",
  12610. drm_get_format_name(mode_cmd->pixel_format));
  12611. return -EINVAL;
  12612. }
  12613. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12614. if (mode_cmd->offsets[0] != 0)
  12615. return -EINVAL;
  12616. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12617. mode_cmd->pixel_format,
  12618. mode_cmd->modifier[0]);
  12619. /* FIXME drm helper for size checks (especially planar formats)? */
  12620. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12621. return -EINVAL;
  12622. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12623. intel_fb->obj = obj;
  12624. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12625. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12626. if (ret) {
  12627. DRM_ERROR("framebuffer init failed %d\n", ret);
  12628. return ret;
  12629. }
  12630. intel_fb->obj->framebuffer_references++;
  12631. return 0;
  12632. }
  12633. static struct drm_framebuffer *
  12634. intel_user_framebuffer_create(struct drm_device *dev,
  12635. struct drm_file *filp,
  12636. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12637. {
  12638. struct drm_framebuffer *fb;
  12639. struct drm_i915_gem_object *obj;
  12640. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12641. obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
  12642. if (&obj->base == NULL)
  12643. return ERR_PTR(-ENOENT);
  12644. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12645. if (IS_ERR(fb))
  12646. drm_gem_object_unreference_unlocked(&obj->base);
  12647. return fb;
  12648. }
  12649. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12650. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12651. {
  12652. }
  12653. #endif
  12654. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12655. .fb_create = intel_user_framebuffer_create,
  12656. .output_poll_changed = intel_fbdev_output_poll_changed,
  12657. .atomic_check = intel_atomic_check,
  12658. .atomic_commit = intel_atomic_commit,
  12659. .atomic_state_alloc = intel_atomic_state_alloc,
  12660. .atomic_state_clear = intel_atomic_state_clear,
  12661. };
  12662. /**
  12663. * intel_init_display_hooks - initialize the display modesetting hooks
  12664. * @dev_priv: device private
  12665. */
  12666. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12667. {
  12668. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12669. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12670. dev_priv->display.get_initial_plane_config =
  12671. skylake_get_initial_plane_config;
  12672. dev_priv->display.crtc_compute_clock =
  12673. haswell_crtc_compute_clock;
  12674. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12675. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12676. } else if (HAS_DDI(dev_priv)) {
  12677. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12678. dev_priv->display.get_initial_plane_config =
  12679. ironlake_get_initial_plane_config;
  12680. dev_priv->display.crtc_compute_clock =
  12681. haswell_crtc_compute_clock;
  12682. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12683. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12684. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12685. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12686. dev_priv->display.get_initial_plane_config =
  12687. ironlake_get_initial_plane_config;
  12688. dev_priv->display.crtc_compute_clock =
  12689. ironlake_crtc_compute_clock;
  12690. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12691. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12692. } else if (IS_CHERRYVIEW(dev_priv)) {
  12693. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12694. dev_priv->display.get_initial_plane_config =
  12695. i9xx_get_initial_plane_config;
  12696. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12697. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12698. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12699. } else if (IS_VALLEYVIEW(dev_priv)) {
  12700. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12701. dev_priv->display.get_initial_plane_config =
  12702. i9xx_get_initial_plane_config;
  12703. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12704. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12705. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12706. } else if (IS_G4X(dev_priv)) {
  12707. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12708. dev_priv->display.get_initial_plane_config =
  12709. i9xx_get_initial_plane_config;
  12710. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12711. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12712. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12713. } else if (IS_PINEVIEW(dev_priv)) {
  12714. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12715. dev_priv->display.get_initial_plane_config =
  12716. i9xx_get_initial_plane_config;
  12717. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12718. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12719. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12720. } else if (!IS_GEN2(dev_priv)) {
  12721. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12722. dev_priv->display.get_initial_plane_config =
  12723. i9xx_get_initial_plane_config;
  12724. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12725. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12726. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12727. } else {
  12728. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12729. dev_priv->display.get_initial_plane_config =
  12730. i9xx_get_initial_plane_config;
  12731. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12732. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12733. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12734. }
  12735. /* Returns the core display clock speed */
  12736. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12737. dev_priv->display.get_display_clock_speed =
  12738. skylake_get_display_clock_speed;
  12739. else if (IS_BROXTON(dev_priv))
  12740. dev_priv->display.get_display_clock_speed =
  12741. broxton_get_display_clock_speed;
  12742. else if (IS_BROADWELL(dev_priv))
  12743. dev_priv->display.get_display_clock_speed =
  12744. broadwell_get_display_clock_speed;
  12745. else if (IS_HASWELL(dev_priv))
  12746. dev_priv->display.get_display_clock_speed =
  12747. haswell_get_display_clock_speed;
  12748. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12749. dev_priv->display.get_display_clock_speed =
  12750. valleyview_get_display_clock_speed;
  12751. else if (IS_GEN5(dev_priv))
  12752. dev_priv->display.get_display_clock_speed =
  12753. ilk_get_display_clock_speed;
  12754. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12755. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12756. dev_priv->display.get_display_clock_speed =
  12757. i945_get_display_clock_speed;
  12758. else if (IS_GM45(dev_priv))
  12759. dev_priv->display.get_display_clock_speed =
  12760. gm45_get_display_clock_speed;
  12761. else if (IS_CRESTLINE(dev_priv))
  12762. dev_priv->display.get_display_clock_speed =
  12763. i965gm_get_display_clock_speed;
  12764. else if (IS_PINEVIEW(dev_priv))
  12765. dev_priv->display.get_display_clock_speed =
  12766. pnv_get_display_clock_speed;
  12767. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12768. dev_priv->display.get_display_clock_speed =
  12769. g33_get_display_clock_speed;
  12770. else if (IS_I915G(dev_priv))
  12771. dev_priv->display.get_display_clock_speed =
  12772. i915_get_display_clock_speed;
  12773. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12774. dev_priv->display.get_display_clock_speed =
  12775. i9xx_misc_get_display_clock_speed;
  12776. else if (IS_I915GM(dev_priv))
  12777. dev_priv->display.get_display_clock_speed =
  12778. i915gm_get_display_clock_speed;
  12779. else if (IS_I865G(dev_priv))
  12780. dev_priv->display.get_display_clock_speed =
  12781. i865_get_display_clock_speed;
  12782. else if (IS_I85X(dev_priv))
  12783. dev_priv->display.get_display_clock_speed =
  12784. i85x_get_display_clock_speed;
  12785. else { /* 830 */
  12786. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12787. dev_priv->display.get_display_clock_speed =
  12788. i830_get_display_clock_speed;
  12789. }
  12790. if (IS_GEN5(dev_priv)) {
  12791. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12792. } else if (IS_GEN6(dev_priv)) {
  12793. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12794. } else if (IS_IVYBRIDGE(dev_priv)) {
  12795. /* FIXME: detect B0+ stepping and use auto training */
  12796. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12797. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12798. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12799. }
  12800. if (IS_BROADWELL(dev_priv)) {
  12801. dev_priv->display.modeset_commit_cdclk =
  12802. broadwell_modeset_commit_cdclk;
  12803. dev_priv->display.modeset_calc_cdclk =
  12804. broadwell_modeset_calc_cdclk;
  12805. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12806. dev_priv->display.modeset_commit_cdclk =
  12807. valleyview_modeset_commit_cdclk;
  12808. dev_priv->display.modeset_calc_cdclk =
  12809. valleyview_modeset_calc_cdclk;
  12810. } else if (IS_BROXTON(dev_priv)) {
  12811. dev_priv->display.modeset_commit_cdclk =
  12812. bxt_modeset_commit_cdclk;
  12813. dev_priv->display.modeset_calc_cdclk =
  12814. bxt_modeset_calc_cdclk;
  12815. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12816. dev_priv->display.modeset_commit_cdclk =
  12817. skl_modeset_commit_cdclk;
  12818. dev_priv->display.modeset_calc_cdclk =
  12819. skl_modeset_calc_cdclk;
  12820. }
  12821. switch (INTEL_INFO(dev_priv)->gen) {
  12822. case 2:
  12823. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12824. break;
  12825. case 3:
  12826. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12827. break;
  12828. case 4:
  12829. case 5:
  12830. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12831. break;
  12832. case 6:
  12833. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12834. break;
  12835. case 7:
  12836. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12837. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12838. break;
  12839. case 9:
  12840. /* Drop through - unsupported since execlist only. */
  12841. default:
  12842. /* Default just returns -ENODEV to indicate unsupported */
  12843. dev_priv->display.queue_flip = intel_default_queue_flip;
  12844. }
  12845. }
  12846. /*
  12847. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12848. * resume, or other times. This quirk makes sure that's the case for
  12849. * affected systems.
  12850. */
  12851. static void quirk_pipea_force(struct drm_device *dev)
  12852. {
  12853. struct drm_i915_private *dev_priv = dev->dev_private;
  12854. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12855. DRM_INFO("applying pipe a force quirk\n");
  12856. }
  12857. static void quirk_pipeb_force(struct drm_device *dev)
  12858. {
  12859. struct drm_i915_private *dev_priv = dev->dev_private;
  12860. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12861. DRM_INFO("applying pipe b force quirk\n");
  12862. }
  12863. /*
  12864. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12865. */
  12866. static void quirk_ssc_force_disable(struct drm_device *dev)
  12867. {
  12868. struct drm_i915_private *dev_priv = dev->dev_private;
  12869. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12870. DRM_INFO("applying lvds SSC disable quirk\n");
  12871. }
  12872. /*
  12873. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12874. * brightness value
  12875. */
  12876. static void quirk_invert_brightness(struct drm_device *dev)
  12877. {
  12878. struct drm_i915_private *dev_priv = dev->dev_private;
  12879. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12880. DRM_INFO("applying inverted panel brightness quirk\n");
  12881. }
  12882. /* Some VBT's incorrectly indicate no backlight is present */
  12883. static void quirk_backlight_present(struct drm_device *dev)
  12884. {
  12885. struct drm_i915_private *dev_priv = dev->dev_private;
  12886. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12887. DRM_INFO("applying backlight present quirk\n");
  12888. }
  12889. struct intel_quirk {
  12890. int device;
  12891. int subsystem_vendor;
  12892. int subsystem_device;
  12893. void (*hook)(struct drm_device *dev);
  12894. };
  12895. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12896. struct intel_dmi_quirk {
  12897. void (*hook)(struct drm_device *dev);
  12898. const struct dmi_system_id (*dmi_id_list)[];
  12899. };
  12900. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12901. {
  12902. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12903. return 1;
  12904. }
  12905. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12906. {
  12907. .dmi_id_list = &(const struct dmi_system_id[]) {
  12908. {
  12909. .callback = intel_dmi_reverse_brightness,
  12910. .ident = "NCR Corporation",
  12911. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12912. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12913. },
  12914. },
  12915. { } /* terminating entry */
  12916. },
  12917. .hook = quirk_invert_brightness,
  12918. },
  12919. };
  12920. static struct intel_quirk intel_quirks[] = {
  12921. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12922. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12923. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12924. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12925. /* 830 needs to leave pipe A & dpll A up */
  12926. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12927. /* 830 needs to leave pipe B & dpll B up */
  12928. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12929. /* Lenovo U160 cannot use SSC on LVDS */
  12930. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12931. /* Sony Vaio Y cannot use SSC on LVDS */
  12932. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12933. /* Acer Aspire 5734Z must invert backlight brightness */
  12934. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12935. /* Acer/eMachines G725 */
  12936. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12937. /* Acer/eMachines e725 */
  12938. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12939. /* Acer/Packard Bell NCL20 */
  12940. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12941. /* Acer Aspire 4736Z */
  12942. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12943. /* Acer Aspire 5336 */
  12944. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12945. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12946. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12947. /* Acer C720 Chromebook (Core i3 4005U) */
  12948. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12949. /* Apple Macbook 2,1 (Core 2 T7400) */
  12950. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12951. /* Apple Macbook 4,1 */
  12952. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12953. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12954. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12955. /* HP Chromebook 14 (Celeron 2955U) */
  12956. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12957. /* Dell Chromebook 11 */
  12958. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12959. /* Dell Chromebook 11 (2015 version) */
  12960. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12961. };
  12962. static void intel_init_quirks(struct drm_device *dev)
  12963. {
  12964. struct pci_dev *d = dev->pdev;
  12965. int i;
  12966. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12967. struct intel_quirk *q = &intel_quirks[i];
  12968. if (d->device == q->device &&
  12969. (d->subsystem_vendor == q->subsystem_vendor ||
  12970. q->subsystem_vendor == PCI_ANY_ID) &&
  12971. (d->subsystem_device == q->subsystem_device ||
  12972. q->subsystem_device == PCI_ANY_ID))
  12973. q->hook(dev);
  12974. }
  12975. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12976. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12977. intel_dmi_quirks[i].hook(dev);
  12978. }
  12979. }
  12980. /* Disable the VGA plane that we never use */
  12981. static void i915_disable_vga(struct drm_device *dev)
  12982. {
  12983. struct drm_i915_private *dev_priv = dev->dev_private;
  12984. u8 sr1;
  12985. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12986. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12987. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12988. outb(SR01, VGA_SR_INDEX);
  12989. sr1 = inb(VGA_SR_DATA);
  12990. outb(sr1 | 1<<5, VGA_SR_DATA);
  12991. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12992. udelay(300);
  12993. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12994. POSTING_READ(vga_reg);
  12995. }
  12996. void intel_modeset_init_hw(struct drm_device *dev)
  12997. {
  12998. struct drm_i915_private *dev_priv = dev->dev_private;
  12999. intel_update_cdclk(dev);
  13000. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13001. intel_init_clock_gating(dev);
  13002. intel_enable_gt_powersave(dev_priv);
  13003. }
  13004. /*
  13005. * Calculate what we think the watermarks should be for the state we've read
  13006. * out of the hardware and then immediately program those watermarks so that
  13007. * we ensure the hardware settings match our internal state.
  13008. *
  13009. * We can calculate what we think WM's should be by creating a duplicate of the
  13010. * current state (which was constructed during hardware readout) and running it
  13011. * through the atomic check code to calculate new watermark values in the
  13012. * state object.
  13013. */
  13014. static void sanitize_watermarks(struct drm_device *dev)
  13015. {
  13016. struct drm_i915_private *dev_priv = to_i915(dev);
  13017. struct drm_atomic_state *state;
  13018. struct drm_crtc *crtc;
  13019. struct drm_crtc_state *cstate;
  13020. struct drm_modeset_acquire_ctx ctx;
  13021. int ret;
  13022. int i;
  13023. /* Only supported on platforms that use atomic watermark design */
  13024. if (!dev_priv->display.optimize_watermarks)
  13025. return;
  13026. /*
  13027. * We need to hold connection_mutex before calling duplicate_state so
  13028. * that the connector loop is protected.
  13029. */
  13030. drm_modeset_acquire_init(&ctx, 0);
  13031. retry:
  13032. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13033. if (ret == -EDEADLK) {
  13034. drm_modeset_backoff(&ctx);
  13035. goto retry;
  13036. } else if (WARN_ON(ret)) {
  13037. goto fail;
  13038. }
  13039. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13040. if (WARN_ON(IS_ERR(state)))
  13041. goto fail;
  13042. /*
  13043. * Hardware readout is the only time we don't want to calculate
  13044. * intermediate watermarks (since we don't trust the current
  13045. * watermarks).
  13046. */
  13047. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13048. ret = intel_atomic_check(dev, state);
  13049. if (ret) {
  13050. /*
  13051. * If we fail here, it means that the hardware appears to be
  13052. * programmed in a way that shouldn't be possible, given our
  13053. * understanding of watermark requirements. This might mean a
  13054. * mistake in the hardware readout code or a mistake in the
  13055. * watermark calculations for a given platform. Raise a WARN
  13056. * so that this is noticeable.
  13057. *
  13058. * If this actually happens, we'll have to just leave the
  13059. * BIOS-programmed watermarks untouched and hope for the best.
  13060. */
  13061. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13062. goto fail;
  13063. }
  13064. /* Write calculated watermark values back */
  13065. for_each_crtc_in_state(state, crtc, cstate, i) {
  13066. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13067. cs->wm.need_postvbl_update = true;
  13068. dev_priv->display.optimize_watermarks(cs);
  13069. }
  13070. drm_atomic_state_free(state);
  13071. fail:
  13072. drm_modeset_drop_locks(&ctx);
  13073. drm_modeset_acquire_fini(&ctx);
  13074. }
  13075. void intel_modeset_init(struct drm_device *dev)
  13076. {
  13077. struct drm_i915_private *dev_priv = to_i915(dev);
  13078. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13079. int sprite, ret;
  13080. enum pipe pipe;
  13081. struct intel_crtc *crtc;
  13082. drm_mode_config_init(dev);
  13083. dev->mode_config.min_width = 0;
  13084. dev->mode_config.min_height = 0;
  13085. dev->mode_config.preferred_depth = 24;
  13086. dev->mode_config.prefer_shadow = 1;
  13087. dev->mode_config.allow_fb_modifiers = true;
  13088. dev->mode_config.funcs = &intel_mode_funcs;
  13089. intel_init_quirks(dev);
  13090. intel_init_pm(dev);
  13091. if (INTEL_INFO(dev)->num_pipes == 0)
  13092. return;
  13093. /*
  13094. * There may be no VBT; and if the BIOS enabled SSC we can
  13095. * just keep using it to avoid unnecessary flicker. Whereas if the
  13096. * BIOS isn't using it, don't assume it will work even if the VBT
  13097. * indicates as much.
  13098. */
  13099. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13100. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13101. DREF_SSC1_ENABLE);
  13102. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13103. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13104. bios_lvds_use_ssc ? "en" : "dis",
  13105. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13106. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13107. }
  13108. }
  13109. if (IS_GEN2(dev)) {
  13110. dev->mode_config.max_width = 2048;
  13111. dev->mode_config.max_height = 2048;
  13112. } else if (IS_GEN3(dev)) {
  13113. dev->mode_config.max_width = 4096;
  13114. dev->mode_config.max_height = 4096;
  13115. } else {
  13116. dev->mode_config.max_width = 8192;
  13117. dev->mode_config.max_height = 8192;
  13118. }
  13119. if (IS_845G(dev) || IS_I865G(dev)) {
  13120. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13121. dev->mode_config.cursor_height = 1023;
  13122. } else if (IS_GEN2(dev)) {
  13123. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13124. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13125. } else {
  13126. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13127. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13128. }
  13129. dev->mode_config.fb_base = ggtt->mappable_base;
  13130. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13131. INTEL_INFO(dev)->num_pipes,
  13132. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13133. for_each_pipe(dev_priv, pipe) {
  13134. intel_crtc_init(dev, pipe);
  13135. for_each_sprite(dev_priv, pipe, sprite) {
  13136. ret = intel_plane_init(dev, pipe, sprite);
  13137. if (ret)
  13138. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13139. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13140. }
  13141. }
  13142. intel_update_czclk(dev_priv);
  13143. intel_update_cdclk(dev);
  13144. intel_shared_dpll_init(dev);
  13145. if (dev_priv->max_cdclk_freq == 0)
  13146. intel_update_max_cdclk(dev);
  13147. /* Just disable it once at startup */
  13148. i915_disable_vga(dev);
  13149. intel_setup_outputs(dev);
  13150. drm_modeset_lock_all(dev);
  13151. intel_modeset_setup_hw_state(dev);
  13152. drm_modeset_unlock_all(dev);
  13153. for_each_intel_crtc(dev, crtc) {
  13154. struct intel_initial_plane_config plane_config = {};
  13155. if (!crtc->active)
  13156. continue;
  13157. /*
  13158. * Note that reserving the BIOS fb up front prevents us
  13159. * from stuffing other stolen allocations like the ring
  13160. * on top. This prevents some ugliness at boot time, and
  13161. * can even allow for smooth boot transitions if the BIOS
  13162. * fb is large enough for the active pipe configuration.
  13163. */
  13164. dev_priv->display.get_initial_plane_config(crtc,
  13165. &plane_config);
  13166. /*
  13167. * If the fb is shared between multiple heads, we'll
  13168. * just get the first one.
  13169. */
  13170. intel_find_initial_plane_obj(crtc, &plane_config);
  13171. }
  13172. /*
  13173. * Make sure hardware watermarks really match the state we read out.
  13174. * Note that we need to do this after reconstructing the BIOS fb's
  13175. * since the watermark calculation done here will use pstate->fb.
  13176. */
  13177. sanitize_watermarks(dev);
  13178. }
  13179. static void intel_enable_pipe_a(struct drm_device *dev)
  13180. {
  13181. struct intel_connector *connector;
  13182. struct drm_connector *crt = NULL;
  13183. struct intel_load_detect_pipe load_detect_temp;
  13184. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13185. /* We can't just switch on the pipe A, we need to set things up with a
  13186. * proper mode and output configuration. As a gross hack, enable pipe A
  13187. * by enabling the load detect pipe once. */
  13188. for_each_intel_connector(dev, connector) {
  13189. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13190. crt = &connector->base;
  13191. break;
  13192. }
  13193. }
  13194. if (!crt)
  13195. return;
  13196. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13197. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13198. }
  13199. static bool
  13200. intel_check_plane_mapping(struct intel_crtc *crtc)
  13201. {
  13202. struct drm_device *dev = crtc->base.dev;
  13203. struct drm_i915_private *dev_priv = dev->dev_private;
  13204. u32 val;
  13205. if (INTEL_INFO(dev)->num_pipes == 1)
  13206. return true;
  13207. val = I915_READ(DSPCNTR(!crtc->plane));
  13208. if ((val & DISPLAY_PLANE_ENABLE) &&
  13209. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13210. return false;
  13211. return true;
  13212. }
  13213. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13214. {
  13215. struct drm_device *dev = crtc->base.dev;
  13216. struct intel_encoder *encoder;
  13217. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13218. return true;
  13219. return false;
  13220. }
  13221. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13222. {
  13223. struct drm_device *dev = encoder->base.dev;
  13224. struct intel_connector *connector;
  13225. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13226. return true;
  13227. return false;
  13228. }
  13229. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13230. {
  13231. struct drm_device *dev = crtc->base.dev;
  13232. struct drm_i915_private *dev_priv = dev->dev_private;
  13233. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13234. /* Clear any frame start delays used for debugging left by the BIOS */
  13235. if (!transcoder_is_dsi(cpu_transcoder)) {
  13236. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13237. I915_WRITE(reg,
  13238. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13239. }
  13240. /* restore vblank interrupts to correct state */
  13241. drm_crtc_vblank_reset(&crtc->base);
  13242. if (crtc->active) {
  13243. struct intel_plane *plane;
  13244. drm_crtc_vblank_on(&crtc->base);
  13245. /* Disable everything but the primary plane */
  13246. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13247. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13248. continue;
  13249. plane->disable_plane(&plane->base, &crtc->base);
  13250. }
  13251. }
  13252. /* We need to sanitize the plane -> pipe mapping first because this will
  13253. * disable the crtc (and hence change the state) if it is wrong. Note
  13254. * that gen4+ has a fixed plane -> pipe mapping. */
  13255. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13256. bool plane;
  13257. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13258. crtc->base.base.id, crtc->base.name);
  13259. /* Pipe has the wrong plane attached and the plane is active.
  13260. * Temporarily change the plane mapping and disable everything
  13261. * ... */
  13262. plane = crtc->plane;
  13263. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13264. crtc->plane = !plane;
  13265. intel_crtc_disable_noatomic(&crtc->base);
  13266. crtc->plane = plane;
  13267. }
  13268. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13269. crtc->pipe == PIPE_A && !crtc->active) {
  13270. /* BIOS forgot to enable pipe A, this mostly happens after
  13271. * resume. Force-enable the pipe to fix this, the update_dpms
  13272. * call below we restore the pipe to the right state, but leave
  13273. * the required bits on. */
  13274. intel_enable_pipe_a(dev);
  13275. }
  13276. /* Adjust the state of the output pipe according to whether we
  13277. * have active connectors/encoders. */
  13278. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13279. intel_crtc_disable_noatomic(&crtc->base);
  13280. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13281. /*
  13282. * We start out with underrun reporting disabled to avoid races.
  13283. * For correct bookkeeping mark this on active crtcs.
  13284. *
  13285. * Also on gmch platforms we dont have any hardware bits to
  13286. * disable the underrun reporting. Which means we need to start
  13287. * out with underrun reporting disabled also on inactive pipes,
  13288. * since otherwise we'll complain about the garbage we read when
  13289. * e.g. coming up after runtime pm.
  13290. *
  13291. * No protection against concurrent access is required - at
  13292. * worst a fifo underrun happens which also sets this to false.
  13293. */
  13294. crtc->cpu_fifo_underrun_disabled = true;
  13295. crtc->pch_fifo_underrun_disabled = true;
  13296. }
  13297. }
  13298. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13299. {
  13300. struct intel_connector *connector;
  13301. struct drm_device *dev = encoder->base.dev;
  13302. /* We need to check both for a crtc link (meaning that the
  13303. * encoder is active and trying to read from a pipe) and the
  13304. * pipe itself being active. */
  13305. bool has_active_crtc = encoder->base.crtc &&
  13306. to_intel_crtc(encoder->base.crtc)->active;
  13307. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13308. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13309. encoder->base.base.id,
  13310. encoder->base.name);
  13311. /* Connector is active, but has no active pipe. This is
  13312. * fallout from our resume register restoring. Disable
  13313. * the encoder manually again. */
  13314. if (encoder->base.crtc) {
  13315. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13316. encoder->base.base.id,
  13317. encoder->base.name);
  13318. encoder->disable(encoder);
  13319. if (encoder->post_disable)
  13320. encoder->post_disable(encoder);
  13321. }
  13322. encoder->base.crtc = NULL;
  13323. /* Inconsistent output/port/pipe state happens presumably due to
  13324. * a bug in one of the get_hw_state functions. Or someplace else
  13325. * in our code, like the register restore mess on resume. Clamp
  13326. * things to off as a safer default. */
  13327. for_each_intel_connector(dev, connector) {
  13328. if (connector->encoder != encoder)
  13329. continue;
  13330. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13331. connector->base.encoder = NULL;
  13332. }
  13333. }
  13334. /* Enabled encoders without active connectors will be fixed in
  13335. * the crtc fixup. */
  13336. }
  13337. void i915_redisable_vga_power_on(struct drm_device *dev)
  13338. {
  13339. struct drm_i915_private *dev_priv = dev->dev_private;
  13340. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13341. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13342. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13343. i915_disable_vga(dev);
  13344. }
  13345. }
  13346. void i915_redisable_vga(struct drm_device *dev)
  13347. {
  13348. struct drm_i915_private *dev_priv = dev->dev_private;
  13349. /* This function can be called both from intel_modeset_setup_hw_state or
  13350. * at a very early point in our resume sequence, where the power well
  13351. * structures are not yet restored. Since this function is at a very
  13352. * paranoid "someone might have enabled VGA while we were not looking"
  13353. * level, just check if the power well is enabled instead of trying to
  13354. * follow the "don't touch the power well if we don't need it" policy
  13355. * the rest of the driver uses. */
  13356. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13357. return;
  13358. i915_redisable_vga_power_on(dev);
  13359. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13360. }
  13361. static bool primary_get_hw_state(struct intel_plane *plane)
  13362. {
  13363. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13364. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13365. }
  13366. /* FIXME read out full plane state for all planes */
  13367. static void readout_plane_state(struct intel_crtc *crtc)
  13368. {
  13369. struct drm_plane *primary = crtc->base.primary;
  13370. struct intel_plane_state *plane_state =
  13371. to_intel_plane_state(primary->state);
  13372. plane_state->visible = crtc->active &&
  13373. primary_get_hw_state(to_intel_plane(primary));
  13374. if (plane_state->visible)
  13375. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13376. }
  13377. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13378. {
  13379. struct drm_i915_private *dev_priv = dev->dev_private;
  13380. enum pipe pipe;
  13381. struct intel_crtc *crtc;
  13382. struct intel_encoder *encoder;
  13383. struct intel_connector *connector;
  13384. int i;
  13385. dev_priv->active_crtcs = 0;
  13386. for_each_intel_crtc(dev, crtc) {
  13387. struct intel_crtc_state *crtc_state = crtc->config;
  13388. int pixclk = 0;
  13389. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13390. memset(crtc_state, 0, sizeof(*crtc_state));
  13391. crtc_state->base.crtc = &crtc->base;
  13392. crtc_state->base.active = crtc_state->base.enable =
  13393. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13394. crtc->base.enabled = crtc_state->base.enable;
  13395. crtc->active = crtc_state->base.active;
  13396. if (crtc_state->base.active) {
  13397. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13398. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13399. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13400. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13401. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13402. else
  13403. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13404. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13405. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13406. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13407. }
  13408. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13409. readout_plane_state(crtc);
  13410. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13411. crtc->base.base.id, crtc->base.name,
  13412. crtc->active ? "enabled" : "disabled");
  13413. }
  13414. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13415. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13416. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13417. &pll->config.hw_state);
  13418. pll->config.crtc_mask = 0;
  13419. for_each_intel_crtc(dev, crtc) {
  13420. if (crtc->active && crtc->config->shared_dpll == pll)
  13421. pll->config.crtc_mask |= 1 << crtc->pipe;
  13422. }
  13423. pll->active_mask = pll->config.crtc_mask;
  13424. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13425. pll->name, pll->config.crtc_mask, pll->on);
  13426. }
  13427. for_each_intel_encoder(dev, encoder) {
  13428. pipe = 0;
  13429. if (encoder->get_hw_state(encoder, &pipe)) {
  13430. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13431. encoder->base.crtc = &crtc->base;
  13432. encoder->get_config(encoder, crtc->config);
  13433. } else {
  13434. encoder->base.crtc = NULL;
  13435. }
  13436. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13437. encoder->base.base.id,
  13438. encoder->base.name,
  13439. encoder->base.crtc ? "enabled" : "disabled",
  13440. pipe_name(pipe));
  13441. }
  13442. for_each_intel_connector(dev, connector) {
  13443. if (connector->get_hw_state(connector)) {
  13444. connector->base.dpms = DRM_MODE_DPMS_ON;
  13445. encoder = connector->encoder;
  13446. connector->base.encoder = &encoder->base;
  13447. if (encoder->base.crtc &&
  13448. encoder->base.crtc->state->active) {
  13449. /*
  13450. * This has to be done during hardware readout
  13451. * because anything calling .crtc_disable may
  13452. * rely on the connector_mask being accurate.
  13453. */
  13454. encoder->base.crtc->state->connector_mask |=
  13455. 1 << drm_connector_index(&connector->base);
  13456. encoder->base.crtc->state->encoder_mask |=
  13457. 1 << drm_encoder_index(&encoder->base);
  13458. }
  13459. } else {
  13460. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13461. connector->base.encoder = NULL;
  13462. }
  13463. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13464. connector->base.base.id,
  13465. connector->base.name,
  13466. connector->base.encoder ? "enabled" : "disabled");
  13467. }
  13468. for_each_intel_crtc(dev, crtc) {
  13469. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13470. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13471. if (crtc->base.state->active) {
  13472. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13473. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13474. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13475. /*
  13476. * The initial mode needs to be set in order to keep
  13477. * the atomic core happy. It wants a valid mode if the
  13478. * crtc's enabled, so we do the above call.
  13479. *
  13480. * At this point some state updated by the connectors
  13481. * in their ->detect() callback has not run yet, so
  13482. * no recalculation can be done yet.
  13483. *
  13484. * Even if we could do a recalculation and modeset
  13485. * right now it would cause a double modeset if
  13486. * fbdev or userspace chooses a different initial mode.
  13487. *
  13488. * If that happens, someone indicated they wanted a
  13489. * mode change, which means it's safe to do a full
  13490. * recalculation.
  13491. */
  13492. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13493. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13494. update_scanline_offset(crtc);
  13495. }
  13496. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13497. }
  13498. }
  13499. /* Scan out the current hw modeset state,
  13500. * and sanitizes it to the current state
  13501. */
  13502. static void
  13503. intel_modeset_setup_hw_state(struct drm_device *dev)
  13504. {
  13505. struct drm_i915_private *dev_priv = dev->dev_private;
  13506. enum pipe pipe;
  13507. struct intel_crtc *crtc;
  13508. struct intel_encoder *encoder;
  13509. int i;
  13510. intel_modeset_readout_hw_state(dev);
  13511. /* HW state is read out, now we need to sanitize this mess. */
  13512. for_each_intel_encoder(dev, encoder) {
  13513. intel_sanitize_encoder(encoder);
  13514. }
  13515. for_each_pipe(dev_priv, pipe) {
  13516. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13517. intel_sanitize_crtc(crtc);
  13518. intel_dump_pipe_config(crtc, crtc->config,
  13519. "[setup_hw_state]");
  13520. }
  13521. intel_modeset_update_connector_atomic_state(dev);
  13522. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13523. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13524. if (!pll->on || pll->active_mask)
  13525. continue;
  13526. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13527. pll->funcs.disable(dev_priv, pll);
  13528. pll->on = false;
  13529. }
  13530. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13531. vlv_wm_get_hw_state(dev);
  13532. else if (IS_GEN9(dev))
  13533. skl_wm_get_hw_state(dev);
  13534. else if (HAS_PCH_SPLIT(dev))
  13535. ilk_wm_get_hw_state(dev);
  13536. for_each_intel_crtc(dev, crtc) {
  13537. unsigned long put_domains;
  13538. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13539. if (WARN_ON(put_domains))
  13540. modeset_put_power_domains(dev_priv, put_domains);
  13541. }
  13542. intel_display_set_init_power(dev_priv, false);
  13543. intel_fbc_init_pipe_state(dev_priv);
  13544. }
  13545. void intel_display_resume(struct drm_device *dev)
  13546. {
  13547. struct drm_i915_private *dev_priv = to_i915(dev);
  13548. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13549. struct drm_modeset_acquire_ctx ctx;
  13550. int ret;
  13551. bool setup = false;
  13552. dev_priv->modeset_restore_state = NULL;
  13553. /*
  13554. * This is a cludge because with real atomic modeset mode_config.mutex
  13555. * won't be taken. Unfortunately some probed state like
  13556. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13557. * it here for now.
  13558. */
  13559. mutex_lock(&dev->mode_config.mutex);
  13560. drm_modeset_acquire_init(&ctx, 0);
  13561. retry:
  13562. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13563. if (ret == 0 && !setup) {
  13564. setup = true;
  13565. intel_modeset_setup_hw_state(dev);
  13566. i915_redisable_vga(dev);
  13567. }
  13568. if (ret == 0 && state) {
  13569. struct drm_crtc_state *crtc_state;
  13570. struct drm_crtc *crtc;
  13571. int i;
  13572. state->acquire_ctx = &ctx;
  13573. /* ignore any reset values/BIOS leftovers in the WM registers */
  13574. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13575. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13576. /*
  13577. * Force recalculation even if we restore
  13578. * current state. With fast modeset this may not result
  13579. * in a modeset when the state is compatible.
  13580. */
  13581. crtc_state->mode_changed = true;
  13582. }
  13583. ret = drm_atomic_commit(state);
  13584. }
  13585. if (ret == -EDEADLK) {
  13586. drm_modeset_backoff(&ctx);
  13587. goto retry;
  13588. }
  13589. drm_modeset_drop_locks(&ctx);
  13590. drm_modeset_acquire_fini(&ctx);
  13591. mutex_unlock(&dev->mode_config.mutex);
  13592. if (ret) {
  13593. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13594. drm_atomic_state_free(state);
  13595. }
  13596. }
  13597. void intel_modeset_gem_init(struct drm_device *dev)
  13598. {
  13599. struct drm_i915_private *dev_priv = to_i915(dev);
  13600. struct drm_crtc *c;
  13601. struct drm_i915_gem_object *obj;
  13602. int ret;
  13603. intel_init_gt_powersave(dev_priv);
  13604. intel_modeset_init_hw(dev);
  13605. intel_setup_overlay(dev_priv);
  13606. /*
  13607. * Make sure any fbs we allocated at startup are properly
  13608. * pinned & fenced. When we do the allocation it's too early
  13609. * for this.
  13610. */
  13611. for_each_crtc(dev, c) {
  13612. obj = intel_fb_obj(c->primary->fb);
  13613. if (obj == NULL)
  13614. continue;
  13615. mutex_lock(&dev->struct_mutex);
  13616. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13617. c->primary->state->rotation);
  13618. mutex_unlock(&dev->struct_mutex);
  13619. if (ret) {
  13620. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13621. to_intel_crtc(c)->pipe);
  13622. drm_framebuffer_unreference(c->primary->fb);
  13623. c->primary->fb = NULL;
  13624. c->primary->crtc = c->primary->state->crtc = NULL;
  13625. update_state_fb(c->primary);
  13626. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13627. }
  13628. }
  13629. intel_backlight_register(dev);
  13630. }
  13631. void intel_connector_unregister(struct intel_connector *intel_connector)
  13632. {
  13633. struct drm_connector *connector = &intel_connector->base;
  13634. intel_panel_destroy_backlight(connector);
  13635. drm_connector_unregister(connector);
  13636. }
  13637. void intel_modeset_cleanup(struct drm_device *dev)
  13638. {
  13639. struct drm_i915_private *dev_priv = dev->dev_private;
  13640. struct intel_connector *connector;
  13641. intel_disable_gt_powersave(dev_priv);
  13642. intel_backlight_unregister(dev);
  13643. /*
  13644. * Interrupts and polling as the first thing to avoid creating havoc.
  13645. * Too much stuff here (turning of connectors, ...) would
  13646. * experience fancy races otherwise.
  13647. */
  13648. intel_irq_uninstall(dev_priv);
  13649. /*
  13650. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13651. * poll handlers. Hence disable polling after hpd handling is shut down.
  13652. */
  13653. drm_kms_helper_poll_fini(dev);
  13654. intel_unregister_dsm_handler();
  13655. intel_fbc_global_disable(dev_priv);
  13656. /* flush any delayed tasks or pending work */
  13657. flush_scheduled_work();
  13658. /* destroy the backlight and sysfs files before encoders/connectors */
  13659. for_each_intel_connector(dev, connector)
  13660. connector->unregister(connector);
  13661. drm_mode_config_cleanup(dev);
  13662. intel_cleanup_overlay(dev_priv);
  13663. intel_cleanup_gt_powersave(dev_priv);
  13664. intel_teardown_gmbus(dev);
  13665. }
  13666. void intel_connector_attach_encoder(struct intel_connector *connector,
  13667. struct intel_encoder *encoder)
  13668. {
  13669. connector->encoder = encoder;
  13670. drm_mode_connector_attach_encoder(&connector->base,
  13671. &encoder->base);
  13672. }
  13673. /*
  13674. * set vga decode state - true == enable VGA decode
  13675. */
  13676. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13677. {
  13678. struct drm_i915_private *dev_priv = dev->dev_private;
  13679. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13680. u16 gmch_ctrl;
  13681. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13682. DRM_ERROR("failed to read control word\n");
  13683. return -EIO;
  13684. }
  13685. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13686. return 0;
  13687. if (state)
  13688. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13689. else
  13690. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13691. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13692. DRM_ERROR("failed to write control word\n");
  13693. return -EIO;
  13694. }
  13695. return 0;
  13696. }
  13697. struct intel_display_error_state {
  13698. u32 power_well_driver;
  13699. int num_transcoders;
  13700. struct intel_cursor_error_state {
  13701. u32 control;
  13702. u32 position;
  13703. u32 base;
  13704. u32 size;
  13705. } cursor[I915_MAX_PIPES];
  13706. struct intel_pipe_error_state {
  13707. bool power_domain_on;
  13708. u32 source;
  13709. u32 stat;
  13710. } pipe[I915_MAX_PIPES];
  13711. struct intel_plane_error_state {
  13712. u32 control;
  13713. u32 stride;
  13714. u32 size;
  13715. u32 pos;
  13716. u32 addr;
  13717. u32 surface;
  13718. u32 tile_offset;
  13719. } plane[I915_MAX_PIPES];
  13720. struct intel_transcoder_error_state {
  13721. bool power_domain_on;
  13722. enum transcoder cpu_transcoder;
  13723. u32 conf;
  13724. u32 htotal;
  13725. u32 hblank;
  13726. u32 hsync;
  13727. u32 vtotal;
  13728. u32 vblank;
  13729. u32 vsync;
  13730. } transcoder[4];
  13731. };
  13732. struct intel_display_error_state *
  13733. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13734. {
  13735. struct intel_display_error_state *error;
  13736. int transcoders[] = {
  13737. TRANSCODER_A,
  13738. TRANSCODER_B,
  13739. TRANSCODER_C,
  13740. TRANSCODER_EDP,
  13741. };
  13742. int i;
  13743. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13744. return NULL;
  13745. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13746. if (error == NULL)
  13747. return NULL;
  13748. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13749. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13750. for_each_pipe(dev_priv, i) {
  13751. error->pipe[i].power_domain_on =
  13752. __intel_display_power_is_enabled(dev_priv,
  13753. POWER_DOMAIN_PIPE(i));
  13754. if (!error->pipe[i].power_domain_on)
  13755. continue;
  13756. error->cursor[i].control = I915_READ(CURCNTR(i));
  13757. error->cursor[i].position = I915_READ(CURPOS(i));
  13758. error->cursor[i].base = I915_READ(CURBASE(i));
  13759. error->plane[i].control = I915_READ(DSPCNTR(i));
  13760. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13761. if (INTEL_GEN(dev_priv) <= 3) {
  13762. error->plane[i].size = I915_READ(DSPSIZE(i));
  13763. error->plane[i].pos = I915_READ(DSPPOS(i));
  13764. }
  13765. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13766. error->plane[i].addr = I915_READ(DSPADDR(i));
  13767. if (INTEL_GEN(dev_priv) >= 4) {
  13768. error->plane[i].surface = I915_READ(DSPSURF(i));
  13769. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13770. }
  13771. error->pipe[i].source = I915_READ(PIPESRC(i));
  13772. if (HAS_GMCH_DISPLAY(dev_priv))
  13773. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13774. }
  13775. /* Note: this does not include DSI transcoders. */
  13776. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13777. if (HAS_DDI(dev_priv))
  13778. error->num_transcoders++; /* Account for eDP. */
  13779. for (i = 0; i < error->num_transcoders; i++) {
  13780. enum transcoder cpu_transcoder = transcoders[i];
  13781. error->transcoder[i].power_domain_on =
  13782. __intel_display_power_is_enabled(dev_priv,
  13783. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13784. if (!error->transcoder[i].power_domain_on)
  13785. continue;
  13786. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13787. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13788. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13789. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13790. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13791. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13792. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13793. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13794. }
  13795. return error;
  13796. }
  13797. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13798. void
  13799. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13800. struct drm_device *dev,
  13801. struct intel_display_error_state *error)
  13802. {
  13803. struct drm_i915_private *dev_priv = dev->dev_private;
  13804. int i;
  13805. if (!error)
  13806. return;
  13807. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13808. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13809. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13810. error->power_well_driver);
  13811. for_each_pipe(dev_priv, i) {
  13812. err_printf(m, "Pipe [%d]:\n", i);
  13813. err_printf(m, " Power: %s\n",
  13814. onoff(error->pipe[i].power_domain_on));
  13815. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13816. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13817. err_printf(m, "Plane [%d]:\n", i);
  13818. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13819. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13820. if (INTEL_INFO(dev)->gen <= 3) {
  13821. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13822. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13823. }
  13824. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13825. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13826. if (INTEL_INFO(dev)->gen >= 4) {
  13827. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13828. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13829. }
  13830. err_printf(m, "Cursor [%d]:\n", i);
  13831. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13832. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13833. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13834. }
  13835. for (i = 0; i < error->num_transcoders; i++) {
  13836. err_printf(m, "CPU transcoder: %s\n",
  13837. transcoder_name(error->transcoder[i].cpu_transcoder));
  13838. err_printf(m, " Power: %s\n",
  13839. onoff(error->transcoder[i].power_domain_on));
  13840. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13841. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13842. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13843. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13844. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13845. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13846. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13847. }
  13848. }