intel_display.c 437 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889
  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. static int
  3691. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3692. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3693. int src_w, int src_h, int dst_w, int dst_h)
  3694. {
  3695. struct intel_crtc_scaler_state *scaler_state =
  3696. &crtc_state->scaler_state;
  3697. struct intel_crtc *intel_crtc =
  3698. to_intel_crtc(crtc_state->base.crtc);
  3699. int need_scaling;
  3700. need_scaling = intel_rotation_90_or_270(rotation) ?
  3701. (src_h != dst_w || src_w != dst_h):
  3702. (src_w != dst_w || src_h != dst_h);
  3703. /*
  3704. * if plane is being disabled or scaler is no more required or force detach
  3705. * - free scaler binded to this plane/crtc
  3706. * - in order to do this, update crtc->scaler_usage
  3707. *
  3708. * Here scaler state in crtc_state is set free so that
  3709. * scaler can be assigned to other user. Actual register
  3710. * update to free the scaler is done in plane/panel-fit programming.
  3711. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3712. */
  3713. if (force_detach || !need_scaling) {
  3714. if (*scaler_id >= 0) {
  3715. scaler_state->scaler_users &= ~(1 << scaler_user);
  3716. scaler_state->scalers[*scaler_id].in_use = 0;
  3717. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3718. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3719. intel_crtc->pipe, scaler_user, *scaler_id,
  3720. scaler_state->scaler_users);
  3721. *scaler_id = -1;
  3722. }
  3723. return 0;
  3724. }
  3725. /* range checks */
  3726. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3727. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3728. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3729. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3730. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3731. "size is out of scaler range\n",
  3732. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3733. return -EINVAL;
  3734. }
  3735. /* mark this plane as a scaler user in crtc_state */
  3736. scaler_state->scaler_users |= (1 << scaler_user);
  3737. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3738. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3739. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3740. scaler_state->scaler_users);
  3741. return 0;
  3742. }
  3743. /**
  3744. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3745. *
  3746. * @state: crtc's scaler state
  3747. * @force_detach: whether to forcibly disable scaler
  3748. *
  3749. * Return
  3750. * 0 - scaler_usage updated successfully
  3751. * error - requested scaling cannot be supported or other error condition
  3752. */
  3753. int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
  3754. {
  3755. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3756. struct drm_display_mode *adjusted_mode =
  3757. &state->base.adjusted_mode;
  3758. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3759. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3760. return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
  3761. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3762. state->pipe_src_w, state->pipe_src_h,
  3763. adjusted_mode->hdisplay, adjusted_mode->hdisplay);
  3764. }
  3765. /**
  3766. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3767. *
  3768. * @state: crtc's scaler state
  3769. * @intel_plane: affected plane
  3770. * @plane_state: atomic plane state to update
  3771. *
  3772. * Return
  3773. * 0 - scaler_usage updated successfully
  3774. * error - requested scaling cannot be supported or other error condition
  3775. */
  3776. int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3777. struct intel_plane *intel_plane,
  3778. struct intel_plane_state *plane_state)
  3779. {
  3780. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3781. struct drm_framebuffer *fb = plane_state->base.fb;
  3782. int ret;
  3783. bool force_detach = !fb || !plane_state->visible;
  3784. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3785. intel_plane->base.base.id, intel_crtc->pipe,
  3786. drm_plane_index(&intel_plane->base));
  3787. ret = skl_update_scaler(crtc_state, force_detach,
  3788. drm_plane_index(&intel_plane->base),
  3789. &plane_state->scaler_id,
  3790. plane_state->base.rotation,
  3791. drm_rect_width(&plane_state->src) >> 16,
  3792. drm_rect_height(&plane_state->src) >> 16,
  3793. drm_rect_width(&plane_state->dst),
  3794. drm_rect_height(&plane_state->dst));
  3795. if (ret || plane_state->scaler_id < 0)
  3796. return ret;
  3797. /* check colorkey */
  3798. if (WARN_ON(intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3799. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3800. intel_plane->base.base.id);
  3801. return -EINVAL;
  3802. }
  3803. /* Check src format */
  3804. switch (fb->pixel_format) {
  3805. case DRM_FORMAT_RGB565:
  3806. case DRM_FORMAT_XBGR8888:
  3807. case DRM_FORMAT_XRGB8888:
  3808. case DRM_FORMAT_ABGR8888:
  3809. case DRM_FORMAT_ARGB8888:
  3810. case DRM_FORMAT_XRGB2101010:
  3811. case DRM_FORMAT_XBGR2101010:
  3812. case DRM_FORMAT_YUYV:
  3813. case DRM_FORMAT_YVYU:
  3814. case DRM_FORMAT_UYVY:
  3815. case DRM_FORMAT_VYUY:
  3816. break;
  3817. default:
  3818. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3819. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3820. return -EINVAL;
  3821. }
  3822. return 0;
  3823. }
  3824. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3825. {
  3826. struct drm_device *dev = crtc->base.dev;
  3827. struct drm_i915_private *dev_priv = dev->dev_private;
  3828. int pipe = crtc->pipe;
  3829. struct intel_crtc_scaler_state *scaler_state =
  3830. &crtc->config->scaler_state;
  3831. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3832. /* To update pfit, first update scaler state */
  3833. skl_update_scaler_crtc(crtc->config, !enable);
  3834. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3835. skl_detach_scalers(crtc);
  3836. if (!enable)
  3837. return;
  3838. if (crtc->config->pch_pfit.enabled) {
  3839. int id;
  3840. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3841. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3842. return;
  3843. }
  3844. id = scaler_state->scaler_id;
  3845. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3846. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3847. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3848. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3849. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3850. }
  3851. }
  3852. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3853. {
  3854. struct drm_device *dev = crtc->base.dev;
  3855. struct drm_i915_private *dev_priv = dev->dev_private;
  3856. int pipe = crtc->pipe;
  3857. if (crtc->config->pch_pfit.enabled) {
  3858. /* Force use of hard-coded filter coefficients
  3859. * as some pre-programmed values are broken,
  3860. * e.g. x201.
  3861. */
  3862. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3863. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3864. PF_PIPE_SEL_IVB(pipe));
  3865. else
  3866. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3867. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3868. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3869. }
  3870. }
  3871. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3872. {
  3873. struct drm_device *dev = crtc->dev;
  3874. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3875. struct drm_plane *plane;
  3876. struct intel_plane *intel_plane;
  3877. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3878. intel_plane = to_intel_plane(plane);
  3879. if (intel_plane->pipe == pipe)
  3880. intel_plane_restore(&intel_plane->base);
  3881. }
  3882. }
  3883. void hsw_enable_ips(struct intel_crtc *crtc)
  3884. {
  3885. struct drm_device *dev = crtc->base.dev;
  3886. struct drm_i915_private *dev_priv = dev->dev_private;
  3887. if (!crtc->config->ips_enabled)
  3888. return;
  3889. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3890. intel_wait_for_vblank(dev, crtc->pipe);
  3891. assert_plane_enabled(dev_priv, crtc->plane);
  3892. if (IS_BROADWELL(dev)) {
  3893. mutex_lock(&dev_priv->rps.hw_lock);
  3894. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3895. mutex_unlock(&dev_priv->rps.hw_lock);
  3896. /* Quoting Art Runyan: "its not safe to expect any particular
  3897. * value in IPS_CTL bit 31 after enabling IPS through the
  3898. * mailbox." Moreover, the mailbox may return a bogus state,
  3899. * so we need to just enable it and continue on.
  3900. */
  3901. } else {
  3902. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3903. /* The bit only becomes 1 in the next vblank, so this wait here
  3904. * is essentially intel_wait_for_vblank. If we don't have this
  3905. * and don't wait for vblanks until the end of crtc_enable, then
  3906. * the HW state readout code will complain that the expected
  3907. * IPS_CTL value is not the one we read. */
  3908. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3909. DRM_ERROR("Timed out waiting for IPS enable\n");
  3910. }
  3911. }
  3912. void hsw_disable_ips(struct intel_crtc *crtc)
  3913. {
  3914. struct drm_device *dev = crtc->base.dev;
  3915. struct drm_i915_private *dev_priv = dev->dev_private;
  3916. if (!crtc->config->ips_enabled)
  3917. return;
  3918. assert_plane_enabled(dev_priv, crtc->plane);
  3919. if (IS_BROADWELL(dev)) {
  3920. mutex_lock(&dev_priv->rps.hw_lock);
  3921. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3922. mutex_unlock(&dev_priv->rps.hw_lock);
  3923. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3924. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3925. DRM_ERROR("Timed out waiting for IPS disable\n");
  3926. } else {
  3927. I915_WRITE(IPS_CTL, 0);
  3928. POSTING_READ(IPS_CTL);
  3929. }
  3930. /* We need to wait for a vblank before we can disable the plane. */
  3931. intel_wait_for_vblank(dev, crtc->pipe);
  3932. }
  3933. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3934. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3935. {
  3936. struct drm_device *dev = crtc->dev;
  3937. struct drm_i915_private *dev_priv = dev->dev_private;
  3938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3939. enum pipe pipe = intel_crtc->pipe;
  3940. int palreg = PALETTE(pipe);
  3941. int i;
  3942. bool reenable_ips = false;
  3943. /* The clocks have to be on to load the palette. */
  3944. if (!crtc->state->active)
  3945. return;
  3946. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3947. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3948. assert_dsi_pll_enabled(dev_priv);
  3949. else
  3950. assert_pll_enabled(dev_priv, pipe);
  3951. }
  3952. /* use legacy palette for Ironlake */
  3953. if (!HAS_GMCH_DISPLAY(dev))
  3954. palreg = LGC_PALETTE(pipe);
  3955. /* Workaround : Do not read or write the pipe palette/gamma data while
  3956. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3957. */
  3958. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3959. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3960. GAMMA_MODE_MODE_SPLIT)) {
  3961. hsw_disable_ips(intel_crtc);
  3962. reenable_ips = true;
  3963. }
  3964. for (i = 0; i < 256; i++) {
  3965. I915_WRITE(palreg + 4 * i,
  3966. (intel_crtc->lut_r[i] << 16) |
  3967. (intel_crtc->lut_g[i] << 8) |
  3968. intel_crtc->lut_b[i]);
  3969. }
  3970. if (reenable_ips)
  3971. hsw_enable_ips(intel_crtc);
  3972. }
  3973. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3974. {
  3975. if (intel_crtc->overlay) {
  3976. struct drm_device *dev = intel_crtc->base.dev;
  3977. struct drm_i915_private *dev_priv = dev->dev_private;
  3978. mutex_lock(&dev->struct_mutex);
  3979. dev_priv->mm.interruptible = false;
  3980. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3981. dev_priv->mm.interruptible = true;
  3982. mutex_unlock(&dev->struct_mutex);
  3983. }
  3984. /* Let userspace switch the overlay on again. In most cases userspace
  3985. * has to recompute where to put it anyway.
  3986. */
  3987. }
  3988. /**
  3989. * intel_post_enable_primary - Perform operations after enabling primary plane
  3990. * @crtc: the CRTC whose primary plane was just enabled
  3991. *
  3992. * Performs potentially sleeping operations that must be done after the primary
  3993. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3994. * called due to an explicit primary plane update, or due to an implicit
  3995. * re-enable that is caused when a sprite plane is updated to no longer
  3996. * completely hide the primary plane.
  3997. */
  3998. static void
  3999. intel_post_enable_primary(struct drm_crtc *crtc)
  4000. {
  4001. struct drm_device *dev = crtc->dev;
  4002. struct drm_i915_private *dev_priv = dev->dev_private;
  4003. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4004. int pipe = intel_crtc->pipe;
  4005. /*
  4006. * BDW signals flip done immediately if the plane
  4007. * is disabled, even if the plane enable is already
  4008. * armed to occur at the next vblank :(
  4009. */
  4010. if (IS_BROADWELL(dev))
  4011. intel_wait_for_vblank(dev, pipe);
  4012. /*
  4013. * FIXME IPS should be fine as long as one plane is
  4014. * enabled, but in practice it seems to have problems
  4015. * when going from primary only to sprite only and vice
  4016. * versa.
  4017. */
  4018. hsw_enable_ips(intel_crtc);
  4019. mutex_lock(&dev->struct_mutex);
  4020. intel_fbc_update(dev);
  4021. mutex_unlock(&dev->struct_mutex);
  4022. /*
  4023. * Gen2 reports pipe underruns whenever all planes are disabled.
  4024. * So don't enable underrun reporting before at least some planes
  4025. * are enabled.
  4026. * FIXME: Need to fix the logic to work when we turn off all planes
  4027. * but leave the pipe running.
  4028. */
  4029. if (IS_GEN2(dev))
  4030. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4031. /* Underruns don't raise interrupts, so check manually. */
  4032. if (HAS_GMCH_DISPLAY(dev))
  4033. i9xx_check_fifo_underruns(dev_priv);
  4034. }
  4035. /**
  4036. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4037. * @crtc: the CRTC whose primary plane is to be disabled
  4038. *
  4039. * Performs potentially sleeping operations that must be done before the
  4040. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4041. * be called due to an explicit primary plane update, or due to an implicit
  4042. * disable that is caused when a sprite plane completely hides the primary
  4043. * plane.
  4044. */
  4045. static void
  4046. intel_pre_disable_primary(struct drm_crtc *crtc)
  4047. {
  4048. struct drm_device *dev = crtc->dev;
  4049. struct drm_i915_private *dev_priv = dev->dev_private;
  4050. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4051. int pipe = intel_crtc->pipe;
  4052. /*
  4053. * Gen2 reports pipe underruns whenever all planes are disabled.
  4054. * So diasble underrun reporting before all the planes get disabled.
  4055. * FIXME: Need to fix the logic to work when we turn off all planes
  4056. * but leave the pipe running.
  4057. */
  4058. if (IS_GEN2(dev))
  4059. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4060. /*
  4061. * Vblank time updates from the shadow to live plane control register
  4062. * are blocked if the memory self-refresh mode is active at that
  4063. * moment. So to make sure the plane gets truly disabled, disable
  4064. * first the self-refresh mode. The self-refresh enable bit in turn
  4065. * will be checked/applied by the HW only at the next frame start
  4066. * event which is after the vblank start event, so we need to have a
  4067. * wait-for-vblank between disabling the plane and the pipe.
  4068. */
  4069. if (HAS_GMCH_DISPLAY(dev))
  4070. intel_set_memory_cxsr(dev_priv, false);
  4071. mutex_lock(&dev->struct_mutex);
  4072. if (dev_priv->fbc.crtc == intel_crtc)
  4073. intel_fbc_disable(dev);
  4074. mutex_unlock(&dev->struct_mutex);
  4075. /*
  4076. * FIXME IPS should be fine as long as one plane is
  4077. * enabled, but in practice it seems to have problems
  4078. * when going from primary only to sprite only and vice
  4079. * versa.
  4080. */
  4081. hsw_disable_ips(intel_crtc);
  4082. }
  4083. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4084. {
  4085. struct drm_device *dev = crtc->dev;
  4086. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4087. int pipe = intel_crtc->pipe;
  4088. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4089. intel_enable_sprite_planes(crtc);
  4090. if (to_intel_plane_state(crtc->cursor->state)->visible)
  4091. intel_crtc_update_cursor(crtc, true);
  4092. intel_post_enable_primary(crtc);
  4093. /*
  4094. * FIXME: Once we grow proper nuclear flip support out of this we need
  4095. * to compute the mask of flip planes precisely. For the time being
  4096. * consider this a flip to a NULL plane.
  4097. */
  4098. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4099. }
  4100. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4101. {
  4102. struct drm_device *dev = crtc->dev;
  4103. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4104. struct intel_plane *intel_plane;
  4105. int pipe = intel_crtc->pipe;
  4106. intel_crtc_wait_for_pending_flips(crtc);
  4107. intel_pre_disable_primary(crtc);
  4108. intel_crtc_dpms_overlay_disable(intel_crtc);
  4109. for_each_intel_plane(dev, intel_plane) {
  4110. if (intel_plane->pipe == pipe) {
  4111. struct drm_crtc *from = intel_plane->base.crtc;
  4112. intel_plane->disable_plane(&intel_plane->base,
  4113. from ?: crtc, true);
  4114. }
  4115. }
  4116. /*
  4117. * FIXME: Once we grow proper nuclear flip support out of this we need
  4118. * to compute the mask of flip planes precisely. For the time being
  4119. * consider this a flip to a NULL plane.
  4120. */
  4121. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4122. }
  4123. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4124. {
  4125. struct drm_device *dev = crtc->dev;
  4126. struct drm_i915_private *dev_priv = dev->dev_private;
  4127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4128. struct intel_encoder *encoder;
  4129. int pipe = intel_crtc->pipe;
  4130. if (WARN_ON(intel_crtc->active))
  4131. return;
  4132. if (intel_crtc->config->has_pch_encoder)
  4133. intel_prepare_shared_dpll(intel_crtc);
  4134. if (intel_crtc->config->has_dp_encoder)
  4135. intel_dp_set_m_n(intel_crtc, M1_N1);
  4136. intel_set_pipe_timings(intel_crtc);
  4137. if (intel_crtc->config->has_pch_encoder) {
  4138. intel_cpu_transcoder_set_m_n(intel_crtc,
  4139. &intel_crtc->config->fdi_m_n, NULL);
  4140. }
  4141. ironlake_set_pipeconf(crtc);
  4142. intel_crtc->active = true;
  4143. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4144. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4145. for_each_encoder_on_crtc(dev, crtc, encoder)
  4146. if (encoder->pre_enable)
  4147. encoder->pre_enable(encoder);
  4148. if (intel_crtc->config->has_pch_encoder) {
  4149. /* Note: FDI PLL enabling _must_ be done before we enable the
  4150. * cpu pipes, hence this is separate from all the other fdi/pch
  4151. * enabling. */
  4152. ironlake_fdi_pll_enable(intel_crtc);
  4153. } else {
  4154. assert_fdi_tx_disabled(dev_priv, pipe);
  4155. assert_fdi_rx_disabled(dev_priv, pipe);
  4156. }
  4157. ironlake_pfit_enable(intel_crtc);
  4158. /*
  4159. * On ILK+ LUT must be loaded before the pipe is running but with
  4160. * clocks enabled
  4161. */
  4162. intel_crtc_load_lut(crtc);
  4163. intel_update_watermarks(crtc);
  4164. intel_enable_pipe(intel_crtc);
  4165. if (intel_crtc->config->has_pch_encoder)
  4166. ironlake_pch_enable(crtc);
  4167. assert_vblank_disabled(crtc);
  4168. drm_crtc_vblank_on(crtc);
  4169. for_each_encoder_on_crtc(dev, crtc, encoder)
  4170. encoder->enable(encoder);
  4171. if (HAS_PCH_CPT(dev))
  4172. cpt_verify_modeset(dev, intel_crtc->pipe);
  4173. }
  4174. /* IPS only exists on ULT machines and is tied to pipe A. */
  4175. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4176. {
  4177. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4178. }
  4179. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4180. {
  4181. struct drm_device *dev = crtc->dev;
  4182. struct drm_i915_private *dev_priv = dev->dev_private;
  4183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4184. struct intel_encoder *encoder;
  4185. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4186. struct intel_crtc_state *pipe_config =
  4187. to_intel_crtc_state(crtc->state);
  4188. if (WARN_ON(intel_crtc->active))
  4189. return;
  4190. if (intel_crtc_to_shared_dpll(intel_crtc))
  4191. intel_enable_shared_dpll(intel_crtc);
  4192. if (intel_crtc->config->has_dp_encoder)
  4193. intel_dp_set_m_n(intel_crtc, M1_N1);
  4194. intel_set_pipe_timings(intel_crtc);
  4195. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4196. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4197. intel_crtc->config->pixel_multiplier - 1);
  4198. }
  4199. if (intel_crtc->config->has_pch_encoder) {
  4200. intel_cpu_transcoder_set_m_n(intel_crtc,
  4201. &intel_crtc->config->fdi_m_n, NULL);
  4202. }
  4203. haswell_set_pipeconf(crtc);
  4204. intel_set_pipe_csc(crtc);
  4205. intel_crtc->active = true;
  4206. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4207. for_each_encoder_on_crtc(dev, crtc, encoder)
  4208. if (encoder->pre_enable)
  4209. encoder->pre_enable(encoder);
  4210. if (intel_crtc->config->has_pch_encoder) {
  4211. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4212. true);
  4213. dev_priv->display.fdi_link_train(crtc);
  4214. }
  4215. intel_ddi_enable_pipe_clock(intel_crtc);
  4216. if (INTEL_INFO(dev)->gen == 9)
  4217. skylake_pfit_update(intel_crtc, 1);
  4218. else if (INTEL_INFO(dev)->gen < 9)
  4219. ironlake_pfit_enable(intel_crtc);
  4220. else
  4221. MISSING_CASE(INTEL_INFO(dev)->gen);
  4222. /*
  4223. * On ILK+ LUT must be loaded before the pipe is running but with
  4224. * clocks enabled
  4225. */
  4226. intel_crtc_load_lut(crtc);
  4227. intel_ddi_set_pipe_settings(crtc);
  4228. intel_ddi_enable_transcoder_func(crtc);
  4229. intel_update_watermarks(crtc);
  4230. intel_enable_pipe(intel_crtc);
  4231. if (intel_crtc->config->has_pch_encoder)
  4232. lpt_pch_enable(crtc);
  4233. if (intel_crtc->config->dp_encoder_is_mst)
  4234. intel_ddi_set_vc_payload_alloc(crtc, true);
  4235. assert_vblank_disabled(crtc);
  4236. drm_crtc_vblank_on(crtc);
  4237. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4238. encoder->enable(encoder);
  4239. intel_opregion_notify_encoder(encoder, true);
  4240. }
  4241. /* If we change the relative order between pipe/planes enabling, we need
  4242. * to change the workaround. */
  4243. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4244. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4245. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4246. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4247. }
  4248. }
  4249. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4250. {
  4251. struct drm_device *dev = crtc->base.dev;
  4252. struct drm_i915_private *dev_priv = dev->dev_private;
  4253. int pipe = crtc->pipe;
  4254. /* To avoid upsetting the power well on haswell only disable the pfit if
  4255. * it's in use. The hw state code will make sure we get this right. */
  4256. if (crtc->config->pch_pfit.enabled) {
  4257. I915_WRITE(PF_CTL(pipe), 0);
  4258. I915_WRITE(PF_WIN_POS(pipe), 0);
  4259. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4260. }
  4261. }
  4262. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4263. {
  4264. struct drm_device *dev = crtc->dev;
  4265. struct drm_i915_private *dev_priv = dev->dev_private;
  4266. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4267. struct intel_encoder *encoder;
  4268. int pipe = intel_crtc->pipe;
  4269. u32 reg, temp;
  4270. if (WARN_ON(!intel_crtc->active))
  4271. return;
  4272. for_each_encoder_on_crtc(dev, crtc, encoder)
  4273. encoder->disable(encoder);
  4274. drm_crtc_vblank_off(crtc);
  4275. assert_vblank_disabled(crtc);
  4276. if (intel_crtc->config->has_pch_encoder)
  4277. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4278. intel_disable_pipe(intel_crtc);
  4279. ironlake_pfit_disable(intel_crtc);
  4280. if (intel_crtc->config->has_pch_encoder)
  4281. ironlake_fdi_disable(crtc);
  4282. for_each_encoder_on_crtc(dev, crtc, encoder)
  4283. if (encoder->post_disable)
  4284. encoder->post_disable(encoder);
  4285. if (intel_crtc->config->has_pch_encoder) {
  4286. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4287. if (HAS_PCH_CPT(dev)) {
  4288. /* disable TRANS_DP_CTL */
  4289. reg = TRANS_DP_CTL(pipe);
  4290. temp = I915_READ(reg);
  4291. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4292. TRANS_DP_PORT_SEL_MASK);
  4293. temp |= TRANS_DP_PORT_SEL_NONE;
  4294. I915_WRITE(reg, temp);
  4295. /* disable DPLL_SEL */
  4296. temp = I915_READ(PCH_DPLL_SEL);
  4297. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4298. I915_WRITE(PCH_DPLL_SEL, temp);
  4299. }
  4300. /* disable PCH DPLL */
  4301. intel_disable_shared_dpll(intel_crtc);
  4302. ironlake_fdi_pll_disable(intel_crtc);
  4303. }
  4304. intel_crtc->active = false;
  4305. intel_update_watermarks(crtc);
  4306. mutex_lock(&dev->struct_mutex);
  4307. intel_fbc_update(dev);
  4308. mutex_unlock(&dev->struct_mutex);
  4309. }
  4310. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4311. {
  4312. struct drm_device *dev = crtc->dev;
  4313. struct drm_i915_private *dev_priv = dev->dev_private;
  4314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4315. struct intel_encoder *encoder;
  4316. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4317. if (WARN_ON(!intel_crtc->active))
  4318. return;
  4319. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4320. intel_opregion_notify_encoder(encoder, false);
  4321. encoder->disable(encoder);
  4322. }
  4323. drm_crtc_vblank_off(crtc);
  4324. assert_vblank_disabled(crtc);
  4325. if (intel_crtc->config->has_pch_encoder)
  4326. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4327. false);
  4328. intel_disable_pipe(intel_crtc);
  4329. if (intel_crtc->config->dp_encoder_is_mst)
  4330. intel_ddi_set_vc_payload_alloc(crtc, false);
  4331. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4332. if (INTEL_INFO(dev)->gen == 9)
  4333. skylake_pfit_update(intel_crtc, 0);
  4334. else if (INTEL_INFO(dev)->gen < 9)
  4335. ironlake_pfit_disable(intel_crtc);
  4336. else
  4337. MISSING_CASE(INTEL_INFO(dev)->gen);
  4338. intel_ddi_disable_pipe_clock(intel_crtc);
  4339. if (intel_crtc->config->has_pch_encoder) {
  4340. lpt_disable_pch_transcoder(dev_priv);
  4341. intel_ddi_fdi_disable(crtc);
  4342. }
  4343. for_each_encoder_on_crtc(dev, crtc, encoder)
  4344. if (encoder->post_disable)
  4345. encoder->post_disable(encoder);
  4346. intel_crtc->active = false;
  4347. intel_update_watermarks(crtc);
  4348. mutex_lock(&dev->struct_mutex);
  4349. intel_fbc_update(dev);
  4350. mutex_unlock(&dev->struct_mutex);
  4351. if (intel_crtc_to_shared_dpll(intel_crtc))
  4352. intel_disable_shared_dpll(intel_crtc);
  4353. }
  4354. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4355. {
  4356. struct drm_device *dev = crtc->base.dev;
  4357. struct drm_i915_private *dev_priv = dev->dev_private;
  4358. struct intel_crtc_state *pipe_config = crtc->config;
  4359. if (!pipe_config->gmch_pfit.control)
  4360. return;
  4361. /*
  4362. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4363. * according to register description and PRM.
  4364. */
  4365. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4366. assert_pipe_disabled(dev_priv, crtc->pipe);
  4367. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4368. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4369. /* Border color in case we don't scale up to the full screen. Black by
  4370. * default, change to something else for debugging. */
  4371. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4372. }
  4373. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4374. {
  4375. switch (port) {
  4376. case PORT_A:
  4377. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4378. case PORT_B:
  4379. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4380. case PORT_C:
  4381. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4382. case PORT_D:
  4383. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4384. default:
  4385. WARN_ON_ONCE(1);
  4386. return POWER_DOMAIN_PORT_OTHER;
  4387. }
  4388. }
  4389. #define for_each_power_domain(domain, mask) \
  4390. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4391. if ((1 << (domain)) & (mask))
  4392. enum intel_display_power_domain
  4393. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4394. {
  4395. struct drm_device *dev = intel_encoder->base.dev;
  4396. struct intel_digital_port *intel_dig_port;
  4397. switch (intel_encoder->type) {
  4398. case INTEL_OUTPUT_UNKNOWN:
  4399. /* Only DDI platforms should ever use this output type */
  4400. WARN_ON_ONCE(!HAS_DDI(dev));
  4401. case INTEL_OUTPUT_DISPLAYPORT:
  4402. case INTEL_OUTPUT_HDMI:
  4403. case INTEL_OUTPUT_EDP:
  4404. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4405. return port_to_power_domain(intel_dig_port->port);
  4406. case INTEL_OUTPUT_DP_MST:
  4407. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4408. return port_to_power_domain(intel_dig_port->port);
  4409. case INTEL_OUTPUT_ANALOG:
  4410. return POWER_DOMAIN_PORT_CRT;
  4411. case INTEL_OUTPUT_DSI:
  4412. return POWER_DOMAIN_PORT_DSI;
  4413. default:
  4414. return POWER_DOMAIN_PORT_OTHER;
  4415. }
  4416. }
  4417. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4418. {
  4419. struct drm_device *dev = crtc->dev;
  4420. struct intel_encoder *intel_encoder;
  4421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4422. enum pipe pipe = intel_crtc->pipe;
  4423. unsigned long mask;
  4424. enum transcoder transcoder;
  4425. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4426. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4427. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4428. if (intel_crtc->config->pch_pfit.enabled ||
  4429. intel_crtc->config->pch_pfit.force_thru)
  4430. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4431. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4432. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4433. return mask;
  4434. }
  4435. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4436. {
  4437. struct drm_device *dev = state->dev;
  4438. struct drm_i915_private *dev_priv = dev->dev_private;
  4439. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4440. struct intel_crtc *crtc;
  4441. /*
  4442. * First get all needed power domains, then put all unneeded, to avoid
  4443. * any unnecessary toggling of the power wells.
  4444. */
  4445. for_each_intel_crtc(dev, crtc) {
  4446. enum intel_display_power_domain domain;
  4447. if (!crtc->base.state->enable)
  4448. continue;
  4449. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4450. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4451. intel_display_power_get(dev_priv, domain);
  4452. }
  4453. if (dev_priv->display.modeset_global_resources)
  4454. dev_priv->display.modeset_global_resources(state);
  4455. for_each_intel_crtc(dev, crtc) {
  4456. enum intel_display_power_domain domain;
  4457. for_each_power_domain(domain, crtc->enabled_power_domains)
  4458. intel_display_power_put(dev_priv, domain);
  4459. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4460. }
  4461. intel_display_set_init_power(dev_priv, false);
  4462. }
  4463. static void intel_update_max_cdclk(struct drm_device *dev)
  4464. {
  4465. struct drm_i915_private *dev_priv = dev->dev_private;
  4466. if (IS_SKYLAKE(dev)) {
  4467. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4468. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4469. dev_priv->max_cdclk_freq = 675000;
  4470. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4471. dev_priv->max_cdclk_freq = 540000;
  4472. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4473. dev_priv->max_cdclk_freq = 450000;
  4474. else
  4475. dev_priv->max_cdclk_freq = 337500;
  4476. } else if (IS_BROADWELL(dev)) {
  4477. /*
  4478. * FIXME with extra cooling we can allow
  4479. * 540 MHz for ULX and 675 Mhz for ULT.
  4480. * How can we know if extra cooling is
  4481. * available? PCI ID, VTB, something else?
  4482. */
  4483. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4484. dev_priv->max_cdclk_freq = 450000;
  4485. else if (IS_BDW_ULX(dev))
  4486. dev_priv->max_cdclk_freq = 450000;
  4487. else if (IS_BDW_ULT(dev))
  4488. dev_priv->max_cdclk_freq = 540000;
  4489. else
  4490. dev_priv->max_cdclk_freq = 675000;
  4491. } else if (IS_CHERRYVIEW(dev)) {
  4492. dev_priv->max_cdclk_freq = 320000;
  4493. } else if (IS_VALLEYVIEW(dev)) {
  4494. dev_priv->max_cdclk_freq = 400000;
  4495. } else {
  4496. /* otherwise assume cdclk is fixed */
  4497. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4498. }
  4499. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4500. dev_priv->max_cdclk_freq);
  4501. }
  4502. static void intel_update_cdclk(struct drm_device *dev)
  4503. {
  4504. struct drm_i915_private *dev_priv = dev->dev_private;
  4505. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4506. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4507. dev_priv->cdclk_freq);
  4508. /*
  4509. * Program the gmbus_freq based on the cdclk frequency.
  4510. * BSpec erroneously claims we should aim for 4MHz, but
  4511. * in fact 1MHz is the correct frequency.
  4512. */
  4513. if (IS_VALLEYVIEW(dev)) {
  4514. /*
  4515. * Program the gmbus_freq based on the cdclk frequency.
  4516. * BSpec erroneously claims we should aim for 4MHz, but
  4517. * in fact 1MHz is the correct frequency.
  4518. */
  4519. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4520. }
  4521. if (dev_priv->max_cdclk_freq == 0)
  4522. intel_update_max_cdclk(dev);
  4523. }
  4524. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4525. {
  4526. struct drm_i915_private *dev_priv = dev->dev_private;
  4527. uint32_t divider;
  4528. uint32_t ratio;
  4529. uint32_t current_freq;
  4530. int ret;
  4531. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4532. switch (frequency) {
  4533. case 144000:
  4534. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4535. ratio = BXT_DE_PLL_RATIO(60);
  4536. break;
  4537. case 288000:
  4538. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4539. ratio = BXT_DE_PLL_RATIO(60);
  4540. break;
  4541. case 384000:
  4542. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4543. ratio = BXT_DE_PLL_RATIO(60);
  4544. break;
  4545. case 576000:
  4546. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4547. ratio = BXT_DE_PLL_RATIO(60);
  4548. break;
  4549. case 624000:
  4550. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4551. ratio = BXT_DE_PLL_RATIO(65);
  4552. break;
  4553. case 19200:
  4554. /*
  4555. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4556. * to suppress GCC warning.
  4557. */
  4558. ratio = 0;
  4559. divider = 0;
  4560. break;
  4561. default:
  4562. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4563. return;
  4564. }
  4565. mutex_lock(&dev_priv->rps.hw_lock);
  4566. /* Inform power controller of upcoming frequency change */
  4567. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4568. 0x80000000);
  4569. mutex_unlock(&dev_priv->rps.hw_lock);
  4570. if (ret) {
  4571. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4572. ret, frequency);
  4573. return;
  4574. }
  4575. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4576. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4577. current_freq = current_freq * 500 + 1000;
  4578. /*
  4579. * DE PLL has to be disabled when
  4580. * - setting to 19.2MHz (bypass, PLL isn't used)
  4581. * - before setting to 624MHz (PLL needs toggling)
  4582. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4583. */
  4584. if (frequency == 19200 || frequency == 624000 ||
  4585. current_freq == 624000) {
  4586. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4587. /* Timeout 200us */
  4588. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4589. 1))
  4590. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4591. }
  4592. if (frequency != 19200) {
  4593. uint32_t val;
  4594. val = I915_READ(BXT_DE_PLL_CTL);
  4595. val &= ~BXT_DE_PLL_RATIO_MASK;
  4596. val |= ratio;
  4597. I915_WRITE(BXT_DE_PLL_CTL, val);
  4598. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4599. /* Timeout 200us */
  4600. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4601. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4602. val = I915_READ(CDCLK_CTL);
  4603. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4604. val |= divider;
  4605. /*
  4606. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4607. * enable otherwise.
  4608. */
  4609. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4610. if (frequency >= 500000)
  4611. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4612. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4613. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4614. val |= (frequency - 1000) / 500;
  4615. I915_WRITE(CDCLK_CTL, val);
  4616. }
  4617. mutex_lock(&dev_priv->rps.hw_lock);
  4618. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4619. DIV_ROUND_UP(frequency, 25000));
  4620. mutex_unlock(&dev_priv->rps.hw_lock);
  4621. if (ret) {
  4622. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4623. ret, frequency);
  4624. return;
  4625. }
  4626. intel_update_cdclk(dev);
  4627. }
  4628. void broxton_init_cdclk(struct drm_device *dev)
  4629. {
  4630. struct drm_i915_private *dev_priv = dev->dev_private;
  4631. uint32_t val;
  4632. /*
  4633. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4634. * or else the reset will hang because there is no PCH to respond.
  4635. * Move the handshake programming to initialization sequence.
  4636. * Previously was left up to BIOS.
  4637. */
  4638. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4639. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4640. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4641. /* Enable PG1 for cdclk */
  4642. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4643. /* check if cd clock is enabled */
  4644. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4645. DRM_DEBUG_KMS("Display already initialized\n");
  4646. return;
  4647. }
  4648. /*
  4649. * FIXME:
  4650. * - The initial CDCLK needs to be read from VBT.
  4651. * Need to make this change after VBT has changes for BXT.
  4652. * - check if setting the max (or any) cdclk freq is really necessary
  4653. * here, it belongs to modeset time
  4654. */
  4655. broxton_set_cdclk(dev, 624000);
  4656. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4657. POSTING_READ(DBUF_CTL);
  4658. udelay(10);
  4659. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4660. DRM_ERROR("DBuf power enable timeout!\n");
  4661. }
  4662. void broxton_uninit_cdclk(struct drm_device *dev)
  4663. {
  4664. struct drm_i915_private *dev_priv = dev->dev_private;
  4665. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4666. POSTING_READ(DBUF_CTL);
  4667. udelay(10);
  4668. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4669. DRM_ERROR("DBuf power disable timeout!\n");
  4670. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4671. broxton_set_cdclk(dev, 19200);
  4672. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4673. }
  4674. static const struct skl_cdclk_entry {
  4675. unsigned int freq;
  4676. unsigned int vco;
  4677. } skl_cdclk_frequencies[] = {
  4678. { .freq = 308570, .vco = 8640 },
  4679. { .freq = 337500, .vco = 8100 },
  4680. { .freq = 432000, .vco = 8640 },
  4681. { .freq = 450000, .vco = 8100 },
  4682. { .freq = 540000, .vco = 8100 },
  4683. { .freq = 617140, .vco = 8640 },
  4684. { .freq = 675000, .vco = 8100 },
  4685. };
  4686. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4687. {
  4688. return (freq - 1000) / 500;
  4689. }
  4690. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4691. {
  4692. unsigned int i;
  4693. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4694. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4695. if (e->freq == freq)
  4696. return e->vco;
  4697. }
  4698. return 8100;
  4699. }
  4700. static void
  4701. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4702. {
  4703. unsigned int min_freq;
  4704. u32 val;
  4705. /* select the minimum CDCLK before enabling DPLL 0 */
  4706. val = I915_READ(CDCLK_CTL);
  4707. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4708. val |= CDCLK_FREQ_337_308;
  4709. if (required_vco == 8640)
  4710. min_freq = 308570;
  4711. else
  4712. min_freq = 337500;
  4713. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4714. I915_WRITE(CDCLK_CTL, val);
  4715. POSTING_READ(CDCLK_CTL);
  4716. /*
  4717. * We always enable DPLL0 with the lowest link rate possible, but still
  4718. * taking into account the VCO required to operate the eDP panel at the
  4719. * desired frequency. The usual DP link rates operate with a VCO of
  4720. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4721. * The modeset code is responsible for the selection of the exact link
  4722. * rate later on, with the constraint of choosing a frequency that
  4723. * works with required_vco.
  4724. */
  4725. val = I915_READ(DPLL_CTRL1);
  4726. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4727. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4728. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4729. if (required_vco == 8640)
  4730. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4731. SKL_DPLL0);
  4732. else
  4733. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4734. SKL_DPLL0);
  4735. I915_WRITE(DPLL_CTRL1, val);
  4736. POSTING_READ(DPLL_CTRL1);
  4737. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4738. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4739. DRM_ERROR("DPLL0 not locked\n");
  4740. }
  4741. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4742. {
  4743. int ret;
  4744. u32 val;
  4745. /* inform PCU we want to change CDCLK */
  4746. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4747. mutex_lock(&dev_priv->rps.hw_lock);
  4748. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4749. mutex_unlock(&dev_priv->rps.hw_lock);
  4750. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4751. }
  4752. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4753. {
  4754. unsigned int i;
  4755. for (i = 0; i < 15; i++) {
  4756. if (skl_cdclk_pcu_ready(dev_priv))
  4757. return true;
  4758. udelay(10);
  4759. }
  4760. return false;
  4761. }
  4762. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4763. {
  4764. struct drm_device *dev = dev_priv->dev;
  4765. u32 freq_select, pcu_ack;
  4766. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4767. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4768. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4769. return;
  4770. }
  4771. /* set CDCLK_CTL */
  4772. switch(freq) {
  4773. case 450000:
  4774. case 432000:
  4775. freq_select = CDCLK_FREQ_450_432;
  4776. pcu_ack = 1;
  4777. break;
  4778. case 540000:
  4779. freq_select = CDCLK_FREQ_540;
  4780. pcu_ack = 2;
  4781. break;
  4782. case 308570:
  4783. case 337500:
  4784. default:
  4785. freq_select = CDCLK_FREQ_337_308;
  4786. pcu_ack = 0;
  4787. break;
  4788. case 617140:
  4789. case 675000:
  4790. freq_select = CDCLK_FREQ_675_617;
  4791. pcu_ack = 3;
  4792. break;
  4793. }
  4794. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4795. POSTING_READ(CDCLK_CTL);
  4796. /* inform PCU of the change */
  4797. mutex_lock(&dev_priv->rps.hw_lock);
  4798. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4799. mutex_unlock(&dev_priv->rps.hw_lock);
  4800. intel_update_cdclk(dev);
  4801. }
  4802. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4803. {
  4804. /* disable DBUF power */
  4805. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4806. POSTING_READ(DBUF_CTL);
  4807. udelay(10);
  4808. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4809. DRM_ERROR("DBuf power disable timeout\n");
  4810. /* disable DPLL0 */
  4811. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4812. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4813. DRM_ERROR("Couldn't disable DPLL0\n");
  4814. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4815. }
  4816. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4817. {
  4818. u32 val;
  4819. unsigned int required_vco;
  4820. /* enable PCH reset handshake */
  4821. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4822. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4823. /* enable PG1 and Misc I/O */
  4824. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4825. /* DPLL0 already enabed !? */
  4826. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4827. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4828. return;
  4829. }
  4830. /* enable DPLL0 */
  4831. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4832. skl_dpll0_enable(dev_priv, required_vco);
  4833. /* set CDCLK to the frequency the BIOS chose */
  4834. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4835. /* enable DBUF power */
  4836. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4837. POSTING_READ(DBUF_CTL);
  4838. udelay(10);
  4839. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4840. DRM_ERROR("DBuf power enable timeout\n");
  4841. }
  4842. /* returns HPLL frequency in kHz */
  4843. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4844. {
  4845. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4846. /* Obtain SKU information */
  4847. mutex_lock(&dev_priv->sb_lock);
  4848. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4849. CCK_FUSE_HPLL_FREQ_MASK;
  4850. mutex_unlock(&dev_priv->sb_lock);
  4851. return vco_freq[hpll_freq] * 1000;
  4852. }
  4853. /* Adjust CDclk dividers to allow high res or save power if possible */
  4854. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4855. {
  4856. struct drm_i915_private *dev_priv = dev->dev_private;
  4857. u32 val, cmd;
  4858. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4859. != dev_priv->cdclk_freq);
  4860. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4861. cmd = 2;
  4862. else if (cdclk == 266667)
  4863. cmd = 1;
  4864. else
  4865. cmd = 0;
  4866. mutex_lock(&dev_priv->rps.hw_lock);
  4867. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4868. val &= ~DSPFREQGUAR_MASK;
  4869. val |= (cmd << DSPFREQGUAR_SHIFT);
  4870. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4871. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4872. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4873. 50)) {
  4874. DRM_ERROR("timed out waiting for CDclk change\n");
  4875. }
  4876. mutex_unlock(&dev_priv->rps.hw_lock);
  4877. mutex_lock(&dev_priv->sb_lock);
  4878. if (cdclk == 400000) {
  4879. u32 divider;
  4880. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4881. /* adjust cdclk divider */
  4882. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4883. val &= ~DISPLAY_FREQUENCY_VALUES;
  4884. val |= divider;
  4885. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4886. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4887. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4888. 50))
  4889. DRM_ERROR("timed out waiting for CDclk change\n");
  4890. }
  4891. /* adjust self-refresh exit latency value */
  4892. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4893. val &= ~0x7f;
  4894. /*
  4895. * For high bandwidth configs, we set a higher latency in the bunit
  4896. * so that the core display fetch happens in time to avoid underruns.
  4897. */
  4898. if (cdclk == 400000)
  4899. val |= 4500 / 250; /* 4.5 usec */
  4900. else
  4901. val |= 3000 / 250; /* 3.0 usec */
  4902. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4903. mutex_unlock(&dev_priv->sb_lock);
  4904. intel_update_cdclk(dev);
  4905. }
  4906. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4907. {
  4908. struct drm_i915_private *dev_priv = dev->dev_private;
  4909. u32 val, cmd;
  4910. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4911. != dev_priv->cdclk_freq);
  4912. switch (cdclk) {
  4913. case 333333:
  4914. case 320000:
  4915. case 266667:
  4916. case 200000:
  4917. break;
  4918. default:
  4919. MISSING_CASE(cdclk);
  4920. return;
  4921. }
  4922. /*
  4923. * Specs are full of misinformation, but testing on actual
  4924. * hardware has shown that we just need to write the desired
  4925. * CCK divider into the Punit register.
  4926. */
  4927. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4928. mutex_lock(&dev_priv->rps.hw_lock);
  4929. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4930. val &= ~DSPFREQGUAR_MASK_CHV;
  4931. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4932. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4933. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4934. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4935. 50)) {
  4936. DRM_ERROR("timed out waiting for CDclk change\n");
  4937. }
  4938. mutex_unlock(&dev_priv->rps.hw_lock);
  4939. intel_update_cdclk(dev);
  4940. }
  4941. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4942. int max_pixclk)
  4943. {
  4944. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4945. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4946. /*
  4947. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4948. * 200MHz
  4949. * 267MHz
  4950. * 320/333MHz (depends on HPLL freq)
  4951. * 400MHz (VLV only)
  4952. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4953. * of the lower bin and adjust if needed.
  4954. *
  4955. * We seem to get an unstable or solid color picture at 200MHz.
  4956. * Not sure what's wrong. For now use 200MHz only when all pipes
  4957. * are off.
  4958. */
  4959. if (!IS_CHERRYVIEW(dev_priv) &&
  4960. max_pixclk > freq_320*limit/100)
  4961. return 400000;
  4962. else if (max_pixclk > 266667*limit/100)
  4963. return freq_320;
  4964. else if (max_pixclk > 0)
  4965. return 266667;
  4966. else
  4967. return 200000;
  4968. }
  4969. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  4970. int max_pixclk)
  4971. {
  4972. /*
  4973. * FIXME:
  4974. * - remove the guardband, it's not needed on BXT
  4975. * - set 19.2MHz bypass frequency if there are no active pipes
  4976. */
  4977. if (max_pixclk > 576000*9/10)
  4978. return 624000;
  4979. else if (max_pixclk > 384000*9/10)
  4980. return 576000;
  4981. else if (max_pixclk > 288000*9/10)
  4982. return 384000;
  4983. else if (max_pixclk > 144000*9/10)
  4984. return 288000;
  4985. else
  4986. return 144000;
  4987. }
  4988. /* Compute the max pixel clock for new configuration. Uses atomic state if
  4989. * that's non-NULL, look at current state otherwise. */
  4990. static int intel_mode_max_pixclk(struct drm_device *dev,
  4991. struct drm_atomic_state *state)
  4992. {
  4993. struct intel_crtc *intel_crtc;
  4994. struct intel_crtc_state *crtc_state;
  4995. int max_pixclk = 0;
  4996. for_each_intel_crtc(dev, intel_crtc) {
  4997. if (state)
  4998. crtc_state =
  4999. intel_atomic_get_crtc_state(state, intel_crtc);
  5000. else
  5001. crtc_state = intel_crtc->config;
  5002. if (IS_ERR(crtc_state))
  5003. return PTR_ERR(crtc_state);
  5004. if (!crtc_state->base.enable)
  5005. continue;
  5006. max_pixclk = max(max_pixclk,
  5007. crtc_state->base.adjusted_mode.crtc_clock);
  5008. }
  5009. return max_pixclk;
  5010. }
  5011. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  5012. {
  5013. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5014. struct drm_crtc *crtc;
  5015. struct drm_crtc_state *crtc_state;
  5016. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  5017. int cdclk, ret = 0;
  5018. if (max_pixclk < 0)
  5019. return max_pixclk;
  5020. if (IS_VALLEYVIEW(dev_priv))
  5021. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5022. else
  5023. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5024. if (cdclk == dev_priv->cdclk_freq)
  5025. return 0;
  5026. /* add all active pipes to the state */
  5027. for_each_crtc(state->dev, crtc) {
  5028. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5029. if (IS_ERR(crtc_state))
  5030. return PTR_ERR(crtc_state);
  5031. if (!crtc_state->active || needs_modeset(crtc_state))
  5032. continue;
  5033. crtc_state->mode_changed = true;
  5034. ret = drm_atomic_add_affected_connectors(state, crtc);
  5035. if (ret)
  5036. break;
  5037. ret = drm_atomic_add_affected_planes(state, crtc);
  5038. if (ret)
  5039. break;
  5040. }
  5041. return ret;
  5042. }
  5043. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5044. {
  5045. unsigned int credits, default_credits;
  5046. if (IS_CHERRYVIEW(dev_priv))
  5047. default_credits = PFI_CREDIT(12);
  5048. else
  5049. default_credits = PFI_CREDIT(8);
  5050. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5051. /* CHV suggested value is 31 or 63 */
  5052. if (IS_CHERRYVIEW(dev_priv))
  5053. credits = PFI_CREDIT_63;
  5054. else
  5055. credits = PFI_CREDIT(15);
  5056. } else {
  5057. credits = default_credits;
  5058. }
  5059. /*
  5060. * WA - write default credits before re-programming
  5061. * FIXME: should we also set the resend bit here?
  5062. */
  5063. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5064. default_credits);
  5065. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5066. credits | PFI_CREDIT_RESEND);
  5067. /*
  5068. * FIXME is this guaranteed to clear
  5069. * immediately or should we poll for it?
  5070. */
  5071. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5072. }
  5073. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5074. {
  5075. struct drm_device *dev = old_state->dev;
  5076. struct drm_i915_private *dev_priv = dev->dev_private;
  5077. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5078. int req_cdclk;
  5079. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5080. * never fail. */
  5081. if (WARN_ON(max_pixclk < 0))
  5082. return;
  5083. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5084. if (req_cdclk != dev_priv->cdclk_freq) {
  5085. /*
  5086. * FIXME: We can end up here with all power domains off, yet
  5087. * with a CDCLK frequency other than the minimum. To account
  5088. * for this take the PIPE-A power domain, which covers the HW
  5089. * blocks needed for the following programming. This can be
  5090. * removed once it's guaranteed that we get here either with
  5091. * the minimum CDCLK set, or the required power domains
  5092. * enabled.
  5093. */
  5094. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5095. if (IS_CHERRYVIEW(dev))
  5096. cherryview_set_cdclk(dev, req_cdclk);
  5097. else
  5098. valleyview_set_cdclk(dev, req_cdclk);
  5099. vlv_program_pfi_credits(dev_priv);
  5100. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5101. }
  5102. }
  5103. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5104. {
  5105. struct drm_device *dev = crtc->dev;
  5106. struct drm_i915_private *dev_priv = to_i915(dev);
  5107. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5108. struct intel_encoder *encoder;
  5109. int pipe = intel_crtc->pipe;
  5110. bool is_dsi;
  5111. if (WARN_ON(intel_crtc->active))
  5112. return;
  5113. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5114. if (!is_dsi) {
  5115. if (IS_CHERRYVIEW(dev))
  5116. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5117. else
  5118. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5119. }
  5120. if (intel_crtc->config->has_dp_encoder)
  5121. intel_dp_set_m_n(intel_crtc, M1_N1);
  5122. intel_set_pipe_timings(intel_crtc);
  5123. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5124. struct drm_i915_private *dev_priv = dev->dev_private;
  5125. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5126. I915_WRITE(CHV_CANVAS(pipe), 0);
  5127. }
  5128. i9xx_set_pipeconf(intel_crtc);
  5129. intel_crtc->active = true;
  5130. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5131. for_each_encoder_on_crtc(dev, crtc, encoder)
  5132. if (encoder->pre_pll_enable)
  5133. encoder->pre_pll_enable(encoder);
  5134. if (!is_dsi) {
  5135. if (IS_CHERRYVIEW(dev))
  5136. chv_enable_pll(intel_crtc, intel_crtc->config);
  5137. else
  5138. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5139. }
  5140. for_each_encoder_on_crtc(dev, crtc, encoder)
  5141. if (encoder->pre_enable)
  5142. encoder->pre_enable(encoder);
  5143. i9xx_pfit_enable(intel_crtc);
  5144. intel_crtc_load_lut(crtc);
  5145. intel_update_watermarks(crtc);
  5146. intel_enable_pipe(intel_crtc);
  5147. assert_vblank_disabled(crtc);
  5148. drm_crtc_vblank_on(crtc);
  5149. for_each_encoder_on_crtc(dev, crtc, encoder)
  5150. encoder->enable(encoder);
  5151. }
  5152. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5153. {
  5154. struct drm_device *dev = crtc->base.dev;
  5155. struct drm_i915_private *dev_priv = dev->dev_private;
  5156. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5157. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5158. }
  5159. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5160. {
  5161. struct drm_device *dev = crtc->dev;
  5162. struct drm_i915_private *dev_priv = to_i915(dev);
  5163. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5164. struct intel_encoder *encoder;
  5165. int pipe = intel_crtc->pipe;
  5166. if (WARN_ON(intel_crtc->active))
  5167. return;
  5168. i9xx_set_pll_dividers(intel_crtc);
  5169. if (intel_crtc->config->has_dp_encoder)
  5170. intel_dp_set_m_n(intel_crtc, M1_N1);
  5171. intel_set_pipe_timings(intel_crtc);
  5172. i9xx_set_pipeconf(intel_crtc);
  5173. intel_crtc->active = true;
  5174. if (!IS_GEN2(dev))
  5175. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5176. for_each_encoder_on_crtc(dev, crtc, encoder)
  5177. if (encoder->pre_enable)
  5178. encoder->pre_enable(encoder);
  5179. i9xx_enable_pll(intel_crtc);
  5180. i9xx_pfit_enable(intel_crtc);
  5181. intel_crtc_load_lut(crtc);
  5182. intel_update_watermarks(crtc);
  5183. intel_enable_pipe(intel_crtc);
  5184. assert_vblank_disabled(crtc);
  5185. drm_crtc_vblank_on(crtc);
  5186. for_each_encoder_on_crtc(dev, crtc, encoder)
  5187. encoder->enable(encoder);
  5188. }
  5189. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5190. {
  5191. struct drm_device *dev = crtc->base.dev;
  5192. struct drm_i915_private *dev_priv = dev->dev_private;
  5193. if (!crtc->config->gmch_pfit.control)
  5194. return;
  5195. assert_pipe_disabled(dev_priv, crtc->pipe);
  5196. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5197. I915_READ(PFIT_CONTROL));
  5198. I915_WRITE(PFIT_CONTROL, 0);
  5199. }
  5200. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5201. {
  5202. struct drm_device *dev = crtc->dev;
  5203. struct drm_i915_private *dev_priv = dev->dev_private;
  5204. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5205. struct intel_encoder *encoder;
  5206. int pipe = intel_crtc->pipe;
  5207. if (WARN_ON(!intel_crtc->active))
  5208. return;
  5209. /*
  5210. * On gen2 planes are double buffered but the pipe isn't, so we must
  5211. * wait for planes to fully turn off before disabling the pipe.
  5212. * We also need to wait on all gmch platforms because of the
  5213. * self-refresh mode constraint explained above.
  5214. */
  5215. intel_wait_for_vblank(dev, pipe);
  5216. for_each_encoder_on_crtc(dev, crtc, encoder)
  5217. encoder->disable(encoder);
  5218. drm_crtc_vblank_off(crtc);
  5219. assert_vblank_disabled(crtc);
  5220. intel_disable_pipe(intel_crtc);
  5221. i9xx_pfit_disable(intel_crtc);
  5222. for_each_encoder_on_crtc(dev, crtc, encoder)
  5223. if (encoder->post_disable)
  5224. encoder->post_disable(encoder);
  5225. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5226. if (IS_CHERRYVIEW(dev))
  5227. chv_disable_pll(dev_priv, pipe);
  5228. else if (IS_VALLEYVIEW(dev))
  5229. vlv_disable_pll(dev_priv, pipe);
  5230. else
  5231. i9xx_disable_pll(intel_crtc);
  5232. }
  5233. if (!IS_GEN2(dev))
  5234. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5235. intel_crtc->active = false;
  5236. intel_update_watermarks(crtc);
  5237. mutex_lock(&dev->struct_mutex);
  5238. intel_fbc_update(dev);
  5239. mutex_unlock(&dev->struct_mutex);
  5240. }
  5241. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5242. {
  5243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5244. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5245. enum intel_display_power_domain domain;
  5246. unsigned long domains;
  5247. if (!intel_crtc->active)
  5248. return;
  5249. intel_crtc_disable_planes(crtc);
  5250. dev_priv->display.crtc_disable(crtc);
  5251. domains = intel_crtc->enabled_power_domains;
  5252. for_each_power_domain(domain, domains)
  5253. intel_display_power_put(dev_priv, domain);
  5254. intel_crtc->enabled_power_domains = 0;
  5255. }
  5256. /*
  5257. * turn all crtc's off, but do not adjust state
  5258. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5259. */
  5260. void intel_display_suspend(struct drm_device *dev)
  5261. {
  5262. struct drm_crtc *crtc;
  5263. for_each_crtc(dev, crtc)
  5264. intel_crtc_disable_noatomic(crtc);
  5265. }
  5266. /* Master function to enable/disable CRTC and corresponding power wells */
  5267. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5268. {
  5269. struct drm_device *dev = crtc->dev;
  5270. struct drm_mode_config *config = &dev->mode_config;
  5271. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5273. struct intel_crtc_state *pipe_config;
  5274. struct drm_atomic_state *state;
  5275. int ret;
  5276. if (enable == intel_crtc->active)
  5277. return 0;
  5278. if (enable && !crtc->state->enable)
  5279. return 0;
  5280. /* this function should be called with drm_modeset_lock_all for now */
  5281. if (WARN_ON(!ctx))
  5282. return -EIO;
  5283. lockdep_assert_held(&ctx->ww_ctx);
  5284. state = drm_atomic_state_alloc(dev);
  5285. if (WARN_ON(!state))
  5286. return -ENOMEM;
  5287. state->acquire_ctx = ctx;
  5288. state->allow_modeset = true;
  5289. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5290. if (IS_ERR(pipe_config)) {
  5291. ret = PTR_ERR(pipe_config);
  5292. goto err;
  5293. }
  5294. pipe_config->base.active = enable;
  5295. ret = intel_set_mode(state);
  5296. if (!ret)
  5297. return ret;
  5298. err:
  5299. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5300. drm_atomic_state_free(state);
  5301. return ret;
  5302. }
  5303. /**
  5304. * Sets the power management mode of the pipe and plane.
  5305. */
  5306. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5307. {
  5308. struct drm_device *dev = crtc->dev;
  5309. struct intel_encoder *intel_encoder;
  5310. bool enable = false;
  5311. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5312. enable |= intel_encoder->connectors_active;
  5313. intel_crtc_control(crtc, enable);
  5314. }
  5315. void intel_encoder_destroy(struct drm_encoder *encoder)
  5316. {
  5317. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5318. drm_encoder_cleanup(encoder);
  5319. kfree(intel_encoder);
  5320. }
  5321. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5322. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5323. * state of the entire output pipe. */
  5324. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5325. {
  5326. if (mode == DRM_MODE_DPMS_ON) {
  5327. encoder->connectors_active = true;
  5328. intel_crtc_update_dpms(encoder->base.crtc);
  5329. } else {
  5330. encoder->connectors_active = false;
  5331. intel_crtc_update_dpms(encoder->base.crtc);
  5332. }
  5333. }
  5334. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5335. * internal consistency). */
  5336. static void intel_connector_check_state(struct intel_connector *connector)
  5337. {
  5338. if (connector->get_hw_state(connector)) {
  5339. struct intel_encoder *encoder = connector->encoder;
  5340. struct drm_crtc *crtc;
  5341. bool encoder_enabled;
  5342. enum pipe pipe;
  5343. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5344. connector->base.base.id,
  5345. connector->base.name);
  5346. /* there is no real hw state for MST connectors */
  5347. if (connector->mst_port)
  5348. return;
  5349. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5350. "wrong connector dpms state\n");
  5351. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5352. "active connector not linked to encoder\n");
  5353. if (encoder) {
  5354. I915_STATE_WARN(!encoder->connectors_active,
  5355. "encoder->connectors_active not set\n");
  5356. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5357. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5358. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5359. return;
  5360. crtc = encoder->base.crtc;
  5361. I915_STATE_WARN(!crtc->state->enable,
  5362. "crtc not enabled\n");
  5363. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5364. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5365. "encoder active on the wrong pipe\n");
  5366. }
  5367. }
  5368. }
  5369. int intel_connector_init(struct intel_connector *connector)
  5370. {
  5371. struct drm_connector_state *connector_state;
  5372. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5373. if (!connector_state)
  5374. return -ENOMEM;
  5375. connector->base.state = connector_state;
  5376. return 0;
  5377. }
  5378. struct intel_connector *intel_connector_alloc(void)
  5379. {
  5380. struct intel_connector *connector;
  5381. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5382. if (!connector)
  5383. return NULL;
  5384. if (intel_connector_init(connector) < 0) {
  5385. kfree(connector);
  5386. return NULL;
  5387. }
  5388. return connector;
  5389. }
  5390. /* Even simpler default implementation, if there's really no special case to
  5391. * consider. */
  5392. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5393. {
  5394. /* All the simple cases only support two dpms states. */
  5395. if (mode != DRM_MODE_DPMS_ON)
  5396. mode = DRM_MODE_DPMS_OFF;
  5397. if (mode == connector->dpms)
  5398. return;
  5399. connector->dpms = mode;
  5400. /* Only need to change hw state when actually enabled */
  5401. if (connector->encoder)
  5402. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5403. intel_modeset_check_state(connector->dev);
  5404. }
  5405. /* Simple connector->get_hw_state implementation for encoders that support only
  5406. * one connector and no cloning and hence the encoder state determines the state
  5407. * of the connector. */
  5408. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5409. {
  5410. enum pipe pipe = 0;
  5411. struct intel_encoder *encoder = connector->encoder;
  5412. return encoder->get_hw_state(encoder, &pipe);
  5413. }
  5414. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5415. {
  5416. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5417. return crtc_state->fdi_lanes;
  5418. return 0;
  5419. }
  5420. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5421. struct intel_crtc_state *pipe_config)
  5422. {
  5423. struct drm_atomic_state *state = pipe_config->base.state;
  5424. struct intel_crtc *other_crtc;
  5425. struct intel_crtc_state *other_crtc_state;
  5426. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5427. pipe_name(pipe), pipe_config->fdi_lanes);
  5428. if (pipe_config->fdi_lanes > 4) {
  5429. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5430. pipe_name(pipe), pipe_config->fdi_lanes);
  5431. return -EINVAL;
  5432. }
  5433. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5434. if (pipe_config->fdi_lanes > 2) {
  5435. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5436. pipe_config->fdi_lanes);
  5437. return -EINVAL;
  5438. } else {
  5439. return 0;
  5440. }
  5441. }
  5442. if (INTEL_INFO(dev)->num_pipes == 2)
  5443. return 0;
  5444. /* Ivybridge 3 pipe is really complicated */
  5445. switch (pipe) {
  5446. case PIPE_A:
  5447. return 0;
  5448. case PIPE_B:
  5449. if (pipe_config->fdi_lanes <= 2)
  5450. return 0;
  5451. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5452. other_crtc_state =
  5453. intel_atomic_get_crtc_state(state, other_crtc);
  5454. if (IS_ERR(other_crtc_state))
  5455. return PTR_ERR(other_crtc_state);
  5456. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5457. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5458. pipe_name(pipe), pipe_config->fdi_lanes);
  5459. return -EINVAL;
  5460. }
  5461. return 0;
  5462. case PIPE_C:
  5463. if (pipe_config->fdi_lanes > 2) {
  5464. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5465. pipe_name(pipe), pipe_config->fdi_lanes);
  5466. return -EINVAL;
  5467. }
  5468. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5469. other_crtc_state =
  5470. intel_atomic_get_crtc_state(state, other_crtc);
  5471. if (IS_ERR(other_crtc_state))
  5472. return PTR_ERR(other_crtc_state);
  5473. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5474. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5475. return -EINVAL;
  5476. }
  5477. return 0;
  5478. default:
  5479. BUG();
  5480. }
  5481. }
  5482. #define RETRY 1
  5483. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5484. struct intel_crtc_state *pipe_config)
  5485. {
  5486. struct drm_device *dev = intel_crtc->base.dev;
  5487. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5488. int lane, link_bw, fdi_dotclock, ret;
  5489. bool needs_recompute = false;
  5490. retry:
  5491. /* FDI is a binary signal running at ~2.7GHz, encoding
  5492. * each output octet as 10 bits. The actual frequency
  5493. * is stored as a divider into a 100MHz clock, and the
  5494. * mode pixel clock is stored in units of 1KHz.
  5495. * Hence the bw of each lane in terms of the mode signal
  5496. * is:
  5497. */
  5498. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5499. fdi_dotclock = adjusted_mode->crtc_clock;
  5500. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5501. pipe_config->pipe_bpp);
  5502. pipe_config->fdi_lanes = lane;
  5503. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5504. link_bw, &pipe_config->fdi_m_n);
  5505. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5506. intel_crtc->pipe, pipe_config);
  5507. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5508. pipe_config->pipe_bpp -= 2*3;
  5509. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5510. pipe_config->pipe_bpp);
  5511. needs_recompute = true;
  5512. pipe_config->bw_constrained = true;
  5513. goto retry;
  5514. }
  5515. if (needs_recompute)
  5516. return RETRY;
  5517. return ret;
  5518. }
  5519. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5520. struct intel_crtc_state *pipe_config)
  5521. {
  5522. if (pipe_config->pipe_bpp > 24)
  5523. return false;
  5524. /* HSW can handle pixel rate up to cdclk? */
  5525. if (IS_HASWELL(dev_priv->dev))
  5526. return true;
  5527. /*
  5528. * We compare against max which means we must take
  5529. * the increased cdclk requirement into account when
  5530. * calculating the new cdclk.
  5531. *
  5532. * Should measure whether using a lower cdclk w/o IPS
  5533. */
  5534. return ilk_pipe_pixel_rate(pipe_config) <=
  5535. dev_priv->max_cdclk_freq * 95 / 100;
  5536. }
  5537. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5538. struct intel_crtc_state *pipe_config)
  5539. {
  5540. struct drm_device *dev = crtc->base.dev;
  5541. struct drm_i915_private *dev_priv = dev->dev_private;
  5542. pipe_config->ips_enabled = i915.enable_ips &&
  5543. hsw_crtc_supports_ips(crtc) &&
  5544. pipe_config_supports_ips(dev_priv, pipe_config);
  5545. }
  5546. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5547. struct intel_crtc_state *pipe_config)
  5548. {
  5549. struct drm_device *dev = crtc->base.dev;
  5550. struct drm_i915_private *dev_priv = dev->dev_private;
  5551. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5552. /* FIXME should check pixel clock limits on all platforms */
  5553. if (INTEL_INFO(dev)->gen < 4) {
  5554. int clock_limit = dev_priv->max_cdclk_freq;
  5555. /*
  5556. * Enable pixel doubling when the dot clock
  5557. * is > 90% of the (display) core speed.
  5558. *
  5559. * GDG double wide on either pipe,
  5560. * otherwise pipe A only.
  5561. */
  5562. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5563. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5564. clock_limit *= 2;
  5565. pipe_config->double_wide = true;
  5566. }
  5567. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5568. return -EINVAL;
  5569. }
  5570. /*
  5571. * Pipe horizontal size must be even in:
  5572. * - DVO ganged mode
  5573. * - LVDS dual channel mode
  5574. * - Double wide pipe
  5575. */
  5576. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5577. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5578. pipe_config->pipe_src_w &= ~1;
  5579. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5580. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5581. */
  5582. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5583. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5584. return -EINVAL;
  5585. if (HAS_IPS(dev))
  5586. hsw_compute_ips_config(crtc, pipe_config);
  5587. if (pipe_config->has_pch_encoder)
  5588. return ironlake_fdi_compute_config(crtc, pipe_config);
  5589. return 0;
  5590. }
  5591. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5592. {
  5593. struct drm_i915_private *dev_priv = to_i915(dev);
  5594. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5595. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5596. uint32_t linkrate;
  5597. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5598. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5599. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5600. return 540000;
  5601. linkrate = (I915_READ(DPLL_CTRL1) &
  5602. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5603. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5604. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5605. /* vco 8640 */
  5606. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5607. case CDCLK_FREQ_450_432:
  5608. return 432000;
  5609. case CDCLK_FREQ_337_308:
  5610. return 308570;
  5611. case CDCLK_FREQ_675_617:
  5612. return 617140;
  5613. default:
  5614. WARN(1, "Unknown cd freq selection\n");
  5615. }
  5616. } else {
  5617. /* vco 8100 */
  5618. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5619. case CDCLK_FREQ_450_432:
  5620. return 450000;
  5621. case CDCLK_FREQ_337_308:
  5622. return 337500;
  5623. case CDCLK_FREQ_675_617:
  5624. return 675000;
  5625. default:
  5626. WARN(1, "Unknown cd freq selection\n");
  5627. }
  5628. }
  5629. /* error case, do as if DPLL0 isn't enabled */
  5630. return 24000;
  5631. }
  5632. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5633. {
  5634. struct drm_i915_private *dev_priv = dev->dev_private;
  5635. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5636. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5637. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5638. return 800000;
  5639. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5640. return 450000;
  5641. else if (freq == LCPLL_CLK_FREQ_450)
  5642. return 450000;
  5643. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5644. return 540000;
  5645. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5646. return 337500;
  5647. else
  5648. return 675000;
  5649. }
  5650. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5651. {
  5652. struct drm_i915_private *dev_priv = dev->dev_private;
  5653. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5654. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5655. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5656. return 800000;
  5657. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5658. return 450000;
  5659. else if (freq == LCPLL_CLK_FREQ_450)
  5660. return 450000;
  5661. else if (IS_HSW_ULT(dev))
  5662. return 337500;
  5663. else
  5664. return 540000;
  5665. }
  5666. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5667. {
  5668. struct drm_i915_private *dev_priv = dev->dev_private;
  5669. u32 val;
  5670. int divider;
  5671. if (dev_priv->hpll_freq == 0)
  5672. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5673. mutex_lock(&dev_priv->sb_lock);
  5674. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5675. mutex_unlock(&dev_priv->sb_lock);
  5676. divider = val & DISPLAY_FREQUENCY_VALUES;
  5677. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5678. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5679. "cdclk change in progress\n");
  5680. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5681. }
  5682. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5683. {
  5684. return 450000;
  5685. }
  5686. static int i945_get_display_clock_speed(struct drm_device *dev)
  5687. {
  5688. return 400000;
  5689. }
  5690. static int i915_get_display_clock_speed(struct drm_device *dev)
  5691. {
  5692. return 333333;
  5693. }
  5694. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5695. {
  5696. return 200000;
  5697. }
  5698. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5699. {
  5700. u16 gcfgc = 0;
  5701. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5702. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5703. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5704. return 266667;
  5705. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5706. return 333333;
  5707. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5708. return 444444;
  5709. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5710. return 200000;
  5711. default:
  5712. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5713. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5714. return 133333;
  5715. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5716. return 166667;
  5717. }
  5718. }
  5719. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5720. {
  5721. u16 gcfgc = 0;
  5722. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5723. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5724. return 133333;
  5725. else {
  5726. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5727. case GC_DISPLAY_CLOCK_333_MHZ:
  5728. return 333333;
  5729. default:
  5730. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5731. return 190000;
  5732. }
  5733. }
  5734. }
  5735. static int i865_get_display_clock_speed(struct drm_device *dev)
  5736. {
  5737. return 266667;
  5738. }
  5739. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5740. {
  5741. u16 hpllcc = 0;
  5742. /*
  5743. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5744. * encoding is different :(
  5745. * FIXME is this the right way to detect 852GM/852GMV?
  5746. */
  5747. if (dev->pdev->revision == 0x1)
  5748. return 133333;
  5749. pci_bus_read_config_word(dev->pdev->bus,
  5750. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5751. /* Assume that the hardware is in the high speed state. This
  5752. * should be the default.
  5753. */
  5754. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5755. case GC_CLOCK_133_200:
  5756. case GC_CLOCK_133_200_2:
  5757. case GC_CLOCK_100_200:
  5758. return 200000;
  5759. case GC_CLOCK_166_250:
  5760. return 250000;
  5761. case GC_CLOCK_100_133:
  5762. return 133333;
  5763. case GC_CLOCK_133_266:
  5764. case GC_CLOCK_133_266_2:
  5765. case GC_CLOCK_166_266:
  5766. return 266667;
  5767. }
  5768. /* Shouldn't happen */
  5769. return 0;
  5770. }
  5771. static int i830_get_display_clock_speed(struct drm_device *dev)
  5772. {
  5773. return 133333;
  5774. }
  5775. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5776. {
  5777. struct drm_i915_private *dev_priv = dev->dev_private;
  5778. static const unsigned int blb_vco[8] = {
  5779. [0] = 3200000,
  5780. [1] = 4000000,
  5781. [2] = 5333333,
  5782. [3] = 4800000,
  5783. [4] = 6400000,
  5784. };
  5785. static const unsigned int pnv_vco[8] = {
  5786. [0] = 3200000,
  5787. [1] = 4000000,
  5788. [2] = 5333333,
  5789. [3] = 4800000,
  5790. [4] = 2666667,
  5791. };
  5792. static const unsigned int cl_vco[8] = {
  5793. [0] = 3200000,
  5794. [1] = 4000000,
  5795. [2] = 5333333,
  5796. [3] = 6400000,
  5797. [4] = 3333333,
  5798. [5] = 3566667,
  5799. [6] = 4266667,
  5800. };
  5801. static const unsigned int elk_vco[8] = {
  5802. [0] = 3200000,
  5803. [1] = 4000000,
  5804. [2] = 5333333,
  5805. [3] = 4800000,
  5806. };
  5807. static const unsigned int ctg_vco[8] = {
  5808. [0] = 3200000,
  5809. [1] = 4000000,
  5810. [2] = 5333333,
  5811. [3] = 6400000,
  5812. [4] = 2666667,
  5813. [5] = 4266667,
  5814. };
  5815. const unsigned int *vco_table;
  5816. unsigned int vco;
  5817. uint8_t tmp = 0;
  5818. /* FIXME other chipsets? */
  5819. if (IS_GM45(dev))
  5820. vco_table = ctg_vco;
  5821. else if (IS_G4X(dev))
  5822. vco_table = elk_vco;
  5823. else if (IS_CRESTLINE(dev))
  5824. vco_table = cl_vco;
  5825. else if (IS_PINEVIEW(dev))
  5826. vco_table = pnv_vco;
  5827. else if (IS_G33(dev))
  5828. vco_table = blb_vco;
  5829. else
  5830. return 0;
  5831. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5832. vco = vco_table[tmp & 0x7];
  5833. if (vco == 0)
  5834. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5835. else
  5836. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5837. return vco;
  5838. }
  5839. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5840. {
  5841. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5842. uint16_t tmp = 0;
  5843. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5844. cdclk_sel = (tmp >> 12) & 0x1;
  5845. switch (vco) {
  5846. case 2666667:
  5847. case 4000000:
  5848. case 5333333:
  5849. return cdclk_sel ? 333333 : 222222;
  5850. case 3200000:
  5851. return cdclk_sel ? 320000 : 228571;
  5852. default:
  5853. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5854. return 222222;
  5855. }
  5856. }
  5857. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5858. {
  5859. static const uint8_t div_3200[] = { 16, 10, 8 };
  5860. static const uint8_t div_4000[] = { 20, 12, 10 };
  5861. static const uint8_t div_5333[] = { 24, 16, 14 };
  5862. const uint8_t *div_table;
  5863. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5864. uint16_t tmp = 0;
  5865. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5866. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5867. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5868. goto fail;
  5869. switch (vco) {
  5870. case 3200000:
  5871. div_table = div_3200;
  5872. break;
  5873. case 4000000:
  5874. div_table = div_4000;
  5875. break;
  5876. case 5333333:
  5877. div_table = div_5333;
  5878. break;
  5879. default:
  5880. goto fail;
  5881. }
  5882. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5883. fail:
  5884. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5885. return 200000;
  5886. }
  5887. static int g33_get_display_clock_speed(struct drm_device *dev)
  5888. {
  5889. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5890. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5891. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5892. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5893. const uint8_t *div_table;
  5894. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5895. uint16_t tmp = 0;
  5896. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5897. cdclk_sel = (tmp >> 4) & 0x7;
  5898. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5899. goto fail;
  5900. switch (vco) {
  5901. case 3200000:
  5902. div_table = div_3200;
  5903. break;
  5904. case 4000000:
  5905. div_table = div_4000;
  5906. break;
  5907. case 4800000:
  5908. div_table = div_4800;
  5909. break;
  5910. case 5333333:
  5911. div_table = div_5333;
  5912. break;
  5913. default:
  5914. goto fail;
  5915. }
  5916. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5917. fail:
  5918. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5919. return 190476;
  5920. }
  5921. static void
  5922. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5923. {
  5924. while (*num > DATA_LINK_M_N_MASK ||
  5925. *den > DATA_LINK_M_N_MASK) {
  5926. *num >>= 1;
  5927. *den >>= 1;
  5928. }
  5929. }
  5930. static void compute_m_n(unsigned int m, unsigned int n,
  5931. uint32_t *ret_m, uint32_t *ret_n)
  5932. {
  5933. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5934. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5935. intel_reduce_m_n_ratio(ret_m, ret_n);
  5936. }
  5937. void
  5938. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5939. int pixel_clock, int link_clock,
  5940. struct intel_link_m_n *m_n)
  5941. {
  5942. m_n->tu = 64;
  5943. compute_m_n(bits_per_pixel * pixel_clock,
  5944. link_clock * nlanes * 8,
  5945. &m_n->gmch_m, &m_n->gmch_n);
  5946. compute_m_n(pixel_clock, link_clock,
  5947. &m_n->link_m, &m_n->link_n);
  5948. }
  5949. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5950. {
  5951. if (i915.panel_use_ssc >= 0)
  5952. return i915.panel_use_ssc != 0;
  5953. return dev_priv->vbt.lvds_use_ssc
  5954. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5955. }
  5956. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5957. int num_connectors)
  5958. {
  5959. struct drm_device *dev = crtc_state->base.crtc->dev;
  5960. struct drm_i915_private *dev_priv = dev->dev_private;
  5961. int refclk;
  5962. WARN_ON(!crtc_state->base.state);
  5963. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5964. refclk = 100000;
  5965. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5966. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5967. refclk = dev_priv->vbt.lvds_ssc_freq;
  5968. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5969. } else if (!IS_GEN2(dev)) {
  5970. refclk = 96000;
  5971. } else {
  5972. refclk = 48000;
  5973. }
  5974. return refclk;
  5975. }
  5976. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5977. {
  5978. return (1 << dpll->n) << 16 | dpll->m2;
  5979. }
  5980. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5981. {
  5982. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5983. }
  5984. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5985. struct intel_crtc_state *crtc_state,
  5986. intel_clock_t *reduced_clock)
  5987. {
  5988. struct drm_device *dev = crtc->base.dev;
  5989. u32 fp, fp2 = 0;
  5990. if (IS_PINEVIEW(dev)) {
  5991. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5992. if (reduced_clock)
  5993. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5994. } else {
  5995. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5996. if (reduced_clock)
  5997. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5998. }
  5999. crtc_state->dpll_hw_state.fp0 = fp;
  6000. crtc->lowfreq_avail = false;
  6001. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6002. reduced_clock) {
  6003. crtc_state->dpll_hw_state.fp1 = fp2;
  6004. crtc->lowfreq_avail = true;
  6005. } else {
  6006. crtc_state->dpll_hw_state.fp1 = fp;
  6007. }
  6008. }
  6009. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6010. pipe)
  6011. {
  6012. u32 reg_val;
  6013. /*
  6014. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6015. * and set it to a reasonable value instead.
  6016. */
  6017. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6018. reg_val &= 0xffffff00;
  6019. reg_val |= 0x00000030;
  6020. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6021. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6022. reg_val &= 0x8cffffff;
  6023. reg_val = 0x8c000000;
  6024. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6025. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6026. reg_val &= 0xffffff00;
  6027. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6028. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6029. reg_val &= 0x00ffffff;
  6030. reg_val |= 0xb0000000;
  6031. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6032. }
  6033. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6034. struct intel_link_m_n *m_n)
  6035. {
  6036. struct drm_device *dev = crtc->base.dev;
  6037. struct drm_i915_private *dev_priv = dev->dev_private;
  6038. int pipe = crtc->pipe;
  6039. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6040. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6041. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6042. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6043. }
  6044. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6045. struct intel_link_m_n *m_n,
  6046. struct intel_link_m_n *m2_n2)
  6047. {
  6048. struct drm_device *dev = crtc->base.dev;
  6049. struct drm_i915_private *dev_priv = dev->dev_private;
  6050. int pipe = crtc->pipe;
  6051. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6052. if (INTEL_INFO(dev)->gen >= 5) {
  6053. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6054. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6055. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6056. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6057. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6058. * for gen < 8) and if DRRS is supported (to make sure the
  6059. * registers are not unnecessarily accessed).
  6060. */
  6061. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6062. crtc->config->has_drrs) {
  6063. I915_WRITE(PIPE_DATA_M2(transcoder),
  6064. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6065. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6066. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6067. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6068. }
  6069. } else {
  6070. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6071. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6072. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6073. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6074. }
  6075. }
  6076. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6077. {
  6078. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6079. if (m_n == M1_N1) {
  6080. dp_m_n = &crtc->config->dp_m_n;
  6081. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6082. } else if (m_n == M2_N2) {
  6083. /*
  6084. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6085. * needs to be programmed into M1_N1.
  6086. */
  6087. dp_m_n = &crtc->config->dp_m2_n2;
  6088. } else {
  6089. DRM_ERROR("Unsupported divider value\n");
  6090. return;
  6091. }
  6092. if (crtc->config->has_pch_encoder)
  6093. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6094. else
  6095. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6096. }
  6097. static void vlv_update_pll(struct intel_crtc *crtc,
  6098. struct intel_crtc_state *pipe_config)
  6099. {
  6100. u32 dpll, dpll_md;
  6101. /*
  6102. * Enable DPIO clock input. We should never disable the reference
  6103. * clock for pipe B, since VGA hotplug / manual detection depends
  6104. * on it.
  6105. */
  6106. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6107. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6108. /* We should never disable this, set it here for state tracking */
  6109. if (crtc->pipe == PIPE_B)
  6110. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6111. dpll |= DPLL_VCO_ENABLE;
  6112. pipe_config->dpll_hw_state.dpll = dpll;
  6113. dpll_md = (pipe_config->pixel_multiplier - 1)
  6114. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6115. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6116. }
  6117. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6118. const struct intel_crtc_state *pipe_config)
  6119. {
  6120. struct drm_device *dev = crtc->base.dev;
  6121. struct drm_i915_private *dev_priv = dev->dev_private;
  6122. int pipe = crtc->pipe;
  6123. u32 mdiv;
  6124. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6125. u32 coreclk, reg_val;
  6126. mutex_lock(&dev_priv->sb_lock);
  6127. bestn = pipe_config->dpll.n;
  6128. bestm1 = pipe_config->dpll.m1;
  6129. bestm2 = pipe_config->dpll.m2;
  6130. bestp1 = pipe_config->dpll.p1;
  6131. bestp2 = pipe_config->dpll.p2;
  6132. /* See eDP HDMI DPIO driver vbios notes doc */
  6133. /* PLL B needs special handling */
  6134. if (pipe == PIPE_B)
  6135. vlv_pllb_recal_opamp(dev_priv, pipe);
  6136. /* Set up Tx target for periodic Rcomp update */
  6137. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6138. /* Disable target IRef on PLL */
  6139. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6140. reg_val &= 0x00ffffff;
  6141. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6142. /* Disable fast lock */
  6143. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6144. /* Set idtafcrecal before PLL is enabled */
  6145. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6146. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6147. mdiv |= ((bestn << DPIO_N_SHIFT));
  6148. mdiv |= (1 << DPIO_K_SHIFT);
  6149. /*
  6150. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6151. * but we don't support that).
  6152. * Note: don't use the DAC post divider as it seems unstable.
  6153. */
  6154. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6155. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6156. mdiv |= DPIO_ENABLE_CALIBRATION;
  6157. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6158. /* Set HBR and RBR LPF coefficients */
  6159. if (pipe_config->port_clock == 162000 ||
  6160. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6161. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6162. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6163. 0x009f0003);
  6164. else
  6165. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6166. 0x00d0000f);
  6167. if (pipe_config->has_dp_encoder) {
  6168. /* Use SSC source */
  6169. if (pipe == PIPE_A)
  6170. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6171. 0x0df40000);
  6172. else
  6173. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6174. 0x0df70000);
  6175. } else { /* HDMI or VGA */
  6176. /* Use bend source */
  6177. if (pipe == PIPE_A)
  6178. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6179. 0x0df70000);
  6180. else
  6181. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6182. 0x0df40000);
  6183. }
  6184. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6185. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6186. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6187. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6188. coreclk |= 0x01000000;
  6189. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6190. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6191. mutex_unlock(&dev_priv->sb_lock);
  6192. }
  6193. static void chv_update_pll(struct intel_crtc *crtc,
  6194. struct intel_crtc_state *pipe_config)
  6195. {
  6196. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6197. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6198. DPLL_VCO_ENABLE;
  6199. if (crtc->pipe != PIPE_A)
  6200. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6201. pipe_config->dpll_hw_state.dpll_md =
  6202. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6203. }
  6204. static void chv_prepare_pll(struct intel_crtc *crtc,
  6205. const struct intel_crtc_state *pipe_config)
  6206. {
  6207. struct drm_device *dev = crtc->base.dev;
  6208. struct drm_i915_private *dev_priv = dev->dev_private;
  6209. int pipe = crtc->pipe;
  6210. int dpll_reg = DPLL(crtc->pipe);
  6211. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6212. u32 loopfilter, tribuf_calcntr;
  6213. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6214. u32 dpio_val;
  6215. int vco;
  6216. bestn = pipe_config->dpll.n;
  6217. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6218. bestm1 = pipe_config->dpll.m1;
  6219. bestm2 = pipe_config->dpll.m2 >> 22;
  6220. bestp1 = pipe_config->dpll.p1;
  6221. bestp2 = pipe_config->dpll.p2;
  6222. vco = pipe_config->dpll.vco;
  6223. dpio_val = 0;
  6224. loopfilter = 0;
  6225. /*
  6226. * Enable Refclk and SSC
  6227. */
  6228. I915_WRITE(dpll_reg,
  6229. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6230. mutex_lock(&dev_priv->sb_lock);
  6231. /* p1 and p2 divider */
  6232. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6233. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6234. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6235. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6236. 1 << DPIO_CHV_K_DIV_SHIFT);
  6237. /* Feedback post-divider - m2 */
  6238. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6239. /* Feedback refclk divider - n and m1 */
  6240. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6241. DPIO_CHV_M1_DIV_BY_2 |
  6242. 1 << DPIO_CHV_N_DIV_SHIFT);
  6243. /* M2 fraction division */
  6244. if (bestm2_frac)
  6245. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6246. /* M2 fraction division enable */
  6247. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6248. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6249. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6250. if (bestm2_frac)
  6251. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6252. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6253. /* Program digital lock detect threshold */
  6254. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6255. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6256. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6257. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6258. if (!bestm2_frac)
  6259. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6260. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6261. /* Loop filter */
  6262. if (vco == 5400000) {
  6263. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6264. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6265. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6266. tribuf_calcntr = 0x9;
  6267. } else if (vco <= 6200000) {
  6268. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6269. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6270. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6271. tribuf_calcntr = 0x9;
  6272. } else if (vco <= 6480000) {
  6273. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6274. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6275. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6276. tribuf_calcntr = 0x8;
  6277. } else {
  6278. /* Not supported. Apply the same limits as in the max case */
  6279. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6280. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6281. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6282. tribuf_calcntr = 0;
  6283. }
  6284. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6285. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6286. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6287. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6288. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6289. /* AFC Recal */
  6290. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6291. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6292. DPIO_AFC_RECAL);
  6293. mutex_unlock(&dev_priv->sb_lock);
  6294. }
  6295. /**
  6296. * vlv_force_pll_on - forcibly enable just the PLL
  6297. * @dev_priv: i915 private structure
  6298. * @pipe: pipe PLL to enable
  6299. * @dpll: PLL configuration
  6300. *
  6301. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6302. * in cases where we need the PLL enabled even when @pipe is not going to
  6303. * be enabled.
  6304. */
  6305. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6306. const struct dpll *dpll)
  6307. {
  6308. struct intel_crtc *crtc =
  6309. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6310. struct intel_crtc_state pipe_config = {
  6311. .base.crtc = &crtc->base,
  6312. .pixel_multiplier = 1,
  6313. .dpll = *dpll,
  6314. };
  6315. if (IS_CHERRYVIEW(dev)) {
  6316. chv_update_pll(crtc, &pipe_config);
  6317. chv_prepare_pll(crtc, &pipe_config);
  6318. chv_enable_pll(crtc, &pipe_config);
  6319. } else {
  6320. vlv_update_pll(crtc, &pipe_config);
  6321. vlv_prepare_pll(crtc, &pipe_config);
  6322. vlv_enable_pll(crtc, &pipe_config);
  6323. }
  6324. }
  6325. /**
  6326. * vlv_force_pll_off - forcibly disable just the PLL
  6327. * @dev_priv: i915 private structure
  6328. * @pipe: pipe PLL to disable
  6329. *
  6330. * Disable the PLL for @pipe. To be used in cases where we need
  6331. * the PLL enabled even when @pipe is not going to be enabled.
  6332. */
  6333. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6334. {
  6335. if (IS_CHERRYVIEW(dev))
  6336. chv_disable_pll(to_i915(dev), pipe);
  6337. else
  6338. vlv_disable_pll(to_i915(dev), pipe);
  6339. }
  6340. static void i9xx_update_pll(struct intel_crtc *crtc,
  6341. struct intel_crtc_state *crtc_state,
  6342. intel_clock_t *reduced_clock,
  6343. int num_connectors)
  6344. {
  6345. struct drm_device *dev = crtc->base.dev;
  6346. struct drm_i915_private *dev_priv = dev->dev_private;
  6347. u32 dpll;
  6348. bool is_sdvo;
  6349. struct dpll *clock = &crtc_state->dpll;
  6350. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6351. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6352. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6353. dpll = DPLL_VGA_MODE_DIS;
  6354. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6355. dpll |= DPLLB_MODE_LVDS;
  6356. else
  6357. dpll |= DPLLB_MODE_DAC_SERIAL;
  6358. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6359. dpll |= (crtc_state->pixel_multiplier - 1)
  6360. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6361. }
  6362. if (is_sdvo)
  6363. dpll |= DPLL_SDVO_HIGH_SPEED;
  6364. if (crtc_state->has_dp_encoder)
  6365. dpll |= DPLL_SDVO_HIGH_SPEED;
  6366. /* compute bitmask from p1 value */
  6367. if (IS_PINEVIEW(dev))
  6368. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6369. else {
  6370. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6371. if (IS_G4X(dev) && reduced_clock)
  6372. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6373. }
  6374. switch (clock->p2) {
  6375. case 5:
  6376. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6377. break;
  6378. case 7:
  6379. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6380. break;
  6381. case 10:
  6382. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6383. break;
  6384. case 14:
  6385. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6386. break;
  6387. }
  6388. if (INTEL_INFO(dev)->gen >= 4)
  6389. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6390. if (crtc_state->sdvo_tv_clock)
  6391. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6392. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6393. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6394. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6395. else
  6396. dpll |= PLL_REF_INPUT_DREFCLK;
  6397. dpll |= DPLL_VCO_ENABLE;
  6398. crtc_state->dpll_hw_state.dpll = dpll;
  6399. if (INTEL_INFO(dev)->gen >= 4) {
  6400. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6401. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6402. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6403. }
  6404. }
  6405. static void i8xx_update_pll(struct intel_crtc *crtc,
  6406. struct intel_crtc_state *crtc_state,
  6407. intel_clock_t *reduced_clock,
  6408. int num_connectors)
  6409. {
  6410. struct drm_device *dev = crtc->base.dev;
  6411. struct drm_i915_private *dev_priv = dev->dev_private;
  6412. u32 dpll;
  6413. struct dpll *clock = &crtc_state->dpll;
  6414. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6415. dpll = DPLL_VGA_MODE_DIS;
  6416. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6417. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6418. } else {
  6419. if (clock->p1 == 2)
  6420. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6421. else
  6422. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6423. if (clock->p2 == 4)
  6424. dpll |= PLL_P2_DIVIDE_BY_4;
  6425. }
  6426. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6427. dpll |= DPLL_DVO_2X_MODE;
  6428. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6429. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6430. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6431. else
  6432. dpll |= PLL_REF_INPUT_DREFCLK;
  6433. dpll |= DPLL_VCO_ENABLE;
  6434. crtc_state->dpll_hw_state.dpll = dpll;
  6435. }
  6436. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6437. {
  6438. struct drm_device *dev = intel_crtc->base.dev;
  6439. struct drm_i915_private *dev_priv = dev->dev_private;
  6440. enum pipe pipe = intel_crtc->pipe;
  6441. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6442. struct drm_display_mode *adjusted_mode =
  6443. &intel_crtc->config->base.adjusted_mode;
  6444. uint32_t crtc_vtotal, crtc_vblank_end;
  6445. int vsyncshift = 0;
  6446. /* We need to be careful not to changed the adjusted mode, for otherwise
  6447. * the hw state checker will get angry at the mismatch. */
  6448. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6449. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6450. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6451. /* the chip adds 2 halflines automatically */
  6452. crtc_vtotal -= 1;
  6453. crtc_vblank_end -= 1;
  6454. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6455. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6456. else
  6457. vsyncshift = adjusted_mode->crtc_hsync_start -
  6458. adjusted_mode->crtc_htotal / 2;
  6459. if (vsyncshift < 0)
  6460. vsyncshift += adjusted_mode->crtc_htotal;
  6461. }
  6462. if (INTEL_INFO(dev)->gen > 3)
  6463. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6464. I915_WRITE(HTOTAL(cpu_transcoder),
  6465. (adjusted_mode->crtc_hdisplay - 1) |
  6466. ((adjusted_mode->crtc_htotal - 1) << 16));
  6467. I915_WRITE(HBLANK(cpu_transcoder),
  6468. (adjusted_mode->crtc_hblank_start - 1) |
  6469. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6470. I915_WRITE(HSYNC(cpu_transcoder),
  6471. (adjusted_mode->crtc_hsync_start - 1) |
  6472. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6473. I915_WRITE(VTOTAL(cpu_transcoder),
  6474. (adjusted_mode->crtc_vdisplay - 1) |
  6475. ((crtc_vtotal - 1) << 16));
  6476. I915_WRITE(VBLANK(cpu_transcoder),
  6477. (adjusted_mode->crtc_vblank_start - 1) |
  6478. ((crtc_vblank_end - 1) << 16));
  6479. I915_WRITE(VSYNC(cpu_transcoder),
  6480. (adjusted_mode->crtc_vsync_start - 1) |
  6481. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6482. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6483. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6484. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6485. * bits. */
  6486. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6487. (pipe == PIPE_B || pipe == PIPE_C))
  6488. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6489. /* pipesrc controls the size that is scaled from, which should
  6490. * always be the user's requested size.
  6491. */
  6492. I915_WRITE(PIPESRC(pipe),
  6493. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6494. (intel_crtc->config->pipe_src_h - 1));
  6495. }
  6496. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6497. struct intel_crtc_state *pipe_config)
  6498. {
  6499. struct drm_device *dev = crtc->base.dev;
  6500. struct drm_i915_private *dev_priv = dev->dev_private;
  6501. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6502. uint32_t tmp;
  6503. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6504. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6505. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6506. tmp = I915_READ(HBLANK(cpu_transcoder));
  6507. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6508. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6509. tmp = I915_READ(HSYNC(cpu_transcoder));
  6510. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6511. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6512. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6513. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6514. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6515. tmp = I915_READ(VBLANK(cpu_transcoder));
  6516. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6517. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6518. tmp = I915_READ(VSYNC(cpu_transcoder));
  6519. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6520. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6521. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6522. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6523. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6524. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6525. }
  6526. tmp = I915_READ(PIPESRC(crtc->pipe));
  6527. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6528. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6529. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6530. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6531. }
  6532. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6533. struct intel_crtc_state *pipe_config)
  6534. {
  6535. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6536. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6537. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6538. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6539. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6540. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6541. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6542. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6543. mode->flags = pipe_config->base.adjusted_mode.flags;
  6544. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6545. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6546. }
  6547. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6548. {
  6549. struct drm_device *dev = intel_crtc->base.dev;
  6550. struct drm_i915_private *dev_priv = dev->dev_private;
  6551. uint32_t pipeconf;
  6552. pipeconf = 0;
  6553. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6554. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6555. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6556. if (intel_crtc->config->double_wide)
  6557. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6558. /* only g4x and later have fancy bpc/dither controls */
  6559. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6560. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6561. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6562. pipeconf |= PIPECONF_DITHER_EN |
  6563. PIPECONF_DITHER_TYPE_SP;
  6564. switch (intel_crtc->config->pipe_bpp) {
  6565. case 18:
  6566. pipeconf |= PIPECONF_6BPC;
  6567. break;
  6568. case 24:
  6569. pipeconf |= PIPECONF_8BPC;
  6570. break;
  6571. case 30:
  6572. pipeconf |= PIPECONF_10BPC;
  6573. break;
  6574. default:
  6575. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6576. BUG();
  6577. }
  6578. }
  6579. if (HAS_PIPE_CXSR(dev)) {
  6580. if (intel_crtc->lowfreq_avail) {
  6581. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6582. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6583. } else {
  6584. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6585. }
  6586. }
  6587. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6588. if (INTEL_INFO(dev)->gen < 4 ||
  6589. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6590. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6591. else
  6592. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6593. } else
  6594. pipeconf |= PIPECONF_PROGRESSIVE;
  6595. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6596. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6597. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6598. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6599. }
  6600. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6601. struct intel_crtc_state *crtc_state)
  6602. {
  6603. struct drm_device *dev = crtc->base.dev;
  6604. struct drm_i915_private *dev_priv = dev->dev_private;
  6605. int refclk, num_connectors = 0;
  6606. intel_clock_t clock, reduced_clock;
  6607. bool ok, has_reduced_clock = false;
  6608. bool is_lvds = false, is_dsi = false;
  6609. struct intel_encoder *encoder;
  6610. const intel_limit_t *limit;
  6611. struct drm_atomic_state *state = crtc_state->base.state;
  6612. struct drm_connector *connector;
  6613. struct drm_connector_state *connector_state;
  6614. int i;
  6615. memset(&crtc_state->dpll_hw_state, 0,
  6616. sizeof(crtc_state->dpll_hw_state));
  6617. for_each_connector_in_state(state, connector, connector_state, i) {
  6618. if (connector_state->crtc != &crtc->base)
  6619. continue;
  6620. encoder = to_intel_encoder(connector_state->best_encoder);
  6621. switch (encoder->type) {
  6622. case INTEL_OUTPUT_LVDS:
  6623. is_lvds = true;
  6624. break;
  6625. case INTEL_OUTPUT_DSI:
  6626. is_dsi = true;
  6627. break;
  6628. default:
  6629. break;
  6630. }
  6631. num_connectors++;
  6632. }
  6633. if (is_dsi)
  6634. return 0;
  6635. if (!crtc_state->clock_set) {
  6636. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6637. /*
  6638. * Returns a set of divisors for the desired target clock with
  6639. * the given refclk, or FALSE. The returned values represent
  6640. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6641. * 2) / p1 / p2.
  6642. */
  6643. limit = intel_limit(crtc_state, refclk);
  6644. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6645. crtc_state->port_clock,
  6646. refclk, NULL, &clock);
  6647. if (!ok) {
  6648. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6649. return -EINVAL;
  6650. }
  6651. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6652. /*
  6653. * Ensure we match the reduced clock's P to the target
  6654. * clock. If the clocks don't match, we can't switch
  6655. * the display clock by using the FP0/FP1. In such case
  6656. * we will disable the LVDS downclock feature.
  6657. */
  6658. has_reduced_clock =
  6659. dev_priv->display.find_dpll(limit, crtc_state,
  6660. dev_priv->lvds_downclock,
  6661. refclk, &clock,
  6662. &reduced_clock);
  6663. }
  6664. /* Compat-code for transition, will disappear. */
  6665. crtc_state->dpll.n = clock.n;
  6666. crtc_state->dpll.m1 = clock.m1;
  6667. crtc_state->dpll.m2 = clock.m2;
  6668. crtc_state->dpll.p1 = clock.p1;
  6669. crtc_state->dpll.p2 = clock.p2;
  6670. }
  6671. if (IS_GEN2(dev)) {
  6672. i8xx_update_pll(crtc, crtc_state,
  6673. has_reduced_clock ? &reduced_clock : NULL,
  6674. num_connectors);
  6675. } else if (IS_CHERRYVIEW(dev)) {
  6676. chv_update_pll(crtc, crtc_state);
  6677. } else if (IS_VALLEYVIEW(dev)) {
  6678. vlv_update_pll(crtc, crtc_state);
  6679. } else {
  6680. i9xx_update_pll(crtc, crtc_state,
  6681. has_reduced_clock ? &reduced_clock : NULL,
  6682. num_connectors);
  6683. }
  6684. return 0;
  6685. }
  6686. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6687. struct intel_crtc_state *pipe_config)
  6688. {
  6689. struct drm_device *dev = crtc->base.dev;
  6690. struct drm_i915_private *dev_priv = dev->dev_private;
  6691. uint32_t tmp;
  6692. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6693. return;
  6694. tmp = I915_READ(PFIT_CONTROL);
  6695. if (!(tmp & PFIT_ENABLE))
  6696. return;
  6697. /* Check whether the pfit is attached to our pipe. */
  6698. if (INTEL_INFO(dev)->gen < 4) {
  6699. if (crtc->pipe != PIPE_B)
  6700. return;
  6701. } else {
  6702. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6703. return;
  6704. }
  6705. pipe_config->gmch_pfit.control = tmp;
  6706. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6707. if (INTEL_INFO(dev)->gen < 5)
  6708. pipe_config->gmch_pfit.lvds_border_bits =
  6709. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6710. }
  6711. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6712. struct intel_crtc_state *pipe_config)
  6713. {
  6714. struct drm_device *dev = crtc->base.dev;
  6715. struct drm_i915_private *dev_priv = dev->dev_private;
  6716. int pipe = pipe_config->cpu_transcoder;
  6717. intel_clock_t clock;
  6718. u32 mdiv;
  6719. int refclk = 100000;
  6720. /* In case of MIPI DPLL will not even be used */
  6721. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6722. return;
  6723. mutex_lock(&dev_priv->sb_lock);
  6724. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6725. mutex_unlock(&dev_priv->sb_lock);
  6726. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6727. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6728. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6729. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6730. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6731. vlv_clock(refclk, &clock);
  6732. /* clock.dot is the fast clock */
  6733. pipe_config->port_clock = clock.dot / 5;
  6734. }
  6735. static void
  6736. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6737. struct intel_initial_plane_config *plane_config)
  6738. {
  6739. struct drm_device *dev = crtc->base.dev;
  6740. struct drm_i915_private *dev_priv = dev->dev_private;
  6741. u32 val, base, offset;
  6742. int pipe = crtc->pipe, plane = crtc->plane;
  6743. int fourcc, pixel_format;
  6744. unsigned int aligned_height;
  6745. struct drm_framebuffer *fb;
  6746. struct intel_framebuffer *intel_fb;
  6747. val = I915_READ(DSPCNTR(plane));
  6748. if (!(val & DISPLAY_PLANE_ENABLE))
  6749. return;
  6750. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6751. if (!intel_fb) {
  6752. DRM_DEBUG_KMS("failed to alloc fb\n");
  6753. return;
  6754. }
  6755. fb = &intel_fb->base;
  6756. if (INTEL_INFO(dev)->gen >= 4) {
  6757. if (val & DISPPLANE_TILED) {
  6758. plane_config->tiling = I915_TILING_X;
  6759. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6760. }
  6761. }
  6762. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6763. fourcc = i9xx_format_to_fourcc(pixel_format);
  6764. fb->pixel_format = fourcc;
  6765. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6766. if (INTEL_INFO(dev)->gen >= 4) {
  6767. if (plane_config->tiling)
  6768. offset = I915_READ(DSPTILEOFF(plane));
  6769. else
  6770. offset = I915_READ(DSPLINOFF(plane));
  6771. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6772. } else {
  6773. base = I915_READ(DSPADDR(plane));
  6774. }
  6775. plane_config->base = base;
  6776. val = I915_READ(PIPESRC(pipe));
  6777. fb->width = ((val >> 16) & 0xfff) + 1;
  6778. fb->height = ((val >> 0) & 0xfff) + 1;
  6779. val = I915_READ(DSPSTRIDE(pipe));
  6780. fb->pitches[0] = val & 0xffffffc0;
  6781. aligned_height = intel_fb_align_height(dev, fb->height,
  6782. fb->pixel_format,
  6783. fb->modifier[0]);
  6784. plane_config->size = fb->pitches[0] * aligned_height;
  6785. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6786. pipe_name(pipe), plane, fb->width, fb->height,
  6787. fb->bits_per_pixel, base, fb->pitches[0],
  6788. plane_config->size);
  6789. plane_config->fb = intel_fb;
  6790. }
  6791. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6792. struct intel_crtc_state *pipe_config)
  6793. {
  6794. struct drm_device *dev = crtc->base.dev;
  6795. struct drm_i915_private *dev_priv = dev->dev_private;
  6796. int pipe = pipe_config->cpu_transcoder;
  6797. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6798. intel_clock_t clock;
  6799. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6800. int refclk = 100000;
  6801. mutex_lock(&dev_priv->sb_lock);
  6802. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6803. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6804. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6805. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6806. mutex_unlock(&dev_priv->sb_lock);
  6807. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6808. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6809. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6810. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6811. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6812. chv_clock(refclk, &clock);
  6813. /* clock.dot is the fast clock */
  6814. pipe_config->port_clock = clock.dot / 5;
  6815. }
  6816. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6817. struct intel_crtc_state *pipe_config)
  6818. {
  6819. struct drm_device *dev = crtc->base.dev;
  6820. struct drm_i915_private *dev_priv = dev->dev_private;
  6821. uint32_t tmp;
  6822. if (!intel_display_power_is_enabled(dev_priv,
  6823. POWER_DOMAIN_PIPE(crtc->pipe)))
  6824. return false;
  6825. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6826. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6827. tmp = I915_READ(PIPECONF(crtc->pipe));
  6828. if (!(tmp & PIPECONF_ENABLE))
  6829. return false;
  6830. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6831. switch (tmp & PIPECONF_BPC_MASK) {
  6832. case PIPECONF_6BPC:
  6833. pipe_config->pipe_bpp = 18;
  6834. break;
  6835. case PIPECONF_8BPC:
  6836. pipe_config->pipe_bpp = 24;
  6837. break;
  6838. case PIPECONF_10BPC:
  6839. pipe_config->pipe_bpp = 30;
  6840. break;
  6841. default:
  6842. break;
  6843. }
  6844. }
  6845. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6846. pipe_config->limited_color_range = true;
  6847. if (INTEL_INFO(dev)->gen < 4)
  6848. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6849. intel_get_pipe_timings(crtc, pipe_config);
  6850. i9xx_get_pfit_config(crtc, pipe_config);
  6851. if (INTEL_INFO(dev)->gen >= 4) {
  6852. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6853. pipe_config->pixel_multiplier =
  6854. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6855. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6856. pipe_config->dpll_hw_state.dpll_md = tmp;
  6857. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6858. tmp = I915_READ(DPLL(crtc->pipe));
  6859. pipe_config->pixel_multiplier =
  6860. ((tmp & SDVO_MULTIPLIER_MASK)
  6861. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6862. } else {
  6863. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6864. * port and will be fixed up in the encoder->get_config
  6865. * function. */
  6866. pipe_config->pixel_multiplier = 1;
  6867. }
  6868. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6869. if (!IS_VALLEYVIEW(dev)) {
  6870. /*
  6871. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6872. * on 830. Filter it out here so that we don't
  6873. * report errors due to that.
  6874. */
  6875. if (IS_I830(dev))
  6876. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6877. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6878. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6879. } else {
  6880. /* Mask out read-only status bits. */
  6881. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6882. DPLL_PORTC_READY_MASK |
  6883. DPLL_PORTB_READY_MASK);
  6884. }
  6885. if (IS_CHERRYVIEW(dev))
  6886. chv_crtc_clock_get(crtc, pipe_config);
  6887. else if (IS_VALLEYVIEW(dev))
  6888. vlv_crtc_clock_get(crtc, pipe_config);
  6889. else
  6890. i9xx_crtc_clock_get(crtc, pipe_config);
  6891. return true;
  6892. }
  6893. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6894. {
  6895. struct drm_i915_private *dev_priv = dev->dev_private;
  6896. struct intel_encoder *encoder;
  6897. u32 val, final;
  6898. bool has_lvds = false;
  6899. bool has_cpu_edp = false;
  6900. bool has_panel = false;
  6901. bool has_ck505 = false;
  6902. bool can_ssc = false;
  6903. /* We need to take the global config into account */
  6904. for_each_intel_encoder(dev, encoder) {
  6905. switch (encoder->type) {
  6906. case INTEL_OUTPUT_LVDS:
  6907. has_panel = true;
  6908. has_lvds = true;
  6909. break;
  6910. case INTEL_OUTPUT_EDP:
  6911. has_panel = true;
  6912. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6913. has_cpu_edp = true;
  6914. break;
  6915. default:
  6916. break;
  6917. }
  6918. }
  6919. if (HAS_PCH_IBX(dev)) {
  6920. has_ck505 = dev_priv->vbt.display_clock_mode;
  6921. can_ssc = has_ck505;
  6922. } else {
  6923. has_ck505 = false;
  6924. can_ssc = true;
  6925. }
  6926. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6927. has_panel, has_lvds, has_ck505);
  6928. /* Ironlake: try to setup display ref clock before DPLL
  6929. * enabling. This is only under driver's control after
  6930. * PCH B stepping, previous chipset stepping should be
  6931. * ignoring this setting.
  6932. */
  6933. val = I915_READ(PCH_DREF_CONTROL);
  6934. /* As we must carefully and slowly disable/enable each source in turn,
  6935. * compute the final state we want first and check if we need to
  6936. * make any changes at all.
  6937. */
  6938. final = val;
  6939. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6940. if (has_ck505)
  6941. final |= DREF_NONSPREAD_CK505_ENABLE;
  6942. else
  6943. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6944. final &= ~DREF_SSC_SOURCE_MASK;
  6945. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6946. final &= ~DREF_SSC1_ENABLE;
  6947. if (has_panel) {
  6948. final |= DREF_SSC_SOURCE_ENABLE;
  6949. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6950. final |= DREF_SSC1_ENABLE;
  6951. if (has_cpu_edp) {
  6952. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6953. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6954. else
  6955. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6956. } else
  6957. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6958. } else {
  6959. final |= DREF_SSC_SOURCE_DISABLE;
  6960. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6961. }
  6962. if (final == val)
  6963. return;
  6964. /* Always enable nonspread source */
  6965. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6966. if (has_ck505)
  6967. val |= DREF_NONSPREAD_CK505_ENABLE;
  6968. else
  6969. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6970. if (has_panel) {
  6971. val &= ~DREF_SSC_SOURCE_MASK;
  6972. val |= DREF_SSC_SOURCE_ENABLE;
  6973. /* SSC must be turned on before enabling the CPU output */
  6974. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6975. DRM_DEBUG_KMS("Using SSC on panel\n");
  6976. val |= DREF_SSC1_ENABLE;
  6977. } else
  6978. val &= ~DREF_SSC1_ENABLE;
  6979. /* Get SSC going before enabling the outputs */
  6980. I915_WRITE(PCH_DREF_CONTROL, val);
  6981. POSTING_READ(PCH_DREF_CONTROL);
  6982. udelay(200);
  6983. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6984. /* Enable CPU source on CPU attached eDP */
  6985. if (has_cpu_edp) {
  6986. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6987. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6988. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6989. } else
  6990. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6991. } else
  6992. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6993. I915_WRITE(PCH_DREF_CONTROL, val);
  6994. POSTING_READ(PCH_DREF_CONTROL);
  6995. udelay(200);
  6996. } else {
  6997. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6998. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6999. /* Turn off CPU output */
  7000. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7001. I915_WRITE(PCH_DREF_CONTROL, val);
  7002. POSTING_READ(PCH_DREF_CONTROL);
  7003. udelay(200);
  7004. /* Turn off the SSC source */
  7005. val &= ~DREF_SSC_SOURCE_MASK;
  7006. val |= DREF_SSC_SOURCE_DISABLE;
  7007. /* Turn off SSC1 */
  7008. val &= ~DREF_SSC1_ENABLE;
  7009. I915_WRITE(PCH_DREF_CONTROL, val);
  7010. POSTING_READ(PCH_DREF_CONTROL);
  7011. udelay(200);
  7012. }
  7013. BUG_ON(val != final);
  7014. }
  7015. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7016. {
  7017. uint32_t tmp;
  7018. tmp = I915_READ(SOUTH_CHICKEN2);
  7019. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7020. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7021. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7022. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7023. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7024. tmp = I915_READ(SOUTH_CHICKEN2);
  7025. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7026. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7027. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7028. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7029. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7030. }
  7031. /* WaMPhyProgramming:hsw */
  7032. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7033. {
  7034. uint32_t tmp;
  7035. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7036. tmp &= ~(0xFF << 24);
  7037. tmp |= (0x12 << 24);
  7038. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7039. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7040. tmp |= (1 << 11);
  7041. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7042. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7043. tmp |= (1 << 11);
  7044. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7045. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7046. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7047. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7048. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7049. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7050. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7051. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7052. tmp &= ~(7 << 13);
  7053. tmp |= (5 << 13);
  7054. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7055. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7056. tmp &= ~(7 << 13);
  7057. tmp |= (5 << 13);
  7058. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7059. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7060. tmp &= ~0xFF;
  7061. tmp |= 0x1C;
  7062. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7063. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7064. tmp &= ~0xFF;
  7065. tmp |= 0x1C;
  7066. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7067. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7068. tmp &= ~(0xFF << 16);
  7069. tmp |= (0x1C << 16);
  7070. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7071. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7072. tmp &= ~(0xFF << 16);
  7073. tmp |= (0x1C << 16);
  7074. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7075. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7076. tmp |= (1 << 27);
  7077. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7078. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7079. tmp |= (1 << 27);
  7080. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7081. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7082. tmp &= ~(0xF << 28);
  7083. tmp |= (4 << 28);
  7084. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7085. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7086. tmp &= ~(0xF << 28);
  7087. tmp |= (4 << 28);
  7088. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7089. }
  7090. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7091. * Programming" based on the parameters passed:
  7092. * - Sequence to enable CLKOUT_DP
  7093. * - Sequence to enable CLKOUT_DP without spread
  7094. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7095. */
  7096. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7097. bool with_fdi)
  7098. {
  7099. struct drm_i915_private *dev_priv = dev->dev_private;
  7100. uint32_t reg, tmp;
  7101. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7102. with_spread = true;
  7103. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7104. with_fdi, "LP PCH doesn't have FDI\n"))
  7105. with_fdi = false;
  7106. mutex_lock(&dev_priv->sb_lock);
  7107. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7108. tmp &= ~SBI_SSCCTL_DISABLE;
  7109. tmp |= SBI_SSCCTL_PATHALT;
  7110. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7111. udelay(24);
  7112. if (with_spread) {
  7113. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7114. tmp &= ~SBI_SSCCTL_PATHALT;
  7115. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7116. if (with_fdi) {
  7117. lpt_reset_fdi_mphy(dev_priv);
  7118. lpt_program_fdi_mphy(dev_priv);
  7119. }
  7120. }
  7121. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7122. SBI_GEN0 : SBI_DBUFF0;
  7123. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7124. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7125. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7126. mutex_unlock(&dev_priv->sb_lock);
  7127. }
  7128. /* Sequence to disable CLKOUT_DP */
  7129. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7130. {
  7131. struct drm_i915_private *dev_priv = dev->dev_private;
  7132. uint32_t reg, tmp;
  7133. mutex_lock(&dev_priv->sb_lock);
  7134. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7135. SBI_GEN0 : SBI_DBUFF0;
  7136. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7137. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7138. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7139. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7140. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7141. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7142. tmp |= SBI_SSCCTL_PATHALT;
  7143. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7144. udelay(32);
  7145. }
  7146. tmp |= SBI_SSCCTL_DISABLE;
  7147. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7148. }
  7149. mutex_unlock(&dev_priv->sb_lock);
  7150. }
  7151. static void lpt_init_pch_refclk(struct drm_device *dev)
  7152. {
  7153. struct intel_encoder *encoder;
  7154. bool has_vga = false;
  7155. for_each_intel_encoder(dev, encoder) {
  7156. switch (encoder->type) {
  7157. case INTEL_OUTPUT_ANALOG:
  7158. has_vga = true;
  7159. break;
  7160. default:
  7161. break;
  7162. }
  7163. }
  7164. if (has_vga)
  7165. lpt_enable_clkout_dp(dev, true, true);
  7166. else
  7167. lpt_disable_clkout_dp(dev);
  7168. }
  7169. /*
  7170. * Initialize reference clocks when the driver loads
  7171. */
  7172. void intel_init_pch_refclk(struct drm_device *dev)
  7173. {
  7174. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7175. ironlake_init_pch_refclk(dev);
  7176. else if (HAS_PCH_LPT(dev))
  7177. lpt_init_pch_refclk(dev);
  7178. }
  7179. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7180. {
  7181. struct drm_device *dev = crtc_state->base.crtc->dev;
  7182. struct drm_i915_private *dev_priv = dev->dev_private;
  7183. struct drm_atomic_state *state = crtc_state->base.state;
  7184. struct drm_connector *connector;
  7185. struct drm_connector_state *connector_state;
  7186. struct intel_encoder *encoder;
  7187. int num_connectors = 0, i;
  7188. bool is_lvds = false;
  7189. for_each_connector_in_state(state, connector, connector_state, i) {
  7190. if (connector_state->crtc != crtc_state->base.crtc)
  7191. continue;
  7192. encoder = to_intel_encoder(connector_state->best_encoder);
  7193. switch (encoder->type) {
  7194. case INTEL_OUTPUT_LVDS:
  7195. is_lvds = true;
  7196. break;
  7197. default:
  7198. break;
  7199. }
  7200. num_connectors++;
  7201. }
  7202. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7203. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7204. dev_priv->vbt.lvds_ssc_freq);
  7205. return dev_priv->vbt.lvds_ssc_freq;
  7206. }
  7207. return 120000;
  7208. }
  7209. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7210. {
  7211. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7213. int pipe = intel_crtc->pipe;
  7214. uint32_t val;
  7215. val = 0;
  7216. switch (intel_crtc->config->pipe_bpp) {
  7217. case 18:
  7218. val |= PIPECONF_6BPC;
  7219. break;
  7220. case 24:
  7221. val |= PIPECONF_8BPC;
  7222. break;
  7223. case 30:
  7224. val |= PIPECONF_10BPC;
  7225. break;
  7226. case 36:
  7227. val |= PIPECONF_12BPC;
  7228. break;
  7229. default:
  7230. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7231. BUG();
  7232. }
  7233. if (intel_crtc->config->dither)
  7234. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7235. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7236. val |= PIPECONF_INTERLACED_ILK;
  7237. else
  7238. val |= PIPECONF_PROGRESSIVE;
  7239. if (intel_crtc->config->limited_color_range)
  7240. val |= PIPECONF_COLOR_RANGE_SELECT;
  7241. I915_WRITE(PIPECONF(pipe), val);
  7242. POSTING_READ(PIPECONF(pipe));
  7243. }
  7244. /*
  7245. * Set up the pipe CSC unit.
  7246. *
  7247. * Currently only full range RGB to limited range RGB conversion
  7248. * is supported, but eventually this should handle various
  7249. * RGB<->YCbCr scenarios as well.
  7250. */
  7251. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7252. {
  7253. struct drm_device *dev = crtc->dev;
  7254. struct drm_i915_private *dev_priv = dev->dev_private;
  7255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7256. int pipe = intel_crtc->pipe;
  7257. uint16_t coeff = 0x7800; /* 1.0 */
  7258. /*
  7259. * TODO: Check what kind of values actually come out of the pipe
  7260. * with these coeff/postoff values and adjust to get the best
  7261. * accuracy. Perhaps we even need to take the bpc value into
  7262. * consideration.
  7263. */
  7264. if (intel_crtc->config->limited_color_range)
  7265. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7266. /*
  7267. * GY/GU and RY/RU should be the other way around according
  7268. * to BSpec, but reality doesn't agree. Just set them up in
  7269. * a way that results in the correct picture.
  7270. */
  7271. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7272. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7273. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7274. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7275. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7276. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7277. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7278. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7279. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7280. if (INTEL_INFO(dev)->gen > 6) {
  7281. uint16_t postoff = 0;
  7282. if (intel_crtc->config->limited_color_range)
  7283. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7284. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7285. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7286. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7287. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7288. } else {
  7289. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7290. if (intel_crtc->config->limited_color_range)
  7291. mode |= CSC_BLACK_SCREEN_OFFSET;
  7292. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7293. }
  7294. }
  7295. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7296. {
  7297. struct drm_device *dev = crtc->dev;
  7298. struct drm_i915_private *dev_priv = dev->dev_private;
  7299. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7300. enum pipe pipe = intel_crtc->pipe;
  7301. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7302. uint32_t val;
  7303. val = 0;
  7304. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7305. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7306. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7307. val |= PIPECONF_INTERLACED_ILK;
  7308. else
  7309. val |= PIPECONF_PROGRESSIVE;
  7310. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7311. POSTING_READ(PIPECONF(cpu_transcoder));
  7312. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7313. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7314. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7315. val = 0;
  7316. switch (intel_crtc->config->pipe_bpp) {
  7317. case 18:
  7318. val |= PIPEMISC_DITHER_6_BPC;
  7319. break;
  7320. case 24:
  7321. val |= PIPEMISC_DITHER_8_BPC;
  7322. break;
  7323. case 30:
  7324. val |= PIPEMISC_DITHER_10_BPC;
  7325. break;
  7326. case 36:
  7327. val |= PIPEMISC_DITHER_12_BPC;
  7328. break;
  7329. default:
  7330. /* Case prevented by pipe_config_set_bpp. */
  7331. BUG();
  7332. }
  7333. if (intel_crtc->config->dither)
  7334. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7335. I915_WRITE(PIPEMISC(pipe), val);
  7336. }
  7337. }
  7338. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7339. struct intel_crtc_state *crtc_state,
  7340. intel_clock_t *clock,
  7341. bool *has_reduced_clock,
  7342. intel_clock_t *reduced_clock)
  7343. {
  7344. struct drm_device *dev = crtc->dev;
  7345. struct drm_i915_private *dev_priv = dev->dev_private;
  7346. int refclk;
  7347. const intel_limit_t *limit;
  7348. bool ret, is_lvds = false;
  7349. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7350. refclk = ironlake_get_refclk(crtc_state);
  7351. /*
  7352. * Returns a set of divisors for the desired target clock with the given
  7353. * refclk, or FALSE. The returned values represent the clock equation:
  7354. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7355. */
  7356. limit = intel_limit(crtc_state, refclk);
  7357. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7358. crtc_state->port_clock,
  7359. refclk, NULL, clock);
  7360. if (!ret)
  7361. return false;
  7362. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7363. /*
  7364. * Ensure we match the reduced clock's P to the target clock.
  7365. * If the clocks don't match, we can't switch the display clock
  7366. * by using the FP0/FP1. In such case we will disable the LVDS
  7367. * downclock feature.
  7368. */
  7369. *has_reduced_clock =
  7370. dev_priv->display.find_dpll(limit, crtc_state,
  7371. dev_priv->lvds_downclock,
  7372. refclk, clock,
  7373. reduced_clock);
  7374. }
  7375. return true;
  7376. }
  7377. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7378. {
  7379. /*
  7380. * Account for spread spectrum to avoid
  7381. * oversubscribing the link. Max center spread
  7382. * is 2.5%; use 5% for safety's sake.
  7383. */
  7384. u32 bps = target_clock * bpp * 21 / 20;
  7385. return DIV_ROUND_UP(bps, link_bw * 8);
  7386. }
  7387. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7388. {
  7389. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7390. }
  7391. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7392. struct intel_crtc_state *crtc_state,
  7393. u32 *fp,
  7394. intel_clock_t *reduced_clock, u32 *fp2)
  7395. {
  7396. struct drm_crtc *crtc = &intel_crtc->base;
  7397. struct drm_device *dev = crtc->dev;
  7398. struct drm_i915_private *dev_priv = dev->dev_private;
  7399. struct drm_atomic_state *state = crtc_state->base.state;
  7400. struct drm_connector *connector;
  7401. struct drm_connector_state *connector_state;
  7402. struct intel_encoder *encoder;
  7403. uint32_t dpll;
  7404. int factor, num_connectors = 0, i;
  7405. bool is_lvds = false, is_sdvo = false;
  7406. for_each_connector_in_state(state, connector, connector_state, i) {
  7407. if (connector_state->crtc != crtc_state->base.crtc)
  7408. continue;
  7409. encoder = to_intel_encoder(connector_state->best_encoder);
  7410. switch (encoder->type) {
  7411. case INTEL_OUTPUT_LVDS:
  7412. is_lvds = true;
  7413. break;
  7414. case INTEL_OUTPUT_SDVO:
  7415. case INTEL_OUTPUT_HDMI:
  7416. is_sdvo = true;
  7417. break;
  7418. default:
  7419. break;
  7420. }
  7421. num_connectors++;
  7422. }
  7423. /* Enable autotuning of the PLL clock (if permissible) */
  7424. factor = 21;
  7425. if (is_lvds) {
  7426. if ((intel_panel_use_ssc(dev_priv) &&
  7427. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7428. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7429. factor = 25;
  7430. } else if (crtc_state->sdvo_tv_clock)
  7431. factor = 20;
  7432. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7433. *fp |= FP_CB_TUNE;
  7434. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7435. *fp2 |= FP_CB_TUNE;
  7436. dpll = 0;
  7437. if (is_lvds)
  7438. dpll |= DPLLB_MODE_LVDS;
  7439. else
  7440. dpll |= DPLLB_MODE_DAC_SERIAL;
  7441. dpll |= (crtc_state->pixel_multiplier - 1)
  7442. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7443. if (is_sdvo)
  7444. dpll |= DPLL_SDVO_HIGH_SPEED;
  7445. if (crtc_state->has_dp_encoder)
  7446. dpll |= DPLL_SDVO_HIGH_SPEED;
  7447. /* compute bitmask from p1 value */
  7448. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7449. /* also FPA1 */
  7450. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7451. switch (crtc_state->dpll.p2) {
  7452. case 5:
  7453. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7454. break;
  7455. case 7:
  7456. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7457. break;
  7458. case 10:
  7459. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7460. break;
  7461. case 14:
  7462. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7463. break;
  7464. }
  7465. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7466. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7467. else
  7468. dpll |= PLL_REF_INPUT_DREFCLK;
  7469. return dpll | DPLL_VCO_ENABLE;
  7470. }
  7471. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7472. struct intel_crtc_state *crtc_state)
  7473. {
  7474. struct drm_device *dev = crtc->base.dev;
  7475. intel_clock_t clock, reduced_clock;
  7476. u32 dpll = 0, fp = 0, fp2 = 0;
  7477. bool ok, has_reduced_clock = false;
  7478. bool is_lvds = false;
  7479. struct intel_shared_dpll *pll;
  7480. memset(&crtc_state->dpll_hw_state, 0,
  7481. sizeof(crtc_state->dpll_hw_state));
  7482. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7483. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7484. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7485. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7486. &has_reduced_clock, &reduced_clock);
  7487. if (!ok && !crtc_state->clock_set) {
  7488. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7489. return -EINVAL;
  7490. }
  7491. /* Compat-code for transition, will disappear. */
  7492. if (!crtc_state->clock_set) {
  7493. crtc_state->dpll.n = clock.n;
  7494. crtc_state->dpll.m1 = clock.m1;
  7495. crtc_state->dpll.m2 = clock.m2;
  7496. crtc_state->dpll.p1 = clock.p1;
  7497. crtc_state->dpll.p2 = clock.p2;
  7498. }
  7499. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7500. if (crtc_state->has_pch_encoder) {
  7501. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7502. if (has_reduced_clock)
  7503. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7504. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7505. &fp, &reduced_clock,
  7506. has_reduced_clock ? &fp2 : NULL);
  7507. crtc_state->dpll_hw_state.dpll = dpll;
  7508. crtc_state->dpll_hw_state.fp0 = fp;
  7509. if (has_reduced_clock)
  7510. crtc_state->dpll_hw_state.fp1 = fp2;
  7511. else
  7512. crtc_state->dpll_hw_state.fp1 = fp;
  7513. pll = intel_get_shared_dpll(crtc, crtc_state);
  7514. if (pll == NULL) {
  7515. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7516. pipe_name(crtc->pipe));
  7517. return -EINVAL;
  7518. }
  7519. }
  7520. if (is_lvds && has_reduced_clock)
  7521. crtc->lowfreq_avail = true;
  7522. else
  7523. crtc->lowfreq_avail = false;
  7524. return 0;
  7525. }
  7526. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7527. struct intel_link_m_n *m_n)
  7528. {
  7529. struct drm_device *dev = crtc->base.dev;
  7530. struct drm_i915_private *dev_priv = dev->dev_private;
  7531. enum pipe pipe = crtc->pipe;
  7532. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7533. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7534. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7535. & ~TU_SIZE_MASK;
  7536. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7537. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7538. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7539. }
  7540. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7541. enum transcoder transcoder,
  7542. struct intel_link_m_n *m_n,
  7543. struct intel_link_m_n *m2_n2)
  7544. {
  7545. struct drm_device *dev = crtc->base.dev;
  7546. struct drm_i915_private *dev_priv = dev->dev_private;
  7547. enum pipe pipe = crtc->pipe;
  7548. if (INTEL_INFO(dev)->gen >= 5) {
  7549. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7550. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7551. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7552. & ~TU_SIZE_MASK;
  7553. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7554. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7555. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7556. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7557. * gen < 8) and if DRRS is supported (to make sure the
  7558. * registers are not unnecessarily read).
  7559. */
  7560. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7561. crtc->config->has_drrs) {
  7562. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7563. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7564. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7565. & ~TU_SIZE_MASK;
  7566. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7567. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7568. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7569. }
  7570. } else {
  7571. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7572. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7573. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7574. & ~TU_SIZE_MASK;
  7575. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7576. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7577. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7578. }
  7579. }
  7580. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7581. struct intel_crtc_state *pipe_config)
  7582. {
  7583. if (pipe_config->has_pch_encoder)
  7584. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7585. else
  7586. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7587. &pipe_config->dp_m_n,
  7588. &pipe_config->dp_m2_n2);
  7589. }
  7590. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7591. struct intel_crtc_state *pipe_config)
  7592. {
  7593. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7594. &pipe_config->fdi_m_n, NULL);
  7595. }
  7596. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7597. struct intel_crtc_state *pipe_config)
  7598. {
  7599. struct drm_device *dev = crtc->base.dev;
  7600. struct drm_i915_private *dev_priv = dev->dev_private;
  7601. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7602. uint32_t ps_ctrl = 0;
  7603. int id = -1;
  7604. int i;
  7605. /* find scaler attached to this pipe */
  7606. for (i = 0; i < crtc->num_scalers; i++) {
  7607. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7608. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7609. id = i;
  7610. pipe_config->pch_pfit.enabled = true;
  7611. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7612. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7613. break;
  7614. }
  7615. }
  7616. scaler_state->scaler_id = id;
  7617. if (id >= 0) {
  7618. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7619. } else {
  7620. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7621. }
  7622. }
  7623. static void
  7624. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7625. struct intel_initial_plane_config *plane_config)
  7626. {
  7627. struct drm_device *dev = crtc->base.dev;
  7628. struct drm_i915_private *dev_priv = dev->dev_private;
  7629. u32 val, base, offset, stride_mult, tiling;
  7630. int pipe = crtc->pipe;
  7631. int fourcc, pixel_format;
  7632. unsigned int aligned_height;
  7633. struct drm_framebuffer *fb;
  7634. struct intel_framebuffer *intel_fb;
  7635. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7636. if (!intel_fb) {
  7637. DRM_DEBUG_KMS("failed to alloc fb\n");
  7638. return;
  7639. }
  7640. fb = &intel_fb->base;
  7641. val = I915_READ(PLANE_CTL(pipe, 0));
  7642. if (!(val & PLANE_CTL_ENABLE))
  7643. goto error;
  7644. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7645. fourcc = skl_format_to_fourcc(pixel_format,
  7646. val & PLANE_CTL_ORDER_RGBX,
  7647. val & PLANE_CTL_ALPHA_MASK);
  7648. fb->pixel_format = fourcc;
  7649. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7650. tiling = val & PLANE_CTL_TILED_MASK;
  7651. switch (tiling) {
  7652. case PLANE_CTL_TILED_LINEAR:
  7653. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7654. break;
  7655. case PLANE_CTL_TILED_X:
  7656. plane_config->tiling = I915_TILING_X;
  7657. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7658. break;
  7659. case PLANE_CTL_TILED_Y:
  7660. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7661. break;
  7662. case PLANE_CTL_TILED_YF:
  7663. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7664. break;
  7665. default:
  7666. MISSING_CASE(tiling);
  7667. goto error;
  7668. }
  7669. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7670. plane_config->base = base;
  7671. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7672. val = I915_READ(PLANE_SIZE(pipe, 0));
  7673. fb->height = ((val >> 16) & 0xfff) + 1;
  7674. fb->width = ((val >> 0) & 0x1fff) + 1;
  7675. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7676. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7677. fb->pixel_format);
  7678. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7679. aligned_height = intel_fb_align_height(dev, fb->height,
  7680. fb->pixel_format,
  7681. fb->modifier[0]);
  7682. plane_config->size = fb->pitches[0] * aligned_height;
  7683. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7684. pipe_name(pipe), fb->width, fb->height,
  7685. fb->bits_per_pixel, base, fb->pitches[0],
  7686. plane_config->size);
  7687. plane_config->fb = intel_fb;
  7688. return;
  7689. error:
  7690. kfree(fb);
  7691. }
  7692. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7693. struct intel_crtc_state *pipe_config)
  7694. {
  7695. struct drm_device *dev = crtc->base.dev;
  7696. struct drm_i915_private *dev_priv = dev->dev_private;
  7697. uint32_t tmp;
  7698. tmp = I915_READ(PF_CTL(crtc->pipe));
  7699. if (tmp & PF_ENABLE) {
  7700. pipe_config->pch_pfit.enabled = true;
  7701. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7702. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7703. /* We currently do not free assignements of panel fitters on
  7704. * ivb/hsw (since we don't use the higher upscaling modes which
  7705. * differentiates them) so just WARN about this case for now. */
  7706. if (IS_GEN7(dev)) {
  7707. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7708. PF_PIPE_SEL_IVB(crtc->pipe));
  7709. }
  7710. }
  7711. }
  7712. static void
  7713. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7714. struct intel_initial_plane_config *plane_config)
  7715. {
  7716. struct drm_device *dev = crtc->base.dev;
  7717. struct drm_i915_private *dev_priv = dev->dev_private;
  7718. u32 val, base, offset;
  7719. int pipe = crtc->pipe;
  7720. int fourcc, pixel_format;
  7721. unsigned int aligned_height;
  7722. struct drm_framebuffer *fb;
  7723. struct intel_framebuffer *intel_fb;
  7724. val = I915_READ(DSPCNTR(pipe));
  7725. if (!(val & DISPLAY_PLANE_ENABLE))
  7726. return;
  7727. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7728. if (!intel_fb) {
  7729. DRM_DEBUG_KMS("failed to alloc fb\n");
  7730. return;
  7731. }
  7732. fb = &intel_fb->base;
  7733. if (INTEL_INFO(dev)->gen >= 4) {
  7734. if (val & DISPPLANE_TILED) {
  7735. plane_config->tiling = I915_TILING_X;
  7736. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7737. }
  7738. }
  7739. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7740. fourcc = i9xx_format_to_fourcc(pixel_format);
  7741. fb->pixel_format = fourcc;
  7742. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7743. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7744. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7745. offset = I915_READ(DSPOFFSET(pipe));
  7746. } else {
  7747. if (plane_config->tiling)
  7748. offset = I915_READ(DSPTILEOFF(pipe));
  7749. else
  7750. offset = I915_READ(DSPLINOFF(pipe));
  7751. }
  7752. plane_config->base = base;
  7753. val = I915_READ(PIPESRC(pipe));
  7754. fb->width = ((val >> 16) & 0xfff) + 1;
  7755. fb->height = ((val >> 0) & 0xfff) + 1;
  7756. val = I915_READ(DSPSTRIDE(pipe));
  7757. fb->pitches[0] = val & 0xffffffc0;
  7758. aligned_height = intel_fb_align_height(dev, fb->height,
  7759. fb->pixel_format,
  7760. fb->modifier[0]);
  7761. plane_config->size = fb->pitches[0] * aligned_height;
  7762. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7763. pipe_name(pipe), fb->width, fb->height,
  7764. fb->bits_per_pixel, base, fb->pitches[0],
  7765. plane_config->size);
  7766. plane_config->fb = intel_fb;
  7767. }
  7768. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7769. struct intel_crtc_state *pipe_config)
  7770. {
  7771. struct drm_device *dev = crtc->base.dev;
  7772. struct drm_i915_private *dev_priv = dev->dev_private;
  7773. uint32_t tmp;
  7774. if (!intel_display_power_is_enabled(dev_priv,
  7775. POWER_DOMAIN_PIPE(crtc->pipe)))
  7776. return false;
  7777. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7778. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7779. tmp = I915_READ(PIPECONF(crtc->pipe));
  7780. if (!(tmp & PIPECONF_ENABLE))
  7781. return false;
  7782. switch (tmp & PIPECONF_BPC_MASK) {
  7783. case PIPECONF_6BPC:
  7784. pipe_config->pipe_bpp = 18;
  7785. break;
  7786. case PIPECONF_8BPC:
  7787. pipe_config->pipe_bpp = 24;
  7788. break;
  7789. case PIPECONF_10BPC:
  7790. pipe_config->pipe_bpp = 30;
  7791. break;
  7792. case PIPECONF_12BPC:
  7793. pipe_config->pipe_bpp = 36;
  7794. break;
  7795. default:
  7796. break;
  7797. }
  7798. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7799. pipe_config->limited_color_range = true;
  7800. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7801. struct intel_shared_dpll *pll;
  7802. pipe_config->has_pch_encoder = true;
  7803. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7804. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7805. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7806. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7807. if (HAS_PCH_IBX(dev_priv->dev)) {
  7808. pipe_config->shared_dpll =
  7809. (enum intel_dpll_id) crtc->pipe;
  7810. } else {
  7811. tmp = I915_READ(PCH_DPLL_SEL);
  7812. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7813. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7814. else
  7815. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7816. }
  7817. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7818. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7819. &pipe_config->dpll_hw_state));
  7820. tmp = pipe_config->dpll_hw_state.dpll;
  7821. pipe_config->pixel_multiplier =
  7822. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7823. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7824. ironlake_pch_clock_get(crtc, pipe_config);
  7825. } else {
  7826. pipe_config->pixel_multiplier = 1;
  7827. }
  7828. intel_get_pipe_timings(crtc, pipe_config);
  7829. ironlake_get_pfit_config(crtc, pipe_config);
  7830. return true;
  7831. }
  7832. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7833. {
  7834. struct drm_device *dev = dev_priv->dev;
  7835. struct intel_crtc *crtc;
  7836. for_each_intel_crtc(dev, crtc)
  7837. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7838. pipe_name(crtc->pipe));
  7839. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7840. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7841. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7842. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7843. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7844. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7845. "CPU PWM1 enabled\n");
  7846. if (IS_HASWELL(dev))
  7847. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7848. "CPU PWM2 enabled\n");
  7849. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7850. "PCH PWM1 enabled\n");
  7851. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7852. "Utility pin enabled\n");
  7853. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7854. /*
  7855. * In theory we can still leave IRQs enabled, as long as only the HPD
  7856. * interrupts remain enabled. We used to check for that, but since it's
  7857. * gen-specific and since we only disable LCPLL after we fully disable
  7858. * the interrupts, the check below should be enough.
  7859. */
  7860. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7861. }
  7862. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7863. {
  7864. struct drm_device *dev = dev_priv->dev;
  7865. if (IS_HASWELL(dev))
  7866. return I915_READ(D_COMP_HSW);
  7867. else
  7868. return I915_READ(D_COMP_BDW);
  7869. }
  7870. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7871. {
  7872. struct drm_device *dev = dev_priv->dev;
  7873. if (IS_HASWELL(dev)) {
  7874. mutex_lock(&dev_priv->rps.hw_lock);
  7875. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7876. val))
  7877. DRM_ERROR("Failed to write to D_COMP\n");
  7878. mutex_unlock(&dev_priv->rps.hw_lock);
  7879. } else {
  7880. I915_WRITE(D_COMP_BDW, val);
  7881. POSTING_READ(D_COMP_BDW);
  7882. }
  7883. }
  7884. /*
  7885. * This function implements pieces of two sequences from BSpec:
  7886. * - Sequence for display software to disable LCPLL
  7887. * - Sequence for display software to allow package C8+
  7888. * The steps implemented here are just the steps that actually touch the LCPLL
  7889. * register. Callers should take care of disabling all the display engine
  7890. * functions, doing the mode unset, fixing interrupts, etc.
  7891. */
  7892. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7893. bool switch_to_fclk, bool allow_power_down)
  7894. {
  7895. uint32_t val;
  7896. assert_can_disable_lcpll(dev_priv);
  7897. val = I915_READ(LCPLL_CTL);
  7898. if (switch_to_fclk) {
  7899. val |= LCPLL_CD_SOURCE_FCLK;
  7900. I915_WRITE(LCPLL_CTL, val);
  7901. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7902. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7903. DRM_ERROR("Switching to FCLK failed\n");
  7904. val = I915_READ(LCPLL_CTL);
  7905. }
  7906. val |= LCPLL_PLL_DISABLE;
  7907. I915_WRITE(LCPLL_CTL, val);
  7908. POSTING_READ(LCPLL_CTL);
  7909. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7910. DRM_ERROR("LCPLL still locked\n");
  7911. val = hsw_read_dcomp(dev_priv);
  7912. val |= D_COMP_COMP_DISABLE;
  7913. hsw_write_dcomp(dev_priv, val);
  7914. ndelay(100);
  7915. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7916. 1))
  7917. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7918. if (allow_power_down) {
  7919. val = I915_READ(LCPLL_CTL);
  7920. val |= LCPLL_POWER_DOWN_ALLOW;
  7921. I915_WRITE(LCPLL_CTL, val);
  7922. POSTING_READ(LCPLL_CTL);
  7923. }
  7924. }
  7925. /*
  7926. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7927. * source.
  7928. */
  7929. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7930. {
  7931. uint32_t val;
  7932. val = I915_READ(LCPLL_CTL);
  7933. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7934. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7935. return;
  7936. /*
  7937. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7938. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7939. */
  7940. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7941. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7942. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7943. I915_WRITE(LCPLL_CTL, val);
  7944. POSTING_READ(LCPLL_CTL);
  7945. }
  7946. val = hsw_read_dcomp(dev_priv);
  7947. val |= D_COMP_COMP_FORCE;
  7948. val &= ~D_COMP_COMP_DISABLE;
  7949. hsw_write_dcomp(dev_priv, val);
  7950. val = I915_READ(LCPLL_CTL);
  7951. val &= ~LCPLL_PLL_DISABLE;
  7952. I915_WRITE(LCPLL_CTL, val);
  7953. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7954. DRM_ERROR("LCPLL not locked yet\n");
  7955. if (val & LCPLL_CD_SOURCE_FCLK) {
  7956. val = I915_READ(LCPLL_CTL);
  7957. val &= ~LCPLL_CD_SOURCE_FCLK;
  7958. I915_WRITE(LCPLL_CTL, val);
  7959. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7960. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7961. DRM_ERROR("Switching back to LCPLL failed\n");
  7962. }
  7963. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7964. intel_update_cdclk(dev_priv->dev);
  7965. }
  7966. /*
  7967. * Package states C8 and deeper are really deep PC states that can only be
  7968. * reached when all the devices on the system allow it, so even if the graphics
  7969. * device allows PC8+, it doesn't mean the system will actually get to these
  7970. * states. Our driver only allows PC8+ when going into runtime PM.
  7971. *
  7972. * The requirements for PC8+ are that all the outputs are disabled, the power
  7973. * well is disabled and most interrupts are disabled, and these are also
  7974. * requirements for runtime PM. When these conditions are met, we manually do
  7975. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7976. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7977. * hang the machine.
  7978. *
  7979. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7980. * the state of some registers, so when we come back from PC8+ we need to
  7981. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7982. * need to take care of the registers kept by RC6. Notice that this happens even
  7983. * if we don't put the device in PCI D3 state (which is what currently happens
  7984. * because of the runtime PM support).
  7985. *
  7986. * For more, read "Display Sequences for Package C8" on the hardware
  7987. * documentation.
  7988. */
  7989. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7990. {
  7991. struct drm_device *dev = dev_priv->dev;
  7992. uint32_t val;
  7993. DRM_DEBUG_KMS("Enabling package C8+\n");
  7994. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  7995. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7996. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7997. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7998. }
  7999. lpt_disable_clkout_dp(dev);
  8000. hsw_disable_lcpll(dev_priv, true, true);
  8001. }
  8002. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8003. {
  8004. struct drm_device *dev = dev_priv->dev;
  8005. uint32_t val;
  8006. DRM_DEBUG_KMS("Disabling package C8+\n");
  8007. hsw_restore_lcpll(dev_priv);
  8008. lpt_init_pch_refclk(dev);
  8009. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8010. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8011. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8012. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8013. }
  8014. intel_prepare_ddi(dev);
  8015. }
  8016. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  8017. {
  8018. struct drm_device *dev = old_state->dev;
  8019. struct drm_i915_private *dev_priv = dev->dev_private;
  8020. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  8021. int req_cdclk;
  8022. /* see the comment in valleyview_modeset_global_resources */
  8023. if (WARN_ON(max_pixclk < 0))
  8024. return;
  8025. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  8026. if (req_cdclk != dev_priv->cdclk_freq)
  8027. broxton_set_cdclk(dev, req_cdclk);
  8028. }
  8029. /* compute the max rate for new configuration */
  8030. static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
  8031. {
  8032. struct drm_device *dev = dev_priv->dev;
  8033. struct intel_crtc *intel_crtc;
  8034. struct drm_crtc *crtc;
  8035. int max_pixel_rate = 0;
  8036. int pixel_rate;
  8037. for_each_crtc(dev, crtc) {
  8038. if (!crtc->state->enable)
  8039. continue;
  8040. intel_crtc = to_intel_crtc(crtc);
  8041. pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
  8042. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8043. if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
  8044. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8045. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8046. }
  8047. return max_pixel_rate;
  8048. }
  8049. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8050. {
  8051. struct drm_i915_private *dev_priv = dev->dev_private;
  8052. uint32_t val, data;
  8053. int ret;
  8054. if (WARN((I915_READ(LCPLL_CTL) &
  8055. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8056. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8057. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8058. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8059. "trying to change cdclk frequency with cdclk not enabled\n"))
  8060. return;
  8061. mutex_lock(&dev_priv->rps.hw_lock);
  8062. ret = sandybridge_pcode_write(dev_priv,
  8063. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8064. mutex_unlock(&dev_priv->rps.hw_lock);
  8065. if (ret) {
  8066. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8067. return;
  8068. }
  8069. val = I915_READ(LCPLL_CTL);
  8070. val |= LCPLL_CD_SOURCE_FCLK;
  8071. I915_WRITE(LCPLL_CTL, val);
  8072. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8073. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8074. DRM_ERROR("Switching to FCLK failed\n");
  8075. val = I915_READ(LCPLL_CTL);
  8076. val &= ~LCPLL_CLK_FREQ_MASK;
  8077. switch (cdclk) {
  8078. case 450000:
  8079. val |= LCPLL_CLK_FREQ_450;
  8080. data = 0;
  8081. break;
  8082. case 540000:
  8083. val |= LCPLL_CLK_FREQ_54O_BDW;
  8084. data = 1;
  8085. break;
  8086. case 337500:
  8087. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8088. data = 2;
  8089. break;
  8090. case 675000:
  8091. val |= LCPLL_CLK_FREQ_675_BDW;
  8092. data = 3;
  8093. break;
  8094. default:
  8095. WARN(1, "invalid cdclk frequency\n");
  8096. return;
  8097. }
  8098. I915_WRITE(LCPLL_CTL, val);
  8099. val = I915_READ(LCPLL_CTL);
  8100. val &= ~LCPLL_CD_SOURCE_FCLK;
  8101. I915_WRITE(LCPLL_CTL, val);
  8102. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8103. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8104. DRM_ERROR("Switching back to LCPLL failed\n");
  8105. mutex_lock(&dev_priv->rps.hw_lock);
  8106. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8107. mutex_unlock(&dev_priv->rps.hw_lock);
  8108. intel_update_cdclk(dev);
  8109. WARN(cdclk != dev_priv->cdclk_freq,
  8110. "cdclk requested %d kHz but got %d kHz\n",
  8111. cdclk, dev_priv->cdclk_freq);
  8112. }
  8113. static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
  8114. int max_pixel_rate)
  8115. {
  8116. int cdclk;
  8117. /*
  8118. * FIXME should also account for plane ratio
  8119. * once 64bpp pixel formats are supported.
  8120. */
  8121. if (max_pixel_rate > 540000)
  8122. cdclk = 675000;
  8123. else if (max_pixel_rate > 450000)
  8124. cdclk = 540000;
  8125. else if (max_pixel_rate > 337500)
  8126. cdclk = 450000;
  8127. else
  8128. cdclk = 337500;
  8129. /*
  8130. * FIXME move the cdclk caclulation to
  8131. * compute_config() so we can fail gracegully.
  8132. */
  8133. if (cdclk > dev_priv->max_cdclk_freq) {
  8134. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8135. cdclk, dev_priv->max_cdclk_freq);
  8136. cdclk = dev_priv->max_cdclk_freq;
  8137. }
  8138. return cdclk;
  8139. }
  8140. static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
  8141. {
  8142. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8143. struct drm_crtc *crtc;
  8144. struct drm_crtc_state *crtc_state;
  8145. int max_pixclk = ilk_max_pixel_rate(dev_priv);
  8146. int cdclk, i;
  8147. cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
  8148. if (cdclk == dev_priv->cdclk_freq)
  8149. return 0;
  8150. /* add all active pipes to the state */
  8151. for_each_crtc(state->dev, crtc) {
  8152. if (!crtc->state->enable)
  8153. continue;
  8154. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  8155. if (IS_ERR(crtc_state))
  8156. return PTR_ERR(crtc_state);
  8157. }
  8158. /* disable/enable all currently active pipes while we change cdclk */
  8159. for_each_crtc_in_state(state, crtc, crtc_state, i)
  8160. if (crtc_state->enable)
  8161. crtc_state->mode_changed = true;
  8162. return 0;
  8163. }
  8164. static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
  8165. {
  8166. struct drm_device *dev = state->dev;
  8167. struct drm_i915_private *dev_priv = dev->dev_private;
  8168. int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
  8169. int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
  8170. if (req_cdclk != dev_priv->cdclk_freq)
  8171. broadwell_set_cdclk(dev, req_cdclk);
  8172. }
  8173. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8174. struct intel_crtc_state *crtc_state)
  8175. {
  8176. if (!intel_ddi_pll_select(crtc, crtc_state))
  8177. return -EINVAL;
  8178. crtc->lowfreq_avail = false;
  8179. return 0;
  8180. }
  8181. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8182. enum port port,
  8183. struct intel_crtc_state *pipe_config)
  8184. {
  8185. switch (port) {
  8186. case PORT_A:
  8187. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8188. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8189. break;
  8190. case PORT_B:
  8191. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8192. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8193. break;
  8194. case PORT_C:
  8195. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8196. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8197. break;
  8198. default:
  8199. DRM_ERROR("Incorrect port type\n");
  8200. }
  8201. }
  8202. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8203. enum port port,
  8204. struct intel_crtc_state *pipe_config)
  8205. {
  8206. u32 temp, dpll_ctl1;
  8207. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8208. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8209. switch (pipe_config->ddi_pll_sel) {
  8210. case SKL_DPLL0:
  8211. /*
  8212. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8213. * of the shared DPLL framework and thus needs to be read out
  8214. * separately
  8215. */
  8216. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8217. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8218. break;
  8219. case SKL_DPLL1:
  8220. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8221. break;
  8222. case SKL_DPLL2:
  8223. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8224. break;
  8225. case SKL_DPLL3:
  8226. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8227. break;
  8228. }
  8229. }
  8230. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8231. enum port port,
  8232. struct intel_crtc_state *pipe_config)
  8233. {
  8234. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8235. switch (pipe_config->ddi_pll_sel) {
  8236. case PORT_CLK_SEL_WRPLL1:
  8237. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8238. break;
  8239. case PORT_CLK_SEL_WRPLL2:
  8240. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8241. break;
  8242. }
  8243. }
  8244. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8245. struct intel_crtc_state *pipe_config)
  8246. {
  8247. struct drm_device *dev = crtc->base.dev;
  8248. struct drm_i915_private *dev_priv = dev->dev_private;
  8249. struct intel_shared_dpll *pll;
  8250. enum port port;
  8251. uint32_t tmp;
  8252. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8253. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8254. if (IS_SKYLAKE(dev))
  8255. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8256. else if (IS_BROXTON(dev))
  8257. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8258. else
  8259. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8260. if (pipe_config->shared_dpll >= 0) {
  8261. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8262. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8263. &pipe_config->dpll_hw_state));
  8264. }
  8265. /*
  8266. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8267. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8268. * the PCH transcoder is on.
  8269. */
  8270. if (INTEL_INFO(dev)->gen < 9 &&
  8271. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8272. pipe_config->has_pch_encoder = true;
  8273. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8274. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8275. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8276. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8277. }
  8278. }
  8279. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8280. struct intel_crtc_state *pipe_config)
  8281. {
  8282. struct drm_device *dev = crtc->base.dev;
  8283. struct drm_i915_private *dev_priv = dev->dev_private;
  8284. enum intel_display_power_domain pfit_domain;
  8285. uint32_t tmp;
  8286. if (!intel_display_power_is_enabled(dev_priv,
  8287. POWER_DOMAIN_PIPE(crtc->pipe)))
  8288. return false;
  8289. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8290. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8291. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8292. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8293. enum pipe trans_edp_pipe;
  8294. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8295. default:
  8296. WARN(1, "unknown pipe linked to edp transcoder\n");
  8297. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8298. case TRANS_DDI_EDP_INPUT_A_ON:
  8299. trans_edp_pipe = PIPE_A;
  8300. break;
  8301. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8302. trans_edp_pipe = PIPE_B;
  8303. break;
  8304. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8305. trans_edp_pipe = PIPE_C;
  8306. break;
  8307. }
  8308. if (trans_edp_pipe == crtc->pipe)
  8309. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8310. }
  8311. if (!intel_display_power_is_enabled(dev_priv,
  8312. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8313. return false;
  8314. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8315. if (!(tmp & PIPECONF_ENABLE))
  8316. return false;
  8317. haswell_get_ddi_port_state(crtc, pipe_config);
  8318. intel_get_pipe_timings(crtc, pipe_config);
  8319. if (INTEL_INFO(dev)->gen >= 9) {
  8320. skl_init_scalers(dev, crtc, pipe_config);
  8321. }
  8322. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8323. if (INTEL_INFO(dev)->gen >= 9) {
  8324. pipe_config->scaler_state.scaler_id = -1;
  8325. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8326. }
  8327. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8328. if (INTEL_INFO(dev)->gen == 9)
  8329. skylake_get_pfit_config(crtc, pipe_config);
  8330. else if (INTEL_INFO(dev)->gen < 9)
  8331. ironlake_get_pfit_config(crtc, pipe_config);
  8332. else
  8333. MISSING_CASE(INTEL_INFO(dev)->gen);
  8334. }
  8335. if (IS_HASWELL(dev))
  8336. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8337. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8338. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8339. pipe_config->pixel_multiplier =
  8340. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8341. } else {
  8342. pipe_config->pixel_multiplier = 1;
  8343. }
  8344. return true;
  8345. }
  8346. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8347. {
  8348. struct drm_device *dev = crtc->dev;
  8349. struct drm_i915_private *dev_priv = dev->dev_private;
  8350. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8351. uint32_t cntl = 0, size = 0;
  8352. if (base) {
  8353. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8354. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8355. unsigned int stride = roundup_pow_of_two(width) * 4;
  8356. switch (stride) {
  8357. default:
  8358. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8359. width, stride);
  8360. stride = 256;
  8361. /* fallthrough */
  8362. case 256:
  8363. case 512:
  8364. case 1024:
  8365. case 2048:
  8366. break;
  8367. }
  8368. cntl |= CURSOR_ENABLE |
  8369. CURSOR_GAMMA_ENABLE |
  8370. CURSOR_FORMAT_ARGB |
  8371. CURSOR_STRIDE(stride);
  8372. size = (height << 12) | width;
  8373. }
  8374. if (intel_crtc->cursor_cntl != 0 &&
  8375. (intel_crtc->cursor_base != base ||
  8376. intel_crtc->cursor_size != size ||
  8377. intel_crtc->cursor_cntl != cntl)) {
  8378. /* On these chipsets we can only modify the base/size/stride
  8379. * whilst the cursor is disabled.
  8380. */
  8381. I915_WRITE(_CURACNTR, 0);
  8382. POSTING_READ(_CURACNTR);
  8383. intel_crtc->cursor_cntl = 0;
  8384. }
  8385. if (intel_crtc->cursor_base != base) {
  8386. I915_WRITE(_CURABASE, base);
  8387. intel_crtc->cursor_base = base;
  8388. }
  8389. if (intel_crtc->cursor_size != size) {
  8390. I915_WRITE(CURSIZE, size);
  8391. intel_crtc->cursor_size = size;
  8392. }
  8393. if (intel_crtc->cursor_cntl != cntl) {
  8394. I915_WRITE(_CURACNTR, cntl);
  8395. POSTING_READ(_CURACNTR);
  8396. intel_crtc->cursor_cntl = cntl;
  8397. }
  8398. }
  8399. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8400. {
  8401. struct drm_device *dev = crtc->dev;
  8402. struct drm_i915_private *dev_priv = dev->dev_private;
  8403. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8404. int pipe = intel_crtc->pipe;
  8405. uint32_t cntl;
  8406. cntl = 0;
  8407. if (base) {
  8408. cntl = MCURSOR_GAMMA_ENABLE;
  8409. switch (intel_crtc->base.cursor->state->crtc_w) {
  8410. case 64:
  8411. cntl |= CURSOR_MODE_64_ARGB_AX;
  8412. break;
  8413. case 128:
  8414. cntl |= CURSOR_MODE_128_ARGB_AX;
  8415. break;
  8416. case 256:
  8417. cntl |= CURSOR_MODE_256_ARGB_AX;
  8418. break;
  8419. default:
  8420. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8421. return;
  8422. }
  8423. cntl |= pipe << 28; /* Connect to correct pipe */
  8424. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8425. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8426. }
  8427. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8428. cntl |= CURSOR_ROTATE_180;
  8429. if (intel_crtc->cursor_cntl != cntl) {
  8430. I915_WRITE(CURCNTR(pipe), cntl);
  8431. POSTING_READ(CURCNTR(pipe));
  8432. intel_crtc->cursor_cntl = cntl;
  8433. }
  8434. /* and commit changes on next vblank */
  8435. I915_WRITE(CURBASE(pipe), base);
  8436. POSTING_READ(CURBASE(pipe));
  8437. intel_crtc->cursor_base = base;
  8438. }
  8439. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8440. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8441. bool on)
  8442. {
  8443. struct drm_device *dev = crtc->dev;
  8444. struct drm_i915_private *dev_priv = dev->dev_private;
  8445. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8446. int pipe = intel_crtc->pipe;
  8447. int x = crtc->cursor_x;
  8448. int y = crtc->cursor_y;
  8449. u32 base = 0, pos = 0;
  8450. if (on)
  8451. base = intel_crtc->cursor_addr;
  8452. if (x >= intel_crtc->config->pipe_src_w)
  8453. base = 0;
  8454. if (y >= intel_crtc->config->pipe_src_h)
  8455. base = 0;
  8456. if (x < 0) {
  8457. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8458. base = 0;
  8459. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8460. x = -x;
  8461. }
  8462. pos |= x << CURSOR_X_SHIFT;
  8463. if (y < 0) {
  8464. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8465. base = 0;
  8466. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8467. y = -y;
  8468. }
  8469. pos |= y << CURSOR_Y_SHIFT;
  8470. if (base == 0 && intel_crtc->cursor_base == 0)
  8471. return;
  8472. I915_WRITE(CURPOS(pipe), pos);
  8473. /* ILK+ do this automagically */
  8474. if (HAS_GMCH_DISPLAY(dev) &&
  8475. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8476. base += (intel_crtc->base.cursor->state->crtc_h *
  8477. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8478. }
  8479. if (IS_845G(dev) || IS_I865G(dev))
  8480. i845_update_cursor(crtc, base);
  8481. else
  8482. i9xx_update_cursor(crtc, base);
  8483. }
  8484. static bool cursor_size_ok(struct drm_device *dev,
  8485. uint32_t width, uint32_t height)
  8486. {
  8487. if (width == 0 || height == 0)
  8488. return false;
  8489. /*
  8490. * 845g/865g are special in that they are only limited by
  8491. * the width of their cursors, the height is arbitrary up to
  8492. * the precision of the register. Everything else requires
  8493. * square cursors, limited to a few power-of-two sizes.
  8494. */
  8495. if (IS_845G(dev) || IS_I865G(dev)) {
  8496. if ((width & 63) != 0)
  8497. return false;
  8498. if (width > (IS_845G(dev) ? 64 : 512))
  8499. return false;
  8500. if (height > 1023)
  8501. return false;
  8502. } else {
  8503. switch (width | height) {
  8504. case 256:
  8505. case 128:
  8506. if (IS_GEN2(dev))
  8507. return false;
  8508. case 64:
  8509. break;
  8510. default:
  8511. return false;
  8512. }
  8513. }
  8514. return true;
  8515. }
  8516. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8517. u16 *blue, uint32_t start, uint32_t size)
  8518. {
  8519. int end = (start + size > 256) ? 256 : start + size, i;
  8520. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8521. for (i = start; i < end; i++) {
  8522. intel_crtc->lut_r[i] = red[i] >> 8;
  8523. intel_crtc->lut_g[i] = green[i] >> 8;
  8524. intel_crtc->lut_b[i] = blue[i] >> 8;
  8525. }
  8526. intel_crtc_load_lut(crtc);
  8527. }
  8528. /* VESA 640x480x72Hz mode to set on the pipe */
  8529. static struct drm_display_mode load_detect_mode = {
  8530. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8531. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8532. };
  8533. struct drm_framebuffer *
  8534. __intel_framebuffer_create(struct drm_device *dev,
  8535. struct drm_mode_fb_cmd2 *mode_cmd,
  8536. struct drm_i915_gem_object *obj)
  8537. {
  8538. struct intel_framebuffer *intel_fb;
  8539. int ret;
  8540. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8541. if (!intel_fb) {
  8542. drm_gem_object_unreference(&obj->base);
  8543. return ERR_PTR(-ENOMEM);
  8544. }
  8545. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8546. if (ret)
  8547. goto err;
  8548. return &intel_fb->base;
  8549. err:
  8550. drm_gem_object_unreference(&obj->base);
  8551. kfree(intel_fb);
  8552. return ERR_PTR(ret);
  8553. }
  8554. static struct drm_framebuffer *
  8555. intel_framebuffer_create(struct drm_device *dev,
  8556. struct drm_mode_fb_cmd2 *mode_cmd,
  8557. struct drm_i915_gem_object *obj)
  8558. {
  8559. struct drm_framebuffer *fb;
  8560. int ret;
  8561. ret = i915_mutex_lock_interruptible(dev);
  8562. if (ret)
  8563. return ERR_PTR(ret);
  8564. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8565. mutex_unlock(&dev->struct_mutex);
  8566. return fb;
  8567. }
  8568. static u32
  8569. intel_framebuffer_pitch_for_width(int width, int bpp)
  8570. {
  8571. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8572. return ALIGN(pitch, 64);
  8573. }
  8574. static u32
  8575. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8576. {
  8577. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8578. return PAGE_ALIGN(pitch * mode->vdisplay);
  8579. }
  8580. static struct drm_framebuffer *
  8581. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8582. struct drm_display_mode *mode,
  8583. int depth, int bpp)
  8584. {
  8585. struct drm_i915_gem_object *obj;
  8586. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8587. obj = i915_gem_alloc_object(dev,
  8588. intel_framebuffer_size_for_mode(mode, bpp));
  8589. if (obj == NULL)
  8590. return ERR_PTR(-ENOMEM);
  8591. mode_cmd.width = mode->hdisplay;
  8592. mode_cmd.height = mode->vdisplay;
  8593. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8594. bpp);
  8595. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8596. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8597. }
  8598. static struct drm_framebuffer *
  8599. mode_fits_in_fbdev(struct drm_device *dev,
  8600. struct drm_display_mode *mode)
  8601. {
  8602. #ifdef CONFIG_DRM_I915_FBDEV
  8603. struct drm_i915_private *dev_priv = dev->dev_private;
  8604. struct drm_i915_gem_object *obj;
  8605. struct drm_framebuffer *fb;
  8606. if (!dev_priv->fbdev)
  8607. return NULL;
  8608. if (!dev_priv->fbdev->fb)
  8609. return NULL;
  8610. obj = dev_priv->fbdev->fb->obj;
  8611. BUG_ON(!obj);
  8612. fb = &dev_priv->fbdev->fb->base;
  8613. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8614. fb->bits_per_pixel))
  8615. return NULL;
  8616. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8617. return NULL;
  8618. return fb;
  8619. #else
  8620. return NULL;
  8621. #endif
  8622. }
  8623. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8624. struct drm_crtc *crtc,
  8625. struct drm_display_mode *mode,
  8626. struct drm_framebuffer *fb,
  8627. int x, int y)
  8628. {
  8629. struct drm_plane_state *plane_state;
  8630. int hdisplay, vdisplay;
  8631. int ret;
  8632. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8633. if (IS_ERR(plane_state))
  8634. return PTR_ERR(plane_state);
  8635. if (mode)
  8636. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8637. else
  8638. hdisplay = vdisplay = 0;
  8639. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8640. if (ret)
  8641. return ret;
  8642. drm_atomic_set_fb_for_plane(plane_state, fb);
  8643. plane_state->crtc_x = 0;
  8644. plane_state->crtc_y = 0;
  8645. plane_state->crtc_w = hdisplay;
  8646. plane_state->crtc_h = vdisplay;
  8647. plane_state->src_x = x << 16;
  8648. plane_state->src_y = y << 16;
  8649. plane_state->src_w = hdisplay << 16;
  8650. plane_state->src_h = vdisplay << 16;
  8651. return 0;
  8652. }
  8653. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8654. struct drm_display_mode *mode,
  8655. struct intel_load_detect_pipe *old,
  8656. struct drm_modeset_acquire_ctx *ctx)
  8657. {
  8658. struct intel_crtc *intel_crtc;
  8659. struct intel_encoder *intel_encoder =
  8660. intel_attached_encoder(connector);
  8661. struct drm_crtc *possible_crtc;
  8662. struct drm_encoder *encoder = &intel_encoder->base;
  8663. struct drm_crtc *crtc = NULL;
  8664. struct drm_device *dev = encoder->dev;
  8665. struct drm_framebuffer *fb;
  8666. struct drm_mode_config *config = &dev->mode_config;
  8667. struct drm_atomic_state *state = NULL;
  8668. struct drm_connector_state *connector_state;
  8669. struct intel_crtc_state *crtc_state;
  8670. int ret, i = -1;
  8671. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8672. connector->base.id, connector->name,
  8673. encoder->base.id, encoder->name);
  8674. retry:
  8675. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8676. if (ret)
  8677. goto fail_unlock;
  8678. /*
  8679. * Algorithm gets a little messy:
  8680. *
  8681. * - if the connector already has an assigned crtc, use it (but make
  8682. * sure it's on first)
  8683. *
  8684. * - try to find the first unused crtc that can drive this connector,
  8685. * and use that if we find one
  8686. */
  8687. /* See if we already have a CRTC for this connector */
  8688. if (encoder->crtc) {
  8689. crtc = encoder->crtc;
  8690. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8691. if (ret)
  8692. goto fail_unlock;
  8693. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8694. if (ret)
  8695. goto fail_unlock;
  8696. old->dpms_mode = connector->dpms;
  8697. old->load_detect_temp = false;
  8698. /* Make sure the crtc and connector are running */
  8699. if (connector->dpms != DRM_MODE_DPMS_ON)
  8700. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8701. return true;
  8702. }
  8703. /* Find an unused one (if possible) */
  8704. for_each_crtc(dev, possible_crtc) {
  8705. i++;
  8706. if (!(encoder->possible_crtcs & (1 << i)))
  8707. continue;
  8708. if (possible_crtc->state->enable)
  8709. continue;
  8710. /* This can occur when applying the pipe A quirk on resume. */
  8711. if (to_intel_crtc(possible_crtc)->new_enabled)
  8712. continue;
  8713. crtc = possible_crtc;
  8714. break;
  8715. }
  8716. /*
  8717. * If we didn't find an unused CRTC, don't use any.
  8718. */
  8719. if (!crtc) {
  8720. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8721. goto fail_unlock;
  8722. }
  8723. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8724. if (ret)
  8725. goto fail_unlock;
  8726. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8727. if (ret)
  8728. goto fail_unlock;
  8729. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8730. to_intel_connector(connector)->new_encoder = intel_encoder;
  8731. intel_crtc = to_intel_crtc(crtc);
  8732. intel_crtc->new_enabled = true;
  8733. old->dpms_mode = connector->dpms;
  8734. old->load_detect_temp = true;
  8735. old->release_fb = NULL;
  8736. state = drm_atomic_state_alloc(dev);
  8737. if (!state)
  8738. return false;
  8739. state->acquire_ctx = ctx;
  8740. connector_state = drm_atomic_get_connector_state(state, connector);
  8741. if (IS_ERR(connector_state)) {
  8742. ret = PTR_ERR(connector_state);
  8743. goto fail;
  8744. }
  8745. connector_state->crtc = crtc;
  8746. connector_state->best_encoder = &intel_encoder->base;
  8747. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8748. if (IS_ERR(crtc_state)) {
  8749. ret = PTR_ERR(crtc_state);
  8750. goto fail;
  8751. }
  8752. crtc_state->base.active = crtc_state->base.enable = true;
  8753. if (!mode)
  8754. mode = &load_detect_mode;
  8755. /* We need a framebuffer large enough to accommodate all accesses
  8756. * that the plane may generate whilst we perform load detection.
  8757. * We can not rely on the fbcon either being present (we get called
  8758. * during its initialisation to detect all boot displays, or it may
  8759. * not even exist) or that it is large enough to satisfy the
  8760. * requested mode.
  8761. */
  8762. fb = mode_fits_in_fbdev(dev, mode);
  8763. if (fb == NULL) {
  8764. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8765. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8766. old->release_fb = fb;
  8767. } else
  8768. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8769. if (IS_ERR(fb)) {
  8770. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8771. goto fail;
  8772. }
  8773. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8774. if (ret)
  8775. goto fail;
  8776. drm_mode_copy(&crtc_state->base.mode, mode);
  8777. if (intel_set_mode(state)) {
  8778. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8779. if (old->release_fb)
  8780. old->release_fb->funcs->destroy(old->release_fb);
  8781. goto fail;
  8782. }
  8783. crtc->primary->crtc = crtc;
  8784. /* let the connector get through one full cycle before testing */
  8785. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8786. return true;
  8787. fail:
  8788. intel_crtc->new_enabled = crtc->state->enable;
  8789. fail_unlock:
  8790. drm_atomic_state_free(state);
  8791. state = NULL;
  8792. if (ret == -EDEADLK) {
  8793. drm_modeset_backoff(ctx);
  8794. goto retry;
  8795. }
  8796. return false;
  8797. }
  8798. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8799. struct intel_load_detect_pipe *old,
  8800. struct drm_modeset_acquire_ctx *ctx)
  8801. {
  8802. struct drm_device *dev = connector->dev;
  8803. struct intel_encoder *intel_encoder =
  8804. intel_attached_encoder(connector);
  8805. struct drm_encoder *encoder = &intel_encoder->base;
  8806. struct drm_crtc *crtc = encoder->crtc;
  8807. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8808. struct drm_atomic_state *state;
  8809. struct drm_connector_state *connector_state;
  8810. struct intel_crtc_state *crtc_state;
  8811. int ret;
  8812. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8813. connector->base.id, connector->name,
  8814. encoder->base.id, encoder->name);
  8815. if (old->load_detect_temp) {
  8816. state = drm_atomic_state_alloc(dev);
  8817. if (!state)
  8818. goto fail;
  8819. state->acquire_ctx = ctx;
  8820. connector_state = drm_atomic_get_connector_state(state, connector);
  8821. if (IS_ERR(connector_state))
  8822. goto fail;
  8823. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8824. if (IS_ERR(crtc_state))
  8825. goto fail;
  8826. to_intel_connector(connector)->new_encoder = NULL;
  8827. intel_encoder->new_crtc = NULL;
  8828. intel_crtc->new_enabled = false;
  8829. connector_state->best_encoder = NULL;
  8830. connector_state->crtc = NULL;
  8831. crtc_state->base.enable = crtc_state->base.active = false;
  8832. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8833. 0, 0);
  8834. if (ret)
  8835. goto fail;
  8836. ret = intel_set_mode(state);
  8837. if (ret)
  8838. goto fail;
  8839. if (old->release_fb) {
  8840. drm_framebuffer_unregister_private(old->release_fb);
  8841. drm_framebuffer_unreference(old->release_fb);
  8842. }
  8843. return;
  8844. }
  8845. /* Switch crtc and encoder back off if necessary */
  8846. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8847. connector->funcs->dpms(connector, old->dpms_mode);
  8848. return;
  8849. fail:
  8850. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8851. drm_atomic_state_free(state);
  8852. }
  8853. static int i9xx_pll_refclk(struct drm_device *dev,
  8854. const struct intel_crtc_state *pipe_config)
  8855. {
  8856. struct drm_i915_private *dev_priv = dev->dev_private;
  8857. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8858. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8859. return dev_priv->vbt.lvds_ssc_freq;
  8860. else if (HAS_PCH_SPLIT(dev))
  8861. return 120000;
  8862. else if (!IS_GEN2(dev))
  8863. return 96000;
  8864. else
  8865. return 48000;
  8866. }
  8867. /* Returns the clock of the currently programmed mode of the given pipe. */
  8868. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8869. struct intel_crtc_state *pipe_config)
  8870. {
  8871. struct drm_device *dev = crtc->base.dev;
  8872. struct drm_i915_private *dev_priv = dev->dev_private;
  8873. int pipe = pipe_config->cpu_transcoder;
  8874. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8875. u32 fp;
  8876. intel_clock_t clock;
  8877. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8878. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8879. fp = pipe_config->dpll_hw_state.fp0;
  8880. else
  8881. fp = pipe_config->dpll_hw_state.fp1;
  8882. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8883. if (IS_PINEVIEW(dev)) {
  8884. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8885. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8886. } else {
  8887. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8888. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8889. }
  8890. if (!IS_GEN2(dev)) {
  8891. if (IS_PINEVIEW(dev))
  8892. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8893. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8894. else
  8895. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8896. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8897. switch (dpll & DPLL_MODE_MASK) {
  8898. case DPLLB_MODE_DAC_SERIAL:
  8899. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8900. 5 : 10;
  8901. break;
  8902. case DPLLB_MODE_LVDS:
  8903. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8904. 7 : 14;
  8905. break;
  8906. default:
  8907. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8908. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8909. return;
  8910. }
  8911. if (IS_PINEVIEW(dev))
  8912. pineview_clock(refclk, &clock);
  8913. else
  8914. i9xx_clock(refclk, &clock);
  8915. } else {
  8916. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8917. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8918. if (is_lvds) {
  8919. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8920. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8921. if (lvds & LVDS_CLKB_POWER_UP)
  8922. clock.p2 = 7;
  8923. else
  8924. clock.p2 = 14;
  8925. } else {
  8926. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8927. clock.p1 = 2;
  8928. else {
  8929. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8930. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8931. }
  8932. if (dpll & PLL_P2_DIVIDE_BY_4)
  8933. clock.p2 = 4;
  8934. else
  8935. clock.p2 = 2;
  8936. }
  8937. i9xx_clock(refclk, &clock);
  8938. }
  8939. /*
  8940. * This value includes pixel_multiplier. We will use
  8941. * port_clock to compute adjusted_mode.crtc_clock in the
  8942. * encoder's get_config() function.
  8943. */
  8944. pipe_config->port_clock = clock.dot;
  8945. }
  8946. int intel_dotclock_calculate(int link_freq,
  8947. const struct intel_link_m_n *m_n)
  8948. {
  8949. /*
  8950. * The calculation for the data clock is:
  8951. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8952. * But we want to avoid losing precison if possible, so:
  8953. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8954. *
  8955. * and the link clock is simpler:
  8956. * link_clock = (m * link_clock) / n
  8957. */
  8958. if (!m_n->link_n)
  8959. return 0;
  8960. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8961. }
  8962. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8963. struct intel_crtc_state *pipe_config)
  8964. {
  8965. struct drm_device *dev = crtc->base.dev;
  8966. /* read out port_clock from the DPLL */
  8967. i9xx_crtc_clock_get(crtc, pipe_config);
  8968. /*
  8969. * This value does not include pixel_multiplier.
  8970. * We will check that port_clock and adjusted_mode.crtc_clock
  8971. * agree once we know their relationship in the encoder's
  8972. * get_config() function.
  8973. */
  8974. pipe_config->base.adjusted_mode.crtc_clock =
  8975. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8976. &pipe_config->fdi_m_n);
  8977. }
  8978. /** Returns the currently programmed mode of the given pipe. */
  8979. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8980. struct drm_crtc *crtc)
  8981. {
  8982. struct drm_i915_private *dev_priv = dev->dev_private;
  8983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8984. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8985. struct drm_display_mode *mode;
  8986. struct intel_crtc_state pipe_config;
  8987. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8988. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8989. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8990. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8991. enum pipe pipe = intel_crtc->pipe;
  8992. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8993. if (!mode)
  8994. return NULL;
  8995. /*
  8996. * Construct a pipe_config sufficient for getting the clock info
  8997. * back out of crtc_clock_get.
  8998. *
  8999. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9000. * to use a real value here instead.
  9001. */
  9002. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  9003. pipe_config.pixel_multiplier = 1;
  9004. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9005. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9006. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9007. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  9008. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  9009. mode->hdisplay = (htot & 0xffff) + 1;
  9010. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9011. mode->hsync_start = (hsync & 0xffff) + 1;
  9012. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9013. mode->vdisplay = (vtot & 0xffff) + 1;
  9014. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9015. mode->vsync_start = (vsync & 0xffff) + 1;
  9016. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9017. drm_mode_set_name(mode);
  9018. return mode;
  9019. }
  9020. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  9021. {
  9022. struct drm_device *dev = crtc->dev;
  9023. struct drm_i915_private *dev_priv = dev->dev_private;
  9024. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9025. if (!HAS_GMCH_DISPLAY(dev))
  9026. return;
  9027. if (!dev_priv->lvds_downclock_avail)
  9028. return;
  9029. /*
  9030. * Since this is called by a timer, we should never get here in
  9031. * the manual case.
  9032. */
  9033. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  9034. int pipe = intel_crtc->pipe;
  9035. int dpll_reg = DPLL(pipe);
  9036. int dpll;
  9037. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  9038. assert_panel_unlocked(dev_priv, pipe);
  9039. dpll = I915_READ(dpll_reg);
  9040. dpll |= DISPLAY_RATE_SELECT_FPA1;
  9041. I915_WRITE(dpll_reg, dpll);
  9042. intel_wait_for_vblank(dev, pipe);
  9043. dpll = I915_READ(dpll_reg);
  9044. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  9045. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  9046. }
  9047. }
  9048. void intel_mark_busy(struct drm_device *dev)
  9049. {
  9050. struct drm_i915_private *dev_priv = dev->dev_private;
  9051. if (dev_priv->mm.busy)
  9052. return;
  9053. intel_runtime_pm_get(dev_priv);
  9054. i915_update_gfx_val(dev_priv);
  9055. if (INTEL_INFO(dev)->gen >= 6)
  9056. gen6_rps_busy(dev_priv);
  9057. dev_priv->mm.busy = true;
  9058. }
  9059. void intel_mark_idle(struct drm_device *dev)
  9060. {
  9061. struct drm_i915_private *dev_priv = dev->dev_private;
  9062. struct drm_crtc *crtc;
  9063. if (!dev_priv->mm.busy)
  9064. return;
  9065. dev_priv->mm.busy = false;
  9066. for_each_crtc(dev, crtc) {
  9067. if (!crtc->primary->fb)
  9068. continue;
  9069. intel_decrease_pllclock(crtc);
  9070. }
  9071. if (INTEL_INFO(dev)->gen >= 6)
  9072. gen6_rps_idle(dev->dev_private);
  9073. intel_runtime_pm_put(dev_priv);
  9074. }
  9075. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9076. {
  9077. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9078. struct drm_device *dev = crtc->dev;
  9079. struct intel_unpin_work *work;
  9080. spin_lock_irq(&dev->event_lock);
  9081. work = intel_crtc->unpin_work;
  9082. intel_crtc->unpin_work = NULL;
  9083. spin_unlock_irq(&dev->event_lock);
  9084. if (work) {
  9085. cancel_work_sync(&work->work);
  9086. kfree(work);
  9087. }
  9088. drm_crtc_cleanup(crtc);
  9089. kfree(intel_crtc);
  9090. }
  9091. static void intel_unpin_work_fn(struct work_struct *__work)
  9092. {
  9093. struct intel_unpin_work *work =
  9094. container_of(__work, struct intel_unpin_work, work);
  9095. struct drm_device *dev = work->crtc->dev;
  9096. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  9097. mutex_lock(&dev->struct_mutex);
  9098. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  9099. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9100. intel_fbc_update(dev);
  9101. if (work->flip_queued_req)
  9102. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9103. mutex_unlock(&dev->struct_mutex);
  9104. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9105. drm_framebuffer_unreference(work->old_fb);
  9106. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  9107. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  9108. kfree(work);
  9109. }
  9110. static void do_intel_finish_page_flip(struct drm_device *dev,
  9111. struct drm_crtc *crtc)
  9112. {
  9113. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9114. struct intel_unpin_work *work;
  9115. unsigned long flags;
  9116. /* Ignore early vblank irqs */
  9117. if (intel_crtc == NULL)
  9118. return;
  9119. /*
  9120. * This is called both by irq handlers and the reset code (to complete
  9121. * lost pageflips) so needs the full irqsave spinlocks.
  9122. */
  9123. spin_lock_irqsave(&dev->event_lock, flags);
  9124. work = intel_crtc->unpin_work;
  9125. /* Ensure we don't miss a work->pending update ... */
  9126. smp_rmb();
  9127. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9128. spin_unlock_irqrestore(&dev->event_lock, flags);
  9129. return;
  9130. }
  9131. page_flip_completed(intel_crtc);
  9132. spin_unlock_irqrestore(&dev->event_lock, flags);
  9133. }
  9134. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9135. {
  9136. struct drm_i915_private *dev_priv = dev->dev_private;
  9137. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9138. do_intel_finish_page_flip(dev, crtc);
  9139. }
  9140. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9141. {
  9142. struct drm_i915_private *dev_priv = dev->dev_private;
  9143. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9144. do_intel_finish_page_flip(dev, crtc);
  9145. }
  9146. /* Is 'a' after or equal to 'b'? */
  9147. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9148. {
  9149. return !((a - b) & 0x80000000);
  9150. }
  9151. static bool page_flip_finished(struct intel_crtc *crtc)
  9152. {
  9153. struct drm_device *dev = crtc->base.dev;
  9154. struct drm_i915_private *dev_priv = dev->dev_private;
  9155. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9156. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9157. return true;
  9158. /*
  9159. * The relevant registers doen't exist on pre-ctg.
  9160. * As the flip done interrupt doesn't trigger for mmio
  9161. * flips on gmch platforms, a flip count check isn't
  9162. * really needed there. But since ctg has the registers,
  9163. * include it in the check anyway.
  9164. */
  9165. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9166. return true;
  9167. /*
  9168. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9169. * used the same base address. In that case the mmio flip might
  9170. * have completed, but the CS hasn't even executed the flip yet.
  9171. *
  9172. * A flip count check isn't enough as the CS might have updated
  9173. * the base address just after start of vblank, but before we
  9174. * managed to process the interrupt. This means we'd complete the
  9175. * CS flip too soon.
  9176. *
  9177. * Combining both checks should get us a good enough result. It may
  9178. * still happen that the CS flip has been executed, but has not
  9179. * yet actually completed. But in case the base address is the same
  9180. * anyway, we don't really care.
  9181. */
  9182. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9183. crtc->unpin_work->gtt_offset &&
  9184. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9185. crtc->unpin_work->flip_count);
  9186. }
  9187. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9188. {
  9189. struct drm_i915_private *dev_priv = dev->dev_private;
  9190. struct intel_crtc *intel_crtc =
  9191. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9192. unsigned long flags;
  9193. /*
  9194. * This is called both by irq handlers and the reset code (to complete
  9195. * lost pageflips) so needs the full irqsave spinlocks.
  9196. *
  9197. * NB: An MMIO update of the plane base pointer will also
  9198. * generate a page-flip completion irq, i.e. every modeset
  9199. * is also accompanied by a spurious intel_prepare_page_flip().
  9200. */
  9201. spin_lock_irqsave(&dev->event_lock, flags);
  9202. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9203. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9204. spin_unlock_irqrestore(&dev->event_lock, flags);
  9205. }
  9206. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9207. {
  9208. /* Ensure that the work item is consistent when activating it ... */
  9209. smp_wmb();
  9210. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9211. /* and that it is marked active as soon as the irq could fire. */
  9212. smp_wmb();
  9213. }
  9214. static int intel_gen2_queue_flip(struct drm_device *dev,
  9215. struct drm_crtc *crtc,
  9216. struct drm_framebuffer *fb,
  9217. struct drm_i915_gem_object *obj,
  9218. struct intel_engine_cs *ring,
  9219. uint32_t flags)
  9220. {
  9221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9222. u32 flip_mask;
  9223. int ret;
  9224. ret = intel_ring_begin(ring, 6);
  9225. if (ret)
  9226. return ret;
  9227. /* Can't queue multiple flips, so wait for the previous
  9228. * one to finish before executing the next.
  9229. */
  9230. if (intel_crtc->plane)
  9231. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9232. else
  9233. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9234. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9235. intel_ring_emit(ring, MI_NOOP);
  9236. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9237. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9238. intel_ring_emit(ring, fb->pitches[0]);
  9239. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9240. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9241. intel_mark_page_flip_active(intel_crtc);
  9242. __intel_ring_advance(ring);
  9243. return 0;
  9244. }
  9245. static int intel_gen3_queue_flip(struct drm_device *dev,
  9246. struct drm_crtc *crtc,
  9247. struct drm_framebuffer *fb,
  9248. struct drm_i915_gem_object *obj,
  9249. struct intel_engine_cs *ring,
  9250. uint32_t flags)
  9251. {
  9252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9253. u32 flip_mask;
  9254. int ret;
  9255. ret = intel_ring_begin(ring, 6);
  9256. if (ret)
  9257. return ret;
  9258. if (intel_crtc->plane)
  9259. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9260. else
  9261. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9262. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9263. intel_ring_emit(ring, MI_NOOP);
  9264. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9265. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9266. intel_ring_emit(ring, fb->pitches[0]);
  9267. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9268. intel_ring_emit(ring, MI_NOOP);
  9269. intel_mark_page_flip_active(intel_crtc);
  9270. __intel_ring_advance(ring);
  9271. return 0;
  9272. }
  9273. static int intel_gen4_queue_flip(struct drm_device *dev,
  9274. struct drm_crtc *crtc,
  9275. struct drm_framebuffer *fb,
  9276. struct drm_i915_gem_object *obj,
  9277. struct intel_engine_cs *ring,
  9278. uint32_t flags)
  9279. {
  9280. struct drm_i915_private *dev_priv = dev->dev_private;
  9281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9282. uint32_t pf, pipesrc;
  9283. int ret;
  9284. ret = intel_ring_begin(ring, 4);
  9285. if (ret)
  9286. return ret;
  9287. /* i965+ uses the linear or tiled offsets from the
  9288. * Display Registers (which do not change across a page-flip)
  9289. * so we need only reprogram the base address.
  9290. */
  9291. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9292. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9293. intel_ring_emit(ring, fb->pitches[0]);
  9294. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9295. obj->tiling_mode);
  9296. /* XXX Enabling the panel-fitter across page-flip is so far
  9297. * untested on non-native modes, so ignore it for now.
  9298. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9299. */
  9300. pf = 0;
  9301. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9302. intel_ring_emit(ring, pf | pipesrc);
  9303. intel_mark_page_flip_active(intel_crtc);
  9304. __intel_ring_advance(ring);
  9305. return 0;
  9306. }
  9307. static int intel_gen6_queue_flip(struct drm_device *dev,
  9308. struct drm_crtc *crtc,
  9309. struct drm_framebuffer *fb,
  9310. struct drm_i915_gem_object *obj,
  9311. struct intel_engine_cs *ring,
  9312. uint32_t flags)
  9313. {
  9314. struct drm_i915_private *dev_priv = dev->dev_private;
  9315. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9316. uint32_t pf, pipesrc;
  9317. int ret;
  9318. ret = intel_ring_begin(ring, 4);
  9319. if (ret)
  9320. return ret;
  9321. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9322. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9323. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9324. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9325. /* Contrary to the suggestions in the documentation,
  9326. * "Enable Panel Fitter" does not seem to be required when page
  9327. * flipping with a non-native mode, and worse causes a normal
  9328. * modeset to fail.
  9329. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9330. */
  9331. pf = 0;
  9332. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9333. intel_ring_emit(ring, pf | pipesrc);
  9334. intel_mark_page_flip_active(intel_crtc);
  9335. __intel_ring_advance(ring);
  9336. return 0;
  9337. }
  9338. static int intel_gen7_queue_flip(struct drm_device *dev,
  9339. struct drm_crtc *crtc,
  9340. struct drm_framebuffer *fb,
  9341. struct drm_i915_gem_object *obj,
  9342. struct intel_engine_cs *ring,
  9343. uint32_t flags)
  9344. {
  9345. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9346. uint32_t plane_bit = 0;
  9347. int len, ret;
  9348. switch (intel_crtc->plane) {
  9349. case PLANE_A:
  9350. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9351. break;
  9352. case PLANE_B:
  9353. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9354. break;
  9355. case PLANE_C:
  9356. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9357. break;
  9358. default:
  9359. WARN_ONCE(1, "unknown plane in flip command\n");
  9360. return -ENODEV;
  9361. }
  9362. len = 4;
  9363. if (ring->id == RCS) {
  9364. len += 6;
  9365. /*
  9366. * On Gen 8, SRM is now taking an extra dword to accommodate
  9367. * 48bits addresses, and we need a NOOP for the batch size to
  9368. * stay even.
  9369. */
  9370. if (IS_GEN8(dev))
  9371. len += 2;
  9372. }
  9373. /*
  9374. * BSpec MI_DISPLAY_FLIP for IVB:
  9375. * "The full packet must be contained within the same cache line."
  9376. *
  9377. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9378. * cacheline, if we ever start emitting more commands before
  9379. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9380. * then do the cacheline alignment, and finally emit the
  9381. * MI_DISPLAY_FLIP.
  9382. */
  9383. ret = intel_ring_cacheline_align(ring);
  9384. if (ret)
  9385. return ret;
  9386. ret = intel_ring_begin(ring, len);
  9387. if (ret)
  9388. return ret;
  9389. /* Unmask the flip-done completion message. Note that the bspec says that
  9390. * we should do this for both the BCS and RCS, and that we must not unmask
  9391. * more than one flip event at any time (or ensure that one flip message
  9392. * can be sent by waiting for flip-done prior to queueing new flips).
  9393. * Experimentation says that BCS works despite DERRMR masking all
  9394. * flip-done completion events and that unmasking all planes at once
  9395. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9396. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9397. */
  9398. if (ring->id == RCS) {
  9399. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9400. intel_ring_emit(ring, DERRMR);
  9401. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9402. DERRMR_PIPEB_PRI_FLIP_DONE |
  9403. DERRMR_PIPEC_PRI_FLIP_DONE));
  9404. if (IS_GEN8(dev))
  9405. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9406. MI_SRM_LRM_GLOBAL_GTT);
  9407. else
  9408. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9409. MI_SRM_LRM_GLOBAL_GTT);
  9410. intel_ring_emit(ring, DERRMR);
  9411. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9412. if (IS_GEN8(dev)) {
  9413. intel_ring_emit(ring, 0);
  9414. intel_ring_emit(ring, MI_NOOP);
  9415. }
  9416. }
  9417. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9418. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9419. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9420. intel_ring_emit(ring, (MI_NOOP));
  9421. intel_mark_page_flip_active(intel_crtc);
  9422. __intel_ring_advance(ring);
  9423. return 0;
  9424. }
  9425. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9426. struct drm_i915_gem_object *obj)
  9427. {
  9428. /*
  9429. * This is not being used for older platforms, because
  9430. * non-availability of flip done interrupt forces us to use
  9431. * CS flips. Older platforms derive flip done using some clever
  9432. * tricks involving the flip_pending status bits and vblank irqs.
  9433. * So using MMIO flips there would disrupt this mechanism.
  9434. */
  9435. if (ring == NULL)
  9436. return true;
  9437. if (INTEL_INFO(ring->dev)->gen < 5)
  9438. return false;
  9439. if (i915.use_mmio_flip < 0)
  9440. return false;
  9441. else if (i915.use_mmio_flip > 0)
  9442. return true;
  9443. else if (i915.enable_execlists)
  9444. return true;
  9445. else
  9446. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9447. }
  9448. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9449. {
  9450. struct drm_device *dev = intel_crtc->base.dev;
  9451. struct drm_i915_private *dev_priv = dev->dev_private;
  9452. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9453. const enum pipe pipe = intel_crtc->pipe;
  9454. u32 ctl, stride;
  9455. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9456. ctl &= ~PLANE_CTL_TILED_MASK;
  9457. switch (fb->modifier[0]) {
  9458. case DRM_FORMAT_MOD_NONE:
  9459. break;
  9460. case I915_FORMAT_MOD_X_TILED:
  9461. ctl |= PLANE_CTL_TILED_X;
  9462. break;
  9463. case I915_FORMAT_MOD_Y_TILED:
  9464. ctl |= PLANE_CTL_TILED_Y;
  9465. break;
  9466. case I915_FORMAT_MOD_Yf_TILED:
  9467. ctl |= PLANE_CTL_TILED_YF;
  9468. break;
  9469. default:
  9470. MISSING_CASE(fb->modifier[0]);
  9471. }
  9472. /*
  9473. * The stride is either expressed as a multiple of 64 bytes chunks for
  9474. * linear buffers or in number of tiles for tiled buffers.
  9475. */
  9476. stride = fb->pitches[0] /
  9477. intel_fb_stride_alignment(dev, fb->modifier[0],
  9478. fb->pixel_format);
  9479. /*
  9480. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9481. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9482. */
  9483. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9484. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9485. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9486. POSTING_READ(PLANE_SURF(pipe, 0));
  9487. }
  9488. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9489. {
  9490. struct drm_device *dev = intel_crtc->base.dev;
  9491. struct drm_i915_private *dev_priv = dev->dev_private;
  9492. struct intel_framebuffer *intel_fb =
  9493. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9494. struct drm_i915_gem_object *obj = intel_fb->obj;
  9495. u32 dspcntr;
  9496. u32 reg;
  9497. reg = DSPCNTR(intel_crtc->plane);
  9498. dspcntr = I915_READ(reg);
  9499. if (obj->tiling_mode != I915_TILING_NONE)
  9500. dspcntr |= DISPPLANE_TILED;
  9501. else
  9502. dspcntr &= ~DISPPLANE_TILED;
  9503. I915_WRITE(reg, dspcntr);
  9504. I915_WRITE(DSPSURF(intel_crtc->plane),
  9505. intel_crtc->unpin_work->gtt_offset);
  9506. POSTING_READ(DSPSURF(intel_crtc->plane));
  9507. }
  9508. /*
  9509. * XXX: This is the temporary way to update the plane registers until we get
  9510. * around to using the usual plane update functions for MMIO flips
  9511. */
  9512. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9513. {
  9514. struct drm_device *dev = intel_crtc->base.dev;
  9515. bool atomic_update;
  9516. u32 start_vbl_count;
  9517. intel_mark_page_flip_active(intel_crtc);
  9518. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9519. if (INTEL_INFO(dev)->gen >= 9)
  9520. skl_do_mmio_flip(intel_crtc);
  9521. else
  9522. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9523. ilk_do_mmio_flip(intel_crtc);
  9524. if (atomic_update)
  9525. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9526. }
  9527. static void intel_mmio_flip_work_func(struct work_struct *work)
  9528. {
  9529. struct intel_mmio_flip *mmio_flip =
  9530. container_of(work, struct intel_mmio_flip, work);
  9531. if (mmio_flip->req)
  9532. WARN_ON(__i915_wait_request(mmio_flip->req,
  9533. mmio_flip->crtc->reset_counter,
  9534. false, NULL,
  9535. &mmio_flip->i915->rps.mmioflips));
  9536. intel_do_mmio_flip(mmio_flip->crtc);
  9537. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9538. kfree(mmio_flip);
  9539. }
  9540. static int intel_queue_mmio_flip(struct drm_device *dev,
  9541. struct drm_crtc *crtc,
  9542. struct drm_framebuffer *fb,
  9543. struct drm_i915_gem_object *obj,
  9544. struct intel_engine_cs *ring,
  9545. uint32_t flags)
  9546. {
  9547. struct intel_mmio_flip *mmio_flip;
  9548. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9549. if (mmio_flip == NULL)
  9550. return -ENOMEM;
  9551. mmio_flip->i915 = to_i915(dev);
  9552. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9553. mmio_flip->crtc = to_intel_crtc(crtc);
  9554. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9555. schedule_work(&mmio_flip->work);
  9556. return 0;
  9557. }
  9558. static int intel_default_queue_flip(struct drm_device *dev,
  9559. struct drm_crtc *crtc,
  9560. struct drm_framebuffer *fb,
  9561. struct drm_i915_gem_object *obj,
  9562. struct intel_engine_cs *ring,
  9563. uint32_t flags)
  9564. {
  9565. return -ENODEV;
  9566. }
  9567. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9568. struct drm_crtc *crtc)
  9569. {
  9570. struct drm_i915_private *dev_priv = dev->dev_private;
  9571. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9572. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9573. u32 addr;
  9574. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9575. return true;
  9576. if (!work->enable_stall_check)
  9577. return false;
  9578. if (work->flip_ready_vblank == 0) {
  9579. if (work->flip_queued_req &&
  9580. !i915_gem_request_completed(work->flip_queued_req, true))
  9581. return false;
  9582. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9583. }
  9584. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9585. return false;
  9586. /* Potential stall - if we see that the flip has happened,
  9587. * assume a missed interrupt. */
  9588. if (INTEL_INFO(dev)->gen >= 4)
  9589. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9590. else
  9591. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9592. /* There is a potential issue here with a false positive after a flip
  9593. * to the same address. We could address this by checking for a
  9594. * non-incrementing frame counter.
  9595. */
  9596. return addr == work->gtt_offset;
  9597. }
  9598. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9599. {
  9600. struct drm_i915_private *dev_priv = dev->dev_private;
  9601. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9602. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9603. struct intel_unpin_work *work;
  9604. WARN_ON(!in_interrupt());
  9605. if (crtc == NULL)
  9606. return;
  9607. spin_lock(&dev->event_lock);
  9608. work = intel_crtc->unpin_work;
  9609. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9610. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9611. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9612. page_flip_completed(intel_crtc);
  9613. work = NULL;
  9614. }
  9615. if (work != NULL &&
  9616. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9617. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9618. spin_unlock(&dev->event_lock);
  9619. }
  9620. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9621. struct drm_framebuffer *fb,
  9622. struct drm_pending_vblank_event *event,
  9623. uint32_t page_flip_flags)
  9624. {
  9625. struct drm_device *dev = crtc->dev;
  9626. struct drm_i915_private *dev_priv = dev->dev_private;
  9627. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9628. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9629. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9630. struct drm_plane *primary = crtc->primary;
  9631. enum pipe pipe = intel_crtc->pipe;
  9632. struct intel_unpin_work *work;
  9633. struct intel_engine_cs *ring;
  9634. bool mmio_flip;
  9635. int ret;
  9636. /*
  9637. * drm_mode_page_flip_ioctl() should already catch this, but double
  9638. * check to be safe. In the future we may enable pageflipping from
  9639. * a disabled primary plane.
  9640. */
  9641. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9642. return -EBUSY;
  9643. /* Can't change pixel format via MI display flips. */
  9644. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9645. return -EINVAL;
  9646. /*
  9647. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9648. * Note that pitch changes could also affect these register.
  9649. */
  9650. if (INTEL_INFO(dev)->gen > 3 &&
  9651. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9652. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9653. return -EINVAL;
  9654. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9655. goto out_hang;
  9656. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9657. if (work == NULL)
  9658. return -ENOMEM;
  9659. work->event = event;
  9660. work->crtc = crtc;
  9661. work->old_fb = old_fb;
  9662. INIT_WORK(&work->work, intel_unpin_work_fn);
  9663. ret = drm_crtc_vblank_get(crtc);
  9664. if (ret)
  9665. goto free_work;
  9666. /* We borrow the event spin lock for protecting unpin_work */
  9667. spin_lock_irq(&dev->event_lock);
  9668. if (intel_crtc->unpin_work) {
  9669. /* Before declaring the flip queue wedged, check if
  9670. * the hardware completed the operation behind our backs.
  9671. */
  9672. if (__intel_pageflip_stall_check(dev, crtc)) {
  9673. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9674. page_flip_completed(intel_crtc);
  9675. } else {
  9676. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9677. spin_unlock_irq(&dev->event_lock);
  9678. drm_crtc_vblank_put(crtc);
  9679. kfree(work);
  9680. return -EBUSY;
  9681. }
  9682. }
  9683. intel_crtc->unpin_work = work;
  9684. spin_unlock_irq(&dev->event_lock);
  9685. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9686. flush_workqueue(dev_priv->wq);
  9687. /* Reference the objects for the scheduled work. */
  9688. drm_framebuffer_reference(work->old_fb);
  9689. drm_gem_object_reference(&obj->base);
  9690. crtc->primary->fb = fb;
  9691. update_state_fb(crtc->primary);
  9692. work->pending_flip_obj = obj;
  9693. ret = i915_mutex_lock_interruptible(dev);
  9694. if (ret)
  9695. goto cleanup;
  9696. atomic_inc(&intel_crtc->unpin_work_count);
  9697. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9698. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9699. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9700. if (IS_VALLEYVIEW(dev)) {
  9701. ring = &dev_priv->ring[BCS];
  9702. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9703. /* vlv: DISPLAY_FLIP fails to change tiling */
  9704. ring = NULL;
  9705. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9706. ring = &dev_priv->ring[BCS];
  9707. } else if (INTEL_INFO(dev)->gen >= 7) {
  9708. ring = i915_gem_request_get_ring(obj->last_write_req);
  9709. if (ring == NULL || ring->id != RCS)
  9710. ring = &dev_priv->ring[BCS];
  9711. } else {
  9712. ring = &dev_priv->ring[RCS];
  9713. }
  9714. mmio_flip = use_mmio_flip(ring, obj);
  9715. /* When using CS flips, we want to emit semaphores between rings.
  9716. * However, when using mmio flips we will create a task to do the
  9717. * synchronisation, so all we want here is to pin the framebuffer
  9718. * into the display plane and skip any waits.
  9719. */
  9720. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9721. crtc->primary->state,
  9722. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9723. if (ret)
  9724. goto cleanup_pending;
  9725. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9726. + intel_crtc->dspaddr_offset;
  9727. if (mmio_flip) {
  9728. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9729. page_flip_flags);
  9730. if (ret)
  9731. goto cleanup_unpin;
  9732. i915_gem_request_assign(&work->flip_queued_req,
  9733. obj->last_write_req);
  9734. } else {
  9735. if (obj->last_write_req) {
  9736. ret = i915_gem_check_olr(obj->last_write_req);
  9737. if (ret)
  9738. goto cleanup_unpin;
  9739. }
  9740. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9741. page_flip_flags);
  9742. if (ret)
  9743. goto cleanup_unpin;
  9744. i915_gem_request_assign(&work->flip_queued_req,
  9745. intel_ring_get_request(ring));
  9746. }
  9747. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9748. work->enable_stall_check = true;
  9749. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9750. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9751. intel_fbc_disable(dev);
  9752. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9753. mutex_unlock(&dev->struct_mutex);
  9754. trace_i915_flip_request(intel_crtc->plane, obj);
  9755. return 0;
  9756. cleanup_unpin:
  9757. intel_unpin_fb_obj(fb, crtc->primary->state);
  9758. cleanup_pending:
  9759. atomic_dec(&intel_crtc->unpin_work_count);
  9760. mutex_unlock(&dev->struct_mutex);
  9761. cleanup:
  9762. crtc->primary->fb = old_fb;
  9763. update_state_fb(crtc->primary);
  9764. drm_gem_object_unreference_unlocked(&obj->base);
  9765. drm_framebuffer_unreference(work->old_fb);
  9766. spin_lock_irq(&dev->event_lock);
  9767. intel_crtc->unpin_work = NULL;
  9768. spin_unlock_irq(&dev->event_lock);
  9769. drm_crtc_vblank_put(crtc);
  9770. free_work:
  9771. kfree(work);
  9772. if (ret == -EIO) {
  9773. struct drm_atomic_state *state;
  9774. struct drm_plane_state *plane_state;
  9775. out_hang:
  9776. state = drm_atomic_state_alloc(dev);
  9777. if (!state)
  9778. return -ENOMEM;
  9779. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9780. retry:
  9781. plane_state = drm_atomic_get_plane_state(state, primary);
  9782. ret = PTR_ERR_OR_ZERO(plane_state);
  9783. if (!ret) {
  9784. drm_atomic_set_fb_for_plane(plane_state, fb);
  9785. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9786. if (!ret)
  9787. ret = drm_atomic_commit(state);
  9788. }
  9789. if (ret == -EDEADLK) {
  9790. drm_modeset_backoff(state->acquire_ctx);
  9791. drm_atomic_state_clear(state);
  9792. goto retry;
  9793. }
  9794. if (ret)
  9795. drm_atomic_state_free(state);
  9796. if (ret == 0 && event) {
  9797. spin_lock_irq(&dev->event_lock);
  9798. drm_send_vblank_event(dev, pipe, event);
  9799. spin_unlock_irq(&dev->event_lock);
  9800. }
  9801. }
  9802. return ret;
  9803. }
  9804. static bool encoders_cloneable(const struct intel_encoder *a,
  9805. const struct intel_encoder *b)
  9806. {
  9807. /* masks could be asymmetric, so check both ways */
  9808. return a == b || (a->cloneable & (1 << b->type) &&
  9809. b->cloneable & (1 << a->type));
  9810. }
  9811. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9812. struct intel_crtc *crtc,
  9813. struct intel_encoder *encoder)
  9814. {
  9815. struct intel_encoder *source_encoder;
  9816. struct drm_connector *connector;
  9817. struct drm_connector_state *connector_state;
  9818. int i;
  9819. for_each_connector_in_state(state, connector, connector_state, i) {
  9820. if (connector_state->crtc != &crtc->base)
  9821. continue;
  9822. source_encoder =
  9823. to_intel_encoder(connector_state->best_encoder);
  9824. if (!encoders_cloneable(encoder, source_encoder))
  9825. return false;
  9826. }
  9827. return true;
  9828. }
  9829. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9830. struct intel_crtc *crtc)
  9831. {
  9832. struct intel_encoder *encoder;
  9833. struct drm_connector *connector;
  9834. struct drm_connector_state *connector_state;
  9835. int i;
  9836. for_each_connector_in_state(state, connector, connector_state, i) {
  9837. if (connector_state->crtc != &crtc->base)
  9838. continue;
  9839. encoder = to_intel_encoder(connector_state->best_encoder);
  9840. if (!check_single_encoder_cloning(state, crtc, encoder))
  9841. return false;
  9842. }
  9843. return true;
  9844. }
  9845. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9846. struct drm_crtc_state *crtc_state)
  9847. {
  9848. struct drm_device *dev = crtc->dev;
  9849. struct drm_i915_private *dev_priv = dev->dev_private;
  9850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9851. struct intel_crtc_state *pipe_config =
  9852. to_intel_crtc_state(crtc_state);
  9853. struct drm_atomic_state *state = crtc_state->state;
  9854. int ret, idx = crtc->base.id;
  9855. bool mode_changed = needs_modeset(crtc_state);
  9856. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9857. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9858. return -EINVAL;
  9859. }
  9860. I915_STATE_WARN(crtc->state->active != intel_crtc->active,
  9861. "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
  9862. idx, crtc->state->active, intel_crtc->active);
  9863. if (mode_changed && crtc_state->enable &&
  9864. dev_priv->display.crtc_compute_clock &&
  9865. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9866. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9867. pipe_config);
  9868. if (ret)
  9869. return ret;
  9870. }
  9871. return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
  9872. }
  9873. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9874. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9875. .load_lut = intel_crtc_load_lut,
  9876. .atomic_begin = intel_begin_crtc_commit,
  9877. .atomic_flush = intel_finish_crtc_commit,
  9878. .atomic_check = intel_crtc_atomic_check,
  9879. };
  9880. /**
  9881. * intel_modeset_update_staged_output_state
  9882. *
  9883. * Updates the staged output configuration state, e.g. after we've read out the
  9884. * current hw state.
  9885. */
  9886. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  9887. {
  9888. struct intel_crtc *crtc;
  9889. struct intel_encoder *encoder;
  9890. struct intel_connector *connector;
  9891. for_each_intel_connector(dev, connector) {
  9892. connector->new_encoder =
  9893. to_intel_encoder(connector->base.encoder);
  9894. }
  9895. for_each_intel_encoder(dev, encoder) {
  9896. encoder->new_crtc =
  9897. to_intel_crtc(encoder->base.crtc);
  9898. }
  9899. for_each_intel_crtc(dev, crtc) {
  9900. crtc->new_enabled = crtc->base.state->enable;
  9901. }
  9902. }
  9903. /* Transitional helper to copy current connector/encoder state to
  9904. * connector->state. This is needed so that code that is partially
  9905. * converted to atomic does the right thing.
  9906. */
  9907. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9908. {
  9909. struct intel_connector *connector;
  9910. for_each_intel_connector(dev, connector) {
  9911. if (connector->base.encoder) {
  9912. connector->base.state->best_encoder =
  9913. connector->base.encoder;
  9914. connector->base.state->crtc =
  9915. connector->base.encoder->crtc;
  9916. } else {
  9917. connector->base.state->best_encoder = NULL;
  9918. connector->base.state->crtc = NULL;
  9919. }
  9920. }
  9921. }
  9922. static void
  9923. connected_sink_compute_bpp(struct intel_connector *connector,
  9924. struct intel_crtc_state *pipe_config)
  9925. {
  9926. int bpp = pipe_config->pipe_bpp;
  9927. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9928. connector->base.base.id,
  9929. connector->base.name);
  9930. /* Don't use an invalid EDID bpc value */
  9931. if (connector->base.display_info.bpc &&
  9932. connector->base.display_info.bpc * 3 < bpp) {
  9933. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9934. bpp, connector->base.display_info.bpc*3);
  9935. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9936. }
  9937. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9938. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  9939. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9940. bpp);
  9941. pipe_config->pipe_bpp = 24;
  9942. }
  9943. }
  9944. static int
  9945. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9946. struct intel_crtc_state *pipe_config)
  9947. {
  9948. struct drm_device *dev = crtc->base.dev;
  9949. struct drm_atomic_state *state;
  9950. struct drm_connector *connector;
  9951. struct drm_connector_state *connector_state;
  9952. int bpp, i;
  9953. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  9954. bpp = 10*3;
  9955. else if (INTEL_INFO(dev)->gen >= 5)
  9956. bpp = 12*3;
  9957. else
  9958. bpp = 8*3;
  9959. pipe_config->pipe_bpp = bpp;
  9960. state = pipe_config->base.state;
  9961. /* Clamp display bpp to EDID value */
  9962. for_each_connector_in_state(state, connector, connector_state, i) {
  9963. if (connector_state->crtc != &crtc->base)
  9964. continue;
  9965. connected_sink_compute_bpp(to_intel_connector(connector),
  9966. pipe_config);
  9967. }
  9968. return bpp;
  9969. }
  9970. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9971. {
  9972. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9973. "type: 0x%x flags: 0x%x\n",
  9974. mode->crtc_clock,
  9975. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9976. mode->crtc_hsync_end, mode->crtc_htotal,
  9977. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9978. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9979. }
  9980. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9981. struct intel_crtc_state *pipe_config,
  9982. const char *context)
  9983. {
  9984. struct drm_device *dev = crtc->base.dev;
  9985. struct drm_plane *plane;
  9986. struct intel_plane *intel_plane;
  9987. struct intel_plane_state *state;
  9988. struct drm_framebuffer *fb;
  9989. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9990. context, pipe_config, pipe_name(crtc->pipe));
  9991. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  9992. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9993. pipe_config->pipe_bpp, pipe_config->dither);
  9994. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9995. pipe_config->has_pch_encoder,
  9996. pipe_config->fdi_lanes,
  9997. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9998. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9999. pipe_config->fdi_m_n.tu);
  10000. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10001. pipe_config->has_dp_encoder,
  10002. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10003. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10004. pipe_config->dp_m_n.tu);
  10005. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10006. pipe_config->has_dp_encoder,
  10007. pipe_config->dp_m2_n2.gmch_m,
  10008. pipe_config->dp_m2_n2.gmch_n,
  10009. pipe_config->dp_m2_n2.link_m,
  10010. pipe_config->dp_m2_n2.link_n,
  10011. pipe_config->dp_m2_n2.tu);
  10012. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10013. pipe_config->has_audio,
  10014. pipe_config->has_infoframe);
  10015. DRM_DEBUG_KMS("requested mode:\n");
  10016. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10017. DRM_DEBUG_KMS("adjusted mode:\n");
  10018. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10019. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10020. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10021. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10022. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10023. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10024. crtc->num_scalers,
  10025. pipe_config->scaler_state.scaler_users,
  10026. pipe_config->scaler_state.scaler_id);
  10027. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10028. pipe_config->gmch_pfit.control,
  10029. pipe_config->gmch_pfit.pgm_ratios,
  10030. pipe_config->gmch_pfit.lvds_border_bits);
  10031. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10032. pipe_config->pch_pfit.pos,
  10033. pipe_config->pch_pfit.size,
  10034. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10035. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10036. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10037. if (IS_BROXTON(dev)) {
  10038. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  10039. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10040. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  10041. pipe_config->ddi_pll_sel,
  10042. pipe_config->dpll_hw_state.ebb0,
  10043. pipe_config->dpll_hw_state.pll0,
  10044. pipe_config->dpll_hw_state.pll1,
  10045. pipe_config->dpll_hw_state.pll2,
  10046. pipe_config->dpll_hw_state.pll3,
  10047. pipe_config->dpll_hw_state.pll6,
  10048. pipe_config->dpll_hw_state.pll8,
  10049. pipe_config->dpll_hw_state.pcsdw12);
  10050. } else if (IS_SKYLAKE(dev)) {
  10051. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10052. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10053. pipe_config->ddi_pll_sel,
  10054. pipe_config->dpll_hw_state.ctrl1,
  10055. pipe_config->dpll_hw_state.cfgcr1,
  10056. pipe_config->dpll_hw_state.cfgcr2);
  10057. } else if (HAS_DDI(dev)) {
  10058. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10059. pipe_config->ddi_pll_sel,
  10060. pipe_config->dpll_hw_state.wrpll);
  10061. } else {
  10062. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10063. "fp0: 0x%x, fp1: 0x%x\n",
  10064. pipe_config->dpll_hw_state.dpll,
  10065. pipe_config->dpll_hw_state.dpll_md,
  10066. pipe_config->dpll_hw_state.fp0,
  10067. pipe_config->dpll_hw_state.fp1);
  10068. }
  10069. DRM_DEBUG_KMS("planes on this crtc\n");
  10070. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10071. intel_plane = to_intel_plane(plane);
  10072. if (intel_plane->pipe != crtc->pipe)
  10073. continue;
  10074. state = to_intel_plane_state(plane->state);
  10075. fb = state->base.fb;
  10076. if (!fb) {
  10077. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10078. "disabled, scaler_id = %d\n",
  10079. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10080. plane->base.id, intel_plane->pipe,
  10081. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10082. drm_plane_index(plane), state->scaler_id);
  10083. continue;
  10084. }
  10085. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10086. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10087. plane->base.id, intel_plane->pipe,
  10088. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10089. drm_plane_index(plane));
  10090. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10091. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10092. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10093. state->scaler_id,
  10094. state->src.x1 >> 16, state->src.y1 >> 16,
  10095. drm_rect_width(&state->src) >> 16,
  10096. drm_rect_height(&state->src) >> 16,
  10097. state->dst.x1, state->dst.y1,
  10098. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10099. }
  10100. }
  10101. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10102. {
  10103. struct drm_device *dev = state->dev;
  10104. struct intel_encoder *encoder;
  10105. struct drm_connector *connector;
  10106. struct drm_connector_state *connector_state;
  10107. unsigned int used_ports = 0;
  10108. int i;
  10109. /*
  10110. * Walk the connector list instead of the encoder
  10111. * list to detect the problem on ddi platforms
  10112. * where there's just one encoder per digital port.
  10113. */
  10114. for_each_connector_in_state(state, connector, connector_state, i) {
  10115. if (!connector_state->best_encoder)
  10116. continue;
  10117. encoder = to_intel_encoder(connector_state->best_encoder);
  10118. WARN_ON(!connector_state->crtc);
  10119. switch (encoder->type) {
  10120. unsigned int port_mask;
  10121. case INTEL_OUTPUT_UNKNOWN:
  10122. if (WARN_ON(!HAS_DDI(dev)))
  10123. break;
  10124. case INTEL_OUTPUT_DISPLAYPORT:
  10125. case INTEL_OUTPUT_HDMI:
  10126. case INTEL_OUTPUT_EDP:
  10127. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10128. /* the same port mustn't appear more than once */
  10129. if (used_ports & port_mask)
  10130. return false;
  10131. used_ports |= port_mask;
  10132. default:
  10133. break;
  10134. }
  10135. }
  10136. return true;
  10137. }
  10138. static void
  10139. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10140. {
  10141. struct drm_crtc_state tmp_state;
  10142. struct intel_crtc_scaler_state scaler_state;
  10143. struct intel_dpll_hw_state dpll_hw_state;
  10144. enum intel_dpll_id shared_dpll;
  10145. uint32_t ddi_pll_sel;
  10146. /* FIXME: before the switch to atomic started, a new pipe_config was
  10147. * kzalloc'd. Code that depends on any field being zero should be
  10148. * fixed, so that the crtc_state can be safely duplicated. For now,
  10149. * only fields that are know to not cause problems are preserved. */
  10150. tmp_state = crtc_state->base;
  10151. scaler_state = crtc_state->scaler_state;
  10152. shared_dpll = crtc_state->shared_dpll;
  10153. dpll_hw_state = crtc_state->dpll_hw_state;
  10154. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10155. memset(crtc_state, 0, sizeof *crtc_state);
  10156. crtc_state->base = tmp_state;
  10157. crtc_state->scaler_state = scaler_state;
  10158. crtc_state->shared_dpll = shared_dpll;
  10159. crtc_state->dpll_hw_state = dpll_hw_state;
  10160. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10161. }
  10162. static int
  10163. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10164. struct intel_crtc_state *pipe_config)
  10165. {
  10166. struct drm_atomic_state *state = pipe_config->base.state;
  10167. struct intel_encoder *encoder;
  10168. struct drm_connector *connector;
  10169. struct drm_connector_state *connector_state;
  10170. int base_bpp, ret = -EINVAL;
  10171. int i;
  10172. bool retry = true;
  10173. clear_intel_crtc_state(pipe_config);
  10174. pipe_config->cpu_transcoder =
  10175. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10176. /*
  10177. * Sanitize sync polarity flags based on requested ones. If neither
  10178. * positive or negative polarity is requested, treat this as meaning
  10179. * negative polarity.
  10180. */
  10181. if (!(pipe_config->base.adjusted_mode.flags &
  10182. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10183. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10184. if (!(pipe_config->base.adjusted_mode.flags &
  10185. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10186. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10187. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10188. * plane pixel format and any sink constraints into account. Returns the
  10189. * source plane bpp so that dithering can be selected on mismatches
  10190. * after encoders and crtc also have had their say. */
  10191. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10192. pipe_config);
  10193. if (base_bpp < 0)
  10194. goto fail;
  10195. /*
  10196. * Determine the real pipe dimensions. Note that stereo modes can
  10197. * increase the actual pipe size due to the frame doubling and
  10198. * insertion of additional space for blanks between the frame. This
  10199. * is stored in the crtc timings. We use the requested mode to do this
  10200. * computation to clearly distinguish it from the adjusted mode, which
  10201. * can be changed by the connectors in the below retry loop.
  10202. */
  10203. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10204. &pipe_config->pipe_src_w,
  10205. &pipe_config->pipe_src_h);
  10206. encoder_retry:
  10207. /* Ensure the port clock defaults are reset when retrying. */
  10208. pipe_config->port_clock = 0;
  10209. pipe_config->pixel_multiplier = 1;
  10210. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10211. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10212. CRTC_STEREO_DOUBLE);
  10213. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10214. * adjust it according to limitations or connector properties, and also
  10215. * a chance to reject the mode entirely.
  10216. */
  10217. for_each_connector_in_state(state, connector, connector_state, i) {
  10218. if (connector_state->crtc != crtc)
  10219. continue;
  10220. encoder = to_intel_encoder(connector_state->best_encoder);
  10221. if (!(encoder->compute_config(encoder, pipe_config))) {
  10222. DRM_DEBUG_KMS("Encoder config failure\n");
  10223. goto fail;
  10224. }
  10225. }
  10226. /* Set default port clock if not overwritten by the encoder. Needs to be
  10227. * done afterwards in case the encoder adjusts the mode. */
  10228. if (!pipe_config->port_clock)
  10229. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10230. * pipe_config->pixel_multiplier;
  10231. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10232. if (ret < 0) {
  10233. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10234. goto fail;
  10235. }
  10236. if (ret == RETRY) {
  10237. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10238. ret = -EINVAL;
  10239. goto fail;
  10240. }
  10241. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10242. retry = false;
  10243. goto encoder_retry;
  10244. }
  10245. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10246. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10247. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10248. /* Check if we need to force a modeset */
  10249. if (pipe_config->has_audio !=
  10250. to_intel_crtc_state(crtc->state)->has_audio) {
  10251. pipe_config->base.mode_changed = true;
  10252. ret = drm_atomic_add_affected_planes(state, crtc);
  10253. }
  10254. /*
  10255. * Note we have an issue here with infoframes: current code
  10256. * only updates them on the full mode set path per hw
  10257. * requirements. So here we should be checking for any
  10258. * required changes and forcing a mode set.
  10259. */
  10260. fail:
  10261. return ret;
  10262. }
  10263. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10264. {
  10265. struct drm_encoder *encoder;
  10266. struct drm_device *dev = crtc->dev;
  10267. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10268. if (encoder->crtc == crtc)
  10269. return true;
  10270. return false;
  10271. }
  10272. static void
  10273. intel_modeset_update_state(struct drm_atomic_state *state)
  10274. {
  10275. struct drm_device *dev = state->dev;
  10276. struct intel_encoder *intel_encoder;
  10277. struct drm_crtc *crtc;
  10278. struct drm_crtc_state *crtc_state;
  10279. struct drm_connector *connector;
  10280. intel_shared_dpll_commit(state);
  10281. for_each_intel_encoder(dev, intel_encoder) {
  10282. if (!intel_encoder->base.crtc)
  10283. continue;
  10284. crtc = intel_encoder->base.crtc;
  10285. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10286. if (!crtc_state || !needs_modeset(crtc->state))
  10287. continue;
  10288. intel_encoder->connectors_active = false;
  10289. }
  10290. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10291. intel_modeset_update_staged_output_state(state->dev);
  10292. /* Double check state. */
  10293. for_each_crtc(dev, crtc) {
  10294. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10295. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10296. /* Update hwmode for vblank functions */
  10297. if (crtc->state->active)
  10298. crtc->hwmode = crtc->state->adjusted_mode;
  10299. else
  10300. crtc->hwmode.crtc_clock = 0;
  10301. }
  10302. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10303. if (!connector->encoder || !connector->encoder->crtc)
  10304. continue;
  10305. crtc = connector->encoder->crtc;
  10306. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10307. if (!crtc_state || !needs_modeset(crtc->state))
  10308. continue;
  10309. if (crtc->state->active) {
  10310. struct drm_property *dpms_property =
  10311. dev->mode_config.dpms_property;
  10312. connector->dpms = DRM_MODE_DPMS_ON;
  10313. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10314. intel_encoder = to_intel_encoder(connector->encoder);
  10315. intel_encoder->connectors_active = true;
  10316. } else
  10317. connector->dpms = DRM_MODE_DPMS_OFF;
  10318. }
  10319. }
  10320. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10321. {
  10322. int diff;
  10323. if (clock1 == clock2)
  10324. return true;
  10325. if (!clock1 || !clock2)
  10326. return false;
  10327. diff = abs(clock1 - clock2);
  10328. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10329. return true;
  10330. return false;
  10331. }
  10332. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10333. list_for_each_entry((intel_crtc), \
  10334. &(dev)->mode_config.crtc_list, \
  10335. base.head) \
  10336. if (mask & (1 <<(intel_crtc)->pipe))
  10337. static bool
  10338. intel_pipe_config_compare(struct drm_device *dev,
  10339. struct intel_crtc_state *current_config,
  10340. struct intel_crtc_state *pipe_config)
  10341. {
  10342. #define PIPE_CONF_CHECK_X(name) \
  10343. if (current_config->name != pipe_config->name) { \
  10344. DRM_ERROR("mismatch in " #name " " \
  10345. "(expected 0x%08x, found 0x%08x)\n", \
  10346. current_config->name, \
  10347. pipe_config->name); \
  10348. return false; \
  10349. }
  10350. #define PIPE_CONF_CHECK_I(name) \
  10351. if (current_config->name != pipe_config->name) { \
  10352. DRM_ERROR("mismatch in " #name " " \
  10353. "(expected %i, found %i)\n", \
  10354. current_config->name, \
  10355. pipe_config->name); \
  10356. return false; \
  10357. }
  10358. /* This is required for BDW+ where there is only one set of registers for
  10359. * switching between high and low RR.
  10360. * This macro can be used whenever a comparison has to be made between one
  10361. * hw state and multiple sw state variables.
  10362. */
  10363. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10364. if ((current_config->name != pipe_config->name) && \
  10365. (current_config->alt_name != pipe_config->name)) { \
  10366. DRM_ERROR("mismatch in " #name " " \
  10367. "(expected %i or %i, found %i)\n", \
  10368. current_config->name, \
  10369. current_config->alt_name, \
  10370. pipe_config->name); \
  10371. return false; \
  10372. }
  10373. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10374. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10375. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10376. "(expected %i, found %i)\n", \
  10377. current_config->name & (mask), \
  10378. pipe_config->name & (mask)); \
  10379. return false; \
  10380. }
  10381. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10382. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10383. DRM_ERROR("mismatch in " #name " " \
  10384. "(expected %i, found %i)\n", \
  10385. current_config->name, \
  10386. pipe_config->name); \
  10387. return false; \
  10388. }
  10389. #define PIPE_CONF_QUIRK(quirk) \
  10390. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10391. PIPE_CONF_CHECK_I(cpu_transcoder);
  10392. PIPE_CONF_CHECK_I(has_pch_encoder);
  10393. PIPE_CONF_CHECK_I(fdi_lanes);
  10394. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10395. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10396. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10397. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10398. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10399. PIPE_CONF_CHECK_I(has_dp_encoder);
  10400. if (INTEL_INFO(dev)->gen < 8) {
  10401. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10402. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10403. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10404. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10405. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10406. if (current_config->has_drrs) {
  10407. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10408. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10409. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10410. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10411. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10412. }
  10413. } else {
  10414. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10415. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10416. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10417. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10418. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10419. }
  10420. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10421. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10422. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10423. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10424. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10425. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10426. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10427. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10428. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10429. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10430. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10431. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10432. PIPE_CONF_CHECK_I(pixel_multiplier);
  10433. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10434. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10435. IS_VALLEYVIEW(dev))
  10436. PIPE_CONF_CHECK_I(limited_color_range);
  10437. PIPE_CONF_CHECK_I(has_infoframe);
  10438. PIPE_CONF_CHECK_I(has_audio);
  10439. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10440. DRM_MODE_FLAG_INTERLACE);
  10441. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10442. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10443. DRM_MODE_FLAG_PHSYNC);
  10444. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10445. DRM_MODE_FLAG_NHSYNC);
  10446. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10447. DRM_MODE_FLAG_PVSYNC);
  10448. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10449. DRM_MODE_FLAG_NVSYNC);
  10450. }
  10451. PIPE_CONF_CHECK_I(pipe_src_w);
  10452. PIPE_CONF_CHECK_I(pipe_src_h);
  10453. /*
  10454. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10455. * screen. Since we don't yet re-compute the pipe config when moving
  10456. * just the lvds port away to another pipe the sw tracking won't match.
  10457. *
  10458. * Proper atomic modesets with recomputed global state will fix this.
  10459. * Until then just don't check gmch state for inherited modes.
  10460. */
  10461. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10462. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10463. /* pfit ratios are autocomputed by the hw on gen4+ */
  10464. if (INTEL_INFO(dev)->gen < 4)
  10465. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10466. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10467. }
  10468. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10469. if (current_config->pch_pfit.enabled) {
  10470. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10471. PIPE_CONF_CHECK_I(pch_pfit.size);
  10472. }
  10473. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10474. /* BDW+ don't expose a synchronous way to read the state */
  10475. if (IS_HASWELL(dev))
  10476. PIPE_CONF_CHECK_I(ips_enabled);
  10477. PIPE_CONF_CHECK_I(double_wide);
  10478. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10479. PIPE_CONF_CHECK_I(shared_dpll);
  10480. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10481. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10482. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10483. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10484. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10485. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10486. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10487. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10488. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10489. PIPE_CONF_CHECK_I(pipe_bpp);
  10490. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10491. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10492. #undef PIPE_CONF_CHECK_X
  10493. #undef PIPE_CONF_CHECK_I
  10494. #undef PIPE_CONF_CHECK_I_ALT
  10495. #undef PIPE_CONF_CHECK_FLAGS
  10496. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10497. #undef PIPE_CONF_QUIRK
  10498. return true;
  10499. }
  10500. static void check_wm_state(struct drm_device *dev)
  10501. {
  10502. struct drm_i915_private *dev_priv = dev->dev_private;
  10503. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10504. struct intel_crtc *intel_crtc;
  10505. int plane;
  10506. if (INTEL_INFO(dev)->gen < 9)
  10507. return;
  10508. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10509. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10510. for_each_intel_crtc(dev, intel_crtc) {
  10511. struct skl_ddb_entry *hw_entry, *sw_entry;
  10512. const enum pipe pipe = intel_crtc->pipe;
  10513. if (!intel_crtc->active)
  10514. continue;
  10515. /* planes */
  10516. for_each_plane(dev_priv, pipe, plane) {
  10517. hw_entry = &hw_ddb.plane[pipe][plane];
  10518. sw_entry = &sw_ddb->plane[pipe][plane];
  10519. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10520. continue;
  10521. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10522. "(expected (%u,%u), found (%u,%u))\n",
  10523. pipe_name(pipe), plane + 1,
  10524. sw_entry->start, sw_entry->end,
  10525. hw_entry->start, hw_entry->end);
  10526. }
  10527. /* cursor */
  10528. hw_entry = &hw_ddb.cursor[pipe];
  10529. sw_entry = &sw_ddb->cursor[pipe];
  10530. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10531. continue;
  10532. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10533. "(expected (%u,%u), found (%u,%u))\n",
  10534. pipe_name(pipe),
  10535. sw_entry->start, sw_entry->end,
  10536. hw_entry->start, hw_entry->end);
  10537. }
  10538. }
  10539. static void
  10540. check_connector_state(struct drm_device *dev)
  10541. {
  10542. struct intel_connector *connector;
  10543. for_each_intel_connector(dev, connector) {
  10544. /* This also checks the encoder/connector hw state with the
  10545. * ->get_hw_state callbacks. */
  10546. intel_connector_check_state(connector);
  10547. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10548. "connector's staged encoder doesn't match current encoder\n");
  10549. }
  10550. }
  10551. static void
  10552. check_encoder_state(struct drm_device *dev)
  10553. {
  10554. struct intel_encoder *encoder;
  10555. struct intel_connector *connector;
  10556. for_each_intel_encoder(dev, encoder) {
  10557. bool enabled = false;
  10558. bool active = false;
  10559. enum pipe pipe, tracked_pipe;
  10560. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10561. encoder->base.base.id,
  10562. encoder->base.name);
  10563. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10564. "encoder's stage crtc doesn't match current crtc\n");
  10565. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10566. "encoder's active_connectors set, but no crtc\n");
  10567. for_each_intel_connector(dev, connector) {
  10568. if (connector->base.encoder != &encoder->base)
  10569. continue;
  10570. enabled = true;
  10571. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10572. active = true;
  10573. }
  10574. /*
  10575. * for MST connectors if we unplug the connector is gone
  10576. * away but the encoder is still connected to a crtc
  10577. * until a modeset happens in response to the hotplug.
  10578. */
  10579. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10580. continue;
  10581. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10582. "encoder's enabled state mismatch "
  10583. "(expected %i, found %i)\n",
  10584. !!encoder->base.crtc, enabled);
  10585. I915_STATE_WARN(active && !encoder->base.crtc,
  10586. "active encoder with no crtc\n");
  10587. I915_STATE_WARN(encoder->connectors_active != active,
  10588. "encoder's computed active state doesn't match tracked active state "
  10589. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10590. active = encoder->get_hw_state(encoder, &pipe);
  10591. I915_STATE_WARN(active != encoder->connectors_active,
  10592. "encoder's hw state doesn't match sw tracking "
  10593. "(expected %i, found %i)\n",
  10594. encoder->connectors_active, active);
  10595. if (!encoder->base.crtc)
  10596. continue;
  10597. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10598. I915_STATE_WARN(active && pipe != tracked_pipe,
  10599. "active encoder's pipe doesn't match"
  10600. "(expected %i, found %i)\n",
  10601. tracked_pipe, pipe);
  10602. }
  10603. }
  10604. static void
  10605. check_crtc_state(struct drm_device *dev)
  10606. {
  10607. struct drm_i915_private *dev_priv = dev->dev_private;
  10608. struct intel_crtc *crtc;
  10609. struct intel_encoder *encoder;
  10610. struct intel_crtc_state pipe_config;
  10611. for_each_intel_crtc(dev, crtc) {
  10612. bool enabled = false;
  10613. bool active = false;
  10614. memset(&pipe_config, 0, sizeof(pipe_config));
  10615. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10616. crtc->base.base.id);
  10617. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10618. "active crtc, but not enabled in sw tracking\n");
  10619. for_each_intel_encoder(dev, encoder) {
  10620. if (encoder->base.crtc != &crtc->base)
  10621. continue;
  10622. enabled = true;
  10623. if (encoder->connectors_active)
  10624. active = true;
  10625. }
  10626. I915_STATE_WARN(active != crtc->active,
  10627. "crtc's computed active state doesn't match tracked active state "
  10628. "(expected %i, found %i)\n", active, crtc->active);
  10629. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10630. "crtc's computed enabled state doesn't match tracked enabled state "
  10631. "(expected %i, found %i)\n", enabled,
  10632. crtc->base.state->enable);
  10633. active = dev_priv->display.get_pipe_config(crtc,
  10634. &pipe_config);
  10635. /* hw state is inconsistent with the pipe quirk */
  10636. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10637. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10638. active = crtc->active;
  10639. for_each_intel_encoder(dev, encoder) {
  10640. enum pipe pipe;
  10641. if (encoder->base.crtc != &crtc->base)
  10642. continue;
  10643. if (encoder->get_hw_state(encoder, &pipe))
  10644. encoder->get_config(encoder, &pipe_config);
  10645. }
  10646. I915_STATE_WARN(crtc->active != active,
  10647. "crtc active state doesn't match with hw state "
  10648. "(expected %i, found %i)\n", crtc->active, active);
  10649. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10650. "transitional active state does not match atomic hw state "
  10651. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10652. if (active &&
  10653. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10654. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10655. intel_dump_pipe_config(crtc, &pipe_config,
  10656. "[hw state]");
  10657. intel_dump_pipe_config(crtc, crtc->config,
  10658. "[sw state]");
  10659. }
  10660. }
  10661. }
  10662. static void
  10663. check_shared_dpll_state(struct drm_device *dev)
  10664. {
  10665. struct drm_i915_private *dev_priv = dev->dev_private;
  10666. struct intel_crtc *crtc;
  10667. struct intel_dpll_hw_state dpll_hw_state;
  10668. int i;
  10669. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10670. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10671. int enabled_crtcs = 0, active_crtcs = 0;
  10672. bool active;
  10673. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10674. DRM_DEBUG_KMS("%s\n", pll->name);
  10675. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10676. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10677. "more active pll users than references: %i vs %i\n",
  10678. pll->active, hweight32(pll->config.crtc_mask));
  10679. I915_STATE_WARN(pll->active && !pll->on,
  10680. "pll in active use but not on in sw tracking\n");
  10681. I915_STATE_WARN(pll->on && !pll->active,
  10682. "pll in on but not on in use in sw tracking\n");
  10683. I915_STATE_WARN(pll->on != active,
  10684. "pll on state mismatch (expected %i, found %i)\n",
  10685. pll->on, active);
  10686. for_each_intel_crtc(dev, crtc) {
  10687. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10688. enabled_crtcs++;
  10689. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10690. active_crtcs++;
  10691. }
  10692. I915_STATE_WARN(pll->active != active_crtcs,
  10693. "pll active crtcs mismatch (expected %i, found %i)\n",
  10694. pll->active, active_crtcs);
  10695. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10696. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10697. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10698. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10699. sizeof(dpll_hw_state)),
  10700. "pll hw state mismatch\n");
  10701. }
  10702. }
  10703. void
  10704. intel_modeset_check_state(struct drm_device *dev)
  10705. {
  10706. check_wm_state(dev);
  10707. check_connector_state(dev);
  10708. check_encoder_state(dev);
  10709. check_crtc_state(dev);
  10710. check_shared_dpll_state(dev);
  10711. }
  10712. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10713. int dotclock)
  10714. {
  10715. /*
  10716. * FDI already provided one idea for the dotclock.
  10717. * Yell if the encoder disagrees.
  10718. */
  10719. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10720. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10721. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10722. }
  10723. static void update_scanline_offset(struct intel_crtc *crtc)
  10724. {
  10725. struct drm_device *dev = crtc->base.dev;
  10726. /*
  10727. * The scanline counter increments at the leading edge of hsync.
  10728. *
  10729. * On most platforms it starts counting from vtotal-1 on the
  10730. * first active line. That means the scanline counter value is
  10731. * always one less than what we would expect. Ie. just after
  10732. * start of vblank, which also occurs at start of hsync (on the
  10733. * last active line), the scanline counter will read vblank_start-1.
  10734. *
  10735. * On gen2 the scanline counter starts counting from 1 instead
  10736. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10737. * to keep the value positive), instead of adding one.
  10738. *
  10739. * On HSW+ the behaviour of the scanline counter depends on the output
  10740. * type. For DP ports it behaves like most other platforms, but on HDMI
  10741. * there's an extra 1 line difference. So we need to add two instead of
  10742. * one to the value.
  10743. */
  10744. if (IS_GEN2(dev)) {
  10745. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10746. int vtotal;
  10747. vtotal = mode->crtc_vtotal;
  10748. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10749. vtotal /= 2;
  10750. crtc->scanline_offset = vtotal - 1;
  10751. } else if (HAS_DDI(dev) &&
  10752. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10753. crtc->scanline_offset = 2;
  10754. } else
  10755. crtc->scanline_offset = 1;
  10756. }
  10757. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10758. {
  10759. struct drm_device *dev = state->dev;
  10760. struct drm_i915_private *dev_priv = to_i915(dev);
  10761. struct intel_shared_dpll_config *shared_dpll = NULL;
  10762. struct intel_crtc *intel_crtc;
  10763. struct intel_crtc_state *intel_crtc_state;
  10764. struct drm_crtc *crtc;
  10765. struct drm_crtc_state *crtc_state;
  10766. int i;
  10767. if (!dev_priv->display.crtc_compute_clock)
  10768. return;
  10769. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10770. int dpll;
  10771. intel_crtc = to_intel_crtc(crtc);
  10772. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10773. dpll = intel_crtc_state->shared_dpll;
  10774. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10775. continue;
  10776. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10777. if (!shared_dpll)
  10778. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10779. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10780. }
  10781. }
  10782. /*
  10783. * This implements the workaround described in the "notes" section of the mode
  10784. * set sequence documentation. When going from no pipes or single pipe to
  10785. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10786. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10787. */
  10788. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10789. {
  10790. struct drm_crtc_state *crtc_state;
  10791. struct intel_crtc *intel_crtc;
  10792. struct drm_crtc *crtc;
  10793. struct intel_crtc_state *first_crtc_state = NULL;
  10794. struct intel_crtc_state *other_crtc_state = NULL;
  10795. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10796. int i;
  10797. /* look at all crtc's that are going to be enabled in during modeset */
  10798. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10799. intel_crtc = to_intel_crtc(crtc);
  10800. if (!crtc_state->active || !needs_modeset(crtc_state))
  10801. continue;
  10802. if (first_crtc_state) {
  10803. other_crtc_state = to_intel_crtc_state(crtc_state);
  10804. break;
  10805. } else {
  10806. first_crtc_state = to_intel_crtc_state(crtc_state);
  10807. first_pipe = intel_crtc->pipe;
  10808. }
  10809. }
  10810. /* No workaround needed? */
  10811. if (!first_crtc_state)
  10812. return 0;
  10813. /* w/a possibly needed, check how many crtc's are already enabled. */
  10814. for_each_intel_crtc(state->dev, intel_crtc) {
  10815. struct intel_crtc_state *pipe_config;
  10816. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10817. if (IS_ERR(pipe_config))
  10818. return PTR_ERR(pipe_config);
  10819. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10820. if (!pipe_config->base.active ||
  10821. needs_modeset(&pipe_config->base))
  10822. continue;
  10823. /* 2 or more enabled crtcs means no need for w/a */
  10824. if (enabled_pipe != INVALID_PIPE)
  10825. return 0;
  10826. enabled_pipe = intel_crtc->pipe;
  10827. }
  10828. if (enabled_pipe != INVALID_PIPE)
  10829. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10830. else if (other_crtc_state)
  10831. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10832. return 0;
  10833. }
  10834. /* Code that should eventually be part of atomic_check() */
  10835. static int intel_modeset_checks(struct drm_atomic_state *state)
  10836. {
  10837. struct drm_device *dev = state->dev;
  10838. int ret;
  10839. if (!check_digital_port_conflicts(state)) {
  10840. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10841. return -EINVAL;
  10842. }
  10843. /*
  10844. * See if the config requires any additional preparation, e.g.
  10845. * to adjust global state with pipes off. We need to do this
  10846. * here so we can get the modeset_pipe updated config for the new
  10847. * mode set on this crtc. For other crtcs we need to use the
  10848. * adjusted_mode bits in the crtc directly.
  10849. */
  10850. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
  10851. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
  10852. ret = valleyview_modeset_global_pipes(state);
  10853. else
  10854. ret = broadwell_modeset_global_pipes(state);
  10855. if (ret)
  10856. return ret;
  10857. }
  10858. intel_modeset_clear_plls(state);
  10859. if (IS_HASWELL(dev))
  10860. return haswell_mode_set_planes_workaround(state);
  10861. return 0;
  10862. }
  10863. static int
  10864. intel_modeset_compute_config(struct drm_atomic_state *state)
  10865. {
  10866. struct drm_crtc *crtc;
  10867. struct drm_crtc_state *crtc_state;
  10868. int ret, i;
  10869. ret = drm_atomic_helper_check_modeset(state->dev, state);
  10870. if (ret)
  10871. return ret;
  10872. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10873. if (!crtc_state->enable &&
  10874. WARN_ON(crtc_state->active))
  10875. crtc_state->active = false;
  10876. if (!crtc_state->enable)
  10877. continue;
  10878. if (!needs_modeset(crtc_state)) {
  10879. ret = drm_atomic_add_affected_connectors(state, crtc);
  10880. if (ret)
  10881. return ret;
  10882. }
  10883. ret = intel_modeset_pipe_config(crtc,
  10884. to_intel_crtc_state(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. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  11118. if (ret)
  11119. return ret;
  11120. if (set->num_connectors)
  11121. crtc_state->active = true;
  11122. return 0;
  11123. }
  11124. static int intel_crtc_set_config(struct drm_mode_set *set)
  11125. {
  11126. struct drm_device *dev;
  11127. struct drm_atomic_state *state = NULL;
  11128. int ret;
  11129. BUG_ON(!set);
  11130. BUG_ON(!set->crtc);
  11131. BUG_ON(!set->crtc->helper_private);
  11132. /* Enforce sane interface api - has been abused by the fb helper. */
  11133. BUG_ON(!set->mode && set->fb);
  11134. BUG_ON(set->fb && set->num_connectors == 0);
  11135. if (set->fb) {
  11136. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11137. set->crtc->base.id, set->fb->base.id,
  11138. (int)set->num_connectors, set->x, set->y);
  11139. } else {
  11140. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11141. }
  11142. dev = set->crtc->dev;
  11143. state = drm_atomic_state_alloc(dev);
  11144. if (!state)
  11145. return -ENOMEM;
  11146. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11147. ret = intel_modeset_stage_output_state(dev, set, state);
  11148. if (ret)
  11149. goto out;
  11150. ret = intel_modeset_compute_config(state);
  11151. if (ret)
  11152. goto out;
  11153. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11154. ret = intel_set_mode_checked(state);
  11155. if (ret) {
  11156. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11157. set->crtc->base.id, ret);
  11158. }
  11159. out:
  11160. if (ret)
  11161. drm_atomic_state_free(state);
  11162. return ret;
  11163. }
  11164. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11165. .gamma_set = intel_crtc_gamma_set,
  11166. .set_config = intel_crtc_set_config,
  11167. .destroy = intel_crtc_destroy,
  11168. .page_flip = intel_crtc_page_flip,
  11169. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11170. .atomic_destroy_state = intel_crtc_destroy_state,
  11171. };
  11172. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11173. struct intel_shared_dpll *pll,
  11174. struct intel_dpll_hw_state *hw_state)
  11175. {
  11176. uint32_t val;
  11177. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11178. return false;
  11179. val = I915_READ(PCH_DPLL(pll->id));
  11180. hw_state->dpll = val;
  11181. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11182. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11183. return val & DPLL_VCO_ENABLE;
  11184. }
  11185. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11186. struct intel_shared_dpll *pll)
  11187. {
  11188. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11189. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11190. }
  11191. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11192. struct intel_shared_dpll *pll)
  11193. {
  11194. /* PCH refclock must be enabled first */
  11195. ibx_assert_pch_refclk_enabled(dev_priv);
  11196. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11197. /* Wait for the clocks to stabilize. */
  11198. POSTING_READ(PCH_DPLL(pll->id));
  11199. udelay(150);
  11200. /* The pixel multiplier can only be updated once the
  11201. * DPLL is enabled and the clocks are stable.
  11202. *
  11203. * So write it again.
  11204. */
  11205. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11206. POSTING_READ(PCH_DPLL(pll->id));
  11207. udelay(200);
  11208. }
  11209. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11210. struct intel_shared_dpll *pll)
  11211. {
  11212. struct drm_device *dev = dev_priv->dev;
  11213. struct intel_crtc *crtc;
  11214. /* Make sure no transcoder isn't still depending on us. */
  11215. for_each_intel_crtc(dev, crtc) {
  11216. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11217. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11218. }
  11219. I915_WRITE(PCH_DPLL(pll->id), 0);
  11220. POSTING_READ(PCH_DPLL(pll->id));
  11221. udelay(200);
  11222. }
  11223. static char *ibx_pch_dpll_names[] = {
  11224. "PCH DPLL A",
  11225. "PCH DPLL B",
  11226. };
  11227. static void ibx_pch_dpll_init(struct drm_device *dev)
  11228. {
  11229. struct drm_i915_private *dev_priv = dev->dev_private;
  11230. int i;
  11231. dev_priv->num_shared_dpll = 2;
  11232. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11233. dev_priv->shared_dplls[i].id = i;
  11234. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11235. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11236. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11237. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11238. dev_priv->shared_dplls[i].get_hw_state =
  11239. ibx_pch_dpll_get_hw_state;
  11240. }
  11241. }
  11242. static void intel_shared_dpll_init(struct drm_device *dev)
  11243. {
  11244. struct drm_i915_private *dev_priv = dev->dev_private;
  11245. intel_update_cdclk(dev);
  11246. if (HAS_DDI(dev))
  11247. intel_ddi_pll_init(dev);
  11248. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11249. ibx_pch_dpll_init(dev);
  11250. else
  11251. dev_priv->num_shared_dpll = 0;
  11252. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11253. }
  11254. /**
  11255. * intel_wm_need_update - Check whether watermarks need updating
  11256. * @plane: drm plane
  11257. * @state: new plane state
  11258. *
  11259. * Check current plane state versus the new one to determine whether
  11260. * watermarks need to be recalculated.
  11261. *
  11262. * Returns true or false.
  11263. */
  11264. bool intel_wm_need_update(struct drm_plane *plane,
  11265. struct drm_plane_state *state)
  11266. {
  11267. /* Update watermarks on tiling changes. */
  11268. if (!plane->state->fb || !state->fb ||
  11269. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  11270. plane->state->rotation != state->rotation)
  11271. return true;
  11272. return false;
  11273. }
  11274. /**
  11275. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11276. * @plane: drm plane to prepare for
  11277. * @fb: framebuffer to prepare for presentation
  11278. *
  11279. * Prepares a framebuffer for usage on a display plane. Generally this
  11280. * involves pinning the underlying object and updating the frontbuffer tracking
  11281. * bits. Some older platforms need special physical address handling for
  11282. * cursor planes.
  11283. *
  11284. * Returns 0 on success, negative error code on failure.
  11285. */
  11286. int
  11287. intel_prepare_plane_fb(struct drm_plane *plane,
  11288. struct drm_framebuffer *fb,
  11289. const struct drm_plane_state *new_state)
  11290. {
  11291. struct drm_device *dev = plane->dev;
  11292. struct intel_plane *intel_plane = to_intel_plane(plane);
  11293. enum pipe pipe = intel_plane->pipe;
  11294. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11295. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11296. unsigned frontbuffer_bits = 0;
  11297. int ret = 0;
  11298. if (!obj)
  11299. return 0;
  11300. switch (plane->type) {
  11301. case DRM_PLANE_TYPE_PRIMARY:
  11302. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11303. break;
  11304. case DRM_PLANE_TYPE_CURSOR:
  11305. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11306. break;
  11307. case DRM_PLANE_TYPE_OVERLAY:
  11308. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11309. break;
  11310. }
  11311. mutex_lock(&dev->struct_mutex);
  11312. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11313. INTEL_INFO(dev)->cursor_needs_physical) {
  11314. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11315. ret = i915_gem_object_attach_phys(obj, align);
  11316. if (ret)
  11317. DRM_DEBUG_KMS("failed to attach phys object\n");
  11318. } else {
  11319. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11320. }
  11321. if (ret == 0)
  11322. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11323. mutex_unlock(&dev->struct_mutex);
  11324. return ret;
  11325. }
  11326. /**
  11327. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11328. * @plane: drm plane to clean up for
  11329. * @fb: old framebuffer that was on plane
  11330. *
  11331. * Cleans up a framebuffer that has just been removed from a plane.
  11332. */
  11333. void
  11334. intel_cleanup_plane_fb(struct drm_plane *plane,
  11335. struct drm_framebuffer *fb,
  11336. const struct drm_plane_state *old_state)
  11337. {
  11338. struct drm_device *dev = plane->dev;
  11339. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11340. if (WARN_ON(!obj))
  11341. return;
  11342. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11343. !INTEL_INFO(dev)->cursor_needs_physical) {
  11344. mutex_lock(&dev->struct_mutex);
  11345. intel_unpin_fb_obj(fb, old_state);
  11346. mutex_unlock(&dev->struct_mutex);
  11347. }
  11348. }
  11349. int
  11350. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11351. {
  11352. int max_scale;
  11353. struct drm_device *dev;
  11354. struct drm_i915_private *dev_priv;
  11355. int crtc_clock, cdclk;
  11356. if (!intel_crtc || !crtc_state)
  11357. return DRM_PLANE_HELPER_NO_SCALING;
  11358. dev = intel_crtc->base.dev;
  11359. dev_priv = dev->dev_private;
  11360. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11361. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11362. if (!crtc_clock || !cdclk)
  11363. return DRM_PLANE_HELPER_NO_SCALING;
  11364. /*
  11365. * skl max scale is lower of:
  11366. * close to 3 but not 3, -1 is for that purpose
  11367. * or
  11368. * cdclk/crtc_clock
  11369. */
  11370. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11371. return max_scale;
  11372. }
  11373. static int
  11374. intel_check_primary_plane(struct drm_plane *plane,
  11375. struct intel_plane_state *state)
  11376. {
  11377. struct drm_device *dev = plane->dev;
  11378. struct drm_i915_private *dev_priv = dev->dev_private;
  11379. struct drm_crtc *crtc = state->base.crtc;
  11380. struct intel_crtc *intel_crtc;
  11381. struct intel_crtc_state *crtc_state;
  11382. struct drm_framebuffer *fb = state->base.fb;
  11383. struct drm_rect *dest = &state->dst;
  11384. struct drm_rect *src = &state->src;
  11385. const struct drm_rect *clip = &state->clip;
  11386. bool can_position = false;
  11387. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11388. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11389. int ret;
  11390. crtc = crtc ? crtc : plane->crtc;
  11391. intel_crtc = to_intel_crtc(crtc);
  11392. crtc_state = state->base.state ?
  11393. intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
  11394. if (INTEL_INFO(dev)->gen >= 9) {
  11395. /* use scaler when colorkey is not required */
  11396. if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11397. min_scale = 1;
  11398. max_scale = skl_max_scale(intel_crtc, crtc_state);
  11399. }
  11400. can_position = true;
  11401. }
  11402. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11403. src, dest, clip,
  11404. min_scale,
  11405. max_scale,
  11406. can_position, true,
  11407. &state->visible);
  11408. if (ret)
  11409. return ret;
  11410. if (intel_crtc->active) {
  11411. struct intel_plane_state *old_state =
  11412. to_intel_plane_state(plane->state);
  11413. intel_crtc->atomic.wait_for_flips = true;
  11414. /*
  11415. * FBC does not work on some platforms for rotated
  11416. * planes, so disable it when rotation is not 0 and
  11417. * update it when rotation is set back to 0.
  11418. *
  11419. * FIXME: This is redundant with the fbc update done in
  11420. * the primary plane enable function except that that
  11421. * one is done too late. We eventually need to unify
  11422. * this.
  11423. */
  11424. if (state->visible &&
  11425. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  11426. dev_priv->fbc.crtc == intel_crtc &&
  11427. state->base.rotation != BIT(DRM_ROTATE_0)) {
  11428. intel_crtc->atomic.disable_fbc = true;
  11429. }
  11430. if (state->visible && !old_state->visible) {
  11431. /*
  11432. * BDW signals flip done immediately if the plane
  11433. * is disabled, even if the plane enable is already
  11434. * armed to occur at the next vblank :(
  11435. */
  11436. if (IS_BROADWELL(dev))
  11437. intel_crtc->atomic.wait_vblank = true;
  11438. if (crtc_state && !needs_modeset(&crtc_state->base))
  11439. intel_crtc->atomic.post_enable_primary = true;
  11440. }
  11441. if (!state->visible && old_state->visible &&
  11442. crtc_state && !needs_modeset(&crtc_state->base))
  11443. intel_crtc->atomic.pre_disable_primary = true;
  11444. intel_crtc->atomic.fb_bits |=
  11445. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  11446. intel_crtc->atomic.update_fbc = true;
  11447. if (intel_wm_need_update(plane, &state->base))
  11448. intel_crtc->atomic.update_wm = true;
  11449. }
  11450. if (INTEL_INFO(dev)->gen >= 9) {
  11451. ret = skl_update_scaler_plane(crtc_state,
  11452. to_intel_plane(plane),
  11453. state);
  11454. if (ret)
  11455. return ret;
  11456. }
  11457. return 0;
  11458. }
  11459. static void
  11460. intel_commit_primary_plane(struct drm_plane *plane,
  11461. struct intel_plane_state *state)
  11462. {
  11463. struct drm_crtc *crtc = state->base.crtc;
  11464. struct drm_framebuffer *fb = state->base.fb;
  11465. struct drm_device *dev = plane->dev;
  11466. struct drm_i915_private *dev_priv = dev->dev_private;
  11467. struct intel_crtc *intel_crtc;
  11468. struct drm_rect *src = &state->src;
  11469. crtc = crtc ? crtc : plane->crtc;
  11470. intel_crtc = to_intel_crtc(crtc);
  11471. plane->fb = fb;
  11472. crtc->x = src->x1 >> 16;
  11473. crtc->y = src->y1 >> 16;
  11474. if (intel_crtc->active) {
  11475. if (state->visible)
  11476. /* FIXME: kill this fastboot hack */
  11477. intel_update_pipe_size(intel_crtc);
  11478. dev_priv->display.update_primary_plane(crtc, plane->fb,
  11479. crtc->x, crtc->y);
  11480. }
  11481. }
  11482. static void
  11483. intel_disable_primary_plane(struct drm_plane *plane,
  11484. struct drm_crtc *crtc,
  11485. bool force)
  11486. {
  11487. struct drm_device *dev = plane->dev;
  11488. struct drm_i915_private *dev_priv = dev->dev_private;
  11489. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11490. }
  11491. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11492. {
  11493. struct drm_device *dev = crtc->dev;
  11494. struct drm_i915_private *dev_priv = dev->dev_private;
  11495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11496. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  11497. struct intel_plane *intel_plane;
  11498. struct drm_plane *p;
  11499. unsigned fb_bits = 0;
  11500. /* Track fb's for any planes being disabled */
  11501. list_for_each_entry(p, &dev->mode_config.plane_list, head) {
  11502. intel_plane = to_intel_plane(p);
  11503. if (intel_crtc->atomic.disabled_planes &
  11504. (1 << drm_plane_index(p))) {
  11505. switch (p->type) {
  11506. case DRM_PLANE_TYPE_PRIMARY:
  11507. fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
  11508. break;
  11509. case DRM_PLANE_TYPE_CURSOR:
  11510. fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
  11511. break;
  11512. case DRM_PLANE_TYPE_OVERLAY:
  11513. fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
  11514. break;
  11515. }
  11516. mutex_lock(&dev->struct_mutex);
  11517. i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
  11518. mutex_unlock(&dev->struct_mutex);
  11519. }
  11520. }
  11521. if (intel_crtc->atomic.wait_for_flips)
  11522. intel_crtc_wait_for_pending_flips(crtc);
  11523. if (intel_crtc->atomic.disable_fbc)
  11524. intel_fbc_disable(dev);
  11525. if (intel_crtc->atomic.pre_disable_primary)
  11526. intel_pre_disable_primary(crtc);
  11527. if (intel_crtc->atomic.update_wm)
  11528. intel_update_watermarks(crtc);
  11529. intel_runtime_pm_get(dev_priv);
  11530. /* Perform vblank evasion around commit operation */
  11531. if (crtc_state->active && !needs_modeset(crtc_state))
  11532. intel_crtc->atomic.evade =
  11533. intel_pipe_update_start(intel_crtc,
  11534. &intel_crtc->atomic.start_vbl_count);
  11535. }
  11536. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11537. {
  11538. struct drm_device *dev = crtc->dev;
  11539. struct drm_i915_private *dev_priv = dev->dev_private;
  11540. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11541. struct drm_plane *p;
  11542. if (intel_crtc->atomic.evade)
  11543. intel_pipe_update_end(intel_crtc,
  11544. intel_crtc->atomic.start_vbl_count);
  11545. intel_runtime_pm_put(dev_priv);
  11546. if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
  11547. intel_wait_for_vblank(dev, intel_crtc->pipe);
  11548. intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
  11549. if (intel_crtc->atomic.update_fbc) {
  11550. mutex_lock(&dev->struct_mutex);
  11551. intel_fbc_update(dev);
  11552. mutex_unlock(&dev->struct_mutex);
  11553. }
  11554. if (intel_crtc->atomic.post_enable_primary)
  11555. intel_post_enable_primary(crtc);
  11556. drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
  11557. if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
  11558. intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
  11559. false, false);
  11560. memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
  11561. }
  11562. /**
  11563. * intel_plane_destroy - destroy a plane
  11564. * @plane: plane to destroy
  11565. *
  11566. * Common destruction function for all types of planes (primary, cursor,
  11567. * sprite).
  11568. */
  11569. void intel_plane_destroy(struct drm_plane *plane)
  11570. {
  11571. struct intel_plane *intel_plane = to_intel_plane(plane);
  11572. drm_plane_cleanup(plane);
  11573. kfree(intel_plane);
  11574. }
  11575. const struct drm_plane_funcs intel_plane_funcs = {
  11576. .update_plane = drm_atomic_helper_update_plane,
  11577. .disable_plane = drm_atomic_helper_disable_plane,
  11578. .destroy = intel_plane_destroy,
  11579. .set_property = drm_atomic_helper_plane_set_property,
  11580. .atomic_get_property = intel_plane_atomic_get_property,
  11581. .atomic_set_property = intel_plane_atomic_set_property,
  11582. .atomic_duplicate_state = intel_plane_duplicate_state,
  11583. .atomic_destroy_state = intel_plane_destroy_state,
  11584. };
  11585. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11586. int pipe)
  11587. {
  11588. struct intel_plane *primary;
  11589. struct intel_plane_state *state;
  11590. const uint32_t *intel_primary_formats;
  11591. int num_formats;
  11592. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11593. if (primary == NULL)
  11594. return NULL;
  11595. state = intel_create_plane_state(&primary->base);
  11596. if (!state) {
  11597. kfree(primary);
  11598. return NULL;
  11599. }
  11600. primary->base.state = &state->base;
  11601. primary->can_scale = false;
  11602. primary->max_downscale = 1;
  11603. if (INTEL_INFO(dev)->gen >= 9) {
  11604. primary->can_scale = true;
  11605. state->scaler_id = -1;
  11606. }
  11607. primary->pipe = pipe;
  11608. primary->plane = pipe;
  11609. primary->check_plane = intel_check_primary_plane;
  11610. primary->commit_plane = intel_commit_primary_plane;
  11611. primary->disable_plane = intel_disable_primary_plane;
  11612. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11613. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11614. primary->plane = !pipe;
  11615. if (INTEL_INFO(dev)->gen >= 9) {
  11616. intel_primary_formats = skl_primary_formats;
  11617. num_formats = ARRAY_SIZE(skl_primary_formats);
  11618. } else if (INTEL_INFO(dev)->gen >= 4) {
  11619. intel_primary_formats = i965_primary_formats;
  11620. num_formats = ARRAY_SIZE(i965_primary_formats);
  11621. } else {
  11622. intel_primary_formats = i8xx_primary_formats;
  11623. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11624. }
  11625. drm_universal_plane_init(dev, &primary->base, 0,
  11626. &intel_plane_funcs,
  11627. intel_primary_formats, num_formats,
  11628. DRM_PLANE_TYPE_PRIMARY);
  11629. if (INTEL_INFO(dev)->gen >= 4)
  11630. intel_create_rotation_property(dev, primary);
  11631. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11632. return &primary->base;
  11633. }
  11634. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11635. {
  11636. if (!dev->mode_config.rotation_property) {
  11637. unsigned long flags = BIT(DRM_ROTATE_0) |
  11638. BIT(DRM_ROTATE_180);
  11639. if (INTEL_INFO(dev)->gen >= 9)
  11640. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11641. dev->mode_config.rotation_property =
  11642. drm_mode_create_rotation_property(dev, flags);
  11643. }
  11644. if (dev->mode_config.rotation_property)
  11645. drm_object_attach_property(&plane->base.base,
  11646. dev->mode_config.rotation_property,
  11647. plane->base.state->rotation);
  11648. }
  11649. static int
  11650. intel_check_cursor_plane(struct drm_plane *plane,
  11651. struct intel_plane_state *state)
  11652. {
  11653. struct drm_crtc *crtc = state->base.crtc;
  11654. struct drm_device *dev = plane->dev;
  11655. struct drm_framebuffer *fb = state->base.fb;
  11656. struct drm_rect *dest = &state->dst;
  11657. struct drm_rect *src = &state->src;
  11658. const struct drm_rect *clip = &state->clip;
  11659. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11660. struct intel_crtc *intel_crtc;
  11661. unsigned stride;
  11662. int ret;
  11663. crtc = crtc ? crtc : plane->crtc;
  11664. intel_crtc = to_intel_crtc(crtc);
  11665. ret = drm_plane_helper_check_update(plane, crtc, fb,
  11666. src, dest, clip,
  11667. DRM_PLANE_HELPER_NO_SCALING,
  11668. DRM_PLANE_HELPER_NO_SCALING,
  11669. true, true, &state->visible);
  11670. if (ret)
  11671. return ret;
  11672. /* if we want to turn off the cursor ignore width and height */
  11673. if (!obj)
  11674. goto finish;
  11675. /* Check for which cursor types we support */
  11676. if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
  11677. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11678. state->base.crtc_w, state->base.crtc_h);
  11679. return -EINVAL;
  11680. }
  11681. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11682. if (obj->base.size < stride * state->base.crtc_h) {
  11683. DRM_DEBUG_KMS("buffer is too small\n");
  11684. return -ENOMEM;
  11685. }
  11686. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11687. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11688. ret = -EINVAL;
  11689. }
  11690. finish:
  11691. if (intel_crtc->active) {
  11692. if (plane->state->crtc_w != state->base.crtc_w)
  11693. intel_crtc->atomic.update_wm = true;
  11694. intel_crtc->atomic.fb_bits |=
  11695. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  11696. }
  11697. return ret;
  11698. }
  11699. static void
  11700. intel_disable_cursor_plane(struct drm_plane *plane,
  11701. struct drm_crtc *crtc,
  11702. bool force)
  11703. {
  11704. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11705. if (!force) {
  11706. plane->fb = NULL;
  11707. intel_crtc->cursor_bo = NULL;
  11708. intel_crtc->cursor_addr = 0;
  11709. }
  11710. intel_crtc_update_cursor(crtc, false);
  11711. }
  11712. static void
  11713. intel_commit_cursor_plane(struct drm_plane *plane,
  11714. struct intel_plane_state *state)
  11715. {
  11716. struct drm_crtc *crtc = state->base.crtc;
  11717. struct drm_device *dev = plane->dev;
  11718. struct intel_crtc *intel_crtc;
  11719. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11720. uint32_t addr;
  11721. crtc = crtc ? crtc : plane->crtc;
  11722. intel_crtc = to_intel_crtc(crtc);
  11723. plane->fb = state->base.fb;
  11724. crtc->cursor_x = state->base.crtc_x;
  11725. crtc->cursor_y = state->base.crtc_y;
  11726. if (intel_crtc->cursor_bo == obj)
  11727. goto update;
  11728. if (!obj)
  11729. addr = 0;
  11730. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11731. addr = i915_gem_obj_ggtt_offset(obj);
  11732. else
  11733. addr = obj->phys_handle->busaddr;
  11734. intel_crtc->cursor_addr = addr;
  11735. intel_crtc->cursor_bo = obj;
  11736. update:
  11737. if (intel_crtc->active)
  11738. intel_crtc_update_cursor(crtc, state->visible);
  11739. }
  11740. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11741. int pipe)
  11742. {
  11743. struct intel_plane *cursor;
  11744. struct intel_plane_state *state;
  11745. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11746. if (cursor == NULL)
  11747. return NULL;
  11748. state = intel_create_plane_state(&cursor->base);
  11749. if (!state) {
  11750. kfree(cursor);
  11751. return NULL;
  11752. }
  11753. cursor->base.state = &state->base;
  11754. cursor->can_scale = false;
  11755. cursor->max_downscale = 1;
  11756. cursor->pipe = pipe;
  11757. cursor->plane = pipe;
  11758. cursor->check_plane = intel_check_cursor_plane;
  11759. cursor->commit_plane = intel_commit_cursor_plane;
  11760. cursor->disable_plane = intel_disable_cursor_plane;
  11761. drm_universal_plane_init(dev, &cursor->base, 0,
  11762. &intel_plane_funcs,
  11763. intel_cursor_formats,
  11764. ARRAY_SIZE(intel_cursor_formats),
  11765. DRM_PLANE_TYPE_CURSOR);
  11766. if (INTEL_INFO(dev)->gen >= 4) {
  11767. if (!dev->mode_config.rotation_property)
  11768. dev->mode_config.rotation_property =
  11769. drm_mode_create_rotation_property(dev,
  11770. BIT(DRM_ROTATE_0) |
  11771. BIT(DRM_ROTATE_180));
  11772. if (dev->mode_config.rotation_property)
  11773. drm_object_attach_property(&cursor->base.base,
  11774. dev->mode_config.rotation_property,
  11775. state->base.rotation);
  11776. }
  11777. if (INTEL_INFO(dev)->gen >=9)
  11778. state->scaler_id = -1;
  11779. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11780. return &cursor->base;
  11781. }
  11782. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11783. struct intel_crtc_state *crtc_state)
  11784. {
  11785. int i;
  11786. struct intel_scaler *intel_scaler;
  11787. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11788. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11789. intel_scaler = &scaler_state->scalers[i];
  11790. intel_scaler->in_use = 0;
  11791. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11792. }
  11793. scaler_state->scaler_id = -1;
  11794. }
  11795. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11796. {
  11797. struct drm_i915_private *dev_priv = dev->dev_private;
  11798. struct intel_crtc *intel_crtc;
  11799. struct intel_crtc_state *crtc_state = NULL;
  11800. struct drm_plane *primary = NULL;
  11801. struct drm_plane *cursor = NULL;
  11802. int i, ret;
  11803. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11804. if (intel_crtc == NULL)
  11805. return;
  11806. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11807. if (!crtc_state)
  11808. goto fail;
  11809. intel_crtc->config = crtc_state;
  11810. intel_crtc->base.state = &crtc_state->base;
  11811. crtc_state->base.crtc = &intel_crtc->base;
  11812. /* initialize shared scalers */
  11813. if (INTEL_INFO(dev)->gen >= 9) {
  11814. if (pipe == PIPE_C)
  11815. intel_crtc->num_scalers = 1;
  11816. else
  11817. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11818. skl_init_scalers(dev, intel_crtc, crtc_state);
  11819. }
  11820. primary = intel_primary_plane_create(dev, pipe);
  11821. if (!primary)
  11822. goto fail;
  11823. cursor = intel_cursor_plane_create(dev, pipe);
  11824. if (!cursor)
  11825. goto fail;
  11826. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11827. cursor, &intel_crtc_funcs);
  11828. if (ret)
  11829. goto fail;
  11830. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11831. for (i = 0; i < 256; i++) {
  11832. intel_crtc->lut_r[i] = i;
  11833. intel_crtc->lut_g[i] = i;
  11834. intel_crtc->lut_b[i] = i;
  11835. }
  11836. /*
  11837. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11838. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11839. */
  11840. intel_crtc->pipe = pipe;
  11841. intel_crtc->plane = pipe;
  11842. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11843. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11844. intel_crtc->plane = !pipe;
  11845. }
  11846. intel_crtc->cursor_base = ~0;
  11847. intel_crtc->cursor_cntl = ~0;
  11848. intel_crtc->cursor_size = ~0;
  11849. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11850. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11851. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11852. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11853. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11854. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11855. return;
  11856. fail:
  11857. if (primary)
  11858. drm_plane_cleanup(primary);
  11859. if (cursor)
  11860. drm_plane_cleanup(cursor);
  11861. kfree(crtc_state);
  11862. kfree(intel_crtc);
  11863. }
  11864. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11865. {
  11866. struct drm_encoder *encoder = connector->base.encoder;
  11867. struct drm_device *dev = connector->base.dev;
  11868. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11869. if (!encoder || WARN_ON(!encoder->crtc))
  11870. return INVALID_PIPE;
  11871. return to_intel_crtc(encoder->crtc)->pipe;
  11872. }
  11873. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11874. struct drm_file *file)
  11875. {
  11876. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11877. struct drm_crtc *drmmode_crtc;
  11878. struct intel_crtc *crtc;
  11879. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11880. if (!drmmode_crtc) {
  11881. DRM_ERROR("no such CRTC id\n");
  11882. return -ENOENT;
  11883. }
  11884. crtc = to_intel_crtc(drmmode_crtc);
  11885. pipe_from_crtc_id->pipe = crtc->pipe;
  11886. return 0;
  11887. }
  11888. static int intel_encoder_clones(struct intel_encoder *encoder)
  11889. {
  11890. struct drm_device *dev = encoder->base.dev;
  11891. struct intel_encoder *source_encoder;
  11892. int index_mask = 0;
  11893. int entry = 0;
  11894. for_each_intel_encoder(dev, source_encoder) {
  11895. if (encoders_cloneable(encoder, source_encoder))
  11896. index_mask |= (1 << entry);
  11897. entry++;
  11898. }
  11899. return index_mask;
  11900. }
  11901. static bool has_edp_a(struct drm_device *dev)
  11902. {
  11903. struct drm_i915_private *dev_priv = dev->dev_private;
  11904. if (!IS_MOBILE(dev))
  11905. return false;
  11906. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11907. return false;
  11908. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11909. return false;
  11910. return true;
  11911. }
  11912. static bool intel_crt_present(struct drm_device *dev)
  11913. {
  11914. struct drm_i915_private *dev_priv = dev->dev_private;
  11915. if (INTEL_INFO(dev)->gen >= 9)
  11916. return false;
  11917. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11918. return false;
  11919. if (IS_CHERRYVIEW(dev))
  11920. return false;
  11921. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11922. return false;
  11923. return true;
  11924. }
  11925. static void intel_setup_outputs(struct drm_device *dev)
  11926. {
  11927. struct drm_i915_private *dev_priv = dev->dev_private;
  11928. struct intel_encoder *encoder;
  11929. bool dpd_is_edp = false;
  11930. intel_lvds_init(dev);
  11931. if (intel_crt_present(dev))
  11932. intel_crt_init(dev);
  11933. if (IS_BROXTON(dev)) {
  11934. /*
  11935. * FIXME: Broxton doesn't support port detection via the
  11936. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11937. * detect the ports.
  11938. */
  11939. intel_ddi_init(dev, PORT_A);
  11940. intel_ddi_init(dev, PORT_B);
  11941. intel_ddi_init(dev, PORT_C);
  11942. } else if (HAS_DDI(dev)) {
  11943. int found;
  11944. /*
  11945. * Haswell uses DDI functions to detect digital outputs.
  11946. * On SKL pre-D0 the strap isn't connected, so we assume
  11947. * it's there.
  11948. */
  11949. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11950. /* WaIgnoreDDIAStrap: skl */
  11951. if (found ||
  11952. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11953. intel_ddi_init(dev, PORT_A);
  11954. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11955. * register */
  11956. found = I915_READ(SFUSE_STRAP);
  11957. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11958. intel_ddi_init(dev, PORT_B);
  11959. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11960. intel_ddi_init(dev, PORT_C);
  11961. if (found & SFUSE_STRAP_DDID_DETECTED)
  11962. intel_ddi_init(dev, PORT_D);
  11963. } else if (HAS_PCH_SPLIT(dev)) {
  11964. int found;
  11965. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11966. if (has_edp_a(dev))
  11967. intel_dp_init(dev, DP_A, PORT_A);
  11968. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11969. /* PCH SDVOB multiplex with HDMIB */
  11970. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  11971. if (!found)
  11972. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11973. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11974. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11975. }
  11976. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11977. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11978. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11979. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11980. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11981. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11982. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11983. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11984. } else if (IS_VALLEYVIEW(dev)) {
  11985. /*
  11986. * The DP_DETECTED bit is the latched state of the DDC
  11987. * SDA pin at boot. However since eDP doesn't require DDC
  11988. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11989. * eDP ports may have been muxed to an alternate function.
  11990. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11991. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11992. * detect eDP ports.
  11993. */
  11994. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  11995. !intel_dp_is_edp(dev, PORT_B))
  11996. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  11997. PORT_B);
  11998. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  11999. intel_dp_is_edp(dev, PORT_B))
  12000. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  12001. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  12002. !intel_dp_is_edp(dev, PORT_C))
  12003. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  12004. PORT_C);
  12005. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  12006. intel_dp_is_edp(dev, PORT_C))
  12007. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  12008. if (IS_CHERRYVIEW(dev)) {
  12009. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  12010. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  12011. PORT_D);
  12012. /* eDP not supported on port D, so don't check VBT */
  12013. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  12014. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  12015. }
  12016. intel_dsi_init(dev);
  12017. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  12018. bool found = false;
  12019. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12020. DRM_DEBUG_KMS("probing SDVOB\n");
  12021. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  12022. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  12023. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12024. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12025. }
  12026. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  12027. intel_dp_init(dev, DP_B, PORT_B);
  12028. }
  12029. /* Before G4X SDVOC doesn't have its own detect register */
  12030. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12031. DRM_DEBUG_KMS("probing SDVOC\n");
  12032. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  12033. }
  12034. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12035. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  12036. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12037. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12038. }
  12039. if (SUPPORTS_INTEGRATED_DP(dev))
  12040. intel_dp_init(dev, DP_C, PORT_C);
  12041. }
  12042. if (SUPPORTS_INTEGRATED_DP(dev) &&
  12043. (I915_READ(DP_D) & DP_DETECTED))
  12044. intel_dp_init(dev, DP_D, PORT_D);
  12045. } else if (IS_GEN2(dev))
  12046. intel_dvo_init(dev);
  12047. if (SUPPORTS_TV(dev))
  12048. intel_tv_init(dev);
  12049. intel_psr_init(dev);
  12050. for_each_intel_encoder(dev, encoder) {
  12051. encoder->base.possible_crtcs = encoder->crtc_mask;
  12052. encoder->base.possible_clones =
  12053. intel_encoder_clones(encoder);
  12054. }
  12055. intel_init_pch_refclk(dev);
  12056. drm_helper_move_panel_connectors_to_head(dev);
  12057. }
  12058. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12059. {
  12060. struct drm_device *dev = fb->dev;
  12061. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12062. drm_framebuffer_cleanup(fb);
  12063. mutex_lock(&dev->struct_mutex);
  12064. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12065. drm_gem_object_unreference(&intel_fb->obj->base);
  12066. mutex_unlock(&dev->struct_mutex);
  12067. kfree(intel_fb);
  12068. }
  12069. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12070. struct drm_file *file,
  12071. unsigned int *handle)
  12072. {
  12073. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12074. struct drm_i915_gem_object *obj = intel_fb->obj;
  12075. return drm_gem_handle_create(file, &obj->base, handle);
  12076. }
  12077. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12078. .destroy = intel_user_framebuffer_destroy,
  12079. .create_handle = intel_user_framebuffer_create_handle,
  12080. };
  12081. static
  12082. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12083. uint32_t pixel_format)
  12084. {
  12085. u32 gen = INTEL_INFO(dev)->gen;
  12086. if (gen >= 9) {
  12087. /* "The stride in bytes must not exceed the of the size of 8K
  12088. * pixels and 32K bytes."
  12089. */
  12090. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12091. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12092. return 32*1024;
  12093. } else if (gen >= 4) {
  12094. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12095. return 16*1024;
  12096. else
  12097. return 32*1024;
  12098. } else if (gen >= 3) {
  12099. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12100. return 8*1024;
  12101. else
  12102. return 16*1024;
  12103. } else {
  12104. /* XXX DSPC is limited to 4k tiled */
  12105. return 8*1024;
  12106. }
  12107. }
  12108. static int intel_framebuffer_init(struct drm_device *dev,
  12109. struct intel_framebuffer *intel_fb,
  12110. struct drm_mode_fb_cmd2 *mode_cmd,
  12111. struct drm_i915_gem_object *obj)
  12112. {
  12113. unsigned int aligned_height;
  12114. int ret;
  12115. u32 pitch_limit, stride_alignment;
  12116. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12117. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12118. /* Enforce that fb modifier and tiling mode match, but only for
  12119. * X-tiled. This is needed for FBC. */
  12120. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12121. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12122. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12123. return -EINVAL;
  12124. }
  12125. } else {
  12126. if (obj->tiling_mode == I915_TILING_X)
  12127. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12128. else if (obj->tiling_mode == I915_TILING_Y) {
  12129. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12130. return -EINVAL;
  12131. }
  12132. }
  12133. /* Passed in modifier sanity checking. */
  12134. switch (mode_cmd->modifier[0]) {
  12135. case I915_FORMAT_MOD_Y_TILED:
  12136. case I915_FORMAT_MOD_Yf_TILED:
  12137. if (INTEL_INFO(dev)->gen < 9) {
  12138. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12139. mode_cmd->modifier[0]);
  12140. return -EINVAL;
  12141. }
  12142. case DRM_FORMAT_MOD_NONE:
  12143. case I915_FORMAT_MOD_X_TILED:
  12144. break;
  12145. default:
  12146. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12147. mode_cmd->modifier[0]);
  12148. return -EINVAL;
  12149. }
  12150. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12151. mode_cmd->pixel_format);
  12152. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12153. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12154. mode_cmd->pitches[0], stride_alignment);
  12155. return -EINVAL;
  12156. }
  12157. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12158. mode_cmd->pixel_format);
  12159. if (mode_cmd->pitches[0] > pitch_limit) {
  12160. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12161. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12162. "tiled" : "linear",
  12163. mode_cmd->pitches[0], pitch_limit);
  12164. return -EINVAL;
  12165. }
  12166. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12167. mode_cmd->pitches[0] != obj->stride) {
  12168. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12169. mode_cmd->pitches[0], obj->stride);
  12170. return -EINVAL;
  12171. }
  12172. /* Reject formats not supported by any plane early. */
  12173. switch (mode_cmd->pixel_format) {
  12174. case DRM_FORMAT_C8:
  12175. case DRM_FORMAT_RGB565:
  12176. case DRM_FORMAT_XRGB8888:
  12177. case DRM_FORMAT_ARGB8888:
  12178. break;
  12179. case DRM_FORMAT_XRGB1555:
  12180. if (INTEL_INFO(dev)->gen > 3) {
  12181. DRM_DEBUG("unsupported pixel format: %s\n",
  12182. drm_get_format_name(mode_cmd->pixel_format));
  12183. return -EINVAL;
  12184. }
  12185. break;
  12186. case DRM_FORMAT_ABGR8888:
  12187. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12188. DRM_DEBUG("unsupported pixel format: %s\n",
  12189. drm_get_format_name(mode_cmd->pixel_format));
  12190. return -EINVAL;
  12191. }
  12192. break;
  12193. case DRM_FORMAT_XBGR8888:
  12194. case DRM_FORMAT_XRGB2101010:
  12195. case DRM_FORMAT_XBGR2101010:
  12196. if (INTEL_INFO(dev)->gen < 4) {
  12197. DRM_DEBUG("unsupported pixel format: %s\n",
  12198. drm_get_format_name(mode_cmd->pixel_format));
  12199. return -EINVAL;
  12200. }
  12201. break;
  12202. case DRM_FORMAT_ABGR2101010:
  12203. if (!IS_VALLEYVIEW(dev)) {
  12204. DRM_DEBUG("unsupported pixel format: %s\n",
  12205. drm_get_format_name(mode_cmd->pixel_format));
  12206. return -EINVAL;
  12207. }
  12208. break;
  12209. case DRM_FORMAT_YUYV:
  12210. case DRM_FORMAT_UYVY:
  12211. case DRM_FORMAT_YVYU:
  12212. case DRM_FORMAT_VYUY:
  12213. if (INTEL_INFO(dev)->gen < 5) {
  12214. DRM_DEBUG("unsupported pixel format: %s\n",
  12215. drm_get_format_name(mode_cmd->pixel_format));
  12216. return -EINVAL;
  12217. }
  12218. break;
  12219. default:
  12220. DRM_DEBUG("unsupported pixel format: %s\n",
  12221. drm_get_format_name(mode_cmd->pixel_format));
  12222. return -EINVAL;
  12223. }
  12224. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12225. if (mode_cmd->offsets[0] != 0)
  12226. return -EINVAL;
  12227. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12228. mode_cmd->pixel_format,
  12229. mode_cmd->modifier[0]);
  12230. /* FIXME drm helper for size checks (especially planar formats)? */
  12231. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12232. return -EINVAL;
  12233. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12234. intel_fb->obj = obj;
  12235. intel_fb->obj->framebuffer_references++;
  12236. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12237. if (ret) {
  12238. DRM_ERROR("framebuffer init failed %d\n", ret);
  12239. return ret;
  12240. }
  12241. return 0;
  12242. }
  12243. static struct drm_framebuffer *
  12244. intel_user_framebuffer_create(struct drm_device *dev,
  12245. struct drm_file *filp,
  12246. struct drm_mode_fb_cmd2 *mode_cmd)
  12247. {
  12248. struct drm_i915_gem_object *obj;
  12249. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12250. mode_cmd->handles[0]));
  12251. if (&obj->base == NULL)
  12252. return ERR_PTR(-ENOENT);
  12253. return intel_framebuffer_create(dev, mode_cmd, obj);
  12254. }
  12255. #ifndef CONFIG_DRM_I915_FBDEV
  12256. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12257. {
  12258. }
  12259. #endif
  12260. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12261. .fb_create = intel_user_framebuffer_create,
  12262. .output_poll_changed = intel_fbdev_output_poll_changed,
  12263. .atomic_check = intel_atomic_check,
  12264. .atomic_commit = intel_atomic_commit,
  12265. .atomic_state_alloc = intel_atomic_state_alloc,
  12266. .atomic_state_clear = intel_atomic_state_clear,
  12267. };
  12268. /* Set up chip specific display functions */
  12269. static void intel_init_display(struct drm_device *dev)
  12270. {
  12271. struct drm_i915_private *dev_priv = dev->dev_private;
  12272. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12273. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12274. else if (IS_CHERRYVIEW(dev))
  12275. dev_priv->display.find_dpll = chv_find_best_dpll;
  12276. else if (IS_VALLEYVIEW(dev))
  12277. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12278. else if (IS_PINEVIEW(dev))
  12279. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12280. else
  12281. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12282. if (INTEL_INFO(dev)->gen >= 9) {
  12283. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12284. dev_priv->display.get_initial_plane_config =
  12285. skylake_get_initial_plane_config;
  12286. dev_priv->display.crtc_compute_clock =
  12287. haswell_crtc_compute_clock;
  12288. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12289. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12290. dev_priv->display.update_primary_plane =
  12291. skylake_update_primary_plane;
  12292. } else if (HAS_DDI(dev)) {
  12293. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12294. dev_priv->display.get_initial_plane_config =
  12295. ironlake_get_initial_plane_config;
  12296. dev_priv->display.crtc_compute_clock =
  12297. haswell_crtc_compute_clock;
  12298. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12299. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12300. dev_priv->display.update_primary_plane =
  12301. ironlake_update_primary_plane;
  12302. } else if (HAS_PCH_SPLIT(dev)) {
  12303. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12304. dev_priv->display.get_initial_plane_config =
  12305. ironlake_get_initial_plane_config;
  12306. dev_priv->display.crtc_compute_clock =
  12307. ironlake_crtc_compute_clock;
  12308. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12309. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12310. dev_priv->display.update_primary_plane =
  12311. ironlake_update_primary_plane;
  12312. } else if (IS_VALLEYVIEW(dev)) {
  12313. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12314. dev_priv->display.get_initial_plane_config =
  12315. i9xx_get_initial_plane_config;
  12316. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12317. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12318. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12319. dev_priv->display.update_primary_plane =
  12320. i9xx_update_primary_plane;
  12321. } else {
  12322. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12323. dev_priv->display.get_initial_plane_config =
  12324. i9xx_get_initial_plane_config;
  12325. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12326. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12327. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12328. dev_priv->display.update_primary_plane =
  12329. i9xx_update_primary_plane;
  12330. }
  12331. /* Returns the core display clock speed */
  12332. if (IS_SKYLAKE(dev))
  12333. dev_priv->display.get_display_clock_speed =
  12334. skylake_get_display_clock_speed;
  12335. else if (IS_BROADWELL(dev))
  12336. dev_priv->display.get_display_clock_speed =
  12337. broadwell_get_display_clock_speed;
  12338. else if (IS_HASWELL(dev))
  12339. dev_priv->display.get_display_clock_speed =
  12340. haswell_get_display_clock_speed;
  12341. else if (IS_VALLEYVIEW(dev))
  12342. dev_priv->display.get_display_clock_speed =
  12343. valleyview_get_display_clock_speed;
  12344. else if (IS_GEN5(dev))
  12345. dev_priv->display.get_display_clock_speed =
  12346. ilk_get_display_clock_speed;
  12347. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12348. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12349. dev_priv->display.get_display_clock_speed =
  12350. i945_get_display_clock_speed;
  12351. else if (IS_GM45(dev))
  12352. dev_priv->display.get_display_clock_speed =
  12353. gm45_get_display_clock_speed;
  12354. else if (IS_CRESTLINE(dev))
  12355. dev_priv->display.get_display_clock_speed =
  12356. i965gm_get_display_clock_speed;
  12357. else if (IS_PINEVIEW(dev))
  12358. dev_priv->display.get_display_clock_speed =
  12359. pnv_get_display_clock_speed;
  12360. else if (IS_G33(dev) || IS_G4X(dev))
  12361. dev_priv->display.get_display_clock_speed =
  12362. g33_get_display_clock_speed;
  12363. else if (IS_I915G(dev))
  12364. dev_priv->display.get_display_clock_speed =
  12365. i915_get_display_clock_speed;
  12366. else if (IS_I945GM(dev) || IS_845G(dev))
  12367. dev_priv->display.get_display_clock_speed =
  12368. i9xx_misc_get_display_clock_speed;
  12369. else if (IS_PINEVIEW(dev))
  12370. dev_priv->display.get_display_clock_speed =
  12371. pnv_get_display_clock_speed;
  12372. else if (IS_I915GM(dev))
  12373. dev_priv->display.get_display_clock_speed =
  12374. i915gm_get_display_clock_speed;
  12375. else if (IS_I865G(dev))
  12376. dev_priv->display.get_display_clock_speed =
  12377. i865_get_display_clock_speed;
  12378. else if (IS_I85X(dev))
  12379. dev_priv->display.get_display_clock_speed =
  12380. i85x_get_display_clock_speed;
  12381. else { /* 830 */
  12382. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12383. dev_priv->display.get_display_clock_speed =
  12384. i830_get_display_clock_speed;
  12385. }
  12386. if (IS_GEN5(dev)) {
  12387. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12388. } else if (IS_GEN6(dev)) {
  12389. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12390. } else if (IS_IVYBRIDGE(dev)) {
  12391. /* FIXME: detect B0+ stepping and use auto training */
  12392. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12393. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12394. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12395. if (IS_BROADWELL(dev))
  12396. dev_priv->display.modeset_global_resources =
  12397. broadwell_modeset_global_resources;
  12398. } else if (IS_VALLEYVIEW(dev)) {
  12399. dev_priv->display.modeset_global_resources =
  12400. valleyview_modeset_global_resources;
  12401. } else if (IS_BROXTON(dev)) {
  12402. dev_priv->display.modeset_global_resources =
  12403. broxton_modeset_global_resources;
  12404. }
  12405. switch (INTEL_INFO(dev)->gen) {
  12406. case 2:
  12407. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12408. break;
  12409. case 3:
  12410. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12411. break;
  12412. case 4:
  12413. case 5:
  12414. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12415. break;
  12416. case 6:
  12417. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12418. break;
  12419. case 7:
  12420. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12421. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12422. break;
  12423. case 9:
  12424. /* Drop through - unsupported since execlist only. */
  12425. default:
  12426. /* Default just returns -ENODEV to indicate unsupported */
  12427. dev_priv->display.queue_flip = intel_default_queue_flip;
  12428. }
  12429. intel_panel_init_backlight_funcs(dev);
  12430. mutex_init(&dev_priv->pps_mutex);
  12431. }
  12432. /*
  12433. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12434. * resume, or other times. This quirk makes sure that's the case for
  12435. * affected systems.
  12436. */
  12437. static void quirk_pipea_force(struct drm_device *dev)
  12438. {
  12439. struct drm_i915_private *dev_priv = dev->dev_private;
  12440. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12441. DRM_INFO("applying pipe a force quirk\n");
  12442. }
  12443. static void quirk_pipeb_force(struct drm_device *dev)
  12444. {
  12445. struct drm_i915_private *dev_priv = dev->dev_private;
  12446. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12447. DRM_INFO("applying pipe b force quirk\n");
  12448. }
  12449. /*
  12450. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12451. */
  12452. static void quirk_ssc_force_disable(struct drm_device *dev)
  12453. {
  12454. struct drm_i915_private *dev_priv = dev->dev_private;
  12455. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12456. DRM_INFO("applying lvds SSC disable quirk\n");
  12457. }
  12458. /*
  12459. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12460. * brightness value
  12461. */
  12462. static void quirk_invert_brightness(struct drm_device *dev)
  12463. {
  12464. struct drm_i915_private *dev_priv = dev->dev_private;
  12465. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12466. DRM_INFO("applying inverted panel brightness quirk\n");
  12467. }
  12468. /* Some VBT's incorrectly indicate no backlight is present */
  12469. static void quirk_backlight_present(struct drm_device *dev)
  12470. {
  12471. struct drm_i915_private *dev_priv = dev->dev_private;
  12472. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12473. DRM_INFO("applying backlight present quirk\n");
  12474. }
  12475. struct intel_quirk {
  12476. int device;
  12477. int subsystem_vendor;
  12478. int subsystem_device;
  12479. void (*hook)(struct drm_device *dev);
  12480. };
  12481. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12482. struct intel_dmi_quirk {
  12483. void (*hook)(struct drm_device *dev);
  12484. const struct dmi_system_id (*dmi_id_list)[];
  12485. };
  12486. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12487. {
  12488. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12489. return 1;
  12490. }
  12491. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12492. {
  12493. .dmi_id_list = &(const struct dmi_system_id[]) {
  12494. {
  12495. .callback = intel_dmi_reverse_brightness,
  12496. .ident = "NCR Corporation",
  12497. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12498. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12499. },
  12500. },
  12501. { } /* terminating entry */
  12502. },
  12503. .hook = quirk_invert_brightness,
  12504. },
  12505. };
  12506. static struct intel_quirk intel_quirks[] = {
  12507. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12508. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12509. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12510. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12511. /* 830 needs to leave pipe A & dpll A up */
  12512. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12513. /* 830 needs to leave pipe B & dpll B up */
  12514. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12515. /* Lenovo U160 cannot use SSC on LVDS */
  12516. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12517. /* Sony Vaio Y cannot use SSC on LVDS */
  12518. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12519. /* Acer Aspire 5734Z must invert backlight brightness */
  12520. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12521. /* Acer/eMachines G725 */
  12522. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12523. /* Acer/eMachines e725 */
  12524. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12525. /* Acer/Packard Bell NCL20 */
  12526. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12527. /* Acer Aspire 4736Z */
  12528. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12529. /* Acer Aspire 5336 */
  12530. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12531. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12532. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12533. /* Acer C720 Chromebook (Core i3 4005U) */
  12534. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12535. /* Apple Macbook 2,1 (Core 2 T7400) */
  12536. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12537. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12538. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12539. /* HP Chromebook 14 (Celeron 2955U) */
  12540. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12541. /* Dell Chromebook 11 */
  12542. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12543. };
  12544. static void intel_init_quirks(struct drm_device *dev)
  12545. {
  12546. struct pci_dev *d = dev->pdev;
  12547. int i;
  12548. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12549. struct intel_quirk *q = &intel_quirks[i];
  12550. if (d->device == q->device &&
  12551. (d->subsystem_vendor == q->subsystem_vendor ||
  12552. q->subsystem_vendor == PCI_ANY_ID) &&
  12553. (d->subsystem_device == q->subsystem_device ||
  12554. q->subsystem_device == PCI_ANY_ID))
  12555. q->hook(dev);
  12556. }
  12557. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12558. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12559. intel_dmi_quirks[i].hook(dev);
  12560. }
  12561. }
  12562. /* Disable the VGA plane that we never use */
  12563. static void i915_disable_vga(struct drm_device *dev)
  12564. {
  12565. struct drm_i915_private *dev_priv = dev->dev_private;
  12566. u8 sr1;
  12567. u32 vga_reg = i915_vgacntrl_reg(dev);
  12568. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12569. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12570. outb(SR01, VGA_SR_INDEX);
  12571. sr1 = inb(VGA_SR_DATA);
  12572. outb(sr1 | 1<<5, VGA_SR_DATA);
  12573. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12574. udelay(300);
  12575. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12576. POSTING_READ(vga_reg);
  12577. }
  12578. void intel_modeset_init_hw(struct drm_device *dev)
  12579. {
  12580. intel_update_cdclk(dev);
  12581. intel_prepare_ddi(dev);
  12582. intel_init_clock_gating(dev);
  12583. intel_enable_gt_powersave(dev);
  12584. }
  12585. void intel_modeset_init(struct drm_device *dev)
  12586. {
  12587. struct drm_i915_private *dev_priv = dev->dev_private;
  12588. int sprite, ret;
  12589. enum pipe pipe;
  12590. struct intel_crtc *crtc;
  12591. drm_mode_config_init(dev);
  12592. dev->mode_config.min_width = 0;
  12593. dev->mode_config.min_height = 0;
  12594. dev->mode_config.preferred_depth = 24;
  12595. dev->mode_config.prefer_shadow = 1;
  12596. dev->mode_config.allow_fb_modifiers = true;
  12597. dev->mode_config.funcs = &intel_mode_funcs;
  12598. intel_init_quirks(dev);
  12599. intel_init_pm(dev);
  12600. if (INTEL_INFO(dev)->num_pipes == 0)
  12601. return;
  12602. intel_init_display(dev);
  12603. intel_init_audio(dev);
  12604. if (IS_GEN2(dev)) {
  12605. dev->mode_config.max_width = 2048;
  12606. dev->mode_config.max_height = 2048;
  12607. } else if (IS_GEN3(dev)) {
  12608. dev->mode_config.max_width = 4096;
  12609. dev->mode_config.max_height = 4096;
  12610. } else {
  12611. dev->mode_config.max_width = 8192;
  12612. dev->mode_config.max_height = 8192;
  12613. }
  12614. if (IS_845G(dev) || IS_I865G(dev)) {
  12615. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12616. dev->mode_config.cursor_height = 1023;
  12617. } else if (IS_GEN2(dev)) {
  12618. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12619. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12620. } else {
  12621. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12622. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12623. }
  12624. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12625. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12626. INTEL_INFO(dev)->num_pipes,
  12627. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12628. for_each_pipe(dev_priv, pipe) {
  12629. intel_crtc_init(dev, pipe);
  12630. for_each_sprite(dev_priv, pipe, sprite) {
  12631. ret = intel_plane_init(dev, pipe, sprite);
  12632. if (ret)
  12633. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12634. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12635. }
  12636. }
  12637. intel_init_dpio(dev);
  12638. intel_shared_dpll_init(dev);
  12639. /* Just disable it once at startup */
  12640. i915_disable_vga(dev);
  12641. intel_setup_outputs(dev);
  12642. /* Just in case the BIOS is doing something questionable. */
  12643. intel_fbc_disable(dev);
  12644. drm_modeset_lock_all(dev);
  12645. intel_modeset_setup_hw_state(dev, false);
  12646. drm_modeset_unlock_all(dev);
  12647. for_each_intel_crtc(dev, crtc) {
  12648. if (!crtc->active)
  12649. continue;
  12650. /*
  12651. * Note that reserving the BIOS fb up front prevents us
  12652. * from stuffing other stolen allocations like the ring
  12653. * on top. This prevents some ugliness at boot time, and
  12654. * can even allow for smooth boot transitions if the BIOS
  12655. * fb is large enough for the active pipe configuration.
  12656. */
  12657. if (dev_priv->display.get_initial_plane_config) {
  12658. dev_priv->display.get_initial_plane_config(crtc,
  12659. &crtc->plane_config);
  12660. /*
  12661. * If the fb is shared between multiple heads, we'll
  12662. * just get the first one.
  12663. */
  12664. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12665. }
  12666. }
  12667. }
  12668. static void intel_enable_pipe_a(struct drm_device *dev)
  12669. {
  12670. struct intel_connector *connector;
  12671. struct drm_connector *crt = NULL;
  12672. struct intel_load_detect_pipe load_detect_temp;
  12673. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12674. /* We can't just switch on the pipe A, we need to set things up with a
  12675. * proper mode and output configuration. As a gross hack, enable pipe A
  12676. * by enabling the load detect pipe once. */
  12677. for_each_intel_connector(dev, connector) {
  12678. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12679. crt = &connector->base;
  12680. break;
  12681. }
  12682. }
  12683. if (!crt)
  12684. return;
  12685. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12686. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12687. }
  12688. static bool
  12689. intel_check_plane_mapping(struct intel_crtc *crtc)
  12690. {
  12691. struct drm_device *dev = crtc->base.dev;
  12692. struct drm_i915_private *dev_priv = dev->dev_private;
  12693. u32 reg, val;
  12694. if (INTEL_INFO(dev)->num_pipes == 1)
  12695. return true;
  12696. reg = DSPCNTR(!crtc->plane);
  12697. val = I915_READ(reg);
  12698. if ((val & DISPLAY_PLANE_ENABLE) &&
  12699. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12700. return false;
  12701. return true;
  12702. }
  12703. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12704. {
  12705. struct drm_device *dev = crtc->base.dev;
  12706. struct drm_i915_private *dev_priv = dev->dev_private;
  12707. struct intel_encoder *encoder;
  12708. u32 reg;
  12709. bool enable;
  12710. /* Clear any frame start delays used for debugging left by the BIOS */
  12711. reg = PIPECONF(crtc->config->cpu_transcoder);
  12712. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12713. /* restore vblank interrupts to correct state */
  12714. drm_crtc_vblank_reset(&crtc->base);
  12715. if (crtc->active) {
  12716. update_scanline_offset(crtc);
  12717. drm_crtc_vblank_on(&crtc->base);
  12718. }
  12719. /* We need to sanitize the plane -> pipe mapping first because this will
  12720. * disable the crtc (and hence change the state) if it is wrong. Note
  12721. * that gen4+ has a fixed plane -> pipe mapping. */
  12722. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12723. bool plane;
  12724. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12725. crtc->base.base.id);
  12726. /* Pipe has the wrong plane attached and the plane is active.
  12727. * Temporarily change the plane mapping and disable everything
  12728. * ... */
  12729. plane = crtc->plane;
  12730. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12731. crtc->plane = !plane;
  12732. intel_crtc_disable_noatomic(&crtc->base);
  12733. crtc->plane = plane;
  12734. }
  12735. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12736. crtc->pipe == PIPE_A && !crtc->active) {
  12737. /* BIOS forgot to enable pipe A, this mostly happens after
  12738. * resume. Force-enable the pipe to fix this, the update_dpms
  12739. * call below we restore the pipe to the right state, but leave
  12740. * the required bits on. */
  12741. intel_enable_pipe_a(dev);
  12742. }
  12743. /* Adjust the state of the output pipe according to whether we
  12744. * have active connectors/encoders. */
  12745. enable = false;
  12746. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12747. enable |= encoder->connectors_active;
  12748. if (!enable)
  12749. intel_crtc_disable_noatomic(&crtc->base);
  12750. if (crtc->active != crtc->base.state->active) {
  12751. /* This can happen either due to bugs in the get_hw_state
  12752. * functions or because of calls to intel_crtc_disable_noatomic,
  12753. * or because the pipe is force-enabled due to the
  12754. * pipe A quirk. */
  12755. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12756. crtc->base.base.id,
  12757. crtc->base.state->enable ? "enabled" : "disabled",
  12758. crtc->active ? "enabled" : "disabled");
  12759. crtc->base.state->enable = crtc->active;
  12760. crtc->base.state->active = crtc->active;
  12761. crtc->base.enabled = crtc->active;
  12762. /* Because we only establish the connector -> encoder ->
  12763. * crtc links if something is active, this means the
  12764. * crtc is now deactivated. Break the links. connector
  12765. * -> encoder links are only establish when things are
  12766. * actually up, hence no need to break them. */
  12767. WARN_ON(crtc->active);
  12768. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12769. WARN_ON(encoder->connectors_active);
  12770. encoder->base.crtc = NULL;
  12771. }
  12772. }
  12773. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12774. /*
  12775. * We start out with underrun reporting disabled to avoid races.
  12776. * For correct bookkeeping mark this on active crtcs.
  12777. *
  12778. * Also on gmch platforms we dont have any hardware bits to
  12779. * disable the underrun reporting. Which means we need to start
  12780. * out with underrun reporting disabled also on inactive pipes,
  12781. * since otherwise we'll complain about the garbage we read when
  12782. * e.g. coming up after runtime pm.
  12783. *
  12784. * No protection against concurrent access is required - at
  12785. * worst a fifo underrun happens which also sets this to false.
  12786. */
  12787. crtc->cpu_fifo_underrun_disabled = true;
  12788. crtc->pch_fifo_underrun_disabled = true;
  12789. }
  12790. }
  12791. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12792. {
  12793. struct intel_connector *connector;
  12794. struct drm_device *dev = encoder->base.dev;
  12795. /* We need to check both for a crtc link (meaning that the
  12796. * encoder is active and trying to read from a pipe) and the
  12797. * pipe itself being active. */
  12798. bool has_active_crtc = encoder->base.crtc &&
  12799. to_intel_crtc(encoder->base.crtc)->active;
  12800. if (encoder->connectors_active && !has_active_crtc) {
  12801. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12802. encoder->base.base.id,
  12803. encoder->base.name);
  12804. /* Connector is active, but has no active pipe. This is
  12805. * fallout from our resume register restoring. Disable
  12806. * the encoder manually again. */
  12807. if (encoder->base.crtc) {
  12808. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12809. encoder->base.base.id,
  12810. encoder->base.name);
  12811. encoder->disable(encoder);
  12812. if (encoder->post_disable)
  12813. encoder->post_disable(encoder);
  12814. }
  12815. encoder->base.crtc = NULL;
  12816. encoder->connectors_active = false;
  12817. /* Inconsistent output/port/pipe state happens presumably due to
  12818. * a bug in one of the get_hw_state functions. Or someplace else
  12819. * in our code, like the register restore mess on resume. Clamp
  12820. * things to off as a safer default. */
  12821. for_each_intel_connector(dev, connector) {
  12822. if (connector->encoder != encoder)
  12823. continue;
  12824. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12825. connector->base.encoder = NULL;
  12826. }
  12827. }
  12828. /* Enabled encoders without active connectors will be fixed in
  12829. * the crtc fixup. */
  12830. }
  12831. void i915_redisable_vga_power_on(struct drm_device *dev)
  12832. {
  12833. struct drm_i915_private *dev_priv = dev->dev_private;
  12834. u32 vga_reg = i915_vgacntrl_reg(dev);
  12835. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12836. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12837. i915_disable_vga(dev);
  12838. }
  12839. }
  12840. void i915_redisable_vga(struct drm_device *dev)
  12841. {
  12842. struct drm_i915_private *dev_priv = dev->dev_private;
  12843. /* This function can be called both from intel_modeset_setup_hw_state or
  12844. * at a very early point in our resume sequence, where the power well
  12845. * structures are not yet restored. Since this function is at a very
  12846. * paranoid "someone might have enabled VGA while we were not looking"
  12847. * level, just check if the power well is enabled instead of trying to
  12848. * follow the "don't touch the power well if we don't need it" policy
  12849. * the rest of the driver uses. */
  12850. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12851. return;
  12852. i915_redisable_vga_power_on(dev);
  12853. }
  12854. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12855. {
  12856. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12857. if (!crtc->active)
  12858. return false;
  12859. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12860. }
  12861. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12862. {
  12863. struct drm_i915_private *dev_priv = dev->dev_private;
  12864. enum pipe pipe;
  12865. struct intel_crtc *crtc;
  12866. struct intel_encoder *encoder;
  12867. struct intel_connector *connector;
  12868. int i;
  12869. for_each_intel_crtc(dev, crtc) {
  12870. struct drm_plane *primary = crtc->base.primary;
  12871. struct intel_plane_state *plane_state;
  12872. memset(crtc->config, 0, sizeof(*crtc->config));
  12873. crtc->config->base.crtc = &crtc->base;
  12874. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12875. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12876. crtc->config);
  12877. crtc->base.state->enable = crtc->active;
  12878. crtc->base.state->active = crtc->active;
  12879. crtc->base.enabled = crtc->active;
  12880. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12881. plane_state = to_intel_plane_state(primary->state);
  12882. plane_state->visible = primary_get_hw_state(crtc);
  12883. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12884. crtc->base.base.id,
  12885. crtc->active ? "enabled" : "disabled");
  12886. }
  12887. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12888. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12889. pll->on = pll->get_hw_state(dev_priv, pll,
  12890. &pll->config.hw_state);
  12891. pll->active = 0;
  12892. pll->config.crtc_mask = 0;
  12893. for_each_intel_crtc(dev, crtc) {
  12894. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12895. pll->active++;
  12896. pll->config.crtc_mask |= 1 << crtc->pipe;
  12897. }
  12898. }
  12899. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12900. pll->name, pll->config.crtc_mask, pll->on);
  12901. if (pll->config.crtc_mask)
  12902. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12903. }
  12904. for_each_intel_encoder(dev, encoder) {
  12905. pipe = 0;
  12906. if (encoder->get_hw_state(encoder, &pipe)) {
  12907. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12908. encoder->base.crtc = &crtc->base;
  12909. encoder->get_config(encoder, crtc->config);
  12910. } else {
  12911. encoder->base.crtc = NULL;
  12912. }
  12913. encoder->connectors_active = false;
  12914. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12915. encoder->base.base.id,
  12916. encoder->base.name,
  12917. encoder->base.crtc ? "enabled" : "disabled",
  12918. pipe_name(pipe));
  12919. }
  12920. for_each_intel_connector(dev, connector) {
  12921. if (connector->get_hw_state(connector)) {
  12922. connector->base.dpms = DRM_MODE_DPMS_ON;
  12923. connector->encoder->connectors_active = true;
  12924. connector->base.encoder = &connector->encoder->base;
  12925. } else {
  12926. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12927. connector->base.encoder = NULL;
  12928. }
  12929. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12930. connector->base.base.id,
  12931. connector->base.name,
  12932. connector->base.encoder ? "enabled" : "disabled");
  12933. }
  12934. }
  12935. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12936. * and i915 state tracking structures. */
  12937. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12938. bool force_restore)
  12939. {
  12940. struct drm_i915_private *dev_priv = dev->dev_private;
  12941. enum pipe pipe;
  12942. struct intel_crtc *crtc;
  12943. struct intel_encoder *encoder;
  12944. int i;
  12945. intel_modeset_readout_hw_state(dev);
  12946. /*
  12947. * Now that we have the config, copy it to each CRTC struct
  12948. * Note that this could go away if we move to using crtc_config
  12949. * checking everywhere.
  12950. */
  12951. for_each_intel_crtc(dev, crtc) {
  12952. if (crtc->active && i915.fastboot) {
  12953. intel_mode_from_pipe_config(&crtc->base.mode,
  12954. crtc->config);
  12955. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12956. crtc->base.base.id);
  12957. drm_mode_debug_printmodeline(&crtc->base.mode);
  12958. }
  12959. }
  12960. /* HW state is read out, now we need to sanitize this mess. */
  12961. for_each_intel_encoder(dev, encoder) {
  12962. intel_sanitize_encoder(encoder);
  12963. }
  12964. for_each_pipe(dev_priv, pipe) {
  12965. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12966. intel_sanitize_crtc(crtc);
  12967. intel_dump_pipe_config(crtc, crtc->config,
  12968. "[setup_hw_state]");
  12969. }
  12970. intel_modeset_update_connector_atomic_state(dev);
  12971. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12972. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12973. if (!pll->on || pll->active)
  12974. continue;
  12975. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12976. pll->disable(dev_priv, pll);
  12977. pll->on = false;
  12978. }
  12979. if (IS_GEN9(dev))
  12980. skl_wm_get_hw_state(dev);
  12981. else if (HAS_PCH_SPLIT(dev))
  12982. ilk_wm_get_hw_state(dev);
  12983. if (force_restore) {
  12984. i915_redisable_vga(dev);
  12985. /*
  12986. * We need to use raw interfaces for restoring state to avoid
  12987. * checking (bogus) intermediate states.
  12988. */
  12989. for_each_pipe(dev_priv, pipe) {
  12990. struct drm_crtc *crtc =
  12991. dev_priv->pipe_to_crtc_mapping[pipe];
  12992. intel_crtc_restore_mode(crtc);
  12993. }
  12994. } else {
  12995. intel_modeset_update_staged_output_state(dev);
  12996. }
  12997. intel_modeset_check_state(dev);
  12998. }
  12999. void intel_modeset_gem_init(struct drm_device *dev)
  13000. {
  13001. struct drm_i915_private *dev_priv = dev->dev_private;
  13002. struct drm_crtc *c;
  13003. struct drm_i915_gem_object *obj;
  13004. int ret;
  13005. mutex_lock(&dev->struct_mutex);
  13006. intel_init_gt_powersave(dev);
  13007. mutex_unlock(&dev->struct_mutex);
  13008. /*
  13009. * There may be no VBT; and if the BIOS enabled SSC we can
  13010. * just keep using it to avoid unnecessary flicker. Whereas if the
  13011. * BIOS isn't using it, don't assume it will work even if the VBT
  13012. * indicates as much.
  13013. */
  13014. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13015. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13016. DREF_SSC1_ENABLE);
  13017. intel_modeset_init_hw(dev);
  13018. intel_setup_overlay(dev);
  13019. /*
  13020. * Make sure any fbs we allocated at startup are properly
  13021. * pinned & fenced. When we do the allocation it's too early
  13022. * for this.
  13023. */
  13024. for_each_crtc(dev, c) {
  13025. obj = intel_fb_obj(c->primary->fb);
  13026. if (obj == NULL)
  13027. continue;
  13028. mutex_lock(&dev->struct_mutex);
  13029. ret = intel_pin_and_fence_fb_obj(c->primary,
  13030. c->primary->fb,
  13031. c->primary->state,
  13032. NULL);
  13033. mutex_unlock(&dev->struct_mutex);
  13034. if (ret) {
  13035. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13036. to_intel_crtc(c)->pipe);
  13037. drm_framebuffer_unreference(c->primary->fb);
  13038. c->primary->fb = NULL;
  13039. c->primary->crtc = c->primary->state->crtc = NULL;
  13040. update_state_fb(c->primary);
  13041. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13042. }
  13043. }
  13044. intel_backlight_register(dev);
  13045. }
  13046. void intel_connector_unregister(struct intel_connector *intel_connector)
  13047. {
  13048. struct drm_connector *connector = &intel_connector->base;
  13049. intel_panel_destroy_backlight(connector);
  13050. drm_connector_unregister(connector);
  13051. }
  13052. void intel_modeset_cleanup(struct drm_device *dev)
  13053. {
  13054. struct drm_i915_private *dev_priv = dev->dev_private;
  13055. struct drm_connector *connector;
  13056. intel_disable_gt_powersave(dev);
  13057. intel_backlight_unregister(dev);
  13058. /*
  13059. * Interrupts and polling as the first thing to avoid creating havoc.
  13060. * Too much stuff here (turning of connectors, ...) would
  13061. * experience fancy races otherwise.
  13062. */
  13063. intel_irq_uninstall(dev_priv);
  13064. /*
  13065. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13066. * poll handlers. Hence disable polling after hpd handling is shut down.
  13067. */
  13068. drm_kms_helper_poll_fini(dev);
  13069. mutex_lock(&dev->struct_mutex);
  13070. intel_unregister_dsm_handler();
  13071. intel_fbc_disable(dev);
  13072. mutex_unlock(&dev->struct_mutex);
  13073. /* flush any delayed tasks or pending work */
  13074. flush_scheduled_work();
  13075. /* destroy the backlight and sysfs files before encoders/connectors */
  13076. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13077. struct intel_connector *intel_connector;
  13078. intel_connector = to_intel_connector(connector);
  13079. intel_connector->unregister(intel_connector);
  13080. }
  13081. drm_mode_config_cleanup(dev);
  13082. intel_cleanup_overlay(dev);
  13083. mutex_lock(&dev->struct_mutex);
  13084. intel_cleanup_gt_powersave(dev);
  13085. mutex_unlock(&dev->struct_mutex);
  13086. }
  13087. /*
  13088. * Return which encoder is currently attached for connector.
  13089. */
  13090. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13091. {
  13092. return &intel_attached_encoder(connector)->base;
  13093. }
  13094. void intel_connector_attach_encoder(struct intel_connector *connector,
  13095. struct intel_encoder *encoder)
  13096. {
  13097. connector->encoder = encoder;
  13098. drm_mode_connector_attach_encoder(&connector->base,
  13099. &encoder->base);
  13100. }
  13101. /*
  13102. * set vga decode state - true == enable VGA decode
  13103. */
  13104. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13105. {
  13106. struct drm_i915_private *dev_priv = dev->dev_private;
  13107. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13108. u16 gmch_ctrl;
  13109. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13110. DRM_ERROR("failed to read control word\n");
  13111. return -EIO;
  13112. }
  13113. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13114. return 0;
  13115. if (state)
  13116. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13117. else
  13118. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13119. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13120. DRM_ERROR("failed to write control word\n");
  13121. return -EIO;
  13122. }
  13123. return 0;
  13124. }
  13125. struct intel_display_error_state {
  13126. u32 power_well_driver;
  13127. int num_transcoders;
  13128. struct intel_cursor_error_state {
  13129. u32 control;
  13130. u32 position;
  13131. u32 base;
  13132. u32 size;
  13133. } cursor[I915_MAX_PIPES];
  13134. struct intel_pipe_error_state {
  13135. bool power_domain_on;
  13136. u32 source;
  13137. u32 stat;
  13138. } pipe[I915_MAX_PIPES];
  13139. struct intel_plane_error_state {
  13140. u32 control;
  13141. u32 stride;
  13142. u32 size;
  13143. u32 pos;
  13144. u32 addr;
  13145. u32 surface;
  13146. u32 tile_offset;
  13147. } plane[I915_MAX_PIPES];
  13148. struct intel_transcoder_error_state {
  13149. bool power_domain_on;
  13150. enum transcoder cpu_transcoder;
  13151. u32 conf;
  13152. u32 htotal;
  13153. u32 hblank;
  13154. u32 hsync;
  13155. u32 vtotal;
  13156. u32 vblank;
  13157. u32 vsync;
  13158. } transcoder[4];
  13159. };
  13160. struct intel_display_error_state *
  13161. intel_display_capture_error_state(struct drm_device *dev)
  13162. {
  13163. struct drm_i915_private *dev_priv = dev->dev_private;
  13164. struct intel_display_error_state *error;
  13165. int transcoders[] = {
  13166. TRANSCODER_A,
  13167. TRANSCODER_B,
  13168. TRANSCODER_C,
  13169. TRANSCODER_EDP,
  13170. };
  13171. int i;
  13172. if (INTEL_INFO(dev)->num_pipes == 0)
  13173. return NULL;
  13174. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13175. if (error == NULL)
  13176. return NULL;
  13177. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13178. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13179. for_each_pipe(dev_priv, i) {
  13180. error->pipe[i].power_domain_on =
  13181. __intel_display_power_is_enabled(dev_priv,
  13182. POWER_DOMAIN_PIPE(i));
  13183. if (!error->pipe[i].power_domain_on)
  13184. continue;
  13185. error->cursor[i].control = I915_READ(CURCNTR(i));
  13186. error->cursor[i].position = I915_READ(CURPOS(i));
  13187. error->cursor[i].base = I915_READ(CURBASE(i));
  13188. error->plane[i].control = I915_READ(DSPCNTR(i));
  13189. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13190. if (INTEL_INFO(dev)->gen <= 3) {
  13191. error->plane[i].size = I915_READ(DSPSIZE(i));
  13192. error->plane[i].pos = I915_READ(DSPPOS(i));
  13193. }
  13194. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13195. error->plane[i].addr = I915_READ(DSPADDR(i));
  13196. if (INTEL_INFO(dev)->gen >= 4) {
  13197. error->plane[i].surface = I915_READ(DSPSURF(i));
  13198. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13199. }
  13200. error->pipe[i].source = I915_READ(PIPESRC(i));
  13201. if (HAS_GMCH_DISPLAY(dev))
  13202. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13203. }
  13204. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13205. if (HAS_DDI(dev_priv->dev))
  13206. error->num_transcoders++; /* Account for eDP. */
  13207. for (i = 0; i < error->num_transcoders; i++) {
  13208. enum transcoder cpu_transcoder = transcoders[i];
  13209. error->transcoder[i].power_domain_on =
  13210. __intel_display_power_is_enabled(dev_priv,
  13211. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13212. if (!error->transcoder[i].power_domain_on)
  13213. continue;
  13214. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13215. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13216. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13217. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13218. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13219. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13220. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13221. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13222. }
  13223. return error;
  13224. }
  13225. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13226. void
  13227. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13228. struct drm_device *dev,
  13229. struct intel_display_error_state *error)
  13230. {
  13231. struct drm_i915_private *dev_priv = dev->dev_private;
  13232. int i;
  13233. if (!error)
  13234. return;
  13235. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13236. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13237. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13238. error->power_well_driver);
  13239. for_each_pipe(dev_priv, i) {
  13240. err_printf(m, "Pipe [%d]:\n", i);
  13241. err_printf(m, " Power: %s\n",
  13242. error->pipe[i].power_domain_on ? "on" : "off");
  13243. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13244. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13245. err_printf(m, "Plane [%d]:\n", i);
  13246. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13247. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13248. if (INTEL_INFO(dev)->gen <= 3) {
  13249. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13250. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13251. }
  13252. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13253. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13254. if (INTEL_INFO(dev)->gen >= 4) {
  13255. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13256. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13257. }
  13258. err_printf(m, "Cursor [%d]:\n", i);
  13259. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13260. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13261. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13262. }
  13263. for (i = 0; i < error->num_transcoders; i++) {
  13264. err_printf(m, "CPU transcoder: %c\n",
  13265. transcoder_name(error->transcoder[i].cpu_transcoder));
  13266. err_printf(m, " Power: %s\n",
  13267. error->transcoder[i].power_domain_on ? "on" : "off");
  13268. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13269. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13270. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13271. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13272. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13273. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13274. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13275. }
  13276. }
  13277. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13278. {
  13279. struct intel_crtc *crtc;
  13280. for_each_intel_crtc(dev, crtc) {
  13281. struct intel_unpin_work *work;
  13282. spin_lock_irq(&dev->event_lock);
  13283. work = crtc->unpin_work;
  13284. if (work && work->event &&
  13285. work->event->base.file_priv == file) {
  13286. kfree(work->event);
  13287. work->event = NULL;
  13288. }
  13289. spin_unlock_irq(&dev->event_lock);
  13290. }
  13291. }