intel_display.c 446 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882
  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 || IS_G4X(dev_priv)) {
  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_LINEAR:
  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_LINEAR)
  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_LINEAR:
  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_LINEAR) {
  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_LINEAR) {
  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_LINEAR) {
  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(intel_plane, intel_crtc);
  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_LINEAR:
  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 u32 i9xx_plane_ctl(const struct intel_crtc_state *crtc_state,
  2548. const struct intel_plane_state *plane_state)
  2549. {
  2550. struct drm_i915_private *dev_priv =
  2551. to_i915(plane_state->base.plane->dev);
  2552. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2553. const struct drm_framebuffer *fb = plane_state->base.fb;
  2554. unsigned int rotation = plane_state->base.rotation;
  2555. u32 dspcntr;
  2556. dspcntr = DISPLAY_PLANE_ENABLE | DISPPLANE_GAMMA_ENABLE;
  2557. if (IS_G4X(dev_priv) || IS_GEN5(dev_priv) ||
  2558. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  2559. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2560. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2561. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2562. if (INTEL_GEN(dev_priv) < 4)
  2563. dspcntr |= DISPPLANE_SEL_PIPE(crtc->pipe);
  2564. switch (fb->format->format) {
  2565. case DRM_FORMAT_C8:
  2566. dspcntr |= DISPPLANE_8BPP;
  2567. break;
  2568. case DRM_FORMAT_XRGB1555:
  2569. dspcntr |= DISPPLANE_BGRX555;
  2570. break;
  2571. case DRM_FORMAT_RGB565:
  2572. dspcntr |= DISPPLANE_BGRX565;
  2573. break;
  2574. case DRM_FORMAT_XRGB8888:
  2575. dspcntr |= DISPPLANE_BGRX888;
  2576. break;
  2577. case DRM_FORMAT_XBGR8888:
  2578. dspcntr |= DISPPLANE_RGBX888;
  2579. break;
  2580. case DRM_FORMAT_XRGB2101010:
  2581. dspcntr |= DISPPLANE_BGRX101010;
  2582. break;
  2583. case DRM_FORMAT_XBGR2101010:
  2584. dspcntr |= DISPPLANE_RGBX101010;
  2585. break;
  2586. default:
  2587. MISSING_CASE(fb->format->format);
  2588. return 0;
  2589. }
  2590. if (INTEL_GEN(dev_priv) >= 4 &&
  2591. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2592. dspcntr |= DISPPLANE_TILED;
  2593. if (rotation & DRM_ROTATE_180)
  2594. dspcntr |= DISPPLANE_ROTATE_180;
  2595. if (rotation & DRM_REFLECT_X)
  2596. dspcntr |= DISPPLANE_MIRROR;
  2597. return dspcntr;
  2598. }
  2599. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2600. {
  2601. struct drm_i915_private *dev_priv =
  2602. to_i915(plane_state->base.plane->dev);
  2603. int src_x = plane_state->base.src.x1 >> 16;
  2604. int src_y = plane_state->base.src.y1 >> 16;
  2605. u32 offset;
  2606. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2607. if (INTEL_GEN(dev_priv) >= 4)
  2608. offset = intel_compute_tile_offset(&src_x, &src_y,
  2609. plane_state, 0);
  2610. else
  2611. offset = 0;
  2612. /* HSW/BDW do this automagically in hardware */
  2613. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2614. unsigned int rotation = plane_state->base.rotation;
  2615. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2616. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2617. if (rotation & DRM_ROTATE_180) {
  2618. src_x += src_w - 1;
  2619. src_y += src_h - 1;
  2620. } else if (rotation & DRM_REFLECT_X) {
  2621. src_x += src_w - 1;
  2622. }
  2623. }
  2624. plane_state->main.offset = offset;
  2625. plane_state->main.x = src_x;
  2626. plane_state->main.y = src_y;
  2627. return 0;
  2628. }
  2629. static void i9xx_update_primary_plane(struct intel_plane *primary,
  2630. const struct intel_crtc_state *crtc_state,
  2631. const struct intel_plane_state *plane_state)
  2632. {
  2633. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2634. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2635. const struct drm_framebuffer *fb = plane_state->base.fb;
  2636. enum plane plane = primary->plane;
  2637. u32 linear_offset;
  2638. u32 dspcntr = plane_state->ctl;
  2639. i915_reg_t reg = DSPCNTR(plane);
  2640. int x = plane_state->main.x;
  2641. int y = plane_state->main.y;
  2642. unsigned long irqflags;
  2643. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2644. if (INTEL_GEN(dev_priv) >= 4)
  2645. crtc->dspaddr_offset = plane_state->main.offset;
  2646. else
  2647. crtc->dspaddr_offset = linear_offset;
  2648. crtc->adjusted_x = x;
  2649. crtc->adjusted_y = y;
  2650. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2651. if (INTEL_GEN(dev_priv) < 4) {
  2652. /* pipesrc and dspsize control the size that is scaled from,
  2653. * which should always be the user's requested size.
  2654. */
  2655. I915_WRITE_FW(DSPSIZE(plane),
  2656. ((crtc_state->pipe_src_h - 1) << 16) |
  2657. (crtc_state->pipe_src_w - 1));
  2658. I915_WRITE_FW(DSPPOS(plane), 0);
  2659. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2660. I915_WRITE_FW(PRIMSIZE(plane),
  2661. ((crtc_state->pipe_src_h - 1) << 16) |
  2662. (crtc_state->pipe_src_w - 1));
  2663. I915_WRITE_FW(PRIMPOS(plane), 0);
  2664. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2665. }
  2666. I915_WRITE_FW(reg, dspcntr);
  2667. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2668. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2669. I915_WRITE_FW(DSPSURF(plane),
  2670. intel_plane_ggtt_offset(plane_state) +
  2671. crtc->dspaddr_offset);
  2672. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2673. } else if (INTEL_GEN(dev_priv) >= 4) {
  2674. I915_WRITE_FW(DSPSURF(plane),
  2675. intel_plane_ggtt_offset(plane_state) +
  2676. crtc->dspaddr_offset);
  2677. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2678. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2679. } else {
  2680. I915_WRITE_FW(DSPADDR(plane),
  2681. intel_plane_ggtt_offset(plane_state) +
  2682. crtc->dspaddr_offset);
  2683. }
  2684. POSTING_READ_FW(reg);
  2685. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2686. }
  2687. static void i9xx_disable_primary_plane(struct intel_plane *primary,
  2688. struct intel_crtc *crtc)
  2689. {
  2690. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2691. enum plane plane = primary->plane;
  2692. unsigned long irqflags;
  2693. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2694. I915_WRITE_FW(DSPCNTR(plane), 0);
  2695. if (INTEL_INFO(dev_priv)->gen >= 4)
  2696. I915_WRITE_FW(DSPSURF(plane), 0);
  2697. else
  2698. I915_WRITE_FW(DSPADDR(plane), 0);
  2699. POSTING_READ_FW(DSPCNTR(plane));
  2700. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2701. }
  2702. static u32
  2703. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2704. {
  2705. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2706. return 64;
  2707. else
  2708. return intel_tile_width_bytes(fb, plane);
  2709. }
  2710. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2711. {
  2712. struct drm_device *dev = intel_crtc->base.dev;
  2713. struct drm_i915_private *dev_priv = to_i915(dev);
  2714. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2715. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2716. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2717. }
  2718. /*
  2719. * This function detaches (aka. unbinds) unused scalers in hardware
  2720. */
  2721. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2722. {
  2723. struct intel_crtc_scaler_state *scaler_state;
  2724. int i;
  2725. scaler_state = &intel_crtc->config->scaler_state;
  2726. /* loop through and disable scalers that aren't in use */
  2727. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2728. if (!scaler_state->scalers[i].in_use)
  2729. skl_detach_scaler(intel_crtc, i);
  2730. }
  2731. }
  2732. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2733. unsigned int rotation)
  2734. {
  2735. u32 stride;
  2736. if (plane >= fb->format->num_planes)
  2737. return 0;
  2738. stride = intel_fb_pitch(fb, plane, rotation);
  2739. /*
  2740. * The stride is either expressed as a multiple of 64 bytes chunks for
  2741. * linear buffers or in number of tiles for tiled buffers.
  2742. */
  2743. if (drm_rotation_90_or_270(rotation))
  2744. stride /= intel_tile_height(fb, plane);
  2745. else
  2746. stride /= intel_fb_stride_alignment(fb, plane);
  2747. return stride;
  2748. }
  2749. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2750. {
  2751. switch (pixel_format) {
  2752. case DRM_FORMAT_C8:
  2753. return PLANE_CTL_FORMAT_INDEXED;
  2754. case DRM_FORMAT_RGB565:
  2755. return PLANE_CTL_FORMAT_RGB_565;
  2756. case DRM_FORMAT_XBGR8888:
  2757. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2758. case DRM_FORMAT_XRGB8888:
  2759. return PLANE_CTL_FORMAT_XRGB_8888;
  2760. /*
  2761. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2762. * to be already pre-multiplied. We need to add a knob (or a different
  2763. * DRM_FORMAT) for user-space to configure that.
  2764. */
  2765. case DRM_FORMAT_ABGR8888:
  2766. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2767. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2768. case DRM_FORMAT_ARGB8888:
  2769. return PLANE_CTL_FORMAT_XRGB_8888 |
  2770. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2771. case DRM_FORMAT_XRGB2101010:
  2772. return PLANE_CTL_FORMAT_XRGB_2101010;
  2773. case DRM_FORMAT_XBGR2101010:
  2774. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2775. case DRM_FORMAT_YUYV:
  2776. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2777. case DRM_FORMAT_YVYU:
  2778. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2779. case DRM_FORMAT_UYVY:
  2780. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2781. case DRM_FORMAT_VYUY:
  2782. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2783. default:
  2784. MISSING_CASE(pixel_format);
  2785. }
  2786. return 0;
  2787. }
  2788. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2789. {
  2790. switch (fb_modifier) {
  2791. case DRM_FORMAT_MOD_LINEAR:
  2792. break;
  2793. case I915_FORMAT_MOD_X_TILED:
  2794. return PLANE_CTL_TILED_X;
  2795. case I915_FORMAT_MOD_Y_TILED:
  2796. return PLANE_CTL_TILED_Y;
  2797. case I915_FORMAT_MOD_Yf_TILED:
  2798. return PLANE_CTL_TILED_YF;
  2799. default:
  2800. MISSING_CASE(fb_modifier);
  2801. }
  2802. return 0;
  2803. }
  2804. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  2805. {
  2806. switch (rotation) {
  2807. case DRM_ROTATE_0:
  2808. break;
  2809. /*
  2810. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2811. * while i915 HW rotation is clockwise, thats why this swapping.
  2812. */
  2813. case DRM_ROTATE_90:
  2814. return PLANE_CTL_ROTATE_270;
  2815. case DRM_ROTATE_180:
  2816. return PLANE_CTL_ROTATE_180;
  2817. case DRM_ROTATE_270:
  2818. return PLANE_CTL_ROTATE_90;
  2819. default:
  2820. MISSING_CASE(rotation);
  2821. }
  2822. return 0;
  2823. }
  2824. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  2825. const struct intel_plane_state *plane_state)
  2826. {
  2827. struct drm_i915_private *dev_priv =
  2828. to_i915(plane_state->base.plane->dev);
  2829. const struct drm_framebuffer *fb = plane_state->base.fb;
  2830. unsigned int rotation = plane_state->base.rotation;
  2831. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  2832. u32 plane_ctl;
  2833. plane_ctl = PLANE_CTL_ENABLE;
  2834. if (!IS_GEMINILAKE(dev_priv)) {
  2835. plane_ctl |=
  2836. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2837. PLANE_CTL_PIPE_CSC_ENABLE |
  2838. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2839. }
  2840. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2841. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2842. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2843. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  2844. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  2845. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  2846. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  2847. return plane_ctl;
  2848. }
  2849. static void skylake_update_primary_plane(struct intel_plane *plane,
  2850. const struct intel_crtc_state *crtc_state,
  2851. const struct intel_plane_state *plane_state)
  2852. {
  2853. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  2854. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  2855. const struct drm_framebuffer *fb = plane_state->base.fb;
  2856. enum plane_id plane_id = plane->id;
  2857. enum pipe pipe = plane->pipe;
  2858. u32 plane_ctl = plane_state->ctl;
  2859. unsigned int rotation = plane_state->base.rotation;
  2860. u32 stride = skl_plane_stride(fb, 0, rotation);
  2861. u32 surf_addr = plane_state->main.offset;
  2862. int scaler_id = plane_state->scaler_id;
  2863. int src_x = plane_state->main.x;
  2864. int src_y = plane_state->main.y;
  2865. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2866. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2867. int dst_x = plane_state->base.dst.x1;
  2868. int dst_y = plane_state->base.dst.y1;
  2869. int dst_w = drm_rect_width(&plane_state->base.dst);
  2870. int dst_h = drm_rect_height(&plane_state->base.dst);
  2871. unsigned long irqflags;
  2872. /* Sizes are 0 based */
  2873. src_w--;
  2874. src_h--;
  2875. dst_w--;
  2876. dst_h--;
  2877. crtc->dspaddr_offset = surf_addr;
  2878. crtc->adjusted_x = src_x;
  2879. crtc->adjusted_y = src_y;
  2880. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2881. if (IS_GEMINILAKE(dev_priv)) {
  2882. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  2883. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2884. PLANE_COLOR_PIPE_CSC_ENABLE |
  2885. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2886. }
  2887. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  2888. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2889. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  2890. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2891. if (scaler_id >= 0) {
  2892. uint32_t ps_ctrl = 0;
  2893. WARN_ON(!dst_w || !dst_h);
  2894. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2895. crtc_state->scaler_state.scalers[scaler_id].mode;
  2896. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2897. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2898. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2899. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2900. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  2901. } else {
  2902. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2903. }
  2904. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  2905. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2906. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2907. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2908. }
  2909. static void skylake_disable_primary_plane(struct intel_plane *primary,
  2910. struct intel_crtc *crtc)
  2911. {
  2912. struct drm_i915_private *dev_priv = to_i915(primary->base.dev);
  2913. enum plane_id plane_id = primary->id;
  2914. enum pipe pipe = primary->pipe;
  2915. unsigned long irqflags;
  2916. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2917. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  2918. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  2919. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2920. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2921. }
  2922. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2923. {
  2924. struct intel_crtc *crtc;
  2925. for_each_intel_crtc(&dev_priv->drm, crtc)
  2926. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2927. }
  2928. static void intel_update_primary_planes(struct drm_device *dev)
  2929. {
  2930. struct drm_crtc *crtc;
  2931. for_each_crtc(dev, crtc) {
  2932. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2933. struct intel_plane_state *plane_state =
  2934. to_intel_plane_state(plane->base.state);
  2935. if (plane_state->base.visible) {
  2936. trace_intel_update_plane(&plane->base,
  2937. to_intel_crtc(crtc));
  2938. plane->update_plane(plane,
  2939. to_intel_crtc_state(crtc->state),
  2940. plane_state);
  2941. }
  2942. }
  2943. }
  2944. static int
  2945. __intel_display_resume(struct drm_device *dev,
  2946. struct drm_atomic_state *state,
  2947. struct drm_modeset_acquire_ctx *ctx)
  2948. {
  2949. struct drm_crtc_state *crtc_state;
  2950. struct drm_crtc *crtc;
  2951. int i, ret;
  2952. intel_modeset_setup_hw_state(dev);
  2953. i915_redisable_vga(to_i915(dev));
  2954. if (!state)
  2955. return 0;
  2956. /*
  2957. * We've duplicated the state, pointers to the old state are invalid.
  2958. *
  2959. * Don't attempt to use the old state until we commit the duplicated state.
  2960. */
  2961. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  2962. /*
  2963. * Force recalculation even if we restore
  2964. * current state. With fast modeset this may not result
  2965. * in a modeset when the state is compatible.
  2966. */
  2967. crtc_state->mode_changed = true;
  2968. }
  2969. /* ignore any reset values/BIOS leftovers in the WM registers */
  2970. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  2971. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2972. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  2973. WARN_ON(ret == -EDEADLK);
  2974. return ret;
  2975. }
  2976. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  2977. {
  2978. return intel_has_gpu_reset(dev_priv) &&
  2979. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  2980. }
  2981. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2982. {
  2983. struct drm_device *dev = &dev_priv->drm;
  2984. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2985. struct drm_atomic_state *state;
  2986. int ret;
  2987. /*
  2988. * Need mode_config.mutex so that we don't
  2989. * trample ongoing ->detect() and whatnot.
  2990. */
  2991. mutex_lock(&dev->mode_config.mutex);
  2992. drm_modeset_acquire_init(ctx, 0);
  2993. while (1) {
  2994. ret = drm_modeset_lock_all_ctx(dev, ctx);
  2995. if (ret != -EDEADLK)
  2996. break;
  2997. drm_modeset_backoff(ctx);
  2998. }
  2999. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3000. if (!i915.force_reset_modeset_test &&
  3001. !gpu_reset_clobbers_display(dev_priv))
  3002. return;
  3003. /*
  3004. * Disabling the crtcs gracefully seems nicer. Also the
  3005. * g33 docs say we should at least disable all the planes.
  3006. */
  3007. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3008. if (IS_ERR(state)) {
  3009. ret = PTR_ERR(state);
  3010. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3011. return;
  3012. }
  3013. ret = drm_atomic_helper_disable_all(dev, ctx);
  3014. if (ret) {
  3015. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3016. drm_atomic_state_put(state);
  3017. return;
  3018. }
  3019. dev_priv->modeset_restore_state = state;
  3020. state->acquire_ctx = ctx;
  3021. }
  3022. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3023. {
  3024. struct drm_device *dev = &dev_priv->drm;
  3025. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3026. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3027. int ret;
  3028. /*
  3029. * Flips in the rings will be nuked by the reset,
  3030. * so complete all pending flips so that user space
  3031. * will get its events and not get stuck.
  3032. */
  3033. intel_complete_page_flips(dev_priv);
  3034. dev_priv->modeset_restore_state = NULL;
  3035. /* reset doesn't touch the display */
  3036. if (!gpu_reset_clobbers_display(dev_priv)) {
  3037. if (!state) {
  3038. /*
  3039. * Flips in the rings have been nuked by the reset,
  3040. * so update the base address of all primary
  3041. * planes to the the last fb to make sure we're
  3042. * showing the correct fb after a reset.
  3043. *
  3044. * FIXME: Atomic will make this obsolete since we won't schedule
  3045. * CS-based flips (which might get lost in gpu resets) any more.
  3046. */
  3047. intel_update_primary_planes(dev);
  3048. } else {
  3049. ret = __intel_display_resume(dev, state, ctx);
  3050. if (ret)
  3051. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3052. }
  3053. } else {
  3054. /*
  3055. * The display has been reset as well,
  3056. * so need a full re-initialization.
  3057. */
  3058. intel_runtime_pm_disable_interrupts(dev_priv);
  3059. intel_runtime_pm_enable_interrupts(dev_priv);
  3060. intel_pps_unlock_regs_wa(dev_priv);
  3061. intel_modeset_init_hw(dev);
  3062. spin_lock_irq(&dev_priv->irq_lock);
  3063. if (dev_priv->display.hpd_irq_setup)
  3064. dev_priv->display.hpd_irq_setup(dev_priv);
  3065. spin_unlock_irq(&dev_priv->irq_lock);
  3066. ret = __intel_display_resume(dev, state, ctx);
  3067. if (ret)
  3068. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3069. intel_hpd_init(dev_priv);
  3070. }
  3071. if (state)
  3072. drm_atomic_state_put(state);
  3073. drm_modeset_drop_locks(ctx);
  3074. drm_modeset_acquire_fini(ctx);
  3075. mutex_unlock(&dev->mode_config.mutex);
  3076. }
  3077. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3078. {
  3079. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3080. if (i915_reset_backoff(error))
  3081. return true;
  3082. if (crtc->reset_count != i915_reset_count(error))
  3083. return true;
  3084. return false;
  3085. }
  3086. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3087. {
  3088. struct drm_device *dev = crtc->dev;
  3089. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3090. bool pending;
  3091. if (abort_flip_on_reset(intel_crtc))
  3092. return false;
  3093. spin_lock_irq(&dev->event_lock);
  3094. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3095. spin_unlock_irq(&dev->event_lock);
  3096. return pending;
  3097. }
  3098. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3099. struct intel_crtc_state *old_crtc_state)
  3100. {
  3101. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3102. struct intel_crtc_state *pipe_config =
  3103. to_intel_crtc_state(crtc->base.state);
  3104. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3105. crtc->base.mode = crtc->base.state->mode;
  3106. /*
  3107. * Update pipe size and adjust fitter if needed: the reason for this is
  3108. * that in compute_mode_changes we check the native mode (not the pfit
  3109. * mode) to see if we can flip rather than do a full mode set. In the
  3110. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3111. * pfit state, we'll end up with a big fb scanned out into the wrong
  3112. * sized surface.
  3113. */
  3114. I915_WRITE(PIPESRC(crtc->pipe),
  3115. ((pipe_config->pipe_src_w - 1) << 16) |
  3116. (pipe_config->pipe_src_h - 1));
  3117. /* on skylake this is done by detaching scalers */
  3118. if (INTEL_GEN(dev_priv) >= 9) {
  3119. skl_detach_scalers(crtc);
  3120. if (pipe_config->pch_pfit.enabled)
  3121. skylake_pfit_enable(crtc);
  3122. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3123. if (pipe_config->pch_pfit.enabled)
  3124. ironlake_pfit_enable(crtc);
  3125. else if (old_crtc_state->pch_pfit.enabled)
  3126. ironlake_pfit_disable(crtc, true);
  3127. }
  3128. }
  3129. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3130. {
  3131. struct drm_device *dev = crtc->base.dev;
  3132. struct drm_i915_private *dev_priv = to_i915(dev);
  3133. int pipe = crtc->pipe;
  3134. i915_reg_t reg;
  3135. u32 temp;
  3136. /* enable normal train */
  3137. reg = FDI_TX_CTL(pipe);
  3138. temp = I915_READ(reg);
  3139. if (IS_IVYBRIDGE(dev_priv)) {
  3140. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3141. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3142. } else {
  3143. temp &= ~FDI_LINK_TRAIN_NONE;
  3144. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3145. }
  3146. I915_WRITE(reg, temp);
  3147. reg = FDI_RX_CTL(pipe);
  3148. temp = I915_READ(reg);
  3149. if (HAS_PCH_CPT(dev_priv)) {
  3150. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3151. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3152. } else {
  3153. temp &= ~FDI_LINK_TRAIN_NONE;
  3154. temp |= FDI_LINK_TRAIN_NONE;
  3155. }
  3156. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3157. /* wait one idle pattern time */
  3158. POSTING_READ(reg);
  3159. udelay(1000);
  3160. /* IVB wants error correction enabled */
  3161. if (IS_IVYBRIDGE(dev_priv))
  3162. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3163. FDI_FE_ERRC_ENABLE);
  3164. }
  3165. /* The FDI link training functions for ILK/Ibexpeak. */
  3166. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3167. const struct intel_crtc_state *crtc_state)
  3168. {
  3169. struct drm_device *dev = crtc->base.dev;
  3170. struct drm_i915_private *dev_priv = to_i915(dev);
  3171. int pipe = crtc->pipe;
  3172. i915_reg_t reg;
  3173. u32 temp, tries;
  3174. /* FDI needs bits from pipe first */
  3175. assert_pipe_enabled(dev_priv, pipe);
  3176. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3177. for train result */
  3178. reg = FDI_RX_IMR(pipe);
  3179. temp = I915_READ(reg);
  3180. temp &= ~FDI_RX_SYMBOL_LOCK;
  3181. temp &= ~FDI_RX_BIT_LOCK;
  3182. I915_WRITE(reg, temp);
  3183. I915_READ(reg);
  3184. udelay(150);
  3185. /* enable CPU FDI TX and PCH FDI RX */
  3186. reg = FDI_TX_CTL(pipe);
  3187. temp = I915_READ(reg);
  3188. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3189. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3190. temp &= ~FDI_LINK_TRAIN_NONE;
  3191. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3192. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3193. reg = FDI_RX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. temp &= ~FDI_LINK_TRAIN_NONE;
  3196. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3197. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3198. POSTING_READ(reg);
  3199. udelay(150);
  3200. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3201. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3202. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3203. FDI_RX_PHASE_SYNC_POINTER_EN);
  3204. reg = FDI_RX_IIR(pipe);
  3205. for (tries = 0; tries < 5; tries++) {
  3206. temp = I915_READ(reg);
  3207. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3208. if ((temp & FDI_RX_BIT_LOCK)) {
  3209. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3210. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3211. break;
  3212. }
  3213. }
  3214. if (tries == 5)
  3215. DRM_ERROR("FDI train 1 fail!\n");
  3216. /* Train 2 */
  3217. reg = FDI_TX_CTL(pipe);
  3218. temp = I915_READ(reg);
  3219. temp &= ~FDI_LINK_TRAIN_NONE;
  3220. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3221. I915_WRITE(reg, temp);
  3222. reg = FDI_RX_CTL(pipe);
  3223. temp = I915_READ(reg);
  3224. temp &= ~FDI_LINK_TRAIN_NONE;
  3225. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3226. I915_WRITE(reg, temp);
  3227. POSTING_READ(reg);
  3228. udelay(150);
  3229. reg = FDI_RX_IIR(pipe);
  3230. for (tries = 0; tries < 5; tries++) {
  3231. temp = I915_READ(reg);
  3232. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3233. if (temp & FDI_RX_SYMBOL_LOCK) {
  3234. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3235. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3236. break;
  3237. }
  3238. }
  3239. if (tries == 5)
  3240. DRM_ERROR("FDI train 2 fail!\n");
  3241. DRM_DEBUG_KMS("FDI train done\n");
  3242. }
  3243. static const int snb_b_fdi_train_param[] = {
  3244. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3245. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3246. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3247. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3248. };
  3249. /* The FDI link training functions for SNB/Cougarpoint. */
  3250. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3251. const struct intel_crtc_state *crtc_state)
  3252. {
  3253. struct drm_device *dev = crtc->base.dev;
  3254. struct drm_i915_private *dev_priv = to_i915(dev);
  3255. int pipe = crtc->pipe;
  3256. i915_reg_t reg;
  3257. u32 temp, i, retry;
  3258. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3259. for train result */
  3260. reg = FDI_RX_IMR(pipe);
  3261. temp = I915_READ(reg);
  3262. temp &= ~FDI_RX_SYMBOL_LOCK;
  3263. temp &= ~FDI_RX_BIT_LOCK;
  3264. I915_WRITE(reg, temp);
  3265. POSTING_READ(reg);
  3266. udelay(150);
  3267. /* enable CPU FDI TX and PCH FDI RX */
  3268. reg = FDI_TX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3271. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3272. temp &= ~FDI_LINK_TRAIN_NONE;
  3273. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3274. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3275. /* SNB-B */
  3276. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3277. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3278. I915_WRITE(FDI_RX_MISC(pipe),
  3279. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3280. reg = FDI_RX_CTL(pipe);
  3281. temp = I915_READ(reg);
  3282. if (HAS_PCH_CPT(dev_priv)) {
  3283. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3284. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3285. } else {
  3286. temp &= ~FDI_LINK_TRAIN_NONE;
  3287. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3288. }
  3289. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3290. POSTING_READ(reg);
  3291. udelay(150);
  3292. for (i = 0; i < 4; i++) {
  3293. reg = FDI_TX_CTL(pipe);
  3294. temp = I915_READ(reg);
  3295. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3296. temp |= snb_b_fdi_train_param[i];
  3297. I915_WRITE(reg, temp);
  3298. POSTING_READ(reg);
  3299. udelay(500);
  3300. for (retry = 0; retry < 5; retry++) {
  3301. reg = FDI_RX_IIR(pipe);
  3302. temp = I915_READ(reg);
  3303. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3304. if (temp & FDI_RX_BIT_LOCK) {
  3305. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3306. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3307. break;
  3308. }
  3309. udelay(50);
  3310. }
  3311. if (retry < 5)
  3312. break;
  3313. }
  3314. if (i == 4)
  3315. DRM_ERROR("FDI train 1 fail!\n");
  3316. /* Train 2 */
  3317. reg = FDI_TX_CTL(pipe);
  3318. temp = I915_READ(reg);
  3319. temp &= ~FDI_LINK_TRAIN_NONE;
  3320. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3321. if (IS_GEN6(dev_priv)) {
  3322. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3323. /* SNB-B */
  3324. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3325. }
  3326. I915_WRITE(reg, temp);
  3327. reg = FDI_RX_CTL(pipe);
  3328. temp = I915_READ(reg);
  3329. if (HAS_PCH_CPT(dev_priv)) {
  3330. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3331. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3332. } else {
  3333. temp &= ~FDI_LINK_TRAIN_NONE;
  3334. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3335. }
  3336. I915_WRITE(reg, temp);
  3337. POSTING_READ(reg);
  3338. udelay(150);
  3339. for (i = 0; i < 4; i++) {
  3340. reg = FDI_TX_CTL(pipe);
  3341. temp = I915_READ(reg);
  3342. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3343. temp |= snb_b_fdi_train_param[i];
  3344. I915_WRITE(reg, temp);
  3345. POSTING_READ(reg);
  3346. udelay(500);
  3347. for (retry = 0; retry < 5; retry++) {
  3348. reg = FDI_RX_IIR(pipe);
  3349. temp = I915_READ(reg);
  3350. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3351. if (temp & FDI_RX_SYMBOL_LOCK) {
  3352. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3353. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3354. break;
  3355. }
  3356. udelay(50);
  3357. }
  3358. if (retry < 5)
  3359. break;
  3360. }
  3361. if (i == 4)
  3362. DRM_ERROR("FDI train 2 fail!\n");
  3363. DRM_DEBUG_KMS("FDI train done.\n");
  3364. }
  3365. /* Manual link training for Ivy Bridge A0 parts */
  3366. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3367. const struct intel_crtc_state *crtc_state)
  3368. {
  3369. struct drm_device *dev = crtc->base.dev;
  3370. struct drm_i915_private *dev_priv = to_i915(dev);
  3371. int pipe = crtc->pipe;
  3372. i915_reg_t reg;
  3373. u32 temp, i, j;
  3374. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3375. for train result */
  3376. reg = FDI_RX_IMR(pipe);
  3377. temp = I915_READ(reg);
  3378. temp &= ~FDI_RX_SYMBOL_LOCK;
  3379. temp &= ~FDI_RX_BIT_LOCK;
  3380. I915_WRITE(reg, temp);
  3381. POSTING_READ(reg);
  3382. udelay(150);
  3383. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3384. I915_READ(FDI_RX_IIR(pipe)));
  3385. /* Try each vswing and preemphasis setting twice before moving on */
  3386. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3387. /* disable first in case we need to retry */
  3388. reg = FDI_TX_CTL(pipe);
  3389. temp = I915_READ(reg);
  3390. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3391. temp &= ~FDI_TX_ENABLE;
  3392. I915_WRITE(reg, temp);
  3393. reg = FDI_RX_CTL(pipe);
  3394. temp = I915_READ(reg);
  3395. temp &= ~FDI_LINK_TRAIN_AUTO;
  3396. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3397. temp &= ~FDI_RX_ENABLE;
  3398. I915_WRITE(reg, temp);
  3399. /* enable CPU FDI TX and PCH FDI RX */
  3400. reg = FDI_TX_CTL(pipe);
  3401. temp = I915_READ(reg);
  3402. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3403. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3404. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3405. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3406. temp |= snb_b_fdi_train_param[j/2];
  3407. temp |= FDI_COMPOSITE_SYNC;
  3408. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3409. I915_WRITE(FDI_RX_MISC(pipe),
  3410. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3411. reg = FDI_RX_CTL(pipe);
  3412. temp = I915_READ(reg);
  3413. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3414. temp |= FDI_COMPOSITE_SYNC;
  3415. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3416. POSTING_READ(reg);
  3417. udelay(1); /* should be 0.5us */
  3418. for (i = 0; i < 4; i++) {
  3419. reg = FDI_RX_IIR(pipe);
  3420. temp = I915_READ(reg);
  3421. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3422. if (temp & FDI_RX_BIT_LOCK ||
  3423. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3424. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3425. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3426. i);
  3427. break;
  3428. }
  3429. udelay(1); /* should be 0.5us */
  3430. }
  3431. if (i == 4) {
  3432. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3433. continue;
  3434. }
  3435. /* Train 2 */
  3436. reg = FDI_TX_CTL(pipe);
  3437. temp = I915_READ(reg);
  3438. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3439. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3440. I915_WRITE(reg, temp);
  3441. reg = FDI_RX_CTL(pipe);
  3442. temp = I915_READ(reg);
  3443. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3444. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3445. I915_WRITE(reg, temp);
  3446. POSTING_READ(reg);
  3447. udelay(2); /* should be 1.5us */
  3448. for (i = 0; i < 4; i++) {
  3449. reg = FDI_RX_IIR(pipe);
  3450. temp = I915_READ(reg);
  3451. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3452. if (temp & FDI_RX_SYMBOL_LOCK ||
  3453. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3454. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3455. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3456. i);
  3457. goto train_done;
  3458. }
  3459. udelay(2); /* should be 1.5us */
  3460. }
  3461. if (i == 4)
  3462. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3463. }
  3464. train_done:
  3465. DRM_DEBUG_KMS("FDI train done.\n");
  3466. }
  3467. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3468. {
  3469. struct drm_device *dev = intel_crtc->base.dev;
  3470. struct drm_i915_private *dev_priv = to_i915(dev);
  3471. int pipe = intel_crtc->pipe;
  3472. i915_reg_t reg;
  3473. u32 temp;
  3474. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3475. reg = FDI_RX_CTL(pipe);
  3476. temp = I915_READ(reg);
  3477. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3478. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3479. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3480. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3481. POSTING_READ(reg);
  3482. udelay(200);
  3483. /* Switch from Rawclk to PCDclk */
  3484. temp = I915_READ(reg);
  3485. I915_WRITE(reg, temp | FDI_PCDCLK);
  3486. POSTING_READ(reg);
  3487. udelay(200);
  3488. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3489. reg = FDI_TX_CTL(pipe);
  3490. temp = I915_READ(reg);
  3491. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3492. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3493. POSTING_READ(reg);
  3494. udelay(100);
  3495. }
  3496. }
  3497. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3498. {
  3499. struct drm_device *dev = intel_crtc->base.dev;
  3500. struct drm_i915_private *dev_priv = to_i915(dev);
  3501. int pipe = intel_crtc->pipe;
  3502. i915_reg_t reg;
  3503. u32 temp;
  3504. /* Switch from PCDclk to Rawclk */
  3505. reg = FDI_RX_CTL(pipe);
  3506. temp = I915_READ(reg);
  3507. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3508. /* Disable CPU FDI TX PLL */
  3509. reg = FDI_TX_CTL(pipe);
  3510. temp = I915_READ(reg);
  3511. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3512. POSTING_READ(reg);
  3513. udelay(100);
  3514. reg = FDI_RX_CTL(pipe);
  3515. temp = I915_READ(reg);
  3516. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3517. /* Wait for the clocks to turn off. */
  3518. POSTING_READ(reg);
  3519. udelay(100);
  3520. }
  3521. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3522. {
  3523. struct drm_device *dev = crtc->dev;
  3524. struct drm_i915_private *dev_priv = to_i915(dev);
  3525. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3526. int pipe = intel_crtc->pipe;
  3527. i915_reg_t reg;
  3528. u32 temp;
  3529. /* disable CPU FDI tx and PCH FDI rx */
  3530. reg = FDI_TX_CTL(pipe);
  3531. temp = I915_READ(reg);
  3532. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3533. POSTING_READ(reg);
  3534. reg = FDI_RX_CTL(pipe);
  3535. temp = I915_READ(reg);
  3536. temp &= ~(0x7 << 16);
  3537. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3538. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3539. POSTING_READ(reg);
  3540. udelay(100);
  3541. /* Ironlake workaround, disable clock pointer after downing FDI */
  3542. if (HAS_PCH_IBX(dev_priv))
  3543. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3544. /* still set train pattern 1 */
  3545. reg = FDI_TX_CTL(pipe);
  3546. temp = I915_READ(reg);
  3547. temp &= ~FDI_LINK_TRAIN_NONE;
  3548. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3549. I915_WRITE(reg, temp);
  3550. reg = FDI_RX_CTL(pipe);
  3551. temp = I915_READ(reg);
  3552. if (HAS_PCH_CPT(dev_priv)) {
  3553. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3554. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3555. } else {
  3556. temp &= ~FDI_LINK_TRAIN_NONE;
  3557. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3558. }
  3559. /* BPC in FDI rx is consistent with that in PIPECONF */
  3560. temp &= ~(0x07 << 16);
  3561. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3562. I915_WRITE(reg, temp);
  3563. POSTING_READ(reg);
  3564. udelay(100);
  3565. }
  3566. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3567. {
  3568. struct intel_crtc *crtc;
  3569. /* Note that we don't need to be called with mode_config.lock here
  3570. * as our list of CRTC objects is static for the lifetime of the
  3571. * device and so cannot disappear as we iterate. Similarly, we can
  3572. * happily treat the predicates as racy, atomic checks as userspace
  3573. * cannot claim and pin a new fb without at least acquring the
  3574. * struct_mutex and so serialising with us.
  3575. */
  3576. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3577. if (atomic_read(&crtc->unpin_work_count) == 0)
  3578. continue;
  3579. if (crtc->flip_work)
  3580. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3581. return true;
  3582. }
  3583. return false;
  3584. }
  3585. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3586. {
  3587. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3588. struct intel_flip_work *work = intel_crtc->flip_work;
  3589. intel_crtc->flip_work = NULL;
  3590. if (work->event)
  3591. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3592. drm_crtc_vblank_put(&intel_crtc->base);
  3593. wake_up_all(&dev_priv->pending_flip_queue);
  3594. trace_i915_flip_complete(intel_crtc->plane,
  3595. work->pending_flip_obj);
  3596. queue_work(dev_priv->wq, &work->unpin_work);
  3597. }
  3598. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3599. {
  3600. struct drm_device *dev = crtc->dev;
  3601. struct drm_i915_private *dev_priv = to_i915(dev);
  3602. long ret;
  3603. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3604. ret = wait_event_interruptible_timeout(
  3605. dev_priv->pending_flip_queue,
  3606. !intel_crtc_has_pending_flip(crtc),
  3607. 60*HZ);
  3608. if (ret < 0)
  3609. return ret;
  3610. if (ret == 0) {
  3611. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3612. struct intel_flip_work *work;
  3613. spin_lock_irq(&dev->event_lock);
  3614. work = intel_crtc->flip_work;
  3615. if (work && !is_mmio_work(work)) {
  3616. WARN_ONCE(1, "Removing stuck page flip\n");
  3617. page_flip_completed(intel_crtc);
  3618. }
  3619. spin_unlock_irq(&dev->event_lock);
  3620. }
  3621. return 0;
  3622. }
  3623. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3624. {
  3625. u32 temp;
  3626. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3627. mutex_lock(&dev_priv->sb_lock);
  3628. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3629. temp |= SBI_SSCCTL_DISABLE;
  3630. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3631. mutex_unlock(&dev_priv->sb_lock);
  3632. }
  3633. /* Program iCLKIP clock to the desired frequency */
  3634. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3635. {
  3636. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3637. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3638. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3639. u32 temp;
  3640. lpt_disable_iclkip(dev_priv);
  3641. /* The iCLK virtual clock root frequency is in MHz,
  3642. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3643. * divisors, it is necessary to divide one by another, so we
  3644. * convert the virtual clock precision to KHz here for higher
  3645. * precision.
  3646. */
  3647. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3648. u32 iclk_virtual_root_freq = 172800 * 1000;
  3649. u32 iclk_pi_range = 64;
  3650. u32 desired_divisor;
  3651. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3652. clock << auxdiv);
  3653. divsel = (desired_divisor / iclk_pi_range) - 2;
  3654. phaseinc = desired_divisor % iclk_pi_range;
  3655. /*
  3656. * Near 20MHz is a corner case which is
  3657. * out of range for the 7-bit divisor
  3658. */
  3659. if (divsel <= 0x7f)
  3660. break;
  3661. }
  3662. /* This should not happen with any sane values */
  3663. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3664. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3665. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3666. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3667. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3668. clock,
  3669. auxdiv,
  3670. divsel,
  3671. phasedir,
  3672. phaseinc);
  3673. mutex_lock(&dev_priv->sb_lock);
  3674. /* Program SSCDIVINTPHASE6 */
  3675. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3676. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3677. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3678. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3679. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3680. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3681. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3682. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3683. /* Program SSCAUXDIV */
  3684. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3685. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3686. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3687. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3688. /* Enable modulator and associated divider */
  3689. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3690. temp &= ~SBI_SSCCTL_DISABLE;
  3691. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3692. mutex_unlock(&dev_priv->sb_lock);
  3693. /* Wait for initialization time */
  3694. udelay(24);
  3695. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3696. }
  3697. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3698. {
  3699. u32 divsel, phaseinc, auxdiv;
  3700. u32 iclk_virtual_root_freq = 172800 * 1000;
  3701. u32 iclk_pi_range = 64;
  3702. u32 desired_divisor;
  3703. u32 temp;
  3704. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3705. return 0;
  3706. mutex_lock(&dev_priv->sb_lock);
  3707. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3708. if (temp & SBI_SSCCTL_DISABLE) {
  3709. mutex_unlock(&dev_priv->sb_lock);
  3710. return 0;
  3711. }
  3712. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3713. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3714. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3715. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3716. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3717. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3718. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3719. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3720. mutex_unlock(&dev_priv->sb_lock);
  3721. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3722. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3723. desired_divisor << auxdiv);
  3724. }
  3725. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3726. enum pipe pch_transcoder)
  3727. {
  3728. struct drm_device *dev = crtc->base.dev;
  3729. struct drm_i915_private *dev_priv = to_i915(dev);
  3730. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3731. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3732. I915_READ(HTOTAL(cpu_transcoder)));
  3733. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3734. I915_READ(HBLANK(cpu_transcoder)));
  3735. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3736. I915_READ(HSYNC(cpu_transcoder)));
  3737. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3738. I915_READ(VTOTAL(cpu_transcoder)));
  3739. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3740. I915_READ(VBLANK(cpu_transcoder)));
  3741. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3742. I915_READ(VSYNC(cpu_transcoder)));
  3743. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3744. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3745. }
  3746. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3747. {
  3748. struct drm_i915_private *dev_priv = to_i915(dev);
  3749. uint32_t temp;
  3750. temp = I915_READ(SOUTH_CHICKEN1);
  3751. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3752. return;
  3753. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3754. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3755. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3756. if (enable)
  3757. temp |= FDI_BC_BIFURCATION_SELECT;
  3758. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3759. I915_WRITE(SOUTH_CHICKEN1, temp);
  3760. POSTING_READ(SOUTH_CHICKEN1);
  3761. }
  3762. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3763. {
  3764. struct drm_device *dev = intel_crtc->base.dev;
  3765. switch (intel_crtc->pipe) {
  3766. case PIPE_A:
  3767. break;
  3768. case PIPE_B:
  3769. if (intel_crtc->config->fdi_lanes > 2)
  3770. cpt_set_fdi_bc_bifurcation(dev, false);
  3771. else
  3772. cpt_set_fdi_bc_bifurcation(dev, true);
  3773. break;
  3774. case PIPE_C:
  3775. cpt_set_fdi_bc_bifurcation(dev, true);
  3776. break;
  3777. default:
  3778. BUG();
  3779. }
  3780. }
  3781. /* Return which DP Port should be selected for Transcoder DP control */
  3782. static enum port
  3783. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3784. {
  3785. struct drm_device *dev = crtc->base.dev;
  3786. struct intel_encoder *encoder;
  3787. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3788. if (encoder->type == INTEL_OUTPUT_DP ||
  3789. encoder->type == INTEL_OUTPUT_EDP)
  3790. return enc_to_dig_port(&encoder->base)->port;
  3791. }
  3792. return -1;
  3793. }
  3794. /*
  3795. * Enable PCH resources required for PCH ports:
  3796. * - PCH PLLs
  3797. * - FDI training & RX/TX
  3798. * - update transcoder timings
  3799. * - DP transcoding bits
  3800. * - transcoder
  3801. */
  3802. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3803. {
  3804. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3805. struct drm_device *dev = crtc->base.dev;
  3806. struct drm_i915_private *dev_priv = to_i915(dev);
  3807. int pipe = crtc->pipe;
  3808. u32 temp;
  3809. assert_pch_transcoder_disabled(dev_priv, pipe);
  3810. if (IS_IVYBRIDGE(dev_priv))
  3811. ivybridge_update_fdi_bc_bifurcation(crtc);
  3812. /* Write the TU size bits before fdi link training, so that error
  3813. * detection works. */
  3814. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3815. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3816. /* For PCH output, training FDI link */
  3817. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3818. /* We need to program the right clock selection before writing the pixel
  3819. * mutliplier into the DPLL. */
  3820. if (HAS_PCH_CPT(dev_priv)) {
  3821. u32 sel;
  3822. temp = I915_READ(PCH_DPLL_SEL);
  3823. temp |= TRANS_DPLL_ENABLE(pipe);
  3824. sel = TRANS_DPLLB_SEL(pipe);
  3825. if (crtc_state->shared_dpll ==
  3826. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3827. temp |= sel;
  3828. else
  3829. temp &= ~sel;
  3830. I915_WRITE(PCH_DPLL_SEL, temp);
  3831. }
  3832. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3833. * transcoder, and we actually should do this to not upset any PCH
  3834. * transcoder that already use the clock when we share it.
  3835. *
  3836. * Note that enable_shared_dpll tries to do the right thing, but
  3837. * get_shared_dpll unconditionally resets the pll - we need that to have
  3838. * the right LVDS enable sequence. */
  3839. intel_enable_shared_dpll(crtc);
  3840. /* set transcoder timing, panel must allow it */
  3841. assert_panel_unlocked(dev_priv, pipe);
  3842. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3843. intel_fdi_normal_train(crtc);
  3844. /* For PCH DP, enable TRANS_DP_CTL */
  3845. if (HAS_PCH_CPT(dev_priv) &&
  3846. intel_crtc_has_dp_encoder(crtc_state)) {
  3847. const struct drm_display_mode *adjusted_mode =
  3848. &crtc_state->base.adjusted_mode;
  3849. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3850. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3851. temp = I915_READ(reg);
  3852. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3853. TRANS_DP_SYNC_MASK |
  3854. TRANS_DP_BPC_MASK);
  3855. temp |= TRANS_DP_OUTPUT_ENABLE;
  3856. temp |= bpc << 9; /* same format but at 11:9 */
  3857. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3858. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3859. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3860. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3861. switch (intel_trans_dp_port_sel(crtc)) {
  3862. case PORT_B:
  3863. temp |= TRANS_DP_PORT_SEL_B;
  3864. break;
  3865. case PORT_C:
  3866. temp |= TRANS_DP_PORT_SEL_C;
  3867. break;
  3868. case PORT_D:
  3869. temp |= TRANS_DP_PORT_SEL_D;
  3870. break;
  3871. default:
  3872. BUG();
  3873. }
  3874. I915_WRITE(reg, temp);
  3875. }
  3876. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3877. }
  3878. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3879. {
  3880. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3881. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3882. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3883. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3884. lpt_program_iclkip(crtc);
  3885. /* Set transcoder timing. */
  3886. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3887. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3888. }
  3889. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3890. {
  3891. struct drm_i915_private *dev_priv = to_i915(dev);
  3892. i915_reg_t dslreg = PIPEDSL(pipe);
  3893. u32 temp;
  3894. temp = I915_READ(dslreg);
  3895. udelay(500);
  3896. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3897. if (wait_for(I915_READ(dslreg) != temp, 5))
  3898. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3899. }
  3900. }
  3901. static int
  3902. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3903. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3904. int src_w, int src_h, int dst_w, int dst_h)
  3905. {
  3906. struct intel_crtc_scaler_state *scaler_state =
  3907. &crtc_state->scaler_state;
  3908. struct intel_crtc *intel_crtc =
  3909. to_intel_crtc(crtc_state->base.crtc);
  3910. int need_scaling;
  3911. need_scaling = drm_rotation_90_or_270(rotation) ?
  3912. (src_h != dst_w || src_w != dst_h):
  3913. (src_w != dst_w || src_h != dst_h);
  3914. /*
  3915. * if plane is being disabled or scaler is no more required or force detach
  3916. * - free scaler binded to this plane/crtc
  3917. * - in order to do this, update crtc->scaler_usage
  3918. *
  3919. * Here scaler state in crtc_state is set free so that
  3920. * scaler can be assigned to other user. Actual register
  3921. * update to free the scaler is done in plane/panel-fit programming.
  3922. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3923. */
  3924. if (force_detach || !need_scaling) {
  3925. if (*scaler_id >= 0) {
  3926. scaler_state->scaler_users &= ~(1 << scaler_user);
  3927. scaler_state->scalers[*scaler_id].in_use = 0;
  3928. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3929. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3930. intel_crtc->pipe, scaler_user, *scaler_id,
  3931. scaler_state->scaler_users);
  3932. *scaler_id = -1;
  3933. }
  3934. return 0;
  3935. }
  3936. /* range checks */
  3937. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3938. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3939. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3940. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3941. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3942. "size is out of scaler range\n",
  3943. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3944. return -EINVAL;
  3945. }
  3946. /* mark this plane as a scaler user in crtc_state */
  3947. scaler_state->scaler_users |= (1 << scaler_user);
  3948. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3949. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3950. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3951. scaler_state->scaler_users);
  3952. return 0;
  3953. }
  3954. /**
  3955. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3956. *
  3957. * @state: crtc's scaler state
  3958. *
  3959. * Return
  3960. * 0 - scaler_usage updated successfully
  3961. * error - requested scaling cannot be supported or other error condition
  3962. */
  3963. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3964. {
  3965. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3966. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3967. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3968. state->pipe_src_w, state->pipe_src_h,
  3969. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3970. }
  3971. /**
  3972. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3973. *
  3974. * @state: crtc's scaler state
  3975. * @plane_state: atomic plane state to update
  3976. *
  3977. * Return
  3978. * 0 - scaler_usage updated successfully
  3979. * error - requested scaling cannot be supported or other error condition
  3980. */
  3981. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3982. struct intel_plane_state *plane_state)
  3983. {
  3984. struct intel_plane *intel_plane =
  3985. to_intel_plane(plane_state->base.plane);
  3986. struct drm_framebuffer *fb = plane_state->base.fb;
  3987. int ret;
  3988. bool force_detach = !fb || !plane_state->base.visible;
  3989. ret = skl_update_scaler(crtc_state, force_detach,
  3990. drm_plane_index(&intel_plane->base),
  3991. &plane_state->scaler_id,
  3992. plane_state->base.rotation,
  3993. drm_rect_width(&plane_state->base.src) >> 16,
  3994. drm_rect_height(&plane_state->base.src) >> 16,
  3995. drm_rect_width(&plane_state->base.dst),
  3996. drm_rect_height(&plane_state->base.dst));
  3997. if (ret || plane_state->scaler_id < 0)
  3998. return ret;
  3999. /* check colorkey */
  4000. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4001. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4002. intel_plane->base.base.id,
  4003. intel_plane->base.name);
  4004. return -EINVAL;
  4005. }
  4006. /* Check src format */
  4007. switch (fb->format->format) {
  4008. case DRM_FORMAT_RGB565:
  4009. case DRM_FORMAT_XBGR8888:
  4010. case DRM_FORMAT_XRGB8888:
  4011. case DRM_FORMAT_ABGR8888:
  4012. case DRM_FORMAT_ARGB8888:
  4013. case DRM_FORMAT_XRGB2101010:
  4014. case DRM_FORMAT_XBGR2101010:
  4015. case DRM_FORMAT_YUYV:
  4016. case DRM_FORMAT_YVYU:
  4017. case DRM_FORMAT_UYVY:
  4018. case DRM_FORMAT_VYUY:
  4019. break;
  4020. default:
  4021. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4022. intel_plane->base.base.id, intel_plane->base.name,
  4023. fb->base.id, fb->format->format);
  4024. return -EINVAL;
  4025. }
  4026. return 0;
  4027. }
  4028. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4029. {
  4030. int i;
  4031. for (i = 0; i < crtc->num_scalers; i++)
  4032. skl_detach_scaler(crtc, i);
  4033. }
  4034. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4035. {
  4036. struct drm_device *dev = crtc->base.dev;
  4037. struct drm_i915_private *dev_priv = to_i915(dev);
  4038. int pipe = crtc->pipe;
  4039. struct intel_crtc_scaler_state *scaler_state =
  4040. &crtc->config->scaler_state;
  4041. if (crtc->config->pch_pfit.enabled) {
  4042. int id;
  4043. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4044. return;
  4045. id = scaler_state->scaler_id;
  4046. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4047. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4048. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4049. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4050. }
  4051. }
  4052. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4053. {
  4054. struct drm_device *dev = crtc->base.dev;
  4055. struct drm_i915_private *dev_priv = to_i915(dev);
  4056. int pipe = crtc->pipe;
  4057. if (crtc->config->pch_pfit.enabled) {
  4058. /* Force use of hard-coded filter coefficients
  4059. * as some pre-programmed values are broken,
  4060. * e.g. x201.
  4061. */
  4062. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4063. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4064. PF_PIPE_SEL_IVB(pipe));
  4065. else
  4066. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4067. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4068. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4069. }
  4070. }
  4071. void hsw_enable_ips(struct intel_crtc *crtc)
  4072. {
  4073. struct drm_device *dev = crtc->base.dev;
  4074. struct drm_i915_private *dev_priv = to_i915(dev);
  4075. if (!crtc->config->ips_enabled)
  4076. return;
  4077. /*
  4078. * We can only enable IPS after we enable a plane and wait for a vblank
  4079. * This function is called from post_plane_update, which is run after
  4080. * a vblank wait.
  4081. */
  4082. assert_plane_enabled(dev_priv, crtc->plane);
  4083. if (IS_BROADWELL(dev_priv)) {
  4084. mutex_lock(&dev_priv->rps.hw_lock);
  4085. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4086. mutex_unlock(&dev_priv->rps.hw_lock);
  4087. /* Quoting Art Runyan: "its not safe to expect any particular
  4088. * value in IPS_CTL bit 31 after enabling IPS through the
  4089. * mailbox." Moreover, the mailbox may return a bogus state,
  4090. * so we need to just enable it and continue on.
  4091. */
  4092. } else {
  4093. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4094. /* The bit only becomes 1 in the next vblank, so this wait here
  4095. * is essentially intel_wait_for_vblank. If we don't have this
  4096. * and don't wait for vblanks until the end of crtc_enable, then
  4097. * the HW state readout code will complain that the expected
  4098. * IPS_CTL value is not the one we read. */
  4099. if (intel_wait_for_register(dev_priv,
  4100. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4101. 50))
  4102. DRM_ERROR("Timed out waiting for IPS enable\n");
  4103. }
  4104. }
  4105. void hsw_disable_ips(struct intel_crtc *crtc)
  4106. {
  4107. struct drm_device *dev = crtc->base.dev;
  4108. struct drm_i915_private *dev_priv = to_i915(dev);
  4109. if (!crtc->config->ips_enabled)
  4110. return;
  4111. assert_plane_enabled(dev_priv, crtc->plane);
  4112. if (IS_BROADWELL(dev_priv)) {
  4113. mutex_lock(&dev_priv->rps.hw_lock);
  4114. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4115. mutex_unlock(&dev_priv->rps.hw_lock);
  4116. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4117. if (intel_wait_for_register(dev_priv,
  4118. IPS_CTL, IPS_ENABLE, 0,
  4119. 42))
  4120. DRM_ERROR("Timed out waiting for IPS disable\n");
  4121. } else {
  4122. I915_WRITE(IPS_CTL, 0);
  4123. POSTING_READ(IPS_CTL);
  4124. }
  4125. /* We need to wait for a vblank before we can disable the plane. */
  4126. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4127. }
  4128. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4129. {
  4130. if (intel_crtc->overlay) {
  4131. struct drm_device *dev = intel_crtc->base.dev;
  4132. mutex_lock(&dev->struct_mutex);
  4133. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4134. mutex_unlock(&dev->struct_mutex);
  4135. }
  4136. /* Let userspace switch the overlay on again. In most cases userspace
  4137. * has to recompute where to put it anyway.
  4138. */
  4139. }
  4140. /**
  4141. * intel_post_enable_primary - Perform operations after enabling primary plane
  4142. * @crtc: the CRTC whose primary plane was just enabled
  4143. *
  4144. * Performs potentially sleeping operations that must be done after the primary
  4145. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4146. * called due to an explicit primary plane update, or due to an implicit
  4147. * re-enable that is caused when a sprite plane is updated to no longer
  4148. * completely hide the primary plane.
  4149. */
  4150. static void
  4151. intel_post_enable_primary(struct drm_crtc *crtc)
  4152. {
  4153. struct drm_device *dev = crtc->dev;
  4154. struct drm_i915_private *dev_priv = to_i915(dev);
  4155. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4156. int pipe = intel_crtc->pipe;
  4157. /*
  4158. * FIXME IPS should be fine as long as one plane is
  4159. * enabled, but in practice it seems to have problems
  4160. * when going from primary only to sprite only and vice
  4161. * versa.
  4162. */
  4163. hsw_enable_ips(intel_crtc);
  4164. /*
  4165. * Gen2 reports pipe underruns whenever all planes are disabled.
  4166. * So don't enable underrun reporting before at least some planes
  4167. * are enabled.
  4168. * FIXME: Need to fix the logic to work when we turn off all planes
  4169. * but leave the pipe running.
  4170. */
  4171. if (IS_GEN2(dev_priv))
  4172. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4173. /* Underruns don't always raise interrupts, so check manually. */
  4174. intel_check_cpu_fifo_underruns(dev_priv);
  4175. intel_check_pch_fifo_underruns(dev_priv);
  4176. }
  4177. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4178. static void
  4179. intel_pre_disable_primary(struct drm_crtc *crtc)
  4180. {
  4181. struct drm_device *dev = crtc->dev;
  4182. struct drm_i915_private *dev_priv = to_i915(dev);
  4183. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4184. int pipe = intel_crtc->pipe;
  4185. /*
  4186. * Gen2 reports pipe underruns whenever all planes are disabled.
  4187. * So diasble underrun reporting before all the planes get disabled.
  4188. * FIXME: Need to fix the logic to work when we turn off all planes
  4189. * but leave the pipe running.
  4190. */
  4191. if (IS_GEN2(dev_priv))
  4192. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4193. /*
  4194. * FIXME IPS should be fine as long as one plane is
  4195. * enabled, but in practice it seems to have problems
  4196. * when going from primary only to sprite only and vice
  4197. * versa.
  4198. */
  4199. hsw_disable_ips(intel_crtc);
  4200. }
  4201. /* FIXME get rid of this and use pre_plane_update */
  4202. static void
  4203. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4204. {
  4205. struct drm_device *dev = crtc->dev;
  4206. struct drm_i915_private *dev_priv = to_i915(dev);
  4207. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4208. int pipe = intel_crtc->pipe;
  4209. intel_pre_disable_primary(crtc);
  4210. /*
  4211. * Vblank time updates from the shadow to live plane control register
  4212. * are blocked if the memory self-refresh mode is active at that
  4213. * moment. So to make sure the plane gets truly disabled, disable
  4214. * first the self-refresh mode. The self-refresh enable bit in turn
  4215. * will be checked/applied by the HW only at the next frame start
  4216. * event which is after the vblank start event, so we need to have a
  4217. * wait-for-vblank between disabling the plane and the pipe.
  4218. */
  4219. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4220. intel_set_memory_cxsr(dev_priv, false))
  4221. intel_wait_for_vblank(dev_priv, pipe);
  4222. }
  4223. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4224. {
  4225. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4226. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4227. struct intel_crtc_state *pipe_config =
  4228. to_intel_crtc_state(crtc->base.state);
  4229. struct drm_plane *primary = crtc->base.primary;
  4230. struct drm_plane_state *old_pri_state =
  4231. drm_atomic_get_existing_plane_state(old_state, primary);
  4232. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4233. if (pipe_config->update_wm_post && pipe_config->base.active)
  4234. intel_update_watermarks(crtc);
  4235. if (old_pri_state) {
  4236. struct intel_plane_state *primary_state =
  4237. to_intel_plane_state(primary->state);
  4238. struct intel_plane_state *old_primary_state =
  4239. to_intel_plane_state(old_pri_state);
  4240. intel_fbc_post_update(crtc);
  4241. if (primary_state->base.visible &&
  4242. (needs_modeset(&pipe_config->base) ||
  4243. !old_primary_state->base.visible))
  4244. intel_post_enable_primary(&crtc->base);
  4245. }
  4246. }
  4247. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4248. struct intel_crtc_state *pipe_config)
  4249. {
  4250. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4251. struct drm_device *dev = crtc->base.dev;
  4252. struct drm_i915_private *dev_priv = to_i915(dev);
  4253. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4254. struct drm_plane *primary = crtc->base.primary;
  4255. struct drm_plane_state *old_pri_state =
  4256. drm_atomic_get_existing_plane_state(old_state, primary);
  4257. bool modeset = needs_modeset(&pipe_config->base);
  4258. struct intel_atomic_state *old_intel_state =
  4259. to_intel_atomic_state(old_state);
  4260. if (old_pri_state) {
  4261. struct intel_plane_state *primary_state =
  4262. to_intel_plane_state(primary->state);
  4263. struct intel_plane_state *old_primary_state =
  4264. to_intel_plane_state(old_pri_state);
  4265. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4266. if (old_primary_state->base.visible &&
  4267. (modeset || !primary_state->base.visible))
  4268. intel_pre_disable_primary(&crtc->base);
  4269. }
  4270. /*
  4271. * Vblank time updates from the shadow to live plane control register
  4272. * are blocked if the memory self-refresh mode is active at that
  4273. * moment. So to make sure the plane gets truly disabled, disable
  4274. * first the self-refresh mode. The self-refresh enable bit in turn
  4275. * will be checked/applied by the HW only at the next frame start
  4276. * event which is after the vblank start event, so we need to have a
  4277. * wait-for-vblank between disabling the plane and the pipe.
  4278. */
  4279. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4280. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4281. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4282. /*
  4283. * IVB workaround: must disable low power watermarks for at least
  4284. * one frame before enabling scaling. LP watermarks can be re-enabled
  4285. * when scaling is disabled.
  4286. *
  4287. * WaCxSRDisabledForSpriteScaling:ivb
  4288. */
  4289. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4290. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4291. /*
  4292. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4293. * watermark programming here.
  4294. */
  4295. if (needs_modeset(&pipe_config->base))
  4296. return;
  4297. /*
  4298. * For platforms that support atomic watermarks, program the
  4299. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4300. * will be the intermediate values that are safe for both pre- and
  4301. * post- vblank; when vblank happens, the 'active' values will be set
  4302. * to the final 'target' values and we'll do this again to get the
  4303. * optimal watermarks. For gen9+ platforms, the values we program here
  4304. * will be the final target values which will get automatically latched
  4305. * at vblank time; no further programming will be necessary.
  4306. *
  4307. * If a platform hasn't been transitioned to atomic watermarks yet,
  4308. * we'll continue to update watermarks the old way, if flags tell
  4309. * us to.
  4310. */
  4311. if (dev_priv->display.initial_watermarks != NULL)
  4312. dev_priv->display.initial_watermarks(old_intel_state,
  4313. pipe_config);
  4314. else if (pipe_config->update_wm_pre)
  4315. intel_update_watermarks(crtc);
  4316. }
  4317. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4318. {
  4319. struct drm_device *dev = crtc->dev;
  4320. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4321. struct drm_plane *p;
  4322. int pipe = intel_crtc->pipe;
  4323. intel_crtc_dpms_overlay_disable(intel_crtc);
  4324. drm_for_each_plane_mask(p, dev, plane_mask)
  4325. to_intel_plane(p)->disable_plane(to_intel_plane(p), intel_crtc);
  4326. /*
  4327. * FIXME: Once we grow proper nuclear flip support out of this we need
  4328. * to compute the mask of flip planes precisely. For the time being
  4329. * consider this a flip to a NULL plane.
  4330. */
  4331. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4332. }
  4333. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4334. struct intel_crtc_state *crtc_state,
  4335. struct drm_atomic_state *old_state)
  4336. {
  4337. struct drm_connector_state *conn_state;
  4338. struct drm_connector *conn;
  4339. int i;
  4340. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4341. struct intel_encoder *encoder =
  4342. to_intel_encoder(conn_state->best_encoder);
  4343. if (conn_state->crtc != crtc)
  4344. continue;
  4345. if (encoder->pre_pll_enable)
  4346. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4347. }
  4348. }
  4349. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4350. struct intel_crtc_state *crtc_state,
  4351. struct drm_atomic_state *old_state)
  4352. {
  4353. struct drm_connector_state *conn_state;
  4354. struct drm_connector *conn;
  4355. int i;
  4356. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4357. struct intel_encoder *encoder =
  4358. to_intel_encoder(conn_state->best_encoder);
  4359. if (conn_state->crtc != crtc)
  4360. continue;
  4361. if (encoder->pre_enable)
  4362. encoder->pre_enable(encoder, crtc_state, conn_state);
  4363. }
  4364. }
  4365. static void intel_encoders_enable(struct drm_crtc *crtc,
  4366. struct intel_crtc_state *crtc_state,
  4367. struct drm_atomic_state *old_state)
  4368. {
  4369. struct drm_connector_state *conn_state;
  4370. struct drm_connector *conn;
  4371. int i;
  4372. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4373. struct intel_encoder *encoder =
  4374. to_intel_encoder(conn_state->best_encoder);
  4375. if (conn_state->crtc != crtc)
  4376. continue;
  4377. encoder->enable(encoder, crtc_state, conn_state);
  4378. intel_opregion_notify_encoder(encoder, true);
  4379. }
  4380. }
  4381. static void intel_encoders_disable(struct drm_crtc *crtc,
  4382. struct intel_crtc_state *old_crtc_state,
  4383. struct drm_atomic_state *old_state)
  4384. {
  4385. struct drm_connector_state *old_conn_state;
  4386. struct drm_connector *conn;
  4387. int i;
  4388. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4389. struct intel_encoder *encoder =
  4390. to_intel_encoder(old_conn_state->best_encoder);
  4391. if (old_conn_state->crtc != crtc)
  4392. continue;
  4393. intel_opregion_notify_encoder(encoder, false);
  4394. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4395. }
  4396. }
  4397. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4398. struct intel_crtc_state *old_crtc_state,
  4399. struct drm_atomic_state *old_state)
  4400. {
  4401. struct drm_connector_state *old_conn_state;
  4402. struct drm_connector *conn;
  4403. int i;
  4404. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4405. struct intel_encoder *encoder =
  4406. to_intel_encoder(old_conn_state->best_encoder);
  4407. if (old_conn_state->crtc != crtc)
  4408. continue;
  4409. if (encoder->post_disable)
  4410. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4411. }
  4412. }
  4413. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4414. struct intel_crtc_state *old_crtc_state,
  4415. struct drm_atomic_state *old_state)
  4416. {
  4417. struct drm_connector_state *old_conn_state;
  4418. struct drm_connector *conn;
  4419. int i;
  4420. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4421. struct intel_encoder *encoder =
  4422. to_intel_encoder(old_conn_state->best_encoder);
  4423. if (old_conn_state->crtc != crtc)
  4424. continue;
  4425. if (encoder->post_pll_disable)
  4426. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4427. }
  4428. }
  4429. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4430. struct drm_atomic_state *old_state)
  4431. {
  4432. struct drm_crtc *crtc = pipe_config->base.crtc;
  4433. struct drm_device *dev = crtc->dev;
  4434. struct drm_i915_private *dev_priv = to_i915(dev);
  4435. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4436. int pipe = intel_crtc->pipe;
  4437. struct intel_atomic_state *old_intel_state =
  4438. to_intel_atomic_state(old_state);
  4439. if (WARN_ON(intel_crtc->active))
  4440. return;
  4441. /*
  4442. * Sometimes spurious CPU pipe underruns happen during FDI
  4443. * training, at least with VGA+HDMI cloning. Suppress them.
  4444. *
  4445. * On ILK we get an occasional spurious CPU pipe underruns
  4446. * between eDP port A enable and vdd enable. Also PCH port
  4447. * enable seems to result in the occasional CPU pipe underrun.
  4448. *
  4449. * Spurious PCH underruns also occur during PCH enabling.
  4450. */
  4451. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4452. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4453. if (intel_crtc->config->has_pch_encoder)
  4454. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4455. if (intel_crtc->config->has_pch_encoder)
  4456. intel_prepare_shared_dpll(intel_crtc);
  4457. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4458. intel_dp_set_m_n(intel_crtc, M1_N1);
  4459. intel_set_pipe_timings(intel_crtc);
  4460. intel_set_pipe_src_size(intel_crtc);
  4461. if (intel_crtc->config->has_pch_encoder) {
  4462. intel_cpu_transcoder_set_m_n(intel_crtc,
  4463. &intel_crtc->config->fdi_m_n, NULL);
  4464. }
  4465. ironlake_set_pipeconf(crtc);
  4466. intel_crtc->active = true;
  4467. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4468. if (intel_crtc->config->has_pch_encoder) {
  4469. /* Note: FDI PLL enabling _must_ be done before we enable the
  4470. * cpu pipes, hence this is separate from all the other fdi/pch
  4471. * enabling. */
  4472. ironlake_fdi_pll_enable(intel_crtc);
  4473. } else {
  4474. assert_fdi_tx_disabled(dev_priv, pipe);
  4475. assert_fdi_rx_disabled(dev_priv, pipe);
  4476. }
  4477. ironlake_pfit_enable(intel_crtc);
  4478. /*
  4479. * On ILK+ LUT must be loaded before the pipe is running but with
  4480. * clocks enabled
  4481. */
  4482. intel_color_load_luts(&pipe_config->base);
  4483. if (dev_priv->display.initial_watermarks != NULL)
  4484. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4485. intel_enable_pipe(intel_crtc);
  4486. if (intel_crtc->config->has_pch_encoder)
  4487. ironlake_pch_enable(pipe_config);
  4488. assert_vblank_disabled(crtc);
  4489. drm_crtc_vblank_on(crtc);
  4490. intel_encoders_enable(crtc, pipe_config, old_state);
  4491. if (HAS_PCH_CPT(dev_priv))
  4492. cpt_verify_modeset(dev, intel_crtc->pipe);
  4493. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4494. if (intel_crtc->config->has_pch_encoder)
  4495. intel_wait_for_vblank(dev_priv, pipe);
  4496. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4497. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4498. }
  4499. /* IPS only exists on ULT machines and is tied to pipe A. */
  4500. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4501. {
  4502. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4503. }
  4504. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4505. struct drm_atomic_state *old_state)
  4506. {
  4507. struct drm_crtc *crtc = pipe_config->base.crtc;
  4508. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4509. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4510. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4511. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4512. struct intel_atomic_state *old_intel_state =
  4513. to_intel_atomic_state(old_state);
  4514. if (WARN_ON(intel_crtc->active))
  4515. return;
  4516. if (intel_crtc->config->has_pch_encoder)
  4517. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4518. false);
  4519. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4520. if (intel_crtc->config->shared_dpll)
  4521. intel_enable_shared_dpll(intel_crtc);
  4522. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4523. intel_dp_set_m_n(intel_crtc, M1_N1);
  4524. if (!transcoder_is_dsi(cpu_transcoder))
  4525. intel_set_pipe_timings(intel_crtc);
  4526. intel_set_pipe_src_size(intel_crtc);
  4527. if (cpu_transcoder != TRANSCODER_EDP &&
  4528. !transcoder_is_dsi(cpu_transcoder)) {
  4529. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4530. intel_crtc->config->pixel_multiplier - 1);
  4531. }
  4532. if (intel_crtc->config->has_pch_encoder) {
  4533. intel_cpu_transcoder_set_m_n(intel_crtc,
  4534. &intel_crtc->config->fdi_m_n, NULL);
  4535. }
  4536. if (!transcoder_is_dsi(cpu_transcoder))
  4537. haswell_set_pipeconf(crtc);
  4538. haswell_set_pipemisc(crtc);
  4539. intel_color_set_csc(&pipe_config->base);
  4540. intel_crtc->active = true;
  4541. if (intel_crtc->config->has_pch_encoder)
  4542. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4543. else
  4544. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4545. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4546. if (intel_crtc->config->has_pch_encoder)
  4547. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4548. if (!transcoder_is_dsi(cpu_transcoder))
  4549. intel_ddi_enable_pipe_clock(pipe_config);
  4550. if (INTEL_GEN(dev_priv) >= 9)
  4551. skylake_pfit_enable(intel_crtc);
  4552. else
  4553. ironlake_pfit_enable(intel_crtc);
  4554. /*
  4555. * On ILK+ LUT must be loaded before the pipe is running but with
  4556. * clocks enabled
  4557. */
  4558. intel_color_load_luts(&pipe_config->base);
  4559. intel_ddi_set_pipe_settings(pipe_config);
  4560. if (!transcoder_is_dsi(cpu_transcoder))
  4561. intel_ddi_enable_transcoder_func(pipe_config);
  4562. if (dev_priv->display.initial_watermarks != NULL)
  4563. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4564. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4565. if (!transcoder_is_dsi(cpu_transcoder))
  4566. intel_enable_pipe(intel_crtc);
  4567. if (intel_crtc->config->has_pch_encoder)
  4568. lpt_pch_enable(pipe_config);
  4569. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4570. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4571. assert_vblank_disabled(crtc);
  4572. drm_crtc_vblank_on(crtc);
  4573. intel_encoders_enable(crtc, pipe_config, old_state);
  4574. if (intel_crtc->config->has_pch_encoder) {
  4575. intel_wait_for_vblank(dev_priv, pipe);
  4576. intel_wait_for_vblank(dev_priv, pipe);
  4577. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4578. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4579. true);
  4580. }
  4581. /* If we change the relative order between pipe/planes enabling, we need
  4582. * to change the workaround. */
  4583. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4584. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4585. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4586. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4587. }
  4588. }
  4589. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4590. {
  4591. struct drm_device *dev = crtc->base.dev;
  4592. struct drm_i915_private *dev_priv = to_i915(dev);
  4593. int pipe = crtc->pipe;
  4594. /* To avoid upsetting the power well on haswell only disable the pfit if
  4595. * it's in use. The hw state code will make sure we get this right. */
  4596. if (force || crtc->config->pch_pfit.enabled) {
  4597. I915_WRITE(PF_CTL(pipe), 0);
  4598. I915_WRITE(PF_WIN_POS(pipe), 0);
  4599. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4600. }
  4601. }
  4602. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4603. struct drm_atomic_state *old_state)
  4604. {
  4605. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4606. struct drm_device *dev = crtc->dev;
  4607. struct drm_i915_private *dev_priv = to_i915(dev);
  4608. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4609. int pipe = intel_crtc->pipe;
  4610. /*
  4611. * Sometimes spurious CPU pipe underruns happen when the
  4612. * pipe is already disabled, but FDI RX/TX is still enabled.
  4613. * Happens at least with VGA+HDMI cloning. Suppress them.
  4614. */
  4615. if (intel_crtc->config->has_pch_encoder) {
  4616. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4617. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4618. }
  4619. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4620. drm_crtc_vblank_off(crtc);
  4621. assert_vblank_disabled(crtc);
  4622. intel_disable_pipe(intel_crtc);
  4623. ironlake_pfit_disable(intel_crtc, false);
  4624. if (intel_crtc->config->has_pch_encoder)
  4625. ironlake_fdi_disable(crtc);
  4626. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4627. if (intel_crtc->config->has_pch_encoder) {
  4628. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4629. if (HAS_PCH_CPT(dev_priv)) {
  4630. i915_reg_t reg;
  4631. u32 temp;
  4632. /* disable TRANS_DP_CTL */
  4633. reg = TRANS_DP_CTL(pipe);
  4634. temp = I915_READ(reg);
  4635. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4636. TRANS_DP_PORT_SEL_MASK);
  4637. temp |= TRANS_DP_PORT_SEL_NONE;
  4638. I915_WRITE(reg, temp);
  4639. /* disable DPLL_SEL */
  4640. temp = I915_READ(PCH_DPLL_SEL);
  4641. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4642. I915_WRITE(PCH_DPLL_SEL, temp);
  4643. }
  4644. ironlake_fdi_pll_disable(intel_crtc);
  4645. }
  4646. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4647. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4648. }
  4649. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4650. struct drm_atomic_state *old_state)
  4651. {
  4652. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4653. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4654. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4655. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4656. if (intel_crtc->config->has_pch_encoder)
  4657. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4658. false);
  4659. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4660. drm_crtc_vblank_off(crtc);
  4661. assert_vblank_disabled(crtc);
  4662. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4663. if (!transcoder_is_dsi(cpu_transcoder))
  4664. intel_disable_pipe(intel_crtc);
  4665. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4666. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4667. if (!transcoder_is_dsi(cpu_transcoder))
  4668. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4669. if (INTEL_GEN(dev_priv) >= 9)
  4670. skylake_scaler_disable(intel_crtc);
  4671. else
  4672. ironlake_pfit_disable(intel_crtc, false);
  4673. if (!transcoder_is_dsi(cpu_transcoder))
  4674. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4675. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4676. if (old_crtc_state->has_pch_encoder)
  4677. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4678. true);
  4679. }
  4680. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4681. {
  4682. struct drm_device *dev = crtc->base.dev;
  4683. struct drm_i915_private *dev_priv = to_i915(dev);
  4684. struct intel_crtc_state *pipe_config = crtc->config;
  4685. if (!pipe_config->gmch_pfit.control)
  4686. return;
  4687. /*
  4688. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4689. * according to register description and PRM.
  4690. */
  4691. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4692. assert_pipe_disabled(dev_priv, crtc->pipe);
  4693. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4694. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4695. /* Border color in case we don't scale up to the full screen. Black by
  4696. * default, change to something else for debugging. */
  4697. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4698. }
  4699. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4700. {
  4701. switch (port) {
  4702. case PORT_A:
  4703. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4704. case PORT_B:
  4705. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4706. case PORT_C:
  4707. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4708. case PORT_D:
  4709. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4710. case PORT_E:
  4711. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4712. default:
  4713. MISSING_CASE(port);
  4714. return POWER_DOMAIN_PORT_OTHER;
  4715. }
  4716. }
  4717. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4718. struct intel_crtc_state *crtc_state)
  4719. {
  4720. struct drm_device *dev = crtc->dev;
  4721. struct drm_i915_private *dev_priv = to_i915(dev);
  4722. struct drm_encoder *encoder;
  4723. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4724. enum pipe pipe = intel_crtc->pipe;
  4725. u64 mask;
  4726. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4727. if (!crtc_state->base.active)
  4728. return 0;
  4729. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4730. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4731. if (crtc_state->pch_pfit.enabled ||
  4732. crtc_state->pch_pfit.force_thru)
  4733. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4734. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4735. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4736. mask |= BIT_ULL(intel_encoder->power_domain);
  4737. }
  4738. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4739. mask |= BIT(POWER_DOMAIN_AUDIO);
  4740. if (crtc_state->shared_dpll)
  4741. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4742. return mask;
  4743. }
  4744. static u64
  4745. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4746. struct intel_crtc_state *crtc_state)
  4747. {
  4748. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4749. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4750. enum intel_display_power_domain domain;
  4751. u64 domains, new_domains, old_domains;
  4752. old_domains = intel_crtc->enabled_power_domains;
  4753. intel_crtc->enabled_power_domains = new_domains =
  4754. get_crtc_power_domains(crtc, crtc_state);
  4755. domains = new_domains & ~old_domains;
  4756. for_each_power_domain(domain, domains)
  4757. intel_display_power_get(dev_priv, domain);
  4758. return old_domains & ~new_domains;
  4759. }
  4760. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4761. u64 domains)
  4762. {
  4763. enum intel_display_power_domain domain;
  4764. for_each_power_domain(domain, domains)
  4765. intel_display_power_put(dev_priv, domain);
  4766. }
  4767. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4768. struct drm_atomic_state *old_state)
  4769. {
  4770. struct intel_atomic_state *old_intel_state =
  4771. to_intel_atomic_state(old_state);
  4772. struct drm_crtc *crtc = pipe_config->base.crtc;
  4773. struct drm_device *dev = crtc->dev;
  4774. struct drm_i915_private *dev_priv = to_i915(dev);
  4775. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4776. int pipe = intel_crtc->pipe;
  4777. if (WARN_ON(intel_crtc->active))
  4778. return;
  4779. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4780. intel_dp_set_m_n(intel_crtc, M1_N1);
  4781. intel_set_pipe_timings(intel_crtc);
  4782. intel_set_pipe_src_size(intel_crtc);
  4783. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4784. struct drm_i915_private *dev_priv = to_i915(dev);
  4785. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4786. I915_WRITE(CHV_CANVAS(pipe), 0);
  4787. }
  4788. i9xx_set_pipeconf(intel_crtc);
  4789. intel_crtc->active = true;
  4790. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4791. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4792. if (IS_CHERRYVIEW(dev_priv)) {
  4793. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4794. chv_enable_pll(intel_crtc, intel_crtc->config);
  4795. } else {
  4796. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4797. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4798. }
  4799. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4800. i9xx_pfit_enable(intel_crtc);
  4801. intel_color_load_luts(&pipe_config->base);
  4802. dev_priv->display.initial_watermarks(old_intel_state,
  4803. pipe_config);
  4804. intel_enable_pipe(intel_crtc);
  4805. assert_vblank_disabled(crtc);
  4806. drm_crtc_vblank_on(crtc);
  4807. intel_encoders_enable(crtc, pipe_config, old_state);
  4808. }
  4809. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4810. {
  4811. struct drm_device *dev = crtc->base.dev;
  4812. struct drm_i915_private *dev_priv = to_i915(dev);
  4813. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4814. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4815. }
  4816. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4817. struct drm_atomic_state *old_state)
  4818. {
  4819. struct intel_atomic_state *old_intel_state =
  4820. to_intel_atomic_state(old_state);
  4821. struct drm_crtc *crtc = pipe_config->base.crtc;
  4822. struct drm_device *dev = crtc->dev;
  4823. struct drm_i915_private *dev_priv = to_i915(dev);
  4824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4825. enum pipe pipe = intel_crtc->pipe;
  4826. if (WARN_ON(intel_crtc->active))
  4827. return;
  4828. i9xx_set_pll_dividers(intel_crtc);
  4829. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4830. intel_dp_set_m_n(intel_crtc, M1_N1);
  4831. intel_set_pipe_timings(intel_crtc);
  4832. intel_set_pipe_src_size(intel_crtc);
  4833. i9xx_set_pipeconf(intel_crtc);
  4834. intel_crtc->active = true;
  4835. if (!IS_GEN2(dev_priv))
  4836. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4837. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4838. i9xx_enable_pll(intel_crtc);
  4839. i9xx_pfit_enable(intel_crtc);
  4840. intel_color_load_luts(&pipe_config->base);
  4841. if (dev_priv->display.initial_watermarks != NULL)
  4842. dev_priv->display.initial_watermarks(old_intel_state,
  4843. intel_crtc->config);
  4844. else
  4845. intel_update_watermarks(intel_crtc);
  4846. intel_enable_pipe(intel_crtc);
  4847. assert_vblank_disabled(crtc);
  4848. drm_crtc_vblank_on(crtc);
  4849. intel_encoders_enable(crtc, pipe_config, old_state);
  4850. }
  4851. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4852. {
  4853. struct drm_device *dev = crtc->base.dev;
  4854. struct drm_i915_private *dev_priv = to_i915(dev);
  4855. if (!crtc->config->gmch_pfit.control)
  4856. return;
  4857. assert_pipe_disabled(dev_priv, crtc->pipe);
  4858. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4859. I915_READ(PFIT_CONTROL));
  4860. I915_WRITE(PFIT_CONTROL, 0);
  4861. }
  4862. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4863. struct drm_atomic_state *old_state)
  4864. {
  4865. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4866. struct drm_device *dev = crtc->dev;
  4867. struct drm_i915_private *dev_priv = to_i915(dev);
  4868. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4869. int pipe = intel_crtc->pipe;
  4870. /*
  4871. * On gen2 planes are double buffered but the pipe isn't, so we must
  4872. * wait for planes to fully turn off before disabling the pipe.
  4873. */
  4874. if (IS_GEN2(dev_priv))
  4875. intel_wait_for_vblank(dev_priv, pipe);
  4876. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4877. drm_crtc_vblank_off(crtc);
  4878. assert_vblank_disabled(crtc);
  4879. intel_disable_pipe(intel_crtc);
  4880. i9xx_pfit_disable(intel_crtc);
  4881. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4882. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4883. if (IS_CHERRYVIEW(dev_priv))
  4884. chv_disable_pll(dev_priv, pipe);
  4885. else if (IS_VALLEYVIEW(dev_priv))
  4886. vlv_disable_pll(dev_priv, pipe);
  4887. else
  4888. i9xx_disable_pll(intel_crtc);
  4889. }
  4890. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4891. if (!IS_GEN2(dev_priv))
  4892. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4893. if (!dev_priv->display.initial_watermarks)
  4894. intel_update_watermarks(intel_crtc);
  4895. }
  4896. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  4897. {
  4898. struct intel_encoder *encoder;
  4899. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4900. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4901. enum intel_display_power_domain domain;
  4902. u64 domains;
  4903. struct drm_atomic_state *state;
  4904. struct intel_crtc_state *crtc_state;
  4905. int ret;
  4906. if (!intel_crtc->active)
  4907. return;
  4908. if (crtc->primary->state->visible) {
  4909. WARN_ON(intel_crtc->flip_work);
  4910. intel_pre_disable_primary_noatomic(crtc);
  4911. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4912. crtc->primary->state->visible = false;
  4913. }
  4914. state = drm_atomic_state_alloc(crtc->dev);
  4915. if (!state) {
  4916. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4917. crtc->base.id, crtc->name);
  4918. return;
  4919. }
  4920. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  4921. /* Everything's already locked, -EDEADLK can't happen. */
  4922. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4923. ret = drm_atomic_add_affected_connectors(state, crtc);
  4924. WARN_ON(IS_ERR(crtc_state) || ret);
  4925. dev_priv->display.crtc_disable(crtc_state, state);
  4926. drm_atomic_state_put(state);
  4927. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4928. crtc->base.id, crtc->name);
  4929. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4930. crtc->state->active = false;
  4931. intel_crtc->active = false;
  4932. crtc->enabled = false;
  4933. crtc->state->connector_mask = 0;
  4934. crtc->state->encoder_mask = 0;
  4935. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4936. encoder->base.crtc = NULL;
  4937. intel_fbc_disable(intel_crtc);
  4938. intel_update_watermarks(intel_crtc);
  4939. intel_disable_shared_dpll(intel_crtc);
  4940. domains = intel_crtc->enabled_power_domains;
  4941. for_each_power_domain(domain, domains)
  4942. intel_display_power_put(dev_priv, domain);
  4943. intel_crtc->enabled_power_domains = 0;
  4944. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  4945. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  4946. }
  4947. /*
  4948. * turn all crtc's off, but do not adjust state
  4949. * This has to be paired with a call to intel_modeset_setup_hw_state.
  4950. */
  4951. int intel_display_suspend(struct drm_device *dev)
  4952. {
  4953. struct drm_i915_private *dev_priv = to_i915(dev);
  4954. struct drm_atomic_state *state;
  4955. int ret;
  4956. state = drm_atomic_helper_suspend(dev);
  4957. ret = PTR_ERR_OR_ZERO(state);
  4958. if (ret)
  4959. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  4960. else
  4961. dev_priv->modeset_restore_state = state;
  4962. return ret;
  4963. }
  4964. void intel_encoder_destroy(struct drm_encoder *encoder)
  4965. {
  4966. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4967. drm_encoder_cleanup(encoder);
  4968. kfree(intel_encoder);
  4969. }
  4970. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4971. * internal consistency). */
  4972. static void intel_connector_verify_state(struct intel_connector *connector)
  4973. {
  4974. struct drm_crtc *crtc = connector->base.state->crtc;
  4975. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4976. connector->base.base.id,
  4977. connector->base.name);
  4978. if (connector->get_hw_state(connector)) {
  4979. struct intel_encoder *encoder = connector->encoder;
  4980. struct drm_connector_state *conn_state = connector->base.state;
  4981. I915_STATE_WARN(!crtc,
  4982. "connector enabled without attached crtc\n");
  4983. if (!crtc)
  4984. return;
  4985. I915_STATE_WARN(!crtc->state->active,
  4986. "connector is active, but attached crtc isn't\n");
  4987. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  4988. return;
  4989. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  4990. "atomic encoder doesn't match attached encoder\n");
  4991. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  4992. "attached encoder crtc differs from connector crtc\n");
  4993. } else {
  4994. I915_STATE_WARN(crtc && crtc->state->active,
  4995. "attached crtc is active, but connector isn't\n");
  4996. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  4997. "best encoder set without crtc!\n");
  4998. }
  4999. }
  5000. int intel_connector_init(struct intel_connector *connector)
  5001. {
  5002. drm_atomic_helper_connector_reset(&connector->base);
  5003. if (!connector->base.state)
  5004. return -ENOMEM;
  5005. return 0;
  5006. }
  5007. struct intel_connector *intel_connector_alloc(void)
  5008. {
  5009. struct intel_connector *connector;
  5010. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5011. if (!connector)
  5012. return NULL;
  5013. if (intel_connector_init(connector) < 0) {
  5014. kfree(connector);
  5015. return NULL;
  5016. }
  5017. return connector;
  5018. }
  5019. /* Simple connector->get_hw_state implementation for encoders that support only
  5020. * one connector and no cloning and hence the encoder state determines the state
  5021. * of the connector. */
  5022. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5023. {
  5024. enum pipe pipe = 0;
  5025. struct intel_encoder *encoder = connector->encoder;
  5026. return encoder->get_hw_state(encoder, &pipe);
  5027. }
  5028. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5029. {
  5030. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5031. return crtc_state->fdi_lanes;
  5032. return 0;
  5033. }
  5034. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5035. struct intel_crtc_state *pipe_config)
  5036. {
  5037. struct drm_i915_private *dev_priv = to_i915(dev);
  5038. struct drm_atomic_state *state = pipe_config->base.state;
  5039. struct intel_crtc *other_crtc;
  5040. struct intel_crtc_state *other_crtc_state;
  5041. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5042. pipe_name(pipe), pipe_config->fdi_lanes);
  5043. if (pipe_config->fdi_lanes > 4) {
  5044. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5045. pipe_name(pipe), pipe_config->fdi_lanes);
  5046. return -EINVAL;
  5047. }
  5048. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5049. if (pipe_config->fdi_lanes > 2) {
  5050. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5051. pipe_config->fdi_lanes);
  5052. return -EINVAL;
  5053. } else {
  5054. return 0;
  5055. }
  5056. }
  5057. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5058. return 0;
  5059. /* Ivybridge 3 pipe is really complicated */
  5060. switch (pipe) {
  5061. case PIPE_A:
  5062. return 0;
  5063. case PIPE_B:
  5064. if (pipe_config->fdi_lanes <= 2)
  5065. return 0;
  5066. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5067. other_crtc_state =
  5068. intel_atomic_get_crtc_state(state, other_crtc);
  5069. if (IS_ERR(other_crtc_state))
  5070. return PTR_ERR(other_crtc_state);
  5071. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5072. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5073. pipe_name(pipe), pipe_config->fdi_lanes);
  5074. return -EINVAL;
  5075. }
  5076. return 0;
  5077. case PIPE_C:
  5078. if (pipe_config->fdi_lanes > 2) {
  5079. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5080. pipe_name(pipe), pipe_config->fdi_lanes);
  5081. return -EINVAL;
  5082. }
  5083. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5084. other_crtc_state =
  5085. intel_atomic_get_crtc_state(state, other_crtc);
  5086. if (IS_ERR(other_crtc_state))
  5087. return PTR_ERR(other_crtc_state);
  5088. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5089. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5090. return -EINVAL;
  5091. }
  5092. return 0;
  5093. default:
  5094. BUG();
  5095. }
  5096. }
  5097. #define RETRY 1
  5098. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5099. struct intel_crtc_state *pipe_config)
  5100. {
  5101. struct drm_device *dev = intel_crtc->base.dev;
  5102. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5103. int lane, link_bw, fdi_dotclock, ret;
  5104. bool needs_recompute = false;
  5105. retry:
  5106. /* FDI is a binary signal running at ~2.7GHz, encoding
  5107. * each output octet as 10 bits. The actual frequency
  5108. * is stored as a divider into a 100MHz clock, and the
  5109. * mode pixel clock is stored in units of 1KHz.
  5110. * Hence the bw of each lane in terms of the mode signal
  5111. * is:
  5112. */
  5113. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5114. fdi_dotclock = adjusted_mode->crtc_clock;
  5115. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5116. pipe_config->pipe_bpp);
  5117. pipe_config->fdi_lanes = lane;
  5118. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5119. link_bw, &pipe_config->fdi_m_n);
  5120. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5121. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5122. pipe_config->pipe_bpp -= 2*3;
  5123. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5124. pipe_config->pipe_bpp);
  5125. needs_recompute = true;
  5126. pipe_config->bw_constrained = true;
  5127. goto retry;
  5128. }
  5129. if (needs_recompute)
  5130. return RETRY;
  5131. return ret;
  5132. }
  5133. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5134. struct intel_crtc_state *pipe_config)
  5135. {
  5136. if (pipe_config->pipe_bpp > 24)
  5137. return false;
  5138. /* HSW can handle pixel rate up to cdclk? */
  5139. if (IS_HASWELL(dev_priv))
  5140. return true;
  5141. /*
  5142. * We compare against max which means we must take
  5143. * the increased cdclk requirement into account when
  5144. * calculating the new cdclk.
  5145. *
  5146. * Should measure whether using a lower cdclk w/o IPS
  5147. */
  5148. return pipe_config->pixel_rate <=
  5149. dev_priv->max_cdclk_freq * 95 / 100;
  5150. }
  5151. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5152. struct intel_crtc_state *pipe_config)
  5153. {
  5154. struct drm_device *dev = crtc->base.dev;
  5155. struct drm_i915_private *dev_priv = to_i915(dev);
  5156. pipe_config->ips_enabled = i915.enable_ips &&
  5157. hsw_crtc_supports_ips(crtc) &&
  5158. pipe_config_supports_ips(dev_priv, pipe_config);
  5159. }
  5160. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5161. {
  5162. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5163. /* GDG double wide on either pipe, otherwise pipe A only */
  5164. return INTEL_INFO(dev_priv)->gen < 4 &&
  5165. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5166. }
  5167. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5168. {
  5169. uint32_t pixel_rate;
  5170. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5171. /*
  5172. * We only use IF-ID interlacing. If we ever use
  5173. * PF-ID we'll need to adjust the pixel_rate here.
  5174. */
  5175. if (pipe_config->pch_pfit.enabled) {
  5176. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5177. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5178. pipe_w = pipe_config->pipe_src_w;
  5179. pipe_h = pipe_config->pipe_src_h;
  5180. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5181. pfit_h = pfit_size & 0xFFFF;
  5182. if (pipe_w < pfit_w)
  5183. pipe_w = pfit_w;
  5184. if (pipe_h < pfit_h)
  5185. pipe_h = pfit_h;
  5186. if (WARN_ON(!pfit_w || !pfit_h))
  5187. return pixel_rate;
  5188. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5189. pfit_w * pfit_h);
  5190. }
  5191. return pixel_rate;
  5192. }
  5193. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5194. {
  5195. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5196. if (HAS_GMCH_DISPLAY(dev_priv))
  5197. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5198. crtc_state->pixel_rate =
  5199. crtc_state->base.adjusted_mode.crtc_clock;
  5200. else
  5201. crtc_state->pixel_rate =
  5202. ilk_pipe_pixel_rate(crtc_state);
  5203. }
  5204. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5205. struct intel_crtc_state *pipe_config)
  5206. {
  5207. struct drm_device *dev = crtc->base.dev;
  5208. struct drm_i915_private *dev_priv = to_i915(dev);
  5209. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5210. int clock_limit = dev_priv->max_dotclk_freq;
  5211. if (INTEL_GEN(dev_priv) < 4) {
  5212. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5213. /*
  5214. * Enable double wide mode when the dot clock
  5215. * is > 90% of the (display) core speed.
  5216. */
  5217. if (intel_crtc_supports_double_wide(crtc) &&
  5218. adjusted_mode->crtc_clock > clock_limit) {
  5219. clock_limit = dev_priv->max_dotclk_freq;
  5220. pipe_config->double_wide = true;
  5221. }
  5222. }
  5223. if (adjusted_mode->crtc_clock > clock_limit) {
  5224. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5225. adjusted_mode->crtc_clock, clock_limit,
  5226. yesno(pipe_config->double_wide));
  5227. return -EINVAL;
  5228. }
  5229. /*
  5230. * Pipe horizontal size must be even in:
  5231. * - DVO ganged mode
  5232. * - LVDS dual channel mode
  5233. * - Double wide pipe
  5234. */
  5235. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5236. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5237. pipe_config->pipe_src_w &= ~1;
  5238. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5239. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5240. */
  5241. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5242. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5243. return -EINVAL;
  5244. intel_crtc_compute_pixel_rate(pipe_config);
  5245. if (HAS_IPS(dev_priv))
  5246. hsw_compute_ips_config(crtc, pipe_config);
  5247. if (pipe_config->has_pch_encoder)
  5248. return ironlake_fdi_compute_config(crtc, pipe_config);
  5249. return 0;
  5250. }
  5251. static void
  5252. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5253. {
  5254. while (*num > DATA_LINK_M_N_MASK ||
  5255. *den > DATA_LINK_M_N_MASK) {
  5256. *num >>= 1;
  5257. *den >>= 1;
  5258. }
  5259. }
  5260. static void compute_m_n(unsigned int m, unsigned int n,
  5261. uint32_t *ret_m, uint32_t *ret_n)
  5262. {
  5263. /*
  5264. * Reduce M/N as much as possible without loss in precision. Several DP
  5265. * dongles in particular seem to be fussy about too large *link* M/N
  5266. * values. The passed in values are more likely to have the least
  5267. * significant bits zero than M after rounding below, so do this first.
  5268. */
  5269. while ((m & 1) == 0 && (n & 1) == 0) {
  5270. m >>= 1;
  5271. n >>= 1;
  5272. }
  5273. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5274. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5275. intel_reduce_m_n_ratio(ret_m, ret_n);
  5276. }
  5277. void
  5278. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5279. int pixel_clock, int link_clock,
  5280. struct intel_link_m_n *m_n)
  5281. {
  5282. m_n->tu = 64;
  5283. compute_m_n(bits_per_pixel * pixel_clock,
  5284. link_clock * nlanes * 8,
  5285. &m_n->gmch_m, &m_n->gmch_n);
  5286. compute_m_n(pixel_clock, link_clock,
  5287. &m_n->link_m, &m_n->link_n);
  5288. }
  5289. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5290. {
  5291. if (i915.panel_use_ssc >= 0)
  5292. return i915.panel_use_ssc != 0;
  5293. return dev_priv->vbt.lvds_use_ssc
  5294. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5295. }
  5296. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5297. {
  5298. return (1 << dpll->n) << 16 | dpll->m2;
  5299. }
  5300. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5301. {
  5302. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5303. }
  5304. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5305. struct intel_crtc_state *crtc_state,
  5306. struct dpll *reduced_clock)
  5307. {
  5308. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5309. u32 fp, fp2 = 0;
  5310. if (IS_PINEVIEW(dev_priv)) {
  5311. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5312. if (reduced_clock)
  5313. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5314. } else {
  5315. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5316. if (reduced_clock)
  5317. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5318. }
  5319. crtc_state->dpll_hw_state.fp0 = fp;
  5320. crtc->lowfreq_avail = false;
  5321. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5322. reduced_clock) {
  5323. crtc_state->dpll_hw_state.fp1 = fp2;
  5324. crtc->lowfreq_avail = true;
  5325. } else {
  5326. crtc_state->dpll_hw_state.fp1 = fp;
  5327. }
  5328. }
  5329. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5330. pipe)
  5331. {
  5332. u32 reg_val;
  5333. /*
  5334. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5335. * and set it to a reasonable value instead.
  5336. */
  5337. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5338. reg_val &= 0xffffff00;
  5339. reg_val |= 0x00000030;
  5340. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5341. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5342. reg_val &= 0x00ffffff;
  5343. reg_val |= 0x8c000000;
  5344. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5345. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5346. reg_val &= 0xffffff00;
  5347. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5348. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5349. reg_val &= 0x00ffffff;
  5350. reg_val |= 0xb0000000;
  5351. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5352. }
  5353. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5354. struct intel_link_m_n *m_n)
  5355. {
  5356. struct drm_device *dev = crtc->base.dev;
  5357. struct drm_i915_private *dev_priv = to_i915(dev);
  5358. int pipe = crtc->pipe;
  5359. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5360. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5361. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5362. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5363. }
  5364. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5365. struct intel_link_m_n *m_n,
  5366. struct intel_link_m_n *m2_n2)
  5367. {
  5368. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5369. int pipe = crtc->pipe;
  5370. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5371. if (INTEL_GEN(dev_priv) >= 5) {
  5372. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5373. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5374. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5375. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5376. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5377. * for gen < 8) and if DRRS is supported (to make sure the
  5378. * registers are not unnecessarily accessed).
  5379. */
  5380. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5381. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5382. I915_WRITE(PIPE_DATA_M2(transcoder),
  5383. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5384. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5385. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5386. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5387. }
  5388. } else {
  5389. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5390. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5391. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5392. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5393. }
  5394. }
  5395. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5396. {
  5397. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5398. if (m_n == M1_N1) {
  5399. dp_m_n = &crtc->config->dp_m_n;
  5400. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5401. } else if (m_n == M2_N2) {
  5402. /*
  5403. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5404. * needs to be programmed into M1_N1.
  5405. */
  5406. dp_m_n = &crtc->config->dp_m2_n2;
  5407. } else {
  5408. DRM_ERROR("Unsupported divider value\n");
  5409. return;
  5410. }
  5411. if (crtc->config->has_pch_encoder)
  5412. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5413. else
  5414. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5415. }
  5416. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5417. struct intel_crtc_state *pipe_config)
  5418. {
  5419. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5420. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5421. if (crtc->pipe != PIPE_A)
  5422. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5423. /* DPLL not used with DSI, but still need the rest set up */
  5424. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5425. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5426. DPLL_EXT_BUFFER_ENABLE_VLV;
  5427. pipe_config->dpll_hw_state.dpll_md =
  5428. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5429. }
  5430. static void chv_compute_dpll(struct intel_crtc *crtc,
  5431. struct intel_crtc_state *pipe_config)
  5432. {
  5433. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5434. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5435. if (crtc->pipe != PIPE_A)
  5436. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5437. /* DPLL not used with DSI, but still need the rest set up */
  5438. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5439. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5440. pipe_config->dpll_hw_state.dpll_md =
  5441. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5442. }
  5443. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5444. const struct intel_crtc_state *pipe_config)
  5445. {
  5446. struct drm_device *dev = crtc->base.dev;
  5447. struct drm_i915_private *dev_priv = to_i915(dev);
  5448. enum pipe pipe = crtc->pipe;
  5449. u32 mdiv;
  5450. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5451. u32 coreclk, reg_val;
  5452. /* Enable Refclk */
  5453. I915_WRITE(DPLL(pipe),
  5454. pipe_config->dpll_hw_state.dpll &
  5455. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5456. /* No need to actually set up the DPLL with DSI */
  5457. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5458. return;
  5459. mutex_lock(&dev_priv->sb_lock);
  5460. bestn = pipe_config->dpll.n;
  5461. bestm1 = pipe_config->dpll.m1;
  5462. bestm2 = pipe_config->dpll.m2;
  5463. bestp1 = pipe_config->dpll.p1;
  5464. bestp2 = pipe_config->dpll.p2;
  5465. /* See eDP HDMI DPIO driver vbios notes doc */
  5466. /* PLL B needs special handling */
  5467. if (pipe == PIPE_B)
  5468. vlv_pllb_recal_opamp(dev_priv, pipe);
  5469. /* Set up Tx target for periodic Rcomp update */
  5470. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5471. /* Disable target IRef on PLL */
  5472. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5473. reg_val &= 0x00ffffff;
  5474. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5475. /* Disable fast lock */
  5476. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5477. /* Set idtafcrecal before PLL is enabled */
  5478. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5479. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5480. mdiv |= ((bestn << DPIO_N_SHIFT));
  5481. mdiv |= (1 << DPIO_K_SHIFT);
  5482. /*
  5483. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5484. * but we don't support that).
  5485. * Note: don't use the DAC post divider as it seems unstable.
  5486. */
  5487. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5488. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5489. mdiv |= DPIO_ENABLE_CALIBRATION;
  5490. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5491. /* Set HBR and RBR LPF coefficients */
  5492. if (pipe_config->port_clock == 162000 ||
  5493. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5494. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5495. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5496. 0x009f0003);
  5497. else
  5498. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5499. 0x00d0000f);
  5500. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5501. /* Use SSC source */
  5502. if (pipe == PIPE_A)
  5503. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5504. 0x0df40000);
  5505. else
  5506. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5507. 0x0df70000);
  5508. } else { /* HDMI or VGA */
  5509. /* Use bend source */
  5510. if (pipe == PIPE_A)
  5511. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5512. 0x0df70000);
  5513. else
  5514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5515. 0x0df40000);
  5516. }
  5517. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5518. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5519. if (intel_crtc_has_dp_encoder(crtc->config))
  5520. coreclk |= 0x01000000;
  5521. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5522. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5523. mutex_unlock(&dev_priv->sb_lock);
  5524. }
  5525. static void chv_prepare_pll(struct intel_crtc *crtc,
  5526. const struct intel_crtc_state *pipe_config)
  5527. {
  5528. struct drm_device *dev = crtc->base.dev;
  5529. struct drm_i915_private *dev_priv = to_i915(dev);
  5530. enum pipe pipe = crtc->pipe;
  5531. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5532. u32 loopfilter, tribuf_calcntr;
  5533. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5534. u32 dpio_val;
  5535. int vco;
  5536. /* Enable Refclk and SSC */
  5537. I915_WRITE(DPLL(pipe),
  5538. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5539. /* No need to actually set up the DPLL with DSI */
  5540. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5541. return;
  5542. bestn = pipe_config->dpll.n;
  5543. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5544. bestm1 = pipe_config->dpll.m1;
  5545. bestm2 = pipe_config->dpll.m2 >> 22;
  5546. bestp1 = pipe_config->dpll.p1;
  5547. bestp2 = pipe_config->dpll.p2;
  5548. vco = pipe_config->dpll.vco;
  5549. dpio_val = 0;
  5550. loopfilter = 0;
  5551. mutex_lock(&dev_priv->sb_lock);
  5552. /* p1 and p2 divider */
  5553. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5554. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5555. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5556. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5557. 1 << DPIO_CHV_K_DIV_SHIFT);
  5558. /* Feedback post-divider - m2 */
  5559. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5560. /* Feedback refclk divider - n and m1 */
  5561. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5562. DPIO_CHV_M1_DIV_BY_2 |
  5563. 1 << DPIO_CHV_N_DIV_SHIFT);
  5564. /* M2 fraction division */
  5565. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5566. /* M2 fraction division enable */
  5567. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5568. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5569. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5570. if (bestm2_frac)
  5571. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5572. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5573. /* Program digital lock detect threshold */
  5574. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5575. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5576. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5577. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5578. if (!bestm2_frac)
  5579. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5580. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5581. /* Loop filter */
  5582. if (vco == 5400000) {
  5583. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5584. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5585. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5586. tribuf_calcntr = 0x9;
  5587. } else if (vco <= 6200000) {
  5588. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5589. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5590. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5591. tribuf_calcntr = 0x9;
  5592. } else if (vco <= 6480000) {
  5593. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5594. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5595. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5596. tribuf_calcntr = 0x8;
  5597. } else {
  5598. /* Not supported. Apply the same limits as in the max case */
  5599. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5600. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5601. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5602. tribuf_calcntr = 0;
  5603. }
  5604. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5605. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5606. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5607. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5608. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5609. /* AFC Recal */
  5610. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5611. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5612. DPIO_AFC_RECAL);
  5613. mutex_unlock(&dev_priv->sb_lock);
  5614. }
  5615. /**
  5616. * vlv_force_pll_on - forcibly enable just the PLL
  5617. * @dev_priv: i915 private structure
  5618. * @pipe: pipe PLL to enable
  5619. * @dpll: PLL configuration
  5620. *
  5621. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5622. * in cases where we need the PLL enabled even when @pipe is not going to
  5623. * be enabled.
  5624. */
  5625. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5626. const struct dpll *dpll)
  5627. {
  5628. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5629. struct intel_crtc_state *pipe_config;
  5630. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5631. if (!pipe_config)
  5632. return -ENOMEM;
  5633. pipe_config->base.crtc = &crtc->base;
  5634. pipe_config->pixel_multiplier = 1;
  5635. pipe_config->dpll = *dpll;
  5636. if (IS_CHERRYVIEW(dev_priv)) {
  5637. chv_compute_dpll(crtc, pipe_config);
  5638. chv_prepare_pll(crtc, pipe_config);
  5639. chv_enable_pll(crtc, pipe_config);
  5640. } else {
  5641. vlv_compute_dpll(crtc, pipe_config);
  5642. vlv_prepare_pll(crtc, pipe_config);
  5643. vlv_enable_pll(crtc, pipe_config);
  5644. }
  5645. kfree(pipe_config);
  5646. return 0;
  5647. }
  5648. /**
  5649. * vlv_force_pll_off - forcibly disable just the PLL
  5650. * @dev_priv: i915 private structure
  5651. * @pipe: pipe PLL to disable
  5652. *
  5653. * Disable the PLL for @pipe. To be used in cases where we need
  5654. * the PLL enabled even when @pipe is not going to be enabled.
  5655. */
  5656. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5657. {
  5658. if (IS_CHERRYVIEW(dev_priv))
  5659. chv_disable_pll(dev_priv, pipe);
  5660. else
  5661. vlv_disable_pll(dev_priv, pipe);
  5662. }
  5663. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5664. struct intel_crtc_state *crtc_state,
  5665. struct dpll *reduced_clock)
  5666. {
  5667. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5668. u32 dpll;
  5669. struct dpll *clock = &crtc_state->dpll;
  5670. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5671. dpll = DPLL_VGA_MODE_DIS;
  5672. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5673. dpll |= DPLLB_MODE_LVDS;
  5674. else
  5675. dpll |= DPLLB_MODE_DAC_SERIAL;
  5676. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5677. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5678. dpll |= (crtc_state->pixel_multiplier - 1)
  5679. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5680. }
  5681. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5682. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5683. dpll |= DPLL_SDVO_HIGH_SPEED;
  5684. if (intel_crtc_has_dp_encoder(crtc_state))
  5685. dpll |= DPLL_SDVO_HIGH_SPEED;
  5686. /* compute bitmask from p1 value */
  5687. if (IS_PINEVIEW(dev_priv))
  5688. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5689. else {
  5690. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5691. if (IS_G4X(dev_priv) && reduced_clock)
  5692. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5693. }
  5694. switch (clock->p2) {
  5695. case 5:
  5696. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5697. break;
  5698. case 7:
  5699. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5700. break;
  5701. case 10:
  5702. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5703. break;
  5704. case 14:
  5705. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5706. break;
  5707. }
  5708. if (INTEL_GEN(dev_priv) >= 4)
  5709. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5710. if (crtc_state->sdvo_tv_clock)
  5711. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5712. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5713. intel_panel_use_ssc(dev_priv))
  5714. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5715. else
  5716. dpll |= PLL_REF_INPUT_DREFCLK;
  5717. dpll |= DPLL_VCO_ENABLE;
  5718. crtc_state->dpll_hw_state.dpll = dpll;
  5719. if (INTEL_GEN(dev_priv) >= 4) {
  5720. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5721. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5722. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5723. }
  5724. }
  5725. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5726. struct intel_crtc_state *crtc_state,
  5727. struct dpll *reduced_clock)
  5728. {
  5729. struct drm_device *dev = crtc->base.dev;
  5730. struct drm_i915_private *dev_priv = to_i915(dev);
  5731. u32 dpll;
  5732. struct dpll *clock = &crtc_state->dpll;
  5733. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5734. dpll = DPLL_VGA_MODE_DIS;
  5735. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5736. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5737. } else {
  5738. if (clock->p1 == 2)
  5739. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5740. else
  5741. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5742. if (clock->p2 == 4)
  5743. dpll |= PLL_P2_DIVIDE_BY_4;
  5744. }
  5745. if (!IS_I830(dev_priv) &&
  5746. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5747. dpll |= DPLL_DVO_2X_MODE;
  5748. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5749. intel_panel_use_ssc(dev_priv))
  5750. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5751. else
  5752. dpll |= PLL_REF_INPUT_DREFCLK;
  5753. dpll |= DPLL_VCO_ENABLE;
  5754. crtc_state->dpll_hw_state.dpll = dpll;
  5755. }
  5756. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5757. {
  5758. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5759. enum pipe pipe = intel_crtc->pipe;
  5760. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5761. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5762. uint32_t crtc_vtotal, crtc_vblank_end;
  5763. int vsyncshift = 0;
  5764. /* We need to be careful not to changed the adjusted mode, for otherwise
  5765. * the hw state checker will get angry at the mismatch. */
  5766. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5767. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5768. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5769. /* the chip adds 2 halflines automatically */
  5770. crtc_vtotal -= 1;
  5771. crtc_vblank_end -= 1;
  5772. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5773. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5774. else
  5775. vsyncshift = adjusted_mode->crtc_hsync_start -
  5776. adjusted_mode->crtc_htotal / 2;
  5777. if (vsyncshift < 0)
  5778. vsyncshift += adjusted_mode->crtc_htotal;
  5779. }
  5780. if (INTEL_GEN(dev_priv) > 3)
  5781. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5782. I915_WRITE(HTOTAL(cpu_transcoder),
  5783. (adjusted_mode->crtc_hdisplay - 1) |
  5784. ((adjusted_mode->crtc_htotal - 1) << 16));
  5785. I915_WRITE(HBLANK(cpu_transcoder),
  5786. (adjusted_mode->crtc_hblank_start - 1) |
  5787. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5788. I915_WRITE(HSYNC(cpu_transcoder),
  5789. (adjusted_mode->crtc_hsync_start - 1) |
  5790. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5791. I915_WRITE(VTOTAL(cpu_transcoder),
  5792. (adjusted_mode->crtc_vdisplay - 1) |
  5793. ((crtc_vtotal - 1) << 16));
  5794. I915_WRITE(VBLANK(cpu_transcoder),
  5795. (adjusted_mode->crtc_vblank_start - 1) |
  5796. ((crtc_vblank_end - 1) << 16));
  5797. I915_WRITE(VSYNC(cpu_transcoder),
  5798. (adjusted_mode->crtc_vsync_start - 1) |
  5799. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5800. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5801. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5802. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5803. * bits. */
  5804. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5805. (pipe == PIPE_B || pipe == PIPE_C))
  5806. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5807. }
  5808. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5809. {
  5810. struct drm_device *dev = intel_crtc->base.dev;
  5811. struct drm_i915_private *dev_priv = to_i915(dev);
  5812. enum pipe pipe = intel_crtc->pipe;
  5813. /* pipesrc controls the size that is scaled from, which should
  5814. * always be the user's requested size.
  5815. */
  5816. I915_WRITE(PIPESRC(pipe),
  5817. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5818. (intel_crtc->config->pipe_src_h - 1));
  5819. }
  5820. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5821. struct intel_crtc_state *pipe_config)
  5822. {
  5823. struct drm_device *dev = crtc->base.dev;
  5824. struct drm_i915_private *dev_priv = to_i915(dev);
  5825. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5826. uint32_t tmp;
  5827. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5828. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5829. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5830. tmp = I915_READ(HBLANK(cpu_transcoder));
  5831. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5832. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5833. tmp = I915_READ(HSYNC(cpu_transcoder));
  5834. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5835. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5836. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5837. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5838. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5839. tmp = I915_READ(VBLANK(cpu_transcoder));
  5840. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5841. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5842. tmp = I915_READ(VSYNC(cpu_transcoder));
  5843. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5844. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5845. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5846. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5847. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5848. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5849. }
  5850. }
  5851. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5852. struct intel_crtc_state *pipe_config)
  5853. {
  5854. struct drm_device *dev = crtc->base.dev;
  5855. struct drm_i915_private *dev_priv = to_i915(dev);
  5856. u32 tmp;
  5857. tmp = I915_READ(PIPESRC(crtc->pipe));
  5858. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5859. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5860. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5861. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5862. }
  5863. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5864. struct intel_crtc_state *pipe_config)
  5865. {
  5866. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5867. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5868. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5869. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5870. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5871. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5872. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5873. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5874. mode->flags = pipe_config->base.adjusted_mode.flags;
  5875. mode->type = DRM_MODE_TYPE_DRIVER;
  5876. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5877. mode->hsync = drm_mode_hsync(mode);
  5878. mode->vrefresh = drm_mode_vrefresh(mode);
  5879. drm_mode_set_name(mode);
  5880. }
  5881. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5882. {
  5883. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5884. uint32_t pipeconf;
  5885. pipeconf = 0;
  5886. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5887. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5888. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5889. if (intel_crtc->config->double_wide)
  5890. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5891. /* only g4x and later have fancy bpc/dither controls */
  5892. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5893. IS_CHERRYVIEW(dev_priv)) {
  5894. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5895. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5896. pipeconf |= PIPECONF_DITHER_EN |
  5897. PIPECONF_DITHER_TYPE_SP;
  5898. switch (intel_crtc->config->pipe_bpp) {
  5899. case 18:
  5900. pipeconf |= PIPECONF_6BPC;
  5901. break;
  5902. case 24:
  5903. pipeconf |= PIPECONF_8BPC;
  5904. break;
  5905. case 30:
  5906. pipeconf |= PIPECONF_10BPC;
  5907. break;
  5908. default:
  5909. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5910. BUG();
  5911. }
  5912. }
  5913. if (HAS_PIPE_CXSR(dev_priv)) {
  5914. if (intel_crtc->lowfreq_avail) {
  5915. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5916. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5917. } else {
  5918. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5919. }
  5920. }
  5921. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5922. if (INTEL_GEN(dev_priv) < 4 ||
  5923. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5924. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5925. else
  5926. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5927. } else
  5928. pipeconf |= PIPECONF_PROGRESSIVE;
  5929. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5930. intel_crtc->config->limited_color_range)
  5931. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5932. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5933. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5934. }
  5935. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5936. struct intel_crtc_state *crtc_state)
  5937. {
  5938. struct drm_device *dev = crtc->base.dev;
  5939. struct drm_i915_private *dev_priv = to_i915(dev);
  5940. const struct intel_limit *limit;
  5941. int refclk = 48000;
  5942. memset(&crtc_state->dpll_hw_state, 0,
  5943. sizeof(crtc_state->dpll_hw_state));
  5944. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5945. if (intel_panel_use_ssc(dev_priv)) {
  5946. refclk = dev_priv->vbt.lvds_ssc_freq;
  5947. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5948. }
  5949. limit = &intel_limits_i8xx_lvds;
  5950. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5951. limit = &intel_limits_i8xx_dvo;
  5952. } else {
  5953. limit = &intel_limits_i8xx_dac;
  5954. }
  5955. if (!crtc_state->clock_set &&
  5956. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5957. refclk, NULL, &crtc_state->dpll)) {
  5958. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5959. return -EINVAL;
  5960. }
  5961. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5962. return 0;
  5963. }
  5964. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5965. struct intel_crtc_state *crtc_state)
  5966. {
  5967. struct drm_device *dev = crtc->base.dev;
  5968. struct drm_i915_private *dev_priv = to_i915(dev);
  5969. const struct intel_limit *limit;
  5970. int refclk = 96000;
  5971. memset(&crtc_state->dpll_hw_state, 0,
  5972. sizeof(crtc_state->dpll_hw_state));
  5973. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5974. if (intel_panel_use_ssc(dev_priv)) {
  5975. refclk = dev_priv->vbt.lvds_ssc_freq;
  5976. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5977. }
  5978. if (intel_is_dual_link_lvds(dev))
  5979. limit = &intel_limits_g4x_dual_channel_lvds;
  5980. else
  5981. limit = &intel_limits_g4x_single_channel_lvds;
  5982. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  5983. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  5984. limit = &intel_limits_g4x_hdmi;
  5985. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  5986. limit = &intel_limits_g4x_sdvo;
  5987. } else {
  5988. /* The option is for other outputs */
  5989. limit = &intel_limits_i9xx_sdvo;
  5990. }
  5991. if (!crtc_state->clock_set &&
  5992. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5993. refclk, NULL, &crtc_state->dpll)) {
  5994. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5995. return -EINVAL;
  5996. }
  5997. i9xx_compute_dpll(crtc, crtc_state, NULL);
  5998. return 0;
  5999. }
  6000. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6001. struct intel_crtc_state *crtc_state)
  6002. {
  6003. struct drm_device *dev = crtc->base.dev;
  6004. struct drm_i915_private *dev_priv = to_i915(dev);
  6005. const struct intel_limit *limit;
  6006. int refclk = 96000;
  6007. memset(&crtc_state->dpll_hw_state, 0,
  6008. sizeof(crtc_state->dpll_hw_state));
  6009. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6010. if (intel_panel_use_ssc(dev_priv)) {
  6011. refclk = dev_priv->vbt.lvds_ssc_freq;
  6012. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6013. }
  6014. limit = &intel_limits_pineview_lvds;
  6015. } else {
  6016. limit = &intel_limits_pineview_sdvo;
  6017. }
  6018. if (!crtc_state->clock_set &&
  6019. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6020. refclk, NULL, &crtc_state->dpll)) {
  6021. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6022. return -EINVAL;
  6023. }
  6024. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6025. return 0;
  6026. }
  6027. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6028. struct intel_crtc_state *crtc_state)
  6029. {
  6030. struct drm_device *dev = crtc->base.dev;
  6031. struct drm_i915_private *dev_priv = to_i915(dev);
  6032. const struct intel_limit *limit;
  6033. int refclk = 96000;
  6034. memset(&crtc_state->dpll_hw_state, 0,
  6035. sizeof(crtc_state->dpll_hw_state));
  6036. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6037. if (intel_panel_use_ssc(dev_priv)) {
  6038. refclk = dev_priv->vbt.lvds_ssc_freq;
  6039. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6040. }
  6041. limit = &intel_limits_i9xx_lvds;
  6042. } else {
  6043. limit = &intel_limits_i9xx_sdvo;
  6044. }
  6045. if (!crtc_state->clock_set &&
  6046. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6047. refclk, NULL, &crtc_state->dpll)) {
  6048. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6049. return -EINVAL;
  6050. }
  6051. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6052. return 0;
  6053. }
  6054. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6055. struct intel_crtc_state *crtc_state)
  6056. {
  6057. int refclk = 100000;
  6058. const struct intel_limit *limit = &intel_limits_chv;
  6059. memset(&crtc_state->dpll_hw_state, 0,
  6060. sizeof(crtc_state->dpll_hw_state));
  6061. if (!crtc_state->clock_set &&
  6062. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6063. refclk, NULL, &crtc_state->dpll)) {
  6064. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6065. return -EINVAL;
  6066. }
  6067. chv_compute_dpll(crtc, crtc_state);
  6068. return 0;
  6069. }
  6070. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6071. struct intel_crtc_state *crtc_state)
  6072. {
  6073. int refclk = 100000;
  6074. const struct intel_limit *limit = &intel_limits_vlv;
  6075. memset(&crtc_state->dpll_hw_state, 0,
  6076. sizeof(crtc_state->dpll_hw_state));
  6077. if (!crtc_state->clock_set &&
  6078. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6079. refclk, NULL, &crtc_state->dpll)) {
  6080. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6081. return -EINVAL;
  6082. }
  6083. vlv_compute_dpll(crtc, crtc_state);
  6084. return 0;
  6085. }
  6086. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6087. struct intel_crtc_state *pipe_config)
  6088. {
  6089. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6090. uint32_t tmp;
  6091. if (INTEL_GEN(dev_priv) <= 3 &&
  6092. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6093. return;
  6094. tmp = I915_READ(PFIT_CONTROL);
  6095. if (!(tmp & PFIT_ENABLE))
  6096. return;
  6097. /* Check whether the pfit is attached to our pipe. */
  6098. if (INTEL_GEN(dev_priv) < 4) {
  6099. if (crtc->pipe != PIPE_B)
  6100. return;
  6101. } else {
  6102. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6103. return;
  6104. }
  6105. pipe_config->gmch_pfit.control = tmp;
  6106. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6107. }
  6108. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6109. struct intel_crtc_state *pipe_config)
  6110. {
  6111. struct drm_device *dev = crtc->base.dev;
  6112. struct drm_i915_private *dev_priv = to_i915(dev);
  6113. int pipe = pipe_config->cpu_transcoder;
  6114. struct dpll clock;
  6115. u32 mdiv;
  6116. int refclk = 100000;
  6117. /* In case of DSI, DPLL will not be used */
  6118. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6119. return;
  6120. mutex_lock(&dev_priv->sb_lock);
  6121. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6122. mutex_unlock(&dev_priv->sb_lock);
  6123. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6124. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6125. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6126. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6127. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6128. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6129. }
  6130. static void
  6131. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6132. struct intel_initial_plane_config *plane_config)
  6133. {
  6134. struct drm_device *dev = crtc->base.dev;
  6135. struct drm_i915_private *dev_priv = to_i915(dev);
  6136. u32 val, base, offset;
  6137. int pipe = crtc->pipe, plane = crtc->plane;
  6138. int fourcc, pixel_format;
  6139. unsigned int aligned_height;
  6140. struct drm_framebuffer *fb;
  6141. struct intel_framebuffer *intel_fb;
  6142. val = I915_READ(DSPCNTR(plane));
  6143. if (!(val & DISPLAY_PLANE_ENABLE))
  6144. return;
  6145. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6146. if (!intel_fb) {
  6147. DRM_DEBUG_KMS("failed to alloc fb\n");
  6148. return;
  6149. }
  6150. fb = &intel_fb->base;
  6151. fb->dev = dev;
  6152. if (INTEL_GEN(dev_priv) >= 4) {
  6153. if (val & DISPPLANE_TILED) {
  6154. plane_config->tiling = I915_TILING_X;
  6155. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6156. }
  6157. }
  6158. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6159. fourcc = i9xx_format_to_fourcc(pixel_format);
  6160. fb->format = drm_format_info(fourcc);
  6161. if (INTEL_GEN(dev_priv) >= 4) {
  6162. if (plane_config->tiling)
  6163. offset = I915_READ(DSPTILEOFF(plane));
  6164. else
  6165. offset = I915_READ(DSPLINOFF(plane));
  6166. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6167. } else {
  6168. base = I915_READ(DSPADDR(plane));
  6169. }
  6170. plane_config->base = base;
  6171. val = I915_READ(PIPESRC(pipe));
  6172. fb->width = ((val >> 16) & 0xfff) + 1;
  6173. fb->height = ((val >> 0) & 0xfff) + 1;
  6174. val = I915_READ(DSPSTRIDE(pipe));
  6175. fb->pitches[0] = val & 0xffffffc0;
  6176. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6177. plane_config->size = fb->pitches[0] * aligned_height;
  6178. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6179. pipe_name(pipe), plane, fb->width, fb->height,
  6180. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6181. plane_config->size);
  6182. plane_config->fb = intel_fb;
  6183. }
  6184. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6185. struct intel_crtc_state *pipe_config)
  6186. {
  6187. struct drm_device *dev = crtc->base.dev;
  6188. struct drm_i915_private *dev_priv = to_i915(dev);
  6189. int pipe = pipe_config->cpu_transcoder;
  6190. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6191. struct dpll clock;
  6192. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6193. int refclk = 100000;
  6194. /* In case of DSI, DPLL will not be used */
  6195. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6196. return;
  6197. mutex_lock(&dev_priv->sb_lock);
  6198. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6199. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6200. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6201. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6202. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6203. mutex_unlock(&dev_priv->sb_lock);
  6204. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6205. clock.m2 = (pll_dw0 & 0xff) << 22;
  6206. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6207. clock.m2 |= pll_dw2 & 0x3fffff;
  6208. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6209. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6210. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6211. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6212. }
  6213. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6214. struct intel_crtc_state *pipe_config)
  6215. {
  6216. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6217. enum intel_display_power_domain power_domain;
  6218. uint32_t tmp;
  6219. bool ret;
  6220. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6221. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6222. return false;
  6223. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6224. pipe_config->shared_dpll = NULL;
  6225. ret = false;
  6226. tmp = I915_READ(PIPECONF(crtc->pipe));
  6227. if (!(tmp & PIPECONF_ENABLE))
  6228. goto out;
  6229. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6230. IS_CHERRYVIEW(dev_priv)) {
  6231. switch (tmp & PIPECONF_BPC_MASK) {
  6232. case PIPECONF_6BPC:
  6233. pipe_config->pipe_bpp = 18;
  6234. break;
  6235. case PIPECONF_8BPC:
  6236. pipe_config->pipe_bpp = 24;
  6237. break;
  6238. case PIPECONF_10BPC:
  6239. pipe_config->pipe_bpp = 30;
  6240. break;
  6241. default:
  6242. break;
  6243. }
  6244. }
  6245. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6246. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6247. pipe_config->limited_color_range = true;
  6248. if (INTEL_GEN(dev_priv) < 4)
  6249. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6250. intel_get_pipe_timings(crtc, pipe_config);
  6251. intel_get_pipe_src_size(crtc, pipe_config);
  6252. i9xx_get_pfit_config(crtc, pipe_config);
  6253. if (INTEL_GEN(dev_priv) >= 4) {
  6254. /* No way to read it out on pipes B and C */
  6255. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6256. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6257. else
  6258. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6259. pipe_config->pixel_multiplier =
  6260. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6261. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6262. pipe_config->dpll_hw_state.dpll_md = tmp;
  6263. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6264. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6265. tmp = I915_READ(DPLL(crtc->pipe));
  6266. pipe_config->pixel_multiplier =
  6267. ((tmp & SDVO_MULTIPLIER_MASK)
  6268. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6269. } else {
  6270. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6271. * port and will be fixed up in the encoder->get_config
  6272. * function. */
  6273. pipe_config->pixel_multiplier = 1;
  6274. }
  6275. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6276. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6277. /*
  6278. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6279. * on 830. Filter it out here so that we don't
  6280. * report errors due to that.
  6281. */
  6282. if (IS_I830(dev_priv))
  6283. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6284. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6285. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6286. } else {
  6287. /* Mask out read-only status bits. */
  6288. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6289. DPLL_PORTC_READY_MASK |
  6290. DPLL_PORTB_READY_MASK);
  6291. }
  6292. if (IS_CHERRYVIEW(dev_priv))
  6293. chv_crtc_clock_get(crtc, pipe_config);
  6294. else if (IS_VALLEYVIEW(dev_priv))
  6295. vlv_crtc_clock_get(crtc, pipe_config);
  6296. else
  6297. i9xx_crtc_clock_get(crtc, pipe_config);
  6298. /*
  6299. * Normally the dotclock is filled in by the encoder .get_config()
  6300. * but in case the pipe is enabled w/o any ports we need a sane
  6301. * default.
  6302. */
  6303. pipe_config->base.adjusted_mode.crtc_clock =
  6304. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6305. ret = true;
  6306. out:
  6307. intel_display_power_put(dev_priv, power_domain);
  6308. return ret;
  6309. }
  6310. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6311. {
  6312. struct intel_encoder *encoder;
  6313. int i;
  6314. u32 val, final;
  6315. bool has_lvds = false;
  6316. bool has_cpu_edp = false;
  6317. bool has_panel = false;
  6318. bool has_ck505 = false;
  6319. bool can_ssc = false;
  6320. bool using_ssc_source = false;
  6321. /* We need to take the global config into account */
  6322. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6323. switch (encoder->type) {
  6324. case INTEL_OUTPUT_LVDS:
  6325. has_panel = true;
  6326. has_lvds = true;
  6327. break;
  6328. case INTEL_OUTPUT_EDP:
  6329. has_panel = true;
  6330. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6331. has_cpu_edp = true;
  6332. break;
  6333. default:
  6334. break;
  6335. }
  6336. }
  6337. if (HAS_PCH_IBX(dev_priv)) {
  6338. has_ck505 = dev_priv->vbt.display_clock_mode;
  6339. can_ssc = has_ck505;
  6340. } else {
  6341. has_ck505 = false;
  6342. can_ssc = true;
  6343. }
  6344. /* Check if any DPLLs are using the SSC source */
  6345. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6346. u32 temp = I915_READ(PCH_DPLL(i));
  6347. if (!(temp & DPLL_VCO_ENABLE))
  6348. continue;
  6349. if ((temp & PLL_REF_INPUT_MASK) ==
  6350. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6351. using_ssc_source = true;
  6352. break;
  6353. }
  6354. }
  6355. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6356. has_panel, has_lvds, has_ck505, using_ssc_source);
  6357. /* Ironlake: try to setup display ref clock before DPLL
  6358. * enabling. This is only under driver's control after
  6359. * PCH B stepping, previous chipset stepping should be
  6360. * ignoring this setting.
  6361. */
  6362. val = I915_READ(PCH_DREF_CONTROL);
  6363. /* As we must carefully and slowly disable/enable each source in turn,
  6364. * compute the final state we want first and check if we need to
  6365. * make any changes at all.
  6366. */
  6367. final = val;
  6368. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6369. if (has_ck505)
  6370. final |= DREF_NONSPREAD_CK505_ENABLE;
  6371. else
  6372. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6373. final &= ~DREF_SSC_SOURCE_MASK;
  6374. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6375. final &= ~DREF_SSC1_ENABLE;
  6376. if (has_panel) {
  6377. final |= DREF_SSC_SOURCE_ENABLE;
  6378. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6379. final |= DREF_SSC1_ENABLE;
  6380. if (has_cpu_edp) {
  6381. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6382. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6383. else
  6384. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6385. } else
  6386. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6387. } else if (using_ssc_source) {
  6388. final |= DREF_SSC_SOURCE_ENABLE;
  6389. final |= DREF_SSC1_ENABLE;
  6390. }
  6391. if (final == val)
  6392. return;
  6393. /* Always enable nonspread source */
  6394. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6395. if (has_ck505)
  6396. val |= DREF_NONSPREAD_CK505_ENABLE;
  6397. else
  6398. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6399. if (has_panel) {
  6400. val &= ~DREF_SSC_SOURCE_MASK;
  6401. val |= DREF_SSC_SOURCE_ENABLE;
  6402. /* SSC must be turned on before enabling the CPU output */
  6403. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6404. DRM_DEBUG_KMS("Using SSC on panel\n");
  6405. val |= DREF_SSC1_ENABLE;
  6406. } else
  6407. val &= ~DREF_SSC1_ENABLE;
  6408. /* Get SSC going before enabling the outputs */
  6409. I915_WRITE(PCH_DREF_CONTROL, val);
  6410. POSTING_READ(PCH_DREF_CONTROL);
  6411. udelay(200);
  6412. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6413. /* Enable CPU source on CPU attached eDP */
  6414. if (has_cpu_edp) {
  6415. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6416. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6417. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6418. } else
  6419. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6420. } else
  6421. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6422. I915_WRITE(PCH_DREF_CONTROL, val);
  6423. POSTING_READ(PCH_DREF_CONTROL);
  6424. udelay(200);
  6425. } else {
  6426. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6427. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6428. /* Turn off CPU output */
  6429. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6430. I915_WRITE(PCH_DREF_CONTROL, val);
  6431. POSTING_READ(PCH_DREF_CONTROL);
  6432. udelay(200);
  6433. if (!using_ssc_source) {
  6434. DRM_DEBUG_KMS("Disabling SSC source\n");
  6435. /* Turn off the SSC source */
  6436. val &= ~DREF_SSC_SOURCE_MASK;
  6437. val |= DREF_SSC_SOURCE_DISABLE;
  6438. /* Turn off SSC1 */
  6439. val &= ~DREF_SSC1_ENABLE;
  6440. I915_WRITE(PCH_DREF_CONTROL, val);
  6441. POSTING_READ(PCH_DREF_CONTROL);
  6442. udelay(200);
  6443. }
  6444. }
  6445. BUG_ON(val != final);
  6446. }
  6447. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6448. {
  6449. uint32_t tmp;
  6450. tmp = I915_READ(SOUTH_CHICKEN2);
  6451. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6452. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6453. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6454. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6455. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6456. tmp = I915_READ(SOUTH_CHICKEN2);
  6457. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6458. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6459. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6460. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6461. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6462. }
  6463. /* WaMPhyProgramming:hsw */
  6464. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6465. {
  6466. uint32_t tmp;
  6467. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6468. tmp &= ~(0xFF << 24);
  6469. tmp |= (0x12 << 24);
  6470. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6471. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6472. tmp |= (1 << 11);
  6473. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6474. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6475. tmp |= (1 << 11);
  6476. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6477. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6478. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6479. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6480. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6481. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6482. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6483. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6484. tmp &= ~(7 << 13);
  6485. tmp |= (5 << 13);
  6486. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6487. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6488. tmp &= ~(7 << 13);
  6489. tmp |= (5 << 13);
  6490. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6491. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6492. tmp &= ~0xFF;
  6493. tmp |= 0x1C;
  6494. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6495. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6496. tmp &= ~0xFF;
  6497. tmp |= 0x1C;
  6498. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6499. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6500. tmp &= ~(0xFF << 16);
  6501. tmp |= (0x1C << 16);
  6502. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6503. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6504. tmp &= ~(0xFF << 16);
  6505. tmp |= (0x1C << 16);
  6506. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6507. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6508. tmp |= (1 << 27);
  6509. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6510. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6511. tmp |= (1 << 27);
  6512. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6513. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6514. tmp &= ~(0xF << 28);
  6515. tmp |= (4 << 28);
  6516. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6517. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6518. tmp &= ~(0xF << 28);
  6519. tmp |= (4 << 28);
  6520. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6521. }
  6522. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6523. * Programming" based on the parameters passed:
  6524. * - Sequence to enable CLKOUT_DP
  6525. * - Sequence to enable CLKOUT_DP without spread
  6526. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6527. */
  6528. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6529. bool with_spread, bool with_fdi)
  6530. {
  6531. uint32_t reg, tmp;
  6532. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6533. with_spread = true;
  6534. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6535. with_fdi, "LP PCH doesn't have FDI\n"))
  6536. with_fdi = false;
  6537. mutex_lock(&dev_priv->sb_lock);
  6538. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6539. tmp &= ~SBI_SSCCTL_DISABLE;
  6540. tmp |= SBI_SSCCTL_PATHALT;
  6541. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6542. udelay(24);
  6543. if (with_spread) {
  6544. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6545. tmp &= ~SBI_SSCCTL_PATHALT;
  6546. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6547. if (with_fdi) {
  6548. lpt_reset_fdi_mphy(dev_priv);
  6549. lpt_program_fdi_mphy(dev_priv);
  6550. }
  6551. }
  6552. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6553. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6554. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6555. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6556. mutex_unlock(&dev_priv->sb_lock);
  6557. }
  6558. /* Sequence to disable CLKOUT_DP */
  6559. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6560. {
  6561. uint32_t reg, tmp;
  6562. mutex_lock(&dev_priv->sb_lock);
  6563. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6564. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6565. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6566. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6567. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6568. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6569. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6570. tmp |= SBI_SSCCTL_PATHALT;
  6571. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6572. udelay(32);
  6573. }
  6574. tmp |= SBI_SSCCTL_DISABLE;
  6575. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6576. }
  6577. mutex_unlock(&dev_priv->sb_lock);
  6578. }
  6579. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6580. static const uint16_t sscdivintphase[] = {
  6581. [BEND_IDX( 50)] = 0x3B23,
  6582. [BEND_IDX( 45)] = 0x3B23,
  6583. [BEND_IDX( 40)] = 0x3C23,
  6584. [BEND_IDX( 35)] = 0x3C23,
  6585. [BEND_IDX( 30)] = 0x3D23,
  6586. [BEND_IDX( 25)] = 0x3D23,
  6587. [BEND_IDX( 20)] = 0x3E23,
  6588. [BEND_IDX( 15)] = 0x3E23,
  6589. [BEND_IDX( 10)] = 0x3F23,
  6590. [BEND_IDX( 5)] = 0x3F23,
  6591. [BEND_IDX( 0)] = 0x0025,
  6592. [BEND_IDX( -5)] = 0x0025,
  6593. [BEND_IDX(-10)] = 0x0125,
  6594. [BEND_IDX(-15)] = 0x0125,
  6595. [BEND_IDX(-20)] = 0x0225,
  6596. [BEND_IDX(-25)] = 0x0225,
  6597. [BEND_IDX(-30)] = 0x0325,
  6598. [BEND_IDX(-35)] = 0x0325,
  6599. [BEND_IDX(-40)] = 0x0425,
  6600. [BEND_IDX(-45)] = 0x0425,
  6601. [BEND_IDX(-50)] = 0x0525,
  6602. };
  6603. /*
  6604. * Bend CLKOUT_DP
  6605. * steps -50 to 50 inclusive, in steps of 5
  6606. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6607. * change in clock period = -(steps / 10) * 5.787 ps
  6608. */
  6609. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6610. {
  6611. uint32_t tmp;
  6612. int idx = BEND_IDX(steps);
  6613. if (WARN_ON(steps % 5 != 0))
  6614. return;
  6615. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6616. return;
  6617. mutex_lock(&dev_priv->sb_lock);
  6618. if (steps % 10 != 0)
  6619. tmp = 0xAAAAAAAB;
  6620. else
  6621. tmp = 0x00000000;
  6622. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6623. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6624. tmp &= 0xffff0000;
  6625. tmp |= sscdivintphase[idx];
  6626. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6627. mutex_unlock(&dev_priv->sb_lock);
  6628. }
  6629. #undef BEND_IDX
  6630. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6631. {
  6632. struct intel_encoder *encoder;
  6633. bool has_vga = false;
  6634. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6635. switch (encoder->type) {
  6636. case INTEL_OUTPUT_ANALOG:
  6637. has_vga = true;
  6638. break;
  6639. default:
  6640. break;
  6641. }
  6642. }
  6643. if (has_vga) {
  6644. lpt_bend_clkout_dp(dev_priv, 0);
  6645. lpt_enable_clkout_dp(dev_priv, true, true);
  6646. } else {
  6647. lpt_disable_clkout_dp(dev_priv);
  6648. }
  6649. }
  6650. /*
  6651. * Initialize reference clocks when the driver loads
  6652. */
  6653. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6654. {
  6655. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6656. ironlake_init_pch_refclk(dev_priv);
  6657. else if (HAS_PCH_LPT(dev_priv))
  6658. lpt_init_pch_refclk(dev_priv);
  6659. }
  6660. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6661. {
  6662. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6664. int pipe = intel_crtc->pipe;
  6665. uint32_t val;
  6666. val = 0;
  6667. switch (intel_crtc->config->pipe_bpp) {
  6668. case 18:
  6669. val |= PIPECONF_6BPC;
  6670. break;
  6671. case 24:
  6672. val |= PIPECONF_8BPC;
  6673. break;
  6674. case 30:
  6675. val |= PIPECONF_10BPC;
  6676. break;
  6677. case 36:
  6678. val |= PIPECONF_12BPC;
  6679. break;
  6680. default:
  6681. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6682. BUG();
  6683. }
  6684. if (intel_crtc->config->dither)
  6685. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6686. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6687. val |= PIPECONF_INTERLACED_ILK;
  6688. else
  6689. val |= PIPECONF_PROGRESSIVE;
  6690. if (intel_crtc->config->limited_color_range)
  6691. val |= PIPECONF_COLOR_RANGE_SELECT;
  6692. I915_WRITE(PIPECONF(pipe), val);
  6693. POSTING_READ(PIPECONF(pipe));
  6694. }
  6695. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6696. {
  6697. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6698. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6699. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6700. u32 val = 0;
  6701. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6702. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6703. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6704. val |= PIPECONF_INTERLACED_ILK;
  6705. else
  6706. val |= PIPECONF_PROGRESSIVE;
  6707. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6708. POSTING_READ(PIPECONF(cpu_transcoder));
  6709. }
  6710. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6711. {
  6712. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6713. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6714. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6715. u32 val = 0;
  6716. switch (intel_crtc->config->pipe_bpp) {
  6717. case 18:
  6718. val |= PIPEMISC_DITHER_6_BPC;
  6719. break;
  6720. case 24:
  6721. val |= PIPEMISC_DITHER_8_BPC;
  6722. break;
  6723. case 30:
  6724. val |= PIPEMISC_DITHER_10_BPC;
  6725. break;
  6726. case 36:
  6727. val |= PIPEMISC_DITHER_12_BPC;
  6728. break;
  6729. default:
  6730. /* Case prevented by pipe_config_set_bpp. */
  6731. BUG();
  6732. }
  6733. if (intel_crtc->config->dither)
  6734. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6735. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6736. }
  6737. }
  6738. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6739. {
  6740. /*
  6741. * Account for spread spectrum to avoid
  6742. * oversubscribing the link. Max center spread
  6743. * is 2.5%; use 5% for safety's sake.
  6744. */
  6745. u32 bps = target_clock * bpp * 21 / 20;
  6746. return DIV_ROUND_UP(bps, link_bw * 8);
  6747. }
  6748. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6749. {
  6750. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6751. }
  6752. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6753. struct intel_crtc_state *crtc_state,
  6754. struct dpll *reduced_clock)
  6755. {
  6756. struct drm_crtc *crtc = &intel_crtc->base;
  6757. struct drm_device *dev = crtc->dev;
  6758. struct drm_i915_private *dev_priv = to_i915(dev);
  6759. u32 dpll, fp, fp2;
  6760. int factor;
  6761. /* Enable autotuning of the PLL clock (if permissible) */
  6762. factor = 21;
  6763. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6764. if ((intel_panel_use_ssc(dev_priv) &&
  6765. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6766. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6767. factor = 25;
  6768. } else if (crtc_state->sdvo_tv_clock)
  6769. factor = 20;
  6770. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6771. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6772. fp |= FP_CB_TUNE;
  6773. if (reduced_clock) {
  6774. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6775. if (reduced_clock->m < factor * reduced_clock->n)
  6776. fp2 |= FP_CB_TUNE;
  6777. } else {
  6778. fp2 = fp;
  6779. }
  6780. dpll = 0;
  6781. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6782. dpll |= DPLLB_MODE_LVDS;
  6783. else
  6784. dpll |= DPLLB_MODE_DAC_SERIAL;
  6785. dpll |= (crtc_state->pixel_multiplier - 1)
  6786. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6787. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6788. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6789. dpll |= DPLL_SDVO_HIGH_SPEED;
  6790. if (intel_crtc_has_dp_encoder(crtc_state))
  6791. dpll |= DPLL_SDVO_HIGH_SPEED;
  6792. /*
  6793. * The high speed IO clock is only really required for
  6794. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6795. * possible to share the DPLL between CRT and HDMI. Enabling
  6796. * the clock needlessly does no real harm, except use up a
  6797. * bit of power potentially.
  6798. *
  6799. * We'll limit this to IVB with 3 pipes, since it has only two
  6800. * DPLLs and so DPLL sharing is the only way to get three pipes
  6801. * driving PCH ports at the same time. On SNB we could do this,
  6802. * and potentially avoid enabling the second DPLL, but it's not
  6803. * clear if it''s a win or loss power wise. No point in doing
  6804. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6805. */
  6806. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6807. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6808. dpll |= DPLL_SDVO_HIGH_SPEED;
  6809. /* compute bitmask from p1 value */
  6810. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6811. /* also FPA1 */
  6812. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6813. switch (crtc_state->dpll.p2) {
  6814. case 5:
  6815. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6816. break;
  6817. case 7:
  6818. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6819. break;
  6820. case 10:
  6821. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6822. break;
  6823. case 14:
  6824. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6825. break;
  6826. }
  6827. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6828. intel_panel_use_ssc(dev_priv))
  6829. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6830. else
  6831. dpll |= PLL_REF_INPUT_DREFCLK;
  6832. dpll |= DPLL_VCO_ENABLE;
  6833. crtc_state->dpll_hw_state.dpll = dpll;
  6834. crtc_state->dpll_hw_state.fp0 = fp;
  6835. crtc_state->dpll_hw_state.fp1 = fp2;
  6836. }
  6837. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6838. struct intel_crtc_state *crtc_state)
  6839. {
  6840. struct drm_device *dev = crtc->base.dev;
  6841. struct drm_i915_private *dev_priv = to_i915(dev);
  6842. struct dpll reduced_clock;
  6843. bool has_reduced_clock = false;
  6844. struct intel_shared_dpll *pll;
  6845. const struct intel_limit *limit;
  6846. int refclk = 120000;
  6847. memset(&crtc_state->dpll_hw_state, 0,
  6848. sizeof(crtc_state->dpll_hw_state));
  6849. crtc->lowfreq_avail = false;
  6850. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6851. if (!crtc_state->has_pch_encoder)
  6852. return 0;
  6853. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6854. if (intel_panel_use_ssc(dev_priv)) {
  6855. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6856. dev_priv->vbt.lvds_ssc_freq);
  6857. refclk = dev_priv->vbt.lvds_ssc_freq;
  6858. }
  6859. if (intel_is_dual_link_lvds(dev)) {
  6860. if (refclk == 100000)
  6861. limit = &intel_limits_ironlake_dual_lvds_100m;
  6862. else
  6863. limit = &intel_limits_ironlake_dual_lvds;
  6864. } else {
  6865. if (refclk == 100000)
  6866. limit = &intel_limits_ironlake_single_lvds_100m;
  6867. else
  6868. limit = &intel_limits_ironlake_single_lvds;
  6869. }
  6870. } else {
  6871. limit = &intel_limits_ironlake_dac;
  6872. }
  6873. if (!crtc_state->clock_set &&
  6874. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6875. refclk, NULL, &crtc_state->dpll)) {
  6876. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6877. return -EINVAL;
  6878. }
  6879. ironlake_compute_dpll(crtc, crtc_state,
  6880. has_reduced_clock ? &reduced_clock : NULL);
  6881. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  6882. if (pll == NULL) {
  6883. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6884. pipe_name(crtc->pipe));
  6885. return -EINVAL;
  6886. }
  6887. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6888. has_reduced_clock)
  6889. crtc->lowfreq_avail = true;
  6890. return 0;
  6891. }
  6892. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6893. struct intel_link_m_n *m_n)
  6894. {
  6895. struct drm_device *dev = crtc->base.dev;
  6896. struct drm_i915_private *dev_priv = to_i915(dev);
  6897. enum pipe pipe = crtc->pipe;
  6898. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6899. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6900. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6901. & ~TU_SIZE_MASK;
  6902. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6903. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6904. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6905. }
  6906. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6907. enum transcoder transcoder,
  6908. struct intel_link_m_n *m_n,
  6909. struct intel_link_m_n *m2_n2)
  6910. {
  6911. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6912. enum pipe pipe = crtc->pipe;
  6913. if (INTEL_GEN(dev_priv) >= 5) {
  6914. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6915. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6916. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6917. & ~TU_SIZE_MASK;
  6918. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6919. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6920. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6921. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6922. * gen < 8) and if DRRS is supported (to make sure the
  6923. * registers are not unnecessarily read).
  6924. */
  6925. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6926. crtc->config->has_drrs) {
  6927. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6928. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6929. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6930. & ~TU_SIZE_MASK;
  6931. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6932. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6933. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6934. }
  6935. } else {
  6936. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6937. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6938. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6939. & ~TU_SIZE_MASK;
  6940. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6941. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6942. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6943. }
  6944. }
  6945. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6946. struct intel_crtc_state *pipe_config)
  6947. {
  6948. if (pipe_config->has_pch_encoder)
  6949. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6950. else
  6951. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6952. &pipe_config->dp_m_n,
  6953. &pipe_config->dp_m2_n2);
  6954. }
  6955. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6956. struct intel_crtc_state *pipe_config)
  6957. {
  6958. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6959. &pipe_config->fdi_m_n, NULL);
  6960. }
  6961. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6962. struct intel_crtc_state *pipe_config)
  6963. {
  6964. struct drm_device *dev = crtc->base.dev;
  6965. struct drm_i915_private *dev_priv = to_i915(dev);
  6966. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6967. uint32_t ps_ctrl = 0;
  6968. int id = -1;
  6969. int i;
  6970. /* find scaler attached to this pipe */
  6971. for (i = 0; i < crtc->num_scalers; i++) {
  6972. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6973. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6974. id = i;
  6975. pipe_config->pch_pfit.enabled = true;
  6976. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6977. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  6978. break;
  6979. }
  6980. }
  6981. scaler_state->scaler_id = id;
  6982. if (id >= 0) {
  6983. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  6984. } else {
  6985. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  6986. }
  6987. }
  6988. static void
  6989. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6990. struct intel_initial_plane_config *plane_config)
  6991. {
  6992. struct drm_device *dev = crtc->base.dev;
  6993. struct drm_i915_private *dev_priv = to_i915(dev);
  6994. u32 val, base, offset, stride_mult, tiling;
  6995. int pipe = crtc->pipe;
  6996. int fourcc, pixel_format;
  6997. unsigned int aligned_height;
  6998. struct drm_framebuffer *fb;
  6999. struct intel_framebuffer *intel_fb;
  7000. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7001. if (!intel_fb) {
  7002. DRM_DEBUG_KMS("failed to alloc fb\n");
  7003. return;
  7004. }
  7005. fb = &intel_fb->base;
  7006. fb->dev = dev;
  7007. val = I915_READ(PLANE_CTL(pipe, 0));
  7008. if (!(val & PLANE_CTL_ENABLE))
  7009. goto error;
  7010. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7011. fourcc = skl_format_to_fourcc(pixel_format,
  7012. val & PLANE_CTL_ORDER_RGBX,
  7013. val & PLANE_CTL_ALPHA_MASK);
  7014. fb->format = drm_format_info(fourcc);
  7015. tiling = val & PLANE_CTL_TILED_MASK;
  7016. switch (tiling) {
  7017. case PLANE_CTL_TILED_LINEAR:
  7018. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7019. break;
  7020. case PLANE_CTL_TILED_X:
  7021. plane_config->tiling = I915_TILING_X;
  7022. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7023. break;
  7024. case PLANE_CTL_TILED_Y:
  7025. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7026. break;
  7027. case PLANE_CTL_TILED_YF:
  7028. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7029. break;
  7030. default:
  7031. MISSING_CASE(tiling);
  7032. goto error;
  7033. }
  7034. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7035. plane_config->base = base;
  7036. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7037. val = I915_READ(PLANE_SIZE(pipe, 0));
  7038. fb->height = ((val >> 16) & 0xfff) + 1;
  7039. fb->width = ((val >> 0) & 0x1fff) + 1;
  7040. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7041. stride_mult = intel_fb_stride_alignment(fb, 0);
  7042. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7043. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7044. plane_config->size = fb->pitches[0] * aligned_height;
  7045. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7046. pipe_name(pipe), fb->width, fb->height,
  7047. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7048. plane_config->size);
  7049. plane_config->fb = intel_fb;
  7050. return;
  7051. error:
  7052. kfree(intel_fb);
  7053. }
  7054. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7055. struct intel_crtc_state *pipe_config)
  7056. {
  7057. struct drm_device *dev = crtc->base.dev;
  7058. struct drm_i915_private *dev_priv = to_i915(dev);
  7059. uint32_t tmp;
  7060. tmp = I915_READ(PF_CTL(crtc->pipe));
  7061. if (tmp & PF_ENABLE) {
  7062. pipe_config->pch_pfit.enabled = true;
  7063. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7064. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7065. /* We currently do not free assignements of panel fitters on
  7066. * ivb/hsw (since we don't use the higher upscaling modes which
  7067. * differentiates them) so just WARN about this case for now. */
  7068. if (IS_GEN7(dev_priv)) {
  7069. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7070. PF_PIPE_SEL_IVB(crtc->pipe));
  7071. }
  7072. }
  7073. }
  7074. static void
  7075. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7076. struct intel_initial_plane_config *plane_config)
  7077. {
  7078. struct drm_device *dev = crtc->base.dev;
  7079. struct drm_i915_private *dev_priv = to_i915(dev);
  7080. u32 val, base, offset;
  7081. int pipe = crtc->pipe;
  7082. int fourcc, pixel_format;
  7083. unsigned int aligned_height;
  7084. struct drm_framebuffer *fb;
  7085. struct intel_framebuffer *intel_fb;
  7086. val = I915_READ(DSPCNTR(pipe));
  7087. if (!(val & DISPLAY_PLANE_ENABLE))
  7088. return;
  7089. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7090. if (!intel_fb) {
  7091. DRM_DEBUG_KMS("failed to alloc fb\n");
  7092. return;
  7093. }
  7094. fb = &intel_fb->base;
  7095. fb->dev = dev;
  7096. if (INTEL_GEN(dev_priv) >= 4) {
  7097. if (val & DISPPLANE_TILED) {
  7098. plane_config->tiling = I915_TILING_X;
  7099. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7100. }
  7101. }
  7102. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7103. fourcc = i9xx_format_to_fourcc(pixel_format);
  7104. fb->format = drm_format_info(fourcc);
  7105. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7106. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7107. offset = I915_READ(DSPOFFSET(pipe));
  7108. } else {
  7109. if (plane_config->tiling)
  7110. offset = I915_READ(DSPTILEOFF(pipe));
  7111. else
  7112. offset = I915_READ(DSPLINOFF(pipe));
  7113. }
  7114. plane_config->base = base;
  7115. val = I915_READ(PIPESRC(pipe));
  7116. fb->width = ((val >> 16) & 0xfff) + 1;
  7117. fb->height = ((val >> 0) & 0xfff) + 1;
  7118. val = I915_READ(DSPSTRIDE(pipe));
  7119. fb->pitches[0] = val & 0xffffffc0;
  7120. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7121. plane_config->size = fb->pitches[0] * aligned_height;
  7122. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7123. pipe_name(pipe), fb->width, fb->height,
  7124. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7125. plane_config->size);
  7126. plane_config->fb = intel_fb;
  7127. }
  7128. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7129. struct intel_crtc_state *pipe_config)
  7130. {
  7131. struct drm_device *dev = crtc->base.dev;
  7132. struct drm_i915_private *dev_priv = to_i915(dev);
  7133. enum intel_display_power_domain power_domain;
  7134. uint32_t tmp;
  7135. bool ret;
  7136. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7137. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7138. return false;
  7139. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7140. pipe_config->shared_dpll = NULL;
  7141. ret = false;
  7142. tmp = I915_READ(PIPECONF(crtc->pipe));
  7143. if (!(tmp & PIPECONF_ENABLE))
  7144. goto out;
  7145. switch (tmp & PIPECONF_BPC_MASK) {
  7146. case PIPECONF_6BPC:
  7147. pipe_config->pipe_bpp = 18;
  7148. break;
  7149. case PIPECONF_8BPC:
  7150. pipe_config->pipe_bpp = 24;
  7151. break;
  7152. case PIPECONF_10BPC:
  7153. pipe_config->pipe_bpp = 30;
  7154. break;
  7155. case PIPECONF_12BPC:
  7156. pipe_config->pipe_bpp = 36;
  7157. break;
  7158. default:
  7159. break;
  7160. }
  7161. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7162. pipe_config->limited_color_range = true;
  7163. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7164. struct intel_shared_dpll *pll;
  7165. enum intel_dpll_id pll_id;
  7166. pipe_config->has_pch_encoder = true;
  7167. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7168. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7169. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7170. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7171. if (HAS_PCH_IBX(dev_priv)) {
  7172. /*
  7173. * The pipe->pch transcoder and pch transcoder->pll
  7174. * mapping is fixed.
  7175. */
  7176. pll_id = (enum intel_dpll_id) crtc->pipe;
  7177. } else {
  7178. tmp = I915_READ(PCH_DPLL_SEL);
  7179. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7180. pll_id = DPLL_ID_PCH_PLL_B;
  7181. else
  7182. pll_id= DPLL_ID_PCH_PLL_A;
  7183. }
  7184. pipe_config->shared_dpll =
  7185. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7186. pll = pipe_config->shared_dpll;
  7187. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7188. &pipe_config->dpll_hw_state));
  7189. tmp = pipe_config->dpll_hw_state.dpll;
  7190. pipe_config->pixel_multiplier =
  7191. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7192. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7193. ironlake_pch_clock_get(crtc, pipe_config);
  7194. } else {
  7195. pipe_config->pixel_multiplier = 1;
  7196. }
  7197. intel_get_pipe_timings(crtc, pipe_config);
  7198. intel_get_pipe_src_size(crtc, pipe_config);
  7199. ironlake_get_pfit_config(crtc, pipe_config);
  7200. ret = true;
  7201. out:
  7202. intel_display_power_put(dev_priv, power_domain);
  7203. return ret;
  7204. }
  7205. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7206. {
  7207. struct drm_device *dev = &dev_priv->drm;
  7208. struct intel_crtc *crtc;
  7209. for_each_intel_crtc(dev, crtc)
  7210. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7211. pipe_name(crtc->pipe));
  7212. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7213. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7214. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7215. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7216. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7217. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7218. "CPU PWM1 enabled\n");
  7219. if (IS_HASWELL(dev_priv))
  7220. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7221. "CPU PWM2 enabled\n");
  7222. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7223. "PCH PWM1 enabled\n");
  7224. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7225. "Utility pin enabled\n");
  7226. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7227. /*
  7228. * In theory we can still leave IRQs enabled, as long as only the HPD
  7229. * interrupts remain enabled. We used to check for that, but since it's
  7230. * gen-specific and since we only disable LCPLL after we fully disable
  7231. * the interrupts, the check below should be enough.
  7232. */
  7233. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7234. }
  7235. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7236. {
  7237. if (IS_HASWELL(dev_priv))
  7238. return I915_READ(D_COMP_HSW);
  7239. else
  7240. return I915_READ(D_COMP_BDW);
  7241. }
  7242. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7243. {
  7244. if (IS_HASWELL(dev_priv)) {
  7245. mutex_lock(&dev_priv->rps.hw_lock);
  7246. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7247. val))
  7248. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7249. mutex_unlock(&dev_priv->rps.hw_lock);
  7250. } else {
  7251. I915_WRITE(D_COMP_BDW, val);
  7252. POSTING_READ(D_COMP_BDW);
  7253. }
  7254. }
  7255. /*
  7256. * This function implements pieces of two sequences from BSpec:
  7257. * - Sequence for display software to disable LCPLL
  7258. * - Sequence for display software to allow package C8+
  7259. * The steps implemented here are just the steps that actually touch the LCPLL
  7260. * register. Callers should take care of disabling all the display engine
  7261. * functions, doing the mode unset, fixing interrupts, etc.
  7262. */
  7263. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7264. bool switch_to_fclk, bool allow_power_down)
  7265. {
  7266. uint32_t val;
  7267. assert_can_disable_lcpll(dev_priv);
  7268. val = I915_READ(LCPLL_CTL);
  7269. if (switch_to_fclk) {
  7270. val |= LCPLL_CD_SOURCE_FCLK;
  7271. I915_WRITE(LCPLL_CTL, val);
  7272. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7273. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7274. DRM_ERROR("Switching to FCLK failed\n");
  7275. val = I915_READ(LCPLL_CTL);
  7276. }
  7277. val |= LCPLL_PLL_DISABLE;
  7278. I915_WRITE(LCPLL_CTL, val);
  7279. POSTING_READ(LCPLL_CTL);
  7280. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7281. DRM_ERROR("LCPLL still locked\n");
  7282. val = hsw_read_dcomp(dev_priv);
  7283. val |= D_COMP_COMP_DISABLE;
  7284. hsw_write_dcomp(dev_priv, val);
  7285. ndelay(100);
  7286. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7287. 1))
  7288. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7289. if (allow_power_down) {
  7290. val = I915_READ(LCPLL_CTL);
  7291. val |= LCPLL_POWER_DOWN_ALLOW;
  7292. I915_WRITE(LCPLL_CTL, val);
  7293. POSTING_READ(LCPLL_CTL);
  7294. }
  7295. }
  7296. /*
  7297. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7298. * source.
  7299. */
  7300. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7301. {
  7302. uint32_t val;
  7303. val = I915_READ(LCPLL_CTL);
  7304. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7305. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7306. return;
  7307. /*
  7308. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7309. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7310. */
  7311. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7312. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7313. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7314. I915_WRITE(LCPLL_CTL, val);
  7315. POSTING_READ(LCPLL_CTL);
  7316. }
  7317. val = hsw_read_dcomp(dev_priv);
  7318. val |= D_COMP_COMP_FORCE;
  7319. val &= ~D_COMP_COMP_DISABLE;
  7320. hsw_write_dcomp(dev_priv, val);
  7321. val = I915_READ(LCPLL_CTL);
  7322. val &= ~LCPLL_PLL_DISABLE;
  7323. I915_WRITE(LCPLL_CTL, val);
  7324. if (intel_wait_for_register(dev_priv,
  7325. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7326. 5))
  7327. DRM_ERROR("LCPLL not locked yet\n");
  7328. if (val & LCPLL_CD_SOURCE_FCLK) {
  7329. val = I915_READ(LCPLL_CTL);
  7330. val &= ~LCPLL_CD_SOURCE_FCLK;
  7331. I915_WRITE(LCPLL_CTL, val);
  7332. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7333. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7334. DRM_ERROR("Switching back to LCPLL failed\n");
  7335. }
  7336. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7337. intel_update_cdclk(dev_priv);
  7338. }
  7339. /*
  7340. * Package states C8 and deeper are really deep PC states that can only be
  7341. * reached when all the devices on the system allow it, so even if the graphics
  7342. * device allows PC8+, it doesn't mean the system will actually get to these
  7343. * states. Our driver only allows PC8+ when going into runtime PM.
  7344. *
  7345. * The requirements for PC8+ are that all the outputs are disabled, the power
  7346. * well is disabled and most interrupts are disabled, and these are also
  7347. * requirements for runtime PM. When these conditions are met, we manually do
  7348. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7349. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7350. * hang the machine.
  7351. *
  7352. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7353. * the state of some registers, so when we come back from PC8+ we need to
  7354. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7355. * need to take care of the registers kept by RC6. Notice that this happens even
  7356. * if we don't put the device in PCI D3 state (which is what currently happens
  7357. * because of the runtime PM support).
  7358. *
  7359. * For more, read "Display Sequences for Package C8" on the hardware
  7360. * documentation.
  7361. */
  7362. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7363. {
  7364. uint32_t val;
  7365. DRM_DEBUG_KMS("Enabling package C8+\n");
  7366. if (HAS_PCH_LPT_LP(dev_priv)) {
  7367. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7368. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7369. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7370. }
  7371. lpt_disable_clkout_dp(dev_priv);
  7372. hsw_disable_lcpll(dev_priv, true, true);
  7373. }
  7374. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7375. {
  7376. uint32_t val;
  7377. DRM_DEBUG_KMS("Disabling package C8+\n");
  7378. hsw_restore_lcpll(dev_priv);
  7379. lpt_init_pch_refclk(dev_priv);
  7380. if (HAS_PCH_LPT_LP(dev_priv)) {
  7381. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7382. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7383. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7384. }
  7385. }
  7386. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7387. struct intel_crtc_state *crtc_state)
  7388. {
  7389. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7390. struct intel_encoder *encoder =
  7391. intel_ddi_get_crtc_new_encoder(crtc_state);
  7392. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7393. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7394. pipe_name(crtc->pipe));
  7395. return -EINVAL;
  7396. }
  7397. }
  7398. crtc->lowfreq_avail = false;
  7399. return 0;
  7400. }
  7401. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7402. enum port port,
  7403. struct intel_crtc_state *pipe_config)
  7404. {
  7405. enum intel_dpll_id id;
  7406. switch (port) {
  7407. case PORT_A:
  7408. id = DPLL_ID_SKL_DPLL0;
  7409. break;
  7410. case PORT_B:
  7411. id = DPLL_ID_SKL_DPLL1;
  7412. break;
  7413. case PORT_C:
  7414. id = DPLL_ID_SKL_DPLL2;
  7415. break;
  7416. default:
  7417. DRM_ERROR("Incorrect port type\n");
  7418. return;
  7419. }
  7420. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7421. }
  7422. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7423. enum port port,
  7424. struct intel_crtc_state *pipe_config)
  7425. {
  7426. enum intel_dpll_id id;
  7427. u32 temp;
  7428. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7429. id = temp >> (port * 3 + 1);
  7430. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7431. return;
  7432. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7433. }
  7434. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7435. enum port port,
  7436. struct intel_crtc_state *pipe_config)
  7437. {
  7438. enum intel_dpll_id id;
  7439. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7440. switch (ddi_pll_sel) {
  7441. case PORT_CLK_SEL_WRPLL1:
  7442. id = DPLL_ID_WRPLL1;
  7443. break;
  7444. case PORT_CLK_SEL_WRPLL2:
  7445. id = DPLL_ID_WRPLL2;
  7446. break;
  7447. case PORT_CLK_SEL_SPLL:
  7448. id = DPLL_ID_SPLL;
  7449. break;
  7450. case PORT_CLK_SEL_LCPLL_810:
  7451. id = DPLL_ID_LCPLL_810;
  7452. break;
  7453. case PORT_CLK_SEL_LCPLL_1350:
  7454. id = DPLL_ID_LCPLL_1350;
  7455. break;
  7456. case PORT_CLK_SEL_LCPLL_2700:
  7457. id = DPLL_ID_LCPLL_2700;
  7458. break;
  7459. default:
  7460. MISSING_CASE(ddi_pll_sel);
  7461. /* fall through */
  7462. case PORT_CLK_SEL_NONE:
  7463. return;
  7464. }
  7465. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7466. }
  7467. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7468. struct intel_crtc_state *pipe_config,
  7469. u64 *power_domain_mask)
  7470. {
  7471. struct drm_device *dev = crtc->base.dev;
  7472. struct drm_i915_private *dev_priv = to_i915(dev);
  7473. enum intel_display_power_domain power_domain;
  7474. u32 tmp;
  7475. /*
  7476. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7477. * transcoder handled below.
  7478. */
  7479. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7480. /*
  7481. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7482. * consistency and less surprising code; it's in always on power).
  7483. */
  7484. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7485. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7486. enum pipe trans_edp_pipe;
  7487. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7488. default:
  7489. WARN(1, "unknown pipe linked to edp transcoder\n");
  7490. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7491. case TRANS_DDI_EDP_INPUT_A_ON:
  7492. trans_edp_pipe = PIPE_A;
  7493. break;
  7494. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7495. trans_edp_pipe = PIPE_B;
  7496. break;
  7497. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7498. trans_edp_pipe = PIPE_C;
  7499. break;
  7500. }
  7501. if (trans_edp_pipe == crtc->pipe)
  7502. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7503. }
  7504. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7505. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7506. return false;
  7507. *power_domain_mask |= BIT_ULL(power_domain);
  7508. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7509. return tmp & PIPECONF_ENABLE;
  7510. }
  7511. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7512. struct intel_crtc_state *pipe_config,
  7513. u64 *power_domain_mask)
  7514. {
  7515. struct drm_device *dev = crtc->base.dev;
  7516. struct drm_i915_private *dev_priv = to_i915(dev);
  7517. enum intel_display_power_domain power_domain;
  7518. enum port port;
  7519. enum transcoder cpu_transcoder;
  7520. u32 tmp;
  7521. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7522. if (port == PORT_A)
  7523. cpu_transcoder = TRANSCODER_DSI_A;
  7524. else
  7525. cpu_transcoder = TRANSCODER_DSI_C;
  7526. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7527. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7528. continue;
  7529. *power_domain_mask |= BIT_ULL(power_domain);
  7530. /*
  7531. * The PLL needs to be enabled with a valid divider
  7532. * configuration, otherwise accessing DSI registers will hang
  7533. * the machine. See BSpec North Display Engine
  7534. * registers/MIPI[BXT]. We can break out here early, since we
  7535. * need the same DSI PLL to be enabled for both DSI ports.
  7536. */
  7537. if (!intel_dsi_pll_is_enabled(dev_priv))
  7538. break;
  7539. /* XXX: this works for video mode only */
  7540. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7541. if (!(tmp & DPI_ENABLE))
  7542. continue;
  7543. tmp = I915_READ(MIPI_CTRL(port));
  7544. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7545. continue;
  7546. pipe_config->cpu_transcoder = cpu_transcoder;
  7547. break;
  7548. }
  7549. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7550. }
  7551. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7552. struct intel_crtc_state *pipe_config)
  7553. {
  7554. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7555. struct intel_shared_dpll *pll;
  7556. enum port port;
  7557. uint32_t tmp;
  7558. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7559. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7560. if (IS_GEN9_BC(dev_priv))
  7561. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7562. else if (IS_GEN9_LP(dev_priv))
  7563. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7564. else
  7565. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7566. pll = pipe_config->shared_dpll;
  7567. if (pll) {
  7568. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7569. &pipe_config->dpll_hw_state));
  7570. }
  7571. /*
  7572. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7573. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7574. * the PCH transcoder is on.
  7575. */
  7576. if (INTEL_GEN(dev_priv) < 9 &&
  7577. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7578. pipe_config->has_pch_encoder = true;
  7579. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7580. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7581. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7582. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7583. }
  7584. }
  7585. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7586. struct intel_crtc_state *pipe_config)
  7587. {
  7588. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7589. enum intel_display_power_domain power_domain;
  7590. u64 power_domain_mask;
  7591. bool active;
  7592. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7593. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7594. return false;
  7595. power_domain_mask = BIT_ULL(power_domain);
  7596. pipe_config->shared_dpll = NULL;
  7597. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7598. if (IS_GEN9_LP(dev_priv) &&
  7599. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7600. WARN_ON(active);
  7601. active = true;
  7602. }
  7603. if (!active)
  7604. goto out;
  7605. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7606. haswell_get_ddi_port_state(crtc, pipe_config);
  7607. intel_get_pipe_timings(crtc, pipe_config);
  7608. }
  7609. intel_get_pipe_src_size(crtc, pipe_config);
  7610. pipe_config->gamma_mode =
  7611. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7612. if (INTEL_GEN(dev_priv) >= 9) {
  7613. intel_crtc_init_scalers(crtc, pipe_config);
  7614. pipe_config->scaler_state.scaler_id = -1;
  7615. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7616. }
  7617. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7618. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7619. power_domain_mask |= BIT_ULL(power_domain);
  7620. if (INTEL_GEN(dev_priv) >= 9)
  7621. skylake_get_pfit_config(crtc, pipe_config);
  7622. else
  7623. ironlake_get_pfit_config(crtc, pipe_config);
  7624. }
  7625. if (IS_HASWELL(dev_priv))
  7626. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7627. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7628. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7629. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7630. pipe_config->pixel_multiplier =
  7631. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7632. } else {
  7633. pipe_config->pixel_multiplier = 1;
  7634. }
  7635. out:
  7636. for_each_power_domain(power_domain, power_domain_mask)
  7637. intel_display_power_put(dev_priv, power_domain);
  7638. return active;
  7639. }
  7640. static u32 intel_cursor_base(const struct intel_plane_state *plane_state)
  7641. {
  7642. struct drm_i915_private *dev_priv =
  7643. to_i915(plane_state->base.plane->dev);
  7644. const struct drm_framebuffer *fb = plane_state->base.fb;
  7645. const struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  7646. u32 base;
  7647. if (INTEL_INFO(dev_priv)->cursor_needs_physical)
  7648. base = obj->phys_handle->busaddr;
  7649. else
  7650. base = intel_plane_ggtt_offset(plane_state);
  7651. /* ILK+ do this automagically */
  7652. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7653. plane_state->base.rotation & DRM_ROTATE_180)
  7654. base += (plane_state->base.crtc_h *
  7655. plane_state->base.crtc_w - 1) * fb->format->cpp[0];
  7656. return base;
  7657. }
  7658. static u32 intel_cursor_position(const struct intel_plane_state *plane_state)
  7659. {
  7660. int x = plane_state->base.crtc_x;
  7661. int y = plane_state->base.crtc_y;
  7662. u32 pos = 0;
  7663. if (x < 0) {
  7664. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7665. x = -x;
  7666. }
  7667. pos |= x << CURSOR_X_SHIFT;
  7668. if (y < 0) {
  7669. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7670. y = -y;
  7671. }
  7672. pos |= y << CURSOR_Y_SHIFT;
  7673. return pos;
  7674. }
  7675. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7676. const struct intel_plane_state *plane_state)
  7677. {
  7678. unsigned int width = plane_state->base.crtc_w;
  7679. unsigned int stride = roundup_pow_of_two(width) * 4;
  7680. switch (stride) {
  7681. default:
  7682. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7683. width, stride);
  7684. stride = 256;
  7685. /* fallthrough */
  7686. case 256:
  7687. case 512:
  7688. case 1024:
  7689. case 2048:
  7690. break;
  7691. }
  7692. return CURSOR_ENABLE |
  7693. CURSOR_GAMMA_ENABLE |
  7694. CURSOR_FORMAT_ARGB |
  7695. CURSOR_STRIDE(stride);
  7696. }
  7697. static void i845_update_cursor(struct intel_plane *plane,
  7698. const struct intel_crtc_state *crtc_state,
  7699. const struct intel_plane_state *plane_state)
  7700. {
  7701. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7702. u32 cntl = 0, base = 0, pos = 0, size = 0;
  7703. unsigned long irqflags;
  7704. if (plane_state && plane_state->base.visible) {
  7705. unsigned int width = plane_state->base.crtc_w;
  7706. unsigned int height = plane_state->base.crtc_h;
  7707. cntl = plane_state->ctl;
  7708. size = (height << 12) | width;
  7709. base = intel_cursor_base(plane_state);
  7710. pos = intel_cursor_position(plane_state);
  7711. }
  7712. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7713. if (plane->cursor.cntl != 0 &&
  7714. (plane->cursor.base != base ||
  7715. plane->cursor.size != size ||
  7716. plane->cursor.cntl != cntl)) {
  7717. /* On these chipsets we can only modify the base/size/stride
  7718. * whilst the cursor is disabled.
  7719. */
  7720. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7721. plane->cursor.cntl = 0;
  7722. }
  7723. if (plane->cursor.base != base)
  7724. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7725. if (plane->cursor.size != size)
  7726. I915_WRITE_FW(CURSIZE, size);
  7727. if (cntl)
  7728. I915_WRITE_FW(CURPOS(PIPE_A), pos);
  7729. if (plane->cursor.cntl != cntl)
  7730. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7731. POSTING_READ_FW(CURCNTR(PIPE_A));
  7732. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7733. plane->cursor.cntl = cntl;
  7734. plane->cursor.base = base;
  7735. plane->cursor.size = size;
  7736. }
  7737. static void i845_disable_cursor(struct intel_plane *plane,
  7738. struct intel_crtc *crtc)
  7739. {
  7740. i845_update_cursor(plane, NULL, NULL);
  7741. }
  7742. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7743. const struct intel_plane_state *plane_state)
  7744. {
  7745. struct drm_i915_private *dev_priv =
  7746. to_i915(plane_state->base.plane->dev);
  7747. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7748. u32 cntl;
  7749. cntl = MCURSOR_GAMMA_ENABLE;
  7750. if (HAS_DDI(dev_priv))
  7751. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7752. cntl |= MCURSOR_PIPE_SELECT(crtc->pipe);
  7753. switch (plane_state->base.crtc_w) {
  7754. case 64:
  7755. cntl |= CURSOR_MODE_64_ARGB_AX;
  7756. break;
  7757. case 128:
  7758. cntl |= CURSOR_MODE_128_ARGB_AX;
  7759. break;
  7760. case 256:
  7761. cntl |= CURSOR_MODE_256_ARGB_AX;
  7762. break;
  7763. default:
  7764. MISSING_CASE(plane_state->base.crtc_w);
  7765. return 0;
  7766. }
  7767. if (plane_state->base.rotation & DRM_ROTATE_180)
  7768. cntl |= CURSOR_ROTATE_180;
  7769. return cntl;
  7770. }
  7771. static void i9xx_update_cursor(struct intel_plane *plane,
  7772. const struct intel_crtc_state *crtc_state,
  7773. const struct intel_plane_state *plane_state)
  7774. {
  7775. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  7776. enum pipe pipe = plane->pipe;
  7777. u32 cntl = 0, base = 0, pos = 0;
  7778. unsigned long irqflags;
  7779. if (plane_state && plane_state->base.visible) {
  7780. cntl = plane_state->ctl;
  7781. base = intel_cursor_base(plane_state);
  7782. pos = intel_cursor_position(plane_state);
  7783. }
  7784. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7785. if (plane->cursor.cntl != cntl)
  7786. I915_WRITE_FW(CURCNTR(pipe), cntl);
  7787. if (cntl)
  7788. I915_WRITE_FW(CURPOS(pipe), pos);
  7789. if (plane->cursor.cntl != cntl ||
  7790. plane->cursor.base != base)
  7791. I915_WRITE_FW(CURBASE(pipe), base);
  7792. POSTING_READ_FW(CURBASE(pipe));
  7793. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7794. plane->cursor.cntl = cntl;
  7795. plane->cursor.base = base;
  7796. }
  7797. static void i9xx_disable_cursor(struct intel_plane *plane,
  7798. struct intel_crtc *crtc)
  7799. {
  7800. i9xx_update_cursor(plane, NULL, NULL);
  7801. }
  7802. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  7803. uint32_t width, uint32_t height)
  7804. {
  7805. if (width == 0 || height == 0)
  7806. return false;
  7807. /*
  7808. * 845g/865g are special in that they are only limited by
  7809. * the width of their cursors, the height is arbitrary up to
  7810. * the precision of the register. Everything else requires
  7811. * square cursors, limited to a few power-of-two sizes.
  7812. */
  7813. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  7814. if ((width & 63) != 0)
  7815. return false;
  7816. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  7817. return false;
  7818. if (height > 1023)
  7819. return false;
  7820. } else {
  7821. switch (width | height) {
  7822. case 256:
  7823. case 128:
  7824. if (IS_GEN2(dev_priv))
  7825. return false;
  7826. case 64:
  7827. break;
  7828. default:
  7829. return false;
  7830. }
  7831. }
  7832. return true;
  7833. }
  7834. /* VESA 640x480x72Hz mode to set on the pipe */
  7835. static struct drm_display_mode load_detect_mode = {
  7836. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7837. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7838. };
  7839. struct drm_framebuffer *
  7840. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7841. struct drm_mode_fb_cmd2 *mode_cmd)
  7842. {
  7843. struct intel_framebuffer *intel_fb;
  7844. int ret;
  7845. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7846. if (!intel_fb)
  7847. return ERR_PTR(-ENOMEM);
  7848. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7849. if (ret)
  7850. goto err;
  7851. return &intel_fb->base;
  7852. err:
  7853. kfree(intel_fb);
  7854. return ERR_PTR(ret);
  7855. }
  7856. static u32
  7857. intel_framebuffer_pitch_for_width(int width, int bpp)
  7858. {
  7859. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7860. return ALIGN(pitch, 64);
  7861. }
  7862. static u32
  7863. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7864. {
  7865. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7866. return PAGE_ALIGN(pitch * mode->vdisplay);
  7867. }
  7868. static struct drm_framebuffer *
  7869. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7870. struct drm_display_mode *mode,
  7871. int depth, int bpp)
  7872. {
  7873. struct drm_framebuffer *fb;
  7874. struct drm_i915_gem_object *obj;
  7875. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7876. obj = i915_gem_object_create(to_i915(dev),
  7877. intel_framebuffer_size_for_mode(mode, bpp));
  7878. if (IS_ERR(obj))
  7879. return ERR_CAST(obj);
  7880. mode_cmd.width = mode->hdisplay;
  7881. mode_cmd.height = mode->vdisplay;
  7882. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7883. bpp);
  7884. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7885. fb = intel_framebuffer_create(obj, &mode_cmd);
  7886. if (IS_ERR(fb))
  7887. i915_gem_object_put(obj);
  7888. return fb;
  7889. }
  7890. static struct drm_framebuffer *
  7891. mode_fits_in_fbdev(struct drm_device *dev,
  7892. struct drm_display_mode *mode)
  7893. {
  7894. #ifdef CONFIG_DRM_FBDEV_EMULATION
  7895. struct drm_i915_private *dev_priv = to_i915(dev);
  7896. struct drm_i915_gem_object *obj;
  7897. struct drm_framebuffer *fb;
  7898. if (!dev_priv->fbdev)
  7899. return NULL;
  7900. if (!dev_priv->fbdev->fb)
  7901. return NULL;
  7902. obj = dev_priv->fbdev->fb->obj;
  7903. BUG_ON(!obj);
  7904. fb = &dev_priv->fbdev->fb->base;
  7905. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7906. fb->format->cpp[0] * 8))
  7907. return NULL;
  7908. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7909. return NULL;
  7910. drm_framebuffer_reference(fb);
  7911. return fb;
  7912. #else
  7913. return NULL;
  7914. #endif
  7915. }
  7916. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  7917. struct drm_crtc *crtc,
  7918. struct drm_display_mode *mode,
  7919. struct drm_framebuffer *fb,
  7920. int x, int y)
  7921. {
  7922. struct drm_plane_state *plane_state;
  7923. int hdisplay, vdisplay;
  7924. int ret;
  7925. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  7926. if (IS_ERR(plane_state))
  7927. return PTR_ERR(plane_state);
  7928. if (mode)
  7929. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  7930. else
  7931. hdisplay = vdisplay = 0;
  7932. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  7933. if (ret)
  7934. return ret;
  7935. drm_atomic_set_fb_for_plane(plane_state, fb);
  7936. plane_state->crtc_x = 0;
  7937. plane_state->crtc_y = 0;
  7938. plane_state->crtc_w = hdisplay;
  7939. plane_state->crtc_h = vdisplay;
  7940. plane_state->src_x = x << 16;
  7941. plane_state->src_y = y << 16;
  7942. plane_state->src_w = hdisplay << 16;
  7943. plane_state->src_h = vdisplay << 16;
  7944. return 0;
  7945. }
  7946. int intel_get_load_detect_pipe(struct drm_connector *connector,
  7947. struct drm_display_mode *mode,
  7948. struct intel_load_detect_pipe *old,
  7949. struct drm_modeset_acquire_ctx *ctx)
  7950. {
  7951. struct intel_crtc *intel_crtc;
  7952. struct intel_encoder *intel_encoder =
  7953. intel_attached_encoder(connector);
  7954. struct drm_crtc *possible_crtc;
  7955. struct drm_encoder *encoder = &intel_encoder->base;
  7956. struct drm_crtc *crtc = NULL;
  7957. struct drm_device *dev = encoder->dev;
  7958. struct drm_i915_private *dev_priv = to_i915(dev);
  7959. struct drm_framebuffer *fb;
  7960. struct drm_mode_config *config = &dev->mode_config;
  7961. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  7962. struct drm_connector_state *connector_state;
  7963. struct intel_crtc_state *crtc_state;
  7964. int ret, i = -1;
  7965. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7966. connector->base.id, connector->name,
  7967. encoder->base.id, encoder->name);
  7968. old->restore_state = NULL;
  7969. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  7970. /*
  7971. * Algorithm gets a little messy:
  7972. *
  7973. * - if the connector already has an assigned crtc, use it (but make
  7974. * sure it's on first)
  7975. *
  7976. * - try to find the first unused crtc that can drive this connector,
  7977. * and use that if we find one
  7978. */
  7979. /* See if we already have a CRTC for this connector */
  7980. if (connector->state->crtc) {
  7981. crtc = connector->state->crtc;
  7982. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7983. if (ret)
  7984. goto fail;
  7985. /* Make sure the crtc and connector are running */
  7986. goto found;
  7987. }
  7988. /* Find an unused one (if possible) */
  7989. for_each_crtc(dev, possible_crtc) {
  7990. i++;
  7991. if (!(encoder->possible_crtcs & (1 << i)))
  7992. continue;
  7993. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  7994. if (ret)
  7995. goto fail;
  7996. if (possible_crtc->state->enable) {
  7997. drm_modeset_unlock(&possible_crtc->mutex);
  7998. continue;
  7999. }
  8000. crtc = possible_crtc;
  8001. break;
  8002. }
  8003. /*
  8004. * If we didn't find an unused CRTC, don't use any.
  8005. */
  8006. if (!crtc) {
  8007. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8008. ret = -ENODEV;
  8009. goto fail;
  8010. }
  8011. found:
  8012. intel_crtc = to_intel_crtc(crtc);
  8013. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8014. if (ret)
  8015. goto fail;
  8016. state = drm_atomic_state_alloc(dev);
  8017. restore_state = drm_atomic_state_alloc(dev);
  8018. if (!state || !restore_state) {
  8019. ret = -ENOMEM;
  8020. goto fail;
  8021. }
  8022. state->acquire_ctx = ctx;
  8023. restore_state->acquire_ctx = ctx;
  8024. connector_state = drm_atomic_get_connector_state(state, connector);
  8025. if (IS_ERR(connector_state)) {
  8026. ret = PTR_ERR(connector_state);
  8027. goto fail;
  8028. }
  8029. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8030. if (ret)
  8031. goto fail;
  8032. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8033. if (IS_ERR(crtc_state)) {
  8034. ret = PTR_ERR(crtc_state);
  8035. goto fail;
  8036. }
  8037. crtc_state->base.active = crtc_state->base.enable = true;
  8038. if (!mode)
  8039. mode = &load_detect_mode;
  8040. /* We need a framebuffer large enough to accommodate all accesses
  8041. * that the plane may generate whilst we perform load detection.
  8042. * We can not rely on the fbcon either being present (we get called
  8043. * during its initialisation to detect all boot displays, or it may
  8044. * not even exist) or that it is large enough to satisfy the
  8045. * requested mode.
  8046. */
  8047. fb = mode_fits_in_fbdev(dev, mode);
  8048. if (fb == NULL) {
  8049. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8050. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8051. } else
  8052. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8053. if (IS_ERR(fb)) {
  8054. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8055. ret = PTR_ERR(fb);
  8056. goto fail;
  8057. }
  8058. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8059. if (ret)
  8060. goto fail;
  8061. drm_framebuffer_unreference(fb);
  8062. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8063. if (ret)
  8064. goto fail;
  8065. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8066. if (!ret)
  8067. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8068. if (!ret)
  8069. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8070. if (ret) {
  8071. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8072. goto fail;
  8073. }
  8074. ret = drm_atomic_commit(state);
  8075. if (ret) {
  8076. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8077. goto fail;
  8078. }
  8079. old->restore_state = restore_state;
  8080. drm_atomic_state_put(state);
  8081. /* let the connector get through one full cycle before testing */
  8082. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8083. return true;
  8084. fail:
  8085. if (state) {
  8086. drm_atomic_state_put(state);
  8087. state = NULL;
  8088. }
  8089. if (restore_state) {
  8090. drm_atomic_state_put(restore_state);
  8091. restore_state = NULL;
  8092. }
  8093. if (ret == -EDEADLK)
  8094. return ret;
  8095. return false;
  8096. }
  8097. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8098. struct intel_load_detect_pipe *old,
  8099. struct drm_modeset_acquire_ctx *ctx)
  8100. {
  8101. struct intel_encoder *intel_encoder =
  8102. intel_attached_encoder(connector);
  8103. struct drm_encoder *encoder = &intel_encoder->base;
  8104. struct drm_atomic_state *state = old->restore_state;
  8105. int ret;
  8106. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8107. connector->base.id, connector->name,
  8108. encoder->base.id, encoder->name);
  8109. if (!state)
  8110. return;
  8111. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8112. if (ret)
  8113. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8114. drm_atomic_state_put(state);
  8115. }
  8116. static int i9xx_pll_refclk(struct drm_device *dev,
  8117. const struct intel_crtc_state *pipe_config)
  8118. {
  8119. struct drm_i915_private *dev_priv = to_i915(dev);
  8120. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8121. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8122. return dev_priv->vbt.lvds_ssc_freq;
  8123. else if (HAS_PCH_SPLIT(dev_priv))
  8124. return 120000;
  8125. else if (!IS_GEN2(dev_priv))
  8126. return 96000;
  8127. else
  8128. return 48000;
  8129. }
  8130. /* Returns the clock of the currently programmed mode of the given pipe. */
  8131. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8132. struct intel_crtc_state *pipe_config)
  8133. {
  8134. struct drm_device *dev = crtc->base.dev;
  8135. struct drm_i915_private *dev_priv = to_i915(dev);
  8136. int pipe = pipe_config->cpu_transcoder;
  8137. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8138. u32 fp;
  8139. struct dpll clock;
  8140. int port_clock;
  8141. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8142. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8143. fp = pipe_config->dpll_hw_state.fp0;
  8144. else
  8145. fp = pipe_config->dpll_hw_state.fp1;
  8146. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8147. if (IS_PINEVIEW(dev_priv)) {
  8148. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8149. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8150. } else {
  8151. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8152. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8153. }
  8154. if (!IS_GEN2(dev_priv)) {
  8155. if (IS_PINEVIEW(dev_priv))
  8156. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8157. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8158. else
  8159. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8160. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8161. switch (dpll & DPLL_MODE_MASK) {
  8162. case DPLLB_MODE_DAC_SERIAL:
  8163. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8164. 5 : 10;
  8165. break;
  8166. case DPLLB_MODE_LVDS:
  8167. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8168. 7 : 14;
  8169. break;
  8170. default:
  8171. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8172. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8173. return;
  8174. }
  8175. if (IS_PINEVIEW(dev_priv))
  8176. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8177. else
  8178. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8179. } else {
  8180. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8181. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8182. if (is_lvds) {
  8183. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8184. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8185. if (lvds & LVDS_CLKB_POWER_UP)
  8186. clock.p2 = 7;
  8187. else
  8188. clock.p2 = 14;
  8189. } else {
  8190. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8191. clock.p1 = 2;
  8192. else {
  8193. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8194. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8195. }
  8196. if (dpll & PLL_P2_DIVIDE_BY_4)
  8197. clock.p2 = 4;
  8198. else
  8199. clock.p2 = 2;
  8200. }
  8201. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8202. }
  8203. /*
  8204. * This value includes pixel_multiplier. We will use
  8205. * port_clock to compute adjusted_mode.crtc_clock in the
  8206. * encoder's get_config() function.
  8207. */
  8208. pipe_config->port_clock = port_clock;
  8209. }
  8210. int intel_dotclock_calculate(int link_freq,
  8211. const struct intel_link_m_n *m_n)
  8212. {
  8213. /*
  8214. * The calculation for the data clock is:
  8215. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8216. * But we want to avoid losing precison if possible, so:
  8217. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8218. *
  8219. * and the link clock is simpler:
  8220. * link_clock = (m * link_clock) / n
  8221. */
  8222. if (!m_n->link_n)
  8223. return 0;
  8224. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8225. }
  8226. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8227. struct intel_crtc_state *pipe_config)
  8228. {
  8229. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8230. /* read out port_clock from the DPLL */
  8231. i9xx_crtc_clock_get(crtc, pipe_config);
  8232. /*
  8233. * In case there is an active pipe without active ports,
  8234. * we may need some idea for the dotclock anyway.
  8235. * Calculate one based on the FDI configuration.
  8236. */
  8237. pipe_config->base.adjusted_mode.crtc_clock =
  8238. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8239. &pipe_config->fdi_m_n);
  8240. }
  8241. /** Returns the currently programmed mode of the given pipe. */
  8242. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8243. struct drm_crtc *crtc)
  8244. {
  8245. struct drm_i915_private *dev_priv = to_i915(dev);
  8246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8247. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8248. struct drm_display_mode *mode;
  8249. struct intel_crtc_state *pipe_config;
  8250. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8251. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8252. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8253. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8254. enum pipe pipe = intel_crtc->pipe;
  8255. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8256. if (!mode)
  8257. return NULL;
  8258. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8259. if (!pipe_config) {
  8260. kfree(mode);
  8261. return NULL;
  8262. }
  8263. /*
  8264. * Construct a pipe_config sufficient for getting the clock info
  8265. * back out of crtc_clock_get.
  8266. *
  8267. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8268. * to use a real value here instead.
  8269. */
  8270. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8271. pipe_config->pixel_multiplier = 1;
  8272. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8273. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8274. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8275. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8276. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8277. mode->hdisplay = (htot & 0xffff) + 1;
  8278. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8279. mode->hsync_start = (hsync & 0xffff) + 1;
  8280. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8281. mode->vdisplay = (vtot & 0xffff) + 1;
  8282. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8283. mode->vsync_start = (vsync & 0xffff) + 1;
  8284. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8285. drm_mode_set_name(mode);
  8286. kfree(pipe_config);
  8287. return mode;
  8288. }
  8289. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8290. {
  8291. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8292. struct drm_device *dev = crtc->dev;
  8293. struct intel_flip_work *work;
  8294. spin_lock_irq(&dev->event_lock);
  8295. work = intel_crtc->flip_work;
  8296. intel_crtc->flip_work = NULL;
  8297. spin_unlock_irq(&dev->event_lock);
  8298. if (work) {
  8299. cancel_work_sync(&work->mmio_work);
  8300. cancel_work_sync(&work->unpin_work);
  8301. kfree(work);
  8302. }
  8303. drm_crtc_cleanup(crtc);
  8304. kfree(intel_crtc);
  8305. }
  8306. static void intel_unpin_work_fn(struct work_struct *__work)
  8307. {
  8308. struct intel_flip_work *work =
  8309. container_of(__work, struct intel_flip_work, unpin_work);
  8310. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8311. struct drm_device *dev = crtc->base.dev;
  8312. struct drm_plane *primary = crtc->base.primary;
  8313. if (is_mmio_work(work))
  8314. flush_work(&work->mmio_work);
  8315. mutex_lock(&dev->struct_mutex);
  8316. intel_unpin_fb_vma(work->old_vma);
  8317. i915_gem_object_put(work->pending_flip_obj);
  8318. mutex_unlock(&dev->struct_mutex);
  8319. i915_gem_request_put(work->flip_queued_req);
  8320. intel_frontbuffer_flip_complete(to_i915(dev),
  8321. to_intel_plane(primary)->frontbuffer_bit);
  8322. intel_fbc_post_update(crtc);
  8323. drm_framebuffer_unreference(work->old_fb);
  8324. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8325. atomic_dec(&crtc->unpin_work_count);
  8326. kfree(work);
  8327. }
  8328. /* Is 'a' after or equal to 'b'? */
  8329. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8330. {
  8331. return !((a - b) & 0x80000000);
  8332. }
  8333. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8334. struct intel_flip_work *work)
  8335. {
  8336. struct drm_device *dev = crtc->base.dev;
  8337. struct drm_i915_private *dev_priv = to_i915(dev);
  8338. if (abort_flip_on_reset(crtc))
  8339. return true;
  8340. /*
  8341. * The relevant registers doen't exist on pre-ctg.
  8342. * As the flip done interrupt doesn't trigger for mmio
  8343. * flips on gmch platforms, a flip count check isn't
  8344. * really needed there. But since ctg has the registers,
  8345. * include it in the check anyway.
  8346. */
  8347. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8348. return true;
  8349. /*
  8350. * BDW signals flip done immediately if the plane
  8351. * is disabled, even if the plane enable is already
  8352. * armed to occur at the next vblank :(
  8353. */
  8354. /*
  8355. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8356. * used the same base address. In that case the mmio flip might
  8357. * have completed, but the CS hasn't even executed the flip yet.
  8358. *
  8359. * A flip count check isn't enough as the CS might have updated
  8360. * the base address just after start of vblank, but before we
  8361. * managed to process the interrupt. This means we'd complete the
  8362. * CS flip too soon.
  8363. *
  8364. * Combining both checks should get us a good enough result. It may
  8365. * still happen that the CS flip has been executed, but has not
  8366. * yet actually completed. But in case the base address is the same
  8367. * anyway, we don't really care.
  8368. */
  8369. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8370. crtc->flip_work->gtt_offset &&
  8371. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8372. crtc->flip_work->flip_count);
  8373. }
  8374. static bool
  8375. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8376. struct intel_flip_work *work)
  8377. {
  8378. /*
  8379. * MMIO work completes when vblank is different from
  8380. * flip_queued_vblank.
  8381. *
  8382. * Reset counter value doesn't matter, this is handled by
  8383. * i915_wait_request finishing early, so no need to handle
  8384. * reset here.
  8385. */
  8386. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8387. }
  8388. static bool pageflip_finished(struct intel_crtc *crtc,
  8389. struct intel_flip_work *work)
  8390. {
  8391. if (!atomic_read(&work->pending))
  8392. return false;
  8393. smp_rmb();
  8394. if (is_mmio_work(work))
  8395. return __pageflip_finished_mmio(crtc, work);
  8396. else
  8397. return __pageflip_finished_cs(crtc, work);
  8398. }
  8399. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8400. {
  8401. struct drm_device *dev = &dev_priv->drm;
  8402. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8403. struct intel_flip_work *work;
  8404. unsigned long flags;
  8405. /* Ignore early vblank irqs */
  8406. if (!crtc)
  8407. return;
  8408. /*
  8409. * This is called both by irq handlers and the reset code (to complete
  8410. * lost pageflips) so needs the full irqsave spinlocks.
  8411. */
  8412. spin_lock_irqsave(&dev->event_lock, flags);
  8413. work = crtc->flip_work;
  8414. if (work != NULL &&
  8415. !is_mmio_work(work) &&
  8416. pageflip_finished(crtc, work))
  8417. page_flip_completed(crtc);
  8418. spin_unlock_irqrestore(&dev->event_lock, flags);
  8419. }
  8420. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8421. {
  8422. struct drm_device *dev = &dev_priv->drm;
  8423. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8424. struct intel_flip_work *work;
  8425. unsigned long flags;
  8426. /* Ignore early vblank irqs */
  8427. if (!crtc)
  8428. return;
  8429. /*
  8430. * This is called both by irq handlers and the reset code (to complete
  8431. * lost pageflips) so needs the full irqsave spinlocks.
  8432. */
  8433. spin_lock_irqsave(&dev->event_lock, flags);
  8434. work = crtc->flip_work;
  8435. if (work != NULL &&
  8436. is_mmio_work(work) &&
  8437. pageflip_finished(crtc, work))
  8438. page_flip_completed(crtc);
  8439. spin_unlock_irqrestore(&dev->event_lock, flags);
  8440. }
  8441. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8442. struct intel_flip_work *work)
  8443. {
  8444. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8445. /* Ensure that the work item is consistent when activating it ... */
  8446. smp_mb__before_atomic();
  8447. atomic_set(&work->pending, 1);
  8448. }
  8449. static int intel_gen2_queue_flip(struct drm_device *dev,
  8450. struct drm_crtc *crtc,
  8451. struct drm_framebuffer *fb,
  8452. struct drm_i915_gem_object *obj,
  8453. struct drm_i915_gem_request *req,
  8454. uint32_t flags)
  8455. {
  8456. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8457. u32 flip_mask, *cs;
  8458. cs = intel_ring_begin(req, 6);
  8459. if (IS_ERR(cs))
  8460. return PTR_ERR(cs);
  8461. /* Can't queue multiple flips, so wait for the previous
  8462. * one to finish before executing the next.
  8463. */
  8464. if (intel_crtc->plane)
  8465. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8466. else
  8467. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8468. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8469. *cs++ = MI_NOOP;
  8470. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8471. *cs++ = fb->pitches[0];
  8472. *cs++ = intel_crtc->flip_work->gtt_offset;
  8473. *cs++ = 0; /* aux display base address, unused */
  8474. return 0;
  8475. }
  8476. static int intel_gen3_queue_flip(struct drm_device *dev,
  8477. struct drm_crtc *crtc,
  8478. struct drm_framebuffer *fb,
  8479. struct drm_i915_gem_object *obj,
  8480. struct drm_i915_gem_request *req,
  8481. uint32_t flags)
  8482. {
  8483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8484. u32 flip_mask, *cs;
  8485. cs = intel_ring_begin(req, 6);
  8486. if (IS_ERR(cs))
  8487. return PTR_ERR(cs);
  8488. if (intel_crtc->plane)
  8489. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8490. else
  8491. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8492. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8493. *cs++ = MI_NOOP;
  8494. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8495. *cs++ = fb->pitches[0];
  8496. *cs++ = intel_crtc->flip_work->gtt_offset;
  8497. *cs++ = MI_NOOP;
  8498. return 0;
  8499. }
  8500. static int intel_gen4_queue_flip(struct drm_device *dev,
  8501. struct drm_crtc *crtc,
  8502. struct drm_framebuffer *fb,
  8503. struct drm_i915_gem_object *obj,
  8504. struct drm_i915_gem_request *req,
  8505. uint32_t flags)
  8506. {
  8507. struct drm_i915_private *dev_priv = to_i915(dev);
  8508. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8509. u32 pf, pipesrc, *cs;
  8510. cs = intel_ring_begin(req, 4);
  8511. if (IS_ERR(cs))
  8512. return PTR_ERR(cs);
  8513. /* i965+ uses the linear or tiled offsets from the
  8514. * Display Registers (which do not change across a page-flip)
  8515. * so we need only reprogram the base address.
  8516. */
  8517. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8518. *cs++ = fb->pitches[0];
  8519. *cs++ = intel_crtc->flip_work->gtt_offset |
  8520. intel_fb_modifier_to_tiling(fb->modifier);
  8521. /* XXX Enabling the panel-fitter across page-flip is so far
  8522. * untested on non-native modes, so ignore it for now.
  8523. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8524. */
  8525. pf = 0;
  8526. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8527. *cs++ = pf | pipesrc;
  8528. return 0;
  8529. }
  8530. static int intel_gen6_queue_flip(struct drm_device *dev,
  8531. struct drm_crtc *crtc,
  8532. struct drm_framebuffer *fb,
  8533. struct drm_i915_gem_object *obj,
  8534. struct drm_i915_gem_request *req,
  8535. uint32_t flags)
  8536. {
  8537. struct drm_i915_private *dev_priv = to_i915(dev);
  8538. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8539. u32 pf, pipesrc, *cs;
  8540. cs = intel_ring_begin(req, 4);
  8541. if (IS_ERR(cs))
  8542. return PTR_ERR(cs);
  8543. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8544. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8545. *cs++ = intel_crtc->flip_work->gtt_offset;
  8546. /* Contrary to the suggestions in the documentation,
  8547. * "Enable Panel Fitter" does not seem to be required when page
  8548. * flipping with a non-native mode, and worse causes a normal
  8549. * modeset to fail.
  8550. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8551. */
  8552. pf = 0;
  8553. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8554. *cs++ = pf | pipesrc;
  8555. return 0;
  8556. }
  8557. static int intel_gen7_queue_flip(struct drm_device *dev,
  8558. struct drm_crtc *crtc,
  8559. struct drm_framebuffer *fb,
  8560. struct drm_i915_gem_object *obj,
  8561. struct drm_i915_gem_request *req,
  8562. uint32_t flags)
  8563. {
  8564. struct drm_i915_private *dev_priv = to_i915(dev);
  8565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8566. u32 *cs, plane_bit = 0;
  8567. int len, ret;
  8568. switch (intel_crtc->plane) {
  8569. case PLANE_A:
  8570. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8571. break;
  8572. case PLANE_B:
  8573. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8574. break;
  8575. case PLANE_C:
  8576. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8577. break;
  8578. default:
  8579. WARN_ONCE(1, "unknown plane in flip command\n");
  8580. return -ENODEV;
  8581. }
  8582. len = 4;
  8583. if (req->engine->id == RCS) {
  8584. len += 6;
  8585. /*
  8586. * On Gen 8, SRM is now taking an extra dword to accommodate
  8587. * 48bits addresses, and we need a NOOP for the batch size to
  8588. * stay even.
  8589. */
  8590. if (IS_GEN8(dev_priv))
  8591. len += 2;
  8592. }
  8593. /*
  8594. * BSpec MI_DISPLAY_FLIP for IVB:
  8595. * "The full packet must be contained within the same cache line."
  8596. *
  8597. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8598. * cacheline, if we ever start emitting more commands before
  8599. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8600. * then do the cacheline alignment, and finally emit the
  8601. * MI_DISPLAY_FLIP.
  8602. */
  8603. ret = intel_ring_cacheline_align(req);
  8604. if (ret)
  8605. return ret;
  8606. cs = intel_ring_begin(req, len);
  8607. if (IS_ERR(cs))
  8608. return PTR_ERR(cs);
  8609. /* Unmask the flip-done completion message. Note that the bspec says that
  8610. * we should do this for both the BCS and RCS, and that we must not unmask
  8611. * more than one flip event at any time (or ensure that one flip message
  8612. * can be sent by waiting for flip-done prior to queueing new flips).
  8613. * Experimentation says that BCS works despite DERRMR masking all
  8614. * flip-done completion events and that unmasking all planes at once
  8615. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8616. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8617. */
  8618. if (req->engine->id == RCS) {
  8619. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8620. *cs++ = i915_mmio_reg_offset(DERRMR);
  8621. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8622. DERRMR_PIPEB_PRI_FLIP_DONE |
  8623. DERRMR_PIPEC_PRI_FLIP_DONE);
  8624. if (IS_GEN8(dev_priv))
  8625. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8626. MI_SRM_LRM_GLOBAL_GTT;
  8627. else
  8628. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8629. *cs++ = i915_mmio_reg_offset(DERRMR);
  8630. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8631. if (IS_GEN8(dev_priv)) {
  8632. *cs++ = 0;
  8633. *cs++ = MI_NOOP;
  8634. }
  8635. }
  8636. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8637. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8638. *cs++ = intel_crtc->flip_work->gtt_offset;
  8639. *cs++ = MI_NOOP;
  8640. return 0;
  8641. }
  8642. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8643. struct drm_i915_gem_object *obj)
  8644. {
  8645. /*
  8646. * This is not being used for older platforms, because
  8647. * non-availability of flip done interrupt forces us to use
  8648. * CS flips. Older platforms derive flip done using some clever
  8649. * tricks involving the flip_pending status bits and vblank irqs.
  8650. * So using MMIO flips there would disrupt this mechanism.
  8651. */
  8652. if (engine == NULL)
  8653. return true;
  8654. if (INTEL_GEN(engine->i915) < 5)
  8655. return false;
  8656. if (i915.use_mmio_flip < 0)
  8657. return false;
  8658. else if (i915.use_mmio_flip > 0)
  8659. return true;
  8660. else if (i915.enable_execlists)
  8661. return true;
  8662. return engine != i915_gem_object_last_write_engine(obj);
  8663. }
  8664. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8665. unsigned int rotation,
  8666. struct intel_flip_work *work)
  8667. {
  8668. struct drm_device *dev = intel_crtc->base.dev;
  8669. struct drm_i915_private *dev_priv = to_i915(dev);
  8670. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8671. const enum pipe pipe = intel_crtc->pipe;
  8672. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8673. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8674. ctl &= ~PLANE_CTL_TILED_MASK;
  8675. switch (fb->modifier) {
  8676. case DRM_FORMAT_MOD_LINEAR:
  8677. break;
  8678. case I915_FORMAT_MOD_X_TILED:
  8679. ctl |= PLANE_CTL_TILED_X;
  8680. break;
  8681. case I915_FORMAT_MOD_Y_TILED:
  8682. ctl |= PLANE_CTL_TILED_Y;
  8683. break;
  8684. case I915_FORMAT_MOD_Yf_TILED:
  8685. ctl |= PLANE_CTL_TILED_YF;
  8686. break;
  8687. default:
  8688. MISSING_CASE(fb->modifier);
  8689. }
  8690. /*
  8691. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8692. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8693. */
  8694. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8695. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8696. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8697. POSTING_READ(PLANE_SURF(pipe, 0));
  8698. }
  8699. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8700. struct intel_flip_work *work)
  8701. {
  8702. struct drm_device *dev = intel_crtc->base.dev;
  8703. struct drm_i915_private *dev_priv = to_i915(dev);
  8704. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8705. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8706. u32 dspcntr;
  8707. dspcntr = I915_READ(reg);
  8708. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8709. dspcntr |= DISPPLANE_TILED;
  8710. else
  8711. dspcntr &= ~DISPPLANE_TILED;
  8712. I915_WRITE(reg, dspcntr);
  8713. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8714. POSTING_READ(DSPSURF(intel_crtc->plane));
  8715. }
  8716. static void intel_mmio_flip_work_func(struct work_struct *w)
  8717. {
  8718. struct intel_flip_work *work =
  8719. container_of(w, struct intel_flip_work, mmio_work);
  8720. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8721. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8722. struct intel_framebuffer *intel_fb =
  8723. to_intel_framebuffer(crtc->base.primary->fb);
  8724. struct drm_i915_gem_object *obj = intel_fb->obj;
  8725. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8726. intel_pipe_update_start(crtc);
  8727. if (INTEL_GEN(dev_priv) >= 9)
  8728. skl_do_mmio_flip(crtc, work->rotation, work);
  8729. else
  8730. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8731. ilk_do_mmio_flip(crtc, work);
  8732. intel_pipe_update_end(crtc, work);
  8733. }
  8734. static int intel_default_queue_flip(struct drm_device *dev,
  8735. struct drm_crtc *crtc,
  8736. struct drm_framebuffer *fb,
  8737. struct drm_i915_gem_object *obj,
  8738. struct drm_i915_gem_request *req,
  8739. uint32_t flags)
  8740. {
  8741. return -ENODEV;
  8742. }
  8743. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8744. struct intel_crtc *intel_crtc,
  8745. struct intel_flip_work *work)
  8746. {
  8747. u32 addr, vblank;
  8748. if (!atomic_read(&work->pending))
  8749. return false;
  8750. smp_rmb();
  8751. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8752. if (work->flip_ready_vblank == 0) {
  8753. if (work->flip_queued_req &&
  8754. !i915_gem_request_completed(work->flip_queued_req))
  8755. return false;
  8756. work->flip_ready_vblank = vblank;
  8757. }
  8758. if (vblank - work->flip_ready_vblank < 3)
  8759. return false;
  8760. /* Potential stall - if we see that the flip has happened,
  8761. * assume a missed interrupt. */
  8762. if (INTEL_GEN(dev_priv) >= 4)
  8763. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8764. else
  8765. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8766. /* There is a potential issue here with a false positive after a flip
  8767. * to the same address. We could address this by checking for a
  8768. * non-incrementing frame counter.
  8769. */
  8770. return addr == work->gtt_offset;
  8771. }
  8772. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8773. {
  8774. struct drm_device *dev = &dev_priv->drm;
  8775. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8776. struct intel_flip_work *work;
  8777. WARN_ON(!in_interrupt());
  8778. if (crtc == NULL)
  8779. return;
  8780. spin_lock(&dev->event_lock);
  8781. work = crtc->flip_work;
  8782. if (work != NULL && !is_mmio_work(work) &&
  8783. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8784. WARN_ONCE(1,
  8785. "Kicking stuck page flip: queued at %d, now %d\n",
  8786. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8787. page_flip_completed(crtc);
  8788. work = NULL;
  8789. }
  8790. if (work != NULL && !is_mmio_work(work) &&
  8791. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8792. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8793. spin_unlock(&dev->event_lock);
  8794. }
  8795. __maybe_unused
  8796. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8797. struct drm_framebuffer *fb,
  8798. struct drm_pending_vblank_event *event,
  8799. uint32_t page_flip_flags)
  8800. {
  8801. struct drm_device *dev = crtc->dev;
  8802. struct drm_i915_private *dev_priv = to_i915(dev);
  8803. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8804. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8805. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8806. struct drm_plane *primary = crtc->primary;
  8807. enum pipe pipe = intel_crtc->pipe;
  8808. struct intel_flip_work *work;
  8809. struct intel_engine_cs *engine;
  8810. bool mmio_flip;
  8811. struct drm_i915_gem_request *request;
  8812. struct i915_vma *vma;
  8813. int ret;
  8814. /*
  8815. * drm_mode_page_flip_ioctl() should already catch this, but double
  8816. * check to be safe. In the future we may enable pageflipping from
  8817. * a disabled primary plane.
  8818. */
  8819. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8820. return -EBUSY;
  8821. /* Can't change pixel format via MI display flips. */
  8822. if (fb->format != crtc->primary->fb->format)
  8823. return -EINVAL;
  8824. /*
  8825. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8826. * Note that pitch changes could also affect these register.
  8827. */
  8828. if (INTEL_GEN(dev_priv) > 3 &&
  8829. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8830. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8831. return -EINVAL;
  8832. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8833. goto out_hang;
  8834. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8835. if (work == NULL)
  8836. return -ENOMEM;
  8837. work->event = event;
  8838. work->crtc = crtc;
  8839. work->old_fb = old_fb;
  8840. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8841. ret = drm_crtc_vblank_get(crtc);
  8842. if (ret)
  8843. goto free_work;
  8844. /* We borrow the event spin lock for protecting flip_work */
  8845. spin_lock_irq(&dev->event_lock);
  8846. if (intel_crtc->flip_work) {
  8847. /* Before declaring the flip queue wedged, check if
  8848. * the hardware completed the operation behind our backs.
  8849. */
  8850. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8851. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8852. page_flip_completed(intel_crtc);
  8853. } else {
  8854. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8855. spin_unlock_irq(&dev->event_lock);
  8856. drm_crtc_vblank_put(crtc);
  8857. kfree(work);
  8858. return -EBUSY;
  8859. }
  8860. }
  8861. intel_crtc->flip_work = work;
  8862. spin_unlock_irq(&dev->event_lock);
  8863. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8864. flush_workqueue(dev_priv->wq);
  8865. /* Reference the objects for the scheduled work. */
  8866. drm_framebuffer_reference(work->old_fb);
  8867. crtc->primary->fb = fb;
  8868. update_state_fb(crtc->primary);
  8869. work->pending_flip_obj = i915_gem_object_get(obj);
  8870. ret = i915_mutex_lock_interruptible(dev);
  8871. if (ret)
  8872. goto cleanup;
  8873. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  8874. if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
  8875. ret = -EIO;
  8876. goto unlock;
  8877. }
  8878. atomic_inc(&intel_crtc->unpin_work_count);
  8879. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8880. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  8881. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8882. engine = dev_priv->engine[BCS];
  8883. if (fb->modifier != old_fb->modifier)
  8884. /* vlv: DISPLAY_FLIP fails to change tiling */
  8885. engine = NULL;
  8886. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  8887. engine = dev_priv->engine[BCS];
  8888. } else if (INTEL_GEN(dev_priv) >= 7) {
  8889. engine = i915_gem_object_last_write_engine(obj);
  8890. if (engine == NULL || engine->id != RCS)
  8891. engine = dev_priv->engine[BCS];
  8892. } else {
  8893. engine = dev_priv->engine[RCS];
  8894. }
  8895. mmio_flip = use_mmio_flip(engine, obj);
  8896. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  8897. if (IS_ERR(vma)) {
  8898. ret = PTR_ERR(vma);
  8899. goto cleanup_pending;
  8900. }
  8901. work->old_vma = to_intel_plane_state(primary->state)->vma;
  8902. to_intel_plane_state(primary->state)->vma = vma;
  8903. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  8904. work->rotation = crtc->primary->state->rotation;
  8905. /*
  8906. * There's the potential that the next frame will not be compatible with
  8907. * FBC, so we want to call pre_update() before the actual page flip.
  8908. * The problem is that pre_update() caches some information about the fb
  8909. * object, so we want to do this only after the object is pinned. Let's
  8910. * be on the safe side and do this immediately before scheduling the
  8911. * flip.
  8912. */
  8913. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  8914. to_intel_plane_state(primary->state));
  8915. if (mmio_flip) {
  8916. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  8917. queue_work(system_unbound_wq, &work->mmio_work);
  8918. } else {
  8919. request = i915_gem_request_alloc(engine,
  8920. dev_priv->kernel_context);
  8921. if (IS_ERR(request)) {
  8922. ret = PTR_ERR(request);
  8923. goto cleanup_unpin;
  8924. }
  8925. ret = i915_gem_request_await_object(request, obj, false);
  8926. if (ret)
  8927. goto cleanup_request;
  8928. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  8929. page_flip_flags);
  8930. if (ret)
  8931. goto cleanup_request;
  8932. intel_mark_page_flip_active(intel_crtc, work);
  8933. work->flip_queued_req = i915_gem_request_get(request);
  8934. i915_add_request(request);
  8935. }
  8936. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  8937. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  8938. to_intel_plane(primary)->frontbuffer_bit);
  8939. mutex_unlock(&dev->struct_mutex);
  8940. intel_frontbuffer_flip_prepare(to_i915(dev),
  8941. to_intel_plane(primary)->frontbuffer_bit);
  8942. trace_i915_flip_request(intel_crtc->plane, obj);
  8943. return 0;
  8944. cleanup_request:
  8945. i915_add_request(request);
  8946. cleanup_unpin:
  8947. to_intel_plane_state(primary->state)->vma = work->old_vma;
  8948. intel_unpin_fb_vma(vma);
  8949. cleanup_pending:
  8950. atomic_dec(&intel_crtc->unpin_work_count);
  8951. unlock:
  8952. mutex_unlock(&dev->struct_mutex);
  8953. cleanup:
  8954. crtc->primary->fb = old_fb;
  8955. update_state_fb(crtc->primary);
  8956. i915_gem_object_put(obj);
  8957. drm_framebuffer_unreference(work->old_fb);
  8958. spin_lock_irq(&dev->event_lock);
  8959. intel_crtc->flip_work = NULL;
  8960. spin_unlock_irq(&dev->event_lock);
  8961. drm_crtc_vblank_put(crtc);
  8962. free_work:
  8963. kfree(work);
  8964. if (ret == -EIO) {
  8965. struct drm_atomic_state *state;
  8966. struct drm_plane_state *plane_state;
  8967. out_hang:
  8968. state = drm_atomic_state_alloc(dev);
  8969. if (!state)
  8970. return -ENOMEM;
  8971. state->acquire_ctx = dev->mode_config.acquire_ctx;
  8972. retry:
  8973. plane_state = drm_atomic_get_plane_state(state, primary);
  8974. ret = PTR_ERR_OR_ZERO(plane_state);
  8975. if (!ret) {
  8976. drm_atomic_set_fb_for_plane(plane_state, fb);
  8977. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  8978. if (!ret)
  8979. ret = drm_atomic_commit(state);
  8980. }
  8981. if (ret == -EDEADLK) {
  8982. drm_modeset_backoff(state->acquire_ctx);
  8983. drm_atomic_state_clear(state);
  8984. goto retry;
  8985. }
  8986. drm_atomic_state_put(state);
  8987. if (ret == 0 && event) {
  8988. spin_lock_irq(&dev->event_lock);
  8989. drm_crtc_send_vblank_event(crtc, event);
  8990. spin_unlock_irq(&dev->event_lock);
  8991. }
  8992. }
  8993. return ret;
  8994. }
  8995. /**
  8996. * intel_wm_need_update - Check whether watermarks need updating
  8997. * @plane: drm plane
  8998. * @state: new plane state
  8999. *
  9000. * Check current plane state versus the new one to determine whether
  9001. * watermarks need to be recalculated.
  9002. *
  9003. * Returns true or false.
  9004. */
  9005. static bool intel_wm_need_update(struct drm_plane *plane,
  9006. struct drm_plane_state *state)
  9007. {
  9008. struct intel_plane_state *new = to_intel_plane_state(state);
  9009. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9010. /* Update watermarks on tiling or size changes. */
  9011. if (new->base.visible != cur->base.visible)
  9012. return true;
  9013. if (!cur->base.fb || !new->base.fb)
  9014. return false;
  9015. if (cur->base.fb->modifier != new->base.fb->modifier ||
  9016. cur->base.rotation != new->base.rotation ||
  9017. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  9018. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  9019. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  9020. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  9021. return true;
  9022. return false;
  9023. }
  9024. static bool needs_scaling(struct intel_plane_state *state)
  9025. {
  9026. int src_w = drm_rect_width(&state->base.src) >> 16;
  9027. int src_h = drm_rect_height(&state->base.src) >> 16;
  9028. int dst_w = drm_rect_width(&state->base.dst);
  9029. int dst_h = drm_rect_height(&state->base.dst);
  9030. return (src_w != dst_w || src_h != dst_h);
  9031. }
  9032. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9033. struct drm_plane_state *plane_state)
  9034. {
  9035. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9036. struct drm_crtc *crtc = crtc_state->crtc;
  9037. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9038. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  9039. struct drm_device *dev = crtc->dev;
  9040. struct drm_i915_private *dev_priv = to_i915(dev);
  9041. struct intel_plane_state *old_plane_state =
  9042. to_intel_plane_state(plane->base.state);
  9043. bool mode_changed = needs_modeset(crtc_state);
  9044. bool was_crtc_enabled = crtc->state->active;
  9045. bool is_crtc_enabled = crtc_state->active;
  9046. bool turn_off, turn_on, visible, was_visible;
  9047. struct drm_framebuffer *fb = plane_state->fb;
  9048. int ret;
  9049. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  9050. ret = skl_update_scaler_plane(
  9051. to_intel_crtc_state(crtc_state),
  9052. to_intel_plane_state(plane_state));
  9053. if (ret)
  9054. return ret;
  9055. }
  9056. was_visible = old_plane_state->base.visible;
  9057. visible = plane_state->visible;
  9058. if (!was_crtc_enabled && WARN_ON(was_visible))
  9059. was_visible = false;
  9060. /*
  9061. * Visibility is calculated as if the crtc was on, but
  9062. * after scaler setup everything depends on it being off
  9063. * when the crtc isn't active.
  9064. *
  9065. * FIXME this is wrong for watermarks. Watermarks should also
  9066. * be computed as if the pipe would be active. Perhaps move
  9067. * per-plane wm computation to the .check_plane() hook, and
  9068. * only combine the results from all planes in the current place?
  9069. */
  9070. if (!is_crtc_enabled) {
  9071. plane_state->visible = visible = false;
  9072. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  9073. }
  9074. if (!was_visible && !visible)
  9075. return 0;
  9076. if (fb != old_plane_state->base.fb)
  9077. pipe_config->fb_changed = true;
  9078. turn_off = was_visible && (!visible || mode_changed);
  9079. turn_on = visible && (!was_visible || mode_changed);
  9080. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9081. intel_crtc->base.base.id, intel_crtc->base.name,
  9082. plane->base.base.id, plane->base.name,
  9083. fb ? fb->base.id : -1);
  9084. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9085. plane->base.base.id, plane->base.name,
  9086. was_visible, visible,
  9087. turn_off, turn_on, mode_changed);
  9088. if (turn_on) {
  9089. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9090. pipe_config->update_wm_pre = true;
  9091. /* must disable cxsr around plane enable/disable */
  9092. if (plane->id != PLANE_CURSOR)
  9093. pipe_config->disable_cxsr = true;
  9094. } else if (turn_off) {
  9095. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9096. pipe_config->update_wm_post = true;
  9097. /* must disable cxsr around plane enable/disable */
  9098. if (plane->id != PLANE_CURSOR)
  9099. pipe_config->disable_cxsr = true;
  9100. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  9101. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  9102. /* FIXME bollocks */
  9103. pipe_config->update_wm_pre = true;
  9104. pipe_config->update_wm_post = true;
  9105. }
  9106. }
  9107. if (visible || was_visible)
  9108. pipe_config->fb_bits |= plane->frontbuffer_bit;
  9109. /*
  9110. * WaCxSRDisabledForSpriteScaling:ivb
  9111. *
  9112. * cstate->update_wm was already set above, so this flag will
  9113. * take effect when we commit and program watermarks.
  9114. */
  9115. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  9116. needs_scaling(to_intel_plane_state(plane_state)) &&
  9117. !needs_scaling(old_plane_state))
  9118. pipe_config->disable_lp_wm = true;
  9119. return 0;
  9120. }
  9121. static bool encoders_cloneable(const struct intel_encoder *a,
  9122. const struct intel_encoder *b)
  9123. {
  9124. /* masks could be asymmetric, so check both ways */
  9125. return a == b || (a->cloneable & (1 << b->type) &&
  9126. b->cloneable & (1 << a->type));
  9127. }
  9128. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9129. struct intel_crtc *crtc,
  9130. struct intel_encoder *encoder)
  9131. {
  9132. struct intel_encoder *source_encoder;
  9133. struct drm_connector *connector;
  9134. struct drm_connector_state *connector_state;
  9135. int i;
  9136. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9137. if (connector_state->crtc != &crtc->base)
  9138. continue;
  9139. source_encoder =
  9140. to_intel_encoder(connector_state->best_encoder);
  9141. if (!encoders_cloneable(encoder, source_encoder))
  9142. return false;
  9143. }
  9144. return true;
  9145. }
  9146. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9147. struct drm_crtc_state *crtc_state)
  9148. {
  9149. struct drm_device *dev = crtc->dev;
  9150. struct drm_i915_private *dev_priv = to_i915(dev);
  9151. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9152. struct intel_crtc_state *pipe_config =
  9153. to_intel_crtc_state(crtc_state);
  9154. struct drm_atomic_state *state = crtc_state->state;
  9155. int ret;
  9156. bool mode_changed = needs_modeset(crtc_state);
  9157. if (mode_changed && !crtc_state->active)
  9158. pipe_config->update_wm_post = true;
  9159. if (mode_changed && crtc_state->enable &&
  9160. dev_priv->display.crtc_compute_clock &&
  9161. !WARN_ON(pipe_config->shared_dpll)) {
  9162. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9163. pipe_config);
  9164. if (ret)
  9165. return ret;
  9166. }
  9167. if (crtc_state->color_mgmt_changed) {
  9168. ret = intel_color_check(crtc, crtc_state);
  9169. if (ret)
  9170. return ret;
  9171. /*
  9172. * Changing color management on Intel hardware is
  9173. * handled as part of planes update.
  9174. */
  9175. crtc_state->planes_changed = true;
  9176. }
  9177. ret = 0;
  9178. if (dev_priv->display.compute_pipe_wm) {
  9179. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9180. if (ret) {
  9181. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9182. return ret;
  9183. }
  9184. }
  9185. if (dev_priv->display.compute_intermediate_wm &&
  9186. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9187. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9188. return 0;
  9189. /*
  9190. * Calculate 'intermediate' watermarks that satisfy both the
  9191. * old state and the new state. We can program these
  9192. * immediately.
  9193. */
  9194. ret = dev_priv->display.compute_intermediate_wm(dev,
  9195. intel_crtc,
  9196. pipe_config);
  9197. if (ret) {
  9198. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9199. return ret;
  9200. }
  9201. } else if (dev_priv->display.compute_intermediate_wm) {
  9202. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9203. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9204. }
  9205. if (INTEL_GEN(dev_priv) >= 9) {
  9206. if (mode_changed)
  9207. ret = skl_update_scaler_crtc(pipe_config);
  9208. if (!ret)
  9209. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9210. pipe_config);
  9211. }
  9212. return ret;
  9213. }
  9214. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9215. .atomic_begin = intel_begin_crtc_commit,
  9216. .atomic_flush = intel_finish_crtc_commit,
  9217. .atomic_check = intel_crtc_atomic_check,
  9218. };
  9219. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9220. {
  9221. struct intel_connector *connector;
  9222. struct drm_connector_list_iter conn_iter;
  9223. drm_connector_list_iter_begin(dev, &conn_iter);
  9224. for_each_intel_connector_iter(connector, &conn_iter) {
  9225. if (connector->base.state->crtc)
  9226. drm_connector_unreference(&connector->base);
  9227. if (connector->base.encoder) {
  9228. connector->base.state->best_encoder =
  9229. connector->base.encoder;
  9230. connector->base.state->crtc =
  9231. connector->base.encoder->crtc;
  9232. drm_connector_reference(&connector->base);
  9233. } else {
  9234. connector->base.state->best_encoder = NULL;
  9235. connector->base.state->crtc = NULL;
  9236. }
  9237. }
  9238. drm_connector_list_iter_end(&conn_iter);
  9239. }
  9240. static void
  9241. connected_sink_compute_bpp(struct intel_connector *connector,
  9242. struct intel_crtc_state *pipe_config)
  9243. {
  9244. const struct drm_display_info *info = &connector->base.display_info;
  9245. int bpp = pipe_config->pipe_bpp;
  9246. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9247. connector->base.base.id,
  9248. connector->base.name);
  9249. /* Don't use an invalid EDID bpc value */
  9250. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9251. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9252. bpp, info->bpc * 3);
  9253. pipe_config->pipe_bpp = info->bpc * 3;
  9254. }
  9255. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9256. if (info->bpc == 0 && bpp > 24) {
  9257. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9258. bpp);
  9259. pipe_config->pipe_bpp = 24;
  9260. }
  9261. }
  9262. static int
  9263. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9264. struct intel_crtc_state *pipe_config)
  9265. {
  9266. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9267. struct drm_atomic_state *state;
  9268. struct drm_connector *connector;
  9269. struct drm_connector_state *connector_state;
  9270. int bpp, i;
  9271. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9272. IS_CHERRYVIEW(dev_priv)))
  9273. bpp = 10*3;
  9274. else if (INTEL_GEN(dev_priv) >= 5)
  9275. bpp = 12*3;
  9276. else
  9277. bpp = 8*3;
  9278. pipe_config->pipe_bpp = bpp;
  9279. state = pipe_config->base.state;
  9280. /* Clamp display bpp to EDID value */
  9281. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9282. if (connector_state->crtc != &crtc->base)
  9283. continue;
  9284. connected_sink_compute_bpp(to_intel_connector(connector),
  9285. pipe_config);
  9286. }
  9287. return bpp;
  9288. }
  9289. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9290. {
  9291. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9292. "type: 0x%x flags: 0x%x\n",
  9293. mode->crtc_clock,
  9294. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9295. mode->crtc_hsync_end, mode->crtc_htotal,
  9296. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9297. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9298. }
  9299. static inline void
  9300. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9301. unsigned int lane_count, struct intel_link_m_n *m_n)
  9302. {
  9303. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9304. id, lane_count,
  9305. m_n->gmch_m, m_n->gmch_n,
  9306. m_n->link_m, m_n->link_n, m_n->tu);
  9307. }
  9308. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9309. struct intel_crtc_state *pipe_config,
  9310. const char *context)
  9311. {
  9312. struct drm_device *dev = crtc->base.dev;
  9313. struct drm_i915_private *dev_priv = to_i915(dev);
  9314. struct drm_plane *plane;
  9315. struct intel_plane *intel_plane;
  9316. struct intel_plane_state *state;
  9317. struct drm_framebuffer *fb;
  9318. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9319. crtc->base.base.id, crtc->base.name, context);
  9320. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9321. transcoder_name(pipe_config->cpu_transcoder),
  9322. pipe_config->pipe_bpp, pipe_config->dither);
  9323. if (pipe_config->has_pch_encoder)
  9324. intel_dump_m_n_config(pipe_config, "fdi",
  9325. pipe_config->fdi_lanes,
  9326. &pipe_config->fdi_m_n);
  9327. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9328. intel_dump_m_n_config(pipe_config, "dp m_n",
  9329. pipe_config->lane_count, &pipe_config->dp_m_n);
  9330. if (pipe_config->has_drrs)
  9331. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9332. pipe_config->lane_count,
  9333. &pipe_config->dp_m2_n2);
  9334. }
  9335. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9336. pipe_config->has_audio, pipe_config->has_infoframe);
  9337. DRM_DEBUG_KMS("requested mode:\n");
  9338. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9339. DRM_DEBUG_KMS("adjusted mode:\n");
  9340. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9341. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9342. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9343. pipe_config->port_clock,
  9344. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9345. pipe_config->pixel_rate);
  9346. if (INTEL_GEN(dev_priv) >= 9)
  9347. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9348. crtc->num_scalers,
  9349. pipe_config->scaler_state.scaler_users,
  9350. pipe_config->scaler_state.scaler_id);
  9351. if (HAS_GMCH_DISPLAY(dev_priv))
  9352. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9353. pipe_config->gmch_pfit.control,
  9354. pipe_config->gmch_pfit.pgm_ratios,
  9355. pipe_config->gmch_pfit.lvds_border_bits);
  9356. else
  9357. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9358. pipe_config->pch_pfit.pos,
  9359. pipe_config->pch_pfit.size,
  9360. enableddisabled(pipe_config->pch_pfit.enabled));
  9361. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9362. pipe_config->ips_enabled, pipe_config->double_wide);
  9363. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9364. DRM_DEBUG_KMS("planes on this crtc\n");
  9365. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9366. struct drm_format_name_buf format_name;
  9367. intel_plane = to_intel_plane(plane);
  9368. if (intel_plane->pipe != crtc->pipe)
  9369. continue;
  9370. state = to_intel_plane_state(plane->state);
  9371. fb = state->base.fb;
  9372. if (!fb) {
  9373. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9374. plane->base.id, plane->name, state->scaler_id);
  9375. continue;
  9376. }
  9377. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9378. plane->base.id, plane->name,
  9379. fb->base.id, fb->width, fb->height,
  9380. drm_get_format_name(fb->format->format, &format_name));
  9381. if (INTEL_GEN(dev_priv) >= 9)
  9382. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9383. state->scaler_id,
  9384. state->base.src.x1 >> 16,
  9385. state->base.src.y1 >> 16,
  9386. drm_rect_width(&state->base.src) >> 16,
  9387. drm_rect_height(&state->base.src) >> 16,
  9388. state->base.dst.x1, state->base.dst.y1,
  9389. drm_rect_width(&state->base.dst),
  9390. drm_rect_height(&state->base.dst));
  9391. }
  9392. }
  9393. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9394. {
  9395. struct drm_device *dev = state->dev;
  9396. struct drm_connector *connector;
  9397. unsigned int used_ports = 0;
  9398. unsigned int used_mst_ports = 0;
  9399. /*
  9400. * Walk the connector list instead of the encoder
  9401. * list to detect the problem on ddi platforms
  9402. * where there's just one encoder per digital port.
  9403. */
  9404. drm_for_each_connector(connector, dev) {
  9405. struct drm_connector_state *connector_state;
  9406. struct intel_encoder *encoder;
  9407. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9408. if (!connector_state)
  9409. connector_state = connector->state;
  9410. if (!connector_state->best_encoder)
  9411. continue;
  9412. encoder = to_intel_encoder(connector_state->best_encoder);
  9413. WARN_ON(!connector_state->crtc);
  9414. switch (encoder->type) {
  9415. unsigned int port_mask;
  9416. case INTEL_OUTPUT_UNKNOWN:
  9417. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9418. break;
  9419. case INTEL_OUTPUT_DP:
  9420. case INTEL_OUTPUT_HDMI:
  9421. case INTEL_OUTPUT_EDP:
  9422. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9423. /* the same port mustn't appear more than once */
  9424. if (used_ports & port_mask)
  9425. return false;
  9426. used_ports |= port_mask;
  9427. break;
  9428. case INTEL_OUTPUT_DP_MST:
  9429. used_mst_ports |=
  9430. 1 << enc_to_mst(&encoder->base)->primary->port;
  9431. break;
  9432. default:
  9433. break;
  9434. }
  9435. }
  9436. /* can't mix MST and SST/HDMI on the same port */
  9437. if (used_ports & used_mst_ports)
  9438. return false;
  9439. return true;
  9440. }
  9441. static void
  9442. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9443. {
  9444. struct drm_i915_private *dev_priv =
  9445. to_i915(crtc_state->base.crtc->dev);
  9446. struct intel_crtc_scaler_state scaler_state;
  9447. struct intel_dpll_hw_state dpll_hw_state;
  9448. struct intel_shared_dpll *shared_dpll;
  9449. struct intel_crtc_wm_state wm_state;
  9450. bool force_thru;
  9451. /* FIXME: before the switch to atomic started, a new pipe_config was
  9452. * kzalloc'd. Code that depends on any field being zero should be
  9453. * fixed, so that the crtc_state can be safely duplicated. For now,
  9454. * only fields that are know to not cause problems are preserved. */
  9455. scaler_state = crtc_state->scaler_state;
  9456. shared_dpll = crtc_state->shared_dpll;
  9457. dpll_hw_state = crtc_state->dpll_hw_state;
  9458. force_thru = crtc_state->pch_pfit.force_thru;
  9459. if (IS_G4X(dev_priv) ||
  9460. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9461. wm_state = crtc_state->wm;
  9462. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9463. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9464. memset(&crtc_state->base + 1, 0,
  9465. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9466. crtc_state->scaler_state = scaler_state;
  9467. crtc_state->shared_dpll = shared_dpll;
  9468. crtc_state->dpll_hw_state = dpll_hw_state;
  9469. crtc_state->pch_pfit.force_thru = force_thru;
  9470. if (IS_G4X(dev_priv) ||
  9471. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9472. crtc_state->wm = wm_state;
  9473. }
  9474. static int
  9475. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9476. struct intel_crtc_state *pipe_config)
  9477. {
  9478. struct drm_atomic_state *state = pipe_config->base.state;
  9479. struct intel_encoder *encoder;
  9480. struct drm_connector *connector;
  9481. struct drm_connector_state *connector_state;
  9482. int base_bpp, ret = -EINVAL;
  9483. int i;
  9484. bool retry = true;
  9485. clear_intel_crtc_state(pipe_config);
  9486. pipe_config->cpu_transcoder =
  9487. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9488. /*
  9489. * Sanitize sync polarity flags based on requested ones. If neither
  9490. * positive or negative polarity is requested, treat this as meaning
  9491. * negative polarity.
  9492. */
  9493. if (!(pipe_config->base.adjusted_mode.flags &
  9494. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9495. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9496. if (!(pipe_config->base.adjusted_mode.flags &
  9497. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9498. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9499. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9500. pipe_config);
  9501. if (base_bpp < 0)
  9502. goto fail;
  9503. /*
  9504. * Determine the real pipe dimensions. Note that stereo modes can
  9505. * increase the actual pipe size due to the frame doubling and
  9506. * insertion of additional space for blanks between the frame. This
  9507. * is stored in the crtc timings. We use the requested mode to do this
  9508. * computation to clearly distinguish it from the adjusted mode, which
  9509. * can be changed by the connectors in the below retry loop.
  9510. */
  9511. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9512. &pipe_config->pipe_src_w,
  9513. &pipe_config->pipe_src_h);
  9514. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9515. if (connector_state->crtc != crtc)
  9516. continue;
  9517. encoder = to_intel_encoder(connector_state->best_encoder);
  9518. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9519. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9520. goto fail;
  9521. }
  9522. /*
  9523. * Determine output_types before calling the .compute_config()
  9524. * hooks so that the hooks can use this information safely.
  9525. */
  9526. pipe_config->output_types |= 1 << encoder->type;
  9527. }
  9528. encoder_retry:
  9529. /* Ensure the port clock defaults are reset when retrying. */
  9530. pipe_config->port_clock = 0;
  9531. pipe_config->pixel_multiplier = 1;
  9532. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9533. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9534. CRTC_STEREO_DOUBLE);
  9535. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9536. * adjust it according to limitations or connector properties, and also
  9537. * a chance to reject the mode entirely.
  9538. */
  9539. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9540. if (connector_state->crtc != crtc)
  9541. continue;
  9542. encoder = to_intel_encoder(connector_state->best_encoder);
  9543. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9544. DRM_DEBUG_KMS("Encoder config failure\n");
  9545. goto fail;
  9546. }
  9547. }
  9548. /* Set default port clock if not overwritten by the encoder. Needs to be
  9549. * done afterwards in case the encoder adjusts the mode. */
  9550. if (!pipe_config->port_clock)
  9551. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9552. * pipe_config->pixel_multiplier;
  9553. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9554. if (ret < 0) {
  9555. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9556. goto fail;
  9557. }
  9558. if (ret == RETRY) {
  9559. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9560. ret = -EINVAL;
  9561. goto fail;
  9562. }
  9563. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9564. retry = false;
  9565. goto encoder_retry;
  9566. }
  9567. /* Dithering seems to not pass-through bits correctly when it should, so
  9568. * only enable it on 6bpc panels and when its not a compliance
  9569. * test requesting 6bpc video pattern.
  9570. */
  9571. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9572. !pipe_config->dither_force_disable;
  9573. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9574. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9575. fail:
  9576. return ret;
  9577. }
  9578. static void
  9579. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9580. {
  9581. struct drm_crtc *crtc;
  9582. struct drm_crtc_state *new_crtc_state;
  9583. int i;
  9584. /* Double check state. */
  9585. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9586. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9587. /* Update hwmode for vblank functions */
  9588. if (new_crtc_state->active)
  9589. crtc->hwmode = new_crtc_state->adjusted_mode;
  9590. else
  9591. crtc->hwmode.crtc_clock = 0;
  9592. /*
  9593. * Update legacy state to satisfy fbc code. This can
  9594. * be removed when fbc uses the atomic state.
  9595. */
  9596. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9597. struct drm_plane_state *plane_state = crtc->primary->state;
  9598. crtc->primary->fb = plane_state->fb;
  9599. crtc->x = plane_state->src_x >> 16;
  9600. crtc->y = plane_state->src_y >> 16;
  9601. }
  9602. }
  9603. }
  9604. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9605. {
  9606. int diff;
  9607. if (clock1 == clock2)
  9608. return true;
  9609. if (!clock1 || !clock2)
  9610. return false;
  9611. diff = abs(clock1 - clock2);
  9612. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9613. return true;
  9614. return false;
  9615. }
  9616. static bool
  9617. intel_compare_m_n(unsigned int m, unsigned int n,
  9618. unsigned int m2, unsigned int n2,
  9619. bool exact)
  9620. {
  9621. if (m == m2 && n == n2)
  9622. return true;
  9623. if (exact || !m || !n || !m2 || !n2)
  9624. return false;
  9625. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9626. if (n > n2) {
  9627. while (n > n2) {
  9628. m2 <<= 1;
  9629. n2 <<= 1;
  9630. }
  9631. } else if (n < n2) {
  9632. while (n < n2) {
  9633. m <<= 1;
  9634. n <<= 1;
  9635. }
  9636. }
  9637. if (n != n2)
  9638. return false;
  9639. return intel_fuzzy_clock_check(m, m2);
  9640. }
  9641. static bool
  9642. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9643. struct intel_link_m_n *m2_n2,
  9644. bool adjust)
  9645. {
  9646. if (m_n->tu == m2_n2->tu &&
  9647. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9648. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9649. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9650. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9651. if (adjust)
  9652. *m2_n2 = *m_n;
  9653. return true;
  9654. }
  9655. return false;
  9656. }
  9657. static void __printf(3, 4)
  9658. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9659. {
  9660. char *level;
  9661. unsigned int category;
  9662. struct va_format vaf;
  9663. va_list args;
  9664. if (adjust) {
  9665. level = KERN_DEBUG;
  9666. category = DRM_UT_KMS;
  9667. } else {
  9668. level = KERN_ERR;
  9669. category = DRM_UT_NONE;
  9670. }
  9671. va_start(args, format);
  9672. vaf.fmt = format;
  9673. vaf.va = &args;
  9674. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9675. va_end(args);
  9676. }
  9677. static bool
  9678. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9679. struct intel_crtc_state *current_config,
  9680. struct intel_crtc_state *pipe_config,
  9681. bool adjust)
  9682. {
  9683. bool ret = true;
  9684. #define PIPE_CONF_CHECK_X(name) \
  9685. if (current_config->name != pipe_config->name) { \
  9686. pipe_config_err(adjust, __stringify(name), \
  9687. "(expected 0x%08x, found 0x%08x)\n", \
  9688. current_config->name, \
  9689. pipe_config->name); \
  9690. ret = false; \
  9691. }
  9692. #define PIPE_CONF_CHECK_I(name) \
  9693. if (current_config->name != pipe_config->name) { \
  9694. pipe_config_err(adjust, __stringify(name), \
  9695. "(expected %i, found %i)\n", \
  9696. current_config->name, \
  9697. pipe_config->name); \
  9698. ret = false; \
  9699. }
  9700. #define PIPE_CONF_CHECK_P(name) \
  9701. if (current_config->name != pipe_config->name) { \
  9702. pipe_config_err(adjust, __stringify(name), \
  9703. "(expected %p, found %p)\n", \
  9704. current_config->name, \
  9705. pipe_config->name); \
  9706. ret = false; \
  9707. }
  9708. #define PIPE_CONF_CHECK_M_N(name) \
  9709. if (!intel_compare_link_m_n(&current_config->name, \
  9710. &pipe_config->name,\
  9711. adjust)) { \
  9712. pipe_config_err(adjust, __stringify(name), \
  9713. "(expected tu %i gmch %i/%i link %i/%i, " \
  9714. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9715. current_config->name.tu, \
  9716. current_config->name.gmch_m, \
  9717. current_config->name.gmch_n, \
  9718. current_config->name.link_m, \
  9719. current_config->name.link_n, \
  9720. pipe_config->name.tu, \
  9721. pipe_config->name.gmch_m, \
  9722. pipe_config->name.gmch_n, \
  9723. pipe_config->name.link_m, \
  9724. pipe_config->name.link_n); \
  9725. ret = false; \
  9726. }
  9727. /* This is required for BDW+ where there is only one set of registers for
  9728. * switching between high and low RR.
  9729. * This macro can be used whenever a comparison has to be made between one
  9730. * hw state and multiple sw state variables.
  9731. */
  9732. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9733. if (!intel_compare_link_m_n(&current_config->name, \
  9734. &pipe_config->name, adjust) && \
  9735. !intel_compare_link_m_n(&current_config->alt_name, \
  9736. &pipe_config->name, adjust)) { \
  9737. pipe_config_err(adjust, __stringify(name), \
  9738. "(expected tu %i gmch %i/%i link %i/%i, " \
  9739. "or tu %i gmch %i/%i link %i/%i, " \
  9740. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9741. current_config->name.tu, \
  9742. current_config->name.gmch_m, \
  9743. current_config->name.gmch_n, \
  9744. current_config->name.link_m, \
  9745. current_config->name.link_n, \
  9746. current_config->alt_name.tu, \
  9747. current_config->alt_name.gmch_m, \
  9748. current_config->alt_name.gmch_n, \
  9749. current_config->alt_name.link_m, \
  9750. current_config->alt_name.link_n, \
  9751. pipe_config->name.tu, \
  9752. pipe_config->name.gmch_m, \
  9753. pipe_config->name.gmch_n, \
  9754. pipe_config->name.link_m, \
  9755. pipe_config->name.link_n); \
  9756. ret = false; \
  9757. }
  9758. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9759. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9760. pipe_config_err(adjust, __stringify(name), \
  9761. "(%x) (expected %i, found %i)\n", \
  9762. (mask), \
  9763. current_config->name & (mask), \
  9764. pipe_config->name & (mask)); \
  9765. ret = false; \
  9766. }
  9767. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9768. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9769. pipe_config_err(adjust, __stringify(name), \
  9770. "(expected %i, found %i)\n", \
  9771. current_config->name, \
  9772. pipe_config->name); \
  9773. ret = false; \
  9774. }
  9775. #define PIPE_CONF_QUIRK(quirk) \
  9776. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9777. PIPE_CONF_CHECK_I(cpu_transcoder);
  9778. PIPE_CONF_CHECK_I(has_pch_encoder);
  9779. PIPE_CONF_CHECK_I(fdi_lanes);
  9780. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9781. PIPE_CONF_CHECK_I(lane_count);
  9782. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9783. if (INTEL_GEN(dev_priv) < 8) {
  9784. PIPE_CONF_CHECK_M_N(dp_m_n);
  9785. if (current_config->has_drrs)
  9786. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9787. } else
  9788. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9789. PIPE_CONF_CHECK_X(output_types);
  9790. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9791. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9792. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9793. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9794. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9795. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9796. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9797. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9798. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9799. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9800. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9801. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9802. PIPE_CONF_CHECK_I(pixel_multiplier);
  9803. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9804. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9805. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9806. PIPE_CONF_CHECK_I(limited_color_range);
  9807. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9808. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9809. PIPE_CONF_CHECK_I(has_infoframe);
  9810. PIPE_CONF_CHECK_I(has_audio);
  9811. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9812. DRM_MODE_FLAG_INTERLACE);
  9813. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9814. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9815. DRM_MODE_FLAG_PHSYNC);
  9816. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9817. DRM_MODE_FLAG_NHSYNC);
  9818. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9819. DRM_MODE_FLAG_PVSYNC);
  9820. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9821. DRM_MODE_FLAG_NVSYNC);
  9822. }
  9823. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9824. /* pfit ratios are autocomputed by the hw on gen4+ */
  9825. if (INTEL_GEN(dev_priv) < 4)
  9826. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9827. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9828. if (!adjust) {
  9829. PIPE_CONF_CHECK_I(pipe_src_w);
  9830. PIPE_CONF_CHECK_I(pipe_src_h);
  9831. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9832. if (current_config->pch_pfit.enabled) {
  9833. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9834. PIPE_CONF_CHECK_X(pch_pfit.size);
  9835. }
  9836. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9837. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9838. }
  9839. /* BDW+ don't expose a synchronous way to read the state */
  9840. if (IS_HASWELL(dev_priv))
  9841. PIPE_CONF_CHECK_I(ips_enabled);
  9842. PIPE_CONF_CHECK_I(double_wide);
  9843. PIPE_CONF_CHECK_P(shared_dpll);
  9844. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9845. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9846. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9847. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9848. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9849. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9850. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9851. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9852. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9853. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9854. PIPE_CONF_CHECK_X(dsi_pll.div);
  9855. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9856. PIPE_CONF_CHECK_I(pipe_bpp);
  9857. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9858. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9859. #undef PIPE_CONF_CHECK_X
  9860. #undef PIPE_CONF_CHECK_I
  9861. #undef PIPE_CONF_CHECK_P
  9862. #undef PIPE_CONF_CHECK_FLAGS
  9863. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9864. #undef PIPE_CONF_QUIRK
  9865. return ret;
  9866. }
  9867. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9868. const struct intel_crtc_state *pipe_config)
  9869. {
  9870. if (pipe_config->has_pch_encoder) {
  9871. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9872. &pipe_config->fdi_m_n);
  9873. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9874. /*
  9875. * FDI already provided one idea for the dotclock.
  9876. * Yell if the encoder disagrees.
  9877. */
  9878. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9879. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9880. fdi_dotclock, dotclock);
  9881. }
  9882. }
  9883. static void verify_wm_state(struct drm_crtc *crtc,
  9884. struct drm_crtc_state *new_state)
  9885. {
  9886. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9887. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9888. struct skl_pipe_wm hw_wm, *sw_wm;
  9889. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9890. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9891. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9892. const enum pipe pipe = intel_crtc->pipe;
  9893. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9894. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9895. return;
  9896. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9897. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9898. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9899. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9900. /* planes */
  9901. for_each_universal_plane(dev_priv, pipe, plane) {
  9902. hw_plane_wm = &hw_wm.planes[plane];
  9903. sw_plane_wm = &sw_wm->planes[plane];
  9904. /* Watermarks */
  9905. for (level = 0; level <= max_level; level++) {
  9906. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9907. &sw_plane_wm->wm[level]))
  9908. continue;
  9909. 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",
  9910. pipe_name(pipe), plane + 1, level,
  9911. sw_plane_wm->wm[level].plane_en,
  9912. sw_plane_wm->wm[level].plane_res_b,
  9913. sw_plane_wm->wm[level].plane_res_l,
  9914. hw_plane_wm->wm[level].plane_en,
  9915. hw_plane_wm->wm[level].plane_res_b,
  9916. hw_plane_wm->wm[level].plane_res_l);
  9917. }
  9918. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9919. &sw_plane_wm->trans_wm)) {
  9920. 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",
  9921. pipe_name(pipe), plane + 1,
  9922. sw_plane_wm->trans_wm.plane_en,
  9923. sw_plane_wm->trans_wm.plane_res_b,
  9924. sw_plane_wm->trans_wm.plane_res_l,
  9925. hw_plane_wm->trans_wm.plane_en,
  9926. hw_plane_wm->trans_wm.plane_res_b,
  9927. hw_plane_wm->trans_wm.plane_res_l);
  9928. }
  9929. /* DDB */
  9930. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9931. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9932. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9933. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9934. pipe_name(pipe), plane + 1,
  9935. sw_ddb_entry->start, sw_ddb_entry->end,
  9936. hw_ddb_entry->start, hw_ddb_entry->end);
  9937. }
  9938. }
  9939. /*
  9940. * cursor
  9941. * If the cursor plane isn't active, we may not have updated it's ddb
  9942. * allocation. In that case since the ddb allocation will be updated
  9943. * once the plane becomes visible, we can skip this check
  9944. */
  9945. if (1) {
  9946. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9947. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9948. /* Watermarks */
  9949. for (level = 0; level <= max_level; level++) {
  9950. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9951. &sw_plane_wm->wm[level]))
  9952. continue;
  9953. 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",
  9954. pipe_name(pipe), level,
  9955. sw_plane_wm->wm[level].plane_en,
  9956. sw_plane_wm->wm[level].plane_res_b,
  9957. sw_plane_wm->wm[level].plane_res_l,
  9958. hw_plane_wm->wm[level].plane_en,
  9959. hw_plane_wm->wm[level].plane_res_b,
  9960. hw_plane_wm->wm[level].plane_res_l);
  9961. }
  9962. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9963. &sw_plane_wm->trans_wm)) {
  9964. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9965. pipe_name(pipe),
  9966. sw_plane_wm->trans_wm.plane_en,
  9967. sw_plane_wm->trans_wm.plane_res_b,
  9968. sw_plane_wm->trans_wm.plane_res_l,
  9969. hw_plane_wm->trans_wm.plane_en,
  9970. hw_plane_wm->trans_wm.plane_res_b,
  9971. hw_plane_wm->trans_wm.plane_res_l);
  9972. }
  9973. /* DDB */
  9974. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9975. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9976. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9977. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9978. pipe_name(pipe),
  9979. sw_ddb_entry->start, sw_ddb_entry->end,
  9980. hw_ddb_entry->start, hw_ddb_entry->end);
  9981. }
  9982. }
  9983. }
  9984. static void
  9985. verify_connector_state(struct drm_device *dev,
  9986. struct drm_atomic_state *state,
  9987. struct drm_crtc *crtc)
  9988. {
  9989. struct drm_connector *connector;
  9990. struct drm_connector_state *new_conn_state;
  9991. int i;
  9992. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9993. struct drm_encoder *encoder = connector->encoder;
  9994. if (new_conn_state->crtc != crtc)
  9995. continue;
  9996. intel_connector_verify_state(to_intel_connector(connector));
  9997. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9998. "connector's atomic encoder doesn't match legacy encoder\n");
  9999. }
  10000. }
  10001. static void
  10002. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  10003. {
  10004. struct intel_encoder *encoder;
  10005. struct drm_connector *connector;
  10006. struct drm_connector_state *old_conn_state, *new_conn_state;
  10007. int i;
  10008. for_each_intel_encoder(dev, encoder) {
  10009. bool enabled = false, found = false;
  10010. enum pipe pipe;
  10011. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10012. encoder->base.base.id,
  10013. encoder->base.name);
  10014. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  10015. new_conn_state, i) {
  10016. if (old_conn_state->best_encoder == &encoder->base)
  10017. found = true;
  10018. if (new_conn_state->best_encoder != &encoder->base)
  10019. continue;
  10020. found = enabled = true;
  10021. I915_STATE_WARN(new_conn_state->crtc !=
  10022. encoder->base.crtc,
  10023. "connector's crtc doesn't match encoder crtc\n");
  10024. }
  10025. if (!found)
  10026. continue;
  10027. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10028. "encoder's enabled state mismatch "
  10029. "(expected %i, found %i)\n",
  10030. !!encoder->base.crtc, enabled);
  10031. if (!encoder->base.crtc) {
  10032. bool active;
  10033. active = encoder->get_hw_state(encoder, &pipe);
  10034. I915_STATE_WARN(active,
  10035. "encoder detached but still enabled on pipe %c.\n",
  10036. pipe_name(pipe));
  10037. }
  10038. }
  10039. }
  10040. static void
  10041. verify_crtc_state(struct drm_crtc *crtc,
  10042. struct drm_crtc_state *old_crtc_state,
  10043. struct drm_crtc_state *new_crtc_state)
  10044. {
  10045. struct drm_device *dev = crtc->dev;
  10046. struct drm_i915_private *dev_priv = to_i915(dev);
  10047. struct intel_encoder *encoder;
  10048. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10049. struct intel_crtc_state *pipe_config, *sw_config;
  10050. struct drm_atomic_state *old_state;
  10051. bool active;
  10052. old_state = old_crtc_state->state;
  10053. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10054. pipe_config = to_intel_crtc_state(old_crtc_state);
  10055. memset(pipe_config, 0, sizeof(*pipe_config));
  10056. pipe_config->base.crtc = crtc;
  10057. pipe_config->base.state = old_state;
  10058. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10059. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10060. /* hw state is inconsistent with the pipe quirk */
  10061. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10062. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10063. active = new_crtc_state->active;
  10064. I915_STATE_WARN(new_crtc_state->active != active,
  10065. "crtc active state doesn't match with hw state "
  10066. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10067. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10068. "transitional active state does not match atomic hw state "
  10069. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10070. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10071. enum pipe pipe;
  10072. active = encoder->get_hw_state(encoder, &pipe);
  10073. I915_STATE_WARN(active != new_crtc_state->active,
  10074. "[ENCODER:%i] active %i with crtc active %i\n",
  10075. encoder->base.base.id, active, new_crtc_state->active);
  10076. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10077. "Encoder connected to wrong pipe %c\n",
  10078. pipe_name(pipe));
  10079. if (active) {
  10080. pipe_config->output_types |= 1 << encoder->type;
  10081. encoder->get_config(encoder, pipe_config);
  10082. }
  10083. }
  10084. intel_crtc_compute_pixel_rate(pipe_config);
  10085. if (!new_crtc_state->active)
  10086. return;
  10087. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10088. sw_config = to_intel_crtc_state(crtc->state);
  10089. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10090. pipe_config, false)) {
  10091. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10092. intel_dump_pipe_config(intel_crtc, pipe_config,
  10093. "[hw state]");
  10094. intel_dump_pipe_config(intel_crtc, sw_config,
  10095. "[sw state]");
  10096. }
  10097. }
  10098. static void
  10099. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10100. struct intel_shared_dpll *pll,
  10101. struct drm_crtc *crtc,
  10102. struct drm_crtc_state *new_state)
  10103. {
  10104. struct intel_dpll_hw_state dpll_hw_state;
  10105. unsigned crtc_mask;
  10106. bool active;
  10107. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10108. DRM_DEBUG_KMS("%s\n", pll->name);
  10109. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10110. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10111. I915_STATE_WARN(!pll->on && pll->active_mask,
  10112. "pll in active use but not on in sw tracking\n");
  10113. I915_STATE_WARN(pll->on && !pll->active_mask,
  10114. "pll is on but not used by any active crtc\n");
  10115. I915_STATE_WARN(pll->on != active,
  10116. "pll on state mismatch (expected %i, found %i)\n",
  10117. pll->on, active);
  10118. }
  10119. if (!crtc) {
  10120. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10121. "more active pll users than references: %x vs %x\n",
  10122. pll->active_mask, pll->state.crtc_mask);
  10123. return;
  10124. }
  10125. crtc_mask = 1 << drm_crtc_index(crtc);
  10126. if (new_state->active)
  10127. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10128. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10129. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10130. else
  10131. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10132. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10133. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10134. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10135. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10136. crtc_mask, pll->state.crtc_mask);
  10137. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10138. &dpll_hw_state,
  10139. sizeof(dpll_hw_state)),
  10140. "pll hw state mismatch\n");
  10141. }
  10142. static void
  10143. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10144. struct drm_crtc_state *old_crtc_state,
  10145. struct drm_crtc_state *new_crtc_state)
  10146. {
  10147. struct drm_i915_private *dev_priv = to_i915(dev);
  10148. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10149. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10150. if (new_state->shared_dpll)
  10151. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10152. if (old_state->shared_dpll &&
  10153. old_state->shared_dpll != new_state->shared_dpll) {
  10154. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10155. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10156. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10157. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10158. pipe_name(drm_crtc_index(crtc)));
  10159. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10160. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10161. pipe_name(drm_crtc_index(crtc)));
  10162. }
  10163. }
  10164. static void
  10165. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10166. struct drm_atomic_state *state,
  10167. struct drm_crtc_state *old_state,
  10168. struct drm_crtc_state *new_state)
  10169. {
  10170. if (!needs_modeset(new_state) &&
  10171. !to_intel_crtc_state(new_state)->update_pipe)
  10172. return;
  10173. verify_wm_state(crtc, new_state);
  10174. verify_connector_state(crtc->dev, state, crtc);
  10175. verify_crtc_state(crtc, old_state, new_state);
  10176. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10177. }
  10178. static void
  10179. verify_disabled_dpll_state(struct drm_device *dev)
  10180. {
  10181. struct drm_i915_private *dev_priv = to_i915(dev);
  10182. int i;
  10183. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10184. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10185. }
  10186. static void
  10187. intel_modeset_verify_disabled(struct drm_device *dev,
  10188. struct drm_atomic_state *state)
  10189. {
  10190. verify_encoder_state(dev, state);
  10191. verify_connector_state(dev, state, NULL);
  10192. verify_disabled_dpll_state(dev);
  10193. }
  10194. static void update_scanline_offset(struct intel_crtc *crtc)
  10195. {
  10196. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10197. /*
  10198. * The scanline counter increments at the leading edge of hsync.
  10199. *
  10200. * On most platforms it starts counting from vtotal-1 on the
  10201. * first active line. That means the scanline counter value is
  10202. * always one less than what we would expect. Ie. just after
  10203. * start of vblank, which also occurs at start of hsync (on the
  10204. * last active line), the scanline counter will read vblank_start-1.
  10205. *
  10206. * On gen2 the scanline counter starts counting from 1 instead
  10207. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10208. * to keep the value positive), instead of adding one.
  10209. *
  10210. * On HSW+ the behaviour of the scanline counter depends on the output
  10211. * type. For DP ports it behaves like most other platforms, but on HDMI
  10212. * there's an extra 1 line difference. So we need to add two instead of
  10213. * one to the value.
  10214. */
  10215. if (IS_GEN2(dev_priv)) {
  10216. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10217. int vtotal;
  10218. vtotal = adjusted_mode->crtc_vtotal;
  10219. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10220. vtotal /= 2;
  10221. crtc->scanline_offset = vtotal - 1;
  10222. } else if (HAS_DDI(dev_priv) &&
  10223. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10224. crtc->scanline_offset = 2;
  10225. } else
  10226. crtc->scanline_offset = 1;
  10227. }
  10228. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10229. {
  10230. struct drm_device *dev = state->dev;
  10231. struct drm_i915_private *dev_priv = to_i915(dev);
  10232. struct drm_crtc *crtc;
  10233. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10234. int i;
  10235. if (!dev_priv->display.crtc_compute_clock)
  10236. return;
  10237. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10238. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10239. struct intel_shared_dpll *old_dpll =
  10240. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10241. if (!needs_modeset(new_crtc_state))
  10242. continue;
  10243. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10244. if (!old_dpll)
  10245. continue;
  10246. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10247. }
  10248. }
  10249. /*
  10250. * This implements the workaround described in the "notes" section of the mode
  10251. * set sequence documentation. When going from no pipes or single pipe to
  10252. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10253. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10254. */
  10255. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10256. {
  10257. struct drm_crtc_state *crtc_state;
  10258. struct intel_crtc *intel_crtc;
  10259. struct drm_crtc *crtc;
  10260. struct intel_crtc_state *first_crtc_state = NULL;
  10261. struct intel_crtc_state *other_crtc_state = NULL;
  10262. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10263. int i;
  10264. /* look at all crtc's that are going to be enabled in during modeset */
  10265. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10266. intel_crtc = to_intel_crtc(crtc);
  10267. if (!crtc_state->active || !needs_modeset(crtc_state))
  10268. continue;
  10269. if (first_crtc_state) {
  10270. other_crtc_state = to_intel_crtc_state(crtc_state);
  10271. break;
  10272. } else {
  10273. first_crtc_state = to_intel_crtc_state(crtc_state);
  10274. first_pipe = intel_crtc->pipe;
  10275. }
  10276. }
  10277. /* No workaround needed? */
  10278. if (!first_crtc_state)
  10279. return 0;
  10280. /* w/a possibly needed, check how many crtc's are already enabled. */
  10281. for_each_intel_crtc(state->dev, intel_crtc) {
  10282. struct intel_crtc_state *pipe_config;
  10283. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10284. if (IS_ERR(pipe_config))
  10285. return PTR_ERR(pipe_config);
  10286. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10287. if (!pipe_config->base.active ||
  10288. needs_modeset(&pipe_config->base))
  10289. continue;
  10290. /* 2 or more enabled crtcs means no need for w/a */
  10291. if (enabled_pipe != INVALID_PIPE)
  10292. return 0;
  10293. enabled_pipe = intel_crtc->pipe;
  10294. }
  10295. if (enabled_pipe != INVALID_PIPE)
  10296. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10297. else if (other_crtc_state)
  10298. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10299. return 0;
  10300. }
  10301. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10302. {
  10303. struct drm_crtc *crtc;
  10304. /* Add all pipes to the state */
  10305. for_each_crtc(state->dev, crtc) {
  10306. struct drm_crtc_state *crtc_state;
  10307. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10308. if (IS_ERR(crtc_state))
  10309. return PTR_ERR(crtc_state);
  10310. }
  10311. return 0;
  10312. }
  10313. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10314. {
  10315. struct drm_crtc *crtc;
  10316. /*
  10317. * Add all pipes to the state, and force
  10318. * a modeset on all the active ones.
  10319. */
  10320. for_each_crtc(state->dev, crtc) {
  10321. struct drm_crtc_state *crtc_state;
  10322. int ret;
  10323. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10324. if (IS_ERR(crtc_state))
  10325. return PTR_ERR(crtc_state);
  10326. if (!crtc_state->active || needs_modeset(crtc_state))
  10327. continue;
  10328. crtc_state->mode_changed = true;
  10329. ret = drm_atomic_add_affected_connectors(state, crtc);
  10330. if (ret)
  10331. return ret;
  10332. ret = drm_atomic_add_affected_planes(state, crtc);
  10333. if (ret)
  10334. return ret;
  10335. }
  10336. return 0;
  10337. }
  10338. static int intel_modeset_checks(struct drm_atomic_state *state)
  10339. {
  10340. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10341. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10342. struct drm_crtc *crtc;
  10343. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10344. int ret = 0, i;
  10345. if (!check_digital_port_conflicts(state)) {
  10346. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10347. return -EINVAL;
  10348. }
  10349. intel_state->modeset = true;
  10350. intel_state->active_crtcs = dev_priv->active_crtcs;
  10351. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10352. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10353. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10354. if (new_crtc_state->active)
  10355. intel_state->active_crtcs |= 1 << i;
  10356. else
  10357. intel_state->active_crtcs &= ~(1 << i);
  10358. if (old_crtc_state->active != new_crtc_state->active)
  10359. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10360. }
  10361. /*
  10362. * See if the config requires any additional preparation, e.g.
  10363. * to adjust global state with pipes off. We need to do this
  10364. * here so we can get the modeset_pipe updated config for the new
  10365. * mode set on this crtc. For other crtcs we need to use the
  10366. * adjusted_mode bits in the crtc directly.
  10367. */
  10368. if (dev_priv->display.modeset_calc_cdclk) {
  10369. ret = dev_priv->display.modeset_calc_cdclk(state);
  10370. if (ret < 0)
  10371. return ret;
  10372. /*
  10373. * Writes to dev_priv->cdclk.logical must protected by
  10374. * holding all the crtc locks, even if we don't end up
  10375. * touching the hardware
  10376. */
  10377. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10378. &intel_state->cdclk.logical)) {
  10379. ret = intel_lock_all_pipes(state);
  10380. if (ret < 0)
  10381. return ret;
  10382. }
  10383. /* All pipes must be switched off while we change the cdclk. */
  10384. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10385. &intel_state->cdclk.actual)) {
  10386. ret = intel_modeset_all_pipes(state);
  10387. if (ret < 0)
  10388. return ret;
  10389. }
  10390. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10391. intel_state->cdclk.logical.cdclk,
  10392. intel_state->cdclk.actual.cdclk);
  10393. } else {
  10394. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10395. }
  10396. intel_modeset_clear_plls(state);
  10397. if (IS_HASWELL(dev_priv))
  10398. return haswell_mode_set_planes_workaround(state);
  10399. return 0;
  10400. }
  10401. /*
  10402. * Handle calculation of various watermark data at the end of the atomic check
  10403. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10404. * handlers to ensure that all derived state has been updated.
  10405. */
  10406. static int calc_watermark_data(struct drm_atomic_state *state)
  10407. {
  10408. struct drm_device *dev = state->dev;
  10409. struct drm_i915_private *dev_priv = to_i915(dev);
  10410. /* Is there platform-specific watermark information to calculate? */
  10411. if (dev_priv->display.compute_global_watermarks)
  10412. return dev_priv->display.compute_global_watermarks(state);
  10413. return 0;
  10414. }
  10415. /**
  10416. * intel_atomic_check - validate state object
  10417. * @dev: drm device
  10418. * @state: state to validate
  10419. */
  10420. static int intel_atomic_check(struct drm_device *dev,
  10421. struct drm_atomic_state *state)
  10422. {
  10423. struct drm_i915_private *dev_priv = to_i915(dev);
  10424. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10425. struct drm_crtc *crtc;
  10426. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10427. int ret, i;
  10428. bool any_ms = false;
  10429. ret = drm_atomic_helper_check_modeset(dev, state);
  10430. if (ret)
  10431. return ret;
  10432. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10433. struct intel_crtc_state *pipe_config =
  10434. to_intel_crtc_state(crtc_state);
  10435. /* Catch I915_MODE_FLAG_INHERITED */
  10436. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10437. crtc_state->mode_changed = true;
  10438. if (!needs_modeset(crtc_state))
  10439. continue;
  10440. if (!crtc_state->enable) {
  10441. any_ms = true;
  10442. continue;
  10443. }
  10444. /* FIXME: For only active_changed we shouldn't need to do any
  10445. * state recomputation at all. */
  10446. ret = drm_atomic_add_affected_connectors(state, crtc);
  10447. if (ret)
  10448. return ret;
  10449. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10450. if (ret) {
  10451. intel_dump_pipe_config(to_intel_crtc(crtc),
  10452. pipe_config, "[failed]");
  10453. return ret;
  10454. }
  10455. if (i915.fastboot &&
  10456. intel_pipe_config_compare(dev_priv,
  10457. to_intel_crtc_state(old_crtc_state),
  10458. pipe_config, true)) {
  10459. crtc_state->mode_changed = false;
  10460. pipe_config->update_pipe = true;
  10461. }
  10462. if (needs_modeset(crtc_state))
  10463. any_ms = true;
  10464. ret = drm_atomic_add_affected_planes(state, crtc);
  10465. if (ret)
  10466. return ret;
  10467. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10468. needs_modeset(crtc_state) ?
  10469. "[modeset]" : "[fastset]");
  10470. }
  10471. if (any_ms) {
  10472. ret = intel_modeset_checks(state);
  10473. if (ret)
  10474. return ret;
  10475. } else {
  10476. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10477. }
  10478. ret = drm_atomic_helper_check_planes(dev, state);
  10479. if (ret)
  10480. return ret;
  10481. intel_fbc_choose_crtc(dev_priv, state);
  10482. return calc_watermark_data(state);
  10483. }
  10484. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10485. struct drm_atomic_state *state)
  10486. {
  10487. struct drm_i915_private *dev_priv = to_i915(dev);
  10488. struct drm_crtc_state *crtc_state;
  10489. struct drm_crtc *crtc;
  10490. int i, ret;
  10491. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10492. if (state->legacy_cursor_update)
  10493. continue;
  10494. ret = intel_crtc_wait_for_pending_flips(crtc);
  10495. if (ret)
  10496. return ret;
  10497. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10498. flush_workqueue(dev_priv->wq);
  10499. }
  10500. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10501. if (ret)
  10502. return ret;
  10503. ret = drm_atomic_helper_prepare_planes(dev, state);
  10504. mutex_unlock(&dev->struct_mutex);
  10505. return ret;
  10506. }
  10507. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10508. {
  10509. struct drm_device *dev = crtc->base.dev;
  10510. if (!dev->max_vblank_count)
  10511. return drm_accurate_vblank_count(&crtc->base);
  10512. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10513. }
  10514. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10515. struct drm_i915_private *dev_priv,
  10516. unsigned crtc_mask)
  10517. {
  10518. unsigned last_vblank_count[I915_MAX_PIPES];
  10519. enum pipe pipe;
  10520. int ret;
  10521. if (!crtc_mask)
  10522. return;
  10523. for_each_pipe(dev_priv, pipe) {
  10524. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10525. pipe);
  10526. if (!((1 << pipe) & crtc_mask))
  10527. continue;
  10528. ret = drm_crtc_vblank_get(&crtc->base);
  10529. if (WARN_ON(ret != 0)) {
  10530. crtc_mask &= ~(1 << pipe);
  10531. continue;
  10532. }
  10533. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10534. }
  10535. for_each_pipe(dev_priv, pipe) {
  10536. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10537. pipe);
  10538. long lret;
  10539. if (!((1 << pipe) & crtc_mask))
  10540. continue;
  10541. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10542. last_vblank_count[pipe] !=
  10543. drm_crtc_vblank_count(&crtc->base),
  10544. msecs_to_jiffies(50));
  10545. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10546. drm_crtc_vblank_put(&crtc->base);
  10547. }
  10548. }
  10549. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10550. {
  10551. /* fb updated, need to unpin old fb */
  10552. if (crtc_state->fb_changed)
  10553. return true;
  10554. /* wm changes, need vblank before final wm's */
  10555. if (crtc_state->update_wm_post)
  10556. return true;
  10557. if (crtc_state->wm.need_postvbl_update)
  10558. return true;
  10559. return false;
  10560. }
  10561. static void intel_update_crtc(struct drm_crtc *crtc,
  10562. struct drm_atomic_state *state,
  10563. struct drm_crtc_state *old_crtc_state,
  10564. struct drm_crtc_state *new_crtc_state,
  10565. unsigned int *crtc_vblank_mask)
  10566. {
  10567. struct drm_device *dev = crtc->dev;
  10568. struct drm_i915_private *dev_priv = to_i915(dev);
  10569. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10570. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10571. bool modeset = needs_modeset(new_crtc_state);
  10572. if (modeset) {
  10573. update_scanline_offset(intel_crtc);
  10574. dev_priv->display.crtc_enable(pipe_config, state);
  10575. } else {
  10576. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10577. pipe_config);
  10578. }
  10579. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10580. intel_fbc_enable(
  10581. intel_crtc, pipe_config,
  10582. to_intel_plane_state(crtc->primary->state));
  10583. }
  10584. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10585. if (needs_vblank_wait(pipe_config))
  10586. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10587. }
  10588. static void intel_update_crtcs(struct drm_atomic_state *state,
  10589. unsigned int *crtc_vblank_mask)
  10590. {
  10591. struct drm_crtc *crtc;
  10592. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10593. int i;
  10594. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10595. if (!new_crtc_state->active)
  10596. continue;
  10597. intel_update_crtc(crtc, state, old_crtc_state,
  10598. new_crtc_state, crtc_vblank_mask);
  10599. }
  10600. }
  10601. static void skl_update_crtcs(struct drm_atomic_state *state,
  10602. unsigned int *crtc_vblank_mask)
  10603. {
  10604. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10605. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10606. struct drm_crtc *crtc;
  10607. struct intel_crtc *intel_crtc;
  10608. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10609. struct intel_crtc_state *cstate;
  10610. unsigned int updated = 0;
  10611. bool progress;
  10612. enum pipe pipe;
  10613. int i;
  10614. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10615. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10616. /* ignore allocations for crtc's that have been turned off. */
  10617. if (new_crtc_state->active)
  10618. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10619. /*
  10620. * Whenever the number of active pipes changes, we need to make sure we
  10621. * update the pipes in the right order so that their ddb allocations
  10622. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10623. * cause pipe underruns and other bad stuff.
  10624. */
  10625. do {
  10626. progress = false;
  10627. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10628. bool vbl_wait = false;
  10629. unsigned int cmask = drm_crtc_mask(crtc);
  10630. intel_crtc = to_intel_crtc(crtc);
  10631. cstate = to_intel_crtc_state(crtc->state);
  10632. pipe = intel_crtc->pipe;
  10633. if (updated & cmask || !cstate->base.active)
  10634. continue;
  10635. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10636. continue;
  10637. updated |= cmask;
  10638. entries[i] = &cstate->wm.skl.ddb;
  10639. /*
  10640. * If this is an already active pipe, it's DDB changed,
  10641. * and this isn't the last pipe that needs updating
  10642. * then we need to wait for a vblank to pass for the
  10643. * new ddb allocation to take effect.
  10644. */
  10645. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10646. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10647. !new_crtc_state->active_changed &&
  10648. intel_state->wm_results.dirty_pipes != updated)
  10649. vbl_wait = true;
  10650. intel_update_crtc(crtc, state, old_crtc_state,
  10651. new_crtc_state, crtc_vblank_mask);
  10652. if (vbl_wait)
  10653. intel_wait_for_vblank(dev_priv, pipe);
  10654. progress = true;
  10655. }
  10656. } while (progress);
  10657. }
  10658. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10659. {
  10660. struct intel_atomic_state *state, *next;
  10661. struct llist_node *freed;
  10662. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10663. llist_for_each_entry_safe(state, next, freed, freed)
  10664. drm_atomic_state_put(&state->base);
  10665. }
  10666. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10667. {
  10668. struct drm_i915_private *dev_priv =
  10669. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10670. intel_atomic_helper_free_state(dev_priv);
  10671. }
  10672. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10673. {
  10674. struct drm_device *dev = state->dev;
  10675. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10676. struct drm_i915_private *dev_priv = to_i915(dev);
  10677. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10678. struct drm_crtc *crtc;
  10679. struct intel_crtc_state *intel_cstate;
  10680. bool hw_check = intel_state->modeset;
  10681. u64 put_domains[I915_MAX_PIPES] = {};
  10682. unsigned crtc_vblank_mask = 0;
  10683. int i;
  10684. drm_atomic_helper_wait_for_dependencies(state);
  10685. if (intel_state->modeset)
  10686. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10687. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10689. if (needs_modeset(new_crtc_state) ||
  10690. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10691. hw_check = true;
  10692. put_domains[to_intel_crtc(crtc)->pipe] =
  10693. modeset_get_crtc_power_domains(crtc,
  10694. to_intel_crtc_state(new_crtc_state));
  10695. }
  10696. if (!needs_modeset(new_crtc_state))
  10697. continue;
  10698. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10699. to_intel_crtc_state(new_crtc_state));
  10700. if (old_crtc_state->active) {
  10701. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10702. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10703. intel_crtc->active = false;
  10704. intel_fbc_disable(intel_crtc);
  10705. intel_disable_shared_dpll(intel_crtc);
  10706. /*
  10707. * Underruns don't always raise
  10708. * interrupts, so check manually.
  10709. */
  10710. intel_check_cpu_fifo_underruns(dev_priv);
  10711. intel_check_pch_fifo_underruns(dev_priv);
  10712. if (!crtc->state->active) {
  10713. /*
  10714. * Make sure we don't call initial_watermarks
  10715. * for ILK-style watermark updates.
  10716. *
  10717. * No clue what this is supposed to achieve.
  10718. */
  10719. if (INTEL_GEN(dev_priv) >= 9)
  10720. dev_priv->display.initial_watermarks(intel_state,
  10721. to_intel_crtc_state(crtc->state));
  10722. }
  10723. }
  10724. }
  10725. /* Only after disabling all output pipelines that will be changed can we
  10726. * update the the output configuration. */
  10727. intel_modeset_update_crtc_state(state);
  10728. if (intel_state->modeset) {
  10729. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10730. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10731. /*
  10732. * SKL workaround: bspec recommends we disable the SAGV when we
  10733. * have more then one pipe enabled
  10734. */
  10735. if (!intel_can_enable_sagv(state))
  10736. intel_disable_sagv(dev_priv);
  10737. intel_modeset_verify_disabled(dev, state);
  10738. }
  10739. /* Complete the events for pipes that have now been disabled */
  10740. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10741. bool modeset = needs_modeset(new_crtc_state);
  10742. /* Complete events for now disable pipes here. */
  10743. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10744. spin_lock_irq(&dev->event_lock);
  10745. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10746. spin_unlock_irq(&dev->event_lock);
  10747. new_crtc_state->event = NULL;
  10748. }
  10749. }
  10750. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10751. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10752. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10753. * already, but still need the state for the delayed optimization. To
  10754. * fix this:
  10755. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10756. * - schedule that vblank worker _before_ calling hw_done
  10757. * - at the start of commit_tail, cancel it _synchrously
  10758. * - switch over to the vblank wait helper in the core after that since
  10759. * we don't need out special handling any more.
  10760. */
  10761. if (!state->legacy_cursor_update)
  10762. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10763. /*
  10764. * Now that the vblank has passed, we can go ahead and program the
  10765. * optimal watermarks on platforms that need two-step watermark
  10766. * programming.
  10767. *
  10768. * TODO: Move this (and other cleanup) to an async worker eventually.
  10769. */
  10770. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10771. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10772. if (dev_priv->display.optimize_watermarks)
  10773. dev_priv->display.optimize_watermarks(intel_state,
  10774. intel_cstate);
  10775. }
  10776. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10777. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10778. if (put_domains[i])
  10779. modeset_put_power_domains(dev_priv, put_domains[i]);
  10780. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10781. }
  10782. if (intel_state->modeset && intel_can_enable_sagv(state))
  10783. intel_enable_sagv(dev_priv);
  10784. drm_atomic_helper_commit_hw_done(state);
  10785. if (intel_state->modeset)
  10786. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10787. mutex_lock(&dev->struct_mutex);
  10788. drm_atomic_helper_cleanup_planes(dev, state);
  10789. mutex_unlock(&dev->struct_mutex);
  10790. drm_atomic_helper_commit_cleanup_done(state);
  10791. drm_atomic_state_put(state);
  10792. /* As one of the primary mmio accessors, KMS has a high likelihood
  10793. * of triggering bugs in unclaimed access. After we finish
  10794. * modesetting, see if an error has been flagged, and if so
  10795. * enable debugging for the next modeset - and hope we catch
  10796. * the culprit.
  10797. *
  10798. * XXX note that we assume display power is on at this point.
  10799. * This might hold true now but we need to add pm helper to check
  10800. * unclaimed only when the hardware is on, as atomic commits
  10801. * can happen also when the device is completely off.
  10802. */
  10803. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10804. intel_atomic_helper_free_state(dev_priv);
  10805. }
  10806. static void intel_atomic_commit_work(struct work_struct *work)
  10807. {
  10808. struct drm_atomic_state *state =
  10809. container_of(work, struct drm_atomic_state, commit_work);
  10810. intel_atomic_commit_tail(state);
  10811. }
  10812. static int __i915_sw_fence_call
  10813. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10814. enum i915_sw_fence_notify notify)
  10815. {
  10816. struct intel_atomic_state *state =
  10817. container_of(fence, struct intel_atomic_state, commit_ready);
  10818. switch (notify) {
  10819. case FENCE_COMPLETE:
  10820. if (state->base.commit_work.func)
  10821. queue_work(system_unbound_wq, &state->base.commit_work);
  10822. break;
  10823. case FENCE_FREE:
  10824. {
  10825. struct intel_atomic_helper *helper =
  10826. &to_i915(state->base.dev)->atomic_helper;
  10827. if (llist_add(&state->freed, &helper->free_list))
  10828. schedule_work(&helper->free_work);
  10829. break;
  10830. }
  10831. }
  10832. return NOTIFY_DONE;
  10833. }
  10834. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10835. {
  10836. struct drm_plane_state *old_plane_state, *new_plane_state;
  10837. struct drm_plane *plane;
  10838. int i;
  10839. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10840. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10841. intel_fb_obj(new_plane_state->fb),
  10842. to_intel_plane(plane)->frontbuffer_bit);
  10843. }
  10844. /**
  10845. * intel_atomic_commit - commit validated state object
  10846. * @dev: DRM device
  10847. * @state: the top-level driver state object
  10848. * @nonblock: nonblocking commit
  10849. *
  10850. * This function commits a top-level state object that has been validated
  10851. * with drm_atomic_helper_check().
  10852. *
  10853. * RETURNS
  10854. * Zero for success or -errno.
  10855. */
  10856. static int intel_atomic_commit(struct drm_device *dev,
  10857. struct drm_atomic_state *state,
  10858. bool nonblock)
  10859. {
  10860. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10861. struct drm_i915_private *dev_priv = to_i915(dev);
  10862. int ret = 0;
  10863. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10864. if (ret)
  10865. return ret;
  10866. drm_atomic_state_get(state);
  10867. i915_sw_fence_init(&intel_state->commit_ready,
  10868. intel_atomic_commit_ready);
  10869. ret = intel_atomic_prepare_commit(dev, state);
  10870. if (ret) {
  10871. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10872. i915_sw_fence_commit(&intel_state->commit_ready);
  10873. return ret;
  10874. }
  10875. /*
  10876. * The intel_legacy_cursor_update() fast path takes care
  10877. * of avoiding the vblank waits for simple cursor
  10878. * movement and flips. For cursor on/off and size changes,
  10879. * we want to perform the vblank waits so that watermark
  10880. * updates happen during the correct frames. Gen9+ have
  10881. * double buffered watermarks and so shouldn't need this.
  10882. *
  10883. * Do this after drm_atomic_helper_setup_commit() and
  10884. * intel_atomic_prepare_commit() because we still want
  10885. * to skip the flip and fb cleanup waits. Although that
  10886. * does risk yanking the mapping from under the display
  10887. * engine.
  10888. *
  10889. * FIXME doing watermarks and fb cleanup from a vblank worker
  10890. * (assuming we had any) would solve these problems.
  10891. */
  10892. if (INTEL_GEN(dev_priv) < 9)
  10893. state->legacy_cursor_update = false;
  10894. drm_atomic_helper_swap_state(state, true);
  10895. dev_priv->wm.distrust_bios_wm = false;
  10896. intel_shared_dpll_swap_state(state);
  10897. intel_atomic_track_fbs(state);
  10898. if (intel_state->modeset) {
  10899. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10900. sizeof(intel_state->min_pixclk));
  10901. dev_priv->active_crtcs = intel_state->active_crtcs;
  10902. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10903. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10904. }
  10905. drm_atomic_state_get(state);
  10906. INIT_WORK(&state->commit_work,
  10907. nonblock ? intel_atomic_commit_work : NULL);
  10908. i915_sw_fence_commit(&intel_state->commit_ready);
  10909. if (!nonblock) {
  10910. i915_sw_fence_wait(&intel_state->commit_ready);
  10911. intel_atomic_commit_tail(state);
  10912. }
  10913. return 0;
  10914. }
  10915. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10916. {
  10917. struct drm_device *dev = crtc->dev;
  10918. struct drm_atomic_state *state;
  10919. struct drm_crtc_state *crtc_state;
  10920. int ret;
  10921. state = drm_atomic_state_alloc(dev);
  10922. if (!state) {
  10923. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  10924. crtc->base.id, crtc->name);
  10925. return;
  10926. }
  10927. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  10928. retry:
  10929. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10930. ret = PTR_ERR_OR_ZERO(crtc_state);
  10931. if (!ret) {
  10932. if (!crtc_state->active)
  10933. goto out;
  10934. crtc_state->mode_changed = true;
  10935. ret = drm_atomic_commit(state);
  10936. }
  10937. if (ret == -EDEADLK) {
  10938. drm_atomic_state_clear(state);
  10939. drm_modeset_backoff(state->acquire_ctx);
  10940. goto retry;
  10941. }
  10942. out:
  10943. drm_atomic_state_put(state);
  10944. }
  10945. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10946. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10947. .set_config = drm_atomic_helper_set_config,
  10948. .set_property = drm_atomic_helper_crtc_set_property,
  10949. .destroy = intel_crtc_destroy,
  10950. .page_flip = drm_atomic_helper_page_flip,
  10951. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10952. .atomic_destroy_state = intel_crtc_destroy_state,
  10953. .set_crc_source = intel_crtc_set_crc_source,
  10954. };
  10955. /**
  10956. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10957. * @plane: drm plane to prepare for
  10958. * @fb: framebuffer to prepare for presentation
  10959. *
  10960. * Prepares a framebuffer for usage on a display plane. Generally this
  10961. * involves pinning the underlying object and updating the frontbuffer tracking
  10962. * bits. Some older platforms need special physical address handling for
  10963. * cursor planes.
  10964. *
  10965. * Must be called with struct_mutex held.
  10966. *
  10967. * Returns 0 on success, negative error code on failure.
  10968. */
  10969. int
  10970. intel_prepare_plane_fb(struct drm_plane *plane,
  10971. struct drm_plane_state *new_state)
  10972. {
  10973. struct intel_atomic_state *intel_state =
  10974. to_intel_atomic_state(new_state->state);
  10975. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10976. struct drm_framebuffer *fb = new_state->fb;
  10977. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10978. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10979. int ret;
  10980. if (obj) {
  10981. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10982. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10983. const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  10984. ret = i915_gem_object_attach_phys(obj, align);
  10985. if (ret) {
  10986. DRM_DEBUG_KMS("failed to attach phys object\n");
  10987. return ret;
  10988. }
  10989. } else {
  10990. struct i915_vma *vma;
  10991. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10992. if (IS_ERR(vma)) {
  10993. DRM_DEBUG_KMS("failed to pin object\n");
  10994. return PTR_ERR(vma);
  10995. }
  10996. to_intel_plane_state(new_state)->vma = vma;
  10997. }
  10998. }
  10999. if (!obj && !old_obj)
  11000. return 0;
  11001. if (old_obj) {
  11002. struct drm_crtc_state *crtc_state =
  11003. drm_atomic_get_existing_crtc_state(new_state->state,
  11004. plane->state->crtc);
  11005. /* Big Hammer, we also need to ensure that any pending
  11006. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  11007. * current scanout is retired before unpinning the old
  11008. * framebuffer. Note that we rely on userspace rendering
  11009. * into the buffer attached to the pipe they are waiting
  11010. * on. If not, userspace generates a GPU hang with IPEHR
  11011. * point to the MI_WAIT_FOR_EVENT.
  11012. *
  11013. * This should only fail upon a hung GPU, in which case we
  11014. * can safely continue.
  11015. */
  11016. if (needs_modeset(crtc_state)) {
  11017. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11018. old_obj->resv, NULL,
  11019. false, 0,
  11020. GFP_KERNEL);
  11021. if (ret < 0)
  11022. return ret;
  11023. }
  11024. }
  11025. if (new_state->fence) { /* explicit fencing */
  11026. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11027. new_state->fence,
  11028. I915_FENCE_TIMEOUT,
  11029. GFP_KERNEL);
  11030. if (ret < 0)
  11031. return ret;
  11032. }
  11033. if (!obj)
  11034. return 0;
  11035. if (!new_state->fence) { /* implicit fencing */
  11036. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11037. obj->resv, NULL,
  11038. false, I915_FENCE_TIMEOUT,
  11039. GFP_KERNEL);
  11040. if (ret < 0)
  11041. return ret;
  11042. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11043. }
  11044. return 0;
  11045. }
  11046. /**
  11047. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11048. * @plane: drm plane to clean up for
  11049. * @fb: old framebuffer that was on plane
  11050. *
  11051. * Cleans up a framebuffer that has just been removed from a plane.
  11052. *
  11053. * Must be called with struct_mutex held.
  11054. */
  11055. void
  11056. intel_cleanup_plane_fb(struct drm_plane *plane,
  11057. struct drm_plane_state *old_state)
  11058. {
  11059. struct i915_vma *vma;
  11060. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11061. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11062. if (vma)
  11063. intel_unpin_fb_vma(vma);
  11064. }
  11065. int
  11066. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11067. {
  11068. struct drm_i915_private *dev_priv;
  11069. int max_scale;
  11070. int crtc_clock, max_dotclk;
  11071. if (!intel_crtc || !crtc_state->base.enable)
  11072. return DRM_PLANE_HELPER_NO_SCALING;
  11073. dev_priv = to_i915(intel_crtc->base.dev);
  11074. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11075. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11076. if (IS_GEMINILAKE(dev_priv))
  11077. max_dotclk *= 2;
  11078. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11079. return DRM_PLANE_HELPER_NO_SCALING;
  11080. /*
  11081. * skl max scale is lower of:
  11082. * close to 3 but not 3, -1 is for that purpose
  11083. * or
  11084. * cdclk/crtc_clock
  11085. */
  11086. max_scale = min((1 << 16) * 3 - 1,
  11087. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  11088. return max_scale;
  11089. }
  11090. static int
  11091. intel_check_primary_plane(struct intel_plane *plane,
  11092. struct intel_crtc_state *crtc_state,
  11093. struct intel_plane_state *state)
  11094. {
  11095. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  11096. struct drm_crtc *crtc = state->base.crtc;
  11097. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11098. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11099. bool can_position = false;
  11100. int ret;
  11101. if (INTEL_GEN(dev_priv) >= 9) {
  11102. /* use scaler when colorkey is not required */
  11103. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11104. min_scale = 1;
  11105. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11106. }
  11107. can_position = true;
  11108. }
  11109. ret = drm_plane_helper_check_state(&state->base,
  11110. &state->clip,
  11111. min_scale, max_scale,
  11112. can_position, true);
  11113. if (ret)
  11114. return ret;
  11115. if (!state->base.fb)
  11116. return 0;
  11117. if (INTEL_GEN(dev_priv) >= 9) {
  11118. ret = skl_check_plane_surface(state);
  11119. if (ret)
  11120. return ret;
  11121. state->ctl = skl_plane_ctl(crtc_state, state);
  11122. } else {
  11123. ret = i9xx_check_plane_surface(state);
  11124. if (ret)
  11125. return ret;
  11126. state->ctl = i9xx_plane_ctl(crtc_state, state);
  11127. }
  11128. return 0;
  11129. }
  11130. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11131. struct drm_crtc_state *old_crtc_state)
  11132. {
  11133. struct drm_device *dev = crtc->dev;
  11134. struct drm_i915_private *dev_priv = to_i915(dev);
  11135. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11136. struct intel_crtc_state *intel_cstate =
  11137. to_intel_crtc_state(crtc->state);
  11138. struct intel_crtc_state *old_intel_cstate =
  11139. to_intel_crtc_state(old_crtc_state);
  11140. struct intel_atomic_state *old_intel_state =
  11141. to_intel_atomic_state(old_crtc_state->state);
  11142. bool modeset = needs_modeset(crtc->state);
  11143. if (!modeset &&
  11144. (intel_cstate->base.color_mgmt_changed ||
  11145. intel_cstate->update_pipe)) {
  11146. intel_color_set_csc(crtc->state);
  11147. intel_color_load_luts(crtc->state);
  11148. }
  11149. /* Perform vblank evasion around commit operation */
  11150. intel_pipe_update_start(intel_crtc);
  11151. if (modeset)
  11152. goto out;
  11153. if (intel_cstate->update_pipe)
  11154. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11155. else if (INTEL_GEN(dev_priv) >= 9)
  11156. skl_detach_scalers(intel_crtc);
  11157. out:
  11158. if (dev_priv->display.atomic_update_watermarks)
  11159. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11160. intel_cstate);
  11161. }
  11162. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11163. struct drm_crtc_state *old_crtc_state)
  11164. {
  11165. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11166. intel_pipe_update_end(intel_crtc, NULL);
  11167. }
  11168. /**
  11169. * intel_plane_destroy - destroy a plane
  11170. * @plane: plane to destroy
  11171. *
  11172. * Common destruction function for all types of planes (primary, cursor,
  11173. * sprite).
  11174. */
  11175. void intel_plane_destroy(struct drm_plane *plane)
  11176. {
  11177. drm_plane_cleanup(plane);
  11178. kfree(to_intel_plane(plane));
  11179. }
  11180. const struct drm_plane_funcs intel_plane_funcs = {
  11181. .update_plane = drm_atomic_helper_update_plane,
  11182. .disable_plane = drm_atomic_helper_disable_plane,
  11183. .destroy = intel_plane_destroy,
  11184. .set_property = drm_atomic_helper_plane_set_property,
  11185. .atomic_get_property = intel_plane_atomic_get_property,
  11186. .atomic_set_property = intel_plane_atomic_set_property,
  11187. .atomic_duplicate_state = intel_plane_duplicate_state,
  11188. .atomic_destroy_state = intel_plane_destroy_state,
  11189. };
  11190. static int
  11191. intel_legacy_cursor_update(struct drm_plane *plane,
  11192. struct drm_crtc *crtc,
  11193. struct drm_framebuffer *fb,
  11194. int crtc_x, int crtc_y,
  11195. unsigned int crtc_w, unsigned int crtc_h,
  11196. uint32_t src_x, uint32_t src_y,
  11197. uint32_t src_w, uint32_t src_h,
  11198. struct drm_modeset_acquire_ctx *ctx)
  11199. {
  11200. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11201. int ret;
  11202. struct drm_plane_state *old_plane_state, *new_plane_state;
  11203. struct intel_plane *intel_plane = to_intel_plane(plane);
  11204. struct drm_framebuffer *old_fb;
  11205. struct drm_crtc_state *crtc_state = crtc->state;
  11206. struct i915_vma *old_vma;
  11207. /*
  11208. * When crtc is inactive or there is a modeset pending,
  11209. * wait for it to complete in the slowpath
  11210. */
  11211. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11212. to_intel_crtc_state(crtc_state)->update_pipe)
  11213. goto slow;
  11214. old_plane_state = plane->state;
  11215. /*
  11216. * If any parameters change that may affect watermarks,
  11217. * take the slowpath. Only changing fb or position should be
  11218. * in the fastpath.
  11219. */
  11220. if (old_plane_state->crtc != crtc ||
  11221. old_plane_state->src_w != src_w ||
  11222. old_plane_state->src_h != src_h ||
  11223. old_plane_state->crtc_w != crtc_w ||
  11224. old_plane_state->crtc_h != crtc_h ||
  11225. !old_plane_state->fb != !fb)
  11226. goto slow;
  11227. new_plane_state = intel_plane_duplicate_state(plane);
  11228. if (!new_plane_state)
  11229. return -ENOMEM;
  11230. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11231. new_plane_state->src_x = src_x;
  11232. new_plane_state->src_y = src_y;
  11233. new_plane_state->src_w = src_w;
  11234. new_plane_state->src_h = src_h;
  11235. new_plane_state->crtc_x = crtc_x;
  11236. new_plane_state->crtc_y = crtc_y;
  11237. new_plane_state->crtc_w = crtc_w;
  11238. new_plane_state->crtc_h = crtc_h;
  11239. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11240. to_intel_plane_state(new_plane_state));
  11241. if (ret)
  11242. goto out_free;
  11243. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11244. if (ret)
  11245. goto out_free;
  11246. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11247. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11248. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11249. if (ret) {
  11250. DRM_DEBUG_KMS("failed to attach phys object\n");
  11251. goto out_unlock;
  11252. }
  11253. } else {
  11254. struct i915_vma *vma;
  11255. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11256. if (IS_ERR(vma)) {
  11257. DRM_DEBUG_KMS("failed to pin object\n");
  11258. ret = PTR_ERR(vma);
  11259. goto out_unlock;
  11260. }
  11261. to_intel_plane_state(new_plane_state)->vma = vma;
  11262. }
  11263. old_fb = old_plane_state->fb;
  11264. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11265. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11266. intel_plane->frontbuffer_bit);
  11267. /* Swap plane state */
  11268. new_plane_state->fence = old_plane_state->fence;
  11269. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11270. new_plane_state->fence = NULL;
  11271. new_plane_state->fb = old_fb;
  11272. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11273. if (plane->state->visible) {
  11274. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11275. intel_plane->update_plane(intel_plane,
  11276. to_intel_crtc_state(crtc->state),
  11277. to_intel_plane_state(plane->state));
  11278. } else {
  11279. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11280. intel_plane->disable_plane(intel_plane, to_intel_crtc(crtc));
  11281. }
  11282. intel_cleanup_plane_fb(plane, new_plane_state);
  11283. out_unlock:
  11284. mutex_unlock(&dev_priv->drm.struct_mutex);
  11285. out_free:
  11286. intel_plane_destroy_state(plane, new_plane_state);
  11287. return ret;
  11288. slow:
  11289. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11290. crtc_x, crtc_y, crtc_w, crtc_h,
  11291. src_x, src_y, src_w, src_h, ctx);
  11292. }
  11293. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11294. .update_plane = intel_legacy_cursor_update,
  11295. .disable_plane = drm_atomic_helper_disable_plane,
  11296. .destroy = intel_plane_destroy,
  11297. .set_property = drm_atomic_helper_plane_set_property,
  11298. .atomic_get_property = intel_plane_atomic_get_property,
  11299. .atomic_set_property = intel_plane_atomic_set_property,
  11300. .atomic_duplicate_state = intel_plane_duplicate_state,
  11301. .atomic_destroy_state = intel_plane_destroy_state,
  11302. };
  11303. static struct intel_plane *
  11304. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11305. {
  11306. struct intel_plane *primary = NULL;
  11307. struct intel_plane_state *state = NULL;
  11308. const uint32_t *intel_primary_formats;
  11309. unsigned int supported_rotations;
  11310. unsigned int num_formats;
  11311. int ret;
  11312. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11313. if (!primary) {
  11314. ret = -ENOMEM;
  11315. goto fail;
  11316. }
  11317. state = intel_create_plane_state(&primary->base);
  11318. if (!state) {
  11319. ret = -ENOMEM;
  11320. goto fail;
  11321. }
  11322. primary->base.state = &state->base;
  11323. primary->can_scale = false;
  11324. primary->max_downscale = 1;
  11325. if (INTEL_GEN(dev_priv) >= 9) {
  11326. primary->can_scale = true;
  11327. state->scaler_id = -1;
  11328. }
  11329. primary->pipe = pipe;
  11330. /*
  11331. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11332. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11333. */
  11334. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11335. primary->plane = (enum plane) !pipe;
  11336. else
  11337. primary->plane = (enum plane) pipe;
  11338. primary->id = PLANE_PRIMARY;
  11339. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11340. primary->check_plane = intel_check_primary_plane;
  11341. if (INTEL_GEN(dev_priv) >= 9) {
  11342. intel_primary_formats = skl_primary_formats;
  11343. num_formats = ARRAY_SIZE(skl_primary_formats);
  11344. primary->update_plane = skylake_update_primary_plane;
  11345. primary->disable_plane = skylake_disable_primary_plane;
  11346. } else if (INTEL_GEN(dev_priv) >= 4) {
  11347. intel_primary_formats = i965_primary_formats;
  11348. num_formats = ARRAY_SIZE(i965_primary_formats);
  11349. primary->update_plane = i9xx_update_primary_plane;
  11350. primary->disable_plane = i9xx_disable_primary_plane;
  11351. } else {
  11352. intel_primary_formats = i8xx_primary_formats;
  11353. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11354. primary->update_plane = i9xx_update_primary_plane;
  11355. primary->disable_plane = i9xx_disable_primary_plane;
  11356. }
  11357. if (INTEL_GEN(dev_priv) >= 9)
  11358. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11359. 0, &intel_plane_funcs,
  11360. intel_primary_formats, num_formats,
  11361. DRM_PLANE_TYPE_PRIMARY,
  11362. "plane 1%c", pipe_name(pipe));
  11363. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11364. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11365. 0, &intel_plane_funcs,
  11366. intel_primary_formats, num_formats,
  11367. DRM_PLANE_TYPE_PRIMARY,
  11368. "primary %c", pipe_name(pipe));
  11369. else
  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 %c", plane_name(primary->plane));
  11375. if (ret)
  11376. goto fail;
  11377. if (INTEL_GEN(dev_priv) >= 9) {
  11378. supported_rotations =
  11379. DRM_ROTATE_0 | DRM_ROTATE_90 |
  11380. DRM_ROTATE_180 | DRM_ROTATE_270;
  11381. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11382. supported_rotations =
  11383. DRM_ROTATE_0 | DRM_ROTATE_180 |
  11384. DRM_REFLECT_X;
  11385. } else if (INTEL_GEN(dev_priv) >= 4) {
  11386. supported_rotations =
  11387. DRM_ROTATE_0 | DRM_ROTATE_180;
  11388. } else {
  11389. supported_rotations = DRM_ROTATE_0;
  11390. }
  11391. if (INTEL_GEN(dev_priv) >= 4)
  11392. drm_plane_create_rotation_property(&primary->base,
  11393. DRM_ROTATE_0,
  11394. supported_rotations);
  11395. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11396. return primary;
  11397. fail:
  11398. kfree(state);
  11399. kfree(primary);
  11400. return ERR_PTR(ret);
  11401. }
  11402. static int
  11403. intel_check_cursor_plane(struct intel_plane *plane,
  11404. struct intel_crtc_state *crtc_state,
  11405. struct intel_plane_state *state)
  11406. {
  11407. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  11408. const struct drm_framebuffer *fb = state->base.fb;
  11409. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11410. enum pipe pipe = plane->pipe;
  11411. unsigned stride;
  11412. int ret;
  11413. ret = drm_plane_helper_check_state(&state->base,
  11414. &state->clip,
  11415. DRM_PLANE_HELPER_NO_SCALING,
  11416. DRM_PLANE_HELPER_NO_SCALING,
  11417. true, true);
  11418. if (ret)
  11419. return ret;
  11420. /* if we want to turn off the cursor ignore width and height */
  11421. if (!obj)
  11422. return 0;
  11423. /* Check for which cursor types we support */
  11424. if (!cursor_size_ok(dev_priv, state->base.crtc_w,
  11425. state->base.crtc_h)) {
  11426. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11427. state->base.crtc_w, state->base.crtc_h);
  11428. return -EINVAL;
  11429. }
  11430. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11431. if (obj->base.size < stride * state->base.crtc_h) {
  11432. DRM_DEBUG_KMS("buffer is too small\n");
  11433. return -ENOMEM;
  11434. }
  11435. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  11436. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11437. return -EINVAL;
  11438. }
  11439. /*
  11440. * There's something wrong with the cursor on CHV pipe C.
  11441. * If it straddles the left edge of the screen then
  11442. * moving it away from the edge or disabling it often
  11443. * results in a pipe underrun, and often that can lead to
  11444. * dead pipe (constant underrun reported, and it scans
  11445. * out just a solid color). To recover from that, the
  11446. * display power well must be turned off and on again.
  11447. * Refuse the put the cursor into that compromised position.
  11448. */
  11449. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  11450. state->base.visible && state->base.crtc_x < 0) {
  11451. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11452. return -EINVAL;
  11453. }
  11454. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  11455. state->ctl = i845_cursor_ctl(crtc_state, state);
  11456. else
  11457. state->ctl = i9xx_cursor_ctl(crtc_state, state);
  11458. return 0;
  11459. }
  11460. static struct intel_plane *
  11461. intel_cursor_plane_create(struct drm_i915_private *dev_priv,
  11462. enum pipe pipe)
  11463. {
  11464. struct intel_plane *cursor = NULL;
  11465. struct intel_plane_state *state = NULL;
  11466. int ret;
  11467. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11468. if (!cursor) {
  11469. ret = -ENOMEM;
  11470. goto fail;
  11471. }
  11472. state = intel_create_plane_state(&cursor->base);
  11473. if (!state) {
  11474. ret = -ENOMEM;
  11475. goto fail;
  11476. }
  11477. cursor->base.state = &state->base;
  11478. cursor->can_scale = false;
  11479. cursor->max_downscale = 1;
  11480. cursor->pipe = pipe;
  11481. cursor->plane = pipe;
  11482. cursor->id = PLANE_CURSOR;
  11483. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11484. cursor->check_plane = intel_check_cursor_plane;
  11485. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  11486. cursor->update_plane = i845_update_cursor;
  11487. cursor->disable_plane = i845_disable_cursor;
  11488. } else {
  11489. cursor->update_plane = i9xx_update_cursor;
  11490. cursor->disable_plane = i9xx_disable_cursor;
  11491. }
  11492. cursor->cursor.base = ~0;
  11493. cursor->cursor.cntl = ~0;
  11494. cursor->cursor.size = ~0;
  11495. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11496. 0, &intel_cursor_plane_funcs,
  11497. intel_cursor_formats,
  11498. ARRAY_SIZE(intel_cursor_formats),
  11499. DRM_PLANE_TYPE_CURSOR,
  11500. "cursor %c", pipe_name(pipe));
  11501. if (ret)
  11502. goto fail;
  11503. if (INTEL_GEN(dev_priv) >= 4)
  11504. drm_plane_create_rotation_property(&cursor->base,
  11505. DRM_ROTATE_0,
  11506. DRM_ROTATE_0 |
  11507. DRM_ROTATE_180);
  11508. if (INTEL_GEN(dev_priv) >= 9)
  11509. state->scaler_id = -1;
  11510. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11511. return cursor;
  11512. fail:
  11513. kfree(state);
  11514. kfree(cursor);
  11515. return ERR_PTR(ret);
  11516. }
  11517. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11518. struct intel_crtc_state *crtc_state)
  11519. {
  11520. struct intel_crtc_scaler_state *scaler_state =
  11521. &crtc_state->scaler_state;
  11522. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11523. int i;
  11524. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11525. if (!crtc->num_scalers)
  11526. return;
  11527. for (i = 0; i < crtc->num_scalers; i++) {
  11528. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11529. scaler->in_use = 0;
  11530. scaler->mode = PS_SCALER_MODE_DYN;
  11531. }
  11532. scaler_state->scaler_id = -1;
  11533. }
  11534. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11535. {
  11536. struct intel_crtc *intel_crtc;
  11537. struct intel_crtc_state *crtc_state = NULL;
  11538. struct intel_plane *primary = NULL;
  11539. struct intel_plane *cursor = NULL;
  11540. int sprite, ret;
  11541. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11542. if (!intel_crtc)
  11543. return -ENOMEM;
  11544. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11545. if (!crtc_state) {
  11546. ret = -ENOMEM;
  11547. goto fail;
  11548. }
  11549. intel_crtc->config = crtc_state;
  11550. intel_crtc->base.state = &crtc_state->base;
  11551. crtc_state->base.crtc = &intel_crtc->base;
  11552. primary = intel_primary_plane_create(dev_priv, pipe);
  11553. if (IS_ERR(primary)) {
  11554. ret = PTR_ERR(primary);
  11555. goto fail;
  11556. }
  11557. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11558. for_each_sprite(dev_priv, pipe, sprite) {
  11559. struct intel_plane *plane;
  11560. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11561. if (IS_ERR(plane)) {
  11562. ret = PTR_ERR(plane);
  11563. goto fail;
  11564. }
  11565. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11566. }
  11567. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11568. if (IS_ERR(cursor)) {
  11569. ret = PTR_ERR(cursor);
  11570. goto fail;
  11571. }
  11572. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11573. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11574. &primary->base, &cursor->base,
  11575. &intel_crtc_funcs,
  11576. "pipe %c", pipe_name(pipe));
  11577. if (ret)
  11578. goto fail;
  11579. intel_crtc->pipe = pipe;
  11580. intel_crtc->plane = primary->plane;
  11581. /* initialize shared scalers */
  11582. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11583. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11584. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11585. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11586. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11587. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11588. intel_color_init(&intel_crtc->base);
  11589. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11590. return 0;
  11591. fail:
  11592. /*
  11593. * drm_mode_config_cleanup() will free up any
  11594. * crtcs/planes already initialized.
  11595. */
  11596. kfree(crtc_state);
  11597. kfree(intel_crtc);
  11598. return ret;
  11599. }
  11600. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11601. {
  11602. struct drm_device *dev = connector->base.dev;
  11603. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11604. if (!connector->base.state->crtc)
  11605. return INVALID_PIPE;
  11606. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11607. }
  11608. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11609. struct drm_file *file)
  11610. {
  11611. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11612. struct drm_crtc *drmmode_crtc;
  11613. struct intel_crtc *crtc;
  11614. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11615. if (!drmmode_crtc)
  11616. return -ENOENT;
  11617. crtc = to_intel_crtc(drmmode_crtc);
  11618. pipe_from_crtc_id->pipe = crtc->pipe;
  11619. return 0;
  11620. }
  11621. static int intel_encoder_clones(struct intel_encoder *encoder)
  11622. {
  11623. struct drm_device *dev = encoder->base.dev;
  11624. struct intel_encoder *source_encoder;
  11625. int index_mask = 0;
  11626. int entry = 0;
  11627. for_each_intel_encoder(dev, source_encoder) {
  11628. if (encoders_cloneable(encoder, source_encoder))
  11629. index_mask |= (1 << entry);
  11630. entry++;
  11631. }
  11632. return index_mask;
  11633. }
  11634. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11635. {
  11636. if (!IS_MOBILE(dev_priv))
  11637. return false;
  11638. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11639. return false;
  11640. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11641. return false;
  11642. return true;
  11643. }
  11644. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11645. {
  11646. if (INTEL_GEN(dev_priv) >= 9)
  11647. return false;
  11648. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11649. return false;
  11650. if (IS_CHERRYVIEW(dev_priv))
  11651. return false;
  11652. if (HAS_PCH_LPT_H(dev_priv) &&
  11653. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11654. return false;
  11655. /* DDI E can't be used if DDI A requires 4 lanes */
  11656. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11657. return false;
  11658. if (!dev_priv->vbt.int_crt_support)
  11659. return false;
  11660. return true;
  11661. }
  11662. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11663. {
  11664. int pps_num;
  11665. int pps_idx;
  11666. if (HAS_DDI(dev_priv))
  11667. return;
  11668. /*
  11669. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11670. * everywhere where registers can be write protected.
  11671. */
  11672. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11673. pps_num = 2;
  11674. else
  11675. pps_num = 1;
  11676. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11677. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11678. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11679. I915_WRITE(PP_CONTROL(pps_idx), val);
  11680. }
  11681. }
  11682. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11683. {
  11684. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11685. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11686. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11687. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11688. else
  11689. dev_priv->pps_mmio_base = PPS_BASE;
  11690. intel_pps_unlock_regs_wa(dev_priv);
  11691. }
  11692. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11693. {
  11694. struct intel_encoder *encoder;
  11695. bool dpd_is_edp = false;
  11696. intel_pps_init(dev_priv);
  11697. /*
  11698. * intel_edp_init_connector() depends on this completing first, to
  11699. * prevent the registeration of both eDP and LVDS and the incorrect
  11700. * sharing of the PPS.
  11701. */
  11702. intel_lvds_init(dev_priv);
  11703. if (intel_crt_present(dev_priv))
  11704. intel_crt_init(dev_priv);
  11705. if (IS_GEN9_LP(dev_priv)) {
  11706. /*
  11707. * FIXME: Broxton doesn't support port detection via the
  11708. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11709. * detect the ports.
  11710. */
  11711. intel_ddi_init(dev_priv, PORT_A);
  11712. intel_ddi_init(dev_priv, PORT_B);
  11713. intel_ddi_init(dev_priv, PORT_C);
  11714. intel_dsi_init(dev_priv);
  11715. } else if (HAS_DDI(dev_priv)) {
  11716. int found;
  11717. /*
  11718. * Haswell uses DDI functions to detect digital outputs.
  11719. * On SKL pre-D0 the strap isn't connected, so we assume
  11720. * it's there.
  11721. */
  11722. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11723. /* WaIgnoreDDIAStrap: skl */
  11724. if (found || IS_GEN9_BC(dev_priv))
  11725. intel_ddi_init(dev_priv, PORT_A);
  11726. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11727. * register */
  11728. found = I915_READ(SFUSE_STRAP);
  11729. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11730. intel_ddi_init(dev_priv, PORT_B);
  11731. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11732. intel_ddi_init(dev_priv, PORT_C);
  11733. if (found & SFUSE_STRAP_DDID_DETECTED)
  11734. intel_ddi_init(dev_priv, PORT_D);
  11735. /*
  11736. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11737. */
  11738. if (IS_GEN9_BC(dev_priv) &&
  11739. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11740. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11741. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11742. intel_ddi_init(dev_priv, PORT_E);
  11743. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11744. int found;
  11745. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11746. if (has_edp_a(dev_priv))
  11747. intel_dp_init(dev_priv, DP_A, PORT_A);
  11748. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11749. /* PCH SDVOB multiplex with HDMIB */
  11750. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11751. if (!found)
  11752. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11753. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11754. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11755. }
  11756. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11757. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11758. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11759. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11760. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11761. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11762. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11763. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11764. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11765. bool has_edp, has_port;
  11766. /*
  11767. * The DP_DETECTED bit is the latched state of the DDC
  11768. * SDA pin at boot. However since eDP doesn't require DDC
  11769. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11770. * eDP ports may have been muxed to an alternate function.
  11771. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11772. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11773. * detect eDP ports.
  11774. *
  11775. * Sadly the straps seem to be missing sometimes even for HDMI
  11776. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11777. * and VBT for the presence of the port. Additionally we can't
  11778. * trust the port type the VBT declares as we've seen at least
  11779. * HDMI ports that the VBT claim are DP or eDP.
  11780. */
  11781. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11782. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11783. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11784. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11785. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11786. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11787. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11788. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11789. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11790. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11791. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11792. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11793. if (IS_CHERRYVIEW(dev_priv)) {
  11794. /*
  11795. * eDP not supported on port D,
  11796. * so no need to worry about it
  11797. */
  11798. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11799. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11800. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11801. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11802. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11803. }
  11804. intel_dsi_init(dev_priv);
  11805. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11806. bool found = false;
  11807. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11808. DRM_DEBUG_KMS("probing SDVOB\n");
  11809. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11810. if (!found && IS_G4X(dev_priv)) {
  11811. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11812. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11813. }
  11814. if (!found && IS_G4X(dev_priv))
  11815. intel_dp_init(dev_priv, DP_B, PORT_B);
  11816. }
  11817. /* Before G4X SDVOC doesn't have its own detect register */
  11818. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11819. DRM_DEBUG_KMS("probing SDVOC\n");
  11820. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11821. }
  11822. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11823. if (IS_G4X(dev_priv)) {
  11824. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11825. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11826. }
  11827. if (IS_G4X(dev_priv))
  11828. intel_dp_init(dev_priv, DP_C, PORT_C);
  11829. }
  11830. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11831. intel_dp_init(dev_priv, DP_D, PORT_D);
  11832. } else if (IS_GEN2(dev_priv))
  11833. intel_dvo_init(dev_priv);
  11834. if (SUPPORTS_TV(dev_priv))
  11835. intel_tv_init(dev_priv);
  11836. intel_psr_init(dev_priv);
  11837. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11838. encoder->base.possible_crtcs = encoder->crtc_mask;
  11839. encoder->base.possible_clones =
  11840. intel_encoder_clones(encoder);
  11841. }
  11842. intel_init_pch_refclk(dev_priv);
  11843. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11844. }
  11845. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11846. {
  11847. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11848. drm_framebuffer_cleanup(fb);
  11849. i915_gem_object_lock(intel_fb->obj);
  11850. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11851. i915_gem_object_unlock(intel_fb->obj);
  11852. i915_gem_object_put(intel_fb->obj);
  11853. kfree(intel_fb);
  11854. }
  11855. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11856. struct drm_file *file,
  11857. unsigned int *handle)
  11858. {
  11859. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11860. struct drm_i915_gem_object *obj = intel_fb->obj;
  11861. if (obj->userptr.mm) {
  11862. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11863. return -EINVAL;
  11864. }
  11865. return drm_gem_handle_create(file, &obj->base, handle);
  11866. }
  11867. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11868. struct drm_file *file,
  11869. unsigned flags, unsigned color,
  11870. struct drm_clip_rect *clips,
  11871. unsigned num_clips)
  11872. {
  11873. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11874. i915_gem_object_flush_if_display(obj);
  11875. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11876. return 0;
  11877. }
  11878. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11879. .destroy = intel_user_framebuffer_destroy,
  11880. .create_handle = intel_user_framebuffer_create_handle,
  11881. .dirty = intel_user_framebuffer_dirty,
  11882. };
  11883. static
  11884. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11885. uint64_t fb_modifier, uint32_t pixel_format)
  11886. {
  11887. u32 gen = INTEL_GEN(dev_priv);
  11888. if (gen >= 9) {
  11889. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11890. /* "The stride in bytes must not exceed the of the size of 8K
  11891. * pixels and 32K bytes."
  11892. */
  11893. return min(8192 * cpp, 32768);
  11894. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11895. return 32*1024;
  11896. } else if (gen >= 4) {
  11897. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11898. return 16*1024;
  11899. else
  11900. return 32*1024;
  11901. } else if (gen >= 3) {
  11902. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11903. return 8*1024;
  11904. else
  11905. return 16*1024;
  11906. } else {
  11907. /* XXX DSPC is limited to 4k tiled */
  11908. return 8*1024;
  11909. }
  11910. }
  11911. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11912. struct drm_i915_gem_object *obj,
  11913. struct drm_mode_fb_cmd2 *mode_cmd)
  11914. {
  11915. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11916. struct drm_format_name_buf format_name;
  11917. u32 pitch_limit, stride_alignment;
  11918. unsigned int tiling, stride;
  11919. int ret = -EINVAL;
  11920. i915_gem_object_lock(obj);
  11921. obj->framebuffer_references++;
  11922. tiling = i915_gem_object_get_tiling(obj);
  11923. stride = i915_gem_object_get_stride(obj);
  11924. i915_gem_object_unlock(obj);
  11925. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11926. /*
  11927. * If there's a fence, enforce that
  11928. * the fb modifier and tiling mode match.
  11929. */
  11930. if (tiling != I915_TILING_NONE &&
  11931. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11932. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11933. goto err;
  11934. }
  11935. } else {
  11936. if (tiling == I915_TILING_X) {
  11937. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11938. } else if (tiling == I915_TILING_Y) {
  11939. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11940. goto err;
  11941. }
  11942. }
  11943. /* Passed in modifier sanity checking. */
  11944. switch (mode_cmd->modifier[0]) {
  11945. case I915_FORMAT_MOD_Y_TILED:
  11946. case I915_FORMAT_MOD_Yf_TILED:
  11947. if (INTEL_GEN(dev_priv) < 9) {
  11948. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11949. mode_cmd->modifier[0]);
  11950. goto err;
  11951. }
  11952. case DRM_FORMAT_MOD_LINEAR:
  11953. case I915_FORMAT_MOD_X_TILED:
  11954. break;
  11955. default:
  11956. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11957. mode_cmd->modifier[0]);
  11958. goto err;
  11959. }
  11960. /*
  11961. * gen2/3 display engine uses the fence if present,
  11962. * so the tiling mode must match the fb modifier exactly.
  11963. */
  11964. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11965. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11966. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11967. goto err;
  11968. }
  11969. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11970. mode_cmd->pixel_format);
  11971. if (mode_cmd->pitches[0] > pitch_limit) {
  11972. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11973. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11974. "tiled" : "linear",
  11975. mode_cmd->pitches[0], pitch_limit);
  11976. goto err;
  11977. }
  11978. /*
  11979. * If there's a fence, enforce that
  11980. * the fb pitch and fence stride match.
  11981. */
  11982. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11983. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11984. mode_cmd->pitches[0], stride);
  11985. goto err;
  11986. }
  11987. /* Reject formats not supported by any plane early. */
  11988. switch (mode_cmd->pixel_format) {
  11989. case DRM_FORMAT_C8:
  11990. case DRM_FORMAT_RGB565:
  11991. case DRM_FORMAT_XRGB8888:
  11992. case DRM_FORMAT_ARGB8888:
  11993. break;
  11994. case DRM_FORMAT_XRGB1555:
  11995. if (INTEL_GEN(dev_priv) > 3) {
  11996. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  11997. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11998. goto err;
  11999. }
  12000. break;
  12001. case DRM_FORMAT_ABGR8888:
  12002. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12003. INTEL_GEN(dev_priv) < 9) {
  12004. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12005. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12006. goto err;
  12007. }
  12008. break;
  12009. case DRM_FORMAT_XBGR8888:
  12010. case DRM_FORMAT_XRGB2101010:
  12011. case DRM_FORMAT_XBGR2101010:
  12012. if (INTEL_GEN(dev_priv) < 4) {
  12013. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12014. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12015. goto err;
  12016. }
  12017. break;
  12018. case DRM_FORMAT_ABGR2101010:
  12019. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12020. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12021. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12022. goto err;
  12023. }
  12024. break;
  12025. case DRM_FORMAT_YUYV:
  12026. case DRM_FORMAT_UYVY:
  12027. case DRM_FORMAT_YVYU:
  12028. case DRM_FORMAT_VYUY:
  12029. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
  12030. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12031. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12032. goto err;
  12033. }
  12034. break;
  12035. default:
  12036. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12037. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12038. goto err;
  12039. }
  12040. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12041. if (mode_cmd->offsets[0] != 0)
  12042. goto err;
  12043. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12044. &intel_fb->base, mode_cmd);
  12045. stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
  12046. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12047. DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
  12048. mode_cmd->pitches[0], stride_alignment);
  12049. goto err;
  12050. }
  12051. intel_fb->obj = obj;
  12052. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12053. if (ret)
  12054. goto err;
  12055. ret = drm_framebuffer_init(obj->base.dev,
  12056. &intel_fb->base,
  12057. &intel_fb_funcs);
  12058. if (ret) {
  12059. DRM_ERROR("framebuffer init failed %d\n", ret);
  12060. goto err;
  12061. }
  12062. return 0;
  12063. err:
  12064. i915_gem_object_lock(obj);
  12065. obj->framebuffer_references--;
  12066. i915_gem_object_unlock(obj);
  12067. return ret;
  12068. }
  12069. static struct drm_framebuffer *
  12070. intel_user_framebuffer_create(struct drm_device *dev,
  12071. struct drm_file *filp,
  12072. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12073. {
  12074. struct drm_framebuffer *fb;
  12075. struct drm_i915_gem_object *obj;
  12076. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12077. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12078. if (!obj)
  12079. return ERR_PTR(-ENOENT);
  12080. fb = intel_framebuffer_create(obj, &mode_cmd);
  12081. if (IS_ERR(fb))
  12082. i915_gem_object_put(obj);
  12083. return fb;
  12084. }
  12085. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12086. {
  12087. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12088. drm_atomic_state_default_release(state);
  12089. i915_sw_fence_fini(&intel_state->commit_ready);
  12090. kfree(state);
  12091. }
  12092. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12093. .fb_create = intel_user_framebuffer_create,
  12094. .output_poll_changed = intel_fbdev_output_poll_changed,
  12095. .atomic_check = intel_atomic_check,
  12096. .atomic_commit = intel_atomic_commit,
  12097. .atomic_state_alloc = intel_atomic_state_alloc,
  12098. .atomic_state_clear = intel_atomic_state_clear,
  12099. .atomic_state_free = intel_atomic_state_free,
  12100. };
  12101. /**
  12102. * intel_init_display_hooks - initialize the display modesetting hooks
  12103. * @dev_priv: device private
  12104. */
  12105. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12106. {
  12107. intel_init_cdclk_hooks(dev_priv);
  12108. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12109. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12110. dev_priv->display.get_initial_plane_config =
  12111. skylake_get_initial_plane_config;
  12112. dev_priv->display.crtc_compute_clock =
  12113. haswell_crtc_compute_clock;
  12114. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12115. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12116. } else if (HAS_DDI(dev_priv)) {
  12117. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12118. dev_priv->display.get_initial_plane_config =
  12119. ironlake_get_initial_plane_config;
  12120. dev_priv->display.crtc_compute_clock =
  12121. haswell_crtc_compute_clock;
  12122. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12123. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12124. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12125. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12126. dev_priv->display.get_initial_plane_config =
  12127. ironlake_get_initial_plane_config;
  12128. dev_priv->display.crtc_compute_clock =
  12129. ironlake_crtc_compute_clock;
  12130. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12131. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12132. } else if (IS_CHERRYVIEW(dev_priv)) {
  12133. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12134. dev_priv->display.get_initial_plane_config =
  12135. i9xx_get_initial_plane_config;
  12136. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12137. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12138. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12139. } else if (IS_VALLEYVIEW(dev_priv)) {
  12140. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12141. dev_priv->display.get_initial_plane_config =
  12142. i9xx_get_initial_plane_config;
  12143. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12144. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12145. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12146. } else if (IS_G4X(dev_priv)) {
  12147. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12148. dev_priv->display.get_initial_plane_config =
  12149. i9xx_get_initial_plane_config;
  12150. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12151. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12152. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12153. } else if (IS_PINEVIEW(dev_priv)) {
  12154. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12155. dev_priv->display.get_initial_plane_config =
  12156. i9xx_get_initial_plane_config;
  12157. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12158. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12159. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12160. } else if (!IS_GEN2(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 = i9xx_crtc_compute_clock;
  12165. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12166. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12167. } else {
  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 = i8xx_crtc_compute_clock;
  12172. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12173. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12174. }
  12175. if (IS_GEN5(dev_priv)) {
  12176. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12177. } else if (IS_GEN6(dev_priv)) {
  12178. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12179. } else if (IS_IVYBRIDGE(dev_priv)) {
  12180. /* FIXME: detect B0+ stepping and use auto training */
  12181. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12182. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12183. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12184. }
  12185. if (dev_priv->info.gen >= 9)
  12186. dev_priv->display.update_crtcs = skl_update_crtcs;
  12187. else
  12188. dev_priv->display.update_crtcs = intel_update_crtcs;
  12189. switch (INTEL_INFO(dev_priv)->gen) {
  12190. case 2:
  12191. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12192. break;
  12193. case 3:
  12194. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12195. break;
  12196. case 4:
  12197. case 5:
  12198. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12199. break;
  12200. case 6:
  12201. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12202. break;
  12203. case 7:
  12204. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12205. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12206. break;
  12207. case 9:
  12208. /* Drop through - unsupported since execlist only. */
  12209. default:
  12210. /* Default just returns -ENODEV to indicate unsupported */
  12211. dev_priv->display.queue_flip = intel_default_queue_flip;
  12212. }
  12213. }
  12214. /*
  12215. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12216. * resume, or other times. This quirk makes sure that's the case for
  12217. * affected systems.
  12218. */
  12219. static void quirk_pipea_force(struct drm_device *dev)
  12220. {
  12221. struct drm_i915_private *dev_priv = to_i915(dev);
  12222. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12223. DRM_INFO("applying pipe a force quirk\n");
  12224. }
  12225. static void quirk_pipeb_force(struct drm_device *dev)
  12226. {
  12227. struct drm_i915_private *dev_priv = to_i915(dev);
  12228. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12229. DRM_INFO("applying pipe b force quirk\n");
  12230. }
  12231. /*
  12232. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12233. */
  12234. static void quirk_ssc_force_disable(struct drm_device *dev)
  12235. {
  12236. struct drm_i915_private *dev_priv = to_i915(dev);
  12237. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12238. DRM_INFO("applying lvds SSC disable quirk\n");
  12239. }
  12240. /*
  12241. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12242. * brightness value
  12243. */
  12244. static void quirk_invert_brightness(struct drm_device *dev)
  12245. {
  12246. struct drm_i915_private *dev_priv = to_i915(dev);
  12247. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12248. DRM_INFO("applying inverted panel brightness quirk\n");
  12249. }
  12250. /* Some VBT's incorrectly indicate no backlight is present */
  12251. static void quirk_backlight_present(struct drm_device *dev)
  12252. {
  12253. struct drm_i915_private *dev_priv = to_i915(dev);
  12254. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12255. DRM_INFO("applying backlight present quirk\n");
  12256. }
  12257. struct intel_quirk {
  12258. int device;
  12259. int subsystem_vendor;
  12260. int subsystem_device;
  12261. void (*hook)(struct drm_device *dev);
  12262. };
  12263. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12264. struct intel_dmi_quirk {
  12265. void (*hook)(struct drm_device *dev);
  12266. const struct dmi_system_id (*dmi_id_list)[];
  12267. };
  12268. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12269. {
  12270. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12271. return 1;
  12272. }
  12273. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12274. {
  12275. .dmi_id_list = &(const struct dmi_system_id[]) {
  12276. {
  12277. .callback = intel_dmi_reverse_brightness,
  12278. .ident = "NCR Corporation",
  12279. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12280. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12281. },
  12282. },
  12283. { } /* terminating entry */
  12284. },
  12285. .hook = quirk_invert_brightness,
  12286. },
  12287. };
  12288. static struct intel_quirk intel_quirks[] = {
  12289. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12290. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12291. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12292. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12293. /* 830 needs to leave pipe A & dpll A up */
  12294. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12295. /* 830 needs to leave pipe B & dpll B up */
  12296. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12297. /* Lenovo U160 cannot use SSC on LVDS */
  12298. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12299. /* Sony Vaio Y cannot use SSC on LVDS */
  12300. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12301. /* Acer Aspire 5734Z must invert backlight brightness */
  12302. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12303. /* Acer/eMachines G725 */
  12304. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12305. /* Acer/eMachines e725 */
  12306. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12307. /* Acer/Packard Bell NCL20 */
  12308. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12309. /* Acer Aspire 4736Z */
  12310. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12311. /* Acer Aspire 5336 */
  12312. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12313. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12314. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12315. /* Acer C720 Chromebook (Core i3 4005U) */
  12316. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12317. /* Apple Macbook 2,1 (Core 2 T7400) */
  12318. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12319. /* Apple Macbook 4,1 */
  12320. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12321. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12322. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12323. /* HP Chromebook 14 (Celeron 2955U) */
  12324. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12325. /* Dell Chromebook 11 */
  12326. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12327. /* Dell Chromebook 11 (2015 version) */
  12328. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12329. };
  12330. static void intel_init_quirks(struct drm_device *dev)
  12331. {
  12332. struct pci_dev *d = dev->pdev;
  12333. int i;
  12334. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12335. struct intel_quirk *q = &intel_quirks[i];
  12336. if (d->device == q->device &&
  12337. (d->subsystem_vendor == q->subsystem_vendor ||
  12338. q->subsystem_vendor == PCI_ANY_ID) &&
  12339. (d->subsystem_device == q->subsystem_device ||
  12340. q->subsystem_device == PCI_ANY_ID))
  12341. q->hook(dev);
  12342. }
  12343. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12344. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12345. intel_dmi_quirks[i].hook(dev);
  12346. }
  12347. }
  12348. /* Disable the VGA plane that we never use */
  12349. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12350. {
  12351. struct pci_dev *pdev = dev_priv->drm.pdev;
  12352. u8 sr1;
  12353. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12354. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12355. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12356. outb(SR01, VGA_SR_INDEX);
  12357. sr1 = inb(VGA_SR_DATA);
  12358. outb(sr1 | 1<<5, VGA_SR_DATA);
  12359. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12360. udelay(300);
  12361. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12362. POSTING_READ(vga_reg);
  12363. }
  12364. void intel_modeset_init_hw(struct drm_device *dev)
  12365. {
  12366. struct drm_i915_private *dev_priv = to_i915(dev);
  12367. intel_update_cdclk(dev_priv);
  12368. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12369. intel_init_clock_gating(dev_priv);
  12370. }
  12371. /*
  12372. * Calculate what we think the watermarks should be for the state we've read
  12373. * out of the hardware and then immediately program those watermarks so that
  12374. * we ensure the hardware settings match our internal state.
  12375. *
  12376. * We can calculate what we think WM's should be by creating a duplicate of the
  12377. * current state (which was constructed during hardware readout) and running it
  12378. * through the atomic check code to calculate new watermark values in the
  12379. * state object.
  12380. */
  12381. static void sanitize_watermarks(struct drm_device *dev)
  12382. {
  12383. struct drm_i915_private *dev_priv = to_i915(dev);
  12384. struct drm_atomic_state *state;
  12385. struct intel_atomic_state *intel_state;
  12386. struct drm_crtc *crtc;
  12387. struct drm_crtc_state *cstate;
  12388. struct drm_modeset_acquire_ctx ctx;
  12389. int ret;
  12390. int i;
  12391. /* Only supported on platforms that use atomic watermark design */
  12392. if (!dev_priv->display.optimize_watermarks)
  12393. return;
  12394. /*
  12395. * We need to hold connection_mutex before calling duplicate_state so
  12396. * that the connector loop is protected.
  12397. */
  12398. drm_modeset_acquire_init(&ctx, 0);
  12399. retry:
  12400. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12401. if (ret == -EDEADLK) {
  12402. drm_modeset_backoff(&ctx);
  12403. goto retry;
  12404. } else if (WARN_ON(ret)) {
  12405. goto fail;
  12406. }
  12407. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12408. if (WARN_ON(IS_ERR(state)))
  12409. goto fail;
  12410. intel_state = to_intel_atomic_state(state);
  12411. /*
  12412. * Hardware readout is the only time we don't want to calculate
  12413. * intermediate watermarks (since we don't trust the current
  12414. * watermarks).
  12415. */
  12416. if (!HAS_GMCH_DISPLAY(dev_priv))
  12417. intel_state->skip_intermediate_wm = true;
  12418. ret = intel_atomic_check(dev, state);
  12419. if (ret) {
  12420. /*
  12421. * If we fail here, it means that the hardware appears to be
  12422. * programmed in a way that shouldn't be possible, given our
  12423. * understanding of watermark requirements. This might mean a
  12424. * mistake in the hardware readout code or a mistake in the
  12425. * watermark calculations for a given platform. Raise a WARN
  12426. * so that this is noticeable.
  12427. *
  12428. * If this actually happens, we'll have to just leave the
  12429. * BIOS-programmed watermarks untouched and hope for the best.
  12430. */
  12431. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12432. goto put_state;
  12433. }
  12434. /* Write calculated watermark values back */
  12435. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12436. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12437. cs->wm.need_postvbl_update = true;
  12438. dev_priv->display.optimize_watermarks(intel_state, cs);
  12439. }
  12440. put_state:
  12441. drm_atomic_state_put(state);
  12442. fail:
  12443. drm_modeset_drop_locks(&ctx);
  12444. drm_modeset_acquire_fini(&ctx);
  12445. }
  12446. int intel_modeset_init(struct drm_device *dev)
  12447. {
  12448. struct drm_i915_private *dev_priv = to_i915(dev);
  12449. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12450. enum pipe pipe;
  12451. struct intel_crtc *crtc;
  12452. drm_mode_config_init(dev);
  12453. dev->mode_config.min_width = 0;
  12454. dev->mode_config.min_height = 0;
  12455. dev->mode_config.preferred_depth = 24;
  12456. dev->mode_config.prefer_shadow = 1;
  12457. dev->mode_config.allow_fb_modifiers = true;
  12458. dev->mode_config.funcs = &intel_mode_funcs;
  12459. init_llist_head(&dev_priv->atomic_helper.free_list);
  12460. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12461. intel_atomic_helper_free_state_worker);
  12462. intel_init_quirks(dev);
  12463. intel_init_pm(dev_priv);
  12464. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12465. return 0;
  12466. /*
  12467. * There may be no VBT; and if the BIOS enabled SSC we can
  12468. * just keep using it to avoid unnecessary flicker. Whereas if the
  12469. * BIOS isn't using it, don't assume it will work even if the VBT
  12470. * indicates as much.
  12471. */
  12472. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12473. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12474. DREF_SSC1_ENABLE);
  12475. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12476. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12477. bios_lvds_use_ssc ? "en" : "dis",
  12478. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12479. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12480. }
  12481. }
  12482. if (IS_GEN2(dev_priv)) {
  12483. dev->mode_config.max_width = 2048;
  12484. dev->mode_config.max_height = 2048;
  12485. } else if (IS_GEN3(dev_priv)) {
  12486. dev->mode_config.max_width = 4096;
  12487. dev->mode_config.max_height = 4096;
  12488. } else {
  12489. dev->mode_config.max_width = 8192;
  12490. dev->mode_config.max_height = 8192;
  12491. }
  12492. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12493. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12494. dev->mode_config.cursor_height = 1023;
  12495. } else if (IS_GEN2(dev_priv)) {
  12496. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12497. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12498. } else {
  12499. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12500. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12501. }
  12502. dev->mode_config.fb_base = ggtt->mappable_base;
  12503. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12504. INTEL_INFO(dev_priv)->num_pipes,
  12505. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12506. for_each_pipe(dev_priv, pipe) {
  12507. int ret;
  12508. ret = intel_crtc_init(dev_priv, pipe);
  12509. if (ret) {
  12510. drm_mode_config_cleanup(dev);
  12511. return ret;
  12512. }
  12513. }
  12514. intel_shared_dpll_init(dev);
  12515. intel_update_czclk(dev_priv);
  12516. intel_modeset_init_hw(dev);
  12517. if (dev_priv->max_cdclk_freq == 0)
  12518. intel_update_max_cdclk(dev_priv);
  12519. /* Just disable it once at startup */
  12520. i915_disable_vga(dev_priv);
  12521. intel_setup_outputs(dev_priv);
  12522. drm_modeset_lock_all(dev);
  12523. intel_modeset_setup_hw_state(dev);
  12524. drm_modeset_unlock_all(dev);
  12525. for_each_intel_crtc(dev, crtc) {
  12526. struct intel_initial_plane_config plane_config = {};
  12527. if (!crtc->active)
  12528. continue;
  12529. /*
  12530. * Note that reserving the BIOS fb up front prevents us
  12531. * from stuffing other stolen allocations like the ring
  12532. * on top. This prevents some ugliness at boot time, and
  12533. * can even allow for smooth boot transitions if the BIOS
  12534. * fb is large enough for the active pipe configuration.
  12535. */
  12536. dev_priv->display.get_initial_plane_config(crtc,
  12537. &plane_config);
  12538. /*
  12539. * If the fb is shared between multiple heads, we'll
  12540. * just get the first one.
  12541. */
  12542. intel_find_initial_plane_obj(crtc, &plane_config);
  12543. }
  12544. /*
  12545. * Make sure hardware watermarks really match the state we read out.
  12546. * Note that we need to do this after reconstructing the BIOS fb's
  12547. * since the watermark calculation done here will use pstate->fb.
  12548. */
  12549. if (!HAS_GMCH_DISPLAY(dev_priv))
  12550. sanitize_watermarks(dev);
  12551. return 0;
  12552. }
  12553. static void intel_enable_pipe_a(struct drm_device *dev)
  12554. {
  12555. struct intel_connector *connector;
  12556. struct drm_connector_list_iter conn_iter;
  12557. struct drm_connector *crt = NULL;
  12558. struct intel_load_detect_pipe load_detect_temp;
  12559. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12560. int ret;
  12561. /* We can't just switch on the pipe A, we need to set things up with a
  12562. * proper mode and output configuration. As a gross hack, enable pipe A
  12563. * by enabling the load detect pipe once. */
  12564. drm_connector_list_iter_begin(dev, &conn_iter);
  12565. for_each_intel_connector_iter(connector, &conn_iter) {
  12566. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12567. crt = &connector->base;
  12568. break;
  12569. }
  12570. }
  12571. drm_connector_list_iter_end(&conn_iter);
  12572. if (!crt)
  12573. return;
  12574. ret = intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx);
  12575. WARN(ret < 0, "All modeset mutexes are locked, but intel_get_load_detect_pipe failed\n");
  12576. if (ret > 0)
  12577. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12578. }
  12579. static bool
  12580. intel_check_plane_mapping(struct intel_crtc *crtc)
  12581. {
  12582. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12583. u32 val;
  12584. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12585. return true;
  12586. val = I915_READ(DSPCNTR(!crtc->plane));
  12587. if ((val & DISPLAY_PLANE_ENABLE) &&
  12588. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12589. return false;
  12590. return true;
  12591. }
  12592. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12593. {
  12594. struct drm_device *dev = crtc->base.dev;
  12595. struct intel_encoder *encoder;
  12596. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12597. return true;
  12598. return false;
  12599. }
  12600. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12601. {
  12602. struct drm_device *dev = encoder->base.dev;
  12603. struct intel_connector *connector;
  12604. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12605. return connector;
  12606. return NULL;
  12607. }
  12608. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12609. enum transcoder pch_transcoder)
  12610. {
  12611. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12612. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12613. }
  12614. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12615. {
  12616. struct drm_device *dev = crtc->base.dev;
  12617. struct drm_i915_private *dev_priv = to_i915(dev);
  12618. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12619. /* Clear any frame start delays used for debugging left by the BIOS */
  12620. if (!transcoder_is_dsi(cpu_transcoder)) {
  12621. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12622. I915_WRITE(reg,
  12623. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12624. }
  12625. /* restore vblank interrupts to correct state */
  12626. drm_crtc_vblank_reset(&crtc->base);
  12627. if (crtc->active) {
  12628. struct intel_plane *plane;
  12629. drm_crtc_vblank_on(&crtc->base);
  12630. /* Disable everything but the primary plane */
  12631. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12632. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12633. continue;
  12634. trace_intel_disable_plane(&plane->base, crtc);
  12635. plane->disable_plane(plane, crtc);
  12636. }
  12637. }
  12638. /* We need to sanitize the plane -> pipe mapping first because this will
  12639. * disable the crtc (and hence change the state) if it is wrong. Note
  12640. * that gen4+ has a fixed plane -> pipe mapping. */
  12641. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12642. bool plane;
  12643. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12644. crtc->base.base.id, crtc->base.name);
  12645. /* Pipe has the wrong plane attached and the plane is active.
  12646. * Temporarily change the plane mapping and disable everything
  12647. * ... */
  12648. plane = crtc->plane;
  12649. crtc->base.primary->state->visible = true;
  12650. crtc->plane = !plane;
  12651. intel_crtc_disable_noatomic(&crtc->base);
  12652. crtc->plane = plane;
  12653. }
  12654. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12655. crtc->pipe == PIPE_A && !crtc->active) {
  12656. /* BIOS forgot to enable pipe A, this mostly happens after
  12657. * resume. Force-enable the pipe to fix this, the update_dpms
  12658. * call below we restore the pipe to the right state, but leave
  12659. * the required bits on. */
  12660. intel_enable_pipe_a(dev);
  12661. }
  12662. /* Adjust the state of the output pipe according to whether we
  12663. * have active connectors/encoders. */
  12664. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12665. intel_crtc_disable_noatomic(&crtc->base);
  12666. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12667. /*
  12668. * We start out with underrun reporting disabled to avoid races.
  12669. * For correct bookkeeping mark this on active crtcs.
  12670. *
  12671. * Also on gmch platforms we dont have any hardware bits to
  12672. * disable the underrun reporting. Which means we need to start
  12673. * out with underrun reporting disabled also on inactive pipes,
  12674. * since otherwise we'll complain about the garbage we read when
  12675. * e.g. coming up after runtime pm.
  12676. *
  12677. * No protection against concurrent access is required - at
  12678. * worst a fifo underrun happens which also sets this to false.
  12679. */
  12680. crtc->cpu_fifo_underrun_disabled = true;
  12681. /*
  12682. * We track the PCH trancoder underrun reporting state
  12683. * within the crtc. With crtc for pipe A housing the underrun
  12684. * reporting state for PCH transcoder A, crtc for pipe B housing
  12685. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12686. * and marking underrun reporting as disabled for the non-existing
  12687. * PCH transcoders B and C would prevent enabling the south
  12688. * error interrupt (see cpt_can_enable_serr_int()).
  12689. */
  12690. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12691. crtc->pch_fifo_underrun_disabled = true;
  12692. }
  12693. }
  12694. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12695. {
  12696. struct intel_connector *connector;
  12697. /* We need to check both for a crtc link (meaning that the
  12698. * encoder is active and trying to read from a pipe) and the
  12699. * pipe itself being active. */
  12700. bool has_active_crtc = encoder->base.crtc &&
  12701. to_intel_crtc(encoder->base.crtc)->active;
  12702. connector = intel_encoder_find_connector(encoder);
  12703. if (connector && !has_active_crtc) {
  12704. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12705. encoder->base.base.id,
  12706. encoder->base.name);
  12707. /* Connector is active, but has no active pipe. This is
  12708. * fallout from our resume register restoring. Disable
  12709. * the encoder manually again. */
  12710. if (encoder->base.crtc) {
  12711. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12712. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12713. encoder->base.base.id,
  12714. encoder->base.name);
  12715. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12716. if (encoder->post_disable)
  12717. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12718. }
  12719. encoder->base.crtc = NULL;
  12720. /* Inconsistent output/port/pipe state happens presumably due to
  12721. * a bug in one of the get_hw_state functions. Or someplace else
  12722. * in our code, like the register restore mess on resume. Clamp
  12723. * things to off as a safer default. */
  12724. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12725. connector->base.encoder = NULL;
  12726. }
  12727. /* Enabled encoders without active connectors will be fixed in
  12728. * the crtc fixup. */
  12729. }
  12730. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12731. {
  12732. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12733. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12734. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12735. i915_disable_vga(dev_priv);
  12736. }
  12737. }
  12738. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12739. {
  12740. /* This function can be called both from intel_modeset_setup_hw_state or
  12741. * at a very early point in our resume sequence, where the power well
  12742. * structures are not yet restored. Since this function is at a very
  12743. * paranoid "someone might have enabled VGA while we were not looking"
  12744. * level, just check if the power well is enabled instead of trying to
  12745. * follow the "don't touch the power well if we don't need it" policy
  12746. * the rest of the driver uses. */
  12747. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12748. return;
  12749. i915_redisable_vga_power_on(dev_priv);
  12750. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12751. }
  12752. static bool primary_get_hw_state(struct intel_plane *plane)
  12753. {
  12754. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12755. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12756. }
  12757. /* FIXME read out full plane state for all planes */
  12758. static void readout_plane_state(struct intel_crtc *crtc)
  12759. {
  12760. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12761. bool visible;
  12762. visible = crtc->active && primary_get_hw_state(primary);
  12763. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12764. to_intel_plane_state(primary->base.state),
  12765. visible);
  12766. }
  12767. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12768. {
  12769. struct drm_i915_private *dev_priv = to_i915(dev);
  12770. enum pipe pipe;
  12771. struct intel_crtc *crtc;
  12772. struct intel_encoder *encoder;
  12773. struct intel_connector *connector;
  12774. struct drm_connector_list_iter conn_iter;
  12775. int i;
  12776. dev_priv->active_crtcs = 0;
  12777. for_each_intel_crtc(dev, crtc) {
  12778. struct intel_crtc_state *crtc_state =
  12779. to_intel_crtc_state(crtc->base.state);
  12780. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12781. memset(crtc_state, 0, sizeof(*crtc_state));
  12782. crtc_state->base.crtc = &crtc->base;
  12783. crtc_state->base.active = crtc_state->base.enable =
  12784. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12785. crtc->base.enabled = crtc_state->base.enable;
  12786. crtc->active = crtc_state->base.active;
  12787. if (crtc_state->base.active)
  12788. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12789. readout_plane_state(crtc);
  12790. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12791. crtc->base.base.id, crtc->base.name,
  12792. enableddisabled(crtc_state->base.active));
  12793. }
  12794. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12795. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12796. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12797. &pll->state.hw_state);
  12798. pll->state.crtc_mask = 0;
  12799. for_each_intel_crtc(dev, crtc) {
  12800. struct intel_crtc_state *crtc_state =
  12801. to_intel_crtc_state(crtc->base.state);
  12802. if (crtc_state->base.active &&
  12803. crtc_state->shared_dpll == pll)
  12804. pll->state.crtc_mask |= 1 << crtc->pipe;
  12805. }
  12806. pll->active_mask = pll->state.crtc_mask;
  12807. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12808. pll->name, pll->state.crtc_mask, pll->on);
  12809. }
  12810. for_each_intel_encoder(dev, encoder) {
  12811. pipe = 0;
  12812. if (encoder->get_hw_state(encoder, &pipe)) {
  12813. struct intel_crtc_state *crtc_state;
  12814. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12815. crtc_state = to_intel_crtc_state(crtc->base.state);
  12816. encoder->base.crtc = &crtc->base;
  12817. crtc_state->output_types |= 1 << encoder->type;
  12818. encoder->get_config(encoder, crtc_state);
  12819. } else {
  12820. encoder->base.crtc = NULL;
  12821. }
  12822. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12823. encoder->base.base.id, encoder->base.name,
  12824. enableddisabled(encoder->base.crtc),
  12825. pipe_name(pipe));
  12826. }
  12827. drm_connector_list_iter_begin(dev, &conn_iter);
  12828. for_each_intel_connector_iter(connector, &conn_iter) {
  12829. if (connector->get_hw_state(connector)) {
  12830. connector->base.dpms = DRM_MODE_DPMS_ON;
  12831. encoder = connector->encoder;
  12832. connector->base.encoder = &encoder->base;
  12833. if (encoder->base.crtc &&
  12834. encoder->base.crtc->state->active) {
  12835. /*
  12836. * This has to be done during hardware readout
  12837. * because anything calling .crtc_disable may
  12838. * rely on the connector_mask being accurate.
  12839. */
  12840. encoder->base.crtc->state->connector_mask |=
  12841. 1 << drm_connector_index(&connector->base);
  12842. encoder->base.crtc->state->encoder_mask |=
  12843. 1 << drm_encoder_index(&encoder->base);
  12844. }
  12845. } else {
  12846. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12847. connector->base.encoder = NULL;
  12848. }
  12849. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12850. connector->base.base.id, connector->base.name,
  12851. enableddisabled(connector->base.encoder));
  12852. }
  12853. drm_connector_list_iter_end(&conn_iter);
  12854. for_each_intel_crtc(dev, crtc) {
  12855. struct intel_crtc_state *crtc_state =
  12856. to_intel_crtc_state(crtc->base.state);
  12857. int pixclk = 0;
  12858. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  12859. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12860. if (crtc_state->base.active) {
  12861. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12862. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12863. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12864. /*
  12865. * The initial mode needs to be set in order to keep
  12866. * the atomic core happy. It wants a valid mode if the
  12867. * crtc's enabled, so we do the above call.
  12868. *
  12869. * But we don't set all the derived state fully, hence
  12870. * set a flag to indicate that a full recalculation is
  12871. * needed on the next commit.
  12872. */
  12873. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12874. intel_crtc_compute_pixel_rate(crtc_state);
  12875. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12876. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12877. pixclk = crtc_state->pixel_rate;
  12878. else
  12879. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12880. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12881. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12882. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12883. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12884. update_scanline_offset(crtc);
  12885. }
  12886. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12887. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12888. }
  12889. }
  12890. static void
  12891. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12892. {
  12893. struct intel_encoder *encoder;
  12894. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12895. u64 get_domains;
  12896. enum intel_display_power_domain domain;
  12897. if (!encoder->get_power_domains)
  12898. continue;
  12899. get_domains = encoder->get_power_domains(encoder);
  12900. for_each_power_domain(domain, get_domains)
  12901. intel_display_power_get(dev_priv, domain);
  12902. }
  12903. }
  12904. /* Scan out the current hw modeset state,
  12905. * and sanitizes it to the current state
  12906. */
  12907. static void
  12908. intel_modeset_setup_hw_state(struct drm_device *dev)
  12909. {
  12910. struct drm_i915_private *dev_priv = to_i915(dev);
  12911. enum pipe pipe;
  12912. struct intel_crtc *crtc;
  12913. struct intel_encoder *encoder;
  12914. int i;
  12915. intel_modeset_readout_hw_state(dev);
  12916. /* HW state is read out, now we need to sanitize this mess. */
  12917. get_encoder_power_domains(dev_priv);
  12918. for_each_intel_encoder(dev, encoder) {
  12919. intel_sanitize_encoder(encoder);
  12920. }
  12921. for_each_pipe(dev_priv, pipe) {
  12922. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12923. intel_sanitize_crtc(crtc);
  12924. intel_dump_pipe_config(crtc, crtc->config,
  12925. "[setup_hw_state]");
  12926. }
  12927. intel_modeset_update_connector_atomic_state(dev);
  12928. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12929. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12930. if (!pll->on || pll->active_mask)
  12931. continue;
  12932. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12933. pll->funcs.disable(dev_priv, pll);
  12934. pll->on = false;
  12935. }
  12936. if (IS_G4X(dev_priv)) {
  12937. g4x_wm_get_hw_state(dev);
  12938. g4x_wm_sanitize(dev_priv);
  12939. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12940. vlv_wm_get_hw_state(dev);
  12941. vlv_wm_sanitize(dev_priv);
  12942. } else if (IS_GEN9(dev_priv)) {
  12943. skl_wm_get_hw_state(dev);
  12944. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12945. ilk_wm_get_hw_state(dev);
  12946. }
  12947. for_each_intel_crtc(dev, crtc) {
  12948. u64 put_domains;
  12949. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12950. if (WARN_ON(put_domains))
  12951. modeset_put_power_domains(dev_priv, put_domains);
  12952. }
  12953. intel_display_set_init_power(dev_priv, false);
  12954. intel_power_domains_verify_state(dev_priv);
  12955. intel_fbc_init_pipe_state(dev_priv);
  12956. }
  12957. void intel_display_resume(struct drm_device *dev)
  12958. {
  12959. struct drm_i915_private *dev_priv = to_i915(dev);
  12960. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12961. struct drm_modeset_acquire_ctx ctx;
  12962. int ret;
  12963. dev_priv->modeset_restore_state = NULL;
  12964. if (state)
  12965. state->acquire_ctx = &ctx;
  12966. drm_modeset_acquire_init(&ctx, 0);
  12967. while (1) {
  12968. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12969. if (ret != -EDEADLK)
  12970. break;
  12971. drm_modeset_backoff(&ctx);
  12972. }
  12973. if (!ret)
  12974. ret = __intel_display_resume(dev, state, &ctx);
  12975. drm_modeset_drop_locks(&ctx);
  12976. drm_modeset_acquire_fini(&ctx);
  12977. if (ret)
  12978. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12979. if (state)
  12980. drm_atomic_state_put(state);
  12981. }
  12982. void intel_modeset_gem_init(struct drm_device *dev)
  12983. {
  12984. struct drm_i915_private *dev_priv = to_i915(dev);
  12985. intel_init_gt_powersave(dev_priv);
  12986. intel_setup_overlay(dev_priv);
  12987. }
  12988. int intel_connector_register(struct drm_connector *connector)
  12989. {
  12990. struct intel_connector *intel_connector = to_intel_connector(connector);
  12991. int ret;
  12992. ret = intel_backlight_device_register(intel_connector);
  12993. if (ret)
  12994. goto err;
  12995. return 0;
  12996. err:
  12997. return ret;
  12998. }
  12999. void intel_connector_unregister(struct drm_connector *connector)
  13000. {
  13001. struct intel_connector *intel_connector = to_intel_connector(connector);
  13002. intel_backlight_device_unregister(intel_connector);
  13003. intel_panel_destroy_backlight(connector);
  13004. }
  13005. void intel_modeset_cleanup(struct drm_device *dev)
  13006. {
  13007. struct drm_i915_private *dev_priv = to_i915(dev);
  13008. flush_work(&dev_priv->atomic_helper.free_work);
  13009. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13010. intel_disable_gt_powersave(dev_priv);
  13011. /*
  13012. * Interrupts and polling as the first thing to avoid creating havoc.
  13013. * Too much stuff here (turning of connectors, ...) would
  13014. * experience fancy races otherwise.
  13015. */
  13016. intel_irq_uninstall(dev_priv);
  13017. /*
  13018. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13019. * poll handlers. Hence disable polling after hpd handling is shut down.
  13020. */
  13021. drm_kms_helper_poll_fini(dev);
  13022. intel_unregister_dsm_handler();
  13023. intel_fbc_global_disable(dev_priv);
  13024. /* flush any delayed tasks or pending work */
  13025. flush_scheduled_work();
  13026. drm_mode_config_cleanup(dev);
  13027. intel_cleanup_overlay(dev_priv);
  13028. intel_cleanup_gt_powersave(dev_priv);
  13029. intel_teardown_gmbus(dev_priv);
  13030. }
  13031. void intel_connector_attach_encoder(struct intel_connector *connector,
  13032. struct intel_encoder *encoder)
  13033. {
  13034. connector->encoder = encoder;
  13035. drm_mode_connector_attach_encoder(&connector->base,
  13036. &encoder->base);
  13037. }
  13038. /*
  13039. * set vga decode state - true == enable VGA decode
  13040. */
  13041. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13042. {
  13043. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13044. u16 gmch_ctrl;
  13045. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13046. DRM_ERROR("failed to read control word\n");
  13047. return -EIO;
  13048. }
  13049. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13050. return 0;
  13051. if (state)
  13052. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13053. else
  13054. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13055. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13056. DRM_ERROR("failed to write control word\n");
  13057. return -EIO;
  13058. }
  13059. return 0;
  13060. }
  13061. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13062. struct intel_display_error_state {
  13063. u32 power_well_driver;
  13064. int num_transcoders;
  13065. struct intel_cursor_error_state {
  13066. u32 control;
  13067. u32 position;
  13068. u32 base;
  13069. u32 size;
  13070. } cursor[I915_MAX_PIPES];
  13071. struct intel_pipe_error_state {
  13072. bool power_domain_on;
  13073. u32 source;
  13074. u32 stat;
  13075. } pipe[I915_MAX_PIPES];
  13076. struct intel_plane_error_state {
  13077. u32 control;
  13078. u32 stride;
  13079. u32 size;
  13080. u32 pos;
  13081. u32 addr;
  13082. u32 surface;
  13083. u32 tile_offset;
  13084. } plane[I915_MAX_PIPES];
  13085. struct intel_transcoder_error_state {
  13086. bool power_domain_on;
  13087. enum transcoder cpu_transcoder;
  13088. u32 conf;
  13089. u32 htotal;
  13090. u32 hblank;
  13091. u32 hsync;
  13092. u32 vtotal;
  13093. u32 vblank;
  13094. u32 vsync;
  13095. } transcoder[4];
  13096. };
  13097. struct intel_display_error_state *
  13098. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13099. {
  13100. struct intel_display_error_state *error;
  13101. int transcoders[] = {
  13102. TRANSCODER_A,
  13103. TRANSCODER_B,
  13104. TRANSCODER_C,
  13105. TRANSCODER_EDP,
  13106. };
  13107. int i;
  13108. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13109. return NULL;
  13110. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13111. if (error == NULL)
  13112. return NULL;
  13113. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13114. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13115. for_each_pipe(dev_priv, i) {
  13116. error->pipe[i].power_domain_on =
  13117. __intel_display_power_is_enabled(dev_priv,
  13118. POWER_DOMAIN_PIPE(i));
  13119. if (!error->pipe[i].power_domain_on)
  13120. continue;
  13121. error->cursor[i].control = I915_READ(CURCNTR(i));
  13122. error->cursor[i].position = I915_READ(CURPOS(i));
  13123. error->cursor[i].base = I915_READ(CURBASE(i));
  13124. error->plane[i].control = I915_READ(DSPCNTR(i));
  13125. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13126. if (INTEL_GEN(dev_priv) <= 3) {
  13127. error->plane[i].size = I915_READ(DSPSIZE(i));
  13128. error->plane[i].pos = I915_READ(DSPPOS(i));
  13129. }
  13130. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13131. error->plane[i].addr = I915_READ(DSPADDR(i));
  13132. if (INTEL_GEN(dev_priv) >= 4) {
  13133. error->plane[i].surface = I915_READ(DSPSURF(i));
  13134. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13135. }
  13136. error->pipe[i].source = I915_READ(PIPESRC(i));
  13137. if (HAS_GMCH_DISPLAY(dev_priv))
  13138. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13139. }
  13140. /* Note: this does not include DSI transcoders. */
  13141. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13142. if (HAS_DDI(dev_priv))
  13143. error->num_transcoders++; /* Account for eDP. */
  13144. for (i = 0; i < error->num_transcoders; i++) {
  13145. enum transcoder cpu_transcoder = transcoders[i];
  13146. error->transcoder[i].power_domain_on =
  13147. __intel_display_power_is_enabled(dev_priv,
  13148. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13149. if (!error->transcoder[i].power_domain_on)
  13150. continue;
  13151. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13152. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13153. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13154. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13155. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13156. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13157. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13158. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13159. }
  13160. return error;
  13161. }
  13162. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13163. void
  13164. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13165. struct intel_display_error_state *error)
  13166. {
  13167. struct drm_i915_private *dev_priv = m->i915;
  13168. int i;
  13169. if (!error)
  13170. return;
  13171. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13172. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13173. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13174. error->power_well_driver);
  13175. for_each_pipe(dev_priv, i) {
  13176. err_printf(m, "Pipe [%d]:\n", i);
  13177. err_printf(m, " Power: %s\n",
  13178. onoff(error->pipe[i].power_domain_on));
  13179. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13180. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13181. err_printf(m, "Plane [%d]:\n", i);
  13182. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13183. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13184. if (INTEL_GEN(dev_priv) <= 3) {
  13185. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13186. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13187. }
  13188. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13189. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13190. if (INTEL_GEN(dev_priv) >= 4) {
  13191. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13192. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13193. }
  13194. err_printf(m, "Cursor [%d]:\n", i);
  13195. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13196. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13197. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13198. }
  13199. for (i = 0; i < error->num_transcoders; i++) {
  13200. err_printf(m, "CPU transcoder: %s\n",
  13201. transcoder_name(error->transcoder[i].cpu_transcoder));
  13202. err_printf(m, " Power: %s\n",
  13203. onoff(error->transcoder[i].power_domain_on));
  13204. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13205. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13206. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13207. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13208. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13209. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13210. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13211. }
  13212. }
  13213. #endif