intel_display.c 437 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883
  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. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. };
  72. /* Cursor formats */
  73. static const uint32_t intel_cursor_formats[] = {
  74. DRM_FORMAT_ARGB8888,
  75. };
  76. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  77. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  78. struct intel_crtc_state *pipe_config);
  79. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  80. struct intel_crtc_state *pipe_config);
  81. static int intel_set_mode(struct drm_atomic_state *state);
  82. static int intel_framebuffer_init(struct drm_device *dev,
  83. struct intel_framebuffer *ifb,
  84. struct drm_mode_fb_cmd2 *mode_cmd,
  85. struct drm_i915_gem_object *obj);
  86. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  87. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  88. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  89. struct intel_link_m_n *m_n,
  90. struct intel_link_m_n *m2_n2);
  91. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  92. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  93. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  94. static void vlv_prepare_pll(struct intel_crtc *crtc,
  95. const struct intel_crtc_state *pipe_config);
  96. static void chv_prepare_pll(struct intel_crtc *crtc,
  97. const struct intel_crtc_state *pipe_config);
  98. static void intel_begin_crtc_commit(struct drm_crtc *crtc);
  99. static void intel_finish_crtc_commit(struct drm_crtc *crtc);
  100. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  101. struct intel_crtc_state *crtc_state);
  102. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  103. int num_connectors);
  104. static void intel_crtc_enable_planes(struct drm_crtc *crtc);
  105. static void intel_crtc_disable_planes(struct drm_crtc *crtc);
  106. static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
  107. {
  108. if (!connector->mst_port)
  109. return connector->encoder;
  110. else
  111. return &connector->mst_port->mst_encoders[pipe]->base;
  112. }
  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. int
  126. intel_pch_rawclk(struct drm_device *dev)
  127. {
  128. struct drm_i915_private *dev_priv = dev->dev_private;
  129. WARN_ON(!HAS_PCH_SPLIT(dev));
  130. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  131. }
  132. static inline u32 /* units of 100MHz */
  133. intel_fdi_link_freq(struct drm_device *dev)
  134. {
  135. if (IS_GEN5(dev)) {
  136. struct drm_i915_private *dev_priv = dev->dev_private;
  137. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  138. } else
  139. return 27;
  140. }
  141. static const intel_limit_t intel_limits_i8xx_dac = {
  142. .dot = { .min = 25000, .max = 350000 },
  143. .vco = { .min = 908000, .max = 1512000 },
  144. .n = { .min = 2, .max = 16 },
  145. .m = { .min = 96, .max = 140 },
  146. .m1 = { .min = 18, .max = 26 },
  147. .m2 = { .min = 6, .max = 16 },
  148. .p = { .min = 4, .max = 128 },
  149. .p1 = { .min = 2, .max = 33 },
  150. .p2 = { .dot_limit = 165000,
  151. .p2_slow = 4, .p2_fast = 2 },
  152. };
  153. static const intel_limit_t intel_limits_i8xx_dvo = {
  154. .dot = { .min = 25000, .max = 350000 },
  155. .vco = { .min = 908000, .max = 1512000 },
  156. .n = { .min = 2, .max = 16 },
  157. .m = { .min = 96, .max = 140 },
  158. .m1 = { .min = 18, .max = 26 },
  159. .m2 = { .min = 6, .max = 16 },
  160. .p = { .min = 4, .max = 128 },
  161. .p1 = { .min = 2, .max = 33 },
  162. .p2 = { .dot_limit = 165000,
  163. .p2_slow = 4, .p2_fast = 4 },
  164. };
  165. static const intel_limit_t intel_limits_i8xx_lvds = {
  166. .dot = { .min = 25000, .max = 350000 },
  167. .vco = { .min = 908000, .max = 1512000 },
  168. .n = { .min = 2, .max = 16 },
  169. .m = { .min = 96, .max = 140 },
  170. .m1 = { .min = 18, .max = 26 },
  171. .m2 = { .min = 6, .max = 16 },
  172. .p = { .min = 4, .max = 128 },
  173. .p1 = { .min = 1, .max = 6 },
  174. .p2 = { .dot_limit = 165000,
  175. .p2_slow = 14, .p2_fast = 7 },
  176. };
  177. static const intel_limit_t intel_limits_i9xx_sdvo = {
  178. .dot = { .min = 20000, .max = 400000 },
  179. .vco = { .min = 1400000, .max = 2800000 },
  180. .n = { .min = 1, .max = 6 },
  181. .m = { .min = 70, .max = 120 },
  182. .m1 = { .min = 8, .max = 18 },
  183. .m2 = { .min = 3, .max = 7 },
  184. .p = { .min = 5, .max = 80 },
  185. .p1 = { .min = 1, .max = 8 },
  186. .p2 = { .dot_limit = 200000,
  187. .p2_slow = 10, .p2_fast = 5 },
  188. };
  189. static const intel_limit_t intel_limits_i9xx_lvds = {
  190. .dot = { .min = 20000, .max = 400000 },
  191. .vco = { .min = 1400000, .max = 2800000 },
  192. .n = { .min = 1, .max = 6 },
  193. .m = { .min = 70, .max = 120 },
  194. .m1 = { .min = 8, .max = 18 },
  195. .m2 = { .min = 3, .max = 7 },
  196. .p = { .min = 7, .max = 98 },
  197. .p1 = { .min = 1, .max = 8 },
  198. .p2 = { .dot_limit = 112000,
  199. .p2_slow = 14, .p2_fast = 7 },
  200. };
  201. static const intel_limit_t intel_limits_g4x_sdvo = {
  202. .dot = { .min = 25000, .max = 270000 },
  203. .vco = { .min = 1750000, .max = 3500000},
  204. .n = { .min = 1, .max = 4 },
  205. .m = { .min = 104, .max = 138 },
  206. .m1 = { .min = 17, .max = 23 },
  207. .m2 = { .min = 5, .max = 11 },
  208. .p = { .min = 10, .max = 30 },
  209. .p1 = { .min = 1, .max = 3},
  210. .p2 = { .dot_limit = 270000,
  211. .p2_slow = 10,
  212. .p2_fast = 10
  213. },
  214. };
  215. static const intel_limit_t intel_limits_g4x_hdmi = {
  216. .dot = { .min = 22000, .max = 400000 },
  217. .vco = { .min = 1750000, .max = 3500000},
  218. .n = { .min = 1, .max = 4 },
  219. .m = { .min = 104, .max = 138 },
  220. .m1 = { .min = 16, .max = 23 },
  221. .m2 = { .min = 5, .max = 11 },
  222. .p = { .min = 5, .max = 80 },
  223. .p1 = { .min = 1, .max = 8},
  224. .p2 = { .dot_limit = 165000,
  225. .p2_slow = 10, .p2_fast = 5 },
  226. };
  227. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  228. .dot = { .min = 20000, .max = 115000 },
  229. .vco = { .min = 1750000, .max = 3500000 },
  230. .n = { .min = 1, .max = 3 },
  231. .m = { .min = 104, .max = 138 },
  232. .m1 = { .min = 17, .max = 23 },
  233. .m2 = { .min = 5, .max = 11 },
  234. .p = { .min = 28, .max = 112 },
  235. .p1 = { .min = 2, .max = 8 },
  236. .p2 = { .dot_limit = 0,
  237. .p2_slow = 14, .p2_fast = 14
  238. },
  239. };
  240. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  241. .dot = { .min = 80000, .max = 224000 },
  242. .vco = { .min = 1750000, .max = 3500000 },
  243. .n = { .min = 1, .max = 3 },
  244. .m = { .min = 104, .max = 138 },
  245. .m1 = { .min = 17, .max = 23 },
  246. .m2 = { .min = 5, .max = 11 },
  247. .p = { .min = 14, .max = 42 },
  248. .p1 = { .min = 2, .max = 6 },
  249. .p2 = { .dot_limit = 0,
  250. .p2_slow = 7, .p2_fast = 7
  251. },
  252. };
  253. static const intel_limit_t intel_limits_pineview_sdvo = {
  254. .dot = { .min = 20000, .max = 400000},
  255. .vco = { .min = 1700000, .max = 3500000 },
  256. /* Pineview's Ncounter is a ring counter */
  257. .n = { .min = 3, .max = 6 },
  258. .m = { .min = 2, .max = 256 },
  259. /* Pineview only has one combined m divider, which we treat as m2. */
  260. .m1 = { .min = 0, .max = 0 },
  261. .m2 = { .min = 0, .max = 254 },
  262. .p = { .min = 5, .max = 80 },
  263. .p1 = { .min = 1, .max = 8 },
  264. .p2 = { .dot_limit = 200000,
  265. .p2_slow = 10, .p2_fast = 5 },
  266. };
  267. static const intel_limit_t intel_limits_pineview_lvds = {
  268. .dot = { .min = 20000, .max = 400000 },
  269. .vco = { .min = 1700000, .max = 3500000 },
  270. .n = { .min = 3, .max = 6 },
  271. .m = { .min = 2, .max = 256 },
  272. .m1 = { .min = 0, .max = 0 },
  273. .m2 = { .min = 0, .max = 254 },
  274. .p = { .min = 7, .max = 112 },
  275. .p1 = { .min = 1, .max = 8 },
  276. .p2 = { .dot_limit = 112000,
  277. .p2_slow = 14, .p2_fast = 14 },
  278. };
  279. /* Ironlake / Sandybridge
  280. *
  281. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  282. * the range value for them is (actual_value - 2).
  283. */
  284. static const intel_limit_t intel_limits_ironlake_dac = {
  285. .dot = { .min = 25000, .max = 350000 },
  286. .vco = { .min = 1760000, .max = 3510000 },
  287. .n = { .min = 1, .max = 5 },
  288. .m = { .min = 79, .max = 127 },
  289. .m1 = { .min = 12, .max = 22 },
  290. .m2 = { .min = 5, .max = 9 },
  291. .p = { .min = 5, .max = 80 },
  292. .p1 = { .min = 1, .max = 8 },
  293. .p2 = { .dot_limit = 225000,
  294. .p2_slow = 10, .p2_fast = 5 },
  295. };
  296. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  297. .dot = { .min = 25000, .max = 350000 },
  298. .vco = { .min = 1760000, .max = 3510000 },
  299. .n = { .min = 1, .max = 3 },
  300. .m = { .min = 79, .max = 118 },
  301. .m1 = { .min = 12, .max = 22 },
  302. .m2 = { .min = 5, .max = 9 },
  303. .p = { .min = 28, .max = 112 },
  304. .p1 = { .min = 2, .max = 8 },
  305. .p2 = { .dot_limit = 225000,
  306. .p2_slow = 14, .p2_fast = 14 },
  307. };
  308. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  309. .dot = { .min = 25000, .max = 350000 },
  310. .vco = { .min = 1760000, .max = 3510000 },
  311. .n = { .min = 1, .max = 3 },
  312. .m = { .min = 79, .max = 127 },
  313. .m1 = { .min = 12, .max = 22 },
  314. .m2 = { .min = 5, .max = 9 },
  315. .p = { .min = 14, .max = 56 },
  316. .p1 = { .min = 2, .max = 8 },
  317. .p2 = { .dot_limit = 225000,
  318. .p2_slow = 7, .p2_fast = 7 },
  319. };
  320. /* LVDS 100mhz refclk limits. */
  321. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 2 },
  325. .m = { .min = 79, .max = 126 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 28, .max = 112 },
  329. .p1 = { .min = 2, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 14, .p2_fast = 14 },
  332. };
  333. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 126 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 14, .max = 42 },
  341. .p1 = { .min = 2, .max = 6 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 7, .p2_fast = 7 },
  344. };
  345. static const intel_limit_t intel_limits_vlv = {
  346. /*
  347. * These are the data rate limits (measured in fast clocks)
  348. * since those are the strictest limits we have. The fast
  349. * clock and actual rate limits are more relaxed, so checking
  350. * them would make no difference.
  351. */
  352. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  353. .vco = { .min = 4000000, .max = 6000000 },
  354. .n = { .min = 1, .max = 7 },
  355. .m1 = { .min = 2, .max = 3 },
  356. .m2 = { .min = 11, .max = 156 },
  357. .p1 = { .min = 2, .max = 3 },
  358. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  359. };
  360. static const intel_limit_t intel_limits_chv = {
  361. /*
  362. * These are the data rate limits (measured in fast clocks)
  363. * since those are the strictest limits we have. The fast
  364. * clock and actual rate limits are more relaxed, so checking
  365. * them would make no difference.
  366. */
  367. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  368. .vco = { .min = 4800000, .max = 6480000 },
  369. .n = { .min = 1, .max = 1 },
  370. .m1 = { .min = 2, .max = 2 },
  371. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  372. .p1 = { .min = 2, .max = 4 },
  373. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  374. };
  375. static const intel_limit_t intel_limits_bxt = {
  376. /* FIXME: find real dot limits */
  377. .dot = { .min = 0, .max = INT_MAX },
  378. .vco = { .min = 4800000, .max = 6480000 },
  379. .n = { .min = 1, .max = 1 },
  380. .m1 = { .min = 2, .max = 2 },
  381. /* FIXME: find real m2 limits */
  382. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  383. .p1 = { .min = 2, .max = 4 },
  384. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  385. };
  386. static void vlv_clock(int refclk, intel_clock_t *clock)
  387. {
  388. clock->m = clock->m1 * clock->m2;
  389. clock->p = clock->p1 * clock->p2;
  390. if (WARN_ON(clock->n == 0 || clock->p == 0))
  391. return;
  392. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  393. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  394. }
  395. static bool
  396. needs_modeset(struct drm_crtc_state *state)
  397. {
  398. return state->mode_changed || state->active_changed;
  399. }
  400. /**
  401. * Returns whether any output on the specified pipe is of the specified type
  402. */
  403. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  404. {
  405. struct drm_device *dev = crtc->base.dev;
  406. struct intel_encoder *encoder;
  407. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  408. if (encoder->type == type)
  409. return true;
  410. return false;
  411. }
  412. /**
  413. * Returns whether any output on the specified pipe will have the specified
  414. * type after a staged modeset is complete, i.e., the same as
  415. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  416. * encoder->crtc.
  417. */
  418. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  419. int type)
  420. {
  421. struct drm_atomic_state *state = crtc_state->base.state;
  422. struct drm_connector *connector;
  423. struct drm_connector_state *connector_state;
  424. struct intel_encoder *encoder;
  425. int i, num_connectors = 0;
  426. for_each_connector_in_state(state, connector, connector_state, i) {
  427. if (connector_state->crtc != crtc_state->base.crtc)
  428. continue;
  429. num_connectors++;
  430. encoder = to_intel_encoder(connector_state->best_encoder);
  431. if (encoder->type == type)
  432. return true;
  433. }
  434. WARN_ON(num_connectors == 0);
  435. return false;
  436. }
  437. static const intel_limit_t *
  438. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  439. {
  440. struct drm_device *dev = crtc_state->base.crtc->dev;
  441. const intel_limit_t *limit;
  442. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  443. if (intel_is_dual_link_lvds(dev)) {
  444. if (refclk == 100000)
  445. limit = &intel_limits_ironlake_dual_lvds_100m;
  446. else
  447. limit = &intel_limits_ironlake_dual_lvds;
  448. } else {
  449. if (refclk == 100000)
  450. limit = &intel_limits_ironlake_single_lvds_100m;
  451. else
  452. limit = &intel_limits_ironlake_single_lvds;
  453. }
  454. } else
  455. limit = &intel_limits_ironlake_dac;
  456. return limit;
  457. }
  458. static const intel_limit_t *
  459. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  460. {
  461. struct drm_device *dev = crtc_state->base.crtc->dev;
  462. const intel_limit_t *limit;
  463. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  464. if (intel_is_dual_link_lvds(dev))
  465. limit = &intel_limits_g4x_dual_channel_lvds;
  466. else
  467. limit = &intel_limits_g4x_single_channel_lvds;
  468. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  469. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  470. limit = &intel_limits_g4x_hdmi;
  471. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  472. limit = &intel_limits_g4x_sdvo;
  473. } else /* The option is for other outputs */
  474. limit = &intel_limits_i9xx_sdvo;
  475. return limit;
  476. }
  477. static const intel_limit_t *
  478. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  479. {
  480. struct drm_device *dev = crtc_state->base.crtc->dev;
  481. const intel_limit_t *limit;
  482. if (IS_BROXTON(dev))
  483. limit = &intel_limits_bxt;
  484. else if (HAS_PCH_SPLIT(dev))
  485. limit = intel_ironlake_limit(crtc_state, refclk);
  486. else if (IS_G4X(dev)) {
  487. limit = intel_g4x_limit(crtc_state);
  488. } else if (IS_PINEVIEW(dev)) {
  489. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  490. limit = &intel_limits_pineview_lvds;
  491. else
  492. limit = &intel_limits_pineview_sdvo;
  493. } else if (IS_CHERRYVIEW(dev)) {
  494. limit = &intel_limits_chv;
  495. } else if (IS_VALLEYVIEW(dev)) {
  496. limit = &intel_limits_vlv;
  497. } else if (!IS_GEN2(dev)) {
  498. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  499. limit = &intel_limits_i9xx_lvds;
  500. else
  501. limit = &intel_limits_i9xx_sdvo;
  502. } else {
  503. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  504. limit = &intel_limits_i8xx_lvds;
  505. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  506. limit = &intel_limits_i8xx_dvo;
  507. else
  508. limit = &intel_limits_i8xx_dac;
  509. }
  510. return limit;
  511. }
  512. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  513. static void pineview_clock(int refclk, intel_clock_t *clock)
  514. {
  515. clock->m = clock->m2 + 2;
  516. clock->p = clock->p1 * clock->p2;
  517. if (WARN_ON(clock->n == 0 || clock->p == 0))
  518. return;
  519. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  520. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  521. }
  522. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  523. {
  524. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  525. }
  526. static void i9xx_clock(int refclk, intel_clock_t *clock)
  527. {
  528. clock->m = i9xx_dpll_compute_m(clock);
  529. clock->p = clock->p1 * clock->p2;
  530. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  531. return;
  532. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. }
  535. static void chv_clock(int refclk, intel_clock_t *clock)
  536. {
  537. clock->m = clock->m1 * clock->m2;
  538. clock->p = clock->p1 * clock->p2;
  539. if (WARN_ON(clock->n == 0 || clock->p == 0))
  540. return;
  541. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  542. clock->n << 22);
  543. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  544. }
  545. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  546. /**
  547. * Returns whether the given set of divisors are valid for a given refclk with
  548. * the given connectors.
  549. */
  550. static bool intel_PLL_is_valid(struct drm_device *dev,
  551. const intel_limit_t *limit,
  552. const intel_clock_t *clock)
  553. {
  554. if (clock->n < limit->n.min || limit->n.max < clock->n)
  555. INTELPllInvalid("n out of range\n");
  556. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  557. INTELPllInvalid("p1 out of range\n");
  558. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  559. INTELPllInvalid("m2 out of range\n");
  560. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  561. INTELPllInvalid("m1 out of range\n");
  562. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  563. if (clock->m1 <= clock->m2)
  564. INTELPllInvalid("m1 <= m2\n");
  565. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  566. if (clock->p < limit->p.min || limit->p.max < clock->p)
  567. INTELPllInvalid("p out of range\n");
  568. if (clock->m < limit->m.min || limit->m.max < clock->m)
  569. INTELPllInvalid("m out of range\n");
  570. }
  571. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  572. INTELPllInvalid("vco out of range\n");
  573. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  574. * connector, etc., rather than just a single range.
  575. */
  576. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  577. INTELPllInvalid("dot out of range\n");
  578. return true;
  579. }
  580. static bool
  581. i9xx_find_best_dpll(const intel_limit_t *limit,
  582. struct intel_crtc_state *crtc_state,
  583. int target, int refclk, intel_clock_t *match_clock,
  584. intel_clock_t *best_clock)
  585. {
  586. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  587. struct drm_device *dev = crtc->base.dev;
  588. intel_clock_t clock;
  589. int err = target;
  590. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  591. /*
  592. * For LVDS just rely on its current settings for dual-channel.
  593. * We haven't figured out how to reliably set up different
  594. * single/dual channel state, if we even can.
  595. */
  596. if (intel_is_dual_link_lvds(dev))
  597. clock.p2 = limit->p2.p2_fast;
  598. else
  599. clock.p2 = limit->p2.p2_slow;
  600. } else {
  601. if (target < limit->p2.dot_limit)
  602. clock.p2 = limit->p2.p2_slow;
  603. else
  604. clock.p2 = limit->p2.p2_fast;
  605. }
  606. memset(best_clock, 0, sizeof(*best_clock));
  607. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  608. clock.m1++) {
  609. for (clock.m2 = limit->m2.min;
  610. clock.m2 <= limit->m2.max; clock.m2++) {
  611. if (clock.m2 >= clock.m1)
  612. break;
  613. for (clock.n = limit->n.min;
  614. clock.n <= limit->n.max; clock.n++) {
  615. for (clock.p1 = limit->p1.min;
  616. clock.p1 <= limit->p1.max; clock.p1++) {
  617. int this_err;
  618. i9xx_clock(refclk, &clock);
  619. if (!intel_PLL_is_valid(dev, limit,
  620. &clock))
  621. continue;
  622. if (match_clock &&
  623. clock.p != match_clock->p)
  624. continue;
  625. this_err = abs(clock.dot - target);
  626. if (this_err < err) {
  627. *best_clock = clock;
  628. err = this_err;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. return (err != target);
  635. }
  636. static bool
  637. pnv_find_best_dpll(const intel_limit_t *limit,
  638. struct intel_crtc_state *crtc_state,
  639. int target, int refclk, intel_clock_t *match_clock,
  640. intel_clock_t *best_clock)
  641. {
  642. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  643. struct drm_device *dev = crtc->base.dev;
  644. intel_clock_t clock;
  645. int err = target;
  646. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  647. /*
  648. * For LVDS just rely on its current settings for dual-channel.
  649. * We haven't figured out how to reliably set up different
  650. * single/dual channel state, if we even can.
  651. */
  652. if (intel_is_dual_link_lvds(dev))
  653. clock.p2 = limit->p2.p2_fast;
  654. else
  655. clock.p2 = limit->p2.p2_slow;
  656. } else {
  657. if (target < limit->p2.dot_limit)
  658. clock.p2 = limit->p2.p2_slow;
  659. else
  660. clock.p2 = limit->p2.p2_fast;
  661. }
  662. memset(best_clock, 0, sizeof(*best_clock));
  663. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  664. clock.m1++) {
  665. for (clock.m2 = limit->m2.min;
  666. clock.m2 <= limit->m2.max; clock.m2++) {
  667. for (clock.n = limit->n.min;
  668. clock.n <= limit->n.max; clock.n++) {
  669. for (clock.p1 = limit->p1.min;
  670. clock.p1 <= limit->p1.max; clock.p1++) {
  671. int this_err;
  672. pineview_clock(refclk, &clock);
  673. if (!intel_PLL_is_valid(dev, limit,
  674. &clock))
  675. continue;
  676. if (match_clock &&
  677. clock.p != match_clock->p)
  678. continue;
  679. this_err = abs(clock.dot - target);
  680. if (this_err < err) {
  681. *best_clock = clock;
  682. err = this_err;
  683. }
  684. }
  685. }
  686. }
  687. }
  688. return (err != target);
  689. }
  690. static bool
  691. g4x_find_best_dpll(const intel_limit_t *limit,
  692. struct intel_crtc_state *crtc_state,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  697. struct drm_device *dev = crtc->base.dev;
  698. intel_clock_t clock;
  699. int max_n;
  700. bool found;
  701. /* approximately equals target * 0.00585 */
  702. int err_most = (target >> 8) + (target >> 9);
  703. found = false;
  704. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  705. if (intel_is_dual_link_lvds(dev))
  706. clock.p2 = limit->p2.p2_fast;
  707. else
  708. clock.p2 = limit->p2.p2_slow;
  709. } else {
  710. if (target < limit->p2.dot_limit)
  711. clock.p2 = limit->p2.p2_slow;
  712. else
  713. clock.p2 = limit->p2.p2_fast;
  714. }
  715. memset(best_clock, 0, sizeof(*best_clock));
  716. max_n = limit->n.max;
  717. /* based on hardware requirement, prefer smaller n to precision */
  718. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  719. /* based on hardware requirement, prefere larger m1,m2 */
  720. for (clock.m1 = limit->m1.max;
  721. clock.m1 >= limit->m1.min; clock.m1--) {
  722. for (clock.m2 = limit->m2.max;
  723. clock.m2 >= limit->m2.min; clock.m2--) {
  724. for (clock.p1 = limit->p1.max;
  725. clock.p1 >= limit->p1.min; clock.p1--) {
  726. int this_err;
  727. i9xx_clock(refclk, &clock);
  728. if (!intel_PLL_is_valid(dev, limit,
  729. &clock))
  730. continue;
  731. this_err = abs(clock.dot - target);
  732. if (this_err < err_most) {
  733. *best_clock = clock;
  734. err_most = this_err;
  735. max_n = clock.n;
  736. found = true;
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return found;
  743. }
  744. /*
  745. * Check if the calculated PLL configuration is more optimal compared to the
  746. * best configuration and error found so far. Return the calculated error.
  747. */
  748. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  749. const intel_clock_t *calculated_clock,
  750. const intel_clock_t *best_clock,
  751. unsigned int best_error_ppm,
  752. unsigned int *error_ppm)
  753. {
  754. /*
  755. * For CHV ignore the error and consider only the P value.
  756. * Prefer a bigger P value based on HW requirements.
  757. */
  758. if (IS_CHERRYVIEW(dev)) {
  759. *error_ppm = 0;
  760. return calculated_clock->p > best_clock->p;
  761. }
  762. if (WARN_ON_ONCE(!target_freq))
  763. return false;
  764. *error_ppm = div_u64(1000000ULL *
  765. abs(target_freq - calculated_clock->dot),
  766. target_freq);
  767. /*
  768. * Prefer a better P value over a better (smaller) error if the error
  769. * is small. Ensure this preference for future configurations too by
  770. * setting the error to 0.
  771. */
  772. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  773. *error_ppm = 0;
  774. return true;
  775. }
  776. return *error_ppm + 10 < best_error_ppm;
  777. }
  778. static bool
  779. vlv_find_best_dpll(const intel_limit_t *limit,
  780. struct intel_crtc_state *crtc_state,
  781. int target, int refclk, intel_clock_t *match_clock,
  782. intel_clock_t *best_clock)
  783. {
  784. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  785. struct drm_device *dev = crtc->base.dev;
  786. intel_clock_t clock;
  787. unsigned int bestppm = 1000000;
  788. /* min update 19.2 MHz */
  789. int max_n = min(limit->n.max, refclk / 19200);
  790. bool found = false;
  791. target *= 5; /* fast clock */
  792. memset(best_clock, 0, sizeof(*best_clock));
  793. /* based on hardware requirement, prefer smaller n to precision */
  794. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  795. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  796. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  797. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  798. clock.p = clock.p1 * clock.p2;
  799. /* based on hardware requirement, prefer bigger m1,m2 values */
  800. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  801. unsigned int ppm;
  802. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  803. refclk * clock.m1);
  804. vlv_clock(refclk, &clock);
  805. if (!intel_PLL_is_valid(dev, limit,
  806. &clock))
  807. continue;
  808. if (!vlv_PLL_is_optimal(dev, target,
  809. &clock,
  810. best_clock,
  811. bestppm, &ppm))
  812. continue;
  813. *best_clock = clock;
  814. bestppm = ppm;
  815. found = true;
  816. }
  817. }
  818. }
  819. }
  820. return found;
  821. }
  822. static bool
  823. chv_find_best_dpll(const intel_limit_t *limit,
  824. struct intel_crtc_state *crtc_state,
  825. int target, int refclk, intel_clock_t *match_clock,
  826. intel_clock_t *best_clock)
  827. {
  828. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  829. struct drm_device *dev = crtc->base.dev;
  830. unsigned int best_error_ppm;
  831. intel_clock_t clock;
  832. uint64_t m2;
  833. int found = false;
  834. memset(best_clock, 0, sizeof(*best_clock));
  835. best_error_ppm = 1000000;
  836. /*
  837. * Based on hardware doc, the n always set to 1, and m1 always
  838. * set to 2. If requires to support 200Mhz refclk, we need to
  839. * revisit this because n may not 1 anymore.
  840. */
  841. clock.n = 1, clock.m1 = 2;
  842. target *= 5; /* fast clock */
  843. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  844. for (clock.p2 = limit->p2.p2_fast;
  845. clock.p2 >= limit->p2.p2_slow;
  846. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  847. unsigned int error_ppm;
  848. clock.p = clock.p1 * clock.p2;
  849. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  850. clock.n) << 22, refclk * clock.m1);
  851. if (m2 > INT_MAX/clock.m1)
  852. continue;
  853. clock.m2 = m2;
  854. chv_clock(refclk, &clock);
  855. if (!intel_PLL_is_valid(dev, limit, &clock))
  856. continue;
  857. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  858. best_error_ppm, &error_ppm))
  859. continue;
  860. *best_clock = clock;
  861. best_error_ppm = error_ppm;
  862. found = true;
  863. }
  864. }
  865. return found;
  866. }
  867. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  868. intel_clock_t *best_clock)
  869. {
  870. int refclk = i9xx_get_refclk(crtc_state, 0);
  871. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  872. target_clock, refclk, NULL, best_clock);
  873. }
  874. bool intel_crtc_active(struct drm_crtc *crtc)
  875. {
  876. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  877. /* Be paranoid as we can arrive here with only partial
  878. * state retrieved from the hardware during setup.
  879. *
  880. * We can ditch the adjusted_mode.crtc_clock check as soon
  881. * as Haswell has gained clock readout/fastboot support.
  882. *
  883. * We can ditch the crtc->primary->fb check as soon as we can
  884. * properly reconstruct framebuffers.
  885. *
  886. * FIXME: The intel_crtc->active here should be switched to
  887. * crtc->state->active once we have proper CRTC states wired up
  888. * for atomic.
  889. */
  890. return intel_crtc->active && crtc->primary->state->fb &&
  891. intel_crtc->config->base.adjusted_mode.crtc_clock;
  892. }
  893. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  894. enum pipe pipe)
  895. {
  896. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  897. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  898. return intel_crtc->config->cpu_transcoder;
  899. }
  900. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  901. {
  902. struct drm_i915_private *dev_priv = dev->dev_private;
  903. u32 reg = PIPEDSL(pipe);
  904. u32 line1, line2;
  905. u32 line_mask;
  906. if (IS_GEN2(dev))
  907. line_mask = DSL_LINEMASK_GEN2;
  908. else
  909. line_mask = DSL_LINEMASK_GEN3;
  910. line1 = I915_READ(reg) & line_mask;
  911. mdelay(5);
  912. line2 = I915_READ(reg) & line_mask;
  913. return line1 == line2;
  914. }
  915. /*
  916. * intel_wait_for_pipe_off - wait for pipe to turn off
  917. * @crtc: crtc whose pipe to wait for
  918. *
  919. * After disabling a pipe, we can't wait for vblank in the usual way,
  920. * spinning on the vblank interrupt status bit, since we won't actually
  921. * see an interrupt when the pipe is disabled.
  922. *
  923. * On Gen4 and above:
  924. * wait for the pipe register state bit to turn off
  925. *
  926. * Otherwise:
  927. * wait for the display line value to settle (it usually
  928. * ends up stopping at the start of the next frame).
  929. *
  930. */
  931. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  932. {
  933. struct drm_device *dev = crtc->base.dev;
  934. struct drm_i915_private *dev_priv = dev->dev_private;
  935. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  936. enum pipe pipe = crtc->pipe;
  937. if (INTEL_INFO(dev)->gen >= 4) {
  938. int reg = PIPECONF(cpu_transcoder);
  939. /* Wait for the Pipe State to go off */
  940. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  941. 100))
  942. WARN(1, "pipe_off wait timed out\n");
  943. } else {
  944. /* Wait for the display line to settle */
  945. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  946. WARN(1, "pipe_off wait timed out\n");
  947. }
  948. }
  949. /*
  950. * ibx_digital_port_connected - is the specified port connected?
  951. * @dev_priv: i915 private structure
  952. * @port: the port to test
  953. *
  954. * Returns true if @port is connected, false otherwise.
  955. */
  956. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  957. struct intel_digital_port *port)
  958. {
  959. u32 bit;
  960. if (HAS_PCH_IBX(dev_priv->dev)) {
  961. switch (port->port) {
  962. case PORT_B:
  963. bit = SDE_PORTB_HOTPLUG;
  964. break;
  965. case PORT_C:
  966. bit = SDE_PORTC_HOTPLUG;
  967. break;
  968. case PORT_D:
  969. bit = SDE_PORTD_HOTPLUG;
  970. break;
  971. default:
  972. return true;
  973. }
  974. } else {
  975. switch (port->port) {
  976. case PORT_B:
  977. bit = SDE_PORTB_HOTPLUG_CPT;
  978. break;
  979. case PORT_C:
  980. bit = SDE_PORTC_HOTPLUG_CPT;
  981. break;
  982. case PORT_D:
  983. bit = SDE_PORTD_HOTPLUG_CPT;
  984. break;
  985. default:
  986. return true;
  987. }
  988. }
  989. return I915_READ(SDEISR) & bit;
  990. }
  991. static const char *state_string(bool enabled)
  992. {
  993. return enabled ? "on" : "off";
  994. }
  995. /* Only for pre-ILK configs */
  996. void assert_pll(struct drm_i915_private *dev_priv,
  997. enum pipe pipe, bool state)
  998. {
  999. int reg;
  1000. u32 val;
  1001. bool cur_state;
  1002. reg = DPLL(pipe);
  1003. val = I915_READ(reg);
  1004. cur_state = !!(val & DPLL_VCO_ENABLE);
  1005. I915_STATE_WARN(cur_state != state,
  1006. "PLL state assertion failure (expected %s, current %s)\n",
  1007. state_string(state), state_string(cur_state));
  1008. }
  1009. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1010. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1011. {
  1012. u32 val;
  1013. bool cur_state;
  1014. mutex_lock(&dev_priv->sb_lock);
  1015. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1016. mutex_unlock(&dev_priv->sb_lock);
  1017. cur_state = val & DSI_PLL_VCO_EN;
  1018. I915_STATE_WARN(cur_state != state,
  1019. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1020. state_string(state), state_string(cur_state));
  1021. }
  1022. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1023. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1024. struct intel_shared_dpll *
  1025. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1026. {
  1027. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1028. if (crtc->config->shared_dpll < 0)
  1029. return NULL;
  1030. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1031. }
  1032. /* For ILK+ */
  1033. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1034. struct intel_shared_dpll *pll,
  1035. bool state)
  1036. {
  1037. bool cur_state;
  1038. struct intel_dpll_hw_state hw_state;
  1039. if (WARN (!pll,
  1040. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1041. return;
  1042. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1043. I915_STATE_WARN(cur_state != state,
  1044. "%s assertion failure (expected %s, current %s)\n",
  1045. pll->name, state_string(state), state_string(cur_state));
  1046. }
  1047. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1048. enum pipe pipe, bool state)
  1049. {
  1050. int reg;
  1051. u32 val;
  1052. bool cur_state;
  1053. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1054. pipe);
  1055. if (HAS_DDI(dev_priv->dev)) {
  1056. /* DDI does not have a specific FDI_TX register */
  1057. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  1058. val = I915_READ(reg);
  1059. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1060. } else {
  1061. reg = FDI_TX_CTL(pipe);
  1062. val = I915_READ(reg);
  1063. cur_state = !!(val & FDI_TX_ENABLE);
  1064. }
  1065. I915_STATE_WARN(cur_state != state,
  1066. "FDI TX state assertion failure (expected %s, current %s)\n",
  1067. state_string(state), state_string(cur_state));
  1068. }
  1069. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1070. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1071. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1072. enum pipe pipe, bool state)
  1073. {
  1074. int reg;
  1075. u32 val;
  1076. bool cur_state;
  1077. reg = FDI_RX_CTL(pipe);
  1078. val = I915_READ(reg);
  1079. cur_state = !!(val & FDI_RX_ENABLE);
  1080. I915_STATE_WARN(cur_state != state,
  1081. "FDI RX state assertion failure (expected %s, current %s)\n",
  1082. state_string(state), state_string(cur_state));
  1083. }
  1084. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1085. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1086. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1087. enum pipe pipe)
  1088. {
  1089. int reg;
  1090. u32 val;
  1091. /* ILK FDI PLL is always enabled */
  1092. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1093. return;
  1094. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1095. if (HAS_DDI(dev_priv->dev))
  1096. return;
  1097. reg = FDI_TX_CTL(pipe);
  1098. val = I915_READ(reg);
  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. int reg;
  1105. u32 val;
  1106. bool cur_state;
  1107. reg = FDI_RX_CTL(pipe);
  1108. val = I915_READ(reg);
  1109. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1110. I915_STATE_WARN(cur_state != state,
  1111. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1112. state_string(state), state_string(cur_state));
  1113. }
  1114. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. int pp_reg;
  1119. u32 val;
  1120. enum pipe panel_pipe = PIPE_A;
  1121. bool locked = true;
  1122. if (WARN_ON(HAS_DDI(dev)))
  1123. return;
  1124. if (HAS_PCH_SPLIT(dev)) {
  1125. u32 port_sel;
  1126. pp_reg = PCH_PP_CONTROL;
  1127. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1128. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1129. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1130. panel_pipe = PIPE_B;
  1131. /* XXX: else fix for eDP */
  1132. } else if (IS_VALLEYVIEW(dev)) {
  1133. /* presumably write lock depends on pipe, not port select */
  1134. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1135. panel_pipe = pipe;
  1136. } else {
  1137. pp_reg = PP_CONTROL;
  1138. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1139. panel_pipe = PIPE_B;
  1140. }
  1141. val = I915_READ(pp_reg);
  1142. if (!(val & PANEL_POWER_ON) ||
  1143. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1144. locked = false;
  1145. I915_STATE_WARN(panel_pipe == pipe && locked,
  1146. "panel assertion failure, pipe %c regs locked\n",
  1147. pipe_name(pipe));
  1148. }
  1149. static void assert_cursor(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, bool state)
  1151. {
  1152. struct drm_device *dev = dev_priv->dev;
  1153. bool cur_state;
  1154. if (IS_845G(dev) || IS_I865G(dev))
  1155. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1156. else
  1157. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1158. I915_STATE_WARN(cur_state != state,
  1159. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1160. pipe_name(pipe), state_string(state), state_string(cur_state));
  1161. }
  1162. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1163. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1164. void assert_pipe(struct drm_i915_private *dev_priv,
  1165. enum pipe pipe, bool state)
  1166. {
  1167. int reg;
  1168. u32 val;
  1169. bool cur_state;
  1170. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1171. pipe);
  1172. /* if we need the pipe quirk it must be always on */
  1173. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1174. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1175. state = true;
  1176. if (!intel_display_power_is_enabled(dev_priv,
  1177. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1178. cur_state = false;
  1179. } else {
  1180. reg = PIPECONF(cpu_transcoder);
  1181. val = I915_READ(reg);
  1182. cur_state = !!(val & PIPECONF_ENABLE);
  1183. }
  1184. I915_STATE_WARN(cur_state != state,
  1185. "pipe %c assertion failure (expected %s, current %s)\n",
  1186. pipe_name(pipe), state_string(state), state_string(cur_state));
  1187. }
  1188. static void assert_plane(struct drm_i915_private *dev_priv,
  1189. enum plane plane, bool state)
  1190. {
  1191. int reg;
  1192. u32 val;
  1193. bool cur_state;
  1194. reg = DSPCNTR(plane);
  1195. val = I915_READ(reg);
  1196. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1197. I915_STATE_WARN(cur_state != state,
  1198. "plane %c assertion failure (expected %s, current %s)\n",
  1199. plane_name(plane), state_string(state), state_string(cur_state));
  1200. }
  1201. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1202. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1203. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1204. enum pipe pipe)
  1205. {
  1206. struct drm_device *dev = dev_priv->dev;
  1207. int reg, i;
  1208. u32 val;
  1209. int cur_pipe;
  1210. /* Primary planes are fixed to pipes on gen4+ */
  1211. if (INTEL_INFO(dev)->gen >= 4) {
  1212. reg = DSPCNTR(pipe);
  1213. val = I915_READ(reg);
  1214. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1215. "plane %c assertion failure, should be disabled but not\n",
  1216. plane_name(pipe));
  1217. return;
  1218. }
  1219. /* Need to check both planes against the pipe */
  1220. for_each_pipe(dev_priv, i) {
  1221. reg = DSPCNTR(i);
  1222. val = I915_READ(reg);
  1223. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1224. DISPPLANE_SEL_PIPE_SHIFT;
  1225. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1226. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1227. plane_name(i), pipe_name(pipe));
  1228. }
  1229. }
  1230. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1231. enum pipe pipe)
  1232. {
  1233. struct drm_device *dev = dev_priv->dev;
  1234. int reg, sprite;
  1235. u32 val;
  1236. if (INTEL_INFO(dev)->gen >= 9) {
  1237. for_each_sprite(dev_priv, pipe, sprite) {
  1238. val = I915_READ(PLANE_CTL(pipe, sprite));
  1239. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1240. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1241. sprite, pipe_name(pipe));
  1242. }
  1243. } else if (IS_VALLEYVIEW(dev)) {
  1244. for_each_sprite(dev_priv, pipe, sprite) {
  1245. reg = SPCNTR(pipe, sprite);
  1246. val = I915_READ(reg);
  1247. I915_STATE_WARN(val & SP_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. sprite_name(pipe, sprite), pipe_name(pipe));
  1250. }
  1251. } else if (INTEL_INFO(dev)->gen >= 7) {
  1252. reg = SPRCTL(pipe);
  1253. val = I915_READ(reg);
  1254. I915_STATE_WARN(val & SPRITE_ENABLE,
  1255. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1256. plane_name(pipe), pipe_name(pipe));
  1257. } else if (INTEL_INFO(dev)->gen >= 5) {
  1258. reg = DVSCNTR(pipe);
  1259. val = I915_READ(reg);
  1260. I915_STATE_WARN(val & DVS_ENABLE,
  1261. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1262. plane_name(pipe), pipe_name(pipe));
  1263. }
  1264. }
  1265. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1266. {
  1267. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1268. drm_crtc_vblank_put(crtc);
  1269. }
  1270. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1271. {
  1272. u32 val;
  1273. bool enabled;
  1274. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1275. val = I915_READ(PCH_DREF_CONTROL);
  1276. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1277. DREF_SUPERSPREAD_SOURCE_MASK));
  1278. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1279. }
  1280. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1281. enum pipe pipe)
  1282. {
  1283. int reg;
  1284. u32 val;
  1285. bool enabled;
  1286. reg = PCH_TRANSCONF(pipe);
  1287. val = I915_READ(reg);
  1288. enabled = !!(val & TRANS_ENABLE);
  1289. I915_STATE_WARN(enabled,
  1290. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1291. pipe_name(pipe));
  1292. }
  1293. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1294. enum pipe pipe, u32 port_sel, u32 val)
  1295. {
  1296. if ((val & DP_PORT_EN) == 0)
  1297. return false;
  1298. if (HAS_PCH_CPT(dev_priv->dev)) {
  1299. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1300. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1301. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1302. return false;
  1303. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1304. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1305. return false;
  1306. } else {
  1307. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1308. return false;
  1309. }
  1310. return true;
  1311. }
  1312. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1313. enum pipe pipe, u32 val)
  1314. {
  1315. if ((val & SDVO_ENABLE) == 0)
  1316. return false;
  1317. if (HAS_PCH_CPT(dev_priv->dev)) {
  1318. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1319. return false;
  1320. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1321. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1322. return false;
  1323. } else {
  1324. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1325. return false;
  1326. }
  1327. return true;
  1328. }
  1329. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1330. enum pipe pipe, u32 val)
  1331. {
  1332. if ((val & LVDS_PORT_EN) == 0)
  1333. return false;
  1334. if (HAS_PCH_CPT(dev_priv->dev)) {
  1335. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1336. return false;
  1337. } else {
  1338. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1339. return false;
  1340. }
  1341. return true;
  1342. }
  1343. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1344. enum pipe pipe, u32 val)
  1345. {
  1346. if ((val & ADPA_DAC_ENABLE) == 0)
  1347. return false;
  1348. if (HAS_PCH_CPT(dev_priv->dev)) {
  1349. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1350. return false;
  1351. } else {
  1352. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1353. return false;
  1354. }
  1355. return true;
  1356. }
  1357. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1358. enum pipe pipe, int reg, u32 port_sel)
  1359. {
  1360. u32 val = I915_READ(reg);
  1361. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1362. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1363. reg, pipe_name(pipe));
  1364. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1365. && (val & DP_PIPEB_SELECT),
  1366. "IBX PCH dp port still using transcoder B\n");
  1367. }
  1368. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1369. enum pipe pipe, int reg)
  1370. {
  1371. u32 val = I915_READ(reg);
  1372. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1374. reg, pipe_name(pipe));
  1375. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1376. && (val & SDVO_PIPE_B_SELECT),
  1377. "IBX PCH hdmi port still using transcoder B\n");
  1378. }
  1379. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1380. enum pipe pipe)
  1381. {
  1382. int reg;
  1383. u32 val;
  1384. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1385. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1386. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1387. reg = PCH_ADPA;
  1388. val = I915_READ(reg);
  1389. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1390. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1391. pipe_name(pipe));
  1392. reg = PCH_LVDS;
  1393. val = I915_READ(reg);
  1394. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1395. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1396. pipe_name(pipe));
  1397. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1398. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1399. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1400. }
  1401. static void intel_init_dpio(struct drm_device *dev)
  1402. {
  1403. struct drm_i915_private *dev_priv = dev->dev_private;
  1404. if (!IS_VALLEYVIEW(dev))
  1405. return;
  1406. /*
  1407. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1408. * CHV x1 PHY (DP/HDMI D)
  1409. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1410. */
  1411. if (IS_CHERRYVIEW(dev)) {
  1412. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1413. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1414. } else {
  1415. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1416. }
  1417. }
  1418. static void vlv_enable_pll(struct intel_crtc *crtc,
  1419. const struct intel_crtc_state *pipe_config)
  1420. {
  1421. struct drm_device *dev = crtc->base.dev;
  1422. struct drm_i915_private *dev_priv = dev->dev_private;
  1423. int reg = DPLL(crtc->pipe);
  1424. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1425. assert_pipe_disabled(dev_priv, crtc->pipe);
  1426. /* No really, not for ILK+ */
  1427. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1428. /* PLL is protected by panel, make sure we can write it */
  1429. if (IS_MOBILE(dev_priv->dev))
  1430. assert_panel_unlocked(dev_priv, crtc->pipe);
  1431. I915_WRITE(reg, dpll);
  1432. POSTING_READ(reg);
  1433. udelay(150);
  1434. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1435. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1436. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1437. POSTING_READ(DPLL_MD(crtc->pipe));
  1438. /* We do this three times for luck */
  1439. I915_WRITE(reg, dpll);
  1440. POSTING_READ(reg);
  1441. udelay(150); /* wait for warmup */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. }
  1449. static void chv_enable_pll(struct intel_crtc *crtc,
  1450. const struct intel_crtc_state *pipe_config)
  1451. {
  1452. struct drm_device *dev = crtc->base.dev;
  1453. struct drm_i915_private *dev_priv = dev->dev_private;
  1454. int pipe = crtc->pipe;
  1455. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1456. u32 tmp;
  1457. assert_pipe_disabled(dev_priv, crtc->pipe);
  1458. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1459. mutex_lock(&dev_priv->sb_lock);
  1460. /* Enable back the 10bit clock to display controller */
  1461. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1462. tmp |= DPIO_DCLKP_EN;
  1463. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1464. mutex_unlock(&dev_priv->sb_lock);
  1465. /*
  1466. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1467. */
  1468. udelay(1);
  1469. /* Enable PLL */
  1470. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1471. /* Check PLL is locked */
  1472. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1473. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1474. /* not sure when this should be written */
  1475. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1476. POSTING_READ(DPLL_MD(pipe));
  1477. }
  1478. static int intel_num_dvo_pipes(struct drm_device *dev)
  1479. {
  1480. struct intel_crtc *crtc;
  1481. int count = 0;
  1482. for_each_intel_crtc(dev, crtc)
  1483. count += crtc->base.state->active &&
  1484. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1485. return count;
  1486. }
  1487. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1488. {
  1489. struct drm_device *dev = crtc->base.dev;
  1490. struct drm_i915_private *dev_priv = dev->dev_private;
  1491. int reg = DPLL(crtc->pipe);
  1492. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1493. assert_pipe_disabled(dev_priv, crtc->pipe);
  1494. /* No really, not for ILK+ */
  1495. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1496. /* PLL is protected by panel, make sure we can write it */
  1497. if (IS_MOBILE(dev) && !IS_I830(dev))
  1498. assert_panel_unlocked(dev_priv, crtc->pipe);
  1499. /* Enable DVO 2x clock on both PLLs if necessary */
  1500. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1501. /*
  1502. * It appears to be important that we don't enable this
  1503. * for the current pipe before otherwise configuring the
  1504. * PLL. No idea how this should be handled if multiple
  1505. * DVO outputs are enabled simultaneosly.
  1506. */
  1507. dpll |= DPLL_DVO_2X_MODE;
  1508. I915_WRITE(DPLL(!crtc->pipe),
  1509. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1510. }
  1511. /* Wait for the clocks to stabilize. */
  1512. POSTING_READ(reg);
  1513. udelay(150);
  1514. if (INTEL_INFO(dev)->gen >= 4) {
  1515. I915_WRITE(DPLL_MD(crtc->pipe),
  1516. crtc->config->dpll_hw_state.dpll_md);
  1517. } else {
  1518. /* The pixel multiplier can only be updated once the
  1519. * DPLL is enabled and the clocks are stable.
  1520. *
  1521. * So write it again.
  1522. */
  1523. I915_WRITE(reg, dpll);
  1524. }
  1525. /* We do this three times for luck */
  1526. I915_WRITE(reg, dpll);
  1527. POSTING_READ(reg);
  1528. udelay(150); /* wait for warmup */
  1529. I915_WRITE(reg, dpll);
  1530. POSTING_READ(reg);
  1531. udelay(150); /* wait for warmup */
  1532. I915_WRITE(reg, dpll);
  1533. POSTING_READ(reg);
  1534. udelay(150); /* wait for warmup */
  1535. }
  1536. /**
  1537. * i9xx_disable_pll - disable a PLL
  1538. * @dev_priv: i915 private structure
  1539. * @pipe: pipe PLL to disable
  1540. *
  1541. * Disable the PLL for @pipe, making sure the pipe is off first.
  1542. *
  1543. * Note! This is for pre-ILK only.
  1544. */
  1545. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1546. {
  1547. struct drm_device *dev = crtc->base.dev;
  1548. struct drm_i915_private *dev_priv = dev->dev_private;
  1549. enum pipe pipe = crtc->pipe;
  1550. /* Disable DVO 2x clock on both PLLs if necessary */
  1551. if (IS_I830(dev) &&
  1552. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1553. !intel_num_dvo_pipes(dev)) {
  1554. I915_WRITE(DPLL(PIPE_B),
  1555. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1556. I915_WRITE(DPLL(PIPE_A),
  1557. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1558. }
  1559. /* Don't disable pipe or pipe PLLs if needed */
  1560. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1561. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1562. return;
  1563. /* Make sure the pipe isn't still relying on us */
  1564. assert_pipe_disabled(dev_priv, pipe);
  1565. I915_WRITE(DPLL(pipe), 0);
  1566. POSTING_READ(DPLL(pipe));
  1567. }
  1568. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1569. {
  1570. u32 val = 0;
  1571. /* Make sure the pipe isn't still relying on us */
  1572. assert_pipe_disabled(dev_priv, pipe);
  1573. /*
  1574. * Leave integrated clock source and reference clock enabled for pipe B.
  1575. * The latter is needed for VGA hotplug / manual detection.
  1576. */
  1577. if (pipe == PIPE_B)
  1578. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1579. I915_WRITE(DPLL(pipe), val);
  1580. POSTING_READ(DPLL(pipe));
  1581. }
  1582. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1583. {
  1584. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1585. u32 val;
  1586. /* Make sure the pipe isn't still relying on us */
  1587. assert_pipe_disabled(dev_priv, pipe);
  1588. /* Set PLL en = 0 */
  1589. val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
  1590. if (pipe != PIPE_A)
  1591. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1592. I915_WRITE(DPLL(pipe), val);
  1593. POSTING_READ(DPLL(pipe));
  1594. mutex_lock(&dev_priv->sb_lock);
  1595. /* Disable 10bit clock to display controller */
  1596. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1597. val &= ~DPIO_DCLKP_EN;
  1598. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1599. /* disable left/right clock distribution */
  1600. if (pipe != PIPE_B) {
  1601. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
  1602. val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
  1603. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
  1604. } else {
  1605. val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
  1606. val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
  1607. vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
  1608. }
  1609. mutex_unlock(&dev_priv->sb_lock);
  1610. }
  1611. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1612. struct intel_digital_port *dport,
  1613. unsigned int expected_mask)
  1614. {
  1615. u32 port_mask;
  1616. int dpll_reg;
  1617. switch (dport->port) {
  1618. case PORT_B:
  1619. port_mask = DPLL_PORTB_READY_MASK;
  1620. dpll_reg = DPLL(0);
  1621. break;
  1622. case PORT_C:
  1623. port_mask = DPLL_PORTC_READY_MASK;
  1624. dpll_reg = DPLL(0);
  1625. expected_mask <<= 4;
  1626. break;
  1627. case PORT_D:
  1628. port_mask = DPLL_PORTD_READY_MASK;
  1629. dpll_reg = DPIO_PHY_STATUS;
  1630. break;
  1631. default:
  1632. BUG();
  1633. }
  1634. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1635. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1636. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1637. }
  1638. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1639. {
  1640. struct drm_device *dev = crtc->base.dev;
  1641. struct drm_i915_private *dev_priv = dev->dev_private;
  1642. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1643. if (WARN_ON(pll == NULL))
  1644. return;
  1645. WARN_ON(!pll->config.crtc_mask);
  1646. if (pll->active == 0) {
  1647. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1648. WARN_ON(pll->on);
  1649. assert_shared_dpll_disabled(dev_priv, pll);
  1650. pll->mode_set(dev_priv, pll);
  1651. }
  1652. }
  1653. /**
  1654. * intel_enable_shared_dpll - enable PCH PLL
  1655. * @dev_priv: i915 private structure
  1656. * @pipe: pipe PLL to enable
  1657. *
  1658. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1659. * drives the transcoder clock.
  1660. */
  1661. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1662. {
  1663. struct drm_device *dev = crtc->base.dev;
  1664. struct drm_i915_private *dev_priv = dev->dev_private;
  1665. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1666. if (WARN_ON(pll == NULL))
  1667. return;
  1668. if (WARN_ON(pll->config.crtc_mask == 0))
  1669. return;
  1670. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1671. pll->name, pll->active, pll->on,
  1672. crtc->base.base.id);
  1673. if (pll->active++) {
  1674. WARN_ON(!pll->on);
  1675. assert_shared_dpll_enabled(dev_priv, pll);
  1676. return;
  1677. }
  1678. WARN_ON(pll->on);
  1679. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1680. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1681. pll->enable(dev_priv, pll);
  1682. pll->on = true;
  1683. }
  1684. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1685. {
  1686. struct drm_device *dev = crtc->base.dev;
  1687. struct drm_i915_private *dev_priv = dev->dev_private;
  1688. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1689. /* PCH only available on ILK+ */
  1690. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1691. if (WARN_ON(pll == NULL))
  1692. return;
  1693. if (WARN_ON(pll->config.crtc_mask == 0))
  1694. return;
  1695. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1696. pll->name, pll->active, pll->on,
  1697. crtc->base.base.id);
  1698. if (WARN_ON(pll->active == 0)) {
  1699. assert_shared_dpll_disabled(dev_priv, pll);
  1700. return;
  1701. }
  1702. assert_shared_dpll_enabled(dev_priv, pll);
  1703. WARN_ON(!pll->on);
  1704. if (--pll->active)
  1705. return;
  1706. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1707. pll->disable(dev_priv, pll);
  1708. pll->on = false;
  1709. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1710. }
  1711. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1712. enum pipe pipe)
  1713. {
  1714. struct drm_device *dev = dev_priv->dev;
  1715. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1717. uint32_t reg, val, pipeconf_val;
  1718. /* PCH only available on ILK+ */
  1719. BUG_ON(!HAS_PCH_SPLIT(dev));
  1720. /* Make sure PCH DPLL is enabled */
  1721. assert_shared_dpll_enabled(dev_priv,
  1722. intel_crtc_to_shared_dpll(intel_crtc));
  1723. /* FDI must be feeding us bits for PCH ports */
  1724. assert_fdi_tx_enabled(dev_priv, pipe);
  1725. assert_fdi_rx_enabled(dev_priv, pipe);
  1726. if (HAS_PCH_CPT(dev)) {
  1727. /* Workaround: Set the timing override bit before enabling the
  1728. * pch transcoder. */
  1729. reg = TRANS_CHICKEN2(pipe);
  1730. val = I915_READ(reg);
  1731. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1732. I915_WRITE(reg, val);
  1733. }
  1734. reg = PCH_TRANSCONF(pipe);
  1735. val = I915_READ(reg);
  1736. pipeconf_val = I915_READ(PIPECONF(pipe));
  1737. if (HAS_PCH_IBX(dev_priv->dev)) {
  1738. /*
  1739. * Make the BPC in transcoder be consistent with
  1740. * that in pipeconf reg. For HDMI we must use 8bpc
  1741. * here for both 8bpc and 12bpc.
  1742. */
  1743. val &= ~PIPECONF_BPC_MASK;
  1744. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1745. val |= PIPECONF_8BPC;
  1746. else
  1747. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1748. }
  1749. val &= ~TRANS_INTERLACE_MASK;
  1750. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1751. if (HAS_PCH_IBX(dev_priv->dev) &&
  1752. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1753. val |= TRANS_LEGACY_INTERLACED_ILK;
  1754. else
  1755. val |= TRANS_INTERLACED;
  1756. else
  1757. val |= TRANS_PROGRESSIVE;
  1758. I915_WRITE(reg, val | TRANS_ENABLE);
  1759. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1760. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1761. }
  1762. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1763. enum transcoder cpu_transcoder)
  1764. {
  1765. u32 val, pipeconf_val;
  1766. /* PCH only available on ILK+ */
  1767. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1768. /* FDI must be feeding us bits for PCH ports */
  1769. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1770. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1771. /* Workaround: set timing override bit. */
  1772. val = I915_READ(_TRANSA_CHICKEN2);
  1773. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1774. I915_WRITE(_TRANSA_CHICKEN2, val);
  1775. val = TRANS_ENABLE;
  1776. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1777. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1778. PIPECONF_INTERLACED_ILK)
  1779. val |= TRANS_INTERLACED;
  1780. else
  1781. val |= TRANS_PROGRESSIVE;
  1782. I915_WRITE(LPT_TRANSCONF, val);
  1783. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1784. DRM_ERROR("Failed to enable PCH transcoder\n");
  1785. }
  1786. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1787. enum pipe pipe)
  1788. {
  1789. struct drm_device *dev = dev_priv->dev;
  1790. uint32_t reg, val;
  1791. /* FDI relies on the transcoder */
  1792. assert_fdi_tx_disabled(dev_priv, pipe);
  1793. assert_fdi_rx_disabled(dev_priv, pipe);
  1794. /* Ports must be off as well */
  1795. assert_pch_ports_disabled(dev_priv, pipe);
  1796. reg = PCH_TRANSCONF(pipe);
  1797. val = I915_READ(reg);
  1798. val &= ~TRANS_ENABLE;
  1799. I915_WRITE(reg, val);
  1800. /* wait for PCH transcoder off, transcoder state */
  1801. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1802. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1803. if (!HAS_PCH_IBX(dev)) {
  1804. /* Workaround: Clear the timing override chicken bit again. */
  1805. reg = TRANS_CHICKEN2(pipe);
  1806. val = I915_READ(reg);
  1807. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1808. I915_WRITE(reg, val);
  1809. }
  1810. }
  1811. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1812. {
  1813. u32 val;
  1814. val = I915_READ(LPT_TRANSCONF);
  1815. val &= ~TRANS_ENABLE;
  1816. I915_WRITE(LPT_TRANSCONF, val);
  1817. /* wait for PCH transcoder off, transcoder state */
  1818. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1819. DRM_ERROR("Failed to disable PCH transcoder\n");
  1820. /* Workaround: clear timing override bit. */
  1821. val = I915_READ(_TRANSA_CHICKEN2);
  1822. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1823. I915_WRITE(_TRANSA_CHICKEN2, val);
  1824. }
  1825. /**
  1826. * intel_enable_pipe - enable a pipe, asserting requirements
  1827. * @crtc: crtc responsible for the pipe
  1828. *
  1829. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1830. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1831. */
  1832. static void intel_enable_pipe(struct intel_crtc *crtc)
  1833. {
  1834. struct drm_device *dev = crtc->base.dev;
  1835. struct drm_i915_private *dev_priv = dev->dev_private;
  1836. enum pipe pipe = crtc->pipe;
  1837. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1838. pipe);
  1839. enum pipe pch_transcoder;
  1840. int reg;
  1841. u32 val;
  1842. assert_planes_disabled(dev_priv, pipe);
  1843. assert_cursor_disabled(dev_priv, pipe);
  1844. assert_sprites_disabled(dev_priv, pipe);
  1845. if (HAS_PCH_LPT(dev_priv->dev))
  1846. pch_transcoder = TRANSCODER_A;
  1847. else
  1848. pch_transcoder = pipe;
  1849. /*
  1850. * A pipe without a PLL won't actually be able to drive bits from
  1851. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1852. * need the check.
  1853. */
  1854. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1855. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1856. assert_dsi_pll_enabled(dev_priv);
  1857. else
  1858. assert_pll_enabled(dev_priv, pipe);
  1859. else {
  1860. if (crtc->config->has_pch_encoder) {
  1861. /* if driving the PCH, we need FDI enabled */
  1862. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1863. assert_fdi_tx_pll_enabled(dev_priv,
  1864. (enum pipe) cpu_transcoder);
  1865. }
  1866. /* FIXME: assert CPU port conditions for SNB+ */
  1867. }
  1868. reg = PIPECONF(cpu_transcoder);
  1869. val = I915_READ(reg);
  1870. if (val & PIPECONF_ENABLE) {
  1871. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1872. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1873. return;
  1874. }
  1875. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1876. POSTING_READ(reg);
  1877. }
  1878. /**
  1879. * intel_disable_pipe - disable a pipe, asserting requirements
  1880. * @crtc: crtc whose pipes is to be disabled
  1881. *
  1882. * Disable the pipe of @crtc, making sure that various hardware
  1883. * specific requirements are met, if applicable, e.g. plane
  1884. * disabled, panel fitter off, etc.
  1885. *
  1886. * Will wait until the pipe has shut down before returning.
  1887. */
  1888. static void intel_disable_pipe(struct intel_crtc *crtc)
  1889. {
  1890. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1891. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1892. enum pipe pipe = crtc->pipe;
  1893. int reg;
  1894. u32 val;
  1895. /*
  1896. * Make sure planes won't keep trying to pump pixels to us,
  1897. * or we might hang the display.
  1898. */
  1899. assert_planes_disabled(dev_priv, pipe);
  1900. assert_cursor_disabled(dev_priv, pipe);
  1901. assert_sprites_disabled(dev_priv, pipe);
  1902. reg = PIPECONF(cpu_transcoder);
  1903. val = I915_READ(reg);
  1904. if ((val & PIPECONF_ENABLE) == 0)
  1905. return;
  1906. /*
  1907. * Double wide has implications for planes
  1908. * so best keep it disabled when not needed.
  1909. */
  1910. if (crtc->config->double_wide)
  1911. val &= ~PIPECONF_DOUBLE_WIDE;
  1912. /* Don't disable pipe or pipe PLLs if needed */
  1913. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1914. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1915. val &= ~PIPECONF_ENABLE;
  1916. I915_WRITE(reg, val);
  1917. if ((val & PIPECONF_ENABLE) == 0)
  1918. intel_wait_for_pipe_off(crtc);
  1919. }
  1920. /**
  1921. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1922. * @plane: plane to be enabled
  1923. * @crtc: crtc for the plane
  1924. *
  1925. * Enable @plane on @crtc, making sure that the pipe is running first.
  1926. */
  1927. static void intel_enable_primary_hw_plane(struct drm_plane *plane,
  1928. struct drm_crtc *crtc)
  1929. {
  1930. struct drm_device *dev = plane->dev;
  1931. struct drm_i915_private *dev_priv = dev->dev_private;
  1932. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1933. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1934. assert_pipe_enabled(dev_priv, intel_crtc->pipe);
  1935. to_intel_plane_state(plane->state)->visible = true;
  1936. dev_priv->display.update_primary_plane(crtc, plane->fb,
  1937. crtc->x, crtc->y);
  1938. }
  1939. static bool need_vtd_wa(struct drm_device *dev)
  1940. {
  1941. #ifdef CONFIG_INTEL_IOMMU
  1942. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1943. return true;
  1944. #endif
  1945. return false;
  1946. }
  1947. unsigned int
  1948. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1949. uint64_t fb_format_modifier)
  1950. {
  1951. unsigned int tile_height;
  1952. uint32_t pixel_bytes;
  1953. switch (fb_format_modifier) {
  1954. case DRM_FORMAT_MOD_NONE:
  1955. tile_height = 1;
  1956. break;
  1957. case I915_FORMAT_MOD_X_TILED:
  1958. tile_height = IS_GEN2(dev) ? 16 : 8;
  1959. break;
  1960. case I915_FORMAT_MOD_Y_TILED:
  1961. tile_height = 32;
  1962. break;
  1963. case I915_FORMAT_MOD_Yf_TILED:
  1964. pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
  1965. switch (pixel_bytes) {
  1966. default:
  1967. case 1:
  1968. tile_height = 64;
  1969. break;
  1970. case 2:
  1971. case 4:
  1972. tile_height = 32;
  1973. break;
  1974. case 8:
  1975. tile_height = 16;
  1976. break;
  1977. case 16:
  1978. WARN_ONCE(1,
  1979. "128-bit pixels are not supported for display!");
  1980. tile_height = 16;
  1981. break;
  1982. }
  1983. break;
  1984. default:
  1985. MISSING_CASE(fb_format_modifier);
  1986. tile_height = 1;
  1987. break;
  1988. }
  1989. return tile_height;
  1990. }
  1991. unsigned int
  1992. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1993. uint32_t pixel_format, uint64_t fb_format_modifier)
  1994. {
  1995. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1996. fb_format_modifier));
  1997. }
  1998. static int
  1999. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  2000. const struct drm_plane_state *plane_state)
  2001. {
  2002. struct intel_rotation_info *info = &view->rotation_info;
  2003. *view = i915_ggtt_view_normal;
  2004. if (!plane_state)
  2005. return 0;
  2006. if (!intel_rotation_90_or_270(plane_state->rotation))
  2007. return 0;
  2008. *view = i915_ggtt_view_rotated;
  2009. info->height = fb->height;
  2010. info->pixel_format = fb->pixel_format;
  2011. info->pitch = fb->pitches[0];
  2012. info->fb_modifier = fb->modifier[0];
  2013. return 0;
  2014. }
  2015. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  2016. {
  2017. if (INTEL_INFO(dev_priv)->gen >= 9)
  2018. return 256 * 1024;
  2019. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  2020. IS_VALLEYVIEW(dev_priv))
  2021. return 128 * 1024;
  2022. else if (INTEL_INFO(dev_priv)->gen >= 4)
  2023. return 4 * 1024;
  2024. else
  2025. return 0;
  2026. }
  2027. int
  2028. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  2029. struct drm_framebuffer *fb,
  2030. const struct drm_plane_state *plane_state,
  2031. struct intel_engine_cs *pipelined)
  2032. {
  2033. struct drm_device *dev = fb->dev;
  2034. struct drm_i915_private *dev_priv = dev->dev_private;
  2035. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2036. struct i915_ggtt_view view;
  2037. u32 alignment;
  2038. int ret;
  2039. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  2040. switch (fb->modifier[0]) {
  2041. case DRM_FORMAT_MOD_NONE:
  2042. alignment = intel_linear_alignment(dev_priv);
  2043. break;
  2044. case I915_FORMAT_MOD_X_TILED:
  2045. if (INTEL_INFO(dev)->gen >= 9)
  2046. alignment = 256 * 1024;
  2047. else {
  2048. /* pin() will align the object as required by fence */
  2049. alignment = 0;
  2050. }
  2051. break;
  2052. case I915_FORMAT_MOD_Y_TILED:
  2053. case I915_FORMAT_MOD_Yf_TILED:
  2054. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2055. "Y tiling bo slipped through, driver bug!\n"))
  2056. return -EINVAL;
  2057. alignment = 1 * 1024 * 1024;
  2058. break;
  2059. default:
  2060. MISSING_CASE(fb->modifier[0]);
  2061. return -EINVAL;
  2062. }
  2063. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2064. if (ret)
  2065. return ret;
  2066. /* Note that the w/a also requires 64 PTE of padding following the
  2067. * bo. We currently fill all unused PTE with the shadow page and so
  2068. * we should always have valid PTE following the scanout preventing
  2069. * the VT-d warning.
  2070. */
  2071. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2072. alignment = 256 * 1024;
  2073. /*
  2074. * Global gtt pte registers are special registers which actually forward
  2075. * writes to a chunk of system memory. Which means that there is no risk
  2076. * that the register values disappear as soon as we call
  2077. * intel_runtime_pm_put(), so it is correct to wrap only the
  2078. * pin/unpin/fence and not more.
  2079. */
  2080. intel_runtime_pm_get(dev_priv);
  2081. dev_priv->mm.interruptible = false;
  2082. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
  2083. &view);
  2084. if (ret)
  2085. goto err_interruptible;
  2086. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2087. * fence, whereas 965+ only requires a fence if using
  2088. * framebuffer compression. For simplicity, we always install
  2089. * a fence as the cost is not that onerous.
  2090. */
  2091. ret = i915_gem_object_get_fence(obj);
  2092. if (ret)
  2093. goto err_unpin;
  2094. i915_gem_object_pin_fence(obj);
  2095. dev_priv->mm.interruptible = true;
  2096. intel_runtime_pm_put(dev_priv);
  2097. return 0;
  2098. err_unpin:
  2099. i915_gem_object_unpin_from_display_plane(obj, &view);
  2100. err_interruptible:
  2101. dev_priv->mm.interruptible = true;
  2102. intel_runtime_pm_put(dev_priv);
  2103. return ret;
  2104. }
  2105. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2106. const struct drm_plane_state *plane_state)
  2107. {
  2108. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2109. struct i915_ggtt_view view;
  2110. int ret;
  2111. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2112. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2113. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2114. i915_gem_object_unpin_fence(obj);
  2115. i915_gem_object_unpin_from_display_plane(obj, &view);
  2116. }
  2117. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2118. * is assumed to be a power-of-two. */
  2119. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2120. int *x, int *y,
  2121. unsigned int tiling_mode,
  2122. unsigned int cpp,
  2123. unsigned int pitch)
  2124. {
  2125. if (tiling_mode != I915_TILING_NONE) {
  2126. unsigned int tile_rows, tiles;
  2127. tile_rows = *y / 8;
  2128. *y %= 8;
  2129. tiles = *x / (512/cpp);
  2130. *x %= 512/cpp;
  2131. return tile_rows * pitch * 8 + tiles * 4096;
  2132. } else {
  2133. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2134. unsigned int offset;
  2135. offset = *y * pitch + *x * cpp;
  2136. *y = (offset & alignment) / pitch;
  2137. *x = ((offset & alignment) - *y * pitch) / cpp;
  2138. return offset & ~alignment;
  2139. }
  2140. }
  2141. static int i9xx_format_to_fourcc(int format)
  2142. {
  2143. switch (format) {
  2144. case DISPPLANE_8BPP:
  2145. return DRM_FORMAT_C8;
  2146. case DISPPLANE_BGRX555:
  2147. return DRM_FORMAT_XRGB1555;
  2148. case DISPPLANE_BGRX565:
  2149. return DRM_FORMAT_RGB565;
  2150. default:
  2151. case DISPPLANE_BGRX888:
  2152. return DRM_FORMAT_XRGB8888;
  2153. case DISPPLANE_RGBX888:
  2154. return DRM_FORMAT_XBGR8888;
  2155. case DISPPLANE_BGRX101010:
  2156. return DRM_FORMAT_XRGB2101010;
  2157. case DISPPLANE_RGBX101010:
  2158. return DRM_FORMAT_XBGR2101010;
  2159. }
  2160. }
  2161. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2162. {
  2163. switch (format) {
  2164. case PLANE_CTL_FORMAT_RGB_565:
  2165. return DRM_FORMAT_RGB565;
  2166. default:
  2167. case PLANE_CTL_FORMAT_XRGB_8888:
  2168. if (rgb_order) {
  2169. if (alpha)
  2170. return DRM_FORMAT_ABGR8888;
  2171. else
  2172. return DRM_FORMAT_XBGR8888;
  2173. } else {
  2174. if (alpha)
  2175. return DRM_FORMAT_ARGB8888;
  2176. else
  2177. return DRM_FORMAT_XRGB8888;
  2178. }
  2179. case PLANE_CTL_FORMAT_XRGB_2101010:
  2180. if (rgb_order)
  2181. return DRM_FORMAT_XBGR2101010;
  2182. else
  2183. return DRM_FORMAT_XRGB2101010;
  2184. }
  2185. }
  2186. static bool
  2187. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2188. struct intel_initial_plane_config *plane_config)
  2189. {
  2190. struct drm_device *dev = crtc->base.dev;
  2191. struct drm_i915_gem_object *obj = NULL;
  2192. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2193. struct drm_framebuffer *fb = &plane_config->fb->base;
  2194. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2195. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2196. PAGE_SIZE);
  2197. size_aligned -= base_aligned;
  2198. if (plane_config->size == 0)
  2199. return false;
  2200. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2201. base_aligned,
  2202. base_aligned,
  2203. size_aligned);
  2204. if (!obj)
  2205. return false;
  2206. obj->tiling_mode = plane_config->tiling;
  2207. if (obj->tiling_mode == I915_TILING_X)
  2208. obj->stride = fb->pitches[0];
  2209. mode_cmd.pixel_format = fb->pixel_format;
  2210. mode_cmd.width = fb->width;
  2211. mode_cmd.height = fb->height;
  2212. mode_cmd.pitches[0] = fb->pitches[0];
  2213. mode_cmd.modifier[0] = fb->modifier[0];
  2214. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2215. mutex_lock(&dev->struct_mutex);
  2216. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2217. &mode_cmd, obj)) {
  2218. DRM_DEBUG_KMS("intel fb init failed\n");
  2219. goto out_unref_obj;
  2220. }
  2221. mutex_unlock(&dev->struct_mutex);
  2222. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2223. return true;
  2224. out_unref_obj:
  2225. drm_gem_object_unreference(&obj->base);
  2226. mutex_unlock(&dev->struct_mutex);
  2227. return false;
  2228. }
  2229. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2230. static void
  2231. update_state_fb(struct drm_plane *plane)
  2232. {
  2233. if (plane->fb == plane->state->fb)
  2234. return;
  2235. if (plane->state->fb)
  2236. drm_framebuffer_unreference(plane->state->fb);
  2237. plane->state->fb = plane->fb;
  2238. if (plane->state->fb)
  2239. drm_framebuffer_reference(plane->state->fb);
  2240. }
  2241. static void
  2242. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2243. struct intel_initial_plane_config *plane_config)
  2244. {
  2245. struct drm_device *dev = intel_crtc->base.dev;
  2246. struct drm_i915_private *dev_priv = dev->dev_private;
  2247. struct drm_crtc *c;
  2248. struct intel_crtc *i;
  2249. struct drm_i915_gem_object *obj;
  2250. struct drm_plane *primary = intel_crtc->base.primary;
  2251. struct drm_framebuffer *fb;
  2252. if (!plane_config->fb)
  2253. return;
  2254. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2255. fb = &plane_config->fb->base;
  2256. goto valid_fb;
  2257. }
  2258. kfree(plane_config->fb);
  2259. /*
  2260. * Failed to alloc the obj, check to see if we should share
  2261. * an fb with another CRTC instead
  2262. */
  2263. for_each_crtc(dev, c) {
  2264. i = to_intel_crtc(c);
  2265. if (c == &intel_crtc->base)
  2266. continue;
  2267. if (!i->active)
  2268. continue;
  2269. fb = c->primary->fb;
  2270. if (!fb)
  2271. continue;
  2272. obj = intel_fb_obj(fb);
  2273. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2274. drm_framebuffer_reference(fb);
  2275. goto valid_fb;
  2276. }
  2277. }
  2278. return;
  2279. valid_fb:
  2280. obj = intel_fb_obj(fb);
  2281. if (obj->tiling_mode != I915_TILING_NONE)
  2282. dev_priv->preserve_bios_swizzle = true;
  2283. primary->fb = fb;
  2284. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2285. update_state_fb(primary);
  2286. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2287. obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  2288. }
  2289. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2290. struct drm_framebuffer *fb,
  2291. int x, int y)
  2292. {
  2293. struct drm_device *dev = crtc->dev;
  2294. struct drm_i915_private *dev_priv = dev->dev_private;
  2295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2296. struct drm_plane *primary = crtc->primary;
  2297. bool visible = to_intel_plane_state(primary->state)->visible;
  2298. struct drm_i915_gem_object *obj;
  2299. int plane = intel_crtc->plane;
  2300. unsigned long linear_offset;
  2301. u32 dspcntr;
  2302. u32 reg = DSPCNTR(plane);
  2303. int pixel_size;
  2304. if (!visible || !fb) {
  2305. I915_WRITE(reg, 0);
  2306. if (INTEL_INFO(dev)->gen >= 4)
  2307. I915_WRITE(DSPSURF(plane), 0);
  2308. else
  2309. I915_WRITE(DSPADDR(plane), 0);
  2310. POSTING_READ(reg);
  2311. return;
  2312. }
  2313. obj = intel_fb_obj(fb);
  2314. if (WARN_ON(obj == NULL))
  2315. return;
  2316. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2317. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2318. dspcntr |= DISPLAY_PLANE_ENABLE;
  2319. if (INTEL_INFO(dev)->gen < 4) {
  2320. if (intel_crtc->pipe == PIPE_B)
  2321. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2322. /* pipesrc and dspsize control the size that is scaled from,
  2323. * which should always be the user's requested size.
  2324. */
  2325. I915_WRITE(DSPSIZE(plane),
  2326. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2327. (intel_crtc->config->pipe_src_w - 1));
  2328. I915_WRITE(DSPPOS(plane), 0);
  2329. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2330. I915_WRITE(PRIMSIZE(plane),
  2331. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2332. (intel_crtc->config->pipe_src_w - 1));
  2333. I915_WRITE(PRIMPOS(plane), 0);
  2334. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2335. }
  2336. switch (fb->pixel_format) {
  2337. case DRM_FORMAT_C8:
  2338. dspcntr |= DISPPLANE_8BPP;
  2339. break;
  2340. case DRM_FORMAT_XRGB1555:
  2341. dspcntr |= DISPPLANE_BGRX555;
  2342. break;
  2343. case DRM_FORMAT_RGB565:
  2344. dspcntr |= DISPPLANE_BGRX565;
  2345. break;
  2346. case DRM_FORMAT_XRGB8888:
  2347. dspcntr |= DISPPLANE_BGRX888;
  2348. break;
  2349. case DRM_FORMAT_XBGR8888:
  2350. dspcntr |= DISPPLANE_RGBX888;
  2351. break;
  2352. case DRM_FORMAT_XRGB2101010:
  2353. dspcntr |= DISPPLANE_BGRX101010;
  2354. break;
  2355. case DRM_FORMAT_XBGR2101010:
  2356. dspcntr |= DISPPLANE_RGBX101010;
  2357. break;
  2358. default:
  2359. BUG();
  2360. }
  2361. if (INTEL_INFO(dev)->gen >= 4 &&
  2362. obj->tiling_mode != I915_TILING_NONE)
  2363. dspcntr |= DISPPLANE_TILED;
  2364. if (IS_G4X(dev))
  2365. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2366. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2367. if (INTEL_INFO(dev)->gen >= 4) {
  2368. intel_crtc->dspaddr_offset =
  2369. intel_gen4_compute_page_offset(dev_priv,
  2370. &x, &y, obj->tiling_mode,
  2371. pixel_size,
  2372. fb->pitches[0]);
  2373. linear_offset -= intel_crtc->dspaddr_offset;
  2374. } else {
  2375. intel_crtc->dspaddr_offset = linear_offset;
  2376. }
  2377. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2378. dspcntr |= DISPPLANE_ROTATE_180;
  2379. x += (intel_crtc->config->pipe_src_w - 1);
  2380. y += (intel_crtc->config->pipe_src_h - 1);
  2381. /* Finding the last pixel of the last line of the display
  2382. data and adding to linear_offset*/
  2383. linear_offset +=
  2384. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2385. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2386. }
  2387. I915_WRITE(reg, dspcntr);
  2388. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2389. if (INTEL_INFO(dev)->gen >= 4) {
  2390. I915_WRITE(DSPSURF(plane),
  2391. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2392. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2393. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2394. } else
  2395. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2396. POSTING_READ(reg);
  2397. }
  2398. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2399. struct drm_framebuffer *fb,
  2400. int x, int y)
  2401. {
  2402. struct drm_device *dev = crtc->dev;
  2403. struct drm_i915_private *dev_priv = dev->dev_private;
  2404. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2405. struct drm_plane *primary = crtc->primary;
  2406. bool visible = to_intel_plane_state(primary->state)->visible;
  2407. struct drm_i915_gem_object *obj;
  2408. int plane = intel_crtc->plane;
  2409. unsigned long linear_offset;
  2410. u32 dspcntr;
  2411. u32 reg = DSPCNTR(plane);
  2412. int pixel_size;
  2413. if (!visible || !fb) {
  2414. I915_WRITE(reg, 0);
  2415. I915_WRITE(DSPSURF(plane), 0);
  2416. POSTING_READ(reg);
  2417. return;
  2418. }
  2419. obj = intel_fb_obj(fb);
  2420. if (WARN_ON(obj == NULL))
  2421. return;
  2422. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2423. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2424. dspcntr |= DISPLAY_PLANE_ENABLE;
  2425. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2426. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2427. switch (fb->pixel_format) {
  2428. case DRM_FORMAT_C8:
  2429. dspcntr |= DISPPLANE_8BPP;
  2430. break;
  2431. case DRM_FORMAT_RGB565:
  2432. dspcntr |= DISPPLANE_BGRX565;
  2433. break;
  2434. case DRM_FORMAT_XRGB8888:
  2435. dspcntr |= DISPPLANE_BGRX888;
  2436. break;
  2437. case DRM_FORMAT_XBGR8888:
  2438. dspcntr |= DISPPLANE_RGBX888;
  2439. break;
  2440. case DRM_FORMAT_XRGB2101010:
  2441. dspcntr |= DISPPLANE_BGRX101010;
  2442. break;
  2443. case DRM_FORMAT_XBGR2101010:
  2444. dspcntr |= DISPPLANE_RGBX101010;
  2445. break;
  2446. default:
  2447. BUG();
  2448. }
  2449. if (obj->tiling_mode != I915_TILING_NONE)
  2450. dspcntr |= DISPPLANE_TILED;
  2451. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2452. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2453. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2454. intel_crtc->dspaddr_offset =
  2455. intel_gen4_compute_page_offset(dev_priv,
  2456. &x, &y, obj->tiling_mode,
  2457. pixel_size,
  2458. fb->pitches[0]);
  2459. linear_offset -= intel_crtc->dspaddr_offset;
  2460. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2461. dspcntr |= DISPPLANE_ROTATE_180;
  2462. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2463. x += (intel_crtc->config->pipe_src_w - 1);
  2464. y += (intel_crtc->config->pipe_src_h - 1);
  2465. /* Finding the last pixel of the last line of the display
  2466. data and adding to linear_offset*/
  2467. linear_offset +=
  2468. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2469. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2470. }
  2471. }
  2472. I915_WRITE(reg, dspcntr);
  2473. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2474. I915_WRITE(DSPSURF(plane),
  2475. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2476. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2477. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2478. } else {
  2479. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2480. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2481. }
  2482. POSTING_READ(reg);
  2483. }
  2484. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2485. uint32_t pixel_format)
  2486. {
  2487. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2488. /*
  2489. * The stride is either expressed as a multiple of 64 bytes
  2490. * chunks for linear buffers or in number of tiles for tiled
  2491. * buffers.
  2492. */
  2493. switch (fb_modifier) {
  2494. case DRM_FORMAT_MOD_NONE:
  2495. return 64;
  2496. case I915_FORMAT_MOD_X_TILED:
  2497. if (INTEL_INFO(dev)->gen == 2)
  2498. return 128;
  2499. return 512;
  2500. case I915_FORMAT_MOD_Y_TILED:
  2501. /* No need to check for old gens and Y tiling since this is
  2502. * about the display engine and those will be blocked before
  2503. * we get here.
  2504. */
  2505. return 128;
  2506. case I915_FORMAT_MOD_Yf_TILED:
  2507. if (bits_per_pixel == 8)
  2508. return 64;
  2509. else
  2510. return 128;
  2511. default:
  2512. MISSING_CASE(fb_modifier);
  2513. return 64;
  2514. }
  2515. }
  2516. unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
  2517. struct drm_i915_gem_object *obj)
  2518. {
  2519. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2520. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2521. view = &i915_ggtt_view_rotated;
  2522. return i915_gem_obj_ggtt_offset_view(obj, view);
  2523. }
  2524. /*
  2525. * This function detaches (aka. unbinds) unused scalers in hardware
  2526. */
  2527. void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2528. {
  2529. struct drm_device *dev;
  2530. struct drm_i915_private *dev_priv;
  2531. struct intel_crtc_scaler_state *scaler_state;
  2532. int i;
  2533. if (!intel_crtc || !intel_crtc->config)
  2534. return;
  2535. dev = intel_crtc->base.dev;
  2536. dev_priv = dev->dev_private;
  2537. scaler_state = &intel_crtc->config->scaler_state;
  2538. /* loop through and disable scalers that aren't in use */
  2539. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2540. if (!scaler_state->scalers[i].in_use) {
  2541. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2542. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2543. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2544. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2545. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2546. }
  2547. }
  2548. }
  2549. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2550. {
  2551. switch (pixel_format) {
  2552. case DRM_FORMAT_C8:
  2553. return PLANE_CTL_FORMAT_INDEXED;
  2554. case DRM_FORMAT_RGB565:
  2555. return PLANE_CTL_FORMAT_RGB_565;
  2556. case DRM_FORMAT_XBGR8888:
  2557. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2558. case DRM_FORMAT_XRGB8888:
  2559. return PLANE_CTL_FORMAT_XRGB_8888;
  2560. /*
  2561. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2562. * to be already pre-multiplied. We need to add a knob (or a different
  2563. * DRM_FORMAT) for user-space to configure that.
  2564. */
  2565. case DRM_FORMAT_ABGR8888:
  2566. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2567. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2568. case DRM_FORMAT_ARGB8888:
  2569. return PLANE_CTL_FORMAT_XRGB_8888 |
  2570. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2571. case DRM_FORMAT_XRGB2101010:
  2572. return PLANE_CTL_FORMAT_XRGB_2101010;
  2573. case DRM_FORMAT_XBGR2101010:
  2574. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2575. case DRM_FORMAT_YUYV:
  2576. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2577. case DRM_FORMAT_YVYU:
  2578. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2579. case DRM_FORMAT_UYVY:
  2580. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2581. case DRM_FORMAT_VYUY:
  2582. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2583. default:
  2584. MISSING_CASE(pixel_format);
  2585. }
  2586. return 0;
  2587. }
  2588. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2589. {
  2590. switch (fb_modifier) {
  2591. case DRM_FORMAT_MOD_NONE:
  2592. break;
  2593. case I915_FORMAT_MOD_X_TILED:
  2594. return PLANE_CTL_TILED_X;
  2595. case I915_FORMAT_MOD_Y_TILED:
  2596. return PLANE_CTL_TILED_Y;
  2597. case I915_FORMAT_MOD_Yf_TILED:
  2598. return PLANE_CTL_TILED_YF;
  2599. default:
  2600. MISSING_CASE(fb_modifier);
  2601. }
  2602. return 0;
  2603. }
  2604. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2605. {
  2606. switch (rotation) {
  2607. case BIT(DRM_ROTATE_0):
  2608. break;
  2609. /*
  2610. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2611. * while i915 HW rotation is clockwise, thats why this swapping.
  2612. */
  2613. case BIT(DRM_ROTATE_90):
  2614. return PLANE_CTL_ROTATE_270;
  2615. case BIT(DRM_ROTATE_180):
  2616. return PLANE_CTL_ROTATE_180;
  2617. case BIT(DRM_ROTATE_270):
  2618. return PLANE_CTL_ROTATE_90;
  2619. default:
  2620. MISSING_CASE(rotation);
  2621. }
  2622. return 0;
  2623. }
  2624. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2625. struct drm_framebuffer *fb,
  2626. int x, int y)
  2627. {
  2628. struct drm_device *dev = crtc->dev;
  2629. struct drm_i915_private *dev_priv = dev->dev_private;
  2630. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2631. struct drm_plane *plane = crtc->primary;
  2632. bool visible = to_intel_plane_state(plane->state)->visible;
  2633. struct drm_i915_gem_object *obj;
  2634. int pipe = intel_crtc->pipe;
  2635. u32 plane_ctl, stride_div, stride;
  2636. u32 tile_height, plane_offset, plane_size;
  2637. unsigned int rotation;
  2638. int x_offset, y_offset;
  2639. unsigned long surf_addr;
  2640. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2641. struct intel_plane_state *plane_state;
  2642. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2643. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2644. int scaler_id = -1;
  2645. plane_state = to_intel_plane_state(plane->state);
  2646. if (!visible || !fb) {
  2647. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2648. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2649. POSTING_READ(PLANE_CTL(pipe, 0));
  2650. return;
  2651. }
  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. rotation = plane->state->rotation;
  2659. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2660. obj = intel_fb_obj(fb);
  2661. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2662. fb->pixel_format);
  2663. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2664. /*
  2665. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2666. * update_plane helpers are called from legacy paths.
  2667. * Once full atomic crtc is available, below check can be avoided.
  2668. */
  2669. if (drm_rect_width(&plane_state->src)) {
  2670. scaler_id = plane_state->scaler_id;
  2671. src_x = plane_state->src.x1 >> 16;
  2672. src_y = plane_state->src.y1 >> 16;
  2673. src_w = drm_rect_width(&plane_state->src) >> 16;
  2674. src_h = drm_rect_height(&plane_state->src) >> 16;
  2675. dst_x = plane_state->dst.x1;
  2676. dst_y = plane_state->dst.y1;
  2677. dst_w = drm_rect_width(&plane_state->dst);
  2678. dst_h = drm_rect_height(&plane_state->dst);
  2679. WARN_ON(x != src_x || y != src_y);
  2680. } else {
  2681. src_w = intel_crtc->config->pipe_src_w;
  2682. src_h = intel_crtc->config->pipe_src_h;
  2683. }
  2684. if (intel_rotation_90_or_270(rotation)) {
  2685. /* stride = Surface height in tiles */
  2686. tile_height = intel_tile_height(dev, fb->pixel_format,
  2687. fb->modifier[0]);
  2688. stride = DIV_ROUND_UP(fb->height, tile_height);
  2689. x_offset = stride * tile_height - y - src_h;
  2690. y_offset = x;
  2691. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2692. } else {
  2693. stride = fb->pitches[0] / stride_div;
  2694. x_offset = x;
  2695. y_offset = y;
  2696. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2697. }
  2698. plane_offset = y_offset << 16 | x_offset;
  2699. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2700. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2701. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2702. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2703. if (scaler_id >= 0) {
  2704. uint32_t ps_ctrl = 0;
  2705. WARN_ON(!dst_w || !dst_h);
  2706. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2707. crtc_state->scaler_state.scalers[scaler_id].mode;
  2708. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2709. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2710. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2711. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2712. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2713. } else {
  2714. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2715. }
  2716. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2717. POSTING_READ(PLANE_SURF(pipe, 0));
  2718. }
  2719. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2720. static int
  2721. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2722. int x, int y, enum mode_set_atomic state)
  2723. {
  2724. struct drm_device *dev = crtc->dev;
  2725. struct drm_i915_private *dev_priv = dev->dev_private;
  2726. if (dev_priv->display.disable_fbc)
  2727. dev_priv->display.disable_fbc(dev);
  2728. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2729. return 0;
  2730. }
  2731. static void intel_complete_page_flips(struct drm_device *dev)
  2732. {
  2733. struct drm_crtc *crtc;
  2734. for_each_crtc(dev, crtc) {
  2735. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2736. enum plane plane = intel_crtc->plane;
  2737. intel_prepare_page_flip(dev, plane);
  2738. intel_finish_page_flip_plane(dev, plane);
  2739. }
  2740. }
  2741. static void intel_update_primary_planes(struct drm_device *dev)
  2742. {
  2743. struct drm_i915_private *dev_priv = dev->dev_private;
  2744. struct drm_crtc *crtc;
  2745. for_each_crtc(dev, crtc) {
  2746. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2747. drm_modeset_lock(&crtc->mutex, NULL);
  2748. /*
  2749. * FIXME: Once we have proper support for primary planes (and
  2750. * disabling them without disabling the entire crtc) allow again
  2751. * a NULL crtc->primary->fb.
  2752. */
  2753. if (intel_crtc->active && crtc->primary->fb)
  2754. dev_priv->display.update_primary_plane(crtc,
  2755. crtc->primary->fb,
  2756. crtc->x,
  2757. crtc->y);
  2758. drm_modeset_unlock(&crtc->mutex);
  2759. }
  2760. }
  2761. void intel_prepare_reset(struct drm_device *dev)
  2762. {
  2763. /* no reset support for gen2 */
  2764. if (IS_GEN2(dev))
  2765. return;
  2766. /* reset doesn't touch the display */
  2767. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2768. return;
  2769. drm_modeset_lock_all(dev);
  2770. /*
  2771. * Disabling the crtcs gracefully seems nicer. Also the
  2772. * g33 docs say we should at least disable all the planes.
  2773. */
  2774. intel_display_suspend(dev);
  2775. }
  2776. void intel_finish_reset(struct drm_device *dev)
  2777. {
  2778. struct drm_i915_private *dev_priv = to_i915(dev);
  2779. /*
  2780. * Flips in the rings will be nuked by the reset,
  2781. * so complete all pending flips so that user space
  2782. * will get its events and not get stuck.
  2783. */
  2784. intel_complete_page_flips(dev);
  2785. /* no reset support for gen2 */
  2786. if (IS_GEN2(dev))
  2787. return;
  2788. /* reset doesn't touch the display */
  2789. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2790. /*
  2791. * Flips in the rings have been nuked by the reset,
  2792. * so update the base address of all primary
  2793. * planes to the the last fb to make sure we're
  2794. * showing the correct fb after a reset.
  2795. */
  2796. intel_update_primary_planes(dev);
  2797. return;
  2798. }
  2799. /*
  2800. * The display has been reset as well,
  2801. * so need a full re-initialization.
  2802. */
  2803. intel_runtime_pm_disable_interrupts(dev_priv);
  2804. intel_runtime_pm_enable_interrupts(dev_priv);
  2805. intel_modeset_init_hw(dev);
  2806. spin_lock_irq(&dev_priv->irq_lock);
  2807. if (dev_priv->display.hpd_irq_setup)
  2808. dev_priv->display.hpd_irq_setup(dev);
  2809. spin_unlock_irq(&dev_priv->irq_lock);
  2810. intel_modeset_setup_hw_state(dev, true);
  2811. intel_hpd_init(dev_priv);
  2812. drm_modeset_unlock_all(dev);
  2813. }
  2814. static void
  2815. intel_finish_fb(struct drm_framebuffer *old_fb)
  2816. {
  2817. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2818. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2819. bool was_interruptible = dev_priv->mm.interruptible;
  2820. int ret;
  2821. /* Big Hammer, we also need to ensure that any pending
  2822. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2823. * current scanout is retired before unpinning the old
  2824. * framebuffer. Note that we rely on userspace rendering
  2825. * into the buffer attached to the pipe they are waiting
  2826. * on. If not, userspace generates a GPU hang with IPEHR
  2827. * point to the MI_WAIT_FOR_EVENT.
  2828. *
  2829. * This should only fail upon a hung GPU, in which case we
  2830. * can safely continue.
  2831. */
  2832. dev_priv->mm.interruptible = false;
  2833. ret = i915_gem_object_wait_rendering(obj, true);
  2834. dev_priv->mm.interruptible = was_interruptible;
  2835. WARN_ON(ret);
  2836. }
  2837. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2838. {
  2839. struct drm_device *dev = crtc->dev;
  2840. struct drm_i915_private *dev_priv = dev->dev_private;
  2841. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2842. bool pending;
  2843. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2844. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2845. return false;
  2846. spin_lock_irq(&dev->event_lock);
  2847. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2848. spin_unlock_irq(&dev->event_lock);
  2849. return pending;
  2850. }
  2851. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2852. {
  2853. struct drm_device *dev = crtc->base.dev;
  2854. struct drm_i915_private *dev_priv = dev->dev_private;
  2855. const struct drm_display_mode *adjusted_mode;
  2856. if (!i915.fastboot)
  2857. return;
  2858. /*
  2859. * Update pipe size and adjust fitter if needed: the reason for this is
  2860. * that in compute_mode_changes we check the native mode (not the pfit
  2861. * mode) to see if we can flip rather than do a full mode set. In the
  2862. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2863. * pfit state, we'll end up with a big fb scanned out into the wrong
  2864. * sized surface.
  2865. *
  2866. * To fix this properly, we need to hoist the checks up into
  2867. * compute_mode_changes (or above), check the actual pfit state and
  2868. * whether the platform allows pfit disable with pipe active, and only
  2869. * then update the pipesrc and pfit state, even on the flip path.
  2870. */
  2871. adjusted_mode = &crtc->config->base.adjusted_mode;
  2872. I915_WRITE(PIPESRC(crtc->pipe),
  2873. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2874. (adjusted_mode->crtc_vdisplay - 1));
  2875. if (!crtc->config->pch_pfit.enabled &&
  2876. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2877. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2878. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2879. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2880. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2881. }
  2882. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2883. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2884. }
  2885. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2886. {
  2887. struct drm_device *dev = crtc->dev;
  2888. struct drm_i915_private *dev_priv = dev->dev_private;
  2889. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2890. int pipe = intel_crtc->pipe;
  2891. u32 reg, temp;
  2892. /* enable normal train */
  2893. reg = FDI_TX_CTL(pipe);
  2894. temp = I915_READ(reg);
  2895. if (IS_IVYBRIDGE(dev)) {
  2896. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2897. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2898. } else {
  2899. temp &= ~FDI_LINK_TRAIN_NONE;
  2900. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2901. }
  2902. I915_WRITE(reg, temp);
  2903. reg = FDI_RX_CTL(pipe);
  2904. temp = I915_READ(reg);
  2905. if (HAS_PCH_CPT(dev)) {
  2906. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2907. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2908. } else {
  2909. temp &= ~FDI_LINK_TRAIN_NONE;
  2910. temp |= FDI_LINK_TRAIN_NONE;
  2911. }
  2912. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2913. /* wait one idle pattern time */
  2914. POSTING_READ(reg);
  2915. udelay(1000);
  2916. /* IVB wants error correction enabled */
  2917. if (IS_IVYBRIDGE(dev))
  2918. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2919. FDI_FE_ERRC_ENABLE);
  2920. }
  2921. /* The FDI link training functions for ILK/Ibexpeak. */
  2922. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2923. {
  2924. struct drm_device *dev = crtc->dev;
  2925. struct drm_i915_private *dev_priv = dev->dev_private;
  2926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2927. int pipe = intel_crtc->pipe;
  2928. u32 reg, temp, tries;
  2929. /* FDI needs bits from pipe first */
  2930. assert_pipe_enabled(dev_priv, pipe);
  2931. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2932. for train result */
  2933. reg = FDI_RX_IMR(pipe);
  2934. temp = I915_READ(reg);
  2935. temp &= ~FDI_RX_SYMBOL_LOCK;
  2936. temp &= ~FDI_RX_BIT_LOCK;
  2937. I915_WRITE(reg, temp);
  2938. I915_READ(reg);
  2939. udelay(150);
  2940. /* enable CPU FDI TX and PCH FDI RX */
  2941. reg = FDI_TX_CTL(pipe);
  2942. temp = I915_READ(reg);
  2943. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2944. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2945. temp &= ~FDI_LINK_TRAIN_NONE;
  2946. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2947. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2948. reg = FDI_RX_CTL(pipe);
  2949. temp = I915_READ(reg);
  2950. temp &= ~FDI_LINK_TRAIN_NONE;
  2951. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2952. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2953. POSTING_READ(reg);
  2954. udelay(150);
  2955. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2956. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2957. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2958. FDI_RX_PHASE_SYNC_POINTER_EN);
  2959. reg = FDI_RX_IIR(pipe);
  2960. for (tries = 0; tries < 5; tries++) {
  2961. temp = I915_READ(reg);
  2962. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2963. if ((temp & FDI_RX_BIT_LOCK)) {
  2964. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2965. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2966. break;
  2967. }
  2968. }
  2969. if (tries == 5)
  2970. DRM_ERROR("FDI train 1 fail!\n");
  2971. /* Train 2 */
  2972. reg = FDI_TX_CTL(pipe);
  2973. temp = I915_READ(reg);
  2974. temp &= ~FDI_LINK_TRAIN_NONE;
  2975. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2976. I915_WRITE(reg, temp);
  2977. reg = FDI_RX_CTL(pipe);
  2978. temp = I915_READ(reg);
  2979. temp &= ~FDI_LINK_TRAIN_NONE;
  2980. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2981. I915_WRITE(reg, temp);
  2982. POSTING_READ(reg);
  2983. udelay(150);
  2984. reg = FDI_RX_IIR(pipe);
  2985. for (tries = 0; tries < 5; tries++) {
  2986. temp = I915_READ(reg);
  2987. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2988. if (temp & FDI_RX_SYMBOL_LOCK) {
  2989. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2990. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2991. break;
  2992. }
  2993. }
  2994. if (tries == 5)
  2995. DRM_ERROR("FDI train 2 fail!\n");
  2996. DRM_DEBUG_KMS("FDI train done\n");
  2997. }
  2998. static const int snb_b_fdi_train_param[] = {
  2999. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3000. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3001. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3002. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3003. };
  3004. /* The FDI link training functions for SNB/Cougarpoint. */
  3005. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3006. {
  3007. struct drm_device *dev = crtc->dev;
  3008. struct drm_i915_private *dev_priv = dev->dev_private;
  3009. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3010. int pipe = intel_crtc->pipe;
  3011. u32 reg, temp, i, retry;
  3012. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3013. for train result */
  3014. reg = FDI_RX_IMR(pipe);
  3015. temp = I915_READ(reg);
  3016. temp &= ~FDI_RX_SYMBOL_LOCK;
  3017. temp &= ~FDI_RX_BIT_LOCK;
  3018. I915_WRITE(reg, temp);
  3019. POSTING_READ(reg);
  3020. udelay(150);
  3021. /* enable CPU FDI TX and PCH FDI RX */
  3022. reg = FDI_TX_CTL(pipe);
  3023. temp = I915_READ(reg);
  3024. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3025. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3026. temp &= ~FDI_LINK_TRAIN_NONE;
  3027. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3028. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3029. /* SNB-B */
  3030. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3031. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3032. I915_WRITE(FDI_RX_MISC(pipe),
  3033. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3034. reg = FDI_RX_CTL(pipe);
  3035. temp = I915_READ(reg);
  3036. if (HAS_PCH_CPT(dev)) {
  3037. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3038. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3039. } else {
  3040. temp &= ~FDI_LINK_TRAIN_NONE;
  3041. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3042. }
  3043. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3044. POSTING_READ(reg);
  3045. udelay(150);
  3046. for (i = 0; i < 4; i++) {
  3047. reg = FDI_TX_CTL(pipe);
  3048. temp = I915_READ(reg);
  3049. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3050. temp |= snb_b_fdi_train_param[i];
  3051. I915_WRITE(reg, temp);
  3052. POSTING_READ(reg);
  3053. udelay(500);
  3054. for (retry = 0; retry < 5; retry++) {
  3055. reg = FDI_RX_IIR(pipe);
  3056. temp = I915_READ(reg);
  3057. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3058. if (temp & FDI_RX_BIT_LOCK) {
  3059. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3060. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3061. break;
  3062. }
  3063. udelay(50);
  3064. }
  3065. if (retry < 5)
  3066. break;
  3067. }
  3068. if (i == 4)
  3069. DRM_ERROR("FDI train 1 fail!\n");
  3070. /* Train 2 */
  3071. reg = FDI_TX_CTL(pipe);
  3072. temp = I915_READ(reg);
  3073. temp &= ~FDI_LINK_TRAIN_NONE;
  3074. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3075. if (IS_GEN6(dev)) {
  3076. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3077. /* SNB-B */
  3078. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3079. }
  3080. I915_WRITE(reg, temp);
  3081. reg = FDI_RX_CTL(pipe);
  3082. temp = I915_READ(reg);
  3083. if (HAS_PCH_CPT(dev)) {
  3084. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3085. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3086. } else {
  3087. temp &= ~FDI_LINK_TRAIN_NONE;
  3088. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3089. }
  3090. I915_WRITE(reg, temp);
  3091. POSTING_READ(reg);
  3092. udelay(150);
  3093. for (i = 0; i < 4; i++) {
  3094. reg = FDI_TX_CTL(pipe);
  3095. temp = I915_READ(reg);
  3096. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3097. temp |= snb_b_fdi_train_param[i];
  3098. I915_WRITE(reg, temp);
  3099. POSTING_READ(reg);
  3100. udelay(500);
  3101. for (retry = 0; retry < 5; retry++) {
  3102. reg = FDI_RX_IIR(pipe);
  3103. temp = I915_READ(reg);
  3104. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3105. if (temp & FDI_RX_SYMBOL_LOCK) {
  3106. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3107. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3108. break;
  3109. }
  3110. udelay(50);
  3111. }
  3112. if (retry < 5)
  3113. break;
  3114. }
  3115. if (i == 4)
  3116. DRM_ERROR("FDI train 2 fail!\n");
  3117. DRM_DEBUG_KMS("FDI train done.\n");
  3118. }
  3119. /* Manual link training for Ivy Bridge A0 parts */
  3120. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3121. {
  3122. struct drm_device *dev = crtc->dev;
  3123. struct drm_i915_private *dev_priv = dev->dev_private;
  3124. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3125. int pipe = intel_crtc->pipe;
  3126. u32 reg, temp, i, j;
  3127. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3128. for train result */
  3129. reg = FDI_RX_IMR(pipe);
  3130. temp = I915_READ(reg);
  3131. temp &= ~FDI_RX_SYMBOL_LOCK;
  3132. temp &= ~FDI_RX_BIT_LOCK;
  3133. I915_WRITE(reg, temp);
  3134. POSTING_READ(reg);
  3135. udelay(150);
  3136. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3137. I915_READ(FDI_RX_IIR(pipe)));
  3138. /* Try each vswing and preemphasis setting twice before moving on */
  3139. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3140. /* disable first in case we need to retry */
  3141. reg = FDI_TX_CTL(pipe);
  3142. temp = I915_READ(reg);
  3143. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3144. temp &= ~FDI_TX_ENABLE;
  3145. I915_WRITE(reg, temp);
  3146. reg = FDI_RX_CTL(pipe);
  3147. temp = I915_READ(reg);
  3148. temp &= ~FDI_LINK_TRAIN_AUTO;
  3149. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3150. temp &= ~FDI_RX_ENABLE;
  3151. I915_WRITE(reg, temp);
  3152. /* enable CPU FDI TX and PCH FDI RX */
  3153. reg = FDI_TX_CTL(pipe);
  3154. temp = I915_READ(reg);
  3155. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3156. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3157. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3158. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3159. temp |= snb_b_fdi_train_param[j/2];
  3160. temp |= FDI_COMPOSITE_SYNC;
  3161. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3162. I915_WRITE(FDI_RX_MISC(pipe),
  3163. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3164. reg = FDI_RX_CTL(pipe);
  3165. temp = I915_READ(reg);
  3166. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3167. temp |= FDI_COMPOSITE_SYNC;
  3168. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3169. POSTING_READ(reg);
  3170. udelay(1); /* should be 0.5us */
  3171. for (i = 0; i < 4; i++) {
  3172. reg = FDI_RX_IIR(pipe);
  3173. temp = I915_READ(reg);
  3174. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3175. if (temp & FDI_RX_BIT_LOCK ||
  3176. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3177. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3178. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3179. i);
  3180. break;
  3181. }
  3182. udelay(1); /* should be 0.5us */
  3183. }
  3184. if (i == 4) {
  3185. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3186. continue;
  3187. }
  3188. /* Train 2 */
  3189. reg = FDI_TX_CTL(pipe);
  3190. temp = I915_READ(reg);
  3191. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3192. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3193. I915_WRITE(reg, temp);
  3194. reg = FDI_RX_CTL(pipe);
  3195. temp = I915_READ(reg);
  3196. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3197. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3198. I915_WRITE(reg, temp);
  3199. POSTING_READ(reg);
  3200. udelay(2); /* should be 1.5us */
  3201. for (i = 0; i < 4; i++) {
  3202. reg = FDI_RX_IIR(pipe);
  3203. temp = I915_READ(reg);
  3204. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3205. if (temp & FDI_RX_SYMBOL_LOCK ||
  3206. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3207. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3208. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3209. i);
  3210. goto train_done;
  3211. }
  3212. udelay(2); /* should be 1.5us */
  3213. }
  3214. if (i == 4)
  3215. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3216. }
  3217. train_done:
  3218. DRM_DEBUG_KMS("FDI train done.\n");
  3219. }
  3220. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3221. {
  3222. struct drm_device *dev = intel_crtc->base.dev;
  3223. struct drm_i915_private *dev_priv = dev->dev_private;
  3224. int pipe = intel_crtc->pipe;
  3225. u32 reg, temp;
  3226. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3227. reg = FDI_RX_CTL(pipe);
  3228. temp = I915_READ(reg);
  3229. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3230. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3231. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3232. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3233. POSTING_READ(reg);
  3234. udelay(200);
  3235. /* Switch from Rawclk to PCDclk */
  3236. temp = I915_READ(reg);
  3237. I915_WRITE(reg, temp | FDI_PCDCLK);
  3238. POSTING_READ(reg);
  3239. udelay(200);
  3240. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3241. reg = FDI_TX_CTL(pipe);
  3242. temp = I915_READ(reg);
  3243. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3244. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3245. POSTING_READ(reg);
  3246. udelay(100);
  3247. }
  3248. }
  3249. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3250. {
  3251. struct drm_device *dev = intel_crtc->base.dev;
  3252. struct drm_i915_private *dev_priv = dev->dev_private;
  3253. int pipe = intel_crtc->pipe;
  3254. u32 reg, temp;
  3255. /* Switch from PCDclk to Rawclk */
  3256. reg = FDI_RX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3259. /* Disable CPU FDI TX PLL */
  3260. reg = FDI_TX_CTL(pipe);
  3261. temp = I915_READ(reg);
  3262. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3263. POSTING_READ(reg);
  3264. udelay(100);
  3265. reg = FDI_RX_CTL(pipe);
  3266. temp = I915_READ(reg);
  3267. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3268. /* Wait for the clocks to turn off. */
  3269. POSTING_READ(reg);
  3270. udelay(100);
  3271. }
  3272. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3273. {
  3274. struct drm_device *dev = crtc->dev;
  3275. struct drm_i915_private *dev_priv = dev->dev_private;
  3276. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3277. int pipe = intel_crtc->pipe;
  3278. u32 reg, temp;
  3279. /* disable CPU FDI tx and PCH FDI rx */
  3280. reg = FDI_TX_CTL(pipe);
  3281. temp = I915_READ(reg);
  3282. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3283. POSTING_READ(reg);
  3284. reg = FDI_RX_CTL(pipe);
  3285. temp = I915_READ(reg);
  3286. temp &= ~(0x7 << 16);
  3287. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3288. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3289. POSTING_READ(reg);
  3290. udelay(100);
  3291. /* Ironlake workaround, disable clock pointer after downing FDI */
  3292. if (HAS_PCH_IBX(dev))
  3293. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3294. /* still set train pattern 1 */
  3295. reg = FDI_TX_CTL(pipe);
  3296. temp = I915_READ(reg);
  3297. temp &= ~FDI_LINK_TRAIN_NONE;
  3298. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3299. I915_WRITE(reg, temp);
  3300. reg = FDI_RX_CTL(pipe);
  3301. temp = I915_READ(reg);
  3302. if (HAS_PCH_CPT(dev)) {
  3303. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3304. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3305. } else {
  3306. temp &= ~FDI_LINK_TRAIN_NONE;
  3307. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3308. }
  3309. /* BPC in FDI rx is consistent with that in PIPECONF */
  3310. temp &= ~(0x07 << 16);
  3311. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3312. I915_WRITE(reg, temp);
  3313. POSTING_READ(reg);
  3314. udelay(100);
  3315. }
  3316. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3317. {
  3318. struct intel_crtc *crtc;
  3319. /* Note that we don't need to be called with mode_config.lock here
  3320. * as our list of CRTC objects is static for the lifetime of the
  3321. * device and so cannot disappear as we iterate. Similarly, we can
  3322. * happily treat the predicates as racy, atomic checks as userspace
  3323. * cannot claim and pin a new fb without at least acquring the
  3324. * struct_mutex and so serialising with us.
  3325. */
  3326. for_each_intel_crtc(dev, crtc) {
  3327. if (atomic_read(&crtc->unpin_work_count) == 0)
  3328. continue;
  3329. if (crtc->unpin_work)
  3330. intel_wait_for_vblank(dev, crtc->pipe);
  3331. return true;
  3332. }
  3333. return false;
  3334. }
  3335. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3336. {
  3337. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3338. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3339. /* ensure that the unpin work is consistent wrt ->pending. */
  3340. smp_rmb();
  3341. intel_crtc->unpin_work = NULL;
  3342. if (work->event)
  3343. drm_send_vblank_event(intel_crtc->base.dev,
  3344. intel_crtc->pipe,
  3345. work->event);
  3346. drm_crtc_vblank_put(&intel_crtc->base);
  3347. wake_up_all(&dev_priv->pending_flip_queue);
  3348. queue_work(dev_priv->wq, &work->work);
  3349. trace_i915_flip_complete(intel_crtc->plane,
  3350. work->pending_flip_obj);
  3351. }
  3352. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3353. {
  3354. struct drm_device *dev = crtc->dev;
  3355. struct drm_i915_private *dev_priv = dev->dev_private;
  3356. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3357. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3358. !intel_crtc_has_pending_flip(crtc),
  3359. 60*HZ) == 0)) {
  3360. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3361. spin_lock_irq(&dev->event_lock);
  3362. if (intel_crtc->unpin_work) {
  3363. WARN_ONCE(1, "Removing stuck page flip\n");
  3364. page_flip_completed(intel_crtc);
  3365. }
  3366. spin_unlock_irq(&dev->event_lock);
  3367. }
  3368. if (crtc->primary->fb) {
  3369. mutex_lock(&dev->struct_mutex);
  3370. intel_finish_fb(crtc->primary->fb);
  3371. mutex_unlock(&dev->struct_mutex);
  3372. }
  3373. }
  3374. /* Program iCLKIP clock to the desired frequency */
  3375. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3376. {
  3377. struct drm_device *dev = crtc->dev;
  3378. struct drm_i915_private *dev_priv = dev->dev_private;
  3379. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3380. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3381. u32 temp;
  3382. mutex_lock(&dev_priv->sb_lock);
  3383. /* It is necessary to ungate the pixclk gate prior to programming
  3384. * the divisors, and gate it back when it is done.
  3385. */
  3386. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3387. /* Disable SSCCTL */
  3388. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3389. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3390. SBI_SSCCTL_DISABLE,
  3391. SBI_ICLK);
  3392. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3393. if (clock == 20000) {
  3394. auxdiv = 1;
  3395. divsel = 0x41;
  3396. phaseinc = 0x20;
  3397. } else {
  3398. /* The iCLK virtual clock root frequency is in MHz,
  3399. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3400. * divisors, it is necessary to divide one by another, so we
  3401. * convert the virtual clock precision to KHz here for higher
  3402. * precision.
  3403. */
  3404. u32 iclk_virtual_root_freq = 172800 * 1000;
  3405. u32 iclk_pi_range = 64;
  3406. u32 desired_divisor, msb_divisor_value, pi_value;
  3407. desired_divisor = (iclk_virtual_root_freq / clock);
  3408. msb_divisor_value = desired_divisor / iclk_pi_range;
  3409. pi_value = desired_divisor % iclk_pi_range;
  3410. auxdiv = 0;
  3411. divsel = msb_divisor_value - 2;
  3412. phaseinc = pi_value;
  3413. }
  3414. /* This should not happen with any sane values */
  3415. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3416. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3417. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3418. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3419. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3420. clock,
  3421. auxdiv,
  3422. divsel,
  3423. phasedir,
  3424. phaseinc);
  3425. /* Program SSCDIVINTPHASE6 */
  3426. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3427. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3428. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3429. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3430. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3431. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3432. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3433. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3434. /* Program SSCAUXDIV */
  3435. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3436. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3437. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3438. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3439. /* Enable modulator and associated divider */
  3440. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3441. temp &= ~SBI_SSCCTL_DISABLE;
  3442. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3443. /* Wait for initialization time */
  3444. udelay(24);
  3445. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3446. mutex_unlock(&dev_priv->sb_lock);
  3447. }
  3448. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3449. enum pipe pch_transcoder)
  3450. {
  3451. struct drm_device *dev = crtc->base.dev;
  3452. struct drm_i915_private *dev_priv = dev->dev_private;
  3453. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3454. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3455. I915_READ(HTOTAL(cpu_transcoder)));
  3456. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3457. I915_READ(HBLANK(cpu_transcoder)));
  3458. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3459. I915_READ(HSYNC(cpu_transcoder)));
  3460. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3461. I915_READ(VTOTAL(cpu_transcoder)));
  3462. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3463. I915_READ(VBLANK(cpu_transcoder)));
  3464. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3465. I915_READ(VSYNC(cpu_transcoder)));
  3466. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3467. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3468. }
  3469. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3470. {
  3471. struct drm_i915_private *dev_priv = dev->dev_private;
  3472. uint32_t temp;
  3473. temp = I915_READ(SOUTH_CHICKEN1);
  3474. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3475. return;
  3476. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3477. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3478. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3479. if (enable)
  3480. temp |= FDI_BC_BIFURCATION_SELECT;
  3481. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3482. I915_WRITE(SOUTH_CHICKEN1, temp);
  3483. POSTING_READ(SOUTH_CHICKEN1);
  3484. }
  3485. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3486. {
  3487. struct drm_device *dev = intel_crtc->base.dev;
  3488. switch (intel_crtc->pipe) {
  3489. case PIPE_A:
  3490. break;
  3491. case PIPE_B:
  3492. if (intel_crtc->config->fdi_lanes > 2)
  3493. cpt_set_fdi_bc_bifurcation(dev, false);
  3494. else
  3495. cpt_set_fdi_bc_bifurcation(dev, true);
  3496. break;
  3497. case PIPE_C:
  3498. cpt_set_fdi_bc_bifurcation(dev, true);
  3499. break;
  3500. default:
  3501. BUG();
  3502. }
  3503. }
  3504. /*
  3505. * Enable PCH resources required for PCH ports:
  3506. * - PCH PLLs
  3507. * - FDI training & RX/TX
  3508. * - update transcoder timings
  3509. * - DP transcoding bits
  3510. * - transcoder
  3511. */
  3512. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3513. {
  3514. struct drm_device *dev = crtc->dev;
  3515. struct drm_i915_private *dev_priv = dev->dev_private;
  3516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3517. int pipe = intel_crtc->pipe;
  3518. u32 reg, temp;
  3519. assert_pch_transcoder_disabled(dev_priv, pipe);
  3520. if (IS_IVYBRIDGE(dev))
  3521. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3522. /* Write the TU size bits before fdi link training, so that error
  3523. * detection works. */
  3524. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3525. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3526. /* For PCH output, training FDI link */
  3527. dev_priv->display.fdi_link_train(crtc);
  3528. /* We need to program the right clock selection before writing the pixel
  3529. * mutliplier into the DPLL. */
  3530. if (HAS_PCH_CPT(dev)) {
  3531. u32 sel;
  3532. temp = I915_READ(PCH_DPLL_SEL);
  3533. temp |= TRANS_DPLL_ENABLE(pipe);
  3534. sel = TRANS_DPLLB_SEL(pipe);
  3535. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3536. temp |= sel;
  3537. else
  3538. temp &= ~sel;
  3539. I915_WRITE(PCH_DPLL_SEL, temp);
  3540. }
  3541. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3542. * transcoder, and we actually should do this to not upset any PCH
  3543. * transcoder that already use the clock when we share it.
  3544. *
  3545. * Note that enable_shared_dpll tries to do the right thing, but
  3546. * get_shared_dpll unconditionally resets the pll - we need that to have
  3547. * the right LVDS enable sequence. */
  3548. intel_enable_shared_dpll(intel_crtc);
  3549. /* set transcoder timing, panel must allow it */
  3550. assert_panel_unlocked(dev_priv, pipe);
  3551. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3552. intel_fdi_normal_train(crtc);
  3553. /* For PCH DP, enable TRANS_DP_CTL */
  3554. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3555. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3556. reg = TRANS_DP_CTL(pipe);
  3557. temp = I915_READ(reg);
  3558. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3559. TRANS_DP_SYNC_MASK |
  3560. TRANS_DP_BPC_MASK);
  3561. temp |= TRANS_DP_OUTPUT_ENABLE;
  3562. temp |= bpc << 9; /* same format but at 11:9 */
  3563. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3564. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3565. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3566. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3567. switch (intel_trans_dp_port_sel(crtc)) {
  3568. case PCH_DP_B:
  3569. temp |= TRANS_DP_PORT_SEL_B;
  3570. break;
  3571. case PCH_DP_C:
  3572. temp |= TRANS_DP_PORT_SEL_C;
  3573. break;
  3574. case PCH_DP_D:
  3575. temp |= TRANS_DP_PORT_SEL_D;
  3576. break;
  3577. default:
  3578. BUG();
  3579. }
  3580. I915_WRITE(reg, temp);
  3581. }
  3582. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3583. }
  3584. static void lpt_pch_enable(struct drm_crtc *crtc)
  3585. {
  3586. struct drm_device *dev = crtc->dev;
  3587. struct drm_i915_private *dev_priv = dev->dev_private;
  3588. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3589. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3590. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3591. lpt_program_iclkip(crtc);
  3592. /* Set transcoder timing. */
  3593. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3594. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3595. }
  3596. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3597. struct intel_crtc_state *crtc_state)
  3598. {
  3599. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3600. struct intel_shared_dpll *pll;
  3601. struct intel_shared_dpll_config *shared_dpll;
  3602. enum intel_dpll_id i;
  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. }
  3629. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3630. pll = &dev_priv->shared_dplls[i];
  3631. /* Only want to check enabled timings first */
  3632. if (shared_dpll[i].crtc_mask == 0)
  3633. continue;
  3634. if (memcmp(&crtc_state->dpll_hw_state,
  3635. &shared_dpll[i].hw_state,
  3636. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3637. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3638. crtc->base.base.id, pll->name,
  3639. shared_dpll[i].crtc_mask,
  3640. pll->active);
  3641. goto found;
  3642. }
  3643. }
  3644. /* Ok no matching timings, maybe there's a free one? */
  3645. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3646. pll = &dev_priv->shared_dplls[i];
  3647. if (shared_dpll[i].crtc_mask == 0) {
  3648. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3649. crtc->base.base.id, pll->name);
  3650. goto found;
  3651. }
  3652. }
  3653. return NULL;
  3654. found:
  3655. if (shared_dpll[i].crtc_mask == 0)
  3656. shared_dpll[i].hw_state =
  3657. crtc_state->dpll_hw_state;
  3658. crtc_state->shared_dpll = i;
  3659. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3660. pipe_name(crtc->pipe));
  3661. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3662. return pll;
  3663. }
  3664. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3665. {
  3666. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3667. struct intel_shared_dpll_config *shared_dpll;
  3668. struct intel_shared_dpll *pll;
  3669. enum intel_dpll_id i;
  3670. if (!to_intel_atomic_state(state)->dpll_set)
  3671. return;
  3672. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3673. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3674. pll = &dev_priv->shared_dplls[i];
  3675. pll->config = shared_dpll[i];
  3676. }
  3677. }
  3678. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3679. {
  3680. struct drm_i915_private *dev_priv = dev->dev_private;
  3681. int dslreg = PIPEDSL(pipe);
  3682. u32 temp;
  3683. temp = I915_READ(dslreg);
  3684. udelay(500);
  3685. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3686. if (wait_for(I915_READ(dslreg) != temp, 5))
  3687. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3688. }
  3689. }
  3690. /**
  3691. * skl_update_scaler_users - Stages update to crtc's scaler state
  3692. * @intel_crtc: crtc
  3693. * @crtc_state: crtc_state
  3694. * @plane: plane (NULL indicates crtc is requesting update)
  3695. * @plane_state: plane's state
  3696. * @force_detach: request unconditional detachment of scaler
  3697. *
  3698. * This function updates scaler state for requested plane or crtc.
  3699. * To request scaler usage update for a plane, caller shall pass plane pointer.
  3700. * To request scaler usage update for crtc, caller shall pass plane pointer
  3701. * as NULL.
  3702. *
  3703. * Return
  3704. * 0 - scaler_usage updated successfully
  3705. * error - requested scaling cannot be supported or other error condition
  3706. */
  3707. int
  3708. skl_update_scaler_users(
  3709. struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
  3710. struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
  3711. int force_detach)
  3712. {
  3713. int need_scaling;
  3714. int idx;
  3715. int src_w, src_h, dst_w, dst_h;
  3716. int *scaler_id;
  3717. struct drm_framebuffer *fb;
  3718. struct intel_crtc_scaler_state *scaler_state;
  3719. unsigned int rotation;
  3720. if (!intel_crtc || !crtc_state)
  3721. return 0;
  3722. scaler_state = &crtc_state->scaler_state;
  3723. idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
  3724. fb = intel_plane ? plane_state->base.fb : NULL;
  3725. if (intel_plane) {
  3726. src_w = drm_rect_width(&plane_state->src) >> 16;
  3727. src_h = drm_rect_height(&plane_state->src) >> 16;
  3728. dst_w = drm_rect_width(&plane_state->dst);
  3729. dst_h = drm_rect_height(&plane_state->dst);
  3730. scaler_id = &plane_state->scaler_id;
  3731. rotation = plane_state->base.rotation;
  3732. } else {
  3733. struct drm_display_mode *adjusted_mode =
  3734. &crtc_state->base.adjusted_mode;
  3735. src_w = crtc_state->pipe_src_w;
  3736. src_h = crtc_state->pipe_src_h;
  3737. dst_w = adjusted_mode->hdisplay;
  3738. dst_h = adjusted_mode->vdisplay;
  3739. scaler_id = &scaler_state->scaler_id;
  3740. rotation = DRM_ROTATE_0;
  3741. }
  3742. need_scaling = intel_rotation_90_or_270(rotation) ?
  3743. (src_h != dst_w || src_w != dst_h):
  3744. (src_w != dst_w || src_h != dst_h);
  3745. /*
  3746. * if plane is being disabled or scaler is no more required or force detach
  3747. * - free scaler binded to this plane/crtc
  3748. * - in order to do this, update crtc->scaler_usage
  3749. *
  3750. * Here scaler state in crtc_state is set free so that
  3751. * scaler can be assigned to other user. Actual register
  3752. * update to free the scaler is done in plane/panel-fit programming.
  3753. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3754. */
  3755. if (force_detach || !need_scaling || (intel_plane &&
  3756. (!fb || !plane_state->visible))) {
  3757. if (*scaler_id >= 0) {
  3758. scaler_state->scaler_users &= ~(1 << idx);
  3759. scaler_state->scalers[*scaler_id].in_use = 0;
  3760. DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
  3761. "crtc_state = %p scaler_users = 0x%x\n",
  3762. intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
  3763. intel_plane ? intel_plane->base.base.id :
  3764. intel_crtc->base.base.id, crtc_state,
  3765. scaler_state->scaler_users);
  3766. *scaler_id = -1;
  3767. }
  3768. return 0;
  3769. }
  3770. /* range checks */
  3771. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3772. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3773. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3774. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3775. DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
  3776. "size is out of scaler range\n",
  3777. intel_plane ? "PLANE" : "CRTC",
  3778. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3779. intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
  3780. return -EINVAL;
  3781. }
  3782. /* check colorkey */
  3783. if (WARN_ON(intel_plane &&
  3784. intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3785. DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
  3786. intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
  3787. return -EINVAL;
  3788. }
  3789. /* Check src format */
  3790. if (intel_plane) {
  3791. switch (fb->pixel_format) {
  3792. case DRM_FORMAT_RGB565:
  3793. case DRM_FORMAT_XBGR8888:
  3794. case DRM_FORMAT_XRGB8888:
  3795. case DRM_FORMAT_ABGR8888:
  3796. case DRM_FORMAT_ARGB8888:
  3797. case DRM_FORMAT_XRGB2101010:
  3798. case DRM_FORMAT_XBGR2101010:
  3799. case DRM_FORMAT_YUYV:
  3800. case DRM_FORMAT_YVYU:
  3801. case DRM_FORMAT_UYVY:
  3802. case DRM_FORMAT_VYUY:
  3803. break;
  3804. default:
  3805. DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
  3806. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3807. return -EINVAL;
  3808. }
  3809. }
  3810. /* mark this plane as a scaler user in crtc_state */
  3811. scaler_state->scaler_users |= (1 << idx);
  3812. DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
  3813. "crtc_state = %p scaler_users = 0x%x\n",
  3814. intel_plane ? "PLANE" : "CRTC",
  3815. intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
  3816. src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
  3817. return 0;
  3818. }
  3819. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3820. {
  3821. struct drm_device *dev = crtc->base.dev;
  3822. struct drm_i915_private *dev_priv = dev->dev_private;
  3823. int pipe = crtc->pipe;
  3824. struct intel_crtc_scaler_state *scaler_state =
  3825. &crtc->config->scaler_state;
  3826. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3827. /* To update pfit, first update scaler state */
  3828. skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
  3829. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3830. skl_detach_scalers(crtc);
  3831. if (!enable)
  3832. return;
  3833. if (crtc->config->pch_pfit.enabled) {
  3834. int id;
  3835. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3836. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3837. return;
  3838. }
  3839. id = scaler_state->scaler_id;
  3840. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3841. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3842. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3843. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3844. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3845. }
  3846. }
  3847. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3848. {
  3849. struct drm_device *dev = crtc->base.dev;
  3850. struct drm_i915_private *dev_priv = dev->dev_private;
  3851. int pipe = crtc->pipe;
  3852. if (crtc->config->pch_pfit.enabled) {
  3853. /* Force use of hard-coded filter coefficients
  3854. * as some pre-programmed values are broken,
  3855. * e.g. x201.
  3856. */
  3857. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3858. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3859. PF_PIPE_SEL_IVB(pipe));
  3860. else
  3861. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3862. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3863. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3864. }
  3865. }
  3866. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->dev;
  3869. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3870. struct drm_plane *plane;
  3871. struct intel_plane *intel_plane;
  3872. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3873. intel_plane = to_intel_plane(plane);
  3874. if (intel_plane->pipe == pipe)
  3875. intel_plane_restore(&intel_plane->base);
  3876. }
  3877. }
  3878. void hsw_enable_ips(struct intel_crtc *crtc)
  3879. {
  3880. struct drm_device *dev = crtc->base.dev;
  3881. struct drm_i915_private *dev_priv = dev->dev_private;
  3882. if (!crtc->config->ips_enabled)
  3883. return;
  3884. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3885. intel_wait_for_vblank(dev, crtc->pipe);
  3886. assert_plane_enabled(dev_priv, crtc->plane);
  3887. if (IS_BROADWELL(dev)) {
  3888. mutex_lock(&dev_priv->rps.hw_lock);
  3889. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3890. mutex_unlock(&dev_priv->rps.hw_lock);
  3891. /* Quoting Art Runyan: "its not safe to expect any particular
  3892. * value in IPS_CTL bit 31 after enabling IPS through the
  3893. * mailbox." Moreover, the mailbox may return a bogus state,
  3894. * so we need to just enable it and continue on.
  3895. */
  3896. } else {
  3897. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3898. /* The bit only becomes 1 in the next vblank, so this wait here
  3899. * is essentially intel_wait_for_vblank. If we don't have this
  3900. * and don't wait for vblanks until the end of crtc_enable, then
  3901. * the HW state readout code will complain that the expected
  3902. * IPS_CTL value is not the one we read. */
  3903. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3904. DRM_ERROR("Timed out waiting for IPS enable\n");
  3905. }
  3906. }
  3907. void hsw_disable_ips(struct intel_crtc *crtc)
  3908. {
  3909. struct drm_device *dev = crtc->base.dev;
  3910. struct drm_i915_private *dev_priv = dev->dev_private;
  3911. if (!crtc->config->ips_enabled)
  3912. return;
  3913. assert_plane_enabled(dev_priv, crtc->plane);
  3914. if (IS_BROADWELL(dev)) {
  3915. mutex_lock(&dev_priv->rps.hw_lock);
  3916. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3917. mutex_unlock(&dev_priv->rps.hw_lock);
  3918. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3919. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3920. DRM_ERROR("Timed out waiting for IPS disable\n");
  3921. } else {
  3922. I915_WRITE(IPS_CTL, 0);
  3923. POSTING_READ(IPS_CTL);
  3924. }
  3925. /* We need to wait for a vblank before we can disable the plane. */
  3926. intel_wait_for_vblank(dev, crtc->pipe);
  3927. }
  3928. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3929. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3930. {
  3931. struct drm_device *dev = crtc->dev;
  3932. struct drm_i915_private *dev_priv = dev->dev_private;
  3933. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3934. enum pipe pipe = intel_crtc->pipe;
  3935. int palreg = PALETTE(pipe);
  3936. int i;
  3937. bool reenable_ips = false;
  3938. /* The clocks have to be on to load the palette. */
  3939. if (!crtc->state->active)
  3940. return;
  3941. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3942. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3943. assert_dsi_pll_enabled(dev_priv);
  3944. else
  3945. assert_pll_enabled(dev_priv, pipe);
  3946. }
  3947. /* use legacy palette for Ironlake */
  3948. if (!HAS_GMCH_DISPLAY(dev))
  3949. palreg = LGC_PALETTE(pipe);
  3950. /* Workaround : Do not read or write the pipe palette/gamma data while
  3951. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3952. */
  3953. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3954. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3955. GAMMA_MODE_MODE_SPLIT)) {
  3956. hsw_disable_ips(intel_crtc);
  3957. reenable_ips = true;
  3958. }
  3959. for (i = 0; i < 256; i++) {
  3960. I915_WRITE(palreg + 4 * i,
  3961. (intel_crtc->lut_r[i] << 16) |
  3962. (intel_crtc->lut_g[i] << 8) |
  3963. intel_crtc->lut_b[i]);
  3964. }
  3965. if (reenable_ips)
  3966. hsw_enable_ips(intel_crtc);
  3967. }
  3968. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3969. {
  3970. if (intel_crtc->overlay) {
  3971. struct drm_device *dev = intel_crtc->base.dev;
  3972. struct drm_i915_private *dev_priv = dev->dev_private;
  3973. mutex_lock(&dev->struct_mutex);
  3974. dev_priv->mm.interruptible = false;
  3975. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3976. dev_priv->mm.interruptible = true;
  3977. mutex_unlock(&dev->struct_mutex);
  3978. }
  3979. /* Let userspace switch the overlay on again. In most cases userspace
  3980. * has to recompute where to put it anyway.
  3981. */
  3982. }
  3983. /**
  3984. * intel_post_enable_primary - Perform operations after enabling primary plane
  3985. * @crtc: the CRTC whose primary plane was just enabled
  3986. *
  3987. * Performs potentially sleeping operations that must be done after the primary
  3988. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3989. * called due to an explicit primary plane update, or due to an implicit
  3990. * re-enable that is caused when a sprite plane is updated to no longer
  3991. * completely hide the primary plane.
  3992. */
  3993. static void
  3994. intel_post_enable_primary(struct drm_crtc *crtc)
  3995. {
  3996. struct drm_device *dev = crtc->dev;
  3997. struct drm_i915_private *dev_priv = dev->dev_private;
  3998. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3999. int pipe = intel_crtc->pipe;
  4000. /*
  4001. * BDW signals flip done immediately if the plane
  4002. * is disabled, even if the plane enable is already
  4003. * armed to occur at the next vblank :(
  4004. */
  4005. if (IS_BROADWELL(dev))
  4006. intel_wait_for_vblank(dev, pipe);
  4007. /*
  4008. * FIXME IPS should be fine as long as one plane is
  4009. * enabled, but in practice it seems to have problems
  4010. * when going from primary only to sprite only and vice
  4011. * versa.
  4012. */
  4013. hsw_enable_ips(intel_crtc);
  4014. mutex_lock(&dev->struct_mutex);
  4015. intel_fbc_update(dev);
  4016. mutex_unlock(&dev->struct_mutex);
  4017. /*
  4018. * Gen2 reports pipe underruns whenever all planes are disabled.
  4019. * So don't enable underrun reporting before at least some planes
  4020. * are enabled.
  4021. * FIXME: Need to fix the logic to work when we turn off all planes
  4022. * but leave the pipe running.
  4023. */
  4024. if (IS_GEN2(dev))
  4025. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4026. /* Underruns don't raise interrupts, so check manually. */
  4027. if (HAS_GMCH_DISPLAY(dev))
  4028. i9xx_check_fifo_underruns(dev_priv);
  4029. }
  4030. /**
  4031. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4032. * @crtc: the CRTC whose primary plane is to be disabled
  4033. *
  4034. * Performs potentially sleeping operations that must be done before the
  4035. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4036. * be called due to an explicit primary plane update, or due to an implicit
  4037. * disable that is caused when a sprite plane completely hides the primary
  4038. * plane.
  4039. */
  4040. static void
  4041. intel_pre_disable_primary(struct drm_crtc *crtc)
  4042. {
  4043. struct drm_device *dev = crtc->dev;
  4044. struct drm_i915_private *dev_priv = dev->dev_private;
  4045. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4046. int pipe = intel_crtc->pipe;
  4047. /*
  4048. * Gen2 reports pipe underruns whenever all planes are disabled.
  4049. * So diasble underrun reporting before all the planes get disabled.
  4050. * FIXME: Need to fix the logic to work when we turn off all planes
  4051. * but leave the pipe running.
  4052. */
  4053. if (IS_GEN2(dev))
  4054. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4055. /*
  4056. * Vblank time updates from the shadow to live plane control register
  4057. * are blocked if the memory self-refresh mode is active at that
  4058. * moment. So to make sure the plane gets truly disabled, disable
  4059. * first the self-refresh mode. The self-refresh enable bit in turn
  4060. * will be checked/applied by the HW only at the next frame start
  4061. * event which is after the vblank start event, so we need to have a
  4062. * wait-for-vblank between disabling the plane and the pipe.
  4063. */
  4064. if (HAS_GMCH_DISPLAY(dev))
  4065. intel_set_memory_cxsr(dev_priv, false);
  4066. mutex_lock(&dev->struct_mutex);
  4067. if (dev_priv->fbc.crtc == intel_crtc)
  4068. intel_fbc_disable(dev);
  4069. mutex_unlock(&dev->struct_mutex);
  4070. /*
  4071. * FIXME IPS should be fine as long as one plane is
  4072. * enabled, but in practice it seems to have problems
  4073. * when going from primary only to sprite only and vice
  4074. * versa.
  4075. */
  4076. hsw_disable_ips(intel_crtc);
  4077. }
  4078. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4079. {
  4080. struct drm_device *dev = crtc->dev;
  4081. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4082. int pipe = intel_crtc->pipe;
  4083. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4084. intel_enable_sprite_planes(crtc);
  4085. if (to_intel_plane_state(crtc->cursor->state)->visible)
  4086. intel_crtc_update_cursor(crtc, true);
  4087. intel_post_enable_primary(crtc);
  4088. /*
  4089. * FIXME: Once we grow proper nuclear flip support out of this we need
  4090. * to compute the mask of flip planes precisely. For the time being
  4091. * consider this a flip to a NULL plane.
  4092. */
  4093. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4094. }
  4095. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4096. {
  4097. struct drm_device *dev = crtc->dev;
  4098. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4099. struct intel_plane *intel_plane;
  4100. int pipe = intel_crtc->pipe;
  4101. intel_crtc_wait_for_pending_flips(crtc);
  4102. intel_pre_disable_primary(crtc);
  4103. intel_crtc_dpms_overlay_disable(intel_crtc);
  4104. for_each_intel_plane(dev, intel_plane) {
  4105. if (intel_plane->pipe == pipe) {
  4106. struct drm_crtc *from = intel_plane->base.crtc;
  4107. intel_plane->disable_plane(&intel_plane->base,
  4108. from ?: crtc, true);
  4109. }
  4110. }
  4111. /*
  4112. * FIXME: Once we grow proper nuclear flip support out of this we need
  4113. * to compute the mask of flip planes precisely. For the time being
  4114. * consider this a flip to a NULL plane.
  4115. */
  4116. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4117. }
  4118. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4119. {
  4120. struct drm_device *dev = crtc->dev;
  4121. struct drm_i915_private *dev_priv = dev->dev_private;
  4122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4123. struct intel_encoder *encoder;
  4124. int pipe = intel_crtc->pipe;
  4125. if (WARN_ON(intel_crtc->active))
  4126. return;
  4127. if (intel_crtc->config->has_pch_encoder)
  4128. intel_prepare_shared_dpll(intel_crtc);
  4129. if (intel_crtc->config->has_dp_encoder)
  4130. intel_dp_set_m_n(intel_crtc, M1_N1);
  4131. intel_set_pipe_timings(intel_crtc);
  4132. if (intel_crtc->config->has_pch_encoder) {
  4133. intel_cpu_transcoder_set_m_n(intel_crtc,
  4134. &intel_crtc->config->fdi_m_n, NULL);
  4135. }
  4136. ironlake_set_pipeconf(crtc);
  4137. intel_crtc->active = true;
  4138. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4139. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4140. for_each_encoder_on_crtc(dev, crtc, encoder)
  4141. if (encoder->pre_enable)
  4142. encoder->pre_enable(encoder);
  4143. if (intel_crtc->config->has_pch_encoder) {
  4144. /* Note: FDI PLL enabling _must_ be done before we enable the
  4145. * cpu pipes, hence this is separate from all the other fdi/pch
  4146. * enabling. */
  4147. ironlake_fdi_pll_enable(intel_crtc);
  4148. } else {
  4149. assert_fdi_tx_disabled(dev_priv, pipe);
  4150. assert_fdi_rx_disabled(dev_priv, pipe);
  4151. }
  4152. ironlake_pfit_enable(intel_crtc);
  4153. /*
  4154. * On ILK+ LUT must be loaded before the pipe is running but with
  4155. * clocks enabled
  4156. */
  4157. intel_crtc_load_lut(crtc);
  4158. intel_update_watermarks(crtc);
  4159. intel_enable_pipe(intel_crtc);
  4160. if (intel_crtc->config->has_pch_encoder)
  4161. ironlake_pch_enable(crtc);
  4162. assert_vblank_disabled(crtc);
  4163. drm_crtc_vblank_on(crtc);
  4164. for_each_encoder_on_crtc(dev, crtc, encoder)
  4165. encoder->enable(encoder);
  4166. if (HAS_PCH_CPT(dev))
  4167. cpt_verify_modeset(dev, intel_crtc->pipe);
  4168. }
  4169. /* IPS only exists on ULT machines and is tied to pipe A. */
  4170. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4171. {
  4172. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4173. }
  4174. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4175. {
  4176. struct drm_device *dev = crtc->dev;
  4177. struct drm_i915_private *dev_priv = dev->dev_private;
  4178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4179. struct intel_encoder *encoder;
  4180. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4181. struct intel_crtc_state *pipe_config =
  4182. to_intel_crtc_state(crtc->state);
  4183. if (WARN_ON(intel_crtc->active))
  4184. return;
  4185. if (intel_crtc_to_shared_dpll(intel_crtc))
  4186. intel_enable_shared_dpll(intel_crtc);
  4187. if (intel_crtc->config->has_dp_encoder)
  4188. intel_dp_set_m_n(intel_crtc, M1_N1);
  4189. intel_set_pipe_timings(intel_crtc);
  4190. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4191. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4192. intel_crtc->config->pixel_multiplier - 1);
  4193. }
  4194. if (intel_crtc->config->has_pch_encoder) {
  4195. intel_cpu_transcoder_set_m_n(intel_crtc,
  4196. &intel_crtc->config->fdi_m_n, NULL);
  4197. }
  4198. haswell_set_pipeconf(crtc);
  4199. intel_set_pipe_csc(crtc);
  4200. intel_crtc->active = true;
  4201. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4202. for_each_encoder_on_crtc(dev, crtc, encoder)
  4203. if (encoder->pre_enable)
  4204. encoder->pre_enable(encoder);
  4205. if (intel_crtc->config->has_pch_encoder) {
  4206. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4207. true);
  4208. dev_priv->display.fdi_link_train(crtc);
  4209. }
  4210. intel_ddi_enable_pipe_clock(intel_crtc);
  4211. if (INTEL_INFO(dev)->gen == 9)
  4212. skylake_pfit_update(intel_crtc, 1);
  4213. else if (INTEL_INFO(dev)->gen < 9)
  4214. ironlake_pfit_enable(intel_crtc);
  4215. else
  4216. MISSING_CASE(INTEL_INFO(dev)->gen);
  4217. /*
  4218. * On ILK+ LUT must be loaded before the pipe is running but with
  4219. * clocks enabled
  4220. */
  4221. intel_crtc_load_lut(crtc);
  4222. intel_ddi_set_pipe_settings(crtc);
  4223. intel_ddi_enable_transcoder_func(crtc);
  4224. intel_update_watermarks(crtc);
  4225. intel_enable_pipe(intel_crtc);
  4226. if (intel_crtc->config->has_pch_encoder)
  4227. lpt_pch_enable(crtc);
  4228. if (intel_crtc->config->dp_encoder_is_mst)
  4229. intel_ddi_set_vc_payload_alloc(crtc, true);
  4230. assert_vblank_disabled(crtc);
  4231. drm_crtc_vblank_on(crtc);
  4232. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4233. encoder->enable(encoder);
  4234. intel_opregion_notify_encoder(encoder, true);
  4235. }
  4236. /* If we change the relative order between pipe/planes enabling, we need
  4237. * to change the workaround. */
  4238. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4239. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4240. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4241. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4242. }
  4243. }
  4244. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4245. {
  4246. struct drm_device *dev = crtc->base.dev;
  4247. struct drm_i915_private *dev_priv = dev->dev_private;
  4248. int pipe = crtc->pipe;
  4249. /* To avoid upsetting the power well on haswell only disable the pfit if
  4250. * it's in use. The hw state code will make sure we get this right. */
  4251. if (crtc->config->pch_pfit.enabled) {
  4252. I915_WRITE(PF_CTL(pipe), 0);
  4253. I915_WRITE(PF_WIN_POS(pipe), 0);
  4254. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4255. }
  4256. }
  4257. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4258. {
  4259. struct drm_device *dev = crtc->dev;
  4260. struct drm_i915_private *dev_priv = dev->dev_private;
  4261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4262. struct intel_encoder *encoder;
  4263. int pipe = intel_crtc->pipe;
  4264. u32 reg, temp;
  4265. if (WARN_ON(!intel_crtc->active))
  4266. return;
  4267. for_each_encoder_on_crtc(dev, crtc, encoder)
  4268. encoder->disable(encoder);
  4269. drm_crtc_vblank_off(crtc);
  4270. assert_vblank_disabled(crtc);
  4271. if (intel_crtc->config->has_pch_encoder)
  4272. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4273. intel_disable_pipe(intel_crtc);
  4274. ironlake_pfit_disable(intel_crtc);
  4275. if (intel_crtc->config->has_pch_encoder)
  4276. ironlake_fdi_disable(crtc);
  4277. for_each_encoder_on_crtc(dev, crtc, encoder)
  4278. if (encoder->post_disable)
  4279. encoder->post_disable(encoder);
  4280. if (intel_crtc->config->has_pch_encoder) {
  4281. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4282. if (HAS_PCH_CPT(dev)) {
  4283. /* disable TRANS_DP_CTL */
  4284. reg = TRANS_DP_CTL(pipe);
  4285. temp = I915_READ(reg);
  4286. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4287. TRANS_DP_PORT_SEL_MASK);
  4288. temp |= TRANS_DP_PORT_SEL_NONE;
  4289. I915_WRITE(reg, temp);
  4290. /* disable DPLL_SEL */
  4291. temp = I915_READ(PCH_DPLL_SEL);
  4292. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4293. I915_WRITE(PCH_DPLL_SEL, temp);
  4294. }
  4295. /* disable PCH DPLL */
  4296. intel_disable_shared_dpll(intel_crtc);
  4297. ironlake_fdi_pll_disable(intel_crtc);
  4298. }
  4299. intel_crtc->active = false;
  4300. intel_update_watermarks(crtc);
  4301. mutex_lock(&dev->struct_mutex);
  4302. intel_fbc_update(dev);
  4303. mutex_unlock(&dev->struct_mutex);
  4304. }
  4305. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4306. {
  4307. struct drm_device *dev = crtc->dev;
  4308. struct drm_i915_private *dev_priv = dev->dev_private;
  4309. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4310. struct intel_encoder *encoder;
  4311. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4312. if (WARN_ON(!intel_crtc->active))
  4313. return;
  4314. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4315. intel_opregion_notify_encoder(encoder, false);
  4316. encoder->disable(encoder);
  4317. }
  4318. drm_crtc_vblank_off(crtc);
  4319. assert_vblank_disabled(crtc);
  4320. if (intel_crtc->config->has_pch_encoder)
  4321. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4322. false);
  4323. intel_disable_pipe(intel_crtc);
  4324. if (intel_crtc->config->dp_encoder_is_mst)
  4325. intel_ddi_set_vc_payload_alloc(crtc, false);
  4326. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4327. if (INTEL_INFO(dev)->gen == 9)
  4328. skylake_pfit_update(intel_crtc, 0);
  4329. else if (INTEL_INFO(dev)->gen < 9)
  4330. ironlake_pfit_disable(intel_crtc);
  4331. else
  4332. MISSING_CASE(INTEL_INFO(dev)->gen);
  4333. intel_ddi_disable_pipe_clock(intel_crtc);
  4334. if (intel_crtc->config->has_pch_encoder) {
  4335. lpt_disable_pch_transcoder(dev_priv);
  4336. intel_ddi_fdi_disable(crtc);
  4337. }
  4338. for_each_encoder_on_crtc(dev, crtc, encoder)
  4339. if (encoder->post_disable)
  4340. encoder->post_disable(encoder);
  4341. intel_crtc->active = false;
  4342. intel_update_watermarks(crtc);
  4343. mutex_lock(&dev->struct_mutex);
  4344. intel_fbc_update(dev);
  4345. mutex_unlock(&dev->struct_mutex);
  4346. if (intel_crtc_to_shared_dpll(intel_crtc))
  4347. intel_disable_shared_dpll(intel_crtc);
  4348. }
  4349. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4350. {
  4351. struct drm_device *dev = crtc->base.dev;
  4352. struct drm_i915_private *dev_priv = dev->dev_private;
  4353. struct intel_crtc_state *pipe_config = crtc->config;
  4354. if (!pipe_config->gmch_pfit.control)
  4355. return;
  4356. /*
  4357. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4358. * according to register description and PRM.
  4359. */
  4360. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4361. assert_pipe_disabled(dev_priv, crtc->pipe);
  4362. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4363. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4364. /* Border color in case we don't scale up to the full screen. Black by
  4365. * default, change to something else for debugging. */
  4366. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4367. }
  4368. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4369. {
  4370. switch (port) {
  4371. case PORT_A:
  4372. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4373. case PORT_B:
  4374. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4375. case PORT_C:
  4376. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4377. case PORT_D:
  4378. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4379. default:
  4380. WARN_ON_ONCE(1);
  4381. return POWER_DOMAIN_PORT_OTHER;
  4382. }
  4383. }
  4384. #define for_each_power_domain(domain, mask) \
  4385. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4386. if ((1 << (domain)) & (mask))
  4387. enum intel_display_power_domain
  4388. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4389. {
  4390. struct drm_device *dev = intel_encoder->base.dev;
  4391. struct intel_digital_port *intel_dig_port;
  4392. switch (intel_encoder->type) {
  4393. case INTEL_OUTPUT_UNKNOWN:
  4394. /* Only DDI platforms should ever use this output type */
  4395. WARN_ON_ONCE(!HAS_DDI(dev));
  4396. case INTEL_OUTPUT_DISPLAYPORT:
  4397. case INTEL_OUTPUT_HDMI:
  4398. case INTEL_OUTPUT_EDP:
  4399. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4400. return port_to_power_domain(intel_dig_port->port);
  4401. case INTEL_OUTPUT_DP_MST:
  4402. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4403. return port_to_power_domain(intel_dig_port->port);
  4404. case INTEL_OUTPUT_ANALOG:
  4405. return POWER_DOMAIN_PORT_CRT;
  4406. case INTEL_OUTPUT_DSI:
  4407. return POWER_DOMAIN_PORT_DSI;
  4408. default:
  4409. return POWER_DOMAIN_PORT_OTHER;
  4410. }
  4411. }
  4412. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4413. {
  4414. struct drm_device *dev = crtc->dev;
  4415. struct intel_encoder *intel_encoder;
  4416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4417. enum pipe pipe = intel_crtc->pipe;
  4418. unsigned long mask;
  4419. enum transcoder transcoder;
  4420. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4421. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4422. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4423. if (intel_crtc->config->pch_pfit.enabled ||
  4424. intel_crtc->config->pch_pfit.force_thru)
  4425. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4426. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4427. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4428. return mask;
  4429. }
  4430. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4431. {
  4432. struct drm_device *dev = state->dev;
  4433. struct drm_i915_private *dev_priv = dev->dev_private;
  4434. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4435. struct intel_crtc *crtc;
  4436. /*
  4437. * First get all needed power domains, then put all unneeded, to avoid
  4438. * any unnecessary toggling of the power wells.
  4439. */
  4440. for_each_intel_crtc(dev, crtc) {
  4441. enum intel_display_power_domain domain;
  4442. if (!crtc->base.state->enable)
  4443. continue;
  4444. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4445. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4446. intel_display_power_get(dev_priv, domain);
  4447. }
  4448. if (dev_priv->display.modeset_global_resources)
  4449. dev_priv->display.modeset_global_resources(state);
  4450. for_each_intel_crtc(dev, crtc) {
  4451. enum intel_display_power_domain domain;
  4452. for_each_power_domain(domain, crtc->enabled_power_domains)
  4453. intel_display_power_put(dev_priv, domain);
  4454. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4455. }
  4456. intel_display_set_init_power(dev_priv, false);
  4457. }
  4458. static void intel_update_max_cdclk(struct drm_device *dev)
  4459. {
  4460. struct drm_i915_private *dev_priv = dev->dev_private;
  4461. if (IS_SKYLAKE(dev)) {
  4462. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4463. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4464. dev_priv->max_cdclk_freq = 675000;
  4465. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4466. dev_priv->max_cdclk_freq = 540000;
  4467. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4468. dev_priv->max_cdclk_freq = 450000;
  4469. else
  4470. dev_priv->max_cdclk_freq = 337500;
  4471. } else if (IS_BROADWELL(dev)) {
  4472. /*
  4473. * FIXME with extra cooling we can allow
  4474. * 540 MHz for ULX and 675 Mhz for ULT.
  4475. * How can we know if extra cooling is
  4476. * available? PCI ID, VTB, something else?
  4477. */
  4478. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4479. dev_priv->max_cdclk_freq = 450000;
  4480. else if (IS_BDW_ULX(dev))
  4481. dev_priv->max_cdclk_freq = 450000;
  4482. else if (IS_BDW_ULT(dev))
  4483. dev_priv->max_cdclk_freq = 540000;
  4484. else
  4485. dev_priv->max_cdclk_freq = 675000;
  4486. } else if (IS_CHERRYVIEW(dev)) {
  4487. dev_priv->max_cdclk_freq = 320000;
  4488. } else if (IS_VALLEYVIEW(dev)) {
  4489. dev_priv->max_cdclk_freq = 400000;
  4490. } else {
  4491. /* otherwise assume cdclk is fixed */
  4492. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4493. }
  4494. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4495. dev_priv->max_cdclk_freq);
  4496. }
  4497. static void intel_update_cdclk(struct drm_device *dev)
  4498. {
  4499. struct drm_i915_private *dev_priv = dev->dev_private;
  4500. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4501. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4502. dev_priv->cdclk_freq);
  4503. /*
  4504. * Program the gmbus_freq based on the cdclk frequency.
  4505. * BSpec erroneously claims we should aim for 4MHz, but
  4506. * in fact 1MHz is the correct frequency.
  4507. */
  4508. if (IS_VALLEYVIEW(dev)) {
  4509. /*
  4510. * Program the gmbus_freq based on the cdclk frequency.
  4511. * BSpec erroneously claims we should aim for 4MHz, but
  4512. * in fact 1MHz is the correct frequency.
  4513. */
  4514. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4515. }
  4516. if (dev_priv->max_cdclk_freq == 0)
  4517. intel_update_max_cdclk(dev);
  4518. }
  4519. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4520. {
  4521. struct drm_i915_private *dev_priv = dev->dev_private;
  4522. uint32_t divider;
  4523. uint32_t ratio;
  4524. uint32_t current_freq;
  4525. int ret;
  4526. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4527. switch (frequency) {
  4528. case 144000:
  4529. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4530. ratio = BXT_DE_PLL_RATIO(60);
  4531. break;
  4532. case 288000:
  4533. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4534. ratio = BXT_DE_PLL_RATIO(60);
  4535. break;
  4536. case 384000:
  4537. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4538. ratio = BXT_DE_PLL_RATIO(60);
  4539. break;
  4540. case 576000:
  4541. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4542. ratio = BXT_DE_PLL_RATIO(60);
  4543. break;
  4544. case 624000:
  4545. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4546. ratio = BXT_DE_PLL_RATIO(65);
  4547. break;
  4548. case 19200:
  4549. /*
  4550. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4551. * to suppress GCC warning.
  4552. */
  4553. ratio = 0;
  4554. divider = 0;
  4555. break;
  4556. default:
  4557. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4558. return;
  4559. }
  4560. mutex_lock(&dev_priv->rps.hw_lock);
  4561. /* Inform power controller of upcoming frequency change */
  4562. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4563. 0x80000000);
  4564. mutex_unlock(&dev_priv->rps.hw_lock);
  4565. if (ret) {
  4566. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4567. ret, frequency);
  4568. return;
  4569. }
  4570. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4571. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4572. current_freq = current_freq * 500 + 1000;
  4573. /*
  4574. * DE PLL has to be disabled when
  4575. * - setting to 19.2MHz (bypass, PLL isn't used)
  4576. * - before setting to 624MHz (PLL needs toggling)
  4577. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4578. */
  4579. if (frequency == 19200 || frequency == 624000 ||
  4580. current_freq == 624000) {
  4581. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4582. /* Timeout 200us */
  4583. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4584. 1))
  4585. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4586. }
  4587. if (frequency != 19200) {
  4588. uint32_t val;
  4589. val = I915_READ(BXT_DE_PLL_CTL);
  4590. val &= ~BXT_DE_PLL_RATIO_MASK;
  4591. val |= ratio;
  4592. I915_WRITE(BXT_DE_PLL_CTL, val);
  4593. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4594. /* Timeout 200us */
  4595. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4596. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4597. val = I915_READ(CDCLK_CTL);
  4598. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4599. val |= divider;
  4600. /*
  4601. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4602. * enable otherwise.
  4603. */
  4604. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4605. if (frequency >= 500000)
  4606. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4607. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4608. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4609. val |= (frequency - 1000) / 500;
  4610. I915_WRITE(CDCLK_CTL, val);
  4611. }
  4612. mutex_lock(&dev_priv->rps.hw_lock);
  4613. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4614. DIV_ROUND_UP(frequency, 25000));
  4615. mutex_unlock(&dev_priv->rps.hw_lock);
  4616. if (ret) {
  4617. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4618. ret, frequency);
  4619. return;
  4620. }
  4621. intel_update_cdclk(dev);
  4622. }
  4623. void broxton_init_cdclk(struct drm_device *dev)
  4624. {
  4625. struct drm_i915_private *dev_priv = dev->dev_private;
  4626. uint32_t val;
  4627. /*
  4628. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4629. * or else the reset will hang because there is no PCH to respond.
  4630. * Move the handshake programming to initialization sequence.
  4631. * Previously was left up to BIOS.
  4632. */
  4633. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4634. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4635. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4636. /* Enable PG1 for cdclk */
  4637. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4638. /* check if cd clock is enabled */
  4639. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4640. DRM_DEBUG_KMS("Display already initialized\n");
  4641. return;
  4642. }
  4643. /*
  4644. * FIXME:
  4645. * - The initial CDCLK needs to be read from VBT.
  4646. * Need to make this change after VBT has changes for BXT.
  4647. * - check if setting the max (or any) cdclk freq is really necessary
  4648. * here, it belongs to modeset time
  4649. */
  4650. broxton_set_cdclk(dev, 624000);
  4651. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4652. POSTING_READ(DBUF_CTL);
  4653. udelay(10);
  4654. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4655. DRM_ERROR("DBuf power enable timeout!\n");
  4656. }
  4657. void broxton_uninit_cdclk(struct drm_device *dev)
  4658. {
  4659. struct drm_i915_private *dev_priv = dev->dev_private;
  4660. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4661. POSTING_READ(DBUF_CTL);
  4662. udelay(10);
  4663. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4664. DRM_ERROR("DBuf power disable timeout!\n");
  4665. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4666. broxton_set_cdclk(dev, 19200);
  4667. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4668. }
  4669. static const struct skl_cdclk_entry {
  4670. unsigned int freq;
  4671. unsigned int vco;
  4672. } skl_cdclk_frequencies[] = {
  4673. { .freq = 308570, .vco = 8640 },
  4674. { .freq = 337500, .vco = 8100 },
  4675. { .freq = 432000, .vco = 8640 },
  4676. { .freq = 450000, .vco = 8100 },
  4677. { .freq = 540000, .vco = 8100 },
  4678. { .freq = 617140, .vco = 8640 },
  4679. { .freq = 675000, .vco = 8100 },
  4680. };
  4681. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4682. {
  4683. return (freq - 1000) / 500;
  4684. }
  4685. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4686. {
  4687. unsigned int i;
  4688. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4689. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4690. if (e->freq == freq)
  4691. return e->vco;
  4692. }
  4693. return 8100;
  4694. }
  4695. static void
  4696. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4697. {
  4698. unsigned int min_freq;
  4699. u32 val;
  4700. /* select the minimum CDCLK before enabling DPLL 0 */
  4701. val = I915_READ(CDCLK_CTL);
  4702. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4703. val |= CDCLK_FREQ_337_308;
  4704. if (required_vco == 8640)
  4705. min_freq = 308570;
  4706. else
  4707. min_freq = 337500;
  4708. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4709. I915_WRITE(CDCLK_CTL, val);
  4710. POSTING_READ(CDCLK_CTL);
  4711. /*
  4712. * We always enable DPLL0 with the lowest link rate possible, but still
  4713. * taking into account the VCO required to operate the eDP panel at the
  4714. * desired frequency. The usual DP link rates operate with a VCO of
  4715. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4716. * The modeset code is responsible for the selection of the exact link
  4717. * rate later on, with the constraint of choosing a frequency that
  4718. * works with required_vco.
  4719. */
  4720. val = I915_READ(DPLL_CTRL1);
  4721. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4722. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4723. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4724. if (required_vco == 8640)
  4725. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4726. SKL_DPLL0);
  4727. else
  4728. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4729. SKL_DPLL0);
  4730. I915_WRITE(DPLL_CTRL1, val);
  4731. POSTING_READ(DPLL_CTRL1);
  4732. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4733. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4734. DRM_ERROR("DPLL0 not locked\n");
  4735. }
  4736. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4737. {
  4738. int ret;
  4739. u32 val;
  4740. /* inform PCU we want to change CDCLK */
  4741. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4742. mutex_lock(&dev_priv->rps.hw_lock);
  4743. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4744. mutex_unlock(&dev_priv->rps.hw_lock);
  4745. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4746. }
  4747. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4748. {
  4749. unsigned int i;
  4750. for (i = 0; i < 15; i++) {
  4751. if (skl_cdclk_pcu_ready(dev_priv))
  4752. return true;
  4753. udelay(10);
  4754. }
  4755. return false;
  4756. }
  4757. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4758. {
  4759. struct drm_device *dev = dev_priv->dev;
  4760. u32 freq_select, pcu_ack;
  4761. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4762. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4763. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4764. return;
  4765. }
  4766. /* set CDCLK_CTL */
  4767. switch(freq) {
  4768. case 450000:
  4769. case 432000:
  4770. freq_select = CDCLK_FREQ_450_432;
  4771. pcu_ack = 1;
  4772. break;
  4773. case 540000:
  4774. freq_select = CDCLK_FREQ_540;
  4775. pcu_ack = 2;
  4776. break;
  4777. case 308570:
  4778. case 337500:
  4779. default:
  4780. freq_select = CDCLK_FREQ_337_308;
  4781. pcu_ack = 0;
  4782. break;
  4783. case 617140:
  4784. case 675000:
  4785. freq_select = CDCLK_FREQ_675_617;
  4786. pcu_ack = 3;
  4787. break;
  4788. }
  4789. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4790. POSTING_READ(CDCLK_CTL);
  4791. /* inform PCU of the change */
  4792. mutex_lock(&dev_priv->rps.hw_lock);
  4793. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4794. mutex_unlock(&dev_priv->rps.hw_lock);
  4795. intel_update_cdclk(dev);
  4796. }
  4797. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4798. {
  4799. /* disable DBUF power */
  4800. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4801. POSTING_READ(DBUF_CTL);
  4802. udelay(10);
  4803. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4804. DRM_ERROR("DBuf power disable timeout\n");
  4805. /* disable DPLL0 */
  4806. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4807. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4808. DRM_ERROR("Couldn't disable DPLL0\n");
  4809. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4810. }
  4811. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4812. {
  4813. u32 val;
  4814. unsigned int required_vco;
  4815. /* enable PCH reset handshake */
  4816. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4817. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4818. /* enable PG1 and Misc I/O */
  4819. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4820. /* DPLL0 already enabed !? */
  4821. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4822. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4823. return;
  4824. }
  4825. /* enable DPLL0 */
  4826. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4827. skl_dpll0_enable(dev_priv, required_vco);
  4828. /* set CDCLK to the frequency the BIOS chose */
  4829. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4830. /* enable DBUF power */
  4831. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4832. POSTING_READ(DBUF_CTL);
  4833. udelay(10);
  4834. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4835. DRM_ERROR("DBuf power enable timeout\n");
  4836. }
  4837. /* returns HPLL frequency in kHz */
  4838. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4839. {
  4840. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4841. /* Obtain SKU information */
  4842. mutex_lock(&dev_priv->sb_lock);
  4843. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4844. CCK_FUSE_HPLL_FREQ_MASK;
  4845. mutex_unlock(&dev_priv->sb_lock);
  4846. return vco_freq[hpll_freq] * 1000;
  4847. }
  4848. /* Adjust CDclk dividers to allow high res or save power if possible */
  4849. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4850. {
  4851. struct drm_i915_private *dev_priv = dev->dev_private;
  4852. u32 val, cmd;
  4853. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4854. != dev_priv->cdclk_freq);
  4855. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4856. cmd = 2;
  4857. else if (cdclk == 266667)
  4858. cmd = 1;
  4859. else
  4860. cmd = 0;
  4861. mutex_lock(&dev_priv->rps.hw_lock);
  4862. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4863. val &= ~DSPFREQGUAR_MASK;
  4864. val |= (cmd << DSPFREQGUAR_SHIFT);
  4865. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4866. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4867. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4868. 50)) {
  4869. DRM_ERROR("timed out waiting for CDclk change\n");
  4870. }
  4871. mutex_unlock(&dev_priv->rps.hw_lock);
  4872. mutex_lock(&dev_priv->sb_lock);
  4873. if (cdclk == 400000) {
  4874. u32 divider;
  4875. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4876. /* adjust cdclk divider */
  4877. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4878. val &= ~DISPLAY_FREQUENCY_VALUES;
  4879. val |= divider;
  4880. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4881. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4882. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4883. 50))
  4884. DRM_ERROR("timed out waiting for CDclk change\n");
  4885. }
  4886. /* adjust self-refresh exit latency value */
  4887. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4888. val &= ~0x7f;
  4889. /*
  4890. * For high bandwidth configs, we set a higher latency in the bunit
  4891. * so that the core display fetch happens in time to avoid underruns.
  4892. */
  4893. if (cdclk == 400000)
  4894. val |= 4500 / 250; /* 4.5 usec */
  4895. else
  4896. val |= 3000 / 250; /* 3.0 usec */
  4897. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4898. mutex_unlock(&dev_priv->sb_lock);
  4899. intel_update_cdclk(dev);
  4900. }
  4901. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4902. {
  4903. struct drm_i915_private *dev_priv = dev->dev_private;
  4904. u32 val, cmd;
  4905. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4906. != dev_priv->cdclk_freq);
  4907. switch (cdclk) {
  4908. case 333333:
  4909. case 320000:
  4910. case 266667:
  4911. case 200000:
  4912. break;
  4913. default:
  4914. MISSING_CASE(cdclk);
  4915. return;
  4916. }
  4917. /*
  4918. * Specs are full of misinformation, but testing on actual
  4919. * hardware has shown that we just need to write the desired
  4920. * CCK divider into the Punit register.
  4921. */
  4922. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4923. mutex_lock(&dev_priv->rps.hw_lock);
  4924. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4925. val &= ~DSPFREQGUAR_MASK_CHV;
  4926. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4927. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4928. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4929. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4930. 50)) {
  4931. DRM_ERROR("timed out waiting for CDclk change\n");
  4932. }
  4933. mutex_unlock(&dev_priv->rps.hw_lock);
  4934. intel_update_cdclk(dev);
  4935. }
  4936. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4937. int max_pixclk)
  4938. {
  4939. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4940. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4941. /*
  4942. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4943. * 200MHz
  4944. * 267MHz
  4945. * 320/333MHz (depends on HPLL freq)
  4946. * 400MHz (VLV only)
  4947. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4948. * of the lower bin and adjust if needed.
  4949. *
  4950. * We seem to get an unstable or solid color picture at 200MHz.
  4951. * Not sure what's wrong. For now use 200MHz only when all pipes
  4952. * are off.
  4953. */
  4954. if (!IS_CHERRYVIEW(dev_priv) &&
  4955. max_pixclk > freq_320*limit/100)
  4956. return 400000;
  4957. else if (max_pixclk > 266667*limit/100)
  4958. return freq_320;
  4959. else if (max_pixclk > 0)
  4960. return 266667;
  4961. else
  4962. return 200000;
  4963. }
  4964. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4965. int max_pixclk)
  4966. {
  4967. /*
  4968. * FIXME:
  4969. * - remove the guardband, it's not needed on BXT
  4970. * - set 19.2MHz bypass frequency if there are no active pipes
  4971. */
  4972. if (max_pixclk > 576000*9/10)
  4973. return 624000;
  4974. else if (max_pixclk > 384000*9/10)
  4975. return 576000;
  4976. else if (max_pixclk > 288000*9/10)
  4977. return 384000;
  4978. else if (max_pixclk > 144000*9/10)
  4979. return 288000;
  4980. else
  4981. return 144000;
  4982. }
  4983. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4984. * that's non-NULL, look at current state otherwise. */
  4985. static int intel_mode_max_pixclk(struct drm_device *dev,
  4986. struct drm_atomic_state *state)
  4987. {
  4988. struct intel_crtc *intel_crtc;
  4989. struct intel_crtc_state *crtc_state;
  4990. int max_pixclk = 0;
  4991. for_each_intel_crtc(dev, intel_crtc) {
  4992. if (state)
  4993. crtc_state =
  4994. intel_atomic_get_crtc_state(state, intel_crtc);
  4995. else
  4996. crtc_state = intel_crtc->config;
  4997. if (IS_ERR(crtc_state))
  4998. return PTR_ERR(crtc_state);
  4999. if (!crtc_state->base.enable)
  5000. continue;
  5001. max_pixclk = max(max_pixclk,
  5002. crtc_state->base.adjusted_mode.crtc_clock);
  5003. }
  5004. return max_pixclk;
  5005. }
  5006. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  5007. {
  5008. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5009. struct drm_crtc *crtc;
  5010. struct drm_crtc_state *crtc_state;
  5011. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  5012. int cdclk, ret = 0;
  5013. if (max_pixclk < 0)
  5014. return max_pixclk;
  5015. if (IS_VALLEYVIEW(dev_priv))
  5016. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5017. else
  5018. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5019. if (cdclk == dev_priv->cdclk_freq)
  5020. return 0;
  5021. /* add all active pipes to the state */
  5022. for_each_crtc(state->dev, crtc) {
  5023. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5024. if (IS_ERR(crtc_state))
  5025. return PTR_ERR(crtc_state);
  5026. if (!crtc_state->active || needs_modeset(crtc_state))
  5027. continue;
  5028. crtc_state->mode_changed = true;
  5029. ret = drm_atomic_add_affected_connectors(state, crtc);
  5030. if (ret)
  5031. break;
  5032. ret = drm_atomic_add_affected_planes(state, crtc);
  5033. if (ret)
  5034. break;
  5035. }
  5036. return ret;
  5037. }
  5038. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5039. {
  5040. unsigned int credits, default_credits;
  5041. if (IS_CHERRYVIEW(dev_priv))
  5042. default_credits = PFI_CREDIT(12);
  5043. else
  5044. default_credits = PFI_CREDIT(8);
  5045. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5046. /* CHV suggested value is 31 or 63 */
  5047. if (IS_CHERRYVIEW(dev_priv))
  5048. credits = PFI_CREDIT_63;
  5049. else
  5050. credits = PFI_CREDIT(15);
  5051. } else {
  5052. credits = default_credits;
  5053. }
  5054. /*
  5055. * WA - write default credits before re-programming
  5056. * FIXME: should we also set the resend bit here?
  5057. */
  5058. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5059. default_credits);
  5060. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5061. credits | PFI_CREDIT_RESEND);
  5062. /*
  5063. * FIXME is this guaranteed to clear
  5064. * immediately or should we poll for it?
  5065. */
  5066. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5067. }
  5068. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5069. {
  5070. struct drm_device *dev = old_state->dev;
  5071. struct drm_i915_private *dev_priv = dev->dev_private;
  5072. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5073. int req_cdclk;
  5074. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5075. * never fail. */
  5076. if (WARN_ON(max_pixclk < 0))
  5077. return;
  5078. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5079. if (req_cdclk != dev_priv->cdclk_freq) {
  5080. /*
  5081. * FIXME: We can end up here with all power domains off, yet
  5082. * with a CDCLK frequency other than the minimum. To account
  5083. * for this take the PIPE-A power domain, which covers the HW
  5084. * blocks needed for the following programming. This can be
  5085. * removed once it's guaranteed that we get here either with
  5086. * the minimum CDCLK set, or the required power domains
  5087. * enabled.
  5088. */
  5089. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5090. if (IS_CHERRYVIEW(dev))
  5091. cherryview_set_cdclk(dev, req_cdclk);
  5092. else
  5093. valleyview_set_cdclk(dev, req_cdclk);
  5094. vlv_program_pfi_credits(dev_priv);
  5095. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5096. }
  5097. }
  5098. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5099. {
  5100. struct drm_device *dev = crtc->dev;
  5101. struct drm_i915_private *dev_priv = to_i915(dev);
  5102. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5103. struct intel_encoder *encoder;
  5104. int pipe = intel_crtc->pipe;
  5105. bool is_dsi;
  5106. if (WARN_ON(intel_crtc->active))
  5107. return;
  5108. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5109. if (!is_dsi) {
  5110. if (IS_CHERRYVIEW(dev))
  5111. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5112. else
  5113. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5114. }
  5115. if (intel_crtc->config->has_dp_encoder)
  5116. intel_dp_set_m_n(intel_crtc, M1_N1);
  5117. intel_set_pipe_timings(intel_crtc);
  5118. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5119. struct drm_i915_private *dev_priv = dev->dev_private;
  5120. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5121. I915_WRITE(CHV_CANVAS(pipe), 0);
  5122. }
  5123. i9xx_set_pipeconf(intel_crtc);
  5124. intel_crtc->active = true;
  5125. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5126. for_each_encoder_on_crtc(dev, crtc, encoder)
  5127. if (encoder->pre_pll_enable)
  5128. encoder->pre_pll_enable(encoder);
  5129. if (!is_dsi) {
  5130. if (IS_CHERRYVIEW(dev))
  5131. chv_enable_pll(intel_crtc, intel_crtc->config);
  5132. else
  5133. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5134. }
  5135. for_each_encoder_on_crtc(dev, crtc, encoder)
  5136. if (encoder->pre_enable)
  5137. encoder->pre_enable(encoder);
  5138. i9xx_pfit_enable(intel_crtc);
  5139. intel_crtc_load_lut(crtc);
  5140. intel_update_watermarks(crtc);
  5141. intel_enable_pipe(intel_crtc);
  5142. assert_vblank_disabled(crtc);
  5143. drm_crtc_vblank_on(crtc);
  5144. for_each_encoder_on_crtc(dev, crtc, encoder)
  5145. encoder->enable(encoder);
  5146. }
  5147. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5148. {
  5149. struct drm_device *dev = crtc->base.dev;
  5150. struct drm_i915_private *dev_priv = dev->dev_private;
  5151. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5152. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5153. }
  5154. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5155. {
  5156. struct drm_device *dev = crtc->dev;
  5157. struct drm_i915_private *dev_priv = to_i915(dev);
  5158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5159. struct intel_encoder *encoder;
  5160. int pipe = intel_crtc->pipe;
  5161. if (WARN_ON(intel_crtc->active))
  5162. return;
  5163. i9xx_set_pll_dividers(intel_crtc);
  5164. if (intel_crtc->config->has_dp_encoder)
  5165. intel_dp_set_m_n(intel_crtc, M1_N1);
  5166. intel_set_pipe_timings(intel_crtc);
  5167. i9xx_set_pipeconf(intel_crtc);
  5168. intel_crtc->active = true;
  5169. if (!IS_GEN2(dev))
  5170. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5171. for_each_encoder_on_crtc(dev, crtc, encoder)
  5172. if (encoder->pre_enable)
  5173. encoder->pre_enable(encoder);
  5174. i9xx_enable_pll(intel_crtc);
  5175. i9xx_pfit_enable(intel_crtc);
  5176. intel_crtc_load_lut(crtc);
  5177. intel_update_watermarks(crtc);
  5178. intel_enable_pipe(intel_crtc);
  5179. assert_vblank_disabled(crtc);
  5180. drm_crtc_vblank_on(crtc);
  5181. for_each_encoder_on_crtc(dev, crtc, encoder)
  5182. encoder->enable(encoder);
  5183. }
  5184. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5185. {
  5186. struct drm_device *dev = crtc->base.dev;
  5187. struct drm_i915_private *dev_priv = dev->dev_private;
  5188. if (!crtc->config->gmch_pfit.control)
  5189. return;
  5190. assert_pipe_disabled(dev_priv, crtc->pipe);
  5191. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5192. I915_READ(PFIT_CONTROL));
  5193. I915_WRITE(PFIT_CONTROL, 0);
  5194. }
  5195. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5196. {
  5197. struct drm_device *dev = crtc->dev;
  5198. struct drm_i915_private *dev_priv = dev->dev_private;
  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. /*
  5205. * On gen2 planes are double buffered but the pipe isn't, so we must
  5206. * wait for planes to fully turn off before disabling the pipe.
  5207. * We also need to wait on all gmch platforms because of the
  5208. * self-refresh mode constraint explained above.
  5209. */
  5210. intel_wait_for_vblank(dev, pipe);
  5211. for_each_encoder_on_crtc(dev, crtc, encoder)
  5212. encoder->disable(encoder);
  5213. drm_crtc_vblank_off(crtc);
  5214. assert_vblank_disabled(crtc);
  5215. intel_disable_pipe(intel_crtc);
  5216. i9xx_pfit_disable(intel_crtc);
  5217. for_each_encoder_on_crtc(dev, crtc, encoder)
  5218. if (encoder->post_disable)
  5219. encoder->post_disable(encoder);
  5220. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5221. if (IS_CHERRYVIEW(dev))
  5222. chv_disable_pll(dev_priv, pipe);
  5223. else if (IS_VALLEYVIEW(dev))
  5224. vlv_disable_pll(dev_priv, pipe);
  5225. else
  5226. i9xx_disable_pll(intel_crtc);
  5227. }
  5228. if (!IS_GEN2(dev))
  5229. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5230. intel_crtc->active = false;
  5231. intel_update_watermarks(crtc);
  5232. mutex_lock(&dev->struct_mutex);
  5233. intel_fbc_update(dev);
  5234. mutex_unlock(&dev->struct_mutex);
  5235. }
  5236. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5237. {
  5238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5239. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5240. enum intel_display_power_domain domain;
  5241. unsigned long domains;
  5242. if (!intel_crtc->active)
  5243. return;
  5244. intel_crtc_disable_planes(crtc);
  5245. dev_priv->display.crtc_disable(crtc);
  5246. domains = intel_crtc->enabled_power_domains;
  5247. for_each_power_domain(domain, domains)
  5248. intel_display_power_put(dev_priv, domain);
  5249. intel_crtc->enabled_power_domains = 0;
  5250. }
  5251. /*
  5252. * turn all crtc's off, but do not adjust state
  5253. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5254. */
  5255. void intel_display_suspend(struct drm_device *dev)
  5256. {
  5257. struct drm_crtc *crtc;
  5258. for_each_crtc(dev, crtc)
  5259. intel_crtc_disable_noatomic(crtc);
  5260. }
  5261. /* Master function to enable/disable CRTC and corresponding power wells */
  5262. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5263. {
  5264. struct drm_device *dev = crtc->dev;
  5265. struct drm_mode_config *config = &dev->mode_config;
  5266. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5267. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5268. struct intel_crtc_state *pipe_config;
  5269. struct drm_atomic_state *state;
  5270. int ret;
  5271. if (enable == intel_crtc->active)
  5272. return 0;
  5273. if (enable && !crtc->state->enable)
  5274. return 0;
  5275. /* this function should be called with drm_modeset_lock_all for now */
  5276. if (WARN_ON(!ctx))
  5277. return -EIO;
  5278. lockdep_assert_held(&ctx->ww_ctx);
  5279. state = drm_atomic_state_alloc(dev);
  5280. if (WARN_ON(!state))
  5281. return -ENOMEM;
  5282. state->acquire_ctx = ctx;
  5283. state->allow_modeset = true;
  5284. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5285. if (IS_ERR(pipe_config)) {
  5286. ret = PTR_ERR(pipe_config);
  5287. goto err;
  5288. }
  5289. pipe_config->base.active = enable;
  5290. ret = intel_set_mode(state);
  5291. if (!ret)
  5292. return ret;
  5293. err:
  5294. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5295. drm_atomic_state_free(state);
  5296. return ret;
  5297. }
  5298. /**
  5299. * Sets the power management mode of the pipe and plane.
  5300. */
  5301. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5302. {
  5303. struct drm_device *dev = crtc->dev;
  5304. struct intel_encoder *intel_encoder;
  5305. bool enable = false;
  5306. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5307. enable |= intel_encoder->connectors_active;
  5308. intel_crtc_control(crtc, enable);
  5309. }
  5310. void intel_encoder_destroy(struct drm_encoder *encoder)
  5311. {
  5312. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5313. drm_encoder_cleanup(encoder);
  5314. kfree(intel_encoder);
  5315. }
  5316. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5317. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5318. * state of the entire output pipe. */
  5319. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5320. {
  5321. if (mode == DRM_MODE_DPMS_ON) {
  5322. encoder->connectors_active = true;
  5323. intel_crtc_update_dpms(encoder->base.crtc);
  5324. } else {
  5325. encoder->connectors_active = false;
  5326. intel_crtc_update_dpms(encoder->base.crtc);
  5327. }
  5328. }
  5329. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5330. * internal consistency). */
  5331. static void intel_connector_check_state(struct intel_connector *connector)
  5332. {
  5333. if (connector->get_hw_state(connector)) {
  5334. struct intel_encoder *encoder = connector->encoder;
  5335. struct drm_crtc *crtc;
  5336. bool encoder_enabled;
  5337. enum pipe pipe;
  5338. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5339. connector->base.base.id,
  5340. connector->base.name);
  5341. /* there is no real hw state for MST connectors */
  5342. if (connector->mst_port)
  5343. return;
  5344. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5345. "wrong connector dpms state\n");
  5346. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5347. "active connector not linked to encoder\n");
  5348. if (encoder) {
  5349. I915_STATE_WARN(!encoder->connectors_active,
  5350. "encoder->connectors_active not set\n");
  5351. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5352. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5353. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5354. return;
  5355. crtc = encoder->base.crtc;
  5356. I915_STATE_WARN(!crtc->state->enable,
  5357. "crtc not enabled\n");
  5358. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5359. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5360. "encoder active on the wrong pipe\n");
  5361. }
  5362. }
  5363. }
  5364. int intel_connector_init(struct intel_connector *connector)
  5365. {
  5366. struct drm_connector_state *connector_state;
  5367. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5368. if (!connector_state)
  5369. return -ENOMEM;
  5370. connector->base.state = connector_state;
  5371. return 0;
  5372. }
  5373. struct intel_connector *intel_connector_alloc(void)
  5374. {
  5375. struct intel_connector *connector;
  5376. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5377. if (!connector)
  5378. return NULL;
  5379. if (intel_connector_init(connector) < 0) {
  5380. kfree(connector);
  5381. return NULL;
  5382. }
  5383. return connector;
  5384. }
  5385. /* Even simpler default implementation, if there's really no special case to
  5386. * consider. */
  5387. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5388. {
  5389. /* All the simple cases only support two dpms states. */
  5390. if (mode != DRM_MODE_DPMS_ON)
  5391. mode = DRM_MODE_DPMS_OFF;
  5392. if (mode == connector->dpms)
  5393. return;
  5394. connector->dpms = mode;
  5395. /* Only need to change hw state when actually enabled */
  5396. if (connector->encoder)
  5397. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5398. intel_modeset_check_state(connector->dev);
  5399. }
  5400. /* Simple connector->get_hw_state implementation for encoders that support only
  5401. * one connector and no cloning and hence the encoder state determines the state
  5402. * of the connector. */
  5403. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5404. {
  5405. enum pipe pipe = 0;
  5406. struct intel_encoder *encoder = connector->encoder;
  5407. return encoder->get_hw_state(encoder, &pipe);
  5408. }
  5409. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5410. {
  5411. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5412. return crtc_state->fdi_lanes;
  5413. return 0;
  5414. }
  5415. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5416. struct intel_crtc_state *pipe_config)
  5417. {
  5418. struct drm_atomic_state *state = pipe_config->base.state;
  5419. struct intel_crtc *other_crtc;
  5420. struct intel_crtc_state *other_crtc_state;
  5421. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5422. pipe_name(pipe), pipe_config->fdi_lanes);
  5423. if (pipe_config->fdi_lanes > 4) {
  5424. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5425. pipe_name(pipe), pipe_config->fdi_lanes);
  5426. return -EINVAL;
  5427. }
  5428. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5429. if (pipe_config->fdi_lanes > 2) {
  5430. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5431. pipe_config->fdi_lanes);
  5432. return -EINVAL;
  5433. } else {
  5434. return 0;
  5435. }
  5436. }
  5437. if (INTEL_INFO(dev)->num_pipes == 2)
  5438. return 0;
  5439. /* Ivybridge 3 pipe is really complicated */
  5440. switch (pipe) {
  5441. case PIPE_A:
  5442. return 0;
  5443. case PIPE_B:
  5444. if (pipe_config->fdi_lanes <= 2)
  5445. return 0;
  5446. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5447. other_crtc_state =
  5448. intel_atomic_get_crtc_state(state, other_crtc);
  5449. if (IS_ERR(other_crtc_state))
  5450. return PTR_ERR(other_crtc_state);
  5451. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5452. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5453. pipe_name(pipe), pipe_config->fdi_lanes);
  5454. return -EINVAL;
  5455. }
  5456. return 0;
  5457. case PIPE_C:
  5458. if (pipe_config->fdi_lanes > 2) {
  5459. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5460. pipe_name(pipe), pipe_config->fdi_lanes);
  5461. return -EINVAL;
  5462. }
  5463. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5464. other_crtc_state =
  5465. intel_atomic_get_crtc_state(state, other_crtc);
  5466. if (IS_ERR(other_crtc_state))
  5467. return PTR_ERR(other_crtc_state);
  5468. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5469. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5470. return -EINVAL;
  5471. }
  5472. return 0;
  5473. default:
  5474. BUG();
  5475. }
  5476. }
  5477. #define RETRY 1
  5478. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5479. struct intel_crtc_state *pipe_config)
  5480. {
  5481. struct drm_device *dev = intel_crtc->base.dev;
  5482. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5483. int lane, link_bw, fdi_dotclock, ret;
  5484. bool needs_recompute = false;
  5485. retry:
  5486. /* FDI is a binary signal running at ~2.7GHz, encoding
  5487. * each output octet as 10 bits. The actual frequency
  5488. * is stored as a divider into a 100MHz clock, and the
  5489. * mode pixel clock is stored in units of 1KHz.
  5490. * Hence the bw of each lane in terms of the mode signal
  5491. * is:
  5492. */
  5493. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5494. fdi_dotclock = adjusted_mode->crtc_clock;
  5495. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5496. pipe_config->pipe_bpp);
  5497. pipe_config->fdi_lanes = lane;
  5498. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5499. link_bw, &pipe_config->fdi_m_n);
  5500. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5501. intel_crtc->pipe, pipe_config);
  5502. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5503. pipe_config->pipe_bpp -= 2*3;
  5504. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5505. pipe_config->pipe_bpp);
  5506. needs_recompute = true;
  5507. pipe_config->bw_constrained = true;
  5508. goto retry;
  5509. }
  5510. if (needs_recompute)
  5511. return RETRY;
  5512. return ret;
  5513. }
  5514. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5515. struct intel_crtc_state *pipe_config)
  5516. {
  5517. if (pipe_config->pipe_bpp > 24)
  5518. return false;
  5519. /* HSW can handle pixel rate up to cdclk? */
  5520. if (IS_HASWELL(dev_priv->dev))
  5521. return true;
  5522. /*
  5523. * We compare against max which means we must take
  5524. * the increased cdclk requirement into account when
  5525. * calculating the new cdclk.
  5526. *
  5527. * Should measure whether using a lower cdclk w/o IPS
  5528. */
  5529. return ilk_pipe_pixel_rate(pipe_config) <=
  5530. dev_priv->max_cdclk_freq * 95 / 100;
  5531. }
  5532. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5533. struct intel_crtc_state *pipe_config)
  5534. {
  5535. struct drm_device *dev = crtc->base.dev;
  5536. struct drm_i915_private *dev_priv = dev->dev_private;
  5537. pipe_config->ips_enabled = i915.enable_ips &&
  5538. hsw_crtc_supports_ips(crtc) &&
  5539. pipe_config_supports_ips(dev_priv, pipe_config);
  5540. }
  5541. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5542. struct intel_crtc_state *pipe_config)
  5543. {
  5544. struct drm_device *dev = crtc->base.dev;
  5545. struct drm_i915_private *dev_priv = dev->dev_private;
  5546. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5547. int ret;
  5548. /* FIXME should check pixel clock limits on all platforms */
  5549. if (INTEL_INFO(dev)->gen < 4) {
  5550. int clock_limit = dev_priv->max_cdclk_freq;
  5551. /*
  5552. * Enable pixel doubling when the dot clock
  5553. * is > 90% of the (display) core speed.
  5554. *
  5555. * GDG double wide on either pipe,
  5556. * otherwise pipe A only.
  5557. */
  5558. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5559. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5560. clock_limit *= 2;
  5561. pipe_config->double_wide = true;
  5562. }
  5563. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5564. return -EINVAL;
  5565. }
  5566. /*
  5567. * Pipe horizontal size must be even in:
  5568. * - DVO ganged mode
  5569. * - LVDS dual channel mode
  5570. * - Double wide pipe
  5571. */
  5572. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5573. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5574. pipe_config->pipe_src_w &= ~1;
  5575. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5576. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5577. */
  5578. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5579. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5580. return -EINVAL;
  5581. if (HAS_IPS(dev))
  5582. hsw_compute_ips_config(crtc, pipe_config);
  5583. if (pipe_config->has_pch_encoder)
  5584. return ironlake_fdi_compute_config(crtc, pipe_config);
  5585. /* FIXME: remove below call once atomic mode set is place and all crtc
  5586. * related checks called from atomic_crtc_check function */
  5587. ret = 0;
  5588. DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
  5589. crtc, pipe_config->base.state);
  5590. ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
  5591. return ret;
  5592. }
  5593. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5594. {
  5595. struct drm_i915_private *dev_priv = to_i915(dev);
  5596. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5597. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5598. uint32_t linkrate;
  5599. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5600. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5601. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5602. return 540000;
  5603. linkrate = (I915_READ(DPLL_CTRL1) &
  5604. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5605. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5606. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5607. /* vco 8640 */
  5608. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5609. case CDCLK_FREQ_450_432:
  5610. return 432000;
  5611. case CDCLK_FREQ_337_308:
  5612. return 308570;
  5613. case CDCLK_FREQ_675_617:
  5614. return 617140;
  5615. default:
  5616. WARN(1, "Unknown cd freq selection\n");
  5617. }
  5618. } else {
  5619. /* vco 8100 */
  5620. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5621. case CDCLK_FREQ_450_432:
  5622. return 450000;
  5623. case CDCLK_FREQ_337_308:
  5624. return 337500;
  5625. case CDCLK_FREQ_675_617:
  5626. return 675000;
  5627. default:
  5628. WARN(1, "Unknown cd freq selection\n");
  5629. }
  5630. }
  5631. /* error case, do as if DPLL0 isn't enabled */
  5632. return 24000;
  5633. }
  5634. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5635. {
  5636. struct drm_i915_private *dev_priv = dev->dev_private;
  5637. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5638. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5639. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5640. return 800000;
  5641. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5642. return 450000;
  5643. else if (freq == LCPLL_CLK_FREQ_450)
  5644. return 450000;
  5645. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5646. return 540000;
  5647. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5648. return 337500;
  5649. else
  5650. return 675000;
  5651. }
  5652. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5653. {
  5654. struct drm_i915_private *dev_priv = dev->dev_private;
  5655. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5656. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5657. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5658. return 800000;
  5659. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5660. return 450000;
  5661. else if (freq == LCPLL_CLK_FREQ_450)
  5662. return 450000;
  5663. else if (IS_HSW_ULT(dev))
  5664. return 337500;
  5665. else
  5666. return 540000;
  5667. }
  5668. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5669. {
  5670. struct drm_i915_private *dev_priv = dev->dev_private;
  5671. u32 val;
  5672. int divider;
  5673. if (dev_priv->hpll_freq == 0)
  5674. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5675. mutex_lock(&dev_priv->sb_lock);
  5676. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5677. mutex_unlock(&dev_priv->sb_lock);
  5678. divider = val & DISPLAY_FREQUENCY_VALUES;
  5679. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5680. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5681. "cdclk change in progress\n");
  5682. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5683. }
  5684. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5685. {
  5686. return 450000;
  5687. }
  5688. static int i945_get_display_clock_speed(struct drm_device *dev)
  5689. {
  5690. return 400000;
  5691. }
  5692. static int i915_get_display_clock_speed(struct drm_device *dev)
  5693. {
  5694. return 333333;
  5695. }
  5696. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5697. {
  5698. return 200000;
  5699. }
  5700. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5701. {
  5702. u16 gcfgc = 0;
  5703. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5704. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5705. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5706. return 266667;
  5707. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5708. return 333333;
  5709. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5710. return 444444;
  5711. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5712. return 200000;
  5713. default:
  5714. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5715. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5716. return 133333;
  5717. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5718. return 166667;
  5719. }
  5720. }
  5721. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5722. {
  5723. u16 gcfgc = 0;
  5724. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5725. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5726. return 133333;
  5727. else {
  5728. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5729. case GC_DISPLAY_CLOCK_333_MHZ:
  5730. return 333333;
  5731. default:
  5732. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5733. return 190000;
  5734. }
  5735. }
  5736. }
  5737. static int i865_get_display_clock_speed(struct drm_device *dev)
  5738. {
  5739. return 266667;
  5740. }
  5741. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5742. {
  5743. u16 hpllcc = 0;
  5744. /*
  5745. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5746. * encoding is different :(
  5747. * FIXME is this the right way to detect 852GM/852GMV?
  5748. */
  5749. if (dev->pdev->revision == 0x1)
  5750. return 133333;
  5751. pci_bus_read_config_word(dev->pdev->bus,
  5752. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5753. /* Assume that the hardware is in the high speed state. This
  5754. * should be the default.
  5755. */
  5756. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5757. case GC_CLOCK_133_200:
  5758. case GC_CLOCK_133_200_2:
  5759. case GC_CLOCK_100_200:
  5760. return 200000;
  5761. case GC_CLOCK_166_250:
  5762. return 250000;
  5763. case GC_CLOCK_100_133:
  5764. return 133333;
  5765. case GC_CLOCK_133_266:
  5766. case GC_CLOCK_133_266_2:
  5767. case GC_CLOCK_166_266:
  5768. return 266667;
  5769. }
  5770. /* Shouldn't happen */
  5771. return 0;
  5772. }
  5773. static int i830_get_display_clock_speed(struct drm_device *dev)
  5774. {
  5775. return 133333;
  5776. }
  5777. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5778. {
  5779. struct drm_i915_private *dev_priv = dev->dev_private;
  5780. static const unsigned int blb_vco[8] = {
  5781. [0] = 3200000,
  5782. [1] = 4000000,
  5783. [2] = 5333333,
  5784. [3] = 4800000,
  5785. [4] = 6400000,
  5786. };
  5787. static const unsigned int pnv_vco[8] = {
  5788. [0] = 3200000,
  5789. [1] = 4000000,
  5790. [2] = 5333333,
  5791. [3] = 4800000,
  5792. [4] = 2666667,
  5793. };
  5794. static const unsigned int cl_vco[8] = {
  5795. [0] = 3200000,
  5796. [1] = 4000000,
  5797. [2] = 5333333,
  5798. [3] = 6400000,
  5799. [4] = 3333333,
  5800. [5] = 3566667,
  5801. [6] = 4266667,
  5802. };
  5803. static const unsigned int elk_vco[8] = {
  5804. [0] = 3200000,
  5805. [1] = 4000000,
  5806. [2] = 5333333,
  5807. [3] = 4800000,
  5808. };
  5809. static const unsigned int ctg_vco[8] = {
  5810. [0] = 3200000,
  5811. [1] = 4000000,
  5812. [2] = 5333333,
  5813. [3] = 6400000,
  5814. [4] = 2666667,
  5815. [5] = 4266667,
  5816. };
  5817. const unsigned int *vco_table;
  5818. unsigned int vco;
  5819. uint8_t tmp = 0;
  5820. /* FIXME other chipsets? */
  5821. if (IS_GM45(dev))
  5822. vco_table = ctg_vco;
  5823. else if (IS_G4X(dev))
  5824. vco_table = elk_vco;
  5825. else if (IS_CRESTLINE(dev))
  5826. vco_table = cl_vco;
  5827. else if (IS_PINEVIEW(dev))
  5828. vco_table = pnv_vco;
  5829. else if (IS_G33(dev))
  5830. vco_table = blb_vco;
  5831. else
  5832. return 0;
  5833. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5834. vco = vco_table[tmp & 0x7];
  5835. if (vco == 0)
  5836. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5837. else
  5838. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5839. return vco;
  5840. }
  5841. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5842. {
  5843. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5844. uint16_t tmp = 0;
  5845. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5846. cdclk_sel = (tmp >> 12) & 0x1;
  5847. switch (vco) {
  5848. case 2666667:
  5849. case 4000000:
  5850. case 5333333:
  5851. return cdclk_sel ? 333333 : 222222;
  5852. case 3200000:
  5853. return cdclk_sel ? 320000 : 228571;
  5854. default:
  5855. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5856. return 222222;
  5857. }
  5858. }
  5859. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5860. {
  5861. static const uint8_t div_3200[] = { 16, 10, 8 };
  5862. static const uint8_t div_4000[] = { 20, 12, 10 };
  5863. static const uint8_t div_5333[] = { 24, 16, 14 };
  5864. const uint8_t *div_table;
  5865. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5866. uint16_t tmp = 0;
  5867. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5868. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5869. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5870. goto fail;
  5871. switch (vco) {
  5872. case 3200000:
  5873. div_table = div_3200;
  5874. break;
  5875. case 4000000:
  5876. div_table = div_4000;
  5877. break;
  5878. case 5333333:
  5879. div_table = div_5333;
  5880. break;
  5881. default:
  5882. goto fail;
  5883. }
  5884. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5885. fail:
  5886. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5887. return 200000;
  5888. }
  5889. static int g33_get_display_clock_speed(struct drm_device *dev)
  5890. {
  5891. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5892. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5893. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5894. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5895. const uint8_t *div_table;
  5896. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5897. uint16_t tmp = 0;
  5898. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5899. cdclk_sel = (tmp >> 4) & 0x7;
  5900. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5901. goto fail;
  5902. switch (vco) {
  5903. case 3200000:
  5904. div_table = div_3200;
  5905. break;
  5906. case 4000000:
  5907. div_table = div_4000;
  5908. break;
  5909. case 4800000:
  5910. div_table = div_4800;
  5911. break;
  5912. case 5333333:
  5913. div_table = div_5333;
  5914. break;
  5915. default:
  5916. goto fail;
  5917. }
  5918. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5919. fail:
  5920. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5921. return 190476;
  5922. }
  5923. static void
  5924. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5925. {
  5926. while (*num > DATA_LINK_M_N_MASK ||
  5927. *den > DATA_LINK_M_N_MASK) {
  5928. *num >>= 1;
  5929. *den >>= 1;
  5930. }
  5931. }
  5932. static void compute_m_n(unsigned int m, unsigned int n,
  5933. uint32_t *ret_m, uint32_t *ret_n)
  5934. {
  5935. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5936. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5937. intel_reduce_m_n_ratio(ret_m, ret_n);
  5938. }
  5939. void
  5940. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5941. int pixel_clock, int link_clock,
  5942. struct intel_link_m_n *m_n)
  5943. {
  5944. m_n->tu = 64;
  5945. compute_m_n(bits_per_pixel * pixel_clock,
  5946. link_clock * nlanes * 8,
  5947. &m_n->gmch_m, &m_n->gmch_n);
  5948. compute_m_n(pixel_clock, link_clock,
  5949. &m_n->link_m, &m_n->link_n);
  5950. }
  5951. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5952. {
  5953. if (i915.panel_use_ssc >= 0)
  5954. return i915.panel_use_ssc != 0;
  5955. return dev_priv->vbt.lvds_use_ssc
  5956. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5957. }
  5958. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5959. int num_connectors)
  5960. {
  5961. struct drm_device *dev = crtc_state->base.crtc->dev;
  5962. struct drm_i915_private *dev_priv = dev->dev_private;
  5963. int refclk;
  5964. WARN_ON(!crtc_state->base.state);
  5965. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5966. refclk = 100000;
  5967. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5968. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5969. refclk = dev_priv->vbt.lvds_ssc_freq;
  5970. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5971. } else if (!IS_GEN2(dev)) {
  5972. refclk = 96000;
  5973. } else {
  5974. refclk = 48000;
  5975. }
  5976. return refclk;
  5977. }
  5978. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5979. {
  5980. return (1 << dpll->n) << 16 | dpll->m2;
  5981. }
  5982. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5983. {
  5984. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5985. }
  5986. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5987. struct intel_crtc_state *crtc_state,
  5988. intel_clock_t *reduced_clock)
  5989. {
  5990. struct drm_device *dev = crtc->base.dev;
  5991. u32 fp, fp2 = 0;
  5992. if (IS_PINEVIEW(dev)) {
  5993. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5994. if (reduced_clock)
  5995. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5996. } else {
  5997. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5998. if (reduced_clock)
  5999. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6000. }
  6001. crtc_state->dpll_hw_state.fp0 = fp;
  6002. crtc->lowfreq_avail = false;
  6003. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6004. reduced_clock) {
  6005. crtc_state->dpll_hw_state.fp1 = fp2;
  6006. crtc->lowfreq_avail = true;
  6007. } else {
  6008. crtc_state->dpll_hw_state.fp1 = fp;
  6009. }
  6010. }
  6011. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6012. pipe)
  6013. {
  6014. u32 reg_val;
  6015. /*
  6016. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6017. * and set it to a reasonable value instead.
  6018. */
  6019. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6020. reg_val &= 0xffffff00;
  6021. reg_val |= 0x00000030;
  6022. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6023. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6024. reg_val &= 0x8cffffff;
  6025. reg_val = 0x8c000000;
  6026. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6027. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6028. reg_val &= 0xffffff00;
  6029. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6030. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6031. reg_val &= 0x00ffffff;
  6032. reg_val |= 0xb0000000;
  6033. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6034. }
  6035. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6036. struct intel_link_m_n *m_n)
  6037. {
  6038. struct drm_device *dev = crtc->base.dev;
  6039. struct drm_i915_private *dev_priv = dev->dev_private;
  6040. int pipe = crtc->pipe;
  6041. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6042. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6043. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6044. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6045. }
  6046. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6047. struct intel_link_m_n *m_n,
  6048. struct intel_link_m_n *m2_n2)
  6049. {
  6050. struct drm_device *dev = crtc->base.dev;
  6051. struct drm_i915_private *dev_priv = dev->dev_private;
  6052. int pipe = crtc->pipe;
  6053. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6054. if (INTEL_INFO(dev)->gen >= 5) {
  6055. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6056. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6057. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6058. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6059. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6060. * for gen < 8) and if DRRS is supported (to make sure the
  6061. * registers are not unnecessarily accessed).
  6062. */
  6063. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6064. crtc->config->has_drrs) {
  6065. I915_WRITE(PIPE_DATA_M2(transcoder),
  6066. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6067. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6068. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6069. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6070. }
  6071. } else {
  6072. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6073. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6074. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6075. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6076. }
  6077. }
  6078. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6079. {
  6080. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6081. if (m_n == M1_N1) {
  6082. dp_m_n = &crtc->config->dp_m_n;
  6083. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6084. } else if (m_n == M2_N2) {
  6085. /*
  6086. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6087. * needs to be programmed into M1_N1.
  6088. */
  6089. dp_m_n = &crtc->config->dp_m2_n2;
  6090. } else {
  6091. DRM_ERROR("Unsupported divider value\n");
  6092. return;
  6093. }
  6094. if (crtc->config->has_pch_encoder)
  6095. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6096. else
  6097. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6098. }
  6099. static void vlv_update_pll(struct intel_crtc *crtc,
  6100. struct intel_crtc_state *pipe_config)
  6101. {
  6102. u32 dpll, dpll_md;
  6103. /*
  6104. * Enable DPIO clock input. We should never disable the reference
  6105. * clock for pipe B, since VGA hotplug / manual detection depends
  6106. * on it.
  6107. */
  6108. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6109. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6110. /* We should never disable this, set it here for state tracking */
  6111. if (crtc->pipe == PIPE_B)
  6112. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6113. dpll |= DPLL_VCO_ENABLE;
  6114. pipe_config->dpll_hw_state.dpll = dpll;
  6115. dpll_md = (pipe_config->pixel_multiplier - 1)
  6116. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6117. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6118. }
  6119. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6120. const struct intel_crtc_state *pipe_config)
  6121. {
  6122. struct drm_device *dev = crtc->base.dev;
  6123. struct drm_i915_private *dev_priv = dev->dev_private;
  6124. int pipe = crtc->pipe;
  6125. u32 mdiv;
  6126. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6127. u32 coreclk, reg_val;
  6128. mutex_lock(&dev_priv->sb_lock);
  6129. bestn = pipe_config->dpll.n;
  6130. bestm1 = pipe_config->dpll.m1;
  6131. bestm2 = pipe_config->dpll.m2;
  6132. bestp1 = pipe_config->dpll.p1;
  6133. bestp2 = pipe_config->dpll.p2;
  6134. /* See eDP HDMI DPIO driver vbios notes doc */
  6135. /* PLL B needs special handling */
  6136. if (pipe == PIPE_B)
  6137. vlv_pllb_recal_opamp(dev_priv, pipe);
  6138. /* Set up Tx target for periodic Rcomp update */
  6139. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6140. /* Disable target IRef on PLL */
  6141. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6142. reg_val &= 0x00ffffff;
  6143. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6144. /* Disable fast lock */
  6145. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6146. /* Set idtafcrecal before PLL is enabled */
  6147. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6148. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6149. mdiv |= ((bestn << DPIO_N_SHIFT));
  6150. mdiv |= (1 << DPIO_K_SHIFT);
  6151. /*
  6152. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6153. * but we don't support that).
  6154. * Note: don't use the DAC post divider as it seems unstable.
  6155. */
  6156. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6157. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6158. mdiv |= DPIO_ENABLE_CALIBRATION;
  6159. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6160. /* Set HBR and RBR LPF coefficients */
  6161. if (pipe_config->port_clock == 162000 ||
  6162. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6163. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6164. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6165. 0x009f0003);
  6166. else
  6167. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6168. 0x00d0000f);
  6169. if (pipe_config->has_dp_encoder) {
  6170. /* Use SSC source */
  6171. if (pipe == PIPE_A)
  6172. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6173. 0x0df40000);
  6174. else
  6175. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6176. 0x0df70000);
  6177. } else { /* HDMI or VGA */
  6178. /* Use bend source */
  6179. if (pipe == PIPE_A)
  6180. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6181. 0x0df70000);
  6182. else
  6183. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6184. 0x0df40000);
  6185. }
  6186. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6187. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6188. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6189. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6190. coreclk |= 0x01000000;
  6191. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6192. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6193. mutex_unlock(&dev_priv->sb_lock);
  6194. }
  6195. static void chv_update_pll(struct intel_crtc *crtc,
  6196. struct intel_crtc_state *pipe_config)
  6197. {
  6198. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6199. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6200. DPLL_VCO_ENABLE;
  6201. if (crtc->pipe != PIPE_A)
  6202. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6203. pipe_config->dpll_hw_state.dpll_md =
  6204. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6205. }
  6206. static void chv_prepare_pll(struct intel_crtc *crtc,
  6207. const struct intel_crtc_state *pipe_config)
  6208. {
  6209. struct drm_device *dev = crtc->base.dev;
  6210. struct drm_i915_private *dev_priv = dev->dev_private;
  6211. int pipe = crtc->pipe;
  6212. int dpll_reg = DPLL(crtc->pipe);
  6213. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6214. u32 loopfilter, tribuf_calcntr;
  6215. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6216. u32 dpio_val;
  6217. int vco;
  6218. bestn = pipe_config->dpll.n;
  6219. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6220. bestm1 = pipe_config->dpll.m1;
  6221. bestm2 = pipe_config->dpll.m2 >> 22;
  6222. bestp1 = pipe_config->dpll.p1;
  6223. bestp2 = pipe_config->dpll.p2;
  6224. vco = pipe_config->dpll.vco;
  6225. dpio_val = 0;
  6226. loopfilter = 0;
  6227. /*
  6228. * Enable Refclk and SSC
  6229. */
  6230. I915_WRITE(dpll_reg,
  6231. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6232. mutex_lock(&dev_priv->sb_lock);
  6233. /* p1 and p2 divider */
  6234. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6235. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6236. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6237. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6238. 1 << DPIO_CHV_K_DIV_SHIFT);
  6239. /* Feedback post-divider - m2 */
  6240. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6241. /* Feedback refclk divider - n and m1 */
  6242. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6243. DPIO_CHV_M1_DIV_BY_2 |
  6244. 1 << DPIO_CHV_N_DIV_SHIFT);
  6245. /* M2 fraction division */
  6246. if (bestm2_frac)
  6247. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6248. /* M2 fraction division enable */
  6249. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6250. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6251. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6252. if (bestm2_frac)
  6253. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6254. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6255. /* Program digital lock detect threshold */
  6256. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6257. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6258. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6259. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6260. if (!bestm2_frac)
  6261. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6262. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6263. /* Loop filter */
  6264. if (vco == 5400000) {
  6265. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6266. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6267. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6268. tribuf_calcntr = 0x9;
  6269. } else if (vco <= 6200000) {
  6270. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6271. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6272. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6273. tribuf_calcntr = 0x9;
  6274. } else if (vco <= 6480000) {
  6275. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6276. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6277. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6278. tribuf_calcntr = 0x8;
  6279. } else {
  6280. /* Not supported. Apply the same limits as in the max case */
  6281. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6282. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6283. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6284. tribuf_calcntr = 0;
  6285. }
  6286. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6287. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6288. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6289. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6290. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6291. /* AFC Recal */
  6292. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6293. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6294. DPIO_AFC_RECAL);
  6295. mutex_unlock(&dev_priv->sb_lock);
  6296. }
  6297. /**
  6298. * vlv_force_pll_on - forcibly enable just the PLL
  6299. * @dev_priv: i915 private structure
  6300. * @pipe: pipe PLL to enable
  6301. * @dpll: PLL configuration
  6302. *
  6303. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6304. * in cases where we need the PLL enabled even when @pipe is not going to
  6305. * be enabled.
  6306. */
  6307. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6308. const struct dpll *dpll)
  6309. {
  6310. struct intel_crtc *crtc =
  6311. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6312. struct intel_crtc_state pipe_config = {
  6313. .base.crtc = &crtc->base,
  6314. .pixel_multiplier = 1,
  6315. .dpll = *dpll,
  6316. };
  6317. if (IS_CHERRYVIEW(dev)) {
  6318. chv_update_pll(crtc, &pipe_config);
  6319. chv_prepare_pll(crtc, &pipe_config);
  6320. chv_enable_pll(crtc, &pipe_config);
  6321. } else {
  6322. vlv_update_pll(crtc, &pipe_config);
  6323. vlv_prepare_pll(crtc, &pipe_config);
  6324. vlv_enable_pll(crtc, &pipe_config);
  6325. }
  6326. }
  6327. /**
  6328. * vlv_force_pll_off - forcibly disable just the PLL
  6329. * @dev_priv: i915 private structure
  6330. * @pipe: pipe PLL to disable
  6331. *
  6332. * Disable the PLL for @pipe. To be used in cases where we need
  6333. * the PLL enabled even when @pipe is not going to be enabled.
  6334. */
  6335. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6336. {
  6337. if (IS_CHERRYVIEW(dev))
  6338. chv_disable_pll(to_i915(dev), pipe);
  6339. else
  6340. vlv_disable_pll(to_i915(dev), pipe);
  6341. }
  6342. static void i9xx_update_pll(struct intel_crtc *crtc,
  6343. struct intel_crtc_state *crtc_state,
  6344. intel_clock_t *reduced_clock,
  6345. int num_connectors)
  6346. {
  6347. struct drm_device *dev = crtc->base.dev;
  6348. struct drm_i915_private *dev_priv = dev->dev_private;
  6349. u32 dpll;
  6350. bool is_sdvo;
  6351. struct dpll *clock = &crtc_state->dpll;
  6352. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6353. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6354. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6355. dpll = DPLL_VGA_MODE_DIS;
  6356. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6357. dpll |= DPLLB_MODE_LVDS;
  6358. else
  6359. dpll |= DPLLB_MODE_DAC_SERIAL;
  6360. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6361. dpll |= (crtc_state->pixel_multiplier - 1)
  6362. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6363. }
  6364. if (is_sdvo)
  6365. dpll |= DPLL_SDVO_HIGH_SPEED;
  6366. if (crtc_state->has_dp_encoder)
  6367. dpll |= DPLL_SDVO_HIGH_SPEED;
  6368. /* compute bitmask from p1 value */
  6369. if (IS_PINEVIEW(dev))
  6370. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6371. else {
  6372. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6373. if (IS_G4X(dev) && reduced_clock)
  6374. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6375. }
  6376. switch (clock->p2) {
  6377. case 5:
  6378. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6379. break;
  6380. case 7:
  6381. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6382. break;
  6383. case 10:
  6384. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6385. break;
  6386. case 14:
  6387. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6388. break;
  6389. }
  6390. if (INTEL_INFO(dev)->gen >= 4)
  6391. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6392. if (crtc_state->sdvo_tv_clock)
  6393. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6394. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6395. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6396. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6397. else
  6398. dpll |= PLL_REF_INPUT_DREFCLK;
  6399. dpll |= DPLL_VCO_ENABLE;
  6400. crtc_state->dpll_hw_state.dpll = dpll;
  6401. if (INTEL_INFO(dev)->gen >= 4) {
  6402. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6403. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6404. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6405. }
  6406. }
  6407. static void i8xx_update_pll(struct intel_crtc *crtc,
  6408. struct intel_crtc_state *crtc_state,
  6409. intel_clock_t *reduced_clock,
  6410. int num_connectors)
  6411. {
  6412. struct drm_device *dev = crtc->base.dev;
  6413. struct drm_i915_private *dev_priv = dev->dev_private;
  6414. u32 dpll;
  6415. struct dpll *clock = &crtc_state->dpll;
  6416. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6417. dpll = DPLL_VGA_MODE_DIS;
  6418. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6419. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6420. } else {
  6421. if (clock->p1 == 2)
  6422. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6423. else
  6424. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6425. if (clock->p2 == 4)
  6426. dpll |= PLL_P2_DIVIDE_BY_4;
  6427. }
  6428. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6429. dpll |= DPLL_DVO_2X_MODE;
  6430. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6431. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6432. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6433. else
  6434. dpll |= PLL_REF_INPUT_DREFCLK;
  6435. dpll |= DPLL_VCO_ENABLE;
  6436. crtc_state->dpll_hw_state.dpll = dpll;
  6437. }
  6438. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6439. {
  6440. struct drm_device *dev = intel_crtc->base.dev;
  6441. struct drm_i915_private *dev_priv = dev->dev_private;
  6442. enum pipe pipe = intel_crtc->pipe;
  6443. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6444. struct drm_display_mode *adjusted_mode =
  6445. &intel_crtc->config->base.adjusted_mode;
  6446. uint32_t crtc_vtotal, crtc_vblank_end;
  6447. int vsyncshift = 0;
  6448. /* We need to be careful not to changed the adjusted mode, for otherwise
  6449. * the hw state checker will get angry at the mismatch. */
  6450. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6451. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6452. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6453. /* the chip adds 2 halflines automatically */
  6454. crtc_vtotal -= 1;
  6455. crtc_vblank_end -= 1;
  6456. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6457. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6458. else
  6459. vsyncshift = adjusted_mode->crtc_hsync_start -
  6460. adjusted_mode->crtc_htotal / 2;
  6461. if (vsyncshift < 0)
  6462. vsyncshift += adjusted_mode->crtc_htotal;
  6463. }
  6464. if (INTEL_INFO(dev)->gen > 3)
  6465. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6466. I915_WRITE(HTOTAL(cpu_transcoder),
  6467. (adjusted_mode->crtc_hdisplay - 1) |
  6468. ((adjusted_mode->crtc_htotal - 1) << 16));
  6469. I915_WRITE(HBLANK(cpu_transcoder),
  6470. (adjusted_mode->crtc_hblank_start - 1) |
  6471. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6472. I915_WRITE(HSYNC(cpu_transcoder),
  6473. (adjusted_mode->crtc_hsync_start - 1) |
  6474. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6475. I915_WRITE(VTOTAL(cpu_transcoder),
  6476. (adjusted_mode->crtc_vdisplay - 1) |
  6477. ((crtc_vtotal - 1) << 16));
  6478. I915_WRITE(VBLANK(cpu_transcoder),
  6479. (adjusted_mode->crtc_vblank_start - 1) |
  6480. ((crtc_vblank_end - 1) << 16));
  6481. I915_WRITE(VSYNC(cpu_transcoder),
  6482. (adjusted_mode->crtc_vsync_start - 1) |
  6483. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6484. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6485. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6486. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6487. * bits. */
  6488. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6489. (pipe == PIPE_B || pipe == PIPE_C))
  6490. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6491. /* pipesrc controls the size that is scaled from, which should
  6492. * always be the user's requested size.
  6493. */
  6494. I915_WRITE(PIPESRC(pipe),
  6495. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6496. (intel_crtc->config->pipe_src_h - 1));
  6497. }
  6498. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6499. struct intel_crtc_state *pipe_config)
  6500. {
  6501. struct drm_device *dev = crtc->base.dev;
  6502. struct drm_i915_private *dev_priv = dev->dev_private;
  6503. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6504. uint32_t tmp;
  6505. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6506. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6507. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6508. tmp = I915_READ(HBLANK(cpu_transcoder));
  6509. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6510. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6511. tmp = I915_READ(HSYNC(cpu_transcoder));
  6512. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6513. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6514. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6515. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6516. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6517. tmp = I915_READ(VBLANK(cpu_transcoder));
  6518. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6519. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6520. tmp = I915_READ(VSYNC(cpu_transcoder));
  6521. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6522. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6523. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6524. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6525. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6526. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6527. }
  6528. tmp = I915_READ(PIPESRC(crtc->pipe));
  6529. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6530. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6531. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6532. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6533. }
  6534. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6535. struct intel_crtc_state *pipe_config)
  6536. {
  6537. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6538. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6539. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6540. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6541. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6542. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6543. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6544. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6545. mode->flags = pipe_config->base.adjusted_mode.flags;
  6546. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6547. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6548. }
  6549. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6550. {
  6551. struct drm_device *dev = intel_crtc->base.dev;
  6552. struct drm_i915_private *dev_priv = dev->dev_private;
  6553. uint32_t pipeconf;
  6554. pipeconf = 0;
  6555. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6556. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6557. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6558. if (intel_crtc->config->double_wide)
  6559. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6560. /* only g4x and later have fancy bpc/dither controls */
  6561. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6562. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6563. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6564. pipeconf |= PIPECONF_DITHER_EN |
  6565. PIPECONF_DITHER_TYPE_SP;
  6566. switch (intel_crtc->config->pipe_bpp) {
  6567. case 18:
  6568. pipeconf |= PIPECONF_6BPC;
  6569. break;
  6570. case 24:
  6571. pipeconf |= PIPECONF_8BPC;
  6572. break;
  6573. case 30:
  6574. pipeconf |= PIPECONF_10BPC;
  6575. break;
  6576. default:
  6577. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6578. BUG();
  6579. }
  6580. }
  6581. if (HAS_PIPE_CXSR(dev)) {
  6582. if (intel_crtc->lowfreq_avail) {
  6583. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6584. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6585. } else {
  6586. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6587. }
  6588. }
  6589. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6590. if (INTEL_INFO(dev)->gen < 4 ||
  6591. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6592. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6593. else
  6594. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6595. } else
  6596. pipeconf |= PIPECONF_PROGRESSIVE;
  6597. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6598. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6599. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6600. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6601. }
  6602. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6603. struct intel_crtc_state *crtc_state)
  6604. {
  6605. struct drm_device *dev = crtc->base.dev;
  6606. struct drm_i915_private *dev_priv = dev->dev_private;
  6607. int refclk, num_connectors = 0;
  6608. intel_clock_t clock, reduced_clock;
  6609. bool ok, has_reduced_clock = false;
  6610. bool is_lvds = false, is_dsi = false;
  6611. struct intel_encoder *encoder;
  6612. const intel_limit_t *limit;
  6613. struct drm_atomic_state *state = crtc_state->base.state;
  6614. struct drm_connector *connector;
  6615. struct drm_connector_state *connector_state;
  6616. int i;
  6617. memset(&crtc_state->dpll_hw_state, 0,
  6618. sizeof(crtc_state->dpll_hw_state));
  6619. for_each_connector_in_state(state, connector, connector_state, i) {
  6620. if (connector_state->crtc != &crtc->base)
  6621. continue;
  6622. encoder = to_intel_encoder(connector_state->best_encoder);
  6623. switch (encoder->type) {
  6624. case INTEL_OUTPUT_LVDS:
  6625. is_lvds = true;
  6626. break;
  6627. case INTEL_OUTPUT_DSI:
  6628. is_dsi = true;
  6629. break;
  6630. default:
  6631. break;
  6632. }
  6633. num_connectors++;
  6634. }
  6635. if (is_dsi)
  6636. return 0;
  6637. if (!crtc_state->clock_set) {
  6638. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6639. /*
  6640. * Returns a set of divisors for the desired target clock with
  6641. * the given refclk, or FALSE. The returned values represent
  6642. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6643. * 2) / p1 / p2.
  6644. */
  6645. limit = intel_limit(crtc_state, refclk);
  6646. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6647. crtc_state->port_clock,
  6648. refclk, NULL, &clock);
  6649. if (!ok) {
  6650. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6651. return -EINVAL;
  6652. }
  6653. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6654. /*
  6655. * Ensure we match the reduced clock's P to the target
  6656. * clock. If the clocks don't match, we can't switch
  6657. * the display clock by using the FP0/FP1. In such case
  6658. * we will disable the LVDS downclock feature.
  6659. */
  6660. has_reduced_clock =
  6661. dev_priv->display.find_dpll(limit, crtc_state,
  6662. dev_priv->lvds_downclock,
  6663. refclk, &clock,
  6664. &reduced_clock);
  6665. }
  6666. /* Compat-code for transition, will disappear. */
  6667. crtc_state->dpll.n = clock.n;
  6668. crtc_state->dpll.m1 = clock.m1;
  6669. crtc_state->dpll.m2 = clock.m2;
  6670. crtc_state->dpll.p1 = clock.p1;
  6671. crtc_state->dpll.p2 = clock.p2;
  6672. }
  6673. if (IS_GEN2(dev)) {
  6674. i8xx_update_pll(crtc, crtc_state,
  6675. has_reduced_clock ? &reduced_clock : NULL,
  6676. num_connectors);
  6677. } else if (IS_CHERRYVIEW(dev)) {
  6678. chv_update_pll(crtc, crtc_state);
  6679. } else if (IS_VALLEYVIEW(dev)) {
  6680. vlv_update_pll(crtc, crtc_state);
  6681. } else {
  6682. i9xx_update_pll(crtc, crtc_state,
  6683. has_reduced_clock ? &reduced_clock : NULL,
  6684. num_connectors);
  6685. }
  6686. return 0;
  6687. }
  6688. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6689. struct intel_crtc_state *pipe_config)
  6690. {
  6691. struct drm_device *dev = crtc->base.dev;
  6692. struct drm_i915_private *dev_priv = dev->dev_private;
  6693. uint32_t tmp;
  6694. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6695. return;
  6696. tmp = I915_READ(PFIT_CONTROL);
  6697. if (!(tmp & PFIT_ENABLE))
  6698. return;
  6699. /* Check whether the pfit is attached to our pipe. */
  6700. if (INTEL_INFO(dev)->gen < 4) {
  6701. if (crtc->pipe != PIPE_B)
  6702. return;
  6703. } else {
  6704. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6705. return;
  6706. }
  6707. pipe_config->gmch_pfit.control = tmp;
  6708. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6709. if (INTEL_INFO(dev)->gen < 5)
  6710. pipe_config->gmch_pfit.lvds_border_bits =
  6711. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6712. }
  6713. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6714. struct intel_crtc_state *pipe_config)
  6715. {
  6716. struct drm_device *dev = crtc->base.dev;
  6717. struct drm_i915_private *dev_priv = dev->dev_private;
  6718. int pipe = pipe_config->cpu_transcoder;
  6719. intel_clock_t clock;
  6720. u32 mdiv;
  6721. int refclk = 100000;
  6722. /* In case of MIPI DPLL will not even be used */
  6723. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6724. return;
  6725. mutex_lock(&dev_priv->sb_lock);
  6726. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6727. mutex_unlock(&dev_priv->sb_lock);
  6728. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6729. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6730. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6731. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6732. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6733. vlv_clock(refclk, &clock);
  6734. /* clock.dot is the fast clock */
  6735. pipe_config->port_clock = clock.dot / 5;
  6736. }
  6737. static void
  6738. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6739. struct intel_initial_plane_config *plane_config)
  6740. {
  6741. struct drm_device *dev = crtc->base.dev;
  6742. struct drm_i915_private *dev_priv = dev->dev_private;
  6743. u32 val, base, offset;
  6744. int pipe = crtc->pipe, plane = crtc->plane;
  6745. int fourcc, pixel_format;
  6746. unsigned int aligned_height;
  6747. struct drm_framebuffer *fb;
  6748. struct intel_framebuffer *intel_fb;
  6749. val = I915_READ(DSPCNTR(plane));
  6750. if (!(val & DISPLAY_PLANE_ENABLE))
  6751. return;
  6752. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6753. if (!intel_fb) {
  6754. DRM_DEBUG_KMS("failed to alloc fb\n");
  6755. return;
  6756. }
  6757. fb = &intel_fb->base;
  6758. if (INTEL_INFO(dev)->gen >= 4) {
  6759. if (val & DISPPLANE_TILED) {
  6760. plane_config->tiling = I915_TILING_X;
  6761. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6762. }
  6763. }
  6764. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6765. fourcc = i9xx_format_to_fourcc(pixel_format);
  6766. fb->pixel_format = fourcc;
  6767. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6768. if (INTEL_INFO(dev)->gen >= 4) {
  6769. if (plane_config->tiling)
  6770. offset = I915_READ(DSPTILEOFF(plane));
  6771. else
  6772. offset = I915_READ(DSPLINOFF(plane));
  6773. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6774. } else {
  6775. base = I915_READ(DSPADDR(plane));
  6776. }
  6777. plane_config->base = base;
  6778. val = I915_READ(PIPESRC(pipe));
  6779. fb->width = ((val >> 16) & 0xfff) + 1;
  6780. fb->height = ((val >> 0) & 0xfff) + 1;
  6781. val = I915_READ(DSPSTRIDE(pipe));
  6782. fb->pitches[0] = val & 0xffffffc0;
  6783. aligned_height = intel_fb_align_height(dev, fb->height,
  6784. fb->pixel_format,
  6785. fb->modifier[0]);
  6786. plane_config->size = fb->pitches[0] * aligned_height;
  6787. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6788. pipe_name(pipe), plane, fb->width, fb->height,
  6789. fb->bits_per_pixel, base, fb->pitches[0],
  6790. plane_config->size);
  6791. plane_config->fb = intel_fb;
  6792. }
  6793. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6794. struct intel_crtc_state *pipe_config)
  6795. {
  6796. struct drm_device *dev = crtc->base.dev;
  6797. struct drm_i915_private *dev_priv = dev->dev_private;
  6798. int pipe = pipe_config->cpu_transcoder;
  6799. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6800. intel_clock_t clock;
  6801. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6802. int refclk = 100000;
  6803. mutex_lock(&dev_priv->sb_lock);
  6804. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6805. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6806. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6807. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6808. mutex_unlock(&dev_priv->sb_lock);
  6809. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6810. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6811. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6812. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6813. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6814. chv_clock(refclk, &clock);
  6815. /* clock.dot is the fast clock */
  6816. pipe_config->port_clock = clock.dot / 5;
  6817. }
  6818. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6819. struct intel_crtc_state *pipe_config)
  6820. {
  6821. struct drm_device *dev = crtc->base.dev;
  6822. struct drm_i915_private *dev_priv = dev->dev_private;
  6823. uint32_t tmp;
  6824. if (!intel_display_power_is_enabled(dev_priv,
  6825. POWER_DOMAIN_PIPE(crtc->pipe)))
  6826. return false;
  6827. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6828. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6829. tmp = I915_READ(PIPECONF(crtc->pipe));
  6830. if (!(tmp & PIPECONF_ENABLE))
  6831. return false;
  6832. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6833. switch (tmp & PIPECONF_BPC_MASK) {
  6834. case PIPECONF_6BPC:
  6835. pipe_config->pipe_bpp = 18;
  6836. break;
  6837. case PIPECONF_8BPC:
  6838. pipe_config->pipe_bpp = 24;
  6839. break;
  6840. case PIPECONF_10BPC:
  6841. pipe_config->pipe_bpp = 30;
  6842. break;
  6843. default:
  6844. break;
  6845. }
  6846. }
  6847. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6848. pipe_config->limited_color_range = true;
  6849. if (INTEL_INFO(dev)->gen < 4)
  6850. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6851. intel_get_pipe_timings(crtc, pipe_config);
  6852. i9xx_get_pfit_config(crtc, pipe_config);
  6853. if (INTEL_INFO(dev)->gen >= 4) {
  6854. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6855. pipe_config->pixel_multiplier =
  6856. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6857. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6858. pipe_config->dpll_hw_state.dpll_md = tmp;
  6859. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6860. tmp = I915_READ(DPLL(crtc->pipe));
  6861. pipe_config->pixel_multiplier =
  6862. ((tmp & SDVO_MULTIPLIER_MASK)
  6863. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6864. } else {
  6865. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6866. * port and will be fixed up in the encoder->get_config
  6867. * function. */
  6868. pipe_config->pixel_multiplier = 1;
  6869. }
  6870. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6871. if (!IS_VALLEYVIEW(dev)) {
  6872. /*
  6873. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6874. * on 830. Filter it out here so that we don't
  6875. * report errors due to that.
  6876. */
  6877. if (IS_I830(dev))
  6878. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6879. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6880. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6881. } else {
  6882. /* Mask out read-only status bits. */
  6883. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6884. DPLL_PORTC_READY_MASK |
  6885. DPLL_PORTB_READY_MASK);
  6886. }
  6887. if (IS_CHERRYVIEW(dev))
  6888. chv_crtc_clock_get(crtc, pipe_config);
  6889. else if (IS_VALLEYVIEW(dev))
  6890. vlv_crtc_clock_get(crtc, pipe_config);
  6891. else
  6892. i9xx_crtc_clock_get(crtc, pipe_config);
  6893. return true;
  6894. }
  6895. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6896. {
  6897. struct drm_i915_private *dev_priv = dev->dev_private;
  6898. struct intel_encoder *encoder;
  6899. u32 val, final;
  6900. bool has_lvds = false;
  6901. bool has_cpu_edp = false;
  6902. bool has_panel = false;
  6903. bool has_ck505 = false;
  6904. bool can_ssc = false;
  6905. /* We need to take the global config into account */
  6906. for_each_intel_encoder(dev, encoder) {
  6907. switch (encoder->type) {
  6908. case INTEL_OUTPUT_LVDS:
  6909. has_panel = true;
  6910. has_lvds = true;
  6911. break;
  6912. case INTEL_OUTPUT_EDP:
  6913. has_panel = true;
  6914. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6915. has_cpu_edp = true;
  6916. break;
  6917. default:
  6918. break;
  6919. }
  6920. }
  6921. if (HAS_PCH_IBX(dev)) {
  6922. has_ck505 = dev_priv->vbt.display_clock_mode;
  6923. can_ssc = has_ck505;
  6924. } else {
  6925. has_ck505 = false;
  6926. can_ssc = true;
  6927. }
  6928. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6929. has_panel, has_lvds, has_ck505);
  6930. /* Ironlake: try to setup display ref clock before DPLL
  6931. * enabling. This is only under driver's control after
  6932. * PCH B stepping, previous chipset stepping should be
  6933. * ignoring this setting.
  6934. */
  6935. val = I915_READ(PCH_DREF_CONTROL);
  6936. /* As we must carefully and slowly disable/enable each source in turn,
  6937. * compute the final state we want first and check if we need to
  6938. * make any changes at all.
  6939. */
  6940. final = val;
  6941. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6942. if (has_ck505)
  6943. final |= DREF_NONSPREAD_CK505_ENABLE;
  6944. else
  6945. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6946. final &= ~DREF_SSC_SOURCE_MASK;
  6947. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6948. final &= ~DREF_SSC1_ENABLE;
  6949. if (has_panel) {
  6950. final |= DREF_SSC_SOURCE_ENABLE;
  6951. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6952. final |= DREF_SSC1_ENABLE;
  6953. if (has_cpu_edp) {
  6954. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6955. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6956. else
  6957. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6958. } else
  6959. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6960. } else {
  6961. final |= DREF_SSC_SOURCE_DISABLE;
  6962. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6963. }
  6964. if (final == val)
  6965. return;
  6966. /* Always enable nonspread source */
  6967. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6968. if (has_ck505)
  6969. val |= DREF_NONSPREAD_CK505_ENABLE;
  6970. else
  6971. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6972. if (has_panel) {
  6973. val &= ~DREF_SSC_SOURCE_MASK;
  6974. val |= DREF_SSC_SOURCE_ENABLE;
  6975. /* SSC must be turned on before enabling the CPU output */
  6976. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6977. DRM_DEBUG_KMS("Using SSC on panel\n");
  6978. val |= DREF_SSC1_ENABLE;
  6979. } else
  6980. val &= ~DREF_SSC1_ENABLE;
  6981. /* Get SSC going before enabling the outputs */
  6982. I915_WRITE(PCH_DREF_CONTROL, val);
  6983. POSTING_READ(PCH_DREF_CONTROL);
  6984. udelay(200);
  6985. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6986. /* Enable CPU source on CPU attached eDP */
  6987. if (has_cpu_edp) {
  6988. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6989. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6990. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6991. } else
  6992. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6993. } else
  6994. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6995. I915_WRITE(PCH_DREF_CONTROL, val);
  6996. POSTING_READ(PCH_DREF_CONTROL);
  6997. udelay(200);
  6998. } else {
  6999. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7000. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7001. /* Turn off CPU output */
  7002. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7003. I915_WRITE(PCH_DREF_CONTROL, val);
  7004. POSTING_READ(PCH_DREF_CONTROL);
  7005. udelay(200);
  7006. /* Turn off the SSC source */
  7007. val &= ~DREF_SSC_SOURCE_MASK;
  7008. val |= DREF_SSC_SOURCE_DISABLE;
  7009. /* Turn off SSC1 */
  7010. val &= ~DREF_SSC1_ENABLE;
  7011. I915_WRITE(PCH_DREF_CONTROL, val);
  7012. POSTING_READ(PCH_DREF_CONTROL);
  7013. udelay(200);
  7014. }
  7015. BUG_ON(val != final);
  7016. }
  7017. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7018. {
  7019. uint32_t tmp;
  7020. tmp = I915_READ(SOUTH_CHICKEN2);
  7021. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7022. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7023. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7024. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7025. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7026. tmp = I915_READ(SOUTH_CHICKEN2);
  7027. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7028. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7029. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7030. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7031. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7032. }
  7033. /* WaMPhyProgramming:hsw */
  7034. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7035. {
  7036. uint32_t tmp;
  7037. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7038. tmp &= ~(0xFF << 24);
  7039. tmp |= (0x12 << 24);
  7040. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7041. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7042. tmp |= (1 << 11);
  7043. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7044. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7045. tmp |= (1 << 11);
  7046. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7047. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7048. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7049. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7050. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7051. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7052. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7053. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7054. tmp &= ~(7 << 13);
  7055. tmp |= (5 << 13);
  7056. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7057. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7058. tmp &= ~(7 << 13);
  7059. tmp |= (5 << 13);
  7060. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7061. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7062. tmp &= ~0xFF;
  7063. tmp |= 0x1C;
  7064. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7065. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7066. tmp &= ~0xFF;
  7067. tmp |= 0x1C;
  7068. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7069. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7070. tmp &= ~(0xFF << 16);
  7071. tmp |= (0x1C << 16);
  7072. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7073. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7074. tmp &= ~(0xFF << 16);
  7075. tmp |= (0x1C << 16);
  7076. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7077. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7078. tmp |= (1 << 27);
  7079. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7080. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7081. tmp |= (1 << 27);
  7082. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7083. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7084. tmp &= ~(0xF << 28);
  7085. tmp |= (4 << 28);
  7086. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7087. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7088. tmp &= ~(0xF << 28);
  7089. tmp |= (4 << 28);
  7090. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7091. }
  7092. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7093. * Programming" based on the parameters passed:
  7094. * - Sequence to enable CLKOUT_DP
  7095. * - Sequence to enable CLKOUT_DP without spread
  7096. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7097. */
  7098. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7099. bool with_fdi)
  7100. {
  7101. struct drm_i915_private *dev_priv = dev->dev_private;
  7102. uint32_t reg, tmp;
  7103. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7104. with_spread = true;
  7105. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7106. with_fdi, "LP PCH doesn't have FDI\n"))
  7107. with_fdi = false;
  7108. mutex_lock(&dev_priv->sb_lock);
  7109. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7110. tmp &= ~SBI_SSCCTL_DISABLE;
  7111. tmp |= SBI_SSCCTL_PATHALT;
  7112. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7113. udelay(24);
  7114. if (with_spread) {
  7115. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7116. tmp &= ~SBI_SSCCTL_PATHALT;
  7117. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7118. if (with_fdi) {
  7119. lpt_reset_fdi_mphy(dev_priv);
  7120. lpt_program_fdi_mphy(dev_priv);
  7121. }
  7122. }
  7123. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7124. SBI_GEN0 : SBI_DBUFF0;
  7125. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7126. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7127. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7128. mutex_unlock(&dev_priv->sb_lock);
  7129. }
  7130. /* Sequence to disable CLKOUT_DP */
  7131. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7132. {
  7133. struct drm_i915_private *dev_priv = dev->dev_private;
  7134. uint32_t reg, tmp;
  7135. mutex_lock(&dev_priv->sb_lock);
  7136. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7137. SBI_GEN0 : SBI_DBUFF0;
  7138. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7139. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7140. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7141. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7142. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7143. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7144. tmp |= SBI_SSCCTL_PATHALT;
  7145. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7146. udelay(32);
  7147. }
  7148. tmp |= SBI_SSCCTL_DISABLE;
  7149. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7150. }
  7151. mutex_unlock(&dev_priv->sb_lock);
  7152. }
  7153. static void lpt_init_pch_refclk(struct drm_device *dev)
  7154. {
  7155. struct intel_encoder *encoder;
  7156. bool has_vga = false;
  7157. for_each_intel_encoder(dev, encoder) {
  7158. switch (encoder->type) {
  7159. case INTEL_OUTPUT_ANALOG:
  7160. has_vga = true;
  7161. break;
  7162. default:
  7163. break;
  7164. }
  7165. }
  7166. if (has_vga)
  7167. lpt_enable_clkout_dp(dev, true, true);
  7168. else
  7169. lpt_disable_clkout_dp(dev);
  7170. }
  7171. /*
  7172. * Initialize reference clocks when the driver loads
  7173. */
  7174. void intel_init_pch_refclk(struct drm_device *dev)
  7175. {
  7176. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7177. ironlake_init_pch_refclk(dev);
  7178. else if (HAS_PCH_LPT(dev))
  7179. lpt_init_pch_refclk(dev);
  7180. }
  7181. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7182. {
  7183. struct drm_device *dev = crtc_state->base.crtc->dev;
  7184. struct drm_i915_private *dev_priv = dev->dev_private;
  7185. struct drm_atomic_state *state = crtc_state->base.state;
  7186. struct drm_connector *connector;
  7187. struct drm_connector_state *connector_state;
  7188. struct intel_encoder *encoder;
  7189. int num_connectors = 0, i;
  7190. bool is_lvds = false;
  7191. for_each_connector_in_state(state, connector, connector_state, i) {
  7192. if (connector_state->crtc != crtc_state->base.crtc)
  7193. continue;
  7194. encoder = to_intel_encoder(connector_state->best_encoder);
  7195. switch (encoder->type) {
  7196. case INTEL_OUTPUT_LVDS:
  7197. is_lvds = true;
  7198. break;
  7199. default:
  7200. break;
  7201. }
  7202. num_connectors++;
  7203. }
  7204. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7205. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7206. dev_priv->vbt.lvds_ssc_freq);
  7207. return dev_priv->vbt.lvds_ssc_freq;
  7208. }
  7209. return 120000;
  7210. }
  7211. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7212. {
  7213. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7215. int pipe = intel_crtc->pipe;
  7216. uint32_t val;
  7217. val = 0;
  7218. switch (intel_crtc->config->pipe_bpp) {
  7219. case 18:
  7220. val |= PIPECONF_6BPC;
  7221. break;
  7222. case 24:
  7223. val |= PIPECONF_8BPC;
  7224. break;
  7225. case 30:
  7226. val |= PIPECONF_10BPC;
  7227. break;
  7228. case 36:
  7229. val |= PIPECONF_12BPC;
  7230. break;
  7231. default:
  7232. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7233. BUG();
  7234. }
  7235. if (intel_crtc->config->dither)
  7236. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7237. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7238. val |= PIPECONF_INTERLACED_ILK;
  7239. else
  7240. val |= PIPECONF_PROGRESSIVE;
  7241. if (intel_crtc->config->limited_color_range)
  7242. val |= PIPECONF_COLOR_RANGE_SELECT;
  7243. I915_WRITE(PIPECONF(pipe), val);
  7244. POSTING_READ(PIPECONF(pipe));
  7245. }
  7246. /*
  7247. * Set up the pipe CSC unit.
  7248. *
  7249. * Currently only full range RGB to limited range RGB conversion
  7250. * is supported, but eventually this should handle various
  7251. * RGB<->YCbCr scenarios as well.
  7252. */
  7253. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7254. {
  7255. struct drm_device *dev = crtc->dev;
  7256. struct drm_i915_private *dev_priv = dev->dev_private;
  7257. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7258. int pipe = intel_crtc->pipe;
  7259. uint16_t coeff = 0x7800; /* 1.0 */
  7260. /*
  7261. * TODO: Check what kind of values actually come out of the pipe
  7262. * with these coeff/postoff values and adjust to get the best
  7263. * accuracy. Perhaps we even need to take the bpc value into
  7264. * consideration.
  7265. */
  7266. if (intel_crtc->config->limited_color_range)
  7267. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7268. /*
  7269. * GY/GU and RY/RU should be the other way around according
  7270. * to BSpec, but reality doesn't agree. Just set them up in
  7271. * a way that results in the correct picture.
  7272. */
  7273. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7274. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7275. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7276. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7277. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7278. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7279. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7280. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7281. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7282. if (INTEL_INFO(dev)->gen > 6) {
  7283. uint16_t postoff = 0;
  7284. if (intel_crtc->config->limited_color_range)
  7285. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7286. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7287. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7288. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7289. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7290. } else {
  7291. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7292. if (intel_crtc->config->limited_color_range)
  7293. mode |= CSC_BLACK_SCREEN_OFFSET;
  7294. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7295. }
  7296. }
  7297. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7298. {
  7299. struct drm_device *dev = crtc->dev;
  7300. struct drm_i915_private *dev_priv = dev->dev_private;
  7301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7302. enum pipe pipe = intel_crtc->pipe;
  7303. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7304. uint32_t val;
  7305. val = 0;
  7306. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7307. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7308. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7309. val |= PIPECONF_INTERLACED_ILK;
  7310. else
  7311. val |= PIPECONF_PROGRESSIVE;
  7312. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7313. POSTING_READ(PIPECONF(cpu_transcoder));
  7314. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7315. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7316. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7317. val = 0;
  7318. switch (intel_crtc->config->pipe_bpp) {
  7319. case 18:
  7320. val |= PIPEMISC_DITHER_6_BPC;
  7321. break;
  7322. case 24:
  7323. val |= PIPEMISC_DITHER_8_BPC;
  7324. break;
  7325. case 30:
  7326. val |= PIPEMISC_DITHER_10_BPC;
  7327. break;
  7328. case 36:
  7329. val |= PIPEMISC_DITHER_12_BPC;
  7330. break;
  7331. default:
  7332. /* Case prevented by pipe_config_set_bpp. */
  7333. BUG();
  7334. }
  7335. if (intel_crtc->config->dither)
  7336. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7337. I915_WRITE(PIPEMISC(pipe), val);
  7338. }
  7339. }
  7340. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7341. struct intel_crtc_state *crtc_state,
  7342. intel_clock_t *clock,
  7343. bool *has_reduced_clock,
  7344. intel_clock_t *reduced_clock)
  7345. {
  7346. struct drm_device *dev = crtc->dev;
  7347. struct drm_i915_private *dev_priv = dev->dev_private;
  7348. int refclk;
  7349. const intel_limit_t *limit;
  7350. bool ret, is_lvds = false;
  7351. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7352. refclk = ironlake_get_refclk(crtc_state);
  7353. /*
  7354. * Returns a set of divisors for the desired target clock with the given
  7355. * refclk, or FALSE. The returned values represent the clock equation:
  7356. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7357. */
  7358. limit = intel_limit(crtc_state, refclk);
  7359. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7360. crtc_state->port_clock,
  7361. refclk, NULL, clock);
  7362. if (!ret)
  7363. return false;
  7364. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7365. /*
  7366. * Ensure we match the reduced clock's P to the target clock.
  7367. * If the clocks don't match, we can't switch the display clock
  7368. * by using the FP0/FP1. In such case we will disable the LVDS
  7369. * downclock feature.
  7370. */
  7371. *has_reduced_clock =
  7372. dev_priv->display.find_dpll(limit, crtc_state,
  7373. dev_priv->lvds_downclock,
  7374. refclk, clock,
  7375. reduced_clock);
  7376. }
  7377. return true;
  7378. }
  7379. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7380. {
  7381. /*
  7382. * Account for spread spectrum to avoid
  7383. * oversubscribing the link. Max center spread
  7384. * is 2.5%; use 5% for safety's sake.
  7385. */
  7386. u32 bps = target_clock * bpp * 21 / 20;
  7387. return DIV_ROUND_UP(bps, link_bw * 8);
  7388. }
  7389. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7390. {
  7391. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7392. }
  7393. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7394. struct intel_crtc_state *crtc_state,
  7395. u32 *fp,
  7396. intel_clock_t *reduced_clock, u32 *fp2)
  7397. {
  7398. struct drm_crtc *crtc = &intel_crtc->base;
  7399. struct drm_device *dev = crtc->dev;
  7400. struct drm_i915_private *dev_priv = dev->dev_private;
  7401. struct drm_atomic_state *state = crtc_state->base.state;
  7402. struct drm_connector *connector;
  7403. struct drm_connector_state *connector_state;
  7404. struct intel_encoder *encoder;
  7405. uint32_t dpll;
  7406. int factor, num_connectors = 0, i;
  7407. bool is_lvds = false, is_sdvo = false;
  7408. for_each_connector_in_state(state, connector, connector_state, i) {
  7409. if (connector_state->crtc != crtc_state->base.crtc)
  7410. continue;
  7411. encoder = to_intel_encoder(connector_state->best_encoder);
  7412. switch (encoder->type) {
  7413. case INTEL_OUTPUT_LVDS:
  7414. is_lvds = true;
  7415. break;
  7416. case INTEL_OUTPUT_SDVO:
  7417. case INTEL_OUTPUT_HDMI:
  7418. is_sdvo = true;
  7419. break;
  7420. default:
  7421. break;
  7422. }
  7423. num_connectors++;
  7424. }
  7425. /* Enable autotuning of the PLL clock (if permissible) */
  7426. factor = 21;
  7427. if (is_lvds) {
  7428. if ((intel_panel_use_ssc(dev_priv) &&
  7429. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7430. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7431. factor = 25;
  7432. } else if (crtc_state->sdvo_tv_clock)
  7433. factor = 20;
  7434. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7435. *fp |= FP_CB_TUNE;
  7436. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7437. *fp2 |= FP_CB_TUNE;
  7438. dpll = 0;
  7439. if (is_lvds)
  7440. dpll |= DPLLB_MODE_LVDS;
  7441. else
  7442. dpll |= DPLLB_MODE_DAC_SERIAL;
  7443. dpll |= (crtc_state->pixel_multiplier - 1)
  7444. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7445. if (is_sdvo)
  7446. dpll |= DPLL_SDVO_HIGH_SPEED;
  7447. if (crtc_state->has_dp_encoder)
  7448. dpll |= DPLL_SDVO_HIGH_SPEED;
  7449. /* compute bitmask from p1 value */
  7450. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7451. /* also FPA1 */
  7452. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7453. switch (crtc_state->dpll.p2) {
  7454. case 5:
  7455. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7456. break;
  7457. case 7:
  7458. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7459. break;
  7460. case 10:
  7461. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7462. break;
  7463. case 14:
  7464. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7465. break;
  7466. }
  7467. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7468. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7469. else
  7470. dpll |= PLL_REF_INPUT_DREFCLK;
  7471. return dpll | DPLL_VCO_ENABLE;
  7472. }
  7473. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7474. struct intel_crtc_state *crtc_state)
  7475. {
  7476. struct drm_device *dev = crtc->base.dev;
  7477. intel_clock_t clock, reduced_clock;
  7478. u32 dpll = 0, fp = 0, fp2 = 0;
  7479. bool ok, has_reduced_clock = false;
  7480. bool is_lvds = false;
  7481. struct intel_shared_dpll *pll;
  7482. memset(&crtc_state->dpll_hw_state, 0,
  7483. sizeof(crtc_state->dpll_hw_state));
  7484. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7485. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7486. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7487. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7488. &has_reduced_clock, &reduced_clock);
  7489. if (!ok && !crtc_state->clock_set) {
  7490. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7491. return -EINVAL;
  7492. }
  7493. /* Compat-code for transition, will disappear. */
  7494. if (!crtc_state->clock_set) {
  7495. crtc_state->dpll.n = clock.n;
  7496. crtc_state->dpll.m1 = clock.m1;
  7497. crtc_state->dpll.m2 = clock.m2;
  7498. crtc_state->dpll.p1 = clock.p1;
  7499. crtc_state->dpll.p2 = clock.p2;
  7500. }
  7501. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7502. if (crtc_state->has_pch_encoder) {
  7503. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7504. if (has_reduced_clock)
  7505. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7506. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7507. &fp, &reduced_clock,
  7508. has_reduced_clock ? &fp2 : NULL);
  7509. crtc_state->dpll_hw_state.dpll = dpll;
  7510. crtc_state->dpll_hw_state.fp0 = fp;
  7511. if (has_reduced_clock)
  7512. crtc_state->dpll_hw_state.fp1 = fp2;
  7513. else
  7514. crtc_state->dpll_hw_state.fp1 = fp;
  7515. pll = intel_get_shared_dpll(crtc, crtc_state);
  7516. if (pll == NULL) {
  7517. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7518. pipe_name(crtc->pipe));
  7519. return -EINVAL;
  7520. }
  7521. }
  7522. if (is_lvds && has_reduced_clock)
  7523. crtc->lowfreq_avail = true;
  7524. else
  7525. crtc->lowfreq_avail = false;
  7526. return 0;
  7527. }
  7528. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7529. struct intel_link_m_n *m_n)
  7530. {
  7531. struct drm_device *dev = crtc->base.dev;
  7532. struct drm_i915_private *dev_priv = dev->dev_private;
  7533. enum pipe pipe = crtc->pipe;
  7534. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7535. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7536. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7537. & ~TU_SIZE_MASK;
  7538. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7539. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7540. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7541. }
  7542. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7543. enum transcoder transcoder,
  7544. struct intel_link_m_n *m_n,
  7545. struct intel_link_m_n *m2_n2)
  7546. {
  7547. struct drm_device *dev = crtc->base.dev;
  7548. struct drm_i915_private *dev_priv = dev->dev_private;
  7549. enum pipe pipe = crtc->pipe;
  7550. if (INTEL_INFO(dev)->gen >= 5) {
  7551. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7552. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7553. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7554. & ~TU_SIZE_MASK;
  7555. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7556. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7557. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7558. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7559. * gen < 8) and if DRRS is supported (to make sure the
  7560. * registers are not unnecessarily read).
  7561. */
  7562. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7563. crtc->config->has_drrs) {
  7564. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7565. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7566. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7567. & ~TU_SIZE_MASK;
  7568. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7569. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7570. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7571. }
  7572. } else {
  7573. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7574. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7575. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7576. & ~TU_SIZE_MASK;
  7577. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7578. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7579. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7580. }
  7581. }
  7582. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7583. struct intel_crtc_state *pipe_config)
  7584. {
  7585. if (pipe_config->has_pch_encoder)
  7586. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7587. else
  7588. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7589. &pipe_config->dp_m_n,
  7590. &pipe_config->dp_m2_n2);
  7591. }
  7592. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7593. struct intel_crtc_state *pipe_config)
  7594. {
  7595. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7596. &pipe_config->fdi_m_n, NULL);
  7597. }
  7598. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7599. struct intel_crtc_state *pipe_config)
  7600. {
  7601. struct drm_device *dev = crtc->base.dev;
  7602. struct drm_i915_private *dev_priv = dev->dev_private;
  7603. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7604. uint32_t ps_ctrl = 0;
  7605. int id = -1;
  7606. int i;
  7607. /* find scaler attached to this pipe */
  7608. for (i = 0; i < crtc->num_scalers; i++) {
  7609. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7610. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7611. id = i;
  7612. pipe_config->pch_pfit.enabled = true;
  7613. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7614. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7615. break;
  7616. }
  7617. }
  7618. scaler_state->scaler_id = id;
  7619. if (id >= 0) {
  7620. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7621. } else {
  7622. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7623. }
  7624. }
  7625. static void
  7626. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7627. struct intel_initial_plane_config *plane_config)
  7628. {
  7629. struct drm_device *dev = crtc->base.dev;
  7630. struct drm_i915_private *dev_priv = dev->dev_private;
  7631. u32 val, base, offset, stride_mult, tiling;
  7632. int pipe = crtc->pipe;
  7633. int fourcc, pixel_format;
  7634. unsigned int aligned_height;
  7635. struct drm_framebuffer *fb;
  7636. struct intel_framebuffer *intel_fb;
  7637. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7638. if (!intel_fb) {
  7639. DRM_DEBUG_KMS("failed to alloc fb\n");
  7640. return;
  7641. }
  7642. fb = &intel_fb->base;
  7643. val = I915_READ(PLANE_CTL(pipe, 0));
  7644. if (!(val & PLANE_CTL_ENABLE))
  7645. goto error;
  7646. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7647. fourcc = skl_format_to_fourcc(pixel_format,
  7648. val & PLANE_CTL_ORDER_RGBX,
  7649. val & PLANE_CTL_ALPHA_MASK);
  7650. fb->pixel_format = fourcc;
  7651. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7652. tiling = val & PLANE_CTL_TILED_MASK;
  7653. switch (tiling) {
  7654. case PLANE_CTL_TILED_LINEAR:
  7655. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7656. break;
  7657. case PLANE_CTL_TILED_X:
  7658. plane_config->tiling = I915_TILING_X;
  7659. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7660. break;
  7661. case PLANE_CTL_TILED_Y:
  7662. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7663. break;
  7664. case PLANE_CTL_TILED_YF:
  7665. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7666. break;
  7667. default:
  7668. MISSING_CASE(tiling);
  7669. goto error;
  7670. }
  7671. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7672. plane_config->base = base;
  7673. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7674. val = I915_READ(PLANE_SIZE(pipe, 0));
  7675. fb->height = ((val >> 16) & 0xfff) + 1;
  7676. fb->width = ((val >> 0) & 0x1fff) + 1;
  7677. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7678. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7679. fb->pixel_format);
  7680. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7681. aligned_height = intel_fb_align_height(dev, fb->height,
  7682. fb->pixel_format,
  7683. fb->modifier[0]);
  7684. plane_config->size = fb->pitches[0] * aligned_height;
  7685. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7686. pipe_name(pipe), fb->width, fb->height,
  7687. fb->bits_per_pixel, base, fb->pitches[0],
  7688. plane_config->size);
  7689. plane_config->fb = intel_fb;
  7690. return;
  7691. error:
  7692. kfree(fb);
  7693. }
  7694. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7695. struct intel_crtc_state *pipe_config)
  7696. {
  7697. struct drm_device *dev = crtc->base.dev;
  7698. struct drm_i915_private *dev_priv = dev->dev_private;
  7699. uint32_t tmp;
  7700. tmp = I915_READ(PF_CTL(crtc->pipe));
  7701. if (tmp & PF_ENABLE) {
  7702. pipe_config->pch_pfit.enabled = true;
  7703. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7704. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7705. /* We currently do not free assignements of panel fitters on
  7706. * ivb/hsw (since we don't use the higher upscaling modes which
  7707. * differentiates them) so just WARN about this case for now. */
  7708. if (IS_GEN7(dev)) {
  7709. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7710. PF_PIPE_SEL_IVB(crtc->pipe));
  7711. }
  7712. }
  7713. }
  7714. static void
  7715. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7716. struct intel_initial_plane_config *plane_config)
  7717. {
  7718. struct drm_device *dev = crtc->base.dev;
  7719. struct drm_i915_private *dev_priv = dev->dev_private;
  7720. u32 val, base, offset;
  7721. int pipe = crtc->pipe;
  7722. int fourcc, pixel_format;
  7723. unsigned int aligned_height;
  7724. struct drm_framebuffer *fb;
  7725. struct intel_framebuffer *intel_fb;
  7726. val = I915_READ(DSPCNTR(pipe));
  7727. if (!(val & DISPLAY_PLANE_ENABLE))
  7728. return;
  7729. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7730. if (!intel_fb) {
  7731. DRM_DEBUG_KMS("failed to alloc fb\n");
  7732. return;
  7733. }
  7734. fb = &intel_fb->base;
  7735. if (INTEL_INFO(dev)->gen >= 4) {
  7736. if (val & DISPPLANE_TILED) {
  7737. plane_config->tiling = I915_TILING_X;
  7738. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7739. }
  7740. }
  7741. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7742. fourcc = i9xx_format_to_fourcc(pixel_format);
  7743. fb->pixel_format = fourcc;
  7744. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7745. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7746. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7747. offset = I915_READ(DSPOFFSET(pipe));
  7748. } else {
  7749. if (plane_config->tiling)
  7750. offset = I915_READ(DSPTILEOFF(pipe));
  7751. else
  7752. offset = I915_READ(DSPLINOFF(pipe));
  7753. }
  7754. plane_config->base = base;
  7755. val = I915_READ(PIPESRC(pipe));
  7756. fb->width = ((val >> 16) & 0xfff) + 1;
  7757. fb->height = ((val >> 0) & 0xfff) + 1;
  7758. val = I915_READ(DSPSTRIDE(pipe));
  7759. fb->pitches[0] = val & 0xffffffc0;
  7760. aligned_height = intel_fb_align_height(dev, fb->height,
  7761. fb->pixel_format,
  7762. fb->modifier[0]);
  7763. plane_config->size = fb->pitches[0] * aligned_height;
  7764. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7765. pipe_name(pipe), fb->width, fb->height,
  7766. fb->bits_per_pixel, base, fb->pitches[0],
  7767. plane_config->size);
  7768. plane_config->fb = intel_fb;
  7769. }
  7770. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7771. struct intel_crtc_state *pipe_config)
  7772. {
  7773. struct drm_device *dev = crtc->base.dev;
  7774. struct drm_i915_private *dev_priv = dev->dev_private;
  7775. uint32_t tmp;
  7776. if (!intel_display_power_is_enabled(dev_priv,
  7777. POWER_DOMAIN_PIPE(crtc->pipe)))
  7778. return false;
  7779. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7780. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7781. tmp = I915_READ(PIPECONF(crtc->pipe));
  7782. if (!(tmp & PIPECONF_ENABLE))
  7783. return false;
  7784. switch (tmp & PIPECONF_BPC_MASK) {
  7785. case PIPECONF_6BPC:
  7786. pipe_config->pipe_bpp = 18;
  7787. break;
  7788. case PIPECONF_8BPC:
  7789. pipe_config->pipe_bpp = 24;
  7790. break;
  7791. case PIPECONF_10BPC:
  7792. pipe_config->pipe_bpp = 30;
  7793. break;
  7794. case PIPECONF_12BPC:
  7795. pipe_config->pipe_bpp = 36;
  7796. break;
  7797. default:
  7798. break;
  7799. }
  7800. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7801. pipe_config->limited_color_range = true;
  7802. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7803. struct intel_shared_dpll *pll;
  7804. pipe_config->has_pch_encoder = true;
  7805. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7806. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7807. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7808. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7809. if (HAS_PCH_IBX(dev_priv->dev)) {
  7810. pipe_config->shared_dpll =
  7811. (enum intel_dpll_id) crtc->pipe;
  7812. } else {
  7813. tmp = I915_READ(PCH_DPLL_SEL);
  7814. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7815. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7816. else
  7817. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7818. }
  7819. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7820. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7821. &pipe_config->dpll_hw_state));
  7822. tmp = pipe_config->dpll_hw_state.dpll;
  7823. pipe_config->pixel_multiplier =
  7824. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7825. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7826. ironlake_pch_clock_get(crtc, pipe_config);
  7827. } else {
  7828. pipe_config->pixel_multiplier = 1;
  7829. }
  7830. intel_get_pipe_timings(crtc, pipe_config);
  7831. ironlake_get_pfit_config(crtc, pipe_config);
  7832. return true;
  7833. }
  7834. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7835. {
  7836. struct drm_device *dev = dev_priv->dev;
  7837. struct intel_crtc *crtc;
  7838. for_each_intel_crtc(dev, crtc)
  7839. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7840. pipe_name(crtc->pipe));
  7841. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7842. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7843. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7844. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7845. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7846. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7847. "CPU PWM1 enabled\n");
  7848. if (IS_HASWELL(dev))
  7849. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7850. "CPU PWM2 enabled\n");
  7851. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7852. "PCH PWM1 enabled\n");
  7853. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7854. "Utility pin enabled\n");
  7855. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7856. /*
  7857. * In theory we can still leave IRQs enabled, as long as only the HPD
  7858. * interrupts remain enabled. We used to check for that, but since it's
  7859. * gen-specific and since we only disable LCPLL after we fully disable
  7860. * the interrupts, the check below should be enough.
  7861. */
  7862. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7863. }
  7864. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7865. {
  7866. struct drm_device *dev = dev_priv->dev;
  7867. if (IS_HASWELL(dev))
  7868. return I915_READ(D_COMP_HSW);
  7869. else
  7870. return I915_READ(D_COMP_BDW);
  7871. }
  7872. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7873. {
  7874. struct drm_device *dev = dev_priv->dev;
  7875. if (IS_HASWELL(dev)) {
  7876. mutex_lock(&dev_priv->rps.hw_lock);
  7877. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7878. val))
  7879. DRM_ERROR("Failed to write to D_COMP\n");
  7880. mutex_unlock(&dev_priv->rps.hw_lock);
  7881. } else {
  7882. I915_WRITE(D_COMP_BDW, val);
  7883. POSTING_READ(D_COMP_BDW);
  7884. }
  7885. }
  7886. /*
  7887. * This function implements pieces of two sequences from BSpec:
  7888. * - Sequence for display software to disable LCPLL
  7889. * - Sequence for display software to allow package C8+
  7890. * The steps implemented here are just the steps that actually touch the LCPLL
  7891. * register. Callers should take care of disabling all the display engine
  7892. * functions, doing the mode unset, fixing interrupts, etc.
  7893. */
  7894. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7895. bool switch_to_fclk, bool allow_power_down)
  7896. {
  7897. uint32_t val;
  7898. assert_can_disable_lcpll(dev_priv);
  7899. val = I915_READ(LCPLL_CTL);
  7900. if (switch_to_fclk) {
  7901. val |= LCPLL_CD_SOURCE_FCLK;
  7902. I915_WRITE(LCPLL_CTL, val);
  7903. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7904. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7905. DRM_ERROR("Switching to FCLK failed\n");
  7906. val = I915_READ(LCPLL_CTL);
  7907. }
  7908. val |= LCPLL_PLL_DISABLE;
  7909. I915_WRITE(LCPLL_CTL, val);
  7910. POSTING_READ(LCPLL_CTL);
  7911. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7912. DRM_ERROR("LCPLL still locked\n");
  7913. val = hsw_read_dcomp(dev_priv);
  7914. val |= D_COMP_COMP_DISABLE;
  7915. hsw_write_dcomp(dev_priv, val);
  7916. ndelay(100);
  7917. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7918. 1))
  7919. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7920. if (allow_power_down) {
  7921. val = I915_READ(LCPLL_CTL);
  7922. val |= LCPLL_POWER_DOWN_ALLOW;
  7923. I915_WRITE(LCPLL_CTL, val);
  7924. POSTING_READ(LCPLL_CTL);
  7925. }
  7926. }
  7927. /*
  7928. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7929. * source.
  7930. */
  7931. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7932. {
  7933. uint32_t val;
  7934. val = I915_READ(LCPLL_CTL);
  7935. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7936. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7937. return;
  7938. /*
  7939. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7940. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7941. */
  7942. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7943. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7944. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7945. I915_WRITE(LCPLL_CTL, val);
  7946. POSTING_READ(LCPLL_CTL);
  7947. }
  7948. val = hsw_read_dcomp(dev_priv);
  7949. val |= D_COMP_COMP_FORCE;
  7950. val &= ~D_COMP_COMP_DISABLE;
  7951. hsw_write_dcomp(dev_priv, val);
  7952. val = I915_READ(LCPLL_CTL);
  7953. val &= ~LCPLL_PLL_DISABLE;
  7954. I915_WRITE(LCPLL_CTL, val);
  7955. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7956. DRM_ERROR("LCPLL not locked yet\n");
  7957. if (val & LCPLL_CD_SOURCE_FCLK) {
  7958. val = I915_READ(LCPLL_CTL);
  7959. val &= ~LCPLL_CD_SOURCE_FCLK;
  7960. I915_WRITE(LCPLL_CTL, val);
  7961. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7962. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7963. DRM_ERROR("Switching back to LCPLL failed\n");
  7964. }
  7965. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7966. intel_update_cdclk(dev_priv->dev);
  7967. }
  7968. /*
  7969. * Package states C8 and deeper are really deep PC states that can only be
  7970. * reached when all the devices on the system allow it, so even if the graphics
  7971. * device allows PC8+, it doesn't mean the system will actually get to these
  7972. * states. Our driver only allows PC8+ when going into runtime PM.
  7973. *
  7974. * The requirements for PC8+ are that all the outputs are disabled, the power
  7975. * well is disabled and most interrupts are disabled, and these are also
  7976. * requirements for runtime PM. When these conditions are met, we manually do
  7977. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7978. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7979. * hang the machine.
  7980. *
  7981. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7982. * the state of some registers, so when we come back from PC8+ we need to
  7983. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7984. * need to take care of the registers kept by RC6. Notice that this happens even
  7985. * if we don't put the device in PCI D3 state (which is what currently happens
  7986. * because of the runtime PM support).
  7987. *
  7988. * For more, read "Display Sequences for Package C8" on the hardware
  7989. * documentation.
  7990. */
  7991. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7992. {
  7993. struct drm_device *dev = dev_priv->dev;
  7994. uint32_t val;
  7995. DRM_DEBUG_KMS("Enabling package C8+\n");
  7996. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7997. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7998. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7999. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8000. }
  8001. lpt_disable_clkout_dp(dev);
  8002. hsw_disable_lcpll(dev_priv, true, true);
  8003. }
  8004. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8005. {
  8006. struct drm_device *dev = dev_priv->dev;
  8007. uint32_t val;
  8008. DRM_DEBUG_KMS("Disabling package C8+\n");
  8009. hsw_restore_lcpll(dev_priv);
  8010. lpt_init_pch_refclk(dev);
  8011. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8012. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8013. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8014. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8015. }
  8016. intel_prepare_ddi(dev);
  8017. }
  8018. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  8019. {
  8020. struct drm_device *dev = old_state->dev;
  8021. struct drm_i915_private *dev_priv = dev->dev_private;
  8022. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  8023. int req_cdclk;
  8024. /* see the comment in valleyview_modeset_global_resources */
  8025. if (WARN_ON(max_pixclk < 0))
  8026. return;
  8027. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  8028. if (req_cdclk != dev_priv->cdclk_freq)
  8029. broxton_set_cdclk(dev, req_cdclk);
  8030. }
  8031. /* compute the max rate for new configuration */
  8032. static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
  8033. {
  8034. struct drm_device *dev = dev_priv->dev;
  8035. struct intel_crtc *intel_crtc;
  8036. struct drm_crtc *crtc;
  8037. int max_pixel_rate = 0;
  8038. int pixel_rate;
  8039. for_each_crtc(dev, crtc) {
  8040. if (!crtc->state->enable)
  8041. continue;
  8042. intel_crtc = to_intel_crtc(crtc);
  8043. pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
  8044. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8045. if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
  8046. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8047. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8048. }
  8049. return max_pixel_rate;
  8050. }
  8051. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8052. {
  8053. struct drm_i915_private *dev_priv = dev->dev_private;
  8054. uint32_t val, data;
  8055. int ret;
  8056. if (WARN((I915_READ(LCPLL_CTL) &
  8057. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8058. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8059. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8060. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8061. "trying to change cdclk frequency with cdclk not enabled\n"))
  8062. return;
  8063. mutex_lock(&dev_priv->rps.hw_lock);
  8064. ret = sandybridge_pcode_write(dev_priv,
  8065. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8066. mutex_unlock(&dev_priv->rps.hw_lock);
  8067. if (ret) {
  8068. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8069. return;
  8070. }
  8071. val = I915_READ(LCPLL_CTL);
  8072. val |= LCPLL_CD_SOURCE_FCLK;
  8073. I915_WRITE(LCPLL_CTL, val);
  8074. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8075. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8076. DRM_ERROR("Switching to FCLK failed\n");
  8077. val = I915_READ(LCPLL_CTL);
  8078. val &= ~LCPLL_CLK_FREQ_MASK;
  8079. switch (cdclk) {
  8080. case 450000:
  8081. val |= LCPLL_CLK_FREQ_450;
  8082. data = 0;
  8083. break;
  8084. case 540000:
  8085. val |= LCPLL_CLK_FREQ_54O_BDW;
  8086. data = 1;
  8087. break;
  8088. case 337500:
  8089. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8090. data = 2;
  8091. break;
  8092. case 675000:
  8093. val |= LCPLL_CLK_FREQ_675_BDW;
  8094. data = 3;
  8095. break;
  8096. default:
  8097. WARN(1, "invalid cdclk frequency\n");
  8098. return;
  8099. }
  8100. I915_WRITE(LCPLL_CTL, val);
  8101. val = I915_READ(LCPLL_CTL);
  8102. val &= ~LCPLL_CD_SOURCE_FCLK;
  8103. I915_WRITE(LCPLL_CTL, val);
  8104. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8105. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8106. DRM_ERROR("Switching back to LCPLL failed\n");
  8107. mutex_lock(&dev_priv->rps.hw_lock);
  8108. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8109. mutex_unlock(&dev_priv->rps.hw_lock);
  8110. intel_update_cdclk(dev);
  8111. WARN(cdclk != dev_priv->cdclk_freq,
  8112. "cdclk requested %d kHz but got %d kHz\n",
  8113. cdclk, dev_priv->cdclk_freq);
  8114. }
  8115. static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
  8116. int max_pixel_rate)
  8117. {
  8118. int cdclk;
  8119. /*
  8120. * FIXME should also account for plane ratio
  8121. * once 64bpp pixel formats are supported.
  8122. */
  8123. if (max_pixel_rate > 540000)
  8124. cdclk = 675000;
  8125. else if (max_pixel_rate > 450000)
  8126. cdclk = 540000;
  8127. else if (max_pixel_rate > 337500)
  8128. cdclk = 450000;
  8129. else
  8130. cdclk = 337500;
  8131. /*
  8132. * FIXME move the cdclk caclulation to
  8133. * compute_config() so we can fail gracegully.
  8134. */
  8135. if (cdclk > dev_priv->max_cdclk_freq) {
  8136. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8137. cdclk, dev_priv->max_cdclk_freq);
  8138. cdclk = dev_priv->max_cdclk_freq;
  8139. }
  8140. return cdclk;
  8141. }
  8142. static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
  8143. {
  8144. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8145. struct drm_crtc *crtc;
  8146. struct drm_crtc_state *crtc_state;
  8147. int max_pixclk = ilk_max_pixel_rate(dev_priv);
  8148. int cdclk, i;
  8149. cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
  8150. if (cdclk == dev_priv->cdclk_freq)
  8151. return 0;
  8152. /* add all active pipes to the state */
  8153. for_each_crtc(state->dev, crtc) {
  8154. if (!crtc->state->enable)
  8155. continue;
  8156. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  8157. if (IS_ERR(crtc_state))
  8158. return PTR_ERR(crtc_state);
  8159. }
  8160. /* disable/enable all currently active pipes while we change cdclk */
  8161. for_each_crtc_in_state(state, crtc, crtc_state, i)
  8162. if (crtc_state->enable)
  8163. crtc_state->mode_changed = true;
  8164. return 0;
  8165. }
  8166. static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
  8167. {
  8168. struct drm_device *dev = state->dev;
  8169. struct drm_i915_private *dev_priv = dev->dev_private;
  8170. int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
  8171. int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
  8172. if (req_cdclk != dev_priv->cdclk_freq)
  8173. broadwell_set_cdclk(dev, req_cdclk);
  8174. }
  8175. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8176. struct intel_crtc_state *crtc_state)
  8177. {
  8178. if (!intel_ddi_pll_select(crtc, crtc_state))
  8179. return -EINVAL;
  8180. crtc->lowfreq_avail = false;
  8181. return 0;
  8182. }
  8183. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8184. enum port port,
  8185. struct intel_crtc_state *pipe_config)
  8186. {
  8187. switch (port) {
  8188. case PORT_A:
  8189. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8190. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8191. break;
  8192. case PORT_B:
  8193. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8194. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8195. break;
  8196. case PORT_C:
  8197. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8198. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8199. break;
  8200. default:
  8201. DRM_ERROR("Incorrect port type\n");
  8202. }
  8203. }
  8204. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8205. enum port port,
  8206. struct intel_crtc_state *pipe_config)
  8207. {
  8208. u32 temp, dpll_ctl1;
  8209. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8210. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8211. switch (pipe_config->ddi_pll_sel) {
  8212. case SKL_DPLL0:
  8213. /*
  8214. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8215. * of the shared DPLL framework and thus needs to be read out
  8216. * separately
  8217. */
  8218. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8219. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8220. break;
  8221. case SKL_DPLL1:
  8222. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8223. break;
  8224. case SKL_DPLL2:
  8225. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8226. break;
  8227. case SKL_DPLL3:
  8228. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8229. break;
  8230. }
  8231. }
  8232. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8233. enum port port,
  8234. struct intel_crtc_state *pipe_config)
  8235. {
  8236. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8237. switch (pipe_config->ddi_pll_sel) {
  8238. case PORT_CLK_SEL_WRPLL1:
  8239. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8240. break;
  8241. case PORT_CLK_SEL_WRPLL2:
  8242. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8243. break;
  8244. }
  8245. }
  8246. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8247. struct intel_crtc_state *pipe_config)
  8248. {
  8249. struct drm_device *dev = crtc->base.dev;
  8250. struct drm_i915_private *dev_priv = dev->dev_private;
  8251. struct intel_shared_dpll *pll;
  8252. enum port port;
  8253. uint32_t tmp;
  8254. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8255. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8256. if (IS_SKYLAKE(dev))
  8257. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8258. else if (IS_BROXTON(dev))
  8259. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8260. else
  8261. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8262. if (pipe_config->shared_dpll >= 0) {
  8263. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8264. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8265. &pipe_config->dpll_hw_state));
  8266. }
  8267. /*
  8268. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8269. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8270. * the PCH transcoder is on.
  8271. */
  8272. if (INTEL_INFO(dev)->gen < 9 &&
  8273. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8274. pipe_config->has_pch_encoder = true;
  8275. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8276. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8277. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8278. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8279. }
  8280. }
  8281. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8282. struct intel_crtc_state *pipe_config)
  8283. {
  8284. struct drm_device *dev = crtc->base.dev;
  8285. struct drm_i915_private *dev_priv = dev->dev_private;
  8286. enum intel_display_power_domain pfit_domain;
  8287. uint32_t tmp;
  8288. if (!intel_display_power_is_enabled(dev_priv,
  8289. POWER_DOMAIN_PIPE(crtc->pipe)))
  8290. return false;
  8291. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8292. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8293. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8294. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8295. enum pipe trans_edp_pipe;
  8296. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8297. default:
  8298. WARN(1, "unknown pipe linked to edp transcoder\n");
  8299. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8300. case TRANS_DDI_EDP_INPUT_A_ON:
  8301. trans_edp_pipe = PIPE_A;
  8302. break;
  8303. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8304. trans_edp_pipe = PIPE_B;
  8305. break;
  8306. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8307. trans_edp_pipe = PIPE_C;
  8308. break;
  8309. }
  8310. if (trans_edp_pipe == crtc->pipe)
  8311. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8312. }
  8313. if (!intel_display_power_is_enabled(dev_priv,
  8314. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8315. return false;
  8316. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8317. if (!(tmp & PIPECONF_ENABLE))
  8318. return false;
  8319. haswell_get_ddi_port_state(crtc, pipe_config);
  8320. intel_get_pipe_timings(crtc, pipe_config);
  8321. if (INTEL_INFO(dev)->gen >= 9) {
  8322. skl_init_scalers(dev, crtc, pipe_config);
  8323. }
  8324. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8325. if (INTEL_INFO(dev)->gen >= 9) {
  8326. pipe_config->scaler_state.scaler_id = -1;
  8327. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8328. }
  8329. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8330. if (INTEL_INFO(dev)->gen == 9)
  8331. skylake_get_pfit_config(crtc, pipe_config);
  8332. else if (INTEL_INFO(dev)->gen < 9)
  8333. ironlake_get_pfit_config(crtc, pipe_config);
  8334. else
  8335. MISSING_CASE(INTEL_INFO(dev)->gen);
  8336. }
  8337. if (IS_HASWELL(dev))
  8338. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8339. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8340. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8341. pipe_config->pixel_multiplier =
  8342. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8343. } else {
  8344. pipe_config->pixel_multiplier = 1;
  8345. }
  8346. return true;
  8347. }
  8348. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8349. {
  8350. struct drm_device *dev = crtc->dev;
  8351. struct drm_i915_private *dev_priv = dev->dev_private;
  8352. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8353. uint32_t cntl = 0, size = 0;
  8354. if (base) {
  8355. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8356. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8357. unsigned int stride = roundup_pow_of_two(width) * 4;
  8358. switch (stride) {
  8359. default:
  8360. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8361. width, stride);
  8362. stride = 256;
  8363. /* fallthrough */
  8364. case 256:
  8365. case 512:
  8366. case 1024:
  8367. case 2048:
  8368. break;
  8369. }
  8370. cntl |= CURSOR_ENABLE |
  8371. CURSOR_GAMMA_ENABLE |
  8372. CURSOR_FORMAT_ARGB |
  8373. CURSOR_STRIDE(stride);
  8374. size = (height << 12) | width;
  8375. }
  8376. if (intel_crtc->cursor_cntl != 0 &&
  8377. (intel_crtc->cursor_base != base ||
  8378. intel_crtc->cursor_size != size ||
  8379. intel_crtc->cursor_cntl != cntl)) {
  8380. /* On these chipsets we can only modify the base/size/stride
  8381. * whilst the cursor is disabled.
  8382. */
  8383. I915_WRITE(_CURACNTR, 0);
  8384. POSTING_READ(_CURACNTR);
  8385. intel_crtc->cursor_cntl = 0;
  8386. }
  8387. if (intel_crtc->cursor_base != base) {
  8388. I915_WRITE(_CURABASE, base);
  8389. intel_crtc->cursor_base = base;
  8390. }
  8391. if (intel_crtc->cursor_size != size) {
  8392. I915_WRITE(CURSIZE, size);
  8393. intel_crtc->cursor_size = size;
  8394. }
  8395. if (intel_crtc->cursor_cntl != cntl) {
  8396. I915_WRITE(_CURACNTR, cntl);
  8397. POSTING_READ(_CURACNTR);
  8398. intel_crtc->cursor_cntl = cntl;
  8399. }
  8400. }
  8401. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8402. {
  8403. struct drm_device *dev = crtc->dev;
  8404. struct drm_i915_private *dev_priv = dev->dev_private;
  8405. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8406. int pipe = intel_crtc->pipe;
  8407. uint32_t cntl;
  8408. cntl = 0;
  8409. if (base) {
  8410. cntl = MCURSOR_GAMMA_ENABLE;
  8411. switch (intel_crtc->base.cursor->state->crtc_w) {
  8412. case 64:
  8413. cntl |= CURSOR_MODE_64_ARGB_AX;
  8414. break;
  8415. case 128:
  8416. cntl |= CURSOR_MODE_128_ARGB_AX;
  8417. break;
  8418. case 256:
  8419. cntl |= CURSOR_MODE_256_ARGB_AX;
  8420. break;
  8421. default:
  8422. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8423. return;
  8424. }
  8425. cntl |= pipe << 28; /* Connect to correct pipe */
  8426. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8427. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8428. }
  8429. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8430. cntl |= CURSOR_ROTATE_180;
  8431. if (intel_crtc->cursor_cntl != cntl) {
  8432. I915_WRITE(CURCNTR(pipe), cntl);
  8433. POSTING_READ(CURCNTR(pipe));
  8434. intel_crtc->cursor_cntl = cntl;
  8435. }
  8436. /* and commit changes on next vblank */
  8437. I915_WRITE(CURBASE(pipe), base);
  8438. POSTING_READ(CURBASE(pipe));
  8439. intel_crtc->cursor_base = base;
  8440. }
  8441. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8442. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8443. bool on)
  8444. {
  8445. struct drm_device *dev = crtc->dev;
  8446. struct drm_i915_private *dev_priv = dev->dev_private;
  8447. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8448. int pipe = intel_crtc->pipe;
  8449. int x = crtc->cursor_x;
  8450. int y = crtc->cursor_y;
  8451. u32 base = 0, pos = 0;
  8452. if (on)
  8453. base = intel_crtc->cursor_addr;
  8454. if (x >= intel_crtc->config->pipe_src_w)
  8455. base = 0;
  8456. if (y >= intel_crtc->config->pipe_src_h)
  8457. base = 0;
  8458. if (x < 0) {
  8459. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8460. base = 0;
  8461. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8462. x = -x;
  8463. }
  8464. pos |= x << CURSOR_X_SHIFT;
  8465. if (y < 0) {
  8466. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8467. base = 0;
  8468. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8469. y = -y;
  8470. }
  8471. pos |= y << CURSOR_Y_SHIFT;
  8472. if (base == 0 && intel_crtc->cursor_base == 0)
  8473. return;
  8474. I915_WRITE(CURPOS(pipe), pos);
  8475. /* ILK+ do this automagically */
  8476. if (HAS_GMCH_DISPLAY(dev) &&
  8477. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8478. base += (intel_crtc->base.cursor->state->crtc_h *
  8479. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8480. }
  8481. if (IS_845G(dev) || IS_I865G(dev))
  8482. i845_update_cursor(crtc, base);
  8483. else
  8484. i9xx_update_cursor(crtc, base);
  8485. }
  8486. static bool cursor_size_ok(struct drm_device *dev,
  8487. uint32_t width, uint32_t height)
  8488. {
  8489. if (width == 0 || height == 0)
  8490. return false;
  8491. /*
  8492. * 845g/865g are special in that they are only limited by
  8493. * the width of their cursors, the height is arbitrary up to
  8494. * the precision of the register. Everything else requires
  8495. * square cursors, limited to a few power-of-two sizes.
  8496. */
  8497. if (IS_845G(dev) || IS_I865G(dev)) {
  8498. if ((width & 63) != 0)
  8499. return false;
  8500. if (width > (IS_845G(dev) ? 64 : 512))
  8501. return false;
  8502. if (height > 1023)
  8503. return false;
  8504. } else {
  8505. switch (width | height) {
  8506. case 256:
  8507. case 128:
  8508. if (IS_GEN2(dev))
  8509. return false;
  8510. case 64:
  8511. break;
  8512. default:
  8513. return false;
  8514. }
  8515. }
  8516. return true;
  8517. }
  8518. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8519. u16 *blue, uint32_t start, uint32_t size)
  8520. {
  8521. int end = (start + size > 256) ? 256 : start + size, i;
  8522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8523. for (i = start; i < end; i++) {
  8524. intel_crtc->lut_r[i] = red[i] >> 8;
  8525. intel_crtc->lut_g[i] = green[i] >> 8;
  8526. intel_crtc->lut_b[i] = blue[i] >> 8;
  8527. }
  8528. intel_crtc_load_lut(crtc);
  8529. }
  8530. /* VESA 640x480x72Hz mode to set on the pipe */
  8531. static struct drm_display_mode load_detect_mode = {
  8532. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8533. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8534. };
  8535. struct drm_framebuffer *
  8536. __intel_framebuffer_create(struct drm_device *dev,
  8537. struct drm_mode_fb_cmd2 *mode_cmd,
  8538. struct drm_i915_gem_object *obj)
  8539. {
  8540. struct intel_framebuffer *intel_fb;
  8541. int ret;
  8542. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8543. if (!intel_fb) {
  8544. drm_gem_object_unreference(&obj->base);
  8545. return ERR_PTR(-ENOMEM);
  8546. }
  8547. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8548. if (ret)
  8549. goto err;
  8550. return &intel_fb->base;
  8551. err:
  8552. drm_gem_object_unreference(&obj->base);
  8553. kfree(intel_fb);
  8554. return ERR_PTR(ret);
  8555. }
  8556. static struct drm_framebuffer *
  8557. intel_framebuffer_create(struct drm_device *dev,
  8558. struct drm_mode_fb_cmd2 *mode_cmd,
  8559. struct drm_i915_gem_object *obj)
  8560. {
  8561. struct drm_framebuffer *fb;
  8562. int ret;
  8563. ret = i915_mutex_lock_interruptible(dev);
  8564. if (ret)
  8565. return ERR_PTR(ret);
  8566. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8567. mutex_unlock(&dev->struct_mutex);
  8568. return fb;
  8569. }
  8570. static u32
  8571. intel_framebuffer_pitch_for_width(int width, int bpp)
  8572. {
  8573. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8574. return ALIGN(pitch, 64);
  8575. }
  8576. static u32
  8577. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8578. {
  8579. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8580. return PAGE_ALIGN(pitch * mode->vdisplay);
  8581. }
  8582. static struct drm_framebuffer *
  8583. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8584. struct drm_display_mode *mode,
  8585. int depth, int bpp)
  8586. {
  8587. struct drm_i915_gem_object *obj;
  8588. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8589. obj = i915_gem_alloc_object(dev,
  8590. intel_framebuffer_size_for_mode(mode, bpp));
  8591. if (obj == NULL)
  8592. return ERR_PTR(-ENOMEM);
  8593. mode_cmd.width = mode->hdisplay;
  8594. mode_cmd.height = mode->vdisplay;
  8595. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8596. bpp);
  8597. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8598. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8599. }
  8600. static struct drm_framebuffer *
  8601. mode_fits_in_fbdev(struct drm_device *dev,
  8602. struct drm_display_mode *mode)
  8603. {
  8604. #ifdef CONFIG_DRM_I915_FBDEV
  8605. struct drm_i915_private *dev_priv = dev->dev_private;
  8606. struct drm_i915_gem_object *obj;
  8607. struct drm_framebuffer *fb;
  8608. if (!dev_priv->fbdev)
  8609. return NULL;
  8610. if (!dev_priv->fbdev->fb)
  8611. return NULL;
  8612. obj = dev_priv->fbdev->fb->obj;
  8613. BUG_ON(!obj);
  8614. fb = &dev_priv->fbdev->fb->base;
  8615. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8616. fb->bits_per_pixel))
  8617. return NULL;
  8618. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8619. return NULL;
  8620. return fb;
  8621. #else
  8622. return NULL;
  8623. #endif
  8624. }
  8625. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8626. struct drm_crtc *crtc,
  8627. struct drm_display_mode *mode,
  8628. struct drm_framebuffer *fb,
  8629. int x, int y)
  8630. {
  8631. struct drm_plane_state *plane_state;
  8632. int hdisplay, vdisplay;
  8633. int ret;
  8634. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8635. if (IS_ERR(plane_state))
  8636. return PTR_ERR(plane_state);
  8637. if (mode)
  8638. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8639. else
  8640. hdisplay = vdisplay = 0;
  8641. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8642. if (ret)
  8643. return ret;
  8644. drm_atomic_set_fb_for_plane(plane_state, fb);
  8645. plane_state->crtc_x = 0;
  8646. plane_state->crtc_y = 0;
  8647. plane_state->crtc_w = hdisplay;
  8648. plane_state->crtc_h = vdisplay;
  8649. plane_state->src_x = x << 16;
  8650. plane_state->src_y = y << 16;
  8651. plane_state->src_w = hdisplay << 16;
  8652. plane_state->src_h = vdisplay << 16;
  8653. return 0;
  8654. }
  8655. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8656. struct drm_display_mode *mode,
  8657. struct intel_load_detect_pipe *old,
  8658. struct drm_modeset_acquire_ctx *ctx)
  8659. {
  8660. struct intel_crtc *intel_crtc;
  8661. struct intel_encoder *intel_encoder =
  8662. intel_attached_encoder(connector);
  8663. struct drm_crtc *possible_crtc;
  8664. struct drm_encoder *encoder = &intel_encoder->base;
  8665. struct drm_crtc *crtc = NULL;
  8666. struct drm_device *dev = encoder->dev;
  8667. struct drm_framebuffer *fb;
  8668. struct drm_mode_config *config = &dev->mode_config;
  8669. struct drm_atomic_state *state = NULL;
  8670. struct drm_connector_state *connector_state;
  8671. struct intel_crtc_state *crtc_state;
  8672. int ret, i = -1;
  8673. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8674. connector->base.id, connector->name,
  8675. encoder->base.id, encoder->name);
  8676. retry:
  8677. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8678. if (ret)
  8679. goto fail_unlock;
  8680. /*
  8681. * Algorithm gets a little messy:
  8682. *
  8683. * - if the connector already has an assigned crtc, use it (but make
  8684. * sure it's on first)
  8685. *
  8686. * - try to find the first unused crtc that can drive this connector,
  8687. * and use that if we find one
  8688. */
  8689. /* See if we already have a CRTC for this connector */
  8690. if (encoder->crtc) {
  8691. crtc = encoder->crtc;
  8692. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8693. if (ret)
  8694. goto fail_unlock;
  8695. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8696. if (ret)
  8697. goto fail_unlock;
  8698. old->dpms_mode = connector->dpms;
  8699. old->load_detect_temp = false;
  8700. /* Make sure the crtc and connector are running */
  8701. if (connector->dpms != DRM_MODE_DPMS_ON)
  8702. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8703. return true;
  8704. }
  8705. /* Find an unused one (if possible) */
  8706. for_each_crtc(dev, possible_crtc) {
  8707. i++;
  8708. if (!(encoder->possible_crtcs & (1 << i)))
  8709. continue;
  8710. if (possible_crtc->state->enable)
  8711. continue;
  8712. /* This can occur when applying the pipe A quirk on resume. */
  8713. if (to_intel_crtc(possible_crtc)->new_enabled)
  8714. continue;
  8715. crtc = possible_crtc;
  8716. break;
  8717. }
  8718. /*
  8719. * If we didn't find an unused CRTC, don't use any.
  8720. */
  8721. if (!crtc) {
  8722. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8723. goto fail_unlock;
  8724. }
  8725. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8726. if (ret)
  8727. goto fail_unlock;
  8728. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8729. if (ret)
  8730. goto fail_unlock;
  8731. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8732. to_intel_connector(connector)->new_encoder = intel_encoder;
  8733. intel_crtc = to_intel_crtc(crtc);
  8734. intel_crtc->new_enabled = true;
  8735. old->dpms_mode = connector->dpms;
  8736. old->load_detect_temp = true;
  8737. old->release_fb = NULL;
  8738. state = drm_atomic_state_alloc(dev);
  8739. if (!state)
  8740. return false;
  8741. state->acquire_ctx = ctx;
  8742. connector_state = drm_atomic_get_connector_state(state, connector);
  8743. if (IS_ERR(connector_state)) {
  8744. ret = PTR_ERR(connector_state);
  8745. goto fail;
  8746. }
  8747. connector_state->crtc = crtc;
  8748. connector_state->best_encoder = &intel_encoder->base;
  8749. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8750. if (IS_ERR(crtc_state)) {
  8751. ret = PTR_ERR(crtc_state);
  8752. goto fail;
  8753. }
  8754. crtc_state->base.active = crtc_state->base.enable = true;
  8755. if (!mode)
  8756. mode = &load_detect_mode;
  8757. /* We need a framebuffer large enough to accommodate all accesses
  8758. * that the plane may generate whilst we perform load detection.
  8759. * We can not rely on the fbcon either being present (we get called
  8760. * during its initialisation to detect all boot displays, or it may
  8761. * not even exist) or that it is large enough to satisfy the
  8762. * requested mode.
  8763. */
  8764. fb = mode_fits_in_fbdev(dev, mode);
  8765. if (fb == NULL) {
  8766. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8767. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8768. old->release_fb = fb;
  8769. } else
  8770. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8771. if (IS_ERR(fb)) {
  8772. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8773. goto fail;
  8774. }
  8775. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8776. if (ret)
  8777. goto fail;
  8778. drm_mode_copy(&crtc_state->base.mode, mode);
  8779. if (intel_set_mode(state)) {
  8780. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8781. if (old->release_fb)
  8782. old->release_fb->funcs->destroy(old->release_fb);
  8783. goto fail;
  8784. }
  8785. crtc->primary->crtc = crtc;
  8786. /* let the connector get through one full cycle before testing */
  8787. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8788. return true;
  8789. fail:
  8790. intel_crtc->new_enabled = crtc->state->enable;
  8791. fail_unlock:
  8792. drm_atomic_state_free(state);
  8793. state = NULL;
  8794. if (ret == -EDEADLK) {
  8795. drm_modeset_backoff(ctx);
  8796. goto retry;
  8797. }
  8798. return false;
  8799. }
  8800. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8801. struct intel_load_detect_pipe *old,
  8802. struct drm_modeset_acquire_ctx *ctx)
  8803. {
  8804. struct drm_device *dev = connector->dev;
  8805. struct intel_encoder *intel_encoder =
  8806. intel_attached_encoder(connector);
  8807. struct drm_encoder *encoder = &intel_encoder->base;
  8808. struct drm_crtc *crtc = encoder->crtc;
  8809. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8810. struct drm_atomic_state *state;
  8811. struct drm_connector_state *connector_state;
  8812. struct intel_crtc_state *crtc_state;
  8813. int ret;
  8814. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8815. connector->base.id, connector->name,
  8816. encoder->base.id, encoder->name);
  8817. if (old->load_detect_temp) {
  8818. state = drm_atomic_state_alloc(dev);
  8819. if (!state)
  8820. goto fail;
  8821. state->acquire_ctx = ctx;
  8822. connector_state = drm_atomic_get_connector_state(state, connector);
  8823. if (IS_ERR(connector_state))
  8824. goto fail;
  8825. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8826. if (IS_ERR(crtc_state))
  8827. goto fail;
  8828. to_intel_connector(connector)->new_encoder = NULL;
  8829. intel_encoder->new_crtc = NULL;
  8830. intel_crtc->new_enabled = false;
  8831. connector_state->best_encoder = NULL;
  8832. connector_state->crtc = NULL;
  8833. crtc_state->base.enable = crtc_state->base.active = false;
  8834. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8835. 0, 0);
  8836. if (ret)
  8837. goto fail;
  8838. ret = intel_set_mode(state);
  8839. if (ret)
  8840. goto fail;
  8841. if (old->release_fb) {
  8842. drm_framebuffer_unregister_private(old->release_fb);
  8843. drm_framebuffer_unreference(old->release_fb);
  8844. }
  8845. return;
  8846. }
  8847. /* Switch crtc and encoder back off if necessary */
  8848. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8849. connector->funcs->dpms(connector, old->dpms_mode);
  8850. return;
  8851. fail:
  8852. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8853. drm_atomic_state_free(state);
  8854. }
  8855. static int i9xx_pll_refclk(struct drm_device *dev,
  8856. const struct intel_crtc_state *pipe_config)
  8857. {
  8858. struct drm_i915_private *dev_priv = dev->dev_private;
  8859. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8860. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8861. return dev_priv->vbt.lvds_ssc_freq;
  8862. else if (HAS_PCH_SPLIT(dev))
  8863. return 120000;
  8864. else if (!IS_GEN2(dev))
  8865. return 96000;
  8866. else
  8867. return 48000;
  8868. }
  8869. /* Returns the clock of the currently programmed mode of the given pipe. */
  8870. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8871. struct intel_crtc_state *pipe_config)
  8872. {
  8873. struct drm_device *dev = crtc->base.dev;
  8874. struct drm_i915_private *dev_priv = dev->dev_private;
  8875. int pipe = pipe_config->cpu_transcoder;
  8876. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8877. u32 fp;
  8878. intel_clock_t clock;
  8879. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8880. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8881. fp = pipe_config->dpll_hw_state.fp0;
  8882. else
  8883. fp = pipe_config->dpll_hw_state.fp1;
  8884. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8885. if (IS_PINEVIEW(dev)) {
  8886. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8887. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8888. } else {
  8889. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8890. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8891. }
  8892. if (!IS_GEN2(dev)) {
  8893. if (IS_PINEVIEW(dev))
  8894. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8895. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8896. else
  8897. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8898. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8899. switch (dpll & DPLL_MODE_MASK) {
  8900. case DPLLB_MODE_DAC_SERIAL:
  8901. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8902. 5 : 10;
  8903. break;
  8904. case DPLLB_MODE_LVDS:
  8905. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8906. 7 : 14;
  8907. break;
  8908. default:
  8909. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8910. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8911. return;
  8912. }
  8913. if (IS_PINEVIEW(dev))
  8914. pineview_clock(refclk, &clock);
  8915. else
  8916. i9xx_clock(refclk, &clock);
  8917. } else {
  8918. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8919. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8920. if (is_lvds) {
  8921. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8922. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8923. if (lvds & LVDS_CLKB_POWER_UP)
  8924. clock.p2 = 7;
  8925. else
  8926. clock.p2 = 14;
  8927. } else {
  8928. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8929. clock.p1 = 2;
  8930. else {
  8931. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8932. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8933. }
  8934. if (dpll & PLL_P2_DIVIDE_BY_4)
  8935. clock.p2 = 4;
  8936. else
  8937. clock.p2 = 2;
  8938. }
  8939. i9xx_clock(refclk, &clock);
  8940. }
  8941. /*
  8942. * This value includes pixel_multiplier. We will use
  8943. * port_clock to compute adjusted_mode.crtc_clock in the
  8944. * encoder's get_config() function.
  8945. */
  8946. pipe_config->port_clock = clock.dot;
  8947. }
  8948. int intel_dotclock_calculate(int link_freq,
  8949. const struct intel_link_m_n *m_n)
  8950. {
  8951. /*
  8952. * The calculation for the data clock is:
  8953. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8954. * But we want to avoid losing precison if possible, so:
  8955. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8956. *
  8957. * and the link clock is simpler:
  8958. * link_clock = (m * link_clock) / n
  8959. */
  8960. if (!m_n->link_n)
  8961. return 0;
  8962. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8963. }
  8964. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8965. struct intel_crtc_state *pipe_config)
  8966. {
  8967. struct drm_device *dev = crtc->base.dev;
  8968. /* read out port_clock from the DPLL */
  8969. i9xx_crtc_clock_get(crtc, pipe_config);
  8970. /*
  8971. * This value does not include pixel_multiplier.
  8972. * We will check that port_clock and adjusted_mode.crtc_clock
  8973. * agree once we know their relationship in the encoder's
  8974. * get_config() function.
  8975. */
  8976. pipe_config->base.adjusted_mode.crtc_clock =
  8977. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8978. &pipe_config->fdi_m_n);
  8979. }
  8980. /** Returns the currently programmed mode of the given pipe. */
  8981. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8982. struct drm_crtc *crtc)
  8983. {
  8984. struct drm_i915_private *dev_priv = dev->dev_private;
  8985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8986. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8987. struct drm_display_mode *mode;
  8988. struct intel_crtc_state pipe_config;
  8989. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8990. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8991. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8992. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8993. enum pipe pipe = intel_crtc->pipe;
  8994. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8995. if (!mode)
  8996. return NULL;
  8997. /*
  8998. * Construct a pipe_config sufficient for getting the clock info
  8999. * back out of crtc_clock_get.
  9000. *
  9001. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9002. * to use a real value here instead.
  9003. */
  9004. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  9005. pipe_config.pixel_multiplier = 1;
  9006. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9007. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9008. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9009. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  9010. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  9011. mode->hdisplay = (htot & 0xffff) + 1;
  9012. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9013. mode->hsync_start = (hsync & 0xffff) + 1;
  9014. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9015. mode->vdisplay = (vtot & 0xffff) + 1;
  9016. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9017. mode->vsync_start = (vsync & 0xffff) + 1;
  9018. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9019. drm_mode_set_name(mode);
  9020. return mode;
  9021. }
  9022. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  9023. {
  9024. struct drm_device *dev = crtc->dev;
  9025. struct drm_i915_private *dev_priv = dev->dev_private;
  9026. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9027. if (!HAS_GMCH_DISPLAY(dev))
  9028. return;
  9029. if (!dev_priv->lvds_downclock_avail)
  9030. return;
  9031. /*
  9032. * Since this is called by a timer, we should never get here in
  9033. * the manual case.
  9034. */
  9035. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  9036. int pipe = intel_crtc->pipe;
  9037. int dpll_reg = DPLL(pipe);
  9038. int dpll;
  9039. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  9040. assert_panel_unlocked(dev_priv, pipe);
  9041. dpll = I915_READ(dpll_reg);
  9042. dpll |= DISPLAY_RATE_SELECT_FPA1;
  9043. I915_WRITE(dpll_reg, dpll);
  9044. intel_wait_for_vblank(dev, pipe);
  9045. dpll = I915_READ(dpll_reg);
  9046. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  9047. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  9048. }
  9049. }
  9050. void intel_mark_busy(struct drm_device *dev)
  9051. {
  9052. struct drm_i915_private *dev_priv = dev->dev_private;
  9053. if (dev_priv->mm.busy)
  9054. return;
  9055. intel_runtime_pm_get(dev_priv);
  9056. i915_update_gfx_val(dev_priv);
  9057. if (INTEL_INFO(dev)->gen >= 6)
  9058. gen6_rps_busy(dev_priv);
  9059. dev_priv->mm.busy = true;
  9060. }
  9061. void intel_mark_idle(struct drm_device *dev)
  9062. {
  9063. struct drm_i915_private *dev_priv = dev->dev_private;
  9064. struct drm_crtc *crtc;
  9065. if (!dev_priv->mm.busy)
  9066. return;
  9067. dev_priv->mm.busy = false;
  9068. for_each_crtc(dev, crtc) {
  9069. if (!crtc->primary->fb)
  9070. continue;
  9071. intel_decrease_pllclock(crtc);
  9072. }
  9073. if (INTEL_INFO(dev)->gen >= 6)
  9074. gen6_rps_idle(dev->dev_private);
  9075. intel_runtime_pm_put(dev_priv);
  9076. }
  9077. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9078. {
  9079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9080. struct drm_device *dev = crtc->dev;
  9081. struct intel_unpin_work *work;
  9082. spin_lock_irq(&dev->event_lock);
  9083. work = intel_crtc->unpin_work;
  9084. intel_crtc->unpin_work = NULL;
  9085. spin_unlock_irq(&dev->event_lock);
  9086. if (work) {
  9087. cancel_work_sync(&work->work);
  9088. kfree(work);
  9089. }
  9090. drm_crtc_cleanup(crtc);
  9091. kfree(intel_crtc);
  9092. }
  9093. static void intel_unpin_work_fn(struct work_struct *__work)
  9094. {
  9095. struct intel_unpin_work *work =
  9096. container_of(__work, struct intel_unpin_work, work);
  9097. struct drm_device *dev = work->crtc->dev;
  9098. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  9099. mutex_lock(&dev->struct_mutex);
  9100. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  9101. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9102. intel_fbc_update(dev);
  9103. if (work->flip_queued_req)
  9104. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9105. mutex_unlock(&dev->struct_mutex);
  9106. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9107. drm_framebuffer_unreference(work->old_fb);
  9108. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  9109. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  9110. kfree(work);
  9111. }
  9112. static void do_intel_finish_page_flip(struct drm_device *dev,
  9113. struct drm_crtc *crtc)
  9114. {
  9115. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9116. struct intel_unpin_work *work;
  9117. unsigned long flags;
  9118. /* Ignore early vblank irqs */
  9119. if (intel_crtc == NULL)
  9120. return;
  9121. /*
  9122. * This is called both by irq handlers and the reset code (to complete
  9123. * lost pageflips) so needs the full irqsave spinlocks.
  9124. */
  9125. spin_lock_irqsave(&dev->event_lock, flags);
  9126. work = intel_crtc->unpin_work;
  9127. /* Ensure we don't miss a work->pending update ... */
  9128. smp_rmb();
  9129. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9130. spin_unlock_irqrestore(&dev->event_lock, flags);
  9131. return;
  9132. }
  9133. page_flip_completed(intel_crtc);
  9134. spin_unlock_irqrestore(&dev->event_lock, flags);
  9135. }
  9136. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9137. {
  9138. struct drm_i915_private *dev_priv = dev->dev_private;
  9139. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9140. do_intel_finish_page_flip(dev, crtc);
  9141. }
  9142. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9143. {
  9144. struct drm_i915_private *dev_priv = dev->dev_private;
  9145. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9146. do_intel_finish_page_flip(dev, crtc);
  9147. }
  9148. /* Is 'a' after or equal to 'b'? */
  9149. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9150. {
  9151. return !((a - b) & 0x80000000);
  9152. }
  9153. static bool page_flip_finished(struct intel_crtc *crtc)
  9154. {
  9155. struct drm_device *dev = crtc->base.dev;
  9156. struct drm_i915_private *dev_priv = dev->dev_private;
  9157. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9158. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9159. return true;
  9160. /*
  9161. * The relevant registers doen't exist on pre-ctg.
  9162. * As the flip done interrupt doesn't trigger for mmio
  9163. * flips on gmch platforms, a flip count check isn't
  9164. * really needed there. But since ctg has the registers,
  9165. * include it in the check anyway.
  9166. */
  9167. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9168. return true;
  9169. /*
  9170. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9171. * used the same base address. In that case the mmio flip might
  9172. * have completed, but the CS hasn't even executed the flip yet.
  9173. *
  9174. * A flip count check isn't enough as the CS might have updated
  9175. * the base address just after start of vblank, but before we
  9176. * managed to process the interrupt. This means we'd complete the
  9177. * CS flip too soon.
  9178. *
  9179. * Combining both checks should get us a good enough result. It may
  9180. * still happen that the CS flip has been executed, but has not
  9181. * yet actually completed. But in case the base address is the same
  9182. * anyway, we don't really care.
  9183. */
  9184. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9185. crtc->unpin_work->gtt_offset &&
  9186. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9187. crtc->unpin_work->flip_count);
  9188. }
  9189. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9190. {
  9191. struct drm_i915_private *dev_priv = dev->dev_private;
  9192. struct intel_crtc *intel_crtc =
  9193. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9194. unsigned long flags;
  9195. /*
  9196. * This is called both by irq handlers and the reset code (to complete
  9197. * lost pageflips) so needs the full irqsave spinlocks.
  9198. *
  9199. * NB: An MMIO update of the plane base pointer will also
  9200. * generate a page-flip completion irq, i.e. every modeset
  9201. * is also accompanied by a spurious intel_prepare_page_flip().
  9202. */
  9203. spin_lock_irqsave(&dev->event_lock, flags);
  9204. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9205. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9206. spin_unlock_irqrestore(&dev->event_lock, flags);
  9207. }
  9208. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9209. {
  9210. /* Ensure that the work item is consistent when activating it ... */
  9211. smp_wmb();
  9212. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9213. /* and that it is marked active as soon as the irq could fire. */
  9214. smp_wmb();
  9215. }
  9216. static int intel_gen2_queue_flip(struct drm_device *dev,
  9217. struct drm_crtc *crtc,
  9218. struct drm_framebuffer *fb,
  9219. struct drm_i915_gem_object *obj,
  9220. struct intel_engine_cs *ring,
  9221. uint32_t flags)
  9222. {
  9223. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9224. u32 flip_mask;
  9225. int ret;
  9226. ret = intel_ring_begin(ring, 6);
  9227. if (ret)
  9228. return ret;
  9229. /* Can't queue multiple flips, so wait for the previous
  9230. * one to finish before executing the next.
  9231. */
  9232. if (intel_crtc->plane)
  9233. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9234. else
  9235. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9236. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9237. intel_ring_emit(ring, MI_NOOP);
  9238. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9239. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9240. intel_ring_emit(ring, fb->pitches[0]);
  9241. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9242. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9243. intel_mark_page_flip_active(intel_crtc);
  9244. __intel_ring_advance(ring);
  9245. return 0;
  9246. }
  9247. static int intel_gen3_queue_flip(struct drm_device *dev,
  9248. struct drm_crtc *crtc,
  9249. struct drm_framebuffer *fb,
  9250. struct drm_i915_gem_object *obj,
  9251. struct intel_engine_cs *ring,
  9252. uint32_t flags)
  9253. {
  9254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9255. u32 flip_mask;
  9256. int ret;
  9257. ret = intel_ring_begin(ring, 6);
  9258. if (ret)
  9259. return ret;
  9260. if (intel_crtc->plane)
  9261. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9262. else
  9263. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9264. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9265. intel_ring_emit(ring, MI_NOOP);
  9266. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9267. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9268. intel_ring_emit(ring, fb->pitches[0]);
  9269. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9270. intel_ring_emit(ring, MI_NOOP);
  9271. intel_mark_page_flip_active(intel_crtc);
  9272. __intel_ring_advance(ring);
  9273. return 0;
  9274. }
  9275. static int intel_gen4_queue_flip(struct drm_device *dev,
  9276. struct drm_crtc *crtc,
  9277. struct drm_framebuffer *fb,
  9278. struct drm_i915_gem_object *obj,
  9279. struct intel_engine_cs *ring,
  9280. uint32_t flags)
  9281. {
  9282. struct drm_i915_private *dev_priv = dev->dev_private;
  9283. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9284. uint32_t pf, pipesrc;
  9285. int ret;
  9286. ret = intel_ring_begin(ring, 4);
  9287. if (ret)
  9288. return ret;
  9289. /* i965+ uses the linear or tiled offsets from the
  9290. * Display Registers (which do not change across a page-flip)
  9291. * so we need only reprogram the base address.
  9292. */
  9293. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9294. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9295. intel_ring_emit(ring, fb->pitches[0]);
  9296. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9297. obj->tiling_mode);
  9298. /* XXX Enabling the panel-fitter across page-flip is so far
  9299. * untested on non-native modes, so ignore it for now.
  9300. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9301. */
  9302. pf = 0;
  9303. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9304. intel_ring_emit(ring, pf | pipesrc);
  9305. intel_mark_page_flip_active(intel_crtc);
  9306. __intel_ring_advance(ring);
  9307. return 0;
  9308. }
  9309. static int intel_gen6_queue_flip(struct drm_device *dev,
  9310. struct drm_crtc *crtc,
  9311. struct drm_framebuffer *fb,
  9312. struct drm_i915_gem_object *obj,
  9313. struct intel_engine_cs *ring,
  9314. uint32_t flags)
  9315. {
  9316. struct drm_i915_private *dev_priv = dev->dev_private;
  9317. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9318. uint32_t pf, pipesrc;
  9319. int ret;
  9320. ret = intel_ring_begin(ring, 4);
  9321. if (ret)
  9322. return ret;
  9323. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9324. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9325. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9326. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9327. /* Contrary to the suggestions in the documentation,
  9328. * "Enable Panel Fitter" does not seem to be required when page
  9329. * flipping with a non-native mode, and worse causes a normal
  9330. * modeset to fail.
  9331. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9332. */
  9333. pf = 0;
  9334. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9335. intel_ring_emit(ring, pf | pipesrc);
  9336. intel_mark_page_flip_active(intel_crtc);
  9337. __intel_ring_advance(ring);
  9338. return 0;
  9339. }
  9340. static int intel_gen7_queue_flip(struct drm_device *dev,
  9341. struct drm_crtc *crtc,
  9342. struct drm_framebuffer *fb,
  9343. struct drm_i915_gem_object *obj,
  9344. struct intel_engine_cs *ring,
  9345. uint32_t flags)
  9346. {
  9347. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9348. uint32_t plane_bit = 0;
  9349. int len, ret;
  9350. switch (intel_crtc->plane) {
  9351. case PLANE_A:
  9352. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9353. break;
  9354. case PLANE_B:
  9355. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9356. break;
  9357. case PLANE_C:
  9358. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9359. break;
  9360. default:
  9361. WARN_ONCE(1, "unknown plane in flip command\n");
  9362. return -ENODEV;
  9363. }
  9364. len = 4;
  9365. if (ring->id == RCS) {
  9366. len += 6;
  9367. /*
  9368. * On Gen 8, SRM is now taking an extra dword to accommodate
  9369. * 48bits addresses, and we need a NOOP for the batch size to
  9370. * stay even.
  9371. */
  9372. if (IS_GEN8(dev))
  9373. len += 2;
  9374. }
  9375. /*
  9376. * BSpec MI_DISPLAY_FLIP for IVB:
  9377. * "The full packet must be contained within the same cache line."
  9378. *
  9379. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9380. * cacheline, if we ever start emitting more commands before
  9381. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9382. * then do the cacheline alignment, and finally emit the
  9383. * MI_DISPLAY_FLIP.
  9384. */
  9385. ret = intel_ring_cacheline_align(ring);
  9386. if (ret)
  9387. return ret;
  9388. ret = intel_ring_begin(ring, len);
  9389. if (ret)
  9390. return ret;
  9391. /* Unmask the flip-done completion message. Note that the bspec says that
  9392. * we should do this for both the BCS and RCS, and that we must not unmask
  9393. * more than one flip event at any time (or ensure that one flip message
  9394. * can be sent by waiting for flip-done prior to queueing new flips).
  9395. * Experimentation says that BCS works despite DERRMR masking all
  9396. * flip-done completion events and that unmasking all planes at once
  9397. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9398. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9399. */
  9400. if (ring->id == RCS) {
  9401. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9402. intel_ring_emit(ring, DERRMR);
  9403. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9404. DERRMR_PIPEB_PRI_FLIP_DONE |
  9405. DERRMR_PIPEC_PRI_FLIP_DONE));
  9406. if (IS_GEN8(dev))
  9407. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9408. MI_SRM_LRM_GLOBAL_GTT);
  9409. else
  9410. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9411. MI_SRM_LRM_GLOBAL_GTT);
  9412. intel_ring_emit(ring, DERRMR);
  9413. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9414. if (IS_GEN8(dev)) {
  9415. intel_ring_emit(ring, 0);
  9416. intel_ring_emit(ring, MI_NOOP);
  9417. }
  9418. }
  9419. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9420. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9421. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9422. intel_ring_emit(ring, (MI_NOOP));
  9423. intel_mark_page_flip_active(intel_crtc);
  9424. __intel_ring_advance(ring);
  9425. return 0;
  9426. }
  9427. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9428. struct drm_i915_gem_object *obj)
  9429. {
  9430. /*
  9431. * This is not being used for older platforms, because
  9432. * non-availability of flip done interrupt forces us to use
  9433. * CS flips. Older platforms derive flip done using some clever
  9434. * tricks involving the flip_pending status bits and vblank irqs.
  9435. * So using MMIO flips there would disrupt this mechanism.
  9436. */
  9437. if (ring == NULL)
  9438. return true;
  9439. if (INTEL_INFO(ring->dev)->gen < 5)
  9440. return false;
  9441. if (i915.use_mmio_flip < 0)
  9442. return false;
  9443. else if (i915.use_mmio_flip > 0)
  9444. return true;
  9445. else if (i915.enable_execlists)
  9446. return true;
  9447. else
  9448. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9449. }
  9450. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9451. {
  9452. struct drm_device *dev = intel_crtc->base.dev;
  9453. struct drm_i915_private *dev_priv = dev->dev_private;
  9454. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9455. const enum pipe pipe = intel_crtc->pipe;
  9456. u32 ctl, stride;
  9457. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9458. ctl &= ~PLANE_CTL_TILED_MASK;
  9459. switch (fb->modifier[0]) {
  9460. case DRM_FORMAT_MOD_NONE:
  9461. break;
  9462. case I915_FORMAT_MOD_X_TILED:
  9463. ctl |= PLANE_CTL_TILED_X;
  9464. break;
  9465. case I915_FORMAT_MOD_Y_TILED:
  9466. ctl |= PLANE_CTL_TILED_Y;
  9467. break;
  9468. case I915_FORMAT_MOD_Yf_TILED:
  9469. ctl |= PLANE_CTL_TILED_YF;
  9470. break;
  9471. default:
  9472. MISSING_CASE(fb->modifier[0]);
  9473. }
  9474. /*
  9475. * The stride is either expressed as a multiple of 64 bytes chunks for
  9476. * linear buffers or in number of tiles for tiled buffers.
  9477. */
  9478. stride = fb->pitches[0] /
  9479. intel_fb_stride_alignment(dev, fb->modifier[0],
  9480. fb->pixel_format);
  9481. /*
  9482. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9483. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9484. */
  9485. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9486. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9487. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9488. POSTING_READ(PLANE_SURF(pipe, 0));
  9489. }
  9490. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9491. {
  9492. struct drm_device *dev = intel_crtc->base.dev;
  9493. struct drm_i915_private *dev_priv = dev->dev_private;
  9494. struct intel_framebuffer *intel_fb =
  9495. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9496. struct drm_i915_gem_object *obj = intel_fb->obj;
  9497. u32 dspcntr;
  9498. u32 reg;
  9499. reg = DSPCNTR(intel_crtc->plane);
  9500. dspcntr = I915_READ(reg);
  9501. if (obj->tiling_mode != I915_TILING_NONE)
  9502. dspcntr |= DISPPLANE_TILED;
  9503. else
  9504. dspcntr &= ~DISPPLANE_TILED;
  9505. I915_WRITE(reg, dspcntr);
  9506. I915_WRITE(DSPSURF(intel_crtc->plane),
  9507. intel_crtc->unpin_work->gtt_offset);
  9508. POSTING_READ(DSPSURF(intel_crtc->plane));
  9509. }
  9510. /*
  9511. * XXX: This is the temporary way to update the plane registers until we get
  9512. * around to using the usual plane update functions for MMIO flips
  9513. */
  9514. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9515. {
  9516. struct drm_device *dev = intel_crtc->base.dev;
  9517. bool atomic_update;
  9518. u32 start_vbl_count;
  9519. intel_mark_page_flip_active(intel_crtc);
  9520. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9521. if (INTEL_INFO(dev)->gen >= 9)
  9522. skl_do_mmio_flip(intel_crtc);
  9523. else
  9524. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9525. ilk_do_mmio_flip(intel_crtc);
  9526. if (atomic_update)
  9527. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9528. }
  9529. static void intel_mmio_flip_work_func(struct work_struct *work)
  9530. {
  9531. struct intel_mmio_flip *mmio_flip =
  9532. container_of(work, struct intel_mmio_flip, work);
  9533. if (mmio_flip->req)
  9534. WARN_ON(__i915_wait_request(mmio_flip->req,
  9535. mmio_flip->crtc->reset_counter,
  9536. false, NULL,
  9537. &mmio_flip->i915->rps.mmioflips));
  9538. intel_do_mmio_flip(mmio_flip->crtc);
  9539. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9540. kfree(mmio_flip);
  9541. }
  9542. static int intel_queue_mmio_flip(struct drm_device *dev,
  9543. struct drm_crtc *crtc,
  9544. struct drm_framebuffer *fb,
  9545. struct drm_i915_gem_object *obj,
  9546. struct intel_engine_cs *ring,
  9547. uint32_t flags)
  9548. {
  9549. struct intel_mmio_flip *mmio_flip;
  9550. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9551. if (mmio_flip == NULL)
  9552. return -ENOMEM;
  9553. mmio_flip->i915 = to_i915(dev);
  9554. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9555. mmio_flip->crtc = to_intel_crtc(crtc);
  9556. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9557. schedule_work(&mmio_flip->work);
  9558. return 0;
  9559. }
  9560. static int intel_default_queue_flip(struct drm_device *dev,
  9561. struct drm_crtc *crtc,
  9562. struct drm_framebuffer *fb,
  9563. struct drm_i915_gem_object *obj,
  9564. struct intel_engine_cs *ring,
  9565. uint32_t flags)
  9566. {
  9567. return -ENODEV;
  9568. }
  9569. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9570. struct drm_crtc *crtc)
  9571. {
  9572. struct drm_i915_private *dev_priv = dev->dev_private;
  9573. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9574. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9575. u32 addr;
  9576. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9577. return true;
  9578. if (!work->enable_stall_check)
  9579. return false;
  9580. if (work->flip_ready_vblank == 0) {
  9581. if (work->flip_queued_req &&
  9582. !i915_gem_request_completed(work->flip_queued_req, true))
  9583. return false;
  9584. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9585. }
  9586. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9587. return false;
  9588. /* Potential stall - if we see that the flip has happened,
  9589. * assume a missed interrupt. */
  9590. if (INTEL_INFO(dev)->gen >= 4)
  9591. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9592. else
  9593. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9594. /* There is a potential issue here with a false positive after a flip
  9595. * to the same address. We could address this by checking for a
  9596. * non-incrementing frame counter.
  9597. */
  9598. return addr == work->gtt_offset;
  9599. }
  9600. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9601. {
  9602. struct drm_i915_private *dev_priv = dev->dev_private;
  9603. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9604. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9605. struct intel_unpin_work *work;
  9606. WARN_ON(!in_interrupt());
  9607. if (crtc == NULL)
  9608. return;
  9609. spin_lock(&dev->event_lock);
  9610. work = intel_crtc->unpin_work;
  9611. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9612. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9613. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9614. page_flip_completed(intel_crtc);
  9615. work = NULL;
  9616. }
  9617. if (work != NULL &&
  9618. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9619. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9620. spin_unlock(&dev->event_lock);
  9621. }
  9622. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9623. struct drm_framebuffer *fb,
  9624. struct drm_pending_vblank_event *event,
  9625. uint32_t page_flip_flags)
  9626. {
  9627. struct drm_device *dev = crtc->dev;
  9628. struct drm_i915_private *dev_priv = dev->dev_private;
  9629. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9630. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9631. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9632. struct drm_plane *primary = crtc->primary;
  9633. enum pipe pipe = intel_crtc->pipe;
  9634. struct intel_unpin_work *work;
  9635. struct intel_engine_cs *ring;
  9636. bool mmio_flip;
  9637. int ret;
  9638. /*
  9639. * drm_mode_page_flip_ioctl() should already catch this, but double
  9640. * check to be safe. In the future we may enable pageflipping from
  9641. * a disabled primary plane.
  9642. */
  9643. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9644. return -EBUSY;
  9645. /* Can't change pixel format via MI display flips. */
  9646. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9647. return -EINVAL;
  9648. /*
  9649. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9650. * Note that pitch changes could also affect these register.
  9651. */
  9652. if (INTEL_INFO(dev)->gen > 3 &&
  9653. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9654. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9655. return -EINVAL;
  9656. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9657. goto out_hang;
  9658. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9659. if (work == NULL)
  9660. return -ENOMEM;
  9661. work->event = event;
  9662. work->crtc = crtc;
  9663. work->old_fb = old_fb;
  9664. INIT_WORK(&work->work, intel_unpin_work_fn);
  9665. ret = drm_crtc_vblank_get(crtc);
  9666. if (ret)
  9667. goto free_work;
  9668. /* We borrow the event spin lock for protecting unpin_work */
  9669. spin_lock_irq(&dev->event_lock);
  9670. if (intel_crtc->unpin_work) {
  9671. /* Before declaring the flip queue wedged, check if
  9672. * the hardware completed the operation behind our backs.
  9673. */
  9674. if (__intel_pageflip_stall_check(dev, crtc)) {
  9675. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9676. page_flip_completed(intel_crtc);
  9677. } else {
  9678. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9679. spin_unlock_irq(&dev->event_lock);
  9680. drm_crtc_vblank_put(crtc);
  9681. kfree(work);
  9682. return -EBUSY;
  9683. }
  9684. }
  9685. intel_crtc->unpin_work = work;
  9686. spin_unlock_irq(&dev->event_lock);
  9687. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9688. flush_workqueue(dev_priv->wq);
  9689. /* Reference the objects for the scheduled work. */
  9690. drm_framebuffer_reference(work->old_fb);
  9691. drm_gem_object_reference(&obj->base);
  9692. crtc->primary->fb = fb;
  9693. update_state_fb(crtc->primary);
  9694. work->pending_flip_obj = obj;
  9695. ret = i915_mutex_lock_interruptible(dev);
  9696. if (ret)
  9697. goto cleanup;
  9698. atomic_inc(&intel_crtc->unpin_work_count);
  9699. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9700. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9701. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9702. if (IS_VALLEYVIEW(dev)) {
  9703. ring = &dev_priv->ring[BCS];
  9704. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9705. /* vlv: DISPLAY_FLIP fails to change tiling */
  9706. ring = NULL;
  9707. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9708. ring = &dev_priv->ring[BCS];
  9709. } else if (INTEL_INFO(dev)->gen >= 7) {
  9710. ring = i915_gem_request_get_ring(obj->last_write_req);
  9711. if (ring == NULL || ring->id != RCS)
  9712. ring = &dev_priv->ring[BCS];
  9713. } else {
  9714. ring = &dev_priv->ring[RCS];
  9715. }
  9716. mmio_flip = use_mmio_flip(ring, obj);
  9717. /* When using CS flips, we want to emit semaphores between rings.
  9718. * However, when using mmio flips we will create a task to do the
  9719. * synchronisation, so all we want here is to pin the framebuffer
  9720. * into the display plane and skip any waits.
  9721. */
  9722. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9723. crtc->primary->state,
  9724. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9725. if (ret)
  9726. goto cleanup_pending;
  9727. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9728. + intel_crtc->dspaddr_offset;
  9729. if (mmio_flip) {
  9730. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9731. page_flip_flags);
  9732. if (ret)
  9733. goto cleanup_unpin;
  9734. i915_gem_request_assign(&work->flip_queued_req,
  9735. obj->last_write_req);
  9736. } else {
  9737. if (obj->last_write_req) {
  9738. ret = i915_gem_check_olr(obj->last_write_req);
  9739. if (ret)
  9740. goto cleanup_unpin;
  9741. }
  9742. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9743. page_flip_flags);
  9744. if (ret)
  9745. goto cleanup_unpin;
  9746. i915_gem_request_assign(&work->flip_queued_req,
  9747. intel_ring_get_request(ring));
  9748. }
  9749. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9750. work->enable_stall_check = true;
  9751. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9752. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9753. intel_fbc_disable(dev);
  9754. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9755. mutex_unlock(&dev->struct_mutex);
  9756. trace_i915_flip_request(intel_crtc->plane, obj);
  9757. return 0;
  9758. cleanup_unpin:
  9759. intel_unpin_fb_obj(fb, crtc->primary->state);
  9760. cleanup_pending:
  9761. atomic_dec(&intel_crtc->unpin_work_count);
  9762. mutex_unlock(&dev->struct_mutex);
  9763. cleanup:
  9764. crtc->primary->fb = old_fb;
  9765. update_state_fb(crtc->primary);
  9766. drm_gem_object_unreference_unlocked(&obj->base);
  9767. drm_framebuffer_unreference(work->old_fb);
  9768. spin_lock_irq(&dev->event_lock);
  9769. intel_crtc->unpin_work = NULL;
  9770. spin_unlock_irq(&dev->event_lock);
  9771. drm_crtc_vblank_put(crtc);
  9772. free_work:
  9773. kfree(work);
  9774. if (ret == -EIO) {
  9775. struct drm_atomic_state *state;
  9776. struct drm_plane_state *plane_state;
  9777. out_hang:
  9778. state = drm_atomic_state_alloc(dev);
  9779. if (!state)
  9780. return -ENOMEM;
  9781. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9782. retry:
  9783. plane_state = drm_atomic_get_plane_state(state, primary);
  9784. ret = PTR_ERR_OR_ZERO(plane_state);
  9785. if (!ret) {
  9786. drm_atomic_set_fb_for_plane(plane_state, fb);
  9787. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9788. if (!ret)
  9789. ret = drm_atomic_commit(state);
  9790. }
  9791. if (ret == -EDEADLK) {
  9792. drm_modeset_backoff(state->acquire_ctx);
  9793. drm_atomic_state_clear(state);
  9794. goto retry;
  9795. }
  9796. if (ret)
  9797. drm_atomic_state_free(state);
  9798. if (ret == 0 && event) {
  9799. spin_lock_irq(&dev->event_lock);
  9800. drm_send_vblank_event(dev, pipe, event);
  9801. spin_unlock_irq(&dev->event_lock);
  9802. }
  9803. }
  9804. return ret;
  9805. }
  9806. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9807. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9808. .load_lut = intel_crtc_load_lut,
  9809. .atomic_begin = intel_begin_crtc_commit,
  9810. .atomic_flush = intel_finish_crtc_commit,
  9811. };
  9812. /**
  9813. * intel_modeset_update_staged_output_state
  9814. *
  9815. * Updates the staged output configuration state, e.g. after we've read out the
  9816. * current hw state.
  9817. */
  9818. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9819. {
  9820. struct intel_crtc *crtc;
  9821. struct intel_encoder *encoder;
  9822. struct intel_connector *connector;
  9823. for_each_intel_connector(dev, connector) {
  9824. connector->new_encoder =
  9825. to_intel_encoder(connector->base.encoder);
  9826. }
  9827. for_each_intel_encoder(dev, encoder) {
  9828. encoder->new_crtc =
  9829. to_intel_crtc(encoder->base.crtc);
  9830. }
  9831. for_each_intel_crtc(dev, crtc) {
  9832. crtc->new_enabled = crtc->base.state->enable;
  9833. }
  9834. }
  9835. /* Transitional helper to copy current connector/encoder state to
  9836. * connector->state. This is needed so that code that is partially
  9837. * converted to atomic does the right thing.
  9838. */
  9839. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9840. {
  9841. struct intel_connector *connector;
  9842. for_each_intel_connector(dev, connector) {
  9843. if (connector->base.encoder) {
  9844. connector->base.state->best_encoder =
  9845. connector->base.encoder;
  9846. connector->base.state->crtc =
  9847. connector->base.encoder->crtc;
  9848. } else {
  9849. connector->base.state->best_encoder = NULL;
  9850. connector->base.state->crtc = NULL;
  9851. }
  9852. }
  9853. }
  9854. static void
  9855. connected_sink_compute_bpp(struct intel_connector *connector,
  9856. struct intel_crtc_state *pipe_config)
  9857. {
  9858. int bpp = pipe_config->pipe_bpp;
  9859. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9860. connector->base.base.id,
  9861. connector->base.name);
  9862. /* Don't use an invalid EDID bpc value */
  9863. if (connector->base.display_info.bpc &&
  9864. connector->base.display_info.bpc * 3 < bpp) {
  9865. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9866. bpp, connector->base.display_info.bpc*3);
  9867. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9868. }
  9869. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9870. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9871. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9872. bpp);
  9873. pipe_config->pipe_bpp = 24;
  9874. }
  9875. }
  9876. static int
  9877. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9878. struct intel_crtc_state *pipe_config)
  9879. {
  9880. struct drm_device *dev = crtc->base.dev;
  9881. struct drm_atomic_state *state;
  9882. struct drm_connector *connector;
  9883. struct drm_connector_state *connector_state;
  9884. int bpp, i;
  9885. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9886. bpp = 10*3;
  9887. else if (INTEL_INFO(dev)->gen >= 5)
  9888. bpp = 12*3;
  9889. else
  9890. bpp = 8*3;
  9891. pipe_config->pipe_bpp = bpp;
  9892. state = pipe_config->base.state;
  9893. /* Clamp display bpp to EDID value */
  9894. for_each_connector_in_state(state, connector, connector_state, i) {
  9895. if (connector_state->crtc != &crtc->base)
  9896. continue;
  9897. connected_sink_compute_bpp(to_intel_connector(connector),
  9898. pipe_config);
  9899. }
  9900. return bpp;
  9901. }
  9902. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9903. {
  9904. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9905. "type: 0x%x flags: 0x%x\n",
  9906. mode->crtc_clock,
  9907. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9908. mode->crtc_hsync_end, mode->crtc_htotal,
  9909. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9910. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9911. }
  9912. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9913. struct intel_crtc_state *pipe_config,
  9914. const char *context)
  9915. {
  9916. struct drm_device *dev = crtc->base.dev;
  9917. struct drm_plane *plane;
  9918. struct intel_plane *intel_plane;
  9919. struct intel_plane_state *state;
  9920. struct drm_framebuffer *fb;
  9921. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9922. context, pipe_config, pipe_name(crtc->pipe));
  9923. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9924. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9925. pipe_config->pipe_bpp, pipe_config->dither);
  9926. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9927. pipe_config->has_pch_encoder,
  9928. pipe_config->fdi_lanes,
  9929. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9930. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9931. pipe_config->fdi_m_n.tu);
  9932. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9933. pipe_config->has_dp_encoder,
  9934. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9935. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9936. pipe_config->dp_m_n.tu);
  9937. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9938. pipe_config->has_dp_encoder,
  9939. pipe_config->dp_m2_n2.gmch_m,
  9940. pipe_config->dp_m2_n2.gmch_n,
  9941. pipe_config->dp_m2_n2.link_m,
  9942. pipe_config->dp_m2_n2.link_n,
  9943. pipe_config->dp_m2_n2.tu);
  9944. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9945. pipe_config->has_audio,
  9946. pipe_config->has_infoframe);
  9947. DRM_DEBUG_KMS("requested mode:\n");
  9948. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9949. DRM_DEBUG_KMS("adjusted mode:\n");
  9950. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9951. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9952. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9953. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9954. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9955. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9956. crtc->num_scalers,
  9957. pipe_config->scaler_state.scaler_users,
  9958. pipe_config->scaler_state.scaler_id);
  9959. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9960. pipe_config->gmch_pfit.control,
  9961. pipe_config->gmch_pfit.pgm_ratios,
  9962. pipe_config->gmch_pfit.lvds_border_bits);
  9963. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9964. pipe_config->pch_pfit.pos,
  9965. pipe_config->pch_pfit.size,
  9966. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9967. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9968. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9969. if (IS_BROXTON(dev)) {
  9970. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  9971. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  9972. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  9973. pipe_config->ddi_pll_sel,
  9974. pipe_config->dpll_hw_state.ebb0,
  9975. pipe_config->dpll_hw_state.pll0,
  9976. pipe_config->dpll_hw_state.pll1,
  9977. pipe_config->dpll_hw_state.pll2,
  9978. pipe_config->dpll_hw_state.pll3,
  9979. pipe_config->dpll_hw_state.pll6,
  9980. pipe_config->dpll_hw_state.pll8,
  9981. pipe_config->dpll_hw_state.pcsdw12);
  9982. } else if (IS_SKYLAKE(dev)) {
  9983. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  9984. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  9985. pipe_config->ddi_pll_sel,
  9986. pipe_config->dpll_hw_state.ctrl1,
  9987. pipe_config->dpll_hw_state.cfgcr1,
  9988. pipe_config->dpll_hw_state.cfgcr2);
  9989. } else if (HAS_DDI(dev)) {
  9990. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  9991. pipe_config->ddi_pll_sel,
  9992. pipe_config->dpll_hw_state.wrpll);
  9993. } else {
  9994. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  9995. "fp0: 0x%x, fp1: 0x%x\n",
  9996. pipe_config->dpll_hw_state.dpll,
  9997. pipe_config->dpll_hw_state.dpll_md,
  9998. pipe_config->dpll_hw_state.fp0,
  9999. pipe_config->dpll_hw_state.fp1);
  10000. }
  10001. DRM_DEBUG_KMS("planes on this crtc\n");
  10002. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10003. intel_plane = to_intel_plane(plane);
  10004. if (intel_plane->pipe != crtc->pipe)
  10005. continue;
  10006. state = to_intel_plane_state(plane->state);
  10007. fb = state->base.fb;
  10008. if (!fb) {
  10009. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10010. "disabled, scaler_id = %d\n",
  10011. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10012. plane->base.id, intel_plane->pipe,
  10013. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10014. drm_plane_index(plane), state->scaler_id);
  10015. continue;
  10016. }
  10017. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10018. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10019. plane->base.id, intel_plane->pipe,
  10020. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10021. drm_plane_index(plane));
  10022. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10023. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10024. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10025. state->scaler_id,
  10026. state->src.x1 >> 16, state->src.y1 >> 16,
  10027. drm_rect_width(&state->src) >> 16,
  10028. drm_rect_height(&state->src) >> 16,
  10029. state->dst.x1, state->dst.y1,
  10030. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10031. }
  10032. }
  10033. static bool encoders_cloneable(const struct intel_encoder *a,
  10034. const struct intel_encoder *b)
  10035. {
  10036. /* masks could be asymmetric, so check both ways */
  10037. return a == b || (a->cloneable & (1 << b->type) &&
  10038. b->cloneable & (1 << a->type));
  10039. }
  10040. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10041. struct intel_crtc *crtc,
  10042. struct intel_encoder *encoder)
  10043. {
  10044. struct intel_encoder *source_encoder;
  10045. struct drm_connector *connector;
  10046. struct drm_connector_state *connector_state;
  10047. int i;
  10048. for_each_connector_in_state(state, connector, connector_state, i) {
  10049. if (connector_state->crtc != &crtc->base)
  10050. continue;
  10051. source_encoder =
  10052. to_intel_encoder(connector_state->best_encoder);
  10053. if (!encoders_cloneable(encoder, source_encoder))
  10054. return false;
  10055. }
  10056. return true;
  10057. }
  10058. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10059. struct intel_crtc *crtc)
  10060. {
  10061. struct intel_encoder *encoder;
  10062. struct drm_connector *connector;
  10063. struct drm_connector_state *connector_state;
  10064. int i;
  10065. for_each_connector_in_state(state, connector, connector_state, i) {
  10066. if (connector_state->crtc != &crtc->base)
  10067. continue;
  10068. encoder = to_intel_encoder(connector_state->best_encoder);
  10069. if (!check_single_encoder_cloning(state, crtc, encoder))
  10070. return false;
  10071. }
  10072. return true;
  10073. }
  10074. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10075. {
  10076. struct drm_device *dev = state->dev;
  10077. struct intel_encoder *encoder;
  10078. struct drm_connector *connector;
  10079. struct drm_connector_state *connector_state;
  10080. unsigned int used_ports = 0;
  10081. int i;
  10082. /*
  10083. * Walk the connector list instead of the encoder
  10084. * list to detect the problem on ddi platforms
  10085. * where there's just one encoder per digital port.
  10086. */
  10087. for_each_connector_in_state(state, connector, connector_state, i) {
  10088. if (!connector_state->best_encoder)
  10089. continue;
  10090. encoder = to_intel_encoder(connector_state->best_encoder);
  10091. WARN_ON(!connector_state->crtc);
  10092. switch (encoder->type) {
  10093. unsigned int port_mask;
  10094. case INTEL_OUTPUT_UNKNOWN:
  10095. if (WARN_ON(!HAS_DDI(dev)))
  10096. break;
  10097. case INTEL_OUTPUT_DISPLAYPORT:
  10098. case INTEL_OUTPUT_HDMI:
  10099. case INTEL_OUTPUT_EDP:
  10100. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10101. /* the same port mustn't appear more than once */
  10102. if (used_ports & port_mask)
  10103. return false;
  10104. used_ports |= port_mask;
  10105. default:
  10106. break;
  10107. }
  10108. }
  10109. return true;
  10110. }
  10111. static void
  10112. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10113. {
  10114. struct drm_crtc_state tmp_state;
  10115. struct intel_crtc_scaler_state scaler_state;
  10116. struct intel_dpll_hw_state dpll_hw_state;
  10117. enum intel_dpll_id shared_dpll;
  10118. uint32_t ddi_pll_sel;
  10119. /* FIXME: before the switch to atomic started, a new pipe_config was
  10120. * kzalloc'd. Code that depends on any field being zero should be
  10121. * fixed, so that the crtc_state can be safely duplicated. For now,
  10122. * only fields that are know to not cause problems are preserved. */
  10123. tmp_state = crtc_state->base;
  10124. scaler_state = crtc_state->scaler_state;
  10125. shared_dpll = crtc_state->shared_dpll;
  10126. dpll_hw_state = crtc_state->dpll_hw_state;
  10127. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10128. memset(crtc_state, 0, sizeof *crtc_state);
  10129. crtc_state->base = tmp_state;
  10130. crtc_state->scaler_state = scaler_state;
  10131. crtc_state->shared_dpll = shared_dpll;
  10132. crtc_state->dpll_hw_state = dpll_hw_state;
  10133. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10134. }
  10135. static int
  10136. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10137. struct drm_atomic_state *state)
  10138. {
  10139. struct drm_crtc_state *crtc_state;
  10140. struct intel_crtc_state *pipe_config;
  10141. struct intel_encoder *encoder;
  10142. struct drm_connector *connector;
  10143. struct drm_connector_state *connector_state;
  10144. int base_bpp, ret = -EINVAL;
  10145. int i;
  10146. bool retry = true;
  10147. if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
  10148. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10149. return -EINVAL;
  10150. }
  10151. if (!check_digital_port_conflicts(state)) {
  10152. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10153. return -EINVAL;
  10154. }
  10155. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10156. if (WARN_ON(!crtc_state))
  10157. return -EINVAL;
  10158. pipe_config = to_intel_crtc_state(crtc_state);
  10159. /*
  10160. * XXX: Add all connectors to make the crtc state match the encoders.
  10161. */
  10162. if (!needs_modeset(&pipe_config->base)) {
  10163. ret = drm_atomic_add_affected_connectors(state, crtc);
  10164. if (ret)
  10165. return ret;
  10166. }
  10167. clear_intel_crtc_state(pipe_config);
  10168. pipe_config->cpu_transcoder =
  10169. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10170. /*
  10171. * Sanitize sync polarity flags based on requested ones. If neither
  10172. * positive or negative polarity is requested, treat this as meaning
  10173. * negative polarity.
  10174. */
  10175. if (!(pipe_config->base.adjusted_mode.flags &
  10176. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10177. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10178. if (!(pipe_config->base.adjusted_mode.flags &
  10179. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10180. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10181. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10182. * plane pixel format and any sink constraints into account. Returns the
  10183. * source plane bpp so that dithering can be selected on mismatches
  10184. * after encoders and crtc also have had their say. */
  10185. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10186. pipe_config);
  10187. if (base_bpp < 0)
  10188. goto fail;
  10189. /*
  10190. * Determine the real pipe dimensions. Note that stereo modes can
  10191. * increase the actual pipe size due to the frame doubling and
  10192. * insertion of additional space for blanks between the frame. This
  10193. * is stored in the crtc timings. We use the requested mode to do this
  10194. * computation to clearly distinguish it from the adjusted mode, which
  10195. * can be changed by the connectors in the below retry loop.
  10196. */
  10197. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10198. &pipe_config->pipe_src_w,
  10199. &pipe_config->pipe_src_h);
  10200. encoder_retry:
  10201. /* Ensure the port clock defaults are reset when retrying. */
  10202. pipe_config->port_clock = 0;
  10203. pipe_config->pixel_multiplier = 1;
  10204. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10205. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10206. CRTC_STEREO_DOUBLE);
  10207. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10208. * adjust it according to limitations or connector properties, and also
  10209. * a chance to reject the mode entirely.
  10210. */
  10211. for_each_connector_in_state(state, connector, connector_state, i) {
  10212. if (connector_state->crtc != crtc)
  10213. continue;
  10214. encoder = to_intel_encoder(connector_state->best_encoder);
  10215. if (!(encoder->compute_config(encoder, pipe_config))) {
  10216. DRM_DEBUG_KMS("Encoder config failure\n");
  10217. goto fail;
  10218. }
  10219. }
  10220. /* Set default port clock if not overwritten by the encoder. Needs to be
  10221. * done afterwards in case the encoder adjusts the mode. */
  10222. if (!pipe_config->port_clock)
  10223. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10224. * pipe_config->pixel_multiplier;
  10225. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10226. if (ret < 0) {
  10227. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10228. goto fail;
  10229. }
  10230. if (ret == RETRY) {
  10231. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10232. ret = -EINVAL;
  10233. goto fail;
  10234. }
  10235. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10236. retry = false;
  10237. goto encoder_retry;
  10238. }
  10239. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10240. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10241. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10242. /* Check if we need to force a modeset */
  10243. if (pipe_config->has_audio !=
  10244. to_intel_crtc_state(crtc->state)->has_audio) {
  10245. pipe_config->base.mode_changed = true;
  10246. ret = drm_atomic_add_affected_planes(state, crtc);
  10247. }
  10248. /*
  10249. * Note we have an issue here with infoframes: current code
  10250. * only updates them on the full mode set path per hw
  10251. * requirements. So here we should be checking for any
  10252. * required changes and forcing a mode set.
  10253. */
  10254. fail:
  10255. return ret;
  10256. }
  10257. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10258. {
  10259. struct drm_encoder *encoder;
  10260. struct drm_device *dev = crtc->dev;
  10261. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10262. if (encoder->crtc == crtc)
  10263. return true;
  10264. return false;
  10265. }
  10266. static void
  10267. intel_modeset_update_state(struct drm_atomic_state *state)
  10268. {
  10269. struct drm_device *dev = state->dev;
  10270. struct intel_encoder *intel_encoder;
  10271. struct drm_crtc *crtc;
  10272. struct drm_crtc_state *crtc_state;
  10273. struct drm_connector *connector;
  10274. intel_shared_dpll_commit(state);
  10275. for_each_intel_encoder(dev, intel_encoder) {
  10276. if (!intel_encoder->base.crtc)
  10277. continue;
  10278. crtc = intel_encoder->base.crtc;
  10279. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10280. if (!crtc_state || !needs_modeset(crtc->state))
  10281. continue;
  10282. intel_encoder->connectors_active = false;
  10283. }
  10284. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10285. intel_modeset_update_staged_output_state(state->dev);
  10286. /* Double check state. */
  10287. for_each_crtc(dev, crtc) {
  10288. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10289. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10290. /* Update hwmode for vblank functions */
  10291. if (crtc->state->active)
  10292. crtc->hwmode = crtc->state->adjusted_mode;
  10293. else
  10294. crtc->hwmode.crtc_clock = 0;
  10295. }
  10296. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10297. if (!connector->encoder || !connector->encoder->crtc)
  10298. continue;
  10299. crtc = connector->encoder->crtc;
  10300. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10301. if (!crtc_state || !needs_modeset(crtc->state))
  10302. continue;
  10303. if (crtc->state->active) {
  10304. struct drm_property *dpms_property =
  10305. dev->mode_config.dpms_property;
  10306. connector->dpms = DRM_MODE_DPMS_ON;
  10307. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10308. intel_encoder = to_intel_encoder(connector->encoder);
  10309. intel_encoder->connectors_active = true;
  10310. } else
  10311. connector->dpms = DRM_MODE_DPMS_OFF;
  10312. }
  10313. }
  10314. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10315. {
  10316. int diff;
  10317. if (clock1 == clock2)
  10318. return true;
  10319. if (!clock1 || !clock2)
  10320. return false;
  10321. diff = abs(clock1 - clock2);
  10322. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10323. return true;
  10324. return false;
  10325. }
  10326. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10327. list_for_each_entry((intel_crtc), \
  10328. &(dev)->mode_config.crtc_list, \
  10329. base.head) \
  10330. if (mask & (1 <<(intel_crtc)->pipe))
  10331. static bool
  10332. intel_pipe_config_compare(struct drm_device *dev,
  10333. struct intel_crtc_state *current_config,
  10334. struct intel_crtc_state *pipe_config)
  10335. {
  10336. #define PIPE_CONF_CHECK_X(name) \
  10337. if (current_config->name != pipe_config->name) { \
  10338. DRM_ERROR("mismatch in " #name " " \
  10339. "(expected 0x%08x, found 0x%08x)\n", \
  10340. current_config->name, \
  10341. pipe_config->name); \
  10342. return false; \
  10343. }
  10344. #define PIPE_CONF_CHECK_I(name) \
  10345. if (current_config->name != pipe_config->name) { \
  10346. DRM_ERROR("mismatch in " #name " " \
  10347. "(expected %i, found %i)\n", \
  10348. current_config->name, \
  10349. pipe_config->name); \
  10350. return false; \
  10351. }
  10352. /* This is required for BDW+ where there is only one set of registers for
  10353. * switching between high and low RR.
  10354. * This macro can be used whenever a comparison has to be made between one
  10355. * hw state and multiple sw state variables.
  10356. */
  10357. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10358. if ((current_config->name != pipe_config->name) && \
  10359. (current_config->alt_name != pipe_config->name)) { \
  10360. DRM_ERROR("mismatch in " #name " " \
  10361. "(expected %i or %i, found %i)\n", \
  10362. current_config->name, \
  10363. current_config->alt_name, \
  10364. pipe_config->name); \
  10365. return false; \
  10366. }
  10367. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10368. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10369. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10370. "(expected %i, found %i)\n", \
  10371. current_config->name & (mask), \
  10372. pipe_config->name & (mask)); \
  10373. return false; \
  10374. }
  10375. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10376. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10377. DRM_ERROR("mismatch in " #name " " \
  10378. "(expected %i, found %i)\n", \
  10379. current_config->name, \
  10380. pipe_config->name); \
  10381. return false; \
  10382. }
  10383. #define PIPE_CONF_QUIRK(quirk) \
  10384. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10385. PIPE_CONF_CHECK_I(cpu_transcoder);
  10386. PIPE_CONF_CHECK_I(has_pch_encoder);
  10387. PIPE_CONF_CHECK_I(fdi_lanes);
  10388. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10389. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10390. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10391. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10392. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10393. PIPE_CONF_CHECK_I(has_dp_encoder);
  10394. if (INTEL_INFO(dev)->gen < 8) {
  10395. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10396. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10397. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10398. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10399. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10400. if (current_config->has_drrs) {
  10401. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10402. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10403. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10404. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10405. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10406. }
  10407. } else {
  10408. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10409. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10410. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10411. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10412. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10413. }
  10414. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10415. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10416. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10417. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10418. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10419. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10420. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10421. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10422. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10423. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10424. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10425. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10426. PIPE_CONF_CHECK_I(pixel_multiplier);
  10427. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10428. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10429. IS_VALLEYVIEW(dev))
  10430. PIPE_CONF_CHECK_I(limited_color_range);
  10431. PIPE_CONF_CHECK_I(has_infoframe);
  10432. PIPE_CONF_CHECK_I(has_audio);
  10433. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10434. DRM_MODE_FLAG_INTERLACE);
  10435. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10436. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10437. DRM_MODE_FLAG_PHSYNC);
  10438. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10439. DRM_MODE_FLAG_NHSYNC);
  10440. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10441. DRM_MODE_FLAG_PVSYNC);
  10442. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10443. DRM_MODE_FLAG_NVSYNC);
  10444. }
  10445. PIPE_CONF_CHECK_I(pipe_src_w);
  10446. PIPE_CONF_CHECK_I(pipe_src_h);
  10447. /*
  10448. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10449. * screen. Since we don't yet re-compute the pipe config when moving
  10450. * just the lvds port away to another pipe the sw tracking won't match.
  10451. *
  10452. * Proper atomic modesets with recomputed global state will fix this.
  10453. * Until then just don't check gmch state for inherited modes.
  10454. */
  10455. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10456. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10457. /* pfit ratios are autocomputed by the hw on gen4+ */
  10458. if (INTEL_INFO(dev)->gen < 4)
  10459. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10460. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10461. }
  10462. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10463. if (current_config->pch_pfit.enabled) {
  10464. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10465. PIPE_CONF_CHECK_I(pch_pfit.size);
  10466. }
  10467. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10468. /* BDW+ don't expose a synchronous way to read the state */
  10469. if (IS_HASWELL(dev))
  10470. PIPE_CONF_CHECK_I(ips_enabled);
  10471. PIPE_CONF_CHECK_I(double_wide);
  10472. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10473. PIPE_CONF_CHECK_I(shared_dpll);
  10474. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10475. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10476. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10477. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10478. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10479. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10480. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10481. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10482. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10483. PIPE_CONF_CHECK_I(pipe_bpp);
  10484. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10485. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10486. #undef PIPE_CONF_CHECK_X
  10487. #undef PIPE_CONF_CHECK_I
  10488. #undef PIPE_CONF_CHECK_I_ALT
  10489. #undef PIPE_CONF_CHECK_FLAGS
  10490. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10491. #undef PIPE_CONF_QUIRK
  10492. return true;
  10493. }
  10494. static void check_wm_state(struct drm_device *dev)
  10495. {
  10496. struct drm_i915_private *dev_priv = dev->dev_private;
  10497. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10498. struct intel_crtc *intel_crtc;
  10499. int plane;
  10500. if (INTEL_INFO(dev)->gen < 9)
  10501. return;
  10502. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10503. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10504. for_each_intel_crtc(dev, intel_crtc) {
  10505. struct skl_ddb_entry *hw_entry, *sw_entry;
  10506. const enum pipe pipe = intel_crtc->pipe;
  10507. if (!intel_crtc->active)
  10508. continue;
  10509. /* planes */
  10510. for_each_plane(dev_priv, pipe, plane) {
  10511. hw_entry = &hw_ddb.plane[pipe][plane];
  10512. sw_entry = &sw_ddb->plane[pipe][plane];
  10513. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10514. continue;
  10515. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10516. "(expected (%u,%u), found (%u,%u))\n",
  10517. pipe_name(pipe), plane + 1,
  10518. sw_entry->start, sw_entry->end,
  10519. hw_entry->start, hw_entry->end);
  10520. }
  10521. /* cursor */
  10522. hw_entry = &hw_ddb.cursor[pipe];
  10523. sw_entry = &sw_ddb->cursor[pipe];
  10524. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10525. continue;
  10526. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10527. "(expected (%u,%u), found (%u,%u))\n",
  10528. pipe_name(pipe),
  10529. sw_entry->start, sw_entry->end,
  10530. hw_entry->start, hw_entry->end);
  10531. }
  10532. }
  10533. static void
  10534. check_connector_state(struct drm_device *dev)
  10535. {
  10536. struct intel_connector *connector;
  10537. for_each_intel_connector(dev, connector) {
  10538. /* This also checks the encoder/connector hw state with the
  10539. * ->get_hw_state callbacks. */
  10540. intel_connector_check_state(connector);
  10541. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10542. "connector's staged encoder doesn't match current encoder\n");
  10543. }
  10544. }
  10545. static void
  10546. check_encoder_state(struct drm_device *dev)
  10547. {
  10548. struct intel_encoder *encoder;
  10549. struct intel_connector *connector;
  10550. for_each_intel_encoder(dev, encoder) {
  10551. bool enabled = false;
  10552. bool active = false;
  10553. enum pipe pipe, tracked_pipe;
  10554. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10555. encoder->base.base.id,
  10556. encoder->base.name);
  10557. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10558. "encoder's stage crtc doesn't match current crtc\n");
  10559. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10560. "encoder's active_connectors set, but no crtc\n");
  10561. for_each_intel_connector(dev, connector) {
  10562. if (connector->base.encoder != &encoder->base)
  10563. continue;
  10564. enabled = true;
  10565. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10566. active = true;
  10567. }
  10568. /*
  10569. * for MST connectors if we unplug the connector is gone
  10570. * away but the encoder is still connected to a crtc
  10571. * until a modeset happens in response to the hotplug.
  10572. */
  10573. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10574. continue;
  10575. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10576. "encoder's enabled state mismatch "
  10577. "(expected %i, found %i)\n",
  10578. !!encoder->base.crtc, enabled);
  10579. I915_STATE_WARN(active && !encoder->base.crtc,
  10580. "active encoder with no crtc\n");
  10581. I915_STATE_WARN(encoder->connectors_active != active,
  10582. "encoder's computed active state doesn't match tracked active state "
  10583. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10584. active = encoder->get_hw_state(encoder, &pipe);
  10585. I915_STATE_WARN(active != encoder->connectors_active,
  10586. "encoder's hw state doesn't match sw tracking "
  10587. "(expected %i, found %i)\n",
  10588. encoder->connectors_active, active);
  10589. if (!encoder->base.crtc)
  10590. continue;
  10591. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10592. I915_STATE_WARN(active && pipe != tracked_pipe,
  10593. "active encoder's pipe doesn't match"
  10594. "(expected %i, found %i)\n",
  10595. tracked_pipe, pipe);
  10596. }
  10597. }
  10598. static void
  10599. check_crtc_state(struct drm_device *dev)
  10600. {
  10601. struct drm_i915_private *dev_priv = dev->dev_private;
  10602. struct intel_crtc *crtc;
  10603. struct intel_encoder *encoder;
  10604. struct intel_crtc_state pipe_config;
  10605. for_each_intel_crtc(dev, crtc) {
  10606. bool enabled = false;
  10607. bool active = false;
  10608. memset(&pipe_config, 0, sizeof(pipe_config));
  10609. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10610. crtc->base.base.id);
  10611. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10612. "active crtc, but not enabled in sw tracking\n");
  10613. for_each_intel_encoder(dev, encoder) {
  10614. if (encoder->base.crtc != &crtc->base)
  10615. continue;
  10616. enabled = true;
  10617. if (encoder->connectors_active)
  10618. active = true;
  10619. }
  10620. I915_STATE_WARN(active != crtc->active,
  10621. "crtc's computed active state doesn't match tracked active state "
  10622. "(expected %i, found %i)\n", active, crtc->active);
  10623. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10624. "crtc's computed enabled state doesn't match tracked enabled state "
  10625. "(expected %i, found %i)\n", enabled,
  10626. crtc->base.state->enable);
  10627. active = dev_priv->display.get_pipe_config(crtc,
  10628. &pipe_config);
  10629. /* hw state is inconsistent with the pipe quirk */
  10630. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10631. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10632. active = crtc->active;
  10633. for_each_intel_encoder(dev, encoder) {
  10634. enum pipe pipe;
  10635. if (encoder->base.crtc != &crtc->base)
  10636. continue;
  10637. if (encoder->get_hw_state(encoder, &pipe))
  10638. encoder->get_config(encoder, &pipe_config);
  10639. }
  10640. I915_STATE_WARN(crtc->active != active,
  10641. "crtc active state doesn't match with hw state "
  10642. "(expected %i, found %i)\n", crtc->active, active);
  10643. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10644. "transitional active state does not match atomic hw state "
  10645. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10646. if (active &&
  10647. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10648. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10649. intel_dump_pipe_config(crtc, &pipe_config,
  10650. "[hw state]");
  10651. intel_dump_pipe_config(crtc, crtc->config,
  10652. "[sw state]");
  10653. }
  10654. }
  10655. }
  10656. static void
  10657. check_shared_dpll_state(struct drm_device *dev)
  10658. {
  10659. struct drm_i915_private *dev_priv = dev->dev_private;
  10660. struct intel_crtc *crtc;
  10661. struct intel_dpll_hw_state dpll_hw_state;
  10662. int i;
  10663. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10664. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10665. int enabled_crtcs = 0, active_crtcs = 0;
  10666. bool active;
  10667. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10668. DRM_DEBUG_KMS("%s\n", pll->name);
  10669. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10670. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10671. "more active pll users than references: %i vs %i\n",
  10672. pll->active, hweight32(pll->config.crtc_mask));
  10673. I915_STATE_WARN(pll->active && !pll->on,
  10674. "pll in active use but not on in sw tracking\n");
  10675. I915_STATE_WARN(pll->on && !pll->active,
  10676. "pll in on but not on in use in sw tracking\n");
  10677. I915_STATE_WARN(pll->on != active,
  10678. "pll on state mismatch (expected %i, found %i)\n",
  10679. pll->on, active);
  10680. for_each_intel_crtc(dev, crtc) {
  10681. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10682. enabled_crtcs++;
  10683. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10684. active_crtcs++;
  10685. }
  10686. I915_STATE_WARN(pll->active != active_crtcs,
  10687. "pll active crtcs mismatch (expected %i, found %i)\n",
  10688. pll->active, active_crtcs);
  10689. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10690. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10691. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10692. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10693. sizeof(dpll_hw_state)),
  10694. "pll hw state mismatch\n");
  10695. }
  10696. }
  10697. void
  10698. intel_modeset_check_state(struct drm_device *dev)
  10699. {
  10700. check_wm_state(dev);
  10701. check_connector_state(dev);
  10702. check_encoder_state(dev);
  10703. check_crtc_state(dev);
  10704. check_shared_dpll_state(dev);
  10705. }
  10706. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10707. int dotclock)
  10708. {
  10709. /*
  10710. * FDI already provided one idea for the dotclock.
  10711. * Yell if the encoder disagrees.
  10712. */
  10713. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10714. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10715. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10716. }
  10717. static void update_scanline_offset(struct intel_crtc *crtc)
  10718. {
  10719. struct drm_device *dev = crtc->base.dev;
  10720. /*
  10721. * The scanline counter increments at the leading edge of hsync.
  10722. *
  10723. * On most platforms it starts counting from vtotal-1 on the
  10724. * first active line. That means the scanline counter value is
  10725. * always one less than what we would expect. Ie. just after
  10726. * start of vblank, which also occurs at start of hsync (on the
  10727. * last active line), the scanline counter will read vblank_start-1.
  10728. *
  10729. * On gen2 the scanline counter starts counting from 1 instead
  10730. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10731. * to keep the value positive), instead of adding one.
  10732. *
  10733. * On HSW+ the behaviour of the scanline counter depends on the output
  10734. * type. For DP ports it behaves like most other platforms, but on HDMI
  10735. * there's an extra 1 line difference. So we need to add two instead of
  10736. * one to the value.
  10737. */
  10738. if (IS_GEN2(dev)) {
  10739. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10740. int vtotal;
  10741. vtotal = mode->crtc_vtotal;
  10742. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10743. vtotal /= 2;
  10744. crtc->scanline_offset = vtotal - 1;
  10745. } else if (HAS_DDI(dev) &&
  10746. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10747. crtc->scanline_offset = 2;
  10748. } else
  10749. crtc->scanline_offset = 1;
  10750. }
  10751. static int intel_modeset_setup_plls(struct drm_atomic_state *state)
  10752. {
  10753. struct drm_device *dev = state->dev;
  10754. struct drm_i915_private *dev_priv = to_i915(dev);
  10755. unsigned clear_pipes = 0;
  10756. struct intel_crtc *intel_crtc;
  10757. struct intel_crtc_state *intel_crtc_state;
  10758. struct drm_crtc *crtc;
  10759. struct drm_crtc_state *crtc_state;
  10760. int ret = 0;
  10761. int i;
  10762. if (!dev_priv->display.crtc_compute_clock)
  10763. return 0;
  10764. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10765. intel_crtc = to_intel_crtc(crtc);
  10766. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10767. if (needs_modeset(crtc_state)) {
  10768. clear_pipes |= 1 << intel_crtc->pipe;
  10769. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10770. }
  10771. }
  10772. if (clear_pipes) {
  10773. struct intel_shared_dpll_config *shared_dpll =
  10774. intel_atomic_get_shared_dpll_state(state);
  10775. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10776. shared_dpll[i].crtc_mask &= ~clear_pipes;
  10777. }
  10778. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10779. if (!needs_modeset(crtc_state) || !crtc_state->enable)
  10780. continue;
  10781. intel_crtc = to_intel_crtc(crtc);
  10782. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10783. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10784. intel_crtc_state);
  10785. if (ret)
  10786. return ret;
  10787. }
  10788. return ret;
  10789. }
  10790. /*
  10791. * This implements the workaround described in the "notes" section of the mode
  10792. * set sequence documentation. When going from no pipes or single pipe to
  10793. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10794. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10795. */
  10796. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10797. {
  10798. struct drm_crtc_state *crtc_state;
  10799. struct intel_crtc *intel_crtc;
  10800. struct drm_crtc *crtc;
  10801. struct intel_crtc_state *first_crtc_state = NULL;
  10802. struct intel_crtc_state *other_crtc_state = NULL;
  10803. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10804. int i;
  10805. /* look at all crtc's that are going to be enabled in during modeset */
  10806. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10807. intel_crtc = to_intel_crtc(crtc);
  10808. if (!crtc_state->active || !needs_modeset(crtc_state))
  10809. continue;
  10810. if (first_crtc_state) {
  10811. other_crtc_state = to_intel_crtc_state(crtc_state);
  10812. break;
  10813. } else {
  10814. first_crtc_state = to_intel_crtc_state(crtc_state);
  10815. first_pipe = intel_crtc->pipe;
  10816. }
  10817. }
  10818. /* No workaround needed? */
  10819. if (!first_crtc_state)
  10820. return 0;
  10821. /* w/a possibly needed, check how many crtc's are already enabled. */
  10822. for_each_intel_crtc(state->dev, intel_crtc) {
  10823. struct intel_crtc_state *pipe_config;
  10824. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10825. if (IS_ERR(pipe_config))
  10826. return PTR_ERR(pipe_config);
  10827. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10828. if (!pipe_config->base.active ||
  10829. needs_modeset(&pipe_config->base))
  10830. continue;
  10831. /* 2 or more enabled crtcs means no need for w/a */
  10832. if (enabled_pipe != INVALID_PIPE)
  10833. return 0;
  10834. enabled_pipe = intel_crtc->pipe;
  10835. }
  10836. if (enabled_pipe != INVALID_PIPE)
  10837. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10838. else if (other_crtc_state)
  10839. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10840. return 0;
  10841. }
  10842. /* Code that should eventually be part of atomic_check() */
  10843. static int intel_modeset_checks(struct drm_atomic_state *state)
  10844. {
  10845. struct drm_device *dev = state->dev;
  10846. int ret;
  10847. /*
  10848. * See if the config requires any additional preparation, e.g.
  10849. * to adjust global state with pipes off. We need to do this
  10850. * here so we can get the modeset_pipe updated config for the new
  10851. * mode set on this crtc. For other crtcs we need to use the
  10852. * adjusted_mode bits in the crtc directly.
  10853. */
  10854. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
  10855. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
  10856. ret = valleyview_modeset_global_pipes(state);
  10857. else
  10858. ret = broadwell_modeset_global_pipes(state);
  10859. if (ret)
  10860. return ret;
  10861. }
  10862. ret = intel_modeset_setup_plls(state);
  10863. if (ret)
  10864. return ret;
  10865. if (IS_HASWELL(dev))
  10866. ret = haswell_mode_set_planes_workaround(state);
  10867. return ret;
  10868. }
  10869. static int
  10870. intel_modeset_compute_config(struct drm_atomic_state *state)
  10871. {
  10872. struct drm_crtc *crtc;
  10873. struct drm_crtc_state *crtc_state;
  10874. int ret, i;
  10875. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10876. if (ret)
  10877. return ret;
  10878. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10879. if (!crtc_state->enable &&
  10880. WARN_ON(crtc_state->active))
  10881. crtc_state->active = false;
  10882. if (!crtc_state->enable)
  10883. continue;
  10884. ret = intel_modeset_pipe_config(crtc, state);
  10885. if (ret)
  10886. return ret;
  10887. intel_dump_pipe_config(to_intel_crtc(crtc),
  10888. to_intel_crtc_state(crtc_state),
  10889. "[modeset]");
  10890. }
  10891. ret = intel_modeset_checks(state);
  10892. if (ret)
  10893. return ret;
  10894. return drm_atomic_helper_check_planes(state->dev, state);
  10895. }
  10896. static int __intel_set_mode(struct drm_atomic_state *state)
  10897. {
  10898. struct drm_device *dev = state->dev;
  10899. struct drm_i915_private *dev_priv = dev->dev_private;
  10900. struct drm_crtc *crtc;
  10901. struct drm_crtc_state *crtc_state;
  10902. int ret = 0;
  10903. int i;
  10904. ret = drm_atomic_helper_prepare_planes(dev, state);
  10905. if (ret)
  10906. return ret;
  10907. drm_atomic_helper_swap_state(dev, state);
  10908. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10909. if (!needs_modeset(crtc->state) || !crtc_state->active)
  10910. continue;
  10911. intel_crtc_disable_planes(crtc);
  10912. dev_priv->display.crtc_disable(crtc);
  10913. }
  10914. /* Only after disabling all output pipelines that will be changed can we
  10915. * update the the output configuration. */
  10916. intel_modeset_update_state(state);
  10917. /* The state has been swaped above, so state actually contains the
  10918. * old state now. */
  10919. modeset_update_crtc_power_domains(state);
  10920. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10921. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10922. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  10923. if (!needs_modeset(crtc->state) || !crtc->state->active)
  10924. continue;
  10925. update_scanline_offset(to_intel_crtc(crtc));
  10926. dev_priv->display.crtc_enable(crtc);
  10927. intel_crtc_enable_planes(crtc);
  10928. }
  10929. /* FIXME: add subpixel order */
  10930. drm_atomic_helper_cleanup_planes(dev, state);
  10931. drm_atomic_state_free(state);
  10932. return 0;
  10933. }
  10934. static int intel_set_mode_checked(struct drm_atomic_state *state)
  10935. {
  10936. struct drm_device *dev = state->dev;
  10937. int ret;
  10938. ret = __intel_set_mode(state);
  10939. if (ret == 0)
  10940. intel_modeset_check_state(dev);
  10941. return ret;
  10942. }
  10943. static int intel_set_mode(struct drm_atomic_state *state)
  10944. {
  10945. int ret;
  10946. ret = intel_modeset_compute_config(state);
  10947. if (ret)
  10948. return ret;
  10949. return intel_set_mode_checked(state);
  10950. }
  10951. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10952. {
  10953. struct drm_device *dev = crtc->dev;
  10954. struct drm_atomic_state *state;
  10955. struct intel_crtc *intel_crtc;
  10956. struct intel_encoder *encoder;
  10957. struct intel_connector *connector;
  10958. struct drm_connector_state *connector_state;
  10959. struct intel_crtc_state *crtc_state;
  10960. int ret;
  10961. state = drm_atomic_state_alloc(dev);
  10962. if (!state) {
  10963. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  10964. crtc->base.id);
  10965. return;
  10966. }
  10967. state->acquire_ctx = dev->mode_config.acquire_ctx;
  10968. /* The force restore path in the HW readout code relies on the staged
  10969. * config still keeping the user requested config while the actual
  10970. * state has been overwritten by the configuration read from HW. We
  10971. * need to copy the staged config to the atomic state, otherwise the
  10972. * mode set will just reapply the state the HW is already in. */
  10973. for_each_intel_encoder(dev, encoder) {
  10974. if (&encoder->new_crtc->base != crtc)
  10975. continue;
  10976. for_each_intel_connector(dev, connector) {
  10977. if (connector->new_encoder != encoder)
  10978. continue;
  10979. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  10980. if (IS_ERR(connector_state)) {
  10981. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  10982. connector->base.base.id,
  10983. connector->base.name,
  10984. PTR_ERR(connector_state));
  10985. continue;
  10986. }
  10987. connector_state->crtc = crtc;
  10988. connector_state->best_encoder = &encoder->base;
  10989. }
  10990. }
  10991. for_each_intel_crtc(dev, intel_crtc) {
  10992. if (intel_crtc->new_enabled == intel_crtc->base.enabled)
  10993. continue;
  10994. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  10995. if (IS_ERR(crtc_state)) {
  10996. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  10997. intel_crtc->base.base.id,
  10998. PTR_ERR(crtc_state));
  10999. continue;
  11000. }
  11001. crtc_state->base.active = crtc_state->base.enable =
  11002. intel_crtc->new_enabled;
  11003. if (&intel_crtc->base == crtc)
  11004. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  11005. }
  11006. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  11007. crtc->primary->fb, crtc->x, crtc->y);
  11008. ret = intel_set_mode(state);
  11009. if (ret)
  11010. drm_atomic_state_free(state);
  11011. }
  11012. #undef for_each_intel_crtc_masked
  11013. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  11014. struct drm_mode_set *set)
  11015. {
  11016. int ro;
  11017. for (ro = 0; ro < set->num_connectors; ro++)
  11018. if (set->connectors[ro] == &connector->base)
  11019. return true;
  11020. return false;
  11021. }
  11022. static int
  11023. intel_modeset_stage_output_state(struct drm_device *dev,
  11024. struct drm_mode_set *set,
  11025. struct drm_atomic_state *state)
  11026. {
  11027. struct intel_connector *connector;
  11028. struct drm_connector *drm_connector;
  11029. struct drm_connector_state *connector_state;
  11030. struct drm_crtc *crtc;
  11031. struct drm_crtc_state *crtc_state;
  11032. int i, ret;
  11033. /* The upper layers ensure that we either disable a crtc or have a list
  11034. * of connectors. For paranoia, double-check this. */
  11035. WARN_ON(!set->fb && (set->num_connectors != 0));
  11036. WARN_ON(set->fb && (set->num_connectors == 0));
  11037. for_each_intel_connector(dev, connector) {
  11038. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  11039. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  11040. continue;
  11041. connector_state =
  11042. drm_atomic_get_connector_state(state, &connector->base);
  11043. if (IS_ERR(connector_state))
  11044. return PTR_ERR(connector_state);
  11045. if (in_mode_set) {
  11046. int pipe = to_intel_crtc(set->crtc)->pipe;
  11047. connector_state->best_encoder =
  11048. &intel_find_encoder(connector, pipe)->base;
  11049. }
  11050. if (connector->base.state->crtc != set->crtc)
  11051. continue;
  11052. /* If we disable the crtc, disable all its connectors. Also, if
  11053. * the connector is on the changing crtc but not on the new
  11054. * connector list, disable it. */
  11055. if (!set->fb || !in_mode_set) {
  11056. connector_state->best_encoder = NULL;
  11057. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11058. connector->base.base.id,
  11059. connector->base.name);
  11060. }
  11061. }
  11062. /* connector->new_encoder is now updated for all connectors. */
  11063. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11064. connector = to_intel_connector(drm_connector);
  11065. if (!connector_state->best_encoder) {
  11066. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11067. NULL);
  11068. if (ret)
  11069. return ret;
  11070. continue;
  11071. }
  11072. if (intel_connector_in_mode_set(connector, set)) {
  11073. struct drm_crtc *crtc = connector->base.state->crtc;
  11074. /* If this connector was in a previous crtc, add it
  11075. * to the state. We might need to disable it. */
  11076. if (crtc) {
  11077. crtc_state =
  11078. drm_atomic_get_crtc_state(state, crtc);
  11079. if (IS_ERR(crtc_state))
  11080. return PTR_ERR(crtc_state);
  11081. }
  11082. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11083. set->crtc);
  11084. if (ret)
  11085. return ret;
  11086. }
  11087. /* Make sure the new CRTC will work with the encoder */
  11088. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11089. connector_state->crtc)) {
  11090. return -EINVAL;
  11091. }
  11092. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11093. connector->base.base.id,
  11094. connector->base.name,
  11095. connector_state->crtc->base.id);
  11096. if (connector_state->best_encoder != &connector->encoder->base)
  11097. connector->encoder =
  11098. to_intel_encoder(connector_state->best_encoder);
  11099. }
  11100. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11101. bool has_connectors;
  11102. ret = drm_atomic_add_affected_connectors(state, crtc);
  11103. if (ret)
  11104. return ret;
  11105. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11106. if (has_connectors != crtc_state->enable)
  11107. crtc_state->enable =
  11108. crtc_state->active = has_connectors;
  11109. }
  11110. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11111. set->fb, set->x, set->y);
  11112. if (ret)
  11113. return ret;
  11114. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11115. if (IS_ERR(crtc_state))
  11116. return PTR_ERR(crtc_state);
  11117. if (set->mode)
  11118. drm_mode_copy(&crtc_state->mode, set->mode);
  11119. if (set->num_connectors)
  11120. crtc_state->active = true;
  11121. return 0;
  11122. }
  11123. static int intel_crtc_set_config(struct drm_mode_set *set)
  11124. {
  11125. struct drm_device *dev;
  11126. struct drm_atomic_state *state = NULL;
  11127. int ret;
  11128. BUG_ON(!set);
  11129. BUG_ON(!set->crtc);
  11130. BUG_ON(!set->crtc->helper_private);
  11131. /* Enforce sane interface api - has been abused by the fb helper. */
  11132. BUG_ON(!set->mode && set->fb);
  11133. BUG_ON(set->fb && set->num_connectors == 0);
  11134. if (set->fb) {
  11135. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11136. set->crtc->base.id, set->fb->base.id,
  11137. (int)set->num_connectors, set->x, set->y);
  11138. } else {
  11139. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11140. }
  11141. dev = set->crtc->dev;
  11142. state = drm_atomic_state_alloc(dev);
  11143. if (!state)
  11144. return -ENOMEM;
  11145. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11146. ret = intel_modeset_stage_output_state(dev, set, state);
  11147. if (ret)
  11148. goto out;
  11149. ret = intel_modeset_compute_config(state);
  11150. if (ret)
  11151. goto out;
  11152. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11153. ret = intel_set_mode_checked(state);
  11154. if (ret) {
  11155. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11156. set->crtc->base.id, ret);
  11157. }
  11158. out:
  11159. if (ret)
  11160. drm_atomic_state_free(state);
  11161. return ret;
  11162. }
  11163. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11164. .gamma_set = intel_crtc_gamma_set,
  11165. .set_config = intel_crtc_set_config,
  11166. .destroy = intel_crtc_destroy,
  11167. .page_flip = intel_crtc_page_flip,
  11168. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11169. .atomic_destroy_state = intel_crtc_destroy_state,
  11170. };
  11171. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11172. struct intel_shared_dpll *pll,
  11173. struct intel_dpll_hw_state *hw_state)
  11174. {
  11175. uint32_t val;
  11176. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11177. return false;
  11178. val = I915_READ(PCH_DPLL(pll->id));
  11179. hw_state->dpll = val;
  11180. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11181. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11182. return val & DPLL_VCO_ENABLE;
  11183. }
  11184. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11185. struct intel_shared_dpll *pll)
  11186. {
  11187. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11188. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11189. }
  11190. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11191. struct intel_shared_dpll *pll)
  11192. {
  11193. /* PCH refclock must be enabled first */
  11194. ibx_assert_pch_refclk_enabled(dev_priv);
  11195. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11196. /* Wait for the clocks to stabilize. */
  11197. POSTING_READ(PCH_DPLL(pll->id));
  11198. udelay(150);
  11199. /* The pixel multiplier can only be updated once the
  11200. * DPLL is enabled and the clocks are stable.
  11201. *
  11202. * So write it again.
  11203. */
  11204. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11205. POSTING_READ(PCH_DPLL(pll->id));
  11206. udelay(200);
  11207. }
  11208. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11209. struct intel_shared_dpll *pll)
  11210. {
  11211. struct drm_device *dev = dev_priv->dev;
  11212. struct intel_crtc *crtc;
  11213. /* Make sure no transcoder isn't still depending on us. */
  11214. for_each_intel_crtc(dev, crtc) {
  11215. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11216. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11217. }
  11218. I915_WRITE(PCH_DPLL(pll->id), 0);
  11219. POSTING_READ(PCH_DPLL(pll->id));
  11220. udelay(200);
  11221. }
  11222. static char *ibx_pch_dpll_names[] = {
  11223. "PCH DPLL A",
  11224. "PCH DPLL B",
  11225. };
  11226. static void ibx_pch_dpll_init(struct drm_device *dev)
  11227. {
  11228. struct drm_i915_private *dev_priv = dev->dev_private;
  11229. int i;
  11230. dev_priv->num_shared_dpll = 2;
  11231. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11232. dev_priv->shared_dplls[i].id = i;
  11233. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11234. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11235. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11236. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11237. dev_priv->shared_dplls[i].get_hw_state =
  11238. ibx_pch_dpll_get_hw_state;
  11239. }
  11240. }
  11241. static void intel_shared_dpll_init(struct drm_device *dev)
  11242. {
  11243. struct drm_i915_private *dev_priv = dev->dev_private;
  11244. intel_update_cdclk(dev);
  11245. if (HAS_DDI(dev))
  11246. intel_ddi_pll_init(dev);
  11247. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11248. ibx_pch_dpll_init(dev);
  11249. else
  11250. dev_priv->num_shared_dpll = 0;
  11251. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11252. }
  11253. /**
  11254. * intel_wm_need_update - Check whether watermarks need updating
  11255. * @plane: drm plane
  11256. * @state: new plane state
  11257. *
  11258. * Check current plane state versus the new one to determine whether
  11259. * watermarks need to be recalculated.
  11260. *
  11261. * Returns true or false.
  11262. */
  11263. bool intel_wm_need_update(struct drm_plane *plane,
  11264. struct drm_plane_state *state)
  11265. {
  11266. /* Update watermarks on tiling changes. */
  11267. if (!plane->state->fb || !state->fb ||
  11268. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11269. plane->state->rotation != state->rotation)
  11270. return true;
  11271. return false;
  11272. }
  11273. /**
  11274. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11275. * @plane: drm plane to prepare for
  11276. * @fb: framebuffer to prepare for presentation
  11277. *
  11278. * Prepares a framebuffer for usage on a display plane. Generally this
  11279. * involves pinning the underlying object and updating the frontbuffer tracking
  11280. * bits. Some older platforms need special physical address handling for
  11281. * cursor planes.
  11282. *
  11283. * Returns 0 on success, negative error code on failure.
  11284. */
  11285. int
  11286. intel_prepare_plane_fb(struct drm_plane *plane,
  11287. struct drm_framebuffer *fb,
  11288. const struct drm_plane_state *new_state)
  11289. {
  11290. struct drm_device *dev = plane->dev;
  11291. struct intel_plane *intel_plane = to_intel_plane(plane);
  11292. enum pipe pipe = intel_plane->pipe;
  11293. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11294. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11295. unsigned frontbuffer_bits = 0;
  11296. int ret = 0;
  11297. if (!obj)
  11298. return 0;
  11299. switch (plane->type) {
  11300. case DRM_PLANE_TYPE_PRIMARY:
  11301. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11302. break;
  11303. case DRM_PLANE_TYPE_CURSOR:
  11304. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11305. break;
  11306. case DRM_PLANE_TYPE_OVERLAY:
  11307. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11308. break;
  11309. }
  11310. mutex_lock(&dev->struct_mutex);
  11311. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11312. INTEL_INFO(dev)->cursor_needs_physical) {
  11313. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11314. ret = i915_gem_object_attach_phys(obj, align);
  11315. if (ret)
  11316. DRM_DEBUG_KMS("failed to attach phys object\n");
  11317. } else {
  11318. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11319. }
  11320. if (ret == 0)
  11321. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11322. mutex_unlock(&dev->struct_mutex);
  11323. return ret;
  11324. }
  11325. /**
  11326. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11327. * @plane: drm plane to clean up for
  11328. * @fb: old framebuffer that was on plane
  11329. *
  11330. * Cleans up a framebuffer that has just been removed from a plane.
  11331. */
  11332. void
  11333. intel_cleanup_plane_fb(struct drm_plane *plane,
  11334. struct drm_framebuffer *fb,
  11335. const struct drm_plane_state *old_state)
  11336. {
  11337. struct drm_device *dev = plane->dev;
  11338. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11339. if (WARN_ON(!obj))
  11340. return;
  11341. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11342. !INTEL_INFO(dev)->cursor_needs_physical) {
  11343. mutex_lock(&dev->struct_mutex);
  11344. intel_unpin_fb_obj(fb, old_state);
  11345. mutex_unlock(&dev->struct_mutex);
  11346. }
  11347. }
  11348. int
  11349. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11350. {
  11351. int max_scale;
  11352. struct drm_device *dev;
  11353. struct drm_i915_private *dev_priv;
  11354. int crtc_clock, cdclk;
  11355. if (!intel_crtc || !crtc_state)
  11356. return DRM_PLANE_HELPER_NO_SCALING;
  11357. dev = intel_crtc->base.dev;
  11358. dev_priv = dev->dev_private;
  11359. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11360. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11361. if (!crtc_clock || !cdclk)
  11362. return DRM_PLANE_HELPER_NO_SCALING;
  11363. /*
  11364. * skl max scale is lower of:
  11365. * close to 3 but not 3, -1 is for that purpose
  11366. * or
  11367. * cdclk/crtc_clock
  11368. */
  11369. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11370. return max_scale;
  11371. }
  11372. static int
  11373. intel_check_primary_plane(struct drm_plane *plane,
  11374. struct intel_plane_state *state)
  11375. {
  11376. struct drm_device *dev = plane->dev;
  11377. struct drm_i915_private *dev_priv = dev->dev_private;
  11378. struct drm_crtc *crtc = state->base.crtc;
  11379. struct intel_crtc *intel_crtc;
  11380. struct intel_crtc_state *crtc_state;
  11381. struct drm_framebuffer *fb = state->base.fb;
  11382. struct drm_rect *dest = &state->dst;
  11383. struct drm_rect *src = &state->src;
  11384. const struct drm_rect *clip = &state->clip;
  11385. bool can_position = false;
  11386. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11387. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11388. int ret;
  11389. crtc = crtc ? crtc : plane->crtc;
  11390. intel_crtc = to_intel_crtc(crtc);
  11391. crtc_state = state->base.state ?
  11392. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11393. if (INTEL_INFO(dev)->gen >= 9) {
  11394. /* use scaler when colorkey is not required */
  11395. if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11396. min_scale = 1;
  11397. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11398. }
  11399. can_position = true;
  11400. }
  11401. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11402. src, dest, clip,
  11403. min_scale,
  11404. max_scale,
  11405. can_position, true,
  11406. &state->visible);
  11407. if (ret)
  11408. return ret;
  11409. if (intel_crtc->active) {
  11410. struct intel_plane_state *old_state =
  11411. to_intel_plane_state(plane->state);
  11412. intel_crtc->atomic.wait_for_flips = true;
  11413. /*
  11414. * FBC does not work on some platforms for rotated
  11415. * planes, so disable it when rotation is not 0 and
  11416. * update it when rotation is set back to 0.
  11417. *
  11418. * FIXME: This is redundant with the fbc update done in
  11419. * the primary plane enable function except that that
  11420. * one is done too late. We eventually need to unify
  11421. * this.
  11422. */
  11423. if (state->visible &&
  11424. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11425. dev_priv->fbc.crtc == intel_crtc &&
  11426. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11427. intel_crtc->atomic.disable_fbc = true;
  11428. }
  11429. if (state->visible && !old_state->visible) {
  11430. /*
  11431. * BDW signals flip done immediately if the plane
  11432. * is disabled, even if the plane enable is already
  11433. * armed to occur at the next vblank :(
  11434. */
  11435. if (IS_BROADWELL(dev))
  11436. intel_crtc->atomic.wait_vblank = true;
  11437. if (crtc_state && !needs_modeset(&crtc_state->base))
  11438. intel_crtc->atomic.post_enable_primary = true;
  11439. }
  11440. if (!state->visible && old_state->visible &&
  11441. crtc_state && !needs_modeset(&crtc_state->base))
  11442. intel_crtc->atomic.pre_disable_primary = true;
  11443. intel_crtc->atomic.fb_bits |=
  11444. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11445. intel_crtc->atomic.update_fbc = true;
  11446. if (intel_wm_need_update(plane, &state->base))
  11447. intel_crtc->atomic.update_wm = true;
  11448. }
  11449. if (INTEL_INFO(dev)->gen >= 9) {
  11450. ret = skl_update_scaler_users(intel_crtc, crtc_state,
  11451. to_intel_plane(plane), state, 0);
  11452. if (ret)
  11453. return ret;
  11454. }
  11455. return 0;
  11456. }
  11457. static void
  11458. intel_commit_primary_plane(struct drm_plane *plane,
  11459. struct intel_plane_state *state)
  11460. {
  11461. struct drm_crtc *crtc = state->base.crtc;
  11462. struct drm_framebuffer *fb = state->base.fb;
  11463. struct drm_device *dev = plane->dev;
  11464. struct drm_i915_private *dev_priv = dev->dev_private;
  11465. struct intel_crtc *intel_crtc;
  11466. struct drm_rect *src = &state->src;
  11467. crtc = crtc ? crtc : plane->crtc;
  11468. intel_crtc = to_intel_crtc(crtc);
  11469. plane->fb = fb;
  11470. crtc->x = src->x1 >> 16;
  11471. crtc->y = src->y1 >> 16;
  11472. if (intel_crtc->active) {
  11473. if (state->visible)
  11474. /* FIXME: kill this fastboot hack */
  11475. intel_update_pipe_size(intel_crtc);
  11476. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11477. crtc->x, crtc->y);
  11478. }
  11479. }
  11480. static void
  11481. intel_disable_primary_plane(struct drm_plane *plane,
  11482. struct drm_crtc *crtc,
  11483. bool force)
  11484. {
  11485. struct drm_device *dev = plane->dev;
  11486. struct drm_i915_private *dev_priv = dev->dev_private;
  11487. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11488. }
  11489. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11490. {
  11491. struct drm_device *dev = crtc->dev;
  11492. struct drm_i915_private *dev_priv = dev->dev_private;
  11493. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11494. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  11495. struct intel_plane *intel_plane;
  11496. struct drm_plane *p;
  11497. unsigned fb_bits = 0;
  11498. /* Track fb's for any planes being disabled */
  11499. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11500. intel_plane = to_intel_plane(p);
  11501. if (intel_crtc->atomic.disabled_planes &
  11502. (1 << drm_plane_index(p))) {
  11503. switch (p->type) {
  11504. case DRM_PLANE_TYPE_PRIMARY:
  11505. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11506. break;
  11507. case DRM_PLANE_TYPE_CURSOR:
  11508. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11509. break;
  11510. case DRM_PLANE_TYPE_OVERLAY:
  11511. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11512. break;
  11513. }
  11514. mutex_lock(&dev->struct_mutex);
  11515. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11516. mutex_unlock(&dev->struct_mutex);
  11517. }
  11518. }
  11519. if (intel_crtc->atomic.wait_for_flips)
  11520. intel_crtc_wait_for_pending_flips(crtc);
  11521. if (intel_crtc->atomic.disable_fbc)
  11522. intel_fbc_disable(dev);
  11523. if (intel_crtc->atomic.pre_disable_primary)
  11524. intel_pre_disable_primary(crtc);
  11525. if (intel_crtc->atomic.update_wm)
  11526. intel_update_watermarks(crtc);
  11527. intel_runtime_pm_get(dev_priv);
  11528. /* Perform vblank evasion around commit operation */
  11529. if (crtc_state->active && !needs_modeset(crtc_state))
  11530. intel_crtc->atomic.evade =
  11531. intel_pipe_update_start(intel_crtc,
  11532. &intel_crtc->atomic.start_vbl_count);
  11533. }
  11534. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11535. {
  11536. struct drm_device *dev = crtc->dev;
  11537. struct drm_i915_private *dev_priv = dev->dev_private;
  11538. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11539. struct drm_plane *p;
  11540. if (intel_crtc->atomic.evade)
  11541. intel_pipe_update_end(intel_crtc,
  11542. intel_crtc->atomic.start_vbl_count);
  11543. intel_runtime_pm_put(dev_priv);
  11544. if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
  11545. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11546. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11547. if (intel_crtc->atomic.update_fbc) {
  11548. mutex_lock(&dev->struct_mutex);
  11549. intel_fbc_update(dev);
  11550. mutex_unlock(&dev->struct_mutex);
  11551. }
  11552. if (intel_crtc->atomic.post_enable_primary)
  11553. intel_post_enable_primary(crtc);
  11554. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11555. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11556. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11557. false, false);
  11558. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11559. }
  11560. /**
  11561. * intel_plane_destroy - destroy a plane
  11562. * @plane: plane to destroy
  11563. *
  11564. * Common destruction function for all types of planes (primary, cursor,
  11565. * sprite).
  11566. */
  11567. void intel_plane_destroy(struct drm_plane *plane)
  11568. {
  11569. struct intel_plane *intel_plane = to_intel_plane(plane);
  11570. drm_plane_cleanup(plane);
  11571. kfree(intel_plane);
  11572. }
  11573. const struct drm_plane_funcs intel_plane_funcs = {
  11574. .update_plane = drm_atomic_helper_update_plane,
  11575. .disable_plane = drm_atomic_helper_disable_plane,
  11576. .destroy = intel_plane_destroy,
  11577. .set_property = drm_atomic_helper_plane_set_property,
  11578. .atomic_get_property = intel_plane_atomic_get_property,
  11579. .atomic_set_property = intel_plane_atomic_set_property,
  11580. .atomic_duplicate_state = intel_plane_duplicate_state,
  11581. .atomic_destroy_state = intel_plane_destroy_state,
  11582. };
  11583. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11584. int pipe)
  11585. {
  11586. struct intel_plane *primary;
  11587. struct intel_plane_state *state;
  11588. const uint32_t *intel_primary_formats;
  11589. int num_formats;
  11590. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11591. if (primary == NULL)
  11592. return NULL;
  11593. state = intel_create_plane_state(&primary->base);
  11594. if (!state) {
  11595. kfree(primary);
  11596. return NULL;
  11597. }
  11598. primary->base.state = &state->base;
  11599. primary->can_scale = false;
  11600. primary->max_downscale = 1;
  11601. if (INTEL_INFO(dev)->gen >= 9) {
  11602. primary->can_scale = true;
  11603. state->scaler_id = -1;
  11604. }
  11605. primary->pipe = pipe;
  11606. primary->plane = pipe;
  11607. primary->check_plane = intel_check_primary_plane;
  11608. primary->commit_plane = intel_commit_primary_plane;
  11609. primary->disable_plane = intel_disable_primary_plane;
  11610. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11611. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11612. primary->plane = !pipe;
  11613. if (INTEL_INFO(dev)->gen >= 9) {
  11614. intel_primary_formats = skl_primary_formats;
  11615. num_formats = ARRAY_SIZE(skl_primary_formats);
  11616. } else if (INTEL_INFO(dev)->gen >= 4) {
  11617. intel_primary_formats = i965_primary_formats;
  11618. num_formats = ARRAY_SIZE(i965_primary_formats);
  11619. } else {
  11620. intel_primary_formats = i8xx_primary_formats;
  11621. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11622. }
  11623. drm_universal_plane_init(dev, &primary->base, 0,
  11624. &intel_plane_funcs,
  11625. intel_primary_formats, num_formats,
  11626. DRM_PLANE_TYPE_PRIMARY);
  11627. if (INTEL_INFO(dev)->gen >= 4)
  11628. intel_create_rotation_property(dev, primary);
  11629. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11630. return &primary->base;
  11631. }
  11632. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11633. {
  11634. if (!dev->mode_config.rotation_property) {
  11635. unsigned long flags = BIT(DRM_ROTATE_0) |
  11636. BIT(DRM_ROTATE_180);
  11637. if (INTEL_INFO(dev)->gen >= 9)
  11638. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11639. dev->mode_config.rotation_property =
  11640. drm_mode_create_rotation_property(dev, flags);
  11641. }
  11642. if (dev->mode_config.rotation_property)
  11643. drm_object_attach_property(&plane->base.base,
  11644. dev->mode_config.rotation_property,
  11645. plane->base.state->rotation);
  11646. }
  11647. static int
  11648. intel_check_cursor_plane(struct drm_plane *plane,
  11649. struct intel_plane_state *state)
  11650. {
  11651. struct drm_crtc *crtc = state->base.crtc;
  11652. struct drm_device *dev = plane->dev;
  11653. struct drm_framebuffer *fb = state->base.fb;
  11654. struct drm_rect *dest = &state->dst;
  11655. struct drm_rect *src = &state->src;
  11656. const struct drm_rect *clip = &state->clip;
  11657. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11658. struct intel_crtc *intel_crtc;
  11659. unsigned stride;
  11660. int ret;
  11661. crtc = crtc ? crtc : plane->crtc;
  11662. intel_crtc = to_intel_crtc(crtc);
  11663. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11664. src, dest, clip,
  11665. DRM_PLANE_HELPER_NO_SCALING,
  11666. DRM_PLANE_HELPER_NO_SCALING,
  11667. true, true, &state->visible);
  11668. if (ret)
  11669. return ret;
  11670. /* if we want to turn off the cursor ignore width and height */
  11671. if (!obj)
  11672. goto finish;
  11673. /* Check for which cursor types we support */
  11674. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11675. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11676. state->base.crtc_w, state->base.crtc_h);
  11677. return -EINVAL;
  11678. }
  11679. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11680. if (obj->base.size < stride * state->base.crtc_h) {
  11681. DRM_DEBUG_KMS("buffer is too small\n");
  11682. return -ENOMEM;
  11683. }
  11684. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11685. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11686. ret = -EINVAL;
  11687. }
  11688. finish:
  11689. if (intel_crtc->active) {
  11690. if (plane->state->crtc_w != state->base.crtc_w)
  11691. intel_crtc->atomic.update_wm = true;
  11692. intel_crtc->atomic.fb_bits |=
  11693. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11694. }
  11695. return ret;
  11696. }
  11697. static void
  11698. intel_disable_cursor_plane(struct drm_plane *plane,
  11699. struct drm_crtc *crtc,
  11700. bool force)
  11701. {
  11702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11703. if (!force) {
  11704. plane->fb = NULL;
  11705. intel_crtc->cursor_bo = NULL;
  11706. intel_crtc->cursor_addr = 0;
  11707. }
  11708. intel_crtc_update_cursor(crtc, false);
  11709. }
  11710. static void
  11711. intel_commit_cursor_plane(struct drm_plane *plane,
  11712. struct intel_plane_state *state)
  11713. {
  11714. struct drm_crtc *crtc = state->base.crtc;
  11715. struct drm_device *dev = plane->dev;
  11716. struct intel_crtc *intel_crtc;
  11717. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11718. uint32_t addr;
  11719. crtc = crtc ? crtc : plane->crtc;
  11720. intel_crtc = to_intel_crtc(crtc);
  11721. plane->fb = state->base.fb;
  11722. crtc->cursor_x = state->base.crtc_x;
  11723. crtc->cursor_y = state->base.crtc_y;
  11724. if (intel_crtc->cursor_bo == obj)
  11725. goto update;
  11726. if (!obj)
  11727. addr = 0;
  11728. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11729. addr = i915_gem_obj_ggtt_offset(obj);
  11730. else
  11731. addr = obj->phys_handle->busaddr;
  11732. intel_crtc->cursor_addr = addr;
  11733. intel_crtc->cursor_bo = obj;
  11734. update:
  11735. if (intel_crtc->active)
  11736. intel_crtc_update_cursor(crtc, state->visible);
  11737. }
  11738. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11739. int pipe)
  11740. {
  11741. struct intel_plane *cursor;
  11742. struct intel_plane_state *state;
  11743. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11744. if (cursor == NULL)
  11745. return NULL;
  11746. state = intel_create_plane_state(&cursor->base);
  11747. if (!state) {
  11748. kfree(cursor);
  11749. return NULL;
  11750. }
  11751. cursor->base.state = &state->base;
  11752. cursor->can_scale = false;
  11753. cursor->max_downscale = 1;
  11754. cursor->pipe = pipe;
  11755. cursor->plane = pipe;
  11756. cursor->check_plane = intel_check_cursor_plane;
  11757. cursor->commit_plane = intel_commit_cursor_plane;
  11758. cursor->disable_plane = intel_disable_cursor_plane;
  11759. drm_universal_plane_init(dev, &cursor->base, 0,
  11760. &intel_plane_funcs,
  11761. intel_cursor_formats,
  11762. ARRAY_SIZE(intel_cursor_formats),
  11763. DRM_PLANE_TYPE_CURSOR);
  11764. if (INTEL_INFO(dev)->gen >= 4) {
  11765. if (!dev->mode_config.rotation_property)
  11766. dev->mode_config.rotation_property =
  11767. drm_mode_create_rotation_property(dev,
  11768. BIT(DRM_ROTATE_0) |
  11769. BIT(DRM_ROTATE_180));
  11770. if (dev->mode_config.rotation_property)
  11771. drm_object_attach_property(&cursor->base.base,
  11772. dev->mode_config.rotation_property,
  11773. state->base.rotation);
  11774. }
  11775. if (INTEL_INFO(dev)->gen >=9)
  11776. state->scaler_id = -1;
  11777. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11778. return &cursor->base;
  11779. }
  11780. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11781. struct intel_crtc_state *crtc_state)
  11782. {
  11783. int i;
  11784. struct intel_scaler *intel_scaler;
  11785. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11786. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11787. intel_scaler = &scaler_state->scalers[i];
  11788. intel_scaler->in_use = 0;
  11789. intel_scaler->id = i;
  11790. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11791. }
  11792. scaler_state->scaler_id = -1;
  11793. }
  11794. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11795. {
  11796. struct drm_i915_private *dev_priv = dev->dev_private;
  11797. struct intel_crtc *intel_crtc;
  11798. struct intel_crtc_state *crtc_state = NULL;
  11799. struct drm_plane *primary = NULL;
  11800. struct drm_plane *cursor = NULL;
  11801. int i, ret;
  11802. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11803. if (intel_crtc == NULL)
  11804. return;
  11805. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11806. if (!crtc_state)
  11807. goto fail;
  11808. intel_crtc->config = crtc_state;
  11809. intel_crtc->base.state = &crtc_state->base;
  11810. crtc_state->base.crtc = &intel_crtc->base;
  11811. /* initialize shared scalers */
  11812. if (INTEL_INFO(dev)->gen >= 9) {
  11813. if (pipe == PIPE_C)
  11814. intel_crtc->num_scalers = 1;
  11815. else
  11816. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11817. skl_init_scalers(dev, intel_crtc, crtc_state);
  11818. }
  11819. primary = intel_primary_plane_create(dev, pipe);
  11820. if (!primary)
  11821. goto fail;
  11822. cursor = intel_cursor_plane_create(dev, pipe);
  11823. if (!cursor)
  11824. goto fail;
  11825. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11826. cursor, &intel_crtc_funcs);
  11827. if (ret)
  11828. goto fail;
  11829. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11830. for (i = 0; i < 256; i++) {
  11831. intel_crtc->lut_r[i] = i;
  11832. intel_crtc->lut_g[i] = i;
  11833. intel_crtc->lut_b[i] = i;
  11834. }
  11835. /*
  11836. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11837. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11838. */
  11839. intel_crtc->pipe = pipe;
  11840. intel_crtc->plane = pipe;
  11841. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11842. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11843. intel_crtc->plane = !pipe;
  11844. }
  11845. intel_crtc->cursor_base = ~0;
  11846. intel_crtc->cursor_cntl = ~0;
  11847. intel_crtc->cursor_size = ~0;
  11848. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11849. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11850. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11851. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11852. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11853. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11854. return;
  11855. fail:
  11856. if (primary)
  11857. drm_plane_cleanup(primary);
  11858. if (cursor)
  11859. drm_plane_cleanup(cursor);
  11860. kfree(crtc_state);
  11861. kfree(intel_crtc);
  11862. }
  11863. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11864. {
  11865. struct drm_encoder *encoder = connector->base.encoder;
  11866. struct drm_device *dev = connector->base.dev;
  11867. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11868. if (!encoder || WARN_ON(!encoder->crtc))
  11869. return INVALID_PIPE;
  11870. return to_intel_crtc(encoder->crtc)->pipe;
  11871. }
  11872. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11873. struct drm_file *file)
  11874. {
  11875. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11876. struct drm_crtc *drmmode_crtc;
  11877. struct intel_crtc *crtc;
  11878. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11879. if (!drmmode_crtc) {
  11880. DRM_ERROR("no such CRTC id\n");
  11881. return -ENOENT;
  11882. }
  11883. crtc = to_intel_crtc(drmmode_crtc);
  11884. pipe_from_crtc_id->pipe = crtc->pipe;
  11885. return 0;
  11886. }
  11887. static int intel_encoder_clones(struct intel_encoder *encoder)
  11888. {
  11889. struct drm_device *dev = encoder->base.dev;
  11890. struct intel_encoder *source_encoder;
  11891. int index_mask = 0;
  11892. int entry = 0;
  11893. for_each_intel_encoder(dev, source_encoder) {
  11894. if (encoders_cloneable(encoder, source_encoder))
  11895. index_mask |= (1 << entry);
  11896. entry++;
  11897. }
  11898. return index_mask;
  11899. }
  11900. static bool has_edp_a(struct drm_device *dev)
  11901. {
  11902. struct drm_i915_private *dev_priv = dev->dev_private;
  11903. if (!IS_MOBILE(dev))
  11904. return false;
  11905. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11906. return false;
  11907. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11908. return false;
  11909. return true;
  11910. }
  11911. static bool intel_crt_present(struct drm_device *dev)
  11912. {
  11913. struct drm_i915_private *dev_priv = dev->dev_private;
  11914. if (INTEL_INFO(dev)->gen >= 9)
  11915. return false;
  11916. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11917. return false;
  11918. if (IS_CHERRYVIEW(dev))
  11919. return false;
  11920. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11921. return false;
  11922. return true;
  11923. }
  11924. static void intel_setup_outputs(struct drm_device *dev)
  11925. {
  11926. struct drm_i915_private *dev_priv = dev->dev_private;
  11927. struct intel_encoder *encoder;
  11928. bool dpd_is_edp = false;
  11929. intel_lvds_init(dev);
  11930. if (intel_crt_present(dev))
  11931. intel_crt_init(dev);
  11932. if (IS_BROXTON(dev)) {
  11933. /*
  11934. * FIXME: Broxton doesn't support port detection via the
  11935. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11936. * detect the ports.
  11937. */
  11938. intel_ddi_init(dev, PORT_A);
  11939. intel_ddi_init(dev, PORT_B);
  11940. intel_ddi_init(dev, PORT_C);
  11941. } else if (HAS_DDI(dev)) {
  11942. int found;
  11943. /*
  11944. * Haswell uses DDI functions to detect digital outputs.
  11945. * On SKL pre-D0 the strap isn't connected, so we assume
  11946. * it's there.
  11947. */
  11948. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11949. /* WaIgnoreDDIAStrap: skl */
  11950. if (found ||
  11951. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11952. intel_ddi_init(dev, PORT_A);
  11953. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11954. * register */
  11955. found = I915_READ(SFUSE_STRAP);
  11956. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11957. intel_ddi_init(dev, PORT_B);
  11958. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11959. intel_ddi_init(dev, PORT_C);
  11960. if (found & SFUSE_STRAP_DDID_DETECTED)
  11961. intel_ddi_init(dev, PORT_D);
  11962. } else if (HAS_PCH_SPLIT(dev)) {
  11963. int found;
  11964. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11965. if (has_edp_a(dev))
  11966. intel_dp_init(dev, DP_A, PORT_A);
  11967. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11968. /* PCH SDVOB multiplex with HDMIB */
  11969. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11970. if (!found)
  11971. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11972. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11973. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11974. }
  11975. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11976. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11977. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11978. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11979. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11980. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11981. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11982. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11983. } else if (IS_VALLEYVIEW(dev)) {
  11984. /*
  11985. * The DP_DETECTED bit is the latched state of the DDC
  11986. * SDA pin at boot. However since eDP doesn't require DDC
  11987. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11988. * eDP ports may have been muxed to an alternate function.
  11989. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11990. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11991. * detect eDP ports.
  11992. */
  11993. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11994. !intel_dp_is_edp(dev, PORT_B))
  11995. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11996. PORT_B);
  11997. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11998. intel_dp_is_edp(dev, PORT_B))
  11999. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  12000. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  12001. !intel_dp_is_edp(dev, PORT_C))
  12002. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  12003. PORT_C);
  12004. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  12005. intel_dp_is_edp(dev, PORT_C))
  12006. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  12007. if (IS_CHERRYVIEW(dev)) {
  12008. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  12009. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  12010. PORT_D);
  12011. /* eDP not supported on port D, so don't check VBT */
  12012. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  12013. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  12014. }
  12015. intel_dsi_init(dev);
  12016. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  12017. bool found = false;
  12018. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12019. DRM_DEBUG_KMS("probing SDVOB\n");
  12020. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  12021. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  12022. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12023. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12024. }
  12025. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  12026. intel_dp_init(dev, DP_B, PORT_B);
  12027. }
  12028. /* Before G4X SDVOC doesn't have its own detect register */
  12029. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12030. DRM_DEBUG_KMS("probing SDVOC\n");
  12031. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  12032. }
  12033. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12034. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  12035. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12036. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12037. }
  12038. if (SUPPORTS_INTEGRATED_DP(dev))
  12039. intel_dp_init(dev, DP_C, PORT_C);
  12040. }
  12041. if (SUPPORTS_INTEGRATED_DP(dev) &&
  12042. (I915_READ(DP_D) & DP_DETECTED))
  12043. intel_dp_init(dev, DP_D, PORT_D);
  12044. } else if (IS_GEN2(dev))
  12045. intel_dvo_init(dev);
  12046. if (SUPPORTS_TV(dev))
  12047. intel_tv_init(dev);
  12048. intel_psr_init(dev);
  12049. for_each_intel_encoder(dev, encoder) {
  12050. encoder->base.possible_crtcs = encoder->crtc_mask;
  12051. encoder->base.possible_clones =
  12052. intel_encoder_clones(encoder);
  12053. }
  12054. intel_init_pch_refclk(dev);
  12055. drm_helper_move_panel_connectors_to_head(dev);
  12056. }
  12057. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12058. {
  12059. struct drm_device *dev = fb->dev;
  12060. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12061. drm_framebuffer_cleanup(fb);
  12062. mutex_lock(&dev->struct_mutex);
  12063. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12064. drm_gem_object_unreference(&intel_fb->obj->base);
  12065. mutex_unlock(&dev->struct_mutex);
  12066. kfree(intel_fb);
  12067. }
  12068. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12069. struct drm_file *file,
  12070. unsigned int *handle)
  12071. {
  12072. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12073. struct drm_i915_gem_object *obj = intel_fb->obj;
  12074. return drm_gem_handle_create(file, &obj->base, handle);
  12075. }
  12076. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12077. .destroy = intel_user_framebuffer_destroy,
  12078. .create_handle = intel_user_framebuffer_create_handle,
  12079. };
  12080. static
  12081. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12082. uint32_t pixel_format)
  12083. {
  12084. u32 gen = INTEL_INFO(dev)->gen;
  12085. if (gen >= 9) {
  12086. /* "The stride in bytes must not exceed the of the size of 8K
  12087. * pixels and 32K bytes."
  12088. */
  12089. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12090. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12091. return 32*1024;
  12092. } else if (gen >= 4) {
  12093. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12094. return 16*1024;
  12095. else
  12096. return 32*1024;
  12097. } else if (gen >= 3) {
  12098. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12099. return 8*1024;
  12100. else
  12101. return 16*1024;
  12102. } else {
  12103. /* XXX DSPC is limited to 4k tiled */
  12104. return 8*1024;
  12105. }
  12106. }
  12107. static int intel_framebuffer_init(struct drm_device *dev,
  12108. struct intel_framebuffer *intel_fb,
  12109. struct drm_mode_fb_cmd2 *mode_cmd,
  12110. struct drm_i915_gem_object *obj)
  12111. {
  12112. unsigned int aligned_height;
  12113. int ret;
  12114. u32 pitch_limit, stride_alignment;
  12115. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12116. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12117. /* Enforce that fb modifier and tiling mode match, but only for
  12118. * X-tiled. This is needed for FBC. */
  12119. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12120. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12121. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12122. return -EINVAL;
  12123. }
  12124. } else {
  12125. if (obj->tiling_mode == I915_TILING_X)
  12126. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12127. else if (obj->tiling_mode == I915_TILING_Y) {
  12128. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12129. return -EINVAL;
  12130. }
  12131. }
  12132. /* Passed in modifier sanity checking. */
  12133. switch (mode_cmd->modifier[0]) {
  12134. case I915_FORMAT_MOD_Y_TILED:
  12135. case I915_FORMAT_MOD_Yf_TILED:
  12136. if (INTEL_INFO(dev)->gen < 9) {
  12137. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12138. mode_cmd->modifier[0]);
  12139. return -EINVAL;
  12140. }
  12141. case DRM_FORMAT_MOD_NONE:
  12142. case I915_FORMAT_MOD_X_TILED:
  12143. break;
  12144. default:
  12145. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12146. mode_cmd->modifier[0]);
  12147. return -EINVAL;
  12148. }
  12149. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12150. mode_cmd->pixel_format);
  12151. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12152. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12153. mode_cmd->pitches[0], stride_alignment);
  12154. return -EINVAL;
  12155. }
  12156. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12157. mode_cmd->pixel_format);
  12158. if (mode_cmd->pitches[0] > pitch_limit) {
  12159. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12160. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12161. "tiled" : "linear",
  12162. mode_cmd->pitches[0], pitch_limit);
  12163. return -EINVAL;
  12164. }
  12165. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12166. mode_cmd->pitches[0] != obj->stride) {
  12167. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12168. mode_cmd->pitches[0], obj->stride);
  12169. return -EINVAL;
  12170. }
  12171. /* Reject formats not supported by any plane early. */
  12172. switch (mode_cmd->pixel_format) {
  12173. case DRM_FORMAT_C8:
  12174. case DRM_FORMAT_RGB565:
  12175. case DRM_FORMAT_XRGB8888:
  12176. case DRM_FORMAT_ARGB8888:
  12177. break;
  12178. case DRM_FORMAT_XRGB1555:
  12179. if (INTEL_INFO(dev)->gen > 3) {
  12180. DRM_DEBUG("unsupported pixel format: %s\n",
  12181. drm_get_format_name(mode_cmd->pixel_format));
  12182. return -EINVAL;
  12183. }
  12184. break;
  12185. case DRM_FORMAT_ABGR8888:
  12186. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12187. DRM_DEBUG("unsupported pixel format: %s\n",
  12188. drm_get_format_name(mode_cmd->pixel_format));
  12189. return -EINVAL;
  12190. }
  12191. break;
  12192. case DRM_FORMAT_XBGR8888:
  12193. case DRM_FORMAT_XRGB2101010:
  12194. case DRM_FORMAT_XBGR2101010:
  12195. if (INTEL_INFO(dev)->gen < 4) {
  12196. DRM_DEBUG("unsupported pixel format: %s\n",
  12197. drm_get_format_name(mode_cmd->pixel_format));
  12198. return -EINVAL;
  12199. }
  12200. break;
  12201. case DRM_FORMAT_ABGR2101010:
  12202. if (!IS_VALLEYVIEW(dev)) {
  12203. DRM_DEBUG("unsupported pixel format: %s\n",
  12204. drm_get_format_name(mode_cmd->pixel_format));
  12205. return -EINVAL;
  12206. }
  12207. break;
  12208. case DRM_FORMAT_YUYV:
  12209. case DRM_FORMAT_UYVY:
  12210. case DRM_FORMAT_YVYU:
  12211. case DRM_FORMAT_VYUY:
  12212. if (INTEL_INFO(dev)->gen < 5) {
  12213. DRM_DEBUG("unsupported pixel format: %s\n",
  12214. drm_get_format_name(mode_cmd->pixel_format));
  12215. return -EINVAL;
  12216. }
  12217. break;
  12218. default:
  12219. DRM_DEBUG("unsupported pixel format: %s\n",
  12220. drm_get_format_name(mode_cmd->pixel_format));
  12221. return -EINVAL;
  12222. }
  12223. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12224. if (mode_cmd->offsets[0] != 0)
  12225. return -EINVAL;
  12226. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12227. mode_cmd->pixel_format,
  12228. mode_cmd->modifier[0]);
  12229. /* FIXME drm helper for size checks (especially planar formats)? */
  12230. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12231. return -EINVAL;
  12232. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12233. intel_fb->obj = obj;
  12234. intel_fb->obj->framebuffer_references++;
  12235. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12236. if (ret) {
  12237. DRM_ERROR("framebuffer init failed %d\n", ret);
  12238. return ret;
  12239. }
  12240. return 0;
  12241. }
  12242. static struct drm_framebuffer *
  12243. intel_user_framebuffer_create(struct drm_device *dev,
  12244. struct drm_file *filp,
  12245. struct drm_mode_fb_cmd2 *mode_cmd)
  12246. {
  12247. struct drm_i915_gem_object *obj;
  12248. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12249. mode_cmd->handles[0]));
  12250. if (&obj->base == NULL)
  12251. return ERR_PTR(-ENOENT);
  12252. return intel_framebuffer_create(dev, mode_cmd, obj);
  12253. }
  12254. #ifndef CONFIG_DRM_I915_FBDEV
  12255. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12256. {
  12257. }
  12258. #endif
  12259. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12260. .fb_create = intel_user_framebuffer_create,
  12261. .output_poll_changed = intel_fbdev_output_poll_changed,
  12262. .atomic_check = intel_atomic_check,
  12263. .atomic_commit = intel_atomic_commit,
  12264. .atomic_state_alloc = intel_atomic_state_alloc,
  12265. .atomic_state_clear = intel_atomic_state_clear,
  12266. };
  12267. /* Set up chip specific display functions */
  12268. static void intel_init_display(struct drm_device *dev)
  12269. {
  12270. struct drm_i915_private *dev_priv = dev->dev_private;
  12271. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12272. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12273. else if (IS_CHERRYVIEW(dev))
  12274. dev_priv->display.find_dpll = chv_find_best_dpll;
  12275. else if (IS_VALLEYVIEW(dev))
  12276. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12277. else if (IS_PINEVIEW(dev))
  12278. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12279. else
  12280. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12281. if (INTEL_INFO(dev)->gen >= 9) {
  12282. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12283. dev_priv->display.get_initial_plane_config =
  12284. skylake_get_initial_plane_config;
  12285. dev_priv->display.crtc_compute_clock =
  12286. haswell_crtc_compute_clock;
  12287. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12288. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12289. dev_priv->display.update_primary_plane =
  12290. skylake_update_primary_plane;
  12291. } else if (HAS_DDI(dev)) {
  12292. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12293. dev_priv->display.get_initial_plane_config =
  12294. ironlake_get_initial_plane_config;
  12295. dev_priv->display.crtc_compute_clock =
  12296. haswell_crtc_compute_clock;
  12297. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12298. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12299. dev_priv->display.update_primary_plane =
  12300. ironlake_update_primary_plane;
  12301. } else if (HAS_PCH_SPLIT(dev)) {
  12302. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12303. dev_priv->display.get_initial_plane_config =
  12304. ironlake_get_initial_plane_config;
  12305. dev_priv->display.crtc_compute_clock =
  12306. ironlake_crtc_compute_clock;
  12307. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12308. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12309. dev_priv->display.update_primary_plane =
  12310. ironlake_update_primary_plane;
  12311. } else if (IS_VALLEYVIEW(dev)) {
  12312. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12313. dev_priv->display.get_initial_plane_config =
  12314. i9xx_get_initial_plane_config;
  12315. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12316. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12317. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12318. dev_priv->display.update_primary_plane =
  12319. i9xx_update_primary_plane;
  12320. } else {
  12321. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12322. dev_priv->display.get_initial_plane_config =
  12323. i9xx_get_initial_plane_config;
  12324. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12325. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12326. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12327. dev_priv->display.update_primary_plane =
  12328. i9xx_update_primary_plane;
  12329. }
  12330. /* Returns the core display clock speed */
  12331. if (IS_SKYLAKE(dev))
  12332. dev_priv->display.get_display_clock_speed =
  12333. skylake_get_display_clock_speed;
  12334. else if (IS_BROADWELL(dev))
  12335. dev_priv->display.get_display_clock_speed =
  12336. broadwell_get_display_clock_speed;
  12337. else if (IS_HASWELL(dev))
  12338. dev_priv->display.get_display_clock_speed =
  12339. haswell_get_display_clock_speed;
  12340. else if (IS_VALLEYVIEW(dev))
  12341. dev_priv->display.get_display_clock_speed =
  12342. valleyview_get_display_clock_speed;
  12343. else if (IS_GEN5(dev))
  12344. dev_priv->display.get_display_clock_speed =
  12345. ilk_get_display_clock_speed;
  12346. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12347. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12348. dev_priv->display.get_display_clock_speed =
  12349. i945_get_display_clock_speed;
  12350. else if (IS_GM45(dev))
  12351. dev_priv->display.get_display_clock_speed =
  12352. gm45_get_display_clock_speed;
  12353. else if (IS_CRESTLINE(dev))
  12354. dev_priv->display.get_display_clock_speed =
  12355. i965gm_get_display_clock_speed;
  12356. else if (IS_PINEVIEW(dev))
  12357. dev_priv->display.get_display_clock_speed =
  12358. pnv_get_display_clock_speed;
  12359. else if (IS_G33(dev) || IS_G4X(dev))
  12360. dev_priv->display.get_display_clock_speed =
  12361. g33_get_display_clock_speed;
  12362. else if (IS_I915G(dev))
  12363. dev_priv->display.get_display_clock_speed =
  12364. i915_get_display_clock_speed;
  12365. else if (IS_I945GM(dev) || IS_845G(dev))
  12366. dev_priv->display.get_display_clock_speed =
  12367. i9xx_misc_get_display_clock_speed;
  12368. else if (IS_PINEVIEW(dev))
  12369. dev_priv->display.get_display_clock_speed =
  12370. pnv_get_display_clock_speed;
  12371. else if (IS_I915GM(dev))
  12372. dev_priv->display.get_display_clock_speed =
  12373. i915gm_get_display_clock_speed;
  12374. else if (IS_I865G(dev))
  12375. dev_priv->display.get_display_clock_speed =
  12376. i865_get_display_clock_speed;
  12377. else if (IS_I85X(dev))
  12378. dev_priv->display.get_display_clock_speed =
  12379. i85x_get_display_clock_speed;
  12380. else { /* 830 */
  12381. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12382. dev_priv->display.get_display_clock_speed =
  12383. i830_get_display_clock_speed;
  12384. }
  12385. if (IS_GEN5(dev)) {
  12386. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12387. } else if (IS_GEN6(dev)) {
  12388. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12389. } else if (IS_IVYBRIDGE(dev)) {
  12390. /* FIXME: detect B0+ stepping and use auto training */
  12391. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12392. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12393. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12394. if (IS_BROADWELL(dev))
  12395. dev_priv->display.modeset_global_resources =
  12396. broadwell_modeset_global_resources;
  12397. } else if (IS_VALLEYVIEW(dev)) {
  12398. dev_priv->display.modeset_global_resources =
  12399. valleyview_modeset_global_resources;
  12400. } else if (IS_BROXTON(dev)) {
  12401. dev_priv->display.modeset_global_resources =
  12402. broxton_modeset_global_resources;
  12403. }
  12404. switch (INTEL_INFO(dev)->gen) {
  12405. case 2:
  12406. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12407. break;
  12408. case 3:
  12409. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12410. break;
  12411. case 4:
  12412. case 5:
  12413. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12414. break;
  12415. case 6:
  12416. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12417. break;
  12418. case 7:
  12419. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12420. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12421. break;
  12422. case 9:
  12423. /* Drop through - unsupported since execlist only. */
  12424. default:
  12425. /* Default just returns -ENODEV to indicate unsupported */
  12426. dev_priv->display.queue_flip = intel_default_queue_flip;
  12427. }
  12428. intel_panel_init_backlight_funcs(dev);
  12429. mutex_init(&dev_priv->pps_mutex);
  12430. }
  12431. /*
  12432. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12433. * resume, or other times. This quirk makes sure that's the case for
  12434. * affected systems.
  12435. */
  12436. static void quirk_pipea_force(struct drm_device *dev)
  12437. {
  12438. struct drm_i915_private *dev_priv = dev->dev_private;
  12439. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12440. DRM_INFO("applying pipe a force quirk\n");
  12441. }
  12442. static void quirk_pipeb_force(struct drm_device *dev)
  12443. {
  12444. struct drm_i915_private *dev_priv = dev->dev_private;
  12445. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12446. DRM_INFO("applying pipe b force quirk\n");
  12447. }
  12448. /*
  12449. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12450. */
  12451. static void quirk_ssc_force_disable(struct drm_device *dev)
  12452. {
  12453. struct drm_i915_private *dev_priv = dev->dev_private;
  12454. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12455. DRM_INFO("applying lvds SSC disable quirk\n");
  12456. }
  12457. /*
  12458. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12459. * brightness value
  12460. */
  12461. static void quirk_invert_brightness(struct drm_device *dev)
  12462. {
  12463. struct drm_i915_private *dev_priv = dev->dev_private;
  12464. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12465. DRM_INFO("applying inverted panel brightness quirk\n");
  12466. }
  12467. /* Some VBT's incorrectly indicate no backlight is present */
  12468. static void quirk_backlight_present(struct drm_device *dev)
  12469. {
  12470. struct drm_i915_private *dev_priv = dev->dev_private;
  12471. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12472. DRM_INFO("applying backlight present quirk\n");
  12473. }
  12474. struct intel_quirk {
  12475. int device;
  12476. int subsystem_vendor;
  12477. int subsystem_device;
  12478. void (*hook)(struct drm_device *dev);
  12479. };
  12480. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12481. struct intel_dmi_quirk {
  12482. void (*hook)(struct drm_device *dev);
  12483. const struct dmi_system_id (*dmi_id_list)[];
  12484. };
  12485. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12486. {
  12487. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12488. return 1;
  12489. }
  12490. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12491. {
  12492. .dmi_id_list = &(const struct dmi_system_id[]) {
  12493. {
  12494. .callback = intel_dmi_reverse_brightness,
  12495. .ident = "NCR Corporation",
  12496. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12497. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12498. },
  12499. },
  12500. { } /* terminating entry */
  12501. },
  12502. .hook = quirk_invert_brightness,
  12503. },
  12504. };
  12505. static struct intel_quirk intel_quirks[] = {
  12506. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12507. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12508. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12509. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12510. /* 830 needs to leave pipe A & dpll A up */
  12511. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12512. /* 830 needs to leave pipe B & dpll B up */
  12513. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12514. /* Lenovo U160 cannot use SSC on LVDS */
  12515. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12516. /* Sony Vaio Y cannot use SSC on LVDS */
  12517. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12518. /* Acer Aspire 5734Z must invert backlight brightness */
  12519. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12520. /* Acer/eMachines G725 */
  12521. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12522. /* Acer/eMachines e725 */
  12523. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12524. /* Acer/Packard Bell NCL20 */
  12525. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12526. /* Acer Aspire 4736Z */
  12527. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12528. /* Acer Aspire 5336 */
  12529. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12530. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12531. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12532. /* Acer C720 Chromebook (Core i3 4005U) */
  12533. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12534. /* Apple Macbook 2,1 (Core 2 T7400) */
  12535. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12536. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12537. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12538. /* HP Chromebook 14 (Celeron 2955U) */
  12539. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12540. /* Dell Chromebook 11 */
  12541. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12542. };
  12543. static void intel_init_quirks(struct drm_device *dev)
  12544. {
  12545. struct pci_dev *d = dev->pdev;
  12546. int i;
  12547. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12548. struct intel_quirk *q = &intel_quirks[i];
  12549. if (d->device == q->device &&
  12550. (d->subsystem_vendor == q->subsystem_vendor ||
  12551. q->subsystem_vendor == PCI_ANY_ID) &&
  12552. (d->subsystem_device == q->subsystem_device ||
  12553. q->subsystem_device == PCI_ANY_ID))
  12554. q->hook(dev);
  12555. }
  12556. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12557. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12558. intel_dmi_quirks[i].hook(dev);
  12559. }
  12560. }
  12561. /* Disable the VGA plane that we never use */
  12562. static void i915_disable_vga(struct drm_device *dev)
  12563. {
  12564. struct drm_i915_private *dev_priv = dev->dev_private;
  12565. u8 sr1;
  12566. u32 vga_reg = i915_vgacntrl_reg(dev);
  12567. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12568. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12569. outb(SR01, VGA_SR_INDEX);
  12570. sr1 = inb(VGA_SR_DATA);
  12571. outb(sr1 | 1<<5, VGA_SR_DATA);
  12572. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12573. udelay(300);
  12574. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12575. POSTING_READ(vga_reg);
  12576. }
  12577. void intel_modeset_init_hw(struct drm_device *dev)
  12578. {
  12579. intel_update_cdclk(dev);
  12580. intel_prepare_ddi(dev);
  12581. intel_init_clock_gating(dev);
  12582. intel_enable_gt_powersave(dev);
  12583. }
  12584. void intel_modeset_init(struct drm_device *dev)
  12585. {
  12586. struct drm_i915_private *dev_priv = dev->dev_private;
  12587. int sprite, ret;
  12588. enum pipe pipe;
  12589. struct intel_crtc *crtc;
  12590. drm_mode_config_init(dev);
  12591. dev->mode_config.min_width = 0;
  12592. dev->mode_config.min_height = 0;
  12593. dev->mode_config.preferred_depth = 24;
  12594. dev->mode_config.prefer_shadow = 1;
  12595. dev->mode_config.allow_fb_modifiers = true;
  12596. dev->mode_config.funcs = &intel_mode_funcs;
  12597. intel_init_quirks(dev);
  12598. intel_init_pm(dev);
  12599. if (INTEL_INFO(dev)->num_pipes == 0)
  12600. return;
  12601. intel_init_display(dev);
  12602. intel_init_audio(dev);
  12603. if (IS_GEN2(dev)) {
  12604. dev->mode_config.max_width = 2048;
  12605. dev->mode_config.max_height = 2048;
  12606. } else if (IS_GEN3(dev)) {
  12607. dev->mode_config.max_width = 4096;
  12608. dev->mode_config.max_height = 4096;
  12609. } else {
  12610. dev->mode_config.max_width = 8192;
  12611. dev->mode_config.max_height = 8192;
  12612. }
  12613. if (IS_845G(dev) || IS_I865G(dev)) {
  12614. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12615. dev->mode_config.cursor_height = 1023;
  12616. } else if (IS_GEN2(dev)) {
  12617. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12618. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12619. } else {
  12620. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12621. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12622. }
  12623. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12624. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12625. INTEL_INFO(dev)->num_pipes,
  12626. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12627. for_each_pipe(dev_priv, pipe) {
  12628. intel_crtc_init(dev, pipe);
  12629. for_each_sprite(dev_priv, pipe, sprite) {
  12630. ret = intel_plane_init(dev, pipe, sprite);
  12631. if (ret)
  12632. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12633. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12634. }
  12635. }
  12636. intel_init_dpio(dev);
  12637. intel_shared_dpll_init(dev);
  12638. /* Just disable it once at startup */
  12639. i915_disable_vga(dev);
  12640. intel_setup_outputs(dev);
  12641. /* Just in case the BIOS is doing something questionable. */
  12642. intel_fbc_disable(dev);
  12643. drm_modeset_lock_all(dev);
  12644. intel_modeset_setup_hw_state(dev, false);
  12645. drm_modeset_unlock_all(dev);
  12646. for_each_intel_crtc(dev, crtc) {
  12647. if (!crtc->active)
  12648. continue;
  12649. /*
  12650. * Note that reserving the BIOS fb up front prevents us
  12651. * from stuffing other stolen allocations like the ring
  12652. * on top. This prevents some ugliness at boot time, and
  12653. * can even allow for smooth boot transitions if the BIOS
  12654. * fb is large enough for the active pipe configuration.
  12655. */
  12656. if (dev_priv->display.get_initial_plane_config) {
  12657. dev_priv->display.get_initial_plane_config(crtc,
  12658. &crtc->plane_config);
  12659. /*
  12660. * If the fb is shared between multiple heads, we'll
  12661. * just get the first one.
  12662. */
  12663. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12664. }
  12665. }
  12666. }
  12667. static void intel_enable_pipe_a(struct drm_device *dev)
  12668. {
  12669. struct intel_connector *connector;
  12670. struct drm_connector *crt = NULL;
  12671. struct intel_load_detect_pipe load_detect_temp;
  12672. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12673. /* We can't just switch on the pipe A, we need to set things up with a
  12674. * proper mode and output configuration. As a gross hack, enable pipe A
  12675. * by enabling the load detect pipe once. */
  12676. for_each_intel_connector(dev, connector) {
  12677. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12678. crt = &connector->base;
  12679. break;
  12680. }
  12681. }
  12682. if (!crt)
  12683. return;
  12684. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12685. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12686. }
  12687. static bool
  12688. intel_check_plane_mapping(struct intel_crtc *crtc)
  12689. {
  12690. struct drm_device *dev = crtc->base.dev;
  12691. struct drm_i915_private *dev_priv = dev->dev_private;
  12692. u32 reg, val;
  12693. if (INTEL_INFO(dev)->num_pipes == 1)
  12694. return true;
  12695. reg = DSPCNTR(!crtc->plane);
  12696. val = I915_READ(reg);
  12697. if ((val & DISPLAY_PLANE_ENABLE) &&
  12698. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12699. return false;
  12700. return true;
  12701. }
  12702. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12703. {
  12704. struct drm_device *dev = crtc->base.dev;
  12705. struct drm_i915_private *dev_priv = dev->dev_private;
  12706. struct intel_encoder *encoder;
  12707. u32 reg;
  12708. bool enable;
  12709. /* Clear any frame start delays used for debugging left by the BIOS */
  12710. reg = PIPECONF(crtc->config->cpu_transcoder);
  12711. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12712. /* restore vblank interrupts to correct state */
  12713. drm_crtc_vblank_reset(&crtc->base);
  12714. if (crtc->active) {
  12715. update_scanline_offset(crtc);
  12716. drm_crtc_vblank_on(&crtc->base);
  12717. }
  12718. /* We need to sanitize the plane -> pipe mapping first because this will
  12719. * disable the crtc (and hence change the state) if it is wrong. Note
  12720. * that gen4+ has a fixed plane -> pipe mapping. */
  12721. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12722. bool plane;
  12723. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12724. crtc->base.base.id);
  12725. /* Pipe has the wrong plane attached and the plane is active.
  12726. * Temporarily change the plane mapping and disable everything
  12727. * ... */
  12728. plane = crtc->plane;
  12729. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12730. crtc->plane = !plane;
  12731. intel_crtc_disable_noatomic(&crtc->base);
  12732. crtc->plane = plane;
  12733. }
  12734. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12735. crtc->pipe == PIPE_A && !crtc->active) {
  12736. /* BIOS forgot to enable pipe A, this mostly happens after
  12737. * resume. Force-enable the pipe to fix this, the update_dpms
  12738. * call below we restore the pipe to the right state, but leave
  12739. * the required bits on. */
  12740. intel_enable_pipe_a(dev);
  12741. }
  12742. /* Adjust the state of the output pipe according to whether we
  12743. * have active connectors/encoders. */
  12744. enable = false;
  12745. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12746. enable |= encoder->connectors_active;
  12747. if (!enable)
  12748. intel_crtc_disable_noatomic(&crtc->base);
  12749. if (crtc->active != crtc->base.state->active) {
  12750. /* This can happen either due to bugs in the get_hw_state
  12751. * functions or because of calls to intel_crtc_disable_noatomic,
  12752. * or because the pipe is force-enabled due to the
  12753. * pipe A quirk. */
  12754. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12755. crtc->base.base.id,
  12756. crtc->base.state->enable ? "enabled" : "disabled",
  12757. crtc->active ? "enabled" : "disabled");
  12758. crtc->base.state->enable = crtc->active;
  12759. crtc->base.state->active = crtc->active;
  12760. crtc->base.enabled = crtc->active;
  12761. /* Because we only establish the connector -> encoder ->
  12762. * crtc links if something is active, this means the
  12763. * crtc is now deactivated. Break the links. connector
  12764. * -> encoder links are only establish when things are
  12765. * actually up, hence no need to break them. */
  12766. WARN_ON(crtc->active);
  12767. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12768. WARN_ON(encoder->connectors_active);
  12769. encoder->base.crtc = NULL;
  12770. }
  12771. }
  12772. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12773. /*
  12774. * We start out with underrun reporting disabled to avoid races.
  12775. * For correct bookkeeping mark this on active crtcs.
  12776. *
  12777. * Also on gmch platforms we dont have any hardware bits to
  12778. * disable the underrun reporting. Which means we need to start
  12779. * out with underrun reporting disabled also on inactive pipes,
  12780. * since otherwise we'll complain about the garbage we read when
  12781. * e.g. coming up after runtime pm.
  12782. *
  12783. * No protection against concurrent access is required - at
  12784. * worst a fifo underrun happens which also sets this to false.
  12785. */
  12786. crtc->cpu_fifo_underrun_disabled = true;
  12787. crtc->pch_fifo_underrun_disabled = true;
  12788. }
  12789. }
  12790. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12791. {
  12792. struct intel_connector *connector;
  12793. struct drm_device *dev = encoder->base.dev;
  12794. /* We need to check both for a crtc link (meaning that the
  12795. * encoder is active and trying to read from a pipe) and the
  12796. * pipe itself being active. */
  12797. bool has_active_crtc = encoder->base.crtc &&
  12798. to_intel_crtc(encoder->base.crtc)->active;
  12799. if (encoder->connectors_active && !has_active_crtc) {
  12800. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12801. encoder->base.base.id,
  12802. encoder->base.name);
  12803. /* Connector is active, but has no active pipe. This is
  12804. * fallout from our resume register restoring. Disable
  12805. * the encoder manually again. */
  12806. if (encoder->base.crtc) {
  12807. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12808. encoder->base.base.id,
  12809. encoder->base.name);
  12810. encoder->disable(encoder);
  12811. if (encoder->post_disable)
  12812. encoder->post_disable(encoder);
  12813. }
  12814. encoder->base.crtc = NULL;
  12815. encoder->connectors_active = false;
  12816. /* Inconsistent output/port/pipe state happens presumably due to
  12817. * a bug in one of the get_hw_state functions. Or someplace else
  12818. * in our code, like the register restore mess on resume. Clamp
  12819. * things to off as a safer default. */
  12820. for_each_intel_connector(dev, connector) {
  12821. if (connector->encoder != encoder)
  12822. continue;
  12823. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12824. connector->base.encoder = NULL;
  12825. }
  12826. }
  12827. /* Enabled encoders without active connectors will be fixed in
  12828. * the crtc fixup. */
  12829. }
  12830. void i915_redisable_vga_power_on(struct drm_device *dev)
  12831. {
  12832. struct drm_i915_private *dev_priv = dev->dev_private;
  12833. u32 vga_reg = i915_vgacntrl_reg(dev);
  12834. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12835. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12836. i915_disable_vga(dev);
  12837. }
  12838. }
  12839. void i915_redisable_vga(struct drm_device *dev)
  12840. {
  12841. struct drm_i915_private *dev_priv = dev->dev_private;
  12842. /* This function can be called both from intel_modeset_setup_hw_state or
  12843. * at a very early point in our resume sequence, where the power well
  12844. * structures are not yet restored. Since this function is at a very
  12845. * paranoid "someone might have enabled VGA while we were not looking"
  12846. * level, just check if the power well is enabled instead of trying to
  12847. * follow the "don't touch the power well if we don't need it" policy
  12848. * the rest of the driver uses. */
  12849. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12850. return;
  12851. i915_redisable_vga_power_on(dev);
  12852. }
  12853. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12854. {
  12855. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12856. if (!crtc->active)
  12857. return false;
  12858. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12859. }
  12860. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12861. {
  12862. struct drm_i915_private *dev_priv = dev->dev_private;
  12863. enum pipe pipe;
  12864. struct intel_crtc *crtc;
  12865. struct intel_encoder *encoder;
  12866. struct intel_connector *connector;
  12867. int i;
  12868. for_each_intel_crtc(dev, crtc) {
  12869. struct drm_plane *primary = crtc->base.primary;
  12870. struct intel_plane_state *plane_state;
  12871. memset(crtc->config, 0, sizeof(*crtc->config));
  12872. crtc->config->base.crtc = &crtc->base;
  12873. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12874. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12875. crtc->config);
  12876. crtc->base.state->enable = crtc->active;
  12877. crtc->base.state->active = crtc->active;
  12878. crtc->base.enabled = crtc->active;
  12879. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12880. plane_state = to_intel_plane_state(primary->state);
  12881. plane_state->visible = primary_get_hw_state(crtc);
  12882. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12883. crtc->base.base.id,
  12884. crtc->active ? "enabled" : "disabled");
  12885. }
  12886. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12887. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12888. pll->on = pll->get_hw_state(dev_priv, pll,
  12889. &pll->config.hw_state);
  12890. pll->active = 0;
  12891. pll->config.crtc_mask = 0;
  12892. for_each_intel_crtc(dev, crtc) {
  12893. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12894. pll->active++;
  12895. pll->config.crtc_mask |= 1 << crtc->pipe;
  12896. }
  12897. }
  12898. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12899. pll->name, pll->config.crtc_mask, pll->on);
  12900. if (pll->config.crtc_mask)
  12901. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12902. }
  12903. for_each_intel_encoder(dev, encoder) {
  12904. pipe = 0;
  12905. if (encoder->get_hw_state(encoder, &pipe)) {
  12906. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12907. encoder->base.crtc = &crtc->base;
  12908. encoder->get_config(encoder, crtc->config);
  12909. } else {
  12910. encoder->base.crtc = NULL;
  12911. }
  12912. encoder->connectors_active = false;
  12913. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12914. encoder->base.base.id,
  12915. encoder->base.name,
  12916. encoder->base.crtc ? "enabled" : "disabled",
  12917. pipe_name(pipe));
  12918. }
  12919. for_each_intel_connector(dev, connector) {
  12920. if (connector->get_hw_state(connector)) {
  12921. connector->base.dpms = DRM_MODE_DPMS_ON;
  12922. connector->encoder->connectors_active = true;
  12923. connector->base.encoder = &connector->encoder->base;
  12924. } else {
  12925. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12926. connector->base.encoder = NULL;
  12927. }
  12928. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12929. connector->base.base.id,
  12930. connector->base.name,
  12931. connector->base.encoder ? "enabled" : "disabled");
  12932. }
  12933. }
  12934. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12935. * and i915 state tracking structures. */
  12936. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12937. bool force_restore)
  12938. {
  12939. struct drm_i915_private *dev_priv = dev->dev_private;
  12940. enum pipe pipe;
  12941. struct intel_crtc *crtc;
  12942. struct intel_encoder *encoder;
  12943. int i;
  12944. intel_modeset_readout_hw_state(dev);
  12945. /*
  12946. * Now that we have the config, copy it to each CRTC struct
  12947. * Note that this could go away if we move to using crtc_config
  12948. * checking everywhere.
  12949. */
  12950. for_each_intel_crtc(dev, crtc) {
  12951. if (crtc->active && i915.fastboot) {
  12952. intel_mode_from_pipe_config(&crtc->base.mode,
  12953. crtc->config);
  12954. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12955. crtc->base.base.id);
  12956. drm_mode_debug_printmodeline(&crtc->base.mode);
  12957. }
  12958. }
  12959. /* HW state is read out, now we need to sanitize this mess. */
  12960. for_each_intel_encoder(dev, encoder) {
  12961. intel_sanitize_encoder(encoder);
  12962. }
  12963. for_each_pipe(dev_priv, pipe) {
  12964. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12965. intel_sanitize_crtc(crtc);
  12966. intel_dump_pipe_config(crtc, crtc->config,
  12967. "[setup_hw_state]");
  12968. }
  12969. intel_modeset_update_connector_atomic_state(dev);
  12970. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12971. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12972. if (!pll->on || pll->active)
  12973. continue;
  12974. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12975. pll->disable(dev_priv, pll);
  12976. pll->on = false;
  12977. }
  12978. if (IS_GEN9(dev))
  12979. skl_wm_get_hw_state(dev);
  12980. else if (HAS_PCH_SPLIT(dev))
  12981. ilk_wm_get_hw_state(dev);
  12982. if (force_restore) {
  12983. i915_redisable_vga(dev);
  12984. /*
  12985. * We need to use raw interfaces for restoring state to avoid
  12986. * checking (bogus) intermediate states.
  12987. */
  12988. for_each_pipe(dev_priv, pipe) {
  12989. struct drm_crtc *crtc =
  12990. dev_priv->pipe_to_crtc_mapping[pipe];
  12991. intel_crtc_restore_mode(crtc);
  12992. }
  12993. } else {
  12994. intel_modeset_update_staged_output_state(dev);
  12995. }
  12996. intel_modeset_check_state(dev);
  12997. }
  12998. void intel_modeset_gem_init(struct drm_device *dev)
  12999. {
  13000. struct drm_i915_private *dev_priv = dev->dev_private;
  13001. struct drm_crtc *c;
  13002. struct drm_i915_gem_object *obj;
  13003. int ret;
  13004. mutex_lock(&dev->struct_mutex);
  13005. intel_init_gt_powersave(dev);
  13006. mutex_unlock(&dev->struct_mutex);
  13007. /*
  13008. * There may be no VBT; and if the BIOS enabled SSC we can
  13009. * just keep using it to avoid unnecessary flicker. Whereas if the
  13010. * BIOS isn't using it, don't assume it will work even if the VBT
  13011. * indicates as much.
  13012. */
  13013. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13014. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13015. DREF_SSC1_ENABLE);
  13016. intel_modeset_init_hw(dev);
  13017. intel_setup_overlay(dev);
  13018. /*
  13019. * Make sure any fbs we allocated at startup are properly
  13020. * pinned & fenced. When we do the allocation it's too early
  13021. * for this.
  13022. */
  13023. for_each_crtc(dev, c) {
  13024. obj = intel_fb_obj(c->primary->fb);
  13025. if (obj == NULL)
  13026. continue;
  13027. mutex_lock(&dev->struct_mutex);
  13028. ret = intel_pin_and_fence_fb_obj(c->primary,
  13029. c->primary->fb,
  13030. c->primary->state,
  13031. NULL);
  13032. mutex_unlock(&dev->struct_mutex);
  13033. if (ret) {
  13034. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13035. to_intel_crtc(c)->pipe);
  13036. drm_framebuffer_unreference(c->primary->fb);
  13037. c->primary->fb = NULL;
  13038. c->primary->crtc = c->primary->state->crtc = NULL;
  13039. update_state_fb(c->primary);
  13040. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13041. }
  13042. }
  13043. intel_backlight_register(dev);
  13044. }
  13045. void intel_connector_unregister(struct intel_connector *intel_connector)
  13046. {
  13047. struct drm_connector *connector = &intel_connector->base;
  13048. intel_panel_destroy_backlight(connector);
  13049. drm_connector_unregister(connector);
  13050. }
  13051. void intel_modeset_cleanup(struct drm_device *dev)
  13052. {
  13053. struct drm_i915_private *dev_priv = dev->dev_private;
  13054. struct drm_connector *connector;
  13055. intel_disable_gt_powersave(dev);
  13056. intel_backlight_unregister(dev);
  13057. /*
  13058. * Interrupts and polling as the first thing to avoid creating havoc.
  13059. * Too much stuff here (turning of connectors, ...) would
  13060. * experience fancy races otherwise.
  13061. */
  13062. intel_irq_uninstall(dev_priv);
  13063. /*
  13064. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13065. * poll handlers. Hence disable polling after hpd handling is shut down.
  13066. */
  13067. drm_kms_helper_poll_fini(dev);
  13068. mutex_lock(&dev->struct_mutex);
  13069. intel_unregister_dsm_handler();
  13070. intel_fbc_disable(dev);
  13071. mutex_unlock(&dev->struct_mutex);
  13072. /* flush any delayed tasks or pending work */
  13073. flush_scheduled_work();
  13074. /* destroy the backlight and sysfs files before encoders/connectors */
  13075. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13076. struct intel_connector *intel_connector;
  13077. intel_connector = to_intel_connector(connector);
  13078. intel_connector->unregister(intel_connector);
  13079. }
  13080. drm_mode_config_cleanup(dev);
  13081. intel_cleanup_overlay(dev);
  13082. mutex_lock(&dev->struct_mutex);
  13083. intel_cleanup_gt_powersave(dev);
  13084. mutex_unlock(&dev->struct_mutex);
  13085. }
  13086. /*
  13087. * Return which encoder is currently attached for connector.
  13088. */
  13089. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13090. {
  13091. return &intel_attached_encoder(connector)->base;
  13092. }
  13093. void intel_connector_attach_encoder(struct intel_connector *connector,
  13094. struct intel_encoder *encoder)
  13095. {
  13096. connector->encoder = encoder;
  13097. drm_mode_connector_attach_encoder(&connector->base,
  13098. &encoder->base);
  13099. }
  13100. /*
  13101. * set vga decode state - true == enable VGA decode
  13102. */
  13103. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13104. {
  13105. struct drm_i915_private *dev_priv = dev->dev_private;
  13106. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13107. u16 gmch_ctrl;
  13108. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13109. DRM_ERROR("failed to read control word\n");
  13110. return -EIO;
  13111. }
  13112. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13113. return 0;
  13114. if (state)
  13115. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13116. else
  13117. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13118. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13119. DRM_ERROR("failed to write control word\n");
  13120. return -EIO;
  13121. }
  13122. return 0;
  13123. }
  13124. struct intel_display_error_state {
  13125. u32 power_well_driver;
  13126. int num_transcoders;
  13127. struct intel_cursor_error_state {
  13128. u32 control;
  13129. u32 position;
  13130. u32 base;
  13131. u32 size;
  13132. } cursor[I915_MAX_PIPES];
  13133. struct intel_pipe_error_state {
  13134. bool power_domain_on;
  13135. u32 source;
  13136. u32 stat;
  13137. } pipe[I915_MAX_PIPES];
  13138. struct intel_plane_error_state {
  13139. u32 control;
  13140. u32 stride;
  13141. u32 size;
  13142. u32 pos;
  13143. u32 addr;
  13144. u32 surface;
  13145. u32 tile_offset;
  13146. } plane[I915_MAX_PIPES];
  13147. struct intel_transcoder_error_state {
  13148. bool power_domain_on;
  13149. enum transcoder cpu_transcoder;
  13150. u32 conf;
  13151. u32 htotal;
  13152. u32 hblank;
  13153. u32 hsync;
  13154. u32 vtotal;
  13155. u32 vblank;
  13156. u32 vsync;
  13157. } transcoder[4];
  13158. };
  13159. struct intel_display_error_state *
  13160. intel_display_capture_error_state(struct drm_device *dev)
  13161. {
  13162. struct drm_i915_private *dev_priv = dev->dev_private;
  13163. struct intel_display_error_state *error;
  13164. int transcoders[] = {
  13165. TRANSCODER_A,
  13166. TRANSCODER_B,
  13167. TRANSCODER_C,
  13168. TRANSCODER_EDP,
  13169. };
  13170. int i;
  13171. if (INTEL_INFO(dev)->num_pipes == 0)
  13172. return NULL;
  13173. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13174. if (error == NULL)
  13175. return NULL;
  13176. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13177. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13178. for_each_pipe(dev_priv, i) {
  13179. error->pipe[i].power_domain_on =
  13180. __intel_display_power_is_enabled(dev_priv,
  13181. POWER_DOMAIN_PIPE(i));
  13182. if (!error->pipe[i].power_domain_on)
  13183. continue;
  13184. error->cursor[i].control = I915_READ(CURCNTR(i));
  13185. error->cursor[i].position = I915_READ(CURPOS(i));
  13186. error->cursor[i].base = I915_READ(CURBASE(i));
  13187. error->plane[i].control = I915_READ(DSPCNTR(i));
  13188. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13189. if (INTEL_INFO(dev)->gen <= 3) {
  13190. error->plane[i].size = I915_READ(DSPSIZE(i));
  13191. error->plane[i].pos = I915_READ(DSPPOS(i));
  13192. }
  13193. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13194. error->plane[i].addr = I915_READ(DSPADDR(i));
  13195. if (INTEL_INFO(dev)->gen >= 4) {
  13196. error->plane[i].surface = I915_READ(DSPSURF(i));
  13197. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13198. }
  13199. error->pipe[i].source = I915_READ(PIPESRC(i));
  13200. if (HAS_GMCH_DISPLAY(dev))
  13201. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13202. }
  13203. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13204. if (HAS_DDI(dev_priv->dev))
  13205. error->num_transcoders++; /* Account for eDP. */
  13206. for (i = 0; i < error->num_transcoders; i++) {
  13207. enum transcoder cpu_transcoder = transcoders[i];
  13208. error->transcoder[i].power_domain_on =
  13209. __intel_display_power_is_enabled(dev_priv,
  13210. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13211. if (!error->transcoder[i].power_domain_on)
  13212. continue;
  13213. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13214. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13215. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13216. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13217. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13218. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13219. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13220. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13221. }
  13222. return error;
  13223. }
  13224. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13225. void
  13226. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13227. struct drm_device *dev,
  13228. struct intel_display_error_state *error)
  13229. {
  13230. struct drm_i915_private *dev_priv = dev->dev_private;
  13231. int i;
  13232. if (!error)
  13233. return;
  13234. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13235. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13236. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13237. error->power_well_driver);
  13238. for_each_pipe(dev_priv, i) {
  13239. err_printf(m, "Pipe [%d]:\n", i);
  13240. err_printf(m, " Power: %s\n",
  13241. error->pipe[i].power_domain_on ? "on" : "off");
  13242. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13243. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13244. err_printf(m, "Plane [%d]:\n", i);
  13245. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13246. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13247. if (INTEL_INFO(dev)->gen <= 3) {
  13248. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13249. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13250. }
  13251. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13252. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13253. if (INTEL_INFO(dev)->gen >= 4) {
  13254. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13255. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13256. }
  13257. err_printf(m, "Cursor [%d]:\n", i);
  13258. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13259. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13260. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13261. }
  13262. for (i = 0; i < error->num_transcoders; i++) {
  13263. err_printf(m, "CPU transcoder: %c\n",
  13264. transcoder_name(error->transcoder[i].cpu_transcoder));
  13265. err_printf(m, " Power: %s\n",
  13266. error->transcoder[i].power_domain_on ? "on" : "off");
  13267. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13268. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13269. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13270. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13271. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13272. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13273. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13274. }
  13275. }
  13276. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13277. {
  13278. struct intel_crtc *crtc;
  13279. for_each_intel_crtc(dev, crtc) {
  13280. struct intel_unpin_work *work;
  13281. spin_lock_irq(&dev->event_lock);
  13282. work = crtc->unpin_work;
  13283. if (work && work->event &&
  13284. work->event->base.file_priv == file) {
  13285. kfree(work->event);
  13286. work->event = NULL;
  13287. }
  13288. spin_unlock_irq(&dev->event_lock);
  13289. }
  13290. }