intel_display.c 485 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  111. struct intel_crtc *crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_BROXTON(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  916. {
  917. i915_reg_t reg = PIPEDSL(pipe);
  918. u32 line1, line2;
  919. u32 line_mask;
  920. if (IS_GEN2(dev_priv))
  921. line_mask = DSL_LINEMASK_GEN2;
  922. else
  923. line_mask = DSL_LINEMASK_GEN3;
  924. line1 = I915_READ(reg) & line_mask;
  925. msleep(5);
  926. line2 = I915_READ(reg) & line_mask;
  927. return line1 == line2;
  928. }
  929. /*
  930. * intel_wait_for_pipe_off - wait for pipe to turn off
  931. * @crtc: crtc whose pipe to wait for
  932. *
  933. * After disabling a pipe, we can't wait for vblank in the usual way,
  934. * spinning on the vblank interrupt status bit, since we won't actually
  935. * see an interrupt when the pipe is disabled.
  936. *
  937. * On Gen4 and above:
  938. * wait for the pipe register state bit to turn off
  939. *
  940. * Otherwise:
  941. * wait for the display line value to settle (it usually
  942. * ends up stopping at the start of the next frame).
  943. *
  944. */
  945. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  946. {
  947. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  948. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  949. enum pipe pipe = crtc->pipe;
  950. if (INTEL_GEN(dev_priv) >= 4) {
  951. i915_reg_t reg = PIPECONF(cpu_transcoder);
  952. /* Wait for the Pipe State to go off */
  953. if (intel_wait_for_register(dev_priv,
  954. reg, I965_PIPECONF_ACTIVE, 0,
  955. 100))
  956. WARN(1, "pipe_off wait timed out\n");
  957. } else {
  958. /* Wait for the display line to settle */
  959. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  960. WARN(1, "pipe_off wait timed out\n");
  961. }
  962. }
  963. /* Only for pre-ILK configs */
  964. void assert_pll(struct drm_i915_private *dev_priv,
  965. enum pipe pipe, bool state)
  966. {
  967. u32 val;
  968. bool cur_state;
  969. val = I915_READ(DPLL(pipe));
  970. cur_state = !!(val & DPLL_VCO_ENABLE);
  971. I915_STATE_WARN(cur_state != state,
  972. "PLL state assertion failure (expected %s, current %s)\n",
  973. onoff(state), onoff(cur_state));
  974. }
  975. /* XXX: the dsi pll is shared between MIPI DSI ports */
  976. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. mutex_lock(&dev_priv->sb_lock);
  981. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  982. mutex_unlock(&dev_priv->sb_lock);
  983. cur_state = val & DSI_PLL_VCO_EN;
  984. I915_STATE_WARN(cur_state != state,
  985. "DSI PLL state assertion failure (expected %s, current %s)\n",
  986. onoff(state), onoff(cur_state));
  987. }
  988. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  989. enum pipe pipe, bool state)
  990. {
  991. bool cur_state;
  992. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  993. pipe);
  994. if (HAS_DDI(dev_priv)) {
  995. /* DDI does not have a specific FDI_TX register */
  996. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  997. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  998. } else {
  999. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. I915_STATE_WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. onoff(state), onoff(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(FDI_RX_CTL(pipe));
  1014. cur_state = !!(val & FDI_RX_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX state assertion failure (expected %s, current %s)\n",
  1017. onoff(state), onoff(cur_state));
  1018. }
  1019. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1020. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1021. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1022. enum pipe pipe)
  1023. {
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (IS_GEN5(dev_priv))
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv))
  1030. return;
  1031. val = I915_READ(FDI_TX_CTL(pipe));
  1032. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1033. }
  1034. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe, bool state)
  1036. {
  1037. u32 val;
  1038. bool cur_state;
  1039. val = I915_READ(FDI_RX_CTL(pipe));
  1040. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1041. I915_STATE_WARN(cur_state != state,
  1042. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1043. onoff(state), onoff(cur_state));
  1044. }
  1045. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1046. {
  1047. i915_reg_t pp_reg;
  1048. u32 val;
  1049. enum pipe panel_pipe = PIPE_A;
  1050. bool locked = true;
  1051. if (WARN_ON(HAS_DDI(dev_priv)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev_priv)) {
  1054. u32 port_sel;
  1055. pp_reg = PP_CONTROL(0);
  1056. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1057. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1058. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1059. panel_pipe = PIPE_B;
  1060. /* XXX: else fix for eDP */
  1061. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL(0);
  1067. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1068. panel_pipe = PIPE_B;
  1069. }
  1070. val = I915_READ(pp_reg);
  1071. if (!(val & PANEL_POWER_ON) ||
  1072. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1073. locked = false;
  1074. I915_STATE_WARN(panel_pipe == pipe && locked,
  1075. "panel assertion failure, pipe %c regs locked\n",
  1076. pipe_name(pipe));
  1077. }
  1078. static void assert_cursor(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. bool cur_state;
  1082. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  1083. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1084. else
  1085. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1086. I915_STATE_WARN(cur_state != state,
  1087. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1088. pipe_name(pipe), onoff(state), onoff(cur_state));
  1089. }
  1090. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1091. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1092. void assert_pipe(struct drm_i915_private *dev_priv,
  1093. enum pipe pipe, bool state)
  1094. {
  1095. bool cur_state;
  1096. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1097. pipe);
  1098. enum intel_display_power_domain power_domain;
  1099. /* if we need the pipe quirk it must be always on */
  1100. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1101. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1102. state = true;
  1103. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1104. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1105. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1106. cur_state = !!(val & PIPECONF_ENABLE);
  1107. intel_display_power_put(dev_priv, power_domain);
  1108. } else {
  1109. cur_state = false;
  1110. }
  1111. I915_STATE_WARN(cur_state != state,
  1112. "pipe %c assertion failure (expected %s, current %s)\n",
  1113. pipe_name(pipe), onoff(state), onoff(cur_state));
  1114. }
  1115. static void assert_plane(struct drm_i915_private *dev_priv,
  1116. enum plane plane, bool state)
  1117. {
  1118. u32 val;
  1119. bool cur_state;
  1120. val = I915_READ(DSPCNTR(plane));
  1121. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1122. I915_STATE_WARN(cur_state != state,
  1123. "plane %c assertion failure (expected %s, current %s)\n",
  1124. plane_name(plane), onoff(state), onoff(cur_state));
  1125. }
  1126. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1127. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1128. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1129. enum pipe pipe)
  1130. {
  1131. int i;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_GEN(dev_priv) >= 4) {
  1134. u32 val = I915_READ(DSPCNTR(pipe));
  1135. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1136. "plane %c assertion failure, should be disabled but not\n",
  1137. plane_name(pipe));
  1138. return;
  1139. }
  1140. /* Need to check both planes against the pipe */
  1141. for_each_pipe(dev_priv, i) {
  1142. u32 val = I915_READ(DSPCNTR(i));
  1143. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1144. DISPPLANE_SEL_PIPE_SHIFT;
  1145. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1146. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1147. plane_name(i), pipe_name(pipe));
  1148. }
  1149. }
  1150. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1151. enum pipe pipe)
  1152. {
  1153. int sprite;
  1154. if (INTEL_GEN(dev_priv) >= 9) {
  1155. for_each_sprite(dev_priv, pipe, sprite) {
  1156. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1157. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1158. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1159. sprite, pipe_name(pipe));
  1160. }
  1161. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1162. for_each_sprite(dev_priv, pipe, sprite) {
  1163. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1164. I915_STATE_WARN(val & SP_ENABLE,
  1165. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1166. sprite_name(pipe, sprite), pipe_name(pipe));
  1167. }
  1168. } else if (INTEL_GEN(dev_priv) >= 7) {
  1169. u32 val = I915_READ(SPRCTL(pipe));
  1170. I915_STATE_WARN(val & SPRITE_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. plane_name(pipe), pipe_name(pipe));
  1173. } else if (INTEL_GEN(dev_priv) >= 5) {
  1174. u32 val = I915_READ(DVSCNTR(pipe));
  1175. I915_STATE_WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1181. {
  1182. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1183. drm_crtc_vblank_put(crtc);
  1184. }
  1185. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. bool enabled;
  1190. val = I915_READ(PCH_TRANSCONF(pipe));
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. I915_STATE_WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv)) {
  1202. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1203. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1204. return false;
  1205. } else if (IS_CHERRYVIEW(dev_priv)) {
  1206. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1207. return false;
  1208. } else {
  1209. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, u32 val)
  1216. {
  1217. if ((val & SDVO_ENABLE) == 0)
  1218. return false;
  1219. if (HAS_PCH_CPT(dev_priv)) {
  1220. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1221. return false;
  1222. } else if (IS_CHERRYVIEW(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1224. return false;
  1225. } else {
  1226. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 val)
  1233. {
  1234. if ((val & LVDS_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1238. return false;
  1239. } else {
  1240. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe, u32 val)
  1247. {
  1248. if ((val & ADPA_DAC_ENABLE) == 0)
  1249. return false;
  1250. if (HAS_PCH_CPT(dev_priv)) {
  1251. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1252. return false;
  1253. } else {
  1254. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1255. return false;
  1256. }
  1257. return true;
  1258. }
  1259. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe, i915_reg_t reg,
  1261. u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1267. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, i915_reg_t reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1278. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. u32 val;
  1286. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1289. val = I915_READ(PCH_ADPA);
  1290. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1291. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1292. pipe_name(pipe));
  1293. val = I915_READ(PCH_LVDS);
  1294. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1298. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1299. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1300. }
  1301. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1302. const struct intel_crtc_state *pipe_config)
  1303. {
  1304. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1305. enum pipe pipe = crtc->pipe;
  1306. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1307. POSTING_READ(DPLL(pipe));
  1308. udelay(150);
  1309. if (intel_wait_for_register(dev_priv,
  1310. DPLL(pipe),
  1311. DPLL_LOCK_VLV,
  1312. DPLL_LOCK_VLV,
  1313. 1))
  1314. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1315. }
  1316. static void vlv_enable_pll(struct intel_crtc *crtc,
  1317. const struct intel_crtc_state *pipe_config)
  1318. {
  1319. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1320. enum pipe pipe = crtc->pipe;
  1321. assert_pipe_disabled(dev_priv, pipe);
  1322. /* PLL is protected by panel, make sure we can write it */
  1323. assert_panel_unlocked(dev_priv, pipe);
  1324. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1325. _vlv_enable_pll(crtc, pipe_config);
  1326. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1327. POSTING_READ(DPLL_MD(pipe));
  1328. }
  1329. static void _chv_enable_pll(struct intel_crtc *crtc,
  1330. const struct intel_crtc_state *pipe_config)
  1331. {
  1332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1333. enum pipe pipe = crtc->pipe;
  1334. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1335. u32 tmp;
  1336. mutex_lock(&dev_priv->sb_lock);
  1337. /* Enable back the 10bit clock to display controller */
  1338. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1339. tmp |= DPIO_DCLKP_EN;
  1340. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1341. mutex_unlock(&dev_priv->sb_lock);
  1342. /*
  1343. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1344. */
  1345. udelay(1);
  1346. /* Enable PLL */
  1347. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1348. /* Check PLL is locked */
  1349. if (intel_wait_for_register(dev_priv,
  1350. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1351. 1))
  1352. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1358. enum pipe pipe = crtc->pipe;
  1359. assert_pipe_disabled(dev_priv, pipe);
  1360. /* PLL is protected by panel, make sure we can write it */
  1361. assert_panel_unlocked(dev_priv, pipe);
  1362. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1363. _chv_enable_pll(crtc, pipe_config);
  1364. if (pipe != PIPE_A) {
  1365. /*
  1366. * WaPixelRepeatModeFixForC0:chv
  1367. *
  1368. * DPLLCMD is AWOL. Use chicken bits to propagate
  1369. * the value from DPLLBMD to either pipe B or C.
  1370. */
  1371. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1372. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1373. I915_WRITE(CBR4_VLV, 0);
  1374. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1375. /*
  1376. * DPLLB VGA mode also seems to cause problems.
  1377. * We should always have it disabled.
  1378. */
  1379. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1380. } else {
  1381. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1382. POSTING_READ(DPLL_MD(pipe));
  1383. }
  1384. }
  1385. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1386. {
  1387. struct intel_crtc *crtc;
  1388. int count = 0;
  1389. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1390. count += crtc->base.state->active &&
  1391. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1392. }
  1393. return count;
  1394. }
  1395. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1396. {
  1397. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1398. i915_reg_t reg = DPLL(crtc->pipe);
  1399. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1400. assert_pipe_disabled(dev_priv, crtc->pipe);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /*
  1417. * Apparently we need to have VGA mode enabled prior to changing
  1418. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1419. * dividers, even though the register value does change.
  1420. */
  1421. I915_WRITE(reg, 0);
  1422. I915_WRITE(reg, dpll);
  1423. /* Wait for the clocks to stabilize. */
  1424. POSTING_READ(reg);
  1425. udelay(150);
  1426. if (INTEL_GEN(dev_priv) >= 4) {
  1427. I915_WRITE(DPLL_MD(crtc->pipe),
  1428. crtc->config->dpll_hw_state.dpll_md);
  1429. } else {
  1430. /* The pixel multiplier can only be updated once the
  1431. * DPLL is enabled and the clocks are stable.
  1432. *
  1433. * So write it again.
  1434. */
  1435. I915_WRITE(reg, dpll);
  1436. }
  1437. /* We do this three times for luck */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. I915_WRITE(reg, dpll);
  1445. POSTING_READ(reg);
  1446. udelay(150); /* wait for warmup */
  1447. }
  1448. /**
  1449. * i9xx_disable_pll - disable a PLL
  1450. * @dev_priv: i915 private structure
  1451. * @pipe: pipe PLL to disable
  1452. *
  1453. * Disable the PLL for @pipe, making sure the pipe is off first.
  1454. *
  1455. * Note! This is for pre-ILK only.
  1456. */
  1457. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1458. {
  1459. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1460. enum pipe pipe = crtc->pipe;
  1461. /* Disable DVO 2x clock on both PLLs if necessary */
  1462. if (IS_I830(dev_priv) &&
  1463. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1464. !intel_num_dvo_pipes(dev_priv)) {
  1465. I915_WRITE(DPLL(PIPE_B),
  1466. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1467. I915_WRITE(DPLL(PIPE_A),
  1468. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1469. }
  1470. /* Don't disable pipe or pipe PLLs if needed */
  1471. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1472. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1473. return;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1477. POSTING_READ(DPLL(pipe));
  1478. }
  1479. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1480. {
  1481. u32 val;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1485. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1486. if (pipe != PIPE_A)
  1487. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1488. I915_WRITE(DPLL(pipe), val);
  1489. POSTING_READ(DPLL(pipe));
  1490. }
  1491. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1492. {
  1493. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1494. u32 val;
  1495. /* Make sure the pipe isn't still relying on us */
  1496. assert_pipe_disabled(dev_priv, pipe);
  1497. val = DPLL_SSC_REF_CLK_CHV |
  1498. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1499. if (pipe != PIPE_A)
  1500. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1501. I915_WRITE(DPLL(pipe), val);
  1502. POSTING_READ(DPLL(pipe));
  1503. mutex_lock(&dev_priv->sb_lock);
  1504. /* Disable 10bit clock to display controller */
  1505. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1506. val &= ~DPIO_DCLKP_EN;
  1507. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1508. mutex_unlock(&dev_priv->sb_lock);
  1509. }
  1510. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1511. struct intel_digital_port *dport,
  1512. unsigned int expected_mask)
  1513. {
  1514. u32 port_mask;
  1515. i915_reg_t dpll_reg;
  1516. switch (dport->port) {
  1517. case PORT_B:
  1518. port_mask = DPLL_PORTB_READY_MASK;
  1519. dpll_reg = DPLL(0);
  1520. break;
  1521. case PORT_C:
  1522. port_mask = DPLL_PORTC_READY_MASK;
  1523. dpll_reg = DPLL(0);
  1524. expected_mask <<= 4;
  1525. break;
  1526. case PORT_D:
  1527. port_mask = DPLL_PORTD_READY_MASK;
  1528. dpll_reg = DPIO_PHY_STATUS;
  1529. break;
  1530. default:
  1531. BUG();
  1532. }
  1533. if (intel_wait_for_register(dev_priv,
  1534. dpll_reg, port_mask, expected_mask,
  1535. 1000))
  1536. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1538. }
  1539. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1540. enum pipe pipe)
  1541. {
  1542. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1543. pipe);
  1544. i915_reg_t reg;
  1545. uint32_t val, pipeconf_val;
  1546. /* Make sure PCH DPLL is enabled */
  1547. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1548. /* FDI must be feeding us bits for PCH ports */
  1549. assert_fdi_tx_enabled(dev_priv, pipe);
  1550. assert_fdi_rx_enabled(dev_priv, pipe);
  1551. if (HAS_PCH_CPT(dev_priv)) {
  1552. /* Workaround: Set the timing override bit before enabling the
  1553. * pch transcoder. */
  1554. reg = TRANS_CHICKEN2(pipe);
  1555. val = I915_READ(reg);
  1556. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1557. I915_WRITE(reg, val);
  1558. }
  1559. reg = PCH_TRANSCONF(pipe);
  1560. val = I915_READ(reg);
  1561. pipeconf_val = I915_READ(PIPECONF(pipe));
  1562. if (HAS_PCH_IBX(dev_priv)) {
  1563. /*
  1564. * Make the BPC in transcoder be consistent with
  1565. * that in pipeconf reg. For HDMI we must use 8bpc
  1566. * here for both 8bpc and 12bpc.
  1567. */
  1568. val &= ~PIPECONF_BPC_MASK;
  1569. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1570. val |= PIPECONF_8BPC;
  1571. else
  1572. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1573. }
  1574. val &= ~TRANS_INTERLACE_MASK;
  1575. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1576. if (HAS_PCH_IBX(dev_priv) &&
  1577. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1578. val |= TRANS_LEGACY_INTERLACED_ILK;
  1579. else
  1580. val |= TRANS_INTERLACED;
  1581. else
  1582. val |= TRANS_PROGRESSIVE;
  1583. I915_WRITE(reg, val | TRANS_ENABLE);
  1584. if (intel_wait_for_register(dev_priv,
  1585. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1586. 100))
  1587. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1588. }
  1589. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1590. enum transcoder cpu_transcoder)
  1591. {
  1592. u32 val, pipeconf_val;
  1593. /* FDI must be feeding us bits for PCH ports */
  1594. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1595. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1596. /* Workaround: set timing override bit. */
  1597. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1598. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1599. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1600. val = TRANS_ENABLE;
  1601. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1602. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1603. PIPECONF_INTERLACED_ILK)
  1604. val |= TRANS_INTERLACED;
  1605. else
  1606. val |= TRANS_PROGRESSIVE;
  1607. I915_WRITE(LPT_TRANSCONF, val);
  1608. if (intel_wait_for_register(dev_priv,
  1609. LPT_TRANSCONF,
  1610. TRANS_STATE_ENABLE,
  1611. TRANS_STATE_ENABLE,
  1612. 100))
  1613. DRM_ERROR("Failed to enable PCH transcoder\n");
  1614. }
  1615. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. i915_reg_t reg;
  1619. uint32_t val;
  1620. /* FDI relies on the transcoder */
  1621. assert_fdi_tx_disabled(dev_priv, pipe);
  1622. assert_fdi_rx_disabled(dev_priv, pipe);
  1623. /* Ports must be off as well */
  1624. assert_pch_ports_disabled(dev_priv, pipe);
  1625. reg = PCH_TRANSCONF(pipe);
  1626. val = I915_READ(reg);
  1627. val &= ~TRANS_ENABLE;
  1628. I915_WRITE(reg, val);
  1629. /* wait for PCH transcoder off, transcoder state */
  1630. if (intel_wait_for_register(dev_priv,
  1631. reg, TRANS_STATE_ENABLE, 0,
  1632. 50))
  1633. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1634. if (HAS_PCH_CPT(dev_priv)) {
  1635. /* Workaround: Clear the timing override chicken bit again. */
  1636. reg = TRANS_CHICKEN2(pipe);
  1637. val = I915_READ(reg);
  1638. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1639. I915_WRITE(reg, val);
  1640. }
  1641. }
  1642. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1643. {
  1644. u32 val;
  1645. val = I915_READ(LPT_TRANSCONF);
  1646. val &= ~TRANS_ENABLE;
  1647. I915_WRITE(LPT_TRANSCONF, val);
  1648. /* wait for PCH transcoder off, transcoder state */
  1649. if (intel_wait_for_register(dev_priv,
  1650. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1651. 50))
  1652. DRM_ERROR("Failed to disable PCH transcoder\n");
  1653. /* Workaround: clear timing override bit. */
  1654. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1655. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1656. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1657. }
  1658. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1659. {
  1660. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1661. WARN_ON(!crtc->config->has_pch_encoder);
  1662. if (HAS_PCH_LPT(dev_priv))
  1663. return TRANSCODER_A;
  1664. else
  1665. return (enum transcoder) crtc->pipe;
  1666. }
  1667. /**
  1668. * intel_enable_pipe - enable a pipe, asserting requirements
  1669. * @crtc: crtc responsible for the pipe
  1670. *
  1671. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1672. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1673. */
  1674. static void intel_enable_pipe(struct intel_crtc *crtc)
  1675. {
  1676. struct drm_device *dev = crtc->base.dev;
  1677. struct drm_i915_private *dev_priv = to_i915(dev);
  1678. enum pipe pipe = crtc->pipe;
  1679. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. /*
  1687. * A pipe without a PLL won't actually be able to drive bits from
  1688. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1689. * need the check.
  1690. */
  1691. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1692. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1693. assert_dsi_pll_enabled(dev_priv);
  1694. else
  1695. assert_pll_enabled(dev_priv, pipe);
  1696. } else {
  1697. if (crtc->config->has_pch_encoder) {
  1698. /* if driving the PCH, we need FDI enabled */
  1699. assert_fdi_rx_pll_enabled(dev_priv,
  1700. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1701. assert_fdi_tx_pll_enabled(dev_priv,
  1702. (enum pipe) cpu_transcoder);
  1703. }
  1704. /* FIXME: assert CPU port conditions for SNB+ */
  1705. }
  1706. reg = PIPECONF(cpu_transcoder);
  1707. val = I915_READ(reg);
  1708. if (val & PIPECONF_ENABLE) {
  1709. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1710. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1711. return;
  1712. }
  1713. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1714. POSTING_READ(reg);
  1715. /*
  1716. * Until the pipe starts DSL will read as 0, which would cause
  1717. * an apparent vblank timestamp jump, which messes up also the
  1718. * frame count when it's derived from the timestamps. So let's
  1719. * wait for the pipe to start properly before we call
  1720. * drm_crtc_vblank_on()
  1721. */
  1722. if (dev->max_vblank_count == 0 &&
  1723. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1724. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1725. }
  1726. /**
  1727. * intel_disable_pipe - disable a pipe, asserting requirements
  1728. * @crtc: crtc whose pipes is to be disabled
  1729. *
  1730. * Disable the pipe of @crtc, making sure that various hardware
  1731. * specific requirements are met, if applicable, e.g. plane
  1732. * disabled, panel fitter off, etc.
  1733. *
  1734. * Will wait until the pipe has shut down before returning.
  1735. */
  1736. static void intel_disable_pipe(struct intel_crtc *crtc)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1739. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1740. enum pipe pipe = crtc->pipe;
  1741. i915_reg_t reg;
  1742. u32 val;
  1743. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1744. /*
  1745. * Make sure planes won't keep trying to pump pixels to us,
  1746. * or we might hang the display.
  1747. */
  1748. assert_planes_disabled(dev_priv, pipe);
  1749. assert_cursor_disabled(dev_priv, pipe);
  1750. assert_sprites_disabled(dev_priv, pipe);
  1751. reg = PIPECONF(cpu_transcoder);
  1752. val = I915_READ(reg);
  1753. if ((val & PIPECONF_ENABLE) == 0)
  1754. return;
  1755. /*
  1756. * Double wide has implications for planes
  1757. * so best keep it disabled when not needed.
  1758. */
  1759. if (crtc->config->double_wide)
  1760. val &= ~PIPECONF_DOUBLE_WIDE;
  1761. /* Don't disable pipe or pipe PLLs if needed */
  1762. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1763. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1764. val &= ~PIPECONF_ENABLE;
  1765. I915_WRITE(reg, val);
  1766. if ((val & PIPECONF_ENABLE) == 0)
  1767. intel_wait_for_pipe_off(crtc);
  1768. }
  1769. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1770. {
  1771. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1772. }
  1773. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1774. uint64_t fb_modifier, unsigned int cpp)
  1775. {
  1776. switch (fb_modifier) {
  1777. case DRM_FORMAT_MOD_NONE:
  1778. return cpp;
  1779. case I915_FORMAT_MOD_X_TILED:
  1780. if (IS_GEN2(dev_priv))
  1781. return 128;
  1782. else
  1783. return 512;
  1784. case I915_FORMAT_MOD_Y_TILED:
  1785. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1786. return 128;
  1787. else
  1788. return 512;
  1789. case I915_FORMAT_MOD_Yf_TILED:
  1790. switch (cpp) {
  1791. case 1:
  1792. return 64;
  1793. case 2:
  1794. case 4:
  1795. return 128;
  1796. case 8:
  1797. case 16:
  1798. return 256;
  1799. default:
  1800. MISSING_CASE(cpp);
  1801. return cpp;
  1802. }
  1803. break;
  1804. default:
  1805. MISSING_CASE(fb_modifier);
  1806. return cpp;
  1807. }
  1808. }
  1809. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1810. uint64_t fb_modifier, unsigned int cpp)
  1811. {
  1812. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1813. return 1;
  1814. else
  1815. return intel_tile_size(dev_priv) /
  1816. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1817. }
  1818. /* Return the tile dimensions in pixel units */
  1819. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1820. unsigned int *tile_width,
  1821. unsigned int *tile_height,
  1822. uint64_t fb_modifier,
  1823. unsigned int cpp)
  1824. {
  1825. unsigned int tile_width_bytes =
  1826. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1827. *tile_width = tile_width_bytes / cpp;
  1828. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1829. }
  1830. unsigned int
  1831. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1832. uint32_t pixel_format, uint64_t fb_modifier)
  1833. {
  1834. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1835. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1836. return ALIGN(height, tile_height);
  1837. }
  1838. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1839. {
  1840. unsigned int size = 0;
  1841. int i;
  1842. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1843. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1844. return size;
  1845. }
  1846. static void
  1847. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1848. const struct drm_framebuffer *fb,
  1849. unsigned int rotation)
  1850. {
  1851. if (drm_rotation_90_or_270(rotation)) {
  1852. *view = i915_ggtt_view_rotated;
  1853. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1854. } else {
  1855. *view = i915_ggtt_view_normal;
  1856. }
  1857. }
  1858. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1859. {
  1860. if (INTEL_INFO(dev_priv)->gen >= 9)
  1861. return 256 * 1024;
  1862. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1863. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1864. return 128 * 1024;
  1865. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1866. return 4 * 1024;
  1867. else
  1868. return 0;
  1869. }
  1870. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1871. uint64_t fb_modifier)
  1872. {
  1873. switch (fb_modifier) {
  1874. case DRM_FORMAT_MOD_NONE:
  1875. return intel_linear_alignment(dev_priv);
  1876. case I915_FORMAT_MOD_X_TILED:
  1877. if (INTEL_INFO(dev_priv)->gen >= 9)
  1878. return 256 * 1024;
  1879. return 0;
  1880. case I915_FORMAT_MOD_Y_TILED:
  1881. case I915_FORMAT_MOD_Yf_TILED:
  1882. return 1 * 1024 * 1024;
  1883. default:
  1884. MISSING_CASE(fb_modifier);
  1885. return 0;
  1886. }
  1887. }
  1888. struct i915_vma *
  1889. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1890. {
  1891. struct drm_device *dev = fb->dev;
  1892. struct drm_i915_private *dev_priv = to_i915(dev);
  1893. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1894. struct i915_ggtt_view view;
  1895. struct i915_vma *vma;
  1896. u32 alignment;
  1897. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1898. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1899. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1900. /* Note that the w/a also requires 64 PTE of padding following the
  1901. * bo. We currently fill all unused PTE with the shadow page and so
  1902. * we should always have valid PTE following the scanout preventing
  1903. * the VT-d warning.
  1904. */
  1905. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1906. alignment = 256 * 1024;
  1907. /*
  1908. * Global gtt pte registers are special registers which actually forward
  1909. * writes to a chunk of system memory. Which means that there is no risk
  1910. * that the register values disappear as soon as we call
  1911. * intel_runtime_pm_put(), so it is correct to wrap only the
  1912. * pin/unpin/fence and not more.
  1913. */
  1914. intel_runtime_pm_get(dev_priv);
  1915. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1916. if (IS_ERR(vma))
  1917. goto err;
  1918. if (i915_vma_is_map_and_fenceable(vma)) {
  1919. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1920. * fence, whereas 965+ only requires a fence if using
  1921. * framebuffer compression. For simplicity, we always, when
  1922. * possible, install a fence as the cost is not that onerous.
  1923. *
  1924. * If we fail to fence the tiled scanout, then either the
  1925. * modeset will reject the change (which is highly unlikely as
  1926. * the affected systems, all but one, do not have unmappable
  1927. * space) or we will not be able to enable full powersaving
  1928. * techniques (also likely not to apply due to various limits
  1929. * FBC and the like impose on the size of the buffer, which
  1930. * presumably we violated anyway with this unmappable buffer).
  1931. * Anyway, it is presumably better to stumble onwards with
  1932. * something and try to run the system in a "less than optimal"
  1933. * mode that matches the user configuration.
  1934. */
  1935. if (i915_vma_get_fence(vma) == 0)
  1936. i915_vma_pin_fence(vma);
  1937. }
  1938. err:
  1939. intel_runtime_pm_put(dev_priv);
  1940. return vma;
  1941. }
  1942. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1943. {
  1944. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1945. struct i915_ggtt_view view;
  1946. struct i915_vma *vma;
  1947. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1948. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1949. vma = i915_gem_object_to_ggtt(obj, &view);
  1950. i915_vma_unpin_fence(vma);
  1951. i915_gem_object_unpin_from_display_plane(vma);
  1952. }
  1953. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1954. unsigned int rotation)
  1955. {
  1956. if (drm_rotation_90_or_270(rotation))
  1957. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1958. else
  1959. return fb->pitches[plane];
  1960. }
  1961. /*
  1962. * Convert the x/y offsets into a linear offset.
  1963. * Only valid with 0/180 degree rotation, which is fine since linear
  1964. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1965. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1966. */
  1967. u32 intel_fb_xy_to_linear(int x, int y,
  1968. const struct intel_plane_state *state,
  1969. int plane)
  1970. {
  1971. const struct drm_framebuffer *fb = state->base.fb;
  1972. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1973. unsigned int pitch = fb->pitches[plane];
  1974. return y * pitch + x * cpp;
  1975. }
  1976. /*
  1977. * Add the x/y offsets derived from fb->offsets[] to the user
  1978. * specified plane src x/y offsets. The resulting x/y offsets
  1979. * specify the start of scanout from the beginning of the gtt mapping.
  1980. */
  1981. void intel_add_fb_offsets(int *x, int *y,
  1982. const struct intel_plane_state *state,
  1983. int plane)
  1984. {
  1985. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1986. unsigned int rotation = state->base.rotation;
  1987. if (drm_rotation_90_or_270(rotation)) {
  1988. *x += intel_fb->rotated[plane].x;
  1989. *y += intel_fb->rotated[plane].y;
  1990. } else {
  1991. *x += intel_fb->normal[plane].x;
  1992. *y += intel_fb->normal[plane].y;
  1993. }
  1994. }
  1995. /*
  1996. * Input tile dimensions and pitch must already be
  1997. * rotated to match x and y, and in pixel units.
  1998. */
  1999. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2000. unsigned int tile_width,
  2001. unsigned int tile_height,
  2002. unsigned int tile_size,
  2003. unsigned int pitch_tiles,
  2004. u32 old_offset,
  2005. u32 new_offset)
  2006. {
  2007. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2008. unsigned int tiles;
  2009. WARN_ON(old_offset & (tile_size - 1));
  2010. WARN_ON(new_offset & (tile_size - 1));
  2011. WARN_ON(new_offset > old_offset);
  2012. tiles = (old_offset - new_offset) / tile_size;
  2013. *y += tiles / pitch_tiles * tile_height;
  2014. *x += tiles % pitch_tiles * tile_width;
  2015. /* minimize x in case it got needlessly big */
  2016. *y += *x / pitch_pixels * tile_height;
  2017. *x %= pitch_pixels;
  2018. return new_offset;
  2019. }
  2020. /*
  2021. * Adjust the tile offset by moving the difference into
  2022. * the x/y offsets.
  2023. */
  2024. static u32 intel_adjust_tile_offset(int *x, int *y,
  2025. const struct intel_plane_state *state, int plane,
  2026. u32 old_offset, u32 new_offset)
  2027. {
  2028. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2029. const struct drm_framebuffer *fb = state->base.fb;
  2030. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2031. unsigned int rotation = state->base.rotation;
  2032. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2033. WARN_ON(new_offset > old_offset);
  2034. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2035. unsigned int tile_size, tile_width, tile_height;
  2036. unsigned int pitch_tiles;
  2037. tile_size = intel_tile_size(dev_priv);
  2038. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2039. fb->modifier, cpp);
  2040. if (drm_rotation_90_or_270(rotation)) {
  2041. pitch_tiles = pitch / tile_height;
  2042. swap(tile_width, tile_height);
  2043. } else {
  2044. pitch_tiles = pitch / (tile_width * cpp);
  2045. }
  2046. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2047. tile_size, pitch_tiles,
  2048. old_offset, new_offset);
  2049. } else {
  2050. old_offset += *y * pitch + *x * cpp;
  2051. *y = (old_offset - new_offset) / pitch;
  2052. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2053. }
  2054. return new_offset;
  2055. }
  2056. /*
  2057. * Computes the linear offset to the base tile and adjusts
  2058. * x, y. bytes per pixel is assumed to be a power-of-two.
  2059. *
  2060. * In the 90/270 rotated case, x and y are assumed
  2061. * to be already rotated to match the rotated GTT view, and
  2062. * pitch is the tile_height aligned framebuffer height.
  2063. *
  2064. * This function is used when computing the derived information
  2065. * under intel_framebuffer, so using any of that information
  2066. * here is not allowed. Anything under drm_framebuffer can be
  2067. * used. This is why the user has to pass in the pitch since it
  2068. * is specified in the rotated orientation.
  2069. */
  2070. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2071. int *x, int *y,
  2072. const struct drm_framebuffer *fb, int plane,
  2073. unsigned int pitch,
  2074. unsigned int rotation,
  2075. u32 alignment)
  2076. {
  2077. uint64_t fb_modifier = fb->modifier;
  2078. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2079. u32 offset, offset_aligned;
  2080. if (alignment)
  2081. alignment--;
  2082. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2083. unsigned int tile_size, tile_width, tile_height;
  2084. unsigned int tile_rows, tiles, pitch_tiles;
  2085. tile_size = intel_tile_size(dev_priv);
  2086. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2087. fb_modifier, cpp);
  2088. if (drm_rotation_90_or_270(rotation)) {
  2089. pitch_tiles = pitch / tile_height;
  2090. swap(tile_width, tile_height);
  2091. } else {
  2092. pitch_tiles = pitch / (tile_width * cpp);
  2093. }
  2094. tile_rows = *y / tile_height;
  2095. *y %= tile_height;
  2096. tiles = *x / tile_width;
  2097. *x %= tile_width;
  2098. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2099. offset_aligned = offset & ~alignment;
  2100. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2101. tile_size, pitch_tiles,
  2102. offset, offset_aligned);
  2103. } else {
  2104. offset = *y * pitch + *x * cpp;
  2105. offset_aligned = offset & ~alignment;
  2106. *y = (offset & alignment) / pitch;
  2107. *x = ((offset & alignment) - *y * pitch) / cpp;
  2108. }
  2109. return offset_aligned;
  2110. }
  2111. u32 intel_compute_tile_offset(int *x, int *y,
  2112. const struct intel_plane_state *state,
  2113. int plane)
  2114. {
  2115. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2116. const struct drm_framebuffer *fb = state->base.fb;
  2117. unsigned int rotation = state->base.rotation;
  2118. int pitch = intel_fb_pitch(fb, plane, rotation);
  2119. u32 alignment;
  2120. /* AUX_DIST needs only 4K alignment */
  2121. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2122. alignment = 4096;
  2123. else
  2124. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2125. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2126. rotation, alignment);
  2127. }
  2128. /* Convert the fb->offset[] linear offset into x/y offsets */
  2129. static void intel_fb_offset_to_xy(int *x, int *y,
  2130. const struct drm_framebuffer *fb, int plane)
  2131. {
  2132. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2133. unsigned int pitch = fb->pitches[plane];
  2134. u32 linear_offset = fb->offsets[plane];
  2135. *y = linear_offset / pitch;
  2136. *x = linear_offset % pitch / cpp;
  2137. }
  2138. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2139. {
  2140. switch (fb_modifier) {
  2141. case I915_FORMAT_MOD_X_TILED:
  2142. return I915_TILING_X;
  2143. case I915_FORMAT_MOD_Y_TILED:
  2144. return I915_TILING_Y;
  2145. default:
  2146. return I915_TILING_NONE;
  2147. }
  2148. }
  2149. static int
  2150. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2151. struct drm_framebuffer *fb)
  2152. {
  2153. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2154. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2155. u32 gtt_offset_rotated = 0;
  2156. unsigned int max_size = 0;
  2157. uint32_t format = fb->pixel_format;
  2158. int i, num_planes = drm_format_num_planes(format);
  2159. unsigned int tile_size = intel_tile_size(dev_priv);
  2160. for (i = 0; i < num_planes; i++) {
  2161. unsigned int width, height;
  2162. unsigned int cpp, size;
  2163. u32 offset;
  2164. int x, y;
  2165. cpp = drm_format_plane_cpp(format, i);
  2166. width = drm_format_plane_width(fb->width, format, i);
  2167. height = drm_format_plane_height(fb->height, format, i);
  2168. intel_fb_offset_to_xy(&x, &y, fb, i);
  2169. /*
  2170. * The fence (if used) is aligned to the start of the object
  2171. * so having the framebuffer wrap around across the edge of the
  2172. * fenced region doesn't really work. We have no API to configure
  2173. * the fence start offset within the object (nor could we probably
  2174. * on gen2/3). So it's just easier if we just require that the
  2175. * fb layout agrees with the fence layout. We already check that the
  2176. * fb stride matches the fence stride elsewhere.
  2177. */
  2178. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2179. (x + width) * cpp > fb->pitches[i]) {
  2180. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2181. i, fb->offsets[i]);
  2182. return -EINVAL;
  2183. }
  2184. /*
  2185. * First pixel of the framebuffer from
  2186. * the start of the normal gtt mapping.
  2187. */
  2188. intel_fb->normal[i].x = x;
  2189. intel_fb->normal[i].y = y;
  2190. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2191. fb, 0, fb->pitches[i],
  2192. DRM_ROTATE_0, tile_size);
  2193. offset /= tile_size;
  2194. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2195. unsigned int tile_width, tile_height;
  2196. unsigned int pitch_tiles;
  2197. struct drm_rect r;
  2198. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2199. fb->modifier, cpp);
  2200. rot_info->plane[i].offset = offset;
  2201. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2202. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2203. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2204. intel_fb->rotated[i].pitch =
  2205. rot_info->plane[i].height * tile_height;
  2206. /* how many tiles does this plane need */
  2207. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2208. /*
  2209. * If the plane isn't horizontally tile aligned,
  2210. * we need one more tile.
  2211. */
  2212. if (x != 0)
  2213. size++;
  2214. /* rotate the x/y offsets to match the GTT view */
  2215. r.x1 = x;
  2216. r.y1 = y;
  2217. r.x2 = x + width;
  2218. r.y2 = y + height;
  2219. drm_rect_rotate(&r,
  2220. rot_info->plane[i].width * tile_width,
  2221. rot_info->plane[i].height * tile_height,
  2222. DRM_ROTATE_270);
  2223. x = r.x1;
  2224. y = r.y1;
  2225. /* rotate the tile dimensions to match the GTT view */
  2226. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2227. swap(tile_width, tile_height);
  2228. /*
  2229. * We only keep the x/y offsets, so push all of the
  2230. * gtt offset into the x/y offsets.
  2231. */
  2232. _intel_adjust_tile_offset(&x, &y, tile_size,
  2233. tile_width, tile_height, pitch_tiles,
  2234. gtt_offset_rotated * tile_size, 0);
  2235. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2236. /*
  2237. * First pixel of the framebuffer from
  2238. * the start of the rotated gtt mapping.
  2239. */
  2240. intel_fb->rotated[i].x = x;
  2241. intel_fb->rotated[i].y = y;
  2242. } else {
  2243. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2244. x * cpp, tile_size);
  2245. }
  2246. /* how many tiles in total needed in the bo */
  2247. max_size = max(max_size, offset + size);
  2248. }
  2249. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2250. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2251. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2252. return -EINVAL;
  2253. }
  2254. return 0;
  2255. }
  2256. static int i9xx_format_to_fourcc(int format)
  2257. {
  2258. switch (format) {
  2259. case DISPPLANE_8BPP:
  2260. return DRM_FORMAT_C8;
  2261. case DISPPLANE_BGRX555:
  2262. return DRM_FORMAT_XRGB1555;
  2263. case DISPPLANE_BGRX565:
  2264. return DRM_FORMAT_RGB565;
  2265. default:
  2266. case DISPPLANE_BGRX888:
  2267. return DRM_FORMAT_XRGB8888;
  2268. case DISPPLANE_RGBX888:
  2269. return DRM_FORMAT_XBGR8888;
  2270. case DISPPLANE_BGRX101010:
  2271. return DRM_FORMAT_XRGB2101010;
  2272. case DISPPLANE_RGBX101010:
  2273. return DRM_FORMAT_XBGR2101010;
  2274. }
  2275. }
  2276. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2277. {
  2278. switch (format) {
  2279. case PLANE_CTL_FORMAT_RGB_565:
  2280. return DRM_FORMAT_RGB565;
  2281. default:
  2282. case PLANE_CTL_FORMAT_XRGB_8888:
  2283. if (rgb_order) {
  2284. if (alpha)
  2285. return DRM_FORMAT_ABGR8888;
  2286. else
  2287. return DRM_FORMAT_XBGR8888;
  2288. } else {
  2289. if (alpha)
  2290. return DRM_FORMAT_ARGB8888;
  2291. else
  2292. return DRM_FORMAT_XRGB8888;
  2293. }
  2294. case PLANE_CTL_FORMAT_XRGB_2101010:
  2295. if (rgb_order)
  2296. return DRM_FORMAT_XBGR2101010;
  2297. else
  2298. return DRM_FORMAT_XRGB2101010;
  2299. }
  2300. }
  2301. static bool
  2302. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2303. struct intel_initial_plane_config *plane_config)
  2304. {
  2305. struct drm_device *dev = crtc->base.dev;
  2306. struct drm_i915_private *dev_priv = to_i915(dev);
  2307. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2308. struct drm_i915_gem_object *obj = NULL;
  2309. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2310. struct drm_framebuffer *fb = &plane_config->fb->base;
  2311. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2312. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2313. PAGE_SIZE);
  2314. size_aligned -= base_aligned;
  2315. if (plane_config->size == 0)
  2316. return false;
  2317. /* If the FB is too big, just don't use it since fbdev is not very
  2318. * important and we should probably use that space with FBC or other
  2319. * features. */
  2320. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2321. return false;
  2322. mutex_lock(&dev->struct_mutex);
  2323. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2324. base_aligned,
  2325. base_aligned,
  2326. size_aligned);
  2327. if (!obj) {
  2328. mutex_unlock(&dev->struct_mutex);
  2329. return false;
  2330. }
  2331. if (plane_config->tiling == I915_TILING_X)
  2332. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2333. mode_cmd.pixel_format = fb->pixel_format;
  2334. mode_cmd.width = fb->width;
  2335. mode_cmd.height = fb->height;
  2336. mode_cmd.pitches[0] = fb->pitches[0];
  2337. mode_cmd.modifier[0] = fb->modifier;
  2338. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2339. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2340. &mode_cmd, obj)) {
  2341. DRM_DEBUG_KMS("intel fb init failed\n");
  2342. goto out_unref_obj;
  2343. }
  2344. mutex_unlock(&dev->struct_mutex);
  2345. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2346. return true;
  2347. out_unref_obj:
  2348. i915_gem_object_put(obj);
  2349. mutex_unlock(&dev->struct_mutex);
  2350. return false;
  2351. }
  2352. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2353. static void
  2354. update_state_fb(struct drm_plane *plane)
  2355. {
  2356. if (plane->fb == plane->state->fb)
  2357. return;
  2358. if (plane->state->fb)
  2359. drm_framebuffer_unreference(plane->state->fb);
  2360. plane->state->fb = plane->fb;
  2361. if (plane->state->fb)
  2362. drm_framebuffer_reference(plane->state->fb);
  2363. }
  2364. static void
  2365. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2366. struct intel_initial_plane_config *plane_config)
  2367. {
  2368. struct drm_device *dev = intel_crtc->base.dev;
  2369. struct drm_i915_private *dev_priv = to_i915(dev);
  2370. struct drm_crtc *c;
  2371. struct intel_crtc *i;
  2372. struct drm_i915_gem_object *obj;
  2373. struct drm_plane *primary = intel_crtc->base.primary;
  2374. struct drm_plane_state *plane_state = primary->state;
  2375. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2376. struct intel_plane *intel_plane = to_intel_plane(primary);
  2377. struct intel_plane_state *intel_state =
  2378. to_intel_plane_state(plane_state);
  2379. struct drm_framebuffer *fb;
  2380. if (!plane_config->fb)
  2381. return;
  2382. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2383. fb = &plane_config->fb->base;
  2384. goto valid_fb;
  2385. }
  2386. kfree(plane_config->fb);
  2387. /*
  2388. * Failed to alloc the obj, check to see if we should share
  2389. * an fb with another CRTC instead
  2390. */
  2391. for_each_crtc(dev, c) {
  2392. i = to_intel_crtc(c);
  2393. if (c == &intel_crtc->base)
  2394. continue;
  2395. if (!i->active)
  2396. continue;
  2397. fb = c->primary->fb;
  2398. if (!fb)
  2399. continue;
  2400. obj = intel_fb_obj(fb);
  2401. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2402. drm_framebuffer_reference(fb);
  2403. goto valid_fb;
  2404. }
  2405. }
  2406. /*
  2407. * We've failed to reconstruct the BIOS FB. Current display state
  2408. * indicates that the primary plane is visible, but has a NULL FB,
  2409. * which will lead to problems later if we don't fix it up. The
  2410. * simplest solution is to just disable the primary plane now and
  2411. * pretend the BIOS never had it enabled.
  2412. */
  2413. to_intel_plane_state(plane_state)->base.visible = false;
  2414. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2415. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2416. intel_plane->disable_plane(primary, &intel_crtc->base);
  2417. return;
  2418. valid_fb:
  2419. plane_state->src_x = 0;
  2420. plane_state->src_y = 0;
  2421. plane_state->src_w = fb->width << 16;
  2422. plane_state->src_h = fb->height << 16;
  2423. plane_state->crtc_x = 0;
  2424. plane_state->crtc_y = 0;
  2425. plane_state->crtc_w = fb->width;
  2426. plane_state->crtc_h = fb->height;
  2427. intel_state->base.src = drm_plane_state_src(plane_state);
  2428. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2429. obj = intel_fb_obj(fb);
  2430. if (i915_gem_object_is_tiled(obj))
  2431. dev_priv->preserve_bios_swizzle = true;
  2432. drm_framebuffer_reference(fb);
  2433. primary->fb = primary->state->fb = fb;
  2434. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2435. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2436. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2437. &obj->frontbuffer_bits);
  2438. }
  2439. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2440. unsigned int rotation)
  2441. {
  2442. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2443. switch (fb->modifier) {
  2444. case DRM_FORMAT_MOD_NONE:
  2445. case I915_FORMAT_MOD_X_TILED:
  2446. switch (cpp) {
  2447. case 8:
  2448. return 4096;
  2449. case 4:
  2450. case 2:
  2451. case 1:
  2452. return 8192;
  2453. default:
  2454. MISSING_CASE(cpp);
  2455. break;
  2456. }
  2457. break;
  2458. case I915_FORMAT_MOD_Y_TILED:
  2459. case I915_FORMAT_MOD_Yf_TILED:
  2460. switch (cpp) {
  2461. case 8:
  2462. return 2048;
  2463. case 4:
  2464. return 4096;
  2465. case 2:
  2466. case 1:
  2467. return 8192;
  2468. default:
  2469. MISSING_CASE(cpp);
  2470. break;
  2471. }
  2472. break;
  2473. default:
  2474. MISSING_CASE(fb->modifier);
  2475. }
  2476. return 2048;
  2477. }
  2478. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2479. {
  2480. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2481. const struct drm_framebuffer *fb = plane_state->base.fb;
  2482. unsigned int rotation = plane_state->base.rotation;
  2483. int x = plane_state->base.src.x1 >> 16;
  2484. int y = plane_state->base.src.y1 >> 16;
  2485. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2486. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2487. int max_width = skl_max_plane_width(fb, 0, rotation);
  2488. int max_height = 4096;
  2489. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2490. if (w > max_width || h > max_height) {
  2491. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2492. w, h, max_width, max_height);
  2493. return -EINVAL;
  2494. }
  2495. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2496. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2497. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2498. /*
  2499. * AUX surface offset is specified as the distance from the
  2500. * main surface offset, and it must be non-negative. Make
  2501. * sure that is what we will get.
  2502. */
  2503. if (offset > aux_offset)
  2504. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2505. offset, aux_offset & ~(alignment - 1));
  2506. /*
  2507. * When using an X-tiled surface, the plane blows up
  2508. * if the x offset + width exceed the stride.
  2509. *
  2510. * TODO: linear and Y-tiled seem fine, Yf untested,
  2511. */
  2512. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2513. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2514. while ((x + w) * cpp > fb->pitches[0]) {
  2515. if (offset == 0) {
  2516. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2517. return -EINVAL;
  2518. }
  2519. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2520. offset, offset - alignment);
  2521. }
  2522. }
  2523. plane_state->main.offset = offset;
  2524. plane_state->main.x = x;
  2525. plane_state->main.y = y;
  2526. return 0;
  2527. }
  2528. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2529. {
  2530. const struct drm_framebuffer *fb = plane_state->base.fb;
  2531. unsigned int rotation = plane_state->base.rotation;
  2532. int max_width = skl_max_plane_width(fb, 1, rotation);
  2533. int max_height = 4096;
  2534. int x = plane_state->base.src.x1 >> 17;
  2535. int y = plane_state->base.src.y1 >> 17;
  2536. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2537. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2538. u32 offset;
  2539. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2540. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2541. /* FIXME not quite sure how/if these apply to the chroma plane */
  2542. if (w > max_width || h > max_height) {
  2543. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2544. w, h, max_width, max_height);
  2545. return -EINVAL;
  2546. }
  2547. plane_state->aux.offset = offset;
  2548. plane_state->aux.x = x;
  2549. plane_state->aux.y = y;
  2550. return 0;
  2551. }
  2552. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2553. {
  2554. const struct drm_framebuffer *fb = plane_state->base.fb;
  2555. unsigned int rotation = plane_state->base.rotation;
  2556. int ret;
  2557. /* Rotate src coordinates to match rotated GTT view */
  2558. if (drm_rotation_90_or_270(rotation))
  2559. drm_rect_rotate(&plane_state->base.src,
  2560. fb->width << 16, fb->height << 16,
  2561. DRM_ROTATE_270);
  2562. /*
  2563. * Handle the AUX surface first since
  2564. * the main surface setup depends on it.
  2565. */
  2566. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2567. ret = skl_check_nv12_aux_surface(plane_state);
  2568. if (ret)
  2569. return ret;
  2570. } else {
  2571. plane_state->aux.offset = ~0xfff;
  2572. plane_state->aux.x = 0;
  2573. plane_state->aux.y = 0;
  2574. }
  2575. ret = skl_check_main_surface(plane_state);
  2576. if (ret)
  2577. return ret;
  2578. return 0;
  2579. }
  2580. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2581. const struct intel_crtc_state *crtc_state,
  2582. const struct intel_plane_state *plane_state)
  2583. {
  2584. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2586. struct drm_framebuffer *fb = plane_state->base.fb;
  2587. int plane = intel_crtc->plane;
  2588. u32 linear_offset;
  2589. u32 dspcntr;
  2590. i915_reg_t reg = DSPCNTR(plane);
  2591. unsigned int rotation = plane_state->base.rotation;
  2592. int x = plane_state->base.src.x1 >> 16;
  2593. int y = plane_state->base.src.y1 >> 16;
  2594. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2595. dspcntr |= DISPLAY_PLANE_ENABLE;
  2596. if (INTEL_GEN(dev_priv) < 4) {
  2597. if (intel_crtc->pipe == PIPE_B)
  2598. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2599. /* pipesrc and dspsize control the size that is scaled from,
  2600. * which should always be the user's requested size.
  2601. */
  2602. I915_WRITE(DSPSIZE(plane),
  2603. ((crtc_state->pipe_src_h - 1) << 16) |
  2604. (crtc_state->pipe_src_w - 1));
  2605. I915_WRITE(DSPPOS(plane), 0);
  2606. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2607. I915_WRITE(PRIMSIZE(plane),
  2608. ((crtc_state->pipe_src_h - 1) << 16) |
  2609. (crtc_state->pipe_src_w - 1));
  2610. I915_WRITE(PRIMPOS(plane), 0);
  2611. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2612. }
  2613. switch (fb->pixel_format) {
  2614. case DRM_FORMAT_C8:
  2615. dspcntr |= DISPPLANE_8BPP;
  2616. break;
  2617. case DRM_FORMAT_XRGB1555:
  2618. dspcntr |= DISPPLANE_BGRX555;
  2619. break;
  2620. case DRM_FORMAT_RGB565:
  2621. dspcntr |= DISPPLANE_BGRX565;
  2622. break;
  2623. case DRM_FORMAT_XRGB8888:
  2624. dspcntr |= DISPPLANE_BGRX888;
  2625. break;
  2626. case DRM_FORMAT_XBGR8888:
  2627. dspcntr |= DISPPLANE_RGBX888;
  2628. break;
  2629. case DRM_FORMAT_XRGB2101010:
  2630. dspcntr |= DISPPLANE_BGRX101010;
  2631. break;
  2632. case DRM_FORMAT_XBGR2101010:
  2633. dspcntr |= DISPPLANE_RGBX101010;
  2634. break;
  2635. default:
  2636. BUG();
  2637. }
  2638. if (INTEL_GEN(dev_priv) >= 4 &&
  2639. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2640. dspcntr |= DISPPLANE_TILED;
  2641. if (rotation & DRM_ROTATE_180)
  2642. dspcntr |= DISPPLANE_ROTATE_180;
  2643. if (rotation & DRM_REFLECT_X)
  2644. dspcntr |= DISPPLANE_MIRROR;
  2645. if (IS_G4X(dev_priv))
  2646. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2647. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2648. if (INTEL_GEN(dev_priv) >= 4)
  2649. intel_crtc->dspaddr_offset =
  2650. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2651. if (rotation & DRM_ROTATE_180) {
  2652. x += crtc_state->pipe_src_w - 1;
  2653. y += crtc_state->pipe_src_h - 1;
  2654. } else if (rotation & DRM_REFLECT_X) {
  2655. x += crtc_state->pipe_src_w - 1;
  2656. }
  2657. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2658. if (INTEL_GEN(dev_priv) < 4)
  2659. intel_crtc->dspaddr_offset = linear_offset;
  2660. intel_crtc->adjusted_x = x;
  2661. intel_crtc->adjusted_y = y;
  2662. I915_WRITE(reg, dspcntr);
  2663. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2664. if (INTEL_GEN(dev_priv) >= 4) {
  2665. I915_WRITE(DSPSURF(plane),
  2666. intel_fb_gtt_offset(fb, rotation) +
  2667. intel_crtc->dspaddr_offset);
  2668. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2669. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2670. } else {
  2671. I915_WRITE(DSPADDR(plane),
  2672. intel_fb_gtt_offset(fb, rotation) +
  2673. intel_crtc->dspaddr_offset);
  2674. }
  2675. POSTING_READ(reg);
  2676. }
  2677. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2678. struct drm_crtc *crtc)
  2679. {
  2680. struct drm_device *dev = crtc->dev;
  2681. struct drm_i915_private *dev_priv = to_i915(dev);
  2682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2683. int plane = intel_crtc->plane;
  2684. I915_WRITE(DSPCNTR(plane), 0);
  2685. if (INTEL_INFO(dev_priv)->gen >= 4)
  2686. I915_WRITE(DSPSURF(plane), 0);
  2687. else
  2688. I915_WRITE(DSPADDR(plane), 0);
  2689. POSTING_READ(DSPCNTR(plane));
  2690. }
  2691. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2692. const struct intel_crtc_state *crtc_state,
  2693. const struct intel_plane_state *plane_state)
  2694. {
  2695. struct drm_device *dev = primary->dev;
  2696. struct drm_i915_private *dev_priv = to_i915(dev);
  2697. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2698. struct drm_framebuffer *fb = plane_state->base.fb;
  2699. int plane = intel_crtc->plane;
  2700. u32 linear_offset;
  2701. u32 dspcntr;
  2702. i915_reg_t reg = DSPCNTR(plane);
  2703. unsigned int rotation = plane_state->base.rotation;
  2704. int x = plane_state->base.src.x1 >> 16;
  2705. int y = plane_state->base.src.y1 >> 16;
  2706. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2707. dspcntr |= DISPLAY_PLANE_ENABLE;
  2708. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2709. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2710. switch (fb->pixel_format) {
  2711. case DRM_FORMAT_C8:
  2712. dspcntr |= DISPPLANE_8BPP;
  2713. break;
  2714. case DRM_FORMAT_RGB565:
  2715. dspcntr |= DISPPLANE_BGRX565;
  2716. break;
  2717. case DRM_FORMAT_XRGB8888:
  2718. dspcntr |= DISPPLANE_BGRX888;
  2719. break;
  2720. case DRM_FORMAT_XBGR8888:
  2721. dspcntr |= DISPPLANE_RGBX888;
  2722. break;
  2723. case DRM_FORMAT_XRGB2101010:
  2724. dspcntr |= DISPPLANE_BGRX101010;
  2725. break;
  2726. case DRM_FORMAT_XBGR2101010:
  2727. dspcntr |= DISPPLANE_RGBX101010;
  2728. break;
  2729. default:
  2730. BUG();
  2731. }
  2732. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2733. dspcntr |= DISPPLANE_TILED;
  2734. if (rotation & DRM_ROTATE_180)
  2735. dspcntr |= DISPPLANE_ROTATE_180;
  2736. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2737. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2738. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2739. intel_crtc->dspaddr_offset =
  2740. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2741. /* HSW+ does this automagically in hardware */
  2742. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2743. rotation & DRM_ROTATE_180) {
  2744. x += crtc_state->pipe_src_w - 1;
  2745. y += crtc_state->pipe_src_h - 1;
  2746. }
  2747. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2748. intel_crtc->adjusted_x = x;
  2749. intel_crtc->adjusted_y = y;
  2750. I915_WRITE(reg, dspcntr);
  2751. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2752. I915_WRITE(DSPSURF(plane),
  2753. intel_fb_gtt_offset(fb, rotation) +
  2754. intel_crtc->dspaddr_offset);
  2755. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2756. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2757. } else {
  2758. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2759. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2760. }
  2761. POSTING_READ(reg);
  2762. }
  2763. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2764. uint64_t fb_modifier, uint32_t pixel_format)
  2765. {
  2766. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2767. return 64;
  2768. } else {
  2769. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2770. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2771. }
  2772. }
  2773. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2774. unsigned int rotation)
  2775. {
  2776. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2777. struct i915_ggtt_view view;
  2778. struct i915_vma *vma;
  2779. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2780. vma = i915_gem_object_to_ggtt(obj, &view);
  2781. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2782. view.type))
  2783. return -1;
  2784. return i915_ggtt_offset(vma);
  2785. }
  2786. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2787. {
  2788. struct drm_device *dev = intel_crtc->base.dev;
  2789. struct drm_i915_private *dev_priv = to_i915(dev);
  2790. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2791. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2792. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2793. }
  2794. /*
  2795. * This function detaches (aka. unbinds) unused scalers in hardware
  2796. */
  2797. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2798. {
  2799. struct intel_crtc_scaler_state *scaler_state;
  2800. int i;
  2801. scaler_state = &intel_crtc->config->scaler_state;
  2802. /* loop through and disable scalers that aren't in use */
  2803. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2804. if (!scaler_state->scalers[i].in_use)
  2805. skl_detach_scaler(intel_crtc, i);
  2806. }
  2807. }
  2808. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2809. unsigned int rotation)
  2810. {
  2811. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2812. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2813. /*
  2814. * The stride is either expressed as a multiple of 64 bytes chunks for
  2815. * linear buffers or in number of tiles for tiled buffers.
  2816. */
  2817. if (drm_rotation_90_or_270(rotation)) {
  2818. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2819. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2820. } else {
  2821. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2822. fb->pixel_format);
  2823. }
  2824. return stride;
  2825. }
  2826. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2827. {
  2828. switch (pixel_format) {
  2829. case DRM_FORMAT_C8:
  2830. return PLANE_CTL_FORMAT_INDEXED;
  2831. case DRM_FORMAT_RGB565:
  2832. return PLANE_CTL_FORMAT_RGB_565;
  2833. case DRM_FORMAT_XBGR8888:
  2834. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2835. case DRM_FORMAT_XRGB8888:
  2836. return PLANE_CTL_FORMAT_XRGB_8888;
  2837. /*
  2838. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2839. * to be already pre-multiplied. We need to add a knob (or a different
  2840. * DRM_FORMAT) for user-space to configure that.
  2841. */
  2842. case DRM_FORMAT_ABGR8888:
  2843. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2844. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2845. case DRM_FORMAT_ARGB8888:
  2846. return PLANE_CTL_FORMAT_XRGB_8888 |
  2847. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2848. case DRM_FORMAT_XRGB2101010:
  2849. return PLANE_CTL_FORMAT_XRGB_2101010;
  2850. case DRM_FORMAT_XBGR2101010:
  2851. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2852. case DRM_FORMAT_YUYV:
  2853. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2854. case DRM_FORMAT_YVYU:
  2855. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2856. case DRM_FORMAT_UYVY:
  2857. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2858. case DRM_FORMAT_VYUY:
  2859. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2860. default:
  2861. MISSING_CASE(pixel_format);
  2862. }
  2863. return 0;
  2864. }
  2865. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2866. {
  2867. switch (fb_modifier) {
  2868. case DRM_FORMAT_MOD_NONE:
  2869. break;
  2870. case I915_FORMAT_MOD_X_TILED:
  2871. return PLANE_CTL_TILED_X;
  2872. case I915_FORMAT_MOD_Y_TILED:
  2873. return PLANE_CTL_TILED_Y;
  2874. case I915_FORMAT_MOD_Yf_TILED:
  2875. return PLANE_CTL_TILED_YF;
  2876. default:
  2877. MISSING_CASE(fb_modifier);
  2878. }
  2879. return 0;
  2880. }
  2881. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2882. {
  2883. switch (rotation) {
  2884. case DRM_ROTATE_0:
  2885. break;
  2886. /*
  2887. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2888. * while i915 HW rotation is clockwise, thats why this swapping.
  2889. */
  2890. case DRM_ROTATE_90:
  2891. return PLANE_CTL_ROTATE_270;
  2892. case DRM_ROTATE_180:
  2893. return PLANE_CTL_ROTATE_180;
  2894. case DRM_ROTATE_270:
  2895. return PLANE_CTL_ROTATE_90;
  2896. default:
  2897. MISSING_CASE(rotation);
  2898. }
  2899. return 0;
  2900. }
  2901. static void skylake_update_primary_plane(struct drm_plane *plane,
  2902. const struct intel_crtc_state *crtc_state,
  2903. const struct intel_plane_state *plane_state)
  2904. {
  2905. struct drm_device *dev = plane->dev;
  2906. struct drm_i915_private *dev_priv = to_i915(dev);
  2907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2908. struct drm_framebuffer *fb = plane_state->base.fb;
  2909. int pipe = intel_crtc->pipe;
  2910. u32 plane_ctl;
  2911. unsigned int rotation = plane_state->base.rotation;
  2912. u32 stride = skl_plane_stride(fb, 0, rotation);
  2913. u32 surf_addr = plane_state->main.offset;
  2914. int scaler_id = plane_state->scaler_id;
  2915. int src_x = plane_state->main.x;
  2916. int src_y = plane_state->main.y;
  2917. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2918. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2919. int dst_x = plane_state->base.dst.x1;
  2920. int dst_y = plane_state->base.dst.y1;
  2921. int dst_w = drm_rect_width(&plane_state->base.dst);
  2922. int dst_h = drm_rect_height(&plane_state->base.dst);
  2923. plane_ctl = PLANE_CTL_ENABLE |
  2924. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2925. PLANE_CTL_PIPE_CSC_ENABLE;
  2926. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2927. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2928. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2929. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2930. /* Sizes are 0 based */
  2931. src_w--;
  2932. src_h--;
  2933. dst_w--;
  2934. dst_h--;
  2935. intel_crtc->dspaddr_offset = surf_addr;
  2936. intel_crtc->adjusted_x = src_x;
  2937. intel_crtc->adjusted_y = src_y;
  2938. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2939. I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
  2940. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2941. I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
  2942. if (scaler_id >= 0) {
  2943. uint32_t ps_ctrl = 0;
  2944. WARN_ON(!dst_w || !dst_h);
  2945. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2946. crtc_state->scaler_state.scalers[scaler_id].mode;
  2947. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2948. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2949. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2950. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2951. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2952. } else {
  2953. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2954. }
  2955. I915_WRITE(PLANE_SURF(pipe, 0),
  2956. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2957. POSTING_READ(PLANE_SURF(pipe, 0));
  2958. }
  2959. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2960. struct drm_crtc *crtc)
  2961. {
  2962. struct drm_device *dev = crtc->dev;
  2963. struct drm_i915_private *dev_priv = to_i915(dev);
  2964. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2965. int pipe = intel_crtc->pipe;
  2966. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2967. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2968. POSTING_READ(PLANE_SURF(pipe, 0));
  2969. }
  2970. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2971. static int
  2972. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2973. int x, int y, enum mode_set_atomic state)
  2974. {
  2975. /* Support for kgdboc is disabled, this needs a major rework. */
  2976. DRM_ERROR("legacy panic handler not supported any more.\n");
  2977. return -ENODEV;
  2978. }
  2979. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2980. {
  2981. struct intel_crtc *crtc;
  2982. for_each_intel_crtc(&dev_priv->drm, crtc)
  2983. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2984. }
  2985. static void intel_update_primary_planes(struct drm_device *dev)
  2986. {
  2987. struct drm_crtc *crtc;
  2988. for_each_crtc(dev, crtc) {
  2989. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2990. struct intel_plane_state *plane_state =
  2991. to_intel_plane_state(plane->base.state);
  2992. if (plane_state->base.visible)
  2993. plane->update_plane(&plane->base,
  2994. to_intel_crtc_state(crtc->state),
  2995. plane_state);
  2996. }
  2997. }
  2998. static int
  2999. __intel_display_resume(struct drm_device *dev,
  3000. struct drm_atomic_state *state)
  3001. {
  3002. struct drm_crtc_state *crtc_state;
  3003. struct drm_crtc *crtc;
  3004. int i, ret;
  3005. intel_modeset_setup_hw_state(dev);
  3006. i915_redisable_vga(to_i915(dev));
  3007. if (!state)
  3008. return 0;
  3009. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3010. /*
  3011. * Force recalculation even if we restore
  3012. * current state. With fast modeset this may not result
  3013. * in a modeset when the state is compatible.
  3014. */
  3015. crtc_state->mode_changed = true;
  3016. }
  3017. /* ignore any reset values/BIOS leftovers in the WM registers */
  3018. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3019. ret = drm_atomic_commit(state);
  3020. WARN_ON(ret == -EDEADLK);
  3021. return ret;
  3022. }
  3023. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3024. {
  3025. return intel_has_gpu_reset(dev_priv) &&
  3026. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3027. }
  3028. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3029. {
  3030. struct drm_device *dev = &dev_priv->drm;
  3031. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3032. struct drm_atomic_state *state;
  3033. int ret;
  3034. /*
  3035. * Need mode_config.mutex so that we don't
  3036. * trample ongoing ->detect() and whatnot.
  3037. */
  3038. mutex_lock(&dev->mode_config.mutex);
  3039. drm_modeset_acquire_init(ctx, 0);
  3040. while (1) {
  3041. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3042. if (ret != -EDEADLK)
  3043. break;
  3044. drm_modeset_backoff(ctx);
  3045. }
  3046. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3047. if (!i915.force_reset_modeset_test &&
  3048. !gpu_reset_clobbers_display(dev_priv))
  3049. return;
  3050. /*
  3051. * Disabling the crtcs gracefully seems nicer. Also the
  3052. * g33 docs say we should at least disable all the planes.
  3053. */
  3054. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3055. if (IS_ERR(state)) {
  3056. ret = PTR_ERR(state);
  3057. state = NULL;
  3058. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3059. goto err;
  3060. }
  3061. ret = drm_atomic_helper_disable_all(dev, ctx);
  3062. if (ret) {
  3063. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3064. goto err;
  3065. }
  3066. dev_priv->modeset_restore_state = state;
  3067. state->acquire_ctx = ctx;
  3068. return;
  3069. err:
  3070. drm_atomic_state_put(state);
  3071. }
  3072. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3073. {
  3074. struct drm_device *dev = &dev_priv->drm;
  3075. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3076. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3077. int ret;
  3078. /*
  3079. * Flips in the rings will be nuked by the reset,
  3080. * so complete all pending flips so that user space
  3081. * will get its events and not get stuck.
  3082. */
  3083. intel_complete_page_flips(dev_priv);
  3084. dev_priv->modeset_restore_state = NULL;
  3085. /* reset doesn't touch the display */
  3086. if (!gpu_reset_clobbers_display(dev_priv)) {
  3087. if (!state) {
  3088. /*
  3089. * Flips in the rings have been nuked by the reset,
  3090. * so update the base address of all primary
  3091. * planes to the the last fb to make sure we're
  3092. * showing the correct fb after a reset.
  3093. *
  3094. * FIXME: Atomic will make this obsolete since we won't schedule
  3095. * CS-based flips (which might get lost in gpu resets) any more.
  3096. */
  3097. intel_update_primary_planes(dev);
  3098. } else {
  3099. ret = __intel_display_resume(dev, state);
  3100. if (ret)
  3101. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3102. }
  3103. } else {
  3104. /*
  3105. * The display has been reset as well,
  3106. * so need a full re-initialization.
  3107. */
  3108. intel_runtime_pm_disable_interrupts(dev_priv);
  3109. intel_runtime_pm_enable_interrupts(dev_priv);
  3110. intel_pps_unlock_regs_wa(dev_priv);
  3111. intel_modeset_init_hw(dev);
  3112. spin_lock_irq(&dev_priv->irq_lock);
  3113. if (dev_priv->display.hpd_irq_setup)
  3114. dev_priv->display.hpd_irq_setup(dev_priv);
  3115. spin_unlock_irq(&dev_priv->irq_lock);
  3116. ret = __intel_display_resume(dev, state);
  3117. if (ret)
  3118. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3119. intel_hpd_init(dev_priv);
  3120. }
  3121. if (state)
  3122. drm_atomic_state_put(state);
  3123. drm_modeset_drop_locks(ctx);
  3124. drm_modeset_acquire_fini(ctx);
  3125. mutex_unlock(&dev->mode_config.mutex);
  3126. }
  3127. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3128. {
  3129. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3130. if (i915_reset_in_progress(error))
  3131. return true;
  3132. if (crtc->reset_count != i915_reset_count(error))
  3133. return true;
  3134. return false;
  3135. }
  3136. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3137. {
  3138. struct drm_device *dev = crtc->dev;
  3139. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3140. bool pending;
  3141. if (abort_flip_on_reset(intel_crtc))
  3142. return false;
  3143. spin_lock_irq(&dev->event_lock);
  3144. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3145. spin_unlock_irq(&dev->event_lock);
  3146. return pending;
  3147. }
  3148. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3149. struct intel_crtc_state *old_crtc_state)
  3150. {
  3151. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3152. struct intel_crtc_state *pipe_config =
  3153. to_intel_crtc_state(crtc->base.state);
  3154. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3155. crtc->base.mode = crtc->base.state->mode;
  3156. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3157. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3158. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3159. /*
  3160. * Update pipe size and adjust fitter if needed: the reason for this is
  3161. * that in compute_mode_changes we check the native mode (not the pfit
  3162. * mode) to see if we can flip rather than do a full mode set. In the
  3163. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3164. * pfit state, we'll end up with a big fb scanned out into the wrong
  3165. * sized surface.
  3166. */
  3167. I915_WRITE(PIPESRC(crtc->pipe),
  3168. ((pipe_config->pipe_src_w - 1) << 16) |
  3169. (pipe_config->pipe_src_h - 1));
  3170. /* on skylake this is done by detaching scalers */
  3171. if (INTEL_GEN(dev_priv) >= 9) {
  3172. skl_detach_scalers(crtc);
  3173. if (pipe_config->pch_pfit.enabled)
  3174. skylake_pfit_enable(crtc);
  3175. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3176. if (pipe_config->pch_pfit.enabled)
  3177. ironlake_pfit_enable(crtc);
  3178. else if (old_crtc_state->pch_pfit.enabled)
  3179. ironlake_pfit_disable(crtc, true);
  3180. }
  3181. }
  3182. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3183. {
  3184. struct drm_device *dev = crtc->dev;
  3185. struct drm_i915_private *dev_priv = to_i915(dev);
  3186. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3187. int pipe = intel_crtc->pipe;
  3188. i915_reg_t reg;
  3189. u32 temp;
  3190. /* enable normal train */
  3191. reg = FDI_TX_CTL(pipe);
  3192. temp = I915_READ(reg);
  3193. if (IS_IVYBRIDGE(dev_priv)) {
  3194. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3195. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3196. } else {
  3197. temp &= ~FDI_LINK_TRAIN_NONE;
  3198. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3199. }
  3200. I915_WRITE(reg, temp);
  3201. reg = FDI_RX_CTL(pipe);
  3202. temp = I915_READ(reg);
  3203. if (HAS_PCH_CPT(dev_priv)) {
  3204. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3205. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3206. } else {
  3207. temp &= ~FDI_LINK_TRAIN_NONE;
  3208. temp |= FDI_LINK_TRAIN_NONE;
  3209. }
  3210. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3211. /* wait one idle pattern time */
  3212. POSTING_READ(reg);
  3213. udelay(1000);
  3214. /* IVB wants error correction enabled */
  3215. if (IS_IVYBRIDGE(dev_priv))
  3216. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3217. FDI_FE_ERRC_ENABLE);
  3218. }
  3219. /* The FDI link training functions for ILK/Ibexpeak. */
  3220. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3221. {
  3222. struct drm_device *dev = crtc->dev;
  3223. struct drm_i915_private *dev_priv = to_i915(dev);
  3224. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3225. int pipe = intel_crtc->pipe;
  3226. i915_reg_t reg;
  3227. u32 temp, tries;
  3228. /* FDI needs bits from pipe first */
  3229. assert_pipe_enabled(dev_priv, pipe);
  3230. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3231. for train result */
  3232. reg = FDI_RX_IMR(pipe);
  3233. temp = I915_READ(reg);
  3234. temp &= ~FDI_RX_SYMBOL_LOCK;
  3235. temp &= ~FDI_RX_BIT_LOCK;
  3236. I915_WRITE(reg, temp);
  3237. I915_READ(reg);
  3238. udelay(150);
  3239. /* enable CPU FDI TX and PCH FDI RX */
  3240. reg = FDI_TX_CTL(pipe);
  3241. temp = I915_READ(reg);
  3242. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3243. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3244. temp &= ~FDI_LINK_TRAIN_NONE;
  3245. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3246. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3247. reg = FDI_RX_CTL(pipe);
  3248. temp = I915_READ(reg);
  3249. temp &= ~FDI_LINK_TRAIN_NONE;
  3250. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3251. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3252. POSTING_READ(reg);
  3253. udelay(150);
  3254. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3255. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3256. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3257. FDI_RX_PHASE_SYNC_POINTER_EN);
  3258. reg = FDI_RX_IIR(pipe);
  3259. for (tries = 0; tries < 5; tries++) {
  3260. temp = I915_READ(reg);
  3261. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3262. if ((temp & FDI_RX_BIT_LOCK)) {
  3263. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3264. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3265. break;
  3266. }
  3267. }
  3268. if (tries == 5)
  3269. DRM_ERROR("FDI train 1 fail!\n");
  3270. /* Train 2 */
  3271. reg = FDI_TX_CTL(pipe);
  3272. temp = I915_READ(reg);
  3273. temp &= ~FDI_LINK_TRAIN_NONE;
  3274. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3275. I915_WRITE(reg, temp);
  3276. reg = FDI_RX_CTL(pipe);
  3277. temp = I915_READ(reg);
  3278. temp &= ~FDI_LINK_TRAIN_NONE;
  3279. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3280. I915_WRITE(reg, temp);
  3281. POSTING_READ(reg);
  3282. udelay(150);
  3283. reg = FDI_RX_IIR(pipe);
  3284. for (tries = 0; tries < 5; tries++) {
  3285. temp = I915_READ(reg);
  3286. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3287. if (temp & FDI_RX_SYMBOL_LOCK) {
  3288. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3289. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3290. break;
  3291. }
  3292. }
  3293. if (tries == 5)
  3294. DRM_ERROR("FDI train 2 fail!\n");
  3295. DRM_DEBUG_KMS("FDI train done\n");
  3296. }
  3297. static const int snb_b_fdi_train_param[] = {
  3298. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3299. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3300. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3301. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3302. };
  3303. /* The FDI link training functions for SNB/Cougarpoint. */
  3304. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3305. {
  3306. struct drm_device *dev = crtc->dev;
  3307. struct drm_i915_private *dev_priv = to_i915(dev);
  3308. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3309. int pipe = intel_crtc->pipe;
  3310. i915_reg_t reg;
  3311. u32 temp, i, retry;
  3312. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3313. for train result */
  3314. reg = FDI_RX_IMR(pipe);
  3315. temp = I915_READ(reg);
  3316. temp &= ~FDI_RX_SYMBOL_LOCK;
  3317. temp &= ~FDI_RX_BIT_LOCK;
  3318. I915_WRITE(reg, temp);
  3319. POSTING_READ(reg);
  3320. udelay(150);
  3321. /* enable CPU FDI TX and PCH FDI RX */
  3322. reg = FDI_TX_CTL(pipe);
  3323. temp = I915_READ(reg);
  3324. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3325. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3326. temp &= ~FDI_LINK_TRAIN_NONE;
  3327. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3328. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3329. /* SNB-B */
  3330. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3331. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3332. I915_WRITE(FDI_RX_MISC(pipe),
  3333. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3334. reg = FDI_RX_CTL(pipe);
  3335. temp = I915_READ(reg);
  3336. if (HAS_PCH_CPT(dev_priv)) {
  3337. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3338. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3339. } else {
  3340. temp &= ~FDI_LINK_TRAIN_NONE;
  3341. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3342. }
  3343. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3344. POSTING_READ(reg);
  3345. udelay(150);
  3346. for (i = 0; i < 4; i++) {
  3347. reg = FDI_TX_CTL(pipe);
  3348. temp = I915_READ(reg);
  3349. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3350. temp |= snb_b_fdi_train_param[i];
  3351. I915_WRITE(reg, temp);
  3352. POSTING_READ(reg);
  3353. udelay(500);
  3354. for (retry = 0; retry < 5; retry++) {
  3355. reg = FDI_RX_IIR(pipe);
  3356. temp = I915_READ(reg);
  3357. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3358. if (temp & FDI_RX_BIT_LOCK) {
  3359. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3360. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3361. break;
  3362. }
  3363. udelay(50);
  3364. }
  3365. if (retry < 5)
  3366. break;
  3367. }
  3368. if (i == 4)
  3369. DRM_ERROR("FDI train 1 fail!\n");
  3370. /* Train 2 */
  3371. reg = FDI_TX_CTL(pipe);
  3372. temp = I915_READ(reg);
  3373. temp &= ~FDI_LINK_TRAIN_NONE;
  3374. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3375. if (IS_GEN6(dev_priv)) {
  3376. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3377. /* SNB-B */
  3378. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3379. }
  3380. I915_WRITE(reg, temp);
  3381. reg = FDI_RX_CTL(pipe);
  3382. temp = I915_READ(reg);
  3383. if (HAS_PCH_CPT(dev_priv)) {
  3384. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3385. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3386. } else {
  3387. temp &= ~FDI_LINK_TRAIN_NONE;
  3388. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3389. }
  3390. I915_WRITE(reg, temp);
  3391. POSTING_READ(reg);
  3392. udelay(150);
  3393. for (i = 0; i < 4; i++) {
  3394. reg = FDI_TX_CTL(pipe);
  3395. temp = I915_READ(reg);
  3396. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3397. temp |= snb_b_fdi_train_param[i];
  3398. I915_WRITE(reg, temp);
  3399. POSTING_READ(reg);
  3400. udelay(500);
  3401. for (retry = 0; retry < 5; retry++) {
  3402. reg = FDI_RX_IIR(pipe);
  3403. temp = I915_READ(reg);
  3404. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3405. if (temp & FDI_RX_SYMBOL_LOCK) {
  3406. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3407. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3408. break;
  3409. }
  3410. udelay(50);
  3411. }
  3412. if (retry < 5)
  3413. break;
  3414. }
  3415. if (i == 4)
  3416. DRM_ERROR("FDI train 2 fail!\n");
  3417. DRM_DEBUG_KMS("FDI train done.\n");
  3418. }
  3419. /* Manual link training for Ivy Bridge A0 parts */
  3420. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3421. {
  3422. struct drm_device *dev = crtc->dev;
  3423. struct drm_i915_private *dev_priv = to_i915(dev);
  3424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3425. int pipe = intel_crtc->pipe;
  3426. i915_reg_t reg;
  3427. u32 temp, i, j;
  3428. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3429. for train result */
  3430. reg = FDI_RX_IMR(pipe);
  3431. temp = I915_READ(reg);
  3432. temp &= ~FDI_RX_SYMBOL_LOCK;
  3433. temp &= ~FDI_RX_BIT_LOCK;
  3434. I915_WRITE(reg, temp);
  3435. POSTING_READ(reg);
  3436. udelay(150);
  3437. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3438. I915_READ(FDI_RX_IIR(pipe)));
  3439. /* Try each vswing and preemphasis setting twice before moving on */
  3440. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3441. /* disable first in case we need to retry */
  3442. reg = FDI_TX_CTL(pipe);
  3443. temp = I915_READ(reg);
  3444. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3445. temp &= ~FDI_TX_ENABLE;
  3446. I915_WRITE(reg, temp);
  3447. reg = FDI_RX_CTL(pipe);
  3448. temp = I915_READ(reg);
  3449. temp &= ~FDI_LINK_TRAIN_AUTO;
  3450. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3451. temp &= ~FDI_RX_ENABLE;
  3452. I915_WRITE(reg, temp);
  3453. /* enable CPU FDI TX and PCH FDI RX */
  3454. reg = FDI_TX_CTL(pipe);
  3455. temp = I915_READ(reg);
  3456. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3457. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3458. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3459. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3460. temp |= snb_b_fdi_train_param[j/2];
  3461. temp |= FDI_COMPOSITE_SYNC;
  3462. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3463. I915_WRITE(FDI_RX_MISC(pipe),
  3464. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3465. reg = FDI_RX_CTL(pipe);
  3466. temp = I915_READ(reg);
  3467. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3468. temp |= FDI_COMPOSITE_SYNC;
  3469. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3470. POSTING_READ(reg);
  3471. udelay(1); /* should be 0.5us */
  3472. for (i = 0; i < 4; i++) {
  3473. reg = FDI_RX_IIR(pipe);
  3474. temp = I915_READ(reg);
  3475. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3476. if (temp & FDI_RX_BIT_LOCK ||
  3477. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3478. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3479. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3480. i);
  3481. break;
  3482. }
  3483. udelay(1); /* should be 0.5us */
  3484. }
  3485. if (i == 4) {
  3486. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3487. continue;
  3488. }
  3489. /* Train 2 */
  3490. reg = FDI_TX_CTL(pipe);
  3491. temp = I915_READ(reg);
  3492. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3493. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3494. I915_WRITE(reg, temp);
  3495. reg = FDI_RX_CTL(pipe);
  3496. temp = I915_READ(reg);
  3497. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3498. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3499. I915_WRITE(reg, temp);
  3500. POSTING_READ(reg);
  3501. udelay(2); /* should be 1.5us */
  3502. for (i = 0; i < 4; i++) {
  3503. reg = FDI_RX_IIR(pipe);
  3504. temp = I915_READ(reg);
  3505. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3506. if (temp & FDI_RX_SYMBOL_LOCK ||
  3507. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3508. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3509. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3510. i);
  3511. goto train_done;
  3512. }
  3513. udelay(2); /* should be 1.5us */
  3514. }
  3515. if (i == 4)
  3516. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3517. }
  3518. train_done:
  3519. DRM_DEBUG_KMS("FDI train done.\n");
  3520. }
  3521. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3522. {
  3523. struct drm_device *dev = intel_crtc->base.dev;
  3524. struct drm_i915_private *dev_priv = to_i915(dev);
  3525. int pipe = intel_crtc->pipe;
  3526. i915_reg_t reg;
  3527. u32 temp;
  3528. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3529. reg = FDI_RX_CTL(pipe);
  3530. temp = I915_READ(reg);
  3531. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3532. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3533. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3534. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3535. POSTING_READ(reg);
  3536. udelay(200);
  3537. /* Switch from Rawclk to PCDclk */
  3538. temp = I915_READ(reg);
  3539. I915_WRITE(reg, temp | FDI_PCDCLK);
  3540. POSTING_READ(reg);
  3541. udelay(200);
  3542. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3543. reg = FDI_TX_CTL(pipe);
  3544. temp = I915_READ(reg);
  3545. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3546. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3547. POSTING_READ(reg);
  3548. udelay(100);
  3549. }
  3550. }
  3551. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3552. {
  3553. struct drm_device *dev = intel_crtc->base.dev;
  3554. struct drm_i915_private *dev_priv = to_i915(dev);
  3555. int pipe = intel_crtc->pipe;
  3556. i915_reg_t reg;
  3557. u32 temp;
  3558. /* Switch from PCDclk to Rawclk */
  3559. reg = FDI_RX_CTL(pipe);
  3560. temp = I915_READ(reg);
  3561. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3562. /* Disable CPU FDI TX PLL */
  3563. reg = FDI_TX_CTL(pipe);
  3564. temp = I915_READ(reg);
  3565. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3566. POSTING_READ(reg);
  3567. udelay(100);
  3568. reg = FDI_RX_CTL(pipe);
  3569. temp = I915_READ(reg);
  3570. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3571. /* Wait for the clocks to turn off. */
  3572. POSTING_READ(reg);
  3573. udelay(100);
  3574. }
  3575. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3576. {
  3577. struct drm_device *dev = crtc->dev;
  3578. struct drm_i915_private *dev_priv = to_i915(dev);
  3579. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3580. int pipe = intel_crtc->pipe;
  3581. i915_reg_t reg;
  3582. u32 temp;
  3583. /* disable CPU FDI tx and PCH FDI rx */
  3584. reg = FDI_TX_CTL(pipe);
  3585. temp = I915_READ(reg);
  3586. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3587. POSTING_READ(reg);
  3588. reg = FDI_RX_CTL(pipe);
  3589. temp = I915_READ(reg);
  3590. temp &= ~(0x7 << 16);
  3591. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3592. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3593. POSTING_READ(reg);
  3594. udelay(100);
  3595. /* Ironlake workaround, disable clock pointer after downing FDI */
  3596. if (HAS_PCH_IBX(dev_priv))
  3597. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3598. /* still set train pattern 1 */
  3599. reg = FDI_TX_CTL(pipe);
  3600. temp = I915_READ(reg);
  3601. temp &= ~FDI_LINK_TRAIN_NONE;
  3602. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3603. I915_WRITE(reg, temp);
  3604. reg = FDI_RX_CTL(pipe);
  3605. temp = I915_READ(reg);
  3606. if (HAS_PCH_CPT(dev_priv)) {
  3607. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3608. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3609. } else {
  3610. temp &= ~FDI_LINK_TRAIN_NONE;
  3611. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3612. }
  3613. /* BPC in FDI rx is consistent with that in PIPECONF */
  3614. temp &= ~(0x07 << 16);
  3615. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3616. I915_WRITE(reg, temp);
  3617. POSTING_READ(reg);
  3618. udelay(100);
  3619. }
  3620. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3621. {
  3622. struct drm_i915_private *dev_priv = to_i915(dev);
  3623. struct intel_crtc *crtc;
  3624. /* Note that we don't need to be called with mode_config.lock here
  3625. * as our list of CRTC objects is static for the lifetime of the
  3626. * device and so cannot disappear as we iterate. Similarly, we can
  3627. * happily treat the predicates as racy, atomic checks as userspace
  3628. * cannot claim and pin a new fb without at least acquring the
  3629. * struct_mutex and so serialising with us.
  3630. */
  3631. for_each_intel_crtc(dev, crtc) {
  3632. if (atomic_read(&crtc->unpin_work_count) == 0)
  3633. continue;
  3634. if (crtc->flip_work)
  3635. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3636. return true;
  3637. }
  3638. return false;
  3639. }
  3640. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3641. {
  3642. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3643. struct intel_flip_work *work = intel_crtc->flip_work;
  3644. intel_crtc->flip_work = NULL;
  3645. if (work->event)
  3646. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3647. drm_crtc_vblank_put(&intel_crtc->base);
  3648. wake_up_all(&dev_priv->pending_flip_queue);
  3649. queue_work(dev_priv->wq, &work->unpin_work);
  3650. trace_i915_flip_complete(intel_crtc->plane,
  3651. work->pending_flip_obj);
  3652. }
  3653. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3654. {
  3655. struct drm_device *dev = crtc->dev;
  3656. struct drm_i915_private *dev_priv = to_i915(dev);
  3657. long ret;
  3658. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3659. ret = wait_event_interruptible_timeout(
  3660. dev_priv->pending_flip_queue,
  3661. !intel_crtc_has_pending_flip(crtc),
  3662. 60*HZ);
  3663. if (ret < 0)
  3664. return ret;
  3665. if (ret == 0) {
  3666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3667. struct intel_flip_work *work;
  3668. spin_lock_irq(&dev->event_lock);
  3669. work = intel_crtc->flip_work;
  3670. if (work && !is_mmio_work(work)) {
  3671. WARN_ONCE(1, "Removing stuck page flip\n");
  3672. page_flip_completed(intel_crtc);
  3673. }
  3674. spin_unlock_irq(&dev->event_lock);
  3675. }
  3676. return 0;
  3677. }
  3678. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3679. {
  3680. u32 temp;
  3681. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3682. mutex_lock(&dev_priv->sb_lock);
  3683. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3684. temp |= SBI_SSCCTL_DISABLE;
  3685. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3686. mutex_unlock(&dev_priv->sb_lock);
  3687. }
  3688. /* Program iCLKIP clock to the desired frequency */
  3689. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3690. {
  3691. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3692. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3693. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3694. u32 temp;
  3695. lpt_disable_iclkip(dev_priv);
  3696. /* The iCLK virtual clock root frequency is in MHz,
  3697. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3698. * divisors, it is necessary to divide one by another, so we
  3699. * convert the virtual clock precision to KHz here for higher
  3700. * precision.
  3701. */
  3702. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3703. u32 iclk_virtual_root_freq = 172800 * 1000;
  3704. u32 iclk_pi_range = 64;
  3705. u32 desired_divisor;
  3706. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3707. clock << auxdiv);
  3708. divsel = (desired_divisor / iclk_pi_range) - 2;
  3709. phaseinc = desired_divisor % iclk_pi_range;
  3710. /*
  3711. * Near 20MHz is a corner case which is
  3712. * out of range for the 7-bit divisor
  3713. */
  3714. if (divsel <= 0x7f)
  3715. break;
  3716. }
  3717. /* This should not happen with any sane values */
  3718. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3719. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3720. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3721. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3722. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3723. clock,
  3724. auxdiv,
  3725. divsel,
  3726. phasedir,
  3727. phaseinc);
  3728. mutex_lock(&dev_priv->sb_lock);
  3729. /* Program SSCDIVINTPHASE6 */
  3730. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3731. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3732. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3733. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3734. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3735. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3736. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3737. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3738. /* Program SSCAUXDIV */
  3739. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3740. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3741. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3742. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3743. /* Enable modulator and associated divider */
  3744. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3745. temp &= ~SBI_SSCCTL_DISABLE;
  3746. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3747. mutex_unlock(&dev_priv->sb_lock);
  3748. /* Wait for initialization time */
  3749. udelay(24);
  3750. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3751. }
  3752. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3753. {
  3754. u32 divsel, phaseinc, auxdiv;
  3755. u32 iclk_virtual_root_freq = 172800 * 1000;
  3756. u32 iclk_pi_range = 64;
  3757. u32 desired_divisor;
  3758. u32 temp;
  3759. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3760. return 0;
  3761. mutex_lock(&dev_priv->sb_lock);
  3762. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3763. if (temp & SBI_SSCCTL_DISABLE) {
  3764. mutex_unlock(&dev_priv->sb_lock);
  3765. return 0;
  3766. }
  3767. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3768. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3769. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3770. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3771. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3772. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3773. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3774. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3775. mutex_unlock(&dev_priv->sb_lock);
  3776. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3777. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3778. desired_divisor << auxdiv);
  3779. }
  3780. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3781. enum pipe pch_transcoder)
  3782. {
  3783. struct drm_device *dev = crtc->base.dev;
  3784. struct drm_i915_private *dev_priv = to_i915(dev);
  3785. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3786. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3787. I915_READ(HTOTAL(cpu_transcoder)));
  3788. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3789. I915_READ(HBLANK(cpu_transcoder)));
  3790. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3791. I915_READ(HSYNC(cpu_transcoder)));
  3792. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3793. I915_READ(VTOTAL(cpu_transcoder)));
  3794. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3795. I915_READ(VBLANK(cpu_transcoder)));
  3796. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3797. I915_READ(VSYNC(cpu_transcoder)));
  3798. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3799. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3800. }
  3801. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3802. {
  3803. struct drm_i915_private *dev_priv = to_i915(dev);
  3804. uint32_t temp;
  3805. temp = I915_READ(SOUTH_CHICKEN1);
  3806. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3807. return;
  3808. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3809. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3810. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3811. if (enable)
  3812. temp |= FDI_BC_BIFURCATION_SELECT;
  3813. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3814. I915_WRITE(SOUTH_CHICKEN1, temp);
  3815. POSTING_READ(SOUTH_CHICKEN1);
  3816. }
  3817. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3818. {
  3819. struct drm_device *dev = intel_crtc->base.dev;
  3820. switch (intel_crtc->pipe) {
  3821. case PIPE_A:
  3822. break;
  3823. case PIPE_B:
  3824. if (intel_crtc->config->fdi_lanes > 2)
  3825. cpt_set_fdi_bc_bifurcation(dev, false);
  3826. else
  3827. cpt_set_fdi_bc_bifurcation(dev, true);
  3828. break;
  3829. case PIPE_C:
  3830. cpt_set_fdi_bc_bifurcation(dev, true);
  3831. break;
  3832. default:
  3833. BUG();
  3834. }
  3835. }
  3836. /* Return which DP Port should be selected for Transcoder DP control */
  3837. static enum port
  3838. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3839. {
  3840. struct drm_device *dev = crtc->dev;
  3841. struct intel_encoder *encoder;
  3842. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3843. if (encoder->type == INTEL_OUTPUT_DP ||
  3844. encoder->type == INTEL_OUTPUT_EDP)
  3845. return enc_to_dig_port(&encoder->base)->port;
  3846. }
  3847. return -1;
  3848. }
  3849. /*
  3850. * Enable PCH resources required for PCH ports:
  3851. * - PCH PLLs
  3852. * - FDI training & RX/TX
  3853. * - update transcoder timings
  3854. * - DP transcoding bits
  3855. * - transcoder
  3856. */
  3857. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3858. {
  3859. struct drm_device *dev = crtc->dev;
  3860. struct drm_i915_private *dev_priv = to_i915(dev);
  3861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3862. int pipe = intel_crtc->pipe;
  3863. u32 temp;
  3864. assert_pch_transcoder_disabled(dev_priv, pipe);
  3865. if (IS_IVYBRIDGE(dev_priv))
  3866. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3867. /* Write the TU size bits before fdi link training, so that error
  3868. * detection works. */
  3869. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3870. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3871. /* For PCH output, training FDI link */
  3872. dev_priv->display.fdi_link_train(crtc);
  3873. /* We need to program the right clock selection before writing the pixel
  3874. * mutliplier into the DPLL. */
  3875. if (HAS_PCH_CPT(dev_priv)) {
  3876. u32 sel;
  3877. temp = I915_READ(PCH_DPLL_SEL);
  3878. temp |= TRANS_DPLL_ENABLE(pipe);
  3879. sel = TRANS_DPLLB_SEL(pipe);
  3880. if (intel_crtc->config->shared_dpll ==
  3881. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3882. temp |= sel;
  3883. else
  3884. temp &= ~sel;
  3885. I915_WRITE(PCH_DPLL_SEL, temp);
  3886. }
  3887. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3888. * transcoder, and we actually should do this to not upset any PCH
  3889. * transcoder that already use the clock when we share it.
  3890. *
  3891. * Note that enable_shared_dpll tries to do the right thing, but
  3892. * get_shared_dpll unconditionally resets the pll - we need that to have
  3893. * the right LVDS enable sequence. */
  3894. intel_enable_shared_dpll(intel_crtc);
  3895. /* set transcoder timing, panel must allow it */
  3896. assert_panel_unlocked(dev_priv, pipe);
  3897. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3898. intel_fdi_normal_train(crtc);
  3899. /* For PCH DP, enable TRANS_DP_CTL */
  3900. if (HAS_PCH_CPT(dev_priv) &&
  3901. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3902. const struct drm_display_mode *adjusted_mode =
  3903. &intel_crtc->config->base.adjusted_mode;
  3904. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3905. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3906. temp = I915_READ(reg);
  3907. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3908. TRANS_DP_SYNC_MASK |
  3909. TRANS_DP_BPC_MASK);
  3910. temp |= TRANS_DP_OUTPUT_ENABLE;
  3911. temp |= bpc << 9; /* same format but at 11:9 */
  3912. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3913. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3914. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3915. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3916. switch (intel_trans_dp_port_sel(crtc)) {
  3917. case PORT_B:
  3918. temp |= TRANS_DP_PORT_SEL_B;
  3919. break;
  3920. case PORT_C:
  3921. temp |= TRANS_DP_PORT_SEL_C;
  3922. break;
  3923. case PORT_D:
  3924. temp |= TRANS_DP_PORT_SEL_D;
  3925. break;
  3926. default:
  3927. BUG();
  3928. }
  3929. I915_WRITE(reg, temp);
  3930. }
  3931. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3932. }
  3933. static void lpt_pch_enable(struct drm_crtc *crtc)
  3934. {
  3935. struct drm_device *dev = crtc->dev;
  3936. struct drm_i915_private *dev_priv = to_i915(dev);
  3937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3938. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3939. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3940. lpt_program_iclkip(crtc);
  3941. /* Set transcoder timing. */
  3942. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3943. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3944. }
  3945. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3946. {
  3947. struct drm_i915_private *dev_priv = to_i915(dev);
  3948. i915_reg_t dslreg = PIPEDSL(pipe);
  3949. u32 temp;
  3950. temp = I915_READ(dslreg);
  3951. udelay(500);
  3952. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3953. if (wait_for(I915_READ(dslreg) != temp, 5))
  3954. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3955. }
  3956. }
  3957. static int
  3958. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3959. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3960. int src_w, int src_h, int dst_w, int dst_h)
  3961. {
  3962. struct intel_crtc_scaler_state *scaler_state =
  3963. &crtc_state->scaler_state;
  3964. struct intel_crtc *intel_crtc =
  3965. to_intel_crtc(crtc_state->base.crtc);
  3966. int need_scaling;
  3967. need_scaling = drm_rotation_90_or_270(rotation) ?
  3968. (src_h != dst_w || src_w != dst_h):
  3969. (src_w != dst_w || src_h != dst_h);
  3970. /*
  3971. * if plane is being disabled or scaler is no more required or force detach
  3972. * - free scaler binded to this plane/crtc
  3973. * - in order to do this, update crtc->scaler_usage
  3974. *
  3975. * Here scaler state in crtc_state is set free so that
  3976. * scaler can be assigned to other user. Actual register
  3977. * update to free the scaler is done in plane/panel-fit programming.
  3978. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3979. */
  3980. if (force_detach || !need_scaling) {
  3981. if (*scaler_id >= 0) {
  3982. scaler_state->scaler_users &= ~(1 << scaler_user);
  3983. scaler_state->scalers[*scaler_id].in_use = 0;
  3984. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3985. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3986. intel_crtc->pipe, scaler_user, *scaler_id,
  3987. scaler_state->scaler_users);
  3988. *scaler_id = -1;
  3989. }
  3990. return 0;
  3991. }
  3992. /* range checks */
  3993. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3994. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3995. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3996. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3997. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3998. "size is out of scaler range\n",
  3999. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4000. return -EINVAL;
  4001. }
  4002. /* mark this plane as a scaler user in crtc_state */
  4003. scaler_state->scaler_users |= (1 << scaler_user);
  4004. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4005. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4006. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4007. scaler_state->scaler_users);
  4008. return 0;
  4009. }
  4010. /**
  4011. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4012. *
  4013. * @state: crtc's scaler state
  4014. *
  4015. * Return
  4016. * 0 - scaler_usage updated successfully
  4017. * error - requested scaling cannot be supported or other error condition
  4018. */
  4019. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4020. {
  4021. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4022. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4023. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4024. state->pipe_src_w, state->pipe_src_h,
  4025. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4026. }
  4027. /**
  4028. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4029. *
  4030. * @state: crtc's scaler state
  4031. * @plane_state: atomic plane state to update
  4032. *
  4033. * Return
  4034. * 0 - scaler_usage updated successfully
  4035. * error - requested scaling cannot be supported or other error condition
  4036. */
  4037. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4038. struct intel_plane_state *plane_state)
  4039. {
  4040. struct intel_plane *intel_plane =
  4041. to_intel_plane(plane_state->base.plane);
  4042. struct drm_framebuffer *fb = plane_state->base.fb;
  4043. int ret;
  4044. bool force_detach = !fb || !plane_state->base.visible;
  4045. ret = skl_update_scaler(crtc_state, force_detach,
  4046. drm_plane_index(&intel_plane->base),
  4047. &plane_state->scaler_id,
  4048. plane_state->base.rotation,
  4049. drm_rect_width(&plane_state->base.src) >> 16,
  4050. drm_rect_height(&plane_state->base.src) >> 16,
  4051. drm_rect_width(&plane_state->base.dst),
  4052. drm_rect_height(&plane_state->base.dst));
  4053. if (ret || plane_state->scaler_id < 0)
  4054. return ret;
  4055. /* check colorkey */
  4056. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4057. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4058. intel_plane->base.base.id,
  4059. intel_plane->base.name);
  4060. return -EINVAL;
  4061. }
  4062. /* Check src format */
  4063. switch (fb->pixel_format) {
  4064. case DRM_FORMAT_RGB565:
  4065. case DRM_FORMAT_XBGR8888:
  4066. case DRM_FORMAT_XRGB8888:
  4067. case DRM_FORMAT_ABGR8888:
  4068. case DRM_FORMAT_ARGB8888:
  4069. case DRM_FORMAT_XRGB2101010:
  4070. case DRM_FORMAT_XBGR2101010:
  4071. case DRM_FORMAT_YUYV:
  4072. case DRM_FORMAT_YVYU:
  4073. case DRM_FORMAT_UYVY:
  4074. case DRM_FORMAT_VYUY:
  4075. break;
  4076. default:
  4077. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4078. intel_plane->base.base.id, intel_plane->base.name,
  4079. fb->base.id, fb->pixel_format);
  4080. return -EINVAL;
  4081. }
  4082. return 0;
  4083. }
  4084. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4085. {
  4086. int i;
  4087. for (i = 0; i < crtc->num_scalers; i++)
  4088. skl_detach_scaler(crtc, i);
  4089. }
  4090. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4091. {
  4092. struct drm_device *dev = crtc->base.dev;
  4093. struct drm_i915_private *dev_priv = to_i915(dev);
  4094. int pipe = crtc->pipe;
  4095. struct intel_crtc_scaler_state *scaler_state =
  4096. &crtc->config->scaler_state;
  4097. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4098. if (crtc->config->pch_pfit.enabled) {
  4099. int id;
  4100. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4101. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4102. return;
  4103. }
  4104. id = scaler_state->scaler_id;
  4105. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4106. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4107. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4108. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4109. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4110. }
  4111. }
  4112. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4113. {
  4114. struct drm_device *dev = crtc->base.dev;
  4115. struct drm_i915_private *dev_priv = to_i915(dev);
  4116. int pipe = crtc->pipe;
  4117. if (crtc->config->pch_pfit.enabled) {
  4118. /* Force use of hard-coded filter coefficients
  4119. * as some pre-programmed values are broken,
  4120. * e.g. x201.
  4121. */
  4122. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4123. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4124. PF_PIPE_SEL_IVB(pipe));
  4125. else
  4126. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4127. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4128. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4129. }
  4130. }
  4131. void hsw_enable_ips(struct intel_crtc *crtc)
  4132. {
  4133. struct drm_device *dev = crtc->base.dev;
  4134. struct drm_i915_private *dev_priv = to_i915(dev);
  4135. if (!crtc->config->ips_enabled)
  4136. return;
  4137. /*
  4138. * We can only enable IPS after we enable a plane and wait for a vblank
  4139. * This function is called from post_plane_update, which is run after
  4140. * a vblank wait.
  4141. */
  4142. assert_plane_enabled(dev_priv, crtc->plane);
  4143. if (IS_BROADWELL(dev_priv)) {
  4144. mutex_lock(&dev_priv->rps.hw_lock);
  4145. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4146. mutex_unlock(&dev_priv->rps.hw_lock);
  4147. /* Quoting Art Runyan: "its not safe to expect any particular
  4148. * value in IPS_CTL bit 31 after enabling IPS through the
  4149. * mailbox." Moreover, the mailbox may return a bogus state,
  4150. * so we need to just enable it and continue on.
  4151. */
  4152. } else {
  4153. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4154. /* The bit only becomes 1 in the next vblank, so this wait here
  4155. * is essentially intel_wait_for_vblank. If we don't have this
  4156. * and don't wait for vblanks until the end of crtc_enable, then
  4157. * the HW state readout code will complain that the expected
  4158. * IPS_CTL value is not the one we read. */
  4159. if (intel_wait_for_register(dev_priv,
  4160. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4161. 50))
  4162. DRM_ERROR("Timed out waiting for IPS enable\n");
  4163. }
  4164. }
  4165. void hsw_disable_ips(struct intel_crtc *crtc)
  4166. {
  4167. struct drm_device *dev = crtc->base.dev;
  4168. struct drm_i915_private *dev_priv = to_i915(dev);
  4169. if (!crtc->config->ips_enabled)
  4170. return;
  4171. assert_plane_enabled(dev_priv, crtc->plane);
  4172. if (IS_BROADWELL(dev_priv)) {
  4173. mutex_lock(&dev_priv->rps.hw_lock);
  4174. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4175. mutex_unlock(&dev_priv->rps.hw_lock);
  4176. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4177. if (intel_wait_for_register(dev_priv,
  4178. IPS_CTL, IPS_ENABLE, 0,
  4179. 42))
  4180. DRM_ERROR("Timed out waiting for IPS disable\n");
  4181. } else {
  4182. I915_WRITE(IPS_CTL, 0);
  4183. POSTING_READ(IPS_CTL);
  4184. }
  4185. /* We need to wait for a vblank before we can disable the plane. */
  4186. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4187. }
  4188. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4189. {
  4190. if (intel_crtc->overlay) {
  4191. struct drm_device *dev = intel_crtc->base.dev;
  4192. struct drm_i915_private *dev_priv = to_i915(dev);
  4193. mutex_lock(&dev->struct_mutex);
  4194. dev_priv->mm.interruptible = false;
  4195. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4196. dev_priv->mm.interruptible = true;
  4197. mutex_unlock(&dev->struct_mutex);
  4198. }
  4199. /* Let userspace switch the overlay on again. In most cases userspace
  4200. * has to recompute where to put it anyway.
  4201. */
  4202. }
  4203. /**
  4204. * intel_post_enable_primary - Perform operations after enabling primary plane
  4205. * @crtc: the CRTC whose primary plane was just enabled
  4206. *
  4207. * Performs potentially sleeping operations that must be done after the primary
  4208. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4209. * called due to an explicit primary plane update, or due to an implicit
  4210. * re-enable that is caused when a sprite plane is updated to no longer
  4211. * completely hide the primary plane.
  4212. */
  4213. static void
  4214. intel_post_enable_primary(struct drm_crtc *crtc)
  4215. {
  4216. struct drm_device *dev = crtc->dev;
  4217. struct drm_i915_private *dev_priv = to_i915(dev);
  4218. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4219. int pipe = intel_crtc->pipe;
  4220. /*
  4221. * FIXME IPS should be fine as long as one plane is
  4222. * enabled, but in practice it seems to have problems
  4223. * when going from primary only to sprite only and vice
  4224. * versa.
  4225. */
  4226. hsw_enable_ips(intel_crtc);
  4227. /*
  4228. * Gen2 reports pipe underruns whenever all planes are disabled.
  4229. * So don't enable underrun reporting before at least some planes
  4230. * are enabled.
  4231. * FIXME: Need to fix the logic to work when we turn off all planes
  4232. * but leave the pipe running.
  4233. */
  4234. if (IS_GEN2(dev_priv))
  4235. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4236. /* Underruns don't always raise interrupts, so check manually. */
  4237. intel_check_cpu_fifo_underruns(dev_priv);
  4238. intel_check_pch_fifo_underruns(dev_priv);
  4239. }
  4240. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4241. static void
  4242. intel_pre_disable_primary(struct drm_crtc *crtc)
  4243. {
  4244. struct drm_device *dev = crtc->dev;
  4245. struct drm_i915_private *dev_priv = to_i915(dev);
  4246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4247. int pipe = intel_crtc->pipe;
  4248. /*
  4249. * Gen2 reports pipe underruns whenever all planes are disabled.
  4250. * So diasble underrun reporting before all the planes get disabled.
  4251. * FIXME: Need to fix the logic to work when we turn off all planes
  4252. * but leave the pipe running.
  4253. */
  4254. if (IS_GEN2(dev_priv))
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4256. /*
  4257. * FIXME IPS should be fine as long as one plane is
  4258. * enabled, but in practice it seems to have problems
  4259. * when going from primary only to sprite only and vice
  4260. * versa.
  4261. */
  4262. hsw_disable_ips(intel_crtc);
  4263. }
  4264. /* FIXME get rid of this and use pre_plane_update */
  4265. static void
  4266. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4267. {
  4268. struct drm_device *dev = crtc->dev;
  4269. struct drm_i915_private *dev_priv = to_i915(dev);
  4270. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4271. int pipe = intel_crtc->pipe;
  4272. intel_pre_disable_primary(crtc);
  4273. /*
  4274. * Vblank time updates from the shadow to live plane control register
  4275. * are blocked if the memory self-refresh mode is active at that
  4276. * moment. So to make sure the plane gets truly disabled, disable
  4277. * first the self-refresh mode. The self-refresh enable bit in turn
  4278. * will be checked/applied by the HW only at the next frame start
  4279. * event which is after the vblank start event, so we need to have a
  4280. * wait-for-vblank between disabling the plane and the pipe.
  4281. */
  4282. if (HAS_GMCH_DISPLAY(dev_priv)) {
  4283. intel_set_memory_cxsr(dev_priv, false);
  4284. dev_priv->wm.vlv.cxsr = false;
  4285. intel_wait_for_vblank(dev_priv, pipe);
  4286. }
  4287. }
  4288. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4289. {
  4290. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4291. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4292. struct intel_crtc_state *pipe_config =
  4293. to_intel_crtc_state(crtc->base.state);
  4294. struct drm_plane *primary = crtc->base.primary;
  4295. struct drm_plane_state *old_pri_state =
  4296. drm_atomic_get_existing_plane_state(old_state, primary);
  4297. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4298. crtc->wm.cxsr_allowed = true;
  4299. if (pipe_config->update_wm_post && pipe_config->base.active)
  4300. intel_update_watermarks(crtc);
  4301. if (old_pri_state) {
  4302. struct intel_plane_state *primary_state =
  4303. to_intel_plane_state(primary->state);
  4304. struct intel_plane_state *old_primary_state =
  4305. to_intel_plane_state(old_pri_state);
  4306. intel_fbc_post_update(crtc);
  4307. if (primary_state->base.visible &&
  4308. (needs_modeset(&pipe_config->base) ||
  4309. !old_primary_state->base.visible))
  4310. intel_post_enable_primary(&crtc->base);
  4311. }
  4312. }
  4313. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4314. {
  4315. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4316. struct drm_device *dev = crtc->base.dev;
  4317. struct drm_i915_private *dev_priv = to_i915(dev);
  4318. struct intel_crtc_state *pipe_config =
  4319. to_intel_crtc_state(crtc->base.state);
  4320. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4321. struct drm_plane *primary = crtc->base.primary;
  4322. struct drm_plane_state *old_pri_state =
  4323. drm_atomic_get_existing_plane_state(old_state, primary);
  4324. bool modeset = needs_modeset(&pipe_config->base);
  4325. struct intel_atomic_state *old_intel_state =
  4326. to_intel_atomic_state(old_state);
  4327. if (old_pri_state) {
  4328. struct intel_plane_state *primary_state =
  4329. to_intel_plane_state(primary->state);
  4330. struct intel_plane_state *old_primary_state =
  4331. to_intel_plane_state(old_pri_state);
  4332. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4333. if (old_primary_state->base.visible &&
  4334. (modeset || !primary_state->base.visible))
  4335. intel_pre_disable_primary(&crtc->base);
  4336. }
  4337. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4338. crtc->wm.cxsr_allowed = false;
  4339. /*
  4340. * Vblank time updates from the shadow to live plane control register
  4341. * are blocked if the memory self-refresh mode is active at that
  4342. * moment. So to make sure the plane gets truly disabled, disable
  4343. * first the self-refresh mode. The self-refresh enable bit in turn
  4344. * will be checked/applied by the HW only at the next frame start
  4345. * event which is after the vblank start event, so we need to have a
  4346. * wait-for-vblank between disabling the plane and the pipe.
  4347. */
  4348. if (old_crtc_state->base.active) {
  4349. intel_set_memory_cxsr(dev_priv, false);
  4350. dev_priv->wm.vlv.cxsr = false;
  4351. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4352. }
  4353. }
  4354. /*
  4355. * IVB workaround: must disable low power watermarks for at least
  4356. * one frame before enabling scaling. LP watermarks can be re-enabled
  4357. * when scaling is disabled.
  4358. *
  4359. * WaCxSRDisabledForSpriteScaling:ivb
  4360. */
  4361. if (pipe_config->disable_lp_wm) {
  4362. ilk_disable_lp_wm(dev);
  4363. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4364. }
  4365. /*
  4366. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4367. * watermark programming here.
  4368. */
  4369. if (needs_modeset(&pipe_config->base))
  4370. return;
  4371. /*
  4372. * For platforms that support atomic watermarks, program the
  4373. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4374. * will be the intermediate values that are safe for both pre- and
  4375. * post- vblank; when vblank happens, the 'active' values will be set
  4376. * to the final 'target' values and we'll do this again to get the
  4377. * optimal watermarks. For gen9+ platforms, the values we program here
  4378. * will be the final target values which will get automatically latched
  4379. * at vblank time; no further programming will be necessary.
  4380. *
  4381. * If a platform hasn't been transitioned to atomic watermarks yet,
  4382. * we'll continue to update watermarks the old way, if flags tell
  4383. * us to.
  4384. */
  4385. if (dev_priv->display.initial_watermarks != NULL)
  4386. dev_priv->display.initial_watermarks(old_intel_state,
  4387. pipe_config);
  4388. else if (pipe_config->update_wm_pre)
  4389. intel_update_watermarks(crtc);
  4390. }
  4391. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4392. {
  4393. struct drm_device *dev = crtc->dev;
  4394. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4395. struct drm_plane *p;
  4396. int pipe = intel_crtc->pipe;
  4397. intel_crtc_dpms_overlay_disable(intel_crtc);
  4398. drm_for_each_plane_mask(p, dev, plane_mask)
  4399. to_intel_plane(p)->disable_plane(p, crtc);
  4400. /*
  4401. * FIXME: Once we grow proper nuclear flip support out of this we need
  4402. * to compute the mask of flip planes precisely. For the time being
  4403. * consider this a flip to a NULL plane.
  4404. */
  4405. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4406. }
  4407. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4408. struct intel_crtc_state *crtc_state,
  4409. struct drm_atomic_state *old_state)
  4410. {
  4411. struct drm_connector_state *old_conn_state;
  4412. struct drm_connector *conn;
  4413. int i;
  4414. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4415. struct drm_connector_state *conn_state = conn->state;
  4416. struct intel_encoder *encoder =
  4417. to_intel_encoder(conn_state->best_encoder);
  4418. if (conn_state->crtc != crtc)
  4419. continue;
  4420. if (encoder->pre_pll_enable)
  4421. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4422. }
  4423. }
  4424. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4425. struct intel_crtc_state *crtc_state,
  4426. struct drm_atomic_state *old_state)
  4427. {
  4428. struct drm_connector_state *old_conn_state;
  4429. struct drm_connector *conn;
  4430. int i;
  4431. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4432. struct drm_connector_state *conn_state = conn->state;
  4433. struct intel_encoder *encoder =
  4434. to_intel_encoder(conn_state->best_encoder);
  4435. if (conn_state->crtc != crtc)
  4436. continue;
  4437. if (encoder->pre_enable)
  4438. encoder->pre_enable(encoder, crtc_state, conn_state);
  4439. }
  4440. }
  4441. static void intel_encoders_enable(struct drm_crtc *crtc,
  4442. struct intel_crtc_state *crtc_state,
  4443. struct drm_atomic_state *old_state)
  4444. {
  4445. struct drm_connector_state *old_conn_state;
  4446. struct drm_connector *conn;
  4447. int i;
  4448. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4449. struct drm_connector_state *conn_state = conn->state;
  4450. struct intel_encoder *encoder =
  4451. to_intel_encoder(conn_state->best_encoder);
  4452. if (conn_state->crtc != crtc)
  4453. continue;
  4454. encoder->enable(encoder, crtc_state, conn_state);
  4455. intel_opregion_notify_encoder(encoder, true);
  4456. }
  4457. }
  4458. static void intel_encoders_disable(struct drm_crtc *crtc,
  4459. struct intel_crtc_state *old_crtc_state,
  4460. struct drm_atomic_state *old_state)
  4461. {
  4462. struct drm_connector_state *old_conn_state;
  4463. struct drm_connector *conn;
  4464. int i;
  4465. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4466. struct intel_encoder *encoder =
  4467. to_intel_encoder(old_conn_state->best_encoder);
  4468. if (old_conn_state->crtc != crtc)
  4469. continue;
  4470. intel_opregion_notify_encoder(encoder, false);
  4471. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4472. }
  4473. }
  4474. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4475. struct intel_crtc_state *old_crtc_state,
  4476. struct drm_atomic_state *old_state)
  4477. {
  4478. struct drm_connector_state *old_conn_state;
  4479. struct drm_connector *conn;
  4480. int i;
  4481. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4482. struct intel_encoder *encoder =
  4483. to_intel_encoder(old_conn_state->best_encoder);
  4484. if (old_conn_state->crtc != crtc)
  4485. continue;
  4486. if (encoder->post_disable)
  4487. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4488. }
  4489. }
  4490. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4491. struct intel_crtc_state *old_crtc_state,
  4492. struct drm_atomic_state *old_state)
  4493. {
  4494. struct drm_connector_state *old_conn_state;
  4495. struct drm_connector *conn;
  4496. int i;
  4497. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4498. struct intel_encoder *encoder =
  4499. to_intel_encoder(old_conn_state->best_encoder);
  4500. if (old_conn_state->crtc != crtc)
  4501. continue;
  4502. if (encoder->post_pll_disable)
  4503. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4504. }
  4505. }
  4506. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4507. struct drm_atomic_state *old_state)
  4508. {
  4509. struct drm_crtc *crtc = pipe_config->base.crtc;
  4510. struct drm_device *dev = crtc->dev;
  4511. struct drm_i915_private *dev_priv = to_i915(dev);
  4512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4513. int pipe = intel_crtc->pipe;
  4514. struct intel_atomic_state *old_intel_state =
  4515. to_intel_atomic_state(old_state);
  4516. if (WARN_ON(intel_crtc->active))
  4517. return;
  4518. /*
  4519. * Sometimes spurious CPU pipe underruns happen during FDI
  4520. * training, at least with VGA+HDMI cloning. Suppress them.
  4521. *
  4522. * On ILK we get an occasional spurious CPU pipe underruns
  4523. * between eDP port A enable and vdd enable. Also PCH port
  4524. * enable seems to result in the occasional CPU pipe underrun.
  4525. *
  4526. * Spurious PCH underruns also occur during PCH enabling.
  4527. */
  4528. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4529. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4530. if (intel_crtc->config->has_pch_encoder)
  4531. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4532. if (intel_crtc->config->has_pch_encoder)
  4533. intel_prepare_shared_dpll(intel_crtc);
  4534. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4535. intel_dp_set_m_n(intel_crtc, M1_N1);
  4536. intel_set_pipe_timings(intel_crtc);
  4537. intel_set_pipe_src_size(intel_crtc);
  4538. if (intel_crtc->config->has_pch_encoder) {
  4539. intel_cpu_transcoder_set_m_n(intel_crtc,
  4540. &intel_crtc->config->fdi_m_n, NULL);
  4541. }
  4542. ironlake_set_pipeconf(crtc);
  4543. intel_crtc->active = true;
  4544. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4545. if (intel_crtc->config->has_pch_encoder) {
  4546. /* Note: FDI PLL enabling _must_ be done before we enable the
  4547. * cpu pipes, hence this is separate from all the other fdi/pch
  4548. * enabling. */
  4549. ironlake_fdi_pll_enable(intel_crtc);
  4550. } else {
  4551. assert_fdi_tx_disabled(dev_priv, pipe);
  4552. assert_fdi_rx_disabled(dev_priv, pipe);
  4553. }
  4554. ironlake_pfit_enable(intel_crtc);
  4555. /*
  4556. * On ILK+ LUT must be loaded before the pipe is running but with
  4557. * clocks enabled
  4558. */
  4559. intel_color_load_luts(&pipe_config->base);
  4560. if (dev_priv->display.initial_watermarks != NULL)
  4561. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4562. intel_enable_pipe(intel_crtc);
  4563. if (intel_crtc->config->has_pch_encoder)
  4564. ironlake_pch_enable(crtc);
  4565. assert_vblank_disabled(crtc);
  4566. drm_crtc_vblank_on(crtc);
  4567. intel_encoders_enable(crtc, pipe_config, old_state);
  4568. if (HAS_PCH_CPT(dev_priv))
  4569. cpt_verify_modeset(dev, intel_crtc->pipe);
  4570. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4571. if (intel_crtc->config->has_pch_encoder)
  4572. intel_wait_for_vblank(dev_priv, pipe);
  4573. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4574. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4575. }
  4576. /* IPS only exists on ULT machines and is tied to pipe A. */
  4577. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4578. {
  4579. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4580. }
  4581. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4582. struct drm_atomic_state *old_state)
  4583. {
  4584. struct drm_crtc *crtc = pipe_config->base.crtc;
  4585. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4587. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4588. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4589. struct intel_atomic_state *old_intel_state =
  4590. to_intel_atomic_state(old_state);
  4591. if (WARN_ON(intel_crtc->active))
  4592. return;
  4593. if (intel_crtc->config->has_pch_encoder)
  4594. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4595. false);
  4596. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4597. if (intel_crtc->config->shared_dpll)
  4598. intel_enable_shared_dpll(intel_crtc);
  4599. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4600. intel_dp_set_m_n(intel_crtc, M1_N1);
  4601. if (!transcoder_is_dsi(cpu_transcoder))
  4602. intel_set_pipe_timings(intel_crtc);
  4603. intel_set_pipe_src_size(intel_crtc);
  4604. if (cpu_transcoder != TRANSCODER_EDP &&
  4605. !transcoder_is_dsi(cpu_transcoder)) {
  4606. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4607. intel_crtc->config->pixel_multiplier - 1);
  4608. }
  4609. if (intel_crtc->config->has_pch_encoder) {
  4610. intel_cpu_transcoder_set_m_n(intel_crtc,
  4611. &intel_crtc->config->fdi_m_n, NULL);
  4612. }
  4613. if (!transcoder_is_dsi(cpu_transcoder))
  4614. haswell_set_pipeconf(crtc);
  4615. haswell_set_pipemisc(crtc);
  4616. intel_color_set_csc(&pipe_config->base);
  4617. intel_crtc->active = true;
  4618. if (intel_crtc->config->has_pch_encoder)
  4619. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4620. else
  4621. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4622. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4623. if (intel_crtc->config->has_pch_encoder)
  4624. dev_priv->display.fdi_link_train(crtc);
  4625. if (!transcoder_is_dsi(cpu_transcoder))
  4626. intel_ddi_enable_pipe_clock(intel_crtc);
  4627. if (INTEL_GEN(dev_priv) >= 9)
  4628. skylake_pfit_enable(intel_crtc);
  4629. else
  4630. ironlake_pfit_enable(intel_crtc);
  4631. /*
  4632. * On ILK+ LUT must be loaded before the pipe is running but with
  4633. * clocks enabled
  4634. */
  4635. intel_color_load_luts(&pipe_config->base);
  4636. intel_ddi_set_pipe_settings(crtc);
  4637. if (!transcoder_is_dsi(cpu_transcoder))
  4638. intel_ddi_enable_transcoder_func(crtc);
  4639. if (dev_priv->display.initial_watermarks != NULL)
  4640. dev_priv->display.initial_watermarks(old_intel_state,
  4641. pipe_config);
  4642. else
  4643. intel_update_watermarks(intel_crtc);
  4644. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4645. if (!transcoder_is_dsi(cpu_transcoder))
  4646. intel_enable_pipe(intel_crtc);
  4647. if (intel_crtc->config->has_pch_encoder)
  4648. lpt_pch_enable(crtc);
  4649. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4650. intel_ddi_set_vc_payload_alloc(crtc, true);
  4651. assert_vblank_disabled(crtc);
  4652. drm_crtc_vblank_on(crtc);
  4653. intel_encoders_enable(crtc, pipe_config, old_state);
  4654. if (intel_crtc->config->has_pch_encoder) {
  4655. intel_wait_for_vblank(dev_priv, pipe);
  4656. intel_wait_for_vblank(dev_priv, pipe);
  4657. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4658. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4659. true);
  4660. }
  4661. /* If we change the relative order between pipe/planes enabling, we need
  4662. * to change the workaround. */
  4663. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4664. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4665. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4666. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4667. }
  4668. }
  4669. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4670. {
  4671. struct drm_device *dev = crtc->base.dev;
  4672. struct drm_i915_private *dev_priv = to_i915(dev);
  4673. int pipe = crtc->pipe;
  4674. /* To avoid upsetting the power well on haswell only disable the pfit if
  4675. * it's in use. The hw state code will make sure we get this right. */
  4676. if (force || crtc->config->pch_pfit.enabled) {
  4677. I915_WRITE(PF_CTL(pipe), 0);
  4678. I915_WRITE(PF_WIN_POS(pipe), 0);
  4679. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4680. }
  4681. }
  4682. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4683. struct drm_atomic_state *old_state)
  4684. {
  4685. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4686. struct drm_device *dev = crtc->dev;
  4687. struct drm_i915_private *dev_priv = to_i915(dev);
  4688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4689. int pipe = intel_crtc->pipe;
  4690. /*
  4691. * Sometimes spurious CPU pipe underruns happen when the
  4692. * pipe is already disabled, but FDI RX/TX is still enabled.
  4693. * Happens at least with VGA+HDMI cloning. Suppress them.
  4694. */
  4695. if (intel_crtc->config->has_pch_encoder) {
  4696. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4697. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4698. }
  4699. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4700. drm_crtc_vblank_off(crtc);
  4701. assert_vblank_disabled(crtc);
  4702. intel_disable_pipe(intel_crtc);
  4703. ironlake_pfit_disable(intel_crtc, false);
  4704. if (intel_crtc->config->has_pch_encoder)
  4705. ironlake_fdi_disable(crtc);
  4706. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4707. if (intel_crtc->config->has_pch_encoder) {
  4708. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4709. if (HAS_PCH_CPT(dev_priv)) {
  4710. i915_reg_t reg;
  4711. u32 temp;
  4712. /* disable TRANS_DP_CTL */
  4713. reg = TRANS_DP_CTL(pipe);
  4714. temp = I915_READ(reg);
  4715. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4716. TRANS_DP_PORT_SEL_MASK);
  4717. temp |= TRANS_DP_PORT_SEL_NONE;
  4718. I915_WRITE(reg, temp);
  4719. /* disable DPLL_SEL */
  4720. temp = I915_READ(PCH_DPLL_SEL);
  4721. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4722. I915_WRITE(PCH_DPLL_SEL, temp);
  4723. }
  4724. ironlake_fdi_pll_disable(intel_crtc);
  4725. }
  4726. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4727. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4728. }
  4729. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4730. struct drm_atomic_state *old_state)
  4731. {
  4732. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4733. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4734. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4735. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4736. if (intel_crtc->config->has_pch_encoder)
  4737. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4738. false);
  4739. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4740. drm_crtc_vblank_off(crtc);
  4741. assert_vblank_disabled(crtc);
  4742. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4743. if (!transcoder_is_dsi(cpu_transcoder))
  4744. intel_disable_pipe(intel_crtc);
  4745. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4746. intel_ddi_set_vc_payload_alloc(crtc, false);
  4747. if (!transcoder_is_dsi(cpu_transcoder))
  4748. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4749. if (INTEL_GEN(dev_priv) >= 9)
  4750. skylake_scaler_disable(intel_crtc);
  4751. else
  4752. ironlake_pfit_disable(intel_crtc, false);
  4753. if (!transcoder_is_dsi(cpu_transcoder))
  4754. intel_ddi_disable_pipe_clock(intel_crtc);
  4755. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4756. if (old_crtc_state->has_pch_encoder)
  4757. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4758. true);
  4759. }
  4760. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4761. {
  4762. struct drm_device *dev = crtc->base.dev;
  4763. struct drm_i915_private *dev_priv = to_i915(dev);
  4764. struct intel_crtc_state *pipe_config = crtc->config;
  4765. if (!pipe_config->gmch_pfit.control)
  4766. return;
  4767. /*
  4768. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4769. * according to register description and PRM.
  4770. */
  4771. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4772. assert_pipe_disabled(dev_priv, crtc->pipe);
  4773. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4774. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4775. /* Border color in case we don't scale up to the full screen. Black by
  4776. * default, change to something else for debugging. */
  4777. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4778. }
  4779. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4780. {
  4781. switch (port) {
  4782. case PORT_A:
  4783. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4784. case PORT_B:
  4785. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4786. case PORT_C:
  4787. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4788. case PORT_D:
  4789. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4790. case PORT_E:
  4791. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4792. default:
  4793. MISSING_CASE(port);
  4794. return POWER_DOMAIN_PORT_OTHER;
  4795. }
  4796. }
  4797. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4798. {
  4799. switch (port) {
  4800. case PORT_A:
  4801. return POWER_DOMAIN_AUX_A;
  4802. case PORT_B:
  4803. return POWER_DOMAIN_AUX_B;
  4804. case PORT_C:
  4805. return POWER_DOMAIN_AUX_C;
  4806. case PORT_D:
  4807. return POWER_DOMAIN_AUX_D;
  4808. case PORT_E:
  4809. /* FIXME: Check VBT for actual wiring of PORT E */
  4810. return POWER_DOMAIN_AUX_D;
  4811. default:
  4812. MISSING_CASE(port);
  4813. return POWER_DOMAIN_AUX_A;
  4814. }
  4815. }
  4816. enum intel_display_power_domain
  4817. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4818. {
  4819. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4820. struct intel_digital_port *intel_dig_port;
  4821. switch (intel_encoder->type) {
  4822. case INTEL_OUTPUT_UNKNOWN:
  4823. /* Only DDI platforms should ever use this output type */
  4824. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4825. case INTEL_OUTPUT_DP:
  4826. case INTEL_OUTPUT_HDMI:
  4827. case INTEL_OUTPUT_EDP:
  4828. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4829. return port_to_power_domain(intel_dig_port->port);
  4830. case INTEL_OUTPUT_DP_MST:
  4831. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4832. return port_to_power_domain(intel_dig_port->port);
  4833. case INTEL_OUTPUT_ANALOG:
  4834. return POWER_DOMAIN_PORT_CRT;
  4835. case INTEL_OUTPUT_DSI:
  4836. return POWER_DOMAIN_PORT_DSI;
  4837. default:
  4838. return POWER_DOMAIN_PORT_OTHER;
  4839. }
  4840. }
  4841. enum intel_display_power_domain
  4842. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4843. {
  4844. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4845. struct intel_digital_port *intel_dig_port;
  4846. switch (intel_encoder->type) {
  4847. case INTEL_OUTPUT_UNKNOWN:
  4848. case INTEL_OUTPUT_HDMI:
  4849. /*
  4850. * Only DDI platforms should ever use these output types.
  4851. * We can get here after the HDMI detect code has already set
  4852. * the type of the shared encoder. Since we can't be sure
  4853. * what's the status of the given connectors, play safe and
  4854. * run the DP detection too.
  4855. */
  4856. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4857. case INTEL_OUTPUT_DP:
  4858. case INTEL_OUTPUT_EDP:
  4859. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4860. return port_to_aux_power_domain(intel_dig_port->port);
  4861. case INTEL_OUTPUT_DP_MST:
  4862. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4863. return port_to_aux_power_domain(intel_dig_port->port);
  4864. default:
  4865. MISSING_CASE(intel_encoder->type);
  4866. return POWER_DOMAIN_AUX_A;
  4867. }
  4868. }
  4869. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4870. struct intel_crtc_state *crtc_state)
  4871. {
  4872. struct drm_device *dev = crtc->dev;
  4873. struct drm_encoder *encoder;
  4874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4875. enum pipe pipe = intel_crtc->pipe;
  4876. unsigned long mask;
  4877. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4878. if (!crtc_state->base.active)
  4879. return 0;
  4880. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4881. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4882. if (crtc_state->pch_pfit.enabled ||
  4883. crtc_state->pch_pfit.force_thru)
  4884. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4885. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4886. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4887. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4888. }
  4889. if (crtc_state->shared_dpll)
  4890. mask |= BIT(POWER_DOMAIN_PLLS);
  4891. return mask;
  4892. }
  4893. static unsigned long
  4894. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4895. struct intel_crtc_state *crtc_state)
  4896. {
  4897. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4898. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4899. enum intel_display_power_domain domain;
  4900. unsigned long domains, new_domains, old_domains;
  4901. old_domains = intel_crtc->enabled_power_domains;
  4902. intel_crtc->enabled_power_domains = new_domains =
  4903. get_crtc_power_domains(crtc, crtc_state);
  4904. domains = new_domains & ~old_domains;
  4905. for_each_power_domain(domain, domains)
  4906. intel_display_power_get(dev_priv, domain);
  4907. return old_domains & ~new_domains;
  4908. }
  4909. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4910. unsigned long domains)
  4911. {
  4912. enum intel_display_power_domain domain;
  4913. for_each_power_domain(domain, domains)
  4914. intel_display_power_put(dev_priv, domain);
  4915. }
  4916. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4917. {
  4918. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4919. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4920. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4921. return max_cdclk_freq;
  4922. else if (IS_CHERRYVIEW(dev_priv))
  4923. return max_cdclk_freq*95/100;
  4924. else if (INTEL_INFO(dev_priv)->gen < 4)
  4925. return 2*max_cdclk_freq*90/100;
  4926. else
  4927. return max_cdclk_freq*90/100;
  4928. }
  4929. static int skl_calc_cdclk(int max_pixclk, int vco);
  4930. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4931. {
  4932. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4933. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4934. int max_cdclk, vco;
  4935. vco = dev_priv->skl_preferred_vco_freq;
  4936. WARN_ON(vco != 8100000 && vco != 8640000);
  4937. /*
  4938. * Use the lower (vco 8640) cdclk values as a
  4939. * first guess. skl_calc_cdclk() will correct it
  4940. * if the preferred vco is 8100 instead.
  4941. */
  4942. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4943. max_cdclk = 617143;
  4944. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4945. max_cdclk = 540000;
  4946. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4947. max_cdclk = 432000;
  4948. else
  4949. max_cdclk = 308571;
  4950. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4951. } else if (IS_BROXTON(dev_priv)) {
  4952. dev_priv->max_cdclk_freq = 624000;
  4953. } else if (IS_BROADWELL(dev_priv)) {
  4954. /*
  4955. * FIXME with extra cooling we can allow
  4956. * 540 MHz for ULX and 675 Mhz for ULT.
  4957. * How can we know if extra cooling is
  4958. * available? PCI ID, VTB, something else?
  4959. */
  4960. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4961. dev_priv->max_cdclk_freq = 450000;
  4962. else if (IS_BDW_ULX(dev_priv))
  4963. dev_priv->max_cdclk_freq = 450000;
  4964. else if (IS_BDW_ULT(dev_priv))
  4965. dev_priv->max_cdclk_freq = 540000;
  4966. else
  4967. dev_priv->max_cdclk_freq = 675000;
  4968. } else if (IS_CHERRYVIEW(dev_priv)) {
  4969. dev_priv->max_cdclk_freq = 320000;
  4970. } else if (IS_VALLEYVIEW(dev_priv)) {
  4971. dev_priv->max_cdclk_freq = 400000;
  4972. } else {
  4973. /* otherwise assume cdclk is fixed */
  4974. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4975. }
  4976. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4977. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4978. dev_priv->max_cdclk_freq);
  4979. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4980. dev_priv->max_dotclk_freq);
  4981. }
  4982. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  4983. {
  4984. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  4985. if (INTEL_GEN(dev_priv) >= 9)
  4986. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4987. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4988. dev_priv->cdclk_pll.ref);
  4989. else
  4990. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4991. dev_priv->cdclk_freq);
  4992. /*
  4993. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4994. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4995. * of cdclk that generates 4MHz reference clock freq which is used to
  4996. * generate GMBus clock. This will vary with the cdclk freq.
  4997. */
  4998. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4999. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  5000. }
  5001. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  5002. static int skl_cdclk_decimal(int cdclk)
  5003. {
  5004. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5005. }
  5006. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5007. {
  5008. int ratio;
  5009. if (cdclk == dev_priv->cdclk_pll.ref)
  5010. return 0;
  5011. switch (cdclk) {
  5012. default:
  5013. MISSING_CASE(cdclk);
  5014. case 144000:
  5015. case 288000:
  5016. case 384000:
  5017. case 576000:
  5018. ratio = 60;
  5019. break;
  5020. case 624000:
  5021. ratio = 65;
  5022. break;
  5023. }
  5024. return dev_priv->cdclk_pll.ref * ratio;
  5025. }
  5026. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5027. {
  5028. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5029. /* Timeout 200us */
  5030. if (intel_wait_for_register(dev_priv,
  5031. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5032. 1))
  5033. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5034. dev_priv->cdclk_pll.vco = 0;
  5035. }
  5036. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5037. {
  5038. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5039. u32 val;
  5040. val = I915_READ(BXT_DE_PLL_CTL);
  5041. val &= ~BXT_DE_PLL_RATIO_MASK;
  5042. val |= BXT_DE_PLL_RATIO(ratio);
  5043. I915_WRITE(BXT_DE_PLL_CTL, val);
  5044. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5045. /* Timeout 200us */
  5046. if (intel_wait_for_register(dev_priv,
  5047. BXT_DE_PLL_ENABLE,
  5048. BXT_DE_PLL_LOCK,
  5049. BXT_DE_PLL_LOCK,
  5050. 1))
  5051. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5052. dev_priv->cdclk_pll.vco = vco;
  5053. }
  5054. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5055. {
  5056. u32 val, divider;
  5057. int vco, ret;
  5058. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5059. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5060. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5061. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5062. case 8:
  5063. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5064. break;
  5065. case 4:
  5066. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5067. break;
  5068. case 3:
  5069. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5070. break;
  5071. case 2:
  5072. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5073. break;
  5074. default:
  5075. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5076. WARN_ON(vco != 0);
  5077. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5078. break;
  5079. }
  5080. /* Inform power controller of upcoming frequency change */
  5081. mutex_lock(&dev_priv->rps.hw_lock);
  5082. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5083. 0x80000000);
  5084. mutex_unlock(&dev_priv->rps.hw_lock);
  5085. if (ret) {
  5086. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5087. ret, cdclk);
  5088. return;
  5089. }
  5090. if (dev_priv->cdclk_pll.vco != 0 &&
  5091. dev_priv->cdclk_pll.vco != vco)
  5092. bxt_de_pll_disable(dev_priv);
  5093. if (dev_priv->cdclk_pll.vco != vco)
  5094. bxt_de_pll_enable(dev_priv, vco);
  5095. val = divider | skl_cdclk_decimal(cdclk);
  5096. /*
  5097. * FIXME if only the cd2x divider needs changing, it could be done
  5098. * without shutting off the pipe (if only one pipe is active).
  5099. */
  5100. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5101. /*
  5102. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5103. * enable otherwise.
  5104. */
  5105. if (cdclk >= 500000)
  5106. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5107. I915_WRITE(CDCLK_CTL, val);
  5108. mutex_lock(&dev_priv->rps.hw_lock);
  5109. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5110. DIV_ROUND_UP(cdclk, 25000));
  5111. mutex_unlock(&dev_priv->rps.hw_lock);
  5112. if (ret) {
  5113. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5114. ret, cdclk);
  5115. return;
  5116. }
  5117. intel_update_cdclk(dev_priv);
  5118. }
  5119. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5120. {
  5121. u32 cdctl, expected;
  5122. intel_update_cdclk(dev_priv);
  5123. if (dev_priv->cdclk_pll.vco == 0 ||
  5124. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5125. goto sanitize;
  5126. /* DPLL okay; verify the cdclock
  5127. *
  5128. * Some BIOS versions leave an incorrect decimal frequency value and
  5129. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5130. * so sanitize this register.
  5131. */
  5132. cdctl = I915_READ(CDCLK_CTL);
  5133. /*
  5134. * Let's ignore the pipe field, since BIOS could have configured the
  5135. * dividers both synching to an active pipe, or asynchronously
  5136. * (PIPE_NONE).
  5137. */
  5138. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5139. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5140. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5141. /*
  5142. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5143. * enable otherwise.
  5144. */
  5145. if (dev_priv->cdclk_freq >= 500000)
  5146. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5147. if (cdctl == expected)
  5148. /* All well; nothing to sanitize */
  5149. return;
  5150. sanitize:
  5151. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5152. /* force cdclk programming */
  5153. dev_priv->cdclk_freq = 0;
  5154. /* force full PLL disable + enable */
  5155. dev_priv->cdclk_pll.vco = -1;
  5156. }
  5157. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5158. {
  5159. bxt_sanitize_cdclk(dev_priv);
  5160. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5161. return;
  5162. /*
  5163. * FIXME:
  5164. * - The initial CDCLK needs to be read from VBT.
  5165. * Need to make this change after VBT has changes for BXT.
  5166. */
  5167. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  5168. }
  5169. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5170. {
  5171. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5172. }
  5173. static int skl_calc_cdclk(int max_pixclk, int vco)
  5174. {
  5175. if (vco == 8640000) {
  5176. if (max_pixclk > 540000)
  5177. return 617143;
  5178. else if (max_pixclk > 432000)
  5179. return 540000;
  5180. else if (max_pixclk > 308571)
  5181. return 432000;
  5182. else
  5183. return 308571;
  5184. } else {
  5185. if (max_pixclk > 540000)
  5186. return 675000;
  5187. else if (max_pixclk > 450000)
  5188. return 540000;
  5189. else if (max_pixclk > 337500)
  5190. return 450000;
  5191. else
  5192. return 337500;
  5193. }
  5194. }
  5195. static void
  5196. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5197. {
  5198. u32 val;
  5199. dev_priv->cdclk_pll.ref = 24000;
  5200. dev_priv->cdclk_pll.vco = 0;
  5201. val = I915_READ(LCPLL1_CTL);
  5202. if ((val & LCPLL_PLL_ENABLE) == 0)
  5203. return;
  5204. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5205. return;
  5206. val = I915_READ(DPLL_CTRL1);
  5207. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5208. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5209. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5210. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5211. return;
  5212. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5213. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5214. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5215. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5216. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5217. dev_priv->cdclk_pll.vco = 8100000;
  5218. break;
  5219. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5220. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5221. dev_priv->cdclk_pll.vco = 8640000;
  5222. break;
  5223. default:
  5224. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5225. break;
  5226. }
  5227. }
  5228. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5229. {
  5230. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5231. dev_priv->skl_preferred_vco_freq = vco;
  5232. if (changed)
  5233. intel_update_max_cdclk(dev_priv);
  5234. }
  5235. static void
  5236. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5237. {
  5238. int min_cdclk = skl_calc_cdclk(0, vco);
  5239. u32 val;
  5240. WARN_ON(vco != 8100000 && vco != 8640000);
  5241. /* select the minimum CDCLK before enabling DPLL 0 */
  5242. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5243. I915_WRITE(CDCLK_CTL, val);
  5244. POSTING_READ(CDCLK_CTL);
  5245. /*
  5246. * We always enable DPLL0 with the lowest link rate possible, but still
  5247. * taking into account the VCO required to operate the eDP panel at the
  5248. * desired frequency. The usual DP link rates operate with a VCO of
  5249. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5250. * The modeset code is responsible for the selection of the exact link
  5251. * rate later on, with the constraint of choosing a frequency that
  5252. * works with vco.
  5253. */
  5254. val = I915_READ(DPLL_CTRL1);
  5255. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5256. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5257. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5258. if (vco == 8640000)
  5259. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5260. SKL_DPLL0);
  5261. else
  5262. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5263. SKL_DPLL0);
  5264. I915_WRITE(DPLL_CTRL1, val);
  5265. POSTING_READ(DPLL_CTRL1);
  5266. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5267. if (intel_wait_for_register(dev_priv,
  5268. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5269. 5))
  5270. DRM_ERROR("DPLL0 not locked\n");
  5271. dev_priv->cdclk_pll.vco = vco;
  5272. /* We'll want to keep using the current vco from now on. */
  5273. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5274. }
  5275. static void
  5276. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5277. {
  5278. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5279. if (intel_wait_for_register(dev_priv,
  5280. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5281. 1))
  5282. DRM_ERROR("Couldn't disable DPLL0\n");
  5283. dev_priv->cdclk_pll.vco = 0;
  5284. }
  5285. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  5286. {
  5287. int ret;
  5288. u32 val;
  5289. /* inform PCU we want to change CDCLK */
  5290. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  5291. mutex_lock(&dev_priv->rps.hw_lock);
  5292. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  5293. mutex_unlock(&dev_priv->rps.hw_lock);
  5294. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  5295. }
  5296. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  5297. {
  5298. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  5299. }
  5300. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5301. {
  5302. u32 freq_select, pcu_ack;
  5303. WARN_ON((cdclk == 24000) != (vco == 0));
  5304. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5305. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  5306. DRM_ERROR("failed to inform PCU about cdclk change\n");
  5307. return;
  5308. }
  5309. /* set CDCLK_CTL */
  5310. switch (cdclk) {
  5311. case 450000:
  5312. case 432000:
  5313. freq_select = CDCLK_FREQ_450_432;
  5314. pcu_ack = 1;
  5315. break;
  5316. case 540000:
  5317. freq_select = CDCLK_FREQ_540;
  5318. pcu_ack = 2;
  5319. break;
  5320. case 308571:
  5321. case 337500:
  5322. default:
  5323. freq_select = CDCLK_FREQ_337_308;
  5324. pcu_ack = 0;
  5325. break;
  5326. case 617143:
  5327. case 675000:
  5328. freq_select = CDCLK_FREQ_675_617;
  5329. pcu_ack = 3;
  5330. break;
  5331. }
  5332. if (dev_priv->cdclk_pll.vco != 0 &&
  5333. dev_priv->cdclk_pll.vco != vco)
  5334. skl_dpll0_disable(dev_priv);
  5335. if (dev_priv->cdclk_pll.vco != vco)
  5336. skl_dpll0_enable(dev_priv, vco);
  5337. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5338. POSTING_READ(CDCLK_CTL);
  5339. /* inform PCU of the change */
  5340. mutex_lock(&dev_priv->rps.hw_lock);
  5341. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5342. mutex_unlock(&dev_priv->rps.hw_lock);
  5343. intel_update_cdclk(dev_priv);
  5344. }
  5345. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5346. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5347. {
  5348. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5349. }
  5350. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5351. {
  5352. int cdclk, vco;
  5353. skl_sanitize_cdclk(dev_priv);
  5354. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5355. /*
  5356. * Use the current vco as our initial
  5357. * guess as to what the preferred vco is.
  5358. */
  5359. if (dev_priv->skl_preferred_vco_freq == 0)
  5360. skl_set_preferred_cdclk_vco(dev_priv,
  5361. dev_priv->cdclk_pll.vco);
  5362. return;
  5363. }
  5364. vco = dev_priv->skl_preferred_vco_freq;
  5365. if (vco == 0)
  5366. vco = 8100000;
  5367. cdclk = skl_calc_cdclk(0, vco);
  5368. skl_set_cdclk(dev_priv, cdclk, vco);
  5369. }
  5370. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5371. {
  5372. uint32_t cdctl, expected;
  5373. /*
  5374. * check if the pre-os intialized the display
  5375. * There is SWF18 scratchpad register defined which is set by the
  5376. * pre-os which can be used by the OS drivers to check the status
  5377. */
  5378. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5379. goto sanitize;
  5380. intel_update_cdclk(dev_priv);
  5381. /* Is PLL enabled and locked ? */
  5382. if (dev_priv->cdclk_pll.vco == 0 ||
  5383. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5384. goto sanitize;
  5385. /* DPLL okay; verify the cdclock
  5386. *
  5387. * Noticed in some instances that the freq selection is correct but
  5388. * decimal part is programmed wrong from BIOS where pre-os does not
  5389. * enable display. Verify the same as well.
  5390. */
  5391. cdctl = I915_READ(CDCLK_CTL);
  5392. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5393. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5394. if (cdctl == expected)
  5395. /* All well; nothing to sanitize */
  5396. return;
  5397. sanitize:
  5398. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5399. /* force cdclk programming */
  5400. dev_priv->cdclk_freq = 0;
  5401. /* force full PLL disable + enable */
  5402. dev_priv->cdclk_pll.vco = -1;
  5403. }
  5404. /* Adjust CDclk dividers to allow high res or save power if possible */
  5405. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5406. {
  5407. struct drm_i915_private *dev_priv = to_i915(dev);
  5408. u32 val, cmd;
  5409. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5410. != dev_priv->cdclk_freq);
  5411. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5412. cmd = 2;
  5413. else if (cdclk == 266667)
  5414. cmd = 1;
  5415. else
  5416. cmd = 0;
  5417. mutex_lock(&dev_priv->rps.hw_lock);
  5418. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5419. val &= ~DSPFREQGUAR_MASK;
  5420. val |= (cmd << DSPFREQGUAR_SHIFT);
  5421. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5422. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5423. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5424. 50)) {
  5425. DRM_ERROR("timed out waiting for CDclk change\n");
  5426. }
  5427. mutex_unlock(&dev_priv->rps.hw_lock);
  5428. mutex_lock(&dev_priv->sb_lock);
  5429. if (cdclk == 400000) {
  5430. u32 divider;
  5431. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5432. /* adjust cdclk divider */
  5433. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5434. val &= ~CCK_FREQUENCY_VALUES;
  5435. val |= divider;
  5436. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5437. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5438. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5439. 50))
  5440. DRM_ERROR("timed out waiting for CDclk change\n");
  5441. }
  5442. /* adjust self-refresh exit latency value */
  5443. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5444. val &= ~0x7f;
  5445. /*
  5446. * For high bandwidth configs, we set a higher latency in the bunit
  5447. * so that the core display fetch happens in time to avoid underruns.
  5448. */
  5449. if (cdclk == 400000)
  5450. val |= 4500 / 250; /* 4.5 usec */
  5451. else
  5452. val |= 3000 / 250; /* 3.0 usec */
  5453. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5454. mutex_unlock(&dev_priv->sb_lock);
  5455. intel_update_cdclk(dev_priv);
  5456. }
  5457. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5458. {
  5459. struct drm_i915_private *dev_priv = to_i915(dev);
  5460. u32 val, cmd;
  5461. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5462. != dev_priv->cdclk_freq);
  5463. switch (cdclk) {
  5464. case 333333:
  5465. case 320000:
  5466. case 266667:
  5467. case 200000:
  5468. break;
  5469. default:
  5470. MISSING_CASE(cdclk);
  5471. return;
  5472. }
  5473. /*
  5474. * Specs are full of misinformation, but testing on actual
  5475. * hardware has shown that we just need to write the desired
  5476. * CCK divider into the Punit register.
  5477. */
  5478. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5479. mutex_lock(&dev_priv->rps.hw_lock);
  5480. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5481. val &= ~DSPFREQGUAR_MASK_CHV;
  5482. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5483. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5484. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5485. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5486. 50)) {
  5487. DRM_ERROR("timed out waiting for CDclk change\n");
  5488. }
  5489. mutex_unlock(&dev_priv->rps.hw_lock);
  5490. intel_update_cdclk(dev_priv);
  5491. }
  5492. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5493. int max_pixclk)
  5494. {
  5495. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5496. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5497. /*
  5498. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5499. * 200MHz
  5500. * 267MHz
  5501. * 320/333MHz (depends on HPLL freq)
  5502. * 400MHz (VLV only)
  5503. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5504. * of the lower bin and adjust if needed.
  5505. *
  5506. * We seem to get an unstable or solid color picture at 200MHz.
  5507. * Not sure what's wrong. For now use 200MHz only when all pipes
  5508. * are off.
  5509. */
  5510. if (!IS_CHERRYVIEW(dev_priv) &&
  5511. max_pixclk > freq_320*limit/100)
  5512. return 400000;
  5513. else if (max_pixclk > 266667*limit/100)
  5514. return freq_320;
  5515. else if (max_pixclk > 0)
  5516. return 266667;
  5517. else
  5518. return 200000;
  5519. }
  5520. static int bxt_calc_cdclk(int max_pixclk)
  5521. {
  5522. if (max_pixclk > 576000)
  5523. return 624000;
  5524. else if (max_pixclk > 384000)
  5525. return 576000;
  5526. else if (max_pixclk > 288000)
  5527. return 384000;
  5528. else if (max_pixclk > 144000)
  5529. return 288000;
  5530. else
  5531. return 144000;
  5532. }
  5533. /* Compute the max pixel clock for new configuration. */
  5534. static int intel_mode_max_pixclk(struct drm_device *dev,
  5535. struct drm_atomic_state *state)
  5536. {
  5537. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5538. struct drm_i915_private *dev_priv = to_i915(dev);
  5539. struct drm_crtc *crtc;
  5540. struct drm_crtc_state *crtc_state;
  5541. unsigned max_pixclk = 0, i;
  5542. enum pipe pipe;
  5543. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5544. sizeof(intel_state->min_pixclk));
  5545. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5546. int pixclk = 0;
  5547. if (crtc_state->enable)
  5548. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5549. intel_state->min_pixclk[i] = pixclk;
  5550. }
  5551. for_each_pipe(dev_priv, pipe)
  5552. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5553. return max_pixclk;
  5554. }
  5555. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5556. {
  5557. struct drm_device *dev = state->dev;
  5558. struct drm_i915_private *dev_priv = to_i915(dev);
  5559. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5560. struct intel_atomic_state *intel_state =
  5561. to_intel_atomic_state(state);
  5562. intel_state->cdclk = intel_state->dev_cdclk =
  5563. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5564. if (!intel_state->active_crtcs)
  5565. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5566. return 0;
  5567. }
  5568. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5569. {
  5570. int max_pixclk = ilk_max_pixel_rate(state);
  5571. struct intel_atomic_state *intel_state =
  5572. to_intel_atomic_state(state);
  5573. intel_state->cdclk = intel_state->dev_cdclk =
  5574. bxt_calc_cdclk(max_pixclk);
  5575. if (!intel_state->active_crtcs)
  5576. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5577. return 0;
  5578. }
  5579. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5580. {
  5581. unsigned int credits, default_credits;
  5582. if (IS_CHERRYVIEW(dev_priv))
  5583. default_credits = PFI_CREDIT(12);
  5584. else
  5585. default_credits = PFI_CREDIT(8);
  5586. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5587. /* CHV suggested value is 31 or 63 */
  5588. if (IS_CHERRYVIEW(dev_priv))
  5589. credits = PFI_CREDIT_63;
  5590. else
  5591. credits = PFI_CREDIT(15);
  5592. } else {
  5593. credits = default_credits;
  5594. }
  5595. /*
  5596. * WA - write default credits before re-programming
  5597. * FIXME: should we also set the resend bit here?
  5598. */
  5599. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5600. default_credits);
  5601. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5602. credits | PFI_CREDIT_RESEND);
  5603. /*
  5604. * FIXME is this guaranteed to clear
  5605. * immediately or should we poll for it?
  5606. */
  5607. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5608. }
  5609. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5610. {
  5611. struct drm_device *dev = old_state->dev;
  5612. struct drm_i915_private *dev_priv = to_i915(dev);
  5613. struct intel_atomic_state *old_intel_state =
  5614. to_intel_atomic_state(old_state);
  5615. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5616. /*
  5617. * FIXME: We can end up here with all power domains off, yet
  5618. * with a CDCLK frequency other than the minimum. To account
  5619. * for this take the PIPE-A power domain, which covers the HW
  5620. * blocks needed for the following programming. This can be
  5621. * removed once it's guaranteed that we get here either with
  5622. * the minimum CDCLK set, or the required power domains
  5623. * enabled.
  5624. */
  5625. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5626. if (IS_CHERRYVIEW(dev_priv))
  5627. cherryview_set_cdclk(dev, req_cdclk);
  5628. else
  5629. valleyview_set_cdclk(dev, req_cdclk);
  5630. vlv_program_pfi_credits(dev_priv);
  5631. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5632. }
  5633. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5634. struct drm_atomic_state *old_state)
  5635. {
  5636. struct drm_crtc *crtc = pipe_config->base.crtc;
  5637. struct drm_device *dev = crtc->dev;
  5638. struct drm_i915_private *dev_priv = to_i915(dev);
  5639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5640. int pipe = intel_crtc->pipe;
  5641. if (WARN_ON(intel_crtc->active))
  5642. return;
  5643. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5644. intel_dp_set_m_n(intel_crtc, M1_N1);
  5645. intel_set_pipe_timings(intel_crtc);
  5646. intel_set_pipe_src_size(intel_crtc);
  5647. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5648. struct drm_i915_private *dev_priv = to_i915(dev);
  5649. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5650. I915_WRITE(CHV_CANVAS(pipe), 0);
  5651. }
  5652. i9xx_set_pipeconf(intel_crtc);
  5653. intel_crtc->active = true;
  5654. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5655. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5656. if (IS_CHERRYVIEW(dev_priv)) {
  5657. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5658. chv_enable_pll(intel_crtc, intel_crtc->config);
  5659. } else {
  5660. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5661. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5662. }
  5663. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5664. i9xx_pfit_enable(intel_crtc);
  5665. intel_color_load_luts(&pipe_config->base);
  5666. intel_update_watermarks(intel_crtc);
  5667. intel_enable_pipe(intel_crtc);
  5668. assert_vblank_disabled(crtc);
  5669. drm_crtc_vblank_on(crtc);
  5670. intel_encoders_enable(crtc, pipe_config, old_state);
  5671. }
  5672. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5673. {
  5674. struct drm_device *dev = crtc->base.dev;
  5675. struct drm_i915_private *dev_priv = to_i915(dev);
  5676. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5677. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5678. }
  5679. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5680. struct drm_atomic_state *old_state)
  5681. {
  5682. struct drm_crtc *crtc = pipe_config->base.crtc;
  5683. struct drm_device *dev = crtc->dev;
  5684. struct drm_i915_private *dev_priv = to_i915(dev);
  5685. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5686. enum pipe pipe = intel_crtc->pipe;
  5687. if (WARN_ON(intel_crtc->active))
  5688. return;
  5689. i9xx_set_pll_dividers(intel_crtc);
  5690. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5691. intel_dp_set_m_n(intel_crtc, M1_N1);
  5692. intel_set_pipe_timings(intel_crtc);
  5693. intel_set_pipe_src_size(intel_crtc);
  5694. i9xx_set_pipeconf(intel_crtc);
  5695. intel_crtc->active = true;
  5696. if (!IS_GEN2(dev_priv))
  5697. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5698. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5699. i9xx_enable_pll(intel_crtc);
  5700. i9xx_pfit_enable(intel_crtc);
  5701. intel_color_load_luts(&pipe_config->base);
  5702. intel_update_watermarks(intel_crtc);
  5703. intel_enable_pipe(intel_crtc);
  5704. assert_vblank_disabled(crtc);
  5705. drm_crtc_vblank_on(crtc);
  5706. intel_encoders_enable(crtc, pipe_config, old_state);
  5707. }
  5708. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5709. {
  5710. struct drm_device *dev = crtc->base.dev;
  5711. struct drm_i915_private *dev_priv = to_i915(dev);
  5712. if (!crtc->config->gmch_pfit.control)
  5713. return;
  5714. assert_pipe_disabled(dev_priv, crtc->pipe);
  5715. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5716. I915_READ(PFIT_CONTROL));
  5717. I915_WRITE(PFIT_CONTROL, 0);
  5718. }
  5719. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5720. struct drm_atomic_state *old_state)
  5721. {
  5722. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5723. struct drm_device *dev = crtc->dev;
  5724. struct drm_i915_private *dev_priv = to_i915(dev);
  5725. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5726. int pipe = intel_crtc->pipe;
  5727. /*
  5728. * On gen2 planes are double buffered but the pipe isn't, so we must
  5729. * wait for planes to fully turn off before disabling the pipe.
  5730. */
  5731. if (IS_GEN2(dev_priv))
  5732. intel_wait_for_vblank(dev_priv, pipe);
  5733. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5734. drm_crtc_vblank_off(crtc);
  5735. assert_vblank_disabled(crtc);
  5736. intel_disable_pipe(intel_crtc);
  5737. i9xx_pfit_disable(intel_crtc);
  5738. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5739. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5740. if (IS_CHERRYVIEW(dev_priv))
  5741. chv_disable_pll(dev_priv, pipe);
  5742. else if (IS_VALLEYVIEW(dev_priv))
  5743. vlv_disable_pll(dev_priv, pipe);
  5744. else
  5745. i9xx_disable_pll(intel_crtc);
  5746. }
  5747. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5748. if (!IS_GEN2(dev_priv))
  5749. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5750. }
  5751. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5752. {
  5753. struct intel_encoder *encoder;
  5754. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5755. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5756. enum intel_display_power_domain domain;
  5757. unsigned long domains;
  5758. struct drm_atomic_state *state;
  5759. struct intel_crtc_state *crtc_state;
  5760. int ret;
  5761. if (!intel_crtc->active)
  5762. return;
  5763. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5764. WARN_ON(intel_crtc->flip_work);
  5765. intel_pre_disable_primary_noatomic(crtc);
  5766. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5767. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5768. }
  5769. state = drm_atomic_state_alloc(crtc->dev);
  5770. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5771. /* Everything's already locked, -EDEADLK can't happen. */
  5772. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5773. ret = drm_atomic_add_affected_connectors(state, crtc);
  5774. WARN_ON(IS_ERR(crtc_state) || ret);
  5775. dev_priv->display.crtc_disable(crtc_state, state);
  5776. drm_atomic_state_put(state);
  5777. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5778. crtc->base.id, crtc->name);
  5779. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5780. crtc->state->active = false;
  5781. intel_crtc->active = false;
  5782. crtc->enabled = false;
  5783. crtc->state->connector_mask = 0;
  5784. crtc->state->encoder_mask = 0;
  5785. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5786. encoder->base.crtc = NULL;
  5787. intel_fbc_disable(intel_crtc);
  5788. intel_update_watermarks(intel_crtc);
  5789. intel_disable_shared_dpll(intel_crtc);
  5790. domains = intel_crtc->enabled_power_domains;
  5791. for_each_power_domain(domain, domains)
  5792. intel_display_power_put(dev_priv, domain);
  5793. intel_crtc->enabled_power_domains = 0;
  5794. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5795. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5796. }
  5797. /*
  5798. * turn all crtc's off, but do not adjust state
  5799. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5800. */
  5801. int intel_display_suspend(struct drm_device *dev)
  5802. {
  5803. struct drm_i915_private *dev_priv = to_i915(dev);
  5804. struct drm_atomic_state *state;
  5805. int ret;
  5806. state = drm_atomic_helper_suspend(dev);
  5807. ret = PTR_ERR_OR_ZERO(state);
  5808. if (ret)
  5809. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5810. else
  5811. dev_priv->modeset_restore_state = state;
  5812. return ret;
  5813. }
  5814. void intel_encoder_destroy(struct drm_encoder *encoder)
  5815. {
  5816. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5817. drm_encoder_cleanup(encoder);
  5818. kfree(intel_encoder);
  5819. }
  5820. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5821. * internal consistency). */
  5822. static void intel_connector_verify_state(struct intel_connector *connector)
  5823. {
  5824. struct drm_crtc *crtc = connector->base.state->crtc;
  5825. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5826. connector->base.base.id,
  5827. connector->base.name);
  5828. if (connector->get_hw_state(connector)) {
  5829. struct intel_encoder *encoder = connector->encoder;
  5830. struct drm_connector_state *conn_state = connector->base.state;
  5831. I915_STATE_WARN(!crtc,
  5832. "connector enabled without attached crtc\n");
  5833. if (!crtc)
  5834. return;
  5835. I915_STATE_WARN(!crtc->state->active,
  5836. "connector is active, but attached crtc isn't\n");
  5837. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5838. return;
  5839. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5840. "atomic encoder doesn't match attached encoder\n");
  5841. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5842. "attached encoder crtc differs from connector crtc\n");
  5843. } else {
  5844. I915_STATE_WARN(crtc && crtc->state->active,
  5845. "attached crtc is active, but connector isn't\n");
  5846. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5847. "best encoder set without crtc!\n");
  5848. }
  5849. }
  5850. int intel_connector_init(struct intel_connector *connector)
  5851. {
  5852. drm_atomic_helper_connector_reset(&connector->base);
  5853. if (!connector->base.state)
  5854. return -ENOMEM;
  5855. return 0;
  5856. }
  5857. struct intel_connector *intel_connector_alloc(void)
  5858. {
  5859. struct intel_connector *connector;
  5860. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5861. if (!connector)
  5862. return NULL;
  5863. if (intel_connector_init(connector) < 0) {
  5864. kfree(connector);
  5865. return NULL;
  5866. }
  5867. return connector;
  5868. }
  5869. /* Simple connector->get_hw_state implementation for encoders that support only
  5870. * one connector and no cloning and hence the encoder state determines the state
  5871. * of the connector. */
  5872. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5873. {
  5874. enum pipe pipe = 0;
  5875. struct intel_encoder *encoder = connector->encoder;
  5876. return encoder->get_hw_state(encoder, &pipe);
  5877. }
  5878. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5879. {
  5880. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5881. return crtc_state->fdi_lanes;
  5882. return 0;
  5883. }
  5884. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5885. struct intel_crtc_state *pipe_config)
  5886. {
  5887. struct drm_i915_private *dev_priv = to_i915(dev);
  5888. struct drm_atomic_state *state = pipe_config->base.state;
  5889. struct intel_crtc *other_crtc;
  5890. struct intel_crtc_state *other_crtc_state;
  5891. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5892. pipe_name(pipe), pipe_config->fdi_lanes);
  5893. if (pipe_config->fdi_lanes > 4) {
  5894. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5895. pipe_name(pipe), pipe_config->fdi_lanes);
  5896. return -EINVAL;
  5897. }
  5898. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5899. if (pipe_config->fdi_lanes > 2) {
  5900. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5901. pipe_config->fdi_lanes);
  5902. return -EINVAL;
  5903. } else {
  5904. return 0;
  5905. }
  5906. }
  5907. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5908. return 0;
  5909. /* Ivybridge 3 pipe is really complicated */
  5910. switch (pipe) {
  5911. case PIPE_A:
  5912. return 0;
  5913. case PIPE_B:
  5914. if (pipe_config->fdi_lanes <= 2)
  5915. return 0;
  5916. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5917. other_crtc_state =
  5918. intel_atomic_get_crtc_state(state, other_crtc);
  5919. if (IS_ERR(other_crtc_state))
  5920. return PTR_ERR(other_crtc_state);
  5921. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5922. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5923. pipe_name(pipe), pipe_config->fdi_lanes);
  5924. return -EINVAL;
  5925. }
  5926. return 0;
  5927. case PIPE_C:
  5928. if (pipe_config->fdi_lanes > 2) {
  5929. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5930. pipe_name(pipe), pipe_config->fdi_lanes);
  5931. return -EINVAL;
  5932. }
  5933. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5934. other_crtc_state =
  5935. intel_atomic_get_crtc_state(state, other_crtc);
  5936. if (IS_ERR(other_crtc_state))
  5937. return PTR_ERR(other_crtc_state);
  5938. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5939. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5940. return -EINVAL;
  5941. }
  5942. return 0;
  5943. default:
  5944. BUG();
  5945. }
  5946. }
  5947. #define RETRY 1
  5948. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5949. struct intel_crtc_state *pipe_config)
  5950. {
  5951. struct drm_device *dev = intel_crtc->base.dev;
  5952. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5953. int lane, link_bw, fdi_dotclock, ret;
  5954. bool needs_recompute = false;
  5955. retry:
  5956. /* FDI is a binary signal running at ~2.7GHz, encoding
  5957. * each output octet as 10 bits. The actual frequency
  5958. * is stored as a divider into a 100MHz clock, and the
  5959. * mode pixel clock is stored in units of 1KHz.
  5960. * Hence the bw of each lane in terms of the mode signal
  5961. * is:
  5962. */
  5963. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5964. fdi_dotclock = adjusted_mode->crtc_clock;
  5965. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5966. pipe_config->pipe_bpp);
  5967. pipe_config->fdi_lanes = lane;
  5968. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5969. link_bw, &pipe_config->fdi_m_n);
  5970. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5971. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5972. pipe_config->pipe_bpp -= 2*3;
  5973. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5974. pipe_config->pipe_bpp);
  5975. needs_recompute = true;
  5976. pipe_config->bw_constrained = true;
  5977. goto retry;
  5978. }
  5979. if (needs_recompute)
  5980. return RETRY;
  5981. return ret;
  5982. }
  5983. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5984. struct intel_crtc_state *pipe_config)
  5985. {
  5986. if (pipe_config->pipe_bpp > 24)
  5987. return false;
  5988. /* HSW can handle pixel rate up to cdclk? */
  5989. if (IS_HASWELL(dev_priv))
  5990. return true;
  5991. /*
  5992. * We compare against max which means we must take
  5993. * the increased cdclk requirement into account when
  5994. * calculating the new cdclk.
  5995. *
  5996. * Should measure whether using a lower cdclk w/o IPS
  5997. */
  5998. return ilk_pipe_pixel_rate(pipe_config) <=
  5999. dev_priv->max_cdclk_freq * 95 / 100;
  6000. }
  6001. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6002. struct intel_crtc_state *pipe_config)
  6003. {
  6004. struct drm_device *dev = crtc->base.dev;
  6005. struct drm_i915_private *dev_priv = to_i915(dev);
  6006. pipe_config->ips_enabled = i915.enable_ips &&
  6007. hsw_crtc_supports_ips(crtc) &&
  6008. pipe_config_supports_ips(dev_priv, pipe_config);
  6009. }
  6010. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6011. {
  6012. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6013. /* GDG double wide on either pipe, otherwise pipe A only */
  6014. return INTEL_INFO(dev_priv)->gen < 4 &&
  6015. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6016. }
  6017. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6018. struct intel_crtc_state *pipe_config)
  6019. {
  6020. struct drm_device *dev = crtc->base.dev;
  6021. struct drm_i915_private *dev_priv = to_i915(dev);
  6022. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6023. int clock_limit = dev_priv->max_dotclk_freq;
  6024. if (INTEL_GEN(dev_priv) < 4) {
  6025. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6026. /*
  6027. * Enable double wide mode when the dot clock
  6028. * is > 90% of the (display) core speed.
  6029. */
  6030. if (intel_crtc_supports_double_wide(crtc) &&
  6031. adjusted_mode->crtc_clock > clock_limit) {
  6032. clock_limit = dev_priv->max_dotclk_freq;
  6033. pipe_config->double_wide = true;
  6034. }
  6035. }
  6036. if (adjusted_mode->crtc_clock > clock_limit) {
  6037. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6038. adjusted_mode->crtc_clock, clock_limit,
  6039. yesno(pipe_config->double_wide));
  6040. return -EINVAL;
  6041. }
  6042. /*
  6043. * Pipe horizontal size must be even in:
  6044. * - DVO ganged mode
  6045. * - LVDS dual channel mode
  6046. * - Double wide pipe
  6047. */
  6048. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6049. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6050. pipe_config->pipe_src_w &= ~1;
  6051. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6052. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6053. */
  6054. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6055. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6056. return -EINVAL;
  6057. if (HAS_IPS(dev_priv))
  6058. hsw_compute_ips_config(crtc, pipe_config);
  6059. if (pipe_config->has_pch_encoder)
  6060. return ironlake_fdi_compute_config(crtc, pipe_config);
  6061. return 0;
  6062. }
  6063. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6064. {
  6065. u32 cdctl;
  6066. skl_dpll0_update(dev_priv);
  6067. if (dev_priv->cdclk_pll.vco == 0)
  6068. return dev_priv->cdclk_pll.ref;
  6069. cdctl = I915_READ(CDCLK_CTL);
  6070. if (dev_priv->cdclk_pll.vco == 8640000) {
  6071. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6072. case CDCLK_FREQ_450_432:
  6073. return 432000;
  6074. case CDCLK_FREQ_337_308:
  6075. return 308571;
  6076. case CDCLK_FREQ_540:
  6077. return 540000;
  6078. case CDCLK_FREQ_675_617:
  6079. return 617143;
  6080. default:
  6081. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6082. }
  6083. } else {
  6084. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6085. case CDCLK_FREQ_450_432:
  6086. return 450000;
  6087. case CDCLK_FREQ_337_308:
  6088. return 337500;
  6089. case CDCLK_FREQ_540:
  6090. return 540000;
  6091. case CDCLK_FREQ_675_617:
  6092. return 675000;
  6093. default:
  6094. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6095. }
  6096. }
  6097. return dev_priv->cdclk_pll.ref;
  6098. }
  6099. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6100. {
  6101. u32 val;
  6102. dev_priv->cdclk_pll.ref = 19200;
  6103. dev_priv->cdclk_pll.vco = 0;
  6104. val = I915_READ(BXT_DE_PLL_ENABLE);
  6105. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6106. return;
  6107. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6108. return;
  6109. val = I915_READ(BXT_DE_PLL_CTL);
  6110. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6111. dev_priv->cdclk_pll.ref;
  6112. }
  6113. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6114. {
  6115. u32 divider;
  6116. int div, vco;
  6117. bxt_de_pll_update(dev_priv);
  6118. vco = dev_priv->cdclk_pll.vco;
  6119. if (vco == 0)
  6120. return dev_priv->cdclk_pll.ref;
  6121. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6122. switch (divider) {
  6123. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6124. div = 2;
  6125. break;
  6126. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6127. div = 3;
  6128. break;
  6129. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6130. div = 4;
  6131. break;
  6132. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6133. div = 8;
  6134. break;
  6135. default:
  6136. MISSING_CASE(divider);
  6137. return dev_priv->cdclk_pll.ref;
  6138. }
  6139. return DIV_ROUND_CLOSEST(vco, div);
  6140. }
  6141. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6142. {
  6143. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6144. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6145. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6146. return 800000;
  6147. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6148. return 450000;
  6149. else if (freq == LCPLL_CLK_FREQ_450)
  6150. return 450000;
  6151. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6152. return 540000;
  6153. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6154. return 337500;
  6155. else
  6156. return 675000;
  6157. }
  6158. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6159. {
  6160. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6161. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6162. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6163. return 800000;
  6164. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6165. return 450000;
  6166. else if (freq == LCPLL_CLK_FREQ_450)
  6167. return 450000;
  6168. else if (IS_HSW_ULT(dev_priv))
  6169. return 337500;
  6170. else
  6171. return 540000;
  6172. }
  6173. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6174. {
  6175. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6176. CCK_DISPLAY_CLOCK_CONTROL);
  6177. }
  6178. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6179. {
  6180. return 450000;
  6181. }
  6182. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6183. {
  6184. return 400000;
  6185. }
  6186. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6187. {
  6188. return 333333;
  6189. }
  6190. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6191. {
  6192. return 200000;
  6193. }
  6194. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6195. {
  6196. struct pci_dev *pdev = dev_priv->drm.pdev;
  6197. u16 gcfgc = 0;
  6198. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6199. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6200. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6201. return 266667;
  6202. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6203. return 333333;
  6204. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6205. return 444444;
  6206. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6207. return 200000;
  6208. default:
  6209. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6210. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6211. return 133333;
  6212. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6213. return 166667;
  6214. }
  6215. }
  6216. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6217. {
  6218. struct pci_dev *pdev = dev_priv->drm.pdev;
  6219. u16 gcfgc = 0;
  6220. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6221. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6222. return 133333;
  6223. else {
  6224. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6225. case GC_DISPLAY_CLOCK_333_MHZ:
  6226. return 333333;
  6227. default:
  6228. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6229. return 190000;
  6230. }
  6231. }
  6232. }
  6233. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6234. {
  6235. return 266667;
  6236. }
  6237. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6238. {
  6239. struct pci_dev *pdev = dev_priv->drm.pdev;
  6240. u16 hpllcc = 0;
  6241. /*
  6242. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6243. * encoding is different :(
  6244. * FIXME is this the right way to detect 852GM/852GMV?
  6245. */
  6246. if (pdev->revision == 0x1)
  6247. return 133333;
  6248. pci_bus_read_config_word(pdev->bus,
  6249. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6250. /* Assume that the hardware is in the high speed state. This
  6251. * should be the default.
  6252. */
  6253. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6254. case GC_CLOCK_133_200:
  6255. case GC_CLOCK_133_200_2:
  6256. case GC_CLOCK_100_200:
  6257. return 200000;
  6258. case GC_CLOCK_166_250:
  6259. return 250000;
  6260. case GC_CLOCK_100_133:
  6261. return 133333;
  6262. case GC_CLOCK_133_266:
  6263. case GC_CLOCK_133_266_2:
  6264. case GC_CLOCK_166_266:
  6265. return 266667;
  6266. }
  6267. /* Shouldn't happen */
  6268. return 0;
  6269. }
  6270. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6271. {
  6272. return 133333;
  6273. }
  6274. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6275. {
  6276. static const unsigned int blb_vco[8] = {
  6277. [0] = 3200000,
  6278. [1] = 4000000,
  6279. [2] = 5333333,
  6280. [3] = 4800000,
  6281. [4] = 6400000,
  6282. };
  6283. static const unsigned int pnv_vco[8] = {
  6284. [0] = 3200000,
  6285. [1] = 4000000,
  6286. [2] = 5333333,
  6287. [3] = 4800000,
  6288. [4] = 2666667,
  6289. };
  6290. static const unsigned int cl_vco[8] = {
  6291. [0] = 3200000,
  6292. [1] = 4000000,
  6293. [2] = 5333333,
  6294. [3] = 6400000,
  6295. [4] = 3333333,
  6296. [5] = 3566667,
  6297. [6] = 4266667,
  6298. };
  6299. static const unsigned int elk_vco[8] = {
  6300. [0] = 3200000,
  6301. [1] = 4000000,
  6302. [2] = 5333333,
  6303. [3] = 4800000,
  6304. };
  6305. static const unsigned int ctg_vco[8] = {
  6306. [0] = 3200000,
  6307. [1] = 4000000,
  6308. [2] = 5333333,
  6309. [3] = 6400000,
  6310. [4] = 2666667,
  6311. [5] = 4266667,
  6312. };
  6313. const unsigned int *vco_table;
  6314. unsigned int vco;
  6315. uint8_t tmp = 0;
  6316. /* FIXME other chipsets? */
  6317. if (IS_GM45(dev_priv))
  6318. vco_table = ctg_vco;
  6319. else if (IS_G4X(dev_priv))
  6320. vco_table = elk_vco;
  6321. else if (IS_CRESTLINE(dev_priv))
  6322. vco_table = cl_vco;
  6323. else if (IS_PINEVIEW(dev_priv))
  6324. vco_table = pnv_vco;
  6325. else if (IS_G33(dev_priv))
  6326. vco_table = blb_vco;
  6327. else
  6328. return 0;
  6329. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6330. vco = vco_table[tmp & 0x7];
  6331. if (vco == 0)
  6332. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6333. else
  6334. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6335. return vco;
  6336. }
  6337. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6338. {
  6339. struct pci_dev *pdev = dev_priv->drm.pdev;
  6340. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6341. uint16_t tmp = 0;
  6342. pci_read_config_word(pdev, GCFGC, &tmp);
  6343. cdclk_sel = (tmp >> 12) & 0x1;
  6344. switch (vco) {
  6345. case 2666667:
  6346. case 4000000:
  6347. case 5333333:
  6348. return cdclk_sel ? 333333 : 222222;
  6349. case 3200000:
  6350. return cdclk_sel ? 320000 : 228571;
  6351. default:
  6352. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6353. return 222222;
  6354. }
  6355. }
  6356. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6357. {
  6358. struct pci_dev *pdev = dev_priv->drm.pdev;
  6359. static const uint8_t div_3200[] = { 16, 10, 8 };
  6360. static const uint8_t div_4000[] = { 20, 12, 10 };
  6361. static const uint8_t div_5333[] = { 24, 16, 14 };
  6362. const uint8_t *div_table;
  6363. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6364. uint16_t tmp = 0;
  6365. pci_read_config_word(pdev, GCFGC, &tmp);
  6366. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6367. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6368. goto fail;
  6369. switch (vco) {
  6370. case 3200000:
  6371. div_table = div_3200;
  6372. break;
  6373. case 4000000:
  6374. div_table = div_4000;
  6375. break;
  6376. case 5333333:
  6377. div_table = div_5333;
  6378. break;
  6379. default:
  6380. goto fail;
  6381. }
  6382. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6383. fail:
  6384. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6385. return 200000;
  6386. }
  6387. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6388. {
  6389. struct pci_dev *pdev = dev_priv->drm.pdev;
  6390. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6391. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6392. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6393. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6394. const uint8_t *div_table;
  6395. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6396. uint16_t tmp = 0;
  6397. pci_read_config_word(pdev, GCFGC, &tmp);
  6398. cdclk_sel = (tmp >> 4) & 0x7;
  6399. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6400. goto fail;
  6401. switch (vco) {
  6402. case 3200000:
  6403. div_table = div_3200;
  6404. break;
  6405. case 4000000:
  6406. div_table = div_4000;
  6407. break;
  6408. case 4800000:
  6409. div_table = div_4800;
  6410. break;
  6411. case 5333333:
  6412. div_table = div_5333;
  6413. break;
  6414. default:
  6415. goto fail;
  6416. }
  6417. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6418. fail:
  6419. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6420. return 190476;
  6421. }
  6422. static void
  6423. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6424. {
  6425. while (*num > DATA_LINK_M_N_MASK ||
  6426. *den > DATA_LINK_M_N_MASK) {
  6427. *num >>= 1;
  6428. *den >>= 1;
  6429. }
  6430. }
  6431. static void compute_m_n(unsigned int m, unsigned int n,
  6432. uint32_t *ret_m, uint32_t *ret_n)
  6433. {
  6434. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6435. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6436. intel_reduce_m_n_ratio(ret_m, ret_n);
  6437. }
  6438. void
  6439. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6440. int pixel_clock, int link_clock,
  6441. struct intel_link_m_n *m_n)
  6442. {
  6443. m_n->tu = 64;
  6444. compute_m_n(bits_per_pixel * pixel_clock,
  6445. link_clock * nlanes * 8,
  6446. &m_n->gmch_m, &m_n->gmch_n);
  6447. compute_m_n(pixel_clock, link_clock,
  6448. &m_n->link_m, &m_n->link_n);
  6449. }
  6450. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6451. {
  6452. if (i915.panel_use_ssc >= 0)
  6453. return i915.panel_use_ssc != 0;
  6454. return dev_priv->vbt.lvds_use_ssc
  6455. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6456. }
  6457. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6458. {
  6459. return (1 << dpll->n) << 16 | dpll->m2;
  6460. }
  6461. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6462. {
  6463. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6464. }
  6465. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6466. struct intel_crtc_state *crtc_state,
  6467. struct dpll *reduced_clock)
  6468. {
  6469. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6470. u32 fp, fp2 = 0;
  6471. if (IS_PINEVIEW(dev_priv)) {
  6472. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6473. if (reduced_clock)
  6474. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6475. } else {
  6476. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6477. if (reduced_clock)
  6478. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6479. }
  6480. crtc_state->dpll_hw_state.fp0 = fp;
  6481. crtc->lowfreq_avail = false;
  6482. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6483. reduced_clock) {
  6484. crtc_state->dpll_hw_state.fp1 = fp2;
  6485. crtc->lowfreq_avail = true;
  6486. } else {
  6487. crtc_state->dpll_hw_state.fp1 = fp;
  6488. }
  6489. }
  6490. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6491. pipe)
  6492. {
  6493. u32 reg_val;
  6494. /*
  6495. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6496. * and set it to a reasonable value instead.
  6497. */
  6498. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6499. reg_val &= 0xffffff00;
  6500. reg_val |= 0x00000030;
  6501. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6502. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6503. reg_val &= 0x8cffffff;
  6504. reg_val = 0x8c000000;
  6505. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6506. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6507. reg_val &= 0xffffff00;
  6508. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6509. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6510. reg_val &= 0x00ffffff;
  6511. reg_val |= 0xb0000000;
  6512. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6513. }
  6514. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6515. struct intel_link_m_n *m_n)
  6516. {
  6517. struct drm_device *dev = crtc->base.dev;
  6518. struct drm_i915_private *dev_priv = to_i915(dev);
  6519. int pipe = crtc->pipe;
  6520. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6521. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6522. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6523. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6524. }
  6525. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6526. struct intel_link_m_n *m_n,
  6527. struct intel_link_m_n *m2_n2)
  6528. {
  6529. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6530. int pipe = crtc->pipe;
  6531. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6532. if (INTEL_GEN(dev_priv) >= 5) {
  6533. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6534. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6535. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6536. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6537. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6538. * for gen < 8) and if DRRS is supported (to make sure the
  6539. * registers are not unnecessarily accessed).
  6540. */
  6541. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6542. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6543. I915_WRITE(PIPE_DATA_M2(transcoder),
  6544. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6545. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6546. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6547. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6548. }
  6549. } else {
  6550. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6551. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6552. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6553. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6554. }
  6555. }
  6556. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6557. {
  6558. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6559. if (m_n == M1_N1) {
  6560. dp_m_n = &crtc->config->dp_m_n;
  6561. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6562. } else if (m_n == M2_N2) {
  6563. /*
  6564. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6565. * needs to be programmed into M1_N1.
  6566. */
  6567. dp_m_n = &crtc->config->dp_m2_n2;
  6568. } else {
  6569. DRM_ERROR("Unsupported divider value\n");
  6570. return;
  6571. }
  6572. if (crtc->config->has_pch_encoder)
  6573. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6574. else
  6575. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6576. }
  6577. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6578. struct intel_crtc_state *pipe_config)
  6579. {
  6580. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6581. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6582. if (crtc->pipe != PIPE_A)
  6583. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6584. /* DPLL not used with DSI, but still need the rest set up */
  6585. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6586. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6587. DPLL_EXT_BUFFER_ENABLE_VLV;
  6588. pipe_config->dpll_hw_state.dpll_md =
  6589. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6590. }
  6591. static void chv_compute_dpll(struct intel_crtc *crtc,
  6592. struct intel_crtc_state *pipe_config)
  6593. {
  6594. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6595. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6596. if (crtc->pipe != PIPE_A)
  6597. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6598. /* DPLL not used with DSI, but still need the rest set up */
  6599. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6600. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6601. pipe_config->dpll_hw_state.dpll_md =
  6602. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6603. }
  6604. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6605. const struct intel_crtc_state *pipe_config)
  6606. {
  6607. struct drm_device *dev = crtc->base.dev;
  6608. struct drm_i915_private *dev_priv = to_i915(dev);
  6609. enum pipe pipe = crtc->pipe;
  6610. u32 mdiv;
  6611. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6612. u32 coreclk, reg_val;
  6613. /* Enable Refclk */
  6614. I915_WRITE(DPLL(pipe),
  6615. pipe_config->dpll_hw_state.dpll &
  6616. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6617. /* No need to actually set up the DPLL with DSI */
  6618. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6619. return;
  6620. mutex_lock(&dev_priv->sb_lock);
  6621. bestn = pipe_config->dpll.n;
  6622. bestm1 = pipe_config->dpll.m1;
  6623. bestm2 = pipe_config->dpll.m2;
  6624. bestp1 = pipe_config->dpll.p1;
  6625. bestp2 = pipe_config->dpll.p2;
  6626. /* See eDP HDMI DPIO driver vbios notes doc */
  6627. /* PLL B needs special handling */
  6628. if (pipe == PIPE_B)
  6629. vlv_pllb_recal_opamp(dev_priv, pipe);
  6630. /* Set up Tx target for periodic Rcomp update */
  6631. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6632. /* Disable target IRef on PLL */
  6633. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6634. reg_val &= 0x00ffffff;
  6635. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6636. /* Disable fast lock */
  6637. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6638. /* Set idtafcrecal before PLL is enabled */
  6639. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6640. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6641. mdiv |= ((bestn << DPIO_N_SHIFT));
  6642. mdiv |= (1 << DPIO_K_SHIFT);
  6643. /*
  6644. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6645. * but we don't support that).
  6646. * Note: don't use the DAC post divider as it seems unstable.
  6647. */
  6648. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6649. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6650. mdiv |= DPIO_ENABLE_CALIBRATION;
  6651. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6652. /* Set HBR and RBR LPF coefficients */
  6653. if (pipe_config->port_clock == 162000 ||
  6654. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6655. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6656. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6657. 0x009f0003);
  6658. else
  6659. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6660. 0x00d0000f);
  6661. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6662. /* Use SSC source */
  6663. if (pipe == PIPE_A)
  6664. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6665. 0x0df40000);
  6666. else
  6667. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6668. 0x0df70000);
  6669. } else { /* HDMI or VGA */
  6670. /* Use bend source */
  6671. if (pipe == PIPE_A)
  6672. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6673. 0x0df70000);
  6674. else
  6675. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6676. 0x0df40000);
  6677. }
  6678. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6679. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6680. if (intel_crtc_has_dp_encoder(crtc->config))
  6681. coreclk |= 0x01000000;
  6682. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6684. mutex_unlock(&dev_priv->sb_lock);
  6685. }
  6686. static void chv_prepare_pll(struct intel_crtc *crtc,
  6687. const struct intel_crtc_state *pipe_config)
  6688. {
  6689. struct drm_device *dev = crtc->base.dev;
  6690. struct drm_i915_private *dev_priv = to_i915(dev);
  6691. enum pipe pipe = crtc->pipe;
  6692. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6693. u32 loopfilter, tribuf_calcntr;
  6694. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6695. u32 dpio_val;
  6696. int vco;
  6697. /* Enable Refclk and SSC */
  6698. I915_WRITE(DPLL(pipe),
  6699. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6700. /* No need to actually set up the DPLL with DSI */
  6701. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6702. return;
  6703. bestn = pipe_config->dpll.n;
  6704. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6705. bestm1 = pipe_config->dpll.m1;
  6706. bestm2 = pipe_config->dpll.m2 >> 22;
  6707. bestp1 = pipe_config->dpll.p1;
  6708. bestp2 = pipe_config->dpll.p2;
  6709. vco = pipe_config->dpll.vco;
  6710. dpio_val = 0;
  6711. loopfilter = 0;
  6712. mutex_lock(&dev_priv->sb_lock);
  6713. /* p1 and p2 divider */
  6714. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6715. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6716. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6717. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6718. 1 << DPIO_CHV_K_DIV_SHIFT);
  6719. /* Feedback post-divider - m2 */
  6720. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6721. /* Feedback refclk divider - n and m1 */
  6722. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6723. DPIO_CHV_M1_DIV_BY_2 |
  6724. 1 << DPIO_CHV_N_DIV_SHIFT);
  6725. /* M2 fraction division */
  6726. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6727. /* M2 fraction division enable */
  6728. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6729. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6730. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6731. if (bestm2_frac)
  6732. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6733. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6734. /* Program digital lock detect threshold */
  6735. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6736. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6737. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6738. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6739. if (!bestm2_frac)
  6740. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6741. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6742. /* Loop filter */
  6743. if (vco == 5400000) {
  6744. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6745. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6746. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6747. tribuf_calcntr = 0x9;
  6748. } else if (vco <= 6200000) {
  6749. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6750. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6751. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6752. tribuf_calcntr = 0x9;
  6753. } else if (vco <= 6480000) {
  6754. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6755. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6756. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6757. tribuf_calcntr = 0x8;
  6758. } else {
  6759. /* Not supported. Apply the same limits as in the max case */
  6760. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6761. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6762. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6763. tribuf_calcntr = 0;
  6764. }
  6765. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6766. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6767. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6768. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6769. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6770. /* AFC Recal */
  6771. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6772. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6773. DPIO_AFC_RECAL);
  6774. mutex_unlock(&dev_priv->sb_lock);
  6775. }
  6776. /**
  6777. * vlv_force_pll_on - forcibly enable just the PLL
  6778. * @dev_priv: i915 private structure
  6779. * @pipe: pipe PLL to enable
  6780. * @dpll: PLL configuration
  6781. *
  6782. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6783. * in cases where we need the PLL enabled even when @pipe is not going to
  6784. * be enabled.
  6785. */
  6786. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6787. const struct dpll *dpll)
  6788. {
  6789. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6790. struct intel_crtc_state *pipe_config;
  6791. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6792. if (!pipe_config)
  6793. return -ENOMEM;
  6794. pipe_config->base.crtc = &crtc->base;
  6795. pipe_config->pixel_multiplier = 1;
  6796. pipe_config->dpll = *dpll;
  6797. if (IS_CHERRYVIEW(dev_priv)) {
  6798. chv_compute_dpll(crtc, pipe_config);
  6799. chv_prepare_pll(crtc, pipe_config);
  6800. chv_enable_pll(crtc, pipe_config);
  6801. } else {
  6802. vlv_compute_dpll(crtc, pipe_config);
  6803. vlv_prepare_pll(crtc, pipe_config);
  6804. vlv_enable_pll(crtc, pipe_config);
  6805. }
  6806. kfree(pipe_config);
  6807. return 0;
  6808. }
  6809. /**
  6810. * vlv_force_pll_off - forcibly disable just the PLL
  6811. * @dev_priv: i915 private structure
  6812. * @pipe: pipe PLL to disable
  6813. *
  6814. * Disable the PLL for @pipe. To be used in cases where we need
  6815. * the PLL enabled even when @pipe is not going to be enabled.
  6816. */
  6817. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6818. {
  6819. if (IS_CHERRYVIEW(dev_priv))
  6820. chv_disable_pll(dev_priv, pipe);
  6821. else
  6822. vlv_disable_pll(dev_priv, pipe);
  6823. }
  6824. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6825. struct intel_crtc_state *crtc_state,
  6826. struct dpll *reduced_clock)
  6827. {
  6828. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6829. u32 dpll;
  6830. struct dpll *clock = &crtc_state->dpll;
  6831. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6832. dpll = DPLL_VGA_MODE_DIS;
  6833. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6834. dpll |= DPLLB_MODE_LVDS;
  6835. else
  6836. dpll |= DPLLB_MODE_DAC_SERIAL;
  6837. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
  6838. dpll |= (crtc_state->pixel_multiplier - 1)
  6839. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6840. }
  6841. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6842. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6843. dpll |= DPLL_SDVO_HIGH_SPEED;
  6844. if (intel_crtc_has_dp_encoder(crtc_state))
  6845. dpll |= DPLL_SDVO_HIGH_SPEED;
  6846. /* compute bitmask from p1 value */
  6847. if (IS_PINEVIEW(dev_priv))
  6848. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6849. else {
  6850. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6851. if (IS_G4X(dev_priv) && reduced_clock)
  6852. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6853. }
  6854. switch (clock->p2) {
  6855. case 5:
  6856. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6857. break;
  6858. case 7:
  6859. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6860. break;
  6861. case 10:
  6862. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6863. break;
  6864. case 14:
  6865. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6866. break;
  6867. }
  6868. if (INTEL_GEN(dev_priv) >= 4)
  6869. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6870. if (crtc_state->sdvo_tv_clock)
  6871. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6872. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6873. intel_panel_use_ssc(dev_priv))
  6874. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6875. else
  6876. dpll |= PLL_REF_INPUT_DREFCLK;
  6877. dpll |= DPLL_VCO_ENABLE;
  6878. crtc_state->dpll_hw_state.dpll = dpll;
  6879. if (INTEL_GEN(dev_priv) >= 4) {
  6880. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6881. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6882. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6883. }
  6884. }
  6885. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6886. struct intel_crtc_state *crtc_state,
  6887. struct dpll *reduced_clock)
  6888. {
  6889. struct drm_device *dev = crtc->base.dev;
  6890. struct drm_i915_private *dev_priv = to_i915(dev);
  6891. u32 dpll;
  6892. struct dpll *clock = &crtc_state->dpll;
  6893. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6894. dpll = DPLL_VGA_MODE_DIS;
  6895. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6896. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6897. } else {
  6898. if (clock->p1 == 2)
  6899. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6900. else
  6901. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6902. if (clock->p2 == 4)
  6903. dpll |= PLL_P2_DIVIDE_BY_4;
  6904. }
  6905. if (!IS_I830(dev_priv) &&
  6906. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6907. dpll |= DPLL_DVO_2X_MODE;
  6908. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6909. intel_panel_use_ssc(dev_priv))
  6910. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6911. else
  6912. dpll |= PLL_REF_INPUT_DREFCLK;
  6913. dpll |= DPLL_VCO_ENABLE;
  6914. crtc_state->dpll_hw_state.dpll = dpll;
  6915. }
  6916. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6917. {
  6918. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6919. enum pipe pipe = intel_crtc->pipe;
  6920. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6921. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6922. uint32_t crtc_vtotal, crtc_vblank_end;
  6923. int vsyncshift = 0;
  6924. /* We need to be careful not to changed the adjusted mode, for otherwise
  6925. * the hw state checker will get angry at the mismatch. */
  6926. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6927. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6928. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6929. /* the chip adds 2 halflines automatically */
  6930. crtc_vtotal -= 1;
  6931. crtc_vblank_end -= 1;
  6932. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6933. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6934. else
  6935. vsyncshift = adjusted_mode->crtc_hsync_start -
  6936. adjusted_mode->crtc_htotal / 2;
  6937. if (vsyncshift < 0)
  6938. vsyncshift += adjusted_mode->crtc_htotal;
  6939. }
  6940. if (INTEL_GEN(dev_priv) > 3)
  6941. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6942. I915_WRITE(HTOTAL(cpu_transcoder),
  6943. (adjusted_mode->crtc_hdisplay - 1) |
  6944. ((adjusted_mode->crtc_htotal - 1) << 16));
  6945. I915_WRITE(HBLANK(cpu_transcoder),
  6946. (adjusted_mode->crtc_hblank_start - 1) |
  6947. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6948. I915_WRITE(HSYNC(cpu_transcoder),
  6949. (adjusted_mode->crtc_hsync_start - 1) |
  6950. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6951. I915_WRITE(VTOTAL(cpu_transcoder),
  6952. (adjusted_mode->crtc_vdisplay - 1) |
  6953. ((crtc_vtotal - 1) << 16));
  6954. I915_WRITE(VBLANK(cpu_transcoder),
  6955. (adjusted_mode->crtc_vblank_start - 1) |
  6956. ((crtc_vblank_end - 1) << 16));
  6957. I915_WRITE(VSYNC(cpu_transcoder),
  6958. (adjusted_mode->crtc_vsync_start - 1) |
  6959. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6960. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6961. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6962. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6963. * bits. */
  6964. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6965. (pipe == PIPE_B || pipe == PIPE_C))
  6966. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6967. }
  6968. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6969. {
  6970. struct drm_device *dev = intel_crtc->base.dev;
  6971. struct drm_i915_private *dev_priv = to_i915(dev);
  6972. enum pipe pipe = intel_crtc->pipe;
  6973. /* pipesrc controls the size that is scaled from, which should
  6974. * always be the user's requested size.
  6975. */
  6976. I915_WRITE(PIPESRC(pipe),
  6977. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6978. (intel_crtc->config->pipe_src_h - 1));
  6979. }
  6980. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6981. struct intel_crtc_state *pipe_config)
  6982. {
  6983. struct drm_device *dev = crtc->base.dev;
  6984. struct drm_i915_private *dev_priv = to_i915(dev);
  6985. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6986. uint32_t tmp;
  6987. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6988. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6989. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6990. tmp = I915_READ(HBLANK(cpu_transcoder));
  6991. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6992. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6993. tmp = I915_READ(HSYNC(cpu_transcoder));
  6994. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6995. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6996. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6997. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6998. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6999. tmp = I915_READ(VBLANK(cpu_transcoder));
  7000. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7001. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7002. tmp = I915_READ(VSYNC(cpu_transcoder));
  7003. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7004. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7005. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7006. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7007. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7008. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7009. }
  7010. }
  7011. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7012. struct intel_crtc_state *pipe_config)
  7013. {
  7014. struct drm_device *dev = crtc->base.dev;
  7015. struct drm_i915_private *dev_priv = to_i915(dev);
  7016. u32 tmp;
  7017. tmp = I915_READ(PIPESRC(crtc->pipe));
  7018. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7019. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7020. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7021. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7022. }
  7023. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7024. struct intel_crtc_state *pipe_config)
  7025. {
  7026. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7027. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7028. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7029. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7030. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7031. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7032. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7033. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7034. mode->flags = pipe_config->base.adjusted_mode.flags;
  7035. mode->type = DRM_MODE_TYPE_DRIVER;
  7036. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7037. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7038. mode->hsync = drm_mode_hsync(mode);
  7039. mode->vrefresh = drm_mode_vrefresh(mode);
  7040. drm_mode_set_name(mode);
  7041. }
  7042. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7043. {
  7044. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  7045. uint32_t pipeconf;
  7046. pipeconf = 0;
  7047. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7048. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7049. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7050. if (intel_crtc->config->double_wide)
  7051. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7052. /* only g4x and later have fancy bpc/dither controls */
  7053. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7054. IS_CHERRYVIEW(dev_priv)) {
  7055. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7056. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7057. pipeconf |= PIPECONF_DITHER_EN |
  7058. PIPECONF_DITHER_TYPE_SP;
  7059. switch (intel_crtc->config->pipe_bpp) {
  7060. case 18:
  7061. pipeconf |= PIPECONF_6BPC;
  7062. break;
  7063. case 24:
  7064. pipeconf |= PIPECONF_8BPC;
  7065. break;
  7066. case 30:
  7067. pipeconf |= PIPECONF_10BPC;
  7068. break;
  7069. default:
  7070. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7071. BUG();
  7072. }
  7073. }
  7074. if (HAS_PIPE_CXSR(dev_priv)) {
  7075. if (intel_crtc->lowfreq_avail) {
  7076. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7077. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7078. } else {
  7079. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7080. }
  7081. }
  7082. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7083. if (INTEL_GEN(dev_priv) < 4 ||
  7084. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7085. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7086. else
  7087. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7088. } else
  7089. pipeconf |= PIPECONF_PROGRESSIVE;
  7090. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7091. intel_crtc->config->limited_color_range)
  7092. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7093. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7094. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7095. }
  7096. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7097. struct intel_crtc_state *crtc_state)
  7098. {
  7099. struct drm_device *dev = crtc->base.dev;
  7100. struct drm_i915_private *dev_priv = to_i915(dev);
  7101. const struct intel_limit *limit;
  7102. int refclk = 48000;
  7103. memset(&crtc_state->dpll_hw_state, 0,
  7104. sizeof(crtc_state->dpll_hw_state));
  7105. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7106. if (intel_panel_use_ssc(dev_priv)) {
  7107. refclk = dev_priv->vbt.lvds_ssc_freq;
  7108. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7109. }
  7110. limit = &intel_limits_i8xx_lvds;
  7111. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7112. limit = &intel_limits_i8xx_dvo;
  7113. } else {
  7114. limit = &intel_limits_i8xx_dac;
  7115. }
  7116. if (!crtc_state->clock_set &&
  7117. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7118. refclk, NULL, &crtc_state->dpll)) {
  7119. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7120. return -EINVAL;
  7121. }
  7122. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7123. return 0;
  7124. }
  7125. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7126. struct intel_crtc_state *crtc_state)
  7127. {
  7128. struct drm_device *dev = crtc->base.dev;
  7129. struct drm_i915_private *dev_priv = to_i915(dev);
  7130. const struct intel_limit *limit;
  7131. int refclk = 96000;
  7132. memset(&crtc_state->dpll_hw_state, 0,
  7133. sizeof(crtc_state->dpll_hw_state));
  7134. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7135. if (intel_panel_use_ssc(dev_priv)) {
  7136. refclk = dev_priv->vbt.lvds_ssc_freq;
  7137. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7138. }
  7139. if (intel_is_dual_link_lvds(dev))
  7140. limit = &intel_limits_g4x_dual_channel_lvds;
  7141. else
  7142. limit = &intel_limits_g4x_single_channel_lvds;
  7143. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7144. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7145. limit = &intel_limits_g4x_hdmi;
  7146. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7147. limit = &intel_limits_g4x_sdvo;
  7148. } else {
  7149. /* The option is for other outputs */
  7150. limit = &intel_limits_i9xx_sdvo;
  7151. }
  7152. if (!crtc_state->clock_set &&
  7153. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7154. refclk, NULL, &crtc_state->dpll)) {
  7155. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7156. return -EINVAL;
  7157. }
  7158. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7159. return 0;
  7160. }
  7161. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7162. struct intel_crtc_state *crtc_state)
  7163. {
  7164. struct drm_device *dev = crtc->base.dev;
  7165. struct drm_i915_private *dev_priv = to_i915(dev);
  7166. const struct intel_limit *limit;
  7167. int refclk = 96000;
  7168. memset(&crtc_state->dpll_hw_state, 0,
  7169. sizeof(crtc_state->dpll_hw_state));
  7170. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7171. if (intel_panel_use_ssc(dev_priv)) {
  7172. refclk = dev_priv->vbt.lvds_ssc_freq;
  7173. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7174. }
  7175. limit = &intel_limits_pineview_lvds;
  7176. } else {
  7177. limit = &intel_limits_pineview_sdvo;
  7178. }
  7179. if (!crtc_state->clock_set &&
  7180. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7181. refclk, NULL, &crtc_state->dpll)) {
  7182. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7183. return -EINVAL;
  7184. }
  7185. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7186. return 0;
  7187. }
  7188. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7189. struct intel_crtc_state *crtc_state)
  7190. {
  7191. struct drm_device *dev = crtc->base.dev;
  7192. struct drm_i915_private *dev_priv = to_i915(dev);
  7193. const struct intel_limit *limit;
  7194. int refclk = 96000;
  7195. memset(&crtc_state->dpll_hw_state, 0,
  7196. sizeof(crtc_state->dpll_hw_state));
  7197. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7198. if (intel_panel_use_ssc(dev_priv)) {
  7199. refclk = dev_priv->vbt.lvds_ssc_freq;
  7200. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7201. }
  7202. limit = &intel_limits_i9xx_lvds;
  7203. } else {
  7204. limit = &intel_limits_i9xx_sdvo;
  7205. }
  7206. if (!crtc_state->clock_set &&
  7207. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7208. refclk, NULL, &crtc_state->dpll)) {
  7209. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7210. return -EINVAL;
  7211. }
  7212. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7213. return 0;
  7214. }
  7215. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7216. struct intel_crtc_state *crtc_state)
  7217. {
  7218. int refclk = 100000;
  7219. const struct intel_limit *limit = &intel_limits_chv;
  7220. memset(&crtc_state->dpll_hw_state, 0,
  7221. sizeof(crtc_state->dpll_hw_state));
  7222. if (!crtc_state->clock_set &&
  7223. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7224. refclk, NULL, &crtc_state->dpll)) {
  7225. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7226. return -EINVAL;
  7227. }
  7228. chv_compute_dpll(crtc, crtc_state);
  7229. return 0;
  7230. }
  7231. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7232. struct intel_crtc_state *crtc_state)
  7233. {
  7234. int refclk = 100000;
  7235. const struct intel_limit *limit = &intel_limits_vlv;
  7236. memset(&crtc_state->dpll_hw_state, 0,
  7237. sizeof(crtc_state->dpll_hw_state));
  7238. if (!crtc_state->clock_set &&
  7239. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7240. refclk, NULL, &crtc_state->dpll)) {
  7241. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7242. return -EINVAL;
  7243. }
  7244. vlv_compute_dpll(crtc, crtc_state);
  7245. return 0;
  7246. }
  7247. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7248. struct intel_crtc_state *pipe_config)
  7249. {
  7250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7251. uint32_t tmp;
  7252. if (INTEL_GEN(dev_priv) <= 3 &&
  7253. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7254. return;
  7255. tmp = I915_READ(PFIT_CONTROL);
  7256. if (!(tmp & PFIT_ENABLE))
  7257. return;
  7258. /* Check whether the pfit is attached to our pipe. */
  7259. if (INTEL_GEN(dev_priv) < 4) {
  7260. if (crtc->pipe != PIPE_B)
  7261. return;
  7262. } else {
  7263. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7264. return;
  7265. }
  7266. pipe_config->gmch_pfit.control = tmp;
  7267. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7268. }
  7269. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7270. struct intel_crtc_state *pipe_config)
  7271. {
  7272. struct drm_device *dev = crtc->base.dev;
  7273. struct drm_i915_private *dev_priv = to_i915(dev);
  7274. int pipe = pipe_config->cpu_transcoder;
  7275. struct dpll clock;
  7276. u32 mdiv;
  7277. int refclk = 100000;
  7278. /* In case of DSI, DPLL will not be used */
  7279. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7280. return;
  7281. mutex_lock(&dev_priv->sb_lock);
  7282. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7283. mutex_unlock(&dev_priv->sb_lock);
  7284. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7285. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7286. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7287. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7288. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7289. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7290. }
  7291. static void
  7292. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7293. struct intel_initial_plane_config *plane_config)
  7294. {
  7295. struct drm_device *dev = crtc->base.dev;
  7296. struct drm_i915_private *dev_priv = to_i915(dev);
  7297. u32 val, base, offset;
  7298. int pipe = crtc->pipe, plane = crtc->plane;
  7299. int fourcc, pixel_format;
  7300. unsigned int aligned_height;
  7301. struct drm_framebuffer *fb;
  7302. struct intel_framebuffer *intel_fb;
  7303. val = I915_READ(DSPCNTR(plane));
  7304. if (!(val & DISPLAY_PLANE_ENABLE))
  7305. return;
  7306. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7307. if (!intel_fb) {
  7308. DRM_DEBUG_KMS("failed to alloc fb\n");
  7309. return;
  7310. }
  7311. fb = &intel_fb->base;
  7312. fb->dev = dev;
  7313. if (INTEL_GEN(dev_priv) >= 4) {
  7314. if (val & DISPPLANE_TILED) {
  7315. plane_config->tiling = I915_TILING_X;
  7316. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7317. }
  7318. }
  7319. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7320. fourcc = i9xx_format_to_fourcc(pixel_format);
  7321. fb->pixel_format = fourcc;
  7322. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7323. if (INTEL_GEN(dev_priv) >= 4) {
  7324. if (plane_config->tiling)
  7325. offset = I915_READ(DSPTILEOFF(plane));
  7326. else
  7327. offset = I915_READ(DSPLINOFF(plane));
  7328. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7329. } else {
  7330. base = I915_READ(DSPADDR(plane));
  7331. }
  7332. plane_config->base = base;
  7333. val = I915_READ(PIPESRC(pipe));
  7334. fb->width = ((val >> 16) & 0xfff) + 1;
  7335. fb->height = ((val >> 0) & 0xfff) + 1;
  7336. val = I915_READ(DSPSTRIDE(pipe));
  7337. fb->pitches[0] = val & 0xffffffc0;
  7338. aligned_height = intel_fb_align_height(dev, fb->height,
  7339. fb->pixel_format,
  7340. fb->modifier);
  7341. plane_config->size = fb->pitches[0] * aligned_height;
  7342. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7343. pipe_name(pipe), plane, fb->width, fb->height,
  7344. fb->bits_per_pixel, base, fb->pitches[0],
  7345. plane_config->size);
  7346. plane_config->fb = intel_fb;
  7347. }
  7348. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7349. struct intel_crtc_state *pipe_config)
  7350. {
  7351. struct drm_device *dev = crtc->base.dev;
  7352. struct drm_i915_private *dev_priv = to_i915(dev);
  7353. int pipe = pipe_config->cpu_transcoder;
  7354. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7355. struct dpll clock;
  7356. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7357. int refclk = 100000;
  7358. /* In case of DSI, DPLL will not be used */
  7359. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7360. return;
  7361. mutex_lock(&dev_priv->sb_lock);
  7362. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7363. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7364. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7365. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7366. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7367. mutex_unlock(&dev_priv->sb_lock);
  7368. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7369. clock.m2 = (pll_dw0 & 0xff) << 22;
  7370. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7371. clock.m2 |= pll_dw2 & 0x3fffff;
  7372. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7373. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7374. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7375. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7376. }
  7377. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7378. struct intel_crtc_state *pipe_config)
  7379. {
  7380. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7381. enum intel_display_power_domain power_domain;
  7382. uint32_t tmp;
  7383. bool ret;
  7384. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7385. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7386. return false;
  7387. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7388. pipe_config->shared_dpll = NULL;
  7389. ret = false;
  7390. tmp = I915_READ(PIPECONF(crtc->pipe));
  7391. if (!(tmp & PIPECONF_ENABLE))
  7392. goto out;
  7393. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7394. IS_CHERRYVIEW(dev_priv)) {
  7395. switch (tmp & PIPECONF_BPC_MASK) {
  7396. case PIPECONF_6BPC:
  7397. pipe_config->pipe_bpp = 18;
  7398. break;
  7399. case PIPECONF_8BPC:
  7400. pipe_config->pipe_bpp = 24;
  7401. break;
  7402. case PIPECONF_10BPC:
  7403. pipe_config->pipe_bpp = 30;
  7404. break;
  7405. default:
  7406. break;
  7407. }
  7408. }
  7409. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7410. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7411. pipe_config->limited_color_range = true;
  7412. if (INTEL_GEN(dev_priv) < 4)
  7413. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7414. intel_get_pipe_timings(crtc, pipe_config);
  7415. intel_get_pipe_src_size(crtc, pipe_config);
  7416. i9xx_get_pfit_config(crtc, pipe_config);
  7417. if (INTEL_GEN(dev_priv) >= 4) {
  7418. /* No way to read it out on pipes B and C */
  7419. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7420. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7421. else
  7422. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7423. pipe_config->pixel_multiplier =
  7424. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7425. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7426. pipe_config->dpll_hw_state.dpll_md = tmp;
  7427. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7428. IS_G33(dev_priv)) {
  7429. tmp = I915_READ(DPLL(crtc->pipe));
  7430. pipe_config->pixel_multiplier =
  7431. ((tmp & SDVO_MULTIPLIER_MASK)
  7432. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7433. } else {
  7434. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7435. * port and will be fixed up in the encoder->get_config
  7436. * function. */
  7437. pipe_config->pixel_multiplier = 1;
  7438. }
  7439. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7440. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7441. /*
  7442. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7443. * on 830. Filter it out here so that we don't
  7444. * report errors due to that.
  7445. */
  7446. if (IS_I830(dev_priv))
  7447. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7448. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7449. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7450. } else {
  7451. /* Mask out read-only status bits. */
  7452. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7453. DPLL_PORTC_READY_MASK |
  7454. DPLL_PORTB_READY_MASK);
  7455. }
  7456. if (IS_CHERRYVIEW(dev_priv))
  7457. chv_crtc_clock_get(crtc, pipe_config);
  7458. else if (IS_VALLEYVIEW(dev_priv))
  7459. vlv_crtc_clock_get(crtc, pipe_config);
  7460. else
  7461. i9xx_crtc_clock_get(crtc, pipe_config);
  7462. /*
  7463. * Normally the dotclock is filled in by the encoder .get_config()
  7464. * but in case the pipe is enabled w/o any ports we need a sane
  7465. * default.
  7466. */
  7467. pipe_config->base.adjusted_mode.crtc_clock =
  7468. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7469. ret = true;
  7470. out:
  7471. intel_display_power_put(dev_priv, power_domain);
  7472. return ret;
  7473. }
  7474. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7475. {
  7476. struct drm_i915_private *dev_priv = to_i915(dev);
  7477. struct intel_encoder *encoder;
  7478. int i;
  7479. u32 val, final;
  7480. bool has_lvds = false;
  7481. bool has_cpu_edp = false;
  7482. bool has_panel = false;
  7483. bool has_ck505 = false;
  7484. bool can_ssc = false;
  7485. bool using_ssc_source = false;
  7486. /* We need to take the global config into account */
  7487. for_each_intel_encoder(dev, encoder) {
  7488. switch (encoder->type) {
  7489. case INTEL_OUTPUT_LVDS:
  7490. has_panel = true;
  7491. has_lvds = true;
  7492. break;
  7493. case INTEL_OUTPUT_EDP:
  7494. has_panel = true;
  7495. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7496. has_cpu_edp = true;
  7497. break;
  7498. default:
  7499. break;
  7500. }
  7501. }
  7502. if (HAS_PCH_IBX(dev_priv)) {
  7503. has_ck505 = dev_priv->vbt.display_clock_mode;
  7504. can_ssc = has_ck505;
  7505. } else {
  7506. has_ck505 = false;
  7507. can_ssc = true;
  7508. }
  7509. /* Check if any DPLLs are using the SSC source */
  7510. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7511. u32 temp = I915_READ(PCH_DPLL(i));
  7512. if (!(temp & DPLL_VCO_ENABLE))
  7513. continue;
  7514. if ((temp & PLL_REF_INPUT_MASK) ==
  7515. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7516. using_ssc_source = true;
  7517. break;
  7518. }
  7519. }
  7520. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7521. has_panel, has_lvds, has_ck505, using_ssc_source);
  7522. /* Ironlake: try to setup display ref clock before DPLL
  7523. * enabling. This is only under driver's control after
  7524. * PCH B stepping, previous chipset stepping should be
  7525. * ignoring this setting.
  7526. */
  7527. val = I915_READ(PCH_DREF_CONTROL);
  7528. /* As we must carefully and slowly disable/enable each source in turn,
  7529. * compute the final state we want first and check if we need to
  7530. * make any changes at all.
  7531. */
  7532. final = val;
  7533. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7534. if (has_ck505)
  7535. final |= DREF_NONSPREAD_CK505_ENABLE;
  7536. else
  7537. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7538. final &= ~DREF_SSC_SOURCE_MASK;
  7539. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7540. final &= ~DREF_SSC1_ENABLE;
  7541. if (has_panel) {
  7542. final |= DREF_SSC_SOURCE_ENABLE;
  7543. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7544. final |= DREF_SSC1_ENABLE;
  7545. if (has_cpu_edp) {
  7546. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7547. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7548. else
  7549. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7550. } else
  7551. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7552. } else if (using_ssc_source) {
  7553. final |= DREF_SSC_SOURCE_ENABLE;
  7554. final |= DREF_SSC1_ENABLE;
  7555. }
  7556. if (final == val)
  7557. return;
  7558. /* Always enable nonspread source */
  7559. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7560. if (has_ck505)
  7561. val |= DREF_NONSPREAD_CK505_ENABLE;
  7562. else
  7563. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7564. if (has_panel) {
  7565. val &= ~DREF_SSC_SOURCE_MASK;
  7566. val |= DREF_SSC_SOURCE_ENABLE;
  7567. /* SSC must be turned on before enabling the CPU output */
  7568. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7569. DRM_DEBUG_KMS("Using SSC on panel\n");
  7570. val |= DREF_SSC1_ENABLE;
  7571. } else
  7572. val &= ~DREF_SSC1_ENABLE;
  7573. /* Get SSC going before enabling the outputs */
  7574. I915_WRITE(PCH_DREF_CONTROL, val);
  7575. POSTING_READ(PCH_DREF_CONTROL);
  7576. udelay(200);
  7577. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7578. /* Enable CPU source on CPU attached eDP */
  7579. if (has_cpu_edp) {
  7580. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7581. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7582. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7583. } else
  7584. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7585. } else
  7586. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7587. I915_WRITE(PCH_DREF_CONTROL, val);
  7588. POSTING_READ(PCH_DREF_CONTROL);
  7589. udelay(200);
  7590. } else {
  7591. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7592. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7593. /* Turn off CPU output */
  7594. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7595. I915_WRITE(PCH_DREF_CONTROL, val);
  7596. POSTING_READ(PCH_DREF_CONTROL);
  7597. udelay(200);
  7598. if (!using_ssc_source) {
  7599. DRM_DEBUG_KMS("Disabling SSC source\n");
  7600. /* Turn off the SSC source */
  7601. val &= ~DREF_SSC_SOURCE_MASK;
  7602. val |= DREF_SSC_SOURCE_DISABLE;
  7603. /* Turn off SSC1 */
  7604. val &= ~DREF_SSC1_ENABLE;
  7605. I915_WRITE(PCH_DREF_CONTROL, val);
  7606. POSTING_READ(PCH_DREF_CONTROL);
  7607. udelay(200);
  7608. }
  7609. }
  7610. BUG_ON(val != final);
  7611. }
  7612. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7613. {
  7614. uint32_t tmp;
  7615. tmp = I915_READ(SOUTH_CHICKEN2);
  7616. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7617. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7618. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7619. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7620. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7621. tmp = I915_READ(SOUTH_CHICKEN2);
  7622. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7623. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7624. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7625. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7626. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7627. }
  7628. /* WaMPhyProgramming:hsw */
  7629. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7630. {
  7631. uint32_t tmp;
  7632. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7633. tmp &= ~(0xFF << 24);
  7634. tmp |= (0x12 << 24);
  7635. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7636. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7637. tmp |= (1 << 11);
  7638. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7639. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7640. tmp |= (1 << 11);
  7641. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7642. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7643. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7644. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7645. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7646. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7647. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7648. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7649. tmp &= ~(7 << 13);
  7650. tmp |= (5 << 13);
  7651. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7652. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7653. tmp &= ~(7 << 13);
  7654. tmp |= (5 << 13);
  7655. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7656. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7657. tmp &= ~0xFF;
  7658. tmp |= 0x1C;
  7659. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7660. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7661. tmp &= ~0xFF;
  7662. tmp |= 0x1C;
  7663. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7664. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7665. tmp &= ~(0xFF << 16);
  7666. tmp |= (0x1C << 16);
  7667. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7668. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7669. tmp &= ~(0xFF << 16);
  7670. tmp |= (0x1C << 16);
  7671. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7672. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7673. tmp |= (1 << 27);
  7674. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7675. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7676. tmp |= (1 << 27);
  7677. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7678. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7679. tmp &= ~(0xF << 28);
  7680. tmp |= (4 << 28);
  7681. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7682. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7683. tmp &= ~(0xF << 28);
  7684. tmp |= (4 << 28);
  7685. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7686. }
  7687. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7688. * Programming" based on the parameters passed:
  7689. * - Sequence to enable CLKOUT_DP
  7690. * - Sequence to enable CLKOUT_DP without spread
  7691. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7692. */
  7693. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7694. bool with_fdi)
  7695. {
  7696. struct drm_i915_private *dev_priv = to_i915(dev);
  7697. uint32_t reg, tmp;
  7698. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7699. with_spread = true;
  7700. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7701. with_fdi, "LP PCH doesn't have FDI\n"))
  7702. with_fdi = false;
  7703. mutex_lock(&dev_priv->sb_lock);
  7704. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7705. tmp &= ~SBI_SSCCTL_DISABLE;
  7706. tmp |= SBI_SSCCTL_PATHALT;
  7707. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7708. udelay(24);
  7709. if (with_spread) {
  7710. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7711. tmp &= ~SBI_SSCCTL_PATHALT;
  7712. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7713. if (with_fdi) {
  7714. lpt_reset_fdi_mphy(dev_priv);
  7715. lpt_program_fdi_mphy(dev_priv);
  7716. }
  7717. }
  7718. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7719. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7720. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7721. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7722. mutex_unlock(&dev_priv->sb_lock);
  7723. }
  7724. /* Sequence to disable CLKOUT_DP */
  7725. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7726. {
  7727. struct drm_i915_private *dev_priv = to_i915(dev);
  7728. uint32_t reg, tmp;
  7729. mutex_lock(&dev_priv->sb_lock);
  7730. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7731. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7732. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7733. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7734. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7735. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7736. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7737. tmp |= SBI_SSCCTL_PATHALT;
  7738. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7739. udelay(32);
  7740. }
  7741. tmp |= SBI_SSCCTL_DISABLE;
  7742. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7743. }
  7744. mutex_unlock(&dev_priv->sb_lock);
  7745. }
  7746. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7747. static const uint16_t sscdivintphase[] = {
  7748. [BEND_IDX( 50)] = 0x3B23,
  7749. [BEND_IDX( 45)] = 0x3B23,
  7750. [BEND_IDX( 40)] = 0x3C23,
  7751. [BEND_IDX( 35)] = 0x3C23,
  7752. [BEND_IDX( 30)] = 0x3D23,
  7753. [BEND_IDX( 25)] = 0x3D23,
  7754. [BEND_IDX( 20)] = 0x3E23,
  7755. [BEND_IDX( 15)] = 0x3E23,
  7756. [BEND_IDX( 10)] = 0x3F23,
  7757. [BEND_IDX( 5)] = 0x3F23,
  7758. [BEND_IDX( 0)] = 0x0025,
  7759. [BEND_IDX( -5)] = 0x0025,
  7760. [BEND_IDX(-10)] = 0x0125,
  7761. [BEND_IDX(-15)] = 0x0125,
  7762. [BEND_IDX(-20)] = 0x0225,
  7763. [BEND_IDX(-25)] = 0x0225,
  7764. [BEND_IDX(-30)] = 0x0325,
  7765. [BEND_IDX(-35)] = 0x0325,
  7766. [BEND_IDX(-40)] = 0x0425,
  7767. [BEND_IDX(-45)] = 0x0425,
  7768. [BEND_IDX(-50)] = 0x0525,
  7769. };
  7770. /*
  7771. * Bend CLKOUT_DP
  7772. * steps -50 to 50 inclusive, in steps of 5
  7773. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7774. * change in clock period = -(steps / 10) * 5.787 ps
  7775. */
  7776. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7777. {
  7778. uint32_t tmp;
  7779. int idx = BEND_IDX(steps);
  7780. if (WARN_ON(steps % 5 != 0))
  7781. return;
  7782. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7783. return;
  7784. mutex_lock(&dev_priv->sb_lock);
  7785. if (steps % 10 != 0)
  7786. tmp = 0xAAAAAAAB;
  7787. else
  7788. tmp = 0x00000000;
  7789. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7790. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7791. tmp &= 0xffff0000;
  7792. tmp |= sscdivintphase[idx];
  7793. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7794. mutex_unlock(&dev_priv->sb_lock);
  7795. }
  7796. #undef BEND_IDX
  7797. static void lpt_init_pch_refclk(struct drm_device *dev)
  7798. {
  7799. struct intel_encoder *encoder;
  7800. bool has_vga = false;
  7801. for_each_intel_encoder(dev, encoder) {
  7802. switch (encoder->type) {
  7803. case INTEL_OUTPUT_ANALOG:
  7804. has_vga = true;
  7805. break;
  7806. default:
  7807. break;
  7808. }
  7809. }
  7810. if (has_vga) {
  7811. lpt_bend_clkout_dp(to_i915(dev), 0);
  7812. lpt_enable_clkout_dp(dev, true, true);
  7813. } else {
  7814. lpt_disable_clkout_dp(dev);
  7815. }
  7816. }
  7817. /*
  7818. * Initialize reference clocks when the driver loads
  7819. */
  7820. void intel_init_pch_refclk(struct drm_device *dev)
  7821. {
  7822. struct drm_i915_private *dev_priv = to_i915(dev);
  7823. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7824. ironlake_init_pch_refclk(dev);
  7825. else if (HAS_PCH_LPT(dev_priv))
  7826. lpt_init_pch_refclk(dev);
  7827. }
  7828. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7829. {
  7830. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7832. int pipe = intel_crtc->pipe;
  7833. uint32_t val;
  7834. val = 0;
  7835. switch (intel_crtc->config->pipe_bpp) {
  7836. case 18:
  7837. val |= PIPECONF_6BPC;
  7838. break;
  7839. case 24:
  7840. val |= PIPECONF_8BPC;
  7841. break;
  7842. case 30:
  7843. val |= PIPECONF_10BPC;
  7844. break;
  7845. case 36:
  7846. val |= PIPECONF_12BPC;
  7847. break;
  7848. default:
  7849. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7850. BUG();
  7851. }
  7852. if (intel_crtc->config->dither)
  7853. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7854. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7855. val |= PIPECONF_INTERLACED_ILK;
  7856. else
  7857. val |= PIPECONF_PROGRESSIVE;
  7858. if (intel_crtc->config->limited_color_range)
  7859. val |= PIPECONF_COLOR_RANGE_SELECT;
  7860. I915_WRITE(PIPECONF(pipe), val);
  7861. POSTING_READ(PIPECONF(pipe));
  7862. }
  7863. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7864. {
  7865. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7867. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7868. u32 val = 0;
  7869. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7870. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7871. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7872. val |= PIPECONF_INTERLACED_ILK;
  7873. else
  7874. val |= PIPECONF_PROGRESSIVE;
  7875. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7876. POSTING_READ(PIPECONF(cpu_transcoder));
  7877. }
  7878. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7879. {
  7880. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7882. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7883. u32 val = 0;
  7884. switch (intel_crtc->config->pipe_bpp) {
  7885. case 18:
  7886. val |= PIPEMISC_DITHER_6_BPC;
  7887. break;
  7888. case 24:
  7889. val |= PIPEMISC_DITHER_8_BPC;
  7890. break;
  7891. case 30:
  7892. val |= PIPEMISC_DITHER_10_BPC;
  7893. break;
  7894. case 36:
  7895. val |= PIPEMISC_DITHER_12_BPC;
  7896. break;
  7897. default:
  7898. /* Case prevented by pipe_config_set_bpp. */
  7899. BUG();
  7900. }
  7901. if (intel_crtc->config->dither)
  7902. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7903. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7904. }
  7905. }
  7906. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7907. {
  7908. /*
  7909. * Account for spread spectrum to avoid
  7910. * oversubscribing the link. Max center spread
  7911. * is 2.5%; use 5% for safety's sake.
  7912. */
  7913. u32 bps = target_clock * bpp * 21 / 20;
  7914. return DIV_ROUND_UP(bps, link_bw * 8);
  7915. }
  7916. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7917. {
  7918. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7919. }
  7920. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7921. struct intel_crtc_state *crtc_state,
  7922. struct dpll *reduced_clock)
  7923. {
  7924. struct drm_crtc *crtc = &intel_crtc->base;
  7925. struct drm_device *dev = crtc->dev;
  7926. struct drm_i915_private *dev_priv = to_i915(dev);
  7927. u32 dpll, fp, fp2;
  7928. int factor;
  7929. /* Enable autotuning of the PLL clock (if permissible) */
  7930. factor = 21;
  7931. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7932. if ((intel_panel_use_ssc(dev_priv) &&
  7933. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7934. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7935. factor = 25;
  7936. } else if (crtc_state->sdvo_tv_clock)
  7937. factor = 20;
  7938. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7939. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7940. fp |= FP_CB_TUNE;
  7941. if (reduced_clock) {
  7942. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7943. if (reduced_clock->m < factor * reduced_clock->n)
  7944. fp2 |= FP_CB_TUNE;
  7945. } else {
  7946. fp2 = fp;
  7947. }
  7948. dpll = 0;
  7949. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7950. dpll |= DPLLB_MODE_LVDS;
  7951. else
  7952. dpll |= DPLLB_MODE_DAC_SERIAL;
  7953. dpll |= (crtc_state->pixel_multiplier - 1)
  7954. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7955. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7956. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7957. dpll |= DPLL_SDVO_HIGH_SPEED;
  7958. if (intel_crtc_has_dp_encoder(crtc_state))
  7959. dpll |= DPLL_SDVO_HIGH_SPEED;
  7960. /*
  7961. * The high speed IO clock is only really required for
  7962. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7963. * possible to share the DPLL between CRT and HDMI. Enabling
  7964. * the clock needlessly does no real harm, except use up a
  7965. * bit of power potentially.
  7966. *
  7967. * We'll limit this to IVB with 3 pipes, since it has only two
  7968. * DPLLs and so DPLL sharing is the only way to get three pipes
  7969. * driving PCH ports at the same time. On SNB we could do this,
  7970. * and potentially avoid enabling the second DPLL, but it's not
  7971. * clear if it''s a win or loss power wise. No point in doing
  7972. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7973. */
  7974. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7975. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7976. dpll |= DPLL_SDVO_HIGH_SPEED;
  7977. /* compute bitmask from p1 value */
  7978. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7979. /* also FPA1 */
  7980. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7981. switch (crtc_state->dpll.p2) {
  7982. case 5:
  7983. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7984. break;
  7985. case 7:
  7986. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7987. break;
  7988. case 10:
  7989. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7990. break;
  7991. case 14:
  7992. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7993. break;
  7994. }
  7995. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7996. intel_panel_use_ssc(dev_priv))
  7997. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7998. else
  7999. dpll |= PLL_REF_INPUT_DREFCLK;
  8000. dpll |= DPLL_VCO_ENABLE;
  8001. crtc_state->dpll_hw_state.dpll = dpll;
  8002. crtc_state->dpll_hw_state.fp0 = fp;
  8003. crtc_state->dpll_hw_state.fp1 = fp2;
  8004. }
  8005. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8006. struct intel_crtc_state *crtc_state)
  8007. {
  8008. struct drm_device *dev = crtc->base.dev;
  8009. struct drm_i915_private *dev_priv = to_i915(dev);
  8010. struct dpll reduced_clock;
  8011. bool has_reduced_clock = false;
  8012. struct intel_shared_dpll *pll;
  8013. const struct intel_limit *limit;
  8014. int refclk = 120000;
  8015. memset(&crtc_state->dpll_hw_state, 0,
  8016. sizeof(crtc_state->dpll_hw_state));
  8017. crtc->lowfreq_avail = false;
  8018. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8019. if (!crtc_state->has_pch_encoder)
  8020. return 0;
  8021. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8022. if (intel_panel_use_ssc(dev_priv)) {
  8023. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8024. dev_priv->vbt.lvds_ssc_freq);
  8025. refclk = dev_priv->vbt.lvds_ssc_freq;
  8026. }
  8027. if (intel_is_dual_link_lvds(dev)) {
  8028. if (refclk == 100000)
  8029. limit = &intel_limits_ironlake_dual_lvds_100m;
  8030. else
  8031. limit = &intel_limits_ironlake_dual_lvds;
  8032. } else {
  8033. if (refclk == 100000)
  8034. limit = &intel_limits_ironlake_single_lvds_100m;
  8035. else
  8036. limit = &intel_limits_ironlake_single_lvds;
  8037. }
  8038. } else {
  8039. limit = &intel_limits_ironlake_dac;
  8040. }
  8041. if (!crtc_state->clock_set &&
  8042. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8043. refclk, NULL, &crtc_state->dpll)) {
  8044. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8045. return -EINVAL;
  8046. }
  8047. ironlake_compute_dpll(crtc, crtc_state,
  8048. has_reduced_clock ? &reduced_clock : NULL);
  8049. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8050. if (pll == NULL) {
  8051. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8052. pipe_name(crtc->pipe));
  8053. return -EINVAL;
  8054. }
  8055. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8056. has_reduced_clock)
  8057. crtc->lowfreq_avail = true;
  8058. return 0;
  8059. }
  8060. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8061. struct intel_link_m_n *m_n)
  8062. {
  8063. struct drm_device *dev = crtc->base.dev;
  8064. struct drm_i915_private *dev_priv = to_i915(dev);
  8065. enum pipe pipe = crtc->pipe;
  8066. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8067. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8068. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8069. & ~TU_SIZE_MASK;
  8070. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8071. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8072. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8073. }
  8074. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8075. enum transcoder transcoder,
  8076. struct intel_link_m_n *m_n,
  8077. struct intel_link_m_n *m2_n2)
  8078. {
  8079. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8080. enum pipe pipe = crtc->pipe;
  8081. if (INTEL_GEN(dev_priv) >= 5) {
  8082. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8083. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8084. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8085. & ~TU_SIZE_MASK;
  8086. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8087. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8088. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8089. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8090. * gen < 8) and if DRRS is supported (to make sure the
  8091. * registers are not unnecessarily read).
  8092. */
  8093. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  8094. crtc->config->has_drrs) {
  8095. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8096. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8097. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8098. & ~TU_SIZE_MASK;
  8099. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8100. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8101. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8102. }
  8103. } else {
  8104. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8105. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8106. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8107. & ~TU_SIZE_MASK;
  8108. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8109. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8110. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8111. }
  8112. }
  8113. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8114. struct intel_crtc_state *pipe_config)
  8115. {
  8116. if (pipe_config->has_pch_encoder)
  8117. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8118. else
  8119. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8120. &pipe_config->dp_m_n,
  8121. &pipe_config->dp_m2_n2);
  8122. }
  8123. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8124. struct intel_crtc_state *pipe_config)
  8125. {
  8126. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8127. &pipe_config->fdi_m_n, NULL);
  8128. }
  8129. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8130. struct intel_crtc_state *pipe_config)
  8131. {
  8132. struct drm_device *dev = crtc->base.dev;
  8133. struct drm_i915_private *dev_priv = to_i915(dev);
  8134. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8135. uint32_t ps_ctrl = 0;
  8136. int id = -1;
  8137. int i;
  8138. /* find scaler attached to this pipe */
  8139. for (i = 0; i < crtc->num_scalers; i++) {
  8140. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8141. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8142. id = i;
  8143. pipe_config->pch_pfit.enabled = true;
  8144. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8145. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8146. break;
  8147. }
  8148. }
  8149. scaler_state->scaler_id = id;
  8150. if (id >= 0) {
  8151. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8152. } else {
  8153. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8154. }
  8155. }
  8156. static void
  8157. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8158. struct intel_initial_plane_config *plane_config)
  8159. {
  8160. struct drm_device *dev = crtc->base.dev;
  8161. struct drm_i915_private *dev_priv = to_i915(dev);
  8162. u32 val, base, offset, stride_mult, tiling;
  8163. int pipe = crtc->pipe;
  8164. int fourcc, pixel_format;
  8165. unsigned int aligned_height;
  8166. struct drm_framebuffer *fb;
  8167. struct intel_framebuffer *intel_fb;
  8168. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8169. if (!intel_fb) {
  8170. DRM_DEBUG_KMS("failed to alloc fb\n");
  8171. return;
  8172. }
  8173. fb = &intel_fb->base;
  8174. fb->dev = dev;
  8175. val = I915_READ(PLANE_CTL(pipe, 0));
  8176. if (!(val & PLANE_CTL_ENABLE))
  8177. goto error;
  8178. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8179. fourcc = skl_format_to_fourcc(pixel_format,
  8180. val & PLANE_CTL_ORDER_RGBX,
  8181. val & PLANE_CTL_ALPHA_MASK);
  8182. fb->pixel_format = fourcc;
  8183. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8184. tiling = val & PLANE_CTL_TILED_MASK;
  8185. switch (tiling) {
  8186. case PLANE_CTL_TILED_LINEAR:
  8187. fb->modifier = DRM_FORMAT_MOD_NONE;
  8188. break;
  8189. case PLANE_CTL_TILED_X:
  8190. plane_config->tiling = I915_TILING_X;
  8191. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8192. break;
  8193. case PLANE_CTL_TILED_Y:
  8194. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  8195. break;
  8196. case PLANE_CTL_TILED_YF:
  8197. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  8198. break;
  8199. default:
  8200. MISSING_CASE(tiling);
  8201. goto error;
  8202. }
  8203. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8204. plane_config->base = base;
  8205. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8206. val = I915_READ(PLANE_SIZE(pipe, 0));
  8207. fb->height = ((val >> 16) & 0xfff) + 1;
  8208. fb->width = ((val >> 0) & 0x1fff) + 1;
  8209. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8210. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  8211. fb->pixel_format);
  8212. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8213. aligned_height = intel_fb_align_height(dev, fb->height,
  8214. fb->pixel_format,
  8215. fb->modifier);
  8216. plane_config->size = fb->pitches[0] * aligned_height;
  8217. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8218. pipe_name(pipe), fb->width, fb->height,
  8219. fb->bits_per_pixel, base, fb->pitches[0],
  8220. plane_config->size);
  8221. plane_config->fb = intel_fb;
  8222. return;
  8223. error:
  8224. kfree(intel_fb);
  8225. }
  8226. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8227. struct intel_crtc_state *pipe_config)
  8228. {
  8229. struct drm_device *dev = crtc->base.dev;
  8230. struct drm_i915_private *dev_priv = to_i915(dev);
  8231. uint32_t tmp;
  8232. tmp = I915_READ(PF_CTL(crtc->pipe));
  8233. if (tmp & PF_ENABLE) {
  8234. pipe_config->pch_pfit.enabled = true;
  8235. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8236. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8237. /* We currently do not free assignements of panel fitters on
  8238. * ivb/hsw (since we don't use the higher upscaling modes which
  8239. * differentiates them) so just WARN about this case for now. */
  8240. if (IS_GEN7(dev_priv)) {
  8241. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8242. PF_PIPE_SEL_IVB(crtc->pipe));
  8243. }
  8244. }
  8245. }
  8246. static void
  8247. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8248. struct intel_initial_plane_config *plane_config)
  8249. {
  8250. struct drm_device *dev = crtc->base.dev;
  8251. struct drm_i915_private *dev_priv = to_i915(dev);
  8252. u32 val, base, offset;
  8253. int pipe = crtc->pipe;
  8254. int fourcc, pixel_format;
  8255. unsigned int aligned_height;
  8256. struct drm_framebuffer *fb;
  8257. struct intel_framebuffer *intel_fb;
  8258. val = I915_READ(DSPCNTR(pipe));
  8259. if (!(val & DISPLAY_PLANE_ENABLE))
  8260. return;
  8261. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8262. if (!intel_fb) {
  8263. DRM_DEBUG_KMS("failed to alloc fb\n");
  8264. return;
  8265. }
  8266. fb = &intel_fb->base;
  8267. fb->dev = dev;
  8268. if (INTEL_GEN(dev_priv) >= 4) {
  8269. if (val & DISPPLANE_TILED) {
  8270. plane_config->tiling = I915_TILING_X;
  8271. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8272. }
  8273. }
  8274. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8275. fourcc = i9xx_format_to_fourcc(pixel_format);
  8276. fb->pixel_format = fourcc;
  8277. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8278. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8279. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8280. offset = I915_READ(DSPOFFSET(pipe));
  8281. } else {
  8282. if (plane_config->tiling)
  8283. offset = I915_READ(DSPTILEOFF(pipe));
  8284. else
  8285. offset = I915_READ(DSPLINOFF(pipe));
  8286. }
  8287. plane_config->base = base;
  8288. val = I915_READ(PIPESRC(pipe));
  8289. fb->width = ((val >> 16) & 0xfff) + 1;
  8290. fb->height = ((val >> 0) & 0xfff) + 1;
  8291. val = I915_READ(DSPSTRIDE(pipe));
  8292. fb->pitches[0] = val & 0xffffffc0;
  8293. aligned_height = intel_fb_align_height(dev, fb->height,
  8294. fb->pixel_format,
  8295. fb->modifier);
  8296. plane_config->size = fb->pitches[0] * aligned_height;
  8297. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8298. pipe_name(pipe), fb->width, fb->height,
  8299. fb->bits_per_pixel, base, fb->pitches[0],
  8300. plane_config->size);
  8301. plane_config->fb = intel_fb;
  8302. }
  8303. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8304. struct intel_crtc_state *pipe_config)
  8305. {
  8306. struct drm_device *dev = crtc->base.dev;
  8307. struct drm_i915_private *dev_priv = to_i915(dev);
  8308. enum intel_display_power_domain power_domain;
  8309. uint32_t tmp;
  8310. bool ret;
  8311. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8312. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8313. return false;
  8314. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8315. pipe_config->shared_dpll = NULL;
  8316. ret = false;
  8317. tmp = I915_READ(PIPECONF(crtc->pipe));
  8318. if (!(tmp & PIPECONF_ENABLE))
  8319. goto out;
  8320. switch (tmp & PIPECONF_BPC_MASK) {
  8321. case PIPECONF_6BPC:
  8322. pipe_config->pipe_bpp = 18;
  8323. break;
  8324. case PIPECONF_8BPC:
  8325. pipe_config->pipe_bpp = 24;
  8326. break;
  8327. case PIPECONF_10BPC:
  8328. pipe_config->pipe_bpp = 30;
  8329. break;
  8330. case PIPECONF_12BPC:
  8331. pipe_config->pipe_bpp = 36;
  8332. break;
  8333. default:
  8334. break;
  8335. }
  8336. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8337. pipe_config->limited_color_range = true;
  8338. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8339. struct intel_shared_dpll *pll;
  8340. enum intel_dpll_id pll_id;
  8341. pipe_config->has_pch_encoder = true;
  8342. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8343. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8344. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8345. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8346. if (HAS_PCH_IBX(dev_priv)) {
  8347. /*
  8348. * The pipe->pch transcoder and pch transcoder->pll
  8349. * mapping is fixed.
  8350. */
  8351. pll_id = (enum intel_dpll_id) crtc->pipe;
  8352. } else {
  8353. tmp = I915_READ(PCH_DPLL_SEL);
  8354. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8355. pll_id = DPLL_ID_PCH_PLL_B;
  8356. else
  8357. pll_id= DPLL_ID_PCH_PLL_A;
  8358. }
  8359. pipe_config->shared_dpll =
  8360. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8361. pll = pipe_config->shared_dpll;
  8362. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8363. &pipe_config->dpll_hw_state));
  8364. tmp = pipe_config->dpll_hw_state.dpll;
  8365. pipe_config->pixel_multiplier =
  8366. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8367. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8368. ironlake_pch_clock_get(crtc, pipe_config);
  8369. } else {
  8370. pipe_config->pixel_multiplier = 1;
  8371. }
  8372. intel_get_pipe_timings(crtc, pipe_config);
  8373. intel_get_pipe_src_size(crtc, pipe_config);
  8374. ironlake_get_pfit_config(crtc, pipe_config);
  8375. ret = true;
  8376. out:
  8377. intel_display_power_put(dev_priv, power_domain);
  8378. return ret;
  8379. }
  8380. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8381. {
  8382. struct drm_device *dev = &dev_priv->drm;
  8383. struct intel_crtc *crtc;
  8384. for_each_intel_crtc(dev, crtc)
  8385. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8386. pipe_name(crtc->pipe));
  8387. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8388. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8389. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8390. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8391. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8392. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8393. "CPU PWM1 enabled\n");
  8394. if (IS_HASWELL(dev_priv))
  8395. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8396. "CPU PWM2 enabled\n");
  8397. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8398. "PCH PWM1 enabled\n");
  8399. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8400. "Utility pin enabled\n");
  8401. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8402. /*
  8403. * In theory we can still leave IRQs enabled, as long as only the HPD
  8404. * interrupts remain enabled. We used to check for that, but since it's
  8405. * gen-specific and since we only disable LCPLL after we fully disable
  8406. * the interrupts, the check below should be enough.
  8407. */
  8408. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8409. }
  8410. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8411. {
  8412. if (IS_HASWELL(dev_priv))
  8413. return I915_READ(D_COMP_HSW);
  8414. else
  8415. return I915_READ(D_COMP_BDW);
  8416. }
  8417. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8418. {
  8419. if (IS_HASWELL(dev_priv)) {
  8420. mutex_lock(&dev_priv->rps.hw_lock);
  8421. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8422. val))
  8423. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8424. mutex_unlock(&dev_priv->rps.hw_lock);
  8425. } else {
  8426. I915_WRITE(D_COMP_BDW, val);
  8427. POSTING_READ(D_COMP_BDW);
  8428. }
  8429. }
  8430. /*
  8431. * This function implements pieces of two sequences from BSpec:
  8432. * - Sequence for display software to disable LCPLL
  8433. * - Sequence for display software to allow package C8+
  8434. * The steps implemented here are just the steps that actually touch the LCPLL
  8435. * register. Callers should take care of disabling all the display engine
  8436. * functions, doing the mode unset, fixing interrupts, etc.
  8437. */
  8438. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8439. bool switch_to_fclk, bool allow_power_down)
  8440. {
  8441. uint32_t val;
  8442. assert_can_disable_lcpll(dev_priv);
  8443. val = I915_READ(LCPLL_CTL);
  8444. if (switch_to_fclk) {
  8445. val |= LCPLL_CD_SOURCE_FCLK;
  8446. I915_WRITE(LCPLL_CTL, val);
  8447. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8448. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8449. DRM_ERROR("Switching to FCLK failed\n");
  8450. val = I915_READ(LCPLL_CTL);
  8451. }
  8452. val |= LCPLL_PLL_DISABLE;
  8453. I915_WRITE(LCPLL_CTL, val);
  8454. POSTING_READ(LCPLL_CTL);
  8455. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8456. DRM_ERROR("LCPLL still locked\n");
  8457. val = hsw_read_dcomp(dev_priv);
  8458. val |= D_COMP_COMP_DISABLE;
  8459. hsw_write_dcomp(dev_priv, val);
  8460. ndelay(100);
  8461. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8462. 1))
  8463. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8464. if (allow_power_down) {
  8465. val = I915_READ(LCPLL_CTL);
  8466. val |= LCPLL_POWER_DOWN_ALLOW;
  8467. I915_WRITE(LCPLL_CTL, val);
  8468. POSTING_READ(LCPLL_CTL);
  8469. }
  8470. }
  8471. /*
  8472. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8473. * source.
  8474. */
  8475. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8476. {
  8477. uint32_t val;
  8478. val = I915_READ(LCPLL_CTL);
  8479. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8480. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8481. return;
  8482. /*
  8483. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8484. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8485. */
  8486. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8487. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8488. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8489. I915_WRITE(LCPLL_CTL, val);
  8490. POSTING_READ(LCPLL_CTL);
  8491. }
  8492. val = hsw_read_dcomp(dev_priv);
  8493. val |= D_COMP_COMP_FORCE;
  8494. val &= ~D_COMP_COMP_DISABLE;
  8495. hsw_write_dcomp(dev_priv, val);
  8496. val = I915_READ(LCPLL_CTL);
  8497. val &= ~LCPLL_PLL_DISABLE;
  8498. I915_WRITE(LCPLL_CTL, val);
  8499. if (intel_wait_for_register(dev_priv,
  8500. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8501. 5))
  8502. DRM_ERROR("LCPLL not locked yet\n");
  8503. if (val & LCPLL_CD_SOURCE_FCLK) {
  8504. val = I915_READ(LCPLL_CTL);
  8505. val &= ~LCPLL_CD_SOURCE_FCLK;
  8506. I915_WRITE(LCPLL_CTL, val);
  8507. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8508. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8509. DRM_ERROR("Switching back to LCPLL failed\n");
  8510. }
  8511. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8512. intel_update_cdclk(dev_priv);
  8513. }
  8514. /*
  8515. * Package states C8 and deeper are really deep PC states that can only be
  8516. * reached when all the devices on the system allow it, so even if the graphics
  8517. * device allows PC8+, it doesn't mean the system will actually get to these
  8518. * states. Our driver only allows PC8+ when going into runtime PM.
  8519. *
  8520. * The requirements for PC8+ are that all the outputs are disabled, the power
  8521. * well is disabled and most interrupts are disabled, and these are also
  8522. * requirements for runtime PM. When these conditions are met, we manually do
  8523. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8524. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8525. * hang the machine.
  8526. *
  8527. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8528. * the state of some registers, so when we come back from PC8+ we need to
  8529. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8530. * need to take care of the registers kept by RC6. Notice that this happens even
  8531. * if we don't put the device in PCI D3 state (which is what currently happens
  8532. * because of the runtime PM support).
  8533. *
  8534. * For more, read "Display Sequences for Package C8" on the hardware
  8535. * documentation.
  8536. */
  8537. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8538. {
  8539. struct drm_device *dev = &dev_priv->drm;
  8540. uint32_t val;
  8541. DRM_DEBUG_KMS("Enabling package C8+\n");
  8542. if (HAS_PCH_LPT_LP(dev_priv)) {
  8543. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8544. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8545. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8546. }
  8547. lpt_disable_clkout_dp(dev);
  8548. hsw_disable_lcpll(dev_priv, true, true);
  8549. }
  8550. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8551. {
  8552. struct drm_device *dev = &dev_priv->drm;
  8553. uint32_t val;
  8554. DRM_DEBUG_KMS("Disabling package C8+\n");
  8555. hsw_restore_lcpll(dev_priv);
  8556. lpt_init_pch_refclk(dev);
  8557. if (HAS_PCH_LPT_LP(dev_priv)) {
  8558. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8559. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8560. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8561. }
  8562. }
  8563. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8564. {
  8565. struct drm_device *dev = old_state->dev;
  8566. struct intel_atomic_state *old_intel_state =
  8567. to_intel_atomic_state(old_state);
  8568. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8569. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8570. }
  8571. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8572. int pixel_rate)
  8573. {
  8574. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8575. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8576. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8577. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8578. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8579. * 432 MHz, audio enabled, port width x4, and link rate
  8580. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8581. * screen corruption."
  8582. */
  8583. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8584. crtc_state->has_audio &&
  8585. crtc_state->port_clock >= 540000 &&
  8586. crtc_state->lane_count == 4)
  8587. pixel_rate = max(432000, pixel_rate);
  8588. return pixel_rate;
  8589. }
  8590. /* compute the max rate for new configuration */
  8591. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8592. {
  8593. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8594. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8595. struct drm_crtc *crtc;
  8596. struct drm_crtc_state *cstate;
  8597. struct intel_crtc_state *crtc_state;
  8598. unsigned max_pixel_rate = 0, i;
  8599. enum pipe pipe;
  8600. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8601. sizeof(intel_state->min_pixclk));
  8602. for_each_crtc_in_state(state, crtc, cstate, i) {
  8603. int pixel_rate;
  8604. crtc_state = to_intel_crtc_state(cstate);
  8605. if (!crtc_state->base.enable) {
  8606. intel_state->min_pixclk[i] = 0;
  8607. continue;
  8608. }
  8609. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8610. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8611. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8612. pixel_rate);
  8613. intel_state->min_pixclk[i] = pixel_rate;
  8614. }
  8615. for_each_pipe(dev_priv, pipe)
  8616. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8617. return max_pixel_rate;
  8618. }
  8619. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8620. {
  8621. struct drm_i915_private *dev_priv = to_i915(dev);
  8622. uint32_t val, data;
  8623. int ret;
  8624. if (WARN((I915_READ(LCPLL_CTL) &
  8625. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8626. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8627. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8628. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8629. "trying to change cdclk frequency with cdclk not enabled\n"))
  8630. return;
  8631. mutex_lock(&dev_priv->rps.hw_lock);
  8632. ret = sandybridge_pcode_write(dev_priv,
  8633. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8634. mutex_unlock(&dev_priv->rps.hw_lock);
  8635. if (ret) {
  8636. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8637. return;
  8638. }
  8639. val = I915_READ(LCPLL_CTL);
  8640. val |= LCPLL_CD_SOURCE_FCLK;
  8641. I915_WRITE(LCPLL_CTL, val);
  8642. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8643. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8644. DRM_ERROR("Switching to FCLK failed\n");
  8645. val = I915_READ(LCPLL_CTL);
  8646. val &= ~LCPLL_CLK_FREQ_MASK;
  8647. switch (cdclk) {
  8648. case 450000:
  8649. val |= LCPLL_CLK_FREQ_450;
  8650. data = 0;
  8651. break;
  8652. case 540000:
  8653. val |= LCPLL_CLK_FREQ_54O_BDW;
  8654. data = 1;
  8655. break;
  8656. case 337500:
  8657. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8658. data = 2;
  8659. break;
  8660. case 675000:
  8661. val |= LCPLL_CLK_FREQ_675_BDW;
  8662. data = 3;
  8663. break;
  8664. default:
  8665. WARN(1, "invalid cdclk frequency\n");
  8666. return;
  8667. }
  8668. I915_WRITE(LCPLL_CTL, val);
  8669. val = I915_READ(LCPLL_CTL);
  8670. val &= ~LCPLL_CD_SOURCE_FCLK;
  8671. I915_WRITE(LCPLL_CTL, val);
  8672. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8673. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8674. DRM_ERROR("Switching back to LCPLL failed\n");
  8675. mutex_lock(&dev_priv->rps.hw_lock);
  8676. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8677. mutex_unlock(&dev_priv->rps.hw_lock);
  8678. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8679. intel_update_cdclk(dev_priv);
  8680. WARN(cdclk != dev_priv->cdclk_freq,
  8681. "cdclk requested %d kHz but got %d kHz\n",
  8682. cdclk, dev_priv->cdclk_freq);
  8683. }
  8684. static int broadwell_calc_cdclk(int max_pixclk)
  8685. {
  8686. if (max_pixclk > 540000)
  8687. return 675000;
  8688. else if (max_pixclk > 450000)
  8689. return 540000;
  8690. else if (max_pixclk > 337500)
  8691. return 450000;
  8692. else
  8693. return 337500;
  8694. }
  8695. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8696. {
  8697. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8698. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8699. int max_pixclk = ilk_max_pixel_rate(state);
  8700. int cdclk;
  8701. /*
  8702. * FIXME should also account for plane ratio
  8703. * once 64bpp pixel formats are supported.
  8704. */
  8705. cdclk = broadwell_calc_cdclk(max_pixclk);
  8706. if (cdclk > dev_priv->max_cdclk_freq) {
  8707. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8708. cdclk, dev_priv->max_cdclk_freq);
  8709. return -EINVAL;
  8710. }
  8711. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8712. if (!intel_state->active_crtcs)
  8713. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8714. return 0;
  8715. }
  8716. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8717. {
  8718. struct drm_device *dev = old_state->dev;
  8719. struct intel_atomic_state *old_intel_state =
  8720. to_intel_atomic_state(old_state);
  8721. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8722. broadwell_set_cdclk(dev, req_cdclk);
  8723. }
  8724. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8725. {
  8726. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8727. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8728. const int max_pixclk = ilk_max_pixel_rate(state);
  8729. int vco = intel_state->cdclk_pll_vco;
  8730. int cdclk;
  8731. /*
  8732. * FIXME should also account for plane ratio
  8733. * once 64bpp pixel formats are supported.
  8734. */
  8735. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8736. /*
  8737. * FIXME move the cdclk caclulation to
  8738. * compute_config() so we can fail gracegully.
  8739. */
  8740. if (cdclk > dev_priv->max_cdclk_freq) {
  8741. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8742. cdclk, dev_priv->max_cdclk_freq);
  8743. cdclk = dev_priv->max_cdclk_freq;
  8744. }
  8745. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8746. if (!intel_state->active_crtcs)
  8747. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8748. return 0;
  8749. }
  8750. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8751. {
  8752. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8753. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8754. unsigned int req_cdclk = intel_state->dev_cdclk;
  8755. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8756. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8757. }
  8758. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8759. struct intel_crtc_state *crtc_state)
  8760. {
  8761. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8762. if (!intel_ddi_pll_select(crtc, crtc_state))
  8763. return -EINVAL;
  8764. }
  8765. crtc->lowfreq_avail = false;
  8766. return 0;
  8767. }
  8768. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8769. enum port port,
  8770. struct intel_crtc_state *pipe_config)
  8771. {
  8772. enum intel_dpll_id id;
  8773. switch (port) {
  8774. case PORT_A:
  8775. id = DPLL_ID_SKL_DPLL0;
  8776. break;
  8777. case PORT_B:
  8778. id = DPLL_ID_SKL_DPLL1;
  8779. break;
  8780. case PORT_C:
  8781. id = DPLL_ID_SKL_DPLL2;
  8782. break;
  8783. default:
  8784. DRM_ERROR("Incorrect port type\n");
  8785. return;
  8786. }
  8787. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8788. }
  8789. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8790. enum port port,
  8791. struct intel_crtc_state *pipe_config)
  8792. {
  8793. enum intel_dpll_id id;
  8794. u32 temp;
  8795. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8796. id = temp >> (port * 3 + 1);
  8797. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8798. return;
  8799. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8800. }
  8801. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8802. enum port port,
  8803. struct intel_crtc_state *pipe_config)
  8804. {
  8805. enum intel_dpll_id id;
  8806. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8807. switch (ddi_pll_sel) {
  8808. case PORT_CLK_SEL_WRPLL1:
  8809. id = DPLL_ID_WRPLL1;
  8810. break;
  8811. case PORT_CLK_SEL_WRPLL2:
  8812. id = DPLL_ID_WRPLL2;
  8813. break;
  8814. case PORT_CLK_SEL_SPLL:
  8815. id = DPLL_ID_SPLL;
  8816. break;
  8817. case PORT_CLK_SEL_LCPLL_810:
  8818. id = DPLL_ID_LCPLL_810;
  8819. break;
  8820. case PORT_CLK_SEL_LCPLL_1350:
  8821. id = DPLL_ID_LCPLL_1350;
  8822. break;
  8823. case PORT_CLK_SEL_LCPLL_2700:
  8824. id = DPLL_ID_LCPLL_2700;
  8825. break;
  8826. default:
  8827. MISSING_CASE(ddi_pll_sel);
  8828. /* fall through */
  8829. case PORT_CLK_SEL_NONE:
  8830. return;
  8831. }
  8832. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8833. }
  8834. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8835. struct intel_crtc_state *pipe_config,
  8836. unsigned long *power_domain_mask)
  8837. {
  8838. struct drm_device *dev = crtc->base.dev;
  8839. struct drm_i915_private *dev_priv = to_i915(dev);
  8840. enum intel_display_power_domain power_domain;
  8841. u32 tmp;
  8842. /*
  8843. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8844. * transcoder handled below.
  8845. */
  8846. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8847. /*
  8848. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8849. * consistency and less surprising code; it's in always on power).
  8850. */
  8851. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8852. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8853. enum pipe trans_edp_pipe;
  8854. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8855. default:
  8856. WARN(1, "unknown pipe linked to edp transcoder\n");
  8857. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8858. case TRANS_DDI_EDP_INPUT_A_ON:
  8859. trans_edp_pipe = PIPE_A;
  8860. break;
  8861. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8862. trans_edp_pipe = PIPE_B;
  8863. break;
  8864. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8865. trans_edp_pipe = PIPE_C;
  8866. break;
  8867. }
  8868. if (trans_edp_pipe == crtc->pipe)
  8869. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8870. }
  8871. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8872. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8873. return false;
  8874. *power_domain_mask |= BIT(power_domain);
  8875. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8876. return tmp & PIPECONF_ENABLE;
  8877. }
  8878. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8879. struct intel_crtc_state *pipe_config,
  8880. unsigned long *power_domain_mask)
  8881. {
  8882. struct drm_device *dev = crtc->base.dev;
  8883. struct drm_i915_private *dev_priv = to_i915(dev);
  8884. enum intel_display_power_domain power_domain;
  8885. enum port port;
  8886. enum transcoder cpu_transcoder;
  8887. u32 tmp;
  8888. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8889. if (port == PORT_A)
  8890. cpu_transcoder = TRANSCODER_DSI_A;
  8891. else
  8892. cpu_transcoder = TRANSCODER_DSI_C;
  8893. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8894. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8895. continue;
  8896. *power_domain_mask |= BIT(power_domain);
  8897. /*
  8898. * The PLL needs to be enabled with a valid divider
  8899. * configuration, otherwise accessing DSI registers will hang
  8900. * the machine. See BSpec North Display Engine
  8901. * registers/MIPI[BXT]. We can break out here early, since we
  8902. * need the same DSI PLL to be enabled for both DSI ports.
  8903. */
  8904. if (!intel_dsi_pll_is_enabled(dev_priv))
  8905. break;
  8906. /* XXX: this works for video mode only */
  8907. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8908. if (!(tmp & DPI_ENABLE))
  8909. continue;
  8910. tmp = I915_READ(MIPI_CTRL(port));
  8911. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8912. continue;
  8913. pipe_config->cpu_transcoder = cpu_transcoder;
  8914. break;
  8915. }
  8916. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8917. }
  8918. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8919. struct intel_crtc_state *pipe_config)
  8920. {
  8921. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8922. struct intel_shared_dpll *pll;
  8923. enum port port;
  8924. uint32_t tmp;
  8925. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8926. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8927. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8928. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8929. else if (IS_BROXTON(dev_priv))
  8930. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8931. else
  8932. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8933. pll = pipe_config->shared_dpll;
  8934. if (pll) {
  8935. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8936. &pipe_config->dpll_hw_state));
  8937. }
  8938. /*
  8939. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8940. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8941. * the PCH transcoder is on.
  8942. */
  8943. if (INTEL_GEN(dev_priv) < 9 &&
  8944. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8945. pipe_config->has_pch_encoder = true;
  8946. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8947. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8948. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8949. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8950. }
  8951. }
  8952. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8953. struct intel_crtc_state *pipe_config)
  8954. {
  8955. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8956. enum intel_display_power_domain power_domain;
  8957. unsigned long power_domain_mask;
  8958. bool active;
  8959. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8960. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8961. return false;
  8962. power_domain_mask = BIT(power_domain);
  8963. pipe_config->shared_dpll = NULL;
  8964. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8965. if (IS_BROXTON(dev_priv) &&
  8966. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8967. WARN_ON(active);
  8968. active = true;
  8969. }
  8970. if (!active)
  8971. goto out;
  8972. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8973. haswell_get_ddi_port_state(crtc, pipe_config);
  8974. intel_get_pipe_timings(crtc, pipe_config);
  8975. }
  8976. intel_get_pipe_src_size(crtc, pipe_config);
  8977. pipe_config->gamma_mode =
  8978. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8979. if (INTEL_GEN(dev_priv) >= 9) {
  8980. skl_init_scalers(dev_priv, crtc, pipe_config);
  8981. pipe_config->scaler_state.scaler_id = -1;
  8982. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8983. }
  8984. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8985. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8986. power_domain_mask |= BIT(power_domain);
  8987. if (INTEL_GEN(dev_priv) >= 9)
  8988. skylake_get_pfit_config(crtc, pipe_config);
  8989. else
  8990. ironlake_get_pfit_config(crtc, pipe_config);
  8991. }
  8992. if (IS_HASWELL(dev_priv))
  8993. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8994. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8995. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8996. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8997. pipe_config->pixel_multiplier =
  8998. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8999. } else {
  9000. pipe_config->pixel_multiplier = 1;
  9001. }
  9002. out:
  9003. for_each_power_domain(power_domain, power_domain_mask)
  9004. intel_display_power_put(dev_priv, power_domain);
  9005. return active;
  9006. }
  9007. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9008. const struct intel_plane_state *plane_state)
  9009. {
  9010. struct drm_device *dev = crtc->dev;
  9011. struct drm_i915_private *dev_priv = to_i915(dev);
  9012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9013. uint32_t cntl = 0, size = 0;
  9014. if (plane_state && plane_state->base.visible) {
  9015. unsigned int width = plane_state->base.crtc_w;
  9016. unsigned int height = plane_state->base.crtc_h;
  9017. unsigned int stride = roundup_pow_of_two(width) * 4;
  9018. switch (stride) {
  9019. default:
  9020. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9021. width, stride);
  9022. stride = 256;
  9023. /* fallthrough */
  9024. case 256:
  9025. case 512:
  9026. case 1024:
  9027. case 2048:
  9028. break;
  9029. }
  9030. cntl |= CURSOR_ENABLE |
  9031. CURSOR_GAMMA_ENABLE |
  9032. CURSOR_FORMAT_ARGB |
  9033. CURSOR_STRIDE(stride);
  9034. size = (height << 12) | width;
  9035. }
  9036. if (intel_crtc->cursor_cntl != 0 &&
  9037. (intel_crtc->cursor_base != base ||
  9038. intel_crtc->cursor_size != size ||
  9039. intel_crtc->cursor_cntl != cntl)) {
  9040. /* On these chipsets we can only modify the base/size/stride
  9041. * whilst the cursor is disabled.
  9042. */
  9043. I915_WRITE(CURCNTR(PIPE_A), 0);
  9044. POSTING_READ(CURCNTR(PIPE_A));
  9045. intel_crtc->cursor_cntl = 0;
  9046. }
  9047. if (intel_crtc->cursor_base != base) {
  9048. I915_WRITE(CURBASE(PIPE_A), base);
  9049. intel_crtc->cursor_base = base;
  9050. }
  9051. if (intel_crtc->cursor_size != size) {
  9052. I915_WRITE(CURSIZE, size);
  9053. intel_crtc->cursor_size = size;
  9054. }
  9055. if (intel_crtc->cursor_cntl != cntl) {
  9056. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9057. POSTING_READ(CURCNTR(PIPE_A));
  9058. intel_crtc->cursor_cntl = cntl;
  9059. }
  9060. }
  9061. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9062. const struct intel_plane_state *plane_state)
  9063. {
  9064. struct drm_device *dev = crtc->dev;
  9065. struct drm_i915_private *dev_priv = to_i915(dev);
  9066. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9067. int pipe = intel_crtc->pipe;
  9068. uint32_t cntl = 0;
  9069. if (plane_state && plane_state->base.visible) {
  9070. cntl = MCURSOR_GAMMA_ENABLE;
  9071. switch (plane_state->base.crtc_w) {
  9072. case 64:
  9073. cntl |= CURSOR_MODE_64_ARGB_AX;
  9074. break;
  9075. case 128:
  9076. cntl |= CURSOR_MODE_128_ARGB_AX;
  9077. break;
  9078. case 256:
  9079. cntl |= CURSOR_MODE_256_ARGB_AX;
  9080. break;
  9081. default:
  9082. MISSING_CASE(plane_state->base.crtc_w);
  9083. return;
  9084. }
  9085. cntl |= pipe << 28; /* Connect to correct pipe */
  9086. if (HAS_DDI(dev_priv))
  9087. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9088. if (plane_state->base.rotation & DRM_ROTATE_180)
  9089. cntl |= CURSOR_ROTATE_180;
  9090. }
  9091. if (intel_crtc->cursor_cntl != cntl) {
  9092. I915_WRITE(CURCNTR(pipe), cntl);
  9093. POSTING_READ(CURCNTR(pipe));
  9094. intel_crtc->cursor_cntl = cntl;
  9095. }
  9096. /* and commit changes on next vblank */
  9097. I915_WRITE(CURBASE(pipe), base);
  9098. POSTING_READ(CURBASE(pipe));
  9099. intel_crtc->cursor_base = base;
  9100. }
  9101. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9102. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9103. const struct intel_plane_state *plane_state)
  9104. {
  9105. struct drm_device *dev = crtc->dev;
  9106. struct drm_i915_private *dev_priv = to_i915(dev);
  9107. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9108. int pipe = intel_crtc->pipe;
  9109. u32 base = intel_crtc->cursor_addr;
  9110. u32 pos = 0;
  9111. if (plane_state) {
  9112. int x = plane_state->base.crtc_x;
  9113. int y = plane_state->base.crtc_y;
  9114. if (x < 0) {
  9115. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9116. x = -x;
  9117. }
  9118. pos |= x << CURSOR_X_SHIFT;
  9119. if (y < 0) {
  9120. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9121. y = -y;
  9122. }
  9123. pos |= y << CURSOR_Y_SHIFT;
  9124. /* ILK+ do this automagically */
  9125. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9126. plane_state->base.rotation & DRM_ROTATE_180) {
  9127. base += (plane_state->base.crtc_h *
  9128. plane_state->base.crtc_w - 1) * 4;
  9129. }
  9130. }
  9131. I915_WRITE(CURPOS(pipe), pos);
  9132. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  9133. i845_update_cursor(crtc, base, plane_state);
  9134. else
  9135. i9xx_update_cursor(crtc, base, plane_state);
  9136. }
  9137. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9138. uint32_t width, uint32_t height)
  9139. {
  9140. if (width == 0 || height == 0)
  9141. return false;
  9142. /*
  9143. * 845g/865g are special in that they are only limited by
  9144. * the width of their cursors, the height is arbitrary up to
  9145. * the precision of the register. Everything else requires
  9146. * square cursors, limited to a few power-of-two sizes.
  9147. */
  9148. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  9149. if ((width & 63) != 0)
  9150. return false;
  9151. if (width > (IS_845G(dev_priv) ? 64 : 512))
  9152. return false;
  9153. if (height > 1023)
  9154. return false;
  9155. } else {
  9156. switch (width | height) {
  9157. case 256:
  9158. case 128:
  9159. if (IS_GEN2(dev_priv))
  9160. return false;
  9161. case 64:
  9162. break;
  9163. default:
  9164. return false;
  9165. }
  9166. }
  9167. return true;
  9168. }
  9169. /* VESA 640x480x72Hz mode to set on the pipe */
  9170. static struct drm_display_mode load_detect_mode = {
  9171. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9172. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9173. };
  9174. struct drm_framebuffer *
  9175. __intel_framebuffer_create(struct drm_device *dev,
  9176. struct drm_mode_fb_cmd2 *mode_cmd,
  9177. struct drm_i915_gem_object *obj)
  9178. {
  9179. struct intel_framebuffer *intel_fb;
  9180. int ret;
  9181. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9182. if (!intel_fb)
  9183. return ERR_PTR(-ENOMEM);
  9184. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9185. if (ret)
  9186. goto err;
  9187. return &intel_fb->base;
  9188. err:
  9189. kfree(intel_fb);
  9190. return ERR_PTR(ret);
  9191. }
  9192. static struct drm_framebuffer *
  9193. intel_framebuffer_create(struct drm_device *dev,
  9194. struct drm_mode_fb_cmd2 *mode_cmd,
  9195. struct drm_i915_gem_object *obj)
  9196. {
  9197. struct drm_framebuffer *fb;
  9198. int ret;
  9199. ret = i915_mutex_lock_interruptible(dev);
  9200. if (ret)
  9201. return ERR_PTR(ret);
  9202. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9203. mutex_unlock(&dev->struct_mutex);
  9204. return fb;
  9205. }
  9206. static u32
  9207. intel_framebuffer_pitch_for_width(int width, int bpp)
  9208. {
  9209. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9210. return ALIGN(pitch, 64);
  9211. }
  9212. static u32
  9213. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9214. {
  9215. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9216. return PAGE_ALIGN(pitch * mode->vdisplay);
  9217. }
  9218. static struct drm_framebuffer *
  9219. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9220. struct drm_display_mode *mode,
  9221. int depth, int bpp)
  9222. {
  9223. struct drm_framebuffer *fb;
  9224. struct drm_i915_gem_object *obj;
  9225. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9226. obj = i915_gem_object_create(dev,
  9227. intel_framebuffer_size_for_mode(mode, bpp));
  9228. if (IS_ERR(obj))
  9229. return ERR_CAST(obj);
  9230. mode_cmd.width = mode->hdisplay;
  9231. mode_cmd.height = mode->vdisplay;
  9232. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9233. bpp);
  9234. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9235. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9236. if (IS_ERR(fb))
  9237. i915_gem_object_put(obj);
  9238. return fb;
  9239. }
  9240. static struct drm_framebuffer *
  9241. mode_fits_in_fbdev(struct drm_device *dev,
  9242. struct drm_display_mode *mode)
  9243. {
  9244. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9245. struct drm_i915_private *dev_priv = to_i915(dev);
  9246. struct drm_i915_gem_object *obj;
  9247. struct drm_framebuffer *fb;
  9248. if (!dev_priv->fbdev)
  9249. return NULL;
  9250. if (!dev_priv->fbdev->fb)
  9251. return NULL;
  9252. obj = dev_priv->fbdev->fb->obj;
  9253. BUG_ON(!obj);
  9254. fb = &dev_priv->fbdev->fb->base;
  9255. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9256. fb->bits_per_pixel))
  9257. return NULL;
  9258. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9259. return NULL;
  9260. drm_framebuffer_reference(fb);
  9261. return fb;
  9262. #else
  9263. return NULL;
  9264. #endif
  9265. }
  9266. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9267. struct drm_crtc *crtc,
  9268. struct drm_display_mode *mode,
  9269. struct drm_framebuffer *fb,
  9270. int x, int y)
  9271. {
  9272. struct drm_plane_state *plane_state;
  9273. int hdisplay, vdisplay;
  9274. int ret;
  9275. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9276. if (IS_ERR(plane_state))
  9277. return PTR_ERR(plane_state);
  9278. if (mode)
  9279. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9280. else
  9281. hdisplay = vdisplay = 0;
  9282. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9283. if (ret)
  9284. return ret;
  9285. drm_atomic_set_fb_for_plane(plane_state, fb);
  9286. plane_state->crtc_x = 0;
  9287. plane_state->crtc_y = 0;
  9288. plane_state->crtc_w = hdisplay;
  9289. plane_state->crtc_h = vdisplay;
  9290. plane_state->src_x = x << 16;
  9291. plane_state->src_y = y << 16;
  9292. plane_state->src_w = hdisplay << 16;
  9293. plane_state->src_h = vdisplay << 16;
  9294. return 0;
  9295. }
  9296. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9297. struct drm_display_mode *mode,
  9298. struct intel_load_detect_pipe *old,
  9299. struct drm_modeset_acquire_ctx *ctx)
  9300. {
  9301. struct intel_crtc *intel_crtc;
  9302. struct intel_encoder *intel_encoder =
  9303. intel_attached_encoder(connector);
  9304. struct drm_crtc *possible_crtc;
  9305. struct drm_encoder *encoder = &intel_encoder->base;
  9306. struct drm_crtc *crtc = NULL;
  9307. struct drm_device *dev = encoder->dev;
  9308. struct drm_i915_private *dev_priv = to_i915(dev);
  9309. struct drm_framebuffer *fb;
  9310. struct drm_mode_config *config = &dev->mode_config;
  9311. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9312. struct drm_connector_state *connector_state;
  9313. struct intel_crtc_state *crtc_state;
  9314. int ret, i = -1;
  9315. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9316. connector->base.id, connector->name,
  9317. encoder->base.id, encoder->name);
  9318. old->restore_state = NULL;
  9319. retry:
  9320. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9321. if (ret)
  9322. goto fail;
  9323. /*
  9324. * Algorithm gets a little messy:
  9325. *
  9326. * - if the connector already has an assigned crtc, use it (but make
  9327. * sure it's on first)
  9328. *
  9329. * - try to find the first unused crtc that can drive this connector,
  9330. * and use that if we find one
  9331. */
  9332. /* See if we already have a CRTC for this connector */
  9333. if (connector->state->crtc) {
  9334. crtc = connector->state->crtc;
  9335. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9336. if (ret)
  9337. goto fail;
  9338. /* Make sure the crtc and connector are running */
  9339. goto found;
  9340. }
  9341. /* Find an unused one (if possible) */
  9342. for_each_crtc(dev, possible_crtc) {
  9343. i++;
  9344. if (!(encoder->possible_crtcs & (1 << i)))
  9345. continue;
  9346. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9347. if (ret)
  9348. goto fail;
  9349. if (possible_crtc->state->enable) {
  9350. drm_modeset_unlock(&possible_crtc->mutex);
  9351. continue;
  9352. }
  9353. crtc = possible_crtc;
  9354. break;
  9355. }
  9356. /*
  9357. * If we didn't find an unused CRTC, don't use any.
  9358. */
  9359. if (!crtc) {
  9360. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9361. goto fail;
  9362. }
  9363. found:
  9364. intel_crtc = to_intel_crtc(crtc);
  9365. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9366. if (ret)
  9367. goto fail;
  9368. state = drm_atomic_state_alloc(dev);
  9369. restore_state = drm_atomic_state_alloc(dev);
  9370. if (!state || !restore_state) {
  9371. ret = -ENOMEM;
  9372. goto fail;
  9373. }
  9374. state->acquire_ctx = ctx;
  9375. restore_state->acquire_ctx = ctx;
  9376. connector_state = drm_atomic_get_connector_state(state, connector);
  9377. if (IS_ERR(connector_state)) {
  9378. ret = PTR_ERR(connector_state);
  9379. goto fail;
  9380. }
  9381. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9382. if (ret)
  9383. goto fail;
  9384. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9385. if (IS_ERR(crtc_state)) {
  9386. ret = PTR_ERR(crtc_state);
  9387. goto fail;
  9388. }
  9389. crtc_state->base.active = crtc_state->base.enable = true;
  9390. if (!mode)
  9391. mode = &load_detect_mode;
  9392. /* We need a framebuffer large enough to accommodate all accesses
  9393. * that the plane may generate whilst we perform load detection.
  9394. * We can not rely on the fbcon either being present (we get called
  9395. * during its initialisation to detect all boot displays, or it may
  9396. * not even exist) or that it is large enough to satisfy the
  9397. * requested mode.
  9398. */
  9399. fb = mode_fits_in_fbdev(dev, mode);
  9400. if (fb == NULL) {
  9401. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9402. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9403. } else
  9404. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9405. if (IS_ERR(fb)) {
  9406. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9407. goto fail;
  9408. }
  9409. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9410. if (ret)
  9411. goto fail;
  9412. drm_framebuffer_unreference(fb);
  9413. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9414. if (ret)
  9415. goto fail;
  9416. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9417. if (!ret)
  9418. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9419. if (!ret)
  9420. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9421. if (ret) {
  9422. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9423. goto fail;
  9424. }
  9425. ret = drm_atomic_commit(state);
  9426. if (ret) {
  9427. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9428. goto fail;
  9429. }
  9430. old->restore_state = restore_state;
  9431. /* let the connector get through one full cycle before testing */
  9432. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9433. return true;
  9434. fail:
  9435. if (state) {
  9436. drm_atomic_state_put(state);
  9437. state = NULL;
  9438. }
  9439. if (restore_state) {
  9440. drm_atomic_state_put(restore_state);
  9441. restore_state = NULL;
  9442. }
  9443. if (ret == -EDEADLK) {
  9444. drm_modeset_backoff(ctx);
  9445. goto retry;
  9446. }
  9447. return false;
  9448. }
  9449. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9450. struct intel_load_detect_pipe *old,
  9451. struct drm_modeset_acquire_ctx *ctx)
  9452. {
  9453. struct intel_encoder *intel_encoder =
  9454. intel_attached_encoder(connector);
  9455. struct drm_encoder *encoder = &intel_encoder->base;
  9456. struct drm_atomic_state *state = old->restore_state;
  9457. int ret;
  9458. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9459. connector->base.id, connector->name,
  9460. encoder->base.id, encoder->name);
  9461. if (!state)
  9462. return;
  9463. ret = drm_atomic_commit(state);
  9464. if (ret)
  9465. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9466. drm_atomic_state_put(state);
  9467. }
  9468. static int i9xx_pll_refclk(struct drm_device *dev,
  9469. const struct intel_crtc_state *pipe_config)
  9470. {
  9471. struct drm_i915_private *dev_priv = to_i915(dev);
  9472. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9473. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9474. return dev_priv->vbt.lvds_ssc_freq;
  9475. else if (HAS_PCH_SPLIT(dev_priv))
  9476. return 120000;
  9477. else if (!IS_GEN2(dev_priv))
  9478. return 96000;
  9479. else
  9480. return 48000;
  9481. }
  9482. /* Returns the clock of the currently programmed mode of the given pipe. */
  9483. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9484. struct intel_crtc_state *pipe_config)
  9485. {
  9486. struct drm_device *dev = crtc->base.dev;
  9487. struct drm_i915_private *dev_priv = to_i915(dev);
  9488. int pipe = pipe_config->cpu_transcoder;
  9489. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9490. u32 fp;
  9491. struct dpll clock;
  9492. int port_clock;
  9493. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9494. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9495. fp = pipe_config->dpll_hw_state.fp0;
  9496. else
  9497. fp = pipe_config->dpll_hw_state.fp1;
  9498. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9499. if (IS_PINEVIEW(dev_priv)) {
  9500. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9501. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9502. } else {
  9503. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9504. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9505. }
  9506. if (!IS_GEN2(dev_priv)) {
  9507. if (IS_PINEVIEW(dev_priv))
  9508. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9509. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9510. else
  9511. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9512. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9513. switch (dpll & DPLL_MODE_MASK) {
  9514. case DPLLB_MODE_DAC_SERIAL:
  9515. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9516. 5 : 10;
  9517. break;
  9518. case DPLLB_MODE_LVDS:
  9519. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9520. 7 : 14;
  9521. break;
  9522. default:
  9523. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9524. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9525. return;
  9526. }
  9527. if (IS_PINEVIEW(dev_priv))
  9528. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9529. else
  9530. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9531. } else {
  9532. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9533. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9534. if (is_lvds) {
  9535. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9536. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9537. if (lvds & LVDS_CLKB_POWER_UP)
  9538. clock.p2 = 7;
  9539. else
  9540. clock.p2 = 14;
  9541. } else {
  9542. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9543. clock.p1 = 2;
  9544. else {
  9545. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9546. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9547. }
  9548. if (dpll & PLL_P2_DIVIDE_BY_4)
  9549. clock.p2 = 4;
  9550. else
  9551. clock.p2 = 2;
  9552. }
  9553. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9554. }
  9555. /*
  9556. * This value includes pixel_multiplier. We will use
  9557. * port_clock to compute adjusted_mode.crtc_clock in the
  9558. * encoder's get_config() function.
  9559. */
  9560. pipe_config->port_clock = port_clock;
  9561. }
  9562. int intel_dotclock_calculate(int link_freq,
  9563. const struct intel_link_m_n *m_n)
  9564. {
  9565. /*
  9566. * The calculation for the data clock is:
  9567. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9568. * But we want to avoid losing precison if possible, so:
  9569. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9570. *
  9571. * and the link clock is simpler:
  9572. * link_clock = (m * link_clock) / n
  9573. */
  9574. if (!m_n->link_n)
  9575. return 0;
  9576. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9577. }
  9578. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9579. struct intel_crtc_state *pipe_config)
  9580. {
  9581. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9582. /* read out port_clock from the DPLL */
  9583. i9xx_crtc_clock_get(crtc, pipe_config);
  9584. /*
  9585. * In case there is an active pipe without active ports,
  9586. * we may need some idea for the dotclock anyway.
  9587. * Calculate one based on the FDI configuration.
  9588. */
  9589. pipe_config->base.adjusted_mode.crtc_clock =
  9590. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9591. &pipe_config->fdi_m_n);
  9592. }
  9593. /** Returns the currently programmed mode of the given pipe. */
  9594. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9595. struct drm_crtc *crtc)
  9596. {
  9597. struct drm_i915_private *dev_priv = to_i915(dev);
  9598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9599. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9600. struct drm_display_mode *mode;
  9601. struct intel_crtc_state *pipe_config;
  9602. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9603. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9604. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9605. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9606. enum pipe pipe = intel_crtc->pipe;
  9607. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9608. if (!mode)
  9609. return NULL;
  9610. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9611. if (!pipe_config) {
  9612. kfree(mode);
  9613. return NULL;
  9614. }
  9615. /*
  9616. * Construct a pipe_config sufficient for getting the clock info
  9617. * back out of crtc_clock_get.
  9618. *
  9619. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9620. * to use a real value here instead.
  9621. */
  9622. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9623. pipe_config->pixel_multiplier = 1;
  9624. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9625. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9626. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9627. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9628. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9629. mode->hdisplay = (htot & 0xffff) + 1;
  9630. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9631. mode->hsync_start = (hsync & 0xffff) + 1;
  9632. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9633. mode->vdisplay = (vtot & 0xffff) + 1;
  9634. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9635. mode->vsync_start = (vsync & 0xffff) + 1;
  9636. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9637. drm_mode_set_name(mode);
  9638. kfree(pipe_config);
  9639. return mode;
  9640. }
  9641. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9642. {
  9643. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9644. struct drm_device *dev = crtc->dev;
  9645. struct intel_flip_work *work;
  9646. spin_lock_irq(&dev->event_lock);
  9647. work = intel_crtc->flip_work;
  9648. intel_crtc->flip_work = NULL;
  9649. spin_unlock_irq(&dev->event_lock);
  9650. if (work) {
  9651. cancel_work_sync(&work->mmio_work);
  9652. cancel_work_sync(&work->unpin_work);
  9653. kfree(work);
  9654. }
  9655. drm_crtc_cleanup(crtc);
  9656. kfree(intel_crtc);
  9657. }
  9658. static void intel_unpin_work_fn(struct work_struct *__work)
  9659. {
  9660. struct intel_flip_work *work =
  9661. container_of(__work, struct intel_flip_work, unpin_work);
  9662. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9663. struct drm_device *dev = crtc->base.dev;
  9664. struct drm_plane *primary = crtc->base.primary;
  9665. if (is_mmio_work(work))
  9666. flush_work(&work->mmio_work);
  9667. mutex_lock(&dev->struct_mutex);
  9668. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9669. i915_gem_object_put(work->pending_flip_obj);
  9670. mutex_unlock(&dev->struct_mutex);
  9671. i915_gem_request_put(work->flip_queued_req);
  9672. intel_frontbuffer_flip_complete(to_i915(dev),
  9673. to_intel_plane(primary)->frontbuffer_bit);
  9674. intel_fbc_post_update(crtc);
  9675. drm_framebuffer_unreference(work->old_fb);
  9676. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9677. atomic_dec(&crtc->unpin_work_count);
  9678. kfree(work);
  9679. }
  9680. /* Is 'a' after or equal to 'b'? */
  9681. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9682. {
  9683. return !((a - b) & 0x80000000);
  9684. }
  9685. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9686. struct intel_flip_work *work)
  9687. {
  9688. struct drm_device *dev = crtc->base.dev;
  9689. struct drm_i915_private *dev_priv = to_i915(dev);
  9690. if (abort_flip_on_reset(crtc))
  9691. return true;
  9692. /*
  9693. * The relevant registers doen't exist on pre-ctg.
  9694. * As the flip done interrupt doesn't trigger for mmio
  9695. * flips on gmch platforms, a flip count check isn't
  9696. * really needed there. But since ctg has the registers,
  9697. * include it in the check anyway.
  9698. */
  9699. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9700. return true;
  9701. /*
  9702. * BDW signals flip done immediately if the plane
  9703. * is disabled, even if the plane enable is already
  9704. * armed to occur at the next vblank :(
  9705. */
  9706. /*
  9707. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9708. * used the same base address. In that case the mmio flip might
  9709. * have completed, but the CS hasn't even executed the flip yet.
  9710. *
  9711. * A flip count check isn't enough as the CS might have updated
  9712. * the base address just after start of vblank, but before we
  9713. * managed to process the interrupt. This means we'd complete the
  9714. * CS flip too soon.
  9715. *
  9716. * Combining both checks should get us a good enough result. It may
  9717. * still happen that the CS flip has been executed, but has not
  9718. * yet actually completed. But in case the base address is the same
  9719. * anyway, we don't really care.
  9720. */
  9721. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9722. crtc->flip_work->gtt_offset &&
  9723. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9724. crtc->flip_work->flip_count);
  9725. }
  9726. static bool
  9727. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9728. struct intel_flip_work *work)
  9729. {
  9730. /*
  9731. * MMIO work completes when vblank is different from
  9732. * flip_queued_vblank.
  9733. *
  9734. * Reset counter value doesn't matter, this is handled by
  9735. * i915_wait_request finishing early, so no need to handle
  9736. * reset here.
  9737. */
  9738. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9739. }
  9740. static bool pageflip_finished(struct intel_crtc *crtc,
  9741. struct intel_flip_work *work)
  9742. {
  9743. if (!atomic_read(&work->pending))
  9744. return false;
  9745. smp_rmb();
  9746. if (is_mmio_work(work))
  9747. return __pageflip_finished_mmio(crtc, work);
  9748. else
  9749. return __pageflip_finished_cs(crtc, work);
  9750. }
  9751. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9752. {
  9753. struct drm_device *dev = &dev_priv->drm;
  9754. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9755. struct intel_flip_work *work;
  9756. unsigned long flags;
  9757. /* Ignore early vblank irqs */
  9758. if (!crtc)
  9759. return;
  9760. /*
  9761. * This is called both by irq handlers and the reset code (to complete
  9762. * lost pageflips) so needs the full irqsave spinlocks.
  9763. */
  9764. spin_lock_irqsave(&dev->event_lock, flags);
  9765. work = crtc->flip_work;
  9766. if (work != NULL &&
  9767. !is_mmio_work(work) &&
  9768. pageflip_finished(crtc, work))
  9769. page_flip_completed(crtc);
  9770. spin_unlock_irqrestore(&dev->event_lock, flags);
  9771. }
  9772. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9773. {
  9774. struct drm_device *dev = &dev_priv->drm;
  9775. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9776. struct intel_flip_work *work;
  9777. unsigned long flags;
  9778. /* Ignore early vblank irqs */
  9779. if (!crtc)
  9780. return;
  9781. /*
  9782. * This is called both by irq handlers and the reset code (to complete
  9783. * lost pageflips) so needs the full irqsave spinlocks.
  9784. */
  9785. spin_lock_irqsave(&dev->event_lock, flags);
  9786. work = crtc->flip_work;
  9787. if (work != NULL &&
  9788. is_mmio_work(work) &&
  9789. pageflip_finished(crtc, work))
  9790. page_flip_completed(crtc);
  9791. spin_unlock_irqrestore(&dev->event_lock, flags);
  9792. }
  9793. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9794. struct intel_flip_work *work)
  9795. {
  9796. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9797. /* Ensure that the work item is consistent when activating it ... */
  9798. smp_mb__before_atomic();
  9799. atomic_set(&work->pending, 1);
  9800. }
  9801. static int intel_gen2_queue_flip(struct drm_device *dev,
  9802. struct drm_crtc *crtc,
  9803. struct drm_framebuffer *fb,
  9804. struct drm_i915_gem_object *obj,
  9805. struct drm_i915_gem_request *req,
  9806. uint32_t flags)
  9807. {
  9808. struct intel_ring *ring = req->ring;
  9809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9810. u32 flip_mask;
  9811. int ret;
  9812. ret = intel_ring_begin(req, 6);
  9813. if (ret)
  9814. return ret;
  9815. /* Can't queue multiple flips, so wait for the previous
  9816. * one to finish before executing the next.
  9817. */
  9818. if (intel_crtc->plane)
  9819. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9820. else
  9821. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9822. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9823. intel_ring_emit(ring, MI_NOOP);
  9824. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9825. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9826. intel_ring_emit(ring, fb->pitches[0]);
  9827. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9828. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9829. return 0;
  9830. }
  9831. static int intel_gen3_queue_flip(struct drm_device *dev,
  9832. struct drm_crtc *crtc,
  9833. struct drm_framebuffer *fb,
  9834. struct drm_i915_gem_object *obj,
  9835. struct drm_i915_gem_request *req,
  9836. uint32_t flags)
  9837. {
  9838. struct intel_ring *ring = req->ring;
  9839. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9840. u32 flip_mask;
  9841. int ret;
  9842. ret = intel_ring_begin(req, 6);
  9843. if (ret)
  9844. return ret;
  9845. if (intel_crtc->plane)
  9846. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9847. else
  9848. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9849. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9850. intel_ring_emit(ring, MI_NOOP);
  9851. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9852. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9853. intel_ring_emit(ring, fb->pitches[0]);
  9854. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9855. intel_ring_emit(ring, MI_NOOP);
  9856. return 0;
  9857. }
  9858. static int intel_gen4_queue_flip(struct drm_device *dev,
  9859. struct drm_crtc *crtc,
  9860. struct drm_framebuffer *fb,
  9861. struct drm_i915_gem_object *obj,
  9862. struct drm_i915_gem_request *req,
  9863. uint32_t flags)
  9864. {
  9865. struct intel_ring *ring = req->ring;
  9866. struct drm_i915_private *dev_priv = to_i915(dev);
  9867. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9868. uint32_t pf, pipesrc;
  9869. int ret;
  9870. ret = intel_ring_begin(req, 4);
  9871. if (ret)
  9872. return ret;
  9873. /* i965+ uses the linear or tiled offsets from the
  9874. * Display Registers (which do not change across a page-flip)
  9875. * so we need only reprogram the base address.
  9876. */
  9877. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9878. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9879. intel_ring_emit(ring, fb->pitches[0]);
  9880. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9881. intel_fb_modifier_to_tiling(fb->modifier));
  9882. /* XXX Enabling the panel-fitter across page-flip is so far
  9883. * untested on non-native modes, so ignore it for now.
  9884. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9885. */
  9886. pf = 0;
  9887. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9888. intel_ring_emit(ring, pf | pipesrc);
  9889. return 0;
  9890. }
  9891. static int intel_gen6_queue_flip(struct drm_device *dev,
  9892. struct drm_crtc *crtc,
  9893. struct drm_framebuffer *fb,
  9894. struct drm_i915_gem_object *obj,
  9895. struct drm_i915_gem_request *req,
  9896. uint32_t flags)
  9897. {
  9898. struct intel_ring *ring = req->ring;
  9899. struct drm_i915_private *dev_priv = to_i915(dev);
  9900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9901. uint32_t pf, pipesrc;
  9902. int ret;
  9903. ret = intel_ring_begin(req, 4);
  9904. if (ret)
  9905. return ret;
  9906. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9907. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9908. intel_ring_emit(ring, fb->pitches[0] |
  9909. intel_fb_modifier_to_tiling(fb->modifier));
  9910. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9911. /* Contrary to the suggestions in the documentation,
  9912. * "Enable Panel Fitter" does not seem to be required when page
  9913. * flipping with a non-native mode, and worse causes a normal
  9914. * modeset to fail.
  9915. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9916. */
  9917. pf = 0;
  9918. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9919. intel_ring_emit(ring, pf | pipesrc);
  9920. return 0;
  9921. }
  9922. static int intel_gen7_queue_flip(struct drm_device *dev,
  9923. struct drm_crtc *crtc,
  9924. struct drm_framebuffer *fb,
  9925. struct drm_i915_gem_object *obj,
  9926. struct drm_i915_gem_request *req,
  9927. uint32_t flags)
  9928. {
  9929. struct drm_i915_private *dev_priv = to_i915(dev);
  9930. struct intel_ring *ring = req->ring;
  9931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9932. uint32_t plane_bit = 0;
  9933. int len, ret;
  9934. switch (intel_crtc->plane) {
  9935. case PLANE_A:
  9936. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9937. break;
  9938. case PLANE_B:
  9939. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9940. break;
  9941. case PLANE_C:
  9942. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9943. break;
  9944. default:
  9945. WARN_ONCE(1, "unknown plane in flip command\n");
  9946. return -ENODEV;
  9947. }
  9948. len = 4;
  9949. if (req->engine->id == RCS) {
  9950. len += 6;
  9951. /*
  9952. * On Gen 8, SRM is now taking an extra dword to accommodate
  9953. * 48bits addresses, and we need a NOOP for the batch size to
  9954. * stay even.
  9955. */
  9956. if (IS_GEN8(dev_priv))
  9957. len += 2;
  9958. }
  9959. /*
  9960. * BSpec MI_DISPLAY_FLIP for IVB:
  9961. * "The full packet must be contained within the same cache line."
  9962. *
  9963. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9964. * cacheline, if we ever start emitting more commands before
  9965. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9966. * then do the cacheline alignment, and finally emit the
  9967. * MI_DISPLAY_FLIP.
  9968. */
  9969. ret = intel_ring_cacheline_align(req);
  9970. if (ret)
  9971. return ret;
  9972. ret = intel_ring_begin(req, len);
  9973. if (ret)
  9974. return ret;
  9975. /* Unmask the flip-done completion message. Note that the bspec says that
  9976. * we should do this for both the BCS and RCS, and that we must not unmask
  9977. * more than one flip event at any time (or ensure that one flip message
  9978. * can be sent by waiting for flip-done prior to queueing new flips).
  9979. * Experimentation says that BCS works despite DERRMR masking all
  9980. * flip-done completion events and that unmasking all planes at once
  9981. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9982. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9983. */
  9984. if (req->engine->id == RCS) {
  9985. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9986. intel_ring_emit_reg(ring, DERRMR);
  9987. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9988. DERRMR_PIPEB_PRI_FLIP_DONE |
  9989. DERRMR_PIPEC_PRI_FLIP_DONE));
  9990. if (IS_GEN8(dev_priv))
  9991. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9992. MI_SRM_LRM_GLOBAL_GTT);
  9993. else
  9994. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9995. MI_SRM_LRM_GLOBAL_GTT);
  9996. intel_ring_emit_reg(ring, DERRMR);
  9997. intel_ring_emit(ring,
  9998. i915_ggtt_offset(req->engine->scratch) + 256);
  9999. if (IS_GEN8(dev_priv)) {
  10000. intel_ring_emit(ring, 0);
  10001. intel_ring_emit(ring, MI_NOOP);
  10002. }
  10003. }
  10004. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10005. intel_ring_emit(ring, fb->pitches[0] |
  10006. intel_fb_modifier_to_tiling(fb->modifier));
  10007. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10008. intel_ring_emit(ring, (MI_NOOP));
  10009. return 0;
  10010. }
  10011. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10012. struct drm_i915_gem_object *obj)
  10013. {
  10014. /*
  10015. * This is not being used for older platforms, because
  10016. * non-availability of flip done interrupt forces us to use
  10017. * CS flips. Older platforms derive flip done using some clever
  10018. * tricks involving the flip_pending status bits and vblank irqs.
  10019. * So using MMIO flips there would disrupt this mechanism.
  10020. */
  10021. if (engine == NULL)
  10022. return true;
  10023. if (INTEL_GEN(engine->i915) < 5)
  10024. return false;
  10025. if (i915.use_mmio_flip < 0)
  10026. return false;
  10027. else if (i915.use_mmio_flip > 0)
  10028. return true;
  10029. else if (i915.enable_execlists)
  10030. return true;
  10031. return engine != i915_gem_object_last_write_engine(obj);
  10032. }
  10033. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10034. unsigned int rotation,
  10035. struct intel_flip_work *work)
  10036. {
  10037. struct drm_device *dev = intel_crtc->base.dev;
  10038. struct drm_i915_private *dev_priv = to_i915(dev);
  10039. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10040. const enum pipe pipe = intel_crtc->pipe;
  10041. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10042. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10043. ctl &= ~PLANE_CTL_TILED_MASK;
  10044. switch (fb->modifier) {
  10045. case DRM_FORMAT_MOD_NONE:
  10046. break;
  10047. case I915_FORMAT_MOD_X_TILED:
  10048. ctl |= PLANE_CTL_TILED_X;
  10049. break;
  10050. case I915_FORMAT_MOD_Y_TILED:
  10051. ctl |= PLANE_CTL_TILED_Y;
  10052. break;
  10053. case I915_FORMAT_MOD_Yf_TILED:
  10054. ctl |= PLANE_CTL_TILED_YF;
  10055. break;
  10056. default:
  10057. MISSING_CASE(fb->modifier);
  10058. }
  10059. /*
  10060. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10061. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10062. */
  10063. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10064. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10065. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10066. POSTING_READ(PLANE_SURF(pipe, 0));
  10067. }
  10068. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10069. struct intel_flip_work *work)
  10070. {
  10071. struct drm_device *dev = intel_crtc->base.dev;
  10072. struct drm_i915_private *dev_priv = to_i915(dev);
  10073. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10074. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10075. u32 dspcntr;
  10076. dspcntr = I915_READ(reg);
  10077. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  10078. dspcntr |= DISPPLANE_TILED;
  10079. else
  10080. dspcntr &= ~DISPPLANE_TILED;
  10081. I915_WRITE(reg, dspcntr);
  10082. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10083. POSTING_READ(DSPSURF(intel_crtc->plane));
  10084. }
  10085. static void intel_mmio_flip_work_func(struct work_struct *w)
  10086. {
  10087. struct intel_flip_work *work =
  10088. container_of(w, struct intel_flip_work, mmio_work);
  10089. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10090. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10091. struct intel_framebuffer *intel_fb =
  10092. to_intel_framebuffer(crtc->base.primary->fb);
  10093. struct drm_i915_gem_object *obj = intel_fb->obj;
  10094. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10095. intel_pipe_update_start(crtc);
  10096. if (INTEL_GEN(dev_priv) >= 9)
  10097. skl_do_mmio_flip(crtc, work->rotation, work);
  10098. else
  10099. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10100. ilk_do_mmio_flip(crtc, work);
  10101. intel_pipe_update_end(crtc, work);
  10102. }
  10103. static int intel_default_queue_flip(struct drm_device *dev,
  10104. struct drm_crtc *crtc,
  10105. struct drm_framebuffer *fb,
  10106. struct drm_i915_gem_object *obj,
  10107. struct drm_i915_gem_request *req,
  10108. uint32_t flags)
  10109. {
  10110. return -ENODEV;
  10111. }
  10112. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10113. struct intel_crtc *intel_crtc,
  10114. struct intel_flip_work *work)
  10115. {
  10116. u32 addr, vblank;
  10117. if (!atomic_read(&work->pending))
  10118. return false;
  10119. smp_rmb();
  10120. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10121. if (work->flip_ready_vblank == 0) {
  10122. if (work->flip_queued_req &&
  10123. !i915_gem_request_completed(work->flip_queued_req))
  10124. return false;
  10125. work->flip_ready_vblank = vblank;
  10126. }
  10127. if (vblank - work->flip_ready_vblank < 3)
  10128. return false;
  10129. /* Potential stall - if we see that the flip has happened,
  10130. * assume a missed interrupt. */
  10131. if (INTEL_GEN(dev_priv) >= 4)
  10132. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10133. else
  10134. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10135. /* There is a potential issue here with a false positive after a flip
  10136. * to the same address. We could address this by checking for a
  10137. * non-incrementing frame counter.
  10138. */
  10139. return addr == work->gtt_offset;
  10140. }
  10141. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10142. {
  10143. struct drm_device *dev = &dev_priv->drm;
  10144. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10145. struct intel_flip_work *work;
  10146. WARN_ON(!in_interrupt());
  10147. if (crtc == NULL)
  10148. return;
  10149. spin_lock(&dev->event_lock);
  10150. work = crtc->flip_work;
  10151. if (work != NULL && !is_mmio_work(work) &&
  10152. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10153. WARN_ONCE(1,
  10154. "Kicking stuck page flip: queued at %d, now %d\n",
  10155. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10156. page_flip_completed(crtc);
  10157. work = NULL;
  10158. }
  10159. if (work != NULL && !is_mmio_work(work) &&
  10160. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10161. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10162. spin_unlock(&dev->event_lock);
  10163. }
  10164. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10165. struct drm_framebuffer *fb,
  10166. struct drm_pending_vblank_event *event,
  10167. uint32_t page_flip_flags)
  10168. {
  10169. struct drm_device *dev = crtc->dev;
  10170. struct drm_i915_private *dev_priv = to_i915(dev);
  10171. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10172. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10173. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10174. struct drm_plane *primary = crtc->primary;
  10175. enum pipe pipe = intel_crtc->pipe;
  10176. struct intel_flip_work *work;
  10177. struct intel_engine_cs *engine;
  10178. bool mmio_flip;
  10179. struct drm_i915_gem_request *request;
  10180. struct i915_vma *vma;
  10181. int ret;
  10182. /*
  10183. * drm_mode_page_flip_ioctl() should already catch this, but double
  10184. * check to be safe. In the future we may enable pageflipping from
  10185. * a disabled primary plane.
  10186. */
  10187. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10188. return -EBUSY;
  10189. /* Can't change pixel format via MI display flips. */
  10190. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10191. return -EINVAL;
  10192. /*
  10193. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10194. * Note that pitch changes could also affect these register.
  10195. */
  10196. if (INTEL_GEN(dev_priv) > 3 &&
  10197. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10198. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10199. return -EINVAL;
  10200. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10201. goto out_hang;
  10202. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10203. if (work == NULL)
  10204. return -ENOMEM;
  10205. work->event = event;
  10206. work->crtc = crtc;
  10207. work->old_fb = old_fb;
  10208. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10209. ret = drm_crtc_vblank_get(crtc);
  10210. if (ret)
  10211. goto free_work;
  10212. /* We borrow the event spin lock for protecting flip_work */
  10213. spin_lock_irq(&dev->event_lock);
  10214. if (intel_crtc->flip_work) {
  10215. /* Before declaring the flip queue wedged, check if
  10216. * the hardware completed the operation behind our backs.
  10217. */
  10218. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10219. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10220. page_flip_completed(intel_crtc);
  10221. } else {
  10222. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10223. spin_unlock_irq(&dev->event_lock);
  10224. drm_crtc_vblank_put(crtc);
  10225. kfree(work);
  10226. return -EBUSY;
  10227. }
  10228. }
  10229. intel_crtc->flip_work = work;
  10230. spin_unlock_irq(&dev->event_lock);
  10231. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10232. flush_workqueue(dev_priv->wq);
  10233. /* Reference the objects for the scheduled work. */
  10234. drm_framebuffer_reference(work->old_fb);
  10235. crtc->primary->fb = fb;
  10236. update_state_fb(crtc->primary);
  10237. work->pending_flip_obj = i915_gem_object_get(obj);
  10238. ret = i915_mutex_lock_interruptible(dev);
  10239. if (ret)
  10240. goto cleanup;
  10241. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10242. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10243. ret = -EIO;
  10244. goto unlock;
  10245. }
  10246. atomic_inc(&intel_crtc->unpin_work_count);
  10247. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10248. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10249. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10250. engine = dev_priv->engine[BCS];
  10251. if (fb->modifier != old_fb->modifier)
  10252. /* vlv: DISPLAY_FLIP fails to change tiling */
  10253. engine = NULL;
  10254. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10255. engine = dev_priv->engine[BCS];
  10256. } else if (INTEL_GEN(dev_priv) >= 7) {
  10257. engine = i915_gem_object_last_write_engine(obj);
  10258. if (engine == NULL || engine->id != RCS)
  10259. engine = dev_priv->engine[BCS];
  10260. } else {
  10261. engine = dev_priv->engine[RCS];
  10262. }
  10263. mmio_flip = use_mmio_flip(engine, obj);
  10264. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10265. if (IS_ERR(vma)) {
  10266. ret = PTR_ERR(vma);
  10267. goto cleanup_pending;
  10268. }
  10269. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10270. work->gtt_offset += intel_crtc->dspaddr_offset;
  10271. work->rotation = crtc->primary->state->rotation;
  10272. /*
  10273. * There's the potential that the next frame will not be compatible with
  10274. * FBC, so we want to call pre_update() before the actual page flip.
  10275. * The problem is that pre_update() caches some information about the fb
  10276. * object, so we want to do this only after the object is pinned. Let's
  10277. * be on the safe side and do this immediately before scheduling the
  10278. * flip.
  10279. */
  10280. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10281. to_intel_plane_state(primary->state));
  10282. if (mmio_flip) {
  10283. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10284. queue_work(system_unbound_wq, &work->mmio_work);
  10285. } else {
  10286. request = i915_gem_request_alloc(engine, engine->last_context);
  10287. if (IS_ERR(request)) {
  10288. ret = PTR_ERR(request);
  10289. goto cleanup_unpin;
  10290. }
  10291. ret = i915_gem_request_await_object(request, obj, false);
  10292. if (ret)
  10293. goto cleanup_request;
  10294. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10295. page_flip_flags);
  10296. if (ret)
  10297. goto cleanup_request;
  10298. intel_mark_page_flip_active(intel_crtc, work);
  10299. work->flip_queued_req = i915_gem_request_get(request);
  10300. i915_add_request_no_flush(request);
  10301. }
  10302. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10303. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10304. to_intel_plane(primary)->frontbuffer_bit);
  10305. mutex_unlock(&dev->struct_mutex);
  10306. intel_frontbuffer_flip_prepare(to_i915(dev),
  10307. to_intel_plane(primary)->frontbuffer_bit);
  10308. trace_i915_flip_request(intel_crtc->plane, obj);
  10309. return 0;
  10310. cleanup_request:
  10311. i915_add_request_no_flush(request);
  10312. cleanup_unpin:
  10313. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10314. cleanup_pending:
  10315. atomic_dec(&intel_crtc->unpin_work_count);
  10316. unlock:
  10317. mutex_unlock(&dev->struct_mutex);
  10318. cleanup:
  10319. crtc->primary->fb = old_fb;
  10320. update_state_fb(crtc->primary);
  10321. i915_gem_object_put(obj);
  10322. drm_framebuffer_unreference(work->old_fb);
  10323. spin_lock_irq(&dev->event_lock);
  10324. intel_crtc->flip_work = NULL;
  10325. spin_unlock_irq(&dev->event_lock);
  10326. drm_crtc_vblank_put(crtc);
  10327. free_work:
  10328. kfree(work);
  10329. if (ret == -EIO) {
  10330. struct drm_atomic_state *state;
  10331. struct drm_plane_state *plane_state;
  10332. out_hang:
  10333. state = drm_atomic_state_alloc(dev);
  10334. if (!state)
  10335. return -ENOMEM;
  10336. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10337. retry:
  10338. plane_state = drm_atomic_get_plane_state(state, primary);
  10339. ret = PTR_ERR_OR_ZERO(plane_state);
  10340. if (!ret) {
  10341. drm_atomic_set_fb_for_plane(plane_state, fb);
  10342. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10343. if (!ret)
  10344. ret = drm_atomic_commit(state);
  10345. }
  10346. if (ret == -EDEADLK) {
  10347. drm_modeset_backoff(state->acquire_ctx);
  10348. drm_atomic_state_clear(state);
  10349. goto retry;
  10350. }
  10351. drm_atomic_state_put(state);
  10352. if (ret == 0 && event) {
  10353. spin_lock_irq(&dev->event_lock);
  10354. drm_crtc_send_vblank_event(crtc, event);
  10355. spin_unlock_irq(&dev->event_lock);
  10356. }
  10357. }
  10358. return ret;
  10359. }
  10360. /**
  10361. * intel_wm_need_update - Check whether watermarks need updating
  10362. * @plane: drm plane
  10363. * @state: new plane state
  10364. *
  10365. * Check current plane state versus the new one to determine whether
  10366. * watermarks need to be recalculated.
  10367. *
  10368. * Returns true or false.
  10369. */
  10370. static bool intel_wm_need_update(struct drm_plane *plane,
  10371. struct drm_plane_state *state)
  10372. {
  10373. struct intel_plane_state *new = to_intel_plane_state(state);
  10374. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10375. /* Update watermarks on tiling or size changes. */
  10376. if (new->base.visible != cur->base.visible)
  10377. return true;
  10378. if (!cur->base.fb || !new->base.fb)
  10379. return false;
  10380. if (cur->base.fb->modifier != new->base.fb->modifier ||
  10381. cur->base.rotation != new->base.rotation ||
  10382. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10383. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10384. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10385. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10386. return true;
  10387. return false;
  10388. }
  10389. static bool needs_scaling(struct intel_plane_state *state)
  10390. {
  10391. int src_w = drm_rect_width(&state->base.src) >> 16;
  10392. int src_h = drm_rect_height(&state->base.src) >> 16;
  10393. int dst_w = drm_rect_width(&state->base.dst);
  10394. int dst_h = drm_rect_height(&state->base.dst);
  10395. return (src_w != dst_w || src_h != dst_h);
  10396. }
  10397. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10398. struct drm_plane_state *plane_state)
  10399. {
  10400. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10401. struct drm_crtc *crtc = crtc_state->crtc;
  10402. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10403. struct drm_plane *plane = plane_state->plane;
  10404. struct drm_device *dev = crtc->dev;
  10405. struct drm_i915_private *dev_priv = to_i915(dev);
  10406. struct intel_plane_state *old_plane_state =
  10407. to_intel_plane_state(plane->state);
  10408. bool mode_changed = needs_modeset(crtc_state);
  10409. bool was_crtc_enabled = crtc->state->active;
  10410. bool is_crtc_enabled = crtc_state->active;
  10411. bool turn_off, turn_on, visible, was_visible;
  10412. struct drm_framebuffer *fb = plane_state->fb;
  10413. int ret;
  10414. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10415. ret = skl_update_scaler_plane(
  10416. to_intel_crtc_state(crtc_state),
  10417. to_intel_plane_state(plane_state));
  10418. if (ret)
  10419. return ret;
  10420. }
  10421. was_visible = old_plane_state->base.visible;
  10422. visible = to_intel_plane_state(plane_state)->base.visible;
  10423. if (!was_crtc_enabled && WARN_ON(was_visible))
  10424. was_visible = false;
  10425. /*
  10426. * Visibility is calculated as if the crtc was on, but
  10427. * after scaler setup everything depends on it being off
  10428. * when the crtc isn't active.
  10429. *
  10430. * FIXME this is wrong for watermarks. Watermarks should also
  10431. * be computed as if the pipe would be active. Perhaps move
  10432. * per-plane wm computation to the .check_plane() hook, and
  10433. * only combine the results from all planes in the current place?
  10434. */
  10435. if (!is_crtc_enabled)
  10436. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10437. if (!was_visible && !visible)
  10438. return 0;
  10439. if (fb != old_plane_state->base.fb)
  10440. pipe_config->fb_changed = true;
  10441. turn_off = was_visible && (!visible || mode_changed);
  10442. turn_on = visible && (!was_visible || mode_changed);
  10443. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10444. intel_crtc->base.base.id,
  10445. intel_crtc->base.name,
  10446. plane->base.id, plane->name,
  10447. fb ? fb->base.id : -1);
  10448. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10449. plane->base.id, plane->name,
  10450. was_visible, visible,
  10451. turn_off, turn_on, mode_changed);
  10452. if (turn_on) {
  10453. pipe_config->update_wm_pre = true;
  10454. /* must disable cxsr around plane enable/disable */
  10455. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10456. pipe_config->disable_cxsr = true;
  10457. } else if (turn_off) {
  10458. pipe_config->update_wm_post = true;
  10459. /* must disable cxsr around plane enable/disable */
  10460. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10461. pipe_config->disable_cxsr = true;
  10462. } else if (intel_wm_need_update(plane, plane_state)) {
  10463. /* FIXME bollocks */
  10464. pipe_config->update_wm_pre = true;
  10465. pipe_config->update_wm_post = true;
  10466. }
  10467. /* Pre-gen9 platforms need two-step watermark updates */
  10468. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10469. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  10470. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10471. if (visible || was_visible)
  10472. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10473. /*
  10474. * WaCxSRDisabledForSpriteScaling:ivb
  10475. *
  10476. * cstate->update_wm was already set above, so this flag will
  10477. * take effect when we commit and program watermarks.
  10478. */
  10479. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10480. needs_scaling(to_intel_plane_state(plane_state)) &&
  10481. !needs_scaling(old_plane_state))
  10482. pipe_config->disable_lp_wm = true;
  10483. return 0;
  10484. }
  10485. static bool encoders_cloneable(const struct intel_encoder *a,
  10486. const struct intel_encoder *b)
  10487. {
  10488. /* masks could be asymmetric, so check both ways */
  10489. return a == b || (a->cloneable & (1 << b->type) &&
  10490. b->cloneable & (1 << a->type));
  10491. }
  10492. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10493. struct intel_crtc *crtc,
  10494. struct intel_encoder *encoder)
  10495. {
  10496. struct intel_encoder *source_encoder;
  10497. struct drm_connector *connector;
  10498. struct drm_connector_state *connector_state;
  10499. int i;
  10500. for_each_connector_in_state(state, connector, connector_state, i) {
  10501. if (connector_state->crtc != &crtc->base)
  10502. continue;
  10503. source_encoder =
  10504. to_intel_encoder(connector_state->best_encoder);
  10505. if (!encoders_cloneable(encoder, source_encoder))
  10506. return false;
  10507. }
  10508. return true;
  10509. }
  10510. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10511. struct drm_crtc_state *crtc_state)
  10512. {
  10513. struct drm_device *dev = crtc->dev;
  10514. struct drm_i915_private *dev_priv = to_i915(dev);
  10515. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10516. struct intel_crtc_state *pipe_config =
  10517. to_intel_crtc_state(crtc_state);
  10518. struct drm_atomic_state *state = crtc_state->state;
  10519. int ret;
  10520. bool mode_changed = needs_modeset(crtc_state);
  10521. if (mode_changed && !crtc_state->active)
  10522. pipe_config->update_wm_post = true;
  10523. if (mode_changed && crtc_state->enable &&
  10524. dev_priv->display.crtc_compute_clock &&
  10525. !WARN_ON(pipe_config->shared_dpll)) {
  10526. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10527. pipe_config);
  10528. if (ret)
  10529. return ret;
  10530. }
  10531. if (crtc_state->color_mgmt_changed) {
  10532. ret = intel_color_check(crtc, crtc_state);
  10533. if (ret)
  10534. return ret;
  10535. /*
  10536. * Changing color management on Intel hardware is
  10537. * handled as part of planes update.
  10538. */
  10539. crtc_state->planes_changed = true;
  10540. }
  10541. ret = 0;
  10542. if (dev_priv->display.compute_pipe_wm) {
  10543. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10544. if (ret) {
  10545. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10546. return ret;
  10547. }
  10548. }
  10549. if (dev_priv->display.compute_intermediate_wm &&
  10550. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10551. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10552. return 0;
  10553. /*
  10554. * Calculate 'intermediate' watermarks that satisfy both the
  10555. * old state and the new state. We can program these
  10556. * immediately.
  10557. */
  10558. ret = dev_priv->display.compute_intermediate_wm(dev,
  10559. intel_crtc,
  10560. pipe_config);
  10561. if (ret) {
  10562. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10563. return ret;
  10564. }
  10565. } else if (dev_priv->display.compute_intermediate_wm) {
  10566. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10567. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10568. }
  10569. if (INTEL_GEN(dev_priv) >= 9) {
  10570. if (mode_changed)
  10571. ret = skl_update_scaler_crtc(pipe_config);
  10572. if (!ret)
  10573. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10574. pipe_config);
  10575. }
  10576. return ret;
  10577. }
  10578. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10579. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10580. .atomic_begin = intel_begin_crtc_commit,
  10581. .atomic_flush = intel_finish_crtc_commit,
  10582. .atomic_check = intel_crtc_atomic_check,
  10583. };
  10584. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10585. {
  10586. struct intel_connector *connector;
  10587. for_each_intel_connector(dev, connector) {
  10588. if (connector->base.state->crtc)
  10589. drm_connector_unreference(&connector->base);
  10590. if (connector->base.encoder) {
  10591. connector->base.state->best_encoder =
  10592. connector->base.encoder;
  10593. connector->base.state->crtc =
  10594. connector->base.encoder->crtc;
  10595. drm_connector_reference(&connector->base);
  10596. } else {
  10597. connector->base.state->best_encoder = NULL;
  10598. connector->base.state->crtc = NULL;
  10599. }
  10600. }
  10601. }
  10602. static void
  10603. connected_sink_compute_bpp(struct intel_connector *connector,
  10604. struct intel_crtc_state *pipe_config)
  10605. {
  10606. const struct drm_display_info *info = &connector->base.display_info;
  10607. int bpp = pipe_config->pipe_bpp;
  10608. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10609. connector->base.base.id,
  10610. connector->base.name);
  10611. /* Don't use an invalid EDID bpc value */
  10612. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10613. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10614. bpp, info->bpc * 3);
  10615. pipe_config->pipe_bpp = info->bpc * 3;
  10616. }
  10617. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10618. if (info->bpc == 0 && bpp > 24) {
  10619. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10620. bpp);
  10621. pipe_config->pipe_bpp = 24;
  10622. }
  10623. }
  10624. static int
  10625. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10626. struct intel_crtc_state *pipe_config)
  10627. {
  10628. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10629. struct drm_atomic_state *state;
  10630. struct drm_connector *connector;
  10631. struct drm_connector_state *connector_state;
  10632. int bpp, i;
  10633. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10634. IS_CHERRYVIEW(dev_priv)))
  10635. bpp = 10*3;
  10636. else if (INTEL_GEN(dev_priv) >= 5)
  10637. bpp = 12*3;
  10638. else
  10639. bpp = 8*3;
  10640. pipe_config->pipe_bpp = bpp;
  10641. state = pipe_config->base.state;
  10642. /* Clamp display bpp to EDID value */
  10643. for_each_connector_in_state(state, connector, connector_state, i) {
  10644. if (connector_state->crtc != &crtc->base)
  10645. continue;
  10646. connected_sink_compute_bpp(to_intel_connector(connector),
  10647. pipe_config);
  10648. }
  10649. return bpp;
  10650. }
  10651. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10652. {
  10653. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10654. "type: 0x%x flags: 0x%x\n",
  10655. mode->crtc_clock,
  10656. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10657. mode->crtc_hsync_end, mode->crtc_htotal,
  10658. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10659. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10660. }
  10661. static inline void
  10662. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  10663. unsigned int lane_count, struct intel_link_m_n *m_n)
  10664. {
  10665. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10666. id, lane_count,
  10667. m_n->gmch_m, m_n->gmch_n,
  10668. m_n->link_m, m_n->link_n, m_n->tu);
  10669. }
  10670. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10671. struct intel_crtc_state *pipe_config,
  10672. const char *context)
  10673. {
  10674. struct drm_device *dev = crtc->base.dev;
  10675. struct drm_i915_private *dev_priv = to_i915(dev);
  10676. struct drm_plane *plane;
  10677. struct intel_plane *intel_plane;
  10678. struct intel_plane_state *state;
  10679. struct drm_framebuffer *fb;
  10680. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  10681. crtc->base.base.id, crtc->base.name, context);
  10682. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  10683. transcoder_name(pipe_config->cpu_transcoder),
  10684. pipe_config->pipe_bpp, pipe_config->dither);
  10685. if (pipe_config->has_pch_encoder)
  10686. intel_dump_m_n_config(pipe_config, "fdi",
  10687. pipe_config->fdi_lanes,
  10688. &pipe_config->fdi_m_n);
  10689. if (intel_crtc_has_dp_encoder(pipe_config)) {
  10690. intel_dump_m_n_config(pipe_config, "dp m_n",
  10691. pipe_config->lane_count, &pipe_config->dp_m_n);
  10692. if (pipe_config->has_drrs)
  10693. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  10694. pipe_config->lane_count,
  10695. &pipe_config->dp_m2_n2);
  10696. }
  10697. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10698. pipe_config->has_audio, pipe_config->has_infoframe);
  10699. DRM_DEBUG_KMS("requested mode:\n");
  10700. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10701. DRM_DEBUG_KMS("adjusted mode:\n");
  10702. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10703. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10704. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
  10705. pipe_config->port_clock,
  10706. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10707. if (INTEL_GEN(dev_priv) >= 9)
  10708. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10709. crtc->num_scalers,
  10710. pipe_config->scaler_state.scaler_users,
  10711. pipe_config->scaler_state.scaler_id);
  10712. if (HAS_GMCH_DISPLAY(dev_priv))
  10713. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10714. pipe_config->gmch_pfit.control,
  10715. pipe_config->gmch_pfit.pgm_ratios,
  10716. pipe_config->gmch_pfit.lvds_border_bits);
  10717. else
  10718. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10719. pipe_config->pch_pfit.pos,
  10720. pipe_config->pch_pfit.size,
  10721. enableddisabled(pipe_config->pch_pfit.enabled));
  10722. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  10723. pipe_config->ips_enabled, pipe_config->double_wide);
  10724. if (IS_BROXTON(dev_priv)) {
  10725. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10726. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10727. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10728. pipe_config->dpll_hw_state.ebb0,
  10729. pipe_config->dpll_hw_state.ebb4,
  10730. pipe_config->dpll_hw_state.pll0,
  10731. pipe_config->dpll_hw_state.pll1,
  10732. pipe_config->dpll_hw_state.pll2,
  10733. pipe_config->dpll_hw_state.pll3,
  10734. pipe_config->dpll_hw_state.pll6,
  10735. pipe_config->dpll_hw_state.pll8,
  10736. pipe_config->dpll_hw_state.pll9,
  10737. pipe_config->dpll_hw_state.pll10,
  10738. pipe_config->dpll_hw_state.pcsdw12);
  10739. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  10740. DRM_DEBUG_KMS("dpll_hw_state: "
  10741. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10742. pipe_config->dpll_hw_state.ctrl1,
  10743. pipe_config->dpll_hw_state.cfgcr1,
  10744. pipe_config->dpll_hw_state.cfgcr2);
  10745. } else if (HAS_DDI(dev_priv)) {
  10746. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10747. pipe_config->dpll_hw_state.wrpll,
  10748. pipe_config->dpll_hw_state.spll);
  10749. } else {
  10750. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10751. "fp0: 0x%x, fp1: 0x%x\n",
  10752. pipe_config->dpll_hw_state.dpll,
  10753. pipe_config->dpll_hw_state.dpll_md,
  10754. pipe_config->dpll_hw_state.fp0,
  10755. pipe_config->dpll_hw_state.fp1);
  10756. }
  10757. DRM_DEBUG_KMS("planes on this crtc\n");
  10758. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10759. struct drm_format_name_buf format_name;
  10760. intel_plane = to_intel_plane(plane);
  10761. if (intel_plane->pipe != crtc->pipe)
  10762. continue;
  10763. state = to_intel_plane_state(plane->state);
  10764. fb = state->base.fb;
  10765. if (!fb) {
  10766. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10767. plane->base.id, plane->name, state->scaler_id);
  10768. continue;
  10769. }
  10770. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  10771. plane->base.id, plane->name,
  10772. fb->base.id, fb->width, fb->height,
  10773. drm_get_format_name(fb->pixel_format, &format_name));
  10774. if (INTEL_GEN(dev_priv) >= 9)
  10775. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10776. state->scaler_id,
  10777. state->base.src.x1 >> 16,
  10778. state->base.src.y1 >> 16,
  10779. drm_rect_width(&state->base.src) >> 16,
  10780. drm_rect_height(&state->base.src) >> 16,
  10781. state->base.dst.x1, state->base.dst.y1,
  10782. drm_rect_width(&state->base.dst),
  10783. drm_rect_height(&state->base.dst));
  10784. }
  10785. }
  10786. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10787. {
  10788. struct drm_device *dev = state->dev;
  10789. struct drm_connector *connector;
  10790. unsigned int used_ports = 0;
  10791. unsigned int used_mst_ports = 0;
  10792. /*
  10793. * Walk the connector list instead of the encoder
  10794. * list to detect the problem on ddi platforms
  10795. * where there's just one encoder per digital port.
  10796. */
  10797. drm_for_each_connector(connector, dev) {
  10798. struct drm_connector_state *connector_state;
  10799. struct intel_encoder *encoder;
  10800. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10801. if (!connector_state)
  10802. connector_state = connector->state;
  10803. if (!connector_state->best_encoder)
  10804. continue;
  10805. encoder = to_intel_encoder(connector_state->best_encoder);
  10806. WARN_ON(!connector_state->crtc);
  10807. switch (encoder->type) {
  10808. unsigned int port_mask;
  10809. case INTEL_OUTPUT_UNKNOWN:
  10810. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10811. break;
  10812. case INTEL_OUTPUT_DP:
  10813. case INTEL_OUTPUT_HDMI:
  10814. case INTEL_OUTPUT_EDP:
  10815. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10816. /* the same port mustn't appear more than once */
  10817. if (used_ports & port_mask)
  10818. return false;
  10819. used_ports |= port_mask;
  10820. break;
  10821. case INTEL_OUTPUT_DP_MST:
  10822. used_mst_ports |=
  10823. 1 << enc_to_mst(&encoder->base)->primary->port;
  10824. break;
  10825. default:
  10826. break;
  10827. }
  10828. }
  10829. /* can't mix MST and SST/HDMI on the same port */
  10830. if (used_ports & used_mst_ports)
  10831. return false;
  10832. return true;
  10833. }
  10834. static void
  10835. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10836. {
  10837. struct drm_crtc_state tmp_state;
  10838. struct intel_crtc_scaler_state scaler_state;
  10839. struct intel_dpll_hw_state dpll_hw_state;
  10840. struct intel_shared_dpll *shared_dpll;
  10841. bool force_thru;
  10842. /* FIXME: before the switch to atomic started, a new pipe_config was
  10843. * kzalloc'd. Code that depends on any field being zero should be
  10844. * fixed, so that the crtc_state can be safely duplicated. For now,
  10845. * only fields that are know to not cause problems are preserved. */
  10846. tmp_state = crtc_state->base;
  10847. scaler_state = crtc_state->scaler_state;
  10848. shared_dpll = crtc_state->shared_dpll;
  10849. dpll_hw_state = crtc_state->dpll_hw_state;
  10850. force_thru = crtc_state->pch_pfit.force_thru;
  10851. memset(crtc_state, 0, sizeof *crtc_state);
  10852. crtc_state->base = tmp_state;
  10853. crtc_state->scaler_state = scaler_state;
  10854. crtc_state->shared_dpll = shared_dpll;
  10855. crtc_state->dpll_hw_state = dpll_hw_state;
  10856. crtc_state->pch_pfit.force_thru = force_thru;
  10857. }
  10858. static int
  10859. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10860. struct intel_crtc_state *pipe_config)
  10861. {
  10862. struct drm_atomic_state *state = pipe_config->base.state;
  10863. struct intel_encoder *encoder;
  10864. struct drm_connector *connector;
  10865. struct drm_connector_state *connector_state;
  10866. int base_bpp, ret = -EINVAL;
  10867. int i;
  10868. bool retry = true;
  10869. clear_intel_crtc_state(pipe_config);
  10870. pipe_config->cpu_transcoder =
  10871. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10872. /*
  10873. * Sanitize sync polarity flags based on requested ones. If neither
  10874. * positive or negative polarity is requested, treat this as meaning
  10875. * negative polarity.
  10876. */
  10877. if (!(pipe_config->base.adjusted_mode.flags &
  10878. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10879. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10880. if (!(pipe_config->base.adjusted_mode.flags &
  10881. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10882. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10883. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10884. pipe_config);
  10885. if (base_bpp < 0)
  10886. goto fail;
  10887. /*
  10888. * Determine the real pipe dimensions. Note that stereo modes can
  10889. * increase the actual pipe size due to the frame doubling and
  10890. * insertion of additional space for blanks between the frame. This
  10891. * is stored in the crtc timings. We use the requested mode to do this
  10892. * computation to clearly distinguish it from the adjusted mode, which
  10893. * can be changed by the connectors in the below retry loop.
  10894. */
  10895. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10896. &pipe_config->pipe_src_w,
  10897. &pipe_config->pipe_src_h);
  10898. for_each_connector_in_state(state, connector, connector_state, i) {
  10899. if (connector_state->crtc != crtc)
  10900. continue;
  10901. encoder = to_intel_encoder(connector_state->best_encoder);
  10902. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10903. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10904. goto fail;
  10905. }
  10906. /*
  10907. * Determine output_types before calling the .compute_config()
  10908. * hooks so that the hooks can use this information safely.
  10909. */
  10910. pipe_config->output_types |= 1 << encoder->type;
  10911. }
  10912. encoder_retry:
  10913. /* Ensure the port clock defaults are reset when retrying. */
  10914. pipe_config->port_clock = 0;
  10915. pipe_config->pixel_multiplier = 1;
  10916. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10917. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10918. CRTC_STEREO_DOUBLE);
  10919. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10920. * adjust it according to limitations or connector properties, and also
  10921. * a chance to reject the mode entirely.
  10922. */
  10923. for_each_connector_in_state(state, connector, connector_state, i) {
  10924. if (connector_state->crtc != crtc)
  10925. continue;
  10926. encoder = to_intel_encoder(connector_state->best_encoder);
  10927. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10928. DRM_DEBUG_KMS("Encoder config failure\n");
  10929. goto fail;
  10930. }
  10931. }
  10932. /* Set default port clock if not overwritten by the encoder. Needs to be
  10933. * done afterwards in case the encoder adjusts the mode. */
  10934. if (!pipe_config->port_clock)
  10935. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10936. * pipe_config->pixel_multiplier;
  10937. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10938. if (ret < 0) {
  10939. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10940. goto fail;
  10941. }
  10942. if (ret == RETRY) {
  10943. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10944. ret = -EINVAL;
  10945. goto fail;
  10946. }
  10947. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10948. retry = false;
  10949. goto encoder_retry;
  10950. }
  10951. /* Dithering seems to not pass-through bits correctly when it should, so
  10952. * only enable it on 6bpc panels. */
  10953. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10954. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10955. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10956. fail:
  10957. return ret;
  10958. }
  10959. static void
  10960. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10961. {
  10962. struct drm_crtc *crtc;
  10963. struct drm_crtc_state *crtc_state;
  10964. int i;
  10965. /* Double check state. */
  10966. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10967. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10968. /* Update hwmode for vblank functions */
  10969. if (crtc->state->active)
  10970. crtc->hwmode = crtc->state->adjusted_mode;
  10971. else
  10972. crtc->hwmode.crtc_clock = 0;
  10973. /*
  10974. * Update legacy state to satisfy fbc code. This can
  10975. * be removed when fbc uses the atomic state.
  10976. */
  10977. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10978. struct drm_plane_state *plane_state = crtc->primary->state;
  10979. crtc->primary->fb = plane_state->fb;
  10980. crtc->x = plane_state->src_x >> 16;
  10981. crtc->y = plane_state->src_y >> 16;
  10982. }
  10983. }
  10984. }
  10985. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10986. {
  10987. int diff;
  10988. if (clock1 == clock2)
  10989. return true;
  10990. if (!clock1 || !clock2)
  10991. return false;
  10992. diff = abs(clock1 - clock2);
  10993. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10994. return true;
  10995. return false;
  10996. }
  10997. static bool
  10998. intel_compare_m_n(unsigned int m, unsigned int n,
  10999. unsigned int m2, unsigned int n2,
  11000. bool exact)
  11001. {
  11002. if (m == m2 && n == n2)
  11003. return true;
  11004. if (exact || !m || !n || !m2 || !n2)
  11005. return false;
  11006. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11007. if (n > n2) {
  11008. while (n > n2) {
  11009. m2 <<= 1;
  11010. n2 <<= 1;
  11011. }
  11012. } else if (n < n2) {
  11013. while (n < n2) {
  11014. m <<= 1;
  11015. n <<= 1;
  11016. }
  11017. }
  11018. if (n != n2)
  11019. return false;
  11020. return intel_fuzzy_clock_check(m, m2);
  11021. }
  11022. static bool
  11023. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11024. struct intel_link_m_n *m2_n2,
  11025. bool adjust)
  11026. {
  11027. if (m_n->tu == m2_n2->tu &&
  11028. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11029. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11030. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11031. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11032. if (adjust)
  11033. *m2_n2 = *m_n;
  11034. return true;
  11035. }
  11036. return false;
  11037. }
  11038. static bool
  11039. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  11040. struct intel_crtc_state *current_config,
  11041. struct intel_crtc_state *pipe_config,
  11042. bool adjust)
  11043. {
  11044. bool ret = true;
  11045. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  11046. do { \
  11047. if (!adjust) \
  11048. DRM_ERROR(fmt, ##__VA_ARGS__); \
  11049. else \
  11050. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  11051. } while (0)
  11052. #define PIPE_CONF_CHECK_X(name) \
  11053. if (current_config->name != pipe_config->name) { \
  11054. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11055. "(expected 0x%08x, found 0x%08x)\n", \
  11056. current_config->name, \
  11057. pipe_config->name); \
  11058. ret = false; \
  11059. }
  11060. #define PIPE_CONF_CHECK_I(name) \
  11061. if (current_config->name != pipe_config->name) { \
  11062. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11063. "(expected %i, found %i)\n", \
  11064. current_config->name, \
  11065. pipe_config->name); \
  11066. ret = false; \
  11067. }
  11068. #define PIPE_CONF_CHECK_P(name) \
  11069. if (current_config->name != pipe_config->name) { \
  11070. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11071. "(expected %p, found %p)\n", \
  11072. current_config->name, \
  11073. pipe_config->name); \
  11074. ret = false; \
  11075. }
  11076. #define PIPE_CONF_CHECK_M_N(name) \
  11077. if (!intel_compare_link_m_n(&current_config->name, \
  11078. &pipe_config->name,\
  11079. adjust)) { \
  11080. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11081. "(expected tu %i gmch %i/%i link %i/%i, " \
  11082. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11083. current_config->name.tu, \
  11084. current_config->name.gmch_m, \
  11085. current_config->name.gmch_n, \
  11086. current_config->name.link_m, \
  11087. current_config->name.link_n, \
  11088. pipe_config->name.tu, \
  11089. pipe_config->name.gmch_m, \
  11090. pipe_config->name.gmch_n, \
  11091. pipe_config->name.link_m, \
  11092. pipe_config->name.link_n); \
  11093. ret = false; \
  11094. }
  11095. /* This is required for BDW+ where there is only one set of registers for
  11096. * switching between high and low RR.
  11097. * This macro can be used whenever a comparison has to be made between one
  11098. * hw state and multiple sw state variables.
  11099. */
  11100. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11101. if (!intel_compare_link_m_n(&current_config->name, \
  11102. &pipe_config->name, adjust) && \
  11103. !intel_compare_link_m_n(&current_config->alt_name, \
  11104. &pipe_config->name, adjust)) { \
  11105. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11106. "(expected tu %i gmch %i/%i link %i/%i, " \
  11107. "or tu %i gmch %i/%i link %i/%i, " \
  11108. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11109. current_config->name.tu, \
  11110. current_config->name.gmch_m, \
  11111. current_config->name.gmch_n, \
  11112. current_config->name.link_m, \
  11113. current_config->name.link_n, \
  11114. current_config->alt_name.tu, \
  11115. current_config->alt_name.gmch_m, \
  11116. current_config->alt_name.gmch_n, \
  11117. current_config->alt_name.link_m, \
  11118. current_config->alt_name.link_n, \
  11119. pipe_config->name.tu, \
  11120. pipe_config->name.gmch_m, \
  11121. pipe_config->name.gmch_n, \
  11122. pipe_config->name.link_m, \
  11123. pipe_config->name.link_n); \
  11124. ret = false; \
  11125. }
  11126. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11127. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11128. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  11129. "(expected %i, found %i)\n", \
  11130. current_config->name & (mask), \
  11131. pipe_config->name & (mask)); \
  11132. ret = false; \
  11133. }
  11134. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11135. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11136. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11137. "(expected %i, found %i)\n", \
  11138. current_config->name, \
  11139. pipe_config->name); \
  11140. ret = false; \
  11141. }
  11142. #define PIPE_CONF_QUIRK(quirk) \
  11143. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11144. PIPE_CONF_CHECK_I(cpu_transcoder);
  11145. PIPE_CONF_CHECK_I(has_pch_encoder);
  11146. PIPE_CONF_CHECK_I(fdi_lanes);
  11147. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11148. PIPE_CONF_CHECK_I(lane_count);
  11149. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11150. if (INTEL_GEN(dev_priv) < 8) {
  11151. PIPE_CONF_CHECK_M_N(dp_m_n);
  11152. if (current_config->has_drrs)
  11153. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11154. } else
  11155. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11156. PIPE_CONF_CHECK_X(output_types);
  11157. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11158. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11159. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11160. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11161. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11162. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11163. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11164. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11165. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11166. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11167. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11168. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11169. PIPE_CONF_CHECK_I(pixel_multiplier);
  11170. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11171. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11172. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11173. PIPE_CONF_CHECK_I(limited_color_range);
  11174. PIPE_CONF_CHECK_I(has_infoframe);
  11175. PIPE_CONF_CHECK_I(has_audio);
  11176. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11177. DRM_MODE_FLAG_INTERLACE);
  11178. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11179. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11180. DRM_MODE_FLAG_PHSYNC);
  11181. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11182. DRM_MODE_FLAG_NHSYNC);
  11183. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11184. DRM_MODE_FLAG_PVSYNC);
  11185. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11186. DRM_MODE_FLAG_NVSYNC);
  11187. }
  11188. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11189. /* pfit ratios are autocomputed by the hw on gen4+ */
  11190. if (INTEL_GEN(dev_priv) < 4)
  11191. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11192. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11193. if (!adjust) {
  11194. PIPE_CONF_CHECK_I(pipe_src_w);
  11195. PIPE_CONF_CHECK_I(pipe_src_h);
  11196. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11197. if (current_config->pch_pfit.enabled) {
  11198. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11199. PIPE_CONF_CHECK_X(pch_pfit.size);
  11200. }
  11201. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11202. }
  11203. /* BDW+ don't expose a synchronous way to read the state */
  11204. if (IS_HASWELL(dev_priv))
  11205. PIPE_CONF_CHECK_I(ips_enabled);
  11206. PIPE_CONF_CHECK_I(double_wide);
  11207. PIPE_CONF_CHECK_P(shared_dpll);
  11208. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11209. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11210. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11211. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11212. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11213. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11214. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11215. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11216. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11217. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11218. PIPE_CONF_CHECK_X(dsi_pll.div);
  11219. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11220. PIPE_CONF_CHECK_I(pipe_bpp);
  11221. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11222. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11223. #undef PIPE_CONF_CHECK_X
  11224. #undef PIPE_CONF_CHECK_I
  11225. #undef PIPE_CONF_CHECK_P
  11226. #undef PIPE_CONF_CHECK_FLAGS
  11227. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11228. #undef PIPE_CONF_QUIRK
  11229. #undef INTEL_ERR_OR_DBG_KMS
  11230. return ret;
  11231. }
  11232. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11233. const struct intel_crtc_state *pipe_config)
  11234. {
  11235. if (pipe_config->has_pch_encoder) {
  11236. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11237. &pipe_config->fdi_m_n);
  11238. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11239. /*
  11240. * FDI already provided one idea for the dotclock.
  11241. * Yell if the encoder disagrees.
  11242. */
  11243. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11244. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11245. fdi_dotclock, dotclock);
  11246. }
  11247. }
  11248. static void verify_wm_state(struct drm_crtc *crtc,
  11249. struct drm_crtc_state *new_state)
  11250. {
  11251. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11252. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11253. struct skl_pipe_wm hw_wm, *sw_wm;
  11254. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11255. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11257. const enum pipe pipe = intel_crtc->pipe;
  11258. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11259. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  11260. return;
  11261. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11262. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11263. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11264. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11265. /* planes */
  11266. for_each_universal_plane(dev_priv, pipe, plane) {
  11267. hw_plane_wm = &hw_wm.planes[plane];
  11268. sw_plane_wm = &sw_wm->planes[plane];
  11269. /* Watermarks */
  11270. for (level = 0; level <= max_level; level++) {
  11271. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11272. &sw_plane_wm->wm[level]))
  11273. continue;
  11274. DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11275. pipe_name(pipe), plane + 1, level,
  11276. sw_plane_wm->wm[level].plane_en,
  11277. sw_plane_wm->wm[level].plane_res_b,
  11278. sw_plane_wm->wm[level].plane_res_l,
  11279. hw_plane_wm->wm[level].plane_en,
  11280. hw_plane_wm->wm[level].plane_res_b,
  11281. hw_plane_wm->wm[level].plane_res_l);
  11282. }
  11283. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11284. &sw_plane_wm->trans_wm)) {
  11285. DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11286. pipe_name(pipe), plane + 1,
  11287. sw_plane_wm->trans_wm.plane_en,
  11288. sw_plane_wm->trans_wm.plane_res_b,
  11289. sw_plane_wm->trans_wm.plane_res_l,
  11290. hw_plane_wm->trans_wm.plane_en,
  11291. hw_plane_wm->trans_wm.plane_res_b,
  11292. hw_plane_wm->trans_wm.plane_res_l);
  11293. }
  11294. /* DDB */
  11295. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11296. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11297. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11298. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11299. pipe_name(pipe), plane + 1,
  11300. sw_ddb_entry->start, sw_ddb_entry->end,
  11301. hw_ddb_entry->start, hw_ddb_entry->end);
  11302. }
  11303. }
  11304. /*
  11305. * cursor
  11306. * If the cursor plane isn't active, we may not have updated it's ddb
  11307. * allocation. In that case since the ddb allocation will be updated
  11308. * once the plane becomes visible, we can skip this check
  11309. */
  11310. if (intel_crtc->cursor_addr) {
  11311. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11312. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11313. /* Watermarks */
  11314. for (level = 0; level <= max_level; level++) {
  11315. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11316. &sw_plane_wm->wm[level]))
  11317. continue;
  11318. DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11319. pipe_name(pipe), level,
  11320. sw_plane_wm->wm[level].plane_en,
  11321. sw_plane_wm->wm[level].plane_res_b,
  11322. sw_plane_wm->wm[level].plane_res_l,
  11323. hw_plane_wm->wm[level].plane_en,
  11324. hw_plane_wm->wm[level].plane_res_b,
  11325. hw_plane_wm->wm[level].plane_res_l);
  11326. }
  11327. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11328. &sw_plane_wm->trans_wm)) {
  11329. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11330. pipe_name(pipe),
  11331. sw_plane_wm->trans_wm.plane_en,
  11332. sw_plane_wm->trans_wm.plane_res_b,
  11333. sw_plane_wm->trans_wm.plane_res_l,
  11334. hw_plane_wm->trans_wm.plane_en,
  11335. hw_plane_wm->trans_wm.plane_res_b,
  11336. hw_plane_wm->trans_wm.plane_res_l);
  11337. }
  11338. /* DDB */
  11339. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11340. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11341. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11342. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11343. pipe_name(pipe),
  11344. sw_ddb_entry->start, sw_ddb_entry->end,
  11345. hw_ddb_entry->start, hw_ddb_entry->end);
  11346. }
  11347. }
  11348. }
  11349. static void
  11350. verify_connector_state(struct drm_device *dev,
  11351. struct drm_atomic_state *state,
  11352. struct drm_crtc *crtc)
  11353. {
  11354. struct drm_connector *connector;
  11355. struct drm_connector_state *old_conn_state;
  11356. int i;
  11357. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11358. struct drm_encoder *encoder = connector->encoder;
  11359. struct drm_connector_state *state = connector->state;
  11360. if (state->crtc != crtc)
  11361. continue;
  11362. intel_connector_verify_state(to_intel_connector(connector));
  11363. I915_STATE_WARN(state->best_encoder != encoder,
  11364. "connector's atomic encoder doesn't match legacy encoder\n");
  11365. }
  11366. }
  11367. static void
  11368. verify_encoder_state(struct drm_device *dev)
  11369. {
  11370. struct intel_encoder *encoder;
  11371. struct intel_connector *connector;
  11372. for_each_intel_encoder(dev, encoder) {
  11373. bool enabled = false;
  11374. enum pipe pipe;
  11375. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11376. encoder->base.base.id,
  11377. encoder->base.name);
  11378. for_each_intel_connector(dev, connector) {
  11379. if (connector->base.state->best_encoder != &encoder->base)
  11380. continue;
  11381. enabled = true;
  11382. I915_STATE_WARN(connector->base.state->crtc !=
  11383. encoder->base.crtc,
  11384. "connector's crtc doesn't match encoder crtc\n");
  11385. }
  11386. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11387. "encoder's enabled state mismatch "
  11388. "(expected %i, found %i)\n",
  11389. !!encoder->base.crtc, enabled);
  11390. if (!encoder->base.crtc) {
  11391. bool active;
  11392. active = encoder->get_hw_state(encoder, &pipe);
  11393. I915_STATE_WARN(active,
  11394. "encoder detached but still enabled on pipe %c.\n",
  11395. pipe_name(pipe));
  11396. }
  11397. }
  11398. }
  11399. static void
  11400. verify_crtc_state(struct drm_crtc *crtc,
  11401. struct drm_crtc_state *old_crtc_state,
  11402. struct drm_crtc_state *new_crtc_state)
  11403. {
  11404. struct drm_device *dev = crtc->dev;
  11405. struct drm_i915_private *dev_priv = to_i915(dev);
  11406. struct intel_encoder *encoder;
  11407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11408. struct intel_crtc_state *pipe_config, *sw_config;
  11409. struct drm_atomic_state *old_state;
  11410. bool active;
  11411. old_state = old_crtc_state->state;
  11412. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11413. pipe_config = to_intel_crtc_state(old_crtc_state);
  11414. memset(pipe_config, 0, sizeof(*pipe_config));
  11415. pipe_config->base.crtc = crtc;
  11416. pipe_config->base.state = old_state;
  11417. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11418. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11419. /* hw state is inconsistent with the pipe quirk */
  11420. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11421. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11422. active = new_crtc_state->active;
  11423. I915_STATE_WARN(new_crtc_state->active != active,
  11424. "crtc active state doesn't match with hw state "
  11425. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11426. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11427. "transitional active state does not match atomic hw state "
  11428. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11429. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11430. enum pipe pipe;
  11431. active = encoder->get_hw_state(encoder, &pipe);
  11432. I915_STATE_WARN(active != new_crtc_state->active,
  11433. "[ENCODER:%i] active %i with crtc active %i\n",
  11434. encoder->base.base.id, active, new_crtc_state->active);
  11435. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11436. "Encoder connected to wrong pipe %c\n",
  11437. pipe_name(pipe));
  11438. if (active) {
  11439. pipe_config->output_types |= 1 << encoder->type;
  11440. encoder->get_config(encoder, pipe_config);
  11441. }
  11442. }
  11443. if (!new_crtc_state->active)
  11444. return;
  11445. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11446. sw_config = to_intel_crtc_state(crtc->state);
  11447. if (!intel_pipe_config_compare(dev_priv, sw_config,
  11448. pipe_config, false)) {
  11449. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11450. intel_dump_pipe_config(intel_crtc, pipe_config,
  11451. "[hw state]");
  11452. intel_dump_pipe_config(intel_crtc, sw_config,
  11453. "[sw state]");
  11454. }
  11455. }
  11456. static void
  11457. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11458. struct intel_shared_dpll *pll,
  11459. struct drm_crtc *crtc,
  11460. struct drm_crtc_state *new_state)
  11461. {
  11462. struct intel_dpll_hw_state dpll_hw_state;
  11463. unsigned crtc_mask;
  11464. bool active;
  11465. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11466. DRM_DEBUG_KMS("%s\n", pll->name);
  11467. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11468. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11469. I915_STATE_WARN(!pll->on && pll->active_mask,
  11470. "pll in active use but not on in sw tracking\n");
  11471. I915_STATE_WARN(pll->on && !pll->active_mask,
  11472. "pll is on but not used by any active crtc\n");
  11473. I915_STATE_WARN(pll->on != active,
  11474. "pll on state mismatch (expected %i, found %i)\n",
  11475. pll->on, active);
  11476. }
  11477. if (!crtc) {
  11478. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11479. "more active pll users than references: %x vs %x\n",
  11480. pll->active_mask, pll->config.crtc_mask);
  11481. return;
  11482. }
  11483. crtc_mask = 1 << drm_crtc_index(crtc);
  11484. if (new_state->active)
  11485. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11486. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11487. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11488. else
  11489. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11490. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11491. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11492. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11493. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11494. crtc_mask, pll->config.crtc_mask);
  11495. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11496. &dpll_hw_state,
  11497. sizeof(dpll_hw_state)),
  11498. "pll hw state mismatch\n");
  11499. }
  11500. static void
  11501. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11502. struct drm_crtc_state *old_crtc_state,
  11503. struct drm_crtc_state *new_crtc_state)
  11504. {
  11505. struct drm_i915_private *dev_priv = to_i915(dev);
  11506. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11507. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11508. if (new_state->shared_dpll)
  11509. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11510. if (old_state->shared_dpll &&
  11511. old_state->shared_dpll != new_state->shared_dpll) {
  11512. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11513. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11514. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11515. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11516. pipe_name(drm_crtc_index(crtc)));
  11517. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11518. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11519. pipe_name(drm_crtc_index(crtc)));
  11520. }
  11521. }
  11522. static void
  11523. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11524. struct drm_atomic_state *state,
  11525. struct drm_crtc_state *old_state,
  11526. struct drm_crtc_state *new_state)
  11527. {
  11528. if (!needs_modeset(new_state) &&
  11529. !to_intel_crtc_state(new_state)->update_pipe)
  11530. return;
  11531. verify_wm_state(crtc, new_state);
  11532. verify_connector_state(crtc->dev, state, crtc);
  11533. verify_crtc_state(crtc, old_state, new_state);
  11534. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11535. }
  11536. static void
  11537. verify_disabled_dpll_state(struct drm_device *dev)
  11538. {
  11539. struct drm_i915_private *dev_priv = to_i915(dev);
  11540. int i;
  11541. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11542. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11543. }
  11544. static void
  11545. intel_modeset_verify_disabled(struct drm_device *dev,
  11546. struct drm_atomic_state *state)
  11547. {
  11548. verify_encoder_state(dev);
  11549. verify_connector_state(dev, state, NULL);
  11550. verify_disabled_dpll_state(dev);
  11551. }
  11552. static void update_scanline_offset(struct intel_crtc *crtc)
  11553. {
  11554. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11555. /*
  11556. * The scanline counter increments at the leading edge of hsync.
  11557. *
  11558. * On most platforms it starts counting from vtotal-1 on the
  11559. * first active line. That means the scanline counter value is
  11560. * always one less than what we would expect. Ie. just after
  11561. * start of vblank, which also occurs at start of hsync (on the
  11562. * last active line), the scanline counter will read vblank_start-1.
  11563. *
  11564. * On gen2 the scanline counter starts counting from 1 instead
  11565. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11566. * to keep the value positive), instead of adding one.
  11567. *
  11568. * On HSW+ the behaviour of the scanline counter depends on the output
  11569. * type. For DP ports it behaves like most other platforms, but on HDMI
  11570. * there's an extra 1 line difference. So we need to add two instead of
  11571. * one to the value.
  11572. */
  11573. if (IS_GEN2(dev_priv)) {
  11574. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11575. int vtotal;
  11576. vtotal = adjusted_mode->crtc_vtotal;
  11577. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11578. vtotal /= 2;
  11579. crtc->scanline_offset = vtotal - 1;
  11580. } else if (HAS_DDI(dev_priv) &&
  11581. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11582. crtc->scanline_offset = 2;
  11583. } else
  11584. crtc->scanline_offset = 1;
  11585. }
  11586. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11587. {
  11588. struct drm_device *dev = state->dev;
  11589. struct drm_i915_private *dev_priv = to_i915(dev);
  11590. struct intel_shared_dpll_config *shared_dpll = NULL;
  11591. struct drm_crtc *crtc;
  11592. struct drm_crtc_state *crtc_state;
  11593. int i;
  11594. if (!dev_priv->display.crtc_compute_clock)
  11595. return;
  11596. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11597. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11598. struct intel_shared_dpll *old_dpll =
  11599. to_intel_crtc_state(crtc->state)->shared_dpll;
  11600. if (!needs_modeset(crtc_state))
  11601. continue;
  11602. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11603. if (!old_dpll)
  11604. continue;
  11605. if (!shared_dpll)
  11606. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11607. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11608. }
  11609. }
  11610. /*
  11611. * This implements the workaround described in the "notes" section of the mode
  11612. * set sequence documentation. When going from no pipes or single pipe to
  11613. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11614. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11615. */
  11616. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11617. {
  11618. struct drm_crtc_state *crtc_state;
  11619. struct intel_crtc *intel_crtc;
  11620. struct drm_crtc *crtc;
  11621. struct intel_crtc_state *first_crtc_state = NULL;
  11622. struct intel_crtc_state *other_crtc_state = NULL;
  11623. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11624. int i;
  11625. /* look at all crtc's that are going to be enabled in during modeset */
  11626. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11627. intel_crtc = to_intel_crtc(crtc);
  11628. if (!crtc_state->active || !needs_modeset(crtc_state))
  11629. continue;
  11630. if (first_crtc_state) {
  11631. other_crtc_state = to_intel_crtc_state(crtc_state);
  11632. break;
  11633. } else {
  11634. first_crtc_state = to_intel_crtc_state(crtc_state);
  11635. first_pipe = intel_crtc->pipe;
  11636. }
  11637. }
  11638. /* No workaround needed? */
  11639. if (!first_crtc_state)
  11640. return 0;
  11641. /* w/a possibly needed, check how many crtc's are already enabled. */
  11642. for_each_intel_crtc(state->dev, intel_crtc) {
  11643. struct intel_crtc_state *pipe_config;
  11644. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11645. if (IS_ERR(pipe_config))
  11646. return PTR_ERR(pipe_config);
  11647. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11648. if (!pipe_config->base.active ||
  11649. needs_modeset(&pipe_config->base))
  11650. continue;
  11651. /* 2 or more enabled crtcs means no need for w/a */
  11652. if (enabled_pipe != INVALID_PIPE)
  11653. return 0;
  11654. enabled_pipe = intel_crtc->pipe;
  11655. }
  11656. if (enabled_pipe != INVALID_PIPE)
  11657. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11658. else if (other_crtc_state)
  11659. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11660. return 0;
  11661. }
  11662. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11663. {
  11664. struct drm_crtc *crtc;
  11665. struct drm_crtc_state *crtc_state;
  11666. int ret = 0;
  11667. /* add all active pipes to the state */
  11668. for_each_crtc(state->dev, crtc) {
  11669. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11670. if (IS_ERR(crtc_state))
  11671. return PTR_ERR(crtc_state);
  11672. if (!crtc_state->active || needs_modeset(crtc_state))
  11673. continue;
  11674. crtc_state->mode_changed = true;
  11675. ret = drm_atomic_add_affected_connectors(state, crtc);
  11676. if (ret)
  11677. break;
  11678. ret = drm_atomic_add_affected_planes(state, crtc);
  11679. if (ret)
  11680. break;
  11681. }
  11682. return ret;
  11683. }
  11684. static int intel_modeset_checks(struct drm_atomic_state *state)
  11685. {
  11686. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11687. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11688. struct drm_crtc *crtc;
  11689. struct drm_crtc_state *crtc_state;
  11690. int ret = 0, i;
  11691. if (!check_digital_port_conflicts(state)) {
  11692. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11693. return -EINVAL;
  11694. }
  11695. intel_state->modeset = true;
  11696. intel_state->active_crtcs = dev_priv->active_crtcs;
  11697. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11698. if (crtc_state->active)
  11699. intel_state->active_crtcs |= 1 << i;
  11700. else
  11701. intel_state->active_crtcs &= ~(1 << i);
  11702. if (crtc_state->active != crtc->state->active)
  11703. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11704. }
  11705. /*
  11706. * See if the config requires any additional preparation, e.g.
  11707. * to adjust global state with pipes off. We need to do this
  11708. * here so we can get the modeset_pipe updated config for the new
  11709. * mode set on this crtc. For other crtcs we need to use the
  11710. * adjusted_mode bits in the crtc directly.
  11711. */
  11712. if (dev_priv->display.modeset_calc_cdclk) {
  11713. if (!intel_state->cdclk_pll_vco)
  11714. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11715. if (!intel_state->cdclk_pll_vco)
  11716. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11717. ret = dev_priv->display.modeset_calc_cdclk(state);
  11718. if (ret < 0)
  11719. return ret;
  11720. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11721. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11722. ret = intel_modeset_all_pipes(state);
  11723. if (ret < 0)
  11724. return ret;
  11725. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11726. intel_state->cdclk, intel_state->dev_cdclk);
  11727. } else {
  11728. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11729. }
  11730. intel_modeset_clear_plls(state);
  11731. if (IS_HASWELL(dev_priv))
  11732. return haswell_mode_set_planes_workaround(state);
  11733. return 0;
  11734. }
  11735. /*
  11736. * Handle calculation of various watermark data at the end of the atomic check
  11737. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11738. * handlers to ensure that all derived state has been updated.
  11739. */
  11740. static int calc_watermark_data(struct drm_atomic_state *state)
  11741. {
  11742. struct drm_device *dev = state->dev;
  11743. struct drm_i915_private *dev_priv = to_i915(dev);
  11744. /* Is there platform-specific watermark information to calculate? */
  11745. if (dev_priv->display.compute_global_watermarks)
  11746. return dev_priv->display.compute_global_watermarks(state);
  11747. return 0;
  11748. }
  11749. /**
  11750. * intel_atomic_check - validate state object
  11751. * @dev: drm device
  11752. * @state: state to validate
  11753. */
  11754. static int intel_atomic_check(struct drm_device *dev,
  11755. struct drm_atomic_state *state)
  11756. {
  11757. struct drm_i915_private *dev_priv = to_i915(dev);
  11758. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11759. struct drm_crtc *crtc;
  11760. struct drm_crtc_state *crtc_state;
  11761. int ret, i;
  11762. bool any_ms = false;
  11763. ret = drm_atomic_helper_check_modeset(dev, state);
  11764. if (ret)
  11765. return ret;
  11766. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11767. struct intel_crtc_state *pipe_config =
  11768. to_intel_crtc_state(crtc_state);
  11769. /* Catch I915_MODE_FLAG_INHERITED */
  11770. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11771. crtc_state->mode_changed = true;
  11772. if (!needs_modeset(crtc_state))
  11773. continue;
  11774. if (!crtc_state->enable) {
  11775. any_ms = true;
  11776. continue;
  11777. }
  11778. /* FIXME: For only active_changed we shouldn't need to do any
  11779. * state recomputation at all. */
  11780. ret = drm_atomic_add_affected_connectors(state, crtc);
  11781. if (ret)
  11782. return ret;
  11783. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11784. if (ret) {
  11785. intel_dump_pipe_config(to_intel_crtc(crtc),
  11786. pipe_config, "[failed]");
  11787. return ret;
  11788. }
  11789. if (i915.fastboot &&
  11790. intel_pipe_config_compare(dev_priv,
  11791. to_intel_crtc_state(crtc->state),
  11792. pipe_config, true)) {
  11793. crtc_state->mode_changed = false;
  11794. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11795. }
  11796. if (needs_modeset(crtc_state))
  11797. any_ms = true;
  11798. ret = drm_atomic_add_affected_planes(state, crtc);
  11799. if (ret)
  11800. return ret;
  11801. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11802. needs_modeset(crtc_state) ?
  11803. "[modeset]" : "[fastset]");
  11804. }
  11805. if (any_ms) {
  11806. ret = intel_modeset_checks(state);
  11807. if (ret)
  11808. return ret;
  11809. } else {
  11810. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11811. }
  11812. ret = drm_atomic_helper_check_planes(dev, state);
  11813. if (ret)
  11814. return ret;
  11815. intel_fbc_choose_crtc(dev_priv, state);
  11816. return calc_watermark_data(state);
  11817. }
  11818. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11819. struct drm_atomic_state *state)
  11820. {
  11821. struct drm_i915_private *dev_priv = to_i915(dev);
  11822. struct drm_crtc_state *crtc_state;
  11823. struct drm_crtc *crtc;
  11824. int i, ret;
  11825. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11826. if (state->legacy_cursor_update)
  11827. continue;
  11828. ret = intel_crtc_wait_for_pending_flips(crtc);
  11829. if (ret)
  11830. return ret;
  11831. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11832. flush_workqueue(dev_priv->wq);
  11833. }
  11834. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11835. if (ret)
  11836. return ret;
  11837. ret = drm_atomic_helper_prepare_planes(dev, state);
  11838. mutex_unlock(&dev->struct_mutex);
  11839. return ret;
  11840. }
  11841. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11842. {
  11843. struct drm_device *dev = crtc->base.dev;
  11844. if (!dev->max_vblank_count)
  11845. return drm_accurate_vblank_count(&crtc->base);
  11846. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11847. }
  11848. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11849. struct drm_i915_private *dev_priv,
  11850. unsigned crtc_mask)
  11851. {
  11852. unsigned last_vblank_count[I915_MAX_PIPES];
  11853. enum pipe pipe;
  11854. int ret;
  11855. if (!crtc_mask)
  11856. return;
  11857. for_each_pipe(dev_priv, pipe) {
  11858. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11859. pipe);
  11860. if (!((1 << pipe) & crtc_mask))
  11861. continue;
  11862. ret = drm_crtc_vblank_get(&crtc->base);
  11863. if (WARN_ON(ret != 0)) {
  11864. crtc_mask &= ~(1 << pipe);
  11865. continue;
  11866. }
  11867. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11868. }
  11869. for_each_pipe(dev_priv, pipe) {
  11870. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11871. pipe);
  11872. long lret;
  11873. if (!((1 << pipe) & crtc_mask))
  11874. continue;
  11875. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11876. last_vblank_count[pipe] !=
  11877. drm_crtc_vblank_count(&crtc->base),
  11878. msecs_to_jiffies(50));
  11879. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11880. drm_crtc_vblank_put(&crtc->base);
  11881. }
  11882. }
  11883. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11884. {
  11885. /* fb updated, need to unpin old fb */
  11886. if (crtc_state->fb_changed)
  11887. return true;
  11888. /* wm changes, need vblank before final wm's */
  11889. if (crtc_state->update_wm_post)
  11890. return true;
  11891. /*
  11892. * cxsr is re-enabled after vblank.
  11893. * This is already handled by crtc_state->update_wm_post,
  11894. * but added for clarity.
  11895. */
  11896. if (crtc_state->disable_cxsr)
  11897. return true;
  11898. return false;
  11899. }
  11900. static void intel_update_crtc(struct drm_crtc *crtc,
  11901. struct drm_atomic_state *state,
  11902. struct drm_crtc_state *old_crtc_state,
  11903. unsigned int *crtc_vblank_mask)
  11904. {
  11905. struct drm_device *dev = crtc->dev;
  11906. struct drm_i915_private *dev_priv = to_i915(dev);
  11907. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11908. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11909. bool modeset = needs_modeset(crtc->state);
  11910. if (modeset) {
  11911. update_scanline_offset(intel_crtc);
  11912. dev_priv->display.crtc_enable(pipe_config, state);
  11913. } else {
  11914. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11915. }
  11916. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11917. intel_fbc_enable(
  11918. intel_crtc, pipe_config,
  11919. to_intel_plane_state(crtc->primary->state));
  11920. }
  11921. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11922. if (needs_vblank_wait(pipe_config))
  11923. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11924. }
  11925. static void intel_update_crtcs(struct drm_atomic_state *state,
  11926. unsigned int *crtc_vblank_mask)
  11927. {
  11928. struct drm_crtc *crtc;
  11929. struct drm_crtc_state *old_crtc_state;
  11930. int i;
  11931. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11932. if (!crtc->state->active)
  11933. continue;
  11934. intel_update_crtc(crtc, state, old_crtc_state,
  11935. crtc_vblank_mask);
  11936. }
  11937. }
  11938. static void skl_update_crtcs(struct drm_atomic_state *state,
  11939. unsigned int *crtc_vblank_mask)
  11940. {
  11941. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11942. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11943. struct drm_crtc *crtc;
  11944. struct intel_crtc *intel_crtc;
  11945. struct drm_crtc_state *old_crtc_state;
  11946. struct intel_crtc_state *cstate;
  11947. unsigned int updated = 0;
  11948. bool progress;
  11949. enum pipe pipe;
  11950. int i;
  11951. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  11952. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  11953. /* ignore allocations for crtc's that have been turned off. */
  11954. if (crtc->state->active)
  11955. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  11956. /*
  11957. * Whenever the number of active pipes changes, we need to make sure we
  11958. * update the pipes in the right order so that their ddb allocations
  11959. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11960. * cause pipe underruns and other bad stuff.
  11961. */
  11962. do {
  11963. progress = false;
  11964. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11965. bool vbl_wait = false;
  11966. unsigned int cmask = drm_crtc_mask(crtc);
  11967. intel_crtc = to_intel_crtc(crtc);
  11968. cstate = to_intel_crtc_state(crtc->state);
  11969. pipe = intel_crtc->pipe;
  11970. if (updated & cmask || !cstate->base.active)
  11971. continue;
  11972. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  11973. continue;
  11974. updated |= cmask;
  11975. entries[i] = &cstate->wm.skl.ddb;
  11976. /*
  11977. * If this is an already active pipe, it's DDB changed,
  11978. * and this isn't the last pipe that needs updating
  11979. * then we need to wait for a vblank to pass for the
  11980. * new ddb allocation to take effect.
  11981. */
  11982. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  11983. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  11984. !crtc->state->active_changed &&
  11985. intel_state->wm_results.dirty_pipes != updated)
  11986. vbl_wait = true;
  11987. intel_update_crtc(crtc, state, old_crtc_state,
  11988. crtc_vblank_mask);
  11989. if (vbl_wait)
  11990. intel_wait_for_vblank(dev_priv, pipe);
  11991. progress = true;
  11992. }
  11993. } while (progress);
  11994. }
  11995. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11996. {
  11997. struct drm_device *dev = state->dev;
  11998. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11999. struct drm_i915_private *dev_priv = to_i915(dev);
  12000. struct drm_crtc_state *old_crtc_state;
  12001. struct drm_crtc *crtc;
  12002. struct intel_crtc_state *intel_cstate;
  12003. bool hw_check = intel_state->modeset;
  12004. unsigned long put_domains[I915_MAX_PIPES] = {};
  12005. unsigned crtc_vblank_mask = 0;
  12006. int i;
  12007. drm_atomic_helper_wait_for_dependencies(state);
  12008. if (intel_state->modeset)
  12009. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12010. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12012. if (needs_modeset(crtc->state) ||
  12013. to_intel_crtc_state(crtc->state)->update_pipe) {
  12014. hw_check = true;
  12015. put_domains[to_intel_crtc(crtc)->pipe] =
  12016. modeset_get_crtc_power_domains(crtc,
  12017. to_intel_crtc_state(crtc->state));
  12018. }
  12019. if (!needs_modeset(crtc->state))
  12020. continue;
  12021. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12022. if (old_crtc_state->active) {
  12023. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12024. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12025. intel_crtc->active = false;
  12026. intel_fbc_disable(intel_crtc);
  12027. intel_disable_shared_dpll(intel_crtc);
  12028. /*
  12029. * Underruns don't always raise
  12030. * interrupts, so check manually.
  12031. */
  12032. intel_check_cpu_fifo_underruns(dev_priv);
  12033. intel_check_pch_fifo_underruns(dev_priv);
  12034. if (!crtc->state->active) {
  12035. /*
  12036. * Make sure we don't call initial_watermarks
  12037. * for ILK-style watermark updates.
  12038. */
  12039. if (dev_priv->display.atomic_update_watermarks)
  12040. dev_priv->display.initial_watermarks(intel_state,
  12041. to_intel_crtc_state(crtc->state));
  12042. else
  12043. intel_update_watermarks(intel_crtc);
  12044. }
  12045. }
  12046. }
  12047. /* Only after disabling all output pipelines that will be changed can we
  12048. * update the the output configuration. */
  12049. intel_modeset_update_crtc_state(state);
  12050. if (intel_state->modeset) {
  12051. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12052. if (dev_priv->display.modeset_commit_cdclk &&
  12053. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12054. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12055. dev_priv->display.modeset_commit_cdclk(state);
  12056. /*
  12057. * SKL workaround: bspec recommends we disable the SAGV when we
  12058. * have more then one pipe enabled
  12059. */
  12060. if (!intel_can_enable_sagv(state))
  12061. intel_disable_sagv(dev_priv);
  12062. intel_modeset_verify_disabled(dev, state);
  12063. }
  12064. /* Complete the events for pipes that have now been disabled */
  12065. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12066. bool modeset = needs_modeset(crtc->state);
  12067. /* Complete events for now disable pipes here. */
  12068. if (modeset && !crtc->state->active && crtc->state->event) {
  12069. spin_lock_irq(&dev->event_lock);
  12070. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12071. spin_unlock_irq(&dev->event_lock);
  12072. crtc->state->event = NULL;
  12073. }
  12074. }
  12075. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12076. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12077. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12078. * already, but still need the state for the delayed optimization. To
  12079. * fix this:
  12080. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12081. * - schedule that vblank worker _before_ calling hw_done
  12082. * - at the start of commit_tail, cancel it _synchrously
  12083. * - switch over to the vblank wait helper in the core after that since
  12084. * we don't need out special handling any more.
  12085. */
  12086. if (!state->legacy_cursor_update)
  12087. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12088. /*
  12089. * Now that the vblank has passed, we can go ahead and program the
  12090. * optimal watermarks on platforms that need two-step watermark
  12091. * programming.
  12092. *
  12093. * TODO: Move this (and other cleanup) to an async worker eventually.
  12094. */
  12095. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12096. intel_cstate = to_intel_crtc_state(crtc->state);
  12097. if (dev_priv->display.optimize_watermarks)
  12098. dev_priv->display.optimize_watermarks(intel_state,
  12099. intel_cstate);
  12100. }
  12101. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12102. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12103. if (put_domains[i])
  12104. modeset_put_power_domains(dev_priv, put_domains[i]);
  12105. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12106. }
  12107. if (intel_state->modeset && intel_can_enable_sagv(state))
  12108. intel_enable_sagv(dev_priv);
  12109. drm_atomic_helper_commit_hw_done(state);
  12110. if (intel_state->modeset)
  12111. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12112. mutex_lock(&dev->struct_mutex);
  12113. drm_atomic_helper_cleanup_planes(dev, state);
  12114. mutex_unlock(&dev->struct_mutex);
  12115. drm_atomic_helper_commit_cleanup_done(state);
  12116. drm_atomic_state_put(state);
  12117. /* As one of the primary mmio accessors, KMS has a high likelihood
  12118. * of triggering bugs in unclaimed access. After we finish
  12119. * modesetting, see if an error has been flagged, and if so
  12120. * enable debugging for the next modeset - and hope we catch
  12121. * the culprit.
  12122. *
  12123. * XXX note that we assume display power is on at this point.
  12124. * This might hold true now but we need to add pm helper to check
  12125. * unclaimed only when the hardware is on, as atomic commits
  12126. * can happen also when the device is completely off.
  12127. */
  12128. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12129. }
  12130. static void intel_atomic_commit_work(struct work_struct *work)
  12131. {
  12132. struct drm_atomic_state *state =
  12133. container_of(work, struct drm_atomic_state, commit_work);
  12134. intel_atomic_commit_tail(state);
  12135. }
  12136. static int __i915_sw_fence_call
  12137. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12138. enum i915_sw_fence_notify notify)
  12139. {
  12140. struct intel_atomic_state *state =
  12141. container_of(fence, struct intel_atomic_state, commit_ready);
  12142. switch (notify) {
  12143. case FENCE_COMPLETE:
  12144. if (state->base.commit_work.func)
  12145. queue_work(system_unbound_wq, &state->base.commit_work);
  12146. break;
  12147. case FENCE_FREE:
  12148. drm_atomic_state_put(&state->base);
  12149. break;
  12150. }
  12151. return NOTIFY_DONE;
  12152. }
  12153. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12154. {
  12155. struct drm_plane_state *old_plane_state;
  12156. struct drm_plane *plane;
  12157. int i;
  12158. for_each_plane_in_state(state, plane, old_plane_state, i)
  12159. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12160. intel_fb_obj(plane->state->fb),
  12161. to_intel_plane(plane)->frontbuffer_bit);
  12162. }
  12163. /**
  12164. * intel_atomic_commit - commit validated state object
  12165. * @dev: DRM device
  12166. * @state: the top-level driver state object
  12167. * @nonblock: nonblocking commit
  12168. *
  12169. * This function commits a top-level state object that has been validated
  12170. * with drm_atomic_helper_check().
  12171. *
  12172. * RETURNS
  12173. * Zero for success or -errno.
  12174. */
  12175. static int intel_atomic_commit(struct drm_device *dev,
  12176. struct drm_atomic_state *state,
  12177. bool nonblock)
  12178. {
  12179. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12180. struct drm_i915_private *dev_priv = to_i915(dev);
  12181. int ret = 0;
  12182. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12183. if (ret)
  12184. return ret;
  12185. drm_atomic_state_get(state);
  12186. i915_sw_fence_init(&intel_state->commit_ready,
  12187. intel_atomic_commit_ready);
  12188. ret = intel_atomic_prepare_commit(dev, state);
  12189. if (ret) {
  12190. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12191. i915_sw_fence_commit(&intel_state->commit_ready);
  12192. return ret;
  12193. }
  12194. drm_atomic_helper_swap_state(state, true);
  12195. dev_priv->wm.distrust_bios_wm = false;
  12196. intel_shared_dpll_commit(state);
  12197. intel_atomic_track_fbs(state);
  12198. if (intel_state->modeset) {
  12199. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12200. sizeof(intel_state->min_pixclk));
  12201. dev_priv->active_crtcs = intel_state->active_crtcs;
  12202. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12203. }
  12204. drm_atomic_state_get(state);
  12205. INIT_WORK(&state->commit_work,
  12206. nonblock ? intel_atomic_commit_work : NULL);
  12207. i915_sw_fence_commit(&intel_state->commit_ready);
  12208. if (!nonblock) {
  12209. i915_sw_fence_wait(&intel_state->commit_ready);
  12210. intel_atomic_commit_tail(state);
  12211. }
  12212. return 0;
  12213. }
  12214. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12215. {
  12216. struct drm_device *dev = crtc->dev;
  12217. struct drm_atomic_state *state;
  12218. struct drm_crtc_state *crtc_state;
  12219. int ret;
  12220. state = drm_atomic_state_alloc(dev);
  12221. if (!state) {
  12222. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12223. crtc->base.id, crtc->name);
  12224. return;
  12225. }
  12226. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12227. retry:
  12228. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12229. ret = PTR_ERR_OR_ZERO(crtc_state);
  12230. if (!ret) {
  12231. if (!crtc_state->active)
  12232. goto out;
  12233. crtc_state->mode_changed = true;
  12234. ret = drm_atomic_commit(state);
  12235. }
  12236. if (ret == -EDEADLK) {
  12237. drm_atomic_state_clear(state);
  12238. drm_modeset_backoff(state->acquire_ctx);
  12239. goto retry;
  12240. }
  12241. out:
  12242. drm_atomic_state_put(state);
  12243. }
  12244. /*
  12245. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12246. * drm_atomic_helper_legacy_gamma_set() directly.
  12247. */
  12248. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12249. u16 *red, u16 *green, u16 *blue,
  12250. uint32_t size)
  12251. {
  12252. struct drm_device *dev = crtc->dev;
  12253. struct drm_mode_config *config = &dev->mode_config;
  12254. struct drm_crtc_state *state;
  12255. int ret;
  12256. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12257. if (ret)
  12258. return ret;
  12259. /*
  12260. * Make sure we update the legacy properties so this works when
  12261. * atomic is not enabled.
  12262. */
  12263. state = crtc->state;
  12264. drm_object_property_set_value(&crtc->base,
  12265. config->degamma_lut_property,
  12266. (state->degamma_lut) ?
  12267. state->degamma_lut->base.id : 0);
  12268. drm_object_property_set_value(&crtc->base,
  12269. config->ctm_property,
  12270. (state->ctm) ?
  12271. state->ctm->base.id : 0);
  12272. drm_object_property_set_value(&crtc->base,
  12273. config->gamma_lut_property,
  12274. (state->gamma_lut) ?
  12275. state->gamma_lut->base.id : 0);
  12276. return 0;
  12277. }
  12278. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12279. .gamma_set = intel_atomic_legacy_gamma_set,
  12280. .set_config = drm_atomic_helper_set_config,
  12281. .set_property = drm_atomic_helper_crtc_set_property,
  12282. .destroy = intel_crtc_destroy,
  12283. .page_flip = intel_crtc_page_flip,
  12284. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12285. .atomic_destroy_state = intel_crtc_destroy_state,
  12286. };
  12287. /**
  12288. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12289. * @plane: drm plane to prepare for
  12290. * @fb: framebuffer to prepare for presentation
  12291. *
  12292. * Prepares a framebuffer for usage on a display plane. Generally this
  12293. * involves pinning the underlying object and updating the frontbuffer tracking
  12294. * bits. Some older platforms need special physical address handling for
  12295. * cursor planes.
  12296. *
  12297. * Must be called with struct_mutex held.
  12298. *
  12299. * Returns 0 on success, negative error code on failure.
  12300. */
  12301. int
  12302. intel_prepare_plane_fb(struct drm_plane *plane,
  12303. struct drm_plane_state *new_state)
  12304. {
  12305. struct intel_atomic_state *intel_state =
  12306. to_intel_atomic_state(new_state->state);
  12307. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12308. struct drm_framebuffer *fb = new_state->fb;
  12309. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12310. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12311. int ret;
  12312. if (!obj && !old_obj)
  12313. return 0;
  12314. if (old_obj) {
  12315. struct drm_crtc_state *crtc_state =
  12316. drm_atomic_get_existing_crtc_state(new_state->state,
  12317. plane->state->crtc);
  12318. /* Big Hammer, we also need to ensure that any pending
  12319. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12320. * current scanout is retired before unpinning the old
  12321. * framebuffer. Note that we rely on userspace rendering
  12322. * into the buffer attached to the pipe they are waiting
  12323. * on. If not, userspace generates a GPU hang with IPEHR
  12324. * point to the MI_WAIT_FOR_EVENT.
  12325. *
  12326. * This should only fail upon a hung GPU, in which case we
  12327. * can safely continue.
  12328. */
  12329. if (needs_modeset(crtc_state)) {
  12330. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12331. old_obj->resv, NULL,
  12332. false, 0,
  12333. GFP_KERNEL);
  12334. if (ret < 0)
  12335. return ret;
  12336. }
  12337. }
  12338. if (new_state->fence) { /* explicit fencing */
  12339. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12340. new_state->fence,
  12341. I915_FENCE_TIMEOUT,
  12342. GFP_KERNEL);
  12343. if (ret < 0)
  12344. return ret;
  12345. }
  12346. if (!obj)
  12347. return 0;
  12348. if (!new_state->fence) { /* implicit fencing */
  12349. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12350. obj->resv, NULL,
  12351. false, I915_FENCE_TIMEOUT,
  12352. GFP_KERNEL);
  12353. if (ret < 0)
  12354. return ret;
  12355. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12356. }
  12357. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12358. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12359. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12360. ret = i915_gem_object_attach_phys(obj, align);
  12361. if (ret) {
  12362. DRM_DEBUG_KMS("failed to attach phys object\n");
  12363. return ret;
  12364. }
  12365. } else {
  12366. struct i915_vma *vma;
  12367. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12368. if (IS_ERR(vma)) {
  12369. DRM_DEBUG_KMS("failed to pin object\n");
  12370. return PTR_ERR(vma);
  12371. }
  12372. }
  12373. return 0;
  12374. }
  12375. /**
  12376. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12377. * @plane: drm plane to clean up for
  12378. * @fb: old framebuffer that was on plane
  12379. *
  12380. * Cleans up a framebuffer that has just been removed from a plane.
  12381. *
  12382. * Must be called with struct_mutex held.
  12383. */
  12384. void
  12385. intel_cleanup_plane_fb(struct drm_plane *plane,
  12386. struct drm_plane_state *old_state)
  12387. {
  12388. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12389. struct intel_plane_state *old_intel_state;
  12390. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12391. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12392. old_intel_state = to_intel_plane_state(old_state);
  12393. if (!obj && !old_obj)
  12394. return;
  12395. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12396. !INTEL_INFO(dev_priv)->cursor_needs_physical))
  12397. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12398. }
  12399. int
  12400. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12401. {
  12402. int max_scale;
  12403. int crtc_clock, cdclk;
  12404. if (!intel_crtc || !crtc_state->base.enable)
  12405. return DRM_PLANE_HELPER_NO_SCALING;
  12406. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12407. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12408. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12409. return DRM_PLANE_HELPER_NO_SCALING;
  12410. /*
  12411. * skl max scale is lower of:
  12412. * close to 3 but not 3, -1 is for that purpose
  12413. * or
  12414. * cdclk/crtc_clock
  12415. */
  12416. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12417. return max_scale;
  12418. }
  12419. static int
  12420. intel_check_primary_plane(struct drm_plane *plane,
  12421. struct intel_crtc_state *crtc_state,
  12422. struct intel_plane_state *state)
  12423. {
  12424. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12425. struct drm_crtc *crtc = state->base.crtc;
  12426. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12427. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12428. bool can_position = false;
  12429. int ret;
  12430. if (INTEL_GEN(dev_priv) >= 9) {
  12431. /* use scaler when colorkey is not required */
  12432. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12433. min_scale = 1;
  12434. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12435. }
  12436. can_position = true;
  12437. }
  12438. ret = drm_plane_helper_check_state(&state->base,
  12439. &state->clip,
  12440. min_scale, max_scale,
  12441. can_position, true);
  12442. if (ret)
  12443. return ret;
  12444. if (!state->base.fb)
  12445. return 0;
  12446. if (INTEL_GEN(dev_priv) >= 9) {
  12447. ret = skl_check_plane_surface(state);
  12448. if (ret)
  12449. return ret;
  12450. }
  12451. return 0;
  12452. }
  12453. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12454. struct drm_crtc_state *old_crtc_state)
  12455. {
  12456. struct drm_device *dev = crtc->dev;
  12457. struct drm_i915_private *dev_priv = to_i915(dev);
  12458. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12459. struct intel_crtc_state *intel_cstate =
  12460. to_intel_crtc_state(crtc->state);
  12461. struct intel_crtc_state *old_intel_cstate =
  12462. to_intel_crtc_state(old_crtc_state);
  12463. struct intel_atomic_state *old_intel_state =
  12464. to_intel_atomic_state(old_crtc_state->state);
  12465. bool modeset = needs_modeset(crtc->state);
  12466. /* Perform vblank evasion around commit operation */
  12467. intel_pipe_update_start(intel_crtc);
  12468. if (modeset)
  12469. goto out;
  12470. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12471. intel_color_set_csc(crtc->state);
  12472. intel_color_load_luts(crtc->state);
  12473. }
  12474. if (intel_cstate->update_pipe)
  12475. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  12476. else if (INTEL_GEN(dev_priv) >= 9)
  12477. skl_detach_scalers(intel_crtc);
  12478. out:
  12479. if (dev_priv->display.atomic_update_watermarks)
  12480. dev_priv->display.atomic_update_watermarks(old_intel_state,
  12481. intel_cstate);
  12482. }
  12483. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12484. struct drm_crtc_state *old_crtc_state)
  12485. {
  12486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12487. intel_pipe_update_end(intel_crtc, NULL);
  12488. }
  12489. /**
  12490. * intel_plane_destroy - destroy a plane
  12491. * @plane: plane to destroy
  12492. *
  12493. * Common destruction function for all types of planes (primary, cursor,
  12494. * sprite).
  12495. */
  12496. void intel_plane_destroy(struct drm_plane *plane)
  12497. {
  12498. drm_plane_cleanup(plane);
  12499. kfree(to_intel_plane(plane));
  12500. }
  12501. const struct drm_plane_funcs intel_plane_funcs = {
  12502. .update_plane = drm_atomic_helper_update_plane,
  12503. .disable_plane = drm_atomic_helper_disable_plane,
  12504. .destroy = intel_plane_destroy,
  12505. .set_property = drm_atomic_helper_plane_set_property,
  12506. .atomic_get_property = intel_plane_atomic_get_property,
  12507. .atomic_set_property = intel_plane_atomic_set_property,
  12508. .atomic_duplicate_state = intel_plane_duplicate_state,
  12509. .atomic_destroy_state = intel_plane_destroy_state,
  12510. };
  12511. static struct intel_plane *
  12512. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12513. {
  12514. struct intel_plane *primary = NULL;
  12515. struct intel_plane_state *state = NULL;
  12516. const uint32_t *intel_primary_formats;
  12517. unsigned int supported_rotations;
  12518. unsigned int num_formats;
  12519. int ret;
  12520. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12521. if (!primary) {
  12522. ret = -ENOMEM;
  12523. goto fail;
  12524. }
  12525. state = intel_create_plane_state(&primary->base);
  12526. if (!state) {
  12527. ret = -ENOMEM;
  12528. goto fail;
  12529. }
  12530. primary->base.state = &state->base;
  12531. primary->can_scale = false;
  12532. primary->max_downscale = 1;
  12533. if (INTEL_GEN(dev_priv) >= 9) {
  12534. primary->can_scale = true;
  12535. state->scaler_id = -1;
  12536. }
  12537. primary->pipe = pipe;
  12538. /*
  12539. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12540. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12541. */
  12542. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12543. primary->plane = (enum plane) !pipe;
  12544. else
  12545. primary->plane = (enum plane) pipe;
  12546. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12547. primary->check_plane = intel_check_primary_plane;
  12548. if (INTEL_GEN(dev_priv) >= 9) {
  12549. intel_primary_formats = skl_primary_formats;
  12550. num_formats = ARRAY_SIZE(skl_primary_formats);
  12551. primary->update_plane = skylake_update_primary_plane;
  12552. primary->disable_plane = skylake_disable_primary_plane;
  12553. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12554. intel_primary_formats = i965_primary_formats;
  12555. num_formats = ARRAY_SIZE(i965_primary_formats);
  12556. primary->update_plane = ironlake_update_primary_plane;
  12557. primary->disable_plane = i9xx_disable_primary_plane;
  12558. } else if (INTEL_GEN(dev_priv) >= 4) {
  12559. intel_primary_formats = i965_primary_formats;
  12560. num_formats = ARRAY_SIZE(i965_primary_formats);
  12561. primary->update_plane = i9xx_update_primary_plane;
  12562. primary->disable_plane = i9xx_disable_primary_plane;
  12563. } else {
  12564. intel_primary_formats = i8xx_primary_formats;
  12565. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12566. primary->update_plane = i9xx_update_primary_plane;
  12567. primary->disable_plane = i9xx_disable_primary_plane;
  12568. }
  12569. if (INTEL_GEN(dev_priv) >= 9)
  12570. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12571. 0, &intel_plane_funcs,
  12572. intel_primary_formats, num_formats,
  12573. DRM_PLANE_TYPE_PRIMARY,
  12574. "plane 1%c", pipe_name(pipe));
  12575. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12576. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12577. 0, &intel_plane_funcs,
  12578. intel_primary_formats, num_formats,
  12579. DRM_PLANE_TYPE_PRIMARY,
  12580. "primary %c", pipe_name(pipe));
  12581. else
  12582. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12583. 0, &intel_plane_funcs,
  12584. intel_primary_formats, num_formats,
  12585. DRM_PLANE_TYPE_PRIMARY,
  12586. "plane %c", plane_name(primary->plane));
  12587. if (ret)
  12588. goto fail;
  12589. if (INTEL_GEN(dev_priv) >= 9) {
  12590. supported_rotations =
  12591. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12592. DRM_ROTATE_180 | DRM_ROTATE_270;
  12593. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12594. supported_rotations =
  12595. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12596. DRM_REFLECT_X;
  12597. } else if (INTEL_GEN(dev_priv) >= 4) {
  12598. supported_rotations =
  12599. DRM_ROTATE_0 | DRM_ROTATE_180;
  12600. } else {
  12601. supported_rotations = DRM_ROTATE_0;
  12602. }
  12603. if (INTEL_GEN(dev_priv) >= 4)
  12604. drm_plane_create_rotation_property(&primary->base,
  12605. DRM_ROTATE_0,
  12606. supported_rotations);
  12607. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12608. return primary;
  12609. fail:
  12610. kfree(state);
  12611. kfree(primary);
  12612. return ERR_PTR(ret);
  12613. }
  12614. static int
  12615. intel_check_cursor_plane(struct drm_plane *plane,
  12616. struct intel_crtc_state *crtc_state,
  12617. struct intel_plane_state *state)
  12618. {
  12619. struct drm_framebuffer *fb = state->base.fb;
  12620. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12621. enum pipe pipe = to_intel_plane(plane)->pipe;
  12622. unsigned stride;
  12623. int ret;
  12624. ret = drm_plane_helper_check_state(&state->base,
  12625. &state->clip,
  12626. DRM_PLANE_HELPER_NO_SCALING,
  12627. DRM_PLANE_HELPER_NO_SCALING,
  12628. true, true);
  12629. if (ret)
  12630. return ret;
  12631. /* if we want to turn off the cursor ignore width and height */
  12632. if (!obj)
  12633. return 0;
  12634. /* Check for which cursor types we support */
  12635. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12636. state->base.crtc_h)) {
  12637. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12638. state->base.crtc_w, state->base.crtc_h);
  12639. return -EINVAL;
  12640. }
  12641. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12642. if (obj->base.size < stride * state->base.crtc_h) {
  12643. DRM_DEBUG_KMS("buffer is too small\n");
  12644. return -ENOMEM;
  12645. }
  12646. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  12647. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12648. return -EINVAL;
  12649. }
  12650. /*
  12651. * There's something wrong with the cursor on CHV pipe C.
  12652. * If it straddles the left edge of the screen then
  12653. * moving it away from the edge or disabling it often
  12654. * results in a pipe underrun, and often that can lead to
  12655. * dead pipe (constant underrun reported, and it scans
  12656. * out just a solid color). To recover from that, the
  12657. * display power well must be turned off and on again.
  12658. * Refuse the put the cursor into that compromised position.
  12659. */
  12660. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12661. state->base.visible && state->base.crtc_x < 0) {
  12662. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12663. return -EINVAL;
  12664. }
  12665. return 0;
  12666. }
  12667. static void
  12668. intel_disable_cursor_plane(struct drm_plane *plane,
  12669. struct drm_crtc *crtc)
  12670. {
  12671. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12672. intel_crtc->cursor_addr = 0;
  12673. intel_crtc_update_cursor(crtc, NULL);
  12674. }
  12675. static void
  12676. intel_update_cursor_plane(struct drm_plane *plane,
  12677. const struct intel_crtc_state *crtc_state,
  12678. const struct intel_plane_state *state)
  12679. {
  12680. struct drm_crtc *crtc = crtc_state->base.crtc;
  12681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12682. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12683. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12684. uint32_t addr;
  12685. if (!obj)
  12686. addr = 0;
  12687. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12688. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12689. else
  12690. addr = obj->phys_handle->busaddr;
  12691. intel_crtc->cursor_addr = addr;
  12692. intel_crtc_update_cursor(crtc, state);
  12693. }
  12694. static struct intel_plane *
  12695. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12696. {
  12697. struct intel_plane *cursor = NULL;
  12698. struct intel_plane_state *state = NULL;
  12699. int ret;
  12700. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12701. if (!cursor) {
  12702. ret = -ENOMEM;
  12703. goto fail;
  12704. }
  12705. state = intel_create_plane_state(&cursor->base);
  12706. if (!state) {
  12707. ret = -ENOMEM;
  12708. goto fail;
  12709. }
  12710. cursor->base.state = &state->base;
  12711. cursor->can_scale = false;
  12712. cursor->max_downscale = 1;
  12713. cursor->pipe = pipe;
  12714. cursor->plane = pipe;
  12715. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12716. cursor->check_plane = intel_check_cursor_plane;
  12717. cursor->update_plane = intel_update_cursor_plane;
  12718. cursor->disable_plane = intel_disable_cursor_plane;
  12719. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12720. 0, &intel_plane_funcs,
  12721. intel_cursor_formats,
  12722. ARRAY_SIZE(intel_cursor_formats),
  12723. DRM_PLANE_TYPE_CURSOR,
  12724. "cursor %c", pipe_name(pipe));
  12725. if (ret)
  12726. goto fail;
  12727. if (INTEL_GEN(dev_priv) >= 4)
  12728. drm_plane_create_rotation_property(&cursor->base,
  12729. DRM_ROTATE_0,
  12730. DRM_ROTATE_0 |
  12731. DRM_ROTATE_180);
  12732. if (INTEL_GEN(dev_priv) >= 9)
  12733. state->scaler_id = -1;
  12734. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12735. return cursor;
  12736. fail:
  12737. kfree(state);
  12738. kfree(cursor);
  12739. return ERR_PTR(ret);
  12740. }
  12741. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  12742. struct intel_crtc *crtc,
  12743. struct intel_crtc_state *crtc_state)
  12744. {
  12745. struct intel_crtc_scaler_state *scaler_state =
  12746. &crtc_state->scaler_state;
  12747. int i;
  12748. for (i = 0; i < crtc->num_scalers; i++) {
  12749. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12750. scaler->in_use = 0;
  12751. scaler->mode = PS_SCALER_MODE_DYN;
  12752. }
  12753. scaler_state->scaler_id = -1;
  12754. }
  12755. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12756. {
  12757. struct intel_crtc *intel_crtc;
  12758. struct intel_crtc_state *crtc_state = NULL;
  12759. struct intel_plane *primary = NULL;
  12760. struct intel_plane *cursor = NULL;
  12761. int sprite, ret;
  12762. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12763. if (!intel_crtc)
  12764. return -ENOMEM;
  12765. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12766. if (!crtc_state) {
  12767. ret = -ENOMEM;
  12768. goto fail;
  12769. }
  12770. intel_crtc->config = crtc_state;
  12771. intel_crtc->base.state = &crtc_state->base;
  12772. crtc_state->base.crtc = &intel_crtc->base;
  12773. /* initialize shared scalers */
  12774. if (INTEL_GEN(dev_priv) >= 9) {
  12775. if (pipe == PIPE_C)
  12776. intel_crtc->num_scalers = 1;
  12777. else
  12778. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12779. skl_init_scalers(dev_priv, intel_crtc, crtc_state);
  12780. }
  12781. primary = intel_primary_plane_create(dev_priv, pipe);
  12782. if (IS_ERR(primary)) {
  12783. ret = PTR_ERR(primary);
  12784. goto fail;
  12785. }
  12786. for_each_sprite(dev_priv, pipe, sprite) {
  12787. struct intel_plane *plane;
  12788. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12789. if (IS_ERR(plane)) {
  12790. ret = PTR_ERR(plane);
  12791. goto fail;
  12792. }
  12793. }
  12794. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12795. if (IS_ERR(cursor)) {
  12796. ret = PTR_ERR(cursor);
  12797. goto fail;
  12798. }
  12799. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12800. &primary->base, &cursor->base,
  12801. &intel_crtc_funcs,
  12802. "pipe %c", pipe_name(pipe));
  12803. if (ret)
  12804. goto fail;
  12805. intel_crtc->pipe = pipe;
  12806. intel_crtc->plane = primary->plane;
  12807. intel_crtc->cursor_base = ~0;
  12808. intel_crtc->cursor_cntl = ~0;
  12809. intel_crtc->cursor_size = ~0;
  12810. intel_crtc->wm.cxsr_allowed = true;
  12811. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12812. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12813. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12814. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12815. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12816. intel_color_init(&intel_crtc->base);
  12817. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12818. return 0;
  12819. fail:
  12820. /*
  12821. * drm_mode_config_cleanup() will free up any
  12822. * crtcs/planes already initialized.
  12823. */
  12824. kfree(crtc_state);
  12825. kfree(intel_crtc);
  12826. return ret;
  12827. }
  12828. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12829. {
  12830. struct drm_encoder *encoder = connector->base.encoder;
  12831. struct drm_device *dev = connector->base.dev;
  12832. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12833. if (!encoder || WARN_ON(!encoder->crtc))
  12834. return INVALID_PIPE;
  12835. return to_intel_crtc(encoder->crtc)->pipe;
  12836. }
  12837. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12838. struct drm_file *file)
  12839. {
  12840. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12841. struct drm_crtc *drmmode_crtc;
  12842. struct intel_crtc *crtc;
  12843. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12844. if (!drmmode_crtc)
  12845. return -ENOENT;
  12846. crtc = to_intel_crtc(drmmode_crtc);
  12847. pipe_from_crtc_id->pipe = crtc->pipe;
  12848. return 0;
  12849. }
  12850. static int intel_encoder_clones(struct intel_encoder *encoder)
  12851. {
  12852. struct drm_device *dev = encoder->base.dev;
  12853. struct intel_encoder *source_encoder;
  12854. int index_mask = 0;
  12855. int entry = 0;
  12856. for_each_intel_encoder(dev, source_encoder) {
  12857. if (encoders_cloneable(encoder, source_encoder))
  12858. index_mask |= (1 << entry);
  12859. entry++;
  12860. }
  12861. return index_mask;
  12862. }
  12863. static bool has_edp_a(struct drm_i915_private *dev_priv)
  12864. {
  12865. if (!IS_MOBILE(dev_priv))
  12866. return false;
  12867. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12868. return false;
  12869. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12870. return false;
  12871. return true;
  12872. }
  12873. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  12874. {
  12875. if (INTEL_GEN(dev_priv) >= 9)
  12876. return false;
  12877. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  12878. return false;
  12879. if (IS_CHERRYVIEW(dev_priv))
  12880. return false;
  12881. if (HAS_PCH_LPT_H(dev_priv) &&
  12882. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12883. return false;
  12884. /* DDI E can't be used if DDI A requires 4 lanes */
  12885. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12886. return false;
  12887. if (!dev_priv->vbt.int_crt_support)
  12888. return false;
  12889. return true;
  12890. }
  12891. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12892. {
  12893. int pps_num;
  12894. int pps_idx;
  12895. if (HAS_DDI(dev_priv))
  12896. return;
  12897. /*
  12898. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12899. * everywhere where registers can be write protected.
  12900. */
  12901. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12902. pps_num = 2;
  12903. else
  12904. pps_num = 1;
  12905. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12906. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12907. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12908. I915_WRITE(PP_CONTROL(pps_idx), val);
  12909. }
  12910. }
  12911. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12912. {
  12913. if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
  12914. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12915. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12916. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12917. else
  12918. dev_priv->pps_mmio_base = PPS_BASE;
  12919. intel_pps_unlock_regs_wa(dev_priv);
  12920. }
  12921. static void intel_setup_outputs(struct drm_device *dev)
  12922. {
  12923. struct drm_i915_private *dev_priv = to_i915(dev);
  12924. struct intel_encoder *encoder;
  12925. bool dpd_is_edp = false;
  12926. intel_pps_init(dev_priv);
  12927. /*
  12928. * intel_edp_init_connector() depends on this completing first, to
  12929. * prevent the registeration of both eDP and LVDS and the incorrect
  12930. * sharing of the PPS.
  12931. */
  12932. intel_lvds_init(dev);
  12933. if (intel_crt_present(dev_priv))
  12934. intel_crt_init(dev);
  12935. if (IS_BROXTON(dev_priv)) {
  12936. /*
  12937. * FIXME: Broxton doesn't support port detection via the
  12938. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12939. * detect the ports.
  12940. */
  12941. intel_ddi_init(dev, PORT_A);
  12942. intel_ddi_init(dev, PORT_B);
  12943. intel_ddi_init(dev, PORT_C);
  12944. intel_dsi_init(dev);
  12945. } else if (HAS_DDI(dev_priv)) {
  12946. int found;
  12947. /*
  12948. * Haswell uses DDI functions to detect digital outputs.
  12949. * On SKL pre-D0 the strap isn't connected, so we assume
  12950. * it's there.
  12951. */
  12952. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12953. /* WaIgnoreDDIAStrap: skl */
  12954. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12955. intel_ddi_init(dev, PORT_A);
  12956. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12957. * register */
  12958. found = I915_READ(SFUSE_STRAP);
  12959. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12960. intel_ddi_init(dev, PORT_B);
  12961. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12962. intel_ddi_init(dev, PORT_C);
  12963. if (found & SFUSE_STRAP_DDID_DETECTED)
  12964. intel_ddi_init(dev, PORT_D);
  12965. /*
  12966. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12967. */
  12968. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  12969. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12970. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12971. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12972. intel_ddi_init(dev, PORT_E);
  12973. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12974. int found;
  12975. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  12976. if (has_edp_a(dev_priv))
  12977. intel_dp_init(dev, DP_A, PORT_A);
  12978. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12979. /* PCH SDVOB multiplex with HDMIB */
  12980. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12981. if (!found)
  12982. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12983. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12984. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12985. }
  12986. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12987. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12988. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12989. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12990. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12991. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12992. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12993. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12994. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12995. bool has_edp, has_port;
  12996. /*
  12997. * The DP_DETECTED bit is the latched state of the DDC
  12998. * SDA pin at boot. However since eDP doesn't require DDC
  12999. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13000. * eDP ports may have been muxed to an alternate function.
  13001. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13002. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13003. * detect eDP ports.
  13004. *
  13005. * Sadly the straps seem to be missing sometimes even for HDMI
  13006. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13007. * and VBT for the presence of the port. Additionally we can't
  13008. * trust the port type the VBT declares as we've seen at least
  13009. * HDMI ports that the VBT claim are DP or eDP.
  13010. */
  13011. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  13012. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13013. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13014. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  13015. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13016. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  13017. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  13018. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13019. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13020. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  13021. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13022. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  13023. if (IS_CHERRYVIEW(dev_priv)) {
  13024. /*
  13025. * eDP not supported on port D,
  13026. * so no need to worry about it
  13027. */
  13028. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13029. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13030. intel_dp_init(dev, CHV_DP_D, PORT_D);
  13031. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13032. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  13033. }
  13034. intel_dsi_init(dev);
  13035. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13036. bool found = false;
  13037. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13038. DRM_DEBUG_KMS("probing SDVOB\n");
  13039. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  13040. if (!found && IS_G4X(dev_priv)) {
  13041. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13042. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  13043. }
  13044. if (!found && IS_G4X(dev_priv))
  13045. intel_dp_init(dev, DP_B, PORT_B);
  13046. }
  13047. /* Before G4X SDVOC doesn't have its own detect register */
  13048. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13049. DRM_DEBUG_KMS("probing SDVOC\n");
  13050. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  13051. }
  13052. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13053. if (IS_G4X(dev_priv)) {
  13054. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13055. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  13056. }
  13057. if (IS_G4X(dev_priv))
  13058. intel_dp_init(dev, DP_C, PORT_C);
  13059. }
  13060. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13061. intel_dp_init(dev, DP_D, PORT_D);
  13062. } else if (IS_GEN2(dev_priv))
  13063. intel_dvo_init(dev);
  13064. if (SUPPORTS_TV(dev_priv))
  13065. intel_tv_init(dev);
  13066. intel_psr_init(dev);
  13067. for_each_intel_encoder(dev, encoder) {
  13068. encoder->base.possible_crtcs = encoder->crtc_mask;
  13069. encoder->base.possible_clones =
  13070. intel_encoder_clones(encoder);
  13071. }
  13072. intel_init_pch_refclk(dev);
  13073. drm_helper_move_panel_connectors_to_head(dev);
  13074. }
  13075. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13076. {
  13077. struct drm_device *dev = fb->dev;
  13078. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13079. drm_framebuffer_cleanup(fb);
  13080. mutex_lock(&dev->struct_mutex);
  13081. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13082. i915_gem_object_put(intel_fb->obj);
  13083. mutex_unlock(&dev->struct_mutex);
  13084. kfree(intel_fb);
  13085. }
  13086. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13087. struct drm_file *file,
  13088. unsigned int *handle)
  13089. {
  13090. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13091. struct drm_i915_gem_object *obj = intel_fb->obj;
  13092. if (obj->userptr.mm) {
  13093. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13094. return -EINVAL;
  13095. }
  13096. return drm_gem_handle_create(file, &obj->base, handle);
  13097. }
  13098. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13099. struct drm_file *file,
  13100. unsigned flags, unsigned color,
  13101. struct drm_clip_rect *clips,
  13102. unsigned num_clips)
  13103. {
  13104. struct drm_device *dev = fb->dev;
  13105. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13106. struct drm_i915_gem_object *obj = intel_fb->obj;
  13107. mutex_lock(&dev->struct_mutex);
  13108. if (obj->pin_display && obj->cache_dirty)
  13109. i915_gem_clflush_object(obj, true);
  13110. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13111. mutex_unlock(&dev->struct_mutex);
  13112. return 0;
  13113. }
  13114. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13115. .destroy = intel_user_framebuffer_destroy,
  13116. .create_handle = intel_user_framebuffer_create_handle,
  13117. .dirty = intel_user_framebuffer_dirty,
  13118. };
  13119. static
  13120. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13121. uint64_t fb_modifier, uint32_t pixel_format)
  13122. {
  13123. u32 gen = INTEL_INFO(dev_priv)->gen;
  13124. if (gen >= 9) {
  13125. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13126. /* "The stride in bytes must not exceed the of the size of 8K
  13127. * pixels and 32K bytes."
  13128. */
  13129. return min(8192 * cpp, 32768);
  13130. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13131. !IS_CHERRYVIEW(dev_priv)) {
  13132. return 32*1024;
  13133. } else if (gen >= 4) {
  13134. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13135. return 16*1024;
  13136. else
  13137. return 32*1024;
  13138. } else if (gen >= 3) {
  13139. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13140. return 8*1024;
  13141. else
  13142. return 16*1024;
  13143. } else {
  13144. /* XXX DSPC is limited to 4k tiled */
  13145. return 8*1024;
  13146. }
  13147. }
  13148. static int intel_framebuffer_init(struct drm_device *dev,
  13149. struct intel_framebuffer *intel_fb,
  13150. struct drm_mode_fb_cmd2 *mode_cmd,
  13151. struct drm_i915_gem_object *obj)
  13152. {
  13153. struct drm_i915_private *dev_priv = to_i915(dev);
  13154. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13155. int ret;
  13156. u32 pitch_limit, stride_alignment;
  13157. struct drm_format_name_buf format_name;
  13158. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13159. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13160. /*
  13161. * If there's a fence, enforce that
  13162. * the fb modifier and tiling mode match.
  13163. */
  13164. if (tiling != I915_TILING_NONE &&
  13165. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13166. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13167. return -EINVAL;
  13168. }
  13169. } else {
  13170. if (tiling == I915_TILING_X) {
  13171. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13172. } else if (tiling == I915_TILING_Y) {
  13173. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13174. return -EINVAL;
  13175. }
  13176. }
  13177. /* Passed in modifier sanity checking. */
  13178. switch (mode_cmd->modifier[0]) {
  13179. case I915_FORMAT_MOD_Y_TILED:
  13180. case I915_FORMAT_MOD_Yf_TILED:
  13181. if (INTEL_GEN(dev_priv) < 9) {
  13182. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13183. mode_cmd->modifier[0]);
  13184. return -EINVAL;
  13185. }
  13186. case DRM_FORMAT_MOD_NONE:
  13187. case I915_FORMAT_MOD_X_TILED:
  13188. break;
  13189. default:
  13190. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13191. mode_cmd->modifier[0]);
  13192. return -EINVAL;
  13193. }
  13194. /*
  13195. * gen2/3 display engine uses the fence if present,
  13196. * so the tiling mode must match the fb modifier exactly.
  13197. */
  13198. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13199. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13200. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13201. return -EINVAL;
  13202. }
  13203. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13204. mode_cmd->modifier[0],
  13205. mode_cmd->pixel_format);
  13206. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13207. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13208. mode_cmd->pitches[0], stride_alignment);
  13209. return -EINVAL;
  13210. }
  13211. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13212. mode_cmd->pixel_format);
  13213. if (mode_cmd->pitches[0] > pitch_limit) {
  13214. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13215. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13216. "tiled" : "linear",
  13217. mode_cmd->pitches[0], pitch_limit);
  13218. return -EINVAL;
  13219. }
  13220. /*
  13221. * If there's a fence, enforce that
  13222. * the fb pitch and fence stride match.
  13223. */
  13224. if (tiling != I915_TILING_NONE &&
  13225. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13226. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13227. mode_cmd->pitches[0],
  13228. i915_gem_object_get_stride(obj));
  13229. return -EINVAL;
  13230. }
  13231. /* Reject formats not supported by any plane early. */
  13232. switch (mode_cmd->pixel_format) {
  13233. case DRM_FORMAT_C8:
  13234. case DRM_FORMAT_RGB565:
  13235. case DRM_FORMAT_XRGB8888:
  13236. case DRM_FORMAT_ARGB8888:
  13237. break;
  13238. case DRM_FORMAT_XRGB1555:
  13239. if (INTEL_GEN(dev_priv) > 3) {
  13240. DRM_DEBUG("unsupported pixel format: %s\n",
  13241. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13242. return -EINVAL;
  13243. }
  13244. break;
  13245. case DRM_FORMAT_ABGR8888:
  13246. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13247. INTEL_GEN(dev_priv) < 9) {
  13248. DRM_DEBUG("unsupported pixel format: %s\n",
  13249. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13250. return -EINVAL;
  13251. }
  13252. break;
  13253. case DRM_FORMAT_XBGR8888:
  13254. case DRM_FORMAT_XRGB2101010:
  13255. case DRM_FORMAT_XBGR2101010:
  13256. if (INTEL_GEN(dev_priv) < 4) {
  13257. DRM_DEBUG("unsupported pixel format: %s\n",
  13258. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13259. return -EINVAL;
  13260. }
  13261. break;
  13262. case DRM_FORMAT_ABGR2101010:
  13263. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13264. DRM_DEBUG("unsupported pixel format: %s\n",
  13265. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13266. return -EINVAL;
  13267. }
  13268. break;
  13269. case DRM_FORMAT_YUYV:
  13270. case DRM_FORMAT_UYVY:
  13271. case DRM_FORMAT_YVYU:
  13272. case DRM_FORMAT_VYUY:
  13273. if (INTEL_GEN(dev_priv) < 5) {
  13274. DRM_DEBUG("unsupported pixel format: %s\n",
  13275. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13276. return -EINVAL;
  13277. }
  13278. break;
  13279. default:
  13280. DRM_DEBUG("unsupported pixel format: %s\n",
  13281. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13282. return -EINVAL;
  13283. }
  13284. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13285. if (mode_cmd->offsets[0] != 0)
  13286. return -EINVAL;
  13287. drm_helper_mode_fill_fb_struct(dev, &intel_fb->base, mode_cmd);
  13288. intel_fb->obj = obj;
  13289. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13290. if (ret)
  13291. return ret;
  13292. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13293. if (ret) {
  13294. DRM_ERROR("framebuffer init failed %d\n", ret);
  13295. return ret;
  13296. }
  13297. intel_fb->obj->framebuffer_references++;
  13298. return 0;
  13299. }
  13300. static struct drm_framebuffer *
  13301. intel_user_framebuffer_create(struct drm_device *dev,
  13302. struct drm_file *filp,
  13303. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13304. {
  13305. struct drm_framebuffer *fb;
  13306. struct drm_i915_gem_object *obj;
  13307. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13308. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13309. if (!obj)
  13310. return ERR_PTR(-ENOENT);
  13311. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13312. if (IS_ERR(fb))
  13313. i915_gem_object_put(obj);
  13314. return fb;
  13315. }
  13316. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13317. .fb_create = intel_user_framebuffer_create,
  13318. .output_poll_changed = intel_fbdev_output_poll_changed,
  13319. .atomic_check = intel_atomic_check,
  13320. .atomic_commit = intel_atomic_commit,
  13321. .atomic_state_alloc = intel_atomic_state_alloc,
  13322. .atomic_state_clear = intel_atomic_state_clear,
  13323. };
  13324. /**
  13325. * intel_init_display_hooks - initialize the display modesetting hooks
  13326. * @dev_priv: device private
  13327. */
  13328. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13329. {
  13330. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13331. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13332. dev_priv->display.get_initial_plane_config =
  13333. skylake_get_initial_plane_config;
  13334. dev_priv->display.crtc_compute_clock =
  13335. haswell_crtc_compute_clock;
  13336. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13337. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13338. } else if (HAS_DDI(dev_priv)) {
  13339. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13340. dev_priv->display.get_initial_plane_config =
  13341. ironlake_get_initial_plane_config;
  13342. dev_priv->display.crtc_compute_clock =
  13343. haswell_crtc_compute_clock;
  13344. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13345. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13346. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13347. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13348. dev_priv->display.get_initial_plane_config =
  13349. ironlake_get_initial_plane_config;
  13350. dev_priv->display.crtc_compute_clock =
  13351. ironlake_crtc_compute_clock;
  13352. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13353. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13354. } else if (IS_CHERRYVIEW(dev_priv)) {
  13355. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13356. dev_priv->display.get_initial_plane_config =
  13357. i9xx_get_initial_plane_config;
  13358. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13359. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13360. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13361. } else if (IS_VALLEYVIEW(dev_priv)) {
  13362. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13363. dev_priv->display.get_initial_plane_config =
  13364. i9xx_get_initial_plane_config;
  13365. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13366. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13367. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13368. } else if (IS_G4X(dev_priv)) {
  13369. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13370. dev_priv->display.get_initial_plane_config =
  13371. i9xx_get_initial_plane_config;
  13372. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13373. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13374. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13375. } else if (IS_PINEVIEW(dev_priv)) {
  13376. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13377. dev_priv->display.get_initial_plane_config =
  13378. i9xx_get_initial_plane_config;
  13379. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13380. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13381. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13382. } else if (!IS_GEN2(dev_priv)) {
  13383. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13384. dev_priv->display.get_initial_plane_config =
  13385. i9xx_get_initial_plane_config;
  13386. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13387. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13388. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13389. } else {
  13390. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13391. dev_priv->display.get_initial_plane_config =
  13392. i9xx_get_initial_plane_config;
  13393. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13394. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13395. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13396. }
  13397. /* Returns the core display clock speed */
  13398. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13399. dev_priv->display.get_display_clock_speed =
  13400. skylake_get_display_clock_speed;
  13401. else if (IS_BROXTON(dev_priv))
  13402. dev_priv->display.get_display_clock_speed =
  13403. broxton_get_display_clock_speed;
  13404. else if (IS_BROADWELL(dev_priv))
  13405. dev_priv->display.get_display_clock_speed =
  13406. broadwell_get_display_clock_speed;
  13407. else if (IS_HASWELL(dev_priv))
  13408. dev_priv->display.get_display_clock_speed =
  13409. haswell_get_display_clock_speed;
  13410. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13411. dev_priv->display.get_display_clock_speed =
  13412. valleyview_get_display_clock_speed;
  13413. else if (IS_GEN5(dev_priv))
  13414. dev_priv->display.get_display_clock_speed =
  13415. ilk_get_display_clock_speed;
  13416. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  13417. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13418. dev_priv->display.get_display_clock_speed =
  13419. i945_get_display_clock_speed;
  13420. else if (IS_GM45(dev_priv))
  13421. dev_priv->display.get_display_clock_speed =
  13422. gm45_get_display_clock_speed;
  13423. else if (IS_CRESTLINE(dev_priv))
  13424. dev_priv->display.get_display_clock_speed =
  13425. i965gm_get_display_clock_speed;
  13426. else if (IS_PINEVIEW(dev_priv))
  13427. dev_priv->display.get_display_clock_speed =
  13428. pnv_get_display_clock_speed;
  13429. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13430. dev_priv->display.get_display_clock_speed =
  13431. g33_get_display_clock_speed;
  13432. else if (IS_I915G(dev_priv))
  13433. dev_priv->display.get_display_clock_speed =
  13434. i915_get_display_clock_speed;
  13435. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  13436. dev_priv->display.get_display_clock_speed =
  13437. i9xx_misc_get_display_clock_speed;
  13438. else if (IS_I915GM(dev_priv))
  13439. dev_priv->display.get_display_clock_speed =
  13440. i915gm_get_display_clock_speed;
  13441. else if (IS_I865G(dev_priv))
  13442. dev_priv->display.get_display_clock_speed =
  13443. i865_get_display_clock_speed;
  13444. else if (IS_I85X(dev_priv))
  13445. dev_priv->display.get_display_clock_speed =
  13446. i85x_get_display_clock_speed;
  13447. else { /* 830 */
  13448. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13449. dev_priv->display.get_display_clock_speed =
  13450. i830_get_display_clock_speed;
  13451. }
  13452. if (IS_GEN5(dev_priv)) {
  13453. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13454. } else if (IS_GEN6(dev_priv)) {
  13455. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13456. } else if (IS_IVYBRIDGE(dev_priv)) {
  13457. /* FIXME: detect B0+ stepping and use auto training */
  13458. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13459. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13460. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13461. }
  13462. if (IS_BROADWELL(dev_priv)) {
  13463. dev_priv->display.modeset_commit_cdclk =
  13464. broadwell_modeset_commit_cdclk;
  13465. dev_priv->display.modeset_calc_cdclk =
  13466. broadwell_modeset_calc_cdclk;
  13467. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13468. dev_priv->display.modeset_commit_cdclk =
  13469. valleyview_modeset_commit_cdclk;
  13470. dev_priv->display.modeset_calc_cdclk =
  13471. valleyview_modeset_calc_cdclk;
  13472. } else if (IS_BROXTON(dev_priv)) {
  13473. dev_priv->display.modeset_commit_cdclk =
  13474. bxt_modeset_commit_cdclk;
  13475. dev_priv->display.modeset_calc_cdclk =
  13476. bxt_modeset_calc_cdclk;
  13477. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13478. dev_priv->display.modeset_commit_cdclk =
  13479. skl_modeset_commit_cdclk;
  13480. dev_priv->display.modeset_calc_cdclk =
  13481. skl_modeset_calc_cdclk;
  13482. }
  13483. if (dev_priv->info.gen >= 9)
  13484. dev_priv->display.update_crtcs = skl_update_crtcs;
  13485. else
  13486. dev_priv->display.update_crtcs = intel_update_crtcs;
  13487. switch (INTEL_INFO(dev_priv)->gen) {
  13488. case 2:
  13489. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13490. break;
  13491. case 3:
  13492. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13493. break;
  13494. case 4:
  13495. case 5:
  13496. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13497. break;
  13498. case 6:
  13499. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13500. break;
  13501. case 7:
  13502. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13503. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13504. break;
  13505. case 9:
  13506. /* Drop through - unsupported since execlist only. */
  13507. default:
  13508. /* Default just returns -ENODEV to indicate unsupported */
  13509. dev_priv->display.queue_flip = intel_default_queue_flip;
  13510. }
  13511. }
  13512. /*
  13513. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13514. * resume, or other times. This quirk makes sure that's the case for
  13515. * affected systems.
  13516. */
  13517. static void quirk_pipea_force(struct drm_device *dev)
  13518. {
  13519. struct drm_i915_private *dev_priv = to_i915(dev);
  13520. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13521. DRM_INFO("applying pipe a force quirk\n");
  13522. }
  13523. static void quirk_pipeb_force(struct drm_device *dev)
  13524. {
  13525. struct drm_i915_private *dev_priv = to_i915(dev);
  13526. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13527. DRM_INFO("applying pipe b force quirk\n");
  13528. }
  13529. /*
  13530. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13531. */
  13532. static void quirk_ssc_force_disable(struct drm_device *dev)
  13533. {
  13534. struct drm_i915_private *dev_priv = to_i915(dev);
  13535. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13536. DRM_INFO("applying lvds SSC disable quirk\n");
  13537. }
  13538. /*
  13539. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13540. * brightness value
  13541. */
  13542. static void quirk_invert_brightness(struct drm_device *dev)
  13543. {
  13544. struct drm_i915_private *dev_priv = to_i915(dev);
  13545. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13546. DRM_INFO("applying inverted panel brightness quirk\n");
  13547. }
  13548. /* Some VBT's incorrectly indicate no backlight is present */
  13549. static void quirk_backlight_present(struct drm_device *dev)
  13550. {
  13551. struct drm_i915_private *dev_priv = to_i915(dev);
  13552. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13553. DRM_INFO("applying backlight present quirk\n");
  13554. }
  13555. struct intel_quirk {
  13556. int device;
  13557. int subsystem_vendor;
  13558. int subsystem_device;
  13559. void (*hook)(struct drm_device *dev);
  13560. };
  13561. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13562. struct intel_dmi_quirk {
  13563. void (*hook)(struct drm_device *dev);
  13564. const struct dmi_system_id (*dmi_id_list)[];
  13565. };
  13566. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13567. {
  13568. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13569. return 1;
  13570. }
  13571. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13572. {
  13573. .dmi_id_list = &(const struct dmi_system_id[]) {
  13574. {
  13575. .callback = intel_dmi_reverse_brightness,
  13576. .ident = "NCR Corporation",
  13577. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13578. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13579. },
  13580. },
  13581. { } /* terminating entry */
  13582. },
  13583. .hook = quirk_invert_brightness,
  13584. },
  13585. };
  13586. static struct intel_quirk intel_quirks[] = {
  13587. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13588. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13589. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13590. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13591. /* 830 needs to leave pipe A & dpll A up */
  13592. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13593. /* 830 needs to leave pipe B & dpll B up */
  13594. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13595. /* Lenovo U160 cannot use SSC on LVDS */
  13596. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13597. /* Sony Vaio Y cannot use SSC on LVDS */
  13598. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13599. /* Acer Aspire 5734Z must invert backlight brightness */
  13600. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13601. /* Acer/eMachines G725 */
  13602. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13603. /* Acer/eMachines e725 */
  13604. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13605. /* Acer/Packard Bell NCL20 */
  13606. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13607. /* Acer Aspire 4736Z */
  13608. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13609. /* Acer Aspire 5336 */
  13610. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13611. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13612. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13613. /* Acer C720 Chromebook (Core i3 4005U) */
  13614. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13615. /* Apple Macbook 2,1 (Core 2 T7400) */
  13616. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13617. /* Apple Macbook 4,1 */
  13618. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13619. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13620. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13621. /* HP Chromebook 14 (Celeron 2955U) */
  13622. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13623. /* Dell Chromebook 11 */
  13624. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13625. /* Dell Chromebook 11 (2015 version) */
  13626. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13627. };
  13628. static void intel_init_quirks(struct drm_device *dev)
  13629. {
  13630. struct pci_dev *d = dev->pdev;
  13631. int i;
  13632. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13633. struct intel_quirk *q = &intel_quirks[i];
  13634. if (d->device == q->device &&
  13635. (d->subsystem_vendor == q->subsystem_vendor ||
  13636. q->subsystem_vendor == PCI_ANY_ID) &&
  13637. (d->subsystem_device == q->subsystem_device ||
  13638. q->subsystem_device == PCI_ANY_ID))
  13639. q->hook(dev);
  13640. }
  13641. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13642. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13643. intel_dmi_quirks[i].hook(dev);
  13644. }
  13645. }
  13646. /* Disable the VGA plane that we never use */
  13647. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  13648. {
  13649. struct pci_dev *pdev = dev_priv->drm.pdev;
  13650. u8 sr1;
  13651. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13652. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13653. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13654. outb(SR01, VGA_SR_INDEX);
  13655. sr1 = inb(VGA_SR_DATA);
  13656. outb(sr1 | 1<<5, VGA_SR_DATA);
  13657. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13658. udelay(300);
  13659. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13660. POSTING_READ(vga_reg);
  13661. }
  13662. void intel_modeset_init_hw(struct drm_device *dev)
  13663. {
  13664. struct drm_i915_private *dev_priv = to_i915(dev);
  13665. intel_update_cdclk(dev_priv);
  13666. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13667. intel_init_clock_gating(dev_priv);
  13668. }
  13669. /*
  13670. * Calculate what we think the watermarks should be for the state we've read
  13671. * out of the hardware and then immediately program those watermarks so that
  13672. * we ensure the hardware settings match our internal state.
  13673. *
  13674. * We can calculate what we think WM's should be by creating a duplicate of the
  13675. * current state (which was constructed during hardware readout) and running it
  13676. * through the atomic check code to calculate new watermark values in the
  13677. * state object.
  13678. */
  13679. static void sanitize_watermarks(struct drm_device *dev)
  13680. {
  13681. struct drm_i915_private *dev_priv = to_i915(dev);
  13682. struct drm_atomic_state *state;
  13683. struct intel_atomic_state *intel_state;
  13684. struct drm_crtc *crtc;
  13685. struct drm_crtc_state *cstate;
  13686. struct drm_modeset_acquire_ctx ctx;
  13687. int ret;
  13688. int i;
  13689. /* Only supported on platforms that use atomic watermark design */
  13690. if (!dev_priv->display.optimize_watermarks)
  13691. return;
  13692. /*
  13693. * We need to hold connection_mutex before calling duplicate_state so
  13694. * that the connector loop is protected.
  13695. */
  13696. drm_modeset_acquire_init(&ctx, 0);
  13697. retry:
  13698. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13699. if (ret == -EDEADLK) {
  13700. drm_modeset_backoff(&ctx);
  13701. goto retry;
  13702. } else if (WARN_ON(ret)) {
  13703. goto fail;
  13704. }
  13705. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13706. if (WARN_ON(IS_ERR(state)))
  13707. goto fail;
  13708. intel_state = to_intel_atomic_state(state);
  13709. /*
  13710. * Hardware readout is the only time we don't want to calculate
  13711. * intermediate watermarks (since we don't trust the current
  13712. * watermarks).
  13713. */
  13714. intel_state->skip_intermediate_wm = true;
  13715. ret = intel_atomic_check(dev, state);
  13716. if (ret) {
  13717. /*
  13718. * If we fail here, it means that the hardware appears to be
  13719. * programmed in a way that shouldn't be possible, given our
  13720. * understanding of watermark requirements. This might mean a
  13721. * mistake in the hardware readout code or a mistake in the
  13722. * watermark calculations for a given platform. Raise a WARN
  13723. * so that this is noticeable.
  13724. *
  13725. * If this actually happens, we'll have to just leave the
  13726. * BIOS-programmed watermarks untouched and hope for the best.
  13727. */
  13728. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13729. goto put_state;
  13730. }
  13731. /* Write calculated watermark values back */
  13732. for_each_crtc_in_state(state, crtc, cstate, i) {
  13733. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13734. cs->wm.need_postvbl_update = true;
  13735. dev_priv->display.optimize_watermarks(intel_state, cs);
  13736. }
  13737. put_state:
  13738. drm_atomic_state_put(state);
  13739. fail:
  13740. drm_modeset_drop_locks(&ctx);
  13741. drm_modeset_acquire_fini(&ctx);
  13742. }
  13743. int intel_modeset_init(struct drm_device *dev)
  13744. {
  13745. struct drm_i915_private *dev_priv = to_i915(dev);
  13746. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13747. enum pipe pipe;
  13748. struct intel_crtc *crtc;
  13749. drm_mode_config_init(dev);
  13750. dev->mode_config.min_width = 0;
  13751. dev->mode_config.min_height = 0;
  13752. dev->mode_config.preferred_depth = 24;
  13753. dev->mode_config.prefer_shadow = 1;
  13754. dev->mode_config.allow_fb_modifiers = true;
  13755. dev->mode_config.funcs = &intel_mode_funcs;
  13756. intel_init_quirks(dev);
  13757. intel_init_pm(dev_priv);
  13758. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13759. return 0;
  13760. /*
  13761. * There may be no VBT; and if the BIOS enabled SSC we can
  13762. * just keep using it to avoid unnecessary flicker. Whereas if the
  13763. * BIOS isn't using it, don't assume it will work even if the VBT
  13764. * indicates as much.
  13765. */
  13766. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13767. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13768. DREF_SSC1_ENABLE);
  13769. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13770. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13771. bios_lvds_use_ssc ? "en" : "dis",
  13772. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13773. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13774. }
  13775. }
  13776. if (IS_GEN2(dev_priv)) {
  13777. dev->mode_config.max_width = 2048;
  13778. dev->mode_config.max_height = 2048;
  13779. } else if (IS_GEN3(dev_priv)) {
  13780. dev->mode_config.max_width = 4096;
  13781. dev->mode_config.max_height = 4096;
  13782. } else {
  13783. dev->mode_config.max_width = 8192;
  13784. dev->mode_config.max_height = 8192;
  13785. }
  13786. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  13787. dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
  13788. dev->mode_config.cursor_height = 1023;
  13789. } else if (IS_GEN2(dev_priv)) {
  13790. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13791. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13792. } else {
  13793. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13794. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13795. }
  13796. dev->mode_config.fb_base = ggtt->mappable_base;
  13797. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13798. INTEL_INFO(dev_priv)->num_pipes,
  13799. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13800. for_each_pipe(dev_priv, pipe) {
  13801. int ret;
  13802. ret = intel_crtc_init(dev_priv, pipe);
  13803. if (ret) {
  13804. drm_mode_config_cleanup(dev);
  13805. return ret;
  13806. }
  13807. }
  13808. intel_update_czclk(dev_priv);
  13809. intel_update_cdclk(dev_priv);
  13810. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13811. intel_shared_dpll_init(dev);
  13812. if (dev_priv->max_cdclk_freq == 0)
  13813. intel_update_max_cdclk(dev_priv);
  13814. /* Just disable it once at startup */
  13815. i915_disable_vga(dev_priv);
  13816. intel_setup_outputs(dev);
  13817. drm_modeset_lock_all(dev);
  13818. intel_modeset_setup_hw_state(dev);
  13819. drm_modeset_unlock_all(dev);
  13820. for_each_intel_crtc(dev, crtc) {
  13821. struct intel_initial_plane_config plane_config = {};
  13822. if (!crtc->active)
  13823. continue;
  13824. /*
  13825. * Note that reserving the BIOS fb up front prevents us
  13826. * from stuffing other stolen allocations like the ring
  13827. * on top. This prevents some ugliness at boot time, and
  13828. * can even allow for smooth boot transitions if the BIOS
  13829. * fb is large enough for the active pipe configuration.
  13830. */
  13831. dev_priv->display.get_initial_plane_config(crtc,
  13832. &plane_config);
  13833. /*
  13834. * If the fb is shared between multiple heads, we'll
  13835. * just get the first one.
  13836. */
  13837. intel_find_initial_plane_obj(crtc, &plane_config);
  13838. }
  13839. /*
  13840. * Make sure hardware watermarks really match the state we read out.
  13841. * Note that we need to do this after reconstructing the BIOS fb's
  13842. * since the watermark calculation done here will use pstate->fb.
  13843. */
  13844. sanitize_watermarks(dev);
  13845. return 0;
  13846. }
  13847. static void intel_enable_pipe_a(struct drm_device *dev)
  13848. {
  13849. struct intel_connector *connector;
  13850. struct drm_connector *crt = NULL;
  13851. struct intel_load_detect_pipe load_detect_temp;
  13852. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13853. /* We can't just switch on the pipe A, we need to set things up with a
  13854. * proper mode and output configuration. As a gross hack, enable pipe A
  13855. * by enabling the load detect pipe once. */
  13856. for_each_intel_connector(dev, connector) {
  13857. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13858. crt = &connector->base;
  13859. break;
  13860. }
  13861. }
  13862. if (!crt)
  13863. return;
  13864. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13865. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13866. }
  13867. static bool
  13868. intel_check_plane_mapping(struct intel_crtc *crtc)
  13869. {
  13870. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  13871. u32 val;
  13872. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  13873. return true;
  13874. val = I915_READ(DSPCNTR(!crtc->plane));
  13875. if ((val & DISPLAY_PLANE_ENABLE) &&
  13876. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13877. return false;
  13878. return true;
  13879. }
  13880. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13881. {
  13882. struct drm_device *dev = crtc->base.dev;
  13883. struct intel_encoder *encoder;
  13884. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13885. return true;
  13886. return false;
  13887. }
  13888. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13889. {
  13890. struct drm_device *dev = encoder->base.dev;
  13891. struct intel_connector *connector;
  13892. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13893. return connector;
  13894. return NULL;
  13895. }
  13896. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13897. enum transcoder pch_transcoder)
  13898. {
  13899. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13900. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13901. }
  13902. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13903. {
  13904. struct drm_device *dev = crtc->base.dev;
  13905. struct drm_i915_private *dev_priv = to_i915(dev);
  13906. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13907. /* Clear any frame start delays used for debugging left by the BIOS */
  13908. if (!transcoder_is_dsi(cpu_transcoder)) {
  13909. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13910. I915_WRITE(reg,
  13911. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13912. }
  13913. /* restore vblank interrupts to correct state */
  13914. drm_crtc_vblank_reset(&crtc->base);
  13915. if (crtc->active) {
  13916. struct intel_plane *plane;
  13917. drm_crtc_vblank_on(&crtc->base);
  13918. /* Disable everything but the primary plane */
  13919. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13920. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13921. continue;
  13922. plane->disable_plane(&plane->base, &crtc->base);
  13923. }
  13924. }
  13925. /* We need to sanitize the plane -> pipe mapping first because this will
  13926. * disable the crtc (and hence change the state) if it is wrong. Note
  13927. * that gen4+ has a fixed plane -> pipe mapping. */
  13928. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  13929. bool plane;
  13930. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13931. crtc->base.base.id, crtc->base.name);
  13932. /* Pipe has the wrong plane attached and the plane is active.
  13933. * Temporarily change the plane mapping and disable everything
  13934. * ... */
  13935. plane = crtc->plane;
  13936. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  13937. crtc->plane = !plane;
  13938. intel_crtc_disable_noatomic(&crtc->base);
  13939. crtc->plane = plane;
  13940. }
  13941. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13942. crtc->pipe == PIPE_A && !crtc->active) {
  13943. /* BIOS forgot to enable pipe A, this mostly happens after
  13944. * resume. Force-enable the pipe to fix this, the update_dpms
  13945. * call below we restore the pipe to the right state, but leave
  13946. * the required bits on. */
  13947. intel_enable_pipe_a(dev);
  13948. }
  13949. /* Adjust the state of the output pipe according to whether we
  13950. * have active connectors/encoders. */
  13951. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13952. intel_crtc_disable_noatomic(&crtc->base);
  13953. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  13954. /*
  13955. * We start out with underrun reporting disabled to avoid races.
  13956. * For correct bookkeeping mark this on active crtcs.
  13957. *
  13958. * Also on gmch platforms we dont have any hardware bits to
  13959. * disable the underrun reporting. Which means we need to start
  13960. * out with underrun reporting disabled also on inactive pipes,
  13961. * since otherwise we'll complain about the garbage we read when
  13962. * e.g. coming up after runtime pm.
  13963. *
  13964. * No protection against concurrent access is required - at
  13965. * worst a fifo underrun happens which also sets this to false.
  13966. */
  13967. crtc->cpu_fifo_underrun_disabled = true;
  13968. /*
  13969. * We track the PCH trancoder underrun reporting state
  13970. * within the crtc. With crtc for pipe A housing the underrun
  13971. * reporting state for PCH transcoder A, crtc for pipe B housing
  13972. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  13973. * and marking underrun reporting as disabled for the non-existing
  13974. * PCH transcoders B and C would prevent enabling the south
  13975. * error interrupt (see cpt_can_enable_serr_int()).
  13976. */
  13977. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  13978. crtc->pch_fifo_underrun_disabled = true;
  13979. }
  13980. }
  13981. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13982. {
  13983. struct intel_connector *connector;
  13984. /* We need to check both for a crtc link (meaning that the
  13985. * encoder is active and trying to read from a pipe) and the
  13986. * pipe itself being active. */
  13987. bool has_active_crtc = encoder->base.crtc &&
  13988. to_intel_crtc(encoder->base.crtc)->active;
  13989. connector = intel_encoder_find_connector(encoder);
  13990. if (connector && !has_active_crtc) {
  13991. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13992. encoder->base.base.id,
  13993. encoder->base.name);
  13994. /* Connector is active, but has no active pipe. This is
  13995. * fallout from our resume register restoring. Disable
  13996. * the encoder manually again. */
  13997. if (encoder->base.crtc) {
  13998. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  13999. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14000. encoder->base.base.id,
  14001. encoder->base.name);
  14002. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14003. if (encoder->post_disable)
  14004. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14005. }
  14006. encoder->base.crtc = NULL;
  14007. /* Inconsistent output/port/pipe state happens presumably due to
  14008. * a bug in one of the get_hw_state functions. Or someplace else
  14009. * in our code, like the register restore mess on resume. Clamp
  14010. * things to off as a safer default. */
  14011. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14012. connector->base.encoder = NULL;
  14013. }
  14014. /* Enabled encoders without active connectors will be fixed in
  14015. * the crtc fixup. */
  14016. }
  14017. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  14018. {
  14019. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14020. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14021. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14022. i915_disable_vga(dev_priv);
  14023. }
  14024. }
  14025. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  14026. {
  14027. /* This function can be called both from intel_modeset_setup_hw_state or
  14028. * at a very early point in our resume sequence, where the power well
  14029. * structures are not yet restored. Since this function is at a very
  14030. * paranoid "someone might have enabled VGA while we were not looking"
  14031. * level, just check if the power well is enabled instead of trying to
  14032. * follow the "don't touch the power well if we don't need it" policy
  14033. * the rest of the driver uses. */
  14034. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14035. return;
  14036. i915_redisable_vga_power_on(dev_priv);
  14037. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14038. }
  14039. static bool primary_get_hw_state(struct intel_plane *plane)
  14040. {
  14041. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14042. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14043. }
  14044. /* FIXME read out full plane state for all planes */
  14045. static void readout_plane_state(struct intel_crtc *crtc)
  14046. {
  14047. struct drm_plane *primary = crtc->base.primary;
  14048. struct intel_plane_state *plane_state =
  14049. to_intel_plane_state(primary->state);
  14050. plane_state->base.visible = crtc->active &&
  14051. primary_get_hw_state(to_intel_plane(primary));
  14052. if (plane_state->base.visible)
  14053. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14054. }
  14055. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14056. {
  14057. struct drm_i915_private *dev_priv = to_i915(dev);
  14058. enum pipe pipe;
  14059. struct intel_crtc *crtc;
  14060. struct intel_encoder *encoder;
  14061. struct intel_connector *connector;
  14062. int i;
  14063. dev_priv->active_crtcs = 0;
  14064. for_each_intel_crtc(dev, crtc) {
  14065. struct intel_crtc_state *crtc_state = crtc->config;
  14066. int pixclk = 0;
  14067. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14068. memset(crtc_state, 0, sizeof(*crtc_state));
  14069. crtc_state->base.crtc = &crtc->base;
  14070. crtc_state->base.active = crtc_state->base.enable =
  14071. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14072. crtc->base.enabled = crtc_state->base.enable;
  14073. crtc->active = crtc_state->base.active;
  14074. if (crtc_state->base.active) {
  14075. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14076. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14077. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14078. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14079. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14080. else
  14081. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14082. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14083. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14084. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14085. }
  14086. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14087. readout_plane_state(crtc);
  14088. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14089. crtc->base.base.id, crtc->base.name,
  14090. enableddisabled(crtc->active));
  14091. }
  14092. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14093. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14094. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14095. &pll->config.hw_state);
  14096. pll->config.crtc_mask = 0;
  14097. for_each_intel_crtc(dev, crtc) {
  14098. if (crtc->active && crtc->config->shared_dpll == pll)
  14099. pll->config.crtc_mask |= 1 << crtc->pipe;
  14100. }
  14101. pll->active_mask = pll->config.crtc_mask;
  14102. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14103. pll->name, pll->config.crtc_mask, pll->on);
  14104. }
  14105. for_each_intel_encoder(dev, encoder) {
  14106. pipe = 0;
  14107. if (encoder->get_hw_state(encoder, &pipe)) {
  14108. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14109. encoder->base.crtc = &crtc->base;
  14110. crtc->config->output_types |= 1 << encoder->type;
  14111. encoder->get_config(encoder, crtc->config);
  14112. } else {
  14113. encoder->base.crtc = NULL;
  14114. }
  14115. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14116. encoder->base.base.id, encoder->base.name,
  14117. enableddisabled(encoder->base.crtc),
  14118. pipe_name(pipe));
  14119. }
  14120. for_each_intel_connector(dev, connector) {
  14121. if (connector->get_hw_state(connector)) {
  14122. connector->base.dpms = DRM_MODE_DPMS_ON;
  14123. encoder = connector->encoder;
  14124. connector->base.encoder = &encoder->base;
  14125. if (encoder->base.crtc &&
  14126. encoder->base.crtc->state->active) {
  14127. /*
  14128. * This has to be done during hardware readout
  14129. * because anything calling .crtc_disable may
  14130. * rely on the connector_mask being accurate.
  14131. */
  14132. encoder->base.crtc->state->connector_mask |=
  14133. 1 << drm_connector_index(&connector->base);
  14134. encoder->base.crtc->state->encoder_mask |=
  14135. 1 << drm_encoder_index(&encoder->base);
  14136. }
  14137. } else {
  14138. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14139. connector->base.encoder = NULL;
  14140. }
  14141. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14142. connector->base.base.id, connector->base.name,
  14143. enableddisabled(connector->base.encoder));
  14144. }
  14145. for_each_intel_crtc(dev, crtc) {
  14146. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14147. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14148. if (crtc->base.state->active) {
  14149. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14150. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14151. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14152. /*
  14153. * The initial mode needs to be set in order to keep
  14154. * the atomic core happy. It wants a valid mode if the
  14155. * crtc's enabled, so we do the above call.
  14156. *
  14157. * At this point some state updated by the connectors
  14158. * in their ->detect() callback has not run yet, so
  14159. * no recalculation can be done yet.
  14160. *
  14161. * Even if we could do a recalculation and modeset
  14162. * right now it would cause a double modeset if
  14163. * fbdev or userspace chooses a different initial mode.
  14164. *
  14165. * If that happens, someone indicated they wanted a
  14166. * mode change, which means it's safe to do a full
  14167. * recalculation.
  14168. */
  14169. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14170. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14171. update_scanline_offset(crtc);
  14172. }
  14173. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14174. }
  14175. }
  14176. /* Scan out the current hw modeset state,
  14177. * and sanitizes it to the current state
  14178. */
  14179. static void
  14180. intel_modeset_setup_hw_state(struct drm_device *dev)
  14181. {
  14182. struct drm_i915_private *dev_priv = to_i915(dev);
  14183. enum pipe pipe;
  14184. struct intel_crtc *crtc;
  14185. struct intel_encoder *encoder;
  14186. int i;
  14187. intel_modeset_readout_hw_state(dev);
  14188. /* HW state is read out, now we need to sanitize this mess. */
  14189. for_each_intel_encoder(dev, encoder) {
  14190. intel_sanitize_encoder(encoder);
  14191. }
  14192. for_each_pipe(dev_priv, pipe) {
  14193. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14194. intel_sanitize_crtc(crtc);
  14195. intel_dump_pipe_config(crtc, crtc->config,
  14196. "[setup_hw_state]");
  14197. }
  14198. intel_modeset_update_connector_atomic_state(dev);
  14199. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14200. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14201. if (!pll->on || pll->active_mask)
  14202. continue;
  14203. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14204. pll->funcs.disable(dev_priv, pll);
  14205. pll->on = false;
  14206. }
  14207. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14208. vlv_wm_get_hw_state(dev);
  14209. else if (IS_GEN9(dev_priv))
  14210. skl_wm_get_hw_state(dev);
  14211. else if (HAS_PCH_SPLIT(dev_priv))
  14212. ilk_wm_get_hw_state(dev);
  14213. for_each_intel_crtc(dev, crtc) {
  14214. unsigned long put_domains;
  14215. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14216. if (WARN_ON(put_domains))
  14217. modeset_put_power_domains(dev_priv, put_domains);
  14218. }
  14219. intel_display_set_init_power(dev_priv, false);
  14220. intel_fbc_init_pipe_state(dev_priv);
  14221. }
  14222. void intel_display_resume(struct drm_device *dev)
  14223. {
  14224. struct drm_i915_private *dev_priv = to_i915(dev);
  14225. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14226. struct drm_modeset_acquire_ctx ctx;
  14227. int ret;
  14228. dev_priv->modeset_restore_state = NULL;
  14229. if (state)
  14230. state->acquire_ctx = &ctx;
  14231. /*
  14232. * This is a cludge because with real atomic modeset mode_config.mutex
  14233. * won't be taken. Unfortunately some probed state like
  14234. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14235. * it here for now.
  14236. */
  14237. mutex_lock(&dev->mode_config.mutex);
  14238. drm_modeset_acquire_init(&ctx, 0);
  14239. while (1) {
  14240. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14241. if (ret != -EDEADLK)
  14242. break;
  14243. drm_modeset_backoff(&ctx);
  14244. }
  14245. if (!ret)
  14246. ret = __intel_display_resume(dev, state);
  14247. drm_modeset_drop_locks(&ctx);
  14248. drm_modeset_acquire_fini(&ctx);
  14249. mutex_unlock(&dev->mode_config.mutex);
  14250. if (ret)
  14251. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14252. drm_atomic_state_put(state);
  14253. }
  14254. void intel_modeset_gem_init(struct drm_device *dev)
  14255. {
  14256. struct drm_i915_private *dev_priv = to_i915(dev);
  14257. struct drm_crtc *c;
  14258. struct drm_i915_gem_object *obj;
  14259. intel_init_gt_powersave(dev_priv);
  14260. intel_modeset_init_hw(dev);
  14261. intel_setup_overlay(dev_priv);
  14262. /*
  14263. * Make sure any fbs we allocated at startup are properly
  14264. * pinned & fenced. When we do the allocation it's too early
  14265. * for this.
  14266. */
  14267. for_each_crtc(dev, c) {
  14268. struct i915_vma *vma;
  14269. obj = intel_fb_obj(c->primary->fb);
  14270. if (obj == NULL)
  14271. continue;
  14272. mutex_lock(&dev->struct_mutex);
  14273. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14274. c->primary->state->rotation);
  14275. mutex_unlock(&dev->struct_mutex);
  14276. if (IS_ERR(vma)) {
  14277. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14278. to_intel_crtc(c)->pipe);
  14279. drm_framebuffer_unreference(c->primary->fb);
  14280. c->primary->fb = NULL;
  14281. c->primary->crtc = c->primary->state->crtc = NULL;
  14282. update_state_fb(c->primary);
  14283. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14284. }
  14285. }
  14286. }
  14287. int intel_connector_register(struct drm_connector *connector)
  14288. {
  14289. struct intel_connector *intel_connector = to_intel_connector(connector);
  14290. int ret;
  14291. ret = intel_backlight_device_register(intel_connector);
  14292. if (ret)
  14293. goto err;
  14294. return 0;
  14295. err:
  14296. return ret;
  14297. }
  14298. void intel_connector_unregister(struct drm_connector *connector)
  14299. {
  14300. struct intel_connector *intel_connector = to_intel_connector(connector);
  14301. intel_backlight_device_unregister(intel_connector);
  14302. intel_panel_destroy_backlight(connector);
  14303. }
  14304. void intel_modeset_cleanup(struct drm_device *dev)
  14305. {
  14306. struct drm_i915_private *dev_priv = to_i915(dev);
  14307. intel_disable_gt_powersave(dev_priv);
  14308. /*
  14309. * Interrupts and polling as the first thing to avoid creating havoc.
  14310. * Too much stuff here (turning of connectors, ...) would
  14311. * experience fancy races otherwise.
  14312. */
  14313. intel_irq_uninstall(dev_priv);
  14314. /*
  14315. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14316. * poll handlers. Hence disable polling after hpd handling is shut down.
  14317. */
  14318. drm_kms_helper_poll_fini(dev);
  14319. intel_unregister_dsm_handler();
  14320. intel_fbc_global_disable(dev_priv);
  14321. /* flush any delayed tasks or pending work */
  14322. flush_scheduled_work();
  14323. drm_mode_config_cleanup(dev);
  14324. intel_cleanup_overlay(dev_priv);
  14325. intel_cleanup_gt_powersave(dev_priv);
  14326. intel_teardown_gmbus(dev);
  14327. }
  14328. void intel_connector_attach_encoder(struct intel_connector *connector,
  14329. struct intel_encoder *encoder)
  14330. {
  14331. connector->encoder = encoder;
  14332. drm_mode_connector_attach_encoder(&connector->base,
  14333. &encoder->base);
  14334. }
  14335. /*
  14336. * set vga decode state - true == enable VGA decode
  14337. */
  14338. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  14339. {
  14340. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14341. u16 gmch_ctrl;
  14342. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14343. DRM_ERROR("failed to read control word\n");
  14344. return -EIO;
  14345. }
  14346. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14347. return 0;
  14348. if (state)
  14349. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14350. else
  14351. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14352. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14353. DRM_ERROR("failed to write control word\n");
  14354. return -EIO;
  14355. }
  14356. return 0;
  14357. }
  14358. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14359. struct intel_display_error_state {
  14360. u32 power_well_driver;
  14361. int num_transcoders;
  14362. struct intel_cursor_error_state {
  14363. u32 control;
  14364. u32 position;
  14365. u32 base;
  14366. u32 size;
  14367. } cursor[I915_MAX_PIPES];
  14368. struct intel_pipe_error_state {
  14369. bool power_domain_on;
  14370. u32 source;
  14371. u32 stat;
  14372. } pipe[I915_MAX_PIPES];
  14373. struct intel_plane_error_state {
  14374. u32 control;
  14375. u32 stride;
  14376. u32 size;
  14377. u32 pos;
  14378. u32 addr;
  14379. u32 surface;
  14380. u32 tile_offset;
  14381. } plane[I915_MAX_PIPES];
  14382. struct intel_transcoder_error_state {
  14383. bool power_domain_on;
  14384. enum transcoder cpu_transcoder;
  14385. u32 conf;
  14386. u32 htotal;
  14387. u32 hblank;
  14388. u32 hsync;
  14389. u32 vtotal;
  14390. u32 vblank;
  14391. u32 vsync;
  14392. } transcoder[4];
  14393. };
  14394. struct intel_display_error_state *
  14395. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14396. {
  14397. struct intel_display_error_state *error;
  14398. int transcoders[] = {
  14399. TRANSCODER_A,
  14400. TRANSCODER_B,
  14401. TRANSCODER_C,
  14402. TRANSCODER_EDP,
  14403. };
  14404. int i;
  14405. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14406. return NULL;
  14407. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14408. if (error == NULL)
  14409. return NULL;
  14410. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14411. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14412. for_each_pipe(dev_priv, i) {
  14413. error->pipe[i].power_domain_on =
  14414. __intel_display_power_is_enabled(dev_priv,
  14415. POWER_DOMAIN_PIPE(i));
  14416. if (!error->pipe[i].power_domain_on)
  14417. continue;
  14418. error->cursor[i].control = I915_READ(CURCNTR(i));
  14419. error->cursor[i].position = I915_READ(CURPOS(i));
  14420. error->cursor[i].base = I915_READ(CURBASE(i));
  14421. error->plane[i].control = I915_READ(DSPCNTR(i));
  14422. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14423. if (INTEL_GEN(dev_priv) <= 3) {
  14424. error->plane[i].size = I915_READ(DSPSIZE(i));
  14425. error->plane[i].pos = I915_READ(DSPPOS(i));
  14426. }
  14427. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14428. error->plane[i].addr = I915_READ(DSPADDR(i));
  14429. if (INTEL_GEN(dev_priv) >= 4) {
  14430. error->plane[i].surface = I915_READ(DSPSURF(i));
  14431. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14432. }
  14433. error->pipe[i].source = I915_READ(PIPESRC(i));
  14434. if (HAS_GMCH_DISPLAY(dev_priv))
  14435. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14436. }
  14437. /* Note: this does not include DSI transcoders. */
  14438. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14439. if (HAS_DDI(dev_priv))
  14440. error->num_transcoders++; /* Account for eDP. */
  14441. for (i = 0; i < error->num_transcoders; i++) {
  14442. enum transcoder cpu_transcoder = transcoders[i];
  14443. error->transcoder[i].power_domain_on =
  14444. __intel_display_power_is_enabled(dev_priv,
  14445. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14446. if (!error->transcoder[i].power_domain_on)
  14447. continue;
  14448. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14449. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14450. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14451. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14452. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14453. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14454. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14455. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14456. }
  14457. return error;
  14458. }
  14459. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14460. void
  14461. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14462. struct drm_i915_private *dev_priv,
  14463. struct intel_display_error_state *error)
  14464. {
  14465. int i;
  14466. if (!error)
  14467. return;
  14468. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14469. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14470. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14471. error->power_well_driver);
  14472. for_each_pipe(dev_priv, i) {
  14473. err_printf(m, "Pipe [%d]:\n", i);
  14474. err_printf(m, " Power: %s\n",
  14475. onoff(error->pipe[i].power_domain_on));
  14476. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14477. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14478. err_printf(m, "Plane [%d]:\n", i);
  14479. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14480. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14481. if (INTEL_GEN(dev_priv) <= 3) {
  14482. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14483. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14484. }
  14485. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14486. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14487. if (INTEL_GEN(dev_priv) >= 4) {
  14488. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14489. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14490. }
  14491. err_printf(m, "Cursor [%d]:\n", i);
  14492. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14493. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14494. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14495. }
  14496. for (i = 0; i < error->num_transcoders; i++) {
  14497. err_printf(m, "CPU transcoder: %s\n",
  14498. transcoder_name(error->transcoder[i].cpu_transcoder));
  14499. err_printf(m, " Power: %s\n",
  14500. onoff(error->transcoder[i].power_domain_on));
  14501. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14502. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14503. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14504. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14505. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14506. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14507. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14508. }
  14509. }
  14510. #endif