intel_display.c 452 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312
  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_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. #include <linux/reservation.h>
  47. #include <linux/dma-buf.h>
  48. /* Primary plane formats for gen <= 3 */
  49. static const uint32_t i8xx_primary_formats[] = {
  50. DRM_FORMAT_C8,
  51. DRM_FORMAT_RGB565,
  52. DRM_FORMAT_XRGB1555,
  53. DRM_FORMAT_XRGB8888,
  54. };
  55. /* Primary plane formats for gen >= 4 */
  56. static const uint32_t i965_primary_formats[] = {
  57. DRM_FORMAT_C8,
  58. DRM_FORMAT_RGB565,
  59. DRM_FORMAT_XRGB8888,
  60. DRM_FORMAT_XBGR8888,
  61. DRM_FORMAT_XRGB2101010,
  62. DRM_FORMAT_XBGR2101010,
  63. };
  64. static const uint32_t skl_primary_formats[] = {
  65. DRM_FORMAT_C8,
  66. DRM_FORMAT_RGB565,
  67. DRM_FORMAT_XRGB8888,
  68. DRM_FORMAT_XBGR8888,
  69. DRM_FORMAT_ARGB8888,
  70. DRM_FORMAT_ABGR8888,
  71. DRM_FORMAT_XRGB2101010,
  72. DRM_FORMAT_XBGR2101010,
  73. DRM_FORMAT_YUYV,
  74. DRM_FORMAT_YVYU,
  75. DRM_FORMAT_UYVY,
  76. DRM_FORMAT_VYUY,
  77. };
  78. /* Cursor formats */
  79. static const uint32_t intel_cursor_formats[] = {
  80. DRM_FORMAT_ARGB8888,
  81. };
  82. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  83. struct intel_crtc_state *pipe_config);
  84. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  85. struct intel_crtc_state *pipe_config);
  86. static int intel_framebuffer_init(struct drm_device *dev,
  87. struct intel_framebuffer *ifb,
  88. struct drm_mode_fb_cmd2 *mode_cmd,
  89. struct drm_i915_gem_object *obj);
  90. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  91. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  92. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  93. struct intel_link_m_n *m_n,
  94. struct intel_link_m_n *m2_n2);
  95. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  96. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  97. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  98. static void vlv_prepare_pll(struct intel_crtc *crtc,
  99. const struct intel_crtc_state *pipe_config);
  100. static void chv_prepare_pll(struct intel_crtc *crtc,
  101. const struct intel_crtc_state *pipe_config);
  102. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  104. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  105. struct intel_crtc_state *crtc_state);
  106. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  107. int num_connectors);
  108. static void skylake_pfit_enable(struct intel_crtc *crtc);
  109. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  110. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  111. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  112. static void intel_pre_disable_primary(struct drm_crtc *crtc);
  113. typedef struct {
  114. int min, max;
  115. } intel_range_t;
  116. typedef struct {
  117. int dot_limit;
  118. int p2_slow, p2_fast;
  119. } intel_p2_t;
  120. typedef struct intel_limit intel_limit_t;
  121. struct intel_limit {
  122. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  123. intel_p2_t p2;
  124. };
  125. /* returns HPLL frequency in kHz */
  126. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  127. {
  128. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  129. /* Obtain SKU information */
  130. mutex_lock(&dev_priv->sb_lock);
  131. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  132. CCK_FUSE_HPLL_FREQ_MASK;
  133. mutex_unlock(&dev_priv->sb_lock);
  134. return vco_freq[hpll_freq] * 1000;
  135. }
  136. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  137. const char *name, u32 reg)
  138. {
  139. u32 val;
  140. int divider;
  141. if (dev_priv->hpll_freq == 0)
  142. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  143. mutex_lock(&dev_priv->sb_lock);
  144. val = vlv_cck_read(dev_priv, reg);
  145. mutex_unlock(&dev_priv->sb_lock);
  146. divider = val & CCK_FREQUENCY_VALUES;
  147. WARN((val & CCK_FREQUENCY_STATUS) !=
  148. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  149. "%s change in progress\n", name);
  150. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  151. }
  152. int
  153. intel_pch_rawclk(struct drm_device *dev)
  154. {
  155. struct drm_i915_private *dev_priv = dev->dev_private;
  156. WARN_ON(!HAS_PCH_SPLIT(dev));
  157. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  158. }
  159. /* hrawclock is 1/4 the FSB frequency */
  160. int intel_hrawclk(struct drm_device *dev)
  161. {
  162. struct drm_i915_private *dev_priv = dev->dev_private;
  163. uint32_t clkcfg;
  164. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  165. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  166. return 200;
  167. clkcfg = I915_READ(CLKCFG);
  168. switch (clkcfg & CLKCFG_FSB_MASK) {
  169. case CLKCFG_FSB_400:
  170. return 100;
  171. case CLKCFG_FSB_533:
  172. return 133;
  173. case CLKCFG_FSB_667:
  174. return 166;
  175. case CLKCFG_FSB_800:
  176. return 200;
  177. case CLKCFG_FSB_1067:
  178. return 266;
  179. case CLKCFG_FSB_1333:
  180. return 333;
  181. /* these two are just a guess; one of them might be right */
  182. case CLKCFG_FSB_1600:
  183. case CLKCFG_FSB_1600_ALT:
  184. return 400;
  185. default:
  186. return 133;
  187. }
  188. }
  189. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  190. {
  191. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  192. return;
  193. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  194. CCK_CZ_CLOCK_CONTROL);
  195. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  196. }
  197. static inline u32 /* units of 100MHz */
  198. intel_fdi_link_freq(struct drm_device *dev)
  199. {
  200. if (IS_GEN5(dev)) {
  201. struct drm_i915_private *dev_priv = dev->dev_private;
  202. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  203. } else
  204. return 27;
  205. }
  206. static const intel_limit_t intel_limits_i8xx_dac = {
  207. .dot = { .min = 25000, .max = 350000 },
  208. .vco = { .min = 908000, .max = 1512000 },
  209. .n = { .min = 2, .max = 16 },
  210. .m = { .min = 96, .max = 140 },
  211. .m1 = { .min = 18, .max = 26 },
  212. .m2 = { .min = 6, .max = 16 },
  213. .p = { .min = 4, .max = 128 },
  214. .p1 = { .min = 2, .max = 33 },
  215. .p2 = { .dot_limit = 165000,
  216. .p2_slow = 4, .p2_fast = 2 },
  217. };
  218. static const intel_limit_t intel_limits_i8xx_dvo = {
  219. .dot = { .min = 25000, .max = 350000 },
  220. .vco = { .min = 908000, .max = 1512000 },
  221. .n = { .min = 2, .max = 16 },
  222. .m = { .min = 96, .max = 140 },
  223. .m1 = { .min = 18, .max = 26 },
  224. .m2 = { .min = 6, .max = 16 },
  225. .p = { .min = 4, .max = 128 },
  226. .p1 = { .min = 2, .max = 33 },
  227. .p2 = { .dot_limit = 165000,
  228. .p2_slow = 4, .p2_fast = 4 },
  229. };
  230. static const intel_limit_t intel_limits_i8xx_lvds = {
  231. .dot = { .min = 25000, .max = 350000 },
  232. .vco = { .min = 908000, .max = 1512000 },
  233. .n = { .min = 2, .max = 16 },
  234. .m = { .min = 96, .max = 140 },
  235. .m1 = { .min = 18, .max = 26 },
  236. .m2 = { .min = 6, .max = 16 },
  237. .p = { .min = 4, .max = 128 },
  238. .p1 = { .min = 1, .max = 6 },
  239. .p2 = { .dot_limit = 165000,
  240. .p2_slow = 14, .p2_fast = 7 },
  241. };
  242. static const intel_limit_t intel_limits_i9xx_sdvo = {
  243. .dot = { .min = 20000, .max = 400000 },
  244. .vco = { .min = 1400000, .max = 2800000 },
  245. .n = { .min = 1, .max = 6 },
  246. .m = { .min = 70, .max = 120 },
  247. .m1 = { .min = 8, .max = 18 },
  248. .m2 = { .min = 3, .max = 7 },
  249. .p = { .min = 5, .max = 80 },
  250. .p1 = { .min = 1, .max = 8 },
  251. .p2 = { .dot_limit = 200000,
  252. .p2_slow = 10, .p2_fast = 5 },
  253. };
  254. static const intel_limit_t intel_limits_i9xx_lvds = {
  255. .dot = { .min = 20000, .max = 400000 },
  256. .vco = { .min = 1400000, .max = 2800000 },
  257. .n = { .min = 1, .max = 6 },
  258. .m = { .min = 70, .max = 120 },
  259. .m1 = { .min = 8, .max = 18 },
  260. .m2 = { .min = 3, .max = 7 },
  261. .p = { .min = 7, .max = 98 },
  262. .p1 = { .min = 1, .max = 8 },
  263. .p2 = { .dot_limit = 112000,
  264. .p2_slow = 14, .p2_fast = 7 },
  265. };
  266. static const intel_limit_t intel_limits_g4x_sdvo = {
  267. .dot = { .min = 25000, .max = 270000 },
  268. .vco = { .min = 1750000, .max = 3500000},
  269. .n = { .min = 1, .max = 4 },
  270. .m = { .min = 104, .max = 138 },
  271. .m1 = { .min = 17, .max = 23 },
  272. .m2 = { .min = 5, .max = 11 },
  273. .p = { .min = 10, .max = 30 },
  274. .p1 = { .min = 1, .max = 3},
  275. .p2 = { .dot_limit = 270000,
  276. .p2_slow = 10,
  277. .p2_fast = 10
  278. },
  279. };
  280. static const intel_limit_t intel_limits_g4x_hdmi = {
  281. .dot = { .min = 22000, .max = 400000 },
  282. .vco = { .min = 1750000, .max = 3500000},
  283. .n = { .min = 1, .max = 4 },
  284. .m = { .min = 104, .max = 138 },
  285. .m1 = { .min = 16, .max = 23 },
  286. .m2 = { .min = 5, .max = 11 },
  287. .p = { .min = 5, .max = 80 },
  288. .p1 = { .min = 1, .max = 8},
  289. .p2 = { .dot_limit = 165000,
  290. .p2_slow = 10, .p2_fast = 5 },
  291. };
  292. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  293. .dot = { .min = 20000, .max = 115000 },
  294. .vco = { .min = 1750000, .max = 3500000 },
  295. .n = { .min = 1, .max = 3 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 28, .max = 112 },
  300. .p1 = { .min = 2, .max = 8 },
  301. .p2 = { .dot_limit = 0,
  302. .p2_slow = 14, .p2_fast = 14
  303. },
  304. };
  305. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  306. .dot = { .min = 80000, .max = 224000 },
  307. .vco = { .min = 1750000, .max = 3500000 },
  308. .n = { .min = 1, .max = 3 },
  309. .m = { .min = 104, .max = 138 },
  310. .m1 = { .min = 17, .max = 23 },
  311. .m2 = { .min = 5, .max = 11 },
  312. .p = { .min = 14, .max = 42 },
  313. .p1 = { .min = 2, .max = 6 },
  314. .p2 = { .dot_limit = 0,
  315. .p2_slow = 7, .p2_fast = 7
  316. },
  317. };
  318. static const intel_limit_t intel_limits_pineview_sdvo = {
  319. .dot = { .min = 20000, .max = 400000},
  320. .vco = { .min = 1700000, .max = 3500000 },
  321. /* Pineview's Ncounter is a ring counter */
  322. .n = { .min = 3, .max = 6 },
  323. .m = { .min = 2, .max = 256 },
  324. /* Pineview only has one combined m divider, which we treat as m2. */
  325. .m1 = { .min = 0, .max = 0 },
  326. .m2 = { .min = 0, .max = 254 },
  327. .p = { .min = 5, .max = 80 },
  328. .p1 = { .min = 1, .max = 8 },
  329. .p2 = { .dot_limit = 200000,
  330. .p2_slow = 10, .p2_fast = 5 },
  331. };
  332. static const intel_limit_t intel_limits_pineview_lvds = {
  333. .dot = { .min = 20000, .max = 400000 },
  334. .vco = { .min = 1700000, .max = 3500000 },
  335. .n = { .min = 3, .max = 6 },
  336. .m = { .min = 2, .max = 256 },
  337. .m1 = { .min = 0, .max = 0 },
  338. .m2 = { .min = 0, .max = 254 },
  339. .p = { .min = 7, .max = 112 },
  340. .p1 = { .min = 1, .max = 8 },
  341. .p2 = { .dot_limit = 112000,
  342. .p2_slow = 14, .p2_fast = 14 },
  343. };
  344. /* Ironlake / Sandybridge
  345. *
  346. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  347. * the range value for them is (actual_value - 2).
  348. */
  349. static const intel_limit_t intel_limits_ironlake_dac = {
  350. .dot = { .min = 25000, .max = 350000 },
  351. .vco = { .min = 1760000, .max = 3510000 },
  352. .n = { .min = 1, .max = 5 },
  353. .m = { .min = 79, .max = 127 },
  354. .m1 = { .min = 12, .max = 22 },
  355. .m2 = { .min = 5, .max = 9 },
  356. .p = { .min = 5, .max = 80 },
  357. .p1 = { .min = 1, .max = 8 },
  358. .p2 = { .dot_limit = 225000,
  359. .p2_slow = 10, .p2_fast = 5 },
  360. };
  361. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  362. .dot = { .min = 25000, .max = 350000 },
  363. .vco = { .min = 1760000, .max = 3510000 },
  364. .n = { .min = 1, .max = 3 },
  365. .m = { .min = 79, .max = 118 },
  366. .m1 = { .min = 12, .max = 22 },
  367. .m2 = { .min = 5, .max = 9 },
  368. .p = { .min = 28, .max = 112 },
  369. .p1 = { .min = 2, .max = 8 },
  370. .p2 = { .dot_limit = 225000,
  371. .p2_slow = 14, .p2_fast = 14 },
  372. };
  373. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  374. .dot = { .min = 25000, .max = 350000 },
  375. .vco = { .min = 1760000, .max = 3510000 },
  376. .n = { .min = 1, .max = 3 },
  377. .m = { .min = 79, .max = 127 },
  378. .m1 = { .min = 12, .max = 22 },
  379. .m2 = { .min = 5, .max = 9 },
  380. .p = { .min = 14, .max = 56 },
  381. .p1 = { .min = 2, .max = 8 },
  382. .p2 = { .dot_limit = 225000,
  383. .p2_slow = 7, .p2_fast = 7 },
  384. };
  385. /* LVDS 100mhz refclk limits. */
  386. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  387. .dot = { .min = 25000, .max = 350000 },
  388. .vco = { .min = 1760000, .max = 3510000 },
  389. .n = { .min = 1, .max = 2 },
  390. .m = { .min = 79, .max = 126 },
  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 intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  399. .dot = { .min = 25000, .max = 350000 },
  400. .vco = { .min = 1760000, .max = 3510000 },
  401. .n = { .min = 1, .max = 3 },
  402. .m = { .min = 79, .max = 126 },
  403. .m1 = { .min = 12, .max = 22 },
  404. .m2 = { .min = 5, .max = 9 },
  405. .p = { .min = 14, .max = 42 },
  406. .p1 = { .min = 2, .max = 6 },
  407. .p2 = { .dot_limit = 225000,
  408. .p2_slow = 7, .p2_fast = 7 },
  409. };
  410. static const intel_limit_t intel_limits_vlv = {
  411. /*
  412. * These are the data rate limits (measured in fast clocks)
  413. * since those are the strictest limits we have. The fast
  414. * clock and actual rate limits are more relaxed, so checking
  415. * them would make no difference.
  416. */
  417. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  418. .vco = { .min = 4000000, .max = 6000000 },
  419. .n = { .min = 1, .max = 7 },
  420. .m1 = { .min = 2, .max = 3 },
  421. .m2 = { .min = 11, .max = 156 },
  422. .p1 = { .min = 2, .max = 3 },
  423. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  424. };
  425. static const intel_limit_t intel_limits_chv = {
  426. /*
  427. * These are the data rate limits (measured in fast clocks)
  428. * since those are the strictest limits we have. The fast
  429. * clock and actual rate limits are more relaxed, so checking
  430. * them would make no difference.
  431. */
  432. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  433. .vco = { .min = 4800000, .max = 6480000 },
  434. .n = { .min = 1, .max = 1 },
  435. .m1 = { .min = 2, .max = 2 },
  436. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  437. .p1 = { .min = 2, .max = 4 },
  438. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  439. };
  440. static const intel_limit_t intel_limits_bxt = {
  441. /* FIXME: find real dot limits */
  442. .dot = { .min = 0, .max = INT_MAX },
  443. .vco = { .min = 4800000, .max = 6700000 },
  444. .n = { .min = 1, .max = 1 },
  445. .m1 = { .min = 2, .max = 2 },
  446. /* FIXME: find real m2 limits */
  447. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  448. .p1 = { .min = 2, .max = 4 },
  449. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  450. };
  451. static bool
  452. needs_modeset(struct drm_crtc_state *state)
  453. {
  454. return drm_atomic_crtc_needs_modeset(state);
  455. }
  456. /**
  457. * Returns whether any output on the specified pipe is of the specified type
  458. */
  459. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  460. {
  461. struct drm_device *dev = crtc->base.dev;
  462. struct intel_encoder *encoder;
  463. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  464. if (encoder->type == type)
  465. return true;
  466. return false;
  467. }
  468. /**
  469. * Returns whether any output on the specified pipe will have the specified
  470. * type after a staged modeset is complete, i.e., the same as
  471. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  472. * encoder->crtc.
  473. */
  474. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  475. int type)
  476. {
  477. struct drm_atomic_state *state = crtc_state->base.state;
  478. struct drm_connector *connector;
  479. struct drm_connector_state *connector_state;
  480. struct intel_encoder *encoder;
  481. int i, num_connectors = 0;
  482. for_each_connector_in_state(state, connector, connector_state, i) {
  483. if (connector_state->crtc != crtc_state->base.crtc)
  484. continue;
  485. num_connectors++;
  486. encoder = to_intel_encoder(connector_state->best_encoder);
  487. if (encoder->type == type)
  488. return true;
  489. }
  490. WARN_ON(num_connectors == 0);
  491. return false;
  492. }
  493. static const intel_limit_t *
  494. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  495. {
  496. struct drm_device *dev = crtc_state->base.crtc->dev;
  497. const intel_limit_t *limit;
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  499. if (intel_is_dual_link_lvds(dev)) {
  500. if (refclk == 100000)
  501. limit = &intel_limits_ironlake_dual_lvds_100m;
  502. else
  503. limit = &intel_limits_ironlake_dual_lvds;
  504. } else {
  505. if (refclk == 100000)
  506. limit = &intel_limits_ironlake_single_lvds_100m;
  507. else
  508. limit = &intel_limits_ironlake_single_lvds;
  509. }
  510. } else
  511. limit = &intel_limits_ironlake_dac;
  512. return limit;
  513. }
  514. static const intel_limit_t *
  515. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  516. {
  517. struct drm_device *dev = crtc_state->base.crtc->dev;
  518. const intel_limit_t *limit;
  519. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  520. if (intel_is_dual_link_lvds(dev))
  521. limit = &intel_limits_g4x_dual_channel_lvds;
  522. else
  523. limit = &intel_limits_g4x_single_channel_lvds;
  524. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  525. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  526. limit = &intel_limits_g4x_hdmi;
  527. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  528. limit = &intel_limits_g4x_sdvo;
  529. } else /* The option is for other outputs */
  530. limit = &intel_limits_i9xx_sdvo;
  531. return limit;
  532. }
  533. static const intel_limit_t *
  534. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  535. {
  536. struct drm_device *dev = crtc_state->base.crtc->dev;
  537. const intel_limit_t *limit;
  538. if (IS_BROXTON(dev))
  539. limit = &intel_limits_bxt;
  540. else if (HAS_PCH_SPLIT(dev))
  541. limit = intel_ironlake_limit(crtc_state, refclk);
  542. else if (IS_G4X(dev)) {
  543. limit = intel_g4x_limit(crtc_state);
  544. } else if (IS_PINEVIEW(dev)) {
  545. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  546. limit = &intel_limits_pineview_lvds;
  547. else
  548. limit = &intel_limits_pineview_sdvo;
  549. } else if (IS_CHERRYVIEW(dev)) {
  550. limit = &intel_limits_chv;
  551. } else if (IS_VALLEYVIEW(dev)) {
  552. limit = &intel_limits_vlv;
  553. } else if (!IS_GEN2(dev)) {
  554. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  555. limit = &intel_limits_i9xx_lvds;
  556. else
  557. limit = &intel_limits_i9xx_sdvo;
  558. } else {
  559. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  560. limit = &intel_limits_i8xx_lvds;
  561. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  562. limit = &intel_limits_i8xx_dvo;
  563. else
  564. limit = &intel_limits_i8xx_dac;
  565. }
  566. return limit;
  567. }
  568. /*
  569. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  570. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  571. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  572. * The helpers' return value is the rate of the clock that is fed to the
  573. * display engine's pipe which can be the above fast dot clock rate or a
  574. * divided-down version of it.
  575. */
  576. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  577. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  578. {
  579. clock->m = clock->m2 + 2;
  580. clock->p = clock->p1 * clock->p2;
  581. if (WARN_ON(clock->n == 0 || clock->p == 0))
  582. return 0;
  583. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  584. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  585. return clock->dot;
  586. }
  587. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  588. {
  589. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  590. }
  591. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  592. {
  593. clock->m = i9xx_dpll_compute_m(clock);
  594. clock->p = clock->p1 * clock->p2;
  595. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  596. return 0;
  597. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  598. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  599. return clock->dot;
  600. }
  601. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  602. {
  603. clock->m = clock->m1 * clock->m2;
  604. clock->p = clock->p1 * clock->p2;
  605. if (WARN_ON(clock->n == 0 || clock->p == 0))
  606. return 0;
  607. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  608. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  609. return clock->dot / 5;
  610. }
  611. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  612. {
  613. clock->m = clock->m1 * clock->m2;
  614. clock->p = clock->p1 * clock->p2;
  615. if (WARN_ON(clock->n == 0 || clock->p == 0))
  616. return 0;
  617. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  618. clock->n << 22);
  619. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  620. return clock->dot / 5;
  621. }
  622. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  623. /**
  624. * Returns whether the given set of divisors are valid for a given refclk with
  625. * the given connectors.
  626. */
  627. static bool intel_PLL_is_valid(struct drm_device *dev,
  628. const intel_limit_t *limit,
  629. const intel_clock_t *clock)
  630. {
  631. if (clock->n < limit->n.min || limit->n.max < clock->n)
  632. INTELPllInvalid("n out of range\n");
  633. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  634. INTELPllInvalid("p1 out of range\n");
  635. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  636. INTELPllInvalid("m2 out of range\n");
  637. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  638. INTELPllInvalid("m1 out of range\n");
  639. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  640. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  641. if (clock->m1 <= clock->m2)
  642. INTELPllInvalid("m1 <= m2\n");
  643. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  644. if (clock->p < limit->p.min || limit->p.max < clock->p)
  645. INTELPllInvalid("p out of range\n");
  646. if (clock->m < limit->m.min || limit->m.max < clock->m)
  647. INTELPllInvalid("m out of range\n");
  648. }
  649. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  650. INTELPllInvalid("vco out of range\n");
  651. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  652. * connector, etc., rather than just a single range.
  653. */
  654. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  655. INTELPllInvalid("dot out of range\n");
  656. return true;
  657. }
  658. static int
  659. i9xx_select_p2_div(const intel_limit_t *limit,
  660. const struct intel_crtc_state *crtc_state,
  661. int target)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  665. /*
  666. * For LVDS just rely on its current settings for dual-channel.
  667. * We haven't figured out how to reliably set up different
  668. * single/dual channel state, if we even can.
  669. */
  670. if (intel_is_dual_link_lvds(dev))
  671. return limit->p2.p2_fast;
  672. else
  673. return limit->p2.p2_slow;
  674. } else {
  675. if (target < limit->p2.dot_limit)
  676. return limit->p2.p2_slow;
  677. else
  678. return limit->p2.p2_fast;
  679. }
  680. }
  681. static bool
  682. i9xx_find_best_dpll(const intel_limit_t *limit,
  683. struct intel_crtc_state *crtc_state,
  684. int target, int refclk, intel_clock_t *match_clock,
  685. intel_clock_t *best_clock)
  686. {
  687. struct drm_device *dev = crtc_state->base.crtc->dev;
  688. intel_clock_t clock;
  689. int err = target;
  690. memset(best_clock, 0, sizeof(*best_clock));
  691. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  692. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  693. clock.m1++) {
  694. for (clock.m2 = limit->m2.min;
  695. clock.m2 <= limit->m2.max; clock.m2++) {
  696. if (clock.m2 >= clock.m1)
  697. break;
  698. for (clock.n = limit->n.min;
  699. clock.n <= limit->n.max; clock.n++) {
  700. for (clock.p1 = limit->p1.min;
  701. clock.p1 <= limit->p1.max; clock.p1++) {
  702. int this_err;
  703. i9xx_calc_dpll_params(refclk, &clock);
  704. if (!intel_PLL_is_valid(dev, limit,
  705. &clock))
  706. continue;
  707. if (match_clock &&
  708. clock.p != match_clock->p)
  709. continue;
  710. this_err = abs(clock.dot - target);
  711. if (this_err < err) {
  712. *best_clock = clock;
  713. err = this_err;
  714. }
  715. }
  716. }
  717. }
  718. }
  719. return (err != target);
  720. }
  721. static bool
  722. pnv_find_best_dpll(const intel_limit_t *limit,
  723. struct intel_crtc_state *crtc_state,
  724. int target, int refclk, intel_clock_t *match_clock,
  725. intel_clock_t *best_clock)
  726. {
  727. struct drm_device *dev = crtc_state->base.crtc->dev;
  728. intel_clock_t clock;
  729. int err = target;
  730. memset(best_clock, 0, sizeof(*best_clock));
  731. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  732. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  733. clock.m1++) {
  734. for (clock.m2 = limit->m2.min;
  735. clock.m2 <= limit->m2.max; clock.m2++) {
  736. for (clock.n = limit->n.min;
  737. clock.n <= limit->n.max; clock.n++) {
  738. for (clock.p1 = limit->p1.min;
  739. clock.p1 <= limit->p1.max; clock.p1++) {
  740. int this_err;
  741. pnv_calc_dpll_params(refclk, &clock);
  742. if (!intel_PLL_is_valid(dev, limit,
  743. &clock))
  744. continue;
  745. if (match_clock &&
  746. clock.p != match_clock->p)
  747. continue;
  748. this_err = abs(clock.dot - target);
  749. if (this_err < err) {
  750. *best_clock = clock;
  751. err = this_err;
  752. }
  753. }
  754. }
  755. }
  756. }
  757. return (err != target);
  758. }
  759. static bool
  760. g4x_find_best_dpll(const intel_limit_t *limit,
  761. struct intel_crtc_state *crtc_state,
  762. int target, int refclk, intel_clock_t *match_clock,
  763. intel_clock_t *best_clock)
  764. {
  765. struct drm_device *dev = crtc_state->base.crtc->dev;
  766. intel_clock_t clock;
  767. int max_n;
  768. bool found = false;
  769. /* approximately equals target * 0.00585 */
  770. int err_most = (target >> 8) + (target >> 9);
  771. memset(best_clock, 0, sizeof(*best_clock));
  772. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  773. max_n = limit->n.max;
  774. /* based on hardware requirement, prefer smaller n to precision */
  775. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  776. /* based on hardware requirement, prefere larger m1,m2 */
  777. for (clock.m1 = limit->m1.max;
  778. clock.m1 >= limit->m1.min; clock.m1--) {
  779. for (clock.m2 = limit->m2.max;
  780. clock.m2 >= limit->m2.min; clock.m2--) {
  781. for (clock.p1 = limit->p1.max;
  782. clock.p1 >= limit->p1.min; clock.p1--) {
  783. int this_err;
  784. i9xx_calc_dpll_params(refclk, &clock);
  785. if (!intel_PLL_is_valid(dev, limit,
  786. &clock))
  787. continue;
  788. this_err = abs(clock.dot - target);
  789. if (this_err < err_most) {
  790. *best_clock = clock;
  791. err_most = this_err;
  792. max_n = clock.n;
  793. found = true;
  794. }
  795. }
  796. }
  797. }
  798. }
  799. return found;
  800. }
  801. /*
  802. * Check if the calculated PLL configuration is more optimal compared to the
  803. * best configuration and error found so far. Return the calculated error.
  804. */
  805. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  806. const intel_clock_t *calculated_clock,
  807. const intel_clock_t *best_clock,
  808. unsigned int best_error_ppm,
  809. unsigned int *error_ppm)
  810. {
  811. /*
  812. * For CHV ignore the error and consider only the P value.
  813. * Prefer a bigger P value based on HW requirements.
  814. */
  815. if (IS_CHERRYVIEW(dev)) {
  816. *error_ppm = 0;
  817. return calculated_clock->p > best_clock->p;
  818. }
  819. if (WARN_ON_ONCE(!target_freq))
  820. return false;
  821. *error_ppm = div_u64(1000000ULL *
  822. abs(target_freq - calculated_clock->dot),
  823. target_freq);
  824. /*
  825. * Prefer a better P value over a better (smaller) error if the error
  826. * is small. Ensure this preference for future configurations too by
  827. * setting the error to 0.
  828. */
  829. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  830. *error_ppm = 0;
  831. return true;
  832. }
  833. return *error_ppm + 10 < best_error_ppm;
  834. }
  835. static bool
  836. vlv_find_best_dpll(const intel_limit_t *limit,
  837. struct intel_crtc_state *crtc_state,
  838. int target, int refclk, intel_clock_t *match_clock,
  839. intel_clock_t *best_clock)
  840. {
  841. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  842. struct drm_device *dev = crtc->base.dev;
  843. intel_clock_t clock;
  844. unsigned int bestppm = 1000000;
  845. /* min update 19.2 MHz */
  846. int max_n = min(limit->n.max, refclk / 19200);
  847. bool found = false;
  848. target *= 5; /* fast clock */
  849. memset(best_clock, 0, sizeof(*best_clock));
  850. /* based on hardware requirement, prefer smaller n to precision */
  851. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  852. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  853. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  854. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  855. clock.p = clock.p1 * clock.p2;
  856. /* based on hardware requirement, prefer bigger m1,m2 values */
  857. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  858. unsigned int ppm;
  859. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  860. refclk * clock.m1);
  861. vlv_calc_dpll_params(refclk, &clock);
  862. if (!intel_PLL_is_valid(dev, limit,
  863. &clock))
  864. continue;
  865. if (!vlv_PLL_is_optimal(dev, target,
  866. &clock,
  867. best_clock,
  868. bestppm, &ppm))
  869. continue;
  870. *best_clock = clock;
  871. bestppm = ppm;
  872. found = true;
  873. }
  874. }
  875. }
  876. }
  877. return found;
  878. }
  879. static bool
  880. chv_find_best_dpll(const intel_limit_t *limit,
  881. struct intel_crtc_state *crtc_state,
  882. int target, int refclk, intel_clock_t *match_clock,
  883. intel_clock_t *best_clock)
  884. {
  885. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  886. struct drm_device *dev = crtc->base.dev;
  887. unsigned int best_error_ppm;
  888. intel_clock_t clock;
  889. uint64_t m2;
  890. int found = false;
  891. memset(best_clock, 0, sizeof(*best_clock));
  892. best_error_ppm = 1000000;
  893. /*
  894. * Based on hardware doc, the n always set to 1, and m1 always
  895. * set to 2. If requires to support 200Mhz refclk, we need to
  896. * revisit this because n may not 1 anymore.
  897. */
  898. clock.n = 1, clock.m1 = 2;
  899. target *= 5; /* fast clock */
  900. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  901. for (clock.p2 = limit->p2.p2_fast;
  902. clock.p2 >= limit->p2.p2_slow;
  903. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  904. unsigned int error_ppm;
  905. clock.p = clock.p1 * clock.p2;
  906. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  907. clock.n) << 22, refclk * clock.m1);
  908. if (m2 > INT_MAX/clock.m1)
  909. continue;
  910. clock.m2 = m2;
  911. chv_calc_dpll_params(refclk, &clock);
  912. if (!intel_PLL_is_valid(dev, limit, &clock))
  913. continue;
  914. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  915. best_error_ppm, &error_ppm))
  916. continue;
  917. *best_clock = clock;
  918. best_error_ppm = error_ppm;
  919. found = true;
  920. }
  921. }
  922. return found;
  923. }
  924. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  925. intel_clock_t *best_clock)
  926. {
  927. int refclk = i9xx_get_refclk(crtc_state, 0);
  928. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  929. target_clock, refclk, NULL, best_clock);
  930. }
  931. bool intel_crtc_active(struct drm_crtc *crtc)
  932. {
  933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  934. /* Be paranoid as we can arrive here with only partial
  935. * state retrieved from the hardware during setup.
  936. *
  937. * We can ditch the adjusted_mode.crtc_clock check as soon
  938. * as Haswell has gained clock readout/fastboot support.
  939. *
  940. * We can ditch the crtc->primary->fb check as soon as we can
  941. * properly reconstruct framebuffers.
  942. *
  943. * FIXME: The intel_crtc->active here should be switched to
  944. * crtc->state->active once we have proper CRTC states wired up
  945. * for atomic.
  946. */
  947. return intel_crtc->active && crtc->primary->state->fb &&
  948. intel_crtc->config->base.adjusted_mode.crtc_clock;
  949. }
  950. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  951. enum pipe pipe)
  952. {
  953. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  955. return intel_crtc->config->cpu_transcoder;
  956. }
  957. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  958. {
  959. struct drm_i915_private *dev_priv = dev->dev_private;
  960. i915_reg_t reg = PIPEDSL(pipe);
  961. u32 line1, line2;
  962. u32 line_mask;
  963. if (IS_GEN2(dev))
  964. line_mask = DSL_LINEMASK_GEN2;
  965. else
  966. line_mask = DSL_LINEMASK_GEN3;
  967. line1 = I915_READ(reg) & line_mask;
  968. msleep(5);
  969. line2 = I915_READ(reg) & line_mask;
  970. return line1 == line2;
  971. }
  972. /*
  973. * intel_wait_for_pipe_off - wait for pipe to turn off
  974. * @crtc: crtc whose pipe to wait for
  975. *
  976. * After disabling a pipe, we can't wait for vblank in the usual way,
  977. * spinning on the vblank interrupt status bit, since we won't actually
  978. * see an interrupt when the pipe is disabled.
  979. *
  980. * On Gen4 and above:
  981. * wait for the pipe register state bit to turn off
  982. *
  983. * Otherwise:
  984. * wait for the display line value to settle (it usually
  985. * ends up stopping at the start of the next frame).
  986. *
  987. */
  988. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  989. {
  990. struct drm_device *dev = crtc->base.dev;
  991. struct drm_i915_private *dev_priv = dev->dev_private;
  992. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  993. enum pipe pipe = crtc->pipe;
  994. if (INTEL_INFO(dev)->gen >= 4) {
  995. i915_reg_t reg = PIPECONF(cpu_transcoder);
  996. /* Wait for the Pipe State to go off */
  997. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  998. 100))
  999. WARN(1, "pipe_off wait timed out\n");
  1000. } else {
  1001. /* Wait for the display line to settle */
  1002. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  1003. WARN(1, "pipe_off wait timed out\n");
  1004. }
  1005. }
  1006. /* Only for pre-ILK configs */
  1007. void assert_pll(struct drm_i915_private *dev_priv,
  1008. enum pipe pipe, bool state)
  1009. {
  1010. u32 val;
  1011. bool cur_state;
  1012. val = I915_READ(DPLL(pipe));
  1013. cur_state = !!(val & DPLL_VCO_ENABLE);
  1014. I915_STATE_WARN(cur_state != state,
  1015. "PLL state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1019. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1020. {
  1021. u32 val;
  1022. bool cur_state;
  1023. mutex_lock(&dev_priv->sb_lock);
  1024. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1025. mutex_unlock(&dev_priv->sb_lock);
  1026. cur_state = val & DSI_PLL_VCO_EN;
  1027. I915_STATE_WARN(cur_state != state,
  1028. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1029. onoff(state), onoff(cur_state));
  1030. }
  1031. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1032. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1033. struct intel_shared_dpll *
  1034. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1035. {
  1036. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1037. if (crtc->config->shared_dpll < 0)
  1038. return NULL;
  1039. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1040. }
  1041. /* For ILK+ */
  1042. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1043. struct intel_shared_dpll *pll,
  1044. bool state)
  1045. {
  1046. bool cur_state;
  1047. struct intel_dpll_hw_state hw_state;
  1048. if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
  1049. return;
  1050. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1051. I915_STATE_WARN(cur_state != state,
  1052. "%s assertion failure (expected %s, current %s)\n",
  1053. pll->name, onoff(state), onoff(cur_state));
  1054. }
  1055. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. bool cur_state;
  1059. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1060. pipe);
  1061. if (HAS_DDI(dev_priv->dev)) {
  1062. /* DDI does not have a specific FDI_TX register */
  1063. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1064. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1065. } else {
  1066. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1067. cur_state = !!(val & FDI_TX_ENABLE);
  1068. }
  1069. I915_STATE_WARN(cur_state != state,
  1070. "FDI TX state assertion failure (expected %s, current %s)\n",
  1071. onoff(state), onoff(cur_state));
  1072. }
  1073. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1074. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1075. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe, bool state)
  1077. {
  1078. u32 val;
  1079. bool cur_state;
  1080. val = I915_READ(FDI_RX_CTL(pipe));
  1081. cur_state = !!(val & FDI_RX_ENABLE);
  1082. I915_STATE_WARN(cur_state != state,
  1083. "FDI RX state assertion failure (expected %s, current %s)\n",
  1084. onoff(state), onoff(cur_state));
  1085. }
  1086. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1087. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1088. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1089. enum pipe pipe)
  1090. {
  1091. u32 val;
  1092. /* ILK FDI PLL is always enabled */
  1093. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1094. return;
  1095. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1096. if (HAS_DDI(dev_priv->dev))
  1097. return;
  1098. val = I915_READ(FDI_TX_CTL(pipe));
  1099. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1100. }
  1101. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1102. enum pipe pipe, bool state)
  1103. {
  1104. u32 val;
  1105. bool cur_state;
  1106. val = I915_READ(FDI_RX_CTL(pipe));
  1107. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1108. I915_STATE_WARN(cur_state != state,
  1109. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1110. onoff(state), onoff(cur_state));
  1111. }
  1112. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1113. enum pipe pipe)
  1114. {
  1115. struct drm_device *dev = dev_priv->dev;
  1116. i915_reg_t pp_reg;
  1117. u32 val;
  1118. enum pipe panel_pipe = PIPE_A;
  1119. bool locked = true;
  1120. if (WARN_ON(HAS_DDI(dev)))
  1121. return;
  1122. if (HAS_PCH_SPLIT(dev)) {
  1123. u32 port_sel;
  1124. pp_reg = PCH_PP_CONTROL;
  1125. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1126. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1127. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1128. panel_pipe = PIPE_B;
  1129. /* XXX: else fix for eDP */
  1130. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1131. /* presumably write lock depends on pipe, not port select */
  1132. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1133. panel_pipe = pipe;
  1134. } else {
  1135. pp_reg = PP_CONTROL;
  1136. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1137. panel_pipe = PIPE_B;
  1138. }
  1139. val = I915_READ(pp_reg);
  1140. if (!(val & PANEL_POWER_ON) ||
  1141. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1142. locked = false;
  1143. I915_STATE_WARN(panel_pipe == pipe && locked,
  1144. "panel assertion failure, pipe %c regs locked\n",
  1145. pipe_name(pipe));
  1146. }
  1147. static void assert_cursor(struct drm_i915_private *dev_priv,
  1148. enum pipe pipe, bool state)
  1149. {
  1150. struct drm_device *dev = dev_priv->dev;
  1151. bool cur_state;
  1152. if (IS_845G(dev) || IS_I865G(dev))
  1153. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1154. else
  1155. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1156. I915_STATE_WARN(cur_state != state,
  1157. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1158. pipe_name(pipe), onoff(state), onoff(cur_state));
  1159. }
  1160. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1161. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1162. void assert_pipe(struct drm_i915_private *dev_priv,
  1163. enum pipe pipe, bool state)
  1164. {
  1165. bool cur_state;
  1166. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1167. pipe);
  1168. /* if we need the pipe quirk it must be always on */
  1169. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1170. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1171. state = true;
  1172. if (!intel_display_power_is_enabled(dev_priv,
  1173. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1174. cur_state = false;
  1175. } else {
  1176. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1177. cur_state = !!(val & PIPECONF_ENABLE);
  1178. }
  1179. I915_STATE_WARN(cur_state != state,
  1180. "pipe %c assertion failure (expected %s, current %s)\n",
  1181. pipe_name(pipe), onoff(state), onoff(cur_state));
  1182. }
  1183. static void assert_plane(struct drm_i915_private *dev_priv,
  1184. enum plane plane, bool state)
  1185. {
  1186. u32 val;
  1187. bool cur_state;
  1188. val = I915_READ(DSPCNTR(plane));
  1189. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1190. I915_STATE_WARN(cur_state != state,
  1191. "plane %c assertion failure (expected %s, current %s)\n",
  1192. plane_name(plane), onoff(state), onoff(cur_state));
  1193. }
  1194. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1195. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1196. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe)
  1198. {
  1199. struct drm_device *dev = dev_priv->dev;
  1200. int i;
  1201. /* Primary planes are fixed to pipes on gen4+ */
  1202. if (INTEL_INFO(dev)->gen >= 4) {
  1203. u32 val = I915_READ(DSPCNTR(pipe));
  1204. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1205. "plane %c assertion failure, should be disabled but not\n",
  1206. plane_name(pipe));
  1207. return;
  1208. }
  1209. /* Need to check both planes against the pipe */
  1210. for_each_pipe(dev_priv, i) {
  1211. u32 val = I915_READ(DSPCNTR(i));
  1212. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1213. DISPPLANE_SEL_PIPE_SHIFT;
  1214. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1215. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1216. plane_name(i), pipe_name(pipe));
  1217. }
  1218. }
  1219. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1220. enum pipe pipe)
  1221. {
  1222. struct drm_device *dev = dev_priv->dev;
  1223. int sprite;
  1224. if (INTEL_INFO(dev)->gen >= 9) {
  1225. for_each_sprite(dev_priv, pipe, sprite) {
  1226. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1227. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1228. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1229. sprite, pipe_name(pipe));
  1230. }
  1231. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1232. for_each_sprite(dev_priv, pipe, sprite) {
  1233. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1234. I915_STATE_WARN(val & SP_ENABLE,
  1235. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1236. sprite_name(pipe, sprite), pipe_name(pipe));
  1237. }
  1238. } else if (INTEL_INFO(dev)->gen >= 7) {
  1239. u32 val = I915_READ(SPRCTL(pipe));
  1240. I915_STATE_WARN(val & SPRITE_ENABLE,
  1241. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1242. plane_name(pipe), pipe_name(pipe));
  1243. } else if (INTEL_INFO(dev)->gen >= 5) {
  1244. u32 val = I915_READ(DVSCNTR(pipe));
  1245. I915_STATE_WARN(val & DVS_ENABLE,
  1246. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1247. plane_name(pipe), pipe_name(pipe));
  1248. }
  1249. }
  1250. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1251. {
  1252. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1253. drm_crtc_vblank_put(crtc);
  1254. }
  1255. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1256. {
  1257. u32 val;
  1258. bool enabled;
  1259. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1260. val = I915_READ(PCH_DREF_CONTROL);
  1261. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1262. DREF_SUPERSPREAD_SOURCE_MASK));
  1263. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1264. }
  1265. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe)
  1267. {
  1268. u32 val;
  1269. bool enabled;
  1270. val = I915_READ(PCH_TRANSCONF(pipe));
  1271. enabled = !!(val & TRANS_ENABLE);
  1272. I915_STATE_WARN(enabled,
  1273. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1274. pipe_name(pipe));
  1275. }
  1276. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1277. enum pipe pipe, u32 port_sel, u32 val)
  1278. {
  1279. if ((val & DP_PORT_EN) == 0)
  1280. return false;
  1281. if (HAS_PCH_CPT(dev_priv->dev)) {
  1282. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1283. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1284. return false;
  1285. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1286. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1287. return false;
  1288. } else {
  1289. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1295. enum pipe pipe, u32 val)
  1296. {
  1297. if ((val & SDVO_ENABLE) == 0)
  1298. return false;
  1299. if (HAS_PCH_CPT(dev_priv->dev)) {
  1300. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1301. return false;
  1302. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1304. return false;
  1305. } else {
  1306. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1307. return false;
  1308. }
  1309. return true;
  1310. }
  1311. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1312. enum pipe pipe, u32 val)
  1313. {
  1314. if ((val & LVDS_PORT_EN) == 0)
  1315. return false;
  1316. if (HAS_PCH_CPT(dev_priv->dev)) {
  1317. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1318. return false;
  1319. } else {
  1320. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1321. return false;
  1322. }
  1323. return true;
  1324. }
  1325. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1326. enum pipe pipe, u32 val)
  1327. {
  1328. if ((val & ADPA_DAC_ENABLE) == 0)
  1329. return false;
  1330. if (HAS_PCH_CPT(dev_priv->dev)) {
  1331. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1332. return false;
  1333. } else {
  1334. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1335. return false;
  1336. }
  1337. return true;
  1338. }
  1339. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1340. enum pipe pipe, i915_reg_t reg,
  1341. u32 port_sel)
  1342. {
  1343. u32 val = I915_READ(reg);
  1344. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1345. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1346. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1347. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1348. && (val & DP_PIPEB_SELECT),
  1349. "IBX PCH dp port still using transcoder B\n");
  1350. }
  1351. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1352. enum pipe pipe, i915_reg_t reg)
  1353. {
  1354. u32 val = I915_READ(reg);
  1355. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1356. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1357. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1358. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1359. && (val & SDVO_PIPE_B_SELECT),
  1360. "IBX PCH hdmi port still using transcoder B\n");
  1361. }
  1362. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1363. enum pipe pipe)
  1364. {
  1365. u32 val;
  1366. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1367. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1369. val = I915_READ(PCH_ADPA);
  1370. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1371. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1372. pipe_name(pipe));
  1373. val = I915_READ(PCH_LVDS);
  1374. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1375. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1376. pipe_name(pipe));
  1377. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1378. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1380. }
  1381. static void vlv_enable_pll(struct intel_crtc *crtc,
  1382. const struct intel_crtc_state *pipe_config)
  1383. {
  1384. struct drm_device *dev = crtc->base.dev;
  1385. struct drm_i915_private *dev_priv = dev->dev_private;
  1386. i915_reg_t reg = DPLL(crtc->pipe);
  1387. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1388. assert_pipe_disabled(dev_priv, crtc->pipe);
  1389. /* PLL is protected by panel, make sure we can write it */
  1390. if (IS_MOBILE(dev_priv->dev))
  1391. assert_panel_unlocked(dev_priv, crtc->pipe);
  1392. I915_WRITE(reg, dpll);
  1393. POSTING_READ(reg);
  1394. udelay(150);
  1395. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1396. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1397. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1398. POSTING_READ(DPLL_MD(crtc->pipe));
  1399. /* We do this three times for luck */
  1400. I915_WRITE(reg, dpll);
  1401. POSTING_READ(reg);
  1402. udelay(150); /* wait for warmup */
  1403. I915_WRITE(reg, dpll);
  1404. POSTING_READ(reg);
  1405. udelay(150); /* wait for warmup */
  1406. I915_WRITE(reg, dpll);
  1407. POSTING_READ(reg);
  1408. udelay(150); /* wait for warmup */
  1409. }
  1410. static void chv_enable_pll(struct intel_crtc *crtc,
  1411. const struct intel_crtc_state *pipe_config)
  1412. {
  1413. struct drm_device *dev = crtc->base.dev;
  1414. struct drm_i915_private *dev_priv = dev->dev_private;
  1415. int pipe = crtc->pipe;
  1416. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1417. u32 tmp;
  1418. assert_pipe_disabled(dev_priv, crtc->pipe);
  1419. mutex_lock(&dev_priv->sb_lock);
  1420. /* Enable back the 10bit clock to display controller */
  1421. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1422. tmp |= DPIO_DCLKP_EN;
  1423. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1424. mutex_unlock(&dev_priv->sb_lock);
  1425. /*
  1426. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1427. */
  1428. udelay(1);
  1429. /* Enable PLL */
  1430. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1431. /* Check PLL is locked */
  1432. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1433. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1434. /* not sure when this should be written */
  1435. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1436. POSTING_READ(DPLL_MD(pipe));
  1437. }
  1438. static int intel_num_dvo_pipes(struct drm_device *dev)
  1439. {
  1440. struct intel_crtc *crtc;
  1441. int count = 0;
  1442. for_each_intel_crtc(dev, crtc)
  1443. count += crtc->base.state->active &&
  1444. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1445. return count;
  1446. }
  1447. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1448. {
  1449. struct drm_device *dev = crtc->base.dev;
  1450. struct drm_i915_private *dev_priv = dev->dev_private;
  1451. i915_reg_t reg = DPLL(crtc->pipe);
  1452. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1453. assert_pipe_disabled(dev_priv, crtc->pipe);
  1454. /* No really, not for ILK+ */
  1455. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1456. /* PLL is protected by panel, make sure we can write it */
  1457. if (IS_MOBILE(dev) && !IS_I830(dev))
  1458. assert_panel_unlocked(dev_priv, crtc->pipe);
  1459. /* Enable DVO 2x clock on both PLLs if necessary */
  1460. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1461. /*
  1462. * It appears to be important that we don't enable this
  1463. * for the current pipe before otherwise configuring the
  1464. * PLL. No idea how this should be handled if multiple
  1465. * DVO outputs are enabled simultaneosly.
  1466. */
  1467. dpll |= DPLL_DVO_2X_MODE;
  1468. I915_WRITE(DPLL(!crtc->pipe),
  1469. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1470. }
  1471. /*
  1472. * Apparently we need to have VGA mode enabled prior to changing
  1473. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1474. * dividers, even though the register value does change.
  1475. */
  1476. I915_WRITE(reg, 0);
  1477. I915_WRITE(reg, dpll);
  1478. /* Wait for the clocks to stabilize. */
  1479. POSTING_READ(reg);
  1480. udelay(150);
  1481. if (INTEL_INFO(dev)->gen >= 4) {
  1482. I915_WRITE(DPLL_MD(crtc->pipe),
  1483. crtc->config->dpll_hw_state.dpll_md);
  1484. } else {
  1485. /* The pixel multiplier can only be updated once the
  1486. * DPLL is enabled and the clocks are stable.
  1487. *
  1488. * So write it again.
  1489. */
  1490. I915_WRITE(reg, dpll);
  1491. }
  1492. /* We do this three times for luck */
  1493. I915_WRITE(reg, dpll);
  1494. POSTING_READ(reg);
  1495. udelay(150); /* wait for warmup */
  1496. I915_WRITE(reg, dpll);
  1497. POSTING_READ(reg);
  1498. udelay(150); /* wait for warmup */
  1499. I915_WRITE(reg, dpll);
  1500. POSTING_READ(reg);
  1501. udelay(150); /* wait for warmup */
  1502. }
  1503. /**
  1504. * i9xx_disable_pll - disable a PLL
  1505. * @dev_priv: i915 private structure
  1506. * @pipe: pipe PLL to disable
  1507. *
  1508. * Disable the PLL for @pipe, making sure the pipe is off first.
  1509. *
  1510. * Note! This is for pre-ILK only.
  1511. */
  1512. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1513. {
  1514. struct drm_device *dev = crtc->base.dev;
  1515. struct drm_i915_private *dev_priv = dev->dev_private;
  1516. enum pipe pipe = crtc->pipe;
  1517. /* Disable DVO 2x clock on both PLLs if necessary */
  1518. if (IS_I830(dev) &&
  1519. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1520. !intel_num_dvo_pipes(dev)) {
  1521. I915_WRITE(DPLL(PIPE_B),
  1522. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1523. I915_WRITE(DPLL(PIPE_A),
  1524. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1525. }
  1526. /* Don't disable pipe or pipe PLLs if needed */
  1527. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1528. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1529. return;
  1530. /* Make sure the pipe isn't still relying on us */
  1531. assert_pipe_disabled(dev_priv, pipe);
  1532. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1533. POSTING_READ(DPLL(pipe));
  1534. }
  1535. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1536. {
  1537. u32 val;
  1538. /* Make sure the pipe isn't still relying on us */
  1539. assert_pipe_disabled(dev_priv, pipe);
  1540. /*
  1541. * Leave integrated clock source and reference clock enabled for pipe B.
  1542. * The latter is needed for VGA hotplug / manual detection.
  1543. */
  1544. val = DPLL_VGA_MODE_DIS;
  1545. if (pipe == PIPE_B)
  1546. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1547. I915_WRITE(DPLL(pipe), val);
  1548. POSTING_READ(DPLL(pipe));
  1549. }
  1550. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1551. {
  1552. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1553. u32 val;
  1554. /* Make sure the pipe isn't still relying on us */
  1555. assert_pipe_disabled(dev_priv, pipe);
  1556. /* Set PLL en = 0 */
  1557. val = DPLL_SSC_REF_CLK_CHV |
  1558. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1559. if (pipe != PIPE_A)
  1560. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1561. I915_WRITE(DPLL(pipe), val);
  1562. POSTING_READ(DPLL(pipe));
  1563. mutex_lock(&dev_priv->sb_lock);
  1564. /* Disable 10bit clock to display controller */
  1565. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1566. val &= ~DPIO_DCLKP_EN;
  1567. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1568. mutex_unlock(&dev_priv->sb_lock);
  1569. }
  1570. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1571. struct intel_digital_port *dport,
  1572. unsigned int expected_mask)
  1573. {
  1574. u32 port_mask;
  1575. i915_reg_t dpll_reg;
  1576. switch (dport->port) {
  1577. case PORT_B:
  1578. port_mask = DPLL_PORTB_READY_MASK;
  1579. dpll_reg = DPLL(0);
  1580. break;
  1581. case PORT_C:
  1582. port_mask = DPLL_PORTC_READY_MASK;
  1583. dpll_reg = DPLL(0);
  1584. expected_mask <<= 4;
  1585. break;
  1586. case PORT_D:
  1587. port_mask = DPLL_PORTD_READY_MASK;
  1588. dpll_reg = DPIO_PHY_STATUS;
  1589. break;
  1590. default:
  1591. BUG();
  1592. }
  1593. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1594. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1595. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1596. }
  1597. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1598. {
  1599. struct drm_device *dev = crtc->base.dev;
  1600. struct drm_i915_private *dev_priv = dev->dev_private;
  1601. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1602. if (WARN_ON(pll == NULL))
  1603. return;
  1604. WARN_ON(!pll->config.crtc_mask);
  1605. if (pll->active == 0) {
  1606. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1607. WARN_ON(pll->on);
  1608. assert_shared_dpll_disabled(dev_priv, pll);
  1609. pll->mode_set(dev_priv, pll);
  1610. }
  1611. }
  1612. /**
  1613. * intel_enable_shared_dpll - enable PCH PLL
  1614. * @dev_priv: i915 private structure
  1615. * @pipe: pipe PLL to enable
  1616. *
  1617. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1618. * drives the transcoder clock.
  1619. */
  1620. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1621. {
  1622. struct drm_device *dev = crtc->base.dev;
  1623. struct drm_i915_private *dev_priv = dev->dev_private;
  1624. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1625. if (WARN_ON(pll == NULL))
  1626. return;
  1627. if (WARN_ON(pll->config.crtc_mask == 0))
  1628. return;
  1629. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1630. pll->name, pll->active, pll->on,
  1631. crtc->base.base.id);
  1632. if (pll->active++) {
  1633. WARN_ON(!pll->on);
  1634. assert_shared_dpll_enabled(dev_priv, pll);
  1635. return;
  1636. }
  1637. WARN_ON(pll->on);
  1638. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1639. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1640. pll->enable(dev_priv, pll);
  1641. pll->on = true;
  1642. }
  1643. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1644. {
  1645. struct drm_device *dev = crtc->base.dev;
  1646. struct drm_i915_private *dev_priv = dev->dev_private;
  1647. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1648. /* PCH only available on ILK+ */
  1649. if (INTEL_INFO(dev)->gen < 5)
  1650. return;
  1651. if (pll == NULL)
  1652. return;
  1653. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1654. return;
  1655. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1656. pll->name, pll->active, pll->on,
  1657. crtc->base.base.id);
  1658. if (WARN_ON(pll->active == 0)) {
  1659. assert_shared_dpll_disabled(dev_priv, pll);
  1660. return;
  1661. }
  1662. assert_shared_dpll_enabled(dev_priv, pll);
  1663. WARN_ON(!pll->on);
  1664. if (--pll->active)
  1665. return;
  1666. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1667. pll->disable(dev_priv, pll);
  1668. pll->on = false;
  1669. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1670. }
  1671. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1672. enum pipe pipe)
  1673. {
  1674. struct drm_device *dev = dev_priv->dev;
  1675. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1677. i915_reg_t reg;
  1678. uint32_t val, pipeconf_val;
  1679. /* PCH only available on ILK+ */
  1680. BUG_ON(!HAS_PCH_SPLIT(dev));
  1681. /* Make sure PCH DPLL is enabled */
  1682. assert_shared_dpll_enabled(dev_priv,
  1683. intel_crtc_to_shared_dpll(intel_crtc));
  1684. /* FDI must be feeding us bits for PCH ports */
  1685. assert_fdi_tx_enabled(dev_priv, pipe);
  1686. assert_fdi_rx_enabled(dev_priv, pipe);
  1687. if (HAS_PCH_CPT(dev)) {
  1688. /* Workaround: Set the timing override bit before enabling the
  1689. * pch transcoder. */
  1690. reg = TRANS_CHICKEN2(pipe);
  1691. val = I915_READ(reg);
  1692. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1693. I915_WRITE(reg, val);
  1694. }
  1695. reg = PCH_TRANSCONF(pipe);
  1696. val = I915_READ(reg);
  1697. pipeconf_val = I915_READ(PIPECONF(pipe));
  1698. if (HAS_PCH_IBX(dev_priv->dev)) {
  1699. /*
  1700. * Make the BPC in transcoder be consistent with
  1701. * that in pipeconf reg. For HDMI we must use 8bpc
  1702. * here for both 8bpc and 12bpc.
  1703. */
  1704. val &= ~PIPECONF_BPC_MASK;
  1705. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1706. val |= PIPECONF_8BPC;
  1707. else
  1708. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1709. }
  1710. val &= ~TRANS_INTERLACE_MASK;
  1711. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1712. if (HAS_PCH_IBX(dev_priv->dev) &&
  1713. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1714. val |= TRANS_LEGACY_INTERLACED_ILK;
  1715. else
  1716. val |= TRANS_INTERLACED;
  1717. else
  1718. val |= TRANS_PROGRESSIVE;
  1719. I915_WRITE(reg, val | TRANS_ENABLE);
  1720. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1721. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1722. }
  1723. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1724. enum transcoder cpu_transcoder)
  1725. {
  1726. u32 val, pipeconf_val;
  1727. /* PCH only available on ILK+ */
  1728. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1729. /* FDI must be feeding us bits for PCH ports */
  1730. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1731. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1732. /* Workaround: set timing override bit. */
  1733. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1734. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1735. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1736. val = TRANS_ENABLE;
  1737. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1738. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1739. PIPECONF_INTERLACED_ILK)
  1740. val |= TRANS_INTERLACED;
  1741. else
  1742. val |= TRANS_PROGRESSIVE;
  1743. I915_WRITE(LPT_TRANSCONF, val);
  1744. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1745. DRM_ERROR("Failed to enable PCH transcoder\n");
  1746. }
  1747. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1748. enum pipe pipe)
  1749. {
  1750. struct drm_device *dev = dev_priv->dev;
  1751. i915_reg_t reg;
  1752. uint32_t val;
  1753. /* FDI relies on the transcoder */
  1754. assert_fdi_tx_disabled(dev_priv, pipe);
  1755. assert_fdi_rx_disabled(dev_priv, pipe);
  1756. /* Ports must be off as well */
  1757. assert_pch_ports_disabled(dev_priv, pipe);
  1758. reg = PCH_TRANSCONF(pipe);
  1759. val = I915_READ(reg);
  1760. val &= ~TRANS_ENABLE;
  1761. I915_WRITE(reg, val);
  1762. /* wait for PCH transcoder off, transcoder state */
  1763. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1764. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1765. if (HAS_PCH_CPT(dev)) {
  1766. /* Workaround: Clear the timing override chicken bit again. */
  1767. reg = TRANS_CHICKEN2(pipe);
  1768. val = I915_READ(reg);
  1769. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1770. I915_WRITE(reg, val);
  1771. }
  1772. }
  1773. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1774. {
  1775. u32 val;
  1776. val = I915_READ(LPT_TRANSCONF);
  1777. val &= ~TRANS_ENABLE;
  1778. I915_WRITE(LPT_TRANSCONF, val);
  1779. /* wait for PCH transcoder off, transcoder state */
  1780. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1781. DRM_ERROR("Failed to disable PCH transcoder\n");
  1782. /* Workaround: clear timing override bit. */
  1783. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1784. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1785. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1786. }
  1787. /**
  1788. * intel_enable_pipe - enable a pipe, asserting requirements
  1789. * @crtc: crtc responsible for the pipe
  1790. *
  1791. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1792. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1793. */
  1794. static void intel_enable_pipe(struct intel_crtc *crtc)
  1795. {
  1796. struct drm_device *dev = crtc->base.dev;
  1797. struct drm_i915_private *dev_priv = dev->dev_private;
  1798. enum pipe pipe = crtc->pipe;
  1799. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1800. enum pipe pch_transcoder;
  1801. i915_reg_t reg;
  1802. u32 val;
  1803. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1804. assert_planes_disabled(dev_priv, pipe);
  1805. assert_cursor_disabled(dev_priv, pipe);
  1806. assert_sprites_disabled(dev_priv, pipe);
  1807. if (HAS_PCH_LPT(dev_priv->dev))
  1808. pch_transcoder = TRANSCODER_A;
  1809. else
  1810. pch_transcoder = pipe;
  1811. /*
  1812. * A pipe without a PLL won't actually be able to drive bits from
  1813. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1814. * need the check.
  1815. */
  1816. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1817. if (crtc->config->has_dsi_encoder)
  1818. assert_dsi_pll_enabled(dev_priv);
  1819. else
  1820. assert_pll_enabled(dev_priv, pipe);
  1821. else {
  1822. if (crtc->config->has_pch_encoder) {
  1823. /* if driving the PCH, we need FDI enabled */
  1824. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1825. assert_fdi_tx_pll_enabled(dev_priv,
  1826. (enum pipe) cpu_transcoder);
  1827. }
  1828. /* FIXME: assert CPU port conditions for SNB+ */
  1829. }
  1830. reg = PIPECONF(cpu_transcoder);
  1831. val = I915_READ(reg);
  1832. if (val & PIPECONF_ENABLE) {
  1833. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1834. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1835. return;
  1836. }
  1837. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1838. POSTING_READ(reg);
  1839. /*
  1840. * Until the pipe starts DSL will read as 0, which would cause
  1841. * an apparent vblank timestamp jump, which messes up also the
  1842. * frame count when it's derived from the timestamps. So let's
  1843. * wait for the pipe to start properly before we call
  1844. * drm_crtc_vblank_on()
  1845. */
  1846. if (dev->max_vblank_count == 0 &&
  1847. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1848. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1849. }
  1850. /**
  1851. * intel_disable_pipe - disable a pipe, asserting requirements
  1852. * @crtc: crtc whose pipes is to be disabled
  1853. *
  1854. * Disable the pipe of @crtc, making sure that various hardware
  1855. * specific requirements are met, if applicable, e.g. plane
  1856. * disabled, panel fitter off, etc.
  1857. *
  1858. * Will wait until the pipe has shut down before returning.
  1859. */
  1860. static void intel_disable_pipe(struct intel_crtc *crtc)
  1861. {
  1862. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1863. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1864. enum pipe pipe = crtc->pipe;
  1865. i915_reg_t reg;
  1866. u32 val;
  1867. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1868. /*
  1869. * Make sure planes won't keep trying to pump pixels to us,
  1870. * or we might hang the display.
  1871. */
  1872. assert_planes_disabled(dev_priv, pipe);
  1873. assert_cursor_disabled(dev_priv, pipe);
  1874. assert_sprites_disabled(dev_priv, pipe);
  1875. reg = PIPECONF(cpu_transcoder);
  1876. val = I915_READ(reg);
  1877. if ((val & PIPECONF_ENABLE) == 0)
  1878. return;
  1879. /*
  1880. * Double wide has implications for planes
  1881. * so best keep it disabled when not needed.
  1882. */
  1883. if (crtc->config->double_wide)
  1884. val &= ~PIPECONF_DOUBLE_WIDE;
  1885. /* Don't disable pipe or pipe PLLs if needed */
  1886. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1887. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1888. val &= ~PIPECONF_ENABLE;
  1889. I915_WRITE(reg, val);
  1890. if ((val & PIPECONF_ENABLE) == 0)
  1891. intel_wait_for_pipe_off(crtc);
  1892. }
  1893. static bool need_vtd_wa(struct drm_device *dev)
  1894. {
  1895. #ifdef CONFIG_INTEL_IOMMU
  1896. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1897. return true;
  1898. #endif
  1899. return false;
  1900. }
  1901. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1902. {
  1903. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1904. }
  1905. static unsigned int intel_tile_width(const struct drm_i915_private *dev_priv,
  1906. uint64_t fb_modifier, unsigned int cpp)
  1907. {
  1908. switch (fb_modifier) {
  1909. case DRM_FORMAT_MOD_NONE:
  1910. return cpp;
  1911. case I915_FORMAT_MOD_X_TILED:
  1912. if (IS_GEN2(dev_priv))
  1913. return 128;
  1914. else
  1915. return 512;
  1916. case I915_FORMAT_MOD_Y_TILED:
  1917. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1918. return 128;
  1919. else
  1920. return 512;
  1921. case I915_FORMAT_MOD_Yf_TILED:
  1922. switch (cpp) {
  1923. case 1:
  1924. return 64;
  1925. case 2:
  1926. case 4:
  1927. return 128;
  1928. case 8:
  1929. case 16:
  1930. return 256;
  1931. default:
  1932. MISSING_CASE(cpp);
  1933. return cpp;
  1934. }
  1935. break;
  1936. default:
  1937. MISSING_CASE(fb_modifier);
  1938. return cpp;
  1939. }
  1940. }
  1941. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1942. uint64_t fb_modifier, unsigned int cpp)
  1943. {
  1944. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1945. return 1;
  1946. else
  1947. return intel_tile_size(dev_priv) /
  1948. intel_tile_width(dev_priv, fb_modifier, cpp);
  1949. }
  1950. unsigned int
  1951. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1952. uint32_t pixel_format, uint64_t fb_modifier)
  1953. {
  1954. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1955. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1956. return ALIGN(height, tile_height);
  1957. }
  1958. static void
  1959. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1960. const struct drm_plane_state *plane_state)
  1961. {
  1962. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1963. struct intel_rotation_info *info = &view->params.rotated;
  1964. unsigned int tile_size, tile_width, tile_height, cpp;
  1965. *view = i915_ggtt_view_normal;
  1966. if (!plane_state)
  1967. return;
  1968. if (!intel_rotation_90_or_270(plane_state->rotation))
  1969. return;
  1970. *view = i915_ggtt_view_rotated;
  1971. info->height = fb->height;
  1972. info->pixel_format = fb->pixel_format;
  1973. info->pitch = fb->pitches[0];
  1974. info->uv_offset = fb->offsets[1];
  1975. info->fb_modifier = fb->modifier[0];
  1976. tile_size = intel_tile_size(dev_priv);
  1977. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1978. tile_width = intel_tile_width(dev_priv, fb->modifier[0], cpp);
  1979. tile_height = tile_size / tile_width;
  1980. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_width);
  1981. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1982. info->size = info->width_pages * info->height_pages * tile_size;
  1983. if (info->pixel_format == DRM_FORMAT_NV12) {
  1984. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1985. tile_width = intel_tile_width(dev_priv, fb->modifier[1], cpp);
  1986. tile_height = tile_size / tile_width;
  1987. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[1], tile_width);
  1988. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2, tile_height);
  1989. info->size_uv = info->width_pages_uv * info->height_pages_uv * tile_size;
  1990. }
  1991. }
  1992. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1993. {
  1994. if (INTEL_INFO(dev_priv)->gen >= 9)
  1995. return 256 * 1024;
  1996. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1997. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1998. return 128 * 1024;
  1999. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2000. return 4 * 1024;
  2001. else
  2002. return 0;
  2003. }
  2004. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  2005. uint64_t fb_modifier)
  2006. {
  2007. switch (fb_modifier) {
  2008. case DRM_FORMAT_MOD_NONE:
  2009. return intel_linear_alignment(dev_priv);
  2010. case I915_FORMAT_MOD_X_TILED:
  2011. if (INTEL_INFO(dev_priv)->gen >= 9)
  2012. return 256 * 1024;
  2013. return 0;
  2014. case I915_FORMAT_MOD_Y_TILED:
  2015. case I915_FORMAT_MOD_Yf_TILED:
  2016. return 1 * 1024 * 1024;
  2017. default:
  2018. MISSING_CASE(fb_modifier);
  2019. return 0;
  2020. }
  2021. }
  2022. int
  2023. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2024. struct drm_framebuffer *fb,
  2025. const struct drm_plane_state *plane_state)
  2026. {
  2027. struct drm_device *dev = fb->dev;
  2028. struct drm_i915_private *dev_priv = dev->dev_private;
  2029. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2030. struct i915_ggtt_view view;
  2031. u32 alignment;
  2032. int ret;
  2033. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2034. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2035. intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2036. /* Note that the w/a also requires 64 PTE of padding following the
  2037. * bo. We currently fill all unused PTE with the shadow page and so
  2038. * we should always have valid PTE following the scanout preventing
  2039. * the VT-d warning.
  2040. */
  2041. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2042. alignment = 256 * 1024;
  2043. /*
  2044. * Global gtt pte registers are special registers which actually forward
  2045. * writes to a chunk of system memory. Which means that there is no risk
  2046. * that the register values disappear as soon as we call
  2047. * intel_runtime_pm_put(), so it is correct to wrap only the
  2048. * pin/unpin/fence and not more.
  2049. */
  2050. intel_runtime_pm_get(dev_priv);
  2051. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  2052. &view);
  2053. if (ret)
  2054. goto err_pm;
  2055. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2056. * fence, whereas 965+ only requires a fence if using
  2057. * framebuffer compression. For simplicity, we always install
  2058. * a fence as the cost is not that onerous.
  2059. */
  2060. if (view.type == I915_GGTT_VIEW_NORMAL) {
  2061. ret = i915_gem_object_get_fence(obj);
  2062. if (ret == -EDEADLK) {
  2063. /*
  2064. * -EDEADLK means there are no free fences
  2065. * no pending flips.
  2066. *
  2067. * This is propagated to atomic, but it uses
  2068. * -EDEADLK to force a locking recovery, so
  2069. * change the returned error to -EBUSY.
  2070. */
  2071. ret = -EBUSY;
  2072. goto err_unpin;
  2073. } else if (ret)
  2074. goto err_unpin;
  2075. i915_gem_object_pin_fence(obj);
  2076. }
  2077. intel_runtime_pm_put(dev_priv);
  2078. return 0;
  2079. err_unpin:
  2080. i915_gem_object_unpin_from_display_plane(obj, &view);
  2081. err_pm:
  2082. intel_runtime_pm_put(dev_priv);
  2083. return ret;
  2084. }
  2085. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2086. const struct drm_plane_state *plane_state)
  2087. {
  2088. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2089. struct i915_ggtt_view view;
  2090. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2091. intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2092. if (view.type == I915_GGTT_VIEW_NORMAL)
  2093. i915_gem_object_unpin_fence(obj);
  2094. i915_gem_object_unpin_from_display_plane(obj, &view);
  2095. }
  2096. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2097. * is assumed to be a power-of-two. */
  2098. u32 intel_compute_tile_offset(struct drm_i915_private *dev_priv,
  2099. int *x, int *y,
  2100. uint64_t fb_modifier,
  2101. unsigned int cpp,
  2102. unsigned int pitch)
  2103. {
  2104. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2105. unsigned int tile_size, tile_width, tile_height;
  2106. unsigned int tile_rows, tiles;
  2107. tile_size = intel_tile_size(dev_priv);
  2108. tile_width = intel_tile_width(dev_priv, fb_modifier, cpp);
  2109. tile_height = tile_size / tile_width;
  2110. tile_rows = *y / tile_height;
  2111. *y %= tile_height;
  2112. tiles = *x / (tile_width/cpp);
  2113. *x %= tile_width/cpp;
  2114. return tile_rows * pitch * tile_height + tiles * tile_size;
  2115. } else {
  2116. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2117. unsigned int offset;
  2118. offset = *y * pitch + *x * cpp;
  2119. *y = (offset & alignment) / pitch;
  2120. *x = ((offset & alignment) - *y * pitch) / cpp;
  2121. return offset & ~alignment;
  2122. }
  2123. }
  2124. static int i9xx_format_to_fourcc(int format)
  2125. {
  2126. switch (format) {
  2127. case DISPPLANE_8BPP:
  2128. return DRM_FORMAT_C8;
  2129. case DISPPLANE_BGRX555:
  2130. return DRM_FORMAT_XRGB1555;
  2131. case DISPPLANE_BGRX565:
  2132. return DRM_FORMAT_RGB565;
  2133. default:
  2134. case DISPPLANE_BGRX888:
  2135. return DRM_FORMAT_XRGB8888;
  2136. case DISPPLANE_RGBX888:
  2137. return DRM_FORMAT_XBGR8888;
  2138. case DISPPLANE_BGRX101010:
  2139. return DRM_FORMAT_XRGB2101010;
  2140. case DISPPLANE_RGBX101010:
  2141. return DRM_FORMAT_XBGR2101010;
  2142. }
  2143. }
  2144. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2145. {
  2146. switch (format) {
  2147. case PLANE_CTL_FORMAT_RGB_565:
  2148. return DRM_FORMAT_RGB565;
  2149. default:
  2150. case PLANE_CTL_FORMAT_XRGB_8888:
  2151. if (rgb_order) {
  2152. if (alpha)
  2153. return DRM_FORMAT_ABGR8888;
  2154. else
  2155. return DRM_FORMAT_XBGR8888;
  2156. } else {
  2157. if (alpha)
  2158. return DRM_FORMAT_ARGB8888;
  2159. else
  2160. return DRM_FORMAT_XRGB8888;
  2161. }
  2162. case PLANE_CTL_FORMAT_XRGB_2101010:
  2163. if (rgb_order)
  2164. return DRM_FORMAT_XBGR2101010;
  2165. else
  2166. return DRM_FORMAT_XRGB2101010;
  2167. }
  2168. }
  2169. static bool
  2170. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2171. struct intel_initial_plane_config *plane_config)
  2172. {
  2173. struct drm_device *dev = crtc->base.dev;
  2174. struct drm_i915_private *dev_priv = to_i915(dev);
  2175. struct drm_i915_gem_object *obj = NULL;
  2176. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2177. struct drm_framebuffer *fb = &plane_config->fb->base;
  2178. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2179. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2180. PAGE_SIZE);
  2181. size_aligned -= base_aligned;
  2182. if (plane_config->size == 0)
  2183. return false;
  2184. /* If the FB is too big, just don't use it since fbdev is not very
  2185. * important and we should probably use that space with FBC or other
  2186. * features. */
  2187. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2188. return false;
  2189. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2190. base_aligned,
  2191. base_aligned,
  2192. size_aligned);
  2193. if (!obj)
  2194. return false;
  2195. obj->tiling_mode = plane_config->tiling;
  2196. if (obj->tiling_mode == I915_TILING_X)
  2197. obj->stride = fb->pitches[0];
  2198. mode_cmd.pixel_format = fb->pixel_format;
  2199. mode_cmd.width = fb->width;
  2200. mode_cmd.height = fb->height;
  2201. mode_cmd.pitches[0] = fb->pitches[0];
  2202. mode_cmd.modifier[0] = fb->modifier[0];
  2203. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2204. mutex_lock(&dev->struct_mutex);
  2205. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2206. &mode_cmd, obj)) {
  2207. DRM_DEBUG_KMS("intel fb init failed\n");
  2208. goto out_unref_obj;
  2209. }
  2210. mutex_unlock(&dev->struct_mutex);
  2211. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2212. return true;
  2213. out_unref_obj:
  2214. drm_gem_object_unreference(&obj->base);
  2215. mutex_unlock(&dev->struct_mutex);
  2216. return false;
  2217. }
  2218. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2219. static void
  2220. update_state_fb(struct drm_plane *plane)
  2221. {
  2222. if (plane->fb == plane->state->fb)
  2223. return;
  2224. if (plane->state->fb)
  2225. drm_framebuffer_unreference(plane->state->fb);
  2226. plane->state->fb = plane->fb;
  2227. if (plane->state->fb)
  2228. drm_framebuffer_reference(plane->state->fb);
  2229. }
  2230. static void
  2231. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2232. struct intel_initial_plane_config *plane_config)
  2233. {
  2234. struct drm_device *dev = intel_crtc->base.dev;
  2235. struct drm_i915_private *dev_priv = dev->dev_private;
  2236. struct drm_crtc *c;
  2237. struct intel_crtc *i;
  2238. struct drm_i915_gem_object *obj;
  2239. struct drm_plane *primary = intel_crtc->base.primary;
  2240. struct drm_plane_state *plane_state = primary->state;
  2241. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2242. struct intel_plane *intel_plane = to_intel_plane(primary);
  2243. struct intel_plane_state *intel_state =
  2244. to_intel_plane_state(plane_state);
  2245. struct drm_framebuffer *fb;
  2246. if (!plane_config->fb)
  2247. return;
  2248. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2249. fb = &plane_config->fb->base;
  2250. goto valid_fb;
  2251. }
  2252. kfree(plane_config->fb);
  2253. /*
  2254. * Failed to alloc the obj, check to see if we should share
  2255. * an fb with another CRTC instead
  2256. */
  2257. for_each_crtc(dev, c) {
  2258. i = to_intel_crtc(c);
  2259. if (c == &intel_crtc->base)
  2260. continue;
  2261. if (!i->active)
  2262. continue;
  2263. fb = c->primary->fb;
  2264. if (!fb)
  2265. continue;
  2266. obj = intel_fb_obj(fb);
  2267. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2268. drm_framebuffer_reference(fb);
  2269. goto valid_fb;
  2270. }
  2271. }
  2272. /*
  2273. * We've failed to reconstruct the BIOS FB. Current display state
  2274. * indicates that the primary plane is visible, but has a NULL FB,
  2275. * which will lead to problems later if we don't fix it up. The
  2276. * simplest solution is to just disable the primary plane now and
  2277. * pretend the BIOS never had it enabled.
  2278. */
  2279. to_intel_plane_state(plane_state)->visible = false;
  2280. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2281. intel_pre_disable_primary(&intel_crtc->base);
  2282. intel_plane->disable_plane(primary, &intel_crtc->base);
  2283. return;
  2284. valid_fb:
  2285. plane_state->src_x = 0;
  2286. plane_state->src_y = 0;
  2287. plane_state->src_w = fb->width << 16;
  2288. plane_state->src_h = fb->height << 16;
  2289. plane_state->crtc_x = 0;
  2290. plane_state->crtc_y = 0;
  2291. plane_state->crtc_w = fb->width;
  2292. plane_state->crtc_h = fb->height;
  2293. intel_state->src.x1 = plane_state->src_x;
  2294. intel_state->src.y1 = plane_state->src_y;
  2295. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2296. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2297. intel_state->dst.x1 = plane_state->crtc_x;
  2298. intel_state->dst.y1 = plane_state->crtc_y;
  2299. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2300. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2301. obj = intel_fb_obj(fb);
  2302. if (obj->tiling_mode != I915_TILING_NONE)
  2303. dev_priv->preserve_bios_swizzle = true;
  2304. drm_framebuffer_reference(fb);
  2305. primary->fb = primary->state->fb = fb;
  2306. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2307. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2308. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2309. }
  2310. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2311. const struct intel_crtc_state *crtc_state,
  2312. const struct intel_plane_state *plane_state)
  2313. {
  2314. struct drm_device *dev = primary->dev;
  2315. struct drm_i915_private *dev_priv = dev->dev_private;
  2316. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2317. struct drm_framebuffer *fb = plane_state->base.fb;
  2318. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2319. int plane = intel_crtc->plane;
  2320. u32 linear_offset;
  2321. u32 dspcntr;
  2322. i915_reg_t reg = DSPCNTR(plane);
  2323. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2324. int x = plane_state->src.x1 >> 16;
  2325. int y = plane_state->src.y1 >> 16;
  2326. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2327. dspcntr |= DISPLAY_PLANE_ENABLE;
  2328. if (INTEL_INFO(dev)->gen < 4) {
  2329. if (intel_crtc->pipe == PIPE_B)
  2330. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2331. /* pipesrc and dspsize control the size that is scaled from,
  2332. * which should always be the user's requested size.
  2333. */
  2334. I915_WRITE(DSPSIZE(plane),
  2335. ((crtc_state->pipe_src_h - 1) << 16) |
  2336. (crtc_state->pipe_src_w - 1));
  2337. I915_WRITE(DSPPOS(plane), 0);
  2338. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2339. I915_WRITE(PRIMSIZE(plane),
  2340. ((crtc_state->pipe_src_h - 1) << 16) |
  2341. (crtc_state->pipe_src_w - 1));
  2342. I915_WRITE(PRIMPOS(plane), 0);
  2343. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2344. }
  2345. switch (fb->pixel_format) {
  2346. case DRM_FORMAT_C8:
  2347. dspcntr |= DISPPLANE_8BPP;
  2348. break;
  2349. case DRM_FORMAT_XRGB1555:
  2350. dspcntr |= DISPPLANE_BGRX555;
  2351. break;
  2352. case DRM_FORMAT_RGB565:
  2353. dspcntr |= DISPPLANE_BGRX565;
  2354. break;
  2355. case DRM_FORMAT_XRGB8888:
  2356. dspcntr |= DISPPLANE_BGRX888;
  2357. break;
  2358. case DRM_FORMAT_XBGR8888:
  2359. dspcntr |= DISPPLANE_RGBX888;
  2360. break;
  2361. case DRM_FORMAT_XRGB2101010:
  2362. dspcntr |= DISPPLANE_BGRX101010;
  2363. break;
  2364. case DRM_FORMAT_XBGR2101010:
  2365. dspcntr |= DISPPLANE_RGBX101010;
  2366. break;
  2367. default:
  2368. BUG();
  2369. }
  2370. if (INTEL_INFO(dev)->gen >= 4 &&
  2371. obj->tiling_mode != I915_TILING_NONE)
  2372. dspcntr |= DISPPLANE_TILED;
  2373. if (IS_G4X(dev))
  2374. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2375. linear_offset = y * fb->pitches[0] + x * cpp;
  2376. if (INTEL_INFO(dev)->gen >= 4) {
  2377. intel_crtc->dspaddr_offset =
  2378. intel_compute_tile_offset(dev_priv, &x, &y,
  2379. fb->modifier[0], cpp,
  2380. fb->pitches[0]);
  2381. linear_offset -= intel_crtc->dspaddr_offset;
  2382. } else {
  2383. intel_crtc->dspaddr_offset = linear_offset;
  2384. }
  2385. if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  2386. dspcntr |= DISPPLANE_ROTATE_180;
  2387. x += (crtc_state->pipe_src_w - 1);
  2388. y += (crtc_state->pipe_src_h - 1);
  2389. /* Finding the last pixel of the last line of the display
  2390. data and adding to linear_offset*/
  2391. linear_offset +=
  2392. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2393. (crtc_state->pipe_src_w - 1) * cpp;
  2394. }
  2395. intel_crtc->adjusted_x = x;
  2396. intel_crtc->adjusted_y = y;
  2397. I915_WRITE(reg, dspcntr);
  2398. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2399. if (INTEL_INFO(dev)->gen >= 4) {
  2400. I915_WRITE(DSPSURF(plane),
  2401. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2402. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2403. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2404. } else
  2405. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2406. POSTING_READ(reg);
  2407. }
  2408. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2409. struct drm_crtc *crtc)
  2410. {
  2411. struct drm_device *dev = crtc->dev;
  2412. struct drm_i915_private *dev_priv = dev->dev_private;
  2413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2414. int plane = intel_crtc->plane;
  2415. I915_WRITE(DSPCNTR(plane), 0);
  2416. if (INTEL_INFO(dev_priv)->gen >= 4)
  2417. I915_WRITE(DSPSURF(plane), 0);
  2418. else
  2419. I915_WRITE(DSPADDR(plane), 0);
  2420. POSTING_READ(DSPCNTR(plane));
  2421. }
  2422. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2423. const struct intel_crtc_state *crtc_state,
  2424. const struct intel_plane_state *plane_state)
  2425. {
  2426. struct drm_device *dev = primary->dev;
  2427. struct drm_i915_private *dev_priv = dev->dev_private;
  2428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2429. struct drm_framebuffer *fb = plane_state->base.fb;
  2430. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2431. int plane = intel_crtc->plane;
  2432. u32 linear_offset;
  2433. u32 dspcntr;
  2434. i915_reg_t reg = DSPCNTR(plane);
  2435. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2436. int x = plane_state->src.x1 >> 16;
  2437. int y = plane_state->src.y1 >> 16;
  2438. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2439. dspcntr |= DISPLAY_PLANE_ENABLE;
  2440. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2441. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2442. switch (fb->pixel_format) {
  2443. case DRM_FORMAT_C8:
  2444. dspcntr |= DISPPLANE_8BPP;
  2445. break;
  2446. case DRM_FORMAT_RGB565:
  2447. dspcntr |= DISPPLANE_BGRX565;
  2448. break;
  2449. case DRM_FORMAT_XRGB8888:
  2450. dspcntr |= DISPPLANE_BGRX888;
  2451. break;
  2452. case DRM_FORMAT_XBGR8888:
  2453. dspcntr |= DISPPLANE_RGBX888;
  2454. break;
  2455. case DRM_FORMAT_XRGB2101010:
  2456. dspcntr |= DISPPLANE_BGRX101010;
  2457. break;
  2458. case DRM_FORMAT_XBGR2101010:
  2459. dspcntr |= DISPPLANE_RGBX101010;
  2460. break;
  2461. default:
  2462. BUG();
  2463. }
  2464. if (obj->tiling_mode != I915_TILING_NONE)
  2465. dspcntr |= DISPPLANE_TILED;
  2466. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2467. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2468. linear_offset = y * fb->pitches[0] + x * cpp;
  2469. intel_crtc->dspaddr_offset =
  2470. intel_compute_tile_offset(dev_priv, &x, &y,
  2471. fb->modifier[0], cpp,
  2472. fb->pitches[0]);
  2473. linear_offset -= intel_crtc->dspaddr_offset;
  2474. if (plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  2475. dspcntr |= DISPPLANE_ROTATE_180;
  2476. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2477. x += (crtc_state->pipe_src_w - 1);
  2478. y += (crtc_state->pipe_src_h - 1);
  2479. /* Finding the last pixel of the last line of the display
  2480. data and adding to linear_offset*/
  2481. linear_offset +=
  2482. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2483. (crtc_state->pipe_src_w - 1) * cpp;
  2484. }
  2485. }
  2486. intel_crtc->adjusted_x = x;
  2487. intel_crtc->adjusted_y = y;
  2488. I915_WRITE(reg, dspcntr);
  2489. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2490. I915_WRITE(DSPSURF(plane),
  2491. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2492. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2493. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2494. } else {
  2495. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2496. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2497. }
  2498. POSTING_READ(reg);
  2499. }
  2500. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2501. uint64_t fb_modifier, uint32_t pixel_format)
  2502. {
  2503. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2504. return 64;
  2505. } else {
  2506. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2507. return intel_tile_width(dev_priv, fb_modifier, cpp);
  2508. }
  2509. }
  2510. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2511. struct drm_i915_gem_object *obj,
  2512. unsigned int plane)
  2513. {
  2514. struct i915_ggtt_view view;
  2515. struct i915_vma *vma;
  2516. u64 offset;
  2517. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2518. intel_plane->base.state);
  2519. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2520. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2521. view.type))
  2522. return -1;
  2523. offset = vma->node.start;
  2524. if (plane == 1) {
  2525. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2526. PAGE_SIZE;
  2527. }
  2528. WARN_ON(upper_32_bits(offset));
  2529. return lower_32_bits(offset);
  2530. }
  2531. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2532. {
  2533. struct drm_device *dev = intel_crtc->base.dev;
  2534. struct drm_i915_private *dev_priv = dev->dev_private;
  2535. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2536. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2537. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2538. }
  2539. /*
  2540. * This function detaches (aka. unbinds) unused scalers in hardware
  2541. */
  2542. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2543. {
  2544. struct intel_crtc_scaler_state *scaler_state;
  2545. int i;
  2546. scaler_state = &intel_crtc->config->scaler_state;
  2547. /* loop through and disable scalers that aren't in use */
  2548. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2549. if (!scaler_state->scalers[i].in_use)
  2550. skl_detach_scaler(intel_crtc, i);
  2551. }
  2552. }
  2553. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2554. {
  2555. switch (pixel_format) {
  2556. case DRM_FORMAT_C8:
  2557. return PLANE_CTL_FORMAT_INDEXED;
  2558. case DRM_FORMAT_RGB565:
  2559. return PLANE_CTL_FORMAT_RGB_565;
  2560. case DRM_FORMAT_XBGR8888:
  2561. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2562. case DRM_FORMAT_XRGB8888:
  2563. return PLANE_CTL_FORMAT_XRGB_8888;
  2564. /*
  2565. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2566. * to be already pre-multiplied. We need to add a knob (or a different
  2567. * DRM_FORMAT) for user-space to configure that.
  2568. */
  2569. case DRM_FORMAT_ABGR8888:
  2570. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2571. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2572. case DRM_FORMAT_ARGB8888:
  2573. return PLANE_CTL_FORMAT_XRGB_8888 |
  2574. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2575. case DRM_FORMAT_XRGB2101010:
  2576. return PLANE_CTL_FORMAT_XRGB_2101010;
  2577. case DRM_FORMAT_XBGR2101010:
  2578. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2579. case DRM_FORMAT_YUYV:
  2580. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2581. case DRM_FORMAT_YVYU:
  2582. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2583. case DRM_FORMAT_UYVY:
  2584. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2585. case DRM_FORMAT_VYUY:
  2586. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2587. default:
  2588. MISSING_CASE(pixel_format);
  2589. }
  2590. return 0;
  2591. }
  2592. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2593. {
  2594. switch (fb_modifier) {
  2595. case DRM_FORMAT_MOD_NONE:
  2596. break;
  2597. case I915_FORMAT_MOD_X_TILED:
  2598. return PLANE_CTL_TILED_X;
  2599. case I915_FORMAT_MOD_Y_TILED:
  2600. return PLANE_CTL_TILED_Y;
  2601. case I915_FORMAT_MOD_Yf_TILED:
  2602. return PLANE_CTL_TILED_YF;
  2603. default:
  2604. MISSING_CASE(fb_modifier);
  2605. }
  2606. return 0;
  2607. }
  2608. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2609. {
  2610. switch (rotation) {
  2611. case BIT(DRM_ROTATE_0):
  2612. break;
  2613. /*
  2614. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2615. * while i915 HW rotation is clockwise, thats why this swapping.
  2616. */
  2617. case BIT(DRM_ROTATE_90):
  2618. return PLANE_CTL_ROTATE_270;
  2619. case BIT(DRM_ROTATE_180):
  2620. return PLANE_CTL_ROTATE_180;
  2621. case BIT(DRM_ROTATE_270):
  2622. return PLANE_CTL_ROTATE_90;
  2623. default:
  2624. MISSING_CASE(rotation);
  2625. }
  2626. return 0;
  2627. }
  2628. static void skylake_update_primary_plane(struct drm_plane *plane,
  2629. const struct intel_crtc_state *crtc_state,
  2630. const struct intel_plane_state *plane_state)
  2631. {
  2632. struct drm_device *dev = plane->dev;
  2633. struct drm_i915_private *dev_priv = dev->dev_private;
  2634. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2635. struct drm_framebuffer *fb = plane_state->base.fb;
  2636. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2637. int pipe = intel_crtc->pipe;
  2638. u32 plane_ctl, stride_div, stride;
  2639. u32 tile_height, plane_offset, plane_size;
  2640. unsigned int rotation = plane_state->base.rotation;
  2641. int x_offset, y_offset;
  2642. u32 surf_addr;
  2643. int scaler_id = plane_state->scaler_id;
  2644. int src_x = plane_state->src.x1 >> 16;
  2645. int src_y = plane_state->src.y1 >> 16;
  2646. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2647. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2648. int dst_x = plane_state->dst.x1;
  2649. int dst_y = plane_state->dst.y1;
  2650. int dst_w = drm_rect_width(&plane_state->dst);
  2651. int dst_h = drm_rect_height(&plane_state->dst);
  2652. plane_ctl = PLANE_CTL_ENABLE |
  2653. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2654. PLANE_CTL_PIPE_CSC_ENABLE;
  2655. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2656. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2657. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2658. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2659. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2660. fb->pixel_format);
  2661. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2662. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2663. if (intel_rotation_90_or_270(rotation)) {
  2664. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2665. /* stride = Surface height in tiles */
  2666. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2667. stride = DIV_ROUND_UP(fb->height, tile_height);
  2668. x_offset = stride * tile_height - src_y - src_h;
  2669. y_offset = src_x;
  2670. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2671. } else {
  2672. stride = fb->pitches[0] / stride_div;
  2673. x_offset = src_x;
  2674. y_offset = src_y;
  2675. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2676. }
  2677. plane_offset = y_offset << 16 | x_offset;
  2678. intel_crtc->adjusted_x = x_offset;
  2679. intel_crtc->adjusted_y = y_offset;
  2680. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2681. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2682. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2683. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2684. if (scaler_id >= 0) {
  2685. uint32_t ps_ctrl = 0;
  2686. WARN_ON(!dst_w || !dst_h);
  2687. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2688. crtc_state->scaler_state.scalers[scaler_id].mode;
  2689. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2690. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2691. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2692. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2693. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2694. } else {
  2695. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2696. }
  2697. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2698. POSTING_READ(PLANE_SURF(pipe, 0));
  2699. }
  2700. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2701. struct drm_crtc *crtc)
  2702. {
  2703. struct drm_device *dev = crtc->dev;
  2704. struct drm_i915_private *dev_priv = dev->dev_private;
  2705. int pipe = to_intel_crtc(crtc)->pipe;
  2706. if (dev_priv->fbc.deactivate)
  2707. dev_priv->fbc.deactivate(dev_priv);
  2708. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2709. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2710. POSTING_READ(PLANE_SURF(pipe, 0));
  2711. }
  2712. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2713. static int
  2714. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2715. int x, int y, enum mode_set_atomic state)
  2716. {
  2717. /* Support for kgdboc is disabled, this needs a major rework. */
  2718. DRM_ERROR("legacy panic handler not supported any more.\n");
  2719. return -ENODEV;
  2720. }
  2721. static void intel_complete_page_flips(struct drm_device *dev)
  2722. {
  2723. struct drm_crtc *crtc;
  2724. for_each_crtc(dev, crtc) {
  2725. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2726. enum plane plane = intel_crtc->plane;
  2727. intel_prepare_page_flip(dev, plane);
  2728. intel_finish_page_flip_plane(dev, plane);
  2729. }
  2730. }
  2731. static void intel_update_primary_planes(struct drm_device *dev)
  2732. {
  2733. struct drm_crtc *crtc;
  2734. for_each_crtc(dev, crtc) {
  2735. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2736. struct intel_plane_state *plane_state;
  2737. drm_modeset_lock_crtc(crtc, &plane->base);
  2738. plane_state = to_intel_plane_state(plane->base.state);
  2739. if (plane_state->visible)
  2740. plane->update_plane(&plane->base,
  2741. to_intel_crtc_state(crtc->state),
  2742. plane_state);
  2743. drm_modeset_unlock_crtc(crtc);
  2744. }
  2745. }
  2746. void intel_prepare_reset(struct drm_device *dev)
  2747. {
  2748. /* no reset support for gen2 */
  2749. if (IS_GEN2(dev))
  2750. return;
  2751. /* reset doesn't touch the display */
  2752. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2753. return;
  2754. drm_modeset_lock_all(dev);
  2755. /*
  2756. * Disabling the crtcs gracefully seems nicer. Also the
  2757. * g33 docs say we should at least disable all the planes.
  2758. */
  2759. intel_display_suspend(dev);
  2760. }
  2761. void intel_finish_reset(struct drm_device *dev)
  2762. {
  2763. struct drm_i915_private *dev_priv = to_i915(dev);
  2764. /*
  2765. * Flips in the rings will be nuked by the reset,
  2766. * so complete all pending flips so that user space
  2767. * will get its events and not get stuck.
  2768. */
  2769. intel_complete_page_flips(dev);
  2770. /* no reset support for gen2 */
  2771. if (IS_GEN2(dev))
  2772. return;
  2773. /* reset doesn't touch the display */
  2774. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2775. /*
  2776. * Flips in the rings have been nuked by the reset,
  2777. * so update the base address of all primary
  2778. * planes to the the last fb to make sure we're
  2779. * showing the correct fb after a reset.
  2780. *
  2781. * FIXME: Atomic will make this obsolete since we won't schedule
  2782. * CS-based flips (which might get lost in gpu resets) any more.
  2783. */
  2784. intel_update_primary_planes(dev);
  2785. return;
  2786. }
  2787. /*
  2788. * The display has been reset as well,
  2789. * so need a full re-initialization.
  2790. */
  2791. intel_runtime_pm_disable_interrupts(dev_priv);
  2792. intel_runtime_pm_enable_interrupts(dev_priv);
  2793. intel_modeset_init_hw(dev);
  2794. spin_lock_irq(&dev_priv->irq_lock);
  2795. if (dev_priv->display.hpd_irq_setup)
  2796. dev_priv->display.hpd_irq_setup(dev);
  2797. spin_unlock_irq(&dev_priv->irq_lock);
  2798. intel_display_resume(dev);
  2799. intel_hpd_init(dev_priv);
  2800. drm_modeset_unlock_all(dev);
  2801. }
  2802. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2803. {
  2804. struct drm_device *dev = crtc->dev;
  2805. struct drm_i915_private *dev_priv = dev->dev_private;
  2806. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2807. bool pending;
  2808. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2809. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2810. return false;
  2811. spin_lock_irq(&dev->event_lock);
  2812. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2813. spin_unlock_irq(&dev->event_lock);
  2814. return pending;
  2815. }
  2816. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2817. struct intel_crtc_state *old_crtc_state)
  2818. {
  2819. struct drm_device *dev = crtc->base.dev;
  2820. struct drm_i915_private *dev_priv = dev->dev_private;
  2821. struct intel_crtc_state *pipe_config =
  2822. to_intel_crtc_state(crtc->base.state);
  2823. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2824. crtc->base.mode = crtc->base.state->mode;
  2825. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2826. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2827. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2828. if (HAS_DDI(dev))
  2829. intel_set_pipe_csc(&crtc->base);
  2830. /*
  2831. * Update pipe size and adjust fitter if needed: the reason for this is
  2832. * that in compute_mode_changes we check the native mode (not the pfit
  2833. * mode) to see if we can flip rather than do a full mode set. In the
  2834. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2835. * pfit state, we'll end up with a big fb scanned out into the wrong
  2836. * sized surface.
  2837. */
  2838. I915_WRITE(PIPESRC(crtc->pipe),
  2839. ((pipe_config->pipe_src_w - 1) << 16) |
  2840. (pipe_config->pipe_src_h - 1));
  2841. /* on skylake this is done by detaching scalers */
  2842. if (INTEL_INFO(dev)->gen >= 9) {
  2843. skl_detach_scalers(crtc);
  2844. if (pipe_config->pch_pfit.enabled)
  2845. skylake_pfit_enable(crtc);
  2846. } else if (HAS_PCH_SPLIT(dev)) {
  2847. if (pipe_config->pch_pfit.enabled)
  2848. ironlake_pfit_enable(crtc);
  2849. else if (old_crtc_state->pch_pfit.enabled)
  2850. ironlake_pfit_disable(crtc, true);
  2851. }
  2852. }
  2853. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2854. {
  2855. struct drm_device *dev = crtc->dev;
  2856. struct drm_i915_private *dev_priv = dev->dev_private;
  2857. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2858. int pipe = intel_crtc->pipe;
  2859. i915_reg_t reg;
  2860. u32 temp;
  2861. /* enable normal train */
  2862. reg = FDI_TX_CTL(pipe);
  2863. temp = I915_READ(reg);
  2864. if (IS_IVYBRIDGE(dev)) {
  2865. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2866. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2867. } else {
  2868. temp &= ~FDI_LINK_TRAIN_NONE;
  2869. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2870. }
  2871. I915_WRITE(reg, temp);
  2872. reg = FDI_RX_CTL(pipe);
  2873. temp = I915_READ(reg);
  2874. if (HAS_PCH_CPT(dev)) {
  2875. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2876. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2877. } else {
  2878. temp &= ~FDI_LINK_TRAIN_NONE;
  2879. temp |= FDI_LINK_TRAIN_NONE;
  2880. }
  2881. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2882. /* wait one idle pattern time */
  2883. POSTING_READ(reg);
  2884. udelay(1000);
  2885. /* IVB wants error correction enabled */
  2886. if (IS_IVYBRIDGE(dev))
  2887. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2888. FDI_FE_ERRC_ENABLE);
  2889. }
  2890. /* The FDI link training functions for ILK/Ibexpeak. */
  2891. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2892. {
  2893. struct drm_device *dev = crtc->dev;
  2894. struct drm_i915_private *dev_priv = dev->dev_private;
  2895. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2896. int pipe = intel_crtc->pipe;
  2897. i915_reg_t reg;
  2898. u32 temp, tries;
  2899. /* FDI needs bits from pipe first */
  2900. assert_pipe_enabled(dev_priv, pipe);
  2901. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2902. for train result */
  2903. reg = FDI_RX_IMR(pipe);
  2904. temp = I915_READ(reg);
  2905. temp &= ~FDI_RX_SYMBOL_LOCK;
  2906. temp &= ~FDI_RX_BIT_LOCK;
  2907. I915_WRITE(reg, temp);
  2908. I915_READ(reg);
  2909. udelay(150);
  2910. /* enable CPU FDI TX and PCH FDI RX */
  2911. reg = FDI_TX_CTL(pipe);
  2912. temp = I915_READ(reg);
  2913. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2914. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2915. temp &= ~FDI_LINK_TRAIN_NONE;
  2916. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2917. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2918. reg = FDI_RX_CTL(pipe);
  2919. temp = I915_READ(reg);
  2920. temp &= ~FDI_LINK_TRAIN_NONE;
  2921. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2922. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2923. POSTING_READ(reg);
  2924. udelay(150);
  2925. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2926. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2927. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2928. FDI_RX_PHASE_SYNC_POINTER_EN);
  2929. reg = FDI_RX_IIR(pipe);
  2930. for (tries = 0; tries < 5; tries++) {
  2931. temp = I915_READ(reg);
  2932. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2933. if ((temp & FDI_RX_BIT_LOCK)) {
  2934. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2935. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2936. break;
  2937. }
  2938. }
  2939. if (tries == 5)
  2940. DRM_ERROR("FDI train 1 fail!\n");
  2941. /* Train 2 */
  2942. reg = FDI_TX_CTL(pipe);
  2943. temp = I915_READ(reg);
  2944. temp &= ~FDI_LINK_TRAIN_NONE;
  2945. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2946. I915_WRITE(reg, temp);
  2947. reg = FDI_RX_CTL(pipe);
  2948. temp = I915_READ(reg);
  2949. temp &= ~FDI_LINK_TRAIN_NONE;
  2950. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2951. I915_WRITE(reg, temp);
  2952. POSTING_READ(reg);
  2953. udelay(150);
  2954. reg = FDI_RX_IIR(pipe);
  2955. for (tries = 0; tries < 5; tries++) {
  2956. temp = I915_READ(reg);
  2957. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2958. if (temp & FDI_RX_SYMBOL_LOCK) {
  2959. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2960. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2961. break;
  2962. }
  2963. }
  2964. if (tries == 5)
  2965. DRM_ERROR("FDI train 2 fail!\n");
  2966. DRM_DEBUG_KMS("FDI train done\n");
  2967. }
  2968. static const int snb_b_fdi_train_param[] = {
  2969. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2970. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2971. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2972. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2973. };
  2974. /* The FDI link training functions for SNB/Cougarpoint. */
  2975. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2976. {
  2977. struct drm_device *dev = crtc->dev;
  2978. struct drm_i915_private *dev_priv = dev->dev_private;
  2979. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2980. int pipe = intel_crtc->pipe;
  2981. i915_reg_t reg;
  2982. u32 temp, i, retry;
  2983. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2984. for train result */
  2985. reg = FDI_RX_IMR(pipe);
  2986. temp = I915_READ(reg);
  2987. temp &= ~FDI_RX_SYMBOL_LOCK;
  2988. temp &= ~FDI_RX_BIT_LOCK;
  2989. I915_WRITE(reg, temp);
  2990. POSTING_READ(reg);
  2991. udelay(150);
  2992. /* enable CPU FDI TX and PCH FDI RX */
  2993. reg = FDI_TX_CTL(pipe);
  2994. temp = I915_READ(reg);
  2995. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2996. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2997. temp &= ~FDI_LINK_TRAIN_NONE;
  2998. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2999. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3000. /* SNB-B */
  3001. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3002. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3003. I915_WRITE(FDI_RX_MISC(pipe),
  3004. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3005. reg = FDI_RX_CTL(pipe);
  3006. temp = I915_READ(reg);
  3007. if (HAS_PCH_CPT(dev)) {
  3008. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3009. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3010. } else {
  3011. temp &= ~FDI_LINK_TRAIN_NONE;
  3012. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3013. }
  3014. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3015. POSTING_READ(reg);
  3016. udelay(150);
  3017. for (i = 0; i < 4; i++) {
  3018. reg = FDI_TX_CTL(pipe);
  3019. temp = I915_READ(reg);
  3020. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3021. temp |= snb_b_fdi_train_param[i];
  3022. I915_WRITE(reg, temp);
  3023. POSTING_READ(reg);
  3024. udelay(500);
  3025. for (retry = 0; retry < 5; retry++) {
  3026. reg = FDI_RX_IIR(pipe);
  3027. temp = I915_READ(reg);
  3028. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3029. if (temp & FDI_RX_BIT_LOCK) {
  3030. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3031. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3032. break;
  3033. }
  3034. udelay(50);
  3035. }
  3036. if (retry < 5)
  3037. break;
  3038. }
  3039. if (i == 4)
  3040. DRM_ERROR("FDI train 1 fail!\n");
  3041. /* Train 2 */
  3042. reg = FDI_TX_CTL(pipe);
  3043. temp = I915_READ(reg);
  3044. temp &= ~FDI_LINK_TRAIN_NONE;
  3045. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3046. if (IS_GEN6(dev)) {
  3047. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3048. /* SNB-B */
  3049. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3050. }
  3051. I915_WRITE(reg, temp);
  3052. reg = FDI_RX_CTL(pipe);
  3053. temp = I915_READ(reg);
  3054. if (HAS_PCH_CPT(dev)) {
  3055. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3056. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3057. } else {
  3058. temp &= ~FDI_LINK_TRAIN_NONE;
  3059. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3060. }
  3061. I915_WRITE(reg, temp);
  3062. POSTING_READ(reg);
  3063. udelay(150);
  3064. for (i = 0; i < 4; i++) {
  3065. reg = FDI_TX_CTL(pipe);
  3066. temp = I915_READ(reg);
  3067. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3068. temp |= snb_b_fdi_train_param[i];
  3069. I915_WRITE(reg, temp);
  3070. POSTING_READ(reg);
  3071. udelay(500);
  3072. for (retry = 0; retry < 5; retry++) {
  3073. reg = FDI_RX_IIR(pipe);
  3074. temp = I915_READ(reg);
  3075. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3076. if (temp & FDI_RX_SYMBOL_LOCK) {
  3077. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3078. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3079. break;
  3080. }
  3081. udelay(50);
  3082. }
  3083. if (retry < 5)
  3084. break;
  3085. }
  3086. if (i == 4)
  3087. DRM_ERROR("FDI train 2 fail!\n");
  3088. DRM_DEBUG_KMS("FDI train done.\n");
  3089. }
  3090. /* Manual link training for Ivy Bridge A0 parts */
  3091. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3092. {
  3093. struct drm_device *dev = crtc->dev;
  3094. struct drm_i915_private *dev_priv = dev->dev_private;
  3095. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3096. int pipe = intel_crtc->pipe;
  3097. i915_reg_t reg;
  3098. u32 temp, i, j;
  3099. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3100. for train result */
  3101. reg = FDI_RX_IMR(pipe);
  3102. temp = I915_READ(reg);
  3103. temp &= ~FDI_RX_SYMBOL_LOCK;
  3104. temp &= ~FDI_RX_BIT_LOCK;
  3105. I915_WRITE(reg, temp);
  3106. POSTING_READ(reg);
  3107. udelay(150);
  3108. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3109. I915_READ(FDI_RX_IIR(pipe)));
  3110. /* Try each vswing and preemphasis setting twice before moving on */
  3111. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3112. /* disable first in case we need to retry */
  3113. reg = FDI_TX_CTL(pipe);
  3114. temp = I915_READ(reg);
  3115. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3116. temp &= ~FDI_TX_ENABLE;
  3117. I915_WRITE(reg, temp);
  3118. reg = FDI_RX_CTL(pipe);
  3119. temp = I915_READ(reg);
  3120. temp &= ~FDI_LINK_TRAIN_AUTO;
  3121. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3122. temp &= ~FDI_RX_ENABLE;
  3123. I915_WRITE(reg, temp);
  3124. /* enable CPU FDI TX and PCH FDI RX */
  3125. reg = FDI_TX_CTL(pipe);
  3126. temp = I915_READ(reg);
  3127. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3128. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3129. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3130. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3131. temp |= snb_b_fdi_train_param[j/2];
  3132. temp |= FDI_COMPOSITE_SYNC;
  3133. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3134. I915_WRITE(FDI_RX_MISC(pipe),
  3135. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3136. reg = FDI_RX_CTL(pipe);
  3137. temp = I915_READ(reg);
  3138. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3139. temp |= FDI_COMPOSITE_SYNC;
  3140. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3141. POSTING_READ(reg);
  3142. udelay(1); /* should be 0.5us */
  3143. for (i = 0; i < 4; i++) {
  3144. reg = FDI_RX_IIR(pipe);
  3145. temp = I915_READ(reg);
  3146. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3147. if (temp & FDI_RX_BIT_LOCK ||
  3148. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3149. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3150. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3151. i);
  3152. break;
  3153. }
  3154. udelay(1); /* should be 0.5us */
  3155. }
  3156. if (i == 4) {
  3157. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3158. continue;
  3159. }
  3160. /* Train 2 */
  3161. reg = FDI_TX_CTL(pipe);
  3162. temp = I915_READ(reg);
  3163. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3164. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3165. I915_WRITE(reg, temp);
  3166. reg = FDI_RX_CTL(pipe);
  3167. temp = I915_READ(reg);
  3168. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3169. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3170. I915_WRITE(reg, temp);
  3171. POSTING_READ(reg);
  3172. udelay(2); /* should be 1.5us */
  3173. for (i = 0; i < 4; i++) {
  3174. reg = FDI_RX_IIR(pipe);
  3175. temp = I915_READ(reg);
  3176. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3177. if (temp & FDI_RX_SYMBOL_LOCK ||
  3178. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3179. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3180. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3181. i);
  3182. goto train_done;
  3183. }
  3184. udelay(2); /* should be 1.5us */
  3185. }
  3186. if (i == 4)
  3187. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3188. }
  3189. train_done:
  3190. DRM_DEBUG_KMS("FDI train done.\n");
  3191. }
  3192. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3193. {
  3194. struct drm_device *dev = intel_crtc->base.dev;
  3195. struct drm_i915_private *dev_priv = dev->dev_private;
  3196. int pipe = intel_crtc->pipe;
  3197. i915_reg_t reg;
  3198. u32 temp;
  3199. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3200. reg = FDI_RX_CTL(pipe);
  3201. temp = I915_READ(reg);
  3202. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3203. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3204. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3205. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3206. POSTING_READ(reg);
  3207. udelay(200);
  3208. /* Switch from Rawclk to PCDclk */
  3209. temp = I915_READ(reg);
  3210. I915_WRITE(reg, temp | FDI_PCDCLK);
  3211. POSTING_READ(reg);
  3212. udelay(200);
  3213. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3214. reg = FDI_TX_CTL(pipe);
  3215. temp = I915_READ(reg);
  3216. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3217. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3218. POSTING_READ(reg);
  3219. udelay(100);
  3220. }
  3221. }
  3222. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3223. {
  3224. struct drm_device *dev = intel_crtc->base.dev;
  3225. struct drm_i915_private *dev_priv = dev->dev_private;
  3226. int pipe = intel_crtc->pipe;
  3227. i915_reg_t reg;
  3228. u32 temp;
  3229. /* Switch from PCDclk to Rawclk */
  3230. reg = FDI_RX_CTL(pipe);
  3231. temp = I915_READ(reg);
  3232. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3233. /* Disable CPU FDI TX PLL */
  3234. reg = FDI_TX_CTL(pipe);
  3235. temp = I915_READ(reg);
  3236. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3237. POSTING_READ(reg);
  3238. udelay(100);
  3239. reg = FDI_RX_CTL(pipe);
  3240. temp = I915_READ(reg);
  3241. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3242. /* Wait for the clocks to turn off. */
  3243. POSTING_READ(reg);
  3244. udelay(100);
  3245. }
  3246. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3247. {
  3248. struct drm_device *dev = crtc->dev;
  3249. struct drm_i915_private *dev_priv = dev->dev_private;
  3250. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3251. int pipe = intel_crtc->pipe;
  3252. i915_reg_t reg;
  3253. u32 temp;
  3254. /* disable CPU FDI tx and PCH FDI rx */
  3255. reg = FDI_TX_CTL(pipe);
  3256. temp = I915_READ(reg);
  3257. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3258. POSTING_READ(reg);
  3259. reg = FDI_RX_CTL(pipe);
  3260. temp = I915_READ(reg);
  3261. temp &= ~(0x7 << 16);
  3262. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3263. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3264. POSTING_READ(reg);
  3265. udelay(100);
  3266. /* Ironlake workaround, disable clock pointer after downing FDI */
  3267. if (HAS_PCH_IBX(dev))
  3268. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3269. /* still set train pattern 1 */
  3270. reg = FDI_TX_CTL(pipe);
  3271. temp = I915_READ(reg);
  3272. temp &= ~FDI_LINK_TRAIN_NONE;
  3273. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3274. I915_WRITE(reg, temp);
  3275. reg = FDI_RX_CTL(pipe);
  3276. temp = I915_READ(reg);
  3277. if (HAS_PCH_CPT(dev)) {
  3278. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3279. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3280. } else {
  3281. temp &= ~FDI_LINK_TRAIN_NONE;
  3282. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3283. }
  3284. /* BPC in FDI rx is consistent with that in PIPECONF */
  3285. temp &= ~(0x07 << 16);
  3286. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3287. I915_WRITE(reg, temp);
  3288. POSTING_READ(reg);
  3289. udelay(100);
  3290. }
  3291. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3292. {
  3293. struct intel_crtc *crtc;
  3294. /* Note that we don't need to be called with mode_config.lock here
  3295. * as our list of CRTC objects is static for the lifetime of the
  3296. * device and so cannot disappear as we iterate. Similarly, we can
  3297. * happily treat the predicates as racy, atomic checks as userspace
  3298. * cannot claim and pin a new fb without at least acquring the
  3299. * struct_mutex and so serialising with us.
  3300. */
  3301. for_each_intel_crtc(dev, crtc) {
  3302. if (atomic_read(&crtc->unpin_work_count) == 0)
  3303. continue;
  3304. if (crtc->unpin_work)
  3305. intel_wait_for_vblank(dev, crtc->pipe);
  3306. return true;
  3307. }
  3308. return false;
  3309. }
  3310. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3311. {
  3312. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3313. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3314. /* ensure that the unpin work is consistent wrt ->pending. */
  3315. smp_rmb();
  3316. intel_crtc->unpin_work = NULL;
  3317. if (work->event)
  3318. drm_send_vblank_event(intel_crtc->base.dev,
  3319. intel_crtc->pipe,
  3320. work->event);
  3321. drm_crtc_vblank_put(&intel_crtc->base);
  3322. wake_up_all(&dev_priv->pending_flip_queue);
  3323. queue_work(dev_priv->wq, &work->work);
  3324. trace_i915_flip_complete(intel_crtc->plane,
  3325. work->pending_flip_obj);
  3326. }
  3327. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3328. {
  3329. struct drm_device *dev = crtc->dev;
  3330. struct drm_i915_private *dev_priv = dev->dev_private;
  3331. long ret;
  3332. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3333. ret = wait_event_interruptible_timeout(
  3334. dev_priv->pending_flip_queue,
  3335. !intel_crtc_has_pending_flip(crtc),
  3336. 60*HZ);
  3337. if (ret < 0)
  3338. return ret;
  3339. if (ret == 0) {
  3340. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3341. spin_lock_irq(&dev->event_lock);
  3342. if (intel_crtc->unpin_work) {
  3343. WARN_ONCE(1, "Removing stuck page flip\n");
  3344. page_flip_completed(intel_crtc);
  3345. }
  3346. spin_unlock_irq(&dev->event_lock);
  3347. }
  3348. return 0;
  3349. }
  3350. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3351. {
  3352. u32 temp;
  3353. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3354. mutex_lock(&dev_priv->sb_lock);
  3355. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3356. temp |= SBI_SSCCTL_DISABLE;
  3357. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3358. mutex_unlock(&dev_priv->sb_lock);
  3359. }
  3360. /* Program iCLKIP clock to the desired frequency */
  3361. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3362. {
  3363. struct drm_device *dev = crtc->dev;
  3364. struct drm_i915_private *dev_priv = dev->dev_private;
  3365. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3366. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3367. u32 temp;
  3368. lpt_disable_iclkip(dev_priv);
  3369. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3370. if (clock == 20000) {
  3371. auxdiv = 1;
  3372. divsel = 0x41;
  3373. phaseinc = 0x20;
  3374. } else {
  3375. /* The iCLK virtual clock root frequency is in MHz,
  3376. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3377. * divisors, it is necessary to divide one by another, so we
  3378. * convert the virtual clock precision to KHz here for higher
  3379. * precision.
  3380. */
  3381. u32 iclk_virtual_root_freq = 172800 * 1000;
  3382. u32 iclk_pi_range = 64;
  3383. u32 desired_divisor, msb_divisor_value, pi_value;
  3384. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq, clock);
  3385. msb_divisor_value = desired_divisor / iclk_pi_range;
  3386. pi_value = desired_divisor % iclk_pi_range;
  3387. auxdiv = 0;
  3388. divsel = msb_divisor_value - 2;
  3389. phaseinc = pi_value;
  3390. }
  3391. /* This should not happen with any sane values */
  3392. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3393. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3394. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3395. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3396. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3397. clock,
  3398. auxdiv,
  3399. divsel,
  3400. phasedir,
  3401. phaseinc);
  3402. mutex_lock(&dev_priv->sb_lock);
  3403. /* Program SSCDIVINTPHASE6 */
  3404. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3405. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3406. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3407. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3408. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3409. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3410. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3411. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3412. /* Program SSCAUXDIV */
  3413. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3414. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3415. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3416. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3417. /* Enable modulator and associated divider */
  3418. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3419. temp &= ~SBI_SSCCTL_DISABLE;
  3420. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3421. mutex_unlock(&dev_priv->sb_lock);
  3422. /* Wait for initialization time */
  3423. udelay(24);
  3424. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3425. }
  3426. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3427. enum pipe pch_transcoder)
  3428. {
  3429. struct drm_device *dev = crtc->base.dev;
  3430. struct drm_i915_private *dev_priv = dev->dev_private;
  3431. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3432. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3433. I915_READ(HTOTAL(cpu_transcoder)));
  3434. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3435. I915_READ(HBLANK(cpu_transcoder)));
  3436. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3437. I915_READ(HSYNC(cpu_transcoder)));
  3438. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3439. I915_READ(VTOTAL(cpu_transcoder)));
  3440. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3441. I915_READ(VBLANK(cpu_transcoder)));
  3442. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3443. I915_READ(VSYNC(cpu_transcoder)));
  3444. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3445. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3446. }
  3447. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3448. {
  3449. struct drm_i915_private *dev_priv = dev->dev_private;
  3450. uint32_t temp;
  3451. temp = I915_READ(SOUTH_CHICKEN1);
  3452. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3453. return;
  3454. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3455. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3456. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3457. if (enable)
  3458. temp |= FDI_BC_BIFURCATION_SELECT;
  3459. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3460. I915_WRITE(SOUTH_CHICKEN1, temp);
  3461. POSTING_READ(SOUTH_CHICKEN1);
  3462. }
  3463. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3464. {
  3465. struct drm_device *dev = intel_crtc->base.dev;
  3466. switch (intel_crtc->pipe) {
  3467. case PIPE_A:
  3468. break;
  3469. case PIPE_B:
  3470. if (intel_crtc->config->fdi_lanes > 2)
  3471. cpt_set_fdi_bc_bifurcation(dev, false);
  3472. else
  3473. cpt_set_fdi_bc_bifurcation(dev, true);
  3474. break;
  3475. case PIPE_C:
  3476. cpt_set_fdi_bc_bifurcation(dev, true);
  3477. break;
  3478. default:
  3479. BUG();
  3480. }
  3481. }
  3482. /* Return which DP Port should be selected for Transcoder DP control */
  3483. static enum port
  3484. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3485. {
  3486. struct drm_device *dev = crtc->dev;
  3487. struct intel_encoder *encoder;
  3488. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3489. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3490. encoder->type == INTEL_OUTPUT_EDP)
  3491. return enc_to_dig_port(&encoder->base)->port;
  3492. }
  3493. return -1;
  3494. }
  3495. /*
  3496. * Enable PCH resources required for PCH ports:
  3497. * - PCH PLLs
  3498. * - FDI training & RX/TX
  3499. * - update transcoder timings
  3500. * - DP transcoding bits
  3501. * - transcoder
  3502. */
  3503. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3504. {
  3505. struct drm_device *dev = crtc->dev;
  3506. struct drm_i915_private *dev_priv = dev->dev_private;
  3507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3508. int pipe = intel_crtc->pipe;
  3509. u32 temp;
  3510. assert_pch_transcoder_disabled(dev_priv, pipe);
  3511. if (IS_IVYBRIDGE(dev))
  3512. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3513. /* Write the TU size bits before fdi link training, so that error
  3514. * detection works. */
  3515. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3516. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3517. /*
  3518. * Sometimes spurious CPU pipe underruns happen during FDI
  3519. * training, at least with VGA+HDMI cloning. Suppress them.
  3520. */
  3521. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3522. /* For PCH output, training FDI link */
  3523. dev_priv->display.fdi_link_train(crtc);
  3524. /* We need to program the right clock selection before writing the pixel
  3525. * mutliplier into the DPLL. */
  3526. if (HAS_PCH_CPT(dev)) {
  3527. u32 sel;
  3528. temp = I915_READ(PCH_DPLL_SEL);
  3529. temp |= TRANS_DPLL_ENABLE(pipe);
  3530. sel = TRANS_DPLLB_SEL(pipe);
  3531. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3532. temp |= sel;
  3533. else
  3534. temp &= ~sel;
  3535. I915_WRITE(PCH_DPLL_SEL, temp);
  3536. }
  3537. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3538. * transcoder, and we actually should do this to not upset any PCH
  3539. * transcoder that already use the clock when we share it.
  3540. *
  3541. * Note that enable_shared_dpll tries to do the right thing, but
  3542. * get_shared_dpll unconditionally resets the pll - we need that to have
  3543. * the right LVDS enable sequence. */
  3544. intel_enable_shared_dpll(intel_crtc);
  3545. /* set transcoder timing, panel must allow it */
  3546. assert_panel_unlocked(dev_priv, pipe);
  3547. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3548. intel_fdi_normal_train(crtc);
  3549. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3550. /* For PCH DP, enable TRANS_DP_CTL */
  3551. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3552. const struct drm_display_mode *adjusted_mode =
  3553. &intel_crtc->config->base.adjusted_mode;
  3554. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3555. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3556. temp = I915_READ(reg);
  3557. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3558. TRANS_DP_SYNC_MASK |
  3559. TRANS_DP_BPC_MASK);
  3560. temp |= TRANS_DP_OUTPUT_ENABLE;
  3561. temp |= bpc << 9; /* same format but at 11:9 */
  3562. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3563. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3564. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3565. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3566. switch (intel_trans_dp_port_sel(crtc)) {
  3567. case PORT_B:
  3568. temp |= TRANS_DP_PORT_SEL_B;
  3569. break;
  3570. case PORT_C:
  3571. temp |= TRANS_DP_PORT_SEL_C;
  3572. break;
  3573. case PORT_D:
  3574. temp |= TRANS_DP_PORT_SEL_D;
  3575. break;
  3576. default:
  3577. BUG();
  3578. }
  3579. I915_WRITE(reg, temp);
  3580. }
  3581. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3582. }
  3583. static void lpt_pch_enable(struct drm_crtc *crtc)
  3584. {
  3585. struct drm_device *dev = crtc->dev;
  3586. struct drm_i915_private *dev_priv = dev->dev_private;
  3587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3588. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3589. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3590. lpt_program_iclkip(crtc);
  3591. /* Set transcoder timing. */
  3592. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3593. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3594. }
  3595. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3596. struct intel_crtc_state *crtc_state)
  3597. {
  3598. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3599. struct intel_shared_dpll *pll;
  3600. struct intel_shared_dpll_config *shared_dpll;
  3601. enum intel_dpll_id i;
  3602. int max = dev_priv->num_shared_dpll;
  3603. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3604. if (HAS_PCH_IBX(dev_priv->dev)) {
  3605. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3606. i = (enum intel_dpll_id) crtc->pipe;
  3607. pll = &dev_priv->shared_dplls[i];
  3608. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3609. crtc->base.base.id, pll->name);
  3610. WARN_ON(shared_dpll[i].crtc_mask);
  3611. goto found;
  3612. }
  3613. if (IS_BROXTON(dev_priv->dev)) {
  3614. /* PLL is attached to port in bxt */
  3615. struct intel_encoder *encoder;
  3616. struct intel_digital_port *intel_dig_port;
  3617. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3618. if (WARN_ON(!encoder))
  3619. return NULL;
  3620. intel_dig_port = enc_to_dig_port(&encoder->base);
  3621. /* 1:1 mapping between ports and PLLs */
  3622. i = (enum intel_dpll_id)intel_dig_port->port;
  3623. pll = &dev_priv->shared_dplls[i];
  3624. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3625. crtc->base.base.id, pll->name);
  3626. WARN_ON(shared_dpll[i].crtc_mask);
  3627. goto found;
  3628. } else if (INTEL_INFO(dev_priv)->gen < 9 && HAS_DDI(dev_priv))
  3629. /* Do not consider SPLL */
  3630. max = 2;
  3631. for (i = 0; i < max; i++) {
  3632. pll = &dev_priv->shared_dplls[i];
  3633. /* Only want to check enabled timings first */
  3634. if (shared_dpll[i].crtc_mask == 0)
  3635. continue;
  3636. if (memcmp(&crtc_state->dpll_hw_state,
  3637. &shared_dpll[i].hw_state,
  3638. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3639. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3640. crtc->base.base.id, pll->name,
  3641. shared_dpll[i].crtc_mask,
  3642. pll->active);
  3643. goto found;
  3644. }
  3645. }
  3646. /* Ok no matching timings, maybe there's a free one? */
  3647. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3648. pll = &dev_priv->shared_dplls[i];
  3649. if (shared_dpll[i].crtc_mask == 0) {
  3650. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3651. crtc->base.base.id, pll->name);
  3652. goto found;
  3653. }
  3654. }
  3655. return NULL;
  3656. found:
  3657. if (shared_dpll[i].crtc_mask == 0)
  3658. shared_dpll[i].hw_state =
  3659. crtc_state->dpll_hw_state;
  3660. crtc_state->shared_dpll = i;
  3661. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3662. pipe_name(crtc->pipe));
  3663. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3664. return pll;
  3665. }
  3666. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3667. {
  3668. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3669. struct intel_shared_dpll_config *shared_dpll;
  3670. struct intel_shared_dpll *pll;
  3671. enum intel_dpll_id i;
  3672. if (!to_intel_atomic_state(state)->dpll_set)
  3673. return;
  3674. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3675. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3676. pll = &dev_priv->shared_dplls[i];
  3677. pll->config = shared_dpll[i];
  3678. }
  3679. }
  3680. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3681. {
  3682. struct drm_i915_private *dev_priv = dev->dev_private;
  3683. i915_reg_t dslreg = PIPEDSL(pipe);
  3684. u32 temp;
  3685. temp = I915_READ(dslreg);
  3686. udelay(500);
  3687. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3688. if (wait_for(I915_READ(dslreg) != temp, 5))
  3689. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3690. }
  3691. }
  3692. static int
  3693. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3694. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3695. int src_w, int src_h, int dst_w, int dst_h)
  3696. {
  3697. struct intel_crtc_scaler_state *scaler_state =
  3698. &crtc_state->scaler_state;
  3699. struct intel_crtc *intel_crtc =
  3700. to_intel_crtc(crtc_state->base.crtc);
  3701. int need_scaling;
  3702. need_scaling = intel_rotation_90_or_270(rotation) ?
  3703. (src_h != dst_w || src_w != dst_h):
  3704. (src_w != dst_w || src_h != dst_h);
  3705. /*
  3706. * if plane is being disabled or scaler is no more required or force detach
  3707. * - free scaler binded to this plane/crtc
  3708. * - in order to do this, update crtc->scaler_usage
  3709. *
  3710. * Here scaler state in crtc_state is set free so that
  3711. * scaler can be assigned to other user. Actual register
  3712. * update to free the scaler is done in plane/panel-fit programming.
  3713. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3714. */
  3715. if (force_detach || !need_scaling) {
  3716. if (*scaler_id >= 0) {
  3717. scaler_state->scaler_users &= ~(1 << scaler_user);
  3718. scaler_state->scalers[*scaler_id].in_use = 0;
  3719. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3720. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3721. intel_crtc->pipe, scaler_user, *scaler_id,
  3722. scaler_state->scaler_users);
  3723. *scaler_id = -1;
  3724. }
  3725. return 0;
  3726. }
  3727. /* range checks */
  3728. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3729. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3730. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3731. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3732. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3733. "size is out of scaler range\n",
  3734. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3735. return -EINVAL;
  3736. }
  3737. /* mark this plane as a scaler user in crtc_state */
  3738. scaler_state->scaler_users |= (1 << scaler_user);
  3739. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3740. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3741. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3742. scaler_state->scaler_users);
  3743. return 0;
  3744. }
  3745. /**
  3746. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3747. *
  3748. * @state: crtc's scaler state
  3749. *
  3750. * Return
  3751. * 0 - scaler_usage updated successfully
  3752. * error - requested scaling cannot be supported or other error condition
  3753. */
  3754. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3755. {
  3756. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3757. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3758. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3759. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3760. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3761. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3762. state->pipe_src_w, state->pipe_src_h,
  3763. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3764. }
  3765. /**
  3766. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3767. *
  3768. * @state: crtc's scaler state
  3769. * @plane_state: atomic plane state to update
  3770. *
  3771. * Return
  3772. * 0 - scaler_usage updated successfully
  3773. * error - requested scaling cannot be supported or other error condition
  3774. */
  3775. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3776. struct intel_plane_state *plane_state)
  3777. {
  3778. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3779. struct intel_plane *intel_plane =
  3780. to_intel_plane(plane_state->base.plane);
  3781. struct drm_framebuffer *fb = plane_state->base.fb;
  3782. int ret;
  3783. bool force_detach = !fb || !plane_state->visible;
  3784. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3785. intel_plane->base.base.id, intel_crtc->pipe,
  3786. drm_plane_index(&intel_plane->base));
  3787. ret = skl_update_scaler(crtc_state, force_detach,
  3788. drm_plane_index(&intel_plane->base),
  3789. &plane_state->scaler_id,
  3790. plane_state->base.rotation,
  3791. drm_rect_width(&plane_state->src) >> 16,
  3792. drm_rect_height(&plane_state->src) >> 16,
  3793. drm_rect_width(&plane_state->dst),
  3794. drm_rect_height(&plane_state->dst));
  3795. if (ret || plane_state->scaler_id < 0)
  3796. return ret;
  3797. /* check colorkey */
  3798. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3799. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3800. intel_plane->base.base.id);
  3801. return -EINVAL;
  3802. }
  3803. /* Check src format */
  3804. switch (fb->pixel_format) {
  3805. case DRM_FORMAT_RGB565:
  3806. case DRM_FORMAT_XBGR8888:
  3807. case DRM_FORMAT_XRGB8888:
  3808. case DRM_FORMAT_ABGR8888:
  3809. case DRM_FORMAT_ARGB8888:
  3810. case DRM_FORMAT_XRGB2101010:
  3811. case DRM_FORMAT_XBGR2101010:
  3812. case DRM_FORMAT_YUYV:
  3813. case DRM_FORMAT_YVYU:
  3814. case DRM_FORMAT_UYVY:
  3815. case DRM_FORMAT_VYUY:
  3816. break;
  3817. default:
  3818. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3819. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3820. return -EINVAL;
  3821. }
  3822. return 0;
  3823. }
  3824. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3825. {
  3826. int i;
  3827. for (i = 0; i < crtc->num_scalers; i++)
  3828. skl_detach_scaler(crtc, i);
  3829. }
  3830. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3831. {
  3832. struct drm_device *dev = crtc->base.dev;
  3833. struct drm_i915_private *dev_priv = dev->dev_private;
  3834. int pipe = crtc->pipe;
  3835. struct intel_crtc_scaler_state *scaler_state =
  3836. &crtc->config->scaler_state;
  3837. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3838. if (crtc->config->pch_pfit.enabled) {
  3839. int id;
  3840. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3841. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3842. return;
  3843. }
  3844. id = scaler_state->scaler_id;
  3845. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3846. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3847. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3848. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3849. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3850. }
  3851. }
  3852. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3853. {
  3854. struct drm_device *dev = crtc->base.dev;
  3855. struct drm_i915_private *dev_priv = dev->dev_private;
  3856. int pipe = crtc->pipe;
  3857. if (crtc->config->pch_pfit.enabled) {
  3858. /* Force use of hard-coded filter coefficients
  3859. * as some pre-programmed values are broken,
  3860. * e.g. x201.
  3861. */
  3862. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3863. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3864. PF_PIPE_SEL_IVB(pipe));
  3865. else
  3866. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3867. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3868. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3869. }
  3870. }
  3871. void hsw_enable_ips(struct intel_crtc *crtc)
  3872. {
  3873. struct drm_device *dev = crtc->base.dev;
  3874. struct drm_i915_private *dev_priv = dev->dev_private;
  3875. if (!crtc->config->ips_enabled)
  3876. return;
  3877. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3878. intel_wait_for_vblank(dev, crtc->pipe);
  3879. assert_plane_enabled(dev_priv, crtc->plane);
  3880. if (IS_BROADWELL(dev)) {
  3881. mutex_lock(&dev_priv->rps.hw_lock);
  3882. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3883. mutex_unlock(&dev_priv->rps.hw_lock);
  3884. /* Quoting Art Runyan: "its not safe to expect any particular
  3885. * value in IPS_CTL bit 31 after enabling IPS through the
  3886. * mailbox." Moreover, the mailbox may return a bogus state,
  3887. * so we need to just enable it and continue on.
  3888. */
  3889. } else {
  3890. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3891. /* The bit only becomes 1 in the next vblank, so this wait here
  3892. * is essentially intel_wait_for_vblank. If we don't have this
  3893. * and don't wait for vblanks until the end of crtc_enable, then
  3894. * the HW state readout code will complain that the expected
  3895. * IPS_CTL value is not the one we read. */
  3896. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3897. DRM_ERROR("Timed out waiting for IPS enable\n");
  3898. }
  3899. }
  3900. void hsw_disable_ips(struct intel_crtc *crtc)
  3901. {
  3902. struct drm_device *dev = crtc->base.dev;
  3903. struct drm_i915_private *dev_priv = dev->dev_private;
  3904. if (!crtc->config->ips_enabled)
  3905. return;
  3906. assert_plane_enabled(dev_priv, crtc->plane);
  3907. if (IS_BROADWELL(dev)) {
  3908. mutex_lock(&dev_priv->rps.hw_lock);
  3909. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3910. mutex_unlock(&dev_priv->rps.hw_lock);
  3911. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3912. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3913. DRM_ERROR("Timed out waiting for IPS disable\n");
  3914. } else {
  3915. I915_WRITE(IPS_CTL, 0);
  3916. POSTING_READ(IPS_CTL);
  3917. }
  3918. /* We need to wait for a vblank before we can disable the plane. */
  3919. intel_wait_for_vblank(dev, crtc->pipe);
  3920. }
  3921. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3922. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3923. {
  3924. struct drm_device *dev = crtc->dev;
  3925. struct drm_i915_private *dev_priv = dev->dev_private;
  3926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3927. enum pipe pipe = intel_crtc->pipe;
  3928. int i;
  3929. bool reenable_ips = false;
  3930. /* The clocks have to be on to load the palette. */
  3931. if (!crtc->state->active)
  3932. return;
  3933. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3934. if (intel_crtc->config->has_dsi_encoder)
  3935. assert_dsi_pll_enabled(dev_priv);
  3936. else
  3937. assert_pll_enabled(dev_priv, pipe);
  3938. }
  3939. /* Workaround : Do not read or write the pipe palette/gamma data while
  3940. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3941. */
  3942. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3943. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3944. GAMMA_MODE_MODE_SPLIT)) {
  3945. hsw_disable_ips(intel_crtc);
  3946. reenable_ips = true;
  3947. }
  3948. for (i = 0; i < 256; i++) {
  3949. i915_reg_t palreg;
  3950. if (HAS_GMCH_DISPLAY(dev))
  3951. palreg = PALETTE(pipe, i);
  3952. else
  3953. palreg = LGC_PALETTE(pipe, i);
  3954. I915_WRITE(palreg,
  3955. (intel_crtc->lut_r[i] << 16) |
  3956. (intel_crtc->lut_g[i] << 8) |
  3957. intel_crtc->lut_b[i]);
  3958. }
  3959. if (reenable_ips)
  3960. hsw_enable_ips(intel_crtc);
  3961. }
  3962. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3963. {
  3964. if (intel_crtc->overlay) {
  3965. struct drm_device *dev = intel_crtc->base.dev;
  3966. struct drm_i915_private *dev_priv = dev->dev_private;
  3967. mutex_lock(&dev->struct_mutex);
  3968. dev_priv->mm.interruptible = false;
  3969. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3970. dev_priv->mm.interruptible = true;
  3971. mutex_unlock(&dev->struct_mutex);
  3972. }
  3973. /* Let userspace switch the overlay on again. In most cases userspace
  3974. * has to recompute where to put it anyway.
  3975. */
  3976. }
  3977. /**
  3978. * intel_post_enable_primary - Perform operations after enabling primary plane
  3979. * @crtc: the CRTC whose primary plane was just enabled
  3980. *
  3981. * Performs potentially sleeping operations that must be done after the primary
  3982. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3983. * called due to an explicit primary plane update, or due to an implicit
  3984. * re-enable that is caused when a sprite plane is updated to no longer
  3985. * completely hide the primary plane.
  3986. */
  3987. static void
  3988. intel_post_enable_primary(struct drm_crtc *crtc)
  3989. {
  3990. struct drm_device *dev = crtc->dev;
  3991. struct drm_i915_private *dev_priv = dev->dev_private;
  3992. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3993. int pipe = intel_crtc->pipe;
  3994. /*
  3995. * FIXME IPS should be fine as long as one plane is
  3996. * enabled, but in practice it seems to have problems
  3997. * when going from primary only to sprite only and vice
  3998. * versa.
  3999. */
  4000. hsw_enable_ips(intel_crtc);
  4001. /*
  4002. * Gen2 reports pipe underruns whenever all planes are disabled.
  4003. * So don't enable underrun reporting before at least some planes
  4004. * are enabled.
  4005. * FIXME: Need to fix the logic to work when we turn off all planes
  4006. * but leave the pipe running.
  4007. */
  4008. if (IS_GEN2(dev))
  4009. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4010. /* Underruns don't always raise interrupts, so check manually. */
  4011. intel_check_cpu_fifo_underruns(dev_priv);
  4012. intel_check_pch_fifo_underruns(dev_priv);
  4013. }
  4014. /**
  4015. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4016. * @crtc: the CRTC whose primary plane is to be disabled
  4017. *
  4018. * Performs potentially sleeping operations that must be done before the
  4019. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4020. * be called due to an explicit primary plane update, or due to an implicit
  4021. * disable that is caused when a sprite plane completely hides the primary
  4022. * plane.
  4023. */
  4024. static void
  4025. intel_pre_disable_primary(struct drm_crtc *crtc)
  4026. {
  4027. struct drm_device *dev = crtc->dev;
  4028. struct drm_i915_private *dev_priv = dev->dev_private;
  4029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4030. int pipe = intel_crtc->pipe;
  4031. /*
  4032. * Gen2 reports pipe underruns whenever all planes are disabled.
  4033. * So diasble underrun reporting before all the planes get disabled.
  4034. * FIXME: Need to fix the logic to work when we turn off all planes
  4035. * but leave the pipe running.
  4036. */
  4037. if (IS_GEN2(dev))
  4038. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4039. /*
  4040. * Vblank time updates from the shadow to live plane control register
  4041. * are blocked if the memory self-refresh mode is active at that
  4042. * moment. So to make sure the plane gets truly disabled, disable
  4043. * first the self-refresh mode. The self-refresh enable bit in turn
  4044. * will be checked/applied by the HW only at the next frame start
  4045. * event which is after the vblank start event, so we need to have a
  4046. * wait-for-vblank between disabling the plane and the pipe.
  4047. */
  4048. if (HAS_GMCH_DISPLAY(dev)) {
  4049. intel_set_memory_cxsr(dev_priv, false);
  4050. dev_priv->wm.vlv.cxsr = false;
  4051. intel_wait_for_vblank(dev, pipe);
  4052. }
  4053. /*
  4054. * FIXME IPS should be fine as long as one plane is
  4055. * enabled, but in practice it seems to have problems
  4056. * when going from primary only to sprite only and vice
  4057. * versa.
  4058. */
  4059. hsw_disable_ips(intel_crtc);
  4060. }
  4061. static void intel_post_plane_update(struct intel_crtc *crtc)
  4062. {
  4063. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4064. struct intel_crtc_state *pipe_config =
  4065. to_intel_crtc_state(crtc->base.state);
  4066. struct drm_device *dev = crtc->base.dev;
  4067. if (atomic->wait_vblank)
  4068. intel_wait_for_vblank(dev, crtc->pipe);
  4069. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4070. crtc->wm.cxsr_allowed = true;
  4071. if (pipe_config->wm_changed && pipe_config->base.active)
  4072. intel_update_watermarks(&crtc->base);
  4073. if (atomic->update_fbc)
  4074. intel_fbc_post_update(crtc);
  4075. if (atomic->post_enable_primary)
  4076. intel_post_enable_primary(&crtc->base);
  4077. memset(atomic, 0, sizeof(*atomic));
  4078. }
  4079. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4080. {
  4081. struct drm_device *dev = crtc->base.dev;
  4082. struct drm_i915_private *dev_priv = dev->dev_private;
  4083. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4084. struct intel_crtc_state *pipe_config =
  4085. to_intel_crtc_state(crtc->base.state);
  4086. if (atomic->update_fbc)
  4087. intel_fbc_pre_update(crtc);
  4088. if (crtc->atomic.disable_ips)
  4089. hsw_disable_ips(crtc);
  4090. if (atomic->pre_disable_primary)
  4091. intel_pre_disable_primary(&crtc->base);
  4092. if (pipe_config->disable_cxsr) {
  4093. crtc->wm.cxsr_allowed = false;
  4094. intel_set_memory_cxsr(dev_priv, false);
  4095. }
  4096. if (!needs_modeset(&pipe_config->base) && pipe_config->wm_changed)
  4097. intel_update_watermarks(&crtc->base);
  4098. }
  4099. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4100. {
  4101. struct drm_device *dev = crtc->dev;
  4102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4103. struct drm_plane *p;
  4104. int pipe = intel_crtc->pipe;
  4105. intel_crtc_dpms_overlay_disable(intel_crtc);
  4106. drm_for_each_plane_mask(p, dev, plane_mask)
  4107. to_intel_plane(p)->disable_plane(p, crtc);
  4108. /*
  4109. * FIXME: Once we grow proper nuclear flip support out of this we need
  4110. * to compute the mask of flip planes precisely. For the time being
  4111. * consider this a flip to a NULL plane.
  4112. */
  4113. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4114. }
  4115. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4116. {
  4117. struct drm_device *dev = crtc->dev;
  4118. struct drm_i915_private *dev_priv = dev->dev_private;
  4119. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4120. struct intel_encoder *encoder;
  4121. int pipe = intel_crtc->pipe;
  4122. if (WARN_ON(intel_crtc->active))
  4123. return;
  4124. if (intel_crtc->config->has_pch_encoder)
  4125. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4126. if (intel_crtc->config->has_pch_encoder)
  4127. intel_prepare_shared_dpll(intel_crtc);
  4128. if (intel_crtc->config->has_dp_encoder)
  4129. intel_dp_set_m_n(intel_crtc, M1_N1);
  4130. intel_set_pipe_timings(intel_crtc);
  4131. if (intel_crtc->config->has_pch_encoder) {
  4132. intel_cpu_transcoder_set_m_n(intel_crtc,
  4133. &intel_crtc->config->fdi_m_n, NULL);
  4134. }
  4135. ironlake_set_pipeconf(crtc);
  4136. intel_crtc->active = true;
  4137. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4138. for_each_encoder_on_crtc(dev, crtc, encoder)
  4139. if (encoder->pre_enable)
  4140. encoder->pre_enable(encoder);
  4141. if (intel_crtc->config->has_pch_encoder) {
  4142. /* Note: FDI PLL enabling _must_ be done before we enable the
  4143. * cpu pipes, hence this is separate from all the other fdi/pch
  4144. * enabling. */
  4145. ironlake_fdi_pll_enable(intel_crtc);
  4146. } else {
  4147. assert_fdi_tx_disabled(dev_priv, pipe);
  4148. assert_fdi_rx_disabled(dev_priv, pipe);
  4149. }
  4150. ironlake_pfit_enable(intel_crtc);
  4151. /*
  4152. * On ILK+ LUT must be loaded before the pipe is running but with
  4153. * clocks enabled
  4154. */
  4155. intel_crtc_load_lut(crtc);
  4156. intel_update_watermarks(crtc);
  4157. intel_enable_pipe(intel_crtc);
  4158. if (intel_crtc->config->has_pch_encoder)
  4159. ironlake_pch_enable(crtc);
  4160. assert_vblank_disabled(crtc);
  4161. drm_crtc_vblank_on(crtc);
  4162. for_each_encoder_on_crtc(dev, crtc, encoder)
  4163. encoder->enable(encoder);
  4164. if (HAS_PCH_CPT(dev))
  4165. cpt_verify_modeset(dev, intel_crtc->pipe);
  4166. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4167. if (intel_crtc->config->has_pch_encoder)
  4168. intel_wait_for_vblank(dev, pipe);
  4169. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4170. intel_fbc_enable(intel_crtc);
  4171. }
  4172. /* IPS only exists on ULT machines and is tied to pipe A. */
  4173. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4174. {
  4175. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4176. }
  4177. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4178. {
  4179. struct drm_device *dev = crtc->dev;
  4180. struct drm_i915_private *dev_priv = dev->dev_private;
  4181. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4182. struct intel_encoder *encoder;
  4183. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4184. struct intel_crtc_state *pipe_config =
  4185. to_intel_crtc_state(crtc->state);
  4186. if (WARN_ON(intel_crtc->active))
  4187. return;
  4188. if (intel_crtc->config->has_pch_encoder)
  4189. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4190. false);
  4191. if (intel_crtc_to_shared_dpll(intel_crtc))
  4192. intel_enable_shared_dpll(intel_crtc);
  4193. if (intel_crtc->config->has_dp_encoder)
  4194. intel_dp_set_m_n(intel_crtc, M1_N1);
  4195. intel_set_pipe_timings(intel_crtc);
  4196. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4197. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4198. intel_crtc->config->pixel_multiplier - 1);
  4199. }
  4200. if (intel_crtc->config->has_pch_encoder) {
  4201. intel_cpu_transcoder_set_m_n(intel_crtc,
  4202. &intel_crtc->config->fdi_m_n, NULL);
  4203. }
  4204. haswell_set_pipeconf(crtc);
  4205. intel_set_pipe_csc(crtc);
  4206. intel_crtc->active = true;
  4207. if (intel_crtc->config->has_pch_encoder)
  4208. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4209. else
  4210. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4211. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4212. if (encoder->pre_enable)
  4213. encoder->pre_enable(encoder);
  4214. }
  4215. if (intel_crtc->config->has_pch_encoder)
  4216. dev_priv->display.fdi_link_train(crtc);
  4217. if (!intel_crtc->config->has_dsi_encoder)
  4218. intel_ddi_enable_pipe_clock(intel_crtc);
  4219. if (INTEL_INFO(dev)->gen >= 9)
  4220. skylake_pfit_enable(intel_crtc);
  4221. else
  4222. ironlake_pfit_enable(intel_crtc);
  4223. /*
  4224. * On ILK+ LUT must be loaded before the pipe is running but with
  4225. * clocks enabled
  4226. */
  4227. intel_crtc_load_lut(crtc);
  4228. intel_ddi_set_pipe_settings(crtc);
  4229. if (!intel_crtc->config->has_dsi_encoder)
  4230. intel_ddi_enable_transcoder_func(crtc);
  4231. intel_update_watermarks(crtc);
  4232. intel_enable_pipe(intel_crtc);
  4233. if (intel_crtc->config->has_pch_encoder)
  4234. lpt_pch_enable(crtc);
  4235. if (intel_crtc->config->dp_encoder_is_mst)
  4236. intel_ddi_set_vc_payload_alloc(crtc, true);
  4237. assert_vblank_disabled(crtc);
  4238. drm_crtc_vblank_on(crtc);
  4239. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4240. encoder->enable(encoder);
  4241. intel_opregion_notify_encoder(encoder, true);
  4242. }
  4243. if (intel_crtc->config->has_pch_encoder) {
  4244. intel_wait_for_vblank(dev, pipe);
  4245. intel_wait_for_vblank(dev, pipe);
  4246. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4247. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4248. true);
  4249. }
  4250. /* If we change the relative order between pipe/planes enabling, we need
  4251. * to change the workaround. */
  4252. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4253. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4254. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4255. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4256. }
  4257. intel_fbc_enable(intel_crtc);
  4258. }
  4259. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4260. {
  4261. struct drm_device *dev = crtc->base.dev;
  4262. struct drm_i915_private *dev_priv = dev->dev_private;
  4263. int pipe = crtc->pipe;
  4264. /* To avoid upsetting the power well on haswell only disable the pfit if
  4265. * it's in use. The hw state code will make sure we get this right. */
  4266. if (force || crtc->config->pch_pfit.enabled) {
  4267. I915_WRITE(PF_CTL(pipe), 0);
  4268. I915_WRITE(PF_WIN_POS(pipe), 0);
  4269. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4270. }
  4271. }
  4272. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4273. {
  4274. struct drm_device *dev = crtc->dev;
  4275. struct drm_i915_private *dev_priv = dev->dev_private;
  4276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4277. struct intel_encoder *encoder;
  4278. int pipe = intel_crtc->pipe;
  4279. if (intel_crtc->config->has_pch_encoder)
  4280. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4281. for_each_encoder_on_crtc(dev, crtc, encoder)
  4282. encoder->disable(encoder);
  4283. drm_crtc_vblank_off(crtc);
  4284. assert_vblank_disabled(crtc);
  4285. /*
  4286. * Sometimes spurious CPU pipe underruns happen when the
  4287. * pipe is already disabled, but FDI RX/TX is still enabled.
  4288. * Happens at least with VGA+HDMI cloning. Suppress them.
  4289. */
  4290. if (intel_crtc->config->has_pch_encoder)
  4291. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4292. intel_disable_pipe(intel_crtc);
  4293. ironlake_pfit_disable(intel_crtc, false);
  4294. if (intel_crtc->config->has_pch_encoder) {
  4295. ironlake_fdi_disable(crtc);
  4296. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4297. }
  4298. for_each_encoder_on_crtc(dev, crtc, encoder)
  4299. if (encoder->post_disable)
  4300. encoder->post_disable(encoder);
  4301. if (intel_crtc->config->has_pch_encoder) {
  4302. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4303. if (HAS_PCH_CPT(dev)) {
  4304. i915_reg_t reg;
  4305. u32 temp;
  4306. /* disable TRANS_DP_CTL */
  4307. reg = TRANS_DP_CTL(pipe);
  4308. temp = I915_READ(reg);
  4309. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4310. TRANS_DP_PORT_SEL_MASK);
  4311. temp |= TRANS_DP_PORT_SEL_NONE;
  4312. I915_WRITE(reg, temp);
  4313. /* disable DPLL_SEL */
  4314. temp = I915_READ(PCH_DPLL_SEL);
  4315. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4316. I915_WRITE(PCH_DPLL_SEL, temp);
  4317. }
  4318. ironlake_fdi_pll_disable(intel_crtc);
  4319. }
  4320. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4321. intel_fbc_disable_crtc(intel_crtc);
  4322. }
  4323. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4324. {
  4325. struct drm_device *dev = crtc->dev;
  4326. struct drm_i915_private *dev_priv = dev->dev_private;
  4327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4328. struct intel_encoder *encoder;
  4329. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4330. if (intel_crtc->config->has_pch_encoder)
  4331. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4332. false);
  4333. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4334. intel_opregion_notify_encoder(encoder, false);
  4335. encoder->disable(encoder);
  4336. }
  4337. drm_crtc_vblank_off(crtc);
  4338. assert_vblank_disabled(crtc);
  4339. intel_disable_pipe(intel_crtc);
  4340. if (intel_crtc->config->dp_encoder_is_mst)
  4341. intel_ddi_set_vc_payload_alloc(crtc, false);
  4342. if (!intel_crtc->config->has_dsi_encoder)
  4343. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4344. if (INTEL_INFO(dev)->gen >= 9)
  4345. skylake_scaler_disable(intel_crtc);
  4346. else
  4347. ironlake_pfit_disable(intel_crtc, false);
  4348. if (!intel_crtc->config->has_dsi_encoder)
  4349. intel_ddi_disable_pipe_clock(intel_crtc);
  4350. for_each_encoder_on_crtc(dev, crtc, encoder)
  4351. if (encoder->post_disable)
  4352. encoder->post_disable(encoder);
  4353. if (intel_crtc->config->has_pch_encoder) {
  4354. lpt_disable_pch_transcoder(dev_priv);
  4355. lpt_disable_iclkip(dev_priv);
  4356. intel_ddi_fdi_disable(crtc);
  4357. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4358. true);
  4359. }
  4360. intel_fbc_disable_crtc(intel_crtc);
  4361. }
  4362. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4363. {
  4364. struct drm_device *dev = crtc->base.dev;
  4365. struct drm_i915_private *dev_priv = dev->dev_private;
  4366. struct intel_crtc_state *pipe_config = crtc->config;
  4367. if (!pipe_config->gmch_pfit.control)
  4368. return;
  4369. /*
  4370. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4371. * according to register description and PRM.
  4372. */
  4373. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4374. assert_pipe_disabled(dev_priv, crtc->pipe);
  4375. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4376. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4377. /* Border color in case we don't scale up to the full screen. Black by
  4378. * default, change to something else for debugging. */
  4379. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4380. }
  4381. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4382. {
  4383. switch (port) {
  4384. case PORT_A:
  4385. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4386. case PORT_B:
  4387. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4388. case PORT_C:
  4389. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4390. case PORT_D:
  4391. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4392. case PORT_E:
  4393. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4394. default:
  4395. MISSING_CASE(port);
  4396. return POWER_DOMAIN_PORT_OTHER;
  4397. }
  4398. }
  4399. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4400. {
  4401. switch (port) {
  4402. case PORT_A:
  4403. return POWER_DOMAIN_AUX_A;
  4404. case PORT_B:
  4405. return POWER_DOMAIN_AUX_B;
  4406. case PORT_C:
  4407. return POWER_DOMAIN_AUX_C;
  4408. case PORT_D:
  4409. return POWER_DOMAIN_AUX_D;
  4410. case PORT_E:
  4411. /* FIXME: Check VBT for actual wiring of PORT E */
  4412. return POWER_DOMAIN_AUX_D;
  4413. default:
  4414. MISSING_CASE(port);
  4415. return POWER_DOMAIN_AUX_A;
  4416. }
  4417. }
  4418. enum intel_display_power_domain
  4419. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4420. {
  4421. struct drm_device *dev = intel_encoder->base.dev;
  4422. struct intel_digital_port *intel_dig_port;
  4423. switch (intel_encoder->type) {
  4424. case INTEL_OUTPUT_UNKNOWN:
  4425. /* Only DDI platforms should ever use this output type */
  4426. WARN_ON_ONCE(!HAS_DDI(dev));
  4427. case INTEL_OUTPUT_DISPLAYPORT:
  4428. case INTEL_OUTPUT_HDMI:
  4429. case INTEL_OUTPUT_EDP:
  4430. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4431. return port_to_power_domain(intel_dig_port->port);
  4432. case INTEL_OUTPUT_DP_MST:
  4433. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4434. return port_to_power_domain(intel_dig_port->port);
  4435. case INTEL_OUTPUT_ANALOG:
  4436. return POWER_DOMAIN_PORT_CRT;
  4437. case INTEL_OUTPUT_DSI:
  4438. return POWER_DOMAIN_PORT_DSI;
  4439. default:
  4440. return POWER_DOMAIN_PORT_OTHER;
  4441. }
  4442. }
  4443. enum intel_display_power_domain
  4444. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4445. {
  4446. struct drm_device *dev = intel_encoder->base.dev;
  4447. struct intel_digital_port *intel_dig_port;
  4448. switch (intel_encoder->type) {
  4449. case INTEL_OUTPUT_UNKNOWN:
  4450. case INTEL_OUTPUT_HDMI:
  4451. /*
  4452. * Only DDI platforms should ever use these output types.
  4453. * We can get here after the HDMI detect code has already set
  4454. * the type of the shared encoder. Since we can't be sure
  4455. * what's the status of the given connectors, play safe and
  4456. * run the DP detection too.
  4457. */
  4458. WARN_ON_ONCE(!HAS_DDI(dev));
  4459. case INTEL_OUTPUT_DISPLAYPORT:
  4460. case INTEL_OUTPUT_EDP:
  4461. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4462. return port_to_aux_power_domain(intel_dig_port->port);
  4463. case INTEL_OUTPUT_DP_MST:
  4464. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4465. return port_to_aux_power_domain(intel_dig_port->port);
  4466. default:
  4467. MISSING_CASE(intel_encoder->type);
  4468. return POWER_DOMAIN_AUX_A;
  4469. }
  4470. }
  4471. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4472. {
  4473. struct drm_device *dev = crtc->dev;
  4474. struct intel_encoder *intel_encoder;
  4475. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4476. enum pipe pipe = intel_crtc->pipe;
  4477. unsigned long mask;
  4478. enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
  4479. if (!crtc->state->active)
  4480. return 0;
  4481. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4482. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4483. if (intel_crtc->config->pch_pfit.enabled ||
  4484. intel_crtc->config->pch_pfit.force_thru)
  4485. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4486. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4487. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4488. return mask;
  4489. }
  4490. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4491. {
  4492. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4493. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4494. enum intel_display_power_domain domain;
  4495. unsigned long domains, new_domains, old_domains;
  4496. old_domains = intel_crtc->enabled_power_domains;
  4497. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4498. domains = new_domains & ~old_domains;
  4499. for_each_power_domain(domain, domains)
  4500. intel_display_power_get(dev_priv, domain);
  4501. return old_domains & ~new_domains;
  4502. }
  4503. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4504. unsigned long domains)
  4505. {
  4506. enum intel_display_power_domain domain;
  4507. for_each_power_domain(domain, domains)
  4508. intel_display_power_put(dev_priv, domain);
  4509. }
  4510. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4511. {
  4512. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4513. struct drm_device *dev = state->dev;
  4514. struct drm_i915_private *dev_priv = dev->dev_private;
  4515. unsigned long put_domains[I915_MAX_PIPES] = {};
  4516. struct drm_crtc_state *crtc_state;
  4517. struct drm_crtc *crtc;
  4518. int i;
  4519. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4520. if (needs_modeset(crtc->state))
  4521. put_domains[to_intel_crtc(crtc)->pipe] =
  4522. modeset_get_crtc_power_domains(crtc);
  4523. }
  4524. if (dev_priv->display.modeset_commit_cdclk &&
  4525. intel_state->dev_cdclk != dev_priv->cdclk_freq)
  4526. dev_priv->display.modeset_commit_cdclk(state);
  4527. for (i = 0; i < I915_MAX_PIPES; i++)
  4528. if (put_domains[i])
  4529. modeset_put_power_domains(dev_priv, put_domains[i]);
  4530. }
  4531. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4532. {
  4533. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4534. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4535. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4536. return max_cdclk_freq;
  4537. else if (IS_CHERRYVIEW(dev_priv))
  4538. return max_cdclk_freq*95/100;
  4539. else if (INTEL_INFO(dev_priv)->gen < 4)
  4540. return 2*max_cdclk_freq*90/100;
  4541. else
  4542. return max_cdclk_freq*90/100;
  4543. }
  4544. static void intel_update_max_cdclk(struct drm_device *dev)
  4545. {
  4546. struct drm_i915_private *dev_priv = dev->dev_private;
  4547. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4548. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4549. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4550. dev_priv->max_cdclk_freq = 675000;
  4551. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4552. dev_priv->max_cdclk_freq = 540000;
  4553. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4554. dev_priv->max_cdclk_freq = 450000;
  4555. else
  4556. dev_priv->max_cdclk_freq = 337500;
  4557. } else if (IS_BROADWELL(dev)) {
  4558. /*
  4559. * FIXME with extra cooling we can allow
  4560. * 540 MHz for ULX and 675 Mhz for ULT.
  4561. * How can we know if extra cooling is
  4562. * available? PCI ID, VTB, something else?
  4563. */
  4564. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4565. dev_priv->max_cdclk_freq = 450000;
  4566. else if (IS_BDW_ULX(dev))
  4567. dev_priv->max_cdclk_freq = 450000;
  4568. else if (IS_BDW_ULT(dev))
  4569. dev_priv->max_cdclk_freq = 540000;
  4570. else
  4571. dev_priv->max_cdclk_freq = 675000;
  4572. } else if (IS_CHERRYVIEW(dev)) {
  4573. dev_priv->max_cdclk_freq = 320000;
  4574. } else if (IS_VALLEYVIEW(dev)) {
  4575. dev_priv->max_cdclk_freq = 400000;
  4576. } else {
  4577. /* otherwise assume cdclk is fixed */
  4578. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4579. }
  4580. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4581. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4582. dev_priv->max_cdclk_freq);
  4583. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4584. dev_priv->max_dotclk_freq);
  4585. }
  4586. static void intel_update_cdclk(struct drm_device *dev)
  4587. {
  4588. struct drm_i915_private *dev_priv = dev->dev_private;
  4589. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4590. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4591. dev_priv->cdclk_freq);
  4592. /*
  4593. * Program the gmbus_freq based on the cdclk frequency.
  4594. * BSpec erroneously claims we should aim for 4MHz, but
  4595. * in fact 1MHz is the correct frequency.
  4596. */
  4597. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  4598. /*
  4599. * Program the gmbus_freq based on the cdclk frequency.
  4600. * BSpec erroneously claims we should aim for 4MHz, but
  4601. * in fact 1MHz is the correct frequency.
  4602. */
  4603. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4604. }
  4605. if (dev_priv->max_cdclk_freq == 0)
  4606. intel_update_max_cdclk(dev);
  4607. }
  4608. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4609. {
  4610. struct drm_i915_private *dev_priv = dev->dev_private;
  4611. uint32_t divider;
  4612. uint32_t ratio;
  4613. uint32_t current_freq;
  4614. int ret;
  4615. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4616. switch (frequency) {
  4617. case 144000:
  4618. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4619. ratio = BXT_DE_PLL_RATIO(60);
  4620. break;
  4621. case 288000:
  4622. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4623. ratio = BXT_DE_PLL_RATIO(60);
  4624. break;
  4625. case 384000:
  4626. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4627. ratio = BXT_DE_PLL_RATIO(60);
  4628. break;
  4629. case 576000:
  4630. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4631. ratio = BXT_DE_PLL_RATIO(60);
  4632. break;
  4633. case 624000:
  4634. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4635. ratio = BXT_DE_PLL_RATIO(65);
  4636. break;
  4637. case 19200:
  4638. /*
  4639. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4640. * to suppress GCC warning.
  4641. */
  4642. ratio = 0;
  4643. divider = 0;
  4644. break;
  4645. default:
  4646. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4647. return;
  4648. }
  4649. mutex_lock(&dev_priv->rps.hw_lock);
  4650. /* Inform power controller of upcoming frequency change */
  4651. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4652. 0x80000000);
  4653. mutex_unlock(&dev_priv->rps.hw_lock);
  4654. if (ret) {
  4655. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4656. ret, frequency);
  4657. return;
  4658. }
  4659. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4660. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4661. current_freq = current_freq * 500 + 1000;
  4662. /*
  4663. * DE PLL has to be disabled when
  4664. * - setting to 19.2MHz (bypass, PLL isn't used)
  4665. * - before setting to 624MHz (PLL needs toggling)
  4666. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4667. */
  4668. if (frequency == 19200 || frequency == 624000 ||
  4669. current_freq == 624000) {
  4670. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4671. /* Timeout 200us */
  4672. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4673. 1))
  4674. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4675. }
  4676. if (frequency != 19200) {
  4677. uint32_t val;
  4678. val = I915_READ(BXT_DE_PLL_CTL);
  4679. val &= ~BXT_DE_PLL_RATIO_MASK;
  4680. val |= ratio;
  4681. I915_WRITE(BXT_DE_PLL_CTL, val);
  4682. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4683. /* Timeout 200us */
  4684. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4685. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4686. val = I915_READ(CDCLK_CTL);
  4687. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4688. val |= divider;
  4689. /*
  4690. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4691. * enable otherwise.
  4692. */
  4693. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4694. if (frequency >= 500000)
  4695. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4696. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4697. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4698. val |= (frequency - 1000) / 500;
  4699. I915_WRITE(CDCLK_CTL, val);
  4700. }
  4701. mutex_lock(&dev_priv->rps.hw_lock);
  4702. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4703. DIV_ROUND_UP(frequency, 25000));
  4704. mutex_unlock(&dev_priv->rps.hw_lock);
  4705. if (ret) {
  4706. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4707. ret, frequency);
  4708. return;
  4709. }
  4710. intel_update_cdclk(dev);
  4711. }
  4712. void broxton_init_cdclk(struct drm_device *dev)
  4713. {
  4714. struct drm_i915_private *dev_priv = dev->dev_private;
  4715. uint32_t val;
  4716. /*
  4717. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4718. * or else the reset will hang because there is no PCH to respond.
  4719. * Move the handshake programming to initialization sequence.
  4720. * Previously was left up to BIOS.
  4721. */
  4722. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4723. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4724. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4725. /* Enable PG1 for cdclk */
  4726. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4727. /* check if cd clock is enabled */
  4728. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4729. DRM_DEBUG_KMS("Display already initialized\n");
  4730. return;
  4731. }
  4732. /*
  4733. * FIXME:
  4734. * - The initial CDCLK needs to be read from VBT.
  4735. * Need to make this change after VBT has changes for BXT.
  4736. * - check if setting the max (or any) cdclk freq is really necessary
  4737. * here, it belongs to modeset time
  4738. */
  4739. broxton_set_cdclk(dev, 624000);
  4740. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4741. POSTING_READ(DBUF_CTL);
  4742. udelay(10);
  4743. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4744. DRM_ERROR("DBuf power enable timeout!\n");
  4745. }
  4746. void broxton_uninit_cdclk(struct drm_device *dev)
  4747. {
  4748. struct drm_i915_private *dev_priv = dev->dev_private;
  4749. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4750. POSTING_READ(DBUF_CTL);
  4751. udelay(10);
  4752. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4753. DRM_ERROR("DBuf power disable timeout!\n");
  4754. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4755. broxton_set_cdclk(dev, 19200);
  4756. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4757. }
  4758. static const struct skl_cdclk_entry {
  4759. unsigned int freq;
  4760. unsigned int vco;
  4761. } skl_cdclk_frequencies[] = {
  4762. { .freq = 308570, .vco = 8640 },
  4763. { .freq = 337500, .vco = 8100 },
  4764. { .freq = 432000, .vco = 8640 },
  4765. { .freq = 450000, .vco = 8100 },
  4766. { .freq = 540000, .vco = 8100 },
  4767. { .freq = 617140, .vco = 8640 },
  4768. { .freq = 675000, .vco = 8100 },
  4769. };
  4770. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4771. {
  4772. return (freq - 1000) / 500;
  4773. }
  4774. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4775. {
  4776. unsigned int i;
  4777. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4778. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4779. if (e->freq == freq)
  4780. return e->vco;
  4781. }
  4782. return 8100;
  4783. }
  4784. static void
  4785. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4786. {
  4787. unsigned int min_freq;
  4788. u32 val;
  4789. /* select the minimum CDCLK before enabling DPLL 0 */
  4790. val = I915_READ(CDCLK_CTL);
  4791. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4792. val |= CDCLK_FREQ_337_308;
  4793. if (required_vco == 8640)
  4794. min_freq = 308570;
  4795. else
  4796. min_freq = 337500;
  4797. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4798. I915_WRITE(CDCLK_CTL, val);
  4799. POSTING_READ(CDCLK_CTL);
  4800. /*
  4801. * We always enable DPLL0 with the lowest link rate possible, but still
  4802. * taking into account the VCO required to operate the eDP panel at the
  4803. * desired frequency. The usual DP link rates operate with a VCO of
  4804. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4805. * The modeset code is responsible for the selection of the exact link
  4806. * rate later on, with the constraint of choosing a frequency that
  4807. * works with required_vco.
  4808. */
  4809. val = I915_READ(DPLL_CTRL1);
  4810. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4811. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4812. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4813. if (required_vco == 8640)
  4814. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4815. SKL_DPLL0);
  4816. else
  4817. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4818. SKL_DPLL0);
  4819. I915_WRITE(DPLL_CTRL1, val);
  4820. POSTING_READ(DPLL_CTRL1);
  4821. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4822. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4823. DRM_ERROR("DPLL0 not locked\n");
  4824. }
  4825. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4826. {
  4827. int ret;
  4828. u32 val;
  4829. /* inform PCU we want to change CDCLK */
  4830. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4831. mutex_lock(&dev_priv->rps.hw_lock);
  4832. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4833. mutex_unlock(&dev_priv->rps.hw_lock);
  4834. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4835. }
  4836. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4837. {
  4838. unsigned int i;
  4839. for (i = 0; i < 15; i++) {
  4840. if (skl_cdclk_pcu_ready(dev_priv))
  4841. return true;
  4842. udelay(10);
  4843. }
  4844. return false;
  4845. }
  4846. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4847. {
  4848. struct drm_device *dev = dev_priv->dev;
  4849. u32 freq_select, pcu_ack;
  4850. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4851. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4852. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4853. return;
  4854. }
  4855. /* set CDCLK_CTL */
  4856. switch(freq) {
  4857. case 450000:
  4858. case 432000:
  4859. freq_select = CDCLK_FREQ_450_432;
  4860. pcu_ack = 1;
  4861. break;
  4862. case 540000:
  4863. freq_select = CDCLK_FREQ_540;
  4864. pcu_ack = 2;
  4865. break;
  4866. case 308570:
  4867. case 337500:
  4868. default:
  4869. freq_select = CDCLK_FREQ_337_308;
  4870. pcu_ack = 0;
  4871. break;
  4872. case 617140:
  4873. case 675000:
  4874. freq_select = CDCLK_FREQ_675_617;
  4875. pcu_ack = 3;
  4876. break;
  4877. }
  4878. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4879. POSTING_READ(CDCLK_CTL);
  4880. /* inform PCU of the change */
  4881. mutex_lock(&dev_priv->rps.hw_lock);
  4882. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4883. mutex_unlock(&dev_priv->rps.hw_lock);
  4884. intel_update_cdclk(dev);
  4885. }
  4886. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4887. {
  4888. /* disable DBUF power */
  4889. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4890. POSTING_READ(DBUF_CTL);
  4891. udelay(10);
  4892. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4893. DRM_ERROR("DBuf power disable timeout\n");
  4894. /* disable DPLL0 */
  4895. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4896. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4897. DRM_ERROR("Couldn't disable DPLL0\n");
  4898. }
  4899. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4900. {
  4901. unsigned int required_vco;
  4902. /* DPLL0 not enabled (happens on early BIOS versions) */
  4903. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4904. /* enable DPLL0 */
  4905. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4906. skl_dpll0_enable(dev_priv, required_vco);
  4907. }
  4908. /* set CDCLK to the frequency the BIOS chose */
  4909. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4910. /* enable DBUF power */
  4911. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4912. POSTING_READ(DBUF_CTL);
  4913. udelay(10);
  4914. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4915. DRM_ERROR("DBuf power enable timeout\n");
  4916. }
  4917. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4918. {
  4919. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4920. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4921. int freq = dev_priv->skl_boot_cdclk;
  4922. /*
  4923. * check if the pre-os intialized the display
  4924. * There is SWF18 scratchpad register defined which is set by the
  4925. * pre-os which can be used by the OS drivers to check the status
  4926. */
  4927. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4928. goto sanitize;
  4929. /* Is PLL enabled and locked ? */
  4930. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4931. goto sanitize;
  4932. /* DPLL okay; verify the cdclock
  4933. *
  4934. * Noticed in some instances that the freq selection is correct but
  4935. * decimal part is programmed wrong from BIOS where pre-os does not
  4936. * enable display. Verify the same as well.
  4937. */
  4938. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4939. /* All well; nothing to sanitize */
  4940. return false;
  4941. sanitize:
  4942. /*
  4943. * As of now initialize with max cdclk till
  4944. * we get dynamic cdclk support
  4945. * */
  4946. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4947. skl_init_cdclk(dev_priv);
  4948. /* we did have to sanitize */
  4949. return true;
  4950. }
  4951. /* Adjust CDclk dividers to allow high res or save power if possible */
  4952. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4953. {
  4954. struct drm_i915_private *dev_priv = dev->dev_private;
  4955. u32 val, cmd;
  4956. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4957. != dev_priv->cdclk_freq);
  4958. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4959. cmd = 2;
  4960. else if (cdclk == 266667)
  4961. cmd = 1;
  4962. else
  4963. cmd = 0;
  4964. mutex_lock(&dev_priv->rps.hw_lock);
  4965. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4966. val &= ~DSPFREQGUAR_MASK;
  4967. val |= (cmd << DSPFREQGUAR_SHIFT);
  4968. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4969. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4970. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4971. 50)) {
  4972. DRM_ERROR("timed out waiting for CDclk change\n");
  4973. }
  4974. mutex_unlock(&dev_priv->rps.hw_lock);
  4975. mutex_lock(&dev_priv->sb_lock);
  4976. if (cdclk == 400000) {
  4977. u32 divider;
  4978. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4979. /* adjust cdclk divider */
  4980. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4981. val &= ~CCK_FREQUENCY_VALUES;
  4982. val |= divider;
  4983. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4984. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4985. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4986. 50))
  4987. DRM_ERROR("timed out waiting for CDclk change\n");
  4988. }
  4989. /* adjust self-refresh exit latency value */
  4990. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4991. val &= ~0x7f;
  4992. /*
  4993. * For high bandwidth configs, we set a higher latency in the bunit
  4994. * so that the core display fetch happens in time to avoid underruns.
  4995. */
  4996. if (cdclk == 400000)
  4997. val |= 4500 / 250; /* 4.5 usec */
  4998. else
  4999. val |= 3000 / 250; /* 3.0 usec */
  5000. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5001. mutex_unlock(&dev_priv->sb_lock);
  5002. intel_update_cdclk(dev);
  5003. }
  5004. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5005. {
  5006. struct drm_i915_private *dev_priv = dev->dev_private;
  5007. u32 val, cmd;
  5008. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5009. != dev_priv->cdclk_freq);
  5010. switch (cdclk) {
  5011. case 333333:
  5012. case 320000:
  5013. case 266667:
  5014. case 200000:
  5015. break;
  5016. default:
  5017. MISSING_CASE(cdclk);
  5018. return;
  5019. }
  5020. /*
  5021. * Specs are full of misinformation, but testing on actual
  5022. * hardware has shown that we just need to write the desired
  5023. * CCK divider into the Punit register.
  5024. */
  5025. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5026. mutex_lock(&dev_priv->rps.hw_lock);
  5027. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5028. val &= ~DSPFREQGUAR_MASK_CHV;
  5029. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5030. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5031. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5032. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5033. 50)) {
  5034. DRM_ERROR("timed out waiting for CDclk change\n");
  5035. }
  5036. mutex_unlock(&dev_priv->rps.hw_lock);
  5037. intel_update_cdclk(dev);
  5038. }
  5039. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5040. int max_pixclk)
  5041. {
  5042. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5043. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5044. /*
  5045. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5046. * 200MHz
  5047. * 267MHz
  5048. * 320/333MHz (depends on HPLL freq)
  5049. * 400MHz (VLV only)
  5050. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5051. * of the lower bin and adjust if needed.
  5052. *
  5053. * We seem to get an unstable or solid color picture at 200MHz.
  5054. * Not sure what's wrong. For now use 200MHz only when all pipes
  5055. * are off.
  5056. */
  5057. if (!IS_CHERRYVIEW(dev_priv) &&
  5058. max_pixclk > freq_320*limit/100)
  5059. return 400000;
  5060. else if (max_pixclk > 266667*limit/100)
  5061. return freq_320;
  5062. else if (max_pixclk > 0)
  5063. return 266667;
  5064. else
  5065. return 200000;
  5066. }
  5067. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5068. int max_pixclk)
  5069. {
  5070. /*
  5071. * FIXME:
  5072. * - remove the guardband, it's not needed on BXT
  5073. * - set 19.2MHz bypass frequency if there are no active pipes
  5074. */
  5075. if (max_pixclk > 576000*9/10)
  5076. return 624000;
  5077. else if (max_pixclk > 384000*9/10)
  5078. return 576000;
  5079. else if (max_pixclk > 288000*9/10)
  5080. return 384000;
  5081. else if (max_pixclk > 144000*9/10)
  5082. return 288000;
  5083. else
  5084. return 144000;
  5085. }
  5086. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5087. * that's non-NULL, look at current state otherwise. */
  5088. static int intel_mode_max_pixclk(struct drm_device *dev,
  5089. struct drm_atomic_state *state)
  5090. {
  5091. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5092. struct drm_i915_private *dev_priv = dev->dev_private;
  5093. struct drm_crtc *crtc;
  5094. struct drm_crtc_state *crtc_state;
  5095. unsigned max_pixclk = 0, i;
  5096. enum pipe pipe;
  5097. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5098. sizeof(intel_state->min_pixclk));
  5099. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5100. int pixclk = 0;
  5101. if (crtc_state->enable)
  5102. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5103. intel_state->min_pixclk[i] = pixclk;
  5104. }
  5105. if (!intel_state->active_crtcs)
  5106. return 0;
  5107. for_each_pipe(dev_priv, pipe)
  5108. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5109. return max_pixclk;
  5110. }
  5111. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5112. {
  5113. struct drm_device *dev = state->dev;
  5114. struct drm_i915_private *dev_priv = dev->dev_private;
  5115. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5116. struct intel_atomic_state *intel_state =
  5117. to_intel_atomic_state(state);
  5118. if (max_pixclk < 0)
  5119. return max_pixclk;
  5120. intel_state->cdclk = intel_state->dev_cdclk =
  5121. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5122. if (!intel_state->active_crtcs)
  5123. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5124. return 0;
  5125. }
  5126. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5127. {
  5128. struct drm_device *dev = state->dev;
  5129. struct drm_i915_private *dev_priv = dev->dev_private;
  5130. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5131. struct intel_atomic_state *intel_state =
  5132. to_intel_atomic_state(state);
  5133. if (max_pixclk < 0)
  5134. return max_pixclk;
  5135. intel_state->cdclk = intel_state->dev_cdclk =
  5136. broxton_calc_cdclk(dev_priv, max_pixclk);
  5137. if (!intel_state->active_crtcs)
  5138. intel_state->dev_cdclk = broxton_calc_cdclk(dev_priv, 0);
  5139. return 0;
  5140. }
  5141. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5142. {
  5143. unsigned int credits, default_credits;
  5144. if (IS_CHERRYVIEW(dev_priv))
  5145. default_credits = PFI_CREDIT(12);
  5146. else
  5147. default_credits = PFI_CREDIT(8);
  5148. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5149. /* CHV suggested value is 31 or 63 */
  5150. if (IS_CHERRYVIEW(dev_priv))
  5151. credits = PFI_CREDIT_63;
  5152. else
  5153. credits = PFI_CREDIT(15);
  5154. } else {
  5155. credits = default_credits;
  5156. }
  5157. /*
  5158. * WA - write default credits before re-programming
  5159. * FIXME: should we also set the resend bit here?
  5160. */
  5161. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5162. default_credits);
  5163. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5164. credits | PFI_CREDIT_RESEND);
  5165. /*
  5166. * FIXME is this guaranteed to clear
  5167. * immediately or should we poll for it?
  5168. */
  5169. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5170. }
  5171. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5172. {
  5173. struct drm_device *dev = old_state->dev;
  5174. struct drm_i915_private *dev_priv = dev->dev_private;
  5175. struct intel_atomic_state *old_intel_state =
  5176. to_intel_atomic_state(old_state);
  5177. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5178. /*
  5179. * FIXME: We can end up here with all power domains off, yet
  5180. * with a CDCLK frequency other than the minimum. To account
  5181. * for this take the PIPE-A power domain, which covers the HW
  5182. * blocks needed for the following programming. This can be
  5183. * removed once it's guaranteed that we get here either with
  5184. * the minimum CDCLK set, or the required power domains
  5185. * enabled.
  5186. */
  5187. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5188. if (IS_CHERRYVIEW(dev))
  5189. cherryview_set_cdclk(dev, req_cdclk);
  5190. else
  5191. valleyview_set_cdclk(dev, req_cdclk);
  5192. vlv_program_pfi_credits(dev_priv);
  5193. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5194. }
  5195. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5196. {
  5197. struct drm_device *dev = crtc->dev;
  5198. struct drm_i915_private *dev_priv = to_i915(dev);
  5199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5200. struct intel_encoder *encoder;
  5201. int pipe = intel_crtc->pipe;
  5202. if (WARN_ON(intel_crtc->active))
  5203. return;
  5204. if (intel_crtc->config->has_dp_encoder)
  5205. intel_dp_set_m_n(intel_crtc, M1_N1);
  5206. intel_set_pipe_timings(intel_crtc);
  5207. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5208. struct drm_i915_private *dev_priv = dev->dev_private;
  5209. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5210. I915_WRITE(CHV_CANVAS(pipe), 0);
  5211. }
  5212. i9xx_set_pipeconf(intel_crtc);
  5213. intel_crtc->active = true;
  5214. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5215. for_each_encoder_on_crtc(dev, crtc, encoder)
  5216. if (encoder->pre_pll_enable)
  5217. encoder->pre_pll_enable(encoder);
  5218. if (!intel_crtc->config->has_dsi_encoder) {
  5219. if (IS_CHERRYVIEW(dev)) {
  5220. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5221. chv_enable_pll(intel_crtc, intel_crtc->config);
  5222. } else {
  5223. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5224. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5225. }
  5226. }
  5227. for_each_encoder_on_crtc(dev, crtc, encoder)
  5228. if (encoder->pre_enable)
  5229. encoder->pre_enable(encoder);
  5230. i9xx_pfit_enable(intel_crtc);
  5231. intel_crtc_load_lut(crtc);
  5232. intel_enable_pipe(intel_crtc);
  5233. assert_vblank_disabled(crtc);
  5234. drm_crtc_vblank_on(crtc);
  5235. for_each_encoder_on_crtc(dev, crtc, encoder)
  5236. encoder->enable(encoder);
  5237. }
  5238. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5239. {
  5240. struct drm_device *dev = crtc->base.dev;
  5241. struct drm_i915_private *dev_priv = dev->dev_private;
  5242. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5243. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5244. }
  5245. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5246. {
  5247. struct drm_device *dev = crtc->dev;
  5248. struct drm_i915_private *dev_priv = to_i915(dev);
  5249. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5250. struct intel_encoder *encoder;
  5251. int pipe = intel_crtc->pipe;
  5252. if (WARN_ON(intel_crtc->active))
  5253. return;
  5254. i9xx_set_pll_dividers(intel_crtc);
  5255. if (intel_crtc->config->has_dp_encoder)
  5256. intel_dp_set_m_n(intel_crtc, M1_N1);
  5257. intel_set_pipe_timings(intel_crtc);
  5258. i9xx_set_pipeconf(intel_crtc);
  5259. intel_crtc->active = true;
  5260. if (!IS_GEN2(dev))
  5261. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5262. for_each_encoder_on_crtc(dev, crtc, encoder)
  5263. if (encoder->pre_enable)
  5264. encoder->pre_enable(encoder);
  5265. i9xx_enable_pll(intel_crtc);
  5266. i9xx_pfit_enable(intel_crtc);
  5267. intel_crtc_load_lut(crtc);
  5268. intel_update_watermarks(crtc);
  5269. intel_enable_pipe(intel_crtc);
  5270. assert_vblank_disabled(crtc);
  5271. drm_crtc_vblank_on(crtc);
  5272. for_each_encoder_on_crtc(dev, crtc, encoder)
  5273. encoder->enable(encoder);
  5274. intel_fbc_enable(intel_crtc);
  5275. }
  5276. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5277. {
  5278. struct drm_device *dev = crtc->base.dev;
  5279. struct drm_i915_private *dev_priv = dev->dev_private;
  5280. if (!crtc->config->gmch_pfit.control)
  5281. return;
  5282. assert_pipe_disabled(dev_priv, crtc->pipe);
  5283. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5284. I915_READ(PFIT_CONTROL));
  5285. I915_WRITE(PFIT_CONTROL, 0);
  5286. }
  5287. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5288. {
  5289. struct drm_device *dev = crtc->dev;
  5290. struct drm_i915_private *dev_priv = dev->dev_private;
  5291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5292. struct intel_encoder *encoder;
  5293. int pipe = intel_crtc->pipe;
  5294. /*
  5295. * On gen2 planes are double buffered but the pipe isn't, so we must
  5296. * wait for planes to fully turn off before disabling the pipe.
  5297. * We also need to wait on all gmch platforms because of the
  5298. * self-refresh mode constraint explained above.
  5299. */
  5300. intel_wait_for_vblank(dev, pipe);
  5301. for_each_encoder_on_crtc(dev, crtc, encoder)
  5302. encoder->disable(encoder);
  5303. drm_crtc_vblank_off(crtc);
  5304. assert_vblank_disabled(crtc);
  5305. intel_disable_pipe(intel_crtc);
  5306. i9xx_pfit_disable(intel_crtc);
  5307. for_each_encoder_on_crtc(dev, crtc, encoder)
  5308. if (encoder->post_disable)
  5309. encoder->post_disable(encoder);
  5310. if (!intel_crtc->config->has_dsi_encoder) {
  5311. if (IS_CHERRYVIEW(dev))
  5312. chv_disable_pll(dev_priv, pipe);
  5313. else if (IS_VALLEYVIEW(dev))
  5314. vlv_disable_pll(dev_priv, pipe);
  5315. else
  5316. i9xx_disable_pll(intel_crtc);
  5317. }
  5318. for_each_encoder_on_crtc(dev, crtc, encoder)
  5319. if (encoder->post_pll_disable)
  5320. encoder->post_pll_disable(encoder);
  5321. if (!IS_GEN2(dev))
  5322. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5323. intel_fbc_disable_crtc(intel_crtc);
  5324. }
  5325. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5326. {
  5327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5328. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5329. enum intel_display_power_domain domain;
  5330. unsigned long domains;
  5331. if (!intel_crtc->active)
  5332. return;
  5333. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5334. WARN_ON(intel_crtc->unpin_work);
  5335. intel_pre_disable_primary(crtc);
  5336. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5337. to_intel_plane_state(crtc->primary->state)->visible = false;
  5338. }
  5339. dev_priv->display.crtc_disable(crtc);
  5340. intel_crtc->active = false;
  5341. intel_update_watermarks(crtc);
  5342. intel_disable_shared_dpll(intel_crtc);
  5343. domains = intel_crtc->enabled_power_domains;
  5344. for_each_power_domain(domain, domains)
  5345. intel_display_power_put(dev_priv, domain);
  5346. intel_crtc->enabled_power_domains = 0;
  5347. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5348. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5349. }
  5350. /*
  5351. * turn all crtc's off, but do not adjust state
  5352. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5353. */
  5354. int intel_display_suspend(struct drm_device *dev)
  5355. {
  5356. struct drm_mode_config *config = &dev->mode_config;
  5357. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5358. struct drm_atomic_state *state;
  5359. struct drm_crtc *crtc;
  5360. unsigned crtc_mask = 0;
  5361. int ret = 0;
  5362. if (WARN_ON(!ctx))
  5363. return 0;
  5364. lockdep_assert_held(&ctx->ww_ctx);
  5365. state = drm_atomic_state_alloc(dev);
  5366. if (WARN_ON(!state))
  5367. return -ENOMEM;
  5368. state->acquire_ctx = ctx;
  5369. state->allow_modeset = true;
  5370. for_each_crtc(dev, crtc) {
  5371. struct drm_crtc_state *crtc_state =
  5372. drm_atomic_get_crtc_state(state, crtc);
  5373. ret = PTR_ERR_OR_ZERO(crtc_state);
  5374. if (ret)
  5375. goto free;
  5376. if (!crtc_state->active)
  5377. continue;
  5378. crtc_state->active = false;
  5379. crtc_mask |= 1 << drm_crtc_index(crtc);
  5380. }
  5381. if (crtc_mask) {
  5382. ret = drm_atomic_commit(state);
  5383. if (!ret) {
  5384. for_each_crtc(dev, crtc)
  5385. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5386. crtc->state->active = true;
  5387. return ret;
  5388. }
  5389. }
  5390. free:
  5391. if (ret)
  5392. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5393. drm_atomic_state_free(state);
  5394. return ret;
  5395. }
  5396. void intel_encoder_destroy(struct drm_encoder *encoder)
  5397. {
  5398. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5399. drm_encoder_cleanup(encoder);
  5400. kfree(intel_encoder);
  5401. }
  5402. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5403. * internal consistency). */
  5404. static void intel_connector_check_state(struct intel_connector *connector)
  5405. {
  5406. struct drm_crtc *crtc = connector->base.state->crtc;
  5407. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5408. connector->base.base.id,
  5409. connector->base.name);
  5410. if (connector->get_hw_state(connector)) {
  5411. struct intel_encoder *encoder = connector->encoder;
  5412. struct drm_connector_state *conn_state = connector->base.state;
  5413. I915_STATE_WARN(!crtc,
  5414. "connector enabled without attached crtc\n");
  5415. if (!crtc)
  5416. return;
  5417. I915_STATE_WARN(!crtc->state->active,
  5418. "connector is active, but attached crtc isn't\n");
  5419. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5420. return;
  5421. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5422. "atomic encoder doesn't match attached encoder\n");
  5423. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5424. "attached encoder crtc differs from connector crtc\n");
  5425. } else {
  5426. I915_STATE_WARN(crtc && crtc->state->active,
  5427. "attached crtc is active, but connector isn't\n");
  5428. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5429. "best encoder set without crtc!\n");
  5430. }
  5431. }
  5432. int intel_connector_init(struct intel_connector *connector)
  5433. {
  5434. drm_atomic_helper_connector_reset(&connector->base);
  5435. if (!connector->base.state)
  5436. return -ENOMEM;
  5437. return 0;
  5438. }
  5439. struct intel_connector *intel_connector_alloc(void)
  5440. {
  5441. struct intel_connector *connector;
  5442. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5443. if (!connector)
  5444. return NULL;
  5445. if (intel_connector_init(connector) < 0) {
  5446. kfree(connector);
  5447. return NULL;
  5448. }
  5449. return connector;
  5450. }
  5451. /* Simple connector->get_hw_state implementation for encoders that support only
  5452. * one connector and no cloning and hence the encoder state determines the state
  5453. * of the connector. */
  5454. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5455. {
  5456. enum pipe pipe = 0;
  5457. struct intel_encoder *encoder = connector->encoder;
  5458. return encoder->get_hw_state(encoder, &pipe);
  5459. }
  5460. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5461. {
  5462. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5463. return crtc_state->fdi_lanes;
  5464. return 0;
  5465. }
  5466. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5467. struct intel_crtc_state *pipe_config)
  5468. {
  5469. struct drm_atomic_state *state = pipe_config->base.state;
  5470. struct intel_crtc *other_crtc;
  5471. struct intel_crtc_state *other_crtc_state;
  5472. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5473. pipe_name(pipe), pipe_config->fdi_lanes);
  5474. if (pipe_config->fdi_lanes > 4) {
  5475. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5476. pipe_name(pipe), pipe_config->fdi_lanes);
  5477. return -EINVAL;
  5478. }
  5479. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5480. if (pipe_config->fdi_lanes > 2) {
  5481. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5482. pipe_config->fdi_lanes);
  5483. return -EINVAL;
  5484. } else {
  5485. return 0;
  5486. }
  5487. }
  5488. if (INTEL_INFO(dev)->num_pipes == 2)
  5489. return 0;
  5490. /* Ivybridge 3 pipe is really complicated */
  5491. switch (pipe) {
  5492. case PIPE_A:
  5493. return 0;
  5494. case PIPE_B:
  5495. if (pipe_config->fdi_lanes <= 2)
  5496. return 0;
  5497. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5498. other_crtc_state =
  5499. intel_atomic_get_crtc_state(state, other_crtc);
  5500. if (IS_ERR(other_crtc_state))
  5501. return PTR_ERR(other_crtc_state);
  5502. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5503. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5504. pipe_name(pipe), pipe_config->fdi_lanes);
  5505. return -EINVAL;
  5506. }
  5507. return 0;
  5508. case PIPE_C:
  5509. if (pipe_config->fdi_lanes > 2) {
  5510. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5511. pipe_name(pipe), pipe_config->fdi_lanes);
  5512. return -EINVAL;
  5513. }
  5514. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5515. other_crtc_state =
  5516. intel_atomic_get_crtc_state(state, other_crtc);
  5517. if (IS_ERR(other_crtc_state))
  5518. return PTR_ERR(other_crtc_state);
  5519. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5520. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5521. return -EINVAL;
  5522. }
  5523. return 0;
  5524. default:
  5525. BUG();
  5526. }
  5527. }
  5528. #define RETRY 1
  5529. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5530. struct intel_crtc_state *pipe_config)
  5531. {
  5532. struct drm_device *dev = intel_crtc->base.dev;
  5533. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5534. int lane, link_bw, fdi_dotclock, ret;
  5535. bool needs_recompute = false;
  5536. retry:
  5537. /* FDI is a binary signal running at ~2.7GHz, encoding
  5538. * each output octet as 10 bits. The actual frequency
  5539. * is stored as a divider into a 100MHz clock, and the
  5540. * mode pixel clock is stored in units of 1KHz.
  5541. * Hence the bw of each lane in terms of the mode signal
  5542. * is:
  5543. */
  5544. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5545. fdi_dotclock = adjusted_mode->crtc_clock;
  5546. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5547. pipe_config->pipe_bpp);
  5548. pipe_config->fdi_lanes = lane;
  5549. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5550. link_bw, &pipe_config->fdi_m_n);
  5551. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5552. intel_crtc->pipe, pipe_config);
  5553. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5554. pipe_config->pipe_bpp -= 2*3;
  5555. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5556. pipe_config->pipe_bpp);
  5557. needs_recompute = true;
  5558. pipe_config->bw_constrained = true;
  5559. goto retry;
  5560. }
  5561. if (needs_recompute)
  5562. return RETRY;
  5563. return ret;
  5564. }
  5565. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5566. struct intel_crtc_state *pipe_config)
  5567. {
  5568. if (pipe_config->pipe_bpp > 24)
  5569. return false;
  5570. /* HSW can handle pixel rate up to cdclk? */
  5571. if (IS_HASWELL(dev_priv->dev))
  5572. return true;
  5573. /*
  5574. * We compare against max which means we must take
  5575. * the increased cdclk requirement into account when
  5576. * calculating the new cdclk.
  5577. *
  5578. * Should measure whether using a lower cdclk w/o IPS
  5579. */
  5580. return ilk_pipe_pixel_rate(pipe_config) <=
  5581. dev_priv->max_cdclk_freq * 95 / 100;
  5582. }
  5583. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5584. struct intel_crtc_state *pipe_config)
  5585. {
  5586. struct drm_device *dev = crtc->base.dev;
  5587. struct drm_i915_private *dev_priv = dev->dev_private;
  5588. pipe_config->ips_enabled = i915.enable_ips &&
  5589. hsw_crtc_supports_ips(crtc) &&
  5590. pipe_config_supports_ips(dev_priv, pipe_config);
  5591. }
  5592. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5593. {
  5594. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5595. /* GDG double wide on either pipe, otherwise pipe A only */
  5596. return INTEL_INFO(dev_priv)->gen < 4 &&
  5597. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5598. }
  5599. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5600. struct intel_crtc_state *pipe_config)
  5601. {
  5602. struct drm_device *dev = crtc->base.dev;
  5603. struct drm_i915_private *dev_priv = dev->dev_private;
  5604. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5605. /* FIXME should check pixel clock limits on all platforms */
  5606. if (INTEL_INFO(dev)->gen < 4) {
  5607. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5608. /*
  5609. * Enable double wide mode when the dot clock
  5610. * is > 90% of the (display) core speed.
  5611. */
  5612. if (intel_crtc_supports_double_wide(crtc) &&
  5613. adjusted_mode->crtc_clock > clock_limit) {
  5614. clock_limit *= 2;
  5615. pipe_config->double_wide = true;
  5616. }
  5617. if (adjusted_mode->crtc_clock > clock_limit) {
  5618. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5619. adjusted_mode->crtc_clock, clock_limit,
  5620. yesno(pipe_config->double_wide));
  5621. return -EINVAL;
  5622. }
  5623. }
  5624. /*
  5625. * Pipe horizontal size must be even in:
  5626. * - DVO ganged mode
  5627. * - LVDS dual channel mode
  5628. * - Double wide pipe
  5629. */
  5630. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5631. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5632. pipe_config->pipe_src_w &= ~1;
  5633. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5634. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5635. */
  5636. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5637. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5638. return -EINVAL;
  5639. if (HAS_IPS(dev))
  5640. hsw_compute_ips_config(crtc, pipe_config);
  5641. if (pipe_config->has_pch_encoder)
  5642. return ironlake_fdi_compute_config(crtc, pipe_config);
  5643. return 0;
  5644. }
  5645. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5646. {
  5647. struct drm_i915_private *dev_priv = to_i915(dev);
  5648. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5649. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5650. uint32_t linkrate;
  5651. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5652. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5653. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5654. return 540000;
  5655. linkrate = (I915_READ(DPLL_CTRL1) &
  5656. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5657. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5658. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5659. /* vco 8640 */
  5660. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5661. case CDCLK_FREQ_450_432:
  5662. return 432000;
  5663. case CDCLK_FREQ_337_308:
  5664. return 308570;
  5665. case CDCLK_FREQ_675_617:
  5666. return 617140;
  5667. default:
  5668. WARN(1, "Unknown cd freq selection\n");
  5669. }
  5670. } else {
  5671. /* vco 8100 */
  5672. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5673. case CDCLK_FREQ_450_432:
  5674. return 450000;
  5675. case CDCLK_FREQ_337_308:
  5676. return 337500;
  5677. case CDCLK_FREQ_675_617:
  5678. return 675000;
  5679. default:
  5680. WARN(1, "Unknown cd freq selection\n");
  5681. }
  5682. }
  5683. /* error case, do as if DPLL0 isn't enabled */
  5684. return 24000;
  5685. }
  5686. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5687. {
  5688. struct drm_i915_private *dev_priv = to_i915(dev);
  5689. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5690. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5691. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5692. int cdclk;
  5693. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5694. return 19200;
  5695. cdclk = 19200 * pll_ratio / 2;
  5696. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5697. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5698. return cdclk; /* 576MHz or 624MHz */
  5699. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5700. return cdclk * 2 / 3; /* 384MHz */
  5701. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5702. return cdclk / 2; /* 288MHz */
  5703. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5704. return cdclk / 4; /* 144MHz */
  5705. }
  5706. /* error case, do as if DE PLL isn't enabled */
  5707. return 19200;
  5708. }
  5709. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5710. {
  5711. struct drm_i915_private *dev_priv = dev->dev_private;
  5712. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5713. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5714. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5715. return 800000;
  5716. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5717. return 450000;
  5718. else if (freq == LCPLL_CLK_FREQ_450)
  5719. return 450000;
  5720. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5721. return 540000;
  5722. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5723. return 337500;
  5724. else
  5725. return 675000;
  5726. }
  5727. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5728. {
  5729. struct drm_i915_private *dev_priv = dev->dev_private;
  5730. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5731. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5732. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5733. return 800000;
  5734. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5735. return 450000;
  5736. else if (freq == LCPLL_CLK_FREQ_450)
  5737. return 450000;
  5738. else if (IS_HSW_ULT(dev))
  5739. return 337500;
  5740. else
  5741. return 540000;
  5742. }
  5743. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5744. {
  5745. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5746. CCK_DISPLAY_CLOCK_CONTROL);
  5747. }
  5748. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5749. {
  5750. return 450000;
  5751. }
  5752. static int i945_get_display_clock_speed(struct drm_device *dev)
  5753. {
  5754. return 400000;
  5755. }
  5756. static int i915_get_display_clock_speed(struct drm_device *dev)
  5757. {
  5758. return 333333;
  5759. }
  5760. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5761. {
  5762. return 200000;
  5763. }
  5764. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5765. {
  5766. u16 gcfgc = 0;
  5767. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5768. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5769. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5770. return 266667;
  5771. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5772. return 333333;
  5773. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5774. return 444444;
  5775. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5776. return 200000;
  5777. default:
  5778. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5779. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5780. return 133333;
  5781. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5782. return 166667;
  5783. }
  5784. }
  5785. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5786. {
  5787. u16 gcfgc = 0;
  5788. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5789. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5790. return 133333;
  5791. else {
  5792. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5793. case GC_DISPLAY_CLOCK_333_MHZ:
  5794. return 333333;
  5795. default:
  5796. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5797. return 190000;
  5798. }
  5799. }
  5800. }
  5801. static int i865_get_display_clock_speed(struct drm_device *dev)
  5802. {
  5803. return 266667;
  5804. }
  5805. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5806. {
  5807. u16 hpllcc = 0;
  5808. /*
  5809. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5810. * encoding is different :(
  5811. * FIXME is this the right way to detect 852GM/852GMV?
  5812. */
  5813. if (dev->pdev->revision == 0x1)
  5814. return 133333;
  5815. pci_bus_read_config_word(dev->pdev->bus,
  5816. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5817. /* Assume that the hardware is in the high speed state. This
  5818. * should be the default.
  5819. */
  5820. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5821. case GC_CLOCK_133_200:
  5822. case GC_CLOCK_133_200_2:
  5823. case GC_CLOCK_100_200:
  5824. return 200000;
  5825. case GC_CLOCK_166_250:
  5826. return 250000;
  5827. case GC_CLOCK_100_133:
  5828. return 133333;
  5829. case GC_CLOCK_133_266:
  5830. case GC_CLOCK_133_266_2:
  5831. case GC_CLOCK_166_266:
  5832. return 266667;
  5833. }
  5834. /* Shouldn't happen */
  5835. return 0;
  5836. }
  5837. static int i830_get_display_clock_speed(struct drm_device *dev)
  5838. {
  5839. return 133333;
  5840. }
  5841. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5842. {
  5843. struct drm_i915_private *dev_priv = dev->dev_private;
  5844. static const unsigned int blb_vco[8] = {
  5845. [0] = 3200000,
  5846. [1] = 4000000,
  5847. [2] = 5333333,
  5848. [3] = 4800000,
  5849. [4] = 6400000,
  5850. };
  5851. static const unsigned int pnv_vco[8] = {
  5852. [0] = 3200000,
  5853. [1] = 4000000,
  5854. [2] = 5333333,
  5855. [3] = 4800000,
  5856. [4] = 2666667,
  5857. };
  5858. static const unsigned int cl_vco[8] = {
  5859. [0] = 3200000,
  5860. [1] = 4000000,
  5861. [2] = 5333333,
  5862. [3] = 6400000,
  5863. [4] = 3333333,
  5864. [5] = 3566667,
  5865. [6] = 4266667,
  5866. };
  5867. static const unsigned int elk_vco[8] = {
  5868. [0] = 3200000,
  5869. [1] = 4000000,
  5870. [2] = 5333333,
  5871. [3] = 4800000,
  5872. };
  5873. static const unsigned int ctg_vco[8] = {
  5874. [0] = 3200000,
  5875. [1] = 4000000,
  5876. [2] = 5333333,
  5877. [3] = 6400000,
  5878. [4] = 2666667,
  5879. [5] = 4266667,
  5880. };
  5881. const unsigned int *vco_table;
  5882. unsigned int vco;
  5883. uint8_t tmp = 0;
  5884. /* FIXME other chipsets? */
  5885. if (IS_GM45(dev))
  5886. vco_table = ctg_vco;
  5887. else if (IS_G4X(dev))
  5888. vco_table = elk_vco;
  5889. else if (IS_CRESTLINE(dev))
  5890. vco_table = cl_vco;
  5891. else if (IS_PINEVIEW(dev))
  5892. vco_table = pnv_vco;
  5893. else if (IS_G33(dev))
  5894. vco_table = blb_vco;
  5895. else
  5896. return 0;
  5897. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5898. vco = vco_table[tmp & 0x7];
  5899. if (vco == 0)
  5900. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5901. else
  5902. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5903. return vco;
  5904. }
  5905. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5906. {
  5907. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5908. uint16_t tmp = 0;
  5909. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5910. cdclk_sel = (tmp >> 12) & 0x1;
  5911. switch (vco) {
  5912. case 2666667:
  5913. case 4000000:
  5914. case 5333333:
  5915. return cdclk_sel ? 333333 : 222222;
  5916. case 3200000:
  5917. return cdclk_sel ? 320000 : 228571;
  5918. default:
  5919. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5920. return 222222;
  5921. }
  5922. }
  5923. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5924. {
  5925. static const uint8_t div_3200[] = { 16, 10, 8 };
  5926. static const uint8_t div_4000[] = { 20, 12, 10 };
  5927. static const uint8_t div_5333[] = { 24, 16, 14 };
  5928. const uint8_t *div_table;
  5929. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5930. uint16_t tmp = 0;
  5931. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5932. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5933. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5934. goto fail;
  5935. switch (vco) {
  5936. case 3200000:
  5937. div_table = div_3200;
  5938. break;
  5939. case 4000000:
  5940. div_table = div_4000;
  5941. break;
  5942. case 5333333:
  5943. div_table = div_5333;
  5944. break;
  5945. default:
  5946. goto fail;
  5947. }
  5948. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5949. fail:
  5950. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5951. return 200000;
  5952. }
  5953. static int g33_get_display_clock_speed(struct drm_device *dev)
  5954. {
  5955. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5956. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5957. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5958. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5959. const uint8_t *div_table;
  5960. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5961. uint16_t tmp = 0;
  5962. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5963. cdclk_sel = (tmp >> 4) & 0x7;
  5964. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5965. goto fail;
  5966. switch (vco) {
  5967. case 3200000:
  5968. div_table = div_3200;
  5969. break;
  5970. case 4000000:
  5971. div_table = div_4000;
  5972. break;
  5973. case 4800000:
  5974. div_table = div_4800;
  5975. break;
  5976. case 5333333:
  5977. div_table = div_5333;
  5978. break;
  5979. default:
  5980. goto fail;
  5981. }
  5982. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5983. fail:
  5984. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5985. return 190476;
  5986. }
  5987. static void
  5988. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5989. {
  5990. while (*num > DATA_LINK_M_N_MASK ||
  5991. *den > DATA_LINK_M_N_MASK) {
  5992. *num >>= 1;
  5993. *den >>= 1;
  5994. }
  5995. }
  5996. static void compute_m_n(unsigned int m, unsigned int n,
  5997. uint32_t *ret_m, uint32_t *ret_n)
  5998. {
  5999. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6000. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6001. intel_reduce_m_n_ratio(ret_m, ret_n);
  6002. }
  6003. void
  6004. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6005. int pixel_clock, int link_clock,
  6006. struct intel_link_m_n *m_n)
  6007. {
  6008. m_n->tu = 64;
  6009. compute_m_n(bits_per_pixel * pixel_clock,
  6010. link_clock * nlanes * 8,
  6011. &m_n->gmch_m, &m_n->gmch_n);
  6012. compute_m_n(pixel_clock, link_clock,
  6013. &m_n->link_m, &m_n->link_n);
  6014. }
  6015. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6016. {
  6017. if (i915.panel_use_ssc >= 0)
  6018. return i915.panel_use_ssc != 0;
  6019. return dev_priv->vbt.lvds_use_ssc
  6020. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6021. }
  6022. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  6023. int num_connectors)
  6024. {
  6025. struct drm_device *dev = crtc_state->base.crtc->dev;
  6026. struct drm_i915_private *dev_priv = dev->dev_private;
  6027. int refclk;
  6028. WARN_ON(!crtc_state->base.state);
  6029. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) {
  6030. refclk = 100000;
  6031. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6032. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6033. refclk = dev_priv->vbt.lvds_ssc_freq;
  6034. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6035. } else if (!IS_GEN2(dev)) {
  6036. refclk = 96000;
  6037. } else {
  6038. refclk = 48000;
  6039. }
  6040. return refclk;
  6041. }
  6042. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6043. {
  6044. return (1 << dpll->n) << 16 | dpll->m2;
  6045. }
  6046. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6047. {
  6048. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6049. }
  6050. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6051. struct intel_crtc_state *crtc_state,
  6052. intel_clock_t *reduced_clock)
  6053. {
  6054. struct drm_device *dev = crtc->base.dev;
  6055. u32 fp, fp2 = 0;
  6056. if (IS_PINEVIEW(dev)) {
  6057. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6058. if (reduced_clock)
  6059. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6060. } else {
  6061. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6062. if (reduced_clock)
  6063. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6064. }
  6065. crtc_state->dpll_hw_state.fp0 = fp;
  6066. crtc->lowfreq_avail = false;
  6067. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6068. reduced_clock) {
  6069. crtc_state->dpll_hw_state.fp1 = fp2;
  6070. crtc->lowfreq_avail = true;
  6071. } else {
  6072. crtc_state->dpll_hw_state.fp1 = fp;
  6073. }
  6074. }
  6075. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6076. pipe)
  6077. {
  6078. u32 reg_val;
  6079. /*
  6080. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6081. * and set it to a reasonable value instead.
  6082. */
  6083. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6084. reg_val &= 0xffffff00;
  6085. reg_val |= 0x00000030;
  6086. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6087. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6088. reg_val &= 0x8cffffff;
  6089. reg_val = 0x8c000000;
  6090. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6091. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6092. reg_val &= 0xffffff00;
  6093. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6094. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6095. reg_val &= 0x00ffffff;
  6096. reg_val |= 0xb0000000;
  6097. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6098. }
  6099. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6100. struct intel_link_m_n *m_n)
  6101. {
  6102. struct drm_device *dev = crtc->base.dev;
  6103. struct drm_i915_private *dev_priv = dev->dev_private;
  6104. int pipe = crtc->pipe;
  6105. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6106. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6107. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6108. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6109. }
  6110. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6111. struct intel_link_m_n *m_n,
  6112. struct intel_link_m_n *m2_n2)
  6113. {
  6114. struct drm_device *dev = crtc->base.dev;
  6115. struct drm_i915_private *dev_priv = dev->dev_private;
  6116. int pipe = crtc->pipe;
  6117. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6118. if (INTEL_INFO(dev)->gen >= 5) {
  6119. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6120. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6121. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6122. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6123. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6124. * for gen < 8) and if DRRS is supported (to make sure the
  6125. * registers are not unnecessarily accessed).
  6126. */
  6127. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6128. crtc->config->has_drrs) {
  6129. I915_WRITE(PIPE_DATA_M2(transcoder),
  6130. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6131. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6132. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6133. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6134. }
  6135. } else {
  6136. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6137. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6138. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6139. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6140. }
  6141. }
  6142. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6143. {
  6144. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6145. if (m_n == M1_N1) {
  6146. dp_m_n = &crtc->config->dp_m_n;
  6147. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6148. } else if (m_n == M2_N2) {
  6149. /*
  6150. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6151. * needs to be programmed into M1_N1.
  6152. */
  6153. dp_m_n = &crtc->config->dp_m2_n2;
  6154. } else {
  6155. DRM_ERROR("Unsupported divider value\n");
  6156. return;
  6157. }
  6158. if (crtc->config->has_pch_encoder)
  6159. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6160. else
  6161. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6162. }
  6163. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6164. struct intel_crtc_state *pipe_config)
  6165. {
  6166. u32 dpll, dpll_md;
  6167. /*
  6168. * Enable DPIO clock input. We should never disable the reference
  6169. * clock for pipe B, since VGA hotplug / manual detection depends
  6170. * on it.
  6171. */
  6172. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6173. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6174. /* We should never disable this, set it here for state tracking */
  6175. if (crtc->pipe == PIPE_B)
  6176. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6177. dpll |= DPLL_VCO_ENABLE;
  6178. pipe_config->dpll_hw_state.dpll = dpll;
  6179. dpll_md = (pipe_config->pixel_multiplier - 1)
  6180. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6181. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6182. }
  6183. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6184. const struct intel_crtc_state *pipe_config)
  6185. {
  6186. struct drm_device *dev = crtc->base.dev;
  6187. struct drm_i915_private *dev_priv = dev->dev_private;
  6188. int pipe = crtc->pipe;
  6189. u32 mdiv;
  6190. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6191. u32 coreclk, reg_val;
  6192. mutex_lock(&dev_priv->sb_lock);
  6193. bestn = pipe_config->dpll.n;
  6194. bestm1 = pipe_config->dpll.m1;
  6195. bestm2 = pipe_config->dpll.m2;
  6196. bestp1 = pipe_config->dpll.p1;
  6197. bestp2 = pipe_config->dpll.p2;
  6198. /* See eDP HDMI DPIO driver vbios notes doc */
  6199. /* PLL B needs special handling */
  6200. if (pipe == PIPE_B)
  6201. vlv_pllb_recal_opamp(dev_priv, pipe);
  6202. /* Set up Tx target for periodic Rcomp update */
  6203. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6204. /* Disable target IRef on PLL */
  6205. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6206. reg_val &= 0x00ffffff;
  6207. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6208. /* Disable fast lock */
  6209. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6210. /* Set idtafcrecal before PLL is enabled */
  6211. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6212. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6213. mdiv |= ((bestn << DPIO_N_SHIFT));
  6214. mdiv |= (1 << DPIO_K_SHIFT);
  6215. /*
  6216. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6217. * but we don't support that).
  6218. * Note: don't use the DAC post divider as it seems unstable.
  6219. */
  6220. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6221. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6222. mdiv |= DPIO_ENABLE_CALIBRATION;
  6223. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6224. /* Set HBR and RBR LPF coefficients */
  6225. if (pipe_config->port_clock == 162000 ||
  6226. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6227. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6228. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6229. 0x009f0003);
  6230. else
  6231. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6232. 0x00d0000f);
  6233. if (pipe_config->has_dp_encoder) {
  6234. /* Use SSC source */
  6235. if (pipe == PIPE_A)
  6236. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6237. 0x0df40000);
  6238. else
  6239. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6240. 0x0df70000);
  6241. } else { /* HDMI or VGA */
  6242. /* Use bend source */
  6243. if (pipe == PIPE_A)
  6244. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6245. 0x0df70000);
  6246. else
  6247. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6248. 0x0df40000);
  6249. }
  6250. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6251. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6252. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6253. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6254. coreclk |= 0x01000000;
  6255. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6256. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6257. mutex_unlock(&dev_priv->sb_lock);
  6258. }
  6259. static void chv_compute_dpll(struct intel_crtc *crtc,
  6260. struct intel_crtc_state *pipe_config)
  6261. {
  6262. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6263. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6264. DPLL_VCO_ENABLE;
  6265. if (crtc->pipe != PIPE_A)
  6266. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6267. pipe_config->dpll_hw_state.dpll_md =
  6268. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6269. }
  6270. static void chv_prepare_pll(struct intel_crtc *crtc,
  6271. const struct intel_crtc_state *pipe_config)
  6272. {
  6273. struct drm_device *dev = crtc->base.dev;
  6274. struct drm_i915_private *dev_priv = dev->dev_private;
  6275. int pipe = crtc->pipe;
  6276. i915_reg_t dpll_reg = DPLL(crtc->pipe);
  6277. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6278. u32 loopfilter, tribuf_calcntr;
  6279. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6280. u32 dpio_val;
  6281. int vco;
  6282. bestn = pipe_config->dpll.n;
  6283. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6284. bestm1 = pipe_config->dpll.m1;
  6285. bestm2 = pipe_config->dpll.m2 >> 22;
  6286. bestp1 = pipe_config->dpll.p1;
  6287. bestp2 = pipe_config->dpll.p2;
  6288. vco = pipe_config->dpll.vco;
  6289. dpio_val = 0;
  6290. loopfilter = 0;
  6291. /*
  6292. * Enable Refclk and SSC
  6293. */
  6294. I915_WRITE(dpll_reg,
  6295. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6296. mutex_lock(&dev_priv->sb_lock);
  6297. /* p1 and p2 divider */
  6298. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6299. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6300. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6301. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6302. 1 << DPIO_CHV_K_DIV_SHIFT);
  6303. /* Feedback post-divider - m2 */
  6304. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6305. /* Feedback refclk divider - n and m1 */
  6306. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6307. DPIO_CHV_M1_DIV_BY_2 |
  6308. 1 << DPIO_CHV_N_DIV_SHIFT);
  6309. /* M2 fraction division */
  6310. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6311. /* M2 fraction division enable */
  6312. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6313. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6314. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6315. if (bestm2_frac)
  6316. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6317. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6318. /* Program digital lock detect threshold */
  6319. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6320. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6321. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6322. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6323. if (!bestm2_frac)
  6324. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6325. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6326. /* Loop filter */
  6327. if (vco == 5400000) {
  6328. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6329. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6330. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6331. tribuf_calcntr = 0x9;
  6332. } else if (vco <= 6200000) {
  6333. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6334. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6335. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6336. tribuf_calcntr = 0x9;
  6337. } else if (vco <= 6480000) {
  6338. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6339. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6340. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6341. tribuf_calcntr = 0x8;
  6342. } else {
  6343. /* Not supported. Apply the same limits as in the max case */
  6344. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6345. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6346. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6347. tribuf_calcntr = 0;
  6348. }
  6349. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6350. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6351. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6352. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6353. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6354. /* AFC Recal */
  6355. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6356. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6357. DPIO_AFC_RECAL);
  6358. mutex_unlock(&dev_priv->sb_lock);
  6359. }
  6360. /**
  6361. * vlv_force_pll_on - forcibly enable just the PLL
  6362. * @dev_priv: i915 private structure
  6363. * @pipe: pipe PLL to enable
  6364. * @dpll: PLL configuration
  6365. *
  6366. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6367. * in cases where we need the PLL enabled even when @pipe is not going to
  6368. * be enabled.
  6369. */
  6370. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6371. const struct dpll *dpll)
  6372. {
  6373. struct intel_crtc *crtc =
  6374. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6375. struct intel_crtc_state *pipe_config;
  6376. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6377. if (!pipe_config)
  6378. return -ENOMEM;
  6379. pipe_config->base.crtc = &crtc->base;
  6380. pipe_config->pixel_multiplier = 1;
  6381. pipe_config->dpll = *dpll;
  6382. if (IS_CHERRYVIEW(dev)) {
  6383. chv_compute_dpll(crtc, pipe_config);
  6384. chv_prepare_pll(crtc, pipe_config);
  6385. chv_enable_pll(crtc, pipe_config);
  6386. } else {
  6387. vlv_compute_dpll(crtc, pipe_config);
  6388. vlv_prepare_pll(crtc, pipe_config);
  6389. vlv_enable_pll(crtc, pipe_config);
  6390. }
  6391. kfree(pipe_config);
  6392. return 0;
  6393. }
  6394. /**
  6395. * vlv_force_pll_off - forcibly disable just the PLL
  6396. * @dev_priv: i915 private structure
  6397. * @pipe: pipe PLL to disable
  6398. *
  6399. * Disable the PLL for @pipe. To be used in cases where we need
  6400. * the PLL enabled even when @pipe is not going to be enabled.
  6401. */
  6402. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6403. {
  6404. if (IS_CHERRYVIEW(dev))
  6405. chv_disable_pll(to_i915(dev), pipe);
  6406. else
  6407. vlv_disable_pll(to_i915(dev), pipe);
  6408. }
  6409. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6410. struct intel_crtc_state *crtc_state,
  6411. intel_clock_t *reduced_clock,
  6412. int num_connectors)
  6413. {
  6414. struct drm_device *dev = crtc->base.dev;
  6415. struct drm_i915_private *dev_priv = dev->dev_private;
  6416. u32 dpll;
  6417. bool is_sdvo;
  6418. struct dpll *clock = &crtc_state->dpll;
  6419. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6420. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6421. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6422. dpll = DPLL_VGA_MODE_DIS;
  6423. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6424. dpll |= DPLLB_MODE_LVDS;
  6425. else
  6426. dpll |= DPLLB_MODE_DAC_SERIAL;
  6427. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6428. dpll |= (crtc_state->pixel_multiplier - 1)
  6429. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6430. }
  6431. if (is_sdvo)
  6432. dpll |= DPLL_SDVO_HIGH_SPEED;
  6433. if (crtc_state->has_dp_encoder)
  6434. dpll |= DPLL_SDVO_HIGH_SPEED;
  6435. /* compute bitmask from p1 value */
  6436. if (IS_PINEVIEW(dev))
  6437. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6438. else {
  6439. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6440. if (IS_G4X(dev) && reduced_clock)
  6441. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6442. }
  6443. switch (clock->p2) {
  6444. case 5:
  6445. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6446. break;
  6447. case 7:
  6448. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6449. break;
  6450. case 10:
  6451. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6452. break;
  6453. case 14:
  6454. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6455. break;
  6456. }
  6457. if (INTEL_INFO(dev)->gen >= 4)
  6458. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6459. if (crtc_state->sdvo_tv_clock)
  6460. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6461. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6462. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6463. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6464. else
  6465. dpll |= PLL_REF_INPUT_DREFCLK;
  6466. dpll |= DPLL_VCO_ENABLE;
  6467. crtc_state->dpll_hw_state.dpll = dpll;
  6468. if (INTEL_INFO(dev)->gen >= 4) {
  6469. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6470. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6471. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6472. }
  6473. }
  6474. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6475. struct intel_crtc_state *crtc_state,
  6476. intel_clock_t *reduced_clock,
  6477. int num_connectors)
  6478. {
  6479. struct drm_device *dev = crtc->base.dev;
  6480. struct drm_i915_private *dev_priv = dev->dev_private;
  6481. u32 dpll;
  6482. struct dpll *clock = &crtc_state->dpll;
  6483. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6484. dpll = DPLL_VGA_MODE_DIS;
  6485. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6486. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6487. } else {
  6488. if (clock->p1 == 2)
  6489. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6490. else
  6491. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6492. if (clock->p2 == 4)
  6493. dpll |= PLL_P2_DIVIDE_BY_4;
  6494. }
  6495. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6496. dpll |= DPLL_DVO_2X_MODE;
  6497. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6498. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6499. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6500. else
  6501. dpll |= PLL_REF_INPUT_DREFCLK;
  6502. dpll |= DPLL_VCO_ENABLE;
  6503. crtc_state->dpll_hw_state.dpll = dpll;
  6504. }
  6505. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6506. {
  6507. struct drm_device *dev = intel_crtc->base.dev;
  6508. struct drm_i915_private *dev_priv = dev->dev_private;
  6509. enum pipe pipe = intel_crtc->pipe;
  6510. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6511. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6512. uint32_t crtc_vtotal, crtc_vblank_end;
  6513. int vsyncshift = 0;
  6514. /* We need to be careful not to changed the adjusted mode, for otherwise
  6515. * the hw state checker will get angry at the mismatch. */
  6516. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6517. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6518. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6519. /* the chip adds 2 halflines automatically */
  6520. crtc_vtotal -= 1;
  6521. crtc_vblank_end -= 1;
  6522. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6523. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6524. else
  6525. vsyncshift = adjusted_mode->crtc_hsync_start -
  6526. adjusted_mode->crtc_htotal / 2;
  6527. if (vsyncshift < 0)
  6528. vsyncshift += adjusted_mode->crtc_htotal;
  6529. }
  6530. if (INTEL_INFO(dev)->gen > 3)
  6531. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6532. I915_WRITE(HTOTAL(cpu_transcoder),
  6533. (adjusted_mode->crtc_hdisplay - 1) |
  6534. ((adjusted_mode->crtc_htotal - 1) << 16));
  6535. I915_WRITE(HBLANK(cpu_transcoder),
  6536. (adjusted_mode->crtc_hblank_start - 1) |
  6537. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6538. I915_WRITE(HSYNC(cpu_transcoder),
  6539. (adjusted_mode->crtc_hsync_start - 1) |
  6540. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6541. I915_WRITE(VTOTAL(cpu_transcoder),
  6542. (adjusted_mode->crtc_vdisplay - 1) |
  6543. ((crtc_vtotal - 1) << 16));
  6544. I915_WRITE(VBLANK(cpu_transcoder),
  6545. (adjusted_mode->crtc_vblank_start - 1) |
  6546. ((crtc_vblank_end - 1) << 16));
  6547. I915_WRITE(VSYNC(cpu_transcoder),
  6548. (adjusted_mode->crtc_vsync_start - 1) |
  6549. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6550. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6551. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6552. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6553. * bits. */
  6554. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6555. (pipe == PIPE_B || pipe == PIPE_C))
  6556. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6557. /* pipesrc controls the size that is scaled from, which should
  6558. * always be the user's requested size.
  6559. */
  6560. I915_WRITE(PIPESRC(pipe),
  6561. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6562. (intel_crtc->config->pipe_src_h - 1));
  6563. }
  6564. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6565. struct intel_crtc_state *pipe_config)
  6566. {
  6567. struct drm_device *dev = crtc->base.dev;
  6568. struct drm_i915_private *dev_priv = dev->dev_private;
  6569. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6570. uint32_t tmp;
  6571. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6572. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6573. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6574. tmp = I915_READ(HBLANK(cpu_transcoder));
  6575. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6576. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6577. tmp = I915_READ(HSYNC(cpu_transcoder));
  6578. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6579. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6580. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6581. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6582. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6583. tmp = I915_READ(VBLANK(cpu_transcoder));
  6584. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6585. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6586. tmp = I915_READ(VSYNC(cpu_transcoder));
  6587. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6588. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6589. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6590. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6591. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6592. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6593. }
  6594. tmp = I915_READ(PIPESRC(crtc->pipe));
  6595. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6596. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6597. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6598. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6599. }
  6600. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6601. struct intel_crtc_state *pipe_config)
  6602. {
  6603. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6604. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6605. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6606. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6607. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6608. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6609. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6610. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6611. mode->flags = pipe_config->base.adjusted_mode.flags;
  6612. mode->type = DRM_MODE_TYPE_DRIVER;
  6613. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6614. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6615. mode->hsync = drm_mode_hsync(mode);
  6616. mode->vrefresh = drm_mode_vrefresh(mode);
  6617. drm_mode_set_name(mode);
  6618. }
  6619. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6620. {
  6621. struct drm_device *dev = intel_crtc->base.dev;
  6622. struct drm_i915_private *dev_priv = dev->dev_private;
  6623. uint32_t pipeconf;
  6624. pipeconf = 0;
  6625. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6626. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6627. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6628. if (intel_crtc->config->double_wide)
  6629. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6630. /* only g4x and later have fancy bpc/dither controls */
  6631. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6632. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6633. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6634. pipeconf |= PIPECONF_DITHER_EN |
  6635. PIPECONF_DITHER_TYPE_SP;
  6636. switch (intel_crtc->config->pipe_bpp) {
  6637. case 18:
  6638. pipeconf |= PIPECONF_6BPC;
  6639. break;
  6640. case 24:
  6641. pipeconf |= PIPECONF_8BPC;
  6642. break;
  6643. case 30:
  6644. pipeconf |= PIPECONF_10BPC;
  6645. break;
  6646. default:
  6647. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6648. BUG();
  6649. }
  6650. }
  6651. if (HAS_PIPE_CXSR(dev)) {
  6652. if (intel_crtc->lowfreq_avail) {
  6653. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6654. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6655. } else {
  6656. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6657. }
  6658. }
  6659. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6660. if (INTEL_INFO(dev)->gen < 4 ||
  6661. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6662. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6663. else
  6664. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6665. } else
  6666. pipeconf |= PIPECONF_PROGRESSIVE;
  6667. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6668. intel_crtc->config->limited_color_range)
  6669. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6670. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6671. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6672. }
  6673. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6674. struct intel_crtc_state *crtc_state)
  6675. {
  6676. struct drm_device *dev = crtc->base.dev;
  6677. struct drm_i915_private *dev_priv = dev->dev_private;
  6678. int refclk, num_connectors = 0;
  6679. intel_clock_t clock;
  6680. bool ok;
  6681. const intel_limit_t *limit;
  6682. struct drm_atomic_state *state = crtc_state->base.state;
  6683. struct drm_connector *connector;
  6684. struct drm_connector_state *connector_state;
  6685. int i;
  6686. memset(&crtc_state->dpll_hw_state, 0,
  6687. sizeof(crtc_state->dpll_hw_state));
  6688. if (crtc_state->has_dsi_encoder)
  6689. return 0;
  6690. for_each_connector_in_state(state, connector, connector_state, i) {
  6691. if (connector_state->crtc == &crtc->base)
  6692. num_connectors++;
  6693. }
  6694. if (!crtc_state->clock_set) {
  6695. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6696. /*
  6697. * Returns a set of divisors for the desired target clock with
  6698. * the given refclk, or FALSE. The returned values represent
  6699. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6700. * 2) / p1 / p2.
  6701. */
  6702. limit = intel_limit(crtc_state, refclk);
  6703. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6704. crtc_state->port_clock,
  6705. refclk, NULL, &clock);
  6706. if (!ok) {
  6707. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6708. return -EINVAL;
  6709. }
  6710. /* Compat-code for transition, will disappear. */
  6711. crtc_state->dpll.n = clock.n;
  6712. crtc_state->dpll.m1 = clock.m1;
  6713. crtc_state->dpll.m2 = clock.m2;
  6714. crtc_state->dpll.p1 = clock.p1;
  6715. crtc_state->dpll.p2 = clock.p2;
  6716. }
  6717. if (IS_GEN2(dev)) {
  6718. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6719. num_connectors);
  6720. } else if (IS_CHERRYVIEW(dev)) {
  6721. chv_compute_dpll(crtc, crtc_state);
  6722. } else if (IS_VALLEYVIEW(dev)) {
  6723. vlv_compute_dpll(crtc, crtc_state);
  6724. } else {
  6725. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6726. num_connectors);
  6727. }
  6728. return 0;
  6729. }
  6730. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6731. struct intel_crtc_state *pipe_config)
  6732. {
  6733. struct drm_device *dev = crtc->base.dev;
  6734. struct drm_i915_private *dev_priv = dev->dev_private;
  6735. uint32_t tmp;
  6736. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6737. return;
  6738. tmp = I915_READ(PFIT_CONTROL);
  6739. if (!(tmp & PFIT_ENABLE))
  6740. return;
  6741. /* Check whether the pfit is attached to our pipe. */
  6742. if (INTEL_INFO(dev)->gen < 4) {
  6743. if (crtc->pipe != PIPE_B)
  6744. return;
  6745. } else {
  6746. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6747. return;
  6748. }
  6749. pipe_config->gmch_pfit.control = tmp;
  6750. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6751. if (INTEL_INFO(dev)->gen < 5)
  6752. pipe_config->gmch_pfit.lvds_border_bits =
  6753. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6754. }
  6755. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6756. struct intel_crtc_state *pipe_config)
  6757. {
  6758. struct drm_device *dev = crtc->base.dev;
  6759. struct drm_i915_private *dev_priv = dev->dev_private;
  6760. int pipe = pipe_config->cpu_transcoder;
  6761. intel_clock_t clock;
  6762. u32 mdiv;
  6763. int refclk = 100000;
  6764. /* In case of MIPI DPLL will not even be used */
  6765. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6766. return;
  6767. mutex_lock(&dev_priv->sb_lock);
  6768. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6769. mutex_unlock(&dev_priv->sb_lock);
  6770. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6771. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6772. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6773. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6774. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6775. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6776. }
  6777. static void
  6778. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6779. struct intel_initial_plane_config *plane_config)
  6780. {
  6781. struct drm_device *dev = crtc->base.dev;
  6782. struct drm_i915_private *dev_priv = dev->dev_private;
  6783. u32 val, base, offset;
  6784. int pipe = crtc->pipe, plane = crtc->plane;
  6785. int fourcc, pixel_format;
  6786. unsigned int aligned_height;
  6787. struct drm_framebuffer *fb;
  6788. struct intel_framebuffer *intel_fb;
  6789. val = I915_READ(DSPCNTR(plane));
  6790. if (!(val & DISPLAY_PLANE_ENABLE))
  6791. return;
  6792. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6793. if (!intel_fb) {
  6794. DRM_DEBUG_KMS("failed to alloc fb\n");
  6795. return;
  6796. }
  6797. fb = &intel_fb->base;
  6798. if (INTEL_INFO(dev)->gen >= 4) {
  6799. if (val & DISPPLANE_TILED) {
  6800. plane_config->tiling = I915_TILING_X;
  6801. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6802. }
  6803. }
  6804. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6805. fourcc = i9xx_format_to_fourcc(pixel_format);
  6806. fb->pixel_format = fourcc;
  6807. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6808. if (INTEL_INFO(dev)->gen >= 4) {
  6809. if (plane_config->tiling)
  6810. offset = I915_READ(DSPTILEOFF(plane));
  6811. else
  6812. offset = I915_READ(DSPLINOFF(plane));
  6813. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6814. } else {
  6815. base = I915_READ(DSPADDR(plane));
  6816. }
  6817. plane_config->base = base;
  6818. val = I915_READ(PIPESRC(pipe));
  6819. fb->width = ((val >> 16) & 0xfff) + 1;
  6820. fb->height = ((val >> 0) & 0xfff) + 1;
  6821. val = I915_READ(DSPSTRIDE(pipe));
  6822. fb->pitches[0] = val & 0xffffffc0;
  6823. aligned_height = intel_fb_align_height(dev, fb->height,
  6824. fb->pixel_format,
  6825. fb->modifier[0]);
  6826. plane_config->size = fb->pitches[0] * aligned_height;
  6827. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6828. pipe_name(pipe), plane, fb->width, fb->height,
  6829. fb->bits_per_pixel, base, fb->pitches[0],
  6830. plane_config->size);
  6831. plane_config->fb = intel_fb;
  6832. }
  6833. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6834. struct intel_crtc_state *pipe_config)
  6835. {
  6836. struct drm_device *dev = crtc->base.dev;
  6837. struct drm_i915_private *dev_priv = dev->dev_private;
  6838. int pipe = pipe_config->cpu_transcoder;
  6839. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6840. intel_clock_t clock;
  6841. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6842. int refclk = 100000;
  6843. mutex_lock(&dev_priv->sb_lock);
  6844. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6845. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6846. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6847. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6848. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6849. mutex_unlock(&dev_priv->sb_lock);
  6850. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6851. clock.m2 = (pll_dw0 & 0xff) << 22;
  6852. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6853. clock.m2 |= pll_dw2 & 0x3fffff;
  6854. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6855. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6856. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6857. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6858. }
  6859. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6860. struct intel_crtc_state *pipe_config)
  6861. {
  6862. struct drm_device *dev = crtc->base.dev;
  6863. struct drm_i915_private *dev_priv = dev->dev_private;
  6864. uint32_t tmp;
  6865. if (!intel_display_power_is_enabled(dev_priv,
  6866. POWER_DOMAIN_PIPE(crtc->pipe)))
  6867. return false;
  6868. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6869. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6870. tmp = I915_READ(PIPECONF(crtc->pipe));
  6871. if (!(tmp & PIPECONF_ENABLE))
  6872. return false;
  6873. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6874. switch (tmp & PIPECONF_BPC_MASK) {
  6875. case PIPECONF_6BPC:
  6876. pipe_config->pipe_bpp = 18;
  6877. break;
  6878. case PIPECONF_8BPC:
  6879. pipe_config->pipe_bpp = 24;
  6880. break;
  6881. case PIPECONF_10BPC:
  6882. pipe_config->pipe_bpp = 30;
  6883. break;
  6884. default:
  6885. break;
  6886. }
  6887. }
  6888. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6889. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6890. pipe_config->limited_color_range = true;
  6891. if (INTEL_INFO(dev)->gen < 4)
  6892. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6893. intel_get_pipe_timings(crtc, pipe_config);
  6894. i9xx_get_pfit_config(crtc, pipe_config);
  6895. if (INTEL_INFO(dev)->gen >= 4) {
  6896. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6897. pipe_config->pixel_multiplier =
  6898. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6899. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6900. pipe_config->dpll_hw_state.dpll_md = tmp;
  6901. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6902. tmp = I915_READ(DPLL(crtc->pipe));
  6903. pipe_config->pixel_multiplier =
  6904. ((tmp & SDVO_MULTIPLIER_MASK)
  6905. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6906. } else {
  6907. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6908. * port and will be fixed up in the encoder->get_config
  6909. * function. */
  6910. pipe_config->pixel_multiplier = 1;
  6911. }
  6912. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6913. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6914. /*
  6915. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6916. * on 830. Filter it out here so that we don't
  6917. * report errors due to that.
  6918. */
  6919. if (IS_I830(dev))
  6920. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6921. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6922. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6923. } else {
  6924. /* Mask out read-only status bits. */
  6925. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6926. DPLL_PORTC_READY_MASK |
  6927. DPLL_PORTB_READY_MASK);
  6928. }
  6929. if (IS_CHERRYVIEW(dev))
  6930. chv_crtc_clock_get(crtc, pipe_config);
  6931. else if (IS_VALLEYVIEW(dev))
  6932. vlv_crtc_clock_get(crtc, pipe_config);
  6933. else
  6934. i9xx_crtc_clock_get(crtc, pipe_config);
  6935. /*
  6936. * Normally the dotclock is filled in by the encoder .get_config()
  6937. * but in case the pipe is enabled w/o any ports we need a sane
  6938. * default.
  6939. */
  6940. pipe_config->base.adjusted_mode.crtc_clock =
  6941. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6942. return true;
  6943. }
  6944. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6945. {
  6946. struct drm_i915_private *dev_priv = dev->dev_private;
  6947. struct intel_encoder *encoder;
  6948. u32 val, final;
  6949. bool has_lvds = false;
  6950. bool has_cpu_edp = false;
  6951. bool has_panel = false;
  6952. bool has_ck505 = false;
  6953. bool can_ssc = false;
  6954. /* We need to take the global config into account */
  6955. for_each_intel_encoder(dev, encoder) {
  6956. switch (encoder->type) {
  6957. case INTEL_OUTPUT_LVDS:
  6958. has_panel = true;
  6959. has_lvds = true;
  6960. break;
  6961. case INTEL_OUTPUT_EDP:
  6962. has_panel = true;
  6963. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6964. has_cpu_edp = true;
  6965. break;
  6966. default:
  6967. break;
  6968. }
  6969. }
  6970. if (HAS_PCH_IBX(dev)) {
  6971. has_ck505 = dev_priv->vbt.display_clock_mode;
  6972. can_ssc = has_ck505;
  6973. } else {
  6974. has_ck505 = false;
  6975. can_ssc = true;
  6976. }
  6977. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6978. has_panel, has_lvds, has_ck505);
  6979. /* Ironlake: try to setup display ref clock before DPLL
  6980. * enabling. This is only under driver's control after
  6981. * PCH B stepping, previous chipset stepping should be
  6982. * ignoring this setting.
  6983. */
  6984. val = I915_READ(PCH_DREF_CONTROL);
  6985. /* As we must carefully and slowly disable/enable each source in turn,
  6986. * compute the final state we want first and check if we need to
  6987. * make any changes at all.
  6988. */
  6989. final = val;
  6990. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6991. if (has_ck505)
  6992. final |= DREF_NONSPREAD_CK505_ENABLE;
  6993. else
  6994. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6995. final &= ~DREF_SSC_SOURCE_MASK;
  6996. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6997. final &= ~DREF_SSC1_ENABLE;
  6998. if (has_panel) {
  6999. final |= DREF_SSC_SOURCE_ENABLE;
  7000. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7001. final |= DREF_SSC1_ENABLE;
  7002. if (has_cpu_edp) {
  7003. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7004. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7005. else
  7006. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7007. } else
  7008. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7009. } else {
  7010. final |= DREF_SSC_SOURCE_DISABLE;
  7011. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7012. }
  7013. if (final == val)
  7014. return;
  7015. /* Always enable nonspread source */
  7016. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7017. if (has_ck505)
  7018. val |= DREF_NONSPREAD_CK505_ENABLE;
  7019. else
  7020. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7021. if (has_panel) {
  7022. val &= ~DREF_SSC_SOURCE_MASK;
  7023. val |= DREF_SSC_SOURCE_ENABLE;
  7024. /* SSC must be turned on before enabling the CPU output */
  7025. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7026. DRM_DEBUG_KMS("Using SSC on panel\n");
  7027. val |= DREF_SSC1_ENABLE;
  7028. } else
  7029. val &= ~DREF_SSC1_ENABLE;
  7030. /* Get SSC going before enabling the outputs */
  7031. I915_WRITE(PCH_DREF_CONTROL, val);
  7032. POSTING_READ(PCH_DREF_CONTROL);
  7033. udelay(200);
  7034. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7035. /* Enable CPU source on CPU attached eDP */
  7036. if (has_cpu_edp) {
  7037. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7038. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7039. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7040. } else
  7041. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7042. } else
  7043. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7044. I915_WRITE(PCH_DREF_CONTROL, val);
  7045. POSTING_READ(PCH_DREF_CONTROL);
  7046. udelay(200);
  7047. } else {
  7048. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7049. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7050. /* Turn off CPU output */
  7051. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7052. I915_WRITE(PCH_DREF_CONTROL, val);
  7053. POSTING_READ(PCH_DREF_CONTROL);
  7054. udelay(200);
  7055. /* Turn off the SSC source */
  7056. val &= ~DREF_SSC_SOURCE_MASK;
  7057. val |= DREF_SSC_SOURCE_DISABLE;
  7058. /* Turn off SSC1 */
  7059. val &= ~DREF_SSC1_ENABLE;
  7060. I915_WRITE(PCH_DREF_CONTROL, val);
  7061. POSTING_READ(PCH_DREF_CONTROL);
  7062. udelay(200);
  7063. }
  7064. BUG_ON(val != final);
  7065. }
  7066. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7067. {
  7068. uint32_t tmp;
  7069. tmp = I915_READ(SOUTH_CHICKEN2);
  7070. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7071. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7072. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7073. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7074. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7075. tmp = I915_READ(SOUTH_CHICKEN2);
  7076. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7077. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7078. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7079. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7080. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7081. }
  7082. /* WaMPhyProgramming:hsw */
  7083. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7084. {
  7085. uint32_t tmp;
  7086. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7087. tmp &= ~(0xFF << 24);
  7088. tmp |= (0x12 << 24);
  7089. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7090. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7091. tmp |= (1 << 11);
  7092. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7093. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7094. tmp |= (1 << 11);
  7095. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7096. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7097. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7098. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7099. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7100. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7101. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7102. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7103. tmp &= ~(7 << 13);
  7104. tmp |= (5 << 13);
  7105. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7106. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7107. tmp &= ~(7 << 13);
  7108. tmp |= (5 << 13);
  7109. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7110. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7111. tmp &= ~0xFF;
  7112. tmp |= 0x1C;
  7113. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7114. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7115. tmp &= ~0xFF;
  7116. tmp |= 0x1C;
  7117. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7118. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7119. tmp &= ~(0xFF << 16);
  7120. tmp |= (0x1C << 16);
  7121. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7122. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7123. tmp &= ~(0xFF << 16);
  7124. tmp |= (0x1C << 16);
  7125. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7126. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7127. tmp |= (1 << 27);
  7128. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7129. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7130. tmp |= (1 << 27);
  7131. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7132. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7133. tmp &= ~(0xF << 28);
  7134. tmp |= (4 << 28);
  7135. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7136. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7137. tmp &= ~(0xF << 28);
  7138. tmp |= (4 << 28);
  7139. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7140. }
  7141. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7142. * Programming" based on the parameters passed:
  7143. * - Sequence to enable CLKOUT_DP
  7144. * - Sequence to enable CLKOUT_DP without spread
  7145. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7146. */
  7147. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7148. bool with_fdi)
  7149. {
  7150. struct drm_i915_private *dev_priv = dev->dev_private;
  7151. uint32_t reg, tmp;
  7152. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7153. with_spread = true;
  7154. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7155. with_fdi = false;
  7156. mutex_lock(&dev_priv->sb_lock);
  7157. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7158. tmp &= ~SBI_SSCCTL_DISABLE;
  7159. tmp |= SBI_SSCCTL_PATHALT;
  7160. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7161. udelay(24);
  7162. if (with_spread) {
  7163. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7164. tmp &= ~SBI_SSCCTL_PATHALT;
  7165. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7166. if (with_fdi) {
  7167. lpt_reset_fdi_mphy(dev_priv);
  7168. lpt_program_fdi_mphy(dev_priv);
  7169. }
  7170. }
  7171. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7172. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7173. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7174. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7175. mutex_unlock(&dev_priv->sb_lock);
  7176. }
  7177. /* Sequence to disable CLKOUT_DP */
  7178. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7179. {
  7180. struct drm_i915_private *dev_priv = dev->dev_private;
  7181. uint32_t reg, tmp;
  7182. mutex_lock(&dev_priv->sb_lock);
  7183. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7184. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7185. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7186. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7187. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7188. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7189. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7190. tmp |= SBI_SSCCTL_PATHALT;
  7191. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7192. udelay(32);
  7193. }
  7194. tmp |= SBI_SSCCTL_DISABLE;
  7195. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7196. }
  7197. mutex_unlock(&dev_priv->sb_lock);
  7198. }
  7199. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7200. static const uint16_t sscdivintphase[] = {
  7201. [BEND_IDX( 50)] = 0x3B23,
  7202. [BEND_IDX( 45)] = 0x3B23,
  7203. [BEND_IDX( 40)] = 0x3C23,
  7204. [BEND_IDX( 35)] = 0x3C23,
  7205. [BEND_IDX( 30)] = 0x3D23,
  7206. [BEND_IDX( 25)] = 0x3D23,
  7207. [BEND_IDX( 20)] = 0x3E23,
  7208. [BEND_IDX( 15)] = 0x3E23,
  7209. [BEND_IDX( 10)] = 0x3F23,
  7210. [BEND_IDX( 5)] = 0x3F23,
  7211. [BEND_IDX( 0)] = 0x0025,
  7212. [BEND_IDX( -5)] = 0x0025,
  7213. [BEND_IDX(-10)] = 0x0125,
  7214. [BEND_IDX(-15)] = 0x0125,
  7215. [BEND_IDX(-20)] = 0x0225,
  7216. [BEND_IDX(-25)] = 0x0225,
  7217. [BEND_IDX(-30)] = 0x0325,
  7218. [BEND_IDX(-35)] = 0x0325,
  7219. [BEND_IDX(-40)] = 0x0425,
  7220. [BEND_IDX(-45)] = 0x0425,
  7221. [BEND_IDX(-50)] = 0x0525,
  7222. };
  7223. /*
  7224. * Bend CLKOUT_DP
  7225. * steps -50 to 50 inclusive, in steps of 5
  7226. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7227. * change in clock period = -(steps / 10) * 5.787 ps
  7228. */
  7229. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7230. {
  7231. uint32_t tmp;
  7232. int idx = BEND_IDX(steps);
  7233. if (WARN_ON(steps % 5 != 0))
  7234. return;
  7235. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7236. return;
  7237. mutex_lock(&dev_priv->sb_lock);
  7238. if (steps % 10 != 0)
  7239. tmp = 0xAAAAAAAB;
  7240. else
  7241. tmp = 0x00000000;
  7242. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7243. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7244. tmp &= 0xffff0000;
  7245. tmp |= sscdivintphase[idx];
  7246. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7247. mutex_unlock(&dev_priv->sb_lock);
  7248. }
  7249. #undef BEND_IDX
  7250. static void lpt_init_pch_refclk(struct drm_device *dev)
  7251. {
  7252. struct intel_encoder *encoder;
  7253. bool has_vga = false;
  7254. for_each_intel_encoder(dev, encoder) {
  7255. switch (encoder->type) {
  7256. case INTEL_OUTPUT_ANALOG:
  7257. has_vga = true;
  7258. break;
  7259. default:
  7260. break;
  7261. }
  7262. }
  7263. if (has_vga) {
  7264. lpt_bend_clkout_dp(to_i915(dev), 0);
  7265. lpt_enable_clkout_dp(dev, true, true);
  7266. } else {
  7267. lpt_disable_clkout_dp(dev);
  7268. }
  7269. }
  7270. /*
  7271. * Initialize reference clocks when the driver loads
  7272. */
  7273. void intel_init_pch_refclk(struct drm_device *dev)
  7274. {
  7275. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7276. ironlake_init_pch_refclk(dev);
  7277. else if (HAS_PCH_LPT(dev))
  7278. lpt_init_pch_refclk(dev);
  7279. }
  7280. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7281. {
  7282. struct drm_device *dev = crtc_state->base.crtc->dev;
  7283. struct drm_i915_private *dev_priv = dev->dev_private;
  7284. struct drm_atomic_state *state = crtc_state->base.state;
  7285. struct drm_connector *connector;
  7286. struct drm_connector_state *connector_state;
  7287. struct intel_encoder *encoder;
  7288. int num_connectors = 0, i;
  7289. bool is_lvds = false;
  7290. for_each_connector_in_state(state, connector, connector_state, i) {
  7291. if (connector_state->crtc != crtc_state->base.crtc)
  7292. continue;
  7293. encoder = to_intel_encoder(connector_state->best_encoder);
  7294. switch (encoder->type) {
  7295. case INTEL_OUTPUT_LVDS:
  7296. is_lvds = true;
  7297. break;
  7298. default:
  7299. break;
  7300. }
  7301. num_connectors++;
  7302. }
  7303. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7304. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7305. dev_priv->vbt.lvds_ssc_freq);
  7306. return dev_priv->vbt.lvds_ssc_freq;
  7307. }
  7308. return 120000;
  7309. }
  7310. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7311. {
  7312. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7313. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7314. int pipe = intel_crtc->pipe;
  7315. uint32_t val;
  7316. val = 0;
  7317. switch (intel_crtc->config->pipe_bpp) {
  7318. case 18:
  7319. val |= PIPECONF_6BPC;
  7320. break;
  7321. case 24:
  7322. val |= PIPECONF_8BPC;
  7323. break;
  7324. case 30:
  7325. val |= PIPECONF_10BPC;
  7326. break;
  7327. case 36:
  7328. val |= PIPECONF_12BPC;
  7329. break;
  7330. default:
  7331. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7332. BUG();
  7333. }
  7334. if (intel_crtc->config->dither)
  7335. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7336. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7337. val |= PIPECONF_INTERLACED_ILK;
  7338. else
  7339. val |= PIPECONF_PROGRESSIVE;
  7340. if (intel_crtc->config->limited_color_range)
  7341. val |= PIPECONF_COLOR_RANGE_SELECT;
  7342. I915_WRITE(PIPECONF(pipe), val);
  7343. POSTING_READ(PIPECONF(pipe));
  7344. }
  7345. /*
  7346. * Set up the pipe CSC unit.
  7347. *
  7348. * Currently only full range RGB to limited range RGB conversion
  7349. * is supported, but eventually this should handle various
  7350. * RGB<->YCbCr scenarios as well.
  7351. */
  7352. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7353. {
  7354. struct drm_device *dev = crtc->dev;
  7355. struct drm_i915_private *dev_priv = dev->dev_private;
  7356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7357. int pipe = intel_crtc->pipe;
  7358. uint16_t coeff = 0x7800; /* 1.0 */
  7359. /*
  7360. * TODO: Check what kind of values actually come out of the pipe
  7361. * with these coeff/postoff values and adjust to get the best
  7362. * accuracy. Perhaps we even need to take the bpc value into
  7363. * consideration.
  7364. */
  7365. if (intel_crtc->config->limited_color_range)
  7366. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7367. /*
  7368. * GY/GU and RY/RU should be the other way around according
  7369. * to BSpec, but reality doesn't agree. Just set them up in
  7370. * a way that results in the correct picture.
  7371. */
  7372. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7373. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7374. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7375. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7376. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7377. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7378. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7379. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7380. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7381. if (INTEL_INFO(dev)->gen > 6) {
  7382. uint16_t postoff = 0;
  7383. if (intel_crtc->config->limited_color_range)
  7384. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7385. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7386. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7387. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7388. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7389. } else {
  7390. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7391. if (intel_crtc->config->limited_color_range)
  7392. mode |= CSC_BLACK_SCREEN_OFFSET;
  7393. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7394. }
  7395. }
  7396. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7397. {
  7398. struct drm_device *dev = crtc->dev;
  7399. struct drm_i915_private *dev_priv = dev->dev_private;
  7400. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7401. enum pipe pipe = intel_crtc->pipe;
  7402. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7403. uint32_t val;
  7404. val = 0;
  7405. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7406. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7407. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7408. val |= PIPECONF_INTERLACED_ILK;
  7409. else
  7410. val |= PIPECONF_PROGRESSIVE;
  7411. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7412. POSTING_READ(PIPECONF(cpu_transcoder));
  7413. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7414. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7415. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7416. val = 0;
  7417. switch (intel_crtc->config->pipe_bpp) {
  7418. case 18:
  7419. val |= PIPEMISC_DITHER_6_BPC;
  7420. break;
  7421. case 24:
  7422. val |= PIPEMISC_DITHER_8_BPC;
  7423. break;
  7424. case 30:
  7425. val |= PIPEMISC_DITHER_10_BPC;
  7426. break;
  7427. case 36:
  7428. val |= PIPEMISC_DITHER_12_BPC;
  7429. break;
  7430. default:
  7431. /* Case prevented by pipe_config_set_bpp. */
  7432. BUG();
  7433. }
  7434. if (intel_crtc->config->dither)
  7435. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7436. I915_WRITE(PIPEMISC(pipe), val);
  7437. }
  7438. }
  7439. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7440. struct intel_crtc_state *crtc_state,
  7441. intel_clock_t *clock,
  7442. bool *has_reduced_clock,
  7443. intel_clock_t *reduced_clock)
  7444. {
  7445. struct drm_device *dev = crtc->dev;
  7446. struct drm_i915_private *dev_priv = dev->dev_private;
  7447. int refclk;
  7448. const intel_limit_t *limit;
  7449. bool ret;
  7450. refclk = ironlake_get_refclk(crtc_state);
  7451. /*
  7452. * Returns a set of divisors for the desired target clock with the given
  7453. * refclk, or FALSE. The returned values represent the clock equation:
  7454. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7455. */
  7456. limit = intel_limit(crtc_state, refclk);
  7457. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7458. crtc_state->port_clock,
  7459. refclk, NULL, clock);
  7460. if (!ret)
  7461. return false;
  7462. return true;
  7463. }
  7464. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7465. {
  7466. /*
  7467. * Account for spread spectrum to avoid
  7468. * oversubscribing the link. Max center spread
  7469. * is 2.5%; use 5% for safety's sake.
  7470. */
  7471. u32 bps = target_clock * bpp * 21 / 20;
  7472. return DIV_ROUND_UP(bps, link_bw * 8);
  7473. }
  7474. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7475. {
  7476. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7477. }
  7478. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7479. struct intel_crtc_state *crtc_state,
  7480. u32 *fp,
  7481. intel_clock_t *reduced_clock, u32 *fp2)
  7482. {
  7483. struct drm_crtc *crtc = &intel_crtc->base;
  7484. struct drm_device *dev = crtc->dev;
  7485. struct drm_i915_private *dev_priv = dev->dev_private;
  7486. struct drm_atomic_state *state = crtc_state->base.state;
  7487. struct drm_connector *connector;
  7488. struct drm_connector_state *connector_state;
  7489. struct intel_encoder *encoder;
  7490. uint32_t dpll;
  7491. int factor, num_connectors = 0, i;
  7492. bool is_lvds = false, is_sdvo = false;
  7493. for_each_connector_in_state(state, connector, connector_state, i) {
  7494. if (connector_state->crtc != crtc_state->base.crtc)
  7495. continue;
  7496. encoder = to_intel_encoder(connector_state->best_encoder);
  7497. switch (encoder->type) {
  7498. case INTEL_OUTPUT_LVDS:
  7499. is_lvds = true;
  7500. break;
  7501. case INTEL_OUTPUT_SDVO:
  7502. case INTEL_OUTPUT_HDMI:
  7503. is_sdvo = true;
  7504. break;
  7505. default:
  7506. break;
  7507. }
  7508. num_connectors++;
  7509. }
  7510. /* Enable autotuning of the PLL clock (if permissible) */
  7511. factor = 21;
  7512. if (is_lvds) {
  7513. if ((intel_panel_use_ssc(dev_priv) &&
  7514. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7515. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7516. factor = 25;
  7517. } else if (crtc_state->sdvo_tv_clock)
  7518. factor = 20;
  7519. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7520. *fp |= FP_CB_TUNE;
  7521. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7522. *fp2 |= FP_CB_TUNE;
  7523. dpll = 0;
  7524. if (is_lvds)
  7525. dpll |= DPLLB_MODE_LVDS;
  7526. else
  7527. dpll |= DPLLB_MODE_DAC_SERIAL;
  7528. dpll |= (crtc_state->pixel_multiplier - 1)
  7529. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7530. if (is_sdvo)
  7531. dpll |= DPLL_SDVO_HIGH_SPEED;
  7532. if (crtc_state->has_dp_encoder)
  7533. dpll |= DPLL_SDVO_HIGH_SPEED;
  7534. /* compute bitmask from p1 value */
  7535. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7536. /* also FPA1 */
  7537. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7538. switch (crtc_state->dpll.p2) {
  7539. case 5:
  7540. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7541. break;
  7542. case 7:
  7543. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7544. break;
  7545. case 10:
  7546. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7547. break;
  7548. case 14:
  7549. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7550. break;
  7551. }
  7552. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7553. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7554. else
  7555. dpll |= PLL_REF_INPUT_DREFCLK;
  7556. return dpll | DPLL_VCO_ENABLE;
  7557. }
  7558. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7559. struct intel_crtc_state *crtc_state)
  7560. {
  7561. struct drm_device *dev = crtc->base.dev;
  7562. intel_clock_t clock, reduced_clock;
  7563. u32 dpll = 0, fp = 0, fp2 = 0;
  7564. bool ok, has_reduced_clock = false;
  7565. bool is_lvds = false;
  7566. struct intel_shared_dpll *pll;
  7567. memset(&crtc_state->dpll_hw_state, 0,
  7568. sizeof(crtc_state->dpll_hw_state));
  7569. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7570. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7571. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7572. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7573. &has_reduced_clock, &reduced_clock);
  7574. if (!ok && !crtc_state->clock_set) {
  7575. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7576. return -EINVAL;
  7577. }
  7578. /* Compat-code for transition, will disappear. */
  7579. if (!crtc_state->clock_set) {
  7580. crtc_state->dpll.n = clock.n;
  7581. crtc_state->dpll.m1 = clock.m1;
  7582. crtc_state->dpll.m2 = clock.m2;
  7583. crtc_state->dpll.p1 = clock.p1;
  7584. crtc_state->dpll.p2 = clock.p2;
  7585. }
  7586. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7587. if (crtc_state->has_pch_encoder) {
  7588. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7589. if (has_reduced_clock)
  7590. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7591. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7592. &fp, &reduced_clock,
  7593. has_reduced_clock ? &fp2 : NULL);
  7594. crtc_state->dpll_hw_state.dpll = dpll;
  7595. crtc_state->dpll_hw_state.fp0 = fp;
  7596. if (has_reduced_clock)
  7597. crtc_state->dpll_hw_state.fp1 = fp2;
  7598. else
  7599. crtc_state->dpll_hw_state.fp1 = fp;
  7600. pll = intel_get_shared_dpll(crtc, crtc_state);
  7601. if (pll == NULL) {
  7602. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7603. pipe_name(crtc->pipe));
  7604. return -EINVAL;
  7605. }
  7606. }
  7607. if (is_lvds && has_reduced_clock)
  7608. crtc->lowfreq_avail = true;
  7609. else
  7610. crtc->lowfreq_avail = false;
  7611. return 0;
  7612. }
  7613. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7614. struct intel_link_m_n *m_n)
  7615. {
  7616. struct drm_device *dev = crtc->base.dev;
  7617. struct drm_i915_private *dev_priv = dev->dev_private;
  7618. enum pipe pipe = crtc->pipe;
  7619. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7620. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7621. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7622. & ~TU_SIZE_MASK;
  7623. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7624. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7625. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7626. }
  7627. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7628. enum transcoder transcoder,
  7629. struct intel_link_m_n *m_n,
  7630. struct intel_link_m_n *m2_n2)
  7631. {
  7632. struct drm_device *dev = crtc->base.dev;
  7633. struct drm_i915_private *dev_priv = dev->dev_private;
  7634. enum pipe pipe = crtc->pipe;
  7635. if (INTEL_INFO(dev)->gen >= 5) {
  7636. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7637. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7638. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7639. & ~TU_SIZE_MASK;
  7640. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7641. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7642. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7643. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7644. * gen < 8) and if DRRS is supported (to make sure the
  7645. * registers are not unnecessarily read).
  7646. */
  7647. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7648. crtc->config->has_drrs) {
  7649. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7650. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7651. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7652. & ~TU_SIZE_MASK;
  7653. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7654. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7655. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7656. }
  7657. } else {
  7658. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7659. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7660. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7661. & ~TU_SIZE_MASK;
  7662. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7663. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7664. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7665. }
  7666. }
  7667. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7668. struct intel_crtc_state *pipe_config)
  7669. {
  7670. if (pipe_config->has_pch_encoder)
  7671. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7672. else
  7673. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7674. &pipe_config->dp_m_n,
  7675. &pipe_config->dp_m2_n2);
  7676. }
  7677. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7678. struct intel_crtc_state *pipe_config)
  7679. {
  7680. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7681. &pipe_config->fdi_m_n, NULL);
  7682. }
  7683. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7684. struct intel_crtc_state *pipe_config)
  7685. {
  7686. struct drm_device *dev = crtc->base.dev;
  7687. struct drm_i915_private *dev_priv = dev->dev_private;
  7688. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7689. uint32_t ps_ctrl = 0;
  7690. int id = -1;
  7691. int i;
  7692. /* find scaler attached to this pipe */
  7693. for (i = 0; i < crtc->num_scalers; i++) {
  7694. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7695. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7696. id = i;
  7697. pipe_config->pch_pfit.enabled = true;
  7698. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7699. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7700. break;
  7701. }
  7702. }
  7703. scaler_state->scaler_id = id;
  7704. if (id >= 0) {
  7705. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7706. } else {
  7707. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7708. }
  7709. }
  7710. static void
  7711. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7712. struct intel_initial_plane_config *plane_config)
  7713. {
  7714. struct drm_device *dev = crtc->base.dev;
  7715. struct drm_i915_private *dev_priv = dev->dev_private;
  7716. u32 val, base, offset, stride_mult, tiling;
  7717. int pipe = crtc->pipe;
  7718. int fourcc, pixel_format;
  7719. unsigned int aligned_height;
  7720. struct drm_framebuffer *fb;
  7721. struct intel_framebuffer *intel_fb;
  7722. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7723. if (!intel_fb) {
  7724. DRM_DEBUG_KMS("failed to alloc fb\n");
  7725. return;
  7726. }
  7727. fb = &intel_fb->base;
  7728. val = I915_READ(PLANE_CTL(pipe, 0));
  7729. if (!(val & PLANE_CTL_ENABLE))
  7730. goto error;
  7731. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7732. fourcc = skl_format_to_fourcc(pixel_format,
  7733. val & PLANE_CTL_ORDER_RGBX,
  7734. val & PLANE_CTL_ALPHA_MASK);
  7735. fb->pixel_format = fourcc;
  7736. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7737. tiling = val & PLANE_CTL_TILED_MASK;
  7738. switch (tiling) {
  7739. case PLANE_CTL_TILED_LINEAR:
  7740. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7741. break;
  7742. case PLANE_CTL_TILED_X:
  7743. plane_config->tiling = I915_TILING_X;
  7744. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7745. break;
  7746. case PLANE_CTL_TILED_Y:
  7747. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7748. break;
  7749. case PLANE_CTL_TILED_YF:
  7750. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7751. break;
  7752. default:
  7753. MISSING_CASE(tiling);
  7754. goto error;
  7755. }
  7756. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7757. plane_config->base = base;
  7758. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7759. val = I915_READ(PLANE_SIZE(pipe, 0));
  7760. fb->height = ((val >> 16) & 0xfff) + 1;
  7761. fb->width = ((val >> 0) & 0x1fff) + 1;
  7762. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7763. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7764. fb->pixel_format);
  7765. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7766. aligned_height = intel_fb_align_height(dev, fb->height,
  7767. fb->pixel_format,
  7768. fb->modifier[0]);
  7769. plane_config->size = fb->pitches[0] * aligned_height;
  7770. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7771. pipe_name(pipe), fb->width, fb->height,
  7772. fb->bits_per_pixel, base, fb->pitches[0],
  7773. plane_config->size);
  7774. plane_config->fb = intel_fb;
  7775. return;
  7776. error:
  7777. kfree(fb);
  7778. }
  7779. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7780. struct intel_crtc_state *pipe_config)
  7781. {
  7782. struct drm_device *dev = crtc->base.dev;
  7783. struct drm_i915_private *dev_priv = dev->dev_private;
  7784. uint32_t tmp;
  7785. tmp = I915_READ(PF_CTL(crtc->pipe));
  7786. if (tmp & PF_ENABLE) {
  7787. pipe_config->pch_pfit.enabled = true;
  7788. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7789. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7790. /* We currently do not free assignements of panel fitters on
  7791. * ivb/hsw (since we don't use the higher upscaling modes which
  7792. * differentiates them) so just WARN about this case for now. */
  7793. if (IS_GEN7(dev)) {
  7794. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7795. PF_PIPE_SEL_IVB(crtc->pipe));
  7796. }
  7797. }
  7798. }
  7799. static void
  7800. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7801. struct intel_initial_plane_config *plane_config)
  7802. {
  7803. struct drm_device *dev = crtc->base.dev;
  7804. struct drm_i915_private *dev_priv = dev->dev_private;
  7805. u32 val, base, offset;
  7806. int pipe = crtc->pipe;
  7807. int fourcc, pixel_format;
  7808. unsigned int aligned_height;
  7809. struct drm_framebuffer *fb;
  7810. struct intel_framebuffer *intel_fb;
  7811. val = I915_READ(DSPCNTR(pipe));
  7812. if (!(val & DISPLAY_PLANE_ENABLE))
  7813. return;
  7814. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7815. if (!intel_fb) {
  7816. DRM_DEBUG_KMS("failed to alloc fb\n");
  7817. return;
  7818. }
  7819. fb = &intel_fb->base;
  7820. if (INTEL_INFO(dev)->gen >= 4) {
  7821. if (val & DISPPLANE_TILED) {
  7822. plane_config->tiling = I915_TILING_X;
  7823. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7824. }
  7825. }
  7826. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7827. fourcc = i9xx_format_to_fourcc(pixel_format);
  7828. fb->pixel_format = fourcc;
  7829. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7830. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7831. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7832. offset = I915_READ(DSPOFFSET(pipe));
  7833. } else {
  7834. if (plane_config->tiling)
  7835. offset = I915_READ(DSPTILEOFF(pipe));
  7836. else
  7837. offset = I915_READ(DSPLINOFF(pipe));
  7838. }
  7839. plane_config->base = base;
  7840. val = I915_READ(PIPESRC(pipe));
  7841. fb->width = ((val >> 16) & 0xfff) + 1;
  7842. fb->height = ((val >> 0) & 0xfff) + 1;
  7843. val = I915_READ(DSPSTRIDE(pipe));
  7844. fb->pitches[0] = val & 0xffffffc0;
  7845. aligned_height = intel_fb_align_height(dev, fb->height,
  7846. fb->pixel_format,
  7847. fb->modifier[0]);
  7848. plane_config->size = fb->pitches[0] * aligned_height;
  7849. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7850. pipe_name(pipe), fb->width, fb->height,
  7851. fb->bits_per_pixel, base, fb->pitches[0],
  7852. plane_config->size);
  7853. plane_config->fb = intel_fb;
  7854. }
  7855. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7856. struct intel_crtc_state *pipe_config)
  7857. {
  7858. struct drm_device *dev = crtc->base.dev;
  7859. struct drm_i915_private *dev_priv = dev->dev_private;
  7860. uint32_t tmp;
  7861. if (!intel_display_power_is_enabled(dev_priv,
  7862. POWER_DOMAIN_PIPE(crtc->pipe)))
  7863. return false;
  7864. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7865. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7866. tmp = I915_READ(PIPECONF(crtc->pipe));
  7867. if (!(tmp & PIPECONF_ENABLE))
  7868. return false;
  7869. switch (tmp & PIPECONF_BPC_MASK) {
  7870. case PIPECONF_6BPC:
  7871. pipe_config->pipe_bpp = 18;
  7872. break;
  7873. case PIPECONF_8BPC:
  7874. pipe_config->pipe_bpp = 24;
  7875. break;
  7876. case PIPECONF_10BPC:
  7877. pipe_config->pipe_bpp = 30;
  7878. break;
  7879. case PIPECONF_12BPC:
  7880. pipe_config->pipe_bpp = 36;
  7881. break;
  7882. default:
  7883. break;
  7884. }
  7885. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7886. pipe_config->limited_color_range = true;
  7887. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7888. struct intel_shared_dpll *pll;
  7889. pipe_config->has_pch_encoder = true;
  7890. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7891. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7892. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7893. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7894. if (HAS_PCH_IBX(dev_priv->dev)) {
  7895. pipe_config->shared_dpll =
  7896. (enum intel_dpll_id) crtc->pipe;
  7897. } else {
  7898. tmp = I915_READ(PCH_DPLL_SEL);
  7899. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7900. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7901. else
  7902. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7903. }
  7904. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7905. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7906. &pipe_config->dpll_hw_state));
  7907. tmp = pipe_config->dpll_hw_state.dpll;
  7908. pipe_config->pixel_multiplier =
  7909. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7910. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7911. ironlake_pch_clock_get(crtc, pipe_config);
  7912. } else {
  7913. pipe_config->pixel_multiplier = 1;
  7914. }
  7915. intel_get_pipe_timings(crtc, pipe_config);
  7916. ironlake_get_pfit_config(crtc, pipe_config);
  7917. return true;
  7918. }
  7919. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7920. {
  7921. struct drm_device *dev = dev_priv->dev;
  7922. struct intel_crtc *crtc;
  7923. for_each_intel_crtc(dev, crtc)
  7924. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7925. pipe_name(crtc->pipe));
  7926. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7927. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7928. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7929. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7930. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7931. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7932. "CPU PWM1 enabled\n");
  7933. if (IS_HASWELL(dev))
  7934. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7935. "CPU PWM2 enabled\n");
  7936. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7937. "PCH PWM1 enabled\n");
  7938. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7939. "Utility pin enabled\n");
  7940. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7941. /*
  7942. * In theory we can still leave IRQs enabled, as long as only the HPD
  7943. * interrupts remain enabled. We used to check for that, but since it's
  7944. * gen-specific and since we only disable LCPLL after we fully disable
  7945. * the interrupts, the check below should be enough.
  7946. */
  7947. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7948. }
  7949. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7950. {
  7951. struct drm_device *dev = dev_priv->dev;
  7952. if (IS_HASWELL(dev))
  7953. return I915_READ(D_COMP_HSW);
  7954. else
  7955. return I915_READ(D_COMP_BDW);
  7956. }
  7957. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7958. {
  7959. struct drm_device *dev = dev_priv->dev;
  7960. if (IS_HASWELL(dev)) {
  7961. mutex_lock(&dev_priv->rps.hw_lock);
  7962. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7963. val))
  7964. DRM_ERROR("Failed to write to D_COMP\n");
  7965. mutex_unlock(&dev_priv->rps.hw_lock);
  7966. } else {
  7967. I915_WRITE(D_COMP_BDW, val);
  7968. POSTING_READ(D_COMP_BDW);
  7969. }
  7970. }
  7971. /*
  7972. * This function implements pieces of two sequences from BSpec:
  7973. * - Sequence for display software to disable LCPLL
  7974. * - Sequence for display software to allow package C8+
  7975. * The steps implemented here are just the steps that actually touch the LCPLL
  7976. * register. Callers should take care of disabling all the display engine
  7977. * functions, doing the mode unset, fixing interrupts, etc.
  7978. */
  7979. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7980. bool switch_to_fclk, bool allow_power_down)
  7981. {
  7982. uint32_t val;
  7983. assert_can_disable_lcpll(dev_priv);
  7984. val = I915_READ(LCPLL_CTL);
  7985. if (switch_to_fclk) {
  7986. val |= LCPLL_CD_SOURCE_FCLK;
  7987. I915_WRITE(LCPLL_CTL, val);
  7988. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7989. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7990. DRM_ERROR("Switching to FCLK failed\n");
  7991. val = I915_READ(LCPLL_CTL);
  7992. }
  7993. val |= LCPLL_PLL_DISABLE;
  7994. I915_WRITE(LCPLL_CTL, val);
  7995. POSTING_READ(LCPLL_CTL);
  7996. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7997. DRM_ERROR("LCPLL still locked\n");
  7998. val = hsw_read_dcomp(dev_priv);
  7999. val |= D_COMP_COMP_DISABLE;
  8000. hsw_write_dcomp(dev_priv, val);
  8001. ndelay(100);
  8002. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8003. 1))
  8004. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8005. if (allow_power_down) {
  8006. val = I915_READ(LCPLL_CTL);
  8007. val |= LCPLL_POWER_DOWN_ALLOW;
  8008. I915_WRITE(LCPLL_CTL, val);
  8009. POSTING_READ(LCPLL_CTL);
  8010. }
  8011. }
  8012. /*
  8013. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8014. * source.
  8015. */
  8016. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8017. {
  8018. uint32_t val;
  8019. val = I915_READ(LCPLL_CTL);
  8020. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8021. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8022. return;
  8023. /*
  8024. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8025. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8026. */
  8027. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8028. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8029. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8030. I915_WRITE(LCPLL_CTL, val);
  8031. POSTING_READ(LCPLL_CTL);
  8032. }
  8033. val = hsw_read_dcomp(dev_priv);
  8034. val |= D_COMP_COMP_FORCE;
  8035. val &= ~D_COMP_COMP_DISABLE;
  8036. hsw_write_dcomp(dev_priv, val);
  8037. val = I915_READ(LCPLL_CTL);
  8038. val &= ~LCPLL_PLL_DISABLE;
  8039. I915_WRITE(LCPLL_CTL, val);
  8040. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  8041. DRM_ERROR("LCPLL not locked yet\n");
  8042. if (val & LCPLL_CD_SOURCE_FCLK) {
  8043. val = I915_READ(LCPLL_CTL);
  8044. val &= ~LCPLL_CD_SOURCE_FCLK;
  8045. I915_WRITE(LCPLL_CTL, val);
  8046. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8047. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8048. DRM_ERROR("Switching back to LCPLL failed\n");
  8049. }
  8050. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8051. intel_update_cdclk(dev_priv->dev);
  8052. }
  8053. /*
  8054. * Package states C8 and deeper are really deep PC states that can only be
  8055. * reached when all the devices on the system allow it, so even if the graphics
  8056. * device allows PC8+, it doesn't mean the system will actually get to these
  8057. * states. Our driver only allows PC8+ when going into runtime PM.
  8058. *
  8059. * The requirements for PC8+ are that all the outputs are disabled, the power
  8060. * well is disabled and most interrupts are disabled, and these are also
  8061. * requirements for runtime PM. When these conditions are met, we manually do
  8062. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8063. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8064. * hang the machine.
  8065. *
  8066. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8067. * the state of some registers, so when we come back from PC8+ we need to
  8068. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8069. * need to take care of the registers kept by RC6. Notice that this happens even
  8070. * if we don't put the device in PCI D3 state (which is what currently happens
  8071. * because of the runtime PM support).
  8072. *
  8073. * For more, read "Display Sequences for Package C8" on the hardware
  8074. * documentation.
  8075. */
  8076. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8077. {
  8078. struct drm_device *dev = dev_priv->dev;
  8079. uint32_t val;
  8080. DRM_DEBUG_KMS("Enabling package C8+\n");
  8081. if (HAS_PCH_LPT_LP(dev)) {
  8082. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8083. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8084. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8085. }
  8086. lpt_disable_clkout_dp(dev);
  8087. hsw_disable_lcpll(dev_priv, true, true);
  8088. }
  8089. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8090. {
  8091. struct drm_device *dev = dev_priv->dev;
  8092. uint32_t val;
  8093. DRM_DEBUG_KMS("Disabling package C8+\n");
  8094. hsw_restore_lcpll(dev_priv);
  8095. lpt_init_pch_refclk(dev);
  8096. if (HAS_PCH_LPT_LP(dev)) {
  8097. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8098. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8099. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8100. }
  8101. }
  8102. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8103. {
  8104. struct drm_device *dev = old_state->dev;
  8105. struct intel_atomic_state *old_intel_state =
  8106. to_intel_atomic_state(old_state);
  8107. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8108. broxton_set_cdclk(dev, req_cdclk);
  8109. }
  8110. /* compute the max rate for new configuration */
  8111. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8112. {
  8113. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8114. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8115. struct drm_crtc *crtc;
  8116. struct drm_crtc_state *cstate;
  8117. struct intel_crtc_state *crtc_state;
  8118. unsigned max_pixel_rate = 0, i;
  8119. enum pipe pipe;
  8120. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8121. sizeof(intel_state->min_pixclk));
  8122. for_each_crtc_in_state(state, crtc, cstate, i) {
  8123. int pixel_rate;
  8124. crtc_state = to_intel_crtc_state(cstate);
  8125. if (!crtc_state->base.enable) {
  8126. intel_state->min_pixclk[i] = 0;
  8127. continue;
  8128. }
  8129. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8130. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8131. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8132. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8133. intel_state->min_pixclk[i] = pixel_rate;
  8134. }
  8135. if (!intel_state->active_crtcs)
  8136. return 0;
  8137. for_each_pipe(dev_priv, pipe)
  8138. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8139. return max_pixel_rate;
  8140. }
  8141. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8142. {
  8143. struct drm_i915_private *dev_priv = dev->dev_private;
  8144. uint32_t val, data;
  8145. int ret;
  8146. if (WARN((I915_READ(LCPLL_CTL) &
  8147. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8148. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8149. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8150. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8151. "trying to change cdclk frequency with cdclk not enabled\n"))
  8152. return;
  8153. mutex_lock(&dev_priv->rps.hw_lock);
  8154. ret = sandybridge_pcode_write(dev_priv,
  8155. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8156. mutex_unlock(&dev_priv->rps.hw_lock);
  8157. if (ret) {
  8158. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8159. return;
  8160. }
  8161. val = I915_READ(LCPLL_CTL);
  8162. val |= LCPLL_CD_SOURCE_FCLK;
  8163. I915_WRITE(LCPLL_CTL, val);
  8164. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8165. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8166. DRM_ERROR("Switching to FCLK failed\n");
  8167. val = I915_READ(LCPLL_CTL);
  8168. val &= ~LCPLL_CLK_FREQ_MASK;
  8169. switch (cdclk) {
  8170. case 450000:
  8171. val |= LCPLL_CLK_FREQ_450;
  8172. data = 0;
  8173. break;
  8174. case 540000:
  8175. val |= LCPLL_CLK_FREQ_54O_BDW;
  8176. data = 1;
  8177. break;
  8178. case 337500:
  8179. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8180. data = 2;
  8181. break;
  8182. case 675000:
  8183. val |= LCPLL_CLK_FREQ_675_BDW;
  8184. data = 3;
  8185. break;
  8186. default:
  8187. WARN(1, "invalid cdclk frequency\n");
  8188. return;
  8189. }
  8190. I915_WRITE(LCPLL_CTL, val);
  8191. val = I915_READ(LCPLL_CTL);
  8192. val &= ~LCPLL_CD_SOURCE_FCLK;
  8193. I915_WRITE(LCPLL_CTL, val);
  8194. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8195. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8196. DRM_ERROR("Switching back to LCPLL failed\n");
  8197. mutex_lock(&dev_priv->rps.hw_lock);
  8198. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8199. mutex_unlock(&dev_priv->rps.hw_lock);
  8200. intel_update_cdclk(dev);
  8201. WARN(cdclk != dev_priv->cdclk_freq,
  8202. "cdclk requested %d kHz but got %d kHz\n",
  8203. cdclk, dev_priv->cdclk_freq);
  8204. }
  8205. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8206. {
  8207. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8208. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8209. int max_pixclk = ilk_max_pixel_rate(state);
  8210. int cdclk;
  8211. /*
  8212. * FIXME should also account for plane ratio
  8213. * once 64bpp pixel formats are supported.
  8214. */
  8215. if (max_pixclk > 540000)
  8216. cdclk = 675000;
  8217. else if (max_pixclk > 450000)
  8218. cdclk = 540000;
  8219. else if (max_pixclk > 337500)
  8220. cdclk = 450000;
  8221. else
  8222. cdclk = 337500;
  8223. if (cdclk > dev_priv->max_cdclk_freq) {
  8224. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8225. cdclk, dev_priv->max_cdclk_freq);
  8226. return -EINVAL;
  8227. }
  8228. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8229. if (!intel_state->active_crtcs)
  8230. intel_state->dev_cdclk = 337500;
  8231. return 0;
  8232. }
  8233. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8234. {
  8235. struct drm_device *dev = old_state->dev;
  8236. struct intel_atomic_state *old_intel_state =
  8237. to_intel_atomic_state(old_state);
  8238. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8239. broadwell_set_cdclk(dev, req_cdclk);
  8240. }
  8241. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8242. struct intel_crtc_state *crtc_state)
  8243. {
  8244. if (!intel_ddi_pll_select(crtc, crtc_state))
  8245. return -EINVAL;
  8246. crtc->lowfreq_avail = false;
  8247. return 0;
  8248. }
  8249. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8250. enum port port,
  8251. struct intel_crtc_state *pipe_config)
  8252. {
  8253. switch (port) {
  8254. case PORT_A:
  8255. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8256. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8257. break;
  8258. case PORT_B:
  8259. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8260. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8261. break;
  8262. case PORT_C:
  8263. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8264. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8265. break;
  8266. default:
  8267. DRM_ERROR("Incorrect port type\n");
  8268. }
  8269. }
  8270. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8271. enum port port,
  8272. struct intel_crtc_state *pipe_config)
  8273. {
  8274. u32 temp, dpll_ctl1;
  8275. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8276. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8277. switch (pipe_config->ddi_pll_sel) {
  8278. case SKL_DPLL0:
  8279. /*
  8280. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8281. * of the shared DPLL framework and thus needs to be read out
  8282. * separately
  8283. */
  8284. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8285. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8286. break;
  8287. case SKL_DPLL1:
  8288. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8289. break;
  8290. case SKL_DPLL2:
  8291. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8292. break;
  8293. case SKL_DPLL3:
  8294. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8295. break;
  8296. }
  8297. }
  8298. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8299. enum port port,
  8300. struct intel_crtc_state *pipe_config)
  8301. {
  8302. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8303. switch (pipe_config->ddi_pll_sel) {
  8304. case PORT_CLK_SEL_WRPLL1:
  8305. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8306. break;
  8307. case PORT_CLK_SEL_WRPLL2:
  8308. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8309. break;
  8310. case PORT_CLK_SEL_SPLL:
  8311. pipe_config->shared_dpll = DPLL_ID_SPLL;
  8312. break;
  8313. }
  8314. }
  8315. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8316. struct intel_crtc_state *pipe_config)
  8317. {
  8318. struct drm_device *dev = crtc->base.dev;
  8319. struct drm_i915_private *dev_priv = dev->dev_private;
  8320. struct intel_shared_dpll *pll;
  8321. enum port port;
  8322. uint32_t tmp;
  8323. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8324. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8325. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8326. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8327. else if (IS_BROXTON(dev))
  8328. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8329. else
  8330. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8331. if (pipe_config->shared_dpll >= 0) {
  8332. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8333. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8334. &pipe_config->dpll_hw_state));
  8335. }
  8336. /*
  8337. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8338. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8339. * the PCH transcoder is on.
  8340. */
  8341. if (INTEL_INFO(dev)->gen < 9 &&
  8342. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8343. pipe_config->has_pch_encoder = true;
  8344. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8345. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8346. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8347. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8348. }
  8349. }
  8350. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8351. struct intel_crtc_state *pipe_config)
  8352. {
  8353. struct drm_device *dev = crtc->base.dev;
  8354. struct drm_i915_private *dev_priv = dev->dev_private;
  8355. enum intel_display_power_domain pfit_domain;
  8356. uint32_t tmp;
  8357. if (!intel_display_power_is_enabled(dev_priv,
  8358. POWER_DOMAIN_PIPE(crtc->pipe)))
  8359. return false;
  8360. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8361. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8362. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8363. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8364. enum pipe trans_edp_pipe;
  8365. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8366. default:
  8367. WARN(1, "unknown pipe linked to edp transcoder\n");
  8368. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8369. case TRANS_DDI_EDP_INPUT_A_ON:
  8370. trans_edp_pipe = PIPE_A;
  8371. break;
  8372. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8373. trans_edp_pipe = PIPE_B;
  8374. break;
  8375. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8376. trans_edp_pipe = PIPE_C;
  8377. break;
  8378. }
  8379. if (trans_edp_pipe == crtc->pipe)
  8380. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8381. }
  8382. if (!intel_display_power_is_enabled(dev_priv,
  8383. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8384. return false;
  8385. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8386. if (!(tmp & PIPECONF_ENABLE))
  8387. return false;
  8388. haswell_get_ddi_port_state(crtc, pipe_config);
  8389. intel_get_pipe_timings(crtc, pipe_config);
  8390. if (INTEL_INFO(dev)->gen >= 9) {
  8391. skl_init_scalers(dev, crtc, pipe_config);
  8392. }
  8393. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8394. if (INTEL_INFO(dev)->gen >= 9) {
  8395. pipe_config->scaler_state.scaler_id = -1;
  8396. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8397. }
  8398. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8399. if (INTEL_INFO(dev)->gen >= 9)
  8400. skylake_get_pfit_config(crtc, pipe_config);
  8401. else
  8402. ironlake_get_pfit_config(crtc, pipe_config);
  8403. }
  8404. if (IS_HASWELL(dev))
  8405. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8406. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8407. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8408. pipe_config->pixel_multiplier =
  8409. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8410. } else {
  8411. pipe_config->pixel_multiplier = 1;
  8412. }
  8413. return true;
  8414. }
  8415. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8416. const struct intel_plane_state *plane_state)
  8417. {
  8418. struct drm_device *dev = crtc->dev;
  8419. struct drm_i915_private *dev_priv = dev->dev_private;
  8420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8421. uint32_t cntl = 0, size = 0;
  8422. if (plane_state && plane_state->visible) {
  8423. unsigned int width = plane_state->base.crtc_w;
  8424. unsigned int height = plane_state->base.crtc_h;
  8425. unsigned int stride = roundup_pow_of_two(width) * 4;
  8426. switch (stride) {
  8427. default:
  8428. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8429. width, stride);
  8430. stride = 256;
  8431. /* fallthrough */
  8432. case 256:
  8433. case 512:
  8434. case 1024:
  8435. case 2048:
  8436. break;
  8437. }
  8438. cntl |= CURSOR_ENABLE |
  8439. CURSOR_GAMMA_ENABLE |
  8440. CURSOR_FORMAT_ARGB |
  8441. CURSOR_STRIDE(stride);
  8442. size = (height << 12) | width;
  8443. }
  8444. if (intel_crtc->cursor_cntl != 0 &&
  8445. (intel_crtc->cursor_base != base ||
  8446. intel_crtc->cursor_size != size ||
  8447. intel_crtc->cursor_cntl != cntl)) {
  8448. /* On these chipsets we can only modify the base/size/stride
  8449. * whilst the cursor is disabled.
  8450. */
  8451. I915_WRITE(CURCNTR(PIPE_A), 0);
  8452. POSTING_READ(CURCNTR(PIPE_A));
  8453. intel_crtc->cursor_cntl = 0;
  8454. }
  8455. if (intel_crtc->cursor_base != base) {
  8456. I915_WRITE(CURBASE(PIPE_A), base);
  8457. intel_crtc->cursor_base = base;
  8458. }
  8459. if (intel_crtc->cursor_size != size) {
  8460. I915_WRITE(CURSIZE, size);
  8461. intel_crtc->cursor_size = size;
  8462. }
  8463. if (intel_crtc->cursor_cntl != cntl) {
  8464. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8465. POSTING_READ(CURCNTR(PIPE_A));
  8466. intel_crtc->cursor_cntl = cntl;
  8467. }
  8468. }
  8469. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8470. const struct intel_plane_state *plane_state)
  8471. {
  8472. struct drm_device *dev = crtc->dev;
  8473. struct drm_i915_private *dev_priv = dev->dev_private;
  8474. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8475. int pipe = intel_crtc->pipe;
  8476. uint32_t cntl = 0;
  8477. if (plane_state && plane_state->visible) {
  8478. cntl = MCURSOR_GAMMA_ENABLE;
  8479. switch (plane_state->base.crtc_w) {
  8480. case 64:
  8481. cntl |= CURSOR_MODE_64_ARGB_AX;
  8482. break;
  8483. case 128:
  8484. cntl |= CURSOR_MODE_128_ARGB_AX;
  8485. break;
  8486. case 256:
  8487. cntl |= CURSOR_MODE_256_ARGB_AX;
  8488. break;
  8489. default:
  8490. MISSING_CASE(plane_state->base.crtc_w);
  8491. return;
  8492. }
  8493. cntl |= pipe << 28; /* Connect to correct pipe */
  8494. if (HAS_DDI(dev))
  8495. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8496. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8497. cntl |= CURSOR_ROTATE_180;
  8498. }
  8499. if (intel_crtc->cursor_cntl != cntl) {
  8500. I915_WRITE(CURCNTR(pipe), cntl);
  8501. POSTING_READ(CURCNTR(pipe));
  8502. intel_crtc->cursor_cntl = cntl;
  8503. }
  8504. /* and commit changes on next vblank */
  8505. I915_WRITE(CURBASE(pipe), base);
  8506. POSTING_READ(CURBASE(pipe));
  8507. intel_crtc->cursor_base = base;
  8508. }
  8509. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8510. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8511. const struct intel_plane_state *plane_state)
  8512. {
  8513. struct drm_device *dev = crtc->dev;
  8514. struct drm_i915_private *dev_priv = dev->dev_private;
  8515. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8516. int pipe = intel_crtc->pipe;
  8517. u32 base = intel_crtc->cursor_addr;
  8518. u32 pos = 0;
  8519. if (plane_state) {
  8520. int x = plane_state->base.crtc_x;
  8521. int y = plane_state->base.crtc_y;
  8522. if (x < 0) {
  8523. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8524. x = -x;
  8525. }
  8526. pos |= x << CURSOR_X_SHIFT;
  8527. if (y < 0) {
  8528. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8529. y = -y;
  8530. }
  8531. pos |= y << CURSOR_Y_SHIFT;
  8532. /* ILK+ do this automagically */
  8533. if (HAS_GMCH_DISPLAY(dev) &&
  8534. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8535. base += (plane_state->base.crtc_h *
  8536. plane_state->base.crtc_w - 1) * 4;
  8537. }
  8538. }
  8539. I915_WRITE(CURPOS(pipe), pos);
  8540. if (IS_845G(dev) || IS_I865G(dev))
  8541. i845_update_cursor(crtc, base, plane_state);
  8542. else
  8543. i9xx_update_cursor(crtc, base, plane_state);
  8544. }
  8545. static bool cursor_size_ok(struct drm_device *dev,
  8546. uint32_t width, uint32_t height)
  8547. {
  8548. if (width == 0 || height == 0)
  8549. return false;
  8550. /*
  8551. * 845g/865g are special in that they are only limited by
  8552. * the width of their cursors, the height is arbitrary up to
  8553. * the precision of the register. Everything else requires
  8554. * square cursors, limited to a few power-of-two sizes.
  8555. */
  8556. if (IS_845G(dev) || IS_I865G(dev)) {
  8557. if ((width & 63) != 0)
  8558. return false;
  8559. if (width > (IS_845G(dev) ? 64 : 512))
  8560. return false;
  8561. if (height > 1023)
  8562. return false;
  8563. } else {
  8564. switch (width | height) {
  8565. case 256:
  8566. case 128:
  8567. if (IS_GEN2(dev))
  8568. return false;
  8569. case 64:
  8570. break;
  8571. default:
  8572. return false;
  8573. }
  8574. }
  8575. return true;
  8576. }
  8577. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8578. u16 *blue, uint32_t start, uint32_t size)
  8579. {
  8580. int end = (start + size > 256) ? 256 : start + size, i;
  8581. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8582. for (i = start; i < end; i++) {
  8583. intel_crtc->lut_r[i] = red[i] >> 8;
  8584. intel_crtc->lut_g[i] = green[i] >> 8;
  8585. intel_crtc->lut_b[i] = blue[i] >> 8;
  8586. }
  8587. intel_crtc_load_lut(crtc);
  8588. }
  8589. /* VESA 640x480x72Hz mode to set on the pipe */
  8590. static struct drm_display_mode load_detect_mode = {
  8591. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8592. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8593. };
  8594. struct drm_framebuffer *
  8595. __intel_framebuffer_create(struct drm_device *dev,
  8596. struct drm_mode_fb_cmd2 *mode_cmd,
  8597. struct drm_i915_gem_object *obj)
  8598. {
  8599. struct intel_framebuffer *intel_fb;
  8600. int ret;
  8601. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8602. if (!intel_fb)
  8603. return ERR_PTR(-ENOMEM);
  8604. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8605. if (ret)
  8606. goto err;
  8607. return &intel_fb->base;
  8608. err:
  8609. kfree(intel_fb);
  8610. return ERR_PTR(ret);
  8611. }
  8612. static struct drm_framebuffer *
  8613. intel_framebuffer_create(struct drm_device *dev,
  8614. struct drm_mode_fb_cmd2 *mode_cmd,
  8615. struct drm_i915_gem_object *obj)
  8616. {
  8617. struct drm_framebuffer *fb;
  8618. int ret;
  8619. ret = i915_mutex_lock_interruptible(dev);
  8620. if (ret)
  8621. return ERR_PTR(ret);
  8622. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8623. mutex_unlock(&dev->struct_mutex);
  8624. return fb;
  8625. }
  8626. static u32
  8627. intel_framebuffer_pitch_for_width(int width, int bpp)
  8628. {
  8629. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8630. return ALIGN(pitch, 64);
  8631. }
  8632. static u32
  8633. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8634. {
  8635. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8636. return PAGE_ALIGN(pitch * mode->vdisplay);
  8637. }
  8638. static struct drm_framebuffer *
  8639. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8640. struct drm_display_mode *mode,
  8641. int depth, int bpp)
  8642. {
  8643. struct drm_framebuffer *fb;
  8644. struct drm_i915_gem_object *obj;
  8645. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8646. obj = i915_gem_alloc_object(dev,
  8647. intel_framebuffer_size_for_mode(mode, bpp));
  8648. if (obj == NULL)
  8649. return ERR_PTR(-ENOMEM);
  8650. mode_cmd.width = mode->hdisplay;
  8651. mode_cmd.height = mode->vdisplay;
  8652. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8653. bpp);
  8654. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8655. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8656. if (IS_ERR(fb))
  8657. drm_gem_object_unreference_unlocked(&obj->base);
  8658. return fb;
  8659. }
  8660. static struct drm_framebuffer *
  8661. mode_fits_in_fbdev(struct drm_device *dev,
  8662. struct drm_display_mode *mode)
  8663. {
  8664. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8665. struct drm_i915_private *dev_priv = dev->dev_private;
  8666. struct drm_i915_gem_object *obj;
  8667. struct drm_framebuffer *fb;
  8668. if (!dev_priv->fbdev)
  8669. return NULL;
  8670. if (!dev_priv->fbdev->fb)
  8671. return NULL;
  8672. obj = dev_priv->fbdev->fb->obj;
  8673. BUG_ON(!obj);
  8674. fb = &dev_priv->fbdev->fb->base;
  8675. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8676. fb->bits_per_pixel))
  8677. return NULL;
  8678. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8679. return NULL;
  8680. return fb;
  8681. #else
  8682. return NULL;
  8683. #endif
  8684. }
  8685. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8686. struct drm_crtc *crtc,
  8687. struct drm_display_mode *mode,
  8688. struct drm_framebuffer *fb,
  8689. int x, int y)
  8690. {
  8691. struct drm_plane_state *plane_state;
  8692. int hdisplay, vdisplay;
  8693. int ret;
  8694. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8695. if (IS_ERR(plane_state))
  8696. return PTR_ERR(plane_state);
  8697. if (mode)
  8698. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8699. else
  8700. hdisplay = vdisplay = 0;
  8701. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8702. if (ret)
  8703. return ret;
  8704. drm_atomic_set_fb_for_plane(plane_state, fb);
  8705. plane_state->crtc_x = 0;
  8706. plane_state->crtc_y = 0;
  8707. plane_state->crtc_w = hdisplay;
  8708. plane_state->crtc_h = vdisplay;
  8709. plane_state->src_x = x << 16;
  8710. plane_state->src_y = y << 16;
  8711. plane_state->src_w = hdisplay << 16;
  8712. plane_state->src_h = vdisplay << 16;
  8713. return 0;
  8714. }
  8715. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8716. struct drm_display_mode *mode,
  8717. struct intel_load_detect_pipe *old,
  8718. struct drm_modeset_acquire_ctx *ctx)
  8719. {
  8720. struct intel_crtc *intel_crtc;
  8721. struct intel_encoder *intel_encoder =
  8722. intel_attached_encoder(connector);
  8723. struct drm_crtc *possible_crtc;
  8724. struct drm_encoder *encoder = &intel_encoder->base;
  8725. struct drm_crtc *crtc = NULL;
  8726. struct drm_device *dev = encoder->dev;
  8727. struct drm_framebuffer *fb;
  8728. struct drm_mode_config *config = &dev->mode_config;
  8729. struct drm_atomic_state *state = NULL;
  8730. struct drm_connector_state *connector_state;
  8731. struct intel_crtc_state *crtc_state;
  8732. int ret, i = -1;
  8733. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8734. connector->base.id, connector->name,
  8735. encoder->base.id, encoder->name);
  8736. retry:
  8737. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8738. if (ret)
  8739. goto fail;
  8740. /*
  8741. * Algorithm gets a little messy:
  8742. *
  8743. * - if the connector already has an assigned crtc, use it (but make
  8744. * sure it's on first)
  8745. *
  8746. * - try to find the first unused crtc that can drive this connector,
  8747. * and use that if we find one
  8748. */
  8749. /* See if we already have a CRTC for this connector */
  8750. if (encoder->crtc) {
  8751. crtc = encoder->crtc;
  8752. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8753. if (ret)
  8754. goto fail;
  8755. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8756. if (ret)
  8757. goto fail;
  8758. old->dpms_mode = connector->dpms;
  8759. old->load_detect_temp = false;
  8760. /* Make sure the crtc and connector are running */
  8761. if (connector->dpms != DRM_MODE_DPMS_ON)
  8762. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8763. return true;
  8764. }
  8765. /* Find an unused one (if possible) */
  8766. for_each_crtc(dev, possible_crtc) {
  8767. i++;
  8768. if (!(encoder->possible_crtcs & (1 << i)))
  8769. continue;
  8770. if (possible_crtc->state->enable)
  8771. continue;
  8772. crtc = possible_crtc;
  8773. break;
  8774. }
  8775. /*
  8776. * If we didn't find an unused CRTC, don't use any.
  8777. */
  8778. if (!crtc) {
  8779. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8780. goto fail;
  8781. }
  8782. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8783. if (ret)
  8784. goto fail;
  8785. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8786. if (ret)
  8787. goto fail;
  8788. intel_crtc = to_intel_crtc(crtc);
  8789. old->dpms_mode = connector->dpms;
  8790. old->load_detect_temp = true;
  8791. old->release_fb = NULL;
  8792. state = drm_atomic_state_alloc(dev);
  8793. if (!state)
  8794. return false;
  8795. state->acquire_ctx = ctx;
  8796. connector_state = drm_atomic_get_connector_state(state, connector);
  8797. if (IS_ERR(connector_state)) {
  8798. ret = PTR_ERR(connector_state);
  8799. goto fail;
  8800. }
  8801. connector_state->crtc = crtc;
  8802. connector_state->best_encoder = &intel_encoder->base;
  8803. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8804. if (IS_ERR(crtc_state)) {
  8805. ret = PTR_ERR(crtc_state);
  8806. goto fail;
  8807. }
  8808. crtc_state->base.active = crtc_state->base.enable = true;
  8809. if (!mode)
  8810. mode = &load_detect_mode;
  8811. /* We need a framebuffer large enough to accommodate all accesses
  8812. * that the plane may generate whilst we perform load detection.
  8813. * We can not rely on the fbcon either being present (we get called
  8814. * during its initialisation to detect all boot displays, or it may
  8815. * not even exist) or that it is large enough to satisfy the
  8816. * requested mode.
  8817. */
  8818. fb = mode_fits_in_fbdev(dev, mode);
  8819. if (fb == NULL) {
  8820. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8821. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8822. old->release_fb = fb;
  8823. } else
  8824. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8825. if (IS_ERR(fb)) {
  8826. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8827. goto fail;
  8828. }
  8829. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8830. if (ret)
  8831. goto fail;
  8832. drm_mode_copy(&crtc_state->base.mode, mode);
  8833. if (drm_atomic_commit(state)) {
  8834. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8835. if (old->release_fb)
  8836. old->release_fb->funcs->destroy(old->release_fb);
  8837. goto fail;
  8838. }
  8839. crtc->primary->crtc = crtc;
  8840. /* let the connector get through one full cycle before testing */
  8841. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8842. return true;
  8843. fail:
  8844. drm_atomic_state_free(state);
  8845. state = NULL;
  8846. if (ret == -EDEADLK) {
  8847. drm_modeset_backoff(ctx);
  8848. goto retry;
  8849. }
  8850. return false;
  8851. }
  8852. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8853. struct intel_load_detect_pipe *old,
  8854. struct drm_modeset_acquire_ctx *ctx)
  8855. {
  8856. struct drm_device *dev = connector->dev;
  8857. struct intel_encoder *intel_encoder =
  8858. intel_attached_encoder(connector);
  8859. struct drm_encoder *encoder = &intel_encoder->base;
  8860. struct drm_crtc *crtc = encoder->crtc;
  8861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8862. struct drm_atomic_state *state;
  8863. struct drm_connector_state *connector_state;
  8864. struct intel_crtc_state *crtc_state;
  8865. int ret;
  8866. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8867. connector->base.id, connector->name,
  8868. encoder->base.id, encoder->name);
  8869. if (old->load_detect_temp) {
  8870. state = drm_atomic_state_alloc(dev);
  8871. if (!state)
  8872. goto fail;
  8873. state->acquire_ctx = ctx;
  8874. connector_state = drm_atomic_get_connector_state(state, connector);
  8875. if (IS_ERR(connector_state))
  8876. goto fail;
  8877. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8878. if (IS_ERR(crtc_state))
  8879. goto fail;
  8880. connector_state->best_encoder = NULL;
  8881. connector_state->crtc = NULL;
  8882. crtc_state->base.enable = crtc_state->base.active = false;
  8883. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8884. 0, 0);
  8885. if (ret)
  8886. goto fail;
  8887. ret = drm_atomic_commit(state);
  8888. if (ret)
  8889. goto fail;
  8890. if (old->release_fb) {
  8891. drm_framebuffer_unregister_private(old->release_fb);
  8892. drm_framebuffer_unreference(old->release_fb);
  8893. }
  8894. return;
  8895. }
  8896. /* Switch crtc and encoder back off if necessary */
  8897. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8898. connector->funcs->dpms(connector, old->dpms_mode);
  8899. return;
  8900. fail:
  8901. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8902. drm_atomic_state_free(state);
  8903. }
  8904. static int i9xx_pll_refclk(struct drm_device *dev,
  8905. const struct intel_crtc_state *pipe_config)
  8906. {
  8907. struct drm_i915_private *dev_priv = dev->dev_private;
  8908. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8909. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8910. return dev_priv->vbt.lvds_ssc_freq;
  8911. else if (HAS_PCH_SPLIT(dev))
  8912. return 120000;
  8913. else if (!IS_GEN2(dev))
  8914. return 96000;
  8915. else
  8916. return 48000;
  8917. }
  8918. /* Returns the clock of the currently programmed mode of the given pipe. */
  8919. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8920. struct intel_crtc_state *pipe_config)
  8921. {
  8922. struct drm_device *dev = crtc->base.dev;
  8923. struct drm_i915_private *dev_priv = dev->dev_private;
  8924. int pipe = pipe_config->cpu_transcoder;
  8925. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8926. u32 fp;
  8927. intel_clock_t clock;
  8928. int port_clock;
  8929. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8930. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8931. fp = pipe_config->dpll_hw_state.fp0;
  8932. else
  8933. fp = pipe_config->dpll_hw_state.fp1;
  8934. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8935. if (IS_PINEVIEW(dev)) {
  8936. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8937. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8938. } else {
  8939. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8940. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8941. }
  8942. if (!IS_GEN2(dev)) {
  8943. if (IS_PINEVIEW(dev))
  8944. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8945. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8946. else
  8947. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8948. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8949. switch (dpll & DPLL_MODE_MASK) {
  8950. case DPLLB_MODE_DAC_SERIAL:
  8951. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8952. 5 : 10;
  8953. break;
  8954. case DPLLB_MODE_LVDS:
  8955. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8956. 7 : 14;
  8957. break;
  8958. default:
  8959. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8960. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8961. return;
  8962. }
  8963. if (IS_PINEVIEW(dev))
  8964. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8965. else
  8966. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8967. } else {
  8968. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8969. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8970. if (is_lvds) {
  8971. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8972. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8973. if (lvds & LVDS_CLKB_POWER_UP)
  8974. clock.p2 = 7;
  8975. else
  8976. clock.p2 = 14;
  8977. } else {
  8978. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8979. clock.p1 = 2;
  8980. else {
  8981. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8982. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8983. }
  8984. if (dpll & PLL_P2_DIVIDE_BY_4)
  8985. clock.p2 = 4;
  8986. else
  8987. clock.p2 = 2;
  8988. }
  8989. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8990. }
  8991. /*
  8992. * This value includes pixel_multiplier. We will use
  8993. * port_clock to compute adjusted_mode.crtc_clock in the
  8994. * encoder's get_config() function.
  8995. */
  8996. pipe_config->port_clock = port_clock;
  8997. }
  8998. int intel_dotclock_calculate(int link_freq,
  8999. const struct intel_link_m_n *m_n)
  9000. {
  9001. /*
  9002. * The calculation for the data clock is:
  9003. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9004. * But we want to avoid losing precison if possible, so:
  9005. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9006. *
  9007. * and the link clock is simpler:
  9008. * link_clock = (m * link_clock) / n
  9009. */
  9010. if (!m_n->link_n)
  9011. return 0;
  9012. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9013. }
  9014. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9015. struct intel_crtc_state *pipe_config)
  9016. {
  9017. struct drm_device *dev = crtc->base.dev;
  9018. /* read out port_clock from the DPLL */
  9019. i9xx_crtc_clock_get(crtc, pipe_config);
  9020. /*
  9021. * This value does not include pixel_multiplier.
  9022. * We will check that port_clock and adjusted_mode.crtc_clock
  9023. * agree once we know their relationship in the encoder's
  9024. * get_config() function.
  9025. */
  9026. pipe_config->base.adjusted_mode.crtc_clock =
  9027. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  9028. &pipe_config->fdi_m_n);
  9029. }
  9030. /** Returns the currently programmed mode of the given pipe. */
  9031. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9032. struct drm_crtc *crtc)
  9033. {
  9034. struct drm_i915_private *dev_priv = dev->dev_private;
  9035. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9036. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9037. struct drm_display_mode *mode;
  9038. struct intel_crtc_state *pipe_config;
  9039. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9040. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9041. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9042. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9043. enum pipe pipe = intel_crtc->pipe;
  9044. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9045. if (!mode)
  9046. return NULL;
  9047. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9048. if (!pipe_config) {
  9049. kfree(mode);
  9050. return NULL;
  9051. }
  9052. /*
  9053. * Construct a pipe_config sufficient for getting the clock info
  9054. * back out of crtc_clock_get.
  9055. *
  9056. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9057. * to use a real value here instead.
  9058. */
  9059. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9060. pipe_config->pixel_multiplier = 1;
  9061. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9062. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9063. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9064. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9065. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9066. mode->hdisplay = (htot & 0xffff) + 1;
  9067. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9068. mode->hsync_start = (hsync & 0xffff) + 1;
  9069. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9070. mode->vdisplay = (vtot & 0xffff) + 1;
  9071. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9072. mode->vsync_start = (vsync & 0xffff) + 1;
  9073. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9074. drm_mode_set_name(mode);
  9075. kfree(pipe_config);
  9076. return mode;
  9077. }
  9078. void intel_mark_busy(struct drm_device *dev)
  9079. {
  9080. struct drm_i915_private *dev_priv = dev->dev_private;
  9081. if (dev_priv->mm.busy)
  9082. return;
  9083. intel_runtime_pm_get(dev_priv);
  9084. i915_update_gfx_val(dev_priv);
  9085. if (INTEL_INFO(dev)->gen >= 6)
  9086. gen6_rps_busy(dev_priv);
  9087. dev_priv->mm.busy = true;
  9088. }
  9089. void intel_mark_idle(struct drm_device *dev)
  9090. {
  9091. struct drm_i915_private *dev_priv = dev->dev_private;
  9092. if (!dev_priv->mm.busy)
  9093. return;
  9094. dev_priv->mm.busy = false;
  9095. if (INTEL_INFO(dev)->gen >= 6)
  9096. gen6_rps_idle(dev->dev_private);
  9097. intel_runtime_pm_put(dev_priv);
  9098. }
  9099. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9100. {
  9101. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9102. struct drm_device *dev = crtc->dev;
  9103. struct intel_unpin_work *work;
  9104. spin_lock_irq(&dev->event_lock);
  9105. work = intel_crtc->unpin_work;
  9106. intel_crtc->unpin_work = NULL;
  9107. spin_unlock_irq(&dev->event_lock);
  9108. if (work) {
  9109. cancel_work_sync(&work->work);
  9110. kfree(work);
  9111. }
  9112. drm_crtc_cleanup(crtc);
  9113. kfree(intel_crtc);
  9114. }
  9115. static void intel_unpin_work_fn(struct work_struct *__work)
  9116. {
  9117. struct intel_unpin_work *work =
  9118. container_of(__work, struct intel_unpin_work, work);
  9119. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9120. struct drm_device *dev = crtc->base.dev;
  9121. struct drm_plane *primary = crtc->base.primary;
  9122. mutex_lock(&dev->struct_mutex);
  9123. intel_unpin_fb_obj(work->old_fb, primary->state);
  9124. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9125. if (work->flip_queued_req)
  9126. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9127. mutex_unlock(&dev->struct_mutex);
  9128. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9129. intel_fbc_post_update(crtc);
  9130. drm_framebuffer_unreference(work->old_fb);
  9131. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9132. atomic_dec(&crtc->unpin_work_count);
  9133. kfree(work);
  9134. }
  9135. static void do_intel_finish_page_flip(struct drm_device *dev,
  9136. struct drm_crtc *crtc)
  9137. {
  9138. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9139. struct intel_unpin_work *work;
  9140. unsigned long flags;
  9141. /* Ignore early vblank irqs */
  9142. if (intel_crtc == NULL)
  9143. return;
  9144. /*
  9145. * This is called both by irq handlers and the reset code (to complete
  9146. * lost pageflips) so needs the full irqsave spinlocks.
  9147. */
  9148. spin_lock_irqsave(&dev->event_lock, flags);
  9149. work = intel_crtc->unpin_work;
  9150. /* Ensure we don't miss a work->pending update ... */
  9151. smp_rmb();
  9152. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9153. spin_unlock_irqrestore(&dev->event_lock, flags);
  9154. return;
  9155. }
  9156. page_flip_completed(intel_crtc);
  9157. spin_unlock_irqrestore(&dev->event_lock, flags);
  9158. }
  9159. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9160. {
  9161. struct drm_i915_private *dev_priv = dev->dev_private;
  9162. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9163. do_intel_finish_page_flip(dev, crtc);
  9164. }
  9165. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9166. {
  9167. struct drm_i915_private *dev_priv = dev->dev_private;
  9168. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9169. do_intel_finish_page_flip(dev, crtc);
  9170. }
  9171. /* Is 'a' after or equal to 'b'? */
  9172. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9173. {
  9174. return !((a - b) & 0x80000000);
  9175. }
  9176. static bool page_flip_finished(struct intel_crtc *crtc)
  9177. {
  9178. struct drm_device *dev = crtc->base.dev;
  9179. struct drm_i915_private *dev_priv = dev->dev_private;
  9180. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9181. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9182. return true;
  9183. /*
  9184. * The relevant registers doen't exist on pre-ctg.
  9185. * As the flip done interrupt doesn't trigger for mmio
  9186. * flips on gmch platforms, a flip count check isn't
  9187. * really needed there. But since ctg has the registers,
  9188. * include it in the check anyway.
  9189. */
  9190. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9191. return true;
  9192. /*
  9193. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9194. * used the same base address. In that case the mmio flip might
  9195. * have completed, but the CS hasn't even executed the flip yet.
  9196. *
  9197. * A flip count check isn't enough as the CS might have updated
  9198. * the base address just after start of vblank, but before we
  9199. * managed to process the interrupt. This means we'd complete the
  9200. * CS flip too soon.
  9201. *
  9202. * Combining both checks should get us a good enough result. It may
  9203. * still happen that the CS flip has been executed, but has not
  9204. * yet actually completed. But in case the base address is the same
  9205. * anyway, we don't really care.
  9206. */
  9207. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9208. crtc->unpin_work->gtt_offset &&
  9209. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9210. crtc->unpin_work->flip_count);
  9211. }
  9212. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9213. {
  9214. struct drm_i915_private *dev_priv = dev->dev_private;
  9215. struct intel_crtc *intel_crtc =
  9216. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9217. unsigned long flags;
  9218. /*
  9219. * This is called both by irq handlers and the reset code (to complete
  9220. * lost pageflips) so needs the full irqsave spinlocks.
  9221. *
  9222. * NB: An MMIO update of the plane base pointer will also
  9223. * generate a page-flip completion irq, i.e. every modeset
  9224. * is also accompanied by a spurious intel_prepare_page_flip().
  9225. */
  9226. spin_lock_irqsave(&dev->event_lock, flags);
  9227. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9228. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9229. spin_unlock_irqrestore(&dev->event_lock, flags);
  9230. }
  9231. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9232. {
  9233. /* Ensure that the work item is consistent when activating it ... */
  9234. smp_wmb();
  9235. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9236. /* and that it is marked active as soon as the irq could fire. */
  9237. smp_wmb();
  9238. }
  9239. static int intel_gen2_queue_flip(struct drm_device *dev,
  9240. struct drm_crtc *crtc,
  9241. struct drm_framebuffer *fb,
  9242. struct drm_i915_gem_object *obj,
  9243. struct drm_i915_gem_request *req,
  9244. uint32_t flags)
  9245. {
  9246. struct intel_engine_cs *ring = req->ring;
  9247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9248. u32 flip_mask;
  9249. int ret;
  9250. ret = intel_ring_begin(req, 6);
  9251. if (ret)
  9252. return ret;
  9253. /* Can't queue multiple flips, so wait for the previous
  9254. * one to finish before executing the next.
  9255. */
  9256. if (intel_crtc->plane)
  9257. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9258. else
  9259. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9260. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9261. intel_ring_emit(ring, MI_NOOP);
  9262. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9263. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9264. intel_ring_emit(ring, fb->pitches[0]);
  9265. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9266. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9267. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9268. return 0;
  9269. }
  9270. static int intel_gen3_queue_flip(struct drm_device *dev,
  9271. struct drm_crtc *crtc,
  9272. struct drm_framebuffer *fb,
  9273. struct drm_i915_gem_object *obj,
  9274. struct drm_i915_gem_request *req,
  9275. uint32_t flags)
  9276. {
  9277. struct intel_engine_cs *ring = req->ring;
  9278. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9279. u32 flip_mask;
  9280. int ret;
  9281. ret = intel_ring_begin(req, 6);
  9282. if (ret)
  9283. return ret;
  9284. if (intel_crtc->plane)
  9285. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9286. else
  9287. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9288. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9289. intel_ring_emit(ring, MI_NOOP);
  9290. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9291. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9292. intel_ring_emit(ring, fb->pitches[0]);
  9293. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9294. intel_ring_emit(ring, MI_NOOP);
  9295. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9296. return 0;
  9297. }
  9298. static int intel_gen4_queue_flip(struct drm_device *dev,
  9299. struct drm_crtc *crtc,
  9300. struct drm_framebuffer *fb,
  9301. struct drm_i915_gem_object *obj,
  9302. struct drm_i915_gem_request *req,
  9303. uint32_t flags)
  9304. {
  9305. struct intel_engine_cs *ring = req->ring;
  9306. struct drm_i915_private *dev_priv = dev->dev_private;
  9307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9308. uint32_t pf, pipesrc;
  9309. int ret;
  9310. ret = intel_ring_begin(req, 4);
  9311. if (ret)
  9312. return ret;
  9313. /* i965+ uses the linear or tiled offsets from the
  9314. * Display Registers (which do not change across a page-flip)
  9315. * so we need only reprogram the base address.
  9316. */
  9317. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9318. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9319. intel_ring_emit(ring, fb->pitches[0]);
  9320. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9321. obj->tiling_mode);
  9322. /* XXX Enabling the panel-fitter across page-flip is so far
  9323. * untested on non-native modes, so ignore it for now.
  9324. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9325. */
  9326. pf = 0;
  9327. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9328. intel_ring_emit(ring, pf | pipesrc);
  9329. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9330. return 0;
  9331. }
  9332. static int intel_gen6_queue_flip(struct drm_device *dev,
  9333. struct drm_crtc *crtc,
  9334. struct drm_framebuffer *fb,
  9335. struct drm_i915_gem_object *obj,
  9336. struct drm_i915_gem_request *req,
  9337. uint32_t flags)
  9338. {
  9339. struct intel_engine_cs *ring = req->ring;
  9340. struct drm_i915_private *dev_priv = dev->dev_private;
  9341. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9342. uint32_t pf, pipesrc;
  9343. int ret;
  9344. ret = intel_ring_begin(req, 4);
  9345. if (ret)
  9346. return ret;
  9347. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9348. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9349. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9350. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9351. /* Contrary to the suggestions in the documentation,
  9352. * "Enable Panel Fitter" does not seem to be required when page
  9353. * flipping with a non-native mode, and worse causes a normal
  9354. * modeset to fail.
  9355. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9356. */
  9357. pf = 0;
  9358. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9359. intel_ring_emit(ring, pf | pipesrc);
  9360. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9361. return 0;
  9362. }
  9363. static int intel_gen7_queue_flip(struct drm_device *dev,
  9364. struct drm_crtc *crtc,
  9365. struct drm_framebuffer *fb,
  9366. struct drm_i915_gem_object *obj,
  9367. struct drm_i915_gem_request *req,
  9368. uint32_t flags)
  9369. {
  9370. struct intel_engine_cs *ring = req->ring;
  9371. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9372. uint32_t plane_bit = 0;
  9373. int len, ret;
  9374. switch (intel_crtc->plane) {
  9375. case PLANE_A:
  9376. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9377. break;
  9378. case PLANE_B:
  9379. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9380. break;
  9381. case PLANE_C:
  9382. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9383. break;
  9384. default:
  9385. WARN_ONCE(1, "unknown plane in flip command\n");
  9386. return -ENODEV;
  9387. }
  9388. len = 4;
  9389. if (ring->id == RCS) {
  9390. len += 6;
  9391. /*
  9392. * On Gen 8, SRM is now taking an extra dword to accommodate
  9393. * 48bits addresses, and we need a NOOP for the batch size to
  9394. * stay even.
  9395. */
  9396. if (IS_GEN8(dev))
  9397. len += 2;
  9398. }
  9399. /*
  9400. * BSpec MI_DISPLAY_FLIP for IVB:
  9401. * "The full packet must be contained within the same cache line."
  9402. *
  9403. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9404. * cacheline, if we ever start emitting more commands before
  9405. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9406. * then do the cacheline alignment, and finally emit the
  9407. * MI_DISPLAY_FLIP.
  9408. */
  9409. ret = intel_ring_cacheline_align(req);
  9410. if (ret)
  9411. return ret;
  9412. ret = intel_ring_begin(req, len);
  9413. if (ret)
  9414. return ret;
  9415. /* Unmask the flip-done completion message. Note that the bspec says that
  9416. * we should do this for both the BCS and RCS, and that we must not unmask
  9417. * more than one flip event at any time (or ensure that one flip message
  9418. * can be sent by waiting for flip-done prior to queueing new flips).
  9419. * Experimentation says that BCS works despite DERRMR masking all
  9420. * flip-done completion events and that unmasking all planes at once
  9421. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9422. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9423. */
  9424. if (ring->id == RCS) {
  9425. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9426. intel_ring_emit_reg(ring, DERRMR);
  9427. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9428. DERRMR_PIPEB_PRI_FLIP_DONE |
  9429. DERRMR_PIPEC_PRI_FLIP_DONE));
  9430. if (IS_GEN8(dev))
  9431. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9432. MI_SRM_LRM_GLOBAL_GTT);
  9433. else
  9434. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9435. MI_SRM_LRM_GLOBAL_GTT);
  9436. intel_ring_emit_reg(ring, DERRMR);
  9437. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9438. if (IS_GEN8(dev)) {
  9439. intel_ring_emit(ring, 0);
  9440. intel_ring_emit(ring, MI_NOOP);
  9441. }
  9442. }
  9443. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9444. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9445. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9446. intel_ring_emit(ring, (MI_NOOP));
  9447. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9448. return 0;
  9449. }
  9450. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9451. struct drm_i915_gem_object *obj)
  9452. {
  9453. /*
  9454. * This is not being used for older platforms, because
  9455. * non-availability of flip done interrupt forces us to use
  9456. * CS flips. Older platforms derive flip done using some clever
  9457. * tricks involving the flip_pending status bits and vblank irqs.
  9458. * So using MMIO flips there would disrupt this mechanism.
  9459. */
  9460. if (ring == NULL)
  9461. return true;
  9462. if (INTEL_INFO(ring->dev)->gen < 5)
  9463. return false;
  9464. if (i915.use_mmio_flip < 0)
  9465. return false;
  9466. else if (i915.use_mmio_flip > 0)
  9467. return true;
  9468. else if (i915.enable_execlists)
  9469. return true;
  9470. else if (obj->base.dma_buf &&
  9471. !reservation_object_test_signaled_rcu(obj->base.dma_buf->resv,
  9472. false))
  9473. return true;
  9474. else
  9475. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9476. }
  9477. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9478. unsigned int rotation,
  9479. struct intel_unpin_work *work)
  9480. {
  9481. struct drm_device *dev = intel_crtc->base.dev;
  9482. struct drm_i915_private *dev_priv = dev->dev_private;
  9483. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9484. const enum pipe pipe = intel_crtc->pipe;
  9485. u32 ctl, stride, tile_height;
  9486. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9487. ctl &= ~PLANE_CTL_TILED_MASK;
  9488. switch (fb->modifier[0]) {
  9489. case DRM_FORMAT_MOD_NONE:
  9490. break;
  9491. case I915_FORMAT_MOD_X_TILED:
  9492. ctl |= PLANE_CTL_TILED_X;
  9493. break;
  9494. case I915_FORMAT_MOD_Y_TILED:
  9495. ctl |= PLANE_CTL_TILED_Y;
  9496. break;
  9497. case I915_FORMAT_MOD_Yf_TILED:
  9498. ctl |= PLANE_CTL_TILED_YF;
  9499. break;
  9500. default:
  9501. MISSING_CASE(fb->modifier[0]);
  9502. }
  9503. /*
  9504. * The stride is either expressed as a multiple of 64 bytes chunks for
  9505. * linear buffers or in number of tiles for tiled buffers.
  9506. */
  9507. if (intel_rotation_90_or_270(rotation)) {
  9508. /* stride = Surface height in tiles */
  9509. tile_height = intel_tile_height(dev_priv, fb->modifier[0], 0);
  9510. stride = DIV_ROUND_UP(fb->height, tile_height);
  9511. } else {
  9512. stride = fb->pitches[0] /
  9513. intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  9514. fb->pixel_format);
  9515. }
  9516. /*
  9517. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9518. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9519. */
  9520. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9521. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9522. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9523. POSTING_READ(PLANE_SURF(pipe, 0));
  9524. }
  9525. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9526. struct intel_unpin_work *work)
  9527. {
  9528. struct drm_device *dev = intel_crtc->base.dev;
  9529. struct drm_i915_private *dev_priv = dev->dev_private;
  9530. struct intel_framebuffer *intel_fb =
  9531. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9532. struct drm_i915_gem_object *obj = intel_fb->obj;
  9533. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  9534. u32 dspcntr;
  9535. dspcntr = I915_READ(reg);
  9536. if (obj->tiling_mode != I915_TILING_NONE)
  9537. dspcntr |= DISPPLANE_TILED;
  9538. else
  9539. dspcntr &= ~DISPPLANE_TILED;
  9540. I915_WRITE(reg, dspcntr);
  9541. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9542. POSTING_READ(DSPSURF(intel_crtc->plane));
  9543. }
  9544. /*
  9545. * XXX: This is the temporary way to update the plane registers until we get
  9546. * around to using the usual plane update functions for MMIO flips
  9547. */
  9548. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9549. {
  9550. struct intel_crtc *crtc = mmio_flip->crtc;
  9551. struct intel_unpin_work *work;
  9552. spin_lock_irq(&crtc->base.dev->event_lock);
  9553. work = crtc->unpin_work;
  9554. spin_unlock_irq(&crtc->base.dev->event_lock);
  9555. if (work == NULL)
  9556. return;
  9557. intel_mark_page_flip_active(work);
  9558. intel_pipe_update_start(crtc);
  9559. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9560. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9561. else
  9562. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9563. ilk_do_mmio_flip(crtc, work);
  9564. intel_pipe_update_end(crtc);
  9565. }
  9566. static void intel_mmio_flip_work_func(struct work_struct *work)
  9567. {
  9568. struct intel_mmio_flip *mmio_flip =
  9569. container_of(work, struct intel_mmio_flip, work);
  9570. struct intel_framebuffer *intel_fb =
  9571. to_intel_framebuffer(mmio_flip->crtc->base.primary->fb);
  9572. struct drm_i915_gem_object *obj = intel_fb->obj;
  9573. if (mmio_flip->req) {
  9574. WARN_ON(__i915_wait_request(mmio_flip->req,
  9575. mmio_flip->crtc->reset_counter,
  9576. false, NULL,
  9577. &mmio_flip->i915->rps.mmioflips));
  9578. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9579. }
  9580. /* For framebuffer backed by dmabuf, wait for fence */
  9581. if (obj->base.dma_buf)
  9582. WARN_ON(reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  9583. false, false,
  9584. MAX_SCHEDULE_TIMEOUT) < 0);
  9585. intel_do_mmio_flip(mmio_flip);
  9586. kfree(mmio_flip);
  9587. }
  9588. static int intel_queue_mmio_flip(struct drm_device *dev,
  9589. struct drm_crtc *crtc,
  9590. struct drm_i915_gem_object *obj)
  9591. {
  9592. struct intel_mmio_flip *mmio_flip;
  9593. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9594. if (mmio_flip == NULL)
  9595. return -ENOMEM;
  9596. mmio_flip->i915 = to_i915(dev);
  9597. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9598. mmio_flip->crtc = to_intel_crtc(crtc);
  9599. mmio_flip->rotation = crtc->primary->state->rotation;
  9600. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9601. schedule_work(&mmio_flip->work);
  9602. return 0;
  9603. }
  9604. static int intel_default_queue_flip(struct drm_device *dev,
  9605. struct drm_crtc *crtc,
  9606. struct drm_framebuffer *fb,
  9607. struct drm_i915_gem_object *obj,
  9608. struct drm_i915_gem_request *req,
  9609. uint32_t flags)
  9610. {
  9611. return -ENODEV;
  9612. }
  9613. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9614. struct drm_crtc *crtc)
  9615. {
  9616. struct drm_i915_private *dev_priv = dev->dev_private;
  9617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9618. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9619. u32 addr;
  9620. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9621. return true;
  9622. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9623. return false;
  9624. if (!work->enable_stall_check)
  9625. return false;
  9626. if (work->flip_ready_vblank == 0) {
  9627. if (work->flip_queued_req &&
  9628. !i915_gem_request_completed(work->flip_queued_req, true))
  9629. return false;
  9630. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9631. }
  9632. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9633. return false;
  9634. /* Potential stall - if we see that the flip has happened,
  9635. * assume a missed interrupt. */
  9636. if (INTEL_INFO(dev)->gen >= 4)
  9637. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9638. else
  9639. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9640. /* There is a potential issue here with a false positive after a flip
  9641. * to the same address. We could address this by checking for a
  9642. * non-incrementing frame counter.
  9643. */
  9644. return addr == work->gtt_offset;
  9645. }
  9646. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9647. {
  9648. struct drm_i915_private *dev_priv = dev->dev_private;
  9649. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9650. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9651. struct intel_unpin_work *work;
  9652. WARN_ON(!in_interrupt());
  9653. if (crtc == NULL)
  9654. return;
  9655. spin_lock(&dev->event_lock);
  9656. work = intel_crtc->unpin_work;
  9657. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9658. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9659. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9660. page_flip_completed(intel_crtc);
  9661. work = NULL;
  9662. }
  9663. if (work != NULL &&
  9664. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9665. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9666. spin_unlock(&dev->event_lock);
  9667. }
  9668. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9669. struct drm_framebuffer *fb,
  9670. struct drm_pending_vblank_event *event,
  9671. uint32_t page_flip_flags)
  9672. {
  9673. struct drm_device *dev = crtc->dev;
  9674. struct drm_i915_private *dev_priv = dev->dev_private;
  9675. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9676. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9677. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9678. struct drm_plane *primary = crtc->primary;
  9679. enum pipe pipe = intel_crtc->pipe;
  9680. struct intel_unpin_work *work;
  9681. struct intel_engine_cs *ring;
  9682. bool mmio_flip;
  9683. struct drm_i915_gem_request *request = NULL;
  9684. int ret;
  9685. /*
  9686. * drm_mode_page_flip_ioctl() should already catch this, but double
  9687. * check to be safe. In the future we may enable pageflipping from
  9688. * a disabled primary plane.
  9689. */
  9690. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9691. return -EBUSY;
  9692. /* Can't change pixel format via MI display flips. */
  9693. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9694. return -EINVAL;
  9695. /*
  9696. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9697. * Note that pitch changes could also affect these register.
  9698. */
  9699. if (INTEL_INFO(dev)->gen > 3 &&
  9700. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9701. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9702. return -EINVAL;
  9703. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9704. goto out_hang;
  9705. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9706. if (work == NULL)
  9707. return -ENOMEM;
  9708. work->event = event;
  9709. work->crtc = crtc;
  9710. work->old_fb = old_fb;
  9711. INIT_WORK(&work->work, intel_unpin_work_fn);
  9712. ret = drm_crtc_vblank_get(crtc);
  9713. if (ret)
  9714. goto free_work;
  9715. /* We borrow the event spin lock for protecting unpin_work */
  9716. spin_lock_irq(&dev->event_lock);
  9717. if (intel_crtc->unpin_work) {
  9718. /* Before declaring the flip queue wedged, check if
  9719. * the hardware completed the operation behind our backs.
  9720. */
  9721. if (__intel_pageflip_stall_check(dev, crtc)) {
  9722. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9723. page_flip_completed(intel_crtc);
  9724. } else {
  9725. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9726. spin_unlock_irq(&dev->event_lock);
  9727. drm_crtc_vblank_put(crtc);
  9728. kfree(work);
  9729. return -EBUSY;
  9730. }
  9731. }
  9732. intel_crtc->unpin_work = work;
  9733. spin_unlock_irq(&dev->event_lock);
  9734. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9735. flush_workqueue(dev_priv->wq);
  9736. /* Reference the objects for the scheduled work. */
  9737. drm_framebuffer_reference(work->old_fb);
  9738. drm_gem_object_reference(&obj->base);
  9739. crtc->primary->fb = fb;
  9740. update_state_fb(crtc->primary);
  9741. work->pending_flip_obj = obj;
  9742. ret = i915_mutex_lock_interruptible(dev);
  9743. if (ret)
  9744. goto cleanup;
  9745. atomic_inc(&intel_crtc->unpin_work_count);
  9746. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9747. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9748. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9749. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  9750. ring = &dev_priv->ring[BCS];
  9751. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9752. /* vlv: DISPLAY_FLIP fails to change tiling */
  9753. ring = NULL;
  9754. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9755. ring = &dev_priv->ring[BCS];
  9756. } else if (INTEL_INFO(dev)->gen >= 7) {
  9757. ring = i915_gem_request_get_ring(obj->last_write_req);
  9758. if (ring == NULL || ring->id != RCS)
  9759. ring = &dev_priv->ring[BCS];
  9760. } else {
  9761. ring = &dev_priv->ring[RCS];
  9762. }
  9763. mmio_flip = use_mmio_flip(ring, obj);
  9764. /* When using CS flips, we want to emit semaphores between rings.
  9765. * However, when using mmio flips we will create a task to do the
  9766. * synchronisation, so all we want here is to pin the framebuffer
  9767. * into the display plane and skip any waits.
  9768. */
  9769. if (!mmio_flip) {
  9770. ret = i915_gem_object_sync(obj, ring, &request);
  9771. if (ret)
  9772. goto cleanup_pending;
  9773. }
  9774. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9775. crtc->primary->state);
  9776. if (ret)
  9777. goto cleanup_pending;
  9778. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9779. obj, 0);
  9780. work->gtt_offset += intel_crtc->dspaddr_offset;
  9781. if (mmio_flip) {
  9782. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9783. if (ret)
  9784. goto cleanup_unpin;
  9785. i915_gem_request_assign(&work->flip_queued_req,
  9786. obj->last_write_req);
  9787. } else {
  9788. if (!request) {
  9789. request = i915_gem_request_alloc(ring, NULL);
  9790. if (IS_ERR(request)) {
  9791. ret = PTR_ERR(request);
  9792. goto cleanup_unpin;
  9793. }
  9794. }
  9795. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9796. page_flip_flags);
  9797. if (ret)
  9798. goto cleanup_unpin;
  9799. i915_gem_request_assign(&work->flip_queued_req, request);
  9800. }
  9801. if (request)
  9802. i915_add_request_no_flush(request);
  9803. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9804. work->enable_stall_check = true;
  9805. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9806. to_intel_plane(primary)->frontbuffer_bit);
  9807. mutex_unlock(&dev->struct_mutex);
  9808. intel_fbc_pre_update(intel_crtc);
  9809. intel_frontbuffer_flip_prepare(dev,
  9810. to_intel_plane(primary)->frontbuffer_bit);
  9811. trace_i915_flip_request(intel_crtc->plane, obj);
  9812. return 0;
  9813. cleanup_unpin:
  9814. intel_unpin_fb_obj(fb, crtc->primary->state);
  9815. cleanup_pending:
  9816. if (!IS_ERR_OR_NULL(request))
  9817. i915_gem_request_cancel(request);
  9818. atomic_dec(&intel_crtc->unpin_work_count);
  9819. mutex_unlock(&dev->struct_mutex);
  9820. cleanup:
  9821. crtc->primary->fb = old_fb;
  9822. update_state_fb(crtc->primary);
  9823. drm_gem_object_unreference_unlocked(&obj->base);
  9824. drm_framebuffer_unreference(work->old_fb);
  9825. spin_lock_irq(&dev->event_lock);
  9826. intel_crtc->unpin_work = NULL;
  9827. spin_unlock_irq(&dev->event_lock);
  9828. drm_crtc_vblank_put(crtc);
  9829. free_work:
  9830. kfree(work);
  9831. if (ret == -EIO) {
  9832. struct drm_atomic_state *state;
  9833. struct drm_plane_state *plane_state;
  9834. out_hang:
  9835. state = drm_atomic_state_alloc(dev);
  9836. if (!state)
  9837. return -ENOMEM;
  9838. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9839. retry:
  9840. plane_state = drm_atomic_get_plane_state(state, primary);
  9841. ret = PTR_ERR_OR_ZERO(plane_state);
  9842. if (!ret) {
  9843. drm_atomic_set_fb_for_plane(plane_state, fb);
  9844. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9845. if (!ret)
  9846. ret = drm_atomic_commit(state);
  9847. }
  9848. if (ret == -EDEADLK) {
  9849. drm_modeset_backoff(state->acquire_ctx);
  9850. drm_atomic_state_clear(state);
  9851. goto retry;
  9852. }
  9853. if (ret)
  9854. drm_atomic_state_free(state);
  9855. if (ret == 0 && event) {
  9856. spin_lock_irq(&dev->event_lock);
  9857. drm_send_vblank_event(dev, pipe, event);
  9858. spin_unlock_irq(&dev->event_lock);
  9859. }
  9860. }
  9861. return ret;
  9862. }
  9863. /**
  9864. * intel_wm_need_update - Check whether watermarks need updating
  9865. * @plane: drm plane
  9866. * @state: new plane state
  9867. *
  9868. * Check current plane state versus the new one to determine whether
  9869. * watermarks need to be recalculated.
  9870. *
  9871. * Returns true or false.
  9872. */
  9873. static bool intel_wm_need_update(struct drm_plane *plane,
  9874. struct drm_plane_state *state)
  9875. {
  9876. struct intel_plane_state *new = to_intel_plane_state(state);
  9877. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9878. /* Update watermarks on tiling or size changes. */
  9879. if (new->visible != cur->visible)
  9880. return true;
  9881. if (!cur->base.fb || !new->base.fb)
  9882. return false;
  9883. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9884. cur->base.rotation != new->base.rotation ||
  9885. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9886. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9887. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9888. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9889. return true;
  9890. return false;
  9891. }
  9892. static bool needs_scaling(struct intel_plane_state *state)
  9893. {
  9894. int src_w = drm_rect_width(&state->src) >> 16;
  9895. int src_h = drm_rect_height(&state->src) >> 16;
  9896. int dst_w = drm_rect_width(&state->dst);
  9897. int dst_h = drm_rect_height(&state->dst);
  9898. return (src_w != dst_w || src_h != dst_h);
  9899. }
  9900. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9901. struct drm_plane_state *plane_state)
  9902. {
  9903. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9904. struct drm_crtc *crtc = crtc_state->crtc;
  9905. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9906. struct drm_plane *plane = plane_state->plane;
  9907. struct drm_device *dev = crtc->dev;
  9908. struct intel_plane_state *old_plane_state =
  9909. to_intel_plane_state(plane->state);
  9910. int idx = intel_crtc->base.base.id, ret;
  9911. int i = drm_plane_index(plane);
  9912. bool mode_changed = needs_modeset(crtc_state);
  9913. bool was_crtc_enabled = crtc->state->active;
  9914. bool is_crtc_enabled = crtc_state->active;
  9915. bool turn_off, turn_on, visible, was_visible;
  9916. struct drm_framebuffer *fb = plane_state->fb;
  9917. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9918. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9919. ret = skl_update_scaler_plane(
  9920. to_intel_crtc_state(crtc_state),
  9921. to_intel_plane_state(plane_state));
  9922. if (ret)
  9923. return ret;
  9924. }
  9925. was_visible = old_plane_state->visible;
  9926. visible = to_intel_plane_state(plane_state)->visible;
  9927. if (!was_crtc_enabled && WARN_ON(was_visible))
  9928. was_visible = false;
  9929. /*
  9930. * Visibility is calculated as if the crtc was on, but
  9931. * after scaler setup everything depends on it being off
  9932. * when the crtc isn't active.
  9933. */
  9934. if (!is_crtc_enabled)
  9935. to_intel_plane_state(plane_state)->visible = visible = false;
  9936. if (!was_visible && !visible)
  9937. return 0;
  9938. turn_off = was_visible && (!visible || mode_changed);
  9939. turn_on = visible && (!was_visible || mode_changed);
  9940. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9941. plane->base.id, fb ? fb->base.id : -1);
  9942. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9943. plane->base.id, was_visible, visible,
  9944. turn_off, turn_on, mode_changed);
  9945. if (turn_on || turn_off) {
  9946. pipe_config->wm_changed = true;
  9947. /* must disable cxsr around plane enable/disable */
  9948. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9949. if (is_crtc_enabled)
  9950. intel_crtc->atomic.wait_vblank = true;
  9951. pipe_config->disable_cxsr = true;
  9952. }
  9953. } else if (intel_wm_need_update(plane, plane_state)) {
  9954. pipe_config->wm_changed = true;
  9955. }
  9956. if (visible || was_visible)
  9957. intel_crtc->atomic.fb_bits |=
  9958. to_intel_plane(plane)->frontbuffer_bit;
  9959. switch (plane->type) {
  9960. case DRM_PLANE_TYPE_PRIMARY:
  9961. intel_crtc->atomic.pre_disable_primary = turn_off;
  9962. intel_crtc->atomic.post_enable_primary = turn_on;
  9963. intel_crtc->atomic.update_fbc = true;
  9964. if (turn_off) {
  9965. /*
  9966. * FIXME: Actually if we will still have any other
  9967. * plane enabled on the pipe we could let IPS enabled
  9968. * still, but for now lets consider that when we make
  9969. * primary invisible by setting DSPCNTR to 0 on
  9970. * update_primary_plane function IPS needs to be
  9971. * disable.
  9972. */
  9973. intel_crtc->atomic.disable_ips = true;
  9974. }
  9975. /*
  9976. * BDW signals flip done immediately if the plane
  9977. * is disabled, even if the plane enable is already
  9978. * armed to occur at the next vblank :(
  9979. */
  9980. if (turn_on && IS_BROADWELL(dev))
  9981. intel_crtc->atomic.wait_vblank = true;
  9982. break;
  9983. case DRM_PLANE_TYPE_CURSOR:
  9984. break;
  9985. case DRM_PLANE_TYPE_OVERLAY:
  9986. /*
  9987. * WaCxSRDisabledForSpriteScaling:ivb
  9988. *
  9989. * cstate->update_wm was already set above, so this flag will
  9990. * take effect when we commit and program watermarks.
  9991. */
  9992. if (IS_IVYBRIDGE(dev) &&
  9993. needs_scaling(to_intel_plane_state(plane_state)) &&
  9994. !needs_scaling(old_plane_state)) {
  9995. to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
  9996. } else if (turn_off && !mode_changed) {
  9997. intel_crtc->atomic.wait_vblank = true;
  9998. intel_crtc->atomic.update_sprite_watermarks |=
  9999. 1 << i;
  10000. }
  10001. break;
  10002. }
  10003. return 0;
  10004. }
  10005. static bool encoders_cloneable(const struct intel_encoder *a,
  10006. const struct intel_encoder *b)
  10007. {
  10008. /* masks could be asymmetric, so check both ways */
  10009. return a == b || (a->cloneable & (1 << b->type) &&
  10010. b->cloneable & (1 << a->type));
  10011. }
  10012. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10013. struct intel_crtc *crtc,
  10014. struct intel_encoder *encoder)
  10015. {
  10016. struct intel_encoder *source_encoder;
  10017. struct drm_connector *connector;
  10018. struct drm_connector_state *connector_state;
  10019. int i;
  10020. for_each_connector_in_state(state, connector, connector_state, i) {
  10021. if (connector_state->crtc != &crtc->base)
  10022. continue;
  10023. source_encoder =
  10024. to_intel_encoder(connector_state->best_encoder);
  10025. if (!encoders_cloneable(encoder, source_encoder))
  10026. return false;
  10027. }
  10028. return true;
  10029. }
  10030. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10031. struct intel_crtc *crtc)
  10032. {
  10033. struct intel_encoder *encoder;
  10034. struct drm_connector *connector;
  10035. struct drm_connector_state *connector_state;
  10036. int i;
  10037. for_each_connector_in_state(state, connector, connector_state, i) {
  10038. if (connector_state->crtc != &crtc->base)
  10039. continue;
  10040. encoder = to_intel_encoder(connector_state->best_encoder);
  10041. if (!check_single_encoder_cloning(state, crtc, encoder))
  10042. return false;
  10043. }
  10044. return true;
  10045. }
  10046. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10047. struct drm_crtc_state *crtc_state)
  10048. {
  10049. struct drm_device *dev = crtc->dev;
  10050. struct drm_i915_private *dev_priv = dev->dev_private;
  10051. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10052. struct intel_crtc_state *pipe_config =
  10053. to_intel_crtc_state(crtc_state);
  10054. struct drm_atomic_state *state = crtc_state->state;
  10055. int ret;
  10056. bool mode_changed = needs_modeset(crtc_state);
  10057. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10058. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10059. return -EINVAL;
  10060. }
  10061. if (mode_changed && !crtc_state->active)
  10062. pipe_config->wm_changed = true;
  10063. if (mode_changed && crtc_state->enable &&
  10064. dev_priv->display.crtc_compute_clock &&
  10065. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  10066. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10067. pipe_config);
  10068. if (ret)
  10069. return ret;
  10070. }
  10071. ret = 0;
  10072. if (dev_priv->display.compute_pipe_wm) {
  10073. ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
  10074. if (ret)
  10075. return ret;
  10076. }
  10077. if (INTEL_INFO(dev)->gen >= 9) {
  10078. if (mode_changed)
  10079. ret = skl_update_scaler_crtc(pipe_config);
  10080. if (!ret)
  10081. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10082. pipe_config);
  10083. }
  10084. return ret;
  10085. }
  10086. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10087. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10088. .load_lut = intel_crtc_load_lut,
  10089. .atomic_begin = intel_begin_crtc_commit,
  10090. .atomic_flush = intel_finish_crtc_commit,
  10091. .atomic_check = intel_crtc_atomic_check,
  10092. };
  10093. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10094. {
  10095. struct intel_connector *connector;
  10096. for_each_intel_connector(dev, connector) {
  10097. if (connector->base.encoder) {
  10098. connector->base.state->best_encoder =
  10099. connector->base.encoder;
  10100. connector->base.state->crtc =
  10101. connector->base.encoder->crtc;
  10102. } else {
  10103. connector->base.state->best_encoder = NULL;
  10104. connector->base.state->crtc = NULL;
  10105. }
  10106. }
  10107. }
  10108. static void
  10109. connected_sink_compute_bpp(struct intel_connector *connector,
  10110. struct intel_crtc_state *pipe_config)
  10111. {
  10112. int bpp = pipe_config->pipe_bpp;
  10113. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10114. connector->base.base.id,
  10115. connector->base.name);
  10116. /* Don't use an invalid EDID bpc value */
  10117. if (connector->base.display_info.bpc &&
  10118. connector->base.display_info.bpc * 3 < bpp) {
  10119. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10120. bpp, connector->base.display_info.bpc*3);
  10121. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10122. }
  10123. /* Clamp bpp to default limit on screens without EDID 1.4 */
  10124. if (connector->base.display_info.bpc == 0) {
  10125. int type = connector->base.connector_type;
  10126. int clamp_bpp = 24;
  10127. /* Fall back to 18 bpp when DP sink capability is unknown. */
  10128. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  10129. type == DRM_MODE_CONNECTOR_eDP)
  10130. clamp_bpp = 18;
  10131. if (bpp > clamp_bpp) {
  10132. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  10133. bpp, clamp_bpp);
  10134. pipe_config->pipe_bpp = clamp_bpp;
  10135. }
  10136. }
  10137. }
  10138. static int
  10139. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10140. struct intel_crtc_state *pipe_config)
  10141. {
  10142. struct drm_device *dev = crtc->base.dev;
  10143. struct drm_atomic_state *state;
  10144. struct drm_connector *connector;
  10145. struct drm_connector_state *connector_state;
  10146. int bpp, i;
  10147. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  10148. bpp = 10*3;
  10149. else if (INTEL_INFO(dev)->gen >= 5)
  10150. bpp = 12*3;
  10151. else
  10152. bpp = 8*3;
  10153. pipe_config->pipe_bpp = bpp;
  10154. state = pipe_config->base.state;
  10155. /* Clamp display bpp to EDID value */
  10156. for_each_connector_in_state(state, connector, connector_state, i) {
  10157. if (connector_state->crtc != &crtc->base)
  10158. continue;
  10159. connected_sink_compute_bpp(to_intel_connector(connector),
  10160. pipe_config);
  10161. }
  10162. return bpp;
  10163. }
  10164. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10165. {
  10166. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10167. "type: 0x%x flags: 0x%x\n",
  10168. mode->crtc_clock,
  10169. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10170. mode->crtc_hsync_end, mode->crtc_htotal,
  10171. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10172. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10173. }
  10174. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10175. struct intel_crtc_state *pipe_config,
  10176. const char *context)
  10177. {
  10178. struct drm_device *dev = crtc->base.dev;
  10179. struct drm_plane *plane;
  10180. struct intel_plane *intel_plane;
  10181. struct intel_plane_state *state;
  10182. struct drm_framebuffer *fb;
  10183. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10184. context, pipe_config, pipe_name(crtc->pipe));
  10185. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10186. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10187. pipe_config->pipe_bpp, pipe_config->dither);
  10188. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10189. pipe_config->has_pch_encoder,
  10190. pipe_config->fdi_lanes,
  10191. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10192. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10193. pipe_config->fdi_m_n.tu);
  10194. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10195. pipe_config->has_dp_encoder,
  10196. pipe_config->lane_count,
  10197. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10198. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10199. pipe_config->dp_m_n.tu);
  10200. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10201. pipe_config->has_dp_encoder,
  10202. pipe_config->lane_count,
  10203. pipe_config->dp_m2_n2.gmch_m,
  10204. pipe_config->dp_m2_n2.gmch_n,
  10205. pipe_config->dp_m2_n2.link_m,
  10206. pipe_config->dp_m2_n2.link_n,
  10207. pipe_config->dp_m2_n2.tu);
  10208. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10209. pipe_config->has_audio,
  10210. pipe_config->has_infoframe);
  10211. DRM_DEBUG_KMS("requested mode:\n");
  10212. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10213. DRM_DEBUG_KMS("adjusted mode:\n");
  10214. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10215. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10216. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10217. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10218. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10219. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10220. crtc->num_scalers,
  10221. pipe_config->scaler_state.scaler_users,
  10222. pipe_config->scaler_state.scaler_id);
  10223. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10224. pipe_config->gmch_pfit.control,
  10225. pipe_config->gmch_pfit.pgm_ratios,
  10226. pipe_config->gmch_pfit.lvds_border_bits);
  10227. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10228. pipe_config->pch_pfit.pos,
  10229. pipe_config->pch_pfit.size,
  10230. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10231. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10232. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10233. if (IS_BROXTON(dev)) {
  10234. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10235. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10236. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10237. pipe_config->ddi_pll_sel,
  10238. pipe_config->dpll_hw_state.ebb0,
  10239. pipe_config->dpll_hw_state.ebb4,
  10240. pipe_config->dpll_hw_state.pll0,
  10241. pipe_config->dpll_hw_state.pll1,
  10242. pipe_config->dpll_hw_state.pll2,
  10243. pipe_config->dpll_hw_state.pll3,
  10244. pipe_config->dpll_hw_state.pll6,
  10245. pipe_config->dpll_hw_state.pll8,
  10246. pipe_config->dpll_hw_state.pll9,
  10247. pipe_config->dpll_hw_state.pll10,
  10248. pipe_config->dpll_hw_state.pcsdw12);
  10249. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10250. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10251. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10252. pipe_config->ddi_pll_sel,
  10253. pipe_config->dpll_hw_state.ctrl1,
  10254. pipe_config->dpll_hw_state.cfgcr1,
  10255. pipe_config->dpll_hw_state.cfgcr2);
  10256. } else if (HAS_DDI(dev)) {
  10257. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10258. pipe_config->ddi_pll_sel,
  10259. pipe_config->dpll_hw_state.wrpll,
  10260. pipe_config->dpll_hw_state.spll);
  10261. } else {
  10262. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10263. "fp0: 0x%x, fp1: 0x%x\n",
  10264. pipe_config->dpll_hw_state.dpll,
  10265. pipe_config->dpll_hw_state.dpll_md,
  10266. pipe_config->dpll_hw_state.fp0,
  10267. pipe_config->dpll_hw_state.fp1);
  10268. }
  10269. DRM_DEBUG_KMS("planes on this crtc\n");
  10270. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10271. intel_plane = to_intel_plane(plane);
  10272. if (intel_plane->pipe != crtc->pipe)
  10273. continue;
  10274. state = to_intel_plane_state(plane->state);
  10275. fb = state->base.fb;
  10276. if (!fb) {
  10277. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10278. "disabled, scaler_id = %d\n",
  10279. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10280. plane->base.id, intel_plane->pipe,
  10281. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10282. drm_plane_index(plane), state->scaler_id);
  10283. continue;
  10284. }
  10285. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10286. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10287. plane->base.id, intel_plane->pipe,
  10288. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10289. drm_plane_index(plane));
  10290. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10291. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10292. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10293. state->scaler_id,
  10294. state->src.x1 >> 16, state->src.y1 >> 16,
  10295. drm_rect_width(&state->src) >> 16,
  10296. drm_rect_height(&state->src) >> 16,
  10297. state->dst.x1, state->dst.y1,
  10298. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10299. }
  10300. }
  10301. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10302. {
  10303. struct drm_device *dev = state->dev;
  10304. struct drm_connector *connector;
  10305. unsigned int used_ports = 0;
  10306. /*
  10307. * Walk the connector list instead of the encoder
  10308. * list to detect the problem on ddi platforms
  10309. * where there's just one encoder per digital port.
  10310. */
  10311. drm_for_each_connector(connector, dev) {
  10312. struct drm_connector_state *connector_state;
  10313. struct intel_encoder *encoder;
  10314. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10315. if (!connector_state)
  10316. connector_state = connector->state;
  10317. if (!connector_state->best_encoder)
  10318. continue;
  10319. encoder = to_intel_encoder(connector_state->best_encoder);
  10320. WARN_ON(!connector_state->crtc);
  10321. switch (encoder->type) {
  10322. unsigned int port_mask;
  10323. case INTEL_OUTPUT_UNKNOWN:
  10324. if (WARN_ON(!HAS_DDI(dev)))
  10325. break;
  10326. case INTEL_OUTPUT_DISPLAYPORT:
  10327. case INTEL_OUTPUT_HDMI:
  10328. case INTEL_OUTPUT_EDP:
  10329. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10330. /* the same port mustn't appear more than once */
  10331. if (used_ports & port_mask)
  10332. return false;
  10333. used_ports |= port_mask;
  10334. default:
  10335. break;
  10336. }
  10337. }
  10338. return true;
  10339. }
  10340. static void
  10341. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10342. {
  10343. struct drm_crtc_state tmp_state;
  10344. struct intel_crtc_scaler_state scaler_state;
  10345. struct intel_dpll_hw_state dpll_hw_state;
  10346. enum intel_dpll_id shared_dpll;
  10347. uint32_t ddi_pll_sel;
  10348. bool force_thru;
  10349. /* FIXME: before the switch to atomic started, a new pipe_config was
  10350. * kzalloc'd. Code that depends on any field being zero should be
  10351. * fixed, so that the crtc_state can be safely duplicated. For now,
  10352. * only fields that are know to not cause problems are preserved. */
  10353. tmp_state = crtc_state->base;
  10354. scaler_state = crtc_state->scaler_state;
  10355. shared_dpll = crtc_state->shared_dpll;
  10356. dpll_hw_state = crtc_state->dpll_hw_state;
  10357. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10358. force_thru = crtc_state->pch_pfit.force_thru;
  10359. memset(crtc_state, 0, sizeof *crtc_state);
  10360. crtc_state->base = tmp_state;
  10361. crtc_state->scaler_state = scaler_state;
  10362. crtc_state->shared_dpll = shared_dpll;
  10363. crtc_state->dpll_hw_state = dpll_hw_state;
  10364. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10365. crtc_state->pch_pfit.force_thru = force_thru;
  10366. }
  10367. static int
  10368. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10369. struct intel_crtc_state *pipe_config)
  10370. {
  10371. struct drm_atomic_state *state = pipe_config->base.state;
  10372. struct intel_encoder *encoder;
  10373. struct drm_connector *connector;
  10374. struct drm_connector_state *connector_state;
  10375. int base_bpp, ret = -EINVAL;
  10376. int i;
  10377. bool retry = true;
  10378. clear_intel_crtc_state(pipe_config);
  10379. pipe_config->cpu_transcoder =
  10380. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10381. /*
  10382. * Sanitize sync polarity flags based on requested ones. If neither
  10383. * positive or negative polarity is requested, treat this as meaning
  10384. * negative polarity.
  10385. */
  10386. if (!(pipe_config->base.adjusted_mode.flags &
  10387. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10388. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10389. if (!(pipe_config->base.adjusted_mode.flags &
  10390. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10391. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10392. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10393. pipe_config);
  10394. if (base_bpp < 0)
  10395. goto fail;
  10396. /*
  10397. * Determine the real pipe dimensions. Note that stereo modes can
  10398. * increase the actual pipe size due to the frame doubling and
  10399. * insertion of additional space for blanks between the frame. This
  10400. * is stored in the crtc timings. We use the requested mode to do this
  10401. * computation to clearly distinguish it from the adjusted mode, which
  10402. * can be changed by the connectors in the below retry loop.
  10403. */
  10404. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10405. &pipe_config->pipe_src_w,
  10406. &pipe_config->pipe_src_h);
  10407. encoder_retry:
  10408. /* Ensure the port clock defaults are reset when retrying. */
  10409. pipe_config->port_clock = 0;
  10410. pipe_config->pixel_multiplier = 1;
  10411. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10412. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10413. CRTC_STEREO_DOUBLE);
  10414. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10415. * adjust it according to limitations or connector properties, and also
  10416. * a chance to reject the mode entirely.
  10417. */
  10418. for_each_connector_in_state(state, connector, connector_state, i) {
  10419. if (connector_state->crtc != crtc)
  10420. continue;
  10421. encoder = to_intel_encoder(connector_state->best_encoder);
  10422. if (!(encoder->compute_config(encoder, pipe_config))) {
  10423. DRM_DEBUG_KMS("Encoder config failure\n");
  10424. goto fail;
  10425. }
  10426. }
  10427. /* Set default port clock if not overwritten by the encoder. Needs to be
  10428. * done afterwards in case the encoder adjusts the mode. */
  10429. if (!pipe_config->port_clock)
  10430. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10431. * pipe_config->pixel_multiplier;
  10432. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10433. if (ret < 0) {
  10434. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10435. goto fail;
  10436. }
  10437. if (ret == RETRY) {
  10438. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10439. ret = -EINVAL;
  10440. goto fail;
  10441. }
  10442. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10443. retry = false;
  10444. goto encoder_retry;
  10445. }
  10446. /* Dithering seems to not pass-through bits correctly when it should, so
  10447. * only enable it on 6bpc panels. */
  10448. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10449. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10450. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10451. fail:
  10452. return ret;
  10453. }
  10454. static void
  10455. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10456. {
  10457. struct drm_crtc *crtc;
  10458. struct drm_crtc_state *crtc_state;
  10459. int i;
  10460. /* Double check state. */
  10461. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10462. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10463. /* Update hwmode for vblank functions */
  10464. if (crtc->state->active)
  10465. crtc->hwmode = crtc->state->adjusted_mode;
  10466. else
  10467. crtc->hwmode.crtc_clock = 0;
  10468. /*
  10469. * Update legacy state to satisfy fbc code. This can
  10470. * be removed when fbc uses the atomic state.
  10471. */
  10472. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10473. struct drm_plane_state *plane_state = crtc->primary->state;
  10474. crtc->primary->fb = plane_state->fb;
  10475. crtc->x = plane_state->src_x >> 16;
  10476. crtc->y = plane_state->src_y >> 16;
  10477. }
  10478. }
  10479. }
  10480. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10481. {
  10482. int diff;
  10483. if (clock1 == clock2)
  10484. return true;
  10485. if (!clock1 || !clock2)
  10486. return false;
  10487. diff = abs(clock1 - clock2);
  10488. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10489. return true;
  10490. return false;
  10491. }
  10492. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10493. list_for_each_entry((intel_crtc), \
  10494. &(dev)->mode_config.crtc_list, \
  10495. base.head) \
  10496. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  10497. static bool
  10498. intel_compare_m_n(unsigned int m, unsigned int n,
  10499. unsigned int m2, unsigned int n2,
  10500. bool exact)
  10501. {
  10502. if (m == m2 && n == n2)
  10503. return true;
  10504. if (exact || !m || !n || !m2 || !n2)
  10505. return false;
  10506. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10507. if (n > n2) {
  10508. while (n > n2) {
  10509. m2 <<= 1;
  10510. n2 <<= 1;
  10511. }
  10512. } else if (n < n2) {
  10513. while (n < n2) {
  10514. m <<= 1;
  10515. n <<= 1;
  10516. }
  10517. }
  10518. if (n != n2)
  10519. return false;
  10520. return intel_fuzzy_clock_check(m, m2);
  10521. }
  10522. static bool
  10523. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10524. struct intel_link_m_n *m2_n2,
  10525. bool adjust)
  10526. {
  10527. if (m_n->tu == m2_n2->tu &&
  10528. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10529. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10530. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10531. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10532. if (adjust)
  10533. *m2_n2 = *m_n;
  10534. return true;
  10535. }
  10536. return false;
  10537. }
  10538. static bool
  10539. intel_pipe_config_compare(struct drm_device *dev,
  10540. struct intel_crtc_state *current_config,
  10541. struct intel_crtc_state *pipe_config,
  10542. bool adjust)
  10543. {
  10544. bool ret = true;
  10545. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10546. do { \
  10547. if (!adjust) \
  10548. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10549. else \
  10550. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10551. } while (0)
  10552. #define PIPE_CONF_CHECK_X(name) \
  10553. if (current_config->name != pipe_config->name) { \
  10554. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10555. "(expected 0x%08x, found 0x%08x)\n", \
  10556. current_config->name, \
  10557. pipe_config->name); \
  10558. ret = false; \
  10559. }
  10560. #define PIPE_CONF_CHECK_I(name) \
  10561. if (current_config->name != pipe_config->name) { \
  10562. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10563. "(expected %i, found %i)\n", \
  10564. current_config->name, \
  10565. pipe_config->name); \
  10566. ret = false; \
  10567. }
  10568. #define PIPE_CONF_CHECK_M_N(name) \
  10569. if (!intel_compare_link_m_n(&current_config->name, \
  10570. &pipe_config->name,\
  10571. adjust)) { \
  10572. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10573. "(expected tu %i gmch %i/%i link %i/%i, " \
  10574. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10575. current_config->name.tu, \
  10576. current_config->name.gmch_m, \
  10577. current_config->name.gmch_n, \
  10578. current_config->name.link_m, \
  10579. current_config->name.link_n, \
  10580. pipe_config->name.tu, \
  10581. pipe_config->name.gmch_m, \
  10582. pipe_config->name.gmch_n, \
  10583. pipe_config->name.link_m, \
  10584. pipe_config->name.link_n); \
  10585. ret = false; \
  10586. }
  10587. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10588. if (!intel_compare_link_m_n(&current_config->name, \
  10589. &pipe_config->name, adjust) && \
  10590. !intel_compare_link_m_n(&current_config->alt_name, \
  10591. &pipe_config->name, adjust)) { \
  10592. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10593. "(expected tu %i gmch %i/%i link %i/%i, " \
  10594. "or tu %i gmch %i/%i link %i/%i, " \
  10595. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10596. current_config->name.tu, \
  10597. current_config->name.gmch_m, \
  10598. current_config->name.gmch_n, \
  10599. current_config->name.link_m, \
  10600. current_config->name.link_n, \
  10601. current_config->alt_name.tu, \
  10602. current_config->alt_name.gmch_m, \
  10603. current_config->alt_name.gmch_n, \
  10604. current_config->alt_name.link_m, \
  10605. current_config->alt_name.link_n, \
  10606. pipe_config->name.tu, \
  10607. pipe_config->name.gmch_m, \
  10608. pipe_config->name.gmch_n, \
  10609. pipe_config->name.link_m, \
  10610. pipe_config->name.link_n); \
  10611. ret = false; \
  10612. }
  10613. /* This is required for BDW+ where there is only one set of registers for
  10614. * switching between high and low RR.
  10615. * This macro can be used whenever a comparison has to be made between one
  10616. * hw state and multiple sw state variables.
  10617. */
  10618. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10619. if ((current_config->name != pipe_config->name) && \
  10620. (current_config->alt_name != pipe_config->name)) { \
  10621. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10622. "(expected %i or %i, found %i)\n", \
  10623. current_config->name, \
  10624. current_config->alt_name, \
  10625. pipe_config->name); \
  10626. ret = false; \
  10627. }
  10628. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10629. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10630. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10631. "(expected %i, found %i)\n", \
  10632. current_config->name & (mask), \
  10633. pipe_config->name & (mask)); \
  10634. ret = false; \
  10635. }
  10636. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10637. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10638. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10639. "(expected %i, found %i)\n", \
  10640. current_config->name, \
  10641. pipe_config->name); \
  10642. ret = false; \
  10643. }
  10644. #define PIPE_CONF_QUIRK(quirk) \
  10645. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10646. PIPE_CONF_CHECK_I(cpu_transcoder);
  10647. PIPE_CONF_CHECK_I(has_pch_encoder);
  10648. PIPE_CONF_CHECK_I(fdi_lanes);
  10649. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10650. PIPE_CONF_CHECK_I(has_dp_encoder);
  10651. PIPE_CONF_CHECK_I(lane_count);
  10652. if (INTEL_INFO(dev)->gen < 8) {
  10653. PIPE_CONF_CHECK_M_N(dp_m_n);
  10654. if (current_config->has_drrs)
  10655. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10656. } else
  10657. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10658. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10659. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10660. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10661. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10662. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10663. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10664. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10665. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10666. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10667. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10668. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10669. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10670. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10671. PIPE_CONF_CHECK_I(pixel_multiplier);
  10672. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10673. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10674. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10675. PIPE_CONF_CHECK_I(limited_color_range);
  10676. PIPE_CONF_CHECK_I(has_infoframe);
  10677. PIPE_CONF_CHECK_I(has_audio);
  10678. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10679. DRM_MODE_FLAG_INTERLACE);
  10680. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10681. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10682. DRM_MODE_FLAG_PHSYNC);
  10683. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10684. DRM_MODE_FLAG_NHSYNC);
  10685. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10686. DRM_MODE_FLAG_PVSYNC);
  10687. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10688. DRM_MODE_FLAG_NVSYNC);
  10689. }
  10690. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10691. /* pfit ratios are autocomputed by the hw on gen4+ */
  10692. if (INTEL_INFO(dev)->gen < 4)
  10693. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10694. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10695. if (!adjust) {
  10696. PIPE_CONF_CHECK_I(pipe_src_w);
  10697. PIPE_CONF_CHECK_I(pipe_src_h);
  10698. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10699. if (current_config->pch_pfit.enabled) {
  10700. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10701. PIPE_CONF_CHECK_X(pch_pfit.size);
  10702. }
  10703. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10704. }
  10705. /* BDW+ don't expose a synchronous way to read the state */
  10706. if (IS_HASWELL(dev))
  10707. PIPE_CONF_CHECK_I(ips_enabled);
  10708. PIPE_CONF_CHECK_I(double_wide);
  10709. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10710. PIPE_CONF_CHECK_I(shared_dpll);
  10711. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10712. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10713. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10714. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10715. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10716. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10717. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10718. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10719. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10720. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10721. PIPE_CONF_CHECK_I(pipe_bpp);
  10722. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10723. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10724. #undef PIPE_CONF_CHECK_X
  10725. #undef PIPE_CONF_CHECK_I
  10726. #undef PIPE_CONF_CHECK_I_ALT
  10727. #undef PIPE_CONF_CHECK_FLAGS
  10728. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10729. #undef PIPE_CONF_QUIRK
  10730. #undef INTEL_ERR_OR_DBG_KMS
  10731. return ret;
  10732. }
  10733. static void check_wm_state(struct drm_device *dev)
  10734. {
  10735. struct drm_i915_private *dev_priv = dev->dev_private;
  10736. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10737. struct intel_crtc *intel_crtc;
  10738. int plane;
  10739. if (INTEL_INFO(dev)->gen < 9)
  10740. return;
  10741. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10742. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10743. for_each_intel_crtc(dev, intel_crtc) {
  10744. struct skl_ddb_entry *hw_entry, *sw_entry;
  10745. const enum pipe pipe = intel_crtc->pipe;
  10746. if (!intel_crtc->active)
  10747. continue;
  10748. /* planes */
  10749. for_each_plane(dev_priv, pipe, plane) {
  10750. hw_entry = &hw_ddb.plane[pipe][plane];
  10751. sw_entry = &sw_ddb->plane[pipe][plane];
  10752. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10753. continue;
  10754. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10755. "(expected (%u,%u), found (%u,%u))\n",
  10756. pipe_name(pipe), plane + 1,
  10757. sw_entry->start, sw_entry->end,
  10758. hw_entry->start, hw_entry->end);
  10759. }
  10760. /* cursor */
  10761. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10762. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10763. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10764. continue;
  10765. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10766. "(expected (%u,%u), found (%u,%u))\n",
  10767. pipe_name(pipe),
  10768. sw_entry->start, sw_entry->end,
  10769. hw_entry->start, hw_entry->end);
  10770. }
  10771. }
  10772. static void
  10773. check_connector_state(struct drm_device *dev,
  10774. struct drm_atomic_state *old_state)
  10775. {
  10776. struct drm_connector_state *old_conn_state;
  10777. struct drm_connector *connector;
  10778. int i;
  10779. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10780. struct drm_encoder *encoder = connector->encoder;
  10781. struct drm_connector_state *state = connector->state;
  10782. /* This also checks the encoder/connector hw state with the
  10783. * ->get_hw_state callbacks. */
  10784. intel_connector_check_state(to_intel_connector(connector));
  10785. I915_STATE_WARN(state->best_encoder != encoder,
  10786. "connector's atomic encoder doesn't match legacy encoder\n");
  10787. }
  10788. }
  10789. static void
  10790. check_encoder_state(struct drm_device *dev)
  10791. {
  10792. struct intel_encoder *encoder;
  10793. struct intel_connector *connector;
  10794. for_each_intel_encoder(dev, encoder) {
  10795. bool enabled = false;
  10796. enum pipe pipe;
  10797. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10798. encoder->base.base.id,
  10799. encoder->base.name);
  10800. for_each_intel_connector(dev, connector) {
  10801. if (connector->base.state->best_encoder != &encoder->base)
  10802. continue;
  10803. enabled = true;
  10804. I915_STATE_WARN(connector->base.state->crtc !=
  10805. encoder->base.crtc,
  10806. "connector's crtc doesn't match encoder crtc\n");
  10807. }
  10808. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10809. "encoder's enabled state mismatch "
  10810. "(expected %i, found %i)\n",
  10811. !!encoder->base.crtc, enabled);
  10812. if (!encoder->base.crtc) {
  10813. bool active;
  10814. active = encoder->get_hw_state(encoder, &pipe);
  10815. I915_STATE_WARN(active,
  10816. "encoder detached but still enabled on pipe %c.\n",
  10817. pipe_name(pipe));
  10818. }
  10819. }
  10820. }
  10821. static void
  10822. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10823. {
  10824. struct drm_i915_private *dev_priv = dev->dev_private;
  10825. struct intel_encoder *encoder;
  10826. struct drm_crtc_state *old_crtc_state;
  10827. struct drm_crtc *crtc;
  10828. int i;
  10829. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10831. struct intel_crtc_state *pipe_config, *sw_config;
  10832. bool active;
  10833. if (!needs_modeset(crtc->state) &&
  10834. !to_intel_crtc_state(crtc->state)->update_pipe)
  10835. continue;
  10836. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10837. pipe_config = to_intel_crtc_state(old_crtc_state);
  10838. memset(pipe_config, 0, sizeof(*pipe_config));
  10839. pipe_config->base.crtc = crtc;
  10840. pipe_config->base.state = old_state;
  10841. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10842. crtc->base.id);
  10843. active = dev_priv->display.get_pipe_config(intel_crtc,
  10844. pipe_config);
  10845. /* hw state is inconsistent with the pipe quirk */
  10846. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10847. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10848. active = crtc->state->active;
  10849. I915_STATE_WARN(crtc->state->active != active,
  10850. "crtc active state doesn't match with hw state "
  10851. "(expected %i, found %i)\n", crtc->state->active, active);
  10852. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10853. "transitional active state does not match atomic hw state "
  10854. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10855. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10856. enum pipe pipe;
  10857. active = encoder->get_hw_state(encoder, &pipe);
  10858. I915_STATE_WARN(active != crtc->state->active,
  10859. "[ENCODER:%i] active %i with crtc active %i\n",
  10860. encoder->base.base.id, active, crtc->state->active);
  10861. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10862. "Encoder connected to wrong pipe %c\n",
  10863. pipe_name(pipe));
  10864. if (active)
  10865. encoder->get_config(encoder, pipe_config);
  10866. }
  10867. if (!crtc->state->active)
  10868. continue;
  10869. sw_config = to_intel_crtc_state(crtc->state);
  10870. if (!intel_pipe_config_compare(dev, sw_config,
  10871. pipe_config, false)) {
  10872. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10873. intel_dump_pipe_config(intel_crtc, pipe_config,
  10874. "[hw state]");
  10875. intel_dump_pipe_config(intel_crtc, sw_config,
  10876. "[sw state]");
  10877. }
  10878. }
  10879. }
  10880. static void
  10881. check_shared_dpll_state(struct drm_device *dev)
  10882. {
  10883. struct drm_i915_private *dev_priv = dev->dev_private;
  10884. struct intel_crtc *crtc;
  10885. struct intel_dpll_hw_state dpll_hw_state;
  10886. int i;
  10887. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10888. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10889. int enabled_crtcs = 0, active_crtcs = 0;
  10890. bool active;
  10891. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10892. DRM_DEBUG_KMS("%s\n", pll->name);
  10893. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10894. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10895. "more active pll users than references: %i vs %i\n",
  10896. pll->active, hweight32(pll->config.crtc_mask));
  10897. I915_STATE_WARN(pll->active && !pll->on,
  10898. "pll in active use but not on in sw tracking\n");
  10899. I915_STATE_WARN(pll->on && !pll->active,
  10900. "pll in on but not on in use in sw tracking\n");
  10901. I915_STATE_WARN(pll->on != active,
  10902. "pll on state mismatch (expected %i, found %i)\n",
  10903. pll->on, active);
  10904. for_each_intel_crtc(dev, crtc) {
  10905. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10906. enabled_crtcs++;
  10907. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10908. active_crtcs++;
  10909. }
  10910. I915_STATE_WARN(pll->active != active_crtcs,
  10911. "pll active crtcs mismatch (expected %i, found %i)\n",
  10912. pll->active, active_crtcs);
  10913. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10914. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10915. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10916. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10917. sizeof(dpll_hw_state)),
  10918. "pll hw state mismatch\n");
  10919. }
  10920. }
  10921. static void
  10922. intel_modeset_check_state(struct drm_device *dev,
  10923. struct drm_atomic_state *old_state)
  10924. {
  10925. check_wm_state(dev);
  10926. check_connector_state(dev, old_state);
  10927. check_encoder_state(dev);
  10928. check_crtc_state(dev, old_state);
  10929. check_shared_dpll_state(dev);
  10930. }
  10931. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10932. int dotclock)
  10933. {
  10934. /*
  10935. * FDI already provided one idea for the dotclock.
  10936. * Yell if the encoder disagrees.
  10937. */
  10938. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10939. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10940. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10941. }
  10942. static void update_scanline_offset(struct intel_crtc *crtc)
  10943. {
  10944. struct drm_device *dev = crtc->base.dev;
  10945. /*
  10946. * The scanline counter increments at the leading edge of hsync.
  10947. *
  10948. * On most platforms it starts counting from vtotal-1 on the
  10949. * first active line. That means the scanline counter value is
  10950. * always one less than what we would expect. Ie. just after
  10951. * start of vblank, which also occurs at start of hsync (on the
  10952. * last active line), the scanline counter will read vblank_start-1.
  10953. *
  10954. * On gen2 the scanline counter starts counting from 1 instead
  10955. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10956. * to keep the value positive), instead of adding one.
  10957. *
  10958. * On HSW+ the behaviour of the scanline counter depends on the output
  10959. * type. For DP ports it behaves like most other platforms, but on HDMI
  10960. * there's an extra 1 line difference. So we need to add two instead of
  10961. * one to the value.
  10962. */
  10963. if (IS_GEN2(dev)) {
  10964. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10965. int vtotal;
  10966. vtotal = adjusted_mode->crtc_vtotal;
  10967. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10968. vtotal /= 2;
  10969. crtc->scanline_offset = vtotal - 1;
  10970. } else if (HAS_DDI(dev) &&
  10971. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10972. crtc->scanline_offset = 2;
  10973. } else
  10974. crtc->scanline_offset = 1;
  10975. }
  10976. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10977. {
  10978. struct drm_device *dev = state->dev;
  10979. struct drm_i915_private *dev_priv = to_i915(dev);
  10980. struct intel_shared_dpll_config *shared_dpll = NULL;
  10981. struct intel_crtc *intel_crtc;
  10982. struct intel_crtc_state *intel_crtc_state;
  10983. struct drm_crtc *crtc;
  10984. struct drm_crtc_state *crtc_state;
  10985. int i;
  10986. if (!dev_priv->display.crtc_compute_clock)
  10987. return;
  10988. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10989. int dpll;
  10990. intel_crtc = to_intel_crtc(crtc);
  10991. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10992. dpll = intel_crtc_state->shared_dpll;
  10993. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10994. continue;
  10995. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10996. if (!shared_dpll)
  10997. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10998. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10999. }
  11000. }
  11001. /*
  11002. * This implements the workaround described in the "notes" section of the mode
  11003. * set sequence documentation. When going from no pipes or single pipe to
  11004. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11005. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11006. */
  11007. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11008. {
  11009. struct drm_crtc_state *crtc_state;
  11010. struct intel_crtc *intel_crtc;
  11011. struct drm_crtc *crtc;
  11012. struct intel_crtc_state *first_crtc_state = NULL;
  11013. struct intel_crtc_state *other_crtc_state = NULL;
  11014. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11015. int i;
  11016. /* look at all crtc's that are going to be enabled in during modeset */
  11017. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11018. intel_crtc = to_intel_crtc(crtc);
  11019. if (!crtc_state->active || !needs_modeset(crtc_state))
  11020. continue;
  11021. if (first_crtc_state) {
  11022. other_crtc_state = to_intel_crtc_state(crtc_state);
  11023. break;
  11024. } else {
  11025. first_crtc_state = to_intel_crtc_state(crtc_state);
  11026. first_pipe = intel_crtc->pipe;
  11027. }
  11028. }
  11029. /* No workaround needed? */
  11030. if (!first_crtc_state)
  11031. return 0;
  11032. /* w/a possibly needed, check how many crtc's are already enabled. */
  11033. for_each_intel_crtc(state->dev, intel_crtc) {
  11034. struct intel_crtc_state *pipe_config;
  11035. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11036. if (IS_ERR(pipe_config))
  11037. return PTR_ERR(pipe_config);
  11038. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11039. if (!pipe_config->base.active ||
  11040. needs_modeset(&pipe_config->base))
  11041. continue;
  11042. /* 2 or more enabled crtcs means no need for w/a */
  11043. if (enabled_pipe != INVALID_PIPE)
  11044. return 0;
  11045. enabled_pipe = intel_crtc->pipe;
  11046. }
  11047. if (enabled_pipe != INVALID_PIPE)
  11048. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11049. else if (other_crtc_state)
  11050. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11051. return 0;
  11052. }
  11053. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11054. {
  11055. struct drm_crtc *crtc;
  11056. struct drm_crtc_state *crtc_state;
  11057. int ret = 0;
  11058. /* add all active pipes to the state */
  11059. for_each_crtc(state->dev, crtc) {
  11060. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11061. if (IS_ERR(crtc_state))
  11062. return PTR_ERR(crtc_state);
  11063. if (!crtc_state->active || needs_modeset(crtc_state))
  11064. continue;
  11065. crtc_state->mode_changed = true;
  11066. ret = drm_atomic_add_affected_connectors(state, crtc);
  11067. if (ret)
  11068. break;
  11069. ret = drm_atomic_add_affected_planes(state, crtc);
  11070. if (ret)
  11071. break;
  11072. }
  11073. return ret;
  11074. }
  11075. static int intel_modeset_checks(struct drm_atomic_state *state)
  11076. {
  11077. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11078. struct drm_i915_private *dev_priv = state->dev->dev_private;
  11079. struct drm_crtc *crtc;
  11080. struct drm_crtc_state *crtc_state;
  11081. int ret = 0, i;
  11082. if (!check_digital_port_conflicts(state)) {
  11083. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11084. return -EINVAL;
  11085. }
  11086. intel_state->modeset = true;
  11087. intel_state->active_crtcs = dev_priv->active_crtcs;
  11088. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11089. if (crtc_state->active)
  11090. intel_state->active_crtcs |= 1 << i;
  11091. else
  11092. intel_state->active_crtcs &= ~(1 << i);
  11093. }
  11094. /*
  11095. * See if the config requires any additional preparation, e.g.
  11096. * to adjust global state with pipes off. We need to do this
  11097. * here so we can get the modeset_pipe updated config for the new
  11098. * mode set on this crtc. For other crtcs we need to use the
  11099. * adjusted_mode bits in the crtc directly.
  11100. */
  11101. if (dev_priv->display.modeset_calc_cdclk) {
  11102. ret = dev_priv->display.modeset_calc_cdclk(state);
  11103. if (!ret && intel_state->dev_cdclk != dev_priv->cdclk_freq)
  11104. ret = intel_modeset_all_pipes(state);
  11105. if (ret < 0)
  11106. return ret;
  11107. } else
  11108. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11109. intel_modeset_clear_plls(state);
  11110. if (IS_HASWELL(dev_priv))
  11111. return haswell_mode_set_planes_workaround(state);
  11112. return 0;
  11113. }
  11114. /*
  11115. * Handle calculation of various watermark data at the end of the atomic check
  11116. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11117. * handlers to ensure that all derived state has been updated.
  11118. */
  11119. static void calc_watermark_data(struct drm_atomic_state *state)
  11120. {
  11121. struct drm_device *dev = state->dev;
  11122. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11123. struct drm_crtc *crtc;
  11124. struct drm_crtc_state *cstate;
  11125. struct drm_plane *plane;
  11126. struct drm_plane_state *pstate;
  11127. /*
  11128. * Calculate watermark configuration details now that derived
  11129. * plane/crtc state is all properly updated.
  11130. */
  11131. drm_for_each_crtc(crtc, dev) {
  11132. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  11133. crtc->state;
  11134. if (cstate->active)
  11135. intel_state->wm_config.num_pipes_active++;
  11136. }
  11137. drm_for_each_legacy_plane(plane, dev) {
  11138. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11139. plane->state;
  11140. if (!to_intel_plane_state(pstate)->visible)
  11141. continue;
  11142. intel_state->wm_config.sprites_enabled = true;
  11143. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11144. pstate->crtc_h != pstate->src_h >> 16)
  11145. intel_state->wm_config.sprites_scaled = true;
  11146. }
  11147. }
  11148. /**
  11149. * intel_atomic_check - validate state object
  11150. * @dev: drm device
  11151. * @state: state to validate
  11152. */
  11153. static int intel_atomic_check(struct drm_device *dev,
  11154. struct drm_atomic_state *state)
  11155. {
  11156. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11157. struct drm_crtc *crtc;
  11158. struct drm_crtc_state *crtc_state;
  11159. int ret, i;
  11160. bool any_ms = false;
  11161. ret = drm_atomic_helper_check_modeset(dev, state);
  11162. if (ret)
  11163. return ret;
  11164. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11165. struct intel_crtc_state *pipe_config =
  11166. to_intel_crtc_state(crtc_state);
  11167. memset(&to_intel_crtc(crtc)->atomic, 0,
  11168. sizeof(struct intel_crtc_atomic_commit));
  11169. /* Catch I915_MODE_FLAG_INHERITED */
  11170. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11171. crtc_state->mode_changed = true;
  11172. if (!crtc_state->enable) {
  11173. if (needs_modeset(crtc_state))
  11174. any_ms = true;
  11175. continue;
  11176. }
  11177. if (!needs_modeset(crtc_state))
  11178. continue;
  11179. /* FIXME: For only active_changed we shouldn't need to do any
  11180. * state recomputation at all. */
  11181. ret = drm_atomic_add_affected_connectors(state, crtc);
  11182. if (ret)
  11183. return ret;
  11184. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11185. if (ret)
  11186. return ret;
  11187. if (i915.fastboot &&
  11188. intel_pipe_config_compare(state->dev,
  11189. to_intel_crtc_state(crtc->state),
  11190. pipe_config, true)) {
  11191. crtc_state->mode_changed = false;
  11192. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11193. }
  11194. if (needs_modeset(crtc_state)) {
  11195. any_ms = true;
  11196. ret = drm_atomic_add_affected_planes(state, crtc);
  11197. if (ret)
  11198. return ret;
  11199. }
  11200. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11201. needs_modeset(crtc_state) ?
  11202. "[modeset]" : "[fastset]");
  11203. }
  11204. if (any_ms) {
  11205. ret = intel_modeset_checks(state);
  11206. if (ret)
  11207. return ret;
  11208. } else
  11209. intel_state->cdclk = to_i915(state->dev)->cdclk_freq;
  11210. ret = drm_atomic_helper_check_planes(state->dev, state);
  11211. if (ret)
  11212. return ret;
  11213. calc_watermark_data(state);
  11214. return 0;
  11215. }
  11216. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11217. struct drm_atomic_state *state,
  11218. bool async)
  11219. {
  11220. struct drm_i915_private *dev_priv = dev->dev_private;
  11221. struct drm_plane_state *plane_state;
  11222. struct drm_crtc_state *crtc_state;
  11223. struct drm_plane *plane;
  11224. struct drm_crtc *crtc;
  11225. int i, ret;
  11226. if (async) {
  11227. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11228. return -EINVAL;
  11229. }
  11230. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11231. ret = intel_crtc_wait_for_pending_flips(crtc);
  11232. if (ret)
  11233. return ret;
  11234. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11235. flush_workqueue(dev_priv->wq);
  11236. }
  11237. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11238. if (ret)
  11239. return ret;
  11240. ret = drm_atomic_helper_prepare_planes(dev, state);
  11241. if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
  11242. u32 reset_counter;
  11243. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  11244. mutex_unlock(&dev->struct_mutex);
  11245. for_each_plane_in_state(state, plane, plane_state, i) {
  11246. struct intel_plane_state *intel_plane_state =
  11247. to_intel_plane_state(plane_state);
  11248. if (!intel_plane_state->wait_req)
  11249. continue;
  11250. ret = __i915_wait_request(intel_plane_state->wait_req,
  11251. reset_counter, true,
  11252. NULL, NULL);
  11253. /* Swallow -EIO errors to allow updates during hw lockup. */
  11254. if (ret == -EIO)
  11255. ret = 0;
  11256. if (ret)
  11257. break;
  11258. }
  11259. if (!ret)
  11260. return 0;
  11261. mutex_lock(&dev->struct_mutex);
  11262. drm_atomic_helper_cleanup_planes(dev, state);
  11263. }
  11264. mutex_unlock(&dev->struct_mutex);
  11265. return ret;
  11266. }
  11267. /**
  11268. * intel_atomic_commit - commit validated state object
  11269. * @dev: DRM device
  11270. * @state: the top-level driver state object
  11271. * @async: asynchronous commit
  11272. *
  11273. * This function commits a top-level state object that has been validated
  11274. * with drm_atomic_helper_check().
  11275. *
  11276. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11277. * we can only handle plane-related operations and do not yet support
  11278. * asynchronous commit.
  11279. *
  11280. * RETURNS
  11281. * Zero for success or -errno.
  11282. */
  11283. static int intel_atomic_commit(struct drm_device *dev,
  11284. struct drm_atomic_state *state,
  11285. bool async)
  11286. {
  11287. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11288. struct drm_i915_private *dev_priv = dev->dev_private;
  11289. struct drm_crtc_state *crtc_state;
  11290. struct drm_crtc *crtc;
  11291. int ret = 0, i;
  11292. bool hw_check = intel_state->modeset;
  11293. ret = intel_atomic_prepare_commit(dev, state, async);
  11294. if (ret) {
  11295. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11296. return ret;
  11297. }
  11298. drm_atomic_helper_swap_state(dev, state);
  11299. dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
  11300. if (intel_state->modeset) {
  11301. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  11302. sizeof(intel_state->min_pixclk));
  11303. dev_priv->active_crtcs = intel_state->active_crtcs;
  11304. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  11305. }
  11306. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11307. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11308. if (!needs_modeset(crtc->state))
  11309. continue;
  11310. intel_pre_plane_update(intel_crtc);
  11311. if (crtc_state->active) {
  11312. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11313. dev_priv->display.crtc_disable(crtc);
  11314. intel_crtc->active = false;
  11315. intel_disable_shared_dpll(intel_crtc);
  11316. /*
  11317. * Underruns don't always raise
  11318. * interrupts, so check manually.
  11319. */
  11320. intel_check_cpu_fifo_underruns(dev_priv);
  11321. intel_check_pch_fifo_underruns(dev_priv);
  11322. if (!crtc->state->active)
  11323. intel_update_watermarks(crtc);
  11324. }
  11325. }
  11326. /* Only after disabling all output pipelines that will be changed can we
  11327. * update the the output configuration. */
  11328. intel_modeset_update_crtc_state(state);
  11329. if (intel_state->modeset) {
  11330. intel_shared_dpll_commit(state);
  11331. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11332. modeset_update_crtc_power_domains(state);
  11333. }
  11334. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11335. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11336. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11337. bool modeset = needs_modeset(crtc->state);
  11338. bool update_pipe = !modeset &&
  11339. to_intel_crtc_state(crtc->state)->update_pipe;
  11340. unsigned long put_domains = 0;
  11341. if (modeset)
  11342. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11343. if (modeset && crtc->state->active) {
  11344. update_scanline_offset(to_intel_crtc(crtc));
  11345. dev_priv->display.crtc_enable(crtc);
  11346. }
  11347. if (update_pipe) {
  11348. put_domains = modeset_get_crtc_power_domains(crtc);
  11349. /* make sure intel_modeset_check_state runs */
  11350. hw_check = true;
  11351. }
  11352. if (!modeset)
  11353. intel_pre_plane_update(intel_crtc);
  11354. if (crtc->state->active &&
  11355. (crtc->state->planes_changed || update_pipe))
  11356. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11357. if (put_domains)
  11358. modeset_put_power_domains(dev_priv, put_domains);
  11359. intel_post_plane_update(intel_crtc);
  11360. if (modeset)
  11361. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11362. }
  11363. /* FIXME: add subpixel order */
  11364. drm_atomic_helper_wait_for_vblanks(dev, state);
  11365. mutex_lock(&dev->struct_mutex);
  11366. drm_atomic_helper_cleanup_planes(dev, state);
  11367. mutex_unlock(&dev->struct_mutex);
  11368. if (hw_check)
  11369. intel_modeset_check_state(dev, state);
  11370. drm_atomic_state_free(state);
  11371. /* As one of the primary mmio accessors, KMS has a high likelihood
  11372. * of triggering bugs in unclaimed access. After we finish
  11373. * modesetting, see if an error has been flagged, and if so
  11374. * enable debugging for the next modeset - and hope we catch
  11375. * the culprit.
  11376. *
  11377. * XXX note that we assume display power is on at this point.
  11378. * This might hold true now but we need to add pm helper to check
  11379. * unclaimed only when the hardware is on, as atomic commits
  11380. * can happen also when the device is completely off.
  11381. */
  11382. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11383. return 0;
  11384. }
  11385. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11386. {
  11387. struct drm_device *dev = crtc->dev;
  11388. struct drm_atomic_state *state;
  11389. struct drm_crtc_state *crtc_state;
  11390. int ret;
  11391. state = drm_atomic_state_alloc(dev);
  11392. if (!state) {
  11393. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11394. crtc->base.id);
  11395. return;
  11396. }
  11397. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11398. retry:
  11399. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11400. ret = PTR_ERR_OR_ZERO(crtc_state);
  11401. if (!ret) {
  11402. if (!crtc_state->active)
  11403. goto out;
  11404. crtc_state->mode_changed = true;
  11405. ret = drm_atomic_commit(state);
  11406. }
  11407. if (ret == -EDEADLK) {
  11408. drm_atomic_state_clear(state);
  11409. drm_modeset_backoff(state->acquire_ctx);
  11410. goto retry;
  11411. }
  11412. if (ret)
  11413. out:
  11414. drm_atomic_state_free(state);
  11415. }
  11416. #undef for_each_intel_crtc_masked
  11417. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11418. .gamma_set = intel_crtc_gamma_set,
  11419. .set_config = drm_atomic_helper_set_config,
  11420. .destroy = intel_crtc_destroy,
  11421. .page_flip = intel_crtc_page_flip,
  11422. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11423. .atomic_destroy_state = intel_crtc_destroy_state,
  11424. };
  11425. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11426. struct intel_shared_dpll *pll,
  11427. struct intel_dpll_hw_state *hw_state)
  11428. {
  11429. uint32_t val;
  11430. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11431. return false;
  11432. val = I915_READ(PCH_DPLL(pll->id));
  11433. hw_state->dpll = val;
  11434. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11435. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11436. return val & DPLL_VCO_ENABLE;
  11437. }
  11438. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11439. struct intel_shared_dpll *pll)
  11440. {
  11441. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11442. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11443. }
  11444. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11445. struct intel_shared_dpll *pll)
  11446. {
  11447. /* PCH refclock must be enabled first */
  11448. ibx_assert_pch_refclk_enabled(dev_priv);
  11449. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11450. /* Wait for the clocks to stabilize. */
  11451. POSTING_READ(PCH_DPLL(pll->id));
  11452. udelay(150);
  11453. /* The pixel multiplier can only be updated once the
  11454. * DPLL is enabled and the clocks are stable.
  11455. *
  11456. * So write it again.
  11457. */
  11458. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11459. POSTING_READ(PCH_DPLL(pll->id));
  11460. udelay(200);
  11461. }
  11462. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11463. struct intel_shared_dpll *pll)
  11464. {
  11465. struct drm_device *dev = dev_priv->dev;
  11466. struct intel_crtc *crtc;
  11467. /* Make sure no transcoder isn't still depending on us. */
  11468. for_each_intel_crtc(dev, crtc) {
  11469. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11470. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11471. }
  11472. I915_WRITE(PCH_DPLL(pll->id), 0);
  11473. POSTING_READ(PCH_DPLL(pll->id));
  11474. udelay(200);
  11475. }
  11476. static char *ibx_pch_dpll_names[] = {
  11477. "PCH DPLL A",
  11478. "PCH DPLL B",
  11479. };
  11480. static void ibx_pch_dpll_init(struct drm_device *dev)
  11481. {
  11482. struct drm_i915_private *dev_priv = dev->dev_private;
  11483. int i;
  11484. dev_priv->num_shared_dpll = 2;
  11485. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11486. dev_priv->shared_dplls[i].id = i;
  11487. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11488. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11489. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11490. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11491. dev_priv->shared_dplls[i].get_hw_state =
  11492. ibx_pch_dpll_get_hw_state;
  11493. }
  11494. }
  11495. static void intel_shared_dpll_init(struct drm_device *dev)
  11496. {
  11497. struct drm_i915_private *dev_priv = dev->dev_private;
  11498. if (HAS_DDI(dev))
  11499. intel_ddi_pll_init(dev);
  11500. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11501. ibx_pch_dpll_init(dev);
  11502. else
  11503. dev_priv->num_shared_dpll = 0;
  11504. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11505. }
  11506. /**
  11507. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11508. * @plane: drm plane to prepare for
  11509. * @fb: framebuffer to prepare for presentation
  11510. *
  11511. * Prepares a framebuffer for usage on a display plane. Generally this
  11512. * involves pinning the underlying object and updating the frontbuffer tracking
  11513. * bits. Some older platforms need special physical address handling for
  11514. * cursor planes.
  11515. *
  11516. * Must be called with struct_mutex held.
  11517. *
  11518. * Returns 0 on success, negative error code on failure.
  11519. */
  11520. int
  11521. intel_prepare_plane_fb(struct drm_plane *plane,
  11522. const struct drm_plane_state *new_state)
  11523. {
  11524. struct drm_device *dev = plane->dev;
  11525. struct drm_framebuffer *fb = new_state->fb;
  11526. struct intel_plane *intel_plane = to_intel_plane(plane);
  11527. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11528. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11529. int ret = 0;
  11530. if (!obj && !old_obj)
  11531. return 0;
  11532. if (old_obj) {
  11533. struct drm_crtc_state *crtc_state =
  11534. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11535. /* Big Hammer, we also need to ensure that any pending
  11536. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11537. * current scanout is retired before unpinning the old
  11538. * framebuffer. Note that we rely on userspace rendering
  11539. * into the buffer attached to the pipe they are waiting
  11540. * on. If not, userspace generates a GPU hang with IPEHR
  11541. * point to the MI_WAIT_FOR_EVENT.
  11542. *
  11543. * This should only fail upon a hung GPU, in which case we
  11544. * can safely continue.
  11545. */
  11546. if (needs_modeset(crtc_state))
  11547. ret = i915_gem_object_wait_rendering(old_obj, true);
  11548. /* Swallow -EIO errors to allow updates during hw lockup. */
  11549. if (ret && ret != -EIO)
  11550. return ret;
  11551. }
  11552. /* For framebuffer backed by dmabuf, wait for fence */
  11553. if (obj && obj->base.dma_buf) {
  11554. long lret;
  11555. lret = reservation_object_wait_timeout_rcu(obj->base.dma_buf->resv,
  11556. false, true,
  11557. MAX_SCHEDULE_TIMEOUT);
  11558. if (lret == -ERESTARTSYS)
  11559. return lret;
  11560. WARN(lret < 0, "waiting returns %li\n", lret);
  11561. }
  11562. if (!obj) {
  11563. ret = 0;
  11564. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11565. INTEL_INFO(dev)->cursor_needs_physical) {
  11566. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11567. ret = i915_gem_object_attach_phys(obj, align);
  11568. if (ret)
  11569. DRM_DEBUG_KMS("failed to attach phys object\n");
  11570. } else {
  11571. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
  11572. }
  11573. if (ret == 0) {
  11574. if (obj) {
  11575. struct intel_plane_state *plane_state =
  11576. to_intel_plane_state(new_state);
  11577. i915_gem_request_assign(&plane_state->wait_req,
  11578. obj->last_write_req);
  11579. }
  11580. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11581. }
  11582. return ret;
  11583. }
  11584. /**
  11585. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11586. * @plane: drm plane to clean up for
  11587. * @fb: old framebuffer that was on plane
  11588. *
  11589. * Cleans up a framebuffer that has just been removed from a plane.
  11590. *
  11591. * Must be called with struct_mutex held.
  11592. */
  11593. void
  11594. intel_cleanup_plane_fb(struct drm_plane *plane,
  11595. const struct drm_plane_state *old_state)
  11596. {
  11597. struct drm_device *dev = plane->dev;
  11598. struct intel_plane *intel_plane = to_intel_plane(plane);
  11599. struct intel_plane_state *old_intel_state;
  11600. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11601. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11602. old_intel_state = to_intel_plane_state(old_state);
  11603. if (!obj && !old_obj)
  11604. return;
  11605. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11606. !INTEL_INFO(dev)->cursor_needs_physical))
  11607. intel_unpin_fb_obj(old_state->fb, old_state);
  11608. /* prepare_fb aborted? */
  11609. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11610. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11611. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11612. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11613. }
  11614. int
  11615. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11616. {
  11617. int max_scale;
  11618. struct drm_device *dev;
  11619. struct drm_i915_private *dev_priv;
  11620. int crtc_clock, cdclk;
  11621. if (!intel_crtc || !crtc_state->base.enable)
  11622. return DRM_PLANE_HELPER_NO_SCALING;
  11623. dev = intel_crtc->base.dev;
  11624. dev_priv = dev->dev_private;
  11625. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11626. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11627. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11628. return DRM_PLANE_HELPER_NO_SCALING;
  11629. /*
  11630. * skl max scale is lower of:
  11631. * close to 3 but not 3, -1 is for that purpose
  11632. * or
  11633. * cdclk/crtc_clock
  11634. */
  11635. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11636. return max_scale;
  11637. }
  11638. static int
  11639. intel_check_primary_plane(struct drm_plane *plane,
  11640. struct intel_crtc_state *crtc_state,
  11641. struct intel_plane_state *state)
  11642. {
  11643. struct drm_crtc *crtc = state->base.crtc;
  11644. struct drm_framebuffer *fb = state->base.fb;
  11645. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11646. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11647. bool can_position = false;
  11648. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11649. /* use scaler when colorkey is not required */
  11650. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11651. min_scale = 1;
  11652. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11653. }
  11654. can_position = true;
  11655. }
  11656. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11657. &state->dst, &state->clip,
  11658. min_scale, max_scale,
  11659. can_position, true,
  11660. &state->visible);
  11661. }
  11662. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11663. struct drm_crtc_state *old_crtc_state)
  11664. {
  11665. struct drm_device *dev = crtc->dev;
  11666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11667. struct intel_crtc_state *old_intel_state =
  11668. to_intel_crtc_state(old_crtc_state);
  11669. bool modeset = needs_modeset(crtc->state);
  11670. /* Perform vblank evasion around commit operation */
  11671. intel_pipe_update_start(intel_crtc);
  11672. if (modeset)
  11673. return;
  11674. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11675. intel_update_pipe_config(intel_crtc, old_intel_state);
  11676. else if (INTEL_INFO(dev)->gen >= 9)
  11677. skl_detach_scalers(intel_crtc);
  11678. }
  11679. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11680. struct drm_crtc_state *old_crtc_state)
  11681. {
  11682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11683. intel_pipe_update_end(intel_crtc);
  11684. }
  11685. /**
  11686. * intel_plane_destroy - destroy a plane
  11687. * @plane: plane to destroy
  11688. *
  11689. * Common destruction function for all types of planes (primary, cursor,
  11690. * sprite).
  11691. */
  11692. void intel_plane_destroy(struct drm_plane *plane)
  11693. {
  11694. struct intel_plane *intel_plane = to_intel_plane(plane);
  11695. drm_plane_cleanup(plane);
  11696. kfree(intel_plane);
  11697. }
  11698. const struct drm_plane_funcs intel_plane_funcs = {
  11699. .update_plane = drm_atomic_helper_update_plane,
  11700. .disable_plane = drm_atomic_helper_disable_plane,
  11701. .destroy = intel_plane_destroy,
  11702. .set_property = drm_atomic_helper_plane_set_property,
  11703. .atomic_get_property = intel_plane_atomic_get_property,
  11704. .atomic_set_property = intel_plane_atomic_set_property,
  11705. .atomic_duplicate_state = intel_plane_duplicate_state,
  11706. .atomic_destroy_state = intel_plane_destroy_state,
  11707. };
  11708. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11709. int pipe)
  11710. {
  11711. struct intel_plane *primary;
  11712. struct intel_plane_state *state;
  11713. const uint32_t *intel_primary_formats;
  11714. unsigned int num_formats;
  11715. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11716. if (primary == NULL)
  11717. return NULL;
  11718. state = intel_create_plane_state(&primary->base);
  11719. if (!state) {
  11720. kfree(primary);
  11721. return NULL;
  11722. }
  11723. primary->base.state = &state->base;
  11724. primary->can_scale = false;
  11725. primary->max_downscale = 1;
  11726. if (INTEL_INFO(dev)->gen >= 9) {
  11727. primary->can_scale = true;
  11728. state->scaler_id = -1;
  11729. }
  11730. primary->pipe = pipe;
  11731. primary->plane = pipe;
  11732. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11733. primary->check_plane = intel_check_primary_plane;
  11734. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11735. primary->plane = !pipe;
  11736. if (INTEL_INFO(dev)->gen >= 9) {
  11737. intel_primary_formats = skl_primary_formats;
  11738. num_formats = ARRAY_SIZE(skl_primary_formats);
  11739. primary->update_plane = skylake_update_primary_plane;
  11740. primary->disable_plane = skylake_disable_primary_plane;
  11741. } else if (HAS_PCH_SPLIT(dev)) {
  11742. intel_primary_formats = i965_primary_formats;
  11743. num_formats = ARRAY_SIZE(i965_primary_formats);
  11744. primary->update_plane = ironlake_update_primary_plane;
  11745. primary->disable_plane = i9xx_disable_primary_plane;
  11746. } else if (INTEL_INFO(dev)->gen >= 4) {
  11747. intel_primary_formats = i965_primary_formats;
  11748. num_formats = ARRAY_SIZE(i965_primary_formats);
  11749. primary->update_plane = i9xx_update_primary_plane;
  11750. primary->disable_plane = i9xx_disable_primary_plane;
  11751. } else {
  11752. intel_primary_formats = i8xx_primary_formats;
  11753. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11754. primary->update_plane = i9xx_update_primary_plane;
  11755. primary->disable_plane = i9xx_disable_primary_plane;
  11756. }
  11757. drm_universal_plane_init(dev, &primary->base, 0,
  11758. &intel_plane_funcs,
  11759. intel_primary_formats, num_formats,
  11760. DRM_PLANE_TYPE_PRIMARY, NULL);
  11761. if (INTEL_INFO(dev)->gen >= 4)
  11762. intel_create_rotation_property(dev, primary);
  11763. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11764. return &primary->base;
  11765. }
  11766. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11767. {
  11768. if (!dev->mode_config.rotation_property) {
  11769. unsigned long flags = BIT(DRM_ROTATE_0) |
  11770. BIT(DRM_ROTATE_180);
  11771. if (INTEL_INFO(dev)->gen >= 9)
  11772. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11773. dev->mode_config.rotation_property =
  11774. drm_mode_create_rotation_property(dev, flags);
  11775. }
  11776. if (dev->mode_config.rotation_property)
  11777. drm_object_attach_property(&plane->base.base,
  11778. dev->mode_config.rotation_property,
  11779. plane->base.state->rotation);
  11780. }
  11781. static int
  11782. intel_check_cursor_plane(struct drm_plane *plane,
  11783. struct intel_crtc_state *crtc_state,
  11784. struct intel_plane_state *state)
  11785. {
  11786. struct drm_crtc *crtc = crtc_state->base.crtc;
  11787. struct drm_framebuffer *fb = state->base.fb;
  11788. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11789. enum pipe pipe = to_intel_plane(plane)->pipe;
  11790. unsigned stride;
  11791. int ret;
  11792. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11793. &state->dst, &state->clip,
  11794. DRM_PLANE_HELPER_NO_SCALING,
  11795. DRM_PLANE_HELPER_NO_SCALING,
  11796. true, true, &state->visible);
  11797. if (ret)
  11798. return ret;
  11799. /* if we want to turn off the cursor ignore width and height */
  11800. if (!obj)
  11801. return 0;
  11802. /* Check for which cursor types we support */
  11803. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11804. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11805. state->base.crtc_w, state->base.crtc_h);
  11806. return -EINVAL;
  11807. }
  11808. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11809. if (obj->base.size < stride * state->base.crtc_h) {
  11810. DRM_DEBUG_KMS("buffer is too small\n");
  11811. return -ENOMEM;
  11812. }
  11813. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11814. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11815. return -EINVAL;
  11816. }
  11817. /*
  11818. * There's something wrong with the cursor on CHV pipe C.
  11819. * If it straddles the left edge of the screen then
  11820. * moving it away from the edge or disabling it often
  11821. * results in a pipe underrun, and often that can lead to
  11822. * dead pipe (constant underrun reported, and it scans
  11823. * out just a solid color). To recover from that, the
  11824. * display power well must be turned off and on again.
  11825. * Refuse the put the cursor into that compromised position.
  11826. */
  11827. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11828. state->visible && state->base.crtc_x < 0) {
  11829. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11830. return -EINVAL;
  11831. }
  11832. return 0;
  11833. }
  11834. static void
  11835. intel_disable_cursor_plane(struct drm_plane *plane,
  11836. struct drm_crtc *crtc)
  11837. {
  11838. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11839. intel_crtc->cursor_addr = 0;
  11840. intel_crtc_update_cursor(crtc, NULL);
  11841. }
  11842. static void
  11843. intel_update_cursor_plane(struct drm_plane *plane,
  11844. const struct intel_crtc_state *crtc_state,
  11845. const struct intel_plane_state *state)
  11846. {
  11847. struct drm_crtc *crtc = crtc_state->base.crtc;
  11848. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11849. struct drm_device *dev = plane->dev;
  11850. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11851. uint32_t addr;
  11852. if (!obj)
  11853. addr = 0;
  11854. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11855. addr = i915_gem_obj_ggtt_offset(obj);
  11856. else
  11857. addr = obj->phys_handle->busaddr;
  11858. intel_crtc->cursor_addr = addr;
  11859. intel_crtc_update_cursor(crtc, state);
  11860. }
  11861. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11862. int pipe)
  11863. {
  11864. struct intel_plane *cursor;
  11865. struct intel_plane_state *state;
  11866. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11867. if (cursor == NULL)
  11868. return NULL;
  11869. state = intel_create_plane_state(&cursor->base);
  11870. if (!state) {
  11871. kfree(cursor);
  11872. return NULL;
  11873. }
  11874. cursor->base.state = &state->base;
  11875. cursor->can_scale = false;
  11876. cursor->max_downscale = 1;
  11877. cursor->pipe = pipe;
  11878. cursor->plane = pipe;
  11879. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11880. cursor->check_plane = intel_check_cursor_plane;
  11881. cursor->update_plane = intel_update_cursor_plane;
  11882. cursor->disable_plane = intel_disable_cursor_plane;
  11883. drm_universal_plane_init(dev, &cursor->base, 0,
  11884. &intel_plane_funcs,
  11885. intel_cursor_formats,
  11886. ARRAY_SIZE(intel_cursor_formats),
  11887. DRM_PLANE_TYPE_CURSOR, NULL);
  11888. if (INTEL_INFO(dev)->gen >= 4) {
  11889. if (!dev->mode_config.rotation_property)
  11890. dev->mode_config.rotation_property =
  11891. drm_mode_create_rotation_property(dev,
  11892. BIT(DRM_ROTATE_0) |
  11893. BIT(DRM_ROTATE_180));
  11894. if (dev->mode_config.rotation_property)
  11895. drm_object_attach_property(&cursor->base.base,
  11896. dev->mode_config.rotation_property,
  11897. state->base.rotation);
  11898. }
  11899. if (INTEL_INFO(dev)->gen >=9)
  11900. state->scaler_id = -1;
  11901. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11902. return &cursor->base;
  11903. }
  11904. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11905. struct intel_crtc_state *crtc_state)
  11906. {
  11907. int i;
  11908. struct intel_scaler *intel_scaler;
  11909. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11910. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11911. intel_scaler = &scaler_state->scalers[i];
  11912. intel_scaler->in_use = 0;
  11913. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11914. }
  11915. scaler_state->scaler_id = -1;
  11916. }
  11917. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11918. {
  11919. struct drm_i915_private *dev_priv = dev->dev_private;
  11920. struct intel_crtc *intel_crtc;
  11921. struct intel_crtc_state *crtc_state = NULL;
  11922. struct drm_plane *primary = NULL;
  11923. struct drm_plane *cursor = NULL;
  11924. int i, ret;
  11925. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11926. if (intel_crtc == NULL)
  11927. return;
  11928. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11929. if (!crtc_state)
  11930. goto fail;
  11931. intel_crtc->config = crtc_state;
  11932. intel_crtc->base.state = &crtc_state->base;
  11933. crtc_state->base.crtc = &intel_crtc->base;
  11934. /* initialize shared scalers */
  11935. if (INTEL_INFO(dev)->gen >= 9) {
  11936. if (pipe == PIPE_C)
  11937. intel_crtc->num_scalers = 1;
  11938. else
  11939. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11940. skl_init_scalers(dev, intel_crtc, crtc_state);
  11941. }
  11942. primary = intel_primary_plane_create(dev, pipe);
  11943. if (!primary)
  11944. goto fail;
  11945. cursor = intel_cursor_plane_create(dev, pipe);
  11946. if (!cursor)
  11947. goto fail;
  11948. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11949. cursor, &intel_crtc_funcs, NULL);
  11950. if (ret)
  11951. goto fail;
  11952. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11953. for (i = 0; i < 256; i++) {
  11954. intel_crtc->lut_r[i] = i;
  11955. intel_crtc->lut_g[i] = i;
  11956. intel_crtc->lut_b[i] = i;
  11957. }
  11958. /*
  11959. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11960. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11961. */
  11962. intel_crtc->pipe = pipe;
  11963. intel_crtc->plane = pipe;
  11964. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11965. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11966. intel_crtc->plane = !pipe;
  11967. }
  11968. intel_crtc->cursor_base = ~0;
  11969. intel_crtc->cursor_cntl = ~0;
  11970. intel_crtc->cursor_size = ~0;
  11971. intel_crtc->wm.cxsr_allowed = true;
  11972. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11973. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11974. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11975. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11976. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11977. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11978. return;
  11979. fail:
  11980. if (primary)
  11981. drm_plane_cleanup(primary);
  11982. if (cursor)
  11983. drm_plane_cleanup(cursor);
  11984. kfree(crtc_state);
  11985. kfree(intel_crtc);
  11986. }
  11987. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11988. {
  11989. struct drm_encoder *encoder = connector->base.encoder;
  11990. struct drm_device *dev = connector->base.dev;
  11991. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11992. if (!encoder || WARN_ON(!encoder->crtc))
  11993. return INVALID_PIPE;
  11994. return to_intel_crtc(encoder->crtc)->pipe;
  11995. }
  11996. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11997. struct drm_file *file)
  11998. {
  11999. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12000. struct drm_crtc *drmmode_crtc;
  12001. struct intel_crtc *crtc;
  12002. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12003. if (!drmmode_crtc) {
  12004. DRM_ERROR("no such CRTC id\n");
  12005. return -ENOENT;
  12006. }
  12007. crtc = to_intel_crtc(drmmode_crtc);
  12008. pipe_from_crtc_id->pipe = crtc->pipe;
  12009. return 0;
  12010. }
  12011. static int intel_encoder_clones(struct intel_encoder *encoder)
  12012. {
  12013. struct drm_device *dev = encoder->base.dev;
  12014. struct intel_encoder *source_encoder;
  12015. int index_mask = 0;
  12016. int entry = 0;
  12017. for_each_intel_encoder(dev, source_encoder) {
  12018. if (encoders_cloneable(encoder, source_encoder))
  12019. index_mask |= (1 << entry);
  12020. entry++;
  12021. }
  12022. return index_mask;
  12023. }
  12024. static bool has_edp_a(struct drm_device *dev)
  12025. {
  12026. struct drm_i915_private *dev_priv = dev->dev_private;
  12027. if (!IS_MOBILE(dev))
  12028. return false;
  12029. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12030. return false;
  12031. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12032. return false;
  12033. return true;
  12034. }
  12035. static bool intel_crt_present(struct drm_device *dev)
  12036. {
  12037. struct drm_i915_private *dev_priv = dev->dev_private;
  12038. if (INTEL_INFO(dev)->gen >= 9)
  12039. return false;
  12040. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  12041. return false;
  12042. if (IS_CHERRYVIEW(dev))
  12043. return false;
  12044. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12045. return false;
  12046. /* DDI E can't be used if DDI A requires 4 lanes */
  12047. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12048. return false;
  12049. if (!dev_priv->vbt.int_crt_support)
  12050. return false;
  12051. return true;
  12052. }
  12053. static void intel_setup_outputs(struct drm_device *dev)
  12054. {
  12055. struct drm_i915_private *dev_priv = dev->dev_private;
  12056. struct intel_encoder *encoder;
  12057. bool dpd_is_edp = false;
  12058. intel_lvds_init(dev);
  12059. if (intel_crt_present(dev))
  12060. intel_crt_init(dev);
  12061. if (IS_BROXTON(dev)) {
  12062. /*
  12063. * FIXME: Broxton doesn't support port detection via the
  12064. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12065. * detect the ports.
  12066. */
  12067. intel_ddi_init(dev, PORT_A);
  12068. intel_ddi_init(dev, PORT_B);
  12069. intel_ddi_init(dev, PORT_C);
  12070. } else if (HAS_DDI(dev)) {
  12071. int found;
  12072. /*
  12073. * Haswell uses DDI functions to detect digital outputs.
  12074. * On SKL pre-D0 the strap isn't connected, so we assume
  12075. * it's there.
  12076. */
  12077. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12078. /* WaIgnoreDDIAStrap: skl */
  12079. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12080. intel_ddi_init(dev, PORT_A);
  12081. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12082. * register */
  12083. found = I915_READ(SFUSE_STRAP);
  12084. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12085. intel_ddi_init(dev, PORT_B);
  12086. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12087. intel_ddi_init(dev, PORT_C);
  12088. if (found & SFUSE_STRAP_DDID_DETECTED)
  12089. intel_ddi_init(dev, PORT_D);
  12090. /*
  12091. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12092. */
  12093. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  12094. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12095. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12096. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12097. intel_ddi_init(dev, PORT_E);
  12098. } else if (HAS_PCH_SPLIT(dev)) {
  12099. int found;
  12100. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12101. if (has_edp_a(dev))
  12102. intel_dp_init(dev, DP_A, PORT_A);
  12103. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12104. /* PCH SDVOB multiplex with HDMIB */
  12105. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12106. if (!found)
  12107. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12108. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12109. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12110. }
  12111. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12112. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12113. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12114. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12115. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12116. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12117. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12118. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12119. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12120. /*
  12121. * The DP_DETECTED bit is the latched state of the DDC
  12122. * SDA pin at boot. However since eDP doesn't require DDC
  12123. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12124. * eDP ports may have been muxed to an alternate function.
  12125. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12126. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12127. * detect eDP ports.
  12128. */
  12129. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  12130. !intel_dp_is_edp(dev, PORT_B))
  12131. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  12132. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  12133. intel_dp_is_edp(dev, PORT_B))
  12134. intel_dp_init(dev, VLV_DP_B, PORT_B);
  12135. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  12136. !intel_dp_is_edp(dev, PORT_C))
  12137. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  12138. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  12139. intel_dp_is_edp(dev, PORT_C))
  12140. intel_dp_init(dev, VLV_DP_C, PORT_C);
  12141. if (IS_CHERRYVIEW(dev)) {
  12142. /* eDP not supported on port D, so don't check VBT */
  12143. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  12144. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  12145. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  12146. intel_dp_init(dev, CHV_DP_D, PORT_D);
  12147. }
  12148. intel_dsi_init(dev);
  12149. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  12150. bool found = false;
  12151. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12152. DRM_DEBUG_KMS("probing SDVOB\n");
  12153. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  12154. if (!found && IS_G4X(dev)) {
  12155. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12156. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12157. }
  12158. if (!found && IS_G4X(dev))
  12159. intel_dp_init(dev, DP_B, PORT_B);
  12160. }
  12161. /* Before G4X SDVOC doesn't have its own detect register */
  12162. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12163. DRM_DEBUG_KMS("probing SDVOC\n");
  12164. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  12165. }
  12166. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12167. if (IS_G4X(dev)) {
  12168. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12169. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12170. }
  12171. if (IS_G4X(dev))
  12172. intel_dp_init(dev, DP_C, PORT_C);
  12173. }
  12174. if (IS_G4X(dev) &&
  12175. (I915_READ(DP_D) & DP_DETECTED))
  12176. intel_dp_init(dev, DP_D, PORT_D);
  12177. } else if (IS_GEN2(dev))
  12178. intel_dvo_init(dev);
  12179. if (SUPPORTS_TV(dev))
  12180. intel_tv_init(dev);
  12181. intel_psr_init(dev);
  12182. for_each_intel_encoder(dev, encoder) {
  12183. encoder->base.possible_crtcs = encoder->crtc_mask;
  12184. encoder->base.possible_clones =
  12185. intel_encoder_clones(encoder);
  12186. }
  12187. intel_init_pch_refclk(dev);
  12188. drm_helper_move_panel_connectors_to_head(dev);
  12189. }
  12190. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12191. {
  12192. struct drm_device *dev = fb->dev;
  12193. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12194. drm_framebuffer_cleanup(fb);
  12195. mutex_lock(&dev->struct_mutex);
  12196. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12197. drm_gem_object_unreference(&intel_fb->obj->base);
  12198. mutex_unlock(&dev->struct_mutex);
  12199. kfree(intel_fb);
  12200. }
  12201. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12202. struct drm_file *file,
  12203. unsigned int *handle)
  12204. {
  12205. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12206. struct drm_i915_gem_object *obj = intel_fb->obj;
  12207. if (obj->userptr.mm) {
  12208. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  12209. return -EINVAL;
  12210. }
  12211. return drm_gem_handle_create(file, &obj->base, handle);
  12212. }
  12213. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12214. struct drm_file *file,
  12215. unsigned flags, unsigned color,
  12216. struct drm_clip_rect *clips,
  12217. unsigned num_clips)
  12218. {
  12219. struct drm_device *dev = fb->dev;
  12220. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12221. struct drm_i915_gem_object *obj = intel_fb->obj;
  12222. mutex_lock(&dev->struct_mutex);
  12223. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12224. mutex_unlock(&dev->struct_mutex);
  12225. return 0;
  12226. }
  12227. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12228. .destroy = intel_user_framebuffer_destroy,
  12229. .create_handle = intel_user_framebuffer_create_handle,
  12230. .dirty = intel_user_framebuffer_dirty,
  12231. };
  12232. static
  12233. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12234. uint32_t pixel_format)
  12235. {
  12236. u32 gen = INTEL_INFO(dev)->gen;
  12237. if (gen >= 9) {
  12238. int cpp = drm_format_plane_cpp(pixel_format, 0);
  12239. /* "The stride in bytes must not exceed the of the size of 8K
  12240. * pixels and 32K bytes."
  12241. */
  12242. return min(8192 * cpp, 32768);
  12243. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12244. return 32*1024;
  12245. } else if (gen >= 4) {
  12246. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12247. return 16*1024;
  12248. else
  12249. return 32*1024;
  12250. } else if (gen >= 3) {
  12251. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12252. return 8*1024;
  12253. else
  12254. return 16*1024;
  12255. } else {
  12256. /* XXX DSPC is limited to 4k tiled */
  12257. return 8*1024;
  12258. }
  12259. }
  12260. static int intel_framebuffer_init(struct drm_device *dev,
  12261. struct intel_framebuffer *intel_fb,
  12262. struct drm_mode_fb_cmd2 *mode_cmd,
  12263. struct drm_i915_gem_object *obj)
  12264. {
  12265. struct drm_i915_private *dev_priv = to_i915(dev);
  12266. unsigned int aligned_height;
  12267. int ret;
  12268. u32 pitch_limit, stride_alignment;
  12269. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12270. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12271. /* Enforce that fb modifier and tiling mode match, but only for
  12272. * X-tiled. This is needed for FBC. */
  12273. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12274. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12275. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12276. return -EINVAL;
  12277. }
  12278. } else {
  12279. if (obj->tiling_mode == I915_TILING_X)
  12280. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12281. else if (obj->tiling_mode == I915_TILING_Y) {
  12282. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12283. return -EINVAL;
  12284. }
  12285. }
  12286. /* Passed in modifier sanity checking. */
  12287. switch (mode_cmd->modifier[0]) {
  12288. case I915_FORMAT_MOD_Y_TILED:
  12289. case I915_FORMAT_MOD_Yf_TILED:
  12290. if (INTEL_INFO(dev)->gen < 9) {
  12291. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12292. mode_cmd->modifier[0]);
  12293. return -EINVAL;
  12294. }
  12295. case DRM_FORMAT_MOD_NONE:
  12296. case I915_FORMAT_MOD_X_TILED:
  12297. break;
  12298. default:
  12299. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12300. mode_cmd->modifier[0]);
  12301. return -EINVAL;
  12302. }
  12303. stride_alignment = intel_fb_stride_alignment(dev_priv,
  12304. mode_cmd->modifier[0],
  12305. mode_cmd->pixel_format);
  12306. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12307. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12308. mode_cmd->pitches[0], stride_alignment);
  12309. return -EINVAL;
  12310. }
  12311. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12312. mode_cmd->pixel_format);
  12313. if (mode_cmd->pitches[0] > pitch_limit) {
  12314. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12315. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12316. "tiled" : "linear",
  12317. mode_cmd->pitches[0], pitch_limit);
  12318. return -EINVAL;
  12319. }
  12320. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12321. mode_cmd->pitches[0] != obj->stride) {
  12322. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12323. mode_cmd->pitches[0], obj->stride);
  12324. return -EINVAL;
  12325. }
  12326. /* Reject formats not supported by any plane early. */
  12327. switch (mode_cmd->pixel_format) {
  12328. case DRM_FORMAT_C8:
  12329. case DRM_FORMAT_RGB565:
  12330. case DRM_FORMAT_XRGB8888:
  12331. case DRM_FORMAT_ARGB8888:
  12332. break;
  12333. case DRM_FORMAT_XRGB1555:
  12334. if (INTEL_INFO(dev)->gen > 3) {
  12335. DRM_DEBUG("unsupported pixel format: %s\n",
  12336. drm_get_format_name(mode_cmd->pixel_format));
  12337. return -EINVAL;
  12338. }
  12339. break;
  12340. case DRM_FORMAT_ABGR8888:
  12341. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  12342. INTEL_INFO(dev)->gen < 9) {
  12343. DRM_DEBUG("unsupported pixel format: %s\n",
  12344. drm_get_format_name(mode_cmd->pixel_format));
  12345. return -EINVAL;
  12346. }
  12347. break;
  12348. case DRM_FORMAT_XBGR8888:
  12349. case DRM_FORMAT_XRGB2101010:
  12350. case DRM_FORMAT_XBGR2101010:
  12351. if (INTEL_INFO(dev)->gen < 4) {
  12352. DRM_DEBUG("unsupported pixel format: %s\n",
  12353. drm_get_format_name(mode_cmd->pixel_format));
  12354. return -EINVAL;
  12355. }
  12356. break;
  12357. case DRM_FORMAT_ABGR2101010:
  12358. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  12359. DRM_DEBUG("unsupported pixel format: %s\n",
  12360. drm_get_format_name(mode_cmd->pixel_format));
  12361. return -EINVAL;
  12362. }
  12363. break;
  12364. case DRM_FORMAT_YUYV:
  12365. case DRM_FORMAT_UYVY:
  12366. case DRM_FORMAT_YVYU:
  12367. case DRM_FORMAT_VYUY:
  12368. if (INTEL_INFO(dev)->gen < 5) {
  12369. DRM_DEBUG("unsupported pixel format: %s\n",
  12370. drm_get_format_name(mode_cmd->pixel_format));
  12371. return -EINVAL;
  12372. }
  12373. break;
  12374. default:
  12375. DRM_DEBUG("unsupported pixel format: %s\n",
  12376. drm_get_format_name(mode_cmd->pixel_format));
  12377. return -EINVAL;
  12378. }
  12379. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12380. if (mode_cmd->offsets[0] != 0)
  12381. return -EINVAL;
  12382. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12383. mode_cmd->pixel_format,
  12384. mode_cmd->modifier[0]);
  12385. /* FIXME drm helper for size checks (especially planar formats)? */
  12386. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12387. return -EINVAL;
  12388. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12389. intel_fb->obj = obj;
  12390. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12391. if (ret) {
  12392. DRM_ERROR("framebuffer init failed %d\n", ret);
  12393. return ret;
  12394. }
  12395. intel_fb->obj->framebuffer_references++;
  12396. return 0;
  12397. }
  12398. static struct drm_framebuffer *
  12399. intel_user_framebuffer_create(struct drm_device *dev,
  12400. struct drm_file *filp,
  12401. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12402. {
  12403. struct drm_framebuffer *fb;
  12404. struct drm_i915_gem_object *obj;
  12405. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12406. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12407. mode_cmd.handles[0]));
  12408. if (&obj->base == NULL)
  12409. return ERR_PTR(-ENOENT);
  12410. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12411. if (IS_ERR(fb))
  12412. drm_gem_object_unreference_unlocked(&obj->base);
  12413. return fb;
  12414. }
  12415. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12416. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12417. {
  12418. }
  12419. #endif
  12420. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12421. .fb_create = intel_user_framebuffer_create,
  12422. .output_poll_changed = intel_fbdev_output_poll_changed,
  12423. .atomic_check = intel_atomic_check,
  12424. .atomic_commit = intel_atomic_commit,
  12425. .atomic_state_alloc = intel_atomic_state_alloc,
  12426. .atomic_state_clear = intel_atomic_state_clear,
  12427. };
  12428. /* Set up chip specific display functions */
  12429. static void intel_init_display(struct drm_device *dev)
  12430. {
  12431. struct drm_i915_private *dev_priv = dev->dev_private;
  12432. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12433. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12434. else if (IS_CHERRYVIEW(dev))
  12435. dev_priv->display.find_dpll = chv_find_best_dpll;
  12436. else if (IS_VALLEYVIEW(dev))
  12437. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12438. else if (IS_PINEVIEW(dev))
  12439. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12440. else
  12441. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12442. if (INTEL_INFO(dev)->gen >= 9) {
  12443. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12444. dev_priv->display.get_initial_plane_config =
  12445. skylake_get_initial_plane_config;
  12446. dev_priv->display.crtc_compute_clock =
  12447. haswell_crtc_compute_clock;
  12448. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12449. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12450. } else if (HAS_DDI(dev)) {
  12451. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12452. dev_priv->display.get_initial_plane_config =
  12453. ironlake_get_initial_plane_config;
  12454. dev_priv->display.crtc_compute_clock =
  12455. haswell_crtc_compute_clock;
  12456. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12457. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12458. } else if (HAS_PCH_SPLIT(dev)) {
  12459. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12460. dev_priv->display.get_initial_plane_config =
  12461. ironlake_get_initial_plane_config;
  12462. dev_priv->display.crtc_compute_clock =
  12463. ironlake_crtc_compute_clock;
  12464. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12465. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12466. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12467. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12468. dev_priv->display.get_initial_plane_config =
  12469. i9xx_get_initial_plane_config;
  12470. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12471. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12472. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12473. } else {
  12474. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12475. dev_priv->display.get_initial_plane_config =
  12476. i9xx_get_initial_plane_config;
  12477. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12478. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12479. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12480. }
  12481. /* Returns the core display clock speed */
  12482. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12483. dev_priv->display.get_display_clock_speed =
  12484. skylake_get_display_clock_speed;
  12485. else if (IS_BROXTON(dev))
  12486. dev_priv->display.get_display_clock_speed =
  12487. broxton_get_display_clock_speed;
  12488. else if (IS_BROADWELL(dev))
  12489. dev_priv->display.get_display_clock_speed =
  12490. broadwell_get_display_clock_speed;
  12491. else if (IS_HASWELL(dev))
  12492. dev_priv->display.get_display_clock_speed =
  12493. haswell_get_display_clock_speed;
  12494. else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  12495. dev_priv->display.get_display_clock_speed =
  12496. valleyview_get_display_clock_speed;
  12497. else if (IS_GEN5(dev))
  12498. dev_priv->display.get_display_clock_speed =
  12499. ilk_get_display_clock_speed;
  12500. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12501. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12502. dev_priv->display.get_display_clock_speed =
  12503. i945_get_display_clock_speed;
  12504. else if (IS_GM45(dev))
  12505. dev_priv->display.get_display_clock_speed =
  12506. gm45_get_display_clock_speed;
  12507. else if (IS_CRESTLINE(dev))
  12508. dev_priv->display.get_display_clock_speed =
  12509. i965gm_get_display_clock_speed;
  12510. else if (IS_PINEVIEW(dev))
  12511. dev_priv->display.get_display_clock_speed =
  12512. pnv_get_display_clock_speed;
  12513. else if (IS_G33(dev) || IS_G4X(dev))
  12514. dev_priv->display.get_display_clock_speed =
  12515. g33_get_display_clock_speed;
  12516. else if (IS_I915G(dev))
  12517. dev_priv->display.get_display_clock_speed =
  12518. i915_get_display_clock_speed;
  12519. else if (IS_I945GM(dev) || IS_845G(dev))
  12520. dev_priv->display.get_display_clock_speed =
  12521. i9xx_misc_get_display_clock_speed;
  12522. else if (IS_I915GM(dev))
  12523. dev_priv->display.get_display_clock_speed =
  12524. i915gm_get_display_clock_speed;
  12525. else if (IS_I865G(dev))
  12526. dev_priv->display.get_display_clock_speed =
  12527. i865_get_display_clock_speed;
  12528. else if (IS_I85X(dev))
  12529. dev_priv->display.get_display_clock_speed =
  12530. i85x_get_display_clock_speed;
  12531. else { /* 830 */
  12532. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12533. dev_priv->display.get_display_clock_speed =
  12534. i830_get_display_clock_speed;
  12535. }
  12536. if (IS_GEN5(dev)) {
  12537. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12538. } else if (IS_GEN6(dev)) {
  12539. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12540. } else if (IS_IVYBRIDGE(dev)) {
  12541. /* FIXME: detect B0+ stepping and use auto training */
  12542. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12543. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12544. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12545. if (IS_BROADWELL(dev)) {
  12546. dev_priv->display.modeset_commit_cdclk =
  12547. broadwell_modeset_commit_cdclk;
  12548. dev_priv->display.modeset_calc_cdclk =
  12549. broadwell_modeset_calc_cdclk;
  12550. }
  12551. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  12552. dev_priv->display.modeset_commit_cdclk =
  12553. valleyview_modeset_commit_cdclk;
  12554. dev_priv->display.modeset_calc_cdclk =
  12555. valleyview_modeset_calc_cdclk;
  12556. } else if (IS_BROXTON(dev)) {
  12557. dev_priv->display.modeset_commit_cdclk =
  12558. broxton_modeset_commit_cdclk;
  12559. dev_priv->display.modeset_calc_cdclk =
  12560. broxton_modeset_calc_cdclk;
  12561. }
  12562. switch (INTEL_INFO(dev)->gen) {
  12563. case 2:
  12564. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12565. break;
  12566. case 3:
  12567. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12568. break;
  12569. case 4:
  12570. case 5:
  12571. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12572. break;
  12573. case 6:
  12574. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12575. break;
  12576. case 7:
  12577. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12578. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12579. break;
  12580. case 9:
  12581. /* Drop through - unsupported since execlist only. */
  12582. default:
  12583. /* Default just returns -ENODEV to indicate unsupported */
  12584. dev_priv->display.queue_flip = intel_default_queue_flip;
  12585. }
  12586. mutex_init(&dev_priv->pps_mutex);
  12587. }
  12588. /*
  12589. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12590. * resume, or other times. This quirk makes sure that's the case for
  12591. * affected systems.
  12592. */
  12593. static void quirk_pipea_force(struct drm_device *dev)
  12594. {
  12595. struct drm_i915_private *dev_priv = dev->dev_private;
  12596. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12597. DRM_INFO("applying pipe a force quirk\n");
  12598. }
  12599. static void quirk_pipeb_force(struct drm_device *dev)
  12600. {
  12601. struct drm_i915_private *dev_priv = dev->dev_private;
  12602. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12603. DRM_INFO("applying pipe b force quirk\n");
  12604. }
  12605. /*
  12606. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12607. */
  12608. static void quirk_ssc_force_disable(struct drm_device *dev)
  12609. {
  12610. struct drm_i915_private *dev_priv = dev->dev_private;
  12611. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12612. DRM_INFO("applying lvds SSC disable quirk\n");
  12613. }
  12614. /*
  12615. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12616. * brightness value
  12617. */
  12618. static void quirk_invert_brightness(struct drm_device *dev)
  12619. {
  12620. struct drm_i915_private *dev_priv = dev->dev_private;
  12621. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12622. DRM_INFO("applying inverted panel brightness quirk\n");
  12623. }
  12624. /* Some VBT's incorrectly indicate no backlight is present */
  12625. static void quirk_backlight_present(struct drm_device *dev)
  12626. {
  12627. struct drm_i915_private *dev_priv = dev->dev_private;
  12628. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12629. DRM_INFO("applying backlight present quirk\n");
  12630. }
  12631. struct intel_quirk {
  12632. int device;
  12633. int subsystem_vendor;
  12634. int subsystem_device;
  12635. void (*hook)(struct drm_device *dev);
  12636. };
  12637. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12638. struct intel_dmi_quirk {
  12639. void (*hook)(struct drm_device *dev);
  12640. const struct dmi_system_id (*dmi_id_list)[];
  12641. };
  12642. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12643. {
  12644. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12645. return 1;
  12646. }
  12647. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12648. {
  12649. .dmi_id_list = &(const struct dmi_system_id[]) {
  12650. {
  12651. .callback = intel_dmi_reverse_brightness,
  12652. .ident = "NCR Corporation",
  12653. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12654. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12655. },
  12656. },
  12657. { } /* terminating entry */
  12658. },
  12659. .hook = quirk_invert_brightness,
  12660. },
  12661. };
  12662. static struct intel_quirk intel_quirks[] = {
  12663. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12664. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12665. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12666. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12667. /* 830 needs to leave pipe A & dpll A up */
  12668. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12669. /* 830 needs to leave pipe B & dpll B up */
  12670. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12671. /* Lenovo U160 cannot use SSC on LVDS */
  12672. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12673. /* Sony Vaio Y cannot use SSC on LVDS */
  12674. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12675. /* Acer Aspire 5734Z must invert backlight brightness */
  12676. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12677. /* Acer/eMachines G725 */
  12678. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12679. /* Acer/eMachines e725 */
  12680. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12681. /* Acer/Packard Bell NCL20 */
  12682. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12683. /* Acer Aspire 4736Z */
  12684. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12685. /* Acer Aspire 5336 */
  12686. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12687. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12688. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12689. /* Acer C720 Chromebook (Core i3 4005U) */
  12690. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12691. /* Apple Macbook 2,1 (Core 2 T7400) */
  12692. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12693. /* Apple Macbook 4,1 */
  12694. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12695. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12696. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12697. /* HP Chromebook 14 (Celeron 2955U) */
  12698. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12699. /* Dell Chromebook 11 */
  12700. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12701. /* Dell Chromebook 11 (2015 version) */
  12702. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12703. };
  12704. static void intel_init_quirks(struct drm_device *dev)
  12705. {
  12706. struct pci_dev *d = dev->pdev;
  12707. int i;
  12708. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12709. struct intel_quirk *q = &intel_quirks[i];
  12710. if (d->device == q->device &&
  12711. (d->subsystem_vendor == q->subsystem_vendor ||
  12712. q->subsystem_vendor == PCI_ANY_ID) &&
  12713. (d->subsystem_device == q->subsystem_device ||
  12714. q->subsystem_device == PCI_ANY_ID))
  12715. q->hook(dev);
  12716. }
  12717. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12718. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12719. intel_dmi_quirks[i].hook(dev);
  12720. }
  12721. }
  12722. /* Disable the VGA plane that we never use */
  12723. static void i915_disable_vga(struct drm_device *dev)
  12724. {
  12725. struct drm_i915_private *dev_priv = dev->dev_private;
  12726. u8 sr1;
  12727. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12728. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12729. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12730. outb(SR01, VGA_SR_INDEX);
  12731. sr1 = inb(VGA_SR_DATA);
  12732. outb(sr1 | 1<<5, VGA_SR_DATA);
  12733. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12734. udelay(300);
  12735. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12736. POSTING_READ(vga_reg);
  12737. }
  12738. void intel_modeset_init_hw(struct drm_device *dev)
  12739. {
  12740. struct drm_i915_private *dev_priv = dev->dev_private;
  12741. intel_update_cdclk(dev);
  12742. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12743. intel_init_clock_gating(dev);
  12744. intel_enable_gt_powersave(dev);
  12745. }
  12746. /*
  12747. * Calculate what we think the watermarks should be for the state we've read
  12748. * out of the hardware and then immediately program those watermarks so that
  12749. * we ensure the hardware settings match our internal state.
  12750. *
  12751. * We can calculate what we think WM's should be by creating a duplicate of the
  12752. * current state (which was constructed during hardware readout) and running it
  12753. * through the atomic check code to calculate new watermark values in the
  12754. * state object.
  12755. */
  12756. static void sanitize_watermarks(struct drm_device *dev)
  12757. {
  12758. struct drm_i915_private *dev_priv = to_i915(dev);
  12759. struct drm_atomic_state *state;
  12760. struct drm_crtc *crtc;
  12761. struct drm_crtc_state *cstate;
  12762. struct drm_modeset_acquire_ctx ctx;
  12763. int ret;
  12764. int i;
  12765. /* Only supported on platforms that use atomic watermark design */
  12766. if (!dev_priv->display.program_watermarks)
  12767. return;
  12768. /*
  12769. * We need to hold connection_mutex before calling duplicate_state so
  12770. * that the connector loop is protected.
  12771. */
  12772. drm_modeset_acquire_init(&ctx, 0);
  12773. retry:
  12774. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12775. if (ret == -EDEADLK) {
  12776. drm_modeset_backoff(&ctx);
  12777. goto retry;
  12778. } else if (WARN_ON(ret)) {
  12779. goto fail;
  12780. }
  12781. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12782. if (WARN_ON(IS_ERR(state)))
  12783. goto fail;
  12784. ret = intel_atomic_check(dev, state);
  12785. if (ret) {
  12786. /*
  12787. * If we fail here, it means that the hardware appears to be
  12788. * programmed in a way that shouldn't be possible, given our
  12789. * understanding of watermark requirements. This might mean a
  12790. * mistake in the hardware readout code or a mistake in the
  12791. * watermark calculations for a given platform. Raise a WARN
  12792. * so that this is noticeable.
  12793. *
  12794. * If this actually happens, we'll have to just leave the
  12795. * BIOS-programmed watermarks untouched and hope for the best.
  12796. */
  12797. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12798. goto fail;
  12799. }
  12800. /* Write calculated watermark values back */
  12801. to_i915(dev)->wm.config = to_intel_atomic_state(state)->wm_config;
  12802. for_each_crtc_in_state(state, crtc, cstate, i) {
  12803. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12804. dev_priv->display.program_watermarks(cs);
  12805. }
  12806. drm_atomic_state_free(state);
  12807. fail:
  12808. drm_modeset_drop_locks(&ctx);
  12809. drm_modeset_acquire_fini(&ctx);
  12810. }
  12811. void intel_modeset_init(struct drm_device *dev)
  12812. {
  12813. struct drm_i915_private *dev_priv = dev->dev_private;
  12814. int sprite, ret;
  12815. enum pipe pipe;
  12816. struct intel_crtc *crtc;
  12817. drm_mode_config_init(dev);
  12818. dev->mode_config.min_width = 0;
  12819. dev->mode_config.min_height = 0;
  12820. dev->mode_config.preferred_depth = 24;
  12821. dev->mode_config.prefer_shadow = 1;
  12822. dev->mode_config.allow_fb_modifiers = true;
  12823. dev->mode_config.funcs = &intel_mode_funcs;
  12824. intel_init_quirks(dev);
  12825. intel_init_pm(dev);
  12826. if (INTEL_INFO(dev)->num_pipes == 0)
  12827. return;
  12828. /*
  12829. * There may be no VBT; and if the BIOS enabled SSC we can
  12830. * just keep using it to avoid unnecessary flicker. Whereas if the
  12831. * BIOS isn't using it, don't assume it will work even if the VBT
  12832. * indicates as much.
  12833. */
  12834. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12835. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12836. DREF_SSC1_ENABLE);
  12837. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12838. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12839. bios_lvds_use_ssc ? "en" : "dis",
  12840. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12841. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12842. }
  12843. }
  12844. intel_init_display(dev);
  12845. intel_init_audio(dev);
  12846. if (IS_GEN2(dev)) {
  12847. dev->mode_config.max_width = 2048;
  12848. dev->mode_config.max_height = 2048;
  12849. } else if (IS_GEN3(dev)) {
  12850. dev->mode_config.max_width = 4096;
  12851. dev->mode_config.max_height = 4096;
  12852. } else {
  12853. dev->mode_config.max_width = 8192;
  12854. dev->mode_config.max_height = 8192;
  12855. }
  12856. if (IS_845G(dev) || IS_I865G(dev)) {
  12857. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12858. dev->mode_config.cursor_height = 1023;
  12859. } else if (IS_GEN2(dev)) {
  12860. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12861. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12862. } else {
  12863. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12864. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12865. }
  12866. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12867. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12868. INTEL_INFO(dev)->num_pipes,
  12869. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12870. for_each_pipe(dev_priv, pipe) {
  12871. intel_crtc_init(dev, pipe);
  12872. for_each_sprite(dev_priv, pipe, sprite) {
  12873. ret = intel_plane_init(dev, pipe, sprite);
  12874. if (ret)
  12875. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12876. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12877. }
  12878. }
  12879. intel_update_czclk(dev_priv);
  12880. intel_update_cdclk(dev);
  12881. intel_shared_dpll_init(dev);
  12882. /* Just disable it once at startup */
  12883. i915_disable_vga(dev);
  12884. intel_setup_outputs(dev);
  12885. drm_modeset_lock_all(dev);
  12886. intel_modeset_setup_hw_state(dev);
  12887. drm_modeset_unlock_all(dev);
  12888. for_each_intel_crtc(dev, crtc) {
  12889. struct intel_initial_plane_config plane_config = {};
  12890. if (!crtc->active)
  12891. continue;
  12892. /*
  12893. * Note that reserving the BIOS fb up front prevents us
  12894. * from stuffing other stolen allocations like the ring
  12895. * on top. This prevents some ugliness at boot time, and
  12896. * can even allow for smooth boot transitions if the BIOS
  12897. * fb is large enough for the active pipe configuration.
  12898. */
  12899. dev_priv->display.get_initial_plane_config(crtc,
  12900. &plane_config);
  12901. /*
  12902. * If the fb is shared between multiple heads, we'll
  12903. * just get the first one.
  12904. */
  12905. intel_find_initial_plane_obj(crtc, &plane_config);
  12906. }
  12907. /*
  12908. * Make sure hardware watermarks really match the state we read out.
  12909. * Note that we need to do this after reconstructing the BIOS fb's
  12910. * since the watermark calculation done here will use pstate->fb.
  12911. */
  12912. sanitize_watermarks(dev);
  12913. }
  12914. static void intel_enable_pipe_a(struct drm_device *dev)
  12915. {
  12916. struct intel_connector *connector;
  12917. struct drm_connector *crt = NULL;
  12918. struct intel_load_detect_pipe load_detect_temp;
  12919. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12920. /* We can't just switch on the pipe A, we need to set things up with a
  12921. * proper mode and output configuration. As a gross hack, enable pipe A
  12922. * by enabling the load detect pipe once. */
  12923. for_each_intel_connector(dev, connector) {
  12924. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12925. crt = &connector->base;
  12926. break;
  12927. }
  12928. }
  12929. if (!crt)
  12930. return;
  12931. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12932. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12933. }
  12934. static bool
  12935. intel_check_plane_mapping(struct intel_crtc *crtc)
  12936. {
  12937. struct drm_device *dev = crtc->base.dev;
  12938. struct drm_i915_private *dev_priv = dev->dev_private;
  12939. u32 val;
  12940. if (INTEL_INFO(dev)->num_pipes == 1)
  12941. return true;
  12942. val = I915_READ(DSPCNTR(!crtc->plane));
  12943. if ((val & DISPLAY_PLANE_ENABLE) &&
  12944. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12945. return false;
  12946. return true;
  12947. }
  12948. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12949. {
  12950. struct drm_device *dev = crtc->base.dev;
  12951. struct intel_encoder *encoder;
  12952. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12953. return true;
  12954. return false;
  12955. }
  12956. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12957. {
  12958. struct drm_device *dev = crtc->base.dev;
  12959. struct drm_i915_private *dev_priv = dev->dev_private;
  12960. i915_reg_t reg = PIPECONF(crtc->config->cpu_transcoder);
  12961. /* Clear any frame start delays used for debugging left by the BIOS */
  12962. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12963. /* restore vblank interrupts to correct state */
  12964. drm_crtc_vblank_reset(&crtc->base);
  12965. if (crtc->active) {
  12966. struct intel_plane *plane;
  12967. drm_crtc_vblank_on(&crtc->base);
  12968. /* Disable everything but the primary plane */
  12969. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12970. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12971. continue;
  12972. plane->disable_plane(&plane->base, &crtc->base);
  12973. }
  12974. }
  12975. /* We need to sanitize the plane -> pipe mapping first because this will
  12976. * disable the crtc (and hence change the state) if it is wrong. Note
  12977. * that gen4+ has a fixed plane -> pipe mapping. */
  12978. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12979. bool plane;
  12980. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12981. crtc->base.base.id);
  12982. /* Pipe has the wrong plane attached and the plane is active.
  12983. * Temporarily change the plane mapping and disable everything
  12984. * ... */
  12985. plane = crtc->plane;
  12986. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12987. crtc->plane = !plane;
  12988. intel_crtc_disable_noatomic(&crtc->base);
  12989. crtc->plane = plane;
  12990. }
  12991. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12992. crtc->pipe == PIPE_A && !crtc->active) {
  12993. /* BIOS forgot to enable pipe A, this mostly happens after
  12994. * resume. Force-enable the pipe to fix this, the update_dpms
  12995. * call below we restore the pipe to the right state, but leave
  12996. * the required bits on. */
  12997. intel_enable_pipe_a(dev);
  12998. }
  12999. /* Adjust the state of the output pipe according to whether we
  13000. * have active connectors/encoders. */
  13001. if (!intel_crtc_has_encoders(crtc))
  13002. intel_crtc_disable_noatomic(&crtc->base);
  13003. if (crtc->active != crtc->base.state->active) {
  13004. struct intel_encoder *encoder;
  13005. /* This can happen either due to bugs in the get_hw_state
  13006. * functions or because of calls to intel_crtc_disable_noatomic,
  13007. * or because the pipe is force-enabled due to the
  13008. * pipe A quirk. */
  13009. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  13010. crtc->base.base.id,
  13011. crtc->base.state->enable ? "enabled" : "disabled",
  13012. crtc->active ? "enabled" : "disabled");
  13013. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  13014. crtc->base.state->active = crtc->active;
  13015. crtc->base.enabled = crtc->active;
  13016. crtc->base.state->connector_mask = 0;
  13017. /* Because we only establish the connector -> encoder ->
  13018. * crtc links if something is active, this means the
  13019. * crtc is now deactivated. Break the links. connector
  13020. * -> encoder links are only establish when things are
  13021. * actually up, hence no need to break them. */
  13022. WARN_ON(crtc->active);
  13023. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13024. encoder->base.crtc = NULL;
  13025. }
  13026. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  13027. /*
  13028. * We start out with underrun reporting disabled to avoid races.
  13029. * For correct bookkeeping mark this on active crtcs.
  13030. *
  13031. * Also on gmch platforms we dont have any hardware bits to
  13032. * disable the underrun reporting. Which means we need to start
  13033. * out with underrun reporting disabled also on inactive pipes,
  13034. * since otherwise we'll complain about the garbage we read when
  13035. * e.g. coming up after runtime pm.
  13036. *
  13037. * No protection against concurrent access is required - at
  13038. * worst a fifo underrun happens which also sets this to false.
  13039. */
  13040. crtc->cpu_fifo_underrun_disabled = true;
  13041. crtc->pch_fifo_underrun_disabled = true;
  13042. }
  13043. }
  13044. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13045. {
  13046. struct intel_connector *connector;
  13047. struct drm_device *dev = encoder->base.dev;
  13048. bool active = false;
  13049. /* We need to check both for a crtc link (meaning that the
  13050. * encoder is active and trying to read from a pipe) and the
  13051. * pipe itself being active. */
  13052. bool has_active_crtc = encoder->base.crtc &&
  13053. to_intel_crtc(encoder->base.crtc)->active;
  13054. for_each_intel_connector(dev, connector) {
  13055. if (connector->base.encoder != &encoder->base)
  13056. continue;
  13057. active = true;
  13058. break;
  13059. }
  13060. if (active && !has_active_crtc) {
  13061. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  13062. encoder->base.base.id,
  13063. encoder->base.name);
  13064. /* Connector is active, but has no active pipe. This is
  13065. * fallout from our resume register restoring. Disable
  13066. * the encoder manually again. */
  13067. if (encoder->base.crtc) {
  13068. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  13069. encoder->base.base.id,
  13070. encoder->base.name);
  13071. encoder->disable(encoder);
  13072. if (encoder->post_disable)
  13073. encoder->post_disable(encoder);
  13074. }
  13075. encoder->base.crtc = NULL;
  13076. /* Inconsistent output/port/pipe state happens presumably due to
  13077. * a bug in one of the get_hw_state functions. Or someplace else
  13078. * in our code, like the register restore mess on resume. Clamp
  13079. * things to off as a safer default. */
  13080. for_each_intel_connector(dev, connector) {
  13081. if (connector->encoder != encoder)
  13082. continue;
  13083. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13084. connector->base.encoder = NULL;
  13085. }
  13086. }
  13087. /* Enabled encoders without active connectors will be fixed in
  13088. * the crtc fixup. */
  13089. }
  13090. void i915_redisable_vga_power_on(struct drm_device *dev)
  13091. {
  13092. struct drm_i915_private *dev_priv = dev->dev_private;
  13093. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  13094. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  13095. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  13096. i915_disable_vga(dev);
  13097. }
  13098. }
  13099. void i915_redisable_vga(struct drm_device *dev)
  13100. {
  13101. struct drm_i915_private *dev_priv = dev->dev_private;
  13102. /* This function can be called both from intel_modeset_setup_hw_state or
  13103. * at a very early point in our resume sequence, where the power well
  13104. * structures are not yet restored. Since this function is at a very
  13105. * paranoid "someone might have enabled VGA while we were not looking"
  13106. * level, just check if the power well is enabled instead of trying to
  13107. * follow the "don't touch the power well if we don't need it" policy
  13108. * the rest of the driver uses. */
  13109. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  13110. return;
  13111. i915_redisable_vga_power_on(dev);
  13112. }
  13113. static bool primary_get_hw_state(struct intel_plane *plane)
  13114. {
  13115. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  13116. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  13117. }
  13118. /* FIXME read out full plane state for all planes */
  13119. static void readout_plane_state(struct intel_crtc *crtc)
  13120. {
  13121. struct drm_plane *primary = crtc->base.primary;
  13122. struct intel_plane_state *plane_state =
  13123. to_intel_plane_state(primary->state);
  13124. plane_state->visible = crtc->active &&
  13125. primary_get_hw_state(to_intel_plane(primary));
  13126. if (plane_state->visible)
  13127. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  13128. }
  13129. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  13130. {
  13131. struct drm_i915_private *dev_priv = dev->dev_private;
  13132. enum pipe pipe;
  13133. struct intel_crtc *crtc;
  13134. struct intel_encoder *encoder;
  13135. struct intel_connector *connector;
  13136. int i;
  13137. dev_priv->active_crtcs = 0;
  13138. for_each_intel_crtc(dev, crtc) {
  13139. struct intel_crtc_state *crtc_state = crtc->config;
  13140. int pixclk = 0;
  13141. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  13142. memset(crtc_state, 0, sizeof(*crtc_state));
  13143. crtc_state->base.crtc = &crtc->base;
  13144. crtc_state->base.active = crtc_state->base.enable =
  13145. dev_priv->display.get_pipe_config(crtc, crtc_state);
  13146. crtc->base.enabled = crtc_state->base.enable;
  13147. crtc->active = crtc_state->base.active;
  13148. if (crtc_state->base.active) {
  13149. dev_priv->active_crtcs |= 1 << crtc->pipe;
  13150. if (IS_BROADWELL(dev_priv)) {
  13151. pixclk = ilk_pipe_pixel_rate(crtc_state);
  13152. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  13153. if (crtc_state->ips_enabled)
  13154. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  13155. } else if (IS_VALLEYVIEW(dev_priv) ||
  13156. IS_CHERRYVIEW(dev_priv) ||
  13157. IS_BROXTON(dev_priv))
  13158. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  13159. else
  13160. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  13161. }
  13162. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  13163. readout_plane_state(crtc);
  13164. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  13165. crtc->base.base.id,
  13166. crtc->active ? "enabled" : "disabled");
  13167. }
  13168. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13169. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13170. pll->on = pll->get_hw_state(dev_priv, pll,
  13171. &pll->config.hw_state);
  13172. pll->active = 0;
  13173. pll->config.crtc_mask = 0;
  13174. for_each_intel_crtc(dev, crtc) {
  13175. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  13176. pll->active++;
  13177. pll->config.crtc_mask |= 1 << crtc->pipe;
  13178. }
  13179. }
  13180. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  13181. pll->name, pll->config.crtc_mask, pll->on);
  13182. if (pll->config.crtc_mask)
  13183. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  13184. }
  13185. for_each_intel_encoder(dev, encoder) {
  13186. pipe = 0;
  13187. if (encoder->get_hw_state(encoder, &pipe)) {
  13188. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13189. encoder->base.crtc = &crtc->base;
  13190. encoder->get_config(encoder, crtc->config);
  13191. } else {
  13192. encoder->base.crtc = NULL;
  13193. }
  13194. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  13195. encoder->base.base.id,
  13196. encoder->base.name,
  13197. encoder->base.crtc ? "enabled" : "disabled",
  13198. pipe_name(pipe));
  13199. }
  13200. for_each_intel_connector(dev, connector) {
  13201. if (connector->get_hw_state(connector)) {
  13202. connector->base.dpms = DRM_MODE_DPMS_ON;
  13203. encoder = connector->encoder;
  13204. connector->base.encoder = &encoder->base;
  13205. if (encoder->base.crtc &&
  13206. encoder->base.crtc->state->active) {
  13207. /*
  13208. * This has to be done during hardware readout
  13209. * because anything calling .crtc_disable may
  13210. * rely on the connector_mask being accurate.
  13211. */
  13212. encoder->base.crtc->state->connector_mask |=
  13213. 1 << drm_connector_index(&connector->base);
  13214. }
  13215. } else {
  13216. connector->base.dpms = DRM_MODE_DPMS_OFF;
  13217. connector->base.encoder = NULL;
  13218. }
  13219. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  13220. connector->base.base.id,
  13221. connector->base.name,
  13222. connector->base.encoder ? "enabled" : "disabled");
  13223. }
  13224. for_each_intel_crtc(dev, crtc) {
  13225. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  13226. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  13227. if (crtc->base.state->active) {
  13228. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  13229. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  13230. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  13231. /*
  13232. * The initial mode needs to be set in order to keep
  13233. * the atomic core happy. It wants a valid mode if the
  13234. * crtc's enabled, so we do the above call.
  13235. *
  13236. * At this point some state updated by the connectors
  13237. * in their ->detect() callback has not run yet, so
  13238. * no recalculation can be done yet.
  13239. *
  13240. * Even if we could do a recalculation and modeset
  13241. * right now it would cause a double modeset if
  13242. * fbdev or userspace chooses a different initial mode.
  13243. *
  13244. * If that happens, someone indicated they wanted a
  13245. * mode change, which means it's safe to do a full
  13246. * recalculation.
  13247. */
  13248. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  13249. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  13250. update_scanline_offset(crtc);
  13251. }
  13252. }
  13253. }
  13254. /* Scan out the current hw modeset state,
  13255. * and sanitizes it to the current state
  13256. */
  13257. static void
  13258. intel_modeset_setup_hw_state(struct drm_device *dev)
  13259. {
  13260. struct drm_i915_private *dev_priv = dev->dev_private;
  13261. enum pipe pipe;
  13262. struct intel_crtc *crtc;
  13263. struct intel_encoder *encoder;
  13264. int i;
  13265. intel_modeset_readout_hw_state(dev);
  13266. /* HW state is read out, now we need to sanitize this mess. */
  13267. for_each_intel_encoder(dev, encoder) {
  13268. intel_sanitize_encoder(encoder);
  13269. }
  13270. for_each_pipe(dev_priv, pipe) {
  13271. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  13272. intel_sanitize_crtc(crtc);
  13273. intel_dump_pipe_config(crtc, crtc->config,
  13274. "[setup_hw_state]");
  13275. }
  13276. intel_modeset_update_connector_atomic_state(dev);
  13277. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13278. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13279. if (!pll->on || pll->active)
  13280. continue;
  13281. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13282. pll->disable(dev_priv, pll);
  13283. pll->on = false;
  13284. }
  13285. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  13286. vlv_wm_get_hw_state(dev);
  13287. else if (IS_GEN9(dev))
  13288. skl_wm_get_hw_state(dev);
  13289. else if (HAS_PCH_SPLIT(dev))
  13290. ilk_wm_get_hw_state(dev);
  13291. for_each_intel_crtc(dev, crtc) {
  13292. unsigned long put_domains;
  13293. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  13294. if (WARN_ON(put_domains))
  13295. modeset_put_power_domains(dev_priv, put_domains);
  13296. }
  13297. intel_display_set_init_power(dev_priv, false);
  13298. }
  13299. void intel_display_resume(struct drm_device *dev)
  13300. {
  13301. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  13302. struct intel_connector *conn;
  13303. struct intel_plane *plane;
  13304. struct drm_crtc *crtc;
  13305. int ret;
  13306. if (!state)
  13307. return;
  13308. state->acquire_ctx = dev->mode_config.acquire_ctx;
  13309. /* preserve complete old state, including dpll */
  13310. intel_atomic_get_shared_dpll_state(state);
  13311. for_each_crtc(dev, crtc) {
  13312. struct drm_crtc_state *crtc_state =
  13313. drm_atomic_get_crtc_state(state, crtc);
  13314. ret = PTR_ERR_OR_ZERO(crtc_state);
  13315. if (ret)
  13316. goto err;
  13317. /* force a restore */
  13318. crtc_state->mode_changed = true;
  13319. }
  13320. for_each_intel_plane(dev, plane) {
  13321. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  13322. if (ret)
  13323. goto err;
  13324. }
  13325. for_each_intel_connector(dev, conn) {
  13326. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  13327. if (ret)
  13328. goto err;
  13329. }
  13330. intel_modeset_setup_hw_state(dev);
  13331. i915_redisable_vga(dev);
  13332. ret = drm_atomic_commit(state);
  13333. if (!ret)
  13334. return;
  13335. err:
  13336. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13337. drm_atomic_state_free(state);
  13338. }
  13339. void intel_modeset_gem_init(struct drm_device *dev)
  13340. {
  13341. struct drm_crtc *c;
  13342. struct drm_i915_gem_object *obj;
  13343. int ret;
  13344. mutex_lock(&dev->struct_mutex);
  13345. intel_init_gt_powersave(dev);
  13346. mutex_unlock(&dev->struct_mutex);
  13347. intel_modeset_init_hw(dev);
  13348. intel_setup_overlay(dev);
  13349. /*
  13350. * Make sure any fbs we allocated at startup are properly
  13351. * pinned & fenced. When we do the allocation it's too early
  13352. * for this.
  13353. */
  13354. for_each_crtc(dev, c) {
  13355. obj = intel_fb_obj(c->primary->fb);
  13356. if (obj == NULL)
  13357. continue;
  13358. mutex_lock(&dev->struct_mutex);
  13359. ret = intel_pin_and_fence_fb_obj(c->primary,
  13360. c->primary->fb,
  13361. c->primary->state);
  13362. mutex_unlock(&dev->struct_mutex);
  13363. if (ret) {
  13364. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13365. to_intel_crtc(c)->pipe);
  13366. drm_framebuffer_unreference(c->primary->fb);
  13367. c->primary->fb = NULL;
  13368. c->primary->crtc = c->primary->state->crtc = NULL;
  13369. update_state_fb(c->primary);
  13370. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13371. }
  13372. }
  13373. intel_backlight_register(dev);
  13374. }
  13375. void intel_connector_unregister(struct intel_connector *intel_connector)
  13376. {
  13377. struct drm_connector *connector = &intel_connector->base;
  13378. intel_panel_destroy_backlight(connector);
  13379. drm_connector_unregister(connector);
  13380. }
  13381. void intel_modeset_cleanup(struct drm_device *dev)
  13382. {
  13383. struct drm_i915_private *dev_priv = dev->dev_private;
  13384. struct intel_connector *connector;
  13385. intel_disable_gt_powersave(dev);
  13386. intel_backlight_unregister(dev);
  13387. /*
  13388. * Interrupts and polling as the first thing to avoid creating havoc.
  13389. * Too much stuff here (turning of connectors, ...) would
  13390. * experience fancy races otherwise.
  13391. */
  13392. intel_irq_uninstall(dev_priv);
  13393. /*
  13394. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13395. * poll handlers. Hence disable polling after hpd handling is shut down.
  13396. */
  13397. drm_kms_helper_poll_fini(dev);
  13398. intel_unregister_dsm_handler();
  13399. intel_fbc_disable(dev_priv);
  13400. /* flush any delayed tasks or pending work */
  13401. flush_scheduled_work();
  13402. /* destroy the backlight and sysfs files before encoders/connectors */
  13403. for_each_intel_connector(dev, connector)
  13404. connector->unregister(connector);
  13405. drm_mode_config_cleanup(dev);
  13406. intel_cleanup_overlay(dev);
  13407. mutex_lock(&dev->struct_mutex);
  13408. intel_cleanup_gt_powersave(dev);
  13409. mutex_unlock(&dev->struct_mutex);
  13410. intel_teardown_gmbus(dev);
  13411. }
  13412. /*
  13413. * Return which encoder is currently attached for connector.
  13414. */
  13415. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13416. {
  13417. return &intel_attached_encoder(connector)->base;
  13418. }
  13419. void intel_connector_attach_encoder(struct intel_connector *connector,
  13420. struct intel_encoder *encoder)
  13421. {
  13422. connector->encoder = encoder;
  13423. drm_mode_connector_attach_encoder(&connector->base,
  13424. &encoder->base);
  13425. }
  13426. /*
  13427. * set vga decode state - true == enable VGA decode
  13428. */
  13429. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13430. {
  13431. struct drm_i915_private *dev_priv = dev->dev_private;
  13432. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13433. u16 gmch_ctrl;
  13434. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13435. DRM_ERROR("failed to read control word\n");
  13436. return -EIO;
  13437. }
  13438. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13439. return 0;
  13440. if (state)
  13441. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13442. else
  13443. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13444. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13445. DRM_ERROR("failed to write control word\n");
  13446. return -EIO;
  13447. }
  13448. return 0;
  13449. }
  13450. struct intel_display_error_state {
  13451. u32 power_well_driver;
  13452. int num_transcoders;
  13453. struct intel_cursor_error_state {
  13454. u32 control;
  13455. u32 position;
  13456. u32 base;
  13457. u32 size;
  13458. } cursor[I915_MAX_PIPES];
  13459. struct intel_pipe_error_state {
  13460. bool power_domain_on;
  13461. u32 source;
  13462. u32 stat;
  13463. } pipe[I915_MAX_PIPES];
  13464. struct intel_plane_error_state {
  13465. u32 control;
  13466. u32 stride;
  13467. u32 size;
  13468. u32 pos;
  13469. u32 addr;
  13470. u32 surface;
  13471. u32 tile_offset;
  13472. } plane[I915_MAX_PIPES];
  13473. struct intel_transcoder_error_state {
  13474. bool power_domain_on;
  13475. enum transcoder cpu_transcoder;
  13476. u32 conf;
  13477. u32 htotal;
  13478. u32 hblank;
  13479. u32 hsync;
  13480. u32 vtotal;
  13481. u32 vblank;
  13482. u32 vsync;
  13483. } transcoder[4];
  13484. };
  13485. struct intel_display_error_state *
  13486. intel_display_capture_error_state(struct drm_device *dev)
  13487. {
  13488. struct drm_i915_private *dev_priv = dev->dev_private;
  13489. struct intel_display_error_state *error;
  13490. int transcoders[] = {
  13491. TRANSCODER_A,
  13492. TRANSCODER_B,
  13493. TRANSCODER_C,
  13494. TRANSCODER_EDP,
  13495. };
  13496. int i;
  13497. if (INTEL_INFO(dev)->num_pipes == 0)
  13498. return NULL;
  13499. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13500. if (error == NULL)
  13501. return NULL;
  13502. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13503. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13504. for_each_pipe(dev_priv, i) {
  13505. error->pipe[i].power_domain_on =
  13506. __intel_display_power_is_enabled(dev_priv,
  13507. POWER_DOMAIN_PIPE(i));
  13508. if (!error->pipe[i].power_domain_on)
  13509. continue;
  13510. error->cursor[i].control = I915_READ(CURCNTR(i));
  13511. error->cursor[i].position = I915_READ(CURPOS(i));
  13512. error->cursor[i].base = I915_READ(CURBASE(i));
  13513. error->plane[i].control = I915_READ(DSPCNTR(i));
  13514. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13515. if (INTEL_INFO(dev)->gen <= 3) {
  13516. error->plane[i].size = I915_READ(DSPSIZE(i));
  13517. error->plane[i].pos = I915_READ(DSPPOS(i));
  13518. }
  13519. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13520. error->plane[i].addr = I915_READ(DSPADDR(i));
  13521. if (INTEL_INFO(dev)->gen >= 4) {
  13522. error->plane[i].surface = I915_READ(DSPSURF(i));
  13523. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13524. }
  13525. error->pipe[i].source = I915_READ(PIPESRC(i));
  13526. if (HAS_GMCH_DISPLAY(dev))
  13527. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13528. }
  13529. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13530. if (HAS_DDI(dev_priv->dev))
  13531. error->num_transcoders++; /* Account for eDP. */
  13532. for (i = 0; i < error->num_transcoders; i++) {
  13533. enum transcoder cpu_transcoder = transcoders[i];
  13534. error->transcoder[i].power_domain_on =
  13535. __intel_display_power_is_enabled(dev_priv,
  13536. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13537. if (!error->transcoder[i].power_domain_on)
  13538. continue;
  13539. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13540. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13541. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13542. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13543. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13544. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13545. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13546. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13547. }
  13548. return error;
  13549. }
  13550. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13551. void
  13552. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13553. struct drm_device *dev,
  13554. struct intel_display_error_state *error)
  13555. {
  13556. struct drm_i915_private *dev_priv = dev->dev_private;
  13557. int i;
  13558. if (!error)
  13559. return;
  13560. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13561. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13562. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13563. error->power_well_driver);
  13564. for_each_pipe(dev_priv, i) {
  13565. err_printf(m, "Pipe [%d]:\n", i);
  13566. err_printf(m, " Power: %s\n",
  13567. onoff(error->pipe[i].power_domain_on));
  13568. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13569. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13570. err_printf(m, "Plane [%d]:\n", i);
  13571. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13572. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13573. if (INTEL_INFO(dev)->gen <= 3) {
  13574. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13575. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13576. }
  13577. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13578. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13579. if (INTEL_INFO(dev)->gen >= 4) {
  13580. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13581. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13582. }
  13583. err_printf(m, "Cursor [%d]:\n", i);
  13584. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13585. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13586. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13587. }
  13588. for (i = 0; i < error->num_transcoders; i++) {
  13589. err_printf(m, "CPU transcoder: %c\n",
  13590. transcoder_name(error->transcoder[i].cpu_transcoder));
  13591. err_printf(m, " Power: %s\n",
  13592. onoff(error->transcoder[i].power_domain_on));
  13593. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13594. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13595. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13596. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13597. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13598. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13599. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13600. }
  13601. }
  13602. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13603. {
  13604. struct intel_crtc *crtc;
  13605. for_each_intel_crtc(dev, crtc) {
  13606. struct intel_unpin_work *work;
  13607. spin_lock_irq(&dev->event_lock);
  13608. work = crtc->unpin_work;
  13609. if (work && work->event &&
  13610. work->event->base.file_priv == file) {
  13611. kfree(work->event);
  13612. work->event = NULL;
  13613. }
  13614. spin_unlock_irq(&dev->event_lock);
  13615. }
  13616. }