intel_display.c 442 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833
  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 "intel_dsi.h"
  39. #include "i915_trace.h"
  40. #include <drm/drm_atomic.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include <drm/drm_dp_helper.h>
  43. #include <drm/drm_crtc_helper.h>
  44. #include <drm/drm_plane_helper.h>
  45. #include <drm/drm_rect.h>
  46. #include <linux/dma_remapping.h>
  47. #include <linux/reservation.h>
  48. #include <linux/dma-buf.h>
  49. /* Primary plane formats for gen <= 3 */
  50. static const uint32_t i8xx_primary_formats[] = {
  51. DRM_FORMAT_C8,
  52. DRM_FORMAT_RGB565,
  53. DRM_FORMAT_XRGB1555,
  54. DRM_FORMAT_XRGB8888,
  55. };
  56. /* Primary plane formats for gen >= 4 */
  57. static const uint32_t i965_primary_formats[] = {
  58. DRM_FORMAT_C8,
  59. DRM_FORMAT_RGB565,
  60. DRM_FORMAT_XRGB8888,
  61. DRM_FORMAT_XBGR8888,
  62. DRM_FORMAT_XRGB2101010,
  63. DRM_FORMAT_XBGR2101010,
  64. };
  65. static const uint32_t skl_primary_formats[] = {
  66. DRM_FORMAT_C8,
  67. DRM_FORMAT_RGB565,
  68. DRM_FORMAT_XRGB8888,
  69. DRM_FORMAT_XBGR8888,
  70. DRM_FORMAT_ARGB8888,
  71. DRM_FORMAT_ABGR8888,
  72. DRM_FORMAT_XRGB2101010,
  73. DRM_FORMAT_XBGR2101010,
  74. DRM_FORMAT_YUYV,
  75. DRM_FORMAT_YVYU,
  76. DRM_FORMAT_UYVY,
  77. DRM_FORMAT_VYUY,
  78. };
  79. /* Cursor formats */
  80. static const uint32_t intel_cursor_formats[] = {
  81. DRM_FORMAT_ARGB8888,
  82. };
  83. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  86. struct intel_crtc_state *pipe_config);
  87. static int intel_framebuffer_init(struct drm_device *dev,
  88. struct intel_framebuffer *ifb,
  89. struct drm_mode_fb_cmd2 *mode_cmd,
  90. struct drm_i915_gem_object *obj);
  91. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  92. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  93. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  94. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  95. struct intel_link_m_n *m_n,
  96. struct intel_link_m_n *m2_n2);
  97. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  98. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  99. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  100. static void vlv_prepare_pll(struct intel_crtc *crtc,
  101. const struct intel_crtc_state *pipe_config);
  102. static void chv_prepare_pll(struct intel_crtc *crtc,
  103. const struct intel_crtc_state *pipe_config);
  104. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  105. struct intel_crtc_state *crtc_state);
  106. static void skylake_pfit_enable(struct intel_crtc *crtc);
  107. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  108. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  109. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  110. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  111. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  112. static void intel_modeset_verify_crtc(struct drm_crtc *crtc,
  113. struct drm_crtc_state *old_state,
  114. struct drm_crtc_state *new_state);
  115. struct intel_limit {
  116. struct {
  117. int min, max;
  118. } dot, vco, n, m, m1, m2, p, p1;
  119. struct {
  120. int dot_limit;
  121. int p2_slow, p2_fast;
  122. } p2;
  123. };
  124. /* returns HPLL frequency in kHz */
  125. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  126. {
  127. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  128. /* Obtain SKU information */
  129. mutex_lock(&dev_priv->sb_lock);
  130. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  131. CCK_FUSE_HPLL_FREQ_MASK;
  132. mutex_unlock(&dev_priv->sb_lock);
  133. return vco_freq[hpll_freq] * 1000;
  134. }
  135. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  136. const char *name, u32 reg, int ref_freq)
  137. {
  138. u32 val;
  139. int divider;
  140. mutex_lock(&dev_priv->sb_lock);
  141. val = vlv_cck_read(dev_priv, reg);
  142. mutex_unlock(&dev_priv->sb_lock);
  143. divider = val & CCK_FREQUENCY_VALUES;
  144. WARN((val & CCK_FREQUENCY_STATUS) !=
  145. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  146. "%s change in progress\n", name);
  147. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  148. }
  149. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  150. const char *name, u32 reg)
  151. {
  152. if (dev_priv->hpll_freq == 0)
  153. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  154. return vlv_get_cck_clock(dev_priv, name, reg,
  155. dev_priv->hpll_freq);
  156. }
  157. static int
  158. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  159. {
  160. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  161. }
  162. static int
  163. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  164. {
  165. /* RAWCLK_FREQ_VLV register updated from power well code */
  166. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  167. CCK_DISPLAY_REF_CLOCK_CONTROL);
  168. }
  169. static int
  170. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  171. {
  172. uint32_t clkcfg;
  173. /* hrawclock is 1/4 the FSB frequency */
  174. clkcfg = I915_READ(CLKCFG);
  175. switch (clkcfg & CLKCFG_FSB_MASK) {
  176. case CLKCFG_FSB_400:
  177. return 100000;
  178. case CLKCFG_FSB_533:
  179. return 133333;
  180. case CLKCFG_FSB_667:
  181. return 166667;
  182. case CLKCFG_FSB_800:
  183. return 200000;
  184. case CLKCFG_FSB_1067:
  185. return 266667;
  186. case CLKCFG_FSB_1333:
  187. return 333333;
  188. /* these two are just a guess; one of them might be right */
  189. case CLKCFG_FSB_1600:
  190. case CLKCFG_FSB_1600_ALT:
  191. return 400000;
  192. default:
  193. return 133333;
  194. }
  195. }
  196. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  197. {
  198. if (HAS_PCH_SPLIT(dev_priv))
  199. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  200. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  201. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  202. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  203. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  204. else
  205. return; /* no rawclk on other platforms, or no need to know it */
  206. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  207. }
  208. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  209. {
  210. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  211. return;
  212. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  213. CCK_CZ_CLOCK_CONTROL);
  214. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  215. }
  216. static inline u32 /* units of 100MHz */
  217. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  218. const struct intel_crtc_state *pipe_config)
  219. {
  220. if (HAS_DDI(dev_priv))
  221. return pipe_config->port_clock; /* SPLL */
  222. else if (IS_GEN5(dev_priv))
  223. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  224. else
  225. return 270000;
  226. }
  227. static const struct intel_limit intel_limits_i8xx_dac = {
  228. .dot = { .min = 25000, .max = 350000 },
  229. .vco = { .min = 908000, .max = 1512000 },
  230. .n = { .min = 2, .max = 16 },
  231. .m = { .min = 96, .max = 140 },
  232. .m1 = { .min = 18, .max = 26 },
  233. .m2 = { .min = 6, .max = 16 },
  234. .p = { .min = 4, .max = 128 },
  235. .p1 = { .min = 2, .max = 33 },
  236. .p2 = { .dot_limit = 165000,
  237. .p2_slow = 4, .p2_fast = 2 },
  238. };
  239. static const struct intel_limit intel_limits_i8xx_dvo = {
  240. .dot = { .min = 25000, .max = 350000 },
  241. .vco = { .min = 908000, .max = 1512000 },
  242. .n = { .min = 2, .max = 16 },
  243. .m = { .min = 96, .max = 140 },
  244. .m1 = { .min = 18, .max = 26 },
  245. .m2 = { .min = 6, .max = 16 },
  246. .p = { .min = 4, .max = 128 },
  247. .p1 = { .min = 2, .max = 33 },
  248. .p2 = { .dot_limit = 165000,
  249. .p2_slow = 4, .p2_fast = 4 },
  250. };
  251. static const struct intel_limit intel_limits_i8xx_lvds = {
  252. .dot = { .min = 25000, .max = 350000 },
  253. .vco = { .min = 908000, .max = 1512000 },
  254. .n = { .min = 2, .max = 16 },
  255. .m = { .min = 96, .max = 140 },
  256. .m1 = { .min = 18, .max = 26 },
  257. .m2 = { .min = 6, .max = 16 },
  258. .p = { .min = 4, .max = 128 },
  259. .p1 = { .min = 1, .max = 6 },
  260. .p2 = { .dot_limit = 165000,
  261. .p2_slow = 14, .p2_fast = 7 },
  262. };
  263. static const struct intel_limit intel_limits_i9xx_sdvo = {
  264. .dot = { .min = 20000, .max = 400000 },
  265. .vco = { .min = 1400000, .max = 2800000 },
  266. .n = { .min = 1, .max = 6 },
  267. .m = { .min = 70, .max = 120 },
  268. .m1 = { .min = 8, .max = 18 },
  269. .m2 = { .min = 3, .max = 7 },
  270. .p = { .min = 5, .max = 80 },
  271. .p1 = { .min = 1, .max = 8 },
  272. .p2 = { .dot_limit = 200000,
  273. .p2_slow = 10, .p2_fast = 5 },
  274. };
  275. static const struct intel_limit intel_limits_i9xx_lvds = {
  276. .dot = { .min = 20000, .max = 400000 },
  277. .vco = { .min = 1400000, .max = 2800000 },
  278. .n = { .min = 1, .max = 6 },
  279. .m = { .min = 70, .max = 120 },
  280. .m1 = { .min = 8, .max = 18 },
  281. .m2 = { .min = 3, .max = 7 },
  282. .p = { .min = 7, .max = 98 },
  283. .p1 = { .min = 1, .max = 8 },
  284. .p2 = { .dot_limit = 112000,
  285. .p2_slow = 14, .p2_fast = 7 },
  286. };
  287. static const struct intel_limit intel_limits_g4x_sdvo = {
  288. .dot = { .min = 25000, .max = 270000 },
  289. .vco = { .min = 1750000, .max = 3500000},
  290. .n = { .min = 1, .max = 4 },
  291. .m = { .min = 104, .max = 138 },
  292. .m1 = { .min = 17, .max = 23 },
  293. .m2 = { .min = 5, .max = 11 },
  294. .p = { .min = 10, .max = 30 },
  295. .p1 = { .min = 1, .max = 3},
  296. .p2 = { .dot_limit = 270000,
  297. .p2_slow = 10,
  298. .p2_fast = 10
  299. },
  300. };
  301. static const struct intel_limit intel_limits_g4x_hdmi = {
  302. .dot = { .min = 22000, .max = 400000 },
  303. .vco = { .min = 1750000, .max = 3500000},
  304. .n = { .min = 1, .max = 4 },
  305. .m = { .min = 104, .max = 138 },
  306. .m1 = { .min = 16, .max = 23 },
  307. .m2 = { .min = 5, .max = 11 },
  308. .p = { .min = 5, .max = 80 },
  309. .p1 = { .min = 1, .max = 8},
  310. .p2 = { .dot_limit = 165000,
  311. .p2_slow = 10, .p2_fast = 5 },
  312. };
  313. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  314. .dot = { .min = 20000, .max = 115000 },
  315. .vco = { .min = 1750000, .max = 3500000 },
  316. .n = { .min = 1, .max = 3 },
  317. .m = { .min = 104, .max = 138 },
  318. .m1 = { .min = 17, .max = 23 },
  319. .m2 = { .min = 5, .max = 11 },
  320. .p = { .min = 28, .max = 112 },
  321. .p1 = { .min = 2, .max = 8 },
  322. .p2 = { .dot_limit = 0,
  323. .p2_slow = 14, .p2_fast = 14
  324. },
  325. };
  326. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  327. .dot = { .min = 80000, .max = 224000 },
  328. .vco = { .min = 1750000, .max = 3500000 },
  329. .n = { .min = 1, .max = 3 },
  330. .m = { .min = 104, .max = 138 },
  331. .m1 = { .min = 17, .max = 23 },
  332. .m2 = { .min = 5, .max = 11 },
  333. .p = { .min = 14, .max = 42 },
  334. .p1 = { .min = 2, .max = 6 },
  335. .p2 = { .dot_limit = 0,
  336. .p2_slow = 7, .p2_fast = 7
  337. },
  338. };
  339. static const struct intel_limit intel_limits_pineview_sdvo = {
  340. .dot = { .min = 20000, .max = 400000},
  341. .vco = { .min = 1700000, .max = 3500000 },
  342. /* Pineview's Ncounter is a ring counter */
  343. .n = { .min = 3, .max = 6 },
  344. .m = { .min = 2, .max = 256 },
  345. /* Pineview only has one combined m divider, which we treat as m2. */
  346. .m1 = { .min = 0, .max = 0 },
  347. .m2 = { .min = 0, .max = 254 },
  348. .p = { .min = 5, .max = 80 },
  349. .p1 = { .min = 1, .max = 8 },
  350. .p2 = { .dot_limit = 200000,
  351. .p2_slow = 10, .p2_fast = 5 },
  352. };
  353. static const struct intel_limit intel_limits_pineview_lvds = {
  354. .dot = { .min = 20000, .max = 400000 },
  355. .vco = { .min = 1700000, .max = 3500000 },
  356. .n = { .min = 3, .max = 6 },
  357. .m = { .min = 2, .max = 256 },
  358. .m1 = { .min = 0, .max = 0 },
  359. .m2 = { .min = 0, .max = 254 },
  360. .p = { .min = 7, .max = 112 },
  361. .p1 = { .min = 1, .max = 8 },
  362. .p2 = { .dot_limit = 112000,
  363. .p2_slow = 14, .p2_fast = 14 },
  364. };
  365. /* Ironlake / Sandybridge
  366. *
  367. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  368. * the range value for them is (actual_value - 2).
  369. */
  370. static const struct intel_limit intel_limits_ironlake_dac = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 5 },
  374. .m = { .min = 79, .max = 127 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 5, .max = 80 },
  378. .p1 = { .min = 1, .max = 8 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 10, .p2_fast = 5 },
  381. };
  382. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  383. .dot = { .min = 25000, .max = 350000 },
  384. .vco = { .min = 1760000, .max = 3510000 },
  385. .n = { .min = 1, .max = 3 },
  386. .m = { .min = 79, .max = 118 },
  387. .m1 = { .min = 12, .max = 22 },
  388. .m2 = { .min = 5, .max = 9 },
  389. .p = { .min = 28, .max = 112 },
  390. .p1 = { .min = 2, .max = 8 },
  391. .p2 = { .dot_limit = 225000,
  392. .p2_slow = 14, .p2_fast = 14 },
  393. };
  394. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  395. .dot = { .min = 25000, .max = 350000 },
  396. .vco = { .min = 1760000, .max = 3510000 },
  397. .n = { .min = 1, .max = 3 },
  398. .m = { .min = 79, .max = 127 },
  399. .m1 = { .min = 12, .max = 22 },
  400. .m2 = { .min = 5, .max = 9 },
  401. .p = { .min = 14, .max = 56 },
  402. .p1 = { .min = 2, .max = 8 },
  403. .p2 = { .dot_limit = 225000,
  404. .p2_slow = 7, .p2_fast = 7 },
  405. };
  406. /* LVDS 100mhz refclk limits. */
  407. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  408. .dot = { .min = 25000, .max = 350000 },
  409. .vco = { .min = 1760000, .max = 3510000 },
  410. .n = { .min = 1, .max = 2 },
  411. .m = { .min = 79, .max = 126 },
  412. .m1 = { .min = 12, .max = 22 },
  413. .m2 = { .min = 5, .max = 9 },
  414. .p = { .min = 28, .max = 112 },
  415. .p1 = { .min = 2, .max = 8 },
  416. .p2 = { .dot_limit = 225000,
  417. .p2_slow = 14, .p2_fast = 14 },
  418. };
  419. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  420. .dot = { .min = 25000, .max = 350000 },
  421. .vco = { .min = 1760000, .max = 3510000 },
  422. .n = { .min = 1, .max = 3 },
  423. .m = { .min = 79, .max = 126 },
  424. .m1 = { .min = 12, .max = 22 },
  425. .m2 = { .min = 5, .max = 9 },
  426. .p = { .min = 14, .max = 42 },
  427. .p1 = { .min = 2, .max = 6 },
  428. .p2 = { .dot_limit = 225000,
  429. .p2_slow = 7, .p2_fast = 7 },
  430. };
  431. static const struct intel_limit intel_limits_vlv = {
  432. /*
  433. * These are the data rate limits (measured in fast clocks)
  434. * since those are the strictest limits we have. The fast
  435. * clock and actual rate limits are more relaxed, so checking
  436. * them would make no difference.
  437. */
  438. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  439. .vco = { .min = 4000000, .max = 6000000 },
  440. .n = { .min = 1, .max = 7 },
  441. .m1 = { .min = 2, .max = 3 },
  442. .m2 = { .min = 11, .max = 156 },
  443. .p1 = { .min = 2, .max = 3 },
  444. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  445. };
  446. static const struct intel_limit intel_limits_chv = {
  447. /*
  448. * These are the data rate limits (measured in fast clocks)
  449. * since those are the strictest limits we have. The fast
  450. * clock and actual rate limits are more relaxed, so checking
  451. * them would make no difference.
  452. */
  453. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  454. .vco = { .min = 4800000, .max = 6480000 },
  455. .n = { .min = 1, .max = 1 },
  456. .m1 = { .min = 2, .max = 2 },
  457. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  458. .p1 = { .min = 2, .max = 4 },
  459. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  460. };
  461. static const struct intel_limit intel_limits_bxt = {
  462. /* FIXME: find real dot limits */
  463. .dot = { .min = 0, .max = INT_MAX },
  464. .vco = { .min = 4800000, .max = 6700000 },
  465. .n = { .min = 1, .max = 1 },
  466. .m1 = { .min = 2, .max = 2 },
  467. /* FIXME: find real m2 limits */
  468. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  469. .p1 = { .min = 2, .max = 4 },
  470. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  471. };
  472. static bool
  473. needs_modeset(struct drm_crtc_state *state)
  474. {
  475. return drm_atomic_crtc_needs_modeset(state);
  476. }
  477. /**
  478. * Returns whether any output on the specified pipe is of the specified type
  479. */
  480. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  481. {
  482. struct drm_device *dev = crtc->base.dev;
  483. struct intel_encoder *encoder;
  484. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  485. if (encoder->type == type)
  486. return true;
  487. return false;
  488. }
  489. /**
  490. * Returns whether any output on the specified pipe will have the specified
  491. * type after a staged modeset is complete, i.e., the same as
  492. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  493. * encoder->crtc.
  494. */
  495. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  496. int type)
  497. {
  498. struct drm_atomic_state *state = crtc_state->base.state;
  499. struct drm_connector *connector;
  500. struct drm_connector_state *connector_state;
  501. struct intel_encoder *encoder;
  502. int i, num_connectors = 0;
  503. for_each_connector_in_state(state, connector, connector_state, i) {
  504. if (connector_state->crtc != crtc_state->base.crtc)
  505. continue;
  506. num_connectors++;
  507. encoder = to_intel_encoder(connector_state->best_encoder);
  508. if (encoder->type == type)
  509. return true;
  510. }
  511. WARN_ON(num_connectors == 0);
  512. return false;
  513. }
  514. /*
  515. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  516. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  517. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  518. * The helpers' return value is the rate of the clock that is fed to the
  519. * display engine's pipe which can be the above fast dot clock rate or a
  520. * divided-down version of it.
  521. */
  522. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  523. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  524. {
  525. clock->m = clock->m2 + 2;
  526. clock->p = clock->p1 * clock->p2;
  527. if (WARN_ON(clock->n == 0 || clock->p == 0))
  528. return 0;
  529. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  530. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  531. return clock->dot;
  532. }
  533. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  534. {
  535. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  536. }
  537. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  538. {
  539. clock->m = i9xx_dpll_compute_m(clock);
  540. clock->p = clock->p1 * clock->p2;
  541. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  542. return 0;
  543. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  544. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  545. return clock->dot;
  546. }
  547. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  548. {
  549. clock->m = clock->m1 * clock->m2;
  550. clock->p = clock->p1 * clock->p2;
  551. if (WARN_ON(clock->n == 0 || clock->p == 0))
  552. return 0;
  553. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  554. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  555. return clock->dot / 5;
  556. }
  557. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  558. {
  559. clock->m = clock->m1 * clock->m2;
  560. clock->p = clock->p1 * clock->p2;
  561. if (WARN_ON(clock->n == 0 || clock->p == 0))
  562. return 0;
  563. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  564. clock->n << 22);
  565. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  566. return clock->dot / 5;
  567. }
  568. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  569. /**
  570. * Returns whether the given set of divisors are valid for a given refclk with
  571. * the given connectors.
  572. */
  573. static bool intel_PLL_is_valid(struct drm_device *dev,
  574. const struct intel_limit *limit,
  575. const struct dpll *clock)
  576. {
  577. if (clock->n < limit->n.min || limit->n.max < clock->n)
  578. INTELPllInvalid("n out of range\n");
  579. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  580. INTELPllInvalid("p1 out of range\n");
  581. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  582. INTELPllInvalid("m2 out of range\n");
  583. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  584. INTELPllInvalid("m1 out of range\n");
  585. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) &&
  586. !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev))
  587. if (clock->m1 <= clock->m2)
  588. INTELPllInvalid("m1 <= m2\n");
  589. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && !IS_BROXTON(dev)) {
  590. if (clock->p < limit->p.min || limit->p.max < clock->p)
  591. INTELPllInvalid("p out of range\n");
  592. if (clock->m < limit->m.min || limit->m.max < clock->m)
  593. INTELPllInvalid("m out of range\n");
  594. }
  595. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  596. INTELPllInvalid("vco out of range\n");
  597. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  598. * connector, etc., rather than just a single range.
  599. */
  600. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  601. INTELPllInvalid("dot out of range\n");
  602. return true;
  603. }
  604. static int
  605. i9xx_select_p2_div(const struct intel_limit *limit,
  606. const struct intel_crtc_state *crtc_state,
  607. int target)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  611. /*
  612. * For LVDS just rely on its current settings for dual-channel.
  613. * We haven't figured out how to reliably set up different
  614. * single/dual channel state, if we even can.
  615. */
  616. if (intel_is_dual_link_lvds(dev))
  617. return limit->p2.p2_fast;
  618. else
  619. return limit->p2.p2_slow;
  620. } else {
  621. if (target < limit->p2.dot_limit)
  622. return limit->p2.p2_slow;
  623. else
  624. return limit->p2.p2_fast;
  625. }
  626. }
  627. /*
  628. * Returns a set of divisors for the desired target clock with the given
  629. * refclk, or FALSE. The returned values represent the clock equation:
  630. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  631. *
  632. * Target and reference clocks are specified in kHz.
  633. *
  634. * If match_clock is provided, then best_clock P divider must match the P
  635. * divider from @match_clock used for LVDS downclocking.
  636. */
  637. static bool
  638. i9xx_find_best_dpll(const struct intel_limit *limit,
  639. struct intel_crtc_state *crtc_state,
  640. int target, int refclk, struct dpll *match_clock,
  641. struct dpll *best_clock)
  642. {
  643. struct drm_device *dev = crtc_state->base.crtc->dev;
  644. struct dpll clock;
  645. int err = target;
  646. memset(best_clock, 0, sizeof(*best_clock));
  647. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  648. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  649. clock.m1++) {
  650. for (clock.m2 = limit->m2.min;
  651. clock.m2 <= limit->m2.max; clock.m2++) {
  652. if (clock.m2 >= clock.m1)
  653. break;
  654. for (clock.n = limit->n.min;
  655. clock.n <= limit->n.max; clock.n++) {
  656. for (clock.p1 = limit->p1.min;
  657. clock.p1 <= limit->p1.max; clock.p1++) {
  658. int this_err;
  659. i9xx_calc_dpll_params(refclk, &clock);
  660. if (!intel_PLL_is_valid(dev, limit,
  661. &clock))
  662. continue;
  663. if (match_clock &&
  664. clock.p != match_clock->p)
  665. continue;
  666. this_err = abs(clock.dot - target);
  667. if (this_err < err) {
  668. *best_clock = clock;
  669. err = this_err;
  670. }
  671. }
  672. }
  673. }
  674. }
  675. return (err != target);
  676. }
  677. /*
  678. * Returns a set of divisors for the desired target clock with the given
  679. * refclk, or FALSE. The returned values represent the clock equation:
  680. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  681. *
  682. * Target and reference clocks are specified in kHz.
  683. *
  684. * If match_clock is provided, then best_clock P divider must match the P
  685. * divider from @match_clock used for LVDS downclocking.
  686. */
  687. static bool
  688. pnv_find_best_dpll(const struct intel_limit *limit,
  689. struct intel_crtc_state *crtc_state,
  690. int target, int refclk, struct dpll *match_clock,
  691. struct dpll *best_clock)
  692. {
  693. struct drm_device *dev = crtc_state->base.crtc->dev;
  694. struct dpll clock;
  695. int err = target;
  696. memset(best_clock, 0, sizeof(*best_clock));
  697. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  698. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  699. clock.m1++) {
  700. for (clock.m2 = limit->m2.min;
  701. clock.m2 <= limit->m2.max; clock.m2++) {
  702. for (clock.n = limit->n.min;
  703. clock.n <= limit->n.max; clock.n++) {
  704. for (clock.p1 = limit->p1.min;
  705. clock.p1 <= limit->p1.max; clock.p1++) {
  706. int this_err;
  707. pnv_calc_dpll_params(refclk, &clock);
  708. if (!intel_PLL_is_valid(dev, limit,
  709. &clock))
  710. continue;
  711. if (match_clock &&
  712. clock.p != match_clock->p)
  713. continue;
  714. this_err = abs(clock.dot - target);
  715. if (this_err < err) {
  716. *best_clock = clock;
  717. err = this_err;
  718. }
  719. }
  720. }
  721. }
  722. }
  723. return (err != target);
  724. }
  725. /*
  726. * Returns a set of divisors for the desired target clock with the given
  727. * refclk, or FALSE. The returned values represent the clock equation:
  728. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  729. *
  730. * Target and reference clocks are specified in kHz.
  731. *
  732. * If match_clock is provided, then best_clock P divider must match the P
  733. * divider from @match_clock used for LVDS downclocking.
  734. */
  735. static bool
  736. g4x_find_best_dpll(const struct intel_limit *limit,
  737. struct intel_crtc_state *crtc_state,
  738. int target, int refclk, struct dpll *match_clock,
  739. struct dpll *best_clock)
  740. {
  741. struct drm_device *dev = crtc_state->base.crtc->dev;
  742. struct dpll clock;
  743. int max_n;
  744. bool found = false;
  745. /* approximately equals target * 0.00585 */
  746. int err_most = (target >> 8) + (target >> 9);
  747. memset(best_clock, 0, sizeof(*best_clock));
  748. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  749. max_n = limit->n.max;
  750. /* based on hardware requirement, prefer smaller n to precision */
  751. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  752. /* based on hardware requirement, prefere larger m1,m2 */
  753. for (clock.m1 = limit->m1.max;
  754. clock.m1 >= limit->m1.min; clock.m1--) {
  755. for (clock.m2 = limit->m2.max;
  756. clock.m2 >= limit->m2.min; clock.m2--) {
  757. for (clock.p1 = limit->p1.max;
  758. clock.p1 >= limit->p1.min; clock.p1--) {
  759. int this_err;
  760. i9xx_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(dev, limit,
  762. &clock))
  763. continue;
  764. this_err = abs(clock.dot - target);
  765. if (this_err < err_most) {
  766. *best_clock = clock;
  767. err_most = this_err;
  768. max_n = clock.n;
  769. found = true;
  770. }
  771. }
  772. }
  773. }
  774. }
  775. return found;
  776. }
  777. /*
  778. * Check if the calculated PLL configuration is more optimal compared to the
  779. * best configuration and error found so far. Return the calculated error.
  780. */
  781. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  782. const struct dpll *calculated_clock,
  783. const struct dpll *best_clock,
  784. unsigned int best_error_ppm,
  785. unsigned int *error_ppm)
  786. {
  787. /*
  788. * For CHV ignore the error and consider only the P value.
  789. * Prefer a bigger P value based on HW requirements.
  790. */
  791. if (IS_CHERRYVIEW(dev)) {
  792. *error_ppm = 0;
  793. return calculated_clock->p > best_clock->p;
  794. }
  795. if (WARN_ON_ONCE(!target_freq))
  796. return false;
  797. *error_ppm = div_u64(1000000ULL *
  798. abs(target_freq - calculated_clock->dot),
  799. target_freq);
  800. /*
  801. * Prefer a better P value over a better (smaller) error if the error
  802. * is small. Ensure this preference for future configurations too by
  803. * setting the error to 0.
  804. */
  805. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  806. *error_ppm = 0;
  807. return true;
  808. }
  809. return *error_ppm + 10 < best_error_ppm;
  810. }
  811. /*
  812. * Returns a set of divisors for the desired target clock with the given
  813. * refclk, or FALSE. The returned values represent the clock equation:
  814. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  815. */
  816. static bool
  817. vlv_find_best_dpll(const struct intel_limit *limit,
  818. struct intel_crtc_state *crtc_state,
  819. int target, int refclk, struct dpll *match_clock,
  820. struct dpll *best_clock)
  821. {
  822. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  823. struct drm_device *dev = crtc->base.dev;
  824. struct dpll clock;
  825. unsigned int bestppm = 1000000;
  826. /* min update 19.2 MHz */
  827. int max_n = min(limit->n.max, refclk / 19200);
  828. bool found = false;
  829. target *= 5; /* fast clock */
  830. memset(best_clock, 0, sizeof(*best_clock));
  831. /* based on hardware requirement, prefer smaller n to precision */
  832. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  833. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  834. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  835. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  836. clock.p = clock.p1 * clock.p2;
  837. /* based on hardware requirement, prefer bigger m1,m2 values */
  838. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  839. unsigned int ppm;
  840. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  841. refclk * clock.m1);
  842. vlv_calc_dpll_params(refclk, &clock);
  843. if (!intel_PLL_is_valid(dev, limit,
  844. &clock))
  845. continue;
  846. if (!vlv_PLL_is_optimal(dev, target,
  847. &clock,
  848. best_clock,
  849. bestppm, &ppm))
  850. continue;
  851. *best_clock = clock;
  852. bestppm = ppm;
  853. found = true;
  854. }
  855. }
  856. }
  857. }
  858. return found;
  859. }
  860. /*
  861. * Returns a set of divisors for the desired target clock with the given
  862. * refclk, or FALSE. The returned values represent the clock equation:
  863. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  864. */
  865. static bool
  866. chv_find_best_dpll(const struct intel_limit *limit,
  867. struct intel_crtc_state *crtc_state,
  868. int target, int refclk, struct dpll *match_clock,
  869. struct dpll *best_clock)
  870. {
  871. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  872. struct drm_device *dev = crtc->base.dev;
  873. unsigned int best_error_ppm;
  874. struct dpll clock;
  875. uint64_t m2;
  876. int found = false;
  877. memset(best_clock, 0, sizeof(*best_clock));
  878. best_error_ppm = 1000000;
  879. /*
  880. * Based on hardware doc, the n always set to 1, and m1 always
  881. * set to 2. If requires to support 200Mhz refclk, we need to
  882. * revisit this because n may not 1 anymore.
  883. */
  884. clock.n = 1, clock.m1 = 2;
  885. target *= 5; /* fast clock */
  886. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  887. for (clock.p2 = limit->p2.p2_fast;
  888. clock.p2 >= limit->p2.p2_slow;
  889. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  890. unsigned int error_ppm;
  891. clock.p = clock.p1 * clock.p2;
  892. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  893. clock.n) << 22, refclk * clock.m1);
  894. if (m2 > INT_MAX/clock.m1)
  895. continue;
  896. clock.m2 = m2;
  897. chv_calc_dpll_params(refclk, &clock);
  898. if (!intel_PLL_is_valid(dev, limit, &clock))
  899. continue;
  900. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  901. best_error_ppm, &error_ppm))
  902. continue;
  903. *best_clock = clock;
  904. best_error_ppm = error_ppm;
  905. found = true;
  906. }
  907. }
  908. return found;
  909. }
  910. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  911. struct dpll *best_clock)
  912. {
  913. int refclk = 100000;
  914. const struct intel_limit *limit = &intel_limits_bxt;
  915. return chv_find_best_dpll(limit, crtc_state,
  916. target_clock, refclk, NULL, best_clock);
  917. }
  918. bool intel_crtc_active(struct drm_crtc *crtc)
  919. {
  920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  921. /* Be paranoid as we can arrive here with only partial
  922. * state retrieved from the hardware during setup.
  923. *
  924. * We can ditch the adjusted_mode.crtc_clock check as soon
  925. * as Haswell has gained clock readout/fastboot support.
  926. *
  927. * We can ditch the crtc->primary->fb check as soon as we can
  928. * properly reconstruct framebuffers.
  929. *
  930. * FIXME: The intel_crtc->active here should be switched to
  931. * crtc->state->active once we have proper CRTC states wired up
  932. * for atomic.
  933. */
  934. return intel_crtc->active && crtc->primary->state->fb &&
  935. intel_crtc->config->base.adjusted_mode.crtc_clock;
  936. }
  937. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  938. enum pipe pipe)
  939. {
  940. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  942. return intel_crtc->config->cpu_transcoder;
  943. }
  944. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  945. {
  946. struct drm_i915_private *dev_priv = dev->dev_private;
  947. i915_reg_t reg = PIPEDSL(pipe);
  948. u32 line1, line2;
  949. u32 line_mask;
  950. if (IS_GEN2(dev))
  951. line_mask = DSL_LINEMASK_GEN2;
  952. else
  953. line_mask = DSL_LINEMASK_GEN3;
  954. line1 = I915_READ(reg) & line_mask;
  955. msleep(5);
  956. line2 = I915_READ(reg) & line_mask;
  957. return line1 == line2;
  958. }
  959. /*
  960. * intel_wait_for_pipe_off - wait for pipe to turn off
  961. * @crtc: crtc whose pipe to wait for
  962. *
  963. * After disabling a pipe, we can't wait for vblank in the usual way,
  964. * spinning on the vblank interrupt status bit, since we won't actually
  965. * see an interrupt when the pipe is disabled.
  966. *
  967. * On Gen4 and above:
  968. * wait for the pipe register state bit to turn off
  969. *
  970. * Otherwise:
  971. * wait for the display line value to settle (it usually
  972. * ends up stopping at the start of the next frame).
  973. *
  974. */
  975. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  976. {
  977. struct drm_device *dev = crtc->base.dev;
  978. struct drm_i915_private *dev_priv = dev->dev_private;
  979. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  980. enum pipe pipe = crtc->pipe;
  981. if (INTEL_INFO(dev)->gen >= 4) {
  982. i915_reg_t reg = PIPECONF(cpu_transcoder);
  983. /* Wait for the Pipe State to go off */
  984. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  985. 100))
  986. WARN(1, "pipe_off wait timed out\n");
  987. } else {
  988. /* Wait for the display line to settle */
  989. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  990. WARN(1, "pipe_off wait timed out\n");
  991. }
  992. }
  993. /* Only for pre-ILK configs */
  994. void assert_pll(struct drm_i915_private *dev_priv,
  995. enum pipe pipe, bool state)
  996. {
  997. u32 val;
  998. bool cur_state;
  999. val = I915_READ(DPLL(pipe));
  1000. cur_state = !!(val & DPLL_VCO_ENABLE);
  1001. I915_STATE_WARN(cur_state != state,
  1002. "PLL state assertion failure (expected %s, current %s)\n",
  1003. onoff(state), onoff(cur_state));
  1004. }
  1005. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1006. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1007. {
  1008. u32 val;
  1009. bool cur_state;
  1010. mutex_lock(&dev_priv->sb_lock);
  1011. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1012. mutex_unlock(&dev_priv->sb_lock);
  1013. cur_state = val & DSI_PLL_VCO_EN;
  1014. I915_STATE_WARN(cur_state != state,
  1015. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1016. onoff(state), onoff(cur_state));
  1017. }
  1018. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1019. enum pipe pipe, bool state)
  1020. {
  1021. bool cur_state;
  1022. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1023. pipe);
  1024. if (HAS_DDI(dev_priv)) {
  1025. /* DDI does not have a specific FDI_TX register */
  1026. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1027. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1028. } else {
  1029. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1030. cur_state = !!(val & FDI_TX_ENABLE);
  1031. }
  1032. I915_STATE_WARN(cur_state != state,
  1033. "FDI TX state assertion failure (expected %s, current %s)\n",
  1034. onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1037. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1038. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. u32 val;
  1042. bool cur_state;
  1043. val = I915_READ(FDI_RX_CTL(pipe));
  1044. cur_state = !!(val & FDI_RX_ENABLE);
  1045. I915_STATE_WARN(cur_state != state,
  1046. "FDI RX state assertion failure (expected %s, current %s)\n",
  1047. onoff(state), onoff(cur_state));
  1048. }
  1049. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1050. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1051. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1052. enum pipe pipe)
  1053. {
  1054. u32 val;
  1055. /* ILK FDI PLL is always enabled */
  1056. if (IS_GEN5(dev_priv))
  1057. return;
  1058. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1059. if (HAS_DDI(dev_priv))
  1060. return;
  1061. val = I915_READ(FDI_TX_CTL(pipe));
  1062. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1063. }
  1064. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1065. enum pipe pipe, bool state)
  1066. {
  1067. u32 val;
  1068. bool cur_state;
  1069. val = I915_READ(FDI_RX_CTL(pipe));
  1070. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1071. I915_STATE_WARN(cur_state != state,
  1072. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1073. onoff(state), onoff(cur_state));
  1074. }
  1075. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1076. enum pipe pipe)
  1077. {
  1078. struct drm_device *dev = dev_priv->dev;
  1079. i915_reg_t pp_reg;
  1080. u32 val;
  1081. enum pipe panel_pipe = PIPE_A;
  1082. bool locked = true;
  1083. if (WARN_ON(HAS_DDI(dev)))
  1084. return;
  1085. if (HAS_PCH_SPLIT(dev)) {
  1086. u32 port_sel;
  1087. pp_reg = PCH_PP_CONTROL;
  1088. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1089. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1090. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1091. panel_pipe = PIPE_B;
  1092. /* XXX: else fix for eDP */
  1093. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1094. /* presumably write lock depends on pipe, not port select */
  1095. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1096. panel_pipe = pipe;
  1097. } else {
  1098. pp_reg = PP_CONTROL;
  1099. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1100. panel_pipe = PIPE_B;
  1101. }
  1102. val = I915_READ(pp_reg);
  1103. if (!(val & PANEL_POWER_ON) ||
  1104. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1105. locked = false;
  1106. I915_STATE_WARN(panel_pipe == pipe && locked,
  1107. "panel assertion failure, pipe %c regs locked\n",
  1108. pipe_name(pipe));
  1109. }
  1110. static void assert_cursor(struct drm_i915_private *dev_priv,
  1111. enum pipe pipe, bool state)
  1112. {
  1113. struct drm_device *dev = dev_priv->dev;
  1114. bool cur_state;
  1115. if (IS_845G(dev) || IS_I865G(dev))
  1116. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1117. else
  1118. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1119. I915_STATE_WARN(cur_state != state,
  1120. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1121. pipe_name(pipe), onoff(state), onoff(cur_state));
  1122. }
  1123. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1124. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1125. void assert_pipe(struct drm_i915_private *dev_priv,
  1126. enum pipe pipe, bool state)
  1127. {
  1128. bool cur_state;
  1129. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1130. pipe);
  1131. enum intel_display_power_domain power_domain;
  1132. /* if we need the pipe quirk it must be always on */
  1133. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1134. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1135. state = true;
  1136. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1137. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1138. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1139. cur_state = !!(val & PIPECONF_ENABLE);
  1140. intel_display_power_put(dev_priv, power_domain);
  1141. } else {
  1142. cur_state = false;
  1143. }
  1144. I915_STATE_WARN(cur_state != state,
  1145. "pipe %c assertion failure (expected %s, current %s)\n",
  1146. pipe_name(pipe), onoff(state), onoff(cur_state));
  1147. }
  1148. static void assert_plane(struct drm_i915_private *dev_priv,
  1149. enum plane plane, bool state)
  1150. {
  1151. u32 val;
  1152. bool cur_state;
  1153. val = I915_READ(DSPCNTR(plane));
  1154. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1155. I915_STATE_WARN(cur_state != state,
  1156. "plane %c assertion failure (expected %s, current %s)\n",
  1157. plane_name(plane), onoff(state), onoff(cur_state));
  1158. }
  1159. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1160. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1161. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1162. enum pipe pipe)
  1163. {
  1164. struct drm_device *dev = dev_priv->dev;
  1165. int i;
  1166. /* Primary planes are fixed to pipes on gen4+ */
  1167. if (INTEL_INFO(dev)->gen >= 4) {
  1168. u32 val = I915_READ(DSPCNTR(pipe));
  1169. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1170. "plane %c assertion failure, should be disabled but not\n",
  1171. plane_name(pipe));
  1172. return;
  1173. }
  1174. /* Need to check both planes against the pipe */
  1175. for_each_pipe(dev_priv, i) {
  1176. u32 val = I915_READ(DSPCNTR(i));
  1177. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1178. DISPPLANE_SEL_PIPE_SHIFT;
  1179. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1180. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(i), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1185. enum pipe pipe)
  1186. {
  1187. struct drm_device *dev = dev_priv->dev;
  1188. int sprite;
  1189. if (INTEL_INFO(dev)->gen >= 9) {
  1190. for_each_sprite(dev_priv, pipe, sprite) {
  1191. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1192. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1193. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1194. sprite, pipe_name(pipe));
  1195. }
  1196. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  1197. for_each_sprite(dev_priv, pipe, sprite) {
  1198. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1199. I915_STATE_WARN(val & SP_ENABLE,
  1200. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1201. sprite_name(pipe, sprite), pipe_name(pipe));
  1202. }
  1203. } else if (INTEL_INFO(dev)->gen >= 7) {
  1204. u32 val = I915_READ(SPRCTL(pipe));
  1205. I915_STATE_WARN(val & SPRITE_ENABLE,
  1206. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1207. plane_name(pipe), pipe_name(pipe));
  1208. } else if (INTEL_INFO(dev)->gen >= 5) {
  1209. u32 val = I915_READ(DVSCNTR(pipe));
  1210. I915_STATE_WARN(val & DVS_ENABLE,
  1211. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1212. plane_name(pipe), pipe_name(pipe));
  1213. }
  1214. }
  1215. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1216. {
  1217. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1218. drm_crtc_vblank_put(crtc);
  1219. }
  1220. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1221. enum pipe pipe)
  1222. {
  1223. u32 val;
  1224. bool enabled;
  1225. val = I915_READ(PCH_TRANSCONF(pipe));
  1226. enabled = !!(val & TRANS_ENABLE);
  1227. I915_STATE_WARN(enabled,
  1228. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1229. pipe_name(pipe));
  1230. }
  1231. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 port_sel, u32 val)
  1233. {
  1234. if ((val & DP_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1238. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1239. return false;
  1240. } else if (IS_CHERRYVIEW(dev_priv)) {
  1241. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & SDVO_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1256. return false;
  1257. } else if (IS_CHERRYVIEW(dev_priv)) {
  1258. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1259. return false;
  1260. } else {
  1261. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1262. return false;
  1263. }
  1264. return true;
  1265. }
  1266. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1267. enum pipe pipe, u32 val)
  1268. {
  1269. if ((val & LVDS_PORT_EN) == 0)
  1270. return false;
  1271. if (HAS_PCH_CPT(dev_priv)) {
  1272. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1273. return false;
  1274. } else {
  1275. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1276. return false;
  1277. }
  1278. return true;
  1279. }
  1280. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1281. enum pipe pipe, u32 val)
  1282. {
  1283. if ((val & ADPA_DAC_ENABLE) == 0)
  1284. return false;
  1285. if (HAS_PCH_CPT(dev_priv)) {
  1286. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1287. return false;
  1288. } else {
  1289. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1290. return false;
  1291. }
  1292. return true;
  1293. }
  1294. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1295. enum pipe pipe, i915_reg_t reg,
  1296. u32 port_sel)
  1297. {
  1298. u32 val = I915_READ(reg);
  1299. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1300. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1301. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1302. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1303. && (val & DP_PIPEB_SELECT),
  1304. "IBX PCH dp port still using transcoder B\n");
  1305. }
  1306. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1307. enum pipe pipe, i915_reg_t reg)
  1308. {
  1309. u32 val = I915_READ(reg);
  1310. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1311. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1312. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1313. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1314. && (val & SDVO_PIPE_B_SELECT),
  1315. "IBX PCH hdmi port still using transcoder B\n");
  1316. }
  1317. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1318. enum pipe pipe)
  1319. {
  1320. u32 val;
  1321. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1322. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1323. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1324. val = I915_READ(PCH_ADPA);
  1325. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1326. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1327. pipe_name(pipe));
  1328. val = I915_READ(PCH_LVDS);
  1329. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1330. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1331. pipe_name(pipe));
  1332. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1333. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1334. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1335. }
  1336. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1337. const struct intel_crtc_state *pipe_config)
  1338. {
  1339. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1340. enum pipe pipe = crtc->pipe;
  1341. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1342. POSTING_READ(DPLL(pipe));
  1343. udelay(150);
  1344. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1345. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1346. }
  1347. static void vlv_enable_pll(struct intel_crtc *crtc,
  1348. const struct intel_crtc_state *pipe_config)
  1349. {
  1350. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1351. enum pipe pipe = crtc->pipe;
  1352. assert_pipe_disabled(dev_priv, pipe);
  1353. /* PLL is protected by panel, make sure we can write it */
  1354. assert_panel_unlocked(dev_priv, pipe);
  1355. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1356. _vlv_enable_pll(crtc, pipe_config);
  1357. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1358. POSTING_READ(DPLL_MD(pipe));
  1359. }
  1360. static void _chv_enable_pll(struct intel_crtc *crtc,
  1361. const struct intel_crtc_state *pipe_config)
  1362. {
  1363. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1364. enum pipe pipe = crtc->pipe;
  1365. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1366. u32 tmp;
  1367. mutex_lock(&dev_priv->sb_lock);
  1368. /* Enable back the 10bit clock to display controller */
  1369. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1370. tmp |= DPIO_DCLKP_EN;
  1371. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1372. mutex_unlock(&dev_priv->sb_lock);
  1373. /*
  1374. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1375. */
  1376. udelay(1);
  1377. /* Enable PLL */
  1378. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1379. /* Check PLL is locked */
  1380. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1381. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1382. }
  1383. static void chv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1387. enum pipe pipe = crtc->pipe;
  1388. assert_pipe_disabled(dev_priv, pipe);
  1389. /* PLL is protected by panel, make sure we can write it */
  1390. assert_panel_unlocked(dev_priv, pipe);
  1391. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1392. _chv_enable_pll(crtc, pipe_config);
  1393. if (pipe != PIPE_A) {
  1394. /*
  1395. * WaPixelRepeatModeFixForC0:chv
  1396. *
  1397. * DPLLCMD is AWOL. Use chicken bits to propagate
  1398. * the value from DPLLBMD to either pipe B or C.
  1399. */
  1400. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1401. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1402. I915_WRITE(CBR4_VLV, 0);
  1403. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1404. /*
  1405. * DPLLB VGA mode also seems to cause problems.
  1406. * We should always have it disabled.
  1407. */
  1408. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1409. } else {
  1410. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1411. POSTING_READ(DPLL_MD(pipe));
  1412. }
  1413. }
  1414. static int intel_num_dvo_pipes(struct drm_device *dev)
  1415. {
  1416. struct intel_crtc *crtc;
  1417. int count = 0;
  1418. for_each_intel_crtc(dev, crtc)
  1419. count += crtc->base.state->active &&
  1420. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1421. return count;
  1422. }
  1423. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1424. {
  1425. struct drm_device *dev = crtc->base.dev;
  1426. struct drm_i915_private *dev_priv = dev->dev_private;
  1427. i915_reg_t reg = DPLL(crtc->pipe);
  1428. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1429. assert_pipe_disabled(dev_priv, crtc->pipe);
  1430. /* PLL is protected by panel, make sure we can write it */
  1431. if (IS_MOBILE(dev) && !IS_I830(dev))
  1432. assert_panel_unlocked(dev_priv, crtc->pipe);
  1433. /* Enable DVO 2x clock on both PLLs if necessary */
  1434. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1435. /*
  1436. * It appears to be important that we don't enable this
  1437. * for the current pipe before otherwise configuring the
  1438. * PLL. No idea how this should be handled if multiple
  1439. * DVO outputs are enabled simultaneosly.
  1440. */
  1441. dpll |= DPLL_DVO_2X_MODE;
  1442. I915_WRITE(DPLL(!crtc->pipe),
  1443. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1444. }
  1445. /*
  1446. * Apparently we need to have VGA mode enabled prior to changing
  1447. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1448. * dividers, even though the register value does change.
  1449. */
  1450. I915_WRITE(reg, 0);
  1451. I915_WRITE(reg, dpll);
  1452. /* Wait for the clocks to stabilize. */
  1453. POSTING_READ(reg);
  1454. udelay(150);
  1455. if (INTEL_INFO(dev)->gen >= 4) {
  1456. I915_WRITE(DPLL_MD(crtc->pipe),
  1457. crtc->config->dpll_hw_state.dpll_md);
  1458. } else {
  1459. /* The pixel multiplier can only be updated once the
  1460. * DPLL is enabled and the clocks are stable.
  1461. *
  1462. * So write it again.
  1463. */
  1464. I915_WRITE(reg, dpll);
  1465. }
  1466. /* We do this three times for luck */
  1467. I915_WRITE(reg, dpll);
  1468. POSTING_READ(reg);
  1469. udelay(150); /* wait for warmup */
  1470. I915_WRITE(reg, dpll);
  1471. POSTING_READ(reg);
  1472. udelay(150); /* wait for warmup */
  1473. I915_WRITE(reg, dpll);
  1474. POSTING_READ(reg);
  1475. udelay(150); /* wait for warmup */
  1476. }
  1477. /**
  1478. * i9xx_disable_pll - disable a PLL
  1479. * @dev_priv: i915 private structure
  1480. * @pipe: pipe PLL to disable
  1481. *
  1482. * Disable the PLL for @pipe, making sure the pipe is off first.
  1483. *
  1484. * Note! This is for pre-ILK only.
  1485. */
  1486. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1487. {
  1488. struct drm_device *dev = crtc->base.dev;
  1489. struct drm_i915_private *dev_priv = dev->dev_private;
  1490. enum pipe pipe = crtc->pipe;
  1491. /* Disable DVO 2x clock on both PLLs if necessary */
  1492. if (IS_I830(dev) &&
  1493. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1494. !intel_num_dvo_pipes(dev)) {
  1495. I915_WRITE(DPLL(PIPE_B),
  1496. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1497. I915_WRITE(DPLL(PIPE_A),
  1498. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1499. }
  1500. /* Don't disable pipe or pipe PLLs if needed */
  1501. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1502. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1503. return;
  1504. /* Make sure the pipe isn't still relying on us */
  1505. assert_pipe_disabled(dev_priv, pipe);
  1506. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1507. POSTING_READ(DPLL(pipe));
  1508. }
  1509. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1510. {
  1511. u32 val;
  1512. /* Make sure the pipe isn't still relying on us */
  1513. assert_pipe_disabled(dev_priv, pipe);
  1514. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1515. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1516. if (pipe != PIPE_A)
  1517. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1518. I915_WRITE(DPLL(pipe), val);
  1519. POSTING_READ(DPLL(pipe));
  1520. }
  1521. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1522. {
  1523. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1524. u32 val;
  1525. /* Make sure the pipe isn't still relying on us */
  1526. assert_pipe_disabled(dev_priv, pipe);
  1527. val = DPLL_SSC_REF_CLK_CHV |
  1528. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1529. if (pipe != PIPE_A)
  1530. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1531. I915_WRITE(DPLL(pipe), val);
  1532. POSTING_READ(DPLL(pipe));
  1533. mutex_lock(&dev_priv->sb_lock);
  1534. /* Disable 10bit clock to display controller */
  1535. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1536. val &= ~DPIO_DCLKP_EN;
  1537. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1538. mutex_unlock(&dev_priv->sb_lock);
  1539. }
  1540. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1541. struct intel_digital_port *dport,
  1542. unsigned int expected_mask)
  1543. {
  1544. u32 port_mask;
  1545. i915_reg_t dpll_reg;
  1546. switch (dport->port) {
  1547. case PORT_B:
  1548. port_mask = DPLL_PORTB_READY_MASK;
  1549. dpll_reg = DPLL(0);
  1550. break;
  1551. case PORT_C:
  1552. port_mask = DPLL_PORTC_READY_MASK;
  1553. dpll_reg = DPLL(0);
  1554. expected_mask <<= 4;
  1555. break;
  1556. case PORT_D:
  1557. port_mask = DPLL_PORTD_READY_MASK;
  1558. dpll_reg = DPIO_PHY_STATUS;
  1559. break;
  1560. default:
  1561. BUG();
  1562. }
  1563. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1564. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1565. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1566. }
  1567. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1568. enum pipe pipe)
  1569. {
  1570. struct drm_device *dev = dev_priv->dev;
  1571. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1572. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1573. i915_reg_t reg;
  1574. uint32_t val, pipeconf_val;
  1575. /* Make sure PCH DPLL is enabled */
  1576. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1577. /* FDI must be feeding us bits for PCH ports */
  1578. assert_fdi_tx_enabled(dev_priv, pipe);
  1579. assert_fdi_rx_enabled(dev_priv, pipe);
  1580. if (HAS_PCH_CPT(dev)) {
  1581. /* Workaround: Set the timing override bit before enabling the
  1582. * pch transcoder. */
  1583. reg = TRANS_CHICKEN2(pipe);
  1584. val = I915_READ(reg);
  1585. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1586. I915_WRITE(reg, val);
  1587. }
  1588. reg = PCH_TRANSCONF(pipe);
  1589. val = I915_READ(reg);
  1590. pipeconf_val = I915_READ(PIPECONF(pipe));
  1591. if (HAS_PCH_IBX(dev_priv)) {
  1592. /*
  1593. * Make the BPC in transcoder be consistent with
  1594. * that in pipeconf reg. For HDMI we must use 8bpc
  1595. * here for both 8bpc and 12bpc.
  1596. */
  1597. val &= ~PIPECONF_BPC_MASK;
  1598. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1599. val |= PIPECONF_8BPC;
  1600. else
  1601. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1602. }
  1603. val &= ~TRANS_INTERLACE_MASK;
  1604. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1605. if (HAS_PCH_IBX(dev_priv) &&
  1606. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1607. val |= TRANS_LEGACY_INTERLACED_ILK;
  1608. else
  1609. val |= TRANS_INTERLACED;
  1610. else
  1611. val |= TRANS_PROGRESSIVE;
  1612. I915_WRITE(reg, val | TRANS_ENABLE);
  1613. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1614. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1615. }
  1616. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1617. enum transcoder cpu_transcoder)
  1618. {
  1619. u32 val, pipeconf_val;
  1620. /* FDI must be feeding us bits for PCH ports */
  1621. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1622. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1623. /* Workaround: set timing override bit. */
  1624. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1625. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1626. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1627. val = TRANS_ENABLE;
  1628. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1629. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1630. PIPECONF_INTERLACED_ILK)
  1631. val |= TRANS_INTERLACED;
  1632. else
  1633. val |= TRANS_PROGRESSIVE;
  1634. I915_WRITE(LPT_TRANSCONF, val);
  1635. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1636. DRM_ERROR("Failed to enable PCH transcoder\n");
  1637. }
  1638. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1639. enum pipe pipe)
  1640. {
  1641. struct drm_device *dev = dev_priv->dev;
  1642. i915_reg_t reg;
  1643. uint32_t val;
  1644. /* FDI relies on the transcoder */
  1645. assert_fdi_tx_disabled(dev_priv, pipe);
  1646. assert_fdi_rx_disabled(dev_priv, pipe);
  1647. /* Ports must be off as well */
  1648. assert_pch_ports_disabled(dev_priv, pipe);
  1649. reg = PCH_TRANSCONF(pipe);
  1650. val = I915_READ(reg);
  1651. val &= ~TRANS_ENABLE;
  1652. I915_WRITE(reg, val);
  1653. /* wait for PCH transcoder off, transcoder state */
  1654. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1655. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1656. if (HAS_PCH_CPT(dev)) {
  1657. /* Workaround: Clear the timing override chicken bit again. */
  1658. reg = TRANS_CHICKEN2(pipe);
  1659. val = I915_READ(reg);
  1660. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1661. I915_WRITE(reg, val);
  1662. }
  1663. }
  1664. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1665. {
  1666. u32 val;
  1667. val = I915_READ(LPT_TRANSCONF);
  1668. val &= ~TRANS_ENABLE;
  1669. I915_WRITE(LPT_TRANSCONF, val);
  1670. /* wait for PCH transcoder off, transcoder state */
  1671. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1672. DRM_ERROR("Failed to disable PCH transcoder\n");
  1673. /* Workaround: clear timing override bit. */
  1674. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1675. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1676. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1677. }
  1678. /**
  1679. * intel_enable_pipe - enable a pipe, asserting requirements
  1680. * @crtc: crtc responsible for the pipe
  1681. *
  1682. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1683. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1684. */
  1685. static void intel_enable_pipe(struct intel_crtc *crtc)
  1686. {
  1687. struct drm_device *dev = crtc->base.dev;
  1688. struct drm_i915_private *dev_priv = dev->dev_private;
  1689. enum pipe pipe = crtc->pipe;
  1690. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1691. enum pipe pch_transcoder;
  1692. i915_reg_t reg;
  1693. u32 val;
  1694. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1695. assert_planes_disabled(dev_priv, pipe);
  1696. assert_cursor_disabled(dev_priv, pipe);
  1697. assert_sprites_disabled(dev_priv, pipe);
  1698. if (HAS_PCH_LPT(dev_priv))
  1699. pch_transcoder = TRANSCODER_A;
  1700. else
  1701. pch_transcoder = pipe;
  1702. /*
  1703. * A pipe without a PLL won't actually be able to drive bits from
  1704. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1705. * need the check.
  1706. */
  1707. if (HAS_GMCH_DISPLAY(dev_priv))
  1708. if (crtc->config->has_dsi_encoder)
  1709. assert_dsi_pll_enabled(dev_priv);
  1710. else
  1711. assert_pll_enabled(dev_priv, pipe);
  1712. else {
  1713. if (crtc->config->has_pch_encoder) {
  1714. /* if driving the PCH, we need FDI enabled */
  1715. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1716. assert_fdi_tx_pll_enabled(dev_priv,
  1717. (enum pipe) cpu_transcoder);
  1718. }
  1719. /* FIXME: assert CPU port conditions for SNB+ */
  1720. }
  1721. reg = PIPECONF(cpu_transcoder);
  1722. val = I915_READ(reg);
  1723. if (val & PIPECONF_ENABLE) {
  1724. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1725. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1726. return;
  1727. }
  1728. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1729. POSTING_READ(reg);
  1730. /*
  1731. * Until the pipe starts DSL will read as 0, which would cause
  1732. * an apparent vblank timestamp jump, which messes up also the
  1733. * frame count when it's derived from the timestamps. So let's
  1734. * wait for the pipe to start properly before we call
  1735. * drm_crtc_vblank_on()
  1736. */
  1737. if (dev->max_vblank_count == 0 &&
  1738. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1739. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1740. }
  1741. /**
  1742. * intel_disable_pipe - disable a pipe, asserting requirements
  1743. * @crtc: crtc whose pipes is to be disabled
  1744. *
  1745. * Disable the pipe of @crtc, making sure that various hardware
  1746. * specific requirements are met, if applicable, e.g. plane
  1747. * disabled, panel fitter off, etc.
  1748. *
  1749. * Will wait until the pipe has shut down before returning.
  1750. */
  1751. static void intel_disable_pipe(struct intel_crtc *crtc)
  1752. {
  1753. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1754. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1755. enum pipe pipe = crtc->pipe;
  1756. i915_reg_t reg;
  1757. u32 val;
  1758. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1759. /*
  1760. * Make sure planes won't keep trying to pump pixels to us,
  1761. * or we might hang the display.
  1762. */
  1763. assert_planes_disabled(dev_priv, pipe);
  1764. assert_cursor_disabled(dev_priv, pipe);
  1765. assert_sprites_disabled(dev_priv, pipe);
  1766. reg = PIPECONF(cpu_transcoder);
  1767. val = I915_READ(reg);
  1768. if ((val & PIPECONF_ENABLE) == 0)
  1769. return;
  1770. /*
  1771. * Double wide has implications for planes
  1772. * so best keep it disabled when not needed.
  1773. */
  1774. if (crtc->config->double_wide)
  1775. val &= ~PIPECONF_DOUBLE_WIDE;
  1776. /* Don't disable pipe or pipe PLLs if needed */
  1777. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1778. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1779. val &= ~PIPECONF_ENABLE;
  1780. I915_WRITE(reg, val);
  1781. if ((val & PIPECONF_ENABLE) == 0)
  1782. intel_wait_for_pipe_off(crtc);
  1783. }
  1784. static bool need_vtd_wa(struct drm_device *dev)
  1785. {
  1786. #ifdef CONFIG_INTEL_IOMMU
  1787. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1788. return true;
  1789. #endif
  1790. return false;
  1791. }
  1792. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1793. {
  1794. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1795. }
  1796. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1797. uint64_t fb_modifier, unsigned int cpp)
  1798. {
  1799. switch (fb_modifier) {
  1800. case DRM_FORMAT_MOD_NONE:
  1801. return cpp;
  1802. case I915_FORMAT_MOD_X_TILED:
  1803. if (IS_GEN2(dev_priv))
  1804. return 128;
  1805. else
  1806. return 512;
  1807. case I915_FORMAT_MOD_Y_TILED:
  1808. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1809. return 128;
  1810. else
  1811. return 512;
  1812. case I915_FORMAT_MOD_Yf_TILED:
  1813. switch (cpp) {
  1814. case 1:
  1815. return 64;
  1816. case 2:
  1817. case 4:
  1818. return 128;
  1819. case 8:
  1820. case 16:
  1821. return 256;
  1822. default:
  1823. MISSING_CASE(cpp);
  1824. return cpp;
  1825. }
  1826. break;
  1827. default:
  1828. MISSING_CASE(fb_modifier);
  1829. return cpp;
  1830. }
  1831. }
  1832. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1833. uint64_t fb_modifier, unsigned int cpp)
  1834. {
  1835. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1836. return 1;
  1837. else
  1838. return intel_tile_size(dev_priv) /
  1839. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1840. }
  1841. /* Return the tile dimensions in pixel units */
  1842. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1843. unsigned int *tile_width,
  1844. unsigned int *tile_height,
  1845. uint64_t fb_modifier,
  1846. unsigned int cpp)
  1847. {
  1848. unsigned int tile_width_bytes =
  1849. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1850. *tile_width = tile_width_bytes / cpp;
  1851. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1852. }
  1853. unsigned int
  1854. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1855. uint32_t pixel_format, uint64_t fb_modifier)
  1856. {
  1857. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1858. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1859. return ALIGN(height, tile_height);
  1860. }
  1861. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1862. {
  1863. unsigned int size = 0;
  1864. int i;
  1865. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1866. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1867. return size;
  1868. }
  1869. static void
  1870. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1871. const struct drm_framebuffer *fb,
  1872. unsigned int rotation)
  1873. {
  1874. if (intel_rotation_90_or_270(rotation)) {
  1875. *view = i915_ggtt_view_rotated;
  1876. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1877. } else {
  1878. *view = i915_ggtt_view_normal;
  1879. }
  1880. }
  1881. static void
  1882. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  1883. struct drm_framebuffer *fb)
  1884. {
  1885. struct intel_rotation_info *info = &to_intel_framebuffer(fb)->rot_info;
  1886. unsigned int tile_size, tile_width, tile_height, cpp;
  1887. tile_size = intel_tile_size(dev_priv);
  1888. cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  1889. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1890. fb->modifier[0], cpp);
  1891. info->plane[0].width = DIV_ROUND_UP(fb->pitches[0], tile_width * cpp);
  1892. info->plane[0].height = DIV_ROUND_UP(fb->height, tile_height);
  1893. if (info->pixel_format == DRM_FORMAT_NV12) {
  1894. cpp = drm_format_plane_cpp(fb->pixel_format, 1);
  1895. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1896. fb->modifier[1], cpp);
  1897. info->uv_offset = fb->offsets[1];
  1898. info->plane[1].width = DIV_ROUND_UP(fb->pitches[1], tile_width * cpp);
  1899. info->plane[1].height = DIV_ROUND_UP(fb->height / 2, tile_height);
  1900. }
  1901. }
  1902. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1903. {
  1904. if (INTEL_INFO(dev_priv)->gen >= 9)
  1905. return 256 * 1024;
  1906. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1907. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1908. return 128 * 1024;
  1909. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1910. return 4 * 1024;
  1911. else
  1912. return 0;
  1913. }
  1914. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1915. uint64_t fb_modifier)
  1916. {
  1917. switch (fb_modifier) {
  1918. case DRM_FORMAT_MOD_NONE:
  1919. return intel_linear_alignment(dev_priv);
  1920. case I915_FORMAT_MOD_X_TILED:
  1921. if (INTEL_INFO(dev_priv)->gen >= 9)
  1922. return 256 * 1024;
  1923. return 0;
  1924. case I915_FORMAT_MOD_Y_TILED:
  1925. case I915_FORMAT_MOD_Yf_TILED:
  1926. return 1 * 1024 * 1024;
  1927. default:
  1928. MISSING_CASE(fb_modifier);
  1929. return 0;
  1930. }
  1931. }
  1932. int
  1933. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
  1934. unsigned int rotation)
  1935. {
  1936. struct drm_device *dev = fb->dev;
  1937. struct drm_i915_private *dev_priv = dev->dev_private;
  1938. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1939. struct i915_ggtt_view view;
  1940. u32 alignment;
  1941. int ret;
  1942. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1943. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1944. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1945. /* Note that the w/a also requires 64 PTE of padding following the
  1946. * bo. We currently fill all unused PTE with the shadow page and so
  1947. * we should always have valid PTE following the scanout preventing
  1948. * the VT-d warning.
  1949. */
  1950. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1951. alignment = 256 * 1024;
  1952. /*
  1953. * Global gtt pte registers are special registers which actually forward
  1954. * writes to a chunk of system memory. Which means that there is no risk
  1955. * that the register values disappear as soon as we call
  1956. * intel_runtime_pm_put(), so it is correct to wrap only the
  1957. * pin/unpin/fence and not more.
  1958. */
  1959. intel_runtime_pm_get(dev_priv);
  1960. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  1961. &view);
  1962. if (ret)
  1963. goto err_pm;
  1964. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1965. * fence, whereas 965+ only requires a fence if using
  1966. * framebuffer compression. For simplicity, we always install
  1967. * a fence as the cost is not that onerous.
  1968. */
  1969. if (view.type == I915_GGTT_VIEW_NORMAL) {
  1970. ret = i915_gem_object_get_fence(obj);
  1971. if (ret == -EDEADLK) {
  1972. /*
  1973. * -EDEADLK means there are no free fences
  1974. * no pending flips.
  1975. *
  1976. * This is propagated to atomic, but it uses
  1977. * -EDEADLK to force a locking recovery, so
  1978. * change the returned error to -EBUSY.
  1979. */
  1980. ret = -EBUSY;
  1981. goto err_unpin;
  1982. } else if (ret)
  1983. goto err_unpin;
  1984. i915_gem_object_pin_fence(obj);
  1985. }
  1986. intel_runtime_pm_put(dev_priv);
  1987. return 0;
  1988. err_unpin:
  1989. i915_gem_object_unpin_from_display_plane(obj, &view);
  1990. err_pm:
  1991. intel_runtime_pm_put(dev_priv);
  1992. return ret;
  1993. }
  1994. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1995. {
  1996. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1997. struct i915_ggtt_view view;
  1998. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1999. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2000. if (view.type == I915_GGTT_VIEW_NORMAL)
  2001. i915_gem_object_unpin_fence(obj);
  2002. i915_gem_object_unpin_from_display_plane(obj, &view);
  2003. }
  2004. /*
  2005. * Adjust the tile offset by moving the difference into
  2006. * the x/y offsets.
  2007. *
  2008. * Input tile dimensions and pitch must already be
  2009. * rotated to match x and y, and in pixel units.
  2010. */
  2011. static u32 intel_adjust_tile_offset(int *x, int *y,
  2012. unsigned int tile_width,
  2013. unsigned int tile_height,
  2014. unsigned int tile_size,
  2015. unsigned int pitch_tiles,
  2016. u32 old_offset,
  2017. u32 new_offset)
  2018. {
  2019. unsigned int tiles;
  2020. WARN_ON(old_offset & (tile_size - 1));
  2021. WARN_ON(new_offset & (tile_size - 1));
  2022. WARN_ON(new_offset > old_offset);
  2023. tiles = (old_offset - new_offset) / tile_size;
  2024. *y += tiles / pitch_tiles * tile_height;
  2025. *x += tiles % pitch_tiles * tile_width;
  2026. return new_offset;
  2027. }
  2028. /*
  2029. * Computes the linear offset to the base tile and adjusts
  2030. * x, y. bytes per pixel is assumed to be a power-of-two.
  2031. *
  2032. * In the 90/270 rotated case, x and y are assumed
  2033. * to be already rotated to match the rotated GTT view, and
  2034. * pitch is the tile_height aligned framebuffer height.
  2035. */
  2036. u32 intel_compute_tile_offset(int *x, int *y,
  2037. const struct drm_framebuffer *fb, int plane,
  2038. unsigned int pitch,
  2039. unsigned int rotation)
  2040. {
  2041. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2042. uint64_t fb_modifier = fb->modifier[plane];
  2043. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2044. u32 offset, offset_aligned, alignment;
  2045. alignment = intel_surf_alignment(dev_priv, fb_modifier);
  2046. if (alignment)
  2047. alignment--;
  2048. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2049. unsigned int tile_size, tile_width, tile_height;
  2050. unsigned int tile_rows, tiles, pitch_tiles;
  2051. tile_size = intel_tile_size(dev_priv);
  2052. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2053. fb_modifier, cpp);
  2054. if (intel_rotation_90_or_270(rotation)) {
  2055. pitch_tiles = pitch / tile_height;
  2056. swap(tile_width, tile_height);
  2057. } else {
  2058. pitch_tiles = pitch / (tile_width * cpp);
  2059. }
  2060. tile_rows = *y / tile_height;
  2061. *y %= tile_height;
  2062. tiles = *x / tile_width;
  2063. *x %= tile_width;
  2064. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2065. offset_aligned = offset & ~alignment;
  2066. intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2067. tile_size, pitch_tiles,
  2068. offset, offset_aligned);
  2069. } else {
  2070. offset = *y * pitch + *x * cpp;
  2071. offset_aligned = offset & ~alignment;
  2072. *y = (offset & alignment) / pitch;
  2073. *x = ((offset & alignment) - *y * pitch) / cpp;
  2074. }
  2075. return offset_aligned;
  2076. }
  2077. static int i9xx_format_to_fourcc(int format)
  2078. {
  2079. switch (format) {
  2080. case DISPPLANE_8BPP:
  2081. return DRM_FORMAT_C8;
  2082. case DISPPLANE_BGRX555:
  2083. return DRM_FORMAT_XRGB1555;
  2084. case DISPPLANE_BGRX565:
  2085. return DRM_FORMAT_RGB565;
  2086. default:
  2087. case DISPPLANE_BGRX888:
  2088. return DRM_FORMAT_XRGB8888;
  2089. case DISPPLANE_RGBX888:
  2090. return DRM_FORMAT_XBGR8888;
  2091. case DISPPLANE_BGRX101010:
  2092. return DRM_FORMAT_XRGB2101010;
  2093. case DISPPLANE_RGBX101010:
  2094. return DRM_FORMAT_XBGR2101010;
  2095. }
  2096. }
  2097. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2098. {
  2099. switch (format) {
  2100. case PLANE_CTL_FORMAT_RGB_565:
  2101. return DRM_FORMAT_RGB565;
  2102. default:
  2103. case PLANE_CTL_FORMAT_XRGB_8888:
  2104. if (rgb_order) {
  2105. if (alpha)
  2106. return DRM_FORMAT_ABGR8888;
  2107. else
  2108. return DRM_FORMAT_XBGR8888;
  2109. } else {
  2110. if (alpha)
  2111. return DRM_FORMAT_ARGB8888;
  2112. else
  2113. return DRM_FORMAT_XRGB8888;
  2114. }
  2115. case PLANE_CTL_FORMAT_XRGB_2101010:
  2116. if (rgb_order)
  2117. return DRM_FORMAT_XBGR2101010;
  2118. else
  2119. return DRM_FORMAT_XRGB2101010;
  2120. }
  2121. }
  2122. static bool
  2123. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2124. struct intel_initial_plane_config *plane_config)
  2125. {
  2126. struct drm_device *dev = crtc->base.dev;
  2127. struct drm_i915_private *dev_priv = to_i915(dev);
  2128. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2129. struct drm_i915_gem_object *obj = NULL;
  2130. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2131. struct drm_framebuffer *fb = &plane_config->fb->base;
  2132. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2133. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2134. PAGE_SIZE);
  2135. size_aligned -= base_aligned;
  2136. if (plane_config->size == 0)
  2137. return false;
  2138. /* If the FB is too big, just don't use it since fbdev is not very
  2139. * important and we should probably use that space with FBC or other
  2140. * features. */
  2141. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2142. return false;
  2143. mutex_lock(&dev->struct_mutex);
  2144. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2145. base_aligned,
  2146. base_aligned,
  2147. size_aligned);
  2148. if (!obj) {
  2149. mutex_unlock(&dev->struct_mutex);
  2150. return false;
  2151. }
  2152. obj->tiling_mode = plane_config->tiling;
  2153. if (obj->tiling_mode == I915_TILING_X)
  2154. obj->stride = fb->pitches[0];
  2155. mode_cmd.pixel_format = fb->pixel_format;
  2156. mode_cmd.width = fb->width;
  2157. mode_cmd.height = fb->height;
  2158. mode_cmd.pitches[0] = fb->pitches[0];
  2159. mode_cmd.modifier[0] = fb->modifier[0];
  2160. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2161. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2162. &mode_cmd, obj)) {
  2163. DRM_DEBUG_KMS("intel fb init failed\n");
  2164. goto out_unref_obj;
  2165. }
  2166. mutex_unlock(&dev->struct_mutex);
  2167. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2168. return true;
  2169. out_unref_obj:
  2170. drm_gem_object_unreference(&obj->base);
  2171. mutex_unlock(&dev->struct_mutex);
  2172. return false;
  2173. }
  2174. static void
  2175. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2176. struct intel_initial_plane_config *plane_config)
  2177. {
  2178. struct drm_device *dev = intel_crtc->base.dev;
  2179. struct drm_i915_private *dev_priv = dev->dev_private;
  2180. struct drm_crtc *c;
  2181. struct intel_crtc *i;
  2182. struct drm_i915_gem_object *obj;
  2183. struct drm_plane *primary = intel_crtc->base.primary;
  2184. struct drm_plane_state *plane_state = primary->state;
  2185. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2186. struct intel_plane *intel_plane = to_intel_plane(primary);
  2187. struct intel_plane_state *intel_state =
  2188. to_intel_plane_state(plane_state);
  2189. struct drm_framebuffer *fb;
  2190. if (!plane_config->fb)
  2191. return;
  2192. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2193. fb = &plane_config->fb->base;
  2194. goto valid_fb;
  2195. }
  2196. kfree(plane_config->fb);
  2197. /*
  2198. * Failed to alloc the obj, check to see if we should share
  2199. * an fb with another CRTC instead
  2200. */
  2201. for_each_crtc(dev, c) {
  2202. i = to_intel_crtc(c);
  2203. if (c == &intel_crtc->base)
  2204. continue;
  2205. if (!i->active)
  2206. continue;
  2207. fb = c->primary->fb;
  2208. if (!fb)
  2209. continue;
  2210. obj = intel_fb_obj(fb);
  2211. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2212. drm_framebuffer_reference(fb);
  2213. goto valid_fb;
  2214. }
  2215. }
  2216. /*
  2217. * We've failed to reconstruct the BIOS FB. Current display state
  2218. * indicates that the primary plane is visible, but has a NULL FB,
  2219. * which will lead to problems later if we don't fix it up. The
  2220. * simplest solution is to just disable the primary plane now and
  2221. * pretend the BIOS never had it enabled.
  2222. */
  2223. to_intel_plane_state(plane_state)->visible = false;
  2224. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2225. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2226. intel_plane->disable_plane(primary, &intel_crtc->base);
  2227. return;
  2228. valid_fb:
  2229. plane_state->src_x = 0;
  2230. plane_state->src_y = 0;
  2231. plane_state->src_w = fb->width << 16;
  2232. plane_state->src_h = fb->height << 16;
  2233. plane_state->crtc_x = 0;
  2234. plane_state->crtc_y = 0;
  2235. plane_state->crtc_w = fb->width;
  2236. plane_state->crtc_h = fb->height;
  2237. intel_state->src.x1 = plane_state->src_x;
  2238. intel_state->src.y1 = plane_state->src_y;
  2239. intel_state->src.x2 = plane_state->src_x + plane_state->src_w;
  2240. intel_state->src.y2 = plane_state->src_y + plane_state->src_h;
  2241. intel_state->dst.x1 = plane_state->crtc_x;
  2242. intel_state->dst.y1 = plane_state->crtc_y;
  2243. intel_state->dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2244. intel_state->dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2245. obj = intel_fb_obj(fb);
  2246. if (obj->tiling_mode != I915_TILING_NONE)
  2247. dev_priv->preserve_bios_swizzle = true;
  2248. drm_framebuffer_reference(fb);
  2249. primary->fb = primary->state->fb = fb;
  2250. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2251. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2252. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2253. }
  2254. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2255. const struct intel_crtc_state *crtc_state,
  2256. const struct intel_plane_state *plane_state)
  2257. {
  2258. struct drm_device *dev = primary->dev;
  2259. struct drm_i915_private *dev_priv = dev->dev_private;
  2260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2261. struct drm_framebuffer *fb = plane_state->base.fb;
  2262. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2263. int plane = intel_crtc->plane;
  2264. u32 linear_offset;
  2265. u32 dspcntr;
  2266. i915_reg_t reg = DSPCNTR(plane);
  2267. unsigned int rotation = plane_state->base.rotation;
  2268. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2269. int x = plane_state->src.x1 >> 16;
  2270. int y = plane_state->src.y1 >> 16;
  2271. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2272. dspcntr |= DISPLAY_PLANE_ENABLE;
  2273. if (INTEL_INFO(dev)->gen < 4) {
  2274. if (intel_crtc->pipe == PIPE_B)
  2275. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2276. /* pipesrc and dspsize control the size that is scaled from,
  2277. * which should always be the user's requested size.
  2278. */
  2279. I915_WRITE(DSPSIZE(plane),
  2280. ((crtc_state->pipe_src_h - 1) << 16) |
  2281. (crtc_state->pipe_src_w - 1));
  2282. I915_WRITE(DSPPOS(plane), 0);
  2283. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2284. I915_WRITE(PRIMSIZE(plane),
  2285. ((crtc_state->pipe_src_h - 1) << 16) |
  2286. (crtc_state->pipe_src_w - 1));
  2287. I915_WRITE(PRIMPOS(plane), 0);
  2288. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2289. }
  2290. switch (fb->pixel_format) {
  2291. case DRM_FORMAT_C8:
  2292. dspcntr |= DISPPLANE_8BPP;
  2293. break;
  2294. case DRM_FORMAT_XRGB1555:
  2295. dspcntr |= DISPPLANE_BGRX555;
  2296. break;
  2297. case DRM_FORMAT_RGB565:
  2298. dspcntr |= DISPPLANE_BGRX565;
  2299. break;
  2300. case DRM_FORMAT_XRGB8888:
  2301. dspcntr |= DISPPLANE_BGRX888;
  2302. break;
  2303. case DRM_FORMAT_XBGR8888:
  2304. dspcntr |= DISPPLANE_RGBX888;
  2305. break;
  2306. case DRM_FORMAT_XRGB2101010:
  2307. dspcntr |= DISPPLANE_BGRX101010;
  2308. break;
  2309. case DRM_FORMAT_XBGR2101010:
  2310. dspcntr |= DISPPLANE_RGBX101010;
  2311. break;
  2312. default:
  2313. BUG();
  2314. }
  2315. if (INTEL_INFO(dev)->gen >= 4 &&
  2316. obj->tiling_mode != I915_TILING_NONE)
  2317. dspcntr |= DISPPLANE_TILED;
  2318. if (IS_G4X(dev))
  2319. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2320. linear_offset = y * fb->pitches[0] + x * cpp;
  2321. if (INTEL_INFO(dev)->gen >= 4) {
  2322. intel_crtc->dspaddr_offset =
  2323. intel_compute_tile_offset(&x, &y, fb, 0,
  2324. fb->pitches[0], rotation);
  2325. linear_offset -= intel_crtc->dspaddr_offset;
  2326. } else {
  2327. intel_crtc->dspaddr_offset = linear_offset;
  2328. }
  2329. if (rotation == BIT(DRM_ROTATE_180)) {
  2330. dspcntr |= DISPPLANE_ROTATE_180;
  2331. x += (crtc_state->pipe_src_w - 1);
  2332. y += (crtc_state->pipe_src_h - 1);
  2333. /* Finding the last pixel of the last line of the display
  2334. data and adding to linear_offset*/
  2335. linear_offset +=
  2336. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2337. (crtc_state->pipe_src_w - 1) * cpp;
  2338. }
  2339. intel_crtc->adjusted_x = x;
  2340. intel_crtc->adjusted_y = y;
  2341. I915_WRITE(reg, dspcntr);
  2342. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2343. if (INTEL_INFO(dev)->gen >= 4) {
  2344. I915_WRITE(DSPSURF(plane),
  2345. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2346. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2347. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2348. } else
  2349. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2350. POSTING_READ(reg);
  2351. }
  2352. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2353. struct drm_crtc *crtc)
  2354. {
  2355. struct drm_device *dev = crtc->dev;
  2356. struct drm_i915_private *dev_priv = dev->dev_private;
  2357. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2358. int plane = intel_crtc->plane;
  2359. I915_WRITE(DSPCNTR(plane), 0);
  2360. if (INTEL_INFO(dev_priv)->gen >= 4)
  2361. I915_WRITE(DSPSURF(plane), 0);
  2362. else
  2363. I915_WRITE(DSPADDR(plane), 0);
  2364. POSTING_READ(DSPCNTR(plane));
  2365. }
  2366. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2367. const struct intel_crtc_state *crtc_state,
  2368. const struct intel_plane_state *plane_state)
  2369. {
  2370. struct drm_device *dev = primary->dev;
  2371. struct drm_i915_private *dev_priv = dev->dev_private;
  2372. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2373. struct drm_framebuffer *fb = plane_state->base.fb;
  2374. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2375. int plane = intel_crtc->plane;
  2376. u32 linear_offset;
  2377. u32 dspcntr;
  2378. i915_reg_t reg = DSPCNTR(plane);
  2379. unsigned int rotation = plane_state->base.rotation;
  2380. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2381. int x = plane_state->src.x1 >> 16;
  2382. int y = plane_state->src.y1 >> 16;
  2383. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2384. dspcntr |= DISPLAY_PLANE_ENABLE;
  2385. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2386. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2387. switch (fb->pixel_format) {
  2388. case DRM_FORMAT_C8:
  2389. dspcntr |= DISPPLANE_8BPP;
  2390. break;
  2391. case DRM_FORMAT_RGB565:
  2392. dspcntr |= DISPPLANE_BGRX565;
  2393. break;
  2394. case DRM_FORMAT_XRGB8888:
  2395. dspcntr |= DISPPLANE_BGRX888;
  2396. break;
  2397. case DRM_FORMAT_XBGR8888:
  2398. dspcntr |= DISPPLANE_RGBX888;
  2399. break;
  2400. case DRM_FORMAT_XRGB2101010:
  2401. dspcntr |= DISPPLANE_BGRX101010;
  2402. break;
  2403. case DRM_FORMAT_XBGR2101010:
  2404. dspcntr |= DISPPLANE_RGBX101010;
  2405. break;
  2406. default:
  2407. BUG();
  2408. }
  2409. if (obj->tiling_mode != I915_TILING_NONE)
  2410. dspcntr |= DISPPLANE_TILED;
  2411. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2412. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2413. linear_offset = y * fb->pitches[0] + x * cpp;
  2414. intel_crtc->dspaddr_offset =
  2415. intel_compute_tile_offset(&x, &y, fb, 0,
  2416. fb->pitches[0], rotation);
  2417. linear_offset -= intel_crtc->dspaddr_offset;
  2418. if (rotation == BIT(DRM_ROTATE_180)) {
  2419. dspcntr |= DISPPLANE_ROTATE_180;
  2420. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2421. x += (crtc_state->pipe_src_w - 1);
  2422. y += (crtc_state->pipe_src_h - 1);
  2423. /* Finding the last pixel of the last line of the display
  2424. data and adding to linear_offset*/
  2425. linear_offset +=
  2426. (crtc_state->pipe_src_h - 1) * fb->pitches[0] +
  2427. (crtc_state->pipe_src_w - 1) * cpp;
  2428. }
  2429. }
  2430. intel_crtc->adjusted_x = x;
  2431. intel_crtc->adjusted_y = y;
  2432. I915_WRITE(reg, dspcntr);
  2433. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2434. I915_WRITE(DSPSURF(plane),
  2435. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2436. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2437. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2438. } else {
  2439. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2440. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2441. }
  2442. POSTING_READ(reg);
  2443. }
  2444. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2445. uint64_t fb_modifier, uint32_t pixel_format)
  2446. {
  2447. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2448. return 64;
  2449. } else {
  2450. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2451. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2452. }
  2453. }
  2454. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2455. struct drm_i915_gem_object *obj,
  2456. unsigned int plane)
  2457. {
  2458. struct i915_ggtt_view view;
  2459. struct i915_vma *vma;
  2460. u64 offset;
  2461. intel_fill_fb_ggtt_view(&view, intel_plane->base.state->fb,
  2462. intel_plane->base.state->rotation);
  2463. vma = i915_gem_obj_to_ggtt_view(obj, &view);
  2464. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2465. view.type))
  2466. return -1;
  2467. offset = vma->node.start;
  2468. if (plane == 1) {
  2469. offset += vma->ggtt_view.params.rotated.uv_start_page *
  2470. PAGE_SIZE;
  2471. }
  2472. WARN_ON(upper_32_bits(offset));
  2473. return lower_32_bits(offset);
  2474. }
  2475. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2476. {
  2477. struct drm_device *dev = intel_crtc->base.dev;
  2478. struct drm_i915_private *dev_priv = dev->dev_private;
  2479. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2480. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2481. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2482. }
  2483. /*
  2484. * This function detaches (aka. unbinds) unused scalers in hardware
  2485. */
  2486. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2487. {
  2488. struct intel_crtc_scaler_state *scaler_state;
  2489. int i;
  2490. scaler_state = &intel_crtc->config->scaler_state;
  2491. /* loop through and disable scalers that aren't in use */
  2492. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2493. if (!scaler_state->scalers[i].in_use)
  2494. skl_detach_scaler(intel_crtc, i);
  2495. }
  2496. }
  2497. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2498. {
  2499. switch (pixel_format) {
  2500. case DRM_FORMAT_C8:
  2501. return PLANE_CTL_FORMAT_INDEXED;
  2502. case DRM_FORMAT_RGB565:
  2503. return PLANE_CTL_FORMAT_RGB_565;
  2504. case DRM_FORMAT_XBGR8888:
  2505. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2506. case DRM_FORMAT_XRGB8888:
  2507. return PLANE_CTL_FORMAT_XRGB_8888;
  2508. /*
  2509. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2510. * to be already pre-multiplied. We need to add a knob (or a different
  2511. * DRM_FORMAT) for user-space to configure that.
  2512. */
  2513. case DRM_FORMAT_ABGR8888:
  2514. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2515. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2516. case DRM_FORMAT_ARGB8888:
  2517. return PLANE_CTL_FORMAT_XRGB_8888 |
  2518. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2519. case DRM_FORMAT_XRGB2101010:
  2520. return PLANE_CTL_FORMAT_XRGB_2101010;
  2521. case DRM_FORMAT_XBGR2101010:
  2522. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2523. case DRM_FORMAT_YUYV:
  2524. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2525. case DRM_FORMAT_YVYU:
  2526. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2527. case DRM_FORMAT_UYVY:
  2528. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2529. case DRM_FORMAT_VYUY:
  2530. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2531. default:
  2532. MISSING_CASE(pixel_format);
  2533. }
  2534. return 0;
  2535. }
  2536. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2537. {
  2538. switch (fb_modifier) {
  2539. case DRM_FORMAT_MOD_NONE:
  2540. break;
  2541. case I915_FORMAT_MOD_X_TILED:
  2542. return PLANE_CTL_TILED_X;
  2543. case I915_FORMAT_MOD_Y_TILED:
  2544. return PLANE_CTL_TILED_Y;
  2545. case I915_FORMAT_MOD_Yf_TILED:
  2546. return PLANE_CTL_TILED_YF;
  2547. default:
  2548. MISSING_CASE(fb_modifier);
  2549. }
  2550. return 0;
  2551. }
  2552. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2553. {
  2554. switch (rotation) {
  2555. case BIT(DRM_ROTATE_0):
  2556. break;
  2557. /*
  2558. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2559. * while i915 HW rotation is clockwise, thats why this swapping.
  2560. */
  2561. case BIT(DRM_ROTATE_90):
  2562. return PLANE_CTL_ROTATE_270;
  2563. case BIT(DRM_ROTATE_180):
  2564. return PLANE_CTL_ROTATE_180;
  2565. case BIT(DRM_ROTATE_270):
  2566. return PLANE_CTL_ROTATE_90;
  2567. default:
  2568. MISSING_CASE(rotation);
  2569. }
  2570. return 0;
  2571. }
  2572. static void skylake_update_primary_plane(struct drm_plane *plane,
  2573. const struct intel_crtc_state *crtc_state,
  2574. const struct intel_plane_state *plane_state)
  2575. {
  2576. struct drm_device *dev = plane->dev;
  2577. struct drm_i915_private *dev_priv = dev->dev_private;
  2578. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2579. struct drm_framebuffer *fb = plane_state->base.fb;
  2580. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2581. int pipe = intel_crtc->pipe;
  2582. u32 plane_ctl, stride_div, stride;
  2583. u32 tile_height, plane_offset, plane_size;
  2584. unsigned int rotation = plane_state->base.rotation;
  2585. int x_offset, y_offset;
  2586. u32 surf_addr;
  2587. int scaler_id = plane_state->scaler_id;
  2588. int src_x = plane_state->src.x1 >> 16;
  2589. int src_y = plane_state->src.y1 >> 16;
  2590. int src_w = drm_rect_width(&plane_state->src) >> 16;
  2591. int src_h = drm_rect_height(&plane_state->src) >> 16;
  2592. int dst_x = plane_state->dst.x1;
  2593. int dst_y = plane_state->dst.y1;
  2594. int dst_w = drm_rect_width(&plane_state->dst);
  2595. int dst_h = drm_rect_height(&plane_state->dst);
  2596. plane_ctl = PLANE_CTL_ENABLE |
  2597. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2598. PLANE_CTL_PIPE_CSC_ENABLE;
  2599. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2600. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2601. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2602. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2603. stride_div = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2604. fb->pixel_format);
  2605. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2606. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2607. if (intel_rotation_90_or_270(rotation)) {
  2608. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2609. /* stride = Surface height in tiles */
  2610. tile_height = intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2611. stride = DIV_ROUND_UP(fb->height, tile_height);
  2612. x_offset = stride * tile_height - src_y - src_h;
  2613. y_offset = src_x;
  2614. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2615. } else {
  2616. stride = fb->pitches[0] / stride_div;
  2617. x_offset = src_x;
  2618. y_offset = src_y;
  2619. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2620. }
  2621. plane_offset = y_offset << 16 | x_offset;
  2622. intel_crtc->adjusted_x = x_offset;
  2623. intel_crtc->adjusted_y = y_offset;
  2624. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2625. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2626. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2627. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2628. if (scaler_id >= 0) {
  2629. uint32_t ps_ctrl = 0;
  2630. WARN_ON(!dst_w || !dst_h);
  2631. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2632. crtc_state->scaler_state.scalers[scaler_id].mode;
  2633. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2634. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2635. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2636. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2637. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2638. } else {
  2639. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2640. }
  2641. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2642. POSTING_READ(PLANE_SURF(pipe, 0));
  2643. }
  2644. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2645. struct drm_crtc *crtc)
  2646. {
  2647. struct drm_device *dev = crtc->dev;
  2648. struct drm_i915_private *dev_priv = dev->dev_private;
  2649. int pipe = to_intel_crtc(crtc)->pipe;
  2650. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2651. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2652. POSTING_READ(PLANE_SURF(pipe, 0));
  2653. }
  2654. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2655. static int
  2656. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2657. int x, int y, enum mode_set_atomic state)
  2658. {
  2659. /* Support for kgdboc is disabled, this needs a major rework. */
  2660. DRM_ERROR("legacy panic handler not supported any more.\n");
  2661. return -ENODEV;
  2662. }
  2663. static void intel_update_primary_planes(struct drm_device *dev)
  2664. {
  2665. struct drm_crtc *crtc;
  2666. for_each_crtc(dev, crtc) {
  2667. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2668. struct intel_plane_state *plane_state;
  2669. drm_modeset_lock_crtc(crtc, &plane->base);
  2670. plane_state = to_intel_plane_state(plane->base.state);
  2671. if (plane_state->visible)
  2672. plane->update_plane(&plane->base,
  2673. to_intel_crtc_state(crtc->state),
  2674. plane_state);
  2675. drm_modeset_unlock_crtc(crtc);
  2676. }
  2677. }
  2678. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2679. {
  2680. /* no reset support for gen2 */
  2681. if (IS_GEN2(dev_priv))
  2682. return;
  2683. /* reset doesn't touch the display */
  2684. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  2685. return;
  2686. drm_modeset_lock_all(dev_priv->dev);
  2687. /*
  2688. * Disabling the crtcs gracefully seems nicer. Also the
  2689. * g33 docs say we should at least disable all the planes.
  2690. */
  2691. intel_display_suspend(dev_priv->dev);
  2692. }
  2693. void intel_finish_reset(struct drm_i915_private *dev_priv)
  2694. {
  2695. /* no reset support for gen2 */
  2696. if (IS_GEN2(dev_priv))
  2697. return;
  2698. /* reset doesn't touch the display */
  2699. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv)) {
  2700. /*
  2701. * Flips in the rings have been nuked by the reset,
  2702. * so update the base address of all primary
  2703. * planes to the the last fb to make sure we're
  2704. * showing the correct fb after a reset.
  2705. *
  2706. * FIXME: Atomic will make this obsolete since we won't schedule
  2707. * CS-based flips (which might get lost in gpu resets) any more.
  2708. */
  2709. intel_update_primary_planes(dev_priv->dev);
  2710. return;
  2711. }
  2712. /*
  2713. * The display has been reset as well,
  2714. * so need a full re-initialization.
  2715. */
  2716. intel_runtime_pm_disable_interrupts(dev_priv);
  2717. intel_runtime_pm_enable_interrupts(dev_priv);
  2718. intel_modeset_init_hw(dev_priv->dev);
  2719. spin_lock_irq(&dev_priv->irq_lock);
  2720. if (dev_priv->display.hpd_irq_setup)
  2721. dev_priv->display.hpd_irq_setup(dev_priv);
  2722. spin_unlock_irq(&dev_priv->irq_lock);
  2723. intel_display_resume(dev_priv->dev);
  2724. intel_hpd_init(dev_priv);
  2725. drm_modeset_unlock_all(dev_priv->dev);
  2726. }
  2727. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2728. {
  2729. return !list_empty_careful(&to_intel_crtc(crtc)->flip_work);
  2730. }
  2731. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2732. struct intel_crtc_state *old_crtc_state)
  2733. {
  2734. struct drm_device *dev = crtc->base.dev;
  2735. struct drm_i915_private *dev_priv = dev->dev_private;
  2736. struct intel_crtc_state *pipe_config =
  2737. to_intel_crtc_state(crtc->base.state);
  2738. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2739. crtc->base.mode = crtc->base.state->mode;
  2740. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2741. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2742. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2743. /*
  2744. * Update pipe size and adjust fitter if needed: the reason for this is
  2745. * that in compute_mode_changes we check the native mode (not the pfit
  2746. * mode) to see if we can flip rather than do a full mode set. In the
  2747. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2748. * pfit state, we'll end up with a big fb scanned out into the wrong
  2749. * sized surface.
  2750. */
  2751. I915_WRITE(PIPESRC(crtc->pipe),
  2752. ((pipe_config->pipe_src_w - 1) << 16) |
  2753. (pipe_config->pipe_src_h - 1));
  2754. /* on skylake this is done by detaching scalers */
  2755. if (INTEL_INFO(dev)->gen >= 9) {
  2756. skl_detach_scalers(crtc);
  2757. if (pipe_config->pch_pfit.enabled)
  2758. skylake_pfit_enable(crtc);
  2759. } else if (HAS_PCH_SPLIT(dev)) {
  2760. if (pipe_config->pch_pfit.enabled)
  2761. ironlake_pfit_enable(crtc);
  2762. else if (old_crtc_state->pch_pfit.enabled)
  2763. ironlake_pfit_disable(crtc, true);
  2764. }
  2765. }
  2766. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2767. {
  2768. struct drm_device *dev = crtc->dev;
  2769. struct drm_i915_private *dev_priv = dev->dev_private;
  2770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2771. int pipe = intel_crtc->pipe;
  2772. i915_reg_t reg;
  2773. u32 temp;
  2774. /* enable normal train */
  2775. reg = FDI_TX_CTL(pipe);
  2776. temp = I915_READ(reg);
  2777. if (IS_IVYBRIDGE(dev)) {
  2778. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2779. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2780. } else {
  2781. temp &= ~FDI_LINK_TRAIN_NONE;
  2782. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2783. }
  2784. I915_WRITE(reg, temp);
  2785. reg = FDI_RX_CTL(pipe);
  2786. temp = I915_READ(reg);
  2787. if (HAS_PCH_CPT(dev)) {
  2788. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2789. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2790. } else {
  2791. temp &= ~FDI_LINK_TRAIN_NONE;
  2792. temp |= FDI_LINK_TRAIN_NONE;
  2793. }
  2794. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2795. /* wait one idle pattern time */
  2796. POSTING_READ(reg);
  2797. udelay(1000);
  2798. /* IVB wants error correction enabled */
  2799. if (IS_IVYBRIDGE(dev))
  2800. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2801. FDI_FE_ERRC_ENABLE);
  2802. }
  2803. /* The FDI link training functions for ILK/Ibexpeak. */
  2804. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2805. {
  2806. struct drm_device *dev = crtc->dev;
  2807. struct drm_i915_private *dev_priv = dev->dev_private;
  2808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2809. int pipe = intel_crtc->pipe;
  2810. i915_reg_t reg;
  2811. u32 temp, tries;
  2812. /* FDI needs bits from pipe first */
  2813. assert_pipe_enabled(dev_priv, pipe);
  2814. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2815. for train result */
  2816. reg = FDI_RX_IMR(pipe);
  2817. temp = I915_READ(reg);
  2818. temp &= ~FDI_RX_SYMBOL_LOCK;
  2819. temp &= ~FDI_RX_BIT_LOCK;
  2820. I915_WRITE(reg, temp);
  2821. I915_READ(reg);
  2822. udelay(150);
  2823. /* enable CPU FDI TX and PCH FDI RX */
  2824. reg = FDI_TX_CTL(pipe);
  2825. temp = I915_READ(reg);
  2826. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2827. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2828. temp &= ~FDI_LINK_TRAIN_NONE;
  2829. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2830. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2831. reg = FDI_RX_CTL(pipe);
  2832. temp = I915_READ(reg);
  2833. temp &= ~FDI_LINK_TRAIN_NONE;
  2834. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2835. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2836. POSTING_READ(reg);
  2837. udelay(150);
  2838. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2839. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2840. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2841. FDI_RX_PHASE_SYNC_POINTER_EN);
  2842. reg = FDI_RX_IIR(pipe);
  2843. for (tries = 0; tries < 5; tries++) {
  2844. temp = I915_READ(reg);
  2845. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2846. if ((temp & FDI_RX_BIT_LOCK)) {
  2847. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2848. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2849. break;
  2850. }
  2851. }
  2852. if (tries == 5)
  2853. DRM_ERROR("FDI train 1 fail!\n");
  2854. /* Train 2 */
  2855. reg = FDI_TX_CTL(pipe);
  2856. temp = I915_READ(reg);
  2857. temp &= ~FDI_LINK_TRAIN_NONE;
  2858. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2859. I915_WRITE(reg, temp);
  2860. reg = FDI_RX_CTL(pipe);
  2861. temp = I915_READ(reg);
  2862. temp &= ~FDI_LINK_TRAIN_NONE;
  2863. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2864. I915_WRITE(reg, temp);
  2865. POSTING_READ(reg);
  2866. udelay(150);
  2867. reg = FDI_RX_IIR(pipe);
  2868. for (tries = 0; tries < 5; tries++) {
  2869. temp = I915_READ(reg);
  2870. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2871. if (temp & FDI_RX_SYMBOL_LOCK) {
  2872. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2873. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2874. break;
  2875. }
  2876. }
  2877. if (tries == 5)
  2878. DRM_ERROR("FDI train 2 fail!\n");
  2879. DRM_DEBUG_KMS("FDI train done\n");
  2880. }
  2881. static const int snb_b_fdi_train_param[] = {
  2882. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2883. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2884. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2885. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2886. };
  2887. /* The FDI link training functions for SNB/Cougarpoint. */
  2888. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2889. {
  2890. struct drm_device *dev = crtc->dev;
  2891. struct drm_i915_private *dev_priv = dev->dev_private;
  2892. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2893. int pipe = intel_crtc->pipe;
  2894. i915_reg_t reg;
  2895. u32 temp, i, retry;
  2896. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2897. for train result */
  2898. reg = FDI_RX_IMR(pipe);
  2899. temp = I915_READ(reg);
  2900. temp &= ~FDI_RX_SYMBOL_LOCK;
  2901. temp &= ~FDI_RX_BIT_LOCK;
  2902. I915_WRITE(reg, temp);
  2903. POSTING_READ(reg);
  2904. udelay(150);
  2905. /* enable CPU FDI TX and PCH FDI RX */
  2906. reg = FDI_TX_CTL(pipe);
  2907. temp = I915_READ(reg);
  2908. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2909. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2910. temp &= ~FDI_LINK_TRAIN_NONE;
  2911. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2912. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2913. /* SNB-B */
  2914. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2915. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2916. I915_WRITE(FDI_RX_MISC(pipe),
  2917. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2918. reg = FDI_RX_CTL(pipe);
  2919. temp = I915_READ(reg);
  2920. if (HAS_PCH_CPT(dev)) {
  2921. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2922. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2923. } else {
  2924. temp &= ~FDI_LINK_TRAIN_NONE;
  2925. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2926. }
  2927. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2928. POSTING_READ(reg);
  2929. udelay(150);
  2930. for (i = 0; i < 4; i++) {
  2931. reg = FDI_TX_CTL(pipe);
  2932. temp = I915_READ(reg);
  2933. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2934. temp |= snb_b_fdi_train_param[i];
  2935. I915_WRITE(reg, temp);
  2936. POSTING_READ(reg);
  2937. udelay(500);
  2938. for (retry = 0; retry < 5; retry++) {
  2939. reg = FDI_RX_IIR(pipe);
  2940. temp = I915_READ(reg);
  2941. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2942. if (temp & FDI_RX_BIT_LOCK) {
  2943. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2944. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2945. break;
  2946. }
  2947. udelay(50);
  2948. }
  2949. if (retry < 5)
  2950. break;
  2951. }
  2952. if (i == 4)
  2953. DRM_ERROR("FDI train 1 fail!\n");
  2954. /* Train 2 */
  2955. reg = FDI_TX_CTL(pipe);
  2956. temp = I915_READ(reg);
  2957. temp &= ~FDI_LINK_TRAIN_NONE;
  2958. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2959. if (IS_GEN6(dev)) {
  2960. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2961. /* SNB-B */
  2962. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2963. }
  2964. I915_WRITE(reg, temp);
  2965. reg = FDI_RX_CTL(pipe);
  2966. temp = I915_READ(reg);
  2967. if (HAS_PCH_CPT(dev)) {
  2968. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2969. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2970. } else {
  2971. temp &= ~FDI_LINK_TRAIN_NONE;
  2972. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2973. }
  2974. I915_WRITE(reg, temp);
  2975. POSTING_READ(reg);
  2976. udelay(150);
  2977. for (i = 0; i < 4; i++) {
  2978. reg = FDI_TX_CTL(pipe);
  2979. temp = I915_READ(reg);
  2980. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2981. temp |= snb_b_fdi_train_param[i];
  2982. I915_WRITE(reg, temp);
  2983. POSTING_READ(reg);
  2984. udelay(500);
  2985. for (retry = 0; retry < 5; retry++) {
  2986. reg = FDI_RX_IIR(pipe);
  2987. temp = I915_READ(reg);
  2988. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2989. if (temp & FDI_RX_SYMBOL_LOCK) {
  2990. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2991. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2992. break;
  2993. }
  2994. udelay(50);
  2995. }
  2996. if (retry < 5)
  2997. break;
  2998. }
  2999. if (i == 4)
  3000. DRM_ERROR("FDI train 2 fail!\n");
  3001. DRM_DEBUG_KMS("FDI train done.\n");
  3002. }
  3003. /* Manual link training for Ivy Bridge A0 parts */
  3004. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3005. {
  3006. struct drm_device *dev = crtc->dev;
  3007. struct drm_i915_private *dev_priv = dev->dev_private;
  3008. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3009. int pipe = intel_crtc->pipe;
  3010. i915_reg_t reg;
  3011. u32 temp, i, j;
  3012. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3013. for train result */
  3014. reg = FDI_RX_IMR(pipe);
  3015. temp = I915_READ(reg);
  3016. temp &= ~FDI_RX_SYMBOL_LOCK;
  3017. temp &= ~FDI_RX_BIT_LOCK;
  3018. I915_WRITE(reg, temp);
  3019. POSTING_READ(reg);
  3020. udelay(150);
  3021. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3022. I915_READ(FDI_RX_IIR(pipe)));
  3023. /* Try each vswing and preemphasis setting twice before moving on */
  3024. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3025. /* disable first in case we need to retry */
  3026. reg = FDI_TX_CTL(pipe);
  3027. temp = I915_READ(reg);
  3028. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3029. temp &= ~FDI_TX_ENABLE;
  3030. I915_WRITE(reg, temp);
  3031. reg = FDI_RX_CTL(pipe);
  3032. temp = I915_READ(reg);
  3033. temp &= ~FDI_LINK_TRAIN_AUTO;
  3034. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3035. temp &= ~FDI_RX_ENABLE;
  3036. I915_WRITE(reg, temp);
  3037. /* enable CPU FDI TX and PCH FDI RX */
  3038. reg = FDI_TX_CTL(pipe);
  3039. temp = I915_READ(reg);
  3040. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3041. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3042. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3043. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3044. temp |= snb_b_fdi_train_param[j/2];
  3045. temp |= FDI_COMPOSITE_SYNC;
  3046. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3047. I915_WRITE(FDI_RX_MISC(pipe),
  3048. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3049. reg = FDI_RX_CTL(pipe);
  3050. temp = I915_READ(reg);
  3051. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3052. temp |= FDI_COMPOSITE_SYNC;
  3053. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3054. POSTING_READ(reg);
  3055. udelay(1); /* should be 0.5us */
  3056. for (i = 0; i < 4; i++) {
  3057. reg = FDI_RX_IIR(pipe);
  3058. temp = I915_READ(reg);
  3059. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3060. if (temp & FDI_RX_BIT_LOCK ||
  3061. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3062. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3063. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3064. i);
  3065. break;
  3066. }
  3067. udelay(1); /* should be 0.5us */
  3068. }
  3069. if (i == 4) {
  3070. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3071. continue;
  3072. }
  3073. /* Train 2 */
  3074. reg = FDI_TX_CTL(pipe);
  3075. temp = I915_READ(reg);
  3076. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3077. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3078. I915_WRITE(reg, temp);
  3079. reg = FDI_RX_CTL(pipe);
  3080. temp = I915_READ(reg);
  3081. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3082. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3083. I915_WRITE(reg, temp);
  3084. POSTING_READ(reg);
  3085. udelay(2); /* should be 1.5us */
  3086. for (i = 0; i < 4; i++) {
  3087. reg = FDI_RX_IIR(pipe);
  3088. temp = I915_READ(reg);
  3089. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3090. if (temp & FDI_RX_SYMBOL_LOCK ||
  3091. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3092. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3093. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3094. i);
  3095. goto train_done;
  3096. }
  3097. udelay(2); /* should be 1.5us */
  3098. }
  3099. if (i == 4)
  3100. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3101. }
  3102. train_done:
  3103. DRM_DEBUG_KMS("FDI train done.\n");
  3104. }
  3105. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3106. {
  3107. struct drm_device *dev = intel_crtc->base.dev;
  3108. struct drm_i915_private *dev_priv = dev->dev_private;
  3109. int pipe = intel_crtc->pipe;
  3110. i915_reg_t reg;
  3111. u32 temp;
  3112. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3113. reg = FDI_RX_CTL(pipe);
  3114. temp = I915_READ(reg);
  3115. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3116. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3117. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3118. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3119. POSTING_READ(reg);
  3120. udelay(200);
  3121. /* Switch from Rawclk to PCDclk */
  3122. temp = I915_READ(reg);
  3123. I915_WRITE(reg, temp | FDI_PCDCLK);
  3124. POSTING_READ(reg);
  3125. udelay(200);
  3126. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3127. reg = FDI_TX_CTL(pipe);
  3128. temp = I915_READ(reg);
  3129. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3130. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3131. POSTING_READ(reg);
  3132. udelay(100);
  3133. }
  3134. }
  3135. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3136. {
  3137. struct drm_device *dev = intel_crtc->base.dev;
  3138. struct drm_i915_private *dev_priv = dev->dev_private;
  3139. int pipe = intel_crtc->pipe;
  3140. i915_reg_t reg;
  3141. u32 temp;
  3142. /* Switch from PCDclk to Rawclk */
  3143. reg = FDI_RX_CTL(pipe);
  3144. temp = I915_READ(reg);
  3145. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3146. /* Disable CPU FDI TX PLL */
  3147. reg = FDI_TX_CTL(pipe);
  3148. temp = I915_READ(reg);
  3149. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3150. POSTING_READ(reg);
  3151. udelay(100);
  3152. reg = FDI_RX_CTL(pipe);
  3153. temp = I915_READ(reg);
  3154. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3155. /* Wait for the clocks to turn off. */
  3156. POSTING_READ(reg);
  3157. udelay(100);
  3158. }
  3159. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3160. {
  3161. struct drm_device *dev = crtc->dev;
  3162. struct drm_i915_private *dev_priv = dev->dev_private;
  3163. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3164. int pipe = intel_crtc->pipe;
  3165. i915_reg_t reg;
  3166. u32 temp;
  3167. /* disable CPU FDI tx and PCH FDI rx */
  3168. reg = FDI_TX_CTL(pipe);
  3169. temp = I915_READ(reg);
  3170. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3171. POSTING_READ(reg);
  3172. reg = FDI_RX_CTL(pipe);
  3173. temp = I915_READ(reg);
  3174. temp &= ~(0x7 << 16);
  3175. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3176. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3177. POSTING_READ(reg);
  3178. udelay(100);
  3179. /* Ironlake workaround, disable clock pointer after downing FDI */
  3180. if (HAS_PCH_IBX(dev))
  3181. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3182. /* still set train pattern 1 */
  3183. reg = FDI_TX_CTL(pipe);
  3184. temp = I915_READ(reg);
  3185. temp &= ~FDI_LINK_TRAIN_NONE;
  3186. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3187. I915_WRITE(reg, temp);
  3188. reg = FDI_RX_CTL(pipe);
  3189. temp = I915_READ(reg);
  3190. if (HAS_PCH_CPT(dev)) {
  3191. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3192. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3193. } else {
  3194. temp &= ~FDI_LINK_TRAIN_NONE;
  3195. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3196. }
  3197. /* BPC in FDI rx is consistent with that in PIPECONF */
  3198. temp &= ~(0x07 << 16);
  3199. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3200. I915_WRITE(reg, temp);
  3201. POSTING_READ(reg);
  3202. udelay(100);
  3203. }
  3204. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3205. {
  3206. struct intel_crtc *crtc;
  3207. /* Note that we don't need to be called with mode_config.lock here
  3208. * as our list of CRTC objects is static for the lifetime of the
  3209. * device and so cannot disappear as we iterate. Similarly, we can
  3210. * happily treat the predicates as racy, atomic checks as userspace
  3211. * cannot claim and pin a new fb without at least acquring the
  3212. * struct_mutex and so serialising with us.
  3213. */
  3214. for_each_intel_crtc(dev, crtc) {
  3215. if (atomic_read(&crtc->unpin_work_count) == 0)
  3216. continue;
  3217. if (!list_empty_careful(&crtc->flip_work))
  3218. intel_wait_for_vblank(dev, crtc->pipe);
  3219. return true;
  3220. }
  3221. return false;
  3222. }
  3223. static void page_flip_completed(struct intel_crtc *intel_crtc, struct intel_flip_work *work)
  3224. {
  3225. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3226. struct drm_plane_state *new_plane_state;
  3227. struct drm_plane *primary = intel_crtc->base.primary;
  3228. if (work->event)
  3229. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3230. drm_crtc_vblank_put(&intel_crtc->base);
  3231. new_plane_state = &work->old_plane_state[0]->base;
  3232. if (work->num_planes >= 1 &&
  3233. new_plane_state->plane == primary &&
  3234. new_plane_state->fb)
  3235. trace_i915_flip_complete(intel_crtc->plane,
  3236. intel_fb_obj(new_plane_state->fb));
  3237. if (work->can_async_unpin) {
  3238. list_del_init(&work->head);
  3239. wake_up_all(&dev_priv->pending_flip_queue);
  3240. }
  3241. queue_work(dev_priv->wq, &work->unpin_work);
  3242. }
  3243. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3244. {
  3245. struct drm_device *dev = crtc->dev;
  3246. struct drm_i915_private *dev_priv = dev->dev_private;
  3247. long ret;
  3248. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3249. ret = wait_event_interruptible_timeout(
  3250. dev_priv->pending_flip_queue,
  3251. !intel_crtc_has_pending_flip(crtc),
  3252. 60*HZ);
  3253. if (ret < 0)
  3254. return ret;
  3255. WARN(ret == 0, "Stuck page flip\n");
  3256. return 0;
  3257. }
  3258. static void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3259. {
  3260. u32 temp;
  3261. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3262. mutex_lock(&dev_priv->sb_lock);
  3263. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3264. temp |= SBI_SSCCTL_DISABLE;
  3265. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3266. mutex_unlock(&dev_priv->sb_lock);
  3267. }
  3268. /* Program iCLKIP clock to the desired frequency */
  3269. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3270. {
  3271. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3272. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3273. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3274. u32 temp;
  3275. lpt_disable_iclkip(dev_priv);
  3276. /* The iCLK virtual clock root frequency is in MHz,
  3277. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3278. * divisors, it is necessary to divide one by another, so we
  3279. * convert the virtual clock precision to KHz here for higher
  3280. * precision.
  3281. */
  3282. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3283. u32 iclk_virtual_root_freq = 172800 * 1000;
  3284. u32 iclk_pi_range = 64;
  3285. u32 desired_divisor;
  3286. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3287. clock << auxdiv);
  3288. divsel = (desired_divisor / iclk_pi_range) - 2;
  3289. phaseinc = desired_divisor % iclk_pi_range;
  3290. /*
  3291. * Near 20MHz is a corner case which is
  3292. * out of range for the 7-bit divisor
  3293. */
  3294. if (divsel <= 0x7f)
  3295. break;
  3296. }
  3297. /* This should not happen with any sane values */
  3298. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3299. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3300. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3301. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3302. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3303. clock,
  3304. auxdiv,
  3305. divsel,
  3306. phasedir,
  3307. phaseinc);
  3308. mutex_lock(&dev_priv->sb_lock);
  3309. /* Program SSCDIVINTPHASE6 */
  3310. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3311. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3312. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3313. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3314. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3315. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3316. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3317. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3318. /* Program SSCAUXDIV */
  3319. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3320. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3321. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3322. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3323. /* Enable modulator and associated divider */
  3324. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3325. temp &= ~SBI_SSCCTL_DISABLE;
  3326. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3327. mutex_unlock(&dev_priv->sb_lock);
  3328. /* Wait for initialization time */
  3329. udelay(24);
  3330. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3331. }
  3332. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3333. {
  3334. u32 divsel, phaseinc, auxdiv;
  3335. u32 iclk_virtual_root_freq = 172800 * 1000;
  3336. u32 iclk_pi_range = 64;
  3337. u32 desired_divisor;
  3338. u32 temp;
  3339. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3340. return 0;
  3341. mutex_lock(&dev_priv->sb_lock);
  3342. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3343. if (temp & SBI_SSCCTL_DISABLE) {
  3344. mutex_unlock(&dev_priv->sb_lock);
  3345. return 0;
  3346. }
  3347. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3348. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3349. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3350. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3351. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3352. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3353. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3354. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3355. mutex_unlock(&dev_priv->sb_lock);
  3356. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3357. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3358. desired_divisor << auxdiv);
  3359. }
  3360. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3361. enum pipe pch_transcoder)
  3362. {
  3363. struct drm_device *dev = crtc->base.dev;
  3364. struct drm_i915_private *dev_priv = dev->dev_private;
  3365. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3366. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3367. I915_READ(HTOTAL(cpu_transcoder)));
  3368. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3369. I915_READ(HBLANK(cpu_transcoder)));
  3370. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3371. I915_READ(HSYNC(cpu_transcoder)));
  3372. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3373. I915_READ(VTOTAL(cpu_transcoder)));
  3374. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3375. I915_READ(VBLANK(cpu_transcoder)));
  3376. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3377. I915_READ(VSYNC(cpu_transcoder)));
  3378. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3379. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3380. }
  3381. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3382. {
  3383. struct drm_i915_private *dev_priv = dev->dev_private;
  3384. uint32_t temp;
  3385. temp = I915_READ(SOUTH_CHICKEN1);
  3386. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3387. return;
  3388. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3389. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3390. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3391. if (enable)
  3392. temp |= FDI_BC_BIFURCATION_SELECT;
  3393. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3394. I915_WRITE(SOUTH_CHICKEN1, temp);
  3395. POSTING_READ(SOUTH_CHICKEN1);
  3396. }
  3397. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3398. {
  3399. struct drm_device *dev = intel_crtc->base.dev;
  3400. switch (intel_crtc->pipe) {
  3401. case PIPE_A:
  3402. break;
  3403. case PIPE_B:
  3404. if (intel_crtc->config->fdi_lanes > 2)
  3405. cpt_set_fdi_bc_bifurcation(dev, false);
  3406. else
  3407. cpt_set_fdi_bc_bifurcation(dev, true);
  3408. break;
  3409. case PIPE_C:
  3410. cpt_set_fdi_bc_bifurcation(dev, true);
  3411. break;
  3412. default:
  3413. BUG();
  3414. }
  3415. }
  3416. /* Return which DP Port should be selected for Transcoder DP control */
  3417. static enum port
  3418. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3419. {
  3420. struct drm_device *dev = crtc->dev;
  3421. struct intel_encoder *encoder;
  3422. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3423. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3424. encoder->type == INTEL_OUTPUT_EDP)
  3425. return enc_to_dig_port(&encoder->base)->port;
  3426. }
  3427. return -1;
  3428. }
  3429. /*
  3430. * Enable PCH resources required for PCH ports:
  3431. * - PCH PLLs
  3432. * - FDI training & RX/TX
  3433. * - update transcoder timings
  3434. * - DP transcoding bits
  3435. * - transcoder
  3436. */
  3437. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3438. {
  3439. struct drm_device *dev = crtc->dev;
  3440. struct drm_i915_private *dev_priv = dev->dev_private;
  3441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3442. int pipe = intel_crtc->pipe;
  3443. u32 temp;
  3444. assert_pch_transcoder_disabled(dev_priv, pipe);
  3445. if (IS_IVYBRIDGE(dev))
  3446. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3447. /* Write the TU size bits before fdi link training, so that error
  3448. * detection works. */
  3449. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3450. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3451. /* For PCH output, training FDI link */
  3452. dev_priv->display.fdi_link_train(crtc);
  3453. /* We need to program the right clock selection before writing the pixel
  3454. * mutliplier into the DPLL. */
  3455. if (HAS_PCH_CPT(dev)) {
  3456. u32 sel;
  3457. temp = I915_READ(PCH_DPLL_SEL);
  3458. temp |= TRANS_DPLL_ENABLE(pipe);
  3459. sel = TRANS_DPLLB_SEL(pipe);
  3460. if (intel_crtc->config->shared_dpll ==
  3461. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3462. temp |= sel;
  3463. else
  3464. temp &= ~sel;
  3465. I915_WRITE(PCH_DPLL_SEL, temp);
  3466. }
  3467. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3468. * transcoder, and we actually should do this to not upset any PCH
  3469. * transcoder that already use the clock when we share it.
  3470. *
  3471. * Note that enable_shared_dpll tries to do the right thing, but
  3472. * get_shared_dpll unconditionally resets the pll - we need that to have
  3473. * the right LVDS enable sequence. */
  3474. intel_enable_shared_dpll(intel_crtc);
  3475. /* set transcoder timing, panel must allow it */
  3476. assert_panel_unlocked(dev_priv, pipe);
  3477. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3478. intel_fdi_normal_train(crtc);
  3479. /* For PCH DP, enable TRANS_DP_CTL */
  3480. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3481. const struct drm_display_mode *adjusted_mode =
  3482. &intel_crtc->config->base.adjusted_mode;
  3483. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3484. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3485. temp = I915_READ(reg);
  3486. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3487. TRANS_DP_SYNC_MASK |
  3488. TRANS_DP_BPC_MASK);
  3489. temp |= TRANS_DP_OUTPUT_ENABLE;
  3490. temp |= bpc << 9; /* same format but at 11:9 */
  3491. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3492. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3493. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3494. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3495. switch (intel_trans_dp_port_sel(crtc)) {
  3496. case PORT_B:
  3497. temp |= TRANS_DP_PORT_SEL_B;
  3498. break;
  3499. case PORT_C:
  3500. temp |= TRANS_DP_PORT_SEL_C;
  3501. break;
  3502. case PORT_D:
  3503. temp |= TRANS_DP_PORT_SEL_D;
  3504. break;
  3505. default:
  3506. BUG();
  3507. }
  3508. I915_WRITE(reg, temp);
  3509. }
  3510. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3511. }
  3512. static void lpt_pch_enable(struct drm_crtc *crtc)
  3513. {
  3514. struct drm_device *dev = crtc->dev;
  3515. struct drm_i915_private *dev_priv = dev->dev_private;
  3516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3517. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3518. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3519. lpt_program_iclkip(crtc);
  3520. /* Set transcoder timing. */
  3521. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3522. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3523. }
  3524. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3525. {
  3526. struct drm_i915_private *dev_priv = dev->dev_private;
  3527. i915_reg_t dslreg = PIPEDSL(pipe);
  3528. u32 temp;
  3529. temp = I915_READ(dslreg);
  3530. udelay(500);
  3531. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3532. if (wait_for(I915_READ(dslreg) != temp, 5))
  3533. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3534. }
  3535. }
  3536. static int
  3537. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3538. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3539. int src_w, int src_h, int dst_w, int dst_h)
  3540. {
  3541. struct intel_crtc_scaler_state *scaler_state =
  3542. &crtc_state->scaler_state;
  3543. struct intel_crtc *intel_crtc =
  3544. to_intel_crtc(crtc_state->base.crtc);
  3545. int need_scaling;
  3546. need_scaling = intel_rotation_90_or_270(rotation) ?
  3547. (src_h != dst_w || src_w != dst_h):
  3548. (src_w != dst_w || src_h != dst_h);
  3549. /*
  3550. * if plane is being disabled or scaler is no more required or force detach
  3551. * - free scaler binded to this plane/crtc
  3552. * - in order to do this, update crtc->scaler_usage
  3553. *
  3554. * Here scaler state in crtc_state is set free so that
  3555. * scaler can be assigned to other user. Actual register
  3556. * update to free the scaler is done in plane/panel-fit programming.
  3557. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3558. */
  3559. if (force_detach || !need_scaling) {
  3560. if (*scaler_id >= 0) {
  3561. scaler_state->scaler_users &= ~(1 << scaler_user);
  3562. scaler_state->scalers[*scaler_id].in_use = 0;
  3563. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3564. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3565. intel_crtc->pipe, scaler_user, *scaler_id,
  3566. scaler_state->scaler_users);
  3567. *scaler_id = -1;
  3568. }
  3569. return 0;
  3570. }
  3571. /* range checks */
  3572. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3573. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3574. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3575. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3576. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3577. "size is out of scaler range\n",
  3578. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3579. return -EINVAL;
  3580. }
  3581. /* mark this plane as a scaler user in crtc_state */
  3582. scaler_state->scaler_users |= (1 << scaler_user);
  3583. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3584. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3585. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3586. scaler_state->scaler_users);
  3587. return 0;
  3588. }
  3589. /**
  3590. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3591. *
  3592. * @state: crtc's scaler state
  3593. *
  3594. * Return
  3595. * 0 - scaler_usage updated successfully
  3596. * error - requested scaling cannot be supported or other error condition
  3597. */
  3598. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3599. {
  3600. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3601. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3602. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3603. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3604. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3605. &state->scaler_state.scaler_id, BIT(DRM_ROTATE_0),
  3606. state->pipe_src_w, state->pipe_src_h,
  3607. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3608. }
  3609. /**
  3610. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3611. *
  3612. * @state: crtc's scaler state
  3613. * @plane_state: atomic plane state to update
  3614. *
  3615. * Return
  3616. * 0 - scaler_usage updated successfully
  3617. * error - requested scaling cannot be supported or other error condition
  3618. */
  3619. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3620. struct intel_plane_state *plane_state)
  3621. {
  3622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3623. struct intel_plane *intel_plane =
  3624. to_intel_plane(plane_state->base.plane);
  3625. struct drm_framebuffer *fb = plane_state->base.fb;
  3626. int ret;
  3627. bool force_detach = !fb || !plane_state->visible;
  3628. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3629. intel_plane->base.base.id, intel_crtc->pipe,
  3630. drm_plane_index(&intel_plane->base));
  3631. ret = skl_update_scaler(crtc_state, force_detach,
  3632. drm_plane_index(&intel_plane->base),
  3633. &plane_state->scaler_id,
  3634. plane_state->base.rotation,
  3635. drm_rect_width(&plane_state->src) >> 16,
  3636. drm_rect_height(&plane_state->src) >> 16,
  3637. drm_rect_width(&plane_state->dst),
  3638. drm_rect_height(&plane_state->dst));
  3639. if (ret || plane_state->scaler_id < 0)
  3640. return ret;
  3641. /* check colorkey */
  3642. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3643. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3644. intel_plane->base.base.id);
  3645. return -EINVAL;
  3646. }
  3647. /* Check src format */
  3648. switch (fb->pixel_format) {
  3649. case DRM_FORMAT_RGB565:
  3650. case DRM_FORMAT_XBGR8888:
  3651. case DRM_FORMAT_XRGB8888:
  3652. case DRM_FORMAT_ABGR8888:
  3653. case DRM_FORMAT_ARGB8888:
  3654. case DRM_FORMAT_XRGB2101010:
  3655. case DRM_FORMAT_XBGR2101010:
  3656. case DRM_FORMAT_YUYV:
  3657. case DRM_FORMAT_YVYU:
  3658. case DRM_FORMAT_UYVY:
  3659. case DRM_FORMAT_VYUY:
  3660. break;
  3661. default:
  3662. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3663. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3664. return -EINVAL;
  3665. }
  3666. return 0;
  3667. }
  3668. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3669. {
  3670. int i;
  3671. for (i = 0; i < crtc->num_scalers; i++)
  3672. skl_detach_scaler(crtc, i);
  3673. }
  3674. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3675. {
  3676. struct drm_device *dev = crtc->base.dev;
  3677. struct drm_i915_private *dev_priv = dev->dev_private;
  3678. int pipe = crtc->pipe;
  3679. struct intel_crtc_scaler_state *scaler_state =
  3680. &crtc->config->scaler_state;
  3681. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3682. if (crtc->config->pch_pfit.enabled) {
  3683. int id;
  3684. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3685. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3686. return;
  3687. }
  3688. id = scaler_state->scaler_id;
  3689. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3690. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3691. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3692. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3693. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3694. }
  3695. }
  3696. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3697. {
  3698. struct drm_device *dev = crtc->base.dev;
  3699. struct drm_i915_private *dev_priv = dev->dev_private;
  3700. int pipe = crtc->pipe;
  3701. if (crtc->config->pch_pfit.enabled) {
  3702. /* Force use of hard-coded filter coefficients
  3703. * as some pre-programmed values are broken,
  3704. * e.g. x201.
  3705. */
  3706. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3707. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3708. PF_PIPE_SEL_IVB(pipe));
  3709. else
  3710. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3711. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3712. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3713. }
  3714. }
  3715. void hsw_enable_ips(struct intel_crtc *crtc)
  3716. {
  3717. struct drm_device *dev = crtc->base.dev;
  3718. struct drm_i915_private *dev_priv = dev->dev_private;
  3719. if (!crtc->config->ips_enabled)
  3720. return;
  3721. /*
  3722. * We can only enable IPS after we enable a plane and wait for a vblank
  3723. * This function is called from post_plane_update, which is run after
  3724. * a vblank wait.
  3725. */
  3726. assert_plane_enabled(dev_priv, crtc->plane);
  3727. if (IS_BROADWELL(dev)) {
  3728. mutex_lock(&dev_priv->rps.hw_lock);
  3729. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3730. mutex_unlock(&dev_priv->rps.hw_lock);
  3731. /* Quoting Art Runyan: "its not safe to expect any particular
  3732. * value in IPS_CTL bit 31 after enabling IPS through the
  3733. * mailbox." Moreover, the mailbox may return a bogus state,
  3734. * so we need to just enable it and continue on.
  3735. */
  3736. } else {
  3737. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3738. /* The bit only becomes 1 in the next vblank, so this wait here
  3739. * is essentially intel_wait_for_vblank. If we don't have this
  3740. * and don't wait for vblanks until the end of crtc_enable, then
  3741. * the HW state readout code will complain that the expected
  3742. * IPS_CTL value is not the one we read. */
  3743. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3744. DRM_ERROR("Timed out waiting for IPS enable\n");
  3745. }
  3746. }
  3747. void hsw_disable_ips(struct intel_crtc *crtc)
  3748. {
  3749. struct drm_device *dev = crtc->base.dev;
  3750. struct drm_i915_private *dev_priv = dev->dev_private;
  3751. if (!crtc->config->ips_enabled)
  3752. return;
  3753. assert_plane_enabled(dev_priv, crtc->plane);
  3754. if (IS_BROADWELL(dev)) {
  3755. mutex_lock(&dev_priv->rps.hw_lock);
  3756. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3757. mutex_unlock(&dev_priv->rps.hw_lock);
  3758. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3759. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3760. DRM_ERROR("Timed out waiting for IPS disable\n");
  3761. } else {
  3762. I915_WRITE(IPS_CTL, 0);
  3763. POSTING_READ(IPS_CTL);
  3764. }
  3765. /* We need to wait for a vblank before we can disable the plane. */
  3766. intel_wait_for_vblank(dev, crtc->pipe);
  3767. }
  3768. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3769. {
  3770. if (intel_crtc->overlay) {
  3771. struct drm_device *dev = intel_crtc->base.dev;
  3772. struct drm_i915_private *dev_priv = dev->dev_private;
  3773. mutex_lock(&dev->struct_mutex);
  3774. dev_priv->mm.interruptible = false;
  3775. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3776. dev_priv->mm.interruptible = true;
  3777. mutex_unlock(&dev->struct_mutex);
  3778. }
  3779. /* Let userspace switch the overlay on again. In most cases userspace
  3780. * has to recompute where to put it anyway.
  3781. */
  3782. }
  3783. /**
  3784. * intel_post_enable_primary - Perform operations after enabling primary plane
  3785. * @crtc: the CRTC whose primary plane was just enabled
  3786. *
  3787. * Performs potentially sleeping operations that must be done after the primary
  3788. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3789. * called due to an explicit primary plane update, or due to an implicit
  3790. * re-enable that is caused when a sprite plane is updated to no longer
  3791. * completely hide the primary plane.
  3792. */
  3793. static void
  3794. intel_post_enable_primary(struct drm_crtc *crtc)
  3795. {
  3796. struct drm_device *dev = crtc->dev;
  3797. struct drm_i915_private *dev_priv = dev->dev_private;
  3798. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3799. int pipe = intel_crtc->pipe;
  3800. /*
  3801. * FIXME IPS should be fine as long as one plane is
  3802. * enabled, but in practice it seems to have problems
  3803. * when going from primary only to sprite only and vice
  3804. * versa.
  3805. */
  3806. hsw_enable_ips(intel_crtc);
  3807. /*
  3808. * Gen2 reports pipe underruns whenever all planes are disabled.
  3809. * So don't enable underrun reporting before at least some planes
  3810. * are enabled.
  3811. * FIXME: Need to fix the logic to work when we turn off all planes
  3812. * but leave the pipe running.
  3813. */
  3814. if (IS_GEN2(dev))
  3815. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3816. /* Underruns don't always raise interrupts, so check manually. */
  3817. intel_check_cpu_fifo_underruns(dev_priv);
  3818. intel_check_pch_fifo_underruns(dev_priv);
  3819. }
  3820. /* FIXME move all this to pre_plane_update() with proper state tracking */
  3821. static void
  3822. intel_pre_disable_primary(struct drm_crtc *crtc)
  3823. {
  3824. struct drm_device *dev = crtc->dev;
  3825. struct drm_i915_private *dev_priv = dev->dev_private;
  3826. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3827. int pipe = intel_crtc->pipe;
  3828. /*
  3829. * Gen2 reports pipe underruns whenever all planes are disabled.
  3830. * So diasble underrun reporting before all the planes get disabled.
  3831. * FIXME: Need to fix the logic to work when we turn off all planes
  3832. * but leave the pipe running.
  3833. */
  3834. if (IS_GEN2(dev))
  3835. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3836. /*
  3837. * FIXME IPS should be fine as long as one plane is
  3838. * enabled, but in practice it seems to have problems
  3839. * when going from primary only to sprite only and vice
  3840. * versa.
  3841. */
  3842. hsw_disable_ips(intel_crtc);
  3843. }
  3844. /* FIXME get rid of this and use pre_plane_update */
  3845. static void
  3846. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  3847. {
  3848. struct drm_device *dev = crtc->dev;
  3849. struct drm_i915_private *dev_priv = dev->dev_private;
  3850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3851. int pipe = intel_crtc->pipe;
  3852. intel_pre_disable_primary(crtc);
  3853. /*
  3854. * Vblank time updates from the shadow to live plane control register
  3855. * are blocked if the memory self-refresh mode is active at that
  3856. * moment. So to make sure the plane gets truly disabled, disable
  3857. * first the self-refresh mode. The self-refresh enable bit in turn
  3858. * will be checked/applied by the HW only at the next frame start
  3859. * event which is after the vblank start event, so we need to have a
  3860. * wait-for-vblank between disabling the plane and the pipe.
  3861. */
  3862. if (HAS_GMCH_DISPLAY(dev)) {
  3863. intel_set_memory_cxsr(dev_priv, false);
  3864. dev_priv->wm.vlv.cxsr = false;
  3865. intel_wait_for_vblank(dev, pipe);
  3866. }
  3867. }
  3868. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  3869. {
  3870. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  3871. struct drm_device *dev = crtc->base.dev;
  3872. struct drm_i915_private *dev_priv = dev->dev_private;
  3873. struct intel_crtc_state *pipe_config =
  3874. to_intel_crtc_state(crtc->base.state);
  3875. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  3876. struct drm_plane *primary = crtc->base.primary;
  3877. struct drm_plane_state *old_pri_state =
  3878. drm_atomic_get_existing_plane_state(old_state, primary);
  3879. bool modeset = needs_modeset(&pipe_config->base);
  3880. if (old_pri_state) {
  3881. struct intel_plane_state *primary_state =
  3882. to_intel_plane_state(primary->state);
  3883. struct intel_plane_state *old_primary_state =
  3884. to_intel_plane_state(old_pri_state);
  3885. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  3886. if (old_primary_state->visible &&
  3887. (modeset || !primary_state->visible))
  3888. intel_pre_disable_primary(&crtc->base);
  3889. }
  3890. if (pipe_config->disable_cxsr) {
  3891. crtc->wm.cxsr_allowed = false;
  3892. /*
  3893. * Vblank time updates from the shadow to live plane control register
  3894. * are blocked if the memory self-refresh mode is active at that
  3895. * moment. So to make sure the plane gets truly disabled, disable
  3896. * first the self-refresh mode. The self-refresh enable bit in turn
  3897. * will be checked/applied by the HW only at the next frame start
  3898. * event which is after the vblank start event, so we need to have a
  3899. * wait-for-vblank between disabling the plane and the pipe.
  3900. */
  3901. if (old_crtc_state->base.active) {
  3902. intel_set_memory_cxsr(dev_priv, false);
  3903. dev_priv->wm.vlv.cxsr = false;
  3904. intel_wait_for_vblank(dev, crtc->pipe);
  3905. }
  3906. }
  3907. /*
  3908. * IVB workaround: must disable low power watermarks for at least
  3909. * one frame before enabling scaling. LP watermarks can be re-enabled
  3910. * when scaling is disabled.
  3911. *
  3912. * WaCxSRDisabledForSpriteScaling:ivb
  3913. */
  3914. if (pipe_config->disable_lp_wm) {
  3915. ilk_disable_lp_wm(dev);
  3916. intel_wait_for_vblank(dev, crtc->pipe);
  3917. }
  3918. /*
  3919. * If we're doing a modeset, we're done. No need to do any pre-vblank
  3920. * watermark programming here.
  3921. */
  3922. if (needs_modeset(&pipe_config->base))
  3923. return;
  3924. /*
  3925. * For platforms that support atomic watermarks, program the
  3926. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  3927. * will be the intermediate values that are safe for both pre- and
  3928. * post- vblank; when vblank happens, the 'active' values will be set
  3929. * to the final 'target' values and we'll do this again to get the
  3930. * optimal watermarks. For gen9+ platforms, the values we program here
  3931. * will be the final target values which will get automatically latched
  3932. * at vblank time; no further programming will be necessary.
  3933. *
  3934. * If a platform hasn't been transitioned to atomic watermarks yet,
  3935. * we'll continue to update watermarks the old way, if flags tell
  3936. * us to.
  3937. */
  3938. if (dev_priv->display.initial_watermarks != NULL)
  3939. dev_priv->display.initial_watermarks(pipe_config);
  3940. else if (pipe_config->update_wm_pre)
  3941. intel_update_watermarks(&crtc->base);
  3942. }
  3943. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  3944. {
  3945. struct drm_device *dev = crtc->dev;
  3946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3947. struct drm_plane *p;
  3948. int pipe = intel_crtc->pipe;
  3949. intel_crtc_dpms_overlay_disable(intel_crtc);
  3950. drm_for_each_plane_mask(p, dev, plane_mask)
  3951. to_intel_plane(p)->disable_plane(p, crtc);
  3952. /*
  3953. * FIXME: Once we grow proper nuclear flip support out of this we need
  3954. * to compute the mask of flip planes precisely. For the time being
  3955. * consider this a flip to a NULL plane.
  3956. */
  3957. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  3958. }
  3959. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3960. {
  3961. struct drm_device *dev = crtc->dev;
  3962. struct drm_i915_private *dev_priv = dev->dev_private;
  3963. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3964. struct intel_encoder *encoder;
  3965. int pipe = intel_crtc->pipe;
  3966. struct intel_crtc_state *pipe_config =
  3967. to_intel_crtc_state(crtc->state);
  3968. if (WARN_ON(intel_crtc->active))
  3969. return;
  3970. /*
  3971. * Sometimes spurious CPU pipe underruns happen during FDI
  3972. * training, at least with VGA+HDMI cloning. Suppress them.
  3973. *
  3974. * On ILK we get an occasional spurious CPU pipe underruns
  3975. * between eDP port A enable and vdd enable. Also PCH port
  3976. * enable seems to result in the occasional CPU pipe underrun.
  3977. *
  3978. * Spurious PCH underruns also occur during PCH enabling.
  3979. */
  3980. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  3981. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  3982. if (intel_crtc->config->has_pch_encoder)
  3983. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  3984. if (intel_crtc->config->has_pch_encoder)
  3985. intel_prepare_shared_dpll(intel_crtc);
  3986. if (intel_crtc->config->has_dp_encoder)
  3987. intel_dp_set_m_n(intel_crtc, M1_N1);
  3988. intel_set_pipe_timings(intel_crtc);
  3989. intel_set_pipe_src_size(intel_crtc);
  3990. if (intel_crtc->config->has_pch_encoder) {
  3991. intel_cpu_transcoder_set_m_n(intel_crtc,
  3992. &intel_crtc->config->fdi_m_n, NULL);
  3993. }
  3994. ironlake_set_pipeconf(crtc);
  3995. intel_crtc->active = true;
  3996. for_each_encoder_on_crtc(dev, crtc, encoder)
  3997. if (encoder->pre_enable)
  3998. encoder->pre_enable(encoder);
  3999. if (intel_crtc->config->has_pch_encoder) {
  4000. /* Note: FDI PLL enabling _must_ be done before we enable the
  4001. * cpu pipes, hence this is separate from all the other fdi/pch
  4002. * enabling. */
  4003. ironlake_fdi_pll_enable(intel_crtc);
  4004. } else {
  4005. assert_fdi_tx_disabled(dev_priv, pipe);
  4006. assert_fdi_rx_disabled(dev_priv, pipe);
  4007. }
  4008. ironlake_pfit_enable(intel_crtc);
  4009. /*
  4010. * On ILK+ LUT must be loaded before the pipe is running but with
  4011. * clocks enabled
  4012. */
  4013. intel_color_load_luts(&pipe_config->base);
  4014. if (dev_priv->display.initial_watermarks != NULL)
  4015. dev_priv->display.initial_watermarks(intel_crtc->config);
  4016. intel_enable_pipe(intel_crtc);
  4017. if (intel_crtc->config->has_pch_encoder)
  4018. ironlake_pch_enable(crtc);
  4019. assert_vblank_disabled(crtc);
  4020. drm_crtc_vblank_on(crtc);
  4021. for_each_encoder_on_crtc(dev, crtc, encoder)
  4022. encoder->enable(encoder);
  4023. if (HAS_PCH_CPT(dev))
  4024. cpt_verify_modeset(dev, intel_crtc->pipe);
  4025. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4026. if (intel_crtc->config->has_pch_encoder)
  4027. intel_wait_for_vblank(dev, pipe);
  4028. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4029. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4030. }
  4031. /* IPS only exists on ULT machines and is tied to pipe A. */
  4032. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4033. {
  4034. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4035. }
  4036. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4037. {
  4038. struct drm_device *dev = crtc->dev;
  4039. struct drm_i915_private *dev_priv = dev->dev_private;
  4040. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4041. struct intel_encoder *encoder;
  4042. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4043. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4044. struct intel_crtc_state *pipe_config =
  4045. to_intel_crtc_state(crtc->state);
  4046. if (WARN_ON(intel_crtc->active))
  4047. return;
  4048. if (intel_crtc->config->has_pch_encoder)
  4049. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4050. false);
  4051. if (intel_crtc->config->shared_dpll)
  4052. intel_enable_shared_dpll(intel_crtc);
  4053. if (intel_crtc->config->has_dp_encoder)
  4054. intel_dp_set_m_n(intel_crtc, M1_N1);
  4055. if (!intel_crtc->config->has_dsi_encoder)
  4056. intel_set_pipe_timings(intel_crtc);
  4057. intel_set_pipe_src_size(intel_crtc);
  4058. if (cpu_transcoder != TRANSCODER_EDP &&
  4059. !transcoder_is_dsi(cpu_transcoder)) {
  4060. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4061. intel_crtc->config->pixel_multiplier - 1);
  4062. }
  4063. if (intel_crtc->config->has_pch_encoder) {
  4064. intel_cpu_transcoder_set_m_n(intel_crtc,
  4065. &intel_crtc->config->fdi_m_n, NULL);
  4066. }
  4067. if (!intel_crtc->config->has_dsi_encoder)
  4068. haswell_set_pipeconf(crtc);
  4069. haswell_set_pipemisc(crtc);
  4070. intel_color_set_csc(&pipe_config->base);
  4071. intel_crtc->active = true;
  4072. if (intel_crtc->config->has_pch_encoder)
  4073. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4074. else
  4075. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4076. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4077. if (encoder->pre_enable)
  4078. encoder->pre_enable(encoder);
  4079. }
  4080. if (intel_crtc->config->has_pch_encoder)
  4081. dev_priv->display.fdi_link_train(crtc);
  4082. if (!intel_crtc->config->has_dsi_encoder)
  4083. intel_ddi_enable_pipe_clock(intel_crtc);
  4084. if (INTEL_INFO(dev)->gen >= 9)
  4085. skylake_pfit_enable(intel_crtc);
  4086. else
  4087. ironlake_pfit_enable(intel_crtc);
  4088. /*
  4089. * On ILK+ LUT must be loaded before the pipe is running but with
  4090. * clocks enabled
  4091. */
  4092. intel_color_load_luts(&pipe_config->base);
  4093. intel_ddi_set_pipe_settings(crtc);
  4094. if (!intel_crtc->config->has_dsi_encoder)
  4095. intel_ddi_enable_transcoder_func(crtc);
  4096. if (dev_priv->display.initial_watermarks != NULL)
  4097. dev_priv->display.initial_watermarks(pipe_config);
  4098. else
  4099. intel_update_watermarks(crtc);
  4100. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4101. if (!intel_crtc->config->has_dsi_encoder)
  4102. intel_enable_pipe(intel_crtc);
  4103. if (intel_crtc->config->has_pch_encoder)
  4104. lpt_pch_enable(crtc);
  4105. if (intel_crtc->config->dp_encoder_is_mst)
  4106. intel_ddi_set_vc_payload_alloc(crtc, true);
  4107. assert_vblank_disabled(crtc);
  4108. drm_crtc_vblank_on(crtc);
  4109. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4110. encoder->enable(encoder);
  4111. intel_opregion_notify_encoder(encoder, true);
  4112. }
  4113. if (intel_crtc->config->has_pch_encoder) {
  4114. intel_wait_for_vblank(dev, pipe);
  4115. intel_wait_for_vblank(dev, pipe);
  4116. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4117. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4118. true);
  4119. }
  4120. /* If we change the relative order between pipe/planes enabling, we need
  4121. * to change the workaround. */
  4122. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4123. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4124. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4125. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4126. }
  4127. }
  4128. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4129. {
  4130. struct drm_device *dev = crtc->base.dev;
  4131. struct drm_i915_private *dev_priv = dev->dev_private;
  4132. int pipe = crtc->pipe;
  4133. /* To avoid upsetting the power well on haswell only disable the pfit if
  4134. * it's in use. The hw state code will make sure we get this right. */
  4135. if (force || crtc->config->pch_pfit.enabled) {
  4136. I915_WRITE(PF_CTL(pipe), 0);
  4137. I915_WRITE(PF_WIN_POS(pipe), 0);
  4138. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4139. }
  4140. }
  4141. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4142. {
  4143. struct drm_device *dev = crtc->dev;
  4144. struct drm_i915_private *dev_priv = dev->dev_private;
  4145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4146. struct intel_encoder *encoder;
  4147. int pipe = intel_crtc->pipe;
  4148. /*
  4149. * Sometimes spurious CPU pipe underruns happen when the
  4150. * pipe is already disabled, but FDI RX/TX is still enabled.
  4151. * Happens at least with VGA+HDMI cloning. Suppress them.
  4152. */
  4153. if (intel_crtc->config->has_pch_encoder) {
  4154. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4155. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4156. }
  4157. for_each_encoder_on_crtc(dev, crtc, encoder)
  4158. encoder->disable(encoder);
  4159. drm_crtc_vblank_off(crtc);
  4160. assert_vblank_disabled(crtc);
  4161. intel_disable_pipe(intel_crtc);
  4162. ironlake_pfit_disable(intel_crtc, false);
  4163. if (intel_crtc->config->has_pch_encoder)
  4164. ironlake_fdi_disable(crtc);
  4165. for_each_encoder_on_crtc(dev, crtc, encoder)
  4166. if (encoder->post_disable)
  4167. encoder->post_disable(encoder);
  4168. if (intel_crtc->config->has_pch_encoder) {
  4169. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4170. if (HAS_PCH_CPT(dev)) {
  4171. i915_reg_t reg;
  4172. u32 temp;
  4173. /* disable TRANS_DP_CTL */
  4174. reg = TRANS_DP_CTL(pipe);
  4175. temp = I915_READ(reg);
  4176. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4177. TRANS_DP_PORT_SEL_MASK);
  4178. temp |= TRANS_DP_PORT_SEL_NONE;
  4179. I915_WRITE(reg, temp);
  4180. /* disable DPLL_SEL */
  4181. temp = I915_READ(PCH_DPLL_SEL);
  4182. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4183. I915_WRITE(PCH_DPLL_SEL, temp);
  4184. }
  4185. ironlake_fdi_pll_disable(intel_crtc);
  4186. }
  4187. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4188. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4189. }
  4190. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4191. {
  4192. struct drm_device *dev = crtc->dev;
  4193. struct drm_i915_private *dev_priv = dev->dev_private;
  4194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4195. struct intel_encoder *encoder;
  4196. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4197. if (intel_crtc->config->has_pch_encoder)
  4198. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4199. false);
  4200. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4201. intel_opregion_notify_encoder(encoder, false);
  4202. encoder->disable(encoder);
  4203. }
  4204. drm_crtc_vblank_off(crtc);
  4205. assert_vblank_disabled(crtc);
  4206. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4207. if (!intel_crtc->config->has_dsi_encoder)
  4208. intel_disable_pipe(intel_crtc);
  4209. if (intel_crtc->config->dp_encoder_is_mst)
  4210. intel_ddi_set_vc_payload_alloc(crtc, false);
  4211. if (!intel_crtc->config->has_dsi_encoder)
  4212. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4213. if (INTEL_INFO(dev)->gen >= 9)
  4214. skylake_scaler_disable(intel_crtc);
  4215. else
  4216. ironlake_pfit_disable(intel_crtc, false);
  4217. if (!intel_crtc->config->has_dsi_encoder)
  4218. intel_ddi_disable_pipe_clock(intel_crtc);
  4219. for_each_encoder_on_crtc(dev, crtc, encoder)
  4220. if (encoder->post_disable)
  4221. encoder->post_disable(encoder);
  4222. if (intel_crtc->config->has_pch_encoder) {
  4223. lpt_disable_pch_transcoder(dev_priv);
  4224. lpt_disable_iclkip(dev_priv);
  4225. intel_ddi_fdi_disable(crtc);
  4226. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4227. true);
  4228. }
  4229. }
  4230. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4231. {
  4232. struct drm_device *dev = crtc->base.dev;
  4233. struct drm_i915_private *dev_priv = dev->dev_private;
  4234. struct intel_crtc_state *pipe_config = crtc->config;
  4235. if (!pipe_config->gmch_pfit.control)
  4236. return;
  4237. /*
  4238. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4239. * according to register description and PRM.
  4240. */
  4241. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4242. assert_pipe_disabled(dev_priv, crtc->pipe);
  4243. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4244. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4245. /* Border color in case we don't scale up to the full screen. Black by
  4246. * default, change to something else for debugging. */
  4247. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4248. }
  4249. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4250. {
  4251. switch (port) {
  4252. case PORT_A:
  4253. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4254. case PORT_B:
  4255. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4256. case PORT_C:
  4257. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4258. case PORT_D:
  4259. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4260. case PORT_E:
  4261. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4262. default:
  4263. MISSING_CASE(port);
  4264. return POWER_DOMAIN_PORT_OTHER;
  4265. }
  4266. }
  4267. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4268. {
  4269. switch (port) {
  4270. case PORT_A:
  4271. return POWER_DOMAIN_AUX_A;
  4272. case PORT_B:
  4273. return POWER_DOMAIN_AUX_B;
  4274. case PORT_C:
  4275. return POWER_DOMAIN_AUX_C;
  4276. case PORT_D:
  4277. return POWER_DOMAIN_AUX_D;
  4278. case PORT_E:
  4279. /* FIXME: Check VBT for actual wiring of PORT E */
  4280. return POWER_DOMAIN_AUX_D;
  4281. default:
  4282. MISSING_CASE(port);
  4283. return POWER_DOMAIN_AUX_A;
  4284. }
  4285. }
  4286. enum intel_display_power_domain
  4287. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4288. {
  4289. struct drm_device *dev = intel_encoder->base.dev;
  4290. struct intel_digital_port *intel_dig_port;
  4291. switch (intel_encoder->type) {
  4292. case INTEL_OUTPUT_UNKNOWN:
  4293. /* Only DDI platforms should ever use this output type */
  4294. WARN_ON_ONCE(!HAS_DDI(dev));
  4295. case INTEL_OUTPUT_DISPLAYPORT:
  4296. case INTEL_OUTPUT_HDMI:
  4297. case INTEL_OUTPUT_EDP:
  4298. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4299. return port_to_power_domain(intel_dig_port->port);
  4300. case INTEL_OUTPUT_DP_MST:
  4301. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4302. return port_to_power_domain(intel_dig_port->port);
  4303. case INTEL_OUTPUT_ANALOG:
  4304. return POWER_DOMAIN_PORT_CRT;
  4305. case INTEL_OUTPUT_DSI:
  4306. return POWER_DOMAIN_PORT_DSI;
  4307. default:
  4308. return POWER_DOMAIN_PORT_OTHER;
  4309. }
  4310. }
  4311. enum intel_display_power_domain
  4312. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4313. {
  4314. struct drm_device *dev = intel_encoder->base.dev;
  4315. struct intel_digital_port *intel_dig_port;
  4316. switch (intel_encoder->type) {
  4317. case INTEL_OUTPUT_UNKNOWN:
  4318. case INTEL_OUTPUT_HDMI:
  4319. /*
  4320. * Only DDI platforms should ever use these output types.
  4321. * We can get here after the HDMI detect code has already set
  4322. * the type of the shared encoder. Since we can't be sure
  4323. * what's the status of the given connectors, play safe and
  4324. * run the DP detection too.
  4325. */
  4326. WARN_ON_ONCE(!HAS_DDI(dev));
  4327. case INTEL_OUTPUT_DISPLAYPORT:
  4328. case INTEL_OUTPUT_EDP:
  4329. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4330. return port_to_aux_power_domain(intel_dig_port->port);
  4331. case INTEL_OUTPUT_DP_MST:
  4332. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4333. return port_to_aux_power_domain(intel_dig_port->port);
  4334. default:
  4335. MISSING_CASE(intel_encoder->type);
  4336. return POWER_DOMAIN_AUX_A;
  4337. }
  4338. }
  4339. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4340. struct intel_crtc_state *crtc_state)
  4341. {
  4342. struct drm_device *dev = crtc->dev;
  4343. struct drm_encoder *encoder;
  4344. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4345. enum pipe pipe = intel_crtc->pipe;
  4346. unsigned long mask;
  4347. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4348. if (!crtc_state->base.active)
  4349. return 0;
  4350. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4351. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4352. if (crtc_state->pch_pfit.enabled ||
  4353. crtc_state->pch_pfit.force_thru)
  4354. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4355. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4356. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4357. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4358. }
  4359. if (crtc_state->shared_dpll)
  4360. mask |= BIT(POWER_DOMAIN_PLLS);
  4361. return mask;
  4362. }
  4363. static unsigned long
  4364. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4365. struct intel_crtc_state *crtc_state)
  4366. {
  4367. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4368. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4369. enum intel_display_power_domain domain;
  4370. unsigned long domains, new_domains, old_domains, ms_domain = 0;
  4371. old_domains = intel_crtc->enabled_power_domains;
  4372. intel_crtc->enabled_power_domains = new_domains =
  4373. get_crtc_power_domains(crtc, crtc_state);
  4374. if (needs_modeset(&crtc_state->base))
  4375. ms_domain = BIT(POWER_DOMAIN_MODESET);
  4376. domains = (new_domains & ~old_domains) | ms_domain;
  4377. for_each_power_domain(domain, domains)
  4378. intel_display_power_get(dev_priv, domain);
  4379. return (old_domains & ~new_domains) | ms_domain;
  4380. }
  4381. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4382. unsigned long domains)
  4383. {
  4384. enum intel_display_power_domain domain;
  4385. for_each_power_domain(domain, domains)
  4386. intel_display_power_put(dev_priv, domain);
  4387. }
  4388. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4389. {
  4390. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4391. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4392. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4393. return max_cdclk_freq;
  4394. else if (IS_CHERRYVIEW(dev_priv))
  4395. return max_cdclk_freq*95/100;
  4396. else if (INTEL_INFO(dev_priv)->gen < 4)
  4397. return 2*max_cdclk_freq*90/100;
  4398. else
  4399. return max_cdclk_freq*90/100;
  4400. }
  4401. static int skl_calc_cdclk(int max_pixclk, int vco);
  4402. static void intel_update_max_cdclk(struct drm_device *dev)
  4403. {
  4404. struct drm_i915_private *dev_priv = dev->dev_private;
  4405. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4406. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4407. int max_cdclk, vco;
  4408. vco = dev_priv->skl_preferred_vco_freq;
  4409. WARN_ON(vco != 8100000 && vco != 8640000);
  4410. /*
  4411. * Use the lower (vco 8640) cdclk values as a
  4412. * first guess. skl_calc_cdclk() will correct it
  4413. * if the preferred vco is 8100 instead.
  4414. */
  4415. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4416. max_cdclk = 617143;
  4417. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4418. max_cdclk = 540000;
  4419. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4420. max_cdclk = 432000;
  4421. else
  4422. max_cdclk = 308571;
  4423. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4424. } else if (IS_BROXTON(dev)) {
  4425. dev_priv->max_cdclk_freq = 624000;
  4426. } else if (IS_BROADWELL(dev)) {
  4427. /*
  4428. * FIXME with extra cooling we can allow
  4429. * 540 MHz for ULX and 675 Mhz for ULT.
  4430. * How can we know if extra cooling is
  4431. * available? PCI ID, VTB, something else?
  4432. */
  4433. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4434. dev_priv->max_cdclk_freq = 450000;
  4435. else if (IS_BDW_ULX(dev))
  4436. dev_priv->max_cdclk_freq = 450000;
  4437. else if (IS_BDW_ULT(dev))
  4438. dev_priv->max_cdclk_freq = 540000;
  4439. else
  4440. dev_priv->max_cdclk_freq = 675000;
  4441. } else if (IS_CHERRYVIEW(dev)) {
  4442. dev_priv->max_cdclk_freq = 320000;
  4443. } else if (IS_VALLEYVIEW(dev)) {
  4444. dev_priv->max_cdclk_freq = 400000;
  4445. } else {
  4446. /* otherwise assume cdclk is fixed */
  4447. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4448. }
  4449. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4450. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4451. dev_priv->max_cdclk_freq);
  4452. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4453. dev_priv->max_dotclk_freq);
  4454. }
  4455. static void intel_update_cdclk(struct drm_device *dev)
  4456. {
  4457. struct drm_i915_private *dev_priv = dev->dev_private;
  4458. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4459. if (INTEL_GEN(dev_priv) >= 9)
  4460. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4461. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4462. dev_priv->cdclk_pll.ref);
  4463. else
  4464. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4465. dev_priv->cdclk_freq);
  4466. /*
  4467. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4468. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4469. * of cdclk that generates 4MHz reference clock freq which is used to
  4470. * generate GMBus clock. This will vary with the cdclk freq.
  4471. */
  4472. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4473. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4474. }
  4475. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4476. static int skl_cdclk_decimal(int cdclk)
  4477. {
  4478. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4479. }
  4480. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  4481. {
  4482. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  4483. /* Timeout 200us */
  4484. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
  4485. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  4486. dev_priv->cdclk_pll.vco = 0;
  4487. }
  4488. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, u32 ratio)
  4489. {
  4490. u32 val;
  4491. val = I915_READ(BXT_DE_PLL_CTL);
  4492. val &= ~BXT_DE_PLL_RATIO_MASK;
  4493. val |= ratio;
  4494. I915_WRITE(BXT_DE_PLL_CTL, val);
  4495. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4496. /* Timeout 200us */
  4497. if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
  4498. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4499. dev_priv->cdclk_pll.vco = ratio * dev_priv->cdclk_pll.ref;
  4500. }
  4501. static void broxton_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  4502. {
  4503. uint32_t divider;
  4504. uint32_t ratio;
  4505. uint32_t current_cdclk;
  4506. int ret;
  4507. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4508. switch (cdclk) {
  4509. case 144000:
  4510. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4511. ratio = BXT_DE_PLL_RATIO(60);
  4512. break;
  4513. case 288000:
  4514. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4515. ratio = BXT_DE_PLL_RATIO(60);
  4516. break;
  4517. case 384000:
  4518. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4519. ratio = BXT_DE_PLL_RATIO(60);
  4520. break;
  4521. case 576000:
  4522. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4523. ratio = BXT_DE_PLL_RATIO(60);
  4524. break;
  4525. case 624000:
  4526. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4527. ratio = BXT_DE_PLL_RATIO(65);
  4528. break;
  4529. case 19200:
  4530. /*
  4531. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4532. * to suppress GCC warning.
  4533. */
  4534. ratio = 0;
  4535. divider = 0;
  4536. break;
  4537. default:
  4538. DRM_ERROR("unsupported CDCLK freq %d", cdclk);
  4539. return;
  4540. }
  4541. mutex_lock(&dev_priv->rps.hw_lock);
  4542. /* Inform power controller of upcoming frequency change */
  4543. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4544. 0x80000000);
  4545. mutex_unlock(&dev_priv->rps.hw_lock);
  4546. if (ret) {
  4547. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4548. ret, cdclk);
  4549. return;
  4550. }
  4551. current_cdclk = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4552. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4553. current_cdclk = current_cdclk * 500 + 1000;
  4554. /*
  4555. * DE PLL has to be disabled when
  4556. * - setting to 19.2MHz (bypass, PLL isn't used)
  4557. * - before setting to 624MHz (PLL needs toggling)
  4558. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4559. */
  4560. if (cdclk == 19200 || cdclk == 624000 ||
  4561. current_cdclk == 624000) {
  4562. bxt_de_pll_disable(dev_priv);
  4563. }
  4564. if (cdclk != 19200) {
  4565. uint32_t val;
  4566. bxt_de_pll_enable(dev_priv, ratio);
  4567. val = divider | skl_cdclk_decimal(cdclk);
  4568. /*
  4569. * FIXME if only the cd2x divider needs changing, it could be done
  4570. * without shutting off the pipe (if only one pipe is active).
  4571. */
  4572. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  4573. /*
  4574. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4575. * enable otherwise.
  4576. */
  4577. if (cdclk >= 500000)
  4578. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4579. I915_WRITE(CDCLK_CTL, val);
  4580. }
  4581. mutex_lock(&dev_priv->rps.hw_lock);
  4582. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4583. DIV_ROUND_UP(cdclk, 25000));
  4584. mutex_unlock(&dev_priv->rps.hw_lock);
  4585. if (ret) {
  4586. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4587. ret, cdclk);
  4588. return;
  4589. }
  4590. intel_update_cdclk(dev_priv->dev);
  4591. }
  4592. static bool broxton_cdclk_is_enabled(struct drm_i915_private *dev_priv)
  4593. {
  4594. if (!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE))
  4595. return false;
  4596. /* TODO: Check for a valid CDCLK rate */
  4597. return true;
  4598. }
  4599. bool broxton_cdclk_verify_state(struct drm_i915_private *dev_priv)
  4600. {
  4601. return broxton_cdclk_is_enabled(dev_priv);
  4602. }
  4603. void broxton_init_cdclk(struct drm_i915_private *dev_priv)
  4604. {
  4605. intel_update_cdclk(dev_priv->dev);
  4606. if (dev_priv->cdclk_pll.vco != 0)
  4607. return;
  4608. /*
  4609. * FIXME:
  4610. * - The initial CDCLK needs to be read from VBT.
  4611. * Need to make this change after VBT has changes for BXT.
  4612. * - check if setting the max (or any) cdclk freq is really necessary
  4613. * here, it belongs to modeset time
  4614. */
  4615. broxton_set_cdclk(dev_priv, 624000);
  4616. }
  4617. void broxton_uninit_cdclk(struct drm_i915_private *dev_priv)
  4618. {
  4619. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4620. broxton_set_cdclk(dev_priv, 19200);
  4621. }
  4622. static int skl_calc_cdclk(int max_pixclk, int vco)
  4623. {
  4624. if (vco == 8640000) {
  4625. if (max_pixclk > 540000)
  4626. return 617143;
  4627. else if (max_pixclk > 432000)
  4628. return 540000;
  4629. else if (max_pixclk > 308571)
  4630. return 432000;
  4631. else
  4632. return 308571;
  4633. } else {
  4634. if (max_pixclk > 540000)
  4635. return 675000;
  4636. else if (max_pixclk > 450000)
  4637. return 540000;
  4638. else if (max_pixclk > 337500)
  4639. return 450000;
  4640. else
  4641. return 337500;
  4642. }
  4643. }
  4644. static void
  4645. skl_dpll0_update(struct drm_i915_private *dev_priv)
  4646. {
  4647. u32 val;
  4648. dev_priv->cdclk_pll.ref = 24000;
  4649. val = I915_READ(LCPLL1_CTL);
  4650. if ((val & LCPLL_PLL_ENABLE) == 0) {
  4651. dev_priv->cdclk_pll.vco = 0;
  4652. return;
  4653. }
  4654. WARN_ON((val & LCPLL_PLL_LOCK) == 0);
  4655. val = I915_READ(DPLL_CTRL1);
  4656. WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4657. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4658. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4659. DPLL_CTRL1_OVERRIDE(SKL_DPLL0));
  4660. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  4661. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  4662. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  4663. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  4664. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  4665. dev_priv->cdclk_pll.vco = 8100000;
  4666. break;
  4667. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  4668. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  4669. dev_priv->cdclk_pll.vco = 8640000;
  4670. break;
  4671. default:
  4672. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4673. dev_priv->cdclk_pll.vco = 0;
  4674. break;
  4675. }
  4676. }
  4677. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  4678. {
  4679. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  4680. dev_priv->skl_preferred_vco_freq = vco;
  4681. if (changed)
  4682. intel_update_max_cdclk(dev_priv->dev);
  4683. }
  4684. static void
  4685. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  4686. {
  4687. int min_cdclk = skl_calc_cdclk(0, vco);
  4688. u32 val;
  4689. WARN_ON(vco != 8100000 && vco != 8640000);
  4690. /* select the minimum CDCLK before enabling DPLL 0 */
  4691. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  4692. I915_WRITE(CDCLK_CTL, val);
  4693. POSTING_READ(CDCLK_CTL);
  4694. /*
  4695. * We always enable DPLL0 with the lowest link rate possible, but still
  4696. * taking into account the VCO required to operate the eDP panel at the
  4697. * desired frequency. The usual DP link rates operate with a VCO of
  4698. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4699. * The modeset code is responsible for the selection of the exact link
  4700. * rate later on, with the constraint of choosing a frequency that
  4701. * works with vco.
  4702. */
  4703. val = I915_READ(DPLL_CTRL1);
  4704. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4705. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4706. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4707. if (vco == 8640000)
  4708. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4709. SKL_DPLL0);
  4710. else
  4711. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4712. SKL_DPLL0);
  4713. I915_WRITE(DPLL_CTRL1, val);
  4714. POSTING_READ(DPLL_CTRL1);
  4715. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4716. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4717. DRM_ERROR("DPLL0 not locked\n");
  4718. dev_priv->cdclk_pll.vco = vco;
  4719. /* We'll want to keep using the current vco from now on. */
  4720. skl_set_preferred_cdclk_vco(dev_priv, vco);
  4721. }
  4722. static void
  4723. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  4724. {
  4725. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4726. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4727. DRM_ERROR("Couldn't disable DPLL0\n");
  4728. dev_priv->cdclk_pll.vco = 0;
  4729. }
  4730. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4731. {
  4732. int ret;
  4733. u32 val;
  4734. /* inform PCU we want to change CDCLK */
  4735. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4736. mutex_lock(&dev_priv->rps.hw_lock);
  4737. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4738. mutex_unlock(&dev_priv->rps.hw_lock);
  4739. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4740. }
  4741. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4742. {
  4743. unsigned int i;
  4744. for (i = 0; i < 15; i++) {
  4745. if (skl_cdclk_pcu_ready(dev_priv))
  4746. return true;
  4747. udelay(10);
  4748. }
  4749. return false;
  4750. }
  4751. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  4752. {
  4753. struct drm_device *dev = dev_priv->dev;
  4754. u32 freq_select, pcu_ack;
  4755. WARN_ON((cdclk == 24000) != (vco == 0));
  4756. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  4757. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4758. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4759. return;
  4760. }
  4761. /* set CDCLK_CTL */
  4762. switch (cdclk) {
  4763. case 450000:
  4764. case 432000:
  4765. freq_select = CDCLK_FREQ_450_432;
  4766. pcu_ack = 1;
  4767. break;
  4768. case 540000:
  4769. freq_select = CDCLK_FREQ_540;
  4770. pcu_ack = 2;
  4771. break;
  4772. case 308571:
  4773. case 337500:
  4774. default:
  4775. freq_select = CDCLK_FREQ_337_308;
  4776. pcu_ack = 0;
  4777. break;
  4778. case 617143:
  4779. case 675000:
  4780. freq_select = CDCLK_FREQ_675_617;
  4781. pcu_ack = 3;
  4782. break;
  4783. }
  4784. if (dev_priv->cdclk_pll.vco != 0 &&
  4785. dev_priv->cdclk_pll.vco != vco)
  4786. skl_dpll0_disable(dev_priv);
  4787. if (dev_priv->cdclk_pll.vco != vco)
  4788. skl_dpll0_enable(dev_priv, vco);
  4789. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  4790. POSTING_READ(CDCLK_CTL);
  4791. /* inform PCU of the change */
  4792. mutex_lock(&dev_priv->rps.hw_lock);
  4793. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4794. mutex_unlock(&dev_priv->rps.hw_lock);
  4795. intel_update_cdclk(dev);
  4796. }
  4797. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  4798. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4799. {
  4800. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  4801. }
  4802. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4803. {
  4804. int cdclk, vco;
  4805. skl_sanitize_cdclk(dev_priv);
  4806. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  4807. /*
  4808. * Use the current vco as our initial
  4809. * guess as to what the preferred vco is.
  4810. */
  4811. if (dev_priv->skl_preferred_vco_freq == 0)
  4812. skl_set_preferred_cdclk_vco(dev_priv,
  4813. dev_priv->cdclk_pll.vco);
  4814. return;
  4815. }
  4816. vco = dev_priv->skl_preferred_vco_freq;
  4817. if (vco == 0)
  4818. vco = 8100000;
  4819. cdclk = skl_calc_cdclk(0, vco);
  4820. skl_set_cdclk(dev_priv, cdclk, vco);
  4821. }
  4822. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4823. {
  4824. uint32_t cdctl, expected;
  4825. /*
  4826. * check if the pre-os intialized the display
  4827. * There is SWF18 scratchpad register defined which is set by the
  4828. * pre-os which can be used by the OS drivers to check the status
  4829. */
  4830. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4831. goto sanitize;
  4832. /* Is PLL enabled and locked ? */
  4833. if ((I915_READ(LCPLL1_CTL) & (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK)) !=
  4834. (LCPLL_PLL_ENABLE | LCPLL_PLL_LOCK))
  4835. goto sanitize;
  4836. if ((I915_READ(DPLL_CTRL1) & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  4837. DPLL_CTRL1_SSC(SKL_DPLL0) |
  4838. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  4839. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))
  4840. goto sanitize;
  4841. intel_update_cdclk(dev_priv->dev);
  4842. /* DPLL okay; verify the cdclock
  4843. *
  4844. * Noticed in some instances that the freq selection is correct but
  4845. * decimal part is programmed wrong from BIOS where pre-os does not
  4846. * enable display. Verify the same as well.
  4847. */
  4848. cdctl = I915_READ(CDCLK_CTL);
  4849. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  4850. skl_cdclk_decimal(dev_priv->cdclk_freq);
  4851. if (cdctl == expected)
  4852. /* All well; nothing to sanitize */
  4853. return;
  4854. sanitize:
  4855. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  4856. /* force cdclk programming */
  4857. dev_priv->cdclk_freq = 0;
  4858. /* force full PLL disable + enable */
  4859. dev_priv->cdclk_pll.vco = -1;
  4860. }
  4861. /* Adjust CDclk dividers to allow high res or save power if possible */
  4862. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4863. {
  4864. struct drm_i915_private *dev_priv = dev->dev_private;
  4865. u32 val, cmd;
  4866. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4867. != dev_priv->cdclk_freq);
  4868. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4869. cmd = 2;
  4870. else if (cdclk == 266667)
  4871. cmd = 1;
  4872. else
  4873. cmd = 0;
  4874. mutex_lock(&dev_priv->rps.hw_lock);
  4875. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4876. val &= ~DSPFREQGUAR_MASK;
  4877. val |= (cmd << DSPFREQGUAR_SHIFT);
  4878. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4879. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4880. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4881. 50)) {
  4882. DRM_ERROR("timed out waiting for CDclk change\n");
  4883. }
  4884. mutex_unlock(&dev_priv->rps.hw_lock);
  4885. mutex_lock(&dev_priv->sb_lock);
  4886. if (cdclk == 400000) {
  4887. u32 divider;
  4888. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4889. /* adjust cdclk divider */
  4890. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4891. val &= ~CCK_FREQUENCY_VALUES;
  4892. val |= divider;
  4893. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4894. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4895. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4896. 50))
  4897. DRM_ERROR("timed out waiting for CDclk change\n");
  4898. }
  4899. /* adjust self-refresh exit latency value */
  4900. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4901. val &= ~0x7f;
  4902. /*
  4903. * For high bandwidth configs, we set a higher latency in the bunit
  4904. * so that the core display fetch happens in time to avoid underruns.
  4905. */
  4906. if (cdclk == 400000)
  4907. val |= 4500 / 250; /* 4.5 usec */
  4908. else
  4909. val |= 3000 / 250; /* 3.0 usec */
  4910. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4911. mutex_unlock(&dev_priv->sb_lock);
  4912. intel_update_cdclk(dev);
  4913. }
  4914. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4915. {
  4916. struct drm_i915_private *dev_priv = dev->dev_private;
  4917. u32 val, cmd;
  4918. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4919. != dev_priv->cdclk_freq);
  4920. switch (cdclk) {
  4921. case 333333:
  4922. case 320000:
  4923. case 266667:
  4924. case 200000:
  4925. break;
  4926. default:
  4927. MISSING_CASE(cdclk);
  4928. return;
  4929. }
  4930. /*
  4931. * Specs are full of misinformation, but testing on actual
  4932. * hardware has shown that we just need to write the desired
  4933. * CCK divider into the Punit register.
  4934. */
  4935. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4936. mutex_lock(&dev_priv->rps.hw_lock);
  4937. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4938. val &= ~DSPFREQGUAR_MASK_CHV;
  4939. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4940. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4941. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4942. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4943. 50)) {
  4944. DRM_ERROR("timed out waiting for CDclk change\n");
  4945. }
  4946. mutex_unlock(&dev_priv->rps.hw_lock);
  4947. intel_update_cdclk(dev);
  4948. }
  4949. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4950. int max_pixclk)
  4951. {
  4952. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  4953. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  4954. /*
  4955. * Really only a few cases to deal with, as only 4 CDclks are supported:
  4956. * 200MHz
  4957. * 267MHz
  4958. * 320/333MHz (depends on HPLL freq)
  4959. * 400MHz (VLV only)
  4960. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  4961. * of the lower bin and adjust if needed.
  4962. *
  4963. * We seem to get an unstable or solid color picture at 200MHz.
  4964. * Not sure what's wrong. For now use 200MHz only when all pipes
  4965. * are off.
  4966. */
  4967. if (!IS_CHERRYVIEW(dev_priv) &&
  4968. max_pixclk > freq_320*limit/100)
  4969. return 400000;
  4970. else if (max_pixclk > 266667*limit/100)
  4971. return freq_320;
  4972. else if (max_pixclk > 0)
  4973. return 266667;
  4974. else
  4975. return 200000;
  4976. }
  4977. static int broxton_calc_cdclk(int max_pixclk)
  4978. {
  4979. /*
  4980. * FIXME:
  4981. * - set 19.2MHz bypass frequency if there are no active pipes
  4982. */
  4983. if (max_pixclk > 576000)
  4984. return 624000;
  4985. else if (max_pixclk > 384000)
  4986. return 576000;
  4987. else if (max_pixclk > 288000)
  4988. return 384000;
  4989. else if (max_pixclk > 144000)
  4990. return 288000;
  4991. else
  4992. return 144000;
  4993. }
  4994. /* Compute the max pixel clock for new configuration. */
  4995. static int intel_mode_max_pixclk(struct drm_device *dev,
  4996. struct drm_atomic_state *state)
  4997. {
  4998. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  4999. struct drm_i915_private *dev_priv = dev->dev_private;
  5000. struct drm_crtc *crtc;
  5001. struct drm_crtc_state *crtc_state;
  5002. unsigned max_pixclk = 0, i;
  5003. enum pipe pipe;
  5004. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5005. sizeof(intel_state->min_pixclk));
  5006. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5007. int pixclk = 0;
  5008. if (crtc_state->enable)
  5009. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5010. intel_state->min_pixclk[i] = pixclk;
  5011. }
  5012. for_each_pipe(dev_priv, pipe)
  5013. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5014. return max_pixclk;
  5015. }
  5016. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5017. {
  5018. struct drm_device *dev = state->dev;
  5019. struct drm_i915_private *dev_priv = dev->dev_private;
  5020. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5021. struct intel_atomic_state *intel_state =
  5022. to_intel_atomic_state(state);
  5023. intel_state->cdclk = intel_state->dev_cdclk =
  5024. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5025. if (!intel_state->active_crtcs)
  5026. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5027. return 0;
  5028. }
  5029. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5030. {
  5031. int max_pixclk = ilk_max_pixel_rate(state);
  5032. struct intel_atomic_state *intel_state =
  5033. to_intel_atomic_state(state);
  5034. intel_state->cdclk = intel_state->dev_cdclk =
  5035. broxton_calc_cdclk(max_pixclk);
  5036. if (!intel_state->active_crtcs)
  5037. intel_state->dev_cdclk = broxton_calc_cdclk(0);
  5038. return 0;
  5039. }
  5040. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5041. {
  5042. unsigned int credits, default_credits;
  5043. if (IS_CHERRYVIEW(dev_priv))
  5044. default_credits = PFI_CREDIT(12);
  5045. else
  5046. default_credits = PFI_CREDIT(8);
  5047. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5048. /* CHV suggested value is 31 or 63 */
  5049. if (IS_CHERRYVIEW(dev_priv))
  5050. credits = PFI_CREDIT_63;
  5051. else
  5052. credits = PFI_CREDIT(15);
  5053. } else {
  5054. credits = default_credits;
  5055. }
  5056. /*
  5057. * WA - write default credits before re-programming
  5058. * FIXME: should we also set the resend bit here?
  5059. */
  5060. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5061. default_credits);
  5062. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5063. credits | PFI_CREDIT_RESEND);
  5064. /*
  5065. * FIXME is this guaranteed to clear
  5066. * immediately or should we poll for it?
  5067. */
  5068. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5069. }
  5070. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5071. {
  5072. struct drm_device *dev = old_state->dev;
  5073. struct drm_i915_private *dev_priv = dev->dev_private;
  5074. struct intel_atomic_state *old_intel_state =
  5075. to_intel_atomic_state(old_state);
  5076. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5077. /*
  5078. * FIXME: We can end up here with all power domains off, yet
  5079. * with a CDCLK frequency other than the minimum. To account
  5080. * for this take the PIPE-A power domain, which covers the HW
  5081. * blocks needed for the following programming. This can be
  5082. * removed once it's guaranteed that we get here either with
  5083. * the minimum CDCLK set, or the required power domains
  5084. * enabled.
  5085. */
  5086. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5087. if (IS_CHERRYVIEW(dev))
  5088. cherryview_set_cdclk(dev, req_cdclk);
  5089. else
  5090. valleyview_set_cdclk(dev, req_cdclk);
  5091. vlv_program_pfi_credits(dev_priv);
  5092. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5093. }
  5094. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5095. {
  5096. struct drm_device *dev = crtc->dev;
  5097. struct drm_i915_private *dev_priv = to_i915(dev);
  5098. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5099. struct intel_encoder *encoder;
  5100. struct intel_crtc_state *pipe_config =
  5101. to_intel_crtc_state(crtc->state);
  5102. int pipe = intel_crtc->pipe;
  5103. if (WARN_ON(intel_crtc->active))
  5104. return;
  5105. if (intel_crtc->config->has_dp_encoder)
  5106. intel_dp_set_m_n(intel_crtc, M1_N1);
  5107. intel_set_pipe_timings(intel_crtc);
  5108. intel_set_pipe_src_size(intel_crtc);
  5109. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5110. struct drm_i915_private *dev_priv = dev->dev_private;
  5111. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5112. I915_WRITE(CHV_CANVAS(pipe), 0);
  5113. }
  5114. i9xx_set_pipeconf(intel_crtc);
  5115. intel_crtc->active = true;
  5116. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5117. for_each_encoder_on_crtc(dev, crtc, encoder)
  5118. if (encoder->pre_pll_enable)
  5119. encoder->pre_pll_enable(encoder);
  5120. if (IS_CHERRYVIEW(dev)) {
  5121. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5122. chv_enable_pll(intel_crtc, intel_crtc->config);
  5123. } else {
  5124. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5125. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5126. }
  5127. for_each_encoder_on_crtc(dev, crtc, encoder)
  5128. if (encoder->pre_enable)
  5129. encoder->pre_enable(encoder);
  5130. i9xx_pfit_enable(intel_crtc);
  5131. intel_color_load_luts(&pipe_config->base);
  5132. intel_update_watermarks(crtc);
  5133. intel_enable_pipe(intel_crtc);
  5134. assert_vblank_disabled(crtc);
  5135. drm_crtc_vblank_on(crtc);
  5136. for_each_encoder_on_crtc(dev, crtc, encoder)
  5137. encoder->enable(encoder);
  5138. }
  5139. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5140. {
  5141. struct drm_device *dev = crtc->base.dev;
  5142. struct drm_i915_private *dev_priv = dev->dev_private;
  5143. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5144. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5145. }
  5146. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5147. {
  5148. struct drm_device *dev = crtc->dev;
  5149. struct drm_i915_private *dev_priv = to_i915(dev);
  5150. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5151. struct intel_encoder *encoder;
  5152. struct intel_crtc_state *pipe_config =
  5153. to_intel_crtc_state(crtc->state);
  5154. enum pipe pipe = intel_crtc->pipe;
  5155. if (WARN_ON(intel_crtc->active))
  5156. return;
  5157. i9xx_set_pll_dividers(intel_crtc);
  5158. if (intel_crtc->config->has_dp_encoder)
  5159. intel_dp_set_m_n(intel_crtc, M1_N1);
  5160. intel_set_pipe_timings(intel_crtc);
  5161. intel_set_pipe_src_size(intel_crtc);
  5162. i9xx_set_pipeconf(intel_crtc);
  5163. intel_crtc->active = true;
  5164. if (!IS_GEN2(dev))
  5165. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5166. for_each_encoder_on_crtc(dev, crtc, encoder)
  5167. if (encoder->pre_enable)
  5168. encoder->pre_enable(encoder);
  5169. i9xx_enable_pll(intel_crtc);
  5170. i9xx_pfit_enable(intel_crtc);
  5171. intel_color_load_luts(&pipe_config->base);
  5172. intel_update_watermarks(crtc);
  5173. intel_enable_pipe(intel_crtc);
  5174. assert_vblank_disabled(crtc);
  5175. drm_crtc_vblank_on(crtc);
  5176. for_each_encoder_on_crtc(dev, crtc, encoder)
  5177. encoder->enable(encoder);
  5178. }
  5179. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5180. {
  5181. struct drm_device *dev = crtc->base.dev;
  5182. struct drm_i915_private *dev_priv = dev->dev_private;
  5183. if (!crtc->config->gmch_pfit.control)
  5184. return;
  5185. assert_pipe_disabled(dev_priv, crtc->pipe);
  5186. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5187. I915_READ(PFIT_CONTROL));
  5188. I915_WRITE(PFIT_CONTROL, 0);
  5189. }
  5190. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5191. {
  5192. struct drm_device *dev = crtc->dev;
  5193. struct drm_i915_private *dev_priv = dev->dev_private;
  5194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5195. struct intel_encoder *encoder;
  5196. int pipe = intel_crtc->pipe;
  5197. /*
  5198. * On gen2 planes are double buffered but the pipe isn't, so we must
  5199. * wait for planes to fully turn off before disabling the pipe.
  5200. */
  5201. if (IS_GEN2(dev))
  5202. intel_wait_for_vblank(dev, pipe);
  5203. for_each_encoder_on_crtc(dev, crtc, encoder)
  5204. encoder->disable(encoder);
  5205. drm_crtc_vblank_off(crtc);
  5206. assert_vblank_disabled(crtc);
  5207. intel_disable_pipe(intel_crtc);
  5208. i9xx_pfit_disable(intel_crtc);
  5209. for_each_encoder_on_crtc(dev, crtc, encoder)
  5210. if (encoder->post_disable)
  5211. encoder->post_disable(encoder);
  5212. if (!intel_crtc->config->has_dsi_encoder) {
  5213. if (IS_CHERRYVIEW(dev))
  5214. chv_disable_pll(dev_priv, pipe);
  5215. else if (IS_VALLEYVIEW(dev))
  5216. vlv_disable_pll(dev_priv, pipe);
  5217. else
  5218. i9xx_disable_pll(intel_crtc);
  5219. }
  5220. for_each_encoder_on_crtc(dev, crtc, encoder)
  5221. if (encoder->post_pll_disable)
  5222. encoder->post_pll_disable(encoder);
  5223. if (!IS_GEN2(dev))
  5224. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5225. }
  5226. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5227. {
  5228. struct intel_encoder *encoder;
  5229. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5230. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5231. enum intel_display_power_domain domain;
  5232. unsigned long domains;
  5233. if (!intel_crtc->active)
  5234. return;
  5235. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5236. WARN_ON(list_empty(&intel_crtc->flip_work));
  5237. intel_pre_disable_primary_noatomic(crtc);
  5238. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5239. to_intel_plane_state(crtc->primary->state)->visible = false;
  5240. }
  5241. dev_priv->display.crtc_disable(crtc);
  5242. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was enabled, now disabled\n",
  5243. crtc->base.id);
  5244. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5245. crtc->state->active = false;
  5246. intel_crtc->active = false;
  5247. crtc->enabled = false;
  5248. crtc->state->connector_mask = 0;
  5249. crtc->state->encoder_mask = 0;
  5250. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5251. encoder->base.crtc = NULL;
  5252. intel_fbc_disable(intel_crtc);
  5253. intel_update_watermarks(crtc);
  5254. intel_disable_shared_dpll(intel_crtc);
  5255. domains = intel_crtc->enabled_power_domains;
  5256. for_each_power_domain(domain, domains)
  5257. intel_display_power_put(dev_priv, domain);
  5258. intel_crtc->enabled_power_domains = 0;
  5259. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5260. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5261. }
  5262. /*
  5263. * turn all crtc's off, but do not adjust state
  5264. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5265. */
  5266. int intel_display_suspend(struct drm_device *dev)
  5267. {
  5268. struct drm_i915_private *dev_priv = to_i915(dev);
  5269. struct drm_atomic_state *state;
  5270. int ret;
  5271. state = drm_atomic_helper_suspend(dev);
  5272. ret = PTR_ERR_OR_ZERO(state);
  5273. if (ret)
  5274. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5275. else
  5276. dev_priv->modeset_restore_state = state;
  5277. /*
  5278. * Make sure all unpin_work completes before returning.
  5279. */
  5280. flush_workqueue(dev_priv->wq);
  5281. return ret;
  5282. }
  5283. void intel_encoder_destroy(struct drm_encoder *encoder)
  5284. {
  5285. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5286. drm_encoder_cleanup(encoder);
  5287. kfree(intel_encoder);
  5288. }
  5289. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5290. * internal consistency). */
  5291. static void intel_connector_verify_state(struct intel_connector *connector,
  5292. struct drm_connector_state *conn_state)
  5293. {
  5294. struct drm_crtc *crtc = conn_state->crtc;
  5295. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5296. connector->base.base.id,
  5297. connector->base.name);
  5298. if (connector->get_hw_state(connector)) {
  5299. struct intel_encoder *encoder = connector->encoder;
  5300. I915_STATE_WARN(!crtc,
  5301. "connector enabled without attached crtc\n");
  5302. if (!crtc)
  5303. return;
  5304. I915_STATE_WARN(!crtc->state->active,
  5305. "connector is active, but attached crtc isn't\n");
  5306. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5307. return;
  5308. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5309. "atomic encoder doesn't match attached encoder\n");
  5310. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5311. "attached encoder crtc differs from connector crtc\n");
  5312. } else {
  5313. I915_STATE_WARN(crtc && crtc->state->active,
  5314. "attached crtc is active, but connector isn't\n");
  5315. I915_STATE_WARN(!crtc && conn_state->best_encoder,
  5316. "best encoder set without crtc!\n");
  5317. }
  5318. }
  5319. int intel_connector_init(struct intel_connector *connector)
  5320. {
  5321. drm_atomic_helper_connector_reset(&connector->base);
  5322. if (!connector->base.state)
  5323. return -ENOMEM;
  5324. return 0;
  5325. }
  5326. struct intel_connector *intel_connector_alloc(void)
  5327. {
  5328. struct intel_connector *connector;
  5329. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5330. if (!connector)
  5331. return NULL;
  5332. if (intel_connector_init(connector) < 0) {
  5333. kfree(connector);
  5334. return NULL;
  5335. }
  5336. return connector;
  5337. }
  5338. /* Simple connector->get_hw_state implementation for encoders that support only
  5339. * one connector and no cloning and hence the encoder state determines the state
  5340. * of the connector. */
  5341. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5342. {
  5343. enum pipe pipe = 0;
  5344. struct intel_encoder *encoder = connector->encoder;
  5345. return encoder->get_hw_state(encoder, &pipe);
  5346. }
  5347. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5348. {
  5349. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5350. return crtc_state->fdi_lanes;
  5351. return 0;
  5352. }
  5353. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5354. struct intel_crtc_state *pipe_config)
  5355. {
  5356. struct drm_atomic_state *state = pipe_config->base.state;
  5357. struct intel_crtc *other_crtc;
  5358. struct intel_crtc_state *other_crtc_state;
  5359. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5360. pipe_name(pipe), pipe_config->fdi_lanes);
  5361. if (pipe_config->fdi_lanes > 4) {
  5362. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5363. pipe_name(pipe), pipe_config->fdi_lanes);
  5364. return -EINVAL;
  5365. }
  5366. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5367. if (pipe_config->fdi_lanes > 2) {
  5368. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5369. pipe_config->fdi_lanes);
  5370. return -EINVAL;
  5371. } else {
  5372. return 0;
  5373. }
  5374. }
  5375. if (INTEL_INFO(dev)->num_pipes == 2)
  5376. return 0;
  5377. /* Ivybridge 3 pipe is really complicated */
  5378. switch (pipe) {
  5379. case PIPE_A:
  5380. return 0;
  5381. case PIPE_B:
  5382. if (pipe_config->fdi_lanes <= 2)
  5383. return 0;
  5384. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5385. other_crtc_state =
  5386. intel_atomic_get_crtc_state(state, other_crtc);
  5387. if (IS_ERR(other_crtc_state))
  5388. return PTR_ERR(other_crtc_state);
  5389. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5390. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5391. pipe_name(pipe), pipe_config->fdi_lanes);
  5392. return -EINVAL;
  5393. }
  5394. return 0;
  5395. case PIPE_C:
  5396. if (pipe_config->fdi_lanes > 2) {
  5397. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5398. pipe_name(pipe), pipe_config->fdi_lanes);
  5399. return -EINVAL;
  5400. }
  5401. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5402. other_crtc_state =
  5403. intel_atomic_get_crtc_state(state, other_crtc);
  5404. if (IS_ERR(other_crtc_state))
  5405. return PTR_ERR(other_crtc_state);
  5406. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5407. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5408. return -EINVAL;
  5409. }
  5410. return 0;
  5411. default:
  5412. BUG();
  5413. }
  5414. }
  5415. #define RETRY 1
  5416. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5417. struct intel_crtc_state *pipe_config)
  5418. {
  5419. struct drm_device *dev = intel_crtc->base.dev;
  5420. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5421. int lane, link_bw, fdi_dotclock, ret;
  5422. bool needs_recompute = false;
  5423. retry:
  5424. /* FDI is a binary signal running at ~2.7GHz, encoding
  5425. * each output octet as 10 bits. The actual frequency
  5426. * is stored as a divider into a 100MHz clock, and the
  5427. * mode pixel clock is stored in units of 1KHz.
  5428. * Hence the bw of each lane in terms of the mode signal
  5429. * is:
  5430. */
  5431. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5432. fdi_dotclock = adjusted_mode->crtc_clock;
  5433. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5434. pipe_config->pipe_bpp);
  5435. pipe_config->fdi_lanes = lane;
  5436. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5437. link_bw, &pipe_config->fdi_m_n);
  5438. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5439. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5440. pipe_config->pipe_bpp -= 2*3;
  5441. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5442. pipe_config->pipe_bpp);
  5443. needs_recompute = true;
  5444. pipe_config->bw_constrained = true;
  5445. goto retry;
  5446. }
  5447. if (needs_recompute)
  5448. return RETRY;
  5449. return ret;
  5450. }
  5451. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5452. struct intel_crtc_state *pipe_config)
  5453. {
  5454. if (pipe_config->pipe_bpp > 24)
  5455. return false;
  5456. /* HSW can handle pixel rate up to cdclk? */
  5457. if (IS_HASWELL(dev_priv))
  5458. return true;
  5459. /*
  5460. * We compare against max which means we must take
  5461. * the increased cdclk requirement into account when
  5462. * calculating the new cdclk.
  5463. *
  5464. * Should measure whether using a lower cdclk w/o IPS
  5465. */
  5466. return ilk_pipe_pixel_rate(pipe_config) <=
  5467. dev_priv->max_cdclk_freq * 95 / 100;
  5468. }
  5469. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5470. struct intel_crtc_state *pipe_config)
  5471. {
  5472. struct drm_device *dev = crtc->base.dev;
  5473. struct drm_i915_private *dev_priv = dev->dev_private;
  5474. pipe_config->ips_enabled = i915.enable_ips &&
  5475. hsw_crtc_supports_ips(crtc) &&
  5476. pipe_config_supports_ips(dev_priv, pipe_config);
  5477. }
  5478. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5479. {
  5480. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5481. /* GDG double wide on either pipe, otherwise pipe A only */
  5482. return INTEL_INFO(dev_priv)->gen < 4 &&
  5483. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5484. }
  5485. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5486. struct intel_crtc_state *pipe_config)
  5487. {
  5488. struct drm_device *dev = crtc->base.dev;
  5489. struct drm_i915_private *dev_priv = dev->dev_private;
  5490. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5491. /* FIXME should check pixel clock limits on all platforms */
  5492. if (INTEL_INFO(dev)->gen < 4) {
  5493. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5494. /*
  5495. * Enable double wide mode when the dot clock
  5496. * is > 90% of the (display) core speed.
  5497. */
  5498. if (intel_crtc_supports_double_wide(crtc) &&
  5499. adjusted_mode->crtc_clock > clock_limit) {
  5500. clock_limit *= 2;
  5501. pipe_config->double_wide = true;
  5502. }
  5503. if (adjusted_mode->crtc_clock > clock_limit) {
  5504. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5505. adjusted_mode->crtc_clock, clock_limit,
  5506. yesno(pipe_config->double_wide));
  5507. return -EINVAL;
  5508. }
  5509. }
  5510. /*
  5511. * Pipe horizontal size must be even in:
  5512. * - DVO ganged mode
  5513. * - LVDS dual channel mode
  5514. * - Double wide pipe
  5515. */
  5516. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5517. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5518. pipe_config->pipe_src_w &= ~1;
  5519. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5520. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5521. */
  5522. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5523. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5524. return -EINVAL;
  5525. if (HAS_IPS(dev))
  5526. hsw_compute_ips_config(crtc, pipe_config);
  5527. if (pipe_config->has_pch_encoder)
  5528. return ironlake_fdi_compute_config(crtc, pipe_config);
  5529. return 0;
  5530. }
  5531. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5532. {
  5533. struct drm_i915_private *dev_priv = to_i915(dev);
  5534. uint32_t cdctl;
  5535. skl_dpll0_update(dev_priv);
  5536. if (dev_priv->cdclk_pll.vco == 0)
  5537. return dev_priv->cdclk_pll.ref;
  5538. cdctl = I915_READ(CDCLK_CTL);
  5539. if (dev_priv->cdclk_pll.vco == 8640000) {
  5540. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5541. case CDCLK_FREQ_450_432:
  5542. return 432000;
  5543. case CDCLK_FREQ_337_308:
  5544. return 308571;
  5545. case CDCLK_FREQ_540:
  5546. return 540000;
  5547. case CDCLK_FREQ_675_617:
  5548. return 617143;
  5549. default:
  5550. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5551. }
  5552. } else {
  5553. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5554. case CDCLK_FREQ_450_432:
  5555. return 450000;
  5556. case CDCLK_FREQ_337_308:
  5557. return 337500;
  5558. case CDCLK_FREQ_540:
  5559. return 540000;
  5560. case CDCLK_FREQ_675_617:
  5561. return 675000;
  5562. default:
  5563. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  5564. }
  5565. }
  5566. return dev_priv->cdclk_pll.ref;
  5567. }
  5568. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  5569. {
  5570. u32 val;
  5571. dev_priv->cdclk_pll.ref = 19200;
  5572. val = I915_READ(BXT_DE_PLL_ENABLE);
  5573. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0) {
  5574. dev_priv->cdclk_pll.vco = 0;
  5575. return;
  5576. }
  5577. WARN_ON((val & BXT_DE_PLL_LOCK) == 0);
  5578. val = I915_READ(BXT_DE_PLL_CTL);
  5579. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  5580. dev_priv->cdclk_pll.ref;
  5581. }
  5582. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5583. {
  5584. struct drm_i915_private *dev_priv = to_i915(dev);
  5585. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5586. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5587. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5588. int cdclk;
  5589. bxt_de_pll_update(dev_priv);
  5590. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5591. return 19200;
  5592. cdclk = 19200 * pll_ratio / 2;
  5593. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5594. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5595. return cdclk; /* 576MHz or 624MHz */
  5596. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5597. return cdclk * 2 / 3; /* 384MHz */
  5598. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5599. return cdclk / 2; /* 288MHz */
  5600. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5601. return cdclk / 4; /* 144MHz */
  5602. }
  5603. /* error case, do as if DE PLL isn't enabled */
  5604. return 19200;
  5605. }
  5606. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5607. {
  5608. struct drm_i915_private *dev_priv = dev->dev_private;
  5609. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5610. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5611. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5612. return 800000;
  5613. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5614. return 450000;
  5615. else if (freq == LCPLL_CLK_FREQ_450)
  5616. return 450000;
  5617. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5618. return 540000;
  5619. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5620. return 337500;
  5621. else
  5622. return 675000;
  5623. }
  5624. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5625. {
  5626. struct drm_i915_private *dev_priv = dev->dev_private;
  5627. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5628. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5629. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5630. return 800000;
  5631. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5632. return 450000;
  5633. else if (freq == LCPLL_CLK_FREQ_450)
  5634. return 450000;
  5635. else if (IS_HSW_ULT(dev))
  5636. return 337500;
  5637. else
  5638. return 540000;
  5639. }
  5640. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5641. {
  5642. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5643. CCK_DISPLAY_CLOCK_CONTROL);
  5644. }
  5645. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5646. {
  5647. return 450000;
  5648. }
  5649. static int i945_get_display_clock_speed(struct drm_device *dev)
  5650. {
  5651. return 400000;
  5652. }
  5653. static int i915_get_display_clock_speed(struct drm_device *dev)
  5654. {
  5655. return 333333;
  5656. }
  5657. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5658. {
  5659. return 200000;
  5660. }
  5661. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5662. {
  5663. u16 gcfgc = 0;
  5664. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5665. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5666. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5667. return 266667;
  5668. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5669. return 333333;
  5670. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5671. return 444444;
  5672. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5673. return 200000;
  5674. default:
  5675. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5676. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5677. return 133333;
  5678. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5679. return 166667;
  5680. }
  5681. }
  5682. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5683. {
  5684. u16 gcfgc = 0;
  5685. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5686. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5687. return 133333;
  5688. else {
  5689. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5690. case GC_DISPLAY_CLOCK_333_MHZ:
  5691. return 333333;
  5692. default:
  5693. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5694. return 190000;
  5695. }
  5696. }
  5697. }
  5698. static int i865_get_display_clock_speed(struct drm_device *dev)
  5699. {
  5700. return 266667;
  5701. }
  5702. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5703. {
  5704. u16 hpllcc = 0;
  5705. /*
  5706. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5707. * encoding is different :(
  5708. * FIXME is this the right way to detect 852GM/852GMV?
  5709. */
  5710. if (dev->pdev->revision == 0x1)
  5711. return 133333;
  5712. pci_bus_read_config_word(dev->pdev->bus,
  5713. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5714. /* Assume that the hardware is in the high speed state. This
  5715. * should be the default.
  5716. */
  5717. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5718. case GC_CLOCK_133_200:
  5719. case GC_CLOCK_133_200_2:
  5720. case GC_CLOCK_100_200:
  5721. return 200000;
  5722. case GC_CLOCK_166_250:
  5723. return 250000;
  5724. case GC_CLOCK_100_133:
  5725. return 133333;
  5726. case GC_CLOCK_133_266:
  5727. case GC_CLOCK_133_266_2:
  5728. case GC_CLOCK_166_266:
  5729. return 266667;
  5730. }
  5731. /* Shouldn't happen */
  5732. return 0;
  5733. }
  5734. static int i830_get_display_clock_speed(struct drm_device *dev)
  5735. {
  5736. return 133333;
  5737. }
  5738. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5739. {
  5740. struct drm_i915_private *dev_priv = dev->dev_private;
  5741. static const unsigned int blb_vco[8] = {
  5742. [0] = 3200000,
  5743. [1] = 4000000,
  5744. [2] = 5333333,
  5745. [3] = 4800000,
  5746. [4] = 6400000,
  5747. };
  5748. static const unsigned int pnv_vco[8] = {
  5749. [0] = 3200000,
  5750. [1] = 4000000,
  5751. [2] = 5333333,
  5752. [3] = 4800000,
  5753. [4] = 2666667,
  5754. };
  5755. static const unsigned int cl_vco[8] = {
  5756. [0] = 3200000,
  5757. [1] = 4000000,
  5758. [2] = 5333333,
  5759. [3] = 6400000,
  5760. [4] = 3333333,
  5761. [5] = 3566667,
  5762. [6] = 4266667,
  5763. };
  5764. static const unsigned int elk_vco[8] = {
  5765. [0] = 3200000,
  5766. [1] = 4000000,
  5767. [2] = 5333333,
  5768. [3] = 4800000,
  5769. };
  5770. static const unsigned int ctg_vco[8] = {
  5771. [0] = 3200000,
  5772. [1] = 4000000,
  5773. [2] = 5333333,
  5774. [3] = 6400000,
  5775. [4] = 2666667,
  5776. [5] = 4266667,
  5777. };
  5778. const unsigned int *vco_table;
  5779. unsigned int vco;
  5780. uint8_t tmp = 0;
  5781. /* FIXME other chipsets? */
  5782. if (IS_GM45(dev))
  5783. vco_table = ctg_vco;
  5784. else if (IS_G4X(dev))
  5785. vco_table = elk_vco;
  5786. else if (IS_CRESTLINE(dev))
  5787. vco_table = cl_vco;
  5788. else if (IS_PINEVIEW(dev))
  5789. vco_table = pnv_vco;
  5790. else if (IS_G33(dev))
  5791. vco_table = blb_vco;
  5792. else
  5793. return 0;
  5794. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5795. vco = vco_table[tmp & 0x7];
  5796. if (vco == 0)
  5797. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5798. else
  5799. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5800. return vco;
  5801. }
  5802. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5803. {
  5804. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5805. uint16_t tmp = 0;
  5806. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5807. cdclk_sel = (tmp >> 12) & 0x1;
  5808. switch (vco) {
  5809. case 2666667:
  5810. case 4000000:
  5811. case 5333333:
  5812. return cdclk_sel ? 333333 : 222222;
  5813. case 3200000:
  5814. return cdclk_sel ? 320000 : 228571;
  5815. default:
  5816. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5817. return 222222;
  5818. }
  5819. }
  5820. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5821. {
  5822. static const uint8_t div_3200[] = { 16, 10, 8 };
  5823. static const uint8_t div_4000[] = { 20, 12, 10 };
  5824. static const uint8_t div_5333[] = { 24, 16, 14 };
  5825. const uint8_t *div_table;
  5826. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5827. uint16_t tmp = 0;
  5828. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5829. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5830. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5831. goto fail;
  5832. switch (vco) {
  5833. case 3200000:
  5834. div_table = div_3200;
  5835. break;
  5836. case 4000000:
  5837. div_table = div_4000;
  5838. break;
  5839. case 5333333:
  5840. div_table = div_5333;
  5841. break;
  5842. default:
  5843. goto fail;
  5844. }
  5845. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5846. fail:
  5847. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5848. return 200000;
  5849. }
  5850. static int g33_get_display_clock_speed(struct drm_device *dev)
  5851. {
  5852. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5853. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5854. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5855. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5856. const uint8_t *div_table;
  5857. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5858. uint16_t tmp = 0;
  5859. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5860. cdclk_sel = (tmp >> 4) & 0x7;
  5861. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5862. goto fail;
  5863. switch (vco) {
  5864. case 3200000:
  5865. div_table = div_3200;
  5866. break;
  5867. case 4000000:
  5868. div_table = div_4000;
  5869. break;
  5870. case 4800000:
  5871. div_table = div_4800;
  5872. break;
  5873. case 5333333:
  5874. div_table = div_5333;
  5875. break;
  5876. default:
  5877. goto fail;
  5878. }
  5879. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5880. fail:
  5881. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5882. return 190476;
  5883. }
  5884. static void
  5885. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5886. {
  5887. while (*num > DATA_LINK_M_N_MASK ||
  5888. *den > DATA_LINK_M_N_MASK) {
  5889. *num >>= 1;
  5890. *den >>= 1;
  5891. }
  5892. }
  5893. static void compute_m_n(unsigned int m, unsigned int n,
  5894. uint32_t *ret_m, uint32_t *ret_n)
  5895. {
  5896. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5897. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5898. intel_reduce_m_n_ratio(ret_m, ret_n);
  5899. }
  5900. void
  5901. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5902. int pixel_clock, int link_clock,
  5903. struct intel_link_m_n *m_n)
  5904. {
  5905. m_n->tu = 64;
  5906. compute_m_n(bits_per_pixel * pixel_clock,
  5907. link_clock * nlanes * 8,
  5908. &m_n->gmch_m, &m_n->gmch_n);
  5909. compute_m_n(pixel_clock, link_clock,
  5910. &m_n->link_m, &m_n->link_n);
  5911. }
  5912. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5913. {
  5914. if (i915.panel_use_ssc >= 0)
  5915. return i915.panel_use_ssc != 0;
  5916. return dev_priv->vbt.lvds_use_ssc
  5917. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5918. }
  5919. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5920. {
  5921. return (1 << dpll->n) << 16 | dpll->m2;
  5922. }
  5923. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5924. {
  5925. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5926. }
  5927. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5928. struct intel_crtc_state *crtc_state,
  5929. struct dpll *reduced_clock)
  5930. {
  5931. struct drm_device *dev = crtc->base.dev;
  5932. u32 fp, fp2 = 0;
  5933. if (IS_PINEVIEW(dev)) {
  5934. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5935. if (reduced_clock)
  5936. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5937. } else {
  5938. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5939. if (reduced_clock)
  5940. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5941. }
  5942. crtc_state->dpll_hw_state.fp0 = fp;
  5943. crtc->lowfreq_avail = false;
  5944. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5945. reduced_clock) {
  5946. crtc_state->dpll_hw_state.fp1 = fp2;
  5947. crtc->lowfreq_avail = true;
  5948. } else {
  5949. crtc_state->dpll_hw_state.fp1 = fp;
  5950. }
  5951. }
  5952. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5953. pipe)
  5954. {
  5955. u32 reg_val;
  5956. /*
  5957. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5958. * and set it to a reasonable value instead.
  5959. */
  5960. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5961. reg_val &= 0xffffff00;
  5962. reg_val |= 0x00000030;
  5963. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5964. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5965. reg_val &= 0x8cffffff;
  5966. reg_val = 0x8c000000;
  5967. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5968. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5969. reg_val &= 0xffffff00;
  5970. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5971. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5972. reg_val &= 0x00ffffff;
  5973. reg_val |= 0xb0000000;
  5974. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5975. }
  5976. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5977. struct intel_link_m_n *m_n)
  5978. {
  5979. struct drm_device *dev = crtc->base.dev;
  5980. struct drm_i915_private *dev_priv = dev->dev_private;
  5981. int pipe = crtc->pipe;
  5982. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5983. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5984. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5985. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5986. }
  5987. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5988. struct intel_link_m_n *m_n,
  5989. struct intel_link_m_n *m2_n2)
  5990. {
  5991. struct drm_device *dev = crtc->base.dev;
  5992. struct drm_i915_private *dev_priv = dev->dev_private;
  5993. int pipe = crtc->pipe;
  5994. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5995. if (INTEL_INFO(dev)->gen >= 5) {
  5996. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5997. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5998. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5999. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6000. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6001. * for gen < 8) and if DRRS is supported (to make sure the
  6002. * registers are not unnecessarily accessed).
  6003. */
  6004. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6005. crtc->config->has_drrs) {
  6006. I915_WRITE(PIPE_DATA_M2(transcoder),
  6007. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6008. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6009. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6010. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6011. }
  6012. } else {
  6013. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6014. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6015. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6016. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6017. }
  6018. }
  6019. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6020. {
  6021. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6022. if (m_n == M1_N1) {
  6023. dp_m_n = &crtc->config->dp_m_n;
  6024. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6025. } else if (m_n == M2_N2) {
  6026. /*
  6027. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6028. * needs to be programmed into M1_N1.
  6029. */
  6030. dp_m_n = &crtc->config->dp_m2_n2;
  6031. } else {
  6032. DRM_ERROR("Unsupported divider value\n");
  6033. return;
  6034. }
  6035. if (crtc->config->has_pch_encoder)
  6036. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6037. else
  6038. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6039. }
  6040. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6041. struct intel_crtc_state *pipe_config)
  6042. {
  6043. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6044. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6045. if (crtc->pipe != PIPE_A)
  6046. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6047. /* DPLL not used with DSI, but still need the rest set up */
  6048. if (!pipe_config->has_dsi_encoder)
  6049. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6050. DPLL_EXT_BUFFER_ENABLE_VLV;
  6051. pipe_config->dpll_hw_state.dpll_md =
  6052. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6053. }
  6054. static void chv_compute_dpll(struct intel_crtc *crtc,
  6055. struct intel_crtc_state *pipe_config)
  6056. {
  6057. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6058. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6059. if (crtc->pipe != PIPE_A)
  6060. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6061. /* DPLL not used with DSI, but still need the rest set up */
  6062. if (!pipe_config->has_dsi_encoder)
  6063. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6064. pipe_config->dpll_hw_state.dpll_md =
  6065. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6066. }
  6067. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6068. const struct intel_crtc_state *pipe_config)
  6069. {
  6070. struct drm_device *dev = crtc->base.dev;
  6071. struct drm_i915_private *dev_priv = dev->dev_private;
  6072. enum pipe pipe = crtc->pipe;
  6073. u32 mdiv;
  6074. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6075. u32 coreclk, reg_val;
  6076. /* Enable Refclk */
  6077. I915_WRITE(DPLL(pipe),
  6078. pipe_config->dpll_hw_state.dpll &
  6079. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6080. /* No need to actually set up the DPLL with DSI */
  6081. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6082. return;
  6083. mutex_lock(&dev_priv->sb_lock);
  6084. bestn = pipe_config->dpll.n;
  6085. bestm1 = pipe_config->dpll.m1;
  6086. bestm2 = pipe_config->dpll.m2;
  6087. bestp1 = pipe_config->dpll.p1;
  6088. bestp2 = pipe_config->dpll.p2;
  6089. /* See eDP HDMI DPIO driver vbios notes doc */
  6090. /* PLL B needs special handling */
  6091. if (pipe == PIPE_B)
  6092. vlv_pllb_recal_opamp(dev_priv, pipe);
  6093. /* Set up Tx target for periodic Rcomp update */
  6094. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6095. /* Disable target IRef on PLL */
  6096. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6097. reg_val &= 0x00ffffff;
  6098. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6099. /* Disable fast lock */
  6100. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6101. /* Set idtafcrecal before PLL is enabled */
  6102. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6103. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6104. mdiv |= ((bestn << DPIO_N_SHIFT));
  6105. mdiv |= (1 << DPIO_K_SHIFT);
  6106. /*
  6107. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6108. * but we don't support that).
  6109. * Note: don't use the DAC post divider as it seems unstable.
  6110. */
  6111. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6112. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6113. mdiv |= DPIO_ENABLE_CALIBRATION;
  6114. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6115. /* Set HBR and RBR LPF coefficients */
  6116. if (pipe_config->port_clock == 162000 ||
  6117. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6118. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6119. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6120. 0x009f0003);
  6121. else
  6122. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6123. 0x00d0000f);
  6124. if (pipe_config->has_dp_encoder) {
  6125. /* Use SSC source */
  6126. if (pipe == PIPE_A)
  6127. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6128. 0x0df40000);
  6129. else
  6130. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6131. 0x0df70000);
  6132. } else { /* HDMI or VGA */
  6133. /* Use bend source */
  6134. if (pipe == PIPE_A)
  6135. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6136. 0x0df70000);
  6137. else
  6138. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6139. 0x0df40000);
  6140. }
  6141. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6142. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6143. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6144. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6145. coreclk |= 0x01000000;
  6146. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6147. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6148. mutex_unlock(&dev_priv->sb_lock);
  6149. }
  6150. static void chv_prepare_pll(struct intel_crtc *crtc,
  6151. const struct intel_crtc_state *pipe_config)
  6152. {
  6153. struct drm_device *dev = crtc->base.dev;
  6154. struct drm_i915_private *dev_priv = dev->dev_private;
  6155. enum pipe pipe = crtc->pipe;
  6156. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6157. u32 loopfilter, tribuf_calcntr;
  6158. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6159. u32 dpio_val;
  6160. int vco;
  6161. /* Enable Refclk and SSC */
  6162. I915_WRITE(DPLL(pipe),
  6163. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6164. /* No need to actually set up the DPLL with DSI */
  6165. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6166. return;
  6167. bestn = pipe_config->dpll.n;
  6168. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6169. bestm1 = pipe_config->dpll.m1;
  6170. bestm2 = pipe_config->dpll.m2 >> 22;
  6171. bestp1 = pipe_config->dpll.p1;
  6172. bestp2 = pipe_config->dpll.p2;
  6173. vco = pipe_config->dpll.vco;
  6174. dpio_val = 0;
  6175. loopfilter = 0;
  6176. mutex_lock(&dev_priv->sb_lock);
  6177. /* p1 and p2 divider */
  6178. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6179. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6180. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6181. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6182. 1 << DPIO_CHV_K_DIV_SHIFT);
  6183. /* Feedback post-divider - m2 */
  6184. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6185. /* Feedback refclk divider - n and m1 */
  6186. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6187. DPIO_CHV_M1_DIV_BY_2 |
  6188. 1 << DPIO_CHV_N_DIV_SHIFT);
  6189. /* M2 fraction division */
  6190. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6191. /* M2 fraction division enable */
  6192. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6193. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6194. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6195. if (bestm2_frac)
  6196. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6197. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6198. /* Program digital lock detect threshold */
  6199. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6200. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6201. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6202. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6203. if (!bestm2_frac)
  6204. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6205. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6206. /* Loop filter */
  6207. if (vco == 5400000) {
  6208. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6209. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6210. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6211. tribuf_calcntr = 0x9;
  6212. } else if (vco <= 6200000) {
  6213. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6214. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6215. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6216. tribuf_calcntr = 0x9;
  6217. } else if (vco <= 6480000) {
  6218. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6219. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6220. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6221. tribuf_calcntr = 0x8;
  6222. } else {
  6223. /* Not supported. Apply the same limits as in the max case */
  6224. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6225. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6226. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6227. tribuf_calcntr = 0;
  6228. }
  6229. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6230. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6231. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6232. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6233. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6234. /* AFC Recal */
  6235. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6236. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6237. DPIO_AFC_RECAL);
  6238. mutex_unlock(&dev_priv->sb_lock);
  6239. }
  6240. /**
  6241. * vlv_force_pll_on - forcibly enable just the PLL
  6242. * @dev_priv: i915 private structure
  6243. * @pipe: pipe PLL to enable
  6244. * @dpll: PLL configuration
  6245. *
  6246. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6247. * in cases where we need the PLL enabled even when @pipe is not going to
  6248. * be enabled.
  6249. */
  6250. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6251. const struct dpll *dpll)
  6252. {
  6253. struct intel_crtc *crtc =
  6254. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6255. struct intel_crtc_state *pipe_config;
  6256. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6257. if (!pipe_config)
  6258. return -ENOMEM;
  6259. pipe_config->base.crtc = &crtc->base;
  6260. pipe_config->pixel_multiplier = 1;
  6261. pipe_config->dpll = *dpll;
  6262. if (IS_CHERRYVIEW(dev)) {
  6263. chv_compute_dpll(crtc, pipe_config);
  6264. chv_prepare_pll(crtc, pipe_config);
  6265. chv_enable_pll(crtc, pipe_config);
  6266. } else {
  6267. vlv_compute_dpll(crtc, pipe_config);
  6268. vlv_prepare_pll(crtc, pipe_config);
  6269. vlv_enable_pll(crtc, pipe_config);
  6270. }
  6271. kfree(pipe_config);
  6272. return 0;
  6273. }
  6274. /**
  6275. * vlv_force_pll_off - forcibly disable just the PLL
  6276. * @dev_priv: i915 private structure
  6277. * @pipe: pipe PLL to disable
  6278. *
  6279. * Disable the PLL for @pipe. To be used in cases where we need
  6280. * the PLL enabled even when @pipe is not going to be enabled.
  6281. */
  6282. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6283. {
  6284. if (IS_CHERRYVIEW(dev))
  6285. chv_disable_pll(to_i915(dev), pipe);
  6286. else
  6287. vlv_disable_pll(to_i915(dev), pipe);
  6288. }
  6289. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6290. struct intel_crtc_state *crtc_state,
  6291. struct dpll *reduced_clock)
  6292. {
  6293. struct drm_device *dev = crtc->base.dev;
  6294. struct drm_i915_private *dev_priv = dev->dev_private;
  6295. u32 dpll;
  6296. bool is_sdvo;
  6297. struct dpll *clock = &crtc_state->dpll;
  6298. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6299. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6300. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6301. dpll = DPLL_VGA_MODE_DIS;
  6302. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6303. dpll |= DPLLB_MODE_LVDS;
  6304. else
  6305. dpll |= DPLLB_MODE_DAC_SERIAL;
  6306. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6307. dpll |= (crtc_state->pixel_multiplier - 1)
  6308. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6309. }
  6310. if (is_sdvo)
  6311. dpll |= DPLL_SDVO_HIGH_SPEED;
  6312. if (crtc_state->has_dp_encoder)
  6313. dpll |= DPLL_SDVO_HIGH_SPEED;
  6314. /* compute bitmask from p1 value */
  6315. if (IS_PINEVIEW(dev))
  6316. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6317. else {
  6318. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6319. if (IS_G4X(dev) && reduced_clock)
  6320. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6321. }
  6322. switch (clock->p2) {
  6323. case 5:
  6324. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6325. break;
  6326. case 7:
  6327. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6328. break;
  6329. case 10:
  6330. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6331. break;
  6332. case 14:
  6333. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6334. break;
  6335. }
  6336. if (INTEL_INFO(dev)->gen >= 4)
  6337. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6338. if (crtc_state->sdvo_tv_clock)
  6339. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6340. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6341. intel_panel_use_ssc(dev_priv))
  6342. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6343. else
  6344. dpll |= PLL_REF_INPUT_DREFCLK;
  6345. dpll |= DPLL_VCO_ENABLE;
  6346. crtc_state->dpll_hw_state.dpll = dpll;
  6347. if (INTEL_INFO(dev)->gen >= 4) {
  6348. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6349. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6350. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6351. }
  6352. }
  6353. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6354. struct intel_crtc_state *crtc_state,
  6355. struct dpll *reduced_clock)
  6356. {
  6357. struct drm_device *dev = crtc->base.dev;
  6358. struct drm_i915_private *dev_priv = dev->dev_private;
  6359. u32 dpll;
  6360. struct dpll *clock = &crtc_state->dpll;
  6361. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6362. dpll = DPLL_VGA_MODE_DIS;
  6363. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6364. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6365. } else {
  6366. if (clock->p1 == 2)
  6367. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6368. else
  6369. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6370. if (clock->p2 == 4)
  6371. dpll |= PLL_P2_DIVIDE_BY_4;
  6372. }
  6373. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6374. dpll |= DPLL_DVO_2X_MODE;
  6375. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6376. intel_panel_use_ssc(dev_priv))
  6377. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6378. else
  6379. dpll |= PLL_REF_INPUT_DREFCLK;
  6380. dpll |= DPLL_VCO_ENABLE;
  6381. crtc_state->dpll_hw_state.dpll = dpll;
  6382. }
  6383. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6384. {
  6385. struct drm_device *dev = intel_crtc->base.dev;
  6386. struct drm_i915_private *dev_priv = dev->dev_private;
  6387. enum pipe pipe = intel_crtc->pipe;
  6388. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6389. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6390. uint32_t crtc_vtotal, crtc_vblank_end;
  6391. int vsyncshift = 0;
  6392. /* We need to be careful not to changed the adjusted mode, for otherwise
  6393. * the hw state checker will get angry at the mismatch. */
  6394. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6395. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6396. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6397. /* the chip adds 2 halflines automatically */
  6398. crtc_vtotal -= 1;
  6399. crtc_vblank_end -= 1;
  6400. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6401. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6402. else
  6403. vsyncshift = adjusted_mode->crtc_hsync_start -
  6404. adjusted_mode->crtc_htotal / 2;
  6405. if (vsyncshift < 0)
  6406. vsyncshift += adjusted_mode->crtc_htotal;
  6407. }
  6408. if (INTEL_INFO(dev)->gen > 3)
  6409. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6410. I915_WRITE(HTOTAL(cpu_transcoder),
  6411. (adjusted_mode->crtc_hdisplay - 1) |
  6412. ((adjusted_mode->crtc_htotal - 1) << 16));
  6413. I915_WRITE(HBLANK(cpu_transcoder),
  6414. (adjusted_mode->crtc_hblank_start - 1) |
  6415. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6416. I915_WRITE(HSYNC(cpu_transcoder),
  6417. (adjusted_mode->crtc_hsync_start - 1) |
  6418. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6419. I915_WRITE(VTOTAL(cpu_transcoder),
  6420. (adjusted_mode->crtc_vdisplay - 1) |
  6421. ((crtc_vtotal - 1) << 16));
  6422. I915_WRITE(VBLANK(cpu_transcoder),
  6423. (adjusted_mode->crtc_vblank_start - 1) |
  6424. ((crtc_vblank_end - 1) << 16));
  6425. I915_WRITE(VSYNC(cpu_transcoder),
  6426. (adjusted_mode->crtc_vsync_start - 1) |
  6427. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6428. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6429. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6430. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6431. * bits. */
  6432. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6433. (pipe == PIPE_B || pipe == PIPE_C))
  6434. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6435. }
  6436. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6437. {
  6438. struct drm_device *dev = intel_crtc->base.dev;
  6439. struct drm_i915_private *dev_priv = dev->dev_private;
  6440. enum pipe pipe = intel_crtc->pipe;
  6441. /* pipesrc controls the size that is scaled from, which should
  6442. * always be the user's requested size.
  6443. */
  6444. I915_WRITE(PIPESRC(pipe),
  6445. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6446. (intel_crtc->config->pipe_src_h - 1));
  6447. }
  6448. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6449. struct intel_crtc_state *pipe_config)
  6450. {
  6451. struct drm_device *dev = crtc->base.dev;
  6452. struct drm_i915_private *dev_priv = dev->dev_private;
  6453. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6454. uint32_t tmp;
  6455. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6456. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6457. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6458. tmp = I915_READ(HBLANK(cpu_transcoder));
  6459. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6460. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6461. tmp = I915_READ(HSYNC(cpu_transcoder));
  6462. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6463. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6464. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6465. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6466. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6467. tmp = I915_READ(VBLANK(cpu_transcoder));
  6468. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6469. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6470. tmp = I915_READ(VSYNC(cpu_transcoder));
  6471. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6472. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6473. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6474. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6475. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6476. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6477. }
  6478. }
  6479. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  6480. struct intel_crtc_state *pipe_config)
  6481. {
  6482. struct drm_device *dev = crtc->base.dev;
  6483. struct drm_i915_private *dev_priv = dev->dev_private;
  6484. u32 tmp;
  6485. tmp = I915_READ(PIPESRC(crtc->pipe));
  6486. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6487. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6488. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6489. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6490. }
  6491. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6492. struct intel_crtc_state *pipe_config)
  6493. {
  6494. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6495. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6496. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6497. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6498. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6499. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6500. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6501. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6502. mode->flags = pipe_config->base.adjusted_mode.flags;
  6503. mode->type = DRM_MODE_TYPE_DRIVER;
  6504. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6505. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6506. mode->hsync = drm_mode_hsync(mode);
  6507. mode->vrefresh = drm_mode_vrefresh(mode);
  6508. drm_mode_set_name(mode);
  6509. }
  6510. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6511. {
  6512. struct drm_device *dev = intel_crtc->base.dev;
  6513. struct drm_i915_private *dev_priv = dev->dev_private;
  6514. uint32_t pipeconf;
  6515. pipeconf = 0;
  6516. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6517. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6518. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6519. if (intel_crtc->config->double_wide)
  6520. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6521. /* only g4x and later have fancy bpc/dither controls */
  6522. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6523. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6524. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6525. pipeconf |= PIPECONF_DITHER_EN |
  6526. PIPECONF_DITHER_TYPE_SP;
  6527. switch (intel_crtc->config->pipe_bpp) {
  6528. case 18:
  6529. pipeconf |= PIPECONF_6BPC;
  6530. break;
  6531. case 24:
  6532. pipeconf |= PIPECONF_8BPC;
  6533. break;
  6534. case 30:
  6535. pipeconf |= PIPECONF_10BPC;
  6536. break;
  6537. default:
  6538. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6539. BUG();
  6540. }
  6541. }
  6542. if (HAS_PIPE_CXSR(dev)) {
  6543. if (intel_crtc->lowfreq_avail) {
  6544. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6545. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6546. } else {
  6547. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6548. }
  6549. }
  6550. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6551. if (INTEL_INFO(dev)->gen < 4 ||
  6552. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6553. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6554. else
  6555. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6556. } else
  6557. pipeconf |= PIPECONF_PROGRESSIVE;
  6558. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6559. intel_crtc->config->limited_color_range)
  6560. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6561. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6562. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6563. }
  6564. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  6565. struct intel_crtc_state *crtc_state)
  6566. {
  6567. struct drm_device *dev = crtc->base.dev;
  6568. struct drm_i915_private *dev_priv = dev->dev_private;
  6569. const struct intel_limit *limit;
  6570. int refclk = 48000;
  6571. memset(&crtc_state->dpll_hw_state, 0,
  6572. sizeof(crtc_state->dpll_hw_state));
  6573. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6574. if (intel_panel_use_ssc(dev_priv)) {
  6575. refclk = dev_priv->vbt.lvds_ssc_freq;
  6576. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6577. }
  6578. limit = &intel_limits_i8xx_lvds;
  6579. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO)) {
  6580. limit = &intel_limits_i8xx_dvo;
  6581. } else {
  6582. limit = &intel_limits_i8xx_dac;
  6583. }
  6584. if (!crtc_state->clock_set &&
  6585. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6586. refclk, NULL, &crtc_state->dpll)) {
  6587. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6588. return -EINVAL;
  6589. }
  6590. i8xx_compute_dpll(crtc, crtc_state, NULL);
  6591. return 0;
  6592. }
  6593. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  6594. struct intel_crtc_state *crtc_state)
  6595. {
  6596. struct drm_device *dev = crtc->base.dev;
  6597. struct drm_i915_private *dev_priv = dev->dev_private;
  6598. const struct intel_limit *limit;
  6599. int refclk = 96000;
  6600. memset(&crtc_state->dpll_hw_state, 0,
  6601. sizeof(crtc_state->dpll_hw_state));
  6602. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6603. if (intel_panel_use_ssc(dev_priv)) {
  6604. refclk = dev_priv->vbt.lvds_ssc_freq;
  6605. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6606. }
  6607. if (intel_is_dual_link_lvds(dev))
  6608. limit = &intel_limits_g4x_dual_channel_lvds;
  6609. else
  6610. limit = &intel_limits_g4x_single_channel_lvds;
  6611. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6612. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6613. limit = &intel_limits_g4x_hdmi;
  6614. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6615. limit = &intel_limits_g4x_sdvo;
  6616. } else {
  6617. /* The option is for other outputs */
  6618. limit = &intel_limits_i9xx_sdvo;
  6619. }
  6620. if (!crtc_state->clock_set &&
  6621. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6622. refclk, NULL, &crtc_state->dpll)) {
  6623. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6624. return -EINVAL;
  6625. }
  6626. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6627. return 0;
  6628. }
  6629. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6630. struct intel_crtc_state *crtc_state)
  6631. {
  6632. struct drm_device *dev = crtc->base.dev;
  6633. struct drm_i915_private *dev_priv = dev->dev_private;
  6634. const struct intel_limit *limit;
  6635. int refclk = 96000;
  6636. memset(&crtc_state->dpll_hw_state, 0,
  6637. sizeof(crtc_state->dpll_hw_state));
  6638. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6639. if (intel_panel_use_ssc(dev_priv)) {
  6640. refclk = dev_priv->vbt.lvds_ssc_freq;
  6641. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6642. }
  6643. limit = &intel_limits_pineview_lvds;
  6644. } else {
  6645. limit = &intel_limits_pineview_sdvo;
  6646. }
  6647. if (!crtc_state->clock_set &&
  6648. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6649. refclk, NULL, &crtc_state->dpll)) {
  6650. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6651. return -EINVAL;
  6652. }
  6653. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6654. return 0;
  6655. }
  6656. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6657. struct intel_crtc_state *crtc_state)
  6658. {
  6659. struct drm_device *dev = crtc->base.dev;
  6660. struct drm_i915_private *dev_priv = dev->dev_private;
  6661. const struct intel_limit *limit;
  6662. int refclk = 96000;
  6663. memset(&crtc_state->dpll_hw_state, 0,
  6664. sizeof(crtc_state->dpll_hw_state));
  6665. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6666. if (intel_panel_use_ssc(dev_priv)) {
  6667. refclk = dev_priv->vbt.lvds_ssc_freq;
  6668. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6669. }
  6670. limit = &intel_limits_i9xx_lvds;
  6671. } else {
  6672. limit = &intel_limits_i9xx_sdvo;
  6673. }
  6674. if (!crtc_state->clock_set &&
  6675. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6676. refclk, NULL, &crtc_state->dpll)) {
  6677. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6678. return -EINVAL;
  6679. }
  6680. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6681. return 0;
  6682. }
  6683. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6684. struct intel_crtc_state *crtc_state)
  6685. {
  6686. int refclk = 100000;
  6687. const struct intel_limit *limit = &intel_limits_chv;
  6688. memset(&crtc_state->dpll_hw_state, 0,
  6689. sizeof(crtc_state->dpll_hw_state));
  6690. if (!crtc_state->clock_set &&
  6691. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6692. refclk, NULL, &crtc_state->dpll)) {
  6693. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6694. return -EINVAL;
  6695. }
  6696. chv_compute_dpll(crtc, crtc_state);
  6697. return 0;
  6698. }
  6699. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6700. struct intel_crtc_state *crtc_state)
  6701. {
  6702. int refclk = 100000;
  6703. const struct intel_limit *limit = &intel_limits_vlv;
  6704. memset(&crtc_state->dpll_hw_state, 0,
  6705. sizeof(crtc_state->dpll_hw_state));
  6706. if (!crtc_state->clock_set &&
  6707. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6708. refclk, NULL, &crtc_state->dpll)) {
  6709. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6710. return -EINVAL;
  6711. }
  6712. vlv_compute_dpll(crtc, crtc_state);
  6713. return 0;
  6714. }
  6715. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6716. struct intel_crtc_state *pipe_config)
  6717. {
  6718. struct drm_device *dev = crtc->base.dev;
  6719. struct drm_i915_private *dev_priv = dev->dev_private;
  6720. uint32_t tmp;
  6721. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6722. return;
  6723. tmp = I915_READ(PFIT_CONTROL);
  6724. if (!(tmp & PFIT_ENABLE))
  6725. return;
  6726. /* Check whether the pfit is attached to our pipe. */
  6727. if (INTEL_INFO(dev)->gen < 4) {
  6728. if (crtc->pipe != PIPE_B)
  6729. return;
  6730. } else {
  6731. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6732. return;
  6733. }
  6734. pipe_config->gmch_pfit.control = tmp;
  6735. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6736. }
  6737. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6738. struct intel_crtc_state *pipe_config)
  6739. {
  6740. struct drm_device *dev = crtc->base.dev;
  6741. struct drm_i915_private *dev_priv = dev->dev_private;
  6742. int pipe = pipe_config->cpu_transcoder;
  6743. struct dpll clock;
  6744. u32 mdiv;
  6745. int refclk = 100000;
  6746. /* In case of DSI, DPLL will not be used */
  6747. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6748. return;
  6749. mutex_lock(&dev_priv->sb_lock);
  6750. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6751. mutex_unlock(&dev_priv->sb_lock);
  6752. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6753. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6754. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6755. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6756. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6757. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6758. }
  6759. static void
  6760. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6761. struct intel_initial_plane_config *plane_config)
  6762. {
  6763. struct drm_device *dev = crtc->base.dev;
  6764. struct drm_i915_private *dev_priv = dev->dev_private;
  6765. u32 val, base, offset;
  6766. int pipe = crtc->pipe, plane = crtc->plane;
  6767. int fourcc, pixel_format;
  6768. unsigned int aligned_height;
  6769. struct drm_framebuffer *fb;
  6770. struct intel_framebuffer *intel_fb;
  6771. val = I915_READ(DSPCNTR(plane));
  6772. if (!(val & DISPLAY_PLANE_ENABLE))
  6773. return;
  6774. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6775. if (!intel_fb) {
  6776. DRM_DEBUG_KMS("failed to alloc fb\n");
  6777. return;
  6778. }
  6779. fb = &intel_fb->base;
  6780. if (INTEL_INFO(dev)->gen >= 4) {
  6781. if (val & DISPPLANE_TILED) {
  6782. plane_config->tiling = I915_TILING_X;
  6783. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6784. }
  6785. }
  6786. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6787. fourcc = i9xx_format_to_fourcc(pixel_format);
  6788. fb->pixel_format = fourcc;
  6789. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6790. if (INTEL_INFO(dev)->gen >= 4) {
  6791. if (plane_config->tiling)
  6792. offset = I915_READ(DSPTILEOFF(plane));
  6793. else
  6794. offset = I915_READ(DSPLINOFF(plane));
  6795. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6796. } else {
  6797. base = I915_READ(DSPADDR(plane));
  6798. }
  6799. plane_config->base = base;
  6800. val = I915_READ(PIPESRC(pipe));
  6801. fb->width = ((val >> 16) & 0xfff) + 1;
  6802. fb->height = ((val >> 0) & 0xfff) + 1;
  6803. val = I915_READ(DSPSTRIDE(pipe));
  6804. fb->pitches[0] = val & 0xffffffc0;
  6805. aligned_height = intel_fb_align_height(dev, fb->height,
  6806. fb->pixel_format,
  6807. fb->modifier[0]);
  6808. plane_config->size = fb->pitches[0] * aligned_height;
  6809. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6810. pipe_name(pipe), plane, fb->width, fb->height,
  6811. fb->bits_per_pixel, base, fb->pitches[0],
  6812. plane_config->size);
  6813. plane_config->fb = intel_fb;
  6814. }
  6815. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6816. struct intel_crtc_state *pipe_config)
  6817. {
  6818. struct drm_device *dev = crtc->base.dev;
  6819. struct drm_i915_private *dev_priv = dev->dev_private;
  6820. int pipe = pipe_config->cpu_transcoder;
  6821. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6822. struct dpll clock;
  6823. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6824. int refclk = 100000;
  6825. /* In case of DSI, DPLL will not be used */
  6826. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6827. return;
  6828. mutex_lock(&dev_priv->sb_lock);
  6829. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6830. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6831. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6832. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6833. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6834. mutex_unlock(&dev_priv->sb_lock);
  6835. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6836. clock.m2 = (pll_dw0 & 0xff) << 22;
  6837. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6838. clock.m2 |= pll_dw2 & 0x3fffff;
  6839. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6840. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6841. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6842. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6843. }
  6844. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6845. struct intel_crtc_state *pipe_config)
  6846. {
  6847. struct drm_device *dev = crtc->base.dev;
  6848. struct drm_i915_private *dev_priv = dev->dev_private;
  6849. enum intel_display_power_domain power_domain;
  6850. uint32_t tmp;
  6851. bool ret;
  6852. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6853. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6854. return false;
  6855. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6856. pipe_config->shared_dpll = NULL;
  6857. ret = false;
  6858. tmp = I915_READ(PIPECONF(crtc->pipe));
  6859. if (!(tmp & PIPECONF_ENABLE))
  6860. goto out;
  6861. if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  6862. switch (tmp & PIPECONF_BPC_MASK) {
  6863. case PIPECONF_6BPC:
  6864. pipe_config->pipe_bpp = 18;
  6865. break;
  6866. case PIPECONF_8BPC:
  6867. pipe_config->pipe_bpp = 24;
  6868. break;
  6869. case PIPECONF_10BPC:
  6870. pipe_config->pipe_bpp = 30;
  6871. break;
  6872. default:
  6873. break;
  6874. }
  6875. }
  6876. if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
  6877. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6878. pipe_config->limited_color_range = true;
  6879. if (INTEL_INFO(dev)->gen < 4)
  6880. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6881. intel_get_pipe_timings(crtc, pipe_config);
  6882. intel_get_pipe_src_size(crtc, pipe_config);
  6883. i9xx_get_pfit_config(crtc, pipe_config);
  6884. if (INTEL_INFO(dev)->gen >= 4) {
  6885. /* No way to read it out on pipes B and C */
  6886. if (IS_CHERRYVIEW(dev) && crtc->pipe != PIPE_A)
  6887. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6888. else
  6889. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6890. pipe_config->pixel_multiplier =
  6891. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6892. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6893. pipe_config->dpll_hw_state.dpll_md = tmp;
  6894. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6895. tmp = I915_READ(DPLL(crtc->pipe));
  6896. pipe_config->pixel_multiplier =
  6897. ((tmp & SDVO_MULTIPLIER_MASK)
  6898. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6899. } else {
  6900. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6901. * port and will be fixed up in the encoder->get_config
  6902. * function. */
  6903. pipe_config->pixel_multiplier = 1;
  6904. }
  6905. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6906. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  6907. /*
  6908. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6909. * on 830. Filter it out here so that we don't
  6910. * report errors due to that.
  6911. */
  6912. if (IS_I830(dev))
  6913. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6914. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6915. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6916. } else {
  6917. /* Mask out read-only status bits. */
  6918. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6919. DPLL_PORTC_READY_MASK |
  6920. DPLL_PORTB_READY_MASK);
  6921. }
  6922. if (IS_CHERRYVIEW(dev))
  6923. chv_crtc_clock_get(crtc, pipe_config);
  6924. else if (IS_VALLEYVIEW(dev))
  6925. vlv_crtc_clock_get(crtc, pipe_config);
  6926. else
  6927. i9xx_crtc_clock_get(crtc, pipe_config);
  6928. /*
  6929. * Normally the dotclock is filled in by the encoder .get_config()
  6930. * but in case the pipe is enabled w/o any ports we need a sane
  6931. * default.
  6932. */
  6933. pipe_config->base.adjusted_mode.crtc_clock =
  6934. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6935. ret = true;
  6936. out:
  6937. intel_display_power_put(dev_priv, power_domain);
  6938. return ret;
  6939. }
  6940. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6941. {
  6942. struct drm_i915_private *dev_priv = dev->dev_private;
  6943. struct intel_encoder *encoder;
  6944. u32 val, final;
  6945. bool has_lvds = false;
  6946. bool has_cpu_edp = false;
  6947. bool has_panel = false;
  6948. bool has_ck505 = false;
  6949. bool can_ssc = false;
  6950. /* We need to take the global config into account */
  6951. for_each_intel_encoder(dev, encoder) {
  6952. switch (encoder->type) {
  6953. case INTEL_OUTPUT_LVDS:
  6954. has_panel = true;
  6955. has_lvds = true;
  6956. break;
  6957. case INTEL_OUTPUT_EDP:
  6958. has_panel = true;
  6959. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6960. has_cpu_edp = true;
  6961. break;
  6962. default:
  6963. break;
  6964. }
  6965. }
  6966. if (HAS_PCH_IBX(dev)) {
  6967. has_ck505 = dev_priv->vbt.display_clock_mode;
  6968. can_ssc = has_ck505;
  6969. } else {
  6970. has_ck505 = false;
  6971. can_ssc = true;
  6972. }
  6973. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6974. has_panel, has_lvds, has_ck505);
  6975. /* Ironlake: try to setup display ref clock before DPLL
  6976. * enabling. This is only under driver's control after
  6977. * PCH B stepping, previous chipset stepping should be
  6978. * ignoring this setting.
  6979. */
  6980. val = I915_READ(PCH_DREF_CONTROL);
  6981. /* As we must carefully and slowly disable/enable each source in turn,
  6982. * compute the final state we want first and check if we need to
  6983. * make any changes at all.
  6984. */
  6985. final = val;
  6986. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6987. if (has_ck505)
  6988. final |= DREF_NONSPREAD_CK505_ENABLE;
  6989. else
  6990. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6991. final &= ~DREF_SSC_SOURCE_MASK;
  6992. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6993. final &= ~DREF_SSC1_ENABLE;
  6994. if (has_panel) {
  6995. final |= DREF_SSC_SOURCE_ENABLE;
  6996. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6997. final |= DREF_SSC1_ENABLE;
  6998. if (has_cpu_edp) {
  6999. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7000. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7001. else
  7002. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7003. } else
  7004. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7005. } else {
  7006. final |= DREF_SSC_SOURCE_DISABLE;
  7007. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7008. }
  7009. if (final == val)
  7010. return;
  7011. /* Always enable nonspread source */
  7012. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7013. if (has_ck505)
  7014. val |= DREF_NONSPREAD_CK505_ENABLE;
  7015. else
  7016. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7017. if (has_panel) {
  7018. val &= ~DREF_SSC_SOURCE_MASK;
  7019. val |= DREF_SSC_SOURCE_ENABLE;
  7020. /* SSC must be turned on before enabling the CPU output */
  7021. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7022. DRM_DEBUG_KMS("Using SSC on panel\n");
  7023. val |= DREF_SSC1_ENABLE;
  7024. } else
  7025. val &= ~DREF_SSC1_ENABLE;
  7026. /* Get SSC going before enabling the outputs */
  7027. I915_WRITE(PCH_DREF_CONTROL, val);
  7028. POSTING_READ(PCH_DREF_CONTROL);
  7029. udelay(200);
  7030. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7031. /* Enable CPU source on CPU attached eDP */
  7032. if (has_cpu_edp) {
  7033. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7034. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7035. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7036. } else
  7037. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7038. } else
  7039. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7040. I915_WRITE(PCH_DREF_CONTROL, val);
  7041. POSTING_READ(PCH_DREF_CONTROL);
  7042. udelay(200);
  7043. } else {
  7044. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  7045. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7046. /* Turn off CPU output */
  7047. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7048. I915_WRITE(PCH_DREF_CONTROL, val);
  7049. POSTING_READ(PCH_DREF_CONTROL);
  7050. udelay(200);
  7051. /* Turn off the SSC source */
  7052. val &= ~DREF_SSC_SOURCE_MASK;
  7053. val |= DREF_SSC_SOURCE_DISABLE;
  7054. /* Turn off SSC1 */
  7055. val &= ~DREF_SSC1_ENABLE;
  7056. I915_WRITE(PCH_DREF_CONTROL, val);
  7057. POSTING_READ(PCH_DREF_CONTROL);
  7058. udelay(200);
  7059. }
  7060. BUG_ON(val != final);
  7061. }
  7062. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7063. {
  7064. uint32_t tmp;
  7065. tmp = I915_READ(SOUTH_CHICKEN2);
  7066. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7067. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7068. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7069. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7070. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7071. tmp = I915_READ(SOUTH_CHICKEN2);
  7072. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7073. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7074. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7075. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7076. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7077. }
  7078. /* WaMPhyProgramming:hsw */
  7079. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7080. {
  7081. uint32_t tmp;
  7082. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7083. tmp &= ~(0xFF << 24);
  7084. tmp |= (0x12 << 24);
  7085. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7086. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7087. tmp |= (1 << 11);
  7088. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7089. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7090. tmp |= (1 << 11);
  7091. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7092. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7093. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7094. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7095. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7096. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7097. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7098. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7099. tmp &= ~(7 << 13);
  7100. tmp |= (5 << 13);
  7101. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7102. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7103. tmp &= ~(7 << 13);
  7104. tmp |= (5 << 13);
  7105. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7106. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7107. tmp &= ~0xFF;
  7108. tmp |= 0x1C;
  7109. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7110. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7111. tmp &= ~0xFF;
  7112. tmp |= 0x1C;
  7113. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7114. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7115. tmp &= ~(0xFF << 16);
  7116. tmp |= (0x1C << 16);
  7117. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7118. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7119. tmp &= ~(0xFF << 16);
  7120. tmp |= (0x1C << 16);
  7121. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7122. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7123. tmp |= (1 << 27);
  7124. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7125. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7126. tmp |= (1 << 27);
  7127. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7128. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7129. tmp &= ~(0xF << 28);
  7130. tmp |= (4 << 28);
  7131. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7132. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7133. tmp &= ~(0xF << 28);
  7134. tmp |= (4 << 28);
  7135. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7136. }
  7137. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7138. * Programming" based on the parameters passed:
  7139. * - Sequence to enable CLKOUT_DP
  7140. * - Sequence to enable CLKOUT_DP without spread
  7141. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7142. */
  7143. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7144. bool with_fdi)
  7145. {
  7146. struct drm_i915_private *dev_priv = dev->dev_private;
  7147. uint32_t reg, tmp;
  7148. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7149. with_spread = true;
  7150. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7151. with_fdi = false;
  7152. mutex_lock(&dev_priv->sb_lock);
  7153. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7154. tmp &= ~SBI_SSCCTL_DISABLE;
  7155. tmp |= SBI_SSCCTL_PATHALT;
  7156. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7157. udelay(24);
  7158. if (with_spread) {
  7159. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7160. tmp &= ~SBI_SSCCTL_PATHALT;
  7161. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7162. if (with_fdi) {
  7163. lpt_reset_fdi_mphy(dev_priv);
  7164. lpt_program_fdi_mphy(dev_priv);
  7165. }
  7166. }
  7167. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7168. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7169. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7170. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7171. mutex_unlock(&dev_priv->sb_lock);
  7172. }
  7173. /* Sequence to disable CLKOUT_DP */
  7174. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7175. {
  7176. struct drm_i915_private *dev_priv = dev->dev_private;
  7177. uint32_t reg, tmp;
  7178. mutex_lock(&dev_priv->sb_lock);
  7179. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7180. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7181. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7182. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7183. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7184. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7185. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7186. tmp |= SBI_SSCCTL_PATHALT;
  7187. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7188. udelay(32);
  7189. }
  7190. tmp |= SBI_SSCCTL_DISABLE;
  7191. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7192. }
  7193. mutex_unlock(&dev_priv->sb_lock);
  7194. }
  7195. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7196. static const uint16_t sscdivintphase[] = {
  7197. [BEND_IDX( 50)] = 0x3B23,
  7198. [BEND_IDX( 45)] = 0x3B23,
  7199. [BEND_IDX( 40)] = 0x3C23,
  7200. [BEND_IDX( 35)] = 0x3C23,
  7201. [BEND_IDX( 30)] = 0x3D23,
  7202. [BEND_IDX( 25)] = 0x3D23,
  7203. [BEND_IDX( 20)] = 0x3E23,
  7204. [BEND_IDX( 15)] = 0x3E23,
  7205. [BEND_IDX( 10)] = 0x3F23,
  7206. [BEND_IDX( 5)] = 0x3F23,
  7207. [BEND_IDX( 0)] = 0x0025,
  7208. [BEND_IDX( -5)] = 0x0025,
  7209. [BEND_IDX(-10)] = 0x0125,
  7210. [BEND_IDX(-15)] = 0x0125,
  7211. [BEND_IDX(-20)] = 0x0225,
  7212. [BEND_IDX(-25)] = 0x0225,
  7213. [BEND_IDX(-30)] = 0x0325,
  7214. [BEND_IDX(-35)] = 0x0325,
  7215. [BEND_IDX(-40)] = 0x0425,
  7216. [BEND_IDX(-45)] = 0x0425,
  7217. [BEND_IDX(-50)] = 0x0525,
  7218. };
  7219. /*
  7220. * Bend CLKOUT_DP
  7221. * steps -50 to 50 inclusive, in steps of 5
  7222. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7223. * change in clock period = -(steps / 10) * 5.787 ps
  7224. */
  7225. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7226. {
  7227. uint32_t tmp;
  7228. int idx = BEND_IDX(steps);
  7229. if (WARN_ON(steps % 5 != 0))
  7230. return;
  7231. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7232. return;
  7233. mutex_lock(&dev_priv->sb_lock);
  7234. if (steps % 10 != 0)
  7235. tmp = 0xAAAAAAAB;
  7236. else
  7237. tmp = 0x00000000;
  7238. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7239. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7240. tmp &= 0xffff0000;
  7241. tmp |= sscdivintphase[idx];
  7242. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7243. mutex_unlock(&dev_priv->sb_lock);
  7244. }
  7245. #undef BEND_IDX
  7246. static void lpt_init_pch_refclk(struct drm_device *dev)
  7247. {
  7248. struct intel_encoder *encoder;
  7249. bool has_vga = false;
  7250. for_each_intel_encoder(dev, encoder) {
  7251. switch (encoder->type) {
  7252. case INTEL_OUTPUT_ANALOG:
  7253. has_vga = true;
  7254. break;
  7255. default:
  7256. break;
  7257. }
  7258. }
  7259. if (has_vga) {
  7260. lpt_bend_clkout_dp(to_i915(dev), 0);
  7261. lpt_enable_clkout_dp(dev, true, true);
  7262. } else {
  7263. lpt_disable_clkout_dp(dev);
  7264. }
  7265. }
  7266. /*
  7267. * Initialize reference clocks when the driver loads
  7268. */
  7269. void intel_init_pch_refclk(struct drm_device *dev)
  7270. {
  7271. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7272. ironlake_init_pch_refclk(dev);
  7273. else if (HAS_PCH_LPT(dev))
  7274. lpt_init_pch_refclk(dev);
  7275. }
  7276. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7277. {
  7278. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7280. int pipe = intel_crtc->pipe;
  7281. uint32_t val;
  7282. val = 0;
  7283. switch (intel_crtc->config->pipe_bpp) {
  7284. case 18:
  7285. val |= PIPECONF_6BPC;
  7286. break;
  7287. case 24:
  7288. val |= PIPECONF_8BPC;
  7289. break;
  7290. case 30:
  7291. val |= PIPECONF_10BPC;
  7292. break;
  7293. case 36:
  7294. val |= PIPECONF_12BPC;
  7295. break;
  7296. default:
  7297. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7298. BUG();
  7299. }
  7300. if (intel_crtc->config->dither)
  7301. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7302. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7303. val |= PIPECONF_INTERLACED_ILK;
  7304. else
  7305. val |= PIPECONF_PROGRESSIVE;
  7306. if (intel_crtc->config->limited_color_range)
  7307. val |= PIPECONF_COLOR_RANGE_SELECT;
  7308. I915_WRITE(PIPECONF(pipe), val);
  7309. POSTING_READ(PIPECONF(pipe));
  7310. }
  7311. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7312. {
  7313. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7315. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7316. u32 val = 0;
  7317. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7318. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7319. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7320. val |= PIPECONF_INTERLACED_ILK;
  7321. else
  7322. val |= PIPECONF_PROGRESSIVE;
  7323. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7324. POSTING_READ(PIPECONF(cpu_transcoder));
  7325. }
  7326. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7327. {
  7328. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7330. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7331. u32 val = 0;
  7332. switch (intel_crtc->config->pipe_bpp) {
  7333. case 18:
  7334. val |= PIPEMISC_DITHER_6_BPC;
  7335. break;
  7336. case 24:
  7337. val |= PIPEMISC_DITHER_8_BPC;
  7338. break;
  7339. case 30:
  7340. val |= PIPEMISC_DITHER_10_BPC;
  7341. break;
  7342. case 36:
  7343. val |= PIPEMISC_DITHER_12_BPC;
  7344. break;
  7345. default:
  7346. /* Case prevented by pipe_config_set_bpp. */
  7347. BUG();
  7348. }
  7349. if (intel_crtc->config->dither)
  7350. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7351. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7352. }
  7353. }
  7354. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7355. {
  7356. /*
  7357. * Account for spread spectrum to avoid
  7358. * oversubscribing the link. Max center spread
  7359. * is 2.5%; use 5% for safety's sake.
  7360. */
  7361. u32 bps = target_clock * bpp * 21 / 20;
  7362. return DIV_ROUND_UP(bps, link_bw * 8);
  7363. }
  7364. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7365. {
  7366. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7367. }
  7368. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7369. struct intel_crtc_state *crtc_state,
  7370. struct dpll *reduced_clock)
  7371. {
  7372. struct drm_crtc *crtc = &intel_crtc->base;
  7373. struct drm_device *dev = crtc->dev;
  7374. struct drm_i915_private *dev_priv = dev->dev_private;
  7375. struct drm_atomic_state *state = crtc_state->base.state;
  7376. struct drm_connector *connector;
  7377. struct drm_connector_state *connector_state;
  7378. struct intel_encoder *encoder;
  7379. u32 dpll, fp, fp2;
  7380. int factor, i;
  7381. bool is_lvds = false, is_sdvo = false;
  7382. for_each_connector_in_state(state, connector, connector_state, i) {
  7383. if (connector_state->crtc != crtc_state->base.crtc)
  7384. continue;
  7385. encoder = to_intel_encoder(connector_state->best_encoder);
  7386. switch (encoder->type) {
  7387. case INTEL_OUTPUT_LVDS:
  7388. is_lvds = true;
  7389. break;
  7390. case INTEL_OUTPUT_SDVO:
  7391. case INTEL_OUTPUT_HDMI:
  7392. is_sdvo = true;
  7393. break;
  7394. default:
  7395. break;
  7396. }
  7397. }
  7398. /* Enable autotuning of the PLL clock (if permissible) */
  7399. factor = 21;
  7400. if (is_lvds) {
  7401. if ((intel_panel_use_ssc(dev_priv) &&
  7402. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7403. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7404. factor = 25;
  7405. } else if (crtc_state->sdvo_tv_clock)
  7406. factor = 20;
  7407. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7408. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7409. fp |= FP_CB_TUNE;
  7410. if (reduced_clock) {
  7411. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7412. if (reduced_clock->m < factor * reduced_clock->n)
  7413. fp2 |= FP_CB_TUNE;
  7414. } else {
  7415. fp2 = fp;
  7416. }
  7417. dpll = 0;
  7418. if (is_lvds)
  7419. dpll |= DPLLB_MODE_LVDS;
  7420. else
  7421. dpll |= DPLLB_MODE_DAC_SERIAL;
  7422. dpll |= (crtc_state->pixel_multiplier - 1)
  7423. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7424. if (is_sdvo)
  7425. dpll |= DPLL_SDVO_HIGH_SPEED;
  7426. if (crtc_state->has_dp_encoder)
  7427. dpll |= DPLL_SDVO_HIGH_SPEED;
  7428. /* compute bitmask from p1 value */
  7429. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7430. /* also FPA1 */
  7431. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7432. switch (crtc_state->dpll.p2) {
  7433. case 5:
  7434. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7435. break;
  7436. case 7:
  7437. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7438. break;
  7439. case 10:
  7440. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7441. break;
  7442. case 14:
  7443. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7444. break;
  7445. }
  7446. if (is_lvds && intel_panel_use_ssc(dev_priv))
  7447. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7448. else
  7449. dpll |= PLL_REF_INPUT_DREFCLK;
  7450. dpll |= DPLL_VCO_ENABLE;
  7451. crtc_state->dpll_hw_state.dpll = dpll;
  7452. crtc_state->dpll_hw_state.fp0 = fp;
  7453. crtc_state->dpll_hw_state.fp1 = fp2;
  7454. }
  7455. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7456. struct intel_crtc_state *crtc_state)
  7457. {
  7458. struct drm_device *dev = crtc->base.dev;
  7459. struct drm_i915_private *dev_priv = dev->dev_private;
  7460. struct dpll reduced_clock;
  7461. bool has_reduced_clock = false;
  7462. struct intel_shared_dpll *pll;
  7463. const struct intel_limit *limit;
  7464. int refclk = 120000;
  7465. memset(&crtc_state->dpll_hw_state, 0,
  7466. sizeof(crtc_state->dpll_hw_state));
  7467. crtc->lowfreq_avail = false;
  7468. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7469. if (!crtc_state->has_pch_encoder)
  7470. return 0;
  7471. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7472. if (intel_panel_use_ssc(dev_priv)) {
  7473. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7474. dev_priv->vbt.lvds_ssc_freq);
  7475. refclk = dev_priv->vbt.lvds_ssc_freq;
  7476. }
  7477. if (intel_is_dual_link_lvds(dev)) {
  7478. if (refclk == 100000)
  7479. limit = &intel_limits_ironlake_dual_lvds_100m;
  7480. else
  7481. limit = &intel_limits_ironlake_dual_lvds;
  7482. } else {
  7483. if (refclk == 100000)
  7484. limit = &intel_limits_ironlake_single_lvds_100m;
  7485. else
  7486. limit = &intel_limits_ironlake_single_lvds;
  7487. }
  7488. } else {
  7489. limit = &intel_limits_ironlake_dac;
  7490. }
  7491. if (!crtc_state->clock_set &&
  7492. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7493. refclk, NULL, &crtc_state->dpll)) {
  7494. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7495. return -EINVAL;
  7496. }
  7497. ironlake_compute_dpll(crtc, crtc_state,
  7498. has_reduced_clock ? &reduced_clock : NULL);
  7499. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  7500. if (pll == NULL) {
  7501. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7502. pipe_name(crtc->pipe));
  7503. return -EINVAL;
  7504. }
  7505. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7506. has_reduced_clock)
  7507. crtc->lowfreq_avail = true;
  7508. return 0;
  7509. }
  7510. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7511. struct intel_link_m_n *m_n)
  7512. {
  7513. struct drm_device *dev = crtc->base.dev;
  7514. struct drm_i915_private *dev_priv = dev->dev_private;
  7515. enum pipe pipe = crtc->pipe;
  7516. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7517. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7518. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7519. & ~TU_SIZE_MASK;
  7520. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7521. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7522. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7523. }
  7524. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7525. enum transcoder transcoder,
  7526. struct intel_link_m_n *m_n,
  7527. struct intel_link_m_n *m2_n2)
  7528. {
  7529. struct drm_device *dev = crtc->base.dev;
  7530. struct drm_i915_private *dev_priv = dev->dev_private;
  7531. enum pipe pipe = crtc->pipe;
  7532. if (INTEL_INFO(dev)->gen >= 5) {
  7533. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7534. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7535. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7536. & ~TU_SIZE_MASK;
  7537. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7538. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7539. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7540. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7541. * gen < 8) and if DRRS is supported (to make sure the
  7542. * registers are not unnecessarily read).
  7543. */
  7544. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7545. crtc->config->has_drrs) {
  7546. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7547. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7548. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7549. & ~TU_SIZE_MASK;
  7550. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7551. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7552. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7553. }
  7554. } else {
  7555. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7556. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7557. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7558. & ~TU_SIZE_MASK;
  7559. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7560. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7561. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7562. }
  7563. }
  7564. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7565. struct intel_crtc_state *pipe_config)
  7566. {
  7567. if (pipe_config->has_pch_encoder)
  7568. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7569. else
  7570. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7571. &pipe_config->dp_m_n,
  7572. &pipe_config->dp_m2_n2);
  7573. }
  7574. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7575. struct intel_crtc_state *pipe_config)
  7576. {
  7577. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7578. &pipe_config->fdi_m_n, NULL);
  7579. }
  7580. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7581. struct intel_crtc_state *pipe_config)
  7582. {
  7583. struct drm_device *dev = crtc->base.dev;
  7584. struct drm_i915_private *dev_priv = dev->dev_private;
  7585. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7586. uint32_t ps_ctrl = 0;
  7587. int id = -1;
  7588. int i;
  7589. /* find scaler attached to this pipe */
  7590. for (i = 0; i < crtc->num_scalers; i++) {
  7591. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7592. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7593. id = i;
  7594. pipe_config->pch_pfit.enabled = true;
  7595. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7596. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7597. break;
  7598. }
  7599. }
  7600. scaler_state->scaler_id = id;
  7601. if (id >= 0) {
  7602. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7603. } else {
  7604. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7605. }
  7606. }
  7607. static void
  7608. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7609. struct intel_initial_plane_config *plane_config)
  7610. {
  7611. struct drm_device *dev = crtc->base.dev;
  7612. struct drm_i915_private *dev_priv = dev->dev_private;
  7613. u32 val, base, offset, stride_mult, tiling;
  7614. int pipe = crtc->pipe;
  7615. int fourcc, pixel_format;
  7616. unsigned int aligned_height;
  7617. struct drm_framebuffer *fb;
  7618. struct intel_framebuffer *intel_fb;
  7619. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7620. if (!intel_fb) {
  7621. DRM_DEBUG_KMS("failed to alloc fb\n");
  7622. return;
  7623. }
  7624. fb = &intel_fb->base;
  7625. val = I915_READ(PLANE_CTL(pipe, 0));
  7626. if (!(val & PLANE_CTL_ENABLE))
  7627. goto error;
  7628. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7629. fourcc = skl_format_to_fourcc(pixel_format,
  7630. val & PLANE_CTL_ORDER_RGBX,
  7631. val & PLANE_CTL_ALPHA_MASK);
  7632. fb->pixel_format = fourcc;
  7633. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7634. tiling = val & PLANE_CTL_TILED_MASK;
  7635. switch (tiling) {
  7636. case PLANE_CTL_TILED_LINEAR:
  7637. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7638. break;
  7639. case PLANE_CTL_TILED_X:
  7640. plane_config->tiling = I915_TILING_X;
  7641. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7642. break;
  7643. case PLANE_CTL_TILED_Y:
  7644. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7645. break;
  7646. case PLANE_CTL_TILED_YF:
  7647. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7648. break;
  7649. default:
  7650. MISSING_CASE(tiling);
  7651. goto error;
  7652. }
  7653. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7654. plane_config->base = base;
  7655. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7656. val = I915_READ(PLANE_SIZE(pipe, 0));
  7657. fb->height = ((val >> 16) & 0xfff) + 1;
  7658. fb->width = ((val >> 0) & 0x1fff) + 1;
  7659. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7660. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  7661. fb->pixel_format);
  7662. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7663. aligned_height = intel_fb_align_height(dev, fb->height,
  7664. fb->pixel_format,
  7665. fb->modifier[0]);
  7666. plane_config->size = fb->pitches[0] * aligned_height;
  7667. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7668. pipe_name(pipe), fb->width, fb->height,
  7669. fb->bits_per_pixel, base, fb->pitches[0],
  7670. plane_config->size);
  7671. plane_config->fb = intel_fb;
  7672. return;
  7673. error:
  7674. kfree(fb);
  7675. }
  7676. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7677. struct intel_crtc_state *pipe_config)
  7678. {
  7679. struct drm_device *dev = crtc->base.dev;
  7680. struct drm_i915_private *dev_priv = dev->dev_private;
  7681. uint32_t tmp;
  7682. tmp = I915_READ(PF_CTL(crtc->pipe));
  7683. if (tmp & PF_ENABLE) {
  7684. pipe_config->pch_pfit.enabled = true;
  7685. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7686. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7687. /* We currently do not free assignements of panel fitters on
  7688. * ivb/hsw (since we don't use the higher upscaling modes which
  7689. * differentiates them) so just WARN about this case for now. */
  7690. if (IS_GEN7(dev)) {
  7691. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7692. PF_PIPE_SEL_IVB(crtc->pipe));
  7693. }
  7694. }
  7695. }
  7696. static void
  7697. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7698. struct intel_initial_plane_config *plane_config)
  7699. {
  7700. struct drm_device *dev = crtc->base.dev;
  7701. struct drm_i915_private *dev_priv = dev->dev_private;
  7702. u32 val, base, offset;
  7703. int pipe = crtc->pipe;
  7704. int fourcc, pixel_format;
  7705. unsigned int aligned_height;
  7706. struct drm_framebuffer *fb;
  7707. struct intel_framebuffer *intel_fb;
  7708. val = I915_READ(DSPCNTR(pipe));
  7709. if (!(val & DISPLAY_PLANE_ENABLE))
  7710. return;
  7711. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7712. if (!intel_fb) {
  7713. DRM_DEBUG_KMS("failed to alloc fb\n");
  7714. return;
  7715. }
  7716. fb = &intel_fb->base;
  7717. if (INTEL_INFO(dev)->gen >= 4) {
  7718. if (val & DISPPLANE_TILED) {
  7719. plane_config->tiling = I915_TILING_X;
  7720. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7721. }
  7722. }
  7723. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7724. fourcc = i9xx_format_to_fourcc(pixel_format);
  7725. fb->pixel_format = fourcc;
  7726. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7727. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7728. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7729. offset = I915_READ(DSPOFFSET(pipe));
  7730. } else {
  7731. if (plane_config->tiling)
  7732. offset = I915_READ(DSPTILEOFF(pipe));
  7733. else
  7734. offset = I915_READ(DSPLINOFF(pipe));
  7735. }
  7736. plane_config->base = base;
  7737. val = I915_READ(PIPESRC(pipe));
  7738. fb->width = ((val >> 16) & 0xfff) + 1;
  7739. fb->height = ((val >> 0) & 0xfff) + 1;
  7740. val = I915_READ(DSPSTRIDE(pipe));
  7741. fb->pitches[0] = val & 0xffffffc0;
  7742. aligned_height = intel_fb_align_height(dev, fb->height,
  7743. fb->pixel_format,
  7744. fb->modifier[0]);
  7745. plane_config->size = fb->pitches[0] * aligned_height;
  7746. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7747. pipe_name(pipe), fb->width, fb->height,
  7748. fb->bits_per_pixel, base, fb->pitches[0],
  7749. plane_config->size);
  7750. plane_config->fb = intel_fb;
  7751. }
  7752. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7753. struct intel_crtc_state *pipe_config)
  7754. {
  7755. struct drm_device *dev = crtc->base.dev;
  7756. struct drm_i915_private *dev_priv = dev->dev_private;
  7757. enum intel_display_power_domain power_domain;
  7758. uint32_t tmp;
  7759. bool ret;
  7760. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7761. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7762. return false;
  7763. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7764. pipe_config->shared_dpll = NULL;
  7765. ret = false;
  7766. tmp = I915_READ(PIPECONF(crtc->pipe));
  7767. if (!(tmp & PIPECONF_ENABLE))
  7768. goto out;
  7769. switch (tmp & PIPECONF_BPC_MASK) {
  7770. case PIPECONF_6BPC:
  7771. pipe_config->pipe_bpp = 18;
  7772. break;
  7773. case PIPECONF_8BPC:
  7774. pipe_config->pipe_bpp = 24;
  7775. break;
  7776. case PIPECONF_10BPC:
  7777. pipe_config->pipe_bpp = 30;
  7778. break;
  7779. case PIPECONF_12BPC:
  7780. pipe_config->pipe_bpp = 36;
  7781. break;
  7782. default:
  7783. break;
  7784. }
  7785. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7786. pipe_config->limited_color_range = true;
  7787. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7788. struct intel_shared_dpll *pll;
  7789. enum intel_dpll_id pll_id;
  7790. pipe_config->has_pch_encoder = true;
  7791. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7792. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7793. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7794. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7795. if (HAS_PCH_IBX(dev_priv)) {
  7796. /*
  7797. * The pipe->pch transcoder and pch transcoder->pll
  7798. * mapping is fixed.
  7799. */
  7800. pll_id = (enum intel_dpll_id) crtc->pipe;
  7801. } else {
  7802. tmp = I915_READ(PCH_DPLL_SEL);
  7803. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7804. pll_id = DPLL_ID_PCH_PLL_B;
  7805. else
  7806. pll_id= DPLL_ID_PCH_PLL_A;
  7807. }
  7808. pipe_config->shared_dpll =
  7809. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7810. pll = pipe_config->shared_dpll;
  7811. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7812. &pipe_config->dpll_hw_state));
  7813. tmp = pipe_config->dpll_hw_state.dpll;
  7814. pipe_config->pixel_multiplier =
  7815. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7816. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7817. ironlake_pch_clock_get(crtc, pipe_config);
  7818. } else {
  7819. pipe_config->pixel_multiplier = 1;
  7820. }
  7821. intel_get_pipe_timings(crtc, pipe_config);
  7822. intel_get_pipe_src_size(crtc, pipe_config);
  7823. ironlake_get_pfit_config(crtc, pipe_config);
  7824. ret = true;
  7825. out:
  7826. intel_display_power_put(dev_priv, power_domain);
  7827. return ret;
  7828. }
  7829. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7830. {
  7831. struct drm_device *dev = dev_priv->dev;
  7832. struct intel_crtc *crtc;
  7833. for_each_intel_crtc(dev, crtc)
  7834. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7835. pipe_name(crtc->pipe));
  7836. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7837. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7838. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7839. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7840. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7841. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7842. "CPU PWM1 enabled\n");
  7843. if (IS_HASWELL(dev))
  7844. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7845. "CPU PWM2 enabled\n");
  7846. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7847. "PCH PWM1 enabled\n");
  7848. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7849. "Utility pin enabled\n");
  7850. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7851. /*
  7852. * In theory we can still leave IRQs enabled, as long as only the HPD
  7853. * interrupts remain enabled. We used to check for that, but since it's
  7854. * gen-specific and since we only disable LCPLL after we fully disable
  7855. * the interrupts, the check below should be enough.
  7856. */
  7857. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7858. }
  7859. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7860. {
  7861. struct drm_device *dev = dev_priv->dev;
  7862. if (IS_HASWELL(dev))
  7863. return I915_READ(D_COMP_HSW);
  7864. else
  7865. return I915_READ(D_COMP_BDW);
  7866. }
  7867. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7868. {
  7869. struct drm_device *dev = dev_priv->dev;
  7870. if (IS_HASWELL(dev)) {
  7871. mutex_lock(&dev_priv->rps.hw_lock);
  7872. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7873. val))
  7874. DRM_ERROR("Failed to write to D_COMP\n");
  7875. mutex_unlock(&dev_priv->rps.hw_lock);
  7876. } else {
  7877. I915_WRITE(D_COMP_BDW, val);
  7878. POSTING_READ(D_COMP_BDW);
  7879. }
  7880. }
  7881. /*
  7882. * This function implements pieces of two sequences from BSpec:
  7883. * - Sequence for display software to disable LCPLL
  7884. * - Sequence for display software to allow package C8+
  7885. * The steps implemented here are just the steps that actually touch the LCPLL
  7886. * register. Callers should take care of disabling all the display engine
  7887. * functions, doing the mode unset, fixing interrupts, etc.
  7888. */
  7889. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7890. bool switch_to_fclk, bool allow_power_down)
  7891. {
  7892. uint32_t val;
  7893. assert_can_disable_lcpll(dev_priv);
  7894. val = I915_READ(LCPLL_CTL);
  7895. if (switch_to_fclk) {
  7896. val |= LCPLL_CD_SOURCE_FCLK;
  7897. I915_WRITE(LCPLL_CTL, val);
  7898. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7899. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7900. DRM_ERROR("Switching to FCLK failed\n");
  7901. val = I915_READ(LCPLL_CTL);
  7902. }
  7903. val |= LCPLL_PLL_DISABLE;
  7904. I915_WRITE(LCPLL_CTL, val);
  7905. POSTING_READ(LCPLL_CTL);
  7906. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7907. DRM_ERROR("LCPLL still locked\n");
  7908. val = hsw_read_dcomp(dev_priv);
  7909. val |= D_COMP_COMP_DISABLE;
  7910. hsw_write_dcomp(dev_priv, val);
  7911. ndelay(100);
  7912. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7913. 1))
  7914. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7915. if (allow_power_down) {
  7916. val = I915_READ(LCPLL_CTL);
  7917. val |= LCPLL_POWER_DOWN_ALLOW;
  7918. I915_WRITE(LCPLL_CTL, val);
  7919. POSTING_READ(LCPLL_CTL);
  7920. }
  7921. }
  7922. /*
  7923. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7924. * source.
  7925. */
  7926. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7927. {
  7928. uint32_t val;
  7929. val = I915_READ(LCPLL_CTL);
  7930. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7931. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7932. return;
  7933. /*
  7934. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7935. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7936. */
  7937. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7938. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7939. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7940. I915_WRITE(LCPLL_CTL, val);
  7941. POSTING_READ(LCPLL_CTL);
  7942. }
  7943. val = hsw_read_dcomp(dev_priv);
  7944. val |= D_COMP_COMP_FORCE;
  7945. val &= ~D_COMP_COMP_DISABLE;
  7946. hsw_write_dcomp(dev_priv, val);
  7947. val = I915_READ(LCPLL_CTL);
  7948. val &= ~LCPLL_PLL_DISABLE;
  7949. I915_WRITE(LCPLL_CTL, val);
  7950. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7951. DRM_ERROR("LCPLL not locked yet\n");
  7952. if (val & LCPLL_CD_SOURCE_FCLK) {
  7953. val = I915_READ(LCPLL_CTL);
  7954. val &= ~LCPLL_CD_SOURCE_FCLK;
  7955. I915_WRITE(LCPLL_CTL, val);
  7956. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7957. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7958. DRM_ERROR("Switching back to LCPLL failed\n");
  7959. }
  7960. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7961. intel_update_cdclk(dev_priv->dev);
  7962. }
  7963. /*
  7964. * Package states C8 and deeper are really deep PC states that can only be
  7965. * reached when all the devices on the system allow it, so even if the graphics
  7966. * device allows PC8+, it doesn't mean the system will actually get to these
  7967. * states. Our driver only allows PC8+ when going into runtime PM.
  7968. *
  7969. * The requirements for PC8+ are that all the outputs are disabled, the power
  7970. * well is disabled and most interrupts are disabled, and these are also
  7971. * requirements for runtime PM. When these conditions are met, we manually do
  7972. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7973. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7974. * hang the machine.
  7975. *
  7976. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7977. * the state of some registers, so when we come back from PC8+ we need to
  7978. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7979. * need to take care of the registers kept by RC6. Notice that this happens even
  7980. * if we don't put the device in PCI D3 state (which is what currently happens
  7981. * because of the runtime PM support).
  7982. *
  7983. * For more, read "Display Sequences for Package C8" on the hardware
  7984. * documentation.
  7985. */
  7986. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7987. {
  7988. struct drm_device *dev = dev_priv->dev;
  7989. uint32_t val;
  7990. DRM_DEBUG_KMS("Enabling package C8+\n");
  7991. if (HAS_PCH_LPT_LP(dev)) {
  7992. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7993. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7994. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7995. }
  7996. lpt_disable_clkout_dp(dev);
  7997. hsw_disable_lcpll(dev_priv, true, true);
  7998. }
  7999. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8000. {
  8001. struct drm_device *dev = dev_priv->dev;
  8002. uint32_t val;
  8003. DRM_DEBUG_KMS("Disabling package C8+\n");
  8004. hsw_restore_lcpll(dev_priv);
  8005. lpt_init_pch_refclk(dev);
  8006. if (HAS_PCH_LPT_LP(dev)) {
  8007. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8008. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8009. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8010. }
  8011. }
  8012. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8013. {
  8014. struct drm_device *dev = old_state->dev;
  8015. struct intel_atomic_state *old_intel_state =
  8016. to_intel_atomic_state(old_state);
  8017. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8018. broxton_set_cdclk(to_i915(dev), req_cdclk);
  8019. }
  8020. /* compute the max rate for new configuration */
  8021. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8022. {
  8023. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8024. struct drm_i915_private *dev_priv = state->dev->dev_private;
  8025. struct drm_crtc *crtc;
  8026. struct drm_crtc_state *cstate;
  8027. struct intel_crtc_state *crtc_state;
  8028. unsigned max_pixel_rate = 0, i;
  8029. enum pipe pipe;
  8030. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8031. sizeof(intel_state->min_pixclk));
  8032. for_each_crtc_in_state(state, crtc, cstate, i) {
  8033. int pixel_rate;
  8034. crtc_state = to_intel_crtc_state(cstate);
  8035. if (!crtc_state->base.enable) {
  8036. intel_state->min_pixclk[i] = 0;
  8037. continue;
  8038. }
  8039. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8040. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8041. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8042. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8043. intel_state->min_pixclk[i] = pixel_rate;
  8044. }
  8045. for_each_pipe(dev_priv, pipe)
  8046. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8047. return max_pixel_rate;
  8048. }
  8049. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8050. {
  8051. struct drm_i915_private *dev_priv = dev->dev_private;
  8052. uint32_t val, data;
  8053. int ret;
  8054. if (WARN((I915_READ(LCPLL_CTL) &
  8055. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8056. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8057. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8058. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8059. "trying to change cdclk frequency with cdclk not enabled\n"))
  8060. return;
  8061. mutex_lock(&dev_priv->rps.hw_lock);
  8062. ret = sandybridge_pcode_write(dev_priv,
  8063. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8064. mutex_unlock(&dev_priv->rps.hw_lock);
  8065. if (ret) {
  8066. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8067. return;
  8068. }
  8069. val = I915_READ(LCPLL_CTL);
  8070. val |= LCPLL_CD_SOURCE_FCLK;
  8071. I915_WRITE(LCPLL_CTL, val);
  8072. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8073. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8074. DRM_ERROR("Switching to FCLK failed\n");
  8075. val = I915_READ(LCPLL_CTL);
  8076. val &= ~LCPLL_CLK_FREQ_MASK;
  8077. switch (cdclk) {
  8078. case 450000:
  8079. val |= LCPLL_CLK_FREQ_450;
  8080. data = 0;
  8081. break;
  8082. case 540000:
  8083. val |= LCPLL_CLK_FREQ_54O_BDW;
  8084. data = 1;
  8085. break;
  8086. case 337500:
  8087. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8088. data = 2;
  8089. break;
  8090. case 675000:
  8091. val |= LCPLL_CLK_FREQ_675_BDW;
  8092. data = 3;
  8093. break;
  8094. default:
  8095. WARN(1, "invalid cdclk frequency\n");
  8096. return;
  8097. }
  8098. I915_WRITE(LCPLL_CTL, val);
  8099. val = I915_READ(LCPLL_CTL);
  8100. val &= ~LCPLL_CD_SOURCE_FCLK;
  8101. I915_WRITE(LCPLL_CTL, val);
  8102. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8103. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8104. DRM_ERROR("Switching back to LCPLL failed\n");
  8105. mutex_lock(&dev_priv->rps.hw_lock);
  8106. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8107. mutex_unlock(&dev_priv->rps.hw_lock);
  8108. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8109. intel_update_cdclk(dev);
  8110. WARN(cdclk != dev_priv->cdclk_freq,
  8111. "cdclk requested %d kHz but got %d kHz\n",
  8112. cdclk, dev_priv->cdclk_freq);
  8113. }
  8114. static int broadwell_calc_cdclk(int max_pixclk)
  8115. {
  8116. if (max_pixclk > 540000)
  8117. return 675000;
  8118. else if (max_pixclk > 450000)
  8119. return 540000;
  8120. else if (max_pixclk > 337500)
  8121. return 450000;
  8122. else
  8123. return 337500;
  8124. }
  8125. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8126. {
  8127. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8128. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8129. int max_pixclk = ilk_max_pixel_rate(state);
  8130. int cdclk;
  8131. /*
  8132. * FIXME should also account for plane ratio
  8133. * once 64bpp pixel formats are supported.
  8134. */
  8135. cdclk = broadwell_calc_cdclk(max_pixclk);
  8136. if (cdclk > dev_priv->max_cdclk_freq) {
  8137. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8138. cdclk, dev_priv->max_cdclk_freq);
  8139. return -EINVAL;
  8140. }
  8141. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8142. if (!intel_state->active_crtcs)
  8143. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8144. return 0;
  8145. }
  8146. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8147. {
  8148. struct drm_device *dev = old_state->dev;
  8149. struct intel_atomic_state *old_intel_state =
  8150. to_intel_atomic_state(old_state);
  8151. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8152. broadwell_set_cdclk(dev, req_cdclk);
  8153. }
  8154. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8155. {
  8156. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8157. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8158. const int max_pixclk = ilk_max_pixel_rate(state);
  8159. int vco = intel_state->cdclk_pll_vco;
  8160. int cdclk;
  8161. /*
  8162. * FIXME should also account for plane ratio
  8163. * once 64bpp pixel formats are supported.
  8164. */
  8165. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8166. /*
  8167. * FIXME move the cdclk caclulation to
  8168. * compute_config() so we can fail gracegully.
  8169. */
  8170. if (cdclk > dev_priv->max_cdclk_freq) {
  8171. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8172. cdclk, dev_priv->max_cdclk_freq);
  8173. cdclk = dev_priv->max_cdclk_freq;
  8174. }
  8175. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8176. if (!intel_state->active_crtcs)
  8177. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8178. return 0;
  8179. }
  8180. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8181. {
  8182. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8183. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8184. unsigned int req_cdclk = intel_state->dev_cdclk;
  8185. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8186. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8187. }
  8188. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8189. struct intel_crtc_state *crtc_state)
  8190. {
  8191. struct intel_encoder *intel_encoder =
  8192. intel_ddi_get_crtc_new_encoder(crtc_state);
  8193. if (intel_encoder->type != INTEL_OUTPUT_DSI) {
  8194. if (!intel_ddi_pll_select(crtc, crtc_state))
  8195. return -EINVAL;
  8196. }
  8197. crtc->lowfreq_avail = false;
  8198. return 0;
  8199. }
  8200. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8201. enum port port,
  8202. struct intel_crtc_state *pipe_config)
  8203. {
  8204. enum intel_dpll_id id;
  8205. switch (port) {
  8206. case PORT_A:
  8207. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8208. id = DPLL_ID_SKL_DPLL0;
  8209. break;
  8210. case PORT_B:
  8211. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8212. id = DPLL_ID_SKL_DPLL1;
  8213. break;
  8214. case PORT_C:
  8215. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8216. id = DPLL_ID_SKL_DPLL2;
  8217. break;
  8218. default:
  8219. DRM_ERROR("Incorrect port type\n");
  8220. return;
  8221. }
  8222. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8223. }
  8224. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8225. enum port port,
  8226. struct intel_crtc_state *pipe_config)
  8227. {
  8228. enum intel_dpll_id id;
  8229. u32 temp;
  8230. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8231. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8232. switch (pipe_config->ddi_pll_sel) {
  8233. case SKL_DPLL0:
  8234. id = DPLL_ID_SKL_DPLL0;
  8235. break;
  8236. case SKL_DPLL1:
  8237. id = DPLL_ID_SKL_DPLL1;
  8238. break;
  8239. case SKL_DPLL2:
  8240. id = DPLL_ID_SKL_DPLL2;
  8241. break;
  8242. case SKL_DPLL3:
  8243. id = DPLL_ID_SKL_DPLL3;
  8244. break;
  8245. default:
  8246. MISSING_CASE(pipe_config->ddi_pll_sel);
  8247. return;
  8248. }
  8249. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8250. }
  8251. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8252. enum port port,
  8253. struct intel_crtc_state *pipe_config)
  8254. {
  8255. enum intel_dpll_id id;
  8256. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8257. switch (pipe_config->ddi_pll_sel) {
  8258. case PORT_CLK_SEL_WRPLL1:
  8259. id = DPLL_ID_WRPLL1;
  8260. break;
  8261. case PORT_CLK_SEL_WRPLL2:
  8262. id = DPLL_ID_WRPLL2;
  8263. break;
  8264. case PORT_CLK_SEL_SPLL:
  8265. id = DPLL_ID_SPLL;
  8266. break;
  8267. case PORT_CLK_SEL_LCPLL_810:
  8268. id = DPLL_ID_LCPLL_810;
  8269. break;
  8270. case PORT_CLK_SEL_LCPLL_1350:
  8271. id = DPLL_ID_LCPLL_1350;
  8272. break;
  8273. case PORT_CLK_SEL_LCPLL_2700:
  8274. id = DPLL_ID_LCPLL_2700;
  8275. break;
  8276. default:
  8277. MISSING_CASE(pipe_config->ddi_pll_sel);
  8278. /* fall through */
  8279. case PORT_CLK_SEL_NONE:
  8280. return;
  8281. }
  8282. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8283. }
  8284. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8285. struct intel_crtc_state *pipe_config,
  8286. unsigned long *power_domain_mask)
  8287. {
  8288. struct drm_device *dev = crtc->base.dev;
  8289. struct drm_i915_private *dev_priv = dev->dev_private;
  8290. enum intel_display_power_domain power_domain;
  8291. u32 tmp;
  8292. /*
  8293. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8294. * transcoder handled below.
  8295. */
  8296. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8297. /*
  8298. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8299. * consistency and less surprising code; it's in always on power).
  8300. */
  8301. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8302. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8303. enum pipe trans_edp_pipe;
  8304. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8305. default:
  8306. WARN(1, "unknown pipe linked to edp transcoder\n");
  8307. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8308. case TRANS_DDI_EDP_INPUT_A_ON:
  8309. trans_edp_pipe = PIPE_A;
  8310. break;
  8311. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8312. trans_edp_pipe = PIPE_B;
  8313. break;
  8314. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8315. trans_edp_pipe = PIPE_C;
  8316. break;
  8317. }
  8318. if (trans_edp_pipe == crtc->pipe)
  8319. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8320. }
  8321. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8322. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8323. return false;
  8324. *power_domain_mask |= BIT(power_domain);
  8325. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8326. return tmp & PIPECONF_ENABLE;
  8327. }
  8328. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8329. struct intel_crtc_state *pipe_config,
  8330. unsigned long *power_domain_mask)
  8331. {
  8332. struct drm_device *dev = crtc->base.dev;
  8333. struct drm_i915_private *dev_priv = dev->dev_private;
  8334. enum intel_display_power_domain power_domain;
  8335. enum port port;
  8336. enum transcoder cpu_transcoder;
  8337. u32 tmp;
  8338. pipe_config->has_dsi_encoder = false;
  8339. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8340. if (port == PORT_A)
  8341. cpu_transcoder = TRANSCODER_DSI_A;
  8342. else
  8343. cpu_transcoder = TRANSCODER_DSI_C;
  8344. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8345. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8346. continue;
  8347. *power_domain_mask |= BIT(power_domain);
  8348. /*
  8349. * The PLL needs to be enabled with a valid divider
  8350. * configuration, otherwise accessing DSI registers will hang
  8351. * the machine. See BSpec North Display Engine
  8352. * registers/MIPI[BXT]. We can break out here early, since we
  8353. * need the same DSI PLL to be enabled for both DSI ports.
  8354. */
  8355. if (!intel_dsi_pll_is_enabled(dev_priv))
  8356. break;
  8357. /* XXX: this works for video mode only */
  8358. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8359. if (!(tmp & DPI_ENABLE))
  8360. continue;
  8361. tmp = I915_READ(MIPI_CTRL(port));
  8362. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8363. continue;
  8364. pipe_config->cpu_transcoder = cpu_transcoder;
  8365. pipe_config->has_dsi_encoder = true;
  8366. break;
  8367. }
  8368. return pipe_config->has_dsi_encoder;
  8369. }
  8370. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8371. struct intel_crtc_state *pipe_config)
  8372. {
  8373. struct drm_device *dev = crtc->base.dev;
  8374. struct drm_i915_private *dev_priv = dev->dev_private;
  8375. struct intel_shared_dpll *pll;
  8376. enum port port;
  8377. uint32_t tmp;
  8378. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8379. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8380. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8381. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8382. else if (IS_BROXTON(dev))
  8383. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8384. else
  8385. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8386. pll = pipe_config->shared_dpll;
  8387. if (pll) {
  8388. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8389. &pipe_config->dpll_hw_state));
  8390. }
  8391. /*
  8392. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8393. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8394. * the PCH transcoder is on.
  8395. */
  8396. if (INTEL_INFO(dev)->gen < 9 &&
  8397. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8398. pipe_config->has_pch_encoder = true;
  8399. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8400. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8401. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8402. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8403. }
  8404. }
  8405. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8406. struct intel_crtc_state *pipe_config)
  8407. {
  8408. struct drm_device *dev = crtc->base.dev;
  8409. struct drm_i915_private *dev_priv = dev->dev_private;
  8410. enum intel_display_power_domain power_domain;
  8411. unsigned long power_domain_mask;
  8412. bool active;
  8413. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8414. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8415. return false;
  8416. power_domain_mask = BIT(power_domain);
  8417. pipe_config->shared_dpll = NULL;
  8418. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8419. if (IS_BROXTON(dev_priv)) {
  8420. bxt_get_dsi_transcoder_state(crtc, pipe_config,
  8421. &power_domain_mask);
  8422. WARN_ON(active && pipe_config->has_dsi_encoder);
  8423. if (pipe_config->has_dsi_encoder)
  8424. active = true;
  8425. }
  8426. if (!active)
  8427. goto out;
  8428. if (!pipe_config->has_dsi_encoder) {
  8429. haswell_get_ddi_port_state(crtc, pipe_config);
  8430. intel_get_pipe_timings(crtc, pipe_config);
  8431. }
  8432. intel_get_pipe_src_size(crtc, pipe_config);
  8433. pipe_config->gamma_mode =
  8434. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8435. if (INTEL_INFO(dev)->gen >= 9) {
  8436. skl_init_scalers(dev, crtc, pipe_config);
  8437. }
  8438. if (INTEL_INFO(dev)->gen >= 9) {
  8439. pipe_config->scaler_state.scaler_id = -1;
  8440. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8441. }
  8442. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8443. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8444. power_domain_mask |= BIT(power_domain);
  8445. if (INTEL_INFO(dev)->gen >= 9)
  8446. skylake_get_pfit_config(crtc, pipe_config);
  8447. else
  8448. ironlake_get_pfit_config(crtc, pipe_config);
  8449. }
  8450. if (IS_HASWELL(dev))
  8451. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8452. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8453. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8454. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8455. pipe_config->pixel_multiplier =
  8456. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8457. } else {
  8458. pipe_config->pixel_multiplier = 1;
  8459. }
  8460. out:
  8461. for_each_power_domain(power_domain, power_domain_mask)
  8462. intel_display_power_put(dev_priv, power_domain);
  8463. return active;
  8464. }
  8465. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  8466. const struct intel_plane_state *plane_state)
  8467. {
  8468. struct drm_device *dev = crtc->dev;
  8469. struct drm_i915_private *dev_priv = dev->dev_private;
  8470. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8471. uint32_t cntl = 0, size = 0;
  8472. if (plane_state && plane_state->visible) {
  8473. unsigned int width = plane_state->base.crtc_w;
  8474. unsigned int height = plane_state->base.crtc_h;
  8475. unsigned int stride = roundup_pow_of_two(width) * 4;
  8476. switch (stride) {
  8477. default:
  8478. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8479. width, stride);
  8480. stride = 256;
  8481. /* fallthrough */
  8482. case 256:
  8483. case 512:
  8484. case 1024:
  8485. case 2048:
  8486. break;
  8487. }
  8488. cntl |= CURSOR_ENABLE |
  8489. CURSOR_GAMMA_ENABLE |
  8490. CURSOR_FORMAT_ARGB |
  8491. CURSOR_STRIDE(stride);
  8492. size = (height << 12) | width;
  8493. }
  8494. if (intel_crtc->cursor_cntl != 0 &&
  8495. (intel_crtc->cursor_base != base ||
  8496. intel_crtc->cursor_size != size ||
  8497. intel_crtc->cursor_cntl != cntl)) {
  8498. /* On these chipsets we can only modify the base/size/stride
  8499. * whilst the cursor is disabled.
  8500. */
  8501. I915_WRITE(CURCNTR(PIPE_A), 0);
  8502. POSTING_READ(CURCNTR(PIPE_A));
  8503. intel_crtc->cursor_cntl = 0;
  8504. }
  8505. if (intel_crtc->cursor_base != base) {
  8506. I915_WRITE(CURBASE(PIPE_A), base);
  8507. intel_crtc->cursor_base = base;
  8508. }
  8509. if (intel_crtc->cursor_size != size) {
  8510. I915_WRITE(CURSIZE, size);
  8511. intel_crtc->cursor_size = size;
  8512. }
  8513. if (intel_crtc->cursor_cntl != cntl) {
  8514. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8515. POSTING_READ(CURCNTR(PIPE_A));
  8516. intel_crtc->cursor_cntl = cntl;
  8517. }
  8518. }
  8519. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  8520. const struct intel_plane_state *plane_state)
  8521. {
  8522. struct drm_device *dev = crtc->dev;
  8523. struct drm_i915_private *dev_priv = dev->dev_private;
  8524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8525. int pipe = intel_crtc->pipe;
  8526. uint32_t cntl = 0;
  8527. if (plane_state && plane_state->visible) {
  8528. cntl = MCURSOR_GAMMA_ENABLE;
  8529. switch (plane_state->base.crtc_w) {
  8530. case 64:
  8531. cntl |= CURSOR_MODE_64_ARGB_AX;
  8532. break;
  8533. case 128:
  8534. cntl |= CURSOR_MODE_128_ARGB_AX;
  8535. break;
  8536. case 256:
  8537. cntl |= CURSOR_MODE_256_ARGB_AX;
  8538. break;
  8539. default:
  8540. MISSING_CASE(plane_state->base.crtc_w);
  8541. return;
  8542. }
  8543. cntl |= pipe << 28; /* Connect to correct pipe */
  8544. if (HAS_DDI(dev))
  8545. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8546. if (plane_state->base.rotation == BIT(DRM_ROTATE_180))
  8547. cntl |= CURSOR_ROTATE_180;
  8548. }
  8549. if (intel_crtc->cursor_cntl != cntl) {
  8550. I915_WRITE(CURCNTR(pipe), cntl);
  8551. POSTING_READ(CURCNTR(pipe));
  8552. intel_crtc->cursor_cntl = cntl;
  8553. }
  8554. /* and commit changes on next vblank */
  8555. I915_WRITE(CURBASE(pipe), base);
  8556. POSTING_READ(CURBASE(pipe));
  8557. intel_crtc->cursor_base = base;
  8558. }
  8559. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8560. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8561. const struct intel_plane_state *plane_state)
  8562. {
  8563. struct drm_device *dev = crtc->dev;
  8564. struct drm_i915_private *dev_priv = dev->dev_private;
  8565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8566. int pipe = intel_crtc->pipe;
  8567. u32 base = intel_crtc->cursor_addr;
  8568. u32 pos = 0;
  8569. if (plane_state) {
  8570. int x = plane_state->base.crtc_x;
  8571. int y = plane_state->base.crtc_y;
  8572. if (x < 0) {
  8573. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8574. x = -x;
  8575. }
  8576. pos |= x << CURSOR_X_SHIFT;
  8577. if (y < 0) {
  8578. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8579. y = -y;
  8580. }
  8581. pos |= y << CURSOR_Y_SHIFT;
  8582. /* ILK+ do this automagically */
  8583. if (HAS_GMCH_DISPLAY(dev) &&
  8584. plane_state->base.rotation == BIT(DRM_ROTATE_180)) {
  8585. base += (plane_state->base.crtc_h *
  8586. plane_state->base.crtc_w - 1) * 4;
  8587. }
  8588. }
  8589. I915_WRITE(CURPOS(pipe), pos);
  8590. if (IS_845G(dev) || IS_I865G(dev))
  8591. i845_update_cursor(crtc, base, plane_state);
  8592. else
  8593. i9xx_update_cursor(crtc, base, plane_state);
  8594. }
  8595. static bool cursor_size_ok(struct drm_device *dev,
  8596. uint32_t width, uint32_t height)
  8597. {
  8598. if (width == 0 || height == 0)
  8599. return false;
  8600. /*
  8601. * 845g/865g are special in that they are only limited by
  8602. * the width of their cursors, the height is arbitrary up to
  8603. * the precision of the register. Everything else requires
  8604. * square cursors, limited to a few power-of-two sizes.
  8605. */
  8606. if (IS_845G(dev) || IS_I865G(dev)) {
  8607. if ((width & 63) != 0)
  8608. return false;
  8609. if (width > (IS_845G(dev) ? 64 : 512))
  8610. return false;
  8611. if (height > 1023)
  8612. return false;
  8613. } else {
  8614. switch (width | height) {
  8615. case 256:
  8616. case 128:
  8617. if (IS_GEN2(dev))
  8618. return false;
  8619. case 64:
  8620. break;
  8621. default:
  8622. return false;
  8623. }
  8624. }
  8625. return true;
  8626. }
  8627. /* VESA 640x480x72Hz mode to set on the pipe */
  8628. static struct drm_display_mode load_detect_mode = {
  8629. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8630. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8631. };
  8632. struct drm_framebuffer *
  8633. __intel_framebuffer_create(struct drm_device *dev,
  8634. struct drm_mode_fb_cmd2 *mode_cmd,
  8635. struct drm_i915_gem_object *obj)
  8636. {
  8637. struct intel_framebuffer *intel_fb;
  8638. int ret;
  8639. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8640. if (!intel_fb)
  8641. return ERR_PTR(-ENOMEM);
  8642. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8643. if (ret)
  8644. goto err;
  8645. return &intel_fb->base;
  8646. err:
  8647. kfree(intel_fb);
  8648. return ERR_PTR(ret);
  8649. }
  8650. static struct drm_framebuffer *
  8651. intel_framebuffer_create(struct drm_device *dev,
  8652. struct drm_mode_fb_cmd2 *mode_cmd,
  8653. struct drm_i915_gem_object *obj)
  8654. {
  8655. struct drm_framebuffer *fb;
  8656. int ret;
  8657. ret = i915_mutex_lock_interruptible(dev);
  8658. if (ret)
  8659. return ERR_PTR(ret);
  8660. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8661. mutex_unlock(&dev->struct_mutex);
  8662. return fb;
  8663. }
  8664. static u32
  8665. intel_framebuffer_pitch_for_width(int width, int bpp)
  8666. {
  8667. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8668. return ALIGN(pitch, 64);
  8669. }
  8670. static u32
  8671. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8672. {
  8673. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8674. return PAGE_ALIGN(pitch * mode->vdisplay);
  8675. }
  8676. static struct drm_framebuffer *
  8677. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8678. struct drm_display_mode *mode,
  8679. int depth, int bpp)
  8680. {
  8681. struct drm_framebuffer *fb;
  8682. struct drm_i915_gem_object *obj;
  8683. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8684. obj = i915_gem_object_create(dev,
  8685. intel_framebuffer_size_for_mode(mode, bpp));
  8686. if (IS_ERR(obj))
  8687. return ERR_CAST(obj);
  8688. mode_cmd.width = mode->hdisplay;
  8689. mode_cmd.height = mode->vdisplay;
  8690. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8691. bpp);
  8692. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8693. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8694. if (IS_ERR(fb))
  8695. drm_gem_object_unreference_unlocked(&obj->base);
  8696. return fb;
  8697. }
  8698. static struct drm_framebuffer *
  8699. mode_fits_in_fbdev(struct drm_device *dev,
  8700. struct drm_display_mode *mode)
  8701. {
  8702. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8703. struct drm_i915_private *dev_priv = dev->dev_private;
  8704. struct drm_i915_gem_object *obj;
  8705. struct drm_framebuffer *fb;
  8706. if (!dev_priv->fbdev)
  8707. return NULL;
  8708. if (!dev_priv->fbdev->fb)
  8709. return NULL;
  8710. obj = dev_priv->fbdev->fb->obj;
  8711. BUG_ON(!obj);
  8712. fb = &dev_priv->fbdev->fb->base;
  8713. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8714. fb->bits_per_pixel))
  8715. return NULL;
  8716. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8717. return NULL;
  8718. drm_framebuffer_reference(fb);
  8719. return fb;
  8720. #else
  8721. return NULL;
  8722. #endif
  8723. }
  8724. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8725. struct drm_crtc *crtc,
  8726. struct drm_display_mode *mode,
  8727. struct drm_framebuffer *fb,
  8728. int x, int y)
  8729. {
  8730. struct drm_plane_state *plane_state;
  8731. int hdisplay, vdisplay;
  8732. int ret;
  8733. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8734. if (IS_ERR(plane_state))
  8735. return PTR_ERR(plane_state);
  8736. if (mode)
  8737. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8738. else
  8739. hdisplay = vdisplay = 0;
  8740. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8741. if (ret)
  8742. return ret;
  8743. drm_atomic_set_fb_for_plane(plane_state, fb);
  8744. plane_state->crtc_x = 0;
  8745. plane_state->crtc_y = 0;
  8746. plane_state->crtc_w = hdisplay;
  8747. plane_state->crtc_h = vdisplay;
  8748. plane_state->src_x = x << 16;
  8749. plane_state->src_y = y << 16;
  8750. plane_state->src_w = hdisplay << 16;
  8751. plane_state->src_h = vdisplay << 16;
  8752. return 0;
  8753. }
  8754. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8755. struct drm_display_mode *mode,
  8756. struct intel_load_detect_pipe *old,
  8757. struct drm_modeset_acquire_ctx *ctx)
  8758. {
  8759. struct intel_crtc *intel_crtc;
  8760. struct intel_encoder *intel_encoder =
  8761. intel_attached_encoder(connector);
  8762. struct drm_crtc *possible_crtc;
  8763. struct drm_encoder *encoder = &intel_encoder->base;
  8764. struct drm_crtc *crtc = NULL;
  8765. struct drm_device *dev = encoder->dev;
  8766. struct drm_framebuffer *fb;
  8767. struct drm_mode_config *config = &dev->mode_config;
  8768. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  8769. struct drm_connector_state *connector_state;
  8770. struct intel_crtc_state *crtc_state;
  8771. int ret, i = -1;
  8772. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8773. connector->base.id, connector->name,
  8774. encoder->base.id, encoder->name);
  8775. old->restore_state = NULL;
  8776. retry:
  8777. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8778. if (ret)
  8779. goto fail;
  8780. /*
  8781. * Algorithm gets a little messy:
  8782. *
  8783. * - if the connector already has an assigned crtc, use it (but make
  8784. * sure it's on first)
  8785. *
  8786. * - try to find the first unused crtc that can drive this connector,
  8787. * and use that if we find one
  8788. */
  8789. /* See if we already have a CRTC for this connector */
  8790. if (connector->state->crtc) {
  8791. crtc = connector->state->crtc;
  8792. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8793. if (ret)
  8794. goto fail;
  8795. /* Make sure the crtc and connector are running */
  8796. goto found;
  8797. }
  8798. /* Find an unused one (if possible) */
  8799. for_each_crtc(dev, possible_crtc) {
  8800. i++;
  8801. if (!(encoder->possible_crtcs & (1 << i)))
  8802. continue;
  8803. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  8804. if (ret)
  8805. goto fail;
  8806. if (possible_crtc->state->enable) {
  8807. drm_modeset_unlock(&possible_crtc->mutex);
  8808. continue;
  8809. }
  8810. crtc = possible_crtc;
  8811. break;
  8812. }
  8813. /*
  8814. * If we didn't find an unused CRTC, don't use any.
  8815. */
  8816. if (!crtc) {
  8817. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8818. goto fail;
  8819. }
  8820. found:
  8821. intel_crtc = to_intel_crtc(crtc);
  8822. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8823. if (ret)
  8824. goto fail;
  8825. state = drm_atomic_state_alloc(dev);
  8826. restore_state = drm_atomic_state_alloc(dev);
  8827. if (!state || !restore_state) {
  8828. ret = -ENOMEM;
  8829. goto fail;
  8830. }
  8831. state->acquire_ctx = ctx;
  8832. restore_state->acquire_ctx = ctx;
  8833. connector_state = drm_atomic_get_connector_state(state, connector);
  8834. if (IS_ERR(connector_state)) {
  8835. ret = PTR_ERR(connector_state);
  8836. goto fail;
  8837. }
  8838. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8839. if (ret)
  8840. goto fail;
  8841. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8842. if (IS_ERR(crtc_state)) {
  8843. ret = PTR_ERR(crtc_state);
  8844. goto fail;
  8845. }
  8846. crtc_state->base.active = crtc_state->base.enable = true;
  8847. if (!mode)
  8848. mode = &load_detect_mode;
  8849. /* We need a framebuffer large enough to accommodate all accesses
  8850. * that the plane may generate whilst we perform load detection.
  8851. * We can not rely on the fbcon either being present (we get called
  8852. * during its initialisation to detect all boot displays, or it may
  8853. * not even exist) or that it is large enough to satisfy the
  8854. * requested mode.
  8855. */
  8856. fb = mode_fits_in_fbdev(dev, mode);
  8857. if (fb == NULL) {
  8858. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8859. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8860. } else
  8861. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8862. if (IS_ERR(fb)) {
  8863. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8864. goto fail;
  8865. }
  8866. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8867. if (ret)
  8868. goto fail;
  8869. drm_framebuffer_unreference(fb);
  8870. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8871. if (ret)
  8872. goto fail;
  8873. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8874. if (!ret)
  8875. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8876. if (!ret)
  8877. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8878. if (ret) {
  8879. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8880. goto fail;
  8881. }
  8882. ret = drm_atomic_commit(state);
  8883. if (ret) {
  8884. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8885. goto fail;
  8886. }
  8887. old->restore_state = restore_state;
  8888. /* let the connector get through one full cycle before testing */
  8889. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8890. return true;
  8891. fail:
  8892. drm_atomic_state_free(state);
  8893. drm_atomic_state_free(restore_state);
  8894. restore_state = state = NULL;
  8895. if (ret == -EDEADLK) {
  8896. drm_modeset_backoff(ctx);
  8897. goto retry;
  8898. }
  8899. return false;
  8900. }
  8901. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8902. struct intel_load_detect_pipe *old,
  8903. struct drm_modeset_acquire_ctx *ctx)
  8904. {
  8905. struct intel_encoder *intel_encoder =
  8906. intel_attached_encoder(connector);
  8907. struct drm_encoder *encoder = &intel_encoder->base;
  8908. struct drm_atomic_state *state = old->restore_state;
  8909. int ret;
  8910. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8911. connector->base.id, connector->name,
  8912. encoder->base.id, encoder->name);
  8913. if (!state)
  8914. return;
  8915. ret = drm_atomic_commit(state);
  8916. if (ret) {
  8917. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8918. drm_atomic_state_free(state);
  8919. }
  8920. }
  8921. static int i9xx_pll_refclk(struct drm_device *dev,
  8922. const struct intel_crtc_state *pipe_config)
  8923. {
  8924. struct drm_i915_private *dev_priv = dev->dev_private;
  8925. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8926. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8927. return dev_priv->vbt.lvds_ssc_freq;
  8928. else if (HAS_PCH_SPLIT(dev))
  8929. return 120000;
  8930. else if (!IS_GEN2(dev))
  8931. return 96000;
  8932. else
  8933. return 48000;
  8934. }
  8935. /* Returns the clock of the currently programmed mode of the given pipe. */
  8936. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8937. struct intel_crtc_state *pipe_config)
  8938. {
  8939. struct drm_device *dev = crtc->base.dev;
  8940. struct drm_i915_private *dev_priv = dev->dev_private;
  8941. int pipe = pipe_config->cpu_transcoder;
  8942. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8943. u32 fp;
  8944. struct dpll clock;
  8945. int port_clock;
  8946. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8947. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8948. fp = pipe_config->dpll_hw_state.fp0;
  8949. else
  8950. fp = pipe_config->dpll_hw_state.fp1;
  8951. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8952. if (IS_PINEVIEW(dev)) {
  8953. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8954. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8955. } else {
  8956. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8957. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8958. }
  8959. if (!IS_GEN2(dev)) {
  8960. if (IS_PINEVIEW(dev))
  8961. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8962. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8963. else
  8964. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8965. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8966. switch (dpll & DPLL_MODE_MASK) {
  8967. case DPLLB_MODE_DAC_SERIAL:
  8968. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8969. 5 : 10;
  8970. break;
  8971. case DPLLB_MODE_LVDS:
  8972. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8973. 7 : 14;
  8974. break;
  8975. default:
  8976. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8977. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8978. return;
  8979. }
  8980. if (IS_PINEVIEW(dev))
  8981. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8982. else
  8983. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8984. } else {
  8985. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8986. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8987. if (is_lvds) {
  8988. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8989. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8990. if (lvds & LVDS_CLKB_POWER_UP)
  8991. clock.p2 = 7;
  8992. else
  8993. clock.p2 = 14;
  8994. } else {
  8995. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8996. clock.p1 = 2;
  8997. else {
  8998. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8999. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9000. }
  9001. if (dpll & PLL_P2_DIVIDE_BY_4)
  9002. clock.p2 = 4;
  9003. else
  9004. clock.p2 = 2;
  9005. }
  9006. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9007. }
  9008. /*
  9009. * This value includes pixel_multiplier. We will use
  9010. * port_clock to compute adjusted_mode.crtc_clock in the
  9011. * encoder's get_config() function.
  9012. */
  9013. pipe_config->port_clock = port_clock;
  9014. }
  9015. int intel_dotclock_calculate(int link_freq,
  9016. const struct intel_link_m_n *m_n)
  9017. {
  9018. /*
  9019. * The calculation for the data clock is:
  9020. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9021. * But we want to avoid losing precison if possible, so:
  9022. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9023. *
  9024. * and the link clock is simpler:
  9025. * link_clock = (m * link_clock) / n
  9026. */
  9027. if (!m_n->link_n)
  9028. return 0;
  9029. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9030. }
  9031. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9032. struct intel_crtc_state *pipe_config)
  9033. {
  9034. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9035. /* read out port_clock from the DPLL */
  9036. i9xx_crtc_clock_get(crtc, pipe_config);
  9037. /*
  9038. * In case there is an active pipe without active ports,
  9039. * we may need some idea for the dotclock anyway.
  9040. * Calculate one based on the FDI configuration.
  9041. */
  9042. pipe_config->base.adjusted_mode.crtc_clock =
  9043. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9044. &pipe_config->fdi_m_n);
  9045. }
  9046. /** Returns the currently programmed mode of the given pipe. */
  9047. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9048. struct drm_crtc *crtc)
  9049. {
  9050. struct drm_i915_private *dev_priv = dev->dev_private;
  9051. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9052. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9053. struct drm_display_mode *mode;
  9054. struct intel_crtc_state *pipe_config;
  9055. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9056. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9057. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9058. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9059. enum pipe pipe = intel_crtc->pipe;
  9060. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9061. if (!mode)
  9062. return NULL;
  9063. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9064. if (!pipe_config) {
  9065. kfree(mode);
  9066. return NULL;
  9067. }
  9068. /*
  9069. * Construct a pipe_config sufficient for getting the clock info
  9070. * back out of crtc_clock_get.
  9071. *
  9072. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9073. * to use a real value here instead.
  9074. */
  9075. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9076. pipe_config->pixel_multiplier = 1;
  9077. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9078. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9079. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9080. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9081. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9082. mode->hdisplay = (htot & 0xffff) + 1;
  9083. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9084. mode->hsync_start = (hsync & 0xffff) + 1;
  9085. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9086. mode->vdisplay = (vtot & 0xffff) + 1;
  9087. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9088. mode->vsync_start = (vsync & 0xffff) + 1;
  9089. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9090. drm_mode_set_name(mode);
  9091. kfree(pipe_config);
  9092. return mode;
  9093. }
  9094. void intel_mark_busy(struct drm_i915_private *dev_priv)
  9095. {
  9096. if (dev_priv->mm.busy)
  9097. return;
  9098. intel_runtime_pm_get(dev_priv);
  9099. i915_update_gfx_val(dev_priv);
  9100. if (INTEL_GEN(dev_priv) >= 6)
  9101. gen6_rps_busy(dev_priv);
  9102. dev_priv->mm.busy = true;
  9103. }
  9104. void intel_mark_idle(struct drm_i915_private *dev_priv)
  9105. {
  9106. if (!dev_priv->mm.busy)
  9107. return;
  9108. dev_priv->mm.busy = false;
  9109. if (INTEL_GEN(dev_priv) >= 6)
  9110. gen6_rps_idle(dev_priv);
  9111. intel_runtime_pm_put(dev_priv);
  9112. }
  9113. void intel_free_flip_work(struct intel_flip_work *work)
  9114. {
  9115. kfree(work->old_connector_state);
  9116. kfree(work->new_connector_state);
  9117. kfree(work);
  9118. }
  9119. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9120. {
  9121. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9122. struct drm_device *dev = crtc->dev;
  9123. struct intel_flip_work *work;
  9124. spin_lock_irq(&dev->event_lock);
  9125. while (!list_empty(&intel_crtc->flip_work)) {
  9126. work = list_first_entry(&intel_crtc->flip_work,
  9127. struct intel_flip_work, head);
  9128. list_del_init(&work->head);
  9129. spin_unlock_irq(&dev->event_lock);
  9130. cancel_work_sync(&work->mmio_work);
  9131. cancel_work_sync(&work->unpin_work);
  9132. intel_free_flip_work(work);
  9133. spin_lock_irq(&dev->event_lock);
  9134. }
  9135. spin_unlock_irq(&dev->event_lock);
  9136. drm_crtc_cleanup(crtc);
  9137. kfree(intel_crtc);
  9138. }
  9139. static void intel_crtc_post_flip_update(struct intel_flip_work *work,
  9140. struct drm_crtc *crtc)
  9141. {
  9142. struct intel_crtc_state *crtc_state = work->new_crtc_state;
  9143. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9144. if (crtc_state->disable_cxsr)
  9145. intel_crtc->wm.cxsr_allowed = true;
  9146. if (crtc_state->update_wm_post && crtc_state->base.active)
  9147. intel_update_watermarks(crtc);
  9148. if (work->num_planes > 0 &&
  9149. work->old_plane_state[0]->base.plane == crtc->primary) {
  9150. struct intel_plane_state *plane_state =
  9151. work->new_plane_state[0];
  9152. if (plane_state->visible &&
  9153. (needs_modeset(&crtc_state->base) ||
  9154. !work->old_plane_state[0]->visible))
  9155. intel_post_enable_primary(crtc);
  9156. }
  9157. }
  9158. static void intel_unpin_work_fn(struct work_struct *__work)
  9159. {
  9160. struct intel_flip_work *work =
  9161. container_of(__work, struct intel_flip_work, unpin_work);
  9162. struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
  9163. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9164. struct drm_device *dev = crtc->dev;
  9165. struct drm_i915_private *dev_priv = dev->dev_private;
  9166. int i;
  9167. if (work->fb_bits)
  9168. intel_frontbuffer_flip_complete(dev, work->fb_bits);
  9169. /*
  9170. * Unless work->can_async_unpin is false, there's no way to ensure
  9171. * that work->new_crtc_state contains valid memory during unpin
  9172. * because intel_atomic_commit may free it before this runs.
  9173. */
  9174. if (!work->can_async_unpin) {
  9175. intel_crtc_post_flip_update(work, crtc);
  9176. if (dev_priv->display.optimize_watermarks)
  9177. dev_priv->display.optimize_watermarks(work->new_crtc_state);
  9178. }
  9179. if (work->fb_bits & to_intel_plane(crtc->primary)->frontbuffer_bit)
  9180. intel_fbc_post_update(intel_crtc);
  9181. if (work->put_power_domains)
  9182. modeset_put_power_domains(dev_priv, work->put_power_domains);
  9183. /* Make sure mmio work is completely finished before freeing all state here. */
  9184. flush_work(&work->mmio_work);
  9185. if (!work->can_async_unpin &&
  9186. (work->new_crtc_state->update_pipe ||
  9187. needs_modeset(&work->new_crtc_state->base))) {
  9188. /* This must be called before work is unpinned for serialization. */
  9189. intel_modeset_verify_crtc(crtc, &work->old_crtc_state->base,
  9190. &work->new_crtc_state->base);
  9191. for (i = 0; i < work->num_new_connectors; i++) {
  9192. struct drm_connector_state *conn_state =
  9193. work->new_connector_state[i];
  9194. struct drm_connector *con = conn_state->connector;
  9195. WARN_ON(!con);
  9196. intel_connector_verify_state(to_intel_connector(con),
  9197. conn_state);
  9198. }
  9199. }
  9200. for (i = 0; i < work->num_old_connectors; i++) {
  9201. struct drm_connector_state *old_con_state =
  9202. work->old_connector_state[i];
  9203. struct drm_connector *con =
  9204. old_con_state->connector;
  9205. con->funcs->atomic_destroy_state(con, old_con_state);
  9206. }
  9207. if (!work->can_async_unpin || !list_empty(&work->head)) {
  9208. spin_lock_irq(&dev->event_lock);
  9209. WARN(list_empty(&work->head) != work->can_async_unpin,
  9210. "[CRTC:%i] Pin work %p async %i with %i planes, active %i -> %i ms %i\n",
  9211. crtc->base.id, work, work->can_async_unpin, work->num_planes,
  9212. work->old_crtc_state->base.active, work->new_crtc_state->base.active,
  9213. needs_modeset(&work->new_crtc_state->base));
  9214. if (!list_empty(&work->head))
  9215. list_del(&work->head);
  9216. wake_up_all(&dev_priv->pending_flip_queue);
  9217. spin_unlock_irq(&dev->event_lock);
  9218. }
  9219. /* New crtc_state freed? */
  9220. if (work->free_new_crtc_state)
  9221. intel_crtc_destroy_state(crtc, &work->new_crtc_state->base);
  9222. intel_crtc_destroy_state(crtc, &work->old_crtc_state->base);
  9223. for (i = 0; i < work->num_planes; i++) {
  9224. struct intel_plane_state *old_plane_state =
  9225. work->old_plane_state[i];
  9226. struct drm_framebuffer *old_fb = old_plane_state->base.fb;
  9227. struct drm_plane *plane = old_plane_state->base.plane;
  9228. struct drm_i915_gem_request *req;
  9229. req = old_plane_state->wait_req;
  9230. old_plane_state->wait_req = NULL;
  9231. if (req)
  9232. i915_gem_request_unreference(req);
  9233. fence_put(old_plane_state->base.fence);
  9234. old_plane_state->base.fence = NULL;
  9235. if (old_fb &&
  9236. (plane->type != DRM_PLANE_TYPE_CURSOR ||
  9237. !INTEL_INFO(dev_priv)->cursor_needs_physical)) {
  9238. mutex_lock(&dev->struct_mutex);
  9239. intel_unpin_fb_obj(old_fb, old_plane_state->base.rotation);
  9240. mutex_unlock(&dev->struct_mutex);
  9241. }
  9242. intel_plane_destroy_state(plane, &old_plane_state->base);
  9243. }
  9244. if (!WARN_ON(atomic_read(&intel_crtc->unpin_work_count) == 0))
  9245. atomic_dec(&intel_crtc->unpin_work_count);
  9246. intel_free_flip_work(work);
  9247. }
  9248. static bool pageflip_finished(struct intel_crtc *crtc,
  9249. struct intel_flip_work *work)
  9250. {
  9251. if (!atomic_read(&work->pending))
  9252. return false;
  9253. smp_rmb();
  9254. /*
  9255. * MMIO work completes when vblank is different from
  9256. * flip_queued_vblank.
  9257. */
  9258. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9259. }
  9260. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9261. {
  9262. struct drm_device *dev = dev_priv->dev;
  9263. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9265. struct intel_flip_work *work;
  9266. unsigned long flags;
  9267. /* Ignore early vblank irqs */
  9268. if (!crtc)
  9269. return;
  9270. /*
  9271. * This is called both by irq handlers and the reset code (to complete
  9272. * lost pageflips) so needs the full irqsave spinlocks.
  9273. */
  9274. spin_lock_irqsave(&dev->event_lock, flags);
  9275. while (!list_empty(&intel_crtc->flip_work)) {
  9276. work = list_first_entry(&intel_crtc->flip_work,
  9277. struct intel_flip_work,
  9278. head);
  9279. if (!pageflip_finished(intel_crtc, work) ||
  9280. work_busy(&work->unpin_work))
  9281. break;
  9282. page_flip_completed(intel_crtc, work);
  9283. }
  9284. spin_unlock_irqrestore(&dev->event_lock, flags);
  9285. }
  9286. static void intel_mmio_flip_work_func(struct work_struct *w)
  9287. {
  9288. struct intel_flip_work *work =
  9289. container_of(w, struct intel_flip_work, mmio_work);
  9290. struct drm_crtc *crtc = work->old_crtc_state->base.crtc;
  9291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9292. struct intel_crtc_state *crtc_state = work->new_crtc_state;
  9293. struct drm_device *dev = crtc->dev;
  9294. struct drm_i915_private *dev_priv = dev->dev_private;
  9295. struct drm_i915_gem_request *req;
  9296. int i, ret;
  9297. if (!needs_modeset(&crtc_state->base) && crtc_state->update_pipe) {
  9298. work->put_power_domains =
  9299. modeset_get_crtc_power_domains(crtc, crtc_state);
  9300. }
  9301. for (i = 0; i < work->num_planes; i++) {
  9302. struct intel_plane_state *old_plane_state = work->old_plane_state[i];
  9303. /* For framebuffer backed by dmabuf, wait for fence */
  9304. if (old_plane_state->base.fence)
  9305. WARN_ON(fence_wait(old_plane_state->base.fence, false) < 0);
  9306. req = old_plane_state->wait_req;
  9307. if (!req)
  9308. continue;
  9309. WARN_ON(__i915_wait_request(req, false, NULL,
  9310. &dev_priv->rps.mmioflips));
  9311. }
  9312. ret = drm_crtc_vblank_get(crtc);
  9313. I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
  9314. if (work->num_planes &&
  9315. work->old_plane_state[0]->base.plane == crtc->primary)
  9316. intel_fbc_enable(intel_crtc, work->new_crtc_state, work->new_plane_state[0]);
  9317. intel_frontbuffer_flip_prepare(dev, work->fb_bits);
  9318. intel_pipe_update_start(intel_crtc);
  9319. if (!needs_modeset(&crtc_state->base)) {
  9320. if (crtc_state->base.color_mgmt_changed || crtc_state->update_pipe) {
  9321. intel_color_set_csc(&crtc_state->base);
  9322. intel_color_load_luts(&crtc_state->base);
  9323. }
  9324. if (crtc_state->update_pipe)
  9325. intel_update_pipe_config(intel_crtc, work->old_crtc_state);
  9326. else if (INTEL_INFO(dev)->gen >= 9)
  9327. skl_detach_scalers(intel_crtc);
  9328. }
  9329. for (i = 0; i < work->num_planes; i++) {
  9330. struct intel_plane_state *new_plane_state = work->new_plane_state[i];
  9331. struct intel_plane *plane = to_intel_plane(new_plane_state->base.plane);
  9332. if (new_plane_state->visible)
  9333. plane->update_plane(&plane->base, crtc_state, new_plane_state);
  9334. else
  9335. plane->disable_plane(&plane->base, crtc);
  9336. }
  9337. intel_pipe_update_end(intel_crtc, work);
  9338. }
  9339. /**
  9340. * intel_wm_need_update - Check whether watermarks need updating
  9341. * @plane: drm plane
  9342. * @state: new plane state
  9343. *
  9344. * Check current plane state versus the new one to determine whether
  9345. * watermarks need to be recalculated.
  9346. *
  9347. * Returns true or false.
  9348. */
  9349. static bool intel_wm_need_update(struct drm_plane *plane,
  9350. struct drm_plane_state *state)
  9351. {
  9352. struct intel_plane_state *new = to_intel_plane_state(state);
  9353. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9354. /* Update watermarks on tiling or size changes. */
  9355. if (new->visible != cur->visible)
  9356. return true;
  9357. if (!cur->base.fb || !new->base.fb)
  9358. return false;
  9359. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  9360. cur->base.rotation != new->base.rotation ||
  9361. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9362. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9363. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9364. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9365. return true;
  9366. return false;
  9367. }
  9368. static bool needs_scaling(struct intel_plane_state *state)
  9369. {
  9370. int src_w = drm_rect_width(&state->src) >> 16;
  9371. int src_h = drm_rect_height(&state->src) >> 16;
  9372. int dst_w = drm_rect_width(&state->dst);
  9373. int dst_h = drm_rect_height(&state->dst);
  9374. return (src_w != dst_w || src_h != dst_h);
  9375. }
  9376. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9377. struct drm_plane_state *plane_state)
  9378. {
  9379. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9380. struct drm_crtc *crtc = crtc_state->crtc;
  9381. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9382. struct drm_plane *plane = plane_state->plane;
  9383. struct drm_device *dev = crtc->dev;
  9384. struct drm_i915_private *dev_priv = to_i915(dev);
  9385. struct intel_plane_state *old_plane_state =
  9386. to_intel_plane_state(plane->state);
  9387. int idx = intel_crtc->base.base.id, ret;
  9388. bool mode_changed = needs_modeset(crtc_state);
  9389. bool was_crtc_enabled = crtc->state->active;
  9390. bool is_crtc_enabled = crtc_state->active;
  9391. bool turn_off, turn_on, visible, was_visible;
  9392. struct drm_framebuffer *fb = plane_state->fb;
  9393. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9394. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9395. ret = skl_update_scaler_plane(
  9396. to_intel_crtc_state(crtc_state),
  9397. to_intel_plane_state(plane_state));
  9398. if (ret)
  9399. return ret;
  9400. }
  9401. was_visible = old_plane_state->visible;
  9402. visible = to_intel_plane_state(plane_state)->visible;
  9403. if (!was_crtc_enabled && WARN_ON(was_visible))
  9404. was_visible = false;
  9405. /*
  9406. * Visibility is calculated as if the crtc was on, but
  9407. * after scaler setup everything depends on it being off
  9408. * when the crtc isn't active.
  9409. *
  9410. * FIXME this is wrong for watermarks. Watermarks should also
  9411. * be computed as if the pipe would be active. Perhaps move
  9412. * per-plane wm computation to the .check_plane() hook, and
  9413. * only combine the results from all planes in the current place?
  9414. */
  9415. if (!is_crtc_enabled)
  9416. to_intel_plane_state(plane_state)->visible = visible = false;
  9417. if (!was_visible && !visible)
  9418. return 0;
  9419. if (fb != old_plane_state->base.fb)
  9420. pipe_config->fb_changed = true;
  9421. turn_off = was_visible && (!visible || mode_changed);
  9422. turn_on = visible && (!was_visible || mode_changed);
  9423. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9424. plane->base.id, fb ? fb->base.id : -1);
  9425. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9426. plane->base.id, was_visible, visible,
  9427. turn_off, turn_on, mode_changed);
  9428. if (turn_on) {
  9429. pipe_config->update_wm_pre = true;
  9430. /* must disable cxsr around plane enable/disable */
  9431. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9432. pipe_config->disable_cxsr = true;
  9433. } else if (turn_off) {
  9434. pipe_config->update_wm_post = true;
  9435. /* must disable cxsr around plane enable/disable */
  9436. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9437. pipe_config->disable_cxsr = true;
  9438. } else if (intel_wm_need_update(plane, plane_state)) {
  9439. /* FIXME bollocks */
  9440. pipe_config->update_wm_pre = true;
  9441. pipe_config->update_wm_post = true;
  9442. }
  9443. /* Pre-gen9 platforms need two-step watermark updates */
  9444. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9445. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  9446. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  9447. if (visible || was_visible)
  9448. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  9449. /*
  9450. * WaCxSRDisabledForSpriteScaling:ivb
  9451. *
  9452. * cstate->update_wm was already set above, so this flag will
  9453. * take effect when we commit and program watermarks.
  9454. */
  9455. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev) &&
  9456. needs_scaling(to_intel_plane_state(plane_state)) &&
  9457. !needs_scaling(old_plane_state))
  9458. pipe_config->disable_lp_wm = true;
  9459. return 0;
  9460. }
  9461. static bool encoders_cloneable(const struct intel_encoder *a,
  9462. const struct intel_encoder *b)
  9463. {
  9464. /* masks could be asymmetric, so check both ways */
  9465. return a == b || (a->cloneable & (1 << b->type) &&
  9466. b->cloneable & (1 << a->type));
  9467. }
  9468. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9469. struct intel_crtc *crtc,
  9470. struct intel_encoder *encoder)
  9471. {
  9472. struct intel_encoder *source_encoder;
  9473. struct drm_connector *connector;
  9474. struct drm_connector_state *connector_state;
  9475. int i;
  9476. for_each_connector_in_state(state, connector, connector_state, i) {
  9477. if (connector_state->crtc != &crtc->base)
  9478. continue;
  9479. source_encoder =
  9480. to_intel_encoder(connector_state->best_encoder);
  9481. if (!encoders_cloneable(encoder, source_encoder))
  9482. return false;
  9483. }
  9484. return true;
  9485. }
  9486. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9487. struct intel_crtc *crtc)
  9488. {
  9489. struct intel_encoder *encoder;
  9490. struct drm_connector *connector;
  9491. struct drm_connector_state *connector_state;
  9492. int i;
  9493. for_each_connector_in_state(state, connector, connector_state, i) {
  9494. if (connector_state->crtc != &crtc->base)
  9495. continue;
  9496. encoder = to_intel_encoder(connector_state->best_encoder);
  9497. if (!check_single_encoder_cloning(state, crtc, encoder))
  9498. return false;
  9499. }
  9500. return true;
  9501. }
  9502. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9503. struct drm_crtc_state *crtc_state)
  9504. {
  9505. struct drm_device *dev = crtc->dev;
  9506. struct drm_i915_private *dev_priv = dev->dev_private;
  9507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9508. struct intel_crtc_state *pipe_config =
  9509. to_intel_crtc_state(crtc_state);
  9510. struct drm_atomic_state *state = crtc_state->state;
  9511. int ret;
  9512. bool mode_changed = needs_modeset(crtc_state);
  9513. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9514. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9515. return -EINVAL;
  9516. }
  9517. if (mode_changed && !crtc_state->active)
  9518. pipe_config->update_wm_post = true;
  9519. if (mode_changed && crtc_state->enable &&
  9520. dev_priv->display.crtc_compute_clock &&
  9521. !WARN_ON(pipe_config->shared_dpll)) {
  9522. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9523. pipe_config);
  9524. if (ret)
  9525. return ret;
  9526. }
  9527. if (crtc_state->color_mgmt_changed) {
  9528. ret = intel_color_check(crtc, crtc_state);
  9529. if (ret)
  9530. return ret;
  9531. }
  9532. ret = 0;
  9533. if (dev_priv->display.compute_pipe_wm) {
  9534. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9535. if (ret) {
  9536. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9537. return ret;
  9538. }
  9539. }
  9540. if (dev_priv->display.compute_intermediate_wm &&
  9541. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9542. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9543. return 0;
  9544. /*
  9545. * Calculate 'intermediate' watermarks that satisfy both the
  9546. * old state and the new state. We can program these
  9547. * immediately.
  9548. */
  9549. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  9550. intel_crtc,
  9551. pipe_config);
  9552. if (ret) {
  9553. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9554. return ret;
  9555. }
  9556. } else if (dev_priv->display.compute_intermediate_wm) {
  9557. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9558. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9559. }
  9560. if (INTEL_INFO(dev)->gen >= 9) {
  9561. if (mode_changed)
  9562. ret = skl_update_scaler_crtc(pipe_config);
  9563. if (!ret)
  9564. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9565. pipe_config);
  9566. }
  9567. return ret;
  9568. }
  9569. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9570. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9571. .atomic_check = intel_crtc_atomic_check,
  9572. };
  9573. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9574. {
  9575. struct intel_connector *connector;
  9576. for_each_intel_connector(dev, connector) {
  9577. if (connector->base.state->crtc)
  9578. drm_connector_unreference(&connector->base);
  9579. if (connector->base.encoder) {
  9580. connector->base.state->best_encoder =
  9581. connector->base.encoder;
  9582. connector->base.state->crtc =
  9583. connector->base.encoder->crtc;
  9584. drm_connector_reference(&connector->base);
  9585. } else {
  9586. connector->base.state->best_encoder = NULL;
  9587. connector->base.state->crtc = NULL;
  9588. }
  9589. }
  9590. }
  9591. static void
  9592. connected_sink_compute_bpp(struct intel_connector *connector,
  9593. struct intel_crtc_state *pipe_config)
  9594. {
  9595. int bpp = pipe_config->pipe_bpp;
  9596. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9597. connector->base.base.id,
  9598. connector->base.name);
  9599. /* Don't use an invalid EDID bpc value */
  9600. if (connector->base.display_info.bpc &&
  9601. connector->base.display_info.bpc * 3 < bpp) {
  9602. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9603. bpp, connector->base.display_info.bpc*3);
  9604. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  9605. }
  9606. /* Clamp bpp to default limit on screens without EDID 1.4 */
  9607. if (connector->base.display_info.bpc == 0) {
  9608. int type = connector->base.connector_type;
  9609. int clamp_bpp = 24;
  9610. /* Fall back to 18 bpp when DP sink capability is unknown. */
  9611. if (type == DRM_MODE_CONNECTOR_DisplayPort ||
  9612. type == DRM_MODE_CONNECTOR_eDP)
  9613. clamp_bpp = 18;
  9614. if (bpp > clamp_bpp) {
  9615. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of %d\n",
  9616. bpp, clamp_bpp);
  9617. pipe_config->pipe_bpp = clamp_bpp;
  9618. }
  9619. }
  9620. }
  9621. static int
  9622. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9623. struct intel_crtc_state *pipe_config)
  9624. {
  9625. struct drm_device *dev = crtc->base.dev;
  9626. struct drm_atomic_state *state;
  9627. struct drm_connector *connector;
  9628. struct drm_connector_state *connector_state;
  9629. int bpp, i;
  9630. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)))
  9631. bpp = 10*3;
  9632. else if (INTEL_INFO(dev)->gen >= 5)
  9633. bpp = 12*3;
  9634. else
  9635. bpp = 8*3;
  9636. pipe_config->pipe_bpp = bpp;
  9637. state = pipe_config->base.state;
  9638. /* Clamp display bpp to EDID value */
  9639. for_each_connector_in_state(state, connector, connector_state, i) {
  9640. if (connector_state->crtc != &crtc->base)
  9641. continue;
  9642. connected_sink_compute_bpp(to_intel_connector(connector),
  9643. pipe_config);
  9644. }
  9645. return bpp;
  9646. }
  9647. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9648. {
  9649. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9650. "type: 0x%x flags: 0x%x\n",
  9651. mode->crtc_clock,
  9652. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9653. mode->crtc_hsync_end, mode->crtc_htotal,
  9654. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9655. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9656. }
  9657. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9658. struct intel_crtc_state *pipe_config,
  9659. const char *context)
  9660. {
  9661. struct drm_device *dev = crtc->base.dev;
  9662. struct drm_plane *plane;
  9663. struct intel_plane *intel_plane;
  9664. struct intel_plane_state *state;
  9665. struct drm_framebuffer *fb;
  9666. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  9667. context, pipe_config, pipe_name(crtc->pipe));
  9668. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  9669. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  9670. pipe_config->pipe_bpp, pipe_config->dither);
  9671. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9672. pipe_config->has_pch_encoder,
  9673. pipe_config->fdi_lanes,
  9674. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  9675. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  9676. pipe_config->fdi_m_n.tu);
  9677. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9678. pipe_config->has_dp_encoder,
  9679. pipe_config->lane_count,
  9680. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  9681. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  9682. pipe_config->dp_m_n.tu);
  9683. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  9684. pipe_config->has_dp_encoder,
  9685. pipe_config->lane_count,
  9686. pipe_config->dp_m2_n2.gmch_m,
  9687. pipe_config->dp_m2_n2.gmch_n,
  9688. pipe_config->dp_m2_n2.link_m,
  9689. pipe_config->dp_m2_n2.link_n,
  9690. pipe_config->dp_m2_n2.tu);
  9691. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9692. pipe_config->has_audio,
  9693. pipe_config->has_infoframe);
  9694. DRM_DEBUG_KMS("requested mode:\n");
  9695. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9696. DRM_DEBUG_KMS("adjusted mode:\n");
  9697. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9698. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9699. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  9700. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  9701. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  9702. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9703. crtc->num_scalers,
  9704. pipe_config->scaler_state.scaler_users,
  9705. pipe_config->scaler_state.scaler_id);
  9706. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9707. pipe_config->gmch_pfit.control,
  9708. pipe_config->gmch_pfit.pgm_ratios,
  9709. pipe_config->gmch_pfit.lvds_border_bits);
  9710. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9711. pipe_config->pch_pfit.pos,
  9712. pipe_config->pch_pfit.size,
  9713. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  9714. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  9715. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  9716. if (IS_BROXTON(dev)) {
  9717. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  9718. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  9719. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  9720. pipe_config->ddi_pll_sel,
  9721. pipe_config->dpll_hw_state.ebb0,
  9722. pipe_config->dpll_hw_state.ebb4,
  9723. pipe_config->dpll_hw_state.pll0,
  9724. pipe_config->dpll_hw_state.pll1,
  9725. pipe_config->dpll_hw_state.pll2,
  9726. pipe_config->dpll_hw_state.pll3,
  9727. pipe_config->dpll_hw_state.pll6,
  9728. pipe_config->dpll_hw_state.pll8,
  9729. pipe_config->dpll_hw_state.pll9,
  9730. pipe_config->dpll_hw_state.pll10,
  9731. pipe_config->dpll_hw_state.pcsdw12);
  9732. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  9733. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  9734. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  9735. pipe_config->ddi_pll_sel,
  9736. pipe_config->dpll_hw_state.ctrl1,
  9737. pipe_config->dpll_hw_state.cfgcr1,
  9738. pipe_config->dpll_hw_state.cfgcr2);
  9739. } else if (HAS_DDI(dev)) {
  9740. DRM_DEBUG_KMS("ddi_pll_sel: 0x%x; dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  9741. pipe_config->ddi_pll_sel,
  9742. pipe_config->dpll_hw_state.wrpll,
  9743. pipe_config->dpll_hw_state.spll);
  9744. } else {
  9745. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  9746. "fp0: 0x%x, fp1: 0x%x\n",
  9747. pipe_config->dpll_hw_state.dpll,
  9748. pipe_config->dpll_hw_state.dpll_md,
  9749. pipe_config->dpll_hw_state.fp0,
  9750. pipe_config->dpll_hw_state.fp1);
  9751. }
  9752. DRM_DEBUG_KMS("planes on this crtc\n");
  9753. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9754. intel_plane = to_intel_plane(plane);
  9755. if (intel_plane->pipe != crtc->pipe)
  9756. continue;
  9757. state = to_intel_plane_state(plane->state);
  9758. fb = state->base.fb;
  9759. if (!fb) {
  9760. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  9761. "disabled, scaler_id = %d\n",
  9762. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9763. plane->base.id, intel_plane->pipe,
  9764. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  9765. drm_plane_index(plane), state->scaler_id);
  9766. continue;
  9767. }
  9768. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  9769. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  9770. plane->base.id, intel_plane->pipe,
  9771. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  9772. drm_plane_index(plane));
  9773. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  9774. fb->base.id, fb->width, fb->height, fb->pixel_format);
  9775. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  9776. state->scaler_id,
  9777. state->src.x1 >> 16, state->src.y1 >> 16,
  9778. drm_rect_width(&state->src) >> 16,
  9779. drm_rect_height(&state->src) >> 16,
  9780. state->dst.x1, state->dst.y1,
  9781. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  9782. }
  9783. }
  9784. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9785. {
  9786. struct drm_device *dev = state->dev;
  9787. struct drm_connector *connector;
  9788. unsigned int used_ports = 0;
  9789. /*
  9790. * Walk the connector list instead of the encoder
  9791. * list to detect the problem on ddi platforms
  9792. * where there's just one encoder per digital port.
  9793. */
  9794. drm_for_each_connector(connector, dev) {
  9795. struct drm_connector_state *connector_state;
  9796. struct intel_encoder *encoder;
  9797. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9798. if (!connector_state)
  9799. connector_state = connector->state;
  9800. if (!connector_state->best_encoder)
  9801. continue;
  9802. encoder = to_intel_encoder(connector_state->best_encoder);
  9803. WARN_ON(!connector_state->crtc);
  9804. switch (encoder->type) {
  9805. unsigned int port_mask;
  9806. case INTEL_OUTPUT_UNKNOWN:
  9807. if (WARN_ON(!HAS_DDI(dev)))
  9808. break;
  9809. case INTEL_OUTPUT_DISPLAYPORT:
  9810. case INTEL_OUTPUT_HDMI:
  9811. case INTEL_OUTPUT_EDP:
  9812. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9813. /* the same port mustn't appear more than once */
  9814. if (used_ports & port_mask)
  9815. return false;
  9816. used_ports |= port_mask;
  9817. default:
  9818. break;
  9819. }
  9820. }
  9821. return true;
  9822. }
  9823. static void
  9824. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9825. {
  9826. struct drm_crtc_state tmp_state;
  9827. struct intel_crtc_scaler_state scaler_state;
  9828. struct intel_dpll_hw_state dpll_hw_state;
  9829. struct intel_shared_dpll *shared_dpll;
  9830. uint32_t ddi_pll_sel;
  9831. bool force_thru;
  9832. /* FIXME: before the switch to atomic started, a new pipe_config was
  9833. * kzalloc'd. Code that depends on any field being zero should be
  9834. * fixed, so that the crtc_state can be safely duplicated. For now,
  9835. * only fields that are know to not cause problems are preserved. */
  9836. tmp_state = crtc_state->base;
  9837. scaler_state = crtc_state->scaler_state;
  9838. shared_dpll = crtc_state->shared_dpll;
  9839. dpll_hw_state = crtc_state->dpll_hw_state;
  9840. ddi_pll_sel = crtc_state->ddi_pll_sel;
  9841. force_thru = crtc_state->pch_pfit.force_thru;
  9842. memset(crtc_state, 0, sizeof *crtc_state);
  9843. crtc_state->base = tmp_state;
  9844. crtc_state->scaler_state = scaler_state;
  9845. crtc_state->shared_dpll = shared_dpll;
  9846. crtc_state->dpll_hw_state = dpll_hw_state;
  9847. crtc_state->ddi_pll_sel = ddi_pll_sel;
  9848. crtc_state->pch_pfit.force_thru = force_thru;
  9849. }
  9850. static int
  9851. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9852. struct intel_crtc_state *pipe_config)
  9853. {
  9854. struct drm_atomic_state *state = pipe_config->base.state;
  9855. struct intel_encoder *encoder;
  9856. struct drm_connector *connector;
  9857. struct drm_connector_state *connector_state;
  9858. int base_bpp, ret = -EINVAL;
  9859. int i;
  9860. bool retry = true;
  9861. clear_intel_crtc_state(pipe_config);
  9862. pipe_config->cpu_transcoder =
  9863. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9864. /*
  9865. * Sanitize sync polarity flags based on requested ones. If neither
  9866. * positive or negative polarity is requested, treat this as meaning
  9867. * negative polarity.
  9868. */
  9869. if (!(pipe_config->base.adjusted_mode.flags &
  9870. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9871. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9872. if (!(pipe_config->base.adjusted_mode.flags &
  9873. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9874. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9875. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9876. pipe_config);
  9877. if (base_bpp < 0)
  9878. goto fail;
  9879. /*
  9880. * Determine the real pipe dimensions. Note that stereo modes can
  9881. * increase the actual pipe size due to the frame doubling and
  9882. * insertion of additional space for blanks between the frame. This
  9883. * is stored in the crtc timings. We use the requested mode to do this
  9884. * computation to clearly distinguish it from the adjusted mode, which
  9885. * can be changed by the connectors in the below retry loop.
  9886. */
  9887. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  9888. &pipe_config->pipe_src_w,
  9889. &pipe_config->pipe_src_h);
  9890. encoder_retry:
  9891. /* Ensure the port clock defaults are reset when retrying. */
  9892. pipe_config->port_clock = 0;
  9893. pipe_config->pixel_multiplier = 1;
  9894. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9895. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9896. CRTC_STEREO_DOUBLE);
  9897. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9898. * adjust it according to limitations or connector properties, and also
  9899. * a chance to reject the mode entirely.
  9900. */
  9901. for_each_connector_in_state(state, connector, connector_state, i) {
  9902. if (connector_state->crtc != crtc)
  9903. continue;
  9904. encoder = to_intel_encoder(connector_state->best_encoder);
  9905. if (!(encoder->compute_config(encoder, pipe_config))) {
  9906. DRM_DEBUG_KMS("Encoder config failure\n");
  9907. goto fail;
  9908. }
  9909. }
  9910. /* Set default port clock if not overwritten by the encoder. Needs to be
  9911. * done afterwards in case the encoder adjusts the mode. */
  9912. if (!pipe_config->port_clock)
  9913. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9914. * pipe_config->pixel_multiplier;
  9915. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9916. if (ret < 0) {
  9917. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9918. goto fail;
  9919. }
  9920. if (ret == RETRY) {
  9921. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9922. ret = -EINVAL;
  9923. goto fail;
  9924. }
  9925. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9926. retry = false;
  9927. goto encoder_retry;
  9928. }
  9929. /* Dithering seems to not pass-through bits correctly when it should, so
  9930. * only enable it on 6bpc panels. */
  9931. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  9932. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9933. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9934. fail:
  9935. return ret;
  9936. }
  9937. static void
  9938. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9939. {
  9940. struct drm_crtc *crtc;
  9941. struct drm_crtc_state *crtc_state;
  9942. int i;
  9943. /* Double check state. */
  9944. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  9945. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  9946. /* Update hwmode for vblank functions */
  9947. if (crtc->state->active)
  9948. crtc->hwmode = crtc->state->adjusted_mode;
  9949. else
  9950. crtc->hwmode.crtc_clock = 0;
  9951. /*
  9952. * Update legacy state to satisfy fbc code. This can
  9953. * be removed when fbc uses the atomic state.
  9954. */
  9955. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9956. struct drm_plane_state *plane_state = crtc->primary->state;
  9957. crtc->primary->fb = plane_state->fb;
  9958. crtc->x = plane_state->src_x >> 16;
  9959. crtc->y = plane_state->src_y >> 16;
  9960. }
  9961. }
  9962. }
  9963. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9964. {
  9965. int diff;
  9966. if (clock1 == clock2)
  9967. return true;
  9968. if (!clock1 || !clock2)
  9969. return false;
  9970. diff = abs(clock1 - clock2);
  9971. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9972. return true;
  9973. return false;
  9974. }
  9975. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  9976. list_for_each_entry((intel_crtc), \
  9977. &(dev)->mode_config.crtc_list, \
  9978. base.head) \
  9979. for_each_if (mask & (1 <<(intel_crtc)->pipe))
  9980. static bool
  9981. intel_compare_m_n(unsigned int m, unsigned int n,
  9982. unsigned int m2, unsigned int n2,
  9983. bool exact)
  9984. {
  9985. if (m == m2 && n == n2)
  9986. return true;
  9987. if (exact || !m || !n || !m2 || !n2)
  9988. return false;
  9989. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9990. if (n > n2) {
  9991. while (n > n2) {
  9992. m2 <<= 1;
  9993. n2 <<= 1;
  9994. }
  9995. } else if (n < n2) {
  9996. while (n < n2) {
  9997. m <<= 1;
  9998. n <<= 1;
  9999. }
  10000. }
  10001. if (n != n2)
  10002. return false;
  10003. return intel_fuzzy_clock_check(m, m2);
  10004. }
  10005. static bool
  10006. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10007. struct intel_link_m_n *m2_n2,
  10008. bool adjust)
  10009. {
  10010. if (m_n->tu == m2_n2->tu &&
  10011. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10012. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10013. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10014. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10015. if (adjust)
  10016. *m2_n2 = *m_n;
  10017. return true;
  10018. }
  10019. return false;
  10020. }
  10021. static bool
  10022. intel_pipe_config_compare(struct drm_device *dev,
  10023. struct intel_crtc_state *current_config,
  10024. struct intel_crtc_state *pipe_config,
  10025. bool adjust)
  10026. {
  10027. bool ret = true;
  10028. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10029. do { \
  10030. if (!adjust) \
  10031. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10032. else \
  10033. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10034. } while (0)
  10035. #define PIPE_CONF_CHECK_X(name) \
  10036. if (current_config->name != pipe_config->name) { \
  10037. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10038. "(expected 0x%08x, found 0x%08x)\n", \
  10039. current_config->name, \
  10040. pipe_config->name); \
  10041. ret = false; \
  10042. }
  10043. #define PIPE_CONF_CHECK_I(name) \
  10044. if (current_config->name != pipe_config->name) { \
  10045. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10046. "(expected %i, found %i)\n", \
  10047. current_config->name, \
  10048. pipe_config->name); \
  10049. ret = false; \
  10050. }
  10051. #define PIPE_CONF_CHECK_P(name) \
  10052. if (current_config->name != pipe_config->name) { \
  10053. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10054. "(expected %p, found %p)\n", \
  10055. current_config->name, \
  10056. pipe_config->name); \
  10057. ret = false; \
  10058. }
  10059. #define PIPE_CONF_CHECK_M_N(name) \
  10060. if (!intel_compare_link_m_n(&current_config->name, \
  10061. &pipe_config->name,\
  10062. adjust)) { \
  10063. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10064. "(expected tu %i gmch %i/%i link %i/%i, " \
  10065. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10066. current_config->name.tu, \
  10067. current_config->name.gmch_m, \
  10068. current_config->name.gmch_n, \
  10069. current_config->name.link_m, \
  10070. current_config->name.link_n, \
  10071. pipe_config->name.tu, \
  10072. pipe_config->name.gmch_m, \
  10073. pipe_config->name.gmch_n, \
  10074. pipe_config->name.link_m, \
  10075. pipe_config->name.link_n); \
  10076. ret = false; \
  10077. }
  10078. /* This is required for BDW+ where there is only one set of registers for
  10079. * switching between high and low RR.
  10080. * This macro can be used whenever a comparison has to be made between one
  10081. * hw state and multiple sw state variables.
  10082. */
  10083. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10084. if (!intel_compare_link_m_n(&current_config->name, \
  10085. &pipe_config->name, adjust) && \
  10086. !intel_compare_link_m_n(&current_config->alt_name, \
  10087. &pipe_config->name, adjust)) { \
  10088. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10089. "(expected tu %i gmch %i/%i link %i/%i, " \
  10090. "or tu %i gmch %i/%i link %i/%i, " \
  10091. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10092. current_config->name.tu, \
  10093. current_config->name.gmch_m, \
  10094. current_config->name.gmch_n, \
  10095. current_config->name.link_m, \
  10096. current_config->name.link_n, \
  10097. current_config->alt_name.tu, \
  10098. current_config->alt_name.gmch_m, \
  10099. current_config->alt_name.gmch_n, \
  10100. current_config->alt_name.link_m, \
  10101. current_config->alt_name.link_n, \
  10102. pipe_config->name.tu, \
  10103. pipe_config->name.gmch_m, \
  10104. pipe_config->name.gmch_n, \
  10105. pipe_config->name.link_m, \
  10106. pipe_config->name.link_n); \
  10107. ret = false; \
  10108. }
  10109. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10110. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10111. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10112. "(expected %i, found %i)\n", \
  10113. current_config->name & (mask), \
  10114. pipe_config->name & (mask)); \
  10115. ret = false; \
  10116. }
  10117. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10118. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10119. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10120. "(expected %i, found %i)\n", \
  10121. current_config->name, \
  10122. pipe_config->name); \
  10123. ret = false; \
  10124. }
  10125. #define PIPE_CONF_QUIRK(quirk) \
  10126. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10127. PIPE_CONF_CHECK_I(cpu_transcoder);
  10128. PIPE_CONF_CHECK_I(has_pch_encoder);
  10129. PIPE_CONF_CHECK_I(fdi_lanes);
  10130. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10131. PIPE_CONF_CHECK_I(has_dp_encoder);
  10132. PIPE_CONF_CHECK_I(lane_count);
  10133. if (INTEL_INFO(dev)->gen < 8) {
  10134. PIPE_CONF_CHECK_M_N(dp_m_n);
  10135. if (current_config->has_drrs)
  10136. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10137. } else
  10138. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10139. PIPE_CONF_CHECK_I(has_dsi_encoder);
  10140. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10141. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10142. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10143. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10144. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10145. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10146. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10147. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10148. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10149. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10150. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10151. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10152. PIPE_CONF_CHECK_I(pixel_multiplier);
  10153. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10154. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10155. IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  10156. PIPE_CONF_CHECK_I(limited_color_range);
  10157. PIPE_CONF_CHECK_I(has_infoframe);
  10158. PIPE_CONF_CHECK_I(has_audio);
  10159. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10160. DRM_MODE_FLAG_INTERLACE);
  10161. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10162. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10163. DRM_MODE_FLAG_PHSYNC);
  10164. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10165. DRM_MODE_FLAG_NHSYNC);
  10166. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10167. DRM_MODE_FLAG_PVSYNC);
  10168. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10169. DRM_MODE_FLAG_NVSYNC);
  10170. }
  10171. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10172. /* pfit ratios are autocomputed by the hw on gen4+ */
  10173. if (INTEL_INFO(dev)->gen < 4)
  10174. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  10175. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10176. if (!adjust) {
  10177. PIPE_CONF_CHECK_I(pipe_src_w);
  10178. PIPE_CONF_CHECK_I(pipe_src_h);
  10179. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10180. if (current_config->pch_pfit.enabled) {
  10181. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10182. PIPE_CONF_CHECK_X(pch_pfit.size);
  10183. }
  10184. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10185. }
  10186. /* BDW+ don't expose a synchronous way to read the state */
  10187. if (IS_HASWELL(dev))
  10188. PIPE_CONF_CHECK_I(ips_enabled);
  10189. PIPE_CONF_CHECK_I(double_wide);
  10190. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10191. PIPE_CONF_CHECK_P(shared_dpll);
  10192. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10193. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10194. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10195. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10196. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10197. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  10198. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10199. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10200. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10201. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  10202. PIPE_CONF_CHECK_X(dsi_pll.div);
  10203. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10204. PIPE_CONF_CHECK_I(pipe_bpp);
  10205. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10206. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10207. #undef PIPE_CONF_CHECK_X
  10208. #undef PIPE_CONF_CHECK_I
  10209. #undef PIPE_CONF_CHECK_P
  10210. #undef PIPE_CONF_CHECK_FLAGS
  10211. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10212. #undef PIPE_CONF_QUIRK
  10213. #undef INTEL_ERR_OR_DBG_KMS
  10214. return ret;
  10215. }
  10216. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  10217. const struct intel_crtc_state *pipe_config)
  10218. {
  10219. if (pipe_config->has_pch_encoder) {
  10220. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  10221. &pipe_config->fdi_m_n);
  10222. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  10223. /*
  10224. * FDI already provided one idea for the dotclock.
  10225. * Yell if the encoder disagrees.
  10226. */
  10227. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  10228. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10229. fdi_dotclock, dotclock);
  10230. }
  10231. }
  10232. static void verify_wm_state(struct drm_crtc *crtc,
  10233. struct drm_crtc_state *new_state)
  10234. {
  10235. struct drm_device *dev = crtc->dev;
  10236. struct drm_i915_private *dev_priv = dev->dev_private;
  10237. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10238. struct skl_ddb_entry *hw_entry, *sw_entry;
  10239. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10240. const enum pipe pipe = intel_crtc->pipe;
  10241. int plane;
  10242. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  10243. return;
  10244. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10245. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10246. /* planes */
  10247. for_each_plane(dev_priv, pipe, plane) {
  10248. hw_entry = &hw_ddb.plane[pipe][plane];
  10249. sw_entry = &sw_ddb->plane[pipe][plane];
  10250. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10251. continue;
  10252. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10253. "(expected (%u,%u), found (%u,%u))\n",
  10254. pipe_name(pipe), plane + 1,
  10255. sw_entry->start, sw_entry->end,
  10256. hw_entry->start, hw_entry->end);
  10257. }
  10258. /* cursor */
  10259. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10260. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10261. if (!skl_ddb_entry_equal(hw_entry, sw_entry)) {
  10262. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10263. "(expected (%u,%u), found (%u,%u))\n",
  10264. pipe_name(pipe),
  10265. sw_entry->start, sw_entry->end,
  10266. hw_entry->start, hw_entry->end);
  10267. }
  10268. }
  10269. static void
  10270. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  10271. {
  10272. struct drm_connector *connector;
  10273. drm_for_each_connector(connector, dev) {
  10274. struct drm_encoder *encoder = connector->encoder;
  10275. struct drm_connector_state *state = connector->state;
  10276. if (state->crtc != crtc)
  10277. continue;
  10278. intel_connector_verify_state(to_intel_connector(connector),
  10279. connector->state);
  10280. I915_STATE_WARN(state->best_encoder != encoder,
  10281. "connector's atomic encoder doesn't match legacy encoder\n");
  10282. }
  10283. }
  10284. static void
  10285. verify_encoder_state(struct drm_device *dev)
  10286. {
  10287. struct intel_encoder *encoder;
  10288. struct intel_connector *connector;
  10289. for_each_intel_encoder(dev, encoder) {
  10290. bool enabled = false;
  10291. enum pipe pipe;
  10292. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10293. encoder->base.base.id,
  10294. encoder->base.name);
  10295. for_each_intel_connector(dev, connector) {
  10296. if (connector->base.state->best_encoder != &encoder->base)
  10297. continue;
  10298. enabled = true;
  10299. I915_STATE_WARN(connector->base.state->crtc !=
  10300. encoder->base.crtc,
  10301. "connector's crtc doesn't match encoder crtc\n");
  10302. }
  10303. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10304. "encoder's enabled state mismatch "
  10305. "(expected %i, found %i)\n",
  10306. !!encoder->base.crtc, enabled);
  10307. if (!encoder->base.crtc) {
  10308. bool active;
  10309. active = encoder->get_hw_state(encoder, &pipe);
  10310. I915_STATE_WARN(active,
  10311. "encoder detached but still enabled on pipe %c.\n",
  10312. pipe_name(pipe));
  10313. }
  10314. }
  10315. }
  10316. static void
  10317. verify_crtc_state(struct drm_crtc *crtc,
  10318. struct drm_crtc_state *old_crtc_state,
  10319. struct drm_crtc_state *new_crtc_state)
  10320. {
  10321. struct drm_device *dev = crtc->dev;
  10322. struct drm_i915_private *dev_priv = dev->dev_private;
  10323. struct intel_encoder *encoder;
  10324. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10325. struct intel_crtc_state *pipe_config, *sw_config;
  10326. struct drm_atomic_state *old_state;
  10327. bool active;
  10328. old_state = old_crtc_state->state;
  10329. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10330. pipe_config = to_intel_crtc_state(old_crtc_state);
  10331. memset(pipe_config, 0, sizeof(*pipe_config));
  10332. pipe_config->base.crtc = crtc;
  10333. pipe_config->base.state = old_state;
  10334. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  10335. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10336. /* hw state is inconsistent with the pipe quirk */
  10337. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10338. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10339. active = new_crtc_state->active;
  10340. I915_STATE_WARN(new_crtc_state->active != active,
  10341. "crtc active state doesn't match with hw state "
  10342. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10343. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10344. "transitional active state does not match atomic hw state "
  10345. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10346. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10347. enum pipe pipe;
  10348. active = encoder->get_hw_state(encoder, &pipe);
  10349. I915_STATE_WARN(active != new_crtc_state->active,
  10350. "[ENCODER:%i] active %i with crtc active %i\n",
  10351. encoder->base.base.id, active, new_crtc_state->active);
  10352. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10353. "Encoder connected to wrong pipe %c\n",
  10354. pipe_name(pipe));
  10355. if (active)
  10356. encoder->get_config(encoder, pipe_config);
  10357. }
  10358. if (!new_crtc_state->active)
  10359. return;
  10360. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10361. sw_config = to_intel_crtc_state(crtc->state);
  10362. if (!intel_pipe_config_compare(dev, sw_config,
  10363. pipe_config, false)) {
  10364. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10365. intel_dump_pipe_config(intel_crtc, pipe_config,
  10366. "[hw state]");
  10367. intel_dump_pipe_config(intel_crtc, sw_config,
  10368. "[sw state]");
  10369. }
  10370. }
  10371. static void
  10372. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10373. struct intel_shared_dpll *pll,
  10374. struct drm_crtc *crtc,
  10375. struct drm_crtc_state *new_state)
  10376. {
  10377. struct intel_dpll_hw_state dpll_hw_state;
  10378. unsigned crtc_mask;
  10379. bool active;
  10380. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10381. DRM_DEBUG_KMS("%s\n", pll->name);
  10382. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10383. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10384. I915_STATE_WARN(!pll->on && pll->active_mask,
  10385. "pll in active use but not on in sw tracking\n");
  10386. I915_STATE_WARN(pll->on && !pll->active_mask,
  10387. "pll is on but not used by any active crtc\n");
  10388. I915_STATE_WARN(pll->on != active,
  10389. "pll on state mismatch (expected %i, found %i)\n",
  10390. pll->on, active);
  10391. }
  10392. if (!crtc) {
  10393. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  10394. "more active pll users than references: %x vs %x\n",
  10395. pll->active_mask, pll->config.crtc_mask);
  10396. return;
  10397. }
  10398. crtc_mask = 1 << drm_crtc_index(crtc);
  10399. if (new_state->active)
  10400. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10401. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10402. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10403. else
  10404. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10405. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10406. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10407. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  10408. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10409. crtc_mask, pll->config.crtc_mask);
  10410. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  10411. &dpll_hw_state,
  10412. sizeof(dpll_hw_state)),
  10413. "pll hw state mismatch\n");
  10414. }
  10415. static void
  10416. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10417. struct drm_crtc_state *old_crtc_state,
  10418. struct drm_crtc_state *new_crtc_state)
  10419. {
  10420. struct drm_i915_private *dev_priv = dev->dev_private;
  10421. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10422. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10423. if (new_state->shared_dpll)
  10424. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10425. if (old_state->shared_dpll &&
  10426. old_state->shared_dpll != new_state->shared_dpll) {
  10427. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10428. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10429. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10430. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10431. pipe_name(drm_crtc_index(crtc)));
  10432. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  10433. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10434. pipe_name(drm_crtc_index(crtc)));
  10435. }
  10436. }
  10437. static void
  10438. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10439. struct drm_crtc_state *old_state,
  10440. struct drm_crtc_state *new_state)
  10441. {
  10442. verify_wm_state(crtc, new_state);
  10443. verify_crtc_state(crtc, old_state, new_state);
  10444. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10445. }
  10446. static void
  10447. verify_disabled_dpll_state(struct drm_device *dev)
  10448. {
  10449. struct drm_i915_private *dev_priv = dev->dev_private;
  10450. int i;
  10451. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10452. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10453. }
  10454. static void
  10455. intel_modeset_verify_disabled(struct drm_device *dev)
  10456. {
  10457. verify_encoder_state(dev);
  10458. verify_connector_state(dev, NULL);
  10459. verify_disabled_dpll_state(dev);
  10460. }
  10461. static void update_scanline_offset(struct intel_crtc *crtc)
  10462. {
  10463. struct drm_device *dev = crtc->base.dev;
  10464. /*
  10465. * The scanline counter increments at the leading edge of hsync.
  10466. *
  10467. * On most platforms it starts counting from vtotal-1 on the
  10468. * first active line. That means the scanline counter value is
  10469. * always one less than what we would expect. Ie. just after
  10470. * start of vblank, which also occurs at start of hsync (on the
  10471. * last active line), the scanline counter will read vblank_start-1.
  10472. *
  10473. * On gen2 the scanline counter starts counting from 1 instead
  10474. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10475. * to keep the value positive), instead of adding one.
  10476. *
  10477. * On HSW+ the behaviour of the scanline counter depends on the output
  10478. * type. For DP ports it behaves like most other platforms, but on HDMI
  10479. * there's an extra 1 line difference. So we need to add two instead of
  10480. * one to the value.
  10481. */
  10482. if (IS_GEN2(dev)) {
  10483. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10484. int vtotal;
  10485. vtotal = adjusted_mode->crtc_vtotal;
  10486. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10487. vtotal /= 2;
  10488. crtc->scanline_offset = vtotal - 1;
  10489. } else if (HAS_DDI(dev) &&
  10490. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10491. crtc->scanline_offset = 2;
  10492. } else
  10493. crtc->scanline_offset = 1;
  10494. }
  10495. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10496. {
  10497. struct drm_device *dev = state->dev;
  10498. struct drm_i915_private *dev_priv = to_i915(dev);
  10499. struct intel_shared_dpll_config *shared_dpll = NULL;
  10500. struct drm_crtc *crtc;
  10501. struct drm_crtc_state *crtc_state;
  10502. int i;
  10503. if (!dev_priv->display.crtc_compute_clock)
  10504. return;
  10505. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10506. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10507. struct intel_shared_dpll *old_dpll =
  10508. to_intel_crtc_state(crtc->state)->shared_dpll;
  10509. if (!needs_modeset(crtc_state))
  10510. continue;
  10511. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  10512. if (!old_dpll)
  10513. continue;
  10514. if (!shared_dpll)
  10515. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10516. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  10517. }
  10518. }
  10519. /*
  10520. * This implements the workaround described in the "notes" section of the mode
  10521. * set sequence documentation. When going from no pipes or single pipe to
  10522. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10523. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10524. */
  10525. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10526. {
  10527. struct drm_crtc_state *crtc_state;
  10528. struct intel_crtc *intel_crtc;
  10529. struct drm_crtc *crtc;
  10530. struct intel_crtc_state *first_crtc_state = NULL;
  10531. struct intel_crtc_state *other_crtc_state = NULL;
  10532. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10533. int i;
  10534. /* look at all crtc's that are going to be enabled in during modeset */
  10535. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10536. intel_crtc = to_intel_crtc(crtc);
  10537. if (!crtc_state->active || !needs_modeset(crtc_state))
  10538. continue;
  10539. if (first_crtc_state) {
  10540. other_crtc_state = to_intel_crtc_state(crtc_state);
  10541. break;
  10542. } else {
  10543. first_crtc_state = to_intel_crtc_state(crtc_state);
  10544. first_pipe = intel_crtc->pipe;
  10545. }
  10546. }
  10547. /* No workaround needed? */
  10548. if (!first_crtc_state)
  10549. return 0;
  10550. /* w/a possibly needed, check how many crtc's are already enabled. */
  10551. for_each_intel_crtc(state->dev, intel_crtc) {
  10552. struct intel_crtc_state *pipe_config;
  10553. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10554. if (IS_ERR(pipe_config))
  10555. return PTR_ERR(pipe_config);
  10556. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10557. if (!pipe_config->base.active ||
  10558. needs_modeset(&pipe_config->base))
  10559. continue;
  10560. /* 2 or more enabled crtcs means no need for w/a */
  10561. if (enabled_pipe != INVALID_PIPE)
  10562. return 0;
  10563. enabled_pipe = intel_crtc->pipe;
  10564. }
  10565. if (enabled_pipe != INVALID_PIPE)
  10566. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10567. else if (other_crtc_state)
  10568. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10569. return 0;
  10570. }
  10571. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10572. {
  10573. struct drm_crtc *crtc;
  10574. struct drm_crtc_state *crtc_state;
  10575. int ret = 0;
  10576. /* add all active pipes to the state */
  10577. for_each_crtc(state->dev, crtc) {
  10578. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10579. if (IS_ERR(crtc_state))
  10580. return PTR_ERR(crtc_state);
  10581. if (!crtc_state->active || needs_modeset(crtc_state))
  10582. continue;
  10583. crtc_state->mode_changed = true;
  10584. ret = drm_atomic_add_affected_connectors(state, crtc);
  10585. if (ret)
  10586. break;
  10587. ret = drm_atomic_add_affected_planes(state, crtc);
  10588. if (ret)
  10589. break;
  10590. }
  10591. return ret;
  10592. }
  10593. static int intel_modeset_checks(struct drm_atomic_state *state)
  10594. {
  10595. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10596. struct drm_i915_private *dev_priv = state->dev->dev_private;
  10597. struct drm_crtc *crtc;
  10598. struct drm_crtc_state *crtc_state;
  10599. int ret = 0, i;
  10600. if (!check_digital_port_conflicts(state)) {
  10601. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10602. return -EINVAL;
  10603. }
  10604. intel_state->modeset = true;
  10605. intel_state->active_crtcs = dev_priv->active_crtcs;
  10606. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10607. if (crtc_state->active)
  10608. intel_state->active_crtcs |= 1 << i;
  10609. else
  10610. intel_state->active_crtcs &= ~(1 << i);
  10611. if (crtc_state->active != crtc->state->active)
  10612. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10613. }
  10614. /*
  10615. * See if the config requires any additional preparation, e.g.
  10616. * to adjust global state with pipes off. We need to do this
  10617. * here so we can get the modeset_pipe updated config for the new
  10618. * mode set on this crtc. For other crtcs we need to use the
  10619. * adjusted_mode bits in the crtc directly.
  10620. */
  10621. if (dev_priv->display.modeset_calc_cdclk) {
  10622. if (!intel_state->cdclk_pll_vco)
  10623. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  10624. if (!intel_state->cdclk_pll_vco)
  10625. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  10626. ret = dev_priv->display.modeset_calc_cdclk(state);
  10627. if (ret < 0)
  10628. return ret;
  10629. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  10630. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  10631. ret = intel_modeset_all_pipes(state);
  10632. if (ret < 0)
  10633. return ret;
  10634. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  10635. intel_state->cdclk, intel_state->dev_cdclk);
  10636. } else
  10637. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  10638. intel_modeset_clear_plls(state);
  10639. if (IS_HASWELL(dev_priv))
  10640. return haswell_mode_set_planes_workaround(state);
  10641. return 0;
  10642. }
  10643. /*
  10644. * Handle calculation of various watermark data at the end of the atomic check
  10645. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10646. * handlers to ensure that all derived state has been updated.
  10647. */
  10648. static int calc_watermark_data(struct drm_atomic_state *state)
  10649. {
  10650. struct drm_device *dev = state->dev;
  10651. struct drm_i915_private *dev_priv = to_i915(dev);
  10652. /* Is there platform-specific watermark information to calculate? */
  10653. if (dev_priv->display.compute_global_watermarks)
  10654. return dev_priv->display.compute_global_watermarks(state);
  10655. return 0;
  10656. }
  10657. /**
  10658. * intel_atomic_check - validate state object
  10659. * @dev: drm device
  10660. * @state: state to validate
  10661. */
  10662. static int intel_atomic_check(struct drm_device *dev,
  10663. struct drm_atomic_state *state)
  10664. {
  10665. struct drm_i915_private *dev_priv = to_i915(dev);
  10666. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10667. struct drm_crtc *crtc;
  10668. struct drm_crtc_state *crtc_state;
  10669. int ret, i;
  10670. bool any_ms = false;
  10671. ret = drm_atomic_helper_check_modeset(dev, state);
  10672. if (ret)
  10673. return ret;
  10674. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10675. struct intel_crtc_state *pipe_config =
  10676. to_intel_crtc_state(crtc_state);
  10677. /* Catch I915_MODE_FLAG_INHERITED */
  10678. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10679. crtc_state->mode_changed = true;
  10680. if (!needs_modeset(crtc_state))
  10681. continue;
  10682. if (!crtc_state->enable) {
  10683. any_ms = true;
  10684. continue;
  10685. }
  10686. /* FIXME: For only active_changed we shouldn't need to do any
  10687. * state recomputation at all. */
  10688. ret = drm_atomic_add_affected_connectors(state, crtc);
  10689. if (ret)
  10690. return ret;
  10691. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10692. if (ret) {
  10693. intel_dump_pipe_config(to_intel_crtc(crtc),
  10694. pipe_config, "[failed]");
  10695. return ret;
  10696. }
  10697. if (i915.fastboot &&
  10698. intel_pipe_config_compare(dev,
  10699. to_intel_crtc_state(crtc->state),
  10700. pipe_config, true)) {
  10701. crtc_state->mode_changed = false;
  10702. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10703. }
  10704. if (needs_modeset(crtc_state))
  10705. any_ms = true;
  10706. ret = drm_atomic_add_affected_planes(state, crtc);
  10707. if (ret)
  10708. return ret;
  10709. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10710. needs_modeset(crtc_state) ?
  10711. "[modeset]" : "[fastset]");
  10712. }
  10713. if (any_ms) {
  10714. ret = intel_modeset_checks(state);
  10715. if (ret)
  10716. return ret;
  10717. } else
  10718. intel_state->cdclk = dev_priv->cdclk_freq;
  10719. ret = drm_atomic_helper_check_planes(dev, state);
  10720. if (ret)
  10721. return ret;
  10722. intel_fbc_choose_crtc(dev_priv, state);
  10723. return calc_watermark_data(state);
  10724. }
  10725. static bool needs_work(struct drm_crtc_state *crtc_state)
  10726. {
  10727. /* hw state checker needs to run */
  10728. if (needs_modeset(crtc_state))
  10729. return true;
  10730. /* unpin old fb's, possibly vblank update */
  10731. if (crtc_state->planes_changed)
  10732. return true;
  10733. /* pipe parameters need to be updated, and hw state checker */
  10734. if (to_intel_crtc_state(crtc_state)->update_pipe)
  10735. return true;
  10736. /* vblank event requested? */
  10737. if (crtc_state->event)
  10738. return true;
  10739. return false;
  10740. }
  10741. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10742. struct drm_atomic_state *state,
  10743. bool nonblock)
  10744. {
  10745. struct drm_i915_private *dev_priv = dev->dev_private;
  10746. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10747. struct drm_plane_state *plane_state;
  10748. struct drm_crtc_state *crtc_state;
  10749. struct drm_plane *plane;
  10750. struct drm_crtc *crtc;
  10751. int i, ret;
  10752. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10753. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10754. struct intel_flip_work *work;
  10755. if (!state->legacy_cursor_update) {
  10756. ret = intel_crtc_wait_for_pending_flips(crtc);
  10757. if (ret)
  10758. return ret;
  10759. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10760. flush_workqueue(dev_priv->wq);
  10761. }
  10762. /* test if we need to update something */
  10763. if (!needs_work(crtc_state))
  10764. continue;
  10765. intel_state->work[i] = work =
  10766. kzalloc(sizeof(**intel_state->work), GFP_KERNEL);
  10767. if (!work)
  10768. return -ENOMEM;
  10769. if (needs_modeset(crtc_state) ||
  10770. to_intel_crtc_state(crtc_state)->update_pipe) {
  10771. work->num_old_connectors = hweight32(crtc->state->connector_mask);
  10772. work->old_connector_state = kcalloc(work->num_old_connectors,
  10773. sizeof(*work->old_connector_state),
  10774. GFP_KERNEL);
  10775. work->num_new_connectors = hweight32(crtc_state->connector_mask);
  10776. work->new_connector_state = kcalloc(work->num_new_connectors,
  10777. sizeof(*work->new_connector_state),
  10778. GFP_KERNEL);
  10779. if (!work->old_connector_state || !work->new_connector_state)
  10780. return -ENOMEM;
  10781. }
  10782. }
  10783. if (intel_state->modeset && nonblock) {
  10784. DRM_DEBUG_ATOMIC("Nonblock modesets are not yet supported!\n");
  10785. return -EINVAL;
  10786. }
  10787. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10788. if (ret)
  10789. return ret;
  10790. ret = drm_atomic_helper_prepare_planes(dev, state);
  10791. mutex_unlock(&dev->struct_mutex);
  10792. if (!ret && !nonblock) {
  10793. for_each_plane_in_state(state, plane, plane_state, i) {
  10794. struct intel_plane_state *intel_plane_state =
  10795. to_intel_plane_state(plane_state);
  10796. if (plane_state->fence) {
  10797. long lret = fence_wait(plane_state->fence, true);
  10798. if (lret < 0) {
  10799. ret = lret;
  10800. break;
  10801. }
  10802. }
  10803. if (!intel_plane_state->wait_req)
  10804. continue;
  10805. ret = __i915_wait_request(intel_plane_state->wait_req,
  10806. true, NULL, NULL);
  10807. if (ret) {
  10808. /* Any hang should be swallowed by the wait */
  10809. WARN_ON(ret == -EIO);
  10810. mutex_lock(&dev->struct_mutex);
  10811. drm_atomic_helper_cleanup_planes(dev, state);
  10812. mutex_unlock(&dev->struct_mutex);
  10813. break;
  10814. }
  10815. }
  10816. }
  10817. return ret;
  10818. }
  10819. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10820. {
  10821. struct drm_device *dev = crtc->base.dev;
  10822. if (!dev->max_vblank_count)
  10823. return drm_accurate_vblank_count(&crtc->base);
  10824. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10825. }
  10826. static void intel_prepare_work(struct drm_crtc *crtc,
  10827. struct intel_flip_work *work,
  10828. struct drm_atomic_state *state,
  10829. struct drm_crtc_state *old_crtc_state)
  10830. {
  10831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10832. struct drm_plane_state *old_plane_state;
  10833. struct drm_plane *plane;
  10834. int i, j = 0;
  10835. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10836. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10837. atomic_inc(&intel_crtc->unpin_work_count);
  10838. for_each_plane_in_state(state, plane, old_plane_state, i) {
  10839. struct intel_plane_state *old_state = to_intel_plane_state(old_plane_state);
  10840. struct intel_plane_state *new_state = to_intel_plane_state(plane->state);
  10841. if (old_state->base.crtc != crtc &&
  10842. new_state->base.crtc != crtc)
  10843. continue;
  10844. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  10845. plane->fb = new_state->base.fb;
  10846. crtc->x = new_state->base.src_x >> 16;
  10847. crtc->y = new_state->base.src_y >> 16;
  10848. }
  10849. old_state->wait_req = new_state->wait_req;
  10850. new_state->wait_req = NULL;
  10851. old_state->base.fence = new_state->base.fence;
  10852. new_state->base.fence = NULL;
  10853. /* remove plane state from the atomic state and move it to work */
  10854. old_plane_state->state = NULL;
  10855. state->planes[i] = NULL;
  10856. state->plane_states[i] = NULL;
  10857. work->old_plane_state[j] = old_state;
  10858. work->new_plane_state[j++] = new_state;
  10859. }
  10860. old_crtc_state->state = NULL;
  10861. state->crtcs[drm_crtc_index(crtc)] = NULL;
  10862. state->crtc_states[drm_crtc_index(crtc)] = NULL;
  10863. work->old_crtc_state = to_intel_crtc_state(old_crtc_state);
  10864. work->new_crtc_state = to_intel_crtc_state(crtc->state);
  10865. work->num_planes = j;
  10866. work->event = crtc->state->event;
  10867. crtc->state->event = NULL;
  10868. if (needs_modeset(crtc->state) || work->new_crtc_state->update_pipe) {
  10869. struct drm_connector *conn;
  10870. struct drm_connector_state *old_conn_state;
  10871. int k = 0;
  10872. j = 0;
  10873. /*
  10874. * intel_unpin_work_fn cannot depend on the connector list
  10875. * because it may be freed from underneath it, so add
  10876. * them all to the work struct while we're holding locks.
  10877. */
  10878. for_each_connector_in_state(state, conn, old_conn_state, i) {
  10879. if (old_conn_state->crtc == crtc) {
  10880. work->old_connector_state[j++] = old_conn_state;
  10881. state->connectors[i] = NULL;
  10882. state->connector_states[i] = NULL;
  10883. }
  10884. }
  10885. /* If another crtc has stolen the connector from state,
  10886. * then for_each_connector_in_state is no longer reliable,
  10887. * so use drm_for_each_connector here.
  10888. */
  10889. drm_for_each_connector(conn, state->dev)
  10890. if (conn->state->crtc == crtc)
  10891. work->new_connector_state[k++] = conn->state;
  10892. WARN(j != work->num_old_connectors, "j = %i, expected %i\n", j, work->num_old_connectors);
  10893. WARN(k != work->num_new_connectors, "k = %i, expected %i\n", k, work->num_new_connectors);
  10894. } else if (!work->new_crtc_state->update_wm_post)
  10895. work->can_async_unpin = true;
  10896. work->fb_bits = work->new_crtc_state->fb_bits;
  10897. }
  10898. static void intel_schedule_unpin(struct drm_crtc *crtc,
  10899. struct intel_atomic_state *state,
  10900. struct intel_flip_work *work)
  10901. {
  10902. struct drm_device *dev = crtc->dev;
  10903. struct drm_i915_private *dev_priv = dev->dev_private;
  10904. to_intel_crtc(crtc)->config = work->new_crtc_state;
  10905. queue_work(dev_priv->wq, &work->unpin_work);
  10906. }
  10907. static void intel_schedule_flip(struct drm_crtc *crtc,
  10908. struct intel_atomic_state *state,
  10909. struct intel_flip_work *work,
  10910. bool nonblock)
  10911. {
  10912. struct intel_crtc_state *crtc_state = work->new_crtc_state;
  10913. if (crtc_state->base.planes_changed ||
  10914. needs_modeset(&crtc_state->base) ||
  10915. crtc_state->update_pipe) {
  10916. if (nonblock)
  10917. schedule_work(&work->mmio_work);
  10918. else
  10919. intel_mmio_flip_work_func(&work->mmio_work);
  10920. } else {
  10921. int ret;
  10922. ret = drm_crtc_vblank_get(crtc);
  10923. I915_STATE_WARN(ret < 0, "enabling vblank failed with %i\n", ret);
  10924. work->flip_queued_vblank = intel_crtc_get_vblank_counter(to_intel_crtc(crtc));
  10925. smp_mb__before_atomic();
  10926. atomic_set(&work->pending, 1);
  10927. }
  10928. }
  10929. static void intel_schedule_update(struct drm_crtc *crtc,
  10930. struct intel_atomic_state *state,
  10931. struct intel_flip_work *work,
  10932. bool nonblock)
  10933. {
  10934. struct drm_device *dev = crtc->dev;
  10935. struct intel_crtc_state *pipe_config = work->new_crtc_state;
  10936. if (!pipe_config->base.active && work->can_async_unpin) {
  10937. INIT_LIST_HEAD(&work->head);
  10938. intel_schedule_unpin(crtc, state, work);
  10939. return;
  10940. }
  10941. spin_lock_irq(&dev->event_lock);
  10942. list_add_tail(&work->head, &to_intel_crtc(crtc)->flip_work);
  10943. spin_unlock_irq(&dev->event_lock);
  10944. if (!pipe_config->base.active)
  10945. intel_schedule_unpin(crtc, state, work);
  10946. else
  10947. intel_schedule_flip(crtc, state, work, nonblock);
  10948. }
  10949. /**
  10950. * intel_atomic_commit - commit validated state object
  10951. * @dev: DRM device
  10952. * @state: the top-level driver state object
  10953. * @nonblock: nonblocking commit
  10954. *
  10955. * This function commits a top-level state object that has been validated
  10956. * with drm_atomic_helper_check().
  10957. *
  10958. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  10959. * we can only handle plane-related operations and do not yet support
  10960. * nonblocking commit.
  10961. *
  10962. * RETURNS
  10963. * Zero for success or -errno.
  10964. */
  10965. static int intel_atomic_commit(struct drm_device *dev,
  10966. struct drm_atomic_state *state,
  10967. bool nonblock)
  10968. {
  10969. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10970. struct drm_i915_private *dev_priv = dev->dev_private;
  10971. struct drm_crtc_state *old_crtc_state;
  10972. struct drm_crtc *crtc;
  10973. int ret = 0, i;
  10974. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  10975. if (ret) {
  10976. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10977. return ret;
  10978. }
  10979. drm_atomic_helper_swap_state(dev, state);
  10980. dev_priv->wm.distrust_bios_wm = false;
  10981. dev_priv->wm.skl_results = intel_state->wm_results;
  10982. intel_shared_dpll_commit(state);
  10983. if (intel_state->modeset) {
  10984. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10985. sizeof(intel_state->min_pixclk));
  10986. dev_priv->active_crtcs = intel_state->active_crtcs;
  10987. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  10988. }
  10989. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10990. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10991. if (!needs_modeset(crtc->state))
  10992. continue;
  10993. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  10994. intel_state->work[i]->put_power_domains =
  10995. modeset_get_crtc_power_domains(crtc,
  10996. to_intel_crtc_state(crtc->state));
  10997. if (old_crtc_state->active) {
  10998. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10999. dev_priv->display.crtc_disable(crtc);
  11000. intel_crtc->active = false;
  11001. intel_fbc_disable(intel_crtc);
  11002. intel_disable_shared_dpll(intel_crtc);
  11003. /*
  11004. * Underruns don't always raise
  11005. * interrupts, so check manually.
  11006. */
  11007. intel_check_cpu_fifo_underruns(dev_priv);
  11008. intel_check_pch_fifo_underruns(dev_priv);
  11009. if (!crtc->state->active)
  11010. intel_update_watermarks(crtc);
  11011. }
  11012. }
  11013. /* Only after disabling all output pipelines that will be changed can we
  11014. * update the the output configuration. */
  11015. intel_modeset_update_crtc_state(state);
  11016. if (intel_state->modeset) {
  11017. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11018. if (dev_priv->display.modeset_commit_cdclk &&
  11019. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11020. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  11021. dev_priv->display.modeset_commit_cdclk(state);
  11022. intel_modeset_verify_disabled(dev);
  11023. }
  11024. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11025. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11026. struct intel_flip_work *work = intel_state->work[i];
  11027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11028. bool modeset = needs_modeset(crtc->state);
  11029. if (modeset && crtc->state->active) {
  11030. update_scanline_offset(to_intel_crtc(crtc));
  11031. dev_priv->display.crtc_enable(crtc);
  11032. }
  11033. if (!modeset)
  11034. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11035. if (!work) {
  11036. if (!list_empty_careful(&intel_crtc->flip_work)) {
  11037. spin_lock_irq(&dev->event_lock);
  11038. if (!list_empty(&intel_crtc->flip_work))
  11039. work = list_last_entry(&intel_crtc->flip_work,
  11040. struct intel_flip_work, head);
  11041. if (work && work->new_crtc_state == to_intel_crtc_state(old_crtc_state)) {
  11042. work->free_new_crtc_state = true;
  11043. state->crtc_states[i] = NULL;
  11044. state->crtcs[i] = NULL;
  11045. }
  11046. spin_unlock_irq(&dev->event_lock);
  11047. }
  11048. continue;
  11049. }
  11050. intel_state->work[i] = NULL;
  11051. intel_prepare_work(crtc, work, state, old_crtc_state);
  11052. intel_schedule_update(crtc, intel_state, work, nonblock);
  11053. }
  11054. /* FIXME: add subpixel order */
  11055. drm_atomic_state_free(state);
  11056. /* As one of the primary mmio accessors, KMS has a high likelihood
  11057. * of triggering bugs in unclaimed access. After we finish
  11058. * modesetting, see if an error has been flagged, and if so
  11059. * enable debugging for the next modeset - and hope we catch
  11060. * the culprit.
  11061. *
  11062. * XXX note that we assume display power is on at this point.
  11063. * This might hold true now but we need to add pm helper to check
  11064. * unclaimed only when the hardware is on, as atomic commits
  11065. * can happen also when the device is completely off.
  11066. */
  11067. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  11068. return 0;
  11069. }
  11070. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11071. {
  11072. struct drm_device *dev = crtc->dev;
  11073. struct drm_atomic_state *state;
  11074. struct drm_crtc_state *crtc_state;
  11075. int ret;
  11076. state = drm_atomic_state_alloc(dev);
  11077. if (!state) {
  11078. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11079. crtc->base.id);
  11080. return;
  11081. }
  11082. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11083. retry:
  11084. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11085. ret = PTR_ERR_OR_ZERO(crtc_state);
  11086. if (!ret) {
  11087. if (!crtc_state->active)
  11088. goto out;
  11089. crtc_state->mode_changed = true;
  11090. ret = drm_atomic_commit(state);
  11091. }
  11092. if (ret == -EDEADLK) {
  11093. drm_atomic_state_clear(state);
  11094. drm_modeset_backoff(state->acquire_ctx);
  11095. goto retry;
  11096. }
  11097. if (ret)
  11098. out:
  11099. drm_atomic_state_free(state);
  11100. }
  11101. #undef for_each_intel_crtc_masked
  11102. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11103. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  11104. .set_config = drm_atomic_helper_set_config,
  11105. .set_property = drm_atomic_helper_crtc_set_property,
  11106. .destroy = intel_crtc_destroy,
  11107. .page_flip = drm_atomic_helper_page_flip,
  11108. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11109. .atomic_destroy_state = intel_crtc_destroy_state,
  11110. };
  11111. static struct fence *intel_get_excl_fence(struct drm_i915_gem_object *obj)
  11112. {
  11113. struct reservation_object *resv;
  11114. if (!obj->base.dma_buf)
  11115. return NULL;
  11116. resv = obj->base.dma_buf->resv;
  11117. /* For framebuffer backed by dmabuf, wait for fence */
  11118. while (1) {
  11119. struct fence *fence_excl, *ret = NULL;
  11120. rcu_read_lock();
  11121. fence_excl = rcu_dereference(resv->fence_excl);
  11122. if (fence_excl)
  11123. ret = fence_get_rcu(fence_excl);
  11124. rcu_read_unlock();
  11125. if (ret == fence_excl)
  11126. return ret;
  11127. }
  11128. }
  11129. /**
  11130. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11131. * @plane: drm plane to prepare for
  11132. * @fb: framebuffer to prepare for presentation
  11133. *
  11134. * Prepares a framebuffer for usage on a display plane. Generally this
  11135. * involves pinning the underlying object and updating the frontbuffer tracking
  11136. * bits. Some older platforms need special physical address handling for
  11137. * cursor planes.
  11138. *
  11139. * Must be called with struct_mutex held.
  11140. *
  11141. * Returns 0 on success, negative error code on failure.
  11142. */
  11143. int
  11144. intel_prepare_plane_fb(struct drm_plane *plane,
  11145. const struct drm_plane_state *new_state)
  11146. {
  11147. struct drm_device *dev = plane->dev;
  11148. struct drm_framebuffer *fb = new_state->fb;
  11149. struct intel_plane *intel_plane = to_intel_plane(plane);
  11150. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11151. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11152. struct drm_crtc *crtc = new_state->crtc ?: plane->state->crtc;
  11153. int ret = 0;
  11154. if (!obj && !old_obj)
  11155. return 0;
  11156. if (WARN_ON(!new_state->state) || WARN_ON(!crtc) ||
  11157. WARN_ON(!to_intel_atomic_state(new_state->state)->work[to_intel_crtc(crtc)->pipe])) {
  11158. if (WARN_ON(old_obj != obj))
  11159. return -EINVAL;
  11160. return 0;
  11161. }
  11162. if (old_obj) {
  11163. struct drm_crtc_state *crtc_state =
  11164. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11165. /* Big Hammer, we also need to ensure that any pending
  11166. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11167. * current scanout is retired before unpinning the old
  11168. * framebuffer. Note that we rely on userspace rendering
  11169. * into the buffer attached to the pipe they are waiting
  11170. * on. If not, userspace generates a GPU hang with IPEHR
  11171. * point to the MI_WAIT_FOR_EVENT.
  11172. *
  11173. * This should only fail upon a hung GPU, in which case we
  11174. * can safely continue.
  11175. */
  11176. if (needs_modeset(crtc_state))
  11177. ret = i915_gem_object_wait_rendering(old_obj, true);
  11178. if (ret) {
  11179. /* GPU hangs should have been swallowed by the wait */
  11180. WARN_ON(ret == -EIO);
  11181. return ret;
  11182. }
  11183. }
  11184. if (!obj) {
  11185. ret = 0;
  11186. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11187. INTEL_INFO(dev)->cursor_needs_physical) {
  11188. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11189. ret = i915_gem_object_attach_phys(obj, align);
  11190. if (ret)
  11191. DRM_DEBUG_KMS("failed to attach phys object\n");
  11192. } else {
  11193. ret = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11194. }
  11195. if (ret == 0) {
  11196. if (obj) {
  11197. struct intel_plane_state *plane_state =
  11198. to_intel_plane_state(new_state);
  11199. i915_gem_request_assign(&plane_state->wait_req,
  11200. obj->last_write_req);
  11201. plane_state->base.fence = intel_get_excl_fence(obj);
  11202. }
  11203. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11204. }
  11205. return ret;
  11206. }
  11207. /**
  11208. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11209. * @plane: drm plane to clean up for
  11210. * @fb: old framebuffer that was on plane
  11211. *
  11212. * Cleans up a framebuffer that has just been removed from a plane.
  11213. *
  11214. * Must be called with struct_mutex held.
  11215. */
  11216. void
  11217. intel_cleanup_plane_fb(struct drm_plane *plane,
  11218. const struct drm_plane_state *old_state)
  11219. {
  11220. struct drm_device *dev = plane->dev;
  11221. struct intel_plane *intel_plane = to_intel_plane(plane);
  11222. struct intel_plane_state *old_intel_state;
  11223. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11224. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11225. old_intel_state = to_intel_plane_state(old_state);
  11226. if (!obj && !old_obj)
  11227. return;
  11228. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11229. !INTEL_INFO(dev)->cursor_needs_physical))
  11230. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  11231. /* prepare_fb aborted? */
  11232. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11233. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11234. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11235. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11236. fence_put(old_intel_state->base.fence);
  11237. old_intel_state->base.fence = NULL;
  11238. }
  11239. int
  11240. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11241. {
  11242. int max_scale;
  11243. struct drm_device *dev;
  11244. struct drm_i915_private *dev_priv;
  11245. int crtc_clock, cdclk;
  11246. if (!intel_crtc || !crtc_state->base.enable)
  11247. return DRM_PLANE_HELPER_NO_SCALING;
  11248. dev = intel_crtc->base.dev;
  11249. dev_priv = dev->dev_private;
  11250. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11251. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11252. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11253. return DRM_PLANE_HELPER_NO_SCALING;
  11254. /*
  11255. * skl max scale is lower of:
  11256. * close to 3 but not 3, -1 is for that purpose
  11257. * or
  11258. * cdclk/crtc_clock
  11259. */
  11260. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11261. return max_scale;
  11262. }
  11263. static int
  11264. intel_check_primary_plane(struct drm_plane *plane,
  11265. struct intel_crtc_state *crtc_state,
  11266. struct intel_plane_state *state)
  11267. {
  11268. struct drm_crtc *crtc = state->base.crtc;
  11269. struct drm_framebuffer *fb = state->base.fb;
  11270. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11271. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11272. bool can_position = false;
  11273. if (INTEL_INFO(plane->dev)->gen >= 9) {
  11274. /* use scaler when colorkey is not required */
  11275. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11276. min_scale = 1;
  11277. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11278. }
  11279. can_position = true;
  11280. }
  11281. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11282. &state->dst, &state->clip,
  11283. min_scale, max_scale,
  11284. can_position, true,
  11285. &state->visible);
  11286. }
  11287. /**
  11288. * intel_plane_destroy - destroy a plane
  11289. * @plane: plane to destroy
  11290. *
  11291. * Common destruction function for all types of planes (primary, cursor,
  11292. * sprite).
  11293. */
  11294. void intel_plane_destroy(struct drm_plane *plane)
  11295. {
  11296. struct intel_plane *intel_plane = to_intel_plane(plane);
  11297. drm_plane_cleanup(plane);
  11298. kfree(intel_plane);
  11299. }
  11300. const struct drm_plane_funcs intel_plane_funcs = {
  11301. .update_plane = drm_atomic_helper_update_plane,
  11302. .disable_plane = drm_atomic_helper_disable_plane,
  11303. .destroy = intel_plane_destroy,
  11304. .set_property = drm_atomic_helper_plane_set_property,
  11305. .atomic_get_property = intel_plane_atomic_get_property,
  11306. .atomic_set_property = intel_plane_atomic_set_property,
  11307. .atomic_duplicate_state = intel_plane_duplicate_state,
  11308. .atomic_destroy_state = intel_plane_destroy_state,
  11309. };
  11310. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11311. int pipe)
  11312. {
  11313. struct intel_plane *primary = NULL;
  11314. struct intel_plane_state *state = NULL;
  11315. const uint32_t *intel_primary_formats;
  11316. unsigned int num_formats;
  11317. int ret;
  11318. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11319. if (!primary)
  11320. goto fail;
  11321. state = intel_create_plane_state(&primary->base);
  11322. if (!state)
  11323. goto fail;
  11324. primary->base.state = &state->base;
  11325. primary->can_scale = false;
  11326. primary->max_downscale = 1;
  11327. if (INTEL_INFO(dev)->gen >= 9) {
  11328. primary->can_scale = true;
  11329. state->scaler_id = -1;
  11330. }
  11331. primary->pipe = pipe;
  11332. primary->plane = pipe;
  11333. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11334. primary->check_plane = intel_check_primary_plane;
  11335. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11336. primary->plane = !pipe;
  11337. if (INTEL_INFO(dev)->gen >= 9) {
  11338. intel_primary_formats = skl_primary_formats;
  11339. num_formats = ARRAY_SIZE(skl_primary_formats);
  11340. primary->update_plane = skylake_update_primary_plane;
  11341. primary->disable_plane = skylake_disable_primary_plane;
  11342. } else if (HAS_PCH_SPLIT(dev)) {
  11343. intel_primary_formats = i965_primary_formats;
  11344. num_formats = ARRAY_SIZE(i965_primary_formats);
  11345. primary->update_plane = ironlake_update_primary_plane;
  11346. primary->disable_plane = i9xx_disable_primary_plane;
  11347. } else if (INTEL_INFO(dev)->gen >= 4) {
  11348. intel_primary_formats = i965_primary_formats;
  11349. num_formats = ARRAY_SIZE(i965_primary_formats);
  11350. primary->update_plane = i9xx_update_primary_plane;
  11351. primary->disable_plane = i9xx_disable_primary_plane;
  11352. } else {
  11353. intel_primary_formats = i8xx_primary_formats;
  11354. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11355. primary->update_plane = i9xx_update_primary_plane;
  11356. primary->disable_plane = i9xx_disable_primary_plane;
  11357. }
  11358. ret = drm_universal_plane_init(dev, &primary->base, 0,
  11359. &intel_plane_funcs,
  11360. intel_primary_formats, num_formats,
  11361. DRM_PLANE_TYPE_PRIMARY, NULL);
  11362. if (ret)
  11363. goto fail;
  11364. if (INTEL_INFO(dev)->gen >= 4)
  11365. intel_create_rotation_property(dev, primary);
  11366. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11367. return &primary->base;
  11368. fail:
  11369. kfree(state);
  11370. kfree(primary);
  11371. return NULL;
  11372. }
  11373. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11374. {
  11375. if (!dev->mode_config.rotation_property) {
  11376. unsigned long flags = BIT(DRM_ROTATE_0) |
  11377. BIT(DRM_ROTATE_180);
  11378. if (INTEL_INFO(dev)->gen >= 9)
  11379. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11380. dev->mode_config.rotation_property =
  11381. drm_mode_create_rotation_property(dev, flags);
  11382. }
  11383. if (dev->mode_config.rotation_property)
  11384. drm_object_attach_property(&plane->base.base,
  11385. dev->mode_config.rotation_property,
  11386. plane->base.state->rotation);
  11387. }
  11388. static int
  11389. intel_check_cursor_plane(struct drm_plane *plane,
  11390. struct intel_crtc_state *crtc_state,
  11391. struct intel_plane_state *state)
  11392. {
  11393. struct drm_crtc *crtc = crtc_state->base.crtc;
  11394. struct drm_framebuffer *fb = state->base.fb;
  11395. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11396. enum pipe pipe = to_intel_plane(plane)->pipe;
  11397. unsigned stride;
  11398. int ret;
  11399. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11400. &state->dst, &state->clip,
  11401. DRM_PLANE_HELPER_NO_SCALING,
  11402. DRM_PLANE_HELPER_NO_SCALING,
  11403. true, true, &state->visible);
  11404. if (ret)
  11405. return ret;
  11406. /* if we want to turn off the cursor ignore width and height */
  11407. if (!obj)
  11408. return 0;
  11409. /* Check for which cursor types we support */
  11410. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11411. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11412. state->base.crtc_w, state->base.crtc_h);
  11413. return -EINVAL;
  11414. }
  11415. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11416. if (obj->base.size < stride * state->base.crtc_h) {
  11417. DRM_DEBUG_KMS("buffer is too small\n");
  11418. return -ENOMEM;
  11419. }
  11420. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11421. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11422. return -EINVAL;
  11423. }
  11424. /*
  11425. * There's something wrong with the cursor on CHV pipe C.
  11426. * If it straddles the left edge of the screen then
  11427. * moving it away from the edge or disabling it often
  11428. * results in a pipe underrun, and often that can lead to
  11429. * dead pipe (constant underrun reported, and it scans
  11430. * out just a solid color). To recover from that, the
  11431. * display power well must be turned off and on again.
  11432. * Refuse the put the cursor into that compromised position.
  11433. */
  11434. if (IS_CHERRYVIEW(plane->dev) && pipe == PIPE_C &&
  11435. state->visible && state->base.crtc_x < 0) {
  11436. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11437. return -EINVAL;
  11438. }
  11439. return 0;
  11440. }
  11441. static void
  11442. intel_disable_cursor_plane(struct drm_plane *plane,
  11443. struct drm_crtc *crtc)
  11444. {
  11445. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11446. intel_crtc->cursor_addr = 0;
  11447. intel_crtc_update_cursor(crtc, NULL);
  11448. }
  11449. static void
  11450. intel_update_cursor_plane(struct drm_plane *plane,
  11451. const struct intel_crtc_state *crtc_state,
  11452. const struct intel_plane_state *state)
  11453. {
  11454. struct drm_crtc *crtc = crtc_state->base.crtc;
  11455. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11456. struct drm_device *dev = plane->dev;
  11457. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11458. uint32_t addr;
  11459. if (!obj)
  11460. addr = 0;
  11461. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11462. addr = i915_gem_obj_ggtt_offset(obj);
  11463. else
  11464. addr = obj->phys_handle->busaddr;
  11465. intel_crtc->cursor_addr = addr;
  11466. intel_crtc_update_cursor(crtc, state);
  11467. }
  11468. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11469. int pipe)
  11470. {
  11471. struct intel_plane *cursor = NULL;
  11472. struct intel_plane_state *state = NULL;
  11473. int ret;
  11474. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11475. if (!cursor)
  11476. goto fail;
  11477. state = intel_create_plane_state(&cursor->base);
  11478. if (!state)
  11479. goto fail;
  11480. cursor->base.state = &state->base;
  11481. cursor->can_scale = false;
  11482. cursor->max_downscale = 1;
  11483. cursor->pipe = pipe;
  11484. cursor->plane = pipe;
  11485. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11486. cursor->check_plane = intel_check_cursor_plane;
  11487. cursor->update_plane = intel_update_cursor_plane;
  11488. cursor->disable_plane = intel_disable_cursor_plane;
  11489. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  11490. &intel_plane_funcs,
  11491. intel_cursor_formats,
  11492. ARRAY_SIZE(intel_cursor_formats),
  11493. DRM_PLANE_TYPE_CURSOR, NULL);
  11494. if (ret)
  11495. goto fail;
  11496. if (INTEL_INFO(dev)->gen >= 4) {
  11497. if (!dev->mode_config.rotation_property)
  11498. dev->mode_config.rotation_property =
  11499. drm_mode_create_rotation_property(dev,
  11500. BIT(DRM_ROTATE_0) |
  11501. BIT(DRM_ROTATE_180));
  11502. if (dev->mode_config.rotation_property)
  11503. drm_object_attach_property(&cursor->base.base,
  11504. dev->mode_config.rotation_property,
  11505. state->base.rotation);
  11506. }
  11507. if (INTEL_INFO(dev)->gen >=9)
  11508. state->scaler_id = -1;
  11509. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11510. return &cursor->base;
  11511. fail:
  11512. kfree(state);
  11513. kfree(cursor);
  11514. return NULL;
  11515. }
  11516. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11517. struct intel_crtc_state *crtc_state)
  11518. {
  11519. int i;
  11520. struct intel_scaler *intel_scaler;
  11521. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11522. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11523. intel_scaler = &scaler_state->scalers[i];
  11524. intel_scaler->in_use = 0;
  11525. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11526. }
  11527. scaler_state->scaler_id = -1;
  11528. }
  11529. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11530. {
  11531. struct drm_i915_private *dev_priv = dev->dev_private;
  11532. struct intel_crtc *intel_crtc;
  11533. struct intel_crtc_state *crtc_state = NULL;
  11534. struct drm_plane *primary = NULL;
  11535. struct drm_plane *cursor = NULL;
  11536. int ret;
  11537. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11538. if (intel_crtc == NULL)
  11539. return;
  11540. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11541. if (!crtc_state)
  11542. goto fail;
  11543. intel_crtc->config = crtc_state;
  11544. intel_crtc->base.state = &crtc_state->base;
  11545. crtc_state->base.crtc = &intel_crtc->base;
  11546. INIT_LIST_HEAD(&intel_crtc->flip_work);
  11547. /* initialize shared scalers */
  11548. if (INTEL_INFO(dev)->gen >= 9) {
  11549. if (pipe == PIPE_C)
  11550. intel_crtc->num_scalers = 1;
  11551. else
  11552. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11553. skl_init_scalers(dev, intel_crtc, crtc_state);
  11554. }
  11555. primary = intel_primary_plane_create(dev, pipe);
  11556. if (!primary)
  11557. goto fail;
  11558. cursor = intel_cursor_plane_create(dev, pipe);
  11559. if (!cursor)
  11560. goto fail;
  11561. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11562. cursor, &intel_crtc_funcs, NULL);
  11563. if (ret)
  11564. goto fail;
  11565. /*
  11566. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11567. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11568. */
  11569. intel_crtc->pipe = pipe;
  11570. intel_crtc->plane = pipe;
  11571. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11572. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11573. intel_crtc->plane = !pipe;
  11574. }
  11575. intel_crtc->cursor_base = ~0;
  11576. intel_crtc->cursor_cntl = ~0;
  11577. intel_crtc->cursor_size = ~0;
  11578. intel_crtc->wm.cxsr_allowed = true;
  11579. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11580. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11581. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11582. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11583. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11584. intel_color_init(&intel_crtc->base);
  11585. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11586. return;
  11587. fail:
  11588. if (primary)
  11589. drm_plane_cleanup(primary);
  11590. if (cursor)
  11591. drm_plane_cleanup(cursor);
  11592. kfree(crtc_state);
  11593. kfree(intel_crtc);
  11594. }
  11595. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11596. {
  11597. struct drm_encoder *encoder = connector->base.encoder;
  11598. struct drm_device *dev = connector->base.dev;
  11599. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11600. if (!encoder || WARN_ON(!encoder->crtc))
  11601. return INVALID_PIPE;
  11602. return to_intel_crtc(encoder->crtc)->pipe;
  11603. }
  11604. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11605. struct drm_file *file)
  11606. {
  11607. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11608. struct drm_crtc *drmmode_crtc;
  11609. struct intel_crtc *crtc;
  11610. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11611. if (!drmmode_crtc) {
  11612. DRM_ERROR("no such CRTC id\n");
  11613. return -ENOENT;
  11614. }
  11615. crtc = to_intel_crtc(drmmode_crtc);
  11616. pipe_from_crtc_id->pipe = crtc->pipe;
  11617. return 0;
  11618. }
  11619. static int intel_encoder_clones(struct intel_encoder *encoder)
  11620. {
  11621. struct drm_device *dev = encoder->base.dev;
  11622. struct intel_encoder *source_encoder;
  11623. int index_mask = 0;
  11624. int entry = 0;
  11625. for_each_intel_encoder(dev, source_encoder) {
  11626. if (encoders_cloneable(encoder, source_encoder))
  11627. index_mask |= (1 << entry);
  11628. entry++;
  11629. }
  11630. return index_mask;
  11631. }
  11632. static bool has_edp_a(struct drm_device *dev)
  11633. {
  11634. struct drm_i915_private *dev_priv = dev->dev_private;
  11635. if (!IS_MOBILE(dev))
  11636. return false;
  11637. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11638. return false;
  11639. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11640. return false;
  11641. return true;
  11642. }
  11643. static bool intel_crt_present(struct drm_device *dev)
  11644. {
  11645. struct drm_i915_private *dev_priv = dev->dev_private;
  11646. if (INTEL_INFO(dev)->gen >= 9)
  11647. return false;
  11648. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11649. return false;
  11650. if (IS_CHERRYVIEW(dev))
  11651. return false;
  11652. if (HAS_PCH_LPT_H(dev) && I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11653. return false;
  11654. /* DDI E can't be used if DDI A requires 4 lanes */
  11655. if (HAS_DDI(dev) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11656. return false;
  11657. if (!dev_priv->vbt.int_crt_support)
  11658. return false;
  11659. return true;
  11660. }
  11661. static void intel_setup_outputs(struct drm_device *dev)
  11662. {
  11663. struct drm_i915_private *dev_priv = dev->dev_private;
  11664. struct intel_encoder *encoder;
  11665. bool dpd_is_edp = false;
  11666. intel_lvds_init(dev);
  11667. if (intel_crt_present(dev))
  11668. intel_crt_init(dev);
  11669. if (IS_BROXTON(dev)) {
  11670. /*
  11671. * FIXME: Broxton doesn't support port detection via the
  11672. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11673. * detect the ports.
  11674. */
  11675. intel_ddi_init(dev, PORT_A);
  11676. intel_ddi_init(dev, PORT_B);
  11677. intel_ddi_init(dev, PORT_C);
  11678. intel_dsi_init(dev);
  11679. } else if (HAS_DDI(dev)) {
  11680. int found;
  11681. /*
  11682. * Haswell uses DDI functions to detect digital outputs.
  11683. * On SKL pre-D0 the strap isn't connected, so we assume
  11684. * it's there.
  11685. */
  11686. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11687. /* WaIgnoreDDIAStrap: skl */
  11688. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  11689. intel_ddi_init(dev, PORT_A);
  11690. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11691. * register */
  11692. found = I915_READ(SFUSE_STRAP);
  11693. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11694. intel_ddi_init(dev, PORT_B);
  11695. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11696. intel_ddi_init(dev, PORT_C);
  11697. if (found & SFUSE_STRAP_DDID_DETECTED)
  11698. intel_ddi_init(dev, PORT_D);
  11699. /*
  11700. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11701. */
  11702. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  11703. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11704. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11705. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11706. intel_ddi_init(dev, PORT_E);
  11707. } else if (HAS_PCH_SPLIT(dev)) {
  11708. int found;
  11709. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11710. if (has_edp_a(dev))
  11711. intel_dp_init(dev, DP_A, PORT_A);
  11712. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11713. /* PCH SDVOB multiplex with HDMIB */
  11714. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  11715. if (!found)
  11716. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11717. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11718. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11719. }
  11720. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11721. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11722. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11723. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11724. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11725. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11726. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11727. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11728. } else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
  11729. /*
  11730. * The DP_DETECTED bit is the latched state of the DDC
  11731. * SDA pin at boot. However since eDP doesn't require DDC
  11732. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11733. * eDP ports may have been muxed to an alternate function.
  11734. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11735. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11736. * detect eDP ports.
  11737. */
  11738. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11739. !intel_dp_is_edp(dev, PORT_B))
  11740. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11741. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11742. intel_dp_is_edp(dev, PORT_B))
  11743. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11744. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11745. !intel_dp_is_edp(dev, PORT_C))
  11746. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11747. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11748. intel_dp_is_edp(dev, PORT_C))
  11749. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11750. if (IS_CHERRYVIEW(dev)) {
  11751. /* eDP not supported on port D, so don't check VBT */
  11752. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11753. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11754. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11755. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11756. }
  11757. intel_dsi_init(dev);
  11758. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11759. bool found = false;
  11760. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11761. DRM_DEBUG_KMS("probing SDVOB\n");
  11762. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  11763. if (!found && IS_G4X(dev)) {
  11764. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11765. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11766. }
  11767. if (!found && IS_G4X(dev))
  11768. intel_dp_init(dev, DP_B, PORT_B);
  11769. }
  11770. /* Before G4X SDVOC doesn't have its own detect register */
  11771. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11772. DRM_DEBUG_KMS("probing SDVOC\n");
  11773. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  11774. }
  11775. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11776. if (IS_G4X(dev)) {
  11777. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11778. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11779. }
  11780. if (IS_G4X(dev))
  11781. intel_dp_init(dev, DP_C, PORT_C);
  11782. }
  11783. if (IS_G4X(dev) &&
  11784. (I915_READ(DP_D) & DP_DETECTED))
  11785. intel_dp_init(dev, DP_D, PORT_D);
  11786. } else if (IS_GEN2(dev))
  11787. intel_dvo_init(dev);
  11788. if (SUPPORTS_TV(dev))
  11789. intel_tv_init(dev);
  11790. intel_psr_init(dev);
  11791. for_each_intel_encoder(dev, encoder) {
  11792. encoder->base.possible_crtcs = encoder->crtc_mask;
  11793. encoder->base.possible_clones =
  11794. intel_encoder_clones(encoder);
  11795. }
  11796. intel_init_pch_refclk(dev);
  11797. drm_helper_move_panel_connectors_to_head(dev);
  11798. }
  11799. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11800. {
  11801. struct drm_device *dev = fb->dev;
  11802. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11803. drm_framebuffer_cleanup(fb);
  11804. mutex_lock(&dev->struct_mutex);
  11805. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11806. drm_gem_object_unreference(&intel_fb->obj->base);
  11807. mutex_unlock(&dev->struct_mutex);
  11808. kfree(intel_fb);
  11809. }
  11810. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11811. struct drm_file *file,
  11812. unsigned int *handle)
  11813. {
  11814. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11815. struct drm_i915_gem_object *obj = intel_fb->obj;
  11816. if (obj->userptr.mm) {
  11817. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11818. return -EINVAL;
  11819. }
  11820. return drm_gem_handle_create(file, &obj->base, handle);
  11821. }
  11822. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11823. struct drm_file *file,
  11824. unsigned flags, unsigned color,
  11825. struct drm_clip_rect *clips,
  11826. unsigned num_clips)
  11827. {
  11828. struct drm_device *dev = fb->dev;
  11829. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11830. struct drm_i915_gem_object *obj = intel_fb->obj;
  11831. mutex_lock(&dev->struct_mutex);
  11832. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  11833. mutex_unlock(&dev->struct_mutex);
  11834. return 0;
  11835. }
  11836. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11837. .destroy = intel_user_framebuffer_destroy,
  11838. .create_handle = intel_user_framebuffer_create_handle,
  11839. .dirty = intel_user_framebuffer_dirty,
  11840. };
  11841. static
  11842. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  11843. uint32_t pixel_format)
  11844. {
  11845. u32 gen = INTEL_INFO(dev)->gen;
  11846. if (gen >= 9) {
  11847. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11848. /* "The stride in bytes must not exceed the of the size of 8K
  11849. * pixels and 32K bytes."
  11850. */
  11851. return min(8192 * cpp, 32768);
  11852. } else if (gen >= 5 && !IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  11853. return 32*1024;
  11854. } else if (gen >= 4) {
  11855. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11856. return 16*1024;
  11857. else
  11858. return 32*1024;
  11859. } else if (gen >= 3) {
  11860. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11861. return 8*1024;
  11862. else
  11863. return 16*1024;
  11864. } else {
  11865. /* XXX DSPC is limited to 4k tiled */
  11866. return 8*1024;
  11867. }
  11868. }
  11869. static int intel_framebuffer_init(struct drm_device *dev,
  11870. struct intel_framebuffer *intel_fb,
  11871. struct drm_mode_fb_cmd2 *mode_cmd,
  11872. struct drm_i915_gem_object *obj)
  11873. {
  11874. struct drm_i915_private *dev_priv = to_i915(dev);
  11875. unsigned int aligned_height;
  11876. int ret;
  11877. u32 pitch_limit, stride_alignment;
  11878. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  11879. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11880. /* Enforce that fb modifier and tiling mode match, but only for
  11881. * X-tiled. This is needed for FBC. */
  11882. if (!!(obj->tiling_mode == I915_TILING_X) !=
  11883. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  11884. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11885. return -EINVAL;
  11886. }
  11887. } else {
  11888. if (obj->tiling_mode == I915_TILING_X)
  11889. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11890. else if (obj->tiling_mode == I915_TILING_Y) {
  11891. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11892. return -EINVAL;
  11893. }
  11894. }
  11895. /* Passed in modifier sanity checking. */
  11896. switch (mode_cmd->modifier[0]) {
  11897. case I915_FORMAT_MOD_Y_TILED:
  11898. case I915_FORMAT_MOD_Yf_TILED:
  11899. if (INTEL_INFO(dev)->gen < 9) {
  11900. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11901. mode_cmd->modifier[0]);
  11902. return -EINVAL;
  11903. }
  11904. case DRM_FORMAT_MOD_NONE:
  11905. case I915_FORMAT_MOD_X_TILED:
  11906. break;
  11907. default:
  11908. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11909. mode_cmd->modifier[0]);
  11910. return -EINVAL;
  11911. }
  11912. stride_alignment = intel_fb_stride_alignment(dev_priv,
  11913. mode_cmd->modifier[0],
  11914. mode_cmd->pixel_format);
  11915. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11916. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11917. mode_cmd->pitches[0], stride_alignment);
  11918. return -EINVAL;
  11919. }
  11920. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  11921. mode_cmd->pixel_format);
  11922. if (mode_cmd->pitches[0] > pitch_limit) {
  11923. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11924. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11925. "tiled" : "linear",
  11926. mode_cmd->pitches[0], pitch_limit);
  11927. return -EINVAL;
  11928. }
  11929. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  11930. mode_cmd->pitches[0] != obj->stride) {
  11931. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11932. mode_cmd->pitches[0], obj->stride);
  11933. return -EINVAL;
  11934. }
  11935. /* Reject formats not supported by any plane early. */
  11936. switch (mode_cmd->pixel_format) {
  11937. case DRM_FORMAT_C8:
  11938. case DRM_FORMAT_RGB565:
  11939. case DRM_FORMAT_XRGB8888:
  11940. case DRM_FORMAT_ARGB8888:
  11941. break;
  11942. case DRM_FORMAT_XRGB1555:
  11943. if (INTEL_INFO(dev)->gen > 3) {
  11944. DRM_DEBUG("unsupported pixel format: %s\n",
  11945. drm_get_format_name(mode_cmd->pixel_format));
  11946. return -EINVAL;
  11947. }
  11948. break;
  11949. case DRM_FORMAT_ABGR8888:
  11950. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
  11951. INTEL_INFO(dev)->gen < 9) {
  11952. DRM_DEBUG("unsupported pixel format: %s\n",
  11953. drm_get_format_name(mode_cmd->pixel_format));
  11954. return -EINVAL;
  11955. }
  11956. break;
  11957. case DRM_FORMAT_XBGR8888:
  11958. case DRM_FORMAT_XRGB2101010:
  11959. case DRM_FORMAT_XBGR2101010:
  11960. if (INTEL_INFO(dev)->gen < 4) {
  11961. DRM_DEBUG("unsupported pixel format: %s\n",
  11962. drm_get_format_name(mode_cmd->pixel_format));
  11963. return -EINVAL;
  11964. }
  11965. break;
  11966. case DRM_FORMAT_ABGR2101010:
  11967. if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev)) {
  11968. DRM_DEBUG("unsupported pixel format: %s\n",
  11969. drm_get_format_name(mode_cmd->pixel_format));
  11970. return -EINVAL;
  11971. }
  11972. break;
  11973. case DRM_FORMAT_YUYV:
  11974. case DRM_FORMAT_UYVY:
  11975. case DRM_FORMAT_YVYU:
  11976. case DRM_FORMAT_VYUY:
  11977. if (INTEL_INFO(dev)->gen < 5) {
  11978. DRM_DEBUG("unsupported pixel format: %s\n",
  11979. drm_get_format_name(mode_cmd->pixel_format));
  11980. return -EINVAL;
  11981. }
  11982. break;
  11983. default:
  11984. DRM_DEBUG("unsupported pixel format: %s\n",
  11985. drm_get_format_name(mode_cmd->pixel_format));
  11986. return -EINVAL;
  11987. }
  11988. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  11989. if (mode_cmd->offsets[0] != 0)
  11990. return -EINVAL;
  11991. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  11992. mode_cmd->pixel_format,
  11993. mode_cmd->modifier[0]);
  11994. /* FIXME drm helper for size checks (especially planar formats)? */
  11995. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  11996. return -EINVAL;
  11997. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  11998. intel_fb->obj = obj;
  11999. intel_fill_fb_info(dev_priv, &intel_fb->base);
  12000. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12001. if (ret) {
  12002. DRM_ERROR("framebuffer init failed %d\n", ret);
  12003. return ret;
  12004. }
  12005. intel_fb->obj->framebuffer_references++;
  12006. return 0;
  12007. }
  12008. static struct drm_framebuffer *
  12009. intel_user_framebuffer_create(struct drm_device *dev,
  12010. struct drm_file *filp,
  12011. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12012. {
  12013. struct drm_framebuffer *fb;
  12014. struct drm_i915_gem_object *obj;
  12015. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12016. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12017. mode_cmd.handles[0]));
  12018. if (&obj->base == NULL)
  12019. return ERR_PTR(-ENOENT);
  12020. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  12021. if (IS_ERR(fb))
  12022. drm_gem_object_unreference_unlocked(&obj->base);
  12023. return fb;
  12024. }
  12025. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12026. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12027. {
  12028. }
  12029. #endif
  12030. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12031. .fb_create = intel_user_framebuffer_create,
  12032. .output_poll_changed = intel_fbdev_output_poll_changed,
  12033. .atomic_check = intel_atomic_check,
  12034. .atomic_commit = intel_atomic_commit,
  12035. .atomic_state_alloc = intel_atomic_state_alloc,
  12036. .atomic_state_clear = intel_atomic_state_clear,
  12037. };
  12038. /**
  12039. * intel_init_display_hooks - initialize the display modesetting hooks
  12040. * @dev_priv: device private
  12041. */
  12042. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12043. {
  12044. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12045. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12046. dev_priv->display.get_initial_plane_config =
  12047. skylake_get_initial_plane_config;
  12048. dev_priv->display.crtc_compute_clock =
  12049. haswell_crtc_compute_clock;
  12050. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12051. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12052. } else if (HAS_DDI(dev_priv)) {
  12053. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12054. dev_priv->display.get_initial_plane_config =
  12055. ironlake_get_initial_plane_config;
  12056. dev_priv->display.crtc_compute_clock =
  12057. haswell_crtc_compute_clock;
  12058. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12059. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12060. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12061. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12062. dev_priv->display.get_initial_plane_config =
  12063. ironlake_get_initial_plane_config;
  12064. dev_priv->display.crtc_compute_clock =
  12065. ironlake_crtc_compute_clock;
  12066. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12067. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12068. } else if (IS_CHERRYVIEW(dev_priv)) {
  12069. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12070. dev_priv->display.get_initial_plane_config =
  12071. i9xx_get_initial_plane_config;
  12072. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12073. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12074. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12075. } else if (IS_VALLEYVIEW(dev_priv)) {
  12076. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12077. dev_priv->display.get_initial_plane_config =
  12078. i9xx_get_initial_plane_config;
  12079. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12080. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12081. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12082. } else if (IS_G4X(dev_priv)) {
  12083. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12084. dev_priv->display.get_initial_plane_config =
  12085. i9xx_get_initial_plane_config;
  12086. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12087. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12088. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12089. } else if (IS_PINEVIEW(dev_priv)) {
  12090. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12091. dev_priv->display.get_initial_plane_config =
  12092. i9xx_get_initial_plane_config;
  12093. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12094. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12095. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12096. } else if (!IS_GEN2(dev_priv)) {
  12097. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12098. dev_priv->display.get_initial_plane_config =
  12099. i9xx_get_initial_plane_config;
  12100. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12101. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12102. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12103. } else {
  12104. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12105. dev_priv->display.get_initial_plane_config =
  12106. i9xx_get_initial_plane_config;
  12107. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12108. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12109. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12110. }
  12111. /* Returns the core display clock speed */
  12112. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12113. dev_priv->display.get_display_clock_speed =
  12114. skylake_get_display_clock_speed;
  12115. else if (IS_BROXTON(dev_priv))
  12116. dev_priv->display.get_display_clock_speed =
  12117. broxton_get_display_clock_speed;
  12118. else if (IS_BROADWELL(dev_priv))
  12119. dev_priv->display.get_display_clock_speed =
  12120. broadwell_get_display_clock_speed;
  12121. else if (IS_HASWELL(dev_priv))
  12122. dev_priv->display.get_display_clock_speed =
  12123. haswell_get_display_clock_speed;
  12124. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12125. dev_priv->display.get_display_clock_speed =
  12126. valleyview_get_display_clock_speed;
  12127. else if (IS_GEN5(dev_priv))
  12128. dev_priv->display.get_display_clock_speed =
  12129. ilk_get_display_clock_speed;
  12130. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  12131. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  12132. dev_priv->display.get_display_clock_speed =
  12133. i945_get_display_clock_speed;
  12134. else if (IS_GM45(dev_priv))
  12135. dev_priv->display.get_display_clock_speed =
  12136. gm45_get_display_clock_speed;
  12137. else if (IS_CRESTLINE(dev_priv))
  12138. dev_priv->display.get_display_clock_speed =
  12139. i965gm_get_display_clock_speed;
  12140. else if (IS_PINEVIEW(dev_priv))
  12141. dev_priv->display.get_display_clock_speed =
  12142. pnv_get_display_clock_speed;
  12143. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  12144. dev_priv->display.get_display_clock_speed =
  12145. g33_get_display_clock_speed;
  12146. else if (IS_I915G(dev_priv))
  12147. dev_priv->display.get_display_clock_speed =
  12148. i915_get_display_clock_speed;
  12149. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  12150. dev_priv->display.get_display_clock_speed =
  12151. i9xx_misc_get_display_clock_speed;
  12152. else if (IS_I915GM(dev_priv))
  12153. dev_priv->display.get_display_clock_speed =
  12154. i915gm_get_display_clock_speed;
  12155. else if (IS_I865G(dev_priv))
  12156. dev_priv->display.get_display_clock_speed =
  12157. i865_get_display_clock_speed;
  12158. else if (IS_I85X(dev_priv))
  12159. dev_priv->display.get_display_clock_speed =
  12160. i85x_get_display_clock_speed;
  12161. else { /* 830 */
  12162. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12163. dev_priv->display.get_display_clock_speed =
  12164. i830_get_display_clock_speed;
  12165. }
  12166. if (IS_GEN5(dev_priv)) {
  12167. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12168. } else if (IS_GEN6(dev_priv)) {
  12169. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12170. } else if (IS_IVYBRIDGE(dev_priv)) {
  12171. /* FIXME: detect B0+ stepping and use auto training */
  12172. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12173. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12174. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12175. }
  12176. if (IS_BROADWELL(dev_priv)) {
  12177. dev_priv->display.modeset_commit_cdclk =
  12178. broadwell_modeset_commit_cdclk;
  12179. dev_priv->display.modeset_calc_cdclk =
  12180. broadwell_modeset_calc_cdclk;
  12181. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12182. dev_priv->display.modeset_commit_cdclk =
  12183. valleyview_modeset_commit_cdclk;
  12184. dev_priv->display.modeset_calc_cdclk =
  12185. valleyview_modeset_calc_cdclk;
  12186. } else if (IS_BROXTON(dev_priv)) {
  12187. dev_priv->display.modeset_commit_cdclk =
  12188. broxton_modeset_commit_cdclk;
  12189. dev_priv->display.modeset_calc_cdclk =
  12190. broxton_modeset_calc_cdclk;
  12191. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  12192. dev_priv->display.modeset_commit_cdclk =
  12193. skl_modeset_commit_cdclk;
  12194. dev_priv->display.modeset_calc_cdclk =
  12195. skl_modeset_calc_cdclk;
  12196. }
  12197. }
  12198. /*
  12199. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12200. * resume, or other times. This quirk makes sure that's the case for
  12201. * affected systems.
  12202. */
  12203. static void quirk_pipea_force(struct drm_device *dev)
  12204. {
  12205. struct drm_i915_private *dev_priv = dev->dev_private;
  12206. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12207. DRM_INFO("applying pipe a force quirk\n");
  12208. }
  12209. static void quirk_pipeb_force(struct drm_device *dev)
  12210. {
  12211. struct drm_i915_private *dev_priv = dev->dev_private;
  12212. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12213. DRM_INFO("applying pipe b force quirk\n");
  12214. }
  12215. /*
  12216. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12217. */
  12218. static void quirk_ssc_force_disable(struct drm_device *dev)
  12219. {
  12220. struct drm_i915_private *dev_priv = dev->dev_private;
  12221. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12222. DRM_INFO("applying lvds SSC disable quirk\n");
  12223. }
  12224. /*
  12225. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12226. * brightness value
  12227. */
  12228. static void quirk_invert_brightness(struct drm_device *dev)
  12229. {
  12230. struct drm_i915_private *dev_priv = dev->dev_private;
  12231. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12232. DRM_INFO("applying inverted panel brightness quirk\n");
  12233. }
  12234. /* Some VBT's incorrectly indicate no backlight is present */
  12235. static void quirk_backlight_present(struct drm_device *dev)
  12236. {
  12237. struct drm_i915_private *dev_priv = dev->dev_private;
  12238. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12239. DRM_INFO("applying backlight present quirk\n");
  12240. }
  12241. struct intel_quirk {
  12242. int device;
  12243. int subsystem_vendor;
  12244. int subsystem_device;
  12245. void (*hook)(struct drm_device *dev);
  12246. };
  12247. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12248. struct intel_dmi_quirk {
  12249. void (*hook)(struct drm_device *dev);
  12250. const struct dmi_system_id (*dmi_id_list)[];
  12251. };
  12252. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12253. {
  12254. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12255. return 1;
  12256. }
  12257. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12258. {
  12259. .dmi_id_list = &(const struct dmi_system_id[]) {
  12260. {
  12261. .callback = intel_dmi_reverse_brightness,
  12262. .ident = "NCR Corporation",
  12263. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12264. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12265. },
  12266. },
  12267. { } /* terminating entry */
  12268. },
  12269. .hook = quirk_invert_brightness,
  12270. },
  12271. };
  12272. static struct intel_quirk intel_quirks[] = {
  12273. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12274. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12275. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12276. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12277. /* 830 needs to leave pipe A & dpll A up */
  12278. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12279. /* 830 needs to leave pipe B & dpll B up */
  12280. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12281. /* Lenovo U160 cannot use SSC on LVDS */
  12282. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12283. /* Sony Vaio Y cannot use SSC on LVDS */
  12284. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12285. /* Acer Aspire 5734Z must invert backlight brightness */
  12286. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12287. /* Acer/eMachines G725 */
  12288. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12289. /* Acer/eMachines e725 */
  12290. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12291. /* Acer/Packard Bell NCL20 */
  12292. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12293. /* Acer Aspire 4736Z */
  12294. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12295. /* Acer Aspire 5336 */
  12296. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12297. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12298. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12299. /* Acer C720 Chromebook (Core i3 4005U) */
  12300. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12301. /* Apple Macbook 2,1 (Core 2 T7400) */
  12302. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12303. /* Apple Macbook 4,1 */
  12304. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12305. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12306. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12307. /* HP Chromebook 14 (Celeron 2955U) */
  12308. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12309. /* Dell Chromebook 11 */
  12310. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12311. /* Dell Chromebook 11 (2015 version) */
  12312. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12313. };
  12314. static void intel_init_quirks(struct drm_device *dev)
  12315. {
  12316. struct pci_dev *d = dev->pdev;
  12317. int i;
  12318. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12319. struct intel_quirk *q = &intel_quirks[i];
  12320. if (d->device == q->device &&
  12321. (d->subsystem_vendor == q->subsystem_vendor ||
  12322. q->subsystem_vendor == PCI_ANY_ID) &&
  12323. (d->subsystem_device == q->subsystem_device ||
  12324. q->subsystem_device == PCI_ANY_ID))
  12325. q->hook(dev);
  12326. }
  12327. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12328. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12329. intel_dmi_quirks[i].hook(dev);
  12330. }
  12331. }
  12332. /* Disable the VGA plane that we never use */
  12333. static void i915_disable_vga(struct drm_device *dev)
  12334. {
  12335. struct drm_i915_private *dev_priv = dev->dev_private;
  12336. u8 sr1;
  12337. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12338. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12339. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12340. outb(SR01, VGA_SR_INDEX);
  12341. sr1 = inb(VGA_SR_DATA);
  12342. outb(sr1 | 1<<5, VGA_SR_DATA);
  12343. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12344. udelay(300);
  12345. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12346. POSTING_READ(vga_reg);
  12347. }
  12348. void intel_modeset_init_hw(struct drm_device *dev)
  12349. {
  12350. struct drm_i915_private *dev_priv = dev->dev_private;
  12351. intel_update_cdclk(dev);
  12352. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  12353. intel_init_clock_gating(dev);
  12354. intel_enable_gt_powersave(dev_priv);
  12355. }
  12356. /*
  12357. * Calculate what we think the watermarks should be for the state we've read
  12358. * out of the hardware and then immediately program those watermarks so that
  12359. * we ensure the hardware settings match our internal state.
  12360. *
  12361. * We can calculate what we think WM's should be by creating a duplicate of the
  12362. * current state (which was constructed during hardware readout) and running it
  12363. * through the atomic check code to calculate new watermark values in the
  12364. * state object.
  12365. */
  12366. static void sanitize_watermarks(struct drm_device *dev)
  12367. {
  12368. struct drm_i915_private *dev_priv = to_i915(dev);
  12369. struct drm_atomic_state *state;
  12370. struct drm_crtc *crtc;
  12371. struct drm_crtc_state *cstate;
  12372. struct drm_modeset_acquire_ctx ctx;
  12373. int ret;
  12374. int i;
  12375. /* Only supported on platforms that use atomic watermark design */
  12376. if (!dev_priv->display.optimize_watermarks)
  12377. return;
  12378. /*
  12379. * We need to hold connection_mutex before calling duplicate_state so
  12380. * that the connector loop is protected.
  12381. */
  12382. drm_modeset_acquire_init(&ctx, 0);
  12383. retry:
  12384. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12385. if (ret == -EDEADLK) {
  12386. drm_modeset_backoff(&ctx);
  12387. goto retry;
  12388. } else if (WARN_ON(ret)) {
  12389. goto fail;
  12390. }
  12391. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12392. if (WARN_ON(IS_ERR(state)))
  12393. goto fail;
  12394. /*
  12395. * Hardware readout is the only time we don't want to calculate
  12396. * intermediate watermarks (since we don't trust the current
  12397. * watermarks).
  12398. */
  12399. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12400. ret = intel_atomic_check(dev, state);
  12401. if (ret) {
  12402. /*
  12403. * If we fail here, it means that the hardware appears to be
  12404. * programmed in a way that shouldn't be possible, given our
  12405. * understanding of watermark requirements. This might mean a
  12406. * mistake in the hardware readout code or a mistake in the
  12407. * watermark calculations for a given platform. Raise a WARN
  12408. * so that this is noticeable.
  12409. *
  12410. * If this actually happens, we'll have to just leave the
  12411. * BIOS-programmed watermarks untouched and hope for the best.
  12412. */
  12413. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12414. goto fail;
  12415. }
  12416. /* Write calculated watermark values back */
  12417. for_each_crtc_in_state(state, crtc, cstate, i) {
  12418. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12419. cs->wm.need_postvbl_update = true;
  12420. dev_priv->display.optimize_watermarks(cs);
  12421. }
  12422. drm_atomic_state_free(state);
  12423. fail:
  12424. drm_modeset_drop_locks(&ctx);
  12425. drm_modeset_acquire_fini(&ctx);
  12426. }
  12427. void intel_modeset_init(struct drm_device *dev)
  12428. {
  12429. struct drm_i915_private *dev_priv = to_i915(dev);
  12430. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12431. int sprite, ret;
  12432. enum pipe pipe;
  12433. struct intel_crtc *crtc;
  12434. drm_mode_config_init(dev);
  12435. dev->mode_config.min_width = 0;
  12436. dev->mode_config.min_height = 0;
  12437. dev->mode_config.preferred_depth = 24;
  12438. dev->mode_config.prefer_shadow = 1;
  12439. dev->mode_config.allow_fb_modifiers = true;
  12440. dev->mode_config.funcs = &intel_mode_funcs;
  12441. intel_init_quirks(dev);
  12442. intel_init_pm(dev);
  12443. if (INTEL_INFO(dev)->num_pipes == 0)
  12444. return;
  12445. /*
  12446. * There may be no VBT; and if the BIOS enabled SSC we can
  12447. * just keep using it to avoid unnecessary flicker. Whereas if the
  12448. * BIOS isn't using it, don't assume it will work even if the VBT
  12449. * indicates as much.
  12450. */
  12451. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12452. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12453. DREF_SSC1_ENABLE);
  12454. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12455. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12456. bios_lvds_use_ssc ? "en" : "dis",
  12457. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12458. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12459. }
  12460. }
  12461. if (IS_GEN2(dev)) {
  12462. dev->mode_config.max_width = 2048;
  12463. dev->mode_config.max_height = 2048;
  12464. } else if (IS_GEN3(dev)) {
  12465. dev->mode_config.max_width = 4096;
  12466. dev->mode_config.max_height = 4096;
  12467. } else {
  12468. dev->mode_config.max_width = 8192;
  12469. dev->mode_config.max_height = 8192;
  12470. }
  12471. if (IS_845G(dev) || IS_I865G(dev)) {
  12472. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12473. dev->mode_config.cursor_height = 1023;
  12474. } else if (IS_GEN2(dev)) {
  12475. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12476. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12477. } else {
  12478. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12479. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12480. }
  12481. dev->mode_config.fb_base = ggtt->mappable_base;
  12482. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12483. INTEL_INFO(dev)->num_pipes,
  12484. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12485. for_each_pipe(dev_priv, pipe) {
  12486. intel_crtc_init(dev, pipe);
  12487. for_each_sprite(dev_priv, pipe, sprite) {
  12488. ret = intel_plane_init(dev, pipe, sprite);
  12489. if (ret)
  12490. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12491. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12492. }
  12493. }
  12494. intel_update_czclk(dev_priv);
  12495. intel_update_cdclk(dev);
  12496. intel_shared_dpll_init(dev);
  12497. if (dev_priv->max_cdclk_freq == 0)
  12498. intel_update_max_cdclk(dev);
  12499. /* Just disable it once at startup */
  12500. i915_disable_vga(dev);
  12501. intel_setup_outputs(dev);
  12502. drm_modeset_lock_all(dev);
  12503. intel_modeset_setup_hw_state(dev);
  12504. drm_modeset_unlock_all(dev);
  12505. for_each_intel_crtc(dev, crtc) {
  12506. struct intel_initial_plane_config plane_config = {};
  12507. if (!crtc->active)
  12508. continue;
  12509. /*
  12510. * Note that reserving the BIOS fb up front prevents us
  12511. * from stuffing other stolen allocations like the ring
  12512. * on top. This prevents some ugliness at boot time, and
  12513. * can even allow for smooth boot transitions if the BIOS
  12514. * fb is large enough for the active pipe configuration.
  12515. */
  12516. dev_priv->display.get_initial_plane_config(crtc,
  12517. &plane_config);
  12518. /*
  12519. * If the fb is shared between multiple heads, we'll
  12520. * just get the first one.
  12521. */
  12522. intel_find_initial_plane_obj(crtc, &plane_config);
  12523. }
  12524. /*
  12525. * Make sure hardware watermarks really match the state we read out.
  12526. * Note that we need to do this after reconstructing the BIOS fb's
  12527. * since the watermark calculation done here will use pstate->fb.
  12528. */
  12529. sanitize_watermarks(dev);
  12530. }
  12531. static void intel_enable_pipe_a(struct drm_device *dev)
  12532. {
  12533. struct intel_connector *connector;
  12534. struct drm_connector *crt = NULL;
  12535. struct intel_load_detect_pipe load_detect_temp;
  12536. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12537. /* We can't just switch on the pipe A, we need to set things up with a
  12538. * proper mode and output configuration. As a gross hack, enable pipe A
  12539. * by enabling the load detect pipe once. */
  12540. for_each_intel_connector(dev, connector) {
  12541. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12542. crt = &connector->base;
  12543. break;
  12544. }
  12545. }
  12546. if (!crt)
  12547. return;
  12548. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12549. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12550. }
  12551. static bool
  12552. intel_check_plane_mapping(struct intel_crtc *crtc)
  12553. {
  12554. struct drm_device *dev = crtc->base.dev;
  12555. struct drm_i915_private *dev_priv = dev->dev_private;
  12556. u32 val;
  12557. if (INTEL_INFO(dev)->num_pipes == 1)
  12558. return true;
  12559. val = I915_READ(DSPCNTR(!crtc->plane));
  12560. if ((val & DISPLAY_PLANE_ENABLE) &&
  12561. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12562. return false;
  12563. return true;
  12564. }
  12565. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12566. {
  12567. struct drm_device *dev = crtc->base.dev;
  12568. struct intel_encoder *encoder;
  12569. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12570. return true;
  12571. return false;
  12572. }
  12573. static bool intel_encoder_has_connectors(struct intel_encoder *encoder)
  12574. {
  12575. struct drm_device *dev = encoder->base.dev;
  12576. struct intel_connector *connector;
  12577. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12578. return true;
  12579. return false;
  12580. }
  12581. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12582. {
  12583. struct drm_device *dev = crtc->base.dev;
  12584. struct drm_i915_private *dev_priv = dev->dev_private;
  12585. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12586. /* Clear any frame start delays used for debugging left by the BIOS */
  12587. if (!transcoder_is_dsi(cpu_transcoder)) {
  12588. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12589. I915_WRITE(reg,
  12590. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12591. }
  12592. /* restore vblank interrupts to correct state */
  12593. drm_crtc_vblank_reset(&crtc->base);
  12594. if (crtc->active) {
  12595. struct intel_plane *plane;
  12596. drm_crtc_vblank_on(&crtc->base);
  12597. /* Disable everything but the primary plane */
  12598. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12599. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12600. continue;
  12601. plane->disable_plane(&plane->base, &crtc->base);
  12602. }
  12603. }
  12604. /* We need to sanitize the plane -> pipe mapping first because this will
  12605. * disable the crtc (and hence change the state) if it is wrong. Note
  12606. * that gen4+ has a fixed plane -> pipe mapping. */
  12607. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12608. bool plane;
  12609. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12610. crtc->base.base.id);
  12611. /* Pipe has the wrong plane attached and the plane is active.
  12612. * Temporarily change the plane mapping and disable everything
  12613. * ... */
  12614. plane = crtc->plane;
  12615. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12616. crtc->plane = !plane;
  12617. intel_crtc_disable_noatomic(&crtc->base);
  12618. crtc->plane = plane;
  12619. }
  12620. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12621. crtc->pipe == PIPE_A && !crtc->active) {
  12622. /* BIOS forgot to enable pipe A, this mostly happens after
  12623. * resume. Force-enable the pipe to fix this, the update_dpms
  12624. * call below we restore the pipe to the right state, but leave
  12625. * the required bits on. */
  12626. intel_enable_pipe_a(dev);
  12627. }
  12628. /* Adjust the state of the output pipe according to whether we
  12629. * have active connectors/encoders. */
  12630. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12631. intel_crtc_disable_noatomic(&crtc->base);
  12632. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12633. /*
  12634. * We start out with underrun reporting disabled to avoid races.
  12635. * For correct bookkeeping mark this on active crtcs.
  12636. *
  12637. * Also on gmch platforms we dont have any hardware bits to
  12638. * disable the underrun reporting. Which means we need to start
  12639. * out with underrun reporting disabled also on inactive pipes,
  12640. * since otherwise we'll complain about the garbage we read when
  12641. * e.g. coming up after runtime pm.
  12642. *
  12643. * No protection against concurrent access is required - at
  12644. * worst a fifo underrun happens which also sets this to false.
  12645. */
  12646. crtc->cpu_fifo_underrun_disabled = true;
  12647. crtc->pch_fifo_underrun_disabled = true;
  12648. }
  12649. }
  12650. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12651. {
  12652. struct intel_connector *connector;
  12653. struct drm_device *dev = encoder->base.dev;
  12654. /* We need to check both for a crtc link (meaning that the
  12655. * encoder is active and trying to read from a pipe) and the
  12656. * pipe itself being active. */
  12657. bool has_active_crtc = encoder->base.crtc &&
  12658. to_intel_crtc(encoder->base.crtc)->active;
  12659. if (intel_encoder_has_connectors(encoder) && !has_active_crtc) {
  12660. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12661. encoder->base.base.id,
  12662. encoder->base.name);
  12663. /* Connector is active, but has no active pipe. This is
  12664. * fallout from our resume register restoring. Disable
  12665. * the encoder manually again. */
  12666. if (encoder->base.crtc) {
  12667. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12668. encoder->base.base.id,
  12669. encoder->base.name);
  12670. encoder->disable(encoder);
  12671. if (encoder->post_disable)
  12672. encoder->post_disable(encoder);
  12673. }
  12674. encoder->base.crtc = NULL;
  12675. /* Inconsistent output/port/pipe state happens presumably due to
  12676. * a bug in one of the get_hw_state functions. Or someplace else
  12677. * in our code, like the register restore mess on resume. Clamp
  12678. * things to off as a safer default. */
  12679. for_each_intel_connector(dev, connector) {
  12680. if (connector->encoder != encoder)
  12681. continue;
  12682. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12683. connector->base.encoder = NULL;
  12684. }
  12685. }
  12686. /* Enabled encoders without active connectors will be fixed in
  12687. * the crtc fixup. */
  12688. }
  12689. void i915_redisable_vga_power_on(struct drm_device *dev)
  12690. {
  12691. struct drm_i915_private *dev_priv = dev->dev_private;
  12692. i915_reg_t vga_reg = i915_vgacntrl_reg(dev);
  12693. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12694. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12695. i915_disable_vga(dev);
  12696. }
  12697. }
  12698. void i915_redisable_vga(struct drm_device *dev)
  12699. {
  12700. struct drm_i915_private *dev_priv = dev->dev_private;
  12701. /* This function can be called both from intel_modeset_setup_hw_state or
  12702. * at a very early point in our resume sequence, where the power well
  12703. * structures are not yet restored. Since this function is at a very
  12704. * paranoid "someone might have enabled VGA while we were not looking"
  12705. * level, just check if the power well is enabled instead of trying to
  12706. * follow the "don't touch the power well if we don't need it" policy
  12707. * the rest of the driver uses. */
  12708. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12709. return;
  12710. i915_redisable_vga_power_on(dev);
  12711. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12712. }
  12713. static bool primary_get_hw_state(struct intel_plane *plane)
  12714. {
  12715. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12716. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12717. }
  12718. /* FIXME read out full plane state for all planes */
  12719. static void readout_plane_state(struct intel_crtc *crtc)
  12720. {
  12721. struct drm_plane *primary = crtc->base.primary;
  12722. struct intel_plane_state *plane_state =
  12723. to_intel_plane_state(primary->state);
  12724. plane_state->visible = crtc->active &&
  12725. primary_get_hw_state(to_intel_plane(primary));
  12726. if (plane_state->visible)
  12727. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12728. }
  12729. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12730. {
  12731. struct drm_i915_private *dev_priv = dev->dev_private;
  12732. enum pipe pipe;
  12733. struct intel_crtc *crtc;
  12734. struct intel_encoder *encoder;
  12735. struct intel_connector *connector;
  12736. int i;
  12737. dev_priv->active_crtcs = 0;
  12738. for_each_intel_crtc(dev, crtc) {
  12739. struct intel_crtc_state *crtc_state = crtc->config;
  12740. int pixclk = 0;
  12741. __drm_atomic_helper_crtc_destroy_state(&crtc->base, &crtc_state->base);
  12742. memset(crtc_state, 0, sizeof(*crtc_state));
  12743. crtc_state->base.crtc = &crtc->base;
  12744. crtc_state->base.active = crtc_state->base.enable =
  12745. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12746. crtc->base.enabled = crtc_state->base.enable;
  12747. crtc->active = crtc_state->base.active;
  12748. if (crtc_state->base.active) {
  12749. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12750. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  12751. pixclk = ilk_pipe_pixel_rate(crtc_state);
  12752. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12753. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  12754. else
  12755. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12756. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12757. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12758. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12759. }
  12760. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12761. readout_plane_state(crtc);
  12762. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12763. crtc->base.base.id,
  12764. crtc->active ? "enabled" : "disabled");
  12765. }
  12766. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12767. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12768. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12769. &pll->config.hw_state);
  12770. pll->config.crtc_mask = 0;
  12771. for_each_intel_crtc(dev, crtc) {
  12772. if (crtc->active && crtc->config->shared_dpll == pll)
  12773. pll->config.crtc_mask |= 1 << crtc->pipe;
  12774. }
  12775. pll->active_mask = pll->config.crtc_mask;
  12776. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12777. pll->name, pll->config.crtc_mask, pll->on);
  12778. }
  12779. for_each_intel_encoder(dev, encoder) {
  12780. pipe = 0;
  12781. if (encoder->get_hw_state(encoder, &pipe)) {
  12782. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12783. encoder->base.crtc = &crtc->base;
  12784. encoder->get_config(encoder, crtc->config);
  12785. } else {
  12786. encoder->base.crtc = NULL;
  12787. }
  12788. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12789. encoder->base.base.id,
  12790. encoder->base.name,
  12791. encoder->base.crtc ? "enabled" : "disabled",
  12792. pipe_name(pipe));
  12793. }
  12794. for_each_intel_connector(dev, connector) {
  12795. if (connector->get_hw_state(connector)) {
  12796. connector->base.dpms = DRM_MODE_DPMS_ON;
  12797. encoder = connector->encoder;
  12798. connector->base.encoder = &encoder->base;
  12799. if (encoder->base.crtc &&
  12800. encoder->base.crtc->state->active) {
  12801. /*
  12802. * This has to be done during hardware readout
  12803. * because anything calling .crtc_disable may
  12804. * rely on the connector_mask being accurate.
  12805. */
  12806. encoder->base.crtc->state->connector_mask |=
  12807. 1 << drm_connector_index(&connector->base);
  12808. encoder->base.crtc->state->encoder_mask |=
  12809. 1 << drm_encoder_index(&encoder->base);
  12810. }
  12811. } else {
  12812. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12813. connector->base.encoder = NULL;
  12814. }
  12815. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12816. connector->base.base.id,
  12817. connector->base.name,
  12818. connector->base.encoder ? "enabled" : "disabled");
  12819. }
  12820. for_each_intel_crtc(dev, crtc) {
  12821. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12822. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12823. if (crtc->base.state->active) {
  12824. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12825. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12826. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12827. /*
  12828. * The initial mode needs to be set in order to keep
  12829. * the atomic core happy. It wants a valid mode if the
  12830. * crtc's enabled, so we do the above call.
  12831. *
  12832. * At this point some state updated by the connectors
  12833. * in their ->detect() callback has not run yet, so
  12834. * no recalculation can be done yet.
  12835. *
  12836. * Even if we could do a recalculation and modeset
  12837. * right now it would cause a double modeset if
  12838. * fbdev or userspace chooses a different initial mode.
  12839. *
  12840. * If that happens, someone indicated they wanted a
  12841. * mode change, which means it's safe to do a full
  12842. * recalculation.
  12843. */
  12844. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12845. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12846. update_scanline_offset(crtc);
  12847. }
  12848. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  12849. }
  12850. }
  12851. /* Scan out the current hw modeset state,
  12852. * and sanitizes it to the current state
  12853. */
  12854. static void
  12855. intel_modeset_setup_hw_state(struct drm_device *dev)
  12856. {
  12857. struct drm_i915_private *dev_priv = dev->dev_private;
  12858. enum pipe pipe;
  12859. struct intel_crtc *crtc;
  12860. struct intel_encoder *encoder;
  12861. int i;
  12862. intel_modeset_readout_hw_state(dev);
  12863. /* HW state is read out, now we need to sanitize this mess. */
  12864. for_each_intel_encoder(dev, encoder) {
  12865. intel_sanitize_encoder(encoder);
  12866. }
  12867. for_each_pipe(dev_priv, pipe) {
  12868. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12869. intel_sanitize_crtc(crtc);
  12870. intel_dump_pipe_config(crtc, crtc->config,
  12871. "[setup_hw_state]");
  12872. }
  12873. intel_modeset_update_connector_atomic_state(dev);
  12874. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12875. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12876. if (!pll->on || pll->active_mask)
  12877. continue;
  12878. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12879. pll->funcs.disable(dev_priv, pll);
  12880. pll->on = false;
  12881. }
  12882. if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
  12883. vlv_wm_get_hw_state(dev);
  12884. else if (IS_GEN9(dev))
  12885. skl_wm_get_hw_state(dev);
  12886. else if (HAS_PCH_SPLIT(dev))
  12887. ilk_wm_get_hw_state(dev);
  12888. for_each_intel_crtc(dev, crtc) {
  12889. unsigned long put_domains;
  12890. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12891. if (WARN_ON(put_domains))
  12892. modeset_put_power_domains(dev_priv, put_domains);
  12893. }
  12894. intel_display_set_init_power(dev_priv, false);
  12895. intel_fbc_init_pipe_state(dev_priv);
  12896. }
  12897. void intel_display_resume(struct drm_device *dev)
  12898. {
  12899. struct drm_i915_private *dev_priv = to_i915(dev);
  12900. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12901. struct drm_modeset_acquire_ctx ctx;
  12902. int ret;
  12903. bool setup = false;
  12904. dev_priv->modeset_restore_state = NULL;
  12905. /*
  12906. * This is a cludge because with real atomic modeset mode_config.mutex
  12907. * won't be taken. Unfortunately some probed state like
  12908. * audio_codec_enable is still protected by mode_config.mutex, so lock
  12909. * it here for now.
  12910. */
  12911. mutex_lock(&dev->mode_config.mutex);
  12912. drm_modeset_acquire_init(&ctx, 0);
  12913. retry:
  12914. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12915. if (ret == 0 && !setup) {
  12916. setup = true;
  12917. intel_modeset_setup_hw_state(dev);
  12918. i915_redisable_vga(dev);
  12919. }
  12920. if (ret == 0 && state) {
  12921. struct drm_crtc_state *crtc_state;
  12922. struct drm_crtc *crtc;
  12923. int i;
  12924. state->acquire_ctx = &ctx;
  12925. /* ignore any reset values/BIOS leftovers in the WM registers */
  12926. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  12927. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  12928. /*
  12929. * Force recalculation even if we restore
  12930. * current state. With fast modeset this may not result
  12931. * in a modeset when the state is compatible.
  12932. */
  12933. crtc_state->mode_changed = true;
  12934. }
  12935. ret = drm_atomic_commit(state);
  12936. }
  12937. if (ret == -EDEADLK) {
  12938. drm_modeset_backoff(&ctx);
  12939. goto retry;
  12940. }
  12941. drm_modeset_drop_locks(&ctx);
  12942. drm_modeset_acquire_fini(&ctx);
  12943. mutex_unlock(&dev->mode_config.mutex);
  12944. if (ret) {
  12945. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12946. drm_atomic_state_free(state);
  12947. }
  12948. }
  12949. void intel_modeset_gem_init(struct drm_device *dev)
  12950. {
  12951. struct drm_i915_private *dev_priv = to_i915(dev);
  12952. struct drm_crtc *c;
  12953. struct drm_i915_gem_object *obj;
  12954. int ret;
  12955. intel_init_gt_powersave(dev_priv);
  12956. intel_modeset_init_hw(dev);
  12957. intel_setup_overlay(dev_priv);
  12958. /*
  12959. * Make sure any fbs we allocated at startup are properly
  12960. * pinned & fenced. When we do the allocation it's too early
  12961. * for this.
  12962. */
  12963. for_each_crtc(dev, c) {
  12964. obj = intel_fb_obj(c->primary->fb);
  12965. if (obj == NULL)
  12966. continue;
  12967. mutex_lock(&dev->struct_mutex);
  12968. ret = intel_pin_and_fence_fb_obj(c->primary->fb,
  12969. c->primary->state->rotation);
  12970. mutex_unlock(&dev->struct_mutex);
  12971. if (ret) {
  12972. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  12973. to_intel_crtc(c)->pipe);
  12974. drm_framebuffer_unreference(c->primary->fb);
  12975. drm_framebuffer_unreference(c->primary->state->fb);
  12976. c->primary->fb = c->primary->state->fb = NULL;
  12977. c->primary->crtc = c->primary->state->crtc = NULL;
  12978. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  12979. }
  12980. }
  12981. intel_backlight_register(dev);
  12982. }
  12983. void intel_connector_unregister(struct intel_connector *intel_connector)
  12984. {
  12985. struct drm_connector *connector = &intel_connector->base;
  12986. intel_panel_destroy_backlight(connector);
  12987. drm_connector_unregister(connector);
  12988. }
  12989. void intel_modeset_cleanup(struct drm_device *dev)
  12990. {
  12991. struct drm_i915_private *dev_priv = dev->dev_private;
  12992. struct intel_connector *connector;
  12993. intel_disable_gt_powersave(dev_priv);
  12994. intel_backlight_unregister(dev);
  12995. /*
  12996. * Interrupts and polling as the first thing to avoid creating havoc.
  12997. * Too much stuff here (turning of connectors, ...) would
  12998. * experience fancy races otherwise.
  12999. */
  13000. intel_irq_uninstall(dev_priv);
  13001. /*
  13002. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13003. * poll handlers. Hence disable polling after hpd handling is shut down.
  13004. */
  13005. drm_kms_helper_poll_fini(dev);
  13006. intel_unregister_dsm_handler();
  13007. intel_fbc_global_disable(dev_priv);
  13008. /* flush any delayed tasks or pending work */
  13009. flush_scheduled_work();
  13010. /* destroy the backlight and sysfs files before encoders/connectors */
  13011. for_each_intel_connector(dev, connector)
  13012. connector->unregister(connector);
  13013. drm_mode_config_cleanup(dev);
  13014. intel_cleanup_overlay(dev_priv);
  13015. intel_cleanup_gt_powersave(dev_priv);
  13016. intel_teardown_gmbus(dev);
  13017. }
  13018. /*
  13019. * Return which encoder is currently attached for connector.
  13020. */
  13021. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13022. {
  13023. return &intel_attached_encoder(connector)->base;
  13024. }
  13025. void intel_connector_attach_encoder(struct intel_connector *connector,
  13026. struct intel_encoder *encoder)
  13027. {
  13028. connector->encoder = encoder;
  13029. drm_mode_connector_attach_encoder(&connector->base,
  13030. &encoder->base);
  13031. }
  13032. /*
  13033. * set vga decode state - true == enable VGA decode
  13034. */
  13035. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13036. {
  13037. struct drm_i915_private *dev_priv = dev->dev_private;
  13038. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13039. u16 gmch_ctrl;
  13040. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13041. DRM_ERROR("failed to read control word\n");
  13042. return -EIO;
  13043. }
  13044. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13045. return 0;
  13046. if (state)
  13047. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13048. else
  13049. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13050. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13051. DRM_ERROR("failed to write control word\n");
  13052. return -EIO;
  13053. }
  13054. return 0;
  13055. }
  13056. struct intel_display_error_state {
  13057. u32 power_well_driver;
  13058. int num_transcoders;
  13059. struct intel_cursor_error_state {
  13060. u32 control;
  13061. u32 position;
  13062. u32 base;
  13063. u32 size;
  13064. } cursor[I915_MAX_PIPES];
  13065. struct intel_pipe_error_state {
  13066. bool power_domain_on;
  13067. u32 source;
  13068. u32 stat;
  13069. } pipe[I915_MAX_PIPES];
  13070. struct intel_plane_error_state {
  13071. u32 control;
  13072. u32 stride;
  13073. u32 size;
  13074. u32 pos;
  13075. u32 addr;
  13076. u32 surface;
  13077. u32 tile_offset;
  13078. } plane[I915_MAX_PIPES];
  13079. struct intel_transcoder_error_state {
  13080. bool power_domain_on;
  13081. enum transcoder cpu_transcoder;
  13082. u32 conf;
  13083. u32 htotal;
  13084. u32 hblank;
  13085. u32 hsync;
  13086. u32 vtotal;
  13087. u32 vblank;
  13088. u32 vsync;
  13089. } transcoder[4];
  13090. };
  13091. struct intel_display_error_state *
  13092. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13093. {
  13094. struct intel_display_error_state *error;
  13095. int transcoders[] = {
  13096. TRANSCODER_A,
  13097. TRANSCODER_B,
  13098. TRANSCODER_C,
  13099. TRANSCODER_EDP,
  13100. };
  13101. int i;
  13102. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13103. return NULL;
  13104. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13105. if (error == NULL)
  13106. return NULL;
  13107. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13108. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13109. for_each_pipe(dev_priv, i) {
  13110. error->pipe[i].power_domain_on =
  13111. __intel_display_power_is_enabled(dev_priv,
  13112. POWER_DOMAIN_PIPE(i));
  13113. if (!error->pipe[i].power_domain_on)
  13114. continue;
  13115. error->cursor[i].control = I915_READ(CURCNTR(i));
  13116. error->cursor[i].position = I915_READ(CURPOS(i));
  13117. error->cursor[i].base = I915_READ(CURBASE(i));
  13118. error->plane[i].control = I915_READ(DSPCNTR(i));
  13119. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13120. if (INTEL_GEN(dev_priv) <= 3) {
  13121. error->plane[i].size = I915_READ(DSPSIZE(i));
  13122. error->plane[i].pos = I915_READ(DSPPOS(i));
  13123. }
  13124. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13125. error->plane[i].addr = I915_READ(DSPADDR(i));
  13126. if (INTEL_GEN(dev_priv) >= 4) {
  13127. error->plane[i].surface = I915_READ(DSPSURF(i));
  13128. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13129. }
  13130. error->pipe[i].source = I915_READ(PIPESRC(i));
  13131. if (HAS_GMCH_DISPLAY(dev_priv))
  13132. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13133. }
  13134. /* Note: this does not include DSI transcoders. */
  13135. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13136. if (HAS_DDI(dev_priv))
  13137. error->num_transcoders++; /* Account for eDP. */
  13138. for (i = 0; i < error->num_transcoders; i++) {
  13139. enum transcoder cpu_transcoder = transcoders[i];
  13140. error->transcoder[i].power_domain_on =
  13141. __intel_display_power_is_enabled(dev_priv,
  13142. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13143. if (!error->transcoder[i].power_domain_on)
  13144. continue;
  13145. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13146. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13147. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13148. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13149. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13150. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13151. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13152. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13153. }
  13154. return error;
  13155. }
  13156. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13157. void
  13158. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13159. struct drm_device *dev,
  13160. struct intel_display_error_state *error)
  13161. {
  13162. struct drm_i915_private *dev_priv = dev->dev_private;
  13163. int i;
  13164. if (!error)
  13165. return;
  13166. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13167. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13168. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13169. error->power_well_driver);
  13170. for_each_pipe(dev_priv, i) {
  13171. err_printf(m, "Pipe [%d]:\n", i);
  13172. err_printf(m, " Power: %s\n",
  13173. onoff(error->pipe[i].power_domain_on));
  13174. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13175. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13176. err_printf(m, "Plane [%d]:\n", i);
  13177. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13178. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13179. if (INTEL_INFO(dev)->gen <= 3) {
  13180. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13181. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13182. }
  13183. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13184. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13185. if (INTEL_INFO(dev)->gen >= 4) {
  13186. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13187. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13188. }
  13189. err_printf(m, "Cursor [%d]:\n", i);
  13190. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13191. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13192. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13193. }
  13194. for (i = 0; i < error->num_transcoders; i++) {
  13195. err_printf(m, "CPU transcoder: %s\n",
  13196. transcoder_name(error->transcoder[i].cpu_transcoder));
  13197. err_printf(m, " Power: %s\n",
  13198. onoff(error->transcoder[i].power_domain_on));
  13199. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13200. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13201. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13202. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13203. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13204. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13205. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13206. }
  13207. }