intel_display.c 461 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_gem_dmabuf.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_device *dev, struct intel_crtc *intel_crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  118. static int bxt_calc_cdclk(int max_pixclk);
  119. struct intel_limit {
  120. struct {
  121. int min, max;
  122. } dot, vco, n, m, m1, m2, p, p1;
  123. struct {
  124. int dot_limit;
  125. int p2_slow, p2_fast;
  126. } p2;
  127. };
  128. /* returns HPLL frequency in kHz */
  129. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  130. {
  131. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  132. /* Obtain SKU information */
  133. mutex_lock(&dev_priv->sb_lock);
  134. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  135. CCK_FUSE_HPLL_FREQ_MASK;
  136. mutex_unlock(&dev_priv->sb_lock);
  137. return vco_freq[hpll_freq] * 1000;
  138. }
  139. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  140. const char *name, u32 reg, int ref_freq)
  141. {
  142. u32 val;
  143. int divider;
  144. mutex_lock(&dev_priv->sb_lock);
  145. val = vlv_cck_read(dev_priv, reg);
  146. mutex_unlock(&dev_priv->sb_lock);
  147. divider = val & CCK_FREQUENCY_VALUES;
  148. WARN((val & CCK_FREQUENCY_STATUS) !=
  149. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  150. "%s change in progress\n", name);
  151. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  152. }
  153. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  154. const char *name, u32 reg)
  155. {
  156. if (dev_priv->hpll_freq == 0)
  157. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  158. return vlv_get_cck_clock(dev_priv, name, reg,
  159. dev_priv->hpll_freq);
  160. }
  161. static int
  162. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  163. {
  164. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  165. }
  166. static int
  167. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  168. {
  169. /* RAWCLK_FREQ_VLV register updated from power well code */
  170. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  171. CCK_DISPLAY_REF_CLOCK_CONTROL);
  172. }
  173. static int
  174. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  175. {
  176. uint32_t clkcfg;
  177. /* hrawclock is 1/4 the FSB frequency */
  178. clkcfg = I915_READ(CLKCFG);
  179. switch (clkcfg & CLKCFG_FSB_MASK) {
  180. case CLKCFG_FSB_400:
  181. return 100000;
  182. case CLKCFG_FSB_533:
  183. return 133333;
  184. case CLKCFG_FSB_667:
  185. return 166667;
  186. case CLKCFG_FSB_800:
  187. return 200000;
  188. case CLKCFG_FSB_1067:
  189. return 266667;
  190. case CLKCFG_FSB_1333:
  191. return 333333;
  192. /* these two are just a guess; one of them might be right */
  193. case CLKCFG_FSB_1600:
  194. case CLKCFG_FSB_1600_ALT:
  195. return 400000;
  196. default:
  197. return 133333;
  198. }
  199. }
  200. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  201. {
  202. if (HAS_PCH_SPLIT(dev_priv))
  203. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  204. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  205. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  206. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  207. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  208. else
  209. return; /* no rawclk on other platforms, or no need to know it */
  210. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  211. }
  212. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  213. {
  214. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  215. return;
  216. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  217. CCK_CZ_CLOCK_CONTROL);
  218. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  219. }
  220. static inline u32 /* units of 100MHz */
  221. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  222. const struct intel_crtc_state *pipe_config)
  223. {
  224. if (HAS_DDI(dev_priv))
  225. return pipe_config->port_clock; /* SPLL */
  226. else if (IS_GEN5(dev_priv))
  227. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  228. else
  229. return 270000;
  230. }
  231. static const struct intel_limit intel_limits_i8xx_dac = {
  232. .dot = { .min = 25000, .max = 350000 },
  233. .vco = { .min = 908000, .max = 1512000 },
  234. .n = { .min = 2, .max = 16 },
  235. .m = { .min = 96, .max = 140 },
  236. .m1 = { .min = 18, .max = 26 },
  237. .m2 = { .min = 6, .max = 16 },
  238. .p = { .min = 4, .max = 128 },
  239. .p1 = { .min = 2, .max = 33 },
  240. .p2 = { .dot_limit = 165000,
  241. .p2_slow = 4, .p2_fast = 2 },
  242. };
  243. static const struct intel_limit intel_limits_i8xx_dvo = {
  244. .dot = { .min = 25000, .max = 350000 },
  245. .vco = { .min = 908000, .max = 1512000 },
  246. .n = { .min = 2, .max = 16 },
  247. .m = { .min = 96, .max = 140 },
  248. .m1 = { .min = 18, .max = 26 },
  249. .m2 = { .min = 6, .max = 16 },
  250. .p = { .min = 4, .max = 128 },
  251. .p1 = { .min = 2, .max = 33 },
  252. .p2 = { .dot_limit = 165000,
  253. .p2_slow = 4, .p2_fast = 4 },
  254. };
  255. static const struct intel_limit intel_limits_i8xx_lvds = {
  256. .dot = { .min = 25000, .max = 350000 },
  257. .vco = { .min = 908000, .max = 1512000 },
  258. .n = { .min = 2, .max = 16 },
  259. .m = { .min = 96, .max = 140 },
  260. .m1 = { .min = 18, .max = 26 },
  261. .m2 = { .min = 6, .max = 16 },
  262. .p = { .min = 4, .max = 128 },
  263. .p1 = { .min = 1, .max = 6 },
  264. .p2 = { .dot_limit = 165000,
  265. .p2_slow = 14, .p2_fast = 7 },
  266. };
  267. static const struct intel_limit intel_limits_i9xx_sdvo = {
  268. .dot = { .min = 20000, .max = 400000 },
  269. .vco = { .min = 1400000, .max = 2800000 },
  270. .n = { .min = 1, .max = 6 },
  271. .m = { .min = 70, .max = 120 },
  272. .m1 = { .min = 8, .max = 18 },
  273. .m2 = { .min = 3, .max = 7 },
  274. .p = { .min = 5, .max = 80 },
  275. .p1 = { .min = 1, .max = 8 },
  276. .p2 = { .dot_limit = 200000,
  277. .p2_slow = 10, .p2_fast = 5 },
  278. };
  279. static const struct intel_limit intel_limits_i9xx_lvds = {
  280. .dot = { .min = 20000, .max = 400000 },
  281. .vco = { .min = 1400000, .max = 2800000 },
  282. .n = { .min = 1, .max = 6 },
  283. .m = { .min = 70, .max = 120 },
  284. .m1 = { .min = 8, .max = 18 },
  285. .m2 = { .min = 3, .max = 7 },
  286. .p = { .min = 7, .max = 98 },
  287. .p1 = { .min = 1, .max = 8 },
  288. .p2 = { .dot_limit = 112000,
  289. .p2_slow = 14, .p2_fast = 7 },
  290. };
  291. static const struct intel_limit intel_limits_g4x_sdvo = {
  292. .dot = { .min = 25000, .max = 270000 },
  293. .vco = { .min = 1750000, .max = 3500000},
  294. .n = { .min = 1, .max = 4 },
  295. .m = { .min = 104, .max = 138 },
  296. .m1 = { .min = 17, .max = 23 },
  297. .m2 = { .min = 5, .max = 11 },
  298. .p = { .min = 10, .max = 30 },
  299. .p1 = { .min = 1, .max = 3},
  300. .p2 = { .dot_limit = 270000,
  301. .p2_slow = 10,
  302. .p2_fast = 10
  303. },
  304. };
  305. static const struct intel_limit intel_limits_g4x_hdmi = {
  306. .dot = { .min = 22000, .max = 400000 },
  307. .vco = { .min = 1750000, .max = 3500000},
  308. .n = { .min = 1, .max = 4 },
  309. .m = { .min = 104, .max = 138 },
  310. .m1 = { .min = 16, .max = 23 },
  311. .m2 = { .min = 5, .max = 11 },
  312. .p = { .min = 5, .max = 80 },
  313. .p1 = { .min = 1, .max = 8},
  314. .p2 = { .dot_limit = 165000,
  315. .p2_slow = 10, .p2_fast = 5 },
  316. };
  317. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  318. .dot = { .min = 20000, .max = 115000 },
  319. .vco = { .min = 1750000, .max = 3500000 },
  320. .n = { .min = 1, .max = 3 },
  321. .m = { .min = 104, .max = 138 },
  322. .m1 = { .min = 17, .max = 23 },
  323. .m2 = { .min = 5, .max = 11 },
  324. .p = { .min = 28, .max = 112 },
  325. .p1 = { .min = 2, .max = 8 },
  326. .p2 = { .dot_limit = 0,
  327. .p2_slow = 14, .p2_fast = 14
  328. },
  329. };
  330. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  331. .dot = { .min = 80000, .max = 224000 },
  332. .vco = { .min = 1750000, .max = 3500000 },
  333. .n = { .min = 1, .max = 3 },
  334. .m = { .min = 104, .max = 138 },
  335. .m1 = { .min = 17, .max = 23 },
  336. .m2 = { .min = 5, .max = 11 },
  337. .p = { .min = 14, .max = 42 },
  338. .p1 = { .min = 2, .max = 6 },
  339. .p2 = { .dot_limit = 0,
  340. .p2_slow = 7, .p2_fast = 7
  341. },
  342. };
  343. static const struct intel_limit intel_limits_pineview_sdvo = {
  344. .dot = { .min = 20000, .max = 400000},
  345. .vco = { .min = 1700000, .max = 3500000 },
  346. /* Pineview's Ncounter is a ring counter */
  347. .n = { .min = 3, .max = 6 },
  348. .m = { .min = 2, .max = 256 },
  349. /* Pineview only has one combined m divider, which we treat as m2. */
  350. .m1 = { .min = 0, .max = 0 },
  351. .m2 = { .min = 0, .max = 254 },
  352. .p = { .min = 5, .max = 80 },
  353. .p1 = { .min = 1, .max = 8 },
  354. .p2 = { .dot_limit = 200000,
  355. .p2_slow = 10, .p2_fast = 5 },
  356. };
  357. static const struct intel_limit intel_limits_pineview_lvds = {
  358. .dot = { .min = 20000, .max = 400000 },
  359. .vco = { .min = 1700000, .max = 3500000 },
  360. .n = { .min = 3, .max = 6 },
  361. .m = { .min = 2, .max = 256 },
  362. .m1 = { .min = 0, .max = 0 },
  363. .m2 = { .min = 0, .max = 254 },
  364. .p = { .min = 7, .max = 112 },
  365. .p1 = { .min = 1, .max = 8 },
  366. .p2 = { .dot_limit = 112000,
  367. .p2_slow = 14, .p2_fast = 14 },
  368. };
  369. /* Ironlake / Sandybridge
  370. *
  371. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  372. * the range value for them is (actual_value - 2).
  373. */
  374. static const struct intel_limit intel_limits_ironlake_dac = {
  375. .dot = { .min = 25000, .max = 350000 },
  376. .vco = { .min = 1760000, .max = 3510000 },
  377. .n = { .min = 1, .max = 5 },
  378. .m = { .min = 79, .max = 127 },
  379. .m1 = { .min = 12, .max = 22 },
  380. .m2 = { .min = 5, .max = 9 },
  381. .p = { .min = 5, .max = 80 },
  382. .p1 = { .min = 1, .max = 8 },
  383. .p2 = { .dot_limit = 225000,
  384. .p2_slow = 10, .p2_fast = 5 },
  385. };
  386. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  387. .dot = { .min = 25000, .max = 350000 },
  388. .vco = { .min = 1760000, .max = 3510000 },
  389. .n = { .min = 1, .max = 3 },
  390. .m = { .min = 79, .max = 118 },
  391. .m1 = { .min = 12, .max = 22 },
  392. .m2 = { .min = 5, .max = 9 },
  393. .p = { .min = 28, .max = 112 },
  394. .p1 = { .min = 2, .max = 8 },
  395. .p2 = { .dot_limit = 225000,
  396. .p2_slow = 14, .p2_fast = 14 },
  397. };
  398. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  399. .dot = { .min = 25000, .max = 350000 },
  400. .vco = { .min = 1760000, .max = 3510000 },
  401. .n = { .min = 1, .max = 3 },
  402. .m = { .min = 79, .max = 127 },
  403. .m1 = { .min = 12, .max = 22 },
  404. .m2 = { .min = 5, .max = 9 },
  405. .p = { .min = 14, .max = 56 },
  406. .p1 = { .min = 2, .max = 8 },
  407. .p2 = { .dot_limit = 225000,
  408. .p2_slow = 7, .p2_fast = 7 },
  409. };
  410. /* LVDS 100mhz refclk limits. */
  411. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  412. .dot = { .min = 25000, .max = 350000 },
  413. .vco = { .min = 1760000, .max = 3510000 },
  414. .n = { .min = 1, .max = 2 },
  415. .m = { .min = 79, .max = 126 },
  416. .m1 = { .min = 12, .max = 22 },
  417. .m2 = { .min = 5, .max = 9 },
  418. .p = { .min = 28, .max = 112 },
  419. .p1 = { .min = 2, .max = 8 },
  420. .p2 = { .dot_limit = 225000,
  421. .p2_slow = 14, .p2_fast = 14 },
  422. };
  423. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  424. .dot = { .min = 25000, .max = 350000 },
  425. .vco = { .min = 1760000, .max = 3510000 },
  426. .n = { .min = 1, .max = 3 },
  427. .m = { .min = 79, .max = 126 },
  428. .m1 = { .min = 12, .max = 22 },
  429. .m2 = { .min = 5, .max = 9 },
  430. .p = { .min = 14, .max = 42 },
  431. .p1 = { .min = 2, .max = 6 },
  432. .p2 = { .dot_limit = 225000,
  433. .p2_slow = 7, .p2_fast = 7 },
  434. };
  435. static const struct intel_limit intel_limits_vlv = {
  436. /*
  437. * These are the data rate limits (measured in fast clocks)
  438. * since those are the strictest limits we have. The fast
  439. * clock and actual rate limits are more relaxed, so checking
  440. * them would make no difference.
  441. */
  442. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  443. .vco = { .min = 4000000, .max = 6000000 },
  444. .n = { .min = 1, .max = 7 },
  445. .m1 = { .min = 2, .max = 3 },
  446. .m2 = { .min = 11, .max = 156 },
  447. .p1 = { .min = 2, .max = 3 },
  448. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  449. };
  450. static const struct intel_limit intel_limits_chv = {
  451. /*
  452. * These are the data rate limits (measured in fast clocks)
  453. * since those are the strictest limits we have. The fast
  454. * clock and actual rate limits are more relaxed, so checking
  455. * them would make no difference.
  456. */
  457. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  458. .vco = { .min = 4800000, .max = 6480000 },
  459. .n = { .min = 1, .max = 1 },
  460. .m1 = { .min = 2, .max = 2 },
  461. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  462. .p1 = { .min = 2, .max = 4 },
  463. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  464. };
  465. static const struct intel_limit intel_limits_bxt = {
  466. /* FIXME: find real dot limits */
  467. .dot = { .min = 0, .max = INT_MAX },
  468. .vco = { .min = 4800000, .max = 6700000 },
  469. .n = { .min = 1, .max = 1 },
  470. .m1 = { .min = 2, .max = 2 },
  471. /* FIXME: find real m2 limits */
  472. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  473. .p1 = { .min = 2, .max = 4 },
  474. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  475. };
  476. static bool
  477. needs_modeset(struct drm_crtc_state *state)
  478. {
  479. return drm_atomic_crtc_needs_modeset(state);
  480. }
  481. /*
  482. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  483. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  484. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  485. * The helpers' return value is the rate of the clock that is fed to the
  486. * display engine's pipe which can be the above fast dot clock rate or a
  487. * divided-down version of it.
  488. */
  489. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  490. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  491. {
  492. clock->m = clock->m2 + 2;
  493. clock->p = clock->p1 * clock->p2;
  494. if (WARN_ON(clock->n == 0 || clock->p == 0))
  495. return 0;
  496. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  497. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  498. return clock->dot;
  499. }
  500. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  501. {
  502. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  503. }
  504. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  505. {
  506. clock->m = i9xx_dpll_compute_m(clock);
  507. clock->p = clock->p1 * clock->p2;
  508. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  509. return 0;
  510. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  511. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  512. return clock->dot;
  513. }
  514. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  515. {
  516. clock->m = clock->m1 * clock->m2;
  517. clock->p = clock->p1 * clock->p2;
  518. if (WARN_ON(clock->n == 0 || clock->p == 0))
  519. return 0;
  520. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  521. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  522. return clock->dot / 5;
  523. }
  524. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  525. {
  526. clock->m = clock->m1 * clock->m2;
  527. clock->p = clock->p1 * clock->p2;
  528. if (WARN_ON(clock->n == 0 || clock->p == 0))
  529. return 0;
  530. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  531. clock->n << 22);
  532. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  533. return clock->dot / 5;
  534. }
  535. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  536. /**
  537. * Returns whether the given set of divisors are valid for a given refclk with
  538. * the given connectors.
  539. */
  540. static bool intel_PLL_is_valid(struct drm_device *dev,
  541. const struct intel_limit *limit,
  542. const struct dpll *clock)
  543. {
  544. if (clock->n < limit->n.min || limit->n.max < clock->n)
  545. INTELPllInvalid("n out of range\n");
  546. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  547. INTELPllInvalid("p1 out of range\n");
  548. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  549. INTELPllInvalid("m2 out of range\n");
  550. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  551. INTELPllInvalid("m1 out of range\n");
  552. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  553. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  554. if (clock->m1 <= clock->m2)
  555. INTELPllInvalid("m1 <= m2\n");
  556. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  557. if (clock->p < limit->p.min || limit->p.max < clock->p)
  558. INTELPllInvalid("p out of range\n");
  559. if (clock->m < limit->m.min || limit->m.max < clock->m)
  560. INTELPllInvalid("m out of range\n");
  561. }
  562. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  563. INTELPllInvalid("vco out of range\n");
  564. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  565. * connector, etc., rather than just a single range.
  566. */
  567. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  568. INTELPllInvalid("dot out of range\n");
  569. return true;
  570. }
  571. static int
  572. i9xx_select_p2_div(const struct intel_limit *limit,
  573. const struct intel_crtc_state *crtc_state,
  574. int target)
  575. {
  576. struct drm_device *dev = crtc_state->base.crtc->dev;
  577. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  578. /*
  579. * For LVDS just rely on its current settings for dual-channel.
  580. * We haven't figured out how to reliably set up different
  581. * single/dual channel state, if we even can.
  582. */
  583. if (intel_is_dual_link_lvds(dev))
  584. return limit->p2.p2_fast;
  585. else
  586. return limit->p2.p2_slow;
  587. } else {
  588. if (target < limit->p2.dot_limit)
  589. return limit->p2.p2_slow;
  590. else
  591. return limit->p2.p2_fast;
  592. }
  593. }
  594. /*
  595. * Returns a set of divisors for the desired target clock with the given
  596. * refclk, or FALSE. The returned values represent the clock equation:
  597. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  598. *
  599. * Target and reference clocks are specified in kHz.
  600. *
  601. * If match_clock is provided, then best_clock P divider must match the P
  602. * divider from @match_clock used for LVDS downclocking.
  603. */
  604. static bool
  605. i9xx_find_best_dpll(const struct intel_limit *limit,
  606. struct intel_crtc_state *crtc_state,
  607. int target, int refclk, struct dpll *match_clock,
  608. struct dpll *best_clock)
  609. {
  610. struct drm_device *dev = crtc_state->base.crtc->dev;
  611. struct dpll clock;
  612. int err = target;
  613. memset(best_clock, 0, sizeof(*best_clock));
  614. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  615. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  616. clock.m1++) {
  617. for (clock.m2 = limit->m2.min;
  618. clock.m2 <= limit->m2.max; clock.m2++) {
  619. if (clock.m2 >= clock.m1)
  620. break;
  621. for (clock.n = limit->n.min;
  622. clock.n <= limit->n.max; clock.n++) {
  623. for (clock.p1 = limit->p1.min;
  624. clock.p1 <= limit->p1.max; clock.p1++) {
  625. int this_err;
  626. i9xx_calc_dpll_params(refclk, &clock);
  627. if (!intel_PLL_is_valid(dev, limit,
  628. &clock))
  629. continue;
  630. if (match_clock &&
  631. clock.p != match_clock->p)
  632. continue;
  633. this_err = abs(clock.dot - target);
  634. if (this_err < err) {
  635. *best_clock = clock;
  636. err = this_err;
  637. }
  638. }
  639. }
  640. }
  641. }
  642. return (err != target);
  643. }
  644. /*
  645. * Returns a set of divisors for the desired target clock with the given
  646. * refclk, or FALSE. The returned values represent the clock equation:
  647. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  648. *
  649. * Target and reference clocks are specified in kHz.
  650. *
  651. * If match_clock is provided, then best_clock P divider must match the P
  652. * divider from @match_clock used for LVDS downclocking.
  653. */
  654. static bool
  655. pnv_find_best_dpll(const struct intel_limit *limit,
  656. struct intel_crtc_state *crtc_state,
  657. int target, int refclk, struct dpll *match_clock,
  658. struct dpll *best_clock)
  659. {
  660. struct drm_device *dev = crtc_state->base.crtc->dev;
  661. struct dpll clock;
  662. int err = target;
  663. memset(best_clock, 0, sizeof(*best_clock));
  664. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  665. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  666. clock.m1++) {
  667. for (clock.m2 = limit->m2.min;
  668. clock.m2 <= limit->m2.max; clock.m2++) {
  669. for (clock.n = limit->n.min;
  670. clock.n <= limit->n.max; clock.n++) {
  671. for (clock.p1 = limit->p1.min;
  672. clock.p1 <= limit->p1.max; clock.p1++) {
  673. int this_err;
  674. pnv_calc_dpll_params(refclk, &clock);
  675. if (!intel_PLL_is_valid(dev, limit,
  676. &clock))
  677. continue;
  678. if (match_clock &&
  679. clock.p != match_clock->p)
  680. continue;
  681. this_err = abs(clock.dot - target);
  682. if (this_err < err) {
  683. *best_clock = clock;
  684. err = this_err;
  685. }
  686. }
  687. }
  688. }
  689. }
  690. return (err != target);
  691. }
  692. /*
  693. * Returns a set of divisors for the desired target clock with the given
  694. * refclk, or FALSE. The returned values represent the clock equation:
  695. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  696. *
  697. * Target and reference clocks are specified in kHz.
  698. *
  699. * If match_clock is provided, then best_clock P divider must match the P
  700. * divider from @match_clock used for LVDS downclocking.
  701. */
  702. static bool
  703. g4x_find_best_dpll(const struct intel_limit *limit,
  704. struct intel_crtc_state *crtc_state,
  705. int target, int refclk, struct dpll *match_clock,
  706. struct dpll *best_clock)
  707. {
  708. struct drm_device *dev = crtc_state->base.crtc->dev;
  709. struct dpll clock;
  710. int max_n;
  711. bool found = false;
  712. /* approximately equals target * 0.00585 */
  713. int err_most = (target >> 8) + (target >> 9);
  714. memset(best_clock, 0, sizeof(*best_clock));
  715. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  716. max_n = limit->n.max;
  717. /* based on hardware requirement, prefer smaller n to precision */
  718. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  719. /* based on hardware requirement, prefere larger m1,m2 */
  720. for (clock.m1 = limit->m1.max;
  721. clock.m1 >= limit->m1.min; clock.m1--) {
  722. for (clock.m2 = limit->m2.max;
  723. clock.m2 >= limit->m2.min; clock.m2--) {
  724. for (clock.p1 = limit->p1.max;
  725. clock.p1 >= limit->p1.min; clock.p1--) {
  726. int this_err;
  727. i9xx_calc_dpll_params(refclk, &clock);
  728. if (!intel_PLL_is_valid(dev, limit,
  729. &clock))
  730. continue;
  731. this_err = abs(clock.dot - target);
  732. if (this_err < err_most) {
  733. *best_clock = clock;
  734. err_most = this_err;
  735. max_n = clock.n;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. /*
  745. * Check if the calculated PLL configuration is more optimal compared to the
  746. * best configuration and error found so far. Return the calculated error.
  747. */
  748. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  749. const struct dpll *calculated_clock,
  750. const struct dpll *best_clock,
  751. unsigned int best_error_ppm,
  752. unsigned int *error_ppm)
  753. {
  754. /*
  755. * For CHV ignore the error and consider only the P value.
  756. * Prefer a bigger P value based on HW requirements.
  757. */
  758. if (IS_CHERRYVIEW(dev)) {
  759. *error_ppm = 0;
  760. return calculated_clock->p > best_clock->p;
  761. }
  762. if (WARN_ON_ONCE(!target_freq))
  763. return false;
  764. *error_ppm = div_u64(1000000ULL *
  765. abs(target_freq - calculated_clock->dot),
  766. target_freq);
  767. /*
  768. * Prefer a better P value over a better (smaller) error if the error
  769. * is small. Ensure this preference for future configurations too by
  770. * setting the error to 0.
  771. */
  772. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  773. *error_ppm = 0;
  774. return true;
  775. }
  776. return *error_ppm + 10 < best_error_ppm;
  777. }
  778. /*
  779. * Returns a set of divisors for the desired target clock with the given
  780. * refclk, or FALSE. The returned values represent the clock equation:
  781. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  782. */
  783. static bool
  784. vlv_find_best_dpll(const struct intel_limit *limit,
  785. struct intel_crtc_state *crtc_state,
  786. int target, int refclk, struct dpll *match_clock,
  787. struct dpll *best_clock)
  788. {
  789. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  790. struct drm_device *dev = crtc->base.dev;
  791. struct dpll clock;
  792. unsigned int bestppm = 1000000;
  793. /* min update 19.2 MHz */
  794. int max_n = min(limit->n.max, refclk / 19200);
  795. bool found = false;
  796. target *= 5; /* fast clock */
  797. memset(best_clock, 0, sizeof(*best_clock));
  798. /* based on hardware requirement, prefer smaller n to precision */
  799. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  800. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  801. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  802. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  803. clock.p = clock.p1 * clock.p2;
  804. /* based on hardware requirement, prefer bigger m1,m2 values */
  805. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  806. unsigned int ppm;
  807. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  808. refclk * clock.m1);
  809. vlv_calc_dpll_params(refclk, &clock);
  810. if (!intel_PLL_is_valid(dev, limit,
  811. &clock))
  812. continue;
  813. if (!vlv_PLL_is_optimal(dev, target,
  814. &clock,
  815. best_clock,
  816. bestppm, &ppm))
  817. continue;
  818. *best_clock = clock;
  819. bestppm = ppm;
  820. found = true;
  821. }
  822. }
  823. }
  824. }
  825. return found;
  826. }
  827. /*
  828. * Returns a set of divisors for the desired target clock with the given
  829. * refclk, or FALSE. The returned values represent the clock equation:
  830. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  831. */
  832. static bool
  833. chv_find_best_dpll(const struct intel_limit *limit,
  834. struct intel_crtc_state *crtc_state,
  835. int target, int refclk, struct dpll *match_clock,
  836. struct dpll *best_clock)
  837. {
  838. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  839. struct drm_device *dev = crtc->base.dev;
  840. unsigned int best_error_ppm;
  841. struct dpll clock;
  842. uint64_t m2;
  843. int found = false;
  844. memset(best_clock, 0, sizeof(*best_clock));
  845. best_error_ppm = 1000000;
  846. /*
  847. * Based on hardware doc, the n always set to 1, and m1 always
  848. * set to 2. If requires to support 200Mhz refclk, we need to
  849. * revisit this because n may not 1 anymore.
  850. */
  851. clock.n = 1, clock.m1 = 2;
  852. target *= 5; /* fast clock */
  853. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  854. for (clock.p2 = limit->p2.p2_fast;
  855. clock.p2 >= limit->p2.p2_slow;
  856. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  857. unsigned int error_ppm;
  858. clock.p = clock.p1 * clock.p2;
  859. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  860. clock.n) << 22, refclk * clock.m1);
  861. if (m2 > INT_MAX/clock.m1)
  862. continue;
  863. clock.m2 = m2;
  864. chv_calc_dpll_params(refclk, &clock);
  865. if (!intel_PLL_is_valid(dev, limit, &clock))
  866. continue;
  867. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  868. best_error_ppm, &error_ppm))
  869. continue;
  870. *best_clock = clock;
  871. best_error_ppm = error_ppm;
  872. found = true;
  873. }
  874. }
  875. return found;
  876. }
  877. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  878. struct dpll *best_clock)
  879. {
  880. int refclk = 100000;
  881. const struct intel_limit *limit = &intel_limits_bxt;
  882. return chv_find_best_dpll(limit, crtc_state,
  883. target_clock, refclk, NULL, best_clock);
  884. }
  885. bool intel_crtc_active(struct drm_crtc *crtc)
  886. {
  887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  888. /* Be paranoid as we can arrive here with only partial
  889. * state retrieved from the hardware during setup.
  890. *
  891. * We can ditch the adjusted_mode.crtc_clock check as soon
  892. * as Haswell has gained clock readout/fastboot support.
  893. *
  894. * We can ditch the crtc->primary->fb check as soon as we can
  895. * properly reconstruct framebuffers.
  896. *
  897. * FIXME: The intel_crtc->active here should be switched to
  898. * crtc->state->active once we have proper CRTC states wired up
  899. * for atomic.
  900. */
  901. return intel_crtc->active && crtc->primary->state->fb &&
  902. intel_crtc->config->base.adjusted_mode.crtc_clock;
  903. }
  904. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  905. enum pipe pipe)
  906. {
  907. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  909. return intel_crtc->config->cpu_transcoder;
  910. }
  911. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  912. {
  913. struct drm_i915_private *dev_priv = to_i915(dev);
  914. i915_reg_t reg = PIPEDSL(pipe);
  915. u32 line1, line2;
  916. u32 line_mask;
  917. if (IS_GEN2(dev))
  918. line_mask = DSL_LINEMASK_GEN2;
  919. else
  920. line_mask = DSL_LINEMASK_GEN3;
  921. line1 = I915_READ(reg) & line_mask;
  922. msleep(5);
  923. line2 = I915_READ(reg) & line_mask;
  924. return line1 == line2;
  925. }
  926. /*
  927. * intel_wait_for_pipe_off - wait for pipe to turn off
  928. * @crtc: crtc whose pipe to wait for
  929. *
  930. * After disabling a pipe, we can't wait for vblank in the usual way,
  931. * spinning on the vblank interrupt status bit, since we won't actually
  932. * see an interrupt when the pipe is disabled.
  933. *
  934. * On Gen4 and above:
  935. * wait for the pipe register state bit to turn off
  936. *
  937. * Otherwise:
  938. * wait for the display line value to settle (it usually
  939. * ends up stopping at the start of the next frame).
  940. *
  941. */
  942. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  943. {
  944. struct drm_device *dev = crtc->base.dev;
  945. struct drm_i915_private *dev_priv = to_i915(dev);
  946. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  947. enum pipe pipe = crtc->pipe;
  948. if (INTEL_INFO(dev)->gen >= 4) {
  949. i915_reg_t reg = PIPECONF(cpu_transcoder);
  950. /* Wait for the Pipe State to go off */
  951. if (intel_wait_for_register(dev_priv,
  952. reg, I965_PIPECONF_ACTIVE, 0,
  953. 100))
  954. WARN(1, "pipe_off wait timed out\n");
  955. } else {
  956. /* Wait for the display line to settle */
  957. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  958. WARN(1, "pipe_off wait timed out\n");
  959. }
  960. }
  961. /* Only for pre-ILK configs */
  962. void assert_pll(struct drm_i915_private *dev_priv,
  963. enum pipe pipe, bool state)
  964. {
  965. u32 val;
  966. bool cur_state;
  967. val = I915_READ(DPLL(pipe));
  968. cur_state = !!(val & DPLL_VCO_ENABLE);
  969. I915_STATE_WARN(cur_state != state,
  970. "PLL state assertion failure (expected %s, current %s)\n",
  971. onoff(state), onoff(cur_state));
  972. }
  973. /* XXX: the dsi pll is shared between MIPI DSI ports */
  974. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  975. {
  976. u32 val;
  977. bool cur_state;
  978. mutex_lock(&dev_priv->sb_lock);
  979. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  980. mutex_unlock(&dev_priv->sb_lock);
  981. cur_state = val & DSI_PLL_VCO_EN;
  982. I915_STATE_WARN(cur_state != state,
  983. "DSI PLL state assertion failure (expected %s, current %s)\n",
  984. onoff(state), onoff(cur_state));
  985. }
  986. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  987. enum pipe pipe, bool state)
  988. {
  989. bool cur_state;
  990. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  991. pipe);
  992. if (HAS_DDI(dev_priv)) {
  993. /* DDI does not have a specific FDI_TX register */
  994. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  995. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  996. } else {
  997. u32 val = I915_READ(FDI_TX_CTL(pipe));
  998. cur_state = !!(val & FDI_TX_ENABLE);
  999. }
  1000. I915_STATE_WARN(cur_state != state,
  1001. "FDI TX state assertion failure (expected %s, current %s)\n",
  1002. onoff(state), onoff(cur_state));
  1003. }
  1004. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1005. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1006. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1007. enum pipe pipe, bool state)
  1008. {
  1009. u32 val;
  1010. bool cur_state;
  1011. val = I915_READ(FDI_RX_CTL(pipe));
  1012. cur_state = !!(val & FDI_RX_ENABLE);
  1013. I915_STATE_WARN(cur_state != state,
  1014. "FDI RX state assertion failure (expected %s, current %s)\n",
  1015. onoff(state), onoff(cur_state));
  1016. }
  1017. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1018. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1019. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1020. enum pipe pipe)
  1021. {
  1022. u32 val;
  1023. /* ILK FDI PLL is always enabled */
  1024. if (IS_GEN5(dev_priv))
  1025. return;
  1026. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1027. if (HAS_DDI(dev_priv))
  1028. return;
  1029. val = I915_READ(FDI_TX_CTL(pipe));
  1030. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1031. }
  1032. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1033. enum pipe pipe, bool state)
  1034. {
  1035. u32 val;
  1036. bool cur_state;
  1037. val = I915_READ(FDI_RX_CTL(pipe));
  1038. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1039. I915_STATE_WARN(cur_state != state,
  1040. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1041. onoff(state), onoff(cur_state));
  1042. }
  1043. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1044. enum pipe pipe)
  1045. {
  1046. struct drm_device *dev = &dev_priv->drm;
  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)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev)) {
  1054. u32 port_sel;
  1055. pp_reg = PCH_PP_CONTROL;
  1056. port_sel = I915_READ(PCH_PP_ON_DELAYS) & 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) || IS_CHERRYVIEW(dev)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL;
  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. struct drm_device *dev = &dev_priv->drm;
  1082. bool cur_state;
  1083. if (IS_845G(dev) || IS_I865G(dev))
  1084. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1085. else
  1086. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1087. I915_STATE_WARN(cur_state != state,
  1088. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1089. pipe_name(pipe), onoff(state), onoff(cur_state));
  1090. }
  1091. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1092. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1093. void assert_pipe(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe, bool state)
  1095. {
  1096. bool cur_state;
  1097. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1098. pipe);
  1099. enum intel_display_power_domain power_domain;
  1100. /* if we need the pipe quirk it must be always on */
  1101. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1102. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1103. state = true;
  1104. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1105. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1106. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1107. cur_state = !!(val & PIPECONF_ENABLE);
  1108. intel_display_power_put(dev_priv, power_domain);
  1109. } else {
  1110. cur_state = false;
  1111. }
  1112. I915_STATE_WARN(cur_state != state,
  1113. "pipe %c assertion failure (expected %s, current %s)\n",
  1114. pipe_name(pipe), onoff(state), onoff(cur_state));
  1115. }
  1116. static void assert_plane(struct drm_i915_private *dev_priv,
  1117. enum plane plane, bool state)
  1118. {
  1119. u32 val;
  1120. bool cur_state;
  1121. val = I915_READ(DSPCNTR(plane));
  1122. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1123. I915_STATE_WARN(cur_state != state,
  1124. "plane %c assertion failure (expected %s, current %s)\n",
  1125. plane_name(plane), onoff(state), onoff(cur_state));
  1126. }
  1127. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1128. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1129. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe)
  1131. {
  1132. struct drm_device *dev = &dev_priv->drm;
  1133. int i;
  1134. /* Primary planes are fixed to pipes on gen4+ */
  1135. if (INTEL_INFO(dev)->gen >= 4) {
  1136. u32 val = I915_READ(DSPCNTR(pipe));
  1137. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1138. "plane %c assertion failure, should be disabled but not\n",
  1139. plane_name(pipe));
  1140. return;
  1141. }
  1142. /* Need to check both planes against the pipe */
  1143. for_each_pipe(dev_priv, i) {
  1144. u32 val = I915_READ(DSPCNTR(i));
  1145. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1146. DISPPLANE_SEL_PIPE_SHIFT;
  1147. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1148. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1149. plane_name(i), pipe_name(pipe));
  1150. }
  1151. }
  1152. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1153. enum pipe pipe)
  1154. {
  1155. struct drm_device *dev = &dev_priv->drm;
  1156. int sprite;
  1157. if (INTEL_INFO(dev)->gen >= 9) {
  1158. for_each_sprite(dev_priv, pipe, sprite) {
  1159. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1160. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1161. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1162. sprite, pipe_name(pipe));
  1163. }
  1164. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1165. for_each_sprite(dev_priv, pipe, sprite) {
  1166. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1167. I915_STATE_WARN(val & SP_ENABLE,
  1168. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1169. sprite_name(pipe, sprite), pipe_name(pipe));
  1170. }
  1171. } else if (INTEL_INFO(dev)->gen >= 7) {
  1172. u32 val = I915_READ(SPRCTL(pipe));
  1173. I915_STATE_WARN(val & SPRITE_ENABLE,
  1174. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1175. plane_name(pipe), pipe_name(pipe));
  1176. } else if (INTEL_INFO(dev)->gen >= 5) {
  1177. u32 val = I915_READ(DVSCNTR(pipe));
  1178. I915_STATE_WARN(val & DVS_ENABLE,
  1179. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1180. plane_name(pipe), pipe_name(pipe));
  1181. }
  1182. }
  1183. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1184. {
  1185. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1186. drm_crtc_vblank_put(crtc);
  1187. }
  1188. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1189. enum pipe pipe)
  1190. {
  1191. u32 val;
  1192. bool enabled;
  1193. val = I915_READ(PCH_TRANSCONF(pipe));
  1194. enabled = !!(val & TRANS_ENABLE);
  1195. I915_STATE_WARN(enabled,
  1196. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1197. pipe_name(pipe));
  1198. }
  1199. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1200. enum pipe pipe, u32 port_sel, u32 val)
  1201. {
  1202. if ((val & DP_PORT_EN) == 0)
  1203. return false;
  1204. if (HAS_PCH_CPT(dev_priv)) {
  1205. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1206. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1207. return false;
  1208. } else if (IS_CHERRYVIEW(dev_priv)) {
  1209. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1210. return false;
  1211. } else {
  1212. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1213. return false;
  1214. }
  1215. return true;
  1216. }
  1217. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, u32 val)
  1219. {
  1220. if ((val & SDVO_ENABLE) == 0)
  1221. return false;
  1222. if (HAS_PCH_CPT(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1224. return false;
  1225. } else if (IS_CHERRYVIEW(dev_priv)) {
  1226. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1227. return false;
  1228. } else {
  1229. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1230. return false;
  1231. }
  1232. return true;
  1233. }
  1234. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1235. enum pipe pipe, u32 val)
  1236. {
  1237. if ((val & LVDS_PORT_EN) == 0)
  1238. return false;
  1239. if (HAS_PCH_CPT(dev_priv)) {
  1240. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1241. return false;
  1242. } else {
  1243. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1244. return false;
  1245. }
  1246. return true;
  1247. }
  1248. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1249. enum pipe pipe, u32 val)
  1250. {
  1251. if ((val & ADPA_DAC_ENABLE) == 0)
  1252. return false;
  1253. if (HAS_PCH_CPT(dev_priv)) {
  1254. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1255. return false;
  1256. } else {
  1257. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1258. return false;
  1259. }
  1260. return true;
  1261. }
  1262. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1263. enum pipe pipe, i915_reg_t reg,
  1264. u32 port_sel)
  1265. {
  1266. u32 val = I915_READ(reg);
  1267. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1268. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1269. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1270. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1271. && (val & DP_PIPEB_SELECT),
  1272. "IBX PCH dp port still using transcoder B\n");
  1273. }
  1274. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1275. enum pipe pipe, i915_reg_t reg)
  1276. {
  1277. u32 val = I915_READ(reg);
  1278. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1279. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1280. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1281. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1282. && (val & SDVO_PIPE_B_SELECT),
  1283. "IBX PCH hdmi port still using transcoder B\n");
  1284. }
  1285. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1286. enum pipe pipe)
  1287. {
  1288. u32 val;
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1292. val = I915_READ(PCH_ADPA);
  1293. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1294. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1295. pipe_name(pipe));
  1296. val = I915_READ(PCH_LVDS);
  1297. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1298. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1299. pipe_name(pipe));
  1300. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1303. }
  1304. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1305. const struct intel_crtc_state *pipe_config)
  1306. {
  1307. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1308. enum pipe pipe = crtc->pipe;
  1309. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1310. POSTING_READ(DPLL(pipe));
  1311. udelay(150);
  1312. if (intel_wait_for_register(dev_priv,
  1313. DPLL(pipe),
  1314. DPLL_LOCK_VLV,
  1315. DPLL_LOCK_VLV,
  1316. 1))
  1317. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1318. }
  1319. static void vlv_enable_pll(struct intel_crtc *crtc,
  1320. const struct intel_crtc_state *pipe_config)
  1321. {
  1322. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1323. enum pipe pipe = crtc->pipe;
  1324. assert_pipe_disabled(dev_priv, pipe);
  1325. /* PLL is protected by panel, make sure we can write it */
  1326. assert_panel_unlocked(dev_priv, pipe);
  1327. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1328. _vlv_enable_pll(crtc, pipe_config);
  1329. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1330. POSTING_READ(DPLL_MD(pipe));
  1331. }
  1332. static void _chv_enable_pll(struct intel_crtc *crtc,
  1333. const struct intel_crtc_state *pipe_config)
  1334. {
  1335. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1336. enum pipe pipe = crtc->pipe;
  1337. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1338. u32 tmp;
  1339. mutex_lock(&dev_priv->sb_lock);
  1340. /* Enable back the 10bit clock to display controller */
  1341. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1342. tmp |= DPIO_DCLKP_EN;
  1343. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1344. mutex_unlock(&dev_priv->sb_lock);
  1345. /*
  1346. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1347. */
  1348. udelay(1);
  1349. /* Enable PLL */
  1350. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1351. /* Check PLL is locked */
  1352. if (intel_wait_for_register(dev_priv,
  1353. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1354. 1))
  1355. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1356. }
  1357. static void chv_enable_pll(struct intel_crtc *crtc,
  1358. const struct intel_crtc_state *pipe_config)
  1359. {
  1360. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1361. enum pipe pipe = crtc->pipe;
  1362. assert_pipe_disabled(dev_priv, pipe);
  1363. /* PLL is protected by panel, make sure we can write it */
  1364. assert_panel_unlocked(dev_priv, pipe);
  1365. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1366. _chv_enable_pll(crtc, pipe_config);
  1367. if (pipe != PIPE_A) {
  1368. /*
  1369. * WaPixelRepeatModeFixForC0:chv
  1370. *
  1371. * DPLLCMD is AWOL. Use chicken bits to propagate
  1372. * the value from DPLLBMD to either pipe B or C.
  1373. */
  1374. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1375. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1376. I915_WRITE(CBR4_VLV, 0);
  1377. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1378. /*
  1379. * DPLLB VGA mode also seems to cause problems.
  1380. * We should always have it disabled.
  1381. */
  1382. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1383. } else {
  1384. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1385. POSTING_READ(DPLL_MD(pipe));
  1386. }
  1387. }
  1388. static int intel_num_dvo_pipes(struct drm_device *dev)
  1389. {
  1390. struct intel_crtc *crtc;
  1391. int count = 0;
  1392. for_each_intel_crtc(dev, crtc) {
  1393. count += crtc->base.state->active &&
  1394. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1395. }
  1396. return count;
  1397. }
  1398. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1399. {
  1400. struct drm_device *dev = crtc->base.dev;
  1401. struct drm_i915_private *dev_priv = to_i915(dev);
  1402. i915_reg_t reg = DPLL(crtc->pipe);
  1403. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1404. assert_pipe_disabled(dev_priv, crtc->pipe);
  1405. /* PLL is protected by panel, make sure we can write it */
  1406. if (IS_MOBILE(dev) && !IS_I830(dev))
  1407. assert_panel_unlocked(dev_priv, crtc->pipe);
  1408. /* Enable DVO 2x clock on both PLLs if necessary */
  1409. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1410. /*
  1411. * It appears to be important that we don't enable this
  1412. * for the current pipe before otherwise configuring the
  1413. * PLL. No idea how this should be handled if multiple
  1414. * DVO outputs are enabled simultaneosly.
  1415. */
  1416. dpll |= DPLL_DVO_2X_MODE;
  1417. I915_WRITE(DPLL(!crtc->pipe),
  1418. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1419. }
  1420. /*
  1421. * Apparently we need to have VGA mode enabled prior to changing
  1422. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1423. * dividers, even though the register value does change.
  1424. */
  1425. I915_WRITE(reg, 0);
  1426. I915_WRITE(reg, dpll);
  1427. /* Wait for the clocks to stabilize. */
  1428. POSTING_READ(reg);
  1429. udelay(150);
  1430. if (INTEL_INFO(dev)->gen >= 4) {
  1431. I915_WRITE(DPLL_MD(crtc->pipe),
  1432. crtc->config->dpll_hw_state.dpll_md);
  1433. } else {
  1434. /* The pixel multiplier can only be updated once the
  1435. * DPLL is enabled and the clocks are stable.
  1436. *
  1437. * So write it again.
  1438. */
  1439. I915_WRITE(reg, dpll);
  1440. }
  1441. /* We do this three times for luck */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. I915_WRITE(reg, dpll);
  1449. POSTING_READ(reg);
  1450. udelay(150); /* wait for warmup */
  1451. }
  1452. /**
  1453. * i9xx_disable_pll - disable a PLL
  1454. * @dev_priv: i915 private structure
  1455. * @pipe: pipe PLL to disable
  1456. *
  1457. * Disable the PLL for @pipe, making sure the pipe is off first.
  1458. *
  1459. * Note! This is for pre-ILK only.
  1460. */
  1461. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1462. {
  1463. struct drm_device *dev = crtc->base.dev;
  1464. struct drm_i915_private *dev_priv = to_i915(dev);
  1465. enum pipe pipe = crtc->pipe;
  1466. /* Disable DVO 2x clock on both PLLs if necessary */
  1467. if (IS_I830(dev) &&
  1468. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1469. !intel_num_dvo_pipes(dev)) {
  1470. I915_WRITE(DPLL(PIPE_B),
  1471. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1472. I915_WRITE(DPLL(PIPE_A),
  1473. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1474. }
  1475. /* Don't disable pipe or pipe PLLs if needed */
  1476. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1477. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1478. return;
  1479. /* Make sure the pipe isn't still relying on us */
  1480. assert_pipe_disabled(dev_priv, pipe);
  1481. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1482. POSTING_READ(DPLL(pipe));
  1483. }
  1484. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1485. {
  1486. u32 val;
  1487. /* Make sure the pipe isn't still relying on us */
  1488. assert_pipe_disabled(dev_priv, pipe);
  1489. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1490. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1491. if (pipe != PIPE_A)
  1492. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1493. I915_WRITE(DPLL(pipe), val);
  1494. POSTING_READ(DPLL(pipe));
  1495. }
  1496. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1497. {
  1498. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1499. u32 val;
  1500. /* Make sure the pipe isn't still relying on us */
  1501. assert_pipe_disabled(dev_priv, pipe);
  1502. val = DPLL_SSC_REF_CLK_CHV |
  1503. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1504. if (pipe != PIPE_A)
  1505. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1506. I915_WRITE(DPLL(pipe), val);
  1507. POSTING_READ(DPLL(pipe));
  1508. mutex_lock(&dev_priv->sb_lock);
  1509. /* Disable 10bit clock to display controller */
  1510. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1511. val &= ~DPIO_DCLKP_EN;
  1512. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1513. mutex_unlock(&dev_priv->sb_lock);
  1514. }
  1515. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1516. struct intel_digital_port *dport,
  1517. unsigned int expected_mask)
  1518. {
  1519. u32 port_mask;
  1520. i915_reg_t dpll_reg;
  1521. switch (dport->port) {
  1522. case PORT_B:
  1523. port_mask = DPLL_PORTB_READY_MASK;
  1524. dpll_reg = DPLL(0);
  1525. break;
  1526. case PORT_C:
  1527. port_mask = DPLL_PORTC_READY_MASK;
  1528. dpll_reg = DPLL(0);
  1529. expected_mask <<= 4;
  1530. break;
  1531. case PORT_D:
  1532. port_mask = DPLL_PORTD_READY_MASK;
  1533. dpll_reg = DPIO_PHY_STATUS;
  1534. break;
  1535. default:
  1536. BUG();
  1537. }
  1538. if (intel_wait_for_register(dev_priv,
  1539. dpll_reg, port_mask, expected_mask,
  1540. 1000))
  1541. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1542. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1543. }
  1544. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1545. enum pipe pipe)
  1546. {
  1547. struct drm_device *dev = &dev_priv->drm;
  1548. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1549. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1550. i915_reg_t reg;
  1551. uint32_t val, pipeconf_val;
  1552. /* Make sure PCH DPLL is enabled */
  1553. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1554. /* FDI must be feeding us bits for PCH ports */
  1555. assert_fdi_tx_enabled(dev_priv, pipe);
  1556. assert_fdi_rx_enabled(dev_priv, pipe);
  1557. if (HAS_PCH_CPT(dev)) {
  1558. /* Workaround: Set the timing override bit before enabling the
  1559. * pch transcoder. */
  1560. reg = TRANS_CHICKEN2(pipe);
  1561. val = I915_READ(reg);
  1562. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1563. I915_WRITE(reg, val);
  1564. }
  1565. reg = PCH_TRANSCONF(pipe);
  1566. val = I915_READ(reg);
  1567. pipeconf_val = I915_READ(PIPECONF(pipe));
  1568. if (HAS_PCH_IBX(dev_priv)) {
  1569. /*
  1570. * Make the BPC in transcoder be consistent with
  1571. * that in pipeconf reg. For HDMI we must use 8bpc
  1572. * here for both 8bpc and 12bpc.
  1573. */
  1574. val &= ~PIPECONF_BPC_MASK;
  1575. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1576. val |= PIPECONF_8BPC;
  1577. else
  1578. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1579. }
  1580. val &= ~TRANS_INTERLACE_MASK;
  1581. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1582. if (HAS_PCH_IBX(dev_priv) &&
  1583. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1584. val |= TRANS_LEGACY_INTERLACED_ILK;
  1585. else
  1586. val |= TRANS_INTERLACED;
  1587. else
  1588. val |= TRANS_PROGRESSIVE;
  1589. I915_WRITE(reg, val | TRANS_ENABLE);
  1590. if (intel_wait_for_register(dev_priv,
  1591. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1592. 100))
  1593. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1594. }
  1595. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1596. enum transcoder cpu_transcoder)
  1597. {
  1598. u32 val, pipeconf_val;
  1599. /* FDI must be feeding us bits for PCH ports */
  1600. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1601. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1602. /* Workaround: set timing override bit. */
  1603. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1604. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1605. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1606. val = TRANS_ENABLE;
  1607. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1608. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1609. PIPECONF_INTERLACED_ILK)
  1610. val |= TRANS_INTERLACED;
  1611. else
  1612. val |= TRANS_PROGRESSIVE;
  1613. I915_WRITE(LPT_TRANSCONF, val);
  1614. if (intel_wait_for_register(dev_priv,
  1615. LPT_TRANSCONF,
  1616. TRANS_STATE_ENABLE,
  1617. TRANS_STATE_ENABLE,
  1618. 100))
  1619. DRM_ERROR("Failed to enable PCH transcoder\n");
  1620. }
  1621. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1622. enum pipe pipe)
  1623. {
  1624. struct drm_device *dev = &dev_priv->drm;
  1625. i915_reg_t reg;
  1626. uint32_t val;
  1627. /* FDI relies on the transcoder */
  1628. assert_fdi_tx_disabled(dev_priv, pipe);
  1629. assert_fdi_rx_disabled(dev_priv, pipe);
  1630. /* Ports must be off as well */
  1631. assert_pch_ports_disabled(dev_priv, pipe);
  1632. reg = PCH_TRANSCONF(pipe);
  1633. val = I915_READ(reg);
  1634. val &= ~TRANS_ENABLE;
  1635. I915_WRITE(reg, val);
  1636. /* wait for PCH transcoder off, transcoder state */
  1637. if (intel_wait_for_register(dev_priv,
  1638. reg, TRANS_STATE_ENABLE, 0,
  1639. 50))
  1640. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1641. if (HAS_PCH_CPT(dev)) {
  1642. /* Workaround: Clear the timing override chicken bit again. */
  1643. reg = TRANS_CHICKEN2(pipe);
  1644. val = I915_READ(reg);
  1645. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1646. I915_WRITE(reg, val);
  1647. }
  1648. }
  1649. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1650. {
  1651. u32 val;
  1652. val = I915_READ(LPT_TRANSCONF);
  1653. val &= ~TRANS_ENABLE;
  1654. I915_WRITE(LPT_TRANSCONF, val);
  1655. /* wait for PCH transcoder off, transcoder state */
  1656. if (intel_wait_for_register(dev_priv,
  1657. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1658. 50))
  1659. DRM_ERROR("Failed to disable PCH transcoder\n");
  1660. /* Workaround: clear timing override bit. */
  1661. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1662. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1663. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1664. }
  1665. /**
  1666. * intel_enable_pipe - enable a pipe, asserting requirements
  1667. * @crtc: crtc responsible for the pipe
  1668. *
  1669. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1670. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1671. */
  1672. static void intel_enable_pipe(struct intel_crtc *crtc)
  1673. {
  1674. struct drm_device *dev = crtc->base.dev;
  1675. struct drm_i915_private *dev_priv = to_i915(dev);
  1676. enum pipe pipe = crtc->pipe;
  1677. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1678. enum pipe pch_transcoder;
  1679. i915_reg_t reg;
  1680. u32 val;
  1681. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1682. assert_planes_disabled(dev_priv, pipe);
  1683. assert_cursor_disabled(dev_priv, pipe);
  1684. assert_sprites_disabled(dev_priv, pipe);
  1685. if (HAS_PCH_LPT(dev_priv))
  1686. pch_transcoder = TRANSCODER_A;
  1687. else
  1688. pch_transcoder = pipe;
  1689. /*
  1690. * A pipe without a PLL won't actually be able to drive bits from
  1691. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1692. * need the check.
  1693. */
  1694. if (HAS_GMCH_DISPLAY(dev_priv))
  1695. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1696. assert_dsi_pll_enabled(dev_priv);
  1697. else
  1698. assert_pll_enabled(dev_priv, pipe);
  1699. else {
  1700. if (crtc->config->has_pch_encoder) {
  1701. /* if driving the PCH, we need FDI enabled */
  1702. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1703. assert_fdi_tx_pll_enabled(dev_priv,
  1704. (enum pipe) cpu_transcoder);
  1705. }
  1706. /* FIXME: assert CPU port conditions for SNB+ */
  1707. }
  1708. reg = PIPECONF(cpu_transcoder);
  1709. val = I915_READ(reg);
  1710. if (val & PIPECONF_ENABLE) {
  1711. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1712. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1713. return;
  1714. }
  1715. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1716. POSTING_READ(reg);
  1717. /*
  1718. * Until the pipe starts DSL will read as 0, which would cause
  1719. * an apparent vblank timestamp jump, which messes up also the
  1720. * frame count when it's derived from the timestamps. So let's
  1721. * wait for the pipe to start properly before we call
  1722. * drm_crtc_vblank_on()
  1723. */
  1724. if (dev->max_vblank_count == 0 &&
  1725. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1726. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1727. }
  1728. /**
  1729. * intel_disable_pipe - disable a pipe, asserting requirements
  1730. * @crtc: crtc whose pipes is to be disabled
  1731. *
  1732. * Disable the pipe of @crtc, making sure that various hardware
  1733. * specific requirements are met, if applicable, e.g. plane
  1734. * disabled, panel fitter off, etc.
  1735. *
  1736. * Will wait until the pipe has shut down before returning.
  1737. */
  1738. static void intel_disable_pipe(struct intel_crtc *crtc)
  1739. {
  1740. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1741. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1742. enum pipe pipe = crtc->pipe;
  1743. i915_reg_t reg;
  1744. u32 val;
  1745. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1746. /*
  1747. * Make sure planes won't keep trying to pump pixels to us,
  1748. * or we might hang the display.
  1749. */
  1750. assert_planes_disabled(dev_priv, pipe);
  1751. assert_cursor_disabled(dev_priv, pipe);
  1752. assert_sprites_disabled(dev_priv, pipe);
  1753. reg = PIPECONF(cpu_transcoder);
  1754. val = I915_READ(reg);
  1755. if ((val & PIPECONF_ENABLE) == 0)
  1756. return;
  1757. /*
  1758. * Double wide has implications for planes
  1759. * so best keep it disabled when not needed.
  1760. */
  1761. if (crtc->config->double_wide)
  1762. val &= ~PIPECONF_DOUBLE_WIDE;
  1763. /* Don't disable pipe or pipe PLLs if needed */
  1764. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1765. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1766. val &= ~PIPECONF_ENABLE;
  1767. I915_WRITE(reg, val);
  1768. if ((val & PIPECONF_ENABLE) == 0)
  1769. intel_wait_for_pipe_off(crtc);
  1770. }
  1771. static bool need_vtd_wa(struct drm_device *dev)
  1772. {
  1773. #ifdef CONFIG_INTEL_IOMMU
  1774. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1775. return true;
  1776. #endif
  1777. return false;
  1778. }
  1779. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1780. {
  1781. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1782. }
  1783. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1784. uint64_t fb_modifier, unsigned int cpp)
  1785. {
  1786. switch (fb_modifier) {
  1787. case DRM_FORMAT_MOD_NONE:
  1788. return cpp;
  1789. case I915_FORMAT_MOD_X_TILED:
  1790. if (IS_GEN2(dev_priv))
  1791. return 128;
  1792. else
  1793. return 512;
  1794. case I915_FORMAT_MOD_Y_TILED:
  1795. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1796. return 128;
  1797. else
  1798. return 512;
  1799. case I915_FORMAT_MOD_Yf_TILED:
  1800. switch (cpp) {
  1801. case 1:
  1802. return 64;
  1803. case 2:
  1804. case 4:
  1805. return 128;
  1806. case 8:
  1807. case 16:
  1808. return 256;
  1809. default:
  1810. MISSING_CASE(cpp);
  1811. return cpp;
  1812. }
  1813. break;
  1814. default:
  1815. MISSING_CASE(fb_modifier);
  1816. return cpp;
  1817. }
  1818. }
  1819. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1820. uint64_t fb_modifier, unsigned int cpp)
  1821. {
  1822. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1823. return 1;
  1824. else
  1825. return intel_tile_size(dev_priv) /
  1826. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1827. }
  1828. /* Return the tile dimensions in pixel units */
  1829. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1830. unsigned int *tile_width,
  1831. unsigned int *tile_height,
  1832. uint64_t fb_modifier,
  1833. unsigned int cpp)
  1834. {
  1835. unsigned int tile_width_bytes =
  1836. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1837. *tile_width = tile_width_bytes / cpp;
  1838. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1839. }
  1840. unsigned int
  1841. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1842. uint32_t pixel_format, uint64_t fb_modifier)
  1843. {
  1844. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1845. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1846. return ALIGN(height, tile_height);
  1847. }
  1848. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1849. {
  1850. unsigned int size = 0;
  1851. int i;
  1852. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1853. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1854. return size;
  1855. }
  1856. static void
  1857. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1858. const struct drm_framebuffer *fb,
  1859. unsigned int rotation)
  1860. {
  1861. if (intel_rotation_90_or_270(rotation)) {
  1862. *view = i915_ggtt_view_rotated;
  1863. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1864. } else {
  1865. *view = i915_ggtt_view_normal;
  1866. }
  1867. }
  1868. static void
  1869. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1870. struct drm_framebuffer *fb)
  1871. {
  1872. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1873. unsigned int tile_size, tile_width, tile_height, cpp;
  1874. tile_size = intel_tile_size(dev_priv);
  1875. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1876. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1877. fb->modifier[0], cpp);
  1878. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1879. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1880. if (info->pixel_format == DRM_FORMAT_NV12) {
  1881. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1882. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1883. fb->modifier[1], cpp);
  1884. info->uv_offset = fb->offsets[1];
  1885. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1886. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1887. }
  1888. }
  1889. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1890. {
  1891. if (INTEL_INFO(dev_priv)->gen >= 9)
  1892. return 256 * 1024;
  1893. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1894. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1895. return 128 * 1024;
  1896. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1897. return 4 * 1024;
  1898. else
  1899. return 0;
  1900. }
  1901. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1902. uint64_t fb_modifier)
  1903. {
  1904. switch (fb_modifier) {
  1905. case DRM_FORMAT_MOD_NONE:
  1906. return intel_linear_alignment(dev_priv);
  1907. case I915_FORMAT_MOD_X_TILED:
  1908. if (INTEL_INFO(dev_priv)->gen >= 9)
  1909. return 256 * 1024;
  1910. return 0;
  1911. case I915_FORMAT_MOD_Y_TILED:
  1912. case I915_FORMAT_MOD_Yf_TILED:
  1913. return 1 * 1024 * 1024;
  1914. default:
  1915. MISSING_CASE(fb_modifier);
  1916. return 0;
  1917. }
  1918. }
  1919. int
  1920. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1921. unsigned int rotation)
  1922. {
  1923. struct drm_device *dev = fb->dev;
  1924. struct drm_i915_private *dev_priv = to_i915(dev);
  1925. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1926. struct i915_ggtt_view view;
  1927. u32 alignment;
  1928. int ret;
  1929. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1930. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1931. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1932. /* Note that the w/a also requires 64 PTE of padding following the
  1933. * bo. We currently fill all unused PTE with the shadow page and so
  1934. * we should always have valid PTE following the scanout preventing
  1935. * the VT-d warning.
  1936. */
  1937. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1938. alignment = 256 * 1024;
  1939. /*
  1940. * Global gtt pte registers are special registers which actually forward
  1941. * writes to a chunk of system memory. Which means that there is no risk
  1942. * that the register values disappear as soon as we call
  1943. * intel_runtime_pm_put(), so it is correct to wrap only the
  1944. * pin/unpin/fence and not more.
  1945. */
  1946. intel_runtime_pm_get(dev_priv);
  1947. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1948. &view);
  1949. if (ret)
  1950. goto err_pm;
  1951. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1952. * fence, whereas 965+ only requires a fence if using
  1953. * framebuffer compression. For simplicity, we always install
  1954. * a fence as the cost is not that onerous.
  1955. */
  1956. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1957. ret = i915_gem_object_get_fence(obj);
  1958. if (ret == -EDEADLK) {
  1959. /*
  1960. * -EDEADLK means there are no free fences
  1961. * no pending flips.
  1962. *
  1963. * This is propagated to atomic, but it uses
  1964. * -EDEADLK to force a locking recovery, so
  1965. * change the returned error to -EBUSY.
  1966. */
  1967. ret = -EBUSY;
  1968. goto err_unpin;
  1969. } else if (ret)
  1970. goto err_unpin;
  1971. i915_gem_object_pin_fence(obj);
  1972. }
  1973. intel_runtime_pm_put(dev_priv);
  1974. return 0;
  1975. err_unpin:
  1976. i915_gem_object_unpin_from_display_plane(obj, &view);
  1977. err_pm:
  1978. intel_runtime_pm_put(dev_priv);
  1979. return ret;
  1980. }
  1981. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1982. {
  1983. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1984. struct i915_ggtt_view view;
  1985. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1986. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1987. if (view.type == I915_GGTT_VIEW_NORMAL)
  1988. i915_gem_object_unpin_fence(obj);
  1989. i915_gem_object_unpin_from_display_plane(obj, &view);
  1990. }
  1991. /*
  1992. * Adjust the tile offset by moving the difference into
  1993. * the x/y offsets.
  1994. *
  1995. * Input tile dimensions and pitch must already be
  1996. * rotated to match x and y, and in pixel units.
  1997. */
  1998. static u32 intel_adjust_tile_offset(int *x, int *y,
  1999. unsigned int tile_width,
  2000. unsigned int tile_height,
  2001. unsigned int tile_size,
  2002. unsigned int pitch_tiles,
  2003. u32 old_offset,
  2004. u32 new_offset)
  2005. {
  2006. unsigned int tiles;
  2007. WARN_ON(old_offset & (tile_size - 1));
  2008. WARN_ON(new_offset & (tile_size - 1));
  2009. WARN_ON(new_offset > old_offset);
  2010. tiles = (old_offset - new_offset) / tile_size;
  2011. *y += tiles / pitch_tiles * tile_height;
  2012. *x += tiles % pitch_tiles * tile_width;
  2013. return new_offset;
  2014. }
  2015. /*
  2016. * Computes the linear offset to the base tile and adjusts
  2017. * x, y. bytes per pixel is assumed to be a power-of-two.
  2018. *
  2019. * In the 90/270 rotated case, x and y are assumed
  2020. * to be already rotated to match the rotated GTT view, and
  2021. * pitch is the tile_height aligned framebuffer height.
  2022. */
  2023. u32 intel_compute_tile_offset(int *x, int *y,
  2024. const struct drm_framebuffer *fb, int plane,
  2025. unsigned int pitch,
  2026. unsigned int rotation)
  2027. {
  2028. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2029. uint64_t fb_modifier = fb->modifier[plane];
  2030. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2031. u32 offset, offset_aligned, alignment;
  2032. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2033. if (alignment)
  2034. alignment--;
  2035. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2036. unsigned int tile_size, tile_width, tile_height;
  2037. unsigned int tile_rows, tiles, pitch_tiles;
  2038. tile_size = intel_tile_size(dev_priv);
  2039. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2040. fb_modifier, cpp);
  2041. if (intel_rotation_90_or_270(rotation)) {
  2042. pitch_tiles = pitch / tile_height;
  2043. swap(tile_width, tile_height);
  2044. } else {
  2045. pitch_tiles = pitch / (tile_width * cpp);
  2046. }
  2047. tile_rows = *y / tile_height;
  2048. *y %= tile_height;
  2049. tiles = *x / tile_width;
  2050. *x %= tile_width;
  2051. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2052. offset_aligned = offset & ~alignment;
  2053. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2054. tile_size, pitch_tiles,
  2055. offset, offset_aligned);
  2056. } else {
  2057. offset = *y * pitch + *x * cpp;
  2058. offset_aligned = offset & ~alignment;
  2059. *y = (offset & alignment) / pitch;
  2060. *x = ((offset & alignment) - *y * pitch) / cpp;
  2061. }
  2062. return offset_aligned;
  2063. }
  2064. static int i9xx_format_to_fourcc(int format)
  2065. {
  2066. switch (format) {
  2067. case DISPPLANE_8BPP:
  2068. return DRM_FORMAT_C8;
  2069. case DISPPLANE_BGRX555:
  2070. return DRM_FORMAT_XRGB1555;
  2071. case DISPPLANE_BGRX565:
  2072. return DRM_FORMAT_RGB565;
  2073. default:
  2074. case DISPPLANE_BGRX888:
  2075. return DRM_FORMAT_XRGB8888;
  2076. case DISPPLANE_RGBX888:
  2077. return DRM_FORMAT_XBGR8888;
  2078. case DISPPLANE_BGRX101010:
  2079. return DRM_FORMAT_XRGB2101010;
  2080. case DISPPLANE_RGBX101010:
  2081. return DRM_FORMAT_XBGR2101010;
  2082. }
  2083. }
  2084. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2085. {
  2086. switch (format) {
  2087. case PLANE_CTL_FORMAT_RGB_565:
  2088. return DRM_FORMAT_RGB565;
  2089. default:
  2090. case PLANE_CTL_FORMAT_XRGB_8888:
  2091. if (rgb_order) {
  2092. if (alpha)
  2093. return DRM_FORMAT_ABGR8888;
  2094. else
  2095. return DRM_FORMAT_XBGR8888;
  2096. } else {
  2097. if (alpha)
  2098. return DRM_FORMAT_ARGB8888;
  2099. else
  2100. return DRM_FORMAT_XRGB8888;
  2101. }
  2102. case PLANE_CTL_FORMAT_XRGB_2101010:
  2103. if (rgb_order)
  2104. return DRM_FORMAT_XBGR2101010;
  2105. else
  2106. return DRM_FORMAT_XRGB2101010;
  2107. }
  2108. }
  2109. static bool
  2110. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2111. struct intel_initial_plane_config *plane_config)
  2112. {
  2113. struct drm_device *dev = crtc->base.dev;
  2114. struct drm_i915_private *dev_priv = to_i915(dev);
  2115. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2116. struct drm_i915_gem_object *obj = NULL;
  2117. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2118. struct drm_framebuffer *fb = &plane_config->fb->base;
  2119. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2120. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2121. PAGE_SIZE);
  2122. size_aligned -= base_aligned;
  2123. if (plane_config->size == 0)
  2124. return false;
  2125. /* If the FB is too big, just don't use it since fbdev is not very
  2126. * important and we should probably use that space with FBC or other
  2127. * features. */
  2128. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2129. return false;
  2130. mutex_lock(&dev->struct_mutex);
  2131. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2132. base_aligned,
  2133. base_aligned,
  2134. size_aligned);
  2135. if (!obj) {
  2136. mutex_unlock(&dev->struct_mutex);
  2137. return false;
  2138. }
  2139. obj->tiling_mode = plane_config->tiling;
  2140. if (obj->tiling_mode == I915_TILING_X)
  2141. obj->stride = fb->pitches[0];
  2142. mode_cmd.pixel_format = fb->pixel_format;
  2143. mode_cmd.width = fb->width;
  2144. mode_cmd.height = fb->height;
  2145. mode_cmd.pitches[0] = fb->pitches[0];
  2146. mode_cmd.modifier[0] = fb->modifier[0];
  2147. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2148. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2149. &mode_cmd, obj)) {
  2150. DRM_DEBUG_KMS("intel fb init failed\n");
  2151. goto out_unref_obj;
  2152. }
  2153. mutex_unlock(&dev->struct_mutex);
  2154. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2155. return true;
  2156. out_unref_obj:
  2157. drm_gem_object_unreference(&obj->base);
  2158. mutex_unlock(&dev->struct_mutex);
  2159. return false;
  2160. }
  2161. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2162. static void
  2163. update_state_fb(struct drm_plane *plane)
  2164. {
  2165. if (plane->fb == plane->state->fb)
  2166. return;
  2167. if (plane->state->fb)
  2168. drm_framebuffer_unreference(plane->state->fb);
  2169. plane->state->fb = plane->fb;
  2170. if (plane->state->fb)
  2171. drm_framebuffer_reference(plane->state->fb);
  2172. }
  2173. static void
  2174. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2175. struct intel_initial_plane_config *plane_config)
  2176. {
  2177. struct drm_device *dev = intel_crtc->base.dev;
  2178. struct drm_i915_private *dev_priv = to_i915(dev);
  2179. struct drm_crtc *c;
  2180. struct intel_crtc *i;
  2181. struct drm_i915_gem_object *obj;
  2182. struct drm_plane *primary = intel_crtc->base.primary;
  2183. struct drm_plane_state *plane_state = primary->state;
  2184. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2185. struct intel_plane *intel_plane = to_intel_plane(primary);
  2186. struct intel_plane_state *intel_state =
  2187. to_intel_plane_state(plane_state);
  2188. struct drm_framebuffer *fb;
  2189. if (!plane_config->fb)
  2190. return;
  2191. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2192. fb = &plane_config->fb->base;
  2193. goto valid_fb;
  2194. }
  2195. kfree(plane_config->fb);
  2196. /*
  2197. * Failed to alloc the obj, check to see if we should share
  2198. * an fb with another CRTC instead
  2199. */
  2200. for_each_crtc(dev, c) {
  2201. i = to_intel_crtc(c);
  2202. if (c == &intel_crtc->base)
  2203. continue;
  2204. if (!i->active)
  2205. continue;
  2206. fb = c->primary->fb;
  2207. if (!fb)
  2208. continue;
  2209. obj = intel_fb_obj(fb);
  2210. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2211. drm_framebuffer_reference(fb);
  2212. goto valid_fb;
  2213. }
  2214. }
  2215. /*
  2216. * We've failed to reconstruct the BIOS FB. Current display state
  2217. * indicates that the primary plane is visible, but has a NULL FB,
  2218. * which will lead to problems later if we don't fix it up. The
  2219. * simplest solution is to just disable the primary plane now and
  2220. * pretend the BIOS never had it enabled.
  2221. */
  2222. to_intel_plane_state(plane_state)->visible = false;
  2223. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2224. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2225. intel_plane->disable_plane(primary, &intel_crtc->base);
  2226. return;
  2227. valid_fb:
  2228. plane_state->src_x = 0;
  2229. plane_state->src_y = 0;
  2230. plane_state->src_w = fb->width << 16;
  2231. plane_state->src_h = fb->height << 16;
  2232. plane_state->crtc_x = 0;
  2233. plane_state->crtc_y = 0;
  2234. plane_state->crtc_w = fb->width;
  2235. plane_state->crtc_h = fb->height;
  2236. intel_state->src.x1 = plane_state->src_x;
  2237. intel_state->src.y1 = plane_state->src_y;
  2238. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2239. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2240. intel_state->dst.x1 = plane_state->crtc_x;
  2241. intel_state->dst.y1 = plane_state->crtc_y;
  2242. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2243. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2244. obj = intel_fb_obj(fb);
  2245. if (obj->tiling_mode != I915_TILING_NONE)
  2246. dev_priv->preserve_bios_swizzle = true;
  2247. drm_framebuffer_reference(fb);
  2248. primary->fb = primary->state->fb = fb;
  2249. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2250. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2251. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2252. }
  2253. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2254. const struct intel_crtc_state *crtc_state,
  2255. const struct intel_plane_state *plane_state)
  2256. {
  2257. struct drm_device *dev = primary->dev;
  2258. struct drm_i915_private *dev_priv = to_i915(dev);
  2259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2260. struct drm_framebuffer *fb = plane_state->base.fb;
  2261. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2262. int plane = intel_crtc->plane;
  2263. u32 linear_offset;
  2264. u32 dspcntr;
  2265. i915_reg_t reg = DSPCNTR(plane);
  2266. unsigned int rotation = plane_state->base.rotation;
  2267. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2268. int x = plane_state->src.x1 >> 16;
  2269. int y = plane_state->src.y1 >> 16;
  2270. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2271. dspcntr |= DISPLAY_PLANE_ENABLE;
  2272. if (INTEL_INFO(dev)->gen < 4) {
  2273. if (intel_crtc->pipe == PIPE_B)
  2274. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2275. /* pipesrc and dspsize control the size that is scaled from,
  2276. * which should always be the user's requested size.
  2277. */
  2278. I915_WRITE(DSPSIZE(plane),
  2279. ((crtc_state->pipe_src_h - 1) << 16) |
  2280. (crtc_state->pipe_src_w - 1));
  2281. I915_WRITE(DSPPOS(plane), 0);
  2282. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2283. I915_WRITE(PRIMSIZE(plane),
  2284. ((crtc_state->pipe_src_h - 1) << 16) |
  2285. (crtc_state->pipe_src_w - 1));
  2286. I915_WRITE(PRIMPOS(plane), 0);
  2287. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2288. }
  2289. switch (fb->pixel_format) {
  2290. case DRM_FORMAT_C8:
  2291. dspcntr |= DISPPLANE_8BPP;
  2292. break;
  2293. case DRM_FORMAT_XRGB1555:
  2294. dspcntr |= DISPPLANE_BGRX555;
  2295. break;
  2296. case DRM_FORMAT_RGB565:
  2297. dspcntr |= DISPPLANE_BGRX565;
  2298. break;
  2299. case DRM_FORMAT_XRGB8888:
  2300. dspcntr |= DISPPLANE_BGRX888;
  2301. break;
  2302. case DRM_FORMAT_XBGR8888:
  2303. dspcntr |= DISPPLANE_RGBX888;
  2304. break;
  2305. case DRM_FORMAT_XRGB2101010:
  2306. dspcntr |= DISPPLANE_BGRX101010;
  2307. break;
  2308. case DRM_FORMAT_XBGR2101010:
  2309. dspcntr |= DISPPLANE_RGBX101010;
  2310. break;
  2311. default:
  2312. BUG();
  2313. }
  2314. if (INTEL_INFO(dev)->gen >= 4 &&
  2315. obj->tiling_mode != I915_TILING_NONE)
  2316. dspcntr |= DISPPLANE_TILED;
  2317. if (IS_G4X(dev))
  2318. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2319. linear_offset = y * fb->pitches[0] + x * cpp;
  2320. if (INTEL_INFO(dev)->gen >= 4) {
  2321. intel_crtc->dspaddr_offset =
  2322. intel_compute_tile_offset(&x, &y, fb, 0,
  2323. fb->pitches[0], rotation);
  2324. linear_offset -= intel_crtc->dspaddr_offset;
  2325. } else {
  2326. intel_crtc->dspaddr_offset = linear_offset;
  2327. }
  2328. if (rotation == BIT(DRM_ROTATE_180)) {
  2329. dspcntr |= DISPPLANE_ROTATE_180;
  2330. x += (crtc_state->pipe_src_w - 1);
  2331. y += (crtc_state->pipe_src_h - 1);
  2332. /* Finding the last pixel of the last line of the display
  2333. data and adding to linear_offset*/
  2334. linear_offset +=
  2335. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2336. (crtc_state->pipe_src_w - 1) * cpp;
  2337. }
  2338. intel_crtc->adjusted_x = x;
  2339. intel_crtc->adjusted_y = y;
  2340. I915_WRITE(reg, dspcntr);
  2341. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2342. if (INTEL_INFO(dev)->gen >= 4) {
  2343. I915_WRITE(DSPSURF(plane),
  2344. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2345. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2346. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2347. } else
  2348. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2349. POSTING_READ(reg);
  2350. }
  2351. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2352. struct drm_crtc *crtc)
  2353. {
  2354. struct drm_device *dev = crtc->dev;
  2355. struct drm_i915_private *dev_priv = to_i915(dev);
  2356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2357. int plane = intel_crtc->plane;
  2358. I915_WRITE(DSPCNTR(plane), 0);
  2359. if (INTEL_INFO(dev_priv)->gen >= 4)
  2360. I915_WRITE(DSPSURF(plane), 0);
  2361. else
  2362. I915_WRITE(DSPADDR(plane), 0);
  2363. POSTING_READ(DSPCNTR(plane));
  2364. }
  2365. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2366. const struct intel_crtc_state *crtc_state,
  2367. const struct intel_plane_state *plane_state)
  2368. {
  2369. struct drm_device *dev = primary->dev;
  2370. struct drm_i915_private *dev_priv = to_i915(dev);
  2371. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2372. struct drm_framebuffer *fb = plane_state->base.fb;
  2373. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2374. int plane = intel_crtc->plane;
  2375. u32 linear_offset;
  2376. u32 dspcntr;
  2377. i915_reg_t reg = DSPCNTR(plane);
  2378. unsigned int rotation = plane_state->base.rotation;
  2379. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2380. int x = plane_state->src.x1 >> 16;
  2381. int y = plane_state->src.y1 >> 16;
  2382. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2383. dspcntr |= DISPLAY_PLANE_ENABLE;
  2384. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2385. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2386. switch (fb->pixel_format) {
  2387. case DRM_FORMAT_C8:
  2388. dspcntr |= DISPPLANE_8BPP;
  2389. break;
  2390. case DRM_FORMAT_RGB565:
  2391. dspcntr |= DISPPLANE_BGRX565;
  2392. break;
  2393. case DRM_FORMAT_XRGB8888:
  2394. dspcntr |= DISPPLANE_BGRX888;
  2395. break;
  2396. case DRM_FORMAT_XBGR8888:
  2397. dspcntr |= DISPPLANE_RGBX888;
  2398. break;
  2399. case DRM_FORMAT_XRGB2101010:
  2400. dspcntr |= DISPPLANE_BGRX101010;
  2401. break;
  2402. case DRM_FORMAT_XBGR2101010:
  2403. dspcntr |= DISPPLANE_RGBX101010;
  2404. break;
  2405. default:
  2406. BUG();
  2407. }
  2408. if (obj->tiling_mode != I915_TILING_NONE)
  2409. dspcntr |= DISPPLANE_TILED;
  2410. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2411. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2412. linear_offset = y * fb->pitches[0] + x * cpp;
  2413. intel_crtc->dspaddr_offset =
  2414. intel_compute_tile_offset(&x, &y, fb, 0,
  2415. fb->pitches[0], rotation);
  2416. linear_offset -= intel_crtc->dspaddr_offset;
  2417. if (rotation == BIT(DRM_ROTATE_180)) {
  2418. dspcntr |= DISPPLANE_ROTATE_180;
  2419. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2420. x += (crtc_state->pipe_src_w - 1);
  2421. y += (crtc_state->pipe_src_h - 1);
  2422. /* Finding the last pixel of the last line of the display
  2423. data and adding to linear_offset*/
  2424. linear_offset +=
  2425. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2426. (crtc_state->pipe_src_w - 1) * cpp;
  2427. }
  2428. }
  2429. intel_crtc->adjusted_x = x;
  2430. intel_crtc->adjusted_y = y;
  2431. I915_WRITE(reg, dspcntr);
  2432. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2433. I915_WRITE(DSPSURF(plane),
  2434. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2435. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2436. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2437. } else {
  2438. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2439. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2440. }
  2441. POSTING_READ(reg);
  2442. }
  2443. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2444. uint64_t fb_modifier, uint32_t pixel_format)
  2445. {
  2446. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2447. return 64;
  2448. } else {
  2449. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2450. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2451. }
  2452. }
  2453. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2454. struct drm_i915_gem_object *obj,
  2455. unsigned int plane)
  2456. {
  2457. struct i915_ggtt_view view;
  2458. struct i915_vma *vma;
  2459. u64 offset;
  2460. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2461. intel_plane->base.state->rotation);
  2462. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2463. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2464. view.type))
  2465. return -1;
  2466. offset = vma->node.start;
  2467. if (plane == 1) {
  2468. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2469. PAGE_SIZE;
  2470. }
  2471. WARN_ON(upper_32_bits(offset));
  2472. return lower_32_bits(offset);
  2473. }
  2474. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2475. {
  2476. struct drm_device *dev = intel_crtc->base.dev;
  2477. struct drm_i915_private *dev_priv = to_i915(dev);
  2478. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2479. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2480. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2481. }
  2482. /*
  2483. * This function detaches (aka. unbinds) unused scalers in hardware
  2484. */
  2485. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2486. {
  2487. struct intel_crtc_scaler_state *scaler_state;
  2488. int i;
  2489. scaler_state = &intel_crtc->config->scaler_state;
  2490. /* loop through and disable scalers that aren't in use */
  2491. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2492. if (!scaler_state->scalers[i].in_use)
  2493. skl_detach_scaler(intel_crtc, i);
  2494. }
  2495. }
  2496. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2497. {
  2498. switch (pixel_format) {
  2499. case DRM_FORMAT_C8:
  2500. return PLANE_CTL_FORMAT_INDEXED;
  2501. case DRM_FORMAT_RGB565:
  2502. return PLANE_CTL_FORMAT_RGB_565;
  2503. case DRM_FORMAT_XBGR8888:
  2504. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2505. case DRM_FORMAT_XRGB8888:
  2506. return PLANE_CTL_FORMAT_XRGB_8888;
  2507. /*
  2508. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2509. * to be already pre-multiplied. We need to add a knob (or a different
  2510. * DRM_FORMAT) for user-space to configure that.
  2511. */
  2512. case DRM_FORMAT_ABGR8888:
  2513. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2514. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2515. case DRM_FORMAT_ARGB8888:
  2516. return PLANE_CTL_FORMAT_XRGB_8888 |
  2517. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2518. case DRM_FORMAT_XRGB2101010:
  2519. return PLANE_CTL_FORMAT_XRGB_2101010;
  2520. case DRM_FORMAT_XBGR2101010:
  2521. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2522. case DRM_FORMAT_YUYV:
  2523. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2524. case DRM_FORMAT_YVYU:
  2525. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2526. case DRM_FORMAT_UYVY:
  2527. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2528. case DRM_FORMAT_VYUY:
  2529. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2530. default:
  2531. MISSING_CASE(pixel_format);
  2532. }
  2533. return 0;
  2534. }
  2535. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2536. {
  2537. switch (fb_modifier) {
  2538. case DRM_FORMAT_MOD_NONE:
  2539. break;
  2540. case I915_FORMAT_MOD_X_TILED:
  2541. return PLANE_CTL_TILED_X;
  2542. case I915_FORMAT_MOD_Y_TILED:
  2543. return PLANE_CTL_TILED_Y;
  2544. case I915_FORMAT_MOD_Yf_TILED:
  2545. return PLANE_CTL_TILED_YF;
  2546. default:
  2547. MISSING_CASE(fb_modifier);
  2548. }
  2549. return 0;
  2550. }
  2551. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2552. {
  2553. switch (rotation) {
  2554. case BIT(DRM_ROTATE_0):
  2555. break;
  2556. /*
  2557. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2558. * while i915 HW rotation is clockwise, thats why this swapping.
  2559. */
  2560. case BIT(DRM_ROTATE_90):
  2561. return PLANE_CTL_ROTATE_270;
  2562. case BIT(DRM_ROTATE_180):
  2563. return PLANE_CTL_ROTATE_180;
  2564. case BIT(DRM_ROTATE_270):
  2565. return PLANE_CTL_ROTATE_90;
  2566. default:
  2567. MISSING_CASE(rotation);
  2568. }
  2569. return 0;
  2570. }
  2571. static void skylake_update_primary_plane(struct drm_plane *plane,
  2572. const struct intel_crtc_state *crtc_state,
  2573. const struct intel_plane_state *plane_state)
  2574. {
  2575. struct drm_device *dev = plane->dev;
  2576. struct drm_i915_private *dev_priv = to_i915(dev);
  2577. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2578. struct drm_framebuffer *fb = plane_state->base.fb;
  2579. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2580. int pipe = intel_crtc->pipe;
  2581. u32 plane_ctl, stride_div, stride;
  2582. u32 tile_height, plane_offset, plane_size;
  2583. unsigned int rotation = plane_state->base.rotation;
  2584. int x_offset, y_offset;
  2585. u32 surf_addr;
  2586. int scaler_id = plane_state->scaler_id;
  2587. int src_x = plane_state->src.x1 >> 16;
  2588. int src_y = plane_state->src.y1 >> 16;
  2589. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2590. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2591. int dst_x = plane_state->dst.x1;
  2592. int dst_y = plane_state->dst.y1;
  2593. int dst_w = drm_rect_width(&plane_state->dst);
  2594. int dst_h = drm_rect_height(&plane_state->dst);
  2595. plane_ctl = PLANE_CTL_ENABLE |
  2596. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2597. PLANE_CTL_PIPE_CSC_ENABLE;
  2598. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2599. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2600. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2601. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2602. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2603. fb->pixel_format);
  2604. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2605. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2606. if (intel_rotation_90_or_270(rotation)) {
  2607. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2608. /* stride = Surface height in tiles */
  2609. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2610. stride = DIV_ROUND_UP(fb->height, tile_height);
  2611. x_offset = stride * tile_height - src_y - src_h;
  2612. y_offset = src_x;
  2613. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2614. } else {
  2615. stride = fb->pitches[0] / stride_div;
  2616. x_offset = src_x;
  2617. y_offset = src_y;
  2618. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2619. }
  2620. plane_offset = y_offset << 16 | x_offset;
  2621. intel_crtc->adjusted_x = x_offset;
  2622. intel_crtc->adjusted_y = y_offset;
  2623. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2624. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2625. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2626. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2627. if (scaler_id >= 0) {
  2628. uint32_t ps_ctrl = 0;
  2629. WARN_ON(!dst_w || !dst_h);
  2630. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2631. crtc_state->scaler_state.scalers[scaler_id].mode;
  2632. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2633. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2634. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2635. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2636. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2637. } else {
  2638. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2639. }
  2640. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2641. POSTING_READ(PLANE_SURF(pipe, 0));
  2642. }
  2643. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2644. struct drm_crtc *crtc)
  2645. {
  2646. struct drm_device *dev = crtc->dev;
  2647. struct drm_i915_private *dev_priv = to_i915(dev);
  2648. int pipe = to_intel_crtc(crtc)->pipe;
  2649. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2650. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2651. POSTING_READ(PLANE_SURF(pipe, 0));
  2652. }
  2653. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2654. static int
  2655. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2656. int x, int y, enum mode_set_atomic state)
  2657. {
  2658. /* Support for kgdboc is disabled, this needs a major rework. */
  2659. DRM_ERROR("legacy panic handler not supported any more.\n");
  2660. return -ENODEV;
  2661. }
  2662. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2663. {
  2664. struct intel_crtc *crtc;
  2665. for_each_intel_crtc(&dev_priv->drm, crtc)
  2666. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2667. }
  2668. static void intel_update_primary_planes(struct drm_device *dev)
  2669. {
  2670. struct drm_crtc *crtc;
  2671. for_each_crtc(dev, crtc) {
  2672. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2673. struct intel_plane_state *plane_state;
  2674. drm_modeset_lock_crtc(crtc, &plane->base);
  2675. plane_state = to_intel_plane_state(plane->base.state);
  2676. if (plane_state->visible)
  2677. plane->update_plane(&plane->base,
  2678. to_intel_crtc_state(crtc->state),
  2679. plane_state);
  2680. drm_modeset_unlock_crtc(crtc);
  2681. }
  2682. }
  2683. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2684. {
  2685. /* no reset support for gen2 */
  2686. if (IS_GEN2(dev_priv))
  2687. return;
  2688. /* reset doesn't touch the display */
  2689. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2690. return;
  2691. drm_modeset_lock_all(&dev_priv->drm);
  2692. /*
  2693. * Disabling the crtcs gracefully seems nicer. Also the
  2694. * g33 docs say we should at least disable all the planes.
  2695. */
  2696. intel_display_suspend(&dev_priv->drm);
  2697. }
  2698. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2699. {
  2700. /*
  2701. * Flips in the rings will be nuked by the reset,
  2702. * so complete all pending flips so that user space
  2703. * will get its events and not get stuck.
  2704. */
  2705. intel_complete_page_flips(dev_priv);
  2706. /* no reset support for gen2 */
  2707. if (IS_GEN2(dev_priv))
  2708. return;
  2709. /* reset doesn't touch the display */
  2710. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2711. /*
  2712. * Flips in the rings have been nuked by the reset,
  2713. * so update the base address of all primary
  2714. * planes to the the last fb to make sure we're
  2715. * showing the correct fb after a reset.
  2716. *
  2717. * FIXME: Atomic will make this obsolete since we won't schedule
  2718. * CS-based flips (which might get lost in gpu resets) any more.
  2719. */
  2720. intel_update_primary_planes(&dev_priv->drm);
  2721. return;
  2722. }
  2723. /*
  2724. * The display has been reset as well,
  2725. * so need a full re-initialization.
  2726. */
  2727. intel_runtime_pm_disable_interrupts(dev_priv);
  2728. intel_runtime_pm_enable_interrupts(dev_priv);
  2729. intel_modeset_init_hw(&dev_priv->drm);
  2730. spin_lock_irq(&dev_priv->irq_lock);
  2731. if (dev_priv->display.hpd_irq_setup)
  2732. dev_priv->display.hpd_irq_setup(dev_priv);
  2733. spin_unlock_irq(&dev_priv->irq_lock);
  2734. intel_display_resume(&dev_priv->drm);
  2735. intel_hpd_init(dev_priv);
  2736. drm_modeset_unlock_all(&dev_priv->drm);
  2737. }
  2738. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2739. {
  2740. struct drm_device *dev = crtc->dev;
  2741. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2742. unsigned reset_counter;
  2743. bool pending;
  2744. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2745. if (intel_crtc->reset_counter != reset_counter)
  2746. return false;
  2747. spin_lock_irq(&dev->event_lock);
  2748. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2749. spin_unlock_irq(&dev->event_lock);
  2750. return pending;
  2751. }
  2752. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2753. struct intel_crtc_state *old_crtc_state)
  2754. {
  2755. struct drm_device *dev = crtc->base.dev;
  2756. struct drm_i915_private *dev_priv = to_i915(dev);
  2757. struct intel_crtc_state *pipe_config =
  2758. to_intel_crtc_state(crtc->base.state);
  2759. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2760. crtc->base.mode = crtc->base.state->mode;
  2761. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2762. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2763. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2764. /*
  2765. * Update pipe size and adjust fitter if needed: the reason for this is
  2766. * that in compute_mode_changes we check the native mode (not the pfit
  2767. * mode) to see if we can flip rather than do a full mode set. In the
  2768. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2769. * pfit state, we'll end up with a big fb scanned out into the wrong
  2770. * sized surface.
  2771. */
  2772. I915_WRITE(PIPESRC(crtc->pipe),
  2773. ((pipe_config->pipe_src_w - 1) << 16) |
  2774. (pipe_config->pipe_src_h - 1));
  2775. /* on skylake this is done by detaching scalers */
  2776. if (INTEL_INFO(dev)->gen >= 9) {
  2777. skl_detach_scalers(crtc);
  2778. if (pipe_config->pch_pfit.enabled)
  2779. skylake_pfit_enable(crtc);
  2780. } else if (HAS_PCH_SPLIT(dev)) {
  2781. if (pipe_config->pch_pfit.enabled)
  2782. ironlake_pfit_enable(crtc);
  2783. else if (old_crtc_state->pch_pfit.enabled)
  2784. ironlake_pfit_disable(crtc, true);
  2785. }
  2786. }
  2787. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2788. {
  2789. struct drm_device *dev = crtc->dev;
  2790. struct drm_i915_private *dev_priv = to_i915(dev);
  2791. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2792. int pipe = intel_crtc->pipe;
  2793. i915_reg_t reg;
  2794. u32 temp;
  2795. /* enable normal train */
  2796. reg = FDI_TX_CTL(pipe);
  2797. temp = I915_READ(reg);
  2798. if (IS_IVYBRIDGE(dev)) {
  2799. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2800. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2801. } else {
  2802. temp &= ~FDI_LINK_TRAIN_NONE;
  2803. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2804. }
  2805. I915_WRITE(reg, temp);
  2806. reg = FDI_RX_CTL(pipe);
  2807. temp = I915_READ(reg);
  2808. if (HAS_PCH_CPT(dev)) {
  2809. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2810. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2811. } else {
  2812. temp &= ~FDI_LINK_TRAIN_NONE;
  2813. temp |= FDI_LINK_TRAIN_NONE;
  2814. }
  2815. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2816. /* wait one idle pattern time */
  2817. POSTING_READ(reg);
  2818. udelay(1000);
  2819. /* IVB wants error correction enabled */
  2820. if (IS_IVYBRIDGE(dev))
  2821. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2822. FDI_FE_ERRC_ENABLE);
  2823. }
  2824. /* The FDI link training functions for ILK/Ibexpeak. */
  2825. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2826. {
  2827. struct drm_device *dev = crtc->dev;
  2828. struct drm_i915_private *dev_priv = to_i915(dev);
  2829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2830. int pipe = intel_crtc->pipe;
  2831. i915_reg_t reg;
  2832. u32 temp, tries;
  2833. /* FDI needs bits from pipe first */
  2834. assert_pipe_enabled(dev_priv, pipe);
  2835. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2836. for train result */
  2837. reg = FDI_RX_IMR(pipe);
  2838. temp = I915_READ(reg);
  2839. temp &= ~FDI_RX_SYMBOL_LOCK;
  2840. temp &= ~FDI_RX_BIT_LOCK;
  2841. I915_WRITE(reg, temp);
  2842. I915_READ(reg);
  2843. udelay(150);
  2844. /* enable CPU FDI TX and PCH FDI RX */
  2845. reg = FDI_TX_CTL(pipe);
  2846. temp = I915_READ(reg);
  2847. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2848. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2849. temp &= ~FDI_LINK_TRAIN_NONE;
  2850. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2851. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2852. reg = FDI_RX_CTL(pipe);
  2853. temp = I915_READ(reg);
  2854. temp &= ~FDI_LINK_TRAIN_NONE;
  2855. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2856. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2857. POSTING_READ(reg);
  2858. udelay(150);
  2859. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2860. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2861. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2862. FDI_RX_PHASE_SYNC_POINTER_EN);
  2863. reg = FDI_RX_IIR(pipe);
  2864. for (tries = 0; tries < 5; tries++) {
  2865. temp = I915_READ(reg);
  2866. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2867. if ((temp & FDI_RX_BIT_LOCK)) {
  2868. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2869. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2870. break;
  2871. }
  2872. }
  2873. if (tries == 5)
  2874. DRM_ERROR("FDI train 1 fail!\n");
  2875. /* Train 2 */
  2876. reg = FDI_TX_CTL(pipe);
  2877. temp = I915_READ(reg);
  2878. temp &= ~FDI_LINK_TRAIN_NONE;
  2879. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2880. I915_WRITE(reg, temp);
  2881. reg = FDI_RX_CTL(pipe);
  2882. temp = I915_READ(reg);
  2883. temp &= ~FDI_LINK_TRAIN_NONE;
  2884. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2885. I915_WRITE(reg, temp);
  2886. POSTING_READ(reg);
  2887. udelay(150);
  2888. reg = FDI_RX_IIR(pipe);
  2889. for (tries = 0; tries < 5; tries++) {
  2890. temp = I915_READ(reg);
  2891. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2892. if (temp & FDI_RX_SYMBOL_LOCK) {
  2893. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2894. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2895. break;
  2896. }
  2897. }
  2898. if (tries == 5)
  2899. DRM_ERROR("FDI train 2 fail!\n");
  2900. DRM_DEBUG_KMS("FDI train done\n");
  2901. }
  2902. static const int snb_b_fdi_train_param[] = {
  2903. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2904. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2905. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2906. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2907. };
  2908. /* The FDI link training functions for SNB/Cougarpoint. */
  2909. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2910. {
  2911. struct drm_device *dev = crtc->dev;
  2912. struct drm_i915_private *dev_priv = to_i915(dev);
  2913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2914. int pipe = intel_crtc->pipe;
  2915. i915_reg_t reg;
  2916. u32 temp, i, retry;
  2917. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2918. for train result */
  2919. reg = FDI_RX_IMR(pipe);
  2920. temp = I915_READ(reg);
  2921. temp &= ~FDI_RX_SYMBOL_LOCK;
  2922. temp &= ~FDI_RX_BIT_LOCK;
  2923. I915_WRITE(reg, temp);
  2924. POSTING_READ(reg);
  2925. udelay(150);
  2926. /* enable CPU FDI TX and PCH FDI RX */
  2927. reg = FDI_TX_CTL(pipe);
  2928. temp = I915_READ(reg);
  2929. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2930. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2931. temp &= ~FDI_LINK_TRAIN_NONE;
  2932. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2933. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2934. /* SNB-B */
  2935. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2936. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2937. I915_WRITE(FDI_RX_MISC(pipe),
  2938. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2939. reg = FDI_RX_CTL(pipe);
  2940. temp = I915_READ(reg);
  2941. if (HAS_PCH_CPT(dev)) {
  2942. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2943. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2944. } else {
  2945. temp &= ~FDI_LINK_TRAIN_NONE;
  2946. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2947. }
  2948. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2949. POSTING_READ(reg);
  2950. udelay(150);
  2951. for (i = 0; i < 4; i++) {
  2952. reg = FDI_TX_CTL(pipe);
  2953. temp = I915_READ(reg);
  2954. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2955. temp |= snb_b_fdi_train_param[i];
  2956. I915_WRITE(reg, temp);
  2957. POSTING_READ(reg);
  2958. udelay(500);
  2959. for (retry = 0; retry < 5; retry++) {
  2960. reg = FDI_RX_IIR(pipe);
  2961. temp = I915_READ(reg);
  2962. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2963. if (temp & FDI_RX_BIT_LOCK) {
  2964. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2965. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2966. break;
  2967. }
  2968. udelay(50);
  2969. }
  2970. if (retry < 5)
  2971. break;
  2972. }
  2973. if (i == 4)
  2974. DRM_ERROR("FDI train 1 fail!\n");
  2975. /* Train 2 */
  2976. reg = FDI_TX_CTL(pipe);
  2977. temp = I915_READ(reg);
  2978. temp &= ~FDI_LINK_TRAIN_NONE;
  2979. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2980. if (IS_GEN6(dev)) {
  2981. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2982. /* SNB-B */
  2983. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2984. }
  2985. I915_WRITE(reg, temp);
  2986. reg = FDI_RX_CTL(pipe);
  2987. temp = I915_READ(reg);
  2988. if (HAS_PCH_CPT(dev)) {
  2989. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2990. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2991. } else {
  2992. temp &= ~FDI_LINK_TRAIN_NONE;
  2993. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2994. }
  2995. I915_WRITE(reg, temp);
  2996. POSTING_READ(reg);
  2997. udelay(150);
  2998. for (i = 0; i < 4; i++) {
  2999. reg = FDI_TX_CTL(pipe);
  3000. temp = I915_READ(reg);
  3001. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3002. temp |= snb_b_fdi_train_param[i];
  3003. I915_WRITE(reg, temp);
  3004. POSTING_READ(reg);
  3005. udelay(500);
  3006. for (retry = 0; retry < 5; retry++) {
  3007. reg = FDI_RX_IIR(pipe);
  3008. temp = I915_READ(reg);
  3009. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3010. if (temp & FDI_RX_SYMBOL_LOCK) {
  3011. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3012. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3013. break;
  3014. }
  3015. udelay(50);
  3016. }
  3017. if (retry < 5)
  3018. break;
  3019. }
  3020. if (i == 4)
  3021. DRM_ERROR("FDI train 2 fail!\n");
  3022. DRM_DEBUG_KMS("FDI train done.\n");
  3023. }
  3024. /* Manual link training for Ivy Bridge A0 parts */
  3025. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3026. {
  3027. struct drm_device *dev = crtc->dev;
  3028. struct drm_i915_private *dev_priv = to_i915(dev);
  3029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3030. int pipe = intel_crtc->pipe;
  3031. i915_reg_t reg;
  3032. u32 temp, i, j;
  3033. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3034. for train result */
  3035. reg = FDI_RX_IMR(pipe);
  3036. temp = I915_READ(reg);
  3037. temp &= ~FDI_RX_SYMBOL_LOCK;
  3038. temp &= ~FDI_RX_BIT_LOCK;
  3039. I915_WRITE(reg, temp);
  3040. POSTING_READ(reg);
  3041. udelay(150);
  3042. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3043. I915_READ(FDI_RX_IIR(pipe)));
  3044. /* Try each vswing and preemphasis setting twice before moving on */
  3045. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3046. /* disable first in case we need to retry */
  3047. reg = FDI_TX_CTL(pipe);
  3048. temp = I915_READ(reg);
  3049. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3050. temp &= ~FDI_TX_ENABLE;
  3051. I915_WRITE(reg, temp);
  3052. reg = FDI_RX_CTL(pipe);
  3053. temp = I915_READ(reg);
  3054. temp &= ~FDI_LINK_TRAIN_AUTO;
  3055. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3056. temp &= ~FDI_RX_ENABLE;
  3057. I915_WRITE(reg, temp);
  3058. /* enable CPU FDI TX and PCH FDI RX */
  3059. reg = FDI_TX_CTL(pipe);
  3060. temp = I915_READ(reg);
  3061. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3062. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3063. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3064. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3065. temp |= snb_b_fdi_train_param[j/2];
  3066. temp |= FDI_COMPOSITE_SYNC;
  3067. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3068. I915_WRITE(FDI_RX_MISC(pipe),
  3069. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3070. reg = FDI_RX_CTL(pipe);
  3071. temp = I915_READ(reg);
  3072. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3073. temp |= FDI_COMPOSITE_SYNC;
  3074. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3075. POSTING_READ(reg);
  3076. udelay(1); /* should be 0.5us */
  3077. for (i = 0; i < 4; i++) {
  3078. reg = FDI_RX_IIR(pipe);
  3079. temp = I915_READ(reg);
  3080. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3081. if (temp & FDI_RX_BIT_LOCK ||
  3082. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3083. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3084. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3085. i);
  3086. break;
  3087. }
  3088. udelay(1); /* should be 0.5us */
  3089. }
  3090. if (i == 4) {
  3091. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3092. continue;
  3093. }
  3094. /* Train 2 */
  3095. reg = FDI_TX_CTL(pipe);
  3096. temp = I915_READ(reg);
  3097. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3098. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3099. I915_WRITE(reg, temp);
  3100. reg = FDI_RX_CTL(pipe);
  3101. temp = I915_READ(reg);
  3102. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3103. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3104. I915_WRITE(reg, temp);
  3105. POSTING_READ(reg);
  3106. udelay(2); /* should be 1.5us */
  3107. for (i = 0; i < 4; i++) {
  3108. reg = FDI_RX_IIR(pipe);
  3109. temp = I915_READ(reg);
  3110. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3111. if (temp & FDI_RX_SYMBOL_LOCK ||
  3112. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3113. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3114. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3115. i);
  3116. goto train_done;
  3117. }
  3118. udelay(2); /* should be 1.5us */
  3119. }
  3120. if (i == 4)
  3121. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3122. }
  3123. train_done:
  3124. DRM_DEBUG_KMS("FDI train done.\n");
  3125. }
  3126. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3127. {
  3128. struct drm_device *dev = intel_crtc->base.dev;
  3129. struct drm_i915_private *dev_priv = to_i915(dev);
  3130. int pipe = intel_crtc->pipe;
  3131. i915_reg_t reg;
  3132. u32 temp;
  3133. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3134. reg = FDI_RX_CTL(pipe);
  3135. temp = I915_READ(reg);
  3136. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3137. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3138. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3139. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3140. POSTING_READ(reg);
  3141. udelay(200);
  3142. /* Switch from Rawclk to PCDclk */
  3143. temp = I915_READ(reg);
  3144. I915_WRITE(reg, temp | FDI_PCDCLK);
  3145. POSTING_READ(reg);
  3146. udelay(200);
  3147. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3148. reg = FDI_TX_CTL(pipe);
  3149. temp = I915_READ(reg);
  3150. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3151. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3152. POSTING_READ(reg);
  3153. udelay(100);
  3154. }
  3155. }
  3156. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3157. {
  3158. struct drm_device *dev = intel_crtc->base.dev;
  3159. struct drm_i915_private *dev_priv = to_i915(dev);
  3160. int pipe = intel_crtc->pipe;
  3161. i915_reg_t reg;
  3162. u32 temp;
  3163. /* Switch from PCDclk to Rawclk */
  3164. reg = FDI_RX_CTL(pipe);
  3165. temp = I915_READ(reg);
  3166. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3167. /* Disable CPU FDI TX PLL */
  3168. reg = FDI_TX_CTL(pipe);
  3169. temp = I915_READ(reg);
  3170. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3171. POSTING_READ(reg);
  3172. udelay(100);
  3173. reg = FDI_RX_CTL(pipe);
  3174. temp = I915_READ(reg);
  3175. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3176. /* Wait for the clocks to turn off. */
  3177. POSTING_READ(reg);
  3178. udelay(100);
  3179. }
  3180. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3181. {
  3182. struct drm_device *dev = crtc->dev;
  3183. struct drm_i915_private *dev_priv = to_i915(dev);
  3184. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3185. int pipe = intel_crtc->pipe;
  3186. i915_reg_t reg;
  3187. u32 temp;
  3188. /* disable CPU FDI tx and PCH FDI rx */
  3189. reg = FDI_TX_CTL(pipe);
  3190. temp = I915_READ(reg);
  3191. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3192. POSTING_READ(reg);
  3193. reg = FDI_RX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. temp &= ~(0x7 << 16);
  3196. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3197. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3198. POSTING_READ(reg);
  3199. udelay(100);
  3200. /* Ironlake workaround, disable clock pointer after downing FDI */
  3201. if (HAS_PCH_IBX(dev))
  3202. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3203. /* still set train pattern 1 */
  3204. reg = FDI_TX_CTL(pipe);
  3205. temp = I915_READ(reg);
  3206. temp &= ~FDI_LINK_TRAIN_NONE;
  3207. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3208. I915_WRITE(reg, temp);
  3209. reg = FDI_RX_CTL(pipe);
  3210. temp = I915_READ(reg);
  3211. if (HAS_PCH_CPT(dev)) {
  3212. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3213. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3214. } else {
  3215. temp &= ~FDI_LINK_TRAIN_NONE;
  3216. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3217. }
  3218. /* BPC in FDI rx is consistent with that in PIPECONF */
  3219. temp &= ~(0x07 << 16);
  3220. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3221. I915_WRITE(reg, temp);
  3222. POSTING_READ(reg);
  3223. udelay(100);
  3224. }
  3225. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3226. {
  3227. struct intel_crtc *crtc;
  3228. /* Note that we don't need to be called with mode_config.lock here
  3229. * as our list of CRTC objects is static for the lifetime of the
  3230. * device and so cannot disappear as we iterate. Similarly, we can
  3231. * happily treat the predicates as racy, atomic checks as userspace
  3232. * cannot claim and pin a new fb without at least acquring the
  3233. * struct_mutex and so serialising with us.
  3234. */
  3235. for_each_intel_crtc(dev, crtc) {
  3236. if (atomic_read(&crtc->unpin_work_count) == 0)
  3237. continue;
  3238. if (crtc->flip_work)
  3239. intel_wait_for_vblank(dev, crtc->pipe);
  3240. return true;
  3241. }
  3242. return false;
  3243. }
  3244. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3245. {
  3246. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3247. struct intel_flip_work *work = intel_crtc->flip_work;
  3248. intel_crtc->flip_work = NULL;
  3249. if (work->event)
  3250. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3251. drm_crtc_vblank_put(&intel_crtc->base);
  3252. wake_up_all(&dev_priv->pending_flip_queue);
  3253. queue_work(dev_priv->wq, &work->unpin_work);
  3254. trace_i915_flip_complete(intel_crtc->plane,
  3255. work->pending_flip_obj);
  3256. }
  3257. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3258. {
  3259. struct drm_device *dev = crtc->dev;
  3260. struct drm_i915_private *dev_priv = to_i915(dev);
  3261. long ret;
  3262. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3263. ret = wait_event_interruptible_timeout(
  3264. dev_priv->pending_flip_queue,
  3265. !intel_crtc_has_pending_flip(crtc),
  3266. 60*HZ);
  3267. if (ret < 0)
  3268. return ret;
  3269. if (ret == 0) {
  3270. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3271. struct intel_flip_work *work;
  3272. spin_lock_irq(&dev->event_lock);
  3273. work = intel_crtc->flip_work;
  3274. if (work && !is_mmio_work(work)) {
  3275. WARN_ONCE(1, "Removing stuck page flip\n");
  3276. page_flip_completed(intel_crtc);
  3277. }
  3278. spin_unlock_irq(&dev->event_lock);
  3279. }
  3280. return 0;
  3281. }
  3282. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3283. {
  3284. u32 temp;
  3285. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3286. mutex_lock(&dev_priv->sb_lock);
  3287. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3288. temp |= SBI_SSCCTL_DISABLE;
  3289. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3290. mutex_unlock(&dev_priv->sb_lock);
  3291. }
  3292. /* Program iCLKIP clock to the desired frequency */
  3293. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3294. {
  3295. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3296. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3297. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3298. u32 temp;
  3299. lpt_disable_iclkip(dev_priv);
  3300. /* The iCLK virtual clock root frequency is in MHz,
  3301. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3302. * divisors, it is necessary to divide one by another, so we
  3303. * convert the virtual clock precision to KHz here for higher
  3304. * precision.
  3305. */
  3306. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3307. u32 iclk_virtual_root_freq = 172800 * 1000;
  3308. u32 iclk_pi_range = 64;
  3309. u32 desired_divisor;
  3310. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3311. clock << auxdiv);
  3312. divsel = (desired_divisor / iclk_pi_range) - 2;
  3313. phaseinc = desired_divisor % iclk_pi_range;
  3314. /*
  3315. * Near 20MHz is a corner case which is
  3316. * out of range for the 7-bit divisor
  3317. */
  3318. if (divsel <= 0x7f)
  3319. break;
  3320. }
  3321. /* This should not happen with any sane values */
  3322. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3323. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3324. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3325. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3326. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3327. clock,
  3328. auxdiv,
  3329. divsel,
  3330. phasedir,
  3331. phaseinc);
  3332. mutex_lock(&dev_priv->sb_lock);
  3333. /* Program SSCDIVINTPHASE6 */
  3334. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3335. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3336. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3337. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3338. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3339. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3340. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3341. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3342. /* Program SSCAUXDIV */
  3343. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3344. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3345. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3346. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3347. /* Enable modulator and associated divider */
  3348. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3349. temp &= ~SBI_SSCCTL_DISABLE;
  3350. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3351. mutex_unlock(&dev_priv->sb_lock);
  3352. /* Wait for initialization time */
  3353. udelay(24);
  3354. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3355. }
  3356. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3357. {
  3358. u32 divsel, phaseinc, auxdiv;
  3359. u32 iclk_virtual_root_freq = 172800 * 1000;
  3360. u32 iclk_pi_range = 64;
  3361. u32 desired_divisor;
  3362. u32 temp;
  3363. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3364. return 0;
  3365. mutex_lock(&dev_priv->sb_lock);
  3366. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3367. if (temp & SBI_SSCCTL_DISABLE) {
  3368. mutex_unlock(&dev_priv->sb_lock);
  3369. return 0;
  3370. }
  3371. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3372. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3373. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3374. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3375. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3376. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3377. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3378. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3379. mutex_unlock(&dev_priv->sb_lock);
  3380. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3381. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3382. desired_divisor << auxdiv);
  3383. }
  3384. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3385. enum pipe pch_transcoder)
  3386. {
  3387. struct drm_device *dev = crtc->base.dev;
  3388. struct drm_i915_private *dev_priv = to_i915(dev);
  3389. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3390. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3391. I915_READ(HTOTAL(cpu_transcoder)));
  3392. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3393. I915_READ(HBLANK(cpu_transcoder)));
  3394. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3395. I915_READ(HSYNC(cpu_transcoder)));
  3396. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3397. I915_READ(VTOTAL(cpu_transcoder)));
  3398. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3399. I915_READ(VBLANK(cpu_transcoder)));
  3400. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3401. I915_READ(VSYNC(cpu_transcoder)));
  3402. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3403. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3404. }
  3405. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3406. {
  3407. struct drm_i915_private *dev_priv = to_i915(dev);
  3408. uint32_t temp;
  3409. temp = I915_READ(SOUTH_CHICKEN1);
  3410. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3411. return;
  3412. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3413. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3414. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3415. if (enable)
  3416. temp |= FDI_BC_BIFURCATION_SELECT;
  3417. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3418. I915_WRITE(SOUTH_CHICKEN1, temp);
  3419. POSTING_READ(SOUTH_CHICKEN1);
  3420. }
  3421. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3422. {
  3423. struct drm_device *dev = intel_crtc->base.dev;
  3424. switch (intel_crtc->pipe) {
  3425. case PIPE_A:
  3426. break;
  3427. case PIPE_B:
  3428. if (intel_crtc->config->fdi_lanes > 2)
  3429. cpt_set_fdi_bc_bifurcation(dev, false);
  3430. else
  3431. cpt_set_fdi_bc_bifurcation(dev, true);
  3432. break;
  3433. case PIPE_C:
  3434. cpt_set_fdi_bc_bifurcation(dev, true);
  3435. break;
  3436. default:
  3437. BUG();
  3438. }
  3439. }
  3440. /* Return which DP Port should be selected for Transcoder DP control */
  3441. static enum port
  3442. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3443. {
  3444. struct drm_device *dev = crtc->dev;
  3445. struct intel_encoder *encoder;
  3446. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3447. if (encoder->type == INTEL_OUTPUT_DP ||
  3448. encoder->type == INTEL_OUTPUT_EDP)
  3449. return enc_to_dig_port(&encoder->base)->port;
  3450. }
  3451. return -1;
  3452. }
  3453. /*
  3454. * Enable PCH resources required for PCH ports:
  3455. * - PCH PLLs
  3456. * - FDI training & RX/TX
  3457. * - update transcoder timings
  3458. * - DP transcoding bits
  3459. * - transcoder
  3460. */
  3461. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3462. {
  3463. struct drm_device *dev = crtc->dev;
  3464. struct drm_i915_private *dev_priv = to_i915(dev);
  3465. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3466. int pipe = intel_crtc->pipe;
  3467. u32 temp;
  3468. assert_pch_transcoder_disabled(dev_priv, pipe);
  3469. if (IS_IVYBRIDGE(dev))
  3470. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3471. /* Write the TU size bits before fdi link training, so that error
  3472. * detection works. */
  3473. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3474. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3475. /* For PCH output, training FDI link */
  3476. dev_priv->display.fdi_link_train(crtc);
  3477. /* We need to program the right clock selection before writing the pixel
  3478. * mutliplier into the DPLL. */
  3479. if (HAS_PCH_CPT(dev)) {
  3480. u32 sel;
  3481. temp = I915_READ(PCH_DPLL_SEL);
  3482. temp |= TRANS_DPLL_ENABLE(pipe);
  3483. sel = TRANS_DPLLB_SEL(pipe);
  3484. if (intel_crtc->config->shared_dpll ==
  3485. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3486. temp |= sel;
  3487. else
  3488. temp &= ~sel;
  3489. I915_WRITE(PCH_DPLL_SEL, temp);
  3490. }
  3491. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3492. * transcoder, and we actually should do this to not upset any PCH
  3493. * transcoder that already use the clock when we share it.
  3494. *
  3495. * Note that enable_shared_dpll tries to do the right thing, but
  3496. * get_shared_dpll unconditionally resets the pll - we need that to have
  3497. * the right LVDS enable sequence. */
  3498. intel_enable_shared_dpll(intel_crtc);
  3499. /* set transcoder timing, panel must allow it */
  3500. assert_panel_unlocked(dev_priv, pipe);
  3501. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3502. intel_fdi_normal_train(crtc);
  3503. /* For PCH DP, enable TRANS_DP_CTL */
  3504. if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3505. const struct drm_display_mode *adjusted_mode =
  3506. &intel_crtc->config->base.adjusted_mode;
  3507. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3508. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3509. temp = I915_READ(reg);
  3510. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3511. TRANS_DP_SYNC_MASK |
  3512. TRANS_DP_BPC_MASK);
  3513. temp |= TRANS_DP_OUTPUT_ENABLE;
  3514. temp |= bpc << 9; /* same format but at 11:9 */
  3515. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3516. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3517. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3518. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3519. switch (intel_trans_dp_port_sel(crtc)) {
  3520. case PORT_B:
  3521. temp |= TRANS_DP_PORT_SEL_B;
  3522. break;
  3523. case PORT_C:
  3524. temp |= TRANS_DP_PORT_SEL_C;
  3525. break;
  3526. case PORT_D:
  3527. temp |= TRANS_DP_PORT_SEL_D;
  3528. break;
  3529. default:
  3530. BUG();
  3531. }
  3532. I915_WRITE(reg, temp);
  3533. }
  3534. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3535. }
  3536. static void lpt_pch_enable(struct drm_crtc *crtc)
  3537. {
  3538. struct drm_device *dev = crtc->dev;
  3539. struct drm_i915_private *dev_priv = to_i915(dev);
  3540. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3541. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3542. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3543. lpt_program_iclkip(crtc);
  3544. /* Set transcoder timing. */
  3545. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3546. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3547. }
  3548. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3549. {
  3550. struct drm_i915_private *dev_priv = to_i915(dev);
  3551. i915_reg_t dslreg = PIPEDSL(pipe);
  3552. u32 temp;
  3553. temp = I915_READ(dslreg);
  3554. udelay(500);
  3555. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3556. if (wait_for(I915_READ(dslreg) != temp, 5))
  3557. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3558. }
  3559. }
  3560. static int
  3561. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3562. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3563. int src_w, int src_h, int dst_w, int dst_h)
  3564. {
  3565. struct intel_crtc_scaler_state *scaler_state =
  3566. &crtc_state->scaler_state;
  3567. struct intel_crtc *intel_crtc =
  3568. to_intel_crtc(crtc_state->base.crtc);
  3569. int need_scaling;
  3570. need_scaling = intel_rotation_90_or_270(rotation) ?
  3571. (src_h != dst_w || src_w != dst_h):
  3572. (src_w != dst_w || src_h != dst_h);
  3573. /*
  3574. * if plane is being disabled or scaler is no more required or force detach
  3575. * - free scaler binded to this plane/crtc
  3576. * - in order to do this, update crtc->scaler_usage
  3577. *
  3578. * Here scaler state in crtc_state is set free so that
  3579. * scaler can be assigned to other user. Actual register
  3580. * update to free the scaler is done in plane/panel-fit programming.
  3581. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3582. */
  3583. if (force_detach || !need_scaling) {
  3584. if (*scaler_id >= 0) {
  3585. scaler_state->scaler_users &= ~(1 << scaler_user);
  3586. scaler_state->scalers[*scaler_id].in_use = 0;
  3587. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3588. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3589. intel_crtc->pipe, scaler_user, *scaler_id,
  3590. scaler_state->scaler_users);
  3591. *scaler_id = -1;
  3592. }
  3593. return 0;
  3594. }
  3595. /* range checks */
  3596. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3597. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3598. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3599. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3600. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3601. "size is out of scaler range\n",
  3602. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3603. return -EINVAL;
  3604. }
  3605. /* mark this plane as a scaler user in crtc_state */
  3606. scaler_state->scaler_users |= (1 << scaler_user);
  3607. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3608. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3609. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3610. scaler_state->scaler_users);
  3611. return 0;
  3612. }
  3613. /**
  3614. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3615. *
  3616. * @state: crtc's scaler state
  3617. *
  3618. * Return
  3619. * 0 - scaler_usage updated successfully
  3620. * error - requested scaling cannot be supported or other error condition
  3621. */
  3622. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3623. {
  3624. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3625. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3626. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3627. intel_crtc->base.base.id, intel_crtc->base.name,
  3628. intel_crtc->pipe, SKL_CRTC_INDEX);
  3629. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3630. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3631. state->pipe_src_w, state->pipe_src_h,
  3632. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3633. }
  3634. /**
  3635. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3636. *
  3637. * @state: crtc's scaler state
  3638. * @plane_state: atomic plane state to update
  3639. *
  3640. * Return
  3641. * 0 - scaler_usage updated successfully
  3642. * error - requested scaling cannot be supported or other error condition
  3643. */
  3644. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3645. struct intel_plane_state *plane_state)
  3646. {
  3647. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3648. struct intel_plane *intel_plane =
  3649. to_intel_plane(plane_state->base.plane);
  3650. struct drm_framebuffer *fb = plane_state->base.fb;
  3651. int ret;
  3652. bool force_detach = !fb || !plane_state->visible;
  3653. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3654. intel_plane->base.base.id, intel_plane->base.name,
  3655. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3656. ret = skl_update_scaler(crtc_state, force_detach,
  3657. drm_plane_index(&intel_plane->base),
  3658. &plane_state->scaler_id,
  3659. plane_state->base.rotation,
  3660. drm_rect_width(&plane_state->src) >> 16,
  3661. drm_rect_height(&plane_state->src) >> 16,
  3662. drm_rect_width(&plane_state->dst),
  3663. drm_rect_height(&plane_state->dst));
  3664. if (ret || plane_state->scaler_id < 0)
  3665. return ret;
  3666. /* check colorkey */
  3667. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3668. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3669. intel_plane->base.base.id,
  3670. intel_plane->base.name);
  3671. return -EINVAL;
  3672. }
  3673. /* Check src format */
  3674. switch (fb->pixel_format) {
  3675. case DRM_FORMAT_RGB565:
  3676. case DRM_FORMAT_XBGR8888:
  3677. case DRM_FORMAT_XRGB8888:
  3678. case DRM_FORMAT_ABGR8888:
  3679. case DRM_FORMAT_ARGB8888:
  3680. case DRM_FORMAT_XRGB2101010:
  3681. case DRM_FORMAT_XBGR2101010:
  3682. case DRM_FORMAT_YUYV:
  3683. case DRM_FORMAT_YVYU:
  3684. case DRM_FORMAT_UYVY:
  3685. case DRM_FORMAT_VYUY:
  3686. break;
  3687. default:
  3688. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3689. intel_plane->base.base.id, intel_plane->base.name,
  3690. fb->base.id, fb->pixel_format);
  3691. return -EINVAL;
  3692. }
  3693. return 0;
  3694. }
  3695. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3696. {
  3697. int i;
  3698. for (i = 0; i < crtc->num_scalers; i++)
  3699. skl_detach_scaler(crtc, i);
  3700. }
  3701. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3702. {
  3703. struct drm_device *dev = crtc->base.dev;
  3704. struct drm_i915_private *dev_priv = to_i915(dev);
  3705. int pipe = crtc->pipe;
  3706. struct intel_crtc_scaler_state *scaler_state =
  3707. &crtc->config->scaler_state;
  3708. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3709. if (crtc->config->pch_pfit.enabled) {
  3710. int id;
  3711. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3712. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3713. return;
  3714. }
  3715. id = scaler_state->scaler_id;
  3716. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3717. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3718. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3719. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3720. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3721. }
  3722. }
  3723. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3724. {
  3725. struct drm_device *dev = crtc->base.dev;
  3726. struct drm_i915_private *dev_priv = to_i915(dev);
  3727. int pipe = crtc->pipe;
  3728. if (crtc->config->pch_pfit.enabled) {
  3729. /* Force use of hard-coded filter coefficients
  3730. * as some pre-programmed values are broken,
  3731. * e.g. x201.
  3732. */
  3733. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3734. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3735. PF_PIPE_SEL_IVB(pipe));
  3736. else
  3737. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3738. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3739. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3740. }
  3741. }
  3742. void hsw_enable_ips(struct intel_crtc *crtc)
  3743. {
  3744. struct drm_device *dev = crtc->base.dev;
  3745. struct drm_i915_private *dev_priv = to_i915(dev);
  3746. if (!crtc->config->ips_enabled)
  3747. return;
  3748. /*
  3749. * We can only enable IPS after we enable a plane and wait for a vblank
  3750. * This function is called from post_plane_update, which is run after
  3751. * a vblank wait.
  3752. */
  3753. assert_plane_enabled(dev_priv, crtc->plane);
  3754. if (IS_BROADWELL(dev)) {
  3755. mutex_lock(&dev_priv->rps.hw_lock);
  3756. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3757. mutex_unlock(&dev_priv->rps.hw_lock);
  3758. /* Quoting Art Runyan: "its not safe to expect any particular
  3759. * value in IPS_CTL bit 31 after enabling IPS through the
  3760. * mailbox." Moreover, the mailbox may return a bogus state,
  3761. * so we need to just enable it and continue on.
  3762. */
  3763. } else {
  3764. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3765. /* The bit only becomes 1 in the next vblank, so this wait here
  3766. * is essentially intel_wait_for_vblank. If we don't have this
  3767. * and don't wait for vblanks until the end of crtc_enable, then
  3768. * the HW state readout code will complain that the expected
  3769. * IPS_CTL value is not the one we read. */
  3770. if (intel_wait_for_register(dev_priv,
  3771. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  3772. 50))
  3773. DRM_ERROR("Timed out waiting for IPS enable\n");
  3774. }
  3775. }
  3776. void hsw_disable_ips(struct intel_crtc *crtc)
  3777. {
  3778. struct drm_device *dev = crtc->base.dev;
  3779. struct drm_i915_private *dev_priv = to_i915(dev);
  3780. if (!crtc->config->ips_enabled)
  3781. return;
  3782. assert_plane_enabled(dev_priv, crtc->plane);
  3783. if (IS_BROADWELL(dev)) {
  3784. mutex_lock(&dev_priv->rps.hw_lock);
  3785. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3786. mutex_unlock(&dev_priv->rps.hw_lock);
  3787. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3788. if (intel_wait_for_register(dev_priv,
  3789. IPS_CTL, IPS_ENABLE, 0,
  3790. 42))
  3791. DRM_ERROR("Timed out waiting for IPS disable\n");
  3792. } else {
  3793. I915_WRITE(IPS_CTL, 0);
  3794. POSTING_READ(IPS_CTL);
  3795. }
  3796. /* We need to wait for a vblank before we can disable the plane. */
  3797. intel_wait_for_vblank(dev, crtc->pipe);
  3798. }
  3799. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3800. {
  3801. if (intel_crtc->overlay) {
  3802. struct drm_device *dev = intel_crtc->base.dev;
  3803. struct drm_i915_private *dev_priv = to_i915(dev);
  3804. mutex_lock(&dev->struct_mutex);
  3805. dev_priv->mm.interruptible = false;
  3806. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3807. dev_priv->mm.interruptible = true;
  3808. mutex_unlock(&dev->struct_mutex);
  3809. }
  3810. /* Let userspace switch the overlay on again. In most cases userspace
  3811. * has to recompute where to put it anyway.
  3812. */
  3813. }
  3814. /**
  3815. * intel_post_enable_primary - Perform operations after enabling primary plane
  3816. * @crtc: the CRTC whose primary plane was just enabled
  3817. *
  3818. * Performs potentially sleeping operations that must be done after the primary
  3819. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3820. * called due to an explicit primary plane update, or due to an implicit
  3821. * re-enable that is caused when a sprite plane is updated to no longer
  3822. * completely hide the primary plane.
  3823. */
  3824. static void
  3825. intel_post_enable_primary(struct drm_crtc *crtc)
  3826. {
  3827. struct drm_device *dev = crtc->dev;
  3828. struct drm_i915_private *dev_priv = to_i915(dev);
  3829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3830. int pipe = intel_crtc->pipe;
  3831. /*
  3832. * FIXME IPS should be fine as long as one plane is
  3833. * enabled, but in practice it seems to have problems
  3834. * when going from primary only to sprite only and vice
  3835. * versa.
  3836. */
  3837. hsw_enable_ips(intel_crtc);
  3838. /*
  3839. * Gen2 reports pipe underruns whenever all planes are disabled.
  3840. * So don't enable underrun reporting before at least some planes
  3841. * are enabled.
  3842. * FIXME: Need to fix the logic to work when we turn off all planes
  3843. * but leave the pipe running.
  3844. */
  3845. if (IS_GEN2(dev))
  3846. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3847. /* Underruns don't always raise interrupts, so check manually. */
  3848. intel_check_cpu_fifo_underruns(dev_priv);
  3849. intel_check_pch_fifo_underruns(dev_priv);
  3850. }
  3851. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3852. static void
  3853. intel_pre_disable_primary(struct drm_crtc *crtc)
  3854. {
  3855. struct drm_device *dev = crtc->dev;
  3856. struct drm_i915_private *dev_priv = to_i915(dev);
  3857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3858. int pipe = intel_crtc->pipe;
  3859. /*
  3860. * Gen2 reports pipe underruns whenever all planes are disabled.
  3861. * So diasble underrun reporting before all the planes get disabled.
  3862. * FIXME: Need to fix the logic to work when we turn off all planes
  3863. * but leave the pipe running.
  3864. */
  3865. if (IS_GEN2(dev))
  3866. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3867. /*
  3868. * FIXME IPS should be fine as long as one plane is
  3869. * enabled, but in practice it seems to have problems
  3870. * when going from primary only to sprite only and vice
  3871. * versa.
  3872. */
  3873. hsw_disable_ips(intel_crtc);
  3874. }
  3875. /* FIXME get rid of this and use pre_plane_update */
  3876. static void
  3877. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3878. {
  3879. struct drm_device *dev = crtc->dev;
  3880. struct drm_i915_private *dev_priv = to_i915(dev);
  3881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3882. int pipe = intel_crtc->pipe;
  3883. intel_pre_disable_primary(crtc);
  3884. /*
  3885. * Vblank time updates from the shadow to live plane control register
  3886. * are blocked if the memory self-refresh mode is active at that
  3887. * moment. So to make sure the plane gets truly disabled, disable
  3888. * first the self-refresh mode. The self-refresh enable bit in turn
  3889. * will be checked/applied by the HW only at the next frame start
  3890. * event which is after the vblank start event, so we need to have a
  3891. * wait-for-vblank between disabling the plane and the pipe.
  3892. */
  3893. if (HAS_GMCH_DISPLAY(dev)) {
  3894. intel_set_memory_cxsr(dev_priv, false);
  3895. dev_priv->wm.vlv.cxsr = false;
  3896. intel_wait_for_vblank(dev, pipe);
  3897. }
  3898. }
  3899. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3900. {
  3901. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3902. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3903. struct intel_crtc_state *pipe_config =
  3904. to_intel_crtc_state(crtc->base.state);
  3905. struct drm_device *dev = crtc->base.dev;
  3906. struct drm_plane *primary = crtc->base.primary;
  3907. struct drm_plane_state *old_pri_state =
  3908. drm_atomic_get_existing_plane_state(old_state, primary);
  3909. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3910. crtc->wm.cxsr_allowed = true;
  3911. if (pipe_config->update_wm_post && pipe_config->base.active)
  3912. intel_update_watermarks(&crtc->base);
  3913. if (old_pri_state) {
  3914. struct intel_plane_state *primary_state =
  3915. to_intel_plane_state(primary->state);
  3916. struct intel_plane_state *old_primary_state =
  3917. to_intel_plane_state(old_pri_state);
  3918. intel_fbc_post_update(crtc);
  3919. if (primary_state->visible &&
  3920. (needs_modeset(&pipe_config->base) ||
  3921. !old_primary_state->visible))
  3922. intel_post_enable_primary(&crtc->base);
  3923. }
  3924. }
  3925. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3926. {
  3927. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3928. struct drm_device *dev = crtc->base.dev;
  3929. struct drm_i915_private *dev_priv = to_i915(dev);
  3930. struct intel_crtc_state *pipe_config =
  3931. to_intel_crtc_state(crtc->base.state);
  3932. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3933. struct drm_plane *primary = crtc->base.primary;
  3934. struct drm_plane_state *old_pri_state =
  3935. drm_atomic_get_existing_plane_state(old_state, primary);
  3936. bool modeset = needs_modeset(&pipe_config->base);
  3937. if (old_pri_state) {
  3938. struct intel_plane_state *primary_state =
  3939. to_intel_plane_state(primary->state);
  3940. struct intel_plane_state *old_primary_state =
  3941. to_intel_plane_state(old_pri_state);
  3942. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3943. if (old_primary_state->visible &&
  3944. (modeset || !primary_state->visible))
  3945. intel_pre_disable_primary(&crtc->base);
  3946. }
  3947. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  3948. crtc->wm.cxsr_allowed = false;
  3949. /*
  3950. * Vblank time updates from the shadow to live plane control register
  3951. * are blocked if the memory self-refresh mode is active at that
  3952. * moment. So to make sure the plane gets truly disabled, disable
  3953. * first the self-refresh mode. The self-refresh enable bit in turn
  3954. * will be checked/applied by the HW only at the next frame start
  3955. * event which is after the vblank start event, so we need to have a
  3956. * wait-for-vblank between disabling the plane and the pipe.
  3957. */
  3958. if (old_crtc_state->base.active) {
  3959. intel_set_memory_cxsr(dev_priv, false);
  3960. dev_priv->wm.vlv.cxsr = false;
  3961. intel_wait_for_vblank(dev, crtc->pipe);
  3962. }
  3963. }
  3964. /*
  3965. * IVB workaround: must disable low power watermarks for at least
  3966. * one frame before enabling scaling. LP watermarks can be re-enabled
  3967. * when scaling is disabled.
  3968. *
  3969. * WaCxSRDisabledForSpriteScaling:ivb
  3970. */
  3971. if (pipe_config->disable_lp_wm) {
  3972. ilk_disable_lp_wm(dev);
  3973. intel_wait_for_vblank(dev, crtc->pipe);
  3974. }
  3975. /*
  3976. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3977. * watermark programming here.
  3978. */
  3979. if (needs_modeset(&pipe_config->base))
  3980. return;
  3981. /*
  3982. * For platforms that support atomic watermarks, program the
  3983. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3984. * will be the intermediate values that are safe for both pre- and
  3985. * post- vblank; when vblank happens, the 'active' values will be set
  3986. * to the final 'target' values and we'll do this again to get the
  3987. * optimal watermarks. For gen9+ platforms, the values we program here
  3988. * will be the final target values which will get automatically latched
  3989. * at vblank time; no further programming will be necessary.
  3990. *
  3991. * If a platform hasn't been transitioned to atomic watermarks yet,
  3992. * we'll continue to update watermarks the old way, if flags tell
  3993. * us to.
  3994. */
  3995. if (dev_priv->display.initial_watermarks != NULL)
  3996. dev_priv->display.initial_watermarks(pipe_config);
  3997. else if (pipe_config->update_wm_pre)
  3998. intel_update_watermarks(&crtc->base);
  3999. }
  4000. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4001. {
  4002. struct drm_device *dev = crtc->dev;
  4003. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4004. struct drm_plane *p;
  4005. int pipe = intel_crtc->pipe;
  4006. intel_crtc_dpms_overlay_disable(intel_crtc);
  4007. drm_for_each_plane_mask(p, dev, plane_mask)
  4008. to_intel_plane(p)->disable_plane(p, crtc);
  4009. /*
  4010. * FIXME: Once we grow proper nuclear flip support out of this we need
  4011. * to compute the mask of flip planes precisely. For the time being
  4012. * consider this a flip to a NULL plane.
  4013. */
  4014. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4015. }
  4016. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4017. {
  4018. struct drm_device *dev = crtc->dev;
  4019. struct drm_i915_private *dev_priv = to_i915(dev);
  4020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4021. struct intel_encoder *encoder;
  4022. int pipe = intel_crtc->pipe;
  4023. struct intel_crtc_state *pipe_config =
  4024. to_intel_crtc_state(crtc->state);
  4025. if (WARN_ON(intel_crtc->active))
  4026. return;
  4027. /*
  4028. * Sometimes spurious CPU pipe underruns happen during FDI
  4029. * training, at least with VGA+HDMI cloning. Suppress them.
  4030. *
  4031. * On ILK we get an occasional spurious CPU pipe underruns
  4032. * between eDP port A enable and vdd enable. Also PCH port
  4033. * enable seems to result in the occasional CPU pipe underrun.
  4034. *
  4035. * Spurious PCH underruns also occur during PCH enabling.
  4036. */
  4037. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4038. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4039. if (intel_crtc->config->has_pch_encoder)
  4040. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4041. if (intel_crtc->config->has_pch_encoder)
  4042. intel_prepare_shared_dpll(intel_crtc);
  4043. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4044. intel_dp_set_m_n(intel_crtc, M1_N1);
  4045. intel_set_pipe_timings(intel_crtc);
  4046. intel_set_pipe_src_size(intel_crtc);
  4047. if (intel_crtc->config->has_pch_encoder) {
  4048. intel_cpu_transcoder_set_m_n(intel_crtc,
  4049. &intel_crtc->config->fdi_m_n, NULL);
  4050. }
  4051. ironlake_set_pipeconf(crtc);
  4052. intel_crtc->active = true;
  4053. for_each_encoder_on_crtc(dev, crtc, encoder)
  4054. if (encoder->pre_enable)
  4055. encoder->pre_enable(encoder);
  4056. if (intel_crtc->config->has_pch_encoder) {
  4057. /* Note: FDI PLL enabling _must_ be done before we enable the
  4058. * cpu pipes, hence this is separate from all the other fdi/pch
  4059. * enabling. */
  4060. ironlake_fdi_pll_enable(intel_crtc);
  4061. } else {
  4062. assert_fdi_tx_disabled(dev_priv, pipe);
  4063. assert_fdi_rx_disabled(dev_priv, pipe);
  4064. }
  4065. ironlake_pfit_enable(intel_crtc);
  4066. /*
  4067. * On ILK+ LUT must be loaded before the pipe is running but with
  4068. * clocks enabled
  4069. */
  4070. intel_color_load_luts(&pipe_config->base);
  4071. if (dev_priv->display.initial_watermarks != NULL)
  4072. dev_priv->display.initial_watermarks(intel_crtc->config);
  4073. intel_enable_pipe(intel_crtc);
  4074. if (intel_crtc->config->has_pch_encoder)
  4075. ironlake_pch_enable(crtc);
  4076. assert_vblank_disabled(crtc);
  4077. drm_crtc_vblank_on(crtc);
  4078. for_each_encoder_on_crtc(dev, crtc, encoder)
  4079. encoder->enable(encoder);
  4080. if (HAS_PCH_CPT(dev))
  4081. cpt_verify_modeset(dev, intel_crtc->pipe);
  4082. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4083. if (intel_crtc->config->has_pch_encoder)
  4084. intel_wait_for_vblank(dev, pipe);
  4085. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4086. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4087. }
  4088. /* IPS only exists on ULT machines and is tied to pipe A. */
  4089. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4090. {
  4091. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4092. }
  4093. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4094. {
  4095. struct drm_device *dev = crtc->dev;
  4096. struct drm_i915_private *dev_priv = to_i915(dev);
  4097. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4098. struct intel_encoder *encoder;
  4099. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4100. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4101. struct intel_crtc_state *pipe_config =
  4102. to_intel_crtc_state(crtc->state);
  4103. if (WARN_ON(intel_crtc->active))
  4104. return;
  4105. if (intel_crtc->config->has_pch_encoder)
  4106. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4107. false);
  4108. for_each_encoder_on_crtc(dev, crtc, encoder)
  4109. if (encoder->pre_pll_enable)
  4110. encoder->pre_pll_enable(encoder);
  4111. if (intel_crtc->config->shared_dpll)
  4112. intel_enable_shared_dpll(intel_crtc);
  4113. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4114. intel_dp_set_m_n(intel_crtc, M1_N1);
  4115. if (!transcoder_is_dsi(cpu_transcoder))
  4116. intel_set_pipe_timings(intel_crtc);
  4117. intel_set_pipe_src_size(intel_crtc);
  4118. if (cpu_transcoder != TRANSCODER_EDP &&
  4119. !transcoder_is_dsi(cpu_transcoder)) {
  4120. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4121. intel_crtc->config->pixel_multiplier - 1);
  4122. }
  4123. if (intel_crtc->config->has_pch_encoder) {
  4124. intel_cpu_transcoder_set_m_n(intel_crtc,
  4125. &intel_crtc->config->fdi_m_n, NULL);
  4126. }
  4127. if (!transcoder_is_dsi(cpu_transcoder))
  4128. haswell_set_pipeconf(crtc);
  4129. haswell_set_pipemisc(crtc);
  4130. intel_color_set_csc(&pipe_config->base);
  4131. intel_crtc->active = true;
  4132. if (intel_crtc->config->has_pch_encoder)
  4133. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4134. else
  4135. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4136. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4137. if (encoder->pre_enable)
  4138. encoder->pre_enable(encoder);
  4139. }
  4140. if (intel_crtc->config->has_pch_encoder)
  4141. dev_priv->display.fdi_link_train(crtc);
  4142. if (!transcoder_is_dsi(cpu_transcoder))
  4143. intel_ddi_enable_pipe_clock(intel_crtc);
  4144. if (INTEL_INFO(dev)->gen >= 9)
  4145. skylake_pfit_enable(intel_crtc);
  4146. else
  4147. ironlake_pfit_enable(intel_crtc);
  4148. /*
  4149. * On ILK+ LUT must be loaded before the pipe is running but with
  4150. * clocks enabled
  4151. */
  4152. intel_color_load_luts(&pipe_config->base);
  4153. intel_ddi_set_pipe_settings(crtc);
  4154. if (!transcoder_is_dsi(cpu_transcoder))
  4155. intel_ddi_enable_transcoder_func(crtc);
  4156. if (dev_priv->display.initial_watermarks != NULL)
  4157. dev_priv->display.initial_watermarks(pipe_config);
  4158. else
  4159. intel_update_watermarks(crtc);
  4160. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4161. if (!transcoder_is_dsi(cpu_transcoder))
  4162. intel_enable_pipe(intel_crtc);
  4163. if (intel_crtc->config->has_pch_encoder)
  4164. lpt_pch_enable(crtc);
  4165. if (intel_crtc->config->dp_encoder_is_mst)
  4166. intel_ddi_set_vc_payload_alloc(crtc, true);
  4167. assert_vblank_disabled(crtc);
  4168. drm_crtc_vblank_on(crtc);
  4169. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4170. encoder->enable(encoder);
  4171. intel_opregion_notify_encoder(encoder, true);
  4172. }
  4173. if (intel_crtc->config->has_pch_encoder) {
  4174. intel_wait_for_vblank(dev, pipe);
  4175. intel_wait_for_vblank(dev, pipe);
  4176. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4177. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4178. true);
  4179. }
  4180. /* If we change the relative order between pipe/planes enabling, we need
  4181. * to change the workaround. */
  4182. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4183. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4184. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4185. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4186. }
  4187. }
  4188. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4189. {
  4190. struct drm_device *dev = crtc->base.dev;
  4191. struct drm_i915_private *dev_priv = to_i915(dev);
  4192. int pipe = crtc->pipe;
  4193. /* To avoid upsetting the power well on haswell only disable the pfit if
  4194. * it's in use. The hw state code will make sure we get this right. */
  4195. if (force || crtc->config->pch_pfit.enabled) {
  4196. I915_WRITE(PF_CTL(pipe), 0);
  4197. I915_WRITE(PF_WIN_POS(pipe), 0);
  4198. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4199. }
  4200. }
  4201. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4202. {
  4203. struct drm_device *dev = crtc->dev;
  4204. struct drm_i915_private *dev_priv = to_i915(dev);
  4205. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4206. struct intel_encoder *encoder;
  4207. int pipe = intel_crtc->pipe;
  4208. /*
  4209. * Sometimes spurious CPU pipe underruns happen when the
  4210. * pipe is already disabled, but FDI RX/TX is still enabled.
  4211. * Happens at least with VGA+HDMI cloning. Suppress them.
  4212. */
  4213. if (intel_crtc->config->has_pch_encoder) {
  4214. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4215. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4216. }
  4217. for_each_encoder_on_crtc(dev, crtc, encoder)
  4218. encoder->disable(encoder);
  4219. drm_crtc_vblank_off(crtc);
  4220. assert_vblank_disabled(crtc);
  4221. intel_disable_pipe(intel_crtc);
  4222. ironlake_pfit_disable(intel_crtc, false);
  4223. if (intel_crtc->config->has_pch_encoder)
  4224. ironlake_fdi_disable(crtc);
  4225. for_each_encoder_on_crtc(dev, crtc, encoder)
  4226. if (encoder->post_disable)
  4227. encoder->post_disable(encoder);
  4228. if (intel_crtc->config->has_pch_encoder) {
  4229. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4230. if (HAS_PCH_CPT(dev)) {
  4231. i915_reg_t reg;
  4232. u32 temp;
  4233. /* disable TRANS_DP_CTL */
  4234. reg = TRANS_DP_CTL(pipe);
  4235. temp = I915_READ(reg);
  4236. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4237. TRANS_DP_PORT_SEL_MASK);
  4238. temp |= TRANS_DP_PORT_SEL_NONE;
  4239. I915_WRITE(reg, temp);
  4240. /* disable DPLL_SEL */
  4241. temp = I915_READ(PCH_DPLL_SEL);
  4242. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4243. I915_WRITE(PCH_DPLL_SEL, temp);
  4244. }
  4245. ironlake_fdi_pll_disable(intel_crtc);
  4246. }
  4247. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4248. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4249. }
  4250. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4251. {
  4252. struct drm_device *dev = crtc->dev;
  4253. struct drm_i915_private *dev_priv = to_i915(dev);
  4254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4255. struct intel_encoder *encoder;
  4256. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4257. if (intel_crtc->config->has_pch_encoder)
  4258. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4259. false);
  4260. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4261. intel_opregion_notify_encoder(encoder, false);
  4262. encoder->disable(encoder);
  4263. }
  4264. drm_crtc_vblank_off(crtc);
  4265. assert_vblank_disabled(crtc);
  4266. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4267. if (!transcoder_is_dsi(cpu_transcoder))
  4268. intel_disable_pipe(intel_crtc);
  4269. if (intel_crtc->config->dp_encoder_is_mst)
  4270. intel_ddi_set_vc_payload_alloc(crtc, false);
  4271. if (!transcoder_is_dsi(cpu_transcoder))
  4272. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4273. if (INTEL_INFO(dev)->gen >= 9)
  4274. skylake_scaler_disable(intel_crtc);
  4275. else
  4276. ironlake_pfit_disable(intel_crtc, false);
  4277. if (!transcoder_is_dsi(cpu_transcoder))
  4278. intel_ddi_disable_pipe_clock(intel_crtc);
  4279. for_each_encoder_on_crtc(dev, crtc, encoder)
  4280. if (encoder->post_disable)
  4281. encoder->post_disable(encoder);
  4282. if (intel_crtc->config->has_pch_encoder) {
  4283. lpt_disable_pch_transcoder(dev_priv);
  4284. lpt_disable_iclkip(dev_priv);
  4285. intel_ddi_fdi_disable(crtc);
  4286. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4287. true);
  4288. }
  4289. }
  4290. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4291. {
  4292. struct drm_device *dev = crtc->base.dev;
  4293. struct drm_i915_private *dev_priv = to_i915(dev);
  4294. struct intel_crtc_state *pipe_config = crtc->config;
  4295. if (!pipe_config->gmch_pfit.control)
  4296. return;
  4297. /*
  4298. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4299. * according to register description and PRM.
  4300. */
  4301. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4302. assert_pipe_disabled(dev_priv, crtc->pipe);
  4303. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4304. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4305. /* Border color in case we don't scale up to the full screen. Black by
  4306. * default, change to something else for debugging. */
  4307. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4308. }
  4309. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4310. {
  4311. switch (port) {
  4312. case PORT_A:
  4313. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4314. case PORT_B:
  4315. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4316. case PORT_C:
  4317. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4318. case PORT_D:
  4319. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4320. case PORT_E:
  4321. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4322. default:
  4323. MISSING_CASE(port);
  4324. return POWER_DOMAIN_PORT_OTHER;
  4325. }
  4326. }
  4327. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4328. {
  4329. switch (port) {
  4330. case PORT_A:
  4331. return POWER_DOMAIN_AUX_A;
  4332. case PORT_B:
  4333. return POWER_DOMAIN_AUX_B;
  4334. case PORT_C:
  4335. return POWER_DOMAIN_AUX_C;
  4336. case PORT_D:
  4337. return POWER_DOMAIN_AUX_D;
  4338. case PORT_E:
  4339. /* FIXME: Check VBT for actual wiring of PORT E */
  4340. return POWER_DOMAIN_AUX_D;
  4341. default:
  4342. MISSING_CASE(port);
  4343. return POWER_DOMAIN_AUX_A;
  4344. }
  4345. }
  4346. enum intel_display_power_domain
  4347. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4348. {
  4349. struct drm_device *dev = intel_encoder->base.dev;
  4350. struct intel_digital_port *intel_dig_port;
  4351. switch (intel_encoder->type) {
  4352. case INTEL_OUTPUT_UNKNOWN:
  4353. /* Only DDI platforms should ever use this output type */
  4354. WARN_ON_ONCE(!HAS_DDI(dev));
  4355. case INTEL_OUTPUT_DP:
  4356. case INTEL_OUTPUT_HDMI:
  4357. case INTEL_OUTPUT_EDP:
  4358. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4359. return port_to_power_domain(intel_dig_port->port);
  4360. case INTEL_OUTPUT_DP_MST:
  4361. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4362. return port_to_power_domain(intel_dig_port->port);
  4363. case INTEL_OUTPUT_ANALOG:
  4364. return POWER_DOMAIN_PORT_CRT;
  4365. case INTEL_OUTPUT_DSI:
  4366. return POWER_DOMAIN_PORT_DSI;
  4367. default:
  4368. return POWER_DOMAIN_PORT_OTHER;
  4369. }
  4370. }
  4371. enum intel_display_power_domain
  4372. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4373. {
  4374. struct drm_device *dev = intel_encoder->base.dev;
  4375. struct intel_digital_port *intel_dig_port;
  4376. switch (intel_encoder->type) {
  4377. case INTEL_OUTPUT_UNKNOWN:
  4378. case INTEL_OUTPUT_HDMI:
  4379. /*
  4380. * Only DDI platforms should ever use these output types.
  4381. * We can get here after the HDMI detect code has already set
  4382. * the type of the shared encoder. Since we can't be sure
  4383. * what's the status of the given connectors, play safe and
  4384. * run the DP detection too.
  4385. */
  4386. WARN_ON_ONCE(!HAS_DDI(dev));
  4387. case INTEL_OUTPUT_DP:
  4388. case INTEL_OUTPUT_EDP:
  4389. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4390. return port_to_aux_power_domain(intel_dig_port->port);
  4391. case INTEL_OUTPUT_DP_MST:
  4392. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4393. return port_to_aux_power_domain(intel_dig_port->port);
  4394. default:
  4395. MISSING_CASE(intel_encoder->type);
  4396. return POWER_DOMAIN_AUX_A;
  4397. }
  4398. }
  4399. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4400. struct intel_crtc_state *crtc_state)
  4401. {
  4402. struct drm_device *dev = crtc->dev;
  4403. struct drm_encoder *encoder;
  4404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4405. enum pipe pipe = intel_crtc->pipe;
  4406. unsigned long mask;
  4407. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4408. if (!crtc_state->base.active)
  4409. return 0;
  4410. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4411. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4412. if (crtc_state->pch_pfit.enabled ||
  4413. crtc_state->pch_pfit.force_thru)
  4414. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4415. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4416. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4417. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4418. }
  4419. if (crtc_state->shared_dpll)
  4420. mask |= BIT(POWER_DOMAIN_PLLS);
  4421. return mask;
  4422. }
  4423. static unsigned long
  4424. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4425. struct intel_crtc_state *crtc_state)
  4426. {
  4427. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4429. enum intel_display_power_domain domain;
  4430. unsigned long domains, new_domains, old_domains;
  4431. old_domains = intel_crtc->enabled_power_domains;
  4432. intel_crtc->enabled_power_domains = new_domains =
  4433. get_crtc_power_domains(crtc, crtc_state);
  4434. domains = new_domains & ~old_domains;
  4435. for_each_power_domain(domain, domains)
  4436. intel_display_power_get(dev_priv, domain);
  4437. return old_domains & ~new_domains;
  4438. }
  4439. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4440. unsigned long domains)
  4441. {
  4442. enum intel_display_power_domain domain;
  4443. for_each_power_domain(domain, domains)
  4444. intel_display_power_put(dev_priv, domain);
  4445. }
  4446. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4447. {
  4448. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4449. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4450. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4451. return max_cdclk_freq;
  4452. else if (IS_CHERRYVIEW(dev_priv))
  4453. return max_cdclk_freq*95/100;
  4454. else if (INTEL_INFO(dev_priv)->gen < 4)
  4455. return 2*max_cdclk_freq*90/100;
  4456. else
  4457. return max_cdclk_freq*90/100;
  4458. }
  4459. static int skl_calc_cdclk(int max_pixclk, int vco);
  4460. static void intel_update_max_cdclk(struct drm_device *dev)
  4461. {
  4462. struct drm_i915_private *dev_priv = to_i915(dev);
  4463. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4464. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4465. int max_cdclk, vco;
  4466. vco = dev_priv->skl_preferred_vco_freq;
  4467. WARN_ON(vco != 8100000 && vco != 8640000);
  4468. /*
  4469. * Use the lower (vco 8640) cdclk values as a
  4470. * first guess. skl_calc_cdclk() will correct it
  4471. * if the preferred vco is 8100 instead.
  4472. */
  4473. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4474. max_cdclk = 617143;
  4475. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4476. max_cdclk = 540000;
  4477. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4478. max_cdclk = 432000;
  4479. else
  4480. max_cdclk = 308571;
  4481. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4482. } else if (IS_BROXTON(dev)) {
  4483. dev_priv->max_cdclk_freq = 624000;
  4484. } else if (IS_BROADWELL(dev)) {
  4485. /*
  4486. * FIXME with extra cooling we can allow
  4487. * 540 MHz for ULX and 675 Mhz for ULT.
  4488. * How can we know if extra cooling is
  4489. * available? PCI ID, VTB, something else?
  4490. */
  4491. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4492. dev_priv->max_cdclk_freq = 450000;
  4493. else if (IS_BDW_ULX(dev))
  4494. dev_priv->max_cdclk_freq = 450000;
  4495. else if (IS_BDW_ULT(dev))
  4496. dev_priv->max_cdclk_freq = 540000;
  4497. else
  4498. dev_priv->max_cdclk_freq = 675000;
  4499. } else if (IS_CHERRYVIEW(dev)) {
  4500. dev_priv->max_cdclk_freq = 320000;
  4501. } else if (IS_VALLEYVIEW(dev)) {
  4502. dev_priv->max_cdclk_freq = 400000;
  4503. } else {
  4504. /* otherwise assume cdclk is fixed */
  4505. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4506. }
  4507. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4508. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4509. dev_priv->max_cdclk_freq);
  4510. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4511. dev_priv->max_dotclk_freq);
  4512. }
  4513. static void intel_update_cdclk(struct drm_device *dev)
  4514. {
  4515. struct drm_i915_private *dev_priv = to_i915(dev);
  4516. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4517. if (INTEL_GEN(dev_priv) >= 9)
  4518. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4519. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4520. dev_priv->cdclk_pll.ref);
  4521. else
  4522. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4523. dev_priv->cdclk_freq);
  4524. /*
  4525. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4526. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4527. * of cdclk that generates 4MHz reference clock freq which is used to
  4528. * generate GMBus clock. This will vary with the cdclk freq.
  4529. */
  4530. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4531. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4532. }
  4533. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4534. static int skl_cdclk_decimal(int cdclk)
  4535. {
  4536. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4537. }
  4538. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4539. {
  4540. int ratio;
  4541. if (cdclk == dev_priv->cdclk_pll.ref)
  4542. return 0;
  4543. switch (cdclk) {
  4544. default:
  4545. MISSING_CASE(cdclk);
  4546. case 144000:
  4547. case 288000:
  4548. case 384000:
  4549. case 576000:
  4550. ratio = 60;
  4551. break;
  4552. case 624000:
  4553. ratio = 65;
  4554. break;
  4555. }
  4556. return dev_priv->cdclk_pll.ref * ratio;
  4557. }
  4558. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4559. {
  4560. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4561. /* Timeout 200us */
  4562. if (intel_wait_for_register(dev_priv,
  4563. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  4564. 1))
  4565. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4566. dev_priv->cdclk_pll.vco = 0;
  4567. }
  4568. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4569. {
  4570. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4571. u32 val;
  4572. val = I915_READ(BXT_DE_PLL_CTL);
  4573. val &= ~BXT_DE_PLL_RATIO_MASK;
  4574. val |= BXT_DE_PLL_RATIO(ratio);
  4575. I915_WRITE(BXT_DE_PLL_CTL, val);
  4576. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4577. /* Timeout 200us */
  4578. if (intel_wait_for_register(dev_priv,
  4579. BXT_DE_PLL_ENABLE,
  4580. BXT_DE_PLL_LOCK,
  4581. BXT_DE_PLL_LOCK,
  4582. 1))
  4583. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4584. dev_priv->cdclk_pll.vco = vco;
  4585. }
  4586. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4587. {
  4588. u32 val, divider;
  4589. int vco, ret;
  4590. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4591. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4592. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4593. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4594. case 8:
  4595. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4596. break;
  4597. case 4:
  4598. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4599. break;
  4600. case 3:
  4601. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4602. break;
  4603. case 2:
  4604. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4605. break;
  4606. default:
  4607. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4608. WARN_ON(vco != 0);
  4609. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4610. break;
  4611. }
  4612. /* Inform power controller of upcoming frequency change */
  4613. mutex_lock(&dev_priv->rps.hw_lock);
  4614. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4615. 0x80000000);
  4616. mutex_unlock(&dev_priv->rps.hw_lock);
  4617. if (ret) {
  4618. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4619. ret, cdclk);
  4620. return;
  4621. }
  4622. if (dev_priv->cdclk_pll.vco != 0 &&
  4623. dev_priv->cdclk_pll.vco != vco)
  4624. bxt_de_pll_disable(dev_priv);
  4625. if (dev_priv->cdclk_pll.vco != vco)
  4626. bxt_de_pll_enable(dev_priv, vco);
  4627. val = divider | skl_cdclk_decimal(cdclk);
  4628. /*
  4629. * FIXME if only the cd2x divider needs changing, it could be done
  4630. * without shutting off the pipe (if only one pipe is active).
  4631. */
  4632. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4633. /*
  4634. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4635. * enable otherwise.
  4636. */
  4637. if (cdclk >= 500000)
  4638. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4639. I915_WRITE(CDCLK_CTL, val);
  4640. mutex_lock(&dev_priv->rps.hw_lock);
  4641. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4642. DIV_ROUND_UP(cdclk, 25000));
  4643. mutex_unlock(&dev_priv->rps.hw_lock);
  4644. if (ret) {
  4645. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4646. ret, cdclk);
  4647. return;
  4648. }
  4649. intel_update_cdclk(&dev_priv->drm);
  4650. }
  4651. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4652. {
  4653. u32 cdctl, expected;
  4654. intel_update_cdclk(&dev_priv->drm);
  4655. if (dev_priv->cdclk_pll.vco == 0 ||
  4656. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4657. goto sanitize;
  4658. /* DPLL okay; verify the cdclock
  4659. *
  4660. * Some BIOS versions leave an incorrect decimal frequency value and
  4661. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4662. * so sanitize this register.
  4663. */
  4664. cdctl = I915_READ(CDCLK_CTL);
  4665. /*
  4666. * Let's ignore the pipe field, since BIOS could have configured the
  4667. * dividers both synching to an active pipe, or asynchronously
  4668. * (PIPE_NONE).
  4669. */
  4670. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4671. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4672. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4673. /*
  4674. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4675. * enable otherwise.
  4676. */
  4677. if (dev_priv->cdclk_freq >= 500000)
  4678. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4679. if (cdctl == expected)
  4680. /* All well; nothing to sanitize */
  4681. return;
  4682. sanitize:
  4683. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4684. /* force cdclk programming */
  4685. dev_priv->cdclk_freq = 0;
  4686. /* force full PLL disable + enable */
  4687. dev_priv->cdclk_pll.vco = -1;
  4688. }
  4689. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  4690. {
  4691. bxt_sanitize_cdclk(dev_priv);
  4692. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4693. return;
  4694. /*
  4695. * FIXME:
  4696. * - The initial CDCLK needs to be read from VBT.
  4697. * Need to make this change after VBT has changes for BXT.
  4698. */
  4699. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  4700. }
  4701. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  4702. {
  4703. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4704. }
  4705. static int skl_calc_cdclk(int max_pixclk, int vco)
  4706. {
  4707. if (vco == 8640000) {
  4708. if (max_pixclk > 540000)
  4709. return 617143;
  4710. else if (max_pixclk > 432000)
  4711. return 540000;
  4712. else if (max_pixclk > 308571)
  4713. return 432000;
  4714. else
  4715. return 308571;
  4716. } else {
  4717. if (max_pixclk > 540000)
  4718. return 675000;
  4719. else if (max_pixclk > 450000)
  4720. return 540000;
  4721. else if (max_pixclk > 337500)
  4722. return 450000;
  4723. else
  4724. return 337500;
  4725. }
  4726. }
  4727. static void
  4728. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4729. {
  4730. u32 val;
  4731. dev_priv->cdclk_pll.ref = 24000;
  4732. dev_priv->cdclk_pll.vco = 0;
  4733. val = I915_READ(LCPLL1_CTL);
  4734. if ((val & LCPLL_PLL_ENABLE) == 0)
  4735. return;
  4736. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4737. return;
  4738. val = I915_READ(DPLL_CTRL1);
  4739. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4740. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4741. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4742. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4743. return;
  4744. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4745. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4746. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4747. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4748. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4749. dev_priv->cdclk_pll.vco = 8100000;
  4750. break;
  4751. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4752. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4753. dev_priv->cdclk_pll.vco = 8640000;
  4754. break;
  4755. default:
  4756. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4757. break;
  4758. }
  4759. }
  4760. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4761. {
  4762. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4763. dev_priv->skl_preferred_vco_freq = vco;
  4764. if (changed)
  4765. intel_update_max_cdclk(&dev_priv->drm);
  4766. }
  4767. static void
  4768. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4769. {
  4770. int min_cdclk = skl_calc_cdclk(0, vco);
  4771. u32 val;
  4772. WARN_ON(vco != 8100000 && vco != 8640000);
  4773. /* select the minimum CDCLK before enabling DPLL 0 */
  4774. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4775. I915_WRITE(CDCLK_CTL, val);
  4776. POSTING_READ(CDCLK_CTL);
  4777. /*
  4778. * We always enable DPLL0 with the lowest link rate possible, but still
  4779. * taking into account the VCO required to operate the eDP panel at the
  4780. * desired frequency. The usual DP link rates operate with a VCO of
  4781. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4782. * The modeset code is responsible for the selection of the exact link
  4783. * rate later on, with the constraint of choosing a frequency that
  4784. * works with vco.
  4785. */
  4786. val = I915_READ(DPLL_CTRL1);
  4787. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4788. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4789. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4790. if (vco == 8640000)
  4791. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4792. SKL_DPLL0);
  4793. else
  4794. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4795. SKL_DPLL0);
  4796. I915_WRITE(DPLL_CTRL1, val);
  4797. POSTING_READ(DPLL_CTRL1);
  4798. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4799. if (intel_wait_for_register(dev_priv,
  4800. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  4801. 5))
  4802. DRM_ERROR("DPLL0 not locked\n");
  4803. dev_priv->cdclk_pll.vco = vco;
  4804. /* We'll want to keep using the current vco from now on. */
  4805. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4806. }
  4807. static void
  4808. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4809. {
  4810. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4811. if (intel_wait_for_register(dev_priv,
  4812. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  4813. 1))
  4814. DRM_ERROR("Couldn't disable DPLL0\n");
  4815. dev_priv->cdclk_pll.vco = 0;
  4816. }
  4817. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4818. {
  4819. int ret;
  4820. u32 val;
  4821. /* inform PCU we want to change CDCLK */
  4822. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4823. mutex_lock(&dev_priv->rps.hw_lock);
  4824. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4825. mutex_unlock(&dev_priv->rps.hw_lock);
  4826. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4827. }
  4828. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4829. {
  4830. unsigned int i;
  4831. for (i = 0; i < 15; i++) {
  4832. if (skl_cdclk_pcu_ready(dev_priv))
  4833. return true;
  4834. udelay(10);
  4835. }
  4836. return false;
  4837. }
  4838. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4839. {
  4840. struct drm_device *dev = &dev_priv->drm;
  4841. u32 freq_select, pcu_ack;
  4842. WARN_ON((cdclk == 24000) != (vco == 0));
  4843. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4844. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4845. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4846. return;
  4847. }
  4848. /* set CDCLK_CTL */
  4849. switch (cdclk) {
  4850. case 450000:
  4851. case 432000:
  4852. freq_select = CDCLK_FREQ_450_432;
  4853. pcu_ack = 1;
  4854. break;
  4855. case 540000:
  4856. freq_select = CDCLK_FREQ_540;
  4857. pcu_ack = 2;
  4858. break;
  4859. case 308571:
  4860. case 337500:
  4861. default:
  4862. freq_select = CDCLK_FREQ_337_308;
  4863. pcu_ack = 0;
  4864. break;
  4865. case 617143:
  4866. case 675000:
  4867. freq_select = CDCLK_FREQ_675_617;
  4868. pcu_ack = 3;
  4869. break;
  4870. }
  4871. if (dev_priv->cdclk_pll.vco != 0 &&
  4872. dev_priv->cdclk_pll.vco != vco)
  4873. skl_dpll0_disable(dev_priv);
  4874. if (dev_priv->cdclk_pll.vco != vco)
  4875. skl_dpll0_enable(dev_priv, vco);
  4876. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4877. POSTING_READ(CDCLK_CTL);
  4878. /* inform PCU of the change */
  4879. mutex_lock(&dev_priv->rps.hw_lock);
  4880. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4881. mutex_unlock(&dev_priv->rps.hw_lock);
  4882. intel_update_cdclk(dev);
  4883. }
  4884. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4885. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4886. {
  4887. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4888. }
  4889. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4890. {
  4891. int cdclk, vco;
  4892. skl_sanitize_cdclk(dev_priv);
  4893. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4894. /*
  4895. * Use the current vco as our initial
  4896. * guess as to what the preferred vco is.
  4897. */
  4898. if (dev_priv->skl_preferred_vco_freq == 0)
  4899. skl_set_preferred_cdclk_vco(dev_priv,
  4900. dev_priv->cdclk_pll.vco);
  4901. return;
  4902. }
  4903. vco = dev_priv->skl_preferred_vco_freq;
  4904. if (vco == 0)
  4905. vco = 8100000;
  4906. cdclk = skl_calc_cdclk(0, vco);
  4907. skl_set_cdclk(dev_priv, cdclk, vco);
  4908. }
  4909. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4910. {
  4911. uint32_t cdctl, expected;
  4912. /*
  4913. * check if the pre-os intialized the display
  4914. * There is SWF18 scratchpad register defined which is set by the
  4915. * pre-os which can be used by the OS drivers to check the status
  4916. */
  4917. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4918. goto sanitize;
  4919. intel_update_cdclk(&dev_priv->drm);
  4920. /* Is PLL enabled and locked ? */
  4921. if (dev_priv->cdclk_pll.vco == 0 ||
  4922. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4923. goto sanitize;
  4924. /* DPLL okay; verify the cdclock
  4925. *
  4926. * Noticed in some instances that the freq selection is correct but
  4927. * decimal part is programmed wrong from BIOS where pre-os does not
  4928. * enable display. Verify the same as well.
  4929. */
  4930. cdctl = I915_READ(CDCLK_CTL);
  4931. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4932. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4933. if (cdctl == expected)
  4934. /* All well; nothing to sanitize */
  4935. return;
  4936. sanitize:
  4937. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4938. /* force cdclk programming */
  4939. dev_priv->cdclk_freq = 0;
  4940. /* force full PLL disable + enable */
  4941. dev_priv->cdclk_pll.vco = -1;
  4942. }
  4943. /* Adjust CDclk dividers to allow high res or save power if possible */
  4944. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4945. {
  4946. struct drm_i915_private *dev_priv = to_i915(dev);
  4947. u32 val, cmd;
  4948. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4949. != dev_priv->cdclk_freq);
  4950. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4951. cmd = 2;
  4952. else if (cdclk == 266667)
  4953. cmd = 1;
  4954. else
  4955. cmd = 0;
  4956. mutex_lock(&dev_priv->rps.hw_lock);
  4957. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4958. val &= ~DSPFREQGUAR_MASK;
  4959. val |= (cmd << DSPFREQGUAR_SHIFT);
  4960. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4961. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4962. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4963. 50)) {
  4964. DRM_ERROR("timed out waiting for CDclk change\n");
  4965. }
  4966. mutex_unlock(&dev_priv->rps.hw_lock);
  4967. mutex_lock(&dev_priv->sb_lock);
  4968. if (cdclk == 400000) {
  4969. u32 divider;
  4970. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4971. /* adjust cdclk divider */
  4972. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4973. val &= ~CCK_FREQUENCY_VALUES;
  4974. val |= divider;
  4975. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4976. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4977. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4978. 50))
  4979. DRM_ERROR("timed out waiting for CDclk change\n");
  4980. }
  4981. /* adjust self-refresh exit latency value */
  4982. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4983. val &= ~0x7f;
  4984. /*
  4985. * For high bandwidth configs, we set a higher latency in the bunit
  4986. * so that the core display fetch happens in time to avoid underruns.
  4987. */
  4988. if (cdclk == 400000)
  4989. val |= 4500 / 250; /* 4.5 usec */
  4990. else
  4991. val |= 3000 / 250; /* 3.0 usec */
  4992. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4993. mutex_unlock(&dev_priv->sb_lock);
  4994. intel_update_cdclk(dev);
  4995. }
  4996. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4997. {
  4998. struct drm_i915_private *dev_priv = to_i915(dev);
  4999. u32 val, cmd;
  5000. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5001. != dev_priv->cdclk_freq);
  5002. switch (cdclk) {
  5003. case 333333:
  5004. case 320000:
  5005. case 266667:
  5006. case 200000:
  5007. break;
  5008. default:
  5009. MISSING_CASE(cdclk);
  5010. return;
  5011. }
  5012. /*
  5013. * Specs are full of misinformation, but testing on actual
  5014. * hardware has shown that we just need to write the desired
  5015. * CCK divider into the Punit register.
  5016. */
  5017. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5018. mutex_lock(&dev_priv->rps.hw_lock);
  5019. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5020. val &= ~DSPFREQGUAR_MASK_CHV;
  5021. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5022. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5023. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5024. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5025. 50)) {
  5026. DRM_ERROR("timed out waiting for CDclk change\n");
  5027. }
  5028. mutex_unlock(&dev_priv->rps.hw_lock);
  5029. intel_update_cdclk(dev);
  5030. }
  5031. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5032. int max_pixclk)
  5033. {
  5034. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5035. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5036. /*
  5037. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5038. * 200MHz
  5039. * 267MHz
  5040. * 320/333MHz (depends on HPLL freq)
  5041. * 400MHz (VLV only)
  5042. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5043. * of the lower bin and adjust if needed.
  5044. *
  5045. * We seem to get an unstable or solid color picture at 200MHz.
  5046. * Not sure what's wrong. For now use 200MHz only when all pipes
  5047. * are off.
  5048. */
  5049. if (!IS_CHERRYVIEW(dev_priv) &&
  5050. max_pixclk > freq_320*limit/100)
  5051. return 400000;
  5052. else if (max_pixclk > 266667*limit/100)
  5053. return freq_320;
  5054. else if (max_pixclk > 0)
  5055. return 266667;
  5056. else
  5057. return 200000;
  5058. }
  5059. static int bxt_calc_cdclk(int max_pixclk)
  5060. {
  5061. if (max_pixclk > 576000)
  5062. return 624000;
  5063. else if (max_pixclk > 384000)
  5064. return 576000;
  5065. else if (max_pixclk > 288000)
  5066. return 384000;
  5067. else if (max_pixclk > 144000)
  5068. return 288000;
  5069. else
  5070. return 144000;
  5071. }
  5072. /* Compute the max pixel clock for new configuration. */
  5073. static int intel_mode_max_pixclk(struct drm_device *dev,
  5074. struct drm_atomic_state *state)
  5075. {
  5076. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5077. struct drm_i915_private *dev_priv = to_i915(dev);
  5078. struct drm_crtc *crtc;
  5079. struct drm_crtc_state *crtc_state;
  5080. unsigned max_pixclk = 0, i;
  5081. enum pipe pipe;
  5082. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5083. sizeof(intel_state->min_pixclk));
  5084. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5085. int pixclk = 0;
  5086. if (crtc_state->enable)
  5087. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5088. intel_state->min_pixclk[i] = pixclk;
  5089. }
  5090. for_each_pipe(dev_priv, pipe)
  5091. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5092. return max_pixclk;
  5093. }
  5094. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5095. {
  5096. struct drm_device *dev = state->dev;
  5097. struct drm_i915_private *dev_priv = to_i915(dev);
  5098. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5099. struct intel_atomic_state *intel_state =
  5100. to_intel_atomic_state(state);
  5101. intel_state->cdclk = intel_state->dev_cdclk =
  5102. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5103. if (!intel_state->active_crtcs)
  5104. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5105. return 0;
  5106. }
  5107. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5108. {
  5109. int max_pixclk = ilk_max_pixel_rate(state);
  5110. struct intel_atomic_state *intel_state =
  5111. to_intel_atomic_state(state);
  5112. intel_state->cdclk = intel_state->dev_cdclk =
  5113. bxt_calc_cdclk(max_pixclk);
  5114. if (!intel_state->active_crtcs)
  5115. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5116. return 0;
  5117. }
  5118. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5119. {
  5120. unsigned int credits, default_credits;
  5121. if (IS_CHERRYVIEW(dev_priv))
  5122. default_credits = PFI_CREDIT(12);
  5123. else
  5124. default_credits = PFI_CREDIT(8);
  5125. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5126. /* CHV suggested value is 31 or 63 */
  5127. if (IS_CHERRYVIEW(dev_priv))
  5128. credits = PFI_CREDIT_63;
  5129. else
  5130. credits = PFI_CREDIT(15);
  5131. } else {
  5132. credits = default_credits;
  5133. }
  5134. /*
  5135. * WA - write default credits before re-programming
  5136. * FIXME: should we also set the resend bit here?
  5137. */
  5138. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5139. default_credits);
  5140. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5141. credits | PFI_CREDIT_RESEND);
  5142. /*
  5143. * FIXME is this guaranteed to clear
  5144. * immediately or should we poll for it?
  5145. */
  5146. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5147. }
  5148. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5149. {
  5150. struct drm_device *dev = old_state->dev;
  5151. struct drm_i915_private *dev_priv = to_i915(dev);
  5152. struct intel_atomic_state *old_intel_state =
  5153. to_intel_atomic_state(old_state);
  5154. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5155. /*
  5156. * FIXME: We can end up here with all power domains off, yet
  5157. * with a CDCLK frequency other than the minimum. To account
  5158. * for this take the PIPE-A power domain, which covers the HW
  5159. * blocks needed for the following programming. This can be
  5160. * removed once it's guaranteed that we get here either with
  5161. * the minimum CDCLK set, or the required power domains
  5162. * enabled.
  5163. */
  5164. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5165. if (IS_CHERRYVIEW(dev))
  5166. cherryview_set_cdclk(dev, req_cdclk);
  5167. else
  5168. valleyview_set_cdclk(dev, req_cdclk);
  5169. vlv_program_pfi_credits(dev_priv);
  5170. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5171. }
  5172. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5173. {
  5174. struct drm_device *dev = crtc->dev;
  5175. struct drm_i915_private *dev_priv = to_i915(dev);
  5176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5177. struct intel_encoder *encoder;
  5178. struct intel_crtc_state *pipe_config =
  5179. to_intel_crtc_state(crtc->state);
  5180. int pipe = intel_crtc->pipe;
  5181. if (WARN_ON(intel_crtc->active))
  5182. return;
  5183. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5184. intel_dp_set_m_n(intel_crtc, M1_N1);
  5185. intel_set_pipe_timings(intel_crtc);
  5186. intel_set_pipe_src_size(intel_crtc);
  5187. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5188. struct drm_i915_private *dev_priv = to_i915(dev);
  5189. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5190. I915_WRITE(CHV_CANVAS(pipe), 0);
  5191. }
  5192. i9xx_set_pipeconf(intel_crtc);
  5193. intel_crtc->active = true;
  5194. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5195. for_each_encoder_on_crtc(dev, crtc, encoder)
  5196. if (encoder->pre_pll_enable)
  5197. encoder->pre_pll_enable(encoder);
  5198. if (IS_CHERRYVIEW(dev)) {
  5199. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5200. chv_enable_pll(intel_crtc, intel_crtc->config);
  5201. } else {
  5202. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5203. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5204. }
  5205. for_each_encoder_on_crtc(dev, crtc, encoder)
  5206. if (encoder->pre_enable)
  5207. encoder->pre_enable(encoder);
  5208. i9xx_pfit_enable(intel_crtc);
  5209. intel_color_load_luts(&pipe_config->base);
  5210. intel_update_watermarks(crtc);
  5211. intel_enable_pipe(intel_crtc);
  5212. assert_vblank_disabled(crtc);
  5213. drm_crtc_vblank_on(crtc);
  5214. for_each_encoder_on_crtc(dev, crtc, encoder)
  5215. encoder->enable(encoder);
  5216. }
  5217. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5218. {
  5219. struct drm_device *dev = crtc->base.dev;
  5220. struct drm_i915_private *dev_priv = to_i915(dev);
  5221. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5222. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5223. }
  5224. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5225. {
  5226. struct drm_device *dev = crtc->dev;
  5227. struct drm_i915_private *dev_priv = to_i915(dev);
  5228. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5229. struct intel_encoder *encoder;
  5230. struct intel_crtc_state *pipe_config =
  5231. to_intel_crtc_state(crtc->state);
  5232. enum pipe pipe = intel_crtc->pipe;
  5233. if (WARN_ON(intel_crtc->active))
  5234. return;
  5235. i9xx_set_pll_dividers(intel_crtc);
  5236. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5237. intel_dp_set_m_n(intel_crtc, M1_N1);
  5238. intel_set_pipe_timings(intel_crtc);
  5239. intel_set_pipe_src_size(intel_crtc);
  5240. i9xx_set_pipeconf(intel_crtc);
  5241. intel_crtc->active = true;
  5242. if (!IS_GEN2(dev))
  5243. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5244. for_each_encoder_on_crtc(dev, crtc, encoder)
  5245. if (encoder->pre_enable)
  5246. encoder->pre_enable(encoder);
  5247. i9xx_enable_pll(intel_crtc);
  5248. i9xx_pfit_enable(intel_crtc);
  5249. intel_color_load_luts(&pipe_config->base);
  5250. intel_update_watermarks(crtc);
  5251. intel_enable_pipe(intel_crtc);
  5252. assert_vblank_disabled(crtc);
  5253. drm_crtc_vblank_on(crtc);
  5254. for_each_encoder_on_crtc(dev, crtc, encoder)
  5255. encoder->enable(encoder);
  5256. }
  5257. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5258. {
  5259. struct drm_device *dev = crtc->base.dev;
  5260. struct drm_i915_private *dev_priv = to_i915(dev);
  5261. if (!crtc->config->gmch_pfit.control)
  5262. return;
  5263. assert_pipe_disabled(dev_priv, crtc->pipe);
  5264. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5265. I915_READ(PFIT_CONTROL));
  5266. I915_WRITE(PFIT_CONTROL, 0);
  5267. }
  5268. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5269. {
  5270. struct drm_device *dev = crtc->dev;
  5271. struct drm_i915_private *dev_priv = to_i915(dev);
  5272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5273. struct intel_encoder *encoder;
  5274. int pipe = intel_crtc->pipe;
  5275. /*
  5276. * On gen2 planes are double buffered but the pipe isn't, so we must
  5277. * wait for planes to fully turn off before disabling the pipe.
  5278. */
  5279. if (IS_GEN2(dev))
  5280. intel_wait_for_vblank(dev, pipe);
  5281. for_each_encoder_on_crtc(dev, crtc, encoder)
  5282. encoder->disable(encoder);
  5283. drm_crtc_vblank_off(crtc);
  5284. assert_vblank_disabled(crtc);
  5285. intel_disable_pipe(intel_crtc);
  5286. i9xx_pfit_disable(intel_crtc);
  5287. for_each_encoder_on_crtc(dev, crtc, encoder)
  5288. if (encoder->post_disable)
  5289. encoder->post_disable(encoder);
  5290. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5291. if (IS_CHERRYVIEW(dev))
  5292. chv_disable_pll(dev_priv, pipe);
  5293. else if (IS_VALLEYVIEW(dev))
  5294. vlv_disable_pll(dev_priv, pipe);
  5295. else
  5296. i9xx_disable_pll(intel_crtc);
  5297. }
  5298. for_each_encoder_on_crtc(dev, crtc, encoder)
  5299. if (encoder->post_pll_disable)
  5300. encoder->post_pll_disable(encoder);
  5301. if (!IS_GEN2(dev))
  5302. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5303. }
  5304. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5305. {
  5306. struct intel_encoder *encoder;
  5307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5308. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5309. enum intel_display_power_domain domain;
  5310. unsigned long domains;
  5311. if (!intel_crtc->active)
  5312. return;
  5313. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5314. WARN_ON(intel_crtc->flip_work);
  5315. intel_pre_disable_primary_noatomic(crtc);
  5316. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5317. to_intel_plane_state(crtc->primary->state)->visible = false;
  5318. }
  5319. dev_priv->display.crtc_disable(crtc);
  5320. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5321. crtc->base.id, crtc->name);
  5322. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5323. crtc->state->active = false;
  5324. intel_crtc->active = false;
  5325. crtc->enabled = false;
  5326. crtc->state->connector_mask = 0;
  5327. crtc->state->encoder_mask = 0;
  5328. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5329. encoder->base.crtc = NULL;
  5330. intel_fbc_disable(intel_crtc);
  5331. intel_update_watermarks(crtc);
  5332. intel_disable_shared_dpll(intel_crtc);
  5333. domains = intel_crtc->enabled_power_domains;
  5334. for_each_power_domain(domain, domains)
  5335. intel_display_power_put(dev_priv, domain);
  5336. intel_crtc->enabled_power_domains = 0;
  5337. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5338. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5339. }
  5340. /*
  5341. * turn all crtc's off, but do not adjust state
  5342. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5343. */
  5344. int intel_display_suspend(struct drm_device *dev)
  5345. {
  5346. struct drm_i915_private *dev_priv = to_i915(dev);
  5347. struct drm_atomic_state *state;
  5348. int ret;
  5349. state = drm_atomic_helper_suspend(dev);
  5350. ret = PTR_ERR_OR_ZERO(state);
  5351. if (ret)
  5352. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5353. else
  5354. dev_priv->modeset_restore_state = state;
  5355. return ret;
  5356. }
  5357. void intel_encoder_destroy(struct drm_encoder *encoder)
  5358. {
  5359. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5360. drm_encoder_cleanup(encoder);
  5361. kfree(intel_encoder);
  5362. }
  5363. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5364. * internal consistency). */
  5365. static void intel_connector_verify_state(struct intel_connector *connector)
  5366. {
  5367. struct drm_crtc *crtc = connector->base.state->crtc;
  5368. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5369. connector->base.base.id,
  5370. connector->base.name);
  5371. if (connector->get_hw_state(connector)) {
  5372. struct intel_encoder *encoder = connector->encoder;
  5373. struct drm_connector_state *conn_state = connector->base.state;
  5374. I915_STATE_WARN(!crtc,
  5375. "connector enabled without attached crtc\n");
  5376. if (!crtc)
  5377. return;
  5378. I915_STATE_WARN(!crtc->state->active,
  5379. "connector is active, but attached crtc isn't\n");
  5380. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5381. return;
  5382. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5383. "atomic encoder doesn't match attached encoder\n");
  5384. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5385. "attached encoder crtc differs from connector crtc\n");
  5386. } else {
  5387. I915_STATE_WARN(crtc && crtc->state->active,
  5388. "attached crtc is active, but connector isn't\n");
  5389. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5390. "best encoder set without crtc!\n");
  5391. }
  5392. }
  5393. int intel_connector_init(struct intel_connector *connector)
  5394. {
  5395. drm_atomic_helper_connector_reset(&connector->base);
  5396. if (!connector->base.state)
  5397. return -ENOMEM;
  5398. return 0;
  5399. }
  5400. struct intel_connector *intel_connector_alloc(void)
  5401. {
  5402. struct intel_connector *connector;
  5403. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5404. if (!connector)
  5405. return NULL;
  5406. if (intel_connector_init(connector) < 0) {
  5407. kfree(connector);
  5408. return NULL;
  5409. }
  5410. return connector;
  5411. }
  5412. /* Simple connector->get_hw_state implementation for encoders that support only
  5413. * one connector and no cloning and hence the encoder state determines the state
  5414. * of the connector. */
  5415. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5416. {
  5417. enum pipe pipe = 0;
  5418. struct intel_encoder *encoder = connector->encoder;
  5419. return encoder->get_hw_state(encoder, &pipe);
  5420. }
  5421. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5422. {
  5423. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5424. return crtc_state->fdi_lanes;
  5425. return 0;
  5426. }
  5427. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5428. struct intel_crtc_state *pipe_config)
  5429. {
  5430. struct drm_atomic_state *state = pipe_config->base.state;
  5431. struct intel_crtc *other_crtc;
  5432. struct intel_crtc_state *other_crtc_state;
  5433. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5434. pipe_name(pipe), pipe_config->fdi_lanes);
  5435. if (pipe_config->fdi_lanes > 4) {
  5436. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5437. pipe_name(pipe), pipe_config->fdi_lanes);
  5438. return -EINVAL;
  5439. }
  5440. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5441. if (pipe_config->fdi_lanes > 2) {
  5442. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5443. pipe_config->fdi_lanes);
  5444. return -EINVAL;
  5445. } else {
  5446. return 0;
  5447. }
  5448. }
  5449. if (INTEL_INFO(dev)->num_pipes == 2)
  5450. return 0;
  5451. /* Ivybridge 3 pipe is really complicated */
  5452. switch (pipe) {
  5453. case PIPE_A:
  5454. return 0;
  5455. case PIPE_B:
  5456. if (pipe_config->fdi_lanes <= 2)
  5457. return 0;
  5458. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5459. other_crtc_state =
  5460. intel_atomic_get_crtc_state(state, other_crtc);
  5461. if (IS_ERR(other_crtc_state))
  5462. return PTR_ERR(other_crtc_state);
  5463. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5464. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5465. pipe_name(pipe), pipe_config->fdi_lanes);
  5466. return -EINVAL;
  5467. }
  5468. return 0;
  5469. case PIPE_C:
  5470. if (pipe_config->fdi_lanes > 2) {
  5471. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5472. pipe_name(pipe), pipe_config->fdi_lanes);
  5473. return -EINVAL;
  5474. }
  5475. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5476. other_crtc_state =
  5477. intel_atomic_get_crtc_state(state, other_crtc);
  5478. if (IS_ERR(other_crtc_state))
  5479. return PTR_ERR(other_crtc_state);
  5480. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5481. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5482. return -EINVAL;
  5483. }
  5484. return 0;
  5485. default:
  5486. BUG();
  5487. }
  5488. }
  5489. #define RETRY 1
  5490. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5491. struct intel_crtc_state *pipe_config)
  5492. {
  5493. struct drm_device *dev = intel_crtc->base.dev;
  5494. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5495. int lane, link_bw, fdi_dotclock, ret;
  5496. bool needs_recompute = false;
  5497. retry:
  5498. /* FDI is a binary signal running at ~2.7GHz, encoding
  5499. * each output octet as 10 bits. The actual frequency
  5500. * is stored as a divider into a 100MHz clock, and the
  5501. * mode pixel clock is stored in units of 1KHz.
  5502. * Hence the bw of each lane in terms of the mode signal
  5503. * is:
  5504. */
  5505. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5506. fdi_dotclock = adjusted_mode->crtc_clock;
  5507. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5508. pipe_config->pipe_bpp);
  5509. pipe_config->fdi_lanes = lane;
  5510. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5511. link_bw, &pipe_config->fdi_m_n);
  5512. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5513. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5514. pipe_config->pipe_bpp -= 2*3;
  5515. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5516. pipe_config->pipe_bpp);
  5517. needs_recompute = true;
  5518. pipe_config->bw_constrained = true;
  5519. goto retry;
  5520. }
  5521. if (needs_recompute)
  5522. return RETRY;
  5523. return ret;
  5524. }
  5525. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5526. struct intel_crtc_state *pipe_config)
  5527. {
  5528. if (pipe_config->pipe_bpp > 24)
  5529. return false;
  5530. /* HSW can handle pixel rate up to cdclk? */
  5531. if (IS_HASWELL(dev_priv))
  5532. return true;
  5533. /*
  5534. * We compare against max which means we must take
  5535. * the increased cdclk requirement into account when
  5536. * calculating the new cdclk.
  5537. *
  5538. * Should measure whether using a lower cdclk w/o IPS
  5539. */
  5540. return ilk_pipe_pixel_rate(pipe_config) <=
  5541. dev_priv->max_cdclk_freq * 95 / 100;
  5542. }
  5543. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5544. struct intel_crtc_state *pipe_config)
  5545. {
  5546. struct drm_device *dev = crtc->base.dev;
  5547. struct drm_i915_private *dev_priv = to_i915(dev);
  5548. pipe_config->ips_enabled = i915.enable_ips &&
  5549. hsw_crtc_supports_ips(crtc) &&
  5550. pipe_config_supports_ips(dev_priv, pipe_config);
  5551. }
  5552. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5553. {
  5554. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5555. /* GDG double wide on either pipe, otherwise pipe A only */
  5556. return INTEL_INFO(dev_priv)->gen < 4 &&
  5557. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5558. }
  5559. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5560. struct intel_crtc_state *pipe_config)
  5561. {
  5562. struct drm_device *dev = crtc->base.dev;
  5563. struct drm_i915_private *dev_priv = to_i915(dev);
  5564. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5565. int clock_limit = dev_priv->max_dotclk_freq;
  5566. if (INTEL_INFO(dev)->gen < 4) {
  5567. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5568. /*
  5569. * Enable double wide mode when the dot clock
  5570. * is > 90% of the (display) core speed.
  5571. */
  5572. if (intel_crtc_supports_double_wide(crtc) &&
  5573. adjusted_mode->crtc_clock > clock_limit) {
  5574. clock_limit = dev_priv->max_dotclk_freq;
  5575. pipe_config->double_wide = true;
  5576. }
  5577. }
  5578. if (adjusted_mode->crtc_clock > clock_limit) {
  5579. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5580. adjusted_mode->crtc_clock, clock_limit,
  5581. yesno(pipe_config->double_wide));
  5582. return -EINVAL;
  5583. }
  5584. /*
  5585. * Pipe horizontal size must be even in:
  5586. * - DVO ganged mode
  5587. * - LVDS dual channel mode
  5588. * - Double wide pipe
  5589. */
  5590. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5591. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5592. pipe_config->pipe_src_w &= ~1;
  5593. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5594. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5595. */
  5596. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5597. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5598. return -EINVAL;
  5599. if (HAS_IPS(dev))
  5600. hsw_compute_ips_config(crtc, pipe_config);
  5601. if (pipe_config->has_pch_encoder)
  5602. return ironlake_fdi_compute_config(crtc, pipe_config);
  5603. return 0;
  5604. }
  5605. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5606. {
  5607. struct drm_i915_private *dev_priv = to_i915(dev);
  5608. uint32_t cdctl;
  5609. skl_dpll0_update(dev_priv);
  5610. if (dev_priv->cdclk_pll.vco == 0)
  5611. return dev_priv->cdclk_pll.ref;
  5612. cdctl = I915_READ(CDCLK_CTL);
  5613. if (dev_priv->cdclk_pll.vco == 8640000) {
  5614. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5615. case CDCLK_FREQ_450_432:
  5616. return 432000;
  5617. case CDCLK_FREQ_337_308:
  5618. return 308571;
  5619. case CDCLK_FREQ_540:
  5620. return 540000;
  5621. case CDCLK_FREQ_675_617:
  5622. return 617143;
  5623. default:
  5624. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5625. }
  5626. } else {
  5627. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5628. case CDCLK_FREQ_450_432:
  5629. return 450000;
  5630. case CDCLK_FREQ_337_308:
  5631. return 337500;
  5632. case CDCLK_FREQ_540:
  5633. return 540000;
  5634. case CDCLK_FREQ_675_617:
  5635. return 675000;
  5636. default:
  5637. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5638. }
  5639. }
  5640. return dev_priv->cdclk_pll.ref;
  5641. }
  5642. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5643. {
  5644. u32 val;
  5645. dev_priv->cdclk_pll.ref = 19200;
  5646. dev_priv->cdclk_pll.vco = 0;
  5647. val = I915_READ(BXT_DE_PLL_ENABLE);
  5648. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5649. return;
  5650. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5651. return;
  5652. val = I915_READ(BXT_DE_PLL_CTL);
  5653. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5654. dev_priv->cdclk_pll.ref;
  5655. }
  5656. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5657. {
  5658. struct drm_i915_private *dev_priv = to_i915(dev);
  5659. u32 divider;
  5660. int div, vco;
  5661. bxt_de_pll_update(dev_priv);
  5662. vco = dev_priv->cdclk_pll.vco;
  5663. if (vco == 0)
  5664. return dev_priv->cdclk_pll.ref;
  5665. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5666. switch (divider) {
  5667. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5668. div = 2;
  5669. break;
  5670. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5671. div = 3;
  5672. break;
  5673. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5674. div = 4;
  5675. break;
  5676. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5677. div = 8;
  5678. break;
  5679. default:
  5680. MISSING_CASE(divider);
  5681. return dev_priv->cdclk_pll.ref;
  5682. }
  5683. return DIV_ROUND_CLOSEST(vco, div);
  5684. }
  5685. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5686. {
  5687. struct drm_i915_private *dev_priv = to_i915(dev);
  5688. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5689. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5690. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5691. return 800000;
  5692. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5693. return 450000;
  5694. else if (freq == LCPLL_CLK_FREQ_450)
  5695. return 450000;
  5696. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5697. return 540000;
  5698. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5699. return 337500;
  5700. else
  5701. return 675000;
  5702. }
  5703. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5704. {
  5705. struct drm_i915_private *dev_priv = to_i915(dev);
  5706. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5707. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5708. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5709. return 800000;
  5710. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5711. return 450000;
  5712. else if (freq == LCPLL_CLK_FREQ_450)
  5713. return 450000;
  5714. else if (IS_HSW_ULT(dev))
  5715. return 337500;
  5716. else
  5717. return 540000;
  5718. }
  5719. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5720. {
  5721. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5722. CCK_DISPLAY_CLOCK_CONTROL);
  5723. }
  5724. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5725. {
  5726. return 450000;
  5727. }
  5728. static int i945_get_display_clock_speed(struct drm_device *dev)
  5729. {
  5730. return 400000;
  5731. }
  5732. static int i915_get_display_clock_speed(struct drm_device *dev)
  5733. {
  5734. return 333333;
  5735. }
  5736. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5737. {
  5738. return 200000;
  5739. }
  5740. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5741. {
  5742. u16 gcfgc = 0;
  5743. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5744. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5745. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5746. return 266667;
  5747. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5748. return 333333;
  5749. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5750. return 444444;
  5751. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5752. return 200000;
  5753. default:
  5754. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5755. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5756. return 133333;
  5757. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5758. return 166667;
  5759. }
  5760. }
  5761. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5762. {
  5763. u16 gcfgc = 0;
  5764. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5765. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5766. return 133333;
  5767. else {
  5768. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5769. case GC_DISPLAY_CLOCK_333_MHZ:
  5770. return 333333;
  5771. default:
  5772. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5773. return 190000;
  5774. }
  5775. }
  5776. }
  5777. static int i865_get_display_clock_speed(struct drm_device *dev)
  5778. {
  5779. return 266667;
  5780. }
  5781. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5782. {
  5783. u16 hpllcc = 0;
  5784. /*
  5785. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5786. * encoding is different :(
  5787. * FIXME is this the right way to detect 852GM/852GMV?
  5788. */
  5789. if (dev->pdev->revision == 0x1)
  5790. return 133333;
  5791. pci_bus_read_config_word(dev->pdev->bus,
  5792. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5793. /* Assume that the hardware is in the high speed state. This
  5794. * should be the default.
  5795. */
  5796. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5797. case GC_CLOCK_133_200:
  5798. case GC_CLOCK_133_200_2:
  5799. case GC_CLOCK_100_200:
  5800. return 200000;
  5801. case GC_CLOCK_166_250:
  5802. return 250000;
  5803. case GC_CLOCK_100_133:
  5804. return 133333;
  5805. case GC_CLOCK_133_266:
  5806. case GC_CLOCK_133_266_2:
  5807. case GC_CLOCK_166_266:
  5808. return 266667;
  5809. }
  5810. /* Shouldn't happen */
  5811. return 0;
  5812. }
  5813. static int i830_get_display_clock_speed(struct drm_device *dev)
  5814. {
  5815. return 133333;
  5816. }
  5817. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5818. {
  5819. struct drm_i915_private *dev_priv = to_i915(dev);
  5820. static const unsigned int blb_vco[8] = {
  5821. [0] = 3200000,
  5822. [1] = 4000000,
  5823. [2] = 5333333,
  5824. [3] = 4800000,
  5825. [4] = 6400000,
  5826. };
  5827. static const unsigned int pnv_vco[8] = {
  5828. [0] = 3200000,
  5829. [1] = 4000000,
  5830. [2] = 5333333,
  5831. [3] = 4800000,
  5832. [4] = 2666667,
  5833. };
  5834. static const unsigned int cl_vco[8] = {
  5835. [0] = 3200000,
  5836. [1] = 4000000,
  5837. [2] = 5333333,
  5838. [3] = 6400000,
  5839. [4] = 3333333,
  5840. [5] = 3566667,
  5841. [6] = 4266667,
  5842. };
  5843. static const unsigned int elk_vco[8] = {
  5844. [0] = 3200000,
  5845. [1] = 4000000,
  5846. [2] = 5333333,
  5847. [3] = 4800000,
  5848. };
  5849. static const unsigned int ctg_vco[8] = {
  5850. [0] = 3200000,
  5851. [1] = 4000000,
  5852. [2] = 5333333,
  5853. [3] = 6400000,
  5854. [4] = 2666667,
  5855. [5] = 4266667,
  5856. };
  5857. const unsigned int *vco_table;
  5858. unsigned int vco;
  5859. uint8_t tmp = 0;
  5860. /* FIXME other chipsets? */
  5861. if (IS_GM45(dev))
  5862. vco_table = ctg_vco;
  5863. else if (IS_G4X(dev))
  5864. vco_table = elk_vco;
  5865. else if (IS_CRESTLINE(dev))
  5866. vco_table = cl_vco;
  5867. else if (IS_PINEVIEW(dev))
  5868. vco_table = pnv_vco;
  5869. else if (IS_G33(dev))
  5870. vco_table = blb_vco;
  5871. else
  5872. return 0;
  5873. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5874. vco = vco_table[tmp & 0x7];
  5875. if (vco == 0)
  5876. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5877. else
  5878. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5879. return vco;
  5880. }
  5881. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5882. {
  5883. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5884. uint16_t tmp = 0;
  5885. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5886. cdclk_sel = (tmp >> 12) & 0x1;
  5887. switch (vco) {
  5888. case 2666667:
  5889. case 4000000:
  5890. case 5333333:
  5891. return cdclk_sel ? 333333 : 222222;
  5892. case 3200000:
  5893. return cdclk_sel ? 320000 : 228571;
  5894. default:
  5895. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5896. return 222222;
  5897. }
  5898. }
  5899. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5900. {
  5901. static const uint8_t div_3200[] = { 16, 10, 8 };
  5902. static const uint8_t div_4000[] = { 20, 12, 10 };
  5903. static const uint8_t div_5333[] = { 24, 16, 14 };
  5904. const uint8_t *div_table;
  5905. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5906. uint16_t tmp = 0;
  5907. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5908. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5909. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5910. goto fail;
  5911. switch (vco) {
  5912. case 3200000:
  5913. div_table = div_3200;
  5914. break;
  5915. case 4000000:
  5916. div_table = div_4000;
  5917. break;
  5918. case 5333333:
  5919. div_table = div_5333;
  5920. break;
  5921. default:
  5922. goto fail;
  5923. }
  5924. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5925. fail:
  5926. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5927. return 200000;
  5928. }
  5929. static int g33_get_display_clock_speed(struct drm_device *dev)
  5930. {
  5931. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5932. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5933. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5934. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5935. const uint8_t *div_table;
  5936. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5937. uint16_t tmp = 0;
  5938. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5939. cdclk_sel = (tmp >> 4) & 0x7;
  5940. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5941. goto fail;
  5942. switch (vco) {
  5943. case 3200000:
  5944. div_table = div_3200;
  5945. break;
  5946. case 4000000:
  5947. div_table = div_4000;
  5948. break;
  5949. case 4800000:
  5950. div_table = div_4800;
  5951. break;
  5952. case 5333333:
  5953. div_table = div_5333;
  5954. break;
  5955. default:
  5956. goto fail;
  5957. }
  5958. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5959. fail:
  5960. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5961. return 190476;
  5962. }
  5963. static void
  5964. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5965. {
  5966. while (*num > DATA_LINK_M_N_MASK ||
  5967. *den > DATA_LINK_M_N_MASK) {
  5968. *num >>= 1;
  5969. *den >>= 1;
  5970. }
  5971. }
  5972. static void compute_m_n(unsigned int m, unsigned int n,
  5973. uint32_t *ret_m, uint32_t *ret_n)
  5974. {
  5975. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5976. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5977. intel_reduce_m_n_ratio(ret_m, ret_n);
  5978. }
  5979. void
  5980. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5981. int pixel_clock, int link_clock,
  5982. struct intel_link_m_n *m_n)
  5983. {
  5984. m_n->tu = 64;
  5985. compute_m_n(bits_per_pixel * pixel_clock,
  5986. link_clock * nlanes * 8,
  5987. &m_n->gmch_m, &m_n->gmch_n);
  5988. compute_m_n(pixel_clock, link_clock,
  5989. &m_n->link_m, &m_n->link_n);
  5990. }
  5991. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5992. {
  5993. if (i915.panel_use_ssc >= 0)
  5994. return i915.panel_use_ssc != 0;
  5995. return dev_priv->vbt.lvds_use_ssc
  5996. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5997. }
  5998. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5999. {
  6000. return (1 << dpll->n) << 16 | dpll->m2;
  6001. }
  6002. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6003. {
  6004. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6005. }
  6006. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6007. struct intel_crtc_state *crtc_state,
  6008. struct dpll *reduced_clock)
  6009. {
  6010. struct drm_device *dev = crtc->base.dev;
  6011. u32 fp, fp2 = 0;
  6012. if (IS_PINEVIEW(dev)) {
  6013. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6014. if (reduced_clock)
  6015. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6016. } else {
  6017. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6018. if (reduced_clock)
  6019. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6020. }
  6021. crtc_state->dpll_hw_state.fp0 = fp;
  6022. crtc->lowfreq_avail = false;
  6023. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6024. reduced_clock) {
  6025. crtc_state->dpll_hw_state.fp1 = fp2;
  6026. crtc->lowfreq_avail = true;
  6027. } else {
  6028. crtc_state->dpll_hw_state.fp1 = fp;
  6029. }
  6030. }
  6031. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6032. pipe)
  6033. {
  6034. u32 reg_val;
  6035. /*
  6036. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6037. * and set it to a reasonable value instead.
  6038. */
  6039. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6040. reg_val &= 0xffffff00;
  6041. reg_val |= 0x00000030;
  6042. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6043. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6044. reg_val &= 0x8cffffff;
  6045. reg_val = 0x8c000000;
  6046. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6047. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6048. reg_val &= 0xffffff00;
  6049. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6050. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6051. reg_val &= 0x00ffffff;
  6052. reg_val |= 0xb0000000;
  6053. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6054. }
  6055. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6056. struct intel_link_m_n *m_n)
  6057. {
  6058. struct drm_device *dev = crtc->base.dev;
  6059. struct drm_i915_private *dev_priv = to_i915(dev);
  6060. int pipe = crtc->pipe;
  6061. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6062. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6063. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6064. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6065. }
  6066. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6067. struct intel_link_m_n *m_n,
  6068. struct intel_link_m_n *m2_n2)
  6069. {
  6070. struct drm_device *dev = crtc->base.dev;
  6071. struct drm_i915_private *dev_priv = to_i915(dev);
  6072. int pipe = crtc->pipe;
  6073. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6074. if (INTEL_INFO(dev)->gen >= 5) {
  6075. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6076. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6077. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6078. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6079. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6080. * for gen < 8) and if DRRS is supported (to make sure the
  6081. * registers are not unnecessarily accessed).
  6082. */
  6083. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6084. crtc->config->has_drrs) {
  6085. I915_WRITE(PIPE_DATA_M2(transcoder),
  6086. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6087. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6088. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6089. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6090. }
  6091. } else {
  6092. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6093. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6094. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6095. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6096. }
  6097. }
  6098. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6099. {
  6100. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6101. if (m_n == M1_N1) {
  6102. dp_m_n = &crtc->config->dp_m_n;
  6103. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6104. } else if (m_n == M2_N2) {
  6105. /*
  6106. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6107. * needs to be programmed into M1_N1.
  6108. */
  6109. dp_m_n = &crtc->config->dp_m2_n2;
  6110. } else {
  6111. DRM_ERROR("Unsupported divider value\n");
  6112. return;
  6113. }
  6114. if (crtc->config->has_pch_encoder)
  6115. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6116. else
  6117. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6118. }
  6119. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6120. struct intel_crtc_state *pipe_config)
  6121. {
  6122. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6123. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6124. if (crtc->pipe != PIPE_A)
  6125. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6126. /* DPLL not used with DSI, but still need the rest set up */
  6127. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6128. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6129. DPLL_EXT_BUFFER_ENABLE_VLV;
  6130. pipe_config->dpll_hw_state.dpll_md =
  6131. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6132. }
  6133. static void chv_compute_dpll(struct intel_crtc *crtc,
  6134. struct intel_crtc_state *pipe_config)
  6135. {
  6136. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6137. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6138. if (crtc->pipe != PIPE_A)
  6139. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6140. /* DPLL not used with DSI, but still need the rest set up */
  6141. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6142. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6143. pipe_config->dpll_hw_state.dpll_md =
  6144. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6145. }
  6146. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6147. const struct intel_crtc_state *pipe_config)
  6148. {
  6149. struct drm_device *dev = crtc->base.dev;
  6150. struct drm_i915_private *dev_priv = to_i915(dev);
  6151. enum pipe pipe = crtc->pipe;
  6152. u32 mdiv;
  6153. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6154. u32 coreclk, reg_val;
  6155. /* Enable Refclk */
  6156. I915_WRITE(DPLL(pipe),
  6157. pipe_config->dpll_hw_state.dpll &
  6158. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6159. /* No need to actually set up the DPLL with DSI */
  6160. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6161. return;
  6162. mutex_lock(&dev_priv->sb_lock);
  6163. bestn = pipe_config->dpll.n;
  6164. bestm1 = pipe_config->dpll.m1;
  6165. bestm2 = pipe_config->dpll.m2;
  6166. bestp1 = pipe_config->dpll.p1;
  6167. bestp2 = pipe_config->dpll.p2;
  6168. /* See eDP HDMI DPIO driver vbios notes doc */
  6169. /* PLL B needs special handling */
  6170. if (pipe == PIPE_B)
  6171. vlv_pllb_recal_opamp(dev_priv, pipe);
  6172. /* Set up Tx target for periodic Rcomp update */
  6173. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6174. /* Disable target IRef on PLL */
  6175. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6176. reg_val &= 0x00ffffff;
  6177. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6178. /* Disable fast lock */
  6179. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6180. /* Set idtafcrecal before PLL is enabled */
  6181. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6182. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6183. mdiv |= ((bestn << DPIO_N_SHIFT));
  6184. mdiv |= (1 << DPIO_K_SHIFT);
  6185. /*
  6186. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6187. * but we don't support that).
  6188. * Note: don't use the DAC post divider as it seems unstable.
  6189. */
  6190. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6192. mdiv |= DPIO_ENABLE_CALIBRATION;
  6193. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6194. /* Set HBR and RBR LPF coefficients */
  6195. if (pipe_config->port_clock == 162000 ||
  6196. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6197. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6198. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6199. 0x009f0003);
  6200. else
  6201. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6202. 0x00d0000f);
  6203. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6204. /* Use SSC source */
  6205. if (pipe == PIPE_A)
  6206. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6207. 0x0df40000);
  6208. else
  6209. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6210. 0x0df70000);
  6211. } else { /* HDMI or VGA */
  6212. /* Use bend source */
  6213. if (pipe == PIPE_A)
  6214. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6215. 0x0df70000);
  6216. else
  6217. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6218. 0x0df40000);
  6219. }
  6220. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6221. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6222. if (intel_crtc_has_dp_encoder(crtc->config))
  6223. coreclk |= 0x01000000;
  6224. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6225. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6226. mutex_unlock(&dev_priv->sb_lock);
  6227. }
  6228. static void chv_prepare_pll(struct intel_crtc *crtc,
  6229. const struct intel_crtc_state *pipe_config)
  6230. {
  6231. struct drm_device *dev = crtc->base.dev;
  6232. struct drm_i915_private *dev_priv = to_i915(dev);
  6233. enum pipe pipe = crtc->pipe;
  6234. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6235. u32 loopfilter, tribuf_calcntr;
  6236. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6237. u32 dpio_val;
  6238. int vco;
  6239. /* Enable Refclk and SSC */
  6240. I915_WRITE(DPLL(pipe),
  6241. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6242. /* No need to actually set up the DPLL with DSI */
  6243. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6244. return;
  6245. bestn = pipe_config->dpll.n;
  6246. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6247. bestm1 = pipe_config->dpll.m1;
  6248. bestm2 = pipe_config->dpll.m2 >> 22;
  6249. bestp1 = pipe_config->dpll.p1;
  6250. bestp2 = pipe_config->dpll.p2;
  6251. vco = pipe_config->dpll.vco;
  6252. dpio_val = 0;
  6253. loopfilter = 0;
  6254. mutex_lock(&dev_priv->sb_lock);
  6255. /* p1 and p2 divider */
  6256. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6257. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6258. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6259. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6260. 1 << DPIO_CHV_K_DIV_SHIFT);
  6261. /* Feedback post-divider - m2 */
  6262. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6263. /* Feedback refclk divider - n and m1 */
  6264. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6265. DPIO_CHV_M1_DIV_BY_2 |
  6266. 1 << DPIO_CHV_N_DIV_SHIFT);
  6267. /* M2 fraction division */
  6268. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6269. /* M2 fraction division enable */
  6270. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6271. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6272. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6273. if (bestm2_frac)
  6274. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6275. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6276. /* Program digital lock detect threshold */
  6277. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6278. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6279. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6280. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6281. if (!bestm2_frac)
  6282. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6283. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6284. /* Loop filter */
  6285. if (vco == 5400000) {
  6286. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6287. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6288. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6289. tribuf_calcntr = 0x9;
  6290. } else if (vco <= 6200000) {
  6291. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6292. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6293. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6294. tribuf_calcntr = 0x9;
  6295. } else if (vco <= 6480000) {
  6296. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6297. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6298. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6299. tribuf_calcntr = 0x8;
  6300. } else {
  6301. /* Not supported. Apply the same limits as in the max case */
  6302. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6303. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6304. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6305. tribuf_calcntr = 0;
  6306. }
  6307. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6308. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6309. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6310. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6311. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6312. /* AFC Recal */
  6313. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6314. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6315. DPIO_AFC_RECAL);
  6316. mutex_unlock(&dev_priv->sb_lock);
  6317. }
  6318. /**
  6319. * vlv_force_pll_on - forcibly enable just the PLL
  6320. * @dev_priv: i915 private structure
  6321. * @pipe: pipe PLL to enable
  6322. * @dpll: PLL configuration
  6323. *
  6324. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6325. * in cases where we need the PLL enabled even when @pipe is not going to
  6326. * be enabled.
  6327. */
  6328. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6329. const struct dpll *dpll)
  6330. {
  6331. struct intel_crtc *crtc =
  6332. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6333. struct intel_crtc_state *pipe_config;
  6334. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6335. if (!pipe_config)
  6336. return -ENOMEM;
  6337. pipe_config->base.crtc = &crtc->base;
  6338. pipe_config->pixel_multiplier = 1;
  6339. pipe_config->dpll = *dpll;
  6340. if (IS_CHERRYVIEW(dev)) {
  6341. chv_compute_dpll(crtc, pipe_config);
  6342. chv_prepare_pll(crtc, pipe_config);
  6343. chv_enable_pll(crtc, pipe_config);
  6344. } else {
  6345. vlv_compute_dpll(crtc, pipe_config);
  6346. vlv_prepare_pll(crtc, pipe_config);
  6347. vlv_enable_pll(crtc, pipe_config);
  6348. }
  6349. kfree(pipe_config);
  6350. return 0;
  6351. }
  6352. /**
  6353. * vlv_force_pll_off - forcibly disable just the PLL
  6354. * @dev_priv: i915 private structure
  6355. * @pipe: pipe PLL to disable
  6356. *
  6357. * Disable the PLL for @pipe. To be used in cases where we need
  6358. * the PLL enabled even when @pipe is not going to be enabled.
  6359. */
  6360. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6361. {
  6362. if (IS_CHERRYVIEW(dev))
  6363. chv_disable_pll(to_i915(dev), pipe);
  6364. else
  6365. vlv_disable_pll(to_i915(dev), pipe);
  6366. }
  6367. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6368. struct intel_crtc_state *crtc_state,
  6369. struct dpll *reduced_clock)
  6370. {
  6371. struct drm_device *dev = crtc->base.dev;
  6372. struct drm_i915_private *dev_priv = to_i915(dev);
  6373. u32 dpll;
  6374. struct dpll *clock = &crtc_state->dpll;
  6375. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6376. dpll = DPLL_VGA_MODE_DIS;
  6377. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6378. dpll |= DPLLB_MODE_LVDS;
  6379. else
  6380. dpll |= DPLLB_MODE_DAC_SERIAL;
  6381. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6382. dpll |= (crtc_state->pixel_multiplier - 1)
  6383. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6384. }
  6385. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6386. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6387. dpll |= DPLL_SDVO_HIGH_SPEED;
  6388. if (intel_crtc_has_dp_encoder(crtc_state))
  6389. dpll |= DPLL_SDVO_HIGH_SPEED;
  6390. /* compute bitmask from p1 value */
  6391. if (IS_PINEVIEW(dev))
  6392. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6393. else {
  6394. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6395. if (IS_G4X(dev) && reduced_clock)
  6396. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6397. }
  6398. switch (clock->p2) {
  6399. case 5:
  6400. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6401. break;
  6402. case 7:
  6403. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6404. break;
  6405. case 10:
  6406. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6407. break;
  6408. case 14:
  6409. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6410. break;
  6411. }
  6412. if (INTEL_INFO(dev)->gen >= 4)
  6413. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6414. if (crtc_state->sdvo_tv_clock)
  6415. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6416. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6417. intel_panel_use_ssc(dev_priv))
  6418. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6419. else
  6420. dpll |= PLL_REF_INPUT_DREFCLK;
  6421. dpll |= DPLL_VCO_ENABLE;
  6422. crtc_state->dpll_hw_state.dpll = dpll;
  6423. if (INTEL_INFO(dev)->gen >= 4) {
  6424. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6425. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6426. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6427. }
  6428. }
  6429. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6430. struct intel_crtc_state *crtc_state,
  6431. struct dpll *reduced_clock)
  6432. {
  6433. struct drm_device *dev = crtc->base.dev;
  6434. struct drm_i915_private *dev_priv = to_i915(dev);
  6435. u32 dpll;
  6436. struct dpll *clock = &crtc_state->dpll;
  6437. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6438. dpll = DPLL_VGA_MODE_DIS;
  6439. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6440. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6441. } else {
  6442. if (clock->p1 == 2)
  6443. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6444. else
  6445. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6446. if (clock->p2 == 4)
  6447. dpll |= PLL_P2_DIVIDE_BY_4;
  6448. }
  6449. if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6450. dpll |= DPLL_DVO_2X_MODE;
  6451. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6452. intel_panel_use_ssc(dev_priv))
  6453. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6454. else
  6455. dpll |= PLL_REF_INPUT_DREFCLK;
  6456. dpll |= DPLL_VCO_ENABLE;
  6457. crtc_state->dpll_hw_state.dpll = dpll;
  6458. }
  6459. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6460. {
  6461. struct drm_device *dev = intel_crtc->base.dev;
  6462. struct drm_i915_private *dev_priv = to_i915(dev);
  6463. enum pipe pipe = intel_crtc->pipe;
  6464. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6465. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6466. uint32_t crtc_vtotal, crtc_vblank_end;
  6467. int vsyncshift = 0;
  6468. /* We need to be careful not to changed the adjusted mode, for otherwise
  6469. * the hw state checker will get angry at the mismatch. */
  6470. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6471. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6472. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6473. /* the chip adds 2 halflines automatically */
  6474. crtc_vtotal -= 1;
  6475. crtc_vblank_end -= 1;
  6476. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6477. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6478. else
  6479. vsyncshift = adjusted_mode->crtc_hsync_start -
  6480. adjusted_mode->crtc_htotal / 2;
  6481. if (vsyncshift < 0)
  6482. vsyncshift += adjusted_mode->crtc_htotal;
  6483. }
  6484. if (INTEL_INFO(dev)->gen > 3)
  6485. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6486. I915_WRITE(HTOTAL(cpu_transcoder),
  6487. (adjusted_mode->crtc_hdisplay - 1) |
  6488. ((adjusted_mode->crtc_htotal - 1) << 16));
  6489. I915_WRITE(HBLANK(cpu_transcoder),
  6490. (adjusted_mode->crtc_hblank_start - 1) |
  6491. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6492. I915_WRITE(HSYNC(cpu_transcoder),
  6493. (adjusted_mode->crtc_hsync_start - 1) |
  6494. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6495. I915_WRITE(VTOTAL(cpu_transcoder),
  6496. (adjusted_mode->crtc_vdisplay - 1) |
  6497. ((crtc_vtotal - 1) << 16));
  6498. I915_WRITE(VBLANK(cpu_transcoder),
  6499. (adjusted_mode->crtc_vblank_start - 1) |
  6500. ((crtc_vblank_end - 1) << 16));
  6501. I915_WRITE(VSYNC(cpu_transcoder),
  6502. (adjusted_mode->crtc_vsync_start - 1) |
  6503. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6504. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6505. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6506. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6507. * bits. */
  6508. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6509. (pipe == PIPE_B || pipe == PIPE_C))
  6510. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6511. }
  6512. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6513. {
  6514. struct drm_device *dev = intel_crtc->base.dev;
  6515. struct drm_i915_private *dev_priv = to_i915(dev);
  6516. enum pipe pipe = intel_crtc->pipe;
  6517. /* pipesrc controls the size that is scaled from, which should
  6518. * always be the user's requested size.
  6519. */
  6520. I915_WRITE(PIPESRC(pipe),
  6521. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6522. (intel_crtc->config->pipe_src_h - 1));
  6523. }
  6524. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6525. struct intel_crtc_state *pipe_config)
  6526. {
  6527. struct drm_device *dev = crtc->base.dev;
  6528. struct drm_i915_private *dev_priv = to_i915(dev);
  6529. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6530. uint32_t tmp;
  6531. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6532. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6533. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6534. tmp = I915_READ(HBLANK(cpu_transcoder));
  6535. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6536. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6537. tmp = I915_READ(HSYNC(cpu_transcoder));
  6538. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6539. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6540. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6541. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6542. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6543. tmp = I915_READ(VBLANK(cpu_transcoder));
  6544. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6545. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6546. tmp = I915_READ(VSYNC(cpu_transcoder));
  6547. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6548. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6549. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6550. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6551. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6552. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6553. }
  6554. }
  6555. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6556. struct intel_crtc_state *pipe_config)
  6557. {
  6558. struct drm_device *dev = crtc->base.dev;
  6559. struct drm_i915_private *dev_priv = to_i915(dev);
  6560. u32 tmp;
  6561. tmp = I915_READ(PIPESRC(crtc->pipe));
  6562. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6563. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6564. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6565. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6566. }
  6567. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6568. struct intel_crtc_state *pipe_config)
  6569. {
  6570. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6571. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6572. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6573. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6574. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6575. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6576. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6577. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6578. mode->flags = pipe_config->base.adjusted_mode.flags;
  6579. mode->type = DRM_MODE_TYPE_DRIVER;
  6580. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6581. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6582. mode->hsync = drm_mode_hsync(mode);
  6583. mode->vrefresh = drm_mode_vrefresh(mode);
  6584. drm_mode_set_name(mode);
  6585. }
  6586. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6587. {
  6588. struct drm_device *dev = intel_crtc->base.dev;
  6589. struct drm_i915_private *dev_priv = to_i915(dev);
  6590. uint32_t pipeconf;
  6591. pipeconf = 0;
  6592. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6593. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6594. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6595. if (intel_crtc->config->double_wide)
  6596. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6597. /* only g4x and later have fancy bpc/dither controls */
  6598. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6599. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6600. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6601. pipeconf |= PIPECONF_DITHER_EN |
  6602. PIPECONF_DITHER_TYPE_SP;
  6603. switch (intel_crtc->config->pipe_bpp) {
  6604. case 18:
  6605. pipeconf |= PIPECONF_6BPC;
  6606. break;
  6607. case 24:
  6608. pipeconf |= PIPECONF_8BPC;
  6609. break;
  6610. case 30:
  6611. pipeconf |= PIPECONF_10BPC;
  6612. break;
  6613. default:
  6614. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6615. BUG();
  6616. }
  6617. }
  6618. if (HAS_PIPE_CXSR(dev)) {
  6619. if (intel_crtc->lowfreq_avail) {
  6620. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6621. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6622. } else {
  6623. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6624. }
  6625. }
  6626. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6627. if (INTEL_INFO(dev)->gen < 4 ||
  6628. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6629. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6630. else
  6631. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6632. } else
  6633. pipeconf |= PIPECONF_PROGRESSIVE;
  6634. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6635. intel_crtc->config->limited_color_range)
  6636. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6637. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6638. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6639. }
  6640. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6641. struct intel_crtc_state *crtc_state)
  6642. {
  6643. struct drm_device *dev = crtc->base.dev;
  6644. struct drm_i915_private *dev_priv = to_i915(dev);
  6645. const struct intel_limit *limit;
  6646. int refclk = 48000;
  6647. memset(&crtc_state->dpll_hw_state, 0,
  6648. sizeof(crtc_state->dpll_hw_state));
  6649. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6650. if (intel_panel_use_ssc(dev_priv)) {
  6651. refclk = dev_priv->vbt.lvds_ssc_freq;
  6652. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6653. }
  6654. limit = &intel_limits_i8xx_lvds;
  6655. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6656. limit = &intel_limits_i8xx_dvo;
  6657. } else {
  6658. limit = &intel_limits_i8xx_dac;
  6659. }
  6660. if (!crtc_state->clock_set &&
  6661. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6662. refclk, NULL, &crtc_state->dpll)) {
  6663. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6664. return -EINVAL;
  6665. }
  6666. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6667. return 0;
  6668. }
  6669. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6670. struct intel_crtc_state *crtc_state)
  6671. {
  6672. struct drm_device *dev = crtc->base.dev;
  6673. struct drm_i915_private *dev_priv = to_i915(dev);
  6674. const struct intel_limit *limit;
  6675. int refclk = 96000;
  6676. memset(&crtc_state->dpll_hw_state, 0,
  6677. sizeof(crtc_state->dpll_hw_state));
  6678. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6679. if (intel_panel_use_ssc(dev_priv)) {
  6680. refclk = dev_priv->vbt.lvds_ssc_freq;
  6681. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6682. }
  6683. if (intel_is_dual_link_lvds(dev))
  6684. limit = &intel_limits_g4x_dual_channel_lvds;
  6685. else
  6686. limit = &intel_limits_g4x_single_channel_lvds;
  6687. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6688. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6689. limit = &intel_limits_g4x_hdmi;
  6690. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6691. limit = &intel_limits_g4x_sdvo;
  6692. } else {
  6693. /* The option is for other outputs */
  6694. limit = &intel_limits_i9xx_sdvo;
  6695. }
  6696. if (!crtc_state->clock_set &&
  6697. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6698. refclk, NULL, &crtc_state->dpll)) {
  6699. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6700. return -EINVAL;
  6701. }
  6702. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6703. return 0;
  6704. }
  6705. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6706. struct intel_crtc_state *crtc_state)
  6707. {
  6708. struct drm_device *dev = crtc->base.dev;
  6709. struct drm_i915_private *dev_priv = to_i915(dev);
  6710. const struct intel_limit *limit;
  6711. int refclk = 96000;
  6712. memset(&crtc_state->dpll_hw_state, 0,
  6713. sizeof(crtc_state->dpll_hw_state));
  6714. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6715. if (intel_panel_use_ssc(dev_priv)) {
  6716. refclk = dev_priv->vbt.lvds_ssc_freq;
  6717. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6718. }
  6719. limit = &intel_limits_pineview_lvds;
  6720. } else {
  6721. limit = &intel_limits_pineview_sdvo;
  6722. }
  6723. if (!crtc_state->clock_set &&
  6724. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6725. refclk, NULL, &crtc_state->dpll)) {
  6726. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6727. return -EINVAL;
  6728. }
  6729. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6730. return 0;
  6731. }
  6732. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6733. struct intel_crtc_state *crtc_state)
  6734. {
  6735. struct drm_device *dev = crtc->base.dev;
  6736. struct drm_i915_private *dev_priv = to_i915(dev);
  6737. const struct intel_limit *limit;
  6738. int refclk = 96000;
  6739. memset(&crtc_state->dpll_hw_state, 0,
  6740. sizeof(crtc_state->dpll_hw_state));
  6741. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6742. if (intel_panel_use_ssc(dev_priv)) {
  6743. refclk = dev_priv->vbt.lvds_ssc_freq;
  6744. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6745. }
  6746. limit = &intel_limits_i9xx_lvds;
  6747. } else {
  6748. limit = &intel_limits_i9xx_sdvo;
  6749. }
  6750. if (!crtc_state->clock_set &&
  6751. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6752. refclk, NULL, &crtc_state->dpll)) {
  6753. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6754. return -EINVAL;
  6755. }
  6756. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6757. return 0;
  6758. }
  6759. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6760. struct intel_crtc_state *crtc_state)
  6761. {
  6762. int refclk = 100000;
  6763. const struct intel_limit *limit = &intel_limits_chv;
  6764. memset(&crtc_state->dpll_hw_state, 0,
  6765. sizeof(crtc_state->dpll_hw_state));
  6766. if (!crtc_state->clock_set &&
  6767. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6768. refclk, NULL, &crtc_state->dpll)) {
  6769. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6770. return -EINVAL;
  6771. }
  6772. chv_compute_dpll(crtc, crtc_state);
  6773. return 0;
  6774. }
  6775. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6776. struct intel_crtc_state *crtc_state)
  6777. {
  6778. int refclk = 100000;
  6779. const struct intel_limit *limit = &intel_limits_vlv;
  6780. memset(&crtc_state->dpll_hw_state, 0,
  6781. sizeof(crtc_state->dpll_hw_state));
  6782. if (!crtc_state->clock_set &&
  6783. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6784. refclk, NULL, &crtc_state->dpll)) {
  6785. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6786. return -EINVAL;
  6787. }
  6788. vlv_compute_dpll(crtc, crtc_state);
  6789. return 0;
  6790. }
  6791. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6792. struct intel_crtc_state *pipe_config)
  6793. {
  6794. struct drm_device *dev = crtc->base.dev;
  6795. struct drm_i915_private *dev_priv = to_i915(dev);
  6796. uint32_t tmp;
  6797. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6798. return;
  6799. tmp = I915_READ(PFIT_CONTROL);
  6800. if (!(tmp & PFIT_ENABLE))
  6801. return;
  6802. /* Check whether the pfit is attached to our pipe. */
  6803. if (INTEL_INFO(dev)->gen < 4) {
  6804. if (crtc->pipe != PIPE_B)
  6805. return;
  6806. } else {
  6807. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6808. return;
  6809. }
  6810. pipe_config->gmch_pfit.control = tmp;
  6811. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6812. }
  6813. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6814. struct intel_crtc_state *pipe_config)
  6815. {
  6816. struct drm_device *dev = crtc->base.dev;
  6817. struct drm_i915_private *dev_priv = to_i915(dev);
  6818. int pipe = pipe_config->cpu_transcoder;
  6819. struct dpll clock;
  6820. u32 mdiv;
  6821. int refclk = 100000;
  6822. /* In case of DSI, DPLL will not be used */
  6823. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6824. return;
  6825. mutex_lock(&dev_priv->sb_lock);
  6826. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6827. mutex_unlock(&dev_priv->sb_lock);
  6828. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6829. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6830. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6831. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6832. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6833. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6834. }
  6835. static void
  6836. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6837. struct intel_initial_plane_config *plane_config)
  6838. {
  6839. struct drm_device *dev = crtc->base.dev;
  6840. struct drm_i915_private *dev_priv = to_i915(dev);
  6841. u32 val, base, offset;
  6842. int pipe = crtc->pipe, plane = crtc->plane;
  6843. int fourcc, pixel_format;
  6844. unsigned int aligned_height;
  6845. struct drm_framebuffer *fb;
  6846. struct intel_framebuffer *intel_fb;
  6847. val = I915_READ(DSPCNTR(plane));
  6848. if (!(val & DISPLAY_PLANE_ENABLE))
  6849. return;
  6850. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6851. if (!intel_fb) {
  6852. DRM_DEBUG_KMS("failed to alloc fb\n");
  6853. return;
  6854. }
  6855. fb = &intel_fb->base;
  6856. if (INTEL_INFO(dev)->gen >= 4) {
  6857. if (val & DISPPLANE_TILED) {
  6858. plane_config->tiling = I915_TILING_X;
  6859. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6860. }
  6861. }
  6862. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6863. fourcc = i9xx_format_to_fourcc(pixel_format);
  6864. fb->pixel_format = fourcc;
  6865. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6866. if (INTEL_INFO(dev)->gen >= 4) {
  6867. if (plane_config->tiling)
  6868. offset = I915_READ(DSPTILEOFF(plane));
  6869. else
  6870. offset = I915_READ(DSPLINOFF(plane));
  6871. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6872. } else {
  6873. base = I915_READ(DSPADDR(plane));
  6874. }
  6875. plane_config->base = base;
  6876. val = I915_READ(PIPESRC(pipe));
  6877. fb->width = ((val >> 16) & 0xfff) + 1;
  6878. fb->height = ((val >> 0) & 0xfff) + 1;
  6879. val = I915_READ(DSPSTRIDE(pipe));
  6880. fb->pitches[0] = val & 0xffffffc0;
  6881. aligned_height = intel_fb_align_height(dev, fb->height,
  6882. fb->pixel_format,
  6883. fb->modifier[0]);
  6884. plane_config->size = fb->pitches[0] * aligned_height;
  6885. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6886. pipe_name(pipe), plane, fb->width, fb->height,
  6887. fb->bits_per_pixel, base, fb->pitches[0],
  6888. plane_config->size);
  6889. plane_config->fb = intel_fb;
  6890. }
  6891. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6892. struct intel_crtc_state *pipe_config)
  6893. {
  6894. struct drm_device *dev = crtc->base.dev;
  6895. struct drm_i915_private *dev_priv = to_i915(dev);
  6896. int pipe = pipe_config->cpu_transcoder;
  6897. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6898. struct dpll clock;
  6899. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6900. int refclk = 100000;
  6901. /* In case of DSI, DPLL will not be used */
  6902. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6903. return;
  6904. mutex_lock(&dev_priv->sb_lock);
  6905. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6906. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6907. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6908. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6909. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6910. mutex_unlock(&dev_priv->sb_lock);
  6911. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6912. clock.m2 = (pll_dw0 & 0xff) << 22;
  6913. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6914. clock.m2 |= pll_dw2 & 0x3fffff;
  6915. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6916. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6917. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6918. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6919. }
  6920. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6921. struct intel_crtc_state *pipe_config)
  6922. {
  6923. struct drm_device *dev = crtc->base.dev;
  6924. struct drm_i915_private *dev_priv = to_i915(dev);
  6925. enum intel_display_power_domain power_domain;
  6926. uint32_t tmp;
  6927. bool ret;
  6928. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6929. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6930. return false;
  6931. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6932. pipe_config->shared_dpll = NULL;
  6933. ret = false;
  6934. tmp = I915_READ(PIPECONF(crtc->pipe));
  6935. if (!(tmp & PIPECONF_ENABLE))
  6936. goto out;
  6937. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6938. switch (tmp & PIPECONF_BPC_MASK) {
  6939. case PIPECONF_6BPC:
  6940. pipe_config->pipe_bpp = 18;
  6941. break;
  6942. case PIPECONF_8BPC:
  6943. pipe_config->pipe_bpp = 24;
  6944. break;
  6945. case PIPECONF_10BPC:
  6946. pipe_config->pipe_bpp = 30;
  6947. break;
  6948. default:
  6949. break;
  6950. }
  6951. }
  6952. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6953. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6954. pipe_config->limited_color_range = true;
  6955. if (INTEL_INFO(dev)->gen < 4)
  6956. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6957. intel_get_pipe_timings(crtc, pipe_config);
  6958. intel_get_pipe_src_size(crtc, pipe_config);
  6959. i9xx_get_pfit_config(crtc, pipe_config);
  6960. if (INTEL_INFO(dev)->gen >= 4) {
  6961. /* No way to read it out on pipes B and C */
  6962. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6963. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6964. else
  6965. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6966. pipe_config->pixel_multiplier =
  6967. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6968. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6969. pipe_config->dpll_hw_state.dpll_md = tmp;
  6970. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6971. tmp = I915_READ(DPLL(crtc->pipe));
  6972. pipe_config->pixel_multiplier =
  6973. ((tmp & SDVO_MULTIPLIER_MASK)
  6974. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6975. } else {
  6976. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6977. * port and will be fixed up in the encoder->get_config
  6978. * function. */
  6979. pipe_config->pixel_multiplier = 1;
  6980. }
  6981. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6982. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6983. /*
  6984. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6985. * on 830. Filter it out here so that we don't
  6986. * report errors due to that.
  6987. */
  6988. if (IS_I830(dev))
  6989. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6990. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6991. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6992. } else {
  6993. /* Mask out read-only status bits. */
  6994. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6995. DPLL_PORTC_READY_MASK |
  6996. DPLL_PORTB_READY_MASK);
  6997. }
  6998. if (IS_CHERRYVIEW(dev))
  6999. chv_crtc_clock_get(crtc, pipe_config);
  7000. else if (IS_VALLEYVIEW(dev))
  7001. vlv_crtc_clock_get(crtc, pipe_config);
  7002. else
  7003. i9xx_crtc_clock_get(crtc, pipe_config);
  7004. /*
  7005. * Normally the dotclock is filled in by the encoder .get_config()
  7006. * but in case the pipe is enabled w/o any ports we need a sane
  7007. * default.
  7008. */
  7009. pipe_config->base.adjusted_mode.crtc_clock =
  7010. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7011. ret = true;
  7012. out:
  7013. intel_display_power_put(dev_priv, power_domain);
  7014. return ret;
  7015. }
  7016. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7017. {
  7018. struct drm_i915_private *dev_priv = to_i915(dev);
  7019. struct intel_encoder *encoder;
  7020. int i;
  7021. u32 val, final;
  7022. bool has_lvds = false;
  7023. bool has_cpu_edp = false;
  7024. bool has_panel = false;
  7025. bool has_ck505 = false;
  7026. bool can_ssc = false;
  7027. bool using_ssc_source = false;
  7028. /* We need to take the global config into account */
  7029. for_each_intel_encoder(dev, encoder) {
  7030. switch (encoder->type) {
  7031. case INTEL_OUTPUT_LVDS:
  7032. has_panel = true;
  7033. has_lvds = true;
  7034. break;
  7035. case INTEL_OUTPUT_EDP:
  7036. has_panel = true;
  7037. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7038. has_cpu_edp = true;
  7039. break;
  7040. default:
  7041. break;
  7042. }
  7043. }
  7044. if (HAS_PCH_IBX(dev)) {
  7045. has_ck505 = dev_priv->vbt.display_clock_mode;
  7046. can_ssc = has_ck505;
  7047. } else {
  7048. has_ck505 = false;
  7049. can_ssc = true;
  7050. }
  7051. /* Check if any DPLLs are using the SSC source */
  7052. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7053. u32 temp = I915_READ(PCH_DPLL(i));
  7054. if (!(temp & DPLL_VCO_ENABLE))
  7055. continue;
  7056. if ((temp & PLL_REF_INPUT_MASK) ==
  7057. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7058. using_ssc_source = true;
  7059. break;
  7060. }
  7061. }
  7062. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7063. has_panel, has_lvds, has_ck505, using_ssc_source);
  7064. /* Ironlake: try to setup display ref clock before DPLL
  7065. * enabling. This is only under driver's control after
  7066. * PCH B stepping, previous chipset stepping should be
  7067. * ignoring this setting.
  7068. */
  7069. val = I915_READ(PCH_DREF_CONTROL);
  7070. /* As we must carefully and slowly disable/enable each source in turn,
  7071. * compute the final state we want first and check if we need to
  7072. * make any changes at all.
  7073. */
  7074. final = val;
  7075. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7076. if (has_ck505)
  7077. final |= DREF_NONSPREAD_CK505_ENABLE;
  7078. else
  7079. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7080. final &= ~DREF_SSC_SOURCE_MASK;
  7081. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7082. final &= ~DREF_SSC1_ENABLE;
  7083. if (has_panel) {
  7084. final |= DREF_SSC_SOURCE_ENABLE;
  7085. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7086. final |= DREF_SSC1_ENABLE;
  7087. if (has_cpu_edp) {
  7088. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7089. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7090. else
  7091. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7092. } else
  7093. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7094. } else if (using_ssc_source) {
  7095. final |= DREF_SSC_SOURCE_ENABLE;
  7096. final |= DREF_SSC1_ENABLE;
  7097. }
  7098. if (final == val)
  7099. return;
  7100. /* Always enable nonspread source */
  7101. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7102. if (has_ck505)
  7103. val |= DREF_NONSPREAD_CK505_ENABLE;
  7104. else
  7105. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7106. if (has_panel) {
  7107. val &= ~DREF_SSC_SOURCE_MASK;
  7108. val |= DREF_SSC_SOURCE_ENABLE;
  7109. /* SSC must be turned on before enabling the CPU output */
  7110. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7111. DRM_DEBUG_KMS("Using SSC on panel\n");
  7112. val |= DREF_SSC1_ENABLE;
  7113. } else
  7114. val &= ~DREF_SSC1_ENABLE;
  7115. /* Get SSC going before enabling the outputs */
  7116. I915_WRITE(PCH_DREF_CONTROL, val);
  7117. POSTING_READ(PCH_DREF_CONTROL);
  7118. udelay(200);
  7119. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7120. /* Enable CPU source on CPU attached eDP */
  7121. if (has_cpu_edp) {
  7122. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7123. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7124. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7125. } else
  7126. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7127. } else
  7128. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7129. I915_WRITE(PCH_DREF_CONTROL, val);
  7130. POSTING_READ(PCH_DREF_CONTROL);
  7131. udelay(200);
  7132. } else {
  7133. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7134. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7135. /* Turn off CPU output */
  7136. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7137. I915_WRITE(PCH_DREF_CONTROL, val);
  7138. POSTING_READ(PCH_DREF_CONTROL);
  7139. udelay(200);
  7140. if (!using_ssc_source) {
  7141. DRM_DEBUG_KMS("Disabling SSC source\n");
  7142. /* Turn off the SSC source */
  7143. val &= ~DREF_SSC_SOURCE_MASK;
  7144. val |= DREF_SSC_SOURCE_DISABLE;
  7145. /* Turn off SSC1 */
  7146. val &= ~DREF_SSC1_ENABLE;
  7147. I915_WRITE(PCH_DREF_CONTROL, val);
  7148. POSTING_READ(PCH_DREF_CONTROL);
  7149. udelay(200);
  7150. }
  7151. }
  7152. BUG_ON(val != final);
  7153. }
  7154. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7155. {
  7156. uint32_t tmp;
  7157. tmp = I915_READ(SOUTH_CHICKEN2);
  7158. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7159. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7160. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7161. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7162. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7163. tmp = I915_READ(SOUTH_CHICKEN2);
  7164. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7165. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7166. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7167. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7168. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7169. }
  7170. /* WaMPhyProgramming:hsw */
  7171. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7172. {
  7173. uint32_t tmp;
  7174. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7175. tmp &= ~(0xFF << 24);
  7176. tmp |= (0x12 << 24);
  7177. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7178. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7179. tmp |= (1 << 11);
  7180. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7181. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7182. tmp |= (1 << 11);
  7183. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7184. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7185. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7186. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7187. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7188. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7189. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7190. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7191. tmp &= ~(7 << 13);
  7192. tmp |= (5 << 13);
  7193. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7194. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7195. tmp &= ~(7 << 13);
  7196. tmp |= (5 << 13);
  7197. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7198. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7199. tmp &= ~0xFF;
  7200. tmp |= 0x1C;
  7201. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7202. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7203. tmp &= ~0xFF;
  7204. tmp |= 0x1C;
  7205. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7206. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7207. tmp &= ~(0xFF << 16);
  7208. tmp |= (0x1C << 16);
  7209. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7210. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7211. tmp &= ~(0xFF << 16);
  7212. tmp |= (0x1C << 16);
  7213. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7214. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7215. tmp |= (1 << 27);
  7216. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7217. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7218. tmp |= (1 << 27);
  7219. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7220. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7221. tmp &= ~(0xF << 28);
  7222. tmp |= (4 << 28);
  7223. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7224. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7225. tmp &= ~(0xF << 28);
  7226. tmp |= (4 << 28);
  7227. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7228. }
  7229. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7230. * Programming" based on the parameters passed:
  7231. * - Sequence to enable CLKOUT_DP
  7232. * - Sequence to enable CLKOUT_DP without spread
  7233. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7234. */
  7235. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7236. bool with_fdi)
  7237. {
  7238. struct drm_i915_private *dev_priv = to_i915(dev);
  7239. uint32_t reg, tmp;
  7240. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7241. with_spread = true;
  7242. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7243. with_fdi = false;
  7244. mutex_lock(&dev_priv->sb_lock);
  7245. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7246. tmp &= ~SBI_SSCCTL_DISABLE;
  7247. tmp |= SBI_SSCCTL_PATHALT;
  7248. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7249. udelay(24);
  7250. if (with_spread) {
  7251. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7252. tmp &= ~SBI_SSCCTL_PATHALT;
  7253. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7254. if (with_fdi) {
  7255. lpt_reset_fdi_mphy(dev_priv);
  7256. lpt_program_fdi_mphy(dev_priv);
  7257. }
  7258. }
  7259. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7260. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7261. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7262. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7263. mutex_unlock(&dev_priv->sb_lock);
  7264. }
  7265. /* Sequence to disable CLKOUT_DP */
  7266. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7267. {
  7268. struct drm_i915_private *dev_priv = to_i915(dev);
  7269. uint32_t reg, tmp;
  7270. mutex_lock(&dev_priv->sb_lock);
  7271. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7272. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7273. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7274. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7275. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7276. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7277. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7278. tmp |= SBI_SSCCTL_PATHALT;
  7279. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7280. udelay(32);
  7281. }
  7282. tmp |= SBI_SSCCTL_DISABLE;
  7283. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7284. }
  7285. mutex_unlock(&dev_priv->sb_lock);
  7286. }
  7287. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7288. static const uint16_t sscdivintphase[] = {
  7289. [BEND_IDX( 50)] = 0x3B23,
  7290. [BEND_IDX( 45)] = 0x3B23,
  7291. [BEND_IDX( 40)] = 0x3C23,
  7292. [BEND_IDX( 35)] = 0x3C23,
  7293. [BEND_IDX( 30)] = 0x3D23,
  7294. [BEND_IDX( 25)] = 0x3D23,
  7295. [BEND_IDX( 20)] = 0x3E23,
  7296. [BEND_IDX( 15)] = 0x3E23,
  7297. [BEND_IDX( 10)] = 0x3F23,
  7298. [BEND_IDX( 5)] = 0x3F23,
  7299. [BEND_IDX( 0)] = 0x0025,
  7300. [BEND_IDX( -5)] = 0x0025,
  7301. [BEND_IDX(-10)] = 0x0125,
  7302. [BEND_IDX(-15)] = 0x0125,
  7303. [BEND_IDX(-20)] = 0x0225,
  7304. [BEND_IDX(-25)] = 0x0225,
  7305. [BEND_IDX(-30)] = 0x0325,
  7306. [BEND_IDX(-35)] = 0x0325,
  7307. [BEND_IDX(-40)] = 0x0425,
  7308. [BEND_IDX(-45)] = 0x0425,
  7309. [BEND_IDX(-50)] = 0x0525,
  7310. };
  7311. /*
  7312. * Bend CLKOUT_DP
  7313. * steps -50 to 50 inclusive, in steps of 5
  7314. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7315. * change in clock period = -(steps / 10) * 5.787 ps
  7316. */
  7317. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7318. {
  7319. uint32_t tmp;
  7320. int idx = BEND_IDX(steps);
  7321. if (WARN_ON(steps % 5 != 0))
  7322. return;
  7323. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7324. return;
  7325. mutex_lock(&dev_priv->sb_lock);
  7326. if (steps % 10 != 0)
  7327. tmp = 0xAAAAAAAB;
  7328. else
  7329. tmp = 0x00000000;
  7330. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7331. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7332. tmp &= 0xffff0000;
  7333. tmp |= sscdivintphase[idx];
  7334. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7335. mutex_unlock(&dev_priv->sb_lock);
  7336. }
  7337. #undef BEND_IDX
  7338. static void lpt_init_pch_refclk(struct drm_device *dev)
  7339. {
  7340. struct intel_encoder *encoder;
  7341. bool has_vga = false;
  7342. for_each_intel_encoder(dev, encoder) {
  7343. switch (encoder->type) {
  7344. case INTEL_OUTPUT_ANALOG:
  7345. has_vga = true;
  7346. break;
  7347. default:
  7348. break;
  7349. }
  7350. }
  7351. if (has_vga) {
  7352. lpt_bend_clkout_dp(to_i915(dev), 0);
  7353. lpt_enable_clkout_dp(dev, true, true);
  7354. } else {
  7355. lpt_disable_clkout_dp(dev);
  7356. }
  7357. }
  7358. /*
  7359. * Initialize reference clocks when the driver loads
  7360. */
  7361. void intel_init_pch_refclk(struct drm_device *dev)
  7362. {
  7363. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7364. ironlake_init_pch_refclk(dev);
  7365. else if (HAS_PCH_LPT(dev))
  7366. lpt_init_pch_refclk(dev);
  7367. }
  7368. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7369. {
  7370. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7371. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7372. int pipe = intel_crtc->pipe;
  7373. uint32_t val;
  7374. val = 0;
  7375. switch (intel_crtc->config->pipe_bpp) {
  7376. case 18:
  7377. val |= PIPECONF_6BPC;
  7378. break;
  7379. case 24:
  7380. val |= PIPECONF_8BPC;
  7381. break;
  7382. case 30:
  7383. val |= PIPECONF_10BPC;
  7384. break;
  7385. case 36:
  7386. val |= PIPECONF_12BPC;
  7387. break;
  7388. default:
  7389. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7390. BUG();
  7391. }
  7392. if (intel_crtc->config->dither)
  7393. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7394. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7395. val |= PIPECONF_INTERLACED_ILK;
  7396. else
  7397. val |= PIPECONF_PROGRESSIVE;
  7398. if (intel_crtc->config->limited_color_range)
  7399. val |= PIPECONF_COLOR_RANGE_SELECT;
  7400. I915_WRITE(PIPECONF(pipe), val);
  7401. POSTING_READ(PIPECONF(pipe));
  7402. }
  7403. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7404. {
  7405. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7407. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7408. u32 val = 0;
  7409. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7410. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7411. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7412. val |= PIPECONF_INTERLACED_ILK;
  7413. else
  7414. val |= PIPECONF_PROGRESSIVE;
  7415. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7416. POSTING_READ(PIPECONF(cpu_transcoder));
  7417. }
  7418. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7419. {
  7420. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7422. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7423. u32 val = 0;
  7424. switch (intel_crtc->config->pipe_bpp) {
  7425. case 18:
  7426. val |= PIPEMISC_DITHER_6_BPC;
  7427. break;
  7428. case 24:
  7429. val |= PIPEMISC_DITHER_8_BPC;
  7430. break;
  7431. case 30:
  7432. val |= PIPEMISC_DITHER_10_BPC;
  7433. break;
  7434. case 36:
  7435. val |= PIPEMISC_DITHER_12_BPC;
  7436. break;
  7437. default:
  7438. /* Case prevented by pipe_config_set_bpp. */
  7439. BUG();
  7440. }
  7441. if (intel_crtc->config->dither)
  7442. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7443. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7444. }
  7445. }
  7446. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7447. {
  7448. /*
  7449. * Account for spread spectrum to avoid
  7450. * oversubscribing the link. Max center spread
  7451. * is 2.5%; use 5% for safety's sake.
  7452. */
  7453. u32 bps = target_clock * bpp * 21 / 20;
  7454. return DIV_ROUND_UP(bps, link_bw * 8);
  7455. }
  7456. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7457. {
  7458. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7459. }
  7460. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7461. struct intel_crtc_state *crtc_state,
  7462. struct dpll *reduced_clock)
  7463. {
  7464. struct drm_crtc *crtc = &intel_crtc->base;
  7465. struct drm_device *dev = crtc->dev;
  7466. struct drm_i915_private *dev_priv = to_i915(dev);
  7467. u32 dpll, fp, fp2;
  7468. int factor;
  7469. /* Enable autotuning of the PLL clock (if permissible) */
  7470. factor = 21;
  7471. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7472. if ((intel_panel_use_ssc(dev_priv) &&
  7473. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7474. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7475. factor = 25;
  7476. } else if (crtc_state->sdvo_tv_clock)
  7477. factor = 20;
  7478. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7479. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7480. fp |= FP_CB_TUNE;
  7481. if (reduced_clock) {
  7482. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7483. if (reduced_clock->m < factor * reduced_clock->n)
  7484. fp2 |= FP_CB_TUNE;
  7485. } else {
  7486. fp2 = fp;
  7487. }
  7488. dpll = 0;
  7489. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7490. dpll |= DPLLB_MODE_LVDS;
  7491. else
  7492. dpll |= DPLLB_MODE_DAC_SERIAL;
  7493. dpll |= (crtc_state->pixel_multiplier - 1)
  7494. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7495. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7496. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7497. dpll |= DPLL_SDVO_HIGH_SPEED;
  7498. if (intel_crtc_has_dp_encoder(crtc_state))
  7499. dpll |= DPLL_SDVO_HIGH_SPEED;
  7500. /* compute bitmask from p1 value */
  7501. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7502. /* also FPA1 */
  7503. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7504. switch (crtc_state->dpll.p2) {
  7505. case 5:
  7506. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7507. break;
  7508. case 7:
  7509. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7510. break;
  7511. case 10:
  7512. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7513. break;
  7514. case 14:
  7515. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7516. break;
  7517. }
  7518. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7519. intel_panel_use_ssc(dev_priv))
  7520. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7521. else
  7522. dpll |= PLL_REF_INPUT_DREFCLK;
  7523. dpll |= DPLL_VCO_ENABLE;
  7524. crtc_state->dpll_hw_state.dpll = dpll;
  7525. crtc_state->dpll_hw_state.fp0 = fp;
  7526. crtc_state->dpll_hw_state.fp1 = fp2;
  7527. }
  7528. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7529. struct intel_crtc_state *crtc_state)
  7530. {
  7531. struct drm_device *dev = crtc->base.dev;
  7532. struct drm_i915_private *dev_priv = to_i915(dev);
  7533. struct dpll reduced_clock;
  7534. bool has_reduced_clock = false;
  7535. struct intel_shared_dpll *pll;
  7536. const struct intel_limit *limit;
  7537. int refclk = 120000;
  7538. memset(&crtc_state->dpll_hw_state, 0,
  7539. sizeof(crtc_state->dpll_hw_state));
  7540. crtc->lowfreq_avail = false;
  7541. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7542. if (!crtc_state->has_pch_encoder)
  7543. return 0;
  7544. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7545. if (intel_panel_use_ssc(dev_priv)) {
  7546. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7547. dev_priv->vbt.lvds_ssc_freq);
  7548. refclk = dev_priv->vbt.lvds_ssc_freq;
  7549. }
  7550. if (intel_is_dual_link_lvds(dev)) {
  7551. if (refclk == 100000)
  7552. limit = &intel_limits_ironlake_dual_lvds_100m;
  7553. else
  7554. limit = &intel_limits_ironlake_dual_lvds;
  7555. } else {
  7556. if (refclk == 100000)
  7557. limit = &intel_limits_ironlake_single_lvds_100m;
  7558. else
  7559. limit = &intel_limits_ironlake_single_lvds;
  7560. }
  7561. } else {
  7562. limit = &intel_limits_ironlake_dac;
  7563. }
  7564. if (!crtc_state->clock_set &&
  7565. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7566. refclk, NULL, &crtc_state->dpll)) {
  7567. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7568. return -EINVAL;
  7569. }
  7570. ironlake_compute_dpll(crtc, crtc_state,
  7571. has_reduced_clock ? &reduced_clock : NULL);
  7572. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7573. if (pll == NULL) {
  7574. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7575. pipe_name(crtc->pipe));
  7576. return -EINVAL;
  7577. }
  7578. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7579. has_reduced_clock)
  7580. crtc->lowfreq_avail = true;
  7581. return 0;
  7582. }
  7583. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7584. struct intel_link_m_n *m_n)
  7585. {
  7586. struct drm_device *dev = crtc->base.dev;
  7587. struct drm_i915_private *dev_priv = to_i915(dev);
  7588. enum pipe pipe = crtc->pipe;
  7589. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7590. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7591. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7592. & ~TU_SIZE_MASK;
  7593. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7594. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7595. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7596. }
  7597. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7598. enum transcoder transcoder,
  7599. struct intel_link_m_n *m_n,
  7600. struct intel_link_m_n *m2_n2)
  7601. {
  7602. struct drm_device *dev = crtc->base.dev;
  7603. struct drm_i915_private *dev_priv = to_i915(dev);
  7604. enum pipe pipe = crtc->pipe;
  7605. if (INTEL_INFO(dev)->gen >= 5) {
  7606. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7607. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7608. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7609. & ~TU_SIZE_MASK;
  7610. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7611. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7612. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7613. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7614. * gen < 8) and if DRRS is supported (to make sure the
  7615. * registers are not unnecessarily read).
  7616. */
  7617. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7618. crtc->config->has_drrs) {
  7619. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7620. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7621. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7622. & ~TU_SIZE_MASK;
  7623. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7624. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7625. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7626. }
  7627. } else {
  7628. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7629. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7630. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7631. & ~TU_SIZE_MASK;
  7632. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7633. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7634. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7635. }
  7636. }
  7637. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7638. struct intel_crtc_state *pipe_config)
  7639. {
  7640. if (pipe_config->has_pch_encoder)
  7641. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7642. else
  7643. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7644. &pipe_config->dp_m_n,
  7645. &pipe_config->dp_m2_n2);
  7646. }
  7647. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7648. struct intel_crtc_state *pipe_config)
  7649. {
  7650. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7651. &pipe_config->fdi_m_n, NULL);
  7652. }
  7653. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7654. struct intel_crtc_state *pipe_config)
  7655. {
  7656. struct drm_device *dev = crtc->base.dev;
  7657. struct drm_i915_private *dev_priv = to_i915(dev);
  7658. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7659. uint32_t ps_ctrl = 0;
  7660. int id = -1;
  7661. int i;
  7662. /* find scaler attached to this pipe */
  7663. for (i = 0; i < crtc->num_scalers; i++) {
  7664. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7665. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7666. id = i;
  7667. pipe_config->pch_pfit.enabled = true;
  7668. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7669. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7670. break;
  7671. }
  7672. }
  7673. scaler_state->scaler_id = id;
  7674. if (id >= 0) {
  7675. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7676. } else {
  7677. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7678. }
  7679. }
  7680. static void
  7681. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7682. struct intel_initial_plane_config *plane_config)
  7683. {
  7684. struct drm_device *dev = crtc->base.dev;
  7685. struct drm_i915_private *dev_priv = to_i915(dev);
  7686. u32 val, base, offset, stride_mult, tiling;
  7687. int pipe = crtc->pipe;
  7688. int fourcc, pixel_format;
  7689. unsigned int aligned_height;
  7690. struct drm_framebuffer *fb;
  7691. struct intel_framebuffer *intel_fb;
  7692. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7693. if (!intel_fb) {
  7694. DRM_DEBUG_KMS("failed to alloc fb\n");
  7695. return;
  7696. }
  7697. fb = &intel_fb->base;
  7698. val = I915_READ(PLANE_CTL(pipe, 0));
  7699. if (!(val & PLANE_CTL_ENABLE))
  7700. goto error;
  7701. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7702. fourcc = skl_format_to_fourcc(pixel_format,
  7703. val & PLANE_CTL_ORDER_RGBX,
  7704. val & PLANE_CTL_ALPHA_MASK);
  7705. fb->pixel_format = fourcc;
  7706. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7707. tiling = val & PLANE_CTL_TILED_MASK;
  7708. switch (tiling) {
  7709. case PLANE_CTL_TILED_LINEAR:
  7710. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7711. break;
  7712. case PLANE_CTL_TILED_X:
  7713. plane_config->tiling = I915_TILING_X;
  7714. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7715. break;
  7716. case PLANE_CTL_TILED_Y:
  7717. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7718. break;
  7719. case PLANE_CTL_TILED_YF:
  7720. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7721. break;
  7722. default:
  7723. MISSING_CASE(tiling);
  7724. goto error;
  7725. }
  7726. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7727. plane_config->base = base;
  7728. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7729. val = I915_READ(PLANE_SIZE(pipe, 0));
  7730. fb->height = ((val >> 16) & 0xfff) + 1;
  7731. fb->width = ((val >> 0) & 0x1fff) + 1;
  7732. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7733. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7734. fb->pixel_format);
  7735. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7736. aligned_height = intel_fb_align_height(dev, fb->height,
  7737. fb->pixel_format,
  7738. fb->modifier[0]);
  7739. plane_config->size = fb->pitches[0] * aligned_height;
  7740. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7741. pipe_name(pipe), fb->width, fb->height,
  7742. fb->bits_per_pixel, base, fb->pitches[0],
  7743. plane_config->size);
  7744. plane_config->fb = intel_fb;
  7745. return;
  7746. error:
  7747. kfree(fb);
  7748. }
  7749. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7750. struct intel_crtc_state *pipe_config)
  7751. {
  7752. struct drm_device *dev = crtc->base.dev;
  7753. struct drm_i915_private *dev_priv = to_i915(dev);
  7754. uint32_t tmp;
  7755. tmp = I915_READ(PF_CTL(crtc->pipe));
  7756. if (tmp & PF_ENABLE) {
  7757. pipe_config->pch_pfit.enabled = true;
  7758. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7759. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7760. /* We currently do not free assignements of panel fitters on
  7761. * ivb/hsw (since we don't use the higher upscaling modes which
  7762. * differentiates them) so just WARN about this case for now. */
  7763. if (IS_GEN7(dev)) {
  7764. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7765. PF_PIPE_SEL_IVB(crtc->pipe));
  7766. }
  7767. }
  7768. }
  7769. static void
  7770. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7771. struct intel_initial_plane_config *plane_config)
  7772. {
  7773. struct drm_device *dev = crtc->base.dev;
  7774. struct drm_i915_private *dev_priv = to_i915(dev);
  7775. u32 val, base, offset;
  7776. int pipe = crtc->pipe;
  7777. int fourcc, pixel_format;
  7778. unsigned int aligned_height;
  7779. struct drm_framebuffer *fb;
  7780. struct intel_framebuffer *intel_fb;
  7781. val = I915_READ(DSPCNTR(pipe));
  7782. if (!(val & DISPLAY_PLANE_ENABLE))
  7783. return;
  7784. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7785. if (!intel_fb) {
  7786. DRM_DEBUG_KMS("failed to alloc fb\n");
  7787. return;
  7788. }
  7789. fb = &intel_fb->base;
  7790. if (INTEL_INFO(dev)->gen >= 4) {
  7791. if (val & DISPPLANE_TILED) {
  7792. plane_config->tiling = I915_TILING_X;
  7793. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7794. }
  7795. }
  7796. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7797. fourcc = i9xx_format_to_fourcc(pixel_format);
  7798. fb->pixel_format = fourcc;
  7799. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7800. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7801. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7802. offset = I915_READ(DSPOFFSET(pipe));
  7803. } else {
  7804. if (plane_config->tiling)
  7805. offset = I915_READ(DSPTILEOFF(pipe));
  7806. else
  7807. offset = I915_READ(DSPLINOFF(pipe));
  7808. }
  7809. plane_config->base = base;
  7810. val = I915_READ(PIPESRC(pipe));
  7811. fb->width = ((val >> 16) & 0xfff) + 1;
  7812. fb->height = ((val >> 0) & 0xfff) + 1;
  7813. val = I915_READ(DSPSTRIDE(pipe));
  7814. fb->pitches[0] = val & 0xffffffc0;
  7815. aligned_height = intel_fb_align_height(dev, fb->height,
  7816. fb->pixel_format,
  7817. fb->modifier[0]);
  7818. plane_config->size = fb->pitches[0] * aligned_height;
  7819. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7820. pipe_name(pipe), fb->width, fb->height,
  7821. fb->bits_per_pixel, base, fb->pitches[0],
  7822. plane_config->size);
  7823. plane_config->fb = intel_fb;
  7824. }
  7825. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7826. struct intel_crtc_state *pipe_config)
  7827. {
  7828. struct drm_device *dev = crtc->base.dev;
  7829. struct drm_i915_private *dev_priv = to_i915(dev);
  7830. enum intel_display_power_domain power_domain;
  7831. uint32_t tmp;
  7832. bool ret;
  7833. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7834. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7835. return false;
  7836. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7837. pipe_config->shared_dpll = NULL;
  7838. ret = false;
  7839. tmp = I915_READ(PIPECONF(crtc->pipe));
  7840. if (!(tmp & PIPECONF_ENABLE))
  7841. goto out;
  7842. switch (tmp & PIPECONF_BPC_MASK) {
  7843. case PIPECONF_6BPC:
  7844. pipe_config->pipe_bpp = 18;
  7845. break;
  7846. case PIPECONF_8BPC:
  7847. pipe_config->pipe_bpp = 24;
  7848. break;
  7849. case PIPECONF_10BPC:
  7850. pipe_config->pipe_bpp = 30;
  7851. break;
  7852. case PIPECONF_12BPC:
  7853. pipe_config->pipe_bpp = 36;
  7854. break;
  7855. default:
  7856. break;
  7857. }
  7858. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7859. pipe_config->limited_color_range = true;
  7860. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7861. struct intel_shared_dpll *pll;
  7862. enum intel_dpll_id pll_id;
  7863. pipe_config->has_pch_encoder = true;
  7864. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7865. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7866. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7867. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7868. if (HAS_PCH_IBX(dev_priv)) {
  7869. /*
  7870. * The pipe->pch transcoder and pch transcoder->pll
  7871. * mapping is fixed.
  7872. */
  7873. pll_id = (enum intel_dpll_id) crtc->pipe;
  7874. } else {
  7875. tmp = I915_READ(PCH_DPLL_SEL);
  7876. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7877. pll_id = DPLL_ID_PCH_PLL_B;
  7878. else
  7879. pll_id= DPLL_ID_PCH_PLL_A;
  7880. }
  7881. pipe_config->shared_dpll =
  7882. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7883. pll = pipe_config->shared_dpll;
  7884. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7885. &pipe_config->dpll_hw_state));
  7886. tmp = pipe_config->dpll_hw_state.dpll;
  7887. pipe_config->pixel_multiplier =
  7888. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7889. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7890. ironlake_pch_clock_get(crtc, pipe_config);
  7891. } else {
  7892. pipe_config->pixel_multiplier = 1;
  7893. }
  7894. intel_get_pipe_timings(crtc, pipe_config);
  7895. intel_get_pipe_src_size(crtc, pipe_config);
  7896. ironlake_get_pfit_config(crtc, pipe_config);
  7897. ret = true;
  7898. out:
  7899. intel_display_power_put(dev_priv, power_domain);
  7900. return ret;
  7901. }
  7902. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7903. {
  7904. struct drm_device *dev = &dev_priv->drm;
  7905. struct intel_crtc *crtc;
  7906. for_each_intel_crtc(dev, crtc)
  7907. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7908. pipe_name(crtc->pipe));
  7909. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7910. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7911. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7912. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7913. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7914. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7915. "CPU PWM1 enabled\n");
  7916. if (IS_HASWELL(dev))
  7917. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7918. "CPU PWM2 enabled\n");
  7919. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7920. "PCH PWM1 enabled\n");
  7921. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7922. "Utility pin enabled\n");
  7923. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7924. /*
  7925. * In theory we can still leave IRQs enabled, as long as only the HPD
  7926. * interrupts remain enabled. We used to check for that, but since it's
  7927. * gen-specific and since we only disable LCPLL after we fully disable
  7928. * the interrupts, the check below should be enough.
  7929. */
  7930. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7931. }
  7932. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7933. {
  7934. struct drm_device *dev = &dev_priv->drm;
  7935. if (IS_HASWELL(dev))
  7936. return I915_READ(D_COMP_HSW);
  7937. else
  7938. return I915_READ(D_COMP_BDW);
  7939. }
  7940. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7941. {
  7942. struct drm_device *dev = &dev_priv->drm;
  7943. if (IS_HASWELL(dev)) {
  7944. mutex_lock(&dev_priv->rps.hw_lock);
  7945. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7946. val))
  7947. DRM_ERROR("Failed to write to D_COMP\n");
  7948. mutex_unlock(&dev_priv->rps.hw_lock);
  7949. } else {
  7950. I915_WRITE(D_COMP_BDW, val);
  7951. POSTING_READ(D_COMP_BDW);
  7952. }
  7953. }
  7954. /*
  7955. * This function implements pieces of two sequences from BSpec:
  7956. * - Sequence for display software to disable LCPLL
  7957. * - Sequence for display software to allow package C8+
  7958. * The steps implemented here are just the steps that actually touch the LCPLL
  7959. * register. Callers should take care of disabling all the display engine
  7960. * functions, doing the mode unset, fixing interrupts, etc.
  7961. */
  7962. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7963. bool switch_to_fclk, bool allow_power_down)
  7964. {
  7965. uint32_t val;
  7966. assert_can_disable_lcpll(dev_priv);
  7967. val = I915_READ(LCPLL_CTL);
  7968. if (switch_to_fclk) {
  7969. val |= LCPLL_CD_SOURCE_FCLK;
  7970. I915_WRITE(LCPLL_CTL, val);
  7971. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7972. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7973. DRM_ERROR("Switching to FCLK failed\n");
  7974. val = I915_READ(LCPLL_CTL);
  7975. }
  7976. val |= LCPLL_PLL_DISABLE;
  7977. I915_WRITE(LCPLL_CTL, val);
  7978. POSTING_READ(LCPLL_CTL);
  7979. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7980. DRM_ERROR("LCPLL still locked\n");
  7981. val = hsw_read_dcomp(dev_priv);
  7982. val |= D_COMP_COMP_DISABLE;
  7983. hsw_write_dcomp(dev_priv, val);
  7984. ndelay(100);
  7985. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7986. 1))
  7987. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7988. if (allow_power_down) {
  7989. val = I915_READ(LCPLL_CTL);
  7990. val |= LCPLL_POWER_DOWN_ALLOW;
  7991. I915_WRITE(LCPLL_CTL, val);
  7992. POSTING_READ(LCPLL_CTL);
  7993. }
  7994. }
  7995. /*
  7996. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7997. * source.
  7998. */
  7999. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8000. {
  8001. uint32_t val;
  8002. val = I915_READ(LCPLL_CTL);
  8003. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8004. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8005. return;
  8006. /*
  8007. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8008. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8009. */
  8010. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8011. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8012. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8013. I915_WRITE(LCPLL_CTL, val);
  8014. POSTING_READ(LCPLL_CTL);
  8015. }
  8016. val = hsw_read_dcomp(dev_priv);
  8017. val |= D_COMP_COMP_FORCE;
  8018. val &= ~D_COMP_COMP_DISABLE;
  8019. hsw_write_dcomp(dev_priv, val);
  8020. val = I915_READ(LCPLL_CTL);
  8021. val &= ~LCPLL_PLL_DISABLE;
  8022. I915_WRITE(LCPLL_CTL, val);
  8023. if (intel_wait_for_register(dev_priv,
  8024. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8025. 5))
  8026. DRM_ERROR("LCPLL not locked yet\n");
  8027. if (val & LCPLL_CD_SOURCE_FCLK) {
  8028. val = I915_READ(LCPLL_CTL);
  8029. val &= ~LCPLL_CD_SOURCE_FCLK;
  8030. I915_WRITE(LCPLL_CTL, val);
  8031. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8032. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8033. DRM_ERROR("Switching back to LCPLL failed\n");
  8034. }
  8035. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8036. intel_update_cdclk(&dev_priv->drm);
  8037. }
  8038. /*
  8039. * Package states C8 and deeper are really deep PC states that can only be
  8040. * reached when all the devices on the system allow it, so even if the graphics
  8041. * device allows PC8+, it doesn't mean the system will actually get to these
  8042. * states. Our driver only allows PC8+ when going into runtime PM.
  8043. *
  8044. * The requirements for PC8+ are that all the outputs are disabled, the power
  8045. * well is disabled and most interrupts are disabled, and these are also
  8046. * requirements for runtime PM. When these conditions are met, we manually do
  8047. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8048. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8049. * hang the machine.
  8050. *
  8051. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8052. * the state of some registers, so when we come back from PC8+ we need to
  8053. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8054. * need to take care of the registers kept by RC6. Notice that this happens even
  8055. * if we don't put the device in PCI D3 state (which is what currently happens
  8056. * because of the runtime PM support).
  8057. *
  8058. * For more, read "Display Sequences for Package C8" on the hardware
  8059. * documentation.
  8060. */
  8061. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8062. {
  8063. struct drm_device *dev = &dev_priv->drm;
  8064. uint32_t val;
  8065. DRM_DEBUG_KMS("Enabling package C8+\n");
  8066. if (HAS_PCH_LPT_LP(dev)) {
  8067. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8068. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8069. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8070. }
  8071. lpt_disable_clkout_dp(dev);
  8072. hsw_disable_lcpll(dev_priv, true, true);
  8073. }
  8074. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8075. {
  8076. struct drm_device *dev = &dev_priv->drm;
  8077. uint32_t val;
  8078. DRM_DEBUG_KMS("Disabling package C8+\n");
  8079. hsw_restore_lcpll(dev_priv);
  8080. lpt_init_pch_refclk(dev);
  8081. if (HAS_PCH_LPT_LP(dev)) {
  8082. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8083. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8084. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8085. }
  8086. }
  8087. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8088. {
  8089. struct drm_device *dev = old_state->dev;
  8090. struct intel_atomic_state *old_intel_state =
  8091. to_intel_atomic_state(old_state);
  8092. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8093. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8094. }
  8095. /* compute the max rate for new configuration */
  8096. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8097. {
  8098. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8099. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8100. struct drm_crtc *crtc;
  8101. struct drm_crtc_state *cstate;
  8102. struct intel_crtc_state *crtc_state;
  8103. unsigned max_pixel_rate = 0, i;
  8104. enum pipe pipe;
  8105. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8106. sizeof(intel_state->min_pixclk));
  8107. for_each_crtc_in_state(state, crtc, cstate, i) {
  8108. int pixel_rate;
  8109. crtc_state = to_intel_crtc_state(cstate);
  8110. if (!crtc_state->base.enable) {
  8111. intel_state->min_pixclk[i] = 0;
  8112. continue;
  8113. }
  8114. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8115. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8116. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8117. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8118. intel_state->min_pixclk[i] = pixel_rate;
  8119. }
  8120. for_each_pipe(dev_priv, pipe)
  8121. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8122. return max_pixel_rate;
  8123. }
  8124. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8125. {
  8126. struct drm_i915_private *dev_priv = to_i915(dev);
  8127. uint32_t val, data;
  8128. int ret;
  8129. if (WARN((I915_READ(LCPLL_CTL) &
  8130. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8131. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8132. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8133. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8134. "trying to change cdclk frequency with cdclk not enabled\n"))
  8135. return;
  8136. mutex_lock(&dev_priv->rps.hw_lock);
  8137. ret = sandybridge_pcode_write(dev_priv,
  8138. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8139. mutex_unlock(&dev_priv->rps.hw_lock);
  8140. if (ret) {
  8141. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8142. return;
  8143. }
  8144. val = I915_READ(LCPLL_CTL);
  8145. val |= LCPLL_CD_SOURCE_FCLK;
  8146. I915_WRITE(LCPLL_CTL, val);
  8147. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8148. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8149. DRM_ERROR("Switching to FCLK failed\n");
  8150. val = I915_READ(LCPLL_CTL);
  8151. val &= ~LCPLL_CLK_FREQ_MASK;
  8152. switch (cdclk) {
  8153. case 450000:
  8154. val |= LCPLL_CLK_FREQ_450;
  8155. data = 0;
  8156. break;
  8157. case 540000:
  8158. val |= LCPLL_CLK_FREQ_54O_BDW;
  8159. data = 1;
  8160. break;
  8161. case 337500:
  8162. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8163. data = 2;
  8164. break;
  8165. case 675000:
  8166. val |= LCPLL_CLK_FREQ_675_BDW;
  8167. data = 3;
  8168. break;
  8169. default:
  8170. WARN(1, "invalid cdclk frequency\n");
  8171. return;
  8172. }
  8173. I915_WRITE(LCPLL_CTL, val);
  8174. val = I915_READ(LCPLL_CTL);
  8175. val &= ~LCPLL_CD_SOURCE_FCLK;
  8176. I915_WRITE(LCPLL_CTL, val);
  8177. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8178. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8179. DRM_ERROR("Switching back to LCPLL failed\n");
  8180. mutex_lock(&dev_priv->rps.hw_lock);
  8181. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8182. mutex_unlock(&dev_priv->rps.hw_lock);
  8183. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8184. intel_update_cdclk(dev);
  8185. WARN(cdclk != dev_priv->cdclk_freq,
  8186. "cdclk requested %d kHz but got %d kHz\n",
  8187. cdclk, dev_priv->cdclk_freq);
  8188. }
  8189. static int broadwell_calc_cdclk(int max_pixclk)
  8190. {
  8191. if (max_pixclk > 540000)
  8192. return 675000;
  8193. else if (max_pixclk > 450000)
  8194. return 540000;
  8195. else if (max_pixclk > 337500)
  8196. return 450000;
  8197. else
  8198. return 337500;
  8199. }
  8200. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8201. {
  8202. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8203. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8204. int max_pixclk = ilk_max_pixel_rate(state);
  8205. int cdclk;
  8206. /*
  8207. * FIXME should also account for plane ratio
  8208. * once 64bpp pixel formats are supported.
  8209. */
  8210. cdclk = broadwell_calc_cdclk(max_pixclk);
  8211. if (cdclk > dev_priv->max_cdclk_freq) {
  8212. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8213. cdclk, dev_priv->max_cdclk_freq);
  8214. return -EINVAL;
  8215. }
  8216. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8217. if (!intel_state->active_crtcs)
  8218. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8219. return 0;
  8220. }
  8221. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8222. {
  8223. struct drm_device *dev = old_state->dev;
  8224. struct intel_atomic_state *old_intel_state =
  8225. to_intel_atomic_state(old_state);
  8226. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8227. broadwell_set_cdclk(dev, req_cdclk);
  8228. }
  8229. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8230. {
  8231. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8232. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8233. const int max_pixclk = ilk_max_pixel_rate(state);
  8234. int vco = intel_state->cdclk_pll_vco;
  8235. int cdclk;
  8236. /*
  8237. * FIXME should also account for plane ratio
  8238. * once 64bpp pixel formats are supported.
  8239. */
  8240. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8241. /*
  8242. * FIXME move the cdclk caclulation to
  8243. * compute_config() so we can fail gracegully.
  8244. */
  8245. if (cdclk > dev_priv->max_cdclk_freq) {
  8246. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8247. cdclk, dev_priv->max_cdclk_freq);
  8248. cdclk = dev_priv->max_cdclk_freq;
  8249. }
  8250. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8251. if (!intel_state->active_crtcs)
  8252. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8253. return 0;
  8254. }
  8255. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8256. {
  8257. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8258. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8259. unsigned int req_cdclk = intel_state->dev_cdclk;
  8260. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8261. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8262. }
  8263. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8264. struct intel_crtc_state *crtc_state)
  8265. {
  8266. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8267. if (!intel_ddi_pll_select(crtc, crtc_state))
  8268. return -EINVAL;
  8269. }
  8270. crtc->lowfreq_avail = false;
  8271. return 0;
  8272. }
  8273. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8274. enum port port,
  8275. struct intel_crtc_state *pipe_config)
  8276. {
  8277. enum intel_dpll_id id;
  8278. switch (port) {
  8279. case PORT_A:
  8280. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8281. id = DPLL_ID_SKL_DPLL0;
  8282. break;
  8283. case PORT_B:
  8284. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8285. id = DPLL_ID_SKL_DPLL1;
  8286. break;
  8287. case PORT_C:
  8288. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8289. id = DPLL_ID_SKL_DPLL2;
  8290. break;
  8291. default:
  8292. DRM_ERROR("Incorrect port type\n");
  8293. return;
  8294. }
  8295. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8296. }
  8297. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8298. enum port port,
  8299. struct intel_crtc_state *pipe_config)
  8300. {
  8301. enum intel_dpll_id id;
  8302. u32 temp;
  8303. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8304. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8305. switch (pipe_config->ddi_pll_sel) {
  8306. case SKL_DPLL0:
  8307. id = DPLL_ID_SKL_DPLL0;
  8308. break;
  8309. case SKL_DPLL1:
  8310. id = DPLL_ID_SKL_DPLL1;
  8311. break;
  8312. case SKL_DPLL2:
  8313. id = DPLL_ID_SKL_DPLL2;
  8314. break;
  8315. case SKL_DPLL3:
  8316. id = DPLL_ID_SKL_DPLL3;
  8317. break;
  8318. default:
  8319. MISSING_CASE(pipe_config->ddi_pll_sel);
  8320. return;
  8321. }
  8322. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8323. }
  8324. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8325. enum port port,
  8326. struct intel_crtc_state *pipe_config)
  8327. {
  8328. enum intel_dpll_id id;
  8329. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8330. switch (pipe_config->ddi_pll_sel) {
  8331. case PORT_CLK_SEL_WRPLL1:
  8332. id = DPLL_ID_WRPLL1;
  8333. break;
  8334. case PORT_CLK_SEL_WRPLL2:
  8335. id = DPLL_ID_WRPLL2;
  8336. break;
  8337. case PORT_CLK_SEL_SPLL:
  8338. id = DPLL_ID_SPLL;
  8339. break;
  8340. case PORT_CLK_SEL_LCPLL_810:
  8341. id = DPLL_ID_LCPLL_810;
  8342. break;
  8343. case PORT_CLK_SEL_LCPLL_1350:
  8344. id = DPLL_ID_LCPLL_1350;
  8345. break;
  8346. case PORT_CLK_SEL_LCPLL_2700:
  8347. id = DPLL_ID_LCPLL_2700;
  8348. break;
  8349. default:
  8350. MISSING_CASE(pipe_config->ddi_pll_sel);
  8351. /* fall through */
  8352. case PORT_CLK_SEL_NONE:
  8353. return;
  8354. }
  8355. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8356. }
  8357. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8358. struct intel_crtc_state *pipe_config,
  8359. unsigned long *power_domain_mask)
  8360. {
  8361. struct drm_device *dev = crtc->base.dev;
  8362. struct drm_i915_private *dev_priv = to_i915(dev);
  8363. enum intel_display_power_domain power_domain;
  8364. u32 tmp;
  8365. /*
  8366. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8367. * transcoder handled below.
  8368. */
  8369. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8370. /*
  8371. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8372. * consistency and less surprising code; it's in always on power).
  8373. */
  8374. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8375. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8376. enum pipe trans_edp_pipe;
  8377. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8378. default:
  8379. WARN(1, "unknown pipe linked to edp transcoder\n");
  8380. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8381. case TRANS_DDI_EDP_INPUT_A_ON:
  8382. trans_edp_pipe = PIPE_A;
  8383. break;
  8384. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8385. trans_edp_pipe = PIPE_B;
  8386. break;
  8387. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8388. trans_edp_pipe = PIPE_C;
  8389. break;
  8390. }
  8391. if (trans_edp_pipe == crtc->pipe)
  8392. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8393. }
  8394. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8395. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8396. return false;
  8397. *power_domain_mask |= BIT(power_domain);
  8398. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8399. return tmp & PIPECONF_ENABLE;
  8400. }
  8401. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8402. struct intel_crtc_state *pipe_config,
  8403. unsigned long *power_domain_mask)
  8404. {
  8405. struct drm_device *dev = crtc->base.dev;
  8406. struct drm_i915_private *dev_priv = to_i915(dev);
  8407. enum intel_display_power_domain power_domain;
  8408. enum port port;
  8409. enum transcoder cpu_transcoder;
  8410. u32 tmp;
  8411. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8412. if (port == PORT_A)
  8413. cpu_transcoder = TRANSCODER_DSI_A;
  8414. else
  8415. cpu_transcoder = TRANSCODER_DSI_C;
  8416. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8417. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8418. continue;
  8419. *power_domain_mask |= BIT(power_domain);
  8420. /*
  8421. * The PLL needs to be enabled with a valid divider
  8422. * configuration, otherwise accessing DSI registers will hang
  8423. * the machine. See BSpec North Display Engine
  8424. * registers/MIPI[BXT]. We can break out here early, since we
  8425. * need the same DSI PLL to be enabled for both DSI ports.
  8426. */
  8427. if (!intel_dsi_pll_is_enabled(dev_priv))
  8428. break;
  8429. /* XXX: this works for video mode only */
  8430. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8431. if (!(tmp & DPI_ENABLE))
  8432. continue;
  8433. tmp = I915_READ(MIPI_CTRL(port));
  8434. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8435. continue;
  8436. pipe_config->cpu_transcoder = cpu_transcoder;
  8437. break;
  8438. }
  8439. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8440. }
  8441. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8442. struct intel_crtc_state *pipe_config)
  8443. {
  8444. struct drm_device *dev = crtc->base.dev;
  8445. struct drm_i915_private *dev_priv = to_i915(dev);
  8446. struct intel_shared_dpll *pll;
  8447. enum port port;
  8448. uint32_t tmp;
  8449. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8450. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8451. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8452. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8453. else if (IS_BROXTON(dev))
  8454. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8455. else
  8456. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8457. pll = pipe_config->shared_dpll;
  8458. if (pll) {
  8459. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8460. &pipe_config->dpll_hw_state));
  8461. }
  8462. /*
  8463. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8464. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8465. * the PCH transcoder is on.
  8466. */
  8467. if (INTEL_INFO(dev)->gen < 9 &&
  8468. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8469. pipe_config->has_pch_encoder = true;
  8470. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8471. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8472. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8473. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8474. }
  8475. }
  8476. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8477. struct intel_crtc_state *pipe_config)
  8478. {
  8479. struct drm_device *dev = crtc->base.dev;
  8480. struct drm_i915_private *dev_priv = to_i915(dev);
  8481. enum intel_display_power_domain power_domain;
  8482. unsigned long power_domain_mask;
  8483. bool active;
  8484. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8485. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8486. return false;
  8487. power_domain_mask = BIT(power_domain);
  8488. pipe_config->shared_dpll = NULL;
  8489. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8490. if (IS_BROXTON(dev_priv) &&
  8491. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8492. WARN_ON(active);
  8493. active = true;
  8494. }
  8495. if (!active)
  8496. goto out;
  8497. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8498. haswell_get_ddi_port_state(crtc, pipe_config);
  8499. intel_get_pipe_timings(crtc, pipe_config);
  8500. }
  8501. intel_get_pipe_src_size(crtc, pipe_config);
  8502. pipe_config->gamma_mode =
  8503. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8504. if (INTEL_INFO(dev)->gen >= 9) {
  8505. skl_init_scalers(dev, crtc, pipe_config);
  8506. }
  8507. if (INTEL_INFO(dev)->gen >= 9) {
  8508. pipe_config->scaler_state.scaler_id = -1;
  8509. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8510. }
  8511. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8512. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8513. power_domain_mask |= BIT(power_domain);
  8514. if (INTEL_INFO(dev)->gen >= 9)
  8515. skylake_get_pfit_config(crtc, pipe_config);
  8516. else
  8517. ironlake_get_pfit_config(crtc, pipe_config);
  8518. }
  8519. if (IS_HASWELL(dev))
  8520. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8521. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8522. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8523. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8524. pipe_config->pixel_multiplier =
  8525. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8526. } else {
  8527. pipe_config->pixel_multiplier = 1;
  8528. }
  8529. out:
  8530. for_each_power_domain(power_domain, power_domain_mask)
  8531. intel_display_power_put(dev_priv, power_domain);
  8532. return active;
  8533. }
  8534. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8535. const struct intel_plane_state *plane_state)
  8536. {
  8537. struct drm_device *dev = crtc->dev;
  8538. struct drm_i915_private *dev_priv = to_i915(dev);
  8539. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8540. uint32_t cntl = 0, size = 0;
  8541. if (plane_state && plane_state->visible) {
  8542. unsigned int width = plane_state->base.crtc_w;
  8543. unsigned int height = plane_state->base.crtc_h;
  8544. unsigned int stride = roundup_pow_of_two(width) * 4;
  8545. switch (stride) {
  8546. default:
  8547. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8548. width, stride);
  8549. stride = 256;
  8550. /* fallthrough */
  8551. case 256:
  8552. case 512:
  8553. case 1024:
  8554. case 2048:
  8555. break;
  8556. }
  8557. cntl |= CURSOR_ENABLE |
  8558. CURSOR_GAMMA_ENABLE |
  8559. CURSOR_FORMAT_ARGB |
  8560. CURSOR_STRIDE(stride);
  8561. size = (height << 12) | width;
  8562. }
  8563. if (intel_crtc->cursor_cntl != 0 &&
  8564. (intel_crtc->cursor_base != base ||
  8565. intel_crtc->cursor_size != size ||
  8566. intel_crtc->cursor_cntl != cntl)) {
  8567. /* On these chipsets we can only modify the base/size/stride
  8568. * whilst the cursor is disabled.
  8569. */
  8570. I915_WRITE(CURCNTR(PIPE_A), 0);
  8571. POSTING_READ(CURCNTR(PIPE_A));
  8572. intel_crtc->cursor_cntl = 0;
  8573. }
  8574. if (intel_crtc->cursor_base != base) {
  8575. I915_WRITE(CURBASE(PIPE_A), base);
  8576. intel_crtc->cursor_base = base;
  8577. }
  8578. if (intel_crtc->cursor_size != size) {
  8579. I915_WRITE(CURSIZE, size);
  8580. intel_crtc->cursor_size = size;
  8581. }
  8582. if (intel_crtc->cursor_cntl != cntl) {
  8583. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8584. POSTING_READ(CURCNTR(PIPE_A));
  8585. intel_crtc->cursor_cntl = cntl;
  8586. }
  8587. }
  8588. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8589. const struct intel_plane_state *plane_state)
  8590. {
  8591. struct drm_device *dev = crtc->dev;
  8592. struct drm_i915_private *dev_priv = to_i915(dev);
  8593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8594. int pipe = intel_crtc->pipe;
  8595. uint32_t cntl = 0;
  8596. if (plane_state && plane_state->visible) {
  8597. cntl = MCURSOR_GAMMA_ENABLE;
  8598. switch (plane_state->base.crtc_w) {
  8599. case 64:
  8600. cntl |= CURSOR_MODE_64_ARGB_AX;
  8601. break;
  8602. case 128:
  8603. cntl |= CURSOR_MODE_128_ARGB_AX;
  8604. break;
  8605. case 256:
  8606. cntl |= CURSOR_MODE_256_ARGB_AX;
  8607. break;
  8608. default:
  8609. MISSING_CASE(plane_state->base.crtc_w);
  8610. return;
  8611. }
  8612. cntl |= pipe << 28; /* Connect to correct pipe */
  8613. if (HAS_DDI(dev))
  8614. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8615. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8616. cntl |= CURSOR_ROTATE_180;
  8617. }
  8618. if (intel_crtc->cursor_cntl != cntl) {
  8619. I915_WRITE(CURCNTR(pipe), cntl);
  8620. POSTING_READ(CURCNTR(pipe));
  8621. intel_crtc->cursor_cntl = cntl;
  8622. }
  8623. /* and commit changes on next vblank */
  8624. I915_WRITE(CURBASE(pipe), base);
  8625. POSTING_READ(CURBASE(pipe));
  8626. intel_crtc->cursor_base = base;
  8627. }
  8628. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8629. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8630. const struct intel_plane_state *plane_state)
  8631. {
  8632. struct drm_device *dev = crtc->dev;
  8633. struct drm_i915_private *dev_priv = to_i915(dev);
  8634. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8635. int pipe = intel_crtc->pipe;
  8636. u32 base = intel_crtc->cursor_addr;
  8637. u32 pos = 0;
  8638. if (plane_state) {
  8639. int x = plane_state->base.crtc_x;
  8640. int y = plane_state->base.crtc_y;
  8641. if (x < 0) {
  8642. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8643. x = -x;
  8644. }
  8645. pos |= x << CURSOR_X_SHIFT;
  8646. if (y < 0) {
  8647. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8648. y = -y;
  8649. }
  8650. pos |= y << CURSOR_Y_SHIFT;
  8651. /* ILK+ do this automagically */
  8652. if (HAS_GMCH_DISPLAY(dev) &&
  8653. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8654. base += (plane_state->base.crtc_h *
  8655. plane_state->base.crtc_w - 1) * 4;
  8656. }
  8657. }
  8658. I915_WRITE(CURPOS(pipe), pos);
  8659. if (IS_845G(dev) || IS_I865G(dev))
  8660. i845_update_cursor(crtc, base, plane_state);
  8661. else
  8662. i9xx_update_cursor(crtc, base, plane_state);
  8663. }
  8664. static bool cursor_size_ok(struct drm_device *dev,
  8665. uint32_t width, uint32_t height)
  8666. {
  8667. if (width == 0 || height == 0)
  8668. return false;
  8669. /*
  8670. * 845g/865g are special in that they are only limited by
  8671. * the width of their cursors, the height is arbitrary up to
  8672. * the precision of the register. Everything else requires
  8673. * square cursors, limited to a few power-of-two sizes.
  8674. */
  8675. if (IS_845G(dev) || IS_I865G(dev)) {
  8676. if ((width & 63) != 0)
  8677. return false;
  8678. if (width > (IS_845G(dev) ? 64 : 512))
  8679. return false;
  8680. if (height > 1023)
  8681. return false;
  8682. } else {
  8683. switch (width | height) {
  8684. case 256:
  8685. case 128:
  8686. if (IS_GEN2(dev))
  8687. return false;
  8688. case 64:
  8689. break;
  8690. default:
  8691. return false;
  8692. }
  8693. }
  8694. return true;
  8695. }
  8696. /* VESA 640x480x72Hz mode to set on the pipe */
  8697. static struct drm_display_mode load_detect_mode = {
  8698. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8699. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8700. };
  8701. struct drm_framebuffer *
  8702. __intel_framebuffer_create(struct drm_device *dev,
  8703. struct drm_mode_fb_cmd2 *mode_cmd,
  8704. struct drm_i915_gem_object *obj)
  8705. {
  8706. struct intel_framebuffer *intel_fb;
  8707. int ret;
  8708. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8709. if (!intel_fb)
  8710. return ERR_PTR(-ENOMEM);
  8711. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8712. if (ret)
  8713. goto err;
  8714. return &intel_fb->base;
  8715. err:
  8716. kfree(intel_fb);
  8717. return ERR_PTR(ret);
  8718. }
  8719. static struct drm_framebuffer *
  8720. intel_framebuffer_create(struct drm_device *dev,
  8721. struct drm_mode_fb_cmd2 *mode_cmd,
  8722. struct drm_i915_gem_object *obj)
  8723. {
  8724. struct drm_framebuffer *fb;
  8725. int ret;
  8726. ret = i915_mutex_lock_interruptible(dev);
  8727. if (ret)
  8728. return ERR_PTR(ret);
  8729. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8730. mutex_unlock(&dev->struct_mutex);
  8731. return fb;
  8732. }
  8733. static u32
  8734. intel_framebuffer_pitch_for_width(int width, int bpp)
  8735. {
  8736. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8737. return ALIGN(pitch, 64);
  8738. }
  8739. static u32
  8740. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8741. {
  8742. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8743. return PAGE_ALIGN(pitch * mode->vdisplay);
  8744. }
  8745. static struct drm_framebuffer *
  8746. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8747. struct drm_display_mode *mode,
  8748. int depth, int bpp)
  8749. {
  8750. struct drm_framebuffer *fb;
  8751. struct drm_i915_gem_object *obj;
  8752. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8753. obj = i915_gem_object_create(dev,
  8754. intel_framebuffer_size_for_mode(mode, bpp));
  8755. if (IS_ERR(obj))
  8756. return ERR_CAST(obj);
  8757. mode_cmd.width = mode->hdisplay;
  8758. mode_cmd.height = mode->vdisplay;
  8759. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8760. bpp);
  8761. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8762. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8763. if (IS_ERR(fb))
  8764. drm_gem_object_unreference_unlocked(&obj->base);
  8765. return fb;
  8766. }
  8767. static struct drm_framebuffer *
  8768. mode_fits_in_fbdev(struct drm_device *dev,
  8769. struct drm_display_mode *mode)
  8770. {
  8771. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8772. struct drm_i915_private *dev_priv = to_i915(dev);
  8773. struct drm_i915_gem_object *obj;
  8774. struct drm_framebuffer *fb;
  8775. if (!dev_priv->fbdev)
  8776. return NULL;
  8777. if (!dev_priv->fbdev->fb)
  8778. return NULL;
  8779. obj = dev_priv->fbdev->fb->obj;
  8780. BUG_ON(!obj);
  8781. fb = &dev_priv->fbdev->fb->base;
  8782. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8783. fb->bits_per_pixel))
  8784. return NULL;
  8785. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8786. return NULL;
  8787. drm_framebuffer_reference(fb);
  8788. return fb;
  8789. #else
  8790. return NULL;
  8791. #endif
  8792. }
  8793. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8794. struct drm_crtc *crtc,
  8795. struct drm_display_mode *mode,
  8796. struct drm_framebuffer *fb,
  8797. int x, int y)
  8798. {
  8799. struct drm_plane_state *plane_state;
  8800. int hdisplay, vdisplay;
  8801. int ret;
  8802. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8803. if (IS_ERR(plane_state))
  8804. return PTR_ERR(plane_state);
  8805. if (mode)
  8806. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8807. else
  8808. hdisplay = vdisplay = 0;
  8809. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8810. if (ret)
  8811. return ret;
  8812. drm_atomic_set_fb_for_plane(plane_state, fb);
  8813. plane_state->crtc_x = 0;
  8814. plane_state->crtc_y = 0;
  8815. plane_state->crtc_w = hdisplay;
  8816. plane_state->crtc_h = vdisplay;
  8817. plane_state->src_x = x << 16;
  8818. plane_state->src_y = y << 16;
  8819. plane_state->src_w = hdisplay << 16;
  8820. plane_state->src_h = vdisplay << 16;
  8821. return 0;
  8822. }
  8823. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8824. struct drm_display_mode *mode,
  8825. struct intel_load_detect_pipe *old,
  8826. struct drm_modeset_acquire_ctx *ctx)
  8827. {
  8828. struct intel_crtc *intel_crtc;
  8829. struct intel_encoder *intel_encoder =
  8830. intel_attached_encoder(connector);
  8831. struct drm_crtc *possible_crtc;
  8832. struct drm_encoder *encoder = &intel_encoder->base;
  8833. struct drm_crtc *crtc = NULL;
  8834. struct drm_device *dev = encoder->dev;
  8835. struct drm_framebuffer *fb;
  8836. struct drm_mode_config *config = &dev->mode_config;
  8837. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8838. struct drm_connector_state *connector_state;
  8839. struct intel_crtc_state *crtc_state;
  8840. int ret, i = -1;
  8841. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8842. connector->base.id, connector->name,
  8843. encoder->base.id, encoder->name);
  8844. old->restore_state = NULL;
  8845. retry:
  8846. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8847. if (ret)
  8848. goto fail;
  8849. /*
  8850. * Algorithm gets a little messy:
  8851. *
  8852. * - if the connector already has an assigned crtc, use it (but make
  8853. * sure it's on first)
  8854. *
  8855. * - try to find the first unused crtc that can drive this connector,
  8856. * and use that if we find one
  8857. */
  8858. /* See if we already have a CRTC for this connector */
  8859. if (connector->state->crtc) {
  8860. crtc = connector->state->crtc;
  8861. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8862. if (ret)
  8863. goto fail;
  8864. /* Make sure the crtc and connector are running */
  8865. goto found;
  8866. }
  8867. /* Find an unused one (if possible) */
  8868. for_each_crtc(dev, possible_crtc) {
  8869. i++;
  8870. if (!(encoder->possible_crtcs & (1 << i)))
  8871. continue;
  8872. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8873. if (ret)
  8874. goto fail;
  8875. if (possible_crtc->state->enable) {
  8876. drm_modeset_unlock(&possible_crtc->mutex);
  8877. continue;
  8878. }
  8879. crtc = possible_crtc;
  8880. break;
  8881. }
  8882. /*
  8883. * If we didn't find an unused CRTC, don't use any.
  8884. */
  8885. if (!crtc) {
  8886. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8887. goto fail;
  8888. }
  8889. found:
  8890. intel_crtc = to_intel_crtc(crtc);
  8891. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8892. if (ret)
  8893. goto fail;
  8894. state = drm_atomic_state_alloc(dev);
  8895. restore_state = drm_atomic_state_alloc(dev);
  8896. if (!state || !restore_state) {
  8897. ret = -ENOMEM;
  8898. goto fail;
  8899. }
  8900. state->acquire_ctx = ctx;
  8901. restore_state->acquire_ctx = ctx;
  8902. connector_state = drm_atomic_get_connector_state(state, connector);
  8903. if (IS_ERR(connector_state)) {
  8904. ret = PTR_ERR(connector_state);
  8905. goto fail;
  8906. }
  8907. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8908. if (ret)
  8909. goto fail;
  8910. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8911. if (IS_ERR(crtc_state)) {
  8912. ret = PTR_ERR(crtc_state);
  8913. goto fail;
  8914. }
  8915. crtc_state->base.active = crtc_state->base.enable = true;
  8916. if (!mode)
  8917. mode = &load_detect_mode;
  8918. /* We need a framebuffer large enough to accommodate all accesses
  8919. * that the plane may generate whilst we perform load detection.
  8920. * We can not rely on the fbcon either being present (we get called
  8921. * during its initialisation to detect all boot displays, or it may
  8922. * not even exist) or that it is large enough to satisfy the
  8923. * requested mode.
  8924. */
  8925. fb = mode_fits_in_fbdev(dev, mode);
  8926. if (fb == NULL) {
  8927. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8928. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8929. } else
  8930. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8931. if (IS_ERR(fb)) {
  8932. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8933. goto fail;
  8934. }
  8935. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8936. if (ret)
  8937. goto fail;
  8938. drm_framebuffer_unreference(fb);
  8939. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8940. if (ret)
  8941. goto fail;
  8942. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8943. if (!ret)
  8944. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8945. if (!ret)
  8946. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8947. if (ret) {
  8948. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8949. goto fail;
  8950. }
  8951. ret = drm_atomic_commit(state);
  8952. if (ret) {
  8953. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8954. goto fail;
  8955. }
  8956. old->restore_state = restore_state;
  8957. /* let the connector get through one full cycle before testing */
  8958. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8959. return true;
  8960. fail:
  8961. drm_atomic_state_free(state);
  8962. drm_atomic_state_free(restore_state);
  8963. restore_state = state = NULL;
  8964. if (ret == -EDEADLK) {
  8965. drm_modeset_backoff(ctx);
  8966. goto retry;
  8967. }
  8968. return false;
  8969. }
  8970. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8971. struct intel_load_detect_pipe *old,
  8972. struct drm_modeset_acquire_ctx *ctx)
  8973. {
  8974. struct intel_encoder *intel_encoder =
  8975. intel_attached_encoder(connector);
  8976. struct drm_encoder *encoder = &intel_encoder->base;
  8977. struct drm_atomic_state *state = old->restore_state;
  8978. int ret;
  8979. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8980. connector->base.id, connector->name,
  8981. encoder->base.id, encoder->name);
  8982. if (!state)
  8983. return;
  8984. ret = drm_atomic_commit(state);
  8985. if (ret) {
  8986. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8987. drm_atomic_state_free(state);
  8988. }
  8989. }
  8990. static int i9xx_pll_refclk(struct drm_device *dev,
  8991. const struct intel_crtc_state *pipe_config)
  8992. {
  8993. struct drm_i915_private *dev_priv = to_i915(dev);
  8994. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8995. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8996. return dev_priv->vbt.lvds_ssc_freq;
  8997. else if (HAS_PCH_SPLIT(dev))
  8998. return 120000;
  8999. else if (!IS_GEN2(dev))
  9000. return 96000;
  9001. else
  9002. return 48000;
  9003. }
  9004. /* Returns the clock of the currently programmed mode of the given pipe. */
  9005. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9006. struct intel_crtc_state *pipe_config)
  9007. {
  9008. struct drm_device *dev = crtc->base.dev;
  9009. struct drm_i915_private *dev_priv = to_i915(dev);
  9010. int pipe = pipe_config->cpu_transcoder;
  9011. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9012. u32 fp;
  9013. struct dpll clock;
  9014. int port_clock;
  9015. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9016. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9017. fp = pipe_config->dpll_hw_state.fp0;
  9018. else
  9019. fp = pipe_config->dpll_hw_state.fp1;
  9020. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9021. if (IS_PINEVIEW(dev)) {
  9022. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9023. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9024. } else {
  9025. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9026. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9027. }
  9028. if (!IS_GEN2(dev)) {
  9029. if (IS_PINEVIEW(dev))
  9030. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9031. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9032. else
  9033. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9034. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9035. switch (dpll & DPLL_MODE_MASK) {
  9036. case DPLLB_MODE_DAC_SERIAL:
  9037. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9038. 5 : 10;
  9039. break;
  9040. case DPLLB_MODE_LVDS:
  9041. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9042. 7 : 14;
  9043. break;
  9044. default:
  9045. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9046. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9047. return;
  9048. }
  9049. if (IS_PINEVIEW(dev))
  9050. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9051. else
  9052. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9053. } else {
  9054. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9055. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9056. if (is_lvds) {
  9057. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9058. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9059. if (lvds & LVDS_CLKB_POWER_UP)
  9060. clock.p2 = 7;
  9061. else
  9062. clock.p2 = 14;
  9063. } else {
  9064. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9065. clock.p1 = 2;
  9066. else {
  9067. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9068. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9069. }
  9070. if (dpll & PLL_P2_DIVIDE_BY_4)
  9071. clock.p2 = 4;
  9072. else
  9073. clock.p2 = 2;
  9074. }
  9075. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9076. }
  9077. /*
  9078. * This value includes pixel_multiplier. We will use
  9079. * port_clock to compute adjusted_mode.crtc_clock in the
  9080. * encoder's get_config() function.
  9081. */
  9082. pipe_config->port_clock = port_clock;
  9083. }
  9084. int intel_dotclock_calculate(int link_freq,
  9085. const struct intel_link_m_n *m_n)
  9086. {
  9087. /*
  9088. * The calculation for the data clock is:
  9089. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9090. * But we want to avoid losing precison if possible, so:
  9091. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9092. *
  9093. * and the link clock is simpler:
  9094. * link_clock = (m * link_clock) / n
  9095. */
  9096. if (!m_n->link_n)
  9097. return 0;
  9098. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9099. }
  9100. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9101. struct intel_crtc_state *pipe_config)
  9102. {
  9103. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9104. /* read out port_clock from the DPLL */
  9105. i9xx_crtc_clock_get(crtc, pipe_config);
  9106. /*
  9107. * In case there is an active pipe without active ports,
  9108. * we may need some idea for the dotclock anyway.
  9109. * Calculate one based on the FDI configuration.
  9110. */
  9111. pipe_config->base.adjusted_mode.crtc_clock =
  9112. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9113. &pipe_config->fdi_m_n);
  9114. }
  9115. /** Returns the currently programmed mode of the given pipe. */
  9116. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9117. struct drm_crtc *crtc)
  9118. {
  9119. struct drm_i915_private *dev_priv = to_i915(dev);
  9120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9121. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9122. struct drm_display_mode *mode;
  9123. struct intel_crtc_state *pipe_config;
  9124. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9125. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9126. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9127. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9128. enum pipe pipe = intel_crtc->pipe;
  9129. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9130. if (!mode)
  9131. return NULL;
  9132. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9133. if (!pipe_config) {
  9134. kfree(mode);
  9135. return NULL;
  9136. }
  9137. /*
  9138. * Construct a pipe_config sufficient for getting the clock info
  9139. * back out of crtc_clock_get.
  9140. *
  9141. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9142. * to use a real value here instead.
  9143. */
  9144. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9145. pipe_config->pixel_multiplier = 1;
  9146. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9147. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9148. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9149. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9150. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9151. mode->hdisplay = (htot & 0xffff) + 1;
  9152. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9153. mode->hsync_start = (hsync & 0xffff) + 1;
  9154. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9155. mode->vdisplay = (vtot & 0xffff) + 1;
  9156. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9157. mode->vsync_start = (vsync & 0xffff) + 1;
  9158. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9159. drm_mode_set_name(mode);
  9160. kfree(pipe_config);
  9161. return mode;
  9162. }
  9163. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9164. {
  9165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9166. struct drm_device *dev = crtc->dev;
  9167. struct intel_flip_work *work;
  9168. spin_lock_irq(&dev->event_lock);
  9169. work = intel_crtc->flip_work;
  9170. intel_crtc->flip_work = NULL;
  9171. spin_unlock_irq(&dev->event_lock);
  9172. if (work) {
  9173. cancel_work_sync(&work->mmio_work);
  9174. cancel_work_sync(&work->unpin_work);
  9175. kfree(work);
  9176. }
  9177. drm_crtc_cleanup(crtc);
  9178. kfree(intel_crtc);
  9179. }
  9180. static void intel_unpin_work_fn(struct work_struct *__work)
  9181. {
  9182. struct intel_flip_work *work =
  9183. container_of(__work, struct intel_flip_work, unpin_work);
  9184. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9185. struct drm_device *dev = crtc->base.dev;
  9186. struct drm_plane *primary = crtc->base.primary;
  9187. if (is_mmio_work(work))
  9188. flush_work(&work->mmio_work);
  9189. mutex_lock(&dev->struct_mutex);
  9190. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9191. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9192. if (work->flip_queued_req)
  9193. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9194. mutex_unlock(&dev->struct_mutex);
  9195. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9196. intel_fbc_post_update(crtc);
  9197. drm_framebuffer_unreference(work->old_fb);
  9198. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9199. atomic_dec(&crtc->unpin_work_count);
  9200. kfree(work);
  9201. }
  9202. /* Is 'a' after or equal to 'b'? */
  9203. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9204. {
  9205. return !((a - b) & 0x80000000);
  9206. }
  9207. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9208. struct intel_flip_work *work)
  9209. {
  9210. struct drm_device *dev = crtc->base.dev;
  9211. struct drm_i915_private *dev_priv = to_i915(dev);
  9212. unsigned reset_counter;
  9213. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9214. if (crtc->reset_counter != reset_counter)
  9215. return true;
  9216. /*
  9217. * The relevant registers doen't exist on pre-ctg.
  9218. * As the flip done interrupt doesn't trigger for mmio
  9219. * flips on gmch platforms, a flip count check isn't
  9220. * really needed there. But since ctg has the registers,
  9221. * include it in the check anyway.
  9222. */
  9223. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9224. return true;
  9225. /*
  9226. * BDW signals flip done immediately if the plane
  9227. * is disabled, even if the plane enable is already
  9228. * armed to occur at the next vblank :(
  9229. */
  9230. /*
  9231. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9232. * used the same base address. In that case the mmio flip might
  9233. * have completed, but the CS hasn't even executed the flip yet.
  9234. *
  9235. * A flip count check isn't enough as the CS might have updated
  9236. * the base address just after start of vblank, but before we
  9237. * managed to process the interrupt. This means we'd complete the
  9238. * CS flip too soon.
  9239. *
  9240. * Combining both checks should get us a good enough result. It may
  9241. * still happen that the CS flip has been executed, but has not
  9242. * yet actually completed. But in case the base address is the same
  9243. * anyway, we don't really care.
  9244. */
  9245. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9246. crtc->flip_work->gtt_offset &&
  9247. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9248. crtc->flip_work->flip_count);
  9249. }
  9250. static bool
  9251. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9252. struct intel_flip_work *work)
  9253. {
  9254. /*
  9255. * MMIO work completes when vblank is different from
  9256. * flip_queued_vblank.
  9257. *
  9258. * Reset counter value doesn't matter, this is handled by
  9259. * i915_wait_request finishing early, so no need to handle
  9260. * reset here.
  9261. */
  9262. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9263. }
  9264. static bool pageflip_finished(struct intel_crtc *crtc,
  9265. struct intel_flip_work *work)
  9266. {
  9267. if (!atomic_read(&work->pending))
  9268. return false;
  9269. smp_rmb();
  9270. if (is_mmio_work(work))
  9271. return __pageflip_finished_mmio(crtc, work);
  9272. else
  9273. return __pageflip_finished_cs(crtc, work);
  9274. }
  9275. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9276. {
  9277. struct drm_device *dev = &dev_priv->drm;
  9278. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9280. struct intel_flip_work *work;
  9281. unsigned long flags;
  9282. /* Ignore early vblank irqs */
  9283. if (!crtc)
  9284. return;
  9285. /*
  9286. * This is called both by irq handlers and the reset code (to complete
  9287. * lost pageflips) so needs the full irqsave spinlocks.
  9288. */
  9289. spin_lock_irqsave(&dev->event_lock, flags);
  9290. work = intel_crtc->flip_work;
  9291. if (work != NULL &&
  9292. !is_mmio_work(work) &&
  9293. pageflip_finished(intel_crtc, work))
  9294. page_flip_completed(intel_crtc);
  9295. spin_unlock_irqrestore(&dev->event_lock, flags);
  9296. }
  9297. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9298. {
  9299. struct drm_device *dev = &dev_priv->drm;
  9300. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9302. struct intel_flip_work *work;
  9303. unsigned long flags;
  9304. /* Ignore early vblank irqs */
  9305. if (!crtc)
  9306. return;
  9307. /*
  9308. * This is called both by irq handlers and the reset code (to complete
  9309. * lost pageflips) so needs the full irqsave spinlocks.
  9310. */
  9311. spin_lock_irqsave(&dev->event_lock, flags);
  9312. work = intel_crtc->flip_work;
  9313. if (work != NULL &&
  9314. is_mmio_work(work) &&
  9315. pageflip_finished(intel_crtc, work))
  9316. page_flip_completed(intel_crtc);
  9317. spin_unlock_irqrestore(&dev->event_lock, flags);
  9318. }
  9319. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9320. struct intel_flip_work *work)
  9321. {
  9322. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9323. /* Ensure that the work item is consistent when activating it ... */
  9324. smp_mb__before_atomic();
  9325. atomic_set(&work->pending, 1);
  9326. }
  9327. static int intel_gen2_queue_flip(struct drm_device *dev,
  9328. struct drm_crtc *crtc,
  9329. struct drm_framebuffer *fb,
  9330. struct drm_i915_gem_object *obj,
  9331. struct drm_i915_gem_request *req,
  9332. uint32_t flags)
  9333. {
  9334. struct intel_engine_cs *engine = req->engine;
  9335. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9336. u32 flip_mask;
  9337. int ret;
  9338. ret = intel_ring_begin(req, 6);
  9339. if (ret)
  9340. return ret;
  9341. /* Can't queue multiple flips, so wait for the previous
  9342. * one to finish before executing the next.
  9343. */
  9344. if (intel_crtc->plane)
  9345. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9346. else
  9347. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9348. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9349. intel_ring_emit(engine, MI_NOOP);
  9350. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9351. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9352. intel_ring_emit(engine, fb->pitches[0]);
  9353. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9354. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9355. return 0;
  9356. }
  9357. static int intel_gen3_queue_flip(struct drm_device *dev,
  9358. struct drm_crtc *crtc,
  9359. struct drm_framebuffer *fb,
  9360. struct drm_i915_gem_object *obj,
  9361. struct drm_i915_gem_request *req,
  9362. uint32_t flags)
  9363. {
  9364. struct intel_engine_cs *engine = req->engine;
  9365. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9366. u32 flip_mask;
  9367. int ret;
  9368. ret = intel_ring_begin(req, 6);
  9369. if (ret)
  9370. return ret;
  9371. if (intel_crtc->plane)
  9372. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9373. else
  9374. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9375. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9376. intel_ring_emit(engine, MI_NOOP);
  9377. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9378. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9379. intel_ring_emit(engine, fb->pitches[0]);
  9380. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9381. intel_ring_emit(engine, MI_NOOP);
  9382. return 0;
  9383. }
  9384. static int intel_gen4_queue_flip(struct drm_device *dev,
  9385. struct drm_crtc *crtc,
  9386. struct drm_framebuffer *fb,
  9387. struct drm_i915_gem_object *obj,
  9388. struct drm_i915_gem_request *req,
  9389. uint32_t flags)
  9390. {
  9391. struct intel_engine_cs *engine = req->engine;
  9392. struct drm_i915_private *dev_priv = to_i915(dev);
  9393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9394. uint32_t pf, pipesrc;
  9395. int ret;
  9396. ret = intel_ring_begin(req, 4);
  9397. if (ret)
  9398. return ret;
  9399. /* i965+ uses the linear or tiled offsets from the
  9400. * Display Registers (which do not change across a page-flip)
  9401. * so we need only reprogram the base address.
  9402. */
  9403. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9404. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9405. intel_ring_emit(engine, fb->pitches[0]);
  9406. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
  9407. obj->tiling_mode);
  9408. /* XXX Enabling the panel-fitter across page-flip is so far
  9409. * untested on non-native modes, so ignore it for now.
  9410. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9411. */
  9412. pf = 0;
  9413. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9414. intel_ring_emit(engine, pf | pipesrc);
  9415. return 0;
  9416. }
  9417. static int intel_gen6_queue_flip(struct drm_device *dev,
  9418. struct drm_crtc *crtc,
  9419. struct drm_framebuffer *fb,
  9420. struct drm_i915_gem_object *obj,
  9421. struct drm_i915_gem_request *req,
  9422. uint32_t flags)
  9423. {
  9424. struct intel_engine_cs *engine = req->engine;
  9425. struct drm_i915_private *dev_priv = to_i915(dev);
  9426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9427. uint32_t pf, pipesrc;
  9428. int ret;
  9429. ret = intel_ring_begin(req, 4);
  9430. if (ret)
  9431. return ret;
  9432. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9433. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9434. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9435. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9436. /* Contrary to the suggestions in the documentation,
  9437. * "Enable Panel Fitter" does not seem to be required when page
  9438. * flipping with a non-native mode, and worse causes a normal
  9439. * modeset to fail.
  9440. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9441. */
  9442. pf = 0;
  9443. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9444. intel_ring_emit(engine, pf | pipesrc);
  9445. return 0;
  9446. }
  9447. static int intel_gen7_queue_flip(struct drm_device *dev,
  9448. struct drm_crtc *crtc,
  9449. struct drm_framebuffer *fb,
  9450. struct drm_i915_gem_object *obj,
  9451. struct drm_i915_gem_request *req,
  9452. uint32_t flags)
  9453. {
  9454. struct intel_engine_cs *engine = req->engine;
  9455. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9456. uint32_t plane_bit = 0;
  9457. int len, ret;
  9458. switch (intel_crtc->plane) {
  9459. case PLANE_A:
  9460. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9461. break;
  9462. case PLANE_B:
  9463. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9464. break;
  9465. case PLANE_C:
  9466. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9467. break;
  9468. default:
  9469. WARN_ONCE(1, "unknown plane in flip command\n");
  9470. return -ENODEV;
  9471. }
  9472. len = 4;
  9473. if (engine->id == RCS) {
  9474. len += 6;
  9475. /*
  9476. * On Gen 8, SRM is now taking an extra dword to accommodate
  9477. * 48bits addresses, and we need a NOOP for the batch size to
  9478. * stay even.
  9479. */
  9480. if (IS_GEN8(dev))
  9481. len += 2;
  9482. }
  9483. /*
  9484. * BSpec MI_DISPLAY_FLIP for IVB:
  9485. * "The full packet must be contained within the same cache line."
  9486. *
  9487. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9488. * cacheline, if we ever start emitting more commands before
  9489. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9490. * then do the cacheline alignment, and finally emit the
  9491. * MI_DISPLAY_FLIP.
  9492. */
  9493. ret = intel_ring_cacheline_align(req);
  9494. if (ret)
  9495. return ret;
  9496. ret = intel_ring_begin(req, len);
  9497. if (ret)
  9498. return ret;
  9499. /* Unmask the flip-done completion message. Note that the bspec says that
  9500. * we should do this for both the BCS and RCS, and that we must not unmask
  9501. * more than one flip event at any time (or ensure that one flip message
  9502. * can be sent by waiting for flip-done prior to queueing new flips).
  9503. * Experimentation says that BCS works despite DERRMR masking all
  9504. * flip-done completion events and that unmasking all planes at once
  9505. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9506. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9507. */
  9508. if (engine->id == RCS) {
  9509. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9510. intel_ring_emit_reg(engine, DERRMR);
  9511. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9512. DERRMR_PIPEB_PRI_FLIP_DONE |
  9513. DERRMR_PIPEC_PRI_FLIP_DONE));
  9514. if (IS_GEN8(dev))
  9515. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9516. MI_SRM_LRM_GLOBAL_GTT);
  9517. else
  9518. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9519. MI_SRM_LRM_GLOBAL_GTT);
  9520. intel_ring_emit_reg(engine, DERRMR);
  9521. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9522. if (IS_GEN8(dev)) {
  9523. intel_ring_emit(engine, 0);
  9524. intel_ring_emit(engine, MI_NOOP);
  9525. }
  9526. }
  9527. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9528. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9529. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9530. intel_ring_emit(engine, (MI_NOOP));
  9531. return 0;
  9532. }
  9533. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9534. struct drm_i915_gem_object *obj)
  9535. {
  9536. struct reservation_object *resv;
  9537. /*
  9538. * This is not being used for older platforms, because
  9539. * non-availability of flip done interrupt forces us to use
  9540. * CS flips. Older platforms derive flip done using some clever
  9541. * tricks involving the flip_pending status bits and vblank irqs.
  9542. * So using MMIO flips there would disrupt this mechanism.
  9543. */
  9544. if (engine == NULL)
  9545. return true;
  9546. if (INTEL_GEN(engine->i915) < 5)
  9547. return false;
  9548. if (i915.use_mmio_flip < 0)
  9549. return false;
  9550. else if (i915.use_mmio_flip > 0)
  9551. return true;
  9552. else if (i915.enable_execlists)
  9553. return true;
  9554. resv = i915_gem_object_get_dmabuf_resv(obj);
  9555. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  9556. return true;
  9557. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9558. }
  9559. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9560. unsigned int rotation,
  9561. struct intel_flip_work *work)
  9562. {
  9563. struct drm_device *dev = intel_crtc->base.dev;
  9564. struct drm_i915_private *dev_priv = to_i915(dev);
  9565. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9566. const enum pipe pipe = intel_crtc->pipe;
  9567. u32 ctl, stride, tile_height;
  9568. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9569. ctl &= ~PLANE_CTL_TILED_MASK;
  9570. switch (fb->modifier[0]) {
  9571. case DRM_FORMAT_MOD_NONE:
  9572. break;
  9573. case I915_FORMAT_MOD_X_TILED:
  9574. ctl |= PLANE_CTL_TILED_X;
  9575. break;
  9576. case I915_FORMAT_MOD_Y_TILED:
  9577. ctl |= PLANE_CTL_TILED_Y;
  9578. break;
  9579. case I915_FORMAT_MOD_Yf_TILED:
  9580. ctl |= PLANE_CTL_TILED_YF;
  9581. break;
  9582. default:
  9583. MISSING_CASE(fb->modifier[0]);
  9584. }
  9585. /*
  9586. * The stride is either expressed as a multiple of 64 bytes chunks for
  9587. * linear buffers or in number of tiles for tiled buffers.
  9588. */
  9589. if (intel_rotation_90_or_270(rotation)) {
  9590. /* stride = Surface height in tiles */
  9591. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9592. stride = DIV_ROUND_UP(fb->height, tile_height);
  9593. } else {
  9594. stride = fb->pitches[0] /
  9595. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9596. fb->pixel_format);
  9597. }
  9598. /*
  9599. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9600. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9601. */
  9602. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9603. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9604. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9605. POSTING_READ(PLANE_SURF(pipe, 0));
  9606. }
  9607. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9608. struct intel_flip_work *work)
  9609. {
  9610. struct drm_device *dev = intel_crtc->base.dev;
  9611. struct drm_i915_private *dev_priv = to_i915(dev);
  9612. struct intel_framebuffer *intel_fb =
  9613. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9614. struct drm_i915_gem_object *obj = intel_fb->obj;
  9615. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9616. u32 dspcntr;
  9617. dspcntr = I915_READ(reg);
  9618. if (obj->tiling_mode != I915_TILING_NONE)
  9619. dspcntr |= DISPPLANE_TILED;
  9620. else
  9621. dspcntr &= ~DISPPLANE_TILED;
  9622. I915_WRITE(reg, dspcntr);
  9623. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9624. POSTING_READ(DSPSURF(intel_crtc->plane));
  9625. }
  9626. static void intel_mmio_flip_work_func(struct work_struct *w)
  9627. {
  9628. struct intel_flip_work *work =
  9629. container_of(w, struct intel_flip_work, mmio_work);
  9630. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9631. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9632. struct intel_framebuffer *intel_fb =
  9633. to_intel_framebuffer(crtc->base.primary->fb);
  9634. struct drm_i915_gem_object *obj = intel_fb->obj;
  9635. struct reservation_object *resv;
  9636. if (work->flip_queued_req)
  9637. WARN_ON(__i915_wait_request(work->flip_queued_req,
  9638. false, NULL,
  9639. &dev_priv->rps.mmioflips));
  9640. /* For framebuffer backed by dmabuf, wait for fence */
  9641. resv = i915_gem_object_get_dmabuf_resv(obj);
  9642. if (resv)
  9643. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  9644. MAX_SCHEDULE_TIMEOUT) < 0);
  9645. intel_pipe_update_start(crtc);
  9646. if (INTEL_GEN(dev_priv) >= 9)
  9647. skl_do_mmio_flip(crtc, work->rotation, work);
  9648. else
  9649. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9650. ilk_do_mmio_flip(crtc, work);
  9651. intel_pipe_update_end(crtc, work);
  9652. }
  9653. static int intel_default_queue_flip(struct drm_device *dev,
  9654. struct drm_crtc *crtc,
  9655. struct drm_framebuffer *fb,
  9656. struct drm_i915_gem_object *obj,
  9657. struct drm_i915_gem_request *req,
  9658. uint32_t flags)
  9659. {
  9660. return -ENODEV;
  9661. }
  9662. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9663. struct intel_crtc *intel_crtc,
  9664. struct intel_flip_work *work)
  9665. {
  9666. u32 addr, vblank;
  9667. if (!atomic_read(&work->pending))
  9668. return false;
  9669. smp_rmb();
  9670. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9671. if (work->flip_ready_vblank == 0) {
  9672. if (work->flip_queued_req &&
  9673. !i915_gem_request_completed(work->flip_queued_req))
  9674. return false;
  9675. work->flip_ready_vblank = vblank;
  9676. }
  9677. if (vblank - work->flip_ready_vblank < 3)
  9678. return false;
  9679. /* Potential stall - if we see that the flip has happened,
  9680. * assume a missed interrupt. */
  9681. if (INTEL_GEN(dev_priv) >= 4)
  9682. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9683. else
  9684. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9685. /* There is a potential issue here with a false positive after a flip
  9686. * to the same address. We could address this by checking for a
  9687. * non-incrementing frame counter.
  9688. */
  9689. return addr == work->gtt_offset;
  9690. }
  9691. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9692. {
  9693. struct drm_device *dev = &dev_priv->drm;
  9694. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9695. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9696. struct intel_flip_work *work;
  9697. WARN_ON(!in_interrupt());
  9698. if (crtc == NULL)
  9699. return;
  9700. spin_lock(&dev->event_lock);
  9701. work = intel_crtc->flip_work;
  9702. if (work != NULL && !is_mmio_work(work) &&
  9703. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9704. WARN_ONCE(1,
  9705. "Kicking stuck page flip: queued at %d, now %d\n",
  9706. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9707. page_flip_completed(intel_crtc);
  9708. work = NULL;
  9709. }
  9710. if (work != NULL && !is_mmio_work(work) &&
  9711. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9712. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9713. spin_unlock(&dev->event_lock);
  9714. }
  9715. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9716. struct drm_framebuffer *fb,
  9717. struct drm_pending_vblank_event *event,
  9718. uint32_t page_flip_flags)
  9719. {
  9720. struct drm_device *dev = crtc->dev;
  9721. struct drm_i915_private *dev_priv = to_i915(dev);
  9722. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9723. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9724. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9725. struct drm_plane *primary = crtc->primary;
  9726. enum pipe pipe = intel_crtc->pipe;
  9727. struct intel_flip_work *work;
  9728. struct intel_engine_cs *engine;
  9729. bool mmio_flip;
  9730. struct drm_i915_gem_request *request = NULL;
  9731. int ret;
  9732. /*
  9733. * drm_mode_page_flip_ioctl() should already catch this, but double
  9734. * check to be safe. In the future we may enable pageflipping from
  9735. * a disabled primary plane.
  9736. */
  9737. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9738. return -EBUSY;
  9739. /* Can't change pixel format via MI display flips. */
  9740. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9741. return -EINVAL;
  9742. /*
  9743. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9744. * Note that pitch changes could also affect these register.
  9745. */
  9746. if (INTEL_INFO(dev)->gen > 3 &&
  9747. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9748. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9749. return -EINVAL;
  9750. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9751. goto out_hang;
  9752. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9753. if (work == NULL)
  9754. return -ENOMEM;
  9755. work->event = event;
  9756. work->crtc = crtc;
  9757. work->old_fb = old_fb;
  9758. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9759. ret = drm_crtc_vblank_get(crtc);
  9760. if (ret)
  9761. goto free_work;
  9762. /* We borrow the event spin lock for protecting flip_work */
  9763. spin_lock_irq(&dev->event_lock);
  9764. if (intel_crtc->flip_work) {
  9765. /* Before declaring the flip queue wedged, check if
  9766. * the hardware completed the operation behind our backs.
  9767. */
  9768. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9769. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9770. page_flip_completed(intel_crtc);
  9771. } else {
  9772. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9773. spin_unlock_irq(&dev->event_lock);
  9774. drm_crtc_vblank_put(crtc);
  9775. kfree(work);
  9776. return -EBUSY;
  9777. }
  9778. }
  9779. intel_crtc->flip_work = work;
  9780. spin_unlock_irq(&dev->event_lock);
  9781. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9782. flush_workqueue(dev_priv->wq);
  9783. /* Reference the objects for the scheduled work. */
  9784. drm_framebuffer_reference(work->old_fb);
  9785. drm_gem_object_reference(&obj->base);
  9786. crtc->primary->fb = fb;
  9787. update_state_fb(crtc->primary);
  9788. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9789. to_intel_plane_state(primary->state));
  9790. work->pending_flip_obj = obj;
  9791. ret = i915_mutex_lock_interruptible(dev);
  9792. if (ret)
  9793. goto cleanup;
  9794. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9795. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9796. ret = -EIO;
  9797. goto cleanup;
  9798. }
  9799. atomic_inc(&intel_crtc->unpin_work_count);
  9800. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9801. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9802. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9803. engine = &dev_priv->engine[BCS];
  9804. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9805. /* vlv: DISPLAY_FLIP fails to change tiling */
  9806. engine = NULL;
  9807. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9808. engine = &dev_priv->engine[BCS];
  9809. } else if (INTEL_INFO(dev)->gen >= 7) {
  9810. engine = i915_gem_request_get_engine(obj->last_write_req);
  9811. if (engine == NULL || engine->id != RCS)
  9812. engine = &dev_priv->engine[BCS];
  9813. } else {
  9814. engine = &dev_priv->engine[RCS];
  9815. }
  9816. mmio_flip = use_mmio_flip(engine, obj);
  9817. /* When using CS flips, we want to emit semaphores between rings.
  9818. * However, when using mmio flips we will create a task to do the
  9819. * synchronisation, so all we want here is to pin the framebuffer
  9820. * into the display plane and skip any waits.
  9821. */
  9822. if (!mmio_flip) {
  9823. ret = i915_gem_object_sync(obj, engine, &request);
  9824. if (!ret && !request) {
  9825. request = i915_gem_request_alloc(engine, NULL);
  9826. ret = PTR_ERR_OR_ZERO(request);
  9827. }
  9828. if (ret)
  9829. goto cleanup_pending;
  9830. }
  9831. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9832. if (ret)
  9833. goto cleanup_pending;
  9834. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9835. obj, 0);
  9836. work->gtt_offset += intel_crtc->dspaddr_offset;
  9837. work->rotation = crtc->primary->state->rotation;
  9838. if (mmio_flip) {
  9839. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9840. i915_gem_request_assign(&work->flip_queued_req,
  9841. obj->last_write_req);
  9842. schedule_work(&work->mmio_work);
  9843. } else {
  9844. i915_gem_request_assign(&work->flip_queued_req, request);
  9845. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9846. page_flip_flags);
  9847. if (ret)
  9848. goto cleanup_unpin;
  9849. intel_mark_page_flip_active(intel_crtc, work);
  9850. i915_add_request_no_flush(request);
  9851. }
  9852. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9853. to_intel_plane(primary)->frontbuffer_bit);
  9854. mutex_unlock(&dev->struct_mutex);
  9855. intel_frontbuffer_flip_prepare(dev,
  9856. to_intel_plane(primary)->frontbuffer_bit);
  9857. trace_i915_flip_request(intel_crtc->plane, obj);
  9858. return 0;
  9859. cleanup_unpin:
  9860. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9861. cleanup_pending:
  9862. if (!IS_ERR_OR_NULL(request))
  9863. i915_add_request_no_flush(request);
  9864. atomic_dec(&intel_crtc->unpin_work_count);
  9865. mutex_unlock(&dev->struct_mutex);
  9866. cleanup:
  9867. crtc->primary->fb = old_fb;
  9868. update_state_fb(crtc->primary);
  9869. drm_gem_object_unreference_unlocked(&obj->base);
  9870. drm_framebuffer_unreference(work->old_fb);
  9871. spin_lock_irq(&dev->event_lock);
  9872. intel_crtc->flip_work = NULL;
  9873. spin_unlock_irq(&dev->event_lock);
  9874. drm_crtc_vblank_put(crtc);
  9875. free_work:
  9876. kfree(work);
  9877. if (ret == -EIO) {
  9878. struct drm_atomic_state *state;
  9879. struct drm_plane_state *plane_state;
  9880. out_hang:
  9881. state = drm_atomic_state_alloc(dev);
  9882. if (!state)
  9883. return -ENOMEM;
  9884. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9885. retry:
  9886. plane_state = drm_atomic_get_plane_state(state, primary);
  9887. ret = PTR_ERR_OR_ZERO(plane_state);
  9888. if (!ret) {
  9889. drm_atomic_set_fb_for_plane(plane_state, fb);
  9890. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9891. if (!ret)
  9892. ret = drm_atomic_commit(state);
  9893. }
  9894. if (ret == -EDEADLK) {
  9895. drm_modeset_backoff(state->acquire_ctx);
  9896. drm_atomic_state_clear(state);
  9897. goto retry;
  9898. }
  9899. if (ret)
  9900. drm_atomic_state_free(state);
  9901. if (ret == 0 && event) {
  9902. spin_lock_irq(&dev->event_lock);
  9903. drm_crtc_send_vblank_event(crtc, event);
  9904. spin_unlock_irq(&dev->event_lock);
  9905. }
  9906. }
  9907. return ret;
  9908. }
  9909. /**
  9910. * intel_wm_need_update - Check whether watermarks need updating
  9911. * @plane: drm plane
  9912. * @state: new plane state
  9913. *
  9914. * Check current plane state versus the new one to determine whether
  9915. * watermarks need to be recalculated.
  9916. *
  9917. * Returns true or false.
  9918. */
  9919. static bool intel_wm_need_update(struct drm_plane *plane,
  9920. struct drm_plane_state *state)
  9921. {
  9922. struct intel_plane_state *new = to_intel_plane_state(state);
  9923. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9924. /* Update watermarks on tiling or size changes. */
  9925. if (new->visible != cur->visible)
  9926. return true;
  9927. if (!cur->base.fb || !new->base.fb)
  9928. return false;
  9929. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9930. cur->base.rotation != new->base.rotation ||
  9931. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9932. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9933. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9934. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9935. return true;
  9936. return false;
  9937. }
  9938. static bool needs_scaling(struct intel_plane_state *state)
  9939. {
  9940. int src_w = drm_rect_width(&state->src) >> 16;
  9941. int src_h = drm_rect_height(&state->src) >> 16;
  9942. int dst_w = drm_rect_width(&state->dst);
  9943. int dst_h = drm_rect_height(&state->dst);
  9944. return (src_w != dst_w || src_h != dst_h);
  9945. }
  9946. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9947. struct drm_plane_state *plane_state)
  9948. {
  9949. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9950. struct drm_crtc *crtc = crtc_state->crtc;
  9951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9952. struct drm_plane *plane = plane_state->plane;
  9953. struct drm_device *dev = crtc->dev;
  9954. struct drm_i915_private *dev_priv = to_i915(dev);
  9955. struct intel_plane_state *old_plane_state =
  9956. to_intel_plane_state(plane->state);
  9957. bool mode_changed = needs_modeset(crtc_state);
  9958. bool was_crtc_enabled = crtc->state->active;
  9959. bool is_crtc_enabled = crtc_state->active;
  9960. bool turn_off, turn_on, visible, was_visible;
  9961. struct drm_framebuffer *fb = plane_state->fb;
  9962. int ret;
  9963. if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  9964. ret = skl_update_scaler_plane(
  9965. to_intel_crtc_state(crtc_state),
  9966. to_intel_plane_state(plane_state));
  9967. if (ret)
  9968. return ret;
  9969. }
  9970. was_visible = old_plane_state->visible;
  9971. visible = to_intel_plane_state(plane_state)->visible;
  9972. if (!was_crtc_enabled && WARN_ON(was_visible))
  9973. was_visible = false;
  9974. /*
  9975. * Visibility is calculated as if the crtc was on, but
  9976. * after scaler setup everything depends on it being off
  9977. * when the crtc isn't active.
  9978. *
  9979. * FIXME this is wrong for watermarks. Watermarks should also
  9980. * be computed as if the pipe would be active. Perhaps move
  9981. * per-plane wm computation to the .check_plane() hook, and
  9982. * only combine the results from all planes in the current place?
  9983. */
  9984. if (!is_crtc_enabled)
  9985. to_intel_plane_state(plane_state)->visible = visible = false;
  9986. if (!was_visible && !visible)
  9987. return 0;
  9988. if (fb != old_plane_state->base.fb)
  9989. pipe_config->fb_changed = true;
  9990. turn_off = was_visible && (!visible || mode_changed);
  9991. turn_on = visible && (!was_visible || mode_changed);
  9992. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9993. intel_crtc->base.base.id,
  9994. intel_crtc->base.name,
  9995. plane->base.id, plane->name,
  9996. fb ? fb->base.id : -1);
  9997. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9998. plane->base.id, plane->name,
  9999. was_visible, visible,
  10000. turn_off, turn_on, mode_changed);
  10001. if (turn_on) {
  10002. pipe_config->update_wm_pre = true;
  10003. /* must disable cxsr around plane enable/disable */
  10004. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10005. pipe_config->disable_cxsr = true;
  10006. } else if (turn_off) {
  10007. pipe_config->update_wm_post = true;
  10008. /* must disable cxsr around plane enable/disable */
  10009. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10010. pipe_config->disable_cxsr = true;
  10011. } else if (intel_wm_need_update(plane, plane_state)) {
  10012. /* FIXME bollocks */
  10013. pipe_config->update_wm_pre = true;
  10014. pipe_config->update_wm_post = true;
  10015. }
  10016. /* Pre-gen9 platforms need two-step watermark updates */
  10017. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10018. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10019. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10020. if (visible || was_visible)
  10021. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10022. /*
  10023. * WaCxSRDisabledForSpriteScaling:ivb
  10024. *
  10025. * cstate->update_wm was already set above, so this flag will
  10026. * take effect when we commit and program watermarks.
  10027. */
  10028. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10029. needs_scaling(to_intel_plane_state(plane_state)) &&
  10030. !needs_scaling(old_plane_state))
  10031. pipe_config->disable_lp_wm = true;
  10032. return 0;
  10033. }
  10034. static bool encoders_cloneable(const struct intel_encoder *a,
  10035. const struct intel_encoder *b)
  10036. {
  10037. /* masks could be asymmetric, so check both ways */
  10038. return a == b || (a->cloneable & (1 << b->type) &&
  10039. b->cloneable & (1 << a->type));
  10040. }
  10041. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10042. struct intel_crtc *crtc,
  10043. struct intel_encoder *encoder)
  10044. {
  10045. struct intel_encoder *source_encoder;
  10046. struct drm_connector *connector;
  10047. struct drm_connector_state *connector_state;
  10048. int i;
  10049. for_each_connector_in_state(state, connector, connector_state, i) {
  10050. if (connector_state->crtc != &crtc->base)
  10051. continue;
  10052. source_encoder =
  10053. to_intel_encoder(connector_state->best_encoder);
  10054. if (!encoders_cloneable(encoder, source_encoder))
  10055. return false;
  10056. }
  10057. return true;
  10058. }
  10059. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10060. struct intel_crtc *crtc)
  10061. {
  10062. struct intel_encoder *encoder;
  10063. struct drm_connector *connector;
  10064. struct drm_connector_state *connector_state;
  10065. int i;
  10066. for_each_connector_in_state(state, connector, connector_state, i) {
  10067. if (connector_state->crtc != &crtc->base)
  10068. continue;
  10069. encoder = to_intel_encoder(connector_state->best_encoder);
  10070. if (!check_single_encoder_cloning(state, crtc, encoder))
  10071. return false;
  10072. }
  10073. return true;
  10074. }
  10075. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10076. struct drm_crtc_state *crtc_state)
  10077. {
  10078. struct drm_device *dev = crtc->dev;
  10079. struct drm_i915_private *dev_priv = to_i915(dev);
  10080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10081. struct intel_crtc_state *pipe_config =
  10082. to_intel_crtc_state(crtc_state);
  10083. struct drm_atomic_state *state = crtc_state->state;
  10084. int ret;
  10085. bool mode_changed = needs_modeset(crtc_state);
  10086. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10087. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10088. return -EINVAL;
  10089. }
  10090. if (mode_changed && !crtc_state->active)
  10091. pipe_config->update_wm_post = true;
  10092. if (mode_changed && crtc_state->enable &&
  10093. dev_priv->display.crtc_compute_clock &&
  10094. !WARN_ON(pipe_config->shared_dpll)) {
  10095. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10096. pipe_config);
  10097. if (ret)
  10098. return ret;
  10099. }
  10100. if (crtc_state->color_mgmt_changed) {
  10101. ret = intel_color_check(crtc, crtc_state);
  10102. if (ret)
  10103. return ret;
  10104. }
  10105. ret = 0;
  10106. if (dev_priv->display.compute_pipe_wm) {
  10107. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10108. if (ret) {
  10109. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10110. return ret;
  10111. }
  10112. }
  10113. if (dev_priv->display.compute_intermediate_wm &&
  10114. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10115. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10116. return 0;
  10117. /*
  10118. * Calculate 'intermediate' watermarks that satisfy both the
  10119. * old state and the new state. We can program these
  10120. * immediately.
  10121. */
  10122. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10123. intel_crtc,
  10124. pipe_config);
  10125. if (ret) {
  10126. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10127. return ret;
  10128. }
  10129. } else if (dev_priv->display.compute_intermediate_wm) {
  10130. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10131. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10132. }
  10133. if (INTEL_INFO(dev)->gen >= 9) {
  10134. if (mode_changed)
  10135. ret = skl_update_scaler_crtc(pipe_config);
  10136. if (!ret)
  10137. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10138. pipe_config);
  10139. }
  10140. return ret;
  10141. }
  10142. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10143. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10144. .atomic_begin = intel_begin_crtc_commit,
  10145. .atomic_flush = intel_finish_crtc_commit,
  10146. .atomic_check = intel_crtc_atomic_check,
  10147. };
  10148. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10149. {
  10150. struct intel_connector *connector;
  10151. for_each_intel_connector(dev, connector) {
  10152. if (connector->base.state->crtc)
  10153. drm_connector_unreference(&connector->base);
  10154. if (connector->base.encoder) {
  10155. connector->base.state->best_encoder =
  10156. connector->base.encoder;
  10157. connector->base.state->crtc =
  10158. connector->base.encoder->crtc;
  10159. drm_connector_reference(&connector->base);
  10160. } else {
  10161. connector->base.state->best_encoder = NULL;
  10162. connector->base.state->crtc = NULL;
  10163. }
  10164. }
  10165. }
  10166. static void
  10167. connected_sink_compute_bpp(struct intel_connector *connector,
  10168. struct intel_crtc_state *pipe_config)
  10169. {
  10170. int bpp = pipe_config->pipe_bpp;
  10171. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10172. connector->base.base.id,
  10173. connector->base.name);
  10174. /* Don't use an invalid EDID bpc value */
  10175. if (connector->base.display_info.bpc &&
  10176. connector->base.display_info.bpc * 3 < bpp) {
  10177. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10178. bpp, connector->base.display_info.bpc*3);
  10179. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10180. }
  10181. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10182. if (connector->base.display_info.bpc == 0) {
  10183. int type = connector->base.connector_type;
  10184. int clamp_bpp = 24;
  10185. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10186. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10187. type == DRM_MODE_CONNECTOR_eDP)
  10188. clamp_bpp = 18;
  10189. if (bpp > clamp_bpp) {
  10190. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10191. bpp, clamp_bpp);
  10192. pipe_config->pipe_bpp = clamp_bpp;
  10193. }
  10194. }
  10195. }
  10196. static int
  10197. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10198. struct intel_crtc_state *pipe_config)
  10199. {
  10200. struct drm_device *dev = crtc->base.dev;
  10201. struct drm_atomic_state *state;
  10202. struct drm_connector *connector;
  10203. struct drm_connector_state *connector_state;
  10204. int bpp, i;
  10205. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10206. bpp = 10*3;
  10207. else if (INTEL_INFO(dev)->gen >= 5)
  10208. bpp = 12*3;
  10209. else
  10210. bpp = 8*3;
  10211. pipe_config->pipe_bpp = bpp;
  10212. state = pipe_config->base.state;
  10213. /* Clamp display bpp to EDID value */
  10214. for_each_connector_in_state(state, connector, connector_state, i) {
  10215. if (connector_state->crtc != &crtc->base)
  10216. continue;
  10217. connected_sink_compute_bpp(to_intel_connector(connector),
  10218. pipe_config);
  10219. }
  10220. return bpp;
  10221. }
  10222. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10223. {
  10224. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10225. "type: 0x%x flags: 0x%x\n",
  10226. mode->crtc_clock,
  10227. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10228. mode->crtc_hsync_end, mode->crtc_htotal,
  10229. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10230. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10231. }
  10232. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10233. struct intel_crtc_state *pipe_config,
  10234. const char *context)
  10235. {
  10236. struct drm_device *dev = crtc->base.dev;
  10237. struct drm_plane *plane;
  10238. struct intel_plane *intel_plane;
  10239. struct intel_plane_state *state;
  10240. struct drm_framebuffer *fb;
  10241. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10242. crtc->base.base.id, crtc->base.name,
  10243. context, pipe_config, pipe_name(crtc->pipe));
  10244. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10245. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10246. pipe_config->pipe_bpp, pipe_config->dither);
  10247. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10248. pipe_config->has_pch_encoder,
  10249. pipe_config->fdi_lanes,
  10250. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10251. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10252. pipe_config->fdi_m_n.tu);
  10253. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10254. intel_crtc_has_dp_encoder(pipe_config),
  10255. pipe_config->lane_count,
  10256. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10257. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10258. pipe_config->dp_m_n.tu);
  10259. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10260. intel_crtc_has_dp_encoder(pipe_config),
  10261. pipe_config->lane_count,
  10262. pipe_config->dp_m2_n2.gmch_m,
  10263. pipe_config->dp_m2_n2.gmch_n,
  10264. pipe_config->dp_m2_n2.link_m,
  10265. pipe_config->dp_m2_n2.link_n,
  10266. pipe_config->dp_m2_n2.tu);
  10267. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10268. pipe_config->has_audio,
  10269. pipe_config->has_infoframe);
  10270. DRM_DEBUG_KMS("requested mode:\n");
  10271. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10272. DRM_DEBUG_KMS("adjusted mode:\n");
  10273. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10274. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10275. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10276. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10277. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10278. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10279. crtc->num_scalers,
  10280. pipe_config->scaler_state.scaler_users,
  10281. pipe_config->scaler_state.scaler_id);
  10282. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10283. pipe_config->gmch_pfit.control,
  10284. pipe_config->gmch_pfit.pgm_ratios,
  10285. pipe_config->gmch_pfit.lvds_border_bits);
  10286. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10287. pipe_config->pch_pfit.pos,
  10288. pipe_config->pch_pfit.size,
  10289. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10290. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10291. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10292. if (IS_BROXTON(dev)) {
  10293. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10294. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10295. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10296. pipe_config->ddi_pll_sel,
  10297. pipe_config->dpll_hw_state.ebb0,
  10298. pipe_config->dpll_hw_state.ebb4,
  10299. pipe_config->dpll_hw_state.pll0,
  10300. pipe_config->dpll_hw_state.pll1,
  10301. pipe_config->dpll_hw_state.pll2,
  10302. pipe_config->dpll_hw_state.pll3,
  10303. pipe_config->dpll_hw_state.pll6,
  10304. pipe_config->dpll_hw_state.pll8,
  10305. pipe_config->dpll_hw_state.pll9,
  10306. pipe_config->dpll_hw_state.pll10,
  10307. pipe_config->dpll_hw_state.pcsdw12);
  10308. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10309. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10310. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10311. pipe_config->ddi_pll_sel,
  10312. pipe_config->dpll_hw_state.ctrl1,
  10313. pipe_config->dpll_hw_state.cfgcr1,
  10314. pipe_config->dpll_hw_state.cfgcr2);
  10315. } else if (HAS_DDI(dev)) {
  10316. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10317. pipe_config->ddi_pll_sel,
  10318. pipe_config->dpll_hw_state.wrpll,
  10319. pipe_config->dpll_hw_state.spll);
  10320. } else {
  10321. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10322. "fp0: 0x%x, fp1: 0x%x\n",
  10323. pipe_config->dpll_hw_state.dpll,
  10324. pipe_config->dpll_hw_state.dpll_md,
  10325. pipe_config->dpll_hw_state.fp0,
  10326. pipe_config->dpll_hw_state.fp1);
  10327. }
  10328. DRM_DEBUG_KMS("planes on this crtc\n");
  10329. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10330. intel_plane = to_intel_plane(plane);
  10331. if (intel_plane->pipe != crtc->pipe)
  10332. continue;
  10333. state = to_intel_plane_state(plane->state);
  10334. fb = state->base.fb;
  10335. if (!fb) {
  10336. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10337. plane->base.id, plane->name, state->scaler_id);
  10338. continue;
  10339. }
  10340. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10341. plane->base.id, plane->name);
  10342. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10343. fb->base.id, fb->width, fb->height,
  10344. drm_get_format_name(fb->pixel_format));
  10345. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10346. state->scaler_id,
  10347. state->src.x1 >> 16, state->src.y1 >> 16,
  10348. drm_rect_width(&state->src) >> 16,
  10349. drm_rect_height(&state->src) >> 16,
  10350. state->dst.x1, state->dst.y1,
  10351. drm_rect_width(&state->dst),
  10352. drm_rect_height(&state->dst));
  10353. }
  10354. }
  10355. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10356. {
  10357. struct drm_device *dev = state->dev;
  10358. struct drm_connector *connector;
  10359. unsigned int used_ports = 0;
  10360. /*
  10361. * Walk the connector list instead of the encoder
  10362. * list to detect the problem on ddi platforms
  10363. * where there's just one encoder per digital port.
  10364. */
  10365. drm_for_each_connector(connector, dev) {
  10366. struct drm_connector_state *connector_state;
  10367. struct intel_encoder *encoder;
  10368. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10369. if (!connector_state)
  10370. connector_state = connector->state;
  10371. if (!connector_state->best_encoder)
  10372. continue;
  10373. encoder = to_intel_encoder(connector_state->best_encoder);
  10374. WARN_ON(!connector_state->crtc);
  10375. switch (encoder->type) {
  10376. unsigned int port_mask;
  10377. case INTEL_OUTPUT_UNKNOWN:
  10378. if (WARN_ON(!HAS_DDI(dev)))
  10379. break;
  10380. case INTEL_OUTPUT_DP:
  10381. case INTEL_OUTPUT_HDMI:
  10382. case INTEL_OUTPUT_EDP:
  10383. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10384. /* the same port mustn't appear more than once */
  10385. if (used_ports & port_mask)
  10386. return false;
  10387. used_ports |= port_mask;
  10388. default:
  10389. break;
  10390. }
  10391. }
  10392. return true;
  10393. }
  10394. static void
  10395. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10396. {
  10397. struct drm_crtc_state tmp_state;
  10398. struct intel_crtc_scaler_state scaler_state;
  10399. struct intel_dpll_hw_state dpll_hw_state;
  10400. struct intel_shared_dpll *shared_dpll;
  10401. uint32_t ddi_pll_sel;
  10402. bool force_thru;
  10403. /* FIXME: before the switch to atomic started, a new pipe_config was
  10404. * kzalloc'd. Code that depends on any field being zero should be
  10405. * fixed, so that the crtc_state can be safely duplicated. For now,
  10406. * only fields that are know to not cause problems are preserved. */
  10407. tmp_state = crtc_state->base;
  10408. scaler_state = crtc_state->scaler_state;
  10409. shared_dpll = crtc_state->shared_dpll;
  10410. dpll_hw_state = crtc_state->dpll_hw_state;
  10411. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10412. force_thru = crtc_state->pch_pfit.force_thru;
  10413. memset(crtc_state, 0, sizeof *crtc_state);
  10414. crtc_state->base = tmp_state;
  10415. crtc_state->scaler_state = scaler_state;
  10416. crtc_state->shared_dpll = shared_dpll;
  10417. crtc_state->dpll_hw_state = dpll_hw_state;
  10418. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10419. crtc_state->pch_pfit.force_thru = force_thru;
  10420. }
  10421. static int
  10422. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10423. struct intel_crtc_state *pipe_config)
  10424. {
  10425. struct drm_atomic_state *state = pipe_config->base.state;
  10426. struct intel_encoder *encoder;
  10427. struct drm_connector *connector;
  10428. struct drm_connector_state *connector_state;
  10429. int base_bpp, ret = -EINVAL;
  10430. int i;
  10431. bool retry = true;
  10432. clear_intel_crtc_state(pipe_config);
  10433. pipe_config->cpu_transcoder =
  10434. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10435. /*
  10436. * Sanitize sync polarity flags based on requested ones. If neither
  10437. * positive or negative polarity is requested, treat this as meaning
  10438. * negative polarity.
  10439. */
  10440. if (!(pipe_config->base.adjusted_mode.flags &
  10441. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10442. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10443. if (!(pipe_config->base.adjusted_mode.flags &
  10444. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10445. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10446. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10447. pipe_config);
  10448. if (base_bpp < 0)
  10449. goto fail;
  10450. /*
  10451. * Determine the real pipe dimensions. Note that stereo modes can
  10452. * increase the actual pipe size due to the frame doubling and
  10453. * insertion of additional space for blanks between the frame. This
  10454. * is stored in the crtc timings. We use the requested mode to do this
  10455. * computation to clearly distinguish it from the adjusted mode, which
  10456. * can be changed by the connectors in the below retry loop.
  10457. */
  10458. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10459. &pipe_config->pipe_src_w,
  10460. &pipe_config->pipe_src_h);
  10461. for_each_connector_in_state(state, connector, connector_state, i) {
  10462. if (connector_state->crtc != crtc)
  10463. continue;
  10464. encoder = to_intel_encoder(connector_state->best_encoder);
  10465. /*
  10466. * Determine output_types before calling the .compute_config()
  10467. * hooks so that the hooks can use this information safely.
  10468. */
  10469. pipe_config->output_types |= 1 << encoder->type;
  10470. }
  10471. encoder_retry:
  10472. /* Ensure the port clock defaults are reset when retrying. */
  10473. pipe_config->port_clock = 0;
  10474. pipe_config->pixel_multiplier = 1;
  10475. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10476. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10477. CRTC_STEREO_DOUBLE);
  10478. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10479. * adjust it according to limitations or connector properties, and also
  10480. * a chance to reject the mode entirely.
  10481. */
  10482. for_each_connector_in_state(state, connector, connector_state, i) {
  10483. if (connector_state->crtc != crtc)
  10484. continue;
  10485. encoder = to_intel_encoder(connector_state->best_encoder);
  10486. if (!(encoder->compute_config(encoder, pipe_config))) {
  10487. DRM_DEBUG_KMS("Encoder config failure\n");
  10488. goto fail;
  10489. }
  10490. }
  10491. /* Set default port clock if not overwritten by the encoder. Needs to be
  10492. * done afterwards in case the encoder adjusts the mode. */
  10493. if (!pipe_config->port_clock)
  10494. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10495. * pipe_config->pixel_multiplier;
  10496. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10497. if (ret < 0) {
  10498. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10499. goto fail;
  10500. }
  10501. if (ret == RETRY) {
  10502. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10503. ret = -EINVAL;
  10504. goto fail;
  10505. }
  10506. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10507. retry = false;
  10508. goto encoder_retry;
  10509. }
  10510. /* Dithering seems to not pass-through bits correctly when it should, so
  10511. * only enable it on 6bpc panels. */
  10512. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10513. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10514. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10515. fail:
  10516. return ret;
  10517. }
  10518. static void
  10519. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10520. {
  10521. struct drm_crtc *crtc;
  10522. struct drm_crtc_state *crtc_state;
  10523. int i;
  10524. /* Double check state. */
  10525. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10526. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10527. /* Update hwmode for vblank functions */
  10528. if (crtc->state->active)
  10529. crtc->hwmode = crtc->state->adjusted_mode;
  10530. else
  10531. crtc->hwmode.crtc_clock = 0;
  10532. /*
  10533. * Update legacy state to satisfy fbc code. This can
  10534. * be removed when fbc uses the atomic state.
  10535. */
  10536. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10537. struct drm_plane_state *plane_state = crtc->primary->state;
  10538. crtc->primary->fb = plane_state->fb;
  10539. crtc->x = plane_state->src_x >> 16;
  10540. crtc->y = plane_state->src_y >> 16;
  10541. }
  10542. }
  10543. }
  10544. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10545. {
  10546. int diff;
  10547. if (clock1 == clock2)
  10548. return true;
  10549. if (!clock1 || !clock2)
  10550. return false;
  10551. diff = abs(clock1 - clock2);
  10552. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10553. return true;
  10554. return false;
  10555. }
  10556. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10557. list_for_each_entry((intel_crtc), \
  10558. &(dev)->mode_config.crtc_list, \
  10559. base.head) \
  10560. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10561. static bool
  10562. intel_compare_m_n(unsigned int m, unsigned int n,
  10563. unsigned int m2, unsigned int n2,
  10564. bool exact)
  10565. {
  10566. if (m == m2 && n == n2)
  10567. return true;
  10568. if (exact || !m || !n || !m2 || !n2)
  10569. return false;
  10570. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10571. if (n > n2) {
  10572. while (n > n2) {
  10573. m2 <<= 1;
  10574. n2 <<= 1;
  10575. }
  10576. } else if (n < n2) {
  10577. while (n < n2) {
  10578. m <<= 1;
  10579. n <<= 1;
  10580. }
  10581. }
  10582. if (n != n2)
  10583. return false;
  10584. return intel_fuzzy_clock_check(m, m2);
  10585. }
  10586. static bool
  10587. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10588. struct intel_link_m_n *m2_n2,
  10589. bool adjust)
  10590. {
  10591. if (m_n->tu == m2_n2->tu &&
  10592. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10593. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10594. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10595. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10596. if (adjust)
  10597. *m2_n2 = *m_n;
  10598. return true;
  10599. }
  10600. return false;
  10601. }
  10602. static bool
  10603. intel_pipe_config_compare(struct drm_device *dev,
  10604. struct intel_crtc_state *current_config,
  10605. struct intel_crtc_state *pipe_config,
  10606. bool adjust)
  10607. {
  10608. bool ret = true;
  10609. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10610. do { \
  10611. if (!adjust) \
  10612. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10613. else \
  10614. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10615. } while (0)
  10616. #define PIPE_CONF_CHECK_X(name) \
  10617. if (current_config->name != pipe_config->name) { \
  10618. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10619. "(expected 0x%08x, found 0x%08x)\n", \
  10620. current_config->name, \
  10621. pipe_config->name); \
  10622. ret = false; \
  10623. }
  10624. #define PIPE_CONF_CHECK_I(name) \
  10625. if (current_config->name != pipe_config->name) { \
  10626. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10627. "(expected %i, found %i)\n", \
  10628. current_config->name, \
  10629. pipe_config->name); \
  10630. ret = false; \
  10631. }
  10632. #define PIPE_CONF_CHECK_P(name) \
  10633. if (current_config->name != pipe_config->name) { \
  10634. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10635. "(expected %p, found %p)\n", \
  10636. current_config->name, \
  10637. pipe_config->name); \
  10638. ret = false; \
  10639. }
  10640. #define PIPE_CONF_CHECK_M_N(name) \
  10641. if (!intel_compare_link_m_n(&current_config->name, \
  10642. &pipe_config->name,\
  10643. adjust)) { \
  10644. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10645. "(expected tu %i gmch %i/%i link %i/%i, " \
  10646. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10647. current_config->name.tu, \
  10648. current_config->name.gmch_m, \
  10649. current_config->name.gmch_n, \
  10650. current_config->name.link_m, \
  10651. current_config->name.link_n, \
  10652. pipe_config->name.tu, \
  10653. pipe_config->name.gmch_m, \
  10654. pipe_config->name.gmch_n, \
  10655. pipe_config->name.link_m, \
  10656. pipe_config->name.link_n); \
  10657. ret = false; \
  10658. }
  10659. /* This is required for BDW+ where there is only one set of registers for
  10660. * switching between high and low RR.
  10661. * This macro can be used whenever a comparison has to be made between one
  10662. * hw state and multiple sw state variables.
  10663. */
  10664. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10665. if (!intel_compare_link_m_n(&current_config->name, \
  10666. &pipe_config->name, adjust) && \
  10667. !intel_compare_link_m_n(&current_config->alt_name, \
  10668. &pipe_config->name, adjust)) { \
  10669. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10670. "(expected tu %i gmch %i/%i link %i/%i, " \
  10671. "or tu %i gmch %i/%i link %i/%i, " \
  10672. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10673. current_config->name.tu, \
  10674. current_config->name.gmch_m, \
  10675. current_config->name.gmch_n, \
  10676. current_config->name.link_m, \
  10677. current_config->name.link_n, \
  10678. current_config->alt_name.tu, \
  10679. current_config->alt_name.gmch_m, \
  10680. current_config->alt_name.gmch_n, \
  10681. current_config->alt_name.link_m, \
  10682. current_config->alt_name.link_n, \
  10683. pipe_config->name.tu, \
  10684. pipe_config->name.gmch_m, \
  10685. pipe_config->name.gmch_n, \
  10686. pipe_config->name.link_m, \
  10687. pipe_config->name.link_n); \
  10688. ret = false; \
  10689. }
  10690. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10691. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10692. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10693. "(expected %i, found %i)\n", \
  10694. current_config->name & (mask), \
  10695. pipe_config->name & (mask)); \
  10696. ret = false; \
  10697. }
  10698. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10699. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10700. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10701. "(expected %i, found %i)\n", \
  10702. current_config->name, \
  10703. pipe_config->name); \
  10704. ret = false; \
  10705. }
  10706. #define PIPE_CONF_QUIRK(quirk) \
  10707. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10708. PIPE_CONF_CHECK_I(cpu_transcoder);
  10709. PIPE_CONF_CHECK_I(has_pch_encoder);
  10710. PIPE_CONF_CHECK_I(fdi_lanes);
  10711. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10712. PIPE_CONF_CHECK_I(lane_count);
  10713. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  10714. if (INTEL_INFO(dev)->gen < 8) {
  10715. PIPE_CONF_CHECK_M_N(dp_m_n);
  10716. if (current_config->has_drrs)
  10717. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10718. } else
  10719. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10720. PIPE_CONF_CHECK_X(output_types);
  10721. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10722. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10723. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10724. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10725. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10726. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10727. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10728. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10729. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10730. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10731. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10732. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10733. PIPE_CONF_CHECK_I(pixel_multiplier);
  10734. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10735. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10736. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10737. PIPE_CONF_CHECK_I(limited_color_range);
  10738. PIPE_CONF_CHECK_I(has_infoframe);
  10739. PIPE_CONF_CHECK_I(has_audio);
  10740. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10741. DRM_MODE_FLAG_INTERLACE);
  10742. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10743. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10744. DRM_MODE_FLAG_PHSYNC);
  10745. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10746. DRM_MODE_FLAG_NHSYNC);
  10747. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10748. DRM_MODE_FLAG_PVSYNC);
  10749. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10750. DRM_MODE_FLAG_NVSYNC);
  10751. }
  10752. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10753. /* pfit ratios are autocomputed by the hw on gen4+ */
  10754. if (INTEL_INFO(dev)->gen < 4)
  10755. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10756. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10757. if (!adjust) {
  10758. PIPE_CONF_CHECK_I(pipe_src_w);
  10759. PIPE_CONF_CHECK_I(pipe_src_h);
  10760. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10761. if (current_config->pch_pfit.enabled) {
  10762. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10763. PIPE_CONF_CHECK_X(pch_pfit.size);
  10764. }
  10765. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10766. }
  10767. /* BDW+ don't expose a synchronous way to read the state */
  10768. if (IS_HASWELL(dev))
  10769. PIPE_CONF_CHECK_I(ips_enabled);
  10770. PIPE_CONF_CHECK_I(double_wide);
  10771. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10772. PIPE_CONF_CHECK_P(shared_dpll);
  10773. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10774. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10775. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10776. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10777. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10778. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10779. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10780. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10781. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10782. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10783. PIPE_CONF_CHECK_X(dsi_pll.div);
  10784. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10785. PIPE_CONF_CHECK_I(pipe_bpp);
  10786. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10787. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10788. #undef PIPE_CONF_CHECK_X
  10789. #undef PIPE_CONF_CHECK_I
  10790. #undef PIPE_CONF_CHECK_P
  10791. #undef PIPE_CONF_CHECK_FLAGS
  10792. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10793. #undef PIPE_CONF_QUIRK
  10794. #undef INTEL_ERR_OR_DBG_KMS
  10795. return ret;
  10796. }
  10797. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10798. const struct intel_crtc_state *pipe_config)
  10799. {
  10800. if (pipe_config->has_pch_encoder) {
  10801. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10802. &pipe_config->fdi_m_n);
  10803. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10804. /*
  10805. * FDI already provided one idea for the dotclock.
  10806. * Yell if the encoder disagrees.
  10807. */
  10808. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10809. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10810. fdi_dotclock, dotclock);
  10811. }
  10812. }
  10813. static void verify_wm_state(struct drm_crtc *crtc,
  10814. struct drm_crtc_state *new_state)
  10815. {
  10816. struct drm_device *dev = crtc->dev;
  10817. struct drm_i915_private *dev_priv = to_i915(dev);
  10818. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10819. struct skl_ddb_entry *hw_entry, *sw_entry;
  10820. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10821. const enum pipe pipe = intel_crtc->pipe;
  10822. int plane;
  10823. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10824. return;
  10825. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10826. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10827. /* planes */
  10828. for_each_plane(dev_priv, pipe, plane) {
  10829. hw_entry = &hw_ddb.plane[pipe][plane];
  10830. sw_entry = &sw_ddb->plane[pipe][plane];
  10831. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10832. continue;
  10833. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10834. "(expected (%u,%u), found (%u,%u))\n",
  10835. pipe_name(pipe), plane + 1,
  10836. sw_entry->start, sw_entry->end,
  10837. hw_entry->start, hw_entry->end);
  10838. }
  10839. /* cursor */
  10840. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10841. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10842. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10843. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10844. "(expected (%u,%u), found (%u,%u))\n",
  10845. pipe_name(pipe),
  10846. sw_entry->start, sw_entry->end,
  10847. hw_entry->start, hw_entry->end);
  10848. }
  10849. }
  10850. static void
  10851. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10852. {
  10853. struct drm_connector *connector;
  10854. drm_for_each_connector(connector, dev) {
  10855. struct drm_encoder *encoder = connector->encoder;
  10856. struct drm_connector_state *state = connector->state;
  10857. if (state->crtc != crtc)
  10858. continue;
  10859. intel_connector_verify_state(to_intel_connector(connector));
  10860. I915_STATE_WARN(state->best_encoder != encoder,
  10861. "connector's atomic encoder doesn't match legacy encoder\n");
  10862. }
  10863. }
  10864. static void
  10865. verify_encoder_state(struct drm_device *dev)
  10866. {
  10867. struct intel_encoder *encoder;
  10868. struct intel_connector *connector;
  10869. for_each_intel_encoder(dev, encoder) {
  10870. bool enabled = false;
  10871. enum pipe pipe;
  10872. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10873. encoder->base.base.id,
  10874. encoder->base.name);
  10875. for_each_intel_connector(dev, connector) {
  10876. if (connector->base.state->best_encoder != &encoder->base)
  10877. continue;
  10878. enabled = true;
  10879. I915_STATE_WARN(connector->base.state->crtc !=
  10880. encoder->base.crtc,
  10881. "connector's crtc doesn't match encoder crtc\n");
  10882. }
  10883. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10884. "encoder's enabled state mismatch "
  10885. "(expected %i, found %i)\n",
  10886. !!encoder->base.crtc, enabled);
  10887. if (!encoder->base.crtc) {
  10888. bool active;
  10889. active = encoder->get_hw_state(encoder, &pipe);
  10890. I915_STATE_WARN(active,
  10891. "encoder detached but still enabled on pipe %c.\n",
  10892. pipe_name(pipe));
  10893. }
  10894. }
  10895. }
  10896. static void
  10897. verify_crtc_state(struct drm_crtc *crtc,
  10898. struct drm_crtc_state *old_crtc_state,
  10899. struct drm_crtc_state *new_crtc_state)
  10900. {
  10901. struct drm_device *dev = crtc->dev;
  10902. struct drm_i915_private *dev_priv = to_i915(dev);
  10903. struct intel_encoder *encoder;
  10904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10905. struct intel_crtc_state *pipe_config, *sw_config;
  10906. struct drm_atomic_state *old_state;
  10907. bool active;
  10908. old_state = old_crtc_state->state;
  10909. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10910. pipe_config = to_intel_crtc_state(old_crtc_state);
  10911. memset(pipe_config, 0, sizeof(*pipe_config));
  10912. pipe_config->base.crtc = crtc;
  10913. pipe_config->base.state = old_state;
  10914. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10915. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10916. /* hw state is inconsistent with the pipe quirk */
  10917. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10918. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10919. active = new_crtc_state->active;
  10920. I915_STATE_WARN(new_crtc_state->active != active,
  10921. "crtc active state doesn't match with hw state "
  10922. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10923. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10924. "transitional active state does not match atomic hw state "
  10925. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10926. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10927. enum pipe pipe;
  10928. active = encoder->get_hw_state(encoder, &pipe);
  10929. I915_STATE_WARN(active != new_crtc_state->active,
  10930. "[ENCODER:%i] active %i with crtc active %i\n",
  10931. encoder->base.base.id, active, new_crtc_state->active);
  10932. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10933. "Encoder connected to wrong pipe %c\n",
  10934. pipe_name(pipe));
  10935. if (active) {
  10936. pipe_config->output_types |= 1 << encoder->type;
  10937. encoder->get_config(encoder, pipe_config);
  10938. }
  10939. }
  10940. if (!new_crtc_state->active)
  10941. return;
  10942. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10943. sw_config = to_intel_crtc_state(crtc->state);
  10944. if (!intel_pipe_config_compare(dev, sw_config,
  10945. pipe_config, false)) {
  10946. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10947. intel_dump_pipe_config(intel_crtc, pipe_config,
  10948. "[hw state]");
  10949. intel_dump_pipe_config(intel_crtc, sw_config,
  10950. "[sw state]");
  10951. }
  10952. }
  10953. static void
  10954. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10955. struct intel_shared_dpll *pll,
  10956. struct drm_crtc *crtc,
  10957. struct drm_crtc_state *new_state)
  10958. {
  10959. struct intel_dpll_hw_state dpll_hw_state;
  10960. unsigned crtc_mask;
  10961. bool active;
  10962. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10963. DRM_DEBUG_KMS("%s\n", pll->name);
  10964. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10965. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10966. I915_STATE_WARN(!pll->on && pll->active_mask,
  10967. "pll in active use but not on in sw tracking\n");
  10968. I915_STATE_WARN(pll->on && !pll->active_mask,
  10969. "pll is on but not used by any active crtc\n");
  10970. I915_STATE_WARN(pll->on != active,
  10971. "pll on state mismatch (expected %i, found %i)\n",
  10972. pll->on, active);
  10973. }
  10974. if (!crtc) {
  10975. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10976. "more active pll users than references: %x vs %x\n",
  10977. pll->active_mask, pll->config.crtc_mask);
  10978. return;
  10979. }
  10980. crtc_mask = 1 << drm_crtc_index(crtc);
  10981. if (new_state->active)
  10982. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10983. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10984. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10985. else
  10986. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10987. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10988. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10989. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10990. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10991. crtc_mask, pll->config.crtc_mask);
  10992. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10993. &dpll_hw_state,
  10994. sizeof(dpll_hw_state)),
  10995. "pll hw state mismatch\n");
  10996. }
  10997. static void
  10998. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10999. struct drm_crtc_state *old_crtc_state,
  11000. struct drm_crtc_state *new_crtc_state)
  11001. {
  11002. struct drm_i915_private *dev_priv = to_i915(dev);
  11003. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11004. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11005. if (new_state->shared_dpll)
  11006. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11007. if (old_state->shared_dpll &&
  11008. old_state->shared_dpll != new_state->shared_dpll) {
  11009. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11010. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11011. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11012. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11013. pipe_name(drm_crtc_index(crtc)));
  11014. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11015. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11016. pipe_name(drm_crtc_index(crtc)));
  11017. }
  11018. }
  11019. static void
  11020. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11021. struct drm_crtc_state *old_state,
  11022. struct drm_crtc_state *new_state)
  11023. {
  11024. if (!needs_modeset(new_state) &&
  11025. !to_intel_crtc_state(new_state)->update_pipe)
  11026. return;
  11027. verify_wm_state(crtc, new_state);
  11028. verify_connector_state(crtc->dev, crtc);
  11029. verify_crtc_state(crtc, old_state, new_state);
  11030. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11031. }
  11032. static void
  11033. verify_disabled_dpll_state(struct drm_device *dev)
  11034. {
  11035. struct drm_i915_private *dev_priv = to_i915(dev);
  11036. int i;
  11037. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11038. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11039. }
  11040. static void
  11041. intel_modeset_verify_disabled(struct drm_device *dev)
  11042. {
  11043. verify_encoder_state(dev);
  11044. verify_connector_state(dev, NULL);
  11045. verify_disabled_dpll_state(dev);
  11046. }
  11047. static void update_scanline_offset(struct intel_crtc *crtc)
  11048. {
  11049. struct drm_device *dev = crtc->base.dev;
  11050. /*
  11051. * The scanline counter increments at the leading edge of hsync.
  11052. *
  11053. * On most platforms it starts counting from vtotal-1 on the
  11054. * first active line. That means the scanline counter value is
  11055. * always one less than what we would expect. Ie. just after
  11056. * start of vblank, which also occurs at start of hsync (on the
  11057. * last active line), the scanline counter will read vblank_start-1.
  11058. *
  11059. * On gen2 the scanline counter starts counting from 1 instead
  11060. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11061. * to keep the value positive), instead of adding one.
  11062. *
  11063. * On HSW+ the behaviour of the scanline counter depends on the output
  11064. * type. For DP ports it behaves like most other platforms, but on HDMI
  11065. * there's an extra 1 line difference. So we need to add two instead of
  11066. * one to the value.
  11067. */
  11068. if (IS_GEN2(dev)) {
  11069. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11070. int vtotal;
  11071. vtotal = adjusted_mode->crtc_vtotal;
  11072. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11073. vtotal /= 2;
  11074. crtc->scanline_offset = vtotal - 1;
  11075. } else if (HAS_DDI(dev) &&
  11076. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11077. crtc->scanline_offset = 2;
  11078. } else
  11079. crtc->scanline_offset = 1;
  11080. }
  11081. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11082. {
  11083. struct drm_device *dev = state->dev;
  11084. struct drm_i915_private *dev_priv = to_i915(dev);
  11085. struct intel_shared_dpll_config *shared_dpll = NULL;
  11086. struct drm_crtc *crtc;
  11087. struct drm_crtc_state *crtc_state;
  11088. int i;
  11089. if (!dev_priv->display.crtc_compute_clock)
  11090. return;
  11091. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11093. struct intel_shared_dpll *old_dpll =
  11094. to_intel_crtc_state(crtc->state)->shared_dpll;
  11095. if (!needs_modeset(crtc_state))
  11096. continue;
  11097. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11098. if (!old_dpll)
  11099. continue;
  11100. if (!shared_dpll)
  11101. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11102. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11103. }
  11104. }
  11105. /*
  11106. * This implements the workaround described in the "notes" section of the mode
  11107. * set sequence documentation. When going from no pipes or single pipe to
  11108. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11109. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11110. */
  11111. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11112. {
  11113. struct drm_crtc_state *crtc_state;
  11114. struct intel_crtc *intel_crtc;
  11115. struct drm_crtc *crtc;
  11116. struct intel_crtc_state *first_crtc_state = NULL;
  11117. struct intel_crtc_state *other_crtc_state = NULL;
  11118. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11119. int i;
  11120. /* look at all crtc's that are going to be enabled in during modeset */
  11121. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11122. intel_crtc = to_intel_crtc(crtc);
  11123. if (!crtc_state->active || !needs_modeset(crtc_state))
  11124. continue;
  11125. if (first_crtc_state) {
  11126. other_crtc_state = to_intel_crtc_state(crtc_state);
  11127. break;
  11128. } else {
  11129. first_crtc_state = to_intel_crtc_state(crtc_state);
  11130. first_pipe = intel_crtc->pipe;
  11131. }
  11132. }
  11133. /* No workaround needed? */
  11134. if (!first_crtc_state)
  11135. return 0;
  11136. /* w/a possibly needed, check how many crtc's are already enabled. */
  11137. for_each_intel_crtc(state->dev, intel_crtc) {
  11138. struct intel_crtc_state *pipe_config;
  11139. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11140. if (IS_ERR(pipe_config))
  11141. return PTR_ERR(pipe_config);
  11142. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11143. if (!pipe_config->base.active ||
  11144. needs_modeset(&pipe_config->base))
  11145. continue;
  11146. /* 2 or more enabled crtcs means no need for w/a */
  11147. if (enabled_pipe != INVALID_PIPE)
  11148. return 0;
  11149. enabled_pipe = intel_crtc->pipe;
  11150. }
  11151. if (enabled_pipe != INVALID_PIPE)
  11152. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11153. else if (other_crtc_state)
  11154. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11155. return 0;
  11156. }
  11157. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11158. {
  11159. struct drm_crtc *crtc;
  11160. struct drm_crtc_state *crtc_state;
  11161. int ret = 0;
  11162. /* add all active pipes to the state */
  11163. for_each_crtc(state->dev, crtc) {
  11164. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11165. if (IS_ERR(crtc_state))
  11166. return PTR_ERR(crtc_state);
  11167. if (!crtc_state->active || needs_modeset(crtc_state))
  11168. continue;
  11169. crtc_state->mode_changed = true;
  11170. ret = drm_atomic_add_affected_connectors(state, crtc);
  11171. if (ret)
  11172. break;
  11173. ret = drm_atomic_add_affected_planes(state, crtc);
  11174. if (ret)
  11175. break;
  11176. }
  11177. return ret;
  11178. }
  11179. static int intel_modeset_checks(struct drm_atomic_state *state)
  11180. {
  11181. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11182. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11183. struct drm_crtc *crtc;
  11184. struct drm_crtc_state *crtc_state;
  11185. int ret = 0, i;
  11186. if (!check_digital_port_conflicts(state)) {
  11187. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11188. return -EINVAL;
  11189. }
  11190. intel_state->modeset = true;
  11191. intel_state->active_crtcs = dev_priv->active_crtcs;
  11192. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11193. if (crtc_state->active)
  11194. intel_state->active_crtcs |= 1 << i;
  11195. else
  11196. intel_state->active_crtcs &= ~(1 << i);
  11197. if (crtc_state->active != crtc->state->active)
  11198. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11199. }
  11200. /*
  11201. * See if the config requires any additional preparation, e.g.
  11202. * to adjust global state with pipes off. We need to do this
  11203. * here so we can get the modeset_pipe updated config for the new
  11204. * mode set on this crtc. For other crtcs we need to use the
  11205. * adjusted_mode bits in the crtc directly.
  11206. */
  11207. if (dev_priv->display.modeset_calc_cdclk) {
  11208. if (!intel_state->cdclk_pll_vco)
  11209. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11210. if (!intel_state->cdclk_pll_vco)
  11211. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11212. ret = dev_priv->display.modeset_calc_cdclk(state);
  11213. if (ret < 0)
  11214. return ret;
  11215. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11216. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11217. ret = intel_modeset_all_pipes(state);
  11218. if (ret < 0)
  11219. return ret;
  11220. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11221. intel_state->cdclk, intel_state->dev_cdclk);
  11222. } else
  11223. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11224. intel_modeset_clear_plls(state);
  11225. if (IS_HASWELL(dev_priv))
  11226. return haswell_mode_set_planes_workaround(state);
  11227. return 0;
  11228. }
  11229. /*
  11230. * Handle calculation of various watermark data at the end of the atomic check
  11231. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11232. * handlers to ensure that all derived state has been updated.
  11233. */
  11234. static int calc_watermark_data(struct drm_atomic_state *state)
  11235. {
  11236. struct drm_device *dev = state->dev;
  11237. struct drm_i915_private *dev_priv = to_i915(dev);
  11238. /* Is there platform-specific watermark information to calculate? */
  11239. if (dev_priv->display.compute_global_watermarks)
  11240. return dev_priv->display.compute_global_watermarks(state);
  11241. return 0;
  11242. }
  11243. /**
  11244. * intel_atomic_check - validate state object
  11245. * @dev: drm device
  11246. * @state: state to validate
  11247. */
  11248. static int intel_atomic_check(struct drm_device *dev,
  11249. struct drm_atomic_state *state)
  11250. {
  11251. struct drm_i915_private *dev_priv = to_i915(dev);
  11252. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11253. struct drm_crtc *crtc;
  11254. struct drm_crtc_state *crtc_state;
  11255. int ret, i;
  11256. bool any_ms = false;
  11257. ret = drm_atomic_helper_check_modeset(dev, state);
  11258. if (ret)
  11259. return ret;
  11260. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11261. struct intel_crtc_state *pipe_config =
  11262. to_intel_crtc_state(crtc_state);
  11263. /* Catch I915_MODE_FLAG_INHERITED */
  11264. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11265. crtc_state->mode_changed = true;
  11266. if (!needs_modeset(crtc_state))
  11267. continue;
  11268. if (!crtc_state->enable) {
  11269. any_ms = true;
  11270. continue;
  11271. }
  11272. /* FIXME: For only active_changed we shouldn't need to do any
  11273. * state recomputation at all. */
  11274. ret = drm_atomic_add_affected_connectors(state, crtc);
  11275. if (ret)
  11276. return ret;
  11277. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11278. if (ret) {
  11279. intel_dump_pipe_config(to_intel_crtc(crtc),
  11280. pipe_config, "[failed]");
  11281. return ret;
  11282. }
  11283. if (i915.fastboot &&
  11284. intel_pipe_config_compare(dev,
  11285. to_intel_crtc_state(crtc->state),
  11286. pipe_config, true)) {
  11287. crtc_state->mode_changed = false;
  11288. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11289. }
  11290. if (needs_modeset(crtc_state))
  11291. any_ms = true;
  11292. ret = drm_atomic_add_affected_planes(state, crtc);
  11293. if (ret)
  11294. return ret;
  11295. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11296. needs_modeset(crtc_state) ?
  11297. "[modeset]" : "[fastset]");
  11298. }
  11299. if (any_ms) {
  11300. ret = intel_modeset_checks(state);
  11301. if (ret)
  11302. return ret;
  11303. } else
  11304. intel_state->cdclk = dev_priv->cdclk_freq;
  11305. ret = drm_atomic_helper_check_planes(dev, state);
  11306. if (ret)
  11307. return ret;
  11308. intel_fbc_choose_crtc(dev_priv, state);
  11309. return calc_watermark_data(state);
  11310. }
  11311. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11312. struct drm_atomic_state *state,
  11313. bool nonblock)
  11314. {
  11315. struct drm_i915_private *dev_priv = to_i915(dev);
  11316. struct drm_plane_state *plane_state;
  11317. struct drm_crtc_state *crtc_state;
  11318. struct drm_plane *plane;
  11319. struct drm_crtc *crtc;
  11320. int i, ret;
  11321. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11322. if (state->legacy_cursor_update)
  11323. continue;
  11324. ret = intel_crtc_wait_for_pending_flips(crtc);
  11325. if (ret)
  11326. return ret;
  11327. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11328. flush_workqueue(dev_priv->wq);
  11329. }
  11330. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11331. if (ret)
  11332. return ret;
  11333. ret = drm_atomic_helper_prepare_planes(dev, state);
  11334. mutex_unlock(&dev->struct_mutex);
  11335. if (!ret && !nonblock) {
  11336. for_each_plane_in_state(state, plane, plane_state, i) {
  11337. struct intel_plane_state *intel_plane_state =
  11338. to_intel_plane_state(plane_state);
  11339. if (!intel_plane_state->wait_req)
  11340. continue;
  11341. ret = __i915_wait_request(intel_plane_state->wait_req,
  11342. true, NULL, NULL);
  11343. if (ret) {
  11344. /* Any hang should be swallowed by the wait */
  11345. WARN_ON(ret == -EIO);
  11346. mutex_lock(&dev->struct_mutex);
  11347. drm_atomic_helper_cleanup_planes(dev, state);
  11348. mutex_unlock(&dev->struct_mutex);
  11349. break;
  11350. }
  11351. }
  11352. }
  11353. return ret;
  11354. }
  11355. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11356. {
  11357. struct drm_device *dev = crtc->base.dev;
  11358. if (!dev->max_vblank_count)
  11359. return drm_accurate_vblank_count(&crtc->base);
  11360. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11361. }
  11362. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11363. struct drm_i915_private *dev_priv,
  11364. unsigned crtc_mask)
  11365. {
  11366. unsigned last_vblank_count[I915_MAX_PIPES];
  11367. enum pipe pipe;
  11368. int ret;
  11369. if (!crtc_mask)
  11370. return;
  11371. for_each_pipe(dev_priv, pipe) {
  11372. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11373. if (!((1 << pipe) & crtc_mask))
  11374. continue;
  11375. ret = drm_crtc_vblank_get(crtc);
  11376. if (WARN_ON(ret != 0)) {
  11377. crtc_mask &= ~(1 << pipe);
  11378. continue;
  11379. }
  11380. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11381. }
  11382. for_each_pipe(dev_priv, pipe) {
  11383. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11384. long lret;
  11385. if (!((1 << pipe) & crtc_mask))
  11386. continue;
  11387. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11388. last_vblank_count[pipe] !=
  11389. drm_crtc_vblank_count(crtc),
  11390. msecs_to_jiffies(50));
  11391. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11392. drm_crtc_vblank_put(crtc);
  11393. }
  11394. }
  11395. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11396. {
  11397. /* fb updated, need to unpin old fb */
  11398. if (crtc_state->fb_changed)
  11399. return true;
  11400. /* wm changes, need vblank before final wm's */
  11401. if (crtc_state->update_wm_post)
  11402. return true;
  11403. /*
  11404. * cxsr is re-enabled after vblank.
  11405. * This is already handled by crtc_state->update_wm_post,
  11406. * but added for clarity.
  11407. */
  11408. if (crtc_state->disable_cxsr)
  11409. return true;
  11410. return false;
  11411. }
  11412. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11413. {
  11414. struct drm_device *dev = state->dev;
  11415. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11416. struct drm_i915_private *dev_priv = to_i915(dev);
  11417. struct drm_crtc_state *old_crtc_state;
  11418. struct drm_crtc *crtc;
  11419. struct intel_crtc_state *intel_cstate;
  11420. struct drm_plane *plane;
  11421. struct drm_plane_state *plane_state;
  11422. bool hw_check = intel_state->modeset;
  11423. unsigned long put_domains[I915_MAX_PIPES] = {};
  11424. unsigned crtc_vblank_mask = 0;
  11425. int i, ret;
  11426. for_each_plane_in_state(state, plane, plane_state, i) {
  11427. struct intel_plane_state *intel_plane_state =
  11428. to_intel_plane_state(plane_state);
  11429. if (!intel_plane_state->wait_req)
  11430. continue;
  11431. ret = __i915_wait_request(intel_plane_state->wait_req,
  11432. true, NULL, NULL);
  11433. /* EIO should be eaten, and we can't get interrupted in the
  11434. * worker, and blocking commits have waited already. */
  11435. WARN_ON(ret);
  11436. }
  11437. drm_atomic_helper_wait_for_dependencies(state);
  11438. if (intel_state->modeset) {
  11439. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11440. sizeof(intel_state->min_pixclk));
  11441. dev_priv->active_crtcs = intel_state->active_crtcs;
  11442. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11443. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11444. }
  11445. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11446. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11447. if (needs_modeset(crtc->state) ||
  11448. to_intel_crtc_state(crtc->state)->update_pipe) {
  11449. hw_check = true;
  11450. put_domains[to_intel_crtc(crtc)->pipe] =
  11451. modeset_get_crtc_power_domains(crtc,
  11452. to_intel_crtc_state(crtc->state));
  11453. }
  11454. if (!needs_modeset(crtc->state))
  11455. continue;
  11456. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11457. if (old_crtc_state->active) {
  11458. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11459. dev_priv->display.crtc_disable(crtc);
  11460. intel_crtc->active = false;
  11461. intel_fbc_disable(intel_crtc);
  11462. intel_disable_shared_dpll(intel_crtc);
  11463. /*
  11464. * Underruns don't always raise
  11465. * interrupts, so check manually.
  11466. */
  11467. intel_check_cpu_fifo_underruns(dev_priv);
  11468. intel_check_pch_fifo_underruns(dev_priv);
  11469. if (!crtc->state->active)
  11470. intel_update_watermarks(crtc);
  11471. }
  11472. }
  11473. /* Only after disabling all output pipelines that will be changed can we
  11474. * update the the output configuration. */
  11475. intel_modeset_update_crtc_state(state);
  11476. if (intel_state->modeset) {
  11477. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11478. if (dev_priv->display.modeset_commit_cdclk &&
  11479. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11480. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11481. dev_priv->display.modeset_commit_cdclk(state);
  11482. intel_modeset_verify_disabled(dev);
  11483. }
  11484. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11485. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11487. bool modeset = needs_modeset(crtc->state);
  11488. struct intel_crtc_state *pipe_config =
  11489. to_intel_crtc_state(crtc->state);
  11490. if (modeset && crtc->state->active) {
  11491. update_scanline_offset(to_intel_crtc(crtc));
  11492. dev_priv->display.crtc_enable(crtc);
  11493. }
  11494. /* Complete events for now disable pipes here. */
  11495. if (modeset && !crtc->state->active && crtc->state->event) {
  11496. spin_lock_irq(&dev->event_lock);
  11497. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  11498. spin_unlock_irq(&dev->event_lock);
  11499. crtc->state->event = NULL;
  11500. }
  11501. if (!modeset)
  11502. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11503. if (crtc->state->active &&
  11504. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11505. intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
  11506. if (crtc->state->active)
  11507. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11508. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11509. crtc_vblank_mask |= 1 << i;
  11510. }
  11511. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  11512. * already, but still need the state for the delayed optimization. To
  11513. * fix this:
  11514. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  11515. * - schedule that vblank worker _before_ calling hw_done
  11516. * - at the start of commit_tail, cancel it _synchrously
  11517. * - switch over to the vblank wait helper in the core after that since
  11518. * we don't need out special handling any more.
  11519. */
  11520. if (!state->legacy_cursor_update)
  11521. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11522. /*
  11523. * Now that the vblank has passed, we can go ahead and program the
  11524. * optimal watermarks on platforms that need two-step watermark
  11525. * programming.
  11526. *
  11527. * TODO: Move this (and other cleanup) to an async worker eventually.
  11528. */
  11529. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11530. intel_cstate = to_intel_crtc_state(crtc->state);
  11531. if (dev_priv->display.optimize_watermarks)
  11532. dev_priv->display.optimize_watermarks(intel_cstate);
  11533. }
  11534. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11535. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11536. if (put_domains[i])
  11537. modeset_put_power_domains(dev_priv, put_domains[i]);
  11538. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11539. }
  11540. drm_atomic_helper_commit_hw_done(state);
  11541. if (intel_state->modeset)
  11542. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11543. mutex_lock(&dev->struct_mutex);
  11544. drm_atomic_helper_cleanup_planes(dev, state);
  11545. mutex_unlock(&dev->struct_mutex);
  11546. drm_atomic_helper_commit_cleanup_done(state);
  11547. drm_atomic_state_free(state);
  11548. /* As one of the primary mmio accessors, KMS has a high likelihood
  11549. * of triggering bugs in unclaimed access. After we finish
  11550. * modesetting, see if an error has been flagged, and if so
  11551. * enable debugging for the next modeset - and hope we catch
  11552. * the culprit.
  11553. *
  11554. * XXX note that we assume display power is on at this point.
  11555. * This might hold true now but we need to add pm helper to check
  11556. * unclaimed only when the hardware is on, as atomic commits
  11557. * can happen also when the device is completely off.
  11558. */
  11559. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11560. }
  11561. static void intel_atomic_commit_work(struct work_struct *work)
  11562. {
  11563. struct drm_atomic_state *state = container_of(work,
  11564. struct drm_atomic_state,
  11565. commit_work);
  11566. intel_atomic_commit_tail(state);
  11567. }
  11568. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  11569. {
  11570. struct drm_plane_state *old_plane_state;
  11571. struct drm_plane *plane;
  11572. struct drm_i915_gem_object *obj, *old_obj;
  11573. struct intel_plane *intel_plane;
  11574. int i;
  11575. mutex_lock(&state->dev->struct_mutex);
  11576. for_each_plane_in_state(state, plane, old_plane_state, i) {
  11577. obj = intel_fb_obj(plane->state->fb);
  11578. old_obj = intel_fb_obj(old_plane_state->fb);
  11579. intel_plane = to_intel_plane(plane);
  11580. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11581. }
  11582. mutex_unlock(&state->dev->struct_mutex);
  11583. }
  11584. /**
  11585. * intel_atomic_commit - commit validated state object
  11586. * @dev: DRM device
  11587. * @state: the top-level driver state object
  11588. * @nonblock: nonblocking commit
  11589. *
  11590. * This function commits a top-level state object that has been validated
  11591. * with drm_atomic_helper_check().
  11592. *
  11593. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11594. * nonblocking commits are only safe for pure plane updates. Everything else
  11595. * should work though.
  11596. *
  11597. * RETURNS
  11598. * Zero for success or -errno.
  11599. */
  11600. static int intel_atomic_commit(struct drm_device *dev,
  11601. struct drm_atomic_state *state,
  11602. bool nonblock)
  11603. {
  11604. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11605. struct drm_i915_private *dev_priv = to_i915(dev);
  11606. int ret = 0;
  11607. if (intel_state->modeset && nonblock) {
  11608. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  11609. return -EINVAL;
  11610. }
  11611. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11612. if (ret)
  11613. return ret;
  11614. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  11615. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11616. if (ret) {
  11617. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11618. return ret;
  11619. }
  11620. drm_atomic_helper_swap_state(state, true);
  11621. dev_priv->wm.distrust_bios_wm = false;
  11622. dev_priv->wm.skl_results = intel_state->wm_results;
  11623. intel_shared_dpll_commit(state);
  11624. intel_atomic_track_fbs(state);
  11625. if (nonblock)
  11626. queue_work(system_unbound_wq, &state->commit_work);
  11627. else
  11628. intel_atomic_commit_tail(state);
  11629. return 0;
  11630. }
  11631. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11632. {
  11633. struct drm_device *dev = crtc->dev;
  11634. struct drm_atomic_state *state;
  11635. struct drm_crtc_state *crtc_state;
  11636. int ret;
  11637. state = drm_atomic_state_alloc(dev);
  11638. if (!state) {
  11639. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11640. crtc->base.id, crtc->name);
  11641. return;
  11642. }
  11643. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11644. retry:
  11645. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11646. ret = PTR_ERR_OR_ZERO(crtc_state);
  11647. if (!ret) {
  11648. if (!crtc_state->active)
  11649. goto out;
  11650. crtc_state->mode_changed = true;
  11651. ret = drm_atomic_commit(state);
  11652. }
  11653. if (ret == -EDEADLK) {
  11654. drm_atomic_state_clear(state);
  11655. drm_modeset_backoff(state->acquire_ctx);
  11656. goto retry;
  11657. }
  11658. if (ret)
  11659. out:
  11660. drm_atomic_state_free(state);
  11661. }
  11662. #undef for_each_intel_crtc_masked
  11663. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11664. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11665. .set_config = drm_atomic_helper_set_config,
  11666. .set_property = drm_atomic_helper_crtc_set_property,
  11667. .destroy = intel_crtc_destroy,
  11668. .page_flip = intel_crtc_page_flip,
  11669. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11670. .atomic_destroy_state = intel_crtc_destroy_state,
  11671. };
  11672. /**
  11673. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11674. * @plane: drm plane to prepare for
  11675. * @fb: framebuffer to prepare for presentation
  11676. *
  11677. * Prepares a framebuffer for usage on a display plane. Generally this
  11678. * involves pinning the underlying object and updating the frontbuffer tracking
  11679. * bits. Some older platforms need special physical address handling for
  11680. * cursor planes.
  11681. *
  11682. * Must be called with struct_mutex held.
  11683. *
  11684. * Returns 0 on success, negative error code on failure.
  11685. */
  11686. int
  11687. intel_prepare_plane_fb(struct drm_plane *plane,
  11688. const struct drm_plane_state *new_state)
  11689. {
  11690. struct drm_device *dev = plane->dev;
  11691. struct drm_framebuffer *fb = new_state->fb;
  11692. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11693. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11694. struct reservation_object *resv;
  11695. int ret = 0;
  11696. if (!obj && !old_obj)
  11697. return 0;
  11698. if (old_obj) {
  11699. struct drm_crtc_state *crtc_state =
  11700. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11701. /* Big Hammer, we also need to ensure that any pending
  11702. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11703. * current scanout is retired before unpinning the old
  11704. * framebuffer. Note that we rely on userspace rendering
  11705. * into the buffer attached to the pipe they are waiting
  11706. * on. If not, userspace generates a GPU hang with IPEHR
  11707. * point to the MI_WAIT_FOR_EVENT.
  11708. *
  11709. * This should only fail upon a hung GPU, in which case we
  11710. * can safely continue.
  11711. */
  11712. if (needs_modeset(crtc_state))
  11713. ret = i915_gem_object_wait_rendering(old_obj, true);
  11714. if (ret) {
  11715. /* GPU hangs should have been swallowed by the wait */
  11716. WARN_ON(ret == -EIO);
  11717. return ret;
  11718. }
  11719. }
  11720. if (!obj)
  11721. return 0;
  11722. /* For framebuffer backed by dmabuf, wait for fence */
  11723. resv = i915_gem_object_get_dmabuf_resv(obj);
  11724. if (resv) {
  11725. long lret;
  11726. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  11727. MAX_SCHEDULE_TIMEOUT);
  11728. if (lret == -ERESTARTSYS)
  11729. return lret;
  11730. WARN(lret < 0, "waiting returns %li\n", lret);
  11731. }
  11732. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11733. INTEL_INFO(dev)->cursor_needs_physical) {
  11734. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11735. ret = i915_gem_object_attach_phys(obj, align);
  11736. if (ret)
  11737. DRM_DEBUG_KMS("failed to attach phys object\n");
  11738. } else {
  11739. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11740. }
  11741. if (ret == 0) {
  11742. struct intel_plane_state *plane_state =
  11743. to_intel_plane_state(new_state);
  11744. i915_gem_request_assign(&plane_state->wait_req,
  11745. obj->last_write_req);
  11746. }
  11747. return ret;
  11748. }
  11749. /**
  11750. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11751. * @plane: drm plane to clean up for
  11752. * @fb: old framebuffer that was on plane
  11753. *
  11754. * Cleans up a framebuffer that has just been removed from a plane.
  11755. *
  11756. * Must be called with struct_mutex held.
  11757. */
  11758. void
  11759. intel_cleanup_plane_fb(struct drm_plane *plane,
  11760. const struct drm_plane_state *old_state)
  11761. {
  11762. struct drm_device *dev = plane->dev;
  11763. struct intel_plane_state *old_intel_state;
  11764. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11765. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11766. old_intel_state = to_intel_plane_state(old_state);
  11767. if (!obj && !old_obj)
  11768. return;
  11769. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11770. !INTEL_INFO(dev)->cursor_needs_physical))
  11771. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11772. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11773. }
  11774. int
  11775. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11776. {
  11777. int max_scale;
  11778. int crtc_clock, cdclk;
  11779. if (!intel_crtc || !crtc_state->base.enable)
  11780. return DRM_PLANE_HELPER_NO_SCALING;
  11781. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11782. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11783. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11784. return DRM_PLANE_HELPER_NO_SCALING;
  11785. /*
  11786. * skl max scale is lower of:
  11787. * close to 3 but not 3, -1 is for that purpose
  11788. * or
  11789. * cdclk/crtc_clock
  11790. */
  11791. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11792. return max_scale;
  11793. }
  11794. static int
  11795. intel_check_primary_plane(struct drm_plane *plane,
  11796. struct intel_crtc_state *crtc_state,
  11797. struct intel_plane_state *state)
  11798. {
  11799. struct drm_crtc *crtc = state->base.crtc;
  11800. struct drm_framebuffer *fb = state->base.fb;
  11801. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11802. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11803. bool can_position = false;
  11804. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11805. /* use scaler when colorkey is not required */
  11806. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11807. min_scale = 1;
  11808. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11809. }
  11810. can_position = true;
  11811. }
  11812. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11813. &state->dst, &state->clip,
  11814. state->base.rotation,
  11815. min_scale, max_scale,
  11816. can_position, true,
  11817. &state->visible);
  11818. }
  11819. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11820. struct drm_crtc_state *old_crtc_state)
  11821. {
  11822. struct drm_device *dev = crtc->dev;
  11823. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11824. struct intel_crtc_state *old_intel_state =
  11825. to_intel_crtc_state(old_crtc_state);
  11826. bool modeset = needs_modeset(crtc->state);
  11827. /* Perform vblank evasion around commit operation */
  11828. intel_pipe_update_start(intel_crtc);
  11829. if (modeset)
  11830. return;
  11831. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11832. intel_color_set_csc(crtc->state);
  11833. intel_color_load_luts(crtc->state);
  11834. }
  11835. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11836. intel_update_pipe_config(intel_crtc, old_intel_state);
  11837. else if (INTEL_INFO(dev)->gen >= 9)
  11838. skl_detach_scalers(intel_crtc);
  11839. }
  11840. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11841. struct drm_crtc_state *old_crtc_state)
  11842. {
  11843. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11844. intel_pipe_update_end(intel_crtc, NULL);
  11845. }
  11846. /**
  11847. * intel_plane_destroy - destroy a plane
  11848. * @plane: plane to destroy
  11849. *
  11850. * Common destruction function for all types of planes (primary, cursor,
  11851. * sprite).
  11852. */
  11853. void intel_plane_destroy(struct drm_plane *plane)
  11854. {
  11855. if (!plane)
  11856. return;
  11857. drm_plane_cleanup(plane);
  11858. kfree(to_intel_plane(plane));
  11859. }
  11860. const struct drm_plane_funcs intel_plane_funcs = {
  11861. .update_plane = drm_atomic_helper_update_plane,
  11862. .disable_plane = drm_atomic_helper_disable_plane,
  11863. .destroy = intel_plane_destroy,
  11864. .set_property = drm_atomic_helper_plane_set_property,
  11865. .atomic_get_property = intel_plane_atomic_get_property,
  11866. .atomic_set_property = intel_plane_atomic_set_property,
  11867. .atomic_duplicate_state = intel_plane_duplicate_state,
  11868. .atomic_destroy_state = intel_plane_destroy_state,
  11869. };
  11870. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11871. int pipe)
  11872. {
  11873. struct intel_plane *primary = NULL;
  11874. struct intel_plane_state *state = NULL;
  11875. const uint32_t *intel_primary_formats;
  11876. unsigned int num_formats;
  11877. int ret;
  11878. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11879. if (!primary)
  11880. goto fail;
  11881. state = intel_create_plane_state(&primary->base);
  11882. if (!state)
  11883. goto fail;
  11884. primary->base.state = &state->base;
  11885. primary->can_scale = false;
  11886. primary->max_downscale = 1;
  11887. if (INTEL_INFO(dev)->gen >= 9) {
  11888. primary->can_scale = true;
  11889. state->scaler_id = -1;
  11890. }
  11891. primary->pipe = pipe;
  11892. primary->plane = pipe;
  11893. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11894. primary->check_plane = intel_check_primary_plane;
  11895. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11896. primary->plane = !pipe;
  11897. if (INTEL_INFO(dev)->gen >= 9) {
  11898. intel_primary_formats = skl_primary_formats;
  11899. num_formats = ARRAY_SIZE(skl_primary_formats);
  11900. primary->update_plane = skylake_update_primary_plane;
  11901. primary->disable_plane = skylake_disable_primary_plane;
  11902. } else if (HAS_PCH_SPLIT(dev)) {
  11903. intel_primary_formats = i965_primary_formats;
  11904. num_formats = ARRAY_SIZE(i965_primary_formats);
  11905. primary->update_plane = ironlake_update_primary_plane;
  11906. primary->disable_plane = i9xx_disable_primary_plane;
  11907. } else if (INTEL_INFO(dev)->gen >= 4) {
  11908. intel_primary_formats = i965_primary_formats;
  11909. num_formats = ARRAY_SIZE(i965_primary_formats);
  11910. primary->update_plane = i9xx_update_primary_plane;
  11911. primary->disable_plane = i9xx_disable_primary_plane;
  11912. } else {
  11913. intel_primary_formats = i8xx_primary_formats;
  11914. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11915. primary->update_plane = i9xx_update_primary_plane;
  11916. primary->disable_plane = i9xx_disable_primary_plane;
  11917. }
  11918. if (INTEL_INFO(dev)->gen >= 9)
  11919. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11920. &intel_plane_funcs,
  11921. intel_primary_formats, num_formats,
  11922. DRM_PLANE_TYPE_PRIMARY,
  11923. "plane 1%c", pipe_name(pipe));
  11924. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11925. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11926. &intel_plane_funcs,
  11927. intel_primary_formats, num_formats,
  11928. DRM_PLANE_TYPE_PRIMARY,
  11929. "primary %c", pipe_name(pipe));
  11930. else
  11931. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11932. &intel_plane_funcs,
  11933. intel_primary_formats, num_formats,
  11934. DRM_PLANE_TYPE_PRIMARY,
  11935. "plane %c", plane_name(primary->plane));
  11936. if (ret)
  11937. goto fail;
  11938. if (INTEL_INFO(dev)->gen >= 4)
  11939. intel_create_rotation_property(dev, primary);
  11940. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11941. return &primary->base;
  11942. fail:
  11943. kfree(state);
  11944. kfree(primary);
  11945. return NULL;
  11946. }
  11947. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11948. {
  11949. if (!dev->mode_config.rotation_property) {
  11950. unsigned long flags = BIT(DRM_ROTATE_0) |
  11951. BIT(DRM_ROTATE_180);
  11952. if (INTEL_INFO(dev)->gen >= 9)
  11953. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11954. dev->mode_config.rotation_property =
  11955. drm_mode_create_rotation_property(dev, flags);
  11956. }
  11957. if (dev->mode_config.rotation_property)
  11958. drm_object_attach_property(&plane->base.base,
  11959. dev->mode_config.rotation_property,
  11960. plane->base.state->rotation);
  11961. }
  11962. static int
  11963. intel_check_cursor_plane(struct drm_plane *plane,
  11964. struct intel_crtc_state *crtc_state,
  11965. struct intel_plane_state *state)
  11966. {
  11967. struct drm_crtc *crtc = crtc_state->base.crtc;
  11968. struct drm_framebuffer *fb = state->base.fb;
  11969. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11970. enum pipe pipe = to_intel_plane(plane)->pipe;
  11971. unsigned stride;
  11972. int ret;
  11973. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11974. &state->dst, &state->clip,
  11975. state->base.rotation,
  11976. DRM_PLANE_HELPER_NO_SCALING,
  11977. DRM_PLANE_HELPER_NO_SCALING,
  11978. true, true, &state->visible);
  11979. if (ret)
  11980. return ret;
  11981. /* if we want to turn off the cursor ignore width and height */
  11982. if (!obj)
  11983. return 0;
  11984. /* Check for which cursor types we support */
  11985. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11986. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11987. state->base.crtc_w, state->base.crtc_h);
  11988. return -EINVAL;
  11989. }
  11990. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11991. if (obj->base.size < stride * state->base.crtc_h) {
  11992. DRM_DEBUG_KMS("buffer is too small\n");
  11993. return -ENOMEM;
  11994. }
  11995. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11996. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11997. return -EINVAL;
  11998. }
  11999. /*
  12000. * There's something wrong with the cursor on CHV pipe C.
  12001. * If it straddles the left edge of the screen then
  12002. * moving it away from the edge or disabling it often
  12003. * results in a pipe underrun, and often that can lead to
  12004. * dead pipe (constant underrun reported, and it scans
  12005. * out just a solid color). To recover from that, the
  12006. * display power well must be turned off and on again.
  12007. * Refuse the put the cursor into that compromised position.
  12008. */
  12009. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12010. state->visible && state->base.crtc_x < 0) {
  12011. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12012. return -EINVAL;
  12013. }
  12014. return 0;
  12015. }
  12016. static void
  12017. intel_disable_cursor_plane(struct drm_plane *plane,
  12018. struct drm_crtc *crtc)
  12019. {
  12020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12021. intel_crtc->cursor_addr = 0;
  12022. intel_crtc_update_cursor(crtc, NULL);
  12023. }
  12024. static void
  12025. intel_update_cursor_plane(struct drm_plane *plane,
  12026. const struct intel_crtc_state *crtc_state,
  12027. const struct intel_plane_state *state)
  12028. {
  12029. struct drm_crtc *crtc = crtc_state->base.crtc;
  12030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12031. struct drm_device *dev = plane->dev;
  12032. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12033. uint32_t addr;
  12034. if (!obj)
  12035. addr = 0;
  12036. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12037. addr = i915_gem_obj_ggtt_offset(obj);
  12038. else
  12039. addr = obj->phys_handle->busaddr;
  12040. intel_crtc->cursor_addr = addr;
  12041. intel_crtc_update_cursor(crtc, state);
  12042. }
  12043. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12044. int pipe)
  12045. {
  12046. struct intel_plane *cursor = NULL;
  12047. struct intel_plane_state *state = NULL;
  12048. int ret;
  12049. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12050. if (!cursor)
  12051. goto fail;
  12052. state = intel_create_plane_state(&cursor->base);
  12053. if (!state)
  12054. goto fail;
  12055. cursor->base.state = &state->base;
  12056. cursor->can_scale = false;
  12057. cursor->max_downscale = 1;
  12058. cursor->pipe = pipe;
  12059. cursor->plane = pipe;
  12060. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12061. cursor->check_plane = intel_check_cursor_plane;
  12062. cursor->update_plane = intel_update_cursor_plane;
  12063. cursor->disable_plane = intel_disable_cursor_plane;
  12064. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12065. &intel_plane_funcs,
  12066. intel_cursor_formats,
  12067. ARRAY_SIZE(intel_cursor_formats),
  12068. DRM_PLANE_TYPE_CURSOR,
  12069. "cursor %c", pipe_name(pipe));
  12070. if (ret)
  12071. goto fail;
  12072. if (INTEL_INFO(dev)->gen >= 4) {
  12073. if (!dev->mode_config.rotation_property)
  12074. dev->mode_config.rotation_property =
  12075. drm_mode_create_rotation_property(dev,
  12076. BIT(DRM_ROTATE_0) |
  12077. BIT(DRM_ROTATE_180));
  12078. if (dev->mode_config.rotation_property)
  12079. drm_object_attach_property(&cursor->base.base,
  12080. dev->mode_config.rotation_property,
  12081. state->base.rotation);
  12082. }
  12083. if (INTEL_INFO(dev)->gen >=9)
  12084. state->scaler_id = -1;
  12085. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12086. return &cursor->base;
  12087. fail:
  12088. kfree(state);
  12089. kfree(cursor);
  12090. return NULL;
  12091. }
  12092. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12093. struct intel_crtc_state *crtc_state)
  12094. {
  12095. int i;
  12096. struct intel_scaler *intel_scaler;
  12097. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12098. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12099. intel_scaler = &scaler_state->scalers[i];
  12100. intel_scaler->in_use = 0;
  12101. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12102. }
  12103. scaler_state->scaler_id = -1;
  12104. }
  12105. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12106. {
  12107. struct drm_i915_private *dev_priv = to_i915(dev);
  12108. struct intel_crtc *intel_crtc;
  12109. struct intel_crtc_state *crtc_state = NULL;
  12110. struct drm_plane *primary = NULL;
  12111. struct drm_plane *cursor = NULL;
  12112. int ret;
  12113. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12114. if (intel_crtc == NULL)
  12115. return;
  12116. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12117. if (!crtc_state)
  12118. goto fail;
  12119. intel_crtc->config = crtc_state;
  12120. intel_crtc->base.state = &crtc_state->base;
  12121. crtc_state->base.crtc = &intel_crtc->base;
  12122. /* initialize shared scalers */
  12123. if (INTEL_INFO(dev)->gen >= 9) {
  12124. if (pipe == PIPE_C)
  12125. intel_crtc->num_scalers = 1;
  12126. else
  12127. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12128. skl_init_scalers(dev, intel_crtc, crtc_state);
  12129. }
  12130. primary = intel_primary_plane_create(dev, pipe);
  12131. if (!primary)
  12132. goto fail;
  12133. cursor = intel_cursor_plane_create(dev, pipe);
  12134. if (!cursor)
  12135. goto fail;
  12136. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12137. cursor, &intel_crtc_funcs,
  12138. "pipe %c", pipe_name(pipe));
  12139. if (ret)
  12140. goto fail;
  12141. /*
  12142. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12143. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12144. */
  12145. intel_crtc->pipe = pipe;
  12146. intel_crtc->plane = pipe;
  12147. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12148. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12149. intel_crtc->plane = !pipe;
  12150. }
  12151. intel_crtc->cursor_base = ~0;
  12152. intel_crtc->cursor_cntl = ~0;
  12153. intel_crtc->cursor_size = ~0;
  12154. intel_crtc->wm.cxsr_allowed = true;
  12155. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12156. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12157. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12158. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12159. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12160. intel_color_init(&intel_crtc->base);
  12161. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12162. return;
  12163. fail:
  12164. intel_plane_destroy(primary);
  12165. intel_plane_destroy(cursor);
  12166. kfree(crtc_state);
  12167. kfree(intel_crtc);
  12168. }
  12169. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12170. {
  12171. struct drm_encoder *encoder = connector->base.encoder;
  12172. struct drm_device *dev = connector->base.dev;
  12173. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12174. if (!encoder || WARN_ON(!encoder->crtc))
  12175. return INVALID_PIPE;
  12176. return to_intel_crtc(encoder->crtc)->pipe;
  12177. }
  12178. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12179. struct drm_file *file)
  12180. {
  12181. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12182. struct drm_crtc *drmmode_crtc;
  12183. struct intel_crtc *crtc;
  12184. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12185. if (!drmmode_crtc)
  12186. return -ENOENT;
  12187. crtc = to_intel_crtc(drmmode_crtc);
  12188. pipe_from_crtc_id->pipe = crtc->pipe;
  12189. return 0;
  12190. }
  12191. static int intel_encoder_clones(struct intel_encoder *encoder)
  12192. {
  12193. struct drm_device *dev = encoder->base.dev;
  12194. struct intel_encoder *source_encoder;
  12195. int index_mask = 0;
  12196. int entry = 0;
  12197. for_each_intel_encoder(dev, source_encoder) {
  12198. if (encoders_cloneable(encoder, source_encoder))
  12199. index_mask |= (1 << entry);
  12200. entry++;
  12201. }
  12202. return index_mask;
  12203. }
  12204. static bool has_edp_a(struct drm_device *dev)
  12205. {
  12206. struct drm_i915_private *dev_priv = to_i915(dev);
  12207. if (!IS_MOBILE(dev))
  12208. return false;
  12209. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12210. return false;
  12211. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12212. return false;
  12213. return true;
  12214. }
  12215. static bool intel_crt_present(struct drm_device *dev)
  12216. {
  12217. struct drm_i915_private *dev_priv = to_i915(dev);
  12218. if (INTEL_INFO(dev)->gen >= 9)
  12219. return false;
  12220. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12221. return false;
  12222. if (IS_CHERRYVIEW(dev))
  12223. return false;
  12224. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12225. return false;
  12226. /* DDI E can't be used if DDI A requires 4 lanes */
  12227. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12228. return false;
  12229. if (!dev_priv->vbt.int_crt_support)
  12230. return false;
  12231. return true;
  12232. }
  12233. static void intel_setup_outputs(struct drm_device *dev)
  12234. {
  12235. struct drm_i915_private *dev_priv = to_i915(dev);
  12236. struct intel_encoder *encoder;
  12237. bool dpd_is_edp = false;
  12238. /*
  12239. * intel_edp_init_connector() depends on this completing first, to
  12240. * prevent the registeration of both eDP and LVDS and the incorrect
  12241. * sharing of the PPS.
  12242. */
  12243. intel_lvds_init(dev);
  12244. if (intel_crt_present(dev))
  12245. intel_crt_init(dev);
  12246. if (IS_BROXTON(dev)) {
  12247. /*
  12248. * FIXME: Broxton doesn't support port detection via the
  12249. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12250. * detect the ports.
  12251. */
  12252. intel_ddi_init(dev, PORT_A);
  12253. intel_ddi_init(dev, PORT_B);
  12254. intel_ddi_init(dev, PORT_C);
  12255. intel_dsi_init(dev);
  12256. } else if (HAS_DDI(dev)) {
  12257. int found;
  12258. /*
  12259. * Haswell uses DDI functions to detect digital outputs.
  12260. * On SKL pre-D0 the strap isn't connected, so we assume
  12261. * it's there.
  12262. */
  12263. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12264. /* WaIgnoreDDIAStrap: skl */
  12265. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12266. intel_ddi_init(dev, PORT_A);
  12267. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12268. * register */
  12269. found = I915_READ(SFUSE_STRAP);
  12270. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12271. intel_ddi_init(dev, PORT_B);
  12272. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12273. intel_ddi_init(dev, PORT_C);
  12274. if (found & SFUSE_STRAP_DDID_DETECTED)
  12275. intel_ddi_init(dev, PORT_D);
  12276. /*
  12277. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12278. */
  12279. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12280. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12281. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12282. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12283. intel_ddi_init(dev, PORT_E);
  12284. } else if (HAS_PCH_SPLIT(dev)) {
  12285. int found;
  12286. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12287. if (has_edp_a(dev))
  12288. intel_dp_init(dev, DP_A, PORT_A);
  12289. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12290. /* PCH SDVOB multiplex with HDMIB */
  12291. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12292. if (!found)
  12293. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12294. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12295. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12296. }
  12297. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12298. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12299. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12300. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12301. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12302. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12303. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12304. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12305. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12306. bool has_edp, has_port;
  12307. /*
  12308. * The DP_DETECTED bit is the latched state of the DDC
  12309. * SDA pin at boot. However since eDP doesn't require DDC
  12310. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12311. * eDP ports may have been muxed to an alternate function.
  12312. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12313. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12314. * detect eDP ports.
  12315. *
  12316. * Sadly the straps seem to be missing sometimes even for HDMI
  12317. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12318. * and VBT for the presence of the port. Additionally we can't
  12319. * trust the port type the VBT declares as we've seen at least
  12320. * HDMI ports that the VBT claim are DP or eDP.
  12321. */
  12322. has_edp = intel_dp_is_edp(dev, PORT_B);
  12323. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12324. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12325. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12326. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12327. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12328. has_edp = intel_dp_is_edp(dev, PORT_C);
  12329. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12330. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12331. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12332. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12333. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12334. if (IS_CHERRYVIEW(dev)) {
  12335. /*
  12336. * eDP not supported on port D,
  12337. * so no need to worry about it
  12338. */
  12339. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12340. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12341. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12342. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12343. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12344. }
  12345. intel_dsi_init(dev);
  12346. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12347. bool found = false;
  12348. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12349. DRM_DEBUG_KMS("probing SDVOB\n");
  12350. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12351. if (!found && IS_G4X(dev)) {
  12352. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12353. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12354. }
  12355. if (!found && IS_G4X(dev))
  12356. intel_dp_init(dev, DP_B, PORT_B);
  12357. }
  12358. /* Before G4X SDVOC doesn't have its own detect register */
  12359. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12360. DRM_DEBUG_KMS("probing SDVOC\n");
  12361. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12362. }
  12363. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12364. if (IS_G4X(dev)) {
  12365. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12366. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12367. }
  12368. if (IS_G4X(dev))
  12369. intel_dp_init(dev, DP_C, PORT_C);
  12370. }
  12371. if (IS_G4X(dev) &&
  12372. (I915_READ(DP_D) & DP_DETECTED))
  12373. intel_dp_init(dev, DP_D, PORT_D);
  12374. } else if (IS_GEN2(dev))
  12375. intel_dvo_init(dev);
  12376. if (SUPPORTS_TV(dev))
  12377. intel_tv_init(dev);
  12378. intel_psr_init(dev);
  12379. for_each_intel_encoder(dev, encoder) {
  12380. encoder->base.possible_crtcs = encoder->crtc_mask;
  12381. encoder->base.possible_clones =
  12382. intel_encoder_clones(encoder);
  12383. }
  12384. intel_init_pch_refclk(dev);
  12385. drm_helper_move_panel_connectors_to_head(dev);
  12386. }
  12387. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12388. {
  12389. struct drm_device *dev = fb->dev;
  12390. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12391. drm_framebuffer_cleanup(fb);
  12392. mutex_lock(&dev->struct_mutex);
  12393. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12394. drm_gem_object_unreference(&intel_fb->obj->base);
  12395. mutex_unlock(&dev->struct_mutex);
  12396. kfree(intel_fb);
  12397. }
  12398. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12399. struct drm_file *file,
  12400. unsigned int *handle)
  12401. {
  12402. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12403. struct drm_i915_gem_object *obj = intel_fb->obj;
  12404. if (obj->userptr.mm) {
  12405. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12406. return -EINVAL;
  12407. }
  12408. return drm_gem_handle_create(file, &obj->base, handle);
  12409. }
  12410. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12411. struct drm_file *file,
  12412. unsigned flags, unsigned color,
  12413. struct drm_clip_rect *clips,
  12414. unsigned num_clips)
  12415. {
  12416. struct drm_device *dev = fb->dev;
  12417. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12418. struct drm_i915_gem_object *obj = intel_fb->obj;
  12419. mutex_lock(&dev->struct_mutex);
  12420. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12421. mutex_unlock(&dev->struct_mutex);
  12422. return 0;
  12423. }
  12424. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12425. .destroy = intel_user_framebuffer_destroy,
  12426. .create_handle = intel_user_framebuffer_create_handle,
  12427. .dirty = intel_user_framebuffer_dirty,
  12428. };
  12429. static
  12430. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12431. uint32_t pixel_format)
  12432. {
  12433. u32 gen = INTEL_INFO(dev)->gen;
  12434. if (gen >= 9) {
  12435. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12436. /* "The stride in bytes must not exceed the of the size of 8K
  12437. * pixels and 32K bytes."
  12438. */
  12439. return min(8192 * cpp, 32768);
  12440. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12441. return 32*1024;
  12442. } else if (gen >= 4) {
  12443. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12444. return 16*1024;
  12445. else
  12446. return 32*1024;
  12447. } else if (gen >= 3) {
  12448. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12449. return 8*1024;
  12450. else
  12451. return 16*1024;
  12452. } else {
  12453. /* XXX DSPC is limited to 4k tiled */
  12454. return 8*1024;
  12455. }
  12456. }
  12457. static int intel_framebuffer_init(struct drm_device *dev,
  12458. struct intel_framebuffer *intel_fb,
  12459. struct drm_mode_fb_cmd2 *mode_cmd,
  12460. struct drm_i915_gem_object *obj)
  12461. {
  12462. struct drm_i915_private *dev_priv = to_i915(dev);
  12463. unsigned int aligned_height;
  12464. int ret;
  12465. u32 pitch_limit, stride_alignment;
  12466. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12467. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12468. /* Enforce that fb modifier and tiling mode match, but only for
  12469. * X-tiled. This is needed for FBC. */
  12470. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12471. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12472. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12473. return -EINVAL;
  12474. }
  12475. } else {
  12476. if (obj->tiling_mode == I915_TILING_X)
  12477. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12478. else if (obj->tiling_mode == I915_TILING_Y) {
  12479. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12480. return -EINVAL;
  12481. }
  12482. }
  12483. /* Passed in modifier sanity checking. */
  12484. switch (mode_cmd->modifier[0]) {
  12485. case I915_FORMAT_MOD_Y_TILED:
  12486. case I915_FORMAT_MOD_Yf_TILED:
  12487. if (INTEL_INFO(dev)->gen < 9) {
  12488. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12489. mode_cmd->modifier[0]);
  12490. return -EINVAL;
  12491. }
  12492. case DRM_FORMAT_MOD_NONE:
  12493. case I915_FORMAT_MOD_X_TILED:
  12494. break;
  12495. default:
  12496. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12497. mode_cmd->modifier[0]);
  12498. return -EINVAL;
  12499. }
  12500. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12501. mode_cmd->modifier[0],
  12502. mode_cmd->pixel_format);
  12503. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12504. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12505. mode_cmd->pitches[0], stride_alignment);
  12506. return -EINVAL;
  12507. }
  12508. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12509. mode_cmd->pixel_format);
  12510. if (mode_cmd->pitches[0] > pitch_limit) {
  12511. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12512. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12513. "tiled" : "linear",
  12514. mode_cmd->pitches[0], pitch_limit);
  12515. return -EINVAL;
  12516. }
  12517. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12518. mode_cmd->pitches[0] != obj->stride) {
  12519. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12520. mode_cmd->pitches[0], obj->stride);
  12521. return -EINVAL;
  12522. }
  12523. /* Reject formats not supported by any plane early. */
  12524. switch (mode_cmd->pixel_format) {
  12525. case DRM_FORMAT_C8:
  12526. case DRM_FORMAT_RGB565:
  12527. case DRM_FORMAT_XRGB8888:
  12528. case DRM_FORMAT_ARGB8888:
  12529. break;
  12530. case DRM_FORMAT_XRGB1555:
  12531. if (INTEL_INFO(dev)->gen > 3) {
  12532. DRM_DEBUG("unsupported pixel format: %s\n",
  12533. drm_get_format_name(mode_cmd->pixel_format));
  12534. return -EINVAL;
  12535. }
  12536. break;
  12537. case DRM_FORMAT_ABGR8888:
  12538. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12539. INTEL_INFO(dev)->gen < 9) {
  12540. DRM_DEBUG("unsupported pixel format: %s\n",
  12541. drm_get_format_name(mode_cmd->pixel_format));
  12542. return -EINVAL;
  12543. }
  12544. break;
  12545. case DRM_FORMAT_XBGR8888:
  12546. case DRM_FORMAT_XRGB2101010:
  12547. case DRM_FORMAT_XBGR2101010:
  12548. if (INTEL_INFO(dev)->gen < 4) {
  12549. DRM_DEBUG("unsupported pixel format: %s\n",
  12550. drm_get_format_name(mode_cmd->pixel_format));
  12551. return -EINVAL;
  12552. }
  12553. break;
  12554. case DRM_FORMAT_ABGR2101010:
  12555. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12556. DRM_DEBUG("unsupported pixel format: %s\n",
  12557. drm_get_format_name(mode_cmd->pixel_format));
  12558. return -EINVAL;
  12559. }
  12560. break;
  12561. case DRM_FORMAT_YUYV:
  12562. case DRM_FORMAT_UYVY:
  12563. case DRM_FORMAT_YVYU:
  12564. case DRM_FORMAT_VYUY:
  12565. if (INTEL_INFO(dev)->gen < 5) {
  12566. DRM_DEBUG("unsupported pixel format: %s\n",
  12567. drm_get_format_name(mode_cmd->pixel_format));
  12568. return -EINVAL;
  12569. }
  12570. break;
  12571. default:
  12572. DRM_DEBUG("unsupported pixel format: %s\n",
  12573. drm_get_format_name(mode_cmd->pixel_format));
  12574. return -EINVAL;
  12575. }
  12576. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12577. if (mode_cmd->offsets[0] != 0)
  12578. return -EINVAL;
  12579. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12580. mode_cmd->pixel_format,
  12581. mode_cmd->modifier[0]);
  12582. /* FIXME drm helper for size checks (especially planar formats)? */
  12583. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12584. return -EINVAL;
  12585. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12586. intel_fb->obj = obj;
  12587. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12588. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12589. if (ret) {
  12590. DRM_ERROR("framebuffer init failed %d\n", ret);
  12591. return ret;
  12592. }
  12593. intel_fb->obj->framebuffer_references++;
  12594. return 0;
  12595. }
  12596. static struct drm_framebuffer *
  12597. intel_user_framebuffer_create(struct drm_device *dev,
  12598. struct drm_file *filp,
  12599. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12600. {
  12601. struct drm_framebuffer *fb;
  12602. struct drm_i915_gem_object *obj;
  12603. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12604. obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
  12605. if (&obj->base == NULL)
  12606. return ERR_PTR(-ENOENT);
  12607. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12608. if (IS_ERR(fb))
  12609. drm_gem_object_unreference_unlocked(&obj->base);
  12610. return fb;
  12611. }
  12612. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12613. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12614. {
  12615. }
  12616. #endif
  12617. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12618. .fb_create = intel_user_framebuffer_create,
  12619. .output_poll_changed = intel_fbdev_output_poll_changed,
  12620. .atomic_check = intel_atomic_check,
  12621. .atomic_commit = intel_atomic_commit,
  12622. .atomic_state_alloc = intel_atomic_state_alloc,
  12623. .atomic_state_clear = intel_atomic_state_clear,
  12624. };
  12625. /**
  12626. * intel_init_display_hooks - initialize the display modesetting hooks
  12627. * @dev_priv: device private
  12628. */
  12629. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12630. {
  12631. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12632. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12633. dev_priv->display.get_initial_plane_config =
  12634. skylake_get_initial_plane_config;
  12635. dev_priv->display.crtc_compute_clock =
  12636. haswell_crtc_compute_clock;
  12637. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12638. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12639. } else if (HAS_DDI(dev_priv)) {
  12640. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12641. dev_priv->display.get_initial_plane_config =
  12642. ironlake_get_initial_plane_config;
  12643. dev_priv->display.crtc_compute_clock =
  12644. haswell_crtc_compute_clock;
  12645. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12646. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12647. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12648. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12649. dev_priv->display.get_initial_plane_config =
  12650. ironlake_get_initial_plane_config;
  12651. dev_priv->display.crtc_compute_clock =
  12652. ironlake_crtc_compute_clock;
  12653. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12654. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12655. } else if (IS_CHERRYVIEW(dev_priv)) {
  12656. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12657. dev_priv->display.get_initial_plane_config =
  12658. i9xx_get_initial_plane_config;
  12659. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12660. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12661. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12662. } else if (IS_VALLEYVIEW(dev_priv)) {
  12663. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12664. dev_priv->display.get_initial_plane_config =
  12665. i9xx_get_initial_plane_config;
  12666. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12667. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12668. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12669. } else if (IS_G4X(dev_priv)) {
  12670. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12671. dev_priv->display.get_initial_plane_config =
  12672. i9xx_get_initial_plane_config;
  12673. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12674. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12675. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12676. } else if (IS_PINEVIEW(dev_priv)) {
  12677. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12678. dev_priv->display.get_initial_plane_config =
  12679. i9xx_get_initial_plane_config;
  12680. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12681. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12682. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12683. } else if (!IS_GEN2(dev_priv)) {
  12684. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12685. dev_priv->display.get_initial_plane_config =
  12686. i9xx_get_initial_plane_config;
  12687. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12688. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12689. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12690. } else {
  12691. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12692. dev_priv->display.get_initial_plane_config =
  12693. i9xx_get_initial_plane_config;
  12694. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12695. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12696. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12697. }
  12698. /* Returns the core display clock speed */
  12699. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12700. dev_priv->display.get_display_clock_speed =
  12701. skylake_get_display_clock_speed;
  12702. else if (IS_BROXTON(dev_priv))
  12703. dev_priv->display.get_display_clock_speed =
  12704. broxton_get_display_clock_speed;
  12705. else if (IS_BROADWELL(dev_priv))
  12706. dev_priv->display.get_display_clock_speed =
  12707. broadwell_get_display_clock_speed;
  12708. else if (IS_HASWELL(dev_priv))
  12709. dev_priv->display.get_display_clock_speed =
  12710. haswell_get_display_clock_speed;
  12711. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12712. dev_priv->display.get_display_clock_speed =
  12713. valleyview_get_display_clock_speed;
  12714. else if (IS_GEN5(dev_priv))
  12715. dev_priv->display.get_display_clock_speed =
  12716. ilk_get_display_clock_speed;
  12717. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12718. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12719. dev_priv->display.get_display_clock_speed =
  12720. i945_get_display_clock_speed;
  12721. else if (IS_GM45(dev_priv))
  12722. dev_priv->display.get_display_clock_speed =
  12723. gm45_get_display_clock_speed;
  12724. else if (IS_CRESTLINE(dev_priv))
  12725. dev_priv->display.get_display_clock_speed =
  12726. i965gm_get_display_clock_speed;
  12727. else if (IS_PINEVIEW(dev_priv))
  12728. dev_priv->display.get_display_clock_speed =
  12729. pnv_get_display_clock_speed;
  12730. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12731. dev_priv->display.get_display_clock_speed =
  12732. g33_get_display_clock_speed;
  12733. else if (IS_I915G(dev_priv))
  12734. dev_priv->display.get_display_clock_speed =
  12735. i915_get_display_clock_speed;
  12736. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12737. dev_priv->display.get_display_clock_speed =
  12738. i9xx_misc_get_display_clock_speed;
  12739. else if (IS_I915GM(dev_priv))
  12740. dev_priv->display.get_display_clock_speed =
  12741. i915gm_get_display_clock_speed;
  12742. else if (IS_I865G(dev_priv))
  12743. dev_priv->display.get_display_clock_speed =
  12744. i865_get_display_clock_speed;
  12745. else if (IS_I85X(dev_priv))
  12746. dev_priv->display.get_display_clock_speed =
  12747. i85x_get_display_clock_speed;
  12748. else { /* 830 */
  12749. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12750. dev_priv->display.get_display_clock_speed =
  12751. i830_get_display_clock_speed;
  12752. }
  12753. if (IS_GEN5(dev_priv)) {
  12754. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12755. } else if (IS_GEN6(dev_priv)) {
  12756. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12757. } else if (IS_IVYBRIDGE(dev_priv)) {
  12758. /* FIXME: detect B0+ stepping and use auto training */
  12759. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12760. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12761. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12762. }
  12763. if (IS_BROADWELL(dev_priv)) {
  12764. dev_priv->display.modeset_commit_cdclk =
  12765. broadwell_modeset_commit_cdclk;
  12766. dev_priv->display.modeset_calc_cdclk =
  12767. broadwell_modeset_calc_cdclk;
  12768. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12769. dev_priv->display.modeset_commit_cdclk =
  12770. valleyview_modeset_commit_cdclk;
  12771. dev_priv->display.modeset_calc_cdclk =
  12772. valleyview_modeset_calc_cdclk;
  12773. } else if (IS_BROXTON(dev_priv)) {
  12774. dev_priv->display.modeset_commit_cdclk =
  12775. bxt_modeset_commit_cdclk;
  12776. dev_priv->display.modeset_calc_cdclk =
  12777. bxt_modeset_calc_cdclk;
  12778. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12779. dev_priv->display.modeset_commit_cdclk =
  12780. skl_modeset_commit_cdclk;
  12781. dev_priv->display.modeset_calc_cdclk =
  12782. skl_modeset_calc_cdclk;
  12783. }
  12784. switch (INTEL_INFO(dev_priv)->gen) {
  12785. case 2:
  12786. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12787. break;
  12788. case 3:
  12789. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12790. break;
  12791. case 4:
  12792. case 5:
  12793. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12794. break;
  12795. case 6:
  12796. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12797. break;
  12798. case 7:
  12799. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12800. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12801. break;
  12802. case 9:
  12803. /* Drop through - unsupported since execlist only. */
  12804. default:
  12805. /* Default just returns -ENODEV to indicate unsupported */
  12806. dev_priv->display.queue_flip = intel_default_queue_flip;
  12807. }
  12808. }
  12809. /*
  12810. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12811. * resume, or other times. This quirk makes sure that's the case for
  12812. * affected systems.
  12813. */
  12814. static void quirk_pipea_force(struct drm_device *dev)
  12815. {
  12816. struct drm_i915_private *dev_priv = to_i915(dev);
  12817. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12818. DRM_INFO("applying pipe a force quirk\n");
  12819. }
  12820. static void quirk_pipeb_force(struct drm_device *dev)
  12821. {
  12822. struct drm_i915_private *dev_priv = to_i915(dev);
  12823. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12824. DRM_INFO("applying pipe b force quirk\n");
  12825. }
  12826. /*
  12827. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12828. */
  12829. static void quirk_ssc_force_disable(struct drm_device *dev)
  12830. {
  12831. struct drm_i915_private *dev_priv = to_i915(dev);
  12832. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12833. DRM_INFO("applying lvds SSC disable quirk\n");
  12834. }
  12835. /*
  12836. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12837. * brightness value
  12838. */
  12839. static void quirk_invert_brightness(struct drm_device *dev)
  12840. {
  12841. struct drm_i915_private *dev_priv = to_i915(dev);
  12842. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12843. DRM_INFO("applying inverted panel brightness quirk\n");
  12844. }
  12845. /* Some VBT's incorrectly indicate no backlight is present */
  12846. static void quirk_backlight_present(struct drm_device *dev)
  12847. {
  12848. struct drm_i915_private *dev_priv = to_i915(dev);
  12849. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12850. DRM_INFO("applying backlight present quirk\n");
  12851. }
  12852. struct intel_quirk {
  12853. int device;
  12854. int subsystem_vendor;
  12855. int subsystem_device;
  12856. void (*hook)(struct drm_device *dev);
  12857. };
  12858. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12859. struct intel_dmi_quirk {
  12860. void (*hook)(struct drm_device *dev);
  12861. const struct dmi_system_id (*dmi_id_list)[];
  12862. };
  12863. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12864. {
  12865. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12866. return 1;
  12867. }
  12868. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12869. {
  12870. .dmi_id_list = &(const struct dmi_system_id[]) {
  12871. {
  12872. .callback = intel_dmi_reverse_brightness,
  12873. .ident = "NCR Corporation",
  12874. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12875. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12876. },
  12877. },
  12878. { } /* terminating entry */
  12879. },
  12880. .hook = quirk_invert_brightness,
  12881. },
  12882. };
  12883. static struct intel_quirk intel_quirks[] = {
  12884. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12885. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12886. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12887. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12888. /* 830 needs to leave pipe A & dpll A up */
  12889. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12890. /* 830 needs to leave pipe B & dpll B up */
  12891. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12892. /* Lenovo U160 cannot use SSC on LVDS */
  12893. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12894. /* Sony Vaio Y cannot use SSC on LVDS */
  12895. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12896. /* Acer Aspire 5734Z must invert backlight brightness */
  12897. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12898. /* Acer/eMachines G725 */
  12899. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12900. /* Acer/eMachines e725 */
  12901. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12902. /* Acer/Packard Bell NCL20 */
  12903. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12904. /* Acer Aspire 4736Z */
  12905. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12906. /* Acer Aspire 5336 */
  12907. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12908. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12909. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12910. /* Acer C720 Chromebook (Core i3 4005U) */
  12911. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12912. /* Apple Macbook 2,1 (Core 2 T7400) */
  12913. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12914. /* Apple Macbook 4,1 */
  12915. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12916. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12917. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12918. /* HP Chromebook 14 (Celeron 2955U) */
  12919. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12920. /* Dell Chromebook 11 */
  12921. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12922. /* Dell Chromebook 11 (2015 version) */
  12923. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12924. };
  12925. static void intel_init_quirks(struct drm_device *dev)
  12926. {
  12927. struct pci_dev *d = dev->pdev;
  12928. int i;
  12929. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12930. struct intel_quirk *q = &intel_quirks[i];
  12931. if (d->device == q->device &&
  12932. (d->subsystem_vendor == q->subsystem_vendor ||
  12933. q->subsystem_vendor == PCI_ANY_ID) &&
  12934. (d->subsystem_device == q->subsystem_device ||
  12935. q->subsystem_device == PCI_ANY_ID))
  12936. q->hook(dev);
  12937. }
  12938. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12939. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12940. intel_dmi_quirks[i].hook(dev);
  12941. }
  12942. }
  12943. /* Disable the VGA plane that we never use */
  12944. static void i915_disable_vga(struct drm_device *dev)
  12945. {
  12946. struct drm_i915_private *dev_priv = to_i915(dev);
  12947. u8 sr1;
  12948. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12949. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12950. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12951. outb(SR01, VGA_SR_INDEX);
  12952. sr1 = inb(VGA_SR_DATA);
  12953. outb(sr1 | 1<<5, VGA_SR_DATA);
  12954. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12955. udelay(300);
  12956. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12957. POSTING_READ(vga_reg);
  12958. }
  12959. void intel_modeset_init_hw(struct drm_device *dev)
  12960. {
  12961. struct drm_i915_private *dev_priv = to_i915(dev);
  12962. intel_update_cdclk(dev);
  12963. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12964. intel_init_clock_gating(dev);
  12965. intel_enable_gt_powersave(dev_priv);
  12966. }
  12967. /*
  12968. * Calculate what we think the watermarks should be for the state we've read
  12969. * out of the hardware and then immediately program those watermarks so that
  12970. * we ensure the hardware settings match our internal state.
  12971. *
  12972. * We can calculate what we think WM's should be by creating a duplicate of the
  12973. * current state (which was constructed during hardware readout) and running it
  12974. * through the atomic check code to calculate new watermark values in the
  12975. * state object.
  12976. */
  12977. static void sanitize_watermarks(struct drm_device *dev)
  12978. {
  12979. struct drm_i915_private *dev_priv = to_i915(dev);
  12980. struct drm_atomic_state *state;
  12981. struct drm_crtc *crtc;
  12982. struct drm_crtc_state *cstate;
  12983. struct drm_modeset_acquire_ctx ctx;
  12984. int ret;
  12985. int i;
  12986. /* Only supported on platforms that use atomic watermark design */
  12987. if (!dev_priv->display.optimize_watermarks)
  12988. return;
  12989. /*
  12990. * We need to hold connection_mutex before calling duplicate_state so
  12991. * that the connector loop is protected.
  12992. */
  12993. drm_modeset_acquire_init(&ctx, 0);
  12994. retry:
  12995. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12996. if (ret == -EDEADLK) {
  12997. drm_modeset_backoff(&ctx);
  12998. goto retry;
  12999. } else if (WARN_ON(ret)) {
  13000. goto fail;
  13001. }
  13002. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13003. if (WARN_ON(IS_ERR(state)))
  13004. goto fail;
  13005. /*
  13006. * Hardware readout is the only time we don't want to calculate
  13007. * intermediate watermarks (since we don't trust the current
  13008. * watermarks).
  13009. */
  13010. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13011. ret = intel_atomic_check(dev, state);
  13012. if (ret) {
  13013. /*
  13014. * If we fail here, it means that the hardware appears to be
  13015. * programmed in a way that shouldn't be possible, given our
  13016. * understanding of watermark requirements. This might mean a
  13017. * mistake in the hardware readout code or a mistake in the
  13018. * watermark calculations for a given platform. Raise a WARN
  13019. * so that this is noticeable.
  13020. *
  13021. * If this actually happens, we'll have to just leave the
  13022. * BIOS-programmed watermarks untouched and hope for the best.
  13023. */
  13024. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13025. goto fail;
  13026. }
  13027. /* Write calculated watermark values back */
  13028. for_each_crtc_in_state(state, crtc, cstate, i) {
  13029. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13030. cs->wm.need_postvbl_update = true;
  13031. dev_priv->display.optimize_watermarks(cs);
  13032. }
  13033. drm_atomic_state_free(state);
  13034. fail:
  13035. drm_modeset_drop_locks(&ctx);
  13036. drm_modeset_acquire_fini(&ctx);
  13037. }
  13038. void intel_modeset_init(struct drm_device *dev)
  13039. {
  13040. struct drm_i915_private *dev_priv = to_i915(dev);
  13041. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13042. int sprite, ret;
  13043. enum pipe pipe;
  13044. struct intel_crtc *crtc;
  13045. drm_mode_config_init(dev);
  13046. dev->mode_config.min_width = 0;
  13047. dev->mode_config.min_height = 0;
  13048. dev->mode_config.preferred_depth = 24;
  13049. dev->mode_config.prefer_shadow = 1;
  13050. dev->mode_config.allow_fb_modifiers = true;
  13051. dev->mode_config.funcs = &intel_mode_funcs;
  13052. intel_init_quirks(dev);
  13053. intel_init_pm(dev);
  13054. if (INTEL_INFO(dev)->num_pipes == 0)
  13055. return;
  13056. /*
  13057. * There may be no VBT; and if the BIOS enabled SSC we can
  13058. * just keep using it to avoid unnecessary flicker. Whereas if the
  13059. * BIOS isn't using it, don't assume it will work even if the VBT
  13060. * indicates as much.
  13061. */
  13062. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13063. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13064. DREF_SSC1_ENABLE);
  13065. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13066. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13067. bios_lvds_use_ssc ? "en" : "dis",
  13068. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13069. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13070. }
  13071. }
  13072. if (IS_GEN2(dev)) {
  13073. dev->mode_config.max_width = 2048;
  13074. dev->mode_config.max_height = 2048;
  13075. } else if (IS_GEN3(dev)) {
  13076. dev->mode_config.max_width = 4096;
  13077. dev->mode_config.max_height = 4096;
  13078. } else {
  13079. dev->mode_config.max_width = 8192;
  13080. dev->mode_config.max_height = 8192;
  13081. }
  13082. if (IS_845G(dev) || IS_I865G(dev)) {
  13083. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13084. dev->mode_config.cursor_height = 1023;
  13085. } else if (IS_GEN2(dev)) {
  13086. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13087. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13088. } else {
  13089. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13090. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13091. }
  13092. dev->mode_config.fb_base = ggtt->mappable_base;
  13093. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13094. INTEL_INFO(dev)->num_pipes,
  13095. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13096. for_each_pipe(dev_priv, pipe) {
  13097. intel_crtc_init(dev, pipe);
  13098. for_each_sprite(dev_priv, pipe, sprite) {
  13099. ret = intel_plane_init(dev, pipe, sprite);
  13100. if (ret)
  13101. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13102. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13103. }
  13104. }
  13105. intel_update_czclk(dev_priv);
  13106. intel_update_cdclk(dev);
  13107. intel_shared_dpll_init(dev);
  13108. if (dev_priv->max_cdclk_freq == 0)
  13109. intel_update_max_cdclk(dev);
  13110. /* Just disable it once at startup */
  13111. i915_disable_vga(dev);
  13112. intel_setup_outputs(dev);
  13113. drm_modeset_lock_all(dev);
  13114. intel_modeset_setup_hw_state(dev);
  13115. drm_modeset_unlock_all(dev);
  13116. for_each_intel_crtc(dev, crtc) {
  13117. struct intel_initial_plane_config plane_config = {};
  13118. if (!crtc->active)
  13119. continue;
  13120. /*
  13121. * Note that reserving the BIOS fb up front prevents us
  13122. * from stuffing other stolen allocations like the ring
  13123. * on top. This prevents some ugliness at boot time, and
  13124. * can even allow for smooth boot transitions if the BIOS
  13125. * fb is large enough for the active pipe configuration.
  13126. */
  13127. dev_priv->display.get_initial_plane_config(crtc,
  13128. &plane_config);
  13129. /*
  13130. * If the fb is shared between multiple heads, we'll
  13131. * just get the first one.
  13132. */
  13133. intel_find_initial_plane_obj(crtc, &plane_config);
  13134. }
  13135. /*
  13136. * Make sure hardware watermarks really match the state we read out.
  13137. * Note that we need to do this after reconstructing the BIOS fb's
  13138. * since the watermark calculation done here will use pstate->fb.
  13139. */
  13140. sanitize_watermarks(dev);
  13141. }
  13142. static void intel_enable_pipe_a(struct drm_device *dev)
  13143. {
  13144. struct intel_connector *connector;
  13145. struct drm_connector *crt = NULL;
  13146. struct intel_load_detect_pipe load_detect_temp;
  13147. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13148. /* We can't just switch on the pipe A, we need to set things up with a
  13149. * proper mode and output configuration. As a gross hack, enable pipe A
  13150. * by enabling the load detect pipe once. */
  13151. for_each_intel_connector(dev, connector) {
  13152. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13153. crt = &connector->base;
  13154. break;
  13155. }
  13156. }
  13157. if (!crt)
  13158. return;
  13159. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13160. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13161. }
  13162. static bool
  13163. intel_check_plane_mapping(struct intel_crtc *crtc)
  13164. {
  13165. struct drm_device *dev = crtc->base.dev;
  13166. struct drm_i915_private *dev_priv = to_i915(dev);
  13167. u32 val;
  13168. if (INTEL_INFO(dev)->num_pipes == 1)
  13169. return true;
  13170. val = I915_READ(DSPCNTR(!crtc->plane));
  13171. if ((val & DISPLAY_PLANE_ENABLE) &&
  13172. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13173. return false;
  13174. return true;
  13175. }
  13176. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13177. {
  13178. struct drm_device *dev = crtc->base.dev;
  13179. struct intel_encoder *encoder;
  13180. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13181. return true;
  13182. return false;
  13183. }
  13184. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13185. {
  13186. struct drm_device *dev = encoder->base.dev;
  13187. struct intel_connector *connector;
  13188. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13189. return true;
  13190. return false;
  13191. }
  13192. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13193. {
  13194. struct drm_device *dev = crtc->base.dev;
  13195. struct drm_i915_private *dev_priv = to_i915(dev);
  13196. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13197. /* Clear any frame start delays used for debugging left by the BIOS */
  13198. if (!transcoder_is_dsi(cpu_transcoder)) {
  13199. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13200. I915_WRITE(reg,
  13201. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13202. }
  13203. /* restore vblank interrupts to correct state */
  13204. drm_crtc_vblank_reset(&crtc->base);
  13205. if (crtc->active) {
  13206. struct intel_plane *plane;
  13207. drm_crtc_vblank_on(&crtc->base);
  13208. /* Disable everything but the primary plane */
  13209. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13210. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13211. continue;
  13212. plane->disable_plane(&plane->base, &crtc->base);
  13213. }
  13214. }
  13215. /* We need to sanitize the plane -> pipe mapping first because this will
  13216. * disable the crtc (and hence change the state) if it is wrong. Note
  13217. * that gen4+ has a fixed plane -> pipe mapping. */
  13218. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13219. bool plane;
  13220. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13221. crtc->base.base.id, crtc->base.name);
  13222. /* Pipe has the wrong plane attached and the plane is active.
  13223. * Temporarily change the plane mapping and disable everything
  13224. * ... */
  13225. plane = crtc->plane;
  13226. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13227. crtc->plane = !plane;
  13228. intel_crtc_disable_noatomic(&crtc->base);
  13229. crtc->plane = plane;
  13230. }
  13231. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13232. crtc->pipe == PIPE_A && !crtc->active) {
  13233. /* BIOS forgot to enable pipe A, this mostly happens after
  13234. * resume. Force-enable the pipe to fix this, the update_dpms
  13235. * call below we restore the pipe to the right state, but leave
  13236. * the required bits on. */
  13237. intel_enable_pipe_a(dev);
  13238. }
  13239. /* Adjust the state of the output pipe according to whether we
  13240. * have active connectors/encoders. */
  13241. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13242. intel_crtc_disable_noatomic(&crtc->base);
  13243. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13244. /*
  13245. * We start out with underrun reporting disabled to avoid races.
  13246. * For correct bookkeeping mark this on active crtcs.
  13247. *
  13248. * Also on gmch platforms we dont have any hardware bits to
  13249. * disable the underrun reporting. Which means we need to start
  13250. * out with underrun reporting disabled also on inactive pipes,
  13251. * since otherwise we'll complain about the garbage we read when
  13252. * e.g. coming up after runtime pm.
  13253. *
  13254. * No protection against concurrent access is required - at
  13255. * worst a fifo underrun happens which also sets this to false.
  13256. */
  13257. crtc->cpu_fifo_underrun_disabled = true;
  13258. crtc->pch_fifo_underrun_disabled = true;
  13259. }
  13260. }
  13261. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13262. {
  13263. struct intel_connector *connector;
  13264. struct drm_device *dev = encoder->base.dev;
  13265. /* We need to check both for a crtc link (meaning that the
  13266. * encoder is active and trying to read from a pipe) and the
  13267. * pipe itself being active. */
  13268. bool has_active_crtc = encoder->base.crtc &&
  13269. to_intel_crtc(encoder->base.crtc)->active;
  13270. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13271. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13272. encoder->base.base.id,
  13273. encoder->base.name);
  13274. /* Connector is active, but has no active pipe. This is
  13275. * fallout from our resume register restoring. Disable
  13276. * the encoder manually again. */
  13277. if (encoder->base.crtc) {
  13278. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13279. encoder->base.base.id,
  13280. encoder->base.name);
  13281. encoder->disable(encoder);
  13282. if (encoder->post_disable)
  13283. encoder->post_disable(encoder);
  13284. }
  13285. encoder->base.crtc = NULL;
  13286. /* Inconsistent output/port/pipe state happens presumably due to
  13287. * a bug in one of the get_hw_state functions. Or someplace else
  13288. * in our code, like the register restore mess on resume. Clamp
  13289. * things to off as a safer default. */
  13290. for_each_intel_connector(dev, connector) {
  13291. if (connector->encoder != encoder)
  13292. continue;
  13293. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13294. connector->base.encoder = NULL;
  13295. }
  13296. }
  13297. /* Enabled encoders without active connectors will be fixed in
  13298. * the crtc fixup. */
  13299. }
  13300. void i915_redisable_vga_power_on(struct drm_device *dev)
  13301. {
  13302. struct drm_i915_private *dev_priv = to_i915(dev);
  13303. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13304. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13305. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13306. i915_disable_vga(dev);
  13307. }
  13308. }
  13309. void i915_redisable_vga(struct drm_device *dev)
  13310. {
  13311. struct drm_i915_private *dev_priv = to_i915(dev);
  13312. /* This function can be called both from intel_modeset_setup_hw_state or
  13313. * at a very early point in our resume sequence, where the power well
  13314. * structures are not yet restored. Since this function is at a very
  13315. * paranoid "someone might have enabled VGA while we were not looking"
  13316. * level, just check if the power well is enabled instead of trying to
  13317. * follow the "don't touch the power well if we don't need it" policy
  13318. * the rest of the driver uses. */
  13319. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13320. return;
  13321. i915_redisable_vga_power_on(dev);
  13322. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13323. }
  13324. static bool primary_get_hw_state(struct intel_plane *plane)
  13325. {
  13326. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13327. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13328. }
  13329. /* FIXME read out full plane state for all planes */
  13330. static void readout_plane_state(struct intel_crtc *crtc)
  13331. {
  13332. struct drm_plane *primary = crtc->base.primary;
  13333. struct intel_plane_state *plane_state =
  13334. to_intel_plane_state(primary->state);
  13335. plane_state->visible = crtc->active &&
  13336. primary_get_hw_state(to_intel_plane(primary));
  13337. if (plane_state->visible)
  13338. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13339. }
  13340. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13341. {
  13342. struct drm_i915_private *dev_priv = to_i915(dev);
  13343. enum pipe pipe;
  13344. struct intel_crtc *crtc;
  13345. struct intel_encoder *encoder;
  13346. struct intel_connector *connector;
  13347. int i;
  13348. dev_priv->active_crtcs = 0;
  13349. for_each_intel_crtc(dev, crtc) {
  13350. struct intel_crtc_state *crtc_state = crtc->config;
  13351. int pixclk = 0;
  13352. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13353. memset(crtc_state, 0, sizeof(*crtc_state));
  13354. crtc_state->base.crtc = &crtc->base;
  13355. crtc_state->base.active = crtc_state->base.enable =
  13356. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13357. crtc->base.enabled = crtc_state->base.enable;
  13358. crtc->active = crtc_state->base.active;
  13359. if (crtc_state->base.active) {
  13360. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13361. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13362. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13363. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13364. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13365. else
  13366. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13367. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13368. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13369. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13370. }
  13371. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13372. readout_plane_state(crtc);
  13373. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13374. crtc->base.base.id, crtc->base.name,
  13375. crtc->active ? "enabled" : "disabled");
  13376. }
  13377. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13378. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13379. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13380. &pll->config.hw_state);
  13381. pll->config.crtc_mask = 0;
  13382. for_each_intel_crtc(dev, crtc) {
  13383. if (crtc->active && crtc->config->shared_dpll == pll)
  13384. pll->config.crtc_mask |= 1 << crtc->pipe;
  13385. }
  13386. pll->active_mask = pll->config.crtc_mask;
  13387. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13388. pll->name, pll->config.crtc_mask, pll->on);
  13389. }
  13390. for_each_intel_encoder(dev, encoder) {
  13391. pipe = 0;
  13392. if (encoder->get_hw_state(encoder, &pipe)) {
  13393. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13394. encoder->base.crtc = &crtc->base;
  13395. crtc->config->output_types |= 1 << encoder->type;
  13396. encoder->get_config(encoder, crtc->config);
  13397. } else {
  13398. encoder->base.crtc = NULL;
  13399. }
  13400. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13401. encoder->base.base.id,
  13402. encoder->base.name,
  13403. encoder->base.crtc ? "enabled" : "disabled",
  13404. pipe_name(pipe));
  13405. }
  13406. for_each_intel_connector(dev, connector) {
  13407. if (connector->get_hw_state(connector)) {
  13408. connector->base.dpms = DRM_MODE_DPMS_ON;
  13409. encoder = connector->encoder;
  13410. connector->base.encoder = &encoder->base;
  13411. if (encoder->base.crtc &&
  13412. encoder->base.crtc->state->active) {
  13413. /*
  13414. * This has to be done during hardware readout
  13415. * because anything calling .crtc_disable may
  13416. * rely on the connector_mask being accurate.
  13417. */
  13418. encoder->base.crtc->state->connector_mask |=
  13419. 1 << drm_connector_index(&connector->base);
  13420. encoder->base.crtc->state->encoder_mask |=
  13421. 1 << drm_encoder_index(&encoder->base);
  13422. }
  13423. } else {
  13424. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13425. connector->base.encoder = NULL;
  13426. }
  13427. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13428. connector->base.base.id,
  13429. connector->base.name,
  13430. connector->base.encoder ? "enabled" : "disabled");
  13431. }
  13432. for_each_intel_crtc(dev, crtc) {
  13433. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13434. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13435. if (crtc->base.state->active) {
  13436. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13437. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13438. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13439. /*
  13440. * The initial mode needs to be set in order to keep
  13441. * the atomic core happy. It wants a valid mode if the
  13442. * crtc's enabled, so we do the above call.
  13443. *
  13444. * At this point some state updated by the connectors
  13445. * in their ->detect() callback has not run yet, so
  13446. * no recalculation can be done yet.
  13447. *
  13448. * Even if we could do a recalculation and modeset
  13449. * right now it would cause a double modeset if
  13450. * fbdev or userspace chooses a different initial mode.
  13451. *
  13452. * If that happens, someone indicated they wanted a
  13453. * mode change, which means it's safe to do a full
  13454. * recalculation.
  13455. */
  13456. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13457. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13458. update_scanline_offset(crtc);
  13459. }
  13460. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13461. }
  13462. }
  13463. /* Scan out the current hw modeset state,
  13464. * and sanitizes it to the current state
  13465. */
  13466. static void
  13467. intel_modeset_setup_hw_state(struct drm_device *dev)
  13468. {
  13469. struct drm_i915_private *dev_priv = to_i915(dev);
  13470. enum pipe pipe;
  13471. struct intel_crtc *crtc;
  13472. struct intel_encoder *encoder;
  13473. int i;
  13474. intel_modeset_readout_hw_state(dev);
  13475. /* HW state is read out, now we need to sanitize this mess. */
  13476. for_each_intel_encoder(dev, encoder) {
  13477. intel_sanitize_encoder(encoder);
  13478. }
  13479. for_each_pipe(dev_priv, pipe) {
  13480. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13481. intel_sanitize_crtc(crtc);
  13482. intel_dump_pipe_config(crtc, crtc->config,
  13483. "[setup_hw_state]");
  13484. }
  13485. intel_modeset_update_connector_atomic_state(dev);
  13486. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13487. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13488. if (!pll->on || pll->active_mask)
  13489. continue;
  13490. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13491. pll->funcs.disable(dev_priv, pll);
  13492. pll->on = false;
  13493. }
  13494. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13495. vlv_wm_get_hw_state(dev);
  13496. else if (IS_GEN9(dev))
  13497. skl_wm_get_hw_state(dev);
  13498. else if (HAS_PCH_SPLIT(dev))
  13499. ilk_wm_get_hw_state(dev);
  13500. for_each_intel_crtc(dev, crtc) {
  13501. unsigned long put_domains;
  13502. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13503. if (WARN_ON(put_domains))
  13504. modeset_put_power_domains(dev_priv, put_domains);
  13505. }
  13506. intel_display_set_init_power(dev_priv, false);
  13507. intel_fbc_init_pipe_state(dev_priv);
  13508. }
  13509. void intel_display_resume(struct drm_device *dev)
  13510. {
  13511. struct drm_i915_private *dev_priv = to_i915(dev);
  13512. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13513. struct drm_modeset_acquire_ctx ctx;
  13514. int ret;
  13515. bool setup = false;
  13516. dev_priv->modeset_restore_state = NULL;
  13517. /*
  13518. * This is a cludge because with real atomic modeset mode_config.mutex
  13519. * won't be taken. Unfortunately some probed state like
  13520. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13521. * it here for now.
  13522. */
  13523. mutex_lock(&dev->mode_config.mutex);
  13524. drm_modeset_acquire_init(&ctx, 0);
  13525. retry:
  13526. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13527. if (ret == 0 && !setup) {
  13528. setup = true;
  13529. intel_modeset_setup_hw_state(dev);
  13530. i915_redisable_vga(dev);
  13531. }
  13532. if (ret == 0 && state) {
  13533. struct drm_crtc_state *crtc_state;
  13534. struct drm_crtc *crtc;
  13535. int i;
  13536. state->acquire_ctx = &ctx;
  13537. /* ignore any reset values/BIOS leftovers in the WM registers */
  13538. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13539. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13540. /*
  13541. * Force recalculation even if we restore
  13542. * current state. With fast modeset this may not result
  13543. * in a modeset when the state is compatible.
  13544. */
  13545. crtc_state->mode_changed = true;
  13546. }
  13547. ret = drm_atomic_commit(state);
  13548. }
  13549. if (ret == -EDEADLK) {
  13550. drm_modeset_backoff(&ctx);
  13551. goto retry;
  13552. }
  13553. drm_modeset_drop_locks(&ctx);
  13554. drm_modeset_acquire_fini(&ctx);
  13555. mutex_unlock(&dev->mode_config.mutex);
  13556. if (ret) {
  13557. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13558. drm_atomic_state_free(state);
  13559. }
  13560. }
  13561. void intel_modeset_gem_init(struct drm_device *dev)
  13562. {
  13563. struct drm_i915_private *dev_priv = to_i915(dev);
  13564. struct drm_crtc *c;
  13565. struct drm_i915_gem_object *obj;
  13566. int ret;
  13567. intel_init_gt_powersave(dev_priv);
  13568. intel_modeset_init_hw(dev);
  13569. intel_setup_overlay(dev_priv);
  13570. /*
  13571. * Make sure any fbs we allocated at startup are properly
  13572. * pinned & fenced. When we do the allocation it's too early
  13573. * for this.
  13574. */
  13575. for_each_crtc(dev, c) {
  13576. obj = intel_fb_obj(c->primary->fb);
  13577. if (obj == NULL)
  13578. continue;
  13579. mutex_lock(&dev->struct_mutex);
  13580. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13581. c->primary->state->rotation);
  13582. mutex_unlock(&dev->struct_mutex);
  13583. if (ret) {
  13584. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13585. to_intel_crtc(c)->pipe);
  13586. drm_framebuffer_unreference(c->primary->fb);
  13587. c->primary->fb = NULL;
  13588. c->primary->crtc = c->primary->state->crtc = NULL;
  13589. update_state_fb(c->primary);
  13590. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13591. }
  13592. }
  13593. }
  13594. int intel_connector_register(struct drm_connector *connector)
  13595. {
  13596. struct intel_connector *intel_connector = to_intel_connector(connector);
  13597. int ret;
  13598. ret = intel_backlight_device_register(intel_connector);
  13599. if (ret)
  13600. goto err;
  13601. return 0;
  13602. err:
  13603. return ret;
  13604. }
  13605. void intel_connector_unregister(struct drm_connector *connector)
  13606. {
  13607. struct intel_connector *intel_connector = to_intel_connector(connector);
  13608. intel_backlight_device_unregister(intel_connector);
  13609. intel_panel_destroy_backlight(connector);
  13610. }
  13611. void intel_modeset_cleanup(struct drm_device *dev)
  13612. {
  13613. struct drm_i915_private *dev_priv = to_i915(dev);
  13614. intel_disable_gt_powersave(dev_priv);
  13615. /*
  13616. * Interrupts and polling as the first thing to avoid creating havoc.
  13617. * Too much stuff here (turning of connectors, ...) would
  13618. * experience fancy races otherwise.
  13619. */
  13620. intel_irq_uninstall(dev_priv);
  13621. /*
  13622. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13623. * poll handlers. Hence disable polling after hpd handling is shut down.
  13624. */
  13625. drm_kms_helper_poll_fini(dev);
  13626. intel_unregister_dsm_handler();
  13627. intel_fbc_global_disable(dev_priv);
  13628. /* flush any delayed tasks or pending work */
  13629. flush_scheduled_work();
  13630. drm_mode_config_cleanup(dev);
  13631. intel_cleanup_overlay(dev_priv);
  13632. intel_cleanup_gt_powersave(dev_priv);
  13633. intel_teardown_gmbus(dev);
  13634. }
  13635. void intel_connector_attach_encoder(struct intel_connector *connector,
  13636. struct intel_encoder *encoder)
  13637. {
  13638. connector->encoder = encoder;
  13639. drm_mode_connector_attach_encoder(&connector->base,
  13640. &encoder->base);
  13641. }
  13642. /*
  13643. * set vga decode state - true == enable VGA decode
  13644. */
  13645. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13646. {
  13647. struct drm_i915_private *dev_priv = to_i915(dev);
  13648. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13649. u16 gmch_ctrl;
  13650. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13651. DRM_ERROR("failed to read control word\n");
  13652. return -EIO;
  13653. }
  13654. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13655. return 0;
  13656. if (state)
  13657. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13658. else
  13659. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13660. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13661. DRM_ERROR("failed to write control word\n");
  13662. return -EIO;
  13663. }
  13664. return 0;
  13665. }
  13666. struct intel_display_error_state {
  13667. u32 power_well_driver;
  13668. int num_transcoders;
  13669. struct intel_cursor_error_state {
  13670. u32 control;
  13671. u32 position;
  13672. u32 base;
  13673. u32 size;
  13674. } cursor[I915_MAX_PIPES];
  13675. struct intel_pipe_error_state {
  13676. bool power_domain_on;
  13677. u32 source;
  13678. u32 stat;
  13679. } pipe[I915_MAX_PIPES];
  13680. struct intel_plane_error_state {
  13681. u32 control;
  13682. u32 stride;
  13683. u32 size;
  13684. u32 pos;
  13685. u32 addr;
  13686. u32 surface;
  13687. u32 tile_offset;
  13688. } plane[I915_MAX_PIPES];
  13689. struct intel_transcoder_error_state {
  13690. bool power_domain_on;
  13691. enum transcoder cpu_transcoder;
  13692. u32 conf;
  13693. u32 htotal;
  13694. u32 hblank;
  13695. u32 hsync;
  13696. u32 vtotal;
  13697. u32 vblank;
  13698. u32 vsync;
  13699. } transcoder[4];
  13700. };
  13701. struct intel_display_error_state *
  13702. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13703. {
  13704. struct intel_display_error_state *error;
  13705. int transcoders[] = {
  13706. TRANSCODER_A,
  13707. TRANSCODER_B,
  13708. TRANSCODER_C,
  13709. TRANSCODER_EDP,
  13710. };
  13711. int i;
  13712. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13713. return NULL;
  13714. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13715. if (error == NULL)
  13716. return NULL;
  13717. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13718. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13719. for_each_pipe(dev_priv, i) {
  13720. error->pipe[i].power_domain_on =
  13721. __intel_display_power_is_enabled(dev_priv,
  13722. POWER_DOMAIN_PIPE(i));
  13723. if (!error->pipe[i].power_domain_on)
  13724. continue;
  13725. error->cursor[i].control = I915_READ(CURCNTR(i));
  13726. error->cursor[i].position = I915_READ(CURPOS(i));
  13727. error->cursor[i].base = I915_READ(CURBASE(i));
  13728. error->plane[i].control = I915_READ(DSPCNTR(i));
  13729. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13730. if (INTEL_GEN(dev_priv) <= 3) {
  13731. error->plane[i].size = I915_READ(DSPSIZE(i));
  13732. error->plane[i].pos = I915_READ(DSPPOS(i));
  13733. }
  13734. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13735. error->plane[i].addr = I915_READ(DSPADDR(i));
  13736. if (INTEL_GEN(dev_priv) >= 4) {
  13737. error->plane[i].surface = I915_READ(DSPSURF(i));
  13738. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13739. }
  13740. error->pipe[i].source = I915_READ(PIPESRC(i));
  13741. if (HAS_GMCH_DISPLAY(dev_priv))
  13742. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13743. }
  13744. /* Note: this does not include DSI transcoders. */
  13745. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13746. if (HAS_DDI(dev_priv))
  13747. error->num_transcoders++; /* Account for eDP. */
  13748. for (i = 0; i < error->num_transcoders; i++) {
  13749. enum transcoder cpu_transcoder = transcoders[i];
  13750. error->transcoder[i].power_domain_on =
  13751. __intel_display_power_is_enabled(dev_priv,
  13752. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13753. if (!error->transcoder[i].power_domain_on)
  13754. continue;
  13755. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13756. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13757. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13758. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13759. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13760. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13761. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13762. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13763. }
  13764. return error;
  13765. }
  13766. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13767. void
  13768. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13769. struct drm_device *dev,
  13770. struct intel_display_error_state *error)
  13771. {
  13772. struct drm_i915_private *dev_priv = to_i915(dev);
  13773. int i;
  13774. if (!error)
  13775. return;
  13776. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13777. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13778. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13779. error->power_well_driver);
  13780. for_each_pipe(dev_priv, i) {
  13781. err_printf(m, "Pipe [%d]:\n", i);
  13782. err_printf(m, " Power: %s\n",
  13783. onoff(error->pipe[i].power_domain_on));
  13784. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13785. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13786. err_printf(m, "Plane [%d]:\n", i);
  13787. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13788. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13789. if (INTEL_INFO(dev)->gen <= 3) {
  13790. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13791. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13792. }
  13793. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13794. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13795. if (INTEL_INFO(dev)->gen >= 4) {
  13796. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13797. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13798. }
  13799. err_printf(m, "Cursor [%d]:\n", i);
  13800. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13801. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13802. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13803. }
  13804. for (i = 0; i < error->num_transcoders; i++) {
  13805. err_printf(m, "CPU transcoder: %s\n",
  13806. transcoder_name(error->transcoder[i].cpu_transcoder));
  13807. err_printf(m, " Power: %s\n",
  13808. onoff(error->transcoder[i].power_domain_on));
  13809. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13810. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13811. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13812. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13813. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13814. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13815. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13816. }
  13817. }