intel_display.c 447 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908
  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 "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  93. struct drm_i915_gem_object *obj,
  94. struct drm_mode_fb_cmd2 *mode_cmd);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. struct intel_limit {
  118. struct {
  119. int min, max;
  120. } dot, vco, n, m, m1, m2, p, p1;
  121. struct {
  122. int dot_limit;
  123. int p2_slow, p2_fast;
  124. } p2;
  125. };
  126. /* returns HPLL frequency in kHz */
  127. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  128. {
  129. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  130. /* Obtain SKU information */
  131. mutex_lock(&dev_priv->sb_lock);
  132. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  133. CCK_FUSE_HPLL_FREQ_MASK;
  134. mutex_unlock(&dev_priv->sb_lock);
  135. return vco_freq[hpll_freq] * 1000;
  136. }
  137. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  138. const char *name, u32 reg, int ref_freq)
  139. {
  140. u32 val;
  141. int divider;
  142. mutex_lock(&dev_priv->sb_lock);
  143. val = vlv_cck_read(dev_priv, reg);
  144. mutex_unlock(&dev_priv->sb_lock);
  145. divider = val & CCK_FREQUENCY_VALUES;
  146. WARN((val & CCK_FREQUENCY_STATUS) !=
  147. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  148. "%s change in progress\n", name);
  149. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  150. }
  151. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  152. const char *name, u32 reg)
  153. {
  154. if (dev_priv->hpll_freq == 0)
  155. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  156. return vlv_get_cck_clock(dev_priv, name, reg,
  157. dev_priv->hpll_freq);
  158. }
  159. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  160. {
  161. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  162. return;
  163. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  164. CCK_CZ_CLOCK_CONTROL);
  165. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  166. }
  167. static inline u32 /* units of 100MHz */
  168. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  169. const struct intel_crtc_state *pipe_config)
  170. {
  171. if (HAS_DDI(dev_priv))
  172. return pipe_config->port_clock; /* SPLL */
  173. else if (IS_GEN5(dev_priv))
  174. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  175. else
  176. return 270000;
  177. }
  178. static const struct intel_limit intel_limits_i8xx_dac = {
  179. .dot = { .min = 25000, .max = 350000 },
  180. .vco = { .min = 908000, .max = 1512000 },
  181. .n = { .min = 2, .max = 16 },
  182. .m = { .min = 96, .max = 140 },
  183. .m1 = { .min = 18, .max = 26 },
  184. .m2 = { .min = 6, .max = 16 },
  185. .p = { .min = 4, .max = 128 },
  186. .p1 = { .min = 2, .max = 33 },
  187. .p2 = { .dot_limit = 165000,
  188. .p2_slow = 4, .p2_fast = 2 },
  189. };
  190. static const struct intel_limit intel_limits_i8xx_dvo = {
  191. .dot = { .min = 25000, .max = 350000 },
  192. .vco = { .min = 908000, .max = 1512000 },
  193. .n = { .min = 2, .max = 16 },
  194. .m = { .min = 96, .max = 140 },
  195. .m1 = { .min = 18, .max = 26 },
  196. .m2 = { .min = 6, .max = 16 },
  197. .p = { .min = 4, .max = 128 },
  198. .p1 = { .min = 2, .max = 33 },
  199. .p2 = { .dot_limit = 165000,
  200. .p2_slow = 4, .p2_fast = 4 },
  201. };
  202. static const struct intel_limit intel_limits_i8xx_lvds = {
  203. .dot = { .min = 25000, .max = 350000 },
  204. .vco = { .min = 908000, .max = 1512000 },
  205. .n = { .min = 2, .max = 16 },
  206. .m = { .min = 96, .max = 140 },
  207. .m1 = { .min = 18, .max = 26 },
  208. .m2 = { .min = 6, .max = 16 },
  209. .p = { .min = 4, .max = 128 },
  210. .p1 = { .min = 1, .max = 6 },
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 14, .p2_fast = 7 },
  213. };
  214. static const struct intel_limit intel_limits_i9xx_sdvo = {
  215. .dot = { .min = 20000, .max = 400000 },
  216. .vco = { .min = 1400000, .max = 2800000 },
  217. .n = { .min = 1, .max = 6 },
  218. .m = { .min = 70, .max = 120 },
  219. .m1 = { .min = 8, .max = 18 },
  220. .m2 = { .min = 3, .max = 7 },
  221. .p = { .min = 5, .max = 80 },
  222. .p1 = { .min = 1, .max = 8 },
  223. .p2 = { .dot_limit = 200000,
  224. .p2_slow = 10, .p2_fast = 5 },
  225. };
  226. static const struct intel_limit intel_limits_i9xx_lvds = {
  227. .dot = { .min = 20000, .max = 400000 },
  228. .vco = { .min = 1400000, .max = 2800000 },
  229. .n = { .min = 1, .max = 6 },
  230. .m = { .min = 70, .max = 120 },
  231. .m1 = { .min = 8, .max = 18 },
  232. .m2 = { .min = 3, .max = 7 },
  233. .p = { .min = 7, .max = 98 },
  234. .p1 = { .min = 1, .max = 8 },
  235. .p2 = { .dot_limit = 112000,
  236. .p2_slow = 14, .p2_fast = 7 },
  237. };
  238. static const struct intel_limit intel_limits_g4x_sdvo = {
  239. .dot = { .min = 25000, .max = 270000 },
  240. .vco = { .min = 1750000, .max = 3500000},
  241. .n = { .min = 1, .max = 4 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 10, .max = 30 },
  246. .p1 = { .min = 1, .max = 3},
  247. .p2 = { .dot_limit = 270000,
  248. .p2_slow = 10,
  249. .p2_fast = 10
  250. },
  251. };
  252. static const struct intel_limit intel_limits_g4x_hdmi = {
  253. .dot = { .min = 22000, .max = 400000 },
  254. .vco = { .min = 1750000, .max = 3500000},
  255. .n = { .min = 1, .max = 4 },
  256. .m = { .min = 104, .max = 138 },
  257. .m1 = { .min = 16, .max = 23 },
  258. .m2 = { .min = 5, .max = 11 },
  259. .p = { .min = 5, .max = 80 },
  260. .p1 = { .min = 1, .max = 8},
  261. .p2 = { .dot_limit = 165000,
  262. .p2_slow = 10, .p2_fast = 5 },
  263. };
  264. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  265. .dot = { .min = 20000, .max = 115000 },
  266. .vco = { .min = 1750000, .max = 3500000 },
  267. .n = { .min = 1, .max = 3 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 28, .max = 112 },
  272. .p1 = { .min = 2, .max = 8 },
  273. .p2 = { .dot_limit = 0,
  274. .p2_slow = 14, .p2_fast = 14
  275. },
  276. };
  277. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  278. .dot = { .min = 80000, .max = 224000 },
  279. .vco = { .min = 1750000, .max = 3500000 },
  280. .n = { .min = 1, .max = 3 },
  281. .m = { .min = 104, .max = 138 },
  282. .m1 = { .min = 17, .max = 23 },
  283. .m2 = { .min = 5, .max = 11 },
  284. .p = { .min = 14, .max = 42 },
  285. .p1 = { .min = 2, .max = 6 },
  286. .p2 = { .dot_limit = 0,
  287. .p2_slow = 7, .p2_fast = 7
  288. },
  289. };
  290. static const struct intel_limit intel_limits_pineview_sdvo = {
  291. .dot = { .min = 20000, .max = 400000},
  292. .vco = { .min = 1700000, .max = 3500000 },
  293. /* Pineview's Ncounter is a ring counter */
  294. .n = { .min = 3, .max = 6 },
  295. .m = { .min = 2, .max = 256 },
  296. /* Pineview only has one combined m divider, which we treat as m2. */
  297. .m1 = { .min = 0, .max = 0 },
  298. .m2 = { .min = 0, .max = 254 },
  299. .p = { .min = 5, .max = 80 },
  300. .p1 = { .min = 1, .max = 8 },
  301. .p2 = { .dot_limit = 200000,
  302. .p2_slow = 10, .p2_fast = 5 },
  303. };
  304. static const struct intel_limit intel_limits_pineview_lvds = {
  305. .dot = { .min = 20000, .max = 400000 },
  306. .vco = { .min = 1700000, .max = 3500000 },
  307. .n = { .min = 3, .max = 6 },
  308. .m = { .min = 2, .max = 256 },
  309. .m1 = { .min = 0, .max = 0 },
  310. .m2 = { .min = 0, .max = 254 },
  311. .p = { .min = 7, .max = 112 },
  312. .p1 = { .min = 1, .max = 8 },
  313. .p2 = { .dot_limit = 112000,
  314. .p2_slow = 14, .p2_fast = 14 },
  315. };
  316. /* Ironlake / Sandybridge
  317. *
  318. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  319. * the range value for them is (actual_value - 2).
  320. */
  321. static const struct intel_limit intel_limits_ironlake_dac = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 5 },
  325. .m = { .min = 79, .max = 127 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 5, .max = 80 },
  329. .p1 = { .min = 1, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 10, .p2_fast = 5 },
  332. };
  333. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 118 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 28, .max = 112 },
  341. .p1 = { .min = 2, .max = 8 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 14, .p2_fast = 14 },
  344. };
  345. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  346. .dot = { .min = 25000, .max = 350000 },
  347. .vco = { .min = 1760000, .max = 3510000 },
  348. .n = { .min = 1, .max = 3 },
  349. .m = { .min = 79, .max = 127 },
  350. .m1 = { .min = 12, .max = 22 },
  351. .m2 = { .min = 5, .max = 9 },
  352. .p = { .min = 14, .max = 56 },
  353. .p1 = { .min = 2, .max = 8 },
  354. .p2 = { .dot_limit = 225000,
  355. .p2_slow = 7, .p2_fast = 7 },
  356. };
  357. /* LVDS 100mhz refclk limits. */
  358. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  359. .dot = { .min = 25000, .max = 350000 },
  360. .vco = { .min = 1760000, .max = 3510000 },
  361. .n = { .min = 1, .max = 2 },
  362. .m = { .min = 79, .max = 126 },
  363. .m1 = { .min = 12, .max = 22 },
  364. .m2 = { .min = 5, .max = 9 },
  365. .p = { .min = 28, .max = 112 },
  366. .p1 = { .min = 2, .max = 8 },
  367. .p2 = { .dot_limit = 225000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 3 },
  374. .m = { .min = 79, .max = 126 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 14, .max = 42 },
  378. .p1 = { .min = 2, .max = 6 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 7, .p2_fast = 7 },
  381. };
  382. static const struct intel_limit intel_limits_vlv = {
  383. /*
  384. * These are the data rate limits (measured in fast clocks)
  385. * since those are the strictest limits we have. The fast
  386. * clock and actual rate limits are more relaxed, so checking
  387. * them would make no difference.
  388. */
  389. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  390. .vco = { .min = 4000000, .max = 6000000 },
  391. .n = { .min = 1, .max = 7 },
  392. .m1 = { .min = 2, .max = 3 },
  393. .m2 = { .min = 11, .max = 156 },
  394. .p1 = { .min = 2, .max = 3 },
  395. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  396. };
  397. static const struct intel_limit intel_limits_chv = {
  398. /*
  399. * These are the data rate limits (measured in fast clocks)
  400. * since those are the strictest limits we have. The fast
  401. * clock and actual rate limits are more relaxed, so checking
  402. * them would make no difference.
  403. */
  404. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  405. .vco = { .min = 4800000, .max = 6480000 },
  406. .n = { .min = 1, .max = 1 },
  407. .m1 = { .min = 2, .max = 2 },
  408. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  409. .p1 = { .min = 2, .max = 4 },
  410. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  411. };
  412. static const struct intel_limit intel_limits_bxt = {
  413. /* FIXME: find real dot limits */
  414. .dot = { .min = 0, .max = INT_MAX },
  415. .vco = { .min = 4800000, .max = 6700000 },
  416. .n = { .min = 1, .max = 1 },
  417. .m1 = { .min = 2, .max = 2 },
  418. /* FIXME: find real m2 limits */
  419. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  420. .p1 = { .min = 2, .max = 4 },
  421. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  422. };
  423. static bool
  424. needs_modeset(struct drm_crtc_state *state)
  425. {
  426. return drm_atomic_crtc_needs_modeset(state);
  427. }
  428. /*
  429. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  430. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  431. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  432. * The helpers' return value is the rate of the clock that is fed to the
  433. * display engine's pipe which can be the above fast dot clock rate or a
  434. * divided-down version of it.
  435. */
  436. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  437. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  438. {
  439. clock->m = clock->m2 + 2;
  440. clock->p = clock->p1 * clock->p2;
  441. if (WARN_ON(clock->n == 0 || clock->p == 0))
  442. return 0;
  443. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  444. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  445. return clock->dot;
  446. }
  447. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  448. {
  449. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  450. }
  451. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  452. {
  453. clock->m = i9xx_dpll_compute_m(clock);
  454. clock->p = clock->p1 * clock->p2;
  455. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  456. return 0;
  457. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  458. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  459. return clock->dot;
  460. }
  461. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  462. {
  463. clock->m = clock->m1 * clock->m2;
  464. clock->p = clock->p1 * clock->p2;
  465. if (WARN_ON(clock->n == 0 || clock->p == 0))
  466. return 0;
  467. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  468. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  469. return clock->dot / 5;
  470. }
  471. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = clock->m1 * clock->m2;
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  478. clock->n << 22);
  479. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  480. return clock->dot / 5;
  481. }
  482. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  483. /**
  484. * Returns whether the given set of divisors are valid for a given refclk with
  485. * the given connectors.
  486. */
  487. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  488. const struct intel_limit *limit,
  489. const struct dpll *clock)
  490. {
  491. if (clock->n < limit->n.min || limit->n.max < clock->n)
  492. INTELPllInvalid("n out of range\n");
  493. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  494. INTELPllInvalid("p1 out of range\n");
  495. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  496. INTELPllInvalid("m2 out of range\n");
  497. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  498. INTELPllInvalid("m1 out of range\n");
  499. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  500. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  501. if (clock->m1 <= clock->m2)
  502. INTELPllInvalid("m1 <= m2\n");
  503. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  504. !IS_GEN9_LP(dev_priv)) {
  505. if (clock->p < limit->p.min || limit->p.max < clock->p)
  506. INTELPllInvalid("p out of range\n");
  507. if (clock->m < limit->m.min || limit->m.max < clock->m)
  508. INTELPllInvalid("m out of range\n");
  509. }
  510. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  511. INTELPllInvalid("vco out of range\n");
  512. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  513. * connector, etc., rather than just a single range.
  514. */
  515. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  516. INTELPllInvalid("dot out of range\n");
  517. return true;
  518. }
  519. static int
  520. i9xx_select_p2_div(const struct intel_limit *limit,
  521. const struct intel_crtc_state *crtc_state,
  522. int target)
  523. {
  524. struct drm_device *dev = crtc_state->base.crtc->dev;
  525. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  526. /*
  527. * For LVDS just rely on its current settings for dual-channel.
  528. * We haven't figured out how to reliably set up different
  529. * single/dual channel state, if we even can.
  530. */
  531. if (intel_is_dual_link_lvds(dev))
  532. return limit->p2.p2_fast;
  533. else
  534. return limit->p2.p2_slow;
  535. } else {
  536. if (target < limit->p2.dot_limit)
  537. return limit->p2.p2_slow;
  538. else
  539. return limit->p2.p2_fast;
  540. }
  541. }
  542. /*
  543. * Returns a set of divisors for the desired target clock with the given
  544. * refclk, or FALSE. The returned values represent the clock equation:
  545. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  546. *
  547. * Target and reference clocks are specified in kHz.
  548. *
  549. * If match_clock is provided, then best_clock P divider must match the P
  550. * divider from @match_clock used for LVDS downclocking.
  551. */
  552. static bool
  553. i9xx_find_best_dpll(const struct intel_limit *limit,
  554. struct intel_crtc_state *crtc_state,
  555. int target, int refclk, struct dpll *match_clock,
  556. struct dpll *best_clock)
  557. {
  558. struct drm_device *dev = crtc_state->base.crtc->dev;
  559. struct dpll clock;
  560. int err = target;
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  563. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  564. clock.m1++) {
  565. for (clock.m2 = limit->m2.min;
  566. clock.m2 <= limit->m2.max; clock.m2++) {
  567. if (clock.m2 >= clock.m1)
  568. break;
  569. for (clock.n = limit->n.min;
  570. clock.n <= limit->n.max; clock.n++) {
  571. for (clock.p1 = limit->p1.min;
  572. clock.p1 <= limit->p1.max; clock.p1++) {
  573. int this_err;
  574. i9xx_calc_dpll_params(refclk, &clock);
  575. if (!intel_PLL_is_valid(to_i915(dev),
  576. limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. /*
  594. * Returns a set of divisors for the desired target clock with the given
  595. * refclk, or FALSE. The returned values represent the clock equation:
  596. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  597. *
  598. * Target and reference clocks are specified in kHz.
  599. *
  600. * If match_clock is provided, then best_clock P divider must match the P
  601. * divider from @match_clock used for LVDS downclocking.
  602. */
  603. static bool
  604. pnv_find_best_dpll(const struct intel_limit *limit,
  605. struct intel_crtc_state *crtc_state,
  606. int target, int refclk, struct dpll *match_clock,
  607. struct dpll *best_clock)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. struct dpll clock;
  611. int err = target;
  612. memset(best_clock, 0, sizeof(*best_clock));
  613. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  614. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  615. clock.m1++) {
  616. for (clock.m2 = limit->m2.min;
  617. clock.m2 <= limit->m2.max; clock.m2++) {
  618. for (clock.n = limit->n.min;
  619. clock.n <= limit->n.max; clock.n++) {
  620. for (clock.p1 = limit->p1.min;
  621. clock.p1 <= limit->p1.max; clock.p1++) {
  622. int this_err;
  623. pnv_calc_dpll_params(refclk, &clock);
  624. if (!intel_PLL_is_valid(to_i915(dev),
  625. limit,
  626. &clock))
  627. continue;
  628. if (match_clock &&
  629. clock.p != match_clock->p)
  630. continue;
  631. this_err = abs(clock.dot - target);
  632. if (this_err < err) {
  633. *best_clock = clock;
  634. err = this_err;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. return (err != target);
  641. }
  642. /*
  643. * Returns a set of divisors for the desired target clock with the given
  644. * refclk, or FALSE. The returned values represent the clock equation:
  645. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  646. *
  647. * Target and reference clocks are specified in kHz.
  648. *
  649. * If match_clock is provided, then best_clock P divider must match the P
  650. * divider from @match_clock used for LVDS downclocking.
  651. */
  652. static bool
  653. g4x_find_best_dpll(const struct intel_limit *limit,
  654. struct intel_crtc_state *crtc_state,
  655. int target, int refclk, struct dpll *match_clock,
  656. struct dpll *best_clock)
  657. {
  658. struct drm_device *dev = crtc_state->base.crtc->dev;
  659. struct dpll clock;
  660. int max_n;
  661. bool found = false;
  662. /* approximately equals target * 0.00585 */
  663. int err_most = (target >> 8) + (target >> 9);
  664. memset(best_clock, 0, sizeof(*best_clock));
  665. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  666. max_n = limit->n.max;
  667. /* based on hardware requirement, prefer smaller n to precision */
  668. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  669. /* based on hardware requirement, prefere larger m1,m2 */
  670. for (clock.m1 = limit->m1.max;
  671. clock.m1 >= limit->m1.min; clock.m1--) {
  672. for (clock.m2 = limit->m2.max;
  673. clock.m2 >= limit->m2.min; clock.m2--) {
  674. for (clock.p1 = limit->p1.max;
  675. clock.p1 >= limit->p1.min; clock.p1--) {
  676. int this_err;
  677. i9xx_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. /*
  696. * Check if the calculated PLL configuration is more optimal compared to the
  697. * best configuration and error found so far. Return the calculated error.
  698. */
  699. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  700. const struct dpll *calculated_clock,
  701. const struct dpll *best_clock,
  702. unsigned int best_error_ppm,
  703. unsigned int *error_ppm)
  704. {
  705. /*
  706. * For CHV ignore the error and consider only the P value.
  707. * Prefer a bigger P value based on HW requirements.
  708. */
  709. if (IS_CHERRYVIEW(to_i915(dev))) {
  710. *error_ppm = 0;
  711. return calculated_clock->p > best_clock->p;
  712. }
  713. if (WARN_ON_ONCE(!target_freq))
  714. return false;
  715. *error_ppm = div_u64(1000000ULL *
  716. abs(target_freq - calculated_clock->dot),
  717. target_freq);
  718. /*
  719. * Prefer a better P value over a better (smaller) error if the error
  720. * is small. Ensure this preference for future configurations too by
  721. * setting the error to 0.
  722. */
  723. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  724. *error_ppm = 0;
  725. return true;
  726. }
  727. return *error_ppm + 10 < best_error_ppm;
  728. }
  729. /*
  730. * Returns a set of divisors for the desired target clock with the given
  731. * refclk, or FALSE. The returned values represent the clock equation:
  732. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  733. */
  734. static bool
  735. vlv_find_best_dpll(const struct intel_limit *limit,
  736. struct intel_crtc_state *crtc_state,
  737. int target, int refclk, struct dpll *match_clock,
  738. struct dpll *best_clock)
  739. {
  740. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  741. struct drm_device *dev = crtc->base.dev;
  742. struct dpll clock;
  743. unsigned int bestppm = 1000000;
  744. /* min update 19.2 MHz */
  745. int max_n = min(limit->n.max, refclk / 19200);
  746. bool found = false;
  747. target *= 5; /* fast clock */
  748. memset(best_clock, 0, sizeof(*best_clock));
  749. /* based on hardware requirement, prefer smaller n to precision */
  750. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  751. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  752. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  753. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  754. clock.p = clock.p1 * clock.p2;
  755. /* based on hardware requirement, prefer bigger m1,m2 values */
  756. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  757. unsigned int ppm;
  758. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  759. refclk * clock.m1);
  760. vlv_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(to_i915(dev),
  762. limit,
  763. &clock))
  764. continue;
  765. if (!vlv_PLL_is_optimal(dev, target,
  766. &clock,
  767. best_clock,
  768. bestppm, &ppm))
  769. continue;
  770. *best_clock = clock;
  771. bestppm = ppm;
  772. found = true;
  773. }
  774. }
  775. }
  776. }
  777. return found;
  778. }
  779. /*
  780. * Returns a set of divisors for the desired target clock with the given
  781. * refclk, or FALSE. The returned values represent the clock equation:
  782. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  783. */
  784. static bool
  785. chv_find_best_dpll(const struct intel_limit *limit,
  786. struct intel_crtc_state *crtc_state,
  787. int target, int refclk, struct dpll *match_clock,
  788. struct dpll *best_clock)
  789. {
  790. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  791. struct drm_device *dev = crtc->base.dev;
  792. unsigned int best_error_ppm;
  793. struct dpll clock;
  794. uint64_t m2;
  795. int found = false;
  796. memset(best_clock, 0, sizeof(*best_clock));
  797. best_error_ppm = 1000000;
  798. /*
  799. * Based on hardware doc, the n always set to 1, and m1 always
  800. * set to 2. If requires to support 200Mhz refclk, we need to
  801. * revisit this because n may not 1 anymore.
  802. */
  803. clock.n = 1, clock.m1 = 2;
  804. target *= 5; /* fast clock */
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast;
  807. clock.p2 >= limit->p2.p2_slow;
  808. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  809. unsigned int error_ppm;
  810. clock.p = clock.p1 * clock.p2;
  811. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  812. clock.n) << 22, refclk * clock.m1);
  813. if (m2 > INT_MAX/clock.m1)
  814. continue;
  815. clock.m2 = m2;
  816. chv_calc_dpll_params(refclk, &clock);
  817. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  820. best_error_ppm, &error_ppm))
  821. continue;
  822. *best_clock = clock;
  823. best_error_ppm = error_ppm;
  824. found = true;
  825. }
  826. }
  827. return found;
  828. }
  829. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  830. struct dpll *best_clock)
  831. {
  832. int refclk = 100000;
  833. const struct intel_limit *limit = &intel_limits_bxt;
  834. return chv_find_best_dpll(limit, crtc_state,
  835. target_clock, refclk, NULL, best_clock);
  836. }
  837. bool intel_crtc_active(struct intel_crtc *crtc)
  838. {
  839. /* Be paranoid as we can arrive here with only partial
  840. * state retrieved from the hardware during setup.
  841. *
  842. * We can ditch the adjusted_mode.crtc_clock check as soon
  843. * as Haswell has gained clock readout/fastboot support.
  844. *
  845. * We can ditch the crtc->primary->fb check as soon as we can
  846. * properly reconstruct framebuffers.
  847. *
  848. * FIXME: The intel_crtc->active here should be switched to
  849. * crtc->state->active once we have proper CRTC states wired up
  850. * for atomic.
  851. */
  852. return crtc->active && crtc->base.primary->state->fb &&
  853. crtc->config->base.adjusted_mode.crtc_clock;
  854. }
  855. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  856. enum pipe pipe)
  857. {
  858. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  859. return crtc->config->cpu_transcoder;
  860. }
  861. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  862. {
  863. i915_reg_t reg = PIPEDSL(pipe);
  864. u32 line1, line2;
  865. u32 line_mask;
  866. if (IS_GEN2(dev_priv))
  867. line_mask = DSL_LINEMASK_GEN2;
  868. else
  869. line_mask = DSL_LINEMASK_GEN3;
  870. line1 = I915_READ(reg) & line_mask;
  871. msleep(5);
  872. line2 = I915_READ(reg) & line_mask;
  873. return line1 == line2;
  874. }
  875. /*
  876. * intel_wait_for_pipe_off - wait for pipe to turn off
  877. * @crtc: crtc whose pipe to wait for
  878. *
  879. * After disabling a pipe, we can't wait for vblank in the usual way,
  880. * spinning on the vblank interrupt status bit, since we won't actually
  881. * see an interrupt when the pipe is disabled.
  882. *
  883. * On Gen4 and above:
  884. * wait for the pipe register state bit to turn off
  885. *
  886. * Otherwise:
  887. * wait for the display line value to settle (it usually
  888. * ends up stopping at the start of the next frame).
  889. *
  890. */
  891. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  892. {
  893. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  894. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  895. enum pipe pipe = crtc->pipe;
  896. if (INTEL_GEN(dev_priv) >= 4) {
  897. i915_reg_t reg = PIPECONF(cpu_transcoder);
  898. /* Wait for the Pipe State to go off */
  899. if (intel_wait_for_register(dev_priv,
  900. reg, I965_PIPECONF_ACTIVE, 0,
  901. 100))
  902. WARN(1, "pipe_off wait timed out\n");
  903. } else {
  904. /* Wait for the display line to settle */
  905. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  906. WARN(1, "pipe_off wait timed out\n");
  907. }
  908. }
  909. /* Only for pre-ILK configs */
  910. void assert_pll(struct drm_i915_private *dev_priv,
  911. enum pipe pipe, bool state)
  912. {
  913. u32 val;
  914. bool cur_state;
  915. val = I915_READ(DPLL(pipe));
  916. cur_state = !!(val & DPLL_VCO_ENABLE);
  917. I915_STATE_WARN(cur_state != state,
  918. "PLL state assertion failure (expected %s, current %s)\n",
  919. onoff(state), onoff(cur_state));
  920. }
  921. /* XXX: the dsi pll is shared between MIPI DSI ports */
  922. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  923. {
  924. u32 val;
  925. bool cur_state;
  926. mutex_lock(&dev_priv->sb_lock);
  927. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  928. mutex_unlock(&dev_priv->sb_lock);
  929. cur_state = val & DSI_PLL_VCO_EN;
  930. I915_STATE_WARN(cur_state != state,
  931. "DSI PLL state assertion failure (expected %s, current %s)\n",
  932. onoff(state), onoff(cur_state));
  933. }
  934. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  935. enum pipe pipe, bool state)
  936. {
  937. bool cur_state;
  938. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  939. pipe);
  940. if (HAS_DDI(dev_priv)) {
  941. /* DDI does not have a specific FDI_TX register */
  942. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  943. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  944. } else {
  945. u32 val = I915_READ(FDI_TX_CTL(pipe));
  946. cur_state = !!(val & FDI_TX_ENABLE);
  947. }
  948. I915_STATE_WARN(cur_state != state,
  949. "FDI TX state assertion failure (expected %s, current %s)\n",
  950. onoff(state), onoff(cur_state));
  951. }
  952. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  953. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  954. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  955. enum pipe pipe, bool state)
  956. {
  957. u32 val;
  958. bool cur_state;
  959. val = I915_READ(FDI_RX_CTL(pipe));
  960. cur_state = !!(val & FDI_RX_ENABLE);
  961. I915_STATE_WARN(cur_state != state,
  962. "FDI RX state assertion failure (expected %s, current %s)\n",
  963. onoff(state), onoff(cur_state));
  964. }
  965. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  966. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  967. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  968. enum pipe pipe)
  969. {
  970. u32 val;
  971. /* ILK FDI PLL is always enabled */
  972. if (IS_GEN5(dev_priv))
  973. return;
  974. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  975. if (HAS_DDI(dev_priv))
  976. return;
  977. val = I915_READ(FDI_TX_CTL(pipe));
  978. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  979. }
  980. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  981. enum pipe pipe, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. val = I915_READ(FDI_RX_CTL(pipe));
  986. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  987. I915_STATE_WARN(cur_state != state,
  988. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  989. onoff(state), onoff(cur_state));
  990. }
  991. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  992. {
  993. i915_reg_t pp_reg;
  994. u32 val;
  995. enum pipe panel_pipe = PIPE_A;
  996. bool locked = true;
  997. if (WARN_ON(HAS_DDI(dev_priv)))
  998. return;
  999. if (HAS_PCH_SPLIT(dev_priv)) {
  1000. u32 port_sel;
  1001. pp_reg = PP_CONTROL(0);
  1002. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1003. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1004. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1005. panel_pipe = PIPE_B;
  1006. /* XXX: else fix for eDP */
  1007. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1008. /* presumably write lock depends on pipe, not port select */
  1009. pp_reg = PP_CONTROL(pipe);
  1010. panel_pipe = pipe;
  1011. } else {
  1012. pp_reg = PP_CONTROL(0);
  1013. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1014. panel_pipe = PIPE_B;
  1015. }
  1016. val = I915_READ(pp_reg);
  1017. if (!(val & PANEL_POWER_ON) ||
  1018. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1019. locked = false;
  1020. I915_STATE_WARN(panel_pipe == pipe && locked,
  1021. "panel assertion failure, pipe %c regs locked\n",
  1022. pipe_name(pipe));
  1023. }
  1024. static void assert_cursor(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. bool cur_state;
  1028. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1029. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1030. else
  1031. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1032. I915_STATE_WARN(cur_state != state,
  1033. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1034. pipe_name(pipe), onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1037. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1038. void assert_pipe(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. bool cur_state;
  1042. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1043. pipe);
  1044. enum intel_display_power_domain power_domain;
  1045. /* if we need the pipe quirk it must be always on */
  1046. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1047. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1048. state = true;
  1049. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1050. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1051. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1052. cur_state = !!(val & PIPECONF_ENABLE);
  1053. intel_display_power_put(dev_priv, power_domain);
  1054. } else {
  1055. cur_state = false;
  1056. }
  1057. I915_STATE_WARN(cur_state != state,
  1058. "pipe %c assertion failure (expected %s, current %s)\n",
  1059. pipe_name(pipe), onoff(state), onoff(cur_state));
  1060. }
  1061. static void assert_plane(struct drm_i915_private *dev_priv,
  1062. enum plane plane, bool state)
  1063. {
  1064. u32 val;
  1065. bool cur_state;
  1066. val = I915_READ(DSPCNTR(plane));
  1067. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1068. I915_STATE_WARN(cur_state != state,
  1069. "plane %c assertion failure (expected %s, current %s)\n",
  1070. plane_name(plane), onoff(state), onoff(cur_state));
  1071. }
  1072. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1073. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1074. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe)
  1076. {
  1077. int i;
  1078. /* Primary planes are fixed to pipes on gen4+ */
  1079. if (INTEL_GEN(dev_priv) >= 4) {
  1080. u32 val = I915_READ(DSPCNTR(pipe));
  1081. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1082. "plane %c assertion failure, should be disabled but not\n",
  1083. plane_name(pipe));
  1084. return;
  1085. }
  1086. /* Need to check both planes against the pipe */
  1087. for_each_pipe(dev_priv, i) {
  1088. u32 val = I915_READ(DSPCNTR(i));
  1089. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1090. DISPPLANE_SEL_PIPE_SHIFT;
  1091. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1092. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1093. plane_name(i), pipe_name(pipe));
  1094. }
  1095. }
  1096. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. int sprite;
  1100. if (INTEL_GEN(dev_priv) >= 9) {
  1101. for_each_sprite(dev_priv, pipe, sprite) {
  1102. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1103. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1104. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1105. sprite, pipe_name(pipe));
  1106. }
  1107. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1108. for_each_sprite(dev_priv, pipe, sprite) {
  1109. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1110. I915_STATE_WARN(val & SP_ENABLE,
  1111. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1112. sprite_name(pipe, sprite), pipe_name(pipe));
  1113. }
  1114. } else if (INTEL_GEN(dev_priv) >= 7) {
  1115. u32 val = I915_READ(SPRCTL(pipe));
  1116. I915_STATE_WARN(val & SPRITE_ENABLE,
  1117. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1118. plane_name(pipe), pipe_name(pipe));
  1119. } else if (INTEL_GEN(dev_priv) >= 5) {
  1120. u32 val = I915_READ(DVSCNTR(pipe));
  1121. I915_STATE_WARN(val & DVS_ENABLE,
  1122. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1123. plane_name(pipe), pipe_name(pipe));
  1124. }
  1125. }
  1126. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1127. {
  1128. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1129. drm_crtc_vblank_put(crtc);
  1130. }
  1131. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe)
  1133. {
  1134. u32 val;
  1135. bool enabled;
  1136. val = I915_READ(PCH_TRANSCONF(pipe));
  1137. enabled = !!(val & TRANS_ENABLE);
  1138. I915_STATE_WARN(enabled,
  1139. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1140. pipe_name(pipe));
  1141. }
  1142. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1143. enum pipe pipe, u32 port_sel, u32 val)
  1144. {
  1145. if ((val & DP_PORT_EN) == 0)
  1146. return false;
  1147. if (HAS_PCH_CPT(dev_priv)) {
  1148. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1149. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1150. return false;
  1151. } else if (IS_CHERRYVIEW(dev_priv)) {
  1152. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1153. return false;
  1154. } else {
  1155. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe, u32 val)
  1162. {
  1163. if ((val & SDVO_ENABLE) == 0)
  1164. return false;
  1165. if (HAS_PCH_CPT(dev_priv)) {
  1166. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1167. return false;
  1168. } else if (IS_CHERRYVIEW(dev_priv)) {
  1169. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1170. return false;
  1171. } else {
  1172. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1178. enum pipe pipe, u32 val)
  1179. {
  1180. if ((val & LVDS_PORT_EN) == 0)
  1181. return false;
  1182. if (HAS_PCH_CPT(dev_priv)) {
  1183. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1184. return false;
  1185. } else {
  1186. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1187. return false;
  1188. }
  1189. return true;
  1190. }
  1191. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1192. enum pipe pipe, u32 val)
  1193. {
  1194. if ((val & ADPA_DAC_ENABLE) == 0)
  1195. return false;
  1196. if (HAS_PCH_CPT(dev_priv)) {
  1197. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1198. return false;
  1199. } else {
  1200. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe, i915_reg_t reg,
  1207. u32 port_sel)
  1208. {
  1209. u32 val = I915_READ(reg);
  1210. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1211. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1212. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1213. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1214. && (val & DP_PIPEB_SELECT),
  1215. "IBX PCH dp port still using transcoder B\n");
  1216. }
  1217. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, i915_reg_t reg)
  1219. {
  1220. u32 val = I915_READ(reg);
  1221. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1222. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1223. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1224. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1225. && (val & SDVO_PIPE_B_SELECT),
  1226. "IBX PCH hdmi port still using transcoder B\n");
  1227. }
  1228. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1229. enum pipe pipe)
  1230. {
  1231. u32 val;
  1232. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1233. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1234. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1235. val = I915_READ(PCH_ADPA);
  1236. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1237. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1238. pipe_name(pipe));
  1239. val = I915_READ(PCH_LVDS);
  1240. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1241. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1242. pipe_name(pipe));
  1243. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1244. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1245. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1246. }
  1247. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1248. const struct intel_crtc_state *pipe_config)
  1249. {
  1250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1251. enum pipe pipe = crtc->pipe;
  1252. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1253. POSTING_READ(DPLL(pipe));
  1254. udelay(150);
  1255. if (intel_wait_for_register(dev_priv,
  1256. DPLL(pipe),
  1257. DPLL_LOCK_VLV,
  1258. DPLL_LOCK_VLV,
  1259. 1))
  1260. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1261. }
  1262. static void vlv_enable_pll(struct intel_crtc *crtc,
  1263. const struct intel_crtc_state *pipe_config)
  1264. {
  1265. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1266. enum pipe pipe = crtc->pipe;
  1267. assert_pipe_disabled(dev_priv, pipe);
  1268. /* PLL is protected by panel, make sure we can write it */
  1269. assert_panel_unlocked(dev_priv, pipe);
  1270. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1271. _vlv_enable_pll(crtc, pipe_config);
  1272. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1273. POSTING_READ(DPLL_MD(pipe));
  1274. }
  1275. static void _chv_enable_pll(struct intel_crtc *crtc,
  1276. const struct intel_crtc_state *pipe_config)
  1277. {
  1278. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1279. enum pipe pipe = crtc->pipe;
  1280. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1281. u32 tmp;
  1282. mutex_lock(&dev_priv->sb_lock);
  1283. /* Enable back the 10bit clock to display controller */
  1284. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1285. tmp |= DPIO_DCLKP_EN;
  1286. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1287. mutex_unlock(&dev_priv->sb_lock);
  1288. /*
  1289. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1290. */
  1291. udelay(1);
  1292. /* Enable PLL */
  1293. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1294. /* Check PLL is locked */
  1295. if (intel_wait_for_register(dev_priv,
  1296. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1297. 1))
  1298. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1299. }
  1300. static void chv_enable_pll(struct intel_crtc *crtc,
  1301. const struct intel_crtc_state *pipe_config)
  1302. {
  1303. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1304. enum pipe pipe = crtc->pipe;
  1305. assert_pipe_disabled(dev_priv, pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. assert_panel_unlocked(dev_priv, pipe);
  1308. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1309. _chv_enable_pll(crtc, pipe_config);
  1310. if (pipe != PIPE_A) {
  1311. /*
  1312. * WaPixelRepeatModeFixForC0:chv
  1313. *
  1314. * DPLLCMD is AWOL. Use chicken bits to propagate
  1315. * the value from DPLLBMD to either pipe B or C.
  1316. */
  1317. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1318. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1319. I915_WRITE(CBR4_VLV, 0);
  1320. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1321. /*
  1322. * DPLLB VGA mode also seems to cause problems.
  1323. * We should always have it disabled.
  1324. */
  1325. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1326. } else {
  1327. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1328. POSTING_READ(DPLL_MD(pipe));
  1329. }
  1330. }
  1331. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1332. {
  1333. struct intel_crtc *crtc;
  1334. int count = 0;
  1335. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1336. count += crtc->base.state->active &&
  1337. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1338. }
  1339. return count;
  1340. }
  1341. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. i915_reg_t reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* PLL is protected by panel, make sure we can write it */
  1348. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1349. assert_panel_unlocked(dev_priv, crtc->pipe);
  1350. /* Enable DVO 2x clock on both PLLs if necessary */
  1351. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1352. /*
  1353. * It appears to be important that we don't enable this
  1354. * for the current pipe before otherwise configuring the
  1355. * PLL. No idea how this should be handled if multiple
  1356. * DVO outputs are enabled simultaneosly.
  1357. */
  1358. dpll |= DPLL_DVO_2X_MODE;
  1359. I915_WRITE(DPLL(!crtc->pipe),
  1360. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1361. }
  1362. /*
  1363. * Apparently we need to have VGA mode enabled prior to changing
  1364. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1365. * dividers, even though the register value does change.
  1366. */
  1367. I915_WRITE(reg, 0);
  1368. I915_WRITE(reg, dpll);
  1369. /* Wait for the clocks to stabilize. */
  1370. POSTING_READ(reg);
  1371. udelay(150);
  1372. if (INTEL_GEN(dev_priv) >= 4) {
  1373. I915_WRITE(DPLL_MD(crtc->pipe),
  1374. crtc->config->dpll_hw_state.dpll_md);
  1375. } else {
  1376. /* The pixel multiplier can only be updated once the
  1377. * DPLL is enabled and the clocks are stable.
  1378. *
  1379. * So write it again.
  1380. */
  1381. I915_WRITE(reg, dpll);
  1382. }
  1383. /* We do this three times for luck */
  1384. I915_WRITE(reg, dpll);
  1385. POSTING_READ(reg);
  1386. udelay(150); /* wait for warmup */
  1387. I915_WRITE(reg, dpll);
  1388. POSTING_READ(reg);
  1389. udelay(150); /* wait for warmup */
  1390. I915_WRITE(reg, dpll);
  1391. POSTING_READ(reg);
  1392. udelay(150); /* wait for warmup */
  1393. }
  1394. /**
  1395. * i9xx_disable_pll - disable a PLL
  1396. * @dev_priv: i915 private structure
  1397. * @pipe: pipe PLL to disable
  1398. *
  1399. * Disable the PLL for @pipe, making sure the pipe is off first.
  1400. *
  1401. * Note! This is for pre-ILK only.
  1402. */
  1403. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1404. {
  1405. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1406. enum pipe pipe = crtc->pipe;
  1407. /* Disable DVO 2x clock on both PLLs if necessary */
  1408. if (IS_I830(dev_priv) &&
  1409. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1410. !intel_num_dvo_pipes(dev_priv)) {
  1411. I915_WRITE(DPLL(PIPE_B),
  1412. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1413. I915_WRITE(DPLL(PIPE_A),
  1414. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1415. }
  1416. /* Don't disable pipe or pipe PLLs if needed */
  1417. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1418. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1419. return;
  1420. /* Make sure the pipe isn't still relying on us */
  1421. assert_pipe_disabled(dev_priv, pipe);
  1422. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1423. POSTING_READ(DPLL(pipe));
  1424. }
  1425. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1426. {
  1427. u32 val;
  1428. /* Make sure the pipe isn't still relying on us */
  1429. assert_pipe_disabled(dev_priv, pipe);
  1430. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1431. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1432. if (pipe != PIPE_A)
  1433. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1434. I915_WRITE(DPLL(pipe), val);
  1435. POSTING_READ(DPLL(pipe));
  1436. }
  1437. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1438. {
  1439. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1440. u32 val;
  1441. /* Make sure the pipe isn't still relying on us */
  1442. assert_pipe_disabled(dev_priv, pipe);
  1443. val = DPLL_SSC_REF_CLK_CHV |
  1444. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1445. if (pipe != PIPE_A)
  1446. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1447. I915_WRITE(DPLL(pipe), val);
  1448. POSTING_READ(DPLL(pipe));
  1449. mutex_lock(&dev_priv->sb_lock);
  1450. /* Disable 10bit clock to display controller */
  1451. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1452. val &= ~DPIO_DCLKP_EN;
  1453. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1454. mutex_unlock(&dev_priv->sb_lock);
  1455. }
  1456. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1457. struct intel_digital_port *dport,
  1458. unsigned int expected_mask)
  1459. {
  1460. u32 port_mask;
  1461. i915_reg_t dpll_reg;
  1462. switch (dport->port) {
  1463. case PORT_B:
  1464. port_mask = DPLL_PORTB_READY_MASK;
  1465. dpll_reg = DPLL(0);
  1466. break;
  1467. case PORT_C:
  1468. port_mask = DPLL_PORTC_READY_MASK;
  1469. dpll_reg = DPLL(0);
  1470. expected_mask <<= 4;
  1471. break;
  1472. case PORT_D:
  1473. port_mask = DPLL_PORTD_READY_MASK;
  1474. dpll_reg = DPIO_PHY_STATUS;
  1475. break;
  1476. default:
  1477. BUG();
  1478. }
  1479. if (intel_wait_for_register(dev_priv,
  1480. dpll_reg, port_mask, expected_mask,
  1481. 1000))
  1482. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1483. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1484. }
  1485. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1486. enum pipe pipe)
  1487. {
  1488. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1489. pipe);
  1490. i915_reg_t reg;
  1491. uint32_t val, pipeconf_val;
  1492. /* Make sure PCH DPLL is enabled */
  1493. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1494. /* FDI must be feeding us bits for PCH ports */
  1495. assert_fdi_tx_enabled(dev_priv, pipe);
  1496. assert_fdi_rx_enabled(dev_priv, pipe);
  1497. if (HAS_PCH_CPT(dev_priv)) {
  1498. /* Workaround: Set the timing override bit before enabling the
  1499. * pch transcoder. */
  1500. reg = TRANS_CHICKEN2(pipe);
  1501. val = I915_READ(reg);
  1502. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1503. I915_WRITE(reg, val);
  1504. }
  1505. reg = PCH_TRANSCONF(pipe);
  1506. val = I915_READ(reg);
  1507. pipeconf_val = I915_READ(PIPECONF(pipe));
  1508. if (HAS_PCH_IBX(dev_priv)) {
  1509. /*
  1510. * Make the BPC in transcoder be consistent with
  1511. * that in pipeconf reg. For HDMI we must use 8bpc
  1512. * here for both 8bpc and 12bpc.
  1513. */
  1514. val &= ~PIPECONF_BPC_MASK;
  1515. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1516. val |= PIPECONF_8BPC;
  1517. else
  1518. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1519. }
  1520. val &= ~TRANS_INTERLACE_MASK;
  1521. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1522. if (HAS_PCH_IBX(dev_priv) &&
  1523. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1524. val |= TRANS_LEGACY_INTERLACED_ILK;
  1525. else
  1526. val |= TRANS_INTERLACED;
  1527. else
  1528. val |= TRANS_PROGRESSIVE;
  1529. I915_WRITE(reg, val | TRANS_ENABLE);
  1530. if (intel_wait_for_register(dev_priv,
  1531. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1532. 100))
  1533. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1534. }
  1535. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1536. enum transcoder cpu_transcoder)
  1537. {
  1538. u32 val, pipeconf_val;
  1539. /* FDI must be feeding us bits for PCH ports */
  1540. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1541. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1542. /* Workaround: set timing override bit. */
  1543. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1544. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1545. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1546. val = TRANS_ENABLE;
  1547. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1548. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1549. PIPECONF_INTERLACED_ILK)
  1550. val |= TRANS_INTERLACED;
  1551. else
  1552. val |= TRANS_PROGRESSIVE;
  1553. I915_WRITE(LPT_TRANSCONF, val);
  1554. if (intel_wait_for_register(dev_priv,
  1555. LPT_TRANSCONF,
  1556. TRANS_STATE_ENABLE,
  1557. TRANS_STATE_ENABLE,
  1558. 100))
  1559. DRM_ERROR("Failed to enable PCH transcoder\n");
  1560. }
  1561. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1562. enum pipe pipe)
  1563. {
  1564. i915_reg_t reg;
  1565. uint32_t val;
  1566. /* FDI relies on the transcoder */
  1567. assert_fdi_tx_disabled(dev_priv, pipe);
  1568. assert_fdi_rx_disabled(dev_priv, pipe);
  1569. /* Ports must be off as well */
  1570. assert_pch_ports_disabled(dev_priv, pipe);
  1571. reg = PCH_TRANSCONF(pipe);
  1572. val = I915_READ(reg);
  1573. val &= ~TRANS_ENABLE;
  1574. I915_WRITE(reg, val);
  1575. /* wait for PCH transcoder off, transcoder state */
  1576. if (intel_wait_for_register(dev_priv,
  1577. reg, TRANS_STATE_ENABLE, 0,
  1578. 50))
  1579. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1580. if (HAS_PCH_CPT(dev_priv)) {
  1581. /* Workaround: Clear the timing override chicken bit again. */
  1582. reg = TRANS_CHICKEN2(pipe);
  1583. val = I915_READ(reg);
  1584. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1585. I915_WRITE(reg, val);
  1586. }
  1587. }
  1588. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1589. {
  1590. u32 val;
  1591. val = I915_READ(LPT_TRANSCONF);
  1592. val &= ~TRANS_ENABLE;
  1593. I915_WRITE(LPT_TRANSCONF, val);
  1594. /* wait for PCH transcoder off, transcoder state */
  1595. if (intel_wait_for_register(dev_priv,
  1596. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1597. 50))
  1598. DRM_ERROR("Failed to disable PCH transcoder\n");
  1599. /* Workaround: clear timing override bit. */
  1600. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1603. }
  1604. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1605. {
  1606. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1607. WARN_ON(!crtc->config->has_pch_encoder);
  1608. if (HAS_PCH_LPT(dev_priv))
  1609. return TRANSCODER_A;
  1610. else
  1611. return (enum transcoder) crtc->pipe;
  1612. }
  1613. /**
  1614. * intel_enable_pipe - enable a pipe, asserting requirements
  1615. * @crtc: crtc responsible for the pipe
  1616. *
  1617. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1618. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1619. */
  1620. static void intel_enable_pipe(struct intel_crtc *crtc)
  1621. {
  1622. struct drm_device *dev = crtc->base.dev;
  1623. struct drm_i915_private *dev_priv = to_i915(dev);
  1624. enum pipe pipe = crtc->pipe;
  1625. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1626. i915_reg_t reg;
  1627. u32 val;
  1628. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1629. assert_planes_disabled(dev_priv, pipe);
  1630. assert_cursor_disabled(dev_priv, pipe);
  1631. assert_sprites_disabled(dev_priv, pipe);
  1632. /*
  1633. * A pipe without a PLL won't actually be able to drive bits from
  1634. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1635. * need the check.
  1636. */
  1637. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1638. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1639. assert_dsi_pll_enabled(dev_priv);
  1640. else
  1641. assert_pll_enabled(dev_priv, pipe);
  1642. } else {
  1643. if (crtc->config->has_pch_encoder) {
  1644. /* if driving the PCH, we need FDI enabled */
  1645. assert_fdi_rx_pll_enabled(dev_priv,
  1646. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1647. assert_fdi_tx_pll_enabled(dev_priv,
  1648. (enum pipe) cpu_transcoder);
  1649. }
  1650. /* FIXME: assert CPU port conditions for SNB+ */
  1651. }
  1652. reg = PIPECONF(cpu_transcoder);
  1653. val = I915_READ(reg);
  1654. if (val & PIPECONF_ENABLE) {
  1655. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1656. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1657. return;
  1658. }
  1659. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1660. POSTING_READ(reg);
  1661. /*
  1662. * Until the pipe starts DSL will read as 0, which would cause
  1663. * an apparent vblank timestamp jump, which messes up also the
  1664. * frame count when it's derived from the timestamps. So let's
  1665. * wait for the pipe to start properly before we call
  1666. * drm_crtc_vblank_on()
  1667. */
  1668. if (dev->max_vblank_count == 0 &&
  1669. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1670. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1671. }
  1672. /**
  1673. * intel_disable_pipe - disable a pipe, asserting requirements
  1674. * @crtc: crtc whose pipes is to be disabled
  1675. *
  1676. * Disable the pipe of @crtc, making sure that various hardware
  1677. * specific requirements are met, if applicable, e.g. plane
  1678. * disabled, panel fitter off, etc.
  1679. *
  1680. * Will wait until the pipe has shut down before returning.
  1681. */
  1682. static void intel_disable_pipe(struct intel_crtc *crtc)
  1683. {
  1684. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1685. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1686. enum pipe pipe = crtc->pipe;
  1687. i915_reg_t reg;
  1688. u32 val;
  1689. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1690. /*
  1691. * Make sure planes won't keep trying to pump pixels to us,
  1692. * or we might hang the display.
  1693. */
  1694. assert_planes_disabled(dev_priv, pipe);
  1695. assert_cursor_disabled(dev_priv, pipe);
  1696. assert_sprites_disabled(dev_priv, pipe);
  1697. reg = PIPECONF(cpu_transcoder);
  1698. val = I915_READ(reg);
  1699. if ((val & PIPECONF_ENABLE) == 0)
  1700. return;
  1701. /*
  1702. * Double wide has implications for planes
  1703. * so best keep it disabled when not needed.
  1704. */
  1705. if (crtc->config->double_wide)
  1706. val &= ~PIPECONF_DOUBLE_WIDE;
  1707. /* Don't disable pipe or pipe PLLs if needed */
  1708. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1709. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1710. val &= ~PIPECONF_ENABLE;
  1711. I915_WRITE(reg, val);
  1712. if ((val & PIPECONF_ENABLE) == 0)
  1713. intel_wait_for_pipe_off(crtc);
  1714. }
  1715. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1716. {
  1717. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1718. }
  1719. static unsigned int
  1720. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1721. {
  1722. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1723. unsigned int cpp = fb->format->cpp[plane];
  1724. switch (fb->modifier) {
  1725. case DRM_FORMAT_MOD_NONE:
  1726. return cpp;
  1727. case I915_FORMAT_MOD_X_TILED:
  1728. if (IS_GEN2(dev_priv))
  1729. return 128;
  1730. else
  1731. return 512;
  1732. case I915_FORMAT_MOD_Y_TILED:
  1733. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1734. return 128;
  1735. else
  1736. return 512;
  1737. case I915_FORMAT_MOD_Yf_TILED:
  1738. switch (cpp) {
  1739. case 1:
  1740. return 64;
  1741. case 2:
  1742. case 4:
  1743. return 128;
  1744. case 8:
  1745. case 16:
  1746. return 256;
  1747. default:
  1748. MISSING_CASE(cpp);
  1749. return cpp;
  1750. }
  1751. break;
  1752. default:
  1753. MISSING_CASE(fb->modifier);
  1754. return cpp;
  1755. }
  1756. }
  1757. static unsigned int
  1758. intel_tile_height(const struct drm_framebuffer *fb, int plane)
  1759. {
  1760. if (fb->modifier == DRM_FORMAT_MOD_NONE)
  1761. return 1;
  1762. else
  1763. return intel_tile_size(to_i915(fb->dev)) /
  1764. intel_tile_width_bytes(fb, plane);
  1765. }
  1766. /* Return the tile dimensions in pixel units */
  1767. static void intel_tile_dims(const struct drm_framebuffer *fb, int plane,
  1768. unsigned int *tile_width,
  1769. unsigned int *tile_height)
  1770. {
  1771. unsigned int tile_width_bytes = intel_tile_width_bytes(fb, plane);
  1772. unsigned int cpp = fb->format->cpp[plane];
  1773. *tile_width = tile_width_bytes / cpp;
  1774. *tile_height = intel_tile_size(to_i915(fb->dev)) / tile_width_bytes;
  1775. }
  1776. unsigned int
  1777. intel_fb_align_height(const struct drm_framebuffer *fb,
  1778. int plane, unsigned int height)
  1779. {
  1780. unsigned int tile_height = intel_tile_height(fb, plane);
  1781. return ALIGN(height, tile_height);
  1782. }
  1783. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1784. {
  1785. unsigned int size = 0;
  1786. int i;
  1787. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1788. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1789. return size;
  1790. }
  1791. static void
  1792. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1793. const struct drm_framebuffer *fb,
  1794. unsigned int rotation)
  1795. {
  1796. view->type = I915_GGTT_VIEW_NORMAL;
  1797. if (drm_rotation_90_or_270(rotation)) {
  1798. view->type = I915_GGTT_VIEW_ROTATED;
  1799. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1800. }
  1801. }
  1802. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1803. {
  1804. if (INTEL_INFO(dev_priv)->gen >= 9)
  1805. return 256 * 1024;
  1806. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1807. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1808. return 128 * 1024;
  1809. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1810. return 4 * 1024;
  1811. else
  1812. return 0;
  1813. }
  1814. static unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
  1815. int plane)
  1816. {
  1817. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1818. /* AUX_DIST needs only 4K alignment */
  1819. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  1820. return 4096;
  1821. switch (fb->modifier) {
  1822. case DRM_FORMAT_MOD_NONE:
  1823. return intel_linear_alignment(dev_priv);
  1824. case I915_FORMAT_MOD_X_TILED:
  1825. if (INTEL_GEN(dev_priv) >= 9)
  1826. return 256 * 1024;
  1827. return 0;
  1828. case I915_FORMAT_MOD_Y_TILED:
  1829. case I915_FORMAT_MOD_Yf_TILED:
  1830. return 1 * 1024 * 1024;
  1831. default:
  1832. MISSING_CASE(fb->modifier);
  1833. return 0;
  1834. }
  1835. }
  1836. struct i915_vma *
  1837. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1838. {
  1839. struct drm_device *dev = fb->dev;
  1840. struct drm_i915_private *dev_priv = to_i915(dev);
  1841. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1842. struct i915_ggtt_view view;
  1843. struct i915_vma *vma;
  1844. u32 alignment;
  1845. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1846. alignment = intel_surf_alignment(fb, 0);
  1847. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1848. /* Note that the w/a also requires 64 PTE of padding following the
  1849. * bo. We currently fill all unused PTE with the shadow page and so
  1850. * we should always have valid PTE following the scanout preventing
  1851. * the VT-d warning.
  1852. */
  1853. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1854. alignment = 256 * 1024;
  1855. /*
  1856. * Global gtt pte registers are special registers which actually forward
  1857. * writes to a chunk of system memory. Which means that there is no risk
  1858. * that the register values disappear as soon as we call
  1859. * intel_runtime_pm_put(), so it is correct to wrap only the
  1860. * pin/unpin/fence and not more.
  1861. */
  1862. intel_runtime_pm_get(dev_priv);
  1863. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1864. if (IS_ERR(vma))
  1865. goto err;
  1866. if (i915_vma_is_map_and_fenceable(vma)) {
  1867. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1868. * fence, whereas 965+ only requires a fence if using
  1869. * framebuffer compression. For simplicity, we always, when
  1870. * possible, install a fence as the cost is not that onerous.
  1871. *
  1872. * If we fail to fence the tiled scanout, then either the
  1873. * modeset will reject the change (which is highly unlikely as
  1874. * the affected systems, all but one, do not have unmappable
  1875. * space) or we will not be able to enable full powersaving
  1876. * techniques (also likely not to apply due to various limits
  1877. * FBC and the like impose on the size of the buffer, which
  1878. * presumably we violated anyway with this unmappable buffer).
  1879. * Anyway, it is presumably better to stumble onwards with
  1880. * something and try to run the system in a "less than optimal"
  1881. * mode that matches the user configuration.
  1882. */
  1883. if (i915_vma_get_fence(vma) == 0)
  1884. i915_vma_pin_fence(vma);
  1885. }
  1886. i915_vma_get(vma);
  1887. err:
  1888. intel_runtime_pm_put(dev_priv);
  1889. return vma;
  1890. }
  1891. void intel_unpin_fb_vma(struct i915_vma *vma)
  1892. {
  1893. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1894. i915_vma_unpin_fence(vma);
  1895. i915_gem_object_unpin_from_display_plane(vma);
  1896. i915_vma_put(vma);
  1897. }
  1898. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1899. unsigned int rotation)
  1900. {
  1901. if (drm_rotation_90_or_270(rotation))
  1902. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1903. else
  1904. return fb->pitches[plane];
  1905. }
  1906. /*
  1907. * Convert the x/y offsets into a linear offset.
  1908. * Only valid with 0/180 degree rotation, which is fine since linear
  1909. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1910. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1911. */
  1912. u32 intel_fb_xy_to_linear(int x, int y,
  1913. const struct intel_plane_state *state,
  1914. int plane)
  1915. {
  1916. const struct drm_framebuffer *fb = state->base.fb;
  1917. unsigned int cpp = fb->format->cpp[plane];
  1918. unsigned int pitch = fb->pitches[plane];
  1919. return y * pitch + x * cpp;
  1920. }
  1921. /*
  1922. * Add the x/y offsets derived from fb->offsets[] to the user
  1923. * specified plane src x/y offsets. The resulting x/y offsets
  1924. * specify the start of scanout from the beginning of the gtt mapping.
  1925. */
  1926. void intel_add_fb_offsets(int *x, int *y,
  1927. const struct intel_plane_state *state,
  1928. int plane)
  1929. {
  1930. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1931. unsigned int rotation = state->base.rotation;
  1932. if (drm_rotation_90_or_270(rotation)) {
  1933. *x += intel_fb->rotated[plane].x;
  1934. *y += intel_fb->rotated[plane].y;
  1935. } else {
  1936. *x += intel_fb->normal[plane].x;
  1937. *y += intel_fb->normal[plane].y;
  1938. }
  1939. }
  1940. /*
  1941. * Input tile dimensions and pitch must already be
  1942. * rotated to match x and y, and in pixel units.
  1943. */
  1944. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1945. unsigned int tile_width,
  1946. unsigned int tile_height,
  1947. unsigned int tile_size,
  1948. unsigned int pitch_tiles,
  1949. u32 old_offset,
  1950. u32 new_offset)
  1951. {
  1952. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1953. unsigned int tiles;
  1954. WARN_ON(old_offset & (tile_size - 1));
  1955. WARN_ON(new_offset & (tile_size - 1));
  1956. WARN_ON(new_offset > old_offset);
  1957. tiles = (old_offset - new_offset) / tile_size;
  1958. *y += tiles / pitch_tiles * tile_height;
  1959. *x += tiles % pitch_tiles * tile_width;
  1960. /* minimize x in case it got needlessly big */
  1961. *y += *x / pitch_pixels * tile_height;
  1962. *x %= pitch_pixels;
  1963. return new_offset;
  1964. }
  1965. /*
  1966. * Adjust the tile offset by moving the difference into
  1967. * the x/y offsets.
  1968. */
  1969. static u32 intel_adjust_tile_offset(int *x, int *y,
  1970. const struct intel_plane_state *state, int plane,
  1971. u32 old_offset, u32 new_offset)
  1972. {
  1973. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  1974. const struct drm_framebuffer *fb = state->base.fb;
  1975. unsigned int cpp = fb->format->cpp[plane];
  1976. unsigned int rotation = state->base.rotation;
  1977. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1978. WARN_ON(new_offset > old_offset);
  1979. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  1980. unsigned int tile_size, tile_width, tile_height;
  1981. unsigned int pitch_tiles;
  1982. tile_size = intel_tile_size(dev_priv);
  1983. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  1984. if (drm_rotation_90_or_270(rotation)) {
  1985. pitch_tiles = pitch / tile_height;
  1986. swap(tile_width, tile_height);
  1987. } else {
  1988. pitch_tiles = pitch / (tile_width * cpp);
  1989. }
  1990. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1991. tile_size, pitch_tiles,
  1992. old_offset, new_offset);
  1993. } else {
  1994. old_offset += *y * pitch + *x * cpp;
  1995. *y = (old_offset - new_offset) / pitch;
  1996. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1997. }
  1998. return new_offset;
  1999. }
  2000. /*
  2001. * Computes the linear offset to the base tile and adjusts
  2002. * x, y. bytes per pixel is assumed to be a power-of-two.
  2003. *
  2004. * In the 90/270 rotated case, x and y are assumed
  2005. * to be already rotated to match the rotated GTT view, and
  2006. * pitch is the tile_height aligned framebuffer height.
  2007. *
  2008. * This function is used when computing the derived information
  2009. * under intel_framebuffer, so using any of that information
  2010. * here is not allowed. Anything under drm_framebuffer can be
  2011. * used. This is why the user has to pass in the pitch since it
  2012. * is specified in the rotated orientation.
  2013. */
  2014. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2015. int *x, int *y,
  2016. const struct drm_framebuffer *fb, int plane,
  2017. unsigned int pitch,
  2018. unsigned int rotation,
  2019. u32 alignment)
  2020. {
  2021. uint64_t fb_modifier = fb->modifier;
  2022. unsigned int cpp = fb->format->cpp[plane];
  2023. u32 offset, offset_aligned;
  2024. if (alignment)
  2025. alignment--;
  2026. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2027. unsigned int tile_size, tile_width, tile_height;
  2028. unsigned int tile_rows, tiles, pitch_tiles;
  2029. tile_size = intel_tile_size(dev_priv);
  2030. intel_tile_dims(fb, plane, &tile_width, &tile_height);
  2031. if (drm_rotation_90_or_270(rotation)) {
  2032. pitch_tiles = pitch / tile_height;
  2033. swap(tile_width, tile_height);
  2034. } else {
  2035. pitch_tiles = pitch / (tile_width * cpp);
  2036. }
  2037. tile_rows = *y / tile_height;
  2038. *y %= tile_height;
  2039. tiles = *x / tile_width;
  2040. *x %= tile_width;
  2041. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2042. offset_aligned = offset & ~alignment;
  2043. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2044. tile_size, pitch_tiles,
  2045. offset, offset_aligned);
  2046. } else {
  2047. offset = *y * pitch + *x * cpp;
  2048. offset_aligned = offset & ~alignment;
  2049. *y = (offset & alignment) / pitch;
  2050. *x = ((offset & alignment) - *y * pitch) / cpp;
  2051. }
  2052. return offset_aligned;
  2053. }
  2054. u32 intel_compute_tile_offset(int *x, int *y,
  2055. const struct intel_plane_state *state,
  2056. int plane)
  2057. {
  2058. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2059. const struct drm_framebuffer *fb = state->base.fb;
  2060. unsigned int rotation = state->base.rotation;
  2061. int pitch = intel_fb_pitch(fb, plane, rotation);
  2062. u32 alignment = intel_surf_alignment(fb, plane);
  2063. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2064. rotation, alignment);
  2065. }
  2066. /* Convert the fb->offset[] linear offset into x/y offsets */
  2067. static void intel_fb_offset_to_xy(int *x, int *y,
  2068. const struct drm_framebuffer *fb, int plane)
  2069. {
  2070. unsigned int cpp = fb->format->cpp[plane];
  2071. unsigned int pitch = fb->pitches[plane];
  2072. u32 linear_offset = fb->offsets[plane];
  2073. *y = linear_offset / pitch;
  2074. *x = linear_offset % pitch / cpp;
  2075. }
  2076. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2077. {
  2078. switch (fb_modifier) {
  2079. case I915_FORMAT_MOD_X_TILED:
  2080. return I915_TILING_X;
  2081. case I915_FORMAT_MOD_Y_TILED:
  2082. return I915_TILING_Y;
  2083. default:
  2084. return I915_TILING_NONE;
  2085. }
  2086. }
  2087. static int
  2088. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2089. struct drm_framebuffer *fb)
  2090. {
  2091. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2092. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2093. u32 gtt_offset_rotated = 0;
  2094. unsigned int max_size = 0;
  2095. int i, num_planes = fb->format->num_planes;
  2096. unsigned int tile_size = intel_tile_size(dev_priv);
  2097. for (i = 0; i < num_planes; i++) {
  2098. unsigned int width, height;
  2099. unsigned int cpp, size;
  2100. u32 offset;
  2101. int x, y;
  2102. cpp = fb->format->cpp[i];
  2103. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2104. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2105. intel_fb_offset_to_xy(&x, &y, fb, i);
  2106. /*
  2107. * The fence (if used) is aligned to the start of the object
  2108. * so having the framebuffer wrap around across the edge of the
  2109. * fenced region doesn't really work. We have no API to configure
  2110. * the fence start offset within the object (nor could we probably
  2111. * on gen2/3). So it's just easier if we just require that the
  2112. * fb layout agrees with the fence layout. We already check that the
  2113. * fb stride matches the fence stride elsewhere.
  2114. */
  2115. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2116. (x + width) * cpp > fb->pitches[i]) {
  2117. DRM_DEBUG_KMS("bad fb plane %d offset: 0x%x\n",
  2118. i, fb->offsets[i]);
  2119. return -EINVAL;
  2120. }
  2121. /*
  2122. * First pixel of the framebuffer from
  2123. * the start of the normal gtt mapping.
  2124. */
  2125. intel_fb->normal[i].x = x;
  2126. intel_fb->normal[i].y = y;
  2127. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2128. fb, i, fb->pitches[i],
  2129. DRM_ROTATE_0, tile_size);
  2130. offset /= tile_size;
  2131. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2132. unsigned int tile_width, tile_height;
  2133. unsigned int pitch_tiles;
  2134. struct drm_rect r;
  2135. intel_tile_dims(fb, i, &tile_width, &tile_height);
  2136. rot_info->plane[i].offset = offset;
  2137. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2138. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2139. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2140. intel_fb->rotated[i].pitch =
  2141. rot_info->plane[i].height * tile_height;
  2142. /* how many tiles does this plane need */
  2143. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2144. /*
  2145. * If the plane isn't horizontally tile aligned,
  2146. * we need one more tile.
  2147. */
  2148. if (x != 0)
  2149. size++;
  2150. /* rotate the x/y offsets to match the GTT view */
  2151. r.x1 = x;
  2152. r.y1 = y;
  2153. r.x2 = x + width;
  2154. r.y2 = y + height;
  2155. drm_rect_rotate(&r,
  2156. rot_info->plane[i].width * tile_width,
  2157. rot_info->plane[i].height * tile_height,
  2158. DRM_ROTATE_270);
  2159. x = r.x1;
  2160. y = r.y1;
  2161. /* rotate the tile dimensions to match the GTT view */
  2162. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2163. swap(tile_width, tile_height);
  2164. /*
  2165. * We only keep the x/y offsets, so push all of the
  2166. * gtt offset into the x/y offsets.
  2167. */
  2168. _intel_adjust_tile_offset(&x, &y,
  2169. tile_width, tile_height,
  2170. tile_size, pitch_tiles,
  2171. gtt_offset_rotated * tile_size, 0);
  2172. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2173. /*
  2174. * First pixel of the framebuffer from
  2175. * the start of the rotated gtt mapping.
  2176. */
  2177. intel_fb->rotated[i].x = x;
  2178. intel_fb->rotated[i].y = y;
  2179. } else {
  2180. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2181. x * cpp, tile_size);
  2182. }
  2183. /* how many tiles in total needed in the bo */
  2184. max_size = max(max_size, offset + size);
  2185. }
  2186. if (max_size * tile_size > intel_fb->obj->base.size) {
  2187. DRM_DEBUG_KMS("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2188. max_size * tile_size, intel_fb->obj->base.size);
  2189. return -EINVAL;
  2190. }
  2191. return 0;
  2192. }
  2193. static int i9xx_format_to_fourcc(int format)
  2194. {
  2195. switch (format) {
  2196. case DISPPLANE_8BPP:
  2197. return DRM_FORMAT_C8;
  2198. case DISPPLANE_BGRX555:
  2199. return DRM_FORMAT_XRGB1555;
  2200. case DISPPLANE_BGRX565:
  2201. return DRM_FORMAT_RGB565;
  2202. default:
  2203. case DISPPLANE_BGRX888:
  2204. return DRM_FORMAT_XRGB8888;
  2205. case DISPPLANE_RGBX888:
  2206. return DRM_FORMAT_XBGR8888;
  2207. case DISPPLANE_BGRX101010:
  2208. return DRM_FORMAT_XRGB2101010;
  2209. case DISPPLANE_RGBX101010:
  2210. return DRM_FORMAT_XBGR2101010;
  2211. }
  2212. }
  2213. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2214. {
  2215. switch (format) {
  2216. case PLANE_CTL_FORMAT_RGB_565:
  2217. return DRM_FORMAT_RGB565;
  2218. default:
  2219. case PLANE_CTL_FORMAT_XRGB_8888:
  2220. if (rgb_order) {
  2221. if (alpha)
  2222. return DRM_FORMAT_ABGR8888;
  2223. else
  2224. return DRM_FORMAT_XBGR8888;
  2225. } else {
  2226. if (alpha)
  2227. return DRM_FORMAT_ARGB8888;
  2228. else
  2229. return DRM_FORMAT_XRGB8888;
  2230. }
  2231. case PLANE_CTL_FORMAT_XRGB_2101010:
  2232. if (rgb_order)
  2233. return DRM_FORMAT_XBGR2101010;
  2234. else
  2235. return DRM_FORMAT_XRGB2101010;
  2236. }
  2237. }
  2238. static bool
  2239. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2240. struct intel_initial_plane_config *plane_config)
  2241. {
  2242. struct drm_device *dev = crtc->base.dev;
  2243. struct drm_i915_private *dev_priv = to_i915(dev);
  2244. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2245. struct drm_i915_gem_object *obj = NULL;
  2246. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2247. struct drm_framebuffer *fb = &plane_config->fb->base;
  2248. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2249. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2250. PAGE_SIZE);
  2251. size_aligned -= base_aligned;
  2252. if (plane_config->size == 0)
  2253. return false;
  2254. /* If the FB is too big, just don't use it since fbdev is not very
  2255. * important and we should probably use that space with FBC or other
  2256. * features. */
  2257. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2258. return false;
  2259. mutex_lock(&dev->struct_mutex);
  2260. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2261. base_aligned,
  2262. base_aligned,
  2263. size_aligned);
  2264. mutex_unlock(&dev->struct_mutex);
  2265. if (!obj)
  2266. return false;
  2267. if (plane_config->tiling == I915_TILING_X)
  2268. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2269. mode_cmd.pixel_format = fb->format->format;
  2270. mode_cmd.width = fb->width;
  2271. mode_cmd.height = fb->height;
  2272. mode_cmd.pitches[0] = fb->pitches[0];
  2273. mode_cmd.modifier[0] = fb->modifier;
  2274. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2275. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2276. DRM_DEBUG_KMS("intel fb init failed\n");
  2277. goto out_unref_obj;
  2278. }
  2279. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2280. return true;
  2281. out_unref_obj:
  2282. i915_gem_object_put(obj);
  2283. return false;
  2284. }
  2285. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2286. static void
  2287. update_state_fb(struct drm_plane *plane)
  2288. {
  2289. if (plane->fb == plane->state->fb)
  2290. return;
  2291. if (plane->state->fb)
  2292. drm_framebuffer_unreference(plane->state->fb);
  2293. plane->state->fb = plane->fb;
  2294. if (plane->state->fb)
  2295. drm_framebuffer_reference(plane->state->fb);
  2296. }
  2297. static void
  2298. intel_set_plane_visible(struct intel_crtc_state *crtc_state,
  2299. struct intel_plane_state *plane_state,
  2300. bool visible)
  2301. {
  2302. struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
  2303. plane_state->base.visible = visible;
  2304. /* FIXME pre-g4x don't work like this */
  2305. if (visible) {
  2306. crtc_state->base.plane_mask |= BIT(drm_plane_index(&plane->base));
  2307. crtc_state->active_planes |= BIT(plane->id);
  2308. } else {
  2309. crtc_state->base.plane_mask &= ~BIT(drm_plane_index(&plane->base));
  2310. crtc_state->active_planes &= ~BIT(plane->id);
  2311. }
  2312. DRM_DEBUG_KMS("%s active planes 0x%x\n",
  2313. crtc_state->base.crtc->name,
  2314. crtc_state->active_planes);
  2315. }
  2316. static void
  2317. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2318. struct intel_initial_plane_config *plane_config)
  2319. {
  2320. struct drm_device *dev = intel_crtc->base.dev;
  2321. struct drm_i915_private *dev_priv = to_i915(dev);
  2322. struct drm_crtc *c;
  2323. struct drm_i915_gem_object *obj;
  2324. struct drm_plane *primary = intel_crtc->base.primary;
  2325. struct drm_plane_state *plane_state = primary->state;
  2326. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2327. struct intel_plane *intel_plane = to_intel_plane(primary);
  2328. struct intel_plane_state *intel_state =
  2329. to_intel_plane_state(plane_state);
  2330. struct drm_framebuffer *fb;
  2331. if (!plane_config->fb)
  2332. return;
  2333. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2334. fb = &plane_config->fb->base;
  2335. goto valid_fb;
  2336. }
  2337. kfree(plane_config->fb);
  2338. /*
  2339. * Failed to alloc the obj, check to see if we should share
  2340. * an fb with another CRTC instead
  2341. */
  2342. for_each_crtc(dev, c) {
  2343. struct intel_plane_state *state;
  2344. if (c == &intel_crtc->base)
  2345. continue;
  2346. if (!to_intel_crtc(c)->active)
  2347. continue;
  2348. state = to_intel_plane_state(c->primary->state);
  2349. if (!state->vma)
  2350. continue;
  2351. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2352. fb = c->primary->fb;
  2353. drm_framebuffer_reference(fb);
  2354. goto valid_fb;
  2355. }
  2356. }
  2357. /*
  2358. * We've failed to reconstruct the BIOS FB. Current display state
  2359. * indicates that the primary plane is visible, but has a NULL FB,
  2360. * which will lead to problems later if we don't fix it up. The
  2361. * simplest solution is to just disable the primary plane now and
  2362. * pretend the BIOS never had it enabled.
  2363. */
  2364. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2365. to_intel_plane_state(plane_state),
  2366. false);
  2367. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2368. trace_intel_disable_plane(primary, intel_crtc);
  2369. intel_plane->disable_plane(primary, &intel_crtc->base);
  2370. return;
  2371. valid_fb:
  2372. mutex_lock(&dev->struct_mutex);
  2373. intel_state->vma =
  2374. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2375. mutex_unlock(&dev->struct_mutex);
  2376. if (IS_ERR(intel_state->vma)) {
  2377. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2378. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2379. intel_state->vma = NULL;
  2380. drm_framebuffer_unreference(fb);
  2381. return;
  2382. }
  2383. plane_state->src_x = 0;
  2384. plane_state->src_y = 0;
  2385. plane_state->src_w = fb->width << 16;
  2386. plane_state->src_h = fb->height << 16;
  2387. plane_state->crtc_x = 0;
  2388. plane_state->crtc_y = 0;
  2389. plane_state->crtc_w = fb->width;
  2390. plane_state->crtc_h = fb->height;
  2391. intel_state->base.src = drm_plane_state_src(plane_state);
  2392. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2393. obj = intel_fb_obj(fb);
  2394. if (i915_gem_object_is_tiled(obj))
  2395. dev_priv->preserve_bios_swizzle = true;
  2396. drm_framebuffer_reference(fb);
  2397. primary->fb = primary->state->fb = fb;
  2398. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2399. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2400. to_intel_plane_state(plane_state),
  2401. true);
  2402. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2403. &obj->frontbuffer_bits);
  2404. }
  2405. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2406. unsigned int rotation)
  2407. {
  2408. int cpp = fb->format->cpp[plane];
  2409. switch (fb->modifier) {
  2410. case DRM_FORMAT_MOD_NONE:
  2411. case I915_FORMAT_MOD_X_TILED:
  2412. switch (cpp) {
  2413. case 8:
  2414. return 4096;
  2415. case 4:
  2416. case 2:
  2417. case 1:
  2418. return 8192;
  2419. default:
  2420. MISSING_CASE(cpp);
  2421. break;
  2422. }
  2423. break;
  2424. case I915_FORMAT_MOD_Y_TILED:
  2425. case I915_FORMAT_MOD_Yf_TILED:
  2426. switch (cpp) {
  2427. case 8:
  2428. return 2048;
  2429. case 4:
  2430. return 4096;
  2431. case 2:
  2432. case 1:
  2433. return 8192;
  2434. default:
  2435. MISSING_CASE(cpp);
  2436. break;
  2437. }
  2438. break;
  2439. default:
  2440. MISSING_CASE(fb->modifier);
  2441. }
  2442. return 2048;
  2443. }
  2444. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2445. {
  2446. const struct drm_framebuffer *fb = plane_state->base.fb;
  2447. unsigned int rotation = plane_state->base.rotation;
  2448. int x = plane_state->base.src.x1 >> 16;
  2449. int y = plane_state->base.src.y1 >> 16;
  2450. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2451. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2452. int max_width = skl_max_plane_width(fb, 0, rotation);
  2453. int max_height = 4096;
  2454. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2455. if (w > max_width || h > max_height) {
  2456. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2457. w, h, max_width, max_height);
  2458. return -EINVAL;
  2459. }
  2460. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2461. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2462. alignment = intel_surf_alignment(fb, 0);
  2463. /*
  2464. * AUX surface offset is specified as the distance from the
  2465. * main surface offset, and it must be non-negative. Make
  2466. * sure that is what we will get.
  2467. */
  2468. if (offset > aux_offset)
  2469. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2470. offset, aux_offset & ~(alignment - 1));
  2471. /*
  2472. * When using an X-tiled surface, the plane blows up
  2473. * if the x offset + width exceed the stride.
  2474. *
  2475. * TODO: linear and Y-tiled seem fine, Yf untested,
  2476. */
  2477. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2478. int cpp = fb->format->cpp[0];
  2479. while ((x + w) * cpp > fb->pitches[0]) {
  2480. if (offset == 0) {
  2481. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2482. return -EINVAL;
  2483. }
  2484. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2485. offset, offset - alignment);
  2486. }
  2487. }
  2488. plane_state->main.offset = offset;
  2489. plane_state->main.x = x;
  2490. plane_state->main.y = y;
  2491. return 0;
  2492. }
  2493. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2494. {
  2495. const struct drm_framebuffer *fb = plane_state->base.fb;
  2496. unsigned int rotation = plane_state->base.rotation;
  2497. int max_width = skl_max_plane_width(fb, 1, rotation);
  2498. int max_height = 4096;
  2499. int x = plane_state->base.src.x1 >> 17;
  2500. int y = plane_state->base.src.y1 >> 17;
  2501. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2502. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2503. u32 offset;
  2504. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2505. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2506. /* FIXME not quite sure how/if these apply to the chroma plane */
  2507. if (w > max_width || h > max_height) {
  2508. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2509. w, h, max_width, max_height);
  2510. return -EINVAL;
  2511. }
  2512. plane_state->aux.offset = offset;
  2513. plane_state->aux.x = x;
  2514. plane_state->aux.y = y;
  2515. return 0;
  2516. }
  2517. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2518. {
  2519. const struct drm_framebuffer *fb = plane_state->base.fb;
  2520. unsigned int rotation = plane_state->base.rotation;
  2521. int ret;
  2522. if (!plane_state->base.visible)
  2523. return 0;
  2524. /* Rotate src coordinates to match rotated GTT view */
  2525. if (drm_rotation_90_or_270(rotation))
  2526. drm_rect_rotate(&plane_state->base.src,
  2527. fb->width << 16, fb->height << 16,
  2528. DRM_ROTATE_270);
  2529. /*
  2530. * Handle the AUX surface first since
  2531. * the main surface setup depends on it.
  2532. */
  2533. if (fb->format->format == DRM_FORMAT_NV12) {
  2534. ret = skl_check_nv12_aux_surface(plane_state);
  2535. if (ret)
  2536. return ret;
  2537. } else {
  2538. plane_state->aux.offset = ~0xfff;
  2539. plane_state->aux.x = 0;
  2540. plane_state->aux.y = 0;
  2541. }
  2542. ret = skl_check_main_surface(plane_state);
  2543. if (ret)
  2544. return ret;
  2545. return 0;
  2546. }
  2547. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2548. const struct intel_crtc_state *crtc_state,
  2549. const struct intel_plane_state *plane_state)
  2550. {
  2551. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2553. struct drm_framebuffer *fb = plane_state->base.fb;
  2554. int plane = intel_crtc->plane;
  2555. u32 linear_offset;
  2556. u32 dspcntr;
  2557. i915_reg_t reg = DSPCNTR(plane);
  2558. unsigned int rotation = plane_state->base.rotation;
  2559. int x = plane_state->base.src.x1 >> 16;
  2560. int y = plane_state->base.src.y1 >> 16;
  2561. unsigned long irqflags;
  2562. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2563. dspcntr |= DISPLAY_PLANE_ENABLE;
  2564. if (INTEL_GEN(dev_priv) < 4) {
  2565. if (intel_crtc->pipe == PIPE_B)
  2566. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2567. }
  2568. switch (fb->format->format) {
  2569. case DRM_FORMAT_C8:
  2570. dspcntr |= DISPPLANE_8BPP;
  2571. break;
  2572. case DRM_FORMAT_XRGB1555:
  2573. dspcntr |= DISPPLANE_BGRX555;
  2574. break;
  2575. case DRM_FORMAT_RGB565:
  2576. dspcntr |= DISPPLANE_BGRX565;
  2577. break;
  2578. case DRM_FORMAT_XRGB8888:
  2579. dspcntr |= DISPPLANE_BGRX888;
  2580. break;
  2581. case DRM_FORMAT_XBGR8888:
  2582. dspcntr |= DISPPLANE_RGBX888;
  2583. break;
  2584. case DRM_FORMAT_XRGB2101010:
  2585. dspcntr |= DISPPLANE_BGRX101010;
  2586. break;
  2587. case DRM_FORMAT_XBGR2101010:
  2588. dspcntr |= DISPPLANE_RGBX101010;
  2589. break;
  2590. default:
  2591. BUG();
  2592. }
  2593. if (INTEL_GEN(dev_priv) >= 4 &&
  2594. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2595. dspcntr |= DISPPLANE_TILED;
  2596. if (rotation & DRM_ROTATE_180)
  2597. dspcntr |= DISPPLANE_ROTATE_180;
  2598. if (rotation & DRM_REFLECT_X)
  2599. dspcntr |= DISPPLANE_MIRROR;
  2600. if (IS_G4X(dev_priv))
  2601. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2602. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2603. if (INTEL_GEN(dev_priv) >= 4)
  2604. intel_crtc->dspaddr_offset =
  2605. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2606. if (rotation & DRM_ROTATE_180) {
  2607. x += crtc_state->pipe_src_w - 1;
  2608. y += crtc_state->pipe_src_h - 1;
  2609. } else if (rotation & DRM_REFLECT_X) {
  2610. x += crtc_state->pipe_src_w - 1;
  2611. }
  2612. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2613. if (INTEL_GEN(dev_priv) < 4)
  2614. intel_crtc->dspaddr_offset = linear_offset;
  2615. intel_crtc->adjusted_x = x;
  2616. intel_crtc->adjusted_y = y;
  2617. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2618. if (INTEL_GEN(dev_priv) < 4) {
  2619. /* pipesrc and dspsize control the size that is scaled from,
  2620. * which should always be the user's requested size.
  2621. */
  2622. I915_WRITE_FW(DSPSIZE(plane),
  2623. ((crtc_state->pipe_src_h - 1) << 16) |
  2624. (crtc_state->pipe_src_w - 1));
  2625. I915_WRITE_FW(DSPPOS(plane), 0);
  2626. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2627. I915_WRITE_FW(PRIMSIZE(plane),
  2628. ((crtc_state->pipe_src_h - 1) << 16) |
  2629. (crtc_state->pipe_src_w - 1));
  2630. I915_WRITE_FW(PRIMPOS(plane), 0);
  2631. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2632. }
  2633. I915_WRITE_FW(reg, dspcntr);
  2634. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2635. if (INTEL_GEN(dev_priv) >= 4) {
  2636. I915_WRITE_FW(DSPSURF(plane),
  2637. intel_plane_ggtt_offset(plane_state) +
  2638. intel_crtc->dspaddr_offset);
  2639. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2640. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2641. } else {
  2642. I915_WRITE_FW(DSPADDR(plane),
  2643. intel_plane_ggtt_offset(plane_state) +
  2644. intel_crtc->dspaddr_offset);
  2645. }
  2646. POSTING_READ_FW(reg);
  2647. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2648. }
  2649. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2650. struct drm_crtc *crtc)
  2651. {
  2652. struct drm_device *dev = crtc->dev;
  2653. struct drm_i915_private *dev_priv = to_i915(dev);
  2654. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2655. int plane = intel_crtc->plane;
  2656. unsigned long irqflags;
  2657. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2658. I915_WRITE_FW(DSPCNTR(plane), 0);
  2659. if (INTEL_INFO(dev_priv)->gen >= 4)
  2660. I915_WRITE_FW(DSPSURF(plane), 0);
  2661. else
  2662. I915_WRITE_FW(DSPADDR(plane), 0);
  2663. POSTING_READ_FW(DSPCNTR(plane));
  2664. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2665. }
  2666. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2667. const struct intel_crtc_state *crtc_state,
  2668. const struct intel_plane_state *plane_state)
  2669. {
  2670. struct drm_device *dev = primary->dev;
  2671. struct drm_i915_private *dev_priv = to_i915(dev);
  2672. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2673. struct drm_framebuffer *fb = plane_state->base.fb;
  2674. int plane = intel_crtc->plane;
  2675. u32 linear_offset;
  2676. u32 dspcntr;
  2677. i915_reg_t reg = DSPCNTR(plane);
  2678. unsigned int rotation = plane_state->base.rotation;
  2679. int x = plane_state->base.src.x1 >> 16;
  2680. int y = plane_state->base.src.y1 >> 16;
  2681. unsigned long irqflags;
  2682. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2683. dspcntr |= DISPLAY_PLANE_ENABLE;
  2684. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2685. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2686. switch (fb->format->format) {
  2687. case DRM_FORMAT_C8:
  2688. dspcntr |= DISPPLANE_8BPP;
  2689. break;
  2690. case DRM_FORMAT_RGB565:
  2691. dspcntr |= DISPPLANE_BGRX565;
  2692. break;
  2693. case DRM_FORMAT_XRGB8888:
  2694. dspcntr |= DISPPLANE_BGRX888;
  2695. break;
  2696. case DRM_FORMAT_XBGR8888:
  2697. dspcntr |= DISPPLANE_RGBX888;
  2698. break;
  2699. case DRM_FORMAT_XRGB2101010:
  2700. dspcntr |= DISPPLANE_BGRX101010;
  2701. break;
  2702. case DRM_FORMAT_XBGR2101010:
  2703. dspcntr |= DISPPLANE_RGBX101010;
  2704. break;
  2705. default:
  2706. BUG();
  2707. }
  2708. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2709. dspcntr |= DISPPLANE_TILED;
  2710. if (rotation & DRM_ROTATE_180)
  2711. dspcntr |= DISPPLANE_ROTATE_180;
  2712. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2713. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2714. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2715. intel_crtc->dspaddr_offset =
  2716. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2717. /* HSW+ does this automagically in hardware */
  2718. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2719. rotation & DRM_ROTATE_180) {
  2720. x += crtc_state->pipe_src_w - 1;
  2721. y += crtc_state->pipe_src_h - 1;
  2722. }
  2723. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2724. intel_crtc->adjusted_x = x;
  2725. intel_crtc->adjusted_y = y;
  2726. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2727. I915_WRITE_FW(reg, dspcntr);
  2728. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2729. I915_WRITE_FW(DSPSURF(plane),
  2730. intel_plane_ggtt_offset(plane_state) +
  2731. intel_crtc->dspaddr_offset);
  2732. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2733. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2734. } else {
  2735. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2736. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2737. }
  2738. POSTING_READ_FW(reg);
  2739. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2740. }
  2741. static u32
  2742. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2743. {
  2744. if (fb->modifier == DRM_FORMAT_MOD_NONE)
  2745. return 64;
  2746. else
  2747. return intel_tile_width_bytes(fb, plane);
  2748. }
  2749. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2750. {
  2751. struct drm_device *dev = intel_crtc->base.dev;
  2752. struct drm_i915_private *dev_priv = to_i915(dev);
  2753. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2754. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2755. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2756. }
  2757. /*
  2758. * This function detaches (aka. unbinds) unused scalers in hardware
  2759. */
  2760. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2761. {
  2762. struct intel_crtc_scaler_state *scaler_state;
  2763. int i;
  2764. scaler_state = &intel_crtc->config->scaler_state;
  2765. /* loop through and disable scalers that aren't in use */
  2766. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2767. if (!scaler_state->scalers[i].in_use)
  2768. skl_detach_scaler(intel_crtc, i);
  2769. }
  2770. }
  2771. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2772. unsigned int rotation)
  2773. {
  2774. u32 stride;
  2775. if (plane >= fb->format->num_planes)
  2776. return 0;
  2777. stride = intel_fb_pitch(fb, plane, rotation);
  2778. /*
  2779. * The stride is either expressed as a multiple of 64 bytes chunks for
  2780. * linear buffers or in number of tiles for tiled buffers.
  2781. */
  2782. if (drm_rotation_90_or_270(rotation))
  2783. stride /= intel_tile_height(fb, plane);
  2784. else
  2785. stride /= intel_fb_stride_alignment(fb, plane);
  2786. return stride;
  2787. }
  2788. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2789. {
  2790. switch (pixel_format) {
  2791. case DRM_FORMAT_C8:
  2792. return PLANE_CTL_FORMAT_INDEXED;
  2793. case DRM_FORMAT_RGB565:
  2794. return PLANE_CTL_FORMAT_RGB_565;
  2795. case DRM_FORMAT_XBGR8888:
  2796. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2797. case DRM_FORMAT_XRGB8888:
  2798. return PLANE_CTL_FORMAT_XRGB_8888;
  2799. /*
  2800. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2801. * to be already pre-multiplied. We need to add a knob (or a different
  2802. * DRM_FORMAT) for user-space to configure that.
  2803. */
  2804. case DRM_FORMAT_ABGR8888:
  2805. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2806. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2807. case DRM_FORMAT_ARGB8888:
  2808. return PLANE_CTL_FORMAT_XRGB_8888 |
  2809. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2810. case DRM_FORMAT_XRGB2101010:
  2811. return PLANE_CTL_FORMAT_XRGB_2101010;
  2812. case DRM_FORMAT_XBGR2101010:
  2813. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2814. case DRM_FORMAT_YUYV:
  2815. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2816. case DRM_FORMAT_YVYU:
  2817. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2818. case DRM_FORMAT_UYVY:
  2819. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2820. case DRM_FORMAT_VYUY:
  2821. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2822. default:
  2823. MISSING_CASE(pixel_format);
  2824. }
  2825. return 0;
  2826. }
  2827. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2828. {
  2829. switch (fb_modifier) {
  2830. case DRM_FORMAT_MOD_NONE:
  2831. break;
  2832. case I915_FORMAT_MOD_X_TILED:
  2833. return PLANE_CTL_TILED_X;
  2834. case I915_FORMAT_MOD_Y_TILED:
  2835. return PLANE_CTL_TILED_Y;
  2836. case I915_FORMAT_MOD_Yf_TILED:
  2837. return PLANE_CTL_TILED_YF;
  2838. default:
  2839. MISSING_CASE(fb_modifier);
  2840. }
  2841. return 0;
  2842. }
  2843. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2844. {
  2845. switch (rotation) {
  2846. case DRM_ROTATE_0:
  2847. break;
  2848. /*
  2849. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2850. * while i915 HW rotation is clockwise, thats why this swapping.
  2851. */
  2852. case DRM_ROTATE_90:
  2853. return PLANE_CTL_ROTATE_270;
  2854. case DRM_ROTATE_180:
  2855. return PLANE_CTL_ROTATE_180;
  2856. case DRM_ROTATE_270:
  2857. return PLANE_CTL_ROTATE_90;
  2858. default:
  2859. MISSING_CASE(rotation);
  2860. }
  2861. return 0;
  2862. }
  2863. static void skylake_update_primary_plane(struct drm_plane *plane,
  2864. const struct intel_crtc_state *crtc_state,
  2865. const struct intel_plane_state *plane_state)
  2866. {
  2867. struct drm_device *dev = plane->dev;
  2868. struct drm_i915_private *dev_priv = to_i915(dev);
  2869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2870. struct drm_framebuffer *fb = plane_state->base.fb;
  2871. enum plane_id plane_id = to_intel_plane(plane)->id;
  2872. enum pipe pipe = to_intel_plane(plane)->pipe;
  2873. u32 plane_ctl;
  2874. unsigned int rotation = plane_state->base.rotation;
  2875. u32 stride = skl_plane_stride(fb, 0, rotation);
  2876. u32 surf_addr = plane_state->main.offset;
  2877. int scaler_id = plane_state->scaler_id;
  2878. int src_x = plane_state->main.x;
  2879. int src_y = plane_state->main.y;
  2880. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2881. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2882. int dst_x = plane_state->base.dst.x1;
  2883. int dst_y = plane_state->base.dst.y1;
  2884. int dst_w = drm_rect_width(&plane_state->base.dst);
  2885. int dst_h = drm_rect_height(&plane_state->base.dst);
  2886. unsigned long irqflags;
  2887. plane_ctl = PLANE_CTL_ENABLE;
  2888. if (!IS_GEMINILAKE(dev_priv)) {
  2889. plane_ctl |=
  2890. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2891. PLANE_CTL_PIPE_CSC_ENABLE |
  2892. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2893. }
  2894. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2895. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2896. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2897. /* Sizes are 0 based */
  2898. src_w--;
  2899. src_h--;
  2900. dst_w--;
  2901. dst_h--;
  2902. intel_crtc->dspaddr_offset = surf_addr;
  2903. intel_crtc->adjusted_x = src_x;
  2904. intel_crtc->adjusted_y = src_y;
  2905. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2906. if (IS_GEMINILAKE(dev_priv)) {
  2907. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  2908. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2909. PLANE_COLOR_PIPE_CSC_ENABLE |
  2910. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2911. }
  2912. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  2913. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2914. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  2915. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2916. if (scaler_id >= 0) {
  2917. uint32_t ps_ctrl = 0;
  2918. WARN_ON(!dst_w || !dst_h);
  2919. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2920. crtc_state->scaler_state.scalers[scaler_id].mode;
  2921. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2922. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2923. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2924. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2925. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  2926. } else {
  2927. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2928. }
  2929. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  2930. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2931. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2932. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2933. }
  2934. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2935. struct drm_crtc *crtc)
  2936. {
  2937. struct drm_device *dev = crtc->dev;
  2938. struct drm_i915_private *dev_priv = to_i915(dev);
  2939. enum plane_id plane_id = to_intel_plane(primary)->id;
  2940. enum pipe pipe = to_intel_plane(primary)->pipe;
  2941. unsigned long irqflags;
  2942. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2943. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  2944. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  2945. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2946. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2947. }
  2948. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2949. static int
  2950. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2951. int x, int y, enum mode_set_atomic state)
  2952. {
  2953. /* Support for kgdboc is disabled, this needs a major rework. */
  2954. DRM_ERROR("legacy panic handler not supported any more.\n");
  2955. return -ENODEV;
  2956. }
  2957. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2958. {
  2959. struct intel_crtc *crtc;
  2960. for_each_intel_crtc(&dev_priv->drm, crtc)
  2961. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2962. }
  2963. static void intel_update_primary_planes(struct drm_device *dev)
  2964. {
  2965. struct drm_crtc *crtc;
  2966. for_each_crtc(dev, crtc) {
  2967. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2968. struct intel_plane_state *plane_state =
  2969. to_intel_plane_state(plane->base.state);
  2970. if (plane_state->base.visible) {
  2971. trace_intel_update_plane(&plane->base,
  2972. to_intel_crtc(crtc));
  2973. plane->update_plane(&plane->base,
  2974. to_intel_crtc_state(crtc->state),
  2975. plane_state);
  2976. }
  2977. }
  2978. }
  2979. static int
  2980. __intel_display_resume(struct drm_device *dev,
  2981. struct drm_atomic_state *state,
  2982. struct drm_modeset_acquire_ctx *ctx)
  2983. {
  2984. struct drm_crtc_state *crtc_state;
  2985. struct drm_crtc *crtc;
  2986. int i, ret;
  2987. intel_modeset_setup_hw_state(dev);
  2988. i915_redisable_vga(to_i915(dev));
  2989. if (!state)
  2990. return 0;
  2991. /*
  2992. * We've duplicated the state, pointers to the old state are invalid.
  2993. *
  2994. * Don't attempt to use the old state until we commit the duplicated state.
  2995. */
  2996. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  2997. /*
  2998. * Force recalculation even if we restore
  2999. * current state. With fast modeset this may not result
  3000. * in a modeset when the state is compatible.
  3001. */
  3002. crtc_state->mode_changed = true;
  3003. }
  3004. /* ignore any reset values/BIOS leftovers in the WM registers */
  3005. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  3006. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3007. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  3008. WARN_ON(ret == -EDEADLK);
  3009. return ret;
  3010. }
  3011. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3012. {
  3013. return intel_has_gpu_reset(dev_priv) &&
  3014. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3015. }
  3016. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3017. {
  3018. struct drm_device *dev = &dev_priv->drm;
  3019. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3020. struct drm_atomic_state *state;
  3021. int ret;
  3022. /*
  3023. * Need mode_config.mutex so that we don't
  3024. * trample ongoing ->detect() and whatnot.
  3025. */
  3026. mutex_lock(&dev->mode_config.mutex);
  3027. drm_modeset_acquire_init(ctx, 0);
  3028. while (1) {
  3029. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3030. if (ret != -EDEADLK)
  3031. break;
  3032. drm_modeset_backoff(ctx);
  3033. }
  3034. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3035. if (!i915.force_reset_modeset_test &&
  3036. !gpu_reset_clobbers_display(dev_priv))
  3037. return;
  3038. /*
  3039. * Disabling the crtcs gracefully seems nicer. Also the
  3040. * g33 docs say we should at least disable all the planes.
  3041. */
  3042. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3043. if (IS_ERR(state)) {
  3044. ret = PTR_ERR(state);
  3045. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3046. return;
  3047. }
  3048. ret = drm_atomic_helper_disable_all(dev, ctx);
  3049. if (ret) {
  3050. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3051. drm_atomic_state_put(state);
  3052. return;
  3053. }
  3054. dev_priv->modeset_restore_state = state;
  3055. state->acquire_ctx = ctx;
  3056. }
  3057. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3058. {
  3059. struct drm_device *dev = &dev_priv->drm;
  3060. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3061. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3062. int ret;
  3063. /*
  3064. * Flips in the rings will be nuked by the reset,
  3065. * so complete all pending flips so that user space
  3066. * will get its events and not get stuck.
  3067. */
  3068. intel_complete_page_flips(dev_priv);
  3069. dev_priv->modeset_restore_state = NULL;
  3070. /* reset doesn't touch the display */
  3071. if (!gpu_reset_clobbers_display(dev_priv)) {
  3072. if (!state) {
  3073. /*
  3074. * Flips in the rings have been nuked by the reset,
  3075. * so update the base address of all primary
  3076. * planes to the the last fb to make sure we're
  3077. * showing the correct fb after a reset.
  3078. *
  3079. * FIXME: Atomic will make this obsolete since we won't schedule
  3080. * CS-based flips (which might get lost in gpu resets) any more.
  3081. */
  3082. intel_update_primary_planes(dev);
  3083. } else {
  3084. ret = __intel_display_resume(dev, state, ctx);
  3085. if (ret)
  3086. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3087. }
  3088. } else {
  3089. /*
  3090. * The display has been reset as well,
  3091. * so need a full re-initialization.
  3092. */
  3093. intel_runtime_pm_disable_interrupts(dev_priv);
  3094. intel_runtime_pm_enable_interrupts(dev_priv);
  3095. intel_pps_unlock_regs_wa(dev_priv);
  3096. intel_modeset_init_hw(dev);
  3097. spin_lock_irq(&dev_priv->irq_lock);
  3098. if (dev_priv->display.hpd_irq_setup)
  3099. dev_priv->display.hpd_irq_setup(dev_priv);
  3100. spin_unlock_irq(&dev_priv->irq_lock);
  3101. ret = __intel_display_resume(dev, state, ctx);
  3102. if (ret)
  3103. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3104. intel_hpd_init(dev_priv);
  3105. }
  3106. if (state)
  3107. drm_atomic_state_put(state);
  3108. drm_modeset_drop_locks(ctx);
  3109. drm_modeset_acquire_fini(ctx);
  3110. mutex_unlock(&dev->mode_config.mutex);
  3111. }
  3112. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3113. {
  3114. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3115. if (i915_reset_backoff(error))
  3116. return true;
  3117. if (crtc->reset_count != i915_reset_count(error))
  3118. return true;
  3119. return false;
  3120. }
  3121. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3122. {
  3123. struct drm_device *dev = crtc->dev;
  3124. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3125. bool pending;
  3126. if (abort_flip_on_reset(intel_crtc))
  3127. return false;
  3128. spin_lock_irq(&dev->event_lock);
  3129. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3130. spin_unlock_irq(&dev->event_lock);
  3131. return pending;
  3132. }
  3133. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3134. struct intel_crtc_state *old_crtc_state)
  3135. {
  3136. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3137. struct intel_crtc_state *pipe_config =
  3138. to_intel_crtc_state(crtc->base.state);
  3139. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3140. crtc->base.mode = crtc->base.state->mode;
  3141. /*
  3142. * Update pipe size and adjust fitter if needed: the reason for this is
  3143. * that in compute_mode_changes we check the native mode (not the pfit
  3144. * mode) to see if we can flip rather than do a full mode set. In the
  3145. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3146. * pfit state, we'll end up with a big fb scanned out into the wrong
  3147. * sized surface.
  3148. */
  3149. I915_WRITE(PIPESRC(crtc->pipe),
  3150. ((pipe_config->pipe_src_w - 1) << 16) |
  3151. (pipe_config->pipe_src_h - 1));
  3152. /* on skylake this is done by detaching scalers */
  3153. if (INTEL_GEN(dev_priv) >= 9) {
  3154. skl_detach_scalers(crtc);
  3155. if (pipe_config->pch_pfit.enabled)
  3156. skylake_pfit_enable(crtc);
  3157. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3158. if (pipe_config->pch_pfit.enabled)
  3159. ironlake_pfit_enable(crtc);
  3160. else if (old_crtc_state->pch_pfit.enabled)
  3161. ironlake_pfit_disable(crtc, true);
  3162. }
  3163. }
  3164. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3165. {
  3166. struct drm_device *dev = crtc->base.dev;
  3167. struct drm_i915_private *dev_priv = to_i915(dev);
  3168. int pipe = crtc->pipe;
  3169. i915_reg_t reg;
  3170. u32 temp;
  3171. /* enable normal train */
  3172. reg = FDI_TX_CTL(pipe);
  3173. temp = I915_READ(reg);
  3174. if (IS_IVYBRIDGE(dev_priv)) {
  3175. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3176. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3177. } else {
  3178. temp &= ~FDI_LINK_TRAIN_NONE;
  3179. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3180. }
  3181. I915_WRITE(reg, temp);
  3182. reg = FDI_RX_CTL(pipe);
  3183. temp = I915_READ(reg);
  3184. if (HAS_PCH_CPT(dev_priv)) {
  3185. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3186. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3187. } else {
  3188. temp &= ~FDI_LINK_TRAIN_NONE;
  3189. temp |= FDI_LINK_TRAIN_NONE;
  3190. }
  3191. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3192. /* wait one idle pattern time */
  3193. POSTING_READ(reg);
  3194. udelay(1000);
  3195. /* IVB wants error correction enabled */
  3196. if (IS_IVYBRIDGE(dev_priv))
  3197. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3198. FDI_FE_ERRC_ENABLE);
  3199. }
  3200. /* The FDI link training functions for ILK/Ibexpeak. */
  3201. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3202. const struct intel_crtc_state *crtc_state)
  3203. {
  3204. struct drm_device *dev = crtc->base.dev;
  3205. struct drm_i915_private *dev_priv = to_i915(dev);
  3206. int pipe = crtc->pipe;
  3207. i915_reg_t reg;
  3208. u32 temp, tries;
  3209. /* FDI needs bits from pipe first */
  3210. assert_pipe_enabled(dev_priv, pipe);
  3211. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3212. for train result */
  3213. reg = FDI_RX_IMR(pipe);
  3214. temp = I915_READ(reg);
  3215. temp &= ~FDI_RX_SYMBOL_LOCK;
  3216. temp &= ~FDI_RX_BIT_LOCK;
  3217. I915_WRITE(reg, temp);
  3218. I915_READ(reg);
  3219. udelay(150);
  3220. /* enable CPU FDI TX and PCH FDI RX */
  3221. reg = FDI_TX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3224. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3225. temp &= ~FDI_LINK_TRAIN_NONE;
  3226. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3227. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. temp &= ~FDI_LINK_TRAIN_NONE;
  3231. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3232. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3233. POSTING_READ(reg);
  3234. udelay(150);
  3235. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3236. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3237. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3238. FDI_RX_PHASE_SYNC_POINTER_EN);
  3239. reg = FDI_RX_IIR(pipe);
  3240. for (tries = 0; tries < 5; tries++) {
  3241. temp = I915_READ(reg);
  3242. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3243. if ((temp & FDI_RX_BIT_LOCK)) {
  3244. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3245. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3246. break;
  3247. }
  3248. }
  3249. if (tries == 5)
  3250. DRM_ERROR("FDI train 1 fail!\n");
  3251. /* Train 2 */
  3252. reg = FDI_TX_CTL(pipe);
  3253. temp = I915_READ(reg);
  3254. temp &= ~FDI_LINK_TRAIN_NONE;
  3255. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3256. I915_WRITE(reg, temp);
  3257. reg = FDI_RX_CTL(pipe);
  3258. temp = I915_READ(reg);
  3259. temp &= ~FDI_LINK_TRAIN_NONE;
  3260. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3261. I915_WRITE(reg, temp);
  3262. POSTING_READ(reg);
  3263. udelay(150);
  3264. reg = FDI_RX_IIR(pipe);
  3265. for (tries = 0; tries < 5; tries++) {
  3266. temp = I915_READ(reg);
  3267. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3268. if (temp & FDI_RX_SYMBOL_LOCK) {
  3269. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3270. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3271. break;
  3272. }
  3273. }
  3274. if (tries == 5)
  3275. DRM_ERROR("FDI train 2 fail!\n");
  3276. DRM_DEBUG_KMS("FDI train done\n");
  3277. }
  3278. static const int snb_b_fdi_train_param[] = {
  3279. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3280. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3281. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3282. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3283. };
  3284. /* The FDI link training functions for SNB/Cougarpoint. */
  3285. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3286. const struct intel_crtc_state *crtc_state)
  3287. {
  3288. struct drm_device *dev = crtc->base.dev;
  3289. struct drm_i915_private *dev_priv = to_i915(dev);
  3290. int pipe = crtc->pipe;
  3291. i915_reg_t reg;
  3292. u32 temp, i, retry;
  3293. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3294. for train result */
  3295. reg = FDI_RX_IMR(pipe);
  3296. temp = I915_READ(reg);
  3297. temp &= ~FDI_RX_SYMBOL_LOCK;
  3298. temp &= ~FDI_RX_BIT_LOCK;
  3299. I915_WRITE(reg, temp);
  3300. POSTING_READ(reg);
  3301. udelay(150);
  3302. /* enable CPU FDI TX and PCH FDI RX */
  3303. reg = FDI_TX_CTL(pipe);
  3304. temp = I915_READ(reg);
  3305. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3306. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3307. temp &= ~FDI_LINK_TRAIN_NONE;
  3308. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3309. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3310. /* SNB-B */
  3311. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3312. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3313. I915_WRITE(FDI_RX_MISC(pipe),
  3314. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3315. reg = FDI_RX_CTL(pipe);
  3316. temp = I915_READ(reg);
  3317. if (HAS_PCH_CPT(dev_priv)) {
  3318. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3319. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3320. } else {
  3321. temp &= ~FDI_LINK_TRAIN_NONE;
  3322. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3323. }
  3324. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3325. POSTING_READ(reg);
  3326. udelay(150);
  3327. for (i = 0; i < 4; i++) {
  3328. reg = FDI_TX_CTL(pipe);
  3329. temp = I915_READ(reg);
  3330. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3331. temp |= snb_b_fdi_train_param[i];
  3332. I915_WRITE(reg, temp);
  3333. POSTING_READ(reg);
  3334. udelay(500);
  3335. for (retry = 0; retry < 5; retry++) {
  3336. reg = FDI_RX_IIR(pipe);
  3337. temp = I915_READ(reg);
  3338. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3339. if (temp & FDI_RX_BIT_LOCK) {
  3340. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3341. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3342. break;
  3343. }
  3344. udelay(50);
  3345. }
  3346. if (retry < 5)
  3347. break;
  3348. }
  3349. if (i == 4)
  3350. DRM_ERROR("FDI train 1 fail!\n");
  3351. /* Train 2 */
  3352. reg = FDI_TX_CTL(pipe);
  3353. temp = I915_READ(reg);
  3354. temp &= ~FDI_LINK_TRAIN_NONE;
  3355. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3356. if (IS_GEN6(dev_priv)) {
  3357. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3358. /* SNB-B */
  3359. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3360. }
  3361. I915_WRITE(reg, temp);
  3362. reg = FDI_RX_CTL(pipe);
  3363. temp = I915_READ(reg);
  3364. if (HAS_PCH_CPT(dev_priv)) {
  3365. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3366. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3367. } else {
  3368. temp &= ~FDI_LINK_TRAIN_NONE;
  3369. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3370. }
  3371. I915_WRITE(reg, temp);
  3372. POSTING_READ(reg);
  3373. udelay(150);
  3374. for (i = 0; i < 4; i++) {
  3375. reg = FDI_TX_CTL(pipe);
  3376. temp = I915_READ(reg);
  3377. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3378. temp |= snb_b_fdi_train_param[i];
  3379. I915_WRITE(reg, temp);
  3380. POSTING_READ(reg);
  3381. udelay(500);
  3382. for (retry = 0; retry < 5; retry++) {
  3383. reg = FDI_RX_IIR(pipe);
  3384. temp = I915_READ(reg);
  3385. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3386. if (temp & FDI_RX_SYMBOL_LOCK) {
  3387. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3388. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3389. break;
  3390. }
  3391. udelay(50);
  3392. }
  3393. if (retry < 5)
  3394. break;
  3395. }
  3396. if (i == 4)
  3397. DRM_ERROR("FDI train 2 fail!\n");
  3398. DRM_DEBUG_KMS("FDI train done.\n");
  3399. }
  3400. /* Manual link training for Ivy Bridge A0 parts */
  3401. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3402. const struct intel_crtc_state *crtc_state)
  3403. {
  3404. struct drm_device *dev = crtc->base.dev;
  3405. struct drm_i915_private *dev_priv = to_i915(dev);
  3406. int pipe = crtc->pipe;
  3407. i915_reg_t reg;
  3408. u32 temp, i, j;
  3409. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3410. for train result */
  3411. reg = FDI_RX_IMR(pipe);
  3412. temp = I915_READ(reg);
  3413. temp &= ~FDI_RX_SYMBOL_LOCK;
  3414. temp &= ~FDI_RX_BIT_LOCK;
  3415. I915_WRITE(reg, temp);
  3416. POSTING_READ(reg);
  3417. udelay(150);
  3418. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3419. I915_READ(FDI_RX_IIR(pipe)));
  3420. /* Try each vswing and preemphasis setting twice before moving on */
  3421. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3422. /* disable first in case we need to retry */
  3423. reg = FDI_TX_CTL(pipe);
  3424. temp = I915_READ(reg);
  3425. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3426. temp &= ~FDI_TX_ENABLE;
  3427. I915_WRITE(reg, temp);
  3428. reg = FDI_RX_CTL(pipe);
  3429. temp = I915_READ(reg);
  3430. temp &= ~FDI_LINK_TRAIN_AUTO;
  3431. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3432. temp &= ~FDI_RX_ENABLE;
  3433. I915_WRITE(reg, temp);
  3434. /* enable CPU FDI TX and PCH FDI RX */
  3435. reg = FDI_TX_CTL(pipe);
  3436. temp = I915_READ(reg);
  3437. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3438. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3439. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3440. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3441. temp |= snb_b_fdi_train_param[j/2];
  3442. temp |= FDI_COMPOSITE_SYNC;
  3443. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3444. I915_WRITE(FDI_RX_MISC(pipe),
  3445. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3446. reg = FDI_RX_CTL(pipe);
  3447. temp = I915_READ(reg);
  3448. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3449. temp |= FDI_COMPOSITE_SYNC;
  3450. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3451. POSTING_READ(reg);
  3452. udelay(1); /* should be 0.5us */
  3453. for (i = 0; i < 4; i++) {
  3454. reg = FDI_RX_IIR(pipe);
  3455. temp = I915_READ(reg);
  3456. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3457. if (temp & FDI_RX_BIT_LOCK ||
  3458. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3459. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3460. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3461. i);
  3462. break;
  3463. }
  3464. udelay(1); /* should be 0.5us */
  3465. }
  3466. if (i == 4) {
  3467. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3468. continue;
  3469. }
  3470. /* Train 2 */
  3471. reg = FDI_TX_CTL(pipe);
  3472. temp = I915_READ(reg);
  3473. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3474. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3475. I915_WRITE(reg, temp);
  3476. reg = FDI_RX_CTL(pipe);
  3477. temp = I915_READ(reg);
  3478. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3479. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3480. I915_WRITE(reg, temp);
  3481. POSTING_READ(reg);
  3482. udelay(2); /* should be 1.5us */
  3483. for (i = 0; i < 4; i++) {
  3484. reg = FDI_RX_IIR(pipe);
  3485. temp = I915_READ(reg);
  3486. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3487. if (temp & FDI_RX_SYMBOL_LOCK ||
  3488. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3489. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3490. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3491. i);
  3492. goto train_done;
  3493. }
  3494. udelay(2); /* should be 1.5us */
  3495. }
  3496. if (i == 4)
  3497. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3498. }
  3499. train_done:
  3500. DRM_DEBUG_KMS("FDI train done.\n");
  3501. }
  3502. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3503. {
  3504. struct drm_device *dev = intel_crtc->base.dev;
  3505. struct drm_i915_private *dev_priv = to_i915(dev);
  3506. int pipe = intel_crtc->pipe;
  3507. i915_reg_t reg;
  3508. u32 temp;
  3509. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3510. reg = FDI_RX_CTL(pipe);
  3511. temp = I915_READ(reg);
  3512. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3513. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3514. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3515. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3516. POSTING_READ(reg);
  3517. udelay(200);
  3518. /* Switch from Rawclk to PCDclk */
  3519. temp = I915_READ(reg);
  3520. I915_WRITE(reg, temp | FDI_PCDCLK);
  3521. POSTING_READ(reg);
  3522. udelay(200);
  3523. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3524. reg = FDI_TX_CTL(pipe);
  3525. temp = I915_READ(reg);
  3526. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3527. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3528. POSTING_READ(reg);
  3529. udelay(100);
  3530. }
  3531. }
  3532. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3533. {
  3534. struct drm_device *dev = intel_crtc->base.dev;
  3535. struct drm_i915_private *dev_priv = to_i915(dev);
  3536. int pipe = intel_crtc->pipe;
  3537. i915_reg_t reg;
  3538. u32 temp;
  3539. /* Switch from PCDclk to Rawclk */
  3540. reg = FDI_RX_CTL(pipe);
  3541. temp = I915_READ(reg);
  3542. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3543. /* Disable CPU FDI TX PLL */
  3544. reg = FDI_TX_CTL(pipe);
  3545. temp = I915_READ(reg);
  3546. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3547. POSTING_READ(reg);
  3548. udelay(100);
  3549. reg = FDI_RX_CTL(pipe);
  3550. temp = I915_READ(reg);
  3551. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3552. /* Wait for the clocks to turn off. */
  3553. POSTING_READ(reg);
  3554. udelay(100);
  3555. }
  3556. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3557. {
  3558. struct drm_device *dev = crtc->dev;
  3559. struct drm_i915_private *dev_priv = to_i915(dev);
  3560. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3561. int pipe = intel_crtc->pipe;
  3562. i915_reg_t reg;
  3563. u32 temp;
  3564. /* disable CPU FDI tx and PCH FDI rx */
  3565. reg = FDI_TX_CTL(pipe);
  3566. temp = I915_READ(reg);
  3567. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3568. POSTING_READ(reg);
  3569. reg = FDI_RX_CTL(pipe);
  3570. temp = I915_READ(reg);
  3571. temp &= ~(0x7 << 16);
  3572. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3573. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3574. POSTING_READ(reg);
  3575. udelay(100);
  3576. /* Ironlake workaround, disable clock pointer after downing FDI */
  3577. if (HAS_PCH_IBX(dev_priv))
  3578. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3579. /* still set train pattern 1 */
  3580. reg = FDI_TX_CTL(pipe);
  3581. temp = I915_READ(reg);
  3582. temp &= ~FDI_LINK_TRAIN_NONE;
  3583. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3584. I915_WRITE(reg, temp);
  3585. reg = FDI_RX_CTL(pipe);
  3586. temp = I915_READ(reg);
  3587. if (HAS_PCH_CPT(dev_priv)) {
  3588. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3589. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3590. } else {
  3591. temp &= ~FDI_LINK_TRAIN_NONE;
  3592. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3593. }
  3594. /* BPC in FDI rx is consistent with that in PIPECONF */
  3595. temp &= ~(0x07 << 16);
  3596. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3597. I915_WRITE(reg, temp);
  3598. POSTING_READ(reg);
  3599. udelay(100);
  3600. }
  3601. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3602. {
  3603. struct intel_crtc *crtc;
  3604. /* Note that we don't need to be called with mode_config.lock here
  3605. * as our list of CRTC objects is static for the lifetime of the
  3606. * device and so cannot disappear as we iterate. Similarly, we can
  3607. * happily treat the predicates as racy, atomic checks as userspace
  3608. * cannot claim and pin a new fb without at least acquring the
  3609. * struct_mutex and so serialising with us.
  3610. */
  3611. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3612. if (atomic_read(&crtc->unpin_work_count) == 0)
  3613. continue;
  3614. if (crtc->flip_work)
  3615. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3616. return true;
  3617. }
  3618. return false;
  3619. }
  3620. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3621. {
  3622. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3623. struct intel_flip_work *work = intel_crtc->flip_work;
  3624. intel_crtc->flip_work = NULL;
  3625. if (work->event)
  3626. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3627. drm_crtc_vblank_put(&intel_crtc->base);
  3628. wake_up_all(&dev_priv->pending_flip_queue);
  3629. trace_i915_flip_complete(intel_crtc->plane,
  3630. work->pending_flip_obj);
  3631. queue_work(dev_priv->wq, &work->unpin_work);
  3632. }
  3633. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3634. {
  3635. struct drm_device *dev = crtc->dev;
  3636. struct drm_i915_private *dev_priv = to_i915(dev);
  3637. long ret;
  3638. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3639. ret = wait_event_interruptible_timeout(
  3640. dev_priv->pending_flip_queue,
  3641. !intel_crtc_has_pending_flip(crtc),
  3642. 60*HZ);
  3643. if (ret < 0)
  3644. return ret;
  3645. if (ret == 0) {
  3646. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3647. struct intel_flip_work *work;
  3648. spin_lock_irq(&dev->event_lock);
  3649. work = intel_crtc->flip_work;
  3650. if (work && !is_mmio_work(work)) {
  3651. WARN_ONCE(1, "Removing stuck page flip\n");
  3652. page_flip_completed(intel_crtc);
  3653. }
  3654. spin_unlock_irq(&dev->event_lock);
  3655. }
  3656. return 0;
  3657. }
  3658. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3659. {
  3660. u32 temp;
  3661. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3662. mutex_lock(&dev_priv->sb_lock);
  3663. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3664. temp |= SBI_SSCCTL_DISABLE;
  3665. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3666. mutex_unlock(&dev_priv->sb_lock);
  3667. }
  3668. /* Program iCLKIP clock to the desired frequency */
  3669. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3670. {
  3671. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3672. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3673. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3674. u32 temp;
  3675. lpt_disable_iclkip(dev_priv);
  3676. /* The iCLK virtual clock root frequency is in MHz,
  3677. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3678. * divisors, it is necessary to divide one by another, so we
  3679. * convert the virtual clock precision to KHz here for higher
  3680. * precision.
  3681. */
  3682. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3683. u32 iclk_virtual_root_freq = 172800 * 1000;
  3684. u32 iclk_pi_range = 64;
  3685. u32 desired_divisor;
  3686. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3687. clock << auxdiv);
  3688. divsel = (desired_divisor / iclk_pi_range) - 2;
  3689. phaseinc = desired_divisor % iclk_pi_range;
  3690. /*
  3691. * Near 20MHz is a corner case which is
  3692. * out of range for the 7-bit divisor
  3693. */
  3694. if (divsel <= 0x7f)
  3695. break;
  3696. }
  3697. /* This should not happen with any sane values */
  3698. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3699. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3700. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3701. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3702. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3703. clock,
  3704. auxdiv,
  3705. divsel,
  3706. phasedir,
  3707. phaseinc);
  3708. mutex_lock(&dev_priv->sb_lock);
  3709. /* Program SSCDIVINTPHASE6 */
  3710. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3711. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3712. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3713. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3714. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3715. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3716. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3717. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3718. /* Program SSCAUXDIV */
  3719. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3720. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3721. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3722. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3723. /* Enable modulator and associated divider */
  3724. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3725. temp &= ~SBI_SSCCTL_DISABLE;
  3726. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3727. mutex_unlock(&dev_priv->sb_lock);
  3728. /* Wait for initialization time */
  3729. udelay(24);
  3730. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3731. }
  3732. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3733. {
  3734. u32 divsel, phaseinc, auxdiv;
  3735. u32 iclk_virtual_root_freq = 172800 * 1000;
  3736. u32 iclk_pi_range = 64;
  3737. u32 desired_divisor;
  3738. u32 temp;
  3739. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3740. return 0;
  3741. mutex_lock(&dev_priv->sb_lock);
  3742. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3743. if (temp & SBI_SSCCTL_DISABLE) {
  3744. mutex_unlock(&dev_priv->sb_lock);
  3745. return 0;
  3746. }
  3747. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3748. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3749. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3750. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3751. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3752. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3753. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3754. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3755. mutex_unlock(&dev_priv->sb_lock);
  3756. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3757. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3758. desired_divisor << auxdiv);
  3759. }
  3760. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3761. enum pipe pch_transcoder)
  3762. {
  3763. struct drm_device *dev = crtc->base.dev;
  3764. struct drm_i915_private *dev_priv = to_i915(dev);
  3765. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3766. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3767. I915_READ(HTOTAL(cpu_transcoder)));
  3768. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3769. I915_READ(HBLANK(cpu_transcoder)));
  3770. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3771. I915_READ(HSYNC(cpu_transcoder)));
  3772. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3773. I915_READ(VTOTAL(cpu_transcoder)));
  3774. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3775. I915_READ(VBLANK(cpu_transcoder)));
  3776. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3777. I915_READ(VSYNC(cpu_transcoder)));
  3778. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3779. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3780. }
  3781. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3782. {
  3783. struct drm_i915_private *dev_priv = to_i915(dev);
  3784. uint32_t temp;
  3785. temp = I915_READ(SOUTH_CHICKEN1);
  3786. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3787. return;
  3788. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3789. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3790. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3791. if (enable)
  3792. temp |= FDI_BC_BIFURCATION_SELECT;
  3793. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3794. I915_WRITE(SOUTH_CHICKEN1, temp);
  3795. POSTING_READ(SOUTH_CHICKEN1);
  3796. }
  3797. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3798. {
  3799. struct drm_device *dev = intel_crtc->base.dev;
  3800. switch (intel_crtc->pipe) {
  3801. case PIPE_A:
  3802. break;
  3803. case PIPE_B:
  3804. if (intel_crtc->config->fdi_lanes > 2)
  3805. cpt_set_fdi_bc_bifurcation(dev, false);
  3806. else
  3807. cpt_set_fdi_bc_bifurcation(dev, true);
  3808. break;
  3809. case PIPE_C:
  3810. cpt_set_fdi_bc_bifurcation(dev, true);
  3811. break;
  3812. default:
  3813. BUG();
  3814. }
  3815. }
  3816. /* Return which DP Port should be selected for Transcoder DP control */
  3817. static enum port
  3818. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3819. {
  3820. struct drm_device *dev = crtc->base.dev;
  3821. struct intel_encoder *encoder;
  3822. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3823. if (encoder->type == INTEL_OUTPUT_DP ||
  3824. encoder->type == INTEL_OUTPUT_EDP)
  3825. return enc_to_dig_port(&encoder->base)->port;
  3826. }
  3827. return -1;
  3828. }
  3829. /*
  3830. * Enable PCH resources required for PCH ports:
  3831. * - PCH PLLs
  3832. * - FDI training & RX/TX
  3833. * - update transcoder timings
  3834. * - DP transcoding bits
  3835. * - transcoder
  3836. */
  3837. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3838. {
  3839. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3840. struct drm_device *dev = crtc->base.dev;
  3841. struct drm_i915_private *dev_priv = to_i915(dev);
  3842. int pipe = crtc->pipe;
  3843. u32 temp;
  3844. assert_pch_transcoder_disabled(dev_priv, pipe);
  3845. if (IS_IVYBRIDGE(dev_priv))
  3846. ivybridge_update_fdi_bc_bifurcation(crtc);
  3847. /* Write the TU size bits before fdi link training, so that error
  3848. * detection works. */
  3849. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3850. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3851. /* For PCH output, training FDI link */
  3852. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3853. /* We need to program the right clock selection before writing the pixel
  3854. * mutliplier into the DPLL. */
  3855. if (HAS_PCH_CPT(dev_priv)) {
  3856. u32 sel;
  3857. temp = I915_READ(PCH_DPLL_SEL);
  3858. temp |= TRANS_DPLL_ENABLE(pipe);
  3859. sel = TRANS_DPLLB_SEL(pipe);
  3860. if (crtc_state->shared_dpll ==
  3861. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3862. temp |= sel;
  3863. else
  3864. temp &= ~sel;
  3865. I915_WRITE(PCH_DPLL_SEL, temp);
  3866. }
  3867. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3868. * transcoder, and we actually should do this to not upset any PCH
  3869. * transcoder that already use the clock when we share it.
  3870. *
  3871. * Note that enable_shared_dpll tries to do the right thing, but
  3872. * get_shared_dpll unconditionally resets the pll - we need that to have
  3873. * the right LVDS enable sequence. */
  3874. intel_enable_shared_dpll(crtc);
  3875. /* set transcoder timing, panel must allow it */
  3876. assert_panel_unlocked(dev_priv, pipe);
  3877. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3878. intel_fdi_normal_train(crtc);
  3879. /* For PCH DP, enable TRANS_DP_CTL */
  3880. if (HAS_PCH_CPT(dev_priv) &&
  3881. intel_crtc_has_dp_encoder(crtc_state)) {
  3882. const struct drm_display_mode *adjusted_mode =
  3883. &crtc_state->base.adjusted_mode;
  3884. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3885. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3886. temp = I915_READ(reg);
  3887. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3888. TRANS_DP_SYNC_MASK |
  3889. TRANS_DP_BPC_MASK);
  3890. temp |= TRANS_DP_OUTPUT_ENABLE;
  3891. temp |= bpc << 9; /* same format but at 11:9 */
  3892. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3893. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3894. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3895. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3896. switch (intel_trans_dp_port_sel(crtc)) {
  3897. case PORT_B:
  3898. temp |= TRANS_DP_PORT_SEL_B;
  3899. break;
  3900. case PORT_C:
  3901. temp |= TRANS_DP_PORT_SEL_C;
  3902. break;
  3903. case PORT_D:
  3904. temp |= TRANS_DP_PORT_SEL_D;
  3905. break;
  3906. default:
  3907. BUG();
  3908. }
  3909. I915_WRITE(reg, temp);
  3910. }
  3911. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3912. }
  3913. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3914. {
  3915. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3916. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3917. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3918. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3919. lpt_program_iclkip(crtc);
  3920. /* Set transcoder timing. */
  3921. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3922. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3923. }
  3924. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3925. {
  3926. struct drm_i915_private *dev_priv = to_i915(dev);
  3927. i915_reg_t dslreg = PIPEDSL(pipe);
  3928. u32 temp;
  3929. temp = I915_READ(dslreg);
  3930. udelay(500);
  3931. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3932. if (wait_for(I915_READ(dslreg) != temp, 5))
  3933. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3934. }
  3935. }
  3936. static int
  3937. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3938. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3939. int src_w, int src_h, int dst_w, int dst_h)
  3940. {
  3941. struct intel_crtc_scaler_state *scaler_state =
  3942. &crtc_state->scaler_state;
  3943. struct intel_crtc *intel_crtc =
  3944. to_intel_crtc(crtc_state->base.crtc);
  3945. int need_scaling;
  3946. need_scaling = drm_rotation_90_or_270(rotation) ?
  3947. (src_h != dst_w || src_w != dst_h):
  3948. (src_w != dst_w || src_h != dst_h);
  3949. /*
  3950. * if plane is being disabled or scaler is no more required or force detach
  3951. * - free scaler binded to this plane/crtc
  3952. * - in order to do this, update crtc->scaler_usage
  3953. *
  3954. * Here scaler state in crtc_state is set free so that
  3955. * scaler can be assigned to other user. Actual register
  3956. * update to free the scaler is done in plane/panel-fit programming.
  3957. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3958. */
  3959. if (force_detach || !need_scaling) {
  3960. if (*scaler_id >= 0) {
  3961. scaler_state->scaler_users &= ~(1 << scaler_user);
  3962. scaler_state->scalers[*scaler_id].in_use = 0;
  3963. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3964. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3965. intel_crtc->pipe, scaler_user, *scaler_id,
  3966. scaler_state->scaler_users);
  3967. *scaler_id = -1;
  3968. }
  3969. return 0;
  3970. }
  3971. /* range checks */
  3972. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3973. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3974. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3975. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3976. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3977. "size is out of scaler range\n",
  3978. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3979. return -EINVAL;
  3980. }
  3981. /* mark this plane as a scaler user in crtc_state */
  3982. scaler_state->scaler_users |= (1 << scaler_user);
  3983. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3984. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3985. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3986. scaler_state->scaler_users);
  3987. return 0;
  3988. }
  3989. /**
  3990. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3991. *
  3992. * @state: crtc's scaler state
  3993. *
  3994. * Return
  3995. * 0 - scaler_usage updated successfully
  3996. * error - requested scaling cannot be supported or other error condition
  3997. */
  3998. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3999. {
  4000. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4001. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4002. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4003. state->pipe_src_w, state->pipe_src_h,
  4004. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4005. }
  4006. /**
  4007. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4008. *
  4009. * @state: crtc's scaler state
  4010. * @plane_state: atomic plane state to update
  4011. *
  4012. * Return
  4013. * 0 - scaler_usage updated successfully
  4014. * error - requested scaling cannot be supported or other error condition
  4015. */
  4016. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4017. struct intel_plane_state *plane_state)
  4018. {
  4019. struct intel_plane *intel_plane =
  4020. to_intel_plane(plane_state->base.plane);
  4021. struct drm_framebuffer *fb = plane_state->base.fb;
  4022. int ret;
  4023. bool force_detach = !fb || !plane_state->base.visible;
  4024. ret = skl_update_scaler(crtc_state, force_detach,
  4025. drm_plane_index(&intel_plane->base),
  4026. &plane_state->scaler_id,
  4027. plane_state->base.rotation,
  4028. drm_rect_width(&plane_state->base.src) >> 16,
  4029. drm_rect_height(&plane_state->base.src) >> 16,
  4030. drm_rect_width(&plane_state->base.dst),
  4031. drm_rect_height(&plane_state->base.dst));
  4032. if (ret || plane_state->scaler_id < 0)
  4033. return ret;
  4034. /* check colorkey */
  4035. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4036. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4037. intel_plane->base.base.id,
  4038. intel_plane->base.name);
  4039. return -EINVAL;
  4040. }
  4041. /* Check src format */
  4042. switch (fb->format->format) {
  4043. case DRM_FORMAT_RGB565:
  4044. case DRM_FORMAT_XBGR8888:
  4045. case DRM_FORMAT_XRGB8888:
  4046. case DRM_FORMAT_ABGR8888:
  4047. case DRM_FORMAT_ARGB8888:
  4048. case DRM_FORMAT_XRGB2101010:
  4049. case DRM_FORMAT_XBGR2101010:
  4050. case DRM_FORMAT_YUYV:
  4051. case DRM_FORMAT_YVYU:
  4052. case DRM_FORMAT_UYVY:
  4053. case DRM_FORMAT_VYUY:
  4054. break;
  4055. default:
  4056. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4057. intel_plane->base.base.id, intel_plane->base.name,
  4058. fb->base.id, fb->format->format);
  4059. return -EINVAL;
  4060. }
  4061. return 0;
  4062. }
  4063. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4064. {
  4065. int i;
  4066. for (i = 0; i < crtc->num_scalers; i++)
  4067. skl_detach_scaler(crtc, i);
  4068. }
  4069. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4070. {
  4071. struct drm_device *dev = crtc->base.dev;
  4072. struct drm_i915_private *dev_priv = to_i915(dev);
  4073. int pipe = crtc->pipe;
  4074. struct intel_crtc_scaler_state *scaler_state =
  4075. &crtc->config->scaler_state;
  4076. if (crtc->config->pch_pfit.enabled) {
  4077. int id;
  4078. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4079. return;
  4080. id = scaler_state->scaler_id;
  4081. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4082. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4083. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4084. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4085. }
  4086. }
  4087. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4088. {
  4089. struct drm_device *dev = crtc->base.dev;
  4090. struct drm_i915_private *dev_priv = to_i915(dev);
  4091. int pipe = crtc->pipe;
  4092. if (crtc->config->pch_pfit.enabled) {
  4093. /* Force use of hard-coded filter coefficients
  4094. * as some pre-programmed values are broken,
  4095. * e.g. x201.
  4096. */
  4097. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4098. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4099. PF_PIPE_SEL_IVB(pipe));
  4100. else
  4101. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4102. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4103. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4104. }
  4105. }
  4106. void hsw_enable_ips(struct intel_crtc *crtc)
  4107. {
  4108. struct drm_device *dev = crtc->base.dev;
  4109. struct drm_i915_private *dev_priv = to_i915(dev);
  4110. if (!crtc->config->ips_enabled)
  4111. return;
  4112. /*
  4113. * We can only enable IPS after we enable a plane and wait for a vblank
  4114. * This function is called from post_plane_update, which is run after
  4115. * a vblank wait.
  4116. */
  4117. assert_plane_enabled(dev_priv, crtc->plane);
  4118. if (IS_BROADWELL(dev_priv)) {
  4119. mutex_lock(&dev_priv->rps.hw_lock);
  4120. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4121. mutex_unlock(&dev_priv->rps.hw_lock);
  4122. /* Quoting Art Runyan: "its not safe to expect any particular
  4123. * value in IPS_CTL bit 31 after enabling IPS through the
  4124. * mailbox." Moreover, the mailbox may return a bogus state,
  4125. * so we need to just enable it and continue on.
  4126. */
  4127. } else {
  4128. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4129. /* The bit only becomes 1 in the next vblank, so this wait here
  4130. * is essentially intel_wait_for_vblank. If we don't have this
  4131. * and don't wait for vblanks until the end of crtc_enable, then
  4132. * the HW state readout code will complain that the expected
  4133. * IPS_CTL value is not the one we read. */
  4134. if (intel_wait_for_register(dev_priv,
  4135. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4136. 50))
  4137. DRM_ERROR("Timed out waiting for IPS enable\n");
  4138. }
  4139. }
  4140. void hsw_disable_ips(struct intel_crtc *crtc)
  4141. {
  4142. struct drm_device *dev = crtc->base.dev;
  4143. struct drm_i915_private *dev_priv = to_i915(dev);
  4144. if (!crtc->config->ips_enabled)
  4145. return;
  4146. assert_plane_enabled(dev_priv, crtc->plane);
  4147. if (IS_BROADWELL(dev_priv)) {
  4148. mutex_lock(&dev_priv->rps.hw_lock);
  4149. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4150. mutex_unlock(&dev_priv->rps.hw_lock);
  4151. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4152. if (intel_wait_for_register(dev_priv,
  4153. IPS_CTL, IPS_ENABLE, 0,
  4154. 42))
  4155. DRM_ERROR("Timed out waiting for IPS disable\n");
  4156. } else {
  4157. I915_WRITE(IPS_CTL, 0);
  4158. POSTING_READ(IPS_CTL);
  4159. }
  4160. /* We need to wait for a vblank before we can disable the plane. */
  4161. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4162. }
  4163. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4164. {
  4165. if (intel_crtc->overlay) {
  4166. struct drm_device *dev = intel_crtc->base.dev;
  4167. struct drm_i915_private *dev_priv = to_i915(dev);
  4168. mutex_lock(&dev->struct_mutex);
  4169. dev_priv->mm.interruptible = false;
  4170. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4171. dev_priv->mm.interruptible = true;
  4172. mutex_unlock(&dev->struct_mutex);
  4173. }
  4174. /* Let userspace switch the overlay on again. In most cases userspace
  4175. * has to recompute where to put it anyway.
  4176. */
  4177. }
  4178. /**
  4179. * intel_post_enable_primary - Perform operations after enabling primary plane
  4180. * @crtc: the CRTC whose primary plane was just enabled
  4181. *
  4182. * Performs potentially sleeping operations that must be done after the primary
  4183. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4184. * called due to an explicit primary plane update, or due to an implicit
  4185. * re-enable that is caused when a sprite plane is updated to no longer
  4186. * completely hide the primary plane.
  4187. */
  4188. static void
  4189. intel_post_enable_primary(struct drm_crtc *crtc)
  4190. {
  4191. struct drm_device *dev = crtc->dev;
  4192. struct drm_i915_private *dev_priv = to_i915(dev);
  4193. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4194. int pipe = intel_crtc->pipe;
  4195. /*
  4196. * FIXME IPS should be fine as long as one plane is
  4197. * enabled, but in practice it seems to have problems
  4198. * when going from primary only to sprite only and vice
  4199. * versa.
  4200. */
  4201. hsw_enable_ips(intel_crtc);
  4202. /*
  4203. * Gen2 reports pipe underruns whenever all planes are disabled.
  4204. * So don't enable underrun reporting before at least some planes
  4205. * are enabled.
  4206. * FIXME: Need to fix the logic to work when we turn off all planes
  4207. * but leave the pipe running.
  4208. */
  4209. if (IS_GEN2(dev_priv))
  4210. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4211. /* Underruns don't always raise interrupts, so check manually. */
  4212. intel_check_cpu_fifo_underruns(dev_priv);
  4213. intel_check_pch_fifo_underruns(dev_priv);
  4214. }
  4215. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4216. static void
  4217. intel_pre_disable_primary(struct drm_crtc *crtc)
  4218. {
  4219. struct drm_device *dev = crtc->dev;
  4220. struct drm_i915_private *dev_priv = to_i915(dev);
  4221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4222. int pipe = intel_crtc->pipe;
  4223. /*
  4224. * Gen2 reports pipe underruns whenever all planes are disabled.
  4225. * So diasble underrun reporting before all the planes get disabled.
  4226. * FIXME: Need to fix the logic to work when we turn off all planes
  4227. * but leave the pipe running.
  4228. */
  4229. if (IS_GEN2(dev_priv))
  4230. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4231. /*
  4232. * FIXME IPS should be fine as long as one plane is
  4233. * enabled, but in practice it seems to have problems
  4234. * when going from primary only to sprite only and vice
  4235. * versa.
  4236. */
  4237. hsw_disable_ips(intel_crtc);
  4238. }
  4239. /* FIXME get rid of this and use pre_plane_update */
  4240. static void
  4241. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4242. {
  4243. struct drm_device *dev = crtc->dev;
  4244. struct drm_i915_private *dev_priv = to_i915(dev);
  4245. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4246. int pipe = intel_crtc->pipe;
  4247. intel_pre_disable_primary(crtc);
  4248. /*
  4249. * Vblank time updates from the shadow to live plane control register
  4250. * are blocked if the memory self-refresh mode is active at that
  4251. * moment. So to make sure the plane gets truly disabled, disable
  4252. * first the self-refresh mode. The self-refresh enable bit in turn
  4253. * will be checked/applied by the HW only at the next frame start
  4254. * event which is after the vblank start event, so we need to have a
  4255. * wait-for-vblank between disabling the plane and the pipe.
  4256. */
  4257. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4258. intel_set_memory_cxsr(dev_priv, false))
  4259. intel_wait_for_vblank(dev_priv, pipe);
  4260. }
  4261. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4262. {
  4263. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4264. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4265. struct intel_crtc_state *pipe_config =
  4266. to_intel_crtc_state(crtc->base.state);
  4267. struct drm_plane *primary = crtc->base.primary;
  4268. struct drm_plane_state *old_pri_state =
  4269. drm_atomic_get_existing_plane_state(old_state, primary);
  4270. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4271. if (pipe_config->update_wm_post && pipe_config->base.active)
  4272. intel_update_watermarks(crtc);
  4273. if (old_pri_state) {
  4274. struct intel_plane_state *primary_state =
  4275. to_intel_plane_state(primary->state);
  4276. struct intel_plane_state *old_primary_state =
  4277. to_intel_plane_state(old_pri_state);
  4278. intel_fbc_post_update(crtc);
  4279. if (primary_state->base.visible &&
  4280. (needs_modeset(&pipe_config->base) ||
  4281. !old_primary_state->base.visible))
  4282. intel_post_enable_primary(&crtc->base);
  4283. }
  4284. }
  4285. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4286. struct intel_crtc_state *pipe_config)
  4287. {
  4288. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4289. struct drm_device *dev = crtc->base.dev;
  4290. struct drm_i915_private *dev_priv = to_i915(dev);
  4291. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4292. struct drm_plane *primary = crtc->base.primary;
  4293. struct drm_plane_state *old_pri_state =
  4294. drm_atomic_get_existing_plane_state(old_state, primary);
  4295. bool modeset = needs_modeset(&pipe_config->base);
  4296. struct intel_atomic_state *old_intel_state =
  4297. to_intel_atomic_state(old_state);
  4298. if (old_pri_state) {
  4299. struct intel_plane_state *primary_state =
  4300. to_intel_plane_state(primary->state);
  4301. struct intel_plane_state *old_primary_state =
  4302. to_intel_plane_state(old_pri_state);
  4303. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4304. if (old_primary_state->base.visible &&
  4305. (modeset || !primary_state->base.visible))
  4306. intel_pre_disable_primary(&crtc->base);
  4307. }
  4308. /*
  4309. * Vblank time updates from the shadow to live plane control register
  4310. * are blocked if the memory self-refresh mode is active at that
  4311. * moment. So to make sure the plane gets truly disabled, disable
  4312. * first the self-refresh mode. The self-refresh enable bit in turn
  4313. * will be checked/applied by the HW only at the next frame start
  4314. * event which is after the vblank start event, so we need to have a
  4315. * wait-for-vblank between disabling the plane and the pipe.
  4316. */
  4317. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4318. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4319. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4320. /*
  4321. * IVB workaround: must disable low power watermarks for at least
  4322. * one frame before enabling scaling. LP watermarks can be re-enabled
  4323. * when scaling is disabled.
  4324. *
  4325. * WaCxSRDisabledForSpriteScaling:ivb
  4326. */
  4327. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4328. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4329. /*
  4330. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4331. * watermark programming here.
  4332. */
  4333. if (needs_modeset(&pipe_config->base))
  4334. return;
  4335. /*
  4336. * For platforms that support atomic watermarks, program the
  4337. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4338. * will be the intermediate values that are safe for both pre- and
  4339. * post- vblank; when vblank happens, the 'active' values will be set
  4340. * to the final 'target' values and we'll do this again to get the
  4341. * optimal watermarks. For gen9+ platforms, the values we program here
  4342. * will be the final target values which will get automatically latched
  4343. * at vblank time; no further programming will be necessary.
  4344. *
  4345. * If a platform hasn't been transitioned to atomic watermarks yet,
  4346. * we'll continue to update watermarks the old way, if flags tell
  4347. * us to.
  4348. */
  4349. if (dev_priv->display.initial_watermarks != NULL)
  4350. dev_priv->display.initial_watermarks(old_intel_state,
  4351. pipe_config);
  4352. else if (pipe_config->update_wm_pre)
  4353. intel_update_watermarks(crtc);
  4354. }
  4355. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4356. {
  4357. struct drm_device *dev = crtc->dev;
  4358. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4359. struct drm_plane *p;
  4360. int pipe = intel_crtc->pipe;
  4361. intel_crtc_dpms_overlay_disable(intel_crtc);
  4362. drm_for_each_plane_mask(p, dev, plane_mask)
  4363. to_intel_plane(p)->disable_plane(p, crtc);
  4364. /*
  4365. * FIXME: Once we grow proper nuclear flip support out of this we need
  4366. * to compute the mask of flip planes precisely. For the time being
  4367. * consider this a flip to a NULL plane.
  4368. */
  4369. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4370. }
  4371. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4372. struct intel_crtc_state *crtc_state,
  4373. struct drm_atomic_state *old_state)
  4374. {
  4375. struct drm_connector_state *conn_state;
  4376. struct drm_connector *conn;
  4377. int i;
  4378. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4379. struct intel_encoder *encoder =
  4380. to_intel_encoder(conn_state->best_encoder);
  4381. if (conn_state->crtc != crtc)
  4382. continue;
  4383. if (encoder->pre_pll_enable)
  4384. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4385. }
  4386. }
  4387. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4388. struct intel_crtc_state *crtc_state,
  4389. struct drm_atomic_state *old_state)
  4390. {
  4391. struct drm_connector_state *conn_state;
  4392. struct drm_connector *conn;
  4393. int i;
  4394. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4395. struct intel_encoder *encoder =
  4396. to_intel_encoder(conn_state->best_encoder);
  4397. if (conn_state->crtc != crtc)
  4398. continue;
  4399. if (encoder->pre_enable)
  4400. encoder->pre_enable(encoder, crtc_state, conn_state);
  4401. }
  4402. }
  4403. static void intel_encoders_enable(struct drm_crtc *crtc,
  4404. struct intel_crtc_state *crtc_state,
  4405. struct drm_atomic_state *old_state)
  4406. {
  4407. struct drm_connector_state *conn_state;
  4408. struct drm_connector *conn;
  4409. int i;
  4410. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4411. struct intel_encoder *encoder =
  4412. to_intel_encoder(conn_state->best_encoder);
  4413. if (conn_state->crtc != crtc)
  4414. continue;
  4415. encoder->enable(encoder, crtc_state, conn_state);
  4416. intel_opregion_notify_encoder(encoder, true);
  4417. }
  4418. }
  4419. static void intel_encoders_disable(struct drm_crtc *crtc,
  4420. struct intel_crtc_state *old_crtc_state,
  4421. struct drm_atomic_state *old_state)
  4422. {
  4423. struct drm_connector_state *old_conn_state;
  4424. struct drm_connector *conn;
  4425. int i;
  4426. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4427. struct intel_encoder *encoder =
  4428. to_intel_encoder(old_conn_state->best_encoder);
  4429. if (old_conn_state->crtc != crtc)
  4430. continue;
  4431. intel_opregion_notify_encoder(encoder, false);
  4432. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4433. }
  4434. }
  4435. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4436. struct intel_crtc_state *old_crtc_state,
  4437. struct drm_atomic_state *old_state)
  4438. {
  4439. struct drm_connector_state *old_conn_state;
  4440. struct drm_connector *conn;
  4441. int i;
  4442. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4443. struct intel_encoder *encoder =
  4444. to_intel_encoder(old_conn_state->best_encoder);
  4445. if (old_conn_state->crtc != crtc)
  4446. continue;
  4447. if (encoder->post_disable)
  4448. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4449. }
  4450. }
  4451. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4452. struct intel_crtc_state *old_crtc_state,
  4453. struct drm_atomic_state *old_state)
  4454. {
  4455. struct drm_connector_state *old_conn_state;
  4456. struct drm_connector *conn;
  4457. int i;
  4458. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4459. struct intel_encoder *encoder =
  4460. to_intel_encoder(old_conn_state->best_encoder);
  4461. if (old_conn_state->crtc != crtc)
  4462. continue;
  4463. if (encoder->post_pll_disable)
  4464. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4465. }
  4466. }
  4467. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4468. struct drm_atomic_state *old_state)
  4469. {
  4470. struct drm_crtc *crtc = pipe_config->base.crtc;
  4471. struct drm_device *dev = crtc->dev;
  4472. struct drm_i915_private *dev_priv = to_i915(dev);
  4473. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4474. int pipe = intel_crtc->pipe;
  4475. struct intel_atomic_state *old_intel_state =
  4476. to_intel_atomic_state(old_state);
  4477. if (WARN_ON(intel_crtc->active))
  4478. return;
  4479. /*
  4480. * Sometimes spurious CPU pipe underruns happen during FDI
  4481. * training, at least with VGA+HDMI cloning. Suppress them.
  4482. *
  4483. * On ILK we get an occasional spurious CPU pipe underruns
  4484. * between eDP port A enable and vdd enable. Also PCH port
  4485. * enable seems to result in the occasional CPU pipe underrun.
  4486. *
  4487. * Spurious PCH underruns also occur during PCH enabling.
  4488. */
  4489. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4490. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4491. if (intel_crtc->config->has_pch_encoder)
  4492. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4493. if (intel_crtc->config->has_pch_encoder)
  4494. intel_prepare_shared_dpll(intel_crtc);
  4495. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4496. intel_dp_set_m_n(intel_crtc, M1_N1);
  4497. intel_set_pipe_timings(intel_crtc);
  4498. intel_set_pipe_src_size(intel_crtc);
  4499. if (intel_crtc->config->has_pch_encoder) {
  4500. intel_cpu_transcoder_set_m_n(intel_crtc,
  4501. &intel_crtc->config->fdi_m_n, NULL);
  4502. }
  4503. ironlake_set_pipeconf(crtc);
  4504. intel_crtc->active = true;
  4505. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4506. if (intel_crtc->config->has_pch_encoder) {
  4507. /* Note: FDI PLL enabling _must_ be done before we enable the
  4508. * cpu pipes, hence this is separate from all the other fdi/pch
  4509. * enabling. */
  4510. ironlake_fdi_pll_enable(intel_crtc);
  4511. } else {
  4512. assert_fdi_tx_disabled(dev_priv, pipe);
  4513. assert_fdi_rx_disabled(dev_priv, pipe);
  4514. }
  4515. ironlake_pfit_enable(intel_crtc);
  4516. /*
  4517. * On ILK+ LUT must be loaded before the pipe is running but with
  4518. * clocks enabled
  4519. */
  4520. intel_color_load_luts(&pipe_config->base);
  4521. if (dev_priv->display.initial_watermarks != NULL)
  4522. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4523. intel_enable_pipe(intel_crtc);
  4524. if (intel_crtc->config->has_pch_encoder)
  4525. ironlake_pch_enable(pipe_config);
  4526. assert_vblank_disabled(crtc);
  4527. drm_crtc_vblank_on(crtc);
  4528. intel_encoders_enable(crtc, pipe_config, old_state);
  4529. if (HAS_PCH_CPT(dev_priv))
  4530. cpt_verify_modeset(dev, intel_crtc->pipe);
  4531. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4532. if (intel_crtc->config->has_pch_encoder)
  4533. intel_wait_for_vblank(dev_priv, pipe);
  4534. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4535. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4536. }
  4537. /* IPS only exists on ULT machines and is tied to pipe A. */
  4538. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4539. {
  4540. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4541. }
  4542. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4543. struct drm_atomic_state *old_state)
  4544. {
  4545. struct drm_crtc *crtc = pipe_config->base.crtc;
  4546. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4547. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4548. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4549. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4550. struct intel_atomic_state *old_intel_state =
  4551. to_intel_atomic_state(old_state);
  4552. if (WARN_ON(intel_crtc->active))
  4553. return;
  4554. if (intel_crtc->config->has_pch_encoder)
  4555. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4556. false);
  4557. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4558. if (intel_crtc->config->shared_dpll)
  4559. intel_enable_shared_dpll(intel_crtc);
  4560. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4561. intel_dp_set_m_n(intel_crtc, M1_N1);
  4562. if (!transcoder_is_dsi(cpu_transcoder))
  4563. intel_set_pipe_timings(intel_crtc);
  4564. intel_set_pipe_src_size(intel_crtc);
  4565. if (cpu_transcoder != TRANSCODER_EDP &&
  4566. !transcoder_is_dsi(cpu_transcoder)) {
  4567. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4568. intel_crtc->config->pixel_multiplier - 1);
  4569. }
  4570. if (intel_crtc->config->has_pch_encoder) {
  4571. intel_cpu_transcoder_set_m_n(intel_crtc,
  4572. &intel_crtc->config->fdi_m_n, NULL);
  4573. }
  4574. if (!transcoder_is_dsi(cpu_transcoder))
  4575. haswell_set_pipeconf(crtc);
  4576. haswell_set_pipemisc(crtc);
  4577. intel_color_set_csc(&pipe_config->base);
  4578. intel_crtc->active = true;
  4579. if (intel_crtc->config->has_pch_encoder)
  4580. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4581. else
  4582. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4583. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4584. if (intel_crtc->config->has_pch_encoder)
  4585. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4586. if (!transcoder_is_dsi(cpu_transcoder))
  4587. intel_ddi_enable_pipe_clock(pipe_config);
  4588. if (INTEL_GEN(dev_priv) >= 9)
  4589. skylake_pfit_enable(intel_crtc);
  4590. else
  4591. ironlake_pfit_enable(intel_crtc);
  4592. /*
  4593. * On ILK+ LUT must be loaded before the pipe is running but with
  4594. * clocks enabled
  4595. */
  4596. intel_color_load_luts(&pipe_config->base);
  4597. intel_ddi_set_pipe_settings(pipe_config);
  4598. if (!transcoder_is_dsi(cpu_transcoder))
  4599. intel_ddi_enable_transcoder_func(pipe_config);
  4600. if (dev_priv->display.initial_watermarks != NULL)
  4601. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4602. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4603. if (!transcoder_is_dsi(cpu_transcoder))
  4604. intel_enable_pipe(intel_crtc);
  4605. if (intel_crtc->config->has_pch_encoder)
  4606. lpt_pch_enable(pipe_config);
  4607. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4608. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4609. assert_vblank_disabled(crtc);
  4610. drm_crtc_vblank_on(crtc);
  4611. intel_encoders_enable(crtc, pipe_config, old_state);
  4612. if (intel_crtc->config->has_pch_encoder) {
  4613. intel_wait_for_vblank(dev_priv, pipe);
  4614. intel_wait_for_vblank(dev_priv, pipe);
  4615. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4616. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4617. true);
  4618. }
  4619. /* If we change the relative order between pipe/planes enabling, we need
  4620. * to change the workaround. */
  4621. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4622. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4623. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4624. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4625. }
  4626. }
  4627. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4628. {
  4629. struct drm_device *dev = crtc->base.dev;
  4630. struct drm_i915_private *dev_priv = to_i915(dev);
  4631. int pipe = crtc->pipe;
  4632. /* To avoid upsetting the power well on haswell only disable the pfit if
  4633. * it's in use. The hw state code will make sure we get this right. */
  4634. if (force || crtc->config->pch_pfit.enabled) {
  4635. I915_WRITE(PF_CTL(pipe), 0);
  4636. I915_WRITE(PF_WIN_POS(pipe), 0);
  4637. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4638. }
  4639. }
  4640. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4641. struct drm_atomic_state *old_state)
  4642. {
  4643. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4644. struct drm_device *dev = crtc->dev;
  4645. struct drm_i915_private *dev_priv = to_i915(dev);
  4646. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4647. int pipe = intel_crtc->pipe;
  4648. /*
  4649. * Sometimes spurious CPU pipe underruns happen when the
  4650. * pipe is already disabled, but FDI RX/TX is still enabled.
  4651. * Happens at least with VGA+HDMI cloning. Suppress them.
  4652. */
  4653. if (intel_crtc->config->has_pch_encoder) {
  4654. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4655. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4656. }
  4657. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4658. drm_crtc_vblank_off(crtc);
  4659. assert_vblank_disabled(crtc);
  4660. intel_disable_pipe(intel_crtc);
  4661. ironlake_pfit_disable(intel_crtc, false);
  4662. if (intel_crtc->config->has_pch_encoder)
  4663. ironlake_fdi_disable(crtc);
  4664. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4665. if (intel_crtc->config->has_pch_encoder) {
  4666. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4667. if (HAS_PCH_CPT(dev_priv)) {
  4668. i915_reg_t reg;
  4669. u32 temp;
  4670. /* disable TRANS_DP_CTL */
  4671. reg = TRANS_DP_CTL(pipe);
  4672. temp = I915_READ(reg);
  4673. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4674. TRANS_DP_PORT_SEL_MASK);
  4675. temp |= TRANS_DP_PORT_SEL_NONE;
  4676. I915_WRITE(reg, temp);
  4677. /* disable DPLL_SEL */
  4678. temp = I915_READ(PCH_DPLL_SEL);
  4679. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4680. I915_WRITE(PCH_DPLL_SEL, temp);
  4681. }
  4682. ironlake_fdi_pll_disable(intel_crtc);
  4683. }
  4684. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4685. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4686. }
  4687. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4688. struct drm_atomic_state *old_state)
  4689. {
  4690. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4691. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4692. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4693. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4694. if (intel_crtc->config->has_pch_encoder)
  4695. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4696. false);
  4697. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4698. drm_crtc_vblank_off(crtc);
  4699. assert_vblank_disabled(crtc);
  4700. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4701. if (!transcoder_is_dsi(cpu_transcoder))
  4702. intel_disable_pipe(intel_crtc);
  4703. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4704. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4705. if (!transcoder_is_dsi(cpu_transcoder))
  4706. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4707. if (INTEL_GEN(dev_priv) >= 9)
  4708. skylake_scaler_disable(intel_crtc);
  4709. else
  4710. ironlake_pfit_disable(intel_crtc, false);
  4711. if (!transcoder_is_dsi(cpu_transcoder))
  4712. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4713. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4714. if (old_crtc_state->has_pch_encoder)
  4715. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4716. true);
  4717. }
  4718. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4719. {
  4720. struct drm_device *dev = crtc->base.dev;
  4721. struct drm_i915_private *dev_priv = to_i915(dev);
  4722. struct intel_crtc_state *pipe_config = crtc->config;
  4723. if (!pipe_config->gmch_pfit.control)
  4724. return;
  4725. /*
  4726. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4727. * according to register description and PRM.
  4728. */
  4729. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4730. assert_pipe_disabled(dev_priv, crtc->pipe);
  4731. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4732. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4733. /* Border color in case we don't scale up to the full screen. Black by
  4734. * default, change to something else for debugging. */
  4735. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4736. }
  4737. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4738. {
  4739. switch (port) {
  4740. case PORT_A:
  4741. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4742. case PORT_B:
  4743. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4744. case PORT_C:
  4745. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4746. case PORT_D:
  4747. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4748. case PORT_E:
  4749. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4750. default:
  4751. MISSING_CASE(port);
  4752. return POWER_DOMAIN_PORT_OTHER;
  4753. }
  4754. }
  4755. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4756. struct intel_crtc_state *crtc_state)
  4757. {
  4758. struct drm_device *dev = crtc->dev;
  4759. struct drm_i915_private *dev_priv = to_i915(dev);
  4760. struct drm_encoder *encoder;
  4761. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4762. enum pipe pipe = intel_crtc->pipe;
  4763. u64 mask;
  4764. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4765. if (!crtc_state->base.active)
  4766. return 0;
  4767. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4768. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4769. if (crtc_state->pch_pfit.enabled ||
  4770. crtc_state->pch_pfit.force_thru)
  4771. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4772. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4773. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4774. mask |= BIT_ULL(intel_encoder->power_domain);
  4775. }
  4776. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4777. mask |= BIT(POWER_DOMAIN_AUDIO);
  4778. if (crtc_state->shared_dpll)
  4779. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4780. return mask;
  4781. }
  4782. static u64
  4783. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4784. struct intel_crtc_state *crtc_state)
  4785. {
  4786. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4788. enum intel_display_power_domain domain;
  4789. u64 domains, new_domains, old_domains;
  4790. old_domains = intel_crtc->enabled_power_domains;
  4791. intel_crtc->enabled_power_domains = new_domains =
  4792. get_crtc_power_domains(crtc, crtc_state);
  4793. domains = new_domains & ~old_domains;
  4794. for_each_power_domain(domain, domains)
  4795. intel_display_power_get(dev_priv, domain);
  4796. return old_domains & ~new_domains;
  4797. }
  4798. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4799. u64 domains)
  4800. {
  4801. enum intel_display_power_domain domain;
  4802. for_each_power_domain(domain, domains)
  4803. intel_display_power_put(dev_priv, domain);
  4804. }
  4805. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4806. struct drm_atomic_state *old_state)
  4807. {
  4808. struct intel_atomic_state *old_intel_state =
  4809. to_intel_atomic_state(old_state);
  4810. struct drm_crtc *crtc = pipe_config->base.crtc;
  4811. struct drm_device *dev = crtc->dev;
  4812. struct drm_i915_private *dev_priv = to_i915(dev);
  4813. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4814. int pipe = intel_crtc->pipe;
  4815. if (WARN_ON(intel_crtc->active))
  4816. return;
  4817. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4818. intel_dp_set_m_n(intel_crtc, M1_N1);
  4819. intel_set_pipe_timings(intel_crtc);
  4820. intel_set_pipe_src_size(intel_crtc);
  4821. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4822. struct drm_i915_private *dev_priv = to_i915(dev);
  4823. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4824. I915_WRITE(CHV_CANVAS(pipe), 0);
  4825. }
  4826. i9xx_set_pipeconf(intel_crtc);
  4827. intel_crtc->active = true;
  4828. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4829. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4830. if (IS_CHERRYVIEW(dev_priv)) {
  4831. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4832. chv_enable_pll(intel_crtc, intel_crtc->config);
  4833. } else {
  4834. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4835. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4836. }
  4837. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4838. i9xx_pfit_enable(intel_crtc);
  4839. intel_color_load_luts(&pipe_config->base);
  4840. dev_priv->display.initial_watermarks(old_intel_state,
  4841. pipe_config);
  4842. intel_enable_pipe(intel_crtc);
  4843. assert_vblank_disabled(crtc);
  4844. drm_crtc_vblank_on(crtc);
  4845. intel_encoders_enable(crtc, pipe_config, old_state);
  4846. }
  4847. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4848. {
  4849. struct drm_device *dev = crtc->base.dev;
  4850. struct drm_i915_private *dev_priv = to_i915(dev);
  4851. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4852. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4853. }
  4854. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4855. struct drm_atomic_state *old_state)
  4856. {
  4857. struct drm_crtc *crtc = pipe_config->base.crtc;
  4858. struct drm_device *dev = crtc->dev;
  4859. struct drm_i915_private *dev_priv = to_i915(dev);
  4860. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4861. enum pipe pipe = intel_crtc->pipe;
  4862. if (WARN_ON(intel_crtc->active))
  4863. return;
  4864. i9xx_set_pll_dividers(intel_crtc);
  4865. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4866. intel_dp_set_m_n(intel_crtc, M1_N1);
  4867. intel_set_pipe_timings(intel_crtc);
  4868. intel_set_pipe_src_size(intel_crtc);
  4869. i9xx_set_pipeconf(intel_crtc);
  4870. intel_crtc->active = true;
  4871. if (!IS_GEN2(dev_priv))
  4872. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4873. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4874. i9xx_enable_pll(intel_crtc);
  4875. i9xx_pfit_enable(intel_crtc);
  4876. intel_color_load_luts(&pipe_config->base);
  4877. intel_update_watermarks(intel_crtc);
  4878. intel_enable_pipe(intel_crtc);
  4879. assert_vblank_disabled(crtc);
  4880. drm_crtc_vblank_on(crtc);
  4881. intel_encoders_enable(crtc, pipe_config, old_state);
  4882. }
  4883. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4884. {
  4885. struct drm_device *dev = crtc->base.dev;
  4886. struct drm_i915_private *dev_priv = to_i915(dev);
  4887. if (!crtc->config->gmch_pfit.control)
  4888. return;
  4889. assert_pipe_disabled(dev_priv, crtc->pipe);
  4890. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4891. I915_READ(PFIT_CONTROL));
  4892. I915_WRITE(PFIT_CONTROL, 0);
  4893. }
  4894. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4895. struct drm_atomic_state *old_state)
  4896. {
  4897. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4898. struct drm_device *dev = crtc->dev;
  4899. struct drm_i915_private *dev_priv = to_i915(dev);
  4900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4901. int pipe = intel_crtc->pipe;
  4902. /*
  4903. * On gen2 planes are double buffered but the pipe isn't, so we must
  4904. * wait for planes to fully turn off before disabling the pipe.
  4905. */
  4906. if (IS_GEN2(dev_priv))
  4907. intel_wait_for_vblank(dev_priv, pipe);
  4908. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4909. drm_crtc_vblank_off(crtc);
  4910. assert_vblank_disabled(crtc);
  4911. intel_disable_pipe(intel_crtc);
  4912. i9xx_pfit_disable(intel_crtc);
  4913. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4914. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4915. if (IS_CHERRYVIEW(dev_priv))
  4916. chv_disable_pll(dev_priv, pipe);
  4917. else if (IS_VALLEYVIEW(dev_priv))
  4918. vlv_disable_pll(dev_priv, pipe);
  4919. else
  4920. i9xx_disable_pll(intel_crtc);
  4921. }
  4922. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4923. if (!IS_GEN2(dev_priv))
  4924. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4925. if (!dev_priv->display.initial_watermarks)
  4926. intel_update_watermarks(intel_crtc);
  4927. }
  4928. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  4929. {
  4930. struct intel_encoder *encoder;
  4931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4932. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4933. enum intel_display_power_domain domain;
  4934. u64 domains;
  4935. struct drm_atomic_state *state;
  4936. struct intel_crtc_state *crtc_state;
  4937. int ret;
  4938. if (!intel_crtc->active)
  4939. return;
  4940. if (crtc->primary->state->visible) {
  4941. WARN_ON(intel_crtc->flip_work);
  4942. intel_pre_disable_primary_noatomic(crtc);
  4943. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4944. crtc->primary->state->visible = false;
  4945. }
  4946. state = drm_atomic_state_alloc(crtc->dev);
  4947. if (!state) {
  4948. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4949. crtc->base.id, crtc->name);
  4950. return;
  4951. }
  4952. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  4953. /* Everything's already locked, -EDEADLK can't happen. */
  4954. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4955. ret = drm_atomic_add_affected_connectors(state, crtc);
  4956. WARN_ON(IS_ERR(crtc_state) || ret);
  4957. dev_priv->display.crtc_disable(crtc_state, state);
  4958. drm_atomic_state_put(state);
  4959. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4960. crtc->base.id, crtc->name);
  4961. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4962. crtc->state->active = false;
  4963. intel_crtc->active = false;
  4964. crtc->enabled = false;
  4965. crtc->state->connector_mask = 0;
  4966. crtc->state->encoder_mask = 0;
  4967. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4968. encoder->base.crtc = NULL;
  4969. intel_fbc_disable(intel_crtc);
  4970. intel_update_watermarks(intel_crtc);
  4971. intel_disable_shared_dpll(intel_crtc);
  4972. domains = intel_crtc->enabled_power_domains;
  4973. for_each_power_domain(domain, domains)
  4974. intel_display_power_put(dev_priv, domain);
  4975. intel_crtc->enabled_power_domains = 0;
  4976. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  4977. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  4978. }
  4979. /*
  4980. * turn all crtc's off, but do not adjust state
  4981. * This has to be paired with a call to intel_modeset_setup_hw_state.
  4982. */
  4983. int intel_display_suspend(struct drm_device *dev)
  4984. {
  4985. struct drm_i915_private *dev_priv = to_i915(dev);
  4986. struct drm_atomic_state *state;
  4987. int ret;
  4988. state = drm_atomic_helper_suspend(dev);
  4989. ret = PTR_ERR_OR_ZERO(state);
  4990. if (ret)
  4991. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  4992. else
  4993. dev_priv->modeset_restore_state = state;
  4994. return ret;
  4995. }
  4996. void intel_encoder_destroy(struct drm_encoder *encoder)
  4997. {
  4998. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4999. drm_encoder_cleanup(encoder);
  5000. kfree(intel_encoder);
  5001. }
  5002. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5003. * internal consistency). */
  5004. static void intel_connector_verify_state(struct intel_connector *connector)
  5005. {
  5006. struct drm_crtc *crtc = connector->base.state->crtc;
  5007. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5008. connector->base.base.id,
  5009. connector->base.name);
  5010. if (connector->get_hw_state(connector)) {
  5011. struct intel_encoder *encoder = connector->encoder;
  5012. struct drm_connector_state *conn_state = connector->base.state;
  5013. I915_STATE_WARN(!crtc,
  5014. "connector enabled without attached crtc\n");
  5015. if (!crtc)
  5016. return;
  5017. I915_STATE_WARN(!crtc->state->active,
  5018. "connector is active, but attached crtc isn't\n");
  5019. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5020. return;
  5021. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5022. "atomic encoder doesn't match attached encoder\n");
  5023. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5024. "attached encoder crtc differs from connector crtc\n");
  5025. } else {
  5026. I915_STATE_WARN(crtc && crtc->state->active,
  5027. "attached crtc is active, but connector isn't\n");
  5028. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5029. "best encoder set without crtc!\n");
  5030. }
  5031. }
  5032. int intel_connector_init(struct intel_connector *connector)
  5033. {
  5034. drm_atomic_helper_connector_reset(&connector->base);
  5035. if (!connector->base.state)
  5036. return -ENOMEM;
  5037. return 0;
  5038. }
  5039. struct intel_connector *intel_connector_alloc(void)
  5040. {
  5041. struct intel_connector *connector;
  5042. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5043. if (!connector)
  5044. return NULL;
  5045. if (intel_connector_init(connector) < 0) {
  5046. kfree(connector);
  5047. return NULL;
  5048. }
  5049. return connector;
  5050. }
  5051. /* Simple connector->get_hw_state implementation for encoders that support only
  5052. * one connector and no cloning and hence the encoder state determines the state
  5053. * of the connector. */
  5054. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5055. {
  5056. enum pipe pipe = 0;
  5057. struct intel_encoder *encoder = connector->encoder;
  5058. return encoder->get_hw_state(encoder, &pipe);
  5059. }
  5060. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5061. {
  5062. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5063. return crtc_state->fdi_lanes;
  5064. return 0;
  5065. }
  5066. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5067. struct intel_crtc_state *pipe_config)
  5068. {
  5069. struct drm_i915_private *dev_priv = to_i915(dev);
  5070. struct drm_atomic_state *state = pipe_config->base.state;
  5071. struct intel_crtc *other_crtc;
  5072. struct intel_crtc_state *other_crtc_state;
  5073. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5074. pipe_name(pipe), pipe_config->fdi_lanes);
  5075. if (pipe_config->fdi_lanes > 4) {
  5076. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5077. pipe_name(pipe), pipe_config->fdi_lanes);
  5078. return -EINVAL;
  5079. }
  5080. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5081. if (pipe_config->fdi_lanes > 2) {
  5082. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5083. pipe_config->fdi_lanes);
  5084. return -EINVAL;
  5085. } else {
  5086. return 0;
  5087. }
  5088. }
  5089. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5090. return 0;
  5091. /* Ivybridge 3 pipe is really complicated */
  5092. switch (pipe) {
  5093. case PIPE_A:
  5094. return 0;
  5095. case PIPE_B:
  5096. if (pipe_config->fdi_lanes <= 2)
  5097. return 0;
  5098. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5099. other_crtc_state =
  5100. intel_atomic_get_crtc_state(state, other_crtc);
  5101. if (IS_ERR(other_crtc_state))
  5102. return PTR_ERR(other_crtc_state);
  5103. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5104. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5105. pipe_name(pipe), pipe_config->fdi_lanes);
  5106. return -EINVAL;
  5107. }
  5108. return 0;
  5109. case PIPE_C:
  5110. if (pipe_config->fdi_lanes > 2) {
  5111. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5112. pipe_name(pipe), pipe_config->fdi_lanes);
  5113. return -EINVAL;
  5114. }
  5115. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5116. other_crtc_state =
  5117. intel_atomic_get_crtc_state(state, other_crtc);
  5118. if (IS_ERR(other_crtc_state))
  5119. return PTR_ERR(other_crtc_state);
  5120. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5121. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5122. return -EINVAL;
  5123. }
  5124. return 0;
  5125. default:
  5126. BUG();
  5127. }
  5128. }
  5129. #define RETRY 1
  5130. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5131. struct intel_crtc_state *pipe_config)
  5132. {
  5133. struct drm_device *dev = intel_crtc->base.dev;
  5134. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5135. int lane, link_bw, fdi_dotclock, ret;
  5136. bool needs_recompute = false;
  5137. retry:
  5138. /* FDI is a binary signal running at ~2.7GHz, encoding
  5139. * each output octet as 10 bits. The actual frequency
  5140. * is stored as a divider into a 100MHz clock, and the
  5141. * mode pixel clock is stored in units of 1KHz.
  5142. * Hence the bw of each lane in terms of the mode signal
  5143. * is:
  5144. */
  5145. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5146. fdi_dotclock = adjusted_mode->crtc_clock;
  5147. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5148. pipe_config->pipe_bpp);
  5149. pipe_config->fdi_lanes = lane;
  5150. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5151. link_bw, &pipe_config->fdi_m_n);
  5152. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5153. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5154. pipe_config->pipe_bpp -= 2*3;
  5155. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5156. pipe_config->pipe_bpp);
  5157. needs_recompute = true;
  5158. pipe_config->bw_constrained = true;
  5159. goto retry;
  5160. }
  5161. if (needs_recompute)
  5162. return RETRY;
  5163. return ret;
  5164. }
  5165. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5166. struct intel_crtc_state *pipe_config)
  5167. {
  5168. if (pipe_config->pipe_bpp > 24)
  5169. return false;
  5170. /* HSW can handle pixel rate up to cdclk? */
  5171. if (IS_HASWELL(dev_priv))
  5172. return true;
  5173. /*
  5174. * We compare against max which means we must take
  5175. * the increased cdclk requirement into account when
  5176. * calculating the new cdclk.
  5177. *
  5178. * Should measure whether using a lower cdclk w/o IPS
  5179. */
  5180. return pipe_config->pixel_rate <=
  5181. dev_priv->max_cdclk_freq * 95 / 100;
  5182. }
  5183. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5184. struct intel_crtc_state *pipe_config)
  5185. {
  5186. struct drm_device *dev = crtc->base.dev;
  5187. struct drm_i915_private *dev_priv = to_i915(dev);
  5188. pipe_config->ips_enabled = i915.enable_ips &&
  5189. hsw_crtc_supports_ips(crtc) &&
  5190. pipe_config_supports_ips(dev_priv, pipe_config);
  5191. }
  5192. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5193. {
  5194. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5195. /* GDG double wide on either pipe, otherwise pipe A only */
  5196. return INTEL_INFO(dev_priv)->gen < 4 &&
  5197. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5198. }
  5199. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5200. {
  5201. uint32_t pixel_rate;
  5202. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5203. /*
  5204. * We only use IF-ID interlacing. If we ever use
  5205. * PF-ID we'll need to adjust the pixel_rate here.
  5206. */
  5207. if (pipe_config->pch_pfit.enabled) {
  5208. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5209. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5210. pipe_w = pipe_config->pipe_src_w;
  5211. pipe_h = pipe_config->pipe_src_h;
  5212. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5213. pfit_h = pfit_size & 0xFFFF;
  5214. if (pipe_w < pfit_w)
  5215. pipe_w = pfit_w;
  5216. if (pipe_h < pfit_h)
  5217. pipe_h = pfit_h;
  5218. if (WARN_ON(!pfit_w || !pfit_h))
  5219. return pixel_rate;
  5220. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5221. pfit_w * pfit_h);
  5222. }
  5223. return pixel_rate;
  5224. }
  5225. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5226. {
  5227. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5228. if (HAS_GMCH_DISPLAY(dev_priv))
  5229. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5230. crtc_state->pixel_rate =
  5231. crtc_state->base.adjusted_mode.crtc_clock;
  5232. else
  5233. crtc_state->pixel_rate =
  5234. ilk_pipe_pixel_rate(crtc_state);
  5235. }
  5236. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5237. struct intel_crtc_state *pipe_config)
  5238. {
  5239. struct drm_device *dev = crtc->base.dev;
  5240. struct drm_i915_private *dev_priv = to_i915(dev);
  5241. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5242. int clock_limit = dev_priv->max_dotclk_freq;
  5243. if (INTEL_GEN(dev_priv) < 4) {
  5244. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5245. /*
  5246. * Enable double wide mode when the dot clock
  5247. * is > 90% of the (display) core speed.
  5248. */
  5249. if (intel_crtc_supports_double_wide(crtc) &&
  5250. adjusted_mode->crtc_clock > clock_limit) {
  5251. clock_limit = dev_priv->max_dotclk_freq;
  5252. pipe_config->double_wide = true;
  5253. }
  5254. }
  5255. if (adjusted_mode->crtc_clock > clock_limit) {
  5256. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5257. adjusted_mode->crtc_clock, clock_limit,
  5258. yesno(pipe_config->double_wide));
  5259. return -EINVAL;
  5260. }
  5261. /*
  5262. * Pipe horizontal size must be even in:
  5263. * - DVO ganged mode
  5264. * - LVDS dual channel mode
  5265. * - Double wide pipe
  5266. */
  5267. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5268. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5269. pipe_config->pipe_src_w &= ~1;
  5270. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5271. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5272. */
  5273. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5274. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5275. return -EINVAL;
  5276. intel_crtc_compute_pixel_rate(pipe_config);
  5277. if (HAS_IPS(dev_priv))
  5278. hsw_compute_ips_config(crtc, pipe_config);
  5279. if (pipe_config->has_pch_encoder)
  5280. return ironlake_fdi_compute_config(crtc, pipe_config);
  5281. return 0;
  5282. }
  5283. static void
  5284. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5285. {
  5286. while (*num > DATA_LINK_M_N_MASK ||
  5287. *den > DATA_LINK_M_N_MASK) {
  5288. *num >>= 1;
  5289. *den >>= 1;
  5290. }
  5291. }
  5292. static void compute_m_n(unsigned int m, unsigned int n,
  5293. uint32_t *ret_m, uint32_t *ret_n)
  5294. {
  5295. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5296. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5297. intel_reduce_m_n_ratio(ret_m, ret_n);
  5298. }
  5299. void
  5300. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5301. int pixel_clock, int link_clock,
  5302. struct intel_link_m_n *m_n)
  5303. {
  5304. m_n->tu = 64;
  5305. compute_m_n(bits_per_pixel * pixel_clock,
  5306. link_clock * nlanes * 8,
  5307. &m_n->gmch_m, &m_n->gmch_n);
  5308. compute_m_n(pixel_clock, link_clock,
  5309. &m_n->link_m, &m_n->link_n);
  5310. }
  5311. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5312. {
  5313. if (i915.panel_use_ssc >= 0)
  5314. return i915.panel_use_ssc != 0;
  5315. return dev_priv->vbt.lvds_use_ssc
  5316. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5317. }
  5318. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5319. {
  5320. return (1 << dpll->n) << 16 | dpll->m2;
  5321. }
  5322. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5323. {
  5324. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5325. }
  5326. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5327. struct intel_crtc_state *crtc_state,
  5328. struct dpll *reduced_clock)
  5329. {
  5330. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5331. u32 fp, fp2 = 0;
  5332. if (IS_PINEVIEW(dev_priv)) {
  5333. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5334. if (reduced_clock)
  5335. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5336. } else {
  5337. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5338. if (reduced_clock)
  5339. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5340. }
  5341. crtc_state->dpll_hw_state.fp0 = fp;
  5342. crtc->lowfreq_avail = false;
  5343. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5344. reduced_clock) {
  5345. crtc_state->dpll_hw_state.fp1 = fp2;
  5346. crtc->lowfreq_avail = true;
  5347. } else {
  5348. crtc_state->dpll_hw_state.fp1 = fp;
  5349. }
  5350. }
  5351. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5352. pipe)
  5353. {
  5354. u32 reg_val;
  5355. /*
  5356. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5357. * and set it to a reasonable value instead.
  5358. */
  5359. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5360. reg_val &= 0xffffff00;
  5361. reg_val |= 0x00000030;
  5362. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5363. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5364. reg_val &= 0x8cffffff;
  5365. reg_val = 0x8c000000;
  5366. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5367. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5368. reg_val &= 0xffffff00;
  5369. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5370. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5371. reg_val &= 0x00ffffff;
  5372. reg_val |= 0xb0000000;
  5373. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5374. }
  5375. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5376. struct intel_link_m_n *m_n)
  5377. {
  5378. struct drm_device *dev = crtc->base.dev;
  5379. struct drm_i915_private *dev_priv = to_i915(dev);
  5380. int pipe = crtc->pipe;
  5381. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5382. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5383. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5384. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5385. }
  5386. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5387. struct intel_link_m_n *m_n,
  5388. struct intel_link_m_n *m2_n2)
  5389. {
  5390. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5391. int pipe = crtc->pipe;
  5392. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5393. if (INTEL_GEN(dev_priv) >= 5) {
  5394. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5395. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5396. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5397. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5398. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5399. * for gen < 8) and if DRRS is supported (to make sure the
  5400. * registers are not unnecessarily accessed).
  5401. */
  5402. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5403. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5404. I915_WRITE(PIPE_DATA_M2(transcoder),
  5405. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5406. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5407. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5408. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5409. }
  5410. } else {
  5411. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5412. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5413. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5414. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5415. }
  5416. }
  5417. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5418. {
  5419. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5420. if (m_n == M1_N1) {
  5421. dp_m_n = &crtc->config->dp_m_n;
  5422. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5423. } else if (m_n == M2_N2) {
  5424. /*
  5425. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5426. * needs to be programmed into M1_N1.
  5427. */
  5428. dp_m_n = &crtc->config->dp_m2_n2;
  5429. } else {
  5430. DRM_ERROR("Unsupported divider value\n");
  5431. return;
  5432. }
  5433. if (crtc->config->has_pch_encoder)
  5434. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5435. else
  5436. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5437. }
  5438. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5439. struct intel_crtc_state *pipe_config)
  5440. {
  5441. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5442. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5443. if (crtc->pipe != PIPE_A)
  5444. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5445. /* DPLL not used with DSI, but still need the rest set up */
  5446. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5447. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5448. DPLL_EXT_BUFFER_ENABLE_VLV;
  5449. pipe_config->dpll_hw_state.dpll_md =
  5450. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5451. }
  5452. static void chv_compute_dpll(struct intel_crtc *crtc,
  5453. struct intel_crtc_state *pipe_config)
  5454. {
  5455. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5456. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5457. if (crtc->pipe != PIPE_A)
  5458. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5459. /* DPLL not used with DSI, but still need the rest set up */
  5460. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5461. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5462. pipe_config->dpll_hw_state.dpll_md =
  5463. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5464. }
  5465. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5466. const struct intel_crtc_state *pipe_config)
  5467. {
  5468. struct drm_device *dev = crtc->base.dev;
  5469. struct drm_i915_private *dev_priv = to_i915(dev);
  5470. enum pipe pipe = crtc->pipe;
  5471. u32 mdiv;
  5472. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5473. u32 coreclk, reg_val;
  5474. /* Enable Refclk */
  5475. I915_WRITE(DPLL(pipe),
  5476. pipe_config->dpll_hw_state.dpll &
  5477. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5478. /* No need to actually set up the DPLL with DSI */
  5479. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5480. return;
  5481. mutex_lock(&dev_priv->sb_lock);
  5482. bestn = pipe_config->dpll.n;
  5483. bestm1 = pipe_config->dpll.m1;
  5484. bestm2 = pipe_config->dpll.m2;
  5485. bestp1 = pipe_config->dpll.p1;
  5486. bestp2 = pipe_config->dpll.p2;
  5487. /* See eDP HDMI DPIO driver vbios notes doc */
  5488. /* PLL B needs special handling */
  5489. if (pipe == PIPE_B)
  5490. vlv_pllb_recal_opamp(dev_priv, pipe);
  5491. /* Set up Tx target for periodic Rcomp update */
  5492. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5493. /* Disable target IRef on PLL */
  5494. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5495. reg_val &= 0x00ffffff;
  5496. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5497. /* Disable fast lock */
  5498. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5499. /* Set idtafcrecal before PLL is enabled */
  5500. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5501. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5502. mdiv |= ((bestn << DPIO_N_SHIFT));
  5503. mdiv |= (1 << DPIO_K_SHIFT);
  5504. /*
  5505. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5506. * but we don't support that).
  5507. * Note: don't use the DAC post divider as it seems unstable.
  5508. */
  5509. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5510. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5511. mdiv |= DPIO_ENABLE_CALIBRATION;
  5512. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5513. /* Set HBR and RBR LPF coefficients */
  5514. if (pipe_config->port_clock == 162000 ||
  5515. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5516. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5517. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5518. 0x009f0003);
  5519. else
  5520. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5521. 0x00d0000f);
  5522. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5523. /* Use SSC source */
  5524. if (pipe == PIPE_A)
  5525. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5526. 0x0df40000);
  5527. else
  5528. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5529. 0x0df70000);
  5530. } else { /* HDMI or VGA */
  5531. /* Use bend source */
  5532. if (pipe == PIPE_A)
  5533. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5534. 0x0df70000);
  5535. else
  5536. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5537. 0x0df40000);
  5538. }
  5539. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5540. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5541. if (intel_crtc_has_dp_encoder(crtc->config))
  5542. coreclk |= 0x01000000;
  5543. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5544. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5545. mutex_unlock(&dev_priv->sb_lock);
  5546. }
  5547. static void chv_prepare_pll(struct intel_crtc *crtc,
  5548. const struct intel_crtc_state *pipe_config)
  5549. {
  5550. struct drm_device *dev = crtc->base.dev;
  5551. struct drm_i915_private *dev_priv = to_i915(dev);
  5552. enum pipe pipe = crtc->pipe;
  5553. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5554. u32 loopfilter, tribuf_calcntr;
  5555. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5556. u32 dpio_val;
  5557. int vco;
  5558. /* Enable Refclk and SSC */
  5559. I915_WRITE(DPLL(pipe),
  5560. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5561. /* No need to actually set up the DPLL with DSI */
  5562. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5563. return;
  5564. bestn = pipe_config->dpll.n;
  5565. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5566. bestm1 = pipe_config->dpll.m1;
  5567. bestm2 = pipe_config->dpll.m2 >> 22;
  5568. bestp1 = pipe_config->dpll.p1;
  5569. bestp2 = pipe_config->dpll.p2;
  5570. vco = pipe_config->dpll.vco;
  5571. dpio_val = 0;
  5572. loopfilter = 0;
  5573. mutex_lock(&dev_priv->sb_lock);
  5574. /* p1 and p2 divider */
  5575. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5576. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5577. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5578. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5579. 1 << DPIO_CHV_K_DIV_SHIFT);
  5580. /* Feedback post-divider - m2 */
  5581. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5582. /* Feedback refclk divider - n and m1 */
  5583. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5584. DPIO_CHV_M1_DIV_BY_2 |
  5585. 1 << DPIO_CHV_N_DIV_SHIFT);
  5586. /* M2 fraction division */
  5587. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5588. /* M2 fraction division enable */
  5589. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5590. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5591. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5592. if (bestm2_frac)
  5593. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5594. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5595. /* Program digital lock detect threshold */
  5596. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5597. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5598. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5599. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5600. if (!bestm2_frac)
  5601. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5602. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5603. /* Loop filter */
  5604. if (vco == 5400000) {
  5605. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5606. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5607. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5608. tribuf_calcntr = 0x9;
  5609. } else if (vco <= 6200000) {
  5610. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5611. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5612. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5613. tribuf_calcntr = 0x9;
  5614. } else if (vco <= 6480000) {
  5615. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5616. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5617. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5618. tribuf_calcntr = 0x8;
  5619. } else {
  5620. /* Not supported. Apply the same limits as in the max case */
  5621. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5622. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5623. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5624. tribuf_calcntr = 0;
  5625. }
  5626. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5627. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5628. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5629. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5630. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5631. /* AFC Recal */
  5632. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5633. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5634. DPIO_AFC_RECAL);
  5635. mutex_unlock(&dev_priv->sb_lock);
  5636. }
  5637. /**
  5638. * vlv_force_pll_on - forcibly enable just the PLL
  5639. * @dev_priv: i915 private structure
  5640. * @pipe: pipe PLL to enable
  5641. * @dpll: PLL configuration
  5642. *
  5643. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5644. * in cases where we need the PLL enabled even when @pipe is not going to
  5645. * be enabled.
  5646. */
  5647. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5648. const struct dpll *dpll)
  5649. {
  5650. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5651. struct intel_crtc_state *pipe_config;
  5652. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5653. if (!pipe_config)
  5654. return -ENOMEM;
  5655. pipe_config->base.crtc = &crtc->base;
  5656. pipe_config->pixel_multiplier = 1;
  5657. pipe_config->dpll = *dpll;
  5658. if (IS_CHERRYVIEW(dev_priv)) {
  5659. chv_compute_dpll(crtc, pipe_config);
  5660. chv_prepare_pll(crtc, pipe_config);
  5661. chv_enable_pll(crtc, pipe_config);
  5662. } else {
  5663. vlv_compute_dpll(crtc, pipe_config);
  5664. vlv_prepare_pll(crtc, pipe_config);
  5665. vlv_enable_pll(crtc, pipe_config);
  5666. }
  5667. kfree(pipe_config);
  5668. return 0;
  5669. }
  5670. /**
  5671. * vlv_force_pll_off - forcibly disable just the PLL
  5672. * @dev_priv: i915 private structure
  5673. * @pipe: pipe PLL to disable
  5674. *
  5675. * Disable the PLL for @pipe. To be used in cases where we need
  5676. * the PLL enabled even when @pipe is not going to be enabled.
  5677. */
  5678. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5679. {
  5680. if (IS_CHERRYVIEW(dev_priv))
  5681. chv_disable_pll(dev_priv, pipe);
  5682. else
  5683. vlv_disable_pll(dev_priv, pipe);
  5684. }
  5685. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5686. struct intel_crtc_state *crtc_state,
  5687. struct dpll *reduced_clock)
  5688. {
  5689. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5690. u32 dpll;
  5691. struct dpll *clock = &crtc_state->dpll;
  5692. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5693. dpll = DPLL_VGA_MODE_DIS;
  5694. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5695. dpll |= DPLLB_MODE_LVDS;
  5696. else
  5697. dpll |= DPLLB_MODE_DAC_SERIAL;
  5698. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5699. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5700. dpll |= (crtc_state->pixel_multiplier - 1)
  5701. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5702. }
  5703. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5704. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5705. dpll |= DPLL_SDVO_HIGH_SPEED;
  5706. if (intel_crtc_has_dp_encoder(crtc_state))
  5707. dpll |= DPLL_SDVO_HIGH_SPEED;
  5708. /* compute bitmask from p1 value */
  5709. if (IS_PINEVIEW(dev_priv))
  5710. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5711. else {
  5712. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5713. if (IS_G4X(dev_priv) && reduced_clock)
  5714. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5715. }
  5716. switch (clock->p2) {
  5717. case 5:
  5718. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5719. break;
  5720. case 7:
  5721. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5722. break;
  5723. case 10:
  5724. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5725. break;
  5726. case 14:
  5727. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5728. break;
  5729. }
  5730. if (INTEL_GEN(dev_priv) >= 4)
  5731. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5732. if (crtc_state->sdvo_tv_clock)
  5733. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5734. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5735. intel_panel_use_ssc(dev_priv))
  5736. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5737. else
  5738. dpll |= PLL_REF_INPUT_DREFCLK;
  5739. dpll |= DPLL_VCO_ENABLE;
  5740. crtc_state->dpll_hw_state.dpll = dpll;
  5741. if (INTEL_GEN(dev_priv) >= 4) {
  5742. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5743. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5744. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5745. }
  5746. }
  5747. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5748. struct intel_crtc_state *crtc_state,
  5749. struct dpll *reduced_clock)
  5750. {
  5751. struct drm_device *dev = crtc->base.dev;
  5752. struct drm_i915_private *dev_priv = to_i915(dev);
  5753. u32 dpll;
  5754. struct dpll *clock = &crtc_state->dpll;
  5755. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5756. dpll = DPLL_VGA_MODE_DIS;
  5757. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5758. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5759. } else {
  5760. if (clock->p1 == 2)
  5761. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5762. else
  5763. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5764. if (clock->p2 == 4)
  5765. dpll |= PLL_P2_DIVIDE_BY_4;
  5766. }
  5767. if (!IS_I830(dev_priv) &&
  5768. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5769. dpll |= DPLL_DVO_2X_MODE;
  5770. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5771. intel_panel_use_ssc(dev_priv))
  5772. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5773. else
  5774. dpll |= PLL_REF_INPUT_DREFCLK;
  5775. dpll |= DPLL_VCO_ENABLE;
  5776. crtc_state->dpll_hw_state.dpll = dpll;
  5777. }
  5778. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5779. {
  5780. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5781. enum pipe pipe = intel_crtc->pipe;
  5782. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5783. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5784. uint32_t crtc_vtotal, crtc_vblank_end;
  5785. int vsyncshift = 0;
  5786. /* We need to be careful not to changed the adjusted mode, for otherwise
  5787. * the hw state checker will get angry at the mismatch. */
  5788. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5789. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5790. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5791. /* the chip adds 2 halflines automatically */
  5792. crtc_vtotal -= 1;
  5793. crtc_vblank_end -= 1;
  5794. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5795. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5796. else
  5797. vsyncshift = adjusted_mode->crtc_hsync_start -
  5798. adjusted_mode->crtc_htotal / 2;
  5799. if (vsyncshift < 0)
  5800. vsyncshift += adjusted_mode->crtc_htotal;
  5801. }
  5802. if (INTEL_GEN(dev_priv) > 3)
  5803. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5804. I915_WRITE(HTOTAL(cpu_transcoder),
  5805. (adjusted_mode->crtc_hdisplay - 1) |
  5806. ((adjusted_mode->crtc_htotal - 1) << 16));
  5807. I915_WRITE(HBLANK(cpu_transcoder),
  5808. (adjusted_mode->crtc_hblank_start - 1) |
  5809. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5810. I915_WRITE(HSYNC(cpu_transcoder),
  5811. (adjusted_mode->crtc_hsync_start - 1) |
  5812. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5813. I915_WRITE(VTOTAL(cpu_transcoder),
  5814. (adjusted_mode->crtc_vdisplay - 1) |
  5815. ((crtc_vtotal - 1) << 16));
  5816. I915_WRITE(VBLANK(cpu_transcoder),
  5817. (adjusted_mode->crtc_vblank_start - 1) |
  5818. ((crtc_vblank_end - 1) << 16));
  5819. I915_WRITE(VSYNC(cpu_transcoder),
  5820. (adjusted_mode->crtc_vsync_start - 1) |
  5821. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5822. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5823. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5824. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5825. * bits. */
  5826. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5827. (pipe == PIPE_B || pipe == PIPE_C))
  5828. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5829. }
  5830. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5831. {
  5832. struct drm_device *dev = intel_crtc->base.dev;
  5833. struct drm_i915_private *dev_priv = to_i915(dev);
  5834. enum pipe pipe = intel_crtc->pipe;
  5835. /* pipesrc controls the size that is scaled from, which should
  5836. * always be the user's requested size.
  5837. */
  5838. I915_WRITE(PIPESRC(pipe),
  5839. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5840. (intel_crtc->config->pipe_src_h - 1));
  5841. }
  5842. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5843. struct intel_crtc_state *pipe_config)
  5844. {
  5845. struct drm_device *dev = crtc->base.dev;
  5846. struct drm_i915_private *dev_priv = to_i915(dev);
  5847. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5848. uint32_t tmp;
  5849. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5850. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5851. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5852. tmp = I915_READ(HBLANK(cpu_transcoder));
  5853. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5854. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5855. tmp = I915_READ(HSYNC(cpu_transcoder));
  5856. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5857. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5858. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5859. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5860. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5861. tmp = I915_READ(VBLANK(cpu_transcoder));
  5862. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5863. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5864. tmp = I915_READ(VSYNC(cpu_transcoder));
  5865. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5866. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5867. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5868. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5869. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5870. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5871. }
  5872. }
  5873. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5874. struct intel_crtc_state *pipe_config)
  5875. {
  5876. struct drm_device *dev = crtc->base.dev;
  5877. struct drm_i915_private *dev_priv = to_i915(dev);
  5878. u32 tmp;
  5879. tmp = I915_READ(PIPESRC(crtc->pipe));
  5880. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5881. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5882. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5883. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5884. }
  5885. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5886. struct intel_crtc_state *pipe_config)
  5887. {
  5888. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5889. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5890. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5891. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5892. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5893. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5894. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5895. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5896. mode->flags = pipe_config->base.adjusted_mode.flags;
  5897. mode->type = DRM_MODE_TYPE_DRIVER;
  5898. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5899. mode->hsync = drm_mode_hsync(mode);
  5900. mode->vrefresh = drm_mode_vrefresh(mode);
  5901. drm_mode_set_name(mode);
  5902. }
  5903. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5904. {
  5905. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5906. uint32_t pipeconf;
  5907. pipeconf = 0;
  5908. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5909. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5910. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5911. if (intel_crtc->config->double_wide)
  5912. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5913. /* only g4x and later have fancy bpc/dither controls */
  5914. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5915. IS_CHERRYVIEW(dev_priv)) {
  5916. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5917. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5918. pipeconf |= PIPECONF_DITHER_EN |
  5919. PIPECONF_DITHER_TYPE_SP;
  5920. switch (intel_crtc->config->pipe_bpp) {
  5921. case 18:
  5922. pipeconf |= PIPECONF_6BPC;
  5923. break;
  5924. case 24:
  5925. pipeconf |= PIPECONF_8BPC;
  5926. break;
  5927. case 30:
  5928. pipeconf |= PIPECONF_10BPC;
  5929. break;
  5930. default:
  5931. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5932. BUG();
  5933. }
  5934. }
  5935. if (HAS_PIPE_CXSR(dev_priv)) {
  5936. if (intel_crtc->lowfreq_avail) {
  5937. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5938. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5939. } else {
  5940. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5941. }
  5942. }
  5943. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5944. if (INTEL_GEN(dev_priv) < 4 ||
  5945. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5946. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5947. else
  5948. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5949. } else
  5950. pipeconf |= PIPECONF_PROGRESSIVE;
  5951. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5952. intel_crtc->config->limited_color_range)
  5953. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5954. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5955. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5956. }
  5957. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5958. struct intel_crtc_state *crtc_state)
  5959. {
  5960. struct drm_device *dev = crtc->base.dev;
  5961. struct drm_i915_private *dev_priv = to_i915(dev);
  5962. const struct intel_limit *limit;
  5963. int refclk = 48000;
  5964. memset(&crtc_state->dpll_hw_state, 0,
  5965. sizeof(crtc_state->dpll_hw_state));
  5966. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5967. if (intel_panel_use_ssc(dev_priv)) {
  5968. refclk = dev_priv->vbt.lvds_ssc_freq;
  5969. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5970. }
  5971. limit = &intel_limits_i8xx_lvds;
  5972. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5973. limit = &intel_limits_i8xx_dvo;
  5974. } else {
  5975. limit = &intel_limits_i8xx_dac;
  5976. }
  5977. if (!crtc_state->clock_set &&
  5978. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5979. refclk, NULL, &crtc_state->dpll)) {
  5980. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5981. return -EINVAL;
  5982. }
  5983. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5984. return 0;
  5985. }
  5986. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5987. struct intel_crtc_state *crtc_state)
  5988. {
  5989. struct drm_device *dev = crtc->base.dev;
  5990. struct drm_i915_private *dev_priv = to_i915(dev);
  5991. const struct intel_limit *limit;
  5992. int refclk = 96000;
  5993. memset(&crtc_state->dpll_hw_state, 0,
  5994. sizeof(crtc_state->dpll_hw_state));
  5995. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5996. if (intel_panel_use_ssc(dev_priv)) {
  5997. refclk = dev_priv->vbt.lvds_ssc_freq;
  5998. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5999. }
  6000. if (intel_is_dual_link_lvds(dev))
  6001. limit = &intel_limits_g4x_dual_channel_lvds;
  6002. else
  6003. limit = &intel_limits_g4x_single_channel_lvds;
  6004. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  6005. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  6006. limit = &intel_limits_g4x_hdmi;
  6007. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  6008. limit = &intel_limits_g4x_sdvo;
  6009. } else {
  6010. /* The option is for other outputs */
  6011. limit = &intel_limits_i9xx_sdvo;
  6012. }
  6013. if (!crtc_state->clock_set &&
  6014. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6015. refclk, NULL, &crtc_state->dpll)) {
  6016. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6017. return -EINVAL;
  6018. }
  6019. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6020. return 0;
  6021. }
  6022. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6023. struct intel_crtc_state *crtc_state)
  6024. {
  6025. struct drm_device *dev = crtc->base.dev;
  6026. struct drm_i915_private *dev_priv = to_i915(dev);
  6027. const struct intel_limit *limit;
  6028. int refclk = 96000;
  6029. memset(&crtc_state->dpll_hw_state, 0,
  6030. sizeof(crtc_state->dpll_hw_state));
  6031. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6032. if (intel_panel_use_ssc(dev_priv)) {
  6033. refclk = dev_priv->vbt.lvds_ssc_freq;
  6034. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6035. }
  6036. limit = &intel_limits_pineview_lvds;
  6037. } else {
  6038. limit = &intel_limits_pineview_sdvo;
  6039. }
  6040. if (!crtc_state->clock_set &&
  6041. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6042. refclk, NULL, &crtc_state->dpll)) {
  6043. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6044. return -EINVAL;
  6045. }
  6046. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6047. return 0;
  6048. }
  6049. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6050. struct intel_crtc_state *crtc_state)
  6051. {
  6052. struct drm_device *dev = crtc->base.dev;
  6053. struct drm_i915_private *dev_priv = to_i915(dev);
  6054. const struct intel_limit *limit;
  6055. int refclk = 96000;
  6056. memset(&crtc_state->dpll_hw_state, 0,
  6057. sizeof(crtc_state->dpll_hw_state));
  6058. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6059. if (intel_panel_use_ssc(dev_priv)) {
  6060. refclk = dev_priv->vbt.lvds_ssc_freq;
  6061. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6062. }
  6063. limit = &intel_limits_i9xx_lvds;
  6064. } else {
  6065. limit = &intel_limits_i9xx_sdvo;
  6066. }
  6067. if (!crtc_state->clock_set &&
  6068. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6069. refclk, NULL, &crtc_state->dpll)) {
  6070. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6071. return -EINVAL;
  6072. }
  6073. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6074. return 0;
  6075. }
  6076. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6077. struct intel_crtc_state *crtc_state)
  6078. {
  6079. int refclk = 100000;
  6080. const struct intel_limit *limit = &intel_limits_chv;
  6081. memset(&crtc_state->dpll_hw_state, 0,
  6082. sizeof(crtc_state->dpll_hw_state));
  6083. if (!crtc_state->clock_set &&
  6084. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6085. refclk, NULL, &crtc_state->dpll)) {
  6086. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6087. return -EINVAL;
  6088. }
  6089. chv_compute_dpll(crtc, crtc_state);
  6090. return 0;
  6091. }
  6092. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6093. struct intel_crtc_state *crtc_state)
  6094. {
  6095. int refclk = 100000;
  6096. const struct intel_limit *limit = &intel_limits_vlv;
  6097. memset(&crtc_state->dpll_hw_state, 0,
  6098. sizeof(crtc_state->dpll_hw_state));
  6099. if (!crtc_state->clock_set &&
  6100. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6101. refclk, NULL, &crtc_state->dpll)) {
  6102. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6103. return -EINVAL;
  6104. }
  6105. vlv_compute_dpll(crtc, crtc_state);
  6106. return 0;
  6107. }
  6108. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6109. struct intel_crtc_state *pipe_config)
  6110. {
  6111. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6112. uint32_t tmp;
  6113. if (INTEL_GEN(dev_priv) <= 3 &&
  6114. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6115. return;
  6116. tmp = I915_READ(PFIT_CONTROL);
  6117. if (!(tmp & PFIT_ENABLE))
  6118. return;
  6119. /* Check whether the pfit is attached to our pipe. */
  6120. if (INTEL_GEN(dev_priv) < 4) {
  6121. if (crtc->pipe != PIPE_B)
  6122. return;
  6123. } else {
  6124. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6125. return;
  6126. }
  6127. pipe_config->gmch_pfit.control = tmp;
  6128. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6129. }
  6130. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6131. struct intel_crtc_state *pipe_config)
  6132. {
  6133. struct drm_device *dev = crtc->base.dev;
  6134. struct drm_i915_private *dev_priv = to_i915(dev);
  6135. int pipe = pipe_config->cpu_transcoder;
  6136. struct dpll clock;
  6137. u32 mdiv;
  6138. int refclk = 100000;
  6139. /* In case of DSI, DPLL will not be used */
  6140. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6141. return;
  6142. mutex_lock(&dev_priv->sb_lock);
  6143. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6144. mutex_unlock(&dev_priv->sb_lock);
  6145. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6146. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6147. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6148. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6149. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6150. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6151. }
  6152. static void
  6153. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6154. struct intel_initial_plane_config *plane_config)
  6155. {
  6156. struct drm_device *dev = crtc->base.dev;
  6157. struct drm_i915_private *dev_priv = to_i915(dev);
  6158. u32 val, base, offset;
  6159. int pipe = crtc->pipe, plane = crtc->plane;
  6160. int fourcc, pixel_format;
  6161. unsigned int aligned_height;
  6162. struct drm_framebuffer *fb;
  6163. struct intel_framebuffer *intel_fb;
  6164. val = I915_READ(DSPCNTR(plane));
  6165. if (!(val & DISPLAY_PLANE_ENABLE))
  6166. return;
  6167. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6168. if (!intel_fb) {
  6169. DRM_DEBUG_KMS("failed to alloc fb\n");
  6170. return;
  6171. }
  6172. fb = &intel_fb->base;
  6173. fb->dev = dev;
  6174. if (INTEL_GEN(dev_priv) >= 4) {
  6175. if (val & DISPPLANE_TILED) {
  6176. plane_config->tiling = I915_TILING_X;
  6177. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6178. }
  6179. }
  6180. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6181. fourcc = i9xx_format_to_fourcc(pixel_format);
  6182. fb->format = drm_format_info(fourcc);
  6183. if (INTEL_GEN(dev_priv) >= 4) {
  6184. if (plane_config->tiling)
  6185. offset = I915_READ(DSPTILEOFF(plane));
  6186. else
  6187. offset = I915_READ(DSPLINOFF(plane));
  6188. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6189. } else {
  6190. base = I915_READ(DSPADDR(plane));
  6191. }
  6192. plane_config->base = base;
  6193. val = I915_READ(PIPESRC(pipe));
  6194. fb->width = ((val >> 16) & 0xfff) + 1;
  6195. fb->height = ((val >> 0) & 0xfff) + 1;
  6196. val = I915_READ(DSPSTRIDE(pipe));
  6197. fb->pitches[0] = val & 0xffffffc0;
  6198. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6199. plane_config->size = fb->pitches[0] * aligned_height;
  6200. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6201. pipe_name(pipe), plane, fb->width, fb->height,
  6202. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6203. plane_config->size);
  6204. plane_config->fb = intel_fb;
  6205. }
  6206. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6207. struct intel_crtc_state *pipe_config)
  6208. {
  6209. struct drm_device *dev = crtc->base.dev;
  6210. struct drm_i915_private *dev_priv = to_i915(dev);
  6211. int pipe = pipe_config->cpu_transcoder;
  6212. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6213. struct dpll clock;
  6214. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6215. int refclk = 100000;
  6216. /* In case of DSI, DPLL will not be used */
  6217. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6218. return;
  6219. mutex_lock(&dev_priv->sb_lock);
  6220. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6221. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6222. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6223. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6224. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6225. mutex_unlock(&dev_priv->sb_lock);
  6226. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6227. clock.m2 = (pll_dw0 & 0xff) << 22;
  6228. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6229. clock.m2 |= pll_dw2 & 0x3fffff;
  6230. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6231. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6232. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6233. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6234. }
  6235. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6236. struct intel_crtc_state *pipe_config)
  6237. {
  6238. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6239. enum intel_display_power_domain power_domain;
  6240. uint32_t tmp;
  6241. bool ret;
  6242. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6243. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6244. return false;
  6245. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6246. pipe_config->shared_dpll = NULL;
  6247. ret = false;
  6248. tmp = I915_READ(PIPECONF(crtc->pipe));
  6249. if (!(tmp & PIPECONF_ENABLE))
  6250. goto out;
  6251. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6252. IS_CHERRYVIEW(dev_priv)) {
  6253. switch (tmp & PIPECONF_BPC_MASK) {
  6254. case PIPECONF_6BPC:
  6255. pipe_config->pipe_bpp = 18;
  6256. break;
  6257. case PIPECONF_8BPC:
  6258. pipe_config->pipe_bpp = 24;
  6259. break;
  6260. case PIPECONF_10BPC:
  6261. pipe_config->pipe_bpp = 30;
  6262. break;
  6263. default:
  6264. break;
  6265. }
  6266. }
  6267. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6268. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6269. pipe_config->limited_color_range = true;
  6270. if (INTEL_GEN(dev_priv) < 4)
  6271. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6272. intel_get_pipe_timings(crtc, pipe_config);
  6273. intel_get_pipe_src_size(crtc, pipe_config);
  6274. i9xx_get_pfit_config(crtc, pipe_config);
  6275. if (INTEL_GEN(dev_priv) >= 4) {
  6276. /* No way to read it out on pipes B and C */
  6277. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6278. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6279. else
  6280. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6281. pipe_config->pixel_multiplier =
  6282. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6283. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6284. pipe_config->dpll_hw_state.dpll_md = tmp;
  6285. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6286. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6287. tmp = I915_READ(DPLL(crtc->pipe));
  6288. pipe_config->pixel_multiplier =
  6289. ((tmp & SDVO_MULTIPLIER_MASK)
  6290. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6291. } else {
  6292. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6293. * port and will be fixed up in the encoder->get_config
  6294. * function. */
  6295. pipe_config->pixel_multiplier = 1;
  6296. }
  6297. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6298. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6299. /*
  6300. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6301. * on 830. Filter it out here so that we don't
  6302. * report errors due to that.
  6303. */
  6304. if (IS_I830(dev_priv))
  6305. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6306. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6307. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6308. } else {
  6309. /* Mask out read-only status bits. */
  6310. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6311. DPLL_PORTC_READY_MASK |
  6312. DPLL_PORTB_READY_MASK);
  6313. }
  6314. if (IS_CHERRYVIEW(dev_priv))
  6315. chv_crtc_clock_get(crtc, pipe_config);
  6316. else if (IS_VALLEYVIEW(dev_priv))
  6317. vlv_crtc_clock_get(crtc, pipe_config);
  6318. else
  6319. i9xx_crtc_clock_get(crtc, pipe_config);
  6320. /*
  6321. * Normally the dotclock is filled in by the encoder .get_config()
  6322. * but in case the pipe is enabled w/o any ports we need a sane
  6323. * default.
  6324. */
  6325. pipe_config->base.adjusted_mode.crtc_clock =
  6326. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6327. ret = true;
  6328. out:
  6329. intel_display_power_put(dev_priv, power_domain);
  6330. return ret;
  6331. }
  6332. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6333. {
  6334. struct intel_encoder *encoder;
  6335. int i;
  6336. u32 val, final;
  6337. bool has_lvds = false;
  6338. bool has_cpu_edp = false;
  6339. bool has_panel = false;
  6340. bool has_ck505 = false;
  6341. bool can_ssc = false;
  6342. bool using_ssc_source = false;
  6343. /* We need to take the global config into account */
  6344. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6345. switch (encoder->type) {
  6346. case INTEL_OUTPUT_LVDS:
  6347. has_panel = true;
  6348. has_lvds = true;
  6349. break;
  6350. case INTEL_OUTPUT_EDP:
  6351. has_panel = true;
  6352. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6353. has_cpu_edp = true;
  6354. break;
  6355. default:
  6356. break;
  6357. }
  6358. }
  6359. if (HAS_PCH_IBX(dev_priv)) {
  6360. has_ck505 = dev_priv->vbt.display_clock_mode;
  6361. can_ssc = has_ck505;
  6362. } else {
  6363. has_ck505 = false;
  6364. can_ssc = true;
  6365. }
  6366. /* Check if any DPLLs are using the SSC source */
  6367. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6368. u32 temp = I915_READ(PCH_DPLL(i));
  6369. if (!(temp & DPLL_VCO_ENABLE))
  6370. continue;
  6371. if ((temp & PLL_REF_INPUT_MASK) ==
  6372. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6373. using_ssc_source = true;
  6374. break;
  6375. }
  6376. }
  6377. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6378. has_panel, has_lvds, has_ck505, using_ssc_source);
  6379. /* Ironlake: try to setup display ref clock before DPLL
  6380. * enabling. This is only under driver's control after
  6381. * PCH B stepping, previous chipset stepping should be
  6382. * ignoring this setting.
  6383. */
  6384. val = I915_READ(PCH_DREF_CONTROL);
  6385. /* As we must carefully and slowly disable/enable each source in turn,
  6386. * compute the final state we want first and check if we need to
  6387. * make any changes at all.
  6388. */
  6389. final = val;
  6390. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6391. if (has_ck505)
  6392. final |= DREF_NONSPREAD_CK505_ENABLE;
  6393. else
  6394. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6395. final &= ~DREF_SSC_SOURCE_MASK;
  6396. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6397. final &= ~DREF_SSC1_ENABLE;
  6398. if (has_panel) {
  6399. final |= DREF_SSC_SOURCE_ENABLE;
  6400. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6401. final |= DREF_SSC1_ENABLE;
  6402. if (has_cpu_edp) {
  6403. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6404. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6405. else
  6406. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6407. } else
  6408. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6409. } else if (using_ssc_source) {
  6410. final |= DREF_SSC_SOURCE_ENABLE;
  6411. final |= DREF_SSC1_ENABLE;
  6412. }
  6413. if (final == val)
  6414. return;
  6415. /* Always enable nonspread source */
  6416. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6417. if (has_ck505)
  6418. val |= DREF_NONSPREAD_CK505_ENABLE;
  6419. else
  6420. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6421. if (has_panel) {
  6422. val &= ~DREF_SSC_SOURCE_MASK;
  6423. val |= DREF_SSC_SOURCE_ENABLE;
  6424. /* SSC must be turned on before enabling the CPU output */
  6425. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6426. DRM_DEBUG_KMS("Using SSC on panel\n");
  6427. val |= DREF_SSC1_ENABLE;
  6428. } else
  6429. val &= ~DREF_SSC1_ENABLE;
  6430. /* Get SSC going before enabling the outputs */
  6431. I915_WRITE(PCH_DREF_CONTROL, val);
  6432. POSTING_READ(PCH_DREF_CONTROL);
  6433. udelay(200);
  6434. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6435. /* Enable CPU source on CPU attached eDP */
  6436. if (has_cpu_edp) {
  6437. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6438. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6439. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6440. } else
  6441. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6442. } else
  6443. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6444. I915_WRITE(PCH_DREF_CONTROL, val);
  6445. POSTING_READ(PCH_DREF_CONTROL);
  6446. udelay(200);
  6447. } else {
  6448. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6449. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6450. /* Turn off CPU output */
  6451. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6452. I915_WRITE(PCH_DREF_CONTROL, val);
  6453. POSTING_READ(PCH_DREF_CONTROL);
  6454. udelay(200);
  6455. if (!using_ssc_source) {
  6456. DRM_DEBUG_KMS("Disabling SSC source\n");
  6457. /* Turn off the SSC source */
  6458. val &= ~DREF_SSC_SOURCE_MASK;
  6459. val |= DREF_SSC_SOURCE_DISABLE;
  6460. /* Turn off SSC1 */
  6461. val &= ~DREF_SSC1_ENABLE;
  6462. I915_WRITE(PCH_DREF_CONTROL, val);
  6463. POSTING_READ(PCH_DREF_CONTROL);
  6464. udelay(200);
  6465. }
  6466. }
  6467. BUG_ON(val != final);
  6468. }
  6469. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6470. {
  6471. uint32_t tmp;
  6472. tmp = I915_READ(SOUTH_CHICKEN2);
  6473. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6474. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6475. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6476. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6477. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6478. tmp = I915_READ(SOUTH_CHICKEN2);
  6479. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6480. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6481. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6482. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6483. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6484. }
  6485. /* WaMPhyProgramming:hsw */
  6486. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6487. {
  6488. uint32_t tmp;
  6489. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6490. tmp &= ~(0xFF << 24);
  6491. tmp |= (0x12 << 24);
  6492. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6493. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6494. tmp |= (1 << 11);
  6495. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6496. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6497. tmp |= (1 << 11);
  6498. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6499. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6500. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6501. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6502. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6503. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6504. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6505. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6506. tmp &= ~(7 << 13);
  6507. tmp |= (5 << 13);
  6508. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6509. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6510. tmp &= ~(7 << 13);
  6511. tmp |= (5 << 13);
  6512. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6513. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6514. tmp &= ~0xFF;
  6515. tmp |= 0x1C;
  6516. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6517. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6518. tmp &= ~0xFF;
  6519. tmp |= 0x1C;
  6520. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6521. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6522. tmp &= ~(0xFF << 16);
  6523. tmp |= (0x1C << 16);
  6524. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6525. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6526. tmp &= ~(0xFF << 16);
  6527. tmp |= (0x1C << 16);
  6528. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6529. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6530. tmp |= (1 << 27);
  6531. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6532. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6533. tmp |= (1 << 27);
  6534. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6535. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6536. tmp &= ~(0xF << 28);
  6537. tmp |= (4 << 28);
  6538. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6539. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6540. tmp &= ~(0xF << 28);
  6541. tmp |= (4 << 28);
  6542. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6543. }
  6544. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6545. * Programming" based on the parameters passed:
  6546. * - Sequence to enable CLKOUT_DP
  6547. * - Sequence to enable CLKOUT_DP without spread
  6548. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6549. */
  6550. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6551. bool with_spread, bool with_fdi)
  6552. {
  6553. uint32_t reg, tmp;
  6554. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6555. with_spread = true;
  6556. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6557. with_fdi, "LP PCH doesn't have FDI\n"))
  6558. with_fdi = false;
  6559. mutex_lock(&dev_priv->sb_lock);
  6560. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6561. tmp &= ~SBI_SSCCTL_DISABLE;
  6562. tmp |= SBI_SSCCTL_PATHALT;
  6563. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6564. udelay(24);
  6565. if (with_spread) {
  6566. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6567. tmp &= ~SBI_SSCCTL_PATHALT;
  6568. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6569. if (with_fdi) {
  6570. lpt_reset_fdi_mphy(dev_priv);
  6571. lpt_program_fdi_mphy(dev_priv);
  6572. }
  6573. }
  6574. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6575. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6576. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6577. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6578. mutex_unlock(&dev_priv->sb_lock);
  6579. }
  6580. /* Sequence to disable CLKOUT_DP */
  6581. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6582. {
  6583. uint32_t reg, tmp;
  6584. mutex_lock(&dev_priv->sb_lock);
  6585. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6586. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6587. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6588. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6589. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6590. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6591. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6592. tmp |= SBI_SSCCTL_PATHALT;
  6593. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6594. udelay(32);
  6595. }
  6596. tmp |= SBI_SSCCTL_DISABLE;
  6597. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6598. }
  6599. mutex_unlock(&dev_priv->sb_lock);
  6600. }
  6601. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6602. static const uint16_t sscdivintphase[] = {
  6603. [BEND_IDX( 50)] = 0x3B23,
  6604. [BEND_IDX( 45)] = 0x3B23,
  6605. [BEND_IDX( 40)] = 0x3C23,
  6606. [BEND_IDX( 35)] = 0x3C23,
  6607. [BEND_IDX( 30)] = 0x3D23,
  6608. [BEND_IDX( 25)] = 0x3D23,
  6609. [BEND_IDX( 20)] = 0x3E23,
  6610. [BEND_IDX( 15)] = 0x3E23,
  6611. [BEND_IDX( 10)] = 0x3F23,
  6612. [BEND_IDX( 5)] = 0x3F23,
  6613. [BEND_IDX( 0)] = 0x0025,
  6614. [BEND_IDX( -5)] = 0x0025,
  6615. [BEND_IDX(-10)] = 0x0125,
  6616. [BEND_IDX(-15)] = 0x0125,
  6617. [BEND_IDX(-20)] = 0x0225,
  6618. [BEND_IDX(-25)] = 0x0225,
  6619. [BEND_IDX(-30)] = 0x0325,
  6620. [BEND_IDX(-35)] = 0x0325,
  6621. [BEND_IDX(-40)] = 0x0425,
  6622. [BEND_IDX(-45)] = 0x0425,
  6623. [BEND_IDX(-50)] = 0x0525,
  6624. };
  6625. /*
  6626. * Bend CLKOUT_DP
  6627. * steps -50 to 50 inclusive, in steps of 5
  6628. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6629. * change in clock period = -(steps / 10) * 5.787 ps
  6630. */
  6631. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6632. {
  6633. uint32_t tmp;
  6634. int idx = BEND_IDX(steps);
  6635. if (WARN_ON(steps % 5 != 0))
  6636. return;
  6637. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6638. return;
  6639. mutex_lock(&dev_priv->sb_lock);
  6640. if (steps % 10 != 0)
  6641. tmp = 0xAAAAAAAB;
  6642. else
  6643. tmp = 0x00000000;
  6644. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6645. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6646. tmp &= 0xffff0000;
  6647. tmp |= sscdivintphase[idx];
  6648. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6649. mutex_unlock(&dev_priv->sb_lock);
  6650. }
  6651. #undef BEND_IDX
  6652. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6653. {
  6654. struct intel_encoder *encoder;
  6655. bool has_vga = false;
  6656. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6657. switch (encoder->type) {
  6658. case INTEL_OUTPUT_ANALOG:
  6659. has_vga = true;
  6660. break;
  6661. default:
  6662. break;
  6663. }
  6664. }
  6665. if (has_vga) {
  6666. lpt_bend_clkout_dp(dev_priv, 0);
  6667. lpt_enable_clkout_dp(dev_priv, true, true);
  6668. } else {
  6669. lpt_disable_clkout_dp(dev_priv);
  6670. }
  6671. }
  6672. /*
  6673. * Initialize reference clocks when the driver loads
  6674. */
  6675. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6676. {
  6677. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6678. ironlake_init_pch_refclk(dev_priv);
  6679. else if (HAS_PCH_LPT(dev_priv))
  6680. lpt_init_pch_refclk(dev_priv);
  6681. }
  6682. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6683. {
  6684. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6685. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6686. int pipe = intel_crtc->pipe;
  6687. uint32_t val;
  6688. val = 0;
  6689. switch (intel_crtc->config->pipe_bpp) {
  6690. case 18:
  6691. val |= PIPECONF_6BPC;
  6692. break;
  6693. case 24:
  6694. val |= PIPECONF_8BPC;
  6695. break;
  6696. case 30:
  6697. val |= PIPECONF_10BPC;
  6698. break;
  6699. case 36:
  6700. val |= PIPECONF_12BPC;
  6701. break;
  6702. default:
  6703. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6704. BUG();
  6705. }
  6706. if (intel_crtc->config->dither)
  6707. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6708. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6709. val |= PIPECONF_INTERLACED_ILK;
  6710. else
  6711. val |= PIPECONF_PROGRESSIVE;
  6712. if (intel_crtc->config->limited_color_range)
  6713. val |= PIPECONF_COLOR_RANGE_SELECT;
  6714. I915_WRITE(PIPECONF(pipe), val);
  6715. POSTING_READ(PIPECONF(pipe));
  6716. }
  6717. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6718. {
  6719. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6721. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6722. u32 val = 0;
  6723. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6724. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6725. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6726. val |= PIPECONF_INTERLACED_ILK;
  6727. else
  6728. val |= PIPECONF_PROGRESSIVE;
  6729. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6730. POSTING_READ(PIPECONF(cpu_transcoder));
  6731. }
  6732. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6733. {
  6734. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6735. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6736. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6737. u32 val = 0;
  6738. switch (intel_crtc->config->pipe_bpp) {
  6739. case 18:
  6740. val |= PIPEMISC_DITHER_6_BPC;
  6741. break;
  6742. case 24:
  6743. val |= PIPEMISC_DITHER_8_BPC;
  6744. break;
  6745. case 30:
  6746. val |= PIPEMISC_DITHER_10_BPC;
  6747. break;
  6748. case 36:
  6749. val |= PIPEMISC_DITHER_12_BPC;
  6750. break;
  6751. default:
  6752. /* Case prevented by pipe_config_set_bpp. */
  6753. BUG();
  6754. }
  6755. if (intel_crtc->config->dither)
  6756. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6757. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6758. }
  6759. }
  6760. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6761. {
  6762. /*
  6763. * Account for spread spectrum to avoid
  6764. * oversubscribing the link. Max center spread
  6765. * is 2.5%; use 5% for safety's sake.
  6766. */
  6767. u32 bps = target_clock * bpp * 21 / 20;
  6768. return DIV_ROUND_UP(bps, link_bw * 8);
  6769. }
  6770. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6771. {
  6772. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6773. }
  6774. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6775. struct intel_crtc_state *crtc_state,
  6776. struct dpll *reduced_clock)
  6777. {
  6778. struct drm_crtc *crtc = &intel_crtc->base;
  6779. struct drm_device *dev = crtc->dev;
  6780. struct drm_i915_private *dev_priv = to_i915(dev);
  6781. u32 dpll, fp, fp2;
  6782. int factor;
  6783. /* Enable autotuning of the PLL clock (if permissible) */
  6784. factor = 21;
  6785. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6786. if ((intel_panel_use_ssc(dev_priv) &&
  6787. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6788. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6789. factor = 25;
  6790. } else if (crtc_state->sdvo_tv_clock)
  6791. factor = 20;
  6792. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6793. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6794. fp |= FP_CB_TUNE;
  6795. if (reduced_clock) {
  6796. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6797. if (reduced_clock->m < factor * reduced_clock->n)
  6798. fp2 |= FP_CB_TUNE;
  6799. } else {
  6800. fp2 = fp;
  6801. }
  6802. dpll = 0;
  6803. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6804. dpll |= DPLLB_MODE_LVDS;
  6805. else
  6806. dpll |= DPLLB_MODE_DAC_SERIAL;
  6807. dpll |= (crtc_state->pixel_multiplier - 1)
  6808. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6809. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6810. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6811. dpll |= DPLL_SDVO_HIGH_SPEED;
  6812. if (intel_crtc_has_dp_encoder(crtc_state))
  6813. dpll |= DPLL_SDVO_HIGH_SPEED;
  6814. /*
  6815. * The high speed IO clock is only really required for
  6816. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6817. * possible to share the DPLL between CRT and HDMI. Enabling
  6818. * the clock needlessly does no real harm, except use up a
  6819. * bit of power potentially.
  6820. *
  6821. * We'll limit this to IVB with 3 pipes, since it has only two
  6822. * DPLLs and so DPLL sharing is the only way to get three pipes
  6823. * driving PCH ports at the same time. On SNB we could do this,
  6824. * and potentially avoid enabling the second DPLL, but it's not
  6825. * clear if it''s a win or loss power wise. No point in doing
  6826. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6827. */
  6828. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6829. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6830. dpll |= DPLL_SDVO_HIGH_SPEED;
  6831. /* compute bitmask from p1 value */
  6832. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6833. /* also FPA1 */
  6834. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6835. switch (crtc_state->dpll.p2) {
  6836. case 5:
  6837. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6838. break;
  6839. case 7:
  6840. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6841. break;
  6842. case 10:
  6843. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6844. break;
  6845. case 14:
  6846. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6847. break;
  6848. }
  6849. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6850. intel_panel_use_ssc(dev_priv))
  6851. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6852. else
  6853. dpll |= PLL_REF_INPUT_DREFCLK;
  6854. dpll |= DPLL_VCO_ENABLE;
  6855. crtc_state->dpll_hw_state.dpll = dpll;
  6856. crtc_state->dpll_hw_state.fp0 = fp;
  6857. crtc_state->dpll_hw_state.fp1 = fp2;
  6858. }
  6859. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6860. struct intel_crtc_state *crtc_state)
  6861. {
  6862. struct drm_device *dev = crtc->base.dev;
  6863. struct drm_i915_private *dev_priv = to_i915(dev);
  6864. struct dpll reduced_clock;
  6865. bool has_reduced_clock = false;
  6866. struct intel_shared_dpll *pll;
  6867. const struct intel_limit *limit;
  6868. int refclk = 120000;
  6869. memset(&crtc_state->dpll_hw_state, 0,
  6870. sizeof(crtc_state->dpll_hw_state));
  6871. crtc->lowfreq_avail = false;
  6872. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6873. if (!crtc_state->has_pch_encoder)
  6874. return 0;
  6875. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6876. if (intel_panel_use_ssc(dev_priv)) {
  6877. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6878. dev_priv->vbt.lvds_ssc_freq);
  6879. refclk = dev_priv->vbt.lvds_ssc_freq;
  6880. }
  6881. if (intel_is_dual_link_lvds(dev)) {
  6882. if (refclk == 100000)
  6883. limit = &intel_limits_ironlake_dual_lvds_100m;
  6884. else
  6885. limit = &intel_limits_ironlake_dual_lvds;
  6886. } else {
  6887. if (refclk == 100000)
  6888. limit = &intel_limits_ironlake_single_lvds_100m;
  6889. else
  6890. limit = &intel_limits_ironlake_single_lvds;
  6891. }
  6892. } else {
  6893. limit = &intel_limits_ironlake_dac;
  6894. }
  6895. if (!crtc_state->clock_set &&
  6896. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6897. refclk, NULL, &crtc_state->dpll)) {
  6898. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6899. return -EINVAL;
  6900. }
  6901. ironlake_compute_dpll(crtc, crtc_state,
  6902. has_reduced_clock ? &reduced_clock : NULL);
  6903. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  6904. if (pll == NULL) {
  6905. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6906. pipe_name(crtc->pipe));
  6907. return -EINVAL;
  6908. }
  6909. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6910. has_reduced_clock)
  6911. crtc->lowfreq_avail = true;
  6912. return 0;
  6913. }
  6914. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6915. struct intel_link_m_n *m_n)
  6916. {
  6917. struct drm_device *dev = crtc->base.dev;
  6918. struct drm_i915_private *dev_priv = to_i915(dev);
  6919. enum pipe pipe = crtc->pipe;
  6920. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6921. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6922. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6923. & ~TU_SIZE_MASK;
  6924. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6925. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6926. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6927. }
  6928. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6929. enum transcoder transcoder,
  6930. struct intel_link_m_n *m_n,
  6931. struct intel_link_m_n *m2_n2)
  6932. {
  6933. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6934. enum pipe pipe = crtc->pipe;
  6935. if (INTEL_GEN(dev_priv) >= 5) {
  6936. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6937. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6938. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6939. & ~TU_SIZE_MASK;
  6940. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6941. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6942. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6943. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6944. * gen < 8) and if DRRS is supported (to make sure the
  6945. * registers are not unnecessarily read).
  6946. */
  6947. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6948. crtc->config->has_drrs) {
  6949. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6950. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6951. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6952. & ~TU_SIZE_MASK;
  6953. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6954. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6955. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6956. }
  6957. } else {
  6958. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6959. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6960. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6961. & ~TU_SIZE_MASK;
  6962. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6963. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6964. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6965. }
  6966. }
  6967. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6968. struct intel_crtc_state *pipe_config)
  6969. {
  6970. if (pipe_config->has_pch_encoder)
  6971. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6972. else
  6973. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6974. &pipe_config->dp_m_n,
  6975. &pipe_config->dp_m2_n2);
  6976. }
  6977. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6978. struct intel_crtc_state *pipe_config)
  6979. {
  6980. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6981. &pipe_config->fdi_m_n, NULL);
  6982. }
  6983. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6984. struct intel_crtc_state *pipe_config)
  6985. {
  6986. struct drm_device *dev = crtc->base.dev;
  6987. struct drm_i915_private *dev_priv = to_i915(dev);
  6988. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6989. uint32_t ps_ctrl = 0;
  6990. int id = -1;
  6991. int i;
  6992. /* find scaler attached to this pipe */
  6993. for (i = 0; i < crtc->num_scalers; i++) {
  6994. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6995. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6996. id = i;
  6997. pipe_config->pch_pfit.enabled = true;
  6998. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6999. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  7000. break;
  7001. }
  7002. }
  7003. scaler_state->scaler_id = id;
  7004. if (id >= 0) {
  7005. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  7006. } else {
  7007. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7008. }
  7009. }
  7010. static void
  7011. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  7012. struct intel_initial_plane_config *plane_config)
  7013. {
  7014. struct drm_device *dev = crtc->base.dev;
  7015. struct drm_i915_private *dev_priv = to_i915(dev);
  7016. u32 val, base, offset, stride_mult, tiling;
  7017. int pipe = crtc->pipe;
  7018. int fourcc, pixel_format;
  7019. unsigned int aligned_height;
  7020. struct drm_framebuffer *fb;
  7021. struct intel_framebuffer *intel_fb;
  7022. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7023. if (!intel_fb) {
  7024. DRM_DEBUG_KMS("failed to alloc fb\n");
  7025. return;
  7026. }
  7027. fb = &intel_fb->base;
  7028. fb->dev = dev;
  7029. val = I915_READ(PLANE_CTL(pipe, 0));
  7030. if (!(val & PLANE_CTL_ENABLE))
  7031. goto error;
  7032. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7033. fourcc = skl_format_to_fourcc(pixel_format,
  7034. val & PLANE_CTL_ORDER_RGBX,
  7035. val & PLANE_CTL_ALPHA_MASK);
  7036. fb->format = drm_format_info(fourcc);
  7037. tiling = val & PLANE_CTL_TILED_MASK;
  7038. switch (tiling) {
  7039. case PLANE_CTL_TILED_LINEAR:
  7040. fb->modifier = DRM_FORMAT_MOD_NONE;
  7041. break;
  7042. case PLANE_CTL_TILED_X:
  7043. plane_config->tiling = I915_TILING_X;
  7044. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7045. break;
  7046. case PLANE_CTL_TILED_Y:
  7047. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7048. break;
  7049. case PLANE_CTL_TILED_YF:
  7050. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7051. break;
  7052. default:
  7053. MISSING_CASE(tiling);
  7054. goto error;
  7055. }
  7056. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7057. plane_config->base = base;
  7058. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7059. val = I915_READ(PLANE_SIZE(pipe, 0));
  7060. fb->height = ((val >> 16) & 0xfff) + 1;
  7061. fb->width = ((val >> 0) & 0x1fff) + 1;
  7062. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7063. stride_mult = intel_fb_stride_alignment(fb, 0);
  7064. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7065. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7066. plane_config->size = fb->pitches[0] * aligned_height;
  7067. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7068. pipe_name(pipe), fb->width, fb->height,
  7069. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7070. plane_config->size);
  7071. plane_config->fb = intel_fb;
  7072. return;
  7073. error:
  7074. kfree(intel_fb);
  7075. }
  7076. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7077. struct intel_crtc_state *pipe_config)
  7078. {
  7079. struct drm_device *dev = crtc->base.dev;
  7080. struct drm_i915_private *dev_priv = to_i915(dev);
  7081. uint32_t tmp;
  7082. tmp = I915_READ(PF_CTL(crtc->pipe));
  7083. if (tmp & PF_ENABLE) {
  7084. pipe_config->pch_pfit.enabled = true;
  7085. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7086. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7087. /* We currently do not free assignements of panel fitters on
  7088. * ivb/hsw (since we don't use the higher upscaling modes which
  7089. * differentiates them) so just WARN about this case for now. */
  7090. if (IS_GEN7(dev_priv)) {
  7091. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7092. PF_PIPE_SEL_IVB(crtc->pipe));
  7093. }
  7094. }
  7095. }
  7096. static void
  7097. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7098. struct intel_initial_plane_config *plane_config)
  7099. {
  7100. struct drm_device *dev = crtc->base.dev;
  7101. struct drm_i915_private *dev_priv = to_i915(dev);
  7102. u32 val, base, offset;
  7103. int pipe = crtc->pipe;
  7104. int fourcc, pixel_format;
  7105. unsigned int aligned_height;
  7106. struct drm_framebuffer *fb;
  7107. struct intel_framebuffer *intel_fb;
  7108. val = I915_READ(DSPCNTR(pipe));
  7109. if (!(val & DISPLAY_PLANE_ENABLE))
  7110. return;
  7111. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7112. if (!intel_fb) {
  7113. DRM_DEBUG_KMS("failed to alloc fb\n");
  7114. return;
  7115. }
  7116. fb = &intel_fb->base;
  7117. fb->dev = dev;
  7118. if (INTEL_GEN(dev_priv) >= 4) {
  7119. if (val & DISPPLANE_TILED) {
  7120. plane_config->tiling = I915_TILING_X;
  7121. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7122. }
  7123. }
  7124. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7125. fourcc = i9xx_format_to_fourcc(pixel_format);
  7126. fb->format = drm_format_info(fourcc);
  7127. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7128. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7129. offset = I915_READ(DSPOFFSET(pipe));
  7130. } else {
  7131. if (plane_config->tiling)
  7132. offset = I915_READ(DSPTILEOFF(pipe));
  7133. else
  7134. offset = I915_READ(DSPLINOFF(pipe));
  7135. }
  7136. plane_config->base = base;
  7137. val = I915_READ(PIPESRC(pipe));
  7138. fb->width = ((val >> 16) & 0xfff) + 1;
  7139. fb->height = ((val >> 0) & 0xfff) + 1;
  7140. val = I915_READ(DSPSTRIDE(pipe));
  7141. fb->pitches[0] = val & 0xffffffc0;
  7142. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7143. plane_config->size = fb->pitches[0] * aligned_height;
  7144. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7145. pipe_name(pipe), fb->width, fb->height,
  7146. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7147. plane_config->size);
  7148. plane_config->fb = intel_fb;
  7149. }
  7150. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7151. struct intel_crtc_state *pipe_config)
  7152. {
  7153. struct drm_device *dev = crtc->base.dev;
  7154. struct drm_i915_private *dev_priv = to_i915(dev);
  7155. enum intel_display_power_domain power_domain;
  7156. uint32_t tmp;
  7157. bool ret;
  7158. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7159. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7160. return false;
  7161. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7162. pipe_config->shared_dpll = NULL;
  7163. ret = false;
  7164. tmp = I915_READ(PIPECONF(crtc->pipe));
  7165. if (!(tmp & PIPECONF_ENABLE))
  7166. goto out;
  7167. switch (tmp & PIPECONF_BPC_MASK) {
  7168. case PIPECONF_6BPC:
  7169. pipe_config->pipe_bpp = 18;
  7170. break;
  7171. case PIPECONF_8BPC:
  7172. pipe_config->pipe_bpp = 24;
  7173. break;
  7174. case PIPECONF_10BPC:
  7175. pipe_config->pipe_bpp = 30;
  7176. break;
  7177. case PIPECONF_12BPC:
  7178. pipe_config->pipe_bpp = 36;
  7179. break;
  7180. default:
  7181. break;
  7182. }
  7183. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7184. pipe_config->limited_color_range = true;
  7185. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7186. struct intel_shared_dpll *pll;
  7187. enum intel_dpll_id pll_id;
  7188. pipe_config->has_pch_encoder = true;
  7189. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7190. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7191. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7192. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7193. if (HAS_PCH_IBX(dev_priv)) {
  7194. /*
  7195. * The pipe->pch transcoder and pch transcoder->pll
  7196. * mapping is fixed.
  7197. */
  7198. pll_id = (enum intel_dpll_id) crtc->pipe;
  7199. } else {
  7200. tmp = I915_READ(PCH_DPLL_SEL);
  7201. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7202. pll_id = DPLL_ID_PCH_PLL_B;
  7203. else
  7204. pll_id= DPLL_ID_PCH_PLL_A;
  7205. }
  7206. pipe_config->shared_dpll =
  7207. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7208. pll = pipe_config->shared_dpll;
  7209. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7210. &pipe_config->dpll_hw_state));
  7211. tmp = pipe_config->dpll_hw_state.dpll;
  7212. pipe_config->pixel_multiplier =
  7213. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7214. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7215. ironlake_pch_clock_get(crtc, pipe_config);
  7216. } else {
  7217. pipe_config->pixel_multiplier = 1;
  7218. }
  7219. intel_get_pipe_timings(crtc, pipe_config);
  7220. intel_get_pipe_src_size(crtc, pipe_config);
  7221. ironlake_get_pfit_config(crtc, pipe_config);
  7222. ret = true;
  7223. out:
  7224. intel_display_power_put(dev_priv, power_domain);
  7225. return ret;
  7226. }
  7227. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7228. {
  7229. struct drm_device *dev = &dev_priv->drm;
  7230. struct intel_crtc *crtc;
  7231. for_each_intel_crtc(dev, crtc)
  7232. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7233. pipe_name(crtc->pipe));
  7234. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7235. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7236. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7237. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7238. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7239. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7240. "CPU PWM1 enabled\n");
  7241. if (IS_HASWELL(dev_priv))
  7242. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7243. "CPU PWM2 enabled\n");
  7244. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7245. "PCH PWM1 enabled\n");
  7246. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7247. "Utility pin enabled\n");
  7248. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7249. /*
  7250. * In theory we can still leave IRQs enabled, as long as only the HPD
  7251. * interrupts remain enabled. We used to check for that, but since it's
  7252. * gen-specific and since we only disable LCPLL after we fully disable
  7253. * the interrupts, the check below should be enough.
  7254. */
  7255. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7256. }
  7257. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7258. {
  7259. if (IS_HASWELL(dev_priv))
  7260. return I915_READ(D_COMP_HSW);
  7261. else
  7262. return I915_READ(D_COMP_BDW);
  7263. }
  7264. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7265. {
  7266. if (IS_HASWELL(dev_priv)) {
  7267. mutex_lock(&dev_priv->rps.hw_lock);
  7268. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7269. val))
  7270. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7271. mutex_unlock(&dev_priv->rps.hw_lock);
  7272. } else {
  7273. I915_WRITE(D_COMP_BDW, val);
  7274. POSTING_READ(D_COMP_BDW);
  7275. }
  7276. }
  7277. /*
  7278. * This function implements pieces of two sequences from BSpec:
  7279. * - Sequence for display software to disable LCPLL
  7280. * - Sequence for display software to allow package C8+
  7281. * The steps implemented here are just the steps that actually touch the LCPLL
  7282. * register. Callers should take care of disabling all the display engine
  7283. * functions, doing the mode unset, fixing interrupts, etc.
  7284. */
  7285. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7286. bool switch_to_fclk, bool allow_power_down)
  7287. {
  7288. uint32_t val;
  7289. assert_can_disable_lcpll(dev_priv);
  7290. val = I915_READ(LCPLL_CTL);
  7291. if (switch_to_fclk) {
  7292. val |= LCPLL_CD_SOURCE_FCLK;
  7293. I915_WRITE(LCPLL_CTL, val);
  7294. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7295. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7296. DRM_ERROR("Switching to FCLK failed\n");
  7297. val = I915_READ(LCPLL_CTL);
  7298. }
  7299. val |= LCPLL_PLL_DISABLE;
  7300. I915_WRITE(LCPLL_CTL, val);
  7301. POSTING_READ(LCPLL_CTL);
  7302. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7303. DRM_ERROR("LCPLL still locked\n");
  7304. val = hsw_read_dcomp(dev_priv);
  7305. val |= D_COMP_COMP_DISABLE;
  7306. hsw_write_dcomp(dev_priv, val);
  7307. ndelay(100);
  7308. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7309. 1))
  7310. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7311. if (allow_power_down) {
  7312. val = I915_READ(LCPLL_CTL);
  7313. val |= LCPLL_POWER_DOWN_ALLOW;
  7314. I915_WRITE(LCPLL_CTL, val);
  7315. POSTING_READ(LCPLL_CTL);
  7316. }
  7317. }
  7318. /*
  7319. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7320. * source.
  7321. */
  7322. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7323. {
  7324. uint32_t val;
  7325. val = I915_READ(LCPLL_CTL);
  7326. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7327. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7328. return;
  7329. /*
  7330. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7331. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7332. */
  7333. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7334. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7335. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7336. I915_WRITE(LCPLL_CTL, val);
  7337. POSTING_READ(LCPLL_CTL);
  7338. }
  7339. val = hsw_read_dcomp(dev_priv);
  7340. val |= D_COMP_COMP_FORCE;
  7341. val &= ~D_COMP_COMP_DISABLE;
  7342. hsw_write_dcomp(dev_priv, val);
  7343. val = I915_READ(LCPLL_CTL);
  7344. val &= ~LCPLL_PLL_DISABLE;
  7345. I915_WRITE(LCPLL_CTL, val);
  7346. if (intel_wait_for_register(dev_priv,
  7347. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7348. 5))
  7349. DRM_ERROR("LCPLL not locked yet\n");
  7350. if (val & LCPLL_CD_SOURCE_FCLK) {
  7351. val = I915_READ(LCPLL_CTL);
  7352. val &= ~LCPLL_CD_SOURCE_FCLK;
  7353. I915_WRITE(LCPLL_CTL, val);
  7354. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7355. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7356. DRM_ERROR("Switching back to LCPLL failed\n");
  7357. }
  7358. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7359. intel_update_cdclk(dev_priv);
  7360. }
  7361. /*
  7362. * Package states C8 and deeper are really deep PC states that can only be
  7363. * reached when all the devices on the system allow it, so even if the graphics
  7364. * device allows PC8+, it doesn't mean the system will actually get to these
  7365. * states. Our driver only allows PC8+ when going into runtime PM.
  7366. *
  7367. * The requirements for PC8+ are that all the outputs are disabled, the power
  7368. * well is disabled and most interrupts are disabled, and these are also
  7369. * requirements for runtime PM. When these conditions are met, we manually do
  7370. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7371. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7372. * hang the machine.
  7373. *
  7374. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7375. * the state of some registers, so when we come back from PC8+ we need to
  7376. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7377. * need to take care of the registers kept by RC6. Notice that this happens even
  7378. * if we don't put the device in PCI D3 state (which is what currently happens
  7379. * because of the runtime PM support).
  7380. *
  7381. * For more, read "Display Sequences for Package C8" on the hardware
  7382. * documentation.
  7383. */
  7384. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7385. {
  7386. uint32_t val;
  7387. DRM_DEBUG_KMS("Enabling package C8+\n");
  7388. if (HAS_PCH_LPT_LP(dev_priv)) {
  7389. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7390. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7391. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7392. }
  7393. lpt_disable_clkout_dp(dev_priv);
  7394. hsw_disable_lcpll(dev_priv, true, true);
  7395. }
  7396. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7397. {
  7398. uint32_t val;
  7399. DRM_DEBUG_KMS("Disabling package C8+\n");
  7400. hsw_restore_lcpll(dev_priv);
  7401. lpt_init_pch_refclk(dev_priv);
  7402. if (HAS_PCH_LPT_LP(dev_priv)) {
  7403. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7404. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7405. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7406. }
  7407. }
  7408. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7409. struct intel_crtc_state *crtc_state)
  7410. {
  7411. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7412. if (!intel_ddi_pll_select(crtc, crtc_state))
  7413. return -EINVAL;
  7414. }
  7415. crtc->lowfreq_avail = false;
  7416. return 0;
  7417. }
  7418. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7419. enum port port,
  7420. struct intel_crtc_state *pipe_config)
  7421. {
  7422. enum intel_dpll_id id;
  7423. switch (port) {
  7424. case PORT_A:
  7425. id = DPLL_ID_SKL_DPLL0;
  7426. break;
  7427. case PORT_B:
  7428. id = DPLL_ID_SKL_DPLL1;
  7429. break;
  7430. case PORT_C:
  7431. id = DPLL_ID_SKL_DPLL2;
  7432. break;
  7433. default:
  7434. DRM_ERROR("Incorrect port type\n");
  7435. return;
  7436. }
  7437. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7438. }
  7439. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7440. enum port port,
  7441. struct intel_crtc_state *pipe_config)
  7442. {
  7443. enum intel_dpll_id id;
  7444. u32 temp;
  7445. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7446. id = temp >> (port * 3 + 1);
  7447. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7448. return;
  7449. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7450. }
  7451. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7452. enum port port,
  7453. struct intel_crtc_state *pipe_config)
  7454. {
  7455. enum intel_dpll_id id;
  7456. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7457. switch (ddi_pll_sel) {
  7458. case PORT_CLK_SEL_WRPLL1:
  7459. id = DPLL_ID_WRPLL1;
  7460. break;
  7461. case PORT_CLK_SEL_WRPLL2:
  7462. id = DPLL_ID_WRPLL2;
  7463. break;
  7464. case PORT_CLK_SEL_SPLL:
  7465. id = DPLL_ID_SPLL;
  7466. break;
  7467. case PORT_CLK_SEL_LCPLL_810:
  7468. id = DPLL_ID_LCPLL_810;
  7469. break;
  7470. case PORT_CLK_SEL_LCPLL_1350:
  7471. id = DPLL_ID_LCPLL_1350;
  7472. break;
  7473. case PORT_CLK_SEL_LCPLL_2700:
  7474. id = DPLL_ID_LCPLL_2700;
  7475. break;
  7476. default:
  7477. MISSING_CASE(ddi_pll_sel);
  7478. /* fall through */
  7479. case PORT_CLK_SEL_NONE:
  7480. return;
  7481. }
  7482. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7483. }
  7484. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7485. struct intel_crtc_state *pipe_config,
  7486. u64 *power_domain_mask)
  7487. {
  7488. struct drm_device *dev = crtc->base.dev;
  7489. struct drm_i915_private *dev_priv = to_i915(dev);
  7490. enum intel_display_power_domain power_domain;
  7491. u32 tmp;
  7492. /*
  7493. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7494. * transcoder handled below.
  7495. */
  7496. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7497. /*
  7498. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7499. * consistency and less surprising code; it's in always on power).
  7500. */
  7501. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7502. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7503. enum pipe trans_edp_pipe;
  7504. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7505. default:
  7506. WARN(1, "unknown pipe linked to edp transcoder\n");
  7507. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7508. case TRANS_DDI_EDP_INPUT_A_ON:
  7509. trans_edp_pipe = PIPE_A;
  7510. break;
  7511. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7512. trans_edp_pipe = PIPE_B;
  7513. break;
  7514. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7515. trans_edp_pipe = PIPE_C;
  7516. break;
  7517. }
  7518. if (trans_edp_pipe == crtc->pipe)
  7519. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7520. }
  7521. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7522. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7523. return false;
  7524. *power_domain_mask |= BIT_ULL(power_domain);
  7525. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7526. return tmp & PIPECONF_ENABLE;
  7527. }
  7528. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7529. struct intel_crtc_state *pipe_config,
  7530. u64 *power_domain_mask)
  7531. {
  7532. struct drm_device *dev = crtc->base.dev;
  7533. struct drm_i915_private *dev_priv = to_i915(dev);
  7534. enum intel_display_power_domain power_domain;
  7535. enum port port;
  7536. enum transcoder cpu_transcoder;
  7537. u32 tmp;
  7538. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7539. if (port == PORT_A)
  7540. cpu_transcoder = TRANSCODER_DSI_A;
  7541. else
  7542. cpu_transcoder = TRANSCODER_DSI_C;
  7543. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7544. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7545. continue;
  7546. *power_domain_mask |= BIT_ULL(power_domain);
  7547. /*
  7548. * The PLL needs to be enabled with a valid divider
  7549. * configuration, otherwise accessing DSI registers will hang
  7550. * the machine. See BSpec North Display Engine
  7551. * registers/MIPI[BXT]. We can break out here early, since we
  7552. * need the same DSI PLL to be enabled for both DSI ports.
  7553. */
  7554. if (!intel_dsi_pll_is_enabled(dev_priv))
  7555. break;
  7556. /* XXX: this works for video mode only */
  7557. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7558. if (!(tmp & DPI_ENABLE))
  7559. continue;
  7560. tmp = I915_READ(MIPI_CTRL(port));
  7561. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7562. continue;
  7563. pipe_config->cpu_transcoder = cpu_transcoder;
  7564. break;
  7565. }
  7566. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7567. }
  7568. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7569. struct intel_crtc_state *pipe_config)
  7570. {
  7571. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7572. struct intel_shared_dpll *pll;
  7573. enum port port;
  7574. uint32_t tmp;
  7575. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7576. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7577. if (IS_GEN9_BC(dev_priv))
  7578. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7579. else if (IS_GEN9_LP(dev_priv))
  7580. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7581. else
  7582. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7583. pll = pipe_config->shared_dpll;
  7584. if (pll) {
  7585. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7586. &pipe_config->dpll_hw_state));
  7587. }
  7588. /*
  7589. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7590. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7591. * the PCH transcoder is on.
  7592. */
  7593. if (INTEL_GEN(dev_priv) < 9 &&
  7594. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7595. pipe_config->has_pch_encoder = true;
  7596. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7597. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7598. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7599. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7600. }
  7601. }
  7602. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7603. struct intel_crtc_state *pipe_config)
  7604. {
  7605. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7606. enum intel_display_power_domain power_domain;
  7607. u64 power_domain_mask;
  7608. bool active;
  7609. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7610. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7611. return false;
  7612. power_domain_mask = BIT_ULL(power_domain);
  7613. pipe_config->shared_dpll = NULL;
  7614. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7615. if (IS_GEN9_LP(dev_priv) &&
  7616. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7617. WARN_ON(active);
  7618. active = true;
  7619. }
  7620. if (!active)
  7621. goto out;
  7622. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7623. haswell_get_ddi_port_state(crtc, pipe_config);
  7624. intel_get_pipe_timings(crtc, pipe_config);
  7625. }
  7626. intel_get_pipe_src_size(crtc, pipe_config);
  7627. pipe_config->gamma_mode =
  7628. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7629. if (INTEL_GEN(dev_priv) >= 9) {
  7630. intel_crtc_init_scalers(crtc, pipe_config);
  7631. pipe_config->scaler_state.scaler_id = -1;
  7632. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7633. }
  7634. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7635. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7636. power_domain_mask |= BIT_ULL(power_domain);
  7637. if (INTEL_GEN(dev_priv) >= 9)
  7638. skylake_get_pfit_config(crtc, pipe_config);
  7639. else
  7640. ironlake_get_pfit_config(crtc, pipe_config);
  7641. }
  7642. if (IS_HASWELL(dev_priv))
  7643. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7644. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7645. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7646. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7647. pipe_config->pixel_multiplier =
  7648. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7649. } else {
  7650. pipe_config->pixel_multiplier = 1;
  7651. }
  7652. out:
  7653. for_each_power_domain(power_domain, power_domain_mask)
  7654. intel_display_power_put(dev_priv, power_domain);
  7655. return active;
  7656. }
  7657. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  7658. const struct intel_plane_state *plane_state)
  7659. {
  7660. struct drm_device *dev = crtc->dev;
  7661. struct drm_i915_private *dev_priv = to_i915(dev);
  7662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7663. uint32_t cntl = 0, size = 0;
  7664. if (plane_state && plane_state->base.visible) {
  7665. unsigned int width = plane_state->base.crtc_w;
  7666. unsigned int height = plane_state->base.crtc_h;
  7667. unsigned int stride = roundup_pow_of_two(width) * 4;
  7668. switch (stride) {
  7669. default:
  7670. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7671. width, stride);
  7672. stride = 256;
  7673. /* fallthrough */
  7674. case 256:
  7675. case 512:
  7676. case 1024:
  7677. case 2048:
  7678. break;
  7679. }
  7680. cntl |= CURSOR_ENABLE |
  7681. CURSOR_GAMMA_ENABLE |
  7682. CURSOR_FORMAT_ARGB |
  7683. CURSOR_STRIDE(stride);
  7684. size = (height << 12) | width;
  7685. }
  7686. if (intel_crtc->cursor_cntl != 0 &&
  7687. (intel_crtc->cursor_base != base ||
  7688. intel_crtc->cursor_size != size ||
  7689. intel_crtc->cursor_cntl != cntl)) {
  7690. /* On these chipsets we can only modify the base/size/stride
  7691. * whilst the cursor is disabled.
  7692. */
  7693. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7694. POSTING_READ_FW(CURCNTR(PIPE_A));
  7695. intel_crtc->cursor_cntl = 0;
  7696. }
  7697. if (intel_crtc->cursor_base != base) {
  7698. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7699. intel_crtc->cursor_base = base;
  7700. }
  7701. if (intel_crtc->cursor_size != size) {
  7702. I915_WRITE_FW(CURSIZE, size);
  7703. intel_crtc->cursor_size = size;
  7704. }
  7705. if (intel_crtc->cursor_cntl != cntl) {
  7706. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7707. POSTING_READ_FW(CURCNTR(PIPE_A));
  7708. intel_crtc->cursor_cntl = cntl;
  7709. }
  7710. }
  7711. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  7712. const struct intel_plane_state *plane_state)
  7713. {
  7714. struct drm_device *dev = crtc->dev;
  7715. struct drm_i915_private *dev_priv = to_i915(dev);
  7716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7717. int pipe = intel_crtc->pipe;
  7718. uint32_t cntl = 0;
  7719. if (plane_state && plane_state->base.visible) {
  7720. cntl = MCURSOR_GAMMA_ENABLE;
  7721. switch (plane_state->base.crtc_w) {
  7722. case 64:
  7723. cntl |= CURSOR_MODE_64_ARGB_AX;
  7724. break;
  7725. case 128:
  7726. cntl |= CURSOR_MODE_128_ARGB_AX;
  7727. break;
  7728. case 256:
  7729. cntl |= CURSOR_MODE_256_ARGB_AX;
  7730. break;
  7731. default:
  7732. MISSING_CASE(plane_state->base.crtc_w);
  7733. return;
  7734. }
  7735. cntl |= pipe << 28; /* Connect to correct pipe */
  7736. if (HAS_DDI(dev_priv))
  7737. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7738. if (plane_state->base.rotation & DRM_ROTATE_180)
  7739. cntl |= CURSOR_ROTATE_180;
  7740. }
  7741. if (intel_crtc->cursor_cntl != cntl) {
  7742. I915_WRITE_FW(CURCNTR(pipe), cntl);
  7743. POSTING_READ_FW(CURCNTR(pipe));
  7744. intel_crtc->cursor_cntl = cntl;
  7745. }
  7746. /* and commit changes on next vblank */
  7747. I915_WRITE_FW(CURBASE(pipe), base);
  7748. POSTING_READ_FW(CURBASE(pipe));
  7749. intel_crtc->cursor_base = base;
  7750. }
  7751. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7752. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7753. const struct intel_plane_state *plane_state)
  7754. {
  7755. struct drm_device *dev = crtc->dev;
  7756. struct drm_i915_private *dev_priv = to_i915(dev);
  7757. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7758. int pipe = intel_crtc->pipe;
  7759. u32 base = intel_crtc->cursor_addr;
  7760. unsigned long irqflags;
  7761. u32 pos = 0;
  7762. if (plane_state) {
  7763. int x = plane_state->base.crtc_x;
  7764. int y = plane_state->base.crtc_y;
  7765. if (x < 0) {
  7766. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7767. x = -x;
  7768. }
  7769. pos |= x << CURSOR_X_SHIFT;
  7770. if (y < 0) {
  7771. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7772. y = -y;
  7773. }
  7774. pos |= y << CURSOR_Y_SHIFT;
  7775. /* ILK+ do this automagically */
  7776. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7777. plane_state->base.rotation & DRM_ROTATE_180) {
  7778. base += (plane_state->base.crtc_h *
  7779. plane_state->base.crtc_w - 1) * 4;
  7780. }
  7781. }
  7782. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7783. I915_WRITE_FW(CURPOS(pipe), pos);
  7784. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  7785. i845_update_cursor(crtc, base, plane_state);
  7786. else
  7787. i9xx_update_cursor(crtc, base, plane_state);
  7788. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7789. }
  7790. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  7791. uint32_t width, uint32_t height)
  7792. {
  7793. if (width == 0 || height == 0)
  7794. return false;
  7795. /*
  7796. * 845g/865g are special in that they are only limited by
  7797. * the width of their cursors, the height is arbitrary up to
  7798. * the precision of the register. Everything else requires
  7799. * square cursors, limited to a few power-of-two sizes.
  7800. */
  7801. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  7802. if ((width & 63) != 0)
  7803. return false;
  7804. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  7805. return false;
  7806. if (height > 1023)
  7807. return false;
  7808. } else {
  7809. switch (width | height) {
  7810. case 256:
  7811. case 128:
  7812. if (IS_GEN2(dev_priv))
  7813. return false;
  7814. case 64:
  7815. break;
  7816. default:
  7817. return false;
  7818. }
  7819. }
  7820. return true;
  7821. }
  7822. /* VESA 640x480x72Hz mode to set on the pipe */
  7823. static struct drm_display_mode load_detect_mode = {
  7824. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7825. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7826. };
  7827. struct drm_framebuffer *
  7828. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7829. struct drm_mode_fb_cmd2 *mode_cmd)
  7830. {
  7831. struct intel_framebuffer *intel_fb;
  7832. int ret;
  7833. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7834. if (!intel_fb)
  7835. return ERR_PTR(-ENOMEM);
  7836. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7837. if (ret)
  7838. goto err;
  7839. return &intel_fb->base;
  7840. err:
  7841. kfree(intel_fb);
  7842. return ERR_PTR(ret);
  7843. }
  7844. static u32
  7845. intel_framebuffer_pitch_for_width(int width, int bpp)
  7846. {
  7847. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7848. return ALIGN(pitch, 64);
  7849. }
  7850. static u32
  7851. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7852. {
  7853. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7854. return PAGE_ALIGN(pitch * mode->vdisplay);
  7855. }
  7856. static struct drm_framebuffer *
  7857. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7858. struct drm_display_mode *mode,
  7859. int depth, int bpp)
  7860. {
  7861. struct drm_framebuffer *fb;
  7862. struct drm_i915_gem_object *obj;
  7863. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7864. obj = i915_gem_object_create(to_i915(dev),
  7865. intel_framebuffer_size_for_mode(mode, bpp));
  7866. if (IS_ERR(obj))
  7867. return ERR_CAST(obj);
  7868. mode_cmd.width = mode->hdisplay;
  7869. mode_cmd.height = mode->vdisplay;
  7870. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7871. bpp);
  7872. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7873. fb = intel_framebuffer_create(obj, &mode_cmd);
  7874. if (IS_ERR(fb))
  7875. i915_gem_object_put(obj);
  7876. return fb;
  7877. }
  7878. static struct drm_framebuffer *
  7879. mode_fits_in_fbdev(struct drm_device *dev,
  7880. struct drm_display_mode *mode)
  7881. {
  7882. #ifdef CONFIG_DRM_FBDEV_EMULATION
  7883. struct drm_i915_private *dev_priv = to_i915(dev);
  7884. struct drm_i915_gem_object *obj;
  7885. struct drm_framebuffer *fb;
  7886. if (!dev_priv->fbdev)
  7887. return NULL;
  7888. if (!dev_priv->fbdev->fb)
  7889. return NULL;
  7890. obj = dev_priv->fbdev->fb->obj;
  7891. BUG_ON(!obj);
  7892. fb = &dev_priv->fbdev->fb->base;
  7893. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7894. fb->format->cpp[0] * 8))
  7895. return NULL;
  7896. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7897. return NULL;
  7898. drm_framebuffer_reference(fb);
  7899. return fb;
  7900. #else
  7901. return NULL;
  7902. #endif
  7903. }
  7904. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  7905. struct drm_crtc *crtc,
  7906. struct drm_display_mode *mode,
  7907. struct drm_framebuffer *fb,
  7908. int x, int y)
  7909. {
  7910. struct drm_plane_state *plane_state;
  7911. int hdisplay, vdisplay;
  7912. int ret;
  7913. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  7914. if (IS_ERR(plane_state))
  7915. return PTR_ERR(plane_state);
  7916. if (mode)
  7917. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  7918. else
  7919. hdisplay = vdisplay = 0;
  7920. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  7921. if (ret)
  7922. return ret;
  7923. drm_atomic_set_fb_for_plane(plane_state, fb);
  7924. plane_state->crtc_x = 0;
  7925. plane_state->crtc_y = 0;
  7926. plane_state->crtc_w = hdisplay;
  7927. plane_state->crtc_h = vdisplay;
  7928. plane_state->src_x = x << 16;
  7929. plane_state->src_y = y << 16;
  7930. plane_state->src_w = hdisplay << 16;
  7931. plane_state->src_h = vdisplay << 16;
  7932. return 0;
  7933. }
  7934. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7935. struct drm_display_mode *mode,
  7936. struct intel_load_detect_pipe *old,
  7937. struct drm_modeset_acquire_ctx *ctx)
  7938. {
  7939. struct intel_crtc *intel_crtc;
  7940. struct intel_encoder *intel_encoder =
  7941. intel_attached_encoder(connector);
  7942. struct drm_crtc *possible_crtc;
  7943. struct drm_encoder *encoder = &intel_encoder->base;
  7944. struct drm_crtc *crtc = NULL;
  7945. struct drm_device *dev = encoder->dev;
  7946. struct drm_i915_private *dev_priv = to_i915(dev);
  7947. struct drm_framebuffer *fb;
  7948. struct drm_mode_config *config = &dev->mode_config;
  7949. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  7950. struct drm_connector_state *connector_state;
  7951. struct intel_crtc_state *crtc_state;
  7952. int ret, i = -1;
  7953. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7954. connector->base.id, connector->name,
  7955. encoder->base.id, encoder->name);
  7956. old->restore_state = NULL;
  7957. retry:
  7958. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7959. if (ret)
  7960. goto fail;
  7961. /*
  7962. * Algorithm gets a little messy:
  7963. *
  7964. * - if the connector already has an assigned crtc, use it (but make
  7965. * sure it's on first)
  7966. *
  7967. * - try to find the first unused crtc that can drive this connector,
  7968. * and use that if we find one
  7969. */
  7970. /* See if we already have a CRTC for this connector */
  7971. if (connector->state->crtc) {
  7972. crtc = connector->state->crtc;
  7973. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7974. if (ret)
  7975. goto fail;
  7976. /* Make sure the crtc and connector are running */
  7977. goto found;
  7978. }
  7979. /* Find an unused one (if possible) */
  7980. for_each_crtc(dev, possible_crtc) {
  7981. i++;
  7982. if (!(encoder->possible_crtcs & (1 << i)))
  7983. continue;
  7984. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  7985. if (ret)
  7986. goto fail;
  7987. if (possible_crtc->state->enable) {
  7988. drm_modeset_unlock(&possible_crtc->mutex);
  7989. continue;
  7990. }
  7991. crtc = possible_crtc;
  7992. break;
  7993. }
  7994. /*
  7995. * If we didn't find an unused CRTC, don't use any.
  7996. */
  7997. if (!crtc) {
  7998. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7999. goto fail;
  8000. }
  8001. found:
  8002. intel_crtc = to_intel_crtc(crtc);
  8003. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8004. if (ret)
  8005. goto fail;
  8006. state = drm_atomic_state_alloc(dev);
  8007. restore_state = drm_atomic_state_alloc(dev);
  8008. if (!state || !restore_state) {
  8009. ret = -ENOMEM;
  8010. goto fail;
  8011. }
  8012. state->acquire_ctx = ctx;
  8013. restore_state->acquire_ctx = ctx;
  8014. connector_state = drm_atomic_get_connector_state(state, connector);
  8015. if (IS_ERR(connector_state)) {
  8016. ret = PTR_ERR(connector_state);
  8017. goto fail;
  8018. }
  8019. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8020. if (ret)
  8021. goto fail;
  8022. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8023. if (IS_ERR(crtc_state)) {
  8024. ret = PTR_ERR(crtc_state);
  8025. goto fail;
  8026. }
  8027. crtc_state->base.active = crtc_state->base.enable = true;
  8028. if (!mode)
  8029. mode = &load_detect_mode;
  8030. /* We need a framebuffer large enough to accommodate all accesses
  8031. * that the plane may generate whilst we perform load detection.
  8032. * We can not rely on the fbcon either being present (we get called
  8033. * during its initialisation to detect all boot displays, or it may
  8034. * not even exist) or that it is large enough to satisfy the
  8035. * requested mode.
  8036. */
  8037. fb = mode_fits_in_fbdev(dev, mode);
  8038. if (fb == NULL) {
  8039. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8040. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8041. } else
  8042. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8043. if (IS_ERR(fb)) {
  8044. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8045. goto fail;
  8046. }
  8047. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8048. if (ret)
  8049. goto fail;
  8050. drm_framebuffer_unreference(fb);
  8051. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8052. if (ret)
  8053. goto fail;
  8054. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8055. if (!ret)
  8056. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8057. if (!ret)
  8058. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8059. if (ret) {
  8060. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8061. goto fail;
  8062. }
  8063. ret = drm_atomic_commit(state);
  8064. if (ret) {
  8065. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8066. goto fail;
  8067. }
  8068. old->restore_state = restore_state;
  8069. drm_atomic_state_put(state);
  8070. /* let the connector get through one full cycle before testing */
  8071. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8072. return true;
  8073. fail:
  8074. if (state) {
  8075. drm_atomic_state_put(state);
  8076. state = NULL;
  8077. }
  8078. if (restore_state) {
  8079. drm_atomic_state_put(restore_state);
  8080. restore_state = NULL;
  8081. }
  8082. if (ret == -EDEADLK) {
  8083. drm_modeset_backoff(ctx);
  8084. goto retry;
  8085. }
  8086. return false;
  8087. }
  8088. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8089. struct intel_load_detect_pipe *old,
  8090. struct drm_modeset_acquire_ctx *ctx)
  8091. {
  8092. struct intel_encoder *intel_encoder =
  8093. intel_attached_encoder(connector);
  8094. struct drm_encoder *encoder = &intel_encoder->base;
  8095. struct drm_atomic_state *state = old->restore_state;
  8096. int ret;
  8097. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8098. connector->base.id, connector->name,
  8099. encoder->base.id, encoder->name);
  8100. if (!state)
  8101. return;
  8102. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8103. if (ret)
  8104. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8105. drm_atomic_state_put(state);
  8106. }
  8107. static int i9xx_pll_refclk(struct drm_device *dev,
  8108. const struct intel_crtc_state *pipe_config)
  8109. {
  8110. struct drm_i915_private *dev_priv = to_i915(dev);
  8111. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8112. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8113. return dev_priv->vbt.lvds_ssc_freq;
  8114. else if (HAS_PCH_SPLIT(dev_priv))
  8115. return 120000;
  8116. else if (!IS_GEN2(dev_priv))
  8117. return 96000;
  8118. else
  8119. return 48000;
  8120. }
  8121. /* Returns the clock of the currently programmed mode of the given pipe. */
  8122. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8123. struct intel_crtc_state *pipe_config)
  8124. {
  8125. struct drm_device *dev = crtc->base.dev;
  8126. struct drm_i915_private *dev_priv = to_i915(dev);
  8127. int pipe = pipe_config->cpu_transcoder;
  8128. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8129. u32 fp;
  8130. struct dpll clock;
  8131. int port_clock;
  8132. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8133. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8134. fp = pipe_config->dpll_hw_state.fp0;
  8135. else
  8136. fp = pipe_config->dpll_hw_state.fp1;
  8137. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8138. if (IS_PINEVIEW(dev_priv)) {
  8139. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8140. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8141. } else {
  8142. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8143. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8144. }
  8145. if (!IS_GEN2(dev_priv)) {
  8146. if (IS_PINEVIEW(dev_priv))
  8147. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8148. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8149. else
  8150. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8151. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8152. switch (dpll & DPLL_MODE_MASK) {
  8153. case DPLLB_MODE_DAC_SERIAL:
  8154. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8155. 5 : 10;
  8156. break;
  8157. case DPLLB_MODE_LVDS:
  8158. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8159. 7 : 14;
  8160. break;
  8161. default:
  8162. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8163. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8164. return;
  8165. }
  8166. if (IS_PINEVIEW(dev_priv))
  8167. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8168. else
  8169. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8170. } else {
  8171. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8172. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8173. if (is_lvds) {
  8174. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8175. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8176. if (lvds & LVDS_CLKB_POWER_UP)
  8177. clock.p2 = 7;
  8178. else
  8179. clock.p2 = 14;
  8180. } else {
  8181. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8182. clock.p1 = 2;
  8183. else {
  8184. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8185. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8186. }
  8187. if (dpll & PLL_P2_DIVIDE_BY_4)
  8188. clock.p2 = 4;
  8189. else
  8190. clock.p2 = 2;
  8191. }
  8192. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8193. }
  8194. /*
  8195. * This value includes pixel_multiplier. We will use
  8196. * port_clock to compute adjusted_mode.crtc_clock in the
  8197. * encoder's get_config() function.
  8198. */
  8199. pipe_config->port_clock = port_clock;
  8200. }
  8201. int intel_dotclock_calculate(int link_freq,
  8202. const struct intel_link_m_n *m_n)
  8203. {
  8204. /*
  8205. * The calculation for the data clock is:
  8206. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8207. * But we want to avoid losing precison if possible, so:
  8208. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8209. *
  8210. * and the link clock is simpler:
  8211. * link_clock = (m * link_clock) / n
  8212. */
  8213. if (!m_n->link_n)
  8214. return 0;
  8215. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8216. }
  8217. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8218. struct intel_crtc_state *pipe_config)
  8219. {
  8220. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8221. /* read out port_clock from the DPLL */
  8222. i9xx_crtc_clock_get(crtc, pipe_config);
  8223. /*
  8224. * In case there is an active pipe without active ports,
  8225. * we may need some idea for the dotclock anyway.
  8226. * Calculate one based on the FDI configuration.
  8227. */
  8228. pipe_config->base.adjusted_mode.crtc_clock =
  8229. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8230. &pipe_config->fdi_m_n);
  8231. }
  8232. /** Returns the currently programmed mode of the given pipe. */
  8233. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8234. struct drm_crtc *crtc)
  8235. {
  8236. struct drm_i915_private *dev_priv = to_i915(dev);
  8237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8238. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8239. struct drm_display_mode *mode;
  8240. struct intel_crtc_state *pipe_config;
  8241. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8242. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8243. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8244. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8245. enum pipe pipe = intel_crtc->pipe;
  8246. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8247. if (!mode)
  8248. return NULL;
  8249. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8250. if (!pipe_config) {
  8251. kfree(mode);
  8252. return NULL;
  8253. }
  8254. /*
  8255. * Construct a pipe_config sufficient for getting the clock info
  8256. * back out of crtc_clock_get.
  8257. *
  8258. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8259. * to use a real value here instead.
  8260. */
  8261. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8262. pipe_config->pixel_multiplier = 1;
  8263. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8264. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8265. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8266. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8267. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8268. mode->hdisplay = (htot & 0xffff) + 1;
  8269. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8270. mode->hsync_start = (hsync & 0xffff) + 1;
  8271. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8272. mode->vdisplay = (vtot & 0xffff) + 1;
  8273. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8274. mode->vsync_start = (vsync & 0xffff) + 1;
  8275. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8276. drm_mode_set_name(mode);
  8277. kfree(pipe_config);
  8278. return mode;
  8279. }
  8280. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8281. {
  8282. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8283. struct drm_device *dev = crtc->dev;
  8284. struct intel_flip_work *work;
  8285. spin_lock_irq(&dev->event_lock);
  8286. work = intel_crtc->flip_work;
  8287. intel_crtc->flip_work = NULL;
  8288. spin_unlock_irq(&dev->event_lock);
  8289. if (work) {
  8290. cancel_work_sync(&work->mmio_work);
  8291. cancel_work_sync(&work->unpin_work);
  8292. kfree(work);
  8293. }
  8294. drm_crtc_cleanup(crtc);
  8295. kfree(intel_crtc);
  8296. }
  8297. static void intel_unpin_work_fn(struct work_struct *__work)
  8298. {
  8299. struct intel_flip_work *work =
  8300. container_of(__work, struct intel_flip_work, unpin_work);
  8301. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8302. struct drm_device *dev = crtc->base.dev;
  8303. struct drm_plane *primary = crtc->base.primary;
  8304. if (is_mmio_work(work))
  8305. flush_work(&work->mmio_work);
  8306. mutex_lock(&dev->struct_mutex);
  8307. intel_unpin_fb_vma(work->old_vma);
  8308. i915_gem_object_put(work->pending_flip_obj);
  8309. mutex_unlock(&dev->struct_mutex);
  8310. i915_gem_request_put(work->flip_queued_req);
  8311. intel_frontbuffer_flip_complete(to_i915(dev),
  8312. to_intel_plane(primary)->frontbuffer_bit);
  8313. intel_fbc_post_update(crtc);
  8314. drm_framebuffer_unreference(work->old_fb);
  8315. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8316. atomic_dec(&crtc->unpin_work_count);
  8317. kfree(work);
  8318. }
  8319. /* Is 'a' after or equal to 'b'? */
  8320. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8321. {
  8322. return !((a - b) & 0x80000000);
  8323. }
  8324. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8325. struct intel_flip_work *work)
  8326. {
  8327. struct drm_device *dev = crtc->base.dev;
  8328. struct drm_i915_private *dev_priv = to_i915(dev);
  8329. if (abort_flip_on_reset(crtc))
  8330. return true;
  8331. /*
  8332. * The relevant registers doen't exist on pre-ctg.
  8333. * As the flip done interrupt doesn't trigger for mmio
  8334. * flips on gmch platforms, a flip count check isn't
  8335. * really needed there. But since ctg has the registers,
  8336. * include it in the check anyway.
  8337. */
  8338. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8339. return true;
  8340. /*
  8341. * BDW signals flip done immediately if the plane
  8342. * is disabled, even if the plane enable is already
  8343. * armed to occur at the next vblank :(
  8344. */
  8345. /*
  8346. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8347. * used the same base address. In that case the mmio flip might
  8348. * have completed, but the CS hasn't even executed the flip yet.
  8349. *
  8350. * A flip count check isn't enough as the CS might have updated
  8351. * the base address just after start of vblank, but before we
  8352. * managed to process the interrupt. This means we'd complete the
  8353. * CS flip too soon.
  8354. *
  8355. * Combining both checks should get us a good enough result. It may
  8356. * still happen that the CS flip has been executed, but has not
  8357. * yet actually completed. But in case the base address is the same
  8358. * anyway, we don't really care.
  8359. */
  8360. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8361. crtc->flip_work->gtt_offset &&
  8362. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8363. crtc->flip_work->flip_count);
  8364. }
  8365. static bool
  8366. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8367. struct intel_flip_work *work)
  8368. {
  8369. /*
  8370. * MMIO work completes when vblank is different from
  8371. * flip_queued_vblank.
  8372. *
  8373. * Reset counter value doesn't matter, this is handled by
  8374. * i915_wait_request finishing early, so no need to handle
  8375. * reset here.
  8376. */
  8377. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8378. }
  8379. static bool pageflip_finished(struct intel_crtc *crtc,
  8380. struct intel_flip_work *work)
  8381. {
  8382. if (!atomic_read(&work->pending))
  8383. return false;
  8384. smp_rmb();
  8385. if (is_mmio_work(work))
  8386. return __pageflip_finished_mmio(crtc, work);
  8387. else
  8388. return __pageflip_finished_cs(crtc, work);
  8389. }
  8390. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8391. {
  8392. struct drm_device *dev = &dev_priv->drm;
  8393. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8394. struct intel_flip_work *work;
  8395. unsigned long flags;
  8396. /* Ignore early vblank irqs */
  8397. if (!crtc)
  8398. return;
  8399. /*
  8400. * This is called both by irq handlers and the reset code (to complete
  8401. * lost pageflips) so needs the full irqsave spinlocks.
  8402. */
  8403. spin_lock_irqsave(&dev->event_lock, flags);
  8404. work = crtc->flip_work;
  8405. if (work != NULL &&
  8406. !is_mmio_work(work) &&
  8407. pageflip_finished(crtc, work))
  8408. page_flip_completed(crtc);
  8409. spin_unlock_irqrestore(&dev->event_lock, flags);
  8410. }
  8411. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8412. {
  8413. struct drm_device *dev = &dev_priv->drm;
  8414. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8415. struct intel_flip_work *work;
  8416. unsigned long flags;
  8417. /* Ignore early vblank irqs */
  8418. if (!crtc)
  8419. return;
  8420. /*
  8421. * This is called both by irq handlers and the reset code (to complete
  8422. * lost pageflips) so needs the full irqsave spinlocks.
  8423. */
  8424. spin_lock_irqsave(&dev->event_lock, flags);
  8425. work = crtc->flip_work;
  8426. if (work != NULL &&
  8427. is_mmio_work(work) &&
  8428. pageflip_finished(crtc, work))
  8429. page_flip_completed(crtc);
  8430. spin_unlock_irqrestore(&dev->event_lock, flags);
  8431. }
  8432. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8433. struct intel_flip_work *work)
  8434. {
  8435. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8436. /* Ensure that the work item is consistent when activating it ... */
  8437. smp_mb__before_atomic();
  8438. atomic_set(&work->pending, 1);
  8439. }
  8440. static int intel_gen2_queue_flip(struct drm_device *dev,
  8441. struct drm_crtc *crtc,
  8442. struct drm_framebuffer *fb,
  8443. struct drm_i915_gem_object *obj,
  8444. struct drm_i915_gem_request *req,
  8445. uint32_t flags)
  8446. {
  8447. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8448. u32 flip_mask, *cs;
  8449. cs = intel_ring_begin(req, 6);
  8450. if (IS_ERR(cs))
  8451. return PTR_ERR(cs);
  8452. /* Can't queue multiple flips, so wait for the previous
  8453. * one to finish before executing the next.
  8454. */
  8455. if (intel_crtc->plane)
  8456. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8457. else
  8458. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8459. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8460. *cs++ = MI_NOOP;
  8461. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8462. *cs++ = fb->pitches[0];
  8463. *cs++ = intel_crtc->flip_work->gtt_offset;
  8464. *cs++ = 0; /* aux display base address, unused */
  8465. return 0;
  8466. }
  8467. static int intel_gen3_queue_flip(struct drm_device *dev,
  8468. struct drm_crtc *crtc,
  8469. struct drm_framebuffer *fb,
  8470. struct drm_i915_gem_object *obj,
  8471. struct drm_i915_gem_request *req,
  8472. uint32_t flags)
  8473. {
  8474. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8475. u32 flip_mask, *cs;
  8476. cs = intel_ring_begin(req, 6);
  8477. if (IS_ERR(cs))
  8478. return PTR_ERR(cs);
  8479. if (intel_crtc->plane)
  8480. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8481. else
  8482. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8483. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8484. *cs++ = MI_NOOP;
  8485. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8486. *cs++ = fb->pitches[0];
  8487. *cs++ = intel_crtc->flip_work->gtt_offset;
  8488. *cs++ = MI_NOOP;
  8489. return 0;
  8490. }
  8491. static int intel_gen4_queue_flip(struct drm_device *dev,
  8492. struct drm_crtc *crtc,
  8493. struct drm_framebuffer *fb,
  8494. struct drm_i915_gem_object *obj,
  8495. struct drm_i915_gem_request *req,
  8496. uint32_t flags)
  8497. {
  8498. struct drm_i915_private *dev_priv = to_i915(dev);
  8499. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8500. u32 pf, pipesrc, *cs;
  8501. cs = intel_ring_begin(req, 4);
  8502. if (IS_ERR(cs))
  8503. return PTR_ERR(cs);
  8504. /* i965+ uses the linear or tiled offsets from the
  8505. * Display Registers (which do not change across a page-flip)
  8506. * so we need only reprogram the base address.
  8507. */
  8508. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8509. *cs++ = fb->pitches[0];
  8510. *cs++ = intel_crtc->flip_work->gtt_offset |
  8511. intel_fb_modifier_to_tiling(fb->modifier);
  8512. /* XXX Enabling the panel-fitter across page-flip is so far
  8513. * untested on non-native modes, so ignore it for now.
  8514. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8515. */
  8516. pf = 0;
  8517. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8518. *cs++ = pf | pipesrc;
  8519. return 0;
  8520. }
  8521. static int intel_gen6_queue_flip(struct drm_device *dev,
  8522. struct drm_crtc *crtc,
  8523. struct drm_framebuffer *fb,
  8524. struct drm_i915_gem_object *obj,
  8525. struct drm_i915_gem_request *req,
  8526. uint32_t flags)
  8527. {
  8528. struct drm_i915_private *dev_priv = to_i915(dev);
  8529. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8530. u32 pf, pipesrc, *cs;
  8531. cs = intel_ring_begin(req, 4);
  8532. if (IS_ERR(cs))
  8533. return PTR_ERR(cs);
  8534. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8535. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8536. *cs++ = intel_crtc->flip_work->gtt_offset;
  8537. /* Contrary to the suggestions in the documentation,
  8538. * "Enable Panel Fitter" does not seem to be required when page
  8539. * flipping with a non-native mode, and worse causes a normal
  8540. * modeset to fail.
  8541. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8542. */
  8543. pf = 0;
  8544. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8545. *cs++ = pf | pipesrc;
  8546. return 0;
  8547. }
  8548. static int intel_gen7_queue_flip(struct drm_device *dev,
  8549. struct drm_crtc *crtc,
  8550. struct drm_framebuffer *fb,
  8551. struct drm_i915_gem_object *obj,
  8552. struct drm_i915_gem_request *req,
  8553. uint32_t flags)
  8554. {
  8555. struct drm_i915_private *dev_priv = to_i915(dev);
  8556. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8557. u32 *cs, plane_bit = 0;
  8558. int len, ret;
  8559. switch (intel_crtc->plane) {
  8560. case PLANE_A:
  8561. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8562. break;
  8563. case PLANE_B:
  8564. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8565. break;
  8566. case PLANE_C:
  8567. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8568. break;
  8569. default:
  8570. WARN_ONCE(1, "unknown plane in flip command\n");
  8571. return -ENODEV;
  8572. }
  8573. len = 4;
  8574. if (req->engine->id == RCS) {
  8575. len += 6;
  8576. /*
  8577. * On Gen 8, SRM is now taking an extra dword to accommodate
  8578. * 48bits addresses, and we need a NOOP for the batch size to
  8579. * stay even.
  8580. */
  8581. if (IS_GEN8(dev_priv))
  8582. len += 2;
  8583. }
  8584. /*
  8585. * BSpec MI_DISPLAY_FLIP for IVB:
  8586. * "The full packet must be contained within the same cache line."
  8587. *
  8588. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8589. * cacheline, if we ever start emitting more commands before
  8590. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8591. * then do the cacheline alignment, and finally emit the
  8592. * MI_DISPLAY_FLIP.
  8593. */
  8594. ret = intel_ring_cacheline_align(req);
  8595. if (ret)
  8596. return ret;
  8597. cs = intel_ring_begin(req, len);
  8598. if (IS_ERR(cs))
  8599. return PTR_ERR(cs);
  8600. /* Unmask the flip-done completion message. Note that the bspec says that
  8601. * we should do this for both the BCS and RCS, and that we must not unmask
  8602. * more than one flip event at any time (or ensure that one flip message
  8603. * can be sent by waiting for flip-done prior to queueing new flips).
  8604. * Experimentation says that BCS works despite DERRMR masking all
  8605. * flip-done completion events and that unmasking all planes at once
  8606. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8607. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8608. */
  8609. if (req->engine->id == RCS) {
  8610. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8611. *cs++ = i915_mmio_reg_offset(DERRMR);
  8612. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8613. DERRMR_PIPEB_PRI_FLIP_DONE |
  8614. DERRMR_PIPEC_PRI_FLIP_DONE);
  8615. if (IS_GEN8(dev_priv))
  8616. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8617. MI_SRM_LRM_GLOBAL_GTT;
  8618. else
  8619. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8620. *cs++ = i915_mmio_reg_offset(DERRMR);
  8621. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8622. if (IS_GEN8(dev_priv)) {
  8623. *cs++ = 0;
  8624. *cs++ = MI_NOOP;
  8625. }
  8626. }
  8627. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8628. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8629. *cs++ = intel_crtc->flip_work->gtt_offset;
  8630. *cs++ = MI_NOOP;
  8631. return 0;
  8632. }
  8633. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8634. struct drm_i915_gem_object *obj)
  8635. {
  8636. /*
  8637. * This is not being used for older platforms, because
  8638. * non-availability of flip done interrupt forces us to use
  8639. * CS flips. Older platforms derive flip done using some clever
  8640. * tricks involving the flip_pending status bits and vblank irqs.
  8641. * So using MMIO flips there would disrupt this mechanism.
  8642. */
  8643. if (engine == NULL)
  8644. return true;
  8645. if (INTEL_GEN(engine->i915) < 5)
  8646. return false;
  8647. if (i915.use_mmio_flip < 0)
  8648. return false;
  8649. else if (i915.use_mmio_flip > 0)
  8650. return true;
  8651. else if (i915.enable_execlists)
  8652. return true;
  8653. return engine != i915_gem_object_last_write_engine(obj);
  8654. }
  8655. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8656. unsigned int rotation,
  8657. struct intel_flip_work *work)
  8658. {
  8659. struct drm_device *dev = intel_crtc->base.dev;
  8660. struct drm_i915_private *dev_priv = to_i915(dev);
  8661. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8662. const enum pipe pipe = intel_crtc->pipe;
  8663. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8664. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8665. ctl &= ~PLANE_CTL_TILED_MASK;
  8666. switch (fb->modifier) {
  8667. case DRM_FORMAT_MOD_NONE:
  8668. break;
  8669. case I915_FORMAT_MOD_X_TILED:
  8670. ctl |= PLANE_CTL_TILED_X;
  8671. break;
  8672. case I915_FORMAT_MOD_Y_TILED:
  8673. ctl |= PLANE_CTL_TILED_Y;
  8674. break;
  8675. case I915_FORMAT_MOD_Yf_TILED:
  8676. ctl |= PLANE_CTL_TILED_YF;
  8677. break;
  8678. default:
  8679. MISSING_CASE(fb->modifier);
  8680. }
  8681. /*
  8682. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8683. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8684. */
  8685. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8686. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8687. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8688. POSTING_READ(PLANE_SURF(pipe, 0));
  8689. }
  8690. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8691. struct intel_flip_work *work)
  8692. {
  8693. struct drm_device *dev = intel_crtc->base.dev;
  8694. struct drm_i915_private *dev_priv = to_i915(dev);
  8695. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8696. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8697. u32 dspcntr;
  8698. dspcntr = I915_READ(reg);
  8699. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8700. dspcntr |= DISPPLANE_TILED;
  8701. else
  8702. dspcntr &= ~DISPPLANE_TILED;
  8703. I915_WRITE(reg, dspcntr);
  8704. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8705. POSTING_READ(DSPSURF(intel_crtc->plane));
  8706. }
  8707. static void intel_mmio_flip_work_func(struct work_struct *w)
  8708. {
  8709. struct intel_flip_work *work =
  8710. container_of(w, struct intel_flip_work, mmio_work);
  8711. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8712. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8713. struct intel_framebuffer *intel_fb =
  8714. to_intel_framebuffer(crtc->base.primary->fb);
  8715. struct drm_i915_gem_object *obj = intel_fb->obj;
  8716. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8717. intel_pipe_update_start(crtc);
  8718. if (INTEL_GEN(dev_priv) >= 9)
  8719. skl_do_mmio_flip(crtc, work->rotation, work);
  8720. else
  8721. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8722. ilk_do_mmio_flip(crtc, work);
  8723. intel_pipe_update_end(crtc, work);
  8724. }
  8725. static int intel_default_queue_flip(struct drm_device *dev,
  8726. struct drm_crtc *crtc,
  8727. struct drm_framebuffer *fb,
  8728. struct drm_i915_gem_object *obj,
  8729. struct drm_i915_gem_request *req,
  8730. uint32_t flags)
  8731. {
  8732. return -ENODEV;
  8733. }
  8734. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8735. struct intel_crtc *intel_crtc,
  8736. struct intel_flip_work *work)
  8737. {
  8738. u32 addr, vblank;
  8739. if (!atomic_read(&work->pending))
  8740. return false;
  8741. smp_rmb();
  8742. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8743. if (work->flip_ready_vblank == 0) {
  8744. if (work->flip_queued_req &&
  8745. !i915_gem_request_completed(work->flip_queued_req))
  8746. return false;
  8747. work->flip_ready_vblank = vblank;
  8748. }
  8749. if (vblank - work->flip_ready_vblank < 3)
  8750. return false;
  8751. /* Potential stall - if we see that the flip has happened,
  8752. * assume a missed interrupt. */
  8753. if (INTEL_GEN(dev_priv) >= 4)
  8754. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8755. else
  8756. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8757. /* There is a potential issue here with a false positive after a flip
  8758. * to the same address. We could address this by checking for a
  8759. * non-incrementing frame counter.
  8760. */
  8761. return addr == work->gtt_offset;
  8762. }
  8763. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8764. {
  8765. struct drm_device *dev = &dev_priv->drm;
  8766. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8767. struct intel_flip_work *work;
  8768. WARN_ON(!in_interrupt());
  8769. if (crtc == NULL)
  8770. return;
  8771. spin_lock(&dev->event_lock);
  8772. work = crtc->flip_work;
  8773. if (work != NULL && !is_mmio_work(work) &&
  8774. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8775. WARN_ONCE(1,
  8776. "Kicking stuck page flip: queued at %d, now %d\n",
  8777. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8778. page_flip_completed(crtc);
  8779. work = NULL;
  8780. }
  8781. if (work != NULL && !is_mmio_work(work) &&
  8782. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8783. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8784. spin_unlock(&dev->event_lock);
  8785. }
  8786. __maybe_unused
  8787. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8788. struct drm_framebuffer *fb,
  8789. struct drm_pending_vblank_event *event,
  8790. uint32_t page_flip_flags)
  8791. {
  8792. struct drm_device *dev = crtc->dev;
  8793. struct drm_i915_private *dev_priv = to_i915(dev);
  8794. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8795. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8796. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8797. struct drm_plane *primary = crtc->primary;
  8798. enum pipe pipe = intel_crtc->pipe;
  8799. struct intel_flip_work *work;
  8800. struct intel_engine_cs *engine;
  8801. bool mmio_flip;
  8802. struct drm_i915_gem_request *request;
  8803. struct i915_vma *vma;
  8804. int ret;
  8805. /*
  8806. * drm_mode_page_flip_ioctl() should already catch this, but double
  8807. * check to be safe. In the future we may enable pageflipping from
  8808. * a disabled primary plane.
  8809. */
  8810. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8811. return -EBUSY;
  8812. /* Can't change pixel format via MI display flips. */
  8813. if (fb->format != crtc->primary->fb->format)
  8814. return -EINVAL;
  8815. /*
  8816. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8817. * Note that pitch changes could also affect these register.
  8818. */
  8819. if (INTEL_GEN(dev_priv) > 3 &&
  8820. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8821. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8822. return -EINVAL;
  8823. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8824. goto out_hang;
  8825. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8826. if (work == NULL)
  8827. return -ENOMEM;
  8828. work->event = event;
  8829. work->crtc = crtc;
  8830. work->old_fb = old_fb;
  8831. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8832. ret = drm_crtc_vblank_get(crtc);
  8833. if (ret)
  8834. goto free_work;
  8835. /* We borrow the event spin lock for protecting flip_work */
  8836. spin_lock_irq(&dev->event_lock);
  8837. if (intel_crtc->flip_work) {
  8838. /* Before declaring the flip queue wedged, check if
  8839. * the hardware completed the operation behind our backs.
  8840. */
  8841. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8842. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8843. page_flip_completed(intel_crtc);
  8844. } else {
  8845. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8846. spin_unlock_irq(&dev->event_lock);
  8847. drm_crtc_vblank_put(crtc);
  8848. kfree(work);
  8849. return -EBUSY;
  8850. }
  8851. }
  8852. intel_crtc->flip_work = work;
  8853. spin_unlock_irq(&dev->event_lock);
  8854. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8855. flush_workqueue(dev_priv->wq);
  8856. /* Reference the objects for the scheduled work. */
  8857. drm_framebuffer_reference(work->old_fb);
  8858. crtc->primary->fb = fb;
  8859. update_state_fb(crtc->primary);
  8860. work->pending_flip_obj = i915_gem_object_get(obj);
  8861. ret = i915_mutex_lock_interruptible(dev);
  8862. if (ret)
  8863. goto cleanup;
  8864. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  8865. if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
  8866. ret = -EIO;
  8867. goto unlock;
  8868. }
  8869. atomic_inc(&intel_crtc->unpin_work_count);
  8870. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8871. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  8872. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8873. engine = dev_priv->engine[BCS];
  8874. if (fb->modifier != old_fb->modifier)
  8875. /* vlv: DISPLAY_FLIP fails to change tiling */
  8876. engine = NULL;
  8877. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  8878. engine = dev_priv->engine[BCS];
  8879. } else if (INTEL_GEN(dev_priv) >= 7) {
  8880. engine = i915_gem_object_last_write_engine(obj);
  8881. if (engine == NULL || engine->id != RCS)
  8882. engine = dev_priv->engine[BCS];
  8883. } else {
  8884. engine = dev_priv->engine[RCS];
  8885. }
  8886. mmio_flip = use_mmio_flip(engine, obj);
  8887. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  8888. if (IS_ERR(vma)) {
  8889. ret = PTR_ERR(vma);
  8890. goto cleanup_pending;
  8891. }
  8892. work->old_vma = to_intel_plane_state(primary->state)->vma;
  8893. to_intel_plane_state(primary->state)->vma = vma;
  8894. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  8895. work->rotation = crtc->primary->state->rotation;
  8896. /*
  8897. * There's the potential that the next frame will not be compatible with
  8898. * FBC, so we want to call pre_update() before the actual page flip.
  8899. * The problem is that pre_update() caches some information about the fb
  8900. * object, so we want to do this only after the object is pinned. Let's
  8901. * be on the safe side and do this immediately before scheduling the
  8902. * flip.
  8903. */
  8904. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  8905. to_intel_plane_state(primary->state));
  8906. if (mmio_flip) {
  8907. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  8908. queue_work(system_unbound_wq, &work->mmio_work);
  8909. } else {
  8910. request = i915_gem_request_alloc(engine,
  8911. dev_priv->kernel_context);
  8912. if (IS_ERR(request)) {
  8913. ret = PTR_ERR(request);
  8914. goto cleanup_unpin;
  8915. }
  8916. ret = i915_gem_request_await_object(request, obj, false);
  8917. if (ret)
  8918. goto cleanup_request;
  8919. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  8920. page_flip_flags);
  8921. if (ret)
  8922. goto cleanup_request;
  8923. intel_mark_page_flip_active(intel_crtc, work);
  8924. work->flip_queued_req = i915_gem_request_get(request);
  8925. i915_add_request(request);
  8926. }
  8927. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  8928. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  8929. to_intel_plane(primary)->frontbuffer_bit);
  8930. mutex_unlock(&dev->struct_mutex);
  8931. intel_frontbuffer_flip_prepare(to_i915(dev),
  8932. to_intel_plane(primary)->frontbuffer_bit);
  8933. trace_i915_flip_request(intel_crtc->plane, obj);
  8934. return 0;
  8935. cleanup_request:
  8936. i915_add_request(request);
  8937. cleanup_unpin:
  8938. to_intel_plane_state(primary->state)->vma = work->old_vma;
  8939. intel_unpin_fb_vma(vma);
  8940. cleanup_pending:
  8941. atomic_dec(&intel_crtc->unpin_work_count);
  8942. unlock:
  8943. mutex_unlock(&dev->struct_mutex);
  8944. cleanup:
  8945. crtc->primary->fb = old_fb;
  8946. update_state_fb(crtc->primary);
  8947. i915_gem_object_put(obj);
  8948. drm_framebuffer_unreference(work->old_fb);
  8949. spin_lock_irq(&dev->event_lock);
  8950. intel_crtc->flip_work = NULL;
  8951. spin_unlock_irq(&dev->event_lock);
  8952. drm_crtc_vblank_put(crtc);
  8953. free_work:
  8954. kfree(work);
  8955. if (ret == -EIO) {
  8956. struct drm_atomic_state *state;
  8957. struct drm_plane_state *plane_state;
  8958. out_hang:
  8959. state = drm_atomic_state_alloc(dev);
  8960. if (!state)
  8961. return -ENOMEM;
  8962. state->acquire_ctx = dev->mode_config.acquire_ctx;
  8963. retry:
  8964. plane_state = drm_atomic_get_plane_state(state, primary);
  8965. ret = PTR_ERR_OR_ZERO(plane_state);
  8966. if (!ret) {
  8967. drm_atomic_set_fb_for_plane(plane_state, fb);
  8968. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  8969. if (!ret)
  8970. ret = drm_atomic_commit(state);
  8971. }
  8972. if (ret == -EDEADLK) {
  8973. drm_modeset_backoff(state->acquire_ctx);
  8974. drm_atomic_state_clear(state);
  8975. goto retry;
  8976. }
  8977. drm_atomic_state_put(state);
  8978. if (ret == 0 && event) {
  8979. spin_lock_irq(&dev->event_lock);
  8980. drm_crtc_send_vblank_event(crtc, event);
  8981. spin_unlock_irq(&dev->event_lock);
  8982. }
  8983. }
  8984. return ret;
  8985. }
  8986. /**
  8987. * intel_wm_need_update - Check whether watermarks need updating
  8988. * @plane: drm plane
  8989. * @state: new plane state
  8990. *
  8991. * Check current plane state versus the new one to determine whether
  8992. * watermarks need to be recalculated.
  8993. *
  8994. * Returns true or false.
  8995. */
  8996. static bool intel_wm_need_update(struct drm_plane *plane,
  8997. struct drm_plane_state *state)
  8998. {
  8999. struct intel_plane_state *new = to_intel_plane_state(state);
  9000. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9001. /* Update watermarks on tiling or size changes. */
  9002. if (new->base.visible != cur->base.visible)
  9003. return true;
  9004. if (!cur->base.fb || !new->base.fb)
  9005. return false;
  9006. if (cur->base.fb->modifier != new->base.fb->modifier ||
  9007. cur->base.rotation != new->base.rotation ||
  9008. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  9009. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  9010. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  9011. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  9012. return true;
  9013. return false;
  9014. }
  9015. static bool needs_scaling(struct intel_plane_state *state)
  9016. {
  9017. int src_w = drm_rect_width(&state->base.src) >> 16;
  9018. int src_h = drm_rect_height(&state->base.src) >> 16;
  9019. int dst_w = drm_rect_width(&state->base.dst);
  9020. int dst_h = drm_rect_height(&state->base.dst);
  9021. return (src_w != dst_w || src_h != dst_h);
  9022. }
  9023. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9024. struct drm_plane_state *plane_state)
  9025. {
  9026. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9027. struct drm_crtc *crtc = crtc_state->crtc;
  9028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9029. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  9030. struct drm_device *dev = crtc->dev;
  9031. struct drm_i915_private *dev_priv = to_i915(dev);
  9032. struct intel_plane_state *old_plane_state =
  9033. to_intel_plane_state(plane->base.state);
  9034. bool mode_changed = needs_modeset(crtc_state);
  9035. bool was_crtc_enabled = crtc->state->active;
  9036. bool is_crtc_enabled = crtc_state->active;
  9037. bool turn_off, turn_on, visible, was_visible;
  9038. struct drm_framebuffer *fb = plane_state->fb;
  9039. int ret;
  9040. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  9041. ret = skl_update_scaler_plane(
  9042. to_intel_crtc_state(crtc_state),
  9043. to_intel_plane_state(plane_state));
  9044. if (ret)
  9045. return ret;
  9046. }
  9047. was_visible = old_plane_state->base.visible;
  9048. visible = plane_state->visible;
  9049. if (!was_crtc_enabled && WARN_ON(was_visible))
  9050. was_visible = false;
  9051. /*
  9052. * Visibility is calculated as if the crtc was on, but
  9053. * after scaler setup everything depends on it being off
  9054. * when the crtc isn't active.
  9055. *
  9056. * FIXME this is wrong for watermarks. Watermarks should also
  9057. * be computed as if the pipe would be active. Perhaps move
  9058. * per-plane wm computation to the .check_plane() hook, and
  9059. * only combine the results from all planes in the current place?
  9060. */
  9061. if (!is_crtc_enabled) {
  9062. plane_state->visible = visible = false;
  9063. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  9064. }
  9065. if (!was_visible && !visible)
  9066. return 0;
  9067. if (fb != old_plane_state->base.fb)
  9068. pipe_config->fb_changed = true;
  9069. turn_off = was_visible && (!visible || mode_changed);
  9070. turn_on = visible && (!was_visible || mode_changed);
  9071. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9072. intel_crtc->base.base.id, intel_crtc->base.name,
  9073. plane->base.base.id, plane->base.name,
  9074. fb ? fb->base.id : -1);
  9075. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9076. plane->base.base.id, plane->base.name,
  9077. was_visible, visible,
  9078. turn_off, turn_on, mode_changed);
  9079. if (turn_on) {
  9080. if (INTEL_GEN(dev_priv) < 5)
  9081. pipe_config->update_wm_pre = true;
  9082. /* must disable cxsr around plane enable/disable */
  9083. if (plane->id != PLANE_CURSOR)
  9084. pipe_config->disable_cxsr = true;
  9085. } else if (turn_off) {
  9086. if (INTEL_GEN(dev_priv) < 5)
  9087. pipe_config->update_wm_post = true;
  9088. /* must disable cxsr around plane enable/disable */
  9089. if (plane->id != PLANE_CURSOR)
  9090. pipe_config->disable_cxsr = true;
  9091. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  9092. if (INTEL_GEN(dev_priv) < 5) {
  9093. /* FIXME bollocks */
  9094. pipe_config->update_wm_pre = true;
  9095. pipe_config->update_wm_post = true;
  9096. }
  9097. }
  9098. if (visible || was_visible)
  9099. pipe_config->fb_bits |= plane->frontbuffer_bit;
  9100. /*
  9101. * WaCxSRDisabledForSpriteScaling:ivb
  9102. *
  9103. * cstate->update_wm was already set above, so this flag will
  9104. * take effect when we commit and program watermarks.
  9105. */
  9106. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  9107. needs_scaling(to_intel_plane_state(plane_state)) &&
  9108. !needs_scaling(old_plane_state))
  9109. pipe_config->disable_lp_wm = true;
  9110. return 0;
  9111. }
  9112. static bool encoders_cloneable(const struct intel_encoder *a,
  9113. const struct intel_encoder *b)
  9114. {
  9115. /* masks could be asymmetric, so check both ways */
  9116. return a == b || (a->cloneable & (1 << b->type) &&
  9117. b->cloneable & (1 << a->type));
  9118. }
  9119. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9120. struct intel_crtc *crtc,
  9121. struct intel_encoder *encoder)
  9122. {
  9123. struct intel_encoder *source_encoder;
  9124. struct drm_connector *connector;
  9125. struct drm_connector_state *connector_state;
  9126. int i;
  9127. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9128. if (connector_state->crtc != &crtc->base)
  9129. continue;
  9130. source_encoder =
  9131. to_intel_encoder(connector_state->best_encoder);
  9132. if (!encoders_cloneable(encoder, source_encoder))
  9133. return false;
  9134. }
  9135. return true;
  9136. }
  9137. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9138. struct drm_crtc_state *crtc_state)
  9139. {
  9140. struct drm_device *dev = crtc->dev;
  9141. struct drm_i915_private *dev_priv = to_i915(dev);
  9142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9143. struct intel_crtc_state *pipe_config =
  9144. to_intel_crtc_state(crtc_state);
  9145. struct drm_atomic_state *state = crtc_state->state;
  9146. int ret;
  9147. bool mode_changed = needs_modeset(crtc_state);
  9148. if (mode_changed && !crtc_state->active)
  9149. pipe_config->update_wm_post = true;
  9150. if (mode_changed && crtc_state->enable &&
  9151. dev_priv->display.crtc_compute_clock &&
  9152. !WARN_ON(pipe_config->shared_dpll)) {
  9153. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9154. pipe_config);
  9155. if (ret)
  9156. return ret;
  9157. }
  9158. if (crtc_state->color_mgmt_changed) {
  9159. ret = intel_color_check(crtc, crtc_state);
  9160. if (ret)
  9161. return ret;
  9162. /*
  9163. * Changing color management on Intel hardware is
  9164. * handled as part of planes update.
  9165. */
  9166. crtc_state->planes_changed = true;
  9167. }
  9168. ret = 0;
  9169. if (dev_priv->display.compute_pipe_wm) {
  9170. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9171. if (ret) {
  9172. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9173. return ret;
  9174. }
  9175. }
  9176. if (dev_priv->display.compute_intermediate_wm &&
  9177. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9178. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9179. return 0;
  9180. /*
  9181. * Calculate 'intermediate' watermarks that satisfy both the
  9182. * old state and the new state. We can program these
  9183. * immediately.
  9184. */
  9185. ret = dev_priv->display.compute_intermediate_wm(dev,
  9186. intel_crtc,
  9187. pipe_config);
  9188. if (ret) {
  9189. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9190. return ret;
  9191. }
  9192. } else if (dev_priv->display.compute_intermediate_wm) {
  9193. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9194. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9195. }
  9196. if (INTEL_GEN(dev_priv) >= 9) {
  9197. if (mode_changed)
  9198. ret = skl_update_scaler_crtc(pipe_config);
  9199. if (!ret)
  9200. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9201. pipe_config);
  9202. }
  9203. return ret;
  9204. }
  9205. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9206. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9207. .atomic_begin = intel_begin_crtc_commit,
  9208. .atomic_flush = intel_finish_crtc_commit,
  9209. .atomic_check = intel_crtc_atomic_check,
  9210. };
  9211. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9212. {
  9213. struct intel_connector *connector;
  9214. struct drm_connector_list_iter conn_iter;
  9215. drm_connector_list_iter_begin(dev, &conn_iter);
  9216. for_each_intel_connector_iter(connector, &conn_iter) {
  9217. if (connector->base.state->crtc)
  9218. drm_connector_unreference(&connector->base);
  9219. if (connector->base.encoder) {
  9220. connector->base.state->best_encoder =
  9221. connector->base.encoder;
  9222. connector->base.state->crtc =
  9223. connector->base.encoder->crtc;
  9224. drm_connector_reference(&connector->base);
  9225. } else {
  9226. connector->base.state->best_encoder = NULL;
  9227. connector->base.state->crtc = NULL;
  9228. }
  9229. }
  9230. drm_connector_list_iter_end(&conn_iter);
  9231. }
  9232. static void
  9233. connected_sink_compute_bpp(struct intel_connector *connector,
  9234. struct intel_crtc_state *pipe_config)
  9235. {
  9236. const struct drm_display_info *info = &connector->base.display_info;
  9237. int bpp = pipe_config->pipe_bpp;
  9238. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9239. connector->base.base.id,
  9240. connector->base.name);
  9241. /* Don't use an invalid EDID bpc value */
  9242. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9243. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9244. bpp, info->bpc * 3);
  9245. pipe_config->pipe_bpp = info->bpc * 3;
  9246. }
  9247. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9248. if (info->bpc == 0 && bpp > 24) {
  9249. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9250. bpp);
  9251. pipe_config->pipe_bpp = 24;
  9252. }
  9253. }
  9254. static int
  9255. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9256. struct intel_crtc_state *pipe_config)
  9257. {
  9258. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9259. struct drm_atomic_state *state;
  9260. struct drm_connector *connector;
  9261. struct drm_connector_state *connector_state;
  9262. int bpp, i;
  9263. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9264. IS_CHERRYVIEW(dev_priv)))
  9265. bpp = 10*3;
  9266. else if (INTEL_GEN(dev_priv) >= 5)
  9267. bpp = 12*3;
  9268. else
  9269. bpp = 8*3;
  9270. pipe_config->pipe_bpp = bpp;
  9271. state = pipe_config->base.state;
  9272. /* Clamp display bpp to EDID value */
  9273. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9274. if (connector_state->crtc != &crtc->base)
  9275. continue;
  9276. connected_sink_compute_bpp(to_intel_connector(connector),
  9277. pipe_config);
  9278. }
  9279. return bpp;
  9280. }
  9281. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9282. {
  9283. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9284. "type: 0x%x flags: 0x%x\n",
  9285. mode->crtc_clock,
  9286. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9287. mode->crtc_hsync_end, mode->crtc_htotal,
  9288. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9289. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9290. }
  9291. static inline void
  9292. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9293. unsigned int lane_count, struct intel_link_m_n *m_n)
  9294. {
  9295. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9296. id, lane_count,
  9297. m_n->gmch_m, m_n->gmch_n,
  9298. m_n->link_m, m_n->link_n, m_n->tu);
  9299. }
  9300. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9301. struct intel_crtc_state *pipe_config,
  9302. const char *context)
  9303. {
  9304. struct drm_device *dev = crtc->base.dev;
  9305. struct drm_i915_private *dev_priv = to_i915(dev);
  9306. struct drm_plane *plane;
  9307. struct intel_plane *intel_plane;
  9308. struct intel_plane_state *state;
  9309. struct drm_framebuffer *fb;
  9310. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9311. crtc->base.base.id, crtc->base.name, context);
  9312. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9313. transcoder_name(pipe_config->cpu_transcoder),
  9314. pipe_config->pipe_bpp, pipe_config->dither);
  9315. if (pipe_config->has_pch_encoder)
  9316. intel_dump_m_n_config(pipe_config, "fdi",
  9317. pipe_config->fdi_lanes,
  9318. &pipe_config->fdi_m_n);
  9319. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9320. intel_dump_m_n_config(pipe_config, "dp m_n",
  9321. pipe_config->lane_count, &pipe_config->dp_m_n);
  9322. if (pipe_config->has_drrs)
  9323. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9324. pipe_config->lane_count,
  9325. &pipe_config->dp_m2_n2);
  9326. }
  9327. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9328. pipe_config->has_audio, pipe_config->has_infoframe);
  9329. DRM_DEBUG_KMS("requested mode:\n");
  9330. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9331. DRM_DEBUG_KMS("adjusted mode:\n");
  9332. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9333. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9334. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9335. pipe_config->port_clock,
  9336. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9337. pipe_config->pixel_rate);
  9338. if (INTEL_GEN(dev_priv) >= 9)
  9339. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9340. crtc->num_scalers,
  9341. pipe_config->scaler_state.scaler_users,
  9342. pipe_config->scaler_state.scaler_id);
  9343. if (HAS_GMCH_DISPLAY(dev_priv))
  9344. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9345. pipe_config->gmch_pfit.control,
  9346. pipe_config->gmch_pfit.pgm_ratios,
  9347. pipe_config->gmch_pfit.lvds_border_bits);
  9348. else
  9349. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9350. pipe_config->pch_pfit.pos,
  9351. pipe_config->pch_pfit.size,
  9352. enableddisabled(pipe_config->pch_pfit.enabled));
  9353. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9354. pipe_config->ips_enabled, pipe_config->double_wide);
  9355. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9356. DRM_DEBUG_KMS("planes on this crtc\n");
  9357. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9358. struct drm_format_name_buf format_name;
  9359. intel_plane = to_intel_plane(plane);
  9360. if (intel_plane->pipe != crtc->pipe)
  9361. continue;
  9362. state = to_intel_plane_state(plane->state);
  9363. fb = state->base.fb;
  9364. if (!fb) {
  9365. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9366. plane->base.id, plane->name, state->scaler_id);
  9367. continue;
  9368. }
  9369. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9370. plane->base.id, plane->name,
  9371. fb->base.id, fb->width, fb->height,
  9372. drm_get_format_name(fb->format->format, &format_name));
  9373. if (INTEL_GEN(dev_priv) >= 9)
  9374. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9375. state->scaler_id,
  9376. state->base.src.x1 >> 16,
  9377. state->base.src.y1 >> 16,
  9378. drm_rect_width(&state->base.src) >> 16,
  9379. drm_rect_height(&state->base.src) >> 16,
  9380. state->base.dst.x1, state->base.dst.y1,
  9381. drm_rect_width(&state->base.dst),
  9382. drm_rect_height(&state->base.dst));
  9383. }
  9384. }
  9385. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9386. {
  9387. struct drm_device *dev = state->dev;
  9388. struct drm_connector *connector;
  9389. unsigned int used_ports = 0;
  9390. unsigned int used_mst_ports = 0;
  9391. /*
  9392. * Walk the connector list instead of the encoder
  9393. * list to detect the problem on ddi platforms
  9394. * where there's just one encoder per digital port.
  9395. */
  9396. drm_for_each_connector(connector, dev) {
  9397. struct drm_connector_state *connector_state;
  9398. struct intel_encoder *encoder;
  9399. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9400. if (!connector_state)
  9401. connector_state = connector->state;
  9402. if (!connector_state->best_encoder)
  9403. continue;
  9404. encoder = to_intel_encoder(connector_state->best_encoder);
  9405. WARN_ON(!connector_state->crtc);
  9406. switch (encoder->type) {
  9407. unsigned int port_mask;
  9408. case INTEL_OUTPUT_UNKNOWN:
  9409. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9410. break;
  9411. case INTEL_OUTPUT_DP:
  9412. case INTEL_OUTPUT_HDMI:
  9413. case INTEL_OUTPUT_EDP:
  9414. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9415. /* the same port mustn't appear more than once */
  9416. if (used_ports & port_mask)
  9417. return false;
  9418. used_ports |= port_mask;
  9419. break;
  9420. case INTEL_OUTPUT_DP_MST:
  9421. used_mst_ports |=
  9422. 1 << enc_to_mst(&encoder->base)->primary->port;
  9423. break;
  9424. default:
  9425. break;
  9426. }
  9427. }
  9428. /* can't mix MST and SST/HDMI on the same port */
  9429. if (used_ports & used_mst_ports)
  9430. return false;
  9431. return true;
  9432. }
  9433. static void
  9434. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9435. {
  9436. struct drm_i915_private *dev_priv =
  9437. to_i915(crtc_state->base.crtc->dev);
  9438. struct intel_crtc_scaler_state scaler_state;
  9439. struct intel_dpll_hw_state dpll_hw_state;
  9440. struct intel_shared_dpll *shared_dpll;
  9441. struct intel_crtc_wm_state wm_state;
  9442. bool force_thru;
  9443. /* FIXME: before the switch to atomic started, a new pipe_config was
  9444. * kzalloc'd. Code that depends on any field being zero should be
  9445. * fixed, so that the crtc_state can be safely duplicated. For now,
  9446. * only fields that are know to not cause problems are preserved. */
  9447. scaler_state = crtc_state->scaler_state;
  9448. shared_dpll = crtc_state->shared_dpll;
  9449. dpll_hw_state = crtc_state->dpll_hw_state;
  9450. force_thru = crtc_state->pch_pfit.force_thru;
  9451. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9452. wm_state = crtc_state->wm;
  9453. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9454. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9455. memset(&crtc_state->base + 1, 0,
  9456. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9457. crtc_state->scaler_state = scaler_state;
  9458. crtc_state->shared_dpll = shared_dpll;
  9459. crtc_state->dpll_hw_state = dpll_hw_state;
  9460. crtc_state->pch_pfit.force_thru = force_thru;
  9461. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9462. crtc_state->wm = wm_state;
  9463. }
  9464. static int
  9465. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9466. struct intel_crtc_state *pipe_config)
  9467. {
  9468. struct drm_atomic_state *state = pipe_config->base.state;
  9469. struct intel_encoder *encoder;
  9470. struct drm_connector *connector;
  9471. struct drm_connector_state *connector_state;
  9472. int base_bpp, ret = -EINVAL;
  9473. int i;
  9474. bool retry = true;
  9475. clear_intel_crtc_state(pipe_config);
  9476. pipe_config->cpu_transcoder =
  9477. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9478. /*
  9479. * Sanitize sync polarity flags based on requested ones. If neither
  9480. * positive or negative polarity is requested, treat this as meaning
  9481. * negative polarity.
  9482. */
  9483. if (!(pipe_config->base.adjusted_mode.flags &
  9484. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9485. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9486. if (!(pipe_config->base.adjusted_mode.flags &
  9487. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9488. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9489. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9490. pipe_config);
  9491. if (base_bpp < 0)
  9492. goto fail;
  9493. /*
  9494. * Determine the real pipe dimensions. Note that stereo modes can
  9495. * increase the actual pipe size due to the frame doubling and
  9496. * insertion of additional space for blanks between the frame. This
  9497. * is stored in the crtc timings. We use the requested mode to do this
  9498. * computation to clearly distinguish it from the adjusted mode, which
  9499. * can be changed by the connectors in the below retry loop.
  9500. */
  9501. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9502. &pipe_config->pipe_src_w,
  9503. &pipe_config->pipe_src_h);
  9504. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9505. if (connector_state->crtc != crtc)
  9506. continue;
  9507. encoder = to_intel_encoder(connector_state->best_encoder);
  9508. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9509. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9510. goto fail;
  9511. }
  9512. /*
  9513. * Determine output_types before calling the .compute_config()
  9514. * hooks so that the hooks can use this information safely.
  9515. */
  9516. pipe_config->output_types |= 1 << encoder->type;
  9517. }
  9518. encoder_retry:
  9519. /* Ensure the port clock defaults are reset when retrying. */
  9520. pipe_config->port_clock = 0;
  9521. pipe_config->pixel_multiplier = 1;
  9522. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9523. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9524. CRTC_STEREO_DOUBLE);
  9525. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9526. * adjust it according to limitations or connector properties, and also
  9527. * a chance to reject the mode entirely.
  9528. */
  9529. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9530. if (connector_state->crtc != crtc)
  9531. continue;
  9532. encoder = to_intel_encoder(connector_state->best_encoder);
  9533. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9534. DRM_DEBUG_KMS("Encoder config failure\n");
  9535. goto fail;
  9536. }
  9537. }
  9538. /* Set default port clock if not overwritten by the encoder. Needs to be
  9539. * done afterwards in case the encoder adjusts the mode. */
  9540. if (!pipe_config->port_clock)
  9541. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9542. * pipe_config->pixel_multiplier;
  9543. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9544. if (ret < 0) {
  9545. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9546. goto fail;
  9547. }
  9548. if (ret == RETRY) {
  9549. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9550. ret = -EINVAL;
  9551. goto fail;
  9552. }
  9553. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9554. retry = false;
  9555. goto encoder_retry;
  9556. }
  9557. /* Dithering seems to not pass-through bits correctly when it should, so
  9558. * only enable it on 6bpc panels and when its not a compliance
  9559. * test requesting 6bpc video pattern.
  9560. */
  9561. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9562. !pipe_config->dither_force_disable;
  9563. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9564. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9565. fail:
  9566. return ret;
  9567. }
  9568. static void
  9569. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9570. {
  9571. struct drm_crtc *crtc;
  9572. struct drm_crtc_state *new_crtc_state;
  9573. int i;
  9574. /* Double check state. */
  9575. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9576. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9577. /* Update hwmode for vblank functions */
  9578. if (new_crtc_state->active)
  9579. crtc->hwmode = new_crtc_state->adjusted_mode;
  9580. else
  9581. crtc->hwmode.crtc_clock = 0;
  9582. /*
  9583. * Update legacy state to satisfy fbc code. This can
  9584. * be removed when fbc uses the atomic state.
  9585. */
  9586. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9587. struct drm_plane_state *plane_state = crtc->primary->state;
  9588. crtc->primary->fb = plane_state->fb;
  9589. crtc->x = plane_state->src_x >> 16;
  9590. crtc->y = plane_state->src_y >> 16;
  9591. }
  9592. }
  9593. }
  9594. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9595. {
  9596. int diff;
  9597. if (clock1 == clock2)
  9598. return true;
  9599. if (!clock1 || !clock2)
  9600. return false;
  9601. diff = abs(clock1 - clock2);
  9602. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9603. return true;
  9604. return false;
  9605. }
  9606. static bool
  9607. intel_compare_m_n(unsigned int m, unsigned int n,
  9608. unsigned int m2, unsigned int n2,
  9609. bool exact)
  9610. {
  9611. if (m == m2 && n == n2)
  9612. return true;
  9613. if (exact || !m || !n || !m2 || !n2)
  9614. return false;
  9615. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9616. if (n > n2) {
  9617. while (n > n2) {
  9618. m2 <<= 1;
  9619. n2 <<= 1;
  9620. }
  9621. } else if (n < n2) {
  9622. while (n < n2) {
  9623. m <<= 1;
  9624. n <<= 1;
  9625. }
  9626. }
  9627. if (n != n2)
  9628. return false;
  9629. return intel_fuzzy_clock_check(m, m2);
  9630. }
  9631. static bool
  9632. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9633. struct intel_link_m_n *m2_n2,
  9634. bool adjust)
  9635. {
  9636. if (m_n->tu == m2_n2->tu &&
  9637. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9638. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9639. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9640. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9641. if (adjust)
  9642. *m2_n2 = *m_n;
  9643. return true;
  9644. }
  9645. return false;
  9646. }
  9647. static void __printf(3, 4)
  9648. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9649. {
  9650. char *level;
  9651. unsigned int category;
  9652. struct va_format vaf;
  9653. va_list args;
  9654. if (adjust) {
  9655. level = KERN_DEBUG;
  9656. category = DRM_UT_KMS;
  9657. } else {
  9658. level = KERN_ERR;
  9659. category = DRM_UT_NONE;
  9660. }
  9661. va_start(args, format);
  9662. vaf.fmt = format;
  9663. vaf.va = &args;
  9664. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9665. va_end(args);
  9666. }
  9667. static bool
  9668. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9669. struct intel_crtc_state *current_config,
  9670. struct intel_crtc_state *pipe_config,
  9671. bool adjust)
  9672. {
  9673. bool ret = true;
  9674. #define PIPE_CONF_CHECK_X(name) \
  9675. if (current_config->name != pipe_config->name) { \
  9676. pipe_config_err(adjust, __stringify(name), \
  9677. "(expected 0x%08x, found 0x%08x)\n", \
  9678. current_config->name, \
  9679. pipe_config->name); \
  9680. ret = false; \
  9681. }
  9682. #define PIPE_CONF_CHECK_I(name) \
  9683. if (current_config->name != pipe_config->name) { \
  9684. pipe_config_err(adjust, __stringify(name), \
  9685. "(expected %i, found %i)\n", \
  9686. current_config->name, \
  9687. pipe_config->name); \
  9688. ret = false; \
  9689. }
  9690. #define PIPE_CONF_CHECK_P(name) \
  9691. if (current_config->name != pipe_config->name) { \
  9692. pipe_config_err(adjust, __stringify(name), \
  9693. "(expected %p, found %p)\n", \
  9694. current_config->name, \
  9695. pipe_config->name); \
  9696. ret = false; \
  9697. }
  9698. #define PIPE_CONF_CHECK_M_N(name) \
  9699. if (!intel_compare_link_m_n(&current_config->name, \
  9700. &pipe_config->name,\
  9701. adjust)) { \
  9702. pipe_config_err(adjust, __stringify(name), \
  9703. "(expected tu %i gmch %i/%i link %i/%i, " \
  9704. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9705. current_config->name.tu, \
  9706. current_config->name.gmch_m, \
  9707. current_config->name.gmch_n, \
  9708. current_config->name.link_m, \
  9709. current_config->name.link_n, \
  9710. pipe_config->name.tu, \
  9711. pipe_config->name.gmch_m, \
  9712. pipe_config->name.gmch_n, \
  9713. pipe_config->name.link_m, \
  9714. pipe_config->name.link_n); \
  9715. ret = false; \
  9716. }
  9717. /* This is required for BDW+ where there is only one set of registers for
  9718. * switching between high and low RR.
  9719. * This macro can be used whenever a comparison has to be made between one
  9720. * hw state and multiple sw state variables.
  9721. */
  9722. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9723. if (!intel_compare_link_m_n(&current_config->name, \
  9724. &pipe_config->name, adjust) && \
  9725. !intel_compare_link_m_n(&current_config->alt_name, \
  9726. &pipe_config->name, adjust)) { \
  9727. pipe_config_err(adjust, __stringify(name), \
  9728. "(expected tu %i gmch %i/%i link %i/%i, " \
  9729. "or tu %i gmch %i/%i link %i/%i, " \
  9730. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9731. current_config->name.tu, \
  9732. current_config->name.gmch_m, \
  9733. current_config->name.gmch_n, \
  9734. current_config->name.link_m, \
  9735. current_config->name.link_n, \
  9736. current_config->alt_name.tu, \
  9737. current_config->alt_name.gmch_m, \
  9738. current_config->alt_name.gmch_n, \
  9739. current_config->alt_name.link_m, \
  9740. current_config->alt_name.link_n, \
  9741. pipe_config->name.tu, \
  9742. pipe_config->name.gmch_m, \
  9743. pipe_config->name.gmch_n, \
  9744. pipe_config->name.link_m, \
  9745. pipe_config->name.link_n); \
  9746. ret = false; \
  9747. }
  9748. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9749. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9750. pipe_config_err(adjust, __stringify(name), \
  9751. "(%x) (expected %i, found %i)\n", \
  9752. (mask), \
  9753. current_config->name & (mask), \
  9754. pipe_config->name & (mask)); \
  9755. ret = false; \
  9756. }
  9757. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9758. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9759. pipe_config_err(adjust, __stringify(name), \
  9760. "(expected %i, found %i)\n", \
  9761. current_config->name, \
  9762. pipe_config->name); \
  9763. ret = false; \
  9764. }
  9765. #define PIPE_CONF_QUIRK(quirk) \
  9766. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9767. PIPE_CONF_CHECK_I(cpu_transcoder);
  9768. PIPE_CONF_CHECK_I(has_pch_encoder);
  9769. PIPE_CONF_CHECK_I(fdi_lanes);
  9770. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9771. PIPE_CONF_CHECK_I(lane_count);
  9772. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9773. if (INTEL_GEN(dev_priv) < 8) {
  9774. PIPE_CONF_CHECK_M_N(dp_m_n);
  9775. if (current_config->has_drrs)
  9776. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9777. } else
  9778. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9779. PIPE_CONF_CHECK_X(output_types);
  9780. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9781. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9782. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9783. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9784. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9785. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9786. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9787. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9788. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9789. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9790. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9791. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9792. PIPE_CONF_CHECK_I(pixel_multiplier);
  9793. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9794. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9795. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9796. PIPE_CONF_CHECK_I(limited_color_range);
  9797. PIPE_CONF_CHECK_I(has_infoframe);
  9798. PIPE_CONF_CHECK_I(has_audio);
  9799. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9800. DRM_MODE_FLAG_INTERLACE);
  9801. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9802. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9803. DRM_MODE_FLAG_PHSYNC);
  9804. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9805. DRM_MODE_FLAG_NHSYNC);
  9806. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9807. DRM_MODE_FLAG_PVSYNC);
  9808. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9809. DRM_MODE_FLAG_NVSYNC);
  9810. }
  9811. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9812. /* pfit ratios are autocomputed by the hw on gen4+ */
  9813. if (INTEL_GEN(dev_priv) < 4)
  9814. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9815. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9816. if (!adjust) {
  9817. PIPE_CONF_CHECK_I(pipe_src_w);
  9818. PIPE_CONF_CHECK_I(pipe_src_h);
  9819. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9820. if (current_config->pch_pfit.enabled) {
  9821. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9822. PIPE_CONF_CHECK_X(pch_pfit.size);
  9823. }
  9824. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9825. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9826. }
  9827. /* BDW+ don't expose a synchronous way to read the state */
  9828. if (IS_HASWELL(dev_priv))
  9829. PIPE_CONF_CHECK_I(ips_enabled);
  9830. PIPE_CONF_CHECK_I(double_wide);
  9831. PIPE_CONF_CHECK_P(shared_dpll);
  9832. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9833. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9834. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9835. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9836. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9837. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9838. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9839. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9840. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9841. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9842. PIPE_CONF_CHECK_X(dsi_pll.div);
  9843. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9844. PIPE_CONF_CHECK_I(pipe_bpp);
  9845. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9846. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9847. #undef PIPE_CONF_CHECK_X
  9848. #undef PIPE_CONF_CHECK_I
  9849. #undef PIPE_CONF_CHECK_P
  9850. #undef PIPE_CONF_CHECK_FLAGS
  9851. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9852. #undef PIPE_CONF_QUIRK
  9853. return ret;
  9854. }
  9855. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9856. const struct intel_crtc_state *pipe_config)
  9857. {
  9858. if (pipe_config->has_pch_encoder) {
  9859. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9860. &pipe_config->fdi_m_n);
  9861. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9862. /*
  9863. * FDI already provided one idea for the dotclock.
  9864. * Yell if the encoder disagrees.
  9865. */
  9866. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9867. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9868. fdi_dotclock, dotclock);
  9869. }
  9870. }
  9871. static void verify_wm_state(struct drm_crtc *crtc,
  9872. struct drm_crtc_state *new_state)
  9873. {
  9874. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9875. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9876. struct skl_pipe_wm hw_wm, *sw_wm;
  9877. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9878. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9880. const enum pipe pipe = intel_crtc->pipe;
  9881. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9882. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9883. return;
  9884. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9885. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9886. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9887. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9888. /* planes */
  9889. for_each_universal_plane(dev_priv, pipe, plane) {
  9890. hw_plane_wm = &hw_wm.planes[plane];
  9891. sw_plane_wm = &sw_wm->planes[plane];
  9892. /* Watermarks */
  9893. for (level = 0; level <= max_level; level++) {
  9894. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9895. &sw_plane_wm->wm[level]))
  9896. continue;
  9897. DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9898. pipe_name(pipe), plane + 1, level,
  9899. sw_plane_wm->wm[level].plane_en,
  9900. sw_plane_wm->wm[level].plane_res_b,
  9901. sw_plane_wm->wm[level].plane_res_l,
  9902. hw_plane_wm->wm[level].plane_en,
  9903. hw_plane_wm->wm[level].plane_res_b,
  9904. hw_plane_wm->wm[level].plane_res_l);
  9905. }
  9906. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9907. &sw_plane_wm->trans_wm)) {
  9908. DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9909. pipe_name(pipe), plane + 1,
  9910. sw_plane_wm->trans_wm.plane_en,
  9911. sw_plane_wm->trans_wm.plane_res_b,
  9912. sw_plane_wm->trans_wm.plane_res_l,
  9913. hw_plane_wm->trans_wm.plane_en,
  9914. hw_plane_wm->trans_wm.plane_res_b,
  9915. hw_plane_wm->trans_wm.plane_res_l);
  9916. }
  9917. /* DDB */
  9918. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9919. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9920. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9921. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9922. pipe_name(pipe), plane + 1,
  9923. sw_ddb_entry->start, sw_ddb_entry->end,
  9924. hw_ddb_entry->start, hw_ddb_entry->end);
  9925. }
  9926. }
  9927. /*
  9928. * cursor
  9929. * If the cursor plane isn't active, we may not have updated it's ddb
  9930. * allocation. In that case since the ddb allocation will be updated
  9931. * once the plane becomes visible, we can skip this check
  9932. */
  9933. if (intel_crtc->cursor_addr) {
  9934. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9935. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9936. /* Watermarks */
  9937. for (level = 0; level <= max_level; level++) {
  9938. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9939. &sw_plane_wm->wm[level]))
  9940. continue;
  9941. DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9942. pipe_name(pipe), level,
  9943. sw_plane_wm->wm[level].plane_en,
  9944. sw_plane_wm->wm[level].plane_res_b,
  9945. sw_plane_wm->wm[level].plane_res_l,
  9946. hw_plane_wm->wm[level].plane_en,
  9947. hw_plane_wm->wm[level].plane_res_b,
  9948. hw_plane_wm->wm[level].plane_res_l);
  9949. }
  9950. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9951. &sw_plane_wm->trans_wm)) {
  9952. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9953. pipe_name(pipe),
  9954. sw_plane_wm->trans_wm.plane_en,
  9955. sw_plane_wm->trans_wm.plane_res_b,
  9956. sw_plane_wm->trans_wm.plane_res_l,
  9957. hw_plane_wm->trans_wm.plane_en,
  9958. hw_plane_wm->trans_wm.plane_res_b,
  9959. hw_plane_wm->trans_wm.plane_res_l);
  9960. }
  9961. /* DDB */
  9962. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9963. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9964. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9965. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9966. pipe_name(pipe),
  9967. sw_ddb_entry->start, sw_ddb_entry->end,
  9968. hw_ddb_entry->start, hw_ddb_entry->end);
  9969. }
  9970. }
  9971. }
  9972. static void
  9973. verify_connector_state(struct drm_device *dev,
  9974. struct drm_atomic_state *state,
  9975. struct drm_crtc *crtc)
  9976. {
  9977. struct drm_connector *connector;
  9978. struct drm_connector_state *new_conn_state;
  9979. int i;
  9980. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9981. struct drm_encoder *encoder = connector->encoder;
  9982. if (new_conn_state->crtc != crtc)
  9983. continue;
  9984. intel_connector_verify_state(to_intel_connector(connector));
  9985. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9986. "connector's atomic encoder doesn't match legacy encoder\n");
  9987. }
  9988. }
  9989. static void
  9990. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9991. {
  9992. struct intel_encoder *encoder;
  9993. struct drm_connector *connector;
  9994. struct drm_connector_state *old_conn_state, *new_conn_state;
  9995. int i;
  9996. for_each_intel_encoder(dev, encoder) {
  9997. bool enabled = false, found = false;
  9998. enum pipe pipe;
  9999. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10000. encoder->base.base.id,
  10001. encoder->base.name);
  10002. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  10003. new_conn_state, i) {
  10004. if (old_conn_state->best_encoder == &encoder->base)
  10005. found = true;
  10006. if (new_conn_state->best_encoder != &encoder->base)
  10007. continue;
  10008. found = enabled = true;
  10009. I915_STATE_WARN(new_conn_state->crtc !=
  10010. encoder->base.crtc,
  10011. "connector's crtc doesn't match encoder crtc\n");
  10012. }
  10013. if (!found)
  10014. continue;
  10015. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10016. "encoder's enabled state mismatch "
  10017. "(expected %i, found %i)\n",
  10018. !!encoder->base.crtc, enabled);
  10019. if (!encoder->base.crtc) {
  10020. bool active;
  10021. active = encoder->get_hw_state(encoder, &pipe);
  10022. I915_STATE_WARN(active,
  10023. "encoder detached but still enabled on pipe %c.\n",
  10024. pipe_name(pipe));
  10025. }
  10026. }
  10027. }
  10028. static void
  10029. verify_crtc_state(struct drm_crtc *crtc,
  10030. struct drm_crtc_state *old_crtc_state,
  10031. struct drm_crtc_state *new_crtc_state)
  10032. {
  10033. struct drm_device *dev = crtc->dev;
  10034. struct drm_i915_private *dev_priv = to_i915(dev);
  10035. struct intel_encoder *encoder;
  10036. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10037. struct intel_crtc_state *pipe_config, *sw_config;
  10038. struct drm_atomic_state *old_state;
  10039. bool active;
  10040. old_state = old_crtc_state->state;
  10041. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10042. pipe_config = to_intel_crtc_state(old_crtc_state);
  10043. memset(pipe_config, 0, sizeof(*pipe_config));
  10044. pipe_config->base.crtc = crtc;
  10045. pipe_config->base.state = old_state;
  10046. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10047. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10048. /* hw state is inconsistent with the pipe quirk */
  10049. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10050. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10051. active = new_crtc_state->active;
  10052. I915_STATE_WARN(new_crtc_state->active != active,
  10053. "crtc active state doesn't match with hw state "
  10054. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10055. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10056. "transitional active state does not match atomic hw state "
  10057. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10058. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10059. enum pipe pipe;
  10060. active = encoder->get_hw_state(encoder, &pipe);
  10061. I915_STATE_WARN(active != new_crtc_state->active,
  10062. "[ENCODER:%i] active %i with crtc active %i\n",
  10063. encoder->base.base.id, active, new_crtc_state->active);
  10064. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10065. "Encoder connected to wrong pipe %c\n",
  10066. pipe_name(pipe));
  10067. if (active) {
  10068. pipe_config->output_types |= 1 << encoder->type;
  10069. encoder->get_config(encoder, pipe_config);
  10070. }
  10071. }
  10072. intel_crtc_compute_pixel_rate(pipe_config);
  10073. if (!new_crtc_state->active)
  10074. return;
  10075. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10076. sw_config = to_intel_crtc_state(crtc->state);
  10077. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10078. pipe_config, false)) {
  10079. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10080. intel_dump_pipe_config(intel_crtc, pipe_config,
  10081. "[hw state]");
  10082. intel_dump_pipe_config(intel_crtc, sw_config,
  10083. "[sw state]");
  10084. }
  10085. }
  10086. static void
  10087. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10088. struct intel_shared_dpll *pll,
  10089. struct drm_crtc *crtc,
  10090. struct drm_crtc_state *new_state)
  10091. {
  10092. struct intel_dpll_hw_state dpll_hw_state;
  10093. unsigned crtc_mask;
  10094. bool active;
  10095. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10096. DRM_DEBUG_KMS("%s\n", pll->name);
  10097. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10098. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10099. I915_STATE_WARN(!pll->on && pll->active_mask,
  10100. "pll in active use but not on in sw tracking\n");
  10101. I915_STATE_WARN(pll->on && !pll->active_mask,
  10102. "pll is on but not used by any active crtc\n");
  10103. I915_STATE_WARN(pll->on != active,
  10104. "pll on state mismatch (expected %i, found %i)\n",
  10105. pll->on, active);
  10106. }
  10107. if (!crtc) {
  10108. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10109. "more active pll users than references: %x vs %x\n",
  10110. pll->active_mask, pll->state.crtc_mask);
  10111. return;
  10112. }
  10113. crtc_mask = 1 << drm_crtc_index(crtc);
  10114. if (new_state->active)
  10115. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10116. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10117. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10118. else
  10119. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10120. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10121. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10122. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10123. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10124. crtc_mask, pll->state.crtc_mask);
  10125. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10126. &dpll_hw_state,
  10127. sizeof(dpll_hw_state)),
  10128. "pll hw state mismatch\n");
  10129. }
  10130. static void
  10131. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10132. struct drm_crtc_state *old_crtc_state,
  10133. struct drm_crtc_state *new_crtc_state)
  10134. {
  10135. struct drm_i915_private *dev_priv = to_i915(dev);
  10136. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10137. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10138. if (new_state->shared_dpll)
  10139. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10140. if (old_state->shared_dpll &&
  10141. old_state->shared_dpll != new_state->shared_dpll) {
  10142. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10143. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10144. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10145. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10146. pipe_name(drm_crtc_index(crtc)));
  10147. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10148. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10149. pipe_name(drm_crtc_index(crtc)));
  10150. }
  10151. }
  10152. static void
  10153. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10154. struct drm_atomic_state *state,
  10155. struct drm_crtc_state *old_state,
  10156. struct drm_crtc_state *new_state)
  10157. {
  10158. if (!needs_modeset(new_state) &&
  10159. !to_intel_crtc_state(new_state)->update_pipe)
  10160. return;
  10161. verify_wm_state(crtc, new_state);
  10162. verify_connector_state(crtc->dev, state, crtc);
  10163. verify_crtc_state(crtc, old_state, new_state);
  10164. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10165. }
  10166. static void
  10167. verify_disabled_dpll_state(struct drm_device *dev)
  10168. {
  10169. struct drm_i915_private *dev_priv = to_i915(dev);
  10170. int i;
  10171. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10172. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10173. }
  10174. static void
  10175. intel_modeset_verify_disabled(struct drm_device *dev,
  10176. struct drm_atomic_state *state)
  10177. {
  10178. verify_encoder_state(dev, state);
  10179. verify_connector_state(dev, state, NULL);
  10180. verify_disabled_dpll_state(dev);
  10181. }
  10182. static void update_scanline_offset(struct intel_crtc *crtc)
  10183. {
  10184. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10185. /*
  10186. * The scanline counter increments at the leading edge of hsync.
  10187. *
  10188. * On most platforms it starts counting from vtotal-1 on the
  10189. * first active line. That means the scanline counter value is
  10190. * always one less than what we would expect. Ie. just after
  10191. * start of vblank, which also occurs at start of hsync (on the
  10192. * last active line), the scanline counter will read vblank_start-1.
  10193. *
  10194. * On gen2 the scanline counter starts counting from 1 instead
  10195. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10196. * to keep the value positive), instead of adding one.
  10197. *
  10198. * On HSW+ the behaviour of the scanline counter depends on the output
  10199. * type. For DP ports it behaves like most other platforms, but on HDMI
  10200. * there's an extra 1 line difference. So we need to add two instead of
  10201. * one to the value.
  10202. */
  10203. if (IS_GEN2(dev_priv)) {
  10204. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10205. int vtotal;
  10206. vtotal = adjusted_mode->crtc_vtotal;
  10207. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10208. vtotal /= 2;
  10209. crtc->scanline_offset = vtotal - 1;
  10210. } else if (HAS_DDI(dev_priv) &&
  10211. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10212. crtc->scanline_offset = 2;
  10213. } else
  10214. crtc->scanline_offset = 1;
  10215. }
  10216. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10217. {
  10218. struct drm_device *dev = state->dev;
  10219. struct drm_i915_private *dev_priv = to_i915(dev);
  10220. struct drm_crtc *crtc;
  10221. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10222. int i;
  10223. if (!dev_priv->display.crtc_compute_clock)
  10224. return;
  10225. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10226. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10227. struct intel_shared_dpll *old_dpll =
  10228. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10229. if (!needs_modeset(new_crtc_state))
  10230. continue;
  10231. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10232. if (!old_dpll)
  10233. continue;
  10234. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10235. }
  10236. }
  10237. /*
  10238. * This implements the workaround described in the "notes" section of the mode
  10239. * set sequence documentation. When going from no pipes or single pipe to
  10240. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10241. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10242. */
  10243. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10244. {
  10245. struct drm_crtc_state *crtc_state;
  10246. struct intel_crtc *intel_crtc;
  10247. struct drm_crtc *crtc;
  10248. struct intel_crtc_state *first_crtc_state = NULL;
  10249. struct intel_crtc_state *other_crtc_state = NULL;
  10250. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10251. int i;
  10252. /* look at all crtc's that are going to be enabled in during modeset */
  10253. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10254. intel_crtc = to_intel_crtc(crtc);
  10255. if (!crtc_state->active || !needs_modeset(crtc_state))
  10256. continue;
  10257. if (first_crtc_state) {
  10258. other_crtc_state = to_intel_crtc_state(crtc_state);
  10259. break;
  10260. } else {
  10261. first_crtc_state = to_intel_crtc_state(crtc_state);
  10262. first_pipe = intel_crtc->pipe;
  10263. }
  10264. }
  10265. /* No workaround needed? */
  10266. if (!first_crtc_state)
  10267. return 0;
  10268. /* w/a possibly needed, check how many crtc's are already enabled. */
  10269. for_each_intel_crtc(state->dev, intel_crtc) {
  10270. struct intel_crtc_state *pipe_config;
  10271. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10272. if (IS_ERR(pipe_config))
  10273. return PTR_ERR(pipe_config);
  10274. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10275. if (!pipe_config->base.active ||
  10276. needs_modeset(&pipe_config->base))
  10277. continue;
  10278. /* 2 or more enabled crtcs means no need for w/a */
  10279. if (enabled_pipe != INVALID_PIPE)
  10280. return 0;
  10281. enabled_pipe = intel_crtc->pipe;
  10282. }
  10283. if (enabled_pipe != INVALID_PIPE)
  10284. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10285. else if (other_crtc_state)
  10286. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10287. return 0;
  10288. }
  10289. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10290. {
  10291. struct drm_crtc *crtc;
  10292. /* Add all pipes to the state */
  10293. for_each_crtc(state->dev, crtc) {
  10294. struct drm_crtc_state *crtc_state;
  10295. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10296. if (IS_ERR(crtc_state))
  10297. return PTR_ERR(crtc_state);
  10298. }
  10299. return 0;
  10300. }
  10301. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10302. {
  10303. struct drm_crtc *crtc;
  10304. /*
  10305. * Add all pipes to the state, and force
  10306. * a modeset on all the active ones.
  10307. */
  10308. for_each_crtc(state->dev, crtc) {
  10309. struct drm_crtc_state *crtc_state;
  10310. int ret;
  10311. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10312. if (IS_ERR(crtc_state))
  10313. return PTR_ERR(crtc_state);
  10314. if (!crtc_state->active || needs_modeset(crtc_state))
  10315. continue;
  10316. crtc_state->mode_changed = true;
  10317. ret = drm_atomic_add_affected_connectors(state, crtc);
  10318. if (ret)
  10319. return ret;
  10320. ret = drm_atomic_add_affected_planes(state, crtc);
  10321. if (ret)
  10322. return ret;
  10323. }
  10324. return 0;
  10325. }
  10326. static int intel_modeset_checks(struct drm_atomic_state *state)
  10327. {
  10328. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10329. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10330. struct drm_crtc *crtc;
  10331. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10332. int ret = 0, i;
  10333. if (!check_digital_port_conflicts(state)) {
  10334. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10335. return -EINVAL;
  10336. }
  10337. intel_state->modeset = true;
  10338. intel_state->active_crtcs = dev_priv->active_crtcs;
  10339. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10340. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10341. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10342. if (new_crtc_state->active)
  10343. intel_state->active_crtcs |= 1 << i;
  10344. else
  10345. intel_state->active_crtcs &= ~(1 << i);
  10346. if (old_crtc_state->active != new_crtc_state->active)
  10347. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10348. }
  10349. /*
  10350. * See if the config requires any additional preparation, e.g.
  10351. * to adjust global state with pipes off. We need to do this
  10352. * here so we can get the modeset_pipe updated config for the new
  10353. * mode set on this crtc. For other crtcs we need to use the
  10354. * adjusted_mode bits in the crtc directly.
  10355. */
  10356. if (dev_priv->display.modeset_calc_cdclk) {
  10357. ret = dev_priv->display.modeset_calc_cdclk(state);
  10358. if (ret < 0)
  10359. return ret;
  10360. /*
  10361. * Writes to dev_priv->cdclk.logical must protected by
  10362. * holding all the crtc locks, even if we don't end up
  10363. * touching the hardware
  10364. */
  10365. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10366. &intel_state->cdclk.logical)) {
  10367. ret = intel_lock_all_pipes(state);
  10368. if (ret < 0)
  10369. return ret;
  10370. }
  10371. /* All pipes must be switched off while we change the cdclk. */
  10372. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10373. &intel_state->cdclk.actual)) {
  10374. ret = intel_modeset_all_pipes(state);
  10375. if (ret < 0)
  10376. return ret;
  10377. }
  10378. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10379. intel_state->cdclk.logical.cdclk,
  10380. intel_state->cdclk.actual.cdclk);
  10381. } else {
  10382. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10383. }
  10384. intel_modeset_clear_plls(state);
  10385. if (IS_HASWELL(dev_priv))
  10386. return haswell_mode_set_planes_workaround(state);
  10387. return 0;
  10388. }
  10389. /*
  10390. * Handle calculation of various watermark data at the end of the atomic check
  10391. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10392. * handlers to ensure that all derived state has been updated.
  10393. */
  10394. static int calc_watermark_data(struct drm_atomic_state *state)
  10395. {
  10396. struct drm_device *dev = state->dev;
  10397. struct drm_i915_private *dev_priv = to_i915(dev);
  10398. /* Is there platform-specific watermark information to calculate? */
  10399. if (dev_priv->display.compute_global_watermarks)
  10400. return dev_priv->display.compute_global_watermarks(state);
  10401. return 0;
  10402. }
  10403. /**
  10404. * intel_atomic_check - validate state object
  10405. * @dev: drm device
  10406. * @state: state to validate
  10407. */
  10408. static int intel_atomic_check(struct drm_device *dev,
  10409. struct drm_atomic_state *state)
  10410. {
  10411. struct drm_i915_private *dev_priv = to_i915(dev);
  10412. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10413. struct drm_crtc *crtc;
  10414. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10415. int ret, i;
  10416. bool any_ms = false;
  10417. ret = drm_atomic_helper_check_modeset(dev, state);
  10418. if (ret)
  10419. return ret;
  10420. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10421. struct intel_crtc_state *pipe_config =
  10422. to_intel_crtc_state(crtc_state);
  10423. /* Catch I915_MODE_FLAG_INHERITED */
  10424. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10425. crtc_state->mode_changed = true;
  10426. if (!needs_modeset(crtc_state))
  10427. continue;
  10428. if (!crtc_state->enable) {
  10429. any_ms = true;
  10430. continue;
  10431. }
  10432. /* FIXME: For only active_changed we shouldn't need to do any
  10433. * state recomputation at all. */
  10434. ret = drm_atomic_add_affected_connectors(state, crtc);
  10435. if (ret)
  10436. return ret;
  10437. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10438. if (ret) {
  10439. intel_dump_pipe_config(to_intel_crtc(crtc),
  10440. pipe_config, "[failed]");
  10441. return ret;
  10442. }
  10443. if (i915.fastboot &&
  10444. intel_pipe_config_compare(dev_priv,
  10445. to_intel_crtc_state(old_crtc_state),
  10446. pipe_config, true)) {
  10447. crtc_state->mode_changed = false;
  10448. pipe_config->update_pipe = true;
  10449. }
  10450. if (needs_modeset(crtc_state))
  10451. any_ms = true;
  10452. ret = drm_atomic_add_affected_planes(state, crtc);
  10453. if (ret)
  10454. return ret;
  10455. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10456. needs_modeset(crtc_state) ?
  10457. "[modeset]" : "[fastset]");
  10458. }
  10459. if (any_ms) {
  10460. ret = intel_modeset_checks(state);
  10461. if (ret)
  10462. return ret;
  10463. } else {
  10464. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10465. }
  10466. ret = drm_atomic_helper_check_planes(dev, state);
  10467. if (ret)
  10468. return ret;
  10469. intel_fbc_choose_crtc(dev_priv, state);
  10470. return calc_watermark_data(state);
  10471. }
  10472. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10473. struct drm_atomic_state *state)
  10474. {
  10475. struct drm_i915_private *dev_priv = to_i915(dev);
  10476. struct drm_crtc_state *crtc_state;
  10477. struct drm_crtc *crtc;
  10478. int i, ret;
  10479. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10480. if (state->legacy_cursor_update)
  10481. continue;
  10482. ret = intel_crtc_wait_for_pending_flips(crtc);
  10483. if (ret)
  10484. return ret;
  10485. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10486. flush_workqueue(dev_priv->wq);
  10487. }
  10488. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10489. if (ret)
  10490. return ret;
  10491. ret = drm_atomic_helper_prepare_planes(dev, state);
  10492. mutex_unlock(&dev->struct_mutex);
  10493. return ret;
  10494. }
  10495. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10496. {
  10497. struct drm_device *dev = crtc->base.dev;
  10498. if (!dev->max_vblank_count)
  10499. return drm_accurate_vblank_count(&crtc->base);
  10500. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10501. }
  10502. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10503. struct drm_i915_private *dev_priv,
  10504. unsigned crtc_mask)
  10505. {
  10506. unsigned last_vblank_count[I915_MAX_PIPES];
  10507. enum pipe pipe;
  10508. int ret;
  10509. if (!crtc_mask)
  10510. return;
  10511. for_each_pipe(dev_priv, pipe) {
  10512. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10513. pipe);
  10514. if (!((1 << pipe) & crtc_mask))
  10515. continue;
  10516. ret = drm_crtc_vblank_get(&crtc->base);
  10517. if (WARN_ON(ret != 0)) {
  10518. crtc_mask &= ~(1 << pipe);
  10519. continue;
  10520. }
  10521. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10522. }
  10523. for_each_pipe(dev_priv, pipe) {
  10524. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10525. pipe);
  10526. long lret;
  10527. if (!((1 << pipe) & crtc_mask))
  10528. continue;
  10529. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10530. last_vblank_count[pipe] !=
  10531. drm_crtc_vblank_count(&crtc->base),
  10532. msecs_to_jiffies(50));
  10533. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10534. drm_crtc_vblank_put(&crtc->base);
  10535. }
  10536. }
  10537. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10538. {
  10539. /* fb updated, need to unpin old fb */
  10540. if (crtc_state->fb_changed)
  10541. return true;
  10542. /* wm changes, need vblank before final wm's */
  10543. if (crtc_state->update_wm_post)
  10544. return true;
  10545. if (crtc_state->wm.need_postvbl_update)
  10546. return true;
  10547. return false;
  10548. }
  10549. static void intel_update_crtc(struct drm_crtc *crtc,
  10550. struct drm_atomic_state *state,
  10551. struct drm_crtc_state *old_crtc_state,
  10552. struct drm_crtc_state *new_crtc_state,
  10553. unsigned int *crtc_vblank_mask)
  10554. {
  10555. struct drm_device *dev = crtc->dev;
  10556. struct drm_i915_private *dev_priv = to_i915(dev);
  10557. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10558. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10559. bool modeset = needs_modeset(new_crtc_state);
  10560. if (modeset) {
  10561. update_scanline_offset(intel_crtc);
  10562. dev_priv->display.crtc_enable(pipe_config, state);
  10563. } else {
  10564. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10565. pipe_config);
  10566. }
  10567. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10568. intel_fbc_enable(
  10569. intel_crtc, pipe_config,
  10570. to_intel_plane_state(crtc->primary->state));
  10571. }
  10572. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10573. if (needs_vblank_wait(pipe_config))
  10574. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10575. }
  10576. static void intel_update_crtcs(struct drm_atomic_state *state,
  10577. unsigned int *crtc_vblank_mask)
  10578. {
  10579. struct drm_crtc *crtc;
  10580. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10581. int i;
  10582. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10583. if (!new_crtc_state->active)
  10584. continue;
  10585. intel_update_crtc(crtc, state, old_crtc_state,
  10586. new_crtc_state, crtc_vblank_mask);
  10587. }
  10588. }
  10589. static void skl_update_crtcs(struct drm_atomic_state *state,
  10590. unsigned int *crtc_vblank_mask)
  10591. {
  10592. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10593. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10594. struct drm_crtc *crtc;
  10595. struct intel_crtc *intel_crtc;
  10596. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10597. struct intel_crtc_state *cstate;
  10598. unsigned int updated = 0;
  10599. bool progress;
  10600. enum pipe pipe;
  10601. int i;
  10602. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10603. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10604. /* ignore allocations for crtc's that have been turned off. */
  10605. if (new_crtc_state->active)
  10606. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10607. /*
  10608. * Whenever the number of active pipes changes, we need to make sure we
  10609. * update the pipes in the right order so that their ddb allocations
  10610. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10611. * cause pipe underruns and other bad stuff.
  10612. */
  10613. do {
  10614. progress = false;
  10615. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10616. bool vbl_wait = false;
  10617. unsigned int cmask = drm_crtc_mask(crtc);
  10618. intel_crtc = to_intel_crtc(crtc);
  10619. cstate = to_intel_crtc_state(crtc->state);
  10620. pipe = intel_crtc->pipe;
  10621. if (updated & cmask || !cstate->base.active)
  10622. continue;
  10623. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10624. continue;
  10625. updated |= cmask;
  10626. entries[i] = &cstate->wm.skl.ddb;
  10627. /*
  10628. * If this is an already active pipe, it's DDB changed,
  10629. * and this isn't the last pipe that needs updating
  10630. * then we need to wait for a vblank to pass for the
  10631. * new ddb allocation to take effect.
  10632. */
  10633. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10634. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10635. !new_crtc_state->active_changed &&
  10636. intel_state->wm_results.dirty_pipes != updated)
  10637. vbl_wait = true;
  10638. intel_update_crtc(crtc, state, old_crtc_state,
  10639. new_crtc_state, crtc_vblank_mask);
  10640. if (vbl_wait)
  10641. intel_wait_for_vblank(dev_priv, pipe);
  10642. progress = true;
  10643. }
  10644. } while (progress);
  10645. }
  10646. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10647. {
  10648. struct intel_atomic_state *state, *next;
  10649. struct llist_node *freed;
  10650. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10651. llist_for_each_entry_safe(state, next, freed, freed)
  10652. drm_atomic_state_put(&state->base);
  10653. }
  10654. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10655. {
  10656. struct drm_i915_private *dev_priv =
  10657. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10658. intel_atomic_helper_free_state(dev_priv);
  10659. }
  10660. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10661. {
  10662. struct drm_device *dev = state->dev;
  10663. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10664. struct drm_i915_private *dev_priv = to_i915(dev);
  10665. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10666. struct drm_crtc *crtc;
  10667. struct intel_crtc_state *intel_cstate;
  10668. bool hw_check = intel_state->modeset;
  10669. u64 put_domains[I915_MAX_PIPES] = {};
  10670. unsigned crtc_vblank_mask = 0;
  10671. int i;
  10672. drm_atomic_helper_wait_for_dependencies(state);
  10673. if (intel_state->modeset)
  10674. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10675. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10677. if (needs_modeset(new_crtc_state) ||
  10678. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10679. hw_check = true;
  10680. put_domains[to_intel_crtc(crtc)->pipe] =
  10681. modeset_get_crtc_power_domains(crtc,
  10682. to_intel_crtc_state(new_crtc_state));
  10683. }
  10684. if (!needs_modeset(new_crtc_state))
  10685. continue;
  10686. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10687. to_intel_crtc_state(new_crtc_state));
  10688. if (old_crtc_state->active) {
  10689. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10690. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10691. intel_crtc->active = false;
  10692. intel_fbc_disable(intel_crtc);
  10693. intel_disable_shared_dpll(intel_crtc);
  10694. /*
  10695. * Underruns don't always raise
  10696. * interrupts, so check manually.
  10697. */
  10698. intel_check_cpu_fifo_underruns(dev_priv);
  10699. intel_check_pch_fifo_underruns(dev_priv);
  10700. if (!crtc->state->active) {
  10701. /*
  10702. * Make sure we don't call initial_watermarks
  10703. * for ILK-style watermark updates.
  10704. *
  10705. * No clue what this is supposed to achieve.
  10706. */
  10707. if (INTEL_GEN(dev_priv) >= 9)
  10708. dev_priv->display.initial_watermarks(intel_state,
  10709. to_intel_crtc_state(crtc->state));
  10710. }
  10711. }
  10712. }
  10713. /* Only after disabling all output pipelines that will be changed can we
  10714. * update the the output configuration. */
  10715. intel_modeset_update_crtc_state(state);
  10716. if (intel_state->modeset) {
  10717. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10718. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10719. /*
  10720. * SKL workaround: bspec recommends we disable the SAGV when we
  10721. * have more then one pipe enabled
  10722. */
  10723. if (!intel_can_enable_sagv(state))
  10724. intel_disable_sagv(dev_priv);
  10725. intel_modeset_verify_disabled(dev, state);
  10726. }
  10727. /* Complete the events for pipes that have now been disabled */
  10728. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10729. bool modeset = needs_modeset(new_crtc_state);
  10730. /* Complete events for now disable pipes here. */
  10731. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10732. spin_lock_irq(&dev->event_lock);
  10733. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10734. spin_unlock_irq(&dev->event_lock);
  10735. new_crtc_state->event = NULL;
  10736. }
  10737. }
  10738. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10739. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10740. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10741. * already, but still need the state for the delayed optimization. To
  10742. * fix this:
  10743. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10744. * - schedule that vblank worker _before_ calling hw_done
  10745. * - at the start of commit_tail, cancel it _synchrously
  10746. * - switch over to the vblank wait helper in the core after that since
  10747. * we don't need out special handling any more.
  10748. */
  10749. if (!state->legacy_cursor_update)
  10750. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10751. /*
  10752. * Now that the vblank has passed, we can go ahead and program the
  10753. * optimal watermarks on platforms that need two-step watermark
  10754. * programming.
  10755. *
  10756. * TODO: Move this (and other cleanup) to an async worker eventually.
  10757. */
  10758. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10759. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10760. if (dev_priv->display.optimize_watermarks)
  10761. dev_priv->display.optimize_watermarks(intel_state,
  10762. intel_cstate);
  10763. }
  10764. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10765. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10766. if (put_domains[i])
  10767. modeset_put_power_domains(dev_priv, put_domains[i]);
  10768. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10769. }
  10770. if (intel_state->modeset && intel_can_enable_sagv(state))
  10771. intel_enable_sagv(dev_priv);
  10772. drm_atomic_helper_commit_hw_done(state);
  10773. if (intel_state->modeset)
  10774. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10775. mutex_lock(&dev->struct_mutex);
  10776. drm_atomic_helper_cleanup_planes(dev, state);
  10777. mutex_unlock(&dev->struct_mutex);
  10778. drm_atomic_helper_commit_cleanup_done(state);
  10779. drm_atomic_state_put(state);
  10780. /* As one of the primary mmio accessors, KMS has a high likelihood
  10781. * of triggering bugs in unclaimed access. After we finish
  10782. * modesetting, see if an error has been flagged, and if so
  10783. * enable debugging for the next modeset - and hope we catch
  10784. * the culprit.
  10785. *
  10786. * XXX note that we assume display power is on at this point.
  10787. * This might hold true now but we need to add pm helper to check
  10788. * unclaimed only when the hardware is on, as atomic commits
  10789. * can happen also when the device is completely off.
  10790. */
  10791. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10792. intel_atomic_helper_free_state(dev_priv);
  10793. }
  10794. static void intel_atomic_commit_work(struct work_struct *work)
  10795. {
  10796. struct drm_atomic_state *state =
  10797. container_of(work, struct drm_atomic_state, commit_work);
  10798. intel_atomic_commit_tail(state);
  10799. }
  10800. static int __i915_sw_fence_call
  10801. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10802. enum i915_sw_fence_notify notify)
  10803. {
  10804. struct intel_atomic_state *state =
  10805. container_of(fence, struct intel_atomic_state, commit_ready);
  10806. switch (notify) {
  10807. case FENCE_COMPLETE:
  10808. if (state->base.commit_work.func)
  10809. queue_work(system_unbound_wq, &state->base.commit_work);
  10810. break;
  10811. case FENCE_FREE:
  10812. {
  10813. struct intel_atomic_helper *helper =
  10814. &to_i915(state->base.dev)->atomic_helper;
  10815. if (llist_add(&state->freed, &helper->free_list))
  10816. schedule_work(&helper->free_work);
  10817. break;
  10818. }
  10819. }
  10820. return NOTIFY_DONE;
  10821. }
  10822. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10823. {
  10824. struct drm_plane_state *old_plane_state, *new_plane_state;
  10825. struct drm_plane *plane;
  10826. int i;
  10827. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10828. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10829. intel_fb_obj(new_plane_state->fb),
  10830. to_intel_plane(plane)->frontbuffer_bit);
  10831. }
  10832. /**
  10833. * intel_atomic_commit - commit validated state object
  10834. * @dev: DRM device
  10835. * @state: the top-level driver state object
  10836. * @nonblock: nonblocking commit
  10837. *
  10838. * This function commits a top-level state object that has been validated
  10839. * with drm_atomic_helper_check().
  10840. *
  10841. * RETURNS
  10842. * Zero for success or -errno.
  10843. */
  10844. static int intel_atomic_commit(struct drm_device *dev,
  10845. struct drm_atomic_state *state,
  10846. bool nonblock)
  10847. {
  10848. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10849. struct drm_i915_private *dev_priv = to_i915(dev);
  10850. int ret = 0;
  10851. /*
  10852. * The intel_legacy_cursor_update() fast path takes care
  10853. * of avoiding the vblank waits for simple cursor
  10854. * movement and flips. For cursor on/off and size changes,
  10855. * we want to perform the vblank waits so that watermark
  10856. * updates happen during the correct frames. Gen9+ have
  10857. * double buffered watermarks and so shouldn't need this.
  10858. */
  10859. if (INTEL_GEN(dev_priv) < 9)
  10860. state->legacy_cursor_update = false;
  10861. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10862. if (ret)
  10863. return ret;
  10864. drm_atomic_state_get(state);
  10865. i915_sw_fence_init(&intel_state->commit_ready,
  10866. intel_atomic_commit_ready);
  10867. ret = intel_atomic_prepare_commit(dev, state);
  10868. if (ret) {
  10869. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10870. i915_sw_fence_commit(&intel_state->commit_ready);
  10871. return ret;
  10872. }
  10873. drm_atomic_helper_swap_state(state, true);
  10874. dev_priv->wm.distrust_bios_wm = false;
  10875. intel_shared_dpll_swap_state(state);
  10876. intel_atomic_track_fbs(state);
  10877. if (intel_state->modeset) {
  10878. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10879. sizeof(intel_state->min_pixclk));
  10880. dev_priv->active_crtcs = intel_state->active_crtcs;
  10881. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10882. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10883. }
  10884. drm_atomic_state_get(state);
  10885. INIT_WORK(&state->commit_work,
  10886. nonblock ? intel_atomic_commit_work : NULL);
  10887. i915_sw_fence_commit(&intel_state->commit_ready);
  10888. if (!nonblock) {
  10889. i915_sw_fence_wait(&intel_state->commit_ready);
  10890. intel_atomic_commit_tail(state);
  10891. }
  10892. return 0;
  10893. }
  10894. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10895. {
  10896. struct drm_device *dev = crtc->dev;
  10897. struct drm_atomic_state *state;
  10898. struct drm_crtc_state *crtc_state;
  10899. int ret;
  10900. state = drm_atomic_state_alloc(dev);
  10901. if (!state) {
  10902. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  10903. crtc->base.id, crtc->name);
  10904. return;
  10905. }
  10906. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  10907. retry:
  10908. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10909. ret = PTR_ERR_OR_ZERO(crtc_state);
  10910. if (!ret) {
  10911. if (!crtc_state->active)
  10912. goto out;
  10913. crtc_state->mode_changed = true;
  10914. ret = drm_atomic_commit(state);
  10915. }
  10916. if (ret == -EDEADLK) {
  10917. drm_atomic_state_clear(state);
  10918. drm_modeset_backoff(state->acquire_ctx);
  10919. goto retry;
  10920. }
  10921. out:
  10922. drm_atomic_state_put(state);
  10923. }
  10924. /*
  10925. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  10926. * drm_atomic_helper_legacy_gamma_set() directly.
  10927. */
  10928. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  10929. u16 *red, u16 *green, u16 *blue,
  10930. uint32_t size)
  10931. {
  10932. struct drm_device *dev = crtc->dev;
  10933. struct drm_mode_config *config = &dev->mode_config;
  10934. struct drm_crtc_state *state;
  10935. int ret;
  10936. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  10937. if (ret)
  10938. return ret;
  10939. /*
  10940. * Make sure we update the legacy properties so this works when
  10941. * atomic is not enabled.
  10942. */
  10943. state = crtc->state;
  10944. drm_object_property_set_value(&crtc->base,
  10945. config->degamma_lut_property,
  10946. (state->degamma_lut) ?
  10947. state->degamma_lut->base.id : 0);
  10948. drm_object_property_set_value(&crtc->base,
  10949. config->ctm_property,
  10950. (state->ctm) ?
  10951. state->ctm->base.id : 0);
  10952. drm_object_property_set_value(&crtc->base,
  10953. config->gamma_lut_property,
  10954. (state->gamma_lut) ?
  10955. state->gamma_lut->base.id : 0);
  10956. return 0;
  10957. }
  10958. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10959. .gamma_set = intel_atomic_legacy_gamma_set,
  10960. .set_config = drm_atomic_helper_set_config,
  10961. .set_property = drm_atomic_helper_crtc_set_property,
  10962. .destroy = intel_crtc_destroy,
  10963. .page_flip = drm_atomic_helper_page_flip,
  10964. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10965. .atomic_destroy_state = intel_crtc_destroy_state,
  10966. .set_crc_source = intel_crtc_set_crc_source,
  10967. };
  10968. /**
  10969. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10970. * @plane: drm plane to prepare for
  10971. * @fb: framebuffer to prepare for presentation
  10972. *
  10973. * Prepares a framebuffer for usage on a display plane. Generally this
  10974. * involves pinning the underlying object and updating the frontbuffer tracking
  10975. * bits. Some older platforms need special physical address handling for
  10976. * cursor planes.
  10977. *
  10978. * Must be called with struct_mutex held.
  10979. *
  10980. * Returns 0 on success, negative error code on failure.
  10981. */
  10982. int
  10983. intel_prepare_plane_fb(struct drm_plane *plane,
  10984. struct drm_plane_state *new_state)
  10985. {
  10986. struct intel_atomic_state *intel_state =
  10987. to_intel_atomic_state(new_state->state);
  10988. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10989. struct drm_framebuffer *fb = new_state->fb;
  10990. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10991. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10992. int ret;
  10993. if (obj) {
  10994. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10995. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10996. const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  10997. ret = i915_gem_object_attach_phys(obj, align);
  10998. if (ret) {
  10999. DRM_DEBUG_KMS("failed to attach phys object\n");
  11000. return ret;
  11001. }
  11002. } else {
  11003. struct i915_vma *vma;
  11004. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  11005. if (IS_ERR(vma)) {
  11006. DRM_DEBUG_KMS("failed to pin object\n");
  11007. return PTR_ERR(vma);
  11008. }
  11009. to_intel_plane_state(new_state)->vma = vma;
  11010. }
  11011. }
  11012. if (!obj && !old_obj)
  11013. return 0;
  11014. if (old_obj) {
  11015. struct drm_crtc_state *crtc_state =
  11016. drm_atomic_get_existing_crtc_state(new_state->state,
  11017. plane->state->crtc);
  11018. /* Big Hammer, we also need to ensure that any pending
  11019. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11020. * current scanout is retired before unpinning the old
  11021. * framebuffer. Note that we rely on userspace rendering
  11022. * into the buffer attached to the pipe they are waiting
  11023. * on. If not, userspace generates a GPU hang with IPEHR
  11024. * point to the MI_WAIT_FOR_EVENT.
  11025. *
  11026. * This should only fail upon a hung GPU, in which case we
  11027. * can safely continue.
  11028. */
  11029. if (needs_modeset(crtc_state)) {
  11030. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11031. old_obj->resv, NULL,
  11032. false, 0,
  11033. GFP_KERNEL);
  11034. if (ret < 0)
  11035. return ret;
  11036. }
  11037. }
  11038. if (new_state->fence) { /* explicit fencing */
  11039. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11040. new_state->fence,
  11041. I915_FENCE_TIMEOUT,
  11042. GFP_KERNEL);
  11043. if (ret < 0)
  11044. return ret;
  11045. }
  11046. if (!obj)
  11047. return 0;
  11048. if (!new_state->fence) { /* implicit fencing */
  11049. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11050. obj->resv, NULL,
  11051. false, I915_FENCE_TIMEOUT,
  11052. GFP_KERNEL);
  11053. if (ret < 0)
  11054. return ret;
  11055. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11056. }
  11057. return 0;
  11058. }
  11059. /**
  11060. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11061. * @plane: drm plane to clean up for
  11062. * @fb: old framebuffer that was on plane
  11063. *
  11064. * Cleans up a framebuffer that has just been removed from a plane.
  11065. *
  11066. * Must be called with struct_mutex held.
  11067. */
  11068. void
  11069. intel_cleanup_plane_fb(struct drm_plane *plane,
  11070. struct drm_plane_state *old_state)
  11071. {
  11072. struct i915_vma *vma;
  11073. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11074. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11075. if (vma)
  11076. intel_unpin_fb_vma(vma);
  11077. }
  11078. int
  11079. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11080. {
  11081. struct drm_i915_private *dev_priv;
  11082. int max_scale;
  11083. int crtc_clock, max_dotclk;
  11084. if (!intel_crtc || !crtc_state->base.enable)
  11085. return DRM_PLANE_HELPER_NO_SCALING;
  11086. dev_priv = to_i915(intel_crtc->base.dev);
  11087. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11088. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11089. if (IS_GEMINILAKE(dev_priv))
  11090. max_dotclk *= 2;
  11091. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11092. return DRM_PLANE_HELPER_NO_SCALING;
  11093. /*
  11094. * skl max scale is lower of:
  11095. * close to 3 but not 3, -1 is for that purpose
  11096. * or
  11097. * cdclk/crtc_clock
  11098. */
  11099. max_scale = min((1 << 16) * 3 - 1,
  11100. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  11101. return max_scale;
  11102. }
  11103. static int
  11104. intel_check_primary_plane(struct drm_plane *plane,
  11105. struct intel_crtc_state *crtc_state,
  11106. struct intel_plane_state *state)
  11107. {
  11108. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11109. struct drm_crtc *crtc = state->base.crtc;
  11110. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11111. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11112. bool can_position = false;
  11113. int ret;
  11114. if (INTEL_GEN(dev_priv) >= 9) {
  11115. /* use scaler when colorkey is not required */
  11116. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11117. min_scale = 1;
  11118. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11119. }
  11120. can_position = true;
  11121. }
  11122. ret = drm_plane_helper_check_state(&state->base,
  11123. &state->clip,
  11124. min_scale, max_scale,
  11125. can_position, true);
  11126. if (ret)
  11127. return ret;
  11128. if (!state->base.fb)
  11129. return 0;
  11130. if (INTEL_GEN(dev_priv) >= 9) {
  11131. ret = skl_check_plane_surface(state);
  11132. if (ret)
  11133. return ret;
  11134. }
  11135. return 0;
  11136. }
  11137. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11138. struct drm_crtc_state *old_crtc_state)
  11139. {
  11140. struct drm_device *dev = crtc->dev;
  11141. struct drm_i915_private *dev_priv = to_i915(dev);
  11142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11143. struct intel_crtc_state *intel_cstate =
  11144. to_intel_crtc_state(crtc->state);
  11145. struct intel_crtc_state *old_intel_cstate =
  11146. to_intel_crtc_state(old_crtc_state);
  11147. struct intel_atomic_state *old_intel_state =
  11148. to_intel_atomic_state(old_crtc_state->state);
  11149. bool modeset = needs_modeset(crtc->state);
  11150. if (!modeset &&
  11151. (intel_cstate->base.color_mgmt_changed ||
  11152. intel_cstate->update_pipe)) {
  11153. intel_color_set_csc(crtc->state);
  11154. intel_color_load_luts(crtc->state);
  11155. }
  11156. /* Perform vblank evasion around commit operation */
  11157. intel_pipe_update_start(intel_crtc);
  11158. if (modeset)
  11159. goto out;
  11160. if (intel_cstate->update_pipe)
  11161. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11162. else if (INTEL_GEN(dev_priv) >= 9)
  11163. skl_detach_scalers(intel_crtc);
  11164. out:
  11165. if (dev_priv->display.atomic_update_watermarks)
  11166. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11167. intel_cstate);
  11168. }
  11169. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11170. struct drm_crtc_state *old_crtc_state)
  11171. {
  11172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11173. intel_pipe_update_end(intel_crtc, NULL);
  11174. }
  11175. /**
  11176. * intel_plane_destroy - destroy a plane
  11177. * @plane: plane to destroy
  11178. *
  11179. * Common destruction function for all types of planes (primary, cursor,
  11180. * sprite).
  11181. */
  11182. void intel_plane_destroy(struct drm_plane *plane)
  11183. {
  11184. drm_plane_cleanup(plane);
  11185. kfree(to_intel_plane(plane));
  11186. }
  11187. const struct drm_plane_funcs intel_plane_funcs = {
  11188. .update_plane = drm_atomic_helper_update_plane,
  11189. .disable_plane = drm_atomic_helper_disable_plane,
  11190. .destroy = intel_plane_destroy,
  11191. .set_property = drm_atomic_helper_plane_set_property,
  11192. .atomic_get_property = intel_plane_atomic_get_property,
  11193. .atomic_set_property = intel_plane_atomic_set_property,
  11194. .atomic_duplicate_state = intel_plane_duplicate_state,
  11195. .atomic_destroy_state = intel_plane_destroy_state,
  11196. };
  11197. static int
  11198. intel_legacy_cursor_update(struct drm_plane *plane,
  11199. struct drm_crtc *crtc,
  11200. struct drm_framebuffer *fb,
  11201. int crtc_x, int crtc_y,
  11202. unsigned int crtc_w, unsigned int crtc_h,
  11203. uint32_t src_x, uint32_t src_y,
  11204. uint32_t src_w, uint32_t src_h,
  11205. struct drm_modeset_acquire_ctx *ctx)
  11206. {
  11207. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11208. int ret;
  11209. struct drm_plane_state *old_plane_state, *new_plane_state;
  11210. struct intel_plane *intel_plane = to_intel_plane(plane);
  11211. struct drm_framebuffer *old_fb;
  11212. struct drm_crtc_state *crtc_state = crtc->state;
  11213. struct i915_vma *old_vma;
  11214. /*
  11215. * When crtc is inactive or there is a modeset pending,
  11216. * wait for it to complete in the slowpath
  11217. */
  11218. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11219. to_intel_crtc_state(crtc_state)->update_pipe)
  11220. goto slow;
  11221. old_plane_state = plane->state;
  11222. /*
  11223. * If any parameters change that may affect watermarks,
  11224. * take the slowpath. Only changing fb or position should be
  11225. * in the fastpath.
  11226. */
  11227. if (old_plane_state->crtc != crtc ||
  11228. old_plane_state->src_w != src_w ||
  11229. old_plane_state->src_h != src_h ||
  11230. old_plane_state->crtc_w != crtc_w ||
  11231. old_plane_state->crtc_h != crtc_h ||
  11232. !old_plane_state->fb != !fb)
  11233. goto slow;
  11234. new_plane_state = intel_plane_duplicate_state(plane);
  11235. if (!new_plane_state)
  11236. return -ENOMEM;
  11237. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11238. new_plane_state->src_x = src_x;
  11239. new_plane_state->src_y = src_y;
  11240. new_plane_state->src_w = src_w;
  11241. new_plane_state->src_h = src_h;
  11242. new_plane_state->crtc_x = crtc_x;
  11243. new_plane_state->crtc_y = crtc_y;
  11244. new_plane_state->crtc_w = crtc_w;
  11245. new_plane_state->crtc_h = crtc_h;
  11246. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11247. to_intel_plane_state(new_plane_state));
  11248. if (ret)
  11249. goto out_free;
  11250. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11251. if (ret)
  11252. goto out_free;
  11253. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11254. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11255. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11256. if (ret) {
  11257. DRM_DEBUG_KMS("failed to attach phys object\n");
  11258. goto out_unlock;
  11259. }
  11260. } else {
  11261. struct i915_vma *vma;
  11262. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11263. if (IS_ERR(vma)) {
  11264. DRM_DEBUG_KMS("failed to pin object\n");
  11265. ret = PTR_ERR(vma);
  11266. goto out_unlock;
  11267. }
  11268. to_intel_plane_state(new_plane_state)->vma = vma;
  11269. }
  11270. old_fb = old_plane_state->fb;
  11271. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11272. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11273. intel_plane->frontbuffer_bit);
  11274. /* Swap plane state */
  11275. new_plane_state->fence = old_plane_state->fence;
  11276. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11277. new_plane_state->fence = NULL;
  11278. new_plane_state->fb = old_fb;
  11279. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11280. if (plane->state->visible) {
  11281. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11282. intel_plane->update_plane(plane,
  11283. to_intel_crtc_state(crtc->state),
  11284. to_intel_plane_state(plane->state));
  11285. } else {
  11286. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11287. intel_plane->disable_plane(plane, crtc);
  11288. }
  11289. intel_cleanup_plane_fb(plane, new_plane_state);
  11290. out_unlock:
  11291. mutex_unlock(&dev_priv->drm.struct_mutex);
  11292. out_free:
  11293. intel_plane_destroy_state(plane, new_plane_state);
  11294. return ret;
  11295. slow:
  11296. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11297. crtc_x, crtc_y, crtc_w, crtc_h,
  11298. src_x, src_y, src_w, src_h, ctx);
  11299. }
  11300. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11301. .update_plane = intel_legacy_cursor_update,
  11302. .disable_plane = drm_atomic_helper_disable_plane,
  11303. .destroy = intel_plane_destroy,
  11304. .set_property = drm_atomic_helper_plane_set_property,
  11305. .atomic_get_property = intel_plane_atomic_get_property,
  11306. .atomic_set_property = intel_plane_atomic_set_property,
  11307. .atomic_duplicate_state = intel_plane_duplicate_state,
  11308. .atomic_destroy_state = intel_plane_destroy_state,
  11309. };
  11310. static struct intel_plane *
  11311. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11312. {
  11313. struct intel_plane *primary = NULL;
  11314. struct intel_plane_state *state = NULL;
  11315. const uint32_t *intel_primary_formats;
  11316. unsigned int supported_rotations;
  11317. unsigned int num_formats;
  11318. int ret;
  11319. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11320. if (!primary) {
  11321. ret = -ENOMEM;
  11322. goto fail;
  11323. }
  11324. state = intel_create_plane_state(&primary->base);
  11325. if (!state) {
  11326. ret = -ENOMEM;
  11327. goto fail;
  11328. }
  11329. primary->base.state = &state->base;
  11330. primary->can_scale = false;
  11331. primary->max_downscale = 1;
  11332. if (INTEL_GEN(dev_priv) >= 9) {
  11333. primary->can_scale = true;
  11334. state->scaler_id = -1;
  11335. }
  11336. primary->pipe = pipe;
  11337. /*
  11338. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11339. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11340. */
  11341. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11342. primary->plane = (enum plane) !pipe;
  11343. else
  11344. primary->plane = (enum plane) pipe;
  11345. primary->id = PLANE_PRIMARY;
  11346. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11347. primary->check_plane = intel_check_primary_plane;
  11348. if (INTEL_GEN(dev_priv) >= 9) {
  11349. intel_primary_formats = skl_primary_formats;
  11350. num_formats = ARRAY_SIZE(skl_primary_formats);
  11351. primary->update_plane = skylake_update_primary_plane;
  11352. primary->disable_plane = skylake_disable_primary_plane;
  11353. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11354. intel_primary_formats = i965_primary_formats;
  11355. num_formats = ARRAY_SIZE(i965_primary_formats);
  11356. primary->update_plane = ironlake_update_primary_plane;
  11357. primary->disable_plane = i9xx_disable_primary_plane;
  11358. } else if (INTEL_GEN(dev_priv) >= 4) {
  11359. intel_primary_formats = i965_primary_formats;
  11360. num_formats = ARRAY_SIZE(i965_primary_formats);
  11361. primary->update_plane = i9xx_update_primary_plane;
  11362. primary->disable_plane = i9xx_disable_primary_plane;
  11363. } else {
  11364. intel_primary_formats = i8xx_primary_formats;
  11365. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11366. primary->update_plane = i9xx_update_primary_plane;
  11367. primary->disable_plane = i9xx_disable_primary_plane;
  11368. }
  11369. if (INTEL_GEN(dev_priv) >= 9)
  11370. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11371. 0, &intel_plane_funcs,
  11372. intel_primary_formats, num_formats,
  11373. DRM_PLANE_TYPE_PRIMARY,
  11374. "plane 1%c", pipe_name(pipe));
  11375. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11376. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11377. 0, &intel_plane_funcs,
  11378. intel_primary_formats, num_formats,
  11379. DRM_PLANE_TYPE_PRIMARY,
  11380. "primary %c", pipe_name(pipe));
  11381. else
  11382. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11383. 0, &intel_plane_funcs,
  11384. intel_primary_formats, num_formats,
  11385. DRM_PLANE_TYPE_PRIMARY,
  11386. "plane %c", plane_name(primary->plane));
  11387. if (ret)
  11388. goto fail;
  11389. if (INTEL_GEN(dev_priv) >= 9) {
  11390. supported_rotations =
  11391. DRM_ROTATE_0 | DRM_ROTATE_90 |
  11392. DRM_ROTATE_180 | DRM_ROTATE_270;
  11393. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11394. supported_rotations =
  11395. DRM_ROTATE_0 | DRM_ROTATE_180 |
  11396. DRM_REFLECT_X;
  11397. } else if (INTEL_GEN(dev_priv) >= 4) {
  11398. supported_rotations =
  11399. DRM_ROTATE_0 | DRM_ROTATE_180;
  11400. } else {
  11401. supported_rotations = DRM_ROTATE_0;
  11402. }
  11403. if (INTEL_GEN(dev_priv) >= 4)
  11404. drm_plane_create_rotation_property(&primary->base,
  11405. DRM_ROTATE_0,
  11406. supported_rotations);
  11407. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11408. return primary;
  11409. fail:
  11410. kfree(state);
  11411. kfree(primary);
  11412. return ERR_PTR(ret);
  11413. }
  11414. static int
  11415. intel_check_cursor_plane(struct drm_plane *plane,
  11416. struct intel_crtc_state *crtc_state,
  11417. struct intel_plane_state *state)
  11418. {
  11419. struct drm_framebuffer *fb = state->base.fb;
  11420. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11421. enum pipe pipe = to_intel_plane(plane)->pipe;
  11422. unsigned stride;
  11423. int ret;
  11424. ret = drm_plane_helper_check_state(&state->base,
  11425. &state->clip,
  11426. DRM_PLANE_HELPER_NO_SCALING,
  11427. DRM_PLANE_HELPER_NO_SCALING,
  11428. true, true);
  11429. if (ret)
  11430. return ret;
  11431. /* if we want to turn off the cursor ignore width and height */
  11432. if (!obj)
  11433. return 0;
  11434. /* Check for which cursor types we support */
  11435. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  11436. state->base.crtc_h)) {
  11437. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11438. state->base.crtc_w, state->base.crtc_h);
  11439. return -EINVAL;
  11440. }
  11441. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11442. if (obj->base.size < stride * state->base.crtc_h) {
  11443. DRM_DEBUG_KMS("buffer is too small\n");
  11444. return -ENOMEM;
  11445. }
  11446. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  11447. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11448. return -EINVAL;
  11449. }
  11450. /*
  11451. * There's something wrong with the cursor on CHV pipe C.
  11452. * If it straddles the left edge of the screen then
  11453. * moving it away from the edge or disabling it often
  11454. * results in a pipe underrun, and often that can lead to
  11455. * dead pipe (constant underrun reported, and it scans
  11456. * out just a solid color). To recover from that, the
  11457. * display power well must be turned off and on again.
  11458. * Refuse the put the cursor into that compromised position.
  11459. */
  11460. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  11461. state->base.visible && state->base.crtc_x < 0) {
  11462. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11463. return -EINVAL;
  11464. }
  11465. return 0;
  11466. }
  11467. static void
  11468. intel_disable_cursor_plane(struct drm_plane *plane,
  11469. struct drm_crtc *crtc)
  11470. {
  11471. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11472. intel_crtc->cursor_addr = 0;
  11473. intel_crtc_update_cursor(crtc, NULL);
  11474. }
  11475. static void
  11476. intel_update_cursor_plane(struct drm_plane *plane,
  11477. const struct intel_crtc_state *crtc_state,
  11478. const struct intel_plane_state *state)
  11479. {
  11480. struct drm_crtc *crtc = crtc_state->base.crtc;
  11481. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11482. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11483. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11484. uint32_t addr;
  11485. if (!obj)
  11486. addr = 0;
  11487. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  11488. addr = intel_plane_ggtt_offset(state);
  11489. else
  11490. addr = obj->phys_handle->busaddr;
  11491. intel_crtc->cursor_addr = addr;
  11492. intel_crtc_update_cursor(crtc, state);
  11493. }
  11494. static struct intel_plane *
  11495. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11496. {
  11497. struct intel_plane *cursor = NULL;
  11498. struct intel_plane_state *state = NULL;
  11499. int ret;
  11500. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11501. if (!cursor) {
  11502. ret = -ENOMEM;
  11503. goto fail;
  11504. }
  11505. state = intel_create_plane_state(&cursor->base);
  11506. if (!state) {
  11507. ret = -ENOMEM;
  11508. goto fail;
  11509. }
  11510. cursor->base.state = &state->base;
  11511. cursor->can_scale = false;
  11512. cursor->max_downscale = 1;
  11513. cursor->pipe = pipe;
  11514. cursor->plane = pipe;
  11515. cursor->id = PLANE_CURSOR;
  11516. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11517. cursor->check_plane = intel_check_cursor_plane;
  11518. cursor->update_plane = intel_update_cursor_plane;
  11519. cursor->disable_plane = intel_disable_cursor_plane;
  11520. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11521. 0, &intel_cursor_plane_funcs,
  11522. intel_cursor_formats,
  11523. ARRAY_SIZE(intel_cursor_formats),
  11524. DRM_PLANE_TYPE_CURSOR,
  11525. "cursor %c", pipe_name(pipe));
  11526. if (ret)
  11527. goto fail;
  11528. if (INTEL_GEN(dev_priv) >= 4)
  11529. drm_plane_create_rotation_property(&cursor->base,
  11530. DRM_ROTATE_0,
  11531. DRM_ROTATE_0 |
  11532. DRM_ROTATE_180);
  11533. if (INTEL_GEN(dev_priv) >= 9)
  11534. state->scaler_id = -1;
  11535. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11536. return cursor;
  11537. fail:
  11538. kfree(state);
  11539. kfree(cursor);
  11540. return ERR_PTR(ret);
  11541. }
  11542. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11543. struct intel_crtc_state *crtc_state)
  11544. {
  11545. struct intel_crtc_scaler_state *scaler_state =
  11546. &crtc_state->scaler_state;
  11547. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11548. int i;
  11549. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11550. if (!crtc->num_scalers)
  11551. return;
  11552. for (i = 0; i < crtc->num_scalers; i++) {
  11553. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11554. scaler->in_use = 0;
  11555. scaler->mode = PS_SCALER_MODE_DYN;
  11556. }
  11557. scaler_state->scaler_id = -1;
  11558. }
  11559. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11560. {
  11561. struct intel_crtc *intel_crtc;
  11562. struct intel_crtc_state *crtc_state = NULL;
  11563. struct intel_plane *primary = NULL;
  11564. struct intel_plane *cursor = NULL;
  11565. int sprite, ret;
  11566. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11567. if (!intel_crtc)
  11568. return -ENOMEM;
  11569. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11570. if (!crtc_state) {
  11571. ret = -ENOMEM;
  11572. goto fail;
  11573. }
  11574. intel_crtc->config = crtc_state;
  11575. intel_crtc->base.state = &crtc_state->base;
  11576. crtc_state->base.crtc = &intel_crtc->base;
  11577. primary = intel_primary_plane_create(dev_priv, pipe);
  11578. if (IS_ERR(primary)) {
  11579. ret = PTR_ERR(primary);
  11580. goto fail;
  11581. }
  11582. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11583. for_each_sprite(dev_priv, pipe, sprite) {
  11584. struct intel_plane *plane;
  11585. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11586. if (IS_ERR(plane)) {
  11587. ret = PTR_ERR(plane);
  11588. goto fail;
  11589. }
  11590. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11591. }
  11592. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11593. if (IS_ERR(cursor)) {
  11594. ret = PTR_ERR(cursor);
  11595. goto fail;
  11596. }
  11597. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11598. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11599. &primary->base, &cursor->base,
  11600. &intel_crtc_funcs,
  11601. "pipe %c", pipe_name(pipe));
  11602. if (ret)
  11603. goto fail;
  11604. intel_crtc->pipe = pipe;
  11605. intel_crtc->plane = primary->plane;
  11606. intel_crtc->cursor_base = ~0;
  11607. intel_crtc->cursor_cntl = ~0;
  11608. intel_crtc->cursor_size = ~0;
  11609. /* initialize shared scalers */
  11610. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11611. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11612. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11613. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11614. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11615. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11616. intel_color_init(&intel_crtc->base);
  11617. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11618. return 0;
  11619. fail:
  11620. /*
  11621. * drm_mode_config_cleanup() will free up any
  11622. * crtcs/planes already initialized.
  11623. */
  11624. kfree(crtc_state);
  11625. kfree(intel_crtc);
  11626. return ret;
  11627. }
  11628. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11629. {
  11630. struct drm_device *dev = connector->base.dev;
  11631. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11632. if (!connector->base.state->crtc)
  11633. return INVALID_PIPE;
  11634. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11635. }
  11636. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11637. struct drm_file *file)
  11638. {
  11639. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11640. struct drm_crtc *drmmode_crtc;
  11641. struct intel_crtc *crtc;
  11642. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11643. if (!drmmode_crtc)
  11644. return -ENOENT;
  11645. crtc = to_intel_crtc(drmmode_crtc);
  11646. pipe_from_crtc_id->pipe = crtc->pipe;
  11647. return 0;
  11648. }
  11649. static int intel_encoder_clones(struct intel_encoder *encoder)
  11650. {
  11651. struct drm_device *dev = encoder->base.dev;
  11652. struct intel_encoder *source_encoder;
  11653. int index_mask = 0;
  11654. int entry = 0;
  11655. for_each_intel_encoder(dev, source_encoder) {
  11656. if (encoders_cloneable(encoder, source_encoder))
  11657. index_mask |= (1 << entry);
  11658. entry++;
  11659. }
  11660. return index_mask;
  11661. }
  11662. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11663. {
  11664. if (!IS_MOBILE(dev_priv))
  11665. return false;
  11666. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11667. return false;
  11668. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11669. return false;
  11670. return true;
  11671. }
  11672. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11673. {
  11674. if (INTEL_GEN(dev_priv) >= 9)
  11675. return false;
  11676. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11677. return false;
  11678. if (IS_CHERRYVIEW(dev_priv))
  11679. return false;
  11680. if (HAS_PCH_LPT_H(dev_priv) &&
  11681. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11682. return false;
  11683. /* DDI E can't be used if DDI A requires 4 lanes */
  11684. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11685. return false;
  11686. if (!dev_priv->vbt.int_crt_support)
  11687. return false;
  11688. return true;
  11689. }
  11690. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11691. {
  11692. int pps_num;
  11693. int pps_idx;
  11694. if (HAS_DDI(dev_priv))
  11695. return;
  11696. /*
  11697. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11698. * everywhere where registers can be write protected.
  11699. */
  11700. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11701. pps_num = 2;
  11702. else
  11703. pps_num = 1;
  11704. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11705. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11706. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11707. I915_WRITE(PP_CONTROL(pps_idx), val);
  11708. }
  11709. }
  11710. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11711. {
  11712. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11713. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11714. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11715. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11716. else
  11717. dev_priv->pps_mmio_base = PPS_BASE;
  11718. intel_pps_unlock_regs_wa(dev_priv);
  11719. }
  11720. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11721. {
  11722. struct intel_encoder *encoder;
  11723. bool dpd_is_edp = false;
  11724. intel_pps_init(dev_priv);
  11725. /*
  11726. * intel_edp_init_connector() depends on this completing first, to
  11727. * prevent the registeration of both eDP and LVDS and the incorrect
  11728. * sharing of the PPS.
  11729. */
  11730. intel_lvds_init(dev_priv);
  11731. if (intel_crt_present(dev_priv))
  11732. intel_crt_init(dev_priv);
  11733. if (IS_GEN9_LP(dev_priv)) {
  11734. /*
  11735. * FIXME: Broxton doesn't support port detection via the
  11736. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11737. * detect the ports.
  11738. */
  11739. intel_ddi_init(dev_priv, PORT_A);
  11740. intel_ddi_init(dev_priv, PORT_B);
  11741. intel_ddi_init(dev_priv, PORT_C);
  11742. intel_dsi_init(dev_priv);
  11743. } else if (HAS_DDI(dev_priv)) {
  11744. int found;
  11745. /*
  11746. * Haswell uses DDI functions to detect digital outputs.
  11747. * On SKL pre-D0 the strap isn't connected, so we assume
  11748. * it's there.
  11749. */
  11750. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11751. /* WaIgnoreDDIAStrap: skl */
  11752. if (found || IS_GEN9_BC(dev_priv))
  11753. intel_ddi_init(dev_priv, PORT_A);
  11754. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11755. * register */
  11756. found = I915_READ(SFUSE_STRAP);
  11757. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11758. intel_ddi_init(dev_priv, PORT_B);
  11759. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11760. intel_ddi_init(dev_priv, PORT_C);
  11761. if (found & SFUSE_STRAP_DDID_DETECTED)
  11762. intel_ddi_init(dev_priv, PORT_D);
  11763. /*
  11764. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11765. */
  11766. if (IS_GEN9_BC(dev_priv) &&
  11767. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11768. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11769. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11770. intel_ddi_init(dev_priv, PORT_E);
  11771. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11772. int found;
  11773. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11774. if (has_edp_a(dev_priv))
  11775. intel_dp_init(dev_priv, DP_A, PORT_A);
  11776. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11777. /* PCH SDVOB multiplex with HDMIB */
  11778. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11779. if (!found)
  11780. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11781. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11782. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11783. }
  11784. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11785. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11786. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11787. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11788. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11789. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11790. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11791. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11792. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11793. bool has_edp, has_port;
  11794. /*
  11795. * The DP_DETECTED bit is the latched state of the DDC
  11796. * SDA pin at boot. However since eDP doesn't require DDC
  11797. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11798. * eDP ports may have been muxed to an alternate function.
  11799. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11800. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11801. * detect eDP ports.
  11802. *
  11803. * Sadly the straps seem to be missing sometimes even for HDMI
  11804. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11805. * and VBT for the presence of the port. Additionally we can't
  11806. * trust the port type the VBT declares as we've seen at least
  11807. * HDMI ports that the VBT claim are DP or eDP.
  11808. */
  11809. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11810. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11811. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11812. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11813. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11814. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11815. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11816. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11817. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11818. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11819. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11820. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11821. if (IS_CHERRYVIEW(dev_priv)) {
  11822. /*
  11823. * eDP not supported on port D,
  11824. * so no need to worry about it
  11825. */
  11826. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11827. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11828. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11829. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11830. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11831. }
  11832. intel_dsi_init(dev_priv);
  11833. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11834. bool found = false;
  11835. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11836. DRM_DEBUG_KMS("probing SDVOB\n");
  11837. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11838. if (!found && IS_G4X(dev_priv)) {
  11839. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11840. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11841. }
  11842. if (!found && IS_G4X(dev_priv))
  11843. intel_dp_init(dev_priv, DP_B, PORT_B);
  11844. }
  11845. /* Before G4X SDVOC doesn't have its own detect register */
  11846. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11847. DRM_DEBUG_KMS("probing SDVOC\n");
  11848. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11849. }
  11850. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11851. if (IS_G4X(dev_priv)) {
  11852. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11853. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11854. }
  11855. if (IS_G4X(dev_priv))
  11856. intel_dp_init(dev_priv, DP_C, PORT_C);
  11857. }
  11858. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11859. intel_dp_init(dev_priv, DP_D, PORT_D);
  11860. } else if (IS_GEN2(dev_priv))
  11861. intel_dvo_init(dev_priv);
  11862. if (SUPPORTS_TV(dev_priv))
  11863. intel_tv_init(dev_priv);
  11864. intel_psr_init(dev_priv);
  11865. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11866. encoder->base.possible_crtcs = encoder->crtc_mask;
  11867. encoder->base.possible_clones =
  11868. intel_encoder_clones(encoder);
  11869. }
  11870. intel_init_pch_refclk(dev_priv);
  11871. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11872. }
  11873. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11874. {
  11875. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11876. drm_framebuffer_cleanup(fb);
  11877. i915_gem_object_lock(intel_fb->obj);
  11878. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11879. i915_gem_object_unlock(intel_fb->obj);
  11880. i915_gem_object_put(intel_fb->obj);
  11881. kfree(intel_fb);
  11882. }
  11883. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11884. struct drm_file *file,
  11885. unsigned int *handle)
  11886. {
  11887. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11888. struct drm_i915_gem_object *obj = intel_fb->obj;
  11889. if (obj->userptr.mm) {
  11890. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11891. return -EINVAL;
  11892. }
  11893. return drm_gem_handle_create(file, &obj->base, handle);
  11894. }
  11895. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11896. struct drm_file *file,
  11897. unsigned flags, unsigned color,
  11898. struct drm_clip_rect *clips,
  11899. unsigned num_clips)
  11900. {
  11901. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11902. i915_gem_object_flush_if_display(obj);
  11903. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11904. return 0;
  11905. }
  11906. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11907. .destroy = intel_user_framebuffer_destroy,
  11908. .create_handle = intel_user_framebuffer_create_handle,
  11909. .dirty = intel_user_framebuffer_dirty,
  11910. };
  11911. static
  11912. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11913. uint64_t fb_modifier, uint32_t pixel_format)
  11914. {
  11915. u32 gen = INTEL_GEN(dev_priv);
  11916. if (gen >= 9) {
  11917. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11918. /* "The stride in bytes must not exceed the of the size of 8K
  11919. * pixels and 32K bytes."
  11920. */
  11921. return min(8192 * cpp, 32768);
  11922. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11923. return 32*1024;
  11924. } else if (gen >= 4) {
  11925. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11926. return 16*1024;
  11927. else
  11928. return 32*1024;
  11929. } else if (gen >= 3) {
  11930. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11931. return 8*1024;
  11932. else
  11933. return 16*1024;
  11934. } else {
  11935. /* XXX DSPC is limited to 4k tiled */
  11936. return 8*1024;
  11937. }
  11938. }
  11939. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11940. struct drm_i915_gem_object *obj,
  11941. struct drm_mode_fb_cmd2 *mode_cmd)
  11942. {
  11943. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11944. struct drm_format_name_buf format_name;
  11945. u32 pitch_limit, stride_alignment;
  11946. unsigned int tiling, stride;
  11947. int ret = -EINVAL;
  11948. i915_gem_object_lock(obj);
  11949. obj->framebuffer_references++;
  11950. tiling = i915_gem_object_get_tiling(obj);
  11951. stride = i915_gem_object_get_stride(obj);
  11952. i915_gem_object_unlock(obj);
  11953. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11954. /*
  11955. * If there's a fence, enforce that
  11956. * the fb modifier and tiling mode match.
  11957. */
  11958. if (tiling != I915_TILING_NONE &&
  11959. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11960. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11961. goto err;
  11962. }
  11963. } else {
  11964. if (tiling == I915_TILING_X) {
  11965. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11966. } else if (tiling == I915_TILING_Y) {
  11967. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11968. goto err;
  11969. }
  11970. }
  11971. /* Passed in modifier sanity checking. */
  11972. switch (mode_cmd->modifier[0]) {
  11973. case I915_FORMAT_MOD_Y_TILED:
  11974. case I915_FORMAT_MOD_Yf_TILED:
  11975. if (INTEL_GEN(dev_priv) < 9) {
  11976. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11977. mode_cmd->modifier[0]);
  11978. goto err;
  11979. }
  11980. case DRM_FORMAT_MOD_NONE:
  11981. case I915_FORMAT_MOD_X_TILED:
  11982. break;
  11983. default:
  11984. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11985. mode_cmd->modifier[0]);
  11986. goto err;
  11987. }
  11988. /*
  11989. * gen2/3 display engine uses the fence if present,
  11990. * so the tiling mode must match the fb modifier exactly.
  11991. */
  11992. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11993. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11994. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11995. goto err;
  11996. }
  11997. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11998. mode_cmd->pixel_format);
  11999. if (mode_cmd->pitches[0] > pitch_limit) {
  12000. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  12001. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  12002. "tiled" : "linear",
  12003. mode_cmd->pitches[0], pitch_limit);
  12004. goto err;
  12005. }
  12006. /*
  12007. * If there's a fence, enforce that
  12008. * the fb pitch and fence stride match.
  12009. */
  12010. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  12011. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  12012. mode_cmd->pitches[0], stride);
  12013. goto err;
  12014. }
  12015. /* Reject formats not supported by any plane early. */
  12016. switch (mode_cmd->pixel_format) {
  12017. case DRM_FORMAT_C8:
  12018. case DRM_FORMAT_RGB565:
  12019. case DRM_FORMAT_XRGB8888:
  12020. case DRM_FORMAT_ARGB8888:
  12021. break;
  12022. case DRM_FORMAT_XRGB1555:
  12023. if (INTEL_GEN(dev_priv) > 3) {
  12024. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12025. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12026. goto err;
  12027. }
  12028. break;
  12029. case DRM_FORMAT_ABGR8888:
  12030. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12031. INTEL_GEN(dev_priv) < 9) {
  12032. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12033. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12034. goto err;
  12035. }
  12036. break;
  12037. case DRM_FORMAT_XBGR8888:
  12038. case DRM_FORMAT_XRGB2101010:
  12039. case DRM_FORMAT_XBGR2101010:
  12040. if (INTEL_GEN(dev_priv) < 4) {
  12041. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12042. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12043. goto err;
  12044. }
  12045. break;
  12046. case DRM_FORMAT_ABGR2101010:
  12047. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12048. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12049. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12050. goto err;
  12051. }
  12052. break;
  12053. case DRM_FORMAT_YUYV:
  12054. case DRM_FORMAT_UYVY:
  12055. case DRM_FORMAT_YVYU:
  12056. case DRM_FORMAT_VYUY:
  12057. if (INTEL_GEN(dev_priv) < 5) {
  12058. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12059. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12060. goto err;
  12061. }
  12062. break;
  12063. default:
  12064. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12065. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12066. goto err;
  12067. }
  12068. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12069. if (mode_cmd->offsets[0] != 0)
  12070. goto err;
  12071. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12072. &intel_fb->base, mode_cmd);
  12073. stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
  12074. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12075. DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
  12076. mode_cmd->pitches[0], stride_alignment);
  12077. goto err;
  12078. }
  12079. intel_fb->obj = obj;
  12080. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12081. if (ret)
  12082. goto err;
  12083. ret = drm_framebuffer_init(obj->base.dev,
  12084. &intel_fb->base,
  12085. &intel_fb_funcs);
  12086. if (ret) {
  12087. DRM_ERROR("framebuffer init failed %d\n", ret);
  12088. goto err;
  12089. }
  12090. return 0;
  12091. err:
  12092. i915_gem_object_lock(obj);
  12093. obj->framebuffer_references--;
  12094. i915_gem_object_unlock(obj);
  12095. return ret;
  12096. }
  12097. static struct drm_framebuffer *
  12098. intel_user_framebuffer_create(struct drm_device *dev,
  12099. struct drm_file *filp,
  12100. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12101. {
  12102. struct drm_framebuffer *fb;
  12103. struct drm_i915_gem_object *obj;
  12104. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12105. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12106. if (!obj)
  12107. return ERR_PTR(-ENOENT);
  12108. fb = intel_framebuffer_create(obj, &mode_cmd);
  12109. if (IS_ERR(fb))
  12110. i915_gem_object_put(obj);
  12111. return fb;
  12112. }
  12113. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12114. {
  12115. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12116. drm_atomic_state_default_release(state);
  12117. i915_sw_fence_fini(&intel_state->commit_ready);
  12118. kfree(state);
  12119. }
  12120. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12121. .fb_create = intel_user_framebuffer_create,
  12122. .output_poll_changed = intel_fbdev_output_poll_changed,
  12123. .atomic_check = intel_atomic_check,
  12124. .atomic_commit = intel_atomic_commit,
  12125. .atomic_state_alloc = intel_atomic_state_alloc,
  12126. .atomic_state_clear = intel_atomic_state_clear,
  12127. .atomic_state_free = intel_atomic_state_free,
  12128. };
  12129. /**
  12130. * intel_init_display_hooks - initialize the display modesetting hooks
  12131. * @dev_priv: device private
  12132. */
  12133. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12134. {
  12135. intel_init_cdclk_hooks(dev_priv);
  12136. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12137. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12138. dev_priv->display.get_initial_plane_config =
  12139. skylake_get_initial_plane_config;
  12140. dev_priv->display.crtc_compute_clock =
  12141. haswell_crtc_compute_clock;
  12142. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12143. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12144. } else if (HAS_DDI(dev_priv)) {
  12145. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12146. dev_priv->display.get_initial_plane_config =
  12147. ironlake_get_initial_plane_config;
  12148. dev_priv->display.crtc_compute_clock =
  12149. haswell_crtc_compute_clock;
  12150. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12151. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12152. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12153. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12154. dev_priv->display.get_initial_plane_config =
  12155. ironlake_get_initial_plane_config;
  12156. dev_priv->display.crtc_compute_clock =
  12157. ironlake_crtc_compute_clock;
  12158. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12159. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12160. } else if (IS_CHERRYVIEW(dev_priv)) {
  12161. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12162. dev_priv->display.get_initial_plane_config =
  12163. i9xx_get_initial_plane_config;
  12164. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12165. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12166. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12167. } else if (IS_VALLEYVIEW(dev_priv)) {
  12168. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12169. dev_priv->display.get_initial_plane_config =
  12170. i9xx_get_initial_plane_config;
  12171. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12172. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12173. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12174. } else if (IS_G4X(dev_priv)) {
  12175. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12176. dev_priv->display.get_initial_plane_config =
  12177. i9xx_get_initial_plane_config;
  12178. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12179. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12180. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12181. } else if (IS_PINEVIEW(dev_priv)) {
  12182. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12183. dev_priv->display.get_initial_plane_config =
  12184. i9xx_get_initial_plane_config;
  12185. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12186. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12187. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12188. } else if (!IS_GEN2(dev_priv)) {
  12189. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12190. dev_priv->display.get_initial_plane_config =
  12191. i9xx_get_initial_plane_config;
  12192. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12193. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12194. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12195. } else {
  12196. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12197. dev_priv->display.get_initial_plane_config =
  12198. i9xx_get_initial_plane_config;
  12199. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12200. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12201. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12202. }
  12203. if (IS_GEN5(dev_priv)) {
  12204. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12205. } else if (IS_GEN6(dev_priv)) {
  12206. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12207. } else if (IS_IVYBRIDGE(dev_priv)) {
  12208. /* FIXME: detect B0+ stepping and use auto training */
  12209. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12210. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12211. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12212. }
  12213. if (dev_priv->info.gen >= 9)
  12214. dev_priv->display.update_crtcs = skl_update_crtcs;
  12215. else
  12216. dev_priv->display.update_crtcs = intel_update_crtcs;
  12217. switch (INTEL_INFO(dev_priv)->gen) {
  12218. case 2:
  12219. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12220. break;
  12221. case 3:
  12222. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12223. break;
  12224. case 4:
  12225. case 5:
  12226. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12227. break;
  12228. case 6:
  12229. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12230. break;
  12231. case 7:
  12232. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12233. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12234. break;
  12235. case 9:
  12236. /* Drop through - unsupported since execlist only. */
  12237. default:
  12238. /* Default just returns -ENODEV to indicate unsupported */
  12239. dev_priv->display.queue_flip = intel_default_queue_flip;
  12240. }
  12241. }
  12242. /*
  12243. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12244. * resume, or other times. This quirk makes sure that's the case for
  12245. * affected systems.
  12246. */
  12247. static void quirk_pipea_force(struct drm_device *dev)
  12248. {
  12249. struct drm_i915_private *dev_priv = to_i915(dev);
  12250. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12251. DRM_INFO("applying pipe a force quirk\n");
  12252. }
  12253. static void quirk_pipeb_force(struct drm_device *dev)
  12254. {
  12255. struct drm_i915_private *dev_priv = to_i915(dev);
  12256. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12257. DRM_INFO("applying pipe b force quirk\n");
  12258. }
  12259. /*
  12260. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12261. */
  12262. static void quirk_ssc_force_disable(struct drm_device *dev)
  12263. {
  12264. struct drm_i915_private *dev_priv = to_i915(dev);
  12265. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12266. DRM_INFO("applying lvds SSC disable quirk\n");
  12267. }
  12268. /*
  12269. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12270. * brightness value
  12271. */
  12272. static void quirk_invert_brightness(struct drm_device *dev)
  12273. {
  12274. struct drm_i915_private *dev_priv = to_i915(dev);
  12275. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12276. DRM_INFO("applying inverted panel brightness quirk\n");
  12277. }
  12278. /* Some VBT's incorrectly indicate no backlight is present */
  12279. static void quirk_backlight_present(struct drm_device *dev)
  12280. {
  12281. struct drm_i915_private *dev_priv = to_i915(dev);
  12282. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12283. DRM_INFO("applying backlight present quirk\n");
  12284. }
  12285. struct intel_quirk {
  12286. int device;
  12287. int subsystem_vendor;
  12288. int subsystem_device;
  12289. void (*hook)(struct drm_device *dev);
  12290. };
  12291. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12292. struct intel_dmi_quirk {
  12293. void (*hook)(struct drm_device *dev);
  12294. const struct dmi_system_id (*dmi_id_list)[];
  12295. };
  12296. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12297. {
  12298. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12299. return 1;
  12300. }
  12301. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12302. {
  12303. .dmi_id_list = &(const struct dmi_system_id[]) {
  12304. {
  12305. .callback = intel_dmi_reverse_brightness,
  12306. .ident = "NCR Corporation",
  12307. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12308. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12309. },
  12310. },
  12311. { } /* terminating entry */
  12312. },
  12313. .hook = quirk_invert_brightness,
  12314. },
  12315. };
  12316. static struct intel_quirk intel_quirks[] = {
  12317. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12318. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12319. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12320. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12321. /* 830 needs to leave pipe A & dpll A up */
  12322. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12323. /* 830 needs to leave pipe B & dpll B up */
  12324. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12325. /* Lenovo U160 cannot use SSC on LVDS */
  12326. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12327. /* Sony Vaio Y cannot use SSC on LVDS */
  12328. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12329. /* Acer Aspire 5734Z must invert backlight brightness */
  12330. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12331. /* Acer/eMachines G725 */
  12332. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12333. /* Acer/eMachines e725 */
  12334. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12335. /* Acer/Packard Bell NCL20 */
  12336. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12337. /* Acer Aspire 4736Z */
  12338. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12339. /* Acer Aspire 5336 */
  12340. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12341. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12342. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12343. /* Acer C720 Chromebook (Core i3 4005U) */
  12344. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12345. /* Apple Macbook 2,1 (Core 2 T7400) */
  12346. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12347. /* Apple Macbook 4,1 */
  12348. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12349. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12350. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12351. /* HP Chromebook 14 (Celeron 2955U) */
  12352. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12353. /* Dell Chromebook 11 */
  12354. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12355. /* Dell Chromebook 11 (2015 version) */
  12356. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12357. };
  12358. static void intel_init_quirks(struct drm_device *dev)
  12359. {
  12360. struct pci_dev *d = dev->pdev;
  12361. int i;
  12362. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12363. struct intel_quirk *q = &intel_quirks[i];
  12364. if (d->device == q->device &&
  12365. (d->subsystem_vendor == q->subsystem_vendor ||
  12366. q->subsystem_vendor == PCI_ANY_ID) &&
  12367. (d->subsystem_device == q->subsystem_device ||
  12368. q->subsystem_device == PCI_ANY_ID))
  12369. q->hook(dev);
  12370. }
  12371. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12372. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12373. intel_dmi_quirks[i].hook(dev);
  12374. }
  12375. }
  12376. /* Disable the VGA plane that we never use */
  12377. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12378. {
  12379. struct pci_dev *pdev = dev_priv->drm.pdev;
  12380. u8 sr1;
  12381. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12382. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12383. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12384. outb(SR01, VGA_SR_INDEX);
  12385. sr1 = inb(VGA_SR_DATA);
  12386. outb(sr1 | 1<<5, VGA_SR_DATA);
  12387. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12388. udelay(300);
  12389. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12390. POSTING_READ(vga_reg);
  12391. }
  12392. void intel_modeset_init_hw(struct drm_device *dev)
  12393. {
  12394. struct drm_i915_private *dev_priv = to_i915(dev);
  12395. intel_update_cdclk(dev_priv);
  12396. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12397. intel_init_clock_gating(dev_priv);
  12398. }
  12399. /*
  12400. * Calculate what we think the watermarks should be for the state we've read
  12401. * out of the hardware and then immediately program those watermarks so that
  12402. * we ensure the hardware settings match our internal state.
  12403. *
  12404. * We can calculate what we think WM's should be by creating a duplicate of the
  12405. * current state (which was constructed during hardware readout) and running it
  12406. * through the atomic check code to calculate new watermark values in the
  12407. * state object.
  12408. */
  12409. static void sanitize_watermarks(struct drm_device *dev)
  12410. {
  12411. struct drm_i915_private *dev_priv = to_i915(dev);
  12412. struct drm_atomic_state *state;
  12413. struct intel_atomic_state *intel_state;
  12414. struct drm_crtc *crtc;
  12415. struct drm_crtc_state *cstate;
  12416. struct drm_modeset_acquire_ctx ctx;
  12417. int ret;
  12418. int i;
  12419. /* Only supported on platforms that use atomic watermark design */
  12420. if (!dev_priv->display.optimize_watermarks)
  12421. return;
  12422. /*
  12423. * We need to hold connection_mutex before calling duplicate_state so
  12424. * that the connector loop is protected.
  12425. */
  12426. drm_modeset_acquire_init(&ctx, 0);
  12427. retry:
  12428. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12429. if (ret == -EDEADLK) {
  12430. drm_modeset_backoff(&ctx);
  12431. goto retry;
  12432. } else if (WARN_ON(ret)) {
  12433. goto fail;
  12434. }
  12435. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12436. if (WARN_ON(IS_ERR(state)))
  12437. goto fail;
  12438. intel_state = to_intel_atomic_state(state);
  12439. /*
  12440. * Hardware readout is the only time we don't want to calculate
  12441. * intermediate watermarks (since we don't trust the current
  12442. * watermarks).
  12443. */
  12444. if (!HAS_GMCH_DISPLAY(dev_priv))
  12445. intel_state->skip_intermediate_wm = true;
  12446. ret = intel_atomic_check(dev, state);
  12447. if (ret) {
  12448. /*
  12449. * If we fail here, it means that the hardware appears to be
  12450. * programmed in a way that shouldn't be possible, given our
  12451. * understanding of watermark requirements. This might mean a
  12452. * mistake in the hardware readout code or a mistake in the
  12453. * watermark calculations for a given platform. Raise a WARN
  12454. * so that this is noticeable.
  12455. *
  12456. * If this actually happens, we'll have to just leave the
  12457. * BIOS-programmed watermarks untouched and hope for the best.
  12458. */
  12459. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12460. goto put_state;
  12461. }
  12462. /* Write calculated watermark values back */
  12463. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12464. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12465. cs->wm.need_postvbl_update = true;
  12466. dev_priv->display.optimize_watermarks(intel_state, cs);
  12467. }
  12468. put_state:
  12469. drm_atomic_state_put(state);
  12470. fail:
  12471. drm_modeset_drop_locks(&ctx);
  12472. drm_modeset_acquire_fini(&ctx);
  12473. }
  12474. int intel_modeset_init(struct drm_device *dev)
  12475. {
  12476. struct drm_i915_private *dev_priv = to_i915(dev);
  12477. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12478. enum pipe pipe;
  12479. struct intel_crtc *crtc;
  12480. drm_mode_config_init(dev);
  12481. dev->mode_config.min_width = 0;
  12482. dev->mode_config.min_height = 0;
  12483. dev->mode_config.preferred_depth = 24;
  12484. dev->mode_config.prefer_shadow = 1;
  12485. dev->mode_config.allow_fb_modifiers = true;
  12486. dev->mode_config.funcs = &intel_mode_funcs;
  12487. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12488. intel_atomic_helper_free_state_worker);
  12489. intel_init_quirks(dev);
  12490. intel_init_pm(dev_priv);
  12491. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12492. return 0;
  12493. /*
  12494. * There may be no VBT; and if the BIOS enabled SSC we can
  12495. * just keep using it to avoid unnecessary flicker. Whereas if the
  12496. * BIOS isn't using it, don't assume it will work even if the VBT
  12497. * indicates as much.
  12498. */
  12499. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12500. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12501. DREF_SSC1_ENABLE);
  12502. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12503. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12504. bios_lvds_use_ssc ? "en" : "dis",
  12505. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12506. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12507. }
  12508. }
  12509. if (IS_GEN2(dev_priv)) {
  12510. dev->mode_config.max_width = 2048;
  12511. dev->mode_config.max_height = 2048;
  12512. } else if (IS_GEN3(dev_priv)) {
  12513. dev->mode_config.max_width = 4096;
  12514. dev->mode_config.max_height = 4096;
  12515. } else {
  12516. dev->mode_config.max_width = 8192;
  12517. dev->mode_config.max_height = 8192;
  12518. }
  12519. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12520. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12521. dev->mode_config.cursor_height = 1023;
  12522. } else if (IS_GEN2(dev_priv)) {
  12523. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12524. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12525. } else {
  12526. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12527. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12528. }
  12529. dev->mode_config.fb_base = ggtt->mappable_base;
  12530. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12531. INTEL_INFO(dev_priv)->num_pipes,
  12532. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12533. for_each_pipe(dev_priv, pipe) {
  12534. int ret;
  12535. ret = intel_crtc_init(dev_priv, pipe);
  12536. if (ret) {
  12537. drm_mode_config_cleanup(dev);
  12538. return ret;
  12539. }
  12540. }
  12541. intel_shared_dpll_init(dev);
  12542. intel_update_czclk(dev_priv);
  12543. intel_modeset_init_hw(dev);
  12544. if (dev_priv->max_cdclk_freq == 0)
  12545. intel_update_max_cdclk(dev_priv);
  12546. /* Just disable it once at startup */
  12547. i915_disable_vga(dev_priv);
  12548. intel_setup_outputs(dev_priv);
  12549. drm_modeset_lock_all(dev);
  12550. intel_modeset_setup_hw_state(dev);
  12551. drm_modeset_unlock_all(dev);
  12552. for_each_intel_crtc(dev, crtc) {
  12553. struct intel_initial_plane_config plane_config = {};
  12554. if (!crtc->active)
  12555. continue;
  12556. /*
  12557. * Note that reserving the BIOS fb up front prevents us
  12558. * from stuffing other stolen allocations like the ring
  12559. * on top. This prevents some ugliness at boot time, and
  12560. * can even allow for smooth boot transitions if the BIOS
  12561. * fb is large enough for the active pipe configuration.
  12562. */
  12563. dev_priv->display.get_initial_plane_config(crtc,
  12564. &plane_config);
  12565. /*
  12566. * If the fb is shared between multiple heads, we'll
  12567. * just get the first one.
  12568. */
  12569. intel_find_initial_plane_obj(crtc, &plane_config);
  12570. }
  12571. /*
  12572. * Make sure hardware watermarks really match the state we read out.
  12573. * Note that we need to do this after reconstructing the BIOS fb's
  12574. * since the watermark calculation done here will use pstate->fb.
  12575. */
  12576. if (!HAS_GMCH_DISPLAY(dev_priv))
  12577. sanitize_watermarks(dev);
  12578. return 0;
  12579. }
  12580. static void intel_enable_pipe_a(struct drm_device *dev)
  12581. {
  12582. struct intel_connector *connector;
  12583. struct drm_connector_list_iter conn_iter;
  12584. struct drm_connector *crt = NULL;
  12585. struct intel_load_detect_pipe load_detect_temp;
  12586. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12587. /* We can't just switch on the pipe A, we need to set things up with a
  12588. * proper mode and output configuration. As a gross hack, enable pipe A
  12589. * by enabling the load detect pipe once. */
  12590. drm_connector_list_iter_begin(dev, &conn_iter);
  12591. for_each_intel_connector_iter(connector, &conn_iter) {
  12592. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12593. crt = &connector->base;
  12594. break;
  12595. }
  12596. }
  12597. drm_connector_list_iter_end(&conn_iter);
  12598. if (!crt)
  12599. return;
  12600. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12601. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12602. }
  12603. static bool
  12604. intel_check_plane_mapping(struct intel_crtc *crtc)
  12605. {
  12606. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12607. u32 val;
  12608. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12609. return true;
  12610. val = I915_READ(DSPCNTR(!crtc->plane));
  12611. if ((val & DISPLAY_PLANE_ENABLE) &&
  12612. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12613. return false;
  12614. return true;
  12615. }
  12616. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12617. {
  12618. struct drm_device *dev = crtc->base.dev;
  12619. struct intel_encoder *encoder;
  12620. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12621. return true;
  12622. return false;
  12623. }
  12624. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12625. {
  12626. struct drm_device *dev = encoder->base.dev;
  12627. struct intel_connector *connector;
  12628. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12629. return connector;
  12630. return NULL;
  12631. }
  12632. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12633. enum transcoder pch_transcoder)
  12634. {
  12635. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12636. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12637. }
  12638. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12639. {
  12640. struct drm_device *dev = crtc->base.dev;
  12641. struct drm_i915_private *dev_priv = to_i915(dev);
  12642. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12643. /* Clear any frame start delays used for debugging left by the BIOS */
  12644. if (!transcoder_is_dsi(cpu_transcoder)) {
  12645. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12646. I915_WRITE(reg,
  12647. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12648. }
  12649. /* restore vblank interrupts to correct state */
  12650. drm_crtc_vblank_reset(&crtc->base);
  12651. if (crtc->active) {
  12652. struct intel_plane *plane;
  12653. drm_crtc_vblank_on(&crtc->base);
  12654. /* Disable everything but the primary plane */
  12655. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12656. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12657. continue;
  12658. trace_intel_disable_plane(&plane->base, crtc);
  12659. plane->disable_plane(&plane->base, &crtc->base);
  12660. }
  12661. }
  12662. /* We need to sanitize the plane -> pipe mapping first because this will
  12663. * disable the crtc (and hence change the state) if it is wrong. Note
  12664. * that gen4+ has a fixed plane -> pipe mapping. */
  12665. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12666. bool plane;
  12667. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12668. crtc->base.base.id, crtc->base.name);
  12669. /* Pipe has the wrong plane attached and the plane is active.
  12670. * Temporarily change the plane mapping and disable everything
  12671. * ... */
  12672. plane = crtc->plane;
  12673. crtc->base.primary->state->visible = true;
  12674. crtc->plane = !plane;
  12675. intel_crtc_disable_noatomic(&crtc->base);
  12676. crtc->plane = plane;
  12677. }
  12678. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12679. crtc->pipe == PIPE_A && !crtc->active) {
  12680. /* BIOS forgot to enable pipe A, this mostly happens after
  12681. * resume. Force-enable the pipe to fix this, the update_dpms
  12682. * call below we restore the pipe to the right state, but leave
  12683. * the required bits on. */
  12684. intel_enable_pipe_a(dev);
  12685. }
  12686. /* Adjust the state of the output pipe according to whether we
  12687. * have active connectors/encoders. */
  12688. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12689. intel_crtc_disable_noatomic(&crtc->base);
  12690. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12691. /*
  12692. * We start out with underrun reporting disabled to avoid races.
  12693. * For correct bookkeeping mark this on active crtcs.
  12694. *
  12695. * Also on gmch platforms we dont have any hardware bits to
  12696. * disable the underrun reporting. Which means we need to start
  12697. * out with underrun reporting disabled also on inactive pipes,
  12698. * since otherwise we'll complain about the garbage we read when
  12699. * e.g. coming up after runtime pm.
  12700. *
  12701. * No protection against concurrent access is required - at
  12702. * worst a fifo underrun happens which also sets this to false.
  12703. */
  12704. crtc->cpu_fifo_underrun_disabled = true;
  12705. /*
  12706. * We track the PCH trancoder underrun reporting state
  12707. * within the crtc. With crtc for pipe A housing the underrun
  12708. * reporting state for PCH transcoder A, crtc for pipe B housing
  12709. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12710. * and marking underrun reporting as disabled for the non-existing
  12711. * PCH transcoders B and C would prevent enabling the south
  12712. * error interrupt (see cpt_can_enable_serr_int()).
  12713. */
  12714. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12715. crtc->pch_fifo_underrun_disabled = true;
  12716. }
  12717. }
  12718. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12719. {
  12720. struct intel_connector *connector;
  12721. /* We need to check both for a crtc link (meaning that the
  12722. * encoder is active and trying to read from a pipe) and the
  12723. * pipe itself being active. */
  12724. bool has_active_crtc = encoder->base.crtc &&
  12725. to_intel_crtc(encoder->base.crtc)->active;
  12726. connector = intel_encoder_find_connector(encoder);
  12727. if (connector && !has_active_crtc) {
  12728. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12729. encoder->base.base.id,
  12730. encoder->base.name);
  12731. /* Connector is active, but has no active pipe. This is
  12732. * fallout from our resume register restoring. Disable
  12733. * the encoder manually again. */
  12734. if (encoder->base.crtc) {
  12735. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12736. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12737. encoder->base.base.id,
  12738. encoder->base.name);
  12739. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12740. if (encoder->post_disable)
  12741. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12742. }
  12743. encoder->base.crtc = NULL;
  12744. /* Inconsistent output/port/pipe state happens presumably due to
  12745. * a bug in one of the get_hw_state functions. Or someplace else
  12746. * in our code, like the register restore mess on resume. Clamp
  12747. * things to off as a safer default. */
  12748. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12749. connector->base.encoder = NULL;
  12750. }
  12751. /* Enabled encoders without active connectors will be fixed in
  12752. * the crtc fixup. */
  12753. }
  12754. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12755. {
  12756. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12757. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12758. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12759. i915_disable_vga(dev_priv);
  12760. }
  12761. }
  12762. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12763. {
  12764. /* This function can be called both from intel_modeset_setup_hw_state or
  12765. * at a very early point in our resume sequence, where the power well
  12766. * structures are not yet restored. Since this function is at a very
  12767. * paranoid "someone might have enabled VGA while we were not looking"
  12768. * level, just check if the power well is enabled instead of trying to
  12769. * follow the "don't touch the power well if we don't need it" policy
  12770. * the rest of the driver uses. */
  12771. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12772. return;
  12773. i915_redisable_vga_power_on(dev_priv);
  12774. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12775. }
  12776. static bool primary_get_hw_state(struct intel_plane *plane)
  12777. {
  12778. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12779. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12780. }
  12781. /* FIXME read out full plane state for all planes */
  12782. static void readout_plane_state(struct intel_crtc *crtc)
  12783. {
  12784. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12785. bool visible;
  12786. visible = crtc->active && primary_get_hw_state(primary);
  12787. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12788. to_intel_plane_state(primary->base.state),
  12789. visible);
  12790. }
  12791. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12792. {
  12793. struct drm_i915_private *dev_priv = to_i915(dev);
  12794. enum pipe pipe;
  12795. struct intel_crtc *crtc;
  12796. struct intel_encoder *encoder;
  12797. struct intel_connector *connector;
  12798. struct drm_connector_list_iter conn_iter;
  12799. int i;
  12800. dev_priv->active_crtcs = 0;
  12801. for_each_intel_crtc(dev, crtc) {
  12802. struct intel_crtc_state *crtc_state =
  12803. to_intel_crtc_state(crtc->base.state);
  12804. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12805. memset(crtc_state, 0, sizeof(*crtc_state));
  12806. crtc_state->base.crtc = &crtc->base;
  12807. crtc_state->base.active = crtc_state->base.enable =
  12808. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12809. crtc->base.enabled = crtc_state->base.enable;
  12810. crtc->active = crtc_state->base.active;
  12811. if (crtc_state->base.active)
  12812. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12813. readout_plane_state(crtc);
  12814. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12815. crtc->base.base.id, crtc->base.name,
  12816. enableddisabled(crtc_state->base.active));
  12817. }
  12818. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12819. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12820. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12821. &pll->state.hw_state);
  12822. pll->state.crtc_mask = 0;
  12823. for_each_intel_crtc(dev, crtc) {
  12824. struct intel_crtc_state *crtc_state =
  12825. to_intel_crtc_state(crtc->base.state);
  12826. if (crtc_state->base.active &&
  12827. crtc_state->shared_dpll == pll)
  12828. pll->state.crtc_mask |= 1 << crtc->pipe;
  12829. }
  12830. pll->active_mask = pll->state.crtc_mask;
  12831. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12832. pll->name, pll->state.crtc_mask, pll->on);
  12833. }
  12834. for_each_intel_encoder(dev, encoder) {
  12835. pipe = 0;
  12836. if (encoder->get_hw_state(encoder, &pipe)) {
  12837. struct intel_crtc_state *crtc_state;
  12838. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12839. crtc_state = to_intel_crtc_state(crtc->base.state);
  12840. encoder->base.crtc = &crtc->base;
  12841. crtc_state->output_types |= 1 << encoder->type;
  12842. encoder->get_config(encoder, crtc_state);
  12843. } else {
  12844. encoder->base.crtc = NULL;
  12845. }
  12846. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12847. encoder->base.base.id, encoder->base.name,
  12848. enableddisabled(encoder->base.crtc),
  12849. pipe_name(pipe));
  12850. }
  12851. drm_connector_list_iter_begin(dev, &conn_iter);
  12852. for_each_intel_connector_iter(connector, &conn_iter) {
  12853. if (connector->get_hw_state(connector)) {
  12854. connector->base.dpms = DRM_MODE_DPMS_ON;
  12855. encoder = connector->encoder;
  12856. connector->base.encoder = &encoder->base;
  12857. if (encoder->base.crtc &&
  12858. encoder->base.crtc->state->active) {
  12859. /*
  12860. * This has to be done during hardware readout
  12861. * because anything calling .crtc_disable may
  12862. * rely on the connector_mask being accurate.
  12863. */
  12864. encoder->base.crtc->state->connector_mask |=
  12865. 1 << drm_connector_index(&connector->base);
  12866. encoder->base.crtc->state->encoder_mask |=
  12867. 1 << drm_encoder_index(&encoder->base);
  12868. }
  12869. } else {
  12870. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12871. connector->base.encoder = NULL;
  12872. }
  12873. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12874. connector->base.base.id, connector->base.name,
  12875. enableddisabled(connector->base.encoder));
  12876. }
  12877. drm_connector_list_iter_end(&conn_iter);
  12878. for_each_intel_crtc(dev, crtc) {
  12879. struct intel_crtc_state *crtc_state =
  12880. to_intel_crtc_state(crtc->base.state);
  12881. int pixclk = 0;
  12882. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  12883. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12884. if (crtc_state->base.active) {
  12885. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12886. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12887. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12888. /*
  12889. * The initial mode needs to be set in order to keep
  12890. * the atomic core happy. It wants a valid mode if the
  12891. * crtc's enabled, so we do the above call.
  12892. *
  12893. * But we don't set all the derived state fully, hence
  12894. * set a flag to indicate that a full recalculation is
  12895. * needed on the next commit.
  12896. */
  12897. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12898. intel_crtc_compute_pixel_rate(crtc_state);
  12899. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12900. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12901. pixclk = crtc_state->pixel_rate;
  12902. else
  12903. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12904. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12905. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12906. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12907. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12908. update_scanline_offset(crtc);
  12909. }
  12910. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12911. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12912. }
  12913. }
  12914. static void
  12915. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12916. {
  12917. struct intel_encoder *encoder;
  12918. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12919. u64 get_domains;
  12920. enum intel_display_power_domain domain;
  12921. if (!encoder->get_power_domains)
  12922. continue;
  12923. get_domains = encoder->get_power_domains(encoder);
  12924. for_each_power_domain(domain, get_domains)
  12925. intel_display_power_get(dev_priv, domain);
  12926. }
  12927. }
  12928. /* Scan out the current hw modeset state,
  12929. * and sanitizes it to the current state
  12930. */
  12931. static void
  12932. intel_modeset_setup_hw_state(struct drm_device *dev)
  12933. {
  12934. struct drm_i915_private *dev_priv = to_i915(dev);
  12935. enum pipe pipe;
  12936. struct intel_crtc *crtc;
  12937. struct intel_encoder *encoder;
  12938. int i;
  12939. intel_modeset_readout_hw_state(dev);
  12940. /* HW state is read out, now we need to sanitize this mess. */
  12941. get_encoder_power_domains(dev_priv);
  12942. for_each_intel_encoder(dev, encoder) {
  12943. intel_sanitize_encoder(encoder);
  12944. }
  12945. for_each_pipe(dev_priv, pipe) {
  12946. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12947. intel_sanitize_crtc(crtc);
  12948. intel_dump_pipe_config(crtc, crtc->config,
  12949. "[setup_hw_state]");
  12950. }
  12951. intel_modeset_update_connector_atomic_state(dev);
  12952. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12953. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12954. if (!pll->on || pll->active_mask)
  12955. continue;
  12956. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12957. pll->funcs.disable(dev_priv, pll);
  12958. pll->on = false;
  12959. }
  12960. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12961. vlv_wm_get_hw_state(dev);
  12962. vlv_wm_sanitize(dev_priv);
  12963. } else if (IS_GEN9(dev_priv)) {
  12964. skl_wm_get_hw_state(dev);
  12965. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12966. ilk_wm_get_hw_state(dev);
  12967. }
  12968. for_each_intel_crtc(dev, crtc) {
  12969. u64 put_domains;
  12970. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12971. if (WARN_ON(put_domains))
  12972. modeset_put_power_domains(dev_priv, put_domains);
  12973. }
  12974. intel_display_set_init_power(dev_priv, false);
  12975. intel_power_domains_verify_state(dev_priv);
  12976. intel_fbc_init_pipe_state(dev_priv);
  12977. }
  12978. void intel_display_resume(struct drm_device *dev)
  12979. {
  12980. struct drm_i915_private *dev_priv = to_i915(dev);
  12981. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12982. struct drm_modeset_acquire_ctx ctx;
  12983. int ret;
  12984. dev_priv->modeset_restore_state = NULL;
  12985. if (state)
  12986. state->acquire_ctx = &ctx;
  12987. /*
  12988. * This is a cludge because with real atomic modeset mode_config.mutex
  12989. * won't be taken. Unfortunately some probed state like
  12990. * audio_codec_enable is still protected by mode_config.mutex, so lock
  12991. * it here for now.
  12992. */
  12993. mutex_lock(&dev->mode_config.mutex);
  12994. drm_modeset_acquire_init(&ctx, 0);
  12995. while (1) {
  12996. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12997. if (ret != -EDEADLK)
  12998. break;
  12999. drm_modeset_backoff(&ctx);
  13000. }
  13001. if (!ret)
  13002. ret = __intel_display_resume(dev, state, &ctx);
  13003. drm_modeset_drop_locks(&ctx);
  13004. drm_modeset_acquire_fini(&ctx);
  13005. mutex_unlock(&dev->mode_config.mutex);
  13006. if (ret)
  13007. DRM_ERROR("Restoring old state failed with %i\n", ret);
  13008. if (state)
  13009. drm_atomic_state_put(state);
  13010. }
  13011. void intel_modeset_gem_init(struct drm_device *dev)
  13012. {
  13013. struct drm_i915_private *dev_priv = to_i915(dev);
  13014. intel_init_gt_powersave(dev_priv);
  13015. intel_setup_overlay(dev_priv);
  13016. }
  13017. int intel_connector_register(struct drm_connector *connector)
  13018. {
  13019. struct intel_connector *intel_connector = to_intel_connector(connector);
  13020. int ret;
  13021. ret = intel_backlight_device_register(intel_connector);
  13022. if (ret)
  13023. goto err;
  13024. return 0;
  13025. err:
  13026. return ret;
  13027. }
  13028. void intel_connector_unregister(struct drm_connector *connector)
  13029. {
  13030. struct intel_connector *intel_connector = to_intel_connector(connector);
  13031. intel_backlight_device_unregister(intel_connector);
  13032. intel_panel_destroy_backlight(connector);
  13033. }
  13034. void intel_modeset_cleanup(struct drm_device *dev)
  13035. {
  13036. struct drm_i915_private *dev_priv = to_i915(dev);
  13037. flush_work(&dev_priv->atomic_helper.free_work);
  13038. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13039. intel_disable_gt_powersave(dev_priv);
  13040. /*
  13041. * Interrupts and polling as the first thing to avoid creating havoc.
  13042. * Too much stuff here (turning of connectors, ...) would
  13043. * experience fancy races otherwise.
  13044. */
  13045. intel_irq_uninstall(dev_priv);
  13046. /*
  13047. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13048. * poll handlers. Hence disable polling after hpd handling is shut down.
  13049. */
  13050. drm_kms_helper_poll_fini(dev);
  13051. intel_unregister_dsm_handler();
  13052. intel_fbc_global_disable(dev_priv);
  13053. /* flush any delayed tasks or pending work */
  13054. flush_scheduled_work();
  13055. drm_mode_config_cleanup(dev);
  13056. intel_cleanup_overlay(dev_priv);
  13057. intel_cleanup_gt_powersave(dev_priv);
  13058. intel_teardown_gmbus(dev_priv);
  13059. }
  13060. void intel_connector_attach_encoder(struct intel_connector *connector,
  13061. struct intel_encoder *encoder)
  13062. {
  13063. connector->encoder = encoder;
  13064. drm_mode_connector_attach_encoder(&connector->base,
  13065. &encoder->base);
  13066. }
  13067. /*
  13068. * set vga decode state - true == enable VGA decode
  13069. */
  13070. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13071. {
  13072. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13073. u16 gmch_ctrl;
  13074. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13075. DRM_ERROR("failed to read control word\n");
  13076. return -EIO;
  13077. }
  13078. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13079. return 0;
  13080. if (state)
  13081. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13082. else
  13083. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13084. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13085. DRM_ERROR("failed to write control word\n");
  13086. return -EIO;
  13087. }
  13088. return 0;
  13089. }
  13090. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13091. struct intel_display_error_state {
  13092. u32 power_well_driver;
  13093. int num_transcoders;
  13094. struct intel_cursor_error_state {
  13095. u32 control;
  13096. u32 position;
  13097. u32 base;
  13098. u32 size;
  13099. } cursor[I915_MAX_PIPES];
  13100. struct intel_pipe_error_state {
  13101. bool power_domain_on;
  13102. u32 source;
  13103. u32 stat;
  13104. } pipe[I915_MAX_PIPES];
  13105. struct intel_plane_error_state {
  13106. u32 control;
  13107. u32 stride;
  13108. u32 size;
  13109. u32 pos;
  13110. u32 addr;
  13111. u32 surface;
  13112. u32 tile_offset;
  13113. } plane[I915_MAX_PIPES];
  13114. struct intel_transcoder_error_state {
  13115. bool power_domain_on;
  13116. enum transcoder cpu_transcoder;
  13117. u32 conf;
  13118. u32 htotal;
  13119. u32 hblank;
  13120. u32 hsync;
  13121. u32 vtotal;
  13122. u32 vblank;
  13123. u32 vsync;
  13124. } transcoder[4];
  13125. };
  13126. struct intel_display_error_state *
  13127. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13128. {
  13129. struct intel_display_error_state *error;
  13130. int transcoders[] = {
  13131. TRANSCODER_A,
  13132. TRANSCODER_B,
  13133. TRANSCODER_C,
  13134. TRANSCODER_EDP,
  13135. };
  13136. int i;
  13137. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13138. return NULL;
  13139. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13140. if (error == NULL)
  13141. return NULL;
  13142. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13143. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13144. for_each_pipe(dev_priv, i) {
  13145. error->pipe[i].power_domain_on =
  13146. __intel_display_power_is_enabled(dev_priv,
  13147. POWER_DOMAIN_PIPE(i));
  13148. if (!error->pipe[i].power_domain_on)
  13149. continue;
  13150. error->cursor[i].control = I915_READ(CURCNTR(i));
  13151. error->cursor[i].position = I915_READ(CURPOS(i));
  13152. error->cursor[i].base = I915_READ(CURBASE(i));
  13153. error->plane[i].control = I915_READ(DSPCNTR(i));
  13154. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13155. if (INTEL_GEN(dev_priv) <= 3) {
  13156. error->plane[i].size = I915_READ(DSPSIZE(i));
  13157. error->plane[i].pos = I915_READ(DSPPOS(i));
  13158. }
  13159. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13160. error->plane[i].addr = I915_READ(DSPADDR(i));
  13161. if (INTEL_GEN(dev_priv) >= 4) {
  13162. error->plane[i].surface = I915_READ(DSPSURF(i));
  13163. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13164. }
  13165. error->pipe[i].source = I915_READ(PIPESRC(i));
  13166. if (HAS_GMCH_DISPLAY(dev_priv))
  13167. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13168. }
  13169. /* Note: this does not include DSI transcoders. */
  13170. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13171. if (HAS_DDI(dev_priv))
  13172. error->num_transcoders++; /* Account for eDP. */
  13173. for (i = 0; i < error->num_transcoders; i++) {
  13174. enum transcoder cpu_transcoder = transcoders[i];
  13175. error->transcoder[i].power_domain_on =
  13176. __intel_display_power_is_enabled(dev_priv,
  13177. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13178. if (!error->transcoder[i].power_domain_on)
  13179. continue;
  13180. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13181. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13182. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13183. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13184. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13185. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13186. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13187. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13188. }
  13189. return error;
  13190. }
  13191. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13192. void
  13193. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13194. struct intel_display_error_state *error)
  13195. {
  13196. struct drm_i915_private *dev_priv = m->i915;
  13197. int i;
  13198. if (!error)
  13199. return;
  13200. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13201. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13202. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13203. error->power_well_driver);
  13204. for_each_pipe(dev_priv, i) {
  13205. err_printf(m, "Pipe [%d]:\n", i);
  13206. err_printf(m, " Power: %s\n",
  13207. onoff(error->pipe[i].power_domain_on));
  13208. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13209. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13210. err_printf(m, "Plane [%d]:\n", i);
  13211. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13212. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13213. if (INTEL_GEN(dev_priv) <= 3) {
  13214. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13215. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13216. }
  13217. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13218. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13219. if (INTEL_GEN(dev_priv) >= 4) {
  13220. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13221. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13222. }
  13223. err_printf(m, "Cursor [%d]:\n", i);
  13224. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13225. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13226. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13227. }
  13228. for (i = 0; i < error->num_transcoders; i++) {
  13229. err_printf(m, "CPU transcoder: %s\n",
  13230. transcoder_name(error->transcoder[i].cpu_transcoder));
  13231. err_printf(m, " Power: %s\n",
  13232. onoff(error->transcoder[i].power_domain_on));
  13233. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13234. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13235. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13236. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13237. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13238. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13239. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13240. }
  13241. }
  13242. #endif