intel_display.c 461 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535
  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 unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1772. {
  1773. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1774. }
  1775. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1776. uint64_t fb_modifier, unsigned int cpp)
  1777. {
  1778. switch (fb_modifier) {
  1779. case DRM_FORMAT_MOD_NONE:
  1780. return cpp;
  1781. case I915_FORMAT_MOD_X_TILED:
  1782. if (IS_GEN2(dev_priv))
  1783. return 128;
  1784. else
  1785. return 512;
  1786. case I915_FORMAT_MOD_Y_TILED:
  1787. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1788. return 128;
  1789. else
  1790. return 512;
  1791. case I915_FORMAT_MOD_Yf_TILED:
  1792. switch (cpp) {
  1793. case 1:
  1794. return 64;
  1795. case 2:
  1796. case 4:
  1797. return 128;
  1798. case 8:
  1799. case 16:
  1800. return 256;
  1801. default:
  1802. MISSING_CASE(cpp);
  1803. return cpp;
  1804. }
  1805. break;
  1806. default:
  1807. MISSING_CASE(fb_modifier);
  1808. return cpp;
  1809. }
  1810. }
  1811. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1812. uint64_t fb_modifier, unsigned int cpp)
  1813. {
  1814. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1815. return 1;
  1816. else
  1817. return intel_tile_size(dev_priv) /
  1818. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1819. }
  1820. /* Return the tile dimensions in pixel units */
  1821. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1822. unsigned int *tile_width,
  1823. unsigned int *tile_height,
  1824. uint64_t fb_modifier,
  1825. unsigned int cpp)
  1826. {
  1827. unsigned int tile_width_bytes =
  1828. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1829. *tile_width = tile_width_bytes / cpp;
  1830. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1831. }
  1832. unsigned int
  1833. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1834. uint32_t pixel_format, uint64_t fb_modifier)
  1835. {
  1836. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1837. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1838. return ALIGN(height, tile_height);
  1839. }
  1840. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1841. {
  1842. unsigned int size = 0;
  1843. int i;
  1844. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1845. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1846. return size;
  1847. }
  1848. static void
  1849. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1850. const struct drm_framebuffer *fb,
  1851. unsigned int rotation)
  1852. {
  1853. if (intel_rotation_90_or_270(rotation)) {
  1854. *view = i915_ggtt_view_rotated;
  1855. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1856. } else {
  1857. *view = i915_ggtt_view_normal;
  1858. }
  1859. }
  1860. static void
  1861. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1862. struct drm_framebuffer *fb)
  1863. {
  1864. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1865. unsigned int tile_size, tile_width, tile_height, cpp;
  1866. tile_size = intel_tile_size(dev_priv);
  1867. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1868. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1869. fb->modifier[0], cpp);
  1870. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1871. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1872. if (info->pixel_format == DRM_FORMAT_NV12) {
  1873. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1874. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1875. fb->modifier[1], cpp);
  1876. info->uv_offset = fb->offsets[1];
  1877. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1878. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1879. }
  1880. }
  1881. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1882. {
  1883. if (INTEL_INFO(dev_priv)->gen >= 9)
  1884. return 256 * 1024;
  1885. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1886. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1887. return 128 * 1024;
  1888. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1889. return 4 * 1024;
  1890. else
  1891. return 0;
  1892. }
  1893. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1894. uint64_t fb_modifier)
  1895. {
  1896. switch (fb_modifier) {
  1897. case DRM_FORMAT_MOD_NONE:
  1898. return intel_linear_alignment(dev_priv);
  1899. case I915_FORMAT_MOD_X_TILED:
  1900. if (INTEL_INFO(dev_priv)->gen >= 9)
  1901. return 256 * 1024;
  1902. return 0;
  1903. case I915_FORMAT_MOD_Y_TILED:
  1904. case I915_FORMAT_MOD_Yf_TILED:
  1905. return 1 * 1024 * 1024;
  1906. default:
  1907. MISSING_CASE(fb_modifier);
  1908. return 0;
  1909. }
  1910. }
  1911. int
  1912. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1913. unsigned int rotation)
  1914. {
  1915. struct drm_device *dev = fb->dev;
  1916. struct drm_i915_private *dev_priv = to_i915(dev);
  1917. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1918. struct i915_ggtt_view view;
  1919. u32 alignment;
  1920. int ret;
  1921. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1922. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1923. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1924. /* Note that the w/a also requires 64 PTE of padding following the
  1925. * bo. We currently fill all unused PTE with the shadow page and so
  1926. * we should always have valid PTE following the scanout preventing
  1927. * the VT-d warning.
  1928. */
  1929. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1930. alignment = 256 * 1024;
  1931. /*
  1932. * Global gtt pte registers are special registers which actually forward
  1933. * writes to a chunk of system memory. Which means that there is no risk
  1934. * that the register values disappear as soon as we call
  1935. * intel_runtime_pm_put(), so it is correct to wrap only the
  1936. * pin/unpin/fence and not more.
  1937. */
  1938. intel_runtime_pm_get(dev_priv);
  1939. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1940. &view);
  1941. if (ret)
  1942. goto err_pm;
  1943. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1944. * fence, whereas 965+ only requires a fence if using
  1945. * framebuffer compression. For simplicity, we always install
  1946. * a fence as the cost is not that onerous.
  1947. */
  1948. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1949. ret = i915_gem_object_get_fence(obj);
  1950. if (ret == -EDEADLK) {
  1951. /*
  1952. * -EDEADLK means there are no free fences
  1953. * no pending flips.
  1954. *
  1955. * This is propagated to atomic, but it uses
  1956. * -EDEADLK to force a locking recovery, so
  1957. * change the returned error to -EBUSY.
  1958. */
  1959. ret = -EBUSY;
  1960. goto err_unpin;
  1961. } else if (ret)
  1962. goto err_unpin;
  1963. i915_gem_object_pin_fence(obj);
  1964. }
  1965. intel_runtime_pm_put(dev_priv);
  1966. return 0;
  1967. err_unpin:
  1968. i915_gem_object_unpin_from_display_plane(obj, &view);
  1969. err_pm:
  1970. intel_runtime_pm_put(dev_priv);
  1971. return ret;
  1972. }
  1973. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1974. {
  1975. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1976. struct i915_ggtt_view view;
  1977. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1978. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1979. if (view.type == I915_GGTT_VIEW_NORMAL)
  1980. i915_gem_object_unpin_fence(obj);
  1981. i915_gem_object_unpin_from_display_plane(obj, &view);
  1982. }
  1983. /*
  1984. * Adjust the tile offset by moving the difference into
  1985. * the x/y offsets.
  1986. *
  1987. * Input tile dimensions and pitch must already be
  1988. * rotated to match x and y, and in pixel units.
  1989. */
  1990. static u32 intel_adjust_tile_offset(int *x, int *y,
  1991. unsigned int tile_width,
  1992. unsigned int tile_height,
  1993. unsigned int tile_size,
  1994. unsigned int pitch_tiles,
  1995. u32 old_offset,
  1996. u32 new_offset)
  1997. {
  1998. unsigned int tiles;
  1999. WARN_ON(old_offset & (tile_size - 1));
  2000. WARN_ON(new_offset & (tile_size - 1));
  2001. WARN_ON(new_offset > old_offset);
  2002. tiles = (old_offset - new_offset) / tile_size;
  2003. *y += tiles / pitch_tiles * tile_height;
  2004. *x += tiles % pitch_tiles * tile_width;
  2005. return new_offset;
  2006. }
  2007. /*
  2008. * Computes the linear offset to the base tile and adjusts
  2009. * x, y. bytes per pixel is assumed to be a power-of-two.
  2010. *
  2011. * In the 90/270 rotated case, x and y are assumed
  2012. * to be already rotated to match the rotated GTT view, and
  2013. * pitch is the tile_height aligned framebuffer height.
  2014. */
  2015. u32 intel_compute_tile_offset(int *x, int *y,
  2016. const struct drm_framebuffer *fb, int plane,
  2017. unsigned int pitch,
  2018. unsigned int rotation)
  2019. {
  2020. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2021. uint64_t fb_modifier = fb->modifier[plane];
  2022. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2023. u32 offset, offset_aligned, alignment;
  2024. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2025. if (alignment)
  2026. alignment--;
  2027. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2028. unsigned int tile_size, tile_width, tile_height;
  2029. unsigned int tile_rows, tiles, pitch_tiles;
  2030. tile_size = intel_tile_size(dev_priv);
  2031. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2032. fb_modifier, cpp);
  2033. if (intel_rotation_90_or_270(rotation)) {
  2034. pitch_tiles = pitch / tile_height;
  2035. swap(tile_width, tile_height);
  2036. } else {
  2037. pitch_tiles = pitch / (tile_width * cpp);
  2038. }
  2039. tile_rows = *y / tile_height;
  2040. *y %= tile_height;
  2041. tiles = *x / tile_width;
  2042. *x %= tile_width;
  2043. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2044. offset_aligned = offset & ~alignment;
  2045. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2046. tile_size, pitch_tiles,
  2047. offset, offset_aligned);
  2048. } else {
  2049. offset = *y * pitch + *x * cpp;
  2050. offset_aligned = offset & ~alignment;
  2051. *y = (offset & alignment) / pitch;
  2052. *x = ((offset & alignment) - *y * pitch) / cpp;
  2053. }
  2054. return offset_aligned;
  2055. }
  2056. static int i9xx_format_to_fourcc(int format)
  2057. {
  2058. switch (format) {
  2059. case DISPPLANE_8BPP:
  2060. return DRM_FORMAT_C8;
  2061. case DISPPLANE_BGRX555:
  2062. return DRM_FORMAT_XRGB1555;
  2063. case DISPPLANE_BGRX565:
  2064. return DRM_FORMAT_RGB565;
  2065. default:
  2066. case DISPPLANE_BGRX888:
  2067. return DRM_FORMAT_XRGB8888;
  2068. case DISPPLANE_RGBX888:
  2069. return DRM_FORMAT_XBGR8888;
  2070. case DISPPLANE_BGRX101010:
  2071. return DRM_FORMAT_XRGB2101010;
  2072. case DISPPLANE_RGBX101010:
  2073. return DRM_FORMAT_XBGR2101010;
  2074. }
  2075. }
  2076. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2077. {
  2078. switch (format) {
  2079. case PLANE_CTL_FORMAT_RGB_565:
  2080. return DRM_FORMAT_RGB565;
  2081. default:
  2082. case PLANE_CTL_FORMAT_XRGB_8888:
  2083. if (rgb_order) {
  2084. if (alpha)
  2085. return DRM_FORMAT_ABGR8888;
  2086. else
  2087. return DRM_FORMAT_XBGR8888;
  2088. } else {
  2089. if (alpha)
  2090. return DRM_FORMAT_ARGB8888;
  2091. else
  2092. return DRM_FORMAT_XRGB8888;
  2093. }
  2094. case PLANE_CTL_FORMAT_XRGB_2101010:
  2095. if (rgb_order)
  2096. return DRM_FORMAT_XBGR2101010;
  2097. else
  2098. return DRM_FORMAT_XRGB2101010;
  2099. }
  2100. }
  2101. static bool
  2102. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2103. struct intel_initial_plane_config *plane_config)
  2104. {
  2105. struct drm_device *dev = crtc->base.dev;
  2106. struct drm_i915_private *dev_priv = to_i915(dev);
  2107. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2108. struct drm_i915_gem_object *obj = NULL;
  2109. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2110. struct drm_framebuffer *fb = &plane_config->fb->base;
  2111. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2112. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2113. PAGE_SIZE);
  2114. size_aligned -= base_aligned;
  2115. if (plane_config->size == 0)
  2116. return false;
  2117. /* If the FB is too big, just don't use it since fbdev is not very
  2118. * important and we should probably use that space with FBC or other
  2119. * features. */
  2120. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2121. return false;
  2122. mutex_lock(&dev->struct_mutex);
  2123. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2124. base_aligned,
  2125. base_aligned,
  2126. size_aligned);
  2127. if (!obj) {
  2128. mutex_unlock(&dev->struct_mutex);
  2129. return false;
  2130. }
  2131. obj->tiling_mode = plane_config->tiling;
  2132. if (obj->tiling_mode == I915_TILING_X)
  2133. obj->stride = fb->pitches[0];
  2134. mode_cmd.pixel_format = fb->pixel_format;
  2135. mode_cmd.width = fb->width;
  2136. mode_cmd.height = fb->height;
  2137. mode_cmd.pitches[0] = fb->pitches[0];
  2138. mode_cmd.modifier[0] = fb->modifier[0];
  2139. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2140. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2141. &mode_cmd, obj)) {
  2142. DRM_DEBUG_KMS("intel fb init failed\n");
  2143. goto out_unref_obj;
  2144. }
  2145. mutex_unlock(&dev->struct_mutex);
  2146. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2147. return true;
  2148. out_unref_obj:
  2149. drm_gem_object_unreference(&obj->base);
  2150. mutex_unlock(&dev->struct_mutex);
  2151. return false;
  2152. }
  2153. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2154. static void
  2155. update_state_fb(struct drm_plane *plane)
  2156. {
  2157. if (plane->fb == plane->state->fb)
  2158. return;
  2159. if (plane->state->fb)
  2160. drm_framebuffer_unreference(plane->state->fb);
  2161. plane->state->fb = plane->fb;
  2162. if (plane->state->fb)
  2163. drm_framebuffer_reference(plane->state->fb);
  2164. }
  2165. static void
  2166. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2167. struct intel_initial_plane_config *plane_config)
  2168. {
  2169. struct drm_device *dev = intel_crtc->base.dev;
  2170. struct drm_i915_private *dev_priv = to_i915(dev);
  2171. struct drm_crtc *c;
  2172. struct intel_crtc *i;
  2173. struct drm_i915_gem_object *obj;
  2174. struct drm_plane *primary = intel_crtc->base.primary;
  2175. struct drm_plane_state *plane_state = primary->state;
  2176. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2177. struct intel_plane *intel_plane = to_intel_plane(primary);
  2178. struct intel_plane_state *intel_state =
  2179. to_intel_plane_state(plane_state);
  2180. struct drm_framebuffer *fb;
  2181. if (!plane_config->fb)
  2182. return;
  2183. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2184. fb = &plane_config->fb->base;
  2185. goto valid_fb;
  2186. }
  2187. kfree(plane_config->fb);
  2188. /*
  2189. * Failed to alloc the obj, check to see if we should share
  2190. * an fb with another CRTC instead
  2191. */
  2192. for_each_crtc(dev, c) {
  2193. i = to_intel_crtc(c);
  2194. if (c == &intel_crtc->base)
  2195. continue;
  2196. if (!i->active)
  2197. continue;
  2198. fb = c->primary->fb;
  2199. if (!fb)
  2200. continue;
  2201. obj = intel_fb_obj(fb);
  2202. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2203. drm_framebuffer_reference(fb);
  2204. goto valid_fb;
  2205. }
  2206. }
  2207. /*
  2208. * We've failed to reconstruct the BIOS FB. Current display state
  2209. * indicates that the primary plane is visible, but has a NULL FB,
  2210. * which will lead to problems later if we don't fix it up. The
  2211. * simplest solution is to just disable the primary plane now and
  2212. * pretend the BIOS never had it enabled.
  2213. */
  2214. to_intel_plane_state(plane_state)->visible = false;
  2215. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2216. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2217. intel_plane->disable_plane(primary, &intel_crtc->base);
  2218. return;
  2219. valid_fb:
  2220. plane_state->src_x = 0;
  2221. plane_state->src_y = 0;
  2222. plane_state->src_w = fb->width << 16;
  2223. plane_state->src_h = fb->height << 16;
  2224. plane_state->crtc_x = 0;
  2225. plane_state->crtc_y = 0;
  2226. plane_state->crtc_w = fb->width;
  2227. plane_state->crtc_h = fb->height;
  2228. intel_state->src.x1 = plane_state->src_x;
  2229. intel_state->src.y1 = plane_state->src_y;
  2230. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2231. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2232. intel_state->dst.x1 = plane_state->crtc_x;
  2233. intel_state->dst.y1 = plane_state->crtc_y;
  2234. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2235. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2236. obj = intel_fb_obj(fb);
  2237. if (obj->tiling_mode != I915_TILING_NONE)
  2238. dev_priv->preserve_bios_swizzle = true;
  2239. drm_framebuffer_reference(fb);
  2240. primary->fb = primary->state->fb = fb;
  2241. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2242. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2243. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2244. }
  2245. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2246. const struct intel_crtc_state *crtc_state,
  2247. const struct intel_plane_state *plane_state)
  2248. {
  2249. struct drm_device *dev = primary->dev;
  2250. struct drm_i915_private *dev_priv = to_i915(dev);
  2251. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2252. struct drm_framebuffer *fb = plane_state->base.fb;
  2253. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2254. int plane = intel_crtc->plane;
  2255. u32 linear_offset;
  2256. u32 dspcntr;
  2257. i915_reg_t reg = DSPCNTR(plane);
  2258. unsigned int rotation = plane_state->base.rotation;
  2259. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2260. int x = plane_state->src.x1 >> 16;
  2261. int y = plane_state->src.y1 >> 16;
  2262. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2263. dspcntr |= DISPLAY_PLANE_ENABLE;
  2264. if (INTEL_INFO(dev)->gen < 4) {
  2265. if (intel_crtc->pipe == PIPE_B)
  2266. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2267. /* pipesrc and dspsize control the size that is scaled from,
  2268. * which should always be the user's requested size.
  2269. */
  2270. I915_WRITE(DSPSIZE(plane),
  2271. ((crtc_state->pipe_src_h - 1) << 16) |
  2272. (crtc_state->pipe_src_w - 1));
  2273. I915_WRITE(DSPPOS(plane), 0);
  2274. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2275. I915_WRITE(PRIMSIZE(plane),
  2276. ((crtc_state->pipe_src_h - 1) << 16) |
  2277. (crtc_state->pipe_src_w - 1));
  2278. I915_WRITE(PRIMPOS(plane), 0);
  2279. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2280. }
  2281. switch (fb->pixel_format) {
  2282. case DRM_FORMAT_C8:
  2283. dspcntr |= DISPPLANE_8BPP;
  2284. break;
  2285. case DRM_FORMAT_XRGB1555:
  2286. dspcntr |= DISPPLANE_BGRX555;
  2287. break;
  2288. case DRM_FORMAT_RGB565:
  2289. dspcntr |= DISPPLANE_BGRX565;
  2290. break;
  2291. case DRM_FORMAT_XRGB8888:
  2292. dspcntr |= DISPPLANE_BGRX888;
  2293. break;
  2294. case DRM_FORMAT_XBGR8888:
  2295. dspcntr |= DISPPLANE_RGBX888;
  2296. break;
  2297. case DRM_FORMAT_XRGB2101010:
  2298. dspcntr |= DISPPLANE_BGRX101010;
  2299. break;
  2300. case DRM_FORMAT_XBGR2101010:
  2301. dspcntr |= DISPPLANE_RGBX101010;
  2302. break;
  2303. default:
  2304. BUG();
  2305. }
  2306. if (INTEL_INFO(dev)->gen >= 4 &&
  2307. obj->tiling_mode != I915_TILING_NONE)
  2308. dspcntr |= DISPPLANE_TILED;
  2309. if (IS_G4X(dev))
  2310. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2311. linear_offset = y * fb->pitches[0] + x * cpp;
  2312. if (INTEL_INFO(dev)->gen >= 4) {
  2313. intel_crtc->dspaddr_offset =
  2314. intel_compute_tile_offset(&x, &y, fb, 0,
  2315. fb->pitches[0], rotation);
  2316. linear_offset -= intel_crtc->dspaddr_offset;
  2317. } else {
  2318. intel_crtc->dspaddr_offset = linear_offset;
  2319. }
  2320. if (rotation == BIT(DRM_ROTATE_180)) {
  2321. dspcntr |= DISPPLANE_ROTATE_180;
  2322. x += (crtc_state->pipe_src_w - 1);
  2323. y += (crtc_state->pipe_src_h - 1);
  2324. /* Finding the last pixel of the last line of the display
  2325. data and adding to linear_offset*/
  2326. linear_offset +=
  2327. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2328. (crtc_state->pipe_src_w - 1) * cpp;
  2329. }
  2330. intel_crtc->adjusted_x = x;
  2331. intel_crtc->adjusted_y = y;
  2332. I915_WRITE(reg, dspcntr);
  2333. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2334. if (INTEL_INFO(dev)->gen >= 4) {
  2335. I915_WRITE(DSPSURF(plane),
  2336. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2337. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2338. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2339. } else
  2340. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2341. POSTING_READ(reg);
  2342. }
  2343. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2344. struct drm_crtc *crtc)
  2345. {
  2346. struct drm_device *dev = crtc->dev;
  2347. struct drm_i915_private *dev_priv = to_i915(dev);
  2348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2349. int plane = intel_crtc->plane;
  2350. I915_WRITE(DSPCNTR(plane), 0);
  2351. if (INTEL_INFO(dev_priv)->gen >= 4)
  2352. I915_WRITE(DSPSURF(plane), 0);
  2353. else
  2354. I915_WRITE(DSPADDR(plane), 0);
  2355. POSTING_READ(DSPCNTR(plane));
  2356. }
  2357. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2358. const struct intel_crtc_state *crtc_state,
  2359. const struct intel_plane_state *plane_state)
  2360. {
  2361. struct drm_device *dev = primary->dev;
  2362. struct drm_i915_private *dev_priv = to_i915(dev);
  2363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2364. struct drm_framebuffer *fb = plane_state->base.fb;
  2365. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2366. int plane = intel_crtc->plane;
  2367. u32 linear_offset;
  2368. u32 dspcntr;
  2369. i915_reg_t reg = DSPCNTR(plane);
  2370. unsigned int rotation = plane_state->base.rotation;
  2371. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2372. int x = plane_state->src.x1 >> 16;
  2373. int y = plane_state->src.y1 >> 16;
  2374. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2375. dspcntr |= DISPLAY_PLANE_ENABLE;
  2376. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2377. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2378. switch (fb->pixel_format) {
  2379. case DRM_FORMAT_C8:
  2380. dspcntr |= DISPPLANE_8BPP;
  2381. break;
  2382. case DRM_FORMAT_RGB565:
  2383. dspcntr |= DISPPLANE_BGRX565;
  2384. break;
  2385. case DRM_FORMAT_XRGB8888:
  2386. dspcntr |= DISPPLANE_BGRX888;
  2387. break;
  2388. case DRM_FORMAT_XBGR8888:
  2389. dspcntr |= DISPPLANE_RGBX888;
  2390. break;
  2391. case DRM_FORMAT_XRGB2101010:
  2392. dspcntr |= DISPPLANE_BGRX101010;
  2393. break;
  2394. case DRM_FORMAT_XBGR2101010:
  2395. dspcntr |= DISPPLANE_RGBX101010;
  2396. break;
  2397. default:
  2398. BUG();
  2399. }
  2400. if (obj->tiling_mode != I915_TILING_NONE)
  2401. dspcntr |= DISPPLANE_TILED;
  2402. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2403. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2404. linear_offset = y * fb->pitches[0] + x * cpp;
  2405. intel_crtc->dspaddr_offset =
  2406. intel_compute_tile_offset(&x, &y, fb, 0,
  2407. fb->pitches[0], rotation);
  2408. linear_offset -= intel_crtc->dspaddr_offset;
  2409. if (rotation == BIT(DRM_ROTATE_180)) {
  2410. dspcntr |= DISPPLANE_ROTATE_180;
  2411. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2412. x += (crtc_state->pipe_src_w - 1);
  2413. y += (crtc_state->pipe_src_h - 1);
  2414. /* Finding the last pixel of the last line of the display
  2415. data and adding to linear_offset*/
  2416. linear_offset +=
  2417. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2418. (crtc_state->pipe_src_w - 1) * cpp;
  2419. }
  2420. }
  2421. intel_crtc->adjusted_x = x;
  2422. intel_crtc->adjusted_y = y;
  2423. I915_WRITE(reg, dspcntr);
  2424. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2425. I915_WRITE(DSPSURF(plane),
  2426. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2427. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2428. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2429. } else {
  2430. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2431. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2432. }
  2433. POSTING_READ(reg);
  2434. }
  2435. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2436. uint64_t fb_modifier, uint32_t pixel_format)
  2437. {
  2438. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2439. return 64;
  2440. } else {
  2441. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2442. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2443. }
  2444. }
  2445. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2446. struct drm_i915_gem_object *obj,
  2447. unsigned int plane)
  2448. {
  2449. struct i915_ggtt_view view;
  2450. struct i915_vma *vma;
  2451. u64 offset;
  2452. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2453. intel_plane->base.state->rotation);
  2454. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2455. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2456. view.type))
  2457. return -1;
  2458. offset = vma->node.start;
  2459. if (plane == 1) {
  2460. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2461. PAGE_SIZE;
  2462. }
  2463. WARN_ON(upper_32_bits(offset));
  2464. return lower_32_bits(offset);
  2465. }
  2466. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2467. {
  2468. struct drm_device *dev = intel_crtc->base.dev;
  2469. struct drm_i915_private *dev_priv = to_i915(dev);
  2470. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2471. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2472. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2473. }
  2474. /*
  2475. * This function detaches (aka. unbinds) unused scalers in hardware
  2476. */
  2477. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2478. {
  2479. struct intel_crtc_scaler_state *scaler_state;
  2480. int i;
  2481. scaler_state = &intel_crtc->config->scaler_state;
  2482. /* loop through and disable scalers that aren't in use */
  2483. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2484. if (!scaler_state->scalers[i].in_use)
  2485. skl_detach_scaler(intel_crtc, i);
  2486. }
  2487. }
  2488. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2489. {
  2490. switch (pixel_format) {
  2491. case DRM_FORMAT_C8:
  2492. return PLANE_CTL_FORMAT_INDEXED;
  2493. case DRM_FORMAT_RGB565:
  2494. return PLANE_CTL_FORMAT_RGB_565;
  2495. case DRM_FORMAT_XBGR8888:
  2496. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2497. case DRM_FORMAT_XRGB8888:
  2498. return PLANE_CTL_FORMAT_XRGB_8888;
  2499. /*
  2500. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2501. * to be already pre-multiplied. We need to add a knob (or a different
  2502. * DRM_FORMAT) for user-space to configure that.
  2503. */
  2504. case DRM_FORMAT_ABGR8888:
  2505. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2506. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2507. case DRM_FORMAT_ARGB8888:
  2508. return PLANE_CTL_FORMAT_XRGB_8888 |
  2509. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2510. case DRM_FORMAT_XRGB2101010:
  2511. return PLANE_CTL_FORMAT_XRGB_2101010;
  2512. case DRM_FORMAT_XBGR2101010:
  2513. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2514. case DRM_FORMAT_YUYV:
  2515. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2516. case DRM_FORMAT_YVYU:
  2517. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2518. case DRM_FORMAT_UYVY:
  2519. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2520. case DRM_FORMAT_VYUY:
  2521. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2522. default:
  2523. MISSING_CASE(pixel_format);
  2524. }
  2525. return 0;
  2526. }
  2527. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2528. {
  2529. switch (fb_modifier) {
  2530. case DRM_FORMAT_MOD_NONE:
  2531. break;
  2532. case I915_FORMAT_MOD_X_TILED:
  2533. return PLANE_CTL_TILED_X;
  2534. case I915_FORMAT_MOD_Y_TILED:
  2535. return PLANE_CTL_TILED_Y;
  2536. case I915_FORMAT_MOD_Yf_TILED:
  2537. return PLANE_CTL_TILED_YF;
  2538. default:
  2539. MISSING_CASE(fb_modifier);
  2540. }
  2541. return 0;
  2542. }
  2543. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2544. {
  2545. switch (rotation) {
  2546. case BIT(DRM_ROTATE_0):
  2547. break;
  2548. /*
  2549. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2550. * while i915 HW rotation is clockwise, thats why this swapping.
  2551. */
  2552. case BIT(DRM_ROTATE_90):
  2553. return PLANE_CTL_ROTATE_270;
  2554. case BIT(DRM_ROTATE_180):
  2555. return PLANE_CTL_ROTATE_180;
  2556. case BIT(DRM_ROTATE_270):
  2557. return PLANE_CTL_ROTATE_90;
  2558. default:
  2559. MISSING_CASE(rotation);
  2560. }
  2561. return 0;
  2562. }
  2563. static void skylake_update_primary_plane(struct drm_plane *plane,
  2564. const struct intel_crtc_state *crtc_state,
  2565. const struct intel_plane_state *plane_state)
  2566. {
  2567. struct drm_device *dev = plane->dev;
  2568. struct drm_i915_private *dev_priv = to_i915(dev);
  2569. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2570. struct drm_framebuffer *fb = plane_state->base.fb;
  2571. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2572. int pipe = intel_crtc->pipe;
  2573. u32 plane_ctl, stride_div, stride;
  2574. u32 tile_height, plane_offset, plane_size;
  2575. unsigned int rotation = plane_state->base.rotation;
  2576. int x_offset, y_offset;
  2577. u32 surf_addr;
  2578. int scaler_id = plane_state->scaler_id;
  2579. int src_x = plane_state->src.x1 >> 16;
  2580. int src_y = plane_state->src.y1 >> 16;
  2581. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2582. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2583. int dst_x = plane_state->dst.x1;
  2584. int dst_y = plane_state->dst.y1;
  2585. int dst_w = drm_rect_width(&plane_state->dst);
  2586. int dst_h = drm_rect_height(&plane_state->dst);
  2587. plane_ctl = PLANE_CTL_ENABLE |
  2588. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2589. PLANE_CTL_PIPE_CSC_ENABLE;
  2590. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2591. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2592. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2593. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2594. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2595. fb->pixel_format);
  2596. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2597. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2598. if (intel_rotation_90_or_270(rotation)) {
  2599. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2600. /* stride = Surface height in tiles */
  2601. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2602. stride = DIV_ROUND_UP(fb->height, tile_height);
  2603. x_offset = stride * tile_height - src_y - src_h;
  2604. y_offset = src_x;
  2605. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2606. } else {
  2607. stride = fb->pitches[0] / stride_div;
  2608. x_offset = src_x;
  2609. y_offset = src_y;
  2610. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2611. }
  2612. plane_offset = y_offset << 16 | x_offset;
  2613. intel_crtc->adjusted_x = x_offset;
  2614. intel_crtc->adjusted_y = y_offset;
  2615. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2616. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2617. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2618. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2619. if (scaler_id >= 0) {
  2620. uint32_t ps_ctrl = 0;
  2621. WARN_ON(!dst_w || !dst_h);
  2622. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2623. crtc_state->scaler_state.scalers[scaler_id].mode;
  2624. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2625. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2626. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2627. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2628. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2629. } else {
  2630. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2631. }
  2632. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2633. POSTING_READ(PLANE_SURF(pipe, 0));
  2634. }
  2635. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2636. struct drm_crtc *crtc)
  2637. {
  2638. struct drm_device *dev = crtc->dev;
  2639. struct drm_i915_private *dev_priv = to_i915(dev);
  2640. int pipe = to_intel_crtc(crtc)->pipe;
  2641. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2642. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2643. POSTING_READ(PLANE_SURF(pipe, 0));
  2644. }
  2645. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2646. static int
  2647. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2648. int x, int y, enum mode_set_atomic state)
  2649. {
  2650. /* Support for kgdboc is disabled, this needs a major rework. */
  2651. DRM_ERROR("legacy panic handler not supported any more.\n");
  2652. return -ENODEV;
  2653. }
  2654. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2655. {
  2656. struct intel_crtc *crtc;
  2657. for_each_intel_crtc(&dev_priv->drm, crtc)
  2658. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2659. }
  2660. static void intel_update_primary_planes(struct drm_device *dev)
  2661. {
  2662. struct drm_crtc *crtc;
  2663. for_each_crtc(dev, crtc) {
  2664. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2665. struct intel_plane_state *plane_state;
  2666. drm_modeset_lock_crtc(crtc, &plane->base);
  2667. plane_state = to_intel_plane_state(plane->base.state);
  2668. if (plane_state->visible)
  2669. plane->update_plane(&plane->base,
  2670. to_intel_crtc_state(crtc->state),
  2671. plane_state);
  2672. drm_modeset_unlock_crtc(crtc);
  2673. }
  2674. }
  2675. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2676. {
  2677. /* no reset support for gen2 */
  2678. if (IS_GEN2(dev_priv))
  2679. return;
  2680. /* reset doesn't touch the display */
  2681. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2682. return;
  2683. drm_modeset_lock_all(&dev_priv->drm);
  2684. /*
  2685. * Disabling the crtcs gracefully seems nicer. Also the
  2686. * g33 docs say we should at least disable all the planes.
  2687. */
  2688. intel_display_suspend(&dev_priv->drm);
  2689. }
  2690. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2691. {
  2692. /*
  2693. * Flips in the rings will be nuked by the reset,
  2694. * so complete all pending flips so that user space
  2695. * will get its events and not get stuck.
  2696. */
  2697. intel_complete_page_flips(dev_priv);
  2698. /* no reset support for gen2 */
  2699. if (IS_GEN2(dev_priv))
  2700. return;
  2701. /* reset doesn't touch the display */
  2702. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2703. /*
  2704. * Flips in the rings have been nuked by the reset,
  2705. * so update the base address of all primary
  2706. * planes to the the last fb to make sure we're
  2707. * showing the correct fb after a reset.
  2708. *
  2709. * FIXME: Atomic will make this obsolete since we won't schedule
  2710. * CS-based flips (which might get lost in gpu resets) any more.
  2711. */
  2712. intel_update_primary_planes(&dev_priv->drm);
  2713. return;
  2714. }
  2715. /*
  2716. * The display has been reset as well,
  2717. * so need a full re-initialization.
  2718. */
  2719. intel_runtime_pm_disable_interrupts(dev_priv);
  2720. intel_runtime_pm_enable_interrupts(dev_priv);
  2721. intel_modeset_init_hw(&dev_priv->drm);
  2722. spin_lock_irq(&dev_priv->irq_lock);
  2723. if (dev_priv->display.hpd_irq_setup)
  2724. dev_priv->display.hpd_irq_setup(dev_priv);
  2725. spin_unlock_irq(&dev_priv->irq_lock);
  2726. intel_display_resume(&dev_priv->drm);
  2727. intel_hpd_init(dev_priv);
  2728. drm_modeset_unlock_all(&dev_priv->drm);
  2729. }
  2730. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2731. {
  2732. struct drm_device *dev = crtc->dev;
  2733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2734. unsigned reset_counter;
  2735. bool pending;
  2736. reset_counter = i915_reset_counter(&to_i915(dev)->gpu_error);
  2737. if (intel_crtc->reset_counter != reset_counter)
  2738. return false;
  2739. spin_lock_irq(&dev->event_lock);
  2740. pending = to_intel_crtc(crtc)->flip_work != NULL;
  2741. spin_unlock_irq(&dev->event_lock);
  2742. return pending;
  2743. }
  2744. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2745. struct intel_crtc_state *old_crtc_state)
  2746. {
  2747. struct drm_device *dev = crtc->base.dev;
  2748. struct drm_i915_private *dev_priv = to_i915(dev);
  2749. struct intel_crtc_state *pipe_config =
  2750. to_intel_crtc_state(crtc->base.state);
  2751. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2752. crtc->base.mode = crtc->base.state->mode;
  2753. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2754. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2755. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2756. /*
  2757. * Update pipe size and adjust fitter if needed: the reason for this is
  2758. * that in compute_mode_changes we check the native mode (not the pfit
  2759. * mode) to see if we can flip rather than do a full mode set. In the
  2760. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2761. * pfit state, we'll end up with a big fb scanned out into the wrong
  2762. * sized surface.
  2763. */
  2764. I915_WRITE(PIPESRC(crtc->pipe),
  2765. ((pipe_config->pipe_src_w - 1) << 16) |
  2766. (pipe_config->pipe_src_h - 1));
  2767. /* on skylake this is done by detaching scalers */
  2768. if (INTEL_INFO(dev)->gen >= 9) {
  2769. skl_detach_scalers(crtc);
  2770. if (pipe_config->pch_pfit.enabled)
  2771. skylake_pfit_enable(crtc);
  2772. } else if (HAS_PCH_SPLIT(dev)) {
  2773. if (pipe_config->pch_pfit.enabled)
  2774. ironlake_pfit_enable(crtc);
  2775. else if (old_crtc_state->pch_pfit.enabled)
  2776. ironlake_pfit_disable(crtc, true);
  2777. }
  2778. }
  2779. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2780. {
  2781. struct drm_device *dev = crtc->dev;
  2782. struct drm_i915_private *dev_priv = to_i915(dev);
  2783. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2784. int pipe = intel_crtc->pipe;
  2785. i915_reg_t reg;
  2786. u32 temp;
  2787. /* enable normal train */
  2788. reg = FDI_TX_CTL(pipe);
  2789. temp = I915_READ(reg);
  2790. if (IS_IVYBRIDGE(dev)) {
  2791. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2792. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2793. } else {
  2794. temp &= ~FDI_LINK_TRAIN_NONE;
  2795. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2796. }
  2797. I915_WRITE(reg, temp);
  2798. reg = FDI_RX_CTL(pipe);
  2799. temp = I915_READ(reg);
  2800. if (HAS_PCH_CPT(dev)) {
  2801. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2802. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2803. } else {
  2804. temp &= ~FDI_LINK_TRAIN_NONE;
  2805. temp |= FDI_LINK_TRAIN_NONE;
  2806. }
  2807. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2808. /* wait one idle pattern time */
  2809. POSTING_READ(reg);
  2810. udelay(1000);
  2811. /* IVB wants error correction enabled */
  2812. if (IS_IVYBRIDGE(dev))
  2813. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2814. FDI_FE_ERRC_ENABLE);
  2815. }
  2816. /* The FDI link training functions for ILK/Ibexpeak. */
  2817. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2818. {
  2819. struct drm_device *dev = crtc->dev;
  2820. struct drm_i915_private *dev_priv = to_i915(dev);
  2821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2822. int pipe = intel_crtc->pipe;
  2823. i915_reg_t reg;
  2824. u32 temp, tries;
  2825. /* FDI needs bits from pipe first */
  2826. assert_pipe_enabled(dev_priv, pipe);
  2827. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2828. for train result */
  2829. reg = FDI_RX_IMR(pipe);
  2830. temp = I915_READ(reg);
  2831. temp &= ~FDI_RX_SYMBOL_LOCK;
  2832. temp &= ~FDI_RX_BIT_LOCK;
  2833. I915_WRITE(reg, temp);
  2834. I915_READ(reg);
  2835. udelay(150);
  2836. /* enable CPU FDI TX and PCH FDI RX */
  2837. reg = FDI_TX_CTL(pipe);
  2838. temp = I915_READ(reg);
  2839. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2840. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2841. temp &= ~FDI_LINK_TRAIN_NONE;
  2842. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2843. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2844. reg = FDI_RX_CTL(pipe);
  2845. temp = I915_READ(reg);
  2846. temp &= ~FDI_LINK_TRAIN_NONE;
  2847. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2848. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2849. POSTING_READ(reg);
  2850. udelay(150);
  2851. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2852. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2853. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2854. FDI_RX_PHASE_SYNC_POINTER_EN);
  2855. reg = FDI_RX_IIR(pipe);
  2856. for (tries = 0; tries < 5; tries++) {
  2857. temp = I915_READ(reg);
  2858. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2859. if ((temp & FDI_RX_BIT_LOCK)) {
  2860. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2861. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2862. break;
  2863. }
  2864. }
  2865. if (tries == 5)
  2866. DRM_ERROR("FDI train 1 fail!\n");
  2867. /* Train 2 */
  2868. reg = FDI_TX_CTL(pipe);
  2869. temp = I915_READ(reg);
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2872. I915_WRITE(reg, temp);
  2873. reg = FDI_RX_CTL(pipe);
  2874. temp = I915_READ(reg);
  2875. temp &= ~FDI_LINK_TRAIN_NONE;
  2876. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2877. I915_WRITE(reg, temp);
  2878. POSTING_READ(reg);
  2879. udelay(150);
  2880. reg = FDI_RX_IIR(pipe);
  2881. for (tries = 0; tries < 5; tries++) {
  2882. temp = I915_READ(reg);
  2883. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2884. if (temp & FDI_RX_SYMBOL_LOCK) {
  2885. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2886. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2887. break;
  2888. }
  2889. }
  2890. if (tries == 5)
  2891. DRM_ERROR("FDI train 2 fail!\n");
  2892. DRM_DEBUG_KMS("FDI train done\n");
  2893. }
  2894. static const int snb_b_fdi_train_param[] = {
  2895. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2896. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2897. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2898. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2899. };
  2900. /* The FDI link training functions for SNB/Cougarpoint. */
  2901. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2902. {
  2903. struct drm_device *dev = crtc->dev;
  2904. struct drm_i915_private *dev_priv = to_i915(dev);
  2905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2906. int pipe = intel_crtc->pipe;
  2907. i915_reg_t reg;
  2908. u32 temp, i, retry;
  2909. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2910. for train result */
  2911. reg = FDI_RX_IMR(pipe);
  2912. temp = I915_READ(reg);
  2913. temp &= ~FDI_RX_SYMBOL_LOCK;
  2914. temp &= ~FDI_RX_BIT_LOCK;
  2915. I915_WRITE(reg, temp);
  2916. POSTING_READ(reg);
  2917. udelay(150);
  2918. /* enable CPU FDI TX and PCH FDI RX */
  2919. reg = FDI_TX_CTL(pipe);
  2920. temp = I915_READ(reg);
  2921. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2922. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2923. temp &= ~FDI_LINK_TRAIN_NONE;
  2924. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2925. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2926. /* SNB-B */
  2927. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2928. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2929. I915_WRITE(FDI_RX_MISC(pipe),
  2930. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2931. reg = FDI_RX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. if (HAS_PCH_CPT(dev)) {
  2934. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2935. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2936. } else {
  2937. temp &= ~FDI_LINK_TRAIN_NONE;
  2938. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2939. }
  2940. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2941. POSTING_READ(reg);
  2942. udelay(150);
  2943. for (i = 0; i < 4; i++) {
  2944. reg = FDI_TX_CTL(pipe);
  2945. temp = I915_READ(reg);
  2946. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2947. temp |= snb_b_fdi_train_param[i];
  2948. I915_WRITE(reg, temp);
  2949. POSTING_READ(reg);
  2950. udelay(500);
  2951. for (retry = 0; retry < 5; retry++) {
  2952. reg = FDI_RX_IIR(pipe);
  2953. temp = I915_READ(reg);
  2954. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2955. if (temp & FDI_RX_BIT_LOCK) {
  2956. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2957. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2958. break;
  2959. }
  2960. udelay(50);
  2961. }
  2962. if (retry < 5)
  2963. break;
  2964. }
  2965. if (i == 4)
  2966. DRM_ERROR("FDI train 1 fail!\n");
  2967. /* Train 2 */
  2968. reg = FDI_TX_CTL(pipe);
  2969. temp = I915_READ(reg);
  2970. temp &= ~FDI_LINK_TRAIN_NONE;
  2971. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2972. if (IS_GEN6(dev)) {
  2973. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2974. /* SNB-B */
  2975. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2976. }
  2977. I915_WRITE(reg, temp);
  2978. reg = FDI_RX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. if (HAS_PCH_CPT(dev)) {
  2981. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2982. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2983. } else {
  2984. temp &= ~FDI_LINK_TRAIN_NONE;
  2985. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2986. }
  2987. I915_WRITE(reg, temp);
  2988. POSTING_READ(reg);
  2989. udelay(150);
  2990. for (i = 0; i < 4; i++) {
  2991. reg = FDI_TX_CTL(pipe);
  2992. temp = I915_READ(reg);
  2993. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2994. temp |= snb_b_fdi_train_param[i];
  2995. I915_WRITE(reg, temp);
  2996. POSTING_READ(reg);
  2997. udelay(500);
  2998. for (retry = 0; retry < 5; retry++) {
  2999. reg = FDI_RX_IIR(pipe);
  3000. temp = I915_READ(reg);
  3001. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3002. if (temp & FDI_RX_SYMBOL_LOCK) {
  3003. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3004. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3005. break;
  3006. }
  3007. udelay(50);
  3008. }
  3009. if (retry < 5)
  3010. break;
  3011. }
  3012. if (i == 4)
  3013. DRM_ERROR("FDI train 2 fail!\n");
  3014. DRM_DEBUG_KMS("FDI train done.\n");
  3015. }
  3016. /* Manual link training for Ivy Bridge A0 parts */
  3017. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3018. {
  3019. struct drm_device *dev = crtc->dev;
  3020. struct drm_i915_private *dev_priv = to_i915(dev);
  3021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3022. int pipe = intel_crtc->pipe;
  3023. i915_reg_t reg;
  3024. u32 temp, i, j;
  3025. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3026. for train result */
  3027. reg = FDI_RX_IMR(pipe);
  3028. temp = I915_READ(reg);
  3029. temp &= ~FDI_RX_SYMBOL_LOCK;
  3030. temp &= ~FDI_RX_BIT_LOCK;
  3031. I915_WRITE(reg, temp);
  3032. POSTING_READ(reg);
  3033. udelay(150);
  3034. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3035. I915_READ(FDI_RX_IIR(pipe)));
  3036. /* Try each vswing and preemphasis setting twice before moving on */
  3037. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3038. /* disable first in case we need to retry */
  3039. reg = FDI_TX_CTL(pipe);
  3040. temp = I915_READ(reg);
  3041. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3042. temp &= ~FDI_TX_ENABLE;
  3043. I915_WRITE(reg, temp);
  3044. reg = FDI_RX_CTL(pipe);
  3045. temp = I915_READ(reg);
  3046. temp &= ~FDI_LINK_TRAIN_AUTO;
  3047. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3048. temp &= ~FDI_RX_ENABLE;
  3049. I915_WRITE(reg, temp);
  3050. /* enable CPU FDI TX and PCH FDI RX */
  3051. reg = FDI_TX_CTL(pipe);
  3052. temp = I915_READ(reg);
  3053. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3054. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3055. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3056. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3057. temp |= snb_b_fdi_train_param[j/2];
  3058. temp |= FDI_COMPOSITE_SYNC;
  3059. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3060. I915_WRITE(FDI_RX_MISC(pipe),
  3061. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3062. reg = FDI_RX_CTL(pipe);
  3063. temp = I915_READ(reg);
  3064. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3065. temp |= FDI_COMPOSITE_SYNC;
  3066. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3067. POSTING_READ(reg);
  3068. udelay(1); /* should be 0.5us */
  3069. for (i = 0; i < 4; i++) {
  3070. reg = FDI_RX_IIR(pipe);
  3071. temp = I915_READ(reg);
  3072. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3073. if (temp & FDI_RX_BIT_LOCK ||
  3074. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3075. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3076. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3077. i);
  3078. break;
  3079. }
  3080. udelay(1); /* should be 0.5us */
  3081. }
  3082. if (i == 4) {
  3083. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3084. continue;
  3085. }
  3086. /* Train 2 */
  3087. reg = FDI_TX_CTL(pipe);
  3088. temp = I915_READ(reg);
  3089. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3090. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3091. I915_WRITE(reg, temp);
  3092. reg = FDI_RX_CTL(pipe);
  3093. temp = I915_READ(reg);
  3094. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3095. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3096. I915_WRITE(reg, temp);
  3097. POSTING_READ(reg);
  3098. udelay(2); /* should be 1.5us */
  3099. for (i = 0; i < 4; i++) {
  3100. reg = FDI_RX_IIR(pipe);
  3101. temp = I915_READ(reg);
  3102. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3103. if (temp & FDI_RX_SYMBOL_LOCK ||
  3104. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3105. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3106. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3107. i);
  3108. goto train_done;
  3109. }
  3110. udelay(2); /* should be 1.5us */
  3111. }
  3112. if (i == 4)
  3113. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3114. }
  3115. train_done:
  3116. DRM_DEBUG_KMS("FDI train done.\n");
  3117. }
  3118. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3119. {
  3120. struct drm_device *dev = intel_crtc->base.dev;
  3121. struct drm_i915_private *dev_priv = to_i915(dev);
  3122. int pipe = intel_crtc->pipe;
  3123. i915_reg_t reg;
  3124. u32 temp;
  3125. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3126. reg = FDI_RX_CTL(pipe);
  3127. temp = I915_READ(reg);
  3128. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3129. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3130. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3131. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3132. POSTING_READ(reg);
  3133. udelay(200);
  3134. /* Switch from Rawclk to PCDclk */
  3135. temp = I915_READ(reg);
  3136. I915_WRITE(reg, temp | FDI_PCDCLK);
  3137. POSTING_READ(reg);
  3138. udelay(200);
  3139. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3140. reg = FDI_TX_CTL(pipe);
  3141. temp = I915_READ(reg);
  3142. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3143. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3144. POSTING_READ(reg);
  3145. udelay(100);
  3146. }
  3147. }
  3148. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3149. {
  3150. struct drm_device *dev = intel_crtc->base.dev;
  3151. struct drm_i915_private *dev_priv = to_i915(dev);
  3152. int pipe = intel_crtc->pipe;
  3153. i915_reg_t reg;
  3154. u32 temp;
  3155. /* Switch from PCDclk to Rawclk */
  3156. reg = FDI_RX_CTL(pipe);
  3157. temp = I915_READ(reg);
  3158. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3159. /* Disable CPU FDI TX PLL */
  3160. reg = FDI_TX_CTL(pipe);
  3161. temp = I915_READ(reg);
  3162. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3163. POSTING_READ(reg);
  3164. udelay(100);
  3165. reg = FDI_RX_CTL(pipe);
  3166. temp = I915_READ(reg);
  3167. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3168. /* Wait for the clocks to turn off. */
  3169. POSTING_READ(reg);
  3170. udelay(100);
  3171. }
  3172. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3173. {
  3174. struct drm_device *dev = crtc->dev;
  3175. struct drm_i915_private *dev_priv = to_i915(dev);
  3176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3177. int pipe = intel_crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp;
  3180. /* disable CPU FDI tx and PCH FDI rx */
  3181. reg = FDI_TX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3184. POSTING_READ(reg);
  3185. reg = FDI_RX_CTL(pipe);
  3186. temp = I915_READ(reg);
  3187. temp &= ~(0x7 << 16);
  3188. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3189. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3190. POSTING_READ(reg);
  3191. udelay(100);
  3192. /* Ironlake workaround, disable clock pointer after downing FDI */
  3193. if (HAS_PCH_IBX(dev))
  3194. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3195. /* still set train pattern 1 */
  3196. reg = FDI_TX_CTL(pipe);
  3197. temp = I915_READ(reg);
  3198. temp &= ~FDI_LINK_TRAIN_NONE;
  3199. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3200. I915_WRITE(reg, temp);
  3201. reg = FDI_RX_CTL(pipe);
  3202. temp = I915_READ(reg);
  3203. if (HAS_PCH_CPT(dev)) {
  3204. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3205. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3206. } else {
  3207. temp &= ~FDI_LINK_TRAIN_NONE;
  3208. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3209. }
  3210. /* BPC in FDI rx is consistent with that in PIPECONF */
  3211. temp &= ~(0x07 << 16);
  3212. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3213. I915_WRITE(reg, temp);
  3214. POSTING_READ(reg);
  3215. udelay(100);
  3216. }
  3217. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3218. {
  3219. struct intel_crtc *crtc;
  3220. /* Note that we don't need to be called with mode_config.lock here
  3221. * as our list of CRTC objects is static for the lifetime of the
  3222. * device and so cannot disappear as we iterate. Similarly, we can
  3223. * happily treat the predicates as racy, atomic checks as userspace
  3224. * cannot claim and pin a new fb without at least acquring the
  3225. * struct_mutex and so serialising with us.
  3226. */
  3227. for_each_intel_crtc(dev, crtc) {
  3228. if (atomic_read(&crtc->unpin_work_count) == 0)
  3229. continue;
  3230. if (crtc->flip_work)
  3231. intel_wait_for_vblank(dev, crtc->pipe);
  3232. return true;
  3233. }
  3234. return false;
  3235. }
  3236. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3237. {
  3238. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3239. struct intel_flip_work *work = intel_crtc->flip_work;
  3240. intel_crtc->flip_work = NULL;
  3241. if (work->event)
  3242. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3243. drm_crtc_vblank_put(&intel_crtc->base);
  3244. wake_up_all(&dev_priv->pending_flip_queue);
  3245. queue_work(dev_priv->wq, &work->unpin_work);
  3246. trace_i915_flip_complete(intel_crtc->plane,
  3247. work->pending_flip_obj);
  3248. }
  3249. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3250. {
  3251. struct drm_device *dev = crtc->dev;
  3252. struct drm_i915_private *dev_priv = to_i915(dev);
  3253. long ret;
  3254. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3255. ret = wait_event_interruptible_timeout(
  3256. dev_priv->pending_flip_queue,
  3257. !intel_crtc_has_pending_flip(crtc),
  3258. 60*HZ);
  3259. if (ret < 0)
  3260. return ret;
  3261. if (ret == 0) {
  3262. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3263. struct intel_flip_work *work;
  3264. spin_lock_irq(&dev->event_lock);
  3265. work = intel_crtc->flip_work;
  3266. if (work && !is_mmio_work(work)) {
  3267. WARN_ONCE(1, "Removing stuck page flip\n");
  3268. page_flip_completed(intel_crtc);
  3269. }
  3270. spin_unlock_irq(&dev->event_lock);
  3271. }
  3272. return 0;
  3273. }
  3274. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3275. {
  3276. u32 temp;
  3277. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3278. mutex_lock(&dev_priv->sb_lock);
  3279. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3280. temp |= SBI_SSCCTL_DISABLE;
  3281. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3282. mutex_unlock(&dev_priv->sb_lock);
  3283. }
  3284. /* Program iCLKIP clock to the desired frequency */
  3285. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3286. {
  3287. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3288. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3289. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3290. u32 temp;
  3291. lpt_disable_iclkip(dev_priv);
  3292. /* The iCLK virtual clock root frequency is in MHz,
  3293. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3294. * divisors, it is necessary to divide one by another, so we
  3295. * convert the virtual clock precision to KHz here for higher
  3296. * precision.
  3297. */
  3298. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3299. u32 iclk_virtual_root_freq = 172800 * 1000;
  3300. u32 iclk_pi_range = 64;
  3301. u32 desired_divisor;
  3302. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3303. clock << auxdiv);
  3304. divsel = (desired_divisor / iclk_pi_range) - 2;
  3305. phaseinc = desired_divisor % iclk_pi_range;
  3306. /*
  3307. * Near 20MHz is a corner case which is
  3308. * out of range for the 7-bit divisor
  3309. */
  3310. if (divsel <= 0x7f)
  3311. break;
  3312. }
  3313. /* This should not happen with any sane values */
  3314. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3315. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3316. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3317. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3318. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3319. clock,
  3320. auxdiv,
  3321. divsel,
  3322. phasedir,
  3323. phaseinc);
  3324. mutex_lock(&dev_priv->sb_lock);
  3325. /* Program SSCDIVINTPHASE6 */
  3326. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3327. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3328. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3329. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3330. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3331. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3332. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3333. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3334. /* Program SSCAUXDIV */
  3335. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3336. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3337. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3338. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3339. /* Enable modulator and associated divider */
  3340. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3341. temp &= ~SBI_SSCCTL_DISABLE;
  3342. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3343. mutex_unlock(&dev_priv->sb_lock);
  3344. /* Wait for initialization time */
  3345. udelay(24);
  3346. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3347. }
  3348. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3349. {
  3350. u32 divsel, phaseinc, auxdiv;
  3351. u32 iclk_virtual_root_freq = 172800 * 1000;
  3352. u32 iclk_pi_range = 64;
  3353. u32 desired_divisor;
  3354. u32 temp;
  3355. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3356. return 0;
  3357. mutex_lock(&dev_priv->sb_lock);
  3358. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3359. if (temp & SBI_SSCCTL_DISABLE) {
  3360. mutex_unlock(&dev_priv->sb_lock);
  3361. return 0;
  3362. }
  3363. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3364. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3365. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3366. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3367. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3368. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3369. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3370. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3371. mutex_unlock(&dev_priv->sb_lock);
  3372. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3373. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3374. desired_divisor << auxdiv);
  3375. }
  3376. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3377. enum pipe pch_transcoder)
  3378. {
  3379. struct drm_device *dev = crtc->base.dev;
  3380. struct drm_i915_private *dev_priv = to_i915(dev);
  3381. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3382. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3383. I915_READ(HTOTAL(cpu_transcoder)));
  3384. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3385. I915_READ(HBLANK(cpu_transcoder)));
  3386. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3387. I915_READ(HSYNC(cpu_transcoder)));
  3388. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3389. I915_READ(VTOTAL(cpu_transcoder)));
  3390. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3391. I915_READ(VBLANK(cpu_transcoder)));
  3392. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3393. I915_READ(VSYNC(cpu_transcoder)));
  3394. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3395. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3396. }
  3397. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3398. {
  3399. struct drm_i915_private *dev_priv = to_i915(dev);
  3400. uint32_t temp;
  3401. temp = I915_READ(SOUTH_CHICKEN1);
  3402. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3403. return;
  3404. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3405. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3406. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3407. if (enable)
  3408. temp |= FDI_BC_BIFURCATION_SELECT;
  3409. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3410. I915_WRITE(SOUTH_CHICKEN1, temp);
  3411. POSTING_READ(SOUTH_CHICKEN1);
  3412. }
  3413. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3414. {
  3415. struct drm_device *dev = intel_crtc->base.dev;
  3416. switch (intel_crtc->pipe) {
  3417. case PIPE_A:
  3418. break;
  3419. case PIPE_B:
  3420. if (intel_crtc->config->fdi_lanes > 2)
  3421. cpt_set_fdi_bc_bifurcation(dev, false);
  3422. else
  3423. cpt_set_fdi_bc_bifurcation(dev, true);
  3424. break;
  3425. case PIPE_C:
  3426. cpt_set_fdi_bc_bifurcation(dev, true);
  3427. break;
  3428. default:
  3429. BUG();
  3430. }
  3431. }
  3432. /* Return which DP Port should be selected for Transcoder DP control */
  3433. static enum port
  3434. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3435. {
  3436. struct drm_device *dev = crtc->dev;
  3437. struct intel_encoder *encoder;
  3438. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3439. if (encoder->type == INTEL_OUTPUT_DP ||
  3440. encoder->type == INTEL_OUTPUT_EDP)
  3441. return enc_to_dig_port(&encoder->base)->port;
  3442. }
  3443. return -1;
  3444. }
  3445. /*
  3446. * Enable PCH resources required for PCH ports:
  3447. * - PCH PLLs
  3448. * - FDI training & RX/TX
  3449. * - update transcoder timings
  3450. * - DP transcoding bits
  3451. * - transcoder
  3452. */
  3453. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3454. {
  3455. struct drm_device *dev = crtc->dev;
  3456. struct drm_i915_private *dev_priv = to_i915(dev);
  3457. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3458. int pipe = intel_crtc->pipe;
  3459. u32 temp;
  3460. assert_pch_transcoder_disabled(dev_priv, pipe);
  3461. if (IS_IVYBRIDGE(dev))
  3462. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3463. /* Write the TU size bits before fdi link training, so that error
  3464. * detection works. */
  3465. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3466. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3467. /* For PCH output, training FDI link */
  3468. dev_priv->display.fdi_link_train(crtc);
  3469. /* We need to program the right clock selection before writing the pixel
  3470. * mutliplier into the DPLL. */
  3471. if (HAS_PCH_CPT(dev)) {
  3472. u32 sel;
  3473. temp = I915_READ(PCH_DPLL_SEL);
  3474. temp |= TRANS_DPLL_ENABLE(pipe);
  3475. sel = TRANS_DPLLB_SEL(pipe);
  3476. if (intel_crtc->config->shared_dpll ==
  3477. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3478. temp |= sel;
  3479. else
  3480. temp &= ~sel;
  3481. I915_WRITE(PCH_DPLL_SEL, temp);
  3482. }
  3483. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3484. * transcoder, and we actually should do this to not upset any PCH
  3485. * transcoder that already use the clock when we share it.
  3486. *
  3487. * Note that enable_shared_dpll tries to do the right thing, but
  3488. * get_shared_dpll unconditionally resets the pll - we need that to have
  3489. * the right LVDS enable sequence. */
  3490. intel_enable_shared_dpll(intel_crtc);
  3491. /* set transcoder timing, panel must allow it */
  3492. assert_panel_unlocked(dev_priv, pipe);
  3493. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3494. intel_fdi_normal_train(crtc);
  3495. /* For PCH DP, enable TRANS_DP_CTL */
  3496. if (HAS_PCH_CPT(dev) && intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3497. const struct drm_display_mode *adjusted_mode =
  3498. &intel_crtc->config->base.adjusted_mode;
  3499. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3500. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3501. temp = I915_READ(reg);
  3502. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3503. TRANS_DP_SYNC_MASK |
  3504. TRANS_DP_BPC_MASK);
  3505. temp |= TRANS_DP_OUTPUT_ENABLE;
  3506. temp |= bpc << 9; /* same format but at 11:9 */
  3507. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3508. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3509. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3510. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3511. switch (intel_trans_dp_port_sel(crtc)) {
  3512. case PORT_B:
  3513. temp |= TRANS_DP_PORT_SEL_B;
  3514. break;
  3515. case PORT_C:
  3516. temp |= TRANS_DP_PORT_SEL_C;
  3517. break;
  3518. case PORT_D:
  3519. temp |= TRANS_DP_PORT_SEL_D;
  3520. break;
  3521. default:
  3522. BUG();
  3523. }
  3524. I915_WRITE(reg, temp);
  3525. }
  3526. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3527. }
  3528. static void lpt_pch_enable(struct drm_crtc *crtc)
  3529. {
  3530. struct drm_device *dev = crtc->dev;
  3531. struct drm_i915_private *dev_priv = to_i915(dev);
  3532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3533. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3534. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3535. lpt_program_iclkip(crtc);
  3536. /* Set transcoder timing. */
  3537. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3538. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3539. }
  3540. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3541. {
  3542. struct drm_i915_private *dev_priv = to_i915(dev);
  3543. i915_reg_t dslreg = PIPEDSL(pipe);
  3544. u32 temp;
  3545. temp = I915_READ(dslreg);
  3546. udelay(500);
  3547. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3548. if (wait_for(I915_READ(dslreg) != temp, 5))
  3549. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3550. }
  3551. }
  3552. static int
  3553. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3554. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3555. int src_w, int src_h, int dst_w, int dst_h)
  3556. {
  3557. struct intel_crtc_scaler_state *scaler_state =
  3558. &crtc_state->scaler_state;
  3559. struct intel_crtc *intel_crtc =
  3560. to_intel_crtc(crtc_state->base.crtc);
  3561. int need_scaling;
  3562. need_scaling = intel_rotation_90_or_270(rotation) ?
  3563. (src_h != dst_w || src_w != dst_h):
  3564. (src_w != dst_w || src_h != dst_h);
  3565. /*
  3566. * if plane is being disabled or scaler is no more required or force detach
  3567. * - free scaler binded to this plane/crtc
  3568. * - in order to do this, update crtc->scaler_usage
  3569. *
  3570. * Here scaler state in crtc_state is set free so that
  3571. * scaler can be assigned to other user. Actual register
  3572. * update to free the scaler is done in plane/panel-fit programming.
  3573. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3574. */
  3575. if (force_detach || !need_scaling) {
  3576. if (*scaler_id >= 0) {
  3577. scaler_state->scaler_users &= ~(1 << scaler_user);
  3578. scaler_state->scalers[*scaler_id].in_use = 0;
  3579. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3580. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3581. intel_crtc->pipe, scaler_user, *scaler_id,
  3582. scaler_state->scaler_users);
  3583. *scaler_id = -1;
  3584. }
  3585. return 0;
  3586. }
  3587. /* range checks */
  3588. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3589. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3590. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3591. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3592. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3593. "size is out of scaler range\n",
  3594. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3595. return -EINVAL;
  3596. }
  3597. /* mark this plane as a scaler user in crtc_state */
  3598. scaler_state->scaler_users |= (1 << scaler_user);
  3599. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3600. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3601. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3602. scaler_state->scaler_users);
  3603. return 0;
  3604. }
  3605. /**
  3606. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3607. *
  3608. * @state: crtc's scaler state
  3609. *
  3610. * Return
  3611. * 0 - scaler_usage updated successfully
  3612. * error - requested scaling cannot be supported or other error condition
  3613. */
  3614. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3615. {
  3616. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3617. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3618. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  3619. intel_crtc->base.base.id, intel_crtc->base.name,
  3620. intel_crtc->pipe, SKL_CRTC_INDEX);
  3621. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3622. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3623. state->pipe_src_w, state->pipe_src_h,
  3624. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3625. }
  3626. /**
  3627. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3628. *
  3629. * @state: crtc's scaler state
  3630. * @plane_state: atomic plane state to update
  3631. *
  3632. * Return
  3633. * 0 - scaler_usage updated successfully
  3634. * error - requested scaling cannot be supported or other error condition
  3635. */
  3636. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3637. struct intel_plane_state *plane_state)
  3638. {
  3639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3640. struct intel_plane *intel_plane =
  3641. to_intel_plane(plane_state->base.plane);
  3642. struct drm_framebuffer *fb = plane_state->base.fb;
  3643. int ret;
  3644. bool force_detach = !fb || !plane_state->visible;
  3645. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  3646. intel_plane->base.base.id, intel_plane->base.name,
  3647. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  3648. ret = skl_update_scaler(crtc_state, force_detach,
  3649. drm_plane_index(&intel_plane->base),
  3650. &plane_state->scaler_id,
  3651. plane_state->base.rotation,
  3652. drm_rect_width(&plane_state->src) >> 16,
  3653. drm_rect_height(&plane_state->src) >> 16,
  3654. drm_rect_width(&plane_state->dst),
  3655. drm_rect_height(&plane_state->dst));
  3656. if (ret || plane_state->scaler_id < 0)
  3657. return ret;
  3658. /* check colorkey */
  3659. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3660. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  3661. intel_plane->base.base.id,
  3662. intel_plane->base.name);
  3663. return -EINVAL;
  3664. }
  3665. /* Check src format */
  3666. switch (fb->pixel_format) {
  3667. case DRM_FORMAT_RGB565:
  3668. case DRM_FORMAT_XBGR8888:
  3669. case DRM_FORMAT_XRGB8888:
  3670. case DRM_FORMAT_ABGR8888:
  3671. case DRM_FORMAT_ARGB8888:
  3672. case DRM_FORMAT_XRGB2101010:
  3673. case DRM_FORMAT_XBGR2101010:
  3674. case DRM_FORMAT_YUYV:
  3675. case DRM_FORMAT_YVYU:
  3676. case DRM_FORMAT_UYVY:
  3677. case DRM_FORMAT_VYUY:
  3678. break;
  3679. default:
  3680. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  3681. intel_plane->base.base.id, intel_plane->base.name,
  3682. fb->base.id, fb->pixel_format);
  3683. return -EINVAL;
  3684. }
  3685. return 0;
  3686. }
  3687. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3688. {
  3689. int i;
  3690. for (i = 0; i < crtc->num_scalers; i++)
  3691. skl_detach_scaler(crtc, i);
  3692. }
  3693. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3694. {
  3695. struct drm_device *dev = crtc->base.dev;
  3696. struct drm_i915_private *dev_priv = to_i915(dev);
  3697. int pipe = crtc->pipe;
  3698. struct intel_crtc_scaler_state *scaler_state =
  3699. &crtc->config->scaler_state;
  3700. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3701. if (crtc->config->pch_pfit.enabled) {
  3702. int id;
  3703. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3704. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3705. return;
  3706. }
  3707. id = scaler_state->scaler_id;
  3708. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3709. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3710. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3711. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3712. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3713. }
  3714. }
  3715. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3716. {
  3717. struct drm_device *dev = crtc->base.dev;
  3718. struct drm_i915_private *dev_priv = to_i915(dev);
  3719. int pipe = crtc->pipe;
  3720. if (crtc->config->pch_pfit.enabled) {
  3721. /* Force use of hard-coded filter coefficients
  3722. * as some pre-programmed values are broken,
  3723. * e.g. x201.
  3724. */
  3725. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3726. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3727. PF_PIPE_SEL_IVB(pipe));
  3728. else
  3729. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3730. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3731. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3732. }
  3733. }
  3734. void hsw_enable_ips(struct intel_crtc *crtc)
  3735. {
  3736. struct drm_device *dev = crtc->base.dev;
  3737. struct drm_i915_private *dev_priv = to_i915(dev);
  3738. if (!crtc->config->ips_enabled)
  3739. return;
  3740. /*
  3741. * We can only enable IPS after we enable a plane and wait for a vblank
  3742. * This function is called from post_plane_update, which is run after
  3743. * a vblank wait.
  3744. */
  3745. assert_plane_enabled(dev_priv, crtc->plane);
  3746. if (IS_BROADWELL(dev)) {
  3747. mutex_lock(&dev_priv->rps.hw_lock);
  3748. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3749. mutex_unlock(&dev_priv->rps.hw_lock);
  3750. /* Quoting Art Runyan: "its not safe to expect any particular
  3751. * value in IPS_CTL bit 31 after enabling IPS through the
  3752. * mailbox." Moreover, the mailbox may return a bogus state,
  3753. * so we need to just enable it and continue on.
  3754. */
  3755. } else {
  3756. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3757. /* The bit only becomes 1 in the next vblank, so this wait here
  3758. * is essentially intel_wait_for_vblank. If we don't have this
  3759. * and don't wait for vblanks until the end of crtc_enable, then
  3760. * the HW state readout code will complain that the expected
  3761. * IPS_CTL value is not the one we read. */
  3762. if (intel_wait_for_register(dev_priv,
  3763. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  3764. 50))
  3765. DRM_ERROR("Timed out waiting for IPS enable\n");
  3766. }
  3767. }
  3768. void hsw_disable_ips(struct intel_crtc *crtc)
  3769. {
  3770. struct drm_device *dev = crtc->base.dev;
  3771. struct drm_i915_private *dev_priv = to_i915(dev);
  3772. if (!crtc->config->ips_enabled)
  3773. return;
  3774. assert_plane_enabled(dev_priv, crtc->plane);
  3775. if (IS_BROADWELL(dev)) {
  3776. mutex_lock(&dev_priv->rps.hw_lock);
  3777. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3778. mutex_unlock(&dev_priv->rps.hw_lock);
  3779. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3780. if (intel_wait_for_register(dev_priv,
  3781. IPS_CTL, IPS_ENABLE, 0,
  3782. 42))
  3783. DRM_ERROR("Timed out waiting for IPS disable\n");
  3784. } else {
  3785. I915_WRITE(IPS_CTL, 0);
  3786. POSTING_READ(IPS_CTL);
  3787. }
  3788. /* We need to wait for a vblank before we can disable the plane. */
  3789. intel_wait_for_vblank(dev, crtc->pipe);
  3790. }
  3791. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3792. {
  3793. if (intel_crtc->overlay) {
  3794. struct drm_device *dev = intel_crtc->base.dev;
  3795. struct drm_i915_private *dev_priv = to_i915(dev);
  3796. mutex_lock(&dev->struct_mutex);
  3797. dev_priv->mm.interruptible = false;
  3798. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3799. dev_priv->mm.interruptible = true;
  3800. mutex_unlock(&dev->struct_mutex);
  3801. }
  3802. /* Let userspace switch the overlay on again. In most cases userspace
  3803. * has to recompute where to put it anyway.
  3804. */
  3805. }
  3806. /**
  3807. * intel_post_enable_primary - Perform operations after enabling primary plane
  3808. * @crtc: the CRTC whose primary plane was just enabled
  3809. *
  3810. * Performs potentially sleeping operations that must be done after the primary
  3811. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3812. * called due to an explicit primary plane update, or due to an implicit
  3813. * re-enable that is caused when a sprite plane is updated to no longer
  3814. * completely hide the primary plane.
  3815. */
  3816. static void
  3817. intel_post_enable_primary(struct drm_crtc *crtc)
  3818. {
  3819. struct drm_device *dev = crtc->dev;
  3820. struct drm_i915_private *dev_priv = to_i915(dev);
  3821. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3822. int pipe = intel_crtc->pipe;
  3823. /*
  3824. * FIXME IPS should be fine as long as one plane is
  3825. * enabled, but in practice it seems to have problems
  3826. * when going from primary only to sprite only and vice
  3827. * versa.
  3828. */
  3829. hsw_enable_ips(intel_crtc);
  3830. /*
  3831. * Gen2 reports pipe underruns whenever all planes are disabled.
  3832. * So don't enable underrun reporting before at least some planes
  3833. * are enabled.
  3834. * FIXME: Need to fix the logic to work when we turn off all planes
  3835. * but leave the pipe running.
  3836. */
  3837. if (IS_GEN2(dev))
  3838. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3839. /* Underruns don't always raise interrupts, so check manually. */
  3840. intel_check_cpu_fifo_underruns(dev_priv);
  3841. intel_check_pch_fifo_underruns(dev_priv);
  3842. }
  3843. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3844. static void
  3845. intel_pre_disable_primary(struct drm_crtc *crtc)
  3846. {
  3847. struct drm_device *dev = crtc->dev;
  3848. struct drm_i915_private *dev_priv = to_i915(dev);
  3849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3850. int pipe = intel_crtc->pipe;
  3851. /*
  3852. * Gen2 reports pipe underruns whenever all planes are disabled.
  3853. * So diasble underrun reporting before all the planes get disabled.
  3854. * FIXME: Need to fix the logic to work when we turn off all planes
  3855. * but leave the pipe running.
  3856. */
  3857. if (IS_GEN2(dev))
  3858. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3859. /*
  3860. * FIXME IPS should be fine as long as one plane is
  3861. * enabled, but in practice it seems to have problems
  3862. * when going from primary only to sprite only and vice
  3863. * versa.
  3864. */
  3865. hsw_disable_ips(intel_crtc);
  3866. }
  3867. /* FIXME get rid of this and use pre_plane_update */
  3868. static void
  3869. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3870. {
  3871. struct drm_device *dev = crtc->dev;
  3872. struct drm_i915_private *dev_priv = to_i915(dev);
  3873. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3874. int pipe = intel_crtc->pipe;
  3875. intel_pre_disable_primary(crtc);
  3876. /*
  3877. * Vblank time updates from the shadow to live plane control register
  3878. * are blocked if the memory self-refresh mode is active at that
  3879. * moment. So to make sure the plane gets truly disabled, disable
  3880. * first the self-refresh mode. The self-refresh enable bit in turn
  3881. * will be checked/applied by the HW only at the next frame start
  3882. * event which is after the vblank start event, so we need to have a
  3883. * wait-for-vblank between disabling the plane and the pipe.
  3884. */
  3885. if (HAS_GMCH_DISPLAY(dev)) {
  3886. intel_set_memory_cxsr(dev_priv, false);
  3887. dev_priv->wm.vlv.cxsr = false;
  3888. intel_wait_for_vblank(dev, pipe);
  3889. }
  3890. }
  3891. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  3892. {
  3893. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3894. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3895. struct intel_crtc_state *pipe_config =
  3896. to_intel_crtc_state(crtc->base.state);
  3897. struct drm_device *dev = crtc->base.dev;
  3898. struct drm_plane *primary = crtc->base.primary;
  3899. struct drm_plane_state *old_pri_state =
  3900. drm_atomic_get_existing_plane_state(old_state, primary);
  3901. intel_frontbuffer_flip(dev, pipe_config->fb_bits);
  3902. crtc->wm.cxsr_allowed = true;
  3903. if (pipe_config->update_wm_post && pipe_config->base.active)
  3904. intel_update_watermarks(&crtc->base);
  3905. if (old_pri_state) {
  3906. struct intel_plane_state *primary_state =
  3907. to_intel_plane_state(primary->state);
  3908. struct intel_plane_state *old_primary_state =
  3909. to_intel_plane_state(old_pri_state);
  3910. intel_fbc_post_update(crtc);
  3911. if (primary_state->visible &&
  3912. (needs_modeset(&pipe_config->base) ||
  3913. !old_primary_state->visible))
  3914. intel_post_enable_primary(&crtc->base);
  3915. }
  3916. }
  3917. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3918. {
  3919. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3920. struct drm_device *dev = crtc->base.dev;
  3921. struct drm_i915_private *dev_priv = to_i915(dev);
  3922. struct intel_crtc_state *pipe_config =
  3923. to_intel_crtc_state(crtc->base.state);
  3924. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3925. struct drm_plane *primary = crtc->base.primary;
  3926. struct drm_plane_state *old_pri_state =
  3927. drm_atomic_get_existing_plane_state(old_state, primary);
  3928. bool modeset = needs_modeset(&pipe_config->base);
  3929. if (old_pri_state) {
  3930. struct intel_plane_state *primary_state =
  3931. to_intel_plane_state(primary->state);
  3932. struct intel_plane_state *old_primary_state =
  3933. to_intel_plane_state(old_pri_state);
  3934. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3935. if (old_primary_state->visible &&
  3936. (modeset || !primary_state->visible))
  3937. intel_pre_disable_primary(&crtc->base);
  3938. }
  3939. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev)) {
  3940. crtc->wm.cxsr_allowed = false;
  3941. /*
  3942. * Vblank time updates from the shadow to live plane control register
  3943. * are blocked if the memory self-refresh mode is active at that
  3944. * moment. So to make sure the plane gets truly disabled, disable
  3945. * first the self-refresh mode. The self-refresh enable bit in turn
  3946. * will be checked/applied by the HW only at the next frame start
  3947. * event which is after the vblank start event, so we need to have a
  3948. * wait-for-vblank between disabling the plane and the pipe.
  3949. */
  3950. if (old_crtc_state->base.active) {
  3951. intel_set_memory_cxsr(dev_priv, false);
  3952. dev_priv->wm.vlv.cxsr = false;
  3953. intel_wait_for_vblank(dev, crtc->pipe);
  3954. }
  3955. }
  3956. /*
  3957. * IVB workaround: must disable low power watermarks for at least
  3958. * one frame before enabling scaling. LP watermarks can be re-enabled
  3959. * when scaling is disabled.
  3960. *
  3961. * WaCxSRDisabledForSpriteScaling:ivb
  3962. */
  3963. if (pipe_config->disable_lp_wm) {
  3964. ilk_disable_lp_wm(dev);
  3965. intel_wait_for_vblank(dev, crtc->pipe);
  3966. }
  3967. /*
  3968. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3969. * watermark programming here.
  3970. */
  3971. if (needs_modeset(&pipe_config->base))
  3972. return;
  3973. /*
  3974. * For platforms that support atomic watermarks, program the
  3975. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3976. * will be the intermediate values that are safe for both pre- and
  3977. * post- vblank; when vblank happens, the 'active' values will be set
  3978. * to the final 'target' values and we'll do this again to get the
  3979. * optimal watermarks. For gen9+ platforms, the values we program here
  3980. * will be the final target values which will get automatically latched
  3981. * at vblank time; no further programming will be necessary.
  3982. *
  3983. * If a platform hasn't been transitioned to atomic watermarks yet,
  3984. * we'll continue to update watermarks the old way, if flags tell
  3985. * us to.
  3986. */
  3987. if (dev_priv->display.initial_watermarks != NULL)
  3988. dev_priv->display.initial_watermarks(pipe_config);
  3989. else if (pipe_config->update_wm_pre)
  3990. intel_update_watermarks(&crtc->base);
  3991. }
  3992. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  3993. {
  3994. struct drm_device *dev = crtc->dev;
  3995. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3996. struct drm_plane *p;
  3997. int pipe = intel_crtc->pipe;
  3998. intel_crtc_dpms_overlay_disable(intel_crtc);
  3999. drm_for_each_plane_mask(p, dev, plane_mask)
  4000. to_intel_plane(p)->disable_plane(p, crtc);
  4001. /*
  4002. * FIXME: Once we grow proper nuclear flip support out of this we need
  4003. * to compute the mask of flip planes precisely. For the time being
  4004. * consider this a flip to a NULL plane.
  4005. */
  4006. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4007. }
  4008. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4009. {
  4010. struct drm_device *dev = crtc->dev;
  4011. struct drm_i915_private *dev_priv = to_i915(dev);
  4012. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4013. struct intel_encoder *encoder;
  4014. int pipe = intel_crtc->pipe;
  4015. struct intel_crtc_state *pipe_config =
  4016. to_intel_crtc_state(crtc->state);
  4017. if (WARN_ON(intel_crtc->active))
  4018. return;
  4019. /*
  4020. * Sometimes spurious CPU pipe underruns happen during FDI
  4021. * training, at least with VGA+HDMI cloning. Suppress them.
  4022. *
  4023. * On ILK we get an occasional spurious CPU pipe underruns
  4024. * between eDP port A enable and vdd enable. Also PCH port
  4025. * enable seems to result in the occasional CPU pipe underrun.
  4026. *
  4027. * Spurious PCH underruns also occur during PCH enabling.
  4028. */
  4029. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4030. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4031. if (intel_crtc->config->has_pch_encoder)
  4032. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4033. if (intel_crtc->config->has_pch_encoder)
  4034. intel_prepare_shared_dpll(intel_crtc);
  4035. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4036. intel_dp_set_m_n(intel_crtc, M1_N1);
  4037. intel_set_pipe_timings(intel_crtc);
  4038. intel_set_pipe_src_size(intel_crtc);
  4039. if (intel_crtc->config->has_pch_encoder) {
  4040. intel_cpu_transcoder_set_m_n(intel_crtc,
  4041. &intel_crtc->config->fdi_m_n, NULL);
  4042. }
  4043. ironlake_set_pipeconf(crtc);
  4044. intel_crtc->active = true;
  4045. for_each_encoder_on_crtc(dev, crtc, encoder)
  4046. if (encoder->pre_enable)
  4047. encoder->pre_enable(encoder);
  4048. if (intel_crtc->config->has_pch_encoder) {
  4049. /* Note: FDI PLL enabling _must_ be done before we enable the
  4050. * cpu pipes, hence this is separate from all the other fdi/pch
  4051. * enabling. */
  4052. ironlake_fdi_pll_enable(intel_crtc);
  4053. } else {
  4054. assert_fdi_tx_disabled(dev_priv, pipe);
  4055. assert_fdi_rx_disabled(dev_priv, pipe);
  4056. }
  4057. ironlake_pfit_enable(intel_crtc);
  4058. /*
  4059. * On ILK+ LUT must be loaded before the pipe is running but with
  4060. * clocks enabled
  4061. */
  4062. intel_color_load_luts(&pipe_config->base);
  4063. if (dev_priv->display.initial_watermarks != NULL)
  4064. dev_priv->display.initial_watermarks(intel_crtc->config);
  4065. intel_enable_pipe(intel_crtc);
  4066. if (intel_crtc->config->has_pch_encoder)
  4067. ironlake_pch_enable(crtc);
  4068. assert_vblank_disabled(crtc);
  4069. drm_crtc_vblank_on(crtc);
  4070. for_each_encoder_on_crtc(dev, crtc, encoder)
  4071. encoder->enable(encoder);
  4072. if (HAS_PCH_CPT(dev))
  4073. cpt_verify_modeset(dev, intel_crtc->pipe);
  4074. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4075. if (intel_crtc->config->has_pch_encoder)
  4076. intel_wait_for_vblank(dev, pipe);
  4077. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4078. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4079. }
  4080. /* IPS only exists on ULT machines and is tied to pipe A. */
  4081. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4082. {
  4083. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4084. }
  4085. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4086. {
  4087. struct drm_device *dev = crtc->dev;
  4088. struct drm_i915_private *dev_priv = to_i915(dev);
  4089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4090. struct intel_encoder *encoder;
  4091. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4092. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4093. struct intel_crtc_state *pipe_config =
  4094. to_intel_crtc_state(crtc->state);
  4095. if (WARN_ON(intel_crtc->active))
  4096. return;
  4097. if (intel_crtc->config->has_pch_encoder)
  4098. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4099. false);
  4100. for_each_encoder_on_crtc(dev, crtc, encoder)
  4101. if (encoder->pre_pll_enable)
  4102. encoder->pre_pll_enable(encoder);
  4103. if (intel_crtc->config->shared_dpll)
  4104. intel_enable_shared_dpll(intel_crtc);
  4105. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4106. intel_dp_set_m_n(intel_crtc, M1_N1);
  4107. if (!transcoder_is_dsi(cpu_transcoder))
  4108. intel_set_pipe_timings(intel_crtc);
  4109. intel_set_pipe_src_size(intel_crtc);
  4110. if (cpu_transcoder != TRANSCODER_EDP &&
  4111. !transcoder_is_dsi(cpu_transcoder)) {
  4112. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4113. intel_crtc->config->pixel_multiplier - 1);
  4114. }
  4115. if (intel_crtc->config->has_pch_encoder) {
  4116. intel_cpu_transcoder_set_m_n(intel_crtc,
  4117. &intel_crtc->config->fdi_m_n, NULL);
  4118. }
  4119. if (!transcoder_is_dsi(cpu_transcoder))
  4120. haswell_set_pipeconf(crtc);
  4121. haswell_set_pipemisc(crtc);
  4122. intel_color_set_csc(&pipe_config->base);
  4123. intel_crtc->active = true;
  4124. if (intel_crtc->config->has_pch_encoder)
  4125. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4126. else
  4127. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4128. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4129. if (encoder->pre_enable)
  4130. encoder->pre_enable(encoder);
  4131. }
  4132. if (intel_crtc->config->has_pch_encoder)
  4133. dev_priv->display.fdi_link_train(crtc);
  4134. if (!transcoder_is_dsi(cpu_transcoder))
  4135. intel_ddi_enable_pipe_clock(intel_crtc);
  4136. if (INTEL_INFO(dev)->gen >= 9)
  4137. skylake_pfit_enable(intel_crtc);
  4138. else
  4139. ironlake_pfit_enable(intel_crtc);
  4140. /*
  4141. * On ILK+ LUT must be loaded before the pipe is running but with
  4142. * clocks enabled
  4143. */
  4144. intel_color_load_luts(&pipe_config->base);
  4145. intel_ddi_set_pipe_settings(crtc);
  4146. if (!transcoder_is_dsi(cpu_transcoder))
  4147. intel_ddi_enable_transcoder_func(crtc);
  4148. if (dev_priv->display.initial_watermarks != NULL)
  4149. dev_priv->display.initial_watermarks(pipe_config);
  4150. else
  4151. intel_update_watermarks(crtc);
  4152. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4153. if (!transcoder_is_dsi(cpu_transcoder))
  4154. intel_enable_pipe(intel_crtc);
  4155. if (intel_crtc->config->has_pch_encoder)
  4156. lpt_pch_enable(crtc);
  4157. if (intel_crtc->config->dp_encoder_is_mst)
  4158. intel_ddi_set_vc_payload_alloc(crtc, true);
  4159. assert_vblank_disabled(crtc);
  4160. drm_crtc_vblank_on(crtc);
  4161. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4162. encoder->enable(encoder);
  4163. intel_opregion_notify_encoder(encoder, true);
  4164. }
  4165. if (intel_crtc->config->has_pch_encoder) {
  4166. intel_wait_for_vblank(dev, pipe);
  4167. intel_wait_for_vblank(dev, pipe);
  4168. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4169. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4170. true);
  4171. }
  4172. /* If we change the relative order between pipe/planes enabling, we need
  4173. * to change the workaround. */
  4174. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4175. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4176. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4177. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4178. }
  4179. }
  4180. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4181. {
  4182. struct drm_device *dev = crtc->base.dev;
  4183. struct drm_i915_private *dev_priv = to_i915(dev);
  4184. int pipe = crtc->pipe;
  4185. /* To avoid upsetting the power well on haswell only disable the pfit if
  4186. * it's in use. The hw state code will make sure we get this right. */
  4187. if (force || crtc->config->pch_pfit.enabled) {
  4188. I915_WRITE(PF_CTL(pipe), 0);
  4189. I915_WRITE(PF_WIN_POS(pipe), 0);
  4190. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4191. }
  4192. }
  4193. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4194. {
  4195. struct drm_device *dev = crtc->dev;
  4196. struct drm_i915_private *dev_priv = to_i915(dev);
  4197. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4198. struct intel_encoder *encoder;
  4199. int pipe = intel_crtc->pipe;
  4200. /*
  4201. * Sometimes spurious CPU pipe underruns happen when the
  4202. * pipe is already disabled, but FDI RX/TX is still enabled.
  4203. * Happens at least with VGA+HDMI cloning. Suppress them.
  4204. */
  4205. if (intel_crtc->config->has_pch_encoder) {
  4206. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4207. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4208. }
  4209. for_each_encoder_on_crtc(dev, crtc, encoder)
  4210. encoder->disable(encoder);
  4211. drm_crtc_vblank_off(crtc);
  4212. assert_vblank_disabled(crtc);
  4213. intel_disable_pipe(intel_crtc);
  4214. ironlake_pfit_disable(intel_crtc, false);
  4215. if (intel_crtc->config->has_pch_encoder)
  4216. ironlake_fdi_disable(crtc);
  4217. for_each_encoder_on_crtc(dev, crtc, encoder)
  4218. if (encoder->post_disable)
  4219. encoder->post_disable(encoder);
  4220. if (intel_crtc->config->has_pch_encoder) {
  4221. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4222. if (HAS_PCH_CPT(dev)) {
  4223. i915_reg_t reg;
  4224. u32 temp;
  4225. /* disable TRANS_DP_CTL */
  4226. reg = TRANS_DP_CTL(pipe);
  4227. temp = I915_READ(reg);
  4228. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4229. TRANS_DP_PORT_SEL_MASK);
  4230. temp |= TRANS_DP_PORT_SEL_NONE;
  4231. I915_WRITE(reg, temp);
  4232. /* disable DPLL_SEL */
  4233. temp = I915_READ(PCH_DPLL_SEL);
  4234. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4235. I915_WRITE(PCH_DPLL_SEL, temp);
  4236. }
  4237. ironlake_fdi_pll_disable(intel_crtc);
  4238. }
  4239. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4240. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4241. }
  4242. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4243. {
  4244. struct drm_device *dev = crtc->dev;
  4245. struct drm_i915_private *dev_priv = to_i915(dev);
  4246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4247. struct intel_encoder *encoder;
  4248. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4249. if (intel_crtc->config->has_pch_encoder)
  4250. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4251. false);
  4252. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4253. intel_opregion_notify_encoder(encoder, false);
  4254. encoder->disable(encoder);
  4255. }
  4256. drm_crtc_vblank_off(crtc);
  4257. assert_vblank_disabled(crtc);
  4258. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4259. if (!transcoder_is_dsi(cpu_transcoder))
  4260. intel_disable_pipe(intel_crtc);
  4261. if (intel_crtc->config->dp_encoder_is_mst)
  4262. intel_ddi_set_vc_payload_alloc(crtc, false);
  4263. if (!transcoder_is_dsi(cpu_transcoder))
  4264. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4265. if (INTEL_INFO(dev)->gen >= 9)
  4266. skylake_scaler_disable(intel_crtc);
  4267. else
  4268. ironlake_pfit_disable(intel_crtc, false);
  4269. if (!transcoder_is_dsi(cpu_transcoder))
  4270. intel_ddi_disable_pipe_clock(intel_crtc);
  4271. for_each_encoder_on_crtc(dev, crtc, encoder)
  4272. if (encoder->post_disable)
  4273. encoder->post_disable(encoder);
  4274. if (intel_crtc->config->has_pch_encoder) {
  4275. lpt_disable_pch_transcoder(dev_priv);
  4276. lpt_disable_iclkip(dev_priv);
  4277. intel_ddi_fdi_disable(crtc);
  4278. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4279. true);
  4280. }
  4281. }
  4282. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4283. {
  4284. struct drm_device *dev = crtc->base.dev;
  4285. struct drm_i915_private *dev_priv = to_i915(dev);
  4286. struct intel_crtc_state *pipe_config = crtc->config;
  4287. if (!pipe_config->gmch_pfit.control)
  4288. return;
  4289. /*
  4290. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4291. * according to register description and PRM.
  4292. */
  4293. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4294. assert_pipe_disabled(dev_priv, crtc->pipe);
  4295. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4296. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4297. /* Border color in case we don't scale up to the full screen. Black by
  4298. * default, change to something else for debugging. */
  4299. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4300. }
  4301. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4302. {
  4303. switch (port) {
  4304. case PORT_A:
  4305. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4306. case PORT_B:
  4307. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4308. case PORT_C:
  4309. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4310. case PORT_D:
  4311. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4312. case PORT_E:
  4313. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4314. default:
  4315. MISSING_CASE(port);
  4316. return POWER_DOMAIN_PORT_OTHER;
  4317. }
  4318. }
  4319. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4320. {
  4321. switch (port) {
  4322. case PORT_A:
  4323. return POWER_DOMAIN_AUX_A;
  4324. case PORT_B:
  4325. return POWER_DOMAIN_AUX_B;
  4326. case PORT_C:
  4327. return POWER_DOMAIN_AUX_C;
  4328. case PORT_D:
  4329. return POWER_DOMAIN_AUX_D;
  4330. case PORT_E:
  4331. /* FIXME: Check VBT for actual wiring of PORT E */
  4332. return POWER_DOMAIN_AUX_D;
  4333. default:
  4334. MISSING_CASE(port);
  4335. return POWER_DOMAIN_AUX_A;
  4336. }
  4337. }
  4338. enum intel_display_power_domain
  4339. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4340. {
  4341. struct drm_device *dev = intel_encoder->base.dev;
  4342. struct intel_digital_port *intel_dig_port;
  4343. switch (intel_encoder->type) {
  4344. case INTEL_OUTPUT_UNKNOWN:
  4345. /* Only DDI platforms should ever use this output type */
  4346. WARN_ON_ONCE(!HAS_DDI(dev));
  4347. case INTEL_OUTPUT_DP:
  4348. case INTEL_OUTPUT_HDMI:
  4349. case INTEL_OUTPUT_EDP:
  4350. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4351. return port_to_power_domain(intel_dig_port->port);
  4352. case INTEL_OUTPUT_DP_MST:
  4353. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4354. return port_to_power_domain(intel_dig_port->port);
  4355. case INTEL_OUTPUT_ANALOG:
  4356. return POWER_DOMAIN_PORT_CRT;
  4357. case INTEL_OUTPUT_DSI:
  4358. return POWER_DOMAIN_PORT_DSI;
  4359. default:
  4360. return POWER_DOMAIN_PORT_OTHER;
  4361. }
  4362. }
  4363. enum intel_display_power_domain
  4364. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4365. {
  4366. struct drm_device *dev = intel_encoder->base.dev;
  4367. struct intel_digital_port *intel_dig_port;
  4368. switch (intel_encoder->type) {
  4369. case INTEL_OUTPUT_UNKNOWN:
  4370. case INTEL_OUTPUT_HDMI:
  4371. /*
  4372. * Only DDI platforms should ever use these output types.
  4373. * We can get here after the HDMI detect code has already set
  4374. * the type of the shared encoder. Since we can't be sure
  4375. * what's the status of the given connectors, play safe and
  4376. * run the DP detection too.
  4377. */
  4378. WARN_ON_ONCE(!HAS_DDI(dev));
  4379. case INTEL_OUTPUT_DP:
  4380. case INTEL_OUTPUT_EDP:
  4381. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4382. return port_to_aux_power_domain(intel_dig_port->port);
  4383. case INTEL_OUTPUT_DP_MST:
  4384. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4385. return port_to_aux_power_domain(intel_dig_port->port);
  4386. default:
  4387. MISSING_CASE(intel_encoder->type);
  4388. return POWER_DOMAIN_AUX_A;
  4389. }
  4390. }
  4391. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4392. struct intel_crtc_state *crtc_state)
  4393. {
  4394. struct drm_device *dev = crtc->dev;
  4395. struct drm_encoder *encoder;
  4396. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4397. enum pipe pipe = intel_crtc->pipe;
  4398. unsigned long mask;
  4399. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4400. if (!crtc_state->base.active)
  4401. return 0;
  4402. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4403. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4404. if (crtc_state->pch_pfit.enabled ||
  4405. crtc_state->pch_pfit.force_thru)
  4406. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4407. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4408. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4409. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4410. }
  4411. if (crtc_state->shared_dpll)
  4412. mask |= BIT(POWER_DOMAIN_PLLS);
  4413. return mask;
  4414. }
  4415. static unsigned long
  4416. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4417. struct intel_crtc_state *crtc_state)
  4418. {
  4419. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4421. enum intel_display_power_domain domain;
  4422. unsigned long domains, new_domains, old_domains;
  4423. old_domains = intel_crtc->enabled_power_domains;
  4424. intel_crtc->enabled_power_domains = new_domains =
  4425. get_crtc_power_domains(crtc, crtc_state);
  4426. domains = new_domains & ~old_domains;
  4427. for_each_power_domain(domain, domains)
  4428. intel_display_power_get(dev_priv, domain);
  4429. return old_domains & ~new_domains;
  4430. }
  4431. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4432. unsigned long domains)
  4433. {
  4434. enum intel_display_power_domain domain;
  4435. for_each_power_domain(domain, domains)
  4436. intel_display_power_put(dev_priv, domain);
  4437. }
  4438. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4439. {
  4440. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4441. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4442. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4443. return max_cdclk_freq;
  4444. else if (IS_CHERRYVIEW(dev_priv))
  4445. return max_cdclk_freq*95/100;
  4446. else if (INTEL_INFO(dev_priv)->gen < 4)
  4447. return 2*max_cdclk_freq*90/100;
  4448. else
  4449. return max_cdclk_freq*90/100;
  4450. }
  4451. static int skl_calc_cdclk(int max_pixclk, int vco);
  4452. static void intel_update_max_cdclk(struct drm_device *dev)
  4453. {
  4454. struct drm_i915_private *dev_priv = to_i915(dev);
  4455. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4456. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4457. int max_cdclk, vco;
  4458. vco = dev_priv->skl_preferred_vco_freq;
  4459. WARN_ON(vco != 8100000 && vco != 8640000);
  4460. /*
  4461. * Use the lower (vco 8640) cdclk values as a
  4462. * first guess. skl_calc_cdclk() will correct it
  4463. * if the preferred vco is 8100 instead.
  4464. */
  4465. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4466. max_cdclk = 617143;
  4467. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4468. max_cdclk = 540000;
  4469. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4470. max_cdclk = 432000;
  4471. else
  4472. max_cdclk = 308571;
  4473. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4474. } else if (IS_BROXTON(dev)) {
  4475. dev_priv->max_cdclk_freq = 624000;
  4476. } else if (IS_BROADWELL(dev)) {
  4477. /*
  4478. * FIXME with extra cooling we can allow
  4479. * 540 MHz for ULX and 675 Mhz for ULT.
  4480. * How can we know if extra cooling is
  4481. * available? PCI ID, VTB, something else?
  4482. */
  4483. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4484. dev_priv->max_cdclk_freq = 450000;
  4485. else if (IS_BDW_ULX(dev))
  4486. dev_priv->max_cdclk_freq = 450000;
  4487. else if (IS_BDW_ULT(dev))
  4488. dev_priv->max_cdclk_freq = 540000;
  4489. else
  4490. dev_priv->max_cdclk_freq = 675000;
  4491. } else if (IS_CHERRYVIEW(dev)) {
  4492. dev_priv->max_cdclk_freq = 320000;
  4493. } else if (IS_VALLEYVIEW(dev)) {
  4494. dev_priv->max_cdclk_freq = 400000;
  4495. } else {
  4496. /* otherwise assume cdclk is fixed */
  4497. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4498. }
  4499. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4500. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4501. dev_priv->max_cdclk_freq);
  4502. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4503. dev_priv->max_dotclk_freq);
  4504. }
  4505. static void intel_update_cdclk(struct drm_device *dev)
  4506. {
  4507. struct drm_i915_private *dev_priv = to_i915(dev);
  4508. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4509. if (INTEL_GEN(dev_priv) >= 9)
  4510. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4511. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4512. dev_priv->cdclk_pll.ref);
  4513. else
  4514. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4515. dev_priv->cdclk_freq);
  4516. /*
  4517. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4518. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4519. * of cdclk that generates 4MHz reference clock freq which is used to
  4520. * generate GMBus clock. This will vary with the cdclk freq.
  4521. */
  4522. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4523. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4524. }
  4525. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4526. static int skl_cdclk_decimal(int cdclk)
  4527. {
  4528. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4529. }
  4530. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4531. {
  4532. int ratio;
  4533. if (cdclk == dev_priv->cdclk_pll.ref)
  4534. return 0;
  4535. switch (cdclk) {
  4536. default:
  4537. MISSING_CASE(cdclk);
  4538. case 144000:
  4539. case 288000:
  4540. case 384000:
  4541. case 576000:
  4542. ratio = 60;
  4543. break;
  4544. case 624000:
  4545. ratio = 65;
  4546. break;
  4547. }
  4548. return dev_priv->cdclk_pll.ref * ratio;
  4549. }
  4550. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4551. {
  4552. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4553. /* Timeout 200us */
  4554. if (intel_wait_for_register(dev_priv,
  4555. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  4556. 1))
  4557. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4558. dev_priv->cdclk_pll.vco = 0;
  4559. }
  4560. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  4561. {
  4562. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  4563. u32 val;
  4564. val = I915_READ(BXT_DE_PLL_CTL);
  4565. val &= ~BXT_DE_PLL_RATIO_MASK;
  4566. val |= BXT_DE_PLL_RATIO(ratio);
  4567. I915_WRITE(BXT_DE_PLL_CTL, val);
  4568. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4569. /* Timeout 200us */
  4570. if (intel_wait_for_register(dev_priv,
  4571. BXT_DE_PLL_ENABLE,
  4572. BXT_DE_PLL_LOCK,
  4573. BXT_DE_PLL_LOCK,
  4574. 1))
  4575. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4576. dev_priv->cdclk_pll.vco = vco;
  4577. }
  4578. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4579. {
  4580. u32 val, divider;
  4581. int vco, ret;
  4582. vco = bxt_de_pll_vco(dev_priv, cdclk);
  4583. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4584. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  4585. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  4586. case 8:
  4587. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4588. break;
  4589. case 4:
  4590. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4591. break;
  4592. case 3:
  4593. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4594. break;
  4595. case 2:
  4596. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4597. break;
  4598. default:
  4599. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  4600. WARN_ON(vco != 0);
  4601. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4602. break;
  4603. }
  4604. /* Inform power controller of upcoming frequency change */
  4605. mutex_lock(&dev_priv->rps.hw_lock);
  4606. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4607. 0x80000000);
  4608. mutex_unlock(&dev_priv->rps.hw_lock);
  4609. if (ret) {
  4610. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4611. ret, cdclk);
  4612. return;
  4613. }
  4614. if (dev_priv->cdclk_pll.vco != 0 &&
  4615. dev_priv->cdclk_pll.vco != vco)
  4616. bxt_de_pll_disable(dev_priv);
  4617. if (dev_priv->cdclk_pll.vco != vco)
  4618. bxt_de_pll_enable(dev_priv, vco);
  4619. val = divider | skl_cdclk_decimal(cdclk);
  4620. /*
  4621. * FIXME if only the cd2x divider needs changing, it could be done
  4622. * without shutting off the pipe (if only one pipe is active).
  4623. */
  4624. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4625. /*
  4626. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4627. * enable otherwise.
  4628. */
  4629. if (cdclk >= 500000)
  4630. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4631. I915_WRITE(CDCLK_CTL, val);
  4632. mutex_lock(&dev_priv->rps.hw_lock);
  4633. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4634. DIV_ROUND_UP(cdclk, 25000));
  4635. mutex_unlock(&dev_priv->rps.hw_lock);
  4636. if (ret) {
  4637. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4638. ret, cdclk);
  4639. return;
  4640. }
  4641. intel_update_cdclk(&dev_priv->drm);
  4642. }
  4643. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4644. {
  4645. u32 cdctl, expected;
  4646. intel_update_cdclk(&dev_priv->drm);
  4647. if (dev_priv->cdclk_pll.vco == 0 ||
  4648. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4649. goto sanitize;
  4650. /* DPLL okay; verify the cdclock
  4651. *
  4652. * Some BIOS versions leave an incorrect decimal frequency value and
  4653. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  4654. * so sanitize this register.
  4655. */
  4656. cdctl = I915_READ(CDCLK_CTL);
  4657. /*
  4658. * Let's ignore the pipe field, since BIOS could have configured the
  4659. * dividers both synching to an active pipe, or asynchronously
  4660. * (PIPE_NONE).
  4661. */
  4662. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  4663. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  4664. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4665. /*
  4666. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4667. * enable otherwise.
  4668. */
  4669. if (dev_priv->cdclk_freq >= 500000)
  4670. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4671. if (cdctl == expected)
  4672. /* All well; nothing to sanitize */
  4673. return;
  4674. sanitize:
  4675. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4676. /* force cdclk programming */
  4677. dev_priv->cdclk_freq = 0;
  4678. /* force full PLL disable + enable */
  4679. dev_priv->cdclk_pll.vco = -1;
  4680. }
  4681. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  4682. {
  4683. bxt_sanitize_cdclk(dev_priv);
  4684. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  4685. return;
  4686. /*
  4687. * FIXME:
  4688. * - The initial CDCLK needs to be read from VBT.
  4689. * Need to make this change after VBT has changes for BXT.
  4690. */
  4691. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  4692. }
  4693. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  4694. {
  4695. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  4696. }
  4697. static int skl_calc_cdclk(int max_pixclk, int vco)
  4698. {
  4699. if (vco == 8640000) {
  4700. if (max_pixclk > 540000)
  4701. return 617143;
  4702. else if (max_pixclk > 432000)
  4703. return 540000;
  4704. else if (max_pixclk > 308571)
  4705. return 432000;
  4706. else
  4707. return 308571;
  4708. } else {
  4709. if (max_pixclk > 540000)
  4710. return 675000;
  4711. else if (max_pixclk > 450000)
  4712. return 540000;
  4713. else if (max_pixclk > 337500)
  4714. return 450000;
  4715. else
  4716. return 337500;
  4717. }
  4718. }
  4719. static void
  4720. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4721. {
  4722. u32 val;
  4723. dev_priv->cdclk_pll.ref = 24000;
  4724. dev_priv->cdclk_pll.vco = 0;
  4725. val = I915_READ(LCPLL1_CTL);
  4726. if ((val & LCPLL_PLL_ENABLE) == 0)
  4727. return;
  4728. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  4729. return;
  4730. val = I915_READ(DPLL_CTRL1);
  4731. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4732. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4733. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4734. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  4735. return;
  4736. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4737. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4738. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4739. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4740. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4741. dev_priv->cdclk_pll.vco = 8100000;
  4742. break;
  4743. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4744. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4745. dev_priv->cdclk_pll.vco = 8640000;
  4746. break;
  4747. default:
  4748. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4749. break;
  4750. }
  4751. }
  4752. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4753. {
  4754. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4755. dev_priv->skl_preferred_vco_freq = vco;
  4756. if (changed)
  4757. intel_update_max_cdclk(&dev_priv->drm);
  4758. }
  4759. static void
  4760. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4761. {
  4762. int min_cdclk = skl_calc_cdclk(0, vco);
  4763. u32 val;
  4764. WARN_ON(vco != 8100000 && vco != 8640000);
  4765. /* select the minimum CDCLK before enabling DPLL 0 */
  4766. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4767. I915_WRITE(CDCLK_CTL, val);
  4768. POSTING_READ(CDCLK_CTL);
  4769. /*
  4770. * We always enable DPLL0 with the lowest link rate possible, but still
  4771. * taking into account the VCO required to operate the eDP panel at the
  4772. * desired frequency. The usual DP link rates operate with a VCO of
  4773. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4774. * The modeset code is responsible for the selection of the exact link
  4775. * rate later on, with the constraint of choosing a frequency that
  4776. * works with vco.
  4777. */
  4778. val = I915_READ(DPLL_CTRL1);
  4779. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4780. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4781. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4782. if (vco == 8640000)
  4783. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4784. SKL_DPLL0);
  4785. else
  4786. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4787. SKL_DPLL0);
  4788. I915_WRITE(DPLL_CTRL1, val);
  4789. POSTING_READ(DPLL_CTRL1);
  4790. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4791. if (intel_wait_for_register(dev_priv,
  4792. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  4793. 5))
  4794. DRM_ERROR("DPLL0 not locked\n");
  4795. dev_priv->cdclk_pll.vco = vco;
  4796. /* We'll want to keep using the current vco from now on. */
  4797. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4798. }
  4799. static void
  4800. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4801. {
  4802. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4803. if (intel_wait_for_register(dev_priv,
  4804. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  4805. 1))
  4806. DRM_ERROR("Couldn't disable DPLL0\n");
  4807. dev_priv->cdclk_pll.vco = 0;
  4808. }
  4809. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4810. {
  4811. int ret;
  4812. u32 val;
  4813. /* inform PCU we want to change CDCLK */
  4814. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4815. mutex_lock(&dev_priv->rps.hw_lock);
  4816. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4817. mutex_unlock(&dev_priv->rps.hw_lock);
  4818. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4819. }
  4820. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4821. {
  4822. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  4823. }
  4824. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4825. {
  4826. struct drm_device *dev = &dev_priv->drm;
  4827. u32 freq_select, pcu_ack;
  4828. WARN_ON((cdclk == 24000) != (vco == 0));
  4829. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4830. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4831. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4832. return;
  4833. }
  4834. /* set CDCLK_CTL */
  4835. switch (cdclk) {
  4836. case 450000:
  4837. case 432000:
  4838. freq_select = CDCLK_FREQ_450_432;
  4839. pcu_ack = 1;
  4840. break;
  4841. case 540000:
  4842. freq_select = CDCLK_FREQ_540;
  4843. pcu_ack = 2;
  4844. break;
  4845. case 308571:
  4846. case 337500:
  4847. default:
  4848. freq_select = CDCLK_FREQ_337_308;
  4849. pcu_ack = 0;
  4850. break;
  4851. case 617143:
  4852. case 675000:
  4853. freq_select = CDCLK_FREQ_675_617;
  4854. pcu_ack = 3;
  4855. break;
  4856. }
  4857. if (dev_priv->cdclk_pll.vco != 0 &&
  4858. dev_priv->cdclk_pll.vco != vco)
  4859. skl_dpll0_disable(dev_priv);
  4860. if (dev_priv->cdclk_pll.vco != vco)
  4861. skl_dpll0_enable(dev_priv, vco);
  4862. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4863. POSTING_READ(CDCLK_CTL);
  4864. /* inform PCU of the change */
  4865. mutex_lock(&dev_priv->rps.hw_lock);
  4866. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4867. mutex_unlock(&dev_priv->rps.hw_lock);
  4868. intel_update_cdclk(dev);
  4869. }
  4870. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4871. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4872. {
  4873. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4874. }
  4875. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4876. {
  4877. int cdclk, vco;
  4878. skl_sanitize_cdclk(dev_priv);
  4879. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4880. /*
  4881. * Use the current vco as our initial
  4882. * guess as to what the preferred vco is.
  4883. */
  4884. if (dev_priv->skl_preferred_vco_freq == 0)
  4885. skl_set_preferred_cdclk_vco(dev_priv,
  4886. dev_priv->cdclk_pll.vco);
  4887. return;
  4888. }
  4889. vco = dev_priv->skl_preferred_vco_freq;
  4890. if (vco == 0)
  4891. vco = 8100000;
  4892. cdclk = skl_calc_cdclk(0, vco);
  4893. skl_set_cdclk(dev_priv, cdclk, vco);
  4894. }
  4895. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4896. {
  4897. uint32_t cdctl, expected;
  4898. /*
  4899. * check if the pre-os intialized the display
  4900. * There is SWF18 scratchpad register defined which is set by the
  4901. * pre-os which can be used by the OS drivers to check the status
  4902. */
  4903. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4904. goto sanitize;
  4905. intel_update_cdclk(&dev_priv->drm);
  4906. /* Is PLL enabled and locked ? */
  4907. if (dev_priv->cdclk_pll.vco == 0 ||
  4908. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  4909. goto sanitize;
  4910. /* DPLL okay; verify the cdclock
  4911. *
  4912. * Noticed in some instances that the freq selection is correct but
  4913. * decimal part is programmed wrong from BIOS where pre-os does not
  4914. * enable display. Verify the same as well.
  4915. */
  4916. cdctl = I915_READ(CDCLK_CTL);
  4917. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4918. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4919. if (cdctl == expected)
  4920. /* All well; nothing to sanitize */
  4921. return;
  4922. sanitize:
  4923. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4924. /* force cdclk programming */
  4925. dev_priv->cdclk_freq = 0;
  4926. /* force full PLL disable + enable */
  4927. dev_priv->cdclk_pll.vco = -1;
  4928. }
  4929. /* Adjust CDclk dividers to allow high res or save power if possible */
  4930. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4931. {
  4932. struct drm_i915_private *dev_priv = to_i915(dev);
  4933. u32 val, cmd;
  4934. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4935. != dev_priv->cdclk_freq);
  4936. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4937. cmd = 2;
  4938. else if (cdclk == 266667)
  4939. cmd = 1;
  4940. else
  4941. cmd = 0;
  4942. mutex_lock(&dev_priv->rps.hw_lock);
  4943. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4944. val &= ~DSPFREQGUAR_MASK;
  4945. val |= (cmd << DSPFREQGUAR_SHIFT);
  4946. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4947. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4948. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4949. 50)) {
  4950. DRM_ERROR("timed out waiting for CDclk change\n");
  4951. }
  4952. mutex_unlock(&dev_priv->rps.hw_lock);
  4953. mutex_lock(&dev_priv->sb_lock);
  4954. if (cdclk == 400000) {
  4955. u32 divider;
  4956. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4957. /* adjust cdclk divider */
  4958. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4959. val &= ~CCK_FREQUENCY_VALUES;
  4960. val |= divider;
  4961. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4962. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4963. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4964. 50))
  4965. DRM_ERROR("timed out waiting for CDclk change\n");
  4966. }
  4967. /* adjust self-refresh exit latency value */
  4968. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4969. val &= ~0x7f;
  4970. /*
  4971. * For high bandwidth configs, we set a higher latency in the bunit
  4972. * so that the core display fetch happens in time to avoid underruns.
  4973. */
  4974. if (cdclk == 400000)
  4975. val |= 4500 / 250; /* 4.5 usec */
  4976. else
  4977. val |= 3000 / 250; /* 3.0 usec */
  4978. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4979. mutex_unlock(&dev_priv->sb_lock);
  4980. intel_update_cdclk(dev);
  4981. }
  4982. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4983. {
  4984. struct drm_i915_private *dev_priv = to_i915(dev);
  4985. u32 val, cmd;
  4986. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4987. != dev_priv->cdclk_freq);
  4988. switch (cdclk) {
  4989. case 333333:
  4990. case 320000:
  4991. case 266667:
  4992. case 200000:
  4993. break;
  4994. default:
  4995. MISSING_CASE(cdclk);
  4996. return;
  4997. }
  4998. /*
  4999. * Specs are full of misinformation, but testing on actual
  5000. * hardware has shown that we just need to write the desired
  5001. * CCK divider into the Punit register.
  5002. */
  5003. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5004. mutex_lock(&dev_priv->rps.hw_lock);
  5005. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5006. val &= ~DSPFREQGUAR_MASK_CHV;
  5007. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5008. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5009. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5010. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5011. 50)) {
  5012. DRM_ERROR("timed out waiting for CDclk change\n");
  5013. }
  5014. mutex_unlock(&dev_priv->rps.hw_lock);
  5015. intel_update_cdclk(dev);
  5016. }
  5017. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5018. int max_pixclk)
  5019. {
  5020. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5021. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5022. /*
  5023. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5024. * 200MHz
  5025. * 267MHz
  5026. * 320/333MHz (depends on HPLL freq)
  5027. * 400MHz (VLV only)
  5028. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5029. * of the lower bin and adjust if needed.
  5030. *
  5031. * We seem to get an unstable or solid color picture at 200MHz.
  5032. * Not sure what's wrong. For now use 200MHz only when all pipes
  5033. * are off.
  5034. */
  5035. if (!IS_CHERRYVIEW(dev_priv) &&
  5036. max_pixclk > freq_320*limit/100)
  5037. return 400000;
  5038. else if (max_pixclk > 266667*limit/100)
  5039. return freq_320;
  5040. else if (max_pixclk > 0)
  5041. return 266667;
  5042. else
  5043. return 200000;
  5044. }
  5045. static int bxt_calc_cdclk(int max_pixclk)
  5046. {
  5047. if (max_pixclk > 576000)
  5048. return 624000;
  5049. else if (max_pixclk > 384000)
  5050. return 576000;
  5051. else if (max_pixclk > 288000)
  5052. return 384000;
  5053. else if (max_pixclk > 144000)
  5054. return 288000;
  5055. else
  5056. return 144000;
  5057. }
  5058. /* Compute the max pixel clock for new configuration. */
  5059. static int intel_mode_max_pixclk(struct drm_device *dev,
  5060. struct drm_atomic_state *state)
  5061. {
  5062. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5063. struct drm_i915_private *dev_priv = to_i915(dev);
  5064. struct drm_crtc *crtc;
  5065. struct drm_crtc_state *crtc_state;
  5066. unsigned max_pixclk = 0, i;
  5067. enum pipe pipe;
  5068. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5069. sizeof(intel_state->min_pixclk));
  5070. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5071. int pixclk = 0;
  5072. if (crtc_state->enable)
  5073. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5074. intel_state->min_pixclk[i] = pixclk;
  5075. }
  5076. for_each_pipe(dev_priv, pipe)
  5077. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5078. return max_pixclk;
  5079. }
  5080. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5081. {
  5082. struct drm_device *dev = state->dev;
  5083. struct drm_i915_private *dev_priv = to_i915(dev);
  5084. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5085. struct intel_atomic_state *intel_state =
  5086. to_intel_atomic_state(state);
  5087. intel_state->cdclk = intel_state->dev_cdclk =
  5088. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5089. if (!intel_state->active_crtcs)
  5090. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5091. return 0;
  5092. }
  5093. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5094. {
  5095. int max_pixclk = ilk_max_pixel_rate(state);
  5096. struct intel_atomic_state *intel_state =
  5097. to_intel_atomic_state(state);
  5098. intel_state->cdclk = intel_state->dev_cdclk =
  5099. bxt_calc_cdclk(max_pixclk);
  5100. if (!intel_state->active_crtcs)
  5101. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5102. return 0;
  5103. }
  5104. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5105. {
  5106. unsigned int credits, default_credits;
  5107. if (IS_CHERRYVIEW(dev_priv))
  5108. default_credits = PFI_CREDIT(12);
  5109. else
  5110. default_credits = PFI_CREDIT(8);
  5111. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5112. /* CHV suggested value is 31 or 63 */
  5113. if (IS_CHERRYVIEW(dev_priv))
  5114. credits = PFI_CREDIT_63;
  5115. else
  5116. credits = PFI_CREDIT(15);
  5117. } else {
  5118. credits = default_credits;
  5119. }
  5120. /*
  5121. * WA - write default credits before re-programming
  5122. * FIXME: should we also set the resend bit here?
  5123. */
  5124. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5125. default_credits);
  5126. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5127. credits | PFI_CREDIT_RESEND);
  5128. /*
  5129. * FIXME is this guaranteed to clear
  5130. * immediately or should we poll for it?
  5131. */
  5132. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5133. }
  5134. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5135. {
  5136. struct drm_device *dev = old_state->dev;
  5137. struct drm_i915_private *dev_priv = to_i915(dev);
  5138. struct intel_atomic_state *old_intel_state =
  5139. to_intel_atomic_state(old_state);
  5140. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5141. /*
  5142. * FIXME: We can end up here with all power domains off, yet
  5143. * with a CDCLK frequency other than the minimum. To account
  5144. * for this take the PIPE-A power domain, which covers the HW
  5145. * blocks needed for the following programming. This can be
  5146. * removed once it's guaranteed that we get here either with
  5147. * the minimum CDCLK set, or the required power domains
  5148. * enabled.
  5149. */
  5150. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5151. if (IS_CHERRYVIEW(dev))
  5152. cherryview_set_cdclk(dev, req_cdclk);
  5153. else
  5154. valleyview_set_cdclk(dev, req_cdclk);
  5155. vlv_program_pfi_credits(dev_priv);
  5156. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5157. }
  5158. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5159. {
  5160. struct drm_device *dev = crtc->dev;
  5161. struct drm_i915_private *dev_priv = to_i915(dev);
  5162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5163. struct intel_encoder *encoder;
  5164. struct intel_crtc_state *pipe_config =
  5165. to_intel_crtc_state(crtc->state);
  5166. int pipe = intel_crtc->pipe;
  5167. if (WARN_ON(intel_crtc->active))
  5168. return;
  5169. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5170. intel_dp_set_m_n(intel_crtc, M1_N1);
  5171. intel_set_pipe_timings(intel_crtc);
  5172. intel_set_pipe_src_size(intel_crtc);
  5173. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5174. struct drm_i915_private *dev_priv = to_i915(dev);
  5175. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5176. I915_WRITE(CHV_CANVAS(pipe), 0);
  5177. }
  5178. i9xx_set_pipeconf(intel_crtc);
  5179. intel_crtc->active = true;
  5180. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5181. for_each_encoder_on_crtc(dev, crtc, encoder)
  5182. if (encoder->pre_pll_enable)
  5183. encoder->pre_pll_enable(encoder);
  5184. if (IS_CHERRYVIEW(dev)) {
  5185. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5186. chv_enable_pll(intel_crtc, intel_crtc->config);
  5187. } else {
  5188. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5189. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5190. }
  5191. for_each_encoder_on_crtc(dev, crtc, encoder)
  5192. if (encoder->pre_enable)
  5193. encoder->pre_enable(encoder);
  5194. i9xx_pfit_enable(intel_crtc);
  5195. intel_color_load_luts(&pipe_config->base);
  5196. intel_update_watermarks(crtc);
  5197. intel_enable_pipe(intel_crtc);
  5198. assert_vblank_disabled(crtc);
  5199. drm_crtc_vblank_on(crtc);
  5200. for_each_encoder_on_crtc(dev, crtc, encoder)
  5201. encoder->enable(encoder);
  5202. }
  5203. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5204. {
  5205. struct drm_device *dev = crtc->base.dev;
  5206. struct drm_i915_private *dev_priv = to_i915(dev);
  5207. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5208. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5209. }
  5210. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5211. {
  5212. struct drm_device *dev = crtc->dev;
  5213. struct drm_i915_private *dev_priv = to_i915(dev);
  5214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5215. struct intel_encoder *encoder;
  5216. struct intel_crtc_state *pipe_config =
  5217. to_intel_crtc_state(crtc->state);
  5218. enum pipe pipe = intel_crtc->pipe;
  5219. if (WARN_ON(intel_crtc->active))
  5220. return;
  5221. i9xx_set_pll_dividers(intel_crtc);
  5222. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5223. intel_dp_set_m_n(intel_crtc, M1_N1);
  5224. intel_set_pipe_timings(intel_crtc);
  5225. intel_set_pipe_src_size(intel_crtc);
  5226. i9xx_set_pipeconf(intel_crtc);
  5227. intel_crtc->active = true;
  5228. if (!IS_GEN2(dev))
  5229. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5230. for_each_encoder_on_crtc(dev, crtc, encoder)
  5231. if (encoder->pre_enable)
  5232. encoder->pre_enable(encoder);
  5233. i9xx_enable_pll(intel_crtc);
  5234. i9xx_pfit_enable(intel_crtc);
  5235. intel_color_load_luts(&pipe_config->base);
  5236. intel_update_watermarks(crtc);
  5237. intel_enable_pipe(intel_crtc);
  5238. assert_vblank_disabled(crtc);
  5239. drm_crtc_vblank_on(crtc);
  5240. for_each_encoder_on_crtc(dev, crtc, encoder)
  5241. encoder->enable(encoder);
  5242. }
  5243. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5244. {
  5245. struct drm_device *dev = crtc->base.dev;
  5246. struct drm_i915_private *dev_priv = to_i915(dev);
  5247. if (!crtc->config->gmch_pfit.control)
  5248. return;
  5249. assert_pipe_disabled(dev_priv, crtc->pipe);
  5250. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5251. I915_READ(PFIT_CONTROL));
  5252. I915_WRITE(PFIT_CONTROL, 0);
  5253. }
  5254. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5255. {
  5256. struct drm_device *dev = crtc->dev;
  5257. struct drm_i915_private *dev_priv = to_i915(dev);
  5258. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5259. struct intel_encoder *encoder;
  5260. int pipe = intel_crtc->pipe;
  5261. /*
  5262. * On gen2 planes are double buffered but the pipe isn't, so we must
  5263. * wait for planes to fully turn off before disabling the pipe.
  5264. */
  5265. if (IS_GEN2(dev))
  5266. intel_wait_for_vblank(dev, pipe);
  5267. for_each_encoder_on_crtc(dev, crtc, encoder)
  5268. encoder->disable(encoder);
  5269. drm_crtc_vblank_off(crtc);
  5270. assert_vblank_disabled(crtc);
  5271. intel_disable_pipe(intel_crtc);
  5272. i9xx_pfit_disable(intel_crtc);
  5273. for_each_encoder_on_crtc(dev, crtc, encoder)
  5274. if (encoder->post_disable)
  5275. encoder->post_disable(encoder);
  5276. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5277. if (IS_CHERRYVIEW(dev))
  5278. chv_disable_pll(dev_priv, pipe);
  5279. else if (IS_VALLEYVIEW(dev))
  5280. vlv_disable_pll(dev_priv, pipe);
  5281. else
  5282. i9xx_disable_pll(intel_crtc);
  5283. }
  5284. for_each_encoder_on_crtc(dev, crtc, encoder)
  5285. if (encoder->post_pll_disable)
  5286. encoder->post_pll_disable(encoder);
  5287. if (!IS_GEN2(dev))
  5288. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5289. }
  5290. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5291. {
  5292. struct intel_encoder *encoder;
  5293. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5294. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5295. enum intel_display_power_domain domain;
  5296. unsigned long domains;
  5297. if (!intel_crtc->active)
  5298. return;
  5299. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5300. WARN_ON(intel_crtc->flip_work);
  5301. intel_pre_disable_primary_noatomic(crtc);
  5302. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5303. to_intel_plane_state(crtc->primary->state)->visible = false;
  5304. }
  5305. dev_priv->display.crtc_disable(crtc);
  5306. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5307. crtc->base.id, crtc->name);
  5308. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5309. crtc->state->active = false;
  5310. intel_crtc->active = false;
  5311. crtc->enabled = false;
  5312. crtc->state->connector_mask = 0;
  5313. crtc->state->encoder_mask = 0;
  5314. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5315. encoder->base.crtc = NULL;
  5316. intel_fbc_disable(intel_crtc);
  5317. intel_update_watermarks(crtc);
  5318. intel_disable_shared_dpll(intel_crtc);
  5319. domains = intel_crtc->enabled_power_domains;
  5320. for_each_power_domain(domain, domains)
  5321. intel_display_power_put(dev_priv, domain);
  5322. intel_crtc->enabled_power_domains = 0;
  5323. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5324. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5325. }
  5326. /*
  5327. * turn all crtc's off, but do not adjust state
  5328. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5329. */
  5330. int intel_display_suspend(struct drm_device *dev)
  5331. {
  5332. struct drm_i915_private *dev_priv = to_i915(dev);
  5333. struct drm_atomic_state *state;
  5334. int ret;
  5335. state = drm_atomic_helper_suspend(dev);
  5336. ret = PTR_ERR_OR_ZERO(state);
  5337. if (ret)
  5338. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5339. else
  5340. dev_priv->modeset_restore_state = state;
  5341. return ret;
  5342. }
  5343. void intel_encoder_destroy(struct drm_encoder *encoder)
  5344. {
  5345. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5346. drm_encoder_cleanup(encoder);
  5347. kfree(intel_encoder);
  5348. }
  5349. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5350. * internal consistency). */
  5351. static void intel_connector_verify_state(struct intel_connector *connector)
  5352. {
  5353. struct drm_crtc *crtc = connector->base.state->crtc;
  5354. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5355. connector->base.base.id,
  5356. connector->base.name);
  5357. if (connector->get_hw_state(connector)) {
  5358. struct intel_encoder *encoder = connector->encoder;
  5359. struct drm_connector_state *conn_state = connector->base.state;
  5360. I915_STATE_WARN(!crtc,
  5361. "connector enabled without attached crtc\n");
  5362. if (!crtc)
  5363. return;
  5364. I915_STATE_WARN(!crtc->state->active,
  5365. "connector is active, but attached crtc isn't\n");
  5366. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5367. return;
  5368. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5369. "atomic encoder doesn't match attached encoder\n");
  5370. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5371. "attached encoder crtc differs from connector crtc\n");
  5372. } else {
  5373. I915_STATE_WARN(crtc && crtc->state->active,
  5374. "attached crtc is active, but connector isn't\n");
  5375. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5376. "best encoder set without crtc!\n");
  5377. }
  5378. }
  5379. int intel_connector_init(struct intel_connector *connector)
  5380. {
  5381. drm_atomic_helper_connector_reset(&connector->base);
  5382. if (!connector->base.state)
  5383. return -ENOMEM;
  5384. return 0;
  5385. }
  5386. struct intel_connector *intel_connector_alloc(void)
  5387. {
  5388. struct intel_connector *connector;
  5389. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5390. if (!connector)
  5391. return NULL;
  5392. if (intel_connector_init(connector) < 0) {
  5393. kfree(connector);
  5394. return NULL;
  5395. }
  5396. return connector;
  5397. }
  5398. /* Simple connector->get_hw_state implementation for encoders that support only
  5399. * one connector and no cloning and hence the encoder state determines the state
  5400. * of the connector. */
  5401. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5402. {
  5403. enum pipe pipe = 0;
  5404. struct intel_encoder *encoder = connector->encoder;
  5405. return encoder->get_hw_state(encoder, &pipe);
  5406. }
  5407. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5408. {
  5409. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5410. return crtc_state->fdi_lanes;
  5411. return 0;
  5412. }
  5413. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5414. struct intel_crtc_state *pipe_config)
  5415. {
  5416. struct drm_atomic_state *state = pipe_config->base.state;
  5417. struct intel_crtc *other_crtc;
  5418. struct intel_crtc_state *other_crtc_state;
  5419. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5420. pipe_name(pipe), pipe_config->fdi_lanes);
  5421. if (pipe_config->fdi_lanes > 4) {
  5422. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5423. pipe_name(pipe), pipe_config->fdi_lanes);
  5424. return -EINVAL;
  5425. }
  5426. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5427. if (pipe_config->fdi_lanes > 2) {
  5428. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5429. pipe_config->fdi_lanes);
  5430. return -EINVAL;
  5431. } else {
  5432. return 0;
  5433. }
  5434. }
  5435. if (INTEL_INFO(dev)->num_pipes == 2)
  5436. return 0;
  5437. /* Ivybridge 3 pipe is really complicated */
  5438. switch (pipe) {
  5439. case PIPE_A:
  5440. return 0;
  5441. case PIPE_B:
  5442. if (pipe_config->fdi_lanes <= 2)
  5443. return 0;
  5444. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5445. other_crtc_state =
  5446. intel_atomic_get_crtc_state(state, other_crtc);
  5447. if (IS_ERR(other_crtc_state))
  5448. return PTR_ERR(other_crtc_state);
  5449. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5450. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5451. pipe_name(pipe), pipe_config->fdi_lanes);
  5452. return -EINVAL;
  5453. }
  5454. return 0;
  5455. case PIPE_C:
  5456. if (pipe_config->fdi_lanes > 2) {
  5457. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5458. pipe_name(pipe), pipe_config->fdi_lanes);
  5459. return -EINVAL;
  5460. }
  5461. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5462. other_crtc_state =
  5463. intel_atomic_get_crtc_state(state, other_crtc);
  5464. if (IS_ERR(other_crtc_state))
  5465. return PTR_ERR(other_crtc_state);
  5466. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5467. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5468. return -EINVAL;
  5469. }
  5470. return 0;
  5471. default:
  5472. BUG();
  5473. }
  5474. }
  5475. #define RETRY 1
  5476. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5477. struct intel_crtc_state *pipe_config)
  5478. {
  5479. struct drm_device *dev = intel_crtc->base.dev;
  5480. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5481. int lane, link_bw, fdi_dotclock, ret;
  5482. bool needs_recompute = false;
  5483. retry:
  5484. /* FDI is a binary signal running at ~2.7GHz, encoding
  5485. * each output octet as 10 bits. The actual frequency
  5486. * is stored as a divider into a 100MHz clock, and the
  5487. * mode pixel clock is stored in units of 1KHz.
  5488. * Hence the bw of each lane in terms of the mode signal
  5489. * is:
  5490. */
  5491. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5492. fdi_dotclock = adjusted_mode->crtc_clock;
  5493. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5494. pipe_config->pipe_bpp);
  5495. pipe_config->fdi_lanes = lane;
  5496. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5497. link_bw, &pipe_config->fdi_m_n);
  5498. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5499. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5500. pipe_config->pipe_bpp -= 2*3;
  5501. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5502. pipe_config->pipe_bpp);
  5503. needs_recompute = true;
  5504. pipe_config->bw_constrained = true;
  5505. goto retry;
  5506. }
  5507. if (needs_recompute)
  5508. return RETRY;
  5509. return ret;
  5510. }
  5511. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5512. struct intel_crtc_state *pipe_config)
  5513. {
  5514. if (pipe_config->pipe_bpp > 24)
  5515. return false;
  5516. /* HSW can handle pixel rate up to cdclk? */
  5517. if (IS_HASWELL(dev_priv))
  5518. return true;
  5519. /*
  5520. * We compare against max which means we must take
  5521. * the increased cdclk requirement into account when
  5522. * calculating the new cdclk.
  5523. *
  5524. * Should measure whether using a lower cdclk w/o IPS
  5525. */
  5526. return ilk_pipe_pixel_rate(pipe_config) <=
  5527. dev_priv->max_cdclk_freq * 95 / 100;
  5528. }
  5529. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5530. struct intel_crtc_state *pipe_config)
  5531. {
  5532. struct drm_device *dev = crtc->base.dev;
  5533. struct drm_i915_private *dev_priv = to_i915(dev);
  5534. pipe_config->ips_enabled = i915.enable_ips &&
  5535. hsw_crtc_supports_ips(crtc) &&
  5536. pipe_config_supports_ips(dev_priv, pipe_config);
  5537. }
  5538. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5539. {
  5540. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5541. /* GDG double wide on either pipe, otherwise pipe A only */
  5542. return INTEL_INFO(dev_priv)->gen < 4 &&
  5543. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5544. }
  5545. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5546. struct intel_crtc_state *pipe_config)
  5547. {
  5548. struct drm_device *dev = crtc->base.dev;
  5549. struct drm_i915_private *dev_priv = to_i915(dev);
  5550. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5551. int clock_limit = dev_priv->max_dotclk_freq;
  5552. if (INTEL_INFO(dev)->gen < 4) {
  5553. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5554. /*
  5555. * Enable double wide mode when the dot clock
  5556. * is > 90% of the (display) core speed.
  5557. */
  5558. if (intel_crtc_supports_double_wide(crtc) &&
  5559. adjusted_mode->crtc_clock > clock_limit) {
  5560. clock_limit = dev_priv->max_dotclk_freq;
  5561. pipe_config->double_wide = true;
  5562. }
  5563. }
  5564. if (adjusted_mode->crtc_clock > clock_limit) {
  5565. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5566. adjusted_mode->crtc_clock, clock_limit,
  5567. yesno(pipe_config->double_wide));
  5568. return -EINVAL;
  5569. }
  5570. /*
  5571. * Pipe horizontal size must be even in:
  5572. * - DVO ganged mode
  5573. * - LVDS dual channel mode
  5574. * - Double wide pipe
  5575. */
  5576. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5577. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5578. pipe_config->pipe_src_w &= ~1;
  5579. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5580. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5581. */
  5582. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5583. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5584. return -EINVAL;
  5585. if (HAS_IPS(dev))
  5586. hsw_compute_ips_config(crtc, pipe_config);
  5587. if (pipe_config->has_pch_encoder)
  5588. return ironlake_fdi_compute_config(crtc, pipe_config);
  5589. return 0;
  5590. }
  5591. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5592. {
  5593. struct drm_i915_private *dev_priv = to_i915(dev);
  5594. uint32_t cdctl;
  5595. skl_dpll0_update(dev_priv);
  5596. if (dev_priv->cdclk_pll.vco == 0)
  5597. return dev_priv->cdclk_pll.ref;
  5598. cdctl = I915_READ(CDCLK_CTL);
  5599. if (dev_priv->cdclk_pll.vco == 8640000) {
  5600. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5601. case CDCLK_FREQ_450_432:
  5602. return 432000;
  5603. case CDCLK_FREQ_337_308:
  5604. return 308571;
  5605. case CDCLK_FREQ_540:
  5606. return 540000;
  5607. case CDCLK_FREQ_675_617:
  5608. return 617143;
  5609. default:
  5610. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5611. }
  5612. } else {
  5613. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5614. case CDCLK_FREQ_450_432:
  5615. return 450000;
  5616. case CDCLK_FREQ_337_308:
  5617. return 337500;
  5618. case CDCLK_FREQ_540:
  5619. return 540000;
  5620. case CDCLK_FREQ_675_617:
  5621. return 675000;
  5622. default:
  5623. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5624. }
  5625. }
  5626. return dev_priv->cdclk_pll.ref;
  5627. }
  5628. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5629. {
  5630. u32 val;
  5631. dev_priv->cdclk_pll.ref = 19200;
  5632. dev_priv->cdclk_pll.vco = 0;
  5633. val = I915_READ(BXT_DE_PLL_ENABLE);
  5634. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  5635. return;
  5636. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  5637. return;
  5638. val = I915_READ(BXT_DE_PLL_CTL);
  5639. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5640. dev_priv->cdclk_pll.ref;
  5641. }
  5642. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5643. {
  5644. struct drm_i915_private *dev_priv = to_i915(dev);
  5645. u32 divider;
  5646. int div, vco;
  5647. bxt_de_pll_update(dev_priv);
  5648. vco = dev_priv->cdclk_pll.vco;
  5649. if (vco == 0)
  5650. return dev_priv->cdclk_pll.ref;
  5651. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  5652. switch (divider) {
  5653. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5654. div = 2;
  5655. break;
  5656. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5657. div = 3;
  5658. break;
  5659. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5660. div = 4;
  5661. break;
  5662. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5663. div = 8;
  5664. break;
  5665. default:
  5666. MISSING_CASE(divider);
  5667. return dev_priv->cdclk_pll.ref;
  5668. }
  5669. return DIV_ROUND_CLOSEST(vco, div);
  5670. }
  5671. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5672. {
  5673. struct drm_i915_private *dev_priv = to_i915(dev);
  5674. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5675. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5676. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5677. return 800000;
  5678. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5679. return 450000;
  5680. else if (freq == LCPLL_CLK_FREQ_450)
  5681. return 450000;
  5682. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5683. return 540000;
  5684. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5685. return 337500;
  5686. else
  5687. return 675000;
  5688. }
  5689. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5690. {
  5691. struct drm_i915_private *dev_priv = to_i915(dev);
  5692. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5693. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5694. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5695. return 800000;
  5696. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5697. return 450000;
  5698. else if (freq == LCPLL_CLK_FREQ_450)
  5699. return 450000;
  5700. else if (IS_HSW_ULT(dev))
  5701. return 337500;
  5702. else
  5703. return 540000;
  5704. }
  5705. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5706. {
  5707. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5708. CCK_DISPLAY_CLOCK_CONTROL);
  5709. }
  5710. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5711. {
  5712. return 450000;
  5713. }
  5714. static int i945_get_display_clock_speed(struct drm_device *dev)
  5715. {
  5716. return 400000;
  5717. }
  5718. static int i915_get_display_clock_speed(struct drm_device *dev)
  5719. {
  5720. return 333333;
  5721. }
  5722. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5723. {
  5724. return 200000;
  5725. }
  5726. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5727. {
  5728. u16 gcfgc = 0;
  5729. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5730. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5731. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5732. return 266667;
  5733. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5734. return 333333;
  5735. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5736. return 444444;
  5737. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5738. return 200000;
  5739. default:
  5740. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5741. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5742. return 133333;
  5743. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5744. return 166667;
  5745. }
  5746. }
  5747. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5748. {
  5749. u16 gcfgc = 0;
  5750. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5751. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5752. return 133333;
  5753. else {
  5754. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5755. case GC_DISPLAY_CLOCK_333_MHZ:
  5756. return 333333;
  5757. default:
  5758. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5759. return 190000;
  5760. }
  5761. }
  5762. }
  5763. static int i865_get_display_clock_speed(struct drm_device *dev)
  5764. {
  5765. return 266667;
  5766. }
  5767. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5768. {
  5769. u16 hpllcc = 0;
  5770. /*
  5771. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5772. * encoding is different :(
  5773. * FIXME is this the right way to detect 852GM/852GMV?
  5774. */
  5775. if (dev->pdev->revision == 0x1)
  5776. return 133333;
  5777. pci_bus_read_config_word(dev->pdev->bus,
  5778. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5779. /* Assume that the hardware is in the high speed state. This
  5780. * should be the default.
  5781. */
  5782. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5783. case GC_CLOCK_133_200:
  5784. case GC_CLOCK_133_200_2:
  5785. case GC_CLOCK_100_200:
  5786. return 200000;
  5787. case GC_CLOCK_166_250:
  5788. return 250000;
  5789. case GC_CLOCK_100_133:
  5790. return 133333;
  5791. case GC_CLOCK_133_266:
  5792. case GC_CLOCK_133_266_2:
  5793. case GC_CLOCK_166_266:
  5794. return 266667;
  5795. }
  5796. /* Shouldn't happen */
  5797. return 0;
  5798. }
  5799. static int i830_get_display_clock_speed(struct drm_device *dev)
  5800. {
  5801. return 133333;
  5802. }
  5803. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5804. {
  5805. struct drm_i915_private *dev_priv = to_i915(dev);
  5806. static const unsigned int blb_vco[8] = {
  5807. [0] = 3200000,
  5808. [1] = 4000000,
  5809. [2] = 5333333,
  5810. [3] = 4800000,
  5811. [4] = 6400000,
  5812. };
  5813. static const unsigned int pnv_vco[8] = {
  5814. [0] = 3200000,
  5815. [1] = 4000000,
  5816. [2] = 5333333,
  5817. [3] = 4800000,
  5818. [4] = 2666667,
  5819. };
  5820. static const unsigned int cl_vco[8] = {
  5821. [0] = 3200000,
  5822. [1] = 4000000,
  5823. [2] = 5333333,
  5824. [3] = 6400000,
  5825. [4] = 3333333,
  5826. [5] = 3566667,
  5827. [6] = 4266667,
  5828. };
  5829. static const unsigned int elk_vco[8] = {
  5830. [0] = 3200000,
  5831. [1] = 4000000,
  5832. [2] = 5333333,
  5833. [3] = 4800000,
  5834. };
  5835. static const unsigned int ctg_vco[8] = {
  5836. [0] = 3200000,
  5837. [1] = 4000000,
  5838. [2] = 5333333,
  5839. [3] = 6400000,
  5840. [4] = 2666667,
  5841. [5] = 4266667,
  5842. };
  5843. const unsigned int *vco_table;
  5844. unsigned int vco;
  5845. uint8_t tmp = 0;
  5846. /* FIXME other chipsets? */
  5847. if (IS_GM45(dev))
  5848. vco_table = ctg_vco;
  5849. else if (IS_G4X(dev))
  5850. vco_table = elk_vco;
  5851. else if (IS_CRESTLINE(dev))
  5852. vco_table = cl_vco;
  5853. else if (IS_PINEVIEW(dev))
  5854. vco_table = pnv_vco;
  5855. else if (IS_G33(dev))
  5856. vco_table = blb_vco;
  5857. else
  5858. return 0;
  5859. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5860. vco = vco_table[tmp & 0x7];
  5861. if (vco == 0)
  5862. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5863. else
  5864. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5865. return vco;
  5866. }
  5867. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5868. {
  5869. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5870. uint16_t tmp = 0;
  5871. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5872. cdclk_sel = (tmp >> 12) & 0x1;
  5873. switch (vco) {
  5874. case 2666667:
  5875. case 4000000:
  5876. case 5333333:
  5877. return cdclk_sel ? 333333 : 222222;
  5878. case 3200000:
  5879. return cdclk_sel ? 320000 : 228571;
  5880. default:
  5881. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5882. return 222222;
  5883. }
  5884. }
  5885. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5886. {
  5887. static const uint8_t div_3200[] = { 16, 10, 8 };
  5888. static const uint8_t div_4000[] = { 20, 12, 10 };
  5889. static const uint8_t div_5333[] = { 24, 16, 14 };
  5890. const uint8_t *div_table;
  5891. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5892. uint16_t tmp = 0;
  5893. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5894. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5895. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5896. goto fail;
  5897. switch (vco) {
  5898. case 3200000:
  5899. div_table = div_3200;
  5900. break;
  5901. case 4000000:
  5902. div_table = div_4000;
  5903. break;
  5904. case 5333333:
  5905. div_table = div_5333;
  5906. break;
  5907. default:
  5908. goto fail;
  5909. }
  5910. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5911. fail:
  5912. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5913. return 200000;
  5914. }
  5915. static int g33_get_display_clock_speed(struct drm_device *dev)
  5916. {
  5917. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5918. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5919. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5920. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5921. const uint8_t *div_table;
  5922. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5923. uint16_t tmp = 0;
  5924. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5925. cdclk_sel = (tmp >> 4) & 0x7;
  5926. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5927. goto fail;
  5928. switch (vco) {
  5929. case 3200000:
  5930. div_table = div_3200;
  5931. break;
  5932. case 4000000:
  5933. div_table = div_4000;
  5934. break;
  5935. case 4800000:
  5936. div_table = div_4800;
  5937. break;
  5938. case 5333333:
  5939. div_table = div_5333;
  5940. break;
  5941. default:
  5942. goto fail;
  5943. }
  5944. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5945. fail:
  5946. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5947. return 190476;
  5948. }
  5949. static void
  5950. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5951. {
  5952. while (*num > DATA_LINK_M_N_MASK ||
  5953. *den > DATA_LINK_M_N_MASK) {
  5954. *num >>= 1;
  5955. *den >>= 1;
  5956. }
  5957. }
  5958. static void compute_m_n(unsigned int m, unsigned int n,
  5959. uint32_t *ret_m, uint32_t *ret_n)
  5960. {
  5961. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5962. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5963. intel_reduce_m_n_ratio(ret_m, ret_n);
  5964. }
  5965. void
  5966. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5967. int pixel_clock, int link_clock,
  5968. struct intel_link_m_n *m_n)
  5969. {
  5970. m_n->tu = 64;
  5971. compute_m_n(bits_per_pixel * pixel_clock,
  5972. link_clock * nlanes * 8,
  5973. &m_n->gmch_m, &m_n->gmch_n);
  5974. compute_m_n(pixel_clock, link_clock,
  5975. &m_n->link_m, &m_n->link_n);
  5976. }
  5977. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5978. {
  5979. if (i915.panel_use_ssc >= 0)
  5980. return i915.panel_use_ssc != 0;
  5981. return dev_priv->vbt.lvds_use_ssc
  5982. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5983. }
  5984. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5985. {
  5986. return (1 << dpll->n) << 16 | dpll->m2;
  5987. }
  5988. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5989. {
  5990. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5991. }
  5992. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5993. struct intel_crtc_state *crtc_state,
  5994. struct dpll *reduced_clock)
  5995. {
  5996. struct drm_device *dev = crtc->base.dev;
  5997. u32 fp, fp2 = 0;
  5998. if (IS_PINEVIEW(dev)) {
  5999. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6000. if (reduced_clock)
  6001. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6002. } else {
  6003. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6004. if (reduced_clock)
  6005. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6006. }
  6007. crtc_state->dpll_hw_state.fp0 = fp;
  6008. crtc->lowfreq_avail = false;
  6009. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6010. reduced_clock) {
  6011. crtc_state->dpll_hw_state.fp1 = fp2;
  6012. crtc->lowfreq_avail = true;
  6013. } else {
  6014. crtc_state->dpll_hw_state.fp1 = fp;
  6015. }
  6016. }
  6017. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6018. pipe)
  6019. {
  6020. u32 reg_val;
  6021. /*
  6022. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6023. * and set it to a reasonable value instead.
  6024. */
  6025. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6026. reg_val &= 0xffffff00;
  6027. reg_val |= 0x00000030;
  6028. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6029. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6030. reg_val &= 0x8cffffff;
  6031. reg_val = 0x8c000000;
  6032. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6033. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6034. reg_val &= 0xffffff00;
  6035. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6036. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6037. reg_val &= 0x00ffffff;
  6038. reg_val |= 0xb0000000;
  6039. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6040. }
  6041. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6042. struct intel_link_m_n *m_n)
  6043. {
  6044. struct drm_device *dev = crtc->base.dev;
  6045. struct drm_i915_private *dev_priv = to_i915(dev);
  6046. int pipe = crtc->pipe;
  6047. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6048. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6049. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6050. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6051. }
  6052. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6053. struct intel_link_m_n *m_n,
  6054. struct intel_link_m_n *m2_n2)
  6055. {
  6056. struct drm_device *dev = crtc->base.dev;
  6057. struct drm_i915_private *dev_priv = to_i915(dev);
  6058. int pipe = crtc->pipe;
  6059. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6060. if (INTEL_INFO(dev)->gen >= 5) {
  6061. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6062. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6063. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6064. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6065. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6066. * for gen < 8) and if DRRS is supported (to make sure the
  6067. * registers are not unnecessarily accessed).
  6068. */
  6069. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6070. crtc->config->has_drrs) {
  6071. I915_WRITE(PIPE_DATA_M2(transcoder),
  6072. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6073. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6074. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6075. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6076. }
  6077. } else {
  6078. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6079. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6080. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6081. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6082. }
  6083. }
  6084. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6085. {
  6086. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6087. if (m_n == M1_N1) {
  6088. dp_m_n = &crtc->config->dp_m_n;
  6089. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6090. } else if (m_n == M2_N2) {
  6091. /*
  6092. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6093. * needs to be programmed into M1_N1.
  6094. */
  6095. dp_m_n = &crtc->config->dp_m2_n2;
  6096. } else {
  6097. DRM_ERROR("Unsupported divider value\n");
  6098. return;
  6099. }
  6100. if (crtc->config->has_pch_encoder)
  6101. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6102. else
  6103. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6104. }
  6105. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6106. struct intel_crtc_state *pipe_config)
  6107. {
  6108. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6109. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6110. if (crtc->pipe != PIPE_A)
  6111. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6112. /* DPLL not used with DSI, but still need the rest set up */
  6113. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6114. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6115. DPLL_EXT_BUFFER_ENABLE_VLV;
  6116. pipe_config->dpll_hw_state.dpll_md =
  6117. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6118. }
  6119. static void chv_compute_dpll(struct intel_crtc *crtc,
  6120. struct intel_crtc_state *pipe_config)
  6121. {
  6122. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  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. pipe_config->dpll_hw_state.dpll_md =
  6130. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6131. }
  6132. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6133. const struct intel_crtc_state *pipe_config)
  6134. {
  6135. struct drm_device *dev = crtc->base.dev;
  6136. struct drm_i915_private *dev_priv = to_i915(dev);
  6137. enum pipe pipe = crtc->pipe;
  6138. u32 mdiv;
  6139. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6140. u32 coreclk, reg_val;
  6141. /* Enable Refclk */
  6142. I915_WRITE(DPLL(pipe),
  6143. pipe_config->dpll_hw_state.dpll &
  6144. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6145. /* No need to actually set up the DPLL with DSI */
  6146. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6147. return;
  6148. mutex_lock(&dev_priv->sb_lock);
  6149. bestn = pipe_config->dpll.n;
  6150. bestm1 = pipe_config->dpll.m1;
  6151. bestm2 = pipe_config->dpll.m2;
  6152. bestp1 = pipe_config->dpll.p1;
  6153. bestp2 = pipe_config->dpll.p2;
  6154. /* See eDP HDMI DPIO driver vbios notes doc */
  6155. /* PLL B needs special handling */
  6156. if (pipe == PIPE_B)
  6157. vlv_pllb_recal_opamp(dev_priv, pipe);
  6158. /* Set up Tx target for periodic Rcomp update */
  6159. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6160. /* Disable target IRef on PLL */
  6161. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6162. reg_val &= 0x00ffffff;
  6163. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6164. /* Disable fast lock */
  6165. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6166. /* Set idtafcrecal before PLL is enabled */
  6167. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6168. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6169. mdiv |= ((bestn << DPIO_N_SHIFT));
  6170. mdiv |= (1 << DPIO_K_SHIFT);
  6171. /*
  6172. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6173. * but we don't support that).
  6174. * Note: don't use the DAC post divider as it seems unstable.
  6175. */
  6176. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6177. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6178. mdiv |= DPIO_ENABLE_CALIBRATION;
  6179. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6180. /* Set HBR and RBR LPF coefficients */
  6181. if (pipe_config->port_clock == 162000 ||
  6182. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6183. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6184. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6185. 0x009f0003);
  6186. else
  6187. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6188. 0x00d0000f);
  6189. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6190. /* Use SSC source */
  6191. if (pipe == PIPE_A)
  6192. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6193. 0x0df40000);
  6194. else
  6195. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6196. 0x0df70000);
  6197. } else { /* HDMI or VGA */
  6198. /* Use bend source */
  6199. if (pipe == PIPE_A)
  6200. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6201. 0x0df70000);
  6202. else
  6203. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6204. 0x0df40000);
  6205. }
  6206. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6207. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6208. if (intel_crtc_has_dp_encoder(crtc->config))
  6209. coreclk |= 0x01000000;
  6210. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6211. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6212. mutex_unlock(&dev_priv->sb_lock);
  6213. }
  6214. static void chv_prepare_pll(struct intel_crtc *crtc,
  6215. const struct intel_crtc_state *pipe_config)
  6216. {
  6217. struct drm_device *dev = crtc->base.dev;
  6218. struct drm_i915_private *dev_priv = to_i915(dev);
  6219. enum pipe pipe = crtc->pipe;
  6220. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6221. u32 loopfilter, tribuf_calcntr;
  6222. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6223. u32 dpio_val;
  6224. int vco;
  6225. /* Enable Refclk and SSC */
  6226. I915_WRITE(DPLL(pipe),
  6227. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6228. /* No need to actually set up the DPLL with DSI */
  6229. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6230. return;
  6231. bestn = pipe_config->dpll.n;
  6232. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6233. bestm1 = pipe_config->dpll.m1;
  6234. bestm2 = pipe_config->dpll.m2 >> 22;
  6235. bestp1 = pipe_config->dpll.p1;
  6236. bestp2 = pipe_config->dpll.p2;
  6237. vco = pipe_config->dpll.vco;
  6238. dpio_val = 0;
  6239. loopfilter = 0;
  6240. mutex_lock(&dev_priv->sb_lock);
  6241. /* p1 and p2 divider */
  6242. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6243. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6244. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6245. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6246. 1 << DPIO_CHV_K_DIV_SHIFT);
  6247. /* Feedback post-divider - m2 */
  6248. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6249. /* Feedback refclk divider - n and m1 */
  6250. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6251. DPIO_CHV_M1_DIV_BY_2 |
  6252. 1 << DPIO_CHV_N_DIV_SHIFT);
  6253. /* M2 fraction division */
  6254. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6255. /* M2 fraction division enable */
  6256. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6257. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6258. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6259. if (bestm2_frac)
  6260. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6261. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6262. /* Program digital lock detect threshold */
  6263. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6264. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6265. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6266. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6267. if (!bestm2_frac)
  6268. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6269. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6270. /* Loop filter */
  6271. if (vco == 5400000) {
  6272. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6273. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6274. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6275. tribuf_calcntr = 0x9;
  6276. } else if (vco <= 6200000) {
  6277. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6278. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6279. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6280. tribuf_calcntr = 0x9;
  6281. } else if (vco <= 6480000) {
  6282. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6283. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6284. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6285. tribuf_calcntr = 0x8;
  6286. } else {
  6287. /* Not supported. Apply the same limits as in the max case */
  6288. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6289. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6290. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6291. tribuf_calcntr = 0;
  6292. }
  6293. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6294. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6295. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6296. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6297. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6298. /* AFC Recal */
  6299. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6300. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6301. DPIO_AFC_RECAL);
  6302. mutex_unlock(&dev_priv->sb_lock);
  6303. }
  6304. /**
  6305. * vlv_force_pll_on - forcibly enable just the PLL
  6306. * @dev_priv: i915 private structure
  6307. * @pipe: pipe PLL to enable
  6308. * @dpll: PLL configuration
  6309. *
  6310. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6311. * in cases where we need the PLL enabled even when @pipe is not going to
  6312. * be enabled.
  6313. */
  6314. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6315. const struct dpll *dpll)
  6316. {
  6317. struct intel_crtc *crtc =
  6318. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6319. struct intel_crtc_state *pipe_config;
  6320. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6321. if (!pipe_config)
  6322. return -ENOMEM;
  6323. pipe_config->base.crtc = &crtc->base;
  6324. pipe_config->pixel_multiplier = 1;
  6325. pipe_config->dpll = *dpll;
  6326. if (IS_CHERRYVIEW(dev)) {
  6327. chv_compute_dpll(crtc, pipe_config);
  6328. chv_prepare_pll(crtc, pipe_config);
  6329. chv_enable_pll(crtc, pipe_config);
  6330. } else {
  6331. vlv_compute_dpll(crtc, pipe_config);
  6332. vlv_prepare_pll(crtc, pipe_config);
  6333. vlv_enable_pll(crtc, pipe_config);
  6334. }
  6335. kfree(pipe_config);
  6336. return 0;
  6337. }
  6338. /**
  6339. * vlv_force_pll_off - forcibly disable just the PLL
  6340. * @dev_priv: i915 private structure
  6341. * @pipe: pipe PLL to disable
  6342. *
  6343. * Disable the PLL for @pipe. To be used in cases where we need
  6344. * the PLL enabled even when @pipe is not going to be enabled.
  6345. */
  6346. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6347. {
  6348. if (IS_CHERRYVIEW(dev))
  6349. chv_disable_pll(to_i915(dev), pipe);
  6350. else
  6351. vlv_disable_pll(to_i915(dev), pipe);
  6352. }
  6353. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6354. struct intel_crtc_state *crtc_state,
  6355. struct dpll *reduced_clock)
  6356. {
  6357. struct drm_device *dev = crtc->base.dev;
  6358. struct drm_i915_private *dev_priv = to_i915(dev);
  6359. u32 dpll;
  6360. struct dpll *clock = &crtc_state->dpll;
  6361. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6362. dpll = DPLL_VGA_MODE_DIS;
  6363. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6364. dpll |= DPLLB_MODE_LVDS;
  6365. else
  6366. dpll |= DPLLB_MODE_DAC_SERIAL;
  6367. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6368. dpll |= (crtc_state->pixel_multiplier - 1)
  6369. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6370. }
  6371. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6372. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6373. dpll |= DPLL_SDVO_HIGH_SPEED;
  6374. if (intel_crtc_has_dp_encoder(crtc_state))
  6375. dpll |= DPLL_SDVO_HIGH_SPEED;
  6376. /* compute bitmask from p1 value */
  6377. if (IS_PINEVIEW(dev))
  6378. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6379. else {
  6380. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6381. if (IS_G4X(dev) && reduced_clock)
  6382. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6383. }
  6384. switch (clock->p2) {
  6385. case 5:
  6386. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6387. break;
  6388. case 7:
  6389. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6390. break;
  6391. case 10:
  6392. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6393. break;
  6394. case 14:
  6395. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6396. break;
  6397. }
  6398. if (INTEL_INFO(dev)->gen >= 4)
  6399. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6400. if (crtc_state->sdvo_tv_clock)
  6401. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6402. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6403. intel_panel_use_ssc(dev_priv))
  6404. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6405. else
  6406. dpll |= PLL_REF_INPUT_DREFCLK;
  6407. dpll |= DPLL_VCO_ENABLE;
  6408. crtc_state->dpll_hw_state.dpll = dpll;
  6409. if (INTEL_INFO(dev)->gen >= 4) {
  6410. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6411. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6412. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6413. }
  6414. }
  6415. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6416. struct intel_crtc_state *crtc_state,
  6417. struct dpll *reduced_clock)
  6418. {
  6419. struct drm_device *dev = crtc->base.dev;
  6420. struct drm_i915_private *dev_priv = to_i915(dev);
  6421. u32 dpll;
  6422. struct dpll *clock = &crtc_state->dpll;
  6423. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6424. dpll = DPLL_VGA_MODE_DIS;
  6425. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6426. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6427. } else {
  6428. if (clock->p1 == 2)
  6429. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6430. else
  6431. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6432. if (clock->p2 == 4)
  6433. dpll |= PLL_P2_DIVIDE_BY_4;
  6434. }
  6435. if (!IS_I830(dev) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6436. dpll |= DPLL_DVO_2X_MODE;
  6437. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6438. intel_panel_use_ssc(dev_priv))
  6439. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6440. else
  6441. dpll |= PLL_REF_INPUT_DREFCLK;
  6442. dpll |= DPLL_VCO_ENABLE;
  6443. crtc_state->dpll_hw_state.dpll = dpll;
  6444. }
  6445. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6446. {
  6447. struct drm_device *dev = intel_crtc->base.dev;
  6448. struct drm_i915_private *dev_priv = to_i915(dev);
  6449. enum pipe pipe = intel_crtc->pipe;
  6450. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6451. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6452. uint32_t crtc_vtotal, crtc_vblank_end;
  6453. int vsyncshift = 0;
  6454. /* We need to be careful not to changed the adjusted mode, for otherwise
  6455. * the hw state checker will get angry at the mismatch. */
  6456. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6457. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6458. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6459. /* the chip adds 2 halflines automatically */
  6460. crtc_vtotal -= 1;
  6461. crtc_vblank_end -= 1;
  6462. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6463. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6464. else
  6465. vsyncshift = adjusted_mode->crtc_hsync_start -
  6466. adjusted_mode->crtc_htotal / 2;
  6467. if (vsyncshift < 0)
  6468. vsyncshift += adjusted_mode->crtc_htotal;
  6469. }
  6470. if (INTEL_INFO(dev)->gen > 3)
  6471. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6472. I915_WRITE(HTOTAL(cpu_transcoder),
  6473. (adjusted_mode->crtc_hdisplay - 1) |
  6474. ((adjusted_mode->crtc_htotal - 1) << 16));
  6475. I915_WRITE(HBLANK(cpu_transcoder),
  6476. (adjusted_mode->crtc_hblank_start - 1) |
  6477. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6478. I915_WRITE(HSYNC(cpu_transcoder),
  6479. (adjusted_mode->crtc_hsync_start - 1) |
  6480. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6481. I915_WRITE(VTOTAL(cpu_transcoder),
  6482. (adjusted_mode->crtc_vdisplay - 1) |
  6483. ((crtc_vtotal - 1) << 16));
  6484. I915_WRITE(VBLANK(cpu_transcoder),
  6485. (adjusted_mode->crtc_vblank_start - 1) |
  6486. ((crtc_vblank_end - 1) << 16));
  6487. I915_WRITE(VSYNC(cpu_transcoder),
  6488. (adjusted_mode->crtc_vsync_start - 1) |
  6489. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6490. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6491. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6492. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6493. * bits. */
  6494. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6495. (pipe == PIPE_B || pipe == PIPE_C))
  6496. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6497. }
  6498. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6499. {
  6500. struct drm_device *dev = intel_crtc->base.dev;
  6501. struct drm_i915_private *dev_priv = to_i915(dev);
  6502. enum pipe pipe = intel_crtc->pipe;
  6503. /* pipesrc controls the size that is scaled from, which should
  6504. * always be the user's requested size.
  6505. */
  6506. I915_WRITE(PIPESRC(pipe),
  6507. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6508. (intel_crtc->config->pipe_src_h - 1));
  6509. }
  6510. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6511. struct intel_crtc_state *pipe_config)
  6512. {
  6513. struct drm_device *dev = crtc->base.dev;
  6514. struct drm_i915_private *dev_priv = to_i915(dev);
  6515. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6516. uint32_t tmp;
  6517. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6518. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6519. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6520. tmp = I915_READ(HBLANK(cpu_transcoder));
  6521. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6522. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6523. tmp = I915_READ(HSYNC(cpu_transcoder));
  6524. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6525. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6526. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6527. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6528. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6529. tmp = I915_READ(VBLANK(cpu_transcoder));
  6530. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6531. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6532. tmp = I915_READ(VSYNC(cpu_transcoder));
  6533. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6534. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6535. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6536. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6537. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6538. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6539. }
  6540. }
  6541. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6542. struct intel_crtc_state *pipe_config)
  6543. {
  6544. struct drm_device *dev = crtc->base.dev;
  6545. struct drm_i915_private *dev_priv = to_i915(dev);
  6546. u32 tmp;
  6547. tmp = I915_READ(PIPESRC(crtc->pipe));
  6548. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6549. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6550. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6551. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6552. }
  6553. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6554. struct intel_crtc_state *pipe_config)
  6555. {
  6556. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6557. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6558. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6559. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6560. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6561. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6562. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6563. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6564. mode->flags = pipe_config->base.adjusted_mode.flags;
  6565. mode->type = DRM_MODE_TYPE_DRIVER;
  6566. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6567. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6568. mode->hsync = drm_mode_hsync(mode);
  6569. mode->vrefresh = drm_mode_vrefresh(mode);
  6570. drm_mode_set_name(mode);
  6571. }
  6572. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6573. {
  6574. struct drm_device *dev = intel_crtc->base.dev;
  6575. struct drm_i915_private *dev_priv = to_i915(dev);
  6576. uint32_t pipeconf;
  6577. pipeconf = 0;
  6578. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6579. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6580. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6581. if (intel_crtc->config->double_wide)
  6582. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6583. /* only g4x and later have fancy bpc/dither controls */
  6584. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6585. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6586. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6587. pipeconf |= PIPECONF_DITHER_EN |
  6588. PIPECONF_DITHER_TYPE_SP;
  6589. switch (intel_crtc->config->pipe_bpp) {
  6590. case 18:
  6591. pipeconf |= PIPECONF_6BPC;
  6592. break;
  6593. case 24:
  6594. pipeconf |= PIPECONF_8BPC;
  6595. break;
  6596. case 30:
  6597. pipeconf |= PIPECONF_10BPC;
  6598. break;
  6599. default:
  6600. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6601. BUG();
  6602. }
  6603. }
  6604. if (HAS_PIPE_CXSR(dev)) {
  6605. if (intel_crtc->lowfreq_avail) {
  6606. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6607. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6608. } else {
  6609. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6610. }
  6611. }
  6612. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6613. if (INTEL_INFO(dev)->gen < 4 ||
  6614. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6615. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6616. else
  6617. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6618. } else
  6619. pipeconf |= PIPECONF_PROGRESSIVE;
  6620. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6621. intel_crtc->config->limited_color_range)
  6622. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6623. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6624. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6625. }
  6626. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6627. struct intel_crtc_state *crtc_state)
  6628. {
  6629. struct drm_device *dev = crtc->base.dev;
  6630. struct drm_i915_private *dev_priv = to_i915(dev);
  6631. const struct intel_limit *limit;
  6632. int refclk = 48000;
  6633. memset(&crtc_state->dpll_hw_state, 0,
  6634. sizeof(crtc_state->dpll_hw_state));
  6635. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6636. if (intel_panel_use_ssc(dev_priv)) {
  6637. refclk = dev_priv->vbt.lvds_ssc_freq;
  6638. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6639. }
  6640. limit = &intel_limits_i8xx_lvds;
  6641. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6642. limit = &intel_limits_i8xx_dvo;
  6643. } else {
  6644. limit = &intel_limits_i8xx_dac;
  6645. }
  6646. if (!crtc_state->clock_set &&
  6647. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6648. refclk, NULL, &crtc_state->dpll)) {
  6649. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6650. return -EINVAL;
  6651. }
  6652. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6653. return 0;
  6654. }
  6655. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6656. struct intel_crtc_state *crtc_state)
  6657. {
  6658. struct drm_device *dev = crtc->base.dev;
  6659. struct drm_i915_private *dev_priv = to_i915(dev);
  6660. const struct intel_limit *limit;
  6661. int refclk = 96000;
  6662. memset(&crtc_state->dpll_hw_state, 0,
  6663. sizeof(crtc_state->dpll_hw_state));
  6664. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6665. if (intel_panel_use_ssc(dev_priv)) {
  6666. refclk = dev_priv->vbt.lvds_ssc_freq;
  6667. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6668. }
  6669. if (intel_is_dual_link_lvds(dev))
  6670. limit = &intel_limits_g4x_dual_channel_lvds;
  6671. else
  6672. limit = &intel_limits_g4x_single_channel_lvds;
  6673. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6674. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6675. limit = &intel_limits_g4x_hdmi;
  6676. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6677. limit = &intel_limits_g4x_sdvo;
  6678. } else {
  6679. /* The option is for other outputs */
  6680. limit = &intel_limits_i9xx_sdvo;
  6681. }
  6682. if (!crtc_state->clock_set &&
  6683. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6684. refclk, NULL, &crtc_state->dpll)) {
  6685. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6686. return -EINVAL;
  6687. }
  6688. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6689. return 0;
  6690. }
  6691. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6692. struct intel_crtc_state *crtc_state)
  6693. {
  6694. struct drm_device *dev = crtc->base.dev;
  6695. struct drm_i915_private *dev_priv = to_i915(dev);
  6696. const struct intel_limit *limit;
  6697. int refclk = 96000;
  6698. memset(&crtc_state->dpll_hw_state, 0,
  6699. sizeof(crtc_state->dpll_hw_state));
  6700. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6701. if (intel_panel_use_ssc(dev_priv)) {
  6702. refclk = dev_priv->vbt.lvds_ssc_freq;
  6703. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6704. }
  6705. limit = &intel_limits_pineview_lvds;
  6706. } else {
  6707. limit = &intel_limits_pineview_sdvo;
  6708. }
  6709. if (!crtc_state->clock_set &&
  6710. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6711. refclk, NULL, &crtc_state->dpll)) {
  6712. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6713. return -EINVAL;
  6714. }
  6715. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6716. return 0;
  6717. }
  6718. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6719. struct intel_crtc_state *crtc_state)
  6720. {
  6721. struct drm_device *dev = crtc->base.dev;
  6722. struct drm_i915_private *dev_priv = to_i915(dev);
  6723. const struct intel_limit *limit;
  6724. int refclk = 96000;
  6725. memset(&crtc_state->dpll_hw_state, 0,
  6726. sizeof(crtc_state->dpll_hw_state));
  6727. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6728. if (intel_panel_use_ssc(dev_priv)) {
  6729. refclk = dev_priv->vbt.lvds_ssc_freq;
  6730. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6731. }
  6732. limit = &intel_limits_i9xx_lvds;
  6733. } else {
  6734. limit = &intel_limits_i9xx_sdvo;
  6735. }
  6736. if (!crtc_state->clock_set &&
  6737. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6738. refclk, NULL, &crtc_state->dpll)) {
  6739. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6740. return -EINVAL;
  6741. }
  6742. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6743. return 0;
  6744. }
  6745. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6746. struct intel_crtc_state *crtc_state)
  6747. {
  6748. int refclk = 100000;
  6749. const struct intel_limit *limit = &intel_limits_chv;
  6750. memset(&crtc_state->dpll_hw_state, 0,
  6751. sizeof(crtc_state->dpll_hw_state));
  6752. if (!crtc_state->clock_set &&
  6753. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6754. refclk, NULL, &crtc_state->dpll)) {
  6755. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6756. return -EINVAL;
  6757. }
  6758. chv_compute_dpll(crtc, crtc_state);
  6759. return 0;
  6760. }
  6761. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6762. struct intel_crtc_state *crtc_state)
  6763. {
  6764. int refclk = 100000;
  6765. const struct intel_limit *limit = &intel_limits_vlv;
  6766. memset(&crtc_state->dpll_hw_state, 0,
  6767. sizeof(crtc_state->dpll_hw_state));
  6768. if (!crtc_state->clock_set &&
  6769. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6770. refclk, NULL, &crtc_state->dpll)) {
  6771. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6772. return -EINVAL;
  6773. }
  6774. vlv_compute_dpll(crtc, crtc_state);
  6775. return 0;
  6776. }
  6777. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6778. struct intel_crtc_state *pipe_config)
  6779. {
  6780. struct drm_device *dev = crtc->base.dev;
  6781. struct drm_i915_private *dev_priv = to_i915(dev);
  6782. uint32_t tmp;
  6783. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6784. return;
  6785. tmp = I915_READ(PFIT_CONTROL);
  6786. if (!(tmp & PFIT_ENABLE))
  6787. return;
  6788. /* Check whether the pfit is attached to our pipe. */
  6789. if (INTEL_INFO(dev)->gen < 4) {
  6790. if (crtc->pipe != PIPE_B)
  6791. return;
  6792. } else {
  6793. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6794. return;
  6795. }
  6796. pipe_config->gmch_pfit.control = tmp;
  6797. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6798. }
  6799. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6800. struct intel_crtc_state *pipe_config)
  6801. {
  6802. struct drm_device *dev = crtc->base.dev;
  6803. struct drm_i915_private *dev_priv = to_i915(dev);
  6804. int pipe = pipe_config->cpu_transcoder;
  6805. struct dpll clock;
  6806. u32 mdiv;
  6807. int refclk = 100000;
  6808. /* In case of DSI, DPLL will not be used */
  6809. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6810. return;
  6811. mutex_lock(&dev_priv->sb_lock);
  6812. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6813. mutex_unlock(&dev_priv->sb_lock);
  6814. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6815. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6816. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6817. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6818. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6819. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6820. }
  6821. static void
  6822. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6823. struct intel_initial_plane_config *plane_config)
  6824. {
  6825. struct drm_device *dev = crtc->base.dev;
  6826. struct drm_i915_private *dev_priv = to_i915(dev);
  6827. u32 val, base, offset;
  6828. int pipe = crtc->pipe, plane = crtc->plane;
  6829. int fourcc, pixel_format;
  6830. unsigned int aligned_height;
  6831. struct drm_framebuffer *fb;
  6832. struct intel_framebuffer *intel_fb;
  6833. val = I915_READ(DSPCNTR(plane));
  6834. if (!(val & DISPLAY_PLANE_ENABLE))
  6835. return;
  6836. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6837. if (!intel_fb) {
  6838. DRM_DEBUG_KMS("failed to alloc fb\n");
  6839. return;
  6840. }
  6841. fb = &intel_fb->base;
  6842. if (INTEL_INFO(dev)->gen >= 4) {
  6843. if (val & DISPPLANE_TILED) {
  6844. plane_config->tiling = I915_TILING_X;
  6845. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6846. }
  6847. }
  6848. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6849. fourcc = i9xx_format_to_fourcc(pixel_format);
  6850. fb->pixel_format = fourcc;
  6851. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6852. if (INTEL_INFO(dev)->gen >= 4) {
  6853. if (plane_config->tiling)
  6854. offset = I915_READ(DSPTILEOFF(plane));
  6855. else
  6856. offset = I915_READ(DSPLINOFF(plane));
  6857. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6858. } else {
  6859. base = I915_READ(DSPADDR(plane));
  6860. }
  6861. plane_config->base = base;
  6862. val = I915_READ(PIPESRC(pipe));
  6863. fb->width = ((val >> 16) & 0xfff) + 1;
  6864. fb->height = ((val >> 0) & 0xfff) + 1;
  6865. val = I915_READ(DSPSTRIDE(pipe));
  6866. fb->pitches[0] = val & 0xffffffc0;
  6867. aligned_height = intel_fb_align_height(dev, fb->height,
  6868. fb->pixel_format,
  6869. fb->modifier[0]);
  6870. plane_config->size = fb->pitches[0] * aligned_height;
  6871. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6872. pipe_name(pipe), plane, fb->width, fb->height,
  6873. fb->bits_per_pixel, base, fb->pitches[0],
  6874. plane_config->size);
  6875. plane_config->fb = intel_fb;
  6876. }
  6877. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6878. struct intel_crtc_state *pipe_config)
  6879. {
  6880. struct drm_device *dev = crtc->base.dev;
  6881. struct drm_i915_private *dev_priv = to_i915(dev);
  6882. int pipe = pipe_config->cpu_transcoder;
  6883. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6884. struct dpll clock;
  6885. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6886. int refclk = 100000;
  6887. /* In case of DSI, DPLL will not be used */
  6888. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6889. return;
  6890. mutex_lock(&dev_priv->sb_lock);
  6891. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6892. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6893. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6894. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6895. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6896. mutex_unlock(&dev_priv->sb_lock);
  6897. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6898. clock.m2 = (pll_dw0 & 0xff) << 22;
  6899. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6900. clock.m2 |= pll_dw2 & 0x3fffff;
  6901. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6902. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6903. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6904. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6905. }
  6906. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6907. struct intel_crtc_state *pipe_config)
  6908. {
  6909. struct drm_device *dev = crtc->base.dev;
  6910. struct drm_i915_private *dev_priv = to_i915(dev);
  6911. enum intel_display_power_domain power_domain;
  6912. uint32_t tmp;
  6913. bool ret;
  6914. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6915. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6916. return false;
  6917. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6918. pipe_config->shared_dpll = NULL;
  6919. ret = false;
  6920. tmp = I915_READ(PIPECONF(crtc->pipe));
  6921. if (!(tmp & PIPECONF_ENABLE))
  6922. goto out;
  6923. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6924. switch (tmp & PIPECONF_BPC_MASK) {
  6925. case PIPECONF_6BPC:
  6926. pipe_config->pipe_bpp = 18;
  6927. break;
  6928. case PIPECONF_8BPC:
  6929. pipe_config->pipe_bpp = 24;
  6930. break;
  6931. case PIPECONF_10BPC:
  6932. pipe_config->pipe_bpp = 30;
  6933. break;
  6934. default:
  6935. break;
  6936. }
  6937. }
  6938. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6939. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6940. pipe_config->limited_color_range = true;
  6941. if (INTEL_INFO(dev)->gen < 4)
  6942. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6943. intel_get_pipe_timings(crtc, pipe_config);
  6944. intel_get_pipe_src_size(crtc, pipe_config);
  6945. i9xx_get_pfit_config(crtc, pipe_config);
  6946. if (INTEL_INFO(dev)->gen >= 4) {
  6947. /* No way to read it out on pipes B and C */
  6948. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6949. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6950. else
  6951. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6952. pipe_config->pixel_multiplier =
  6953. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6954. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6955. pipe_config->dpll_hw_state.dpll_md = tmp;
  6956. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6957. tmp = I915_READ(DPLL(crtc->pipe));
  6958. pipe_config->pixel_multiplier =
  6959. ((tmp & SDVO_MULTIPLIER_MASK)
  6960. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6961. } else {
  6962. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6963. * port and will be fixed up in the encoder->get_config
  6964. * function. */
  6965. pipe_config->pixel_multiplier = 1;
  6966. }
  6967. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6968. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6969. /*
  6970. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6971. * on 830. Filter it out here so that we don't
  6972. * report errors due to that.
  6973. */
  6974. if (IS_I830(dev))
  6975. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6976. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6977. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6978. } else {
  6979. /* Mask out read-only status bits. */
  6980. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6981. DPLL_PORTC_READY_MASK |
  6982. DPLL_PORTB_READY_MASK);
  6983. }
  6984. if (IS_CHERRYVIEW(dev))
  6985. chv_crtc_clock_get(crtc, pipe_config);
  6986. else if (IS_VALLEYVIEW(dev))
  6987. vlv_crtc_clock_get(crtc, pipe_config);
  6988. else
  6989. i9xx_crtc_clock_get(crtc, pipe_config);
  6990. /*
  6991. * Normally the dotclock is filled in by the encoder .get_config()
  6992. * but in case the pipe is enabled w/o any ports we need a sane
  6993. * default.
  6994. */
  6995. pipe_config->base.adjusted_mode.crtc_clock =
  6996. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6997. ret = true;
  6998. out:
  6999. intel_display_power_put(dev_priv, power_domain);
  7000. return ret;
  7001. }
  7002. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7003. {
  7004. struct drm_i915_private *dev_priv = to_i915(dev);
  7005. struct intel_encoder *encoder;
  7006. int i;
  7007. u32 val, final;
  7008. bool has_lvds = false;
  7009. bool has_cpu_edp = false;
  7010. bool has_panel = false;
  7011. bool has_ck505 = false;
  7012. bool can_ssc = false;
  7013. bool using_ssc_source = false;
  7014. /* We need to take the global config into account */
  7015. for_each_intel_encoder(dev, encoder) {
  7016. switch (encoder->type) {
  7017. case INTEL_OUTPUT_LVDS:
  7018. has_panel = true;
  7019. has_lvds = true;
  7020. break;
  7021. case INTEL_OUTPUT_EDP:
  7022. has_panel = true;
  7023. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7024. has_cpu_edp = true;
  7025. break;
  7026. default:
  7027. break;
  7028. }
  7029. }
  7030. if (HAS_PCH_IBX(dev)) {
  7031. has_ck505 = dev_priv->vbt.display_clock_mode;
  7032. can_ssc = has_ck505;
  7033. } else {
  7034. has_ck505 = false;
  7035. can_ssc = true;
  7036. }
  7037. /* Check if any DPLLs are using the SSC source */
  7038. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7039. u32 temp = I915_READ(PCH_DPLL(i));
  7040. if (!(temp & DPLL_VCO_ENABLE))
  7041. continue;
  7042. if ((temp & PLL_REF_INPUT_MASK) ==
  7043. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7044. using_ssc_source = true;
  7045. break;
  7046. }
  7047. }
  7048. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7049. has_panel, has_lvds, has_ck505, using_ssc_source);
  7050. /* Ironlake: try to setup display ref clock before DPLL
  7051. * enabling. This is only under driver's control after
  7052. * PCH B stepping, previous chipset stepping should be
  7053. * ignoring this setting.
  7054. */
  7055. val = I915_READ(PCH_DREF_CONTROL);
  7056. /* As we must carefully and slowly disable/enable each source in turn,
  7057. * compute the final state we want first and check if we need to
  7058. * make any changes at all.
  7059. */
  7060. final = val;
  7061. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7062. if (has_ck505)
  7063. final |= DREF_NONSPREAD_CK505_ENABLE;
  7064. else
  7065. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7066. final &= ~DREF_SSC_SOURCE_MASK;
  7067. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7068. final &= ~DREF_SSC1_ENABLE;
  7069. if (has_panel) {
  7070. final |= DREF_SSC_SOURCE_ENABLE;
  7071. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7072. final |= DREF_SSC1_ENABLE;
  7073. if (has_cpu_edp) {
  7074. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7075. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7076. else
  7077. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7078. } else
  7079. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7080. } else if (using_ssc_source) {
  7081. final |= DREF_SSC_SOURCE_ENABLE;
  7082. final |= DREF_SSC1_ENABLE;
  7083. }
  7084. if (final == val)
  7085. return;
  7086. /* Always enable nonspread source */
  7087. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7088. if (has_ck505)
  7089. val |= DREF_NONSPREAD_CK505_ENABLE;
  7090. else
  7091. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7092. if (has_panel) {
  7093. val &= ~DREF_SSC_SOURCE_MASK;
  7094. val |= DREF_SSC_SOURCE_ENABLE;
  7095. /* SSC must be turned on before enabling the CPU output */
  7096. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7097. DRM_DEBUG_KMS("Using SSC on panel\n");
  7098. val |= DREF_SSC1_ENABLE;
  7099. } else
  7100. val &= ~DREF_SSC1_ENABLE;
  7101. /* Get SSC going before enabling the outputs */
  7102. I915_WRITE(PCH_DREF_CONTROL, val);
  7103. POSTING_READ(PCH_DREF_CONTROL);
  7104. udelay(200);
  7105. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7106. /* Enable CPU source on CPU attached eDP */
  7107. if (has_cpu_edp) {
  7108. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7109. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7110. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7111. } else
  7112. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7113. } else
  7114. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7115. I915_WRITE(PCH_DREF_CONTROL, val);
  7116. POSTING_READ(PCH_DREF_CONTROL);
  7117. udelay(200);
  7118. } else {
  7119. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7120. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7121. /* Turn off CPU output */
  7122. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7123. I915_WRITE(PCH_DREF_CONTROL, val);
  7124. POSTING_READ(PCH_DREF_CONTROL);
  7125. udelay(200);
  7126. if (!using_ssc_source) {
  7127. DRM_DEBUG_KMS("Disabling SSC source\n");
  7128. /* Turn off the SSC source */
  7129. val &= ~DREF_SSC_SOURCE_MASK;
  7130. val |= DREF_SSC_SOURCE_DISABLE;
  7131. /* Turn off SSC1 */
  7132. val &= ~DREF_SSC1_ENABLE;
  7133. I915_WRITE(PCH_DREF_CONTROL, val);
  7134. POSTING_READ(PCH_DREF_CONTROL);
  7135. udelay(200);
  7136. }
  7137. }
  7138. BUG_ON(val != final);
  7139. }
  7140. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7141. {
  7142. uint32_t tmp;
  7143. tmp = I915_READ(SOUTH_CHICKEN2);
  7144. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7145. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7146. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7147. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7148. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7149. tmp = I915_READ(SOUTH_CHICKEN2);
  7150. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7151. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7152. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7153. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7154. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7155. }
  7156. /* WaMPhyProgramming:hsw */
  7157. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7158. {
  7159. uint32_t tmp;
  7160. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7161. tmp &= ~(0xFF << 24);
  7162. tmp |= (0x12 << 24);
  7163. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7164. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7165. tmp |= (1 << 11);
  7166. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7167. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7168. tmp |= (1 << 11);
  7169. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7170. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7171. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7172. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7173. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7174. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7175. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7176. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7177. tmp &= ~(7 << 13);
  7178. tmp |= (5 << 13);
  7179. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7180. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7181. tmp &= ~(7 << 13);
  7182. tmp |= (5 << 13);
  7183. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7184. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7185. tmp &= ~0xFF;
  7186. tmp |= 0x1C;
  7187. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7188. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7189. tmp &= ~0xFF;
  7190. tmp |= 0x1C;
  7191. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7192. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7193. tmp &= ~(0xFF << 16);
  7194. tmp |= (0x1C << 16);
  7195. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7196. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7197. tmp &= ~(0xFF << 16);
  7198. tmp |= (0x1C << 16);
  7199. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7200. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7201. tmp |= (1 << 27);
  7202. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7203. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7204. tmp |= (1 << 27);
  7205. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7206. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7207. tmp &= ~(0xF << 28);
  7208. tmp |= (4 << 28);
  7209. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7210. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7211. tmp &= ~(0xF << 28);
  7212. tmp |= (4 << 28);
  7213. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7214. }
  7215. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7216. * Programming" based on the parameters passed:
  7217. * - Sequence to enable CLKOUT_DP
  7218. * - Sequence to enable CLKOUT_DP without spread
  7219. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7220. */
  7221. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7222. bool with_fdi)
  7223. {
  7224. struct drm_i915_private *dev_priv = to_i915(dev);
  7225. uint32_t reg, tmp;
  7226. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7227. with_spread = true;
  7228. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7229. with_fdi = false;
  7230. mutex_lock(&dev_priv->sb_lock);
  7231. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7232. tmp &= ~SBI_SSCCTL_DISABLE;
  7233. tmp |= SBI_SSCCTL_PATHALT;
  7234. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7235. udelay(24);
  7236. if (with_spread) {
  7237. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7238. tmp &= ~SBI_SSCCTL_PATHALT;
  7239. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7240. if (with_fdi) {
  7241. lpt_reset_fdi_mphy(dev_priv);
  7242. lpt_program_fdi_mphy(dev_priv);
  7243. }
  7244. }
  7245. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7246. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7247. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7248. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7249. mutex_unlock(&dev_priv->sb_lock);
  7250. }
  7251. /* Sequence to disable CLKOUT_DP */
  7252. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7253. {
  7254. struct drm_i915_private *dev_priv = to_i915(dev);
  7255. uint32_t reg, tmp;
  7256. mutex_lock(&dev_priv->sb_lock);
  7257. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7258. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7259. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7260. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7261. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7262. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7263. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7264. tmp |= SBI_SSCCTL_PATHALT;
  7265. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7266. udelay(32);
  7267. }
  7268. tmp |= SBI_SSCCTL_DISABLE;
  7269. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7270. }
  7271. mutex_unlock(&dev_priv->sb_lock);
  7272. }
  7273. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7274. static const uint16_t sscdivintphase[] = {
  7275. [BEND_IDX( 50)] = 0x3B23,
  7276. [BEND_IDX( 45)] = 0x3B23,
  7277. [BEND_IDX( 40)] = 0x3C23,
  7278. [BEND_IDX( 35)] = 0x3C23,
  7279. [BEND_IDX( 30)] = 0x3D23,
  7280. [BEND_IDX( 25)] = 0x3D23,
  7281. [BEND_IDX( 20)] = 0x3E23,
  7282. [BEND_IDX( 15)] = 0x3E23,
  7283. [BEND_IDX( 10)] = 0x3F23,
  7284. [BEND_IDX( 5)] = 0x3F23,
  7285. [BEND_IDX( 0)] = 0x0025,
  7286. [BEND_IDX( -5)] = 0x0025,
  7287. [BEND_IDX(-10)] = 0x0125,
  7288. [BEND_IDX(-15)] = 0x0125,
  7289. [BEND_IDX(-20)] = 0x0225,
  7290. [BEND_IDX(-25)] = 0x0225,
  7291. [BEND_IDX(-30)] = 0x0325,
  7292. [BEND_IDX(-35)] = 0x0325,
  7293. [BEND_IDX(-40)] = 0x0425,
  7294. [BEND_IDX(-45)] = 0x0425,
  7295. [BEND_IDX(-50)] = 0x0525,
  7296. };
  7297. /*
  7298. * Bend CLKOUT_DP
  7299. * steps -50 to 50 inclusive, in steps of 5
  7300. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7301. * change in clock period = -(steps / 10) * 5.787 ps
  7302. */
  7303. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7304. {
  7305. uint32_t tmp;
  7306. int idx = BEND_IDX(steps);
  7307. if (WARN_ON(steps % 5 != 0))
  7308. return;
  7309. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7310. return;
  7311. mutex_lock(&dev_priv->sb_lock);
  7312. if (steps % 10 != 0)
  7313. tmp = 0xAAAAAAAB;
  7314. else
  7315. tmp = 0x00000000;
  7316. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7317. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7318. tmp &= 0xffff0000;
  7319. tmp |= sscdivintphase[idx];
  7320. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7321. mutex_unlock(&dev_priv->sb_lock);
  7322. }
  7323. #undef BEND_IDX
  7324. static void lpt_init_pch_refclk(struct drm_device *dev)
  7325. {
  7326. struct intel_encoder *encoder;
  7327. bool has_vga = false;
  7328. for_each_intel_encoder(dev, encoder) {
  7329. switch (encoder->type) {
  7330. case INTEL_OUTPUT_ANALOG:
  7331. has_vga = true;
  7332. break;
  7333. default:
  7334. break;
  7335. }
  7336. }
  7337. if (has_vga) {
  7338. lpt_bend_clkout_dp(to_i915(dev), 0);
  7339. lpt_enable_clkout_dp(dev, true, true);
  7340. } else {
  7341. lpt_disable_clkout_dp(dev);
  7342. }
  7343. }
  7344. /*
  7345. * Initialize reference clocks when the driver loads
  7346. */
  7347. void intel_init_pch_refclk(struct drm_device *dev)
  7348. {
  7349. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7350. ironlake_init_pch_refclk(dev);
  7351. else if (HAS_PCH_LPT(dev))
  7352. lpt_init_pch_refclk(dev);
  7353. }
  7354. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7355. {
  7356. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7357. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7358. int pipe = intel_crtc->pipe;
  7359. uint32_t val;
  7360. val = 0;
  7361. switch (intel_crtc->config->pipe_bpp) {
  7362. case 18:
  7363. val |= PIPECONF_6BPC;
  7364. break;
  7365. case 24:
  7366. val |= PIPECONF_8BPC;
  7367. break;
  7368. case 30:
  7369. val |= PIPECONF_10BPC;
  7370. break;
  7371. case 36:
  7372. val |= PIPECONF_12BPC;
  7373. break;
  7374. default:
  7375. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7376. BUG();
  7377. }
  7378. if (intel_crtc->config->dither)
  7379. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7380. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7381. val |= PIPECONF_INTERLACED_ILK;
  7382. else
  7383. val |= PIPECONF_PROGRESSIVE;
  7384. if (intel_crtc->config->limited_color_range)
  7385. val |= PIPECONF_COLOR_RANGE_SELECT;
  7386. I915_WRITE(PIPECONF(pipe), val);
  7387. POSTING_READ(PIPECONF(pipe));
  7388. }
  7389. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7390. {
  7391. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7392. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7393. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7394. u32 val = 0;
  7395. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7396. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7397. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7398. val |= PIPECONF_INTERLACED_ILK;
  7399. else
  7400. val |= PIPECONF_PROGRESSIVE;
  7401. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7402. POSTING_READ(PIPECONF(cpu_transcoder));
  7403. }
  7404. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7405. {
  7406. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7408. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7409. u32 val = 0;
  7410. switch (intel_crtc->config->pipe_bpp) {
  7411. case 18:
  7412. val |= PIPEMISC_DITHER_6_BPC;
  7413. break;
  7414. case 24:
  7415. val |= PIPEMISC_DITHER_8_BPC;
  7416. break;
  7417. case 30:
  7418. val |= PIPEMISC_DITHER_10_BPC;
  7419. break;
  7420. case 36:
  7421. val |= PIPEMISC_DITHER_12_BPC;
  7422. break;
  7423. default:
  7424. /* Case prevented by pipe_config_set_bpp. */
  7425. BUG();
  7426. }
  7427. if (intel_crtc->config->dither)
  7428. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7429. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7430. }
  7431. }
  7432. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7433. {
  7434. /*
  7435. * Account for spread spectrum to avoid
  7436. * oversubscribing the link. Max center spread
  7437. * is 2.5%; use 5% for safety's sake.
  7438. */
  7439. u32 bps = target_clock * bpp * 21 / 20;
  7440. return DIV_ROUND_UP(bps, link_bw * 8);
  7441. }
  7442. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7443. {
  7444. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7445. }
  7446. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7447. struct intel_crtc_state *crtc_state,
  7448. struct dpll *reduced_clock)
  7449. {
  7450. struct drm_crtc *crtc = &intel_crtc->base;
  7451. struct drm_device *dev = crtc->dev;
  7452. struct drm_i915_private *dev_priv = to_i915(dev);
  7453. u32 dpll, fp, fp2;
  7454. int factor;
  7455. /* Enable autotuning of the PLL clock (if permissible) */
  7456. factor = 21;
  7457. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7458. if ((intel_panel_use_ssc(dev_priv) &&
  7459. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7460. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7461. factor = 25;
  7462. } else if (crtc_state->sdvo_tv_clock)
  7463. factor = 20;
  7464. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7465. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7466. fp |= FP_CB_TUNE;
  7467. if (reduced_clock) {
  7468. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7469. if (reduced_clock->m < factor * reduced_clock->n)
  7470. fp2 |= FP_CB_TUNE;
  7471. } else {
  7472. fp2 = fp;
  7473. }
  7474. dpll = 0;
  7475. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7476. dpll |= DPLLB_MODE_LVDS;
  7477. else
  7478. dpll |= DPLLB_MODE_DAC_SERIAL;
  7479. dpll |= (crtc_state->pixel_multiplier - 1)
  7480. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7481. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7482. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7483. dpll |= DPLL_SDVO_HIGH_SPEED;
  7484. if (intel_crtc_has_dp_encoder(crtc_state))
  7485. dpll |= DPLL_SDVO_HIGH_SPEED;
  7486. /* compute bitmask from p1 value */
  7487. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7488. /* also FPA1 */
  7489. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7490. switch (crtc_state->dpll.p2) {
  7491. case 5:
  7492. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7493. break;
  7494. case 7:
  7495. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7496. break;
  7497. case 10:
  7498. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7499. break;
  7500. case 14:
  7501. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7502. break;
  7503. }
  7504. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7505. intel_panel_use_ssc(dev_priv))
  7506. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7507. else
  7508. dpll |= PLL_REF_INPUT_DREFCLK;
  7509. dpll |= DPLL_VCO_ENABLE;
  7510. crtc_state->dpll_hw_state.dpll = dpll;
  7511. crtc_state->dpll_hw_state.fp0 = fp;
  7512. crtc_state->dpll_hw_state.fp1 = fp2;
  7513. }
  7514. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7515. struct intel_crtc_state *crtc_state)
  7516. {
  7517. struct drm_device *dev = crtc->base.dev;
  7518. struct drm_i915_private *dev_priv = to_i915(dev);
  7519. struct dpll reduced_clock;
  7520. bool has_reduced_clock = false;
  7521. struct intel_shared_dpll *pll;
  7522. const struct intel_limit *limit;
  7523. int refclk = 120000;
  7524. memset(&crtc_state->dpll_hw_state, 0,
  7525. sizeof(crtc_state->dpll_hw_state));
  7526. crtc->lowfreq_avail = false;
  7527. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7528. if (!crtc_state->has_pch_encoder)
  7529. return 0;
  7530. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7531. if (intel_panel_use_ssc(dev_priv)) {
  7532. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7533. dev_priv->vbt.lvds_ssc_freq);
  7534. refclk = dev_priv->vbt.lvds_ssc_freq;
  7535. }
  7536. if (intel_is_dual_link_lvds(dev)) {
  7537. if (refclk == 100000)
  7538. limit = &intel_limits_ironlake_dual_lvds_100m;
  7539. else
  7540. limit = &intel_limits_ironlake_dual_lvds;
  7541. } else {
  7542. if (refclk == 100000)
  7543. limit = &intel_limits_ironlake_single_lvds_100m;
  7544. else
  7545. limit = &intel_limits_ironlake_single_lvds;
  7546. }
  7547. } else {
  7548. limit = &intel_limits_ironlake_dac;
  7549. }
  7550. if (!crtc_state->clock_set &&
  7551. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7552. refclk, NULL, &crtc_state->dpll)) {
  7553. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7554. return -EINVAL;
  7555. }
  7556. ironlake_compute_dpll(crtc, crtc_state,
  7557. has_reduced_clock ? &reduced_clock : NULL);
  7558. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7559. if (pll == NULL) {
  7560. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7561. pipe_name(crtc->pipe));
  7562. return -EINVAL;
  7563. }
  7564. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7565. has_reduced_clock)
  7566. crtc->lowfreq_avail = true;
  7567. return 0;
  7568. }
  7569. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7570. struct intel_link_m_n *m_n)
  7571. {
  7572. struct drm_device *dev = crtc->base.dev;
  7573. struct drm_i915_private *dev_priv = to_i915(dev);
  7574. enum pipe pipe = crtc->pipe;
  7575. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7576. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7577. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7578. & ~TU_SIZE_MASK;
  7579. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7580. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7581. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7582. }
  7583. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7584. enum transcoder transcoder,
  7585. struct intel_link_m_n *m_n,
  7586. struct intel_link_m_n *m2_n2)
  7587. {
  7588. struct drm_device *dev = crtc->base.dev;
  7589. struct drm_i915_private *dev_priv = to_i915(dev);
  7590. enum pipe pipe = crtc->pipe;
  7591. if (INTEL_INFO(dev)->gen >= 5) {
  7592. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7593. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7594. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7595. & ~TU_SIZE_MASK;
  7596. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7597. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7598. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7599. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7600. * gen < 8) and if DRRS is supported (to make sure the
  7601. * registers are not unnecessarily read).
  7602. */
  7603. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7604. crtc->config->has_drrs) {
  7605. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7606. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7607. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7608. & ~TU_SIZE_MASK;
  7609. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7610. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7611. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7612. }
  7613. } else {
  7614. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7615. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7616. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7617. & ~TU_SIZE_MASK;
  7618. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7619. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7620. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7621. }
  7622. }
  7623. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7624. struct intel_crtc_state *pipe_config)
  7625. {
  7626. if (pipe_config->has_pch_encoder)
  7627. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7628. else
  7629. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7630. &pipe_config->dp_m_n,
  7631. &pipe_config->dp_m2_n2);
  7632. }
  7633. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7634. struct intel_crtc_state *pipe_config)
  7635. {
  7636. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7637. &pipe_config->fdi_m_n, NULL);
  7638. }
  7639. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7640. struct intel_crtc_state *pipe_config)
  7641. {
  7642. struct drm_device *dev = crtc->base.dev;
  7643. struct drm_i915_private *dev_priv = to_i915(dev);
  7644. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7645. uint32_t ps_ctrl = 0;
  7646. int id = -1;
  7647. int i;
  7648. /* find scaler attached to this pipe */
  7649. for (i = 0; i < crtc->num_scalers; i++) {
  7650. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7651. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7652. id = i;
  7653. pipe_config->pch_pfit.enabled = true;
  7654. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7655. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7656. break;
  7657. }
  7658. }
  7659. scaler_state->scaler_id = id;
  7660. if (id >= 0) {
  7661. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7662. } else {
  7663. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7664. }
  7665. }
  7666. static void
  7667. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7668. struct intel_initial_plane_config *plane_config)
  7669. {
  7670. struct drm_device *dev = crtc->base.dev;
  7671. struct drm_i915_private *dev_priv = to_i915(dev);
  7672. u32 val, base, offset, stride_mult, tiling;
  7673. int pipe = crtc->pipe;
  7674. int fourcc, pixel_format;
  7675. unsigned int aligned_height;
  7676. struct drm_framebuffer *fb;
  7677. struct intel_framebuffer *intel_fb;
  7678. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7679. if (!intel_fb) {
  7680. DRM_DEBUG_KMS("failed to alloc fb\n");
  7681. return;
  7682. }
  7683. fb = &intel_fb->base;
  7684. val = I915_READ(PLANE_CTL(pipe, 0));
  7685. if (!(val & PLANE_CTL_ENABLE))
  7686. goto error;
  7687. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7688. fourcc = skl_format_to_fourcc(pixel_format,
  7689. val & PLANE_CTL_ORDER_RGBX,
  7690. val & PLANE_CTL_ALPHA_MASK);
  7691. fb->pixel_format = fourcc;
  7692. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7693. tiling = val & PLANE_CTL_TILED_MASK;
  7694. switch (tiling) {
  7695. case PLANE_CTL_TILED_LINEAR:
  7696. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7697. break;
  7698. case PLANE_CTL_TILED_X:
  7699. plane_config->tiling = I915_TILING_X;
  7700. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7701. break;
  7702. case PLANE_CTL_TILED_Y:
  7703. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7704. break;
  7705. case PLANE_CTL_TILED_YF:
  7706. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7707. break;
  7708. default:
  7709. MISSING_CASE(tiling);
  7710. goto error;
  7711. }
  7712. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7713. plane_config->base = base;
  7714. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7715. val = I915_READ(PLANE_SIZE(pipe, 0));
  7716. fb->height = ((val >> 16) & 0xfff) + 1;
  7717. fb->width = ((val >> 0) & 0x1fff) + 1;
  7718. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7719. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7720. fb->pixel_format);
  7721. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7722. aligned_height = intel_fb_align_height(dev, fb->height,
  7723. fb->pixel_format,
  7724. fb->modifier[0]);
  7725. plane_config->size = fb->pitches[0] * aligned_height;
  7726. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7727. pipe_name(pipe), fb->width, fb->height,
  7728. fb->bits_per_pixel, base, fb->pitches[0],
  7729. plane_config->size);
  7730. plane_config->fb = intel_fb;
  7731. return;
  7732. error:
  7733. kfree(fb);
  7734. }
  7735. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7736. struct intel_crtc_state *pipe_config)
  7737. {
  7738. struct drm_device *dev = crtc->base.dev;
  7739. struct drm_i915_private *dev_priv = to_i915(dev);
  7740. uint32_t tmp;
  7741. tmp = I915_READ(PF_CTL(crtc->pipe));
  7742. if (tmp & PF_ENABLE) {
  7743. pipe_config->pch_pfit.enabled = true;
  7744. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7745. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7746. /* We currently do not free assignements of panel fitters on
  7747. * ivb/hsw (since we don't use the higher upscaling modes which
  7748. * differentiates them) so just WARN about this case for now. */
  7749. if (IS_GEN7(dev)) {
  7750. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7751. PF_PIPE_SEL_IVB(crtc->pipe));
  7752. }
  7753. }
  7754. }
  7755. static void
  7756. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7757. struct intel_initial_plane_config *plane_config)
  7758. {
  7759. struct drm_device *dev = crtc->base.dev;
  7760. struct drm_i915_private *dev_priv = to_i915(dev);
  7761. u32 val, base, offset;
  7762. int pipe = crtc->pipe;
  7763. int fourcc, pixel_format;
  7764. unsigned int aligned_height;
  7765. struct drm_framebuffer *fb;
  7766. struct intel_framebuffer *intel_fb;
  7767. val = I915_READ(DSPCNTR(pipe));
  7768. if (!(val & DISPLAY_PLANE_ENABLE))
  7769. return;
  7770. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7771. if (!intel_fb) {
  7772. DRM_DEBUG_KMS("failed to alloc fb\n");
  7773. return;
  7774. }
  7775. fb = &intel_fb->base;
  7776. if (INTEL_INFO(dev)->gen >= 4) {
  7777. if (val & DISPPLANE_TILED) {
  7778. plane_config->tiling = I915_TILING_X;
  7779. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7780. }
  7781. }
  7782. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7783. fourcc = i9xx_format_to_fourcc(pixel_format);
  7784. fb->pixel_format = fourcc;
  7785. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7786. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7787. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7788. offset = I915_READ(DSPOFFSET(pipe));
  7789. } else {
  7790. if (plane_config->tiling)
  7791. offset = I915_READ(DSPTILEOFF(pipe));
  7792. else
  7793. offset = I915_READ(DSPLINOFF(pipe));
  7794. }
  7795. plane_config->base = base;
  7796. val = I915_READ(PIPESRC(pipe));
  7797. fb->width = ((val >> 16) & 0xfff) + 1;
  7798. fb->height = ((val >> 0) & 0xfff) + 1;
  7799. val = I915_READ(DSPSTRIDE(pipe));
  7800. fb->pitches[0] = val & 0xffffffc0;
  7801. aligned_height = intel_fb_align_height(dev, fb->height,
  7802. fb->pixel_format,
  7803. fb->modifier[0]);
  7804. plane_config->size = fb->pitches[0] * aligned_height;
  7805. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7806. pipe_name(pipe), fb->width, fb->height,
  7807. fb->bits_per_pixel, base, fb->pitches[0],
  7808. plane_config->size);
  7809. plane_config->fb = intel_fb;
  7810. }
  7811. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7812. struct intel_crtc_state *pipe_config)
  7813. {
  7814. struct drm_device *dev = crtc->base.dev;
  7815. struct drm_i915_private *dev_priv = to_i915(dev);
  7816. enum intel_display_power_domain power_domain;
  7817. uint32_t tmp;
  7818. bool ret;
  7819. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7820. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7821. return false;
  7822. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7823. pipe_config->shared_dpll = NULL;
  7824. ret = false;
  7825. tmp = I915_READ(PIPECONF(crtc->pipe));
  7826. if (!(tmp & PIPECONF_ENABLE))
  7827. goto out;
  7828. switch (tmp & PIPECONF_BPC_MASK) {
  7829. case PIPECONF_6BPC:
  7830. pipe_config->pipe_bpp = 18;
  7831. break;
  7832. case PIPECONF_8BPC:
  7833. pipe_config->pipe_bpp = 24;
  7834. break;
  7835. case PIPECONF_10BPC:
  7836. pipe_config->pipe_bpp = 30;
  7837. break;
  7838. case PIPECONF_12BPC:
  7839. pipe_config->pipe_bpp = 36;
  7840. break;
  7841. default:
  7842. break;
  7843. }
  7844. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7845. pipe_config->limited_color_range = true;
  7846. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7847. struct intel_shared_dpll *pll;
  7848. enum intel_dpll_id pll_id;
  7849. pipe_config->has_pch_encoder = true;
  7850. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7851. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7852. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7853. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7854. if (HAS_PCH_IBX(dev_priv)) {
  7855. /*
  7856. * The pipe->pch transcoder and pch transcoder->pll
  7857. * mapping is fixed.
  7858. */
  7859. pll_id = (enum intel_dpll_id) crtc->pipe;
  7860. } else {
  7861. tmp = I915_READ(PCH_DPLL_SEL);
  7862. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7863. pll_id = DPLL_ID_PCH_PLL_B;
  7864. else
  7865. pll_id= DPLL_ID_PCH_PLL_A;
  7866. }
  7867. pipe_config->shared_dpll =
  7868. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7869. pll = pipe_config->shared_dpll;
  7870. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7871. &pipe_config->dpll_hw_state));
  7872. tmp = pipe_config->dpll_hw_state.dpll;
  7873. pipe_config->pixel_multiplier =
  7874. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7875. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7876. ironlake_pch_clock_get(crtc, pipe_config);
  7877. } else {
  7878. pipe_config->pixel_multiplier = 1;
  7879. }
  7880. intel_get_pipe_timings(crtc, pipe_config);
  7881. intel_get_pipe_src_size(crtc, pipe_config);
  7882. ironlake_get_pfit_config(crtc, pipe_config);
  7883. ret = true;
  7884. out:
  7885. intel_display_power_put(dev_priv, power_domain);
  7886. return ret;
  7887. }
  7888. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7889. {
  7890. struct drm_device *dev = &dev_priv->drm;
  7891. struct intel_crtc *crtc;
  7892. for_each_intel_crtc(dev, crtc)
  7893. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7894. pipe_name(crtc->pipe));
  7895. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7896. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7897. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7898. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7899. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7900. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7901. "CPU PWM1 enabled\n");
  7902. if (IS_HASWELL(dev))
  7903. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7904. "CPU PWM2 enabled\n");
  7905. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7906. "PCH PWM1 enabled\n");
  7907. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7908. "Utility pin enabled\n");
  7909. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7910. /*
  7911. * In theory we can still leave IRQs enabled, as long as only the HPD
  7912. * interrupts remain enabled. We used to check for that, but since it's
  7913. * gen-specific and since we only disable LCPLL after we fully disable
  7914. * the interrupts, the check below should be enough.
  7915. */
  7916. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7917. }
  7918. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7919. {
  7920. struct drm_device *dev = &dev_priv->drm;
  7921. if (IS_HASWELL(dev))
  7922. return I915_READ(D_COMP_HSW);
  7923. else
  7924. return I915_READ(D_COMP_BDW);
  7925. }
  7926. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7927. {
  7928. struct drm_device *dev = &dev_priv->drm;
  7929. if (IS_HASWELL(dev)) {
  7930. mutex_lock(&dev_priv->rps.hw_lock);
  7931. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7932. val))
  7933. DRM_ERROR("Failed to write to D_COMP\n");
  7934. mutex_unlock(&dev_priv->rps.hw_lock);
  7935. } else {
  7936. I915_WRITE(D_COMP_BDW, val);
  7937. POSTING_READ(D_COMP_BDW);
  7938. }
  7939. }
  7940. /*
  7941. * This function implements pieces of two sequences from BSpec:
  7942. * - Sequence for display software to disable LCPLL
  7943. * - Sequence for display software to allow package C8+
  7944. * The steps implemented here are just the steps that actually touch the LCPLL
  7945. * register. Callers should take care of disabling all the display engine
  7946. * functions, doing the mode unset, fixing interrupts, etc.
  7947. */
  7948. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7949. bool switch_to_fclk, bool allow_power_down)
  7950. {
  7951. uint32_t val;
  7952. assert_can_disable_lcpll(dev_priv);
  7953. val = I915_READ(LCPLL_CTL);
  7954. if (switch_to_fclk) {
  7955. val |= LCPLL_CD_SOURCE_FCLK;
  7956. I915_WRITE(LCPLL_CTL, val);
  7957. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7958. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7959. DRM_ERROR("Switching to FCLK failed\n");
  7960. val = I915_READ(LCPLL_CTL);
  7961. }
  7962. val |= LCPLL_PLL_DISABLE;
  7963. I915_WRITE(LCPLL_CTL, val);
  7964. POSTING_READ(LCPLL_CTL);
  7965. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7966. DRM_ERROR("LCPLL still locked\n");
  7967. val = hsw_read_dcomp(dev_priv);
  7968. val |= D_COMP_COMP_DISABLE;
  7969. hsw_write_dcomp(dev_priv, val);
  7970. ndelay(100);
  7971. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7972. 1))
  7973. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7974. if (allow_power_down) {
  7975. val = I915_READ(LCPLL_CTL);
  7976. val |= LCPLL_POWER_DOWN_ALLOW;
  7977. I915_WRITE(LCPLL_CTL, val);
  7978. POSTING_READ(LCPLL_CTL);
  7979. }
  7980. }
  7981. /*
  7982. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7983. * source.
  7984. */
  7985. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7986. {
  7987. uint32_t val;
  7988. val = I915_READ(LCPLL_CTL);
  7989. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7990. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7991. return;
  7992. /*
  7993. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7994. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7995. */
  7996. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7997. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7998. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7999. I915_WRITE(LCPLL_CTL, val);
  8000. POSTING_READ(LCPLL_CTL);
  8001. }
  8002. val = hsw_read_dcomp(dev_priv);
  8003. val |= D_COMP_COMP_FORCE;
  8004. val &= ~D_COMP_COMP_DISABLE;
  8005. hsw_write_dcomp(dev_priv, val);
  8006. val = I915_READ(LCPLL_CTL);
  8007. val &= ~LCPLL_PLL_DISABLE;
  8008. I915_WRITE(LCPLL_CTL, val);
  8009. if (intel_wait_for_register(dev_priv,
  8010. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8011. 5))
  8012. DRM_ERROR("LCPLL not locked yet\n");
  8013. if (val & LCPLL_CD_SOURCE_FCLK) {
  8014. val = I915_READ(LCPLL_CTL);
  8015. val &= ~LCPLL_CD_SOURCE_FCLK;
  8016. I915_WRITE(LCPLL_CTL, val);
  8017. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8018. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8019. DRM_ERROR("Switching back to LCPLL failed\n");
  8020. }
  8021. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8022. intel_update_cdclk(&dev_priv->drm);
  8023. }
  8024. /*
  8025. * Package states C8 and deeper are really deep PC states that can only be
  8026. * reached when all the devices on the system allow it, so even if the graphics
  8027. * device allows PC8+, it doesn't mean the system will actually get to these
  8028. * states. Our driver only allows PC8+ when going into runtime PM.
  8029. *
  8030. * The requirements for PC8+ are that all the outputs are disabled, the power
  8031. * well is disabled and most interrupts are disabled, and these are also
  8032. * requirements for runtime PM. When these conditions are met, we manually do
  8033. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8034. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8035. * hang the machine.
  8036. *
  8037. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8038. * the state of some registers, so when we come back from PC8+ we need to
  8039. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8040. * need to take care of the registers kept by RC6. Notice that this happens even
  8041. * if we don't put the device in PCI D3 state (which is what currently happens
  8042. * because of the runtime PM support).
  8043. *
  8044. * For more, read "Display Sequences for Package C8" on the hardware
  8045. * documentation.
  8046. */
  8047. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8048. {
  8049. struct drm_device *dev = &dev_priv->drm;
  8050. uint32_t val;
  8051. DRM_DEBUG_KMS("Enabling package C8+\n");
  8052. if (HAS_PCH_LPT_LP(dev)) {
  8053. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8054. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8055. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8056. }
  8057. lpt_disable_clkout_dp(dev);
  8058. hsw_disable_lcpll(dev_priv, true, true);
  8059. }
  8060. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8061. {
  8062. struct drm_device *dev = &dev_priv->drm;
  8063. uint32_t val;
  8064. DRM_DEBUG_KMS("Disabling package C8+\n");
  8065. hsw_restore_lcpll(dev_priv);
  8066. lpt_init_pch_refclk(dev);
  8067. if (HAS_PCH_LPT_LP(dev)) {
  8068. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8069. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8070. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8071. }
  8072. }
  8073. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8074. {
  8075. struct drm_device *dev = old_state->dev;
  8076. struct intel_atomic_state *old_intel_state =
  8077. to_intel_atomic_state(old_state);
  8078. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8079. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8080. }
  8081. /* compute the max rate for new configuration */
  8082. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8083. {
  8084. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8085. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8086. struct drm_crtc *crtc;
  8087. struct drm_crtc_state *cstate;
  8088. struct intel_crtc_state *crtc_state;
  8089. unsigned max_pixel_rate = 0, i;
  8090. enum pipe pipe;
  8091. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8092. sizeof(intel_state->min_pixclk));
  8093. for_each_crtc_in_state(state, crtc, cstate, i) {
  8094. int pixel_rate;
  8095. crtc_state = to_intel_crtc_state(cstate);
  8096. if (!crtc_state->base.enable) {
  8097. intel_state->min_pixclk[i] = 0;
  8098. continue;
  8099. }
  8100. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8101. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8102. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8103. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8104. intel_state->min_pixclk[i] = pixel_rate;
  8105. }
  8106. for_each_pipe(dev_priv, pipe)
  8107. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8108. return max_pixel_rate;
  8109. }
  8110. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8111. {
  8112. struct drm_i915_private *dev_priv = to_i915(dev);
  8113. uint32_t val, data;
  8114. int ret;
  8115. if (WARN((I915_READ(LCPLL_CTL) &
  8116. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8117. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8118. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8119. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8120. "trying to change cdclk frequency with cdclk not enabled\n"))
  8121. return;
  8122. mutex_lock(&dev_priv->rps.hw_lock);
  8123. ret = sandybridge_pcode_write(dev_priv,
  8124. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8125. mutex_unlock(&dev_priv->rps.hw_lock);
  8126. if (ret) {
  8127. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8128. return;
  8129. }
  8130. val = I915_READ(LCPLL_CTL);
  8131. val |= LCPLL_CD_SOURCE_FCLK;
  8132. I915_WRITE(LCPLL_CTL, val);
  8133. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8134. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8135. DRM_ERROR("Switching to FCLK failed\n");
  8136. val = I915_READ(LCPLL_CTL);
  8137. val &= ~LCPLL_CLK_FREQ_MASK;
  8138. switch (cdclk) {
  8139. case 450000:
  8140. val |= LCPLL_CLK_FREQ_450;
  8141. data = 0;
  8142. break;
  8143. case 540000:
  8144. val |= LCPLL_CLK_FREQ_54O_BDW;
  8145. data = 1;
  8146. break;
  8147. case 337500:
  8148. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8149. data = 2;
  8150. break;
  8151. case 675000:
  8152. val |= LCPLL_CLK_FREQ_675_BDW;
  8153. data = 3;
  8154. break;
  8155. default:
  8156. WARN(1, "invalid cdclk frequency\n");
  8157. return;
  8158. }
  8159. I915_WRITE(LCPLL_CTL, val);
  8160. val = I915_READ(LCPLL_CTL);
  8161. val &= ~LCPLL_CD_SOURCE_FCLK;
  8162. I915_WRITE(LCPLL_CTL, val);
  8163. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8164. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8165. DRM_ERROR("Switching back to LCPLL failed\n");
  8166. mutex_lock(&dev_priv->rps.hw_lock);
  8167. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8168. mutex_unlock(&dev_priv->rps.hw_lock);
  8169. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8170. intel_update_cdclk(dev);
  8171. WARN(cdclk != dev_priv->cdclk_freq,
  8172. "cdclk requested %d kHz but got %d kHz\n",
  8173. cdclk, dev_priv->cdclk_freq);
  8174. }
  8175. static int broadwell_calc_cdclk(int max_pixclk)
  8176. {
  8177. if (max_pixclk > 540000)
  8178. return 675000;
  8179. else if (max_pixclk > 450000)
  8180. return 540000;
  8181. else if (max_pixclk > 337500)
  8182. return 450000;
  8183. else
  8184. return 337500;
  8185. }
  8186. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8187. {
  8188. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8189. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8190. int max_pixclk = ilk_max_pixel_rate(state);
  8191. int cdclk;
  8192. /*
  8193. * FIXME should also account for plane ratio
  8194. * once 64bpp pixel formats are supported.
  8195. */
  8196. cdclk = broadwell_calc_cdclk(max_pixclk);
  8197. if (cdclk > dev_priv->max_cdclk_freq) {
  8198. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8199. cdclk, dev_priv->max_cdclk_freq);
  8200. return -EINVAL;
  8201. }
  8202. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8203. if (!intel_state->active_crtcs)
  8204. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8205. return 0;
  8206. }
  8207. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8208. {
  8209. struct drm_device *dev = old_state->dev;
  8210. struct intel_atomic_state *old_intel_state =
  8211. to_intel_atomic_state(old_state);
  8212. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8213. broadwell_set_cdclk(dev, req_cdclk);
  8214. }
  8215. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8216. {
  8217. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8218. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8219. const int max_pixclk = ilk_max_pixel_rate(state);
  8220. int vco = intel_state->cdclk_pll_vco;
  8221. int cdclk;
  8222. /*
  8223. * FIXME should also account for plane ratio
  8224. * once 64bpp pixel formats are supported.
  8225. */
  8226. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8227. /*
  8228. * FIXME move the cdclk caclulation to
  8229. * compute_config() so we can fail gracegully.
  8230. */
  8231. if (cdclk > dev_priv->max_cdclk_freq) {
  8232. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8233. cdclk, dev_priv->max_cdclk_freq);
  8234. cdclk = dev_priv->max_cdclk_freq;
  8235. }
  8236. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8237. if (!intel_state->active_crtcs)
  8238. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8239. return 0;
  8240. }
  8241. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8242. {
  8243. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8244. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8245. unsigned int req_cdclk = intel_state->dev_cdclk;
  8246. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8247. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8248. }
  8249. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8250. struct intel_crtc_state *crtc_state)
  8251. {
  8252. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8253. if (!intel_ddi_pll_select(crtc, crtc_state))
  8254. return -EINVAL;
  8255. }
  8256. crtc->lowfreq_avail = false;
  8257. return 0;
  8258. }
  8259. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8260. enum port port,
  8261. struct intel_crtc_state *pipe_config)
  8262. {
  8263. enum intel_dpll_id id;
  8264. switch (port) {
  8265. case PORT_A:
  8266. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8267. id = DPLL_ID_SKL_DPLL0;
  8268. break;
  8269. case PORT_B:
  8270. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8271. id = DPLL_ID_SKL_DPLL1;
  8272. break;
  8273. case PORT_C:
  8274. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8275. id = DPLL_ID_SKL_DPLL2;
  8276. break;
  8277. default:
  8278. DRM_ERROR("Incorrect port type\n");
  8279. return;
  8280. }
  8281. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8282. }
  8283. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8284. enum port port,
  8285. struct intel_crtc_state *pipe_config)
  8286. {
  8287. enum intel_dpll_id id;
  8288. u32 temp;
  8289. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8290. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8291. switch (pipe_config->ddi_pll_sel) {
  8292. case SKL_DPLL0:
  8293. id = DPLL_ID_SKL_DPLL0;
  8294. break;
  8295. case SKL_DPLL1:
  8296. id = DPLL_ID_SKL_DPLL1;
  8297. break;
  8298. case SKL_DPLL2:
  8299. id = DPLL_ID_SKL_DPLL2;
  8300. break;
  8301. case SKL_DPLL3:
  8302. id = DPLL_ID_SKL_DPLL3;
  8303. break;
  8304. default:
  8305. MISSING_CASE(pipe_config->ddi_pll_sel);
  8306. return;
  8307. }
  8308. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8309. }
  8310. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8311. enum port port,
  8312. struct intel_crtc_state *pipe_config)
  8313. {
  8314. enum intel_dpll_id id;
  8315. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8316. switch (pipe_config->ddi_pll_sel) {
  8317. case PORT_CLK_SEL_WRPLL1:
  8318. id = DPLL_ID_WRPLL1;
  8319. break;
  8320. case PORT_CLK_SEL_WRPLL2:
  8321. id = DPLL_ID_WRPLL2;
  8322. break;
  8323. case PORT_CLK_SEL_SPLL:
  8324. id = DPLL_ID_SPLL;
  8325. break;
  8326. case PORT_CLK_SEL_LCPLL_810:
  8327. id = DPLL_ID_LCPLL_810;
  8328. break;
  8329. case PORT_CLK_SEL_LCPLL_1350:
  8330. id = DPLL_ID_LCPLL_1350;
  8331. break;
  8332. case PORT_CLK_SEL_LCPLL_2700:
  8333. id = DPLL_ID_LCPLL_2700;
  8334. break;
  8335. default:
  8336. MISSING_CASE(pipe_config->ddi_pll_sel);
  8337. /* fall through */
  8338. case PORT_CLK_SEL_NONE:
  8339. return;
  8340. }
  8341. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8342. }
  8343. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8344. struct intel_crtc_state *pipe_config,
  8345. unsigned long *power_domain_mask)
  8346. {
  8347. struct drm_device *dev = crtc->base.dev;
  8348. struct drm_i915_private *dev_priv = to_i915(dev);
  8349. enum intel_display_power_domain power_domain;
  8350. u32 tmp;
  8351. /*
  8352. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8353. * transcoder handled below.
  8354. */
  8355. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8356. /*
  8357. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8358. * consistency and less surprising code; it's in always on power).
  8359. */
  8360. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8361. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8362. enum pipe trans_edp_pipe;
  8363. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8364. default:
  8365. WARN(1, "unknown pipe linked to edp transcoder\n");
  8366. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8367. case TRANS_DDI_EDP_INPUT_A_ON:
  8368. trans_edp_pipe = PIPE_A;
  8369. break;
  8370. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8371. trans_edp_pipe = PIPE_B;
  8372. break;
  8373. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8374. trans_edp_pipe = PIPE_C;
  8375. break;
  8376. }
  8377. if (trans_edp_pipe == crtc->pipe)
  8378. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8379. }
  8380. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8381. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8382. return false;
  8383. *power_domain_mask |= BIT(power_domain);
  8384. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8385. return tmp & PIPECONF_ENABLE;
  8386. }
  8387. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8388. struct intel_crtc_state *pipe_config,
  8389. unsigned long *power_domain_mask)
  8390. {
  8391. struct drm_device *dev = crtc->base.dev;
  8392. struct drm_i915_private *dev_priv = to_i915(dev);
  8393. enum intel_display_power_domain power_domain;
  8394. enum port port;
  8395. enum transcoder cpu_transcoder;
  8396. u32 tmp;
  8397. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8398. if (port == PORT_A)
  8399. cpu_transcoder = TRANSCODER_DSI_A;
  8400. else
  8401. cpu_transcoder = TRANSCODER_DSI_C;
  8402. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8403. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8404. continue;
  8405. *power_domain_mask |= BIT(power_domain);
  8406. /*
  8407. * The PLL needs to be enabled with a valid divider
  8408. * configuration, otherwise accessing DSI registers will hang
  8409. * the machine. See BSpec North Display Engine
  8410. * registers/MIPI[BXT]. We can break out here early, since we
  8411. * need the same DSI PLL to be enabled for both DSI ports.
  8412. */
  8413. if (!intel_dsi_pll_is_enabled(dev_priv))
  8414. break;
  8415. /* XXX: this works for video mode only */
  8416. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8417. if (!(tmp & DPI_ENABLE))
  8418. continue;
  8419. tmp = I915_READ(MIPI_CTRL(port));
  8420. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8421. continue;
  8422. pipe_config->cpu_transcoder = cpu_transcoder;
  8423. break;
  8424. }
  8425. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8426. }
  8427. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8428. struct intel_crtc_state *pipe_config)
  8429. {
  8430. struct drm_device *dev = crtc->base.dev;
  8431. struct drm_i915_private *dev_priv = to_i915(dev);
  8432. struct intel_shared_dpll *pll;
  8433. enum port port;
  8434. uint32_t tmp;
  8435. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8436. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8437. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8438. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8439. else if (IS_BROXTON(dev))
  8440. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8441. else
  8442. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8443. pll = pipe_config->shared_dpll;
  8444. if (pll) {
  8445. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8446. &pipe_config->dpll_hw_state));
  8447. }
  8448. /*
  8449. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8450. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8451. * the PCH transcoder is on.
  8452. */
  8453. if (INTEL_INFO(dev)->gen < 9 &&
  8454. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8455. pipe_config->has_pch_encoder = true;
  8456. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8457. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8458. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8459. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8460. }
  8461. }
  8462. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8463. struct intel_crtc_state *pipe_config)
  8464. {
  8465. struct drm_device *dev = crtc->base.dev;
  8466. struct drm_i915_private *dev_priv = to_i915(dev);
  8467. enum intel_display_power_domain power_domain;
  8468. unsigned long power_domain_mask;
  8469. bool active;
  8470. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8471. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8472. return false;
  8473. power_domain_mask = BIT(power_domain);
  8474. pipe_config->shared_dpll = NULL;
  8475. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8476. if (IS_BROXTON(dev_priv) &&
  8477. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8478. WARN_ON(active);
  8479. active = true;
  8480. }
  8481. if (!active)
  8482. goto out;
  8483. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8484. haswell_get_ddi_port_state(crtc, pipe_config);
  8485. intel_get_pipe_timings(crtc, pipe_config);
  8486. }
  8487. intel_get_pipe_src_size(crtc, pipe_config);
  8488. pipe_config->gamma_mode =
  8489. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8490. if (INTEL_INFO(dev)->gen >= 9) {
  8491. skl_init_scalers(dev, crtc, pipe_config);
  8492. }
  8493. if (INTEL_INFO(dev)->gen >= 9) {
  8494. pipe_config->scaler_state.scaler_id = -1;
  8495. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8496. }
  8497. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8498. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8499. power_domain_mask |= BIT(power_domain);
  8500. if (INTEL_INFO(dev)->gen >= 9)
  8501. skylake_get_pfit_config(crtc, pipe_config);
  8502. else
  8503. ironlake_get_pfit_config(crtc, pipe_config);
  8504. }
  8505. if (IS_HASWELL(dev))
  8506. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8507. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8508. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8509. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8510. pipe_config->pixel_multiplier =
  8511. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8512. } else {
  8513. pipe_config->pixel_multiplier = 1;
  8514. }
  8515. out:
  8516. for_each_power_domain(power_domain, power_domain_mask)
  8517. intel_display_power_put(dev_priv, power_domain);
  8518. return active;
  8519. }
  8520. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8521. const struct intel_plane_state *plane_state)
  8522. {
  8523. struct drm_device *dev = crtc->dev;
  8524. struct drm_i915_private *dev_priv = to_i915(dev);
  8525. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8526. uint32_t cntl = 0, size = 0;
  8527. if (plane_state && plane_state->visible) {
  8528. unsigned int width = plane_state->base.crtc_w;
  8529. unsigned int height = plane_state->base.crtc_h;
  8530. unsigned int stride = roundup_pow_of_two(width) * 4;
  8531. switch (stride) {
  8532. default:
  8533. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8534. width, stride);
  8535. stride = 256;
  8536. /* fallthrough */
  8537. case 256:
  8538. case 512:
  8539. case 1024:
  8540. case 2048:
  8541. break;
  8542. }
  8543. cntl |= CURSOR_ENABLE |
  8544. CURSOR_GAMMA_ENABLE |
  8545. CURSOR_FORMAT_ARGB |
  8546. CURSOR_STRIDE(stride);
  8547. size = (height << 12) | width;
  8548. }
  8549. if (intel_crtc->cursor_cntl != 0 &&
  8550. (intel_crtc->cursor_base != base ||
  8551. intel_crtc->cursor_size != size ||
  8552. intel_crtc->cursor_cntl != cntl)) {
  8553. /* On these chipsets we can only modify the base/size/stride
  8554. * whilst the cursor is disabled.
  8555. */
  8556. I915_WRITE(CURCNTR(PIPE_A), 0);
  8557. POSTING_READ(CURCNTR(PIPE_A));
  8558. intel_crtc->cursor_cntl = 0;
  8559. }
  8560. if (intel_crtc->cursor_base != base) {
  8561. I915_WRITE(CURBASE(PIPE_A), base);
  8562. intel_crtc->cursor_base = base;
  8563. }
  8564. if (intel_crtc->cursor_size != size) {
  8565. I915_WRITE(CURSIZE, size);
  8566. intel_crtc->cursor_size = size;
  8567. }
  8568. if (intel_crtc->cursor_cntl != cntl) {
  8569. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8570. POSTING_READ(CURCNTR(PIPE_A));
  8571. intel_crtc->cursor_cntl = cntl;
  8572. }
  8573. }
  8574. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8575. const struct intel_plane_state *plane_state)
  8576. {
  8577. struct drm_device *dev = crtc->dev;
  8578. struct drm_i915_private *dev_priv = to_i915(dev);
  8579. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8580. int pipe = intel_crtc->pipe;
  8581. uint32_t cntl = 0;
  8582. if (plane_state && plane_state->visible) {
  8583. cntl = MCURSOR_GAMMA_ENABLE;
  8584. switch (plane_state->base.crtc_w) {
  8585. case 64:
  8586. cntl |= CURSOR_MODE_64_ARGB_AX;
  8587. break;
  8588. case 128:
  8589. cntl |= CURSOR_MODE_128_ARGB_AX;
  8590. break;
  8591. case 256:
  8592. cntl |= CURSOR_MODE_256_ARGB_AX;
  8593. break;
  8594. default:
  8595. MISSING_CASE(plane_state->base.crtc_w);
  8596. return;
  8597. }
  8598. cntl |= pipe << 28; /* Connect to correct pipe */
  8599. if (HAS_DDI(dev))
  8600. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8601. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8602. cntl |= CURSOR_ROTATE_180;
  8603. }
  8604. if (intel_crtc->cursor_cntl != cntl) {
  8605. I915_WRITE(CURCNTR(pipe), cntl);
  8606. POSTING_READ(CURCNTR(pipe));
  8607. intel_crtc->cursor_cntl = cntl;
  8608. }
  8609. /* and commit changes on next vblank */
  8610. I915_WRITE(CURBASE(pipe), base);
  8611. POSTING_READ(CURBASE(pipe));
  8612. intel_crtc->cursor_base = base;
  8613. }
  8614. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8615. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8616. const struct intel_plane_state *plane_state)
  8617. {
  8618. struct drm_device *dev = crtc->dev;
  8619. struct drm_i915_private *dev_priv = to_i915(dev);
  8620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8621. int pipe = intel_crtc->pipe;
  8622. u32 base = intel_crtc->cursor_addr;
  8623. u32 pos = 0;
  8624. if (plane_state) {
  8625. int x = plane_state->base.crtc_x;
  8626. int y = plane_state->base.crtc_y;
  8627. if (x < 0) {
  8628. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8629. x = -x;
  8630. }
  8631. pos |= x << CURSOR_X_SHIFT;
  8632. if (y < 0) {
  8633. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8634. y = -y;
  8635. }
  8636. pos |= y << CURSOR_Y_SHIFT;
  8637. /* ILK+ do this automagically */
  8638. if (HAS_GMCH_DISPLAY(dev) &&
  8639. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8640. base += (plane_state->base.crtc_h *
  8641. plane_state->base.crtc_w - 1) * 4;
  8642. }
  8643. }
  8644. I915_WRITE(CURPOS(pipe), pos);
  8645. if (IS_845G(dev) || IS_I865G(dev))
  8646. i845_update_cursor(crtc, base, plane_state);
  8647. else
  8648. i9xx_update_cursor(crtc, base, plane_state);
  8649. }
  8650. static bool cursor_size_ok(struct drm_device *dev,
  8651. uint32_t width, uint32_t height)
  8652. {
  8653. if (width == 0 || height == 0)
  8654. return false;
  8655. /*
  8656. * 845g/865g are special in that they are only limited by
  8657. * the width of their cursors, the height is arbitrary up to
  8658. * the precision of the register. Everything else requires
  8659. * square cursors, limited to a few power-of-two sizes.
  8660. */
  8661. if (IS_845G(dev) || IS_I865G(dev)) {
  8662. if ((width & 63) != 0)
  8663. return false;
  8664. if (width > (IS_845G(dev) ? 64 : 512))
  8665. return false;
  8666. if (height > 1023)
  8667. return false;
  8668. } else {
  8669. switch (width | height) {
  8670. case 256:
  8671. case 128:
  8672. if (IS_GEN2(dev))
  8673. return false;
  8674. case 64:
  8675. break;
  8676. default:
  8677. return false;
  8678. }
  8679. }
  8680. return true;
  8681. }
  8682. /* VESA 640x480x72Hz mode to set on the pipe */
  8683. static struct drm_display_mode load_detect_mode = {
  8684. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8685. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8686. };
  8687. struct drm_framebuffer *
  8688. __intel_framebuffer_create(struct drm_device *dev,
  8689. struct drm_mode_fb_cmd2 *mode_cmd,
  8690. struct drm_i915_gem_object *obj)
  8691. {
  8692. struct intel_framebuffer *intel_fb;
  8693. int ret;
  8694. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8695. if (!intel_fb)
  8696. return ERR_PTR(-ENOMEM);
  8697. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8698. if (ret)
  8699. goto err;
  8700. return &intel_fb->base;
  8701. err:
  8702. kfree(intel_fb);
  8703. return ERR_PTR(ret);
  8704. }
  8705. static struct drm_framebuffer *
  8706. intel_framebuffer_create(struct drm_device *dev,
  8707. struct drm_mode_fb_cmd2 *mode_cmd,
  8708. struct drm_i915_gem_object *obj)
  8709. {
  8710. struct drm_framebuffer *fb;
  8711. int ret;
  8712. ret = i915_mutex_lock_interruptible(dev);
  8713. if (ret)
  8714. return ERR_PTR(ret);
  8715. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8716. mutex_unlock(&dev->struct_mutex);
  8717. return fb;
  8718. }
  8719. static u32
  8720. intel_framebuffer_pitch_for_width(int width, int bpp)
  8721. {
  8722. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8723. return ALIGN(pitch, 64);
  8724. }
  8725. static u32
  8726. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8727. {
  8728. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8729. return PAGE_ALIGN(pitch * mode->vdisplay);
  8730. }
  8731. static struct drm_framebuffer *
  8732. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8733. struct drm_display_mode *mode,
  8734. int depth, int bpp)
  8735. {
  8736. struct drm_framebuffer *fb;
  8737. struct drm_i915_gem_object *obj;
  8738. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8739. obj = i915_gem_object_create(dev,
  8740. intel_framebuffer_size_for_mode(mode, bpp));
  8741. if (IS_ERR(obj))
  8742. return ERR_CAST(obj);
  8743. mode_cmd.width = mode->hdisplay;
  8744. mode_cmd.height = mode->vdisplay;
  8745. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8746. bpp);
  8747. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8748. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8749. if (IS_ERR(fb))
  8750. drm_gem_object_unreference_unlocked(&obj->base);
  8751. return fb;
  8752. }
  8753. static struct drm_framebuffer *
  8754. mode_fits_in_fbdev(struct drm_device *dev,
  8755. struct drm_display_mode *mode)
  8756. {
  8757. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8758. struct drm_i915_private *dev_priv = to_i915(dev);
  8759. struct drm_i915_gem_object *obj;
  8760. struct drm_framebuffer *fb;
  8761. if (!dev_priv->fbdev)
  8762. return NULL;
  8763. if (!dev_priv->fbdev->fb)
  8764. return NULL;
  8765. obj = dev_priv->fbdev->fb->obj;
  8766. BUG_ON(!obj);
  8767. fb = &dev_priv->fbdev->fb->base;
  8768. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8769. fb->bits_per_pixel))
  8770. return NULL;
  8771. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8772. return NULL;
  8773. drm_framebuffer_reference(fb);
  8774. return fb;
  8775. #else
  8776. return NULL;
  8777. #endif
  8778. }
  8779. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8780. struct drm_crtc *crtc,
  8781. struct drm_display_mode *mode,
  8782. struct drm_framebuffer *fb,
  8783. int x, int y)
  8784. {
  8785. struct drm_plane_state *plane_state;
  8786. int hdisplay, vdisplay;
  8787. int ret;
  8788. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8789. if (IS_ERR(plane_state))
  8790. return PTR_ERR(plane_state);
  8791. if (mode)
  8792. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8793. else
  8794. hdisplay = vdisplay = 0;
  8795. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8796. if (ret)
  8797. return ret;
  8798. drm_atomic_set_fb_for_plane(plane_state, fb);
  8799. plane_state->crtc_x = 0;
  8800. plane_state->crtc_y = 0;
  8801. plane_state->crtc_w = hdisplay;
  8802. plane_state->crtc_h = vdisplay;
  8803. plane_state->src_x = x << 16;
  8804. plane_state->src_y = y << 16;
  8805. plane_state->src_w = hdisplay << 16;
  8806. plane_state->src_h = vdisplay << 16;
  8807. return 0;
  8808. }
  8809. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8810. struct drm_display_mode *mode,
  8811. struct intel_load_detect_pipe *old,
  8812. struct drm_modeset_acquire_ctx *ctx)
  8813. {
  8814. struct intel_crtc *intel_crtc;
  8815. struct intel_encoder *intel_encoder =
  8816. intel_attached_encoder(connector);
  8817. struct drm_crtc *possible_crtc;
  8818. struct drm_encoder *encoder = &intel_encoder->base;
  8819. struct drm_crtc *crtc = NULL;
  8820. struct drm_device *dev = encoder->dev;
  8821. struct drm_framebuffer *fb;
  8822. struct drm_mode_config *config = &dev->mode_config;
  8823. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8824. struct drm_connector_state *connector_state;
  8825. struct intel_crtc_state *crtc_state;
  8826. int ret, i = -1;
  8827. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8828. connector->base.id, connector->name,
  8829. encoder->base.id, encoder->name);
  8830. old->restore_state = NULL;
  8831. retry:
  8832. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8833. if (ret)
  8834. goto fail;
  8835. /*
  8836. * Algorithm gets a little messy:
  8837. *
  8838. * - if the connector already has an assigned crtc, use it (but make
  8839. * sure it's on first)
  8840. *
  8841. * - try to find the first unused crtc that can drive this connector,
  8842. * and use that if we find one
  8843. */
  8844. /* See if we already have a CRTC for this connector */
  8845. if (connector->state->crtc) {
  8846. crtc = connector->state->crtc;
  8847. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8848. if (ret)
  8849. goto fail;
  8850. /* Make sure the crtc and connector are running */
  8851. goto found;
  8852. }
  8853. /* Find an unused one (if possible) */
  8854. for_each_crtc(dev, possible_crtc) {
  8855. i++;
  8856. if (!(encoder->possible_crtcs & (1 << i)))
  8857. continue;
  8858. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8859. if (ret)
  8860. goto fail;
  8861. if (possible_crtc->state->enable) {
  8862. drm_modeset_unlock(&possible_crtc->mutex);
  8863. continue;
  8864. }
  8865. crtc = possible_crtc;
  8866. break;
  8867. }
  8868. /*
  8869. * If we didn't find an unused CRTC, don't use any.
  8870. */
  8871. if (!crtc) {
  8872. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8873. goto fail;
  8874. }
  8875. found:
  8876. intel_crtc = to_intel_crtc(crtc);
  8877. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8878. if (ret)
  8879. goto fail;
  8880. state = drm_atomic_state_alloc(dev);
  8881. restore_state = drm_atomic_state_alloc(dev);
  8882. if (!state || !restore_state) {
  8883. ret = -ENOMEM;
  8884. goto fail;
  8885. }
  8886. state->acquire_ctx = ctx;
  8887. restore_state->acquire_ctx = ctx;
  8888. connector_state = drm_atomic_get_connector_state(state, connector);
  8889. if (IS_ERR(connector_state)) {
  8890. ret = PTR_ERR(connector_state);
  8891. goto fail;
  8892. }
  8893. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8894. if (ret)
  8895. goto fail;
  8896. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8897. if (IS_ERR(crtc_state)) {
  8898. ret = PTR_ERR(crtc_state);
  8899. goto fail;
  8900. }
  8901. crtc_state->base.active = crtc_state->base.enable = true;
  8902. if (!mode)
  8903. mode = &load_detect_mode;
  8904. /* We need a framebuffer large enough to accommodate all accesses
  8905. * that the plane may generate whilst we perform load detection.
  8906. * We can not rely on the fbcon either being present (we get called
  8907. * during its initialisation to detect all boot displays, or it may
  8908. * not even exist) or that it is large enough to satisfy the
  8909. * requested mode.
  8910. */
  8911. fb = mode_fits_in_fbdev(dev, mode);
  8912. if (fb == NULL) {
  8913. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8914. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8915. } else
  8916. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8917. if (IS_ERR(fb)) {
  8918. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8919. goto fail;
  8920. }
  8921. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8922. if (ret)
  8923. goto fail;
  8924. drm_framebuffer_unreference(fb);
  8925. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8926. if (ret)
  8927. goto fail;
  8928. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8929. if (!ret)
  8930. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8931. if (!ret)
  8932. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8933. if (ret) {
  8934. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8935. goto fail;
  8936. }
  8937. ret = drm_atomic_commit(state);
  8938. if (ret) {
  8939. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8940. goto fail;
  8941. }
  8942. old->restore_state = restore_state;
  8943. /* let the connector get through one full cycle before testing */
  8944. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8945. return true;
  8946. fail:
  8947. drm_atomic_state_free(state);
  8948. drm_atomic_state_free(restore_state);
  8949. restore_state = state = NULL;
  8950. if (ret == -EDEADLK) {
  8951. drm_modeset_backoff(ctx);
  8952. goto retry;
  8953. }
  8954. return false;
  8955. }
  8956. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8957. struct intel_load_detect_pipe *old,
  8958. struct drm_modeset_acquire_ctx *ctx)
  8959. {
  8960. struct intel_encoder *intel_encoder =
  8961. intel_attached_encoder(connector);
  8962. struct drm_encoder *encoder = &intel_encoder->base;
  8963. struct drm_atomic_state *state = old->restore_state;
  8964. int ret;
  8965. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8966. connector->base.id, connector->name,
  8967. encoder->base.id, encoder->name);
  8968. if (!state)
  8969. return;
  8970. ret = drm_atomic_commit(state);
  8971. if (ret) {
  8972. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8973. drm_atomic_state_free(state);
  8974. }
  8975. }
  8976. static int i9xx_pll_refclk(struct drm_device *dev,
  8977. const struct intel_crtc_state *pipe_config)
  8978. {
  8979. struct drm_i915_private *dev_priv = to_i915(dev);
  8980. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8981. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8982. return dev_priv->vbt.lvds_ssc_freq;
  8983. else if (HAS_PCH_SPLIT(dev))
  8984. return 120000;
  8985. else if (!IS_GEN2(dev))
  8986. return 96000;
  8987. else
  8988. return 48000;
  8989. }
  8990. /* Returns the clock of the currently programmed mode of the given pipe. */
  8991. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8992. struct intel_crtc_state *pipe_config)
  8993. {
  8994. struct drm_device *dev = crtc->base.dev;
  8995. struct drm_i915_private *dev_priv = to_i915(dev);
  8996. int pipe = pipe_config->cpu_transcoder;
  8997. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8998. u32 fp;
  8999. struct dpll clock;
  9000. int port_clock;
  9001. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9002. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9003. fp = pipe_config->dpll_hw_state.fp0;
  9004. else
  9005. fp = pipe_config->dpll_hw_state.fp1;
  9006. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9007. if (IS_PINEVIEW(dev)) {
  9008. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9009. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9010. } else {
  9011. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9012. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9013. }
  9014. if (!IS_GEN2(dev)) {
  9015. if (IS_PINEVIEW(dev))
  9016. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9017. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9018. else
  9019. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9020. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9021. switch (dpll & DPLL_MODE_MASK) {
  9022. case DPLLB_MODE_DAC_SERIAL:
  9023. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9024. 5 : 10;
  9025. break;
  9026. case DPLLB_MODE_LVDS:
  9027. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9028. 7 : 14;
  9029. break;
  9030. default:
  9031. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9032. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9033. return;
  9034. }
  9035. if (IS_PINEVIEW(dev))
  9036. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9037. else
  9038. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9039. } else {
  9040. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  9041. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9042. if (is_lvds) {
  9043. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9044. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9045. if (lvds & LVDS_CLKB_POWER_UP)
  9046. clock.p2 = 7;
  9047. else
  9048. clock.p2 = 14;
  9049. } else {
  9050. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9051. clock.p1 = 2;
  9052. else {
  9053. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9054. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9055. }
  9056. if (dpll & PLL_P2_DIVIDE_BY_4)
  9057. clock.p2 = 4;
  9058. else
  9059. clock.p2 = 2;
  9060. }
  9061. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9062. }
  9063. /*
  9064. * This value includes pixel_multiplier. We will use
  9065. * port_clock to compute adjusted_mode.crtc_clock in the
  9066. * encoder's get_config() function.
  9067. */
  9068. pipe_config->port_clock = port_clock;
  9069. }
  9070. int intel_dotclock_calculate(int link_freq,
  9071. const struct intel_link_m_n *m_n)
  9072. {
  9073. /*
  9074. * The calculation for the data clock is:
  9075. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9076. * But we want to avoid losing precison if possible, so:
  9077. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9078. *
  9079. * and the link clock is simpler:
  9080. * link_clock = (m * link_clock) / n
  9081. */
  9082. if (!m_n->link_n)
  9083. return 0;
  9084. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9085. }
  9086. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9087. struct intel_crtc_state *pipe_config)
  9088. {
  9089. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9090. /* read out port_clock from the DPLL */
  9091. i9xx_crtc_clock_get(crtc, pipe_config);
  9092. /*
  9093. * In case there is an active pipe without active ports,
  9094. * we may need some idea for the dotclock anyway.
  9095. * Calculate one based on the FDI configuration.
  9096. */
  9097. pipe_config->base.adjusted_mode.crtc_clock =
  9098. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9099. &pipe_config->fdi_m_n);
  9100. }
  9101. /** Returns the currently programmed mode of the given pipe. */
  9102. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9103. struct drm_crtc *crtc)
  9104. {
  9105. struct drm_i915_private *dev_priv = to_i915(dev);
  9106. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9107. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9108. struct drm_display_mode *mode;
  9109. struct intel_crtc_state *pipe_config;
  9110. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9111. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9112. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9113. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9114. enum pipe pipe = intel_crtc->pipe;
  9115. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9116. if (!mode)
  9117. return NULL;
  9118. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9119. if (!pipe_config) {
  9120. kfree(mode);
  9121. return NULL;
  9122. }
  9123. /*
  9124. * Construct a pipe_config sufficient for getting the clock info
  9125. * back out of crtc_clock_get.
  9126. *
  9127. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9128. * to use a real value here instead.
  9129. */
  9130. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9131. pipe_config->pixel_multiplier = 1;
  9132. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9133. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9134. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9135. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9136. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9137. mode->hdisplay = (htot & 0xffff) + 1;
  9138. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9139. mode->hsync_start = (hsync & 0xffff) + 1;
  9140. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9141. mode->vdisplay = (vtot & 0xffff) + 1;
  9142. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9143. mode->vsync_start = (vsync & 0xffff) + 1;
  9144. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9145. drm_mode_set_name(mode);
  9146. kfree(pipe_config);
  9147. return mode;
  9148. }
  9149. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9150. {
  9151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9152. struct drm_device *dev = crtc->dev;
  9153. struct intel_flip_work *work;
  9154. spin_lock_irq(&dev->event_lock);
  9155. work = intel_crtc->flip_work;
  9156. intel_crtc->flip_work = NULL;
  9157. spin_unlock_irq(&dev->event_lock);
  9158. if (work) {
  9159. cancel_work_sync(&work->mmio_work);
  9160. cancel_work_sync(&work->unpin_work);
  9161. kfree(work);
  9162. }
  9163. drm_crtc_cleanup(crtc);
  9164. kfree(intel_crtc);
  9165. }
  9166. static void intel_unpin_work_fn(struct work_struct *__work)
  9167. {
  9168. struct intel_flip_work *work =
  9169. container_of(__work, struct intel_flip_work, unpin_work);
  9170. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9171. struct drm_device *dev = crtc->base.dev;
  9172. struct drm_plane *primary = crtc->base.primary;
  9173. if (is_mmio_work(work))
  9174. flush_work(&work->mmio_work);
  9175. mutex_lock(&dev->struct_mutex);
  9176. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9177. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9178. if (work->flip_queued_req)
  9179. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9180. mutex_unlock(&dev->struct_mutex);
  9181. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9182. intel_fbc_post_update(crtc);
  9183. drm_framebuffer_unreference(work->old_fb);
  9184. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9185. atomic_dec(&crtc->unpin_work_count);
  9186. kfree(work);
  9187. }
  9188. /* Is 'a' after or equal to 'b'? */
  9189. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9190. {
  9191. return !((a - b) & 0x80000000);
  9192. }
  9193. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9194. struct intel_flip_work *work)
  9195. {
  9196. struct drm_device *dev = crtc->base.dev;
  9197. struct drm_i915_private *dev_priv = to_i915(dev);
  9198. unsigned reset_counter;
  9199. reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9200. if (crtc->reset_counter != reset_counter)
  9201. return true;
  9202. /*
  9203. * The relevant registers doen't exist on pre-ctg.
  9204. * As the flip done interrupt doesn't trigger for mmio
  9205. * flips on gmch platforms, a flip count check isn't
  9206. * really needed there. But since ctg has the registers,
  9207. * include it in the check anyway.
  9208. */
  9209. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9210. return true;
  9211. /*
  9212. * BDW signals flip done immediately if the plane
  9213. * is disabled, even if the plane enable is already
  9214. * armed to occur at the next vblank :(
  9215. */
  9216. /*
  9217. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9218. * used the same base address. In that case the mmio flip might
  9219. * have completed, but the CS hasn't even executed the flip yet.
  9220. *
  9221. * A flip count check isn't enough as the CS might have updated
  9222. * the base address just after start of vblank, but before we
  9223. * managed to process the interrupt. This means we'd complete the
  9224. * CS flip too soon.
  9225. *
  9226. * Combining both checks should get us a good enough result. It may
  9227. * still happen that the CS flip has been executed, but has not
  9228. * yet actually completed. But in case the base address is the same
  9229. * anyway, we don't really care.
  9230. */
  9231. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9232. crtc->flip_work->gtt_offset &&
  9233. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9234. crtc->flip_work->flip_count);
  9235. }
  9236. static bool
  9237. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9238. struct intel_flip_work *work)
  9239. {
  9240. /*
  9241. * MMIO work completes when vblank is different from
  9242. * flip_queued_vblank.
  9243. *
  9244. * Reset counter value doesn't matter, this is handled by
  9245. * i915_wait_request finishing early, so no need to handle
  9246. * reset here.
  9247. */
  9248. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9249. }
  9250. static bool pageflip_finished(struct intel_crtc *crtc,
  9251. struct intel_flip_work *work)
  9252. {
  9253. if (!atomic_read(&work->pending))
  9254. return false;
  9255. smp_rmb();
  9256. if (is_mmio_work(work))
  9257. return __pageflip_finished_mmio(crtc, work);
  9258. else
  9259. return __pageflip_finished_cs(crtc, work);
  9260. }
  9261. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9262. {
  9263. struct drm_device *dev = &dev_priv->drm;
  9264. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9265. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9266. struct intel_flip_work *work;
  9267. unsigned long flags;
  9268. /* Ignore early vblank irqs */
  9269. if (!crtc)
  9270. return;
  9271. /*
  9272. * This is called both by irq handlers and the reset code (to complete
  9273. * lost pageflips) so needs the full irqsave spinlocks.
  9274. */
  9275. spin_lock_irqsave(&dev->event_lock, flags);
  9276. work = intel_crtc->flip_work;
  9277. if (work != NULL &&
  9278. !is_mmio_work(work) &&
  9279. pageflip_finished(intel_crtc, work))
  9280. page_flip_completed(intel_crtc);
  9281. spin_unlock_irqrestore(&dev->event_lock, flags);
  9282. }
  9283. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9284. {
  9285. struct drm_device *dev = &dev_priv->drm;
  9286. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9288. struct intel_flip_work *work;
  9289. unsigned long flags;
  9290. /* Ignore early vblank irqs */
  9291. if (!crtc)
  9292. return;
  9293. /*
  9294. * This is called both by irq handlers and the reset code (to complete
  9295. * lost pageflips) so needs the full irqsave spinlocks.
  9296. */
  9297. spin_lock_irqsave(&dev->event_lock, flags);
  9298. work = intel_crtc->flip_work;
  9299. if (work != NULL &&
  9300. is_mmio_work(work) &&
  9301. pageflip_finished(intel_crtc, work))
  9302. page_flip_completed(intel_crtc);
  9303. spin_unlock_irqrestore(&dev->event_lock, flags);
  9304. }
  9305. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9306. struct intel_flip_work *work)
  9307. {
  9308. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9309. /* Ensure that the work item is consistent when activating it ... */
  9310. smp_mb__before_atomic();
  9311. atomic_set(&work->pending, 1);
  9312. }
  9313. static int intel_gen2_queue_flip(struct drm_device *dev,
  9314. struct drm_crtc *crtc,
  9315. struct drm_framebuffer *fb,
  9316. struct drm_i915_gem_object *obj,
  9317. struct drm_i915_gem_request *req,
  9318. uint32_t flags)
  9319. {
  9320. struct intel_engine_cs *engine = req->engine;
  9321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9322. u32 flip_mask;
  9323. int ret;
  9324. ret = intel_ring_begin(req, 6);
  9325. if (ret)
  9326. return ret;
  9327. /* Can't queue multiple flips, so wait for the previous
  9328. * one to finish before executing the next.
  9329. */
  9330. if (intel_crtc->plane)
  9331. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9332. else
  9333. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9334. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9335. intel_ring_emit(engine, MI_NOOP);
  9336. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9337. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9338. intel_ring_emit(engine, fb->pitches[0]);
  9339. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9340. intel_ring_emit(engine, 0); /* aux display base address, unused */
  9341. return 0;
  9342. }
  9343. static int intel_gen3_queue_flip(struct drm_device *dev,
  9344. struct drm_crtc *crtc,
  9345. struct drm_framebuffer *fb,
  9346. struct drm_i915_gem_object *obj,
  9347. struct drm_i915_gem_request *req,
  9348. uint32_t flags)
  9349. {
  9350. struct intel_engine_cs *engine = req->engine;
  9351. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9352. u32 flip_mask;
  9353. int ret;
  9354. ret = intel_ring_begin(req, 6);
  9355. if (ret)
  9356. return ret;
  9357. if (intel_crtc->plane)
  9358. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9359. else
  9360. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9361. intel_ring_emit(engine, MI_WAIT_FOR_EVENT | flip_mask);
  9362. intel_ring_emit(engine, MI_NOOP);
  9363. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 |
  9364. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9365. intel_ring_emit(engine, fb->pitches[0]);
  9366. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9367. intel_ring_emit(engine, MI_NOOP);
  9368. return 0;
  9369. }
  9370. static int intel_gen4_queue_flip(struct drm_device *dev,
  9371. struct drm_crtc *crtc,
  9372. struct drm_framebuffer *fb,
  9373. struct drm_i915_gem_object *obj,
  9374. struct drm_i915_gem_request *req,
  9375. uint32_t flags)
  9376. {
  9377. struct intel_engine_cs *engine = req->engine;
  9378. struct drm_i915_private *dev_priv = to_i915(dev);
  9379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9380. uint32_t pf, pipesrc;
  9381. int ret;
  9382. ret = intel_ring_begin(req, 4);
  9383. if (ret)
  9384. return ret;
  9385. /* i965+ uses the linear or tiled offsets from the
  9386. * Display Registers (which do not change across a page-flip)
  9387. * so we need only reprogram the base address.
  9388. */
  9389. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9390. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9391. intel_ring_emit(engine, fb->pitches[0]);
  9392. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset |
  9393. obj->tiling_mode);
  9394. /* XXX Enabling the panel-fitter across page-flip is so far
  9395. * untested on non-native modes, so ignore it for now.
  9396. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9397. */
  9398. pf = 0;
  9399. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9400. intel_ring_emit(engine, pf | pipesrc);
  9401. return 0;
  9402. }
  9403. static int intel_gen6_queue_flip(struct drm_device *dev,
  9404. struct drm_crtc *crtc,
  9405. struct drm_framebuffer *fb,
  9406. struct drm_i915_gem_object *obj,
  9407. struct drm_i915_gem_request *req,
  9408. uint32_t flags)
  9409. {
  9410. struct intel_engine_cs *engine = req->engine;
  9411. struct drm_i915_private *dev_priv = to_i915(dev);
  9412. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9413. uint32_t pf, pipesrc;
  9414. int ret;
  9415. ret = intel_ring_begin(req, 4);
  9416. if (ret)
  9417. return ret;
  9418. intel_ring_emit(engine, MI_DISPLAY_FLIP |
  9419. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9420. intel_ring_emit(engine, fb->pitches[0] | obj->tiling_mode);
  9421. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9422. /* Contrary to the suggestions in the documentation,
  9423. * "Enable Panel Fitter" does not seem to be required when page
  9424. * flipping with a non-native mode, and worse causes a normal
  9425. * modeset to fail.
  9426. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9427. */
  9428. pf = 0;
  9429. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9430. intel_ring_emit(engine, pf | pipesrc);
  9431. return 0;
  9432. }
  9433. static int intel_gen7_queue_flip(struct drm_device *dev,
  9434. struct drm_crtc *crtc,
  9435. struct drm_framebuffer *fb,
  9436. struct drm_i915_gem_object *obj,
  9437. struct drm_i915_gem_request *req,
  9438. uint32_t flags)
  9439. {
  9440. struct intel_engine_cs *engine = req->engine;
  9441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9442. uint32_t plane_bit = 0;
  9443. int len, ret;
  9444. switch (intel_crtc->plane) {
  9445. case PLANE_A:
  9446. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9447. break;
  9448. case PLANE_B:
  9449. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9450. break;
  9451. case PLANE_C:
  9452. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9453. break;
  9454. default:
  9455. WARN_ONCE(1, "unknown plane in flip command\n");
  9456. return -ENODEV;
  9457. }
  9458. len = 4;
  9459. if (engine->id == RCS) {
  9460. len += 6;
  9461. /*
  9462. * On Gen 8, SRM is now taking an extra dword to accommodate
  9463. * 48bits addresses, and we need a NOOP for the batch size to
  9464. * stay even.
  9465. */
  9466. if (IS_GEN8(dev))
  9467. len += 2;
  9468. }
  9469. /*
  9470. * BSpec MI_DISPLAY_FLIP for IVB:
  9471. * "The full packet must be contained within the same cache line."
  9472. *
  9473. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9474. * cacheline, if we ever start emitting more commands before
  9475. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9476. * then do the cacheline alignment, and finally emit the
  9477. * MI_DISPLAY_FLIP.
  9478. */
  9479. ret = intel_ring_cacheline_align(req);
  9480. if (ret)
  9481. return ret;
  9482. ret = intel_ring_begin(req, len);
  9483. if (ret)
  9484. return ret;
  9485. /* Unmask the flip-done completion message. Note that the bspec says that
  9486. * we should do this for both the BCS and RCS, and that we must not unmask
  9487. * more than one flip event at any time (or ensure that one flip message
  9488. * can be sent by waiting for flip-done prior to queueing new flips).
  9489. * Experimentation says that BCS works despite DERRMR masking all
  9490. * flip-done completion events and that unmasking all planes at once
  9491. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9492. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9493. */
  9494. if (engine->id == RCS) {
  9495. intel_ring_emit(engine, MI_LOAD_REGISTER_IMM(1));
  9496. intel_ring_emit_reg(engine, DERRMR);
  9497. intel_ring_emit(engine, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9498. DERRMR_PIPEB_PRI_FLIP_DONE |
  9499. DERRMR_PIPEC_PRI_FLIP_DONE));
  9500. if (IS_GEN8(dev))
  9501. intel_ring_emit(engine, MI_STORE_REGISTER_MEM_GEN8 |
  9502. MI_SRM_LRM_GLOBAL_GTT);
  9503. else
  9504. intel_ring_emit(engine, MI_STORE_REGISTER_MEM |
  9505. MI_SRM_LRM_GLOBAL_GTT);
  9506. intel_ring_emit_reg(engine, DERRMR);
  9507. intel_ring_emit(engine, engine->scratch.gtt_offset + 256);
  9508. if (IS_GEN8(dev)) {
  9509. intel_ring_emit(engine, 0);
  9510. intel_ring_emit(engine, MI_NOOP);
  9511. }
  9512. }
  9513. intel_ring_emit(engine, MI_DISPLAY_FLIP_I915 | plane_bit);
  9514. intel_ring_emit(engine, (fb->pitches[0] | obj->tiling_mode));
  9515. intel_ring_emit(engine, intel_crtc->flip_work->gtt_offset);
  9516. intel_ring_emit(engine, (MI_NOOP));
  9517. return 0;
  9518. }
  9519. static bool use_mmio_flip(struct intel_engine_cs *engine,
  9520. struct drm_i915_gem_object *obj)
  9521. {
  9522. struct reservation_object *resv;
  9523. /*
  9524. * This is not being used for older platforms, because
  9525. * non-availability of flip done interrupt forces us to use
  9526. * CS flips. Older platforms derive flip done using some clever
  9527. * tricks involving the flip_pending status bits and vblank irqs.
  9528. * So using MMIO flips there would disrupt this mechanism.
  9529. */
  9530. if (engine == NULL)
  9531. return true;
  9532. if (INTEL_GEN(engine->i915) < 5)
  9533. return false;
  9534. if (i915.use_mmio_flip < 0)
  9535. return false;
  9536. else if (i915.use_mmio_flip > 0)
  9537. return true;
  9538. else if (i915.enable_execlists)
  9539. return true;
  9540. resv = i915_gem_object_get_dmabuf_resv(obj);
  9541. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  9542. return true;
  9543. return engine != i915_gem_request_get_engine(obj->last_write_req);
  9544. }
  9545. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9546. unsigned int rotation,
  9547. struct intel_flip_work *work)
  9548. {
  9549. struct drm_device *dev = intel_crtc->base.dev;
  9550. struct drm_i915_private *dev_priv = to_i915(dev);
  9551. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9552. const enum pipe pipe = intel_crtc->pipe;
  9553. u32 ctl, stride, tile_height;
  9554. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9555. ctl &= ~PLANE_CTL_TILED_MASK;
  9556. switch (fb->modifier[0]) {
  9557. case DRM_FORMAT_MOD_NONE:
  9558. break;
  9559. case I915_FORMAT_MOD_X_TILED:
  9560. ctl |= PLANE_CTL_TILED_X;
  9561. break;
  9562. case I915_FORMAT_MOD_Y_TILED:
  9563. ctl |= PLANE_CTL_TILED_Y;
  9564. break;
  9565. case I915_FORMAT_MOD_Yf_TILED:
  9566. ctl |= PLANE_CTL_TILED_YF;
  9567. break;
  9568. default:
  9569. MISSING_CASE(fb->modifier[0]);
  9570. }
  9571. /*
  9572. * The stride is either expressed as a multiple of 64 bytes chunks for
  9573. * linear buffers or in number of tiles for tiled buffers.
  9574. */
  9575. if (intel_rotation_90_or_270(rotation)) {
  9576. /* stride = Surface height in tiles */
  9577. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9578. stride = DIV_ROUND_UP(fb->height, tile_height);
  9579. } else {
  9580. stride = fb->pitches[0] /
  9581. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9582. fb->pixel_format);
  9583. }
  9584. /*
  9585. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9586. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9587. */
  9588. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9589. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9590. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9591. POSTING_READ(PLANE_SURF(pipe, 0));
  9592. }
  9593. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9594. struct intel_flip_work *work)
  9595. {
  9596. struct drm_device *dev = intel_crtc->base.dev;
  9597. struct drm_i915_private *dev_priv = to_i915(dev);
  9598. struct intel_framebuffer *intel_fb =
  9599. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9600. struct drm_i915_gem_object *obj = intel_fb->obj;
  9601. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9602. u32 dspcntr;
  9603. dspcntr = I915_READ(reg);
  9604. if (obj->tiling_mode != I915_TILING_NONE)
  9605. dspcntr |= DISPPLANE_TILED;
  9606. else
  9607. dspcntr &= ~DISPPLANE_TILED;
  9608. I915_WRITE(reg, dspcntr);
  9609. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9610. POSTING_READ(DSPSURF(intel_crtc->plane));
  9611. }
  9612. static void intel_mmio_flip_work_func(struct work_struct *w)
  9613. {
  9614. struct intel_flip_work *work =
  9615. container_of(w, struct intel_flip_work, mmio_work);
  9616. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9617. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9618. struct intel_framebuffer *intel_fb =
  9619. to_intel_framebuffer(crtc->base.primary->fb);
  9620. struct drm_i915_gem_object *obj = intel_fb->obj;
  9621. struct reservation_object *resv;
  9622. if (work->flip_queued_req)
  9623. WARN_ON(__i915_wait_request(work->flip_queued_req,
  9624. false, NULL,
  9625. &dev_priv->rps.mmioflips));
  9626. /* For framebuffer backed by dmabuf, wait for fence */
  9627. resv = i915_gem_object_get_dmabuf_resv(obj);
  9628. if (resv)
  9629. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  9630. MAX_SCHEDULE_TIMEOUT) < 0);
  9631. intel_pipe_update_start(crtc);
  9632. if (INTEL_GEN(dev_priv) >= 9)
  9633. skl_do_mmio_flip(crtc, work->rotation, work);
  9634. else
  9635. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9636. ilk_do_mmio_flip(crtc, work);
  9637. intel_pipe_update_end(crtc, work);
  9638. }
  9639. static int intel_default_queue_flip(struct drm_device *dev,
  9640. struct drm_crtc *crtc,
  9641. struct drm_framebuffer *fb,
  9642. struct drm_i915_gem_object *obj,
  9643. struct drm_i915_gem_request *req,
  9644. uint32_t flags)
  9645. {
  9646. return -ENODEV;
  9647. }
  9648. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  9649. struct intel_crtc *intel_crtc,
  9650. struct intel_flip_work *work)
  9651. {
  9652. u32 addr, vblank;
  9653. if (!atomic_read(&work->pending))
  9654. return false;
  9655. smp_rmb();
  9656. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  9657. if (work->flip_ready_vblank == 0) {
  9658. if (work->flip_queued_req &&
  9659. !i915_gem_request_completed(work->flip_queued_req))
  9660. return false;
  9661. work->flip_ready_vblank = vblank;
  9662. }
  9663. if (vblank - work->flip_ready_vblank < 3)
  9664. return false;
  9665. /* Potential stall - if we see that the flip has happened,
  9666. * assume a missed interrupt. */
  9667. if (INTEL_GEN(dev_priv) >= 4)
  9668. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9669. else
  9670. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9671. /* There is a potential issue here with a false positive after a flip
  9672. * to the same address. We could address this by checking for a
  9673. * non-incrementing frame counter.
  9674. */
  9675. return addr == work->gtt_offset;
  9676. }
  9677. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  9678. {
  9679. struct drm_device *dev = &dev_priv->drm;
  9680. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9682. struct intel_flip_work *work;
  9683. WARN_ON(!in_interrupt());
  9684. if (crtc == NULL)
  9685. return;
  9686. spin_lock(&dev->event_lock);
  9687. work = intel_crtc->flip_work;
  9688. if (work != NULL && !is_mmio_work(work) &&
  9689. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  9690. WARN_ONCE(1,
  9691. "Kicking stuck page flip: queued at %d, now %d\n",
  9692. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  9693. page_flip_completed(intel_crtc);
  9694. work = NULL;
  9695. }
  9696. if (work != NULL && !is_mmio_work(work) &&
  9697. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  9698. intel_queue_rps_boost_for_request(work->flip_queued_req);
  9699. spin_unlock(&dev->event_lock);
  9700. }
  9701. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9702. struct drm_framebuffer *fb,
  9703. struct drm_pending_vblank_event *event,
  9704. uint32_t page_flip_flags)
  9705. {
  9706. struct drm_device *dev = crtc->dev;
  9707. struct drm_i915_private *dev_priv = to_i915(dev);
  9708. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9709. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9711. struct drm_plane *primary = crtc->primary;
  9712. enum pipe pipe = intel_crtc->pipe;
  9713. struct intel_flip_work *work;
  9714. struct intel_engine_cs *engine;
  9715. bool mmio_flip;
  9716. struct drm_i915_gem_request *request = NULL;
  9717. int ret;
  9718. /*
  9719. * drm_mode_page_flip_ioctl() should already catch this, but double
  9720. * check to be safe. In the future we may enable pageflipping from
  9721. * a disabled primary plane.
  9722. */
  9723. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9724. return -EBUSY;
  9725. /* Can't change pixel format via MI display flips. */
  9726. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9727. return -EINVAL;
  9728. /*
  9729. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9730. * Note that pitch changes could also affect these register.
  9731. */
  9732. if (INTEL_INFO(dev)->gen > 3 &&
  9733. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9734. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9735. return -EINVAL;
  9736. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9737. goto out_hang;
  9738. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9739. if (work == NULL)
  9740. return -ENOMEM;
  9741. work->event = event;
  9742. work->crtc = crtc;
  9743. work->old_fb = old_fb;
  9744. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  9745. ret = drm_crtc_vblank_get(crtc);
  9746. if (ret)
  9747. goto free_work;
  9748. /* We borrow the event spin lock for protecting flip_work */
  9749. spin_lock_irq(&dev->event_lock);
  9750. if (intel_crtc->flip_work) {
  9751. /* Before declaring the flip queue wedged, check if
  9752. * the hardware completed the operation behind our backs.
  9753. */
  9754. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  9755. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9756. page_flip_completed(intel_crtc);
  9757. } else {
  9758. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9759. spin_unlock_irq(&dev->event_lock);
  9760. drm_crtc_vblank_put(crtc);
  9761. kfree(work);
  9762. return -EBUSY;
  9763. }
  9764. }
  9765. intel_crtc->flip_work = work;
  9766. spin_unlock_irq(&dev->event_lock);
  9767. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9768. flush_workqueue(dev_priv->wq);
  9769. /* Reference the objects for the scheduled work. */
  9770. drm_framebuffer_reference(work->old_fb);
  9771. drm_gem_object_reference(&obj->base);
  9772. crtc->primary->fb = fb;
  9773. update_state_fb(crtc->primary);
  9774. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  9775. to_intel_plane_state(primary->state));
  9776. work->pending_flip_obj = obj;
  9777. ret = i915_mutex_lock_interruptible(dev);
  9778. if (ret)
  9779. goto cleanup;
  9780. intel_crtc->reset_counter = i915_reset_counter(&dev_priv->gpu_error);
  9781. if (__i915_reset_in_progress_or_wedged(intel_crtc->reset_counter)) {
  9782. ret = -EIO;
  9783. goto cleanup;
  9784. }
  9785. atomic_inc(&intel_crtc->unpin_work_count);
  9786. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9787. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9788. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9789. engine = &dev_priv->engine[BCS];
  9790. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9791. /* vlv: DISPLAY_FLIP fails to change tiling */
  9792. engine = NULL;
  9793. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9794. engine = &dev_priv->engine[BCS];
  9795. } else if (INTEL_INFO(dev)->gen >= 7) {
  9796. engine = i915_gem_request_get_engine(obj->last_write_req);
  9797. if (engine == NULL || engine->id != RCS)
  9798. engine = &dev_priv->engine[BCS];
  9799. } else {
  9800. engine = &dev_priv->engine[RCS];
  9801. }
  9802. mmio_flip = use_mmio_flip(engine, obj);
  9803. /* When using CS flips, we want to emit semaphores between rings.
  9804. * However, when using mmio flips we will create a task to do the
  9805. * synchronisation, so all we want here is to pin the framebuffer
  9806. * into the display plane and skip any waits.
  9807. */
  9808. if (!mmio_flip) {
  9809. ret = i915_gem_object_sync(obj, engine, &request);
  9810. if (!ret && !request) {
  9811. request = i915_gem_request_alloc(engine, NULL);
  9812. ret = PTR_ERR_OR_ZERO(request);
  9813. }
  9814. if (ret)
  9815. goto cleanup_pending;
  9816. }
  9817. ret = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  9818. if (ret)
  9819. goto cleanup_pending;
  9820. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9821. obj, 0);
  9822. work->gtt_offset += intel_crtc->dspaddr_offset;
  9823. work->rotation = crtc->primary->state->rotation;
  9824. if (mmio_flip) {
  9825. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  9826. i915_gem_request_assign(&work->flip_queued_req,
  9827. obj->last_write_req);
  9828. schedule_work(&work->mmio_work);
  9829. } else {
  9830. i915_gem_request_assign(&work->flip_queued_req, request);
  9831. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9832. page_flip_flags);
  9833. if (ret)
  9834. goto cleanup_unpin;
  9835. intel_mark_page_flip_active(intel_crtc, work);
  9836. i915_add_request_no_flush(request);
  9837. }
  9838. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  9839. to_intel_plane(primary)->frontbuffer_bit);
  9840. mutex_unlock(&dev->struct_mutex);
  9841. intel_frontbuffer_flip_prepare(dev,
  9842. to_intel_plane(primary)->frontbuffer_bit);
  9843. trace_i915_flip_request(intel_crtc->plane, obj);
  9844. return 0;
  9845. cleanup_unpin:
  9846. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  9847. cleanup_pending:
  9848. if (!IS_ERR_OR_NULL(request))
  9849. i915_add_request_no_flush(request);
  9850. atomic_dec(&intel_crtc->unpin_work_count);
  9851. mutex_unlock(&dev->struct_mutex);
  9852. cleanup:
  9853. crtc->primary->fb = old_fb;
  9854. update_state_fb(crtc->primary);
  9855. drm_gem_object_unreference_unlocked(&obj->base);
  9856. drm_framebuffer_unreference(work->old_fb);
  9857. spin_lock_irq(&dev->event_lock);
  9858. intel_crtc->flip_work = NULL;
  9859. spin_unlock_irq(&dev->event_lock);
  9860. drm_crtc_vblank_put(crtc);
  9861. free_work:
  9862. kfree(work);
  9863. if (ret == -EIO) {
  9864. struct drm_atomic_state *state;
  9865. struct drm_plane_state *plane_state;
  9866. out_hang:
  9867. state = drm_atomic_state_alloc(dev);
  9868. if (!state)
  9869. return -ENOMEM;
  9870. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9871. retry:
  9872. plane_state = drm_atomic_get_plane_state(state, primary);
  9873. ret = PTR_ERR_OR_ZERO(plane_state);
  9874. if (!ret) {
  9875. drm_atomic_set_fb_for_plane(plane_state, fb);
  9876. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9877. if (!ret)
  9878. ret = drm_atomic_commit(state);
  9879. }
  9880. if (ret == -EDEADLK) {
  9881. drm_modeset_backoff(state->acquire_ctx);
  9882. drm_atomic_state_clear(state);
  9883. goto retry;
  9884. }
  9885. if (ret)
  9886. drm_atomic_state_free(state);
  9887. if (ret == 0 && event) {
  9888. spin_lock_irq(&dev->event_lock);
  9889. drm_crtc_send_vblank_event(crtc, event);
  9890. spin_unlock_irq(&dev->event_lock);
  9891. }
  9892. }
  9893. return ret;
  9894. }
  9895. /**
  9896. * intel_wm_need_update - Check whether watermarks need updating
  9897. * @plane: drm plane
  9898. * @state: new plane state
  9899. *
  9900. * Check current plane state versus the new one to determine whether
  9901. * watermarks need to be recalculated.
  9902. *
  9903. * Returns true or false.
  9904. */
  9905. static bool intel_wm_need_update(struct drm_plane *plane,
  9906. struct drm_plane_state *state)
  9907. {
  9908. struct intel_plane_state *new = to_intel_plane_state(state);
  9909. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9910. /* Update watermarks on tiling or size changes. */
  9911. if (new->visible != cur->visible)
  9912. return true;
  9913. if (!cur->base.fb || !new->base.fb)
  9914. return false;
  9915. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9916. cur->base.rotation != new->base.rotation ||
  9917. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9918. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9919. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9920. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9921. return true;
  9922. return false;
  9923. }
  9924. static bool needs_scaling(struct intel_plane_state *state)
  9925. {
  9926. int src_w = drm_rect_width(&state->src) >> 16;
  9927. int src_h = drm_rect_height(&state->src) >> 16;
  9928. int dst_w = drm_rect_width(&state->dst);
  9929. int dst_h = drm_rect_height(&state->dst);
  9930. return (src_w != dst_w || src_h != dst_h);
  9931. }
  9932. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9933. struct drm_plane_state *plane_state)
  9934. {
  9935. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9936. struct drm_crtc *crtc = crtc_state->crtc;
  9937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9938. struct drm_plane *plane = plane_state->plane;
  9939. struct drm_device *dev = crtc->dev;
  9940. struct drm_i915_private *dev_priv = to_i915(dev);
  9941. struct intel_plane_state *old_plane_state =
  9942. to_intel_plane_state(plane->state);
  9943. bool mode_changed = needs_modeset(crtc_state);
  9944. bool was_crtc_enabled = crtc->state->active;
  9945. bool is_crtc_enabled = crtc_state->active;
  9946. bool turn_off, turn_on, visible, was_visible;
  9947. struct drm_framebuffer *fb = plane_state->fb;
  9948. int ret;
  9949. if (INTEL_GEN(dev) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  9950. ret = skl_update_scaler_plane(
  9951. to_intel_crtc_state(crtc_state),
  9952. to_intel_plane_state(plane_state));
  9953. if (ret)
  9954. return ret;
  9955. }
  9956. was_visible = old_plane_state->visible;
  9957. visible = to_intel_plane_state(plane_state)->visible;
  9958. if (!was_crtc_enabled && WARN_ON(was_visible))
  9959. was_visible = false;
  9960. /*
  9961. * Visibility is calculated as if the crtc was on, but
  9962. * after scaler setup everything depends on it being off
  9963. * when the crtc isn't active.
  9964. *
  9965. * FIXME this is wrong for watermarks. Watermarks should also
  9966. * be computed as if the pipe would be active. Perhaps move
  9967. * per-plane wm computation to the .check_plane() hook, and
  9968. * only combine the results from all planes in the current place?
  9969. */
  9970. if (!is_crtc_enabled)
  9971. to_intel_plane_state(plane_state)->visible = visible = false;
  9972. if (!was_visible && !visible)
  9973. return 0;
  9974. if (fb != old_plane_state->base.fb)
  9975. pipe_config->fb_changed = true;
  9976. turn_off = was_visible && (!visible || mode_changed);
  9977. turn_on = visible && (!was_visible || mode_changed);
  9978. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9979. intel_crtc->base.base.id,
  9980. intel_crtc->base.name,
  9981. plane->base.id, plane->name,
  9982. fb ? fb->base.id : -1);
  9983. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9984. plane->base.id, plane->name,
  9985. was_visible, visible,
  9986. turn_off, turn_on, mode_changed);
  9987. if (turn_on) {
  9988. pipe_config->update_wm_pre = true;
  9989. /* must disable cxsr around plane enable/disable */
  9990. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9991. pipe_config->disable_cxsr = true;
  9992. } else if (turn_off) {
  9993. pipe_config->update_wm_post = true;
  9994. /* must disable cxsr around plane enable/disable */
  9995. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9996. pipe_config->disable_cxsr = true;
  9997. } else if (intel_wm_need_update(plane, plane_state)) {
  9998. /* FIXME bollocks */
  9999. pipe_config->update_wm_pre = true;
  10000. pipe_config->update_wm_post = true;
  10001. }
  10002. /* Pre-gen9 platforms need two-step watermark updates */
  10003. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10004. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10005. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10006. if (visible || was_visible)
  10007. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10008. /*
  10009. * WaCxSRDisabledForSpriteScaling:ivb
  10010. *
  10011. * cstate->update_wm was already set above, so this flag will
  10012. * take effect when we commit and program watermarks.
  10013. */
  10014. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  10015. needs_scaling(to_intel_plane_state(plane_state)) &&
  10016. !needs_scaling(old_plane_state))
  10017. pipe_config->disable_lp_wm = true;
  10018. return 0;
  10019. }
  10020. static bool encoders_cloneable(const struct intel_encoder *a,
  10021. const struct intel_encoder *b)
  10022. {
  10023. /* masks could be asymmetric, so check both ways */
  10024. return a == b || (a->cloneable & (1 << b->type) &&
  10025. b->cloneable & (1 << a->type));
  10026. }
  10027. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10028. struct intel_crtc *crtc,
  10029. struct intel_encoder *encoder)
  10030. {
  10031. struct intel_encoder *source_encoder;
  10032. struct drm_connector *connector;
  10033. struct drm_connector_state *connector_state;
  10034. int i;
  10035. for_each_connector_in_state(state, connector, connector_state, i) {
  10036. if (connector_state->crtc != &crtc->base)
  10037. continue;
  10038. source_encoder =
  10039. to_intel_encoder(connector_state->best_encoder);
  10040. if (!encoders_cloneable(encoder, source_encoder))
  10041. return false;
  10042. }
  10043. return true;
  10044. }
  10045. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10046. struct drm_crtc_state *crtc_state)
  10047. {
  10048. struct drm_device *dev = crtc->dev;
  10049. struct drm_i915_private *dev_priv = to_i915(dev);
  10050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10051. struct intel_crtc_state *pipe_config =
  10052. to_intel_crtc_state(crtc_state);
  10053. struct drm_atomic_state *state = crtc_state->state;
  10054. int ret;
  10055. bool mode_changed = needs_modeset(crtc_state);
  10056. if (mode_changed && !crtc_state->active)
  10057. pipe_config->update_wm_post = true;
  10058. if (mode_changed && crtc_state->enable &&
  10059. dev_priv->display.crtc_compute_clock &&
  10060. !WARN_ON(pipe_config->shared_dpll)) {
  10061. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10062. pipe_config);
  10063. if (ret)
  10064. return ret;
  10065. }
  10066. if (crtc_state->color_mgmt_changed) {
  10067. ret = intel_color_check(crtc, crtc_state);
  10068. if (ret)
  10069. return ret;
  10070. /*
  10071. * Changing color management on Intel hardware is
  10072. * handled as part of planes update.
  10073. */
  10074. crtc_state->planes_changed = true;
  10075. }
  10076. ret = 0;
  10077. if (dev_priv->display.compute_pipe_wm) {
  10078. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10079. if (ret) {
  10080. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10081. return ret;
  10082. }
  10083. }
  10084. if (dev_priv->display.compute_intermediate_wm &&
  10085. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10086. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10087. return 0;
  10088. /*
  10089. * Calculate 'intermediate' watermarks that satisfy both the
  10090. * old state and the new state. We can program these
  10091. * immediately.
  10092. */
  10093. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10094. intel_crtc,
  10095. pipe_config);
  10096. if (ret) {
  10097. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10098. return ret;
  10099. }
  10100. } else if (dev_priv->display.compute_intermediate_wm) {
  10101. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10102. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10103. }
  10104. if (INTEL_INFO(dev)->gen >= 9) {
  10105. if (mode_changed)
  10106. ret = skl_update_scaler_crtc(pipe_config);
  10107. if (!ret)
  10108. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10109. pipe_config);
  10110. }
  10111. return ret;
  10112. }
  10113. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10114. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10115. .atomic_begin = intel_begin_crtc_commit,
  10116. .atomic_flush = intel_finish_crtc_commit,
  10117. .atomic_check = intel_crtc_atomic_check,
  10118. };
  10119. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10120. {
  10121. struct intel_connector *connector;
  10122. for_each_intel_connector(dev, connector) {
  10123. if (connector->base.state->crtc)
  10124. drm_connector_unreference(&connector->base);
  10125. if (connector->base.encoder) {
  10126. connector->base.state->best_encoder =
  10127. connector->base.encoder;
  10128. connector->base.state->crtc =
  10129. connector->base.encoder->crtc;
  10130. drm_connector_reference(&connector->base);
  10131. } else {
  10132. connector->base.state->best_encoder = NULL;
  10133. connector->base.state->crtc = NULL;
  10134. }
  10135. }
  10136. }
  10137. static void
  10138. connected_sink_compute_bpp(struct intel_connector *connector,
  10139. struct intel_crtc_state *pipe_config)
  10140. {
  10141. int bpp = pipe_config->pipe_bpp;
  10142. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10143. connector->base.base.id,
  10144. connector->base.name);
  10145. /* Don't use an invalid EDID bpc value */
  10146. if (connector->base.display_info.bpc &&
  10147. connector->base.display_info.bpc * 3 < bpp) {
  10148. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10149. bpp, connector->base.display_info.bpc*3);
  10150. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10151. }
  10152. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10153. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  10154. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10155. bpp);
  10156. pipe_config->pipe_bpp = 24;
  10157. }
  10158. }
  10159. static int
  10160. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10161. struct intel_crtc_state *pipe_config)
  10162. {
  10163. struct drm_device *dev = crtc->base.dev;
  10164. struct drm_atomic_state *state;
  10165. struct drm_connector *connector;
  10166. struct drm_connector_state *connector_state;
  10167. int bpp, i;
  10168. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10169. bpp = 10*3;
  10170. else if (INTEL_INFO(dev)->gen >= 5)
  10171. bpp = 12*3;
  10172. else
  10173. bpp = 8*3;
  10174. pipe_config->pipe_bpp = bpp;
  10175. state = pipe_config->base.state;
  10176. /* Clamp display bpp to EDID value */
  10177. for_each_connector_in_state(state, connector, connector_state, i) {
  10178. if (connector_state->crtc != &crtc->base)
  10179. continue;
  10180. connected_sink_compute_bpp(to_intel_connector(connector),
  10181. pipe_config);
  10182. }
  10183. return bpp;
  10184. }
  10185. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10186. {
  10187. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10188. "type: 0x%x flags: 0x%x\n",
  10189. mode->crtc_clock,
  10190. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10191. mode->crtc_hsync_end, mode->crtc_htotal,
  10192. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10193. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10194. }
  10195. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10196. struct intel_crtc_state *pipe_config,
  10197. const char *context)
  10198. {
  10199. struct drm_device *dev = crtc->base.dev;
  10200. struct drm_plane *plane;
  10201. struct intel_plane *intel_plane;
  10202. struct intel_plane_state *state;
  10203. struct drm_framebuffer *fb;
  10204. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10205. crtc->base.base.id, crtc->base.name,
  10206. context, pipe_config, pipe_name(crtc->pipe));
  10207. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10208. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10209. pipe_config->pipe_bpp, pipe_config->dither);
  10210. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10211. pipe_config->has_pch_encoder,
  10212. pipe_config->fdi_lanes,
  10213. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10214. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10215. pipe_config->fdi_m_n.tu);
  10216. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10217. intel_crtc_has_dp_encoder(pipe_config),
  10218. pipe_config->lane_count,
  10219. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10220. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10221. pipe_config->dp_m_n.tu);
  10222. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10223. intel_crtc_has_dp_encoder(pipe_config),
  10224. pipe_config->lane_count,
  10225. pipe_config->dp_m2_n2.gmch_m,
  10226. pipe_config->dp_m2_n2.gmch_n,
  10227. pipe_config->dp_m2_n2.link_m,
  10228. pipe_config->dp_m2_n2.link_n,
  10229. pipe_config->dp_m2_n2.tu);
  10230. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10231. pipe_config->has_audio,
  10232. pipe_config->has_infoframe);
  10233. DRM_DEBUG_KMS("requested mode:\n");
  10234. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10235. DRM_DEBUG_KMS("adjusted mode:\n");
  10236. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10237. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10238. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10239. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10240. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10241. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10242. crtc->num_scalers,
  10243. pipe_config->scaler_state.scaler_users,
  10244. pipe_config->scaler_state.scaler_id);
  10245. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10246. pipe_config->gmch_pfit.control,
  10247. pipe_config->gmch_pfit.pgm_ratios,
  10248. pipe_config->gmch_pfit.lvds_border_bits);
  10249. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10250. pipe_config->pch_pfit.pos,
  10251. pipe_config->pch_pfit.size,
  10252. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10253. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10254. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10255. if (IS_BROXTON(dev)) {
  10256. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10257. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10258. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10259. pipe_config->ddi_pll_sel,
  10260. pipe_config->dpll_hw_state.ebb0,
  10261. pipe_config->dpll_hw_state.ebb4,
  10262. pipe_config->dpll_hw_state.pll0,
  10263. pipe_config->dpll_hw_state.pll1,
  10264. pipe_config->dpll_hw_state.pll2,
  10265. pipe_config->dpll_hw_state.pll3,
  10266. pipe_config->dpll_hw_state.pll6,
  10267. pipe_config->dpll_hw_state.pll8,
  10268. pipe_config->dpll_hw_state.pll9,
  10269. pipe_config->dpll_hw_state.pll10,
  10270. pipe_config->dpll_hw_state.pcsdw12);
  10271. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10272. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10273. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10274. pipe_config->ddi_pll_sel,
  10275. pipe_config->dpll_hw_state.ctrl1,
  10276. pipe_config->dpll_hw_state.cfgcr1,
  10277. pipe_config->dpll_hw_state.cfgcr2);
  10278. } else if (HAS_DDI(dev)) {
  10279. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10280. pipe_config->ddi_pll_sel,
  10281. pipe_config->dpll_hw_state.wrpll,
  10282. pipe_config->dpll_hw_state.spll);
  10283. } else {
  10284. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10285. "fp0: 0x%x, fp1: 0x%x\n",
  10286. pipe_config->dpll_hw_state.dpll,
  10287. pipe_config->dpll_hw_state.dpll_md,
  10288. pipe_config->dpll_hw_state.fp0,
  10289. pipe_config->dpll_hw_state.fp1);
  10290. }
  10291. DRM_DEBUG_KMS("planes on this crtc\n");
  10292. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10293. intel_plane = to_intel_plane(plane);
  10294. if (intel_plane->pipe != crtc->pipe)
  10295. continue;
  10296. state = to_intel_plane_state(plane->state);
  10297. fb = state->base.fb;
  10298. if (!fb) {
  10299. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10300. plane->base.id, plane->name, state->scaler_id);
  10301. continue;
  10302. }
  10303. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10304. plane->base.id, plane->name);
  10305. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10306. fb->base.id, fb->width, fb->height,
  10307. drm_get_format_name(fb->pixel_format));
  10308. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10309. state->scaler_id,
  10310. state->src.x1 >> 16, state->src.y1 >> 16,
  10311. drm_rect_width(&state->src) >> 16,
  10312. drm_rect_height(&state->src) >> 16,
  10313. state->dst.x1, state->dst.y1,
  10314. drm_rect_width(&state->dst),
  10315. drm_rect_height(&state->dst));
  10316. }
  10317. }
  10318. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10319. {
  10320. struct drm_device *dev = state->dev;
  10321. struct drm_connector *connector;
  10322. unsigned int used_ports = 0;
  10323. /*
  10324. * Walk the connector list instead of the encoder
  10325. * list to detect the problem on ddi platforms
  10326. * where there's just one encoder per digital port.
  10327. */
  10328. drm_for_each_connector(connector, dev) {
  10329. struct drm_connector_state *connector_state;
  10330. struct intel_encoder *encoder;
  10331. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10332. if (!connector_state)
  10333. connector_state = connector->state;
  10334. if (!connector_state->best_encoder)
  10335. continue;
  10336. encoder = to_intel_encoder(connector_state->best_encoder);
  10337. WARN_ON(!connector_state->crtc);
  10338. switch (encoder->type) {
  10339. unsigned int port_mask;
  10340. case INTEL_OUTPUT_UNKNOWN:
  10341. if (WARN_ON(!HAS_DDI(dev)))
  10342. break;
  10343. case INTEL_OUTPUT_DP:
  10344. case INTEL_OUTPUT_HDMI:
  10345. case INTEL_OUTPUT_EDP:
  10346. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10347. /* the same port mustn't appear more than once */
  10348. if (used_ports & port_mask)
  10349. return false;
  10350. used_ports |= port_mask;
  10351. default:
  10352. break;
  10353. }
  10354. }
  10355. return true;
  10356. }
  10357. static void
  10358. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10359. {
  10360. struct drm_crtc_state tmp_state;
  10361. struct intel_crtc_scaler_state scaler_state;
  10362. struct intel_dpll_hw_state dpll_hw_state;
  10363. struct intel_shared_dpll *shared_dpll;
  10364. uint32_t ddi_pll_sel;
  10365. bool force_thru;
  10366. /* FIXME: before the switch to atomic started, a new pipe_config was
  10367. * kzalloc'd. Code that depends on any field being zero should be
  10368. * fixed, so that the crtc_state can be safely duplicated. For now,
  10369. * only fields that are know to not cause problems are preserved. */
  10370. tmp_state = crtc_state->base;
  10371. scaler_state = crtc_state->scaler_state;
  10372. shared_dpll = crtc_state->shared_dpll;
  10373. dpll_hw_state = crtc_state->dpll_hw_state;
  10374. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10375. force_thru = crtc_state->pch_pfit.force_thru;
  10376. memset(crtc_state, 0, sizeof *crtc_state);
  10377. crtc_state->base = tmp_state;
  10378. crtc_state->scaler_state = scaler_state;
  10379. crtc_state->shared_dpll = shared_dpll;
  10380. crtc_state->dpll_hw_state = dpll_hw_state;
  10381. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10382. crtc_state->pch_pfit.force_thru = force_thru;
  10383. }
  10384. static int
  10385. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10386. struct intel_crtc_state *pipe_config)
  10387. {
  10388. struct drm_atomic_state *state = pipe_config->base.state;
  10389. struct intel_encoder *encoder;
  10390. struct drm_connector *connector;
  10391. struct drm_connector_state *connector_state;
  10392. int base_bpp, ret = -EINVAL;
  10393. int i;
  10394. bool retry = true;
  10395. clear_intel_crtc_state(pipe_config);
  10396. pipe_config->cpu_transcoder =
  10397. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10398. /*
  10399. * Sanitize sync polarity flags based on requested ones. If neither
  10400. * positive or negative polarity is requested, treat this as meaning
  10401. * negative polarity.
  10402. */
  10403. if (!(pipe_config->base.adjusted_mode.flags &
  10404. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10405. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10406. if (!(pipe_config->base.adjusted_mode.flags &
  10407. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10408. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10409. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10410. pipe_config);
  10411. if (base_bpp < 0)
  10412. goto fail;
  10413. /*
  10414. * Determine the real pipe dimensions. Note that stereo modes can
  10415. * increase the actual pipe size due to the frame doubling and
  10416. * insertion of additional space for blanks between the frame. This
  10417. * is stored in the crtc timings. We use the requested mode to do this
  10418. * computation to clearly distinguish it from the adjusted mode, which
  10419. * can be changed by the connectors in the below retry loop.
  10420. */
  10421. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10422. &pipe_config->pipe_src_w,
  10423. &pipe_config->pipe_src_h);
  10424. for_each_connector_in_state(state, connector, connector_state, i) {
  10425. if (connector_state->crtc != crtc)
  10426. continue;
  10427. encoder = to_intel_encoder(connector_state->best_encoder);
  10428. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10429. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10430. goto fail;
  10431. }
  10432. /*
  10433. * Determine output_types before calling the .compute_config()
  10434. * hooks so that the hooks can use this information safely.
  10435. */
  10436. pipe_config->output_types |= 1 << encoder->type;
  10437. }
  10438. encoder_retry:
  10439. /* Ensure the port clock defaults are reset when retrying. */
  10440. pipe_config->port_clock = 0;
  10441. pipe_config->pixel_multiplier = 1;
  10442. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10443. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10444. CRTC_STEREO_DOUBLE);
  10445. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10446. * adjust it according to limitations or connector properties, and also
  10447. * a chance to reject the mode entirely.
  10448. */
  10449. for_each_connector_in_state(state, connector, connector_state, i) {
  10450. if (connector_state->crtc != crtc)
  10451. continue;
  10452. encoder = to_intel_encoder(connector_state->best_encoder);
  10453. if (!(encoder->compute_config(encoder, pipe_config))) {
  10454. DRM_DEBUG_KMS("Encoder config failure\n");
  10455. goto fail;
  10456. }
  10457. }
  10458. /* Set default port clock if not overwritten by the encoder. Needs to be
  10459. * done afterwards in case the encoder adjusts the mode. */
  10460. if (!pipe_config->port_clock)
  10461. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10462. * pipe_config->pixel_multiplier;
  10463. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10464. if (ret < 0) {
  10465. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10466. goto fail;
  10467. }
  10468. if (ret == RETRY) {
  10469. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10470. ret = -EINVAL;
  10471. goto fail;
  10472. }
  10473. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10474. retry = false;
  10475. goto encoder_retry;
  10476. }
  10477. /* Dithering seems to not pass-through bits correctly when it should, so
  10478. * only enable it on 6bpc panels. */
  10479. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10480. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10481. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10482. fail:
  10483. return ret;
  10484. }
  10485. static void
  10486. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10487. {
  10488. struct drm_crtc *crtc;
  10489. struct drm_crtc_state *crtc_state;
  10490. int i;
  10491. /* Double check state. */
  10492. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10493. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10494. /* Update hwmode for vblank functions */
  10495. if (crtc->state->active)
  10496. crtc->hwmode = crtc->state->adjusted_mode;
  10497. else
  10498. crtc->hwmode.crtc_clock = 0;
  10499. /*
  10500. * Update legacy state to satisfy fbc code. This can
  10501. * be removed when fbc uses the atomic state.
  10502. */
  10503. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10504. struct drm_plane_state *plane_state = crtc->primary->state;
  10505. crtc->primary->fb = plane_state->fb;
  10506. crtc->x = plane_state->src_x >> 16;
  10507. crtc->y = plane_state->src_y >> 16;
  10508. }
  10509. }
  10510. }
  10511. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10512. {
  10513. int diff;
  10514. if (clock1 == clock2)
  10515. return true;
  10516. if (!clock1 || !clock2)
  10517. return false;
  10518. diff = abs(clock1 - clock2);
  10519. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10520. return true;
  10521. return false;
  10522. }
  10523. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10524. list_for_each_entry((intel_crtc), \
  10525. &(dev)->mode_config.crtc_list, \
  10526. base.head) \
  10527. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10528. static bool
  10529. intel_compare_m_n(unsigned int m, unsigned int n,
  10530. unsigned int m2, unsigned int n2,
  10531. bool exact)
  10532. {
  10533. if (m == m2 && n == n2)
  10534. return true;
  10535. if (exact || !m || !n || !m2 || !n2)
  10536. return false;
  10537. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10538. if (n > n2) {
  10539. while (n > n2) {
  10540. m2 <<= 1;
  10541. n2 <<= 1;
  10542. }
  10543. } else if (n < n2) {
  10544. while (n < n2) {
  10545. m <<= 1;
  10546. n <<= 1;
  10547. }
  10548. }
  10549. if (n != n2)
  10550. return false;
  10551. return intel_fuzzy_clock_check(m, m2);
  10552. }
  10553. static bool
  10554. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10555. struct intel_link_m_n *m2_n2,
  10556. bool adjust)
  10557. {
  10558. if (m_n->tu == m2_n2->tu &&
  10559. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10560. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10561. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10562. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10563. if (adjust)
  10564. *m2_n2 = *m_n;
  10565. return true;
  10566. }
  10567. return false;
  10568. }
  10569. static bool
  10570. intel_pipe_config_compare(struct drm_device *dev,
  10571. struct intel_crtc_state *current_config,
  10572. struct intel_crtc_state *pipe_config,
  10573. bool adjust)
  10574. {
  10575. bool ret = true;
  10576. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10577. do { \
  10578. if (!adjust) \
  10579. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10580. else \
  10581. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10582. } while (0)
  10583. #define PIPE_CONF_CHECK_X(name) \
  10584. if (current_config->name != pipe_config->name) { \
  10585. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10586. "(expected 0x%08x, found 0x%08x)\n", \
  10587. current_config->name, \
  10588. pipe_config->name); \
  10589. ret = false; \
  10590. }
  10591. #define PIPE_CONF_CHECK_I(name) \
  10592. if (current_config->name != pipe_config->name) { \
  10593. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10594. "(expected %i, found %i)\n", \
  10595. current_config->name, \
  10596. pipe_config->name); \
  10597. ret = false; \
  10598. }
  10599. #define PIPE_CONF_CHECK_P(name) \
  10600. if (current_config->name != pipe_config->name) { \
  10601. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10602. "(expected %p, found %p)\n", \
  10603. current_config->name, \
  10604. pipe_config->name); \
  10605. ret = false; \
  10606. }
  10607. #define PIPE_CONF_CHECK_M_N(name) \
  10608. if (!intel_compare_link_m_n(&current_config->name, \
  10609. &pipe_config->name,\
  10610. adjust)) { \
  10611. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10612. "(expected tu %i gmch %i/%i link %i/%i, " \
  10613. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10614. current_config->name.tu, \
  10615. current_config->name.gmch_m, \
  10616. current_config->name.gmch_n, \
  10617. current_config->name.link_m, \
  10618. current_config->name.link_n, \
  10619. pipe_config->name.tu, \
  10620. pipe_config->name.gmch_m, \
  10621. pipe_config->name.gmch_n, \
  10622. pipe_config->name.link_m, \
  10623. pipe_config->name.link_n); \
  10624. ret = false; \
  10625. }
  10626. /* This is required for BDW+ where there is only one set of registers for
  10627. * switching between high and low RR.
  10628. * This macro can be used whenever a comparison has to be made between one
  10629. * hw state and multiple sw state variables.
  10630. */
  10631. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10632. if (!intel_compare_link_m_n(&current_config->name, \
  10633. &pipe_config->name, adjust) && \
  10634. !intel_compare_link_m_n(&current_config->alt_name, \
  10635. &pipe_config->name, adjust)) { \
  10636. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10637. "(expected tu %i gmch %i/%i link %i/%i, " \
  10638. "or tu %i gmch %i/%i link %i/%i, " \
  10639. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10640. current_config->name.tu, \
  10641. current_config->name.gmch_m, \
  10642. current_config->name.gmch_n, \
  10643. current_config->name.link_m, \
  10644. current_config->name.link_n, \
  10645. current_config->alt_name.tu, \
  10646. current_config->alt_name.gmch_m, \
  10647. current_config->alt_name.gmch_n, \
  10648. current_config->alt_name.link_m, \
  10649. current_config->alt_name.link_n, \
  10650. pipe_config->name.tu, \
  10651. pipe_config->name.gmch_m, \
  10652. pipe_config->name.gmch_n, \
  10653. pipe_config->name.link_m, \
  10654. pipe_config->name.link_n); \
  10655. ret = false; \
  10656. }
  10657. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10658. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10659. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10660. "(expected %i, found %i)\n", \
  10661. current_config->name & (mask), \
  10662. pipe_config->name & (mask)); \
  10663. ret = false; \
  10664. }
  10665. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10666. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10667. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10668. "(expected %i, found %i)\n", \
  10669. current_config->name, \
  10670. pipe_config->name); \
  10671. ret = false; \
  10672. }
  10673. #define PIPE_CONF_QUIRK(quirk) \
  10674. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10675. PIPE_CONF_CHECK_I(cpu_transcoder);
  10676. PIPE_CONF_CHECK_I(has_pch_encoder);
  10677. PIPE_CONF_CHECK_I(fdi_lanes);
  10678. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10679. PIPE_CONF_CHECK_I(lane_count);
  10680. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  10681. if (INTEL_INFO(dev)->gen < 8) {
  10682. PIPE_CONF_CHECK_M_N(dp_m_n);
  10683. if (current_config->has_drrs)
  10684. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10685. } else
  10686. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10687. PIPE_CONF_CHECK_X(output_types);
  10688. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10689. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10690. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10691. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10692. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10693. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10694. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10695. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10696. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10697. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10698. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10699. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10700. PIPE_CONF_CHECK_I(pixel_multiplier);
  10701. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10702. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10703. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10704. PIPE_CONF_CHECK_I(limited_color_range);
  10705. PIPE_CONF_CHECK_I(has_infoframe);
  10706. PIPE_CONF_CHECK_I(has_audio);
  10707. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10708. DRM_MODE_FLAG_INTERLACE);
  10709. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10710. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10711. DRM_MODE_FLAG_PHSYNC);
  10712. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10713. DRM_MODE_FLAG_NHSYNC);
  10714. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10715. DRM_MODE_FLAG_PVSYNC);
  10716. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10717. DRM_MODE_FLAG_NVSYNC);
  10718. }
  10719. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10720. /* pfit ratios are autocomputed by the hw on gen4+ */
  10721. if (INTEL_INFO(dev)->gen < 4)
  10722. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10723. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10724. if (!adjust) {
  10725. PIPE_CONF_CHECK_I(pipe_src_w);
  10726. PIPE_CONF_CHECK_I(pipe_src_h);
  10727. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10728. if (current_config->pch_pfit.enabled) {
  10729. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10730. PIPE_CONF_CHECK_X(pch_pfit.size);
  10731. }
  10732. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10733. }
  10734. /* BDW+ don't expose a synchronous way to read the state */
  10735. if (IS_HASWELL(dev))
  10736. PIPE_CONF_CHECK_I(ips_enabled);
  10737. PIPE_CONF_CHECK_I(double_wide);
  10738. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10739. PIPE_CONF_CHECK_P(shared_dpll);
  10740. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10741. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10742. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10743. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10744. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10745. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10746. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10747. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10748. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10749. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10750. PIPE_CONF_CHECK_X(dsi_pll.div);
  10751. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10752. PIPE_CONF_CHECK_I(pipe_bpp);
  10753. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10754. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10755. #undef PIPE_CONF_CHECK_X
  10756. #undef PIPE_CONF_CHECK_I
  10757. #undef PIPE_CONF_CHECK_P
  10758. #undef PIPE_CONF_CHECK_FLAGS
  10759. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10760. #undef PIPE_CONF_QUIRK
  10761. #undef INTEL_ERR_OR_DBG_KMS
  10762. return ret;
  10763. }
  10764. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10765. const struct intel_crtc_state *pipe_config)
  10766. {
  10767. if (pipe_config->has_pch_encoder) {
  10768. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10769. &pipe_config->fdi_m_n);
  10770. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10771. /*
  10772. * FDI already provided one idea for the dotclock.
  10773. * Yell if the encoder disagrees.
  10774. */
  10775. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10776. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10777. fdi_dotclock, dotclock);
  10778. }
  10779. }
  10780. static void verify_wm_state(struct drm_crtc *crtc,
  10781. struct drm_crtc_state *new_state)
  10782. {
  10783. struct drm_device *dev = crtc->dev;
  10784. struct drm_i915_private *dev_priv = to_i915(dev);
  10785. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10786. struct skl_ddb_entry *hw_entry, *sw_entry;
  10787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10788. const enum pipe pipe = intel_crtc->pipe;
  10789. int plane;
  10790. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10791. return;
  10792. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10793. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10794. /* planes */
  10795. for_each_plane(dev_priv, pipe, plane) {
  10796. hw_entry = &hw_ddb.plane[pipe][plane];
  10797. sw_entry = &sw_ddb->plane[pipe][plane];
  10798. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10799. continue;
  10800. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10801. "(expected (%u,%u), found (%u,%u))\n",
  10802. pipe_name(pipe), plane + 1,
  10803. sw_entry->start, sw_entry->end,
  10804. hw_entry->start, hw_entry->end);
  10805. }
  10806. /* cursor */
  10807. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10808. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10809. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10810. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10811. "(expected (%u,%u), found (%u,%u))\n",
  10812. pipe_name(pipe),
  10813. sw_entry->start, sw_entry->end,
  10814. hw_entry->start, hw_entry->end);
  10815. }
  10816. }
  10817. static void
  10818. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10819. {
  10820. struct drm_connector *connector;
  10821. drm_for_each_connector(connector, dev) {
  10822. struct drm_encoder *encoder = connector->encoder;
  10823. struct drm_connector_state *state = connector->state;
  10824. if (state->crtc != crtc)
  10825. continue;
  10826. intel_connector_verify_state(to_intel_connector(connector));
  10827. I915_STATE_WARN(state->best_encoder != encoder,
  10828. "connector's atomic encoder doesn't match legacy encoder\n");
  10829. }
  10830. }
  10831. static void
  10832. verify_encoder_state(struct drm_device *dev)
  10833. {
  10834. struct intel_encoder *encoder;
  10835. struct intel_connector *connector;
  10836. for_each_intel_encoder(dev, encoder) {
  10837. bool enabled = false;
  10838. enum pipe pipe;
  10839. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10840. encoder->base.base.id,
  10841. encoder->base.name);
  10842. for_each_intel_connector(dev, connector) {
  10843. if (connector->base.state->best_encoder != &encoder->base)
  10844. continue;
  10845. enabled = true;
  10846. I915_STATE_WARN(connector->base.state->crtc !=
  10847. encoder->base.crtc,
  10848. "connector's crtc doesn't match encoder crtc\n");
  10849. }
  10850. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10851. "encoder's enabled state mismatch "
  10852. "(expected %i, found %i)\n",
  10853. !!encoder->base.crtc, enabled);
  10854. if (!encoder->base.crtc) {
  10855. bool active;
  10856. active = encoder->get_hw_state(encoder, &pipe);
  10857. I915_STATE_WARN(active,
  10858. "encoder detached but still enabled on pipe %c.\n",
  10859. pipe_name(pipe));
  10860. }
  10861. }
  10862. }
  10863. static void
  10864. verify_crtc_state(struct drm_crtc *crtc,
  10865. struct drm_crtc_state *old_crtc_state,
  10866. struct drm_crtc_state *new_crtc_state)
  10867. {
  10868. struct drm_device *dev = crtc->dev;
  10869. struct drm_i915_private *dev_priv = to_i915(dev);
  10870. struct intel_encoder *encoder;
  10871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10872. struct intel_crtc_state *pipe_config, *sw_config;
  10873. struct drm_atomic_state *old_state;
  10874. bool active;
  10875. old_state = old_crtc_state->state;
  10876. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10877. pipe_config = to_intel_crtc_state(old_crtc_state);
  10878. memset(pipe_config, 0, sizeof(*pipe_config));
  10879. pipe_config->base.crtc = crtc;
  10880. pipe_config->base.state = old_state;
  10881. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10882. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10883. /* hw state is inconsistent with the pipe quirk */
  10884. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10885. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10886. active = new_crtc_state->active;
  10887. I915_STATE_WARN(new_crtc_state->active != active,
  10888. "crtc active state doesn't match with hw state "
  10889. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10890. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10891. "transitional active state does not match atomic hw state "
  10892. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10893. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10894. enum pipe pipe;
  10895. active = encoder->get_hw_state(encoder, &pipe);
  10896. I915_STATE_WARN(active != new_crtc_state->active,
  10897. "[ENCODER:%i] active %i with crtc active %i\n",
  10898. encoder->base.base.id, active, new_crtc_state->active);
  10899. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10900. "Encoder connected to wrong pipe %c\n",
  10901. pipe_name(pipe));
  10902. if (active) {
  10903. pipe_config->output_types |= 1 << encoder->type;
  10904. encoder->get_config(encoder, pipe_config);
  10905. }
  10906. }
  10907. if (!new_crtc_state->active)
  10908. return;
  10909. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10910. sw_config = to_intel_crtc_state(crtc->state);
  10911. if (!intel_pipe_config_compare(dev, sw_config,
  10912. pipe_config, false)) {
  10913. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10914. intel_dump_pipe_config(intel_crtc, pipe_config,
  10915. "[hw state]");
  10916. intel_dump_pipe_config(intel_crtc, sw_config,
  10917. "[sw state]");
  10918. }
  10919. }
  10920. static void
  10921. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10922. struct intel_shared_dpll *pll,
  10923. struct drm_crtc *crtc,
  10924. struct drm_crtc_state *new_state)
  10925. {
  10926. struct intel_dpll_hw_state dpll_hw_state;
  10927. unsigned crtc_mask;
  10928. bool active;
  10929. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10930. DRM_DEBUG_KMS("%s\n", pll->name);
  10931. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10932. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10933. I915_STATE_WARN(!pll->on && pll->active_mask,
  10934. "pll in active use but not on in sw tracking\n");
  10935. I915_STATE_WARN(pll->on && !pll->active_mask,
  10936. "pll is on but not used by any active crtc\n");
  10937. I915_STATE_WARN(pll->on != active,
  10938. "pll on state mismatch (expected %i, found %i)\n",
  10939. pll->on, active);
  10940. }
  10941. if (!crtc) {
  10942. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10943. "more active pll users than references: %x vs %x\n",
  10944. pll->active_mask, pll->config.crtc_mask);
  10945. return;
  10946. }
  10947. crtc_mask = 1 << drm_crtc_index(crtc);
  10948. if (new_state->active)
  10949. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10950. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10951. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10952. else
  10953. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10954. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10955. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10956. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10957. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10958. crtc_mask, pll->config.crtc_mask);
  10959. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10960. &dpll_hw_state,
  10961. sizeof(dpll_hw_state)),
  10962. "pll hw state mismatch\n");
  10963. }
  10964. static void
  10965. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10966. struct drm_crtc_state *old_crtc_state,
  10967. struct drm_crtc_state *new_crtc_state)
  10968. {
  10969. struct drm_i915_private *dev_priv = to_i915(dev);
  10970. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10971. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10972. if (new_state->shared_dpll)
  10973. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10974. if (old_state->shared_dpll &&
  10975. old_state->shared_dpll != new_state->shared_dpll) {
  10976. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10977. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10978. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10979. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10980. pipe_name(drm_crtc_index(crtc)));
  10981. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10982. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10983. pipe_name(drm_crtc_index(crtc)));
  10984. }
  10985. }
  10986. static void
  10987. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10988. struct drm_crtc_state *old_state,
  10989. struct drm_crtc_state *new_state)
  10990. {
  10991. if (!needs_modeset(new_state) &&
  10992. !to_intel_crtc_state(new_state)->update_pipe)
  10993. return;
  10994. verify_wm_state(crtc, new_state);
  10995. verify_connector_state(crtc->dev, crtc);
  10996. verify_crtc_state(crtc, old_state, new_state);
  10997. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10998. }
  10999. static void
  11000. verify_disabled_dpll_state(struct drm_device *dev)
  11001. {
  11002. struct drm_i915_private *dev_priv = to_i915(dev);
  11003. int i;
  11004. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11005. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11006. }
  11007. static void
  11008. intel_modeset_verify_disabled(struct drm_device *dev)
  11009. {
  11010. verify_encoder_state(dev);
  11011. verify_connector_state(dev, NULL);
  11012. verify_disabled_dpll_state(dev);
  11013. }
  11014. static void update_scanline_offset(struct intel_crtc *crtc)
  11015. {
  11016. struct drm_device *dev = crtc->base.dev;
  11017. /*
  11018. * The scanline counter increments at the leading edge of hsync.
  11019. *
  11020. * On most platforms it starts counting from vtotal-1 on the
  11021. * first active line. That means the scanline counter value is
  11022. * always one less than what we would expect. Ie. just after
  11023. * start of vblank, which also occurs at start of hsync (on the
  11024. * last active line), the scanline counter will read vblank_start-1.
  11025. *
  11026. * On gen2 the scanline counter starts counting from 1 instead
  11027. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11028. * to keep the value positive), instead of adding one.
  11029. *
  11030. * On HSW+ the behaviour of the scanline counter depends on the output
  11031. * type. For DP ports it behaves like most other platforms, but on HDMI
  11032. * there's an extra 1 line difference. So we need to add two instead of
  11033. * one to the value.
  11034. */
  11035. if (IS_GEN2(dev)) {
  11036. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11037. int vtotal;
  11038. vtotal = adjusted_mode->crtc_vtotal;
  11039. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11040. vtotal /= 2;
  11041. crtc->scanline_offset = vtotal - 1;
  11042. } else if (HAS_DDI(dev) &&
  11043. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11044. crtc->scanline_offset = 2;
  11045. } else
  11046. crtc->scanline_offset = 1;
  11047. }
  11048. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11049. {
  11050. struct drm_device *dev = state->dev;
  11051. struct drm_i915_private *dev_priv = to_i915(dev);
  11052. struct intel_shared_dpll_config *shared_dpll = NULL;
  11053. struct drm_crtc *crtc;
  11054. struct drm_crtc_state *crtc_state;
  11055. int i;
  11056. if (!dev_priv->display.crtc_compute_clock)
  11057. return;
  11058. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11059. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11060. struct intel_shared_dpll *old_dpll =
  11061. to_intel_crtc_state(crtc->state)->shared_dpll;
  11062. if (!needs_modeset(crtc_state))
  11063. continue;
  11064. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11065. if (!old_dpll)
  11066. continue;
  11067. if (!shared_dpll)
  11068. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11069. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11070. }
  11071. }
  11072. /*
  11073. * This implements the workaround described in the "notes" section of the mode
  11074. * set sequence documentation. When going from no pipes or single pipe to
  11075. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11076. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11077. */
  11078. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11079. {
  11080. struct drm_crtc_state *crtc_state;
  11081. struct intel_crtc *intel_crtc;
  11082. struct drm_crtc *crtc;
  11083. struct intel_crtc_state *first_crtc_state = NULL;
  11084. struct intel_crtc_state *other_crtc_state = NULL;
  11085. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11086. int i;
  11087. /* look at all crtc's that are going to be enabled in during modeset */
  11088. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11089. intel_crtc = to_intel_crtc(crtc);
  11090. if (!crtc_state->active || !needs_modeset(crtc_state))
  11091. continue;
  11092. if (first_crtc_state) {
  11093. other_crtc_state = to_intel_crtc_state(crtc_state);
  11094. break;
  11095. } else {
  11096. first_crtc_state = to_intel_crtc_state(crtc_state);
  11097. first_pipe = intel_crtc->pipe;
  11098. }
  11099. }
  11100. /* No workaround needed? */
  11101. if (!first_crtc_state)
  11102. return 0;
  11103. /* w/a possibly needed, check how many crtc's are already enabled. */
  11104. for_each_intel_crtc(state->dev, intel_crtc) {
  11105. struct intel_crtc_state *pipe_config;
  11106. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11107. if (IS_ERR(pipe_config))
  11108. return PTR_ERR(pipe_config);
  11109. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11110. if (!pipe_config->base.active ||
  11111. needs_modeset(&pipe_config->base))
  11112. continue;
  11113. /* 2 or more enabled crtcs means no need for w/a */
  11114. if (enabled_pipe != INVALID_PIPE)
  11115. return 0;
  11116. enabled_pipe = intel_crtc->pipe;
  11117. }
  11118. if (enabled_pipe != INVALID_PIPE)
  11119. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11120. else if (other_crtc_state)
  11121. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11122. return 0;
  11123. }
  11124. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11125. {
  11126. struct drm_crtc *crtc;
  11127. struct drm_crtc_state *crtc_state;
  11128. int ret = 0;
  11129. /* add all active pipes to the state */
  11130. for_each_crtc(state->dev, crtc) {
  11131. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11132. if (IS_ERR(crtc_state))
  11133. return PTR_ERR(crtc_state);
  11134. if (!crtc_state->active || needs_modeset(crtc_state))
  11135. continue;
  11136. crtc_state->mode_changed = true;
  11137. ret = drm_atomic_add_affected_connectors(state, crtc);
  11138. if (ret)
  11139. break;
  11140. ret = drm_atomic_add_affected_planes(state, crtc);
  11141. if (ret)
  11142. break;
  11143. }
  11144. return ret;
  11145. }
  11146. static int intel_modeset_checks(struct drm_atomic_state *state)
  11147. {
  11148. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11149. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11150. struct drm_crtc *crtc;
  11151. struct drm_crtc_state *crtc_state;
  11152. int ret = 0, i;
  11153. if (!check_digital_port_conflicts(state)) {
  11154. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11155. return -EINVAL;
  11156. }
  11157. intel_state->modeset = true;
  11158. intel_state->active_crtcs = dev_priv->active_crtcs;
  11159. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11160. if (crtc_state->active)
  11161. intel_state->active_crtcs |= 1 << i;
  11162. else
  11163. intel_state->active_crtcs &= ~(1 << i);
  11164. if (crtc_state->active != crtc->state->active)
  11165. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11166. }
  11167. /*
  11168. * See if the config requires any additional preparation, e.g.
  11169. * to adjust global state with pipes off. We need to do this
  11170. * here so we can get the modeset_pipe updated config for the new
  11171. * mode set on this crtc. For other crtcs we need to use the
  11172. * adjusted_mode bits in the crtc directly.
  11173. */
  11174. if (dev_priv->display.modeset_calc_cdclk) {
  11175. if (!intel_state->cdclk_pll_vco)
  11176. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11177. if (!intel_state->cdclk_pll_vco)
  11178. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11179. ret = dev_priv->display.modeset_calc_cdclk(state);
  11180. if (ret < 0)
  11181. return ret;
  11182. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11183. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11184. ret = intel_modeset_all_pipes(state);
  11185. if (ret < 0)
  11186. return ret;
  11187. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11188. intel_state->cdclk, intel_state->dev_cdclk);
  11189. } else
  11190. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11191. intel_modeset_clear_plls(state);
  11192. if (IS_HASWELL(dev_priv))
  11193. return haswell_mode_set_planes_workaround(state);
  11194. return 0;
  11195. }
  11196. /*
  11197. * Handle calculation of various watermark data at the end of the atomic check
  11198. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11199. * handlers to ensure that all derived state has been updated.
  11200. */
  11201. static int calc_watermark_data(struct drm_atomic_state *state)
  11202. {
  11203. struct drm_device *dev = state->dev;
  11204. struct drm_i915_private *dev_priv = to_i915(dev);
  11205. /* Is there platform-specific watermark information to calculate? */
  11206. if (dev_priv->display.compute_global_watermarks)
  11207. return dev_priv->display.compute_global_watermarks(state);
  11208. return 0;
  11209. }
  11210. /**
  11211. * intel_atomic_check - validate state object
  11212. * @dev: drm device
  11213. * @state: state to validate
  11214. */
  11215. static int intel_atomic_check(struct drm_device *dev,
  11216. struct drm_atomic_state *state)
  11217. {
  11218. struct drm_i915_private *dev_priv = to_i915(dev);
  11219. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11220. struct drm_crtc *crtc;
  11221. struct drm_crtc_state *crtc_state;
  11222. int ret, i;
  11223. bool any_ms = false;
  11224. ret = drm_atomic_helper_check_modeset(dev, state);
  11225. if (ret)
  11226. return ret;
  11227. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11228. struct intel_crtc_state *pipe_config =
  11229. to_intel_crtc_state(crtc_state);
  11230. /* Catch I915_MODE_FLAG_INHERITED */
  11231. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11232. crtc_state->mode_changed = true;
  11233. if (!needs_modeset(crtc_state))
  11234. continue;
  11235. if (!crtc_state->enable) {
  11236. any_ms = true;
  11237. continue;
  11238. }
  11239. /* FIXME: For only active_changed we shouldn't need to do any
  11240. * state recomputation at all. */
  11241. ret = drm_atomic_add_affected_connectors(state, crtc);
  11242. if (ret)
  11243. return ret;
  11244. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11245. if (ret) {
  11246. intel_dump_pipe_config(to_intel_crtc(crtc),
  11247. pipe_config, "[failed]");
  11248. return ret;
  11249. }
  11250. if (i915.fastboot &&
  11251. intel_pipe_config_compare(dev,
  11252. to_intel_crtc_state(crtc->state),
  11253. pipe_config, true)) {
  11254. crtc_state->mode_changed = false;
  11255. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11256. }
  11257. if (needs_modeset(crtc_state))
  11258. any_ms = true;
  11259. ret = drm_atomic_add_affected_planes(state, crtc);
  11260. if (ret)
  11261. return ret;
  11262. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11263. needs_modeset(crtc_state) ?
  11264. "[modeset]" : "[fastset]");
  11265. }
  11266. if (any_ms) {
  11267. ret = intel_modeset_checks(state);
  11268. if (ret)
  11269. return ret;
  11270. } else
  11271. intel_state->cdclk = dev_priv->cdclk_freq;
  11272. ret = drm_atomic_helper_check_planes(dev, state);
  11273. if (ret)
  11274. return ret;
  11275. intel_fbc_choose_crtc(dev_priv, state);
  11276. return calc_watermark_data(state);
  11277. }
  11278. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11279. struct drm_atomic_state *state,
  11280. bool nonblock)
  11281. {
  11282. struct drm_i915_private *dev_priv = to_i915(dev);
  11283. struct drm_plane_state *plane_state;
  11284. struct drm_crtc_state *crtc_state;
  11285. struct drm_plane *plane;
  11286. struct drm_crtc *crtc;
  11287. int i, ret;
  11288. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11289. if (state->legacy_cursor_update)
  11290. continue;
  11291. ret = intel_crtc_wait_for_pending_flips(crtc);
  11292. if (ret)
  11293. return ret;
  11294. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11295. flush_workqueue(dev_priv->wq);
  11296. }
  11297. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11298. if (ret)
  11299. return ret;
  11300. ret = drm_atomic_helper_prepare_planes(dev, state);
  11301. mutex_unlock(&dev->struct_mutex);
  11302. if (!ret && !nonblock) {
  11303. for_each_plane_in_state(state, plane, plane_state, i) {
  11304. struct intel_plane_state *intel_plane_state =
  11305. to_intel_plane_state(plane_state);
  11306. if (!intel_plane_state->wait_req)
  11307. continue;
  11308. ret = __i915_wait_request(intel_plane_state->wait_req,
  11309. true, NULL, NULL);
  11310. if (ret) {
  11311. /* Any hang should be swallowed by the wait */
  11312. WARN_ON(ret == -EIO);
  11313. mutex_lock(&dev->struct_mutex);
  11314. drm_atomic_helper_cleanup_planes(dev, state);
  11315. mutex_unlock(&dev->struct_mutex);
  11316. break;
  11317. }
  11318. }
  11319. }
  11320. return ret;
  11321. }
  11322. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11323. {
  11324. struct drm_device *dev = crtc->base.dev;
  11325. if (!dev->max_vblank_count)
  11326. return drm_accurate_vblank_count(&crtc->base);
  11327. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11328. }
  11329. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11330. struct drm_i915_private *dev_priv,
  11331. unsigned crtc_mask)
  11332. {
  11333. unsigned last_vblank_count[I915_MAX_PIPES];
  11334. enum pipe pipe;
  11335. int ret;
  11336. if (!crtc_mask)
  11337. return;
  11338. for_each_pipe(dev_priv, pipe) {
  11339. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11340. if (!((1 << pipe) & crtc_mask))
  11341. continue;
  11342. ret = drm_crtc_vblank_get(crtc);
  11343. if (WARN_ON(ret != 0)) {
  11344. crtc_mask &= ~(1 << pipe);
  11345. continue;
  11346. }
  11347. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11348. }
  11349. for_each_pipe(dev_priv, pipe) {
  11350. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11351. long lret;
  11352. if (!((1 << pipe) & crtc_mask))
  11353. continue;
  11354. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11355. last_vblank_count[pipe] !=
  11356. drm_crtc_vblank_count(crtc),
  11357. msecs_to_jiffies(50));
  11358. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11359. drm_crtc_vblank_put(crtc);
  11360. }
  11361. }
  11362. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11363. {
  11364. /* fb updated, need to unpin old fb */
  11365. if (crtc_state->fb_changed)
  11366. return true;
  11367. /* wm changes, need vblank before final wm's */
  11368. if (crtc_state->update_wm_post)
  11369. return true;
  11370. /*
  11371. * cxsr is re-enabled after vblank.
  11372. * This is already handled by crtc_state->update_wm_post,
  11373. * but added for clarity.
  11374. */
  11375. if (crtc_state->disable_cxsr)
  11376. return true;
  11377. return false;
  11378. }
  11379. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11380. {
  11381. struct drm_device *dev = state->dev;
  11382. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11383. struct drm_i915_private *dev_priv = to_i915(dev);
  11384. struct drm_crtc_state *old_crtc_state;
  11385. struct drm_crtc *crtc;
  11386. struct intel_crtc_state *intel_cstate;
  11387. struct drm_plane *plane;
  11388. struct drm_plane_state *plane_state;
  11389. bool hw_check = intel_state->modeset;
  11390. unsigned long put_domains[I915_MAX_PIPES] = {};
  11391. unsigned crtc_vblank_mask = 0;
  11392. int i, ret;
  11393. for_each_plane_in_state(state, plane, plane_state, i) {
  11394. struct intel_plane_state *intel_plane_state =
  11395. to_intel_plane_state(plane_state);
  11396. if (!intel_plane_state->wait_req)
  11397. continue;
  11398. ret = __i915_wait_request(intel_plane_state->wait_req,
  11399. true, NULL, NULL);
  11400. /* EIO should be eaten, and we can't get interrupted in the
  11401. * worker, and blocking commits have waited already. */
  11402. WARN_ON(ret);
  11403. }
  11404. drm_atomic_helper_wait_for_dependencies(state);
  11405. if (intel_state->modeset) {
  11406. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11407. sizeof(intel_state->min_pixclk));
  11408. dev_priv->active_crtcs = intel_state->active_crtcs;
  11409. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11410. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11411. }
  11412. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11414. if (needs_modeset(crtc->state) ||
  11415. to_intel_crtc_state(crtc->state)->update_pipe) {
  11416. hw_check = true;
  11417. put_domains[to_intel_crtc(crtc)->pipe] =
  11418. modeset_get_crtc_power_domains(crtc,
  11419. to_intel_crtc_state(crtc->state));
  11420. }
  11421. if (!needs_modeset(crtc->state))
  11422. continue;
  11423. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11424. if (old_crtc_state->active) {
  11425. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  11426. dev_priv->display.crtc_disable(crtc);
  11427. intel_crtc->active = false;
  11428. intel_fbc_disable(intel_crtc);
  11429. intel_disable_shared_dpll(intel_crtc);
  11430. /*
  11431. * Underruns don't always raise
  11432. * interrupts, so check manually.
  11433. */
  11434. intel_check_cpu_fifo_underruns(dev_priv);
  11435. intel_check_pch_fifo_underruns(dev_priv);
  11436. if (!crtc->state->active)
  11437. intel_update_watermarks(crtc);
  11438. }
  11439. }
  11440. /* Only after disabling all output pipelines that will be changed can we
  11441. * update the the output configuration. */
  11442. intel_modeset_update_crtc_state(state);
  11443. if (intel_state->modeset) {
  11444. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11445. if (dev_priv->display.modeset_commit_cdclk &&
  11446. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11447. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11448. dev_priv->display.modeset_commit_cdclk(state);
  11449. intel_modeset_verify_disabled(dev);
  11450. }
  11451. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11452. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11453. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11454. bool modeset = needs_modeset(crtc->state);
  11455. struct intel_crtc_state *pipe_config =
  11456. to_intel_crtc_state(crtc->state);
  11457. if (modeset && crtc->state->active) {
  11458. update_scanline_offset(to_intel_crtc(crtc));
  11459. dev_priv->display.crtc_enable(crtc);
  11460. }
  11461. /* Complete events for now disable pipes here. */
  11462. if (modeset && !crtc->state->active && crtc->state->event) {
  11463. spin_lock_irq(&dev->event_lock);
  11464. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  11465. spin_unlock_irq(&dev->event_lock);
  11466. crtc->state->event = NULL;
  11467. }
  11468. if (!modeset)
  11469. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11470. if (crtc->state->active &&
  11471. drm_atomic_get_existing_plane_state(state, crtc->primary))
  11472. intel_fbc_enable(intel_crtc, pipe_config, to_intel_plane_state(crtc->primary->state));
  11473. if (crtc->state->active)
  11474. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11475. if (pipe_config->base.active && needs_vblank_wait(pipe_config))
  11476. crtc_vblank_mask |= 1 << i;
  11477. }
  11478. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  11479. * already, but still need the state for the delayed optimization. To
  11480. * fix this:
  11481. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  11482. * - schedule that vblank worker _before_ calling hw_done
  11483. * - at the start of commit_tail, cancel it _synchrously
  11484. * - switch over to the vblank wait helper in the core after that since
  11485. * we don't need out special handling any more.
  11486. */
  11487. if (!state->legacy_cursor_update)
  11488. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  11489. /*
  11490. * Now that the vblank has passed, we can go ahead and program the
  11491. * optimal watermarks on platforms that need two-step watermark
  11492. * programming.
  11493. *
  11494. * TODO: Move this (and other cleanup) to an async worker eventually.
  11495. */
  11496. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11497. intel_cstate = to_intel_crtc_state(crtc->state);
  11498. if (dev_priv->display.optimize_watermarks)
  11499. dev_priv->display.optimize_watermarks(intel_cstate);
  11500. }
  11501. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11502. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  11503. if (put_domains[i])
  11504. modeset_put_power_domains(dev_priv, put_domains[i]);
  11505. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  11506. }
  11507. drm_atomic_helper_commit_hw_done(state);
  11508. if (intel_state->modeset)
  11509. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11510. mutex_lock(&dev->struct_mutex);
  11511. drm_atomic_helper_cleanup_planes(dev, state);
  11512. mutex_unlock(&dev->struct_mutex);
  11513. drm_atomic_helper_commit_cleanup_done(state);
  11514. drm_atomic_state_free(state);
  11515. /* As one of the primary mmio accessors, KMS has a high likelihood
  11516. * of triggering bugs in unclaimed access. After we finish
  11517. * modesetting, see if an error has been flagged, and if so
  11518. * enable debugging for the next modeset - and hope we catch
  11519. * the culprit.
  11520. *
  11521. * XXX note that we assume display power is on at this point.
  11522. * This might hold true now but we need to add pm helper to check
  11523. * unclaimed only when the hardware is on, as atomic commits
  11524. * can happen also when the device is completely off.
  11525. */
  11526. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11527. }
  11528. static void intel_atomic_commit_work(struct work_struct *work)
  11529. {
  11530. struct drm_atomic_state *state = container_of(work,
  11531. struct drm_atomic_state,
  11532. commit_work);
  11533. intel_atomic_commit_tail(state);
  11534. }
  11535. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  11536. {
  11537. struct drm_plane_state *old_plane_state;
  11538. struct drm_plane *plane;
  11539. struct drm_i915_gem_object *obj, *old_obj;
  11540. struct intel_plane *intel_plane;
  11541. int i;
  11542. mutex_lock(&state->dev->struct_mutex);
  11543. for_each_plane_in_state(state, plane, old_plane_state, i) {
  11544. obj = intel_fb_obj(plane->state->fb);
  11545. old_obj = intel_fb_obj(old_plane_state->fb);
  11546. intel_plane = to_intel_plane(plane);
  11547. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11548. }
  11549. mutex_unlock(&state->dev->struct_mutex);
  11550. }
  11551. /**
  11552. * intel_atomic_commit - commit validated state object
  11553. * @dev: DRM device
  11554. * @state: the top-level driver state object
  11555. * @nonblock: nonblocking commit
  11556. *
  11557. * This function commits a top-level state object that has been validated
  11558. * with drm_atomic_helper_check().
  11559. *
  11560. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11561. * nonblocking commits are only safe for pure plane updates. Everything else
  11562. * should work though.
  11563. *
  11564. * RETURNS
  11565. * Zero for success or -errno.
  11566. */
  11567. static int intel_atomic_commit(struct drm_device *dev,
  11568. struct drm_atomic_state *state,
  11569. bool nonblock)
  11570. {
  11571. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11572. struct drm_i915_private *dev_priv = to_i915(dev);
  11573. int ret = 0;
  11574. if (intel_state->modeset && nonblock) {
  11575. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  11576. return -EINVAL;
  11577. }
  11578. ret = drm_atomic_helper_setup_commit(state, nonblock);
  11579. if (ret)
  11580. return ret;
  11581. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  11582. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  11583. if (ret) {
  11584. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11585. return ret;
  11586. }
  11587. drm_atomic_helper_swap_state(state, true);
  11588. dev_priv->wm.distrust_bios_wm = false;
  11589. dev_priv->wm.skl_results = intel_state->wm_results;
  11590. intel_shared_dpll_commit(state);
  11591. intel_atomic_track_fbs(state);
  11592. if (nonblock)
  11593. queue_work(system_unbound_wq, &state->commit_work);
  11594. else
  11595. intel_atomic_commit_tail(state);
  11596. return 0;
  11597. }
  11598. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11599. {
  11600. struct drm_device *dev = crtc->dev;
  11601. struct drm_atomic_state *state;
  11602. struct drm_crtc_state *crtc_state;
  11603. int ret;
  11604. state = drm_atomic_state_alloc(dev);
  11605. if (!state) {
  11606. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  11607. crtc->base.id, crtc->name);
  11608. return;
  11609. }
  11610. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11611. retry:
  11612. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11613. ret = PTR_ERR_OR_ZERO(crtc_state);
  11614. if (!ret) {
  11615. if (!crtc_state->active)
  11616. goto out;
  11617. crtc_state->mode_changed = true;
  11618. ret = drm_atomic_commit(state);
  11619. }
  11620. if (ret == -EDEADLK) {
  11621. drm_atomic_state_clear(state);
  11622. drm_modeset_backoff(state->acquire_ctx);
  11623. goto retry;
  11624. }
  11625. if (ret)
  11626. out:
  11627. drm_atomic_state_free(state);
  11628. }
  11629. #undef for_each_intel_crtc_masked
  11630. /*
  11631. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  11632. * drm_atomic_helper_legacy_gamma_set() directly.
  11633. */
  11634. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  11635. u16 *red, u16 *green, u16 *blue,
  11636. uint32_t size)
  11637. {
  11638. struct drm_device *dev = crtc->dev;
  11639. struct drm_mode_config *config = &dev->mode_config;
  11640. struct drm_crtc_state *state;
  11641. int ret;
  11642. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  11643. if (ret)
  11644. return ret;
  11645. /*
  11646. * Make sure we update the legacy properties so this works when
  11647. * atomic is not enabled.
  11648. */
  11649. state = crtc->state;
  11650. drm_object_property_set_value(&crtc->base,
  11651. config->degamma_lut_property,
  11652. (state->degamma_lut) ?
  11653. state->degamma_lut->base.id : 0);
  11654. drm_object_property_set_value(&crtc->base,
  11655. config->ctm_property,
  11656. (state->ctm) ?
  11657. state->ctm->base.id : 0);
  11658. drm_object_property_set_value(&crtc->base,
  11659. config->gamma_lut_property,
  11660. (state->gamma_lut) ?
  11661. state->gamma_lut->base.id : 0);
  11662. return 0;
  11663. }
  11664. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11665. .gamma_set = intel_atomic_legacy_gamma_set,
  11666. .set_config = drm_atomic_helper_set_config,
  11667. .set_property = drm_atomic_helper_crtc_set_property,
  11668. .destroy = intel_crtc_destroy,
  11669. .page_flip = intel_crtc_page_flip,
  11670. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11671. .atomic_destroy_state = intel_crtc_destroy_state,
  11672. };
  11673. /**
  11674. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11675. * @plane: drm plane to prepare for
  11676. * @fb: framebuffer to prepare for presentation
  11677. *
  11678. * Prepares a framebuffer for usage on a display plane. Generally this
  11679. * involves pinning the underlying object and updating the frontbuffer tracking
  11680. * bits. Some older platforms need special physical address handling for
  11681. * cursor planes.
  11682. *
  11683. * Must be called with struct_mutex held.
  11684. *
  11685. * Returns 0 on success, negative error code on failure.
  11686. */
  11687. int
  11688. intel_prepare_plane_fb(struct drm_plane *plane,
  11689. const struct drm_plane_state *new_state)
  11690. {
  11691. struct drm_device *dev = plane->dev;
  11692. struct drm_framebuffer *fb = new_state->fb;
  11693. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11694. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11695. struct reservation_object *resv;
  11696. int ret = 0;
  11697. if (!obj && !old_obj)
  11698. return 0;
  11699. if (old_obj) {
  11700. struct drm_crtc_state *crtc_state =
  11701. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11702. /* Big Hammer, we also need to ensure that any pending
  11703. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11704. * current scanout is retired before unpinning the old
  11705. * framebuffer. Note that we rely on userspace rendering
  11706. * into the buffer attached to the pipe they are waiting
  11707. * on. If not, userspace generates a GPU hang with IPEHR
  11708. * point to the MI_WAIT_FOR_EVENT.
  11709. *
  11710. * This should only fail upon a hung GPU, in which case we
  11711. * can safely continue.
  11712. */
  11713. if (needs_modeset(crtc_state))
  11714. ret = i915_gem_object_wait_rendering(old_obj, true);
  11715. if (ret) {
  11716. /* GPU hangs should have been swallowed by the wait */
  11717. WARN_ON(ret == -EIO);
  11718. return ret;
  11719. }
  11720. }
  11721. if (!obj)
  11722. return 0;
  11723. /* For framebuffer backed by dmabuf, wait for fence */
  11724. resv = i915_gem_object_get_dmabuf_resv(obj);
  11725. if (resv) {
  11726. long lret;
  11727. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  11728. MAX_SCHEDULE_TIMEOUT);
  11729. if (lret == -ERESTARTSYS)
  11730. return lret;
  11731. WARN(lret < 0, "waiting returns %li\n", lret);
  11732. }
  11733. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11734. INTEL_INFO(dev)->cursor_needs_physical) {
  11735. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11736. ret = i915_gem_object_attach_phys(obj, align);
  11737. if (ret)
  11738. DRM_DEBUG_KMS("failed to attach phys object\n");
  11739. } else {
  11740. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11741. }
  11742. if (ret == 0) {
  11743. struct intel_plane_state *plane_state =
  11744. to_intel_plane_state(new_state);
  11745. i915_gem_request_assign(&plane_state->wait_req,
  11746. obj->last_write_req);
  11747. }
  11748. return ret;
  11749. }
  11750. /**
  11751. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11752. * @plane: drm plane to clean up for
  11753. * @fb: old framebuffer that was on plane
  11754. *
  11755. * Cleans up a framebuffer that has just been removed from a plane.
  11756. *
  11757. * Must be called with struct_mutex held.
  11758. */
  11759. void
  11760. intel_cleanup_plane_fb(struct drm_plane *plane,
  11761. const struct drm_plane_state *old_state)
  11762. {
  11763. struct drm_device *dev = plane->dev;
  11764. struct intel_plane_state *old_intel_state;
  11765. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11766. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11767. old_intel_state = to_intel_plane_state(old_state);
  11768. if (!obj && !old_obj)
  11769. return;
  11770. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11771. !INTEL_INFO(dev)->cursor_needs_physical))
  11772. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11773. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11774. }
  11775. int
  11776. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11777. {
  11778. int max_scale;
  11779. int crtc_clock, cdclk;
  11780. if (!intel_crtc || !crtc_state->base.enable)
  11781. return DRM_PLANE_HELPER_NO_SCALING;
  11782. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11783. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11784. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11785. return DRM_PLANE_HELPER_NO_SCALING;
  11786. /*
  11787. * skl max scale is lower of:
  11788. * close to 3 but not 3, -1 is for that purpose
  11789. * or
  11790. * cdclk/crtc_clock
  11791. */
  11792. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11793. return max_scale;
  11794. }
  11795. static int
  11796. intel_check_primary_plane(struct drm_plane *plane,
  11797. struct intel_crtc_state *crtc_state,
  11798. struct intel_plane_state *state)
  11799. {
  11800. struct drm_crtc *crtc = state->base.crtc;
  11801. struct drm_framebuffer *fb = state->base.fb;
  11802. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11803. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11804. bool can_position = false;
  11805. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11806. /* use scaler when colorkey is not required */
  11807. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11808. min_scale = 1;
  11809. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11810. }
  11811. can_position = true;
  11812. }
  11813. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11814. &state->dst, &state->clip,
  11815. state->base.rotation,
  11816. min_scale, max_scale,
  11817. can_position, true,
  11818. &state->visible);
  11819. }
  11820. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11821. struct drm_crtc_state *old_crtc_state)
  11822. {
  11823. struct drm_device *dev = crtc->dev;
  11824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11825. struct intel_crtc_state *old_intel_state =
  11826. to_intel_crtc_state(old_crtc_state);
  11827. bool modeset = needs_modeset(crtc->state);
  11828. /* Perform vblank evasion around commit operation */
  11829. intel_pipe_update_start(intel_crtc);
  11830. if (modeset)
  11831. return;
  11832. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11833. intel_color_set_csc(crtc->state);
  11834. intel_color_load_luts(crtc->state);
  11835. }
  11836. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11837. intel_update_pipe_config(intel_crtc, old_intel_state);
  11838. else if (INTEL_INFO(dev)->gen >= 9)
  11839. skl_detach_scalers(intel_crtc);
  11840. }
  11841. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11842. struct drm_crtc_state *old_crtc_state)
  11843. {
  11844. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11845. intel_pipe_update_end(intel_crtc, NULL);
  11846. }
  11847. /**
  11848. * intel_plane_destroy - destroy a plane
  11849. * @plane: plane to destroy
  11850. *
  11851. * Common destruction function for all types of planes (primary, cursor,
  11852. * sprite).
  11853. */
  11854. void intel_plane_destroy(struct drm_plane *plane)
  11855. {
  11856. if (!plane)
  11857. return;
  11858. drm_plane_cleanup(plane);
  11859. kfree(to_intel_plane(plane));
  11860. }
  11861. const struct drm_plane_funcs intel_plane_funcs = {
  11862. .update_plane = drm_atomic_helper_update_plane,
  11863. .disable_plane = drm_atomic_helper_disable_plane,
  11864. .destroy = intel_plane_destroy,
  11865. .set_property = drm_atomic_helper_plane_set_property,
  11866. .atomic_get_property = intel_plane_atomic_get_property,
  11867. .atomic_set_property = intel_plane_atomic_set_property,
  11868. .atomic_duplicate_state = intel_plane_duplicate_state,
  11869. .atomic_destroy_state = intel_plane_destroy_state,
  11870. };
  11871. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11872. int pipe)
  11873. {
  11874. struct intel_plane *primary = NULL;
  11875. struct intel_plane_state *state = NULL;
  11876. const uint32_t *intel_primary_formats;
  11877. unsigned int num_formats;
  11878. int ret;
  11879. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11880. if (!primary)
  11881. goto fail;
  11882. state = intel_create_plane_state(&primary->base);
  11883. if (!state)
  11884. goto fail;
  11885. primary->base.state = &state->base;
  11886. primary->can_scale = false;
  11887. primary->max_downscale = 1;
  11888. if (INTEL_INFO(dev)->gen >= 9) {
  11889. primary->can_scale = true;
  11890. state->scaler_id = -1;
  11891. }
  11892. primary->pipe = pipe;
  11893. primary->plane = pipe;
  11894. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11895. primary->check_plane = intel_check_primary_plane;
  11896. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11897. primary->plane = !pipe;
  11898. if (INTEL_INFO(dev)->gen >= 9) {
  11899. intel_primary_formats = skl_primary_formats;
  11900. num_formats = ARRAY_SIZE(skl_primary_formats);
  11901. primary->update_plane = skylake_update_primary_plane;
  11902. primary->disable_plane = skylake_disable_primary_plane;
  11903. } else if (HAS_PCH_SPLIT(dev)) {
  11904. intel_primary_formats = i965_primary_formats;
  11905. num_formats = ARRAY_SIZE(i965_primary_formats);
  11906. primary->update_plane = ironlake_update_primary_plane;
  11907. primary->disable_plane = i9xx_disable_primary_plane;
  11908. } else if (INTEL_INFO(dev)->gen >= 4) {
  11909. intel_primary_formats = i965_primary_formats;
  11910. num_formats = ARRAY_SIZE(i965_primary_formats);
  11911. primary->update_plane = i9xx_update_primary_plane;
  11912. primary->disable_plane = i9xx_disable_primary_plane;
  11913. } else {
  11914. intel_primary_formats = i8xx_primary_formats;
  11915. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11916. primary->update_plane = i9xx_update_primary_plane;
  11917. primary->disable_plane = i9xx_disable_primary_plane;
  11918. }
  11919. if (INTEL_INFO(dev)->gen >= 9)
  11920. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11921. &intel_plane_funcs,
  11922. intel_primary_formats, num_formats,
  11923. DRM_PLANE_TYPE_PRIMARY,
  11924. "plane 1%c", pipe_name(pipe));
  11925. else if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  11926. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11927. &intel_plane_funcs,
  11928. intel_primary_formats, num_formats,
  11929. DRM_PLANE_TYPE_PRIMARY,
  11930. "primary %c", pipe_name(pipe));
  11931. else
  11932. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11933. &intel_plane_funcs,
  11934. intel_primary_formats, num_formats,
  11935. DRM_PLANE_TYPE_PRIMARY,
  11936. "plane %c", plane_name(primary->plane));
  11937. if (ret)
  11938. goto fail;
  11939. if (INTEL_INFO(dev)->gen >= 4)
  11940. intel_create_rotation_property(dev, primary);
  11941. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11942. return &primary->base;
  11943. fail:
  11944. kfree(state);
  11945. kfree(primary);
  11946. return NULL;
  11947. }
  11948. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11949. {
  11950. if (!dev->mode_config.rotation_property) {
  11951. unsigned long flags = BIT(DRM_ROTATE_0) |
  11952. BIT(DRM_ROTATE_180);
  11953. if (INTEL_INFO(dev)->gen >= 9)
  11954. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11955. dev->mode_config.rotation_property =
  11956. drm_mode_create_rotation_property(dev, flags);
  11957. }
  11958. if (dev->mode_config.rotation_property)
  11959. drm_object_attach_property(&plane->base.base,
  11960. dev->mode_config.rotation_property,
  11961. plane->base.state->rotation);
  11962. }
  11963. static int
  11964. intel_check_cursor_plane(struct drm_plane *plane,
  11965. struct intel_crtc_state *crtc_state,
  11966. struct intel_plane_state *state)
  11967. {
  11968. struct drm_crtc *crtc = crtc_state->base.crtc;
  11969. struct drm_framebuffer *fb = state->base.fb;
  11970. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11971. enum pipe pipe = to_intel_plane(plane)->pipe;
  11972. unsigned stride;
  11973. int ret;
  11974. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11975. &state->dst, &state->clip,
  11976. state->base.rotation,
  11977. DRM_PLANE_HELPER_NO_SCALING,
  11978. DRM_PLANE_HELPER_NO_SCALING,
  11979. true, true, &state->visible);
  11980. if (ret)
  11981. return ret;
  11982. /* if we want to turn off the cursor ignore width and height */
  11983. if (!obj)
  11984. return 0;
  11985. /* Check for which cursor types we support */
  11986. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11987. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11988. state->base.crtc_w, state->base.crtc_h);
  11989. return -EINVAL;
  11990. }
  11991. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11992. if (obj->base.size < stride * state->base.crtc_h) {
  11993. DRM_DEBUG_KMS("buffer is too small\n");
  11994. return -ENOMEM;
  11995. }
  11996. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11997. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11998. return -EINVAL;
  11999. }
  12000. /*
  12001. * There's something wrong with the cursor on CHV pipe C.
  12002. * If it straddles the left edge of the screen then
  12003. * moving it away from the edge or disabling it often
  12004. * results in a pipe underrun, and often that can lead to
  12005. * dead pipe (constant underrun reported, and it scans
  12006. * out just a solid color). To recover from that, the
  12007. * display power well must be turned off and on again.
  12008. * Refuse the put the cursor into that compromised position.
  12009. */
  12010. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  12011. state->visible && state->base.crtc_x < 0) {
  12012. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12013. return -EINVAL;
  12014. }
  12015. return 0;
  12016. }
  12017. static void
  12018. intel_disable_cursor_plane(struct drm_plane *plane,
  12019. struct drm_crtc *crtc)
  12020. {
  12021. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12022. intel_crtc->cursor_addr = 0;
  12023. intel_crtc_update_cursor(crtc, NULL);
  12024. }
  12025. static void
  12026. intel_update_cursor_plane(struct drm_plane *plane,
  12027. const struct intel_crtc_state *crtc_state,
  12028. const struct intel_plane_state *state)
  12029. {
  12030. struct drm_crtc *crtc = crtc_state->base.crtc;
  12031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12032. struct drm_device *dev = plane->dev;
  12033. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12034. uint32_t addr;
  12035. if (!obj)
  12036. addr = 0;
  12037. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12038. addr = i915_gem_obj_ggtt_offset(obj);
  12039. else
  12040. addr = obj->phys_handle->busaddr;
  12041. intel_crtc->cursor_addr = addr;
  12042. intel_crtc_update_cursor(crtc, state);
  12043. }
  12044. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12045. int pipe)
  12046. {
  12047. struct intel_plane *cursor = NULL;
  12048. struct intel_plane_state *state = NULL;
  12049. int ret;
  12050. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12051. if (!cursor)
  12052. goto fail;
  12053. state = intel_create_plane_state(&cursor->base);
  12054. if (!state)
  12055. goto fail;
  12056. cursor->base.state = &state->base;
  12057. cursor->can_scale = false;
  12058. cursor->max_downscale = 1;
  12059. cursor->pipe = pipe;
  12060. cursor->plane = pipe;
  12061. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12062. cursor->check_plane = intel_check_cursor_plane;
  12063. cursor->update_plane = intel_update_cursor_plane;
  12064. cursor->disable_plane = intel_disable_cursor_plane;
  12065. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12066. &intel_plane_funcs,
  12067. intel_cursor_formats,
  12068. ARRAY_SIZE(intel_cursor_formats),
  12069. DRM_PLANE_TYPE_CURSOR,
  12070. "cursor %c", pipe_name(pipe));
  12071. if (ret)
  12072. goto fail;
  12073. if (INTEL_INFO(dev)->gen >= 4) {
  12074. if (!dev->mode_config.rotation_property)
  12075. dev->mode_config.rotation_property =
  12076. drm_mode_create_rotation_property(dev,
  12077. BIT(DRM_ROTATE_0) |
  12078. BIT(DRM_ROTATE_180));
  12079. if (dev->mode_config.rotation_property)
  12080. drm_object_attach_property(&cursor->base.base,
  12081. dev->mode_config.rotation_property,
  12082. state->base.rotation);
  12083. }
  12084. if (INTEL_INFO(dev)->gen >=9)
  12085. state->scaler_id = -1;
  12086. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12087. return &cursor->base;
  12088. fail:
  12089. kfree(state);
  12090. kfree(cursor);
  12091. return NULL;
  12092. }
  12093. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12094. struct intel_crtc_state *crtc_state)
  12095. {
  12096. int i;
  12097. struct intel_scaler *intel_scaler;
  12098. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12099. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12100. intel_scaler = &scaler_state->scalers[i];
  12101. intel_scaler->in_use = 0;
  12102. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12103. }
  12104. scaler_state->scaler_id = -1;
  12105. }
  12106. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12107. {
  12108. struct drm_i915_private *dev_priv = to_i915(dev);
  12109. struct intel_crtc *intel_crtc;
  12110. struct intel_crtc_state *crtc_state = NULL;
  12111. struct drm_plane *primary = NULL;
  12112. struct drm_plane *cursor = NULL;
  12113. int ret;
  12114. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12115. if (intel_crtc == NULL)
  12116. return;
  12117. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12118. if (!crtc_state)
  12119. goto fail;
  12120. intel_crtc->config = crtc_state;
  12121. intel_crtc->base.state = &crtc_state->base;
  12122. crtc_state->base.crtc = &intel_crtc->base;
  12123. /* initialize shared scalers */
  12124. if (INTEL_INFO(dev)->gen >= 9) {
  12125. if (pipe == PIPE_C)
  12126. intel_crtc->num_scalers = 1;
  12127. else
  12128. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12129. skl_init_scalers(dev, intel_crtc, crtc_state);
  12130. }
  12131. primary = intel_primary_plane_create(dev, pipe);
  12132. if (!primary)
  12133. goto fail;
  12134. cursor = intel_cursor_plane_create(dev, pipe);
  12135. if (!cursor)
  12136. goto fail;
  12137. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12138. cursor, &intel_crtc_funcs,
  12139. "pipe %c", pipe_name(pipe));
  12140. if (ret)
  12141. goto fail;
  12142. /*
  12143. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12144. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12145. */
  12146. intel_crtc->pipe = pipe;
  12147. intel_crtc->plane = pipe;
  12148. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12149. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12150. intel_crtc->plane = !pipe;
  12151. }
  12152. intel_crtc->cursor_base = ~0;
  12153. intel_crtc->cursor_cntl = ~0;
  12154. intel_crtc->cursor_size = ~0;
  12155. intel_crtc->wm.cxsr_allowed = true;
  12156. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12157. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12158. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12159. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12160. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12161. intel_color_init(&intel_crtc->base);
  12162. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12163. return;
  12164. fail:
  12165. intel_plane_destroy(primary);
  12166. intel_plane_destroy(cursor);
  12167. kfree(crtc_state);
  12168. kfree(intel_crtc);
  12169. }
  12170. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12171. {
  12172. struct drm_encoder *encoder = connector->base.encoder;
  12173. struct drm_device *dev = connector->base.dev;
  12174. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12175. if (!encoder || WARN_ON(!encoder->crtc))
  12176. return INVALID_PIPE;
  12177. return to_intel_crtc(encoder->crtc)->pipe;
  12178. }
  12179. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12180. struct drm_file *file)
  12181. {
  12182. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12183. struct drm_crtc *drmmode_crtc;
  12184. struct intel_crtc *crtc;
  12185. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12186. if (!drmmode_crtc)
  12187. return -ENOENT;
  12188. crtc = to_intel_crtc(drmmode_crtc);
  12189. pipe_from_crtc_id->pipe = crtc->pipe;
  12190. return 0;
  12191. }
  12192. static int intel_encoder_clones(struct intel_encoder *encoder)
  12193. {
  12194. struct drm_device *dev = encoder->base.dev;
  12195. struct intel_encoder *source_encoder;
  12196. int index_mask = 0;
  12197. int entry = 0;
  12198. for_each_intel_encoder(dev, source_encoder) {
  12199. if (encoders_cloneable(encoder, source_encoder))
  12200. index_mask |= (1 << entry);
  12201. entry++;
  12202. }
  12203. return index_mask;
  12204. }
  12205. static bool has_edp_a(struct drm_device *dev)
  12206. {
  12207. struct drm_i915_private *dev_priv = to_i915(dev);
  12208. if (!IS_MOBILE(dev))
  12209. return false;
  12210. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12211. return false;
  12212. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12213. return false;
  12214. return true;
  12215. }
  12216. static bool intel_crt_present(struct drm_device *dev)
  12217. {
  12218. struct drm_i915_private *dev_priv = to_i915(dev);
  12219. if (INTEL_INFO(dev)->gen >= 9)
  12220. return false;
  12221. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12222. return false;
  12223. if (IS_CHERRYVIEW(dev))
  12224. return false;
  12225. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12226. return false;
  12227. /* DDI E can't be used if DDI A requires 4 lanes */
  12228. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12229. return false;
  12230. if (!dev_priv->vbt.int_crt_support)
  12231. return false;
  12232. return true;
  12233. }
  12234. static void intel_setup_outputs(struct drm_device *dev)
  12235. {
  12236. struct drm_i915_private *dev_priv = to_i915(dev);
  12237. struct intel_encoder *encoder;
  12238. bool dpd_is_edp = false;
  12239. /*
  12240. * intel_edp_init_connector() depends on this completing first, to
  12241. * prevent the registeration of both eDP and LVDS and the incorrect
  12242. * sharing of the PPS.
  12243. */
  12244. intel_lvds_init(dev);
  12245. if (intel_crt_present(dev))
  12246. intel_crt_init(dev);
  12247. if (IS_BROXTON(dev)) {
  12248. /*
  12249. * FIXME: Broxton doesn't support port detection via the
  12250. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12251. * detect the ports.
  12252. */
  12253. intel_ddi_init(dev, PORT_A);
  12254. intel_ddi_init(dev, PORT_B);
  12255. intel_ddi_init(dev, PORT_C);
  12256. intel_dsi_init(dev);
  12257. } else if (HAS_DDI(dev)) {
  12258. int found;
  12259. /*
  12260. * Haswell uses DDI functions to detect digital outputs.
  12261. * On SKL pre-D0 the strap isn't connected, so we assume
  12262. * it's there.
  12263. */
  12264. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12265. /* WaIgnoreDDIAStrap: skl */
  12266. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12267. intel_ddi_init(dev, PORT_A);
  12268. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12269. * register */
  12270. found = I915_READ(SFUSE_STRAP);
  12271. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12272. intel_ddi_init(dev, PORT_B);
  12273. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12274. intel_ddi_init(dev, PORT_C);
  12275. if (found & SFUSE_STRAP_DDID_DETECTED)
  12276. intel_ddi_init(dev, PORT_D);
  12277. /*
  12278. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12279. */
  12280. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12281. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12282. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12283. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12284. intel_ddi_init(dev, PORT_E);
  12285. } else if (HAS_PCH_SPLIT(dev)) {
  12286. int found;
  12287. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12288. if (has_edp_a(dev))
  12289. intel_dp_init(dev, DP_A, PORT_A);
  12290. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12291. /* PCH SDVOB multiplex with HDMIB */
  12292. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12293. if (!found)
  12294. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12295. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12296. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12297. }
  12298. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12299. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12300. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12301. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12302. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12303. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12304. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12305. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12306. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12307. bool has_edp, has_port;
  12308. /*
  12309. * The DP_DETECTED bit is the latched state of the DDC
  12310. * SDA pin at boot. However since eDP doesn't require DDC
  12311. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12312. * eDP ports may have been muxed to an alternate function.
  12313. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12314. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12315. * detect eDP ports.
  12316. *
  12317. * Sadly the straps seem to be missing sometimes even for HDMI
  12318. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  12319. * and VBT for the presence of the port. Additionally we can't
  12320. * trust the port type the VBT declares as we've seen at least
  12321. * HDMI ports that the VBT claim are DP or eDP.
  12322. */
  12323. has_edp = intel_dp_is_edp(dev, PORT_B);
  12324. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  12325. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  12326. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  12327. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  12328. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12329. has_edp = intel_dp_is_edp(dev, PORT_C);
  12330. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  12331. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  12332. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  12333. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  12334. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12335. if (IS_CHERRYVIEW(dev)) {
  12336. /*
  12337. * eDP not supported on port D,
  12338. * so no need to worry about it
  12339. */
  12340. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  12341. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  12342. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12343. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  12344. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12345. }
  12346. intel_dsi_init(dev);
  12347. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12348. bool found = false;
  12349. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12350. DRM_DEBUG_KMS("probing SDVOB\n");
  12351. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12352. if (!found && IS_G4X(dev)) {
  12353. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12354. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12355. }
  12356. if (!found && IS_G4X(dev))
  12357. intel_dp_init(dev, DP_B, PORT_B);
  12358. }
  12359. /* Before G4X SDVOC doesn't have its own detect register */
  12360. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12361. DRM_DEBUG_KMS("probing SDVOC\n");
  12362. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12363. }
  12364. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12365. if (IS_G4X(dev)) {
  12366. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12367. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12368. }
  12369. if (IS_G4X(dev))
  12370. intel_dp_init(dev, DP_C, PORT_C);
  12371. }
  12372. if (IS_G4X(dev) &&
  12373. (I915_READ(DP_D) & DP_DETECTED))
  12374. intel_dp_init(dev, DP_D, PORT_D);
  12375. } else if (IS_GEN2(dev))
  12376. intel_dvo_init(dev);
  12377. if (SUPPORTS_TV(dev))
  12378. intel_tv_init(dev);
  12379. intel_psr_init(dev);
  12380. for_each_intel_encoder(dev, encoder) {
  12381. encoder->base.possible_crtcs = encoder->crtc_mask;
  12382. encoder->base.possible_clones =
  12383. intel_encoder_clones(encoder);
  12384. }
  12385. intel_init_pch_refclk(dev);
  12386. drm_helper_move_panel_connectors_to_head(dev);
  12387. }
  12388. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12389. {
  12390. struct drm_device *dev = fb->dev;
  12391. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12392. drm_framebuffer_cleanup(fb);
  12393. mutex_lock(&dev->struct_mutex);
  12394. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12395. drm_gem_object_unreference(&intel_fb->obj->base);
  12396. mutex_unlock(&dev->struct_mutex);
  12397. kfree(intel_fb);
  12398. }
  12399. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12400. struct drm_file *file,
  12401. unsigned int *handle)
  12402. {
  12403. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12404. struct drm_i915_gem_object *obj = intel_fb->obj;
  12405. if (obj->userptr.mm) {
  12406. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12407. return -EINVAL;
  12408. }
  12409. return drm_gem_handle_create(file, &obj->base, handle);
  12410. }
  12411. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12412. struct drm_file *file,
  12413. unsigned flags, unsigned color,
  12414. struct drm_clip_rect *clips,
  12415. unsigned num_clips)
  12416. {
  12417. struct drm_device *dev = fb->dev;
  12418. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12419. struct drm_i915_gem_object *obj = intel_fb->obj;
  12420. mutex_lock(&dev->struct_mutex);
  12421. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12422. mutex_unlock(&dev->struct_mutex);
  12423. return 0;
  12424. }
  12425. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12426. .destroy = intel_user_framebuffer_destroy,
  12427. .create_handle = intel_user_framebuffer_create_handle,
  12428. .dirty = intel_user_framebuffer_dirty,
  12429. };
  12430. static
  12431. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12432. uint32_t pixel_format)
  12433. {
  12434. u32 gen = INTEL_INFO(dev)->gen;
  12435. if (gen >= 9) {
  12436. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12437. /* "The stride in bytes must not exceed the of the size of 8K
  12438. * pixels and 32K bytes."
  12439. */
  12440. return min(8192 * cpp, 32768);
  12441. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12442. return 32*1024;
  12443. } else if (gen >= 4) {
  12444. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12445. return 16*1024;
  12446. else
  12447. return 32*1024;
  12448. } else if (gen >= 3) {
  12449. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12450. return 8*1024;
  12451. else
  12452. return 16*1024;
  12453. } else {
  12454. /* XXX DSPC is limited to 4k tiled */
  12455. return 8*1024;
  12456. }
  12457. }
  12458. static int intel_framebuffer_init(struct drm_device *dev,
  12459. struct intel_framebuffer *intel_fb,
  12460. struct drm_mode_fb_cmd2 *mode_cmd,
  12461. struct drm_i915_gem_object *obj)
  12462. {
  12463. struct drm_i915_private *dev_priv = to_i915(dev);
  12464. unsigned int aligned_height;
  12465. int ret;
  12466. u32 pitch_limit, stride_alignment;
  12467. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12468. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12469. /* Enforce that fb modifier and tiling mode match, but only for
  12470. * X-tiled. This is needed for FBC. */
  12471. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12472. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12473. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12474. return -EINVAL;
  12475. }
  12476. } else {
  12477. if (obj->tiling_mode == I915_TILING_X)
  12478. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12479. else if (obj->tiling_mode == I915_TILING_Y) {
  12480. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12481. return -EINVAL;
  12482. }
  12483. }
  12484. /* Passed in modifier sanity checking. */
  12485. switch (mode_cmd->modifier[0]) {
  12486. case I915_FORMAT_MOD_Y_TILED:
  12487. case I915_FORMAT_MOD_Yf_TILED:
  12488. if (INTEL_INFO(dev)->gen < 9) {
  12489. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12490. mode_cmd->modifier[0]);
  12491. return -EINVAL;
  12492. }
  12493. case DRM_FORMAT_MOD_NONE:
  12494. case I915_FORMAT_MOD_X_TILED:
  12495. break;
  12496. default:
  12497. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12498. mode_cmd->modifier[0]);
  12499. return -EINVAL;
  12500. }
  12501. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12502. mode_cmd->modifier[0],
  12503. mode_cmd->pixel_format);
  12504. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12505. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12506. mode_cmd->pitches[0], stride_alignment);
  12507. return -EINVAL;
  12508. }
  12509. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12510. mode_cmd->pixel_format);
  12511. if (mode_cmd->pitches[0] > pitch_limit) {
  12512. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12513. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12514. "tiled" : "linear",
  12515. mode_cmd->pitches[0], pitch_limit);
  12516. return -EINVAL;
  12517. }
  12518. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12519. mode_cmd->pitches[0] != obj->stride) {
  12520. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12521. mode_cmd->pitches[0], obj->stride);
  12522. return -EINVAL;
  12523. }
  12524. /* Reject formats not supported by any plane early. */
  12525. switch (mode_cmd->pixel_format) {
  12526. case DRM_FORMAT_C8:
  12527. case DRM_FORMAT_RGB565:
  12528. case DRM_FORMAT_XRGB8888:
  12529. case DRM_FORMAT_ARGB8888:
  12530. break;
  12531. case DRM_FORMAT_XRGB1555:
  12532. if (INTEL_INFO(dev)->gen > 3) {
  12533. DRM_DEBUG("unsupported pixel format: %s\n",
  12534. drm_get_format_name(mode_cmd->pixel_format));
  12535. return -EINVAL;
  12536. }
  12537. break;
  12538. case DRM_FORMAT_ABGR8888:
  12539. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12540. INTEL_INFO(dev)->gen < 9) {
  12541. DRM_DEBUG("unsupported pixel format: %s\n",
  12542. drm_get_format_name(mode_cmd->pixel_format));
  12543. return -EINVAL;
  12544. }
  12545. break;
  12546. case DRM_FORMAT_XBGR8888:
  12547. case DRM_FORMAT_XRGB2101010:
  12548. case DRM_FORMAT_XBGR2101010:
  12549. if (INTEL_INFO(dev)->gen < 4) {
  12550. DRM_DEBUG("unsupported pixel format: %s\n",
  12551. drm_get_format_name(mode_cmd->pixel_format));
  12552. return -EINVAL;
  12553. }
  12554. break;
  12555. case DRM_FORMAT_ABGR2101010:
  12556. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12557. DRM_DEBUG("unsupported pixel format: %s\n",
  12558. drm_get_format_name(mode_cmd->pixel_format));
  12559. return -EINVAL;
  12560. }
  12561. break;
  12562. case DRM_FORMAT_YUYV:
  12563. case DRM_FORMAT_UYVY:
  12564. case DRM_FORMAT_YVYU:
  12565. case DRM_FORMAT_VYUY:
  12566. if (INTEL_INFO(dev)->gen < 5) {
  12567. DRM_DEBUG("unsupported pixel format: %s\n",
  12568. drm_get_format_name(mode_cmd->pixel_format));
  12569. return -EINVAL;
  12570. }
  12571. break;
  12572. default:
  12573. DRM_DEBUG("unsupported pixel format: %s\n",
  12574. drm_get_format_name(mode_cmd->pixel_format));
  12575. return -EINVAL;
  12576. }
  12577. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12578. if (mode_cmd->offsets[0] != 0)
  12579. return -EINVAL;
  12580. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12581. mode_cmd->pixel_format,
  12582. mode_cmd->modifier[0]);
  12583. /* FIXME drm helper for size checks (especially planar formats)? */
  12584. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12585. return -EINVAL;
  12586. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12587. intel_fb->obj = obj;
  12588. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12589. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12590. if (ret) {
  12591. DRM_ERROR("framebuffer init failed %d\n", ret);
  12592. return ret;
  12593. }
  12594. intel_fb->obj->framebuffer_references++;
  12595. return 0;
  12596. }
  12597. static struct drm_framebuffer *
  12598. intel_user_framebuffer_create(struct drm_device *dev,
  12599. struct drm_file *filp,
  12600. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12601. {
  12602. struct drm_framebuffer *fb;
  12603. struct drm_i915_gem_object *obj;
  12604. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12605. obj = to_intel_bo(drm_gem_object_lookup(filp, mode_cmd.handles[0]));
  12606. if (&obj->base == NULL)
  12607. return ERR_PTR(-ENOENT);
  12608. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12609. if (IS_ERR(fb))
  12610. drm_gem_object_unreference_unlocked(&obj->base);
  12611. return fb;
  12612. }
  12613. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12614. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12615. {
  12616. }
  12617. #endif
  12618. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12619. .fb_create = intel_user_framebuffer_create,
  12620. .output_poll_changed = intel_fbdev_output_poll_changed,
  12621. .atomic_check = intel_atomic_check,
  12622. .atomic_commit = intel_atomic_commit,
  12623. .atomic_state_alloc = intel_atomic_state_alloc,
  12624. .atomic_state_clear = intel_atomic_state_clear,
  12625. };
  12626. /**
  12627. * intel_init_display_hooks - initialize the display modesetting hooks
  12628. * @dev_priv: device private
  12629. */
  12630. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12631. {
  12632. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12633. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12634. dev_priv->display.get_initial_plane_config =
  12635. skylake_get_initial_plane_config;
  12636. dev_priv->display.crtc_compute_clock =
  12637. haswell_crtc_compute_clock;
  12638. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12639. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12640. } else if (HAS_DDI(dev_priv)) {
  12641. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12642. dev_priv->display.get_initial_plane_config =
  12643. ironlake_get_initial_plane_config;
  12644. dev_priv->display.crtc_compute_clock =
  12645. haswell_crtc_compute_clock;
  12646. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12647. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12648. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12649. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12650. dev_priv->display.get_initial_plane_config =
  12651. ironlake_get_initial_plane_config;
  12652. dev_priv->display.crtc_compute_clock =
  12653. ironlake_crtc_compute_clock;
  12654. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12655. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12656. } else if (IS_CHERRYVIEW(dev_priv)) {
  12657. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12658. dev_priv->display.get_initial_plane_config =
  12659. i9xx_get_initial_plane_config;
  12660. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12661. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12662. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12663. } else if (IS_VALLEYVIEW(dev_priv)) {
  12664. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12665. dev_priv->display.get_initial_plane_config =
  12666. i9xx_get_initial_plane_config;
  12667. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12668. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12669. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12670. } else if (IS_G4X(dev_priv)) {
  12671. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12672. dev_priv->display.get_initial_plane_config =
  12673. i9xx_get_initial_plane_config;
  12674. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12675. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12676. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12677. } else if (IS_PINEVIEW(dev_priv)) {
  12678. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12679. dev_priv->display.get_initial_plane_config =
  12680. i9xx_get_initial_plane_config;
  12681. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12682. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12683. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12684. } else if (!IS_GEN2(dev_priv)) {
  12685. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12686. dev_priv->display.get_initial_plane_config =
  12687. i9xx_get_initial_plane_config;
  12688. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12689. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12690. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12691. } else {
  12692. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12693. dev_priv->display.get_initial_plane_config =
  12694. i9xx_get_initial_plane_config;
  12695. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12696. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12697. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12698. }
  12699. /* Returns the core display clock speed */
  12700. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12701. dev_priv->display.get_display_clock_speed =
  12702. skylake_get_display_clock_speed;
  12703. else if (IS_BROXTON(dev_priv))
  12704. dev_priv->display.get_display_clock_speed =
  12705. broxton_get_display_clock_speed;
  12706. else if (IS_BROADWELL(dev_priv))
  12707. dev_priv->display.get_display_clock_speed =
  12708. broadwell_get_display_clock_speed;
  12709. else if (IS_HASWELL(dev_priv))
  12710. dev_priv->display.get_display_clock_speed =
  12711. haswell_get_display_clock_speed;
  12712. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12713. dev_priv->display.get_display_clock_speed =
  12714. valleyview_get_display_clock_speed;
  12715. else if (IS_GEN5(dev_priv))
  12716. dev_priv->display.get_display_clock_speed =
  12717. ilk_get_display_clock_speed;
  12718. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12719. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12720. dev_priv->display.get_display_clock_speed =
  12721. i945_get_display_clock_speed;
  12722. else if (IS_GM45(dev_priv))
  12723. dev_priv->display.get_display_clock_speed =
  12724. gm45_get_display_clock_speed;
  12725. else if (IS_CRESTLINE(dev_priv))
  12726. dev_priv->display.get_display_clock_speed =
  12727. i965gm_get_display_clock_speed;
  12728. else if (IS_PINEVIEW(dev_priv))
  12729. dev_priv->display.get_display_clock_speed =
  12730. pnv_get_display_clock_speed;
  12731. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12732. dev_priv->display.get_display_clock_speed =
  12733. g33_get_display_clock_speed;
  12734. else if (IS_I915G(dev_priv))
  12735. dev_priv->display.get_display_clock_speed =
  12736. i915_get_display_clock_speed;
  12737. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12738. dev_priv->display.get_display_clock_speed =
  12739. i9xx_misc_get_display_clock_speed;
  12740. else if (IS_I915GM(dev_priv))
  12741. dev_priv->display.get_display_clock_speed =
  12742. i915gm_get_display_clock_speed;
  12743. else if (IS_I865G(dev_priv))
  12744. dev_priv->display.get_display_clock_speed =
  12745. i865_get_display_clock_speed;
  12746. else if (IS_I85X(dev_priv))
  12747. dev_priv->display.get_display_clock_speed =
  12748. i85x_get_display_clock_speed;
  12749. else { /* 830 */
  12750. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12751. dev_priv->display.get_display_clock_speed =
  12752. i830_get_display_clock_speed;
  12753. }
  12754. if (IS_GEN5(dev_priv)) {
  12755. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12756. } else if (IS_GEN6(dev_priv)) {
  12757. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12758. } else if (IS_IVYBRIDGE(dev_priv)) {
  12759. /* FIXME: detect B0+ stepping and use auto training */
  12760. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12761. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12762. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12763. }
  12764. if (IS_BROADWELL(dev_priv)) {
  12765. dev_priv->display.modeset_commit_cdclk =
  12766. broadwell_modeset_commit_cdclk;
  12767. dev_priv->display.modeset_calc_cdclk =
  12768. broadwell_modeset_calc_cdclk;
  12769. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12770. dev_priv->display.modeset_commit_cdclk =
  12771. valleyview_modeset_commit_cdclk;
  12772. dev_priv->display.modeset_calc_cdclk =
  12773. valleyview_modeset_calc_cdclk;
  12774. } else if (IS_BROXTON(dev_priv)) {
  12775. dev_priv->display.modeset_commit_cdclk =
  12776. bxt_modeset_commit_cdclk;
  12777. dev_priv->display.modeset_calc_cdclk =
  12778. bxt_modeset_calc_cdclk;
  12779. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12780. dev_priv->display.modeset_commit_cdclk =
  12781. skl_modeset_commit_cdclk;
  12782. dev_priv->display.modeset_calc_cdclk =
  12783. skl_modeset_calc_cdclk;
  12784. }
  12785. switch (INTEL_INFO(dev_priv)->gen) {
  12786. case 2:
  12787. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12788. break;
  12789. case 3:
  12790. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12791. break;
  12792. case 4:
  12793. case 5:
  12794. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12795. break;
  12796. case 6:
  12797. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12798. break;
  12799. case 7:
  12800. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12801. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12802. break;
  12803. case 9:
  12804. /* Drop through - unsupported since execlist only. */
  12805. default:
  12806. /* Default just returns -ENODEV to indicate unsupported */
  12807. dev_priv->display.queue_flip = intel_default_queue_flip;
  12808. }
  12809. }
  12810. /*
  12811. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12812. * resume, or other times. This quirk makes sure that's the case for
  12813. * affected systems.
  12814. */
  12815. static void quirk_pipea_force(struct drm_device *dev)
  12816. {
  12817. struct drm_i915_private *dev_priv = to_i915(dev);
  12818. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12819. DRM_INFO("applying pipe a force quirk\n");
  12820. }
  12821. static void quirk_pipeb_force(struct drm_device *dev)
  12822. {
  12823. struct drm_i915_private *dev_priv = to_i915(dev);
  12824. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12825. DRM_INFO("applying pipe b force quirk\n");
  12826. }
  12827. /*
  12828. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12829. */
  12830. static void quirk_ssc_force_disable(struct drm_device *dev)
  12831. {
  12832. struct drm_i915_private *dev_priv = to_i915(dev);
  12833. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12834. DRM_INFO("applying lvds SSC disable quirk\n");
  12835. }
  12836. /*
  12837. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12838. * brightness value
  12839. */
  12840. static void quirk_invert_brightness(struct drm_device *dev)
  12841. {
  12842. struct drm_i915_private *dev_priv = to_i915(dev);
  12843. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12844. DRM_INFO("applying inverted panel brightness quirk\n");
  12845. }
  12846. /* Some VBT's incorrectly indicate no backlight is present */
  12847. static void quirk_backlight_present(struct drm_device *dev)
  12848. {
  12849. struct drm_i915_private *dev_priv = to_i915(dev);
  12850. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12851. DRM_INFO("applying backlight present quirk\n");
  12852. }
  12853. struct intel_quirk {
  12854. int device;
  12855. int subsystem_vendor;
  12856. int subsystem_device;
  12857. void (*hook)(struct drm_device *dev);
  12858. };
  12859. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12860. struct intel_dmi_quirk {
  12861. void (*hook)(struct drm_device *dev);
  12862. const struct dmi_system_id (*dmi_id_list)[];
  12863. };
  12864. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12865. {
  12866. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12867. return 1;
  12868. }
  12869. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12870. {
  12871. .dmi_id_list = &(const struct dmi_system_id[]) {
  12872. {
  12873. .callback = intel_dmi_reverse_brightness,
  12874. .ident = "NCR Corporation",
  12875. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12876. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12877. },
  12878. },
  12879. { } /* terminating entry */
  12880. },
  12881. .hook = quirk_invert_brightness,
  12882. },
  12883. };
  12884. static struct intel_quirk intel_quirks[] = {
  12885. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12886. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12887. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12888. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12889. /* 830 needs to leave pipe A & dpll A up */
  12890. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12891. /* 830 needs to leave pipe B & dpll B up */
  12892. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12893. /* Lenovo U160 cannot use SSC on LVDS */
  12894. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12895. /* Sony Vaio Y cannot use SSC on LVDS */
  12896. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12897. /* Acer Aspire 5734Z must invert backlight brightness */
  12898. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12899. /* Acer/eMachines G725 */
  12900. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12901. /* Acer/eMachines e725 */
  12902. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12903. /* Acer/Packard Bell NCL20 */
  12904. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12905. /* Acer Aspire 4736Z */
  12906. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12907. /* Acer Aspire 5336 */
  12908. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12909. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12910. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12911. /* Acer C720 Chromebook (Core i3 4005U) */
  12912. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12913. /* Apple Macbook 2,1 (Core 2 T7400) */
  12914. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12915. /* Apple Macbook 4,1 */
  12916. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12917. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12918. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12919. /* HP Chromebook 14 (Celeron 2955U) */
  12920. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12921. /* Dell Chromebook 11 */
  12922. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12923. /* Dell Chromebook 11 (2015 version) */
  12924. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12925. };
  12926. static void intel_init_quirks(struct drm_device *dev)
  12927. {
  12928. struct pci_dev *d = dev->pdev;
  12929. int i;
  12930. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12931. struct intel_quirk *q = &intel_quirks[i];
  12932. if (d->device == q->device &&
  12933. (d->subsystem_vendor == q->subsystem_vendor ||
  12934. q->subsystem_vendor == PCI_ANY_ID) &&
  12935. (d->subsystem_device == q->subsystem_device ||
  12936. q->subsystem_device == PCI_ANY_ID))
  12937. q->hook(dev);
  12938. }
  12939. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12940. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12941. intel_dmi_quirks[i].hook(dev);
  12942. }
  12943. }
  12944. /* Disable the VGA plane that we never use */
  12945. static void i915_disable_vga(struct drm_device *dev)
  12946. {
  12947. struct drm_i915_private *dev_priv = to_i915(dev);
  12948. u8 sr1;
  12949. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12950. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12951. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12952. outb(SR01, VGA_SR_INDEX);
  12953. sr1 = inb(VGA_SR_DATA);
  12954. outb(sr1 | 1<<5, VGA_SR_DATA);
  12955. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12956. udelay(300);
  12957. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12958. POSTING_READ(vga_reg);
  12959. }
  12960. void intel_modeset_init_hw(struct drm_device *dev)
  12961. {
  12962. struct drm_i915_private *dev_priv = to_i915(dev);
  12963. intel_update_cdclk(dev);
  12964. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12965. intel_init_clock_gating(dev);
  12966. intel_enable_gt_powersave(dev_priv);
  12967. }
  12968. /*
  12969. * Calculate what we think the watermarks should be for the state we've read
  12970. * out of the hardware and then immediately program those watermarks so that
  12971. * we ensure the hardware settings match our internal state.
  12972. *
  12973. * We can calculate what we think WM's should be by creating a duplicate of the
  12974. * current state (which was constructed during hardware readout) and running it
  12975. * through the atomic check code to calculate new watermark values in the
  12976. * state object.
  12977. */
  12978. static void sanitize_watermarks(struct drm_device *dev)
  12979. {
  12980. struct drm_i915_private *dev_priv = to_i915(dev);
  12981. struct drm_atomic_state *state;
  12982. struct drm_crtc *crtc;
  12983. struct drm_crtc_state *cstate;
  12984. struct drm_modeset_acquire_ctx ctx;
  12985. int ret;
  12986. int i;
  12987. /* Only supported on platforms that use atomic watermark design */
  12988. if (!dev_priv->display.optimize_watermarks)
  12989. return;
  12990. /*
  12991. * We need to hold connection_mutex before calling duplicate_state so
  12992. * that the connector loop is protected.
  12993. */
  12994. drm_modeset_acquire_init(&ctx, 0);
  12995. retry:
  12996. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12997. if (ret == -EDEADLK) {
  12998. drm_modeset_backoff(&ctx);
  12999. goto retry;
  13000. } else if (WARN_ON(ret)) {
  13001. goto fail;
  13002. }
  13003. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13004. if (WARN_ON(IS_ERR(state)))
  13005. goto fail;
  13006. /*
  13007. * Hardware readout is the only time we don't want to calculate
  13008. * intermediate watermarks (since we don't trust the current
  13009. * watermarks).
  13010. */
  13011. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13012. ret = intel_atomic_check(dev, state);
  13013. if (ret) {
  13014. /*
  13015. * If we fail here, it means that the hardware appears to be
  13016. * programmed in a way that shouldn't be possible, given our
  13017. * understanding of watermark requirements. This might mean a
  13018. * mistake in the hardware readout code or a mistake in the
  13019. * watermark calculations for a given platform. Raise a WARN
  13020. * so that this is noticeable.
  13021. *
  13022. * If this actually happens, we'll have to just leave the
  13023. * BIOS-programmed watermarks untouched and hope for the best.
  13024. */
  13025. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13026. goto fail;
  13027. }
  13028. /* Write calculated watermark values back */
  13029. for_each_crtc_in_state(state, crtc, cstate, i) {
  13030. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13031. cs->wm.need_postvbl_update = true;
  13032. dev_priv->display.optimize_watermarks(cs);
  13033. }
  13034. drm_atomic_state_free(state);
  13035. fail:
  13036. drm_modeset_drop_locks(&ctx);
  13037. drm_modeset_acquire_fini(&ctx);
  13038. }
  13039. void intel_modeset_init(struct drm_device *dev)
  13040. {
  13041. struct drm_i915_private *dev_priv = to_i915(dev);
  13042. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13043. int sprite, ret;
  13044. enum pipe pipe;
  13045. struct intel_crtc *crtc;
  13046. drm_mode_config_init(dev);
  13047. dev->mode_config.min_width = 0;
  13048. dev->mode_config.min_height = 0;
  13049. dev->mode_config.preferred_depth = 24;
  13050. dev->mode_config.prefer_shadow = 1;
  13051. dev->mode_config.allow_fb_modifiers = true;
  13052. dev->mode_config.funcs = &intel_mode_funcs;
  13053. intel_init_quirks(dev);
  13054. intel_init_pm(dev);
  13055. if (INTEL_INFO(dev)->num_pipes == 0)
  13056. return;
  13057. /*
  13058. * There may be no VBT; and if the BIOS enabled SSC we can
  13059. * just keep using it to avoid unnecessary flicker. Whereas if the
  13060. * BIOS isn't using it, don't assume it will work even if the VBT
  13061. * indicates as much.
  13062. */
  13063. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  13064. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13065. DREF_SSC1_ENABLE);
  13066. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13067. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13068. bios_lvds_use_ssc ? "en" : "dis",
  13069. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13070. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13071. }
  13072. }
  13073. if (IS_GEN2(dev)) {
  13074. dev->mode_config.max_width = 2048;
  13075. dev->mode_config.max_height = 2048;
  13076. } else if (IS_GEN3(dev)) {
  13077. dev->mode_config.max_width = 4096;
  13078. dev->mode_config.max_height = 4096;
  13079. } else {
  13080. dev->mode_config.max_width = 8192;
  13081. dev->mode_config.max_height = 8192;
  13082. }
  13083. if (IS_845G(dev) || IS_I865G(dev)) {
  13084. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  13085. dev->mode_config.cursor_height = 1023;
  13086. } else if (IS_GEN2(dev)) {
  13087. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13088. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13089. } else {
  13090. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13091. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13092. }
  13093. dev->mode_config.fb_base = ggtt->mappable_base;
  13094. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13095. INTEL_INFO(dev)->num_pipes,
  13096. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13097. for_each_pipe(dev_priv, pipe) {
  13098. intel_crtc_init(dev, pipe);
  13099. for_each_sprite(dev_priv, pipe, sprite) {
  13100. ret = intel_plane_init(dev, pipe, sprite);
  13101. if (ret)
  13102. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13103. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13104. }
  13105. }
  13106. intel_update_czclk(dev_priv);
  13107. intel_update_cdclk(dev);
  13108. intel_shared_dpll_init(dev);
  13109. if (dev_priv->max_cdclk_freq == 0)
  13110. intel_update_max_cdclk(dev);
  13111. /* Just disable it once at startup */
  13112. i915_disable_vga(dev);
  13113. intel_setup_outputs(dev);
  13114. drm_modeset_lock_all(dev);
  13115. intel_modeset_setup_hw_state(dev);
  13116. drm_modeset_unlock_all(dev);
  13117. for_each_intel_crtc(dev, crtc) {
  13118. struct intel_initial_plane_config plane_config = {};
  13119. if (!crtc->active)
  13120. continue;
  13121. /*
  13122. * Note that reserving the BIOS fb up front prevents us
  13123. * from stuffing other stolen allocations like the ring
  13124. * on top. This prevents some ugliness at boot time, and
  13125. * can even allow for smooth boot transitions if the BIOS
  13126. * fb is large enough for the active pipe configuration.
  13127. */
  13128. dev_priv->display.get_initial_plane_config(crtc,
  13129. &plane_config);
  13130. /*
  13131. * If the fb is shared between multiple heads, we'll
  13132. * just get the first one.
  13133. */
  13134. intel_find_initial_plane_obj(crtc, &plane_config);
  13135. }
  13136. /*
  13137. * Make sure hardware watermarks really match the state we read out.
  13138. * Note that we need to do this after reconstructing the BIOS fb's
  13139. * since the watermark calculation done here will use pstate->fb.
  13140. */
  13141. sanitize_watermarks(dev);
  13142. }
  13143. static void intel_enable_pipe_a(struct drm_device *dev)
  13144. {
  13145. struct intel_connector *connector;
  13146. struct drm_connector *crt = NULL;
  13147. struct intel_load_detect_pipe load_detect_temp;
  13148. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13149. /* We can't just switch on the pipe A, we need to set things up with a
  13150. * proper mode and output configuration. As a gross hack, enable pipe A
  13151. * by enabling the load detect pipe once. */
  13152. for_each_intel_connector(dev, connector) {
  13153. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13154. crt = &connector->base;
  13155. break;
  13156. }
  13157. }
  13158. if (!crt)
  13159. return;
  13160. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13161. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13162. }
  13163. static bool
  13164. intel_check_plane_mapping(struct intel_crtc *crtc)
  13165. {
  13166. struct drm_device *dev = crtc->base.dev;
  13167. struct drm_i915_private *dev_priv = to_i915(dev);
  13168. u32 val;
  13169. if (INTEL_INFO(dev)->num_pipes == 1)
  13170. return true;
  13171. val = I915_READ(DSPCNTR(!crtc->plane));
  13172. if ((val & DISPLAY_PLANE_ENABLE) &&
  13173. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13174. return false;
  13175. return true;
  13176. }
  13177. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13178. {
  13179. struct drm_device *dev = crtc->base.dev;
  13180. struct intel_encoder *encoder;
  13181. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13182. return true;
  13183. return false;
  13184. }
  13185. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  13186. {
  13187. struct drm_device *dev = encoder->base.dev;
  13188. struct intel_connector *connector;
  13189. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13190. return true;
  13191. return false;
  13192. }
  13193. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13194. {
  13195. struct drm_device *dev = crtc->base.dev;
  13196. struct drm_i915_private *dev_priv = to_i915(dev);
  13197. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13198. /* Clear any frame start delays used for debugging left by the BIOS */
  13199. if (!transcoder_is_dsi(cpu_transcoder)) {
  13200. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13201. I915_WRITE(reg,
  13202. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13203. }
  13204. /* restore vblank interrupts to correct state */
  13205. drm_crtc_vblank_reset(&crtc->base);
  13206. if (crtc->active) {
  13207. struct intel_plane *plane;
  13208. drm_crtc_vblank_on(&crtc->base);
  13209. /* Disable everything but the primary plane */
  13210. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13211. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13212. continue;
  13213. plane->disable_plane(&plane->base, &crtc->base);
  13214. }
  13215. }
  13216. /* We need to sanitize the plane -> pipe mapping first because this will
  13217. * disable the crtc (and hence change the state) if it is wrong. Note
  13218. * that gen4+ has a fixed plane -> pipe mapping. */
  13219. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13220. bool plane;
  13221. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13222. crtc->base.base.id, crtc->base.name);
  13223. /* Pipe has the wrong plane attached and the plane is active.
  13224. * Temporarily change the plane mapping and disable everything
  13225. * ... */
  13226. plane = crtc->plane;
  13227. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  13228. crtc->plane = !plane;
  13229. intel_crtc_disable_noatomic(&crtc->base);
  13230. crtc->plane = plane;
  13231. }
  13232. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13233. crtc->pipe == PIPE_A && !crtc->active) {
  13234. /* BIOS forgot to enable pipe A, this mostly happens after
  13235. * resume. Force-enable the pipe to fix this, the update_dpms
  13236. * call below we restore the pipe to the right state, but leave
  13237. * the required bits on. */
  13238. intel_enable_pipe_a(dev);
  13239. }
  13240. /* Adjust the state of the output pipe according to whether we
  13241. * have active connectors/encoders. */
  13242. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13243. intel_crtc_disable_noatomic(&crtc->base);
  13244. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13245. /*
  13246. * We start out with underrun reporting disabled to avoid races.
  13247. * For correct bookkeeping mark this on active crtcs.
  13248. *
  13249. * Also on gmch platforms we dont have any hardware bits to
  13250. * disable the underrun reporting. Which means we need to start
  13251. * out with underrun reporting disabled also on inactive pipes,
  13252. * since otherwise we'll complain about the garbage we read when
  13253. * e.g. coming up after runtime pm.
  13254. *
  13255. * No protection against concurrent access is required - at
  13256. * worst a fifo underrun happens which also sets this to false.
  13257. */
  13258. crtc->cpu_fifo_underrun_disabled = true;
  13259. crtc->pch_fifo_underrun_disabled = true;
  13260. }
  13261. }
  13262. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13263. {
  13264. struct intel_connector *connector;
  13265. struct drm_device *dev = encoder->base.dev;
  13266. /* We need to check both for a crtc link (meaning that the
  13267. * encoder is active and trying to read from a pipe) and the
  13268. * pipe itself being active. */
  13269. bool has_active_crtc = encoder->base.crtc &&
  13270. to_intel_crtc(encoder->base.crtc)->active;
  13271. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  13272. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13273. encoder->base.base.id,
  13274. encoder->base.name);
  13275. /* Connector is active, but has no active pipe. This is
  13276. * fallout from our resume register restoring. Disable
  13277. * the encoder manually again. */
  13278. if (encoder->base.crtc) {
  13279. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13280. encoder->base.base.id,
  13281. encoder->base.name);
  13282. encoder->disable(encoder);
  13283. if (encoder->post_disable)
  13284. encoder->post_disable(encoder);
  13285. }
  13286. encoder->base.crtc = NULL;
  13287. /* Inconsistent output/port/pipe state happens presumably due to
  13288. * a bug in one of the get_hw_state functions. Or someplace else
  13289. * in our code, like the register restore mess on resume. Clamp
  13290. * things to off as a safer default. */
  13291. for_each_intel_connector(dev, connector) {
  13292. if (connector->encoder != encoder)
  13293. continue;
  13294. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13295. connector->base.encoder = NULL;
  13296. }
  13297. }
  13298. /* Enabled encoders without active connectors will be fixed in
  13299. * the crtc fixup. */
  13300. }
  13301. void i915_redisable_vga_power_on(struct drm_device *dev)
  13302. {
  13303. struct drm_i915_private *dev_priv = to_i915(dev);
  13304. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13305. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13306. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13307. i915_disable_vga(dev);
  13308. }
  13309. }
  13310. void i915_redisable_vga(struct drm_device *dev)
  13311. {
  13312. struct drm_i915_private *dev_priv = to_i915(dev);
  13313. /* This function can be called both from intel_modeset_setup_hw_state or
  13314. * at a very early point in our resume sequence, where the power well
  13315. * structures are not yet restored. Since this function is at a very
  13316. * paranoid "someone might have enabled VGA while we were not looking"
  13317. * level, just check if the power well is enabled instead of trying to
  13318. * follow the "don't touch the power well if we don't need it" policy
  13319. * the rest of the driver uses. */
  13320. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  13321. return;
  13322. i915_redisable_vga_power_on(dev);
  13323. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  13324. }
  13325. static bool primary_get_hw_state(struct intel_plane *plane)
  13326. {
  13327. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13328. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13329. }
  13330. /* FIXME read out full plane state for all planes */
  13331. static void readout_plane_state(struct intel_crtc *crtc)
  13332. {
  13333. struct drm_plane *primary = crtc->base.primary;
  13334. struct intel_plane_state *plane_state =
  13335. to_intel_plane_state(primary->state);
  13336. plane_state->visible = crtc->active &&
  13337. primary_get_hw_state(to_intel_plane(primary));
  13338. if (plane_state->visible)
  13339. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13340. }
  13341. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13342. {
  13343. struct drm_i915_private *dev_priv = to_i915(dev);
  13344. enum pipe pipe;
  13345. struct intel_crtc *crtc;
  13346. struct intel_encoder *encoder;
  13347. struct intel_connector *connector;
  13348. int i;
  13349. dev_priv->active_crtcs = 0;
  13350. for_each_intel_crtc(dev, crtc) {
  13351. struct intel_crtc_state *crtc_state = crtc->config;
  13352. int pixclk = 0;
  13353. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  13354. memset(crtc_state, 0, sizeof(*crtc_state));
  13355. crtc_state->base.crtc = &crtc->base;
  13356. crtc_state->base.active = crtc_state->base.enable =
  13357. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13358. crtc->base.enabled = crtc_state->base.enable;
  13359. crtc->active = crtc_state->base.active;
  13360. if (crtc_state->base.active) {
  13361. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13362. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  13363. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13364. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13365. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13366. else
  13367. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13368. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13369. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  13370. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13371. }
  13372. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13373. readout_plane_state(crtc);
  13374. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  13375. crtc->base.base.id, crtc->base.name,
  13376. crtc->active ? "enabled" : "disabled");
  13377. }
  13378. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13379. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13380. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  13381. &pll->config.hw_state);
  13382. pll->config.crtc_mask = 0;
  13383. for_each_intel_crtc(dev, crtc) {
  13384. if (crtc->active && crtc->config->shared_dpll == pll)
  13385. pll->config.crtc_mask |= 1 << crtc->pipe;
  13386. }
  13387. pll->active_mask = pll->config.crtc_mask;
  13388. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13389. pll->name, pll->config.crtc_mask, pll->on);
  13390. }
  13391. for_each_intel_encoder(dev, encoder) {
  13392. pipe = 0;
  13393. if (encoder->get_hw_state(encoder, &pipe)) {
  13394. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13395. encoder->base.crtc = &crtc->base;
  13396. crtc->config->output_types |= 1 << encoder->type;
  13397. encoder->get_config(encoder, crtc->config);
  13398. } else {
  13399. encoder->base.crtc = NULL;
  13400. }
  13401. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13402. encoder->base.base.id,
  13403. encoder->base.name,
  13404. encoder->base.crtc ? "enabled" : "disabled",
  13405. pipe_name(pipe));
  13406. }
  13407. for_each_intel_connector(dev, connector) {
  13408. if (connector->get_hw_state(connector)) {
  13409. connector->base.dpms = DRM_MODE_DPMS_ON;
  13410. encoder = connector->encoder;
  13411. connector->base.encoder = &encoder->base;
  13412. if (encoder->base.crtc &&
  13413. encoder->base.crtc->state->active) {
  13414. /*
  13415. * This has to be done during hardware readout
  13416. * because anything calling .crtc_disable may
  13417. * rely on the connector_mask being accurate.
  13418. */
  13419. encoder->base.crtc->state->connector_mask |=
  13420. 1 << drm_connector_index(&connector->base);
  13421. encoder->base.crtc->state->encoder_mask |=
  13422. 1 << drm_encoder_index(&encoder->base);
  13423. }
  13424. } else {
  13425. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13426. connector->base.encoder = NULL;
  13427. }
  13428. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13429. connector->base.base.id,
  13430. connector->base.name,
  13431. connector->base.encoder ? "enabled" : "disabled");
  13432. }
  13433. for_each_intel_crtc(dev, crtc) {
  13434. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13435. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13436. if (crtc->base.state->active) {
  13437. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13438. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13439. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13440. /*
  13441. * The initial mode needs to be set in order to keep
  13442. * the atomic core happy. It wants a valid mode if the
  13443. * crtc's enabled, so we do the above call.
  13444. *
  13445. * At this point some state updated by the connectors
  13446. * in their ->detect() callback has not run yet, so
  13447. * no recalculation can be done yet.
  13448. *
  13449. * Even if we could do a recalculation and modeset
  13450. * right now it would cause a double modeset if
  13451. * fbdev or userspace chooses a different initial mode.
  13452. *
  13453. * If that happens, someone indicated they wanted a
  13454. * mode change, which means it's safe to do a full
  13455. * recalculation.
  13456. */
  13457. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13458. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13459. update_scanline_offset(crtc);
  13460. }
  13461. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  13462. }
  13463. }
  13464. /* Scan out the current hw modeset state,
  13465. * and sanitizes it to the current state
  13466. */
  13467. static void
  13468. intel_modeset_setup_hw_state(struct drm_device *dev)
  13469. {
  13470. struct drm_i915_private *dev_priv = to_i915(dev);
  13471. enum pipe pipe;
  13472. struct intel_crtc *crtc;
  13473. struct intel_encoder *encoder;
  13474. int i;
  13475. intel_modeset_readout_hw_state(dev);
  13476. /* HW state is read out, now we need to sanitize this mess. */
  13477. for_each_intel_encoder(dev, encoder) {
  13478. intel_sanitize_encoder(encoder);
  13479. }
  13480. for_each_pipe(dev_priv, pipe) {
  13481. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13482. intel_sanitize_crtc(crtc);
  13483. intel_dump_pipe_config(crtc, crtc->config,
  13484. "[setup_hw_state]");
  13485. }
  13486. intel_modeset_update_connector_atomic_state(dev);
  13487. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13488. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13489. if (!pll->on || pll->active_mask)
  13490. continue;
  13491. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13492. pll->funcs.disable(dev_priv, pll);
  13493. pll->on = false;
  13494. }
  13495. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13496. vlv_wm_get_hw_state(dev);
  13497. else if (IS_GEN9(dev))
  13498. skl_wm_get_hw_state(dev);
  13499. else if (HAS_PCH_SPLIT(dev))
  13500. ilk_wm_get_hw_state(dev);
  13501. for_each_intel_crtc(dev, crtc) {
  13502. unsigned long put_domains;
  13503. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  13504. if (WARN_ON(put_domains))
  13505. modeset_put_power_domains(dev_priv, put_domains);
  13506. }
  13507. intel_display_set_init_power(dev_priv, false);
  13508. intel_fbc_init_pipe_state(dev_priv);
  13509. }
  13510. void intel_display_resume(struct drm_device *dev)
  13511. {
  13512. struct drm_i915_private *dev_priv = to_i915(dev);
  13513. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  13514. struct drm_modeset_acquire_ctx ctx;
  13515. int ret;
  13516. bool setup = false;
  13517. dev_priv->modeset_restore_state = NULL;
  13518. /*
  13519. * This is a cludge because with real atomic modeset mode_config.mutex
  13520. * won't be taken. Unfortunately some probed state like
  13521. * audio_codec_enable is still protected by mode_config.mutex, so lock
  13522. * it here for now.
  13523. */
  13524. mutex_lock(&dev->mode_config.mutex);
  13525. drm_modeset_acquire_init(&ctx, 0);
  13526. retry:
  13527. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13528. if (ret == 0 && !setup) {
  13529. setup = true;
  13530. intel_modeset_setup_hw_state(dev);
  13531. i915_redisable_vga(dev);
  13532. }
  13533. if (ret == 0 && state) {
  13534. struct drm_crtc_state *crtc_state;
  13535. struct drm_crtc *crtc;
  13536. int i;
  13537. state->acquire_ctx = &ctx;
  13538. /* ignore any reset values/BIOS leftovers in the WM registers */
  13539. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13540. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  13541. /*
  13542. * Force recalculation even if we restore
  13543. * current state. With fast modeset this may not result
  13544. * in a modeset when the state is compatible.
  13545. */
  13546. crtc_state->mode_changed = true;
  13547. }
  13548. ret = drm_atomic_commit(state);
  13549. }
  13550. if (ret == -EDEADLK) {
  13551. drm_modeset_backoff(&ctx);
  13552. goto retry;
  13553. }
  13554. drm_modeset_drop_locks(&ctx);
  13555. drm_modeset_acquire_fini(&ctx);
  13556. mutex_unlock(&dev->mode_config.mutex);
  13557. if (ret) {
  13558. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13559. drm_atomic_state_free(state);
  13560. }
  13561. }
  13562. void intel_modeset_gem_init(struct drm_device *dev)
  13563. {
  13564. struct drm_i915_private *dev_priv = to_i915(dev);
  13565. struct drm_crtc *c;
  13566. struct drm_i915_gem_object *obj;
  13567. int ret;
  13568. intel_init_gt_powersave(dev_priv);
  13569. intel_modeset_init_hw(dev);
  13570. intel_setup_overlay(dev_priv);
  13571. /*
  13572. * Make sure any fbs we allocated at startup are properly
  13573. * pinned & fenced. When we do the allocation it's too early
  13574. * for this.
  13575. */
  13576. for_each_crtc(dev, c) {
  13577. obj = intel_fb_obj(c->primary->fb);
  13578. if (obj == NULL)
  13579. continue;
  13580. mutex_lock(&dev->struct_mutex);
  13581. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  13582. c->primary->state->rotation);
  13583. mutex_unlock(&dev->struct_mutex);
  13584. if (ret) {
  13585. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13586. to_intel_crtc(c)->pipe);
  13587. drm_framebuffer_unreference(c->primary->fb);
  13588. c->primary->fb = NULL;
  13589. c->primary->crtc = c->primary->state->crtc = NULL;
  13590. update_state_fb(c->primary);
  13591. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13592. }
  13593. }
  13594. }
  13595. int intel_connector_register(struct drm_connector *connector)
  13596. {
  13597. struct intel_connector *intel_connector = to_intel_connector(connector);
  13598. int ret;
  13599. ret = intel_backlight_device_register(intel_connector);
  13600. if (ret)
  13601. goto err;
  13602. return 0;
  13603. err:
  13604. return ret;
  13605. }
  13606. void intel_connector_unregister(struct drm_connector *connector)
  13607. {
  13608. struct intel_connector *intel_connector = to_intel_connector(connector);
  13609. intel_backlight_device_unregister(intel_connector);
  13610. intel_panel_destroy_backlight(connector);
  13611. }
  13612. void intel_modeset_cleanup(struct drm_device *dev)
  13613. {
  13614. struct drm_i915_private *dev_priv = to_i915(dev);
  13615. intel_disable_gt_powersave(dev_priv);
  13616. /*
  13617. * Interrupts and polling as the first thing to avoid creating havoc.
  13618. * Too much stuff here (turning of connectors, ...) would
  13619. * experience fancy races otherwise.
  13620. */
  13621. intel_irq_uninstall(dev_priv);
  13622. /*
  13623. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13624. * poll handlers. Hence disable polling after hpd handling is shut down.
  13625. */
  13626. drm_kms_helper_poll_fini(dev);
  13627. intel_unregister_dsm_handler();
  13628. intel_fbc_global_disable(dev_priv);
  13629. /* flush any delayed tasks or pending work */
  13630. flush_scheduled_work();
  13631. drm_mode_config_cleanup(dev);
  13632. intel_cleanup_overlay(dev_priv);
  13633. intel_cleanup_gt_powersave(dev_priv);
  13634. intel_teardown_gmbus(dev);
  13635. }
  13636. void intel_connector_attach_encoder(struct intel_connector *connector,
  13637. struct intel_encoder *encoder)
  13638. {
  13639. connector->encoder = encoder;
  13640. drm_mode_connector_attach_encoder(&connector->base,
  13641. &encoder->base);
  13642. }
  13643. /*
  13644. * set vga decode state - true == enable VGA decode
  13645. */
  13646. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13647. {
  13648. struct drm_i915_private *dev_priv = to_i915(dev);
  13649. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13650. u16 gmch_ctrl;
  13651. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13652. DRM_ERROR("failed to read control word\n");
  13653. return -EIO;
  13654. }
  13655. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13656. return 0;
  13657. if (state)
  13658. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13659. else
  13660. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13661. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13662. DRM_ERROR("failed to write control word\n");
  13663. return -EIO;
  13664. }
  13665. return 0;
  13666. }
  13667. struct intel_display_error_state {
  13668. u32 power_well_driver;
  13669. int num_transcoders;
  13670. struct intel_cursor_error_state {
  13671. u32 control;
  13672. u32 position;
  13673. u32 base;
  13674. u32 size;
  13675. } cursor[I915_MAX_PIPES];
  13676. struct intel_pipe_error_state {
  13677. bool power_domain_on;
  13678. u32 source;
  13679. u32 stat;
  13680. } pipe[I915_MAX_PIPES];
  13681. struct intel_plane_error_state {
  13682. u32 control;
  13683. u32 stride;
  13684. u32 size;
  13685. u32 pos;
  13686. u32 addr;
  13687. u32 surface;
  13688. u32 tile_offset;
  13689. } plane[I915_MAX_PIPES];
  13690. struct intel_transcoder_error_state {
  13691. bool power_domain_on;
  13692. enum transcoder cpu_transcoder;
  13693. u32 conf;
  13694. u32 htotal;
  13695. u32 hblank;
  13696. u32 hsync;
  13697. u32 vtotal;
  13698. u32 vblank;
  13699. u32 vsync;
  13700. } transcoder[4];
  13701. };
  13702. struct intel_display_error_state *
  13703. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13704. {
  13705. struct intel_display_error_state *error;
  13706. int transcoders[] = {
  13707. TRANSCODER_A,
  13708. TRANSCODER_B,
  13709. TRANSCODER_C,
  13710. TRANSCODER_EDP,
  13711. };
  13712. int i;
  13713. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13714. return NULL;
  13715. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13716. if (error == NULL)
  13717. return NULL;
  13718. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13719. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13720. for_each_pipe(dev_priv, i) {
  13721. error->pipe[i].power_domain_on =
  13722. __intel_display_power_is_enabled(dev_priv,
  13723. POWER_DOMAIN_PIPE(i));
  13724. if (!error->pipe[i].power_domain_on)
  13725. continue;
  13726. error->cursor[i].control = I915_READ(CURCNTR(i));
  13727. error->cursor[i].position = I915_READ(CURPOS(i));
  13728. error->cursor[i].base = I915_READ(CURBASE(i));
  13729. error->plane[i].control = I915_READ(DSPCNTR(i));
  13730. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13731. if (INTEL_GEN(dev_priv) <= 3) {
  13732. error->plane[i].size = I915_READ(DSPSIZE(i));
  13733. error->plane[i].pos = I915_READ(DSPPOS(i));
  13734. }
  13735. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13736. error->plane[i].addr = I915_READ(DSPADDR(i));
  13737. if (INTEL_GEN(dev_priv) >= 4) {
  13738. error->plane[i].surface = I915_READ(DSPSURF(i));
  13739. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13740. }
  13741. error->pipe[i].source = I915_READ(PIPESRC(i));
  13742. if (HAS_GMCH_DISPLAY(dev_priv))
  13743. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13744. }
  13745. /* Note: this does not include DSI transcoders. */
  13746. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13747. if (HAS_DDI(dev_priv))
  13748. error->num_transcoders++; /* Account for eDP. */
  13749. for (i = 0; i < error->num_transcoders; i++) {
  13750. enum transcoder cpu_transcoder = transcoders[i];
  13751. error->transcoder[i].power_domain_on =
  13752. __intel_display_power_is_enabled(dev_priv,
  13753. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13754. if (!error->transcoder[i].power_domain_on)
  13755. continue;
  13756. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13757. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13758. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13759. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13760. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13761. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13762. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13763. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13764. }
  13765. return error;
  13766. }
  13767. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13768. void
  13769. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13770. struct drm_device *dev,
  13771. struct intel_display_error_state *error)
  13772. {
  13773. struct drm_i915_private *dev_priv = to_i915(dev);
  13774. int i;
  13775. if (!error)
  13776. return;
  13777. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13778. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13779. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13780. error->power_well_driver);
  13781. for_each_pipe(dev_priv, i) {
  13782. err_printf(m, "Pipe [%d]:\n", i);
  13783. err_printf(m, " Power: %s\n",
  13784. onoff(error->pipe[i].power_domain_on));
  13785. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13786. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13787. err_printf(m, "Plane [%d]:\n", i);
  13788. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13789. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13790. if (INTEL_INFO(dev)->gen <= 3) {
  13791. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13792. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13793. }
  13794. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13795. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13796. if (INTEL_INFO(dev)->gen >= 4) {
  13797. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13798. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13799. }
  13800. err_printf(m, "Cursor [%d]:\n", i);
  13801. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13802. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13803. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13804. }
  13805. for (i = 0; i < error->num_transcoders; i++) {
  13806. err_printf(m, "CPU transcoder: %s\n",
  13807. transcoder_name(error->transcoder[i].cpu_transcoder));
  13808. err_printf(m, " Power: %s\n",
  13809. onoff(error->transcoder[i].power_domain_on));
  13810. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13811. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13812. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13813. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13814. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13815. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13816. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13817. }
  13818. }