intel_display.c 438 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931
  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. static 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. dev = intel_crtc->base.dev;
  2534. dev_priv = dev->dev_private;
  2535. scaler_state = &intel_crtc->config->scaler_state;
  2536. /* loop through and disable scalers that aren't in use */
  2537. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2538. if (!scaler_state->scalers[i].in_use) {
  2539. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
  2540. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
  2541. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
  2542. DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
  2543. intel_crtc->base.base.id, intel_crtc->pipe, i);
  2544. }
  2545. }
  2546. }
  2547. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2548. {
  2549. switch (pixel_format) {
  2550. case DRM_FORMAT_C8:
  2551. return PLANE_CTL_FORMAT_INDEXED;
  2552. case DRM_FORMAT_RGB565:
  2553. return PLANE_CTL_FORMAT_RGB_565;
  2554. case DRM_FORMAT_XBGR8888:
  2555. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2556. case DRM_FORMAT_XRGB8888:
  2557. return PLANE_CTL_FORMAT_XRGB_8888;
  2558. /*
  2559. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2560. * to be already pre-multiplied. We need to add a knob (or a different
  2561. * DRM_FORMAT) for user-space to configure that.
  2562. */
  2563. case DRM_FORMAT_ABGR8888:
  2564. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2565. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2566. case DRM_FORMAT_ARGB8888:
  2567. return PLANE_CTL_FORMAT_XRGB_8888 |
  2568. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2569. case DRM_FORMAT_XRGB2101010:
  2570. return PLANE_CTL_FORMAT_XRGB_2101010;
  2571. case DRM_FORMAT_XBGR2101010:
  2572. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2573. case DRM_FORMAT_YUYV:
  2574. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2575. case DRM_FORMAT_YVYU:
  2576. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2577. case DRM_FORMAT_UYVY:
  2578. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2579. case DRM_FORMAT_VYUY:
  2580. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2581. default:
  2582. MISSING_CASE(pixel_format);
  2583. }
  2584. return 0;
  2585. }
  2586. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2587. {
  2588. switch (fb_modifier) {
  2589. case DRM_FORMAT_MOD_NONE:
  2590. break;
  2591. case I915_FORMAT_MOD_X_TILED:
  2592. return PLANE_CTL_TILED_X;
  2593. case I915_FORMAT_MOD_Y_TILED:
  2594. return PLANE_CTL_TILED_Y;
  2595. case I915_FORMAT_MOD_Yf_TILED:
  2596. return PLANE_CTL_TILED_YF;
  2597. default:
  2598. MISSING_CASE(fb_modifier);
  2599. }
  2600. return 0;
  2601. }
  2602. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2603. {
  2604. switch (rotation) {
  2605. case BIT(DRM_ROTATE_0):
  2606. break;
  2607. /*
  2608. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2609. * while i915 HW rotation is clockwise, thats why this swapping.
  2610. */
  2611. case BIT(DRM_ROTATE_90):
  2612. return PLANE_CTL_ROTATE_270;
  2613. case BIT(DRM_ROTATE_180):
  2614. return PLANE_CTL_ROTATE_180;
  2615. case BIT(DRM_ROTATE_270):
  2616. return PLANE_CTL_ROTATE_90;
  2617. default:
  2618. MISSING_CASE(rotation);
  2619. }
  2620. return 0;
  2621. }
  2622. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2623. struct drm_framebuffer *fb,
  2624. int x, int y)
  2625. {
  2626. struct drm_device *dev = crtc->dev;
  2627. struct drm_i915_private *dev_priv = dev->dev_private;
  2628. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2629. struct drm_plane *plane = crtc->primary;
  2630. bool visible = to_intel_plane_state(plane->state)->visible;
  2631. struct drm_i915_gem_object *obj;
  2632. int pipe = intel_crtc->pipe;
  2633. u32 plane_ctl, stride_div, stride;
  2634. u32 tile_height, plane_offset, plane_size;
  2635. unsigned int rotation;
  2636. int x_offset, y_offset;
  2637. unsigned long surf_addr;
  2638. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2639. struct intel_plane_state *plane_state;
  2640. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2641. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2642. int scaler_id = -1;
  2643. plane_state = to_intel_plane_state(plane->state);
  2644. if (!visible || !fb) {
  2645. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2646. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2647. POSTING_READ(PLANE_CTL(pipe, 0));
  2648. return;
  2649. }
  2650. plane_ctl = PLANE_CTL_ENABLE |
  2651. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2652. PLANE_CTL_PIPE_CSC_ENABLE;
  2653. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2654. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2655. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2656. rotation = plane->state->rotation;
  2657. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2658. obj = intel_fb_obj(fb);
  2659. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2660. fb->pixel_format);
  2661. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
  2662. /*
  2663. * FIXME: intel_plane_state->src, dst aren't set when transitional
  2664. * update_plane helpers are called from legacy paths.
  2665. * Once full atomic crtc is available, below check can be avoided.
  2666. */
  2667. if (drm_rect_width(&plane_state->src)) {
  2668. scaler_id = plane_state->scaler_id;
  2669. src_x = plane_state->src.x1 >> 16;
  2670. src_y = plane_state->src.y1 >> 16;
  2671. src_w = drm_rect_width(&plane_state->src) >> 16;
  2672. src_h = drm_rect_height(&plane_state->src) >> 16;
  2673. dst_x = plane_state->dst.x1;
  2674. dst_y = plane_state->dst.y1;
  2675. dst_w = drm_rect_width(&plane_state->dst);
  2676. dst_h = drm_rect_height(&plane_state->dst);
  2677. WARN_ON(x != src_x || y != src_y);
  2678. } else {
  2679. src_w = intel_crtc->config->pipe_src_w;
  2680. src_h = intel_crtc->config->pipe_src_h;
  2681. }
  2682. if (intel_rotation_90_or_270(rotation)) {
  2683. /* stride = Surface height in tiles */
  2684. tile_height = intel_tile_height(dev, fb->pixel_format,
  2685. fb->modifier[0]);
  2686. stride = DIV_ROUND_UP(fb->height, tile_height);
  2687. x_offset = stride * tile_height - y - src_h;
  2688. y_offset = x;
  2689. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2690. } else {
  2691. stride = fb->pitches[0] / stride_div;
  2692. x_offset = x;
  2693. y_offset = y;
  2694. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2695. }
  2696. plane_offset = y_offset << 16 | x_offset;
  2697. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2698. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2699. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2700. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2701. if (scaler_id >= 0) {
  2702. uint32_t ps_ctrl = 0;
  2703. WARN_ON(!dst_w || !dst_h);
  2704. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2705. crtc_state->scaler_state.scalers[scaler_id].mode;
  2706. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2707. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2708. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2709. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2710. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2711. } else {
  2712. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2713. }
  2714. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2715. POSTING_READ(PLANE_SURF(pipe, 0));
  2716. }
  2717. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2718. static int
  2719. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2720. int x, int y, enum mode_set_atomic state)
  2721. {
  2722. struct drm_device *dev = crtc->dev;
  2723. struct drm_i915_private *dev_priv = dev->dev_private;
  2724. if (dev_priv->display.disable_fbc)
  2725. dev_priv->display.disable_fbc(dev);
  2726. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2727. return 0;
  2728. }
  2729. static void intel_complete_page_flips(struct drm_device *dev)
  2730. {
  2731. struct drm_crtc *crtc;
  2732. for_each_crtc(dev, crtc) {
  2733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2734. enum plane plane = intel_crtc->plane;
  2735. intel_prepare_page_flip(dev, plane);
  2736. intel_finish_page_flip_plane(dev, plane);
  2737. }
  2738. }
  2739. static void intel_update_primary_planes(struct drm_device *dev)
  2740. {
  2741. struct drm_i915_private *dev_priv = dev->dev_private;
  2742. struct drm_crtc *crtc;
  2743. for_each_crtc(dev, crtc) {
  2744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2745. drm_modeset_lock(&crtc->mutex, NULL);
  2746. /*
  2747. * FIXME: Once we have proper support for primary planes (and
  2748. * disabling them without disabling the entire crtc) allow again
  2749. * a NULL crtc->primary->fb.
  2750. */
  2751. if (intel_crtc->active && crtc->primary->fb)
  2752. dev_priv->display.update_primary_plane(crtc,
  2753. crtc->primary->fb,
  2754. crtc->x,
  2755. crtc->y);
  2756. drm_modeset_unlock(&crtc->mutex);
  2757. }
  2758. }
  2759. void intel_prepare_reset(struct drm_device *dev)
  2760. {
  2761. /* no reset support for gen2 */
  2762. if (IS_GEN2(dev))
  2763. return;
  2764. /* reset doesn't touch the display */
  2765. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2766. return;
  2767. drm_modeset_lock_all(dev);
  2768. /*
  2769. * Disabling the crtcs gracefully seems nicer. Also the
  2770. * g33 docs say we should at least disable all the planes.
  2771. */
  2772. intel_display_suspend(dev);
  2773. }
  2774. void intel_finish_reset(struct drm_device *dev)
  2775. {
  2776. struct drm_i915_private *dev_priv = to_i915(dev);
  2777. /*
  2778. * Flips in the rings will be nuked by the reset,
  2779. * so complete all pending flips so that user space
  2780. * will get its events and not get stuck.
  2781. */
  2782. intel_complete_page_flips(dev);
  2783. /* no reset support for gen2 */
  2784. if (IS_GEN2(dev))
  2785. return;
  2786. /* reset doesn't touch the display */
  2787. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2788. /*
  2789. * Flips in the rings have been nuked by the reset,
  2790. * so update the base address of all primary
  2791. * planes to the the last fb to make sure we're
  2792. * showing the correct fb after a reset.
  2793. */
  2794. intel_update_primary_planes(dev);
  2795. return;
  2796. }
  2797. /*
  2798. * The display has been reset as well,
  2799. * so need a full re-initialization.
  2800. */
  2801. intel_runtime_pm_disable_interrupts(dev_priv);
  2802. intel_runtime_pm_enable_interrupts(dev_priv);
  2803. intel_modeset_init_hw(dev);
  2804. spin_lock_irq(&dev_priv->irq_lock);
  2805. if (dev_priv->display.hpd_irq_setup)
  2806. dev_priv->display.hpd_irq_setup(dev);
  2807. spin_unlock_irq(&dev_priv->irq_lock);
  2808. intel_modeset_setup_hw_state(dev, true);
  2809. intel_hpd_init(dev_priv);
  2810. drm_modeset_unlock_all(dev);
  2811. }
  2812. static void
  2813. intel_finish_fb(struct drm_framebuffer *old_fb)
  2814. {
  2815. struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
  2816. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  2817. bool was_interruptible = dev_priv->mm.interruptible;
  2818. int ret;
  2819. /* Big Hammer, we also need to ensure that any pending
  2820. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2821. * current scanout is retired before unpinning the old
  2822. * framebuffer. Note that we rely on userspace rendering
  2823. * into the buffer attached to the pipe they are waiting
  2824. * on. If not, userspace generates a GPU hang with IPEHR
  2825. * point to the MI_WAIT_FOR_EVENT.
  2826. *
  2827. * This should only fail upon a hung GPU, in which case we
  2828. * can safely continue.
  2829. */
  2830. dev_priv->mm.interruptible = false;
  2831. ret = i915_gem_object_wait_rendering(obj, true);
  2832. dev_priv->mm.interruptible = was_interruptible;
  2833. WARN_ON(ret);
  2834. }
  2835. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2836. {
  2837. struct drm_device *dev = crtc->dev;
  2838. struct drm_i915_private *dev_priv = dev->dev_private;
  2839. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2840. bool pending;
  2841. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2842. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2843. return false;
  2844. spin_lock_irq(&dev->event_lock);
  2845. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2846. spin_unlock_irq(&dev->event_lock);
  2847. return pending;
  2848. }
  2849. static void intel_update_pipe_size(struct intel_crtc *crtc)
  2850. {
  2851. struct drm_device *dev = crtc->base.dev;
  2852. struct drm_i915_private *dev_priv = dev->dev_private;
  2853. const struct drm_display_mode *adjusted_mode;
  2854. if (!i915.fastboot)
  2855. return;
  2856. /*
  2857. * Update pipe size and adjust fitter if needed: the reason for this is
  2858. * that in compute_mode_changes we check the native mode (not the pfit
  2859. * mode) to see if we can flip rather than do a full mode set. In the
  2860. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2861. * pfit state, we'll end up with a big fb scanned out into the wrong
  2862. * sized surface.
  2863. *
  2864. * To fix this properly, we need to hoist the checks up into
  2865. * compute_mode_changes (or above), check the actual pfit state and
  2866. * whether the platform allows pfit disable with pipe active, and only
  2867. * then update the pipesrc and pfit state, even on the flip path.
  2868. */
  2869. adjusted_mode = &crtc->config->base.adjusted_mode;
  2870. I915_WRITE(PIPESRC(crtc->pipe),
  2871. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2872. (adjusted_mode->crtc_vdisplay - 1));
  2873. if (!crtc->config->pch_pfit.enabled &&
  2874. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2875. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2876. I915_WRITE(PF_CTL(crtc->pipe), 0);
  2877. I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
  2878. I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
  2879. }
  2880. crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
  2881. crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
  2882. }
  2883. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2884. {
  2885. struct drm_device *dev = crtc->dev;
  2886. struct drm_i915_private *dev_priv = dev->dev_private;
  2887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2888. int pipe = intel_crtc->pipe;
  2889. u32 reg, temp;
  2890. /* enable normal train */
  2891. reg = FDI_TX_CTL(pipe);
  2892. temp = I915_READ(reg);
  2893. if (IS_IVYBRIDGE(dev)) {
  2894. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2895. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2896. } else {
  2897. temp &= ~FDI_LINK_TRAIN_NONE;
  2898. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2899. }
  2900. I915_WRITE(reg, temp);
  2901. reg = FDI_RX_CTL(pipe);
  2902. temp = I915_READ(reg);
  2903. if (HAS_PCH_CPT(dev)) {
  2904. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2905. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2906. } else {
  2907. temp &= ~FDI_LINK_TRAIN_NONE;
  2908. temp |= FDI_LINK_TRAIN_NONE;
  2909. }
  2910. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2911. /* wait one idle pattern time */
  2912. POSTING_READ(reg);
  2913. udelay(1000);
  2914. /* IVB wants error correction enabled */
  2915. if (IS_IVYBRIDGE(dev))
  2916. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2917. FDI_FE_ERRC_ENABLE);
  2918. }
  2919. /* The FDI link training functions for ILK/Ibexpeak. */
  2920. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2921. {
  2922. struct drm_device *dev = crtc->dev;
  2923. struct drm_i915_private *dev_priv = dev->dev_private;
  2924. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2925. int pipe = intel_crtc->pipe;
  2926. u32 reg, temp, tries;
  2927. /* FDI needs bits from pipe first */
  2928. assert_pipe_enabled(dev_priv, pipe);
  2929. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2930. for train result */
  2931. reg = FDI_RX_IMR(pipe);
  2932. temp = I915_READ(reg);
  2933. temp &= ~FDI_RX_SYMBOL_LOCK;
  2934. temp &= ~FDI_RX_BIT_LOCK;
  2935. I915_WRITE(reg, temp);
  2936. I915_READ(reg);
  2937. udelay(150);
  2938. /* enable CPU FDI TX and PCH FDI RX */
  2939. reg = FDI_TX_CTL(pipe);
  2940. temp = I915_READ(reg);
  2941. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2942. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2943. temp &= ~FDI_LINK_TRAIN_NONE;
  2944. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2945. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2946. reg = FDI_RX_CTL(pipe);
  2947. temp = I915_READ(reg);
  2948. temp &= ~FDI_LINK_TRAIN_NONE;
  2949. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2950. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2951. POSTING_READ(reg);
  2952. udelay(150);
  2953. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2954. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2955. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2956. FDI_RX_PHASE_SYNC_POINTER_EN);
  2957. reg = FDI_RX_IIR(pipe);
  2958. for (tries = 0; tries < 5; tries++) {
  2959. temp = I915_READ(reg);
  2960. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2961. if ((temp & FDI_RX_BIT_LOCK)) {
  2962. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2963. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2964. break;
  2965. }
  2966. }
  2967. if (tries == 5)
  2968. DRM_ERROR("FDI train 1 fail!\n");
  2969. /* Train 2 */
  2970. reg = FDI_TX_CTL(pipe);
  2971. temp = I915_READ(reg);
  2972. temp &= ~FDI_LINK_TRAIN_NONE;
  2973. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2974. I915_WRITE(reg, temp);
  2975. reg = FDI_RX_CTL(pipe);
  2976. temp = I915_READ(reg);
  2977. temp &= ~FDI_LINK_TRAIN_NONE;
  2978. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2979. I915_WRITE(reg, temp);
  2980. POSTING_READ(reg);
  2981. udelay(150);
  2982. reg = FDI_RX_IIR(pipe);
  2983. for (tries = 0; tries < 5; tries++) {
  2984. temp = I915_READ(reg);
  2985. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2986. if (temp & FDI_RX_SYMBOL_LOCK) {
  2987. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2988. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2989. break;
  2990. }
  2991. }
  2992. if (tries == 5)
  2993. DRM_ERROR("FDI train 2 fail!\n");
  2994. DRM_DEBUG_KMS("FDI train done\n");
  2995. }
  2996. static const int snb_b_fdi_train_param[] = {
  2997. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2998. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2999. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3000. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3001. };
  3002. /* The FDI link training functions for SNB/Cougarpoint. */
  3003. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3004. {
  3005. struct drm_device *dev = crtc->dev;
  3006. struct drm_i915_private *dev_priv = dev->dev_private;
  3007. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3008. int pipe = intel_crtc->pipe;
  3009. u32 reg, temp, i, retry;
  3010. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3011. for train result */
  3012. reg = FDI_RX_IMR(pipe);
  3013. temp = I915_READ(reg);
  3014. temp &= ~FDI_RX_SYMBOL_LOCK;
  3015. temp &= ~FDI_RX_BIT_LOCK;
  3016. I915_WRITE(reg, temp);
  3017. POSTING_READ(reg);
  3018. udelay(150);
  3019. /* enable CPU FDI TX and PCH FDI RX */
  3020. reg = FDI_TX_CTL(pipe);
  3021. temp = I915_READ(reg);
  3022. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3023. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3024. temp &= ~FDI_LINK_TRAIN_NONE;
  3025. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3026. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3027. /* SNB-B */
  3028. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3029. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3030. I915_WRITE(FDI_RX_MISC(pipe),
  3031. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3032. reg = FDI_RX_CTL(pipe);
  3033. temp = I915_READ(reg);
  3034. if (HAS_PCH_CPT(dev)) {
  3035. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3036. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3037. } else {
  3038. temp &= ~FDI_LINK_TRAIN_NONE;
  3039. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3040. }
  3041. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3042. POSTING_READ(reg);
  3043. udelay(150);
  3044. for (i = 0; i < 4; i++) {
  3045. reg = FDI_TX_CTL(pipe);
  3046. temp = I915_READ(reg);
  3047. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3048. temp |= snb_b_fdi_train_param[i];
  3049. I915_WRITE(reg, temp);
  3050. POSTING_READ(reg);
  3051. udelay(500);
  3052. for (retry = 0; retry < 5; retry++) {
  3053. reg = FDI_RX_IIR(pipe);
  3054. temp = I915_READ(reg);
  3055. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3056. if (temp & FDI_RX_BIT_LOCK) {
  3057. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3058. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3059. break;
  3060. }
  3061. udelay(50);
  3062. }
  3063. if (retry < 5)
  3064. break;
  3065. }
  3066. if (i == 4)
  3067. DRM_ERROR("FDI train 1 fail!\n");
  3068. /* Train 2 */
  3069. reg = FDI_TX_CTL(pipe);
  3070. temp = I915_READ(reg);
  3071. temp &= ~FDI_LINK_TRAIN_NONE;
  3072. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3073. if (IS_GEN6(dev)) {
  3074. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3075. /* SNB-B */
  3076. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3077. }
  3078. I915_WRITE(reg, temp);
  3079. reg = FDI_RX_CTL(pipe);
  3080. temp = I915_READ(reg);
  3081. if (HAS_PCH_CPT(dev)) {
  3082. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3083. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3084. } else {
  3085. temp &= ~FDI_LINK_TRAIN_NONE;
  3086. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3087. }
  3088. I915_WRITE(reg, temp);
  3089. POSTING_READ(reg);
  3090. udelay(150);
  3091. for (i = 0; i < 4; i++) {
  3092. reg = FDI_TX_CTL(pipe);
  3093. temp = I915_READ(reg);
  3094. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3095. temp |= snb_b_fdi_train_param[i];
  3096. I915_WRITE(reg, temp);
  3097. POSTING_READ(reg);
  3098. udelay(500);
  3099. for (retry = 0; retry < 5; retry++) {
  3100. reg = FDI_RX_IIR(pipe);
  3101. temp = I915_READ(reg);
  3102. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3103. if (temp & FDI_RX_SYMBOL_LOCK) {
  3104. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3105. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3106. break;
  3107. }
  3108. udelay(50);
  3109. }
  3110. if (retry < 5)
  3111. break;
  3112. }
  3113. if (i == 4)
  3114. DRM_ERROR("FDI train 2 fail!\n");
  3115. DRM_DEBUG_KMS("FDI train done.\n");
  3116. }
  3117. /* Manual link training for Ivy Bridge A0 parts */
  3118. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3119. {
  3120. struct drm_device *dev = crtc->dev;
  3121. struct drm_i915_private *dev_priv = dev->dev_private;
  3122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3123. int pipe = intel_crtc->pipe;
  3124. u32 reg, temp, i, j;
  3125. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3126. for train result */
  3127. reg = FDI_RX_IMR(pipe);
  3128. temp = I915_READ(reg);
  3129. temp &= ~FDI_RX_SYMBOL_LOCK;
  3130. temp &= ~FDI_RX_BIT_LOCK;
  3131. I915_WRITE(reg, temp);
  3132. POSTING_READ(reg);
  3133. udelay(150);
  3134. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3135. I915_READ(FDI_RX_IIR(pipe)));
  3136. /* Try each vswing and preemphasis setting twice before moving on */
  3137. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3138. /* disable first in case we need to retry */
  3139. reg = FDI_TX_CTL(pipe);
  3140. temp = I915_READ(reg);
  3141. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3142. temp &= ~FDI_TX_ENABLE;
  3143. I915_WRITE(reg, temp);
  3144. reg = FDI_RX_CTL(pipe);
  3145. temp = I915_READ(reg);
  3146. temp &= ~FDI_LINK_TRAIN_AUTO;
  3147. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3148. temp &= ~FDI_RX_ENABLE;
  3149. I915_WRITE(reg, temp);
  3150. /* enable CPU FDI TX and PCH FDI RX */
  3151. reg = FDI_TX_CTL(pipe);
  3152. temp = I915_READ(reg);
  3153. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3154. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3155. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3156. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3157. temp |= snb_b_fdi_train_param[j/2];
  3158. temp |= FDI_COMPOSITE_SYNC;
  3159. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3160. I915_WRITE(FDI_RX_MISC(pipe),
  3161. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3162. reg = FDI_RX_CTL(pipe);
  3163. temp = I915_READ(reg);
  3164. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3165. temp |= FDI_COMPOSITE_SYNC;
  3166. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3167. POSTING_READ(reg);
  3168. udelay(1); /* should be 0.5us */
  3169. for (i = 0; i < 4; i++) {
  3170. reg = FDI_RX_IIR(pipe);
  3171. temp = I915_READ(reg);
  3172. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3173. if (temp & FDI_RX_BIT_LOCK ||
  3174. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3175. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3176. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3177. i);
  3178. break;
  3179. }
  3180. udelay(1); /* should be 0.5us */
  3181. }
  3182. if (i == 4) {
  3183. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3184. continue;
  3185. }
  3186. /* Train 2 */
  3187. reg = FDI_TX_CTL(pipe);
  3188. temp = I915_READ(reg);
  3189. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3190. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3191. I915_WRITE(reg, temp);
  3192. reg = FDI_RX_CTL(pipe);
  3193. temp = I915_READ(reg);
  3194. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3195. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3196. I915_WRITE(reg, temp);
  3197. POSTING_READ(reg);
  3198. udelay(2); /* should be 1.5us */
  3199. for (i = 0; i < 4; i++) {
  3200. reg = FDI_RX_IIR(pipe);
  3201. temp = I915_READ(reg);
  3202. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3203. if (temp & FDI_RX_SYMBOL_LOCK ||
  3204. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3205. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3206. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3207. i);
  3208. goto train_done;
  3209. }
  3210. udelay(2); /* should be 1.5us */
  3211. }
  3212. if (i == 4)
  3213. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3214. }
  3215. train_done:
  3216. DRM_DEBUG_KMS("FDI train done.\n");
  3217. }
  3218. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3219. {
  3220. struct drm_device *dev = intel_crtc->base.dev;
  3221. struct drm_i915_private *dev_priv = dev->dev_private;
  3222. int pipe = intel_crtc->pipe;
  3223. u32 reg, temp;
  3224. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3225. reg = FDI_RX_CTL(pipe);
  3226. temp = I915_READ(reg);
  3227. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3228. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3229. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3230. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3231. POSTING_READ(reg);
  3232. udelay(200);
  3233. /* Switch from Rawclk to PCDclk */
  3234. temp = I915_READ(reg);
  3235. I915_WRITE(reg, temp | FDI_PCDCLK);
  3236. POSTING_READ(reg);
  3237. udelay(200);
  3238. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3239. reg = FDI_TX_CTL(pipe);
  3240. temp = I915_READ(reg);
  3241. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3242. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3243. POSTING_READ(reg);
  3244. udelay(100);
  3245. }
  3246. }
  3247. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3248. {
  3249. struct drm_device *dev = intel_crtc->base.dev;
  3250. struct drm_i915_private *dev_priv = dev->dev_private;
  3251. int pipe = intel_crtc->pipe;
  3252. u32 reg, temp;
  3253. /* Switch from PCDclk to Rawclk */
  3254. reg = FDI_RX_CTL(pipe);
  3255. temp = I915_READ(reg);
  3256. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3257. /* Disable CPU FDI TX PLL */
  3258. reg = FDI_TX_CTL(pipe);
  3259. temp = I915_READ(reg);
  3260. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3261. POSTING_READ(reg);
  3262. udelay(100);
  3263. reg = FDI_RX_CTL(pipe);
  3264. temp = I915_READ(reg);
  3265. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3266. /* Wait for the clocks to turn off. */
  3267. POSTING_READ(reg);
  3268. udelay(100);
  3269. }
  3270. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3271. {
  3272. struct drm_device *dev = crtc->dev;
  3273. struct drm_i915_private *dev_priv = dev->dev_private;
  3274. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3275. int pipe = intel_crtc->pipe;
  3276. u32 reg, temp;
  3277. /* disable CPU FDI tx and PCH FDI rx */
  3278. reg = FDI_TX_CTL(pipe);
  3279. temp = I915_READ(reg);
  3280. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3281. POSTING_READ(reg);
  3282. reg = FDI_RX_CTL(pipe);
  3283. temp = I915_READ(reg);
  3284. temp &= ~(0x7 << 16);
  3285. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3286. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3287. POSTING_READ(reg);
  3288. udelay(100);
  3289. /* Ironlake workaround, disable clock pointer after downing FDI */
  3290. if (HAS_PCH_IBX(dev))
  3291. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3292. /* still set train pattern 1 */
  3293. reg = FDI_TX_CTL(pipe);
  3294. temp = I915_READ(reg);
  3295. temp &= ~FDI_LINK_TRAIN_NONE;
  3296. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3297. I915_WRITE(reg, temp);
  3298. reg = FDI_RX_CTL(pipe);
  3299. temp = I915_READ(reg);
  3300. if (HAS_PCH_CPT(dev)) {
  3301. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3302. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3303. } else {
  3304. temp &= ~FDI_LINK_TRAIN_NONE;
  3305. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3306. }
  3307. /* BPC in FDI rx is consistent with that in PIPECONF */
  3308. temp &= ~(0x07 << 16);
  3309. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3310. I915_WRITE(reg, temp);
  3311. POSTING_READ(reg);
  3312. udelay(100);
  3313. }
  3314. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3315. {
  3316. struct intel_crtc *crtc;
  3317. /* Note that we don't need to be called with mode_config.lock here
  3318. * as our list of CRTC objects is static for the lifetime of the
  3319. * device and so cannot disappear as we iterate. Similarly, we can
  3320. * happily treat the predicates as racy, atomic checks as userspace
  3321. * cannot claim and pin a new fb without at least acquring the
  3322. * struct_mutex and so serialising with us.
  3323. */
  3324. for_each_intel_crtc(dev, crtc) {
  3325. if (atomic_read(&crtc->unpin_work_count) == 0)
  3326. continue;
  3327. if (crtc->unpin_work)
  3328. intel_wait_for_vblank(dev, crtc->pipe);
  3329. return true;
  3330. }
  3331. return false;
  3332. }
  3333. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3334. {
  3335. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3336. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3337. /* ensure that the unpin work is consistent wrt ->pending. */
  3338. smp_rmb();
  3339. intel_crtc->unpin_work = NULL;
  3340. if (work->event)
  3341. drm_send_vblank_event(intel_crtc->base.dev,
  3342. intel_crtc->pipe,
  3343. work->event);
  3344. drm_crtc_vblank_put(&intel_crtc->base);
  3345. wake_up_all(&dev_priv->pending_flip_queue);
  3346. queue_work(dev_priv->wq, &work->work);
  3347. trace_i915_flip_complete(intel_crtc->plane,
  3348. work->pending_flip_obj);
  3349. }
  3350. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3351. {
  3352. struct drm_device *dev = crtc->dev;
  3353. struct drm_i915_private *dev_priv = dev->dev_private;
  3354. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3355. if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  3356. !intel_crtc_has_pending_flip(crtc),
  3357. 60*HZ) == 0)) {
  3358. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3359. spin_lock_irq(&dev->event_lock);
  3360. if (intel_crtc->unpin_work) {
  3361. WARN_ONCE(1, "Removing stuck page flip\n");
  3362. page_flip_completed(intel_crtc);
  3363. }
  3364. spin_unlock_irq(&dev->event_lock);
  3365. }
  3366. if (crtc->primary->fb) {
  3367. mutex_lock(&dev->struct_mutex);
  3368. intel_finish_fb(crtc->primary->fb);
  3369. mutex_unlock(&dev->struct_mutex);
  3370. }
  3371. }
  3372. /* Program iCLKIP clock to the desired frequency */
  3373. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3374. {
  3375. struct drm_device *dev = crtc->dev;
  3376. struct drm_i915_private *dev_priv = dev->dev_private;
  3377. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3378. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3379. u32 temp;
  3380. mutex_lock(&dev_priv->sb_lock);
  3381. /* It is necessary to ungate the pixclk gate prior to programming
  3382. * the divisors, and gate it back when it is done.
  3383. */
  3384. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3385. /* Disable SSCCTL */
  3386. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3387. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3388. SBI_SSCCTL_DISABLE,
  3389. SBI_ICLK);
  3390. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3391. if (clock == 20000) {
  3392. auxdiv = 1;
  3393. divsel = 0x41;
  3394. phaseinc = 0x20;
  3395. } else {
  3396. /* The iCLK virtual clock root frequency is in MHz,
  3397. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3398. * divisors, it is necessary to divide one by another, so we
  3399. * convert the virtual clock precision to KHz here for higher
  3400. * precision.
  3401. */
  3402. u32 iclk_virtual_root_freq = 172800 * 1000;
  3403. u32 iclk_pi_range = 64;
  3404. u32 desired_divisor, msb_divisor_value, pi_value;
  3405. desired_divisor = (iclk_virtual_root_freq / clock);
  3406. msb_divisor_value = desired_divisor / iclk_pi_range;
  3407. pi_value = desired_divisor % iclk_pi_range;
  3408. auxdiv = 0;
  3409. divsel = msb_divisor_value - 2;
  3410. phaseinc = pi_value;
  3411. }
  3412. /* This should not happen with any sane values */
  3413. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3414. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3415. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3416. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3417. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3418. clock,
  3419. auxdiv,
  3420. divsel,
  3421. phasedir,
  3422. phaseinc);
  3423. /* Program SSCDIVINTPHASE6 */
  3424. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3425. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3426. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3427. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3428. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3429. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3430. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3431. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3432. /* Program SSCAUXDIV */
  3433. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3434. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3435. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3436. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3437. /* Enable modulator and associated divider */
  3438. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3439. temp &= ~SBI_SSCCTL_DISABLE;
  3440. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3441. /* Wait for initialization time */
  3442. udelay(24);
  3443. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3444. mutex_unlock(&dev_priv->sb_lock);
  3445. }
  3446. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3447. enum pipe pch_transcoder)
  3448. {
  3449. struct drm_device *dev = crtc->base.dev;
  3450. struct drm_i915_private *dev_priv = dev->dev_private;
  3451. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3452. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3453. I915_READ(HTOTAL(cpu_transcoder)));
  3454. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3455. I915_READ(HBLANK(cpu_transcoder)));
  3456. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3457. I915_READ(HSYNC(cpu_transcoder)));
  3458. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3459. I915_READ(VTOTAL(cpu_transcoder)));
  3460. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3461. I915_READ(VBLANK(cpu_transcoder)));
  3462. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3463. I915_READ(VSYNC(cpu_transcoder)));
  3464. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3465. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3466. }
  3467. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3468. {
  3469. struct drm_i915_private *dev_priv = dev->dev_private;
  3470. uint32_t temp;
  3471. temp = I915_READ(SOUTH_CHICKEN1);
  3472. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3473. return;
  3474. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3475. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3476. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3477. if (enable)
  3478. temp |= FDI_BC_BIFURCATION_SELECT;
  3479. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3480. I915_WRITE(SOUTH_CHICKEN1, temp);
  3481. POSTING_READ(SOUTH_CHICKEN1);
  3482. }
  3483. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3484. {
  3485. struct drm_device *dev = intel_crtc->base.dev;
  3486. switch (intel_crtc->pipe) {
  3487. case PIPE_A:
  3488. break;
  3489. case PIPE_B:
  3490. if (intel_crtc->config->fdi_lanes > 2)
  3491. cpt_set_fdi_bc_bifurcation(dev, false);
  3492. else
  3493. cpt_set_fdi_bc_bifurcation(dev, true);
  3494. break;
  3495. case PIPE_C:
  3496. cpt_set_fdi_bc_bifurcation(dev, true);
  3497. break;
  3498. default:
  3499. BUG();
  3500. }
  3501. }
  3502. /*
  3503. * Enable PCH resources required for PCH ports:
  3504. * - PCH PLLs
  3505. * - FDI training & RX/TX
  3506. * - update transcoder timings
  3507. * - DP transcoding bits
  3508. * - transcoder
  3509. */
  3510. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3511. {
  3512. struct drm_device *dev = crtc->dev;
  3513. struct drm_i915_private *dev_priv = dev->dev_private;
  3514. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3515. int pipe = intel_crtc->pipe;
  3516. u32 reg, temp;
  3517. assert_pch_transcoder_disabled(dev_priv, pipe);
  3518. if (IS_IVYBRIDGE(dev))
  3519. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3520. /* Write the TU size bits before fdi link training, so that error
  3521. * detection works. */
  3522. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3523. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3524. /* For PCH output, training FDI link */
  3525. dev_priv->display.fdi_link_train(crtc);
  3526. /* We need to program the right clock selection before writing the pixel
  3527. * mutliplier into the DPLL. */
  3528. if (HAS_PCH_CPT(dev)) {
  3529. u32 sel;
  3530. temp = I915_READ(PCH_DPLL_SEL);
  3531. temp |= TRANS_DPLL_ENABLE(pipe);
  3532. sel = TRANS_DPLLB_SEL(pipe);
  3533. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3534. temp |= sel;
  3535. else
  3536. temp &= ~sel;
  3537. I915_WRITE(PCH_DPLL_SEL, temp);
  3538. }
  3539. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3540. * transcoder, and we actually should do this to not upset any PCH
  3541. * transcoder that already use the clock when we share it.
  3542. *
  3543. * Note that enable_shared_dpll tries to do the right thing, but
  3544. * get_shared_dpll unconditionally resets the pll - we need that to have
  3545. * the right LVDS enable sequence. */
  3546. intel_enable_shared_dpll(intel_crtc);
  3547. /* set transcoder timing, panel must allow it */
  3548. assert_panel_unlocked(dev_priv, pipe);
  3549. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3550. intel_fdi_normal_train(crtc);
  3551. /* For PCH DP, enable TRANS_DP_CTL */
  3552. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3553. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3554. reg = TRANS_DP_CTL(pipe);
  3555. temp = I915_READ(reg);
  3556. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3557. TRANS_DP_SYNC_MASK |
  3558. TRANS_DP_BPC_MASK);
  3559. temp |= TRANS_DP_OUTPUT_ENABLE;
  3560. temp |= bpc << 9; /* same format but at 11:9 */
  3561. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3562. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3563. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3564. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3565. switch (intel_trans_dp_port_sel(crtc)) {
  3566. case PCH_DP_B:
  3567. temp |= TRANS_DP_PORT_SEL_B;
  3568. break;
  3569. case PCH_DP_C:
  3570. temp |= TRANS_DP_PORT_SEL_C;
  3571. break;
  3572. case PCH_DP_D:
  3573. temp |= TRANS_DP_PORT_SEL_D;
  3574. break;
  3575. default:
  3576. BUG();
  3577. }
  3578. I915_WRITE(reg, temp);
  3579. }
  3580. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3581. }
  3582. static void lpt_pch_enable(struct drm_crtc *crtc)
  3583. {
  3584. struct drm_device *dev = crtc->dev;
  3585. struct drm_i915_private *dev_priv = dev->dev_private;
  3586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3587. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3588. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3589. lpt_program_iclkip(crtc);
  3590. /* Set transcoder timing. */
  3591. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3592. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3593. }
  3594. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3595. struct intel_crtc_state *crtc_state)
  3596. {
  3597. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3598. struct intel_shared_dpll *pll;
  3599. struct intel_shared_dpll_config *shared_dpll;
  3600. enum intel_dpll_id i;
  3601. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3602. if (HAS_PCH_IBX(dev_priv->dev)) {
  3603. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3604. i = (enum intel_dpll_id) crtc->pipe;
  3605. pll = &dev_priv->shared_dplls[i];
  3606. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3607. crtc->base.base.id, pll->name);
  3608. WARN_ON(shared_dpll[i].crtc_mask);
  3609. goto found;
  3610. }
  3611. if (IS_BROXTON(dev_priv->dev)) {
  3612. /* PLL is attached to port in bxt */
  3613. struct intel_encoder *encoder;
  3614. struct intel_digital_port *intel_dig_port;
  3615. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3616. if (WARN_ON(!encoder))
  3617. return NULL;
  3618. intel_dig_port = enc_to_dig_port(&encoder->base);
  3619. /* 1:1 mapping between ports and PLLs */
  3620. i = (enum intel_dpll_id)intel_dig_port->port;
  3621. pll = &dev_priv->shared_dplls[i];
  3622. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3623. crtc->base.base.id, pll->name);
  3624. WARN_ON(shared_dpll[i].crtc_mask);
  3625. goto found;
  3626. }
  3627. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3628. pll = &dev_priv->shared_dplls[i];
  3629. /* Only want to check enabled timings first */
  3630. if (shared_dpll[i].crtc_mask == 0)
  3631. continue;
  3632. if (memcmp(&crtc_state->dpll_hw_state,
  3633. &shared_dpll[i].hw_state,
  3634. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3635. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3636. crtc->base.base.id, pll->name,
  3637. shared_dpll[i].crtc_mask,
  3638. pll->active);
  3639. goto found;
  3640. }
  3641. }
  3642. /* Ok no matching timings, maybe there's a free one? */
  3643. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3644. pll = &dev_priv->shared_dplls[i];
  3645. if (shared_dpll[i].crtc_mask == 0) {
  3646. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3647. crtc->base.base.id, pll->name);
  3648. goto found;
  3649. }
  3650. }
  3651. return NULL;
  3652. found:
  3653. if (shared_dpll[i].crtc_mask == 0)
  3654. shared_dpll[i].hw_state =
  3655. crtc_state->dpll_hw_state;
  3656. crtc_state->shared_dpll = i;
  3657. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3658. pipe_name(crtc->pipe));
  3659. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3660. return pll;
  3661. }
  3662. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3663. {
  3664. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3665. struct intel_shared_dpll_config *shared_dpll;
  3666. struct intel_shared_dpll *pll;
  3667. enum intel_dpll_id i;
  3668. if (!to_intel_atomic_state(state)->dpll_set)
  3669. return;
  3670. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3671. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3672. pll = &dev_priv->shared_dplls[i];
  3673. pll->config = shared_dpll[i];
  3674. }
  3675. }
  3676. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3677. {
  3678. struct drm_i915_private *dev_priv = dev->dev_private;
  3679. int dslreg = PIPEDSL(pipe);
  3680. u32 temp;
  3681. temp = I915_READ(dslreg);
  3682. udelay(500);
  3683. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3684. if (wait_for(I915_READ(dslreg) != temp, 5))
  3685. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3686. }
  3687. }
  3688. static int
  3689. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3690. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3691. int src_w, int src_h, int dst_w, int dst_h)
  3692. {
  3693. struct intel_crtc_scaler_state *scaler_state =
  3694. &crtc_state->scaler_state;
  3695. struct intel_crtc *intel_crtc =
  3696. to_intel_crtc(crtc_state->base.crtc);
  3697. int need_scaling;
  3698. need_scaling = intel_rotation_90_or_270(rotation) ?
  3699. (src_h != dst_w || src_w != dst_h):
  3700. (src_w != dst_w || src_h != dst_h);
  3701. /*
  3702. * if plane is being disabled or scaler is no more required or force detach
  3703. * - free scaler binded to this plane/crtc
  3704. * - in order to do this, update crtc->scaler_usage
  3705. *
  3706. * Here scaler state in crtc_state is set free so that
  3707. * scaler can be assigned to other user. Actual register
  3708. * update to free the scaler is done in plane/panel-fit programming.
  3709. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3710. */
  3711. if (force_detach || !need_scaling) {
  3712. if (*scaler_id >= 0) {
  3713. scaler_state->scaler_users &= ~(1 << scaler_user);
  3714. scaler_state->scalers[*scaler_id].in_use = 0;
  3715. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3716. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3717. intel_crtc->pipe, scaler_user, *scaler_id,
  3718. scaler_state->scaler_users);
  3719. *scaler_id = -1;
  3720. }
  3721. return 0;
  3722. }
  3723. /* range checks */
  3724. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3725. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3726. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3727. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3728. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3729. "size is out of scaler range\n",
  3730. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3731. return -EINVAL;
  3732. }
  3733. /* mark this plane as a scaler user in crtc_state */
  3734. scaler_state->scaler_users |= (1 << scaler_user);
  3735. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3736. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3737. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3738. scaler_state->scaler_users);
  3739. return 0;
  3740. }
  3741. /**
  3742. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3743. *
  3744. * @state: crtc's scaler state
  3745. * @force_detach: whether to forcibly disable scaler
  3746. *
  3747. * Return
  3748. * 0 - scaler_usage updated successfully
  3749. * error - requested scaling cannot be supported or other error condition
  3750. */
  3751. int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
  3752. {
  3753. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3754. struct drm_display_mode *adjusted_mode =
  3755. &state->base.adjusted_mode;
  3756. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3757. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3758. return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
  3759. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3760. state->pipe_src_w, state->pipe_src_h,
  3761. adjusted_mode->hdisplay, adjusted_mode->hdisplay);
  3762. }
  3763. /**
  3764. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3765. *
  3766. * @state: crtc's scaler state
  3767. * @plane_state: atomic plane state to update
  3768. *
  3769. * Return
  3770. * 0 - scaler_usage updated successfully
  3771. * error - requested scaling cannot be supported or other error condition
  3772. */
  3773. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3774. struct intel_plane_state *plane_state)
  3775. {
  3776. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3777. struct intel_plane *intel_plane =
  3778. to_intel_plane(plane_state->base.plane);
  3779. struct drm_framebuffer *fb = plane_state->base.fb;
  3780. int ret;
  3781. bool force_detach = !fb || !plane_state->visible;
  3782. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3783. intel_plane->base.base.id, intel_crtc->pipe,
  3784. drm_plane_index(&intel_plane->base));
  3785. ret = skl_update_scaler(crtc_state, force_detach,
  3786. drm_plane_index(&intel_plane->base),
  3787. &plane_state->scaler_id,
  3788. plane_state->base.rotation,
  3789. drm_rect_width(&plane_state->src) >> 16,
  3790. drm_rect_height(&plane_state->src) >> 16,
  3791. drm_rect_width(&plane_state->dst),
  3792. drm_rect_height(&plane_state->dst));
  3793. if (ret || plane_state->scaler_id < 0)
  3794. return ret;
  3795. /* check colorkey */
  3796. if (WARN_ON(intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
  3797. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3798. intel_plane->base.base.id);
  3799. return -EINVAL;
  3800. }
  3801. /* Check src format */
  3802. switch (fb->pixel_format) {
  3803. case DRM_FORMAT_RGB565:
  3804. case DRM_FORMAT_XBGR8888:
  3805. case DRM_FORMAT_XRGB8888:
  3806. case DRM_FORMAT_ABGR8888:
  3807. case DRM_FORMAT_ARGB8888:
  3808. case DRM_FORMAT_XRGB2101010:
  3809. case DRM_FORMAT_XBGR2101010:
  3810. case DRM_FORMAT_YUYV:
  3811. case DRM_FORMAT_YVYU:
  3812. case DRM_FORMAT_UYVY:
  3813. case DRM_FORMAT_VYUY:
  3814. break;
  3815. default:
  3816. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3817. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3818. return -EINVAL;
  3819. }
  3820. return 0;
  3821. }
  3822. static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
  3823. {
  3824. struct drm_device *dev = crtc->base.dev;
  3825. struct drm_i915_private *dev_priv = dev->dev_private;
  3826. int pipe = crtc->pipe;
  3827. struct intel_crtc_scaler_state *scaler_state =
  3828. &crtc->config->scaler_state;
  3829. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3830. /* To update pfit, first update scaler state */
  3831. skl_update_scaler_crtc(crtc->config, !enable);
  3832. intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
  3833. skl_detach_scalers(crtc);
  3834. if (!enable)
  3835. return;
  3836. if (crtc->config->pch_pfit.enabled) {
  3837. int id;
  3838. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3839. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3840. return;
  3841. }
  3842. id = scaler_state->scaler_id;
  3843. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3844. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3845. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3846. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3847. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3848. }
  3849. }
  3850. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3851. {
  3852. struct drm_device *dev = crtc->base.dev;
  3853. struct drm_i915_private *dev_priv = dev->dev_private;
  3854. int pipe = crtc->pipe;
  3855. if (crtc->config->pch_pfit.enabled) {
  3856. /* Force use of hard-coded filter coefficients
  3857. * as some pre-programmed values are broken,
  3858. * e.g. x201.
  3859. */
  3860. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3861. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3862. PF_PIPE_SEL_IVB(pipe));
  3863. else
  3864. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3865. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3866. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3867. }
  3868. }
  3869. static void intel_enable_sprite_planes(struct drm_crtc *crtc)
  3870. {
  3871. struct drm_device *dev = crtc->dev;
  3872. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3873. struct drm_plane *plane;
  3874. struct intel_plane *intel_plane;
  3875. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3876. intel_plane = to_intel_plane(plane);
  3877. if (intel_plane->pipe == pipe)
  3878. intel_plane_restore(&intel_plane->base);
  3879. }
  3880. }
  3881. void hsw_enable_ips(struct intel_crtc *crtc)
  3882. {
  3883. struct drm_device *dev = crtc->base.dev;
  3884. struct drm_i915_private *dev_priv = dev->dev_private;
  3885. if (!crtc->config->ips_enabled)
  3886. return;
  3887. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3888. intel_wait_for_vblank(dev, crtc->pipe);
  3889. assert_plane_enabled(dev_priv, crtc->plane);
  3890. if (IS_BROADWELL(dev)) {
  3891. mutex_lock(&dev_priv->rps.hw_lock);
  3892. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3893. mutex_unlock(&dev_priv->rps.hw_lock);
  3894. /* Quoting Art Runyan: "its not safe to expect any particular
  3895. * value in IPS_CTL bit 31 after enabling IPS through the
  3896. * mailbox." Moreover, the mailbox may return a bogus state,
  3897. * so we need to just enable it and continue on.
  3898. */
  3899. } else {
  3900. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3901. /* The bit only becomes 1 in the next vblank, so this wait here
  3902. * is essentially intel_wait_for_vblank. If we don't have this
  3903. * and don't wait for vblanks until the end of crtc_enable, then
  3904. * the HW state readout code will complain that the expected
  3905. * IPS_CTL value is not the one we read. */
  3906. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3907. DRM_ERROR("Timed out waiting for IPS enable\n");
  3908. }
  3909. }
  3910. void hsw_disable_ips(struct intel_crtc *crtc)
  3911. {
  3912. struct drm_device *dev = crtc->base.dev;
  3913. struct drm_i915_private *dev_priv = dev->dev_private;
  3914. if (!crtc->config->ips_enabled)
  3915. return;
  3916. assert_plane_enabled(dev_priv, crtc->plane);
  3917. if (IS_BROADWELL(dev)) {
  3918. mutex_lock(&dev_priv->rps.hw_lock);
  3919. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3920. mutex_unlock(&dev_priv->rps.hw_lock);
  3921. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3922. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3923. DRM_ERROR("Timed out waiting for IPS disable\n");
  3924. } else {
  3925. I915_WRITE(IPS_CTL, 0);
  3926. POSTING_READ(IPS_CTL);
  3927. }
  3928. /* We need to wait for a vblank before we can disable the plane. */
  3929. intel_wait_for_vblank(dev, crtc->pipe);
  3930. }
  3931. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3932. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3933. {
  3934. struct drm_device *dev = crtc->dev;
  3935. struct drm_i915_private *dev_priv = dev->dev_private;
  3936. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3937. enum pipe pipe = intel_crtc->pipe;
  3938. int palreg = PALETTE(pipe);
  3939. int i;
  3940. bool reenable_ips = false;
  3941. /* The clocks have to be on to load the palette. */
  3942. if (!crtc->state->active)
  3943. return;
  3944. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3945. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3946. assert_dsi_pll_enabled(dev_priv);
  3947. else
  3948. assert_pll_enabled(dev_priv, pipe);
  3949. }
  3950. /* use legacy palette for Ironlake */
  3951. if (!HAS_GMCH_DISPLAY(dev))
  3952. palreg = LGC_PALETTE(pipe);
  3953. /* Workaround : Do not read or write the pipe palette/gamma data while
  3954. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3955. */
  3956. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3957. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3958. GAMMA_MODE_MODE_SPLIT)) {
  3959. hsw_disable_ips(intel_crtc);
  3960. reenable_ips = true;
  3961. }
  3962. for (i = 0; i < 256; i++) {
  3963. I915_WRITE(palreg + 4 * i,
  3964. (intel_crtc->lut_r[i] << 16) |
  3965. (intel_crtc->lut_g[i] << 8) |
  3966. intel_crtc->lut_b[i]);
  3967. }
  3968. if (reenable_ips)
  3969. hsw_enable_ips(intel_crtc);
  3970. }
  3971. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3972. {
  3973. if (intel_crtc->overlay) {
  3974. struct drm_device *dev = intel_crtc->base.dev;
  3975. struct drm_i915_private *dev_priv = dev->dev_private;
  3976. mutex_lock(&dev->struct_mutex);
  3977. dev_priv->mm.interruptible = false;
  3978. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3979. dev_priv->mm.interruptible = true;
  3980. mutex_unlock(&dev->struct_mutex);
  3981. }
  3982. /* Let userspace switch the overlay on again. In most cases userspace
  3983. * has to recompute where to put it anyway.
  3984. */
  3985. }
  3986. /**
  3987. * intel_post_enable_primary - Perform operations after enabling primary plane
  3988. * @crtc: the CRTC whose primary plane was just enabled
  3989. *
  3990. * Performs potentially sleeping operations that must be done after the primary
  3991. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3992. * called due to an explicit primary plane update, or due to an implicit
  3993. * re-enable that is caused when a sprite plane is updated to no longer
  3994. * completely hide the primary plane.
  3995. */
  3996. static void
  3997. intel_post_enable_primary(struct drm_crtc *crtc)
  3998. {
  3999. struct drm_device *dev = crtc->dev;
  4000. struct drm_i915_private *dev_priv = dev->dev_private;
  4001. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4002. int pipe = intel_crtc->pipe;
  4003. /*
  4004. * BDW signals flip done immediately if the plane
  4005. * is disabled, even if the plane enable is already
  4006. * armed to occur at the next vblank :(
  4007. */
  4008. if (IS_BROADWELL(dev))
  4009. intel_wait_for_vblank(dev, pipe);
  4010. /*
  4011. * FIXME IPS should be fine as long as one plane is
  4012. * enabled, but in practice it seems to have problems
  4013. * when going from primary only to sprite only and vice
  4014. * versa.
  4015. */
  4016. hsw_enable_ips(intel_crtc);
  4017. mutex_lock(&dev->struct_mutex);
  4018. intel_fbc_update(dev);
  4019. mutex_unlock(&dev->struct_mutex);
  4020. /*
  4021. * Gen2 reports pipe underruns whenever all planes are disabled.
  4022. * So don't enable underrun reporting before at least some planes
  4023. * are enabled.
  4024. * FIXME: Need to fix the logic to work when we turn off all planes
  4025. * but leave the pipe running.
  4026. */
  4027. if (IS_GEN2(dev))
  4028. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4029. /* Underruns don't raise interrupts, so check manually. */
  4030. if (HAS_GMCH_DISPLAY(dev))
  4031. i9xx_check_fifo_underruns(dev_priv);
  4032. }
  4033. /**
  4034. * intel_pre_disable_primary - Perform operations before disabling primary plane
  4035. * @crtc: the CRTC whose primary plane is to be disabled
  4036. *
  4037. * Performs potentially sleeping operations that must be done before the
  4038. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4039. * be called due to an explicit primary plane update, or due to an implicit
  4040. * disable that is caused when a sprite plane completely hides the primary
  4041. * plane.
  4042. */
  4043. static void
  4044. intel_pre_disable_primary(struct drm_crtc *crtc)
  4045. {
  4046. struct drm_device *dev = crtc->dev;
  4047. struct drm_i915_private *dev_priv = dev->dev_private;
  4048. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4049. int pipe = intel_crtc->pipe;
  4050. /*
  4051. * Gen2 reports pipe underruns whenever all planes are disabled.
  4052. * So diasble underrun reporting before all the planes get disabled.
  4053. * FIXME: Need to fix the logic to work when we turn off all planes
  4054. * but leave the pipe running.
  4055. */
  4056. if (IS_GEN2(dev))
  4057. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4058. /*
  4059. * Vblank time updates from the shadow to live plane control register
  4060. * are blocked if the memory self-refresh mode is active at that
  4061. * moment. So to make sure the plane gets truly disabled, disable
  4062. * first the self-refresh mode. The self-refresh enable bit in turn
  4063. * will be checked/applied by the HW only at the next frame start
  4064. * event which is after the vblank start event, so we need to have a
  4065. * wait-for-vblank between disabling the plane and the pipe.
  4066. */
  4067. if (HAS_GMCH_DISPLAY(dev))
  4068. intel_set_memory_cxsr(dev_priv, false);
  4069. mutex_lock(&dev->struct_mutex);
  4070. if (dev_priv->fbc.crtc == intel_crtc)
  4071. intel_fbc_disable(dev);
  4072. mutex_unlock(&dev->struct_mutex);
  4073. /*
  4074. * FIXME IPS should be fine as long as one plane is
  4075. * enabled, but in practice it seems to have problems
  4076. * when going from primary only to sprite only and vice
  4077. * versa.
  4078. */
  4079. hsw_disable_ips(intel_crtc);
  4080. }
  4081. static void intel_post_plane_update(struct intel_crtc *crtc)
  4082. {
  4083. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4084. struct drm_device *dev = crtc->base.dev;
  4085. struct drm_plane *plane;
  4086. if (atomic->wait_vblank)
  4087. intel_wait_for_vblank(dev, crtc->pipe);
  4088. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4089. if (atomic->update_fbc) {
  4090. mutex_lock(&dev->struct_mutex);
  4091. intel_fbc_update(dev);
  4092. mutex_unlock(&dev->struct_mutex);
  4093. }
  4094. if (atomic->post_enable_primary)
  4095. intel_post_enable_primary(&crtc->base);
  4096. drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
  4097. intel_update_sprite_watermarks(plane, &crtc->base,
  4098. 0, 0, 0, false, false);
  4099. memset(atomic, 0, sizeof(*atomic));
  4100. }
  4101. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4102. {
  4103. struct drm_device *dev = crtc->base.dev;
  4104. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4105. struct drm_plane *p;
  4106. /* Track fb's for any planes being disabled */
  4107. drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
  4108. struct intel_plane *plane = to_intel_plane(p);
  4109. unsigned fb_bits = 0;
  4110. switch (p->type) {
  4111. case DRM_PLANE_TYPE_PRIMARY:
  4112. fb_bits = INTEL_FRONTBUFFER_PRIMARY(plane->pipe);
  4113. break;
  4114. case DRM_PLANE_TYPE_CURSOR:
  4115. fb_bits = INTEL_FRONTBUFFER_CURSOR(plane->pipe);
  4116. break;
  4117. case DRM_PLANE_TYPE_OVERLAY:
  4118. fb_bits = INTEL_FRONTBUFFER_SPRITE(plane->pipe);
  4119. break;
  4120. }
  4121. mutex_lock(&dev->struct_mutex);
  4122. i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL, fb_bits);
  4123. mutex_unlock(&dev->struct_mutex);
  4124. }
  4125. if (atomic->wait_for_flips)
  4126. intel_crtc_wait_for_pending_flips(&crtc->base);
  4127. if (atomic->disable_fbc)
  4128. intel_fbc_disable(dev);
  4129. if (atomic->pre_disable_primary)
  4130. intel_pre_disable_primary(&crtc->base);
  4131. }
  4132. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  4133. {
  4134. struct drm_device *dev = crtc->dev;
  4135. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4136. int pipe = intel_crtc->pipe;
  4137. intel_enable_primary_hw_plane(crtc->primary, crtc);
  4138. intel_enable_sprite_planes(crtc);
  4139. if (to_intel_plane_state(crtc->cursor->state)->visible)
  4140. intel_crtc_update_cursor(crtc, true);
  4141. intel_post_enable_primary(crtc);
  4142. /*
  4143. * FIXME: Once we grow proper nuclear flip support out of this we need
  4144. * to compute the mask of flip planes precisely. For the time being
  4145. * consider this a flip to a NULL plane.
  4146. */
  4147. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4148. }
  4149. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  4150. {
  4151. struct drm_device *dev = crtc->dev;
  4152. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4153. struct intel_plane *intel_plane;
  4154. int pipe = intel_crtc->pipe;
  4155. intel_crtc_wait_for_pending_flips(crtc);
  4156. intel_pre_disable_primary(crtc);
  4157. intel_crtc_dpms_overlay_disable(intel_crtc);
  4158. for_each_intel_plane(dev, intel_plane) {
  4159. if (intel_plane->pipe == pipe) {
  4160. struct drm_crtc *from = intel_plane->base.crtc;
  4161. intel_plane->disable_plane(&intel_plane->base,
  4162. from ?: crtc);
  4163. }
  4164. }
  4165. /*
  4166. * FIXME: Once we grow proper nuclear flip support out of this we need
  4167. * to compute the mask of flip planes precisely. For the time being
  4168. * consider this a flip to a NULL plane.
  4169. */
  4170. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4171. }
  4172. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4173. {
  4174. struct drm_device *dev = crtc->dev;
  4175. struct drm_i915_private *dev_priv = dev->dev_private;
  4176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4177. struct intel_encoder *encoder;
  4178. int pipe = intel_crtc->pipe;
  4179. if (WARN_ON(intel_crtc->active))
  4180. return;
  4181. if (intel_crtc->config->has_pch_encoder)
  4182. intel_prepare_shared_dpll(intel_crtc);
  4183. if (intel_crtc->config->has_dp_encoder)
  4184. intel_dp_set_m_n(intel_crtc, M1_N1);
  4185. intel_set_pipe_timings(intel_crtc);
  4186. if (intel_crtc->config->has_pch_encoder) {
  4187. intel_cpu_transcoder_set_m_n(intel_crtc,
  4188. &intel_crtc->config->fdi_m_n, NULL);
  4189. }
  4190. ironlake_set_pipeconf(crtc);
  4191. intel_crtc->active = true;
  4192. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4193. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4194. for_each_encoder_on_crtc(dev, crtc, encoder)
  4195. if (encoder->pre_enable)
  4196. encoder->pre_enable(encoder);
  4197. if (intel_crtc->config->has_pch_encoder) {
  4198. /* Note: FDI PLL enabling _must_ be done before we enable the
  4199. * cpu pipes, hence this is separate from all the other fdi/pch
  4200. * enabling. */
  4201. ironlake_fdi_pll_enable(intel_crtc);
  4202. } else {
  4203. assert_fdi_tx_disabled(dev_priv, pipe);
  4204. assert_fdi_rx_disabled(dev_priv, pipe);
  4205. }
  4206. ironlake_pfit_enable(intel_crtc);
  4207. /*
  4208. * On ILK+ LUT must be loaded before the pipe is running but with
  4209. * clocks enabled
  4210. */
  4211. intel_crtc_load_lut(crtc);
  4212. intel_update_watermarks(crtc);
  4213. intel_enable_pipe(intel_crtc);
  4214. if (intel_crtc->config->has_pch_encoder)
  4215. ironlake_pch_enable(crtc);
  4216. assert_vblank_disabled(crtc);
  4217. drm_crtc_vblank_on(crtc);
  4218. for_each_encoder_on_crtc(dev, crtc, encoder)
  4219. encoder->enable(encoder);
  4220. if (HAS_PCH_CPT(dev))
  4221. cpt_verify_modeset(dev, intel_crtc->pipe);
  4222. }
  4223. /* IPS only exists on ULT machines and is tied to pipe A. */
  4224. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4225. {
  4226. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4227. }
  4228. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4229. {
  4230. struct drm_device *dev = crtc->dev;
  4231. struct drm_i915_private *dev_priv = dev->dev_private;
  4232. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4233. struct intel_encoder *encoder;
  4234. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4235. struct intel_crtc_state *pipe_config =
  4236. to_intel_crtc_state(crtc->state);
  4237. if (WARN_ON(intel_crtc->active))
  4238. return;
  4239. if (intel_crtc_to_shared_dpll(intel_crtc))
  4240. intel_enable_shared_dpll(intel_crtc);
  4241. if (intel_crtc->config->has_dp_encoder)
  4242. intel_dp_set_m_n(intel_crtc, M1_N1);
  4243. intel_set_pipe_timings(intel_crtc);
  4244. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4245. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4246. intel_crtc->config->pixel_multiplier - 1);
  4247. }
  4248. if (intel_crtc->config->has_pch_encoder) {
  4249. intel_cpu_transcoder_set_m_n(intel_crtc,
  4250. &intel_crtc->config->fdi_m_n, NULL);
  4251. }
  4252. haswell_set_pipeconf(crtc);
  4253. intel_set_pipe_csc(crtc);
  4254. intel_crtc->active = true;
  4255. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4256. for_each_encoder_on_crtc(dev, crtc, encoder)
  4257. if (encoder->pre_enable)
  4258. encoder->pre_enable(encoder);
  4259. if (intel_crtc->config->has_pch_encoder) {
  4260. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4261. true);
  4262. dev_priv->display.fdi_link_train(crtc);
  4263. }
  4264. intel_ddi_enable_pipe_clock(intel_crtc);
  4265. if (INTEL_INFO(dev)->gen == 9)
  4266. skylake_pfit_update(intel_crtc, 1);
  4267. else if (INTEL_INFO(dev)->gen < 9)
  4268. ironlake_pfit_enable(intel_crtc);
  4269. else
  4270. MISSING_CASE(INTEL_INFO(dev)->gen);
  4271. /*
  4272. * On ILK+ LUT must be loaded before the pipe is running but with
  4273. * clocks enabled
  4274. */
  4275. intel_crtc_load_lut(crtc);
  4276. intel_ddi_set_pipe_settings(crtc);
  4277. intel_ddi_enable_transcoder_func(crtc);
  4278. intel_update_watermarks(crtc);
  4279. intel_enable_pipe(intel_crtc);
  4280. if (intel_crtc->config->has_pch_encoder)
  4281. lpt_pch_enable(crtc);
  4282. if (intel_crtc->config->dp_encoder_is_mst)
  4283. intel_ddi_set_vc_payload_alloc(crtc, true);
  4284. assert_vblank_disabled(crtc);
  4285. drm_crtc_vblank_on(crtc);
  4286. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4287. encoder->enable(encoder);
  4288. intel_opregion_notify_encoder(encoder, true);
  4289. }
  4290. /* If we change the relative order between pipe/planes enabling, we need
  4291. * to change the workaround. */
  4292. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4293. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4294. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4295. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4296. }
  4297. }
  4298. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  4299. {
  4300. struct drm_device *dev = crtc->base.dev;
  4301. struct drm_i915_private *dev_priv = dev->dev_private;
  4302. int pipe = crtc->pipe;
  4303. /* To avoid upsetting the power well on haswell only disable the pfit if
  4304. * it's in use. The hw state code will make sure we get this right. */
  4305. if (crtc->config->pch_pfit.enabled) {
  4306. I915_WRITE(PF_CTL(pipe), 0);
  4307. I915_WRITE(PF_WIN_POS(pipe), 0);
  4308. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4309. }
  4310. }
  4311. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4312. {
  4313. struct drm_device *dev = crtc->dev;
  4314. struct drm_i915_private *dev_priv = dev->dev_private;
  4315. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4316. struct intel_encoder *encoder;
  4317. int pipe = intel_crtc->pipe;
  4318. u32 reg, temp;
  4319. if (WARN_ON(!intel_crtc->active))
  4320. return;
  4321. for_each_encoder_on_crtc(dev, crtc, encoder)
  4322. encoder->disable(encoder);
  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, pipe, false);
  4327. intel_disable_pipe(intel_crtc);
  4328. ironlake_pfit_disable(intel_crtc);
  4329. if (intel_crtc->config->has_pch_encoder)
  4330. ironlake_fdi_disable(crtc);
  4331. for_each_encoder_on_crtc(dev, crtc, encoder)
  4332. if (encoder->post_disable)
  4333. encoder->post_disable(encoder);
  4334. if (intel_crtc->config->has_pch_encoder) {
  4335. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4336. if (HAS_PCH_CPT(dev)) {
  4337. /* disable TRANS_DP_CTL */
  4338. reg = TRANS_DP_CTL(pipe);
  4339. temp = I915_READ(reg);
  4340. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4341. TRANS_DP_PORT_SEL_MASK);
  4342. temp |= TRANS_DP_PORT_SEL_NONE;
  4343. I915_WRITE(reg, temp);
  4344. /* disable DPLL_SEL */
  4345. temp = I915_READ(PCH_DPLL_SEL);
  4346. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4347. I915_WRITE(PCH_DPLL_SEL, temp);
  4348. }
  4349. /* disable PCH DPLL */
  4350. intel_disable_shared_dpll(intel_crtc);
  4351. ironlake_fdi_pll_disable(intel_crtc);
  4352. }
  4353. intel_crtc->active = false;
  4354. intel_update_watermarks(crtc);
  4355. mutex_lock(&dev->struct_mutex);
  4356. intel_fbc_update(dev);
  4357. mutex_unlock(&dev->struct_mutex);
  4358. }
  4359. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4360. {
  4361. struct drm_device *dev = crtc->dev;
  4362. struct drm_i915_private *dev_priv = dev->dev_private;
  4363. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4364. struct intel_encoder *encoder;
  4365. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4366. if (WARN_ON(!intel_crtc->active))
  4367. return;
  4368. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4369. intel_opregion_notify_encoder(encoder, false);
  4370. encoder->disable(encoder);
  4371. }
  4372. drm_crtc_vblank_off(crtc);
  4373. assert_vblank_disabled(crtc);
  4374. if (intel_crtc->config->has_pch_encoder)
  4375. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4376. false);
  4377. intel_disable_pipe(intel_crtc);
  4378. if (intel_crtc->config->dp_encoder_is_mst)
  4379. intel_ddi_set_vc_payload_alloc(crtc, false);
  4380. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4381. if (INTEL_INFO(dev)->gen == 9)
  4382. skylake_pfit_update(intel_crtc, 0);
  4383. else if (INTEL_INFO(dev)->gen < 9)
  4384. ironlake_pfit_disable(intel_crtc);
  4385. else
  4386. MISSING_CASE(INTEL_INFO(dev)->gen);
  4387. intel_ddi_disable_pipe_clock(intel_crtc);
  4388. if (intel_crtc->config->has_pch_encoder) {
  4389. lpt_disable_pch_transcoder(dev_priv);
  4390. intel_ddi_fdi_disable(crtc);
  4391. }
  4392. for_each_encoder_on_crtc(dev, crtc, encoder)
  4393. if (encoder->post_disable)
  4394. encoder->post_disable(encoder);
  4395. intel_crtc->active = false;
  4396. intel_update_watermarks(crtc);
  4397. mutex_lock(&dev->struct_mutex);
  4398. intel_fbc_update(dev);
  4399. mutex_unlock(&dev->struct_mutex);
  4400. if (intel_crtc_to_shared_dpll(intel_crtc))
  4401. intel_disable_shared_dpll(intel_crtc);
  4402. }
  4403. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4404. {
  4405. struct drm_device *dev = crtc->base.dev;
  4406. struct drm_i915_private *dev_priv = dev->dev_private;
  4407. struct intel_crtc_state *pipe_config = crtc->config;
  4408. if (!pipe_config->gmch_pfit.control)
  4409. return;
  4410. /*
  4411. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4412. * according to register description and PRM.
  4413. */
  4414. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4415. assert_pipe_disabled(dev_priv, crtc->pipe);
  4416. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4417. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4418. /* Border color in case we don't scale up to the full screen. Black by
  4419. * default, change to something else for debugging. */
  4420. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4421. }
  4422. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4423. {
  4424. switch (port) {
  4425. case PORT_A:
  4426. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  4427. case PORT_B:
  4428. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  4429. case PORT_C:
  4430. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  4431. case PORT_D:
  4432. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  4433. default:
  4434. WARN_ON_ONCE(1);
  4435. return POWER_DOMAIN_PORT_OTHER;
  4436. }
  4437. }
  4438. #define for_each_power_domain(domain, mask) \
  4439. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4440. if ((1 << (domain)) & (mask))
  4441. enum intel_display_power_domain
  4442. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4443. {
  4444. struct drm_device *dev = intel_encoder->base.dev;
  4445. struct intel_digital_port *intel_dig_port;
  4446. switch (intel_encoder->type) {
  4447. case INTEL_OUTPUT_UNKNOWN:
  4448. /* Only DDI platforms should ever use this output type */
  4449. WARN_ON_ONCE(!HAS_DDI(dev));
  4450. case INTEL_OUTPUT_DISPLAYPORT:
  4451. case INTEL_OUTPUT_HDMI:
  4452. case INTEL_OUTPUT_EDP:
  4453. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4454. return port_to_power_domain(intel_dig_port->port);
  4455. case INTEL_OUTPUT_DP_MST:
  4456. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4457. return port_to_power_domain(intel_dig_port->port);
  4458. case INTEL_OUTPUT_ANALOG:
  4459. return POWER_DOMAIN_PORT_CRT;
  4460. case INTEL_OUTPUT_DSI:
  4461. return POWER_DOMAIN_PORT_DSI;
  4462. default:
  4463. return POWER_DOMAIN_PORT_OTHER;
  4464. }
  4465. }
  4466. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4467. {
  4468. struct drm_device *dev = crtc->dev;
  4469. struct intel_encoder *intel_encoder;
  4470. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4471. enum pipe pipe = intel_crtc->pipe;
  4472. unsigned long mask;
  4473. enum transcoder transcoder;
  4474. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  4475. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4476. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4477. if (intel_crtc->config->pch_pfit.enabled ||
  4478. intel_crtc->config->pch_pfit.force_thru)
  4479. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4480. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4481. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4482. return mask;
  4483. }
  4484. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4485. {
  4486. struct drm_device *dev = state->dev;
  4487. struct drm_i915_private *dev_priv = dev->dev_private;
  4488. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  4489. struct intel_crtc *crtc;
  4490. /*
  4491. * First get all needed power domains, then put all unneeded, to avoid
  4492. * any unnecessary toggling of the power wells.
  4493. */
  4494. for_each_intel_crtc(dev, crtc) {
  4495. enum intel_display_power_domain domain;
  4496. if (!crtc->base.state->enable)
  4497. continue;
  4498. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  4499. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  4500. intel_display_power_get(dev_priv, domain);
  4501. }
  4502. if (dev_priv->display.modeset_global_resources)
  4503. dev_priv->display.modeset_global_resources(state);
  4504. for_each_intel_crtc(dev, crtc) {
  4505. enum intel_display_power_domain domain;
  4506. for_each_power_domain(domain, crtc->enabled_power_domains)
  4507. intel_display_power_put(dev_priv, domain);
  4508. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  4509. }
  4510. intel_display_set_init_power(dev_priv, false);
  4511. }
  4512. static void intel_update_max_cdclk(struct drm_device *dev)
  4513. {
  4514. struct drm_i915_private *dev_priv = dev->dev_private;
  4515. if (IS_SKYLAKE(dev)) {
  4516. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4517. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4518. dev_priv->max_cdclk_freq = 675000;
  4519. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4520. dev_priv->max_cdclk_freq = 540000;
  4521. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4522. dev_priv->max_cdclk_freq = 450000;
  4523. else
  4524. dev_priv->max_cdclk_freq = 337500;
  4525. } else if (IS_BROADWELL(dev)) {
  4526. /*
  4527. * FIXME with extra cooling we can allow
  4528. * 540 MHz for ULX and 675 Mhz for ULT.
  4529. * How can we know if extra cooling is
  4530. * available? PCI ID, VTB, something else?
  4531. */
  4532. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4533. dev_priv->max_cdclk_freq = 450000;
  4534. else if (IS_BDW_ULX(dev))
  4535. dev_priv->max_cdclk_freq = 450000;
  4536. else if (IS_BDW_ULT(dev))
  4537. dev_priv->max_cdclk_freq = 540000;
  4538. else
  4539. dev_priv->max_cdclk_freq = 675000;
  4540. } else if (IS_CHERRYVIEW(dev)) {
  4541. dev_priv->max_cdclk_freq = 320000;
  4542. } else if (IS_VALLEYVIEW(dev)) {
  4543. dev_priv->max_cdclk_freq = 400000;
  4544. } else {
  4545. /* otherwise assume cdclk is fixed */
  4546. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4547. }
  4548. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4549. dev_priv->max_cdclk_freq);
  4550. }
  4551. static void intel_update_cdclk(struct drm_device *dev)
  4552. {
  4553. struct drm_i915_private *dev_priv = dev->dev_private;
  4554. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4555. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4556. dev_priv->cdclk_freq);
  4557. /*
  4558. * Program the gmbus_freq based on the cdclk frequency.
  4559. * BSpec erroneously claims we should aim for 4MHz, but
  4560. * in fact 1MHz is the correct frequency.
  4561. */
  4562. if (IS_VALLEYVIEW(dev)) {
  4563. /*
  4564. * Program the gmbus_freq based on the cdclk frequency.
  4565. * BSpec erroneously claims we should aim for 4MHz, but
  4566. * in fact 1MHz is the correct frequency.
  4567. */
  4568. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4569. }
  4570. if (dev_priv->max_cdclk_freq == 0)
  4571. intel_update_max_cdclk(dev);
  4572. }
  4573. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4574. {
  4575. struct drm_i915_private *dev_priv = dev->dev_private;
  4576. uint32_t divider;
  4577. uint32_t ratio;
  4578. uint32_t current_freq;
  4579. int ret;
  4580. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4581. switch (frequency) {
  4582. case 144000:
  4583. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4584. ratio = BXT_DE_PLL_RATIO(60);
  4585. break;
  4586. case 288000:
  4587. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4588. ratio = BXT_DE_PLL_RATIO(60);
  4589. break;
  4590. case 384000:
  4591. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4592. ratio = BXT_DE_PLL_RATIO(60);
  4593. break;
  4594. case 576000:
  4595. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4596. ratio = BXT_DE_PLL_RATIO(60);
  4597. break;
  4598. case 624000:
  4599. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4600. ratio = BXT_DE_PLL_RATIO(65);
  4601. break;
  4602. case 19200:
  4603. /*
  4604. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4605. * to suppress GCC warning.
  4606. */
  4607. ratio = 0;
  4608. divider = 0;
  4609. break;
  4610. default:
  4611. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4612. return;
  4613. }
  4614. mutex_lock(&dev_priv->rps.hw_lock);
  4615. /* Inform power controller of upcoming frequency change */
  4616. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4617. 0x80000000);
  4618. mutex_unlock(&dev_priv->rps.hw_lock);
  4619. if (ret) {
  4620. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4621. ret, frequency);
  4622. return;
  4623. }
  4624. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4625. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4626. current_freq = current_freq * 500 + 1000;
  4627. /*
  4628. * DE PLL has to be disabled when
  4629. * - setting to 19.2MHz (bypass, PLL isn't used)
  4630. * - before setting to 624MHz (PLL needs toggling)
  4631. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4632. */
  4633. if (frequency == 19200 || frequency == 624000 ||
  4634. current_freq == 624000) {
  4635. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4636. /* Timeout 200us */
  4637. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4638. 1))
  4639. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4640. }
  4641. if (frequency != 19200) {
  4642. uint32_t val;
  4643. val = I915_READ(BXT_DE_PLL_CTL);
  4644. val &= ~BXT_DE_PLL_RATIO_MASK;
  4645. val |= ratio;
  4646. I915_WRITE(BXT_DE_PLL_CTL, val);
  4647. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4648. /* Timeout 200us */
  4649. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4650. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4651. val = I915_READ(CDCLK_CTL);
  4652. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4653. val |= divider;
  4654. /*
  4655. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4656. * enable otherwise.
  4657. */
  4658. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4659. if (frequency >= 500000)
  4660. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4661. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4662. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4663. val |= (frequency - 1000) / 500;
  4664. I915_WRITE(CDCLK_CTL, val);
  4665. }
  4666. mutex_lock(&dev_priv->rps.hw_lock);
  4667. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4668. DIV_ROUND_UP(frequency, 25000));
  4669. mutex_unlock(&dev_priv->rps.hw_lock);
  4670. if (ret) {
  4671. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4672. ret, frequency);
  4673. return;
  4674. }
  4675. intel_update_cdclk(dev);
  4676. }
  4677. void broxton_init_cdclk(struct drm_device *dev)
  4678. {
  4679. struct drm_i915_private *dev_priv = dev->dev_private;
  4680. uint32_t val;
  4681. /*
  4682. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4683. * or else the reset will hang because there is no PCH to respond.
  4684. * Move the handshake programming to initialization sequence.
  4685. * Previously was left up to BIOS.
  4686. */
  4687. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4688. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4689. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4690. /* Enable PG1 for cdclk */
  4691. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4692. /* check if cd clock is enabled */
  4693. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4694. DRM_DEBUG_KMS("Display already initialized\n");
  4695. return;
  4696. }
  4697. /*
  4698. * FIXME:
  4699. * - The initial CDCLK needs to be read from VBT.
  4700. * Need to make this change after VBT has changes for BXT.
  4701. * - check if setting the max (or any) cdclk freq is really necessary
  4702. * here, it belongs to modeset time
  4703. */
  4704. broxton_set_cdclk(dev, 624000);
  4705. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4706. POSTING_READ(DBUF_CTL);
  4707. udelay(10);
  4708. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4709. DRM_ERROR("DBuf power enable timeout!\n");
  4710. }
  4711. void broxton_uninit_cdclk(struct drm_device *dev)
  4712. {
  4713. struct drm_i915_private *dev_priv = dev->dev_private;
  4714. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4715. POSTING_READ(DBUF_CTL);
  4716. udelay(10);
  4717. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4718. DRM_ERROR("DBuf power disable timeout!\n");
  4719. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4720. broxton_set_cdclk(dev, 19200);
  4721. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4722. }
  4723. static const struct skl_cdclk_entry {
  4724. unsigned int freq;
  4725. unsigned int vco;
  4726. } skl_cdclk_frequencies[] = {
  4727. { .freq = 308570, .vco = 8640 },
  4728. { .freq = 337500, .vco = 8100 },
  4729. { .freq = 432000, .vco = 8640 },
  4730. { .freq = 450000, .vco = 8100 },
  4731. { .freq = 540000, .vco = 8100 },
  4732. { .freq = 617140, .vco = 8640 },
  4733. { .freq = 675000, .vco = 8100 },
  4734. };
  4735. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4736. {
  4737. return (freq - 1000) / 500;
  4738. }
  4739. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4740. {
  4741. unsigned int i;
  4742. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4743. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4744. if (e->freq == freq)
  4745. return e->vco;
  4746. }
  4747. return 8100;
  4748. }
  4749. static void
  4750. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4751. {
  4752. unsigned int min_freq;
  4753. u32 val;
  4754. /* select the minimum CDCLK before enabling DPLL 0 */
  4755. val = I915_READ(CDCLK_CTL);
  4756. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4757. val |= CDCLK_FREQ_337_308;
  4758. if (required_vco == 8640)
  4759. min_freq = 308570;
  4760. else
  4761. min_freq = 337500;
  4762. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4763. I915_WRITE(CDCLK_CTL, val);
  4764. POSTING_READ(CDCLK_CTL);
  4765. /*
  4766. * We always enable DPLL0 with the lowest link rate possible, but still
  4767. * taking into account the VCO required to operate the eDP panel at the
  4768. * desired frequency. The usual DP link rates operate with a VCO of
  4769. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4770. * The modeset code is responsible for the selection of the exact link
  4771. * rate later on, with the constraint of choosing a frequency that
  4772. * works with required_vco.
  4773. */
  4774. val = I915_READ(DPLL_CTRL1);
  4775. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4776. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4777. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4778. if (required_vco == 8640)
  4779. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4780. SKL_DPLL0);
  4781. else
  4782. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4783. SKL_DPLL0);
  4784. I915_WRITE(DPLL_CTRL1, val);
  4785. POSTING_READ(DPLL_CTRL1);
  4786. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4787. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4788. DRM_ERROR("DPLL0 not locked\n");
  4789. }
  4790. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4791. {
  4792. int ret;
  4793. u32 val;
  4794. /* inform PCU we want to change CDCLK */
  4795. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4796. mutex_lock(&dev_priv->rps.hw_lock);
  4797. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4798. mutex_unlock(&dev_priv->rps.hw_lock);
  4799. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4800. }
  4801. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4802. {
  4803. unsigned int i;
  4804. for (i = 0; i < 15; i++) {
  4805. if (skl_cdclk_pcu_ready(dev_priv))
  4806. return true;
  4807. udelay(10);
  4808. }
  4809. return false;
  4810. }
  4811. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4812. {
  4813. struct drm_device *dev = dev_priv->dev;
  4814. u32 freq_select, pcu_ack;
  4815. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4816. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4817. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4818. return;
  4819. }
  4820. /* set CDCLK_CTL */
  4821. switch(freq) {
  4822. case 450000:
  4823. case 432000:
  4824. freq_select = CDCLK_FREQ_450_432;
  4825. pcu_ack = 1;
  4826. break;
  4827. case 540000:
  4828. freq_select = CDCLK_FREQ_540;
  4829. pcu_ack = 2;
  4830. break;
  4831. case 308570:
  4832. case 337500:
  4833. default:
  4834. freq_select = CDCLK_FREQ_337_308;
  4835. pcu_ack = 0;
  4836. break;
  4837. case 617140:
  4838. case 675000:
  4839. freq_select = CDCLK_FREQ_675_617;
  4840. pcu_ack = 3;
  4841. break;
  4842. }
  4843. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4844. POSTING_READ(CDCLK_CTL);
  4845. /* inform PCU of the change */
  4846. mutex_lock(&dev_priv->rps.hw_lock);
  4847. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4848. mutex_unlock(&dev_priv->rps.hw_lock);
  4849. intel_update_cdclk(dev);
  4850. }
  4851. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4852. {
  4853. /* disable DBUF power */
  4854. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4855. POSTING_READ(DBUF_CTL);
  4856. udelay(10);
  4857. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4858. DRM_ERROR("DBuf power disable timeout\n");
  4859. /* disable DPLL0 */
  4860. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4861. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4862. DRM_ERROR("Couldn't disable DPLL0\n");
  4863. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4864. }
  4865. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4866. {
  4867. u32 val;
  4868. unsigned int required_vco;
  4869. /* enable PCH reset handshake */
  4870. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4871. I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
  4872. /* enable PG1 and Misc I/O */
  4873. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4874. /* DPLL0 already enabed !? */
  4875. if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
  4876. DRM_DEBUG_DRIVER("DPLL0 already running\n");
  4877. return;
  4878. }
  4879. /* enable DPLL0 */
  4880. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4881. skl_dpll0_enable(dev_priv, required_vco);
  4882. /* set CDCLK to the frequency the BIOS chose */
  4883. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4884. /* enable DBUF power */
  4885. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4886. POSTING_READ(DBUF_CTL);
  4887. udelay(10);
  4888. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4889. DRM_ERROR("DBuf power enable timeout\n");
  4890. }
  4891. /* returns HPLL frequency in kHz */
  4892. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  4893. {
  4894. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  4895. /* Obtain SKU information */
  4896. mutex_lock(&dev_priv->sb_lock);
  4897. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  4898. CCK_FUSE_HPLL_FREQ_MASK;
  4899. mutex_unlock(&dev_priv->sb_lock);
  4900. return vco_freq[hpll_freq] * 1000;
  4901. }
  4902. /* Adjust CDclk dividers to allow high res or save power if possible */
  4903. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4904. {
  4905. struct drm_i915_private *dev_priv = dev->dev_private;
  4906. u32 val, cmd;
  4907. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4908. != dev_priv->cdclk_freq);
  4909. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4910. cmd = 2;
  4911. else if (cdclk == 266667)
  4912. cmd = 1;
  4913. else
  4914. cmd = 0;
  4915. mutex_lock(&dev_priv->rps.hw_lock);
  4916. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4917. val &= ~DSPFREQGUAR_MASK;
  4918. val |= (cmd << DSPFREQGUAR_SHIFT);
  4919. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4920. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4921. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4922. 50)) {
  4923. DRM_ERROR("timed out waiting for CDclk change\n");
  4924. }
  4925. mutex_unlock(&dev_priv->rps.hw_lock);
  4926. mutex_lock(&dev_priv->sb_lock);
  4927. if (cdclk == 400000) {
  4928. u32 divider;
  4929. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4930. /* adjust cdclk divider */
  4931. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4932. val &= ~DISPLAY_FREQUENCY_VALUES;
  4933. val |= divider;
  4934. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4935. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4936. DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  4937. 50))
  4938. DRM_ERROR("timed out waiting for CDclk change\n");
  4939. }
  4940. /* adjust self-refresh exit latency value */
  4941. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4942. val &= ~0x7f;
  4943. /*
  4944. * For high bandwidth configs, we set a higher latency in the bunit
  4945. * so that the core display fetch happens in time to avoid underruns.
  4946. */
  4947. if (cdclk == 400000)
  4948. val |= 4500 / 250; /* 4.5 usec */
  4949. else
  4950. val |= 3000 / 250; /* 3.0 usec */
  4951. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4952. mutex_unlock(&dev_priv->sb_lock);
  4953. intel_update_cdclk(dev);
  4954. }
  4955. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4956. {
  4957. struct drm_i915_private *dev_priv = dev->dev_private;
  4958. u32 val, cmd;
  4959. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4960. != dev_priv->cdclk_freq);
  4961. switch (cdclk) {
  4962. case 333333:
  4963. case 320000:
  4964. case 266667:
  4965. case 200000:
  4966. break;
  4967. default:
  4968. MISSING_CASE(cdclk);
  4969. return;
  4970. }
  4971. /*
  4972. * Specs are full of misinformation, but testing on actual
  4973. * hardware has shown that we just need to write the desired
  4974. * CCK divider into the Punit register.
  4975. */
  4976. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4977. mutex_lock(&dev_priv->rps.hw_lock);
  4978. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4979. val &= ~DSPFREQGUAR_MASK_CHV;
  4980. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4981. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4982. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4983. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4984. 50)) {
  4985. DRM_ERROR("timed out waiting for CDclk change\n");
  4986. }
  4987. mutex_unlock(&dev_priv->rps.hw_lock);
  4988. intel_update_cdclk(dev);
  4989. }
  4990. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4991. int max_pixclk)
  4992. {
  4993. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4994. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4995. /*
  4996. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4997. * 200MHz
  4998. * 267MHz
  4999. * 320/333MHz (depends on HPLL freq)
  5000. * 400MHz (VLV only)
  5001. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5002. * of the lower bin and adjust if needed.
  5003. *
  5004. * We seem to get an unstable or solid color picture at 200MHz.
  5005. * Not sure what's wrong. For now use 200MHz only when all pipes
  5006. * are off.
  5007. */
  5008. if (!IS_CHERRYVIEW(dev_priv) &&
  5009. max_pixclk > freq_320*limit/100)
  5010. return 400000;
  5011. else if (max_pixclk > 266667*limit/100)
  5012. return freq_320;
  5013. else if (max_pixclk > 0)
  5014. return 266667;
  5015. else
  5016. return 200000;
  5017. }
  5018. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5019. int max_pixclk)
  5020. {
  5021. /*
  5022. * FIXME:
  5023. * - remove the guardband, it's not needed on BXT
  5024. * - set 19.2MHz bypass frequency if there are no active pipes
  5025. */
  5026. if (max_pixclk > 576000*9/10)
  5027. return 624000;
  5028. else if (max_pixclk > 384000*9/10)
  5029. return 576000;
  5030. else if (max_pixclk > 288000*9/10)
  5031. return 384000;
  5032. else if (max_pixclk > 144000*9/10)
  5033. return 288000;
  5034. else
  5035. return 144000;
  5036. }
  5037. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5038. * that's non-NULL, look at current state otherwise. */
  5039. static int intel_mode_max_pixclk(struct drm_device *dev,
  5040. struct drm_atomic_state *state)
  5041. {
  5042. struct intel_crtc *intel_crtc;
  5043. struct intel_crtc_state *crtc_state;
  5044. int max_pixclk = 0;
  5045. for_each_intel_crtc(dev, intel_crtc) {
  5046. if (state)
  5047. crtc_state =
  5048. intel_atomic_get_crtc_state(state, intel_crtc);
  5049. else
  5050. crtc_state = intel_crtc->config;
  5051. if (IS_ERR(crtc_state))
  5052. return PTR_ERR(crtc_state);
  5053. if (!crtc_state->base.enable)
  5054. continue;
  5055. max_pixclk = max(max_pixclk,
  5056. crtc_state->base.adjusted_mode.crtc_clock);
  5057. }
  5058. return max_pixclk;
  5059. }
  5060. static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
  5061. {
  5062. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5063. struct drm_crtc *crtc;
  5064. struct drm_crtc_state *crtc_state;
  5065. int max_pixclk = intel_mode_max_pixclk(state->dev, state);
  5066. int cdclk, ret = 0;
  5067. if (max_pixclk < 0)
  5068. return max_pixclk;
  5069. if (IS_VALLEYVIEW(dev_priv))
  5070. cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5071. else
  5072. cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  5073. if (cdclk == dev_priv->cdclk_freq)
  5074. return 0;
  5075. /* add all active pipes to the state */
  5076. for_each_crtc(state->dev, crtc) {
  5077. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  5078. if (IS_ERR(crtc_state))
  5079. return PTR_ERR(crtc_state);
  5080. if (!crtc_state->active || needs_modeset(crtc_state))
  5081. continue;
  5082. crtc_state->mode_changed = true;
  5083. ret = drm_atomic_add_affected_connectors(state, crtc);
  5084. if (ret)
  5085. break;
  5086. ret = drm_atomic_add_affected_planes(state, crtc);
  5087. if (ret)
  5088. break;
  5089. }
  5090. return ret;
  5091. }
  5092. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5093. {
  5094. unsigned int credits, default_credits;
  5095. if (IS_CHERRYVIEW(dev_priv))
  5096. default_credits = PFI_CREDIT(12);
  5097. else
  5098. default_credits = PFI_CREDIT(8);
  5099. if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
  5100. /* CHV suggested value is 31 or 63 */
  5101. if (IS_CHERRYVIEW(dev_priv))
  5102. credits = PFI_CREDIT_63;
  5103. else
  5104. credits = PFI_CREDIT(15);
  5105. } else {
  5106. credits = default_credits;
  5107. }
  5108. /*
  5109. * WA - write default credits before re-programming
  5110. * FIXME: should we also set the resend bit here?
  5111. */
  5112. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5113. default_credits);
  5114. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5115. credits | PFI_CREDIT_RESEND);
  5116. /*
  5117. * FIXME is this guaranteed to clear
  5118. * immediately or should we poll for it?
  5119. */
  5120. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5121. }
  5122. static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
  5123. {
  5124. struct drm_device *dev = old_state->dev;
  5125. struct drm_i915_private *dev_priv = dev->dev_private;
  5126. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  5127. int req_cdclk;
  5128. /* The path in intel_mode_max_pixclk() with a NULL atomic state should
  5129. * never fail. */
  5130. if (WARN_ON(max_pixclk < 0))
  5131. return;
  5132. req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  5133. if (req_cdclk != dev_priv->cdclk_freq) {
  5134. /*
  5135. * FIXME: We can end up here with all power domains off, yet
  5136. * with a CDCLK frequency other than the minimum. To account
  5137. * for this take the PIPE-A power domain, which covers the HW
  5138. * blocks needed for the following programming. This can be
  5139. * removed once it's guaranteed that we get here either with
  5140. * the minimum CDCLK set, or the required power domains
  5141. * enabled.
  5142. */
  5143. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5144. if (IS_CHERRYVIEW(dev))
  5145. cherryview_set_cdclk(dev, req_cdclk);
  5146. else
  5147. valleyview_set_cdclk(dev, req_cdclk);
  5148. vlv_program_pfi_credits(dev_priv);
  5149. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5150. }
  5151. }
  5152. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5153. {
  5154. struct drm_device *dev = crtc->dev;
  5155. struct drm_i915_private *dev_priv = to_i915(dev);
  5156. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5157. struct intel_encoder *encoder;
  5158. int pipe = intel_crtc->pipe;
  5159. bool is_dsi;
  5160. if (WARN_ON(intel_crtc->active))
  5161. return;
  5162. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5163. if (!is_dsi) {
  5164. if (IS_CHERRYVIEW(dev))
  5165. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5166. else
  5167. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5168. }
  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. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5173. struct drm_i915_private *dev_priv = dev->dev_private;
  5174. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5175. I915_WRITE(CHV_CANVAS(pipe), 0);
  5176. }
  5177. i9xx_set_pipeconf(intel_crtc);
  5178. intel_crtc->active = true;
  5179. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5180. for_each_encoder_on_crtc(dev, crtc, encoder)
  5181. if (encoder->pre_pll_enable)
  5182. encoder->pre_pll_enable(encoder);
  5183. if (!is_dsi) {
  5184. if (IS_CHERRYVIEW(dev))
  5185. chv_enable_pll(intel_crtc, intel_crtc->config);
  5186. else
  5187. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5188. }
  5189. for_each_encoder_on_crtc(dev, crtc, encoder)
  5190. if (encoder->pre_enable)
  5191. encoder->pre_enable(encoder);
  5192. i9xx_pfit_enable(intel_crtc);
  5193. intel_crtc_load_lut(crtc);
  5194. intel_update_watermarks(crtc);
  5195. intel_enable_pipe(intel_crtc);
  5196. assert_vblank_disabled(crtc);
  5197. drm_crtc_vblank_on(crtc);
  5198. for_each_encoder_on_crtc(dev, crtc, encoder)
  5199. encoder->enable(encoder);
  5200. }
  5201. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5202. {
  5203. struct drm_device *dev = crtc->base.dev;
  5204. struct drm_i915_private *dev_priv = dev->dev_private;
  5205. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5206. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5207. }
  5208. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5209. {
  5210. struct drm_device *dev = crtc->dev;
  5211. struct drm_i915_private *dev_priv = to_i915(dev);
  5212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5213. struct intel_encoder *encoder;
  5214. int pipe = intel_crtc->pipe;
  5215. if (WARN_ON(intel_crtc->active))
  5216. return;
  5217. i9xx_set_pll_dividers(intel_crtc);
  5218. if (intel_crtc->config->has_dp_encoder)
  5219. intel_dp_set_m_n(intel_crtc, M1_N1);
  5220. intel_set_pipe_timings(intel_crtc);
  5221. i9xx_set_pipeconf(intel_crtc);
  5222. intel_crtc->active = true;
  5223. if (!IS_GEN2(dev))
  5224. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5225. for_each_encoder_on_crtc(dev, crtc, encoder)
  5226. if (encoder->pre_enable)
  5227. encoder->pre_enable(encoder);
  5228. i9xx_enable_pll(intel_crtc);
  5229. i9xx_pfit_enable(intel_crtc);
  5230. intel_crtc_load_lut(crtc);
  5231. intel_update_watermarks(crtc);
  5232. intel_enable_pipe(intel_crtc);
  5233. assert_vblank_disabled(crtc);
  5234. drm_crtc_vblank_on(crtc);
  5235. for_each_encoder_on_crtc(dev, crtc, encoder)
  5236. encoder->enable(encoder);
  5237. }
  5238. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5239. {
  5240. struct drm_device *dev = crtc->base.dev;
  5241. struct drm_i915_private *dev_priv = dev->dev_private;
  5242. if (!crtc->config->gmch_pfit.control)
  5243. return;
  5244. assert_pipe_disabled(dev_priv, crtc->pipe);
  5245. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5246. I915_READ(PFIT_CONTROL));
  5247. I915_WRITE(PFIT_CONTROL, 0);
  5248. }
  5249. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5250. {
  5251. struct drm_device *dev = crtc->dev;
  5252. struct drm_i915_private *dev_priv = dev->dev_private;
  5253. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5254. struct intel_encoder *encoder;
  5255. int pipe = intel_crtc->pipe;
  5256. if (WARN_ON(!intel_crtc->active))
  5257. return;
  5258. /*
  5259. * On gen2 planes are double buffered but the pipe isn't, so we must
  5260. * wait for planes to fully turn off before disabling the pipe.
  5261. * We also need to wait on all gmch platforms because of the
  5262. * self-refresh mode constraint explained above.
  5263. */
  5264. intel_wait_for_vblank(dev, pipe);
  5265. for_each_encoder_on_crtc(dev, crtc, encoder)
  5266. encoder->disable(encoder);
  5267. drm_crtc_vblank_off(crtc);
  5268. assert_vblank_disabled(crtc);
  5269. intel_disable_pipe(intel_crtc);
  5270. i9xx_pfit_disable(intel_crtc);
  5271. for_each_encoder_on_crtc(dev, crtc, encoder)
  5272. if (encoder->post_disable)
  5273. encoder->post_disable(encoder);
  5274. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5275. if (IS_CHERRYVIEW(dev))
  5276. chv_disable_pll(dev_priv, pipe);
  5277. else if (IS_VALLEYVIEW(dev))
  5278. vlv_disable_pll(dev_priv, pipe);
  5279. else
  5280. i9xx_disable_pll(intel_crtc);
  5281. }
  5282. if (!IS_GEN2(dev))
  5283. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5284. intel_crtc->active = false;
  5285. intel_update_watermarks(crtc);
  5286. mutex_lock(&dev->struct_mutex);
  5287. intel_fbc_update(dev);
  5288. mutex_unlock(&dev->struct_mutex);
  5289. }
  5290. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5291. {
  5292. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5293. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5294. enum intel_display_power_domain domain;
  5295. unsigned long domains;
  5296. if (!intel_crtc->active)
  5297. return;
  5298. intel_crtc_disable_planes(crtc);
  5299. dev_priv->display.crtc_disable(crtc);
  5300. domains = intel_crtc->enabled_power_domains;
  5301. for_each_power_domain(domain, domains)
  5302. intel_display_power_put(dev_priv, domain);
  5303. intel_crtc->enabled_power_domains = 0;
  5304. }
  5305. /*
  5306. * turn all crtc's off, but do not adjust state
  5307. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5308. */
  5309. void intel_display_suspend(struct drm_device *dev)
  5310. {
  5311. struct drm_crtc *crtc;
  5312. for_each_crtc(dev, crtc)
  5313. intel_crtc_disable_noatomic(crtc);
  5314. }
  5315. /* Master function to enable/disable CRTC and corresponding power wells */
  5316. int intel_crtc_control(struct drm_crtc *crtc, bool enable)
  5317. {
  5318. struct drm_device *dev = crtc->dev;
  5319. struct drm_mode_config *config = &dev->mode_config;
  5320. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5321. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5322. struct intel_crtc_state *pipe_config;
  5323. struct drm_atomic_state *state;
  5324. int ret;
  5325. if (enable == intel_crtc->active)
  5326. return 0;
  5327. if (enable && !crtc->state->enable)
  5328. return 0;
  5329. /* this function should be called with drm_modeset_lock_all for now */
  5330. if (WARN_ON(!ctx))
  5331. return -EIO;
  5332. lockdep_assert_held(&ctx->ww_ctx);
  5333. state = drm_atomic_state_alloc(dev);
  5334. if (WARN_ON(!state))
  5335. return -ENOMEM;
  5336. state->acquire_ctx = ctx;
  5337. state->allow_modeset = true;
  5338. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  5339. if (IS_ERR(pipe_config)) {
  5340. ret = PTR_ERR(pipe_config);
  5341. goto err;
  5342. }
  5343. pipe_config->base.active = enable;
  5344. ret = intel_set_mode(state);
  5345. if (!ret)
  5346. return ret;
  5347. err:
  5348. DRM_ERROR("Updating crtc active failed with %i\n", ret);
  5349. drm_atomic_state_free(state);
  5350. return ret;
  5351. }
  5352. /**
  5353. * Sets the power management mode of the pipe and plane.
  5354. */
  5355. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  5356. {
  5357. struct drm_device *dev = crtc->dev;
  5358. struct intel_encoder *intel_encoder;
  5359. bool enable = false;
  5360. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  5361. enable |= intel_encoder->connectors_active;
  5362. intel_crtc_control(crtc, enable);
  5363. }
  5364. void intel_encoder_destroy(struct drm_encoder *encoder)
  5365. {
  5366. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5367. drm_encoder_cleanup(encoder);
  5368. kfree(intel_encoder);
  5369. }
  5370. /* Simple dpms helper for encoders with just one connector, no cloning and only
  5371. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  5372. * state of the entire output pipe. */
  5373. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  5374. {
  5375. if (mode == DRM_MODE_DPMS_ON) {
  5376. encoder->connectors_active = true;
  5377. intel_crtc_update_dpms(encoder->base.crtc);
  5378. } else {
  5379. encoder->connectors_active = false;
  5380. intel_crtc_update_dpms(encoder->base.crtc);
  5381. }
  5382. }
  5383. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5384. * internal consistency). */
  5385. static void intel_connector_check_state(struct intel_connector *connector)
  5386. {
  5387. if (connector->get_hw_state(connector)) {
  5388. struct intel_encoder *encoder = connector->encoder;
  5389. struct drm_crtc *crtc;
  5390. bool encoder_enabled;
  5391. enum pipe pipe;
  5392. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5393. connector->base.base.id,
  5394. connector->base.name);
  5395. /* there is no real hw state for MST connectors */
  5396. if (connector->mst_port)
  5397. return;
  5398. I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  5399. "wrong connector dpms state\n");
  5400. I915_STATE_WARN(connector->base.encoder != &encoder->base,
  5401. "active connector not linked to encoder\n");
  5402. if (encoder) {
  5403. I915_STATE_WARN(!encoder->connectors_active,
  5404. "encoder->connectors_active not set\n");
  5405. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  5406. I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
  5407. if (I915_STATE_WARN_ON(!encoder->base.crtc))
  5408. return;
  5409. crtc = encoder->base.crtc;
  5410. I915_STATE_WARN(!crtc->state->enable,
  5411. "crtc not enabled\n");
  5412. I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  5413. I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
  5414. "encoder active on the wrong pipe\n");
  5415. }
  5416. }
  5417. }
  5418. int intel_connector_init(struct intel_connector *connector)
  5419. {
  5420. struct drm_connector_state *connector_state;
  5421. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5422. if (!connector_state)
  5423. return -ENOMEM;
  5424. connector->base.state = connector_state;
  5425. return 0;
  5426. }
  5427. struct intel_connector *intel_connector_alloc(void)
  5428. {
  5429. struct intel_connector *connector;
  5430. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5431. if (!connector)
  5432. return NULL;
  5433. if (intel_connector_init(connector) < 0) {
  5434. kfree(connector);
  5435. return NULL;
  5436. }
  5437. return connector;
  5438. }
  5439. /* Even simpler default implementation, if there's really no special case to
  5440. * consider. */
  5441. void intel_connector_dpms(struct drm_connector *connector, int mode)
  5442. {
  5443. /* All the simple cases only support two dpms states. */
  5444. if (mode != DRM_MODE_DPMS_ON)
  5445. mode = DRM_MODE_DPMS_OFF;
  5446. if (mode == connector->dpms)
  5447. return;
  5448. connector->dpms = mode;
  5449. /* Only need to change hw state when actually enabled */
  5450. if (connector->encoder)
  5451. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  5452. intel_modeset_check_state(connector->dev);
  5453. }
  5454. /* Simple connector->get_hw_state implementation for encoders that support only
  5455. * one connector and no cloning and hence the encoder state determines the state
  5456. * of the connector. */
  5457. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5458. {
  5459. enum pipe pipe = 0;
  5460. struct intel_encoder *encoder = connector->encoder;
  5461. return encoder->get_hw_state(encoder, &pipe);
  5462. }
  5463. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5464. {
  5465. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5466. return crtc_state->fdi_lanes;
  5467. return 0;
  5468. }
  5469. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5470. struct intel_crtc_state *pipe_config)
  5471. {
  5472. struct drm_atomic_state *state = pipe_config->base.state;
  5473. struct intel_crtc *other_crtc;
  5474. struct intel_crtc_state *other_crtc_state;
  5475. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5476. pipe_name(pipe), pipe_config->fdi_lanes);
  5477. if (pipe_config->fdi_lanes > 4) {
  5478. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5479. pipe_name(pipe), pipe_config->fdi_lanes);
  5480. return -EINVAL;
  5481. }
  5482. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5483. if (pipe_config->fdi_lanes > 2) {
  5484. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5485. pipe_config->fdi_lanes);
  5486. return -EINVAL;
  5487. } else {
  5488. return 0;
  5489. }
  5490. }
  5491. if (INTEL_INFO(dev)->num_pipes == 2)
  5492. return 0;
  5493. /* Ivybridge 3 pipe is really complicated */
  5494. switch (pipe) {
  5495. case PIPE_A:
  5496. return 0;
  5497. case PIPE_B:
  5498. if (pipe_config->fdi_lanes <= 2)
  5499. return 0;
  5500. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5501. other_crtc_state =
  5502. intel_atomic_get_crtc_state(state, other_crtc);
  5503. if (IS_ERR(other_crtc_state))
  5504. return PTR_ERR(other_crtc_state);
  5505. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5506. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5507. pipe_name(pipe), pipe_config->fdi_lanes);
  5508. return -EINVAL;
  5509. }
  5510. return 0;
  5511. case PIPE_C:
  5512. if (pipe_config->fdi_lanes > 2) {
  5513. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5514. pipe_name(pipe), pipe_config->fdi_lanes);
  5515. return -EINVAL;
  5516. }
  5517. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5518. other_crtc_state =
  5519. intel_atomic_get_crtc_state(state, other_crtc);
  5520. if (IS_ERR(other_crtc_state))
  5521. return PTR_ERR(other_crtc_state);
  5522. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5523. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5524. return -EINVAL;
  5525. }
  5526. return 0;
  5527. default:
  5528. BUG();
  5529. }
  5530. }
  5531. #define RETRY 1
  5532. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5533. struct intel_crtc_state *pipe_config)
  5534. {
  5535. struct drm_device *dev = intel_crtc->base.dev;
  5536. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5537. int lane, link_bw, fdi_dotclock, ret;
  5538. bool needs_recompute = false;
  5539. retry:
  5540. /* FDI is a binary signal running at ~2.7GHz, encoding
  5541. * each output octet as 10 bits. The actual frequency
  5542. * is stored as a divider into a 100MHz clock, and the
  5543. * mode pixel clock is stored in units of 1KHz.
  5544. * Hence the bw of each lane in terms of the mode signal
  5545. * is:
  5546. */
  5547. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5548. fdi_dotclock = adjusted_mode->crtc_clock;
  5549. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5550. pipe_config->pipe_bpp);
  5551. pipe_config->fdi_lanes = lane;
  5552. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5553. link_bw, &pipe_config->fdi_m_n);
  5554. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5555. intel_crtc->pipe, pipe_config);
  5556. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5557. pipe_config->pipe_bpp -= 2*3;
  5558. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5559. pipe_config->pipe_bpp);
  5560. needs_recompute = true;
  5561. pipe_config->bw_constrained = true;
  5562. goto retry;
  5563. }
  5564. if (needs_recompute)
  5565. return RETRY;
  5566. return ret;
  5567. }
  5568. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5569. struct intel_crtc_state *pipe_config)
  5570. {
  5571. if (pipe_config->pipe_bpp > 24)
  5572. return false;
  5573. /* HSW can handle pixel rate up to cdclk? */
  5574. if (IS_HASWELL(dev_priv->dev))
  5575. return true;
  5576. /*
  5577. * We compare against max which means we must take
  5578. * the increased cdclk requirement into account when
  5579. * calculating the new cdclk.
  5580. *
  5581. * Should measure whether using a lower cdclk w/o IPS
  5582. */
  5583. return ilk_pipe_pixel_rate(pipe_config) <=
  5584. dev_priv->max_cdclk_freq * 95 / 100;
  5585. }
  5586. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5587. struct intel_crtc_state *pipe_config)
  5588. {
  5589. struct drm_device *dev = crtc->base.dev;
  5590. struct drm_i915_private *dev_priv = dev->dev_private;
  5591. pipe_config->ips_enabled = i915.enable_ips &&
  5592. hsw_crtc_supports_ips(crtc) &&
  5593. pipe_config_supports_ips(dev_priv, pipe_config);
  5594. }
  5595. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5596. struct intel_crtc_state *pipe_config)
  5597. {
  5598. struct drm_device *dev = crtc->base.dev;
  5599. struct drm_i915_private *dev_priv = dev->dev_private;
  5600. struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5601. /* FIXME should check pixel clock limits on all platforms */
  5602. if (INTEL_INFO(dev)->gen < 4) {
  5603. int clock_limit = dev_priv->max_cdclk_freq;
  5604. /*
  5605. * Enable pixel doubling when the dot clock
  5606. * is > 90% of the (display) core speed.
  5607. *
  5608. * GDG double wide on either pipe,
  5609. * otherwise pipe A only.
  5610. */
  5611. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  5612. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  5613. clock_limit *= 2;
  5614. pipe_config->double_wide = true;
  5615. }
  5616. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  5617. return -EINVAL;
  5618. }
  5619. /*
  5620. * Pipe horizontal size must be even in:
  5621. * - DVO ganged mode
  5622. * - LVDS dual channel mode
  5623. * - Double wide pipe
  5624. */
  5625. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5626. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5627. pipe_config->pipe_src_w &= ~1;
  5628. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5629. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5630. */
  5631. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5632. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  5633. return -EINVAL;
  5634. if (HAS_IPS(dev))
  5635. hsw_compute_ips_config(crtc, pipe_config);
  5636. if (pipe_config->has_pch_encoder)
  5637. return ironlake_fdi_compute_config(crtc, pipe_config);
  5638. return 0;
  5639. }
  5640. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5641. {
  5642. struct drm_i915_private *dev_priv = to_i915(dev);
  5643. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5644. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5645. uint32_t linkrate;
  5646. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5647. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5648. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5649. return 540000;
  5650. linkrate = (I915_READ(DPLL_CTRL1) &
  5651. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5652. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5653. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5654. /* vco 8640 */
  5655. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5656. case CDCLK_FREQ_450_432:
  5657. return 432000;
  5658. case CDCLK_FREQ_337_308:
  5659. return 308570;
  5660. case CDCLK_FREQ_675_617:
  5661. return 617140;
  5662. default:
  5663. WARN(1, "Unknown cd freq selection\n");
  5664. }
  5665. } else {
  5666. /* vco 8100 */
  5667. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5668. case CDCLK_FREQ_450_432:
  5669. return 450000;
  5670. case CDCLK_FREQ_337_308:
  5671. return 337500;
  5672. case CDCLK_FREQ_675_617:
  5673. return 675000;
  5674. default:
  5675. WARN(1, "Unknown cd freq selection\n");
  5676. }
  5677. }
  5678. /* error case, do as if DPLL0 isn't enabled */
  5679. return 24000;
  5680. }
  5681. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5682. {
  5683. struct drm_i915_private *dev_priv = dev->dev_private;
  5684. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5685. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5686. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5687. return 800000;
  5688. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5689. return 450000;
  5690. else if (freq == LCPLL_CLK_FREQ_450)
  5691. return 450000;
  5692. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5693. return 540000;
  5694. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5695. return 337500;
  5696. else
  5697. return 675000;
  5698. }
  5699. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5700. {
  5701. struct drm_i915_private *dev_priv = dev->dev_private;
  5702. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5703. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5704. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5705. return 800000;
  5706. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5707. return 450000;
  5708. else if (freq == LCPLL_CLK_FREQ_450)
  5709. return 450000;
  5710. else if (IS_HSW_ULT(dev))
  5711. return 337500;
  5712. else
  5713. return 540000;
  5714. }
  5715. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5716. {
  5717. struct drm_i915_private *dev_priv = dev->dev_private;
  5718. u32 val;
  5719. int divider;
  5720. if (dev_priv->hpll_freq == 0)
  5721. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  5722. mutex_lock(&dev_priv->sb_lock);
  5723. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5724. mutex_unlock(&dev_priv->sb_lock);
  5725. divider = val & DISPLAY_FREQUENCY_VALUES;
  5726. WARN((val & DISPLAY_FREQUENCY_STATUS) !=
  5727. (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
  5728. "cdclk change in progress\n");
  5729. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  5730. }
  5731. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5732. {
  5733. return 450000;
  5734. }
  5735. static int i945_get_display_clock_speed(struct drm_device *dev)
  5736. {
  5737. return 400000;
  5738. }
  5739. static int i915_get_display_clock_speed(struct drm_device *dev)
  5740. {
  5741. return 333333;
  5742. }
  5743. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5744. {
  5745. return 200000;
  5746. }
  5747. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5748. {
  5749. u16 gcfgc = 0;
  5750. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5751. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5752. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5753. return 266667;
  5754. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5755. return 333333;
  5756. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5757. return 444444;
  5758. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5759. return 200000;
  5760. default:
  5761. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5762. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5763. return 133333;
  5764. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5765. return 166667;
  5766. }
  5767. }
  5768. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5769. {
  5770. u16 gcfgc = 0;
  5771. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5772. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5773. return 133333;
  5774. else {
  5775. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5776. case GC_DISPLAY_CLOCK_333_MHZ:
  5777. return 333333;
  5778. default:
  5779. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5780. return 190000;
  5781. }
  5782. }
  5783. }
  5784. static int i865_get_display_clock_speed(struct drm_device *dev)
  5785. {
  5786. return 266667;
  5787. }
  5788. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5789. {
  5790. u16 hpllcc = 0;
  5791. /*
  5792. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5793. * encoding is different :(
  5794. * FIXME is this the right way to detect 852GM/852GMV?
  5795. */
  5796. if (dev->pdev->revision == 0x1)
  5797. return 133333;
  5798. pci_bus_read_config_word(dev->pdev->bus,
  5799. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5800. /* Assume that the hardware is in the high speed state. This
  5801. * should be the default.
  5802. */
  5803. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5804. case GC_CLOCK_133_200:
  5805. case GC_CLOCK_133_200_2:
  5806. case GC_CLOCK_100_200:
  5807. return 200000;
  5808. case GC_CLOCK_166_250:
  5809. return 250000;
  5810. case GC_CLOCK_100_133:
  5811. return 133333;
  5812. case GC_CLOCK_133_266:
  5813. case GC_CLOCK_133_266_2:
  5814. case GC_CLOCK_166_266:
  5815. return 266667;
  5816. }
  5817. /* Shouldn't happen */
  5818. return 0;
  5819. }
  5820. static int i830_get_display_clock_speed(struct drm_device *dev)
  5821. {
  5822. return 133333;
  5823. }
  5824. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5825. {
  5826. struct drm_i915_private *dev_priv = dev->dev_private;
  5827. static const unsigned int blb_vco[8] = {
  5828. [0] = 3200000,
  5829. [1] = 4000000,
  5830. [2] = 5333333,
  5831. [3] = 4800000,
  5832. [4] = 6400000,
  5833. };
  5834. static const unsigned int pnv_vco[8] = {
  5835. [0] = 3200000,
  5836. [1] = 4000000,
  5837. [2] = 5333333,
  5838. [3] = 4800000,
  5839. [4] = 2666667,
  5840. };
  5841. static const unsigned int cl_vco[8] = {
  5842. [0] = 3200000,
  5843. [1] = 4000000,
  5844. [2] = 5333333,
  5845. [3] = 6400000,
  5846. [4] = 3333333,
  5847. [5] = 3566667,
  5848. [6] = 4266667,
  5849. };
  5850. static const unsigned int elk_vco[8] = {
  5851. [0] = 3200000,
  5852. [1] = 4000000,
  5853. [2] = 5333333,
  5854. [3] = 4800000,
  5855. };
  5856. static const unsigned int ctg_vco[8] = {
  5857. [0] = 3200000,
  5858. [1] = 4000000,
  5859. [2] = 5333333,
  5860. [3] = 6400000,
  5861. [4] = 2666667,
  5862. [5] = 4266667,
  5863. };
  5864. const unsigned int *vco_table;
  5865. unsigned int vco;
  5866. uint8_t tmp = 0;
  5867. /* FIXME other chipsets? */
  5868. if (IS_GM45(dev))
  5869. vco_table = ctg_vco;
  5870. else if (IS_G4X(dev))
  5871. vco_table = elk_vco;
  5872. else if (IS_CRESTLINE(dev))
  5873. vco_table = cl_vco;
  5874. else if (IS_PINEVIEW(dev))
  5875. vco_table = pnv_vco;
  5876. else if (IS_G33(dev))
  5877. vco_table = blb_vco;
  5878. else
  5879. return 0;
  5880. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5881. vco = vco_table[tmp & 0x7];
  5882. if (vco == 0)
  5883. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5884. else
  5885. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5886. return vco;
  5887. }
  5888. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5889. {
  5890. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5891. uint16_t tmp = 0;
  5892. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5893. cdclk_sel = (tmp >> 12) & 0x1;
  5894. switch (vco) {
  5895. case 2666667:
  5896. case 4000000:
  5897. case 5333333:
  5898. return cdclk_sel ? 333333 : 222222;
  5899. case 3200000:
  5900. return cdclk_sel ? 320000 : 228571;
  5901. default:
  5902. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5903. return 222222;
  5904. }
  5905. }
  5906. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5907. {
  5908. static const uint8_t div_3200[] = { 16, 10, 8 };
  5909. static const uint8_t div_4000[] = { 20, 12, 10 };
  5910. static const uint8_t div_5333[] = { 24, 16, 14 };
  5911. const uint8_t *div_table;
  5912. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5913. uint16_t tmp = 0;
  5914. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5915. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5916. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5917. goto fail;
  5918. switch (vco) {
  5919. case 3200000:
  5920. div_table = div_3200;
  5921. break;
  5922. case 4000000:
  5923. div_table = div_4000;
  5924. break;
  5925. case 5333333:
  5926. div_table = div_5333;
  5927. break;
  5928. default:
  5929. goto fail;
  5930. }
  5931. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5932. fail:
  5933. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5934. return 200000;
  5935. }
  5936. static int g33_get_display_clock_speed(struct drm_device *dev)
  5937. {
  5938. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5939. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5940. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5941. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5942. const uint8_t *div_table;
  5943. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5944. uint16_t tmp = 0;
  5945. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5946. cdclk_sel = (tmp >> 4) & 0x7;
  5947. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5948. goto fail;
  5949. switch (vco) {
  5950. case 3200000:
  5951. div_table = div_3200;
  5952. break;
  5953. case 4000000:
  5954. div_table = div_4000;
  5955. break;
  5956. case 4800000:
  5957. div_table = div_4800;
  5958. break;
  5959. case 5333333:
  5960. div_table = div_5333;
  5961. break;
  5962. default:
  5963. goto fail;
  5964. }
  5965. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5966. fail:
  5967. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5968. return 190476;
  5969. }
  5970. static void
  5971. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5972. {
  5973. while (*num > DATA_LINK_M_N_MASK ||
  5974. *den > DATA_LINK_M_N_MASK) {
  5975. *num >>= 1;
  5976. *den >>= 1;
  5977. }
  5978. }
  5979. static void compute_m_n(unsigned int m, unsigned int n,
  5980. uint32_t *ret_m, uint32_t *ret_n)
  5981. {
  5982. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5983. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5984. intel_reduce_m_n_ratio(ret_m, ret_n);
  5985. }
  5986. void
  5987. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5988. int pixel_clock, int link_clock,
  5989. struct intel_link_m_n *m_n)
  5990. {
  5991. m_n->tu = 64;
  5992. compute_m_n(bits_per_pixel * pixel_clock,
  5993. link_clock * nlanes * 8,
  5994. &m_n->gmch_m, &m_n->gmch_n);
  5995. compute_m_n(pixel_clock, link_clock,
  5996. &m_n->link_m, &m_n->link_n);
  5997. }
  5998. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5999. {
  6000. if (i915.panel_use_ssc >= 0)
  6001. return i915.panel_use_ssc != 0;
  6002. return dev_priv->vbt.lvds_use_ssc
  6003. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6004. }
  6005. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  6006. int num_connectors)
  6007. {
  6008. struct drm_device *dev = crtc_state->base.crtc->dev;
  6009. struct drm_i915_private *dev_priv = dev->dev_private;
  6010. int refclk;
  6011. WARN_ON(!crtc_state->base.state);
  6012. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  6013. refclk = 100000;
  6014. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6015. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  6016. refclk = dev_priv->vbt.lvds_ssc_freq;
  6017. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6018. } else if (!IS_GEN2(dev)) {
  6019. refclk = 96000;
  6020. } else {
  6021. refclk = 48000;
  6022. }
  6023. return refclk;
  6024. }
  6025. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6026. {
  6027. return (1 << dpll->n) << 16 | dpll->m2;
  6028. }
  6029. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6030. {
  6031. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6032. }
  6033. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6034. struct intel_crtc_state *crtc_state,
  6035. intel_clock_t *reduced_clock)
  6036. {
  6037. struct drm_device *dev = crtc->base.dev;
  6038. u32 fp, fp2 = 0;
  6039. if (IS_PINEVIEW(dev)) {
  6040. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6041. if (reduced_clock)
  6042. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6043. } else {
  6044. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6045. if (reduced_clock)
  6046. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6047. }
  6048. crtc_state->dpll_hw_state.fp0 = fp;
  6049. crtc->lowfreq_avail = false;
  6050. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6051. reduced_clock) {
  6052. crtc_state->dpll_hw_state.fp1 = fp2;
  6053. crtc->lowfreq_avail = true;
  6054. } else {
  6055. crtc_state->dpll_hw_state.fp1 = fp;
  6056. }
  6057. }
  6058. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6059. pipe)
  6060. {
  6061. u32 reg_val;
  6062. /*
  6063. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6064. * and set it to a reasonable value instead.
  6065. */
  6066. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6067. reg_val &= 0xffffff00;
  6068. reg_val |= 0x00000030;
  6069. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6070. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6071. reg_val &= 0x8cffffff;
  6072. reg_val = 0x8c000000;
  6073. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6074. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6075. reg_val &= 0xffffff00;
  6076. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6077. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6078. reg_val &= 0x00ffffff;
  6079. reg_val |= 0xb0000000;
  6080. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6081. }
  6082. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6083. struct intel_link_m_n *m_n)
  6084. {
  6085. struct drm_device *dev = crtc->base.dev;
  6086. struct drm_i915_private *dev_priv = dev->dev_private;
  6087. int pipe = crtc->pipe;
  6088. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6089. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6090. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6091. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6092. }
  6093. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6094. struct intel_link_m_n *m_n,
  6095. struct intel_link_m_n *m2_n2)
  6096. {
  6097. struct drm_device *dev = crtc->base.dev;
  6098. struct drm_i915_private *dev_priv = dev->dev_private;
  6099. int pipe = crtc->pipe;
  6100. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6101. if (INTEL_INFO(dev)->gen >= 5) {
  6102. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6103. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6104. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6105. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6106. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6107. * for gen < 8) and if DRRS is supported (to make sure the
  6108. * registers are not unnecessarily accessed).
  6109. */
  6110. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6111. crtc->config->has_drrs) {
  6112. I915_WRITE(PIPE_DATA_M2(transcoder),
  6113. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6114. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6115. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6116. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6117. }
  6118. } else {
  6119. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6120. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6121. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6122. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6123. }
  6124. }
  6125. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6126. {
  6127. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6128. if (m_n == M1_N1) {
  6129. dp_m_n = &crtc->config->dp_m_n;
  6130. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6131. } else if (m_n == M2_N2) {
  6132. /*
  6133. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6134. * needs to be programmed into M1_N1.
  6135. */
  6136. dp_m_n = &crtc->config->dp_m2_n2;
  6137. } else {
  6138. DRM_ERROR("Unsupported divider value\n");
  6139. return;
  6140. }
  6141. if (crtc->config->has_pch_encoder)
  6142. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6143. else
  6144. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6145. }
  6146. static void vlv_update_pll(struct intel_crtc *crtc,
  6147. struct intel_crtc_state *pipe_config)
  6148. {
  6149. u32 dpll, dpll_md;
  6150. /*
  6151. * Enable DPIO clock input. We should never disable the reference
  6152. * clock for pipe B, since VGA hotplug / manual detection depends
  6153. * on it.
  6154. */
  6155. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  6156. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  6157. /* We should never disable this, set it here for state tracking */
  6158. if (crtc->pipe == PIPE_B)
  6159. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6160. dpll |= DPLL_VCO_ENABLE;
  6161. pipe_config->dpll_hw_state.dpll = dpll;
  6162. dpll_md = (pipe_config->pixel_multiplier - 1)
  6163. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6164. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6165. }
  6166. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6167. const struct intel_crtc_state *pipe_config)
  6168. {
  6169. struct drm_device *dev = crtc->base.dev;
  6170. struct drm_i915_private *dev_priv = dev->dev_private;
  6171. int pipe = crtc->pipe;
  6172. u32 mdiv;
  6173. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6174. u32 coreclk, reg_val;
  6175. mutex_lock(&dev_priv->sb_lock);
  6176. bestn = pipe_config->dpll.n;
  6177. bestm1 = pipe_config->dpll.m1;
  6178. bestm2 = pipe_config->dpll.m2;
  6179. bestp1 = pipe_config->dpll.p1;
  6180. bestp2 = pipe_config->dpll.p2;
  6181. /* See eDP HDMI DPIO driver vbios notes doc */
  6182. /* PLL B needs special handling */
  6183. if (pipe == PIPE_B)
  6184. vlv_pllb_recal_opamp(dev_priv, pipe);
  6185. /* Set up Tx target for periodic Rcomp update */
  6186. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6187. /* Disable target IRef on PLL */
  6188. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6189. reg_val &= 0x00ffffff;
  6190. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6191. /* Disable fast lock */
  6192. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6193. /* Set idtafcrecal before PLL is enabled */
  6194. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6195. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6196. mdiv |= ((bestn << DPIO_N_SHIFT));
  6197. mdiv |= (1 << DPIO_K_SHIFT);
  6198. /*
  6199. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6200. * but we don't support that).
  6201. * Note: don't use the DAC post divider as it seems unstable.
  6202. */
  6203. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6204. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6205. mdiv |= DPIO_ENABLE_CALIBRATION;
  6206. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6207. /* Set HBR and RBR LPF coefficients */
  6208. if (pipe_config->port_clock == 162000 ||
  6209. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6210. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6211. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6212. 0x009f0003);
  6213. else
  6214. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6215. 0x00d0000f);
  6216. if (pipe_config->has_dp_encoder) {
  6217. /* Use SSC source */
  6218. if (pipe == PIPE_A)
  6219. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6220. 0x0df40000);
  6221. else
  6222. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6223. 0x0df70000);
  6224. } else { /* HDMI or VGA */
  6225. /* Use bend source */
  6226. if (pipe == PIPE_A)
  6227. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6228. 0x0df70000);
  6229. else
  6230. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6231. 0x0df40000);
  6232. }
  6233. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6234. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6235. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6236. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6237. coreclk |= 0x01000000;
  6238. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6239. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6240. mutex_unlock(&dev_priv->sb_lock);
  6241. }
  6242. static void chv_update_pll(struct intel_crtc *crtc,
  6243. struct intel_crtc_state *pipe_config)
  6244. {
  6245. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  6246. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6247. DPLL_VCO_ENABLE;
  6248. if (crtc->pipe != PIPE_A)
  6249. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6250. pipe_config->dpll_hw_state.dpll_md =
  6251. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6252. }
  6253. static void chv_prepare_pll(struct intel_crtc *crtc,
  6254. const struct intel_crtc_state *pipe_config)
  6255. {
  6256. struct drm_device *dev = crtc->base.dev;
  6257. struct drm_i915_private *dev_priv = dev->dev_private;
  6258. int pipe = crtc->pipe;
  6259. int dpll_reg = DPLL(crtc->pipe);
  6260. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6261. u32 loopfilter, tribuf_calcntr;
  6262. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6263. u32 dpio_val;
  6264. int vco;
  6265. bestn = pipe_config->dpll.n;
  6266. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6267. bestm1 = pipe_config->dpll.m1;
  6268. bestm2 = pipe_config->dpll.m2 >> 22;
  6269. bestp1 = pipe_config->dpll.p1;
  6270. bestp2 = pipe_config->dpll.p2;
  6271. vco = pipe_config->dpll.vco;
  6272. dpio_val = 0;
  6273. loopfilter = 0;
  6274. /*
  6275. * Enable Refclk and SSC
  6276. */
  6277. I915_WRITE(dpll_reg,
  6278. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6279. mutex_lock(&dev_priv->sb_lock);
  6280. /* p1 and p2 divider */
  6281. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6282. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6283. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6284. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6285. 1 << DPIO_CHV_K_DIV_SHIFT);
  6286. /* Feedback post-divider - m2 */
  6287. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6288. /* Feedback refclk divider - n and m1 */
  6289. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6290. DPIO_CHV_M1_DIV_BY_2 |
  6291. 1 << DPIO_CHV_N_DIV_SHIFT);
  6292. /* M2 fraction division */
  6293. if (bestm2_frac)
  6294. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6295. /* M2 fraction division enable */
  6296. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6297. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6298. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6299. if (bestm2_frac)
  6300. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6301. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6302. /* Program digital lock detect threshold */
  6303. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6304. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6305. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6306. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6307. if (!bestm2_frac)
  6308. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6309. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6310. /* Loop filter */
  6311. if (vco == 5400000) {
  6312. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6313. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6314. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6315. tribuf_calcntr = 0x9;
  6316. } else if (vco <= 6200000) {
  6317. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6318. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6319. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6320. tribuf_calcntr = 0x9;
  6321. } else if (vco <= 6480000) {
  6322. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6323. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6324. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6325. tribuf_calcntr = 0x8;
  6326. } else {
  6327. /* Not supported. Apply the same limits as in the max case */
  6328. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6329. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6330. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6331. tribuf_calcntr = 0;
  6332. }
  6333. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6334. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6335. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6336. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6337. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6338. /* AFC Recal */
  6339. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6340. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6341. DPIO_AFC_RECAL);
  6342. mutex_unlock(&dev_priv->sb_lock);
  6343. }
  6344. /**
  6345. * vlv_force_pll_on - forcibly enable just the PLL
  6346. * @dev_priv: i915 private structure
  6347. * @pipe: pipe PLL to enable
  6348. * @dpll: PLL configuration
  6349. *
  6350. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6351. * in cases where we need the PLL enabled even when @pipe is not going to
  6352. * be enabled.
  6353. */
  6354. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6355. const struct dpll *dpll)
  6356. {
  6357. struct intel_crtc *crtc =
  6358. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6359. struct intel_crtc_state pipe_config = {
  6360. .base.crtc = &crtc->base,
  6361. .pixel_multiplier = 1,
  6362. .dpll = *dpll,
  6363. };
  6364. if (IS_CHERRYVIEW(dev)) {
  6365. chv_update_pll(crtc, &pipe_config);
  6366. chv_prepare_pll(crtc, &pipe_config);
  6367. chv_enable_pll(crtc, &pipe_config);
  6368. } else {
  6369. vlv_update_pll(crtc, &pipe_config);
  6370. vlv_prepare_pll(crtc, &pipe_config);
  6371. vlv_enable_pll(crtc, &pipe_config);
  6372. }
  6373. }
  6374. /**
  6375. * vlv_force_pll_off - forcibly disable just the PLL
  6376. * @dev_priv: i915 private structure
  6377. * @pipe: pipe PLL to disable
  6378. *
  6379. * Disable the PLL for @pipe. To be used in cases where we need
  6380. * the PLL enabled even when @pipe is not going to be enabled.
  6381. */
  6382. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6383. {
  6384. if (IS_CHERRYVIEW(dev))
  6385. chv_disable_pll(to_i915(dev), pipe);
  6386. else
  6387. vlv_disable_pll(to_i915(dev), pipe);
  6388. }
  6389. static void i9xx_update_pll(struct intel_crtc *crtc,
  6390. struct intel_crtc_state *crtc_state,
  6391. intel_clock_t *reduced_clock,
  6392. int num_connectors)
  6393. {
  6394. struct drm_device *dev = crtc->base.dev;
  6395. struct drm_i915_private *dev_priv = dev->dev_private;
  6396. u32 dpll;
  6397. bool is_sdvo;
  6398. struct dpll *clock = &crtc_state->dpll;
  6399. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6400. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6401. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6402. dpll = DPLL_VGA_MODE_DIS;
  6403. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6404. dpll |= DPLLB_MODE_LVDS;
  6405. else
  6406. dpll |= DPLLB_MODE_DAC_SERIAL;
  6407. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6408. dpll |= (crtc_state->pixel_multiplier - 1)
  6409. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6410. }
  6411. if (is_sdvo)
  6412. dpll |= DPLL_SDVO_HIGH_SPEED;
  6413. if (crtc_state->has_dp_encoder)
  6414. dpll |= DPLL_SDVO_HIGH_SPEED;
  6415. /* compute bitmask from p1 value */
  6416. if (IS_PINEVIEW(dev))
  6417. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6418. else {
  6419. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6420. if (IS_G4X(dev) && reduced_clock)
  6421. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6422. }
  6423. switch (clock->p2) {
  6424. case 5:
  6425. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6426. break;
  6427. case 7:
  6428. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6429. break;
  6430. case 10:
  6431. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6432. break;
  6433. case 14:
  6434. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6435. break;
  6436. }
  6437. if (INTEL_INFO(dev)->gen >= 4)
  6438. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6439. if (crtc_state->sdvo_tv_clock)
  6440. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6441. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6442. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6443. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6444. else
  6445. dpll |= PLL_REF_INPUT_DREFCLK;
  6446. dpll |= DPLL_VCO_ENABLE;
  6447. crtc_state->dpll_hw_state.dpll = dpll;
  6448. if (INTEL_INFO(dev)->gen >= 4) {
  6449. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6450. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6451. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6452. }
  6453. }
  6454. static void i8xx_update_pll(struct intel_crtc *crtc,
  6455. struct intel_crtc_state *crtc_state,
  6456. intel_clock_t *reduced_clock,
  6457. int num_connectors)
  6458. {
  6459. struct drm_device *dev = crtc->base.dev;
  6460. struct drm_i915_private *dev_priv = dev->dev_private;
  6461. u32 dpll;
  6462. struct dpll *clock = &crtc_state->dpll;
  6463. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6464. dpll = DPLL_VGA_MODE_DIS;
  6465. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6466. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6467. } else {
  6468. if (clock->p1 == 2)
  6469. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6470. else
  6471. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6472. if (clock->p2 == 4)
  6473. dpll |= PLL_P2_DIVIDE_BY_4;
  6474. }
  6475. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6476. dpll |= DPLL_DVO_2X_MODE;
  6477. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6478. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6479. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6480. else
  6481. dpll |= PLL_REF_INPUT_DREFCLK;
  6482. dpll |= DPLL_VCO_ENABLE;
  6483. crtc_state->dpll_hw_state.dpll = dpll;
  6484. }
  6485. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6486. {
  6487. struct drm_device *dev = intel_crtc->base.dev;
  6488. struct drm_i915_private *dev_priv = dev->dev_private;
  6489. enum pipe pipe = intel_crtc->pipe;
  6490. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6491. struct drm_display_mode *adjusted_mode =
  6492. &intel_crtc->config->base.adjusted_mode;
  6493. uint32_t crtc_vtotal, crtc_vblank_end;
  6494. int vsyncshift = 0;
  6495. /* We need to be careful not to changed the adjusted mode, for otherwise
  6496. * the hw state checker will get angry at the mismatch. */
  6497. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6498. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6499. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6500. /* the chip adds 2 halflines automatically */
  6501. crtc_vtotal -= 1;
  6502. crtc_vblank_end -= 1;
  6503. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6504. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6505. else
  6506. vsyncshift = adjusted_mode->crtc_hsync_start -
  6507. adjusted_mode->crtc_htotal / 2;
  6508. if (vsyncshift < 0)
  6509. vsyncshift += adjusted_mode->crtc_htotal;
  6510. }
  6511. if (INTEL_INFO(dev)->gen > 3)
  6512. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6513. I915_WRITE(HTOTAL(cpu_transcoder),
  6514. (adjusted_mode->crtc_hdisplay - 1) |
  6515. ((adjusted_mode->crtc_htotal - 1) << 16));
  6516. I915_WRITE(HBLANK(cpu_transcoder),
  6517. (adjusted_mode->crtc_hblank_start - 1) |
  6518. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6519. I915_WRITE(HSYNC(cpu_transcoder),
  6520. (adjusted_mode->crtc_hsync_start - 1) |
  6521. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6522. I915_WRITE(VTOTAL(cpu_transcoder),
  6523. (adjusted_mode->crtc_vdisplay - 1) |
  6524. ((crtc_vtotal - 1) << 16));
  6525. I915_WRITE(VBLANK(cpu_transcoder),
  6526. (adjusted_mode->crtc_vblank_start - 1) |
  6527. ((crtc_vblank_end - 1) << 16));
  6528. I915_WRITE(VSYNC(cpu_transcoder),
  6529. (adjusted_mode->crtc_vsync_start - 1) |
  6530. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6531. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6532. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6533. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6534. * bits. */
  6535. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6536. (pipe == PIPE_B || pipe == PIPE_C))
  6537. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6538. /* pipesrc controls the size that is scaled from, which should
  6539. * always be the user's requested size.
  6540. */
  6541. I915_WRITE(PIPESRC(pipe),
  6542. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6543. (intel_crtc->config->pipe_src_h - 1));
  6544. }
  6545. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6546. struct intel_crtc_state *pipe_config)
  6547. {
  6548. struct drm_device *dev = crtc->base.dev;
  6549. struct drm_i915_private *dev_priv = dev->dev_private;
  6550. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6551. uint32_t tmp;
  6552. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6553. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6554. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6555. tmp = I915_READ(HBLANK(cpu_transcoder));
  6556. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6557. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6558. tmp = I915_READ(HSYNC(cpu_transcoder));
  6559. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6560. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6561. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6562. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6563. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6564. tmp = I915_READ(VBLANK(cpu_transcoder));
  6565. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6566. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6567. tmp = I915_READ(VSYNC(cpu_transcoder));
  6568. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6569. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6570. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6571. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6572. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6573. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6574. }
  6575. tmp = I915_READ(PIPESRC(crtc->pipe));
  6576. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6577. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6578. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6579. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6580. }
  6581. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6582. struct intel_crtc_state *pipe_config)
  6583. {
  6584. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6585. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6586. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6587. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6588. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6589. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6590. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6591. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6592. mode->flags = pipe_config->base.adjusted_mode.flags;
  6593. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6594. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6595. }
  6596. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6597. {
  6598. struct drm_device *dev = intel_crtc->base.dev;
  6599. struct drm_i915_private *dev_priv = dev->dev_private;
  6600. uint32_t pipeconf;
  6601. pipeconf = 0;
  6602. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6603. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6604. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6605. if (intel_crtc->config->double_wide)
  6606. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6607. /* only g4x and later have fancy bpc/dither controls */
  6608. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6609. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6610. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6611. pipeconf |= PIPECONF_DITHER_EN |
  6612. PIPECONF_DITHER_TYPE_SP;
  6613. switch (intel_crtc->config->pipe_bpp) {
  6614. case 18:
  6615. pipeconf |= PIPECONF_6BPC;
  6616. break;
  6617. case 24:
  6618. pipeconf |= PIPECONF_8BPC;
  6619. break;
  6620. case 30:
  6621. pipeconf |= PIPECONF_10BPC;
  6622. break;
  6623. default:
  6624. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6625. BUG();
  6626. }
  6627. }
  6628. if (HAS_PIPE_CXSR(dev)) {
  6629. if (intel_crtc->lowfreq_avail) {
  6630. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6631. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6632. } else {
  6633. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6634. }
  6635. }
  6636. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6637. if (INTEL_INFO(dev)->gen < 4 ||
  6638. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6639. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6640. else
  6641. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6642. } else
  6643. pipeconf |= PIPECONF_PROGRESSIVE;
  6644. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6645. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6646. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6647. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6648. }
  6649. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6650. struct intel_crtc_state *crtc_state)
  6651. {
  6652. struct drm_device *dev = crtc->base.dev;
  6653. struct drm_i915_private *dev_priv = dev->dev_private;
  6654. int refclk, num_connectors = 0;
  6655. intel_clock_t clock, reduced_clock;
  6656. bool ok, has_reduced_clock = false;
  6657. bool is_lvds = false, is_dsi = false;
  6658. struct intel_encoder *encoder;
  6659. const intel_limit_t *limit;
  6660. struct drm_atomic_state *state = crtc_state->base.state;
  6661. struct drm_connector *connector;
  6662. struct drm_connector_state *connector_state;
  6663. int i;
  6664. memset(&crtc_state->dpll_hw_state, 0,
  6665. sizeof(crtc_state->dpll_hw_state));
  6666. for_each_connector_in_state(state, connector, connector_state, i) {
  6667. if (connector_state->crtc != &crtc->base)
  6668. continue;
  6669. encoder = to_intel_encoder(connector_state->best_encoder);
  6670. switch (encoder->type) {
  6671. case INTEL_OUTPUT_LVDS:
  6672. is_lvds = true;
  6673. break;
  6674. case INTEL_OUTPUT_DSI:
  6675. is_dsi = true;
  6676. break;
  6677. default:
  6678. break;
  6679. }
  6680. num_connectors++;
  6681. }
  6682. if (is_dsi)
  6683. return 0;
  6684. if (!crtc_state->clock_set) {
  6685. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6686. /*
  6687. * Returns a set of divisors for the desired target clock with
  6688. * the given refclk, or FALSE. The returned values represent
  6689. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6690. * 2) / p1 / p2.
  6691. */
  6692. limit = intel_limit(crtc_state, refclk);
  6693. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6694. crtc_state->port_clock,
  6695. refclk, NULL, &clock);
  6696. if (!ok) {
  6697. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6698. return -EINVAL;
  6699. }
  6700. if (is_lvds && dev_priv->lvds_downclock_avail) {
  6701. /*
  6702. * Ensure we match the reduced clock's P to the target
  6703. * clock. If the clocks don't match, we can't switch
  6704. * the display clock by using the FP0/FP1. In such case
  6705. * we will disable the LVDS downclock feature.
  6706. */
  6707. has_reduced_clock =
  6708. dev_priv->display.find_dpll(limit, crtc_state,
  6709. dev_priv->lvds_downclock,
  6710. refclk, &clock,
  6711. &reduced_clock);
  6712. }
  6713. /* Compat-code for transition, will disappear. */
  6714. crtc_state->dpll.n = clock.n;
  6715. crtc_state->dpll.m1 = clock.m1;
  6716. crtc_state->dpll.m2 = clock.m2;
  6717. crtc_state->dpll.p1 = clock.p1;
  6718. crtc_state->dpll.p2 = clock.p2;
  6719. }
  6720. if (IS_GEN2(dev)) {
  6721. i8xx_update_pll(crtc, crtc_state,
  6722. has_reduced_clock ? &reduced_clock : NULL,
  6723. num_connectors);
  6724. } else if (IS_CHERRYVIEW(dev)) {
  6725. chv_update_pll(crtc, crtc_state);
  6726. } else if (IS_VALLEYVIEW(dev)) {
  6727. vlv_update_pll(crtc, crtc_state);
  6728. } else {
  6729. i9xx_update_pll(crtc, crtc_state,
  6730. has_reduced_clock ? &reduced_clock : NULL,
  6731. num_connectors);
  6732. }
  6733. return 0;
  6734. }
  6735. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6736. struct intel_crtc_state *pipe_config)
  6737. {
  6738. struct drm_device *dev = crtc->base.dev;
  6739. struct drm_i915_private *dev_priv = dev->dev_private;
  6740. uint32_t tmp;
  6741. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6742. return;
  6743. tmp = I915_READ(PFIT_CONTROL);
  6744. if (!(tmp & PFIT_ENABLE))
  6745. return;
  6746. /* Check whether the pfit is attached to our pipe. */
  6747. if (INTEL_INFO(dev)->gen < 4) {
  6748. if (crtc->pipe != PIPE_B)
  6749. return;
  6750. } else {
  6751. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6752. return;
  6753. }
  6754. pipe_config->gmch_pfit.control = tmp;
  6755. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6756. if (INTEL_INFO(dev)->gen < 5)
  6757. pipe_config->gmch_pfit.lvds_border_bits =
  6758. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6759. }
  6760. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6761. struct intel_crtc_state *pipe_config)
  6762. {
  6763. struct drm_device *dev = crtc->base.dev;
  6764. struct drm_i915_private *dev_priv = dev->dev_private;
  6765. int pipe = pipe_config->cpu_transcoder;
  6766. intel_clock_t clock;
  6767. u32 mdiv;
  6768. int refclk = 100000;
  6769. /* In case of MIPI DPLL will not even be used */
  6770. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6771. return;
  6772. mutex_lock(&dev_priv->sb_lock);
  6773. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6774. mutex_unlock(&dev_priv->sb_lock);
  6775. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6776. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6777. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6778. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6779. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6780. vlv_clock(refclk, &clock);
  6781. /* clock.dot is the fast clock */
  6782. pipe_config->port_clock = clock.dot / 5;
  6783. }
  6784. static void
  6785. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6786. struct intel_initial_plane_config *plane_config)
  6787. {
  6788. struct drm_device *dev = crtc->base.dev;
  6789. struct drm_i915_private *dev_priv = dev->dev_private;
  6790. u32 val, base, offset;
  6791. int pipe = crtc->pipe, plane = crtc->plane;
  6792. int fourcc, pixel_format;
  6793. unsigned int aligned_height;
  6794. struct drm_framebuffer *fb;
  6795. struct intel_framebuffer *intel_fb;
  6796. val = I915_READ(DSPCNTR(plane));
  6797. if (!(val & DISPLAY_PLANE_ENABLE))
  6798. return;
  6799. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6800. if (!intel_fb) {
  6801. DRM_DEBUG_KMS("failed to alloc fb\n");
  6802. return;
  6803. }
  6804. fb = &intel_fb->base;
  6805. if (INTEL_INFO(dev)->gen >= 4) {
  6806. if (val & DISPPLANE_TILED) {
  6807. plane_config->tiling = I915_TILING_X;
  6808. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6809. }
  6810. }
  6811. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6812. fourcc = i9xx_format_to_fourcc(pixel_format);
  6813. fb->pixel_format = fourcc;
  6814. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6815. if (INTEL_INFO(dev)->gen >= 4) {
  6816. if (plane_config->tiling)
  6817. offset = I915_READ(DSPTILEOFF(plane));
  6818. else
  6819. offset = I915_READ(DSPLINOFF(plane));
  6820. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6821. } else {
  6822. base = I915_READ(DSPADDR(plane));
  6823. }
  6824. plane_config->base = base;
  6825. val = I915_READ(PIPESRC(pipe));
  6826. fb->width = ((val >> 16) & 0xfff) + 1;
  6827. fb->height = ((val >> 0) & 0xfff) + 1;
  6828. val = I915_READ(DSPSTRIDE(pipe));
  6829. fb->pitches[0] = val & 0xffffffc0;
  6830. aligned_height = intel_fb_align_height(dev, fb->height,
  6831. fb->pixel_format,
  6832. fb->modifier[0]);
  6833. plane_config->size = fb->pitches[0] * aligned_height;
  6834. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6835. pipe_name(pipe), plane, fb->width, fb->height,
  6836. fb->bits_per_pixel, base, fb->pitches[0],
  6837. plane_config->size);
  6838. plane_config->fb = intel_fb;
  6839. }
  6840. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6841. struct intel_crtc_state *pipe_config)
  6842. {
  6843. struct drm_device *dev = crtc->base.dev;
  6844. struct drm_i915_private *dev_priv = dev->dev_private;
  6845. int pipe = pipe_config->cpu_transcoder;
  6846. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6847. intel_clock_t clock;
  6848. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  6849. int refclk = 100000;
  6850. mutex_lock(&dev_priv->sb_lock);
  6851. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6852. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6853. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6854. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6855. mutex_unlock(&dev_priv->sb_lock);
  6856. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6857. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  6858. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6859. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6860. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6861. chv_clock(refclk, &clock);
  6862. /* clock.dot is the fast clock */
  6863. pipe_config->port_clock = clock.dot / 5;
  6864. }
  6865. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6866. struct intel_crtc_state *pipe_config)
  6867. {
  6868. struct drm_device *dev = crtc->base.dev;
  6869. struct drm_i915_private *dev_priv = dev->dev_private;
  6870. uint32_t tmp;
  6871. if (!intel_display_power_is_enabled(dev_priv,
  6872. POWER_DOMAIN_PIPE(crtc->pipe)))
  6873. return false;
  6874. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6875. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6876. tmp = I915_READ(PIPECONF(crtc->pipe));
  6877. if (!(tmp & PIPECONF_ENABLE))
  6878. return false;
  6879. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6880. switch (tmp & PIPECONF_BPC_MASK) {
  6881. case PIPECONF_6BPC:
  6882. pipe_config->pipe_bpp = 18;
  6883. break;
  6884. case PIPECONF_8BPC:
  6885. pipe_config->pipe_bpp = 24;
  6886. break;
  6887. case PIPECONF_10BPC:
  6888. pipe_config->pipe_bpp = 30;
  6889. break;
  6890. default:
  6891. break;
  6892. }
  6893. }
  6894. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6895. pipe_config->limited_color_range = true;
  6896. if (INTEL_INFO(dev)->gen < 4)
  6897. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6898. intel_get_pipe_timings(crtc, pipe_config);
  6899. i9xx_get_pfit_config(crtc, pipe_config);
  6900. if (INTEL_INFO(dev)->gen >= 4) {
  6901. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6902. pipe_config->pixel_multiplier =
  6903. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6904. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6905. pipe_config->dpll_hw_state.dpll_md = tmp;
  6906. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6907. tmp = I915_READ(DPLL(crtc->pipe));
  6908. pipe_config->pixel_multiplier =
  6909. ((tmp & SDVO_MULTIPLIER_MASK)
  6910. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6911. } else {
  6912. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6913. * port and will be fixed up in the encoder->get_config
  6914. * function. */
  6915. pipe_config->pixel_multiplier = 1;
  6916. }
  6917. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6918. if (!IS_VALLEYVIEW(dev)) {
  6919. /*
  6920. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6921. * on 830. Filter it out here so that we don't
  6922. * report errors due to that.
  6923. */
  6924. if (IS_I830(dev))
  6925. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6926. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6927. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6928. } else {
  6929. /* Mask out read-only status bits. */
  6930. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6931. DPLL_PORTC_READY_MASK |
  6932. DPLL_PORTB_READY_MASK);
  6933. }
  6934. if (IS_CHERRYVIEW(dev))
  6935. chv_crtc_clock_get(crtc, pipe_config);
  6936. else if (IS_VALLEYVIEW(dev))
  6937. vlv_crtc_clock_get(crtc, pipe_config);
  6938. else
  6939. i9xx_crtc_clock_get(crtc, pipe_config);
  6940. return true;
  6941. }
  6942. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6943. {
  6944. struct drm_i915_private *dev_priv = dev->dev_private;
  6945. struct intel_encoder *encoder;
  6946. u32 val, final;
  6947. bool has_lvds = false;
  6948. bool has_cpu_edp = false;
  6949. bool has_panel = false;
  6950. bool has_ck505 = false;
  6951. bool can_ssc = false;
  6952. /* We need to take the global config into account */
  6953. for_each_intel_encoder(dev, encoder) {
  6954. switch (encoder->type) {
  6955. case INTEL_OUTPUT_LVDS:
  6956. has_panel = true;
  6957. has_lvds = true;
  6958. break;
  6959. case INTEL_OUTPUT_EDP:
  6960. has_panel = true;
  6961. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6962. has_cpu_edp = true;
  6963. break;
  6964. default:
  6965. break;
  6966. }
  6967. }
  6968. if (HAS_PCH_IBX(dev)) {
  6969. has_ck505 = dev_priv->vbt.display_clock_mode;
  6970. can_ssc = has_ck505;
  6971. } else {
  6972. has_ck505 = false;
  6973. can_ssc = true;
  6974. }
  6975. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6976. has_panel, has_lvds, has_ck505);
  6977. /* Ironlake: try to setup display ref clock before DPLL
  6978. * enabling. This is only under driver's control after
  6979. * PCH B stepping, previous chipset stepping should be
  6980. * ignoring this setting.
  6981. */
  6982. val = I915_READ(PCH_DREF_CONTROL);
  6983. /* As we must carefully and slowly disable/enable each source in turn,
  6984. * compute the final state we want first and check if we need to
  6985. * make any changes at all.
  6986. */
  6987. final = val;
  6988. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6989. if (has_ck505)
  6990. final |= DREF_NONSPREAD_CK505_ENABLE;
  6991. else
  6992. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6993. final &= ~DREF_SSC_SOURCE_MASK;
  6994. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6995. final &= ~DREF_SSC1_ENABLE;
  6996. if (has_panel) {
  6997. final |= DREF_SSC_SOURCE_ENABLE;
  6998. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6999. final |= DREF_SSC1_ENABLE;
  7000. if (has_cpu_edp) {
  7001. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7002. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7003. else
  7004. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7005. } else
  7006. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7007. } else {
  7008. final |= DREF_SSC_SOURCE_DISABLE;
  7009. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7010. }
  7011. if (final == val)
  7012. return;
  7013. /* Always enable nonspread source */
  7014. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7015. if (has_ck505)
  7016. val |= DREF_NONSPREAD_CK505_ENABLE;
  7017. else
  7018. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7019. if (has_panel) {
  7020. val &= ~DREF_SSC_SOURCE_MASK;
  7021. val |= DREF_SSC_SOURCE_ENABLE;
  7022. /* SSC must be turned on before enabling the CPU output */
  7023. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7024. DRM_DEBUG_KMS("Using SSC on panel\n");
  7025. val |= DREF_SSC1_ENABLE;
  7026. } else
  7027. val &= ~DREF_SSC1_ENABLE;
  7028. /* Get SSC going before enabling the outputs */
  7029. I915_WRITE(PCH_DREF_CONTROL, val);
  7030. POSTING_READ(PCH_DREF_CONTROL);
  7031. udelay(200);
  7032. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7033. /* Enable CPU source on CPU attached eDP */
  7034. if (has_cpu_edp) {
  7035. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7036. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7037. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7038. } else
  7039. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7040. } else
  7041. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7042. I915_WRITE(PCH_DREF_CONTROL, val);
  7043. POSTING_READ(PCH_DREF_CONTROL);
  7044. udelay(200);
  7045. } else {
  7046. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7047. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7048. /* Turn off CPU output */
  7049. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7050. I915_WRITE(PCH_DREF_CONTROL, val);
  7051. POSTING_READ(PCH_DREF_CONTROL);
  7052. udelay(200);
  7053. /* Turn off the SSC source */
  7054. val &= ~DREF_SSC_SOURCE_MASK;
  7055. val |= DREF_SSC_SOURCE_DISABLE;
  7056. /* Turn off SSC1 */
  7057. val &= ~DREF_SSC1_ENABLE;
  7058. I915_WRITE(PCH_DREF_CONTROL, val);
  7059. POSTING_READ(PCH_DREF_CONTROL);
  7060. udelay(200);
  7061. }
  7062. BUG_ON(val != final);
  7063. }
  7064. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7065. {
  7066. uint32_t tmp;
  7067. tmp = I915_READ(SOUTH_CHICKEN2);
  7068. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7069. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7070. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7071. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7072. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7073. tmp = I915_READ(SOUTH_CHICKEN2);
  7074. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7075. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7076. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7077. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7078. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7079. }
  7080. /* WaMPhyProgramming:hsw */
  7081. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7082. {
  7083. uint32_t tmp;
  7084. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7085. tmp &= ~(0xFF << 24);
  7086. tmp |= (0x12 << 24);
  7087. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7088. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7089. tmp |= (1 << 11);
  7090. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7091. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7092. tmp |= (1 << 11);
  7093. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7094. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7095. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7096. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7097. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7098. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7099. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7100. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7101. tmp &= ~(7 << 13);
  7102. tmp |= (5 << 13);
  7103. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7104. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7105. tmp &= ~(7 << 13);
  7106. tmp |= (5 << 13);
  7107. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7108. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7109. tmp &= ~0xFF;
  7110. tmp |= 0x1C;
  7111. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7112. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7113. tmp &= ~0xFF;
  7114. tmp |= 0x1C;
  7115. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7116. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7117. tmp &= ~(0xFF << 16);
  7118. tmp |= (0x1C << 16);
  7119. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7120. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7121. tmp &= ~(0xFF << 16);
  7122. tmp |= (0x1C << 16);
  7123. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7124. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7125. tmp |= (1 << 27);
  7126. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7127. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7128. tmp |= (1 << 27);
  7129. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7130. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7131. tmp &= ~(0xF << 28);
  7132. tmp |= (4 << 28);
  7133. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7134. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7135. tmp &= ~(0xF << 28);
  7136. tmp |= (4 << 28);
  7137. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7138. }
  7139. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7140. * Programming" based on the parameters passed:
  7141. * - Sequence to enable CLKOUT_DP
  7142. * - Sequence to enable CLKOUT_DP without spread
  7143. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7144. */
  7145. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7146. bool with_fdi)
  7147. {
  7148. struct drm_i915_private *dev_priv = dev->dev_private;
  7149. uint32_t reg, tmp;
  7150. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7151. with_spread = true;
  7152. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  7153. with_fdi, "LP PCH doesn't have FDI\n"))
  7154. with_fdi = false;
  7155. mutex_lock(&dev_priv->sb_lock);
  7156. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7157. tmp &= ~SBI_SSCCTL_DISABLE;
  7158. tmp |= SBI_SSCCTL_PATHALT;
  7159. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7160. udelay(24);
  7161. if (with_spread) {
  7162. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7163. tmp &= ~SBI_SSCCTL_PATHALT;
  7164. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7165. if (with_fdi) {
  7166. lpt_reset_fdi_mphy(dev_priv);
  7167. lpt_program_fdi_mphy(dev_priv);
  7168. }
  7169. }
  7170. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7171. SBI_GEN0 : SBI_DBUFF0;
  7172. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7173. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7174. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7175. mutex_unlock(&dev_priv->sb_lock);
  7176. }
  7177. /* Sequence to disable CLKOUT_DP */
  7178. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7179. {
  7180. struct drm_i915_private *dev_priv = dev->dev_private;
  7181. uint32_t reg, tmp;
  7182. mutex_lock(&dev_priv->sb_lock);
  7183. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  7184. SBI_GEN0 : SBI_DBUFF0;
  7185. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7186. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7187. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7188. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7189. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7190. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7191. tmp |= SBI_SSCCTL_PATHALT;
  7192. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7193. udelay(32);
  7194. }
  7195. tmp |= SBI_SSCCTL_DISABLE;
  7196. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7197. }
  7198. mutex_unlock(&dev_priv->sb_lock);
  7199. }
  7200. static void lpt_init_pch_refclk(struct drm_device *dev)
  7201. {
  7202. struct intel_encoder *encoder;
  7203. bool has_vga = false;
  7204. for_each_intel_encoder(dev, encoder) {
  7205. switch (encoder->type) {
  7206. case INTEL_OUTPUT_ANALOG:
  7207. has_vga = true;
  7208. break;
  7209. default:
  7210. break;
  7211. }
  7212. }
  7213. if (has_vga)
  7214. lpt_enable_clkout_dp(dev, true, true);
  7215. else
  7216. lpt_disable_clkout_dp(dev);
  7217. }
  7218. /*
  7219. * Initialize reference clocks when the driver loads
  7220. */
  7221. void intel_init_pch_refclk(struct drm_device *dev)
  7222. {
  7223. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7224. ironlake_init_pch_refclk(dev);
  7225. else if (HAS_PCH_LPT(dev))
  7226. lpt_init_pch_refclk(dev);
  7227. }
  7228. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7229. {
  7230. struct drm_device *dev = crtc_state->base.crtc->dev;
  7231. struct drm_i915_private *dev_priv = dev->dev_private;
  7232. struct drm_atomic_state *state = crtc_state->base.state;
  7233. struct drm_connector *connector;
  7234. struct drm_connector_state *connector_state;
  7235. struct intel_encoder *encoder;
  7236. int num_connectors = 0, i;
  7237. bool is_lvds = false;
  7238. for_each_connector_in_state(state, connector, connector_state, i) {
  7239. if (connector_state->crtc != crtc_state->base.crtc)
  7240. continue;
  7241. encoder = to_intel_encoder(connector_state->best_encoder);
  7242. switch (encoder->type) {
  7243. case INTEL_OUTPUT_LVDS:
  7244. is_lvds = true;
  7245. break;
  7246. default:
  7247. break;
  7248. }
  7249. num_connectors++;
  7250. }
  7251. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7252. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7253. dev_priv->vbt.lvds_ssc_freq);
  7254. return dev_priv->vbt.lvds_ssc_freq;
  7255. }
  7256. return 120000;
  7257. }
  7258. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7259. {
  7260. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7262. int pipe = intel_crtc->pipe;
  7263. uint32_t val;
  7264. val = 0;
  7265. switch (intel_crtc->config->pipe_bpp) {
  7266. case 18:
  7267. val |= PIPECONF_6BPC;
  7268. break;
  7269. case 24:
  7270. val |= PIPECONF_8BPC;
  7271. break;
  7272. case 30:
  7273. val |= PIPECONF_10BPC;
  7274. break;
  7275. case 36:
  7276. val |= PIPECONF_12BPC;
  7277. break;
  7278. default:
  7279. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7280. BUG();
  7281. }
  7282. if (intel_crtc->config->dither)
  7283. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7284. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7285. val |= PIPECONF_INTERLACED_ILK;
  7286. else
  7287. val |= PIPECONF_PROGRESSIVE;
  7288. if (intel_crtc->config->limited_color_range)
  7289. val |= PIPECONF_COLOR_RANGE_SELECT;
  7290. I915_WRITE(PIPECONF(pipe), val);
  7291. POSTING_READ(PIPECONF(pipe));
  7292. }
  7293. /*
  7294. * Set up the pipe CSC unit.
  7295. *
  7296. * Currently only full range RGB to limited range RGB conversion
  7297. * is supported, but eventually this should handle various
  7298. * RGB<->YCbCr scenarios as well.
  7299. */
  7300. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7301. {
  7302. struct drm_device *dev = crtc->dev;
  7303. struct drm_i915_private *dev_priv = dev->dev_private;
  7304. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7305. int pipe = intel_crtc->pipe;
  7306. uint16_t coeff = 0x7800; /* 1.0 */
  7307. /*
  7308. * TODO: Check what kind of values actually come out of the pipe
  7309. * with these coeff/postoff values and adjust to get the best
  7310. * accuracy. Perhaps we even need to take the bpc value into
  7311. * consideration.
  7312. */
  7313. if (intel_crtc->config->limited_color_range)
  7314. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7315. /*
  7316. * GY/GU and RY/RU should be the other way around according
  7317. * to BSpec, but reality doesn't agree. Just set them up in
  7318. * a way that results in the correct picture.
  7319. */
  7320. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7321. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7322. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7323. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7324. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7325. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7326. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7327. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7328. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7329. if (INTEL_INFO(dev)->gen > 6) {
  7330. uint16_t postoff = 0;
  7331. if (intel_crtc->config->limited_color_range)
  7332. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7333. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7334. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7335. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7336. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7337. } else {
  7338. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7339. if (intel_crtc->config->limited_color_range)
  7340. mode |= CSC_BLACK_SCREEN_OFFSET;
  7341. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7342. }
  7343. }
  7344. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7345. {
  7346. struct drm_device *dev = crtc->dev;
  7347. struct drm_i915_private *dev_priv = dev->dev_private;
  7348. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7349. enum pipe pipe = intel_crtc->pipe;
  7350. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7351. uint32_t val;
  7352. val = 0;
  7353. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7354. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7355. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7356. val |= PIPECONF_INTERLACED_ILK;
  7357. else
  7358. val |= PIPECONF_PROGRESSIVE;
  7359. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7360. POSTING_READ(PIPECONF(cpu_transcoder));
  7361. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7362. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7363. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7364. val = 0;
  7365. switch (intel_crtc->config->pipe_bpp) {
  7366. case 18:
  7367. val |= PIPEMISC_DITHER_6_BPC;
  7368. break;
  7369. case 24:
  7370. val |= PIPEMISC_DITHER_8_BPC;
  7371. break;
  7372. case 30:
  7373. val |= PIPEMISC_DITHER_10_BPC;
  7374. break;
  7375. case 36:
  7376. val |= PIPEMISC_DITHER_12_BPC;
  7377. break;
  7378. default:
  7379. /* Case prevented by pipe_config_set_bpp. */
  7380. BUG();
  7381. }
  7382. if (intel_crtc->config->dither)
  7383. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7384. I915_WRITE(PIPEMISC(pipe), val);
  7385. }
  7386. }
  7387. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7388. struct intel_crtc_state *crtc_state,
  7389. intel_clock_t *clock,
  7390. bool *has_reduced_clock,
  7391. intel_clock_t *reduced_clock)
  7392. {
  7393. struct drm_device *dev = crtc->dev;
  7394. struct drm_i915_private *dev_priv = dev->dev_private;
  7395. int refclk;
  7396. const intel_limit_t *limit;
  7397. bool ret, is_lvds = false;
  7398. is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
  7399. refclk = ironlake_get_refclk(crtc_state);
  7400. /*
  7401. * Returns a set of divisors for the desired target clock with the given
  7402. * refclk, or FALSE. The returned values represent the clock equation:
  7403. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7404. */
  7405. limit = intel_limit(crtc_state, refclk);
  7406. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7407. crtc_state->port_clock,
  7408. refclk, NULL, clock);
  7409. if (!ret)
  7410. return false;
  7411. if (is_lvds && dev_priv->lvds_downclock_avail) {
  7412. /*
  7413. * Ensure we match the reduced clock's P to the target clock.
  7414. * If the clocks don't match, we can't switch the display clock
  7415. * by using the FP0/FP1. In such case we will disable the LVDS
  7416. * downclock feature.
  7417. */
  7418. *has_reduced_clock =
  7419. dev_priv->display.find_dpll(limit, crtc_state,
  7420. dev_priv->lvds_downclock,
  7421. refclk, clock,
  7422. reduced_clock);
  7423. }
  7424. return true;
  7425. }
  7426. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7427. {
  7428. /*
  7429. * Account for spread spectrum to avoid
  7430. * oversubscribing the link. Max center spread
  7431. * is 2.5%; use 5% for safety's sake.
  7432. */
  7433. u32 bps = target_clock * bpp * 21 / 20;
  7434. return DIV_ROUND_UP(bps, link_bw * 8);
  7435. }
  7436. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7437. {
  7438. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7439. }
  7440. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7441. struct intel_crtc_state *crtc_state,
  7442. u32 *fp,
  7443. intel_clock_t *reduced_clock, u32 *fp2)
  7444. {
  7445. struct drm_crtc *crtc = &intel_crtc->base;
  7446. struct drm_device *dev = crtc->dev;
  7447. struct drm_i915_private *dev_priv = dev->dev_private;
  7448. struct drm_atomic_state *state = crtc_state->base.state;
  7449. struct drm_connector *connector;
  7450. struct drm_connector_state *connector_state;
  7451. struct intel_encoder *encoder;
  7452. uint32_t dpll;
  7453. int factor, num_connectors = 0, i;
  7454. bool is_lvds = false, is_sdvo = false;
  7455. for_each_connector_in_state(state, connector, connector_state, i) {
  7456. if (connector_state->crtc != crtc_state->base.crtc)
  7457. continue;
  7458. encoder = to_intel_encoder(connector_state->best_encoder);
  7459. switch (encoder->type) {
  7460. case INTEL_OUTPUT_LVDS:
  7461. is_lvds = true;
  7462. break;
  7463. case INTEL_OUTPUT_SDVO:
  7464. case INTEL_OUTPUT_HDMI:
  7465. is_sdvo = true;
  7466. break;
  7467. default:
  7468. break;
  7469. }
  7470. num_connectors++;
  7471. }
  7472. /* Enable autotuning of the PLL clock (if permissible) */
  7473. factor = 21;
  7474. if (is_lvds) {
  7475. if ((intel_panel_use_ssc(dev_priv) &&
  7476. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7477. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7478. factor = 25;
  7479. } else if (crtc_state->sdvo_tv_clock)
  7480. factor = 20;
  7481. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7482. *fp |= FP_CB_TUNE;
  7483. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7484. *fp2 |= FP_CB_TUNE;
  7485. dpll = 0;
  7486. if (is_lvds)
  7487. dpll |= DPLLB_MODE_LVDS;
  7488. else
  7489. dpll |= DPLLB_MODE_DAC_SERIAL;
  7490. dpll |= (crtc_state->pixel_multiplier - 1)
  7491. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7492. if (is_sdvo)
  7493. dpll |= DPLL_SDVO_HIGH_SPEED;
  7494. if (crtc_state->has_dp_encoder)
  7495. dpll |= DPLL_SDVO_HIGH_SPEED;
  7496. /* compute bitmask from p1 value */
  7497. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7498. /* also FPA1 */
  7499. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7500. switch (crtc_state->dpll.p2) {
  7501. case 5:
  7502. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7503. break;
  7504. case 7:
  7505. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7506. break;
  7507. case 10:
  7508. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7509. break;
  7510. case 14:
  7511. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7512. break;
  7513. }
  7514. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  7515. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7516. else
  7517. dpll |= PLL_REF_INPUT_DREFCLK;
  7518. return dpll | DPLL_VCO_ENABLE;
  7519. }
  7520. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7521. struct intel_crtc_state *crtc_state)
  7522. {
  7523. struct drm_device *dev = crtc->base.dev;
  7524. intel_clock_t clock, reduced_clock;
  7525. u32 dpll = 0, fp = 0, fp2 = 0;
  7526. bool ok, has_reduced_clock = false;
  7527. bool is_lvds = false;
  7528. struct intel_shared_dpll *pll;
  7529. memset(&crtc_state->dpll_hw_state, 0,
  7530. sizeof(crtc_state->dpll_hw_state));
  7531. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7532. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7533. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7534. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7535. &has_reduced_clock, &reduced_clock);
  7536. if (!ok && !crtc_state->clock_set) {
  7537. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7538. return -EINVAL;
  7539. }
  7540. /* Compat-code for transition, will disappear. */
  7541. if (!crtc_state->clock_set) {
  7542. crtc_state->dpll.n = clock.n;
  7543. crtc_state->dpll.m1 = clock.m1;
  7544. crtc_state->dpll.m2 = clock.m2;
  7545. crtc_state->dpll.p1 = clock.p1;
  7546. crtc_state->dpll.p2 = clock.p2;
  7547. }
  7548. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7549. if (crtc_state->has_pch_encoder) {
  7550. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7551. if (has_reduced_clock)
  7552. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7553. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7554. &fp, &reduced_clock,
  7555. has_reduced_clock ? &fp2 : NULL);
  7556. crtc_state->dpll_hw_state.dpll = dpll;
  7557. crtc_state->dpll_hw_state.fp0 = fp;
  7558. if (has_reduced_clock)
  7559. crtc_state->dpll_hw_state.fp1 = fp2;
  7560. else
  7561. crtc_state->dpll_hw_state.fp1 = fp;
  7562. pll = intel_get_shared_dpll(crtc, crtc_state);
  7563. if (pll == NULL) {
  7564. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7565. pipe_name(crtc->pipe));
  7566. return -EINVAL;
  7567. }
  7568. }
  7569. if (is_lvds && has_reduced_clock)
  7570. crtc->lowfreq_avail = true;
  7571. else
  7572. crtc->lowfreq_avail = false;
  7573. return 0;
  7574. }
  7575. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7576. struct intel_link_m_n *m_n)
  7577. {
  7578. struct drm_device *dev = crtc->base.dev;
  7579. struct drm_i915_private *dev_priv = dev->dev_private;
  7580. enum pipe pipe = crtc->pipe;
  7581. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7582. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7583. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7584. & ~TU_SIZE_MASK;
  7585. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7586. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7587. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7588. }
  7589. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7590. enum transcoder transcoder,
  7591. struct intel_link_m_n *m_n,
  7592. struct intel_link_m_n *m2_n2)
  7593. {
  7594. struct drm_device *dev = crtc->base.dev;
  7595. struct drm_i915_private *dev_priv = dev->dev_private;
  7596. enum pipe pipe = crtc->pipe;
  7597. if (INTEL_INFO(dev)->gen >= 5) {
  7598. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7599. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7600. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7601. & ~TU_SIZE_MASK;
  7602. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7603. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7604. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7605. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7606. * gen < 8) and if DRRS is supported (to make sure the
  7607. * registers are not unnecessarily read).
  7608. */
  7609. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7610. crtc->config->has_drrs) {
  7611. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7612. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7613. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7614. & ~TU_SIZE_MASK;
  7615. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7616. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7617. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7618. }
  7619. } else {
  7620. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7621. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7622. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7623. & ~TU_SIZE_MASK;
  7624. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7625. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7626. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7627. }
  7628. }
  7629. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7630. struct intel_crtc_state *pipe_config)
  7631. {
  7632. if (pipe_config->has_pch_encoder)
  7633. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7634. else
  7635. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7636. &pipe_config->dp_m_n,
  7637. &pipe_config->dp_m2_n2);
  7638. }
  7639. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7640. struct intel_crtc_state *pipe_config)
  7641. {
  7642. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7643. &pipe_config->fdi_m_n, NULL);
  7644. }
  7645. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7646. struct intel_crtc_state *pipe_config)
  7647. {
  7648. struct drm_device *dev = crtc->base.dev;
  7649. struct drm_i915_private *dev_priv = dev->dev_private;
  7650. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7651. uint32_t ps_ctrl = 0;
  7652. int id = -1;
  7653. int i;
  7654. /* find scaler attached to this pipe */
  7655. for (i = 0; i < crtc->num_scalers; i++) {
  7656. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7657. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7658. id = i;
  7659. pipe_config->pch_pfit.enabled = true;
  7660. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7661. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7662. break;
  7663. }
  7664. }
  7665. scaler_state->scaler_id = id;
  7666. if (id >= 0) {
  7667. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7668. } else {
  7669. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7670. }
  7671. }
  7672. static void
  7673. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7674. struct intel_initial_plane_config *plane_config)
  7675. {
  7676. struct drm_device *dev = crtc->base.dev;
  7677. struct drm_i915_private *dev_priv = dev->dev_private;
  7678. u32 val, base, offset, stride_mult, tiling;
  7679. int pipe = crtc->pipe;
  7680. int fourcc, pixel_format;
  7681. unsigned int aligned_height;
  7682. struct drm_framebuffer *fb;
  7683. struct intel_framebuffer *intel_fb;
  7684. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7685. if (!intel_fb) {
  7686. DRM_DEBUG_KMS("failed to alloc fb\n");
  7687. return;
  7688. }
  7689. fb = &intel_fb->base;
  7690. val = I915_READ(PLANE_CTL(pipe, 0));
  7691. if (!(val & PLANE_CTL_ENABLE))
  7692. goto error;
  7693. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7694. fourcc = skl_format_to_fourcc(pixel_format,
  7695. val & PLANE_CTL_ORDER_RGBX,
  7696. val & PLANE_CTL_ALPHA_MASK);
  7697. fb->pixel_format = fourcc;
  7698. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7699. tiling = val & PLANE_CTL_TILED_MASK;
  7700. switch (tiling) {
  7701. case PLANE_CTL_TILED_LINEAR:
  7702. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7703. break;
  7704. case PLANE_CTL_TILED_X:
  7705. plane_config->tiling = I915_TILING_X;
  7706. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7707. break;
  7708. case PLANE_CTL_TILED_Y:
  7709. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7710. break;
  7711. case PLANE_CTL_TILED_YF:
  7712. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7713. break;
  7714. default:
  7715. MISSING_CASE(tiling);
  7716. goto error;
  7717. }
  7718. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7719. plane_config->base = base;
  7720. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7721. val = I915_READ(PLANE_SIZE(pipe, 0));
  7722. fb->height = ((val >> 16) & 0xfff) + 1;
  7723. fb->width = ((val >> 0) & 0x1fff) + 1;
  7724. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7725. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7726. fb->pixel_format);
  7727. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7728. aligned_height = intel_fb_align_height(dev, fb->height,
  7729. fb->pixel_format,
  7730. fb->modifier[0]);
  7731. plane_config->size = fb->pitches[0] * aligned_height;
  7732. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7733. pipe_name(pipe), fb->width, fb->height,
  7734. fb->bits_per_pixel, base, fb->pitches[0],
  7735. plane_config->size);
  7736. plane_config->fb = intel_fb;
  7737. return;
  7738. error:
  7739. kfree(fb);
  7740. }
  7741. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7742. struct intel_crtc_state *pipe_config)
  7743. {
  7744. struct drm_device *dev = crtc->base.dev;
  7745. struct drm_i915_private *dev_priv = dev->dev_private;
  7746. uint32_t tmp;
  7747. tmp = I915_READ(PF_CTL(crtc->pipe));
  7748. if (tmp & PF_ENABLE) {
  7749. pipe_config->pch_pfit.enabled = true;
  7750. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7751. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7752. /* We currently do not free assignements of panel fitters on
  7753. * ivb/hsw (since we don't use the higher upscaling modes which
  7754. * differentiates them) so just WARN about this case for now. */
  7755. if (IS_GEN7(dev)) {
  7756. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7757. PF_PIPE_SEL_IVB(crtc->pipe));
  7758. }
  7759. }
  7760. }
  7761. static void
  7762. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7763. struct intel_initial_plane_config *plane_config)
  7764. {
  7765. struct drm_device *dev = crtc->base.dev;
  7766. struct drm_i915_private *dev_priv = dev->dev_private;
  7767. u32 val, base, offset;
  7768. int pipe = crtc->pipe;
  7769. int fourcc, pixel_format;
  7770. unsigned int aligned_height;
  7771. struct drm_framebuffer *fb;
  7772. struct intel_framebuffer *intel_fb;
  7773. val = I915_READ(DSPCNTR(pipe));
  7774. if (!(val & DISPLAY_PLANE_ENABLE))
  7775. return;
  7776. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7777. if (!intel_fb) {
  7778. DRM_DEBUG_KMS("failed to alloc fb\n");
  7779. return;
  7780. }
  7781. fb = &intel_fb->base;
  7782. if (INTEL_INFO(dev)->gen >= 4) {
  7783. if (val & DISPPLANE_TILED) {
  7784. plane_config->tiling = I915_TILING_X;
  7785. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7786. }
  7787. }
  7788. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7789. fourcc = i9xx_format_to_fourcc(pixel_format);
  7790. fb->pixel_format = fourcc;
  7791. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7792. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7793. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7794. offset = I915_READ(DSPOFFSET(pipe));
  7795. } else {
  7796. if (plane_config->tiling)
  7797. offset = I915_READ(DSPTILEOFF(pipe));
  7798. else
  7799. offset = I915_READ(DSPLINOFF(pipe));
  7800. }
  7801. plane_config->base = base;
  7802. val = I915_READ(PIPESRC(pipe));
  7803. fb->width = ((val >> 16) & 0xfff) + 1;
  7804. fb->height = ((val >> 0) & 0xfff) + 1;
  7805. val = I915_READ(DSPSTRIDE(pipe));
  7806. fb->pitches[0] = val & 0xffffffc0;
  7807. aligned_height = intel_fb_align_height(dev, fb->height,
  7808. fb->pixel_format,
  7809. fb->modifier[0]);
  7810. plane_config->size = fb->pitches[0] * aligned_height;
  7811. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7812. pipe_name(pipe), fb->width, fb->height,
  7813. fb->bits_per_pixel, base, fb->pitches[0],
  7814. plane_config->size);
  7815. plane_config->fb = intel_fb;
  7816. }
  7817. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7818. struct intel_crtc_state *pipe_config)
  7819. {
  7820. struct drm_device *dev = crtc->base.dev;
  7821. struct drm_i915_private *dev_priv = dev->dev_private;
  7822. uint32_t tmp;
  7823. if (!intel_display_power_is_enabled(dev_priv,
  7824. POWER_DOMAIN_PIPE(crtc->pipe)))
  7825. return false;
  7826. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7827. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7828. tmp = I915_READ(PIPECONF(crtc->pipe));
  7829. if (!(tmp & PIPECONF_ENABLE))
  7830. return false;
  7831. switch (tmp & PIPECONF_BPC_MASK) {
  7832. case PIPECONF_6BPC:
  7833. pipe_config->pipe_bpp = 18;
  7834. break;
  7835. case PIPECONF_8BPC:
  7836. pipe_config->pipe_bpp = 24;
  7837. break;
  7838. case PIPECONF_10BPC:
  7839. pipe_config->pipe_bpp = 30;
  7840. break;
  7841. case PIPECONF_12BPC:
  7842. pipe_config->pipe_bpp = 36;
  7843. break;
  7844. default:
  7845. break;
  7846. }
  7847. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7848. pipe_config->limited_color_range = true;
  7849. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7850. struct intel_shared_dpll *pll;
  7851. pipe_config->has_pch_encoder = true;
  7852. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7853. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7854. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7855. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7856. if (HAS_PCH_IBX(dev_priv->dev)) {
  7857. pipe_config->shared_dpll =
  7858. (enum intel_dpll_id) crtc->pipe;
  7859. } else {
  7860. tmp = I915_READ(PCH_DPLL_SEL);
  7861. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7862. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7863. else
  7864. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7865. }
  7866. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7867. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7868. &pipe_config->dpll_hw_state));
  7869. tmp = pipe_config->dpll_hw_state.dpll;
  7870. pipe_config->pixel_multiplier =
  7871. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7872. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7873. ironlake_pch_clock_get(crtc, pipe_config);
  7874. } else {
  7875. pipe_config->pixel_multiplier = 1;
  7876. }
  7877. intel_get_pipe_timings(crtc, pipe_config);
  7878. ironlake_get_pfit_config(crtc, pipe_config);
  7879. return true;
  7880. }
  7881. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7882. {
  7883. struct drm_device *dev = dev_priv->dev;
  7884. struct intel_crtc *crtc;
  7885. for_each_intel_crtc(dev, crtc)
  7886. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7887. pipe_name(crtc->pipe));
  7888. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7889. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7890. I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7891. I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7892. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7893. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7894. "CPU PWM1 enabled\n");
  7895. if (IS_HASWELL(dev))
  7896. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7897. "CPU PWM2 enabled\n");
  7898. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7899. "PCH PWM1 enabled\n");
  7900. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7901. "Utility pin enabled\n");
  7902. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7903. /*
  7904. * In theory we can still leave IRQs enabled, as long as only the HPD
  7905. * interrupts remain enabled. We used to check for that, but since it's
  7906. * gen-specific and since we only disable LCPLL after we fully disable
  7907. * the interrupts, the check below should be enough.
  7908. */
  7909. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7910. }
  7911. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7912. {
  7913. struct drm_device *dev = dev_priv->dev;
  7914. if (IS_HASWELL(dev))
  7915. return I915_READ(D_COMP_HSW);
  7916. else
  7917. return I915_READ(D_COMP_BDW);
  7918. }
  7919. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7920. {
  7921. struct drm_device *dev = dev_priv->dev;
  7922. if (IS_HASWELL(dev)) {
  7923. mutex_lock(&dev_priv->rps.hw_lock);
  7924. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7925. val))
  7926. DRM_ERROR("Failed to write to D_COMP\n");
  7927. mutex_unlock(&dev_priv->rps.hw_lock);
  7928. } else {
  7929. I915_WRITE(D_COMP_BDW, val);
  7930. POSTING_READ(D_COMP_BDW);
  7931. }
  7932. }
  7933. /*
  7934. * This function implements pieces of two sequences from BSpec:
  7935. * - Sequence for display software to disable LCPLL
  7936. * - Sequence for display software to allow package C8+
  7937. * The steps implemented here are just the steps that actually touch the LCPLL
  7938. * register. Callers should take care of disabling all the display engine
  7939. * functions, doing the mode unset, fixing interrupts, etc.
  7940. */
  7941. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7942. bool switch_to_fclk, bool allow_power_down)
  7943. {
  7944. uint32_t val;
  7945. assert_can_disable_lcpll(dev_priv);
  7946. val = I915_READ(LCPLL_CTL);
  7947. if (switch_to_fclk) {
  7948. val |= LCPLL_CD_SOURCE_FCLK;
  7949. I915_WRITE(LCPLL_CTL, val);
  7950. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7951. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7952. DRM_ERROR("Switching to FCLK failed\n");
  7953. val = I915_READ(LCPLL_CTL);
  7954. }
  7955. val |= LCPLL_PLL_DISABLE;
  7956. I915_WRITE(LCPLL_CTL, val);
  7957. POSTING_READ(LCPLL_CTL);
  7958. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7959. DRM_ERROR("LCPLL still locked\n");
  7960. val = hsw_read_dcomp(dev_priv);
  7961. val |= D_COMP_COMP_DISABLE;
  7962. hsw_write_dcomp(dev_priv, val);
  7963. ndelay(100);
  7964. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7965. 1))
  7966. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7967. if (allow_power_down) {
  7968. val = I915_READ(LCPLL_CTL);
  7969. val |= LCPLL_POWER_DOWN_ALLOW;
  7970. I915_WRITE(LCPLL_CTL, val);
  7971. POSTING_READ(LCPLL_CTL);
  7972. }
  7973. }
  7974. /*
  7975. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7976. * source.
  7977. */
  7978. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7979. {
  7980. uint32_t val;
  7981. val = I915_READ(LCPLL_CTL);
  7982. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7983. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7984. return;
  7985. /*
  7986. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7987. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7988. */
  7989. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7990. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7991. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7992. I915_WRITE(LCPLL_CTL, val);
  7993. POSTING_READ(LCPLL_CTL);
  7994. }
  7995. val = hsw_read_dcomp(dev_priv);
  7996. val |= D_COMP_COMP_FORCE;
  7997. val &= ~D_COMP_COMP_DISABLE;
  7998. hsw_write_dcomp(dev_priv, val);
  7999. val = I915_READ(LCPLL_CTL);
  8000. val &= ~LCPLL_PLL_DISABLE;
  8001. I915_WRITE(LCPLL_CTL, val);
  8002. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  8003. DRM_ERROR("LCPLL not locked yet\n");
  8004. if (val & LCPLL_CD_SOURCE_FCLK) {
  8005. val = I915_READ(LCPLL_CTL);
  8006. val &= ~LCPLL_CD_SOURCE_FCLK;
  8007. I915_WRITE(LCPLL_CTL, val);
  8008. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8009. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8010. DRM_ERROR("Switching back to LCPLL failed\n");
  8011. }
  8012. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8013. intel_update_cdclk(dev_priv->dev);
  8014. }
  8015. /*
  8016. * Package states C8 and deeper are really deep PC states that can only be
  8017. * reached when all the devices on the system allow it, so even if the graphics
  8018. * device allows PC8+, it doesn't mean the system will actually get to these
  8019. * states. Our driver only allows PC8+ when going into runtime PM.
  8020. *
  8021. * The requirements for PC8+ are that all the outputs are disabled, the power
  8022. * well is disabled and most interrupts are disabled, and these are also
  8023. * requirements for runtime PM. When these conditions are met, we manually do
  8024. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8025. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8026. * hang the machine.
  8027. *
  8028. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8029. * the state of some registers, so when we come back from PC8+ we need to
  8030. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8031. * need to take care of the registers kept by RC6. Notice that this happens even
  8032. * if we don't put the device in PCI D3 state (which is what currently happens
  8033. * because of the runtime PM support).
  8034. *
  8035. * For more, read "Display Sequences for Package C8" on the hardware
  8036. * documentation.
  8037. */
  8038. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8039. {
  8040. struct drm_device *dev = dev_priv->dev;
  8041. uint32_t val;
  8042. DRM_DEBUG_KMS("Enabling package C8+\n");
  8043. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8044. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8045. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8046. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8047. }
  8048. lpt_disable_clkout_dp(dev);
  8049. hsw_disable_lcpll(dev_priv, true, true);
  8050. }
  8051. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8052. {
  8053. struct drm_device *dev = dev_priv->dev;
  8054. uint32_t val;
  8055. DRM_DEBUG_KMS("Disabling package C8+\n");
  8056. hsw_restore_lcpll(dev_priv);
  8057. lpt_init_pch_refclk(dev);
  8058. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  8059. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8060. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8061. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8062. }
  8063. intel_prepare_ddi(dev);
  8064. }
  8065. static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
  8066. {
  8067. struct drm_device *dev = old_state->dev;
  8068. struct drm_i915_private *dev_priv = dev->dev_private;
  8069. int max_pixclk = intel_mode_max_pixclk(dev, NULL);
  8070. int req_cdclk;
  8071. /* see the comment in valleyview_modeset_global_resources */
  8072. if (WARN_ON(max_pixclk < 0))
  8073. return;
  8074. req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
  8075. if (req_cdclk != dev_priv->cdclk_freq)
  8076. broxton_set_cdclk(dev, req_cdclk);
  8077. }
  8078. /* compute the max rate for new configuration */
  8079. static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
  8080. {
  8081. struct drm_device *dev = dev_priv->dev;
  8082. struct intel_crtc *intel_crtc;
  8083. struct drm_crtc *crtc;
  8084. int max_pixel_rate = 0;
  8085. int pixel_rate;
  8086. for_each_crtc(dev, crtc) {
  8087. if (!crtc->state->enable)
  8088. continue;
  8089. intel_crtc = to_intel_crtc(crtc);
  8090. pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
  8091. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8092. if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
  8093. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8094. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8095. }
  8096. return max_pixel_rate;
  8097. }
  8098. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8099. {
  8100. struct drm_i915_private *dev_priv = dev->dev_private;
  8101. uint32_t val, data;
  8102. int ret;
  8103. if (WARN((I915_READ(LCPLL_CTL) &
  8104. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8105. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8106. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8107. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8108. "trying to change cdclk frequency with cdclk not enabled\n"))
  8109. return;
  8110. mutex_lock(&dev_priv->rps.hw_lock);
  8111. ret = sandybridge_pcode_write(dev_priv,
  8112. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8113. mutex_unlock(&dev_priv->rps.hw_lock);
  8114. if (ret) {
  8115. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8116. return;
  8117. }
  8118. val = I915_READ(LCPLL_CTL);
  8119. val |= LCPLL_CD_SOURCE_FCLK;
  8120. I915_WRITE(LCPLL_CTL, val);
  8121. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8122. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8123. DRM_ERROR("Switching to FCLK failed\n");
  8124. val = I915_READ(LCPLL_CTL);
  8125. val &= ~LCPLL_CLK_FREQ_MASK;
  8126. switch (cdclk) {
  8127. case 450000:
  8128. val |= LCPLL_CLK_FREQ_450;
  8129. data = 0;
  8130. break;
  8131. case 540000:
  8132. val |= LCPLL_CLK_FREQ_54O_BDW;
  8133. data = 1;
  8134. break;
  8135. case 337500:
  8136. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8137. data = 2;
  8138. break;
  8139. case 675000:
  8140. val |= LCPLL_CLK_FREQ_675_BDW;
  8141. data = 3;
  8142. break;
  8143. default:
  8144. WARN(1, "invalid cdclk frequency\n");
  8145. return;
  8146. }
  8147. I915_WRITE(LCPLL_CTL, val);
  8148. val = I915_READ(LCPLL_CTL);
  8149. val &= ~LCPLL_CD_SOURCE_FCLK;
  8150. I915_WRITE(LCPLL_CTL, val);
  8151. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8152. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8153. DRM_ERROR("Switching back to LCPLL failed\n");
  8154. mutex_lock(&dev_priv->rps.hw_lock);
  8155. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8156. mutex_unlock(&dev_priv->rps.hw_lock);
  8157. intel_update_cdclk(dev);
  8158. WARN(cdclk != dev_priv->cdclk_freq,
  8159. "cdclk requested %d kHz but got %d kHz\n",
  8160. cdclk, dev_priv->cdclk_freq);
  8161. }
  8162. static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
  8163. int max_pixel_rate)
  8164. {
  8165. int cdclk;
  8166. /*
  8167. * FIXME should also account for plane ratio
  8168. * once 64bpp pixel formats are supported.
  8169. */
  8170. if (max_pixel_rate > 540000)
  8171. cdclk = 675000;
  8172. else if (max_pixel_rate > 450000)
  8173. cdclk = 540000;
  8174. else if (max_pixel_rate > 337500)
  8175. cdclk = 450000;
  8176. else
  8177. cdclk = 337500;
  8178. /*
  8179. * FIXME move the cdclk caclulation to
  8180. * compute_config() so we can fail gracegully.
  8181. */
  8182. if (cdclk > dev_priv->max_cdclk_freq) {
  8183. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8184. cdclk, dev_priv->max_cdclk_freq);
  8185. cdclk = dev_priv->max_cdclk_freq;
  8186. }
  8187. return cdclk;
  8188. }
  8189. static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
  8190. {
  8191. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8192. struct drm_crtc *crtc;
  8193. struct drm_crtc_state *crtc_state;
  8194. int max_pixclk = ilk_max_pixel_rate(dev_priv);
  8195. int cdclk, i;
  8196. cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
  8197. if (cdclk == dev_priv->cdclk_freq)
  8198. return 0;
  8199. /* add all active pipes to the state */
  8200. for_each_crtc(state->dev, crtc) {
  8201. if (!crtc->state->enable)
  8202. continue;
  8203. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  8204. if (IS_ERR(crtc_state))
  8205. return PTR_ERR(crtc_state);
  8206. }
  8207. /* disable/enable all currently active pipes while we change cdclk */
  8208. for_each_crtc_in_state(state, crtc, crtc_state, i)
  8209. if (crtc_state->enable)
  8210. crtc_state->mode_changed = true;
  8211. return 0;
  8212. }
  8213. static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
  8214. {
  8215. struct drm_device *dev = state->dev;
  8216. struct drm_i915_private *dev_priv = dev->dev_private;
  8217. int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
  8218. int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
  8219. if (req_cdclk != dev_priv->cdclk_freq)
  8220. broadwell_set_cdclk(dev, req_cdclk);
  8221. }
  8222. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8223. struct intel_crtc_state *crtc_state)
  8224. {
  8225. if (!intel_ddi_pll_select(crtc, crtc_state))
  8226. return -EINVAL;
  8227. crtc->lowfreq_avail = false;
  8228. return 0;
  8229. }
  8230. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8231. enum port port,
  8232. struct intel_crtc_state *pipe_config)
  8233. {
  8234. switch (port) {
  8235. case PORT_A:
  8236. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8237. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8238. break;
  8239. case PORT_B:
  8240. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8241. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8242. break;
  8243. case PORT_C:
  8244. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8245. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8246. break;
  8247. default:
  8248. DRM_ERROR("Incorrect port type\n");
  8249. }
  8250. }
  8251. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8252. enum port port,
  8253. struct intel_crtc_state *pipe_config)
  8254. {
  8255. u32 temp, dpll_ctl1;
  8256. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8257. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8258. switch (pipe_config->ddi_pll_sel) {
  8259. case SKL_DPLL0:
  8260. /*
  8261. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8262. * of the shared DPLL framework and thus needs to be read out
  8263. * separately
  8264. */
  8265. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8266. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8267. break;
  8268. case SKL_DPLL1:
  8269. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8270. break;
  8271. case SKL_DPLL2:
  8272. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8273. break;
  8274. case SKL_DPLL3:
  8275. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8276. break;
  8277. }
  8278. }
  8279. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8280. enum port port,
  8281. struct intel_crtc_state *pipe_config)
  8282. {
  8283. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8284. switch (pipe_config->ddi_pll_sel) {
  8285. case PORT_CLK_SEL_WRPLL1:
  8286. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8287. break;
  8288. case PORT_CLK_SEL_WRPLL2:
  8289. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8290. break;
  8291. }
  8292. }
  8293. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8294. struct intel_crtc_state *pipe_config)
  8295. {
  8296. struct drm_device *dev = crtc->base.dev;
  8297. struct drm_i915_private *dev_priv = dev->dev_private;
  8298. struct intel_shared_dpll *pll;
  8299. enum port port;
  8300. uint32_t tmp;
  8301. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8302. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8303. if (IS_SKYLAKE(dev))
  8304. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8305. else if (IS_BROXTON(dev))
  8306. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8307. else
  8308. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8309. if (pipe_config->shared_dpll >= 0) {
  8310. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8311. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8312. &pipe_config->dpll_hw_state));
  8313. }
  8314. /*
  8315. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8316. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8317. * the PCH transcoder is on.
  8318. */
  8319. if (INTEL_INFO(dev)->gen < 9 &&
  8320. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8321. pipe_config->has_pch_encoder = true;
  8322. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8323. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8324. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8325. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8326. }
  8327. }
  8328. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8329. struct intel_crtc_state *pipe_config)
  8330. {
  8331. struct drm_device *dev = crtc->base.dev;
  8332. struct drm_i915_private *dev_priv = dev->dev_private;
  8333. enum intel_display_power_domain pfit_domain;
  8334. uint32_t tmp;
  8335. if (!intel_display_power_is_enabled(dev_priv,
  8336. POWER_DOMAIN_PIPE(crtc->pipe)))
  8337. return false;
  8338. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8339. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8340. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8341. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8342. enum pipe trans_edp_pipe;
  8343. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8344. default:
  8345. WARN(1, "unknown pipe linked to edp transcoder\n");
  8346. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8347. case TRANS_DDI_EDP_INPUT_A_ON:
  8348. trans_edp_pipe = PIPE_A;
  8349. break;
  8350. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8351. trans_edp_pipe = PIPE_B;
  8352. break;
  8353. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8354. trans_edp_pipe = PIPE_C;
  8355. break;
  8356. }
  8357. if (trans_edp_pipe == crtc->pipe)
  8358. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8359. }
  8360. if (!intel_display_power_is_enabled(dev_priv,
  8361. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8362. return false;
  8363. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8364. if (!(tmp & PIPECONF_ENABLE))
  8365. return false;
  8366. haswell_get_ddi_port_state(crtc, pipe_config);
  8367. intel_get_pipe_timings(crtc, pipe_config);
  8368. if (INTEL_INFO(dev)->gen >= 9) {
  8369. skl_init_scalers(dev, crtc, pipe_config);
  8370. }
  8371. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8372. if (INTEL_INFO(dev)->gen >= 9) {
  8373. pipe_config->scaler_state.scaler_id = -1;
  8374. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8375. }
  8376. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8377. if (INTEL_INFO(dev)->gen == 9)
  8378. skylake_get_pfit_config(crtc, pipe_config);
  8379. else if (INTEL_INFO(dev)->gen < 9)
  8380. ironlake_get_pfit_config(crtc, pipe_config);
  8381. else
  8382. MISSING_CASE(INTEL_INFO(dev)->gen);
  8383. }
  8384. if (IS_HASWELL(dev))
  8385. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8386. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8387. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8388. pipe_config->pixel_multiplier =
  8389. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8390. } else {
  8391. pipe_config->pixel_multiplier = 1;
  8392. }
  8393. return true;
  8394. }
  8395. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8396. {
  8397. struct drm_device *dev = crtc->dev;
  8398. struct drm_i915_private *dev_priv = dev->dev_private;
  8399. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8400. uint32_t cntl = 0, size = 0;
  8401. if (base) {
  8402. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8403. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8404. unsigned int stride = roundup_pow_of_two(width) * 4;
  8405. switch (stride) {
  8406. default:
  8407. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8408. width, stride);
  8409. stride = 256;
  8410. /* fallthrough */
  8411. case 256:
  8412. case 512:
  8413. case 1024:
  8414. case 2048:
  8415. break;
  8416. }
  8417. cntl |= CURSOR_ENABLE |
  8418. CURSOR_GAMMA_ENABLE |
  8419. CURSOR_FORMAT_ARGB |
  8420. CURSOR_STRIDE(stride);
  8421. size = (height << 12) | width;
  8422. }
  8423. if (intel_crtc->cursor_cntl != 0 &&
  8424. (intel_crtc->cursor_base != base ||
  8425. intel_crtc->cursor_size != size ||
  8426. intel_crtc->cursor_cntl != cntl)) {
  8427. /* On these chipsets we can only modify the base/size/stride
  8428. * whilst the cursor is disabled.
  8429. */
  8430. I915_WRITE(_CURACNTR, 0);
  8431. POSTING_READ(_CURACNTR);
  8432. intel_crtc->cursor_cntl = 0;
  8433. }
  8434. if (intel_crtc->cursor_base != base) {
  8435. I915_WRITE(_CURABASE, base);
  8436. intel_crtc->cursor_base = base;
  8437. }
  8438. if (intel_crtc->cursor_size != size) {
  8439. I915_WRITE(CURSIZE, size);
  8440. intel_crtc->cursor_size = size;
  8441. }
  8442. if (intel_crtc->cursor_cntl != cntl) {
  8443. I915_WRITE(_CURACNTR, cntl);
  8444. POSTING_READ(_CURACNTR);
  8445. intel_crtc->cursor_cntl = cntl;
  8446. }
  8447. }
  8448. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8449. {
  8450. struct drm_device *dev = crtc->dev;
  8451. struct drm_i915_private *dev_priv = dev->dev_private;
  8452. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8453. int pipe = intel_crtc->pipe;
  8454. uint32_t cntl;
  8455. cntl = 0;
  8456. if (base) {
  8457. cntl = MCURSOR_GAMMA_ENABLE;
  8458. switch (intel_crtc->base.cursor->state->crtc_w) {
  8459. case 64:
  8460. cntl |= CURSOR_MODE_64_ARGB_AX;
  8461. break;
  8462. case 128:
  8463. cntl |= CURSOR_MODE_128_ARGB_AX;
  8464. break;
  8465. case 256:
  8466. cntl |= CURSOR_MODE_256_ARGB_AX;
  8467. break;
  8468. default:
  8469. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8470. return;
  8471. }
  8472. cntl |= pipe << 28; /* Connect to correct pipe */
  8473. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  8474. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8475. }
  8476. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8477. cntl |= CURSOR_ROTATE_180;
  8478. if (intel_crtc->cursor_cntl != cntl) {
  8479. I915_WRITE(CURCNTR(pipe), cntl);
  8480. POSTING_READ(CURCNTR(pipe));
  8481. intel_crtc->cursor_cntl = cntl;
  8482. }
  8483. /* and commit changes on next vblank */
  8484. I915_WRITE(CURBASE(pipe), base);
  8485. POSTING_READ(CURBASE(pipe));
  8486. intel_crtc->cursor_base = base;
  8487. }
  8488. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8489. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8490. bool on)
  8491. {
  8492. struct drm_device *dev = crtc->dev;
  8493. struct drm_i915_private *dev_priv = dev->dev_private;
  8494. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8495. int pipe = intel_crtc->pipe;
  8496. int x = crtc->cursor_x;
  8497. int y = crtc->cursor_y;
  8498. u32 base = 0, pos = 0;
  8499. if (on)
  8500. base = intel_crtc->cursor_addr;
  8501. if (x >= intel_crtc->config->pipe_src_w)
  8502. base = 0;
  8503. if (y >= intel_crtc->config->pipe_src_h)
  8504. base = 0;
  8505. if (x < 0) {
  8506. if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
  8507. base = 0;
  8508. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8509. x = -x;
  8510. }
  8511. pos |= x << CURSOR_X_SHIFT;
  8512. if (y < 0) {
  8513. if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
  8514. base = 0;
  8515. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8516. y = -y;
  8517. }
  8518. pos |= y << CURSOR_Y_SHIFT;
  8519. if (base == 0 && intel_crtc->cursor_base == 0)
  8520. return;
  8521. I915_WRITE(CURPOS(pipe), pos);
  8522. /* ILK+ do this automagically */
  8523. if (HAS_GMCH_DISPLAY(dev) &&
  8524. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8525. base += (intel_crtc->base.cursor->state->crtc_h *
  8526. intel_crtc->base.cursor->state->crtc_w - 1) * 4;
  8527. }
  8528. if (IS_845G(dev) || IS_I865G(dev))
  8529. i845_update_cursor(crtc, base);
  8530. else
  8531. i9xx_update_cursor(crtc, base);
  8532. }
  8533. static bool cursor_size_ok(struct drm_device *dev,
  8534. uint32_t width, uint32_t height)
  8535. {
  8536. if (width == 0 || height == 0)
  8537. return false;
  8538. /*
  8539. * 845g/865g are special in that they are only limited by
  8540. * the width of their cursors, the height is arbitrary up to
  8541. * the precision of the register. Everything else requires
  8542. * square cursors, limited to a few power-of-two sizes.
  8543. */
  8544. if (IS_845G(dev) || IS_I865G(dev)) {
  8545. if ((width & 63) != 0)
  8546. return false;
  8547. if (width > (IS_845G(dev) ? 64 : 512))
  8548. return false;
  8549. if (height > 1023)
  8550. return false;
  8551. } else {
  8552. switch (width | height) {
  8553. case 256:
  8554. case 128:
  8555. if (IS_GEN2(dev))
  8556. return false;
  8557. case 64:
  8558. break;
  8559. default:
  8560. return false;
  8561. }
  8562. }
  8563. return true;
  8564. }
  8565. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8566. u16 *blue, uint32_t start, uint32_t size)
  8567. {
  8568. int end = (start + size > 256) ? 256 : start + size, i;
  8569. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8570. for (i = start; i < end; i++) {
  8571. intel_crtc->lut_r[i] = red[i] >> 8;
  8572. intel_crtc->lut_g[i] = green[i] >> 8;
  8573. intel_crtc->lut_b[i] = blue[i] >> 8;
  8574. }
  8575. intel_crtc_load_lut(crtc);
  8576. }
  8577. /* VESA 640x480x72Hz mode to set on the pipe */
  8578. static struct drm_display_mode load_detect_mode = {
  8579. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8580. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8581. };
  8582. struct drm_framebuffer *
  8583. __intel_framebuffer_create(struct drm_device *dev,
  8584. struct drm_mode_fb_cmd2 *mode_cmd,
  8585. struct drm_i915_gem_object *obj)
  8586. {
  8587. struct intel_framebuffer *intel_fb;
  8588. int ret;
  8589. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8590. if (!intel_fb) {
  8591. drm_gem_object_unreference(&obj->base);
  8592. return ERR_PTR(-ENOMEM);
  8593. }
  8594. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8595. if (ret)
  8596. goto err;
  8597. return &intel_fb->base;
  8598. err:
  8599. drm_gem_object_unreference(&obj->base);
  8600. kfree(intel_fb);
  8601. return ERR_PTR(ret);
  8602. }
  8603. static struct drm_framebuffer *
  8604. intel_framebuffer_create(struct drm_device *dev,
  8605. struct drm_mode_fb_cmd2 *mode_cmd,
  8606. struct drm_i915_gem_object *obj)
  8607. {
  8608. struct drm_framebuffer *fb;
  8609. int ret;
  8610. ret = i915_mutex_lock_interruptible(dev);
  8611. if (ret)
  8612. return ERR_PTR(ret);
  8613. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8614. mutex_unlock(&dev->struct_mutex);
  8615. return fb;
  8616. }
  8617. static u32
  8618. intel_framebuffer_pitch_for_width(int width, int bpp)
  8619. {
  8620. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8621. return ALIGN(pitch, 64);
  8622. }
  8623. static u32
  8624. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8625. {
  8626. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8627. return PAGE_ALIGN(pitch * mode->vdisplay);
  8628. }
  8629. static struct drm_framebuffer *
  8630. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8631. struct drm_display_mode *mode,
  8632. int depth, int bpp)
  8633. {
  8634. struct drm_i915_gem_object *obj;
  8635. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8636. obj = i915_gem_alloc_object(dev,
  8637. intel_framebuffer_size_for_mode(mode, bpp));
  8638. if (obj == NULL)
  8639. return ERR_PTR(-ENOMEM);
  8640. mode_cmd.width = mode->hdisplay;
  8641. mode_cmd.height = mode->vdisplay;
  8642. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8643. bpp);
  8644. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8645. return intel_framebuffer_create(dev, &mode_cmd, obj);
  8646. }
  8647. static struct drm_framebuffer *
  8648. mode_fits_in_fbdev(struct drm_device *dev,
  8649. struct drm_display_mode *mode)
  8650. {
  8651. #ifdef CONFIG_DRM_I915_FBDEV
  8652. struct drm_i915_private *dev_priv = dev->dev_private;
  8653. struct drm_i915_gem_object *obj;
  8654. struct drm_framebuffer *fb;
  8655. if (!dev_priv->fbdev)
  8656. return NULL;
  8657. if (!dev_priv->fbdev->fb)
  8658. return NULL;
  8659. obj = dev_priv->fbdev->fb->obj;
  8660. BUG_ON(!obj);
  8661. fb = &dev_priv->fbdev->fb->base;
  8662. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8663. fb->bits_per_pixel))
  8664. return NULL;
  8665. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8666. return NULL;
  8667. return fb;
  8668. #else
  8669. return NULL;
  8670. #endif
  8671. }
  8672. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8673. struct drm_crtc *crtc,
  8674. struct drm_display_mode *mode,
  8675. struct drm_framebuffer *fb,
  8676. int x, int y)
  8677. {
  8678. struct drm_plane_state *plane_state;
  8679. int hdisplay, vdisplay;
  8680. int ret;
  8681. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8682. if (IS_ERR(plane_state))
  8683. return PTR_ERR(plane_state);
  8684. if (mode)
  8685. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8686. else
  8687. hdisplay = vdisplay = 0;
  8688. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8689. if (ret)
  8690. return ret;
  8691. drm_atomic_set_fb_for_plane(plane_state, fb);
  8692. plane_state->crtc_x = 0;
  8693. plane_state->crtc_y = 0;
  8694. plane_state->crtc_w = hdisplay;
  8695. plane_state->crtc_h = vdisplay;
  8696. plane_state->src_x = x << 16;
  8697. plane_state->src_y = y << 16;
  8698. plane_state->src_w = hdisplay << 16;
  8699. plane_state->src_h = vdisplay << 16;
  8700. return 0;
  8701. }
  8702. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8703. struct drm_display_mode *mode,
  8704. struct intel_load_detect_pipe *old,
  8705. struct drm_modeset_acquire_ctx *ctx)
  8706. {
  8707. struct intel_crtc *intel_crtc;
  8708. struct intel_encoder *intel_encoder =
  8709. intel_attached_encoder(connector);
  8710. struct drm_crtc *possible_crtc;
  8711. struct drm_encoder *encoder = &intel_encoder->base;
  8712. struct drm_crtc *crtc = NULL;
  8713. struct drm_device *dev = encoder->dev;
  8714. struct drm_framebuffer *fb;
  8715. struct drm_mode_config *config = &dev->mode_config;
  8716. struct drm_atomic_state *state = NULL;
  8717. struct drm_connector_state *connector_state;
  8718. struct intel_crtc_state *crtc_state;
  8719. int ret, i = -1;
  8720. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8721. connector->base.id, connector->name,
  8722. encoder->base.id, encoder->name);
  8723. retry:
  8724. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8725. if (ret)
  8726. goto fail_unlock;
  8727. /*
  8728. * Algorithm gets a little messy:
  8729. *
  8730. * - if the connector already has an assigned crtc, use it (but make
  8731. * sure it's on first)
  8732. *
  8733. * - try to find the first unused crtc that can drive this connector,
  8734. * and use that if we find one
  8735. */
  8736. /* See if we already have a CRTC for this connector */
  8737. if (encoder->crtc) {
  8738. crtc = encoder->crtc;
  8739. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8740. if (ret)
  8741. goto fail_unlock;
  8742. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8743. if (ret)
  8744. goto fail_unlock;
  8745. old->dpms_mode = connector->dpms;
  8746. old->load_detect_temp = false;
  8747. /* Make sure the crtc and connector are running */
  8748. if (connector->dpms != DRM_MODE_DPMS_ON)
  8749. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8750. return true;
  8751. }
  8752. /* Find an unused one (if possible) */
  8753. for_each_crtc(dev, possible_crtc) {
  8754. i++;
  8755. if (!(encoder->possible_crtcs & (1 << i)))
  8756. continue;
  8757. if (possible_crtc->state->enable)
  8758. continue;
  8759. /* This can occur when applying the pipe A quirk on resume. */
  8760. if (to_intel_crtc(possible_crtc)->new_enabled)
  8761. continue;
  8762. crtc = possible_crtc;
  8763. break;
  8764. }
  8765. /*
  8766. * If we didn't find an unused CRTC, don't use any.
  8767. */
  8768. if (!crtc) {
  8769. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8770. goto fail_unlock;
  8771. }
  8772. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8773. if (ret)
  8774. goto fail_unlock;
  8775. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8776. if (ret)
  8777. goto fail_unlock;
  8778. intel_encoder->new_crtc = to_intel_crtc(crtc);
  8779. to_intel_connector(connector)->new_encoder = intel_encoder;
  8780. intel_crtc = to_intel_crtc(crtc);
  8781. intel_crtc->new_enabled = true;
  8782. old->dpms_mode = connector->dpms;
  8783. old->load_detect_temp = true;
  8784. old->release_fb = NULL;
  8785. state = drm_atomic_state_alloc(dev);
  8786. if (!state)
  8787. return false;
  8788. state->acquire_ctx = ctx;
  8789. connector_state = drm_atomic_get_connector_state(state, connector);
  8790. if (IS_ERR(connector_state)) {
  8791. ret = PTR_ERR(connector_state);
  8792. goto fail;
  8793. }
  8794. connector_state->crtc = crtc;
  8795. connector_state->best_encoder = &intel_encoder->base;
  8796. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8797. if (IS_ERR(crtc_state)) {
  8798. ret = PTR_ERR(crtc_state);
  8799. goto fail;
  8800. }
  8801. crtc_state->base.active = crtc_state->base.enable = true;
  8802. if (!mode)
  8803. mode = &load_detect_mode;
  8804. /* We need a framebuffer large enough to accommodate all accesses
  8805. * that the plane may generate whilst we perform load detection.
  8806. * We can not rely on the fbcon either being present (we get called
  8807. * during its initialisation to detect all boot displays, or it may
  8808. * not even exist) or that it is large enough to satisfy the
  8809. * requested mode.
  8810. */
  8811. fb = mode_fits_in_fbdev(dev, mode);
  8812. if (fb == NULL) {
  8813. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8814. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8815. old->release_fb = fb;
  8816. } else
  8817. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8818. if (IS_ERR(fb)) {
  8819. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8820. goto fail;
  8821. }
  8822. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8823. if (ret)
  8824. goto fail;
  8825. drm_mode_copy(&crtc_state->base.mode, mode);
  8826. if (intel_set_mode(state)) {
  8827. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8828. if (old->release_fb)
  8829. old->release_fb->funcs->destroy(old->release_fb);
  8830. goto fail;
  8831. }
  8832. crtc->primary->crtc = crtc;
  8833. /* let the connector get through one full cycle before testing */
  8834. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8835. return true;
  8836. fail:
  8837. intel_crtc->new_enabled = crtc->state->enable;
  8838. fail_unlock:
  8839. drm_atomic_state_free(state);
  8840. state = NULL;
  8841. if (ret == -EDEADLK) {
  8842. drm_modeset_backoff(ctx);
  8843. goto retry;
  8844. }
  8845. return false;
  8846. }
  8847. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8848. struct intel_load_detect_pipe *old,
  8849. struct drm_modeset_acquire_ctx *ctx)
  8850. {
  8851. struct drm_device *dev = connector->dev;
  8852. struct intel_encoder *intel_encoder =
  8853. intel_attached_encoder(connector);
  8854. struct drm_encoder *encoder = &intel_encoder->base;
  8855. struct drm_crtc *crtc = encoder->crtc;
  8856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8857. struct drm_atomic_state *state;
  8858. struct drm_connector_state *connector_state;
  8859. struct intel_crtc_state *crtc_state;
  8860. int ret;
  8861. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8862. connector->base.id, connector->name,
  8863. encoder->base.id, encoder->name);
  8864. if (old->load_detect_temp) {
  8865. state = drm_atomic_state_alloc(dev);
  8866. if (!state)
  8867. goto fail;
  8868. state->acquire_ctx = ctx;
  8869. connector_state = drm_atomic_get_connector_state(state, connector);
  8870. if (IS_ERR(connector_state))
  8871. goto fail;
  8872. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8873. if (IS_ERR(crtc_state))
  8874. goto fail;
  8875. to_intel_connector(connector)->new_encoder = NULL;
  8876. intel_encoder->new_crtc = NULL;
  8877. intel_crtc->new_enabled = false;
  8878. connector_state->best_encoder = NULL;
  8879. connector_state->crtc = NULL;
  8880. crtc_state->base.enable = crtc_state->base.active = false;
  8881. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8882. 0, 0);
  8883. if (ret)
  8884. goto fail;
  8885. ret = intel_set_mode(state);
  8886. if (ret)
  8887. goto fail;
  8888. if (old->release_fb) {
  8889. drm_framebuffer_unregister_private(old->release_fb);
  8890. drm_framebuffer_unreference(old->release_fb);
  8891. }
  8892. return;
  8893. }
  8894. /* Switch crtc and encoder back off if necessary */
  8895. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8896. connector->funcs->dpms(connector, old->dpms_mode);
  8897. return;
  8898. fail:
  8899. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8900. drm_atomic_state_free(state);
  8901. }
  8902. static int i9xx_pll_refclk(struct drm_device *dev,
  8903. const struct intel_crtc_state *pipe_config)
  8904. {
  8905. struct drm_i915_private *dev_priv = dev->dev_private;
  8906. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8907. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8908. return dev_priv->vbt.lvds_ssc_freq;
  8909. else if (HAS_PCH_SPLIT(dev))
  8910. return 120000;
  8911. else if (!IS_GEN2(dev))
  8912. return 96000;
  8913. else
  8914. return 48000;
  8915. }
  8916. /* Returns the clock of the currently programmed mode of the given pipe. */
  8917. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8918. struct intel_crtc_state *pipe_config)
  8919. {
  8920. struct drm_device *dev = crtc->base.dev;
  8921. struct drm_i915_private *dev_priv = dev->dev_private;
  8922. int pipe = pipe_config->cpu_transcoder;
  8923. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8924. u32 fp;
  8925. intel_clock_t clock;
  8926. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8927. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8928. fp = pipe_config->dpll_hw_state.fp0;
  8929. else
  8930. fp = pipe_config->dpll_hw_state.fp1;
  8931. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8932. if (IS_PINEVIEW(dev)) {
  8933. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8934. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8935. } else {
  8936. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8937. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8938. }
  8939. if (!IS_GEN2(dev)) {
  8940. if (IS_PINEVIEW(dev))
  8941. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8942. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8943. else
  8944. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8945. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8946. switch (dpll & DPLL_MODE_MASK) {
  8947. case DPLLB_MODE_DAC_SERIAL:
  8948. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8949. 5 : 10;
  8950. break;
  8951. case DPLLB_MODE_LVDS:
  8952. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8953. 7 : 14;
  8954. break;
  8955. default:
  8956. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8957. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8958. return;
  8959. }
  8960. if (IS_PINEVIEW(dev))
  8961. pineview_clock(refclk, &clock);
  8962. else
  8963. i9xx_clock(refclk, &clock);
  8964. } else {
  8965. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8966. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8967. if (is_lvds) {
  8968. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8969. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8970. if (lvds & LVDS_CLKB_POWER_UP)
  8971. clock.p2 = 7;
  8972. else
  8973. clock.p2 = 14;
  8974. } else {
  8975. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8976. clock.p1 = 2;
  8977. else {
  8978. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8979. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8980. }
  8981. if (dpll & PLL_P2_DIVIDE_BY_4)
  8982. clock.p2 = 4;
  8983. else
  8984. clock.p2 = 2;
  8985. }
  8986. i9xx_clock(refclk, &clock);
  8987. }
  8988. /*
  8989. * This value includes pixel_multiplier. We will use
  8990. * port_clock to compute adjusted_mode.crtc_clock in the
  8991. * encoder's get_config() function.
  8992. */
  8993. pipe_config->port_clock = clock.dot;
  8994. }
  8995. int intel_dotclock_calculate(int link_freq,
  8996. const struct intel_link_m_n *m_n)
  8997. {
  8998. /*
  8999. * The calculation for the data clock is:
  9000. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9001. * But we want to avoid losing precison if possible, so:
  9002. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9003. *
  9004. * and the link clock is simpler:
  9005. * link_clock = (m * link_clock) / n
  9006. */
  9007. if (!m_n->link_n)
  9008. return 0;
  9009. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9010. }
  9011. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9012. struct intel_crtc_state *pipe_config)
  9013. {
  9014. struct drm_device *dev = crtc->base.dev;
  9015. /* read out port_clock from the DPLL */
  9016. i9xx_crtc_clock_get(crtc, pipe_config);
  9017. /*
  9018. * This value does not include pixel_multiplier.
  9019. * We will check that port_clock and adjusted_mode.crtc_clock
  9020. * agree once we know their relationship in the encoder's
  9021. * get_config() function.
  9022. */
  9023. pipe_config->base.adjusted_mode.crtc_clock =
  9024. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  9025. &pipe_config->fdi_m_n);
  9026. }
  9027. /** Returns the currently programmed mode of the given pipe. */
  9028. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9029. struct drm_crtc *crtc)
  9030. {
  9031. struct drm_i915_private *dev_priv = dev->dev_private;
  9032. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9033. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9034. struct drm_display_mode *mode;
  9035. struct intel_crtc_state pipe_config;
  9036. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9037. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9038. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9039. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9040. enum pipe pipe = intel_crtc->pipe;
  9041. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9042. if (!mode)
  9043. return NULL;
  9044. /*
  9045. * Construct a pipe_config sufficient for getting the clock info
  9046. * back out of crtc_clock_get.
  9047. *
  9048. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9049. * to use a real value here instead.
  9050. */
  9051. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  9052. pipe_config.pixel_multiplier = 1;
  9053. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9054. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9055. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9056. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  9057. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  9058. mode->hdisplay = (htot & 0xffff) + 1;
  9059. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9060. mode->hsync_start = (hsync & 0xffff) + 1;
  9061. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9062. mode->vdisplay = (vtot & 0xffff) + 1;
  9063. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9064. mode->vsync_start = (vsync & 0xffff) + 1;
  9065. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9066. drm_mode_set_name(mode);
  9067. return mode;
  9068. }
  9069. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  9070. {
  9071. struct drm_device *dev = crtc->dev;
  9072. struct drm_i915_private *dev_priv = dev->dev_private;
  9073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9074. if (!HAS_GMCH_DISPLAY(dev))
  9075. return;
  9076. if (!dev_priv->lvds_downclock_avail)
  9077. return;
  9078. /*
  9079. * Since this is called by a timer, we should never get here in
  9080. * the manual case.
  9081. */
  9082. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  9083. int pipe = intel_crtc->pipe;
  9084. int dpll_reg = DPLL(pipe);
  9085. int dpll;
  9086. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  9087. assert_panel_unlocked(dev_priv, pipe);
  9088. dpll = I915_READ(dpll_reg);
  9089. dpll |= DISPLAY_RATE_SELECT_FPA1;
  9090. I915_WRITE(dpll_reg, dpll);
  9091. intel_wait_for_vblank(dev, pipe);
  9092. dpll = I915_READ(dpll_reg);
  9093. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  9094. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  9095. }
  9096. }
  9097. void intel_mark_busy(struct drm_device *dev)
  9098. {
  9099. struct drm_i915_private *dev_priv = dev->dev_private;
  9100. if (dev_priv->mm.busy)
  9101. return;
  9102. intel_runtime_pm_get(dev_priv);
  9103. i915_update_gfx_val(dev_priv);
  9104. if (INTEL_INFO(dev)->gen >= 6)
  9105. gen6_rps_busy(dev_priv);
  9106. dev_priv->mm.busy = true;
  9107. }
  9108. void intel_mark_idle(struct drm_device *dev)
  9109. {
  9110. struct drm_i915_private *dev_priv = dev->dev_private;
  9111. struct drm_crtc *crtc;
  9112. if (!dev_priv->mm.busy)
  9113. return;
  9114. dev_priv->mm.busy = false;
  9115. for_each_crtc(dev, crtc) {
  9116. if (!crtc->primary->fb)
  9117. continue;
  9118. intel_decrease_pllclock(crtc);
  9119. }
  9120. if (INTEL_INFO(dev)->gen >= 6)
  9121. gen6_rps_idle(dev->dev_private);
  9122. intel_runtime_pm_put(dev_priv);
  9123. }
  9124. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9125. {
  9126. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9127. struct drm_device *dev = crtc->dev;
  9128. struct intel_unpin_work *work;
  9129. spin_lock_irq(&dev->event_lock);
  9130. work = intel_crtc->unpin_work;
  9131. intel_crtc->unpin_work = NULL;
  9132. spin_unlock_irq(&dev->event_lock);
  9133. if (work) {
  9134. cancel_work_sync(&work->work);
  9135. kfree(work);
  9136. }
  9137. drm_crtc_cleanup(crtc);
  9138. kfree(intel_crtc);
  9139. }
  9140. static void intel_unpin_work_fn(struct work_struct *__work)
  9141. {
  9142. struct intel_unpin_work *work =
  9143. container_of(__work, struct intel_unpin_work, work);
  9144. struct drm_device *dev = work->crtc->dev;
  9145. enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
  9146. mutex_lock(&dev->struct_mutex);
  9147. intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
  9148. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9149. intel_fbc_update(dev);
  9150. if (work->flip_queued_req)
  9151. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9152. mutex_unlock(&dev->struct_mutex);
  9153. intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9154. drm_framebuffer_unreference(work->old_fb);
  9155. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  9156. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  9157. kfree(work);
  9158. }
  9159. static void do_intel_finish_page_flip(struct drm_device *dev,
  9160. struct drm_crtc *crtc)
  9161. {
  9162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9163. struct intel_unpin_work *work;
  9164. unsigned long flags;
  9165. /* Ignore early vblank irqs */
  9166. if (intel_crtc == NULL)
  9167. return;
  9168. /*
  9169. * This is called both by irq handlers and the reset code (to complete
  9170. * lost pageflips) so needs the full irqsave spinlocks.
  9171. */
  9172. spin_lock_irqsave(&dev->event_lock, flags);
  9173. work = intel_crtc->unpin_work;
  9174. /* Ensure we don't miss a work->pending update ... */
  9175. smp_rmb();
  9176. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9177. spin_unlock_irqrestore(&dev->event_lock, flags);
  9178. return;
  9179. }
  9180. page_flip_completed(intel_crtc);
  9181. spin_unlock_irqrestore(&dev->event_lock, flags);
  9182. }
  9183. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9184. {
  9185. struct drm_i915_private *dev_priv = dev->dev_private;
  9186. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9187. do_intel_finish_page_flip(dev, crtc);
  9188. }
  9189. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9190. {
  9191. struct drm_i915_private *dev_priv = dev->dev_private;
  9192. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9193. do_intel_finish_page_flip(dev, crtc);
  9194. }
  9195. /* Is 'a' after or equal to 'b'? */
  9196. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9197. {
  9198. return !((a - b) & 0x80000000);
  9199. }
  9200. static bool page_flip_finished(struct intel_crtc *crtc)
  9201. {
  9202. struct drm_device *dev = crtc->base.dev;
  9203. struct drm_i915_private *dev_priv = dev->dev_private;
  9204. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9205. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9206. return true;
  9207. /*
  9208. * The relevant registers doen't exist on pre-ctg.
  9209. * As the flip done interrupt doesn't trigger for mmio
  9210. * flips on gmch platforms, a flip count check isn't
  9211. * really needed there. But since ctg has the registers,
  9212. * include it in the check anyway.
  9213. */
  9214. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9215. return true;
  9216. /*
  9217. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9218. * used the same base address. In that case the mmio flip might
  9219. * have completed, but the CS hasn't even executed the flip yet.
  9220. *
  9221. * A flip count check isn't enough as the CS might have updated
  9222. * the base address just after start of vblank, but before we
  9223. * managed to process the interrupt. This means we'd complete the
  9224. * CS flip too soon.
  9225. *
  9226. * Combining both checks should get us a good enough result. It may
  9227. * still happen that the CS flip has been executed, but has not
  9228. * yet actually completed. But in case the base address is the same
  9229. * anyway, we don't really care.
  9230. */
  9231. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9232. crtc->unpin_work->gtt_offset &&
  9233. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  9234. crtc->unpin_work->flip_count);
  9235. }
  9236. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9237. {
  9238. struct drm_i915_private *dev_priv = dev->dev_private;
  9239. struct intel_crtc *intel_crtc =
  9240. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9241. unsigned long flags;
  9242. /*
  9243. * This is called both by irq handlers and the reset code (to complete
  9244. * lost pageflips) so needs the full irqsave spinlocks.
  9245. *
  9246. * NB: An MMIO update of the plane base pointer will also
  9247. * generate a page-flip completion irq, i.e. every modeset
  9248. * is also accompanied by a spurious intel_prepare_page_flip().
  9249. */
  9250. spin_lock_irqsave(&dev->event_lock, flags);
  9251. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9252. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9253. spin_unlock_irqrestore(&dev->event_lock, flags);
  9254. }
  9255. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  9256. {
  9257. /* Ensure that the work item is consistent when activating it ... */
  9258. smp_wmb();
  9259. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  9260. /* and that it is marked active as soon as the irq could fire. */
  9261. smp_wmb();
  9262. }
  9263. static int intel_gen2_queue_flip(struct drm_device *dev,
  9264. struct drm_crtc *crtc,
  9265. struct drm_framebuffer *fb,
  9266. struct drm_i915_gem_object *obj,
  9267. struct intel_engine_cs *ring,
  9268. uint32_t flags)
  9269. {
  9270. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9271. u32 flip_mask;
  9272. int ret;
  9273. ret = intel_ring_begin(ring, 6);
  9274. if (ret)
  9275. return ret;
  9276. /* Can't queue multiple flips, so wait for the previous
  9277. * one to finish before executing the next.
  9278. */
  9279. if (intel_crtc->plane)
  9280. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9281. else
  9282. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9283. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9284. intel_ring_emit(ring, MI_NOOP);
  9285. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9286. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9287. intel_ring_emit(ring, fb->pitches[0]);
  9288. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9289. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9290. intel_mark_page_flip_active(intel_crtc);
  9291. __intel_ring_advance(ring);
  9292. return 0;
  9293. }
  9294. static int intel_gen3_queue_flip(struct drm_device *dev,
  9295. struct drm_crtc *crtc,
  9296. struct drm_framebuffer *fb,
  9297. struct drm_i915_gem_object *obj,
  9298. struct intel_engine_cs *ring,
  9299. uint32_t flags)
  9300. {
  9301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9302. u32 flip_mask;
  9303. int ret;
  9304. ret = intel_ring_begin(ring, 6);
  9305. if (ret)
  9306. return ret;
  9307. if (intel_crtc->plane)
  9308. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9309. else
  9310. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9311. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9312. intel_ring_emit(ring, MI_NOOP);
  9313. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9314. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9315. intel_ring_emit(ring, fb->pitches[0]);
  9316. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9317. intel_ring_emit(ring, MI_NOOP);
  9318. intel_mark_page_flip_active(intel_crtc);
  9319. __intel_ring_advance(ring);
  9320. return 0;
  9321. }
  9322. static int intel_gen4_queue_flip(struct drm_device *dev,
  9323. struct drm_crtc *crtc,
  9324. struct drm_framebuffer *fb,
  9325. struct drm_i915_gem_object *obj,
  9326. struct intel_engine_cs *ring,
  9327. uint32_t flags)
  9328. {
  9329. struct drm_i915_private *dev_priv = dev->dev_private;
  9330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9331. uint32_t pf, pipesrc;
  9332. int ret;
  9333. ret = intel_ring_begin(ring, 4);
  9334. if (ret)
  9335. return ret;
  9336. /* i965+ uses the linear or tiled offsets from the
  9337. * Display Registers (which do not change across a page-flip)
  9338. * so we need only reprogram the base address.
  9339. */
  9340. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9341. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9342. intel_ring_emit(ring, fb->pitches[0]);
  9343. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9344. obj->tiling_mode);
  9345. /* XXX Enabling the panel-fitter across page-flip is so far
  9346. * untested on non-native modes, so ignore it for now.
  9347. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9348. */
  9349. pf = 0;
  9350. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9351. intel_ring_emit(ring, pf | pipesrc);
  9352. intel_mark_page_flip_active(intel_crtc);
  9353. __intel_ring_advance(ring);
  9354. return 0;
  9355. }
  9356. static int intel_gen6_queue_flip(struct drm_device *dev,
  9357. struct drm_crtc *crtc,
  9358. struct drm_framebuffer *fb,
  9359. struct drm_i915_gem_object *obj,
  9360. struct intel_engine_cs *ring,
  9361. uint32_t flags)
  9362. {
  9363. struct drm_i915_private *dev_priv = dev->dev_private;
  9364. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9365. uint32_t pf, pipesrc;
  9366. int ret;
  9367. ret = intel_ring_begin(ring, 4);
  9368. if (ret)
  9369. return ret;
  9370. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9371. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9372. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9373. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9374. /* Contrary to the suggestions in the documentation,
  9375. * "Enable Panel Fitter" does not seem to be required when page
  9376. * flipping with a non-native mode, and worse causes a normal
  9377. * modeset to fail.
  9378. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9379. */
  9380. pf = 0;
  9381. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9382. intel_ring_emit(ring, pf | pipesrc);
  9383. intel_mark_page_flip_active(intel_crtc);
  9384. __intel_ring_advance(ring);
  9385. return 0;
  9386. }
  9387. static int intel_gen7_queue_flip(struct drm_device *dev,
  9388. struct drm_crtc *crtc,
  9389. struct drm_framebuffer *fb,
  9390. struct drm_i915_gem_object *obj,
  9391. struct intel_engine_cs *ring,
  9392. uint32_t flags)
  9393. {
  9394. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9395. uint32_t plane_bit = 0;
  9396. int len, ret;
  9397. switch (intel_crtc->plane) {
  9398. case PLANE_A:
  9399. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9400. break;
  9401. case PLANE_B:
  9402. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9403. break;
  9404. case PLANE_C:
  9405. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9406. break;
  9407. default:
  9408. WARN_ONCE(1, "unknown plane in flip command\n");
  9409. return -ENODEV;
  9410. }
  9411. len = 4;
  9412. if (ring->id == RCS) {
  9413. len += 6;
  9414. /*
  9415. * On Gen 8, SRM is now taking an extra dword to accommodate
  9416. * 48bits addresses, and we need a NOOP for the batch size to
  9417. * stay even.
  9418. */
  9419. if (IS_GEN8(dev))
  9420. len += 2;
  9421. }
  9422. /*
  9423. * BSpec MI_DISPLAY_FLIP for IVB:
  9424. * "The full packet must be contained within the same cache line."
  9425. *
  9426. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9427. * cacheline, if we ever start emitting more commands before
  9428. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9429. * then do the cacheline alignment, and finally emit the
  9430. * MI_DISPLAY_FLIP.
  9431. */
  9432. ret = intel_ring_cacheline_align(ring);
  9433. if (ret)
  9434. return ret;
  9435. ret = intel_ring_begin(ring, len);
  9436. if (ret)
  9437. return ret;
  9438. /* Unmask the flip-done completion message. Note that the bspec says that
  9439. * we should do this for both the BCS and RCS, and that we must not unmask
  9440. * more than one flip event at any time (or ensure that one flip message
  9441. * can be sent by waiting for flip-done prior to queueing new flips).
  9442. * Experimentation says that BCS works despite DERRMR masking all
  9443. * flip-done completion events and that unmasking all planes at once
  9444. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9445. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9446. */
  9447. if (ring->id == RCS) {
  9448. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9449. intel_ring_emit(ring, DERRMR);
  9450. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9451. DERRMR_PIPEB_PRI_FLIP_DONE |
  9452. DERRMR_PIPEC_PRI_FLIP_DONE));
  9453. if (IS_GEN8(dev))
  9454. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  9455. MI_SRM_LRM_GLOBAL_GTT);
  9456. else
  9457. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  9458. MI_SRM_LRM_GLOBAL_GTT);
  9459. intel_ring_emit(ring, DERRMR);
  9460. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9461. if (IS_GEN8(dev)) {
  9462. intel_ring_emit(ring, 0);
  9463. intel_ring_emit(ring, MI_NOOP);
  9464. }
  9465. }
  9466. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9467. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9468. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9469. intel_ring_emit(ring, (MI_NOOP));
  9470. intel_mark_page_flip_active(intel_crtc);
  9471. __intel_ring_advance(ring);
  9472. return 0;
  9473. }
  9474. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9475. struct drm_i915_gem_object *obj)
  9476. {
  9477. /*
  9478. * This is not being used for older platforms, because
  9479. * non-availability of flip done interrupt forces us to use
  9480. * CS flips. Older platforms derive flip done using some clever
  9481. * tricks involving the flip_pending status bits and vblank irqs.
  9482. * So using MMIO flips there would disrupt this mechanism.
  9483. */
  9484. if (ring == NULL)
  9485. return true;
  9486. if (INTEL_INFO(ring->dev)->gen < 5)
  9487. return false;
  9488. if (i915.use_mmio_flip < 0)
  9489. return false;
  9490. else if (i915.use_mmio_flip > 0)
  9491. return true;
  9492. else if (i915.enable_execlists)
  9493. return true;
  9494. else
  9495. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9496. }
  9497. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
  9498. {
  9499. struct drm_device *dev = intel_crtc->base.dev;
  9500. struct drm_i915_private *dev_priv = dev->dev_private;
  9501. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9502. const enum pipe pipe = intel_crtc->pipe;
  9503. u32 ctl, stride;
  9504. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9505. ctl &= ~PLANE_CTL_TILED_MASK;
  9506. switch (fb->modifier[0]) {
  9507. case DRM_FORMAT_MOD_NONE:
  9508. break;
  9509. case I915_FORMAT_MOD_X_TILED:
  9510. ctl |= PLANE_CTL_TILED_X;
  9511. break;
  9512. case I915_FORMAT_MOD_Y_TILED:
  9513. ctl |= PLANE_CTL_TILED_Y;
  9514. break;
  9515. case I915_FORMAT_MOD_Yf_TILED:
  9516. ctl |= PLANE_CTL_TILED_YF;
  9517. break;
  9518. default:
  9519. MISSING_CASE(fb->modifier[0]);
  9520. }
  9521. /*
  9522. * The stride is either expressed as a multiple of 64 bytes chunks for
  9523. * linear buffers or in number of tiles for tiled buffers.
  9524. */
  9525. stride = fb->pitches[0] /
  9526. intel_fb_stride_alignment(dev, fb->modifier[0],
  9527. fb->pixel_format);
  9528. /*
  9529. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9530. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9531. */
  9532. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9533. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9534. I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
  9535. POSTING_READ(PLANE_SURF(pipe, 0));
  9536. }
  9537. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
  9538. {
  9539. struct drm_device *dev = intel_crtc->base.dev;
  9540. struct drm_i915_private *dev_priv = dev->dev_private;
  9541. struct intel_framebuffer *intel_fb =
  9542. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9543. struct drm_i915_gem_object *obj = intel_fb->obj;
  9544. u32 dspcntr;
  9545. u32 reg;
  9546. reg = DSPCNTR(intel_crtc->plane);
  9547. dspcntr = I915_READ(reg);
  9548. if (obj->tiling_mode != I915_TILING_NONE)
  9549. dspcntr |= DISPPLANE_TILED;
  9550. else
  9551. dspcntr &= ~DISPPLANE_TILED;
  9552. I915_WRITE(reg, dspcntr);
  9553. I915_WRITE(DSPSURF(intel_crtc->plane),
  9554. intel_crtc->unpin_work->gtt_offset);
  9555. POSTING_READ(DSPSURF(intel_crtc->plane));
  9556. }
  9557. /*
  9558. * XXX: This is the temporary way to update the plane registers until we get
  9559. * around to using the usual plane update functions for MMIO flips
  9560. */
  9561. static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
  9562. {
  9563. struct drm_device *dev = intel_crtc->base.dev;
  9564. bool atomic_update;
  9565. u32 start_vbl_count;
  9566. intel_mark_page_flip_active(intel_crtc);
  9567. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  9568. if (INTEL_INFO(dev)->gen >= 9)
  9569. skl_do_mmio_flip(intel_crtc);
  9570. else
  9571. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9572. ilk_do_mmio_flip(intel_crtc);
  9573. if (atomic_update)
  9574. intel_pipe_update_end(intel_crtc, start_vbl_count);
  9575. }
  9576. static void intel_mmio_flip_work_func(struct work_struct *work)
  9577. {
  9578. struct intel_mmio_flip *mmio_flip =
  9579. container_of(work, struct intel_mmio_flip, work);
  9580. if (mmio_flip->req)
  9581. WARN_ON(__i915_wait_request(mmio_flip->req,
  9582. mmio_flip->crtc->reset_counter,
  9583. false, NULL,
  9584. &mmio_flip->i915->rps.mmioflips));
  9585. intel_do_mmio_flip(mmio_flip->crtc);
  9586. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9587. kfree(mmio_flip);
  9588. }
  9589. static int intel_queue_mmio_flip(struct drm_device *dev,
  9590. struct drm_crtc *crtc,
  9591. struct drm_framebuffer *fb,
  9592. struct drm_i915_gem_object *obj,
  9593. struct intel_engine_cs *ring,
  9594. uint32_t flags)
  9595. {
  9596. struct intel_mmio_flip *mmio_flip;
  9597. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9598. if (mmio_flip == NULL)
  9599. return -ENOMEM;
  9600. mmio_flip->i915 = to_i915(dev);
  9601. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9602. mmio_flip->crtc = to_intel_crtc(crtc);
  9603. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9604. schedule_work(&mmio_flip->work);
  9605. return 0;
  9606. }
  9607. static int intel_default_queue_flip(struct drm_device *dev,
  9608. struct drm_crtc *crtc,
  9609. struct drm_framebuffer *fb,
  9610. struct drm_i915_gem_object *obj,
  9611. struct intel_engine_cs *ring,
  9612. uint32_t flags)
  9613. {
  9614. return -ENODEV;
  9615. }
  9616. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9617. struct drm_crtc *crtc)
  9618. {
  9619. struct drm_i915_private *dev_priv = dev->dev_private;
  9620. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9621. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9622. u32 addr;
  9623. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9624. return true;
  9625. if (!work->enable_stall_check)
  9626. return false;
  9627. if (work->flip_ready_vblank == 0) {
  9628. if (work->flip_queued_req &&
  9629. !i915_gem_request_completed(work->flip_queued_req, true))
  9630. return false;
  9631. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9632. }
  9633. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9634. return false;
  9635. /* Potential stall - if we see that the flip has happened,
  9636. * assume a missed interrupt. */
  9637. if (INTEL_INFO(dev)->gen >= 4)
  9638. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9639. else
  9640. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9641. /* There is a potential issue here with a false positive after a flip
  9642. * to the same address. We could address this by checking for a
  9643. * non-incrementing frame counter.
  9644. */
  9645. return addr == work->gtt_offset;
  9646. }
  9647. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9648. {
  9649. struct drm_i915_private *dev_priv = dev->dev_private;
  9650. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9651. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9652. struct intel_unpin_work *work;
  9653. WARN_ON(!in_interrupt());
  9654. if (crtc == NULL)
  9655. return;
  9656. spin_lock(&dev->event_lock);
  9657. work = intel_crtc->unpin_work;
  9658. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9659. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9660. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9661. page_flip_completed(intel_crtc);
  9662. work = NULL;
  9663. }
  9664. if (work != NULL &&
  9665. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9666. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9667. spin_unlock(&dev->event_lock);
  9668. }
  9669. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9670. struct drm_framebuffer *fb,
  9671. struct drm_pending_vblank_event *event,
  9672. uint32_t page_flip_flags)
  9673. {
  9674. struct drm_device *dev = crtc->dev;
  9675. struct drm_i915_private *dev_priv = dev->dev_private;
  9676. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9677. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9678. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9679. struct drm_plane *primary = crtc->primary;
  9680. enum pipe pipe = intel_crtc->pipe;
  9681. struct intel_unpin_work *work;
  9682. struct intel_engine_cs *ring;
  9683. bool mmio_flip;
  9684. int ret;
  9685. /*
  9686. * drm_mode_page_flip_ioctl() should already catch this, but double
  9687. * check to be safe. In the future we may enable pageflipping from
  9688. * a disabled primary plane.
  9689. */
  9690. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9691. return -EBUSY;
  9692. /* Can't change pixel format via MI display flips. */
  9693. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9694. return -EINVAL;
  9695. /*
  9696. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9697. * Note that pitch changes could also affect these register.
  9698. */
  9699. if (INTEL_INFO(dev)->gen > 3 &&
  9700. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9701. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9702. return -EINVAL;
  9703. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9704. goto out_hang;
  9705. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9706. if (work == NULL)
  9707. return -ENOMEM;
  9708. work->event = event;
  9709. work->crtc = crtc;
  9710. work->old_fb = old_fb;
  9711. INIT_WORK(&work->work, intel_unpin_work_fn);
  9712. ret = drm_crtc_vblank_get(crtc);
  9713. if (ret)
  9714. goto free_work;
  9715. /* We borrow the event spin lock for protecting unpin_work */
  9716. spin_lock_irq(&dev->event_lock);
  9717. if (intel_crtc->unpin_work) {
  9718. /* Before declaring the flip queue wedged, check if
  9719. * the hardware completed the operation behind our backs.
  9720. */
  9721. if (__intel_pageflip_stall_check(dev, crtc)) {
  9722. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9723. page_flip_completed(intel_crtc);
  9724. } else {
  9725. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9726. spin_unlock_irq(&dev->event_lock);
  9727. drm_crtc_vblank_put(crtc);
  9728. kfree(work);
  9729. return -EBUSY;
  9730. }
  9731. }
  9732. intel_crtc->unpin_work = work;
  9733. spin_unlock_irq(&dev->event_lock);
  9734. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9735. flush_workqueue(dev_priv->wq);
  9736. /* Reference the objects for the scheduled work. */
  9737. drm_framebuffer_reference(work->old_fb);
  9738. drm_gem_object_reference(&obj->base);
  9739. crtc->primary->fb = fb;
  9740. update_state_fb(crtc->primary);
  9741. work->pending_flip_obj = obj;
  9742. ret = i915_mutex_lock_interruptible(dev);
  9743. if (ret)
  9744. goto cleanup;
  9745. atomic_inc(&intel_crtc->unpin_work_count);
  9746. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9747. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9748. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
  9749. if (IS_VALLEYVIEW(dev)) {
  9750. ring = &dev_priv->ring[BCS];
  9751. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9752. /* vlv: DISPLAY_FLIP fails to change tiling */
  9753. ring = NULL;
  9754. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9755. ring = &dev_priv->ring[BCS];
  9756. } else if (INTEL_INFO(dev)->gen >= 7) {
  9757. ring = i915_gem_request_get_ring(obj->last_write_req);
  9758. if (ring == NULL || ring->id != RCS)
  9759. ring = &dev_priv->ring[BCS];
  9760. } else {
  9761. ring = &dev_priv->ring[RCS];
  9762. }
  9763. mmio_flip = use_mmio_flip(ring, obj);
  9764. /* When using CS flips, we want to emit semaphores between rings.
  9765. * However, when using mmio flips we will create a task to do the
  9766. * synchronisation, so all we want here is to pin the framebuffer
  9767. * into the display plane and skip any waits.
  9768. */
  9769. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9770. crtc->primary->state,
  9771. mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
  9772. if (ret)
  9773. goto cleanup_pending;
  9774. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
  9775. + intel_crtc->dspaddr_offset;
  9776. if (mmio_flip) {
  9777. ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
  9778. page_flip_flags);
  9779. if (ret)
  9780. goto cleanup_unpin;
  9781. i915_gem_request_assign(&work->flip_queued_req,
  9782. obj->last_write_req);
  9783. } else {
  9784. if (obj->last_write_req) {
  9785. ret = i915_gem_check_olr(obj->last_write_req);
  9786. if (ret)
  9787. goto cleanup_unpin;
  9788. }
  9789. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
  9790. page_flip_flags);
  9791. if (ret)
  9792. goto cleanup_unpin;
  9793. i915_gem_request_assign(&work->flip_queued_req,
  9794. intel_ring_get_request(ring));
  9795. }
  9796. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9797. work->enable_stall_check = true;
  9798. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9799. INTEL_FRONTBUFFER_PRIMARY(pipe));
  9800. intel_fbc_disable(dev);
  9801. intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
  9802. mutex_unlock(&dev->struct_mutex);
  9803. trace_i915_flip_request(intel_crtc->plane, obj);
  9804. return 0;
  9805. cleanup_unpin:
  9806. intel_unpin_fb_obj(fb, crtc->primary->state);
  9807. cleanup_pending:
  9808. atomic_dec(&intel_crtc->unpin_work_count);
  9809. mutex_unlock(&dev->struct_mutex);
  9810. cleanup:
  9811. crtc->primary->fb = old_fb;
  9812. update_state_fb(crtc->primary);
  9813. drm_gem_object_unreference_unlocked(&obj->base);
  9814. drm_framebuffer_unreference(work->old_fb);
  9815. spin_lock_irq(&dev->event_lock);
  9816. intel_crtc->unpin_work = NULL;
  9817. spin_unlock_irq(&dev->event_lock);
  9818. drm_crtc_vblank_put(crtc);
  9819. free_work:
  9820. kfree(work);
  9821. if (ret == -EIO) {
  9822. struct drm_atomic_state *state;
  9823. struct drm_plane_state *plane_state;
  9824. out_hang:
  9825. state = drm_atomic_state_alloc(dev);
  9826. if (!state)
  9827. return -ENOMEM;
  9828. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9829. retry:
  9830. plane_state = drm_atomic_get_plane_state(state, primary);
  9831. ret = PTR_ERR_OR_ZERO(plane_state);
  9832. if (!ret) {
  9833. drm_atomic_set_fb_for_plane(plane_state, fb);
  9834. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9835. if (!ret)
  9836. ret = drm_atomic_commit(state);
  9837. }
  9838. if (ret == -EDEADLK) {
  9839. drm_modeset_backoff(state->acquire_ctx);
  9840. drm_atomic_state_clear(state);
  9841. goto retry;
  9842. }
  9843. if (ret)
  9844. drm_atomic_state_free(state);
  9845. if (ret == 0 && event) {
  9846. spin_lock_irq(&dev->event_lock);
  9847. drm_send_vblank_event(dev, pipe, event);
  9848. spin_unlock_irq(&dev->event_lock);
  9849. }
  9850. }
  9851. return ret;
  9852. }
  9853. /**
  9854. * intel_wm_need_update - Check whether watermarks need updating
  9855. * @plane: drm plane
  9856. * @state: new plane state
  9857. *
  9858. * Check current plane state versus the new one to determine whether
  9859. * watermarks need to be recalculated.
  9860. *
  9861. * Returns true or false.
  9862. */
  9863. static bool intel_wm_need_update(struct drm_plane *plane,
  9864. struct drm_plane_state *state)
  9865. {
  9866. /* Update watermarks on tiling changes. */
  9867. if (!plane->state->fb || !state->fb ||
  9868. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9869. plane->state->rotation != state->rotation)
  9870. return true;
  9871. if (plane->state->crtc_w != state->crtc_w)
  9872. return true;
  9873. return false;
  9874. }
  9875. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9876. struct drm_plane_state *plane_state)
  9877. {
  9878. struct drm_crtc *crtc = crtc_state->crtc;
  9879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9880. struct drm_plane *plane = plane_state->plane;
  9881. struct drm_device *dev = crtc->dev;
  9882. struct drm_i915_private *dev_priv = dev->dev_private;
  9883. struct intel_plane_state *old_plane_state =
  9884. to_intel_plane_state(plane->state);
  9885. int idx = intel_crtc->base.base.id, ret;
  9886. int i = drm_plane_index(plane);
  9887. bool mode_changed = needs_modeset(crtc_state);
  9888. bool was_crtc_enabled = crtc->state->active;
  9889. bool is_crtc_enabled = crtc_state->active;
  9890. bool turn_off, turn_on, visible, was_visible;
  9891. struct drm_framebuffer *fb = plane_state->fb;
  9892. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9893. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9894. ret = skl_update_scaler_plane(
  9895. to_intel_crtc_state(crtc_state),
  9896. to_intel_plane_state(plane_state));
  9897. if (ret)
  9898. return ret;
  9899. }
  9900. /*
  9901. * Disabling a plane is always okay; we just need to update
  9902. * fb tracking in a special way since cleanup_fb() won't
  9903. * get called by the plane helpers.
  9904. */
  9905. if (old_plane_state->base.fb && !fb)
  9906. intel_crtc->atomic.disabled_planes |= 1 << i;
  9907. /* don't run rest during modeset yet */
  9908. if (!intel_crtc->active || mode_changed)
  9909. return 0;
  9910. was_visible = old_plane_state->visible;
  9911. visible = to_intel_plane_state(plane_state)->visible;
  9912. if (!was_crtc_enabled && WARN_ON(was_visible))
  9913. was_visible = false;
  9914. if (!is_crtc_enabled && WARN_ON(visible))
  9915. visible = false;
  9916. if (!was_visible && !visible)
  9917. return 0;
  9918. turn_off = was_visible && (!visible || mode_changed);
  9919. turn_on = visible && (!was_visible || mode_changed);
  9920. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9921. plane->base.id, fb ? fb->base.id : -1);
  9922. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9923. plane->base.id, was_visible, visible,
  9924. turn_off, turn_on, mode_changed);
  9925. if (intel_wm_need_update(plane, plane_state))
  9926. intel_crtc->atomic.update_wm = true;
  9927. switch (plane->type) {
  9928. case DRM_PLANE_TYPE_PRIMARY:
  9929. if (visible)
  9930. intel_crtc->atomic.fb_bits |=
  9931. INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
  9932. intel_crtc->atomic.wait_for_flips = true;
  9933. intel_crtc->atomic.pre_disable_primary = turn_off;
  9934. intel_crtc->atomic.post_enable_primary = turn_on;
  9935. if (turn_off)
  9936. intel_crtc->atomic.disable_fbc = true;
  9937. /*
  9938. * FBC does not work on some platforms for rotated
  9939. * planes, so disable it when rotation is not 0 and
  9940. * update it when rotation is set back to 0.
  9941. *
  9942. * FIXME: This is redundant with the fbc update done in
  9943. * the primary plane enable function except that that
  9944. * one is done too late. We eventually need to unify
  9945. * this.
  9946. */
  9947. if (visible &&
  9948. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9949. dev_priv->fbc.crtc == intel_crtc &&
  9950. plane_state->rotation != BIT(DRM_ROTATE_0))
  9951. intel_crtc->atomic.disable_fbc = true;
  9952. /*
  9953. * BDW signals flip done immediately if the plane
  9954. * is disabled, even if the plane enable is already
  9955. * armed to occur at the next vblank :(
  9956. */
  9957. if (turn_on && IS_BROADWELL(dev))
  9958. intel_crtc->atomic.wait_vblank = true;
  9959. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9960. break;
  9961. case DRM_PLANE_TYPE_CURSOR:
  9962. if (visible)
  9963. intel_crtc->atomic.fb_bits |=
  9964. INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
  9965. break;
  9966. case DRM_PLANE_TYPE_OVERLAY:
  9967. /*
  9968. * 'prepare' is never called when plane is being disabled, so
  9969. * we need to handle frontbuffer tracking as a special case
  9970. */
  9971. if (visible)
  9972. intel_crtc->atomic.fb_bits |=
  9973. INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
  9974. if (turn_off && is_crtc_enabled) {
  9975. intel_crtc->atomic.wait_vblank = true;
  9976. intel_crtc->atomic.update_sprite_watermarks |=
  9977. 1 << i;
  9978. }
  9979. break;
  9980. }
  9981. return 0;
  9982. }
  9983. static bool encoders_cloneable(const struct intel_encoder *a,
  9984. const struct intel_encoder *b)
  9985. {
  9986. /* masks could be asymmetric, so check both ways */
  9987. return a == b || (a->cloneable & (1 << b->type) &&
  9988. b->cloneable & (1 << a->type));
  9989. }
  9990. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9991. struct intel_crtc *crtc,
  9992. struct intel_encoder *encoder)
  9993. {
  9994. struct intel_encoder *source_encoder;
  9995. struct drm_connector *connector;
  9996. struct drm_connector_state *connector_state;
  9997. int i;
  9998. for_each_connector_in_state(state, connector, connector_state, i) {
  9999. if (connector_state->crtc != &crtc->base)
  10000. continue;
  10001. source_encoder =
  10002. to_intel_encoder(connector_state->best_encoder);
  10003. if (!encoders_cloneable(encoder, source_encoder))
  10004. return false;
  10005. }
  10006. return true;
  10007. }
  10008. static bool check_encoder_cloning(struct drm_atomic_state *state,
  10009. struct intel_crtc *crtc)
  10010. {
  10011. struct intel_encoder *encoder;
  10012. struct drm_connector *connector;
  10013. struct drm_connector_state *connector_state;
  10014. int i;
  10015. for_each_connector_in_state(state, connector, connector_state, i) {
  10016. if (connector_state->crtc != &crtc->base)
  10017. continue;
  10018. encoder = to_intel_encoder(connector_state->best_encoder);
  10019. if (!check_single_encoder_cloning(state, crtc, encoder))
  10020. return false;
  10021. }
  10022. return true;
  10023. }
  10024. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10025. struct drm_crtc_state *crtc_state)
  10026. {
  10027. struct drm_device *dev = crtc->dev;
  10028. struct drm_i915_private *dev_priv = dev->dev_private;
  10029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10030. struct intel_crtc_state *pipe_config =
  10031. to_intel_crtc_state(crtc_state);
  10032. struct drm_atomic_state *state = crtc_state->state;
  10033. int ret, idx = crtc->base.id;
  10034. bool mode_changed = needs_modeset(crtc_state);
  10035. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  10036. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10037. return -EINVAL;
  10038. }
  10039. I915_STATE_WARN(crtc->state->active != intel_crtc->active,
  10040. "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
  10041. idx, crtc->state->active, intel_crtc->active);
  10042. if (mode_changed && crtc_state->enable &&
  10043. dev_priv->display.crtc_compute_clock &&
  10044. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  10045. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10046. pipe_config);
  10047. if (ret)
  10048. return ret;
  10049. }
  10050. return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
  10051. }
  10052. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10053. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10054. .load_lut = intel_crtc_load_lut,
  10055. .atomic_begin = intel_begin_crtc_commit,
  10056. .atomic_flush = intel_finish_crtc_commit,
  10057. .atomic_check = intel_crtc_atomic_check,
  10058. };
  10059. /**
  10060. * intel_modeset_update_staged_output_state
  10061. *
  10062. * Updates the staged output configuration state, e.g. after we've read out the
  10063. * current hw state.
  10064. */
  10065. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  10066. {
  10067. struct intel_crtc *crtc;
  10068. struct intel_encoder *encoder;
  10069. struct intel_connector *connector;
  10070. for_each_intel_connector(dev, connector) {
  10071. connector->new_encoder =
  10072. to_intel_encoder(connector->base.encoder);
  10073. }
  10074. for_each_intel_encoder(dev, encoder) {
  10075. encoder->new_crtc =
  10076. to_intel_crtc(encoder->base.crtc);
  10077. }
  10078. for_each_intel_crtc(dev, crtc) {
  10079. crtc->new_enabled = crtc->base.state->enable;
  10080. }
  10081. }
  10082. /* Transitional helper to copy current connector/encoder state to
  10083. * connector->state. This is needed so that code that is partially
  10084. * converted to atomic does the right thing.
  10085. */
  10086. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10087. {
  10088. struct intel_connector *connector;
  10089. for_each_intel_connector(dev, connector) {
  10090. if (connector->base.encoder) {
  10091. connector->base.state->best_encoder =
  10092. connector->base.encoder;
  10093. connector->base.state->crtc =
  10094. connector->base.encoder->crtc;
  10095. } else {
  10096. connector->base.state->best_encoder = NULL;
  10097. connector->base.state->crtc = NULL;
  10098. }
  10099. }
  10100. }
  10101. static void
  10102. connected_sink_compute_bpp(struct intel_connector *connector,
  10103. struct intel_crtc_state *pipe_config)
  10104. {
  10105. int bpp = pipe_config->pipe_bpp;
  10106. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10107. connector->base.base.id,
  10108. connector->base.name);
  10109. /* Don't use an invalid EDID bpc value */
  10110. if (connector->base.display_info.bpc &&
  10111. connector->base.display_info.bpc * 3 < bpp) {
  10112. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10113. bpp, connector->base.display_info.bpc*3);
  10114. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10115. }
  10116. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10117. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  10118. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10119. bpp);
  10120. pipe_config->pipe_bpp = 24;
  10121. }
  10122. }
  10123. static int
  10124. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10125. struct intel_crtc_state *pipe_config)
  10126. {
  10127. struct drm_device *dev = crtc->base.dev;
  10128. struct drm_atomic_state *state;
  10129. struct drm_connector *connector;
  10130. struct drm_connector_state *connector_state;
  10131. int bpp, i;
  10132. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  10133. bpp = 10*3;
  10134. else if (INTEL_INFO(dev)->gen >= 5)
  10135. bpp = 12*3;
  10136. else
  10137. bpp = 8*3;
  10138. pipe_config->pipe_bpp = bpp;
  10139. state = pipe_config->base.state;
  10140. /* Clamp display bpp to EDID value */
  10141. for_each_connector_in_state(state, connector, connector_state, i) {
  10142. if (connector_state->crtc != &crtc->base)
  10143. continue;
  10144. connected_sink_compute_bpp(to_intel_connector(connector),
  10145. pipe_config);
  10146. }
  10147. return bpp;
  10148. }
  10149. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10150. {
  10151. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10152. "type: 0x%x flags: 0x%x\n",
  10153. mode->crtc_clock,
  10154. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10155. mode->crtc_hsync_end, mode->crtc_htotal,
  10156. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10157. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10158. }
  10159. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10160. struct intel_crtc_state *pipe_config,
  10161. const char *context)
  10162. {
  10163. struct drm_device *dev = crtc->base.dev;
  10164. struct drm_plane *plane;
  10165. struct intel_plane *intel_plane;
  10166. struct intel_plane_state *state;
  10167. struct drm_framebuffer *fb;
  10168. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10169. context, pipe_config, pipe_name(crtc->pipe));
  10170. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10171. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10172. pipe_config->pipe_bpp, pipe_config->dither);
  10173. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10174. pipe_config->has_pch_encoder,
  10175. pipe_config->fdi_lanes,
  10176. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10177. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10178. pipe_config->fdi_m_n.tu);
  10179. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10180. pipe_config->has_dp_encoder,
  10181. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10182. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10183. pipe_config->dp_m_n.tu);
  10184. DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10185. pipe_config->has_dp_encoder,
  10186. pipe_config->dp_m2_n2.gmch_m,
  10187. pipe_config->dp_m2_n2.gmch_n,
  10188. pipe_config->dp_m2_n2.link_m,
  10189. pipe_config->dp_m2_n2.link_n,
  10190. pipe_config->dp_m2_n2.tu);
  10191. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10192. pipe_config->has_audio,
  10193. pipe_config->has_infoframe);
  10194. DRM_DEBUG_KMS("requested mode:\n");
  10195. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10196. DRM_DEBUG_KMS("adjusted mode:\n");
  10197. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10198. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10199. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10200. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10201. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10202. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10203. crtc->num_scalers,
  10204. pipe_config->scaler_state.scaler_users,
  10205. pipe_config->scaler_state.scaler_id);
  10206. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10207. pipe_config->gmch_pfit.control,
  10208. pipe_config->gmch_pfit.pgm_ratios,
  10209. pipe_config->gmch_pfit.lvds_border_bits);
  10210. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10211. pipe_config->pch_pfit.pos,
  10212. pipe_config->pch_pfit.size,
  10213. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10214. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10215. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10216. if (IS_BROXTON(dev)) {
  10217. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
  10218. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10219. "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
  10220. pipe_config->ddi_pll_sel,
  10221. pipe_config->dpll_hw_state.ebb0,
  10222. pipe_config->dpll_hw_state.pll0,
  10223. pipe_config->dpll_hw_state.pll1,
  10224. pipe_config->dpll_hw_state.pll2,
  10225. pipe_config->dpll_hw_state.pll3,
  10226. pipe_config->dpll_hw_state.pll6,
  10227. pipe_config->dpll_hw_state.pll8,
  10228. pipe_config->dpll_hw_state.pcsdw12);
  10229. } else if (IS_SKYLAKE(dev)) {
  10230. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10231. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10232. pipe_config->ddi_pll_sel,
  10233. pipe_config->dpll_hw_state.ctrl1,
  10234. pipe_config->dpll_hw_state.cfgcr1,
  10235. pipe_config->dpll_hw_state.cfgcr2);
  10236. } else if (HAS_DDI(dev)) {
  10237. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10238. pipe_config->ddi_pll_sel,
  10239. pipe_config->dpll_hw_state.wrpll);
  10240. } else {
  10241. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10242. "fp0: 0x%x, fp1: 0x%x\n",
  10243. pipe_config->dpll_hw_state.dpll,
  10244. pipe_config->dpll_hw_state.dpll_md,
  10245. pipe_config->dpll_hw_state.fp0,
  10246. pipe_config->dpll_hw_state.fp1);
  10247. }
  10248. DRM_DEBUG_KMS("planes on this crtc\n");
  10249. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10250. intel_plane = to_intel_plane(plane);
  10251. if (intel_plane->pipe != crtc->pipe)
  10252. continue;
  10253. state = to_intel_plane_state(plane->state);
  10254. fb = state->base.fb;
  10255. if (!fb) {
  10256. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10257. "disabled, scaler_id = %d\n",
  10258. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10259. plane->base.id, intel_plane->pipe,
  10260. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10261. drm_plane_index(plane), state->scaler_id);
  10262. continue;
  10263. }
  10264. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10265. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10266. plane->base.id, intel_plane->pipe,
  10267. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10268. drm_plane_index(plane));
  10269. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10270. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10271. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10272. state->scaler_id,
  10273. state->src.x1 >> 16, state->src.y1 >> 16,
  10274. drm_rect_width(&state->src) >> 16,
  10275. drm_rect_height(&state->src) >> 16,
  10276. state->dst.x1, state->dst.y1,
  10277. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10278. }
  10279. }
  10280. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10281. {
  10282. struct drm_device *dev = state->dev;
  10283. struct intel_encoder *encoder;
  10284. struct drm_connector *connector;
  10285. struct drm_connector_state *connector_state;
  10286. unsigned int used_ports = 0;
  10287. int i;
  10288. /*
  10289. * Walk the connector list instead of the encoder
  10290. * list to detect the problem on ddi platforms
  10291. * where there's just one encoder per digital port.
  10292. */
  10293. for_each_connector_in_state(state, connector, connector_state, i) {
  10294. if (!connector_state->best_encoder)
  10295. continue;
  10296. encoder = to_intel_encoder(connector_state->best_encoder);
  10297. WARN_ON(!connector_state->crtc);
  10298. switch (encoder->type) {
  10299. unsigned int port_mask;
  10300. case INTEL_OUTPUT_UNKNOWN:
  10301. if (WARN_ON(!HAS_DDI(dev)))
  10302. break;
  10303. case INTEL_OUTPUT_DISPLAYPORT:
  10304. case INTEL_OUTPUT_HDMI:
  10305. case INTEL_OUTPUT_EDP:
  10306. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10307. /* the same port mustn't appear more than once */
  10308. if (used_ports & port_mask)
  10309. return false;
  10310. used_ports |= port_mask;
  10311. default:
  10312. break;
  10313. }
  10314. }
  10315. return true;
  10316. }
  10317. static void
  10318. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10319. {
  10320. struct drm_crtc_state tmp_state;
  10321. struct intel_crtc_scaler_state scaler_state;
  10322. struct intel_dpll_hw_state dpll_hw_state;
  10323. enum intel_dpll_id shared_dpll;
  10324. uint32_t ddi_pll_sel;
  10325. /* FIXME: before the switch to atomic started, a new pipe_config was
  10326. * kzalloc'd. Code that depends on any field being zero should be
  10327. * fixed, so that the crtc_state can be safely duplicated. For now,
  10328. * only fields that are know to not cause problems are preserved. */
  10329. tmp_state = crtc_state->base;
  10330. scaler_state = crtc_state->scaler_state;
  10331. shared_dpll = crtc_state->shared_dpll;
  10332. dpll_hw_state = crtc_state->dpll_hw_state;
  10333. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10334. memset(crtc_state, 0, sizeof *crtc_state);
  10335. crtc_state->base = tmp_state;
  10336. crtc_state->scaler_state = scaler_state;
  10337. crtc_state->shared_dpll = shared_dpll;
  10338. crtc_state->dpll_hw_state = dpll_hw_state;
  10339. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10340. }
  10341. static int
  10342. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10343. struct intel_crtc_state *pipe_config)
  10344. {
  10345. struct drm_atomic_state *state = pipe_config->base.state;
  10346. struct intel_encoder *encoder;
  10347. struct drm_connector *connector;
  10348. struct drm_connector_state *connector_state;
  10349. int base_bpp, ret = -EINVAL;
  10350. int i;
  10351. bool retry = true;
  10352. clear_intel_crtc_state(pipe_config);
  10353. pipe_config->cpu_transcoder =
  10354. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10355. /*
  10356. * Sanitize sync polarity flags based on requested ones. If neither
  10357. * positive or negative polarity is requested, treat this as meaning
  10358. * negative polarity.
  10359. */
  10360. if (!(pipe_config->base.adjusted_mode.flags &
  10361. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10362. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10363. if (!(pipe_config->base.adjusted_mode.flags &
  10364. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10365. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10366. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  10367. * plane pixel format and any sink constraints into account. Returns the
  10368. * source plane bpp so that dithering can be selected on mismatches
  10369. * after encoders and crtc also have had their say. */
  10370. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10371. pipe_config);
  10372. if (base_bpp < 0)
  10373. goto fail;
  10374. /*
  10375. * Determine the real pipe dimensions. Note that stereo modes can
  10376. * increase the actual pipe size due to the frame doubling and
  10377. * insertion of additional space for blanks between the frame. This
  10378. * is stored in the crtc timings. We use the requested mode to do this
  10379. * computation to clearly distinguish it from the adjusted mode, which
  10380. * can be changed by the connectors in the below retry loop.
  10381. */
  10382. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10383. &pipe_config->pipe_src_w,
  10384. &pipe_config->pipe_src_h);
  10385. encoder_retry:
  10386. /* Ensure the port clock defaults are reset when retrying. */
  10387. pipe_config->port_clock = 0;
  10388. pipe_config->pixel_multiplier = 1;
  10389. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10390. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10391. CRTC_STEREO_DOUBLE);
  10392. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10393. * adjust it according to limitations or connector properties, and also
  10394. * a chance to reject the mode entirely.
  10395. */
  10396. for_each_connector_in_state(state, connector, connector_state, i) {
  10397. if (connector_state->crtc != crtc)
  10398. continue;
  10399. encoder = to_intel_encoder(connector_state->best_encoder);
  10400. if (!(encoder->compute_config(encoder, pipe_config))) {
  10401. DRM_DEBUG_KMS("Encoder config failure\n");
  10402. goto fail;
  10403. }
  10404. }
  10405. /* Set default port clock if not overwritten by the encoder. Needs to be
  10406. * done afterwards in case the encoder adjusts the mode. */
  10407. if (!pipe_config->port_clock)
  10408. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10409. * pipe_config->pixel_multiplier;
  10410. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10411. if (ret < 0) {
  10412. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10413. goto fail;
  10414. }
  10415. if (ret == RETRY) {
  10416. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10417. ret = -EINVAL;
  10418. goto fail;
  10419. }
  10420. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10421. retry = false;
  10422. goto encoder_retry;
  10423. }
  10424. pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
  10425. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  10426. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10427. /* Check if we need to force a modeset */
  10428. if (pipe_config->has_audio !=
  10429. to_intel_crtc_state(crtc->state)->has_audio) {
  10430. pipe_config->base.mode_changed = true;
  10431. ret = drm_atomic_add_affected_planes(state, crtc);
  10432. }
  10433. /*
  10434. * Note we have an issue here with infoframes: current code
  10435. * only updates them on the full mode set path per hw
  10436. * requirements. So here we should be checking for any
  10437. * required changes and forcing a mode set.
  10438. */
  10439. fail:
  10440. return ret;
  10441. }
  10442. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  10443. {
  10444. struct drm_encoder *encoder;
  10445. struct drm_device *dev = crtc->dev;
  10446. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  10447. if (encoder->crtc == crtc)
  10448. return true;
  10449. return false;
  10450. }
  10451. static void
  10452. intel_modeset_update_state(struct drm_atomic_state *state)
  10453. {
  10454. struct drm_device *dev = state->dev;
  10455. struct intel_encoder *intel_encoder;
  10456. struct drm_crtc *crtc;
  10457. struct drm_crtc_state *crtc_state;
  10458. struct drm_connector *connector;
  10459. intel_shared_dpll_commit(state);
  10460. for_each_intel_encoder(dev, intel_encoder) {
  10461. if (!intel_encoder->base.crtc)
  10462. continue;
  10463. crtc = intel_encoder->base.crtc;
  10464. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10465. if (!crtc_state || !needs_modeset(crtc->state))
  10466. continue;
  10467. intel_encoder->connectors_active = false;
  10468. }
  10469. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10470. intel_modeset_update_staged_output_state(state->dev);
  10471. /* Double check state. */
  10472. for_each_crtc(dev, crtc) {
  10473. WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
  10474. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10475. /* Update hwmode for vblank functions */
  10476. if (crtc->state->active)
  10477. crtc->hwmode = crtc->state->adjusted_mode;
  10478. else
  10479. crtc->hwmode.crtc_clock = 0;
  10480. }
  10481. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10482. if (!connector->encoder || !connector->encoder->crtc)
  10483. continue;
  10484. crtc = connector->encoder->crtc;
  10485. crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
  10486. if (!crtc_state || !needs_modeset(crtc->state))
  10487. continue;
  10488. if (crtc->state->active) {
  10489. struct drm_property *dpms_property =
  10490. dev->mode_config.dpms_property;
  10491. connector->dpms = DRM_MODE_DPMS_ON;
  10492. drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
  10493. intel_encoder = to_intel_encoder(connector->encoder);
  10494. intel_encoder->connectors_active = true;
  10495. } else
  10496. connector->dpms = DRM_MODE_DPMS_OFF;
  10497. }
  10498. }
  10499. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10500. {
  10501. int diff;
  10502. if (clock1 == clock2)
  10503. return true;
  10504. if (!clock1 || !clock2)
  10505. return false;
  10506. diff = abs(clock1 - clock2);
  10507. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10508. return true;
  10509. return false;
  10510. }
  10511. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10512. list_for_each_entry((intel_crtc), \
  10513. &(dev)->mode_config.crtc_list, \
  10514. base.head) \
  10515. if (mask & (1 <<(intel_crtc)->pipe))
  10516. static bool
  10517. intel_pipe_config_compare(struct drm_device *dev,
  10518. struct intel_crtc_state *current_config,
  10519. struct intel_crtc_state *pipe_config)
  10520. {
  10521. #define PIPE_CONF_CHECK_X(name) \
  10522. if (current_config->name != pipe_config->name) { \
  10523. DRM_ERROR("mismatch in " #name " " \
  10524. "(expected 0x%08x, found 0x%08x)\n", \
  10525. current_config->name, \
  10526. pipe_config->name); \
  10527. return false; \
  10528. }
  10529. #define PIPE_CONF_CHECK_I(name) \
  10530. if (current_config->name != pipe_config->name) { \
  10531. DRM_ERROR("mismatch in " #name " " \
  10532. "(expected %i, found %i)\n", \
  10533. current_config->name, \
  10534. pipe_config->name); \
  10535. return false; \
  10536. }
  10537. /* This is required for BDW+ where there is only one set of registers for
  10538. * switching between high and low RR.
  10539. * This macro can be used whenever a comparison has to be made between one
  10540. * hw state and multiple sw state variables.
  10541. */
  10542. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10543. if ((current_config->name != pipe_config->name) && \
  10544. (current_config->alt_name != pipe_config->name)) { \
  10545. DRM_ERROR("mismatch in " #name " " \
  10546. "(expected %i or %i, found %i)\n", \
  10547. current_config->name, \
  10548. current_config->alt_name, \
  10549. pipe_config->name); \
  10550. return false; \
  10551. }
  10552. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10553. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10554. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  10555. "(expected %i, found %i)\n", \
  10556. current_config->name & (mask), \
  10557. pipe_config->name & (mask)); \
  10558. return false; \
  10559. }
  10560. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10561. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10562. DRM_ERROR("mismatch in " #name " " \
  10563. "(expected %i, found %i)\n", \
  10564. current_config->name, \
  10565. pipe_config->name); \
  10566. return false; \
  10567. }
  10568. #define PIPE_CONF_QUIRK(quirk) \
  10569. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10570. PIPE_CONF_CHECK_I(cpu_transcoder);
  10571. PIPE_CONF_CHECK_I(has_pch_encoder);
  10572. PIPE_CONF_CHECK_I(fdi_lanes);
  10573. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  10574. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  10575. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  10576. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  10577. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  10578. PIPE_CONF_CHECK_I(has_dp_encoder);
  10579. if (INTEL_INFO(dev)->gen < 8) {
  10580. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  10581. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  10582. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  10583. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  10584. PIPE_CONF_CHECK_I(dp_m_n.tu);
  10585. if (current_config->has_drrs) {
  10586. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
  10587. PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
  10588. PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
  10589. PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
  10590. PIPE_CONF_CHECK_I(dp_m2_n2.tu);
  10591. }
  10592. } else {
  10593. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
  10594. PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
  10595. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
  10596. PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
  10597. PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
  10598. }
  10599. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10600. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10601. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10602. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10603. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10604. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10605. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10606. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10607. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10608. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10609. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10610. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10611. PIPE_CONF_CHECK_I(pixel_multiplier);
  10612. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10613. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10614. IS_VALLEYVIEW(dev))
  10615. PIPE_CONF_CHECK_I(limited_color_range);
  10616. PIPE_CONF_CHECK_I(has_infoframe);
  10617. PIPE_CONF_CHECK_I(has_audio);
  10618. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10619. DRM_MODE_FLAG_INTERLACE);
  10620. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10621. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10622. DRM_MODE_FLAG_PHSYNC);
  10623. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10624. DRM_MODE_FLAG_NHSYNC);
  10625. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10626. DRM_MODE_FLAG_PVSYNC);
  10627. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10628. DRM_MODE_FLAG_NVSYNC);
  10629. }
  10630. PIPE_CONF_CHECK_I(pipe_src_w);
  10631. PIPE_CONF_CHECK_I(pipe_src_h);
  10632. /*
  10633. * FIXME: BIOS likes to set up a cloned config with lvds+external
  10634. * screen. Since we don't yet re-compute the pipe config when moving
  10635. * just the lvds port away to another pipe the sw tracking won't match.
  10636. *
  10637. * Proper atomic modesets with recomputed global state will fix this.
  10638. * Until then just don't check gmch state for inherited modes.
  10639. */
  10640. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  10641. PIPE_CONF_CHECK_I(gmch_pfit.control);
  10642. /* pfit ratios are autocomputed by the hw on gen4+ */
  10643. if (INTEL_INFO(dev)->gen < 4)
  10644. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10645. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  10646. }
  10647. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10648. if (current_config->pch_pfit.enabled) {
  10649. PIPE_CONF_CHECK_I(pch_pfit.pos);
  10650. PIPE_CONF_CHECK_I(pch_pfit.size);
  10651. }
  10652. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10653. /* BDW+ don't expose a synchronous way to read the state */
  10654. if (IS_HASWELL(dev))
  10655. PIPE_CONF_CHECK_I(ips_enabled);
  10656. PIPE_CONF_CHECK_I(double_wide);
  10657. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10658. PIPE_CONF_CHECK_I(shared_dpll);
  10659. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10660. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10661. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10662. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10663. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10664. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10665. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10666. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10667. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10668. PIPE_CONF_CHECK_I(pipe_bpp);
  10669. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10670. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10671. #undef PIPE_CONF_CHECK_X
  10672. #undef PIPE_CONF_CHECK_I
  10673. #undef PIPE_CONF_CHECK_I_ALT
  10674. #undef PIPE_CONF_CHECK_FLAGS
  10675. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10676. #undef PIPE_CONF_QUIRK
  10677. return true;
  10678. }
  10679. static void check_wm_state(struct drm_device *dev)
  10680. {
  10681. struct drm_i915_private *dev_priv = dev->dev_private;
  10682. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10683. struct intel_crtc *intel_crtc;
  10684. int plane;
  10685. if (INTEL_INFO(dev)->gen < 9)
  10686. return;
  10687. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10688. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10689. for_each_intel_crtc(dev, intel_crtc) {
  10690. struct skl_ddb_entry *hw_entry, *sw_entry;
  10691. const enum pipe pipe = intel_crtc->pipe;
  10692. if (!intel_crtc->active)
  10693. continue;
  10694. /* planes */
  10695. for_each_plane(dev_priv, pipe, plane) {
  10696. hw_entry = &hw_ddb.plane[pipe][plane];
  10697. sw_entry = &sw_ddb->plane[pipe][plane];
  10698. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10699. continue;
  10700. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10701. "(expected (%u,%u), found (%u,%u))\n",
  10702. pipe_name(pipe), plane + 1,
  10703. sw_entry->start, sw_entry->end,
  10704. hw_entry->start, hw_entry->end);
  10705. }
  10706. /* cursor */
  10707. hw_entry = &hw_ddb.cursor[pipe];
  10708. sw_entry = &sw_ddb->cursor[pipe];
  10709. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10710. continue;
  10711. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10712. "(expected (%u,%u), found (%u,%u))\n",
  10713. pipe_name(pipe),
  10714. sw_entry->start, sw_entry->end,
  10715. hw_entry->start, hw_entry->end);
  10716. }
  10717. }
  10718. static void
  10719. check_connector_state(struct drm_device *dev)
  10720. {
  10721. struct intel_connector *connector;
  10722. for_each_intel_connector(dev, connector) {
  10723. /* This also checks the encoder/connector hw state with the
  10724. * ->get_hw_state callbacks. */
  10725. intel_connector_check_state(connector);
  10726. I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
  10727. "connector's staged encoder doesn't match current encoder\n");
  10728. }
  10729. }
  10730. static void
  10731. check_encoder_state(struct drm_device *dev)
  10732. {
  10733. struct intel_encoder *encoder;
  10734. struct intel_connector *connector;
  10735. for_each_intel_encoder(dev, encoder) {
  10736. bool enabled = false;
  10737. bool active = false;
  10738. enum pipe pipe, tracked_pipe;
  10739. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10740. encoder->base.base.id,
  10741. encoder->base.name);
  10742. I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
  10743. "encoder's stage crtc doesn't match current crtc\n");
  10744. I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
  10745. "encoder's active_connectors set, but no crtc\n");
  10746. for_each_intel_connector(dev, connector) {
  10747. if (connector->base.encoder != &encoder->base)
  10748. continue;
  10749. enabled = true;
  10750. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  10751. active = true;
  10752. }
  10753. /*
  10754. * for MST connectors if we unplug the connector is gone
  10755. * away but the encoder is still connected to a crtc
  10756. * until a modeset happens in response to the hotplug.
  10757. */
  10758. if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
  10759. continue;
  10760. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10761. "encoder's enabled state mismatch "
  10762. "(expected %i, found %i)\n",
  10763. !!encoder->base.crtc, enabled);
  10764. I915_STATE_WARN(active && !encoder->base.crtc,
  10765. "active encoder with no crtc\n");
  10766. I915_STATE_WARN(encoder->connectors_active != active,
  10767. "encoder's computed active state doesn't match tracked active state "
  10768. "(expected %i, found %i)\n", active, encoder->connectors_active);
  10769. active = encoder->get_hw_state(encoder, &pipe);
  10770. I915_STATE_WARN(active != encoder->connectors_active,
  10771. "encoder's hw state doesn't match sw tracking "
  10772. "(expected %i, found %i)\n",
  10773. encoder->connectors_active, active);
  10774. if (!encoder->base.crtc)
  10775. continue;
  10776. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  10777. I915_STATE_WARN(active && pipe != tracked_pipe,
  10778. "active encoder's pipe doesn't match"
  10779. "(expected %i, found %i)\n",
  10780. tracked_pipe, pipe);
  10781. }
  10782. }
  10783. static void
  10784. check_crtc_state(struct drm_device *dev)
  10785. {
  10786. struct drm_i915_private *dev_priv = dev->dev_private;
  10787. struct intel_crtc *crtc;
  10788. struct intel_encoder *encoder;
  10789. struct intel_crtc_state pipe_config;
  10790. for_each_intel_crtc(dev, crtc) {
  10791. bool enabled = false;
  10792. bool active = false;
  10793. memset(&pipe_config, 0, sizeof(pipe_config));
  10794. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10795. crtc->base.base.id);
  10796. I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
  10797. "active crtc, but not enabled in sw tracking\n");
  10798. for_each_intel_encoder(dev, encoder) {
  10799. if (encoder->base.crtc != &crtc->base)
  10800. continue;
  10801. enabled = true;
  10802. if (encoder->connectors_active)
  10803. active = true;
  10804. }
  10805. I915_STATE_WARN(active != crtc->active,
  10806. "crtc's computed active state doesn't match tracked active state "
  10807. "(expected %i, found %i)\n", active, crtc->active);
  10808. I915_STATE_WARN(enabled != crtc->base.state->enable,
  10809. "crtc's computed enabled state doesn't match tracked enabled state "
  10810. "(expected %i, found %i)\n", enabled,
  10811. crtc->base.state->enable);
  10812. active = dev_priv->display.get_pipe_config(crtc,
  10813. &pipe_config);
  10814. /* hw state is inconsistent with the pipe quirk */
  10815. if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10816. (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10817. active = crtc->active;
  10818. for_each_intel_encoder(dev, encoder) {
  10819. enum pipe pipe;
  10820. if (encoder->base.crtc != &crtc->base)
  10821. continue;
  10822. if (encoder->get_hw_state(encoder, &pipe))
  10823. encoder->get_config(encoder, &pipe_config);
  10824. }
  10825. I915_STATE_WARN(crtc->active != active,
  10826. "crtc active state doesn't match with hw state "
  10827. "(expected %i, found %i)\n", crtc->active, active);
  10828. I915_STATE_WARN(crtc->active != crtc->base.state->active,
  10829. "transitional active state does not match atomic hw state "
  10830. "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
  10831. if (active &&
  10832. !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
  10833. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10834. intel_dump_pipe_config(crtc, &pipe_config,
  10835. "[hw state]");
  10836. intel_dump_pipe_config(crtc, crtc->config,
  10837. "[sw state]");
  10838. }
  10839. }
  10840. }
  10841. static void
  10842. check_shared_dpll_state(struct drm_device *dev)
  10843. {
  10844. struct drm_i915_private *dev_priv = dev->dev_private;
  10845. struct intel_crtc *crtc;
  10846. struct intel_dpll_hw_state dpll_hw_state;
  10847. int i;
  10848. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10849. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10850. int enabled_crtcs = 0, active_crtcs = 0;
  10851. bool active;
  10852. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10853. DRM_DEBUG_KMS("%s\n", pll->name);
  10854. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10855. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10856. "more active pll users than references: %i vs %i\n",
  10857. pll->active, hweight32(pll->config.crtc_mask));
  10858. I915_STATE_WARN(pll->active && !pll->on,
  10859. "pll in active use but not on in sw tracking\n");
  10860. I915_STATE_WARN(pll->on && !pll->active,
  10861. "pll in on but not on in use in sw tracking\n");
  10862. I915_STATE_WARN(pll->on != active,
  10863. "pll on state mismatch (expected %i, found %i)\n",
  10864. pll->on, active);
  10865. for_each_intel_crtc(dev, crtc) {
  10866. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10867. enabled_crtcs++;
  10868. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10869. active_crtcs++;
  10870. }
  10871. I915_STATE_WARN(pll->active != active_crtcs,
  10872. "pll active crtcs mismatch (expected %i, found %i)\n",
  10873. pll->active, active_crtcs);
  10874. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10875. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10876. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10877. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10878. sizeof(dpll_hw_state)),
  10879. "pll hw state mismatch\n");
  10880. }
  10881. }
  10882. void
  10883. intel_modeset_check_state(struct drm_device *dev)
  10884. {
  10885. check_wm_state(dev);
  10886. check_connector_state(dev);
  10887. check_encoder_state(dev);
  10888. check_crtc_state(dev);
  10889. check_shared_dpll_state(dev);
  10890. }
  10891. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10892. int dotclock)
  10893. {
  10894. /*
  10895. * FDI already provided one idea for the dotclock.
  10896. * Yell if the encoder disagrees.
  10897. */
  10898. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10899. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10900. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10901. }
  10902. static void update_scanline_offset(struct intel_crtc *crtc)
  10903. {
  10904. struct drm_device *dev = crtc->base.dev;
  10905. /*
  10906. * The scanline counter increments at the leading edge of hsync.
  10907. *
  10908. * On most platforms it starts counting from vtotal-1 on the
  10909. * first active line. That means the scanline counter value is
  10910. * always one less than what we would expect. Ie. just after
  10911. * start of vblank, which also occurs at start of hsync (on the
  10912. * last active line), the scanline counter will read vblank_start-1.
  10913. *
  10914. * On gen2 the scanline counter starts counting from 1 instead
  10915. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10916. * to keep the value positive), instead of adding one.
  10917. *
  10918. * On HSW+ the behaviour of the scanline counter depends on the output
  10919. * type. For DP ports it behaves like most other platforms, but on HDMI
  10920. * there's an extra 1 line difference. So we need to add two instead of
  10921. * one to the value.
  10922. */
  10923. if (IS_GEN2(dev)) {
  10924. const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
  10925. int vtotal;
  10926. vtotal = mode->crtc_vtotal;
  10927. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  10928. vtotal /= 2;
  10929. crtc->scanline_offset = vtotal - 1;
  10930. } else if (HAS_DDI(dev) &&
  10931. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10932. crtc->scanline_offset = 2;
  10933. } else
  10934. crtc->scanline_offset = 1;
  10935. }
  10936. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10937. {
  10938. struct drm_device *dev = state->dev;
  10939. struct drm_i915_private *dev_priv = to_i915(dev);
  10940. struct intel_shared_dpll_config *shared_dpll = NULL;
  10941. struct intel_crtc *intel_crtc;
  10942. struct intel_crtc_state *intel_crtc_state;
  10943. struct drm_crtc *crtc;
  10944. struct drm_crtc_state *crtc_state;
  10945. int i;
  10946. if (!dev_priv->display.crtc_compute_clock)
  10947. return;
  10948. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10949. int dpll;
  10950. intel_crtc = to_intel_crtc(crtc);
  10951. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10952. dpll = intel_crtc_state->shared_dpll;
  10953. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10954. continue;
  10955. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10956. if (!shared_dpll)
  10957. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10958. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10959. }
  10960. }
  10961. /*
  10962. * This implements the workaround described in the "notes" section of the mode
  10963. * set sequence documentation. When going from no pipes or single pipe to
  10964. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10965. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10966. */
  10967. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10968. {
  10969. struct drm_crtc_state *crtc_state;
  10970. struct intel_crtc *intel_crtc;
  10971. struct drm_crtc *crtc;
  10972. struct intel_crtc_state *first_crtc_state = NULL;
  10973. struct intel_crtc_state *other_crtc_state = NULL;
  10974. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10975. int i;
  10976. /* look at all crtc's that are going to be enabled in during modeset */
  10977. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10978. intel_crtc = to_intel_crtc(crtc);
  10979. if (!crtc_state->active || !needs_modeset(crtc_state))
  10980. continue;
  10981. if (first_crtc_state) {
  10982. other_crtc_state = to_intel_crtc_state(crtc_state);
  10983. break;
  10984. } else {
  10985. first_crtc_state = to_intel_crtc_state(crtc_state);
  10986. first_pipe = intel_crtc->pipe;
  10987. }
  10988. }
  10989. /* No workaround needed? */
  10990. if (!first_crtc_state)
  10991. return 0;
  10992. /* w/a possibly needed, check how many crtc's are already enabled. */
  10993. for_each_intel_crtc(state->dev, intel_crtc) {
  10994. struct intel_crtc_state *pipe_config;
  10995. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10996. if (IS_ERR(pipe_config))
  10997. return PTR_ERR(pipe_config);
  10998. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10999. if (!pipe_config->base.active ||
  11000. needs_modeset(&pipe_config->base))
  11001. continue;
  11002. /* 2 or more enabled crtcs means no need for w/a */
  11003. if (enabled_pipe != INVALID_PIPE)
  11004. return 0;
  11005. enabled_pipe = intel_crtc->pipe;
  11006. }
  11007. if (enabled_pipe != INVALID_PIPE)
  11008. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11009. else if (other_crtc_state)
  11010. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11011. return 0;
  11012. }
  11013. /* Code that should eventually be part of atomic_check() */
  11014. static int intel_modeset_checks(struct drm_atomic_state *state)
  11015. {
  11016. struct drm_device *dev = state->dev;
  11017. int ret;
  11018. if (!check_digital_port_conflicts(state)) {
  11019. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11020. return -EINVAL;
  11021. }
  11022. /*
  11023. * See if the config requires any additional preparation, e.g.
  11024. * to adjust global state with pipes off. We need to do this
  11025. * here so we can get the modeset_pipe updated config for the new
  11026. * mode set on this crtc. For other crtcs we need to use the
  11027. * adjusted_mode bits in the crtc directly.
  11028. */
  11029. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
  11030. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
  11031. ret = valleyview_modeset_global_pipes(state);
  11032. else
  11033. ret = broadwell_modeset_global_pipes(state);
  11034. if (ret)
  11035. return ret;
  11036. }
  11037. intel_modeset_clear_plls(state);
  11038. if (IS_HASWELL(dev))
  11039. return haswell_mode_set_planes_workaround(state);
  11040. return 0;
  11041. }
  11042. static int
  11043. intel_modeset_compute_config(struct drm_atomic_state *state)
  11044. {
  11045. struct drm_crtc *crtc;
  11046. struct drm_crtc_state *crtc_state;
  11047. int ret, i;
  11048. ret = drm_atomic_helper_check_modeset(state->dev, state);
  11049. if (ret)
  11050. return ret;
  11051. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11052. if (!crtc_state->enable &&
  11053. WARN_ON(crtc_state->active))
  11054. crtc_state->active = false;
  11055. if (!crtc_state->enable)
  11056. continue;
  11057. if (!needs_modeset(crtc_state)) {
  11058. ret = drm_atomic_add_affected_connectors(state, crtc);
  11059. if (ret)
  11060. return ret;
  11061. }
  11062. ret = intel_modeset_pipe_config(crtc,
  11063. to_intel_crtc_state(crtc_state));
  11064. if (ret)
  11065. return ret;
  11066. intel_dump_pipe_config(to_intel_crtc(crtc),
  11067. to_intel_crtc_state(crtc_state),
  11068. "[modeset]");
  11069. }
  11070. ret = intel_modeset_checks(state);
  11071. if (ret)
  11072. return ret;
  11073. return drm_atomic_helper_check_planes(state->dev, state);
  11074. }
  11075. static int __intel_set_mode(struct drm_atomic_state *state)
  11076. {
  11077. struct drm_device *dev = state->dev;
  11078. struct drm_i915_private *dev_priv = dev->dev_private;
  11079. struct drm_crtc *crtc;
  11080. struct drm_crtc_state *crtc_state;
  11081. int ret = 0;
  11082. int i;
  11083. ret = drm_atomic_helper_prepare_planes(dev, state);
  11084. if (ret)
  11085. return ret;
  11086. drm_atomic_helper_swap_state(dev, state);
  11087. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11088. if (!needs_modeset(crtc->state) || !crtc_state->active)
  11089. continue;
  11090. intel_crtc_disable_planes(crtc);
  11091. dev_priv->display.crtc_disable(crtc);
  11092. }
  11093. /* Only after disabling all output pipelines that will be changed can we
  11094. * update the the output configuration. */
  11095. intel_modeset_update_state(state);
  11096. /* The state has been swaped above, so state actually contains the
  11097. * old state now. */
  11098. modeset_update_crtc_power_domains(state);
  11099. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11100. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11101. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11102. if (!needs_modeset(crtc->state) || !crtc->state->active)
  11103. continue;
  11104. update_scanline_offset(to_intel_crtc(crtc));
  11105. dev_priv->display.crtc_enable(crtc);
  11106. intel_crtc_enable_planes(crtc);
  11107. }
  11108. /* FIXME: add subpixel order */
  11109. drm_atomic_helper_cleanup_planes(dev, state);
  11110. drm_atomic_state_free(state);
  11111. return 0;
  11112. }
  11113. static int intel_set_mode_checked(struct drm_atomic_state *state)
  11114. {
  11115. struct drm_device *dev = state->dev;
  11116. int ret;
  11117. ret = __intel_set_mode(state);
  11118. if (ret == 0)
  11119. intel_modeset_check_state(dev);
  11120. return ret;
  11121. }
  11122. static int intel_set_mode(struct drm_atomic_state *state)
  11123. {
  11124. int ret;
  11125. ret = intel_modeset_compute_config(state);
  11126. if (ret)
  11127. return ret;
  11128. return intel_set_mode_checked(state);
  11129. }
  11130. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11131. {
  11132. struct drm_device *dev = crtc->dev;
  11133. struct drm_atomic_state *state;
  11134. struct intel_crtc *intel_crtc;
  11135. struct intel_encoder *encoder;
  11136. struct intel_connector *connector;
  11137. struct drm_connector_state *connector_state;
  11138. struct intel_crtc_state *crtc_state;
  11139. int ret;
  11140. state = drm_atomic_state_alloc(dev);
  11141. if (!state) {
  11142. DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
  11143. crtc->base.id);
  11144. return;
  11145. }
  11146. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11147. /* The force restore path in the HW readout code relies on the staged
  11148. * config still keeping the user requested config while the actual
  11149. * state has been overwritten by the configuration read from HW. We
  11150. * need to copy the staged config to the atomic state, otherwise the
  11151. * mode set will just reapply the state the HW is already in. */
  11152. for_each_intel_encoder(dev, encoder) {
  11153. if (&encoder->new_crtc->base != crtc)
  11154. continue;
  11155. for_each_intel_connector(dev, connector) {
  11156. if (connector->new_encoder != encoder)
  11157. continue;
  11158. connector_state = drm_atomic_get_connector_state(state, &connector->base);
  11159. if (IS_ERR(connector_state)) {
  11160. DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
  11161. connector->base.base.id,
  11162. connector->base.name,
  11163. PTR_ERR(connector_state));
  11164. continue;
  11165. }
  11166. connector_state->crtc = crtc;
  11167. connector_state->best_encoder = &encoder->base;
  11168. }
  11169. }
  11170. for_each_intel_crtc(dev, intel_crtc) {
  11171. if (intel_crtc->new_enabled == intel_crtc->base.enabled)
  11172. continue;
  11173. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  11174. if (IS_ERR(crtc_state)) {
  11175. DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
  11176. intel_crtc->base.base.id,
  11177. PTR_ERR(crtc_state));
  11178. continue;
  11179. }
  11180. crtc_state->base.active = crtc_state->base.enable =
  11181. intel_crtc->new_enabled;
  11182. if (&intel_crtc->base == crtc)
  11183. drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
  11184. }
  11185. intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
  11186. crtc->primary->fb, crtc->x, crtc->y);
  11187. ret = intel_set_mode(state);
  11188. if (ret)
  11189. drm_atomic_state_free(state);
  11190. }
  11191. #undef for_each_intel_crtc_masked
  11192. static bool intel_connector_in_mode_set(struct intel_connector *connector,
  11193. struct drm_mode_set *set)
  11194. {
  11195. int ro;
  11196. for (ro = 0; ro < set->num_connectors; ro++)
  11197. if (set->connectors[ro] == &connector->base)
  11198. return true;
  11199. return false;
  11200. }
  11201. static int
  11202. intel_modeset_stage_output_state(struct drm_device *dev,
  11203. struct drm_mode_set *set,
  11204. struct drm_atomic_state *state)
  11205. {
  11206. struct intel_connector *connector;
  11207. struct drm_connector *drm_connector;
  11208. struct drm_connector_state *connector_state;
  11209. struct drm_crtc *crtc;
  11210. struct drm_crtc_state *crtc_state;
  11211. int i, ret;
  11212. /* The upper layers ensure that we either disable a crtc or have a list
  11213. * of connectors. For paranoia, double-check this. */
  11214. WARN_ON(!set->fb && (set->num_connectors != 0));
  11215. WARN_ON(set->fb && (set->num_connectors == 0));
  11216. for_each_intel_connector(dev, connector) {
  11217. bool in_mode_set = intel_connector_in_mode_set(connector, set);
  11218. if (!in_mode_set && connector->base.state->crtc != set->crtc)
  11219. continue;
  11220. connector_state =
  11221. drm_atomic_get_connector_state(state, &connector->base);
  11222. if (IS_ERR(connector_state))
  11223. return PTR_ERR(connector_state);
  11224. if (in_mode_set) {
  11225. int pipe = to_intel_crtc(set->crtc)->pipe;
  11226. connector_state->best_encoder =
  11227. &intel_find_encoder(connector, pipe)->base;
  11228. }
  11229. if (connector->base.state->crtc != set->crtc)
  11230. continue;
  11231. /* If we disable the crtc, disable all its connectors. Also, if
  11232. * the connector is on the changing crtc but not on the new
  11233. * connector list, disable it. */
  11234. if (!set->fb || !in_mode_set) {
  11235. connector_state->best_encoder = NULL;
  11236. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  11237. connector->base.base.id,
  11238. connector->base.name);
  11239. }
  11240. }
  11241. /* connector->new_encoder is now updated for all connectors. */
  11242. for_each_connector_in_state(state, drm_connector, connector_state, i) {
  11243. connector = to_intel_connector(drm_connector);
  11244. if (!connector_state->best_encoder) {
  11245. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11246. NULL);
  11247. if (ret)
  11248. return ret;
  11249. continue;
  11250. }
  11251. if (intel_connector_in_mode_set(connector, set)) {
  11252. struct drm_crtc *crtc = connector->base.state->crtc;
  11253. /* If this connector was in a previous crtc, add it
  11254. * to the state. We might need to disable it. */
  11255. if (crtc) {
  11256. crtc_state =
  11257. drm_atomic_get_crtc_state(state, crtc);
  11258. if (IS_ERR(crtc_state))
  11259. return PTR_ERR(crtc_state);
  11260. }
  11261. ret = drm_atomic_set_crtc_for_connector(connector_state,
  11262. set->crtc);
  11263. if (ret)
  11264. return ret;
  11265. }
  11266. /* Make sure the new CRTC will work with the encoder */
  11267. if (!drm_encoder_crtc_ok(connector_state->best_encoder,
  11268. connector_state->crtc)) {
  11269. return -EINVAL;
  11270. }
  11271. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  11272. connector->base.base.id,
  11273. connector->base.name,
  11274. connector_state->crtc->base.id);
  11275. if (connector_state->best_encoder != &connector->encoder->base)
  11276. connector->encoder =
  11277. to_intel_encoder(connector_state->best_encoder);
  11278. }
  11279. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11280. bool has_connectors;
  11281. ret = drm_atomic_add_affected_connectors(state, crtc);
  11282. if (ret)
  11283. return ret;
  11284. has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
  11285. if (has_connectors != crtc_state->enable)
  11286. crtc_state->enable =
  11287. crtc_state->active = has_connectors;
  11288. }
  11289. ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
  11290. set->fb, set->x, set->y);
  11291. if (ret)
  11292. return ret;
  11293. crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
  11294. if (IS_ERR(crtc_state))
  11295. return PTR_ERR(crtc_state);
  11296. ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
  11297. if (ret)
  11298. return ret;
  11299. if (set->num_connectors)
  11300. crtc_state->active = true;
  11301. return 0;
  11302. }
  11303. static int intel_crtc_set_config(struct drm_mode_set *set)
  11304. {
  11305. struct drm_device *dev;
  11306. struct drm_atomic_state *state = NULL;
  11307. int ret;
  11308. BUG_ON(!set);
  11309. BUG_ON(!set->crtc);
  11310. BUG_ON(!set->crtc->helper_private);
  11311. /* Enforce sane interface api - has been abused by the fb helper. */
  11312. BUG_ON(!set->mode && set->fb);
  11313. BUG_ON(set->fb && set->num_connectors == 0);
  11314. if (set->fb) {
  11315. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  11316. set->crtc->base.id, set->fb->base.id,
  11317. (int)set->num_connectors, set->x, set->y);
  11318. } else {
  11319. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  11320. }
  11321. dev = set->crtc->dev;
  11322. state = drm_atomic_state_alloc(dev);
  11323. if (!state)
  11324. return -ENOMEM;
  11325. state->acquire_ctx = dev->mode_config.acquire_ctx;
  11326. ret = intel_modeset_stage_output_state(dev, set, state);
  11327. if (ret)
  11328. goto out;
  11329. ret = intel_modeset_compute_config(state);
  11330. if (ret)
  11331. goto out;
  11332. intel_update_pipe_size(to_intel_crtc(set->crtc));
  11333. ret = intel_set_mode_checked(state);
  11334. if (ret) {
  11335. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  11336. set->crtc->base.id, ret);
  11337. }
  11338. out:
  11339. if (ret)
  11340. drm_atomic_state_free(state);
  11341. return ret;
  11342. }
  11343. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11344. .gamma_set = intel_crtc_gamma_set,
  11345. .set_config = intel_crtc_set_config,
  11346. .destroy = intel_crtc_destroy,
  11347. .page_flip = intel_crtc_page_flip,
  11348. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11349. .atomic_destroy_state = intel_crtc_destroy_state,
  11350. };
  11351. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11352. struct intel_shared_dpll *pll,
  11353. struct intel_dpll_hw_state *hw_state)
  11354. {
  11355. uint32_t val;
  11356. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11357. return false;
  11358. val = I915_READ(PCH_DPLL(pll->id));
  11359. hw_state->dpll = val;
  11360. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11361. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11362. return val & DPLL_VCO_ENABLE;
  11363. }
  11364. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11365. struct intel_shared_dpll *pll)
  11366. {
  11367. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11368. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11369. }
  11370. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11371. struct intel_shared_dpll *pll)
  11372. {
  11373. /* PCH refclock must be enabled first */
  11374. ibx_assert_pch_refclk_enabled(dev_priv);
  11375. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11376. /* Wait for the clocks to stabilize. */
  11377. POSTING_READ(PCH_DPLL(pll->id));
  11378. udelay(150);
  11379. /* The pixel multiplier can only be updated once the
  11380. * DPLL is enabled and the clocks are stable.
  11381. *
  11382. * So write it again.
  11383. */
  11384. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11385. POSTING_READ(PCH_DPLL(pll->id));
  11386. udelay(200);
  11387. }
  11388. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11389. struct intel_shared_dpll *pll)
  11390. {
  11391. struct drm_device *dev = dev_priv->dev;
  11392. struct intel_crtc *crtc;
  11393. /* Make sure no transcoder isn't still depending on us. */
  11394. for_each_intel_crtc(dev, crtc) {
  11395. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11396. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11397. }
  11398. I915_WRITE(PCH_DPLL(pll->id), 0);
  11399. POSTING_READ(PCH_DPLL(pll->id));
  11400. udelay(200);
  11401. }
  11402. static char *ibx_pch_dpll_names[] = {
  11403. "PCH DPLL A",
  11404. "PCH DPLL B",
  11405. };
  11406. static void ibx_pch_dpll_init(struct drm_device *dev)
  11407. {
  11408. struct drm_i915_private *dev_priv = dev->dev_private;
  11409. int i;
  11410. dev_priv->num_shared_dpll = 2;
  11411. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11412. dev_priv->shared_dplls[i].id = i;
  11413. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11414. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11415. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11416. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11417. dev_priv->shared_dplls[i].get_hw_state =
  11418. ibx_pch_dpll_get_hw_state;
  11419. }
  11420. }
  11421. static void intel_shared_dpll_init(struct drm_device *dev)
  11422. {
  11423. struct drm_i915_private *dev_priv = dev->dev_private;
  11424. intel_update_cdclk(dev);
  11425. if (HAS_DDI(dev))
  11426. intel_ddi_pll_init(dev);
  11427. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11428. ibx_pch_dpll_init(dev);
  11429. else
  11430. dev_priv->num_shared_dpll = 0;
  11431. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11432. }
  11433. /**
  11434. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11435. * @plane: drm plane to prepare for
  11436. * @fb: framebuffer to prepare for presentation
  11437. *
  11438. * Prepares a framebuffer for usage on a display plane. Generally this
  11439. * involves pinning the underlying object and updating the frontbuffer tracking
  11440. * bits. Some older platforms need special physical address handling for
  11441. * cursor planes.
  11442. *
  11443. * Returns 0 on success, negative error code on failure.
  11444. */
  11445. int
  11446. intel_prepare_plane_fb(struct drm_plane *plane,
  11447. struct drm_framebuffer *fb,
  11448. const struct drm_plane_state *new_state)
  11449. {
  11450. struct drm_device *dev = plane->dev;
  11451. struct intel_plane *intel_plane = to_intel_plane(plane);
  11452. enum pipe pipe = intel_plane->pipe;
  11453. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11454. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
  11455. unsigned frontbuffer_bits = 0;
  11456. int ret = 0;
  11457. if (!obj)
  11458. return 0;
  11459. switch (plane->type) {
  11460. case DRM_PLANE_TYPE_PRIMARY:
  11461. frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11462. break;
  11463. case DRM_PLANE_TYPE_CURSOR:
  11464. frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
  11465. break;
  11466. case DRM_PLANE_TYPE_OVERLAY:
  11467. frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
  11468. break;
  11469. }
  11470. mutex_lock(&dev->struct_mutex);
  11471. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11472. INTEL_INFO(dev)->cursor_needs_physical) {
  11473. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11474. ret = i915_gem_object_attach_phys(obj, align);
  11475. if (ret)
  11476. DRM_DEBUG_KMS("failed to attach phys object\n");
  11477. } else {
  11478. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
  11479. }
  11480. if (ret == 0)
  11481. i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
  11482. mutex_unlock(&dev->struct_mutex);
  11483. return ret;
  11484. }
  11485. /**
  11486. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11487. * @plane: drm plane to clean up for
  11488. * @fb: old framebuffer that was on plane
  11489. *
  11490. * Cleans up a framebuffer that has just been removed from a plane.
  11491. */
  11492. void
  11493. intel_cleanup_plane_fb(struct drm_plane *plane,
  11494. struct drm_framebuffer *fb,
  11495. const struct drm_plane_state *old_state)
  11496. {
  11497. struct drm_device *dev = plane->dev;
  11498. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11499. if (WARN_ON(!obj))
  11500. return;
  11501. if (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11502. !INTEL_INFO(dev)->cursor_needs_physical) {
  11503. mutex_lock(&dev->struct_mutex);
  11504. intel_unpin_fb_obj(fb, old_state);
  11505. mutex_unlock(&dev->struct_mutex);
  11506. }
  11507. }
  11508. int
  11509. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11510. {
  11511. int max_scale;
  11512. struct drm_device *dev;
  11513. struct drm_i915_private *dev_priv;
  11514. int crtc_clock, cdclk;
  11515. if (!intel_crtc || !crtc_state)
  11516. return DRM_PLANE_HELPER_NO_SCALING;
  11517. dev = intel_crtc->base.dev;
  11518. dev_priv = dev->dev_private;
  11519. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11520. cdclk = dev_priv->display.get_display_clock_speed(dev);
  11521. if (!crtc_clock || !cdclk)
  11522. return DRM_PLANE_HELPER_NO_SCALING;
  11523. /*
  11524. * skl max scale is lower of:
  11525. * close to 3 but not 3, -1 is for that purpose
  11526. * or
  11527. * cdclk/crtc_clock
  11528. */
  11529. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11530. return max_scale;
  11531. }
  11532. static int
  11533. intel_check_primary_plane(struct drm_plane *plane,
  11534. struct intel_crtc_state *crtc_state,
  11535. struct intel_plane_state *state)
  11536. {
  11537. struct drm_crtc *crtc = state->base.crtc;
  11538. struct drm_framebuffer *fb = state->base.fb;
  11539. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11540. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11541. bool can_position = false;
  11542. /* use scaler when colorkey is not required */
  11543. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11544. to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
  11545. min_scale = 1;
  11546. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11547. can_position = true;
  11548. }
  11549. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11550. &state->dst, &state->clip,
  11551. min_scale, max_scale,
  11552. can_position, true,
  11553. &state->visible);
  11554. }
  11555. static void
  11556. intel_commit_primary_plane(struct drm_plane *plane,
  11557. struct intel_plane_state *state)
  11558. {
  11559. struct drm_crtc *crtc = state->base.crtc;
  11560. struct drm_framebuffer *fb = state->base.fb;
  11561. struct drm_device *dev = plane->dev;
  11562. struct drm_i915_private *dev_priv = dev->dev_private;
  11563. struct intel_crtc *intel_crtc;
  11564. struct drm_rect *src = &state->src;
  11565. crtc = crtc ? crtc : plane->crtc;
  11566. intel_crtc = to_intel_crtc(crtc);
  11567. plane->fb = fb;
  11568. crtc->x = src->x1 >> 16;
  11569. crtc->y = src->y1 >> 16;
  11570. if (!intel_crtc->active)
  11571. return;
  11572. if (state->visible)
  11573. /* FIXME: kill this fastboot hack */
  11574. intel_update_pipe_size(intel_crtc);
  11575. dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
  11576. }
  11577. static void
  11578. intel_disable_primary_plane(struct drm_plane *plane,
  11579. struct drm_crtc *crtc)
  11580. {
  11581. struct drm_device *dev = plane->dev;
  11582. struct drm_i915_private *dev_priv = dev->dev_private;
  11583. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11584. }
  11585. static void intel_begin_crtc_commit(struct drm_crtc *crtc)
  11586. {
  11587. struct drm_device *dev = crtc->dev;
  11588. struct drm_i915_private *dev_priv = dev->dev_private;
  11589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11590. intel_pre_plane_update(intel_crtc);
  11591. if (intel_crtc->atomic.update_wm)
  11592. intel_update_watermarks(crtc);
  11593. intel_runtime_pm_get(dev_priv);
  11594. /* Perform vblank evasion around commit operation */
  11595. if (crtc->state->active && !needs_modeset(crtc->state))
  11596. intel_crtc->atomic.evade =
  11597. intel_pipe_update_start(intel_crtc,
  11598. &intel_crtc->atomic.start_vbl_count);
  11599. if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
  11600. skl_detach_scalers(intel_crtc);
  11601. }
  11602. static void intel_finish_crtc_commit(struct drm_crtc *crtc)
  11603. {
  11604. struct drm_device *dev = crtc->dev;
  11605. struct drm_i915_private *dev_priv = dev->dev_private;
  11606. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11607. if (intel_crtc->atomic.evade)
  11608. intel_pipe_update_end(intel_crtc,
  11609. intel_crtc->atomic.start_vbl_count);
  11610. intel_runtime_pm_put(dev_priv);
  11611. intel_post_plane_update(intel_crtc);
  11612. }
  11613. /**
  11614. * intel_plane_destroy - destroy a plane
  11615. * @plane: plane to destroy
  11616. *
  11617. * Common destruction function for all types of planes (primary, cursor,
  11618. * sprite).
  11619. */
  11620. void intel_plane_destroy(struct drm_plane *plane)
  11621. {
  11622. struct intel_plane *intel_plane = to_intel_plane(plane);
  11623. drm_plane_cleanup(plane);
  11624. kfree(intel_plane);
  11625. }
  11626. const struct drm_plane_funcs intel_plane_funcs = {
  11627. .update_plane = drm_atomic_helper_update_plane,
  11628. .disable_plane = drm_atomic_helper_disable_plane,
  11629. .destroy = intel_plane_destroy,
  11630. .set_property = drm_atomic_helper_plane_set_property,
  11631. .atomic_get_property = intel_plane_atomic_get_property,
  11632. .atomic_set_property = intel_plane_atomic_set_property,
  11633. .atomic_duplicate_state = intel_plane_duplicate_state,
  11634. .atomic_destroy_state = intel_plane_destroy_state,
  11635. };
  11636. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11637. int pipe)
  11638. {
  11639. struct intel_plane *primary;
  11640. struct intel_plane_state *state;
  11641. const uint32_t *intel_primary_formats;
  11642. int num_formats;
  11643. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11644. if (primary == NULL)
  11645. return NULL;
  11646. state = intel_create_plane_state(&primary->base);
  11647. if (!state) {
  11648. kfree(primary);
  11649. return NULL;
  11650. }
  11651. primary->base.state = &state->base;
  11652. primary->can_scale = false;
  11653. primary->max_downscale = 1;
  11654. if (INTEL_INFO(dev)->gen >= 9) {
  11655. primary->can_scale = true;
  11656. state->scaler_id = -1;
  11657. }
  11658. primary->pipe = pipe;
  11659. primary->plane = pipe;
  11660. primary->check_plane = intel_check_primary_plane;
  11661. primary->commit_plane = intel_commit_primary_plane;
  11662. primary->disable_plane = intel_disable_primary_plane;
  11663. primary->ckey.flags = I915_SET_COLORKEY_NONE;
  11664. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11665. primary->plane = !pipe;
  11666. if (INTEL_INFO(dev)->gen >= 9) {
  11667. intel_primary_formats = skl_primary_formats;
  11668. num_formats = ARRAY_SIZE(skl_primary_formats);
  11669. } else if (INTEL_INFO(dev)->gen >= 4) {
  11670. intel_primary_formats = i965_primary_formats;
  11671. num_formats = ARRAY_SIZE(i965_primary_formats);
  11672. } else {
  11673. intel_primary_formats = i8xx_primary_formats;
  11674. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11675. }
  11676. drm_universal_plane_init(dev, &primary->base, 0,
  11677. &intel_plane_funcs,
  11678. intel_primary_formats, num_formats,
  11679. DRM_PLANE_TYPE_PRIMARY);
  11680. if (INTEL_INFO(dev)->gen >= 4)
  11681. intel_create_rotation_property(dev, primary);
  11682. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11683. return &primary->base;
  11684. }
  11685. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11686. {
  11687. if (!dev->mode_config.rotation_property) {
  11688. unsigned long flags = BIT(DRM_ROTATE_0) |
  11689. BIT(DRM_ROTATE_180);
  11690. if (INTEL_INFO(dev)->gen >= 9)
  11691. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11692. dev->mode_config.rotation_property =
  11693. drm_mode_create_rotation_property(dev, flags);
  11694. }
  11695. if (dev->mode_config.rotation_property)
  11696. drm_object_attach_property(&plane->base.base,
  11697. dev->mode_config.rotation_property,
  11698. plane->base.state->rotation);
  11699. }
  11700. static int
  11701. intel_check_cursor_plane(struct drm_plane *plane,
  11702. struct intel_crtc_state *crtc_state,
  11703. struct intel_plane_state *state)
  11704. {
  11705. struct drm_crtc *crtc = crtc_state->base.crtc;
  11706. struct drm_framebuffer *fb = state->base.fb;
  11707. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11708. unsigned stride;
  11709. int ret;
  11710. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11711. &state->dst, &state->clip,
  11712. DRM_PLANE_HELPER_NO_SCALING,
  11713. DRM_PLANE_HELPER_NO_SCALING,
  11714. true, true, &state->visible);
  11715. if (ret)
  11716. return ret;
  11717. /* if we want to turn off the cursor ignore width and height */
  11718. if (!obj)
  11719. return 0;
  11720. /* Check for which cursor types we support */
  11721. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11722. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11723. state->base.crtc_w, state->base.crtc_h);
  11724. return -EINVAL;
  11725. }
  11726. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11727. if (obj->base.size < stride * state->base.crtc_h) {
  11728. DRM_DEBUG_KMS("buffer is too small\n");
  11729. return -ENOMEM;
  11730. }
  11731. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11732. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11733. return -EINVAL;
  11734. }
  11735. return 0;
  11736. }
  11737. static void
  11738. intel_disable_cursor_plane(struct drm_plane *plane,
  11739. struct drm_crtc *crtc)
  11740. {
  11741. intel_crtc_update_cursor(crtc, false);
  11742. }
  11743. static void
  11744. intel_commit_cursor_plane(struct drm_plane *plane,
  11745. struct intel_plane_state *state)
  11746. {
  11747. struct drm_crtc *crtc = state->base.crtc;
  11748. struct drm_device *dev = plane->dev;
  11749. struct intel_crtc *intel_crtc;
  11750. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11751. uint32_t addr;
  11752. crtc = crtc ? crtc : plane->crtc;
  11753. intel_crtc = to_intel_crtc(crtc);
  11754. plane->fb = state->base.fb;
  11755. crtc->cursor_x = state->base.crtc_x;
  11756. crtc->cursor_y = state->base.crtc_y;
  11757. if (intel_crtc->cursor_bo == obj)
  11758. goto update;
  11759. if (!obj)
  11760. addr = 0;
  11761. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11762. addr = i915_gem_obj_ggtt_offset(obj);
  11763. else
  11764. addr = obj->phys_handle->busaddr;
  11765. intel_crtc->cursor_addr = addr;
  11766. intel_crtc->cursor_bo = obj;
  11767. update:
  11768. if (intel_crtc->active)
  11769. intel_crtc_update_cursor(crtc, state->visible);
  11770. }
  11771. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11772. int pipe)
  11773. {
  11774. struct intel_plane *cursor;
  11775. struct intel_plane_state *state;
  11776. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11777. if (cursor == NULL)
  11778. return NULL;
  11779. state = intel_create_plane_state(&cursor->base);
  11780. if (!state) {
  11781. kfree(cursor);
  11782. return NULL;
  11783. }
  11784. cursor->base.state = &state->base;
  11785. cursor->can_scale = false;
  11786. cursor->max_downscale = 1;
  11787. cursor->pipe = pipe;
  11788. cursor->plane = pipe;
  11789. cursor->check_plane = intel_check_cursor_plane;
  11790. cursor->commit_plane = intel_commit_cursor_plane;
  11791. cursor->disable_plane = intel_disable_cursor_plane;
  11792. drm_universal_plane_init(dev, &cursor->base, 0,
  11793. &intel_plane_funcs,
  11794. intel_cursor_formats,
  11795. ARRAY_SIZE(intel_cursor_formats),
  11796. DRM_PLANE_TYPE_CURSOR);
  11797. if (INTEL_INFO(dev)->gen >= 4) {
  11798. if (!dev->mode_config.rotation_property)
  11799. dev->mode_config.rotation_property =
  11800. drm_mode_create_rotation_property(dev,
  11801. BIT(DRM_ROTATE_0) |
  11802. BIT(DRM_ROTATE_180));
  11803. if (dev->mode_config.rotation_property)
  11804. drm_object_attach_property(&cursor->base.base,
  11805. dev->mode_config.rotation_property,
  11806. state->base.rotation);
  11807. }
  11808. if (INTEL_INFO(dev)->gen >=9)
  11809. state->scaler_id = -1;
  11810. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11811. return &cursor->base;
  11812. }
  11813. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11814. struct intel_crtc_state *crtc_state)
  11815. {
  11816. int i;
  11817. struct intel_scaler *intel_scaler;
  11818. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11819. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11820. intel_scaler = &scaler_state->scalers[i];
  11821. intel_scaler->in_use = 0;
  11822. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11823. }
  11824. scaler_state->scaler_id = -1;
  11825. }
  11826. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11827. {
  11828. struct drm_i915_private *dev_priv = dev->dev_private;
  11829. struct intel_crtc *intel_crtc;
  11830. struct intel_crtc_state *crtc_state = NULL;
  11831. struct drm_plane *primary = NULL;
  11832. struct drm_plane *cursor = NULL;
  11833. int i, ret;
  11834. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11835. if (intel_crtc == NULL)
  11836. return;
  11837. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11838. if (!crtc_state)
  11839. goto fail;
  11840. intel_crtc->config = crtc_state;
  11841. intel_crtc->base.state = &crtc_state->base;
  11842. crtc_state->base.crtc = &intel_crtc->base;
  11843. /* initialize shared scalers */
  11844. if (INTEL_INFO(dev)->gen >= 9) {
  11845. if (pipe == PIPE_C)
  11846. intel_crtc->num_scalers = 1;
  11847. else
  11848. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11849. skl_init_scalers(dev, intel_crtc, crtc_state);
  11850. }
  11851. primary = intel_primary_plane_create(dev, pipe);
  11852. if (!primary)
  11853. goto fail;
  11854. cursor = intel_cursor_plane_create(dev, pipe);
  11855. if (!cursor)
  11856. goto fail;
  11857. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11858. cursor, &intel_crtc_funcs);
  11859. if (ret)
  11860. goto fail;
  11861. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11862. for (i = 0; i < 256; i++) {
  11863. intel_crtc->lut_r[i] = i;
  11864. intel_crtc->lut_g[i] = i;
  11865. intel_crtc->lut_b[i] = i;
  11866. }
  11867. /*
  11868. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11869. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11870. */
  11871. intel_crtc->pipe = pipe;
  11872. intel_crtc->plane = pipe;
  11873. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11874. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11875. intel_crtc->plane = !pipe;
  11876. }
  11877. intel_crtc->cursor_base = ~0;
  11878. intel_crtc->cursor_cntl = ~0;
  11879. intel_crtc->cursor_size = ~0;
  11880. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11881. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11882. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11883. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11884. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11885. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11886. return;
  11887. fail:
  11888. if (primary)
  11889. drm_plane_cleanup(primary);
  11890. if (cursor)
  11891. drm_plane_cleanup(cursor);
  11892. kfree(crtc_state);
  11893. kfree(intel_crtc);
  11894. }
  11895. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11896. {
  11897. struct drm_encoder *encoder = connector->base.encoder;
  11898. struct drm_device *dev = connector->base.dev;
  11899. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11900. if (!encoder || WARN_ON(!encoder->crtc))
  11901. return INVALID_PIPE;
  11902. return to_intel_crtc(encoder->crtc)->pipe;
  11903. }
  11904. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11905. struct drm_file *file)
  11906. {
  11907. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11908. struct drm_crtc *drmmode_crtc;
  11909. struct intel_crtc *crtc;
  11910. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11911. if (!drmmode_crtc) {
  11912. DRM_ERROR("no such CRTC id\n");
  11913. return -ENOENT;
  11914. }
  11915. crtc = to_intel_crtc(drmmode_crtc);
  11916. pipe_from_crtc_id->pipe = crtc->pipe;
  11917. return 0;
  11918. }
  11919. static int intel_encoder_clones(struct intel_encoder *encoder)
  11920. {
  11921. struct drm_device *dev = encoder->base.dev;
  11922. struct intel_encoder *source_encoder;
  11923. int index_mask = 0;
  11924. int entry = 0;
  11925. for_each_intel_encoder(dev, source_encoder) {
  11926. if (encoders_cloneable(encoder, source_encoder))
  11927. index_mask |= (1 << entry);
  11928. entry++;
  11929. }
  11930. return index_mask;
  11931. }
  11932. static bool has_edp_a(struct drm_device *dev)
  11933. {
  11934. struct drm_i915_private *dev_priv = dev->dev_private;
  11935. if (!IS_MOBILE(dev))
  11936. return false;
  11937. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11938. return false;
  11939. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11940. return false;
  11941. return true;
  11942. }
  11943. static bool intel_crt_present(struct drm_device *dev)
  11944. {
  11945. struct drm_i915_private *dev_priv = dev->dev_private;
  11946. if (INTEL_INFO(dev)->gen >= 9)
  11947. return false;
  11948. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11949. return false;
  11950. if (IS_CHERRYVIEW(dev))
  11951. return false;
  11952. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11953. return false;
  11954. return true;
  11955. }
  11956. static void intel_setup_outputs(struct drm_device *dev)
  11957. {
  11958. struct drm_i915_private *dev_priv = dev->dev_private;
  11959. struct intel_encoder *encoder;
  11960. bool dpd_is_edp = false;
  11961. intel_lvds_init(dev);
  11962. if (intel_crt_present(dev))
  11963. intel_crt_init(dev);
  11964. if (IS_BROXTON(dev)) {
  11965. /*
  11966. * FIXME: Broxton doesn't support port detection via the
  11967. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11968. * detect the ports.
  11969. */
  11970. intel_ddi_init(dev, PORT_A);
  11971. intel_ddi_init(dev, PORT_B);
  11972. intel_ddi_init(dev, PORT_C);
  11973. } else if (HAS_DDI(dev)) {
  11974. int found;
  11975. /*
  11976. * Haswell uses DDI functions to detect digital outputs.
  11977. * On SKL pre-D0 the strap isn't connected, so we assume
  11978. * it's there.
  11979. */
  11980. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  11981. /* WaIgnoreDDIAStrap: skl */
  11982. if (found ||
  11983. (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
  11984. intel_ddi_init(dev, PORT_A);
  11985. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11986. * register */
  11987. found = I915_READ(SFUSE_STRAP);
  11988. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11989. intel_ddi_init(dev, PORT_B);
  11990. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11991. intel_ddi_init(dev, PORT_C);
  11992. if (found & SFUSE_STRAP_DDID_DETECTED)
  11993. intel_ddi_init(dev, PORT_D);
  11994. } else if (HAS_PCH_SPLIT(dev)) {
  11995. int found;
  11996. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11997. if (has_edp_a(dev))
  11998. intel_dp_init(dev, DP_A, PORT_A);
  11999. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12000. /* PCH SDVOB multiplex with HDMIB */
  12001. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  12002. if (!found)
  12003. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12004. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12005. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12006. }
  12007. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12008. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12009. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12010. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12011. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12012. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12013. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12014. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12015. } else if (IS_VALLEYVIEW(dev)) {
  12016. /*
  12017. * The DP_DETECTED bit is the latched state of the DDC
  12018. * SDA pin at boot. However since eDP doesn't require DDC
  12019. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  12020. * eDP ports may have been muxed to an alternate function.
  12021. * Thus we can't rely on the DP_DETECTED bit alone to detect
  12022. * eDP ports. Consult the VBT as well as DP_DETECTED to
  12023. * detect eDP ports.
  12024. */
  12025. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
  12026. !intel_dp_is_edp(dev, PORT_B))
  12027. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  12028. PORT_B);
  12029. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
  12030. intel_dp_is_edp(dev, PORT_B))
  12031. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  12032. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
  12033. !intel_dp_is_edp(dev, PORT_C))
  12034. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  12035. PORT_C);
  12036. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
  12037. intel_dp_is_edp(dev, PORT_C))
  12038. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  12039. if (IS_CHERRYVIEW(dev)) {
  12040. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
  12041. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  12042. PORT_D);
  12043. /* eDP not supported on port D, so don't check VBT */
  12044. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  12045. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  12046. }
  12047. intel_dsi_init(dev);
  12048. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  12049. bool found = false;
  12050. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12051. DRM_DEBUG_KMS("probing SDVOB\n");
  12052. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  12053. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  12054. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  12055. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  12056. }
  12057. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  12058. intel_dp_init(dev, DP_B, PORT_B);
  12059. }
  12060. /* Before G4X SDVOC doesn't have its own detect register */
  12061. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  12062. DRM_DEBUG_KMS("probing SDVOC\n");
  12063. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  12064. }
  12065. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  12066. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  12067. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  12068. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  12069. }
  12070. if (SUPPORTS_INTEGRATED_DP(dev))
  12071. intel_dp_init(dev, DP_C, PORT_C);
  12072. }
  12073. if (SUPPORTS_INTEGRATED_DP(dev) &&
  12074. (I915_READ(DP_D) & DP_DETECTED))
  12075. intel_dp_init(dev, DP_D, PORT_D);
  12076. } else if (IS_GEN2(dev))
  12077. intel_dvo_init(dev);
  12078. if (SUPPORTS_TV(dev))
  12079. intel_tv_init(dev);
  12080. intel_psr_init(dev);
  12081. for_each_intel_encoder(dev, encoder) {
  12082. encoder->base.possible_crtcs = encoder->crtc_mask;
  12083. encoder->base.possible_clones =
  12084. intel_encoder_clones(encoder);
  12085. }
  12086. intel_init_pch_refclk(dev);
  12087. drm_helper_move_panel_connectors_to_head(dev);
  12088. }
  12089. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12090. {
  12091. struct drm_device *dev = fb->dev;
  12092. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12093. drm_framebuffer_cleanup(fb);
  12094. mutex_lock(&dev->struct_mutex);
  12095. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12096. drm_gem_object_unreference(&intel_fb->obj->base);
  12097. mutex_unlock(&dev->struct_mutex);
  12098. kfree(intel_fb);
  12099. }
  12100. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12101. struct drm_file *file,
  12102. unsigned int *handle)
  12103. {
  12104. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12105. struct drm_i915_gem_object *obj = intel_fb->obj;
  12106. return drm_gem_handle_create(file, &obj->base, handle);
  12107. }
  12108. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12109. .destroy = intel_user_framebuffer_destroy,
  12110. .create_handle = intel_user_framebuffer_create_handle,
  12111. };
  12112. static
  12113. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12114. uint32_t pixel_format)
  12115. {
  12116. u32 gen = INTEL_INFO(dev)->gen;
  12117. if (gen >= 9) {
  12118. /* "The stride in bytes must not exceed the of the size of 8K
  12119. * pixels and 32K bytes."
  12120. */
  12121. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12122. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12123. return 32*1024;
  12124. } else if (gen >= 4) {
  12125. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12126. return 16*1024;
  12127. else
  12128. return 32*1024;
  12129. } else if (gen >= 3) {
  12130. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12131. return 8*1024;
  12132. else
  12133. return 16*1024;
  12134. } else {
  12135. /* XXX DSPC is limited to 4k tiled */
  12136. return 8*1024;
  12137. }
  12138. }
  12139. static int intel_framebuffer_init(struct drm_device *dev,
  12140. struct intel_framebuffer *intel_fb,
  12141. struct drm_mode_fb_cmd2 *mode_cmd,
  12142. struct drm_i915_gem_object *obj)
  12143. {
  12144. unsigned int aligned_height;
  12145. int ret;
  12146. u32 pitch_limit, stride_alignment;
  12147. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12148. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12149. /* Enforce that fb modifier and tiling mode match, but only for
  12150. * X-tiled. This is needed for FBC. */
  12151. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12152. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12153. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12154. return -EINVAL;
  12155. }
  12156. } else {
  12157. if (obj->tiling_mode == I915_TILING_X)
  12158. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12159. else if (obj->tiling_mode == I915_TILING_Y) {
  12160. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12161. return -EINVAL;
  12162. }
  12163. }
  12164. /* Passed in modifier sanity checking. */
  12165. switch (mode_cmd->modifier[0]) {
  12166. case I915_FORMAT_MOD_Y_TILED:
  12167. case I915_FORMAT_MOD_Yf_TILED:
  12168. if (INTEL_INFO(dev)->gen < 9) {
  12169. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12170. mode_cmd->modifier[0]);
  12171. return -EINVAL;
  12172. }
  12173. case DRM_FORMAT_MOD_NONE:
  12174. case I915_FORMAT_MOD_X_TILED:
  12175. break;
  12176. default:
  12177. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12178. mode_cmd->modifier[0]);
  12179. return -EINVAL;
  12180. }
  12181. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12182. mode_cmd->pixel_format);
  12183. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12184. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12185. mode_cmd->pitches[0], stride_alignment);
  12186. return -EINVAL;
  12187. }
  12188. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12189. mode_cmd->pixel_format);
  12190. if (mode_cmd->pitches[0] > pitch_limit) {
  12191. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12192. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12193. "tiled" : "linear",
  12194. mode_cmd->pitches[0], pitch_limit);
  12195. return -EINVAL;
  12196. }
  12197. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12198. mode_cmd->pitches[0] != obj->stride) {
  12199. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12200. mode_cmd->pitches[0], obj->stride);
  12201. return -EINVAL;
  12202. }
  12203. /* Reject formats not supported by any plane early. */
  12204. switch (mode_cmd->pixel_format) {
  12205. case DRM_FORMAT_C8:
  12206. case DRM_FORMAT_RGB565:
  12207. case DRM_FORMAT_XRGB8888:
  12208. case DRM_FORMAT_ARGB8888:
  12209. break;
  12210. case DRM_FORMAT_XRGB1555:
  12211. if (INTEL_INFO(dev)->gen > 3) {
  12212. DRM_DEBUG("unsupported pixel format: %s\n",
  12213. drm_get_format_name(mode_cmd->pixel_format));
  12214. return -EINVAL;
  12215. }
  12216. break;
  12217. case DRM_FORMAT_ABGR8888:
  12218. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12219. DRM_DEBUG("unsupported pixel format: %s\n",
  12220. drm_get_format_name(mode_cmd->pixel_format));
  12221. return -EINVAL;
  12222. }
  12223. break;
  12224. case DRM_FORMAT_XBGR8888:
  12225. case DRM_FORMAT_XRGB2101010:
  12226. case DRM_FORMAT_XBGR2101010:
  12227. if (INTEL_INFO(dev)->gen < 4) {
  12228. DRM_DEBUG("unsupported pixel format: %s\n",
  12229. drm_get_format_name(mode_cmd->pixel_format));
  12230. return -EINVAL;
  12231. }
  12232. break;
  12233. case DRM_FORMAT_ABGR2101010:
  12234. if (!IS_VALLEYVIEW(dev)) {
  12235. DRM_DEBUG("unsupported pixel format: %s\n",
  12236. drm_get_format_name(mode_cmd->pixel_format));
  12237. return -EINVAL;
  12238. }
  12239. break;
  12240. case DRM_FORMAT_YUYV:
  12241. case DRM_FORMAT_UYVY:
  12242. case DRM_FORMAT_YVYU:
  12243. case DRM_FORMAT_VYUY:
  12244. if (INTEL_INFO(dev)->gen < 5) {
  12245. DRM_DEBUG("unsupported pixel format: %s\n",
  12246. drm_get_format_name(mode_cmd->pixel_format));
  12247. return -EINVAL;
  12248. }
  12249. break;
  12250. default:
  12251. DRM_DEBUG("unsupported pixel format: %s\n",
  12252. drm_get_format_name(mode_cmd->pixel_format));
  12253. return -EINVAL;
  12254. }
  12255. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12256. if (mode_cmd->offsets[0] != 0)
  12257. return -EINVAL;
  12258. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12259. mode_cmd->pixel_format,
  12260. mode_cmd->modifier[0]);
  12261. /* FIXME drm helper for size checks (especially planar formats)? */
  12262. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12263. return -EINVAL;
  12264. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12265. intel_fb->obj = obj;
  12266. intel_fb->obj->framebuffer_references++;
  12267. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12268. if (ret) {
  12269. DRM_ERROR("framebuffer init failed %d\n", ret);
  12270. return ret;
  12271. }
  12272. return 0;
  12273. }
  12274. static struct drm_framebuffer *
  12275. intel_user_framebuffer_create(struct drm_device *dev,
  12276. struct drm_file *filp,
  12277. struct drm_mode_fb_cmd2 *mode_cmd)
  12278. {
  12279. struct drm_i915_gem_object *obj;
  12280. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12281. mode_cmd->handles[0]));
  12282. if (&obj->base == NULL)
  12283. return ERR_PTR(-ENOENT);
  12284. return intel_framebuffer_create(dev, mode_cmd, obj);
  12285. }
  12286. #ifndef CONFIG_DRM_I915_FBDEV
  12287. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12288. {
  12289. }
  12290. #endif
  12291. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12292. .fb_create = intel_user_framebuffer_create,
  12293. .output_poll_changed = intel_fbdev_output_poll_changed,
  12294. .atomic_check = intel_atomic_check,
  12295. .atomic_commit = intel_atomic_commit,
  12296. .atomic_state_alloc = intel_atomic_state_alloc,
  12297. .atomic_state_clear = intel_atomic_state_clear,
  12298. };
  12299. /* Set up chip specific display functions */
  12300. static void intel_init_display(struct drm_device *dev)
  12301. {
  12302. struct drm_i915_private *dev_priv = dev->dev_private;
  12303. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12304. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12305. else if (IS_CHERRYVIEW(dev))
  12306. dev_priv->display.find_dpll = chv_find_best_dpll;
  12307. else if (IS_VALLEYVIEW(dev))
  12308. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12309. else if (IS_PINEVIEW(dev))
  12310. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12311. else
  12312. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12313. if (INTEL_INFO(dev)->gen >= 9) {
  12314. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12315. dev_priv->display.get_initial_plane_config =
  12316. skylake_get_initial_plane_config;
  12317. dev_priv->display.crtc_compute_clock =
  12318. haswell_crtc_compute_clock;
  12319. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12320. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12321. dev_priv->display.update_primary_plane =
  12322. skylake_update_primary_plane;
  12323. } else if (HAS_DDI(dev)) {
  12324. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12325. dev_priv->display.get_initial_plane_config =
  12326. ironlake_get_initial_plane_config;
  12327. dev_priv->display.crtc_compute_clock =
  12328. haswell_crtc_compute_clock;
  12329. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12330. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12331. dev_priv->display.update_primary_plane =
  12332. ironlake_update_primary_plane;
  12333. } else if (HAS_PCH_SPLIT(dev)) {
  12334. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12335. dev_priv->display.get_initial_plane_config =
  12336. ironlake_get_initial_plane_config;
  12337. dev_priv->display.crtc_compute_clock =
  12338. ironlake_crtc_compute_clock;
  12339. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12340. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12341. dev_priv->display.update_primary_plane =
  12342. ironlake_update_primary_plane;
  12343. } else if (IS_VALLEYVIEW(dev)) {
  12344. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12345. dev_priv->display.get_initial_plane_config =
  12346. i9xx_get_initial_plane_config;
  12347. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12348. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12349. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12350. dev_priv->display.update_primary_plane =
  12351. i9xx_update_primary_plane;
  12352. } else {
  12353. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12354. dev_priv->display.get_initial_plane_config =
  12355. i9xx_get_initial_plane_config;
  12356. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12357. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12358. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12359. dev_priv->display.update_primary_plane =
  12360. i9xx_update_primary_plane;
  12361. }
  12362. /* Returns the core display clock speed */
  12363. if (IS_SKYLAKE(dev))
  12364. dev_priv->display.get_display_clock_speed =
  12365. skylake_get_display_clock_speed;
  12366. else if (IS_BROADWELL(dev))
  12367. dev_priv->display.get_display_clock_speed =
  12368. broadwell_get_display_clock_speed;
  12369. else if (IS_HASWELL(dev))
  12370. dev_priv->display.get_display_clock_speed =
  12371. haswell_get_display_clock_speed;
  12372. else if (IS_VALLEYVIEW(dev))
  12373. dev_priv->display.get_display_clock_speed =
  12374. valleyview_get_display_clock_speed;
  12375. else if (IS_GEN5(dev))
  12376. dev_priv->display.get_display_clock_speed =
  12377. ilk_get_display_clock_speed;
  12378. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12379. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12380. dev_priv->display.get_display_clock_speed =
  12381. i945_get_display_clock_speed;
  12382. else if (IS_GM45(dev))
  12383. dev_priv->display.get_display_clock_speed =
  12384. gm45_get_display_clock_speed;
  12385. else if (IS_CRESTLINE(dev))
  12386. dev_priv->display.get_display_clock_speed =
  12387. i965gm_get_display_clock_speed;
  12388. else if (IS_PINEVIEW(dev))
  12389. dev_priv->display.get_display_clock_speed =
  12390. pnv_get_display_clock_speed;
  12391. else if (IS_G33(dev) || IS_G4X(dev))
  12392. dev_priv->display.get_display_clock_speed =
  12393. g33_get_display_clock_speed;
  12394. else if (IS_I915G(dev))
  12395. dev_priv->display.get_display_clock_speed =
  12396. i915_get_display_clock_speed;
  12397. else if (IS_I945GM(dev) || IS_845G(dev))
  12398. dev_priv->display.get_display_clock_speed =
  12399. i9xx_misc_get_display_clock_speed;
  12400. else if (IS_PINEVIEW(dev))
  12401. dev_priv->display.get_display_clock_speed =
  12402. pnv_get_display_clock_speed;
  12403. else if (IS_I915GM(dev))
  12404. dev_priv->display.get_display_clock_speed =
  12405. i915gm_get_display_clock_speed;
  12406. else if (IS_I865G(dev))
  12407. dev_priv->display.get_display_clock_speed =
  12408. i865_get_display_clock_speed;
  12409. else if (IS_I85X(dev))
  12410. dev_priv->display.get_display_clock_speed =
  12411. i85x_get_display_clock_speed;
  12412. else { /* 830 */
  12413. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12414. dev_priv->display.get_display_clock_speed =
  12415. i830_get_display_clock_speed;
  12416. }
  12417. if (IS_GEN5(dev)) {
  12418. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12419. } else if (IS_GEN6(dev)) {
  12420. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12421. } else if (IS_IVYBRIDGE(dev)) {
  12422. /* FIXME: detect B0+ stepping and use auto training */
  12423. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12424. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12425. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12426. if (IS_BROADWELL(dev))
  12427. dev_priv->display.modeset_global_resources =
  12428. broadwell_modeset_global_resources;
  12429. } else if (IS_VALLEYVIEW(dev)) {
  12430. dev_priv->display.modeset_global_resources =
  12431. valleyview_modeset_global_resources;
  12432. } else if (IS_BROXTON(dev)) {
  12433. dev_priv->display.modeset_global_resources =
  12434. broxton_modeset_global_resources;
  12435. }
  12436. switch (INTEL_INFO(dev)->gen) {
  12437. case 2:
  12438. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12439. break;
  12440. case 3:
  12441. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12442. break;
  12443. case 4:
  12444. case 5:
  12445. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12446. break;
  12447. case 6:
  12448. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12449. break;
  12450. case 7:
  12451. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12452. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12453. break;
  12454. case 9:
  12455. /* Drop through - unsupported since execlist only. */
  12456. default:
  12457. /* Default just returns -ENODEV to indicate unsupported */
  12458. dev_priv->display.queue_flip = intel_default_queue_flip;
  12459. }
  12460. intel_panel_init_backlight_funcs(dev);
  12461. mutex_init(&dev_priv->pps_mutex);
  12462. }
  12463. /*
  12464. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12465. * resume, or other times. This quirk makes sure that's the case for
  12466. * affected systems.
  12467. */
  12468. static void quirk_pipea_force(struct drm_device *dev)
  12469. {
  12470. struct drm_i915_private *dev_priv = dev->dev_private;
  12471. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12472. DRM_INFO("applying pipe a force quirk\n");
  12473. }
  12474. static void quirk_pipeb_force(struct drm_device *dev)
  12475. {
  12476. struct drm_i915_private *dev_priv = dev->dev_private;
  12477. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12478. DRM_INFO("applying pipe b force quirk\n");
  12479. }
  12480. /*
  12481. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12482. */
  12483. static void quirk_ssc_force_disable(struct drm_device *dev)
  12484. {
  12485. struct drm_i915_private *dev_priv = dev->dev_private;
  12486. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12487. DRM_INFO("applying lvds SSC disable quirk\n");
  12488. }
  12489. /*
  12490. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12491. * brightness value
  12492. */
  12493. static void quirk_invert_brightness(struct drm_device *dev)
  12494. {
  12495. struct drm_i915_private *dev_priv = dev->dev_private;
  12496. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12497. DRM_INFO("applying inverted panel brightness quirk\n");
  12498. }
  12499. /* Some VBT's incorrectly indicate no backlight is present */
  12500. static void quirk_backlight_present(struct drm_device *dev)
  12501. {
  12502. struct drm_i915_private *dev_priv = dev->dev_private;
  12503. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12504. DRM_INFO("applying backlight present quirk\n");
  12505. }
  12506. struct intel_quirk {
  12507. int device;
  12508. int subsystem_vendor;
  12509. int subsystem_device;
  12510. void (*hook)(struct drm_device *dev);
  12511. };
  12512. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12513. struct intel_dmi_quirk {
  12514. void (*hook)(struct drm_device *dev);
  12515. const struct dmi_system_id (*dmi_id_list)[];
  12516. };
  12517. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12518. {
  12519. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12520. return 1;
  12521. }
  12522. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12523. {
  12524. .dmi_id_list = &(const struct dmi_system_id[]) {
  12525. {
  12526. .callback = intel_dmi_reverse_brightness,
  12527. .ident = "NCR Corporation",
  12528. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12529. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12530. },
  12531. },
  12532. { } /* terminating entry */
  12533. },
  12534. .hook = quirk_invert_brightness,
  12535. },
  12536. };
  12537. static struct intel_quirk intel_quirks[] = {
  12538. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12539. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12540. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12541. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12542. /* 830 needs to leave pipe A & dpll A up */
  12543. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12544. /* 830 needs to leave pipe B & dpll B up */
  12545. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12546. /* Lenovo U160 cannot use SSC on LVDS */
  12547. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12548. /* Sony Vaio Y cannot use SSC on LVDS */
  12549. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12550. /* Acer Aspire 5734Z must invert backlight brightness */
  12551. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12552. /* Acer/eMachines G725 */
  12553. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12554. /* Acer/eMachines e725 */
  12555. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12556. /* Acer/Packard Bell NCL20 */
  12557. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12558. /* Acer Aspire 4736Z */
  12559. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12560. /* Acer Aspire 5336 */
  12561. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12562. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12563. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12564. /* Acer C720 Chromebook (Core i3 4005U) */
  12565. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12566. /* Apple Macbook 2,1 (Core 2 T7400) */
  12567. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12568. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12569. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12570. /* HP Chromebook 14 (Celeron 2955U) */
  12571. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12572. /* Dell Chromebook 11 */
  12573. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12574. };
  12575. static void intel_init_quirks(struct drm_device *dev)
  12576. {
  12577. struct pci_dev *d = dev->pdev;
  12578. int i;
  12579. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12580. struct intel_quirk *q = &intel_quirks[i];
  12581. if (d->device == q->device &&
  12582. (d->subsystem_vendor == q->subsystem_vendor ||
  12583. q->subsystem_vendor == PCI_ANY_ID) &&
  12584. (d->subsystem_device == q->subsystem_device ||
  12585. q->subsystem_device == PCI_ANY_ID))
  12586. q->hook(dev);
  12587. }
  12588. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12589. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12590. intel_dmi_quirks[i].hook(dev);
  12591. }
  12592. }
  12593. /* Disable the VGA plane that we never use */
  12594. static void i915_disable_vga(struct drm_device *dev)
  12595. {
  12596. struct drm_i915_private *dev_priv = dev->dev_private;
  12597. u8 sr1;
  12598. u32 vga_reg = i915_vgacntrl_reg(dev);
  12599. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12600. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12601. outb(SR01, VGA_SR_INDEX);
  12602. sr1 = inb(VGA_SR_DATA);
  12603. outb(sr1 | 1<<5, VGA_SR_DATA);
  12604. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12605. udelay(300);
  12606. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12607. POSTING_READ(vga_reg);
  12608. }
  12609. void intel_modeset_init_hw(struct drm_device *dev)
  12610. {
  12611. intel_update_cdclk(dev);
  12612. intel_prepare_ddi(dev);
  12613. intel_init_clock_gating(dev);
  12614. intel_enable_gt_powersave(dev);
  12615. }
  12616. void intel_modeset_init(struct drm_device *dev)
  12617. {
  12618. struct drm_i915_private *dev_priv = dev->dev_private;
  12619. int sprite, ret;
  12620. enum pipe pipe;
  12621. struct intel_crtc *crtc;
  12622. drm_mode_config_init(dev);
  12623. dev->mode_config.min_width = 0;
  12624. dev->mode_config.min_height = 0;
  12625. dev->mode_config.preferred_depth = 24;
  12626. dev->mode_config.prefer_shadow = 1;
  12627. dev->mode_config.allow_fb_modifiers = true;
  12628. dev->mode_config.funcs = &intel_mode_funcs;
  12629. intel_init_quirks(dev);
  12630. intel_init_pm(dev);
  12631. if (INTEL_INFO(dev)->num_pipes == 0)
  12632. return;
  12633. intel_init_display(dev);
  12634. intel_init_audio(dev);
  12635. if (IS_GEN2(dev)) {
  12636. dev->mode_config.max_width = 2048;
  12637. dev->mode_config.max_height = 2048;
  12638. } else if (IS_GEN3(dev)) {
  12639. dev->mode_config.max_width = 4096;
  12640. dev->mode_config.max_height = 4096;
  12641. } else {
  12642. dev->mode_config.max_width = 8192;
  12643. dev->mode_config.max_height = 8192;
  12644. }
  12645. if (IS_845G(dev) || IS_I865G(dev)) {
  12646. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12647. dev->mode_config.cursor_height = 1023;
  12648. } else if (IS_GEN2(dev)) {
  12649. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12650. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12651. } else {
  12652. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12653. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12654. }
  12655. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12656. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12657. INTEL_INFO(dev)->num_pipes,
  12658. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12659. for_each_pipe(dev_priv, pipe) {
  12660. intel_crtc_init(dev, pipe);
  12661. for_each_sprite(dev_priv, pipe, sprite) {
  12662. ret = intel_plane_init(dev, pipe, sprite);
  12663. if (ret)
  12664. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12665. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12666. }
  12667. }
  12668. intel_init_dpio(dev);
  12669. intel_shared_dpll_init(dev);
  12670. /* Just disable it once at startup */
  12671. i915_disable_vga(dev);
  12672. intel_setup_outputs(dev);
  12673. /* Just in case the BIOS is doing something questionable. */
  12674. intel_fbc_disable(dev);
  12675. drm_modeset_lock_all(dev);
  12676. intel_modeset_setup_hw_state(dev, false);
  12677. drm_modeset_unlock_all(dev);
  12678. for_each_intel_crtc(dev, crtc) {
  12679. if (!crtc->active)
  12680. continue;
  12681. /*
  12682. * Note that reserving the BIOS fb up front prevents us
  12683. * from stuffing other stolen allocations like the ring
  12684. * on top. This prevents some ugliness at boot time, and
  12685. * can even allow for smooth boot transitions if the BIOS
  12686. * fb is large enough for the active pipe configuration.
  12687. */
  12688. if (dev_priv->display.get_initial_plane_config) {
  12689. dev_priv->display.get_initial_plane_config(crtc,
  12690. &crtc->plane_config);
  12691. /*
  12692. * If the fb is shared between multiple heads, we'll
  12693. * just get the first one.
  12694. */
  12695. intel_find_initial_plane_obj(crtc, &crtc->plane_config);
  12696. }
  12697. }
  12698. }
  12699. static void intel_enable_pipe_a(struct drm_device *dev)
  12700. {
  12701. struct intel_connector *connector;
  12702. struct drm_connector *crt = NULL;
  12703. struct intel_load_detect_pipe load_detect_temp;
  12704. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12705. /* We can't just switch on the pipe A, we need to set things up with a
  12706. * proper mode and output configuration. As a gross hack, enable pipe A
  12707. * by enabling the load detect pipe once. */
  12708. for_each_intel_connector(dev, connector) {
  12709. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12710. crt = &connector->base;
  12711. break;
  12712. }
  12713. }
  12714. if (!crt)
  12715. return;
  12716. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12717. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12718. }
  12719. static bool
  12720. intel_check_plane_mapping(struct intel_crtc *crtc)
  12721. {
  12722. struct drm_device *dev = crtc->base.dev;
  12723. struct drm_i915_private *dev_priv = dev->dev_private;
  12724. u32 reg, val;
  12725. if (INTEL_INFO(dev)->num_pipes == 1)
  12726. return true;
  12727. reg = DSPCNTR(!crtc->plane);
  12728. val = I915_READ(reg);
  12729. if ((val & DISPLAY_PLANE_ENABLE) &&
  12730. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12731. return false;
  12732. return true;
  12733. }
  12734. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12735. {
  12736. struct drm_device *dev = crtc->base.dev;
  12737. struct drm_i915_private *dev_priv = dev->dev_private;
  12738. struct intel_encoder *encoder;
  12739. u32 reg;
  12740. bool enable;
  12741. /* Clear any frame start delays used for debugging left by the BIOS */
  12742. reg = PIPECONF(crtc->config->cpu_transcoder);
  12743. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12744. /* restore vblank interrupts to correct state */
  12745. drm_crtc_vblank_reset(&crtc->base);
  12746. if (crtc->active) {
  12747. update_scanline_offset(crtc);
  12748. drm_crtc_vblank_on(&crtc->base);
  12749. }
  12750. /* We need to sanitize the plane -> pipe mapping first because this will
  12751. * disable the crtc (and hence change the state) if it is wrong. Note
  12752. * that gen4+ has a fixed plane -> pipe mapping. */
  12753. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12754. bool plane;
  12755. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12756. crtc->base.base.id);
  12757. /* Pipe has the wrong plane attached and the plane is active.
  12758. * Temporarily change the plane mapping and disable everything
  12759. * ... */
  12760. plane = crtc->plane;
  12761. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12762. crtc->plane = !plane;
  12763. intel_crtc_disable_noatomic(&crtc->base);
  12764. crtc->plane = plane;
  12765. }
  12766. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12767. crtc->pipe == PIPE_A && !crtc->active) {
  12768. /* BIOS forgot to enable pipe A, this mostly happens after
  12769. * resume. Force-enable the pipe to fix this, the update_dpms
  12770. * call below we restore the pipe to the right state, but leave
  12771. * the required bits on. */
  12772. intel_enable_pipe_a(dev);
  12773. }
  12774. /* Adjust the state of the output pipe according to whether we
  12775. * have active connectors/encoders. */
  12776. enable = false;
  12777. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12778. enable |= encoder->connectors_active;
  12779. if (!enable)
  12780. intel_crtc_disable_noatomic(&crtc->base);
  12781. if (crtc->active != crtc->base.state->active) {
  12782. /* This can happen either due to bugs in the get_hw_state
  12783. * functions or because of calls to intel_crtc_disable_noatomic,
  12784. * or because the pipe is force-enabled due to the
  12785. * pipe A quirk. */
  12786. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12787. crtc->base.base.id,
  12788. crtc->base.state->enable ? "enabled" : "disabled",
  12789. crtc->active ? "enabled" : "disabled");
  12790. crtc->base.state->enable = crtc->active;
  12791. crtc->base.state->active = crtc->active;
  12792. crtc->base.enabled = crtc->active;
  12793. /* Because we only establish the connector -> encoder ->
  12794. * crtc links if something is active, this means the
  12795. * crtc is now deactivated. Break the links. connector
  12796. * -> encoder links are only establish when things are
  12797. * actually up, hence no need to break them. */
  12798. WARN_ON(crtc->active);
  12799. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  12800. WARN_ON(encoder->connectors_active);
  12801. encoder->base.crtc = NULL;
  12802. }
  12803. }
  12804. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12805. /*
  12806. * We start out with underrun reporting disabled to avoid races.
  12807. * For correct bookkeeping mark this on active crtcs.
  12808. *
  12809. * Also on gmch platforms we dont have any hardware bits to
  12810. * disable the underrun reporting. Which means we need to start
  12811. * out with underrun reporting disabled also on inactive pipes,
  12812. * since otherwise we'll complain about the garbage we read when
  12813. * e.g. coming up after runtime pm.
  12814. *
  12815. * No protection against concurrent access is required - at
  12816. * worst a fifo underrun happens which also sets this to false.
  12817. */
  12818. crtc->cpu_fifo_underrun_disabled = true;
  12819. crtc->pch_fifo_underrun_disabled = true;
  12820. }
  12821. }
  12822. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12823. {
  12824. struct intel_connector *connector;
  12825. struct drm_device *dev = encoder->base.dev;
  12826. /* We need to check both for a crtc link (meaning that the
  12827. * encoder is active and trying to read from a pipe) and the
  12828. * pipe itself being active. */
  12829. bool has_active_crtc = encoder->base.crtc &&
  12830. to_intel_crtc(encoder->base.crtc)->active;
  12831. if (encoder->connectors_active && !has_active_crtc) {
  12832. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12833. encoder->base.base.id,
  12834. encoder->base.name);
  12835. /* Connector is active, but has no active pipe. This is
  12836. * fallout from our resume register restoring. Disable
  12837. * the encoder manually again. */
  12838. if (encoder->base.crtc) {
  12839. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12840. encoder->base.base.id,
  12841. encoder->base.name);
  12842. encoder->disable(encoder);
  12843. if (encoder->post_disable)
  12844. encoder->post_disable(encoder);
  12845. }
  12846. encoder->base.crtc = NULL;
  12847. encoder->connectors_active = false;
  12848. /* Inconsistent output/port/pipe state happens presumably due to
  12849. * a bug in one of the get_hw_state functions. Or someplace else
  12850. * in our code, like the register restore mess on resume. Clamp
  12851. * things to off as a safer default. */
  12852. for_each_intel_connector(dev, connector) {
  12853. if (connector->encoder != encoder)
  12854. continue;
  12855. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12856. connector->base.encoder = NULL;
  12857. }
  12858. }
  12859. /* Enabled encoders without active connectors will be fixed in
  12860. * the crtc fixup. */
  12861. }
  12862. void i915_redisable_vga_power_on(struct drm_device *dev)
  12863. {
  12864. struct drm_i915_private *dev_priv = dev->dev_private;
  12865. u32 vga_reg = i915_vgacntrl_reg(dev);
  12866. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12867. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12868. i915_disable_vga(dev);
  12869. }
  12870. }
  12871. void i915_redisable_vga(struct drm_device *dev)
  12872. {
  12873. struct drm_i915_private *dev_priv = dev->dev_private;
  12874. /* This function can be called both from intel_modeset_setup_hw_state or
  12875. * at a very early point in our resume sequence, where the power well
  12876. * structures are not yet restored. Since this function is at a very
  12877. * paranoid "someone might have enabled VGA while we were not looking"
  12878. * level, just check if the power well is enabled instead of trying to
  12879. * follow the "don't touch the power well if we don't need it" policy
  12880. * the rest of the driver uses. */
  12881. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12882. return;
  12883. i915_redisable_vga_power_on(dev);
  12884. }
  12885. static bool primary_get_hw_state(struct intel_crtc *crtc)
  12886. {
  12887. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  12888. if (!crtc->active)
  12889. return false;
  12890. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  12891. }
  12892. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12893. {
  12894. struct drm_i915_private *dev_priv = dev->dev_private;
  12895. enum pipe pipe;
  12896. struct intel_crtc *crtc;
  12897. struct intel_encoder *encoder;
  12898. struct intel_connector *connector;
  12899. int i;
  12900. for_each_intel_crtc(dev, crtc) {
  12901. struct drm_plane *primary = crtc->base.primary;
  12902. struct intel_plane_state *plane_state;
  12903. memset(crtc->config, 0, sizeof(*crtc->config));
  12904. crtc->config->base.crtc = &crtc->base;
  12905. crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  12906. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12907. crtc->config);
  12908. crtc->base.state->enable = crtc->active;
  12909. crtc->base.state->active = crtc->active;
  12910. crtc->base.enabled = crtc->active;
  12911. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12912. plane_state = to_intel_plane_state(primary->state);
  12913. plane_state->visible = primary_get_hw_state(crtc);
  12914. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12915. crtc->base.base.id,
  12916. crtc->active ? "enabled" : "disabled");
  12917. }
  12918. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12919. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12920. pll->on = pll->get_hw_state(dev_priv, pll,
  12921. &pll->config.hw_state);
  12922. pll->active = 0;
  12923. pll->config.crtc_mask = 0;
  12924. for_each_intel_crtc(dev, crtc) {
  12925. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12926. pll->active++;
  12927. pll->config.crtc_mask |= 1 << crtc->pipe;
  12928. }
  12929. }
  12930. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12931. pll->name, pll->config.crtc_mask, pll->on);
  12932. if (pll->config.crtc_mask)
  12933. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12934. }
  12935. for_each_intel_encoder(dev, encoder) {
  12936. pipe = 0;
  12937. if (encoder->get_hw_state(encoder, &pipe)) {
  12938. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12939. encoder->base.crtc = &crtc->base;
  12940. encoder->get_config(encoder, crtc->config);
  12941. } else {
  12942. encoder->base.crtc = NULL;
  12943. }
  12944. encoder->connectors_active = false;
  12945. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12946. encoder->base.base.id,
  12947. encoder->base.name,
  12948. encoder->base.crtc ? "enabled" : "disabled",
  12949. pipe_name(pipe));
  12950. }
  12951. for_each_intel_connector(dev, connector) {
  12952. if (connector->get_hw_state(connector)) {
  12953. connector->base.dpms = DRM_MODE_DPMS_ON;
  12954. connector->encoder->connectors_active = true;
  12955. connector->base.encoder = &connector->encoder->base;
  12956. } else {
  12957. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12958. connector->base.encoder = NULL;
  12959. }
  12960. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12961. connector->base.base.id,
  12962. connector->base.name,
  12963. connector->base.encoder ? "enabled" : "disabled");
  12964. }
  12965. }
  12966. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  12967. * and i915 state tracking structures. */
  12968. void intel_modeset_setup_hw_state(struct drm_device *dev,
  12969. bool force_restore)
  12970. {
  12971. struct drm_i915_private *dev_priv = dev->dev_private;
  12972. enum pipe pipe;
  12973. struct intel_crtc *crtc;
  12974. struct intel_encoder *encoder;
  12975. int i;
  12976. intel_modeset_readout_hw_state(dev);
  12977. /*
  12978. * Now that we have the config, copy it to each CRTC struct
  12979. * Note that this could go away if we move to using crtc_config
  12980. * checking everywhere.
  12981. */
  12982. for_each_intel_crtc(dev, crtc) {
  12983. if (crtc->active && i915.fastboot) {
  12984. intel_mode_from_pipe_config(&crtc->base.mode,
  12985. crtc->config);
  12986. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  12987. crtc->base.base.id);
  12988. drm_mode_debug_printmodeline(&crtc->base.mode);
  12989. }
  12990. }
  12991. /* HW state is read out, now we need to sanitize this mess. */
  12992. for_each_intel_encoder(dev, encoder) {
  12993. intel_sanitize_encoder(encoder);
  12994. }
  12995. for_each_pipe(dev_priv, pipe) {
  12996. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12997. intel_sanitize_crtc(crtc);
  12998. intel_dump_pipe_config(crtc, crtc->config,
  12999. "[setup_hw_state]");
  13000. }
  13001. intel_modeset_update_connector_atomic_state(dev);
  13002. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  13003. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  13004. if (!pll->on || pll->active)
  13005. continue;
  13006. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13007. pll->disable(dev_priv, pll);
  13008. pll->on = false;
  13009. }
  13010. if (IS_GEN9(dev))
  13011. skl_wm_get_hw_state(dev);
  13012. else if (HAS_PCH_SPLIT(dev))
  13013. ilk_wm_get_hw_state(dev);
  13014. if (force_restore) {
  13015. i915_redisable_vga(dev);
  13016. /*
  13017. * We need to use raw interfaces for restoring state to avoid
  13018. * checking (bogus) intermediate states.
  13019. */
  13020. for_each_pipe(dev_priv, pipe) {
  13021. struct drm_crtc *crtc =
  13022. dev_priv->pipe_to_crtc_mapping[pipe];
  13023. intel_crtc_restore_mode(crtc);
  13024. }
  13025. } else {
  13026. intel_modeset_update_staged_output_state(dev);
  13027. }
  13028. intel_modeset_check_state(dev);
  13029. }
  13030. void intel_modeset_gem_init(struct drm_device *dev)
  13031. {
  13032. struct drm_i915_private *dev_priv = dev->dev_private;
  13033. struct drm_crtc *c;
  13034. struct drm_i915_gem_object *obj;
  13035. int ret;
  13036. mutex_lock(&dev->struct_mutex);
  13037. intel_init_gt_powersave(dev);
  13038. mutex_unlock(&dev->struct_mutex);
  13039. /*
  13040. * There may be no VBT; and if the BIOS enabled SSC we can
  13041. * just keep using it to avoid unnecessary flicker. Whereas if the
  13042. * BIOS isn't using it, don't assume it will work even if the VBT
  13043. * indicates as much.
  13044. */
  13045. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  13046. dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13047. DREF_SSC1_ENABLE);
  13048. intel_modeset_init_hw(dev);
  13049. intel_setup_overlay(dev);
  13050. /*
  13051. * Make sure any fbs we allocated at startup are properly
  13052. * pinned & fenced. When we do the allocation it's too early
  13053. * for this.
  13054. */
  13055. for_each_crtc(dev, c) {
  13056. obj = intel_fb_obj(c->primary->fb);
  13057. if (obj == NULL)
  13058. continue;
  13059. mutex_lock(&dev->struct_mutex);
  13060. ret = intel_pin_and_fence_fb_obj(c->primary,
  13061. c->primary->fb,
  13062. c->primary->state,
  13063. NULL);
  13064. mutex_unlock(&dev->struct_mutex);
  13065. if (ret) {
  13066. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13067. to_intel_crtc(c)->pipe);
  13068. drm_framebuffer_unreference(c->primary->fb);
  13069. c->primary->fb = NULL;
  13070. c->primary->crtc = c->primary->state->crtc = NULL;
  13071. update_state_fb(c->primary);
  13072. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13073. }
  13074. }
  13075. intel_backlight_register(dev);
  13076. }
  13077. void intel_connector_unregister(struct intel_connector *intel_connector)
  13078. {
  13079. struct drm_connector *connector = &intel_connector->base;
  13080. intel_panel_destroy_backlight(connector);
  13081. drm_connector_unregister(connector);
  13082. }
  13083. void intel_modeset_cleanup(struct drm_device *dev)
  13084. {
  13085. struct drm_i915_private *dev_priv = dev->dev_private;
  13086. struct drm_connector *connector;
  13087. intel_disable_gt_powersave(dev);
  13088. intel_backlight_unregister(dev);
  13089. /*
  13090. * Interrupts and polling as the first thing to avoid creating havoc.
  13091. * Too much stuff here (turning of connectors, ...) would
  13092. * experience fancy races otherwise.
  13093. */
  13094. intel_irq_uninstall(dev_priv);
  13095. /*
  13096. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13097. * poll handlers. Hence disable polling after hpd handling is shut down.
  13098. */
  13099. drm_kms_helper_poll_fini(dev);
  13100. mutex_lock(&dev->struct_mutex);
  13101. intel_unregister_dsm_handler();
  13102. intel_fbc_disable(dev);
  13103. mutex_unlock(&dev->struct_mutex);
  13104. /* flush any delayed tasks or pending work */
  13105. flush_scheduled_work();
  13106. /* destroy the backlight and sysfs files before encoders/connectors */
  13107. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13108. struct intel_connector *intel_connector;
  13109. intel_connector = to_intel_connector(connector);
  13110. intel_connector->unregister(intel_connector);
  13111. }
  13112. drm_mode_config_cleanup(dev);
  13113. intel_cleanup_overlay(dev);
  13114. mutex_lock(&dev->struct_mutex);
  13115. intel_cleanup_gt_powersave(dev);
  13116. mutex_unlock(&dev->struct_mutex);
  13117. }
  13118. /*
  13119. * Return which encoder is currently attached for connector.
  13120. */
  13121. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13122. {
  13123. return &intel_attached_encoder(connector)->base;
  13124. }
  13125. void intel_connector_attach_encoder(struct intel_connector *connector,
  13126. struct intel_encoder *encoder)
  13127. {
  13128. connector->encoder = encoder;
  13129. drm_mode_connector_attach_encoder(&connector->base,
  13130. &encoder->base);
  13131. }
  13132. /*
  13133. * set vga decode state - true == enable VGA decode
  13134. */
  13135. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13136. {
  13137. struct drm_i915_private *dev_priv = dev->dev_private;
  13138. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13139. u16 gmch_ctrl;
  13140. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13141. DRM_ERROR("failed to read control word\n");
  13142. return -EIO;
  13143. }
  13144. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13145. return 0;
  13146. if (state)
  13147. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13148. else
  13149. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13150. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13151. DRM_ERROR("failed to write control word\n");
  13152. return -EIO;
  13153. }
  13154. return 0;
  13155. }
  13156. struct intel_display_error_state {
  13157. u32 power_well_driver;
  13158. int num_transcoders;
  13159. struct intel_cursor_error_state {
  13160. u32 control;
  13161. u32 position;
  13162. u32 base;
  13163. u32 size;
  13164. } cursor[I915_MAX_PIPES];
  13165. struct intel_pipe_error_state {
  13166. bool power_domain_on;
  13167. u32 source;
  13168. u32 stat;
  13169. } pipe[I915_MAX_PIPES];
  13170. struct intel_plane_error_state {
  13171. u32 control;
  13172. u32 stride;
  13173. u32 size;
  13174. u32 pos;
  13175. u32 addr;
  13176. u32 surface;
  13177. u32 tile_offset;
  13178. } plane[I915_MAX_PIPES];
  13179. struct intel_transcoder_error_state {
  13180. bool power_domain_on;
  13181. enum transcoder cpu_transcoder;
  13182. u32 conf;
  13183. u32 htotal;
  13184. u32 hblank;
  13185. u32 hsync;
  13186. u32 vtotal;
  13187. u32 vblank;
  13188. u32 vsync;
  13189. } transcoder[4];
  13190. };
  13191. struct intel_display_error_state *
  13192. intel_display_capture_error_state(struct drm_device *dev)
  13193. {
  13194. struct drm_i915_private *dev_priv = dev->dev_private;
  13195. struct intel_display_error_state *error;
  13196. int transcoders[] = {
  13197. TRANSCODER_A,
  13198. TRANSCODER_B,
  13199. TRANSCODER_C,
  13200. TRANSCODER_EDP,
  13201. };
  13202. int i;
  13203. if (INTEL_INFO(dev)->num_pipes == 0)
  13204. return NULL;
  13205. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13206. if (error == NULL)
  13207. return NULL;
  13208. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13209. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13210. for_each_pipe(dev_priv, i) {
  13211. error->pipe[i].power_domain_on =
  13212. __intel_display_power_is_enabled(dev_priv,
  13213. POWER_DOMAIN_PIPE(i));
  13214. if (!error->pipe[i].power_domain_on)
  13215. continue;
  13216. error->cursor[i].control = I915_READ(CURCNTR(i));
  13217. error->cursor[i].position = I915_READ(CURPOS(i));
  13218. error->cursor[i].base = I915_READ(CURBASE(i));
  13219. error->plane[i].control = I915_READ(DSPCNTR(i));
  13220. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13221. if (INTEL_INFO(dev)->gen <= 3) {
  13222. error->plane[i].size = I915_READ(DSPSIZE(i));
  13223. error->plane[i].pos = I915_READ(DSPPOS(i));
  13224. }
  13225. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13226. error->plane[i].addr = I915_READ(DSPADDR(i));
  13227. if (INTEL_INFO(dev)->gen >= 4) {
  13228. error->plane[i].surface = I915_READ(DSPSURF(i));
  13229. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13230. }
  13231. error->pipe[i].source = I915_READ(PIPESRC(i));
  13232. if (HAS_GMCH_DISPLAY(dev))
  13233. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13234. }
  13235. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13236. if (HAS_DDI(dev_priv->dev))
  13237. error->num_transcoders++; /* Account for eDP. */
  13238. for (i = 0; i < error->num_transcoders; i++) {
  13239. enum transcoder cpu_transcoder = transcoders[i];
  13240. error->transcoder[i].power_domain_on =
  13241. __intel_display_power_is_enabled(dev_priv,
  13242. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13243. if (!error->transcoder[i].power_domain_on)
  13244. continue;
  13245. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13246. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13247. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13248. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13249. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13250. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13251. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13252. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13253. }
  13254. return error;
  13255. }
  13256. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13257. void
  13258. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13259. struct drm_device *dev,
  13260. struct intel_display_error_state *error)
  13261. {
  13262. struct drm_i915_private *dev_priv = dev->dev_private;
  13263. int i;
  13264. if (!error)
  13265. return;
  13266. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13267. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13268. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13269. error->power_well_driver);
  13270. for_each_pipe(dev_priv, i) {
  13271. err_printf(m, "Pipe [%d]:\n", i);
  13272. err_printf(m, " Power: %s\n",
  13273. error->pipe[i].power_domain_on ? "on" : "off");
  13274. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13275. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13276. err_printf(m, "Plane [%d]:\n", i);
  13277. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13278. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13279. if (INTEL_INFO(dev)->gen <= 3) {
  13280. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13281. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13282. }
  13283. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13284. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13285. if (INTEL_INFO(dev)->gen >= 4) {
  13286. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13287. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13288. }
  13289. err_printf(m, "Cursor [%d]:\n", i);
  13290. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13291. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13292. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13293. }
  13294. for (i = 0; i < error->num_transcoders; i++) {
  13295. err_printf(m, "CPU transcoder: %c\n",
  13296. transcoder_name(error->transcoder[i].cpu_transcoder));
  13297. err_printf(m, " Power: %s\n",
  13298. error->transcoder[i].power_domain_on ? "on" : "off");
  13299. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13300. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13301. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13302. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13303. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13304. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13305. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13306. }
  13307. }
  13308. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13309. {
  13310. struct intel_crtc *crtc;
  13311. for_each_intel_crtc(dev, crtc) {
  13312. struct intel_unpin_work *work;
  13313. spin_lock_irq(&dev->event_lock);
  13314. work = crtc->unpin_work;
  13315. if (work && work->event &&
  13316. work->event->base.file_priv == file) {
  13317. kfree(work->event);
  13318. work->event = NULL;
  13319. }
  13320. spin_unlock_irq(&dev->event_lock);
  13321. }
  13322. }