intel_display.c 440 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_atomic.h>
  40. #include <drm/drm_atomic_helper.h>
  41. #include <drm/drm_dp_helper.h>
  42. #include <drm/drm_crtc_helper.h>
  43. #include <drm/drm_plane_helper.h>
  44. #include <drm/drm_rect.h>
  45. #include <linux/dma_remapping.h>
  46. /* Primary plane formats for gen <= 3 */
  47. static const uint32_t i8xx_primary_formats[] = {
  48. DRM_FORMAT_C8,
  49. DRM_FORMAT_RGB565,
  50. DRM_FORMAT_XRGB1555,
  51. DRM_FORMAT_XRGB8888,
  52. };
  53. /* Primary plane formats for gen >= 4 */
  54. static const uint32_t i965_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB8888,
  58. DRM_FORMAT_XBGR8888,
  59. DRM_FORMAT_XRGB2101010,
  60. DRM_FORMAT_XBGR2101010,
  61. };
  62. static const uint32_t skl_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_ARGB8888,
  68. DRM_FORMAT_ABGR8888,
  69. DRM_FORMAT_XRGB2101010,
  70. DRM_FORMAT_XBGR2101010,
  71. DRM_FORMAT_YUYV,
  72. DRM_FORMAT_YVYU,
  73. DRM_FORMAT_UYVY,
  74. DRM_FORMAT_VYUY,
  75. };
  76. /* Cursor formats */
  77. static const uint32_t intel_cursor_formats[] = {
  78. DRM_FORMAT_ARGB8888,
  79. };
  80. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  81. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  82. struct intel_crtc_state *pipe_config);
  83. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  84. struct intel_crtc_state *pipe_config);
  85. static int intel_framebuffer_init(struct drm_device *dev,
  86. struct intel_framebuffer *ifb,
  87. struct drm_mode_fb_cmd2 *mode_cmd,
  88. struct drm_i915_gem_object *obj);
  89. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  90. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  91. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  92. struct intel_link_m_n *m_n,
  93. struct intel_link_m_n *m2_n2);
  94. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  95. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  96. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  97. static void vlv_prepare_pll(struct intel_crtc *crtc,
  98. const struct intel_crtc_state *pipe_config);
  99. static void chv_prepare_pll(struct intel_crtc *crtc,
  100. const struct intel_crtc_state *pipe_config);
  101. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  102. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  103. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  104. struct intel_crtc_state *crtc_state);
  105. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  106. int num_connectors);
  107. static void skylake_pfit_enable(struct intel_crtc *crtc);
  108. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  109. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  110. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  111. typedef struct {
  112. int min, max;
  113. } intel_range_t;
  114. typedef struct {
  115. int dot_limit;
  116. int p2_slow, p2_fast;
  117. } intel_p2_t;
  118. typedef struct intel_limit intel_limit_t;
  119. struct intel_limit {
  120. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  121. intel_p2_t p2;
  122. };
  123. /* returns HPLL frequency in kHz */
  124. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  125. {
  126. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  127. /* Obtain SKU information */
  128. mutex_lock(&dev_priv->sb_lock);
  129. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  130. CCK_FUSE_HPLL_FREQ_MASK;
  131. mutex_unlock(&dev_priv->sb_lock);
  132. return vco_freq[hpll_freq] * 1000;
  133. }
  134. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  135. const char *name, u32 reg)
  136. {
  137. u32 val;
  138. int divider;
  139. if (dev_priv->hpll_freq == 0)
  140. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  141. mutex_lock(&dev_priv->sb_lock);
  142. val = vlv_cck_read(dev_priv, reg);
  143. mutex_unlock(&dev_priv->sb_lock);
  144. divider = val & CCK_FREQUENCY_VALUES;
  145. WARN((val & CCK_FREQUENCY_STATUS) !=
  146. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  147. "%s change in progress\n", name);
  148. return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
  149. }
  150. int
  151. intel_pch_rawclk(struct drm_device *dev)
  152. {
  153. struct drm_i915_private *dev_priv = dev->dev_private;
  154. WARN_ON(!HAS_PCH_SPLIT(dev));
  155. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  156. }
  157. /* hrawclock is 1/4 the FSB frequency */
  158. int intel_hrawclk(struct drm_device *dev)
  159. {
  160. struct drm_i915_private *dev_priv = dev->dev_private;
  161. uint32_t clkcfg;
  162. /* There is no CLKCFG reg in Valleyview. VLV hrawclk is 200 MHz */
  163. if (IS_VALLEYVIEW(dev))
  164. return 200;
  165. clkcfg = I915_READ(CLKCFG);
  166. switch (clkcfg & CLKCFG_FSB_MASK) {
  167. case CLKCFG_FSB_400:
  168. return 100;
  169. case CLKCFG_FSB_533:
  170. return 133;
  171. case CLKCFG_FSB_667:
  172. return 166;
  173. case CLKCFG_FSB_800:
  174. return 200;
  175. case CLKCFG_FSB_1067:
  176. return 266;
  177. case CLKCFG_FSB_1333:
  178. return 333;
  179. /* these two are just a guess; one of them might be right */
  180. case CLKCFG_FSB_1600:
  181. case CLKCFG_FSB_1600_ALT:
  182. return 400;
  183. default:
  184. return 133;
  185. }
  186. }
  187. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  188. {
  189. if (!IS_VALLEYVIEW(dev_priv))
  190. return;
  191. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  192. CCK_CZ_CLOCK_CONTROL);
  193. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  194. }
  195. static inline u32 /* units of 100MHz */
  196. intel_fdi_link_freq(struct drm_device *dev)
  197. {
  198. if (IS_GEN5(dev)) {
  199. struct drm_i915_private *dev_priv = dev->dev_private;
  200. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  201. } else
  202. return 27;
  203. }
  204. static const intel_limit_t intel_limits_i8xx_dac = {
  205. .dot = { .min = 25000, .max = 350000 },
  206. .vco = { .min = 908000, .max = 1512000 },
  207. .n = { .min = 2, .max = 16 },
  208. .m = { .min = 96, .max = 140 },
  209. .m1 = { .min = 18, .max = 26 },
  210. .m2 = { .min = 6, .max = 16 },
  211. .p = { .min = 4, .max = 128 },
  212. .p1 = { .min = 2, .max = 33 },
  213. .p2 = { .dot_limit = 165000,
  214. .p2_slow = 4, .p2_fast = 2 },
  215. };
  216. static const intel_limit_t intel_limits_i8xx_dvo = {
  217. .dot = { .min = 25000, .max = 350000 },
  218. .vco = { .min = 908000, .max = 1512000 },
  219. .n = { .min = 2, .max = 16 },
  220. .m = { .min = 96, .max = 140 },
  221. .m1 = { .min = 18, .max = 26 },
  222. .m2 = { .min = 6, .max = 16 },
  223. .p = { .min = 4, .max = 128 },
  224. .p1 = { .min = 2, .max = 33 },
  225. .p2 = { .dot_limit = 165000,
  226. .p2_slow = 4, .p2_fast = 4 },
  227. };
  228. static const intel_limit_t intel_limits_i8xx_lvds = {
  229. .dot = { .min = 25000, .max = 350000 },
  230. .vco = { .min = 908000, .max = 1512000 },
  231. .n = { .min = 2, .max = 16 },
  232. .m = { .min = 96, .max = 140 },
  233. .m1 = { .min = 18, .max = 26 },
  234. .m2 = { .min = 6, .max = 16 },
  235. .p = { .min = 4, .max = 128 },
  236. .p1 = { .min = 1, .max = 6 },
  237. .p2 = { .dot_limit = 165000,
  238. .p2_slow = 14, .p2_fast = 7 },
  239. };
  240. static const intel_limit_t intel_limits_i9xx_sdvo = {
  241. .dot = { .min = 20000, .max = 400000 },
  242. .vco = { .min = 1400000, .max = 2800000 },
  243. .n = { .min = 1, .max = 6 },
  244. .m = { .min = 70, .max = 120 },
  245. .m1 = { .min = 8, .max = 18 },
  246. .m2 = { .min = 3, .max = 7 },
  247. .p = { .min = 5, .max = 80 },
  248. .p1 = { .min = 1, .max = 8 },
  249. .p2 = { .dot_limit = 200000,
  250. .p2_slow = 10, .p2_fast = 5 },
  251. };
  252. static const intel_limit_t intel_limits_i9xx_lvds = {
  253. .dot = { .min = 20000, .max = 400000 },
  254. .vco = { .min = 1400000, .max = 2800000 },
  255. .n = { .min = 1, .max = 6 },
  256. .m = { .min = 70, .max = 120 },
  257. .m1 = { .min = 8, .max = 18 },
  258. .m2 = { .min = 3, .max = 7 },
  259. .p = { .min = 7, .max = 98 },
  260. .p1 = { .min = 1, .max = 8 },
  261. .p2 = { .dot_limit = 112000,
  262. .p2_slow = 14, .p2_fast = 7 },
  263. };
  264. static const intel_limit_t intel_limits_g4x_sdvo = {
  265. .dot = { .min = 25000, .max = 270000 },
  266. .vco = { .min = 1750000, .max = 3500000},
  267. .n = { .min = 1, .max = 4 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 10, .max = 30 },
  272. .p1 = { .min = 1, .max = 3},
  273. .p2 = { .dot_limit = 270000,
  274. .p2_slow = 10,
  275. .p2_fast = 10
  276. },
  277. };
  278. static const intel_limit_t intel_limits_g4x_hdmi = {
  279. .dot = { .min = 22000, .max = 400000 },
  280. .vco = { .min = 1750000, .max = 3500000},
  281. .n = { .min = 1, .max = 4 },
  282. .m = { .min = 104, .max = 138 },
  283. .m1 = { .min = 16, .max = 23 },
  284. .m2 = { .min = 5, .max = 11 },
  285. .p = { .min = 5, .max = 80 },
  286. .p1 = { .min = 1, .max = 8},
  287. .p2 = { .dot_limit = 165000,
  288. .p2_slow = 10, .p2_fast = 5 },
  289. };
  290. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  291. .dot = { .min = 20000, .max = 115000 },
  292. .vco = { .min = 1750000, .max = 3500000 },
  293. .n = { .min = 1, .max = 3 },
  294. .m = { .min = 104, .max = 138 },
  295. .m1 = { .min = 17, .max = 23 },
  296. .m2 = { .min = 5, .max = 11 },
  297. .p = { .min = 28, .max = 112 },
  298. .p1 = { .min = 2, .max = 8 },
  299. .p2 = { .dot_limit = 0,
  300. .p2_slow = 14, .p2_fast = 14
  301. },
  302. };
  303. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  304. .dot = { .min = 80000, .max = 224000 },
  305. .vco = { .min = 1750000, .max = 3500000 },
  306. .n = { .min = 1, .max = 3 },
  307. .m = { .min = 104, .max = 138 },
  308. .m1 = { .min = 17, .max = 23 },
  309. .m2 = { .min = 5, .max = 11 },
  310. .p = { .min = 14, .max = 42 },
  311. .p1 = { .min = 2, .max = 6 },
  312. .p2 = { .dot_limit = 0,
  313. .p2_slow = 7, .p2_fast = 7
  314. },
  315. };
  316. static const intel_limit_t intel_limits_pineview_sdvo = {
  317. .dot = { .min = 20000, .max = 400000},
  318. .vco = { .min = 1700000, .max = 3500000 },
  319. /* Pineview's Ncounter is a ring counter */
  320. .n = { .min = 3, .max = 6 },
  321. .m = { .min = 2, .max = 256 },
  322. /* Pineview only has one combined m divider, which we treat as m2. */
  323. .m1 = { .min = 0, .max = 0 },
  324. .m2 = { .min = 0, .max = 254 },
  325. .p = { .min = 5, .max = 80 },
  326. .p1 = { .min = 1, .max = 8 },
  327. .p2 = { .dot_limit = 200000,
  328. .p2_slow = 10, .p2_fast = 5 },
  329. };
  330. static const intel_limit_t intel_limits_pineview_lvds = {
  331. .dot = { .min = 20000, .max = 400000 },
  332. .vco = { .min = 1700000, .max = 3500000 },
  333. .n = { .min = 3, .max = 6 },
  334. .m = { .min = 2, .max = 256 },
  335. .m1 = { .min = 0, .max = 0 },
  336. .m2 = { .min = 0, .max = 254 },
  337. .p = { .min = 7, .max = 112 },
  338. .p1 = { .min = 1, .max = 8 },
  339. .p2 = { .dot_limit = 112000,
  340. .p2_slow = 14, .p2_fast = 14 },
  341. };
  342. /* Ironlake / Sandybridge
  343. *
  344. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  345. * the range value for them is (actual_value - 2).
  346. */
  347. static const intel_limit_t intel_limits_ironlake_dac = {
  348. .dot = { .min = 25000, .max = 350000 },
  349. .vco = { .min = 1760000, .max = 3510000 },
  350. .n = { .min = 1, .max = 5 },
  351. .m = { .min = 79, .max = 127 },
  352. .m1 = { .min = 12, .max = 22 },
  353. .m2 = { .min = 5, .max = 9 },
  354. .p = { .min = 5, .max = 80 },
  355. .p1 = { .min = 1, .max = 8 },
  356. .p2 = { .dot_limit = 225000,
  357. .p2_slow = 10, .p2_fast = 5 },
  358. };
  359. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  360. .dot = { .min = 25000, .max = 350000 },
  361. .vco = { .min = 1760000, .max = 3510000 },
  362. .n = { .min = 1, .max = 3 },
  363. .m = { .min = 79, .max = 118 },
  364. .m1 = { .min = 12, .max = 22 },
  365. .m2 = { .min = 5, .max = 9 },
  366. .p = { .min = 28, .max = 112 },
  367. .p1 = { .min = 2, .max = 8 },
  368. .p2 = { .dot_limit = 225000,
  369. .p2_slow = 14, .p2_fast = 14 },
  370. };
  371. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  372. .dot = { .min = 25000, .max = 350000 },
  373. .vco = { .min = 1760000, .max = 3510000 },
  374. .n = { .min = 1, .max = 3 },
  375. .m = { .min = 79, .max = 127 },
  376. .m1 = { .min = 12, .max = 22 },
  377. .m2 = { .min = 5, .max = 9 },
  378. .p = { .min = 14, .max = 56 },
  379. .p1 = { .min = 2, .max = 8 },
  380. .p2 = { .dot_limit = 225000,
  381. .p2_slow = 7, .p2_fast = 7 },
  382. };
  383. /* LVDS 100mhz refclk limits. */
  384. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  385. .dot = { .min = 25000, .max = 350000 },
  386. .vco = { .min = 1760000, .max = 3510000 },
  387. .n = { .min = 1, .max = 2 },
  388. .m = { .min = 79, .max = 126 },
  389. .m1 = { .min = 12, .max = 22 },
  390. .m2 = { .min = 5, .max = 9 },
  391. .p = { .min = 28, .max = 112 },
  392. .p1 = { .min = 2, .max = 8 },
  393. .p2 = { .dot_limit = 225000,
  394. .p2_slow = 14, .p2_fast = 14 },
  395. };
  396. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  397. .dot = { .min = 25000, .max = 350000 },
  398. .vco = { .min = 1760000, .max = 3510000 },
  399. .n = { .min = 1, .max = 3 },
  400. .m = { .min = 79, .max = 126 },
  401. .m1 = { .min = 12, .max = 22 },
  402. .m2 = { .min = 5, .max = 9 },
  403. .p = { .min = 14, .max = 42 },
  404. .p1 = { .min = 2, .max = 6 },
  405. .p2 = { .dot_limit = 225000,
  406. .p2_slow = 7, .p2_fast = 7 },
  407. };
  408. static const intel_limit_t intel_limits_vlv = {
  409. /*
  410. * These are the data rate limits (measured in fast clocks)
  411. * since those are the strictest limits we have. The fast
  412. * clock and actual rate limits are more relaxed, so checking
  413. * them would make no difference.
  414. */
  415. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  416. .vco = { .min = 4000000, .max = 6000000 },
  417. .n = { .min = 1, .max = 7 },
  418. .m1 = { .min = 2, .max = 3 },
  419. .m2 = { .min = 11, .max = 156 },
  420. .p1 = { .min = 2, .max = 3 },
  421. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  422. };
  423. static const intel_limit_t intel_limits_chv = {
  424. /*
  425. * These are the data rate limits (measured in fast clocks)
  426. * since those are the strictest limits we have. The fast
  427. * clock and actual rate limits are more relaxed, so checking
  428. * them would make no difference.
  429. */
  430. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  431. .vco = { .min = 4800000, .max = 6480000 },
  432. .n = { .min = 1, .max = 1 },
  433. .m1 = { .min = 2, .max = 2 },
  434. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  435. .p1 = { .min = 2, .max = 4 },
  436. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  437. };
  438. static const intel_limit_t intel_limits_bxt = {
  439. /* FIXME: find real dot limits */
  440. .dot = { .min = 0, .max = INT_MAX },
  441. .vco = { .min = 4800000, .max = 6700000 },
  442. .n = { .min = 1, .max = 1 },
  443. .m1 = { .min = 2, .max = 2 },
  444. /* FIXME: find real m2 limits */
  445. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  446. .p1 = { .min = 2, .max = 4 },
  447. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  448. };
  449. static bool
  450. needs_modeset(struct drm_crtc_state *state)
  451. {
  452. return drm_atomic_crtc_needs_modeset(state);
  453. }
  454. /**
  455. * Returns whether any output on the specified pipe is of the specified type
  456. */
  457. bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
  458. {
  459. struct drm_device *dev = crtc->base.dev;
  460. struct intel_encoder *encoder;
  461. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  462. if (encoder->type == type)
  463. return true;
  464. return false;
  465. }
  466. /**
  467. * Returns whether any output on the specified pipe will have the specified
  468. * type after a staged modeset is complete, i.e., the same as
  469. * intel_pipe_has_type() but looking at encoder->new_crtc instead of
  470. * encoder->crtc.
  471. */
  472. static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
  473. int type)
  474. {
  475. struct drm_atomic_state *state = crtc_state->base.state;
  476. struct drm_connector *connector;
  477. struct drm_connector_state *connector_state;
  478. struct intel_encoder *encoder;
  479. int i, num_connectors = 0;
  480. for_each_connector_in_state(state, connector, connector_state, i) {
  481. if (connector_state->crtc != crtc_state->base.crtc)
  482. continue;
  483. num_connectors++;
  484. encoder = to_intel_encoder(connector_state->best_encoder);
  485. if (encoder->type == type)
  486. return true;
  487. }
  488. WARN_ON(num_connectors == 0);
  489. return false;
  490. }
  491. static const intel_limit_t *
  492. intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
  493. {
  494. struct drm_device *dev = crtc_state->base.crtc->dev;
  495. const intel_limit_t *limit;
  496. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  497. if (intel_is_dual_link_lvds(dev)) {
  498. if (refclk == 100000)
  499. limit = &intel_limits_ironlake_dual_lvds_100m;
  500. else
  501. limit = &intel_limits_ironlake_dual_lvds;
  502. } else {
  503. if (refclk == 100000)
  504. limit = &intel_limits_ironlake_single_lvds_100m;
  505. else
  506. limit = &intel_limits_ironlake_single_lvds;
  507. }
  508. } else
  509. limit = &intel_limits_ironlake_dac;
  510. return limit;
  511. }
  512. static const intel_limit_t *
  513. intel_g4x_limit(struct intel_crtc_state *crtc_state)
  514. {
  515. struct drm_device *dev = crtc_state->base.crtc->dev;
  516. const intel_limit_t *limit;
  517. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  518. if (intel_is_dual_link_lvds(dev))
  519. limit = &intel_limits_g4x_dual_channel_lvds;
  520. else
  521. limit = &intel_limits_g4x_single_channel_lvds;
  522. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  523. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  524. limit = &intel_limits_g4x_hdmi;
  525. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  526. limit = &intel_limits_g4x_sdvo;
  527. } else /* The option is for other outputs */
  528. limit = &intel_limits_i9xx_sdvo;
  529. return limit;
  530. }
  531. static const intel_limit_t *
  532. intel_limit(struct intel_crtc_state *crtc_state, int refclk)
  533. {
  534. struct drm_device *dev = crtc_state->base.crtc->dev;
  535. const intel_limit_t *limit;
  536. if (IS_BROXTON(dev))
  537. limit = &intel_limits_bxt;
  538. else if (HAS_PCH_SPLIT(dev))
  539. limit = intel_ironlake_limit(crtc_state, refclk);
  540. else if (IS_G4X(dev)) {
  541. limit = intel_g4x_limit(crtc_state);
  542. } else if (IS_PINEVIEW(dev)) {
  543. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  544. limit = &intel_limits_pineview_lvds;
  545. else
  546. limit = &intel_limits_pineview_sdvo;
  547. } else if (IS_CHERRYVIEW(dev)) {
  548. limit = &intel_limits_chv;
  549. } else if (IS_VALLEYVIEW(dev)) {
  550. limit = &intel_limits_vlv;
  551. } else if (!IS_GEN2(dev)) {
  552. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  553. limit = &intel_limits_i9xx_lvds;
  554. else
  555. limit = &intel_limits_i9xx_sdvo;
  556. } else {
  557. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  558. limit = &intel_limits_i8xx_lvds;
  559. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  560. limit = &intel_limits_i8xx_dvo;
  561. else
  562. limit = &intel_limits_i8xx_dac;
  563. }
  564. return limit;
  565. }
  566. /*
  567. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  568. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  569. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  570. * The helpers' return value is the rate of the clock that is fed to the
  571. * display engine's pipe which can be the above fast dot clock rate or a
  572. * divided-down version of it.
  573. */
  574. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  575. static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
  576. {
  577. clock->m = clock->m2 + 2;
  578. clock->p = clock->p1 * clock->p2;
  579. if (WARN_ON(clock->n == 0 || clock->p == 0))
  580. return 0;
  581. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  582. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  583. return clock->dot;
  584. }
  585. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  586. {
  587. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  588. }
  589. static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
  590. {
  591. clock->m = i9xx_dpll_compute_m(clock);
  592. clock->p = clock->p1 * clock->p2;
  593. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  594. return 0;
  595. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  596. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  597. return clock->dot;
  598. }
  599. static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
  600. {
  601. clock->m = clock->m1 * clock->m2;
  602. clock->p = clock->p1 * clock->p2;
  603. if (WARN_ON(clock->n == 0 || clock->p == 0))
  604. return 0;
  605. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  606. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  607. return clock->dot / 5;
  608. }
  609. int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
  610. {
  611. clock->m = clock->m1 * clock->m2;
  612. clock->p = clock->p1 * clock->p2;
  613. if (WARN_ON(clock->n == 0 || clock->p == 0))
  614. return 0;
  615. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  616. clock->n << 22);
  617. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  618. return clock->dot / 5;
  619. }
  620. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  621. /**
  622. * Returns whether the given set of divisors are valid for a given refclk with
  623. * the given connectors.
  624. */
  625. static bool intel_PLL_is_valid(struct drm_device *dev,
  626. const intel_limit_t *limit,
  627. const intel_clock_t *clock)
  628. {
  629. if (clock->n < limit->n.min || limit->n.max < clock->n)
  630. INTELPllInvalid("n out of range\n");
  631. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  632. INTELPllInvalid("p1 out of range\n");
  633. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  634. INTELPllInvalid("m2 out of range\n");
  635. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  636. INTELPllInvalid("m1 out of range\n");
  637. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
  638. if (clock->m1 <= clock->m2)
  639. INTELPllInvalid("m1 <= m2\n");
  640. if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
  641. if (clock->p < limit->p.min || limit->p.max < clock->p)
  642. INTELPllInvalid("p out of range\n");
  643. if (clock->m < limit->m.min || limit->m.max < clock->m)
  644. INTELPllInvalid("m out of range\n");
  645. }
  646. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  647. INTELPllInvalid("vco out of range\n");
  648. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  649. * connector, etc., rather than just a single range.
  650. */
  651. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  652. INTELPllInvalid("dot out of range\n");
  653. return true;
  654. }
  655. static int
  656. i9xx_select_p2_div(const intel_limit_t *limit,
  657. const struct intel_crtc_state *crtc_state,
  658. int target)
  659. {
  660. struct drm_device *dev = crtc_state->base.crtc->dev;
  661. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  662. /*
  663. * For LVDS just rely on its current settings for dual-channel.
  664. * We haven't figured out how to reliably set up different
  665. * single/dual channel state, if we even can.
  666. */
  667. if (intel_is_dual_link_lvds(dev))
  668. return limit->p2.p2_fast;
  669. else
  670. return limit->p2.p2_slow;
  671. } else {
  672. if (target < limit->p2.dot_limit)
  673. return limit->p2.p2_slow;
  674. else
  675. return limit->p2.p2_fast;
  676. }
  677. }
  678. static bool
  679. i9xx_find_best_dpll(const intel_limit_t *limit,
  680. struct intel_crtc_state *crtc_state,
  681. int target, int refclk, intel_clock_t *match_clock,
  682. intel_clock_t *best_clock)
  683. {
  684. struct drm_device *dev = crtc_state->base.crtc->dev;
  685. intel_clock_t clock;
  686. int err = target;
  687. memset(best_clock, 0, sizeof(*best_clock));
  688. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  689. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  690. clock.m1++) {
  691. for (clock.m2 = limit->m2.min;
  692. clock.m2 <= limit->m2.max; clock.m2++) {
  693. if (clock.m2 >= clock.m1)
  694. break;
  695. for (clock.n = limit->n.min;
  696. clock.n <= limit->n.max; clock.n++) {
  697. for (clock.p1 = limit->p1.min;
  698. clock.p1 <= limit->p1.max; clock.p1++) {
  699. int this_err;
  700. i9xx_calc_dpll_params(refclk, &clock);
  701. if (!intel_PLL_is_valid(dev, limit,
  702. &clock))
  703. continue;
  704. if (match_clock &&
  705. clock.p != match_clock->p)
  706. continue;
  707. this_err = abs(clock.dot - target);
  708. if (this_err < err) {
  709. *best_clock = clock;
  710. err = this_err;
  711. }
  712. }
  713. }
  714. }
  715. }
  716. return (err != target);
  717. }
  718. static bool
  719. pnv_find_best_dpll(const intel_limit_t *limit,
  720. struct intel_crtc_state *crtc_state,
  721. int target, int refclk, intel_clock_t *match_clock,
  722. intel_clock_t *best_clock)
  723. {
  724. struct drm_device *dev = crtc_state->base.crtc->dev;
  725. intel_clock_t clock;
  726. int err = target;
  727. memset(best_clock, 0, sizeof(*best_clock));
  728. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  729. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  730. clock.m1++) {
  731. for (clock.m2 = limit->m2.min;
  732. clock.m2 <= limit->m2.max; clock.m2++) {
  733. for (clock.n = limit->n.min;
  734. clock.n <= limit->n.max; clock.n++) {
  735. for (clock.p1 = limit->p1.min;
  736. clock.p1 <= limit->p1.max; clock.p1++) {
  737. int this_err;
  738. pnv_calc_dpll_params(refclk, &clock);
  739. if (!intel_PLL_is_valid(dev, limit,
  740. &clock))
  741. continue;
  742. if (match_clock &&
  743. clock.p != match_clock->p)
  744. continue;
  745. this_err = abs(clock.dot - target);
  746. if (this_err < err) {
  747. *best_clock = clock;
  748. err = this_err;
  749. }
  750. }
  751. }
  752. }
  753. }
  754. return (err != target);
  755. }
  756. static bool
  757. g4x_find_best_dpll(const intel_limit_t *limit,
  758. struct intel_crtc_state *crtc_state,
  759. int target, int refclk, intel_clock_t *match_clock,
  760. intel_clock_t *best_clock)
  761. {
  762. struct drm_device *dev = crtc_state->base.crtc->dev;
  763. intel_clock_t clock;
  764. int max_n;
  765. bool found = false;
  766. /* approximately equals target * 0.00585 */
  767. int err_most = (target >> 8) + (target >> 9);
  768. memset(best_clock, 0, sizeof(*best_clock));
  769. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  770. max_n = limit->n.max;
  771. /* based on hardware requirement, prefer smaller n to precision */
  772. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  773. /* based on hardware requirement, prefere larger m1,m2 */
  774. for (clock.m1 = limit->m1.max;
  775. clock.m1 >= limit->m1.min; clock.m1--) {
  776. for (clock.m2 = limit->m2.max;
  777. clock.m2 >= limit->m2.min; clock.m2--) {
  778. for (clock.p1 = limit->p1.max;
  779. clock.p1 >= limit->p1.min; clock.p1--) {
  780. int this_err;
  781. i9xx_calc_dpll_params(refclk, &clock);
  782. if (!intel_PLL_is_valid(dev, limit,
  783. &clock))
  784. continue;
  785. this_err = abs(clock.dot - target);
  786. if (this_err < err_most) {
  787. *best_clock = clock;
  788. err_most = this_err;
  789. max_n = clock.n;
  790. found = true;
  791. }
  792. }
  793. }
  794. }
  795. }
  796. return found;
  797. }
  798. /*
  799. * Check if the calculated PLL configuration is more optimal compared to the
  800. * best configuration and error found so far. Return the calculated error.
  801. */
  802. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  803. const intel_clock_t *calculated_clock,
  804. const intel_clock_t *best_clock,
  805. unsigned int best_error_ppm,
  806. unsigned int *error_ppm)
  807. {
  808. /*
  809. * For CHV ignore the error and consider only the P value.
  810. * Prefer a bigger P value based on HW requirements.
  811. */
  812. if (IS_CHERRYVIEW(dev)) {
  813. *error_ppm = 0;
  814. return calculated_clock->p > best_clock->p;
  815. }
  816. if (WARN_ON_ONCE(!target_freq))
  817. return false;
  818. *error_ppm = div_u64(1000000ULL *
  819. abs(target_freq - calculated_clock->dot),
  820. target_freq);
  821. /*
  822. * Prefer a better P value over a better (smaller) error if the error
  823. * is small. Ensure this preference for future configurations too by
  824. * setting the error to 0.
  825. */
  826. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  827. *error_ppm = 0;
  828. return true;
  829. }
  830. return *error_ppm + 10 < best_error_ppm;
  831. }
  832. static bool
  833. vlv_find_best_dpll(const intel_limit_t *limit,
  834. struct intel_crtc_state *crtc_state,
  835. int target, int refclk, intel_clock_t *match_clock,
  836. intel_clock_t *best_clock)
  837. {
  838. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  839. struct drm_device *dev = crtc->base.dev;
  840. intel_clock_t clock;
  841. unsigned int bestppm = 1000000;
  842. /* min update 19.2 MHz */
  843. int max_n = min(limit->n.max, refclk / 19200);
  844. bool found = false;
  845. target *= 5; /* fast clock */
  846. memset(best_clock, 0, sizeof(*best_clock));
  847. /* based on hardware requirement, prefer smaller n to precision */
  848. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  849. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  850. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  851. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  852. clock.p = clock.p1 * clock.p2;
  853. /* based on hardware requirement, prefer bigger m1,m2 values */
  854. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  855. unsigned int ppm;
  856. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  857. refclk * clock.m1);
  858. vlv_calc_dpll_params(refclk, &clock);
  859. if (!intel_PLL_is_valid(dev, limit,
  860. &clock))
  861. continue;
  862. if (!vlv_PLL_is_optimal(dev, target,
  863. &clock,
  864. best_clock,
  865. bestppm, &ppm))
  866. continue;
  867. *best_clock = clock;
  868. bestppm = ppm;
  869. found = true;
  870. }
  871. }
  872. }
  873. }
  874. return found;
  875. }
  876. static bool
  877. chv_find_best_dpll(const intel_limit_t *limit,
  878. struct intel_crtc_state *crtc_state,
  879. int target, int refclk, intel_clock_t *match_clock,
  880. intel_clock_t *best_clock)
  881. {
  882. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  883. struct drm_device *dev = crtc->base.dev;
  884. unsigned int best_error_ppm;
  885. intel_clock_t clock;
  886. uint64_t m2;
  887. int found = false;
  888. memset(best_clock, 0, sizeof(*best_clock));
  889. best_error_ppm = 1000000;
  890. /*
  891. * Based on hardware doc, the n always set to 1, and m1 always
  892. * set to 2. If requires to support 200Mhz refclk, we need to
  893. * revisit this because n may not 1 anymore.
  894. */
  895. clock.n = 1, clock.m1 = 2;
  896. target *= 5; /* fast clock */
  897. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  898. for (clock.p2 = limit->p2.p2_fast;
  899. clock.p2 >= limit->p2.p2_slow;
  900. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  901. unsigned int error_ppm;
  902. clock.p = clock.p1 * clock.p2;
  903. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  904. clock.n) << 22, refclk * clock.m1);
  905. if (m2 > INT_MAX/clock.m1)
  906. continue;
  907. clock.m2 = m2;
  908. chv_calc_dpll_params(refclk, &clock);
  909. if (!intel_PLL_is_valid(dev, limit, &clock))
  910. continue;
  911. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  912. best_error_ppm, &error_ppm))
  913. continue;
  914. *best_clock = clock;
  915. best_error_ppm = error_ppm;
  916. found = true;
  917. }
  918. }
  919. return found;
  920. }
  921. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  922. intel_clock_t *best_clock)
  923. {
  924. int refclk = i9xx_get_refclk(crtc_state, 0);
  925. return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
  926. target_clock, refclk, NULL, best_clock);
  927. }
  928. bool intel_crtc_active(struct drm_crtc *crtc)
  929. {
  930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  931. /* Be paranoid as we can arrive here with only partial
  932. * state retrieved from the hardware during setup.
  933. *
  934. * We can ditch the adjusted_mode.crtc_clock check as soon
  935. * as Haswell has gained clock readout/fastboot support.
  936. *
  937. * We can ditch the crtc->primary->fb check as soon as we can
  938. * properly reconstruct framebuffers.
  939. *
  940. * FIXME: The intel_crtc->active here should be switched to
  941. * crtc->state->active once we have proper CRTC states wired up
  942. * for atomic.
  943. */
  944. return intel_crtc->active && crtc->primary->state->fb &&
  945. intel_crtc->config->base.adjusted_mode.crtc_clock;
  946. }
  947. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  948. enum pipe pipe)
  949. {
  950. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  951. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  952. return intel_crtc->config->cpu_transcoder;
  953. }
  954. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  955. {
  956. struct drm_i915_private *dev_priv = dev->dev_private;
  957. u32 reg = PIPEDSL(pipe);
  958. u32 line1, line2;
  959. u32 line_mask;
  960. if (IS_GEN2(dev))
  961. line_mask = DSL_LINEMASK_GEN2;
  962. else
  963. line_mask = DSL_LINEMASK_GEN3;
  964. line1 = I915_READ(reg) & line_mask;
  965. msleep(5);
  966. line2 = I915_READ(reg) & line_mask;
  967. return line1 == line2;
  968. }
  969. /*
  970. * intel_wait_for_pipe_off - wait for pipe to turn off
  971. * @crtc: crtc whose pipe to wait for
  972. *
  973. * After disabling a pipe, we can't wait for vblank in the usual way,
  974. * spinning on the vblank interrupt status bit, since we won't actually
  975. * see an interrupt when the pipe is disabled.
  976. *
  977. * On Gen4 and above:
  978. * wait for the pipe register state bit to turn off
  979. *
  980. * Otherwise:
  981. * wait for the display line value to settle (it usually
  982. * ends up stopping at the start of the next frame).
  983. *
  984. */
  985. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  986. {
  987. struct drm_device *dev = crtc->base.dev;
  988. struct drm_i915_private *dev_priv = dev->dev_private;
  989. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  990. enum pipe pipe = crtc->pipe;
  991. if (INTEL_INFO(dev)->gen >= 4) {
  992. int reg = PIPECONF(cpu_transcoder);
  993. /* Wait for the Pipe State to go off */
  994. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  995. 100))
  996. WARN(1, "pipe_off wait timed out\n");
  997. } else {
  998. /* Wait for the display line to settle */
  999. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  1000. WARN(1, "pipe_off wait timed out\n");
  1001. }
  1002. }
  1003. static const char *state_string(bool enabled)
  1004. {
  1005. return enabled ? "on" : "off";
  1006. }
  1007. /* Only for pre-ILK configs */
  1008. void assert_pll(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(DPLL(pipe));
  1014. cur_state = !!(val & DPLL_VCO_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "PLL state assertion failure (expected %s, current %s)\n",
  1017. state_string(state), state_string(cur_state));
  1018. }
  1019. /* XXX: the dsi pll is shared between MIPI DSI ports */
  1020. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  1021. {
  1022. u32 val;
  1023. bool cur_state;
  1024. mutex_lock(&dev_priv->sb_lock);
  1025. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  1026. mutex_unlock(&dev_priv->sb_lock);
  1027. cur_state = val & DSI_PLL_VCO_EN;
  1028. I915_STATE_WARN(cur_state != state,
  1029. "DSI PLL state assertion failure (expected %s, current %s)\n",
  1030. state_string(state), state_string(cur_state));
  1031. }
  1032. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  1033. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  1034. struct intel_shared_dpll *
  1035. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  1036. {
  1037. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1038. if (crtc->config->shared_dpll < 0)
  1039. return NULL;
  1040. return &dev_priv->shared_dplls[crtc->config->shared_dpll];
  1041. }
  1042. /* For ILK+ */
  1043. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  1044. struct intel_shared_dpll *pll,
  1045. bool state)
  1046. {
  1047. bool cur_state;
  1048. struct intel_dpll_hw_state hw_state;
  1049. if (WARN (!pll,
  1050. "asserting DPLL %s with no DPLL\n", state_string(state)))
  1051. return;
  1052. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  1053. I915_STATE_WARN(cur_state != state,
  1054. "%s assertion failure (expected %s, current %s)\n",
  1055. pll->name, state_string(state), state_string(cur_state));
  1056. }
  1057. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  1058. enum pipe pipe, bool state)
  1059. {
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. if (HAS_DDI(dev_priv->dev)) {
  1064. /* DDI does not have a specific FDI_TX register */
  1065. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1066. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1067. } else {
  1068. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1069. cur_state = !!(val & FDI_TX_ENABLE);
  1070. }
  1071. I915_STATE_WARN(cur_state != state,
  1072. "FDI TX state assertion failure (expected %s, current %s)\n",
  1073. state_string(state), state_string(cur_state));
  1074. }
  1075. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1076. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1077. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1078. enum pipe pipe, bool state)
  1079. {
  1080. u32 val;
  1081. bool cur_state;
  1082. val = I915_READ(FDI_RX_CTL(pipe));
  1083. cur_state = !!(val & FDI_RX_ENABLE);
  1084. I915_STATE_WARN(cur_state != state,
  1085. "FDI RX state assertion failure (expected %s, current %s)\n",
  1086. state_string(state), state_string(cur_state));
  1087. }
  1088. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1089. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1090. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1091. enum pipe pipe)
  1092. {
  1093. u32 val;
  1094. /* ILK FDI PLL is always enabled */
  1095. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  1096. return;
  1097. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1098. if (HAS_DDI(dev_priv->dev))
  1099. return;
  1100. val = I915_READ(FDI_TX_CTL(pipe));
  1101. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1102. }
  1103. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1104. enum pipe pipe, bool state)
  1105. {
  1106. u32 val;
  1107. bool cur_state;
  1108. val = I915_READ(FDI_RX_CTL(pipe));
  1109. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1110. I915_STATE_WARN(cur_state != state,
  1111. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1112. state_string(state), state_string(cur_state));
  1113. }
  1114. void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1115. enum pipe pipe)
  1116. {
  1117. struct drm_device *dev = dev_priv->dev;
  1118. int pp_reg;
  1119. u32 val;
  1120. enum pipe panel_pipe = PIPE_A;
  1121. bool locked = true;
  1122. if (WARN_ON(HAS_DDI(dev)))
  1123. return;
  1124. if (HAS_PCH_SPLIT(dev)) {
  1125. u32 port_sel;
  1126. pp_reg = PCH_PP_CONTROL;
  1127. port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
  1128. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1129. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1130. panel_pipe = PIPE_B;
  1131. /* XXX: else fix for eDP */
  1132. } else if (IS_VALLEYVIEW(dev)) {
  1133. /* presumably write lock depends on pipe, not port select */
  1134. pp_reg = VLV_PIPE_PP_CONTROL(pipe);
  1135. panel_pipe = pipe;
  1136. } else {
  1137. pp_reg = PP_CONTROL;
  1138. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1139. panel_pipe = PIPE_B;
  1140. }
  1141. val = I915_READ(pp_reg);
  1142. if (!(val & PANEL_POWER_ON) ||
  1143. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1144. locked = false;
  1145. I915_STATE_WARN(panel_pipe == pipe && locked,
  1146. "panel assertion failure, pipe %c regs locked\n",
  1147. pipe_name(pipe));
  1148. }
  1149. static void assert_cursor(struct drm_i915_private *dev_priv,
  1150. enum pipe pipe, bool state)
  1151. {
  1152. struct drm_device *dev = dev_priv->dev;
  1153. bool cur_state;
  1154. if (IS_845G(dev) || IS_I865G(dev))
  1155. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1156. else
  1157. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1158. I915_STATE_WARN(cur_state != state,
  1159. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1160. pipe_name(pipe), state_string(state), state_string(cur_state));
  1161. }
  1162. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1163. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1164. void assert_pipe(struct drm_i915_private *dev_priv,
  1165. enum pipe pipe, bool state)
  1166. {
  1167. bool cur_state;
  1168. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1169. pipe);
  1170. /* if we need the pipe quirk it must be always on */
  1171. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1172. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1173. state = true;
  1174. if (!intel_display_power_is_enabled(dev_priv,
  1175. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1176. cur_state = false;
  1177. } else {
  1178. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1179. cur_state = !!(val & PIPECONF_ENABLE);
  1180. }
  1181. I915_STATE_WARN(cur_state != state,
  1182. "pipe %c assertion failure (expected %s, current %s)\n",
  1183. pipe_name(pipe), state_string(state), state_string(cur_state));
  1184. }
  1185. static void assert_plane(struct drm_i915_private *dev_priv,
  1186. enum plane plane, bool state)
  1187. {
  1188. u32 val;
  1189. bool cur_state;
  1190. val = I915_READ(DSPCNTR(plane));
  1191. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1192. I915_STATE_WARN(cur_state != state,
  1193. "plane %c assertion failure (expected %s, current %s)\n",
  1194. plane_name(plane), state_string(state), state_string(cur_state));
  1195. }
  1196. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1197. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1198. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1199. enum pipe pipe)
  1200. {
  1201. struct drm_device *dev = dev_priv->dev;
  1202. int i;
  1203. /* Primary planes are fixed to pipes on gen4+ */
  1204. if (INTEL_INFO(dev)->gen >= 4) {
  1205. u32 val = I915_READ(DSPCNTR(pipe));
  1206. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1207. "plane %c assertion failure, should be disabled but not\n",
  1208. plane_name(pipe));
  1209. return;
  1210. }
  1211. /* Need to check both planes against the pipe */
  1212. for_each_pipe(dev_priv, i) {
  1213. u32 val = I915_READ(DSPCNTR(i));
  1214. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1215. DISPPLANE_SEL_PIPE_SHIFT;
  1216. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1217. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1218. plane_name(i), pipe_name(pipe));
  1219. }
  1220. }
  1221. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe)
  1223. {
  1224. struct drm_device *dev = dev_priv->dev;
  1225. int sprite;
  1226. if (INTEL_INFO(dev)->gen >= 9) {
  1227. for_each_sprite(dev_priv, pipe, sprite) {
  1228. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1229. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1230. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1231. sprite, pipe_name(pipe));
  1232. }
  1233. } else if (IS_VALLEYVIEW(dev)) {
  1234. for_each_sprite(dev_priv, pipe, sprite) {
  1235. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1236. I915_STATE_WARN(val & SP_ENABLE,
  1237. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1238. sprite_name(pipe, sprite), pipe_name(pipe));
  1239. }
  1240. } else if (INTEL_INFO(dev)->gen >= 7) {
  1241. u32 val = I915_READ(SPRCTL(pipe));
  1242. I915_STATE_WARN(val & SPRITE_ENABLE,
  1243. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1244. plane_name(pipe), pipe_name(pipe));
  1245. } else if (INTEL_INFO(dev)->gen >= 5) {
  1246. u32 val = I915_READ(DVSCNTR(pipe));
  1247. I915_STATE_WARN(val & DVS_ENABLE,
  1248. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1249. plane_name(pipe), pipe_name(pipe));
  1250. }
  1251. }
  1252. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1253. {
  1254. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1255. drm_crtc_vblank_put(crtc);
  1256. }
  1257. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1258. {
  1259. u32 val;
  1260. bool enabled;
  1261. I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1262. val = I915_READ(PCH_DREF_CONTROL);
  1263. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1264. DREF_SUPERSPREAD_SOURCE_MASK));
  1265. I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1266. }
  1267. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1268. enum pipe pipe)
  1269. {
  1270. u32 val;
  1271. bool enabled;
  1272. val = I915_READ(PCH_TRANSCONF(pipe));
  1273. enabled = !!(val & TRANS_ENABLE);
  1274. I915_STATE_WARN(enabled,
  1275. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1276. pipe_name(pipe));
  1277. }
  1278. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1279. enum pipe pipe, u32 port_sel, u32 val)
  1280. {
  1281. if ((val & DP_PORT_EN) == 0)
  1282. return false;
  1283. if (HAS_PCH_CPT(dev_priv->dev)) {
  1284. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1285. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1286. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1287. return false;
  1288. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1289. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1290. return false;
  1291. } else {
  1292. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1293. return false;
  1294. }
  1295. return true;
  1296. }
  1297. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1298. enum pipe pipe, u32 val)
  1299. {
  1300. if ((val & SDVO_ENABLE) == 0)
  1301. return false;
  1302. if (HAS_PCH_CPT(dev_priv->dev)) {
  1303. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1304. return false;
  1305. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1306. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1307. return false;
  1308. } else {
  1309. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1310. return false;
  1311. }
  1312. return true;
  1313. }
  1314. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1315. enum pipe pipe, u32 val)
  1316. {
  1317. if ((val & LVDS_PORT_EN) == 0)
  1318. return false;
  1319. if (HAS_PCH_CPT(dev_priv->dev)) {
  1320. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1321. return false;
  1322. } else {
  1323. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1324. return false;
  1325. }
  1326. return true;
  1327. }
  1328. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1329. enum pipe pipe, u32 val)
  1330. {
  1331. if ((val & ADPA_DAC_ENABLE) == 0)
  1332. return false;
  1333. if (HAS_PCH_CPT(dev_priv->dev)) {
  1334. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1335. return false;
  1336. } else {
  1337. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1338. return false;
  1339. }
  1340. return true;
  1341. }
  1342. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1343. enum pipe pipe, int reg, u32 port_sel)
  1344. {
  1345. u32 val = I915_READ(reg);
  1346. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1347. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1348. reg, pipe_name(pipe));
  1349. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1350. && (val & DP_PIPEB_SELECT),
  1351. "IBX PCH dp port still using transcoder B\n");
  1352. }
  1353. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1354. enum pipe pipe, int reg)
  1355. {
  1356. u32 val = I915_READ(reg);
  1357. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1358. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1359. reg, pipe_name(pipe));
  1360. I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1361. && (val & SDVO_PIPE_B_SELECT),
  1362. "IBX PCH hdmi port still using transcoder B\n");
  1363. }
  1364. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1365. enum pipe pipe)
  1366. {
  1367. u32 val;
  1368. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1369. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1370. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1371. val = I915_READ(PCH_ADPA);
  1372. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1373. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1374. pipe_name(pipe));
  1375. val = I915_READ(PCH_LVDS);
  1376. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1377. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1378. pipe_name(pipe));
  1379. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1380. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1381. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1382. }
  1383. static void vlv_enable_pll(struct intel_crtc *crtc,
  1384. const struct intel_crtc_state *pipe_config)
  1385. {
  1386. struct drm_device *dev = crtc->base.dev;
  1387. struct drm_i915_private *dev_priv = dev->dev_private;
  1388. int reg = DPLL(crtc->pipe);
  1389. u32 dpll = pipe_config->dpll_hw_state.dpll;
  1390. assert_pipe_disabled(dev_priv, crtc->pipe);
  1391. /* No really, not for ILK+ */
  1392. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1393. /* PLL is protected by panel, make sure we can write it */
  1394. if (IS_MOBILE(dev_priv->dev))
  1395. assert_panel_unlocked(dev_priv, crtc->pipe);
  1396. I915_WRITE(reg, dpll);
  1397. POSTING_READ(reg);
  1398. udelay(150);
  1399. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1400. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1401. I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
  1402. POSTING_READ(DPLL_MD(crtc->pipe));
  1403. /* We do this three times for luck */
  1404. I915_WRITE(reg, dpll);
  1405. POSTING_READ(reg);
  1406. udelay(150); /* wait for warmup */
  1407. I915_WRITE(reg, dpll);
  1408. POSTING_READ(reg);
  1409. udelay(150); /* wait for warmup */
  1410. I915_WRITE(reg, dpll);
  1411. POSTING_READ(reg);
  1412. udelay(150); /* wait for warmup */
  1413. }
  1414. static void chv_enable_pll(struct intel_crtc *crtc,
  1415. const struct intel_crtc_state *pipe_config)
  1416. {
  1417. struct drm_device *dev = crtc->base.dev;
  1418. struct drm_i915_private *dev_priv = dev->dev_private;
  1419. int pipe = crtc->pipe;
  1420. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1421. u32 tmp;
  1422. assert_pipe_disabled(dev_priv, crtc->pipe);
  1423. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1424. mutex_lock(&dev_priv->sb_lock);
  1425. /* Enable back the 10bit clock to display controller */
  1426. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1427. tmp |= DPIO_DCLKP_EN;
  1428. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1429. mutex_unlock(&dev_priv->sb_lock);
  1430. /*
  1431. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1432. */
  1433. udelay(1);
  1434. /* Enable PLL */
  1435. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1436. /* Check PLL is locked */
  1437. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1438. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1439. /* not sure when this should be written */
  1440. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1441. POSTING_READ(DPLL_MD(pipe));
  1442. }
  1443. static int intel_num_dvo_pipes(struct drm_device *dev)
  1444. {
  1445. struct intel_crtc *crtc;
  1446. int count = 0;
  1447. for_each_intel_crtc(dev, crtc)
  1448. count += crtc->base.state->active &&
  1449. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
  1450. return count;
  1451. }
  1452. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1453. {
  1454. struct drm_device *dev = crtc->base.dev;
  1455. struct drm_i915_private *dev_priv = dev->dev_private;
  1456. int reg = DPLL(crtc->pipe);
  1457. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1458. assert_pipe_disabled(dev_priv, crtc->pipe);
  1459. /* No really, not for ILK+ */
  1460. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1461. /* PLL is protected by panel, make sure we can write it */
  1462. if (IS_MOBILE(dev) && !IS_I830(dev))
  1463. assert_panel_unlocked(dev_priv, crtc->pipe);
  1464. /* Enable DVO 2x clock on both PLLs if necessary */
  1465. if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
  1466. /*
  1467. * It appears to be important that we don't enable this
  1468. * for the current pipe before otherwise configuring the
  1469. * PLL. No idea how this should be handled if multiple
  1470. * DVO outputs are enabled simultaneosly.
  1471. */
  1472. dpll |= DPLL_DVO_2X_MODE;
  1473. I915_WRITE(DPLL(!crtc->pipe),
  1474. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Wait for the clocks to stabilize. */
  1477. POSTING_READ(reg);
  1478. udelay(150);
  1479. if (INTEL_INFO(dev)->gen >= 4) {
  1480. I915_WRITE(DPLL_MD(crtc->pipe),
  1481. crtc->config->dpll_hw_state.dpll_md);
  1482. } else {
  1483. /* The pixel multiplier can only be updated once the
  1484. * DPLL is enabled and the clocks are stable.
  1485. *
  1486. * So write it again.
  1487. */
  1488. I915_WRITE(reg, dpll);
  1489. }
  1490. /* We do this three times for luck */
  1491. I915_WRITE(reg, dpll);
  1492. POSTING_READ(reg);
  1493. udelay(150); /* wait for warmup */
  1494. I915_WRITE(reg, dpll);
  1495. POSTING_READ(reg);
  1496. udelay(150); /* wait for warmup */
  1497. I915_WRITE(reg, dpll);
  1498. POSTING_READ(reg);
  1499. udelay(150); /* wait for warmup */
  1500. }
  1501. /**
  1502. * i9xx_disable_pll - disable a PLL
  1503. * @dev_priv: i915 private structure
  1504. * @pipe: pipe PLL to disable
  1505. *
  1506. * Disable the PLL for @pipe, making sure the pipe is off first.
  1507. *
  1508. * Note! This is for pre-ILK only.
  1509. */
  1510. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1511. {
  1512. struct drm_device *dev = crtc->base.dev;
  1513. struct drm_i915_private *dev_priv = dev->dev_private;
  1514. enum pipe pipe = crtc->pipe;
  1515. /* Disable DVO 2x clock on both PLLs if necessary */
  1516. if (IS_I830(dev) &&
  1517. intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
  1518. !intel_num_dvo_pipes(dev)) {
  1519. I915_WRITE(DPLL(PIPE_B),
  1520. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1521. I915_WRITE(DPLL(PIPE_A),
  1522. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1523. }
  1524. /* Don't disable pipe or pipe PLLs if needed */
  1525. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1526. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1527. return;
  1528. /* Make sure the pipe isn't still relying on us */
  1529. assert_pipe_disabled(dev_priv, pipe);
  1530. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1531. POSTING_READ(DPLL(pipe));
  1532. }
  1533. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1534. {
  1535. u32 val;
  1536. /* Make sure the pipe isn't still relying on us */
  1537. assert_pipe_disabled(dev_priv, pipe);
  1538. /*
  1539. * Leave integrated clock source and reference clock enabled for pipe B.
  1540. * The latter is needed for VGA hotplug / manual detection.
  1541. */
  1542. val = DPLL_VGA_MODE_DIS;
  1543. if (pipe == PIPE_B)
  1544. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
  1545. I915_WRITE(DPLL(pipe), val);
  1546. POSTING_READ(DPLL(pipe));
  1547. }
  1548. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1549. {
  1550. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1551. u32 val;
  1552. /* Make sure the pipe isn't still relying on us */
  1553. assert_pipe_disabled(dev_priv, pipe);
  1554. /* Set PLL en = 0 */
  1555. val = DPLL_SSC_REF_CLK_CHV |
  1556. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1557. if (pipe != PIPE_A)
  1558. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1559. I915_WRITE(DPLL(pipe), val);
  1560. POSTING_READ(DPLL(pipe));
  1561. mutex_lock(&dev_priv->sb_lock);
  1562. /* Disable 10bit clock to display controller */
  1563. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1564. val &= ~DPIO_DCLKP_EN;
  1565. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1566. mutex_unlock(&dev_priv->sb_lock);
  1567. }
  1568. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1569. struct intel_digital_port *dport,
  1570. unsigned int expected_mask)
  1571. {
  1572. u32 port_mask;
  1573. int dpll_reg;
  1574. switch (dport->port) {
  1575. case PORT_B:
  1576. port_mask = DPLL_PORTB_READY_MASK;
  1577. dpll_reg = DPLL(0);
  1578. break;
  1579. case PORT_C:
  1580. port_mask = DPLL_PORTC_READY_MASK;
  1581. dpll_reg = DPLL(0);
  1582. expected_mask <<= 4;
  1583. break;
  1584. case PORT_D:
  1585. port_mask = DPLL_PORTD_READY_MASK;
  1586. dpll_reg = DPIO_PHY_STATUS;
  1587. break;
  1588. default:
  1589. BUG();
  1590. }
  1591. if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
  1592. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1593. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1594. }
  1595. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1596. {
  1597. struct drm_device *dev = crtc->base.dev;
  1598. struct drm_i915_private *dev_priv = dev->dev_private;
  1599. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1600. if (WARN_ON(pll == NULL))
  1601. return;
  1602. WARN_ON(!pll->config.crtc_mask);
  1603. if (pll->active == 0) {
  1604. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1605. WARN_ON(pll->on);
  1606. assert_shared_dpll_disabled(dev_priv, pll);
  1607. pll->mode_set(dev_priv, pll);
  1608. }
  1609. }
  1610. /**
  1611. * intel_enable_shared_dpll - enable PCH PLL
  1612. * @dev_priv: i915 private structure
  1613. * @pipe: pipe PLL to enable
  1614. *
  1615. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1616. * drives the transcoder clock.
  1617. */
  1618. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1619. {
  1620. struct drm_device *dev = crtc->base.dev;
  1621. struct drm_i915_private *dev_priv = dev->dev_private;
  1622. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1623. if (WARN_ON(pll == NULL))
  1624. return;
  1625. if (WARN_ON(pll->config.crtc_mask == 0))
  1626. return;
  1627. DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
  1628. pll->name, pll->active, pll->on,
  1629. crtc->base.base.id);
  1630. if (pll->active++) {
  1631. WARN_ON(!pll->on);
  1632. assert_shared_dpll_enabled(dev_priv, pll);
  1633. return;
  1634. }
  1635. WARN_ON(pll->on);
  1636. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  1637. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1638. pll->enable(dev_priv, pll);
  1639. pll->on = true;
  1640. }
  1641. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1642. {
  1643. struct drm_device *dev = crtc->base.dev;
  1644. struct drm_i915_private *dev_priv = dev->dev_private;
  1645. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1646. /* PCH only available on ILK+ */
  1647. if (INTEL_INFO(dev)->gen < 5)
  1648. return;
  1649. if (pll == NULL)
  1650. return;
  1651. if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
  1652. return;
  1653. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1654. pll->name, pll->active, pll->on,
  1655. crtc->base.base.id);
  1656. if (WARN_ON(pll->active == 0)) {
  1657. assert_shared_dpll_disabled(dev_priv, pll);
  1658. return;
  1659. }
  1660. assert_shared_dpll_enabled(dev_priv, pll);
  1661. WARN_ON(!pll->on);
  1662. if (--pll->active)
  1663. return;
  1664. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1665. pll->disable(dev_priv, pll);
  1666. pll->on = false;
  1667. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  1668. }
  1669. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1670. enum pipe pipe)
  1671. {
  1672. struct drm_device *dev = dev_priv->dev;
  1673. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1674. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1675. uint32_t reg, val, pipeconf_val;
  1676. /* PCH only available on ILK+ */
  1677. BUG_ON(!HAS_PCH_SPLIT(dev));
  1678. /* Make sure PCH DPLL is enabled */
  1679. assert_shared_dpll_enabled(dev_priv,
  1680. intel_crtc_to_shared_dpll(intel_crtc));
  1681. /* FDI must be feeding us bits for PCH ports */
  1682. assert_fdi_tx_enabled(dev_priv, pipe);
  1683. assert_fdi_rx_enabled(dev_priv, pipe);
  1684. if (HAS_PCH_CPT(dev)) {
  1685. /* Workaround: Set the timing override bit before enabling the
  1686. * pch transcoder. */
  1687. reg = TRANS_CHICKEN2(pipe);
  1688. val = I915_READ(reg);
  1689. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(reg, val);
  1691. }
  1692. reg = PCH_TRANSCONF(pipe);
  1693. val = I915_READ(reg);
  1694. pipeconf_val = I915_READ(PIPECONF(pipe));
  1695. if (HAS_PCH_IBX(dev_priv->dev)) {
  1696. /*
  1697. * Make the BPC in transcoder be consistent with
  1698. * that in pipeconf reg. For HDMI we must use 8bpc
  1699. * here for both 8bpc and 12bpc.
  1700. */
  1701. val &= ~PIPECONF_BPC_MASK;
  1702. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
  1703. val |= PIPECONF_8BPC;
  1704. else
  1705. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1706. }
  1707. val &= ~TRANS_INTERLACE_MASK;
  1708. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1709. if (HAS_PCH_IBX(dev_priv->dev) &&
  1710. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  1711. val |= TRANS_LEGACY_INTERLACED_ILK;
  1712. else
  1713. val |= TRANS_INTERLACED;
  1714. else
  1715. val |= TRANS_PROGRESSIVE;
  1716. I915_WRITE(reg, val | TRANS_ENABLE);
  1717. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1718. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1719. }
  1720. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1721. enum transcoder cpu_transcoder)
  1722. {
  1723. u32 val, pipeconf_val;
  1724. /* PCH only available on ILK+ */
  1725. BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
  1726. /* FDI must be feeding us bits for PCH ports */
  1727. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1728. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1729. /* Workaround: set timing override bit. */
  1730. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1731. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1732. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1733. val = TRANS_ENABLE;
  1734. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1735. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1736. PIPECONF_INTERLACED_ILK)
  1737. val |= TRANS_INTERLACED;
  1738. else
  1739. val |= TRANS_PROGRESSIVE;
  1740. I915_WRITE(LPT_TRANSCONF, val);
  1741. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1742. DRM_ERROR("Failed to enable PCH transcoder\n");
  1743. }
  1744. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1745. enum pipe pipe)
  1746. {
  1747. struct drm_device *dev = dev_priv->dev;
  1748. uint32_t reg, val;
  1749. /* FDI relies on the transcoder */
  1750. assert_fdi_tx_disabled(dev_priv, pipe);
  1751. assert_fdi_rx_disabled(dev_priv, pipe);
  1752. /* Ports must be off as well */
  1753. assert_pch_ports_disabled(dev_priv, pipe);
  1754. reg = PCH_TRANSCONF(pipe);
  1755. val = I915_READ(reg);
  1756. val &= ~TRANS_ENABLE;
  1757. I915_WRITE(reg, val);
  1758. /* wait for PCH transcoder off, transcoder state */
  1759. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1760. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1761. if (HAS_PCH_CPT(dev)) {
  1762. /* Workaround: Clear the timing override chicken bit again. */
  1763. reg = TRANS_CHICKEN2(pipe);
  1764. val = I915_READ(reg);
  1765. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1766. I915_WRITE(reg, val);
  1767. }
  1768. }
  1769. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1770. {
  1771. u32 val;
  1772. val = I915_READ(LPT_TRANSCONF);
  1773. val &= ~TRANS_ENABLE;
  1774. I915_WRITE(LPT_TRANSCONF, val);
  1775. /* wait for PCH transcoder off, transcoder state */
  1776. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1777. DRM_ERROR("Failed to disable PCH transcoder\n");
  1778. /* Workaround: clear timing override bit. */
  1779. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1780. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1781. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1782. }
  1783. /**
  1784. * intel_enable_pipe - enable a pipe, asserting requirements
  1785. * @crtc: crtc responsible for the pipe
  1786. *
  1787. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1788. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1789. */
  1790. static void intel_enable_pipe(struct intel_crtc *crtc)
  1791. {
  1792. struct drm_device *dev = crtc->base.dev;
  1793. struct drm_i915_private *dev_priv = dev->dev_private;
  1794. enum pipe pipe = crtc->pipe;
  1795. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1796. enum pipe pch_transcoder;
  1797. int reg;
  1798. u32 val;
  1799. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1800. assert_planes_disabled(dev_priv, pipe);
  1801. assert_cursor_disabled(dev_priv, pipe);
  1802. assert_sprites_disabled(dev_priv, pipe);
  1803. if (HAS_PCH_LPT(dev_priv->dev))
  1804. pch_transcoder = TRANSCODER_A;
  1805. else
  1806. pch_transcoder = pipe;
  1807. /*
  1808. * A pipe without a PLL won't actually be able to drive bits from
  1809. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1810. * need the check.
  1811. */
  1812. if (HAS_GMCH_DISPLAY(dev_priv->dev))
  1813. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  1814. assert_dsi_pll_enabled(dev_priv);
  1815. else
  1816. assert_pll_enabled(dev_priv, pipe);
  1817. else {
  1818. if (crtc->config->has_pch_encoder) {
  1819. /* if driving the PCH, we need FDI enabled */
  1820. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1821. assert_fdi_tx_pll_enabled(dev_priv,
  1822. (enum pipe) cpu_transcoder);
  1823. }
  1824. /* FIXME: assert CPU port conditions for SNB+ */
  1825. }
  1826. reg = PIPECONF(cpu_transcoder);
  1827. val = I915_READ(reg);
  1828. if (val & PIPECONF_ENABLE) {
  1829. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1830. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1831. return;
  1832. }
  1833. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1834. POSTING_READ(reg);
  1835. }
  1836. /**
  1837. * intel_disable_pipe - disable a pipe, asserting requirements
  1838. * @crtc: crtc whose pipes is to be disabled
  1839. *
  1840. * Disable the pipe of @crtc, making sure that various hardware
  1841. * specific requirements are met, if applicable, e.g. plane
  1842. * disabled, panel fitter off, etc.
  1843. *
  1844. * Will wait until the pipe has shut down before returning.
  1845. */
  1846. static void intel_disable_pipe(struct intel_crtc *crtc)
  1847. {
  1848. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  1849. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1850. enum pipe pipe = crtc->pipe;
  1851. int reg;
  1852. u32 val;
  1853. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1854. /*
  1855. * Make sure planes won't keep trying to pump pixels to us,
  1856. * or we might hang the display.
  1857. */
  1858. assert_planes_disabled(dev_priv, pipe);
  1859. assert_cursor_disabled(dev_priv, pipe);
  1860. assert_sprites_disabled(dev_priv, pipe);
  1861. reg = PIPECONF(cpu_transcoder);
  1862. val = I915_READ(reg);
  1863. if ((val & PIPECONF_ENABLE) == 0)
  1864. return;
  1865. /*
  1866. * Double wide has implications for planes
  1867. * so best keep it disabled when not needed.
  1868. */
  1869. if (crtc->config->double_wide)
  1870. val &= ~PIPECONF_DOUBLE_WIDE;
  1871. /* Don't disable pipe or pipe PLLs if needed */
  1872. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1873. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1874. val &= ~PIPECONF_ENABLE;
  1875. I915_WRITE(reg, val);
  1876. if ((val & PIPECONF_ENABLE) == 0)
  1877. intel_wait_for_pipe_off(crtc);
  1878. }
  1879. static bool need_vtd_wa(struct drm_device *dev)
  1880. {
  1881. #ifdef CONFIG_INTEL_IOMMU
  1882. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1883. return true;
  1884. #endif
  1885. return false;
  1886. }
  1887. unsigned int
  1888. intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
  1889. uint64_t fb_format_modifier, unsigned int plane)
  1890. {
  1891. unsigned int tile_height;
  1892. uint32_t pixel_bytes;
  1893. switch (fb_format_modifier) {
  1894. case DRM_FORMAT_MOD_NONE:
  1895. tile_height = 1;
  1896. break;
  1897. case I915_FORMAT_MOD_X_TILED:
  1898. tile_height = IS_GEN2(dev) ? 16 : 8;
  1899. break;
  1900. case I915_FORMAT_MOD_Y_TILED:
  1901. tile_height = 32;
  1902. break;
  1903. case I915_FORMAT_MOD_Yf_TILED:
  1904. pixel_bytes = drm_format_plane_cpp(pixel_format, plane);
  1905. switch (pixel_bytes) {
  1906. default:
  1907. case 1:
  1908. tile_height = 64;
  1909. break;
  1910. case 2:
  1911. case 4:
  1912. tile_height = 32;
  1913. break;
  1914. case 8:
  1915. tile_height = 16;
  1916. break;
  1917. case 16:
  1918. WARN_ONCE(1,
  1919. "128-bit pixels are not supported for display!");
  1920. tile_height = 16;
  1921. break;
  1922. }
  1923. break;
  1924. default:
  1925. MISSING_CASE(fb_format_modifier);
  1926. tile_height = 1;
  1927. break;
  1928. }
  1929. return tile_height;
  1930. }
  1931. unsigned int
  1932. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1933. uint32_t pixel_format, uint64_t fb_format_modifier)
  1934. {
  1935. return ALIGN(height, intel_tile_height(dev, pixel_format,
  1936. fb_format_modifier, 0));
  1937. }
  1938. static int
  1939. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
  1940. const struct drm_plane_state *plane_state)
  1941. {
  1942. struct intel_rotation_info *info = &view->rotation_info;
  1943. unsigned int tile_height, tile_pitch;
  1944. *view = i915_ggtt_view_normal;
  1945. if (!plane_state)
  1946. return 0;
  1947. if (!intel_rotation_90_or_270(plane_state->rotation))
  1948. return 0;
  1949. *view = i915_ggtt_view_rotated;
  1950. info->height = fb->height;
  1951. info->pixel_format = fb->pixel_format;
  1952. info->pitch = fb->pitches[0];
  1953. info->uv_offset = fb->offsets[1];
  1954. info->fb_modifier = fb->modifier[0];
  1955. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1956. fb->modifier[0], 0);
  1957. tile_pitch = PAGE_SIZE / tile_height;
  1958. info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1959. info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
  1960. info->size = info->width_pages * info->height_pages * PAGE_SIZE;
  1961. if (info->pixel_format == DRM_FORMAT_NV12) {
  1962. tile_height = intel_tile_height(fb->dev, fb->pixel_format,
  1963. fb->modifier[0], 1);
  1964. tile_pitch = PAGE_SIZE / tile_height;
  1965. info->width_pages_uv = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
  1966. info->height_pages_uv = DIV_ROUND_UP(fb->height / 2,
  1967. tile_height);
  1968. info->size_uv = info->width_pages_uv * info->height_pages_uv *
  1969. PAGE_SIZE;
  1970. }
  1971. return 0;
  1972. }
  1973. static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
  1974. {
  1975. if (INTEL_INFO(dev_priv)->gen >= 9)
  1976. return 256 * 1024;
  1977. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1978. IS_VALLEYVIEW(dev_priv))
  1979. return 128 * 1024;
  1980. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1981. return 4 * 1024;
  1982. else
  1983. return 0;
  1984. }
  1985. int
  1986. intel_pin_and_fence_fb_obj(struct drm_plane *plane,
  1987. struct drm_framebuffer *fb,
  1988. const struct drm_plane_state *plane_state)
  1989. {
  1990. struct drm_device *dev = fb->dev;
  1991. struct drm_i915_private *dev_priv = dev->dev_private;
  1992. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1993. struct i915_ggtt_view view;
  1994. u32 alignment;
  1995. int ret;
  1996. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1997. switch (fb->modifier[0]) {
  1998. case DRM_FORMAT_MOD_NONE:
  1999. alignment = intel_linear_alignment(dev_priv);
  2000. break;
  2001. case I915_FORMAT_MOD_X_TILED:
  2002. if (INTEL_INFO(dev)->gen >= 9)
  2003. alignment = 256 * 1024;
  2004. else {
  2005. /* pin() will align the object as required by fence */
  2006. alignment = 0;
  2007. }
  2008. break;
  2009. case I915_FORMAT_MOD_Y_TILED:
  2010. case I915_FORMAT_MOD_Yf_TILED:
  2011. if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
  2012. "Y tiling bo slipped through, driver bug!\n"))
  2013. return -EINVAL;
  2014. alignment = 1 * 1024 * 1024;
  2015. break;
  2016. default:
  2017. MISSING_CASE(fb->modifier[0]);
  2018. return -EINVAL;
  2019. }
  2020. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2021. if (ret)
  2022. return ret;
  2023. /* Note that the w/a also requires 64 PTE of padding following the
  2024. * bo. We currently fill all unused PTE with the shadow page and so
  2025. * we should always have valid PTE following the scanout preventing
  2026. * the VT-d warning.
  2027. */
  2028. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  2029. alignment = 256 * 1024;
  2030. /*
  2031. * Global gtt pte registers are special registers which actually forward
  2032. * writes to a chunk of system memory. Which means that there is no risk
  2033. * that the register values disappear as soon as we call
  2034. * intel_runtime_pm_put(), so it is correct to wrap only the
  2035. * pin/unpin/fence and not more.
  2036. */
  2037. intel_runtime_pm_get(dev_priv);
  2038. ret = i915_gem_object_pin_to_display_plane(obj, alignment,
  2039. &view);
  2040. if (ret)
  2041. goto err_pm;
  2042. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  2043. * fence, whereas 965+ only requires a fence if using
  2044. * framebuffer compression. For simplicity, we always install
  2045. * a fence as the cost is not that onerous.
  2046. */
  2047. ret = i915_gem_object_get_fence(obj);
  2048. if (ret == -EDEADLK) {
  2049. /*
  2050. * -EDEADLK means there are no free fences
  2051. * no pending flips.
  2052. *
  2053. * This is propagated to atomic, but it uses
  2054. * -EDEADLK to force a locking recovery, so
  2055. * change the returned error to -EBUSY.
  2056. */
  2057. ret = -EBUSY;
  2058. goto err_unpin;
  2059. } else if (ret)
  2060. goto err_unpin;
  2061. i915_gem_object_pin_fence(obj);
  2062. intel_runtime_pm_put(dev_priv);
  2063. return 0;
  2064. err_unpin:
  2065. i915_gem_object_unpin_from_display_plane(obj, &view);
  2066. err_pm:
  2067. intel_runtime_pm_put(dev_priv);
  2068. return ret;
  2069. }
  2070. static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
  2071. const struct drm_plane_state *plane_state)
  2072. {
  2073. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2074. struct i915_ggtt_view view;
  2075. int ret;
  2076. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  2077. ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
  2078. WARN_ONCE(ret, "Couldn't get view from plane state!");
  2079. i915_gem_object_unpin_fence(obj);
  2080. i915_gem_object_unpin_from_display_plane(obj, &view);
  2081. }
  2082. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  2083. * is assumed to be a power-of-two. */
  2084. unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
  2085. int *x, int *y,
  2086. unsigned int tiling_mode,
  2087. unsigned int cpp,
  2088. unsigned int pitch)
  2089. {
  2090. if (tiling_mode != I915_TILING_NONE) {
  2091. unsigned int tile_rows, tiles;
  2092. tile_rows = *y / 8;
  2093. *y %= 8;
  2094. tiles = *x / (512/cpp);
  2095. *x %= 512/cpp;
  2096. return tile_rows * pitch * 8 + tiles * 4096;
  2097. } else {
  2098. unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
  2099. unsigned int offset;
  2100. offset = *y * pitch + *x * cpp;
  2101. *y = (offset & alignment) / pitch;
  2102. *x = ((offset & alignment) - *y * pitch) / cpp;
  2103. return offset & ~alignment;
  2104. }
  2105. }
  2106. static int i9xx_format_to_fourcc(int format)
  2107. {
  2108. switch (format) {
  2109. case DISPPLANE_8BPP:
  2110. return DRM_FORMAT_C8;
  2111. case DISPPLANE_BGRX555:
  2112. return DRM_FORMAT_XRGB1555;
  2113. case DISPPLANE_BGRX565:
  2114. return DRM_FORMAT_RGB565;
  2115. default:
  2116. case DISPPLANE_BGRX888:
  2117. return DRM_FORMAT_XRGB8888;
  2118. case DISPPLANE_RGBX888:
  2119. return DRM_FORMAT_XBGR8888;
  2120. case DISPPLANE_BGRX101010:
  2121. return DRM_FORMAT_XRGB2101010;
  2122. case DISPPLANE_RGBX101010:
  2123. return DRM_FORMAT_XBGR2101010;
  2124. }
  2125. }
  2126. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2127. {
  2128. switch (format) {
  2129. case PLANE_CTL_FORMAT_RGB_565:
  2130. return DRM_FORMAT_RGB565;
  2131. default:
  2132. case PLANE_CTL_FORMAT_XRGB_8888:
  2133. if (rgb_order) {
  2134. if (alpha)
  2135. return DRM_FORMAT_ABGR8888;
  2136. else
  2137. return DRM_FORMAT_XBGR8888;
  2138. } else {
  2139. if (alpha)
  2140. return DRM_FORMAT_ARGB8888;
  2141. else
  2142. return DRM_FORMAT_XRGB8888;
  2143. }
  2144. case PLANE_CTL_FORMAT_XRGB_2101010:
  2145. if (rgb_order)
  2146. return DRM_FORMAT_XBGR2101010;
  2147. else
  2148. return DRM_FORMAT_XRGB2101010;
  2149. }
  2150. }
  2151. static bool
  2152. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2153. struct intel_initial_plane_config *plane_config)
  2154. {
  2155. struct drm_device *dev = crtc->base.dev;
  2156. struct drm_i915_private *dev_priv = to_i915(dev);
  2157. struct drm_i915_gem_object *obj = NULL;
  2158. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2159. struct drm_framebuffer *fb = &plane_config->fb->base;
  2160. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2161. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2162. PAGE_SIZE);
  2163. size_aligned -= base_aligned;
  2164. if (plane_config->size == 0)
  2165. return false;
  2166. /* If the FB is too big, just don't use it since fbdev is not very
  2167. * important and we should probably use that space with FBC or other
  2168. * features. */
  2169. if (size_aligned * 2 > dev_priv->gtt.stolen_usable_size)
  2170. return false;
  2171. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2172. base_aligned,
  2173. base_aligned,
  2174. size_aligned);
  2175. if (!obj)
  2176. return false;
  2177. obj->tiling_mode = plane_config->tiling;
  2178. if (obj->tiling_mode == I915_TILING_X)
  2179. obj->stride = fb->pitches[0];
  2180. mode_cmd.pixel_format = fb->pixel_format;
  2181. mode_cmd.width = fb->width;
  2182. mode_cmd.height = fb->height;
  2183. mode_cmd.pitches[0] = fb->pitches[0];
  2184. mode_cmd.modifier[0] = fb->modifier[0];
  2185. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2186. mutex_lock(&dev->struct_mutex);
  2187. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2188. &mode_cmd, obj)) {
  2189. DRM_DEBUG_KMS("intel fb init failed\n");
  2190. goto out_unref_obj;
  2191. }
  2192. mutex_unlock(&dev->struct_mutex);
  2193. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2194. return true;
  2195. out_unref_obj:
  2196. drm_gem_object_unreference(&obj->base);
  2197. mutex_unlock(&dev->struct_mutex);
  2198. return false;
  2199. }
  2200. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2201. static void
  2202. update_state_fb(struct drm_plane *plane)
  2203. {
  2204. if (plane->fb == plane->state->fb)
  2205. return;
  2206. if (plane->state->fb)
  2207. drm_framebuffer_unreference(plane->state->fb);
  2208. plane->state->fb = plane->fb;
  2209. if (plane->state->fb)
  2210. drm_framebuffer_reference(plane->state->fb);
  2211. }
  2212. static void
  2213. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2214. struct intel_initial_plane_config *plane_config)
  2215. {
  2216. struct drm_device *dev = intel_crtc->base.dev;
  2217. struct drm_i915_private *dev_priv = dev->dev_private;
  2218. struct drm_crtc *c;
  2219. struct intel_crtc *i;
  2220. struct drm_i915_gem_object *obj;
  2221. struct drm_plane *primary = intel_crtc->base.primary;
  2222. struct drm_plane_state *plane_state = primary->state;
  2223. struct drm_framebuffer *fb;
  2224. if (!plane_config->fb)
  2225. return;
  2226. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2227. fb = &plane_config->fb->base;
  2228. goto valid_fb;
  2229. }
  2230. kfree(plane_config->fb);
  2231. /*
  2232. * Failed to alloc the obj, check to see if we should share
  2233. * an fb with another CRTC instead
  2234. */
  2235. for_each_crtc(dev, c) {
  2236. i = to_intel_crtc(c);
  2237. if (c == &intel_crtc->base)
  2238. continue;
  2239. if (!i->active)
  2240. continue;
  2241. fb = c->primary->fb;
  2242. if (!fb)
  2243. continue;
  2244. obj = intel_fb_obj(fb);
  2245. if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
  2246. drm_framebuffer_reference(fb);
  2247. goto valid_fb;
  2248. }
  2249. }
  2250. return;
  2251. valid_fb:
  2252. plane_state->src_x = plane_state->src_y = 0;
  2253. plane_state->src_w = fb->width << 16;
  2254. plane_state->src_h = fb->height << 16;
  2255. plane_state->crtc_x = plane_state->src_y = 0;
  2256. plane_state->crtc_w = fb->width;
  2257. plane_state->crtc_h = fb->height;
  2258. obj = intel_fb_obj(fb);
  2259. if (obj->tiling_mode != I915_TILING_NONE)
  2260. dev_priv->preserve_bios_swizzle = true;
  2261. drm_framebuffer_reference(fb);
  2262. primary->fb = primary->state->fb = fb;
  2263. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2264. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2265. obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
  2266. }
  2267. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2268. struct drm_framebuffer *fb,
  2269. int x, int y)
  2270. {
  2271. struct drm_device *dev = crtc->dev;
  2272. struct drm_i915_private *dev_priv = dev->dev_private;
  2273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2274. struct drm_plane *primary = crtc->primary;
  2275. bool visible = to_intel_plane_state(primary->state)->visible;
  2276. struct drm_i915_gem_object *obj;
  2277. int plane = intel_crtc->plane;
  2278. unsigned long linear_offset;
  2279. u32 dspcntr;
  2280. u32 reg = DSPCNTR(plane);
  2281. int pixel_size;
  2282. if (!visible || !fb) {
  2283. I915_WRITE(reg, 0);
  2284. if (INTEL_INFO(dev)->gen >= 4)
  2285. I915_WRITE(DSPSURF(plane), 0);
  2286. else
  2287. I915_WRITE(DSPADDR(plane), 0);
  2288. POSTING_READ(reg);
  2289. return;
  2290. }
  2291. obj = intel_fb_obj(fb);
  2292. if (WARN_ON(obj == NULL))
  2293. return;
  2294. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2295. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2296. dspcntr |= DISPLAY_PLANE_ENABLE;
  2297. if (INTEL_INFO(dev)->gen < 4) {
  2298. if (intel_crtc->pipe == PIPE_B)
  2299. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2300. /* pipesrc and dspsize control the size that is scaled from,
  2301. * which should always be the user's requested size.
  2302. */
  2303. I915_WRITE(DSPSIZE(plane),
  2304. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2305. (intel_crtc->config->pipe_src_w - 1));
  2306. I915_WRITE(DSPPOS(plane), 0);
  2307. } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
  2308. I915_WRITE(PRIMSIZE(plane),
  2309. ((intel_crtc->config->pipe_src_h - 1) << 16) |
  2310. (intel_crtc->config->pipe_src_w - 1));
  2311. I915_WRITE(PRIMPOS(plane), 0);
  2312. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2313. }
  2314. switch (fb->pixel_format) {
  2315. case DRM_FORMAT_C8:
  2316. dspcntr |= DISPPLANE_8BPP;
  2317. break;
  2318. case DRM_FORMAT_XRGB1555:
  2319. dspcntr |= DISPPLANE_BGRX555;
  2320. break;
  2321. case DRM_FORMAT_RGB565:
  2322. dspcntr |= DISPPLANE_BGRX565;
  2323. break;
  2324. case DRM_FORMAT_XRGB8888:
  2325. dspcntr |= DISPPLANE_BGRX888;
  2326. break;
  2327. case DRM_FORMAT_XBGR8888:
  2328. dspcntr |= DISPPLANE_RGBX888;
  2329. break;
  2330. case DRM_FORMAT_XRGB2101010:
  2331. dspcntr |= DISPPLANE_BGRX101010;
  2332. break;
  2333. case DRM_FORMAT_XBGR2101010:
  2334. dspcntr |= DISPPLANE_RGBX101010;
  2335. break;
  2336. default:
  2337. BUG();
  2338. }
  2339. if (INTEL_INFO(dev)->gen >= 4 &&
  2340. obj->tiling_mode != I915_TILING_NONE)
  2341. dspcntr |= DISPPLANE_TILED;
  2342. if (IS_G4X(dev))
  2343. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2344. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2345. if (INTEL_INFO(dev)->gen >= 4) {
  2346. intel_crtc->dspaddr_offset =
  2347. intel_gen4_compute_page_offset(dev_priv,
  2348. &x, &y, obj->tiling_mode,
  2349. pixel_size,
  2350. fb->pitches[0]);
  2351. linear_offset -= intel_crtc->dspaddr_offset;
  2352. } else {
  2353. intel_crtc->dspaddr_offset = linear_offset;
  2354. }
  2355. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2356. dspcntr |= DISPPLANE_ROTATE_180;
  2357. x += (intel_crtc->config->pipe_src_w - 1);
  2358. y += (intel_crtc->config->pipe_src_h - 1);
  2359. /* Finding the last pixel of the last line of the display
  2360. data and adding to linear_offset*/
  2361. linear_offset +=
  2362. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2363. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2364. }
  2365. intel_crtc->adjusted_x = x;
  2366. intel_crtc->adjusted_y = y;
  2367. I915_WRITE(reg, dspcntr);
  2368. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2369. if (INTEL_INFO(dev)->gen >= 4) {
  2370. I915_WRITE(DSPSURF(plane),
  2371. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2372. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2373. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2374. } else
  2375. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2376. POSTING_READ(reg);
  2377. }
  2378. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2379. struct drm_framebuffer *fb,
  2380. int x, int y)
  2381. {
  2382. struct drm_device *dev = crtc->dev;
  2383. struct drm_i915_private *dev_priv = dev->dev_private;
  2384. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2385. struct drm_plane *primary = crtc->primary;
  2386. bool visible = to_intel_plane_state(primary->state)->visible;
  2387. struct drm_i915_gem_object *obj;
  2388. int plane = intel_crtc->plane;
  2389. unsigned long linear_offset;
  2390. u32 dspcntr;
  2391. u32 reg = DSPCNTR(plane);
  2392. int pixel_size;
  2393. if (!visible || !fb) {
  2394. I915_WRITE(reg, 0);
  2395. I915_WRITE(DSPSURF(plane), 0);
  2396. POSTING_READ(reg);
  2397. return;
  2398. }
  2399. obj = intel_fb_obj(fb);
  2400. if (WARN_ON(obj == NULL))
  2401. return;
  2402. pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  2403. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2404. dspcntr |= DISPLAY_PLANE_ENABLE;
  2405. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2406. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2407. switch (fb->pixel_format) {
  2408. case DRM_FORMAT_C8:
  2409. dspcntr |= DISPPLANE_8BPP;
  2410. break;
  2411. case DRM_FORMAT_RGB565:
  2412. dspcntr |= DISPPLANE_BGRX565;
  2413. break;
  2414. case DRM_FORMAT_XRGB8888:
  2415. dspcntr |= DISPPLANE_BGRX888;
  2416. break;
  2417. case DRM_FORMAT_XBGR8888:
  2418. dspcntr |= DISPPLANE_RGBX888;
  2419. break;
  2420. case DRM_FORMAT_XRGB2101010:
  2421. dspcntr |= DISPPLANE_BGRX101010;
  2422. break;
  2423. case DRM_FORMAT_XBGR2101010:
  2424. dspcntr |= DISPPLANE_RGBX101010;
  2425. break;
  2426. default:
  2427. BUG();
  2428. }
  2429. if (obj->tiling_mode != I915_TILING_NONE)
  2430. dspcntr |= DISPPLANE_TILED;
  2431. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
  2432. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2433. linear_offset = y * fb->pitches[0] + x * pixel_size;
  2434. intel_crtc->dspaddr_offset =
  2435. intel_gen4_compute_page_offset(dev_priv,
  2436. &x, &y, obj->tiling_mode,
  2437. pixel_size,
  2438. fb->pitches[0]);
  2439. linear_offset -= intel_crtc->dspaddr_offset;
  2440. if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
  2441. dspcntr |= DISPPLANE_ROTATE_180;
  2442. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  2443. x += (intel_crtc->config->pipe_src_w - 1);
  2444. y += (intel_crtc->config->pipe_src_h - 1);
  2445. /* Finding the last pixel of the last line of the display
  2446. data and adding to linear_offset*/
  2447. linear_offset +=
  2448. (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
  2449. (intel_crtc->config->pipe_src_w - 1) * pixel_size;
  2450. }
  2451. }
  2452. intel_crtc->adjusted_x = x;
  2453. intel_crtc->adjusted_y = y;
  2454. I915_WRITE(reg, dspcntr);
  2455. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2456. I915_WRITE(DSPSURF(plane),
  2457. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2458. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2459. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2460. } else {
  2461. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2462. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2463. }
  2464. POSTING_READ(reg);
  2465. }
  2466. u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
  2467. uint32_t pixel_format)
  2468. {
  2469. u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
  2470. /*
  2471. * The stride is either expressed as a multiple of 64 bytes
  2472. * chunks for linear buffers or in number of tiles for tiled
  2473. * buffers.
  2474. */
  2475. switch (fb_modifier) {
  2476. case DRM_FORMAT_MOD_NONE:
  2477. return 64;
  2478. case I915_FORMAT_MOD_X_TILED:
  2479. if (INTEL_INFO(dev)->gen == 2)
  2480. return 128;
  2481. return 512;
  2482. case I915_FORMAT_MOD_Y_TILED:
  2483. /* No need to check for old gens and Y tiling since this is
  2484. * about the display engine and those will be blocked before
  2485. * we get here.
  2486. */
  2487. return 128;
  2488. case I915_FORMAT_MOD_Yf_TILED:
  2489. if (bits_per_pixel == 8)
  2490. return 64;
  2491. else
  2492. return 128;
  2493. default:
  2494. MISSING_CASE(fb_modifier);
  2495. return 64;
  2496. }
  2497. }
  2498. u32 intel_plane_obj_offset(struct intel_plane *intel_plane,
  2499. struct drm_i915_gem_object *obj,
  2500. unsigned int plane)
  2501. {
  2502. const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
  2503. struct i915_vma *vma;
  2504. u64 offset;
  2505. if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
  2506. view = &i915_ggtt_view_rotated;
  2507. vma = i915_gem_obj_to_ggtt_view(obj, view);
  2508. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2509. view->type))
  2510. return -1;
  2511. offset = vma->node.start;
  2512. if (plane == 1) {
  2513. offset += vma->ggtt_view.rotation_info.uv_start_page *
  2514. PAGE_SIZE;
  2515. }
  2516. WARN_ON(upper_32_bits(offset));
  2517. return lower_32_bits(offset);
  2518. }
  2519. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2520. {
  2521. struct drm_device *dev = intel_crtc->base.dev;
  2522. struct drm_i915_private *dev_priv = dev->dev_private;
  2523. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2524. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2525. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2526. }
  2527. /*
  2528. * This function detaches (aka. unbinds) unused scalers in hardware
  2529. */
  2530. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2531. {
  2532. struct intel_crtc_scaler_state *scaler_state;
  2533. int i;
  2534. scaler_state = &intel_crtc->config->scaler_state;
  2535. /* loop through and disable scalers that aren't in use */
  2536. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2537. if (!scaler_state->scalers[i].in_use)
  2538. skl_detach_scaler(intel_crtc, i);
  2539. }
  2540. }
  2541. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2542. {
  2543. switch (pixel_format) {
  2544. case DRM_FORMAT_C8:
  2545. return PLANE_CTL_FORMAT_INDEXED;
  2546. case DRM_FORMAT_RGB565:
  2547. return PLANE_CTL_FORMAT_RGB_565;
  2548. case DRM_FORMAT_XBGR8888:
  2549. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2550. case DRM_FORMAT_XRGB8888:
  2551. return PLANE_CTL_FORMAT_XRGB_8888;
  2552. /*
  2553. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2554. * to be already pre-multiplied. We need to add a knob (or a different
  2555. * DRM_FORMAT) for user-space to configure that.
  2556. */
  2557. case DRM_FORMAT_ABGR8888:
  2558. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2559. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2560. case DRM_FORMAT_ARGB8888:
  2561. return PLANE_CTL_FORMAT_XRGB_8888 |
  2562. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2563. case DRM_FORMAT_XRGB2101010:
  2564. return PLANE_CTL_FORMAT_XRGB_2101010;
  2565. case DRM_FORMAT_XBGR2101010:
  2566. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2567. case DRM_FORMAT_YUYV:
  2568. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2569. case DRM_FORMAT_YVYU:
  2570. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2571. case DRM_FORMAT_UYVY:
  2572. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2573. case DRM_FORMAT_VYUY:
  2574. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2575. default:
  2576. MISSING_CASE(pixel_format);
  2577. }
  2578. return 0;
  2579. }
  2580. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2581. {
  2582. switch (fb_modifier) {
  2583. case DRM_FORMAT_MOD_NONE:
  2584. break;
  2585. case I915_FORMAT_MOD_X_TILED:
  2586. return PLANE_CTL_TILED_X;
  2587. case I915_FORMAT_MOD_Y_TILED:
  2588. return PLANE_CTL_TILED_Y;
  2589. case I915_FORMAT_MOD_Yf_TILED:
  2590. return PLANE_CTL_TILED_YF;
  2591. default:
  2592. MISSING_CASE(fb_modifier);
  2593. }
  2594. return 0;
  2595. }
  2596. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2597. {
  2598. switch (rotation) {
  2599. case BIT(DRM_ROTATE_0):
  2600. break;
  2601. /*
  2602. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2603. * while i915 HW rotation is clockwise, thats why this swapping.
  2604. */
  2605. case BIT(DRM_ROTATE_90):
  2606. return PLANE_CTL_ROTATE_270;
  2607. case BIT(DRM_ROTATE_180):
  2608. return PLANE_CTL_ROTATE_180;
  2609. case BIT(DRM_ROTATE_270):
  2610. return PLANE_CTL_ROTATE_90;
  2611. default:
  2612. MISSING_CASE(rotation);
  2613. }
  2614. return 0;
  2615. }
  2616. static void skylake_update_primary_plane(struct drm_crtc *crtc,
  2617. struct drm_framebuffer *fb,
  2618. int x, int y)
  2619. {
  2620. struct drm_device *dev = crtc->dev;
  2621. struct drm_i915_private *dev_priv = dev->dev_private;
  2622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2623. struct drm_plane *plane = crtc->primary;
  2624. bool visible = to_intel_plane_state(plane->state)->visible;
  2625. struct drm_i915_gem_object *obj;
  2626. int pipe = intel_crtc->pipe;
  2627. u32 plane_ctl, stride_div, stride;
  2628. u32 tile_height, plane_offset, plane_size;
  2629. unsigned int rotation;
  2630. int x_offset, y_offset;
  2631. u32 surf_addr;
  2632. struct intel_crtc_state *crtc_state = intel_crtc->config;
  2633. struct intel_plane_state *plane_state;
  2634. int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
  2635. int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
  2636. int scaler_id = -1;
  2637. plane_state = to_intel_plane_state(plane->state);
  2638. if (!visible || !fb) {
  2639. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2640. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2641. POSTING_READ(PLANE_CTL(pipe, 0));
  2642. return;
  2643. }
  2644. plane_ctl = PLANE_CTL_ENABLE |
  2645. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2646. PLANE_CTL_PIPE_CSC_ENABLE;
  2647. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2648. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2649. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2650. rotation = plane->state->rotation;
  2651. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2652. obj = intel_fb_obj(fb);
  2653. stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
  2654. fb->pixel_format);
  2655. surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj, 0);
  2656. WARN_ON(drm_rect_width(&plane_state->src) == 0);
  2657. scaler_id = plane_state->scaler_id;
  2658. src_x = plane_state->src.x1 >> 16;
  2659. src_y = plane_state->src.y1 >> 16;
  2660. src_w = drm_rect_width(&plane_state->src) >> 16;
  2661. src_h = drm_rect_height(&plane_state->src) >> 16;
  2662. dst_x = plane_state->dst.x1;
  2663. dst_y = plane_state->dst.y1;
  2664. dst_w = drm_rect_width(&plane_state->dst);
  2665. dst_h = drm_rect_height(&plane_state->dst);
  2666. WARN_ON(x != src_x || y != src_y);
  2667. if (intel_rotation_90_or_270(rotation)) {
  2668. /* stride = Surface height in tiles */
  2669. tile_height = intel_tile_height(dev, fb->pixel_format,
  2670. fb->modifier[0], 0);
  2671. stride = DIV_ROUND_UP(fb->height, tile_height);
  2672. x_offset = stride * tile_height - y - src_h;
  2673. y_offset = x;
  2674. plane_size = (src_w - 1) << 16 | (src_h - 1);
  2675. } else {
  2676. stride = fb->pitches[0] / stride_div;
  2677. x_offset = x;
  2678. y_offset = y;
  2679. plane_size = (src_h - 1) << 16 | (src_w - 1);
  2680. }
  2681. plane_offset = y_offset << 16 | x_offset;
  2682. intel_crtc->adjusted_x = x_offset;
  2683. intel_crtc->adjusted_y = y_offset;
  2684. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2685. I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
  2686. I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
  2687. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2688. if (scaler_id >= 0) {
  2689. uint32_t ps_ctrl = 0;
  2690. WARN_ON(!dst_w || !dst_h);
  2691. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2692. crtc_state->scaler_state.scalers[scaler_id].mode;
  2693. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2694. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2695. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2696. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2697. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2698. } else {
  2699. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2700. }
  2701. I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
  2702. POSTING_READ(PLANE_SURF(pipe, 0));
  2703. }
  2704. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2705. static int
  2706. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2707. int x, int y, enum mode_set_atomic state)
  2708. {
  2709. struct drm_device *dev = crtc->dev;
  2710. struct drm_i915_private *dev_priv = dev->dev_private;
  2711. if (dev_priv->fbc.disable_fbc)
  2712. dev_priv->fbc.disable_fbc(dev_priv);
  2713. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2714. return 0;
  2715. }
  2716. static void intel_complete_page_flips(struct drm_device *dev)
  2717. {
  2718. struct drm_crtc *crtc;
  2719. for_each_crtc(dev, crtc) {
  2720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2721. enum plane plane = intel_crtc->plane;
  2722. intel_prepare_page_flip(dev, plane);
  2723. intel_finish_page_flip_plane(dev, plane);
  2724. }
  2725. }
  2726. static void intel_update_primary_planes(struct drm_device *dev)
  2727. {
  2728. struct drm_crtc *crtc;
  2729. for_each_crtc(dev, crtc) {
  2730. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2731. struct intel_plane_state *plane_state;
  2732. drm_modeset_lock_crtc(crtc, &plane->base);
  2733. plane_state = to_intel_plane_state(plane->base.state);
  2734. if (crtc->state->active && plane_state->base.fb)
  2735. plane->commit_plane(&plane->base, plane_state);
  2736. drm_modeset_unlock_crtc(crtc);
  2737. }
  2738. }
  2739. void intel_prepare_reset(struct drm_device *dev)
  2740. {
  2741. /* no reset support for gen2 */
  2742. if (IS_GEN2(dev))
  2743. return;
  2744. /* reset doesn't touch the display */
  2745. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  2746. return;
  2747. drm_modeset_lock_all(dev);
  2748. /*
  2749. * Disabling the crtcs gracefully seems nicer. Also the
  2750. * g33 docs say we should at least disable all the planes.
  2751. */
  2752. intel_display_suspend(dev);
  2753. }
  2754. void intel_finish_reset(struct drm_device *dev)
  2755. {
  2756. struct drm_i915_private *dev_priv = to_i915(dev);
  2757. /*
  2758. * Flips in the rings will be nuked by the reset,
  2759. * so complete all pending flips so that user space
  2760. * will get its events and not get stuck.
  2761. */
  2762. intel_complete_page_flips(dev);
  2763. /* no reset support for gen2 */
  2764. if (IS_GEN2(dev))
  2765. return;
  2766. /* reset doesn't touch the display */
  2767. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
  2768. /*
  2769. * Flips in the rings have been nuked by the reset,
  2770. * so update the base address of all primary
  2771. * planes to the the last fb to make sure we're
  2772. * showing the correct fb after a reset.
  2773. *
  2774. * FIXME: Atomic will make this obsolete since we won't schedule
  2775. * CS-based flips (which might get lost in gpu resets) any more.
  2776. */
  2777. intel_update_primary_planes(dev);
  2778. return;
  2779. }
  2780. /*
  2781. * The display has been reset as well,
  2782. * so need a full re-initialization.
  2783. */
  2784. intel_runtime_pm_disable_interrupts(dev_priv);
  2785. intel_runtime_pm_enable_interrupts(dev_priv);
  2786. intel_modeset_init_hw(dev);
  2787. spin_lock_irq(&dev_priv->irq_lock);
  2788. if (dev_priv->display.hpd_irq_setup)
  2789. dev_priv->display.hpd_irq_setup(dev);
  2790. spin_unlock_irq(&dev_priv->irq_lock);
  2791. intel_display_resume(dev);
  2792. intel_hpd_init(dev_priv);
  2793. drm_modeset_unlock_all(dev);
  2794. }
  2795. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2796. {
  2797. struct drm_device *dev = crtc->dev;
  2798. struct drm_i915_private *dev_priv = dev->dev_private;
  2799. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2800. bool pending;
  2801. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2802. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2803. return false;
  2804. spin_lock_irq(&dev->event_lock);
  2805. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2806. spin_unlock_irq(&dev->event_lock);
  2807. return pending;
  2808. }
  2809. static void intel_update_pipe_config(struct intel_crtc *crtc,
  2810. struct intel_crtc_state *old_crtc_state)
  2811. {
  2812. struct drm_device *dev = crtc->base.dev;
  2813. struct drm_i915_private *dev_priv = dev->dev_private;
  2814. struct intel_crtc_state *pipe_config =
  2815. to_intel_crtc_state(crtc->base.state);
  2816. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  2817. crtc->base.mode = crtc->base.state->mode;
  2818. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  2819. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  2820. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  2821. if (HAS_DDI(dev))
  2822. intel_set_pipe_csc(&crtc->base);
  2823. /*
  2824. * Update pipe size and adjust fitter if needed: the reason for this is
  2825. * that in compute_mode_changes we check the native mode (not the pfit
  2826. * mode) to see if we can flip rather than do a full mode set. In the
  2827. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2828. * pfit state, we'll end up with a big fb scanned out into the wrong
  2829. * sized surface.
  2830. */
  2831. I915_WRITE(PIPESRC(crtc->pipe),
  2832. ((pipe_config->pipe_src_w - 1) << 16) |
  2833. (pipe_config->pipe_src_h - 1));
  2834. /* on skylake this is done by detaching scalers */
  2835. if (INTEL_INFO(dev)->gen >= 9) {
  2836. skl_detach_scalers(crtc);
  2837. if (pipe_config->pch_pfit.enabled)
  2838. skylake_pfit_enable(crtc);
  2839. } else if (HAS_PCH_SPLIT(dev)) {
  2840. if (pipe_config->pch_pfit.enabled)
  2841. ironlake_pfit_enable(crtc);
  2842. else if (old_crtc_state->pch_pfit.enabled)
  2843. ironlake_pfit_disable(crtc, true);
  2844. }
  2845. }
  2846. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2847. {
  2848. struct drm_device *dev = crtc->dev;
  2849. struct drm_i915_private *dev_priv = dev->dev_private;
  2850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2851. int pipe = intel_crtc->pipe;
  2852. u32 reg, temp;
  2853. /* enable normal train */
  2854. reg = FDI_TX_CTL(pipe);
  2855. temp = I915_READ(reg);
  2856. if (IS_IVYBRIDGE(dev)) {
  2857. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2858. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2859. } else {
  2860. temp &= ~FDI_LINK_TRAIN_NONE;
  2861. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2862. }
  2863. I915_WRITE(reg, temp);
  2864. reg = FDI_RX_CTL(pipe);
  2865. temp = I915_READ(reg);
  2866. if (HAS_PCH_CPT(dev)) {
  2867. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2868. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2869. } else {
  2870. temp &= ~FDI_LINK_TRAIN_NONE;
  2871. temp |= FDI_LINK_TRAIN_NONE;
  2872. }
  2873. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2874. /* wait one idle pattern time */
  2875. POSTING_READ(reg);
  2876. udelay(1000);
  2877. /* IVB wants error correction enabled */
  2878. if (IS_IVYBRIDGE(dev))
  2879. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2880. FDI_FE_ERRC_ENABLE);
  2881. }
  2882. /* The FDI link training functions for ILK/Ibexpeak. */
  2883. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2884. {
  2885. struct drm_device *dev = crtc->dev;
  2886. struct drm_i915_private *dev_priv = dev->dev_private;
  2887. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2888. int pipe = intel_crtc->pipe;
  2889. u32 reg, temp, tries;
  2890. /* FDI needs bits from pipe first */
  2891. assert_pipe_enabled(dev_priv, pipe);
  2892. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2893. for train result */
  2894. reg = FDI_RX_IMR(pipe);
  2895. temp = I915_READ(reg);
  2896. temp &= ~FDI_RX_SYMBOL_LOCK;
  2897. temp &= ~FDI_RX_BIT_LOCK;
  2898. I915_WRITE(reg, temp);
  2899. I915_READ(reg);
  2900. udelay(150);
  2901. /* enable CPU FDI TX and PCH FDI RX */
  2902. reg = FDI_TX_CTL(pipe);
  2903. temp = I915_READ(reg);
  2904. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2905. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2906. temp &= ~FDI_LINK_TRAIN_NONE;
  2907. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2908. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2909. reg = FDI_RX_CTL(pipe);
  2910. temp = I915_READ(reg);
  2911. temp &= ~FDI_LINK_TRAIN_NONE;
  2912. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2913. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2914. POSTING_READ(reg);
  2915. udelay(150);
  2916. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2917. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2918. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2919. FDI_RX_PHASE_SYNC_POINTER_EN);
  2920. reg = FDI_RX_IIR(pipe);
  2921. for (tries = 0; tries < 5; tries++) {
  2922. temp = I915_READ(reg);
  2923. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2924. if ((temp & FDI_RX_BIT_LOCK)) {
  2925. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2926. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2927. break;
  2928. }
  2929. }
  2930. if (tries == 5)
  2931. DRM_ERROR("FDI train 1 fail!\n");
  2932. /* Train 2 */
  2933. reg = FDI_TX_CTL(pipe);
  2934. temp = I915_READ(reg);
  2935. temp &= ~FDI_LINK_TRAIN_NONE;
  2936. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2937. I915_WRITE(reg, temp);
  2938. reg = FDI_RX_CTL(pipe);
  2939. temp = I915_READ(reg);
  2940. temp &= ~FDI_LINK_TRAIN_NONE;
  2941. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2942. I915_WRITE(reg, temp);
  2943. POSTING_READ(reg);
  2944. udelay(150);
  2945. reg = FDI_RX_IIR(pipe);
  2946. for (tries = 0; tries < 5; tries++) {
  2947. temp = I915_READ(reg);
  2948. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2949. if (temp & FDI_RX_SYMBOL_LOCK) {
  2950. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2951. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2952. break;
  2953. }
  2954. }
  2955. if (tries == 5)
  2956. DRM_ERROR("FDI train 2 fail!\n");
  2957. DRM_DEBUG_KMS("FDI train done\n");
  2958. }
  2959. static const int snb_b_fdi_train_param[] = {
  2960. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2961. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2962. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2963. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2964. };
  2965. /* The FDI link training functions for SNB/Cougarpoint. */
  2966. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2967. {
  2968. struct drm_device *dev = crtc->dev;
  2969. struct drm_i915_private *dev_priv = dev->dev_private;
  2970. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2971. int pipe = intel_crtc->pipe;
  2972. u32 reg, temp, i, retry;
  2973. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2974. for train result */
  2975. reg = FDI_RX_IMR(pipe);
  2976. temp = I915_READ(reg);
  2977. temp &= ~FDI_RX_SYMBOL_LOCK;
  2978. temp &= ~FDI_RX_BIT_LOCK;
  2979. I915_WRITE(reg, temp);
  2980. POSTING_READ(reg);
  2981. udelay(150);
  2982. /* enable CPU FDI TX and PCH FDI RX */
  2983. reg = FDI_TX_CTL(pipe);
  2984. temp = I915_READ(reg);
  2985. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2986. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  2987. temp &= ~FDI_LINK_TRAIN_NONE;
  2988. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2989. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2990. /* SNB-B */
  2991. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2992. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2993. I915_WRITE(FDI_RX_MISC(pipe),
  2994. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2995. reg = FDI_RX_CTL(pipe);
  2996. temp = I915_READ(reg);
  2997. if (HAS_PCH_CPT(dev)) {
  2998. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2999. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3000. } else {
  3001. temp &= ~FDI_LINK_TRAIN_NONE;
  3002. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3003. }
  3004. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3005. POSTING_READ(reg);
  3006. udelay(150);
  3007. for (i = 0; i < 4; i++) {
  3008. reg = FDI_TX_CTL(pipe);
  3009. temp = I915_READ(reg);
  3010. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3011. temp |= snb_b_fdi_train_param[i];
  3012. I915_WRITE(reg, temp);
  3013. POSTING_READ(reg);
  3014. udelay(500);
  3015. for (retry = 0; retry < 5; retry++) {
  3016. reg = FDI_RX_IIR(pipe);
  3017. temp = I915_READ(reg);
  3018. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3019. if (temp & FDI_RX_BIT_LOCK) {
  3020. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3021. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3022. break;
  3023. }
  3024. udelay(50);
  3025. }
  3026. if (retry < 5)
  3027. break;
  3028. }
  3029. if (i == 4)
  3030. DRM_ERROR("FDI train 1 fail!\n");
  3031. /* Train 2 */
  3032. reg = FDI_TX_CTL(pipe);
  3033. temp = I915_READ(reg);
  3034. temp &= ~FDI_LINK_TRAIN_NONE;
  3035. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3036. if (IS_GEN6(dev)) {
  3037. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3038. /* SNB-B */
  3039. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3040. }
  3041. I915_WRITE(reg, temp);
  3042. reg = FDI_RX_CTL(pipe);
  3043. temp = I915_READ(reg);
  3044. if (HAS_PCH_CPT(dev)) {
  3045. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3046. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3047. } else {
  3048. temp &= ~FDI_LINK_TRAIN_NONE;
  3049. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3050. }
  3051. I915_WRITE(reg, temp);
  3052. POSTING_READ(reg);
  3053. udelay(150);
  3054. for (i = 0; i < 4; i++) {
  3055. reg = FDI_TX_CTL(pipe);
  3056. temp = I915_READ(reg);
  3057. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3058. temp |= snb_b_fdi_train_param[i];
  3059. I915_WRITE(reg, temp);
  3060. POSTING_READ(reg);
  3061. udelay(500);
  3062. for (retry = 0; retry < 5; retry++) {
  3063. reg = FDI_RX_IIR(pipe);
  3064. temp = I915_READ(reg);
  3065. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3066. if (temp & FDI_RX_SYMBOL_LOCK) {
  3067. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3068. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3069. break;
  3070. }
  3071. udelay(50);
  3072. }
  3073. if (retry < 5)
  3074. break;
  3075. }
  3076. if (i == 4)
  3077. DRM_ERROR("FDI train 2 fail!\n");
  3078. DRM_DEBUG_KMS("FDI train done.\n");
  3079. }
  3080. /* Manual link training for Ivy Bridge A0 parts */
  3081. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3082. {
  3083. struct drm_device *dev = crtc->dev;
  3084. struct drm_i915_private *dev_priv = dev->dev_private;
  3085. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3086. int pipe = intel_crtc->pipe;
  3087. u32 reg, temp, i, j;
  3088. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3089. for train result */
  3090. reg = FDI_RX_IMR(pipe);
  3091. temp = I915_READ(reg);
  3092. temp &= ~FDI_RX_SYMBOL_LOCK;
  3093. temp &= ~FDI_RX_BIT_LOCK;
  3094. I915_WRITE(reg, temp);
  3095. POSTING_READ(reg);
  3096. udelay(150);
  3097. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3098. I915_READ(FDI_RX_IIR(pipe)));
  3099. /* Try each vswing and preemphasis setting twice before moving on */
  3100. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3101. /* disable first in case we need to retry */
  3102. reg = FDI_TX_CTL(pipe);
  3103. temp = I915_READ(reg);
  3104. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3105. temp &= ~FDI_TX_ENABLE;
  3106. I915_WRITE(reg, temp);
  3107. reg = FDI_RX_CTL(pipe);
  3108. temp = I915_READ(reg);
  3109. temp &= ~FDI_LINK_TRAIN_AUTO;
  3110. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3111. temp &= ~FDI_RX_ENABLE;
  3112. I915_WRITE(reg, temp);
  3113. /* enable CPU FDI TX and PCH FDI RX */
  3114. reg = FDI_TX_CTL(pipe);
  3115. temp = I915_READ(reg);
  3116. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3117. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3118. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3119. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3120. temp |= snb_b_fdi_train_param[j/2];
  3121. temp |= FDI_COMPOSITE_SYNC;
  3122. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3123. I915_WRITE(FDI_RX_MISC(pipe),
  3124. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3125. reg = FDI_RX_CTL(pipe);
  3126. temp = I915_READ(reg);
  3127. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3128. temp |= FDI_COMPOSITE_SYNC;
  3129. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3130. POSTING_READ(reg);
  3131. udelay(1); /* should be 0.5us */
  3132. for (i = 0; i < 4; i++) {
  3133. reg = FDI_RX_IIR(pipe);
  3134. temp = I915_READ(reg);
  3135. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3136. if (temp & FDI_RX_BIT_LOCK ||
  3137. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3138. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3139. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3140. i);
  3141. break;
  3142. }
  3143. udelay(1); /* should be 0.5us */
  3144. }
  3145. if (i == 4) {
  3146. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3147. continue;
  3148. }
  3149. /* Train 2 */
  3150. reg = FDI_TX_CTL(pipe);
  3151. temp = I915_READ(reg);
  3152. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3153. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3154. I915_WRITE(reg, temp);
  3155. reg = FDI_RX_CTL(pipe);
  3156. temp = I915_READ(reg);
  3157. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3158. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3159. I915_WRITE(reg, temp);
  3160. POSTING_READ(reg);
  3161. udelay(2); /* should be 1.5us */
  3162. for (i = 0; i < 4; i++) {
  3163. reg = FDI_RX_IIR(pipe);
  3164. temp = I915_READ(reg);
  3165. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3166. if (temp & FDI_RX_SYMBOL_LOCK ||
  3167. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3168. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3169. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3170. i);
  3171. goto train_done;
  3172. }
  3173. udelay(2); /* should be 1.5us */
  3174. }
  3175. if (i == 4)
  3176. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3177. }
  3178. train_done:
  3179. DRM_DEBUG_KMS("FDI train done.\n");
  3180. }
  3181. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3182. {
  3183. struct drm_device *dev = intel_crtc->base.dev;
  3184. struct drm_i915_private *dev_priv = dev->dev_private;
  3185. int pipe = intel_crtc->pipe;
  3186. u32 reg, temp;
  3187. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3188. reg = FDI_RX_CTL(pipe);
  3189. temp = I915_READ(reg);
  3190. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3191. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3192. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3193. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3194. POSTING_READ(reg);
  3195. udelay(200);
  3196. /* Switch from Rawclk to PCDclk */
  3197. temp = I915_READ(reg);
  3198. I915_WRITE(reg, temp | FDI_PCDCLK);
  3199. POSTING_READ(reg);
  3200. udelay(200);
  3201. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3202. reg = FDI_TX_CTL(pipe);
  3203. temp = I915_READ(reg);
  3204. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3205. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3206. POSTING_READ(reg);
  3207. udelay(100);
  3208. }
  3209. }
  3210. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3211. {
  3212. struct drm_device *dev = intel_crtc->base.dev;
  3213. struct drm_i915_private *dev_priv = dev->dev_private;
  3214. int pipe = intel_crtc->pipe;
  3215. u32 reg, temp;
  3216. /* Switch from PCDclk to Rawclk */
  3217. reg = FDI_RX_CTL(pipe);
  3218. temp = I915_READ(reg);
  3219. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3220. /* Disable CPU FDI TX PLL */
  3221. reg = FDI_TX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3224. POSTING_READ(reg);
  3225. udelay(100);
  3226. reg = FDI_RX_CTL(pipe);
  3227. temp = I915_READ(reg);
  3228. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3229. /* Wait for the clocks to turn off. */
  3230. POSTING_READ(reg);
  3231. udelay(100);
  3232. }
  3233. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3234. {
  3235. struct drm_device *dev = crtc->dev;
  3236. struct drm_i915_private *dev_priv = dev->dev_private;
  3237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3238. int pipe = intel_crtc->pipe;
  3239. u32 reg, temp;
  3240. /* disable CPU FDI tx and PCH FDI rx */
  3241. reg = FDI_TX_CTL(pipe);
  3242. temp = I915_READ(reg);
  3243. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3244. POSTING_READ(reg);
  3245. reg = FDI_RX_CTL(pipe);
  3246. temp = I915_READ(reg);
  3247. temp &= ~(0x7 << 16);
  3248. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3249. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3250. POSTING_READ(reg);
  3251. udelay(100);
  3252. /* Ironlake workaround, disable clock pointer after downing FDI */
  3253. if (HAS_PCH_IBX(dev))
  3254. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3255. /* still set train pattern 1 */
  3256. reg = FDI_TX_CTL(pipe);
  3257. temp = I915_READ(reg);
  3258. temp &= ~FDI_LINK_TRAIN_NONE;
  3259. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3260. I915_WRITE(reg, temp);
  3261. reg = FDI_RX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. if (HAS_PCH_CPT(dev)) {
  3264. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3265. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3266. } else {
  3267. temp &= ~FDI_LINK_TRAIN_NONE;
  3268. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3269. }
  3270. /* BPC in FDI rx is consistent with that in PIPECONF */
  3271. temp &= ~(0x07 << 16);
  3272. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3273. I915_WRITE(reg, temp);
  3274. POSTING_READ(reg);
  3275. udelay(100);
  3276. }
  3277. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3278. {
  3279. struct intel_crtc *crtc;
  3280. /* Note that we don't need to be called with mode_config.lock here
  3281. * as our list of CRTC objects is static for the lifetime of the
  3282. * device and so cannot disappear as we iterate. Similarly, we can
  3283. * happily treat the predicates as racy, atomic checks as userspace
  3284. * cannot claim and pin a new fb without at least acquring the
  3285. * struct_mutex and so serialising with us.
  3286. */
  3287. for_each_intel_crtc(dev, crtc) {
  3288. if (atomic_read(&crtc->unpin_work_count) == 0)
  3289. continue;
  3290. if (crtc->unpin_work)
  3291. intel_wait_for_vblank(dev, crtc->pipe);
  3292. return true;
  3293. }
  3294. return false;
  3295. }
  3296. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3297. {
  3298. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3299. struct intel_unpin_work *work = intel_crtc->unpin_work;
  3300. /* ensure that the unpin work is consistent wrt ->pending. */
  3301. smp_rmb();
  3302. intel_crtc->unpin_work = NULL;
  3303. if (work->event)
  3304. drm_send_vblank_event(intel_crtc->base.dev,
  3305. intel_crtc->pipe,
  3306. work->event);
  3307. drm_crtc_vblank_put(&intel_crtc->base);
  3308. wake_up_all(&dev_priv->pending_flip_queue);
  3309. queue_work(dev_priv->wq, &work->work);
  3310. trace_i915_flip_complete(intel_crtc->plane,
  3311. work->pending_flip_obj);
  3312. }
  3313. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3314. {
  3315. struct drm_device *dev = crtc->dev;
  3316. struct drm_i915_private *dev_priv = dev->dev_private;
  3317. long ret;
  3318. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3319. ret = wait_event_interruptible_timeout(
  3320. dev_priv->pending_flip_queue,
  3321. !intel_crtc_has_pending_flip(crtc),
  3322. 60*HZ);
  3323. if (ret < 0)
  3324. return ret;
  3325. if (ret == 0) {
  3326. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3327. spin_lock_irq(&dev->event_lock);
  3328. if (intel_crtc->unpin_work) {
  3329. WARN_ONCE(1, "Removing stuck page flip\n");
  3330. page_flip_completed(intel_crtc);
  3331. }
  3332. spin_unlock_irq(&dev->event_lock);
  3333. }
  3334. return 0;
  3335. }
  3336. /* Program iCLKIP clock to the desired frequency */
  3337. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3338. {
  3339. struct drm_device *dev = crtc->dev;
  3340. struct drm_i915_private *dev_priv = dev->dev_private;
  3341. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3342. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3343. u32 temp;
  3344. mutex_lock(&dev_priv->sb_lock);
  3345. /* It is necessary to ungate the pixclk gate prior to programming
  3346. * the divisors, and gate it back when it is done.
  3347. */
  3348. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3349. /* Disable SSCCTL */
  3350. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  3351. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  3352. SBI_SSCCTL_DISABLE,
  3353. SBI_ICLK);
  3354. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  3355. if (clock == 20000) {
  3356. auxdiv = 1;
  3357. divsel = 0x41;
  3358. phaseinc = 0x20;
  3359. } else {
  3360. /* The iCLK virtual clock root frequency is in MHz,
  3361. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3362. * divisors, it is necessary to divide one by another, so we
  3363. * convert the virtual clock precision to KHz here for higher
  3364. * precision.
  3365. */
  3366. u32 iclk_virtual_root_freq = 172800 * 1000;
  3367. u32 iclk_pi_range = 64;
  3368. u32 desired_divisor, msb_divisor_value, pi_value;
  3369. desired_divisor = (iclk_virtual_root_freq / clock);
  3370. msb_divisor_value = desired_divisor / iclk_pi_range;
  3371. pi_value = desired_divisor % iclk_pi_range;
  3372. auxdiv = 0;
  3373. divsel = msb_divisor_value - 2;
  3374. phaseinc = pi_value;
  3375. }
  3376. /* This should not happen with any sane values */
  3377. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3378. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3379. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3380. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3381. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3382. clock,
  3383. auxdiv,
  3384. divsel,
  3385. phasedir,
  3386. phaseinc);
  3387. /* Program SSCDIVINTPHASE6 */
  3388. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3389. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3390. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3391. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3392. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3393. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3394. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3395. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3396. /* Program SSCAUXDIV */
  3397. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3398. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3399. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3400. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3401. /* Enable modulator and associated divider */
  3402. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3403. temp &= ~SBI_SSCCTL_DISABLE;
  3404. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3405. /* Wait for initialization time */
  3406. udelay(24);
  3407. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3408. mutex_unlock(&dev_priv->sb_lock);
  3409. }
  3410. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3411. enum pipe pch_transcoder)
  3412. {
  3413. struct drm_device *dev = crtc->base.dev;
  3414. struct drm_i915_private *dev_priv = dev->dev_private;
  3415. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3416. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3417. I915_READ(HTOTAL(cpu_transcoder)));
  3418. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3419. I915_READ(HBLANK(cpu_transcoder)));
  3420. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3421. I915_READ(HSYNC(cpu_transcoder)));
  3422. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3423. I915_READ(VTOTAL(cpu_transcoder)));
  3424. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3425. I915_READ(VBLANK(cpu_transcoder)));
  3426. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3427. I915_READ(VSYNC(cpu_transcoder)));
  3428. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3429. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3430. }
  3431. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3432. {
  3433. struct drm_i915_private *dev_priv = dev->dev_private;
  3434. uint32_t temp;
  3435. temp = I915_READ(SOUTH_CHICKEN1);
  3436. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3437. return;
  3438. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3439. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3440. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3441. if (enable)
  3442. temp |= FDI_BC_BIFURCATION_SELECT;
  3443. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3444. I915_WRITE(SOUTH_CHICKEN1, temp);
  3445. POSTING_READ(SOUTH_CHICKEN1);
  3446. }
  3447. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3448. {
  3449. struct drm_device *dev = intel_crtc->base.dev;
  3450. switch (intel_crtc->pipe) {
  3451. case PIPE_A:
  3452. break;
  3453. case PIPE_B:
  3454. if (intel_crtc->config->fdi_lanes > 2)
  3455. cpt_set_fdi_bc_bifurcation(dev, false);
  3456. else
  3457. cpt_set_fdi_bc_bifurcation(dev, true);
  3458. break;
  3459. case PIPE_C:
  3460. cpt_set_fdi_bc_bifurcation(dev, true);
  3461. break;
  3462. default:
  3463. BUG();
  3464. }
  3465. }
  3466. /* Return which DP Port should be selected for Transcoder DP control */
  3467. static enum port
  3468. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3469. {
  3470. struct drm_device *dev = crtc->dev;
  3471. struct intel_encoder *encoder;
  3472. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3473. if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
  3474. encoder->type == INTEL_OUTPUT_EDP)
  3475. return enc_to_dig_port(&encoder->base)->port;
  3476. }
  3477. return -1;
  3478. }
  3479. /*
  3480. * Enable PCH resources required for PCH ports:
  3481. * - PCH PLLs
  3482. * - FDI training & RX/TX
  3483. * - update transcoder timings
  3484. * - DP transcoding bits
  3485. * - transcoder
  3486. */
  3487. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3488. {
  3489. struct drm_device *dev = crtc->dev;
  3490. struct drm_i915_private *dev_priv = dev->dev_private;
  3491. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3492. int pipe = intel_crtc->pipe;
  3493. u32 reg, temp;
  3494. assert_pch_transcoder_disabled(dev_priv, pipe);
  3495. if (IS_IVYBRIDGE(dev))
  3496. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3497. /* Write the TU size bits before fdi link training, so that error
  3498. * detection works. */
  3499. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3500. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3501. /* For PCH output, training FDI link */
  3502. dev_priv->display.fdi_link_train(crtc);
  3503. /* We need to program the right clock selection before writing the pixel
  3504. * mutliplier into the DPLL. */
  3505. if (HAS_PCH_CPT(dev)) {
  3506. u32 sel;
  3507. temp = I915_READ(PCH_DPLL_SEL);
  3508. temp |= TRANS_DPLL_ENABLE(pipe);
  3509. sel = TRANS_DPLLB_SEL(pipe);
  3510. if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
  3511. temp |= sel;
  3512. else
  3513. temp &= ~sel;
  3514. I915_WRITE(PCH_DPLL_SEL, temp);
  3515. }
  3516. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3517. * transcoder, and we actually should do this to not upset any PCH
  3518. * transcoder that already use the clock when we share it.
  3519. *
  3520. * Note that enable_shared_dpll tries to do the right thing, but
  3521. * get_shared_dpll unconditionally resets the pll - we need that to have
  3522. * the right LVDS enable sequence. */
  3523. intel_enable_shared_dpll(intel_crtc);
  3524. /* set transcoder timing, panel must allow it */
  3525. assert_panel_unlocked(dev_priv, pipe);
  3526. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3527. intel_fdi_normal_train(crtc);
  3528. /* For PCH DP, enable TRANS_DP_CTL */
  3529. if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
  3530. const struct drm_display_mode *adjusted_mode =
  3531. &intel_crtc->config->base.adjusted_mode;
  3532. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3533. reg = TRANS_DP_CTL(pipe);
  3534. temp = I915_READ(reg);
  3535. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3536. TRANS_DP_SYNC_MASK |
  3537. TRANS_DP_BPC_MASK);
  3538. temp |= TRANS_DP_OUTPUT_ENABLE;
  3539. temp |= bpc << 9; /* same format but at 11:9 */
  3540. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3541. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3542. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3543. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3544. switch (intel_trans_dp_port_sel(crtc)) {
  3545. case PORT_B:
  3546. temp |= TRANS_DP_PORT_SEL_B;
  3547. break;
  3548. case PORT_C:
  3549. temp |= TRANS_DP_PORT_SEL_C;
  3550. break;
  3551. case PORT_D:
  3552. temp |= TRANS_DP_PORT_SEL_D;
  3553. break;
  3554. default:
  3555. BUG();
  3556. }
  3557. I915_WRITE(reg, temp);
  3558. }
  3559. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3560. }
  3561. static void lpt_pch_enable(struct drm_crtc *crtc)
  3562. {
  3563. struct drm_device *dev = crtc->dev;
  3564. struct drm_i915_private *dev_priv = dev->dev_private;
  3565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3566. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3567. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3568. lpt_program_iclkip(crtc);
  3569. /* Set transcoder timing. */
  3570. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3571. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3572. }
  3573. struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
  3574. struct intel_crtc_state *crtc_state)
  3575. {
  3576. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3577. struct intel_shared_dpll *pll;
  3578. struct intel_shared_dpll_config *shared_dpll;
  3579. enum intel_dpll_id i;
  3580. shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
  3581. if (HAS_PCH_IBX(dev_priv->dev)) {
  3582. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3583. i = (enum intel_dpll_id) crtc->pipe;
  3584. pll = &dev_priv->shared_dplls[i];
  3585. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3586. crtc->base.base.id, pll->name);
  3587. WARN_ON(shared_dpll[i].crtc_mask);
  3588. goto found;
  3589. }
  3590. if (IS_BROXTON(dev_priv->dev)) {
  3591. /* PLL is attached to port in bxt */
  3592. struct intel_encoder *encoder;
  3593. struct intel_digital_port *intel_dig_port;
  3594. encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
  3595. if (WARN_ON(!encoder))
  3596. return NULL;
  3597. intel_dig_port = enc_to_dig_port(&encoder->base);
  3598. /* 1:1 mapping between ports and PLLs */
  3599. i = (enum intel_dpll_id)intel_dig_port->port;
  3600. pll = &dev_priv->shared_dplls[i];
  3601. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3602. crtc->base.base.id, pll->name);
  3603. WARN_ON(shared_dpll[i].crtc_mask);
  3604. goto found;
  3605. }
  3606. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3607. pll = &dev_priv->shared_dplls[i];
  3608. /* Only want to check enabled timings first */
  3609. if (shared_dpll[i].crtc_mask == 0)
  3610. continue;
  3611. if (memcmp(&crtc_state->dpll_hw_state,
  3612. &shared_dpll[i].hw_state,
  3613. sizeof(crtc_state->dpll_hw_state)) == 0) {
  3614. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
  3615. crtc->base.base.id, pll->name,
  3616. shared_dpll[i].crtc_mask,
  3617. pll->active);
  3618. goto found;
  3619. }
  3620. }
  3621. /* Ok no matching timings, maybe there's a free one? */
  3622. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3623. pll = &dev_priv->shared_dplls[i];
  3624. if (shared_dpll[i].crtc_mask == 0) {
  3625. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3626. crtc->base.base.id, pll->name);
  3627. goto found;
  3628. }
  3629. }
  3630. return NULL;
  3631. found:
  3632. if (shared_dpll[i].crtc_mask == 0)
  3633. shared_dpll[i].hw_state =
  3634. crtc_state->dpll_hw_state;
  3635. crtc_state->shared_dpll = i;
  3636. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3637. pipe_name(crtc->pipe));
  3638. shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
  3639. return pll;
  3640. }
  3641. static void intel_shared_dpll_commit(struct drm_atomic_state *state)
  3642. {
  3643. struct drm_i915_private *dev_priv = to_i915(state->dev);
  3644. struct intel_shared_dpll_config *shared_dpll;
  3645. struct intel_shared_dpll *pll;
  3646. enum intel_dpll_id i;
  3647. if (!to_intel_atomic_state(state)->dpll_set)
  3648. return;
  3649. shared_dpll = to_intel_atomic_state(state)->shared_dpll;
  3650. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3651. pll = &dev_priv->shared_dplls[i];
  3652. pll->config = shared_dpll[i];
  3653. }
  3654. }
  3655. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3656. {
  3657. struct drm_i915_private *dev_priv = dev->dev_private;
  3658. int dslreg = PIPEDSL(pipe);
  3659. u32 temp;
  3660. temp = I915_READ(dslreg);
  3661. udelay(500);
  3662. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3663. if (wait_for(I915_READ(dslreg) != temp, 5))
  3664. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3665. }
  3666. }
  3667. static int
  3668. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3669. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3670. int src_w, int src_h, int dst_w, int dst_h)
  3671. {
  3672. struct intel_crtc_scaler_state *scaler_state =
  3673. &crtc_state->scaler_state;
  3674. struct intel_crtc *intel_crtc =
  3675. to_intel_crtc(crtc_state->base.crtc);
  3676. int need_scaling;
  3677. need_scaling = intel_rotation_90_or_270(rotation) ?
  3678. (src_h != dst_w || src_w != dst_h):
  3679. (src_w != dst_w || src_h != dst_h);
  3680. /*
  3681. * if plane is being disabled or scaler is no more required or force detach
  3682. * - free scaler binded to this plane/crtc
  3683. * - in order to do this, update crtc->scaler_usage
  3684. *
  3685. * Here scaler state in crtc_state is set free so that
  3686. * scaler can be assigned to other user. Actual register
  3687. * update to free the scaler is done in plane/panel-fit programming.
  3688. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3689. */
  3690. if (force_detach || !need_scaling) {
  3691. if (*scaler_id >= 0) {
  3692. scaler_state->scaler_users &= ~(1 << scaler_user);
  3693. scaler_state->scalers[*scaler_id].in_use = 0;
  3694. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3695. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3696. intel_crtc->pipe, scaler_user, *scaler_id,
  3697. scaler_state->scaler_users);
  3698. *scaler_id = -1;
  3699. }
  3700. return 0;
  3701. }
  3702. /* range checks */
  3703. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3704. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3705. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3706. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3707. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3708. "size is out of scaler range\n",
  3709. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3710. return -EINVAL;
  3711. }
  3712. /* mark this plane as a scaler user in crtc_state */
  3713. scaler_state->scaler_users |= (1 << scaler_user);
  3714. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3715. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3716. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3717. scaler_state->scaler_users);
  3718. return 0;
  3719. }
  3720. /**
  3721. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3722. *
  3723. * @state: crtc's scaler state
  3724. *
  3725. * Return
  3726. * 0 - scaler_usage updated successfully
  3727. * error - requested scaling cannot be supported or other error condition
  3728. */
  3729. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3730. {
  3731. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  3732. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3733. DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
  3734. intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
  3735. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3736. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3737. state->pipe_src_w, state->pipe_src_h,
  3738. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3739. }
  3740. /**
  3741. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3742. *
  3743. * @state: crtc's scaler state
  3744. * @plane_state: atomic plane state to update
  3745. *
  3746. * Return
  3747. * 0 - scaler_usage updated successfully
  3748. * error - requested scaling cannot be supported or other error condition
  3749. */
  3750. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3751. struct intel_plane_state *plane_state)
  3752. {
  3753. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  3754. struct intel_plane *intel_plane =
  3755. to_intel_plane(plane_state->base.plane);
  3756. struct drm_framebuffer *fb = plane_state->base.fb;
  3757. int ret;
  3758. bool force_detach = !fb || !plane_state->visible;
  3759. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
  3760. intel_plane->base.base.id, intel_crtc->pipe,
  3761. drm_plane_index(&intel_plane->base));
  3762. ret = skl_update_scaler(crtc_state, force_detach,
  3763. drm_plane_index(&intel_plane->base),
  3764. &plane_state->scaler_id,
  3765. plane_state->base.rotation,
  3766. drm_rect_width(&plane_state->src) >> 16,
  3767. drm_rect_height(&plane_state->src) >> 16,
  3768. drm_rect_width(&plane_state->dst),
  3769. drm_rect_height(&plane_state->dst));
  3770. if (ret || plane_state->scaler_id < 0)
  3771. return ret;
  3772. /* check colorkey */
  3773. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3774. DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
  3775. intel_plane->base.base.id);
  3776. return -EINVAL;
  3777. }
  3778. /* Check src format */
  3779. switch (fb->pixel_format) {
  3780. case DRM_FORMAT_RGB565:
  3781. case DRM_FORMAT_XBGR8888:
  3782. case DRM_FORMAT_XRGB8888:
  3783. case DRM_FORMAT_ABGR8888:
  3784. case DRM_FORMAT_ARGB8888:
  3785. case DRM_FORMAT_XRGB2101010:
  3786. case DRM_FORMAT_XBGR2101010:
  3787. case DRM_FORMAT_YUYV:
  3788. case DRM_FORMAT_YVYU:
  3789. case DRM_FORMAT_UYVY:
  3790. case DRM_FORMAT_VYUY:
  3791. break;
  3792. default:
  3793. DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
  3794. intel_plane->base.base.id, fb->base.id, fb->pixel_format);
  3795. return -EINVAL;
  3796. }
  3797. return 0;
  3798. }
  3799. static void skylake_scaler_disable(struct intel_crtc *crtc)
  3800. {
  3801. int i;
  3802. for (i = 0; i < crtc->num_scalers; i++)
  3803. skl_detach_scaler(crtc, i);
  3804. }
  3805. static void skylake_pfit_enable(struct intel_crtc *crtc)
  3806. {
  3807. struct drm_device *dev = crtc->base.dev;
  3808. struct drm_i915_private *dev_priv = dev->dev_private;
  3809. int pipe = crtc->pipe;
  3810. struct intel_crtc_scaler_state *scaler_state =
  3811. &crtc->config->scaler_state;
  3812. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  3813. if (crtc->config->pch_pfit.enabled) {
  3814. int id;
  3815. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  3816. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  3817. return;
  3818. }
  3819. id = scaler_state->scaler_id;
  3820. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  3821. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  3822. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  3823. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  3824. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  3825. }
  3826. }
  3827. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3828. {
  3829. struct drm_device *dev = crtc->base.dev;
  3830. struct drm_i915_private *dev_priv = dev->dev_private;
  3831. int pipe = crtc->pipe;
  3832. if (crtc->config->pch_pfit.enabled) {
  3833. /* Force use of hard-coded filter coefficients
  3834. * as some pre-programmed values are broken,
  3835. * e.g. x201.
  3836. */
  3837. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3838. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3839. PF_PIPE_SEL_IVB(pipe));
  3840. else
  3841. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3842. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  3843. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  3844. }
  3845. }
  3846. void hsw_enable_ips(struct intel_crtc *crtc)
  3847. {
  3848. struct drm_device *dev = crtc->base.dev;
  3849. struct drm_i915_private *dev_priv = dev->dev_private;
  3850. if (!crtc->config->ips_enabled)
  3851. return;
  3852. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3853. intel_wait_for_vblank(dev, crtc->pipe);
  3854. assert_plane_enabled(dev_priv, crtc->plane);
  3855. if (IS_BROADWELL(dev)) {
  3856. mutex_lock(&dev_priv->rps.hw_lock);
  3857. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3858. mutex_unlock(&dev_priv->rps.hw_lock);
  3859. /* Quoting Art Runyan: "its not safe to expect any particular
  3860. * value in IPS_CTL bit 31 after enabling IPS through the
  3861. * mailbox." Moreover, the mailbox may return a bogus state,
  3862. * so we need to just enable it and continue on.
  3863. */
  3864. } else {
  3865. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3866. /* The bit only becomes 1 in the next vblank, so this wait here
  3867. * is essentially intel_wait_for_vblank. If we don't have this
  3868. * and don't wait for vblanks until the end of crtc_enable, then
  3869. * the HW state readout code will complain that the expected
  3870. * IPS_CTL value is not the one we read. */
  3871. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3872. DRM_ERROR("Timed out waiting for IPS enable\n");
  3873. }
  3874. }
  3875. void hsw_disable_ips(struct intel_crtc *crtc)
  3876. {
  3877. struct drm_device *dev = crtc->base.dev;
  3878. struct drm_i915_private *dev_priv = dev->dev_private;
  3879. if (!crtc->config->ips_enabled)
  3880. return;
  3881. assert_plane_enabled(dev_priv, crtc->plane);
  3882. if (IS_BROADWELL(dev)) {
  3883. mutex_lock(&dev_priv->rps.hw_lock);
  3884. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3885. mutex_unlock(&dev_priv->rps.hw_lock);
  3886. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3887. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3888. DRM_ERROR("Timed out waiting for IPS disable\n");
  3889. } else {
  3890. I915_WRITE(IPS_CTL, 0);
  3891. POSTING_READ(IPS_CTL);
  3892. }
  3893. /* We need to wait for a vblank before we can disable the plane. */
  3894. intel_wait_for_vblank(dev, crtc->pipe);
  3895. }
  3896. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3897. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3898. {
  3899. struct drm_device *dev = crtc->dev;
  3900. struct drm_i915_private *dev_priv = dev->dev_private;
  3901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3902. enum pipe pipe = intel_crtc->pipe;
  3903. int i;
  3904. bool reenable_ips = false;
  3905. /* The clocks have to be on to load the palette. */
  3906. if (!crtc->state->active)
  3907. return;
  3908. if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
  3909. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
  3910. assert_dsi_pll_enabled(dev_priv);
  3911. else
  3912. assert_pll_enabled(dev_priv, pipe);
  3913. }
  3914. /* Workaround : Do not read or write the pipe palette/gamma data while
  3915. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3916. */
  3917. if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
  3918. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3919. GAMMA_MODE_MODE_SPLIT)) {
  3920. hsw_disable_ips(intel_crtc);
  3921. reenable_ips = true;
  3922. }
  3923. for (i = 0; i < 256; i++) {
  3924. u32 palreg;
  3925. if (HAS_GMCH_DISPLAY(dev))
  3926. palreg = PALETTE(pipe, i);
  3927. else
  3928. palreg = LGC_PALETTE(pipe, i);
  3929. I915_WRITE(palreg,
  3930. (intel_crtc->lut_r[i] << 16) |
  3931. (intel_crtc->lut_g[i] << 8) |
  3932. intel_crtc->lut_b[i]);
  3933. }
  3934. if (reenable_ips)
  3935. hsw_enable_ips(intel_crtc);
  3936. }
  3937. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  3938. {
  3939. if (intel_crtc->overlay) {
  3940. struct drm_device *dev = intel_crtc->base.dev;
  3941. struct drm_i915_private *dev_priv = dev->dev_private;
  3942. mutex_lock(&dev->struct_mutex);
  3943. dev_priv->mm.interruptible = false;
  3944. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3945. dev_priv->mm.interruptible = true;
  3946. mutex_unlock(&dev->struct_mutex);
  3947. }
  3948. /* Let userspace switch the overlay on again. In most cases userspace
  3949. * has to recompute where to put it anyway.
  3950. */
  3951. }
  3952. /**
  3953. * intel_post_enable_primary - Perform operations after enabling primary plane
  3954. * @crtc: the CRTC whose primary plane was just enabled
  3955. *
  3956. * Performs potentially sleeping operations that must be done after the primary
  3957. * plane is enabled, such as updating FBC and IPS. Note that this may be
  3958. * called due to an explicit primary plane update, or due to an implicit
  3959. * re-enable that is caused when a sprite plane is updated to no longer
  3960. * completely hide the primary plane.
  3961. */
  3962. static void
  3963. intel_post_enable_primary(struct drm_crtc *crtc)
  3964. {
  3965. struct drm_device *dev = crtc->dev;
  3966. struct drm_i915_private *dev_priv = dev->dev_private;
  3967. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3968. int pipe = intel_crtc->pipe;
  3969. /*
  3970. * BDW signals flip done immediately if the plane
  3971. * is disabled, even if the plane enable is already
  3972. * armed to occur at the next vblank :(
  3973. */
  3974. if (IS_BROADWELL(dev))
  3975. intel_wait_for_vblank(dev, pipe);
  3976. /*
  3977. * FIXME IPS should be fine as long as one plane is
  3978. * enabled, but in practice it seems to have problems
  3979. * when going from primary only to sprite only and vice
  3980. * versa.
  3981. */
  3982. hsw_enable_ips(intel_crtc);
  3983. /*
  3984. * Gen2 reports pipe underruns whenever all planes are disabled.
  3985. * So don't enable underrun reporting before at least some planes
  3986. * are enabled.
  3987. * FIXME: Need to fix the logic to work when we turn off all planes
  3988. * but leave the pipe running.
  3989. */
  3990. if (IS_GEN2(dev))
  3991. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  3992. /* Underruns don't always raise interrupts, so check manually. */
  3993. intel_check_cpu_fifo_underruns(dev_priv);
  3994. intel_check_pch_fifo_underruns(dev_priv);
  3995. }
  3996. /**
  3997. * intel_pre_disable_primary - Perform operations before disabling primary plane
  3998. * @crtc: the CRTC whose primary plane is to be disabled
  3999. *
  4000. * Performs potentially sleeping operations that must be done before the
  4001. * primary plane is disabled, such as updating FBC and IPS. Note that this may
  4002. * be called due to an explicit primary plane update, or due to an implicit
  4003. * disable that is caused when a sprite plane completely hides the primary
  4004. * plane.
  4005. */
  4006. static void
  4007. intel_pre_disable_primary(struct drm_crtc *crtc)
  4008. {
  4009. struct drm_device *dev = crtc->dev;
  4010. struct drm_i915_private *dev_priv = dev->dev_private;
  4011. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4012. int pipe = intel_crtc->pipe;
  4013. /*
  4014. * Gen2 reports pipe underruns whenever all planes are disabled.
  4015. * So diasble underrun reporting before all the planes get disabled.
  4016. * FIXME: Need to fix the logic to work when we turn off all planes
  4017. * but leave the pipe running.
  4018. */
  4019. if (IS_GEN2(dev))
  4020. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4021. /*
  4022. * Vblank time updates from the shadow to live plane control register
  4023. * are blocked if the memory self-refresh mode is active at that
  4024. * moment. So to make sure the plane gets truly disabled, disable
  4025. * first the self-refresh mode. The self-refresh enable bit in turn
  4026. * will be checked/applied by the HW only at the next frame start
  4027. * event which is after the vblank start event, so we need to have a
  4028. * wait-for-vblank between disabling the plane and the pipe.
  4029. */
  4030. if (HAS_GMCH_DISPLAY(dev)) {
  4031. intel_set_memory_cxsr(dev_priv, false);
  4032. dev_priv->wm.vlv.cxsr = false;
  4033. intel_wait_for_vblank(dev, pipe);
  4034. }
  4035. /*
  4036. * FIXME IPS should be fine as long as one plane is
  4037. * enabled, but in practice it seems to have problems
  4038. * when going from primary only to sprite only and vice
  4039. * versa.
  4040. */
  4041. hsw_disable_ips(intel_crtc);
  4042. }
  4043. static void intel_post_plane_update(struct intel_crtc *crtc)
  4044. {
  4045. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4046. struct drm_device *dev = crtc->base.dev;
  4047. struct drm_i915_private *dev_priv = dev->dev_private;
  4048. if (atomic->wait_vblank)
  4049. intel_wait_for_vblank(dev, crtc->pipe);
  4050. intel_frontbuffer_flip(dev, atomic->fb_bits);
  4051. if (atomic->disable_cxsr)
  4052. crtc->wm.cxsr_allowed = true;
  4053. if (crtc->atomic.update_wm_post)
  4054. intel_update_watermarks(&crtc->base);
  4055. if (atomic->update_fbc)
  4056. intel_fbc_update(dev_priv);
  4057. if (atomic->post_enable_primary)
  4058. intel_post_enable_primary(&crtc->base);
  4059. memset(atomic, 0, sizeof(*atomic));
  4060. }
  4061. static void intel_pre_plane_update(struct intel_crtc *crtc)
  4062. {
  4063. struct drm_device *dev = crtc->base.dev;
  4064. struct drm_i915_private *dev_priv = dev->dev_private;
  4065. struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
  4066. if (atomic->disable_fbc)
  4067. intel_fbc_disable_crtc(crtc);
  4068. if (crtc->atomic.disable_ips)
  4069. hsw_disable_ips(crtc);
  4070. if (atomic->pre_disable_primary)
  4071. intel_pre_disable_primary(&crtc->base);
  4072. if (atomic->disable_cxsr) {
  4073. crtc->wm.cxsr_allowed = false;
  4074. intel_set_memory_cxsr(dev_priv, false);
  4075. }
  4076. }
  4077. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4078. {
  4079. struct drm_device *dev = crtc->dev;
  4080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4081. struct drm_plane *p;
  4082. int pipe = intel_crtc->pipe;
  4083. intel_crtc_dpms_overlay_disable(intel_crtc);
  4084. drm_for_each_plane_mask(p, dev, plane_mask)
  4085. to_intel_plane(p)->disable_plane(p, crtc);
  4086. /*
  4087. * FIXME: Once we grow proper nuclear flip support out of this we need
  4088. * to compute the mask of flip planes precisely. For the time being
  4089. * consider this a flip to a NULL plane.
  4090. */
  4091. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4092. }
  4093. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  4094. {
  4095. struct drm_device *dev = crtc->dev;
  4096. struct drm_i915_private *dev_priv = dev->dev_private;
  4097. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4098. struct intel_encoder *encoder;
  4099. int pipe = intel_crtc->pipe;
  4100. if (WARN_ON(intel_crtc->active))
  4101. return;
  4102. if (intel_crtc->config->has_pch_encoder)
  4103. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4104. if (intel_crtc->config->has_pch_encoder)
  4105. intel_prepare_shared_dpll(intel_crtc);
  4106. if (intel_crtc->config->has_dp_encoder)
  4107. intel_dp_set_m_n(intel_crtc, M1_N1);
  4108. intel_set_pipe_timings(intel_crtc);
  4109. if (intel_crtc->config->has_pch_encoder) {
  4110. intel_cpu_transcoder_set_m_n(intel_crtc,
  4111. &intel_crtc->config->fdi_m_n, NULL);
  4112. }
  4113. ironlake_set_pipeconf(crtc);
  4114. intel_crtc->active = true;
  4115. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4116. for_each_encoder_on_crtc(dev, crtc, encoder)
  4117. if (encoder->pre_enable)
  4118. encoder->pre_enable(encoder);
  4119. if (intel_crtc->config->has_pch_encoder) {
  4120. /* Note: FDI PLL enabling _must_ be done before we enable the
  4121. * cpu pipes, hence this is separate from all the other fdi/pch
  4122. * enabling. */
  4123. ironlake_fdi_pll_enable(intel_crtc);
  4124. } else {
  4125. assert_fdi_tx_disabled(dev_priv, pipe);
  4126. assert_fdi_rx_disabled(dev_priv, pipe);
  4127. }
  4128. ironlake_pfit_enable(intel_crtc);
  4129. /*
  4130. * On ILK+ LUT must be loaded before the pipe is running but with
  4131. * clocks enabled
  4132. */
  4133. intel_crtc_load_lut(crtc);
  4134. intel_update_watermarks(crtc);
  4135. intel_enable_pipe(intel_crtc);
  4136. if (intel_crtc->config->has_pch_encoder)
  4137. ironlake_pch_enable(crtc);
  4138. assert_vblank_disabled(crtc);
  4139. drm_crtc_vblank_on(crtc);
  4140. for_each_encoder_on_crtc(dev, crtc, encoder)
  4141. encoder->enable(encoder);
  4142. if (HAS_PCH_CPT(dev))
  4143. cpt_verify_modeset(dev, intel_crtc->pipe);
  4144. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4145. if (intel_crtc->config->has_pch_encoder)
  4146. intel_wait_for_vblank(dev, pipe);
  4147. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4148. }
  4149. /* IPS only exists on ULT machines and is tied to pipe A. */
  4150. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4151. {
  4152. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  4153. }
  4154. static void haswell_crtc_enable(struct drm_crtc *crtc)
  4155. {
  4156. struct drm_device *dev = crtc->dev;
  4157. struct drm_i915_private *dev_priv = dev->dev_private;
  4158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4159. struct intel_encoder *encoder;
  4160. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4161. struct intel_crtc_state *pipe_config =
  4162. to_intel_crtc_state(crtc->state);
  4163. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4164. if (WARN_ON(intel_crtc->active))
  4165. return;
  4166. if (intel_crtc->config->has_pch_encoder)
  4167. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4168. false);
  4169. if (intel_crtc_to_shared_dpll(intel_crtc))
  4170. intel_enable_shared_dpll(intel_crtc);
  4171. if (intel_crtc->config->has_dp_encoder)
  4172. intel_dp_set_m_n(intel_crtc, M1_N1);
  4173. intel_set_pipe_timings(intel_crtc);
  4174. if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
  4175. I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
  4176. intel_crtc->config->pixel_multiplier - 1);
  4177. }
  4178. if (intel_crtc->config->has_pch_encoder) {
  4179. intel_cpu_transcoder_set_m_n(intel_crtc,
  4180. &intel_crtc->config->fdi_m_n, NULL);
  4181. }
  4182. haswell_set_pipeconf(crtc);
  4183. intel_set_pipe_csc(crtc);
  4184. intel_crtc->active = true;
  4185. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4186. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4187. if (encoder->pre_pll_enable)
  4188. encoder->pre_pll_enable(encoder);
  4189. if (encoder->pre_enable)
  4190. encoder->pre_enable(encoder);
  4191. }
  4192. if (intel_crtc->config->has_pch_encoder)
  4193. dev_priv->display.fdi_link_train(crtc);
  4194. if (!is_dsi)
  4195. intel_ddi_enable_pipe_clock(intel_crtc);
  4196. if (INTEL_INFO(dev)->gen >= 9)
  4197. skylake_pfit_enable(intel_crtc);
  4198. else
  4199. ironlake_pfit_enable(intel_crtc);
  4200. /*
  4201. * On ILK+ LUT must be loaded before the pipe is running but with
  4202. * clocks enabled
  4203. */
  4204. intel_crtc_load_lut(crtc);
  4205. intel_ddi_set_pipe_settings(crtc);
  4206. if (!is_dsi)
  4207. intel_ddi_enable_transcoder_func(crtc);
  4208. intel_update_watermarks(crtc);
  4209. intel_enable_pipe(intel_crtc);
  4210. if (intel_crtc->config->has_pch_encoder)
  4211. lpt_pch_enable(crtc);
  4212. if (intel_crtc->config->dp_encoder_is_mst && !is_dsi)
  4213. intel_ddi_set_vc_payload_alloc(crtc, true);
  4214. assert_vblank_disabled(crtc);
  4215. drm_crtc_vblank_on(crtc);
  4216. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4217. encoder->enable(encoder);
  4218. intel_opregion_notify_encoder(encoder, true);
  4219. }
  4220. if (intel_crtc->config->has_pch_encoder)
  4221. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4222. true);
  4223. /* If we change the relative order between pipe/planes enabling, we need
  4224. * to change the workaround. */
  4225. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4226. if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
  4227. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4228. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4229. }
  4230. }
  4231. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4232. {
  4233. struct drm_device *dev = crtc->base.dev;
  4234. struct drm_i915_private *dev_priv = dev->dev_private;
  4235. int pipe = crtc->pipe;
  4236. /* To avoid upsetting the power well on haswell only disable the pfit if
  4237. * it's in use. The hw state code will make sure we get this right. */
  4238. if (force || crtc->config->pch_pfit.enabled) {
  4239. I915_WRITE(PF_CTL(pipe), 0);
  4240. I915_WRITE(PF_WIN_POS(pipe), 0);
  4241. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4242. }
  4243. }
  4244. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  4245. {
  4246. struct drm_device *dev = crtc->dev;
  4247. struct drm_i915_private *dev_priv = dev->dev_private;
  4248. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4249. struct intel_encoder *encoder;
  4250. int pipe = intel_crtc->pipe;
  4251. u32 reg, temp;
  4252. if (intel_crtc->config->has_pch_encoder)
  4253. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4254. for_each_encoder_on_crtc(dev, crtc, encoder)
  4255. encoder->disable(encoder);
  4256. drm_crtc_vblank_off(crtc);
  4257. assert_vblank_disabled(crtc);
  4258. intel_disable_pipe(intel_crtc);
  4259. ironlake_pfit_disable(intel_crtc, false);
  4260. if (intel_crtc->config->has_pch_encoder)
  4261. ironlake_fdi_disable(crtc);
  4262. for_each_encoder_on_crtc(dev, crtc, encoder)
  4263. if (encoder->post_disable)
  4264. encoder->post_disable(encoder);
  4265. if (intel_crtc->config->has_pch_encoder) {
  4266. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4267. if (HAS_PCH_CPT(dev)) {
  4268. /* disable TRANS_DP_CTL */
  4269. reg = TRANS_DP_CTL(pipe);
  4270. temp = I915_READ(reg);
  4271. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4272. TRANS_DP_PORT_SEL_MASK);
  4273. temp |= TRANS_DP_PORT_SEL_NONE;
  4274. I915_WRITE(reg, temp);
  4275. /* disable DPLL_SEL */
  4276. temp = I915_READ(PCH_DPLL_SEL);
  4277. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4278. I915_WRITE(PCH_DPLL_SEL, temp);
  4279. }
  4280. ironlake_fdi_pll_disable(intel_crtc);
  4281. }
  4282. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4283. }
  4284. static void haswell_crtc_disable(struct drm_crtc *crtc)
  4285. {
  4286. struct drm_device *dev = crtc->dev;
  4287. struct drm_i915_private *dev_priv = dev->dev_private;
  4288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4289. struct intel_encoder *encoder;
  4290. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4291. bool is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  4292. if (intel_crtc->config->has_pch_encoder)
  4293. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4294. false);
  4295. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4296. intel_opregion_notify_encoder(encoder, false);
  4297. encoder->disable(encoder);
  4298. }
  4299. drm_crtc_vblank_off(crtc);
  4300. assert_vblank_disabled(crtc);
  4301. intel_disable_pipe(intel_crtc);
  4302. if (intel_crtc->config->dp_encoder_is_mst)
  4303. intel_ddi_set_vc_payload_alloc(crtc, false);
  4304. if (!is_dsi)
  4305. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4306. if (INTEL_INFO(dev)->gen >= 9)
  4307. skylake_scaler_disable(intel_crtc);
  4308. else
  4309. ironlake_pfit_disable(intel_crtc, false);
  4310. if (!is_dsi)
  4311. intel_ddi_disable_pipe_clock(intel_crtc);
  4312. if (intel_crtc->config->has_pch_encoder) {
  4313. lpt_disable_pch_transcoder(dev_priv);
  4314. intel_ddi_fdi_disable(crtc);
  4315. }
  4316. for_each_encoder_on_crtc(dev, crtc, encoder)
  4317. if (encoder->post_disable)
  4318. encoder->post_disable(encoder);
  4319. if (intel_crtc->config->has_pch_encoder)
  4320. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4321. true);
  4322. }
  4323. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4324. {
  4325. struct drm_device *dev = crtc->base.dev;
  4326. struct drm_i915_private *dev_priv = dev->dev_private;
  4327. struct intel_crtc_state *pipe_config = crtc->config;
  4328. if (!pipe_config->gmch_pfit.control)
  4329. return;
  4330. /*
  4331. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4332. * according to register description and PRM.
  4333. */
  4334. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4335. assert_pipe_disabled(dev_priv, crtc->pipe);
  4336. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4337. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4338. /* Border color in case we don't scale up to the full screen. Black by
  4339. * default, change to something else for debugging. */
  4340. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4341. }
  4342. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4343. {
  4344. switch (port) {
  4345. case PORT_A:
  4346. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4347. case PORT_B:
  4348. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4349. case PORT_C:
  4350. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4351. case PORT_D:
  4352. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4353. case PORT_E:
  4354. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4355. default:
  4356. WARN_ON_ONCE(1);
  4357. return POWER_DOMAIN_PORT_OTHER;
  4358. }
  4359. }
  4360. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4361. {
  4362. switch (port) {
  4363. case PORT_A:
  4364. return POWER_DOMAIN_AUX_A;
  4365. case PORT_B:
  4366. return POWER_DOMAIN_AUX_B;
  4367. case PORT_C:
  4368. return POWER_DOMAIN_AUX_C;
  4369. case PORT_D:
  4370. return POWER_DOMAIN_AUX_D;
  4371. case PORT_E:
  4372. /* FIXME: Check VBT for actual wiring of PORT E */
  4373. return POWER_DOMAIN_AUX_D;
  4374. default:
  4375. WARN_ON_ONCE(1);
  4376. return POWER_DOMAIN_AUX_A;
  4377. }
  4378. }
  4379. #define for_each_power_domain(domain, mask) \
  4380. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  4381. if ((1 << (domain)) & (mask))
  4382. enum intel_display_power_domain
  4383. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4384. {
  4385. struct drm_device *dev = intel_encoder->base.dev;
  4386. struct intel_digital_port *intel_dig_port;
  4387. switch (intel_encoder->type) {
  4388. case INTEL_OUTPUT_UNKNOWN:
  4389. /* Only DDI platforms should ever use this output type */
  4390. WARN_ON_ONCE(!HAS_DDI(dev));
  4391. case INTEL_OUTPUT_DISPLAYPORT:
  4392. case INTEL_OUTPUT_HDMI:
  4393. case INTEL_OUTPUT_EDP:
  4394. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4395. return port_to_power_domain(intel_dig_port->port);
  4396. case INTEL_OUTPUT_DP_MST:
  4397. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4398. return port_to_power_domain(intel_dig_port->port);
  4399. case INTEL_OUTPUT_ANALOG:
  4400. return POWER_DOMAIN_PORT_CRT;
  4401. case INTEL_OUTPUT_DSI:
  4402. return POWER_DOMAIN_PORT_DSI;
  4403. default:
  4404. return POWER_DOMAIN_PORT_OTHER;
  4405. }
  4406. }
  4407. enum intel_display_power_domain
  4408. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4409. {
  4410. struct drm_device *dev = intel_encoder->base.dev;
  4411. struct intel_digital_port *intel_dig_port;
  4412. switch (intel_encoder->type) {
  4413. case INTEL_OUTPUT_UNKNOWN:
  4414. /* Only DDI platforms should ever use this output type */
  4415. WARN_ON_ONCE(!HAS_DDI(dev));
  4416. case INTEL_OUTPUT_DISPLAYPORT:
  4417. case INTEL_OUTPUT_EDP:
  4418. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4419. return port_to_aux_power_domain(intel_dig_port->port);
  4420. case INTEL_OUTPUT_DP_MST:
  4421. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4422. return port_to_aux_power_domain(intel_dig_port->port);
  4423. default:
  4424. WARN_ON_ONCE(1);
  4425. return POWER_DOMAIN_AUX_A;
  4426. }
  4427. }
  4428. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  4429. {
  4430. struct drm_device *dev = crtc->dev;
  4431. struct intel_encoder *intel_encoder;
  4432. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4433. enum pipe pipe = intel_crtc->pipe;
  4434. unsigned long mask;
  4435. enum transcoder transcoder = intel_crtc->config->cpu_transcoder;
  4436. if (!crtc->state->active)
  4437. return 0;
  4438. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4439. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4440. if (intel_crtc->config->pch_pfit.enabled ||
  4441. intel_crtc->config->pch_pfit.force_thru)
  4442. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4443. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4444. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4445. return mask;
  4446. }
  4447. static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
  4448. {
  4449. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4450. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4451. enum intel_display_power_domain domain;
  4452. unsigned long domains, new_domains, old_domains;
  4453. old_domains = intel_crtc->enabled_power_domains;
  4454. intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
  4455. domains = new_domains & ~old_domains;
  4456. for_each_power_domain(domain, domains)
  4457. intel_display_power_get(dev_priv, domain);
  4458. return old_domains & ~new_domains;
  4459. }
  4460. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4461. unsigned long domains)
  4462. {
  4463. enum intel_display_power_domain domain;
  4464. for_each_power_domain(domain, domains)
  4465. intel_display_power_put(dev_priv, domain);
  4466. }
  4467. static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
  4468. {
  4469. struct drm_device *dev = state->dev;
  4470. struct drm_i915_private *dev_priv = dev->dev_private;
  4471. unsigned long put_domains[I915_MAX_PIPES] = {};
  4472. struct drm_crtc_state *crtc_state;
  4473. struct drm_crtc *crtc;
  4474. int i;
  4475. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  4476. if (needs_modeset(crtc->state))
  4477. put_domains[to_intel_crtc(crtc)->pipe] =
  4478. modeset_get_crtc_power_domains(crtc);
  4479. }
  4480. if (dev_priv->display.modeset_commit_cdclk) {
  4481. unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
  4482. if (cdclk != dev_priv->cdclk_freq &&
  4483. !WARN_ON(!state->allow_modeset))
  4484. dev_priv->display.modeset_commit_cdclk(state);
  4485. }
  4486. for (i = 0; i < I915_MAX_PIPES; i++)
  4487. if (put_domains[i])
  4488. modeset_put_power_domains(dev_priv, put_domains[i]);
  4489. }
  4490. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4491. {
  4492. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4493. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4494. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4495. return max_cdclk_freq;
  4496. else if (IS_CHERRYVIEW(dev_priv))
  4497. return max_cdclk_freq*95/100;
  4498. else if (INTEL_INFO(dev_priv)->gen < 4)
  4499. return 2*max_cdclk_freq*90/100;
  4500. else
  4501. return max_cdclk_freq*90/100;
  4502. }
  4503. static void intel_update_max_cdclk(struct drm_device *dev)
  4504. {
  4505. struct drm_i915_private *dev_priv = dev->dev_private;
  4506. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  4507. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4508. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4509. dev_priv->max_cdclk_freq = 675000;
  4510. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4511. dev_priv->max_cdclk_freq = 540000;
  4512. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4513. dev_priv->max_cdclk_freq = 450000;
  4514. else
  4515. dev_priv->max_cdclk_freq = 337500;
  4516. } else if (IS_BROADWELL(dev)) {
  4517. /*
  4518. * FIXME with extra cooling we can allow
  4519. * 540 MHz for ULX and 675 Mhz for ULT.
  4520. * How can we know if extra cooling is
  4521. * available? PCI ID, VTB, something else?
  4522. */
  4523. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4524. dev_priv->max_cdclk_freq = 450000;
  4525. else if (IS_BDW_ULX(dev))
  4526. dev_priv->max_cdclk_freq = 450000;
  4527. else if (IS_BDW_ULT(dev))
  4528. dev_priv->max_cdclk_freq = 540000;
  4529. else
  4530. dev_priv->max_cdclk_freq = 675000;
  4531. } else if (IS_CHERRYVIEW(dev)) {
  4532. dev_priv->max_cdclk_freq = 320000;
  4533. } else if (IS_VALLEYVIEW(dev)) {
  4534. dev_priv->max_cdclk_freq = 400000;
  4535. } else {
  4536. /* otherwise assume cdclk is fixed */
  4537. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4538. }
  4539. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4540. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4541. dev_priv->max_cdclk_freq);
  4542. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4543. dev_priv->max_dotclk_freq);
  4544. }
  4545. static void intel_update_cdclk(struct drm_device *dev)
  4546. {
  4547. struct drm_i915_private *dev_priv = dev->dev_private;
  4548. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  4549. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4550. dev_priv->cdclk_freq);
  4551. /*
  4552. * Program the gmbus_freq based on the cdclk frequency.
  4553. * BSpec erroneously claims we should aim for 4MHz, but
  4554. * in fact 1MHz is the correct frequency.
  4555. */
  4556. if (IS_VALLEYVIEW(dev)) {
  4557. /*
  4558. * Program the gmbus_freq based on the cdclk frequency.
  4559. * BSpec erroneously claims we should aim for 4MHz, but
  4560. * in fact 1MHz is the correct frequency.
  4561. */
  4562. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4563. }
  4564. if (dev_priv->max_cdclk_freq == 0)
  4565. intel_update_max_cdclk(dev);
  4566. }
  4567. static void broxton_set_cdclk(struct drm_device *dev, int frequency)
  4568. {
  4569. struct drm_i915_private *dev_priv = dev->dev_private;
  4570. uint32_t divider;
  4571. uint32_t ratio;
  4572. uint32_t current_freq;
  4573. int ret;
  4574. /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
  4575. switch (frequency) {
  4576. case 144000:
  4577. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  4578. ratio = BXT_DE_PLL_RATIO(60);
  4579. break;
  4580. case 288000:
  4581. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  4582. ratio = BXT_DE_PLL_RATIO(60);
  4583. break;
  4584. case 384000:
  4585. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  4586. ratio = BXT_DE_PLL_RATIO(60);
  4587. break;
  4588. case 576000:
  4589. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4590. ratio = BXT_DE_PLL_RATIO(60);
  4591. break;
  4592. case 624000:
  4593. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  4594. ratio = BXT_DE_PLL_RATIO(65);
  4595. break;
  4596. case 19200:
  4597. /*
  4598. * Bypass frequency with DE PLL disabled. Init ratio, divider
  4599. * to suppress GCC warning.
  4600. */
  4601. ratio = 0;
  4602. divider = 0;
  4603. break;
  4604. default:
  4605. DRM_ERROR("unsupported CDCLK freq %d", frequency);
  4606. return;
  4607. }
  4608. mutex_lock(&dev_priv->rps.hw_lock);
  4609. /* Inform power controller of upcoming frequency change */
  4610. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4611. 0x80000000);
  4612. mutex_unlock(&dev_priv->rps.hw_lock);
  4613. if (ret) {
  4614. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  4615. ret, frequency);
  4616. return;
  4617. }
  4618. current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
  4619. /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
  4620. current_freq = current_freq * 500 + 1000;
  4621. /*
  4622. * DE PLL has to be disabled when
  4623. * - setting to 19.2MHz (bypass, PLL isn't used)
  4624. * - before setting to 624MHz (PLL needs toggling)
  4625. * - before setting to any frequency from 624MHz (PLL needs toggling)
  4626. */
  4627. if (frequency == 19200 || frequency == 624000 ||
  4628. current_freq == 624000) {
  4629. I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
  4630. /* Timeout 200us */
  4631. if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
  4632. 1))
  4633. DRM_ERROR("timout waiting for DE PLL unlock\n");
  4634. }
  4635. if (frequency != 19200) {
  4636. uint32_t val;
  4637. val = I915_READ(BXT_DE_PLL_CTL);
  4638. val &= ~BXT_DE_PLL_RATIO_MASK;
  4639. val |= ratio;
  4640. I915_WRITE(BXT_DE_PLL_CTL, val);
  4641. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  4642. /* Timeout 200us */
  4643. if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
  4644. DRM_ERROR("timeout waiting for DE PLL lock\n");
  4645. val = I915_READ(CDCLK_CTL);
  4646. val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
  4647. val |= divider;
  4648. /*
  4649. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  4650. * enable otherwise.
  4651. */
  4652. val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4653. if (frequency >= 500000)
  4654. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  4655. val &= ~CDCLK_FREQ_DECIMAL_MASK;
  4656. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4657. val |= (frequency - 1000) / 500;
  4658. I915_WRITE(CDCLK_CTL, val);
  4659. }
  4660. mutex_lock(&dev_priv->rps.hw_lock);
  4661. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  4662. DIV_ROUND_UP(frequency, 25000));
  4663. mutex_unlock(&dev_priv->rps.hw_lock);
  4664. if (ret) {
  4665. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  4666. ret, frequency);
  4667. return;
  4668. }
  4669. intel_update_cdclk(dev);
  4670. }
  4671. void broxton_init_cdclk(struct drm_device *dev)
  4672. {
  4673. struct drm_i915_private *dev_priv = dev->dev_private;
  4674. uint32_t val;
  4675. /*
  4676. * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
  4677. * or else the reset will hang because there is no PCH to respond.
  4678. * Move the handshake programming to initialization sequence.
  4679. * Previously was left up to BIOS.
  4680. */
  4681. val = I915_READ(HSW_NDE_RSTWRN_OPT);
  4682. val &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4683. I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
  4684. /* Enable PG1 for cdclk */
  4685. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  4686. /* check if cd clock is enabled */
  4687. if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
  4688. DRM_DEBUG_KMS("Display already initialized\n");
  4689. return;
  4690. }
  4691. /*
  4692. * FIXME:
  4693. * - The initial CDCLK needs to be read from VBT.
  4694. * Need to make this change after VBT has changes for BXT.
  4695. * - check if setting the max (or any) cdclk freq is really necessary
  4696. * here, it belongs to modeset time
  4697. */
  4698. broxton_set_cdclk(dev, 624000);
  4699. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4700. POSTING_READ(DBUF_CTL);
  4701. udelay(10);
  4702. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4703. DRM_ERROR("DBuf power enable timeout!\n");
  4704. }
  4705. void broxton_uninit_cdclk(struct drm_device *dev)
  4706. {
  4707. struct drm_i915_private *dev_priv = dev->dev_private;
  4708. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4709. POSTING_READ(DBUF_CTL);
  4710. udelay(10);
  4711. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4712. DRM_ERROR("DBuf power disable timeout!\n");
  4713. /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
  4714. broxton_set_cdclk(dev, 19200);
  4715. intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
  4716. }
  4717. static const struct skl_cdclk_entry {
  4718. unsigned int freq;
  4719. unsigned int vco;
  4720. } skl_cdclk_frequencies[] = {
  4721. { .freq = 308570, .vco = 8640 },
  4722. { .freq = 337500, .vco = 8100 },
  4723. { .freq = 432000, .vco = 8640 },
  4724. { .freq = 450000, .vco = 8100 },
  4725. { .freq = 540000, .vco = 8100 },
  4726. { .freq = 617140, .vco = 8640 },
  4727. { .freq = 675000, .vco = 8100 },
  4728. };
  4729. static unsigned int skl_cdclk_decimal(unsigned int freq)
  4730. {
  4731. return (freq - 1000) / 500;
  4732. }
  4733. static unsigned int skl_cdclk_get_vco(unsigned int freq)
  4734. {
  4735. unsigned int i;
  4736. for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
  4737. const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
  4738. if (e->freq == freq)
  4739. return e->vco;
  4740. }
  4741. return 8100;
  4742. }
  4743. static void
  4744. skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
  4745. {
  4746. unsigned int min_freq;
  4747. u32 val;
  4748. /* select the minimum CDCLK before enabling DPLL 0 */
  4749. val = I915_READ(CDCLK_CTL);
  4750. val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
  4751. val |= CDCLK_FREQ_337_308;
  4752. if (required_vco == 8640)
  4753. min_freq = 308570;
  4754. else
  4755. min_freq = 337500;
  4756. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
  4757. I915_WRITE(CDCLK_CTL, val);
  4758. POSTING_READ(CDCLK_CTL);
  4759. /*
  4760. * We always enable DPLL0 with the lowest link rate possible, but still
  4761. * taking into account the VCO required to operate the eDP panel at the
  4762. * desired frequency. The usual DP link rates operate with a VCO of
  4763. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  4764. * The modeset code is responsible for the selection of the exact link
  4765. * rate later on, with the constraint of choosing a frequency that
  4766. * works with required_vco.
  4767. */
  4768. val = I915_READ(DPLL_CTRL1);
  4769. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  4770. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  4771. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  4772. if (required_vco == 8640)
  4773. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  4774. SKL_DPLL0);
  4775. else
  4776. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  4777. SKL_DPLL0);
  4778. I915_WRITE(DPLL_CTRL1, val);
  4779. POSTING_READ(DPLL_CTRL1);
  4780. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  4781. if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
  4782. DRM_ERROR("DPLL0 not locked\n");
  4783. }
  4784. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  4785. {
  4786. int ret;
  4787. u32 val;
  4788. /* inform PCU we want to change CDCLK */
  4789. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  4790. mutex_lock(&dev_priv->rps.hw_lock);
  4791. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  4792. mutex_unlock(&dev_priv->rps.hw_lock);
  4793. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  4794. }
  4795. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  4796. {
  4797. unsigned int i;
  4798. for (i = 0; i < 15; i++) {
  4799. if (skl_cdclk_pcu_ready(dev_priv))
  4800. return true;
  4801. udelay(10);
  4802. }
  4803. return false;
  4804. }
  4805. static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
  4806. {
  4807. struct drm_device *dev = dev_priv->dev;
  4808. u32 freq_select, pcu_ack;
  4809. DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
  4810. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  4811. DRM_ERROR("failed to inform PCU about cdclk change\n");
  4812. return;
  4813. }
  4814. /* set CDCLK_CTL */
  4815. switch(freq) {
  4816. case 450000:
  4817. case 432000:
  4818. freq_select = CDCLK_FREQ_450_432;
  4819. pcu_ack = 1;
  4820. break;
  4821. case 540000:
  4822. freq_select = CDCLK_FREQ_540;
  4823. pcu_ack = 2;
  4824. break;
  4825. case 308570:
  4826. case 337500:
  4827. default:
  4828. freq_select = CDCLK_FREQ_337_308;
  4829. pcu_ack = 0;
  4830. break;
  4831. case 617140:
  4832. case 675000:
  4833. freq_select = CDCLK_FREQ_675_617;
  4834. pcu_ack = 3;
  4835. break;
  4836. }
  4837. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
  4838. POSTING_READ(CDCLK_CTL);
  4839. /* inform PCU of the change */
  4840. mutex_lock(&dev_priv->rps.hw_lock);
  4841. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  4842. mutex_unlock(&dev_priv->rps.hw_lock);
  4843. intel_update_cdclk(dev);
  4844. }
  4845. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  4846. {
  4847. /* disable DBUF power */
  4848. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
  4849. POSTING_READ(DBUF_CTL);
  4850. udelay(10);
  4851. if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
  4852. DRM_ERROR("DBuf power disable timeout\n");
  4853. /* disable DPLL0 */
  4854. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  4855. if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
  4856. DRM_ERROR("Couldn't disable DPLL0\n");
  4857. }
  4858. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  4859. {
  4860. unsigned int required_vco;
  4861. /* DPLL0 not enabled (happens on early BIOS versions) */
  4862. if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE)) {
  4863. /* enable DPLL0 */
  4864. required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
  4865. skl_dpll0_enable(dev_priv, required_vco);
  4866. }
  4867. /* set CDCLK to the frequency the BIOS chose */
  4868. skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
  4869. /* enable DBUF power */
  4870. I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
  4871. POSTING_READ(DBUF_CTL);
  4872. udelay(10);
  4873. if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
  4874. DRM_ERROR("DBuf power enable timeout\n");
  4875. }
  4876. int skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  4877. {
  4878. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  4879. uint32_t cdctl = I915_READ(CDCLK_CTL);
  4880. int freq = dev_priv->skl_boot_cdclk;
  4881. /*
  4882. * check if the pre-os intialized the display
  4883. * There is SWF18 scratchpad register defined which is set by the
  4884. * pre-os which can be used by the OS drivers to check the status
  4885. */
  4886. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  4887. goto sanitize;
  4888. /* Is PLL enabled and locked ? */
  4889. if (!((lcpll1 & LCPLL_PLL_ENABLE) && (lcpll1 & LCPLL_PLL_LOCK)))
  4890. goto sanitize;
  4891. /* DPLL okay; verify the cdclock
  4892. *
  4893. * Noticed in some instances that the freq selection is correct but
  4894. * decimal part is programmed wrong from BIOS where pre-os does not
  4895. * enable display. Verify the same as well.
  4896. */
  4897. if (cdctl == ((cdctl & CDCLK_FREQ_SEL_MASK) | skl_cdclk_decimal(freq)))
  4898. /* All well; nothing to sanitize */
  4899. return false;
  4900. sanitize:
  4901. /*
  4902. * As of now initialize with max cdclk till
  4903. * we get dynamic cdclk support
  4904. * */
  4905. dev_priv->skl_boot_cdclk = dev_priv->max_cdclk_freq;
  4906. skl_init_cdclk(dev_priv);
  4907. /* we did have to sanitize */
  4908. return true;
  4909. }
  4910. /* Adjust CDclk dividers to allow high res or save power if possible */
  4911. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  4912. {
  4913. struct drm_i915_private *dev_priv = dev->dev_private;
  4914. u32 val, cmd;
  4915. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4916. != dev_priv->cdclk_freq);
  4917. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  4918. cmd = 2;
  4919. else if (cdclk == 266667)
  4920. cmd = 1;
  4921. else
  4922. cmd = 0;
  4923. mutex_lock(&dev_priv->rps.hw_lock);
  4924. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4925. val &= ~DSPFREQGUAR_MASK;
  4926. val |= (cmd << DSPFREQGUAR_SHIFT);
  4927. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4928. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4929. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  4930. 50)) {
  4931. DRM_ERROR("timed out waiting for CDclk change\n");
  4932. }
  4933. mutex_unlock(&dev_priv->rps.hw_lock);
  4934. mutex_lock(&dev_priv->sb_lock);
  4935. if (cdclk == 400000) {
  4936. u32 divider;
  4937. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4938. /* adjust cdclk divider */
  4939. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  4940. val &= ~CCK_FREQUENCY_VALUES;
  4941. val |= divider;
  4942. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  4943. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  4944. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  4945. 50))
  4946. DRM_ERROR("timed out waiting for CDclk change\n");
  4947. }
  4948. /* adjust self-refresh exit latency value */
  4949. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  4950. val &= ~0x7f;
  4951. /*
  4952. * For high bandwidth configs, we set a higher latency in the bunit
  4953. * so that the core display fetch happens in time to avoid underruns.
  4954. */
  4955. if (cdclk == 400000)
  4956. val |= 4500 / 250; /* 4.5 usec */
  4957. else
  4958. val |= 3000 / 250; /* 3.0 usec */
  4959. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  4960. mutex_unlock(&dev_priv->sb_lock);
  4961. intel_update_cdclk(dev);
  4962. }
  4963. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  4964. {
  4965. struct drm_i915_private *dev_priv = dev->dev_private;
  4966. u32 val, cmd;
  4967. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  4968. != dev_priv->cdclk_freq);
  4969. switch (cdclk) {
  4970. case 333333:
  4971. case 320000:
  4972. case 266667:
  4973. case 200000:
  4974. break;
  4975. default:
  4976. MISSING_CASE(cdclk);
  4977. return;
  4978. }
  4979. /*
  4980. * Specs are full of misinformation, but testing on actual
  4981. * hardware has shown that we just need to write the desired
  4982. * CCK divider into the Punit register.
  4983. */
  4984. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  4985. mutex_lock(&dev_priv->rps.hw_lock);
  4986. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  4987. val &= ~DSPFREQGUAR_MASK_CHV;
  4988. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  4989. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  4990. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  4991. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  4992. 50)) {
  4993. DRM_ERROR("timed out waiting for CDclk change\n");
  4994. }
  4995. mutex_unlock(&dev_priv->rps.hw_lock);
  4996. intel_update_cdclk(dev);
  4997. }
  4998. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  4999. int max_pixclk)
  5000. {
  5001. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5002. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5003. /*
  5004. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5005. * 200MHz
  5006. * 267MHz
  5007. * 320/333MHz (depends on HPLL freq)
  5008. * 400MHz (VLV only)
  5009. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5010. * of the lower bin and adjust if needed.
  5011. *
  5012. * We seem to get an unstable or solid color picture at 200MHz.
  5013. * Not sure what's wrong. For now use 200MHz only when all pipes
  5014. * are off.
  5015. */
  5016. if (!IS_CHERRYVIEW(dev_priv) &&
  5017. max_pixclk > freq_320*limit/100)
  5018. return 400000;
  5019. else if (max_pixclk > 266667*limit/100)
  5020. return freq_320;
  5021. else if (max_pixclk > 0)
  5022. return 266667;
  5023. else
  5024. return 200000;
  5025. }
  5026. static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
  5027. int max_pixclk)
  5028. {
  5029. /*
  5030. * FIXME:
  5031. * - remove the guardband, it's not needed on BXT
  5032. * - set 19.2MHz bypass frequency if there are no active pipes
  5033. */
  5034. if (max_pixclk > 576000*9/10)
  5035. return 624000;
  5036. else if (max_pixclk > 384000*9/10)
  5037. return 576000;
  5038. else if (max_pixclk > 288000*9/10)
  5039. return 384000;
  5040. else if (max_pixclk > 144000*9/10)
  5041. return 288000;
  5042. else
  5043. return 144000;
  5044. }
  5045. /* Compute the max pixel clock for new configuration. Uses atomic state if
  5046. * that's non-NULL, look at current state otherwise. */
  5047. static int intel_mode_max_pixclk(struct drm_device *dev,
  5048. struct drm_atomic_state *state)
  5049. {
  5050. struct intel_crtc *intel_crtc;
  5051. struct intel_crtc_state *crtc_state;
  5052. int max_pixclk = 0;
  5053. for_each_intel_crtc(dev, intel_crtc) {
  5054. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5055. if (IS_ERR(crtc_state))
  5056. return PTR_ERR(crtc_state);
  5057. if (!crtc_state->base.enable)
  5058. continue;
  5059. max_pixclk = max(max_pixclk,
  5060. crtc_state->base.adjusted_mode.crtc_clock);
  5061. }
  5062. return max_pixclk;
  5063. }
  5064. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5065. {
  5066. struct drm_device *dev = state->dev;
  5067. struct drm_i915_private *dev_priv = dev->dev_private;
  5068. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5069. if (max_pixclk < 0)
  5070. return max_pixclk;
  5071. to_intel_atomic_state(state)->cdclk =
  5072. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5073. return 0;
  5074. }
  5075. static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
  5076. {
  5077. struct drm_device *dev = state->dev;
  5078. struct drm_i915_private *dev_priv = dev->dev_private;
  5079. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5080. if (max_pixclk < 0)
  5081. return max_pixclk;
  5082. to_intel_atomic_state(state)->cdclk =
  5083. broxton_calc_cdclk(dev_priv, max_pixclk);
  5084. return 0;
  5085. }
  5086. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5087. {
  5088. unsigned int credits, default_credits;
  5089. if (IS_CHERRYVIEW(dev_priv))
  5090. default_credits = PFI_CREDIT(12);
  5091. else
  5092. default_credits = PFI_CREDIT(8);
  5093. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5094. /* CHV suggested value is 31 or 63 */
  5095. if (IS_CHERRYVIEW(dev_priv))
  5096. credits = PFI_CREDIT_63;
  5097. else
  5098. credits = PFI_CREDIT(15);
  5099. } else {
  5100. credits = default_credits;
  5101. }
  5102. /*
  5103. * WA - write default credits before re-programming
  5104. * FIXME: should we also set the resend bit here?
  5105. */
  5106. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5107. default_credits);
  5108. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5109. credits | PFI_CREDIT_RESEND);
  5110. /*
  5111. * FIXME is this guaranteed to clear
  5112. * immediately or should we poll for it?
  5113. */
  5114. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5115. }
  5116. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5117. {
  5118. struct drm_device *dev = old_state->dev;
  5119. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  5120. struct drm_i915_private *dev_priv = dev->dev_private;
  5121. /*
  5122. * FIXME: We can end up here with all power domains off, yet
  5123. * with a CDCLK frequency other than the minimum. To account
  5124. * for this take the PIPE-A power domain, which covers the HW
  5125. * blocks needed for the following programming. This can be
  5126. * removed once it's guaranteed that we get here either with
  5127. * the minimum CDCLK set, or the required power domains
  5128. * enabled.
  5129. */
  5130. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5131. if (IS_CHERRYVIEW(dev))
  5132. cherryview_set_cdclk(dev, req_cdclk);
  5133. else
  5134. valleyview_set_cdclk(dev, req_cdclk);
  5135. vlv_program_pfi_credits(dev_priv);
  5136. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5137. }
  5138. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  5139. {
  5140. struct drm_device *dev = crtc->dev;
  5141. struct drm_i915_private *dev_priv = to_i915(dev);
  5142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5143. struct intel_encoder *encoder;
  5144. int pipe = intel_crtc->pipe;
  5145. bool is_dsi;
  5146. if (WARN_ON(intel_crtc->active))
  5147. return;
  5148. is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
  5149. if (intel_crtc->config->has_dp_encoder)
  5150. intel_dp_set_m_n(intel_crtc, M1_N1);
  5151. intel_set_pipe_timings(intel_crtc);
  5152. if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
  5153. struct drm_i915_private *dev_priv = dev->dev_private;
  5154. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5155. I915_WRITE(CHV_CANVAS(pipe), 0);
  5156. }
  5157. i9xx_set_pipeconf(intel_crtc);
  5158. intel_crtc->active = true;
  5159. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5160. for_each_encoder_on_crtc(dev, crtc, encoder)
  5161. if (encoder->pre_pll_enable)
  5162. encoder->pre_pll_enable(encoder);
  5163. if (!is_dsi) {
  5164. if (IS_CHERRYVIEW(dev)) {
  5165. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5166. chv_enable_pll(intel_crtc, intel_crtc->config);
  5167. } else {
  5168. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5169. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5170. }
  5171. }
  5172. for_each_encoder_on_crtc(dev, crtc, encoder)
  5173. if (encoder->pre_enable)
  5174. encoder->pre_enable(encoder);
  5175. i9xx_pfit_enable(intel_crtc);
  5176. intel_crtc_load_lut(crtc);
  5177. intel_enable_pipe(intel_crtc);
  5178. assert_vblank_disabled(crtc);
  5179. drm_crtc_vblank_on(crtc);
  5180. for_each_encoder_on_crtc(dev, crtc, encoder)
  5181. encoder->enable(encoder);
  5182. }
  5183. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5184. {
  5185. struct drm_device *dev = crtc->base.dev;
  5186. struct drm_i915_private *dev_priv = dev->dev_private;
  5187. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5188. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5189. }
  5190. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  5191. {
  5192. struct drm_device *dev = crtc->dev;
  5193. struct drm_i915_private *dev_priv = to_i915(dev);
  5194. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5195. struct intel_encoder *encoder;
  5196. int pipe = intel_crtc->pipe;
  5197. if (WARN_ON(intel_crtc->active))
  5198. return;
  5199. i9xx_set_pll_dividers(intel_crtc);
  5200. if (intel_crtc->config->has_dp_encoder)
  5201. intel_dp_set_m_n(intel_crtc, M1_N1);
  5202. intel_set_pipe_timings(intel_crtc);
  5203. i9xx_set_pipeconf(intel_crtc);
  5204. intel_crtc->active = true;
  5205. if (!IS_GEN2(dev))
  5206. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5207. for_each_encoder_on_crtc(dev, crtc, encoder)
  5208. if (encoder->pre_enable)
  5209. encoder->pre_enable(encoder);
  5210. i9xx_enable_pll(intel_crtc);
  5211. i9xx_pfit_enable(intel_crtc);
  5212. intel_crtc_load_lut(crtc);
  5213. intel_update_watermarks(crtc);
  5214. intel_enable_pipe(intel_crtc);
  5215. assert_vblank_disabled(crtc);
  5216. drm_crtc_vblank_on(crtc);
  5217. for_each_encoder_on_crtc(dev, crtc, encoder)
  5218. encoder->enable(encoder);
  5219. }
  5220. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5221. {
  5222. struct drm_device *dev = crtc->base.dev;
  5223. struct drm_i915_private *dev_priv = dev->dev_private;
  5224. if (!crtc->config->gmch_pfit.control)
  5225. return;
  5226. assert_pipe_disabled(dev_priv, crtc->pipe);
  5227. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5228. I915_READ(PFIT_CONTROL));
  5229. I915_WRITE(PFIT_CONTROL, 0);
  5230. }
  5231. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  5232. {
  5233. struct drm_device *dev = crtc->dev;
  5234. struct drm_i915_private *dev_priv = dev->dev_private;
  5235. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5236. struct intel_encoder *encoder;
  5237. int pipe = intel_crtc->pipe;
  5238. /*
  5239. * On gen2 planes are double buffered but the pipe isn't, so we must
  5240. * wait for planes to fully turn off before disabling the pipe.
  5241. * We also need to wait on all gmch platforms because of the
  5242. * self-refresh mode constraint explained above.
  5243. */
  5244. intel_wait_for_vblank(dev, pipe);
  5245. for_each_encoder_on_crtc(dev, crtc, encoder)
  5246. encoder->disable(encoder);
  5247. drm_crtc_vblank_off(crtc);
  5248. assert_vblank_disabled(crtc);
  5249. intel_disable_pipe(intel_crtc);
  5250. i9xx_pfit_disable(intel_crtc);
  5251. for_each_encoder_on_crtc(dev, crtc, encoder)
  5252. if (encoder->post_disable)
  5253. encoder->post_disable(encoder);
  5254. if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
  5255. if (IS_CHERRYVIEW(dev))
  5256. chv_disable_pll(dev_priv, pipe);
  5257. else if (IS_VALLEYVIEW(dev))
  5258. vlv_disable_pll(dev_priv, pipe);
  5259. else
  5260. i9xx_disable_pll(intel_crtc);
  5261. }
  5262. for_each_encoder_on_crtc(dev, crtc, encoder)
  5263. if (encoder->post_pll_disable)
  5264. encoder->post_pll_disable(encoder);
  5265. if (!IS_GEN2(dev))
  5266. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5267. }
  5268. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5269. {
  5270. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5271. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5272. enum intel_display_power_domain domain;
  5273. unsigned long domains;
  5274. if (!intel_crtc->active)
  5275. return;
  5276. if (to_intel_plane_state(crtc->primary->state)->visible) {
  5277. WARN_ON(intel_crtc->unpin_work);
  5278. intel_pre_disable_primary(crtc);
  5279. }
  5280. intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
  5281. dev_priv->display.crtc_disable(crtc);
  5282. intel_crtc->active = false;
  5283. intel_update_watermarks(crtc);
  5284. intel_disable_shared_dpll(intel_crtc);
  5285. domains = intel_crtc->enabled_power_domains;
  5286. for_each_power_domain(domain, domains)
  5287. intel_display_power_put(dev_priv, domain);
  5288. intel_crtc->enabled_power_domains = 0;
  5289. }
  5290. /*
  5291. * turn all crtc's off, but do not adjust state
  5292. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5293. */
  5294. int intel_display_suspend(struct drm_device *dev)
  5295. {
  5296. struct drm_mode_config *config = &dev->mode_config;
  5297. struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  5298. struct drm_atomic_state *state;
  5299. struct drm_crtc *crtc;
  5300. unsigned crtc_mask = 0;
  5301. int ret = 0;
  5302. if (WARN_ON(!ctx))
  5303. return 0;
  5304. lockdep_assert_held(&ctx->ww_ctx);
  5305. state = drm_atomic_state_alloc(dev);
  5306. if (WARN_ON(!state))
  5307. return -ENOMEM;
  5308. state->acquire_ctx = ctx;
  5309. state->allow_modeset = true;
  5310. for_each_crtc(dev, crtc) {
  5311. struct drm_crtc_state *crtc_state =
  5312. drm_atomic_get_crtc_state(state, crtc);
  5313. ret = PTR_ERR_OR_ZERO(crtc_state);
  5314. if (ret)
  5315. goto free;
  5316. if (!crtc_state->active)
  5317. continue;
  5318. crtc_state->active = false;
  5319. crtc_mask |= 1 << drm_crtc_index(crtc);
  5320. }
  5321. if (crtc_mask) {
  5322. ret = drm_atomic_commit(state);
  5323. if (!ret) {
  5324. for_each_crtc(dev, crtc)
  5325. if (crtc_mask & (1 << drm_crtc_index(crtc)))
  5326. crtc->state->active = true;
  5327. return ret;
  5328. }
  5329. }
  5330. free:
  5331. if (ret)
  5332. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5333. drm_atomic_state_free(state);
  5334. return ret;
  5335. }
  5336. void intel_encoder_destroy(struct drm_encoder *encoder)
  5337. {
  5338. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5339. drm_encoder_cleanup(encoder);
  5340. kfree(intel_encoder);
  5341. }
  5342. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5343. * internal consistency). */
  5344. static void intel_connector_check_state(struct intel_connector *connector)
  5345. {
  5346. struct drm_crtc *crtc = connector->base.state->crtc;
  5347. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5348. connector->base.base.id,
  5349. connector->base.name);
  5350. if (connector->get_hw_state(connector)) {
  5351. struct intel_encoder *encoder = connector->encoder;
  5352. struct drm_connector_state *conn_state = connector->base.state;
  5353. I915_STATE_WARN(!crtc,
  5354. "connector enabled without attached crtc\n");
  5355. if (!crtc)
  5356. return;
  5357. I915_STATE_WARN(!crtc->state->active,
  5358. "connector is active, but attached crtc isn't\n");
  5359. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5360. return;
  5361. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5362. "atomic encoder doesn't match attached encoder\n");
  5363. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5364. "attached encoder crtc differs from connector crtc\n");
  5365. } else {
  5366. I915_STATE_WARN(crtc && crtc->state->active,
  5367. "attached crtc is active, but connector isn't\n");
  5368. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5369. "best encoder set without crtc!\n");
  5370. }
  5371. }
  5372. int intel_connector_init(struct intel_connector *connector)
  5373. {
  5374. struct drm_connector_state *connector_state;
  5375. connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
  5376. if (!connector_state)
  5377. return -ENOMEM;
  5378. connector->base.state = connector_state;
  5379. return 0;
  5380. }
  5381. struct intel_connector *intel_connector_alloc(void)
  5382. {
  5383. struct intel_connector *connector;
  5384. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5385. if (!connector)
  5386. return NULL;
  5387. if (intel_connector_init(connector) < 0) {
  5388. kfree(connector);
  5389. return NULL;
  5390. }
  5391. return connector;
  5392. }
  5393. /* Simple connector->get_hw_state implementation for encoders that support only
  5394. * one connector and no cloning and hence the encoder state determines the state
  5395. * of the connector. */
  5396. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5397. {
  5398. enum pipe pipe = 0;
  5399. struct intel_encoder *encoder = connector->encoder;
  5400. return encoder->get_hw_state(encoder, &pipe);
  5401. }
  5402. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5403. {
  5404. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5405. return crtc_state->fdi_lanes;
  5406. return 0;
  5407. }
  5408. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5409. struct intel_crtc_state *pipe_config)
  5410. {
  5411. struct drm_atomic_state *state = pipe_config->base.state;
  5412. struct intel_crtc *other_crtc;
  5413. struct intel_crtc_state *other_crtc_state;
  5414. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5415. pipe_name(pipe), pipe_config->fdi_lanes);
  5416. if (pipe_config->fdi_lanes > 4) {
  5417. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5418. pipe_name(pipe), pipe_config->fdi_lanes);
  5419. return -EINVAL;
  5420. }
  5421. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5422. if (pipe_config->fdi_lanes > 2) {
  5423. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5424. pipe_config->fdi_lanes);
  5425. return -EINVAL;
  5426. } else {
  5427. return 0;
  5428. }
  5429. }
  5430. if (INTEL_INFO(dev)->num_pipes == 2)
  5431. return 0;
  5432. /* Ivybridge 3 pipe is really complicated */
  5433. switch (pipe) {
  5434. case PIPE_A:
  5435. return 0;
  5436. case PIPE_B:
  5437. if (pipe_config->fdi_lanes <= 2)
  5438. return 0;
  5439. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5440. other_crtc_state =
  5441. intel_atomic_get_crtc_state(state, other_crtc);
  5442. if (IS_ERR(other_crtc_state))
  5443. return PTR_ERR(other_crtc_state);
  5444. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5445. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5446. pipe_name(pipe), pipe_config->fdi_lanes);
  5447. return -EINVAL;
  5448. }
  5449. return 0;
  5450. case PIPE_C:
  5451. if (pipe_config->fdi_lanes > 2) {
  5452. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5453. pipe_name(pipe), pipe_config->fdi_lanes);
  5454. return -EINVAL;
  5455. }
  5456. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5457. other_crtc_state =
  5458. intel_atomic_get_crtc_state(state, other_crtc);
  5459. if (IS_ERR(other_crtc_state))
  5460. return PTR_ERR(other_crtc_state);
  5461. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5462. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5463. return -EINVAL;
  5464. }
  5465. return 0;
  5466. default:
  5467. BUG();
  5468. }
  5469. }
  5470. #define RETRY 1
  5471. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5472. struct intel_crtc_state *pipe_config)
  5473. {
  5474. struct drm_device *dev = intel_crtc->base.dev;
  5475. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5476. int lane, link_bw, fdi_dotclock, ret;
  5477. bool needs_recompute = false;
  5478. retry:
  5479. /* FDI is a binary signal running at ~2.7GHz, encoding
  5480. * each output octet as 10 bits. The actual frequency
  5481. * is stored as a divider into a 100MHz clock, and the
  5482. * mode pixel clock is stored in units of 1KHz.
  5483. * Hence the bw of each lane in terms of the mode signal
  5484. * is:
  5485. */
  5486. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  5487. fdi_dotclock = adjusted_mode->crtc_clock;
  5488. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5489. pipe_config->pipe_bpp);
  5490. pipe_config->fdi_lanes = lane;
  5491. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5492. link_bw, &pipe_config->fdi_m_n);
  5493. ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  5494. intel_crtc->pipe, pipe_config);
  5495. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5496. pipe_config->pipe_bpp -= 2*3;
  5497. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5498. pipe_config->pipe_bpp);
  5499. needs_recompute = true;
  5500. pipe_config->bw_constrained = true;
  5501. goto retry;
  5502. }
  5503. if (needs_recompute)
  5504. return RETRY;
  5505. return ret;
  5506. }
  5507. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5508. struct intel_crtc_state *pipe_config)
  5509. {
  5510. if (pipe_config->pipe_bpp > 24)
  5511. return false;
  5512. /* HSW can handle pixel rate up to cdclk? */
  5513. if (IS_HASWELL(dev_priv->dev))
  5514. return true;
  5515. /*
  5516. * We compare against max which means we must take
  5517. * the increased cdclk requirement into account when
  5518. * calculating the new cdclk.
  5519. *
  5520. * Should measure whether using a lower cdclk w/o IPS
  5521. */
  5522. return ilk_pipe_pixel_rate(pipe_config) <=
  5523. dev_priv->max_cdclk_freq * 95 / 100;
  5524. }
  5525. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5526. struct intel_crtc_state *pipe_config)
  5527. {
  5528. struct drm_device *dev = crtc->base.dev;
  5529. struct drm_i915_private *dev_priv = dev->dev_private;
  5530. pipe_config->ips_enabled = i915.enable_ips &&
  5531. hsw_crtc_supports_ips(crtc) &&
  5532. pipe_config_supports_ips(dev_priv, pipe_config);
  5533. }
  5534. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5535. {
  5536. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5537. /* GDG double wide on either pipe, otherwise pipe A only */
  5538. return INTEL_INFO(dev_priv)->gen < 4 &&
  5539. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5540. }
  5541. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5542. struct intel_crtc_state *pipe_config)
  5543. {
  5544. struct drm_device *dev = crtc->base.dev;
  5545. struct drm_i915_private *dev_priv = dev->dev_private;
  5546. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5547. /* FIXME should check pixel clock limits on all platforms */
  5548. if (INTEL_INFO(dev)->gen < 4) {
  5549. int clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5550. /*
  5551. * Enable double wide mode when the dot clock
  5552. * is > 90% of the (display) core speed.
  5553. */
  5554. if (intel_crtc_supports_double_wide(crtc) &&
  5555. adjusted_mode->crtc_clock > clock_limit) {
  5556. clock_limit *= 2;
  5557. pipe_config->double_wide = true;
  5558. }
  5559. if (adjusted_mode->crtc_clock > clock_limit) {
  5560. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5561. adjusted_mode->crtc_clock, clock_limit,
  5562. yesno(pipe_config->double_wide));
  5563. return -EINVAL;
  5564. }
  5565. }
  5566. /*
  5567. * Pipe horizontal size must be even in:
  5568. * - DVO ganged mode
  5569. * - LVDS dual channel mode
  5570. * - Double wide pipe
  5571. */
  5572. if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5573. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5574. pipe_config->pipe_src_w &= ~1;
  5575. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5576. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5577. */
  5578. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  5579. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5580. return -EINVAL;
  5581. if (HAS_IPS(dev))
  5582. hsw_compute_ips_config(crtc, pipe_config);
  5583. if (pipe_config->has_pch_encoder)
  5584. return ironlake_fdi_compute_config(crtc, pipe_config);
  5585. return 0;
  5586. }
  5587. static int skylake_get_display_clock_speed(struct drm_device *dev)
  5588. {
  5589. struct drm_i915_private *dev_priv = to_i915(dev);
  5590. uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
  5591. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5592. uint32_t linkrate;
  5593. if (!(lcpll1 & LCPLL_PLL_ENABLE))
  5594. return 24000; /* 24MHz is the cd freq with NSSC ref */
  5595. if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
  5596. return 540000;
  5597. linkrate = (I915_READ(DPLL_CTRL1) &
  5598. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
  5599. if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
  5600. linkrate == DPLL_CTRL1_LINK_RATE_1080) {
  5601. /* vco 8640 */
  5602. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5603. case CDCLK_FREQ_450_432:
  5604. return 432000;
  5605. case CDCLK_FREQ_337_308:
  5606. return 308570;
  5607. case CDCLK_FREQ_675_617:
  5608. return 617140;
  5609. default:
  5610. WARN(1, "Unknown cd freq selection\n");
  5611. }
  5612. } else {
  5613. /* vco 8100 */
  5614. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  5615. case CDCLK_FREQ_450_432:
  5616. return 450000;
  5617. case CDCLK_FREQ_337_308:
  5618. return 337500;
  5619. case CDCLK_FREQ_675_617:
  5620. return 675000;
  5621. default:
  5622. WARN(1, "Unknown cd freq selection\n");
  5623. }
  5624. }
  5625. /* error case, do as if DPLL0 isn't enabled */
  5626. return 24000;
  5627. }
  5628. static int broxton_get_display_clock_speed(struct drm_device *dev)
  5629. {
  5630. struct drm_i915_private *dev_priv = to_i915(dev);
  5631. uint32_t cdctl = I915_READ(CDCLK_CTL);
  5632. uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
  5633. uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
  5634. int cdclk;
  5635. if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
  5636. return 19200;
  5637. cdclk = 19200 * pll_ratio / 2;
  5638. switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
  5639. case BXT_CDCLK_CD2X_DIV_SEL_1:
  5640. return cdclk; /* 576MHz or 624MHz */
  5641. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  5642. return cdclk * 2 / 3; /* 384MHz */
  5643. case BXT_CDCLK_CD2X_DIV_SEL_2:
  5644. return cdclk / 2; /* 288MHz */
  5645. case BXT_CDCLK_CD2X_DIV_SEL_4:
  5646. return cdclk / 4; /* 144MHz */
  5647. }
  5648. /* error case, do as if DE PLL isn't enabled */
  5649. return 19200;
  5650. }
  5651. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  5652. {
  5653. struct drm_i915_private *dev_priv = dev->dev_private;
  5654. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5655. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5656. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5657. return 800000;
  5658. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5659. return 450000;
  5660. else if (freq == LCPLL_CLK_FREQ_450)
  5661. return 450000;
  5662. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  5663. return 540000;
  5664. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  5665. return 337500;
  5666. else
  5667. return 675000;
  5668. }
  5669. static int haswell_get_display_clock_speed(struct drm_device *dev)
  5670. {
  5671. struct drm_i915_private *dev_priv = dev->dev_private;
  5672. uint32_t lcpll = I915_READ(LCPLL_CTL);
  5673. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  5674. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  5675. return 800000;
  5676. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  5677. return 450000;
  5678. else if (freq == LCPLL_CLK_FREQ_450)
  5679. return 450000;
  5680. else if (IS_HSW_ULT(dev))
  5681. return 337500;
  5682. else
  5683. return 540000;
  5684. }
  5685. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  5686. {
  5687. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  5688. CCK_DISPLAY_CLOCK_CONTROL);
  5689. }
  5690. static int ilk_get_display_clock_speed(struct drm_device *dev)
  5691. {
  5692. return 450000;
  5693. }
  5694. static int i945_get_display_clock_speed(struct drm_device *dev)
  5695. {
  5696. return 400000;
  5697. }
  5698. static int i915_get_display_clock_speed(struct drm_device *dev)
  5699. {
  5700. return 333333;
  5701. }
  5702. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  5703. {
  5704. return 200000;
  5705. }
  5706. static int pnv_get_display_clock_speed(struct drm_device *dev)
  5707. {
  5708. u16 gcfgc = 0;
  5709. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5710. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5711. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  5712. return 266667;
  5713. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  5714. return 333333;
  5715. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  5716. return 444444;
  5717. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  5718. return 200000;
  5719. default:
  5720. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  5721. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  5722. return 133333;
  5723. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  5724. return 166667;
  5725. }
  5726. }
  5727. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  5728. {
  5729. u16 gcfgc = 0;
  5730. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  5731. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  5732. return 133333;
  5733. else {
  5734. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  5735. case GC_DISPLAY_CLOCK_333_MHZ:
  5736. return 333333;
  5737. default:
  5738. case GC_DISPLAY_CLOCK_190_200_MHZ:
  5739. return 190000;
  5740. }
  5741. }
  5742. }
  5743. static int i865_get_display_clock_speed(struct drm_device *dev)
  5744. {
  5745. return 266667;
  5746. }
  5747. static int i85x_get_display_clock_speed(struct drm_device *dev)
  5748. {
  5749. u16 hpllcc = 0;
  5750. /*
  5751. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  5752. * encoding is different :(
  5753. * FIXME is this the right way to detect 852GM/852GMV?
  5754. */
  5755. if (dev->pdev->revision == 0x1)
  5756. return 133333;
  5757. pci_bus_read_config_word(dev->pdev->bus,
  5758. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  5759. /* Assume that the hardware is in the high speed state. This
  5760. * should be the default.
  5761. */
  5762. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  5763. case GC_CLOCK_133_200:
  5764. case GC_CLOCK_133_200_2:
  5765. case GC_CLOCK_100_200:
  5766. return 200000;
  5767. case GC_CLOCK_166_250:
  5768. return 250000;
  5769. case GC_CLOCK_100_133:
  5770. return 133333;
  5771. case GC_CLOCK_133_266:
  5772. case GC_CLOCK_133_266_2:
  5773. case GC_CLOCK_166_266:
  5774. return 266667;
  5775. }
  5776. /* Shouldn't happen */
  5777. return 0;
  5778. }
  5779. static int i830_get_display_clock_speed(struct drm_device *dev)
  5780. {
  5781. return 133333;
  5782. }
  5783. static unsigned int intel_hpll_vco(struct drm_device *dev)
  5784. {
  5785. struct drm_i915_private *dev_priv = dev->dev_private;
  5786. static const unsigned int blb_vco[8] = {
  5787. [0] = 3200000,
  5788. [1] = 4000000,
  5789. [2] = 5333333,
  5790. [3] = 4800000,
  5791. [4] = 6400000,
  5792. };
  5793. static const unsigned int pnv_vco[8] = {
  5794. [0] = 3200000,
  5795. [1] = 4000000,
  5796. [2] = 5333333,
  5797. [3] = 4800000,
  5798. [4] = 2666667,
  5799. };
  5800. static const unsigned int cl_vco[8] = {
  5801. [0] = 3200000,
  5802. [1] = 4000000,
  5803. [2] = 5333333,
  5804. [3] = 6400000,
  5805. [4] = 3333333,
  5806. [5] = 3566667,
  5807. [6] = 4266667,
  5808. };
  5809. static const unsigned int elk_vco[8] = {
  5810. [0] = 3200000,
  5811. [1] = 4000000,
  5812. [2] = 5333333,
  5813. [3] = 4800000,
  5814. };
  5815. static const unsigned int ctg_vco[8] = {
  5816. [0] = 3200000,
  5817. [1] = 4000000,
  5818. [2] = 5333333,
  5819. [3] = 6400000,
  5820. [4] = 2666667,
  5821. [5] = 4266667,
  5822. };
  5823. const unsigned int *vco_table;
  5824. unsigned int vco;
  5825. uint8_t tmp = 0;
  5826. /* FIXME other chipsets? */
  5827. if (IS_GM45(dev))
  5828. vco_table = ctg_vco;
  5829. else if (IS_G4X(dev))
  5830. vco_table = elk_vco;
  5831. else if (IS_CRESTLINE(dev))
  5832. vco_table = cl_vco;
  5833. else if (IS_PINEVIEW(dev))
  5834. vco_table = pnv_vco;
  5835. else if (IS_G33(dev))
  5836. vco_table = blb_vco;
  5837. else
  5838. return 0;
  5839. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  5840. vco = vco_table[tmp & 0x7];
  5841. if (vco == 0)
  5842. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  5843. else
  5844. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  5845. return vco;
  5846. }
  5847. static int gm45_get_display_clock_speed(struct drm_device *dev)
  5848. {
  5849. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5850. uint16_t tmp = 0;
  5851. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5852. cdclk_sel = (tmp >> 12) & 0x1;
  5853. switch (vco) {
  5854. case 2666667:
  5855. case 4000000:
  5856. case 5333333:
  5857. return cdclk_sel ? 333333 : 222222;
  5858. case 3200000:
  5859. return cdclk_sel ? 320000 : 228571;
  5860. default:
  5861. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  5862. return 222222;
  5863. }
  5864. }
  5865. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  5866. {
  5867. static const uint8_t div_3200[] = { 16, 10, 8 };
  5868. static const uint8_t div_4000[] = { 20, 12, 10 };
  5869. static const uint8_t div_5333[] = { 24, 16, 14 };
  5870. const uint8_t *div_table;
  5871. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5872. uint16_t tmp = 0;
  5873. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5874. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  5875. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5876. goto fail;
  5877. switch (vco) {
  5878. case 3200000:
  5879. div_table = div_3200;
  5880. break;
  5881. case 4000000:
  5882. div_table = div_4000;
  5883. break;
  5884. case 5333333:
  5885. div_table = div_5333;
  5886. break;
  5887. default:
  5888. goto fail;
  5889. }
  5890. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5891. fail:
  5892. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  5893. return 200000;
  5894. }
  5895. static int g33_get_display_clock_speed(struct drm_device *dev)
  5896. {
  5897. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  5898. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  5899. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  5900. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  5901. const uint8_t *div_table;
  5902. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  5903. uint16_t tmp = 0;
  5904. pci_read_config_word(dev->pdev, GCFGC, &tmp);
  5905. cdclk_sel = (tmp >> 4) & 0x7;
  5906. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  5907. goto fail;
  5908. switch (vco) {
  5909. case 3200000:
  5910. div_table = div_3200;
  5911. break;
  5912. case 4000000:
  5913. div_table = div_4000;
  5914. break;
  5915. case 4800000:
  5916. div_table = div_4800;
  5917. break;
  5918. case 5333333:
  5919. div_table = div_5333;
  5920. break;
  5921. default:
  5922. goto fail;
  5923. }
  5924. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  5925. fail:
  5926. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  5927. return 190476;
  5928. }
  5929. static void
  5930. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5931. {
  5932. while (*num > DATA_LINK_M_N_MASK ||
  5933. *den > DATA_LINK_M_N_MASK) {
  5934. *num >>= 1;
  5935. *den >>= 1;
  5936. }
  5937. }
  5938. static void compute_m_n(unsigned int m, unsigned int n,
  5939. uint32_t *ret_m, uint32_t *ret_n)
  5940. {
  5941. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5942. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5943. intel_reduce_m_n_ratio(ret_m, ret_n);
  5944. }
  5945. void
  5946. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5947. int pixel_clock, int link_clock,
  5948. struct intel_link_m_n *m_n)
  5949. {
  5950. m_n->tu = 64;
  5951. compute_m_n(bits_per_pixel * pixel_clock,
  5952. link_clock * nlanes * 8,
  5953. &m_n->gmch_m, &m_n->gmch_n);
  5954. compute_m_n(pixel_clock, link_clock,
  5955. &m_n->link_m, &m_n->link_n);
  5956. }
  5957. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5958. {
  5959. if (i915.panel_use_ssc >= 0)
  5960. return i915.panel_use_ssc != 0;
  5961. return dev_priv->vbt.lvds_use_ssc
  5962. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5963. }
  5964. static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
  5965. int num_connectors)
  5966. {
  5967. struct drm_device *dev = crtc_state->base.crtc->dev;
  5968. struct drm_i915_private *dev_priv = dev->dev_private;
  5969. int refclk;
  5970. WARN_ON(!crtc_state->base.state);
  5971. if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
  5972. refclk = 100000;
  5973. } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5974. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5975. refclk = dev_priv->vbt.lvds_ssc_freq;
  5976. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5977. } else if (!IS_GEN2(dev)) {
  5978. refclk = 96000;
  5979. } else {
  5980. refclk = 48000;
  5981. }
  5982. return refclk;
  5983. }
  5984. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5985. {
  5986. return (1 << dpll->n) << 16 | dpll->m2;
  5987. }
  5988. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5989. {
  5990. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5991. }
  5992. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5993. struct intel_crtc_state *crtc_state,
  5994. intel_clock_t *reduced_clock)
  5995. {
  5996. struct drm_device *dev = crtc->base.dev;
  5997. u32 fp, fp2 = 0;
  5998. if (IS_PINEVIEW(dev)) {
  5999. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6000. if (reduced_clock)
  6001. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6002. } else {
  6003. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6004. if (reduced_clock)
  6005. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6006. }
  6007. crtc_state->dpll_hw_state.fp0 = fp;
  6008. crtc->lowfreq_avail = false;
  6009. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6010. reduced_clock) {
  6011. crtc_state->dpll_hw_state.fp1 = fp2;
  6012. crtc->lowfreq_avail = true;
  6013. } else {
  6014. crtc_state->dpll_hw_state.fp1 = fp;
  6015. }
  6016. }
  6017. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6018. pipe)
  6019. {
  6020. u32 reg_val;
  6021. /*
  6022. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6023. * and set it to a reasonable value instead.
  6024. */
  6025. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6026. reg_val &= 0xffffff00;
  6027. reg_val |= 0x00000030;
  6028. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6029. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6030. reg_val &= 0x8cffffff;
  6031. reg_val = 0x8c000000;
  6032. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6033. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6034. reg_val &= 0xffffff00;
  6035. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6036. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6037. reg_val &= 0x00ffffff;
  6038. reg_val |= 0xb0000000;
  6039. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6040. }
  6041. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6042. struct intel_link_m_n *m_n)
  6043. {
  6044. struct drm_device *dev = crtc->base.dev;
  6045. struct drm_i915_private *dev_priv = dev->dev_private;
  6046. int pipe = crtc->pipe;
  6047. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6048. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6049. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6050. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6051. }
  6052. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6053. struct intel_link_m_n *m_n,
  6054. struct intel_link_m_n *m2_n2)
  6055. {
  6056. struct drm_device *dev = crtc->base.dev;
  6057. struct drm_i915_private *dev_priv = dev->dev_private;
  6058. int pipe = crtc->pipe;
  6059. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6060. if (INTEL_INFO(dev)->gen >= 5) {
  6061. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6062. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6063. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6064. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6065. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6066. * for gen < 8) and if DRRS is supported (to make sure the
  6067. * registers are not unnecessarily accessed).
  6068. */
  6069. if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
  6070. crtc->config->has_drrs) {
  6071. I915_WRITE(PIPE_DATA_M2(transcoder),
  6072. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6073. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6074. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6075. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6076. }
  6077. } else {
  6078. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6079. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6080. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6081. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6082. }
  6083. }
  6084. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6085. {
  6086. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6087. if (m_n == M1_N1) {
  6088. dp_m_n = &crtc->config->dp_m_n;
  6089. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6090. } else if (m_n == M2_N2) {
  6091. /*
  6092. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6093. * needs to be programmed into M1_N1.
  6094. */
  6095. dp_m_n = &crtc->config->dp_m2_n2;
  6096. } else {
  6097. DRM_ERROR("Unsupported divider value\n");
  6098. return;
  6099. }
  6100. if (crtc->config->has_pch_encoder)
  6101. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6102. else
  6103. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6104. }
  6105. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6106. struct intel_crtc_state *pipe_config)
  6107. {
  6108. u32 dpll, dpll_md;
  6109. /*
  6110. * Enable DPIO clock input. We should never disable the reference
  6111. * clock for pipe B, since VGA hotplug / manual detection depends
  6112. * on it.
  6113. */
  6114. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
  6115. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
  6116. /* We should never disable this, set it here for state tracking */
  6117. if (crtc->pipe == PIPE_B)
  6118. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6119. dpll |= DPLL_VCO_ENABLE;
  6120. pipe_config->dpll_hw_state.dpll = dpll;
  6121. dpll_md = (pipe_config->pixel_multiplier - 1)
  6122. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6123. pipe_config->dpll_hw_state.dpll_md = dpll_md;
  6124. }
  6125. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6126. const struct intel_crtc_state *pipe_config)
  6127. {
  6128. struct drm_device *dev = crtc->base.dev;
  6129. struct drm_i915_private *dev_priv = dev->dev_private;
  6130. int pipe = crtc->pipe;
  6131. u32 mdiv;
  6132. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6133. u32 coreclk, reg_val;
  6134. mutex_lock(&dev_priv->sb_lock);
  6135. bestn = pipe_config->dpll.n;
  6136. bestm1 = pipe_config->dpll.m1;
  6137. bestm2 = pipe_config->dpll.m2;
  6138. bestp1 = pipe_config->dpll.p1;
  6139. bestp2 = pipe_config->dpll.p2;
  6140. /* See eDP HDMI DPIO driver vbios notes doc */
  6141. /* PLL B needs special handling */
  6142. if (pipe == PIPE_B)
  6143. vlv_pllb_recal_opamp(dev_priv, pipe);
  6144. /* Set up Tx target for periodic Rcomp update */
  6145. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6146. /* Disable target IRef on PLL */
  6147. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6148. reg_val &= 0x00ffffff;
  6149. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6150. /* Disable fast lock */
  6151. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6152. /* Set idtafcrecal before PLL is enabled */
  6153. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6154. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6155. mdiv |= ((bestn << DPIO_N_SHIFT));
  6156. mdiv |= (1 << DPIO_K_SHIFT);
  6157. /*
  6158. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6159. * but we don't support that).
  6160. * Note: don't use the DAC post divider as it seems unstable.
  6161. */
  6162. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6163. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6164. mdiv |= DPIO_ENABLE_CALIBRATION;
  6165. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6166. /* Set HBR and RBR LPF coefficients */
  6167. if (pipe_config->port_clock == 162000 ||
  6168. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
  6169. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
  6170. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6171. 0x009f0003);
  6172. else
  6173. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6174. 0x00d0000f);
  6175. if (pipe_config->has_dp_encoder) {
  6176. /* Use SSC source */
  6177. if (pipe == PIPE_A)
  6178. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6179. 0x0df40000);
  6180. else
  6181. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6182. 0x0df70000);
  6183. } else { /* HDMI or VGA */
  6184. /* Use bend source */
  6185. if (pipe == PIPE_A)
  6186. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6187. 0x0df70000);
  6188. else
  6189. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6190. 0x0df40000);
  6191. }
  6192. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6193. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6194. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  6195. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  6196. coreclk |= 0x01000000;
  6197. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6198. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6199. mutex_unlock(&dev_priv->sb_lock);
  6200. }
  6201. static void chv_compute_dpll(struct intel_crtc *crtc,
  6202. struct intel_crtc_state *pipe_config)
  6203. {
  6204. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6205. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  6206. DPLL_VCO_ENABLE;
  6207. if (crtc->pipe != PIPE_A)
  6208. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6209. pipe_config->dpll_hw_state.dpll_md =
  6210. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6211. }
  6212. static void chv_prepare_pll(struct intel_crtc *crtc,
  6213. const struct intel_crtc_state *pipe_config)
  6214. {
  6215. struct drm_device *dev = crtc->base.dev;
  6216. struct drm_i915_private *dev_priv = dev->dev_private;
  6217. int pipe = crtc->pipe;
  6218. int dpll_reg = DPLL(crtc->pipe);
  6219. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6220. u32 loopfilter, tribuf_calcntr;
  6221. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6222. u32 dpio_val;
  6223. int vco;
  6224. bestn = pipe_config->dpll.n;
  6225. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6226. bestm1 = pipe_config->dpll.m1;
  6227. bestm2 = pipe_config->dpll.m2 >> 22;
  6228. bestp1 = pipe_config->dpll.p1;
  6229. bestp2 = pipe_config->dpll.p2;
  6230. vco = pipe_config->dpll.vco;
  6231. dpio_val = 0;
  6232. loopfilter = 0;
  6233. /*
  6234. * Enable Refclk and SSC
  6235. */
  6236. I915_WRITE(dpll_reg,
  6237. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6238. mutex_lock(&dev_priv->sb_lock);
  6239. /* p1 and p2 divider */
  6240. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6241. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6242. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6243. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6244. 1 << DPIO_CHV_K_DIV_SHIFT);
  6245. /* Feedback post-divider - m2 */
  6246. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6247. /* Feedback refclk divider - n and m1 */
  6248. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6249. DPIO_CHV_M1_DIV_BY_2 |
  6250. 1 << DPIO_CHV_N_DIV_SHIFT);
  6251. /* M2 fraction division */
  6252. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6253. /* M2 fraction division enable */
  6254. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6255. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6256. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6257. if (bestm2_frac)
  6258. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6259. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6260. /* Program digital lock detect threshold */
  6261. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6262. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6263. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6264. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6265. if (!bestm2_frac)
  6266. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6267. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6268. /* Loop filter */
  6269. if (vco == 5400000) {
  6270. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6271. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6272. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6273. tribuf_calcntr = 0x9;
  6274. } else if (vco <= 6200000) {
  6275. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6276. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6277. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6278. tribuf_calcntr = 0x9;
  6279. } else if (vco <= 6480000) {
  6280. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6281. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6282. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6283. tribuf_calcntr = 0x8;
  6284. } else {
  6285. /* Not supported. Apply the same limits as in the max case */
  6286. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6287. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6288. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6289. tribuf_calcntr = 0;
  6290. }
  6291. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6292. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6293. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6294. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6295. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6296. /* AFC Recal */
  6297. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6298. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6299. DPIO_AFC_RECAL);
  6300. mutex_unlock(&dev_priv->sb_lock);
  6301. }
  6302. /**
  6303. * vlv_force_pll_on - forcibly enable just the PLL
  6304. * @dev_priv: i915 private structure
  6305. * @pipe: pipe PLL to enable
  6306. * @dpll: PLL configuration
  6307. *
  6308. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6309. * in cases where we need the PLL enabled even when @pipe is not going to
  6310. * be enabled.
  6311. */
  6312. void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6313. const struct dpll *dpll)
  6314. {
  6315. struct intel_crtc *crtc =
  6316. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6317. struct intel_crtc_state pipe_config = {
  6318. .base.crtc = &crtc->base,
  6319. .pixel_multiplier = 1,
  6320. .dpll = *dpll,
  6321. };
  6322. if (IS_CHERRYVIEW(dev)) {
  6323. chv_compute_dpll(crtc, &pipe_config);
  6324. chv_prepare_pll(crtc, &pipe_config);
  6325. chv_enable_pll(crtc, &pipe_config);
  6326. } else {
  6327. vlv_compute_dpll(crtc, &pipe_config);
  6328. vlv_prepare_pll(crtc, &pipe_config);
  6329. vlv_enable_pll(crtc, &pipe_config);
  6330. }
  6331. }
  6332. /**
  6333. * vlv_force_pll_off - forcibly disable just the PLL
  6334. * @dev_priv: i915 private structure
  6335. * @pipe: pipe PLL to disable
  6336. *
  6337. * Disable the PLL for @pipe. To be used in cases where we need
  6338. * the PLL enabled even when @pipe is not going to be enabled.
  6339. */
  6340. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6341. {
  6342. if (IS_CHERRYVIEW(dev))
  6343. chv_disable_pll(to_i915(dev), pipe);
  6344. else
  6345. vlv_disable_pll(to_i915(dev), pipe);
  6346. }
  6347. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6348. struct intel_crtc_state *crtc_state,
  6349. intel_clock_t *reduced_clock,
  6350. int num_connectors)
  6351. {
  6352. struct drm_device *dev = crtc->base.dev;
  6353. struct drm_i915_private *dev_priv = dev->dev_private;
  6354. u32 dpll;
  6355. bool is_sdvo;
  6356. struct dpll *clock = &crtc_state->dpll;
  6357. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6358. is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6359. intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
  6360. dpll = DPLL_VGA_MODE_DIS;
  6361. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
  6362. dpll |= DPLLB_MODE_LVDS;
  6363. else
  6364. dpll |= DPLLB_MODE_DAC_SERIAL;
  6365. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6366. dpll |= (crtc_state->pixel_multiplier - 1)
  6367. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6368. }
  6369. if (is_sdvo)
  6370. dpll |= DPLL_SDVO_HIGH_SPEED;
  6371. if (crtc_state->has_dp_encoder)
  6372. dpll |= DPLL_SDVO_HIGH_SPEED;
  6373. /* compute bitmask from p1 value */
  6374. if (IS_PINEVIEW(dev))
  6375. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6376. else {
  6377. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6378. if (IS_G4X(dev) && reduced_clock)
  6379. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6380. }
  6381. switch (clock->p2) {
  6382. case 5:
  6383. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6384. break;
  6385. case 7:
  6386. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6387. break;
  6388. case 10:
  6389. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6390. break;
  6391. case 14:
  6392. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6393. break;
  6394. }
  6395. if (INTEL_INFO(dev)->gen >= 4)
  6396. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6397. if (crtc_state->sdvo_tv_clock)
  6398. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6399. else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6400. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6401. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6402. else
  6403. dpll |= PLL_REF_INPUT_DREFCLK;
  6404. dpll |= DPLL_VCO_ENABLE;
  6405. crtc_state->dpll_hw_state.dpll = dpll;
  6406. if (INTEL_INFO(dev)->gen >= 4) {
  6407. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6408. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6409. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6410. }
  6411. }
  6412. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6413. struct intel_crtc_state *crtc_state,
  6414. intel_clock_t *reduced_clock,
  6415. int num_connectors)
  6416. {
  6417. struct drm_device *dev = crtc->base.dev;
  6418. struct drm_i915_private *dev_priv = dev->dev_private;
  6419. u32 dpll;
  6420. struct dpll *clock = &crtc_state->dpll;
  6421. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6422. dpll = DPLL_VGA_MODE_DIS;
  6423. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6424. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6425. } else {
  6426. if (clock->p1 == 2)
  6427. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6428. else
  6429. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6430. if (clock->p2 == 4)
  6431. dpll |= PLL_P2_DIVIDE_BY_4;
  6432. }
  6433. if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
  6434. dpll |= DPLL_DVO_2X_MODE;
  6435. if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6436. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  6437. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6438. else
  6439. dpll |= PLL_REF_INPUT_DREFCLK;
  6440. dpll |= DPLL_VCO_ENABLE;
  6441. crtc_state->dpll_hw_state.dpll = dpll;
  6442. }
  6443. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6444. {
  6445. struct drm_device *dev = intel_crtc->base.dev;
  6446. struct drm_i915_private *dev_priv = dev->dev_private;
  6447. enum pipe pipe = intel_crtc->pipe;
  6448. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6449. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6450. uint32_t crtc_vtotal, crtc_vblank_end;
  6451. int vsyncshift = 0;
  6452. /* We need to be careful not to changed the adjusted mode, for otherwise
  6453. * the hw state checker will get angry at the mismatch. */
  6454. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6455. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6456. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6457. /* the chip adds 2 halflines automatically */
  6458. crtc_vtotal -= 1;
  6459. crtc_vblank_end -= 1;
  6460. if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6461. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6462. else
  6463. vsyncshift = adjusted_mode->crtc_hsync_start -
  6464. adjusted_mode->crtc_htotal / 2;
  6465. if (vsyncshift < 0)
  6466. vsyncshift += adjusted_mode->crtc_htotal;
  6467. }
  6468. if (INTEL_INFO(dev)->gen > 3)
  6469. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6470. I915_WRITE(HTOTAL(cpu_transcoder),
  6471. (adjusted_mode->crtc_hdisplay - 1) |
  6472. ((adjusted_mode->crtc_htotal - 1) << 16));
  6473. I915_WRITE(HBLANK(cpu_transcoder),
  6474. (adjusted_mode->crtc_hblank_start - 1) |
  6475. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6476. I915_WRITE(HSYNC(cpu_transcoder),
  6477. (adjusted_mode->crtc_hsync_start - 1) |
  6478. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6479. I915_WRITE(VTOTAL(cpu_transcoder),
  6480. (adjusted_mode->crtc_vdisplay - 1) |
  6481. ((crtc_vtotal - 1) << 16));
  6482. I915_WRITE(VBLANK(cpu_transcoder),
  6483. (adjusted_mode->crtc_vblank_start - 1) |
  6484. ((crtc_vblank_end - 1) << 16));
  6485. I915_WRITE(VSYNC(cpu_transcoder),
  6486. (adjusted_mode->crtc_vsync_start - 1) |
  6487. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6488. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6489. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6490. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6491. * bits. */
  6492. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  6493. (pipe == PIPE_B || pipe == PIPE_C))
  6494. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6495. /* pipesrc controls the size that is scaled from, which should
  6496. * always be the user's requested size.
  6497. */
  6498. I915_WRITE(PIPESRC(pipe),
  6499. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6500. (intel_crtc->config->pipe_src_h - 1));
  6501. }
  6502. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6503. struct intel_crtc_state *pipe_config)
  6504. {
  6505. struct drm_device *dev = crtc->base.dev;
  6506. struct drm_i915_private *dev_priv = dev->dev_private;
  6507. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6508. uint32_t tmp;
  6509. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6510. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6511. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6512. tmp = I915_READ(HBLANK(cpu_transcoder));
  6513. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6514. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6515. tmp = I915_READ(HSYNC(cpu_transcoder));
  6516. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6517. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6518. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6519. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6520. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  6521. tmp = I915_READ(VBLANK(cpu_transcoder));
  6522. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  6523. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  6524. tmp = I915_READ(VSYNC(cpu_transcoder));
  6525. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  6526. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  6527. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  6528. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  6529. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  6530. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  6531. }
  6532. tmp = I915_READ(PIPESRC(crtc->pipe));
  6533. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  6534. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  6535. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  6536. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  6537. }
  6538. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  6539. struct intel_crtc_state *pipe_config)
  6540. {
  6541. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  6542. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  6543. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  6544. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  6545. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  6546. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  6547. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  6548. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  6549. mode->flags = pipe_config->base.adjusted_mode.flags;
  6550. mode->type = DRM_MODE_TYPE_DRIVER;
  6551. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  6552. mode->flags |= pipe_config->base.adjusted_mode.flags;
  6553. mode->hsync = drm_mode_hsync(mode);
  6554. mode->vrefresh = drm_mode_vrefresh(mode);
  6555. drm_mode_set_name(mode);
  6556. }
  6557. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  6558. {
  6559. struct drm_device *dev = intel_crtc->base.dev;
  6560. struct drm_i915_private *dev_priv = dev->dev_private;
  6561. uint32_t pipeconf;
  6562. pipeconf = 0;
  6563. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  6564. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  6565. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  6566. if (intel_crtc->config->double_wide)
  6567. pipeconf |= PIPECONF_DOUBLE_WIDE;
  6568. /* only g4x and later have fancy bpc/dither controls */
  6569. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6570. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  6571. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  6572. pipeconf |= PIPECONF_DITHER_EN |
  6573. PIPECONF_DITHER_TYPE_SP;
  6574. switch (intel_crtc->config->pipe_bpp) {
  6575. case 18:
  6576. pipeconf |= PIPECONF_6BPC;
  6577. break;
  6578. case 24:
  6579. pipeconf |= PIPECONF_8BPC;
  6580. break;
  6581. case 30:
  6582. pipeconf |= PIPECONF_10BPC;
  6583. break;
  6584. default:
  6585. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6586. BUG();
  6587. }
  6588. }
  6589. if (HAS_PIPE_CXSR(dev)) {
  6590. if (intel_crtc->lowfreq_avail) {
  6591. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  6592. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  6593. } else {
  6594. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  6595. }
  6596. }
  6597. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  6598. if (INTEL_INFO(dev)->gen < 4 ||
  6599. intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
  6600. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  6601. else
  6602. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  6603. } else
  6604. pipeconf |= PIPECONF_PROGRESSIVE;
  6605. if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
  6606. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  6607. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  6608. POSTING_READ(PIPECONF(intel_crtc->pipe));
  6609. }
  6610. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6611. struct intel_crtc_state *crtc_state)
  6612. {
  6613. struct drm_device *dev = crtc->base.dev;
  6614. struct drm_i915_private *dev_priv = dev->dev_private;
  6615. int refclk, num_connectors = 0;
  6616. intel_clock_t clock;
  6617. bool ok;
  6618. bool is_dsi = false;
  6619. struct intel_encoder *encoder;
  6620. const intel_limit_t *limit;
  6621. struct drm_atomic_state *state = crtc_state->base.state;
  6622. struct drm_connector *connector;
  6623. struct drm_connector_state *connector_state;
  6624. int i;
  6625. memset(&crtc_state->dpll_hw_state, 0,
  6626. sizeof(crtc_state->dpll_hw_state));
  6627. for_each_connector_in_state(state, connector, connector_state, i) {
  6628. if (connector_state->crtc != &crtc->base)
  6629. continue;
  6630. encoder = to_intel_encoder(connector_state->best_encoder);
  6631. switch (encoder->type) {
  6632. case INTEL_OUTPUT_DSI:
  6633. is_dsi = true;
  6634. break;
  6635. default:
  6636. break;
  6637. }
  6638. num_connectors++;
  6639. }
  6640. if (is_dsi)
  6641. return 0;
  6642. if (!crtc_state->clock_set) {
  6643. refclk = i9xx_get_refclk(crtc_state, num_connectors);
  6644. /*
  6645. * Returns a set of divisors for the desired target clock with
  6646. * the given refclk, or FALSE. The returned values represent
  6647. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  6648. * 2) / p1 / p2.
  6649. */
  6650. limit = intel_limit(crtc_state, refclk);
  6651. ok = dev_priv->display.find_dpll(limit, crtc_state,
  6652. crtc_state->port_clock,
  6653. refclk, NULL, &clock);
  6654. if (!ok) {
  6655. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6656. return -EINVAL;
  6657. }
  6658. /* Compat-code for transition, will disappear. */
  6659. crtc_state->dpll.n = clock.n;
  6660. crtc_state->dpll.m1 = clock.m1;
  6661. crtc_state->dpll.m2 = clock.m2;
  6662. crtc_state->dpll.p1 = clock.p1;
  6663. crtc_state->dpll.p2 = clock.p2;
  6664. }
  6665. if (IS_GEN2(dev)) {
  6666. i8xx_compute_dpll(crtc, crtc_state, NULL,
  6667. num_connectors);
  6668. } else if (IS_CHERRYVIEW(dev)) {
  6669. chv_compute_dpll(crtc, crtc_state);
  6670. } else if (IS_VALLEYVIEW(dev)) {
  6671. vlv_compute_dpll(crtc, crtc_state);
  6672. } else {
  6673. i9xx_compute_dpll(crtc, crtc_state, NULL,
  6674. num_connectors);
  6675. }
  6676. return 0;
  6677. }
  6678. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6679. struct intel_crtc_state *pipe_config)
  6680. {
  6681. struct drm_device *dev = crtc->base.dev;
  6682. struct drm_i915_private *dev_priv = dev->dev_private;
  6683. uint32_t tmp;
  6684. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  6685. return;
  6686. tmp = I915_READ(PFIT_CONTROL);
  6687. if (!(tmp & PFIT_ENABLE))
  6688. return;
  6689. /* Check whether the pfit is attached to our pipe. */
  6690. if (INTEL_INFO(dev)->gen < 4) {
  6691. if (crtc->pipe != PIPE_B)
  6692. return;
  6693. } else {
  6694. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6695. return;
  6696. }
  6697. pipe_config->gmch_pfit.control = tmp;
  6698. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6699. if (INTEL_INFO(dev)->gen < 5)
  6700. pipe_config->gmch_pfit.lvds_border_bits =
  6701. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  6702. }
  6703. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6704. struct intel_crtc_state *pipe_config)
  6705. {
  6706. struct drm_device *dev = crtc->base.dev;
  6707. struct drm_i915_private *dev_priv = dev->dev_private;
  6708. int pipe = pipe_config->cpu_transcoder;
  6709. intel_clock_t clock;
  6710. u32 mdiv;
  6711. int refclk = 100000;
  6712. /* In case of MIPI DPLL will not even be used */
  6713. if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
  6714. return;
  6715. mutex_lock(&dev_priv->sb_lock);
  6716. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6717. mutex_unlock(&dev_priv->sb_lock);
  6718. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6719. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6720. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6721. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6722. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6723. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6724. }
  6725. static void
  6726. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6727. struct intel_initial_plane_config *plane_config)
  6728. {
  6729. struct drm_device *dev = crtc->base.dev;
  6730. struct drm_i915_private *dev_priv = dev->dev_private;
  6731. u32 val, base, offset;
  6732. int pipe = crtc->pipe, plane = crtc->plane;
  6733. int fourcc, pixel_format;
  6734. unsigned int aligned_height;
  6735. struct drm_framebuffer *fb;
  6736. struct intel_framebuffer *intel_fb;
  6737. val = I915_READ(DSPCNTR(plane));
  6738. if (!(val & DISPLAY_PLANE_ENABLE))
  6739. return;
  6740. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6741. if (!intel_fb) {
  6742. DRM_DEBUG_KMS("failed to alloc fb\n");
  6743. return;
  6744. }
  6745. fb = &intel_fb->base;
  6746. if (INTEL_INFO(dev)->gen >= 4) {
  6747. if (val & DISPPLANE_TILED) {
  6748. plane_config->tiling = I915_TILING_X;
  6749. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  6750. }
  6751. }
  6752. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6753. fourcc = i9xx_format_to_fourcc(pixel_format);
  6754. fb->pixel_format = fourcc;
  6755. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  6756. if (INTEL_INFO(dev)->gen >= 4) {
  6757. if (plane_config->tiling)
  6758. offset = I915_READ(DSPTILEOFF(plane));
  6759. else
  6760. offset = I915_READ(DSPLINOFF(plane));
  6761. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6762. } else {
  6763. base = I915_READ(DSPADDR(plane));
  6764. }
  6765. plane_config->base = base;
  6766. val = I915_READ(PIPESRC(pipe));
  6767. fb->width = ((val >> 16) & 0xfff) + 1;
  6768. fb->height = ((val >> 0) & 0xfff) + 1;
  6769. val = I915_READ(DSPSTRIDE(pipe));
  6770. fb->pitches[0] = val & 0xffffffc0;
  6771. aligned_height = intel_fb_align_height(dev, fb->height,
  6772. fb->pixel_format,
  6773. fb->modifier[0]);
  6774. plane_config->size = fb->pitches[0] * aligned_height;
  6775. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6776. pipe_name(pipe), plane, fb->width, fb->height,
  6777. fb->bits_per_pixel, base, fb->pitches[0],
  6778. plane_config->size);
  6779. plane_config->fb = intel_fb;
  6780. }
  6781. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6782. struct intel_crtc_state *pipe_config)
  6783. {
  6784. struct drm_device *dev = crtc->base.dev;
  6785. struct drm_i915_private *dev_priv = dev->dev_private;
  6786. int pipe = pipe_config->cpu_transcoder;
  6787. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6788. intel_clock_t clock;
  6789. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6790. int refclk = 100000;
  6791. mutex_lock(&dev_priv->sb_lock);
  6792. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6793. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6794. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6795. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6796. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6797. mutex_unlock(&dev_priv->sb_lock);
  6798. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6799. clock.m2 = (pll_dw0 & 0xff) << 22;
  6800. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6801. clock.m2 |= pll_dw2 & 0x3fffff;
  6802. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6803. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6804. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6805. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6806. }
  6807. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6808. struct intel_crtc_state *pipe_config)
  6809. {
  6810. struct drm_device *dev = crtc->base.dev;
  6811. struct drm_i915_private *dev_priv = dev->dev_private;
  6812. uint32_t tmp;
  6813. if (!intel_display_power_is_enabled(dev_priv,
  6814. POWER_DOMAIN_PIPE(crtc->pipe)))
  6815. return false;
  6816. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6817. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6818. tmp = I915_READ(PIPECONF(crtc->pipe));
  6819. if (!(tmp & PIPECONF_ENABLE))
  6820. return false;
  6821. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  6822. switch (tmp & PIPECONF_BPC_MASK) {
  6823. case PIPECONF_6BPC:
  6824. pipe_config->pipe_bpp = 18;
  6825. break;
  6826. case PIPECONF_8BPC:
  6827. pipe_config->pipe_bpp = 24;
  6828. break;
  6829. case PIPECONF_10BPC:
  6830. pipe_config->pipe_bpp = 30;
  6831. break;
  6832. default:
  6833. break;
  6834. }
  6835. }
  6836. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6837. pipe_config->limited_color_range = true;
  6838. if (INTEL_INFO(dev)->gen < 4)
  6839. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6840. intel_get_pipe_timings(crtc, pipe_config);
  6841. i9xx_get_pfit_config(crtc, pipe_config);
  6842. if (INTEL_INFO(dev)->gen >= 4) {
  6843. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6844. pipe_config->pixel_multiplier =
  6845. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6846. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6847. pipe_config->dpll_hw_state.dpll_md = tmp;
  6848. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  6849. tmp = I915_READ(DPLL(crtc->pipe));
  6850. pipe_config->pixel_multiplier =
  6851. ((tmp & SDVO_MULTIPLIER_MASK)
  6852. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6853. } else {
  6854. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6855. * port and will be fixed up in the encoder->get_config
  6856. * function. */
  6857. pipe_config->pixel_multiplier = 1;
  6858. }
  6859. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6860. if (!IS_VALLEYVIEW(dev)) {
  6861. /*
  6862. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6863. * on 830. Filter it out here so that we don't
  6864. * report errors due to that.
  6865. */
  6866. if (IS_I830(dev))
  6867. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6868. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6869. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6870. } else {
  6871. /* Mask out read-only status bits. */
  6872. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6873. DPLL_PORTC_READY_MASK |
  6874. DPLL_PORTB_READY_MASK);
  6875. }
  6876. if (IS_CHERRYVIEW(dev))
  6877. chv_crtc_clock_get(crtc, pipe_config);
  6878. else if (IS_VALLEYVIEW(dev))
  6879. vlv_crtc_clock_get(crtc, pipe_config);
  6880. else
  6881. i9xx_crtc_clock_get(crtc, pipe_config);
  6882. /*
  6883. * Normally the dotclock is filled in by the encoder .get_config()
  6884. * but in case the pipe is enabled w/o any ports we need a sane
  6885. * default.
  6886. */
  6887. pipe_config->base.adjusted_mode.crtc_clock =
  6888. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6889. return true;
  6890. }
  6891. static void ironlake_init_pch_refclk(struct drm_device *dev)
  6892. {
  6893. struct drm_i915_private *dev_priv = dev->dev_private;
  6894. struct intel_encoder *encoder;
  6895. u32 val, final;
  6896. bool has_lvds = false;
  6897. bool has_cpu_edp = false;
  6898. bool has_panel = false;
  6899. bool has_ck505 = false;
  6900. bool can_ssc = false;
  6901. /* We need to take the global config into account */
  6902. for_each_intel_encoder(dev, encoder) {
  6903. switch (encoder->type) {
  6904. case INTEL_OUTPUT_LVDS:
  6905. has_panel = true;
  6906. has_lvds = true;
  6907. break;
  6908. case INTEL_OUTPUT_EDP:
  6909. has_panel = true;
  6910. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6911. has_cpu_edp = true;
  6912. break;
  6913. default:
  6914. break;
  6915. }
  6916. }
  6917. if (HAS_PCH_IBX(dev)) {
  6918. has_ck505 = dev_priv->vbt.display_clock_mode;
  6919. can_ssc = has_ck505;
  6920. } else {
  6921. has_ck505 = false;
  6922. can_ssc = true;
  6923. }
  6924. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  6925. has_panel, has_lvds, has_ck505);
  6926. /* Ironlake: try to setup display ref clock before DPLL
  6927. * enabling. This is only under driver's control after
  6928. * PCH B stepping, previous chipset stepping should be
  6929. * ignoring this setting.
  6930. */
  6931. val = I915_READ(PCH_DREF_CONTROL);
  6932. /* As we must carefully and slowly disable/enable each source in turn,
  6933. * compute the final state we want first and check if we need to
  6934. * make any changes at all.
  6935. */
  6936. final = val;
  6937. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6938. if (has_ck505)
  6939. final |= DREF_NONSPREAD_CK505_ENABLE;
  6940. else
  6941. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6942. final &= ~DREF_SSC_SOURCE_MASK;
  6943. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6944. final &= ~DREF_SSC1_ENABLE;
  6945. if (has_panel) {
  6946. final |= DREF_SSC_SOURCE_ENABLE;
  6947. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6948. final |= DREF_SSC1_ENABLE;
  6949. if (has_cpu_edp) {
  6950. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6951. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6952. else
  6953. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6954. } else
  6955. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6956. } else {
  6957. final |= DREF_SSC_SOURCE_DISABLE;
  6958. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6959. }
  6960. if (final == val)
  6961. return;
  6962. /* Always enable nonspread source */
  6963. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6964. if (has_ck505)
  6965. val |= DREF_NONSPREAD_CK505_ENABLE;
  6966. else
  6967. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6968. if (has_panel) {
  6969. val &= ~DREF_SSC_SOURCE_MASK;
  6970. val |= DREF_SSC_SOURCE_ENABLE;
  6971. /* SSC must be turned on before enabling the CPU output */
  6972. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6973. DRM_DEBUG_KMS("Using SSC on panel\n");
  6974. val |= DREF_SSC1_ENABLE;
  6975. } else
  6976. val &= ~DREF_SSC1_ENABLE;
  6977. /* Get SSC going before enabling the outputs */
  6978. I915_WRITE(PCH_DREF_CONTROL, val);
  6979. POSTING_READ(PCH_DREF_CONTROL);
  6980. udelay(200);
  6981. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6982. /* Enable CPU source on CPU attached eDP */
  6983. if (has_cpu_edp) {
  6984. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6985. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6986. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6987. } else
  6988. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6989. } else
  6990. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6991. I915_WRITE(PCH_DREF_CONTROL, val);
  6992. POSTING_READ(PCH_DREF_CONTROL);
  6993. udelay(200);
  6994. } else {
  6995. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  6996. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6997. /* Turn off CPU output */
  6998. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6999. I915_WRITE(PCH_DREF_CONTROL, val);
  7000. POSTING_READ(PCH_DREF_CONTROL);
  7001. udelay(200);
  7002. /* Turn off the SSC source */
  7003. val &= ~DREF_SSC_SOURCE_MASK;
  7004. val |= DREF_SSC_SOURCE_DISABLE;
  7005. /* Turn off SSC1 */
  7006. val &= ~DREF_SSC1_ENABLE;
  7007. I915_WRITE(PCH_DREF_CONTROL, val);
  7008. POSTING_READ(PCH_DREF_CONTROL);
  7009. udelay(200);
  7010. }
  7011. BUG_ON(val != final);
  7012. }
  7013. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7014. {
  7015. uint32_t tmp;
  7016. tmp = I915_READ(SOUTH_CHICKEN2);
  7017. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7018. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7019. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  7020. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7021. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7022. tmp = I915_READ(SOUTH_CHICKEN2);
  7023. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7024. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7025. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  7026. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7027. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7028. }
  7029. /* WaMPhyProgramming:hsw */
  7030. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7031. {
  7032. uint32_t tmp;
  7033. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7034. tmp &= ~(0xFF << 24);
  7035. tmp |= (0x12 << 24);
  7036. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7037. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7038. tmp |= (1 << 11);
  7039. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7040. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7041. tmp |= (1 << 11);
  7042. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7043. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7044. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7045. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7046. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7047. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7048. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7049. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7050. tmp &= ~(7 << 13);
  7051. tmp |= (5 << 13);
  7052. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7053. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7054. tmp &= ~(7 << 13);
  7055. tmp |= (5 << 13);
  7056. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7057. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7058. tmp &= ~0xFF;
  7059. tmp |= 0x1C;
  7060. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7061. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7062. tmp &= ~0xFF;
  7063. tmp |= 0x1C;
  7064. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7065. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7066. tmp &= ~(0xFF << 16);
  7067. tmp |= (0x1C << 16);
  7068. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7069. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7070. tmp &= ~(0xFF << 16);
  7071. tmp |= (0x1C << 16);
  7072. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7073. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7074. tmp |= (1 << 27);
  7075. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7076. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7077. tmp |= (1 << 27);
  7078. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7079. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7080. tmp &= ~(0xF << 28);
  7081. tmp |= (4 << 28);
  7082. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7083. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7084. tmp &= ~(0xF << 28);
  7085. tmp |= (4 << 28);
  7086. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7087. }
  7088. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7089. * Programming" based on the parameters passed:
  7090. * - Sequence to enable CLKOUT_DP
  7091. * - Sequence to enable CLKOUT_DP without spread
  7092. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7093. */
  7094. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7095. bool with_fdi)
  7096. {
  7097. struct drm_i915_private *dev_priv = dev->dev_private;
  7098. uint32_t reg, tmp;
  7099. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7100. with_spread = true;
  7101. if (WARN(HAS_PCH_LPT_LP(dev) && with_fdi, "LP PCH doesn't have FDI\n"))
  7102. with_fdi = false;
  7103. mutex_lock(&dev_priv->sb_lock);
  7104. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7105. tmp &= ~SBI_SSCCTL_DISABLE;
  7106. tmp |= SBI_SSCCTL_PATHALT;
  7107. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7108. udelay(24);
  7109. if (with_spread) {
  7110. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7111. tmp &= ~SBI_SSCCTL_PATHALT;
  7112. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7113. if (with_fdi) {
  7114. lpt_reset_fdi_mphy(dev_priv);
  7115. lpt_program_fdi_mphy(dev_priv);
  7116. }
  7117. }
  7118. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7119. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7120. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7121. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7122. mutex_unlock(&dev_priv->sb_lock);
  7123. }
  7124. /* Sequence to disable CLKOUT_DP */
  7125. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7126. {
  7127. struct drm_i915_private *dev_priv = dev->dev_private;
  7128. uint32_t reg, tmp;
  7129. mutex_lock(&dev_priv->sb_lock);
  7130. reg = HAS_PCH_LPT_LP(dev) ? SBI_GEN0 : SBI_DBUFF0;
  7131. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7132. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7133. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7134. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7135. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7136. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7137. tmp |= SBI_SSCCTL_PATHALT;
  7138. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7139. udelay(32);
  7140. }
  7141. tmp |= SBI_SSCCTL_DISABLE;
  7142. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7143. }
  7144. mutex_unlock(&dev_priv->sb_lock);
  7145. }
  7146. static void lpt_init_pch_refclk(struct drm_device *dev)
  7147. {
  7148. struct intel_encoder *encoder;
  7149. bool has_vga = false;
  7150. for_each_intel_encoder(dev, encoder) {
  7151. switch (encoder->type) {
  7152. case INTEL_OUTPUT_ANALOG:
  7153. has_vga = true;
  7154. break;
  7155. default:
  7156. break;
  7157. }
  7158. }
  7159. if (has_vga)
  7160. lpt_enable_clkout_dp(dev, true, true);
  7161. else
  7162. lpt_disable_clkout_dp(dev);
  7163. }
  7164. /*
  7165. * Initialize reference clocks when the driver loads
  7166. */
  7167. void intel_init_pch_refclk(struct drm_device *dev)
  7168. {
  7169. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  7170. ironlake_init_pch_refclk(dev);
  7171. else if (HAS_PCH_LPT(dev))
  7172. lpt_init_pch_refclk(dev);
  7173. }
  7174. static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
  7175. {
  7176. struct drm_device *dev = crtc_state->base.crtc->dev;
  7177. struct drm_i915_private *dev_priv = dev->dev_private;
  7178. struct drm_atomic_state *state = crtc_state->base.state;
  7179. struct drm_connector *connector;
  7180. struct drm_connector_state *connector_state;
  7181. struct intel_encoder *encoder;
  7182. int num_connectors = 0, i;
  7183. bool is_lvds = false;
  7184. for_each_connector_in_state(state, connector, connector_state, i) {
  7185. if (connector_state->crtc != crtc_state->base.crtc)
  7186. continue;
  7187. encoder = to_intel_encoder(connector_state->best_encoder);
  7188. switch (encoder->type) {
  7189. case INTEL_OUTPUT_LVDS:
  7190. is_lvds = true;
  7191. break;
  7192. default:
  7193. break;
  7194. }
  7195. num_connectors++;
  7196. }
  7197. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  7198. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  7199. dev_priv->vbt.lvds_ssc_freq);
  7200. return dev_priv->vbt.lvds_ssc_freq;
  7201. }
  7202. return 120000;
  7203. }
  7204. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7205. {
  7206. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  7207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7208. int pipe = intel_crtc->pipe;
  7209. uint32_t val;
  7210. val = 0;
  7211. switch (intel_crtc->config->pipe_bpp) {
  7212. case 18:
  7213. val |= PIPECONF_6BPC;
  7214. break;
  7215. case 24:
  7216. val |= PIPECONF_8BPC;
  7217. break;
  7218. case 30:
  7219. val |= PIPECONF_10BPC;
  7220. break;
  7221. case 36:
  7222. val |= PIPECONF_12BPC;
  7223. break;
  7224. default:
  7225. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7226. BUG();
  7227. }
  7228. if (intel_crtc->config->dither)
  7229. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7230. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7231. val |= PIPECONF_INTERLACED_ILK;
  7232. else
  7233. val |= PIPECONF_PROGRESSIVE;
  7234. if (intel_crtc->config->limited_color_range)
  7235. val |= PIPECONF_COLOR_RANGE_SELECT;
  7236. I915_WRITE(PIPECONF(pipe), val);
  7237. POSTING_READ(PIPECONF(pipe));
  7238. }
  7239. /*
  7240. * Set up the pipe CSC unit.
  7241. *
  7242. * Currently only full range RGB to limited range RGB conversion
  7243. * is supported, but eventually this should handle various
  7244. * RGB<->YCbCr scenarios as well.
  7245. */
  7246. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  7247. {
  7248. struct drm_device *dev = crtc->dev;
  7249. struct drm_i915_private *dev_priv = dev->dev_private;
  7250. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7251. int pipe = intel_crtc->pipe;
  7252. uint16_t coeff = 0x7800; /* 1.0 */
  7253. /*
  7254. * TODO: Check what kind of values actually come out of the pipe
  7255. * with these coeff/postoff values and adjust to get the best
  7256. * accuracy. Perhaps we even need to take the bpc value into
  7257. * consideration.
  7258. */
  7259. if (intel_crtc->config->limited_color_range)
  7260. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  7261. /*
  7262. * GY/GU and RY/RU should be the other way around according
  7263. * to BSpec, but reality doesn't agree. Just set them up in
  7264. * a way that results in the correct picture.
  7265. */
  7266. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  7267. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  7268. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  7269. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  7270. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  7271. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  7272. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  7273. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  7274. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  7275. if (INTEL_INFO(dev)->gen > 6) {
  7276. uint16_t postoff = 0;
  7277. if (intel_crtc->config->limited_color_range)
  7278. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  7279. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  7280. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  7281. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  7282. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  7283. } else {
  7284. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  7285. if (intel_crtc->config->limited_color_range)
  7286. mode |= CSC_BLACK_SCREEN_OFFSET;
  7287. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  7288. }
  7289. }
  7290. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7291. {
  7292. struct drm_device *dev = crtc->dev;
  7293. struct drm_i915_private *dev_priv = dev->dev_private;
  7294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7295. enum pipe pipe = intel_crtc->pipe;
  7296. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7297. uint32_t val;
  7298. val = 0;
  7299. if (IS_HASWELL(dev) && intel_crtc->config->dither)
  7300. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7301. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7302. val |= PIPECONF_INTERLACED_ILK;
  7303. else
  7304. val |= PIPECONF_PROGRESSIVE;
  7305. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7306. POSTING_READ(PIPECONF(cpu_transcoder));
  7307. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  7308. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  7309. if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
  7310. val = 0;
  7311. switch (intel_crtc->config->pipe_bpp) {
  7312. case 18:
  7313. val |= PIPEMISC_DITHER_6_BPC;
  7314. break;
  7315. case 24:
  7316. val |= PIPEMISC_DITHER_8_BPC;
  7317. break;
  7318. case 30:
  7319. val |= PIPEMISC_DITHER_10_BPC;
  7320. break;
  7321. case 36:
  7322. val |= PIPEMISC_DITHER_12_BPC;
  7323. break;
  7324. default:
  7325. /* Case prevented by pipe_config_set_bpp. */
  7326. BUG();
  7327. }
  7328. if (intel_crtc->config->dither)
  7329. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7330. I915_WRITE(PIPEMISC(pipe), val);
  7331. }
  7332. }
  7333. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  7334. struct intel_crtc_state *crtc_state,
  7335. intel_clock_t *clock,
  7336. bool *has_reduced_clock,
  7337. intel_clock_t *reduced_clock)
  7338. {
  7339. struct drm_device *dev = crtc->dev;
  7340. struct drm_i915_private *dev_priv = dev->dev_private;
  7341. int refclk;
  7342. const intel_limit_t *limit;
  7343. bool ret;
  7344. refclk = ironlake_get_refclk(crtc_state);
  7345. /*
  7346. * Returns a set of divisors for the desired target clock with the given
  7347. * refclk, or FALSE. The returned values represent the clock equation:
  7348. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  7349. */
  7350. limit = intel_limit(crtc_state, refclk);
  7351. ret = dev_priv->display.find_dpll(limit, crtc_state,
  7352. crtc_state->port_clock,
  7353. refclk, NULL, clock);
  7354. if (!ret)
  7355. return false;
  7356. return true;
  7357. }
  7358. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7359. {
  7360. /*
  7361. * Account for spread spectrum to avoid
  7362. * oversubscribing the link. Max center spread
  7363. * is 2.5%; use 5% for safety's sake.
  7364. */
  7365. u32 bps = target_clock * bpp * 21 / 20;
  7366. return DIV_ROUND_UP(bps, link_bw * 8);
  7367. }
  7368. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7369. {
  7370. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7371. }
  7372. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7373. struct intel_crtc_state *crtc_state,
  7374. u32 *fp,
  7375. intel_clock_t *reduced_clock, u32 *fp2)
  7376. {
  7377. struct drm_crtc *crtc = &intel_crtc->base;
  7378. struct drm_device *dev = crtc->dev;
  7379. struct drm_i915_private *dev_priv = dev->dev_private;
  7380. struct drm_atomic_state *state = crtc_state->base.state;
  7381. struct drm_connector *connector;
  7382. struct drm_connector_state *connector_state;
  7383. struct intel_encoder *encoder;
  7384. uint32_t dpll;
  7385. int factor, num_connectors = 0, i;
  7386. bool is_lvds = false, is_sdvo = false;
  7387. for_each_connector_in_state(state, connector, connector_state, i) {
  7388. if (connector_state->crtc != crtc_state->base.crtc)
  7389. continue;
  7390. encoder = to_intel_encoder(connector_state->best_encoder);
  7391. switch (encoder->type) {
  7392. case INTEL_OUTPUT_LVDS:
  7393. is_lvds = true;
  7394. break;
  7395. case INTEL_OUTPUT_SDVO:
  7396. case INTEL_OUTPUT_HDMI:
  7397. is_sdvo = true;
  7398. break;
  7399. default:
  7400. break;
  7401. }
  7402. num_connectors++;
  7403. }
  7404. /* Enable autotuning of the PLL clock (if permissible) */
  7405. factor = 21;
  7406. if (is_lvds) {
  7407. if ((intel_panel_use_ssc(dev_priv) &&
  7408. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7409. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  7410. factor = 25;
  7411. } else if (crtc_state->sdvo_tv_clock)
  7412. factor = 20;
  7413. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7414. *fp |= FP_CB_TUNE;
  7415. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  7416. *fp2 |= FP_CB_TUNE;
  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) && num_connectors < 2)
  7447. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7448. else
  7449. dpll |= PLL_REF_INPUT_DREFCLK;
  7450. return dpll | DPLL_VCO_ENABLE;
  7451. }
  7452. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  7453. struct intel_crtc_state *crtc_state)
  7454. {
  7455. struct drm_device *dev = crtc->base.dev;
  7456. intel_clock_t clock, reduced_clock;
  7457. u32 dpll = 0, fp = 0, fp2 = 0;
  7458. bool ok, has_reduced_clock = false;
  7459. bool is_lvds = false;
  7460. struct intel_shared_dpll *pll;
  7461. memset(&crtc_state->dpll_hw_state, 0,
  7462. sizeof(crtc_state->dpll_hw_state));
  7463. is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
  7464. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  7465. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  7466. ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
  7467. &has_reduced_clock, &reduced_clock);
  7468. if (!ok && !crtc_state->clock_set) {
  7469. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7470. return -EINVAL;
  7471. }
  7472. /* Compat-code for transition, will disappear. */
  7473. if (!crtc_state->clock_set) {
  7474. crtc_state->dpll.n = clock.n;
  7475. crtc_state->dpll.m1 = clock.m1;
  7476. crtc_state->dpll.m2 = clock.m2;
  7477. crtc_state->dpll.p1 = clock.p1;
  7478. crtc_state->dpll.p2 = clock.p2;
  7479. }
  7480. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  7481. if (crtc_state->has_pch_encoder) {
  7482. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7483. if (has_reduced_clock)
  7484. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  7485. dpll = ironlake_compute_dpll(crtc, crtc_state,
  7486. &fp, &reduced_clock,
  7487. has_reduced_clock ? &fp2 : NULL);
  7488. crtc_state->dpll_hw_state.dpll = dpll;
  7489. crtc_state->dpll_hw_state.fp0 = fp;
  7490. if (has_reduced_clock)
  7491. crtc_state->dpll_hw_state.fp1 = fp2;
  7492. else
  7493. crtc_state->dpll_hw_state.fp1 = fp;
  7494. pll = intel_get_shared_dpll(crtc, crtc_state);
  7495. if (pll == NULL) {
  7496. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7497. pipe_name(crtc->pipe));
  7498. return -EINVAL;
  7499. }
  7500. }
  7501. if (is_lvds && has_reduced_clock)
  7502. crtc->lowfreq_avail = true;
  7503. else
  7504. crtc->lowfreq_avail = false;
  7505. return 0;
  7506. }
  7507. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  7508. struct intel_link_m_n *m_n)
  7509. {
  7510. struct drm_device *dev = crtc->base.dev;
  7511. struct drm_i915_private *dev_priv = dev->dev_private;
  7512. enum pipe pipe = crtc->pipe;
  7513. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  7514. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  7515. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  7516. & ~TU_SIZE_MASK;
  7517. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  7518. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  7519. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7520. }
  7521. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  7522. enum transcoder transcoder,
  7523. struct intel_link_m_n *m_n,
  7524. struct intel_link_m_n *m2_n2)
  7525. {
  7526. struct drm_device *dev = crtc->base.dev;
  7527. struct drm_i915_private *dev_priv = dev->dev_private;
  7528. enum pipe pipe = crtc->pipe;
  7529. if (INTEL_INFO(dev)->gen >= 5) {
  7530. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  7531. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  7532. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  7533. & ~TU_SIZE_MASK;
  7534. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  7535. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  7536. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7537. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  7538. * gen < 8) and if DRRS is supported (to make sure the
  7539. * registers are not unnecessarily read).
  7540. */
  7541. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  7542. crtc->config->has_drrs) {
  7543. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  7544. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  7545. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  7546. & ~TU_SIZE_MASK;
  7547. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  7548. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  7549. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7550. }
  7551. } else {
  7552. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  7553. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  7554. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  7555. & ~TU_SIZE_MASK;
  7556. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  7557. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  7558. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  7559. }
  7560. }
  7561. void intel_dp_get_m_n(struct intel_crtc *crtc,
  7562. struct intel_crtc_state *pipe_config)
  7563. {
  7564. if (pipe_config->has_pch_encoder)
  7565. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  7566. else
  7567. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7568. &pipe_config->dp_m_n,
  7569. &pipe_config->dp_m2_n2);
  7570. }
  7571. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  7572. struct intel_crtc_state *pipe_config)
  7573. {
  7574. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  7575. &pipe_config->fdi_m_n, NULL);
  7576. }
  7577. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  7578. struct intel_crtc_state *pipe_config)
  7579. {
  7580. struct drm_device *dev = crtc->base.dev;
  7581. struct drm_i915_private *dev_priv = dev->dev_private;
  7582. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  7583. uint32_t ps_ctrl = 0;
  7584. int id = -1;
  7585. int i;
  7586. /* find scaler attached to this pipe */
  7587. for (i = 0; i < crtc->num_scalers; i++) {
  7588. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  7589. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  7590. id = i;
  7591. pipe_config->pch_pfit.enabled = true;
  7592. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  7593. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7594. break;
  7595. }
  7596. }
  7597. scaler_state->scaler_id = id;
  7598. if (id >= 0) {
  7599. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7600. } else {
  7601. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7602. }
  7603. }
  7604. static void
  7605. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7606. struct intel_initial_plane_config *plane_config)
  7607. {
  7608. struct drm_device *dev = crtc->base.dev;
  7609. struct drm_i915_private *dev_priv = dev->dev_private;
  7610. u32 val, base, offset, stride_mult, tiling;
  7611. int pipe = crtc->pipe;
  7612. int fourcc, pixel_format;
  7613. unsigned int aligned_height;
  7614. struct drm_framebuffer *fb;
  7615. struct intel_framebuffer *intel_fb;
  7616. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7617. if (!intel_fb) {
  7618. DRM_DEBUG_KMS("failed to alloc fb\n");
  7619. return;
  7620. }
  7621. fb = &intel_fb->base;
  7622. val = I915_READ(PLANE_CTL(pipe, 0));
  7623. if (!(val & PLANE_CTL_ENABLE))
  7624. goto error;
  7625. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7626. fourcc = skl_format_to_fourcc(pixel_format,
  7627. val & PLANE_CTL_ORDER_RGBX,
  7628. val & PLANE_CTL_ALPHA_MASK);
  7629. fb->pixel_format = fourcc;
  7630. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7631. tiling = val & PLANE_CTL_TILED_MASK;
  7632. switch (tiling) {
  7633. case PLANE_CTL_TILED_LINEAR:
  7634. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  7635. break;
  7636. case PLANE_CTL_TILED_X:
  7637. plane_config->tiling = I915_TILING_X;
  7638. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7639. break;
  7640. case PLANE_CTL_TILED_Y:
  7641. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  7642. break;
  7643. case PLANE_CTL_TILED_YF:
  7644. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  7645. break;
  7646. default:
  7647. MISSING_CASE(tiling);
  7648. goto error;
  7649. }
  7650. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7651. plane_config->base = base;
  7652. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7653. val = I915_READ(PLANE_SIZE(pipe, 0));
  7654. fb->height = ((val >> 16) & 0xfff) + 1;
  7655. fb->width = ((val >> 0) & 0x1fff) + 1;
  7656. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7657. stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
  7658. fb->pixel_format);
  7659. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7660. aligned_height = intel_fb_align_height(dev, fb->height,
  7661. fb->pixel_format,
  7662. fb->modifier[0]);
  7663. plane_config->size = fb->pitches[0] * aligned_height;
  7664. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7665. pipe_name(pipe), fb->width, fb->height,
  7666. fb->bits_per_pixel, base, fb->pitches[0],
  7667. plane_config->size);
  7668. plane_config->fb = intel_fb;
  7669. return;
  7670. error:
  7671. kfree(fb);
  7672. }
  7673. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7674. struct intel_crtc_state *pipe_config)
  7675. {
  7676. struct drm_device *dev = crtc->base.dev;
  7677. struct drm_i915_private *dev_priv = dev->dev_private;
  7678. uint32_t tmp;
  7679. tmp = I915_READ(PF_CTL(crtc->pipe));
  7680. if (tmp & PF_ENABLE) {
  7681. pipe_config->pch_pfit.enabled = true;
  7682. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7683. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7684. /* We currently do not free assignements of panel fitters on
  7685. * ivb/hsw (since we don't use the higher upscaling modes which
  7686. * differentiates them) so just WARN about this case for now. */
  7687. if (IS_GEN7(dev)) {
  7688. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7689. PF_PIPE_SEL_IVB(crtc->pipe));
  7690. }
  7691. }
  7692. }
  7693. static void
  7694. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7695. struct intel_initial_plane_config *plane_config)
  7696. {
  7697. struct drm_device *dev = crtc->base.dev;
  7698. struct drm_i915_private *dev_priv = dev->dev_private;
  7699. u32 val, base, offset;
  7700. int pipe = crtc->pipe;
  7701. int fourcc, pixel_format;
  7702. unsigned int aligned_height;
  7703. struct drm_framebuffer *fb;
  7704. struct intel_framebuffer *intel_fb;
  7705. val = I915_READ(DSPCNTR(pipe));
  7706. if (!(val & DISPLAY_PLANE_ENABLE))
  7707. return;
  7708. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7709. if (!intel_fb) {
  7710. DRM_DEBUG_KMS("failed to alloc fb\n");
  7711. return;
  7712. }
  7713. fb = &intel_fb->base;
  7714. if (INTEL_INFO(dev)->gen >= 4) {
  7715. if (val & DISPPLANE_TILED) {
  7716. plane_config->tiling = I915_TILING_X;
  7717. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7718. }
  7719. }
  7720. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7721. fourcc = i9xx_format_to_fourcc(pixel_format);
  7722. fb->pixel_format = fourcc;
  7723. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7724. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7725. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  7726. offset = I915_READ(DSPOFFSET(pipe));
  7727. } else {
  7728. if (plane_config->tiling)
  7729. offset = I915_READ(DSPTILEOFF(pipe));
  7730. else
  7731. offset = I915_READ(DSPLINOFF(pipe));
  7732. }
  7733. plane_config->base = base;
  7734. val = I915_READ(PIPESRC(pipe));
  7735. fb->width = ((val >> 16) & 0xfff) + 1;
  7736. fb->height = ((val >> 0) & 0xfff) + 1;
  7737. val = I915_READ(DSPSTRIDE(pipe));
  7738. fb->pitches[0] = val & 0xffffffc0;
  7739. aligned_height = intel_fb_align_height(dev, fb->height,
  7740. fb->pixel_format,
  7741. fb->modifier[0]);
  7742. plane_config->size = fb->pitches[0] * aligned_height;
  7743. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7744. pipe_name(pipe), fb->width, fb->height,
  7745. fb->bits_per_pixel, base, fb->pitches[0],
  7746. plane_config->size);
  7747. plane_config->fb = intel_fb;
  7748. }
  7749. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7750. struct intel_crtc_state *pipe_config)
  7751. {
  7752. struct drm_device *dev = crtc->base.dev;
  7753. struct drm_i915_private *dev_priv = dev->dev_private;
  7754. uint32_t tmp;
  7755. if (!intel_display_power_is_enabled(dev_priv,
  7756. POWER_DOMAIN_PIPE(crtc->pipe)))
  7757. return false;
  7758. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7759. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  7760. tmp = I915_READ(PIPECONF(crtc->pipe));
  7761. if (!(tmp & PIPECONF_ENABLE))
  7762. return false;
  7763. switch (tmp & PIPECONF_BPC_MASK) {
  7764. case PIPECONF_6BPC:
  7765. pipe_config->pipe_bpp = 18;
  7766. break;
  7767. case PIPECONF_8BPC:
  7768. pipe_config->pipe_bpp = 24;
  7769. break;
  7770. case PIPECONF_10BPC:
  7771. pipe_config->pipe_bpp = 30;
  7772. break;
  7773. case PIPECONF_12BPC:
  7774. pipe_config->pipe_bpp = 36;
  7775. break;
  7776. default:
  7777. break;
  7778. }
  7779. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7780. pipe_config->limited_color_range = true;
  7781. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7782. struct intel_shared_dpll *pll;
  7783. pipe_config->has_pch_encoder = true;
  7784. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7785. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7786. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7787. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7788. if (HAS_PCH_IBX(dev_priv->dev)) {
  7789. pipe_config->shared_dpll =
  7790. (enum intel_dpll_id) crtc->pipe;
  7791. } else {
  7792. tmp = I915_READ(PCH_DPLL_SEL);
  7793. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7794. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  7795. else
  7796. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  7797. }
  7798. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  7799. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  7800. &pipe_config->dpll_hw_state));
  7801. tmp = pipe_config->dpll_hw_state.dpll;
  7802. pipe_config->pixel_multiplier =
  7803. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7804. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7805. ironlake_pch_clock_get(crtc, pipe_config);
  7806. } else {
  7807. pipe_config->pixel_multiplier = 1;
  7808. }
  7809. intel_get_pipe_timings(crtc, pipe_config);
  7810. ironlake_get_pfit_config(crtc, pipe_config);
  7811. return true;
  7812. }
  7813. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7814. {
  7815. struct drm_device *dev = dev_priv->dev;
  7816. struct intel_crtc *crtc;
  7817. for_each_intel_crtc(dev, crtc)
  7818. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7819. pipe_name(crtc->pipe));
  7820. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7821. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7822. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7823. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7824. I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  7825. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7826. "CPU PWM1 enabled\n");
  7827. if (IS_HASWELL(dev))
  7828. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7829. "CPU PWM2 enabled\n");
  7830. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7831. "PCH PWM1 enabled\n");
  7832. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7833. "Utility pin enabled\n");
  7834. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7835. /*
  7836. * In theory we can still leave IRQs enabled, as long as only the HPD
  7837. * interrupts remain enabled. We used to check for that, but since it's
  7838. * gen-specific and since we only disable LCPLL after we fully disable
  7839. * the interrupts, the check below should be enough.
  7840. */
  7841. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7842. }
  7843. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7844. {
  7845. struct drm_device *dev = dev_priv->dev;
  7846. if (IS_HASWELL(dev))
  7847. return I915_READ(D_COMP_HSW);
  7848. else
  7849. return I915_READ(D_COMP_BDW);
  7850. }
  7851. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7852. {
  7853. struct drm_device *dev = dev_priv->dev;
  7854. if (IS_HASWELL(dev)) {
  7855. mutex_lock(&dev_priv->rps.hw_lock);
  7856. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7857. val))
  7858. DRM_ERROR("Failed to write to D_COMP\n");
  7859. mutex_unlock(&dev_priv->rps.hw_lock);
  7860. } else {
  7861. I915_WRITE(D_COMP_BDW, val);
  7862. POSTING_READ(D_COMP_BDW);
  7863. }
  7864. }
  7865. /*
  7866. * This function implements pieces of two sequences from BSpec:
  7867. * - Sequence for display software to disable LCPLL
  7868. * - Sequence for display software to allow package C8+
  7869. * The steps implemented here are just the steps that actually touch the LCPLL
  7870. * register. Callers should take care of disabling all the display engine
  7871. * functions, doing the mode unset, fixing interrupts, etc.
  7872. */
  7873. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7874. bool switch_to_fclk, bool allow_power_down)
  7875. {
  7876. uint32_t val;
  7877. assert_can_disable_lcpll(dev_priv);
  7878. val = I915_READ(LCPLL_CTL);
  7879. if (switch_to_fclk) {
  7880. val |= LCPLL_CD_SOURCE_FCLK;
  7881. I915_WRITE(LCPLL_CTL, val);
  7882. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  7883. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7884. DRM_ERROR("Switching to FCLK failed\n");
  7885. val = I915_READ(LCPLL_CTL);
  7886. }
  7887. val |= LCPLL_PLL_DISABLE;
  7888. I915_WRITE(LCPLL_CTL, val);
  7889. POSTING_READ(LCPLL_CTL);
  7890. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  7891. DRM_ERROR("LCPLL still locked\n");
  7892. val = hsw_read_dcomp(dev_priv);
  7893. val |= D_COMP_COMP_DISABLE;
  7894. hsw_write_dcomp(dev_priv, val);
  7895. ndelay(100);
  7896. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7897. 1))
  7898. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7899. if (allow_power_down) {
  7900. val = I915_READ(LCPLL_CTL);
  7901. val |= LCPLL_POWER_DOWN_ALLOW;
  7902. I915_WRITE(LCPLL_CTL, val);
  7903. POSTING_READ(LCPLL_CTL);
  7904. }
  7905. }
  7906. /*
  7907. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7908. * source.
  7909. */
  7910. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7911. {
  7912. uint32_t val;
  7913. val = I915_READ(LCPLL_CTL);
  7914. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7915. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7916. return;
  7917. /*
  7918. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7919. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7920. */
  7921. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7922. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7923. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7924. I915_WRITE(LCPLL_CTL, val);
  7925. POSTING_READ(LCPLL_CTL);
  7926. }
  7927. val = hsw_read_dcomp(dev_priv);
  7928. val |= D_COMP_COMP_FORCE;
  7929. val &= ~D_COMP_COMP_DISABLE;
  7930. hsw_write_dcomp(dev_priv, val);
  7931. val = I915_READ(LCPLL_CTL);
  7932. val &= ~LCPLL_PLL_DISABLE;
  7933. I915_WRITE(LCPLL_CTL, val);
  7934. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  7935. DRM_ERROR("LCPLL not locked yet\n");
  7936. if (val & LCPLL_CD_SOURCE_FCLK) {
  7937. val = I915_READ(LCPLL_CTL);
  7938. val &= ~LCPLL_CD_SOURCE_FCLK;
  7939. I915_WRITE(LCPLL_CTL, val);
  7940. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  7941. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7942. DRM_ERROR("Switching back to LCPLL failed\n");
  7943. }
  7944. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7945. intel_update_cdclk(dev_priv->dev);
  7946. }
  7947. /*
  7948. * Package states C8 and deeper are really deep PC states that can only be
  7949. * reached when all the devices on the system allow it, so even if the graphics
  7950. * device allows PC8+, it doesn't mean the system will actually get to these
  7951. * states. Our driver only allows PC8+ when going into runtime PM.
  7952. *
  7953. * The requirements for PC8+ are that all the outputs are disabled, the power
  7954. * well is disabled and most interrupts are disabled, and these are also
  7955. * requirements for runtime PM. When these conditions are met, we manually do
  7956. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7957. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7958. * hang the machine.
  7959. *
  7960. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7961. * the state of some registers, so when we come back from PC8+ we need to
  7962. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7963. * need to take care of the registers kept by RC6. Notice that this happens even
  7964. * if we don't put the device in PCI D3 state (which is what currently happens
  7965. * because of the runtime PM support).
  7966. *
  7967. * For more, read "Display Sequences for Package C8" on the hardware
  7968. * documentation.
  7969. */
  7970. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7971. {
  7972. struct drm_device *dev = dev_priv->dev;
  7973. uint32_t val;
  7974. DRM_DEBUG_KMS("Enabling package C8+\n");
  7975. if (HAS_PCH_LPT_LP(dev)) {
  7976. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7977. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7978. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7979. }
  7980. lpt_disable_clkout_dp(dev);
  7981. hsw_disable_lcpll(dev_priv, true, true);
  7982. }
  7983. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7984. {
  7985. struct drm_device *dev = dev_priv->dev;
  7986. uint32_t val;
  7987. DRM_DEBUG_KMS("Disabling package C8+\n");
  7988. hsw_restore_lcpll(dev_priv);
  7989. lpt_init_pch_refclk(dev);
  7990. if (HAS_PCH_LPT_LP(dev)) {
  7991. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7992. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7993. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7994. }
  7995. intel_prepare_ddi(dev);
  7996. }
  7997. static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  7998. {
  7999. struct drm_device *dev = old_state->dev;
  8000. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8001. broxton_set_cdclk(dev, req_cdclk);
  8002. }
  8003. /* compute the max rate for new configuration */
  8004. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8005. {
  8006. struct intel_crtc *intel_crtc;
  8007. struct intel_crtc_state *crtc_state;
  8008. int max_pixel_rate = 0;
  8009. for_each_intel_crtc(state->dev, intel_crtc) {
  8010. int pixel_rate;
  8011. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8012. if (IS_ERR(crtc_state))
  8013. return PTR_ERR(crtc_state);
  8014. if (!crtc_state->base.enable)
  8015. continue;
  8016. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8017. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8018. if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
  8019. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8020. max_pixel_rate = max(max_pixel_rate, pixel_rate);
  8021. }
  8022. return max_pixel_rate;
  8023. }
  8024. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8025. {
  8026. struct drm_i915_private *dev_priv = dev->dev_private;
  8027. uint32_t val, data;
  8028. int ret;
  8029. if (WARN((I915_READ(LCPLL_CTL) &
  8030. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8031. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8032. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8033. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8034. "trying to change cdclk frequency with cdclk not enabled\n"))
  8035. return;
  8036. mutex_lock(&dev_priv->rps.hw_lock);
  8037. ret = sandybridge_pcode_write(dev_priv,
  8038. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8039. mutex_unlock(&dev_priv->rps.hw_lock);
  8040. if (ret) {
  8041. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8042. return;
  8043. }
  8044. val = I915_READ(LCPLL_CTL);
  8045. val |= LCPLL_CD_SOURCE_FCLK;
  8046. I915_WRITE(LCPLL_CTL, val);
  8047. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  8048. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8049. DRM_ERROR("Switching to FCLK failed\n");
  8050. val = I915_READ(LCPLL_CTL);
  8051. val &= ~LCPLL_CLK_FREQ_MASK;
  8052. switch (cdclk) {
  8053. case 450000:
  8054. val |= LCPLL_CLK_FREQ_450;
  8055. data = 0;
  8056. break;
  8057. case 540000:
  8058. val |= LCPLL_CLK_FREQ_54O_BDW;
  8059. data = 1;
  8060. break;
  8061. case 337500:
  8062. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8063. data = 2;
  8064. break;
  8065. case 675000:
  8066. val |= LCPLL_CLK_FREQ_675_BDW;
  8067. data = 3;
  8068. break;
  8069. default:
  8070. WARN(1, "invalid cdclk frequency\n");
  8071. return;
  8072. }
  8073. I915_WRITE(LCPLL_CTL, val);
  8074. val = I915_READ(LCPLL_CTL);
  8075. val &= ~LCPLL_CD_SOURCE_FCLK;
  8076. I915_WRITE(LCPLL_CTL, val);
  8077. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  8078. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8079. DRM_ERROR("Switching back to LCPLL failed\n");
  8080. mutex_lock(&dev_priv->rps.hw_lock);
  8081. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8082. mutex_unlock(&dev_priv->rps.hw_lock);
  8083. intel_update_cdclk(dev);
  8084. WARN(cdclk != dev_priv->cdclk_freq,
  8085. "cdclk requested %d kHz but got %d kHz\n",
  8086. cdclk, dev_priv->cdclk_freq);
  8087. }
  8088. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8089. {
  8090. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8091. int max_pixclk = ilk_max_pixel_rate(state);
  8092. int cdclk;
  8093. /*
  8094. * FIXME should also account for plane ratio
  8095. * once 64bpp pixel formats are supported.
  8096. */
  8097. if (max_pixclk > 540000)
  8098. cdclk = 675000;
  8099. else if (max_pixclk > 450000)
  8100. cdclk = 540000;
  8101. else if (max_pixclk > 337500)
  8102. cdclk = 450000;
  8103. else
  8104. cdclk = 337500;
  8105. /*
  8106. * FIXME move the cdclk caclulation to
  8107. * compute_config() so we can fail gracegully.
  8108. */
  8109. if (cdclk > dev_priv->max_cdclk_freq) {
  8110. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8111. cdclk, dev_priv->max_cdclk_freq);
  8112. cdclk = dev_priv->max_cdclk_freq;
  8113. }
  8114. to_intel_atomic_state(state)->cdclk = cdclk;
  8115. return 0;
  8116. }
  8117. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8118. {
  8119. struct drm_device *dev = old_state->dev;
  8120. unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
  8121. broadwell_set_cdclk(dev, req_cdclk);
  8122. }
  8123. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8124. struct intel_crtc_state *crtc_state)
  8125. {
  8126. if (!intel_ddi_pll_select(crtc, crtc_state))
  8127. return -EINVAL;
  8128. crtc->lowfreq_avail = false;
  8129. return 0;
  8130. }
  8131. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8132. enum port port,
  8133. struct intel_crtc_state *pipe_config)
  8134. {
  8135. switch (port) {
  8136. case PORT_A:
  8137. pipe_config->ddi_pll_sel = SKL_DPLL0;
  8138. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8139. break;
  8140. case PORT_B:
  8141. pipe_config->ddi_pll_sel = SKL_DPLL1;
  8142. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8143. break;
  8144. case PORT_C:
  8145. pipe_config->ddi_pll_sel = SKL_DPLL2;
  8146. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8147. break;
  8148. default:
  8149. DRM_ERROR("Incorrect port type\n");
  8150. }
  8151. }
  8152. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8153. enum port port,
  8154. struct intel_crtc_state *pipe_config)
  8155. {
  8156. u32 temp, dpll_ctl1;
  8157. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8158. pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
  8159. switch (pipe_config->ddi_pll_sel) {
  8160. case SKL_DPLL0:
  8161. /*
  8162. * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
  8163. * of the shared DPLL framework and thus needs to be read out
  8164. * separately
  8165. */
  8166. dpll_ctl1 = I915_READ(DPLL_CTRL1);
  8167. pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
  8168. break;
  8169. case SKL_DPLL1:
  8170. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
  8171. break;
  8172. case SKL_DPLL2:
  8173. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
  8174. break;
  8175. case SKL_DPLL3:
  8176. pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
  8177. break;
  8178. }
  8179. }
  8180. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8181. enum port port,
  8182. struct intel_crtc_state *pipe_config)
  8183. {
  8184. pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8185. switch (pipe_config->ddi_pll_sel) {
  8186. case PORT_CLK_SEL_WRPLL1:
  8187. pipe_config->shared_dpll = DPLL_ID_WRPLL1;
  8188. break;
  8189. case PORT_CLK_SEL_WRPLL2:
  8190. pipe_config->shared_dpll = DPLL_ID_WRPLL2;
  8191. break;
  8192. }
  8193. }
  8194. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8195. struct intel_crtc_state *pipe_config)
  8196. {
  8197. struct drm_device *dev = crtc->base.dev;
  8198. struct drm_i915_private *dev_priv = dev->dev_private;
  8199. struct intel_shared_dpll *pll;
  8200. enum port port;
  8201. uint32_t tmp;
  8202. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8203. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8204. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  8205. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8206. else if (IS_BROXTON(dev))
  8207. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8208. else
  8209. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8210. if (pipe_config->shared_dpll >= 0) {
  8211. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  8212. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  8213. &pipe_config->dpll_hw_state));
  8214. }
  8215. /*
  8216. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8217. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8218. * the PCH transcoder is on.
  8219. */
  8220. if (INTEL_INFO(dev)->gen < 9 &&
  8221. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8222. pipe_config->has_pch_encoder = true;
  8223. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8224. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8225. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8226. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8227. }
  8228. }
  8229. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8230. struct intel_crtc_state *pipe_config)
  8231. {
  8232. struct drm_device *dev = crtc->base.dev;
  8233. struct drm_i915_private *dev_priv = dev->dev_private;
  8234. enum intel_display_power_domain pfit_domain;
  8235. uint32_t tmp;
  8236. if (!intel_display_power_is_enabled(dev_priv,
  8237. POWER_DOMAIN_PIPE(crtc->pipe)))
  8238. return false;
  8239. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8240. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8241. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8242. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8243. enum pipe trans_edp_pipe;
  8244. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8245. default:
  8246. WARN(1, "unknown pipe linked to edp transcoder\n");
  8247. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8248. case TRANS_DDI_EDP_INPUT_A_ON:
  8249. trans_edp_pipe = PIPE_A;
  8250. break;
  8251. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8252. trans_edp_pipe = PIPE_B;
  8253. break;
  8254. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8255. trans_edp_pipe = PIPE_C;
  8256. break;
  8257. }
  8258. if (trans_edp_pipe == crtc->pipe)
  8259. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8260. }
  8261. if (!intel_display_power_is_enabled(dev_priv,
  8262. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  8263. return false;
  8264. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8265. if (!(tmp & PIPECONF_ENABLE))
  8266. return false;
  8267. haswell_get_ddi_port_state(crtc, pipe_config);
  8268. intel_get_pipe_timings(crtc, pipe_config);
  8269. if (INTEL_INFO(dev)->gen >= 9) {
  8270. skl_init_scalers(dev, crtc, pipe_config);
  8271. }
  8272. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8273. if (INTEL_INFO(dev)->gen >= 9) {
  8274. pipe_config->scaler_state.scaler_id = -1;
  8275. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8276. }
  8277. if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
  8278. if (INTEL_INFO(dev)->gen >= 9)
  8279. skylake_get_pfit_config(crtc, pipe_config);
  8280. else
  8281. ironlake_get_pfit_config(crtc, pipe_config);
  8282. }
  8283. if (IS_HASWELL(dev))
  8284. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8285. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8286. if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
  8287. pipe_config->pixel_multiplier =
  8288. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8289. } else {
  8290. pipe_config->pixel_multiplier = 1;
  8291. }
  8292. return true;
  8293. }
  8294. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  8295. {
  8296. struct drm_device *dev = crtc->dev;
  8297. struct drm_i915_private *dev_priv = dev->dev_private;
  8298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8299. uint32_t cntl = 0, size = 0;
  8300. if (base) {
  8301. unsigned int width = intel_crtc->base.cursor->state->crtc_w;
  8302. unsigned int height = intel_crtc->base.cursor->state->crtc_h;
  8303. unsigned int stride = roundup_pow_of_two(width) * 4;
  8304. switch (stride) {
  8305. default:
  8306. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  8307. width, stride);
  8308. stride = 256;
  8309. /* fallthrough */
  8310. case 256:
  8311. case 512:
  8312. case 1024:
  8313. case 2048:
  8314. break;
  8315. }
  8316. cntl |= CURSOR_ENABLE |
  8317. CURSOR_GAMMA_ENABLE |
  8318. CURSOR_FORMAT_ARGB |
  8319. CURSOR_STRIDE(stride);
  8320. size = (height << 12) | width;
  8321. }
  8322. if (intel_crtc->cursor_cntl != 0 &&
  8323. (intel_crtc->cursor_base != base ||
  8324. intel_crtc->cursor_size != size ||
  8325. intel_crtc->cursor_cntl != cntl)) {
  8326. /* On these chipsets we can only modify the base/size/stride
  8327. * whilst the cursor is disabled.
  8328. */
  8329. I915_WRITE(CURCNTR(PIPE_A), 0);
  8330. POSTING_READ(CURCNTR(PIPE_A));
  8331. intel_crtc->cursor_cntl = 0;
  8332. }
  8333. if (intel_crtc->cursor_base != base) {
  8334. I915_WRITE(CURBASE(PIPE_A), base);
  8335. intel_crtc->cursor_base = base;
  8336. }
  8337. if (intel_crtc->cursor_size != size) {
  8338. I915_WRITE(CURSIZE, size);
  8339. intel_crtc->cursor_size = size;
  8340. }
  8341. if (intel_crtc->cursor_cntl != cntl) {
  8342. I915_WRITE(CURCNTR(PIPE_A), cntl);
  8343. POSTING_READ(CURCNTR(PIPE_A));
  8344. intel_crtc->cursor_cntl = cntl;
  8345. }
  8346. }
  8347. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  8348. {
  8349. struct drm_device *dev = crtc->dev;
  8350. struct drm_i915_private *dev_priv = dev->dev_private;
  8351. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8352. int pipe = intel_crtc->pipe;
  8353. uint32_t cntl;
  8354. cntl = 0;
  8355. if (base) {
  8356. cntl = MCURSOR_GAMMA_ENABLE;
  8357. switch (intel_crtc->base.cursor->state->crtc_w) {
  8358. case 64:
  8359. cntl |= CURSOR_MODE_64_ARGB_AX;
  8360. break;
  8361. case 128:
  8362. cntl |= CURSOR_MODE_128_ARGB_AX;
  8363. break;
  8364. case 256:
  8365. cntl |= CURSOR_MODE_256_ARGB_AX;
  8366. break;
  8367. default:
  8368. MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
  8369. return;
  8370. }
  8371. cntl |= pipe << 28; /* Connect to correct pipe */
  8372. if (HAS_DDI(dev))
  8373. cntl |= CURSOR_PIPE_CSC_ENABLE;
  8374. }
  8375. if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
  8376. cntl |= CURSOR_ROTATE_180;
  8377. if (intel_crtc->cursor_cntl != cntl) {
  8378. I915_WRITE(CURCNTR(pipe), cntl);
  8379. POSTING_READ(CURCNTR(pipe));
  8380. intel_crtc->cursor_cntl = cntl;
  8381. }
  8382. /* and commit changes on next vblank */
  8383. I915_WRITE(CURBASE(pipe), base);
  8384. POSTING_READ(CURBASE(pipe));
  8385. intel_crtc->cursor_base = base;
  8386. }
  8387. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  8388. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  8389. bool on)
  8390. {
  8391. struct drm_device *dev = crtc->dev;
  8392. struct drm_i915_private *dev_priv = dev->dev_private;
  8393. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8394. int pipe = intel_crtc->pipe;
  8395. struct drm_plane_state *cursor_state = crtc->cursor->state;
  8396. int x = cursor_state->crtc_x;
  8397. int y = cursor_state->crtc_y;
  8398. u32 base = 0, pos = 0;
  8399. if (on)
  8400. base = intel_crtc->cursor_addr;
  8401. if (x >= intel_crtc->config->pipe_src_w)
  8402. base = 0;
  8403. if (y >= intel_crtc->config->pipe_src_h)
  8404. base = 0;
  8405. if (x < 0) {
  8406. if (x + cursor_state->crtc_w <= 0)
  8407. base = 0;
  8408. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  8409. x = -x;
  8410. }
  8411. pos |= x << CURSOR_X_SHIFT;
  8412. if (y < 0) {
  8413. if (y + cursor_state->crtc_h <= 0)
  8414. base = 0;
  8415. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  8416. y = -y;
  8417. }
  8418. pos |= y << CURSOR_Y_SHIFT;
  8419. if (base == 0 && intel_crtc->cursor_base == 0)
  8420. return;
  8421. I915_WRITE(CURPOS(pipe), pos);
  8422. /* ILK+ do this automagically */
  8423. if (HAS_GMCH_DISPLAY(dev) &&
  8424. crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
  8425. base += (cursor_state->crtc_h *
  8426. cursor_state->crtc_w - 1) * 4;
  8427. }
  8428. if (IS_845G(dev) || IS_I865G(dev))
  8429. i845_update_cursor(crtc, base);
  8430. else
  8431. i9xx_update_cursor(crtc, base);
  8432. }
  8433. static bool cursor_size_ok(struct drm_device *dev,
  8434. uint32_t width, uint32_t height)
  8435. {
  8436. if (width == 0 || height == 0)
  8437. return false;
  8438. /*
  8439. * 845g/865g are special in that they are only limited by
  8440. * the width of their cursors, the height is arbitrary up to
  8441. * the precision of the register. Everything else requires
  8442. * square cursors, limited to a few power-of-two sizes.
  8443. */
  8444. if (IS_845G(dev) || IS_I865G(dev)) {
  8445. if ((width & 63) != 0)
  8446. return false;
  8447. if (width > (IS_845G(dev) ? 64 : 512))
  8448. return false;
  8449. if (height > 1023)
  8450. return false;
  8451. } else {
  8452. switch (width | height) {
  8453. case 256:
  8454. case 128:
  8455. if (IS_GEN2(dev))
  8456. return false;
  8457. case 64:
  8458. break;
  8459. default:
  8460. return false;
  8461. }
  8462. }
  8463. return true;
  8464. }
  8465. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  8466. u16 *blue, uint32_t start, uint32_t size)
  8467. {
  8468. int end = (start + size > 256) ? 256 : start + size, i;
  8469. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8470. for (i = start; i < end; i++) {
  8471. intel_crtc->lut_r[i] = red[i] >> 8;
  8472. intel_crtc->lut_g[i] = green[i] >> 8;
  8473. intel_crtc->lut_b[i] = blue[i] >> 8;
  8474. }
  8475. intel_crtc_load_lut(crtc);
  8476. }
  8477. /* VESA 640x480x72Hz mode to set on the pipe */
  8478. static struct drm_display_mode load_detect_mode = {
  8479. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  8480. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  8481. };
  8482. struct drm_framebuffer *
  8483. __intel_framebuffer_create(struct drm_device *dev,
  8484. struct drm_mode_fb_cmd2 *mode_cmd,
  8485. struct drm_i915_gem_object *obj)
  8486. {
  8487. struct intel_framebuffer *intel_fb;
  8488. int ret;
  8489. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8490. if (!intel_fb)
  8491. return ERR_PTR(-ENOMEM);
  8492. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  8493. if (ret)
  8494. goto err;
  8495. return &intel_fb->base;
  8496. err:
  8497. kfree(intel_fb);
  8498. return ERR_PTR(ret);
  8499. }
  8500. static struct drm_framebuffer *
  8501. intel_framebuffer_create(struct drm_device *dev,
  8502. struct drm_mode_fb_cmd2 *mode_cmd,
  8503. struct drm_i915_gem_object *obj)
  8504. {
  8505. struct drm_framebuffer *fb;
  8506. int ret;
  8507. ret = i915_mutex_lock_interruptible(dev);
  8508. if (ret)
  8509. return ERR_PTR(ret);
  8510. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  8511. mutex_unlock(&dev->struct_mutex);
  8512. return fb;
  8513. }
  8514. static u32
  8515. intel_framebuffer_pitch_for_width(int width, int bpp)
  8516. {
  8517. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  8518. return ALIGN(pitch, 64);
  8519. }
  8520. static u32
  8521. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  8522. {
  8523. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  8524. return PAGE_ALIGN(pitch * mode->vdisplay);
  8525. }
  8526. static struct drm_framebuffer *
  8527. intel_framebuffer_create_for_mode(struct drm_device *dev,
  8528. struct drm_display_mode *mode,
  8529. int depth, int bpp)
  8530. {
  8531. struct drm_framebuffer *fb;
  8532. struct drm_i915_gem_object *obj;
  8533. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  8534. obj = i915_gem_alloc_object(dev,
  8535. intel_framebuffer_size_for_mode(mode, bpp));
  8536. if (obj == NULL)
  8537. return ERR_PTR(-ENOMEM);
  8538. mode_cmd.width = mode->hdisplay;
  8539. mode_cmd.height = mode->vdisplay;
  8540. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  8541. bpp);
  8542. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  8543. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  8544. if (IS_ERR(fb))
  8545. drm_gem_object_unreference_unlocked(&obj->base);
  8546. return fb;
  8547. }
  8548. static struct drm_framebuffer *
  8549. mode_fits_in_fbdev(struct drm_device *dev,
  8550. struct drm_display_mode *mode)
  8551. {
  8552. #ifdef CONFIG_DRM_FBDEV_EMULATION
  8553. struct drm_i915_private *dev_priv = dev->dev_private;
  8554. struct drm_i915_gem_object *obj;
  8555. struct drm_framebuffer *fb;
  8556. if (!dev_priv->fbdev)
  8557. return NULL;
  8558. if (!dev_priv->fbdev->fb)
  8559. return NULL;
  8560. obj = dev_priv->fbdev->fb->obj;
  8561. BUG_ON(!obj);
  8562. fb = &dev_priv->fbdev->fb->base;
  8563. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  8564. fb->bits_per_pixel))
  8565. return NULL;
  8566. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  8567. return NULL;
  8568. return fb;
  8569. #else
  8570. return NULL;
  8571. #endif
  8572. }
  8573. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  8574. struct drm_crtc *crtc,
  8575. struct drm_display_mode *mode,
  8576. struct drm_framebuffer *fb,
  8577. int x, int y)
  8578. {
  8579. struct drm_plane_state *plane_state;
  8580. int hdisplay, vdisplay;
  8581. int ret;
  8582. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  8583. if (IS_ERR(plane_state))
  8584. return PTR_ERR(plane_state);
  8585. if (mode)
  8586. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  8587. else
  8588. hdisplay = vdisplay = 0;
  8589. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  8590. if (ret)
  8591. return ret;
  8592. drm_atomic_set_fb_for_plane(plane_state, fb);
  8593. plane_state->crtc_x = 0;
  8594. plane_state->crtc_y = 0;
  8595. plane_state->crtc_w = hdisplay;
  8596. plane_state->crtc_h = vdisplay;
  8597. plane_state->src_x = x << 16;
  8598. plane_state->src_y = y << 16;
  8599. plane_state->src_w = hdisplay << 16;
  8600. plane_state->src_h = vdisplay << 16;
  8601. return 0;
  8602. }
  8603. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  8604. struct drm_display_mode *mode,
  8605. struct intel_load_detect_pipe *old,
  8606. struct drm_modeset_acquire_ctx *ctx)
  8607. {
  8608. struct intel_crtc *intel_crtc;
  8609. struct intel_encoder *intel_encoder =
  8610. intel_attached_encoder(connector);
  8611. struct drm_crtc *possible_crtc;
  8612. struct drm_encoder *encoder = &intel_encoder->base;
  8613. struct drm_crtc *crtc = NULL;
  8614. struct drm_device *dev = encoder->dev;
  8615. struct drm_framebuffer *fb;
  8616. struct drm_mode_config *config = &dev->mode_config;
  8617. struct drm_atomic_state *state = NULL;
  8618. struct drm_connector_state *connector_state;
  8619. struct intel_crtc_state *crtc_state;
  8620. int ret, i = -1;
  8621. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8622. connector->base.id, connector->name,
  8623. encoder->base.id, encoder->name);
  8624. retry:
  8625. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  8626. if (ret)
  8627. goto fail;
  8628. /*
  8629. * Algorithm gets a little messy:
  8630. *
  8631. * - if the connector already has an assigned crtc, use it (but make
  8632. * sure it's on first)
  8633. *
  8634. * - try to find the first unused crtc that can drive this connector,
  8635. * and use that if we find one
  8636. */
  8637. /* See if we already have a CRTC for this connector */
  8638. if (encoder->crtc) {
  8639. crtc = encoder->crtc;
  8640. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8641. if (ret)
  8642. goto fail;
  8643. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8644. if (ret)
  8645. goto fail;
  8646. old->dpms_mode = connector->dpms;
  8647. old->load_detect_temp = false;
  8648. /* Make sure the crtc and connector are running */
  8649. if (connector->dpms != DRM_MODE_DPMS_ON)
  8650. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  8651. return true;
  8652. }
  8653. /* Find an unused one (if possible) */
  8654. for_each_crtc(dev, possible_crtc) {
  8655. i++;
  8656. if (!(encoder->possible_crtcs & (1 << i)))
  8657. continue;
  8658. if (possible_crtc->state->enable)
  8659. continue;
  8660. crtc = possible_crtc;
  8661. break;
  8662. }
  8663. /*
  8664. * If we didn't find an unused CRTC, don't use any.
  8665. */
  8666. if (!crtc) {
  8667. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8668. goto fail;
  8669. }
  8670. ret = drm_modeset_lock(&crtc->mutex, ctx);
  8671. if (ret)
  8672. goto fail;
  8673. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8674. if (ret)
  8675. goto fail;
  8676. intel_crtc = to_intel_crtc(crtc);
  8677. old->dpms_mode = connector->dpms;
  8678. old->load_detect_temp = true;
  8679. old->release_fb = NULL;
  8680. state = drm_atomic_state_alloc(dev);
  8681. if (!state)
  8682. return false;
  8683. state->acquire_ctx = ctx;
  8684. connector_state = drm_atomic_get_connector_state(state, connector);
  8685. if (IS_ERR(connector_state)) {
  8686. ret = PTR_ERR(connector_state);
  8687. goto fail;
  8688. }
  8689. connector_state->crtc = crtc;
  8690. connector_state->best_encoder = &intel_encoder->base;
  8691. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8692. if (IS_ERR(crtc_state)) {
  8693. ret = PTR_ERR(crtc_state);
  8694. goto fail;
  8695. }
  8696. crtc_state->base.active = crtc_state->base.enable = true;
  8697. if (!mode)
  8698. mode = &load_detect_mode;
  8699. /* We need a framebuffer large enough to accommodate all accesses
  8700. * that the plane may generate whilst we perform load detection.
  8701. * We can not rely on the fbcon either being present (we get called
  8702. * during its initialisation to detect all boot displays, or it may
  8703. * not even exist) or that it is large enough to satisfy the
  8704. * requested mode.
  8705. */
  8706. fb = mode_fits_in_fbdev(dev, mode);
  8707. if (fb == NULL) {
  8708. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8709. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8710. old->release_fb = fb;
  8711. } else
  8712. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8713. if (IS_ERR(fb)) {
  8714. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8715. goto fail;
  8716. }
  8717. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8718. if (ret)
  8719. goto fail;
  8720. drm_mode_copy(&crtc_state->base.mode, mode);
  8721. if (drm_atomic_commit(state)) {
  8722. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8723. if (old->release_fb)
  8724. old->release_fb->funcs->destroy(old->release_fb);
  8725. goto fail;
  8726. }
  8727. crtc->primary->crtc = crtc;
  8728. /* let the connector get through one full cycle before testing */
  8729. intel_wait_for_vblank(dev, intel_crtc->pipe);
  8730. return true;
  8731. fail:
  8732. drm_atomic_state_free(state);
  8733. state = NULL;
  8734. if (ret == -EDEADLK) {
  8735. drm_modeset_backoff(ctx);
  8736. goto retry;
  8737. }
  8738. return false;
  8739. }
  8740. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8741. struct intel_load_detect_pipe *old,
  8742. struct drm_modeset_acquire_ctx *ctx)
  8743. {
  8744. struct drm_device *dev = connector->dev;
  8745. struct intel_encoder *intel_encoder =
  8746. intel_attached_encoder(connector);
  8747. struct drm_encoder *encoder = &intel_encoder->base;
  8748. struct drm_crtc *crtc = encoder->crtc;
  8749. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8750. struct drm_atomic_state *state;
  8751. struct drm_connector_state *connector_state;
  8752. struct intel_crtc_state *crtc_state;
  8753. int ret;
  8754. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8755. connector->base.id, connector->name,
  8756. encoder->base.id, encoder->name);
  8757. if (old->load_detect_temp) {
  8758. state = drm_atomic_state_alloc(dev);
  8759. if (!state)
  8760. goto fail;
  8761. state->acquire_ctx = ctx;
  8762. connector_state = drm_atomic_get_connector_state(state, connector);
  8763. if (IS_ERR(connector_state))
  8764. goto fail;
  8765. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8766. if (IS_ERR(crtc_state))
  8767. goto fail;
  8768. connector_state->best_encoder = NULL;
  8769. connector_state->crtc = NULL;
  8770. crtc_state->base.enable = crtc_state->base.active = false;
  8771. ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
  8772. 0, 0);
  8773. if (ret)
  8774. goto fail;
  8775. ret = drm_atomic_commit(state);
  8776. if (ret)
  8777. goto fail;
  8778. if (old->release_fb) {
  8779. drm_framebuffer_unregister_private(old->release_fb);
  8780. drm_framebuffer_unreference(old->release_fb);
  8781. }
  8782. return;
  8783. }
  8784. /* Switch crtc and encoder back off if necessary */
  8785. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  8786. connector->funcs->dpms(connector, old->dpms_mode);
  8787. return;
  8788. fail:
  8789. DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
  8790. drm_atomic_state_free(state);
  8791. }
  8792. static int i9xx_pll_refclk(struct drm_device *dev,
  8793. const struct intel_crtc_state *pipe_config)
  8794. {
  8795. struct drm_i915_private *dev_priv = dev->dev_private;
  8796. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8797. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8798. return dev_priv->vbt.lvds_ssc_freq;
  8799. else if (HAS_PCH_SPLIT(dev))
  8800. return 120000;
  8801. else if (!IS_GEN2(dev))
  8802. return 96000;
  8803. else
  8804. return 48000;
  8805. }
  8806. /* Returns the clock of the currently programmed mode of the given pipe. */
  8807. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8808. struct intel_crtc_state *pipe_config)
  8809. {
  8810. struct drm_device *dev = crtc->base.dev;
  8811. struct drm_i915_private *dev_priv = dev->dev_private;
  8812. int pipe = pipe_config->cpu_transcoder;
  8813. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8814. u32 fp;
  8815. intel_clock_t clock;
  8816. int port_clock;
  8817. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8818. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8819. fp = pipe_config->dpll_hw_state.fp0;
  8820. else
  8821. fp = pipe_config->dpll_hw_state.fp1;
  8822. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8823. if (IS_PINEVIEW(dev)) {
  8824. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8825. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8826. } else {
  8827. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8828. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8829. }
  8830. if (!IS_GEN2(dev)) {
  8831. if (IS_PINEVIEW(dev))
  8832. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8833. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8834. else
  8835. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8836. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8837. switch (dpll & DPLL_MODE_MASK) {
  8838. case DPLLB_MODE_DAC_SERIAL:
  8839. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8840. 5 : 10;
  8841. break;
  8842. case DPLLB_MODE_LVDS:
  8843. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8844. 7 : 14;
  8845. break;
  8846. default:
  8847. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8848. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8849. return;
  8850. }
  8851. if (IS_PINEVIEW(dev))
  8852. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8853. else
  8854. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8855. } else {
  8856. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  8857. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8858. if (is_lvds) {
  8859. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8860. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8861. if (lvds & LVDS_CLKB_POWER_UP)
  8862. clock.p2 = 7;
  8863. else
  8864. clock.p2 = 14;
  8865. } else {
  8866. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8867. clock.p1 = 2;
  8868. else {
  8869. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8870. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8871. }
  8872. if (dpll & PLL_P2_DIVIDE_BY_4)
  8873. clock.p2 = 4;
  8874. else
  8875. clock.p2 = 2;
  8876. }
  8877. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8878. }
  8879. /*
  8880. * This value includes pixel_multiplier. We will use
  8881. * port_clock to compute adjusted_mode.crtc_clock in the
  8882. * encoder's get_config() function.
  8883. */
  8884. pipe_config->port_clock = port_clock;
  8885. }
  8886. int intel_dotclock_calculate(int link_freq,
  8887. const struct intel_link_m_n *m_n)
  8888. {
  8889. /*
  8890. * The calculation for the data clock is:
  8891. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8892. * But we want to avoid losing precison if possible, so:
  8893. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8894. *
  8895. * and the link clock is simpler:
  8896. * link_clock = (m * link_clock) / n
  8897. */
  8898. if (!m_n->link_n)
  8899. return 0;
  8900. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8901. }
  8902. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8903. struct intel_crtc_state *pipe_config)
  8904. {
  8905. struct drm_device *dev = crtc->base.dev;
  8906. /* read out port_clock from the DPLL */
  8907. i9xx_crtc_clock_get(crtc, pipe_config);
  8908. /*
  8909. * This value does not include pixel_multiplier.
  8910. * We will check that port_clock and adjusted_mode.crtc_clock
  8911. * agree once we know their relationship in the encoder's
  8912. * get_config() function.
  8913. */
  8914. pipe_config->base.adjusted_mode.crtc_clock =
  8915. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  8916. &pipe_config->fdi_m_n);
  8917. }
  8918. /** Returns the currently programmed mode of the given pipe. */
  8919. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8920. struct drm_crtc *crtc)
  8921. {
  8922. struct drm_i915_private *dev_priv = dev->dev_private;
  8923. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8924. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8925. struct drm_display_mode *mode;
  8926. struct intel_crtc_state pipe_config;
  8927. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8928. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8929. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8930. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8931. enum pipe pipe = intel_crtc->pipe;
  8932. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8933. if (!mode)
  8934. return NULL;
  8935. /*
  8936. * Construct a pipe_config sufficient for getting the clock info
  8937. * back out of crtc_clock_get.
  8938. *
  8939. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8940. * to use a real value here instead.
  8941. */
  8942. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  8943. pipe_config.pixel_multiplier = 1;
  8944. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8945. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8946. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8947. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  8948. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  8949. mode->hdisplay = (htot & 0xffff) + 1;
  8950. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8951. mode->hsync_start = (hsync & 0xffff) + 1;
  8952. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8953. mode->vdisplay = (vtot & 0xffff) + 1;
  8954. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8955. mode->vsync_start = (vsync & 0xffff) + 1;
  8956. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8957. drm_mode_set_name(mode);
  8958. return mode;
  8959. }
  8960. void intel_mark_busy(struct drm_device *dev)
  8961. {
  8962. struct drm_i915_private *dev_priv = dev->dev_private;
  8963. if (dev_priv->mm.busy)
  8964. return;
  8965. intel_runtime_pm_get(dev_priv);
  8966. i915_update_gfx_val(dev_priv);
  8967. if (INTEL_INFO(dev)->gen >= 6)
  8968. gen6_rps_busy(dev_priv);
  8969. dev_priv->mm.busy = true;
  8970. }
  8971. void intel_mark_idle(struct drm_device *dev)
  8972. {
  8973. struct drm_i915_private *dev_priv = dev->dev_private;
  8974. if (!dev_priv->mm.busy)
  8975. return;
  8976. dev_priv->mm.busy = false;
  8977. if (INTEL_INFO(dev)->gen >= 6)
  8978. gen6_rps_idle(dev->dev_private);
  8979. intel_runtime_pm_put(dev_priv);
  8980. }
  8981. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8982. {
  8983. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8984. struct drm_device *dev = crtc->dev;
  8985. struct intel_unpin_work *work;
  8986. spin_lock_irq(&dev->event_lock);
  8987. work = intel_crtc->unpin_work;
  8988. intel_crtc->unpin_work = NULL;
  8989. spin_unlock_irq(&dev->event_lock);
  8990. if (work) {
  8991. cancel_work_sync(&work->work);
  8992. kfree(work);
  8993. }
  8994. drm_crtc_cleanup(crtc);
  8995. kfree(intel_crtc);
  8996. }
  8997. static void intel_unpin_work_fn(struct work_struct *__work)
  8998. {
  8999. struct intel_unpin_work *work =
  9000. container_of(__work, struct intel_unpin_work, work);
  9001. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9002. struct drm_device *dev = crtc->base.dev;
  9003. struct drm_plane *primary = crtc->base.primary;
  9004. mutex_lock(&dev->struct_mutex);
  9005. intel_unpin_fb_obj(work->old_fb, primary->state);
  9006. drm_gem_object_unreference(&work->pending_flip_obj->base);
  9007. if (work->flip_queued_req)
  9008. i915_gem_request_assign(&work->flip_queued_req, NULL);
  9009. mutex_unlock(&dev->struct_mutex);
  9010. intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
  9011. drm_framebuffer_unreference(work->old_fb);
  9012. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9013. atomic_dec(&crtc->unpin_work_count);
  9014. kfree(work);
  9015. }
  9016. static void do_intel_finish_page_flip(struct drm_device *dev,
  9017. struct drm_crtc *crtc)
  9018. {
  9019. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9020. struct intel_unpin_work *work;
  9021. unsigned long flags;
  9022. /* Ignore early vblank irqs */
  9023. if (intel_crtc == NULL)
  9024. return;
  9025. /*
  9026. * This is called both by irq handlers and the reset code (to complete
  9027. * lost pageflips) so needs the full irqsave spinlocks.
  9028. */
  9029. spin_lock_irqsave(&dev->event_lock, flags);
  9030. work = intel_crtc->unpin_work;
  9031. /* Ensure we don't miss a work->pending update ... */
  9032. smp_rmb();
  9033. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  9034. spin_unlock_irqrestore(&dev->event_lock, flags);
  9035. return;
  9036. }
  9037. page_flip_completed(intel_crtc);
  9038. spin_unlock_irqrestore(&dev->event_lock, flags);
  9039. }
  9040. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  9041. {
  9042. struct drm_i915_private *dev_priv = dev->dev_private;
  9043. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9044. do_intel_finish_page_flip(dev, crtc);
  9045. }
  9046. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  9047. {
  9048. struct drm_i915_private *dev_priv = dev->dev_private;
  9049. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  9050. do_intel_finish_page_flip(dev, crtc);
  9051. }
  9052. /* Is 'a' after or equal to 'b'? */
  9053. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9054. {
  9055. return !((a - b) & 0x80000000);
  9056. }
  9057. static bool page_flip_finished(struct intel_crtc *crtc)
  9058. {
  9059. struct drm_device *dev = crtc->base.dev;
  9060. struct drm_i915_private *dev_priv = dev->dev_private;
  9061. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  9062. crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  9063. return true;
  9064. /*
  9065. * The relevant registers doen't exist on pre-ctg.
  9066. * As the flip done interrupt doesn't trigger for mmio
  9067. * flips on gmch platforms, a flip count check isn't
  9068. * really needed there. But since ctg has the registers,
  9069. * include it in the check anyway.
  9070. */
  9071. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  9072. return true;
  9073. /*
  9074. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9075. * used the same base address. In that case the mmio flip might
  9076. * have completed, but the CS hasn't even executed the flip yet.
  9077. *
  9078. * A flip count check isn't enough as the CS might have updated
  9079. * the base address just after start of vblank, but before we
  9080. * managed to process the interrupt. This means we'd complete the
  9081. * CS flip too soon.
  9082. *
  9083. * Combining both checks should get us a good enough result. It may
  9084. * still happen that the CS flip has been executed, but has not
  9085. * yet actually completed. But in case the base address is the same
  9086. * anyway, we don't really care.
  9087. */
  9088. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9089. crtc->unpin_work->gtt_offset &&
  9090. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9091. crtc->unpin_work->flip_count);
  9092. }
  9093. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  9094. {
  9095. struct drm_i915_private *dev_priv = dev->dev_private;
  9096. struct intel_crtc *intel_crtc =
  9097. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  9098. unsigned long flags;
  9099. /*
  9100. * This is called both by irq handlers and the reset code (to complete
  9101. * lost pageflips) so needs the full irqsave spinlocks.
  9102. *
  9103. * NB: An MMIO update of the plane base pointer will also
  9104. * generate a page-flip completion irq, i.e. every modeset
  9105. * is also accompanied by a spurious intel_prepare_page_flip().
  9106. */
  9107. spin_lock_irqsave(&dev->event_lock, flags);
  9108. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  9109. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  9110. spin_unlock_irqrestore(&dev->event_lock, flags);
  9111. }
  9112. static inline void intel_mark_page_flip_active(struct intel_unpin_work *work)
  9113. {
  9114. /* Ensure that the work item is consistent when activating it ... */
  9115. smp_wmb();
  9116. atomic_set(&work->pending, INTEL_FLIP_PENDING);
  9117. /* and that it is marked active as soon as the irq could fire. */
  9118. smp_wmb();
  9119. }
  9120. static int intel_gen2_queue_flip(struct drm_device *dev,
  9121. struct drm_crtc *crtc,
  9122. struct drm_framebuffer *fb,
  9123. struct drm_i915_gem_object *obj,
  9124. struct drm_i915_gem_request *req,
  9125. uint32_t flags)
  9126. {
  9127. struct intel_engine_cs *ring = req->ring;
  9128. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9129. u32 flip_mask;
  9130. int ret;
  9131. ret = intel_ring_begin(req, 6);
  9132. if (ret)
  9133. return ret;
  9134. /* Can't queue multiple flips, so wait for the previous
  9135. * one to finish before executing the next.
  9136. */
  9137. if (intel_crtc->plane)
  9138. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9139. else
  9140. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9141. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9142. intel_ring_emit(ring, MI_NOOP);
  9143. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9144. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9145. intel_ring_emit(ring, fb->pitches[0]);
  9146. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9147. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9148. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9149. return 0;
  9150. }
  9151. static int intel_gen3_queue_flip(struct drm_device *dev,
  9152. struct drm_crtc *crtc,
  9153. struct drm_framebuffer *fb,
  9154. struct drm_i915_gem_object *obj,
  9155. struct drm_i915_gem_request *req,
  9156. uint32_t flags)
  9157. {
  9158. struct intel_engine_cs *ring = req->ring;
  9159. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9160. u32 flip_mask;
  9161. int ret;
  9162. ret = intel_ring_begin(req, 6);
  9163. if (ret)
  9164. return ret;
  9165. if (intel_crtc->plane)
  9166. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9167. else
  9168. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9169. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9170. intel_ring_emit(ring, MI_NOOP);
  9171. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9172. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9173. intel_ring_emit(ring, fb->pitches[0]);
  9174. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9175. intel_ring_emit(ring, MI_NOOP);
  9176. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9177. return 0;
  9178. }
  9179. static int intel_gen4_queue_flip(struct drm_device *dev,
  9180. struct drm_crtc *crtc,
  9181. struct drm_framebuffer *fb,
  9182. struct drm_i915_gem_object *obj,
  9183. struct drm_i915_gem_request *req,
  9184. uint32_t flags)
  9185. {
  9186. struct intel_engine_cs *ring = req->ring;
  9187. struct drm_i915_private *dev_priv = dev->dev_private;
  9188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9189. uint32_t pf, pipesrc;
  9190. int ret;
  9191. ret = intel_ring_begin(req, 4);
  9192. if (ret)
  9193. return ret;
  9194. /* i965+ uses the linear or tiled offsets from the
  9195. * Display Registers (which do not change across a page-flip)
  9196. * so we need only reprogram the base address.
  9197. */
  9198. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9199. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9200. intel_ring_emit(ring, fb->pitches[0]);
  9201. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  9202. obj->tiling_mode);
  9203. /* XXX Enabling the panel-fitter across page-flip is so far
  9204. * untested on non-native modes, so ignore it for now.
  9205. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9206. */
  9207. pf = 0;
  9208. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9209. intel_ring_emit(ring, pf | pipesrc);
  9210. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9211. return 0;
  9212. }
  9213. static int intel_gen6_queue_flip(struct drm_device *dev,
  9214. struct drm_crtc *crtc,
  9215. struct drm_framebuffer *fb,
  9216. struct drm_i915_gem_object *obj,
  9217. struct drm_i915_gem_request *req,
  9218. uint32_t flags)
  9219. {
  9220. struct intel_engine_cs *ring = req->ring;
  9221. struct drm_i915_private *dev_priv = dev->dev_private;
  9222. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9223. uint32_t pf, pipesrc;
  9224. int ret;
  9225. ret = intel_ring_begin(req, 4);
  9226. if (ret)
  9227. return ret;
  9228. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9229. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9230. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  9231. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9232. /* Contrary to the suggestions in the documentation,
  9233. * "Enable Panel Fitter" does not seem to be required when page
  9234. * flipping with a non-native mode, and worse causes a normal
  9235. * modeset to fail.
  9236. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9237. */
  9238. pf = 0;
  9239. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9240. intel_ring_emit(ring, pf | pipesrc);
  9241. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9242. return 0;
  9243. }
  9244. static int intel_gen7_queue_flip(struct drm_device *dev,
  9245. struct drm_crtc *crtc,
  9246. struct drm_framebuffer *fb,
  9247. struct drm_i915_gem_object *obj,
  9248. struct drm_i915_gem_request *req,
  9249. uint32_t flags)
  9250. {
  9251. struct intel_engine_cs *ring = req->ring;
  9252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9253. uint32_t plane_bit = 0;
  9254. int len, ret;
  9255. switch (intel_crtc->plane) {
  9256. case PLANE_A:
  9257. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9258. break;
  9259. case PLANE_B:
  9260. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9261. break;
  9262. case PLANE_C:
  9263. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9264. break;
  9265. default:
  9266. WARN_ONCE(1, "unknown plane in flip command\n");
  9267. return -ENODEV;
  9268. }
  9269. len = 4;
  9270. if (ring->id == RCS) {
  9271. len += 6;
  9272. /*
  9273. * On Gen 8, SRM is now taking an extra dword to accommodate
  9274. * 48bits addresses, and we need a NOOP for the batch size to
  9275. * stay even.
  9276. */
  9277. if (IS_GEN8(dev))
  9278. len += 2;
  9279. }
  9280. /*
  9281. * BSpec MI_DISPLAY_FLIP for IVB:
  9282. * "The full packet must be contained within the same cache line."
  9283. *
  9284. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9285. * cacheline, if we ever start emitting more commands before
  9286. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9287. * then do the cacheline alignment, and finally emit the
  9288. * MI_DISPLAY_FLIP.
  9289. */
  9290. ret = intel_ring_cacheline_align(req);
  9291. if (ret)
  9292. return ret;
  9293. ret = intel_ring_begin(req, len);
  9294. if (ret)
  9295. return ret;
  9296. /* Unmask the flip-done completion message. Note that the bspec says that
  9297. * we should do this for both the BCS and RCS, and that we must not unmask
  9298. * more than one flip event at any time (or ensure that one flip message
  9299. * can be sent by waiting for flip-done prior to queueing new flips).
  9300. * Experimentation says that BCS works despite DERRMR masking all
  9301. * flip-done completion events and that unmasking all planes at once
  9302. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9303. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9304. */
  9305. if (ring->id == RCS) {
  9306. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9307. intel_ring_emit_reg(ring, DERRMR);
  9308. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9309. DERRMR_PIPEB_PRI_FLIP_DONE |
  9310. DERRMR_PIPEC_PRI_FLIP_DONE));
  9311. if (IS_GEN8(dev))
  9312. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9313. MI_SRM_LRM_GLOBAL_GTT);
  9314. else
  9315. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9316. MI_SRM_LRM_GLOBAL_GTT);
  9317. intel_ring_emit_reg(ring, DERRMR);
  9318. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  9319. if (IS_GEN8(dev)) {
  9320. intel_ring_emit(ring, 0);
  9321. intel_ring_emit(ring, MI_NOOP);
  9322. }
  9323. }
  9324. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  9325. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  9326. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  9327. intel_ring_emit(ring, (MI_NOOP));
  9328. intel_mark_page_flip_active(intel_crtc->unpin_work);
  9329. return 0;
  9330. }
  9331. static bool use_mmio_flip(struct intel_engine_cs *ring,
  9332. struct drm_i915_gem_object *obj)
  9333. {
  9334. /*
  9335. * This is not being used for older platforms, because
  9336. * non-availability of flip done interrupt forces us to use
  9337. * CS flips. Older platforms derive flip done using some clever
  9338. * tricks involving the flip_pending status bits and vblank irqs.
  9339. * So using MMIO flips there would disrupt this mechanism.
  9340. */
  9341. if (ring == NULL)
  9342. return true;
  9343. if (INTEL_INFO(ring->dev)->gen < 5)
  9344. return false;
  9345. if (i915.use_mmio_flip < 0)
  9346. return false;
  9347. else if (i915.use_mmio_flip > 0)
  9348. return true;
  9349. else if (i915.enable_execlists)
  9350. return true;
  9351. else
  9352. return ring != i915_gem_request_get_ring(obj->last_write_req);
  9353. }
  9354. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  9355. unsigned int rotation,
  9356. struct intel_unpin_work *work)
  9357. {
  9358. struct drm_device *dev = intel_crtc->base.dev;
  9359. struct drm_i915_private *dev_priv = dev->dev_private;
  9360. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  9361. const enum pipe pipe = intel_crtc->pipe;
  9362. u32 ctl, stride, tile_height;
  9363. ctl = I915_READ(PLANE_CTL(pipe, 0));
  9364. ctl &= ~PLANE_CTL_TILED_MASK;
  9365. switch (fb->modifier[0]) {
  9366. case DRM_FORMAT_MOD_NONE:
  9367. break;
  9368. case I915_FORMAT_MOD_X_TILED:
  9369. ctl |= PLANE_CTL_TILED_X;
  9370. break;
  9371. case I915_FORMAT_MOD_Y_TILED:
  9372. ctl |= PLANE_CTL_TILED_Y;
  9373. break;
  9374. case I915_FORMAT_MOD_Yf_TILED:
  9375. ctl |= PLANE_CTL_TILED_YF;
  9376. break;
  9377. default:
  9378. MISSING_CASE(fb->modifier[0]);
  9379. }
  9380. /*
  9381. * The stride is either expressed as a multiple of 64 bytes chunks for
  9382. * linear buffers or in number of tiles for tiled buffers.
  9383. */
  9384. if (intel_rotation_90_or_270(rotation)) {
  9385. /* stride = Surface height in tiles */
  9386. tile_height = intel_tile_height(dev, fb->pixel_format,
  9387. fb->modifier[0], 0);
  9388. stride = DIV_ROUND_UP(fb->height, tile_height);
  9389. } else {
  9390. stride = fb->pitches[0] /
  9391. intel_fb_stride_alignment(dev, fb->modifier[0],
  9392. fb->pixel_format);
  9393. }
  9394. /*
  9395. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  9396. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  9397. */
  9398. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  9399. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  9400. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  9401. POSTING_READ(PLANE_SURF(pipe, 0));
  9402. }
  9403. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  9404. struct intel_unpin_work *work)
  9405. {
  9406. struct drm_device *dev = intel_crtc->base.dev;
  9407. struct drm_i915_private *dev_priv = dev->dev_private;
  9408. struct intel_framebuffer *intel_fb =
  9409. to_intel_framebuffer(intel_crtc->base.primary->fb);
  9410. struct drm_i915_gem_object *obj = intel_fb->obj;
  9411. u32 dspcntr;
  9412. u32 reg;
  9413. reg = DSPCNTR(intel_crtc->plane);
  9414. dspcntr = I915_READ(reg);
  9415. if (obj->tiling_mode != I915_TILING_NONE)
  9416. dspcntr |= DISPPLANE_TILED;
  9417. else
  9418. dspcntr &= ~DISPPLANE_TILED;
  9419. I915_WRITE(reg, dspcntr);
  9420. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  9421. POSTING_READ(DSPSURF(intel_crtc->plane));
  9422. }
  9423. /*
  9424. * XXX: This is the temporary way to update the plane registers until we get
  9425. * around to using the usual plane update functions for MMIO flips
  9426. */
  9427. static void intel_do_mmio_flip(struct intel_mmio_flip *mmio_flip)
  9428. {
  9429. struct intel_crtc *crtc = mmio_flip->crtc;
  9430. struct intel_unpin_work *work;
  9431. spin_lock_irq(&crtc->base.dev->event_lock);
  9432. work = crtc->unpin_work;
  9433. spin_unlock_irq(&crtc->base.dev->event_lock);
  9434. if (work == NULL)
  9435. return;
  9436. intel_mark_page_flip_active(work);
  9437. intel_pipe_update_start(crtc);
  9438. if (INTEL_INFO(mmio_flip->i915)->gen >= 9)
  9439. skl_do_mmio_flip(crtc, mmio_flip->rotation, work);
  9440. else
  9441. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  9442. ilk_do_mmio_flip(crtc, work);
  9443. intel_pipe_update_end(crtc);
  9444. }
  9445. static void intel_mmio_flip_work_func(struct work_struct *work)
  9446. {
  9447. struct intel_mmio_flip *mmio_flip =
  9448. container_of(work, struct intel_mmio_flip, work);
  9449. if (mmio_flip->req) {
  9450. WARN_ON(__i915_wait_request(mmio_flip->req,
  9451. mmio_flip->crtc->reset_counter,
  9452. false, NULL,
  9453. &mmio_flip->i915->rps.mmioflips));
  9454. i915_gem_request_unreference__unlocked(mmio_flip->req);
  9455. }
  9456. intel_do_mmio_flip(mmio_flip);
  9457. kfree(mmio_flip);
  9458. }
  9459. static int intel_queue_mmio_flip(struct drm_device *dev,
  9460. struct drm_crtc *crtc,
  9461. struct drm_i915_gem_object *obj)
  9462. {
  9463. struct intel_mmio_flip *mmio_flip;
  9464. mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
  9465. if (mmio_flip == NULL)
  9466. return -ENOMEM;
  9467. mmio_flip->i915 = to_i915(dev);
  9468. mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
  9469. mmio_flip->crtc = to_intel_crtc(crtc);
  9470. mmio_flip->rotation = crtc->primary->state->rotation;
  9471. INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
  9472. schedule_work(&mmio_flip->work);
  9473. return 0;
  9474. }
  9475. static int intel_default_queue_flip(struct drm_device *dev,
  9476. struct drm_crtc *crtc,
  9477. struct drm_framebuffer *fb,
  9478. struct drm_i915_gem_object *obj,
  9479. struct drm_i915_gem_request *req,
  9480. uint32_t flags)
  9481. {
  9482. return -ENODEV;
  9483. }
  9484. static bool __intel_pageflip_stall_check(struct drm_device *dev,
  9485. struct drm_crtc *crtc)
  9486. {
  9487. struct drm_i915_private *dev_priv = dev->dev_private;
  9488. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9489. struct intel_unpin_work *work = intel_crtc->unpin_work;
  9490. u32 addr;
  9491. if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
  9492. return true;
  9493. if (atomic_read(&work->pending) < INTEL_FLIP_PENDING)
  9494. return false;
  9495. if (!work->enable_stall_check)
  9496. return false;
  9497. if (work->flip_ready_vblank == 0) {
  9498. if (work->flip_queued_req &&
  9499. !i915_gem_request_completed(work->flip_queued_req, true))
  9500. return false;
  9501. work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
  9502. }
  9503. if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
  9504. return false;
  9505. /* Potential stall - if we see that the flip has happened,
  9506. * assume a missed interrupt. */
  9507. if (INTEL_INFO(dev)->gen >= 4)
  9508. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  9509. else
  9510. addr = I915_READ(DSPADDR(intel_crtc->plane));
  9511. /* There is a potential issue here with a false positive after a flip
  9512. * to the same address. We could address this by checking for a
  9513. * non-incrementing frame counter.
  9514. */
  9515. return addr == work->gtt_offset;
  9516. }
  9517. void intel_check_page_flip(struct drm_device *dev, int pipe)
  9518. {
  9519. struct drm_i915_private *dev_priv = dev->dev_private;
  9520. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9521. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9522. struct intel_unpin_work *work;
  9523. WARN_ON(!in_interrupt());
  9524. if (crtc == NULL)
  9525. return;
  9526. spin_lock(&dev->event_lock);
  9527. work = intel_crtc->unpin_work;
  9528. if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
  9529. WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
  9530. work->flip_queued_vblank, drm_vblank_count(dev, pipe));
  9531. page_flip_completed(intel_crtc);
  9532. work = NULL;
  9533. }
  9534. if (work != NULL &&
  9535. drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
  9536. intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
  9537. spin_unlock(&dev->event_lock);
  9538. }
  9539. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  9540. struct drm_framebuffer *fb,
  9541. struct drm_pending_vblank_event *event,
  9542. uint32_t page_flip_flags)
  9543. {
  9544. struct drm_device *dev = crtc->dev;
  9545. struct drm_i915_private *dev_priv = dev->dev_private;
  9546. struct drm_framebuffer *old_fb = crtc->primary->fb;
  9547. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  9548. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9549. struct drm_plane *primary = crtc->primary;
  9550. enum pipe pipe = intel_crtc->pipe;
  9551. struct intel_unpin_work *work;
  9552. struct intel_engine_cs *ring;
  9553. bool mmio_flip;
  9554. struct drm_i915_gem_request *request = NULL;
  9555. int ret;
  9556. /*
  9557. * drm_mode_page_flip_ioctl() should already catch this, but double
  9558. * check to be safe. In the future we may enable pageflipping from
  9559. * a disabled primary plane.
  9560. */
  9561. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  9562. return -EBUSY;
  9563. /* Can't change pixel format via MI display flips. */
  9564. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  9565. return -EINVAL;
  9566. /*
  9567. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  9568. * Note that pitch changes could also affect these register.
  9569. */
  9570. if (INTEL_INFO(dev)->gen > 3 &&
  9571. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  9572. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  9573. return -EINVAL;
  9574. if (i915_terminally_wedged(&dev_priv->gpu_error))
  9575. goto out_hang;
  9576. work = kzalloc(sizeof(*work), GFP_KERNEL);
  9577. if (work == NULL)
  9578. return -ENOMEM;
  9579. work->event = event;
  9580. work->crtc = crtc;
  9581. work->old_fb = old_fb;
  9582. INIT_WORK(&work->work, intel_unpin_work_fn);
  9583. ret = drm_crtc_vblank_get(crtc);
  9584. if (ret)
  9585. goto free_work;
  9586. /* We borrow the event spin lock for protecting unpin_work */
  9587. spin_lock_irq(&dev->event_lock);
  9588. if (intel_crtc->unpin_work) {
  9589. /* Before declaring the flip queue wedged, check if
  9590. * the hardware completed the operation behind our backs.
  9591. */
  9592. if (__intel_pageflip_stall_check(dev, crtc)) {
  9593. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  9594. page_flip_completed(intel_crtc);
  9595. } else {
  9596. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  9597. spin_unlock_irq(&dev->event_lock);
  9598. drm_crtc_vblank_put(crtc);
  9599. kfree(work);
  9600. return -EBUSY;
  9601. }
  9602. }
  9603. intel_crtc->unpin_work = work;
  9604. spin_unlock_irq(&dev->event_lock);
  9605. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  9606. flush_workqueue(dev_priv->wq);
  9607. /* Reference the objects for the scheduled work. */
  9608. drm_framebuffer_reference(work->old_fb);
  9609. drm_gem_object_reference(&obj->base);
  9610. crtc->primary->fb = fb;
  9611. update_state_fb(crtc->primary);
  9612. work->pending_flip_obj = obj;
  9613. ret = i915_mutex_lock_interruptible(dev);
  9614. if (ret)
  9615. goto cleanup;
  9616. atomic_inc(&intel_crtc->unpin_work_count);
  9617. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  9618. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  9619. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  9620. if (IS_VALLEYVIEW(dev)) {
  9621. ring = &dev_priv->ring[BCS];
  9622. if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
  9623. /* vlv: DISPLAY_FLIP fails to change tiling */
  9624. ring = NULL;
  9625. } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  9626. ring = &dev_priv->ring[BCS];
  9627. } else if (INTEL_INFO(dev)->gen >= 7) {
  9628. ring = i915_gem_request_get_ring(obj->last_write_req);
  9629. if (ring == NULL || ring->id != RCS)
  9630. ring = &dev_priv->ring[BCS];
  9631. } else {
  9632. ring = &dev_priv->ring[RCS];
  9633. }
  9634. mmio_flip = use_mmio_flip(ring, obj);
  9635. /* When using CS flips, we want to emit semaphores between rings.
  9636. * However, when using mmio flips we will create a task to do the
  9637. * synchronisation, so all we want here is to pin the framebuffer
  9638. * into the display plane and skip any waits.
  9639. */
  9640. if (!mmio_flip) {
  9641. ret = i915_gem_object_sync(obj, ring, &request);
  9642. if (ret)
  9643. goto cleanup_pending;
  9644. }
  9645. ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
  9646. crtc->primary->state);
  9647. if (ret)
  9648. goto cleanup_pending;
  9649. work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary),
  9650. obj, 0);
  9651. work->gtt_offset += intel_crtc->dspaddr_offset;
  9652. if (mmio_flip) {
  9653. ret = intel_queue_mmio_flip(dev, crtc, obj);
  9654. if (ret)
  9655. goto cleanup_unpin;
  9656. i915_gem_request_assign(&work->flip_queued_req,
  9657. obj->last_write_req);
  9658. } else {
  9659. if (!request) {
  9660. ret = i915_gem_request_alloc(ring, ring->default_context, &request);
  9661. if (ret)
  9662. goto cleanup_unpin;
  9663. }
  9664. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  9665. page_flip_flags);
  9666. if (ret)
  9667. goto cleanup_unpin;
  9668. i915_gem_request_assign(&work->flip_queued_req, request);
  9669. }
  9670. if (request)
  9671. i915_add_request_no_flush(request);
  9672. work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
  9673. work->enable_stall_check = true;
  9674. i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
  9675. to_intel_plane(primary)->frontbuffer_bit);
  9676. mutex_unlock(&dev->struct_mutex);
  9677. intel_fbc_disable_crtc(intel_crtc);
  9678. intel_frontbuffer_flip_prepare(dev,
  9679. to_intel_plane(primary)->frontbuffer_bit);
  9680. trace_i915_flip_request(intel_crtc->plane, obj);
  9681. return 0;
  9682. cleanup_unpin:
  9683. intel_unpin_fb_obj(fb, crtc->primary->state);
  9684. cleanup_pending:
  9685. if (request)
  9686. i915_gem_request_cancel(request);
  9687. atomic_dec(&intel_crtc->unpin_work_count);
  9688. mutex_unlock(&dev->struct_mutex);
  9689. cleanup:
  9690. crtc->primary->fb = old_fb;
  9691. update_state_fb(crtc->primary);
  9692. drm_gem_object_unreference_unlocked(&obj->base);
  9693. drm_framebuffer_unreference(work->old_fb);
  9694. spin_lock_irq(&dev->event_lock);
  9695. intel_crtc->unpin_work = NULL;
  9696. spin_unlock_irq(&dev->event_lock);
  9697. drm_crtc_vblank_put(crtc);
  9698. free_work:
  9699. kfree(work);
  9700. if (ret == -EIO) {
  9701. struct drm_atomic_state *state;
  9702. struct drm_plane_state *plane_state;
  9703. out_hang:
  9704. state = drm_atomic_state_alloc(dev);
  9705. if (!state)
  9706. return -ENOMEM;
  9707. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  9708. retry:
  9709. plane_state = drm_atomic_get_plane_state(state, primary);
  9710. ret = PTR_ERR_OR_ZERO(plane_state);
  9711. if (!ret) {
  9712. drm_atomic_set_fb_for_plane(plane_state, fb);
  9713. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  9714. if (!ret)
  9715. ret = drm_atomic_commit(state);
  9716. }
  9717. if (ret == -EDEADLK) {
  9718. drm_modeset_backoff(state->acquire_ctx);
  9719. drm_atomic_state_clear(state);
  9720. goto retry;
  9721. }
  9722. if (ret)
  9723. drm_atomic_state_free(state);
  9724. if (ret == 0 && event) {
  9725. spin_lock_irq(&dev->event_lock);
  9726. drm_send_vblank_event(dev, pipe, event);
  9727. spin_unlock_irq(&dev->event_lock);
  9728. }
  9729. }
  9730. return ret;
  9731. }
  9732. /**
  9733. * intel_wm_need_update - Check whether watermarks need updating
  9734. * @plane: drm plane
  9735. * @state: new plane state
  9736. *
  9737. * Check current plane state versus the new one to determine whether
  9738. * watermarks need to be recalculated.
  9739. *
  9740. * Returns true or false.
  9741. */
  9742. static bool intel_wm_need_update(struct drm_plane *plane,
  9743. struct drm_plane_state *state)
  9744. {
  9745. struct intel_plane_state *new = to_intel_plane_state(state);
  9746. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9747. /* Update watermarks on tiling or size changes. */
  9748. if (!plane->state->fb || !state->fb ||
  9749. plane->state->fb->modifier[0] != state->fb->modifier[0] ||
  9750. plane->state->rotation != state->rotation ||
  9751. drm_rect_width(&new->src) != drm_rect_width(&cur->src) ||
  9752. drm_rect_height(&new->src) != drm_rect_height(&cur->src) ||
  9753. drm_rect_width(&new->dst) != drm_rect_width(&cur->dst) ||
  9754. drm_rect_height(&new->dst) != drm_rect_height(&cur->dst))
  9755. return true;
  9756. return false;
  9757. }
  9758. static bool needs_scaling(struct intel_plane_state *state)
  9759. {
  9760. int src_w = drm_rect_width(&state->src) >> 16;
  9761. int src_h = drm_rect_height(&state->src) >> 16;
  9762. int dst_w = drm_rect_width(&state->dst);
  9763. int dst_h = drm_rect_height(&state->dst);
  9764. return (src_w != dst_w || src_h != dst_h);
  9765. }
  9766. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9767. struct drm_plane_state *plane_state)
  9768. {
  9769. struct drm_crtc *crtc = crtc_state->crtc;
  9770. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9771. struct drm_plane *plane = plane_state->plane;
  9772. struct drm_device *dev = crtc->dev;
  9773. struct drm_i915_private *dev_priv = dev->dev_private;
  9774. struct intel_plane_state *old_plane_state =
  9775. to_intel_plane_state(plane->state);
  9776. int idx = intel_crtc->base.base.id, ret;
  9777. int i = drm_plane_index(plane);
  9778. bool mode_changed = needs_modeset(crtc_state);
  9779. bool was_crtc_enabled = crtc->state->active;
  9780. bool is_crtc_enabled = crtc_state->active;
  9781. bool turn_off, turn_on, visible, was_visible;
  9782. struct drm_framebuffer *fb = plane_state->fb;
  9783. if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
  9784. plane->type != DRM_PLANE_TYPE_CURSOR) {
  9785. ret = skl_update_scaler_plane(
  9786. to_intel_crtc_state(crtc_state),
  9787. to_intel_plane_state(plane_state));
  9788. if (ret)
  9789. return ret;
  9790. }
  9791. was_visible = old_plane_state->visible;
  9792. visible = to_intel_plane_state(plane_state)->visible;
  9793. if (!was_crtc_enabled && WARN_ON(was_visible))
  9794. was_visible = false;
  9795. if (!is_crtc_enabled && WARN_ON(visible))
  9796. visible = false;
  9797. if (!was_visible && !visible)
  9798. return 0;
  9799. turn_off = was_visible && (!visible || mode_changed);
  9800. turn_on = visible && (!was_visible || mode_changed);
  9801. DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
  9802. plane->base.id, fb ? fb->base.id : -1);
  9803. DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
  9804. plane->base.id, was_visible, visible,
  9805. turn_off, turn_on, mode_changed);
  9806. if (turn_on) {
  9807. intel_crtc->atomic.update_wm_pre = true;
  9808. /* must disable cxsr around plane enable/disable */
  9809. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9810. intel_crtc->atomic.disable_cxsr = true;
  9811. /* to potentially re-enable cxsr */
  9812. intel_crtc->atomic.wait_vblank = true;
  9813. intel_crtc->atomic.update_wm_post = true;
  9814. }
  9815. } else if (turn_off) {
  9816. intel_crtc->atomic.update_wm_post = true;
  9817. /* must disable cxsr around plane enable/disable */
  9818. if (plane->type != DRM_PLANE_TYPE_CURSOR) {
  9819. if (is_crtc_enabled)
  9820. intel_crtc->atomic.wait_vblank = true;
  9821. intel_crtc->atomic.disable_cxsr = true;
  9822. }
  9823. } else if (intel_wm_need_update(plane, plane_state)) {
  9824. intel_crtc->atomic.update_wm_pre = true;
  9825. }
  9826. if (visible || was_visible)
  9827. intel_crtc->atomic.fb_bits |=
  9828. to_intel_plane(plane)->frontbuffer_bit;
  9829. switch (plane->type) {
  9830. case DRM_PLANE_TYPE_PRIMARY:
  9831. intel_crtc->atomic.pre_disable_primary = turn_off;
  9832. intel_crtc->atomic.post_enable_primary = turn_on;
  9833. if (turn_off) {
  9834. /*
  9835. * FIXME: Actually if we will still have any other
  9836. * plane enabled on the pipe we could let IPS enabled
  9837. * still, but for now lets consider that when we make
  9838. * primary invisible by setting DSPCNTR to 0 on
  9839. * update_primary_plane function IPS needs to be
  9840. * disable.
  9841. */
  9842. intel_crtc->atomic.disable_ips = true;
  9843. intel_crtc->atomic.disable_fbc = true;
  9844. }
  9845. /*
  9846. * FBC does not work on some platforms for rotated
  9847. * planes, so disable it when rotation is not 0 and
  9848. * update it when rotation is set back to 0.
  9849. *
  9850. * FIXME: This is redundant with the fbc update done in
  9851. * the primary plane enable function except that that
  9852. * one is done too late. We eventually need to unify
  9853. * this.
  9854. */
  9855. if (visible &&
  9856. INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
  9857. dev_priv->fbc.crtc == intel_crtc &&
  9858. plane_state->rotation != BIT(DRM_ROTATE_0))
  9859. intel_crtc->atomic.disable_fbc = true;
  9860. /*
  9861. * BDW signals flip done immediately if the plane
  9862. * is disabled, even if the plane enable is already
  9863. * armed to occur at the next vblank :(
  9864. */
  9865. if (turn_on && IS_BROADWELL(dev))
  9866. intel_crtc->atomic.wait_vblank = true;
  9867. intel_crtc->atomic.update_fbc |= visible || mode_changed;
  9868. break;
  9869. case DRM_PLANE_TYPE_CURSOR:
  9870. break;
  9871. case DRM_PLANE_TYPE_OVERLAY:
  9872. /*
  9873. * WaCxSRDisabledForSpriteScaling:ivb
  9874. *
  9875. * cstate->update_wm was already set above, so this flag will
  9876. * take effect when we commit and program watermarks.
  9877. */
  9878. if (IS_IVYBRIDGE(dev) &&
  9879. needs_scaling(to_intel_plane_state(plane_state)) &&
  9880. !needs_scaling(old_plane_state)) {
  9881. to_intel_crtc_state(crtc_state)->disable_lp_wm = true;
  9882. } else if (turn_off && !mode_changed) {
  9883. intel_crtc->atomic.wait_vblank = true;
  9884. intel_crtc->atomic.update_sprite_watermarks |=
  9885. 1 << i;
  9886. }
  9887. break;
  9888. }
  9889. return 0;
  9890. }
  9891. static bool encoders_cloneable(const struct intel_encoder *a,
  9892. const struct intel_encoder *b)
  9893. {
  9894. /* masks could be asymmetric, so check both ways */
  9895. return a == b || (a->cloneable & (1 << b->type) &&
  9896. b->cloneable & (1 << a->type));
  9897. }
  9898. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9899. struct intel_crtc *crtc,
  9900. struct intel_encoder *encoder)
  9901. {
  9902. struct intel_encoder *source_encoder;
  9903. struct drm_connector *connector;
  9904. struct drm_connector_state *connector_state;
  9905. int i;
  9906. for_each_connector_in_state(state, connector, connector_state, i) {
  9907. if (connector_state->crtc != &crtc->base)
  9908. continue;
  9909. source_encoder =
  9910. to_intel_encoder(connector_state->best_encoder);
  9911. if (!encoders_cloneable(encoder, source_encoder))
  9912. return false;
  9913. }
  9914. return true;
  9915. }
  9916. static bool check_encoder_cloning(struct drm_atomic_state *state,
  9917. struct intel_crtc *crtc)
  9918. {
  9919. struct intel_encoder *encoder;
  9920. struct drm_connector *connector;
  9921. struct drm_connector_state *connector_state;
  9922. int i;
  9923. for_each_connector_in_state(state, connector, connector_state, i) {
  9924. if (connector_state->crtc != &crtc->base)
  9925. continue;
  9926. encoder = to_intel_encoder(connector_state->best_encoder);
  9927. if (!check_single_encoder_cloning(state, crtc, encoder))
  9928. return false;
  9929. }
  9930. return true;
  9931. }
  9932. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9933. struct drm_crtc_state *crtc_state)
  9934. {
  9935. struct drm_device *dev = crtc->dev;
  9936. struct drm_i915_private *dev_priv = dev->dev_private;
  9937. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9938. struct intel_crtc_state *pipe_config =
  9939. to_intel_crtc_state(crtc_state);
  9940. struct drm_atomic_state *state = crtc_state->state;
  9941. int ret;
  9942. bool mode_changed = needs_modeset(crtc_state);
  9943. if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
  9944. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9945. return -EINVAL;
  9946. }
  9947. if (mode_changed && !crtc_state->active)
  9948. intel_crtc->atomic.update_wm_post = true;
  9949. if (mode_changed && crtc_state->enable &&
  9950. dev_priv->display.crtc_compute_clock &&
  9951. !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
  9952. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9953. pipe_config);
  9954. if (ret)
  9955. return ret;
  9956. }
  9957. ret = 0;
  9958. if (dev_priv->display.compute_pipe_wm) {
  9959. ret = dev_priv->display.compute_pipe_wm(intel_crtc, state);
  9960. if (ret)
  9961. return ret;
  9962. }
  9963. if (INTEL_INFO(dev)->gen >= 9) {
  9964. if (mode_changed)
  9965. ret = skl_update_scaler_crtc(pipe_config);
  9966. if (!ret)
  9967. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  9968. pipe_config);
  9969. }
  9970. return ret;
  9971. }
  9972. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9973. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9974. .load_lut = intel_crtc_load_lut,
  9975. .atomic_begin = intel_begin_crtc_commit,
  9976. .atomic_flush = intel_finish_crtc_commit,
  9977. .atomic_check = intel_crtc_atomic_check,
  9978. };
  9979. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9980. {
  9981. struct intel_connector *connector;
  9982. for_each_intel_connector(dev, connector) {
  9983. if (connector->base.encoder) {
  9984. connector->base.state->best_encoder =
  9985. connector->base.encoder;
  9986. connector->base.state->crtc =
  9987. connector->base.encoder->crtc;
  9988. } else {
  9989. connector->base.state->best_encoder = NULL;
  9990. connector->base.state->crtc = NULL;
  9991. }
  9992. }
  9993. }
  9994. static void
  9995. connected_sink_compute_bpp(struct intel_connector *connector,
  9996. struct intel_crtc_state *pipe_config)
  9997. {
  9998. int bpp = pipe_config->pipe_bpp;
  9999. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10000. connector->base.base.id,
  10001. connector->base.name);
  10002. /* Don't use an invalid EDID bpc value */
  10003. if (connector->base.display_info.bpc &&
  10004. connector->base.display_info.bpc * 3 < bpp) {
  10005. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10006. bpp, connector->base.display_info.bpc*3);
  10007. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  10008. }
  10009. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10010. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  10011. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10012. bpp);
  10013. pipe_config->pipe_bpp = 24;
  10014. }
  10015. }
  10016. static int
  10017. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10018. struct intel_crtc_state *pipe_config)
  10019. {
  10020. struct drm_device *dev = crtc->base.dev;
  10021. struct drm_atomic_state *state;
  10022. struct drm_connector *connector;
  10023. struct drm_connector_state *connector_state;
  10024. int bpp, i;
  10025. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
  10026. bpp = 10*3;
  10027. else if (INTEL_INFO(dev)->gen >= 5)
  10028. bpp = 12*3;
  10029. else
  10030. bpp = 8*3;
  10031. pipe_config->pipe_bpp = bpp;
  10032. state = pipe_config->base.state;
  10033. /* Clamp display bpp to EDID value */
  10034. for_each_connector_in_state(state, connector, connector_state, i) {
  10035. if (connector_state->crtc != &crtc->base)
  10036. continue;
  10037. connected_sink_compute_bpp(to_intel_connector(connector),
  10038. pipe_config);
  10039. }
  10040. return bpp;
  10041. }
  10042. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10043. {
  10044. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10045. "type: 0x%x flags: 0x%x\n",
  10046. mode->crtc_clock,
  10047. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10048. mode->crtc_hsync_end, mode->crtc_htotal,
  10049. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10050. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10051. }
  10052. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10053. struct intel_crtc_state *pipe_config,
  10054. const char *context)
  10055. {
  10056. struct drm_device *dev = crtc->base.dev;
  10057. struct drm_plane *plane;
  10058. struct intel_plane *intel_plane;
  10059. struct intel_plane_state *state;
  10060. struct drm_framebuffer *fb;
  10061. DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
  10062. context, pipe_config, pipe_name(crtc->pipe));
  10063. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  10064. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10065. pipe_config->pipe_bpp, pipe_config->dither);
  10066. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10067. pipe_config->has_pch_encoder,
  10068. pipe_config->fdi_lanes,
  10069. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10070. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10071. pipe_config->fdi_m_n.tu);
  10072. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10073. pipe_config->has_dp_encoder,
  10074. pipe_config->lane_count,
  10075. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10076. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10077. pipe_config->dp_m_n.tu);
  10078. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10079. pipe_config->has_dp_encoder,
  10080. pipe_config->lane_count,
  10081. pipe_config->dp_m2_n2.gmch_m,
  10082. pipe_config->dp_m2_n2.gmch_n,
  10083. pipe_config->dp_m2_n2.link_m,
  10084. pipe_config->dp_m2_n2.link_n,
  10085. pipe_config->dp_m2_n2.tu);
  10086. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10087. pipe_config->has_audio,
  10088. pipe_config->has_infoframe);
  10089. DRM_DEBUG_KMS("requested mode:\n");
  10090. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10091. DRM_DEBUG_KMS("adjusted mode:\n");
  10092. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10093. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10094. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10095. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10096. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10097. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10098. crtc->num_scalers,
  10099. pipe_config->scaler_state.scaler_users,
  10100. pipe_config->scaler_state.scaler_id);
  10101. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10102. pipe_config->gmch_pfit.control,
  10103. pipe_config->gmch_pfit.pgm_ratios,
  10104. pipe_config->gmch_pfit.lvds_border_bits);
  10105. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10106. pipe_config->pch_pfit.pos,
  10107. pipe_config->pch_pfit.size,
  10108. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10109. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10110. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10111. if (IS_BROXTON(dev)) {
  10112. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10113. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10114. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10115. pipe_config->ddi_pll_sel,
  10116. pipe_config->dpll_hw_state.ebb0,
  10117. pipe_config->dpll_hw_state.ebb4,
  10118. pipe_config->dpll_hw_state.pll0,
  10119. pipe_config->dpll_hw_state.pll1,
  10120. pipe_config->dpll_hw_state.pll2,
  10121. pipe_config->dpll_hw_state.pll3,
  10122. pipe_config->dpll_hw_state.pll6,
  10123. pipe_config->dpll_hw_state.pll8,
  10124. pipe_config->dpll_hw_state.pll9,
  10125. pipe_config->dpll_hw_state.pll10,
  10126. pipe_config->dpll_hw_state.pcsdw12);
  10127. } else if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
  10128. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
  10129. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10130. pipe_config->ddi_pll_sel,
  10131. pipe_config->dpll_hw_state.ctrl1,
  10132. pipe_config->dpll_hw_state.cfgcr1,
  10133. pipe_config->dpll_hw_state.cfgcr2);
  10134. } else if (HAS_DDI(dev)) {
  10135. DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
  10136. pipe_config->ddi_pll_sel,
  10137. pipe_config->dpll_hw_state.wrpll);
  10138. } else {
  10139. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10140. "fp0: 0x%x, fp1: 0x%x\n",
  10141. pipe_config->dpll_hw_state.dpll,
  10142. pipe_config->dpll_hw_state.dpll_md,
  10143. pipe_config->dpll_hw_state.fp0,
  10144. pipe_config->dpll_hw_state.fp1);
  10145. }
  10146. DRM_DEBUG_KMS("planes on this crtc\n");
  10147. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10148. intel_plane = to_intel_plane(plane);
  10149. if (intel_plane->pipe != crtc->pipe)
  10150. continue;
  10151. state = to_intel_plane_state(plane->state);
  10152. fb = state->base.fb;
  10153. if (!fb) {
  10154. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
  10155. "disabled, scaler_id = %d\n",
  10156. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10157. plane->base.id, intel_plane->pipe,
  10158. (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
  10159. drm_plane_index(plane), state->scaler_id);
  10160. continue;
  10161. }
  10162. DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
  10163. plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
  10164. plane->base.id, intel_plane->pipe,
  10165. crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
  10166. drm_plane_index(plane));
  10167. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
  10168. fb->base.id, fb->width, fb->height, fb->pixel_format);
  10169. DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
  10170. state->scaler_id,
  10171. state->src.x1 >> 16, state->src.y1 >> 16,
  10172. drm_rect_width(&state->src) >> 16,
  10173. drm_rect_height(&state->src) >> 16,
  10174. state->dst.x1, state->dst.y1,
  10175. drm_rect_width(&state->dst), drm_rect_height(&state->dst));
  10176. }
  10177. }
  10178. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10179. {
  10180. struct drm_device *dev = state->dev;
  10181. struct intel_encoder *encoder;
  10182. struct drm_connector *connector;
  10183. struct drm_connector_state *connector_state;
  10184. unsigned int used_ports = 0;
  10185. int i;
  10186. /*
  10187. * Walk the connector list instead of the encoder
  10188. * list to detect the problem on ddi platforms
  10189. * where there's just one encoder per digital port.
  10190. */
  10191. for_each_connector_in_state(state, connector, connector_state, i) {
  10192. if (!connector_state->best_encoder)
  10193. continue;
  10194. encoder = to_intel_encoder(connector_state->best_encoder);
  10195. WARN_ON(!connector_state->crtc);
  10196. switch (encoder->type) {
  10197. unsigned int port_mask;
  10198. case INTEL_OUTPUT_UNKNOWN:
  10199. if (WARN_ON(!HAS_DDI(dev)))
  10200. break;
  10201. case INTEL_OUTPUT_DISPLAYPORT:
  10202. case INTEL_OUTPUT_HDMI:
  10203. case INTEL_OUTPUT_EDP:
  10204. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10205. /* the same port mustn't appear more than once */
  10206. if (used_ports & port_mask)
  10207. return false;
  10208. used_ports |= port_mask;
  10209. default:
  10210. break;
  10211. }
  10212. }
  10213. return true;
  10214. }
  10215. static void
  10216. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10217. {
  10218. struct drm_crtc_state tmp_state;
  10219. struct intel_crtc_scaler_state scaler_state;
  10220. struct intel_dpll_hw_state dpll_hw_state;
  10221. enum intel_dpll_id shared_dpll;
  10222. uint32_t ddi_pll_sel;
  10223. bool force_thru;
  10224. /* FIXME: before the switch to atomic started, a new pipe_config was
  10225. * kzalloc'd. Code that depends on any field being zero should be
  10226. * fixed, so that the crtc_state can be safely duplicated. For now,
  10227. * only fields that are know to not cause problems are preserved. */
  10228. tmp_state = crtc_state->base;
  10229. scaler_state = crtc_state->scaler_state;
  10230. shared_dpll = crtc_state->shared_dpll;
  10231. dpll_hw_state = crtc_state->dpll_hw_state;
  10232. ddi_pll_sel = crtc_state->ddi_pll_sel;
  10233. force_thru = crtc_state->pch_pfit.force_thru;
  10234. memset(crtc_state, 0, sizeof *crtc_state);
  10235. crtc_state->base = tmp_state;
  10236. crtc_state->scaler_state = scaler_state;
  10237. crtc_state->shared_dpll = shared_dpll;
  10238. crtc_state->dpll_hw_state = dpll_hw_state;
  10239. crtc_state->ddi_pll_sel = ddi_pll_sel;
  10240. crtc_state->pch_pfit.force_thru = force_thru;
  10241. }
  10242. static int
  10243. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10244. struct intel_crtc_state *pipe_config)
  10245. {
  10246. struct drm_atomic_state *state = pipe_config->base.state;
  10247. struct intel_encoder *encoder;
  10248. struct drm_connector *connector;
  10249. struct drm_connector_state *connector_state;
  10250. int base_bpp, ret = -EINVAL;
  10251. int i;
  10252. bool retry = true;
  10253. clear_intel_crtc_state(pipe_config);
  10254. pipe_config->cpu_transcoder =
  10255. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10256. /*
  10257. * Sanitize sync polarity flags based on requested ones. If neither
  10258. * positive or negative polarity is requested, treat this as meaning
  10259. * negative polarity.
  10260. */
  10261. if (!(pipe_config->base.adjusted_mode.flags &
  10262. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10263. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10264. if (!(pipe_config->base.adjusted_mode.flags &
  10265. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10266. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10267. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10268. pipe_config);
  10269. if (base_bpp < 0)
  10270. goto fail;
  10271. /*
  10272. * Determine the real pipe dimensions. Note that stereo modes can
  10273. * increase the actual pipe size due to the frame doubling and
  10274. * insertion of additional space for blanks between the frame. This
  10275. * is stored in the crtc timings. We use the requested mode to do this
  10276. * computation to clearly distinguish it from the adjusted mode, which
  10277. * can be changed by the connectors in the below retry loop.
  10278. */
  10279. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10280. &pipe_config->pipe_src_w,
  10281. &pipe_config->pipe_src_h);
  10282. encoder_retry:
  10283. /* Ensure the port clock defaults are reset when retrying. */
  10284. pipe_config->port_clock = 0;
  10285. pipe_config->pixel_multiplier = 1;
  10286. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10287. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10288. CRTC_STEREO_DOUBLE);
  10289. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10290. * adjust it according to limitations or connector properties, and also
  10291. * a chance to reject the mode entirely.
  10292. */
  10293. for_each_connector_in_state(state, connector, connector_state, i) {
  10294. if (connector_state->crtc != crtc)
  10295. continue;
  10296. encoder = to_intel_encoder(connector_state->best_encoder);
  10297. if (!(encoder->compute_config(encoder, pipe_config))) {
  10298. DRM_DEBUG_KMS("Encoder config failure\n");
  10299. goto fail;
  10300. }
  10301. }
  10302. /* Set default port clock if not overwritten by the encoder. Needs to be
  10303. * done afterwards in case the encoder adjusts the mode. */
  10304. if (!pipe_config->port_clock)
  10305. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10306. * pipe_config->pixel_multiplier;
  10307. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10308. if (ret < 0) {
  10309. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10310. goto fail;
  10311. }
  10312. if (ret == RETRY) {
  10313. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10314. ret = -EINVAL;
  10315. goto fail;
  10316. }
  10317. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10318. retry = false;
  10319. goto encoder_retry;
  10320. }
  10321. /* Dithering seems to not pass-through bits correctly when it should, so
  10322. * only enable it on 6bpc panels. */
  10323. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10324. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10325. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10326. fail:
  10327. return ret;
  10328. }
  10329. static void
  10330. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10331. {
  10332. struct drm_crtc *crtc;
  10333. struct drm_crtc_state *crtc_state;
  10334. int i;
  10335. /* Double check state. */
  10336. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10337. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10338. /* Update hwmode for vblank functions */
  10339. if (crtc->state->active)
  10340. crtc->hwmode = crtc->state->adjusted_mode;
  10341. else
  10342. crtc->hwmode.crtc_clock = 0;
  10343. /*
  10344. * Update legacy state to satisfy fbc code. This can
  10345. * be removed when fbc uses the atomic state.
  10346. */
  10347. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10348. struct drm_plane_state *plane_state = crtc->primary->state;
  10349. crtc->primary->fb = plane_state->fb;
  10350. crtc->x = plane_state->src_x >> 16;
  10351. crtc->y = plane_state->src_y >> 16;
  10352. }
  10353. }
  10354. }
  10355. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10356. {
  10357. int diff;
  10358. if (clock1 == clock2)
  10359. return true;
  10360. if (!clock1 || !clock2)
  10361. return false;
  10362. diff = abs(clock1 - clock2);
  10363. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10364. return true;
  10365. return false;
  10366. }
  10367. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  10368. list_for_each_entry((intel_crtc), \
  10369. &(dev)->mode_config.crtc_list, \
  10370. base.head) \
  10371. if (mask & (1 <<(intel_crtc)->pipe))
  10372. static bool
  10373. intel_compare_m_n(unsigned int m, unsigned int n,
  10374. unsigned int m2, unsigned int n2,
  10375. bool exact)
  10376. {
  10377. if (m == m2 && n == n2)
  10378. return true;
  10379. if (exact || !m || !n || !m2 || !n2)
  10380. return false;
  10381. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10382. if (m > m2) {
  10383. while (m > m2) {
  10384. m2 <<= 1;
  10385. n2 <<= 1;
  10386. }
  10387. } else if (m < m2) {
  10388. while (m < m2) {
  10389. m <<= 1;
  10390. n <<= 1;
  10391. }
  10392. }
  10393. return m == m2 && n == n2;
  10394. }
  10395. static bool
  10396. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  10397. struct intel_link_m_n *m2_n2,
  10398. bool adjust)
  10399. {
  10400. if (m_n->tu == m2_n2->tu &&
  10401. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  10402. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  10403. intel_compare_m_n(m_n->link_m, m_n->link_n,
  10404. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  10405. if (adjust)
  10406. *m2_n2 = *m_n;
  10407. return true;
  10408. }
  10409. return false;
  10410. }
  10411. static bool
  10412. intel_pipe_config_compare(struct drm_device *dev,
  10413. struct intel_crtc_state *current_config,
  10414. struct intel_crtc_state *pipe_config,
  10415. bool adjust)
  10416. {
  10417. bool ret = true;
  10418. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  10419. do { \
  10420. if (!adjust) \
  10421. DRM_ERROR(fmt, ##__VA_ARGS__); \
  10422. else \
  10423. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  10424. } while (0)
  10425. #define PIPE_CONF_CHECK_X(name) \
  10426. if (current_config->name != pipe_config->name) { \
  10427. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10428. "(expected 0x%08x, found 0x%08x)\n", \
  10429. current_config->name, \
  10430. pipe_config->name); \
  10431. ret = false; \
  10432. }
  10433. #define PIPE_CONF_CHECK_I(name) \
  10434. if (current_config->name != pipe_config->name) { \
  10435. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10436. "(expected %i, found %i)\n", \
  10437. current_config->name, \
  10438. pipe_config->name); \
  10439. ret = false; \
  10440. }
  10441. #define PIPE_CONF_CHECK_M_N(name) \
  10442. if (!intel_compare_link_m_n(&current_config->name, \
  10443. &pipe_config->name,\
  10444. adjust)) { \
  10445. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10446. "(expected tu %i gmch %i/%i link %i/%i, " \
  10447. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10448. current_config->name.tu, \
  10449. current_config->name.gmch_m, \
  10450. current_config->name.gmch_n, \
  10451. current_config->name.link_m, \
  10452. current_config->name.link_n, \
  10453. pipe_config->name.tu, \
  10454. pipe_config->name.gmch_m, \
  10455. pipe_config->name.gmch_n, \
  10456. pipe_config->name.link_m, \
  10457. pipe_config->name.link_n); \
  10458. ret = false; \
  10459. }
  10460. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  10461. if (!intel_compare_link_m_n(&current_config->name, \
  10462. &pipe_config->name, adjust) && \
  10463. !intel_compare_link_m_n(&current_config->alt_name, \
  10464. &pipe_config->name, adjust)) { \
  10465. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10466. "(expected tu %i gmch %i/%i link %i/%i, " \
  10467. "or tu %i gmch %i/%i link %i/%i, " \
  10468. "found tu %i, gmch %i/%i link %i/%i)\n", \
  10469. current_config->name.tu, \
  10470. current_config->name.gmch_m, \
  10471. current_config->name.gmch_n, \
  10472. current_config->name.link_m, \
  10473. current_config->name.link_n, \
  10474. current_config->alt_name.tu, \
  10475. current_config->alt_name.gmch_m, \
  10476. current_config->alt_name.gmch_n, \
  10477. current_config->alt_name.link_m, \
  10478. current_config->alt_name.link_n, \
  10479. pipe_config->name.tu, \
  10480. pipe_config->name.gmch_m, \
  10481. pipe_config->name.gmch_n, \
  10482. pipe_config->name.link_m, \
  10483. pipe_config->name.link_n); \
  10484. ret = false; \
  10485. }
  10486. /* This is required for BDW+ where there is only one set of registers for
  10487. * switching between high and low RR.
  10488. * This macro can be used whenever a comparison has to be made between one
  10489. * hw state and multiple sw state variables.
  10490. */
  10491. #define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
  10492. if ((current_config->name != pipe_config->name) && \
  10493. (current_config->alt_name != pipe_config->name)) { \
  10494. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10495. "(expected %i or %i, found %i)\n", \
  10496. current_config->name, \
  10497. current_config->alt_name, \
  10498. pipe_config->name); \
  10499. ret = false; \
  10500. }
  10501. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  10502. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  10503. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  10504. "(expected %i, found %i)\n", \
  10505. current_config->name & (mask), \
  10506. pipe_config->name & (mask)); \
  10507. ret = false; \
  10508. }
  10509. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  10510. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  10511. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  10512. "(expected %i, found %i)\n", \
  10513. current_config->name, \
  10514. pipe_config->name); \
  10515. ret = false; \
  10516. }
  10517. #define PIPE_CONF_QUIRK(quirk) \
  10518. ((current_config->quirks | pipe_config->quirks) & (quirk))
  10519. PIPE_CONF_CHECK_I(cpu_transcoder);
  10520. PIPE_CONF_CHECK_I(has_pch_encoder);
  10521. PIPE_CONF_CHECK_I(fdi_lanes);
  10522. PIPE_CONF_CHECK_M_N(fdi_m_n);
  10523. PIPE_CONF_CHECK_I(has_dp_encoder);
  10524. PIPE_CONF_CHECK_I(lane_count);
  10525. if (INTEL_INFO(dev)->gen < 8) {
  10526. PIPE_CONF_CHECK_M_N(dp_m_n);
  10527. PIPE_CONF_CHECK_I(has_drrs);
  10528. if (current_config->has_drrs)
  10529. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  10530. } else
  10531. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  10532. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  10533. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  10534. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  10535. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  10536. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  10537. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  10538. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  10539. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  10540. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  10541. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  10542. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  10543. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  10544. PIPE_CONF_CHECK_I(pixel_multiplier);
  10545. PIPE_CONF_CHECK_I(has_hdmi_sink);
  10546. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  10547. IS_VALLEYVIEW(dev))
  10548. PIPE_CONF_CHECK_I(limited_color_range);
  10549. PIPE_CONF_CHECK_I(has_infoframe);
  10550. PIPE_CONF_CHECK_I(has_audio);
  10551. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10552. DRM_MODE_FLAG_INTERLACE);
  10553. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  10554. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10555. DRM_MODE_FLAG_PHSYNC);
  10556. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10557. DRM_MODE_FLAG_NHSYNC);
  10558. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10559. DRM_MODE_FLAG_PVSYNC);
  10560. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  10561. DRM_MODE_FLAG_NVSYNC);
  10562. }
  10563. PIPE_CONF_CHECK_X(gmch_pfit.control);
  10564. /* pfit ratios are autocomputed by the hw on gen4+ */
  10565. if (INTEL_INFO(dev)->gen < 4)
  10566. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  10567. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  10568. if (!adjust) {
  10569. PIPE_CONF_CHECK_I(pipe_src_w);
  10570. PIPE_CONF_CHECK_I(pipe_src_h);
  10571. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  10572. if (current_config->pch_pfit.enabled) {
  10573. PIPE_CONF_CHECK_X(pch_pfit.pos);
  10574. PIPE_CONF_CHECK_X(pch_pfit.size);
  10575. }
  10576. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  10577. }
  10578. /* BDW+ don't expose a synchronous way to read the state */
  10579. if (IS_HASWELL(dev))
  10580. PIPE_CONF_CHECK_I(ips_enabled);
  10581. PIPE_CONF_CHECK_I(double_wide);
  10582. PIPE_CONF_CHECK_X(ddi_pll_sel);
  10583. PIPE_CONF_CHECK_I(shared_dpll);
  10584. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  10585. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  10586. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  10587. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  10588. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  10589. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  10590. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  10591. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  10592. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  10593. PIPE_CONF_CHECK_I(pipe_bpp);
  10594. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  10595. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  10596. #undef PIPE_CONF_CHECK_X
  10597. #undef PIPE_CONF_CHECK_I
  10598. #undef PIPE_CONF_CHECK_I_ALT
  10599. #undef PIPE_CONF_CHECK_FLAGS
  10600. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  10601. #undef PIPE_CONF_QUIRK
  10602. #undef INTEL_ERR_OR_DBG_KMS
  10603. return ret;
  10604. }
  10605. static void check_wm_state(struct drm_device *dev)
  10606. {
  10607. struct drm_i915_private *dev_priv = dev->dev_private;
  10608. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  10609. struct intel_crtc *intel_crtc;
  10610. int plane;
  10611. if (INTEL_INFO(dev)->gen < 9)
  10612. return;
  10613. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  10614. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  10615. for_each_intel_crtc(dev, intel_crtc) {
  10616. struct skl_ddb_entry *hw_entry, *sw_entry;
  10617. const enum pipe pipe = intel_crtc->pipe;
  10618. if (!intel_crtc->active)
  10619. continue;
  10620. /* planes */
  10621. for_each_plane(dev_priv, pipe, plane) {
  10622. hw_entry = &hw_ddb.plane[pipe][plane];
  10623. sw_entry = &sw_ddb->plane[pipe][plane];
  10624. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10625. continue;
  10626. DRM_ERROR("mismatch in DDB state pipe %c plane %d "
  10627. "(expected (%u,%u), found (%u,%u))\n",
  10628. pipe_name(pipe), plane + 1,
  10629. sw_entry->start, sw_entry->end,
  10630. hw_entry->start, hw_entry->end);
  10631. }
  10632. /* cursor */
  10633. hw_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  10634. sw_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  10635. if (skl_ddb_entry_equal(hw_entry, sw_entry))
  10636. continue;
  10637. DRM_ERROR("mismatch in DDB state pipe %c cursor "
  10638. "(expected (%u,%u), found (%u,%u))\n",
  10639. pipe_name(pipe),
  10640. sw_entry->start, sw_entry->end,
  10641. hw_entry->start, hw_entry->end);
  10642. }
  10643. }
  10644. static void
  10645. check_connector_state(struct drm_device *dev,
  10646. struct drm_atomic_state *old_state)
  10647. {
  10648. struct drm_connector_state *old_conn_state;
  10649. struct drm_connector *connector;
  10650. int i;
  10651. for_each_connector_in_state(old_state, connector, old_conn_state, i) {
  10652. struct drm_encoder *encoder = connector->encoder;
  10653. struct drm_connector_state *state = connector->state;
  10654. /* This also checks the encoder/connector hw state with the
  10655. * ->get_hw_state callbacks. */
  10656. intel_connector_check_state(to_intel_connector(connector));
  10657. I915_STATE_WARN(state->best_encoder != encoder,
  10658. "connector's atomic encoder doesn't match legacy encoder\n");
  10659. }
  10660. }
  10661. static void
  10662. check_encoder_state(struct drm_device *dev)
  10663. {
  10664. struct intel_encoder *encoder;
  10665. struct intel_connector *connector;
  10666. for_each_intel_encoder(dev, encoder) {
  10667. bool enabled = false;
  10668. enum pipe pipe;
  10669. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10670. encoder->base.base.id,
  10671. encoder->base.name);
  10672. for_each_intel_connector(dev, connector) {
  10673. if (connector->base.state->best_encoder != &encoder->base)
  10674. continue;
  10675. enabled = true;
  10676. I915_STATE_WARN(connector->base.state->crtc !=
  10677. encoder->base.crtc,
  10678. "connector's crtc doesn't match encoder crtc\n");
  10679. }
  10680. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10681. "encoder's enabled state mismatch "
  10682. "(expected %i, found %i)\n",
  10683. !!encoder->base.crtc, enabled);
  10684. if (!encoder->base.crtc) {
  10685. bool active;
  10686. active = encoder->get_hw_state(encoder, &pipe);
  10687. I915_STATE_WARN(active,
  10688. "encoder detached but still enabled on pipe %c.\n",
  10689. pipe_name(pipe));
  10690. }
  10691. }
  10692. }
  10693. static void
  10694. check_crtc_state(struct drm_device *dev, struct drm_atomic_state *old_state)
  10695. {
  10696. struct drm_i915_private *dev_priv = dev->dev_private;
  10697. struct intel_encoder *encoder;
  10698. struct drm_crtc_state *old_crtc_state;
  10699. struct drm_crtc *crtc;
  10700. int i;
  10701. for_each_crtc_in_state(old_state, crtc, old_crtc_state, i) {
  10702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10703. struct intel_crtc_state *pipe_config, *sw_config;
  10704. bool active;
  10705. if (!needs_modeset(crtc->state) &&
  10706. !to_intel_crtc_state(crtc->state)->update_pipe)
  10707. continue;
  10708. __drm_atomic_helper_crtc_destroy_state(crtc, old_crtc_state);
  10709. pipe_config = to_intel_crtc_state(old_crtc_state);
  10710. memset(pipe_config, 0, sizeof(*pipe_config));
  10711. pipe_config->base.crtc = crtc;
  10712. pipe_config->base.state = old_state;
  10713. DRM_DEBUG_KMS("[CRTC:%d]\n",
  10714. crtc->base.id);
  10715. active = dev_priv->display.get_pipe_config(intel_crtc,
  10716. pipe_config);
  10717. /* hw state is inconsistent with the pipe quirk */
  10718. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10719. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10720. active = crtc->state->active;
  10721. I915_STATE_WARN(crtc->state->active != active,
  10722. "crtc active state doesn't match with hw state "
  10723. "(expected %i, found %i)\n", crtc->state->active, active);
  10724. I915_STATE_WARN(intel_crtc->active != crtc->state->active,
  10725. "transitional active state does not match atomic hw state "
  10726. "(expected %i, found %i)\n", crtc->state->active, intel_crtc->active);
  10727. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10728. enum pipe pipe;
  10729. active = encoder->get_hw_state(encoder, &pipe);
  10730. I915_STATE_WARN(active != crtc->state->active,
  10731. "[ENCODER:%i] active %i with crtc active %i\n",
  10732. encoder->base.base.id, active, crtc->state->active);
  10733. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10734. "Encoder connected to wrong pipe %c\n",
  10735. pipe_name(pipe));
  10736. if (active)
  10737. encoder->get_config(encoder, pipe_config);
  10738. }
  10739. if (!crtc->state->active)
  10740. continue;
  10741. sw_config = to_intel_crtc_state(crtc->state);
  10742. if (!intel_pipe_config_compare(dev, sw_config,
  10743. pipe_config, false)) {
  10744. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10745. intel_dump_pipe_config(intel_crtc, pipe_config,
  10746. "[hw state]");
  10747. intel_dump_pipe_config(intel_crtc, sw_config,
  10748. "[sw state]");
  10749. }
  10750. }
  10751. }
  10752. static void
  10753. check_shared_dpll_state(struct drm_device *dev)
  10754. {
  10755. struct drm_i915_private *dev_priv = dev->dev_private;
  10756. struct intel_crtc *crtc;
  10757. struct intel_dpll_hw_state dpll_hw_state;
  10758. int i;
  10759. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10760. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10761. int enabled_crtcs = 0, active_crtcs = 0;
  10762. bool active;
  10763. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10764. DRM_DEBUG_KMS("%s\n", pll->name);
  10765. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  10766. I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
  10767. "more active pll users than references: %i vs %i\n",
  10768. pll->active, hweight32(pll->config.crtc_mask));
  10769. I915_STATE_WARN(pll->active && !pll->on,
  10770. "pll in active use but not on in sw tracking\n");
  10771. I915_STATE_WARN(pll->on && !pll->active,
  10772. "pll in on but not on in use in sw tracking\n");
  10773. I915_STATE_WARN(pll->on != active,
  10774. "pll on state mismatch (expected %i, found %i)\n",
  10775. pll->on, active);
  10776. for_each_intel_crtc(dev, crtc) {
  10777. if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
  10778. enabled_crtcs++;
  10779. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10780. active_crtcs++;
  10781. }
  10782. I915_STATE_WARN(pll->active != active_crtcs,
  10783. "pll active crtcs mismatch (expected %i, found %i)\n",
  10784. pll->active, active_crtcs);
  10785. I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
  10786. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  10787. hweight32(pll->config.crtc_mask), enabled_crtcs);
  10788. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
  10789. sizeof(dpll_hw_state)),
  10790. "pll hw state mismatch\n");
  10791. }
  10792. }
  10793. static void
  10794. intel_modeset_check_state(struct drm_device *dev,
  10795. struct drm_atomic_state *old_state)
  10796. {
  10797. check_wm_state(dev);
  10798. check_connector_state(dev, old_state);
  10799. check_encoder_state(dev);
  10800. check_crtc_state(dev, old_state);
  10801. check_shared_dpll_state(dev);
  10802. }
  10803. void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
  10804. int dotclock)
  10805. {
  10806. /*
  10807. * FDI already provided one idea for the dotclock.
  10808. * Yell if the encoder disagrees.
  10809. */
  10810. WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
  10811. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  10812. pipe_config->base.adjusted_mode.crtc_clock, dotclock);
  10813. }
  10814. static void update_scanline_offset(struct intel_crtc *crtc)
  10815. {
  10816. struct drm_device *dev = crtc->base.dev;
  10817. /*
  10818. * The scanline counter increments at the leading edge of hsync.
  10819. *
  10820. * On most platforms it starts counting from vtotal-1 on the
  10821. * first active line. That means the scanline counter value is
  10822. * always one less than what we would expect. Ie. just after
  10823. * start of vblank, which also occurs at start of hsync (on the
  10824. * last active line), the scanline counter will read vblank_start-1.
  10825. *
  10826. * On gen2 the scanline counter starts counting from 1 instead
  10827. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10828. * to keep the value positive), instead of adding one.
  10829. *
  10830. * On HSW+ the behaviour of the scanline counter depends on the output
  10831. * type. For DP ports it behaves like most other platforms, but on HDMI
  10832. * there's an extra 1 line difference. So we need to add two instead of
  10833. * one to the value.
  10834. */
  10835. if (IS_GEN2(dev)) {
  10836. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10837. int vtotal;
  10838. vtotal = adjusted_mode->crtc_vtotal;
  10839. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10840. vtotal /= 2;
  10841. crtc->scanline_offset = vtotal - 1;
  10842. } else if (HAS_DDI(dev) &&
  10843. intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
  10844. crtc->scanline_offset = 2;
  10845. } else
  10846. crtc->scanline_offset = 1;
  10847. }
  10848. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10849. {
  10850. struct drm_device *dev = state->dev;
  10851. struct drm_i915_private *dev_priv = to_i915(dev);
  10852. struct intel_shared_dpll_config *shared_dpll = NULL;
  10853. struct intel_crtc *intel_crtc;
  10854. struct intel_crtc_state *intel_crtc_state;
  10855. struct drm_crtc *crtc;
  10856. struct drm_crtc_state *crtc_state;
  10857. int i;
  10858. if (!dev_priv->display.crtc_compute_clock)
  10859. return;
  10860. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10861. int dpll;
  10862. intel_crtc = to_intel_crtc(crtc);
  10863. intel_crtc_state = to_intel_crtc_state(crtc_state);
  10864. dpll = intel_crtc_state->shared_dpll;
  10865. if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
  10866. continue;
  10867. intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
  10868. if (!shared_dpll)
  10869. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  10870. shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
  10871. }
  10872. }
  10873. /*
  10874. * This implements the workaround described in the "notes" section of the mode
  10875. * set sequence documentation. When going from no pipes or single pipe to
  10876. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10877. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10878. */
  10879. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10880. {
  10881. struct drm_crtc_state *crtc_state;
  10882. struct intel_crtc *intel_crtc;
  10883. struct drm_crtc *crtc;
  10884. struct intel_crtc_state *first_crtc_state = NULL;
  10885. struct intel_crtc_state *other_crtc_state = NULL;
  10886. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10887. int i;
  10888. /* look at all crtc's that are going to be enabled in during modeset */
  10889. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10890. intel_crtc = to_intel_crtc(crtc);
  10891. if (!crtc_state->active || !needs_modeset(crtc_state))
  10892. continue;
  10893. if (first_crtc_state) {
  10894. other_crtc_state = to_intel_crtc_state(crtc_state);
  10895. break;
  10896. } else {
  10897. first_crtc_state = to_intel_crtc_state(crtc_state);
  10898. first_pipe = intel_crtc->pipe;
  10899. }
  10900. }
  10901. /* No workaround needed? */
  10902. if (!first_crtc_state)
  10903. return 0;
  10904. /* w/a possibly needed, check how many crtc's are already enabled. */
  10905. for_each_intel_crtc(state->dev, intel_crtc) {
  10906. struct intel_crtc_state *pipe_config;
  10907. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10908. if (IS_ERR(pipe_config))
  10909. return PTR_ERR(pipe_config);
  10910. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10911. if (!pipe_config->base.active ||
  10912. needs_modeset(&pipe_config->base))
  10913. continue;
  10914. /* 2 or more enabled crtcs means no need for w/a */
  10915. if (enabled_pipe != INVALID_PIPE)
  10916. return 0;
  10917. enabled_pipe = intel_crtc->pipe;
  10918. }
  10919. if (enabled_pipe != INVALID_PIPE)
  10920. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10921. else if (other_crtc_state)
  10922. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10923. return 0;
  10924. }
  10925. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10926. {
  10927. struct drm_crtc *crtc;
  10928. struct drm_crtc_state *crtc_state;
  10929. int ret = 0;
  10930. /* add all active pipes to the state */
  10931. for_each_crtc(state->dev, crtc) {
  10932. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10933. if (IS_ERR(crtc_state))
  10934. return PTR_ERR(crtc_state);
  10935. if (!crtc_state->active || needs_modeset(crtc_state))
  10936. continue;
  10937. crtc_state->mode_changed = true;
  10938. ret = drm_atomic_add_affected_connectors(state, crtc);
  10939. if (ret)
  10940. break;
  10941. ret = drm_atomic_add_affected_planes(state, crtc);
  10942. if (ret)
  10943. break;
  10944. }
  10945. return ret;
  10946. }
  10947. static int intel_modeset_checks(struct drm_atomic_state *state)
  10948. {
  10949. struct drm_device *dev = state->dev;
  10950. struct drm_i915_private *dev_priv = dev->dev_private;
  10951. int ret;
  10952. if (!check_digital_port_conflicts(state)) {
  10953. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10954. return -EINVAL;
  10955. }
  10956. /*
  10957. * See if the config requires any additional preparation, e.g.
  10958. * to adjust global state with pipes off. We need to do this
  10959. * here so we can get the modeset_pipe updated config for the new
  10960. * mode set on this crtc. For other crtcs we need to use the
  10961. * adjusted_mode bits in the crtc directly.
  10962. */
  10963. if (dev_priv->display.modeset_calc_cdclk) {
  10964. unsigned int cdclk;
  10965. ret = dev_priv->display.modeset_calc_cdclk(state);
  10966. cdclk = to_intel_atomic_state(state)->cdclk;
  10967. if (!ret && cdclk != dev_priv->cdclk_freq)
  10968. ret = intel_modeset_all_pipes(state);
  10969. if (ret < 0)
  10970. return ret;
  10971. } else
  10972. to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
  10973. intel_modeset_clear_plls(state);
  10974. if (IS_HASWELL(dev))
  10975. return haswell_mode_set_planes_workaround(state);
  10976. return 0;
  10977. }
  10978. /*
  10979. * Handle calculation of various watermark data at the end of the atomic check
  10980. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10981. * handlers to ensure that all derived state has been updated.
  10982. */
  10983. static void calc_watermark_data(struct drm_atomic_state *state)
  10984. {
  10985. struct drm_device *dev = state->dev;
  10986. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10987. struct drm_crtc *crtc;
  10988. struct drm_crtc_state *cstate;
  10989. struct drm_plane *plane;
  10990. struct drm_plane_state *pstate;
  10991. /*
  10992. * Calculate watermark configuration details now that derived
  10993. * plane/crtc state is all properly updated.
  10994. */
  10995. drm_for_each_crtc(crtc, dev) {
  10996. cstate = drm_atomic_get_existing_crtc_state(state, crtc) ?:
  10997. crtc->state;
  10998. if (cstate->active)
  10999. intel_state->wm_config.num_pipes_active++;
  11000. }
  11001. drm_for_each_legacy_plane(plane, dev) {
  11002. pstate = drm_atomic_get_existing_plane_state(state, plane) ?:
  11003. plane->state;
  11004. if (!to_intel_plane_state(pstate)->visible)
  11005. continue;
  11006. intel_state->wm_config.sprites_enabled = true;
  11007. if (pstate->crtc_w != pstate->src_w >> 16 ||
  11008. pstate->crtc_h != pstate->src_h >> 16)
  11009. intel_state->wm_config.sprites_scaled = true;
  11010. }
  11011. }
  11012. /**
  11013. * intel_atomic_check - validate state object
  11014. * @dev: drm device
  11015. * @state: state to validate
  11016. */
  11017. static int intel_atomic_check(struct drm_device *dev,
  11018. struct drm_atomic_state *state)
  11019. {
  11020. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11021. struct drm_crtc *crtc;
  11022. struct drm_crtc_state *crtc_state;
  11023. int ret, i;
  11024. bool any_ms = false;
  11025. ret = drm_atomic_helper_check_modeset(dev, state);
  11026. if (ret)
  11027. return ret;
  11028. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11029. struct intel_crtc_state *pipe_config =
  11030. to_intel_crtc_state(crtc_state);
  11031. /* Catch I915_MODE_FLAG_INHERITED */
  11032. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11033. crtc_state->mode_changed = true;
  11034. if (!crtc_state->enable) {
  11035. if (needs_modeset(crtc_state))
  11036. any_ms = true;
  11037. continue;
  11038. }
  11039. if (!needs_modeset(crtc_state))
  11040. continue;
  11041. /* FIXME: For only active_changed we shouldn't need to do any
  11042. * state recomputation at all. */
  11043. ret = drm_atomic_add_affected_connectors(state, crtc);
  11044. if (ret)
  11045. return ret;
  11046. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11047. if (ret)
  11048. return ret;
  11049. if (intel_pipe_config_compare(state->dev,
  11050. to_intel_crtc_state(crtc->state),
  11051. pipe_config, true)) {
  11052. crtc_state->mode_changed = false;
  11053. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11054. }
  11055. if (needs_modeset(crtc_state)) {
  11056. any_ms = true;
  11057. ret = drm_atomic_add_affected_planes(state, crtc);
  11058. if (ret)
  11059. return ret;
  11060. }
  11061. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11062. needs_modeset(crtc_state) ?
  11063. "[modeset]" : "[fastset]");
  11064. }
  11065. if (any_ms) {
  11066. ret = intel_modeset_checks(state);
  11067. if (ret)
  11068. return ret;
  11069. } else
  11070. intel_state->cdclk = to_i915(state->dev)->cdclk_freq;
  11071. ret = drm_atomic_helper_check_planes(state->dev, state);
  11072. if (ret)
  11073. return ret;
  11074. calc_watermark_data(state);
  11075. return 0;
  11076. }
  11077. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11078. struct drm_atomic_state *state,
  11079. bool async)
  11080. {
  11081. struct drm_i915_private *dev_priv = dev->dev_private;
  11082. struct drm_plane_state *plane_state;
  11083. struct drm_crtc_state *crtc_state;
  11084. struct drm_plane *plane;
  11085. struct drm_crtc *crtc;
  11086. int i, ret;
  11087. if (async) {
  11088. DRM_DEBUG_KMS("i915 does not yet support async commit\n");
  11089. return -EINVAL;
  11090. }
  11091. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11092. ret = intel_crtc_wait_for_pending_flips(crtc);
  11093. if (ret)
  11094. return ret;
  11095. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11096. flush_workqueue(dev_priv->wq);
  11097. }
  11098. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11099. if (ret)
  11100. return ret;
  11101. ret = drm_atomic_helper_prepare_planes(dev, state);
  11102. if (!ret && !async && !i915_reset_in_progress(&dev_priv->gpu_error)) {
  11103. u32 reset_counter;
  11104. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  11105. mutex_unlock(&dev->struct_mutex);
  11106. for_each_plane_in_state(state, plane, plane_state, i) {
  11107. struct intel_plane_state *intel_plane_state =
  11108. to_intel_plane_state(plane_state);
  11109. if (!intel_plane_state->wait_req)
  11110. continue;
  11111. ret = __i915_wait_request(intel_plane_state->wait_req,
  11112. reset_counter, true,
  11113. NULL, NULL);
  11114. /* Swallow -EIO errors to allow updates during hw lockup. */
  11115. if (ret == -EIO)
  11116. ret = 0;
  11117. if (ret)
  11118. break;
  11119. }
  11120. if (!ret)
  11121. return 0;
  11122. mutex_lock(&dev->struct_mutex);
  11123. drm_atomic_helper_cleanup_planes(dev, state);
  11124. }
  11125. mutex_unlock(&dev->struct_mutex);
  11126. return ret;
  11127. }
  11128. /**
  11129. * intel_atomic_commit - commit validated state object
  11130. * @dev: DRM device
  11131. * @state: the top-level driver state object
  11132. * @async: asynchronous commit
  11133. *
  11134. * This function commits a top-level state object that has been validated
  11135. * with drm_atomic_helper_check().
  11136. *
  11137. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  11138. * we can only handle plane-related operations and do not yet support
  11139. * asynchronous commit.
  11140. *
  11141. * RETURNS
  11142. * Zero for success or -errno.
  11143. */
  11144. static int intel_atomic_commit(struct drm_device *dev,
  11145. struct drm_atomic_state *state,
  11146. bool async)
  11147. {
  11148. struct drm_i915_private *dev_priv = dev->dev_private;
  11149. struct drm_crtc_state *crtc_state;
  11150. struct drm_crtc *crtc;
  11151. int ret = 0;
  11152. int i;
  11153. bool any_ms = false;
  11154. ret = intel_atomic_prepare_commit(dev, state, async);
  11155. if (ret) {
  11156. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  11157. return ret;
  11158. }
  11159. drm_atomic_helper_swap_state(dev, state);
  11160. dev_priv->wm.config = to_intel_atomic_state(state)->wm_config;
  11161. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11162. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11163. if (!needs_modeset(crtc->state))
  11164. continue;
  11165. any_ms = true;
  11166. intel_pre_plane_update(intel_crtc);
  11167. if (crtc_state->active) {
  11168. intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
  11169. dev_priv->display.crtc_disable(crtc);
  11170. intel_crtc->active = false;
  11171. intel_disable_shared_dpll(intel_crtc);
  11172. }
  11173. }
  11174. /* Only after disabling all output pipelines that will be changed can we
  11175. * update the the output configuration. */
  11176. intel_modeset_update_crtc_state(state);
  11177. if (any_ms) {
  11178. intel_shared_dpll_commit(state);
  11179. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  11180. modeset_update_crtc_power_domains(state);
  11181. }
  11182. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  11183. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11184. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11185. bool modeset = needs_modeset(crtc->state);
  11186. bool update_pipe = !modeset &&
  11187. to_intel_crtc_state(crtc->state)->update_pipe;
  11188. unsigned long put_domains = 0;
  11189. if (modeset)
  11190. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  11191. if (modeset && crtc->state->active) {
  11192. update_scanline_offset(to_intel_crtc(crtc));
  11193. dev_priv->display.crtc_enable(crtc);
  11194. }
  11195. if (update_pipe) {
  11196. put_domains = modeset_get_crtc_power_domains(crtc);
  11197. /* make sure intel_modeset_check_state runs */
  11198. any_ms = true;
  11199. }
  11200. if (!modeset)
  11201. intel_pre_plane_update(intel_crtc);
  11202. if (crtc->state->active &&
  11203. (crtc->state->planes_changed || update_pipe))
  11204. drm_atomic_helper_commit_planes_on_crtc(crtc_state);
  11205. if (put_domains)
  11206. modeset_put_power_domains(dev_priv, put_domains);
  11207. intel_post_plane_update(intel_crtc);
  11208. if (modeset)
  11209. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  11210. }
  11211. /* FIXME: add subpixel order */
  11212. drm_atomic_helper_wait_for_vblanks(dev, state);
  11213. mutex_lock(&dev->struct_mutex);
  11214. drm_atomic_helper_cleanup_planes(dev, state);
  11215. mutex_unlock(&dev->struct_mutex);
  11216. if (any_ms)
  11217. intel_modeset_check_state(dev, state);
  11218. drm_atomic_state_free(state);
  11219. return 0;
  11220. }
  11221. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  11222. {
  11223. struct drm_device *dev = crtc->dev;
  11224. struct drm_atomic_state *state;
  11225. struct drm_crtc_state *crtc_state;
  11226. int ret;
  11227. state = drm_atomic_state_alloc(dev);
  11228. if (!state) {
  11229. DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
  11230. crtc->base.id);
  11231. return;
  11232. }
  11233. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  11234. retry:
  11235. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11236. ret = PTR_ERR_OR_ZERO(crtc_state);
  11237. if (!ret) {
  11238. if (!crtc_state->active)
  11239. goto out;
  11240. crtc_state->mode_changed = true;
  11241. ret = drm_atomic_commit(state);
  11242. }
  11243. if (ret == -EDEADLK) {
  11244. drm_atomic_state_clear(state);
  11245. drm_modeset_backoff(state->acquire_ctx);
  11246. goto retry;
  11247. }
  11248. if (ret)
  11249. out:
  11250. drm_atomic_state_free(state);
  11251. }
  11252. #undef for_each_intel_crtc_masked
  11253. static const struct drm_crtc_funcs intel_crtc_funcs = {
  11254. .gamma_set = intel_crtc_gamma_set,
  11255. .set_config = drm_atomic_helper_set_config,
  11256. .destroy = intel_crtc_destroy,
  11257. .page_flip = intel_crtc_page_flip,
  11258. .atomic_duplicate_state = intel_crtc_duplicate_state,
  11259. .atomic_destroy_state = intel_crtc_destroy_state,
  11260. };
  11261. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  11262. struct intel_shared_dpll *pll,
  11263. struct intel_dpll_hw_state *hw_state)
  11264. {
  11265. uint32_t val;
  11266. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
  11267. return false;
  11268. val = I915_READ(PCH_DPLL(pll->id));
  11269. hw_state->dpll = val;
  11270. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  11271. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  11272. return val & DPLL_VCO_ENABLE;
  11273. }
  11274. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  11275. struct intel_shared_dpll *pll)
  11276. {
  11277. I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
  11278. I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
  11279. }
  11280. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  11281. struct intel_shared_dpll *pll)
  11282. {
  11283. /* PCH refclock must be enabled first */
  11284. ibx_assert_pch_refclk_enabled(dev_priv);
  11285. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11286. /* Wait for the clocks to stabilize. */
  11287. POSTING_READ(PCH_DPLL(pll->id));
  11288. udelay(150);
  11289. /* The pixel multiplier can only be updated once the
  11290. * DPLL is enabled and the clocks are stable.
  11291. *
  11292. * So write it again.
  11293. */
  11294. I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
  11295. POSTING_READ(PCH_DPLL(pll->id));
  11296. udelay(200);
  11297. }
  11298. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  11299. struct intel_shared_dpll *pll)
  11300. {
  11301. struct drm_device *dev = dev_priv->dev;
  11302. struct intel_crtc *crtc;
  11303. /* Make sure no transcoder isn't still depending on us. */
  11304. for_each_intel_crtc(dev, crtc) {
  11305. if (intel_crtc_to_shared_dpll(crtc) == pll)
  11306. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  11307. }
  11308. I915_WRITE(PCH_DPLL(pll->id), 0);
  11309. POSTING_READ(PCH_DPLL(pll->id));
  11310. udelay(200);
  11311. }
  11312. static char *ibx_pch_dpll_names[] = {
  11313. "PCH DPLL A",
  11314. "PCH DPLL B",
  11315. };
  11316. static void ibx_pch_dpll_init(struct drm_device *dev)
  11317. {
  11318. struct drm_i915_private *dev_priv = dev->dev_private;
  11319. int i;
  11320. dev_priv->num_shared_dpll = 2;
  11321. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  11322. dev_priv->shared_dplls[i].id = i;
  11323. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  11324. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  11325. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  11326. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  11327. dev_priv->shared_dplls[i].get_hw_state =
  11328. ibx_pch_dpll_get_hw_state;
  11329. }
  11330. }
  11331. static void intel_shared_dpll_init(struct drm_device *dev)
  11332. {
  11333. struct drm_i915_private *dev_priv = dev->dev_private;
  11334. if (HAS_DDI(dev))
  11335. intel_ddi_pll_init(dev);
  11336. else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  11337. ibx_pch_dpll_init(dev);
  11338. else
  11339. dev_priv->num_shared_dpll = 0;
  11340. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  11341. }
  11342. /**
  11343. * intel_prepare_plane_fb - Prepare fb for usage on plane
  11344. * @plane: drm plane to prepare for
  11345. * @fb: framebuffer to prepare for presentation
  11346. *
  11347. * Prepares a framebuffer for usage on a display plane. Generally this
  11348. * involves pinning the underlying object and updating the frontbuffer tracking
  11349. * bits. Some older platforms need special physical address handling for
  11350. * cursor planes.
  11351. *
  11352. * Must be called with struct_mutex held.
  11353. *
  11354. * Returns 0 on success, negative error code on failure.
  11355. */
  11356. int
  11357. intel_prepare_plane_fb(struct drm_plane *plane,
  11358. const struct drm_plane_state *new_state)
  11359. {
  11360. struct drm_device *dev = plane->dev;
  11361. struct drm_framebuffer *fb = new_state->fb;
  11362. struct intel_plane *intel_plane = to_intel_plane(plane);
  11363. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11364. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  11365. int ret = 0;
  11366. if (!obj && !old_obj)
  11367. return 0;
  11368. if (old_obj) {
  11369. struct drm_crtc_state *crtc_state =
  11370. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  11371. /* Big Hammer, we also need to ensure that any pending
  11372. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11373. * current scanout is retired before unpinning the old
  11374. * framebuffer. Note that we rely on userspace rendering
  11375. * into the buffer attached to the pipe they are waiting
  11376. * on. If not, userspace generates a GPU hang with IPEHR
  11377. * point to the MI_WAIT_FOR_EVENT.
  11378. *
  11379. * This should only fail upon a hung GPU, in which case we
  11380. * can safely continue.
  11381. */
  11382. if (needs_modeset(crtc_state))
  11383. ret = i915_gem_object_wait_rendering(old_obj, true);
  11384. /* Swallow -EIO errors to allow updates during hw lockup. */
  11385. if (ret && ret != -EIO)
  11386. return ret;
  11387. }
  11388. if (!obj) {
  11389. ret = 0;
  11390. } else if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  11391. INTEL_INFO(dev)->cursor_needs_physical) {
  11392. int align = IS_I830(dev) ? 16 * 1024 : 256;
  11393. ret = i915_gem_object_attach_phys(obj, align);
  11394. if (ret)
  11395. DRM_DEBUG_KMS("failed to attach phys object\n");
  11396. } else {
  11397. ret = intel_pin_and_fence_fb_obj(plane, fb, new_state);
  11398. }
  11399. if (ret == 0) {
  11400. if (obj) {
  11401. struct intel_plane_state *plane_state =
  11402. to_intel_plane_state(new_state);
  11403. i915_gem_request_assign(&plane_state->wait_req,
  11404. obj->last_write_req);
  11405. }
  11406. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11407. }
  11408. return ret;
  11409. }
  11410. /**
  11411. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11412. * @plane: drm plane to clean up for
  11413. * @fb: old framebuffer that was on plane
  11414. *
  11415. * Cleans up a framebuffer that has just been removed from a plane.
  11416. *
  11417. * Must be called with struct_mutex held.
  11418. */
  11419. void
  11420. intel_cleanup_plane_fb(struct drm_plane *plane,
  11421. const struct drm_plane_state *old_state)
  11422. {
  11423. struct drm_device *dev = plane->dev;
  11424. struct intel_plane *intel_plane = to_intel_plane(plane);
  11425. struct intel_plane_state *old_intel_state;
  11426. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  11427. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  11428. old_intel_state = to_intel_plane_state(old_state);
  11429. if (!obj && !old_obj)
  11430. return;
  11431. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  11432. !INTEL_INFO(dev)->cursor_needs_physical))
  11433. intel_unpin_fb_obj(old_state->fb, old_state);
  11434. /* prepare_fb aborted? */
  11435. if ((old_obj && (old_obj->frontbuffer_bits & intel_plane->frontbuffer_bit)) ||
  11436. (obj && !(obj->frontbuffer_bits & intel_plane->frontbuffer_bit)))
  11437. i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
  11438. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  11439. }
  11440. int
  11441. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11442. {
  11443. int max_scale;
  11444. struct drm_device *dev;
  11445. struct drm_i915_private *dev_priv;
  11446. int crtc_clock, cdclk;
  11447. if (!intel_crtc || !crtc_state)
  11448. return DRM_PLANE_HELPER_NO_SCALING;
  11449. dev = intel_crtc->base.dev;
  11450. dev_priv = dev->dev_private;
  11451. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11452. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  11453. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  11454. return DRM_PLANE_HELPER_NO_SCALING;
  11455. /*
  11456. * skl max scale is lower of:
  11457. * close to 3 but not 3, -1 is for that purpose
  11458. * or
  11459. * cdclk/crtc_clock
  11460. */
  11461. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  11462. return max_scale;
  11463. }
  11464. static int
  11465. intel_check_primary_plane(struct drm_plane *plane,
  11466. struct intel_crtc_state *crtc_state,
  11467. struct intel_plane_state *state)
  11468. {
  11469. struct drm_crtc *crtc = state->base.crtc;
  11470. struct drm_framebuffer *fb = state->base.fb;
  11471. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11472. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11473. bool can_position = false;
  11474. /* use scaler when colorkey is not required */
  11475. if (INTEL_INFO(plane->dev)->gen >= 9 &&
  11476. state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11477. min_scale = 1;
  11478. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11479. can_position = true;
  11480. }
  11481. return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11482. &state->dst, &state->clip,
  11483. min_scale, max_scale,
  11484. can_position, true,
  11485. &state->visible);
  11486. }
  11487. static void
  11488. intel_commit_primary_plane(struct drm_plane *plane,
  11489. struct intel_plane_state *state)
  11490. {
  11491. struct drm_crtc *crtc = state->base.crtc;
  11492. struct drm_framebuffer *fb = state->base.fb;
  11493. struct drm_device *dev = plane->dev;
  11494. struct drm_i915_private *dev_priv = dev->dev_private;
  11495. crtc = crtc ? crtc : plane->crtc;
  11496. dev_priv->display.update_primary_plane(crtc, fb,
  11497. state->src.x1 >> 16,
  11498. state->src.y1 >> 16);
  11499. }
  11500. static void
  11501. intel_disable_primary_plane(struct drm_plane *plane,
  11502. struct drm_crtc *crtc)
  11503. {
  11504. struct drm_device *dev = plane->dev;
  11505. struct drm_i915_private *dev_priv = dev->dev_private;
  11506. dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
  11507. }
  11508. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11509. struct drm_crtc_state *old_crtc_state)
  11510. {
  11511. struct drm_device *dev = crtc->dev;
  11512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11513. struct intel_crtc_state *old_intel_state =
  11514. to_intel_crtc_state(old_crtc_state);
  11515. bool modeset = needs_modeset(crtc->state);
  11516. if (intel_crtc->atomic.update_wm_pre)
  11517. intel_update_watermarks(crtc);
  11518. /* Perform vblank evasion around commit operation */
  11519. intel_pipe_update_start(intel_crtc);
  11520. if (modeset)
  11521. return;
  11522. if (to_intel_crtc_state(crtc->state)->update_pipe)
  11523. intel_update_pipe_config(intel_crtc, old_intel_state);
  11524. else if (INTEL_INFO(dev)->gen >= 9)
  11525. skl_detach_scalers(intel_crtc);
  11526. }
  11527. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11528. struct drm_crtc_state *old_crtc_state)
  11529. {
  11530. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11531. intel_pipe_update_end(intel_crtc);
  11532. }
  11533. /**
  11534. * intel_plane_destroy - destroy a plane
  11535. * @plane: plane to destroy
  11536. *
  11537. * Common destruction function for all types of planes (primary, cursor,
  11538. * sprite).
  11539. */
  11540. void intel_plane_destroy(struct drm_plane *plane)
  11541. {
  11542. struct intel_plane *intel_plane = to_intel_plane(plane);
  11543. drm_plane_cleanup(plane);
  11544. kfree(intel_plane);
  11545. }
  11546. const struct drm_plane_funcs intel_plane_funcs = {
  11547. .update_plane = drm_atomic_helper_update_plane,
  11548. .disable_plane = drm_atomic_helper_disable_plane,
  11549. .destroy = intel_plane_destroy,
  11550. .set_property = drm_atomic_helper_plane_set_property,
  11551. .atomic_get_property = intel_plane_atomic_get_property,
  11552. .atomic_set_property = intel_plane_atomic_set_property,
  11553. .atomic_duplicate_state = intel_plane_duplicate_state,
  11554. .atomic_destroy_state = intel_plane_destroy_state,
  11555. };
  11556. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  11557. int pipe)
  11558. {
  11559. struct intel_plane *primary;
  11560. struct intel_plane_state *state;
  11561. const uint32_t *intel_primary_formats;
  11562. unsigned int num_formats;
  11563. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11564. if (primary == NULL)
  11565. return NULL;
  11566. state = intel_create_plane_state(&primary->base);
  11567. if (!state) {
  11568. kfree(primary);
  11569. return NULL;
  11570. }
  11571. primary->base.state = &state->base;
  11572. primary->can_scale = false;
  11573. primary->max_downscale = 1;
  11574. if (INTEL_INFO(dev)->gen >= 9) {
  11575. primary->can_scale = true;
  11576. state->scaler_id = -1;
  11577. }
  11578. primary->pipe = pipe;
  11579. primary->plane = pipe;
  11580. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11581. primary->check_plane = intel_check_primary_plane;
  11582. primary->commit_plane = intel_commit_primary_plane;
  11583. primary->disable_plane = intel_disable_primary_plane;
  11584. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  11585. primary->plane = !pipe;
  11586. if (INTEL_INFO(dev)->gen >= 9) {
  11587. intel_primary_formats = skl_primary_formats;
  11588. num_formats = ARRAY_SIZE(skl_primary_formats);
  11589. } else if (INTEL_INFO(dev)->gen >= 4) {
  11590. intel_primary_formats = i965_primary_formats;
  11591. num_formats = ARRAY_SIZE(i965_primary_formats);
  11592. } else {
  11593. intel_primary_formats = i8xx_primary_formats;
  11594. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11595. }
  11596. drm_universal_plane_init(dev, &primary->base, 0,
  11597. &intel_plane_funcs,
  11598. intel_primary_formats, num_formats,
  11599. DRM_PLANE_TYPE_PRIMARY);
  11600. if (INTEL_INFO(dev)->gen >= 4)
  11601. intel_create_rotation_property(dev, primary);
  11602. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11603. return &primary->base;
  11604. }
  11605. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  11606. {
  11607. if (!dev->mode_config.rotation_property) {
  11608. unsigned long flags = BIT(DRM_ROTATE_0) |
  11609. BIT(DRM_ROTATE_180);
  11610. if (INTEL_INFO(dev)->gen >= 9)
  11611. flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
  11612. dev->mode_config.rotation_property =
  11613. drm_mode_create_rotation_property(dev, flags);
  11614. }
  11615. if (dev->mode_config.rotation_property)
  11616. drm_object_attach_property(&plane->base.base,
  11617. dev->mode_config.rotation_property,
  11618. plane->base.state->rotation);
  11619. }
  11620. static int
  11621. intel_check_cursor_plane(struct drm_plane *plane,
  11622. struct intel_crtc_state *crtc_state,
  11623. struct intel_plane_state *state)
  11624. {
  11625. struct drm_crtc *crtc = crtc_state->base.crtc;
  11626. struct drm_framebuffer *fb = state->base.fb;
  11627. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11628. unsigned stride;
  11629. int ret;
  11630. ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
  11631. &state->dst, &state->clip,
  11632. DRM_PLANE_HELPER_NO_SCALING,
  11633. DRM_PLANE_HELPER_NO_SCALING,
  11634. true, true, &state->visible);
  11635. if (ret)
  11636. return ret;
  11637. /* if we want to turn off the cursor ignore width and height */
  11638. if (!obj)
  11639. return 0;
  11640. /* Check for which cursor types we support */
  11641. if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
  11642. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11643. state->base.crtc_w, state->base.crtc_h);
  11644. return -EINVAL;
  11645. }
  11646. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11647. if (obj->base.size < stride * state->base.crtc_h) {
  11648. DRM_DEBUG_KMS("buffer is too small\n");
  11649. return -ENOMEM;
  11650. }
  11651. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  11652. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11653. return -EINVAL;
  11654. }
  11655. return 0;
  11656. }
  11657. static void
  11658. intel_disable_cursor_plane(struct drm_plane *plane,
  11659. struct drm_crtc *crtc)
  11660. {
  11661. intel_crtc_update_cursor(crtc, false);
  11662. }
  11663. static void
  11664. intel_commit_cursor_plane(struct drm_plane *plane,
  11665. struct intel_plane_state *state)
  11666. {
  11667. struct drm_crtc *crtc = state->base.crtc;
  11668. struct drm_device *dev = plane->dev;
  11669. struct intel_crtc *intel_crtc;
  11670. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11671. uint32_t addr;
  11672. crtc = crtc ? crtc : plane->crtc;
  11673. intel_crtc = to_intel_crtc(crtc);
  11674. if (intel_crtc->cursor_bo == obj)
  11675. goto update;
  11676. if (!obj)
  11677. addr = 0;
  11678. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  11679. addr = i915_gem_obj_ggtt_offset(obj);
  11680. else
  11681. addr = obj->phys_handle->busaddr;
  11682. intel_crtc->cursor_addr = addr;
  11683. intel_crtc->cursor_bo = obj;
  11684. update:
  11685. intel_crtc_update_cursor(crtc, state->visible);
  11686. }
  11687. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  11688. int pipe)
  11689. {
  11690. struct intel_plane *cursor;
  11691. struct intel_plane_state *state;
  11692. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11693. if (cursor == NULL)
  11694. return NULL;
  11695. state = intel_create_plane_state(&cursor->base);
  11696. if (!state) {
  11697. kfree(cursor);
  11698. return NULL;
  11699. }
  11700. cursor->base.state = &state->base;
  11701. cursor->can_scale = false;
  11702. cursor->max_downscale = 1;
  11703. cursor->pipe = pipe;
  11704. cursor->plane = pipe;
  11705. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11706. cursor->check_plane = intel_check_cursor_plane;
  11707. cursor->commit_plane = intel_commit_cursor_plane;
  11708. cursor->disable_plane = intel_disable_cursor_plane;
  11709. drm_universal_plane_init(dev, &cursor->base, 0,
  11710. &intel_plane_funcs,
  11711. intel_cursor_formats,
  11712. ARRAY_SIZE(intel_cursor_formats),
  11713. DRM_PLANE_TYPE_CURSOR);
  11714. if (INTEL_INFO(dev)->gen >= 4) {
  11715. if (!dev->mode_config.rotation_property)
  11716. dev->mode_config.rotation_property =
  11717. drm_mode_create_rotation_property(dev,
  11718. BIT(DRM_ROTATE_0) |
  11719. BIT(DRM_ROTATE_180));
  11720. if (dev->mode_config.rotation_property)
  11721. drm_object_attach_property(&cursor->base.base,
  11722. dev->mode_config.rotation_property,
  11723. state->base.rotation);
  11724. }
  11725. if (INTEL_INFO(dev)->gen >=9)
  11726. state->scaler_id = -1;
  11727. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11728. return &cursor->base;
  11729. }
  11730. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  11731. struct intel_crtc_state *crtc_state)
  11732. {
  11733. int i;
  11734. struct intel_scaler *intel_scaler;
  11735. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  11736. for (i = 0; i < intel_crtc->num_scalers; i++) {
  11737. intel_scaler = &scaler_state->scalers[i];
  11738. intel_scaler->in_use = 0;
  11739. intel_scaler->mode = PS_SCALER_MODE_DYN;
  11740. }
  11741. scaler_state->scaler_id = -1;
  11742. }
  11743. static void intel_crtc_init(struct drm_device *dev, int pipe)
  11744. {
  11745. struct drm_i915_private *dev_priv = dev->dev_private;
  11746. struct intel_crtc *intel_crtc;
  11747. struct intel_crtc_state *crtc_state = NULL;
  11748. struct drm_plane *primary = NULL;
  11749. struct drm_plane *cursor = NULL;
  11750. int i, ret;
  11751. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11752. if (intel_crtc == NULL)
  11753. return;
  11754. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11755. if (!crtc_state)
  11756. goto fail;
  11757. intel_crtc->config = crtc_state;
  11758. intel_crtc->base.state = &crtc_state->base;
  11759. crtc_state->base.crtc = &intel_crtc->base;
  11760. /* initialize shared scalers */
  11761. if (INTEL_INFO(dev)->gen >= 9) {
  11762. if (pipe == PIPE_C)
  11763. intel_crtc->num_scalers = 1;
  11764. else
  11765. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  11766. skl_init_scalers(dev, intel_crtc, crtc_state);
  11767. }
  11768. primary = intel_primary_plane_create(dev, pipe);
  11769. if (!primary)
  11770. goto fail;
  11771. cursor = intel_cursor_plane_create(dev, pipe);
  11772. if (!cursor)
  11773. goto fail;
  11774. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  11775. cursor, &intel_crtc_funcs);
  11776. if (ret)
  11777. goto fail;
  11778. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  11779. for (i = 0; i < 256; i++) {
  11780. intel_crtc->lut_r[i] = i;
  11781. intel_crtc->lut_g[i] = i;
  11782. intel_crtc->lut_b[i] = i;
  11783. }
  11784. /*
  11785. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  11786. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  11787. */
  11788. intel_crtc->pipe = pipe;
  11789. intel_crtc->plane = pipe;
  11790. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  11791. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  11792. intel_crtc->plane = !pipe;
  11793. }
  11794. intel_crtc->cursor_base = ~0;
  11795. intel_crtc->cursor_cntl = ~0;
  11796. intel_crtc->cursor_size = ~0;
  11797. intel_crtc->wm.cxsr_allowed = true;
  11798. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11799. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11800. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  11801. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  11802. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11803. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11804. return;
  11805. fail:
  11806. if (primary)
  11807. drm_plane_cleanup(primary);
  11808. if (cursor)
  11809. drm_plane_cleanup(cursor);
  11810. kfree(crtc_state);
  11811. kfree(intel_crtc);
  11812. }
  11813. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11814. {
  11815. struct drm_encoder *encoder = connector->base.encoder;
  11816. struct drm_device *dev = connector->base.dev;
  11817. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11818. if (!encoder || WARN_ON(!encoder->crtc))
  11819. return INVALID_PIPE;
  11820. return to_intel_crtc(encoder->crtc)->pipe;
  11821. }
  11822. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11823. struct drm_file *file)
  11824. {
  11825. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11826. struct drm_crtc *drmmode_crtc;
  11827. struct intel_crtc *crtc;
  11828. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11829. if (!drmmode_crtc) {
  11830. DRM_ERROR("no such CRTC id\n");
  11831. return -ENOENT;
  11832. }
  11833. crtc = to_intel_crtc(drmmode_crtc);
  11834. pipe_from_crtc_id->pipe = crtc->pipe;
  11835. return 0;
  11836. }
  11837. static int intel_encoder_clones(struct intel_encoder *encoder)
  11838. {
  11839. struct drm_device *dev = encoder->base.dev;
  11840. struct intel_encoder *source_encoder;
  11841. int index_mask = 0;
  11842. int entry = 0;
  11843. for_each_intel_encoder(dev, source_encoder) {
  11844. if (encoders_cloneable(encoder, source_encoder))
  11845. index_mask |= (1 << entry);
  11846. entry++;
  11847. }
  11848. return index_mask;
  11849. }
  11850. static bool has_edp_a(struct drm_device *dev)
  11851. {
  11852. struct drm_i915_private *dev_priv = dev->dev_private;
  11853. if (!IS_MOBILE(dev))
  11854. return false;
  11855. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11856. return false;
  11857. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11858. return false;
  11859. return true;
  11860. }
  11861. static bool intel_crt_present(struct drm_device *dev)
  11862. {
  11863. struct drm_i915_private *dev_priv = dev->dev_private;
  11864. if (INTEL_INFO(dev)->gen >= 9)
  11865. return false;
  11866. if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
  11867. return false;
  11868. if (IS_CHERRYVIEW(dev))
  11869. return false;
  11870. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  11871. return false;
  11872. return true;
  11873. }
  11874. static void intel_setup_outputs(struct drm_device *dev)
  11875. {
  11876. struct drm_i915_private *dev_priv = dev->dev_private;
  11877. struct intel_encoder *encoder;
  11878. bool dpd_is_edp = false;
  11879. intel_lvds_init(dev);
  11880. if (intel_crt_present(dev))
  11881. intel_crt_init(dev);
  11882. if (IS_BROXTON(dev)) {
  11883. /*
  11884. * FIXME: Broxton doesn't support port detection via the
  11885. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11886. * detect the ports.
  11887. */
  11888. intel_ddi_init(dev, PORT_A);
  11889. intel_ddi_init(dev, PORT_B);
  11890. intel_ddi_init(dev, PORT_C);
  11891. } else if (HAS_DDI(dev)) {
  11892. int found;
  11893. /*
  11894. * Haswell uses DDI functions to detect digital outputs.
  11895. * On SKL pre-D0 the strap isn't connected, so we assume
  11896. * it's there.
  11897. */
  11898. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11899. /* WaIgnoreDDIAStrap: skl */
  11900. if (found || IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  11901. intel_ddi_init(dev, PORT_A);
  11902. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11903. * register */
  11904. found = I915_READ(SFUSE_STRAP);
  11905. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11906. intel_ddi_init(dev, PORT_B);
  11907. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11908. intel_ddi_init(dev, PORT_C);
  11909. if (found & SFUSE_STRAP_DDID_DETECTED)
  11910. intel_ddi_init(dev, PORT_D);
  11911. /*
  11912. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11913. */
  11914. if ((IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) &&
  11915. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11916. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11917. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11918. intel_ddi_init(dev, PORT_E);
  11919. } else if (HAS_PCH_SPLIT(dev)) {
  11920. int found;
  11921. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  11922. if (has_edp_a(dev))
  11923. intel_dp_init(dev, DP_A, PORT_A);
  11924. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11925. /* PCH SDVOB multiplex with HDMIB */
  11926. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  11927. if (!found)
  11928. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  11929. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11930. intel_dp_init(dev, PCH_DP_B, PORT_B);
  11931. }
  11932. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11933. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  11934. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11935. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  11936. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11937. intel_dp_init(dev, PCH_DP_C, PORT_C);
  11938. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11939. intel_dp_init(dev, PCH_DP_D, PORT_D);
  11940. } else if (IS_VALLEYVIEW(dev)) {
  11941. /*
  11942. * The DP_DETECTED bit is the latched state of the DDC
  11943. * SDA pin at boot. However since eDP doesn't require DDC
  11944. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11945. * eDP ports may have been muxed to an alternate function.
  11946. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11947. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11948. * detect eDP ports.
  11949. */
  11950. if (I915_READ(VLV_HDMIB) & SDVO_DETECTED &&
  11951. !intel_dp_is_edp(dev, PORT_B))
  11952. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  11953. if (I915_READ(VLV_DP_B) & DP_DETECTED ||
  11954. intel_dp_is_edp(dev, PORT_B))
  11955. intel_dp_init(dev, VLV_DP_B, PORT_B);
  11956. if (I915_READ(VLV_HDMIC) & SDVO_DETECTED &&
  11957. !intel_dp_is_edp(dev, PORT_C))
  11958. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  11959. if (I915_READ(VLV_DP_C) & DP_DETECTED ||
  11960. intel_dp_is_edp(dev, PORT_C))
  11961. intel_dp_init(dev, VLV_DP_C, PORT_C);
  11962. if (IS_CHERRYVIEW(dev)) {
  11963. /* eDP not supported on port D, so don't check VBT */
  11964. if (I915_READ(CHV_HDMID) & SDVO_DETECTED)
  11965. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  11966. if (I915_READ(CHV_DP_D) & DP_DETECTED)
  11967. intel_dp_init(dev, CHV_DP_D, PORT_D);
  11968. }
  11969. intel_dsi_init(dev);
  11970. } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
  11971. bool found = false;
  11972. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11973. DRM_DEBUG_KMS("probing SDVOB\n");
  11974. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  11975. if (!found && IS_G4X(dev)) {
  11976. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11977. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  11978. }
  11979. if (!found && IS_G4X(dev))
  11980. intel_dp_init(dev, DP_B, PORT_B);
  11981. }
  11982. /* Before G4X SDVOC doesn't have its own detect register */
  11983. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11984. DRM_DEBUG_KMS("probing SDVOC\n");
  11985. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  11986. }
  11987. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11988. if (IS_G4X(dev)) {
  11989. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11990. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  11991. }
  11992. if (IS_G4X(dev))
  11993. intel_dp_init(dev, DP_C, PORT_C);
  11994. }
  11995. if (IS_G4X(dev) &&
  11996. (I915_READ(DP_D) & DP_DETECTED))
  11997. intel_dp_init(dev, DP_D, PORT_D);
  11998. } else if (IS_GEN2(dev))
  11999. intel_dvo_init(dev);
  12000. if (SUPPORTS_TV(dev))
  12001. intel_tv_init(dev);
  12002. intel_psr_init(dev);
  12003. for_each_intel_encoder(dev, encoder) {
  12004. encoder->base.possible_crtcs = encoder->crtc_mask;
  12005. encoder->base.possible_clones =
  12006. intel_encoder_clones(encoder);
  12007. }
  12008. intel_init_pch_refclk(dev);
  12009. drm_helper_move_panel_connectors_to_head(dev);
  12010. }
  12011. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  12012. {
  12013. struct drm_device *dev = fb->dev;
  12014. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12015. drm_framebuffer_cleanup(fb);
  12016. mutex_lock(&dev->struct_mutex);
  12017. WARN_ON(!intel_fb->obj->framebuffer_references--);
  12018. drm_gem_object_unreference(&intel_fb->obj->base);
  12019. mutex_unlock(&dev->struct_mutex);
  12020. kfree(intel_fb);
  12021. }
  12022. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  12023. struct drm_file *file,
  12024. unsigned int *handle)
  12025. {
  12026. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12027. struct drm_i915_gem_object *obj = intel_fb->obj;
  12028. return drm_gem_handle_create(file, &obj->base, handle);
  12029. }
  12030. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  12031. struct drm_file *file,
  12032. unsigned flags, unsigned color,
  12033. struct drm_clip_rect *clips,
  12034. unsigned num_clips)
  12035. {
  12036. struct drm_device *dev = fb->dev;
  12037. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  12038. struct drm_i915_gem_object *obj = intel_fb->obj;
  12039. mutex_lock(&dev->struct_mutex);
  12040. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  12041. mutex_unlock(&dev->struct_mutex);
  12042. return 0;
  12043. }
  12044. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  12045. .destroy = intel_user_framebuffer_destroy,
  12046. .create_handle = intel_user_framebuffer_create_handle,
  12047. .dirty = intel_user_framebuffer_dirty,
  12048. };
  12049. static
  12050. u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
  12051. uint32_t pixel_format)
  12052. {
  12053. u32 gen = INTEL_INFO(dev)->gen;
  12054. if (gen >= 9) {
  12055. /* "The stride in bytes must not exceed the of the size of 8K
  12056. * pixels and 32K bytes."
  12057. */
  12058. return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
  12059. } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
  12060. return 32*1024;
  12061. } else if (gen >= 4) {
  12062. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12063. return 16*1024;
  12064. else
  12065. return 32*1024;
  12066. } else if (gen >= 3) {
  12067. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  12068. return 8*1024;
  12069. else
  12070. return 16*1024;
  12071. } else {
  12072. /* XXX DSPC is limited to 4k tiled */
  12073. return 8*1024;
  12074. }
  12075. }
  12076. static int intel_framebuffer_init(struct drm_device *dev,
  12077. struct intel_framebuffer *intel_fb,
  12078. struct drm_mode_fb_cmd2 *mode_cmd,
  12079. struct drm_i915_gem_object *obj)
  12080. {
  12081. unsigned int aligned_height;
  12082. int ret;
  12083. u32 pitch_limit, stride_alignment;
  12084. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  12085. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  12086. /* Enforce that fb modifier and tiling mode match, but only for
  12087. * X-tiled. This is needed for FBC. */
  12088. if (!!(obj->tiling_mode == I915_TILING_X) !=
  12089. !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
  12090. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  12091. return -EINVAL;
  12092. }
  12093. } else {
  12094. if (obj->tiling_mode == I915_TILING_X)
  12095. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  12096. else if (obj->tiling_mode == I915_TILING_Y) {
  12097. DRM_DEBUG("No Y tiling for legacy addfb\n");
  12098. return -EINVAL;
  12099. }
  12100. }
  12101. /* Passed in modifier sanity checking. */
  12102. switch (mode_cmd->modifier[0]) {
  12103. case I915_FORMAT_MOD_Y_TILED:
  12104. case I915_FORMAT_MOD_Yf_TILED:
  12105. if (INTEL_INFO(dev)->gen < 9) {
  12106. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  12107. mode_cmd->modifier[0]);
  12108. return -EINVAL;
  12109. }
  12110. case DRM_FORMAT_MOD_NONE:
  12111. case I915_FORMAT_MOD_X_TILED:
  12112. break;
  12113. default:
  12114. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  12115. mode_cmd->modifier[0]);
  12116. return -EINVAL;
  12117. }
  12118. stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
  12119. mode_cmd->pixel_format);
  12120. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12121. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  12122. mode_cmd->pitches[0], stride_alignment);
  12123. return -EINVAL;
  12124. }
  12125. pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
  12126. mode_cmd->pixel_format);
  12127. if (mode_cmd->pitches[0] > pitch_limit) {
  12128. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  12129. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12130. "tiled" : "linear",
  12131. mode_cmd->pitches[0], pitch_limit);
  12132. return -EINVAL;
  12133. }
  12134. if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
  12135. mode_cmd->pitches[0] != obj->stride) {
  12136. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  12137. mode_cmd->pitches[0], obj->stride);
  12138. return -EINVAL;
  12139. }
  12140. /* Reject formats not supported by any plane early. */
  12141. switch (mode_cmd->pixel_format) {
  12142. case DRM_FORMAT_C8:
  12143. case DRM_FORMAT_RGB565:
  12144. case DRM_FORMAT_XRGB8888:
  12145. case DRM_FORMAT_ARGB8888:
  12146. break;
  12147. case DRM_FORMAT_XRGB1555:
  12148. if (INTEL_INFO(dev)->gen > 3) {
  12149. DRM_DEBUG("unsupported pixel format: %s\n",
  12150. drm_get_format_name(mode_cmd->pixel_format));
  12151. return -EINVAL;
  12152. }
  12153. break;
  12154. case DRM_FORMAT_ABGR8888:
  12155. if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
  12156. DRM_DEBUG("unsupported pixel format: %s\n",
  12157. drm_get_format_name(mode_cmd->pixel_format));
  12158. return -EINVAL;
  12159. }
  12160. break;
  12161. case DRM_FORMAT_XBGR8888:
  12162. case DRM_FORMAT_XRGB2101010:
  12163. case DRM_FORMAT_XBGR2101010:
  12164. if (INTEL_INFO(dev)->gen < 4) {
  12165. DRM_DEBUG("unsupported pixel format: %s\n",
  12166. drm_get_format_name(mode_cmd->pixel_format));
  12167. return -EINVAL;
  12168. }
  12169. break;
  12170. case DRM_FORMAT_ABGR2101010:
  12171. if (!IS_VALLEYVIEW(dev)) {
  12172. DRM_DEBUG("unsupported pixel format: %s\n",
  12173. drm_get_format_name(mode_cmd->pixel_format));
  12174. return -EINVAL;
  12175. }
  12176. break;
  12177. case DRM_FORMAT_YUYV:
  12178. case DRM_FORMAT_UYVY:
  12179. case DRM_FORMAT_YVYU:
  12180. case DRM_FORMAT_VYUY:
  12181. if (INTEL_INFO(dev)->gen < 5) {
  12182. DRM_DEBUG("unsupported pixel format: %s\n",
  12183. drm_get_format_name(mode_cmd->pixel_format));
  12184. return -EINVAL;
  12185. }
  12186. break;
  12187. default:
  12188. DRM_DEBUG("unsupported pixel format: %s\n",
  12189. drm_get_format_name(mode_cmd->pixel_format));
  12190. return -EINVAL;
  12191. }
  12192. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12193. if (mode_cmd->offsets[0] != 0)
  12194. return -EINVAL;
  12195. aligned_height = intel_fb_align_height(dev, mode_cmd->height,
  12196. mode_cmd->pixel_format,
  12197. mode_cmd->modifier[0]);
  12198. /* FIXME drm helper for size checks (especially planar formats)? */
  12199. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  12200. return -EINVAL;
  12201. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  12202. intel_fb->obj = obj;
  12203. intel_fb->obj->framebuffer_references++;
  12204. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  12205. if (ret) {
  12206. DRM_ERROR("framebuffer init failed %d\n", ret);
  12207. return ret;
  12208. }
  12209. return 0;
  12210. }
  12211. static struct drm_framebuffer *
  12212. intel_user_framebuffer_create(struct drm_device *dev,
  12213. struct drm_file *filp,
  12214. struct drm_mode_fb_cmd2 *mode_cmd)
  12215. {
  12216. struct drm_framebuffer *fb;
  12217. struct drm_i915_gem_object *obj;
  12218. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  12219. mode_cmd->handles[0]));
  12220. if (&obj->base == NULL)
  12221. return ERR_PTR(-ENOENT);
  12222. fb = intel_framebuffer_create(dev, mode_cmd, obj);
  12223. if (IS_ERR(fb))
  12224. drm_gem_object_unreference_unlocked(&obj->base);
  12225. return fb;
  12226. }
  12227. #ifndef CONFIG_DRM_FBDEV_EMULATION
  12228. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  12229. {
  12230. }
  12231. #endif
  12232. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12233. .fb_create = intel_user_framebuffer_create,
  12234. .output_poll_changed = intel_fbdev_output_poll_changed,
  12235. .atomic_check = intel_atomic_check,
  12236. .atomic_commit = intel_atomic_commit,
  12237. .atomic_state_alloc = intel_atomic_state_alloc,
  12238. .atomic_state_clear = intel_atomic_state_clear,
  12239. };
  12240. /* Set up chip specific display functions */
  12241. static void intel_init_display(struct drm_device *dev)
  12242. {
  12243. struct drm_i915_private *dev_priv = dev->dev_private;
  12244. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  12245. dev_priv->display.find_dpll = g4x_find_best_dpll;
  12246. else if (IS_CHERRYVIEW(dev))
  12247. dev_priv->display.find_dpll = chv_find_best_dpll;
  12248. else if (IS_VALLEYVIEW(dev))
  12249. dev_priv->display.find_dpll = vlv_find_best_dpll;
  12250. else if (IS_PINEVIEW(dev))
  12251. dev_priv->display.find_dpll = pnv_find_best_dpll;
  12252. else
  12253. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  12254. if (INTEL_INFO(dev)->gen >= 9) {
  12255. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12256. dev_priv->display.get_initial_plane_config =
  12257. skylake_get_initial_plane_config;
  12258. dev_priv->display.crtc_compute_clock =
  12259. haswell_crtc_compute_clock;
  12260. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12261. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12262. dev_priv->display.update_primary_plane =
  12263. skylake_update_primary_plane;
  12264. } else if (HAS_DDI(dev)) {
  12265. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12266. dev_priv->display.get_initial_plane_config =
  12267. ironlake_get_initial_plane_config;
  12268. dev_priv->display.crtc_compute_clock =
  12269. haswell_crtc_compute_clock;
  12270. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12271. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12272. dev_priv->display.update_primary_plane =
  12273. ironlake_update_primary_plane;
  12274. } else if (HAS_PCH_SPLIT(dev)) {
  12275. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12276. dev_priv->display.get_initial_plane_config =
  12277. ironlake_get_initial_plane_config;
  12278. dev_priv->display.crtc_compute_clock =
  12279. ironlake_crtc_compute_clock;
  12280. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12281. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12282. dev_priv->display.update_primary_plane =
  12283. ironlake_update_primary_plane;
  12284. } else if (IS_VALLEYVIEW(dev)) {
  12285. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12286. dev_priv->display.get_initial_plane_config =
  12287. i9xx_get_initial_plane_config;
  12288. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12289. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12290. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12291. dev_priv->display.update_primary_plane =
  12292. i9xx_update_primary_plane;
  12293. } else {
  12294. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12295. dev_priv->display.get_initial_plane_config =
  12296. i9xx_get_initial_plane_config;
  12297. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12298. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12299. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12300. dev_priv->display.update_primary_plane =
  12301. i9xx_update_primary_plane;
  12302. }
  12303. /* Returns the core display clock speed */
  12304. if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
  12305. dev_priv->display.get_display_clock_speed =
  12306. skylake_get_display_clock_speed;
  12307. else if (IS_BROXTON(dev))
  12308. dev_priv->display.get_display_clock_speed =
  12309. broxton_get_display_clock_speed;
  12310. else if (IS_BROADWELL(dev))
  12311. dev_priv->display.get_display_clock_speed =
  12312. broadwell_get_display_clock_speed;
  12313. else if (IS_HASWELL(dev))
  12314. dev_priv->display.get_display_clock_speed =
  12315. haswell_get_display_clock_speed;
  12316. else if (IS_VALLEYVIEW(dev))
  12317. dev_priv->display.get_display_clock_speed =
  12318. valleyview_get_display_clock_speed;
  12319. else if (IS_GEN5(dev))
  12320. dev_priv->display.get_display_clock_speed =
  12321. ilk_get_display_clock_speed;
  12322. else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
  12323. IS_GEN6(dev) || IS_IVYBRIDGE(dev))
  12324. dev_priv->display.get_display_clock_speed =
  12325. i945_get_display_clock_speed;
  12326. else if (IS_GM45(dev))
  12327. dev_priv->display.get_display_clock_speed =
  12328. gm45_get_display_clock_speed;
  12329. else if (IS_CRESTLINE(dev))
  12330. dev_priv->display.get_display_clock_speed =
  12331. i965gm_get_display_clock_speed;
  12332. else if (IS_PINEVIEW(dev))
  12333. dev_priv->display.get_display_clock_speed =
  12334. pnv_get_display_clock_speed;
  12335. else if (IS_G33(dev) || IS_G4X(dev))
  12336. dev_priv->display.get_display_clock_speed =
  12337. g33_get_display_clock_speed;
  12338. else if (IS_I915G(dev))
  12339. dev_priv->display.get_display_clock_speed =
  12340. i915_get_display_clock_speed;
  12341. else if (IS_I945GM(dev) || IS_845G(dev))
  12342. dev_priv->display.get_display_clock_speed =
  12343. i9xx_misc_get_display_clock_speed;
  12344. else if (IS_PINEVIEW(dev))
  12345. dev_priv->display.get_display_clock_speed =
  12346. pnv_get_display_clock_speed;
  12347. else if (IS_I915GM(dev))
  12348. dev_priv->display.get_display_clock_speed =
  12349. i915gm_get_display_clock_speed;
  12350. else if (IS_I865G(dev))
  12351. dev_priv->display.get_display_clock_speed =
  12352. i865_get_display_clock_speed;
  12353. else if (IS_I85X(dev))
  12354. dev_priv->display.get_display_clock_speed =
  12355. i85x_get_display_clock_speed;
  12356. else { /* 830 */
  12357. WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
  12358. dev_priv->display.get_display_clock_speed =
  12359. i830_get_display_clock_speed;
  12360. }
  12361. if (IS_GEN5(dev)) {
  12362. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12363. } else if (IS_GEN6(dev)) {
  12364. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12365. } else if (IS_IVYBRIDGE(dev)) {
  12366. /* FIXME: detect B0+ stepping and use auto training */
  12367. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12368. } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  12369. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12370. if (IS_BROADWELL(dev)) {
  12371. dev_priv->display.modeset_commit_cdclk =
  12372. broadwell_modeset_commit_cdclk;
  12373. dev_priv->display.modeset_calc_cdclk =
  12374. broadwell_modeset_calc_cdclk;
  12375. }
  12376. } else if (IS_VALLEYVIEW(dev)) {
  12377. dev_priv->display.modeset_commit_cdclk =
  12378. valleyview_modeset_commit_cdclk;
  12379. dev_priv->display.modeset_calc_cdclk =
  12380. valleyview_modeset_calc_cdclk;
  12381. } else if (IS_BROXTON(dev)) {
  12382. dev_priv->display.modeset_commit_cdclk =
  12383. broxton_modeset_commit_cdclk;
  12384. dev_priv->display.modeset_calc_cdclk =
  12385. broxton_modeset_calc_cdclk;
  12386. }
  12387. switch (INTEL_INFO(dev)->gen) {
  12388. case 2:
  12389. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12390. break;
  12391. case 3:
  12392. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12393. break;
  12394. case 4:
  12395. case 5:
  12396. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12397. break;
  12398. case 6:
  12399. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12400. break;
  12401. case 7:
  12402. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12403. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12404. break;
  12405. case 9:
  12406. /* Drop through - unsupported since execlist only. */
  12407. default:
  12408. /* Default just returns -ENODEV to indicate unsupported */
  12409. dev_priv->display.queue_flip = intel_default_queue_flip;
  12410. }
  12411. mutex_init(&dev_priv->pps_mutex);
  12412. }
  12413. /*
  12414. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12415. * resume, or other times. This quirk makes sure that's the case for
  12416. * affected systems.
  12417. */
  12418. static void quirk_pipea_force(struct drm_device *dev)
  12419. {
  12420. struct drm_i915_private *dev_priv = dev->dev_private;
  12421. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12422. DRM_INFO("applying pipe a force quirk\n");
  12423. }
  12424. static void quirk_pipeb_force(struct drm_device *dev)
  12425. {
  12426. struct drm_i915_private *dev_priv = dev->dev_private;
  12427. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12428. DRM_INFO("applying pipe b force quirk\n");
  12429. }
  12430. /*
  12431. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12432. */
  12433. static void quirk_ssc_force_disable(struct drm_device *dev)
  12434. {
  12435. struct drm_i915_private *dev_priv = dev->dev_private;
  12436. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12437. DRM_INFO("applying lvds SSC disable quirk\n");
  12438. }
  12439. /*
  12440. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12441. * brightness value
  12442. */
  12443. static void quirk_invert_brightness(struct drm_device *dev)
  12444. {
  12445. struct drm_i915_private *dev_priv = dev->dev_private;
  12446. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12447. DRM_INFO("applying inverted panel brightness quirk\n");
  12448. }
  12449. /* Some VBT's incorrectly indicate no backlight is present */
  12450. static void quirk_backlight_present(struct drm_device *dev)
  12451. {
  12452. struct drm_i915_private *dev_priv = dev->dev_private;
  12453. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12454. DRM_INFO("applying backlight present quirk\n");
  12455. }
  12456. struct intel_quirk {
  12457. int device;
  12458. int subsystem_vendor;
  12459. int subsystem_device;
  12460. void (*hook)(struct drm_device *dev);
  12461. };
  12462. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12463. struct intel_dmi_quirk {
  12464. void (*hook)(struct drm_device *dev);
  12465. const struct dmi_system_id (*dmi_id_list)[];
  12466. };
  12467. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12468. {
  12469. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12470. return 1;
  12471. }
  12472. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12473. {
  12474. .dmi_id_list = &(const struct dmi_system_id[]) {
  12475. {
  12476. .callback = intel_dmi_reverse_brightness,
  12477. .ident = "NCR Corporation",
  12478. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12479. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12480. },
  12481. },
  12482. { } /* terminating entry */
  12483. },
  12484. .hook = quirk_invert_brightness,
  12485. },
  12486. };
  12487. static struct intel_quirk intel_quirks[] = {
  12488. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12489. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12490. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12491. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12492. /* 830 needs to leave pipe A & dpll A up */
  12493. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12494. /* 830 needs to leave pipe B & dpll B up */
  12495. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12496. /* Lenovo U160 cannot use SSC on LVDS */
  12497. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12498. /* Sony Vaio Y cannot use SSC on LVDS */
  12499. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12500. /* Acer Aspire 5734Z must invert backlight brightness */
  12501. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12502. /* Acer/eMachines G725 */
  12503. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12504. /* Acer/eMachines e725 */
  12505. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12506. /* Acer/Packard Bell NCL20 */
  12507. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12508. /* Acer Aspire 4736Z */
  12509. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12510. /* Acer Aspire 5336 */
  12511. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12512. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12513. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12514. /* Acer C720 Chromebook (Core i3 4005U) */
  12515. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12516. /* Apple Macbook 2,1 (Core 2 T7400) */
  12517. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12518. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12519. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12520. /* HP Chromebook 14 (Celeron 2955U) */
  12521. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12522. /* Dell Chromebook 11 */
  12523. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12524. };
  12525. static void intel_init_quirks(struct drm_device *dev)
  12526. {
  12527. struct pci_dev *d = dev->pdev;
  12528. int i;
  12529. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12530. struct intel_quirk *q = &intel_quirks[i];
  12531. if (d->device == q->device &&
  12532. (d->subsystem_vendor == q->subsystem_vendor ||
  12533. q->subsystem_vendor == PCI_ANY_ID) &&
  12534. (d->subsystem_device == q->subsystem_device ||
  12535. q->subsystem_device == PCI_ANY_ID))
  12536. q->hook(dev);
  12537. }
  12538. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12539. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12540. intel_dmi_quirks[i].hook(dev);
  12541. }
  12542. }
  12543. /* Disable the VGA plane that we never use */
  12544. static void i915_disable_vga(struct drm_device *dev)
  12545. {
  12546. struct drm_i915_private *dev_priv = dev->dev_private;
  12547. u8 sr1;
  12548. u32 vga_reg = i915_vgacntrl_reg(dev);
  12549. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12550. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  12551. outb(SR01, VGA_SR_INDEX);
  12552. sr1 = inb(VGA_SR_DATA);
  12553. outb(sr1 | 1<<5, VGA_SR_DATA);
  12554. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  12555. udelay(300);
  12556. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12557. POSTING_READ(vga_reg);
  12558. }
  12559. void intel_modeset_init_hw(struct drm_device *dev)
  12560. {
  12561. intel_update_cdclk(dev);
  12562. intel_prepare_ddi(dev);
  12563. intel_init_clock_gating(dev);
  12564. intel_enable_gt_powersave(dev);
  12565. }
  12566. void intel_modeset_init(struct drm_device *dev)
  12567. {
  12568. struct drm_i915_private *dev_priv = dev->dev_private;
  12569. int sprite, ret;
  12570. enum pipe pipe;
  12571. struct intel_crtc *crtc;
  12572. drm_mode_config_init(dev);
  12573. dev->mode_config.min_width = 0;
  12574. dev->mode_config.min_height = 0;
  12575. dev->mode_config.preferred_depth = 24;
  12576. dev->mode_config.prefer_shadow = 1;
  12577. dev->mode_config.allow_fb_modifiers = true;
  12578. dev->mode_config.funcs = &intel_mode_funcs;
  12579. intel_init_quirks(dev);
  12580. intel_init_pm(dev);
  12581. if (INTEL_INFO(dev)->num_pipes == 0)
  12582. return;
  12583. /*
  12584. * There may be no VBT; and if the BIOS enabled SSC we can
  12585. * just keep using it to avoid unnecessary flicker. Whereas if the
  12586. * BIOS isn't using it, don't assume it will work even if the VBT
  12587. * indicates as much.
  12588. */
  12589. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)) {
  12590. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12591. DREF_SSC1_ENABLE);
  12592. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12593. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12594. bios_lvds_use_ssc ? "en" : "dis",
  12595. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12596. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12597. }
  12598. }
  12599. intel_init_display(dev);
  12600. intel_init_audio(dev);
  12601. if (IS_GEN2(dev)) {
  12602. dev->mode_config.max_width = 2048;
  12603. dev->mode_config.max_height = 2048;
  12604. } else if (IS_GEN3(dev)) {
  12605. dev->mode_config.max_width = 4096;
  12606. dev->mode_config.max_height = 4096;
  12607. } else {
  12608. dev->mode_config.max_width = 8192;
  12609. dev->mode_config.max_height = 8192;
  12610. }
  12611. if (IS_845G(dev) || IS_I865G(dev)) {
  12612. dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
  12613. dev->mode_config.cursor_height = 1023;
  12614. } else if (IS_GEN2(dev)) {
  12615. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12616. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12617. } else {
  12618. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12619. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12620. }
  12621. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  12622. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12623. INTEL_INFO(dev)->num_pipes,
  12624. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  12625. for_each_pipe(dev_priv, pipe) {
  12626. intel_crtc_init(dev, pipe);
  12627. for_each_sprite(dev_priv, pipe, sprite) {
  12628. ret = intel_plane_init(dev, pipe, sprite);
  12629. if (ret)
  12630. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  12631. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  12632. }
  12633. }
  12634. intel_update_czclk(dev_priv);
  12635. intel_update_cdclk(dev);
  12636. intel_shared_dpll_init(dev);
  12637. /* Just disable it once at startup */
  12638. i915_disable_vga(dev);
  12639. intel_setup_outputs(dev);
  12640. drm_modeset_lock_all(dev);
  12641. intel_modeset_setup_hw_state(dev);
  12642. drm_modeset_unlock_all(dev);
  12643. for_each_intel_crtc(dev, crtc) {
  12644. struct intel_initial_plane_config plane_config = {};
  12645. if (!crtc->active)
  12646. continue;
  12647. /*
  12648. * Note that reserving the BIOS fb up front prevents us
  12649. * from stuffing other stolen allocations like the ring
  12650. * on top. This prevents some ugliness at boot time, and
  12651. * can even allow for smooth boot transitions if the BIOS
  12652. * fb is large enough for the active pipe configuration.
  12653. */
  12654. dev_priv->display.get_initial_plane_config(crtc,
  12655. &plane_config);
  12656. /*
  12657. * If the fb is shared between multiple heads, we'll
  12658. * just get the first one.
  12659. */
  12660. intel_find_initial_plane_obj(crtc, &plane_config);
  12661. }
  12662. }
  12663. static void intel_enable_pipe_a(struct drm_device *dev)
  12664. {
  12665. struct intel_connector *connector;
  12666. struct drm_connector *crt = NULL;
  12667. struct intel_load_detect_pipe load_detect_temp;
  12668. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12669. /* We can't just switch on the pipe A, we need to set things up with a
  12670. * proper mode and output configuration. As a gross hack, enable pipe A
  12671. * by enabling the load detect pipe once. */
  12672. for_each_intel_connector(dev, connector) {
  12673. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12674. crt = &connector->base;
  12675. break;
  12676. }
  12677. }
  12678. if (!crt)
  12679. return;
  12680. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12681. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12682. }
  12683. static bool
  12684. intel_check_plane_mapping(struct intel_crtc *crtc)
  12685. {
  12686. struct drm_device *dev = crtc->base.dev;
  12687. struct drm_i915_private *dev_priv = dev->dev_private;
  12688. u32 val;
  12689. if (INTEL_INFO(dev)->num_pipes == 1)
  12690. return true;
  12691. val = I915_READ(DSPCNTR(!crtc->plane));
  12692. if ((val & DISPLAY_PLANE_ENABLE) &&
  12693. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12694. return false;
  12695. return true;
  12696. }
  12697. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12698. {
  12699. struct drm_device *dev = crtc->base.dev;
  12700. struct intel_encoder *encoder;
  12701. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12702. return true;
  12703. return false;
  12704. }
  12705. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12706. {
  12707. struct drm_device *dev = crtc->base.dev;
  12708. struct drm_i915_private *dev_priv = dev->dev_private;
  12709. u32 reg;
  12710. /* Clear any frame start delays used for debugging left by the BIOS */
  12711. reg = PIPECONF(crtc->config->cpu_transcoder);
  12712. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12713. /* restore vblank interrupts to correct state */
  12714. drm_crtc_vblank_reset(&crtc->base);
  12715. if (crtc->active) {
  12716. struct intel_plane *plane;
  12717. drm_crtc_vblank_on(&crtc->base);
  12718. /* Disable everything but the primary plane */
  12719. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12720. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12721. continue;
  12722. plane->disable_plane(&plane->base, &crtc->base);
  12723. }
  12724. }
  12725. /* We need to sanitize the plane -> pipe mapping first because this will
  12726. * disable the crtc (and hence change the state) if it is wrong. Note
  12727. * that gen4+ has a fixed plane -> pipe mapping. */
  12728. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  12729. bool plane;
  12730. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  12731. crtc->base.base.id);
  12732. /* Pipe has the wrong plane attached and the plane is active.
  12733. * Temporarily change the plane mapping and disable everything
  12734. * ... */
  12735. plane = crtc->plane;
  12736. to_intel_plane_state(crtc->base.primary->state)->visible = true;
  12737. crtc->plane = !plane;
  12738. intel_crtc_disable_noatomic(&crtc->base);
  12739. crtc->plane = plane;
  12740. }
  12741. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12742. crtc->pipe == PIPE_A && !crtc->active) {
  12743. /* BIOS forgot to enable pipe A, this mostly happens after
  12744. * resume. Force-enable the pipe to fix this, the update_dpms
  12745. * call below we restore the pipe to the right state, but leave
  12746. * the required bits on. */
  12747. intel_enable_pipe_a(dev);
  12748. }
  12749. /* Adjust the state of the output pipe according to whether we
  12750. * have active connectors/encoders. */
  12751. if (!intel_crtc_has_encoders(crtc))
  12752. intel_crtc_disable_noatomic(&crtc->base);
  12753. if (crtc->active != crtc->base.state->active) {
  12754. struct intel_encoder *encoder;
  12755. /* This can happen either due to bugs in the get_hw_state
  12756. * functions or because of calls to intel_crtc_disable_noatomic,
  12757. * or because the pipe is force-enabled due to the
  12758. * pipe A quirk. */
  12759. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  12760. crtc->base.base.id,
  12761. crtc->base.state->enable ? "enabled" : "disabled",
  12762. crtc->active ? "enabled" : "disabled");
  12763. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
  12764. crtc->base.state->active = crtc->active;
  12765. crtc->base.enabled = crtc->active;
  12766. /* Because we only establish the connector -> encoder ->
  12767. * crtc links if something is active, this means the
  12768. * crtc is now deactivated. Break the links. connector
  12769. * -> encoder links are only establish when things are
  12770. * actually up, hence no need to break them. */
  12771. WARN_ON(crtc->active);
  12772. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12773. encoder->base.crtc = NULL;
  12774. }
  12775. if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
  12776. /*
  12777. * We start out with underrun reporting disabled to avoid races.
  12778. * For correct bookkeeping mark this on active crtcs.
  12779. *
  12780. * Also on gmch platforms we dont have any hardware bits to
  12781. * disable the underrun reporting. Which means we need to start
  12782. * out with underrun reporting disabled also on inactive pipes,
  12783. * since otherwise we'll complain about the garbage we read when
  12784. * e.g. coming up after runtime pm.
  12785. *
  12786. * No protection against concurrent access is required - at
  12787. * worst a fifo underrun happens which also sets this to false.
  12788. */
  12789. crtc->cpu_fifo_underrun_disabled = true;
  12790. crtc->pch_fifo_underrun_disabled = true;
  12791. }
  12792. }
  12793. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12794. {
  12795. struct intel_connector *connector;
  12796. struct drm_device *dev = encoder->base.dev;
  12797. bool active = false;
  12798. /* We need to check both for a crtc link (meaning that the
  12799. * encoder is active and trying to read from a pipe) and the
  12800. * pipe itself being active. */
  12801. bool has_active_crtc = encoder->base.crtc &&
  12802. to_intel_crtc(encoder->base.crtc)->active;
  12803. for_each_intel_connector(dev, connector) {
  12804. if (connector->base.encoder != &encoder->base)
  12805. continue;
  12806. active = true;
  12807. break;
  12808. }
  12809. if (active && !has_active_crtc) {
  12810. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12811. encoder->base.base.id,
  12812. encoder->base.name);
  12813. /* Connector is active, but has no active pipe. This is
  12814. * fallout from our resume register restoring. Disable
  12815. * the encoder manually again. */
  12816. if (encoder->base.crtc) {
  12817. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12818. encoder->base.base.id,
  12819. encoder->base.name);
  12820. encoder->disable(encoder);
  12821. if (encoder->post_disable)
  12822. encoder->post_disable(encoder);
  12823. }
  12824. encoder->base.crtc = NULL;
  12825. /* Inconsistent output/port/pipe state happens presumably due to
  12826. * a bug in one of the get_hw_state functions. Or someplace else
  12827. * in our code, like the register restore mess on resume. Clamp
  12828. * things to off as a safer default. */
  12829. for_each_intel_connector(dev, connector) {
  12830. if (connector->encoder != encoder)
  12831. continue;
  12832. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12833. connector->base.encoder = NULL;
  12834. }
  12835. }
  12836. /* Enabled encoders without active connectors will be fixed in
  12837. * the crtc fixup. */
  12838. }
  12839. void i915_redisable_vga_power_on(struct drm_device *dev)
  12840. {
  12841. struct drm_i915_private *dev_priv = dev->dev_private;
  12842. u32 vga_reg = i915_vgacntrl_reg(dev);
  12843. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12844. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12845. i915_disable_vga(dev);
  12846. }
  12847. }
  12848. void i915_redisable_vga(struct drm_device *dev)
  12849. {
  12850. struct drm_i915_private *dev_priv = dev->dev_private;
  12851. /* This function can be called both from intel_modeset_setup_hw_state or
  12852. * at a very early point in our resume sequence, where the power well
  12853. * structures are not yet restored. Since this function is at a very
  12854. * paranoid "someone might have enabled VGA while we were not looking"
  12855. * level, just check if the power well is enabled instead of trying to
  12856. * follow the "don't touch the power well if we don't need it" policy
  12857. * the rest of the driver uses. */
  12858. if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
  12859. return;
  12860. i915_redisable_vga_power_on(dev);
  12861. }
  12862. static bool primary_get_hw_state(struct intel_plane *plane)
  12863. {
  12864. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12865. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12866. }
  12867. /* FIXME read out full plane state for all planes */
  12868. static void readout_plane_state(struct intel_crtc *crtc)
  12869. {
  12870. struct drm_plane *primary = crtc->base.primary;
  12871. struct intel_plane_state *plane_state =
  12872. to_intel_plane_state(primary->state);
  12873. plane_state->visible = crtc->active &&
  12874. primary_get_hw_state(to_intel_plane(primary));
  12875. if (plane_state->visible)
  12876. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12877. }
  12878. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12879. {
  12880. struct drm_i915_private *dev_priv = dev->dev_private;
  12881. enum pipe pipe;
  12882. struct intel_crtc *crtc;
  12883. struct intel_encoder *encoder;
  12884. struct intel_connector *connector;
  12885. int i;
  12886. for_each_intel_crtc(dev, crtc) {
  12887. __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
  12888. memset(crtc->config, 0, sizeof(*crtc->config));
  12889. crtc->config->base.crtc = &crtc->base;
  12890. crtc->active = dev_priv->display.get_pipe_config(crtc,
  12891. crtc->config);
  12892. crtc->base.state->active = crtc->active;
  12893. crtc->base.enabled = crtc->active;
  12894. readout_plane_state(crtc);
  12895. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  12896. crtc->base.base.id,
  12897. crtc->active ? "enabled" : "disabled");
  12898. }
  12899. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12900. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12901. pll->on = pll->get_hw_state(dev_priv, pll,
  12902. &pll->config.hw_state);
  12903. pll->active = 0;
  12904. pll->config.crtc_mask = 0;
  12905. for_each_intel_crtc(dev, crtc) {
  12906. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
  12907. pll->active++;
  12908. pll->config.crtc_mask |= 1 << crtc->pipe;
  12909. }
  12910. }
  12911. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12912. pll->name, pll->config.crtc_mask, pll->on);
  12913. if (pll->config.crtc_mask)
  12914. intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
  12915. }
  12916. for_each_intel_encoder(dev, encoder) {
  12917. pipe = 0;
  12918. if (encoder->get_hw_state(encoder, &pipe)) {
  12919. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12920. encoder->base.crtc = &crtc->base;
  12921. encoder->get_config(encoder, crtc->config);
  12922. } else {
  12923. encoder->base.crtc = NULL;
  12924. }
  12925. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12926. encoder->base.base.id,
  12927. encoder->base.name,
  12928. encoder->base.crtc ? "enabled" : "disabled",
  12929. pipe_name(pipe));
  12930. }
  12931. for_each_intel_connector(dev, connector) {
  12932. if (connector->get_hw_state(connector)) {
  12933. connector->base.dpms = DRM_MODE_DPMS_ON;
  12934. connector->base.encoder = &connector->encoder->base;
  12935. } else {
  12936. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12937. connector->base.encoder = NULL;
  12938. }
  12939. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12940. connector->base.base.id,
  12941. connector->base.name,
  12942. connector->base.encoder ? "enabled" : "disabled");
  12943. }
  12944. for_each_intel_crtc(dev, crtc) {
  12945. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  12946. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12947. if (crtc->base.state->active) {
  12948. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  12949. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  12950. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12951. /*
  12952. * The initial mode needs to be set in order to keep
  12953. * the atomic core happy. It wants a valid mode if the
  12954. * crtc's enabled, so we do the above call.
  12955. *
  12956. * At this point some state updated by the connectors
  12957. * in their ->detect() callback has not run yet, so
  12958. * no recalculation can be done yet.
  12959. *
  12960. * Even if we could do a recalculation and modeset
  12961. * right now it would cause a double modeset if
  12962. * fbdev or userspace chooses a different initial mode.
  12963. *
  12964. * If that happens, someone indicated they wanted a
  12965. * mode change, which means it's safe to do a full
  12966. * recalculation.
  12967. */
  12968. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  12969. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12970. update_scanline_offset(crtc);
  12971. }
  12972. }
  12973. }
  12974. /* Scan out the current hw modeset state,
  12975. * and sanitizes it to the current state
  12976. */
  12977. static void
  12978. intel_modeset_setup_hw_state(struct drm_device *dev)
  12979. {
  12980. struct drm_i915_private *dev_priv = dev->dev_private;
  12981. enum pipe pipe;
  12982. struct intel_crtc *crtc;
  12983. struct intel_encoder *encoder;
  12984. int i;
  12985. intel_modeset_readout_hw_state(dev);
  12986. /* HW state is read out, now we need to sanitize this mess. */
  12987. for_each_intel_encoder(dev, encoder) {
  12988. intel_sanitize_encoder(encoder);
  12989. }
  12990. for_each_pipe(dev_priv, pipe) {
  12991. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  12992. intel_sanitize_crtc(crtc);
  12993. intel_dump_pipe_config(crtc, crtc->config,
  12994. "[setup_hw_state]");
  12995. }
  12996. intel_modeset_update_connector_atomic_state(dev);
  12997. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12998. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12999. if (!pll->on || pll->active)
  13000. continue;
  13001. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  13002. pll->disable(dev_priv, pll);
  13003. pll->on = false;
  13004. }
  13005. if (IS_VALLEYVIEW(dev))
  13006. vlv_wm_get_hw_state(dev);
  13007. else if (IS_GEN9(dev))
  13008. skl_wm_get_hw_state(dev);
  13009. else if (HAS_PCH_SPLIT(dev))
  13010. ilk_wm_get_hw_state(dev);
  13011. for_each_intel_crtc(dev, crtc) {
  13012. unsigned long put_domains;
  13013. put_domains = modeset_get_crtc_power_domains(&crtc->base);
  13014. if (WARN_ON(put_domains))
  13015. modeset_put_power_domains(dev_priv, put_domains);
  13016. }
  13017. intel_display_set_init_power(dev_priv, false);
  13018. }
  13019. void intel_display_resume(struct drm_device *dev)
  13020. {
  13021. struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
  13022. struct intel_connector *conn;
  13023. struct intel_plane *plane;
  13024. struct drm_crtc *crtc;
  13025. int ret;
  13026. if (!state)
  13027. return;
  13028. state->acquire_ctx = dev->mode_config.acquire_ctx;
  13029. /* preserve complete old state, including dpll */
  13030. intel_atomic_get_shared_dpll_state(state);
  13031. for_each_crtc(dev, crtc) {
  13032. struct drm_crtc_state *crtc_state =
  13033. drm_atomic_get_crtc_state(state, crtc);
  13034. ret = PTR_ERR_OR_ZERO(crtc_state);
  13035. if (ret)
  13036. goto err;
  13037. /* force a restore */
  13038. crtc_state->mode_changed = true;
  13039. }
  13040. for_each_intel_plane(dev, plane) {
  13041. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
  13042. if (ret)
  13043. goto err;
  13044. }
  13045. for_each_intel_connector(dev, conn) {
  13046. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
  13047. if (ret)
  13048. goto err;
  13049. }
  13050. intel_modeset_setup_hw_state(dev);
  13051. i915_redisable_vga(dev);
  13052. ret = drm_atomic_commit(state);
  13053. if (!ret)
  13054. return;
  13055. err:
  13056. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13057. drm_atomic_state_free(state);
  13058. }
  13059. void intel_modeset_gem_init(struct drm_device *dev)
  13060. {
  13061. struct drm_crtc *c;
  13062. struct drm_i915_gem_object *obj;
  13063. int ret;
  13064. mutex_lock(&dev->struct_mutex);
  13065. intel_init_gt_powersave(dev);
  13066. mutex_unlock(&dev->struct_mutex);
  13067. intel_modeset_init_hw(dev);
  13068. intel_setup_overlay(dev);
  13069. /*
  13070. * Make sure any fbs we allocated at startup are properly
  13071. * pinned & fenced. When we do the allocation it's too early
  13072. * for this.
  13073. */
  13074. for_each_crtc(dev, c) {
  13075. obj = intel_fb_obj(c->primary->fb);
  13076. if (obj == NULL)
  13077. continue;
  13078. mutex_lock(&dev->struct_mutex);
  13079. ret = intel_pin_and_fence_fb_obj(c->primary,
  13080. c->primary->fb,
  13081. c->primary->state);
  13082. mutex_unlock(&dev->struct_mutex);
  13083. if (ret) {
  13084. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  13085. to_intel_crtc(c)->pipe);
  13086. drm_framebuffer_unreference(c->primary->fb);
  13087. c->primary->fb = NULL;
  13088. c->primary->crtc = c->primary->state->crtc = NULL;
  13089. update_state_fb(c->primary);
  13090. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  13091. }
  13092. }
  13093. intel_backlight_register(dev);
  13094. }
  13095. void intel_connector_unregister(struct intel_connector *intel_connector)
  13096. {
  13097. struct drm_connector *connector = &intel_connector->base;
  13098. intel_panel_destroy_backlight(connector);
  13099. drm_connector_unregister(connector);
  13100. }
  13101. void intel_modeset_cleanup(struct drm_device *dev)
  13102. {
  13103. struct drm_i915_private *dev_priv = dev->dev_private;
  13104. struct drm_connector *connector;
  13105. intel_disable_gt_powersave(dev);
  13106. intel_backlight_unregister(dev);
  13107. /*
  13108. * Interrupts and polling as the first thing to avoid creating havoc.
  13109. * Too much stuff here (turning of connectors, ...) would
  13110. * experience fancy races otherwise.
  13111. */
  13112. intel_irq_uninstall(dev_priv);
  13113. /*
  13114. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13115. * poll handlers. Hence disable polling after hpd handling is shut down.
  13116. */
  13117. drm_kms_helper_poll_fini(dev);
  13118. intel_unregister_dsm_handler();
  13119. intel_fbc_disable(dev_priv);
  13120. /* flush any delayed tasks or pending work */
  13121. flush_scheduled_work();
  13122. /* destroy the backlight and sysfs files before encoders/connectors */
  13123. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  13124. struct intel_connector *intel_connector;
  13125. intel_connector = to_intel_connector(connector);
  13126. intel_connector->unregister(intel_connector);
  13127. }
  13128. drm_mode_config_cleanup(dev);
  13129. intel_cleanup_overlay(dev);
  13130. mutex_lock(&dev->struct_mutex);
  13131. intel_cleanup_gt_powersave(dev);
  13132. mutex_unlock(&dev->struct_mutex);
  13133. }
  13134. /*
  13135. * Return which encoder is currently attached for connector.
  13136. */
  13137. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  13138. {
  13139. return &intel_attached_encoder(connector)->base;
  13140. }
  13141. void intel_connector_attach_encoder(struct intel_connector *connector,
  13142. struct intel_encoder *encoder)
  13143. {
  13144. connector->encoder = encoder;
  13145. drm_mode_connector_attach_encoder(&connector->base,
  13146. &encoder->base);
  13147. }
  13148. /*
  13149. * set vga decode state - true == enable VGA decode
  13150. */
  13151. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  13152. {
  13153. struct drm_i915_private *dev_priv = dev->dev_private;
  13154. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13155. u16 gmch_ctrl;
  13156. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13157. DRM_ERROR("failed to read control word\n");
  13158. return -EIO;
  13159. }
  13160. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13161. return 0;
  13162. if (state)
  13163. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13164. else
  13165. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13166. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13167. DRM_ERROR("failed to write control word\n");
  13168. return -EIO;
  13169. }
  13170. return 0;
  13171. }
  13172. struct intel_display_error_state {
  13173. u32 power_well_driver;
  13174. int num_transcoders;
  13175. struct intel_cursor_error_state {
  13176. u32 control;
  13177. u32 position;
  13178. u32 base;
  13179. u32 size;
  13180. } cursor[I915_MAX_PIPES];
  13181. struct intel_pipe_error_state {
  13182. bool power_domain_on;
  13183. u32 source;
  13184. u32 stat;
  13185. } pipe[I915_MAX_PIPES];
  13186. struct intel_plane_error_state {
  13187. u32 control;
  13188. u32 stride;
  13189. u32 size;
  13190. u32 pos;
  13191. u32 addr;
  13192. u32 surface;
  13193. u32 tile_offset;
  13194. } plane[I915_MAX_PIPES];
  13195. struct intel_transcoder_error_state {
  13196. bool power_domain_on;
  13197. enum transcoder cpu_transcoder;
  13198. u32 conf;
  13199. u32 htotal;
  13200. u32 hblank;
  13201. u32 hsync;
  13202. u32 vtotal;
  13203. u32 vblank;
  13204. u32 vsync;
  13205. } transcoder[4];
  13206. };
  13207. struct intel_display_error_state *
  13208. intel_display_capture_error_state(struct drm_device *dev)
  13209. {
  13210. struct drm_i915_private *dev_priv = dev->dev_private;
  13211. struct intel_display_error_state *error;
  13212. int transcoders[] = {
  13213. TRANSCODER_A,
  13214. TRANSCODER_B,
  13215. TRANSCODER_C,
  13216. TRANSCODER_EDP,
  13217. };
  13218. int i;
  13219. if (INTEL_INFO(dev)->num_pipes == 0)
  13220. return NULL;
  13221. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13222. if (error == NULL)
  13223. return NULL;
  13224. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13225. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13226. for_each_pipe(dev_priv, i) {
  13227. error->pipe[i].power_domain_on =
  13228. __intel_display_power_is_enabled(dev_priv,
  13229. POWER_DOMAIN_PIPE(i));
  13230. if (!error->pipe[i].power_domain_on)
  13231. continue;
  13232. error->cursor[i].control = I915_READ(CURCNTR(i));
  13233. error->cursor[i].position = I915_READ(CURPOS(i));
  13234. error->cursor[i].base = I915_READ(CURBASE(i));
  13235. error->plane[i].control = I915_READ(DSPCNTR(i));
  13236. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13237. if (INTEL_INFO(dev)->gen <= 3) {
  13238. error->plane[i].size = I915_READ(DSPSIZE(i));
  13239. error->plane[i].pos = I915_READ(DSPPOS(i));
  13240. }
  13241. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13242. error->plane[i].addr = I915_READ(DSPADDR(i));
  13243. if (INTEL_INFO(dev)->gen >= 4) {
  13244. error->plane[i].surface = I915_READ(DSPSURF(i));
  13245. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13246. }
  13247. error->pipe[i].source = I915_READ(PIPESRC(i));
  13248. if (HAS_GMCH_DISPLAY(dev))
  13249. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13250. }
  13251. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  13252. if (HAS_DDI(dev_priv->dev))
  13253. error->num_transcoders++; /* Account for eDP. */
  13254. for (i = 0; i < error->num_transcoders; i++) {
  13255. enum transcoder cpu_transcoder = transcoders[i];
  13256. error->transcoder[i].power_domain_on =
  13257. __intel_display_power_is_enabled(dev_priv,
  13258. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13259. if (!error->transcoder[i].power_domain_on)
  13260. continue;
  13261. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13262. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13263. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13264. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13265. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13266. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13267. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13268. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13269. }
  13270. return error;
  13271. }
  13272. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13273. void
  13274. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13275. struct drm_device *dev,
  13276. struct intel_display_error_state *error)
  13277. {
  13278. struct drm_i915_private *dev_priv = dev->dev_private;
  13279. int i;
  13280. if (!error)
  13281. return;
  13282. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  13283. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  13284. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13285. error->power_well_driver);
  13286. for_each_pipe(dev_priv, i) {
  13287. err_printf(m, "Pipe [%d]:\n", i);
  13288. err_printf(m, " Power: %s\n",
  13289. error->pipe[i].power_domain_on ? "on" : "off");
  13290. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13291. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13292. err_printf(m, "Plane [%d]:\n", i);
  13293. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13294. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13295. if (INTEL_INFO(dev)->gen <= 3) {
  13296. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13297. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13298. }
  13299. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  13300. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13301. if (INTEL_INFO(dev)->gen >= 4) {
  13302. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13303. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13304. }
  13305. err_printf(m, "Cursor [%d]:\n", i);
  13306. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13307. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13308. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13309. }
  13310. for (i = 0; i < error->num_transcoders; i++) {
  13311. err_printf(m, "CPU transcoder: %c\n",
  13312. transcoder_name(error->transcoder[i].cpu_transcoder));
  13313. err_printf(m, " Power: %s\n",
  13314. error->transcoder[i].power_domain_on ? "on" : "off");
  13315. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13316. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13317. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13318. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13319. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13320. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13321. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13322. }
  13323. }
  13324. void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
  13325. {
  13326. struct intel_crtc *crtc;
  13327. for_each_intel_crtc(dev, crtc) {
  13328. struct intel_unpin_work *work;
  13329. spin_lock_irq(&dev->event_lock);
  13330. work = crtc->unpin_work;
  13331. if (work && work->event &&
  13332. work->event->base.file_priv == file) {
  13333. kfree(work->event);
  13334. work->event = NULL;
  13335. }
  13336. spin_unlock_irq(&dev->event_lock);
  13337. }
  13338. }