intel_display.c 447 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  93. struct drm_i915_gem_object *obj,
  94. struct drm_mode_fb_cmd2 *mode_cmd);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. struct intel_limit {
  118. struct {
  119. int min, max;
  120. } dot, vco, n, m, m1, m2, p, p1;
  121. struct {
  122. int dot_limit;
  123. int p2_slow, p2_fast;
  124. } p2;
  125. };
  126. /* returns HPLL frequency in kHz */
  127. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  128. {
  129. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  130. /* Obtain SKU information */
  131. mutex_lock(&dev_priv->sb_lock);
  132. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  133. CCK_FUSE_HPLL_FREQ_MASK;
  134. mutex_unlock(&dev_priv->sb_lock);
  135. return vco_freq[hpll_freq] * 1000;
  136. }
  137. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  138. const char *name, u32 reg, int ref_freq)
  139. {
  140. u32 val;
  141. int divider;
  142. mutex_lock(&dev_priv->sb_lock);
  143. val = vlv_cck_read(dev_priv, reg);
  144. mutex_unlock(&dev_priv->sb_lock);
  145. divider = val & CCK_FREQUENCY_VALUES;
  146. WARN((val & CCK_FREQUENCY_STATUS) !=
  147. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  148. "%s change in progress\n", name);
  149. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  150. }
  151. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  152. const char *name, u32 reg)
  153. {
  154. if (dev_priv->hpll_freq == 0)
  155. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  156. return vlv_get_cck_clock(dev_priv, name, reg,
  157. dev_priv->hpll_freq);
  158. }
  159. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  160. {
  161. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  162. return;
  163. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  164. CCK_CZ_CLOCK_CONTROL);
  165. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  166. }
  167. static inline u32 /* units of 100MHz */
  168. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  169. const struct intel_crtc_state *pipe_config)
  170. {
  171. if (HAS_DDI(dev_priv))
  172. return pipe_config->port_clock; /* SPLL */
  173. else if (IS_GEN5(dev_priv))
  174. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  175. else
  176. return 270000;
  177. }
  178. static const struct intel_limit intel_limits_i8xx_dac = {
  179. .dot = { .min = 25000, .max = 350000 },
  180. .vco = { .min = 908000, .max = 1512000 },
  181. .n = { .min = 2, .max = 16 },
  182. .m = { .min = 96, .max = 140 },
  183. .m1 = { .min = 18, .max = 26 },
  184. .m2 = { .min = 6, .max = 16 },
  185. .p = { .min = 4, .max = 128 },
  186. .p1 = { .min = 2, .max = 33 },
  187. .p2 = { .dot_limit = 165000,
  188. .p2_slow = 4, .p2_fast = 2 },
  189. };
  190. static const struct intel_limit intel_limits_i8xx_dvo = {
  191. .dot = { .min = 25000, .max = 350000 },
  192. .vco = { .min = 908000, .max = 1512000 },
  193. .n = { .min = 2, .max = 16 },
  194. .m = { .min = 96, .max = 140 },
  195. .m1 = { .min = 18, .max = 26 },
  196. .m2 = { .min = 6, .max = 16 },
  197. .p = { .min = 4, .max = 128 },
  198. .p1 = { .min = 2, .max = 33 },
  199. .p2 = { .dot_limit = 165000,
  200. .p2_slow = 4, .p2_fast = 4 },
  201. };
  202. static const struct intel_limit intel_limits_i8xx_lvds = {
  203. .dot = { .min = 25000, .max = 350000 },
  204. .vco = { .min = 908000, .max = 1512000 },
  205. .n = { .min = 2, .max = 16 },
  206. .m = { .min = 96, .max = 140 },
  207. .m1 = { .min = 18, .max = 26 },
  208. .m2 = { .min = 6, .max = 16 },
  209. .p = { .min = 4, .max = 128 },
  210. .p1 = { .min = 1, .max = 6 },
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 14, .p2_fast = 7 },
  213. };
  214. static const struct intel_limit intel_limits_i9xx_sdvo = {
  215. .dot = { .min = 20000, .max = 400000 },
  216. .vco = { .min = 1400000, .max = 2800000 },
  217. .n = { .min = 1, .max = 6 },
  218. .m = { .min = 70, .max = 120 },
  219. .m1 = { .min = 8, .max = 18 },
  220. .m2 = { .min = 3, .max = 7 },
  221. .p = { .min = 5, .max = 80 },
  222. .p1 = { .min = 1, .max = 8 },
  223. .p2 = { .dot_limit = 200000,
  224. .p2_slow = 10, .p2_fast = 5 },
  225. };
  226. static const struct intel_limit intel_limits_i9xx_lvds = {
  227. .dot = { .min = 20000, .max = 400000 },
  228. .vco = { .min = 1400000, .max = 2800000 },
  229. .n = { .min = 1, .max = 6 },
  230. .m = { .min = 70, .max = 120 },
  231. .m1 = { .min = 8, .max = 18 },
  232. .m2 = { .min = 3, .max = 7 },
  233. .p = { .min = 7, .max = 98 },
  234. .p1 = { .min = 1, .max = 8 },
  235. .p2 = { .dot_limit = 112000,
  236. .p2_slow = 14, .p2_fast = 7 },
  237. };
  238. static const struct intel_limit intel_limits_g4x_sdvo = {
  239. .dot = { .min = 25000, .max = 270000 },
  240. .vco = { .min = 1750000, .max = 3500000},
  241. .n = { .min = 1, .max = 4 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 10, .max = 30 },
  246. .p1 = { .min = 1, .max = 3},
  247. .p2 = { .dot_limit = 270000,
  248. .p2_slow = 10,
  249. .p2_fast = 10
  250. },
  251. };
  252. static const struct intel_limit intel_limits_g4x_hdmi = {
  253. .dot = { .min = 22000, .max = 400000 },
  254. .vco = { .min = 1750000, .max = 3500000},
  255. .n = { .min = 1, .max = 4 },
  256. .m = { .min = 104, .max = 138 },
  257. .m1 = { .min = 16, .max = 23 },
  258. .m2 = { .min = 5, .max = 11 },
  259. .p = { .min = 5, .max = 80 },
  260. .p1 = { .min = 1, .max = 8},
  261. .p2 = { .dot_limit = 165000,
  262. .p2_slow = 10, .p2_fast = 5 },
  263. };
  264. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  265. .dot = { .min = 20000, .max = 115000 },
  266. .vco = { .min = 1750000, .max = 3500000 },
  267. .n = { .min = 1, .max = 3 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 28, .max = 112 },
  272. .p1 = { .min = 2, .max = 8 },
  273. .p2 = { .dot_limit = 0,
  274. .p2_slow = 14, .p2_fast = 14
  275. },
  276. };
  277. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  278. .dot = { .min = 80000, .max = 224000 },
  279. .vco = { .min = 1750000, .max = 3500000 },
  280. .n = { .min = 1, .max = 3 },
  281. .m = { .min = 104, .max = 138 },
  282. .m1 = { .min = 17, .max = 23 },
  283. .m2 = { .min = 5, .max = 11 },
  284. .p = { .min = 14, .max = 42 },
  285. .p1 = { .min = 2, .max = 6 },
  286. .p2 = { .dot_limit = 0,
  287. .p2_slow = 7, .p2_fast = 7
  288. },
  289. };
  290. static const struct intel_limit intel_limits_pineview_sdvo = {
  291. .dot = { .min = 20000, .max = 400000},
  292. .vco = { .min = 1700000, .max = 3500000 },
  293. /* Pineview's Ncounter is a ring counter */
  294. .n = { .min = 3, .max = 6 },
  295. .m = { .min = 2, .max = 256 },
  296. /* Pineview only has one combined m divider, which we treat as m2. */
  297. .m1 = { .min = 0, .max = 0 },
  298. .m2 = { .min = 0, .max = 254 },
  299. .p = { .min = 5, .max = 80 },
  300. .p1 = { .min = 1, .max = 8 },
  301. .p2 = { .dot_limit = 200000,
  302. .p2_slow = 10, .p2_fast = 5 },
  303. };
  304. static const struct intel_limit intel_limits_pineview_lvds = {
  305. .dot = { .min = 20000, .max = 400000 },
  306. .vco = { .min = 1700000, .max = 3500000 },
  307. .n = { .min = 3, .max = 6 },
  308. .m = { .min = 2, .max = 256 },
  309. .m1 = { .min = 0, .max = 0 },
  310. .m2 = { .min = 0, .max = 254 },
  311. .p = { .min = 7, .max = 112 },
  312. .p1 = { .min = 1, .max = 8 },
  313. .p2 = { .dot_limit = 112000,
  314. .p2_slow = 14, .p2_fast = 14 },
  315. };
  316. /* Ironlake / Sandybridge
  317. *
  318. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  319. * the range value for them is (actual_value - 2).
  320. */
  321. static const struct intel_limit intel_limits_ironlake_dac = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 5 },
  325. .m = { .min = 79, .max = 127 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 5, .max = 80 },
  329. .p1 = { .min = 1, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 10, .p2_fast = 5 },
  332. };
  333. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 118 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 28, .max = 112 },
  341. .p1 = { .min = 2, .max = 8 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 14, .p2_fast = 14 },
  344. };
  345. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  346. .dot = { .min = 25000, .max = 350000 },
  347. .vco = { .min = 1760000, .max = 3510000 },
  348. .n = { .min = 1, .max = 3 },
  349. .m = { .min = 79, .max = 127 },
  350. .m1 = { .min = 12, .max = 22 },
  351. .m2 = { .min = 5, .max = 9 },
  352. .p = { .min = 14, .max = 56 },
  353. .p1 = { .min = 2, .max = 8 },
  354. .p2 = { .dot_limit = 225000,
  355. .p2_slow = 7, .p2_fast = 7 },
  356. };
  357. /* LVDS 100mhz refclk limits. */
  358. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  359. .dot = { .min = 25000, .max = 350000 },
  360. .vco = { .min = 1760000, .max = 3510000 },
  361. .n = { .min = 1, .max = 2 },
  362. .m = { .min = 79, .max = 126 },
  363. .m1 = { .min = 12, .max = 22 },
  364. .m2 = { .min = 5, .max = 9 },
  365. .p = { .min = 28, .max = 112 },
  366. .p1 = { .min = 2, .max = 8 },
  367. .p2 = { .dot_limit = 225000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 3 },
  374. .m = { .min = 79, .max = 126 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 14, .max = 42 },
  378. .p1 = { .min = 2, .max = 6 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 7, .p2_fast = 7 },
  381. };
  382. static const struct intel_limit intel_limits_vlv = {
  383. /*
  384. * These are the data rate limits (measured in fast clocks)
  385. * since those are the strictest limits we have. The fast
  386. * clock and actual rate limits are more relaxed, so checking
  387. * them would make no difference.
  388. */
  389. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  390. .vco = { .min = 4000000, .max = 6000000 },
  391. .n = { .min = 1, .max = 7 },
  392. .m1 = { .min = 2, .max = 3 },
  393. .m2 = { .min = 11, .max = 156 },
  394. .p1 = { .min = 2, .max = 3 },
  395. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  396. };
  397. static const struct intel_limit intel_limits_chv = {
  398. /*
  399. * These are the data rate limits (measured in fast clocks)
  400. * since those are the strictest limits we have. The fast
  401. * clock and actual rate limits are more relaxed, so checking
  402. * them would make no difference.
  403. */
  404. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  405. .vco = { .min = 4800000, .max = 6480000 },
  406. .n = { .min = 1, .max = 1 },
  407. .m1 = { .min = 2, .max = 2 },
  408. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  409. .p1 = { .min = 2, .max = 4 },
  410. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  411. };
  412. static const struct intel_limit intel_limits_bxt = {
  413. /* FIXME: find real dot limits */
  414. .dot = { .min = 0, .max = INT_MAX },
  415. .vco = { .min = 4800000, .max = 6700000 },
  416. .n = { .min = 1, .max = 1 },
  417. .m1 = { .min = 2, .max = 2 },
  418. /* FIXME: find real m2 limits */
  419. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  420. .p1 = { .min = 2, .max = 4 },
  421. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  422. };
  423. static bool
  424. needs_modeset(struct drm_crtc_state *state)
  425. {
  426. return drm_atomic_crtc_needs_modeset(state);
  427. }
  428. /*
  429. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  430. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  431. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  432. * The helpers' return value is the rate of the clock that is fed to the
  433. * display engine's pipe which can be the above fast dot clock rate or a
  434. * divided-down version of it.
  435. */
  436. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  437. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  438. {
  439. clock->m = clock->m2 + 2;
  440. clock->p = clock->p1 * clock->p2;
  441. if (WARN_ON(clock->n == 0 || clock->p == 0))
  442. return 0;
  443. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  444. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  445. return clock->dot;
  446. }
  447. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  448. {
  449. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  450. }
  451. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  452. {
  453. clock->m = i9xx_dpll_compute_m(clock);
  454. clock->p = clock->p1 * clock->p2;
  455. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  456. return 0;
  457. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  458. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  459. return clock->dot;
  460. }
  461. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  462. {
  463. clock->m = clock->m1 * clock->m2;
  464. clock->p = clock->p1 * clock->p2;
  465. if (WARN_ON(clock->n == 0 || clock->p == 0))
  466. return 0;
  467. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  468. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  469. return clock->dot / 5;
  470. }
  471. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = clock->m1 * clock->m2;
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  478. clock->n << 22);
  479. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  480. return clock->dot / 5;
  481. }
  482. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  483. /**
  484. * Returns whether the given set of divisors are valid for a given refclk with
  485. * the given connectors.
  486. */
  487. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  488. const struct intel_limit *limit,
  489. const struct dpll *clock)
  490. {
  491. if (clock->n < limit->n.min || limit->n.max < clock->n)
  492. INTELPllInvalid("n out of range\n");
  493. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  494. INTELPllInvalid("p1 out of range\n");
  495. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  496. INTELPllInvalid("m2 out of range\n");
  497. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  498. INTELPllInvalid("m1 out of range\n");
  499. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  500. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  501. if (clock->m1 <= clock->m2)
  502. INTELPllInvalid("m1 <= m2\n");
  503. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  504. !IS_GEN9_LP(dev_priv)) {
  505. if (clock->p < limit->p.min || limit->p.max < clock->p)
  506. INTELPllInvalid("p out of range\n");
  507. if (clock->m < limit->m.min || limit->m.max < clock->m)
  508. INTELPllInvalid("m out of range\n");
  509. }
  510. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  511. INTELPllInvalid("vco out of range\n");
  512. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  513. * connector, etc., rather than just a single range.
  514. */
  515. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  516. INTELPllInvalid("dot out of range\n");
  517. return true;
  518. }
  519. static int
  520. i9xx_select_p2_div(const struct intel_limit *limit,
  521. const struct intel_crtc_state *crtc_state,
  522. int target)
  523. {
  524. struct drm_device *dev = crtc_state->base.crtc->dev;
  525. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  526. /*
  527. * For LVDS just rely on its current settings for dual-channel.
  528. * We haven't figured out how to reliably set up different
  529. * single/dual channel state, if we even can.
  530. */
  531. if (intel_is_dual_link_lvds(dev))
  532. return limit->p2.p2_fast;
  533. else
  534. return limit->p2.p2_slow;
  535. } else {
  536. if (target < limit->p2.dot_limit)
  537. return limit->p2.p2_slow;
  538. else
  539. return limit->p2.p2_fast;
  540. }
  541. }
  542. /*
  543. * Returns a set of divisors for the desired target clock with the given
  544. * refclk, or FALSE. The returned values represent the clock equation:
  545. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  546. *
  547. * Target and reference clocks are specified in kHz.
  548. *
  549. * If match_clock is provided, then best_clock P divider must match the P
  550. * divider from @match_clock used for LVDS downclocking.
  551. */
  552. static bool
  553. i9xx_find_best_dpll(const struct intel_limit *limit,
  554. struct intel_crtc_state *crtc_state,
  555. int target, int refclk, struct dpll *match_clock,
  556. struct dpll *best_clock)
  557. {
  558. struct drm_device *dev = crtc_state->base.crtc->dev;
  559. struct dpll clock;
  560. int err = target;
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  563. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  564. clock.m1++) {
  565. for (clock.m2 = limit->m2.min;
  566. clock.m2 <= limit->m2.max; clock.m2++) {
  567. if (clock.m2 >= clock.m1)
  568. break;
  569. for (clock.n = limit->n.min;
  570. clock.n <= limit->n.max; clock.n++) {
  571. for (clock.p1 = limit->p1.min;
  572. clock.p1 <= limit->p1.max; clock.p1++) {
  573. int this_err;
  574. i9xx_calc_dpll_params(refclk, &clock);
  575. if (!intel_PLL_is_valid(to_i915(dev),
  576. limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. /*
  594. * Returns a set of divisors for the desired target clock with the given
  595. * refclk, or FALSE. The returned values represent the clock equation:
  596. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  597. *
  598. * Target and reference clocks are specified in kHz.
  599. *
  600. * If match_clock is provided, then best_clock P divider must match the P
  601. * divider from @match_clock used for LVDS downclocking.
  602. */
  603. static bool
  604. pnv_find_best_dpll(const struct intel_limit *limit,
  605. struct intel_crtc_state *crtc_state,
  606. int target, int refclk, struct dpll *match_clock,
  607. struct dpll *best_clock)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. struct dpll clock;
  611. int err = target;
  612. memset(best_clock, 0, sizeof(*best_clock));
  613. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  614. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  615. clock.m1++) {
  616. for (clock.m2 = limit->m2.min;
  617. clock.m2 <= limit->m2.max; clock.m2++) {
  618. for (clock.n = limit->n.min;
  619. clock.n <= limit->n.max; clock.n++) {
  620. for (clock.p1 = limit->p1.min;
  621. clock.p1 <= limit->p1.max; clock.p1++) {
  622. int this_err;
  623. pnv_calc_dpll_params(refclk, &clock);
  624. if (!intel_PLL_is_valid(to_i915(dev),
  625. limit,
  626. &clock))
  627. continue;
  628. if (match_clock &&
  629. clock.p != match_clock->p)
  630. continue;
  631. this_err = abs(clock.dot - target);
  632. if (this_err < err) {
  633. *best_clock = clock;
  634. err = this_err;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. return (err != target);
  641. }
  642. /*
  643. * Returns a set of divisors for the desired target clock with the given
  644. * refclk, or FALSE. The returned values represent the clock equation:
  645. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  646. *
  647. * Target and reference clocks are specified in kHz.
  648. *
  649. * If match_clock is provided, then best_clock P divider must match the P
  650. * divider from @match_clock used for LVDS downclocking.
  651. */
  652. static bool
  653. g4x_find_best_dpll(const struct intel_limit *limit,
  654. struct intel_crtc_state *crtc_state,
  655. int target, int refclk, struct dpll *match_clock,
  656. struct dpll *best_clock)
  657. {
  658. struct drm_device *dev = crtc_state->base.crtc->dev;
  659. struct dpll clock;
  660. int max_n;
  661. bool found = false;
  662. /* approximately equals target * 0.00585 */
  663. int err_most = (target >> 8) + (target >> 9);
  664. memset(best_clock, 0, sizeof(*best_clock));
  665. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  666. max_n = limit->n.max;
  667. /* based on hardware requirement, prefer smaller n to precision */
  668. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  669. /* based on hardware requirement, prefere larger m1,m2 */
  670. for (clock.m1 = limit->m1.max;
  671. clock.m1 >= limit->m1.min; clock.m1--) {
  672. for (clock.m2 = limit->m2.max;
  673. clock.m2 >= limit->m2.min; clock.m2--) {
  674. for (clock.p1 = limit->p1.max;
  675. clock.p1 >= limit->p1.min; clock.p1--) {
  676. int this_err;
  677. i9xx_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. /*
  696. * Check if the calculated PLL configuration is more optimal compared to the
  697. * best configuration and error found so far. Return the calculated error.
  698. */
  699. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  700. const struct dpll *calculated_clock,
  701. const struct dpll *best_clock,
  702. unsigned int best_error_ppm,
  703. unsigned int *error_ppm)
  704. {
  705. /*
  706. * For CHV ignore the error and consider only the P value.
  707. * Prefer a bigger P value based on HW requirements.
  708. */
  709. if (IS_CHERRYVIEW(to_i915(dev))) {
  710. *error_ppm = 0;
  711. return calculated_clock->p > best_clock->p;
  712. }
  713. if (WARN_ON_ONCE(!target_freq))
  714. return false;
  715. *error_ppm = div_u64(1000000ULL *
  716. abs(target_freq - calculated_clock->dot),
  717. target_freq);
  718. /*
  719. * Prefer a better P value over a better (smaller) error if the error
  720. * is small. Ensure this preference for future configurations too by
  721. * setting the error to 0.
  722. */
  723. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  724. *error_ppm = 0;
  725. return true;
  726. }
  727. return *error_ppm + 10 < best_error_ppm;
  728. }
  729. /*
  730. * Returns a set of divisors for the desired target clock with the given
  731. * refclk, or FALSE. The returned values represent the clock equation:
  732. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  733. */
  734. static bool
  735. vlv_find_best_dpll(const struct intel_limit *limit,
  736. struct intel_crtc_state *crtc_state,
  737. int target, int refclk, struct dpll *match_clock,
  738. struct dpll *best_clock)
  739. {
  740. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  741. struct drm_device *dev = crtc->base.dev;
  742. struct dpll clock;
  743. unsigned int bestppm = 1000000;
  744. /* min update 19.2 MHz */
  745. int max_n = min(limit->n.max, refclk / 19200);
  746. bool found = false;
  747. target *= 5; /* fast clock */
  748. memset(best_clock, 0, sizeof(*best_clock));
  749. /* based on hardware requirement, prefer smaller n to precision */
  750. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  751. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  752. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  753. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  754. clock.p = clock.p1 * clock.p2;
  755. /* based on hardware requirement, prefer bigger m1,m2 values */
  756. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  757. unsigned int ppm;
  758. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  759. refclk * clock.m1);
  760. vlv_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(to_i915(dev),
  762. limit,
  763. &clock))
  764. continue;
  765. if (!vlv_PLL_is_optimal(dev, target,
  766. &clock,
  767. best_clock,
  768. bestppm, &ppm))
  769. continue;
  770. *best_clock = clock;
  771. bestppm = ppm;
  772. found = true;
  773. }
  774. }
  775. }
  776. }
  777. return found;
  778. }
  779. /*
  780. * Returns a set of divisors for the desired target clock with the given
  781. * refclk, or FALSE. The returned values represent the clock equation:
  782. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  783. */
  784. static bool
  785. chv_find_best_dpll(const struct intel_limit *limit,
  786. struct intel_crtc_state *crtc_state,
  787. int target, int refclk, struct dpll *match_clock,
  788. struct dpll *best_clock)
  789. {
  790. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  791. struct drm_device *dev = crtc->base.dev;
  792. unsigned int best_error_ppm;
  793. struct dpll clock;
  794. uint64_t m2;
  795. int found = false;
  796. memset(best_clock, 0, sizeof(*best_clock));
  797. best_error_ppm = 1000000;
  798. /*
  799. * Based on hardware doc, the n always set to 1, and m1 always
  800. * set to 2. If requires to support 200Mhz refclk, we need to
  801. * revisit this because n may not 1 anymore.
  802. */
  803. clock.n = 1, clock.m1 = 2;
  804. target *= 5; /* fast clock */
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast;
  807. clock.p2 >= limit->p2.p2_slow;
  808. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  809. unsigned int error_ppm;
  810. clock.p = clock.p1 * clock.p2;
  811. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  812. clock.n) << 22, refclk * clock.m1);
  813. if (m2 > INT_MAX/clock.m1)
  814. continue;
  815. clock.m2 = m2;
  816. chv_calc_dpll_params(refclk, &clock);
  817. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  820. best_error_ppm, &error_ppm))
  821. continue;
  822. *best_clock = clock;
  823. best_error_ppm = error_ppm;
  824. found = true;
  825. }
  826. }
  827. return found;
  828. }
  829. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  830. struct dpll *best_clock)
  831. {
  832. int refclk = 100000;
  833. const struct intel_limit *limit = &intel_limits_bxt;
  834. return chv_find_best_dpll(limit, crtc_state,
  835. target_clock, refclk, NULL, best_clock);
  836. }
  837. bool intel_crtc_active(struct intel_crtc *crtc)
  838. {
  839. /* Be paranoid as we can arrive here with only partial
  840. * state retrieved from the hardware during setup.
  841. *
  842. * We can ditch the adjusted_mode.crtc_clock check as soon
  843. * as Haswell has gained clock readout/fastboot support.
  844. *
  845. * We can ditch the crtc->primary->fb check as soon as we can
  846. * properly reconstruct framebuffers.
  847. *
  848. * FIXME: The intel_crtc->active here should be switched to
  849. * crtc->state->active once we have proper CRTC states wired up
  850. * for atomic.
  851. */
  852. return crtc->active && crtc->base.primary->state->fb &&
  853. crtc->config->base.adjusted_mode.crtc_clock;
  854. }
  855. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  856. enum pipe pipe)
  857. {
  858. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  859. return crtc->config->cpu_transcoder;
  860. }
  861. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  862. {
  863. i915_reg_t reg = PIPEDSL(pipe);
  864. u32 line1, line2;
  865. u32 line_mask;
  866. if (IS_GEN2(dev_priv))
  867. line_mask = DSL_LINEMASK_GEN2;
  868. else
  869. line_mask = DSL_LINEMASK_GEN3;
  870. line1 = I915_READ(reg) & line_mask;
  871. msleep(5);
  872. line2 = I915_READ(reg) & line_mask;
  873. return line1 == line2;
  874. }
  875. /*
  876. * intel_wait_for_pipe_off - wait for pipe to turn off
  877. * @crtc: crtc whose pipe to wait for
  878. *
  879. * After disabling a pipe, we can't wait for vblank in the usual way,
  880. * spinning on the vblank interrupt status bit, since we won't actually
  881. * see an interrupt when the pipe is disabled.
  882. *
  883. * On Gen4 and above:
  884. * wait for the pipe register state bit to turn off
  885. *
  886. * Otherwise:
  887. * wait for the display line value to settle (it usually
  888. * ends up stopping at the start of the next frame).
  889. *
  890. */
  891. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  892. {
  893. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  894. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  895. enum pipe pipe = crtc->pipe;
  896. if (INTEL_GEN(dev_priv) >= 4) {
  897. i915_reg_t reg = PIPECONF(cpu_transcoder);
  898. /* Wait for the Pipe State to go off */
  899. if (intel_wait_for_register(dev_priv,
  900. reg, I965_PIPECONF_ACTIVE, 0,
  901. 100))
  902. WARN(1, "pipe_off wait timed out\n");
  903. } else {
  904. /* Wait for the display line to settle */
  905. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  906. WARN(1, "pipe_off wait timed out\n");
  907. }
  908. }
  909. /* Only for pre-ILK configs */
  910. void assert_pll(struct drm_i915_private *dev_priv,
  911. enum pipe pipe, bool state)
  912. {
  913. u32 val;
  914. bool cur_state;
  915. val = I915_READ(DPLL(pipe));
  916. cur_state = !!(val & DPLL_VCO_ENABLE);
  917. I915_STATE_WARN(cur_state != state,
  918. "PLL state assertion failure (expected %s, current %s)\n",
  919. onoff(state), onoff(cur_state));
  920. }
  921. /* XXX: the dsi pll is shared between MIPI DSI ports */
  922. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  923. {
  924. u32 val;
  925. bool cur_state;
  926. mutex_lock(&dev_priv->sb_lock);
  927. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  928. mutex_unlock(&dev_priv->sb_lock);
  929. cur_state = val & DSI_PLL_VCO_EN;
  930. I915_STATE_WARN(cur_state != state,
  931. "DSI PLL state assertion failure (expected %s, current %s)\n",
  932. onoff(state), onoff(cur_state));
  933. }
  934. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  935. enum pipe pipe, bool state)
  936. {
  937. bool cur_state;
  938. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  939. pipe);
  940. if (HAS_DDI(dev_priv)) {
  941. /* DDI does not have a specific FDI_TX register */
  942. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  943. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  944. } else {
  945. u32 val = I915_READ(FDI_TX_CTL(pipe));
  946. cur_state = !!(val & FDI_TX_ENABLE);
  947. }
  948. I915_STATE_WARN(cur_state != state,
  949. "FDI TX state assertion failure (expected %s, current %s)\n",
  950. onoff(state), onoff(cur_state));
  951. }
  952. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  953. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  954. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  955. enum pipe pipe, bool state)
  956. {
  957. u32 val;
  958. bool cur_state;
  959. val = I915_READ(FDI_RX_CTL(pipe));
  960. cur_state = !!(val & FDI_RX_ENABLE);
  961. I915_STATE_WARN(cur_state != state,
  962. "FDI RX state assertion failure (expected %s, current %s)\n",
  963. onoff(state), onoff(cur_state));
  964. }
  965. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  966. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  967. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  968. enum pipe pipe)
  969. {
  970. u32 val;
  971. /* ILK FDI PLL is always enabled */
  972. if (IS_GEN5(dev_priv))
  973. return;
  974. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  975. if (HAS_DDI(dev_priv))
  976. return;
  977. val = I915_READ(FDI_TX_CTL(pipe));
  978. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  979. }
  980. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  981. enum pipe pipe, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. val = I915_READ(FDI_RX_CTL(pipe));
  986. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  987. I915_STATE_WARN(cur_state != state,
  988. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  989. onoff(state), onoff(cur_state));
  990. }
  991. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  992. {
  993. i915_reg_t pp_reg;
  994. u32 val;
  995. enum pipe panel_pipe = PIPE_A;
  996. bool locked = true;
  997. if (WARN_ON(HAS_DDI(dev_priv)))
  998. return;
  999. if (HAS_PCH_SPLIT(dev_priv)) {
  1000. u32 port_sel;
  1001. pp_reg = PP_CONTROL(0);
  1002. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1003. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1004. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1005. panel_pipe = PIPE_B;
  1006. /* XXX: else fix for eDP */
  1007. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1008. /* presumably write lock depends on pipe, not port select */
  1009. pp_reg = PP_CONTROL(pipe);
  1010. panel_pipe = pipe;
  1011. } else {
  1012. pp_reg = PP_CONTROL(0);
  1013. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1014. panel_pipe = PIPE_B;
  1015. }
  1016. val = I915_READ(pp_reg);
  1017. if (!(val & PANEL_POWER_ON) ||
  1018. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1019. locked = false;
  1020. I915_STATE_WARN(panel_pipe == pipe && locked,
  1021. "panel assertion failure, pipe %c regs locked\n",
  1022. pipe_name(pipe));
  1023. }
  1024. static void assert_cursor(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. bool cur_state;
  1028. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1029. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1030. else
  1031. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1032. I915_STATE_WARN(cur_state != state,
  1033. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1034. pipe_name(pipe), onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1037. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1038. void assert_pipe(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. bool cur_state;
  1042. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1043. pipe);
  1044. enum intel_display_power_domain power_domain;
  1045. /* if we need the pipe quirk it must be always on */
  1046. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1047. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1048. state = true;
  1049. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1050. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1051. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1052. cur_state = !!(val & PIPECONF_ENABLE);
  1053. intel_display_power_put(dev_priv, power_domain);
  1054. } else {
  1055. cur_state = false;
  1056. }
  1057. I915_STATE_WARN(cur_state != state,
  1058. "pipe %c assertion failure (expected %s, current %s)\n",
  1059. pipe_name(pipe), onoff(state), onoff(cur_state));
  1060. }
  1061. static void assert_plane(struct drm_i915_private *dev_priv,
  1062. enum plane plane, bool state)
  1063. {
  1064. u32 val;
  1065. bool cur_state;
  1066. val = I915_READ(DSPCNTR(plane));
  1067. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1068. I915_STATE_WARN(cur_state != state,
  1069. "plane %c assertion failure (expected %s, current %s)\n",
  1070. plane_name(plane), onoff(state), onoff(cur_state));
  1071. }
  1072. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1073. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1074. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe)
  1076. {
  1077. int i;
  1078. /* Primary planes are fixed to pipes on gen4+ */
  1079. if (INTEL_GEN(dev_priv) >= 4) {
  1080. u32 val = I915_READ(DSPCNTR(pipe));
  1081. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1082. "plane %c assertion failure, should be disabled but not\n",
  1083. plane_name(pipe));
  1084. return;
  1085. }
  1086. /* Need to check both planes against the pipe */
  1087. for_each_pipe(dev_priv, i) {
  1088. u32 val = I915_READ(DSPCNTR(i));
  1089. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1090. DISPPLANE_SEL_PIPE_SHIFT;
  1091. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1092. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1093. plane_name(i), pipe_name(pipe));
  1094. }
  1095. }
  1096. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. int sprite;
  1100. if (INTEL_GEN(dev_priv) >= 9) {
  1101. for_each_sprite(dev_priv, pipe, sprite) {
  1102. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1103. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1104. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1105. sprite, pipe_name(pipe));
  1106. }
  1107. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1108. for_each_sprite(dev_priv, pipe, sprite) {
  1109. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1110. I915_STATE_WARN(val & SP_ENABLE,
  1111. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1112. sprite_name(pipe, sprite), pipe_name(pipe));
  1113. }
  1114. } else if (INTEL_GEN(dev_priv) >= 7) {
  1115. u32 val = I915_READ(SPRCTL(pipe));
  1116. I915_STATE_WARN(val & SPRITE_ENABLE,
  1117. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1118. plane_name(pipe), pipe_name(pipe));
  1119. } else if (INTEL_GEN(dev_priv) >= 5) {
  1120. u32 val = I915_READ(DVSCNTR(pipe));
  1121. I915_STATE_WARN(val & DVS_ENABLE,
  1122. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1123. plane_name(pipe), pipe_name(pipe));
  1124. }
  1125. }
  1126. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1127. {
  1128. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1129. drm_crtc_vblank_put(crtc);
  1130. }
  1131. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe)
  1133. {
  1134. u32 val;
  1135. bool enabled;
  1136. val = I915_READ(PCH_TRANSCONF(pipe));
  1137. enabled = !!(val & TRANS_ENABLE);
  1138. I915_STATE_WARN(enabled,
  1139. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1140. pipe_name(pipe));
  1141. }
  1142. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1143. enum pipe pipe, u32 port_sel, u32 val)
  1144. {
  1145. if ((val & DP_PORT_EN) == 0)
  1146. return false;
  1147. if (HAS_PCH_CPT(dev_priv)) {
  1148. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1149. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1150. return false;
  1151. } else if (IS_CHERRYVIEW(dev_priv)) {
  1152. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1153. return false;
  1154. } else {
  1155. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe, u32 val)
  1162. {
  1163. if ((val & SDVO_ENABLE) == 0)
  1164. return false;
  1165. if (HAS_PCH_CPT(dev_priv)) {
  1166. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1167. return false;
  1168. } else if (IS_CHERRYVIEW(dev_priv)) {
  1169. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1170. return false;
  1171. } else {
  1172. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1178. enum pipe pipe, u32 val)
  1179. {
  1180. if ((val & LVDS_PORT_EN) == 0)
  1181. return false;
  1182. if (HAS_PCH_CPT(dev_priv)) {
  1183. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1184. return false;
  1185. } else {
  1186. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1187. return false;
  1188. }
  1189. return true;
  1190. }
  1191. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1192. enum pipe pipe, u32 val)
  1193. {
  1194. if ((val & ADPA_DAC_ENABLE) == 0)
  1195. return false;
  1196. if (HAS_PCH_CPT(dev_priv)) {
  1197. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1198. return false;
  1199. } else {
  1200. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe, i915_reg_t reg,
  1207. u32 port_sel)
  1208. {
  1209. u32 val = I915_READ(reg);
  1210. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1211. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1212. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1213. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1214. && (val & DP_PIPEB_SELECT),
  1215. "IBX PCH dp port still using transcoder B\n");
  1216. }
  1217. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, i915_reg_t reg)
  1219. {
  1220. u32 val = I915_READ(reg);
  1221. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1222. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1223. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1224. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1225. && (val & SDVO_PIPE_B_SELECT),
  1226. "IBX PCH hdmi port still using transcoder B\n");
  1227. }
  1228. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1229. enum pipe pipe)
  1230. {
  1231. u32 val;
  1232. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1233. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1234. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1235. val = I915_READ(PCH_ADPA);
  1236. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1237. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1238. pipe_name(pipe));
  1239. val = I915_READ(PCH_LVDS);
  1240. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1241. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1242. pipe_name(pipe));
  1243. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1244. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1245. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1246. }
  1247. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1248. const struct intel_crtc_state *pipe_config)
  1249. {
  1250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1251. enum pipe pipe = crtc->pipe;
  1252. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1253. POSTING_READ(DPLL(pipe));
  1254. udelay(150);
  1255. if (intel_wait_for_register(dev_priv,
  1256. DPLL(pipe),
  1257. DPLL_LOCK_VLV,
  1258. DPLL_LOCK_VLV,
  1259. 1))
  1260. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1261. }
  1262. static void vlv_enable_pll(struct intel_crtc *crtc,
  1263. const struct intel_crtc_state *pipe_config)
  1264. {
  1265. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1266. enum pipe pipe = crtc->pipe;
  1267. assert_pipe_disabled(dev_priv, pipe);
  1268. /* PLL is protected by panel, make sure we can write it */
  1269. assert_panel_unlocked(dev_priv, pipe);
  1270. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1271. _vlv_enable_pll(crtc, pipe_config);
  1272. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1273. POSTING_READ(DPLL_MD(pipe));
  1274. }
  1275. static void _chv_enable_pll(struct intel_crtc *crtc,
  1276. const struct intel_crtc_state *pipe_config)
  1277. {
  1278. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1279. enum pipe pipe = crtc->pipe;
  1280. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1281. u32 tmp;
  1282. mutex_lock(&dev_priv->sb_lock);
  1283. /* Enable back the 10bit clock to display controller */
  1284. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1285. tmp |= DPIO_DCLKP_EN;
  1286. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1287. mutex_unlock(&dev_priv->sb_lock);
  1288. /*
  1289. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1290. */
  1291. udelay(1);
  1292. /* Enable PLL */
  1293. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1294. /* Check PLL is locked */
  1295. if (intel_wait_for_register(dev_priv,
  1296. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1297. 1))
  1298. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1299. }
  1300. static void chv_enable_pll(struct intel_crtc *crtc,
  1301. const struct intel_crtc_state *pipe_config)
  1302. {
  1303. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1304. enum pipe pipe = crtc->pipe;
  1305. assert_pipe_disabled(dev_priv, pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. assert_panel_unlocked(dev_priv, pipe);
  1308. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1309. _chv_enable_pll(crtc, pipe_config);
  1310. if (pipe != PIPE_A) {
  1311. /*
  1312. * WaPixelRepeatModeFixForC0:chv
  1313. *
  1314. * DPLLCMD is AWOL. Use chicken bits to propagate
  1315. * the value from DPLLBMD to either pipe B or C.
  1316. */
  1317. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1318. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1319. I915_WRITE(CBR4_VLV, 0);
  1320. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1321. /*
  1322. * DPLLB VGA mode also seems to cause problems.
  1323. * We should always have it disabled.
  1324. */
  1325. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1326. } else {
  1327. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1328. POSTING_READ(DPLL_MD(pipe));
  1329. }
  1330. }
  1331. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1332. {
  1333. struct intel_crtc *crtc;
  1334. int count = 0;
  1335. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1336. count += crtc->base.state->active &&
  1337. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1338. }
  1339. return count;
  1340. }
  1341. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. i915_reg_t reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* PLL is protected by panel, make sure we can write it */
  1348. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1349. assert_panel_unlocked(dev_priv, crtc->pipe);
  1350. /* Enable DVO 2x clock on both PLLs if necessary */
  1351. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1352. /*
  1353. * It appears to be important that we don't enable this
  1354. * for the current pipe before otherwise configuring the
  1355. * PLL. No idea how this should be handled if multiple
  1356. * DVO outputs are enabled simultaneosly.
  1357. */
  1358. dpll |= DPLL_DVO_2X_MODE;
  1359. I915_WRITE(DPLL(!crtc->pipe),
  1360. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1361. }
  1362. /*
  1363. * Apparently we need to have VGA mode enabled prior to changing
  1364. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1365. * dividers, even though the register value does change.
  1366. */
  1367. I915_WRITE(reg, 0);
  1368. I915_WRITE(reg, dpll);
  1369. /* Wait for the clocks to stabilize. */
  1370. POSTING_READ(reg);
  1371. udelay(150);
  1372. if (INTEL_GEN(dev_priv) >= 4) {
  1373. I915_WRITE(DPLL_MD(crtc->pipe),
  1374. crtc->config->dpll_hw_state.dpll_md);
  1375. } else {
  1376. /* The pixel multiplier can only be updated once the
  1377. * DPLL is enabled and the clocks are stable.
  1378. *
  1379. * So write it again.
  1380. */
  1381. I915_WRITE(reg, dpll);
  1382. }
  1383. /* We do this three times for luck */
  1384. I915_WRITE(reg, dpll);
  1385. POSTING_READ(reg);
  1386. udelay(150); /* wait for warmup */
  1387. I915_WRITE(reg, dpll);
  1388. POSTING_READ(reg);
  1389. udelay(150); /* wait for warmup */
  1390. I915_WRITE(reg, dpll);
  1391. POSTING_READ(reg);
  1392. udelay(150); /* wait for warmup */
  1393. }
  1394. /**
  1395. * i9xx_disable_pll - disable a PLL
  1396. * @dev_priv: i915 private structure
  1397. * @pipe: pipe PLL to disable
  1398. *
  1399. * Disable the PLL for @pipe, making sure the pipe is off first.
  1400. *
  1401. * Note! This is for pre-ILK only.
  1402. */
  1403. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1404. {
  1405. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1406. enum pipe pipe = crtc->pipe;
  1407. /* Disable DVO 2x clock on both PLLs if necessary */
  1408. if (IS_I830(dev_priv) &&
  1409. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1410. !intel_num_dvo_pipes(dev_priv)) {
  1411. I915_WRITE(DPLL(PIPE_B),
  1412. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1413. I915_WRITE(DPLL(PIPE_A),
  1414. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1415. }
  1416. /* Don't disable pipe or pipe PLLs if needed */
  1417. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1418. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1419. return;
  1420. /* Make sure the pipe isn't still relying on us */
  1421. assert_pipe_disabled(dev_priv, pipe);
  1422. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1423. POSTING_READ(DPLL(pipe));
  1424. }
  1425. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1426. {
  1427. u32 val;
  1428. /* Make sure the pipe isn't still relying on us */
  1429. assert_pipe_disabled(dev_priv, pipe);
  1430. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1431. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1432. if (pipe != PIPE_A)
  1433. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1434. I915_WRITE(DPLL(pipe), val);
  1435. POSTING_READ(DPLL(pipe));
  1436. }
  1437. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1438. {
  1439. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1440. u32 val;
  1441. /* Make sure the pipe isn't still relying on us */
  1442. assert_pipe_disabled(dev_priv, pipe);
  1443. val = DPLL_SSC_REF_CLK_CHV |
  1444. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1445. if (pipe != PIPE_A)
  1446. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1447. I915_WRITE(DPLL(pipe), val);
  1448. POSTING_READ(DPLL(pipe));
  1449. mutex_lock(&dev_priv->sb_lock);
  1450. /* Disable 10bit clock to display controller */
  1451. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1452. val &= ~DPIO_DCLKP_EN;
  1453. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1454. mutex_unlock(&dev_priv->sb_lock);
  1455. }
  1456. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1457. struct intel_digital_port *dport,
  1458. unsigned int expected_mask)
  1459. {
  1460. u32 port_mask;
  1461. i915_reg_t dpll_reg;
  1462. switch (dport->port) {
  1463. case PORT_B:
  1464. port_mask = DPLL_PORTB_READY_MASK;
  1465. dpll_reg = DPLL(0);
  1466. break;
  1467. case PORT_C:
  1468. port_mask = DPLL_PORTC_READY_MASK;
  1469. dpll_reg = DPLL(0);
  1470. expected_mask <<= 4;
  1471. break;
  1472. case PORT_D:
  1473. port_mask = DPLL_PORTD_READY_MASK;
  1474. dpll_reg = DPIO_PHY_STATUS;
  1475. break;
  1476. default:
  1477. BUG();
  1478. }
  1479. if (intel_wait_for_register(dev_priv,
  1480. dpll_reg, port_mask, expected_mask,
  1481. 1000))
  1482. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1483. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1484. }
  1485. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1486. enum pipe pipe)
  1487. {
  1488. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1489. pipe);
  1490. i915_reg_t reg;
  1491. uint32_t val, pipeconf_val;
  1492. /* Make sure PCH DPLL is enabled */
  1493. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1494. /* FDI must be feeding us bits for PCH ports */
  1495. assert_fdi_tx_enabled(dev_priv, pipe);
  1496. assert_fdi_rx_enabled(dev_priv, pipe);
  1497. if (HAS_PCH_CPT(dev_priv)) {
  1498. /* Workaround: Set the timing override bit before enabling the
  1499. * pch transcoder. */
  1500. reg = TRANS_CHICKEN2(pipe);
  1501. val = I915_READ(reg);
  1502. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1503. I915_WRITE(reg, val);
  1504. }
  1505. reg = PCH_TRANSCONF(pipe);
  1506. val = I915_READ(reg);
  1507. pipeconf_val = I915_READ(PIPECONF(pipe));
  1508. if (HAS_PCH_IBX(dev_priv)) {
  1509. /*
  1510. * Make the BPC in transcoder be consistent with
  1511. * that in pipeconf reg. For HDMI we must use 8bpc
  1512. * here for both 8bpc and 12bpc.
  1513. */
  1514. val &= ~PIPECONF_BPC_MASK;
  1515. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1516. val |= PIPECONF_8BPC;
  1517. else
  1518. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1519. }
  1520. val &= ~TRANS_INTERLACE_MASK;
  1521. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1522. if (HAS_PCH_IBX(dev_priv) &&
  1523. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1524. val |= TRANS_LEGACY_INTERLACED_ILK;
  1525. else
  1526. val |= TRANS_INTERLACED;
  1527. else
  1528. val |= TRANS_PROGRESSIVE;
  1529. I915_WRITE(reg, val | TRANS_ENABLE);
  1530. if (intel_wait_for_register(dev_priv,
  1531. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1532. 100))
  1533. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1534. }
  1535. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1536. enum transcoder cpu_transcoder)
  1537. {
  1538. u32 val, pipeconf_val;
  1539. /* FDI must be feeding us bits for PCH ports */
  1540. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1541. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1542. /* Workaround: set timing override bit. */
  1543. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1544. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1545. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1546. val = TRANS_ENABLE;
  1547. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1548. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1549. PIPECONF_INTERLACED_ILK)
  1550. val |= TRANS_INTERLACED;
  1551. else
  1552. val |= TRANS_PROGRESSIVE;
  1553. I915_WRITE(LPT_TRANSCONF, val);
  1554. if (intel_wait_for_register(dev_priv,
  1555. LPT_TRANSCONF,
  1556. TRANS_STATE_ENABLE,
  1557. TRANS_STATE_ENABLE,
  1558. 100))
  1559. DRM_ERROR("Failed to enable PCH transcoder\n");
  1560. }
  1561. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1562. enum pipe pipe)
  1563. {
  1564. i915_reg_t reg;
  1565. uint32_t val;
  1566. /* FDI relies on the transcoder */
  1567. assert_fdi_tx_disabled(dev_priv, pipe);
  1568. assert_fdi_rx_disabled(dev_priv, pipe);
  1569. /* Ports must be off as well */
  1570. assert_pch_ports_disabled(dev_priv, pipe);
  1571. reg = PCH_TRANSCONF(pipe);
  1572. val = I915_READ(reg);
  1573. val &= ~TRANS_ENABLE;
  1574. I915_WRITE(reg, val);
  1575. /* wait for PCH transcoder off, transcoder state */
  1576. if (intel_wait_for_register(dev_priv,
  1577. reg, TRANS_STATE_ENABLE, 0,
  1578. 50))
  1579. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1580. if (HAS_PCH_CPT(dev_priv)) {
  1581. /* Workaround: Clear the timing override chicken bit again. */
  1582. reg = TRANS_CHICKEN2(pipe);
  1583. val = I915_READ(reg);
  1584. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1585. I915_WRITE(reg, val);
  1586. }
  1587. }
  1588. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1589. {
  1590. u32 val;
  1591. val = I915_READ(LPT_TRANSCONF);
  1592. val &= ~TRANS_ENABLE;
  1593. I915_WRITE(LPT_TRANSCONF, val);
  1594. /* wait for PCH transcoder off, transcoder state */
  1595. if (intel_wait_for_register(dev_priv,
  1596. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1597. 50))
  1598. DRM_ERROR("Failed to disable PCH transcoder\n");
  1599. /* Workaround: clear timing override bit. */
  1600. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1603. }
  1604. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1605. {
  1606. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1607. WARN_ON(!crtc->config->has_pch_encoder);
  1608. if (HAS_PCH_LPT(dev_priv))
  1609. return TRANSCODER_A;
  1610. else
  1611. return (enum transcoder) crtc->pipe;
  1612. }
  1613. /**
  1614. * intel_enable_pipe - enable a pipe, asserting requirements
  1615. * @crtc: crtc responsible for the pipe
  1616. *
  1617. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1618. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1619. */
  1620. static void intel_enable_pipe(struct intel_crtc *crtc)
  1621. {
  1622. struct drm_device *dev = crtc->base.dev;
  1623. struct drm_i915_private *dev_priv = to_i915(dev);
  1624. enum pipe pipe = crtc->pipe;
  1625. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1626. i915_reg_t reg;
  1627. u32 val;
  1628. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1629. assert_planes_disabled(dev_priv, pipe);
  1630. assert_cursor_disabled(dev_priv, pipe);
  1631. assert_sprites_disabled(dev_priv, pipe);
  1632. /*
  1633. * A pipe without a PLL won't actually be able to drive bits from
  1634. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1635. * need the check.
  1636. */
  1637. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1638. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1639. assert_dsi_pll_enabled(dev_priv);
  1640. else
  1641. assert_pll_enabled(dev_priv, pipe);
  1642. } else {
  1643. if (crtc->config->has_pch_encoder) {
  1644. /* if driving the PCH, we need FDI enabled */
  1645. assert_fdi_rx_pll_enabled(dev_priv,
  1646. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1647. assert_fdi_tx_pll_enabled(dev_priv,
  1648. (enum pipe) cpu_transcoder);
  1649. }
  1650. /* FIXME: assert CPU port conditions for SNB+ */
  1651. }
  1652. reg = PIPECONF(cpu_transcoder);
  1653. val = I915_READ(reg);
  1654. if (val & PIPECONF_ENABLE) {
  1655. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1656. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1657. return;
  1658. }
  1659. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1660. POSTING_READ(reg);
  1661. /*
  1662. * Until the pipe starts DSL will read as 0, which would cause
  1663. * an apparent vblank timestamp jump, which messes up also the
  1664. * frame count when it's derived from the timestamps. So let's
  1665. * wait for the pipe to start properly before we call
  1666. * drm_crtc_vblank_on()
  1667. */
  1668. if (dev->max_vblank_count == 0 &&
  1669. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1670. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1671. }
  1672. /**
  1673. * intel_disable_pipe - disable a pipe, asserting requirements
  1674. * @crtc: crtc whose pipes is to be disabled
  1675. *
  1676. * Disable the pipe of @crtc, making sure that various hardware
  1677. * specific requirements are met, if applicable, e.g. plane
  1678. * disabled, panel fitter off, etc.
  1679. *
  1680. * Will wait until the pipe has shut down before returning.
  1681. */
  1682. static void intel_disable_pipe(struct intel_crtc *crtc)
  1683. {
  1684. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1685. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1686. enum pipe pipe = crtc->pipe;
  1687. i915_reg_t reg;
  1688. u32 val;
  1689. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1690. /*
  1691. * Make sure planes won't keep trying to pump pixels to us,
  1692. * or we might hang the display.
  1693. */
  1694. assert_planes_disabled(dev_priv, pipe);
  1695. assert_cursor_disabled(dev_priv, pipe);
  1696. assert_sprites_disabled(dev_priv, pipe);
  1697. reg = PIPECONF(cpu_transcoder);
  1698. val = I915_READ(reg);
  1699. if ((val & PIPECONF_ENABLE) == 0)
  1700. return;
  1701. /*
  1702. * Double wide has implications for planes
  1703. * so best keep it disabled when not needed.
  1704. */
  1705. if (crtc->config->double_wide)
  1706. val &= ~PIPECONF_DOUBLE_WIDE;
  1707. /* Don't disable pipe or pipe PLLs if needed */
  1708. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1709. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1710. val &= ~PIPECONF_ENABLE;
  1711. I915_WRITE(reg, val);
  1712. if ((val & PIPECONF_ENABLE) == 0)
  1713. intel_wait_for_pipe_off(crtc);
  1714. }
  1715. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1716. {
  1717. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1718. }
  1719. static unsigned int
  1720. intel_tile_width_bytes(const struct drm_framebuffer *fb, int plane)
  1721. {
  1722. struct drm_i915_private *dev_priv = to_i915(fb->dev);
  1723. unsigned int cpp = fb->format->cpp[plane];
  1724. switch (fb->modifier) {
  1725. case DRM_FORMAT_MOD_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(primary, &intel_crtc->base);
  2370. return;
  2371. valid_fb:
  2372. mutex_lock(&dev->struct_mutex);
  2373. intel_state->vma =
  2374. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2375. mutex_unlock(&dev->struct_mutex);
  2376. if (IS_ERR(intel_state->vma)) {
  2377. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2378. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2379. intel_state->vma = NULL;
  2380. drm_framebuffer_unreference(fb);
  2381. return;
  2382. }
  2383. plane_state->src_x = 0;
  2384. plane_state->src_y = 0;
  2385. plane_state->src_w = fb->width << 16;
  2386. plane_state->src_h = fb->height << 16;
  2387. plane_state->crtc_x = 0;
  2388. plane_state->crtc_y = 0;
  2389. plane_state->crtc_w = fb->width;
  2390. plane_state->crtc_h = fb->height;
  2391. intel_state->base.src = drm_plane_state_src(plane_state);
  2392. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2393. obj = intel_fb_obj(fb);
  2394. if (i915_gem_object_is_tiled(obj))
  2395. dev_priv->preserve_bios_swizzle = true;
  2396. drm_framebuffer_reference(fb);
  2397. primary->fb = primary->state->fb = fb;
  2398. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2399. intel_set_plane_visible(to_intel_crtc_state(crtc_state),
  2400. to_intel_plane_state(plane_state),
  2401. true);
  2402. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2403. &obj->frontbuffer_bits);
  2404. }
  2405. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2406. unsigned int rotation)
  2407. {
  2408. int cpp = fb->format->cpp[plane];
  2409. switch (fb->modifier) {
  2410. case DRM_FORMAT_MOD_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. if (crtc->pipe == PIPE_B)
  2564. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2565. }
  2566. switch (fb->format->format) {
  2567. case DRM_FORMAT_C8:
  2568. dspcntr |= DISPPLANE_8BPP;
  2569. break;
  2570. case DRM_FORMAT_XRGB1555:
  2571. dspcntr |= DISPPLANE_BGRX555;
  2572. break;
  2573. case DRM_FORMAT_RGB565:
  2574. dspcntr |= DISPPLANE_BGRX565;
  2575. break;
  2576. case DRM_FORMAT_XRGB8888:
  2577. dspcntr |= DISPPLANE_BGRX888;
  2578. break;
  2579. case DRM_FORMAT_XBGR8888:
  2580. dspcntr |= DISPPLANE_RGBX888;
  2581. break;
  2582. case DRM_FORMAT_XRGB2101010:
  2583. dspcntr |= DISPPLANE_BGRX101010;
  2584. break;
  2585. case DRM_FORMAT_XBGR2101010:
  2586. dspcntr |= DISPPLANE_RGBX101010;
  2587. break;
  2588. default:
  2589. MISSING_CASE(fb->format->format);
  2590. return 0;
  2591. }
  2592. if (INTEL_GEN(dev_priv) >= 4 &&
  2593. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2594. dspcntr |= DISPPLANE_TILED;
  2595. if (rotation & DRM_ROTATE_180)
  2596. dspcntr |= DISPPLANE_ROTATE_180;
  2597. if (rotation & DRM_REFLECT_X)
  2598. dspcntr |= DISPPLANE_MIRROR;
  2599. return dspcntr;
  2600. }
  2601. int i9xx_check_plane_surface(struct intel_plane_state *plane_state)
  2602. {
  2603. struct drm_i915_private *dev_priv =
  2604. to_i915(plane_state->base.plane->dev);
  2605. int src_x = plane_state->base.src.x1 >> 16;
  2606. int src_y = plane_state->base.src.y1 >> 16;
  2607. u32 offset;
  2608. intel_add_fb_offsets(&src_x, &src_y, plane_state, 0);
  2609. if (INTEL_GEN(dev_priv) >= 4)
  2610. offset = intel_compute_tile_offset(&src_x, &src_y,
  2611. plane_state, 0);
  2612. else
  2613. offset = 0;
  2614. /* HSW/BDW do this automagically in hardware */
  2615. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2616. unsigned int rotation = plane_state->base.rotation;
  2617. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2618. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2619. if (rotation & DRM_ROTATE_180) {
  2620. src_x += src_w - 1;
  2621. src_y += src_h - 1;
  2622. } else if (rotation & DRM_REFLECT_X) {
  2623. src_x += src_w - 1;
  2624. }
  2625. }
  2626. plane_state->main.offset = offset;
  2627. plane_state->main.x = src_x;
  2628. plane_state->main.y = src_y;
  2629. return 0;
  2630. }
  2631. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2632. const struct intel_crtc_state *crtc_state,
  2633. const struct intel_plane_state *plane_state)
  2634. {
  2635. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2636. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2637. struct drm_framebuffer *fb = plane_state->base.fb;
  2638. int plane = intel_crtc->plane;
  2639. u32 linear_offset;
  2640. u32 dspcntr = plane_state->ctl;
  2641. i915_reg_t reg = DSPCNTR(plane);
  2642. int x = plane_state->main.x;
  2643. int y = plane_state->main.y;
  2644. unsigned long irqflags;
  2645. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2646. if (INTEL_GEN(dev_priv) >= 4)
  2647. intel_crtc->dspaddr_offset = plane_state->main.offset;
  2648. else
  2649. intel_crtc->dspaddr_offset = linear_offset;
  2650. intel_crtc->adjusted_x = x;
  2651. intel_crtc->adjusted_y = y;
  2652. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2653. if (INTEL_GEN(dev_priv) < 4) {
  2654. /* pipesrc and dspsize control the size that is scaled from,
  2655. * which should always be the user's requested size.
  2656. */
  2657. I915_WRITE_FW(DSPSIZE(plane),
  2658. ((crtc_state->pipe_src_h - 1) << 16) |
  2659. (crtc_state->pipe_src_w - 1));
  2660. I915_WRITE_FW(DSPPOS(plane), 0);
  2661. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2662. I915_WRITE_FW(PRIMSIZE(plane),
  2663. ((crtc_state->pipe_src_h - 1) << 16) |
  2664. (crtc_state->pipe_src_w - 1));
  2665. I915_WRITE_FW(PRIMPOS(plane), 0);
  2666. I915_WRITE_FW(PRIMCNSTALPHA(plane), 0);
  2667. }
  2668. I915_WRITE_FW(reg, dspcntr);
  2669. I915_WRITE_FW(DSPSTRIDE(plane), fb->pitches[0]);
  2670. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2671. I915_WRITE_FW(DSPSURF(plane),
  2672. intel_plane_ggtt_offset(plane_state) +
  2673. intel_crtc->dspaddr_offset);
  2674. I915_WRITE_FW(DSPOFFSET(plane), (y << 16) | x);
  2675. } else if (INTEL_GEN(dev_priv) >= 4) {
  2676. I915_WRITE_FW(DSPSURF(plane),
  2677. intel_plane_ggtt_offset(plane_state) +
  2678. intel_crtc->dspaddr_offset);
  2679. I915_WRITE_FW(DSPTILEOFF(plane), (y << 16) | x);
  2680. I915_WRITE_FW(DSPLINOFF(plane), linear_offset);
  2681. } else {
  2682. I915_WRITE_FW(DSPADDR(plane),
  2683. intel_plane_ggtt_offset(plane_state) +
  2684. intel_crtc->dspaddr_offset);
  2685. }
  2686. POSTING_READ_FW(reg);
  2687. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2688. }
  2689. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2690. struct drm_crtc *crtc)
  2691. {
  2692. struct drm_device *dev = crtc->dev;
  2693. struct drm_i915_private *dev_priv = to_i915(dev);
  2694. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2695. int plane = intel_crtc->plane;
  2696. unsigned long irqflags;
  2697. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2698. I915_WRITE_FW(DSPCNTR(plane), 0);
  2699. if (INTEL_INFO(dev_priv)->gen >= 4)
  2700. I915_WRITE_FW(DSPSURF(plane), 0);
  2701. else
  2702. I915_WRITE_FW(DSPADDR(plane), 0);
  2703. POSTING_READ_FW(DSPCNTR(plane));
  2704. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2705. }
  2706. static u32
  2707. intel_fb_stride_alignment(const struct drm_framebuffer *fb, int plane)
  2708. {
  2709. if (fb->modifier == DRM_FORMAT_MOD_LINEAR)
  2710. return 64;
  2711. else
  2712. return intel_tile_width_bytes(fb, plane);
  2713. }
  2714. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2715. {
  2716. struct drm_device *dev = intel_crtc->base.dev;
  2717. struct drm_i915_private *dev_priv = to_i915(dev);
  2718. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2719. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2720. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2721. }
  2722. /*
  2723. * This function detaches (aka. unbinds) unused scalers in hardware
  2724. */
  2725. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2726. {
  2727. struct intel_crtc_scaler_state *scaler_state;
  2728. int i;
  2729. scaler_state = &intel_crtc->config->scaler_state;
  2730. /* loop through and disable scalers that aren't in use */
  2731. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2732. if (!scaler_state->scalers[i].in_use)
  2733. skl_detach_scaler(intel_crtc, i);
  2734. }
  2735. }
  2736. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2737. unsigned int rotation)
  2738. {
  2739. u32 stride;
  2740. if (plane >= fb->format->num_planes)
  2741. return 0;
  2742. stride = intel_fb_pitch(fb, plane, rotation);
  2743. /*
  2744. * The stride is either expressed as a multiple of 64 bytes chunks for
  2745. * linear buffers or in number of tiles for tiled buffers.
  2746. */
  2747. if (drm_rotation_90_or_270(rotation))
  2748. stride /= intel_tile_height(fb, plane);
  2749. else
  2750. stride /= intel_fb_stride_alignment(fb, plane);
  2751. return stride;
  2752. }
  2753. static u32 skl_plane_ctl_format(uint32_t pixel_format)
  2754. {
  2755. switch (pixel_format) {
  2756. case DRM_FORMAT_C8:
  2757. return PLANE_CTL_FORMAT_INDEXED;
  2758. case DRM_FORMAT_RGB565:
  2759. return PLANE_CTL_FORMAT_RGB_565;
  2760. case DRM_FORMAT_XBGR8888:
  2761. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2762. case DRM_FORMAT_XRGB8888:
  2763. return PLANE_CTL_FORMAT_XRGB_8888;
  2764. /*
  2765. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2766. * to be already pre-multiplied. We need to add a knob (or a different
  2767. * DRM_FORMAT) for user-space to configure that.
  2768. */
  2769. case DRM_FORMAT_ABGR8888:
  2770. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2771. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2772. case DRM_FORMAT_ARGB8888:
  2773. return PLANE_CTL_FORMAT_XRGB_8888 |
  2774. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2775. case DRM_FORMAT_XRGB2101010:
  2776. return PLANE_CTL_FORMAT_XRGB_2101010;
  2777. case DRM_FORMAT_XBGR2101010:
  2778. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2779. case DRM_FORMAT_YUYV:
  2780. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2781. case DRM_FORMAT_YVYU:
  2782. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2783. case DRM_FORMAT_UYVY:
  2784. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2785. case DRM_FORMAT_VYUY:
  2786. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2787. default:
  2788. MISSING_CASE(pixel_format);
  2789. }
  2790. return 0;
  2791. }
  2792. static u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2793. {
  2794. switch (fb_modifier) {
  2795. case DRM_FORMAT_MOD_LINEAR:
  2796. break;
  2797. case I915_FORMAT_MOD_X_TILED:
  2798. return PLANE_CTL_TILED_X;
  2799. case I915_FORMAT_MOD_Y_TILED:
  2800. return PLANE_CTL_TILED_Y;
  2801. case I915_FORMAT_MOD_Yf_TILED:
  2802. return PLANE_CTL_TILED_YF;
  2803. default:
  2804. MISSING_CASE(fb_modifier);
  2805. }
  2806. return 0;
  2807. }
  2808. static u32 skl_plane_ctl_rotation(unsigned int rotation)
  2809. {
  2810. switch (rotation) {
  2811. case DRM_ROTATE_0:
  2812. break;
  2813. /*
  2814. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2815. * while i915 HW rotation is clockwise, thats why this swapping.
  2816. */
  2817. case DRM_ROTATE_90:
  2818. return PLANE_CTL_ROTATE_270;
  2819. case DRM_ROTATE_180:
  2820. return PLANE_CTL_ROTATE_180;
  2821. case DRM_ROTATE_270:
  2822. return PLANE_CTL_ROTATE_90;
  2823. default:
  2824. MISSING_CASE(rotation);
  2825. }
  2826. return 0;
  2827. }
  2828. u32 skl_plane_ctl(const struct intel_crtc_state *crtc_state,
  2829. const struct intel_plane_state *plane_state)
  2830. {
  2831. struct drm_i915_private *dev_priv =
  2832. to_i915(plane_state->base.plane->dev);
  2833. const struct drm_framebuffer *fb = plane_state->base.fb;
  2834. unsigned int rotation = plane_state->base.rotation;
  2835. const struct drm_intel_sprite_colorkey *key = &plane_state->ckey;
  2836. u32 plane_ctl;
  2837. plane_ctl = PLANE_CTL_ENABLE;
  2838. if (!IS_GEMINILAKE(dev_priv)) {
  2839. plane_ctl |=
  2840. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2841. PLANE_CTL_PIPE_CSC_ENABLE |
  2842. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2843. }
  2844. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2845. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2846. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2847. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  2848. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  2849. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  2850. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  2851. return plane_ctl;
  2852. }
  2853. static void skylake_update_primary_plane(struct drm_plane *plane,
  2854. const struct intel_crtc_state *crtc_state,
  2855. const struct intel_plane_state *plane_state)
  2856. {
  2857. struct drm_device *dev = plane->dev;
  2858. struct drm_i915_private *dev_priv = to_i915(dev);
  2859. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2860. struct drm_framebuffer *fb = plane_state->base.fb;
  2861. enum plane_id plane_id = to_intel_plane(plane)->id;
  2862. enum pipe pipe = to_intel_plane(plane)->pipe;
  2863. u32 plane_ctl = plane_state->ctl;
  2864. unsigned int rotation = plane_state->base.rotation;
  2865. u32 stride = skl_plane_stride(fb, 0, rotation);
  2866. u32 surf_addr = plane_state->main.offset;
  2867. int scaler_id = plane_state->scaler_id;
  2868. int src_x = plane_state->main.x;
  2869. int src_y = plane_state->main.y;
  2870. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2871. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2872. int dst_x = plane_state->base.dst.x1;
  2873. int dst_y = plane_state->base.dst.y1;
  2874. int dst_w = drm_rect_width(&plane_state->base.dst);
  2875. int dst_h = drm_rect_height(&plane_state->base.dst);
  2876. unsigned long irqflags;
  2877. /* Sizes are 0 based */
  2878. src_w--;
  2879. src_h--;
  2880. dst_w--;
  2881. dst_h--;
  2882. intel_crtc->dspaddr_offset = surf_addr;
  2883. intel_crtc->adjusted_x = src_x;
  2884. intel_crtc->adjusted_y = src_y;
  2885. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2886. if (IS_GEMINILAKE(dev_priv)) {
  2887. I915_WRITE_FW(PLANE_COLOR_CTL(pipe, plane_id),
  2888. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2889. PLANE_COLOR_PIPE_CSC_ENABLE |
  2890. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2891. }
  2892. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), plane_ctl);
  2893. I915_WRITE_FW(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2894. I915_WRITE_FW(PLANE_STRIDE(pipe, plane_id), stride);
  2895. I915_WRITE_FW(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2896. if (scaler_id >= 0) {
  2897. uint32_t ps_ctrl = 0;
  2898. WARN_ON(!dst_w || !dst_h);
  2899. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2900. crtc_state->scaler_state.scalers[scaler_id].mode;
  2901. I915_WRITE_FW(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2902. I915_WRITE_FW(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2903. I915_WRITE_FW(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2904. I915_WRITE_FW(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2905. I915_WRITE_FW(PLANE_POS(pipe, plane_id), 0);
  2906. } else {
  2907. I915_WRITE_FW(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2908. }
  2909. I915_WRITE_FW(PLANE_SURF(pipe, plane_id),
  2910. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2911. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2912. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2913. }
  2914. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2915. struct drm_crtc *crtc)
  2916. {
  2917. struct drm_device *dev = crtc->dev;
  2918. struct drm_i915_private *dev_priv = to_i915(dev);
  2919. enum plane_id plane_id = to_intel_plane(primary)->id;
  2920. enum pipe pipe = to_intel_plane(primary)->pipe;
  2921. unsigned long irqflags;
  2922. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  2923. I915_WRITE_FW(PLANE_CTL(pipe, plane_id), 0);
  2924. I915_WRITE_FW(PLANE_SURF(pipe, plane_id), 0);
  2925. POSTING_READ_FW(PLANE_SURF(pipe, plane_id));
  2926. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  2927. }
  2928. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2929. {
  2930. struct intel_crtc *crtc;
  2931. for_each_intel_crtc(&dev_priv->drm, crtc)
  2932. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2933. }
  2934. static void intel_update_primary_planes(struct drm_device *dev)
  2935. {
  2936. struct drm_crtc *crtc;
  2937. for_each_crtc(dev, crtc) {
  2938. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2939. struct intel_plane_state *plane_state =
  2940. to_intel_plane_state(plane->base.state);
  2941. if (plane_state->base.visible) {
  2942. trace_intel_update_plane(&plane->base,
  2943. to_intel_crtc(crtc));
  2944. plane->update_plane(&plane->base,
  2945. to_intel_crtc_state(crtc->state),
  2946. plane_state);
  2947. }
  2948. }
  2949. }
  2950. static int
  2951. __intel_display_resume(struct drm_device *dev,
  2952. struct drm_atomic_state *state,
  2953. struct drm_modeset_acquire_ctx *ctx)
  2954. {
  2955. struct drm_crtc_state *crtc_state;
  2956. struct drm_crtc *crtc;
  2957. int i, ret;
  2958. intel_modeset_setup_hw_state(dev);
  2959. i915_redisable_vga(to_i915(dev));
  2960. if (!state)
  2961. return 0;
  2962. /*
  2963. * We've duplicated the state, pointers to the old state are invalid.
  2964. *
  2965. * Don't attempt to use the old state until we commit the duplicated state.
  2966. */
  2967. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  2968. /*
  2969. * Force recalculation even if we restore
  2970. * current state. With fast modeset this may not result
  2971. * in a modeset when the state is compatible.
  2972. */
  2973. crtc_state->mode_changed = true;
  2974. }
  2975. /* ignore any reset values/BIOS leftovers in the WM registers */
  2976. if (!HAS_GMCH_DISPLAY(to_i915(dev)))
  2977. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2978. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  2979. WARN_ON(ret == -EDEADLK);
  2980. return ret;
  2981. }
  2982. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  2983. {
  2984. return intel_has_gpu_reset(dev_priv) &&
  2985. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  2986. }
  2987. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2988. {
  2989. struct drm_device *dev = &dev_priv->drm;
  2990. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2991. struct drm_atomic_state *state;
  2992. int ret;
  2993. /*
  2994. * Need mode_config.mutex so that we don't
  2995. * trample ongoing ->detect() and whatnot.
  2996. */
  2997. mutex_lock(&dev->mode_config.mutex);
  2998. drm_modeset_acquire_init(ctx, 0);
  2999. while (1) {
  3000. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3001. if (ret != -EDEADLK)
  3002. break;
  3003. drm_modeset_backoff(ctx);
  3004. }
  3005. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3006. if (!i915.force_reset_modeset_test &&
  3007. !gpu_reset_clobbers_display(dev_priv))
  3008. return;
  3009. /*
  3010. * Disabling the crtcs gracefully seems nicer. Also the
  3011. * g33 docs say we should at least disable all the planes.
  3012. */
  3013. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3014. if (IS_ERR(state)) {
  3015. ret = PTR_ERR(state);
  3016. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3017. return;
  3018. }
  3019. ret = drm_atomic_helper_disable_all(dev, ctx);
  3020. if (ret) {
  3021. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3022. drm_atomic_state_put(state);
  3023. return;
  3024. }
  3025. dev_priv->modeset_restore_state = state;
  3026. state->acquire_ctx = ctx;
  3027. }
  3028. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3029. {
  3030. struct drm_device *dev = &dev_priv->drm;
  3031. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3032. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3033. int ret;
  3034. /*
  3035. * Flips in the rings will be nuked by the reset,
  3036. * so complete all pending flips so that user space
  3037. * will get its events and not get stuck.
  3038. */
  3039. intel_complete_page_flips(dev_priv);
  3040. dev_priv->modeset_restore_state = NULL;
  3041. /* reset doesn't touch the display */
  3042. if (!gpu_reset_clobbers_display(dev_priv)) {
  3043. if (!state) {
  3044. /*
  3045. * Flips in the rings have been nuked by the reset,
  3046. * so update the base address of all primary
  3047. * planes to the the last fb to make sure we're
  3048. * showing the correct fb after a reset.
  3049. *
  3050. * FIXME: Atomic will make this obsolete since we won't schedule
  3051. * CS-based flips (which might get lost in gpu resets) any more.
  3052. */
  3053. intel_update_primary_planes(dev);
  3054. } else {
  3055. ret = __intel_display_resume(dev, state, ctx);
  3056. if (ret)
  3057. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3058. }
  3059. } else {
  3060. /*
  3061. * The display has been reset as well,
  3062. * so need a full re-initialization.
  3063. */
  3064. intel_runtime_pm_disable_interrupts(dev_priv);
  3065. intel_runtime_pm_enable_interrupts(dev_priv);
  3066. intel_pps_unlock_regs_wa(dev_priv);
  3067. intel_modeset_init_hw(dev);
  3068. spin_lock_irq(&dev_priv->irq_lock);
  3069. if (dev_priv->display.hpd_irq_setup)
  3070. dev_priv->display.hpd_irq_setup(dev_priv);
  3071. spin_unlock_irq(&dev_priv->irq_lock);
  3072. ret = __intel_display_resume(dev, state, ctx);
  3073. if (ret)
  3074. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3075. intel_hpd_init(dev_priv);
  3076. }
  3077. if (state)
  3078. drm_atomic_state_put(state);
  3079. drm_modeset_drop_locks(ctx);
  3080. drm_modeset_acquire_fini(ctx);
  3081. mutex_unlock(&dev->mode_config.mutex);
  3082. }
  3083. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3084. {
  3085. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3086. if (i915_reset_backoff(error))
  3087. return true;
  3088. if (crtc->reset_count != i915_reset_count(error))
  3089. return true;
  3090. return false;
  3091. }
  3092. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3093. {
  3094. struct drm_device *dev = crtc->dev;
  3095. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3096. bool pending;
  3097. if (abort_flip_on_reset(intel_crtc))
  3098. return false;
  3099. spin_lock_irq(&dev->event_lock);
  3100. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3101. spin_unlock_irq(&dev->event_lock);
  3102. return pending;
  3103. }
  3104. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3105. struct intel_crtc_state *old_crtc_state)
  3106. {
  3107. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3108. struct intel_crtc_state *pipe_config =
  3109. to_intel_crtc_state(crtc->base.state);
  3110. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3111. crtc->base.mode = crtc->base.state->mode;
  3112. /*
  3113. * Update pipe size and adjust fitter if needed: the reason for this is
  3114. * that in compute_mode_changes we check the native mode (not the pfit
  3115. * mode) to see if we can flip rather than do a full mode set. In the
  3116. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3117. * pfit state, we'll end up with a big fb scanned out into the wrong
  3118. * sized surface.
  3119. */
  3120. I915_WRITE(PIPESRC(crtc->pipe),
  3121. ((pipe_config->pipe_src_w - 1) << 16) |
  3122. (pipe_config->pipe_src_h - 1));
  3123. /* on skylake this is done by detaching scalers */
  3124. if (INTEL_GEN(dev_priv) >= 9) {
  3125. skl_detach_scalers(crtc);
  3126. if (pipe_config->pch_pfit.enabled)
  3127. skylake_pfit_enable(crtc);
  3128. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3129. if (pipe_config->pch_pfit.enabled)
  3130. ironlake_pfit_enable(crtc);
  3131. else if (old_crtc_state->pch_pfit.enabled)
  3132. ironlake_pfit_disable(crtc, true);
  3133. }
  3134. }
  3135. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3136. {
  3137. struct drm_device *dev = crtc->base.dev;
  3138. struct drm_i915_private *dev_priv = to_i915(dev);
  3139. int pipe = crtc->pipe;
  3140. i915_reg_t reg;
  3141. u32 temp;
  3142. /* enable normal train */
  3143. reg = FDI_TX_CTL(pipe);
  3144. temp = I915_READ(reg);
  3145. if (IS_IVYBRIDGE(dev_priv)) {
  3146. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3147. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3148. } else {
  3149. temp &= ~FDI_LINK_TRAIN_NONE;
  3150. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3151. }
  3152. I915_WRITE(reg, temp);
  3153. reg = FDI_RX_CTL(pipe);
  3154. temp = I915_READ(reg);
  3155. if (HAS_PCH_CPT(dev_priv)) {
  3156. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3157. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3158. } else {
  3159. temp &= ~FDI_LINK_TRAIN_NONE;
  3160. temp |= FDI_LINK_TRAIN_NONE;
  3161. }
  3162. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3163. /* wait one idle pattern time */
  3164. POSTING_READ(reg);
  3165. udelay(1000);
  3166. /* IVB wants error correction enabled */
  3167. if (IS_IVYBRIDGE(dev_priv))
  3168. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3169. FDI_FE_ERRC_ENABLE);
  3170. }
  3171. /* The FDI link training functions for ILK/Ibexpeak. */
  3172. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3173. const struct intel_crtc_state *crtc_state)
  3174. {
  3175. struct drm_device *dev = crtc->base.dev;
  3176. struct drm_i915_private *dev_priv = to_i915(dev);
  3177. int pipe = crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp, tries;
  3180. /* FDI needs bits from pipe first */
  3181. assert_pipe_enabled(dev_priv, pipe);
  3182. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3183. for train result */
  3184. reg = FDI_RX_IMR(pipe);
  3185. temp = I915_READ(reg);
  3186. temp &= ~FDI_RX_SYMBOL_LOCK;
  3187. temp &= ~FDI_RX_BIT_LOCK;
  3188. I915_WRITE(reg, temp);
  3189. I915_READ(reg);
  3190. udelay(150);
  3191. /* enable CPU FDI TX and PCH FDI RX */
  3192. reg = FDI_TX_CTL(pipe);
  3193. temp = I915_READ(reg);
  3194. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3195. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3196. temp &= ~FDI_LINK_TRAIN_NONE;
  3197. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3198. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3199. reg = FDI_RX_CTL(pipe);
  3200. temp = I915_READ(reg);
  3201. temp &= ~FDI_LINK_TRAIN_NONE;
  3202. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3203. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3204. POSTING_READ(reg);
  3205. udelay(150);
  3206. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3207. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3208. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3209. FDI_RX_PHASE_SYNC_POINTER_EN);
  3210. reg = FDI_RX_IIR(pipe);
  3211. for (tries = 0; tries < 5; tries++) {
  3212. temp = I915_READ(reg);
  3213. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3214. if ((temp & FDI_RX_BIT_LOCK)) {
  3215. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3216. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3217. break;
  3218. }
  3219. }
  3220. if (tries == 5)
  3221. DRM_ERROR("FDI train 1 fail!\n");
  3222. /* Train 2 */
  3223. reg = FDI_TX_CTL(pipe);
  3224. temp = I915_READ(reg);
  3225. temp &= ~FDI_LINK_TRAIN_NONE;
  3226. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3227. I915_WRITE(reg, temp);
  3228. reg = FDI_RX_CTL(pipe);
  3229. temp = I915_READ(reg);
  3230. temp &= ~FDI_LINK_TRAIN_NONE;
  3231. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3232. I915_WRITE(reg, temp);
  3233. POSTING_READ(reg);
  3234. udelay(150);
  3235. reg = FDI_RX_IIR(pipe);
  3236. for (tries = 0; tries < 5; tries++) {
  3237. temp = I915_READ(reg);
  3238. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3239. if (temp & FDI_RX_SYMBOL_LOCK) {
  3240. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3241. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3242. break;
  3243. }
  3244. }
  3245. if (tries == 5)
  3246. DRM_ERROR("FDI train 2 fail!\n");
  3247. DRM_DEBUG_KMS("FDI train done\n");
  3248. }
  3249. static const int snb_b_fdi_train_param[] = {
  3250. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3251. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3252. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3253. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3254. };
  3255. /* The FDI link training functions for SNB/Cougarpoint. */
  3256. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3257. const struct intel_crtc_state *crtc_state)
  3258. {
  3259. struct drm_device *dev = crtc->base.dev;
  3260. struct drm_i915_private *dev_priv = to_i915(dev);
  3261. int pipe = crtc->pipe;
  3262. i915_reg_t reg;
  3263. u32 temp, i, retry;
  3264. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3265. for train result */
  3266. reg = FDI_RX_IMR(pipe);
  3267. temp = I915_READ(reg);
  3268. temp &= ~FDI_RX_SYMBOL_LOCK;
  3269. temp &= ~FDI_RX_BIT_LOCK;
  3270. I915_WRITE(reg, temp);
  3271. POSTING_READ(reg);
  3272. udelay(150);
  3273. /* enable CPU FDI TX and PCH FDI RX */
  3274. reg = FDI_TX_CTL(pipe);
  3275. temp = I915_READ(reg);
  3276. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3277. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3278. temp &= ~FDI_LINK_TRAIN_NONE;
  3279. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3280. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3281. /* SNB-B */
  3282. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3283. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3284. I915_WRITE(FDI_RX_MISC(pipe),
  3285. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3286. reg = FDI_RX_CTL(pipe);
  3287. temp = I915_READ(reg);
  3288. if (HAS_PCH_CPT(dev_priv)) {
  3289. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3290. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3291. } else {
  3292. temp &= ~FDI_LINK_TRAIN_NONE;
  3293. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3294. }
  3295. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3296. POSTING_READ(reg);
  3297. udelay(150);
  3298. for (i = 0; i < 4; i++) {
  3299. reg = FDI_TX_CTL(pipe);
  3300. temp = I915_READ(reg);
  3301. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3302. temp |= snb_b_fdi_train_param[i];
  3303. I915_WRITE(reg, temp);
  3304. POSTING_READ(reg);
  3305. udelay(500);
  3306. for (retry = 0; retry < 5; retry++) {
  3307. reg = FDI_RX_IIR(pipe);
  3308. temp = I915_READ(reg);
  3309. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3310. if (temp & FDI_RX_BIT_LOCK) {
  3311. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3312. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3313. break;
  3314. }
  3315. udelay(50);
  3316. }
  3317. if (retry < 5)
  3318. break;
  3319. }
  3320. if (i == 4)
  3321. DRM_ERROR("FDI train 1 fail!\n");
  3322. /* Train 2 */
  3323. reg = FDI_TX_CTL(pipe);
  3324. temp = I915_READ(reg);
  3325. temp &= ~FDI_LINK_TRAIN_NONE;
  3326. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3327. if (IS_GEN6(dev_priv)) {
  3328. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3329. /* SNB-B */
  3330. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3331. }
  3332. I915_WRITE(reg, temp);
  3333. reg = FDI_RX_CTL(pipe);
  3334. temp = I915_READ(reg);
  3335. if (HAS_PCH_CPT(dev_priv)) {
  3336. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3337. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3338. } else {
  3339. temp &= ~FDI_LINK_TRAIN_NONE;
  3340. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3341. }
  3342. I915_WRITE(reg, temp);
  3343. POSTING_READ(reg);
  3344. udelay(150);
  3345. for (i = 0; i < 4; i++) {
  3346. reg = FDI_TX_CTL(pipe);
  3347. temp = I915_READ(reg);
  3348. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3349. temp |= snb_b_fdi_train_param[i];
  3350. I915_WRITE(reg, temp);
  3351. POSTING_READ(reg);
  3352. udelay(500);
  3353. for (retry = 0; retry < 5; retry++) {
  3354. reg = FDI_RX_IIR(pipe);
  3355. temp = I915_READ(reg);
  3356. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3357. if (temp & FDI_RX_SYMBOL_LOCK) {
  3358. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3359. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3360. break;
  3361. }
  3362. udelay(50);
  3363. }
  3364. if (retry < 5)
  3365. break;
  3366. }
  3367. if (i == 4)
  3368. DRM_ERROR("FDI train 2 fail!\n");
  3369. DRM_DEBUG_KMS("FDI train done.\n");
  3370. }
  3371. /* Manual link training for Ivy Bridge A0 parts */
  3372. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3373. const struct intel_crtc_state *crtc_state)
  3374. {
  3375. struct drm_device *dev = crtc->base.dev;
  3376. struct drm_i915_private *dev_priv = to_i915(dev);
  3377. int pipe = crtc->pipe;
  3378. i915_reg_t reg;
  3379. u32 temp, i, j;
  3380. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3381. for train result */
  3382. reg = FDI_RX_IMR(pipe);
  3383. temp = I915_READ(reg);
  3384. temp &= ~FDI_RX_SYMBOL_LOCK;
  3385. temp &= ~FDI_RX_BIT_LOCK;
  3386. I915_WRITE(reg, temp);
  3387. POSTING_READ(reg);
  3388. udelay(150);
  3389. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3390. I915_READ(FDI_RX_IIR(pipe)));
  3391. /* Try each vswing and preemphasis setting twice before moving on */
  3392. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3393. /* disable first in case we need to retry */
  3394. reg = FDI_TX_CTL(pipe);
  3395. temp = I915_READ(reg);
  3396. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3397. temp &= ~FDI_TX_ENABLE;
  3398. I915_WRITE(reg, temp);
  3399. reg = FDI_RX_CTL(pipe);
  3400. temp = I915_READ(reg);
  3401. temp &= ~FDI_LINK_TRAIN_AUTO;
  3402. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3403. temp &= ~FDI_RX_ENABLE;
  3404. I915_WRITE(reg, temp);
  3405. /* enable CPU FDI TX and PCH FDI RX */
  3406. reg = FDI_TX_CTL(pipe);
  3407. temp = I915_READ(reg);
  3408. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3409. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3410. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3411. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3412. temp |= snb_b_fdi_train_param[j/2];
  3413. temp |= FDI_COMPOSITE_SYNC;
  3414. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3415. I915_WRITE(FDI_RX_MISC(pipe),
  3416. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3417. reg = FDI_RX_CTL(pipe);
  3418. temp = I915_READ(reg);
  3419. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3420. temp |= FDI_COMPOSITE_SYNC;
  3421. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3422. POSTING_READ(reg);
  3423. udelay(1); /* should be 0.5us */
  3424. for (i = 0; i < 4; i++) {
  3425. reg = FDI_RX_IIR(pipe);
  3426. temp = I915_READ(reg);
  3427. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3428. if (temp & FDI_RX_BIT_LOCK ||
  3429. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3430. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3431. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3432. i);
  3433. break;
  3434. }
  3435. udelay(1); /* should be 0.5us */
  3436. }
  3437. if (i == 4) {
  3438. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3439. continue;
  3440. }
  3441. /* Train 2 */
  3442. reg = FDI_TX_CTL(pipe);
  3443. temp = I915_READ(reg);
  3444. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3445. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3446. I915_WRITE(reg, temp);
  3447. reg = FDI_RX_CTL(pipe);
  3448. temp = I915_READ(reg);
  3449. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3450. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3451. I915_WRITE(reg, temp);
  3452. POSTING_READ(reg);
  3453. udelay(2); /* should be 1.5us */
  3454. for (i = 0; i < 4; i++) {
  3455. reg = FDI_RX_IIR(pipe);
  3456. temp = I915_READ(reg);
  3457. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3458. if (temp & FDI_RX_SYMBOL_LOCK ||
  3459. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3460. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3461. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3462. i);
  3463. goto train_done;
  3464. }
  3465. udelay(2); /* should be 1.5us */
  3466. }
  3467. if (i == 4)
  3468. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3469. }
  3470. train_done:
  3471. DRM_DEBUG_KMS("FDI train done.\n");
  3472. }
  3473. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3474. {
  3475. struct drm_device *dev = intel_crtc->base.dev;
  3476. struct drm_i915_private *dev_priv = to_i915(dev);
  3477. int pipe = intel_crtc->pipe;
  3478. i915_reg_t reg;
  3479. u32 temp;
  3480. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3481. reg = FDI_RX_CTL(pipe);
  3482. temp = I915_READ(reg);
  3483. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3484. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3485. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3486. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3487. POSTING_READ(reg);
  3488. udelay(200);
  3489. /* Switch from Rawclk to PCDclk */
  3490. temp = I915_READ(reg);
  3491. I915_WRITE(reg, temp | FDI_PCDCLK);
  3492. POSTING_READ(reg);
  3493. udelay(200);
  3494. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3495. reg = FDI_TX_CTL(pipe);
  3496. temp = I915_READ(reg);
  3497. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3498. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3499. POSTING_READ(reg);
  3500. udelay(100);
  3501. }
  3502. }
  3503. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3504. {
  3505. struct drm_device *dev = intel_crtc->base.dev;
  3506. struct drm_i915_private *dev_priv = to_i915(dev);
  3507. int pipe = intel_crtc->pipe;
  3508. i915_reg_t reg;
  3509. u32 temp;
  3510. /* Switch from PCDclk to Rawclk */
  3511. reg = FDI_RX_CTL(pipe);
  3512. temp = I915_READ(reg);
  3513. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3514. /* Disable CPU FDI TX PLL */
  3515. reg = FDI_TX_CTL(pipe);
  3516. temp = I915_READ(reg);
  3517. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3518. POSTING_READ(reg);
  3519. udelay(100);
  3520. reg = FDI_RX_CTL(pipe);
  3521. temp = I915_READ(reg);
  3522. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3523. /* Wait for the clocks to turn off. */
  3524. POSTING_READ(reg);
  3525. udelay(100);
  3526. }
  3527. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3528. {
  3529. struct drm_device *dev = crtc->dev;
  3530. struct drm_i915_private *dev_priv = to_i915(dev);
  3531. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3532. int pipe = intel_crtc->pipe;
  3533. i915_reg_t reg;
  3534. u32 temp;
  3535. /* disable CPU FDI tx and PCH FDI rx */
  3536. reg = FDI_TX_CTL(pipe);
  3537. temp = I915_READ(reg);
  3538. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3539. POSTING_READ(reg);
  3540. reg = FDI_RX_CTL(pipe);
  3541. temp = I915_READ(reg);
  3542. temp &= ~(0x7 << 16);
  3543. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3544. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3545. POSTING_READ(reg);
  3546. udelay(100);
  3547. /* Ironlake workaround, disable clock pointer after downing FDI */
  3548. if (HAS_PCH_IBX(dev_priv))
  3549. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3550. /* still set train pattern 1 */
  3551. reg = FDI_TX_CTL(pipe);
  3552. temp = I915_READ(reg);
  3553. temp &= ~FDI_LINK_TRAIN_NONE;
  3554. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3555. I915_WRITE(reg, temp);
  3556. reg = FDI_RX_CTL(pipe);
  3557. temp = I915_READ(reg);
  3558. if (HAS_PCH_CPT(dev_priv)) {
  3559. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3560. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3561. } else {
  3562. temp &= ~FDI_LINK_TRAIN_NONE;
  3563. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3564. }
  3565. /* BPC in FDI rx is consistent with that in PIPECONF */
  3566. temp &= ~(0x07 << 16);
  3567. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3568. I915_WRITE(reg, temp);
  3569. POSTING_READ(reg);
  3570. udelay(100);
  3571. }
  3572. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3573. {
  3574. struct intel_crtc *crtc;
  3575. /* Note that we don't need to be called with mode_config.lock here
  3576. * as our list of CRTC objects is static for the lifetime of the
  3577. * device and so cannot disappear as we iterate. Similarly, we can
  3578. * happily treat the predicates as racy, atomic checks as userspace
  3579. * cannot claim and pin a new fb without at least acquring the
  3580. * struct_mutex and so serialising with us.
  3581. */
  3582. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3583. if (atomic_read(&crtc->unpin_work_count) == 0)
  3584. continue;
  3585. if (crtc->flip_work)
  3586. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3587. return true;
  3588. }
  3589. return false;
  3590. }
  3591. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3592. {
  3593. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3594. struct intel_flip_work *work = intel_crtc->flip_work;
  3595. intel_crtc->flip_work = NULL;
  3596. if (work->event)
  3597. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3598. drm_crtc_vblank_put(&intel_crtc->base);
  3599. wake_up_all(&dev_priv->pending_flip_queue);
  3600. trace_i915_flip_complete(intel_crtc->plane,
  3601. work->pending_flip_obj);
  3602. queue_work(dev_priv->wq, &work->unpin_work);
  3603. }
  3604. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3605. {
  3606. struct drm_device *dev = crtc->dev;
  3607. struct drm_i915_private *dev_priv = to_i915(dev);
  3608. long ret;
  3609. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3610. ret = wait_event_interruptible_timeout(
  3611. dev_priv->pending_flip_queue,
  3612. !intel_crtc_has_pending_flip(crtc),
  3613. 60*HZ);
  3614. if (ret < 0)
  3615. return ret;
  3616. if (ret == 0) {
  3617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3618. struct intel_flip_work *work;
  3619. spin_lock_irq(&dev->event_lock);
  3620. work = intel_crtc->flip_work;
  3621. if (work && !is_mmio_work(work)) {
  3622. WARN_ONCE(1, "Removing stuck page flip\n");
  3623. page_flip_completed(intel_crtc);
  3624. }
  3625. spin_unlock_irq(&dev->event_lock);
  3626. }
  3627. return 0;
  3628. }
  3629. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3630. {
  3631. u32 temp;
  3632. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3633. mutex_lock(&dev_priv->sb_lock);
  3634. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3635. temp |= SBI_SSCCTL_DISABLE;
  3636. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3637. mutex_unlock(&dev_priv->sb_lock);
  3638. }
  3639. /* Program iCLKIP clock to the desired frequency */
  3640. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3641. {
  3642. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3643. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3644. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3645. u32 temp;
  3646. lpt_disable_iclkip(dev_priv);
  3647. /* The iCLK virtual clock root frequency is in MHz,
  3648. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3649. * divisors, it is necessary to divide one by another, so we
  3650. * convert the virtual clock precision to KHz here for higher
  3651. * precision.
  3652. */
  3653. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3654. u32 iclk_virtual_root_freq = 172800 * 1000;
  3655. u32 iclk_pi_range = 64;
  3656. u32 desired_divisor;
  3657. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3658. clock << auxdiv);
  3659. divsel = (desired_divisor / iclk_pi_range) - 2;
  3660. phaseinc = desired_divisor % iclk_pi_range;
  3661. /*
  3662. * Near 20MHz is a corner case which is
  3663. * out of range for the 7-bit divisor
  3664. */
  3665. if (divsel <= 0x7f)
  3666. break;
  3667. }
  3668. /* This should not happen with any sane values */
  3669. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3670. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3671. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3672. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3673. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3674. clock,
  3675. auxdiv,
  3676. divsel,
  3677. phasedir,
  3678. phaseinc);
  3679. mutex_lock(&dev_priv->sb_lock);
  3680. /* Program SSCDIVINTPHASE6 */
  3681. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3682. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3683. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3684. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3685. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3686. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3687. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3688. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3689. /* Program SSCAUXDIV */
  3690. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3691. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3692. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3693. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3694. /* Enable modulator and associated divider */
  3695. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3696. temp &= ~SBI_SSCCTL_DISABLE;
  3697. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3698. mutex_unlock(&dev_priv->sb_lock);
  3699. /* Wait for initialization time */
  3700. udelay(24);
  3701. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3702. }
  3703. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3704. {
  3705. u32 divsel, phaseinc, auxdiv;
  3706. u32 iclk_virtual_root_freq = 172800 * 1000;
  3707. u32 iclk_pi_range = 64;
  3708. u32 desired_divisor;
  3709. u32 temp;
  3710. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3711. return 0;
  3712. mutex_lock(&dev_priv->sb_lock);
  3713. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3714. if (temp & SBI_SSCCTL_DISABLE) {
  3715. mutex_unlock(&dev_priv->sb_lock);
  3716. return 0;
  3717. }
  3718. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3719. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3720. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3721. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3722. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3723. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3724. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3725. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3726. mutex_unlock(&dev_priv->sb_lock);
  3727. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3728. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3729. desired_divisor << auxdiv);
  3730. }
  3731. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3732. enum pipe pch_transcoder)
  3733. {
  3734. struct drm_device *dev = crtc->base.dev;
  3735. struct drm_i915_private *dev_priv = to_i915(dev);
  3736. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3737. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3738. I915_READ(HTOTAL(cpu_transcoder)));
  3739. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3740. I915_READ(HBLANK(cpu_transcoder)));
  3741. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3742. I915_READ(HSYNC(cpu_transcoder)));
  3743. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3744. I915_READ(VTOTAL(cpu_transcoder)));
  3745. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3746. I915_READ(VBLANK(cpu_transcoder)));
  3747. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3748. I915_READ(VSYNC(cpu_transcoder)));
  3749. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3750. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3751. }
  3752. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3753. {
  3754. struct drm_i915_private *dev_priv = to_i915(dev);
  3755. uint32_t temp;
  3756. temp = I915_READ(SOUTH_CHICKEN1);
  3757. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3758. return;
  3759. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3760. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3761. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3762. if (enable)
  3763. temp |= FDI_BC_BIFURCATION_SELECT;
  3764. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3765. I915_WRITE(SOUTH_CHICKEN1, temp);
  3766. POSTING_READ(SOUTH_CHICKEN1);
  3767. }
  3768. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3769. {
  3770. struct drm_device *dev = intel_crtc->base.dev;
  3771. switch (intel_crtc->pipe) {
  3772. case PIPE_A:
  3773. break;
  3774. case PIPE_B:
  3775. if (intel_crtc->config->fdi_lanes > 2)
  3776. cpt_set_fdi_bc_bifurcation(dev, false);
  3777. else
  3778. cpt_set_fdi_bc_bifurcation(dev, true);
  3779. break;
  3780. case PIPE_C:
  3781. cpt_set_fdi_bc_bifurcation(dev, true);
  3782. break;
  3783. default:
  3784. BUG();
  3785. }
  3786. }
  3787. /* Return which DP Port should be selected for Transcoder DP control */
  3788. static enum port
  3789. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3790. {
  3791. struct drm_device *dev = crtc->base.dev;
  3792. struct intel_encoder *encoder;
  3793. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3794. if (encoder->type == INTEL_OUTPUT_DP ||
  3795. encoder->type == INTEL_OUTPUT_EDP)
  3796. return enc_to_dig_port(&encoder->base)->port;
  3797. }
  3798. return -1;
  3799. }
  3800. /*
  3801. * Enable PCH resources required for PCH ports:
  3802. * - PCH PLLs
  3803. * - FDI training & RX/TX
  3804. * - update transcoder timings
  3805. * - DP transcoding bits
  3806. * - transcoder
  3807. */
  3808. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3809. {
  3810. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3811. struct drm_device *dev = crtc->base.dev;
  3812. struct drm_i915_private *dev_priv = to_i915(dev);
  3813. int pipe = crtc->pipe;
  3814. u32 temp;
  3815. assert_pch_transcoder_disabled(dev_priv, pipe);
  3816. if (IS_IVYBRIDGE(dev_priv))
  3817. ivybridge_update_fdi_bc_bifurcation(crtc);
  3818. /* Write the TU size bits before fdi link training, so that error
  3819. * detection works. */
  3820. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3821. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3822. /* For PCH output, training FDI link */
  3823. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3824. /* We need to program the right clock selection before writing the pixel
  3825. * mutliplier into the DPLL. */
  3826. if (HAS_PCH_CPT(dev_priv)) {
  3827. u32 sel;
  3828. temp = I915_READ(PCH_DPLL_SEL);
  3829. temp |= TRANS_DPLL_ENABLE(pipe);
  3830. sel = TRANS_DPLLB_SEL(pipe);
  3831. if (crtc_state->shared_dpll ==
  3832. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3833. temp |= sel;
  3834. else
  3835. temp &= ~sel;
  3836. I915_WRITE(PCH_DPLL_SEL, temp);
  3837. }
  3838. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3839. * transcoder, and we actually should do this to not upset any PCH
  3840. * transcoder that already use the clock when we share it.
  3841. *
  3842. * Note that enable_shared_dpll tries to do the right thing, but
  3843. * get_shared_dpll unconditionally resets the pll - we need that to have
  3844. * the right LVDS enable sequence. */
  3845. intel_enable_shared_dpll(crtc);
  3846. /* set transcoder timing, panel must allow it */
  3847. assert_panel_unlocked(dev_priv, pipe);
  3848. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3849. intel_fdi_normal_train(crtc);
  3850. /* For PCH DP, enable TRANS_DP_CTL */
  3851. if (HAS_PCH_CPT(dev_priv) &&
  3852. intel_crtc_has_dp_encoder(crtc_state)) {
  3853. const struct drm_display_mode *adjusted_mode =
  3854. &crtc_state->base.adjusted_mode;
  3855. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3856. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3857. temp = I915_READ(reg);
  3858. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3859. TRANS_DP_SYNC_MASK |
  3860. TRANS_DP_BPC_MASK);
  3861. temp |= TRANS_DP_OUTPUT_ENABLE;
  3862. temp |= bpc << 9; /* same format but at 11:9 */
  3863. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3864. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3865. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3866. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3867. switch (intel_trans_dp_port_sel(crtc)) {
  3868. case PORT_B:
  3869. temp |= TRANS_DP_PORT_SEL_B;
  3870. break;
  3871. case PORT_C:
  3872. temp |= TRANS_DP_PORT_SEL_C;
  3873. break;
  3874. case PORT_D:
  3875. temp |= TRANS_DP_PORT_SEL_D;
  3876. break;
  3877. default:
  3878. BUG();
  3879. }
  3880. I915_WRITE(reg, temp);
  3881. }
  3882. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3883. }
  3884. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3885. {
  3886. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3887. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3888. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3889. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3890. lpt_program_iclkip(crtc);
  3891. /* Set transcoder timing. */
  3892. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3893. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3894. }
  3895. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3896. {
  3897. struct drm_i915_private *dev_priv = to_i915(dev);
  3898. i915_reg_t dslreg = PIPEDSL(pipe);
  3899. u32 temp;
  3900. temp = I915_READ(dslreg);
  3901. udelay(500);
  3902. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3903. if (wait_for(I915_READ(dslreg) != temp, 5))
  3904. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3905. }
  3906. }
  3907. static int
  3908. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3909. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3910. int src_w, int src_h, int dst_w, int dst_h)
  3911. {
  3912. struct intel_crtc_scaler_state *scaler_state =
  3913. &crtc_state->scaler_state;
  3914. struct intel_crtc *intel_crtc =
  3915. to_intel_crtc(crtc_state->base.crtc);
  3916. int need_scaling;
  3917. need_scaling = drm_rotation_90_or_270(rotation) ?
  3918. (src_h != dst_w || src_w != dst_h):
  3919. (src_w != dst_w || src_h != dst_h);
  3920. /*
  3921. * if plane is being disabled or scaler is no more required or force detach
  3922. * - free scaler binded to this plane/crtc
  3923. * - in order to do this, update crtc->scaler_usage
  3924. *
  3925. * Here scaler state in crtc_state is set free so that
  3926. * scaler can be assigned to other user. Actual register
  3927. * update to free the scaler is done in plane/panel-fit programming.
  3928. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3929. */
  3930. if (force_detach || !need_scaling) {
  3931. if (*scaler_id >= 0) {
  3932. scaler_state->scaler_users &= ~(1 << scaler_user);
  3933. scaler_state->scalers[*scaler_id].in_use = 0;
  3934. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3935. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3936. intel_crtc->pipe, scaler_user, *scaler_id,
  3937. scaler_state->scaler_users);
  3938. *scaler_id = -1;
  3939. }
  3940. return 0;
  3941. }
  3942. /* range checks */
  3943. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3944. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3945. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3946. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3947. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3948. "size is out of scaler range\n",
  3949. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3950. return -EINVAL;
  3951. }
  3952. /* mark this plane as a scaler user in crtc_state */
  3953. scaler_state->scaler_users |= (1 << scaler_user);
  3954. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3955. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3956. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3957. scaler_state->scaler_users);
  3958. return 0;
  3959. }
  3960. /**
  3961. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3962. *
  3963. * @state: crtc's scaler state
  3964. *
  3965. * Return
  3966. * 0 - scaler_usage updated successfully
  3967. * error - requested scaling cannot be supported or other error condition
  3968. */
  3969. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3970. {
  3971. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3972. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3973. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3974. state->pipe_src_w, state->pipe_src_h,
  3975. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3976. }
  3977. /**
  3978. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3979. *
  3980. * @state: crtc's scaler state
  3981. * @plane_state: atomic plane state to update
  3982. *
  3983. * Return
  3984. * 0 - scaler_usage updated successfully
  3985. * error - requested scaling cannot be supported or other error condition
  3986. */
  3987. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3988. struct intel_plane_state *plane_state)
  3989. {
  3990. struct intel_plane *intel_plane =
  3991. to_intel_plane(plane_state->base.plane);
  3992. struct drm_framebuffer *fb = plane_state->base.fb;
  3993. int ret;
  3994. bool force_detach = !fb || !plane_state->base.visible;
  3995. ret = skl_update_scaler(crtc_state, force_detach,
  3996. drm_plane_index(&intel_plane->base),
  3997. &plane_state->scaler_id,
  3998. plane_state->base.rotation,
  3999. drm_rect_width(&plane_state->base.src) >> 16,
  4000. drm_rect_height(&plane_state->base.src) >> 16,
  4001. drm_rect_width(&plane_state->base.dst),
  4002. drm_rect_height(&plane_state->base.dst));
  4003. if (ret || plane_state->scaler_id < 0)
  4004. return ret;
  4005. /* check colorkey */
  4006. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4007. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4008. intel_plane->base.base.id,
  4009. intel_plane->base.name);
  4010. return -EINVAL;
  4011. }
  4012. /* Check src format */
  4013. switch (fb->format->format) {
  4014. case DRM_FORMAT_RGB565:
  4015. case DRM_FORMAT_XBGR8888:
  4016. case DRM_FORMAT_XRGB8888:
  4017. case DRM_FORMAT_ABGR8888:
  4018. case DRM_FORMAT_ARGB8888:
  4019. case DRM_FORMAT_XRGB2101010:
  4020. case DRM_FORMAT_XBGR2101010:
  4021. case DRM_FORMAT_YUYV:
  4022. case DRM_FORMAT_YVYU:
  4023. case DRM_FORMAT_UYVY:
  4024. case DRM_FORMAT_VYUY:
  4025. break;
  4026. default:
  4027. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4028. intel_plane->base.base.id, intel_plane->base.name,
  4029. fb->base.id, fb->format->format);
  4030. return -EINVAL;
  4031. }
  4032. return 0;
  4033. }
  4034. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4035. {
  4036. int i;
  4037. for (i = 0; i < crtc->num_scalers; i++)
  4038. skl_detach_scaler(crtc, i);
  4039. }
  4040. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4041. {
  4042. struct drm_device *dev = crtc->base.dev;
  4043. struct drm_i915_private *dev_priv = to_i915(dev);
  4044. int pipe = crtc->pipe;
  4045. struct intel_crtc_scaler_state *scaler_state =
  4046. &crtc->config->scaler_state;
  4047. if (crtc->config->pch_pfit.enabled) {
  4048. int id;
  4049. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4050. return;
  4051. id = scaler_state->scaler_id;
  4052. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4053. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4054. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4055. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4056. }
  4057. }
  4058. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4059. {
  4060. struct drm_device *dev = crtc->base.dev;
  4061. struct drm_i915_private *dev_priv = to_i915(dev);
  4062. int pipe = crtc->pipe;
  4063. if (crtc->config->pch_pfit.enabled) {
  4064. /* Force use of hard-coded filter coefficients
  4065. * as some pre-programmed values are broken,
  4066. * e.g. x201.
  4067. */
  4068. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4069. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4070. PF_PIPE_SEL_IVB(pipe));
  4071. else
  4072. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4073. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4074. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4075. }
  4076. }
  4077. void hsw_enable_ips(struct intel_crtc *crtc)
  4078. {
  4079. struct drm_device *dev = crtc->base.dev;
  4080. struct drm_i915_private *dev_priv = to_i915(dev);
  4081. if (!crtc->config->ips_enabled)
  4082. return;
  4083. /*
  4084. * We can only enable IPS after we enable a plane and wait for a vblank
  4085. * This function is called from post_plane_update, which is run after
  4086. * a vblank wait.
  4087. */
  4088. assert_plane_enabled(dev_priv, crtc->plane);
  4089. if (IS_BROADWELL(dev_priv)) {
  4090. mutex_lock(&dev_priv->rps.hw_lock);
  4091. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4092. mutex_unlock(&dev_priv->rps.hw_lock);
  4093. /* Quoting Art Runyan: "its not safe to expect any particular
  4094. * value in IPS_CTL bit 31 after enabling IPS through the
  4095. * mailbox." Moreover, the mailbox may return a bogus state,
  4096. * so we need to just enable it and continue on.
  4097. */
  4098. } else {
  4099. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4100. /* The bit only becomes 1 in the next vblank, so this wait here
  4101. * is essentially intel_wait_for_vblank. If we don't have this
  4102. * and don't wait for vblanks until the end of crtc_enable, then
  4103. * the HW state readout code will complain that the expected
  4104. * IPS_CTL value is not the one we read. */
  4105. if (intel_wait_for_register(dev_priv,
  4106. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4107. 50))
  4108. DRM_ERROR("Timed out waiting for IPS enable\n");
  4109. }
  4110. }
  4111. void hsw_disable_ips(struct intel_crtc *crtc)
  4112. {
  4113. struct drm_device *dev = crtc->base.dev;
  4114. struct drm_i915_private *dev_priv = to_i915(dev);
  4115. if (!crtc->config->ips_enabled)
  4116. return;
  4117. assert_plane_enabled(dev_priv, crtc->plane);
  4118. if (IS_BROADWELL(dev_priv)) {
  4119. mutex_lock(&dev_priv->rps.hw_lock);
  4120. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4121. mutex_unlock(&dev_priv->rps.hw_lock);
  4122. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4123. if (intel_wait_for_register(dev_priv,
  4124. IPS_CTL, IPS_ENABLE, 0,
  4125. 42))
  4126. DRM_ERROR("Timed out waiting for IPS disable\n");
  4127. } else {
  4128. I915_WRITE(IPS_CTL, 0);
  4129. POSTING_READ(IPS_CTL);
  4130. }
  4131. /* We need to wait for a vblank before we can disable the plane. */
  4132. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4133. }
  4134. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4135. {
  4136. if (intel_crtc->overlay) {
  4137. struct drm_device *dev = intel_crtc->base.dev;
  4138. struct drm_i915_private *dev_priv = to_i915(dev);
  4139. mutex_lock(&dev->struct_mutex);
  4140. dev_priv->mm.interruptible = false;
  4141. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4142. dev_priv->mm.interruptible = true;
  4143. mutex_unlock(&dev->struct_mutex);
  4144. }
  4145. /* Let userspace switch the overlay on again. In most cases userspace
  4146. * has to recompute where to put it anyway.
  4147. */
  4148. }
  4149. /**
  4150. * intel_post_enable_primary - Perform operations after enabling primary plane
  4151. * @crtc: the CRTC whose primary plane was just enabled
  4152. *
  4153. * Performs potentially sleeping operations that must be done after the primary
  4154. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4155. * called due to an explicit primary plane update, or due to an implicit
  4156. * re-enable that is caused when a sprite plane is updated to no longer
  4157. * completely hide the primary plane.
  4158. */
  4159. static void
  4160. intel_post_enable_primary(struct drm_crtc *crtc)
  4161. {
  4162. struct drm_device *dev = crtc->dev;
  4163. struct drm_i915_private *dev_priv = to_i915(dev);
  4164. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4165. int pipe = intel_crtc->pipe;
  4166. /*
  4167. * FIXME IPS should be fine as long as one plane is
  4168. * enabled, but in practice it seems to have problems
  4169. * when going from primary only to sprite only and vice
  4170. * versa.
  4171. */
  4172. hsw_enable_ips(intel_crtc);
  4173. /*
  4174. * Gen2 reports pipe underruns whenever all planes are disabled.
  4175. * So don't enable underrun reporting before at least some planes
  4176. * are enabled.
  4177. * FIXME: Need to fix the logic to work when we turn off all planes
  4178. * but leave the pipe running.
  4179. */
  4180. if (IS_GEN2(dev_priv))
  4181. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4182. /* Underruns don't always raise interrupts, so check manually. */
  4183. intel_check_cpu_fifo_underruns(dev_priv);
  4184. intel_check_pch_fifo_underruns(dev_priv);
  4185. }
  4186. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4187. static void
  4188. intel_pre_disable_primary(struct drm_crtc *crtc)
  4189. {
  4190. struct drm_device *dev = crtc->dev;
  4191. struct drm_i915_private *dev_priv = to_i915(dev);
  4192. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4193. int pipe = intel_crtc->pipe;
  4194. /*
  4195. * Gen2 reports pipe underruns whenever all planes are disabled.
  4196. * So diasble underrun reporting before all the planes get disabled.
  4197. * FIXME: Need to fix the logic to work when we turn off all planes
  4198. * but leave the pipe running.
  4199. */
  4200. if (IS_GEN2(dev_priv))
  4201. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4202. /*
  4203. * FIXME IPS should be fine as long as one plane is
  4204. * enabled, but in practice it seems to have problems
  4205. * when going from primary only to sprite only and vice
  4206. * versa.
  4207. */
  4208. hsw_disable_ips(intel_crtc);
  4209. }
  4210. /* FIXME get rid of this and use pre_plane_update */
  4211. static void
  4212. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4213. {
  4214. struct drm_device *dev = crtc->dev;
  4215. struct drm_i915_private *dev_priv = to_i915(dev);
  4216. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4217. int pipe = intel_crtc->pipe;
  4218. intel_pre_disable_primary(crtc);
  4219. /*
  4220. * Vblank time updates from the shadow to live plane control register
  4221. * are blocked if the memory self-refresh mode is active at that
  4222. * moment. So to make sure the plane gets truly disabled, disable
  4223. * first the self-refresh mode. The self-refresh enable bit in turn
  4224. * will be checked/applied by the HW only at the next frame start
  4225. * event which is after the vblank start event, so we need to have a
  4226. * wait-for-vblank between disabling the plane and the pipe.
  4227. */
  4228. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4229. intel_set_memory_cxsr(dev_priv, false))
  4230. intel_wait_for_vblank(dev_priv, pipe);
  4231. }
  4232. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4233. {
  4234. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4235. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4236. struct intel_crtc_state *pipe_config =
  4237. to_intel_crtc_state(crtc->base.state);
  4238. struct drm_plane *primary = crtc->base.primary;
  4239. struct drm_plane_state *old_pri_state =
  4240. drm_atomic_get_existing_plane_state(old_state, primary);
  4241. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4242. if (pipe_config->update_wm_post && pipe_config->base.active)
  4243. intel_update_watermarks(crtc);
  4244. if (old_pri_state) {
  4245. struct intel_plane_state *primary_state =
  4246. to_intel_plane_state(primary->state);
  4247. struct intel_plane_state *old_primary_state =
  4248. to_intel_plane_state(old_pri_state);
  4249. intel_fbc_post_update(crtc);
  4250. if (primary_state->base.visible &&
  4251. (needs_modeset(&pipe_config->base) ||
  4252. !old_primary_state->base.visible))
  4253. intel_post_enable_primary(&crtc->base);
  4254. }
  4255. }
  4256. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state,
  4257. struct intel_crtc_state *pipe_config)
  4258. {
  4259. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4260. struct drm_device *dev = crtc->base.dev;
  4261. struct drm_i915_private *dev_priv = to_i915(dev);
  4262. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4263. struct drm_plane *primary = crtc->base.primary;
  4264. struct drm_plane_state *old_pri_state =
  4265. drm_atomic_get_existing_plane_state(old_state, primary);
  4266. bool modeset = needs_modeset(&pipe_config->base);
  4267. struct intel_atomic_state *old_intel_state =
  4268. to_intel_atomic_state(old_state);
  4269. if (old_pri_state) {
  4270. struct intel_plane_state *primary_state =
  4271. to_intel_plane_state(primary->state);
  4272. struct intel_plane_state *old_primary_state =
  4273. to_intel_plane_state(old_pri_state);
  4274. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4275. if (old_primary_state->base.visible &&
  4276. (modeset || !primary_state->base.visible))
  4277. intel_pre_disable_primary(&crtc->base);
  4278. }
  4279. /*
  4280. * Vblank time updates from the shadow to live plane control register
  4281. * are blocked if the memory self-refresh mode is active at that
  4282. * moment. So to make sure the plane gets truly disabled, disable
  4283. * first the self-refresh mode. The self-refresh enable bit in turn
  4284. * will be checked/applied by the HW only at the next frame start
  4285. * event which is after the vblank start event, so we need to have a
  4286. * wait-for-vblank between disabling the plane and the pipe.
  4287. */
  4288. if (HAS_GMCH_DISPLAY(dev_priv) && old_crtc_state->base.active &&
  4289. pipe_config->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
  4290. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4291. /*
  4292. * IVB workaround: must disable low power watermarks for at least
  4293. * one frame before enabling scaling. LP watermarks can be re-enabled
  4294. * when scaling is disabled.
  4295. *
  4296. * WaCxSRDisabledForSpriteScaling:ivb
  4297. */
  4298. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4299. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4300. /*
  4301. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4302. * watermark programming here.
  4303. */
  4304. if (needs_modeset(&pipe_config->base))
  4305. return;
  4306. /*
  4307. * For platforms that support atomic watermarks, program the
  4308. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4309. * will be the intermediate values that are safe for both pre- and
  4310. * post- vblank; when vblank happens, the 'active' values will be set
  4311. * to the final 'target' values and we'll do this again to get the
  4312. * optimal watermarks. For gen9+ platforms, the values we program here
  4313. * will be the final target values which will get automatically latched
  4314. * at vblank time; no further programming will be necessary.
  4315. *
  4316. * If a platform hasn't been transitioned to atomic watermarks yet,
  4317. * we'll continue to update watermarks the old way, if flags tell
  4318. * us to.
  4319. */
  4320. if (dev_priv->display.initial_watermarks != NULL)
  4321. dev_priv->display.initial_watermarks(old_intel_state,
  4322. pipe_config);
  4323. else if (pipe_config->update_wm_pre)
  4324. intel_update_watermarks(crtc);
  4325. }
  4326. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4327. {
  4328. struct drm_device *dev = crtc->dev;
  4329. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4330. struct drm_plane *p;
  4331. int pipe = intel_crtc->pipe;
  4332. intel_crtc_dpms_overlay_disable(intel_crtc);
  4333. drm_for_each_plane_mask(p, dev, plane_mask)
  4334. to_intel_plane(p)->disable_plane(p, crtc);
  4335. /*
  4336. * FIXME: Once we grow proper nuclear flip support out of this we need
  4337. * to compute the mask of flip planes precisely. For the time being
  4338. * consider this a flip to a NULL plane.
  4339. */
  4340. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4341. }
  4342. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4343. struct intel_crtc_state *crtc_state,
  4344. struct drm_atomic_state *old_state)
  4345. {
  4346. struct drm_connector_state *conn_state;
  4347. struct drm_connector *conn;
  4348. int i;
  4349. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4350. struct intel_encoder *encoder =
  4351. to_intel_encoder(conn_state->best_encoder);
  4352. if (conn_state->crtc != crtc)
  4353. continue;
  4354. if (encoder->pre_pll_enable)
  4355. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4356. }
  4357. }
  4358. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4359. struct intel_crtc_state *crtc_state,
  4360. struct drm_atomic_state *old_state)
  4361. {
  4362. struct drm_connector_state *conn_state;
  4363. struct drm_connector *conn;
  4364. int i;
  4365. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4366. struct intel_encoder *encoder =
  4367. to_intel_encoder(conn_state->best_encoder);
  4368. if (conn_state->crtc != crtc)
  4369. continue;
  4370. if (encoder->pre_enable)
  4371. encoder->pre_enable(encoder, crtc_state, conn_state);
  4372. }
  4373. }
  4374. static void intel_encoders_enable(struct drm_crtc *crtc,
  4375. struct intel_crtc_state *crtc_state,
  4376. struct drm_atomic_state *old_state)
  4377. {
  4378. struct drm_connector_state *conn_state;
  4379. struct drm_connector *conn;
  4380. int i;
  4381. for_each_new_connector_in_state(old_state, conn, conn_state, i) {
  4382. struct intel_encoder *encoder =
  4383. to_intel_encoder(conn_state->best_encoder);
  4384. if (conn_state->crtc != crtc)
  4385. continue;
  4386. encoder->enable(encoder, crtc_state, conn_state);
  4387. intel_opregion_notify_encoder(encoder, true);
  4388. }
  4389. }
  4390. static void intel_encoders_disable(struct drm_crtc *crtc,
  4391. struct intel_crtc_state *old_crtc_state,
  4392. struct drm_atomic_state *old_state)
  4393. {
  4394. struct drm_connector_state *old_conn_state;
  4395. struct drm_connector *conn;
  4396. int i;
  4397. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4398. struct intel_encoder *encoder =
  4399. to_intel_encoder(old_conn_state->best_encoder);
  4400. if (old_conn_state->crtc != crtc)
  4401. continue;
  4402. intel_opregion_notify_encoder(encoder, false);
  4403. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4404. }
  4405. }
  4406. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4407. struct intel_crtc_state *old_crtc_state,
  4408. struct drm_atomic_state *old_state)
  4409. {
  4410. struct drm_connector_state *old_conn_state;
  4411. struct drm_connector *conn;
  4412. int i;
  4413. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4414. struct intel_encoder *encoder =
  4415. to_intel_encoder(old_conn_state->best_encoder);
  4416. if (old_conn_state->crtc != crtc)
  4417. continue;
  4418. if (encoder->post_disable)
  4419. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4420. }
  4421. }
  4422. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4423. struct intel_crtc_state *old_crtc_state,
  4424. struct drm_atomic_state *old_state)
  4425. {
  4426. struct drm_connector_state *old_conn_state;
  4427. struct drm_connector *conn;
  4428. int i;
  4429. for_each_old_connector_in_state(old_state, conn, old_conn_state, i) {
  4430. struct intel_encoder *encoder =
  4431. to_intel_encoder(old_conn_state->best_encoder);
  4432. if (old_conn_state->crtc != crtc)
  4433. continue;
  4434. if (encoder->post_pll_disable)
  4435. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4436. }
  4437. }
  4438. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4439. struct drm_atomic_state *old_state)
  4440. {
  4441. struct drm_crtc *crtc = pipe_config->base.crtc;
  4442. struct drm_device *dev = crtc->dev;
  4443. struct drm_i915_private *dev_priv = to_i915(dev);
  4444. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4445. int pipe = intel_crtc->pipe;
  4446. struct intel_atomic_state *old_intel_state =
  4447. to_intel_atomic_state(old_state);
  4448. if (WARN_ON(intel_crtc->active))
  4449. return;
  4450. /*
  4451. * Sometimes spurious CPU pipe underruns happen during FDI
  4452. * training, at least with VGA+HDMI cloning. Suppress them.
  4453. *
  4454. * On ILK we get an occasional spurious CPU pipe underruns
  4455. * between eDP port A enable and vdd enable. Also PCH port
  4456. * enable seems to result in the occasional CPU pipe underrun.
  4457. *
  4458. * Spurious PCH underruns also occur during PCH enabling.
  4459. */
  4460. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4461. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4462. if (intel_crtc->config->has_pch_encoder)
  4463. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4464. if (intel_crtc->config->has_pch_encoder)
  4465. intel_prepare_shared_dpll(intel_crtc);
  4466. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4467. intel_dp_set_m_n(intel_crtc, M1_N1);
  4468. intel_set_pipe_timings(intel_crtc);
  4469. intel_set_pipe_src_size(intel_crtc);
  4470. if (intel_crtc->config->has_pch_encoder) {
  4471. intel_cpu_transcoder_set_m_n(intel_crtc,
  4472. &intel_crtc->config->fdi_m_n, NULL);
  4473. }
  4474. ironlake_set_pipeconf(crtc);
  4475. intel_crtc->active = true;
  4476. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4477. if (intel_crtc->config->has_pch_encoder) {
  4478. /* Note: FDI PLL enabling _must_ be done before we enable the
  4479. * cpu pipes, hence this is separate from all the other fdi/pch
  4480. * enabling. */
  4481. ironlake_fdi_pll_enable(intel_crtc);
  4482. } else {
  4483. assert_fdi_tx_disabled(dev_priv, pipe);
  4484. assert_fdi_rx_disabled(dev_priv, pipe);
  4485. }
  4486. ironlake_pfit_enable(intel_crtc);
  4487. /*
  4488. * On ILK+ LUT must be loaded before the pipe is running but with
  4489. * clocks enabled
  4490. */
  4491. intel_color_load_luts(&pipe_config->base);
  4492. if (dev_priv->display.initial_watermarks != NULL)
  4493. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4494. intel_enable_pipe(intel_crtc);
  4495. if (intel_crtc->config->has_pch_encoder)
  4496. ironlake_pch_enable(pipe_config);
  4497. assert_vblank_disabled(crtc);
  4498. drm_crtc_vblank_on(crtc);
  4499. intel_encoders_enable(crtc, pipe_config, old_state);
  4500. if (HAS_PCH_CPT(dev_priv))
  4501. cpt_verify_modeset(dev, intel_crtc->pipe);
  4502. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4503. if (intel_crtc->config->has_pch_encoder)
  4504. intel_wait_for_vblank(dev_priv, pipe);
  4505. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4506. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4507. }
  4508. /* IPS only exists on ULT machines and is tied to pipe A. */
  4509. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4510. {
  4511. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4512. }
  4513. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4514. struct drm_atomic_state *old_state)
  4515. {
  4516. struct drm_crtc *crtc = pipe_config->base.crtc;
  4517. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4518. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4519. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4520. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4521. struct intel_atomic_state *old_intel_state =
  4522. to_intel_atomic_state(old_state);
  4523. if (WARN_ON(intel_crtc->active))
  4524. return;
  4525. if (intel_crtc->config->has_pch_encoder)
  4526. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4527. false);
  4528. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4529. if (intel_crtc->config->shared_dpll)
  4530. intel_enable_shared_dpll(intel_crtc);
  4531. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4532. intel_dp_set_m_n(intel_crtc, M1_N1);
  4533. if (!transcoder_is_dsi(cpu_transcoder))
  4534. intel_set_pipe_timings(intel_crtc);
  4535. intel_set_pipe_src_size(intel_crtc);
  4536. if (cpu_transcoder != TRANSCODER_EDP &&
  4537. !transcoder_is_dsi(cpu_transcoder)) {
  4538. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4539. intel_crtc->config->pixel_multiplier - 1);
  4540. }
  4541. if (intel_crtc->config->has_pch_encoder) {
  4542. intel_cpu_transcoder_set_m_n(intel_crtc,
  4543. &intel_crtc->config->fdi_m_n, NULL);
  4544. }
  4545. if (!transcoder_is_dsi(cpu_transcoder))
  4546. haswell_set_pipeconf(crtc);
  4547. haswell_set_pipemisc(crtc);
  4548. intel_color_set_csc(&pipe_config->base);
  4549. intel_crtc->active = true;
  4550. if (intel_crtc->config->has_pch_encoder)
  4551. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4552. else
  4553. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4554. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4555. if (intel_crtc->config->has_pch_encoder)
  4556. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4557. if (!transcoder_is_dsi(cpu_transcoder))
  4558. intel_ddi_enable_pipe_clock(pipe_config);
  4559. if (INTEL_GEN(dev_priv) >= 9)
  4560. skylake_pfit_enable(intel_crtc);
  4561. else
  4562. ironlake_pfit_enable(intel_crtc);
  4563. /*
  4564. * On ILK+ LUT must be loaded before the pipe is running but with
  4565. * clocks enabled
  4566. */
  4567. intel_color_load_luts(&pipe_config->base);
  4568. intel_ddi_set_pipe_settings(pipe_config);
  4569. if (!transcoder_is_dsi(cpu_transcoder))
  4570. intel_ddi_enable_transcoder_func(pipe_config);
  4571. if (dev_priv->display.initial_watermarks != NULL)
  4572. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4573. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4574. if (!transcoder_is_dsi(cpu_transcoder))
  4575. intel_enable_pipe(intel_crtc);
  4576. if (intel_crtc->config->has_pch_encoder)
  4577. lpt_pch_enable(pipe_config);
  4578. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4579. intel_ddi_set_vc_payload_alloc(pipe_config, true);
  4580. assert_vblank_disabled(crtc);
  4581. drm_crtc_vblank_on(crtc);
  4582. intel_encoders_enable(crtc, pipe_config, old_state);
  4583. if (intel_crtc->config->has_pch_encoder) {
  4584. intel_wait_for_vblank(dev_priv, pipe);
  4585. intel_wait_for_vblank(dev_priv, pipe);
  4586. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4587. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4588. true);
  4589. }
  4590. /* If we change the relative order between pipe/planes enabling, we need
  4591. * to change the workaround. */
  4592. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4593. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4594. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4595. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4596. }
  4597. }
  4598. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4599. {
  4600. struct drm_device *dev = crtc->base.dev;
  4601. struct drm_i915_private *dev_priv = to_i915(dev);
  4602. int pipe = crtc->pipe;
  4603. /* To avoid upsetting the power well on haswell only disable the pfit if
  4604. * it's in use. The hw state code will make sure we get this right. */
  4605. if (force || crtc->config->pch_pfit.enabled) {
  4606. I915_WRITE(PF_CTL(pipe), 0);
  4607. I915_WRITE(PF_WIN_POS(pipe), 0);
  4608. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4609. }
  4610. }
  4611. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4612. struct drm_atomic_state *old_state)
  4613. {
  4614. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4615. struct drm_device *dev = crtc->dev;
  4616. struct drm_i915_private *dev_priv = to_i915(dev);
  4617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4618. int pipe = intel_crtc->pipe;
  4619. /*
  4620. * Sometimes spurious CPU pipe underruns happen when the
  4621. * pipe is already disabled, but FDI RX/TX is still enabled.
  4622. * Happens at least with VGA+HDMI cloning. Suppress them.
  4623. */
  4624. if (intel_crtc->config->has_pch_encoder) {
  4625. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4626. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4627. }
  4628. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4629. drm_crtc_vblank_off(crtc);
  4630. assert_vblank_disabled(crtc);
  4631. intel_disable_pipe(intel_crtc);
  4632. ironlake_pfit_disable(intel_crtc, false);
  4633. if (intel_crtc->config->has_pch_encoder)
  4634. ironlake_fdi_disable(crtc);
  4635. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4636. if (intel_crtc->config->has_pch_encoder) {
  4637. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4638. if (HAS_PCH_CPT(dev_priv)) {
  4639. i915_reg_t reg;
  4640. u32 temp;
  4641. /* disable TRANS_DP_CTL */
  4642. reg = TRANS_DP_CTL(pipe);
  4643. temp = I915_READ(reg);
  4644. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4645. TRANS_DP_PORT_SEL_MASK);
  4646. temp |= TRANS_DP_PORT_SEL_NONE;
  4647. I915_WRITE(reg, temp);
  4648. /* disable DPLL_SEL */
  4649. temp = I915_READ(PCH_DPLL_SEL);
  4650. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4651. I915_WRITE(PCH_DPLL_SEL, temp);
  4652. }
  4653. ironlake_fdi_pll_disable(intel_crtc);
  4654. }
  4655. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4656. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4657. }
  4658. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4659. struct drm_atomic_state *old_state)
  4660. {
  4661. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4662. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4663. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4664. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4665. if (intel_crtc->config->has_pch_encoder)
  4666. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4667. false);
  4668. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4669. drm_crtc_vblank_off(crtc);
  4670. assert_vblank_disabled(crtc);
  4671. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4672. if (!transcoder_is_dsi(cpu_transcoder))
  4673. intel_disable_pipe(intel_crtc);
  4674. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4675. intel_ddi_set_vc_payload_alloc(intel_crtc->config, false);
  4676. if (!transcoder_is_dsi(cpu_transcoder))
  4677. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4678. if (INTEL_GEN(dev_priv) >= 9)
  4679. skylake_scaler_disable(intel_crtc);
  4680. else
  4681. ironlake_pfit_disable(intel_crtc, false);
  4682. if (!transcoder_is_dsi(cpu_transcoder))
  4683. intel_ddi_disable_pipe_clock(intel_crtc->config);
  4684. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4685. if (old_crtc_state->has_pch_encoder)
  4686. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4687. true);
  4688. }
  4689. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4690. {
  4691. struct drm_device *dev = crtc->base.dev;
  4692. struct drm_i915_private *dev_priv = to_i915(dev);
  4693. struct intel_crtc_state *pipe_config = crtc->config;
  4694. if (!pipe_config->gmch_pfit.control)
  4695. return;
  4696. /*
  4697. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4698. * according to register description and PRM.
  4699. */
  4700. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4701. assert_pipe_disabled(dev_priv, crtc->pipe);
  4702. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4703. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4704. /* Border color in case we don't scale up to the full screen. Black by
  4705. * default, change to something else for debugging. */
  4706. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4707. }
  4708. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4709. {
  4710. switch (port) {
  4711. case PORT_A:
  4712. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4713. case PORT_B:
  4714. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4715. case PORT_C:
  4716. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4717. case PORT_D:
  4718. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4719. case PORT_E:
  4720. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4721. default:
  4722. MISSING_CASE(port);
  4723. return POWER_DOMAIN_PORT_OTHER;
  4724. }
  4725. }
  4726. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4727. struct intel_crtc_state *crtc_state)
  4728. {
  4729. struct drm_device *dev = crtc->dev;
  4730. struct drm_i915_private *dev_priv = to_i915(dev);
  4731. struct drm_encoder *encoder;
  4732. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4733. enum pipe pipe = intel_crtc->pipe;
  4734. u64 mask;
  4735. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4736. if (!crtc_state->base.active)
  4737. return 0;
  4738. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4739. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4740. if (crtc_state->pch_pfit.enabled ||
  4741. crtc_state->pch_pfit.force_thru)
  4742. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4743. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4744. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4745. mask |= BIT_ULL(intel_encoder->power_domain);
  4746. }
  4747. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4748. mask |= BIT(POWER_DOMAIN_AUDIO);
  4749. if (crtc_state->shared_dpll)
  4750. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4751. return mask;
  4752. }
  4753. static u64
  4754. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4755. struct intel_crtc_state *crtc_state)
  4756. {
  4757. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4759. enum intel_display_power_domain domain;
  4760. u64 domains, new_domains, old_domains;
  4761. old_domains = intel_crtc->enabled_power_domains;
  4762. intel_crtc->enabled_power_domains = new_domains =
  4763. get_crtc_power_domains(crtc, crtc_state);
  4764. domains = new_domains & ~old_domains;
  4765. for_each_power_domain(domain, domains)
  4766. intel_display_power_get(dev_priv, domain);
  4767. return old_domains & ~new_domains;
  4768. }
  4769. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4770. u64 domains)
  4771. {
  4772. enum intel_display_power_domain domain;
  4773. for_each_power_domain(domain, domains)
  4774. intel_display_power_put(dev_priv, domain);
  4775. }
  4776. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4777. struct drm_atomic_state *old_state)
  4778. {
  4779. struct intel_atomic_state *old_intel_state =
  4780. to_intel_atomic_state(old_state);
  4781. struct drm_crtc *crtc = pipe_config->base.crtc;
  4782. struct drm_device *dev = crtc->dev;
  4783. struct drm_i915_private *dev_priv = to_i915(dev);
  4784. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4785. int pipe = intel_crtc->pipe;
  4786. if (WARN_ON(intel_crtc->active))
  4787. return;
  4788. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4789. intel_dp_set_m_n(intel_crtc, M1_N1);
  4790. intel_set_pipe_timings(intel_crtc);
  4791. intel_set_pipe_src_size(intel_crtc);
  4792. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4793. struct drm_i915_private *dev_priv = to_i915(dev);
  4794. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4795. I915_WRITE(CHV_CANVAS(pipe), 0);
  4796. }
  4797. i9xx_set_pipeconf(intel_crtc);
  4798. intel_crtc->active = true;
  4799. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4800. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4801. if (IS_CHERRYVIEW(dev_priv)) {
  4802. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4803. chv_enable_pll(intel_crtc, intel_crtc->config);
  4804. } else {
  4805. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4806. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4807. }
  4808. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4809. i9xx_pfit_enable(intel_crtc);
  4810. intel_color_load_luts(&pipe_config->base);
  4811. dev_priv->display.initial_watermarks(old_intel_state,
  4812. pipe_config);
  4813. intel_enable_pipe(intel_crtc);
  4814. assert_vblank_disabled(crtc);
  4815. drm_crtc_vblank_on(crtc);
  4816. intel_encoders_enable(crtc, pipe_config, old_state);
  4817. }
  4818. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4819. {
  4820. struct drm_device *dev = crtc->base.dev;
  4821. struct drm_i915_private *dev_priv = to_i915(dev);
  4822. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4823. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4824. }
  4825. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4826. struct drm_atomic_state *old_state)
  4827. {
  4828. struct drm_crtc *crtc = pipe_config->base.crtc;
  4829. struct drm_device *dev = crtc->dev;
  4830. struct drm_i915_private *dev_priv = to_i915(dev);
  4831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4832. enum pipe pipe = intel_crtc->pipe;
  4833. if (WARN_ON(intel_crtc->active))
  4834. return;
  4835. i9xx_set_pll_dividers(intel_crtc);
  4836. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4837. intel_dp_set_m_n(intel_crtc, M1_N1);
  4838. intel_set_pipe_timings(intel_crtc);
  4839. intel_set_pipe_src_size(intel_crtc);
  4840. i9xx_set_pipeconf(intel_crtc);
  4841. intel_crtc->active = true;
  4842. if (!IS_GEN2(dev_priv))
  4843. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4844. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4845. i9xx_enable_pll(intel_crtc);
  4846. i9xx_pfit_enable(intel_crtc);
  4847. intel_color_load_luts(&pipe_config->base);
  4848. intel_update_watermarks(intel_crtc);
  4849. intel_enable_pipe(intel_crtc);
  4850. assert_vblank_disabled(crtc);
  4851. drm_crtc_vblank_on(crtc);
  4852. intel_encoders_enable(crtc, pipe_config, old_state);
  4853. }
  4854. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4855. {
  4856. struct drm_device *dev = crtc->base.dev;
  4857. struct drm_i915_private *dev_priv = to_i915(dev);
  4858. if (!crtc->config->gmch_pfit.control)
  4859. return;
  4860. assert_pipe_disabled(dev_priv, crtc->pipe);
  4861. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4862. I915_READ(PFIT_CONTROL));
  4863. I915_WRITE(PFIT_CONTROL, 0);
  4864. }
  4865. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4866. struct drm_atomic_state *old_state)
  4867. {
  4868. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4869. struct drm_device *dev = crtc->dev;
  4870. struct drm_i915_private *dev_priv = to_i915(dev);
  4871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4872. int pipe = intel_crtc->pipe;
  4873. /*
  4874. * On gen2 planes are double buffered but the pipe isn't, so we must
  4875. * wait for planes to fully turn off before disabling the pipe.
  4876. */
  4877. if (IS_GEN2(dev_priv))
  4878. intel_wait_for_vblank(dev_priv, pipe);
  4879. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4880. drm_crtc_vblank_off(crtc);
  4881. assert_vblank_disabled(crtc);
  4882. intel_disable_pipe(intel_crtc);
  4883. i9xx_pfit_disable(intel_crtc);
  4884. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4885. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4886. if (IS_CHERRYVIEW(dev_priv))
  4887. chv_disable_pll(dev_priv, pipe);
  4888. else if (IS_VALLEYVIEW(dev_priv))
  4889. vlv_disable_pll(dev_priv, pipe);
  4890. else
  4891. i9xx_disable_pll(intel_crtc);
  4892. }
  4893. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4894. if (!IS_GEN2(dev_priv))
  4895. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4896. if (!dev_priv->display.initial_watermarks)
  4897. intel_update_watermarks(intel_crtc);
  4898. }
  4899. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  4900. {
  4901. struct intel_encoder *encoder;
  4902. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4903. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4904. enum intel_display_power_domain domain;
  4905. u64 domains;
  4906. struct drm_atomic_state *state;
  4907. struct intel_crtc_state *crtc_state;
  4908. int ret;
  4909. if (!intel_crtc->active)
  4910. return;
  4911. if (crtc->primary->state->visible) {
  4912. WARN_ON(intel_crtc->flip_work);
  4913. intel_pre_disable_primary_noatomic(crtc);
  4914. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4915. crtc->primary->state->visible = false;
  4916. }
  4917. state = drm_atomic_state_alloc(crtc->dev);
  4918. if (!state) {
  4919. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4920. crtc->base.id, crtc->name);
  4921. return;
  4922. }
  4923. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  4924. /* Everything's already locked, -EDEADLK can't happen. */
  4925. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4926. ret = drm_atomic_add_affected_connectors(state, crtc);
  4927. WARN_ON(IS_ERR(crtc_state) || ret);
  4928. dev_priv->display.crtc_disable(crtc_state, state);
  4929. drm_atomic_state_put(state);
  4930. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4931. crtc->base.id, crtc->name);
  4932. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4933. crtc->state->active = false;
  4934. intel_crtc->active = false;
  4935. crtc->enabled = false;
  4936. crtc->state->connector_mask = 0;
  4937. crtc->state->encoder_mask = 0;
  4938. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4939. encoder->base.crtc = NULL;
  4940. intel_fbc_disable(intel_crtc);
  4941. intel_update_watermarks(intel_crtc);
  4942. intel_disable_shared_dpll(intel_crtc);
  4943. domains = intel_crtc->enabled_power_domains;
  4944. for_each_power_domain(domain, domains)
  4945. intel_display_power_put(dev_priv, domain);
  4946. intel_crtc->enabled_power_domains = 0;
  4947. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  4948. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  4949. }
  4950. /*
  4951. * turn all crtc's off, but do not adjust state
  4952. * This has to be paired with a call to intel_modeset_setup_hw_state.
  4953. */
  4954. int intel_display_suspend(struct drm_device *dev)
  4955. {
  4956. struct drm_i915_private *dev_priv = to_i915(dev);
  4957. struct drm_atomic_state *state;
  4958. int ret;
  4959. state = drm_atomic_helper_suspend(dev);
  4960. ret = PTR_ERR_OR_ZERO(state);
  4961. if (ret)
  4962. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  4963. else
  4964. dev_priv->modeset_restore_state = state;
  4965. return ret;
  4966. }
  4967. void intel_encoder_destroy(struct drm_encoder *encoder)
  4968. {
  4969. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4970. drm_encoder_cleanup(encoder);
  4971. kfree(intel_encoder);
  4972. }
  4973. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4974. * internal consistency). */
  4975. static void intel_connector_verify_state(struct intel_connector *connector)
  4976. {
  4977. struct drm_crtc *crtc = connector->base.state->crtc;
  4978. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4979. connector->base.base.id,
  4980. connector->base.name);
  4981. if (connector->get_hw_state(connector)) {
  4982. struct intel_encoder *encoder = connector->encoder;
  4983. struct drm_connector_state *conn_state = connector->base.state;
  4984. I915_STATE_WARN(!crtc,
  4985. "connector enabled without attached crtc\n");
  4986. if (!crtc)
  4987. return;
  4988. I915_STATE_WARN(!crtc->state->active,
  4989. "connector is active, but attached crtc isn't\n");
  4990. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  4991. return;
  4992. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  4993. "atomic encoder doesn't match attached encoder\n");
  4994. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  4995. "attached encoder crtc differs from connector crtc\n");
  4996. } else {
  4997. I915_STATE_WARN(crtc && crtc->state->active,
  4998. "attached crtc is active, but connector isn't\n");
  4999. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5000. "best encoder set without crtc!\n");
  5001. }
  5002. }
  5003. int intel_connector_init(struct intel_connector *connector)
  5004. {
  5005. drm_atomic_helper_connector_reset(&connector->base);
  5006. if (!connector->base.state)
  5007. return -ENOMEM;
  5008. return 0;
  5009. }
  5010. struct intel_connector *intel_connector_alloc(void)
  5011. {
  5012. struct intel_connector *connector;
  5013. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5014. if (!connector)
  5015. return NULL;
  5016. if (intel_connector_init(connector) < 0) {
  5017. kfree(connector);
  5018. return NULL;
  5019. }
  5020. return connector;
  5021. }
  5022. /* Simple connector->get_hw_state implementation for encoders that support only
  5023. * one connector and no cloning and hence the encoder state determines the state
  5024. * of the connector. */
  5025. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5026. {
  5027. enum pipe pipe = 0;
  5028. struct intel_encoder *encoder = connector->encoder;
  5029. return encoder->get_hw_state(encoder, &pipe);
  5030. }
  5031. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5032. {
  5033. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5034. return crtc_state->fdi_lanes;
  5035. return 0;
  5036. }
  5037. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5038. struct intel_crtc_state *pipe_config)
  5039. {
  5040. struct drm_i915_private *dev_priv = to_i915(dev);
  5041. struct drm_atomic_state *state = pipe_config->base.state;
  5042. struct intel_crtc *other_crtc;
  5043. struct intel_crtc_state *other_crtc_state;
  5044. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5045. pipe_name(pipe), pipe_config->fdi_lanes);
  5046. if (pipe_config->fdi_lanes > 4) {
  5047. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5048. pipe_name(pipe), pipe_config->fdi_lanes);
  5049. return -EINVAL;
  5050. }
  5051. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5052. if (pipe_config->fdi_lanes > 2) {
  5053. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5054. pipe_config->fdi_lanes);
  5055. return -EINVAL;
  5056. } else {
  5057. return 0;
  5058. }
  5059. }
  5060. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5061. return 0;
  5062. /* Ivybridge 3 pipe is really complicated */
  5063. switch (pipe) {
  5064. case PIPE_A:
  5065. return 0;
  5066. case PIPE_B:
  5067. if (pipe_config->fdi_lanes <= 2)
  5068. return 0;
  5069. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5070. other_crtc_state =
  5071. intel_atomic_get_crtc_state(state, other_crtc);
  5072. if (IS_ERR(other_crtc_state))
  5073. return PTR_ERR(other_crtc_state);
  5074. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5075. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5076. pipe_name(pipe), pipe_config->fdi_lanes);
  5077. return -EINVAL;
  5078. }
  5079. return 0;
  5080. case PIPE_C:
  5081. if (pipe_config->fdi_lanes > 2) {
  5082. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5083. pipe_name(pipe), pipe_config->fdi_lanes);
  5084. return -EINVAL;
  5085. }
  5086. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5087. other_crtc_state =
  5088. intel_atomic_get_crtc_state(state, other_crtc);
  5089. if (IS_ERR(other_crtc_state))
  5090. return PTR_ERR(other_crtc_state);
  5091. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5092. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5093. return -EINVAL;
  5094. }
  5095. return 0;
  5096. default:
  5097. BUG();
  5098. }
  5099. }
  5100. #define RETRY 1
  5101. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5102. struct intel_crtc_state *pipe_config)
  5103. {
  5104. struct drm_device *dev = intel_crtc->base.dev;
  5105. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5106. int lane, link_bw, fdi_dotclock, ret;
  5107. bool needs_recompute = false;
  5108. retry:
  5109. /* FDI is a binary signal running at ~2.7GHz, encoding
  5110. * each output octet as 10 bits. The actual frequency
  5111. * is stored as a divider into a 100MHz clock, and the
  5112. * mode pixel clock is stored in units of 1KHz.
  5113. * Hence the bw of each lane in terms of the mode signal
  5114. * is:
  5115. */
  5116. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5117. fdi_dotclock = adjusted_mode->crtc_clock;
  5118. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5119. pipe_config->pipe_bpp);
  5120. pipe_config->fdi_lanes = lane;
  5121. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5122. link_bw, &pipe_config->fdi_m_n, false);
  5123. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5124. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5125. pipe_config->pipe_bpp -= 2*3;
  5126. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5127. pipe_config->pipe_bpp);
  5128. needs_recompute = true;
  5129. pipe_config->bw_constrained = true;
  5130. goto retry;
  5131. }
  5132. if (needs_recompute)
  5133. return RETRY;
  5134. return ret;
  5135. }
  5136. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5137. struct intel_crtc_state *pipe_config)
  5138. {
  5139. if (pipe_config->pipe_bpp > 24)
  5140. return false;
  5141. /* HSW can handle pixel rate up to cdclk? */
  5142. if (IS_HASWELL(dev_priv))
  5143. return true;
  5144. /*
  5145. * We compare against max which means we must take
  5146. * the increased cdclk requirement into account when
  5147. * calculating the new cdclk.
  5148. *
  5149. * Should measure whether using a lower cdclk w/o IPS
  5150. */
  5151. return pipe_config->pixel_rate <=
  5152. dev_priv->max_cdclk_freq * 95 / 100;
  5153. }
  5154. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5155. struct intel_crtc_state *pipe_config)
  5156. {
  5157. struct drm_device *dev = crtc->base.dev;
  5158. struct drm_i915_private *dev_priv = to_i915(dev);
  5159. pipe_config->ips_enabled = i915.enable_ips &&
  5160. hsw_crtc_supports_ips(crtc) &&
  5161. pipe_config_supports_ips(dev_priv, pipe_config);
  5162. }
  5163. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5164. {
  5165. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5166. /* GDG double wide on either pipe, otherwise pipe A only */
  5167. return INTEL_INFO(dev_priv)->gen < 4 &&
  5168. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5169. }
  5170. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5171. {
  5172. uint32_t pixel_rate;
  5173. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5174. /*
  5175. * We only use IF-ID interlacing. If we ever use
  5176. * PF-ID we'll need to adjust the pixel_rate here.
  5177. */
  5178. if (pipe_config->pch_pfit.enabled) {
  5179. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5180. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5181. pipe_w = pipe_config->pipe_src_w;
  5182. pipe_h = pipe_config->pipe_src_h;
  5183. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5184. pfit_h = pfit_size & 0xFFFF;
  5185. if (pipe_w < pfit_w)
  5186. pipe_w = pfit_w;
  5187. if (pipe_h < pfit_h)
  5188. pipe_h = pfit_h;
  5189. if (WARN_ON(!pfit_w || !pfit_h))
  5190. return pixel_rate;
  5191. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5192. pfit_w * pfit_h);
  5193. }
  5194. return pixel_rate;
  5195. }
  5196. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5197. {
  5198. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5199. if (HAS_GMCH_DISPLAY(dev_priv))
  5200. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5201. crtc_state->pixel_rate =
  5202. crtc_state->base.adjusted_mode.crtc_clock;
  5203. else
  5204. crtc_state->pixel_rate =
  5205. ilk_pipe_pixel_rate(crtc_state);
  5206. }
  5207. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5208. struct intel_crtc_state *pipe_config)
  5209. {
  5210. struct drm_device *dev = crtc->base.dev;
  5211. struct drm_i915_private *dev_priv = to_i915(dev);
  5212. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5213. int clock_limit = dev_priv->max_dotclk_freq;
  5214. if (INTEL_GEN(dev_priv) < 4) {
  5215. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5216. /*
  5217. * Enable double wide mode when the dot clock
  5218. * is > 90% of the (display) core speed.
  5219. */
  5220. if (intel_crtc_supports_double_wide(crtc) &&
  5221. adjusted_mode->crtc_clock > clock_limit) {
  5222. clock_limit = dev_priv->max_dotclk_freq;
  5223. pipe_config->double_wide = true;
  5224. }
  5225. }
  5226. if (adjusted_mode->crtc_clock > clock_limit) {
  5227. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5228. adjusted_mode->crtc_clock, clock_limit,
  5229. yesno(pipe_config->double_wide));
  5230. return -EINVAL;
  5231. }
  5232. /*
  5233. * Pipe horizontal size must be even in:
  5234. * - DVO ganged mode
  5235. * - LVDS dual channel mode
  5236. * - Double wide pipe
  5237. */
  5238. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5239. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5240. pipe_config->pipe_src_w &= ~1;
  5241. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5242. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5243. */
  5244. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5245. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5246. return -EINVAL;
  5247. intel_crtc_compute_pixel_rate(pipe_config);
  5248. if (HAS_IPS(dev_priv))
  5249. hsw_compute_ips_config(crtc, pipe_config);
  5250. if (pipe_config->has_pch_encoder)
  5251. return ironlake_fdi_compute_config(crtc, pipe_config);
  5252. return 0;
  5253. }
  5254. static void
  5255. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5256. {
  5257. while (*num > DATA_LINK_M_N_MASK ||
  5258. *den > DATA_LINK_M_N_MASK) {
  5259. *num >>= 1;
  5260. *den >>= 1;
  5261. }
  5262. }
  5263. static void compute_m_n(unsigned int m, unsigned int n,
  5264. uint32_t *ret_m, uint32_t *ret_n,
  5265. bool reduce_m_n)
  5266. {
  5267. /*
  5268. * Reduce M/N as much as possible without loss in precision. Several DP
  5269. * dongles in particular seem to be fussy about too large *link* M/N
  5270. * values. The passed in values are more likely to have the least
  5271. * significant bits zero than M after rounding below, so do this first.
  5272. */
  5273. if (reduce_m_n) {
  5274. while ((m & 1) == 0 && (n & 1) == 0) {
  5275. m >>= 1;
  5276. n >>= 1;
  5277. }
  5278. }
  5279. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5280. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5281. intel_reduce_m_n_ratio(ret_m, ret_n);
  5282. }
  5283. void
  5284. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5285. int pixel_clock, int link_clock,
  5286. struct intel_link_m_n *m_n,
  5287. bool reduce_m_n)
  5288. {
  5289. m_n->tu = 64;
  5290. compute_m_n(bits_per_pixel * pixel_clock,
  5291. link_clock * nlanes * 8,
  5292. &m_n->gmch_m, &m_n->gmch_n,
  5293. reduce_m_n);
  5294. compute_m_n(pixel_clock, link_clock,
  5295. &m_n->link_m, &m_n->link_n,
  5296. reduce_m_n);
  5297. }
  5298. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5299. {
  5300. if (i915.panel_use_ssc >= 0)
  5301. return i915.panel_use_ssc != 0;
  5302. return dev_priv->vbt.lvds_use_ssc
  5303. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5304. }
  5305. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5306. {
  5307. return (1 << dpll->n) << 16 | dpll->m2;
  5308. }
  5309. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5310. {
  5311. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5312. }
  5313. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5314. struct intel_crtc_state *crtc_state,
  5315. struct dpll *reduced_clock)
  5316. {
  5317. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5318. u32 fp, fp2 = 0;
  5319. if (IS_PINEVIEW(dev_priv)) {
  5320. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5321. if (reduced_clock)
  5322. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5323. } else {
  5324. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5325. if (reduced_clock)
  5326. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5327. }
  5328. crtc_state->dpll_hw_state.fp0 = fp;
  5329. crtc->lowfreq_avail = false;
  5330. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5331. reduced_clock) {
  5332. crtc_state->dpll_hw_state.fp1 = fp2;
  5333. crtc->lowfreq_avail = true;
  5334. } else {
  5335. crtc_state->dpll_hw_state.fp1 = fp;
  5336. }
  5337. }
  5338. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5339. pipe)
  5340. {
  5341. u32 reg_val;
  5342. /*
  5343. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5344. * and set it to a reasonable value instead.
  5345. */
  5346. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5347. reg_val &= 0xffffff00;
  5348. reg_val |= 0x00000030;
  5349. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5350. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5351. reg_val &= 0x8cffffff;
  5352. reg_val = 0x8c000000;
  5353. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5354. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5355. reg_val &= 0xffffff00;
  5356. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5357. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5358. reg_val &= 0x00ffffff;
  5359. reg_val |= 0xb0000000;
  5360. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5361. }
  5362. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5363. struct intel_link_m_n *m_n)
  5364. {
  5365. struct drm_device *dev = crtc->base.dev;
  5366. struct drm_i915_private *dev_priv = to_i915(dev);
  5367. int pipe = crtc->pipe;
  5368. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5369. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5370. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5371. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5372. }
  5373. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5374. struct intel_link_m_n *m_n,
  5375. struct intel_link_m_n *m2_n2)
  5376. {
  5377. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5378. int pipe = crtc->pipe;
  5379. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5380. if (INTEL_GEN(dev_priv) >= 5) {
  5381. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5382. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5383. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5384. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5385. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5386. * for gen < 8) and if DRRS is supported (to make sure the
  5387. * registers are not unnecessarily accessed).
  5388. */
  5389. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5390. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5391. I915_WRITE(PIPE_DATA_M2(transcoder),
  5392. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5393. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5394. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5395. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5396. }
  5397. } else {
  5398. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5399. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5400. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5401. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5402. }
  5403. }
  5404. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5405. {
  5406. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5407. if (m_n == M1_N1) {
  5408. dp_m_n = &crtc->config->dp_m_n;
  5409. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5410. } else if (m_n == M2_N2) {
  5411. /*
  5412. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5413. * needs to be programmed into M1_N1.
  5414. */
  5415. dp_m_n = &crtc->config->dp_m2_n2;
  5416. } else {
  5417. DRM_ERROR("Unsupported divider value\n");
  5418. return;
  5419. }
  5420. if (crtc->config->has_pch_encoder)
  5421. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5422. else
  5423. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5424. }
  5425. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5426. struct intel_crtc_state *pipe_config)
  5427. {
  5428. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5429. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5430. if (crtc->pipe != PIPE_A)
  5431. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5432. /* DPLL not used with DSI, but still need the rest set up */
  5433. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5434. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5435. DPLL_EXT_BUFFER_ENABLE_VLV;
  5436. pipe_config->dpll_hw_state.dpll_md =
  5437. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5438. }
  5439. static void chv_compute_dpll(struct intel_crtc *crtc,
  5440. struct intel_crtc_state *pipe_config)
  5441. {
  5442. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5443. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5444. if (crtc->pipe != PIPE_A)
  5445. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5446. /* DPLL not used with DSI, but still need the rest set up */
  5447. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5448. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5449. pipe_config->dpll_hw_state.dpll_md =
  5450. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5451. }
  5452. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5453. const struct intel_crtc_state *pipe_config)
  5454. {
  5455. struct drm_device *dev = crtc->base.dev;
  5456. struct drm_i915_private *dev_priv = to_i915(dev);
  5457. enum pipe pipe = crtc->pipe;
  5458. u32 mdiv;
  5459. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5460. u32 coreclk, reg_val;
  5461. /* Enable Refclk */
  5462. I915_WRITE(DPLL(pipe),
  5463. pipe_config->dpll_hw_state.dpll &
  5464. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5465. /* No need to actually set up the DPLL with DSI */
  5466. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5467. return;
  5468. mutex_lock(&dev_priv->sb_lock);
  5469. bestn = pipe_config->dpll.n;
  5470. bestm1 = pipe_config->dpll.m1;
  5471. bestm2 = pipe_config->dpll.m2;
  5472. bestp1 = pipe_config->dpll.p1;
  5473. bestp2 = pipe_config->dpll.p2;
  5474. /* See eDP HDMI DPIO driver vbios notes doc */
  5475. /* PLL B needs special handling */
  5476. if (pipe == PIPE_B)
  5477. vlv_pllb_recal_opamp(dev_priv, pipe);
  5478. /* Set up Tx target for periodic Rcomp update */
  5479. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5480. /* Disable target IRef on PLL */
  5481. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5482. reg_val &= 0x00ffffff;
  5483. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5484. /* Disable fast lock */
  5485. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5486. /* Set idtafcrecal before PLL is enabled */
  5487. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5488. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5489. mdiv |= ((bestn << DPIO_N_SHIFT));
  5490. mdiv |= (1 << DPIO_K_SHIFT);
  5491. /*
  5492. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5493. * but we don't support that).
  5494. * Note: don't use the DAC post divider as it seems unstable.
  5495. */
  5496. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5497. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5498. mdiv |= DPIO_ENABLE_CALIBRATION;
  5499. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5500. /* Set HBR and RBR LPF coefficients */
  5501. if (pipe_config->port_clock == 162000 ||
  5502. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5503. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5504. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5505. 0x009f0003);
  5506. else
  5507. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5508. 0x00d0000f);
  5509. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5510. /* Use SSC source */
  5511. if (pipe == PIPE_A)
  5512. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5513. 0x0df40000);
  5514. else
  5515. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5516. 0x0df70000);
  5517. } else { /* HDMI or VGA */
  5518. /* Use bend source */
  5519. if (pipe == PIPE_A)
  5520. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5521. 0x0df70000);
  5522. else
  5523. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5524. 0x0df40000);
  5525. }
  5526. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5527. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5528. if (intel_crtc_has_dp_encoder(crtc->config))
  5529. coreclk |= 0x01000000;
  5530. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5531. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5532. mutex_unlock(&dev_priv->sb_lock);
  5533. }
  5534. static void chv_prepare_pll(struct intel_crtc *crtc,
  5535. const struct intel_crtc_state *pipe_config)
  5536. {
  5537. struct drm_device *dev = crtc->base.dev;
  5538. struct drm_i915_private *dev_priv = to_i915(dev);
  5539. enum pipe pipe = crtc->pipe;
  5540. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5541. u32 loopfilter, tribuf_calcntr;
  5542. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5543. u32 dpio_val;
  5544. int vco;
  5545. /* Enable Refclk and SSC */
  5546. I915_WRITE(DPLL(pipe),
  5547. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5548. /* No need to actually set up the DPLL with DSI */
  5549. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5550. return;
  5551. bestn = pipe_config->dpll.n;
  5552. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5553. bestm1 = pipe_config->dpll.m1;
  5554. bestm2 = pipe_config->dpll.m2 >> 22;
  5555. bestp1 = pipe_config->dpll.p1;
  5556. bestp2 = pipe_config->dpll.p2;
  5557. vco = pipe_config->dpll.vco;
  5558. dpio_val = 0;
  5559. loopfilter = 0;
  5560. mutex_lock(&dev_priv->sb_lock);
  5561. /* p1 and p2 divider */
  5562. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5563. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5564. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5565. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5566. 1 << DPIO_CHV_K_DIV_SHIFT);
  5567. /* Feedback post-divider - m2 */
  5568. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5569. /* Feedback refclk divider - n and m1 */
  5570. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5571. DPIO_CHV_M1_DIV_BY_2 |
  5572. 1 << DPIO_CHV_N_DIV_SHIFT);
  5573. /* M2 fraction division */
  5574. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5575. /* M2 fraction division enable */
  5576. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5577. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5578. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5579. if (bestm2_frac)
  5580. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5581. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5582. /* Program digital lock detect threshold */
  5583. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5584. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5585. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5586. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5587. if (!bestm2_frac)
  5588. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5589. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5590. /* Loop filter */
  5591. if (vco == 5400000) {
  5592. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5593. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5594. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5595. tribuf_calcntr = 0x9;
  5596. } else if (vco <= 6200000) {
  5597. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5598. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5599. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5600. tribuf_calcntr = 0x9;
  5601. } else if (vco <= 6480000) {
  5602. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5603. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5604. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5605. tribuf_calcntr = 0x8;
  5606. } else {
  5607. /* Not supported. Apply the same limits as in the max case */
  5608. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5609. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5610. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5611. tribuf_calcntr = 0;
  5612. }
  5613. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5614. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5615. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5616. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5617. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5618. /* AFC Recal */
  5619. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5620. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5621. DPIO_AFC_RECAL);
  5622. mutex_unlock(&dev_priv->sb_lock);
  5623. }
  5624. /**
  5625. * vlv_force_pll_on - forcibly enable just the PLL
  5626. * @dev_priv: i915 private structure
  5627. * @pipe: pipe PLL to enable
  5628. * @dpll: PLL configuration
  5629. *
  5630. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5631. * in cases where we need the PLL enabled even when @pipe is not going to
  5632. * be enabled.
  5633. */
  5634. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5635. const struct dpll *dpll)
  5636. {
  5637. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5638. struct intel_crtc_state *pipe_config;
  5639. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5640. if (!pipe_config)
  5641. return -ENOMEM;
  5642. pipe_config->base.crtc = &crtc->base;
  5643. pipe_config->pixel_multiplier = 1;
  5644. pipe_config->dpll = *dpll;
  5645. if (IS_CHERRYVIEW(dev_priv)) {
  5646. chv_compute_dpll(crtc, pipe_config);
  5647. chv_prepare_pll(crtc, pipe_config);
  5648. chv_enable_pll(crtc, pipe_config);
  5649. } else {
  5650. vlv_compute_dpll(crtc, pipe_config);
  5651. vlv_prepare_pll(crtc, pipe_config);
  5652. vlv_enable_pll(crtc, pipe_config);
  5653. }
  5654. kfree(pipe_config);
  5655. return 0;
  5656. }
  5657. /**
  5658. * vlv_force_pll_off - forcibly disable just the PLL
  5659. * @dev_priv: i915 private structure
  5660. * @pipe: pipe PLL to disable
  5661. *
  5662. * Disable the PLL for @pipe. To be used in cases where we need
  5663. * the PLL enabled even when @pipe is not going to be enabled.
  5664. */
  5665. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5666. {
  5667. if (IS_CHERRYVIEW(dev_priv))
  5668. chv_disable_pll(dev_priv, pipe);
  5669. else
  5670. vlv_disable_pll(dev_priv, pipe);
  5671. }
  5672. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5673. struct intel_crtc_state *crtc_state,
  5674. struct dpll *reduced_clock)
  5675. {
  5676. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5677. u32 dpll;
  5678. struct dpll *clock = &crtc_state->dpll;
  5679. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5680. dpll = DPLL_VGA_MODE_DIS;
  5681. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5682. dpll |= DPLLB_MODE_LVDS;
  5683. else
  5684. dpll |= DPLLB_MODE_DAC_SERIAL;
  5685. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5686. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5687. dpll |= (crtc_state->pixel_multiplier - 1)
  5688. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5689. }
  5690. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5691. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5692. dpll |= DPLL_SDVO_HIGH_SPEED;
  5693. if (intel_crtc_has_dp_encoder(crtc_state))
  5694. dpll |= DPLL_SDVO_HIGH_SPEED;
  5695. /* compute bitmask from p1 value */
  5696. if (IS_PINEVIEW(dev_priv))
  5697. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5698. else {
  5699. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5700. if (IS_G4X(dev_priv) && reduced_clock)
  5701. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5702. }
  5703. switch (clock->p2) {
  5704. case 5:
  5705. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5706. break;
  5707. case 7:
  5708. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5709. break;
  5710. case 10:
  5711. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5712. break;
  5713. case 14:
  5714. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5715. break;
  5716. }
  5717. if (INTEL_GEN(dev_priv) >= 4)
  5718. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5719. if (crtc_state->sdvo_tv_clock)
  5720. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5721. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5722. intel_panel_use_ssc(dev_priv))
  5723. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5724. else
  5725. dpll |= PLL_REF_INPUT_DREFCLK;
  5726. dpll |= DPLL_VCO_ENABLE;
  5727. crtc_state->dpll_hw_state.dpll = dpll;
  5728. if (INTEL_GEN(dev_priv) >= 4) {
  5729. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5730. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5731. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5732. }
  5733. }
  5734. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5735. struct intel_crtc_state *crtc_state,
  5736. struct dpll *reduced_clock)
  5737. {
  5738. struct drm_device *dev = crtc->base.dev;
  5739. struct drm_i915_private *dev_priv = to_i915(dev);
  5740. u32 dpll;
  5741. struct dpll *clock = &crtc_state->dpll;
  5742. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5743. dpll = DPLL_VGA_MODE_DIS;
  5744. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5745. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5746. } else {
  5747. if (clock->p1 == 2)
  5748. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5749. else
  5750. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5751. if (clock->p2 == 4)
  5752. dpll |= PLL_P2_DIVIDE_BY_4;
  5753. }
  5754. if (!IS_I830(dev_priv) &&
  5755. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5756. dpll |= DPLL_DVO_2X_MODE;
  5757. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5758. intel_panel_use_ssc(dev_priv))
  5759. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5760. else
  5761. dpll |= PLL_REF_INPUT_DREFCLK;
  5762. dpll |= DPLL_VCO_ENABLE;
  5763. crtc_state->dpll_hw_state.dpll = dpll;
  5764. }
  5765. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5766. {
  5767. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5768. enum pipe pipe = intel_crtc->pipe;
  5769. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5770. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5771. uint32_t crtc_vtotal, crtc_vblank_end;
  5772. int vsyncshift = 0;
  5773. /* We need to be careful not to changed the adjusted mode, for otherwise
  5774. * the hw state checker will get angry at the mismatch. */
  5775. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5776. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5777. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5778. /* the chip adds 2 halflines automatically */
  5779. crtc_vtotal -= 1;
  5780. crtc_vblank_end -= 1;
  5781. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5782. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5783. else
  5784. vsyncshift = adjusted_mode->crtc_hsync_start -
  5785. adjusted_mode->crtc_htotal / 2;
  5786. if (vsyncshift < 0)
  5787. vsyncshift += adjusted_mode->crtc_htotal;
  5788. }
  5789. if (INTEL_GEN(dev_priv) > 3)
  5790. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5791. I915_WRITE(HTOTAL(cpu_transcoder),
  5792. (adjusted_mode->crtc_hdisplay - 1) |
  5793. ((adjusted_mode->crtc_htotal - 1) << 16));
  5794. I915_WRITE(HBLANK(cpu_transcoder),
  5795. (adjusted_mode->crtc_hblank_start - 1) |
  5796. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5797. I915_WRITE(HSYNC(cpu_transcoder),
  5798. (adjusted_mode->crtc_hsync_start - 1) |
  5799. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5800. I915_WRITE(VTOTAL(cpu_transcoder),
  5801. (adjusted_mode->crtc_vdisplay - 1) |
  5802. ((crtc_vtotal - 1) << 16));
  5803. I915_WRITE(VBLANK(cpu_transcoder),
  5804. (adjusted_mode->crtc_vblank_start - 1) |
  5805. ((crtc_vblank_end - 1) << 16));
  5806. I915_WRITE(VSYNC(cpu_transcoder),
  5807. (adjusted_mode->crtc_vsync_start - 1) |
  5808. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5809. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5810. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5811. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5812. * bits. */
  5813. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5814. (pipe == PIPE_B || pipe == PIPE_C))
  5815. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5816. }
  5817. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5818. {
  5819. struct drm_device *dev = intel_crtc->base.dev;
  5820. struct drm_i915_private *dev_priv = to_i915(dev);
  5821. enum pipe pipe = intel_crtc->pipe;
  5822. /* pipesrc controls the size that is scaled from, which should
  5823. * always be the user's requested size.
  5824. */
  5825. I915_WRITE(PIPESRC(pipe),
  5826. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5827. (intel_crtc->config->pipe_src_h - 1));
  5828. }
  5829. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5830. struct intel_crtc_state *pipe_config)
  5831. {
  5832. struct drm_device *dev = crtc->base.dev;
  5833. struct drm_i915_private *dev_priv = to_i915(dev);
  5834. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5835. uint32_t tmp;
  5836. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5837. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5838. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5839. tmp = I915_READ(HBLANK(cpu_transcoder));
  5840. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5841. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5842. tmp = I915_READ(HSYNC(cpu_transcoder));
  5843. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5844. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5845. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5846. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5847. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5848. tmp = I915_READ(VBLANK(cpu_transcoder));
  5849. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5850. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5851. tmp = I915_READ(VSYNC(cpu_transcoder));
  5852. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5853. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5854. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5855. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5856. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5857. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5858. }
  5859. }
  5860. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5861. struct intel_crtc_state *pipe_config)
  5862. {
  5863. struct drm_device *dev = crtc->base.dev;
  5864. struct drm_i915_private *dev_priv = to_i915(dev);
  5865. u32 tmp;
  5866. tmp = I915_READ(PIPESRC(crtc->pipe));
  5867. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5868. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5869. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5870. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5871. }
  5872. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5873. struct intel_crtc_state *pipe_config)
  5874. {
  5875. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5876. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5877. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5878. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5879. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5880. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5881. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5882. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5883. mode->flags = pipe_config->base.adjusted_mode.flags;
  5884. mode->type = DRM_MODE_TYPE_DRIVER;
  5885. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5886. mode->hsync = drm_mode_hsync(mode);
  5887. mode->vrefresh = drm_mode_vrefresh(mode);
  5888. drm_mode_set_name(mode);
  5889. }
  5890. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5891. {
  5892. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5893. uint32_t pipeconf;
  5894. pipeconf = 0;
  5895. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5896. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5897. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5898. if (intel_crtc->config->double_wide)
  5899. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5900. /* only g4x and later have fancy bpc/dither controls */
  5901. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5902. IS_CHERRYVIEW(dev_priv)) {
  5903. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5904. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5905. pipeconf |= PIPECONF_DITHER_EN |
  5906. PIPECONF_DITHER_TYPE_SP;
  5907. switch (intel_crtc->config->pipe_bpp) {
  5908. case 18:
  5909. pipeconf |= PIPECONF_6BPC;
  5910. break;
  5911. case 24:
  5912. pipeconf |= PIPECONF_8BPC;
  5913. break;
  5914. case 30:
  5915. pipeconf |= PIPECONF_10BPC;
  5916. break;
  5917. default:
  5918. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5919. BUG();
  5920. }
  5921. }
  5922. if (HAS_PIPE_CXSR(dev_priv)) {
  5923. if (intel_crtc->lowfreq_avail) {
  5924. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5925. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5926. } else {
  5927. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5928. }
  5929. }
  5930. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5931. if (INTEL_GEN(dev_priv) < 4 ||
  5932. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5933. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5934. else
  5935. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5936. } else
  5937. pipeconf |= PIPECONF_PROGRESSIVE;
  5938. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5939. intel_crtc->config->limited_color_range)
  5940. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5941. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5942. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5943. }
  5944. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5945. struct intel_crtc_state *crtc_state)
  5946. {
  5947. struct drm_device *dev = crtc->base.dev;
  5948. struct drm_i915_private *dev_priv = to_i915(dev);
  5949. const struct intel_limit *limit;
  5950. int refclk = 48000;
  5951. memset(&crtc_state->dpll_hw_state, 0,
  5952. sizeof(crtc_state->dpll_hw_state));
  5953. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5954. if (intel_panel_use_ssc(dev_priv)) {
  5955. refclk = dev_priv->vbt.lvds_ssc_freq;
  5956. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5957. }
  5958. limit = &intel_limits_i8xx_lvds;
  5959. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5960. limit = &intel_limits_i8xx_dvo;
  5961. } else {
  5962. limit = &intel_limits_i8xx_dac;
  5963. }
  5964. if (!crtc_state->clock_set &&
  5965. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5966. refclk, NULL, &crtc_state->dpll)) {
  5967. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5968. return -EINVAL;
  5969. }
  5970. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5971. return 0;
  5972. }
  5973. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5974. struct intel_crtc_state *crtc_state)
  5975. {
  5976. struct drm_device *dev = crtc->base.dev;
  5977. struct drm_i915_private *dev_priv = to_i915(dev);
  5978. const struct intel_limit *limit;
  5979. int refclk = 96000;
  5980. memset(&crtc_state->dpll_hw_state, 0,
  5981. sizeof(crtc_state->dpll_hw_state));
  5982. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5983. if (intel_panel_use_ssc(dev_priv)) {
  5984. refclk = dev_priv->vbt.lvds_ssc_freq;
  5985. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5986. }
  5987. if (intel_is_dual_link_lvds(dev))
  5988. limit = &intel_limits_g4x_dual_channel_lvds;
  5989. else
  5990. limit = &intel_limits_g4x_single_channel_lvds;
  5991. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  5992. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  5993. limit = &intel_limits_g4x_hdmi;
  5994. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  5995. limit = &intel_limits_g4x_sdvo;
  5996. } else {
  5997. /* The option is for other outputs */
  5998. limit = &intel_limits_i9xx_sdvo;
  5999. }
  6000. if (!crtc_state->clock_set &&
  6001. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6002. refclk, NULL, &crtc_state->dpll)) {
  6003. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6004. return -EINVAL;
  6005. }
  6006. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6007. return 0;
  6008. }
  6009. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  6010. struct intel_crtc_state *crtc_state)
  6011. {
  6012. struct drm_device *dev = crtc->base.dev;
  6013. struct drm_i915_private *dev_priv = to_i915(dev);
  6014. const struct intel_limit *limit;
  6015. int refclk = 96000;
  6016. memset(&crtc_state->dpll_hw_state, 0,
  6017. sizeof(crtc_state->dpll_hw_state));
  6018. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6019. if (intel_panel_use_ssc(dev_priv)) {
  6020. refclk = dev_priv->vbt.lvds_ssc_freq;
  6021. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6022. }
  6023. limit = &intel_limits_pineview_lvds;
  6024. } else {
  6025. limit = &intel_limits_pineview_sdvo;
  6026. }
  6027. if (!crtc_state->clock_set &&
  6028. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6029. refclk, NULL, &crtc_state->dpll)) {
  6030. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6031. return -EINVAL;
  6032. }
  6033. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6034. return 0;
  6035. }
  6036. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6037. struct intel_crtc_state *crtc_state)
  6038. {
  6039. struct drm_device *dev = crtc->base.dev;
  6040. struct drm_i915_private *dev_priv = to_i915(dev);
  6041. const struct intel_limit *limit;
  6042. int refclk = 96000;
  6043. memset(&crtc_state->dpll_hw_state, 0,
  6044. sizeof(crtc_state->dpll_hw_state));
  6045. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6046. if (intel_panel_use_ssc(dev_priv)) {
  6047. refclk = dev_priv->vbt.lvds_ssc_freq;
  6048. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6049. }
  6050. limit = &intel_limits_i9xx_lvds;
  6051. } else {
  6052. limit = &intel_limits_i9xx_sdvo;
  6053. }
  6054. if (!crtc_state->clock_set &&
  6055. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6056. refclk, NULL, &crtc_state->dpll)) {
  6057. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6058. return -EINVAL;
  6059. }
  6060. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6061. return 0;
  6062. }
  6063. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6064. struct intel_crtc_state *crtc_state)
  6065. {
  6066. int refclk = 100000;
  6067. const struct intel_limit *limit = &intel_limits_chv;
  6068. memset(&crtc_state->dpll_hw_state, 0,
  6069. sizeof(crtc_state->dpll_hw_state));
  6070. if (!crtc_state->clock_set &&
  6071. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6072. refclk, NULL, &crtc_state->dpll)) {
  6073. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6074. return -EINVAL;
  6075. }
  6076. chv_compute_dpll(crtc, crtc_state);
  6077. return 0;
  6078. }
  6079. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6080. struct intel_crtc_state *crtc_state)
  6081. {
  6082. int refclk = 100000;
  6083. const struct intel_limit *limit = &intel_limits_vlv;
  6084. memset(&crtc_state->dpll_hw_state, 0,
  6085. sizeof(crtc_state->dpll_hw_state));
  6086. if (!crtc_state->clock_set &&
  6087. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6088. refclk, NULL, &crtc_state->dpll)) {
  6089. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6090. return -EINVAL;
  6091. }
  6092. vlv_compute_dpll(crtc, crtc_state);
  6093. return 0;
  6094. }
  6095. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6096. struct intel_crtc_state *pipe_config)
  6097. {
  6098. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6099. uint32_t tmp;
  6100. if (INTEL_GEN(dev_priv) <= 3 &&
  6101. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6102. return;
  6103. tmp = I915_READ(PFIT_CONTROL);
  6104. if (!(tmp & PFIT_ENABLE))
  6105. return;
  6106. /* Check whether the pfit is attached to our pipe. */
  6107. if (INTEL_GEN(dev_priv) < 4) {
  6108. if (crtc->pipe != PIPE_B)
  6109. return;
  6110. } else {
  6111. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6112. return;
  6113. }
  6114. pipe_config->gmch_pfit.control = tmp;
  6115. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6116. }
  6117. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6118. struct intel_crtc_state *pipe_config)
  6119. {
  6120. struct drm_device *dev = crtc->base.dev;
  6121. struct drm_i915_private *dev_priv = to_i915(dev);
  6122. int pipe = pipe_config->cpu_transcoder;
  6123. struct dpll clock;
  6124. u32 mdiv;
  6125. int refclk = 100000;
  6126. /* In case of DSI, DPLL will not be used */
  6127. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6128. return;
  6129. mutex_lock(&dev_priv->sb_lock);
  6130. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6131. mutex_unlock(&dev_priv->sb_lock);
  6132. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6133. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6134. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6135. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6136. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6137. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6138. }
  6139. static void
  6140. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6141. struct intel_initial_plane_config *plane_config)
  6142. {
  6143. struct drm_device *dev = crtc->base.dev;
  6144. struct drm_i915_private *dev_priv = to_i915(dev);
  6145. u32 val, base, offset;
  6146. int pipe = crtc->pipe, plane = crtc->plane;
  6147. int fourcc, pixel_format;
  6148. unsigned int aligned_height;
  6149. struct drm_framebuffer *fb;
  6150. struct intel_framebuffer *intel_fb;
  6151. val = I915_READ(DSPCNTR(plane));
  6152. if (!(val & DISPLAY_PLANE_ENABLE))
  6153. return;
  6154. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6155. if (!intel_fb) {
  6156. DRM_DEBUG_KMS("failed to alloc fb\n");
  6157. return;
  6158. }
  6159. fb = &intel_fb->base;
  6160. fb->dev = dev;
  6161. if (INTEL_GEN(dev_priv) >= 4) {
  6162. if (val & DISPPLANE_TILED) {
  6163. plane_config->tiling = I915_TILING_X;
  6164. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6165. }
  6166. }
  6167. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6168. fourcc = i9xx_format_to_fourcc(pixel_format);
  6169. fb->format = drm_format_info(fourcc);
  6170. if (INTEL_GEN(dev_priv) >= 4) {
  6171. if (plane_config->tiling)
  6172. offset = I915_READ(DSPTILEOFF(plane));
  6173. else
  6174. offset = I915_READ(DSPLINOFF(plane));
  6175. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6176. } else {
  6177. base = I915_READ(DSPADDR(plane));
  6178. }
  6179. plane_config->base = base;
  6180. val = I915_READ(PIPESRC(pipe));
  6181. fb->width = ((val >> 16) & 0xfff) + 1;
  6182. fb->height = ((val >> 0) & 0xfff) + 1;
  6183. val = I915_READ(DSPSTRIDE(pipe));
  6184. fb->pitches[0] = val & 0xffffffc0;
  6185. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  6186. plane_config->size = fb->pitches[0] * aligned_height;
  6187. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6188. pipe_name(pipe), plane, fb->width, fb->height,
  6189. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6190. plane_config->size);
  6191. plane_config->fb = intel_fb;
  6192. }
  6193. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6194. struct intel_crtc_state *pipe_config)
  6195. {
  6196. struct drm_device *dev = crtc->base.dev;
  6197. struct drm_i915_private *dev_priv = to_i915(dev);
  6198. int pipe = pipe_config->cpu_transcoder;
  6199. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6200. struct dpll clock;
  6201. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6202. int refclk = 100000;
  6203. /* In case of DSI, DPLL will not be used */
  6204. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6205. return;
  6206. mutex_lock(&dev_priv->sb_lock);
  6207. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6208. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6209. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6210. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6211. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6212. mutex_unlock(&dev_priv->sb_lock);
  6213. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6214. clock.m2 = (pll_dw0 & 0xff) << 22;
  6215. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6216. clock.m2 |= pll_dw2 & 0x3fffff;
  6217. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6218. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6219. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6220. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6221. }
  6222. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6223. struct intel_crtc_state *pipe_config)
  6224. {
  6225. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6226. enum intel_display_power_domain power_domain;
  6227. uint32_t tmp;
  6228. bool ret;
  6229. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6230. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6231. return false;
  6232. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6233. pipe_config->shared_dpll = NULL;
  6234. ret = false;
  6235. tmp = I915_READ(PIPECONF(crtc->pipe));
  6236. if (!(tmp & PIPECONF_ENABLE))
  6237. goto out;
  6238. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6239. IS_CHERRYVIEW(dev_priv)) {
  6240. switch (tmp & PIPECONF_BPC_MASK) {
  6241. case PIPECONF_6BPC:
  6242. pipe_config->pipe_bpp = 18;
  6243. break;
  6244. case PIPECONF_8BPC:
  6245. pipe_config->pipe_bpp = 24;
  6246. break;
  6247. case PIPECONF_10BPC:
  6248. pipe_config->pipe_bpp = 30;
  6249. break;
  6250. default:
  6251. break;
  6252. }
  6253. }
  6254. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6255. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6256. pipe_config->limited_color_range = true;
  6257. if (INTEL_GEN(dev_priv) < 4)
  6258. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6259. intel_get_pipe_timings(crtc, pipe_config);
  6260. intel_get_pipe_src_size(crtc, pipe_config);
  6261. i9xx_get_pfit_config(crtc, pipe_config);
  6262. if (INTEL_GEN(dev_priv) >= 4) {
  6263. /* No way to read it out on pipes B and C */
  6264. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6265. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6266. else
  6267. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6268. pipe_config->pixel_multiplier =
  6269. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6270. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6271. pipe_config->dpll_hw_state.dpll_md = tmp;
  6272. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6273. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6274. tmp = I915_READ(DPLL(crtc->pipe));
  6275. pipe_config->pixel_multiplier =
  6276. ((tmp & SDVO_MULTIPLIER_MASK)
  6277. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6278. } else {
  6279. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6280. * port and will be fixed up in the encoder->get_config
  6281. * function. */
  6282. pipe_config->pixel_multiplier = 1;
  6283. }
  6284. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6285. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6286. /*
  6287. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6288. * on 830. Filter it out here so that we don't
  6289. * report errors due to that.
  6290. */
  6291. if (IS_I830(dev_priv))
  6292. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6293. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6294. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6295. } else {
  6296. /* Mask out read-only status bits. */
  6297. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6298. DPLL_PORTC_READY_MASK |
  6299. DPLL_PORTB_READY_MASK);
  6300. }
  6301. if (IS_CHERRYVIEW(dev_priv))
  6302. chv_crtc_clock_get(crtc, pipe_config);
  6303. else if (IS_VALLEYVIEW(dev_priv))
  6304. vlv_crtc_clock_get(crtc, pipe_config);
  6305. else
  6306. i9xx_crtc_clock_get(crtc, pipe_config);
  6307. /*
  6308. * Normally the dotclock is filled in by the encoder .get_config()
  6309. * but in case the pipe is enabled w/o any ports we need a sane
  6310. * default.
  6311. */
  6312. pipe_config->base.adjusted_mode.crtc_clock =
  6313. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6314. ret = true;
  6315. out:
  6316. intel_display_power_put(dev_priv, power_domain);
  6317. return ret;
  6318. }
  6319. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6320. {
  6321. struct intel_encoder *encoder;
  6322. int i;
  6323. u32 val, final;
  6324. bool has_lvds = false;
  6325. bool has_cpu_edp = false;
  6326. bool has_panel = false;
  6327. bool has_ck505 = false;
  6328. bool can_ssc = false;
  6329. bool using_ssc_source = false;
  6330. /* We need to take the global config into account */
  6331. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6332. switch (encoder->type) {
  6333. case INTEL_OUTPUT_LVDS:
  6334. has_panel = true;
  6335. has_lvds = true;
  6336. break;
  6337. case INTEL_OUTPUT_EDP:
  6338. has_panel = true;
  6339. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6340. has_cpu_edp = true;
  6341. break;
  6342. default:
  6343. break;
  6344. }
  6345. }
  6346. if (HAS_PCH_IBX(dev_priv)) {
  6347. has_ck505 = dev_priv->vbt.display_clock_mode;
  6348. can_ssc = has_ck505;
  6349. } else {
  6350. has_ck505 = false;
  6351. can_ssc = true;
  6352. }
  6353. /* Check if any DPLLs are using the SSC source */
  6354. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6355. u32 temp = I915_READ(PCH_DPLL(i));
  6356. if (!(temp & DPLL_VCO_ENABLE))
  6357. continue;
  6358. if ((temp & PLL_REF_INPUT_MASK) ==
  6359. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6360. using_ssc_source = true;
  6361. break;
  6362. }
  6363. }
  6364. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6365. has_panel, has_lvds, has_ck505, using_ssc_source);
  6366. /* Ironlake: try to setup display ref clock before DPLL
  6367. * enabling. This is only under driver's control after
  6368. * PCH B stepping, previous chipset stepping should be
  6369. * ignoring this setting.
  6370. */
  6371. val = I915_READ(PCH_DREF_CONTROL);
  6372. /* As we must carefully and slowly disable/enable each source in turn,
  6373. * compute the final state we want first and check if we need to
  6374. * make any changes at all.
  6375. */
  6376. final = val;
  6377. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6378. if (has_ck505)
  6379. final |= DREF_NONSPREAD_CK505_ENABLE;
  6380. else
  6381. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6382. final &= ~DREF_SSC_SOURCE_MASK;
  6383. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6384. final &= ~DREF_SSC1_ENABLE;
  6385. if (has_panel) {
  6386. final |= DREF_SSC_SOURCE_ENABLE;
  6387. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6388. final |= DREF_SSC1_ENABLE;
  6389. if (has_cpu_edp) {
  6390. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6391. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6392. else
  6393. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6394. } else
  6395. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6396. } else if (using_ssc_source) {
  6397. final |= DREF_SSC_SOURCE_ENABLE;
  6398. final |= DREF_SSC1_ENABLE;
  6399. }
  6400. if (final == val)
  6401. return;
  6402. /* Always enable nonspread source */
  6403. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6404. if (has_ck505)
  6405. val |= DREF_NONSPREAD_CK505_ENABLE;
  6406. else
  6407. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6408. if (has_panel) {
  6409. val &= ~DREF_SSC_SOURCE_MASK;
  6410. val |= DREF_SSC_SOURCE_ENABLE;
  6411. /* SSC must be turned on before enabling the CPU output */
  6412. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6413. DRM_DEBUG_KMS("Using SSC on panel\n");
  6414. val |= DREF_SSC1_ENABLE;
  6415. } else
  6416. val &= ~DREF_SSC1_ENABLE;
  6417. /* Get SSC going before enabling the outputs */
  6418. I915_WRITE(PCH_DREF_CONTROL, val);
  6419. POSTING_READ(PCH_DREF_CONTROL);
  6420. udelay(200);
  6421. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6422. /* Enable CPU source on CPU attached eDP */
  6423. if (has_cpu_edp) {
  6424. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6425. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6426. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6427. } else
  6428. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6429. } else
  6430. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6431. I915_WRITE(PCH_DREF_CONTROL, val);
  6432. POSTING_READ(PCH_DREF_CONTROL);
  6433. udelay(200);
  6434. } else {
  6435. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6436. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6437. /* Turn off CPU output */
  6438. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6439. I915_WRITE(PCH_DREF_CONTROL, val);
  6440. POSTING_READ(PCH_DREF_CONTROL);
  6441. udelay(200);
  6442. if (!using_ssc_source) {
  6443. DRM_DEBUG_KMS("Disabling SSC source\n");
  6444. /* Turn off the SSC source */
  6445. val &= ~DREF_SSC_SOURCE_MASK;
  6446. val |= DREF_SSC_SOURCE_DISABLE;
  6447. /* Turn off SSC1 */
  6448. val &= ~DREF_SSC1_ENABLE;
  6449. I915_WRITE(PCH_DREF_CONTROL, val);
  6450. POSTING_READ(PCH_DREF_CONTROL);
  6451. udelay(200);
  6452. }
  6453. }
  6454. BUG_ON(val != final);
  6455. }
  6456. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6457. {
  6458. uint32_t tmp;
  6459. tmp = I915_READ(SOUTH_CHICKEN2);
  6460. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6461. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6462. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6463. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6464. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6465. tmp = I915_READ(SOUTH_CHICKEN2);
  6466. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6467. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6468. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6469. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6470. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6471. }
  6472. /* WaMPhyProgramming:hsw */
  6473. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6474. {
  6475. uint32_t tmp;
  6476. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6477. tmp &= ~(0xFF << 24);
  6478. tmp |= (0x12 << 24);
  6479. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6480. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6481. tmp |= (1 << 11);
  6482. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6483. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6484. tmp |= (1 << 11);
  6485. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6486. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6487. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6488. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6489. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6490. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6491. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6492. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6493. tmp &= ~(7 << 13);
  6494. tmp |= (5 << 13);
  6495. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6496. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6497. tmp &= ~(7 << 13);
  6498. tmp |= (5 << 13);
  6499. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6500. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6501. tmp &= ~0xFF;
  6502. tmp |= 0x1C;
  6503. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6504. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6505. tmp &= ~0xFF;
  6506. tmp |= 0x1C;
  6507. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6508. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6509. tmp &= ~(0xFF << 16);
  6510. tmp |= (0x1C << 16);
  6511. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6512. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6513. tmp &= ~(0xFF << 16);
  6514. tmp |= (0x1C << 16);
  6515. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6516. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6517. tmp |= (1 << 27);
  6518. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6519. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6520. tmp |= (1 << 27);
  6521. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6522. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6523. tmp &= ~(0xF << 28);
  6524. tmp |= (4 << 28);
  6525. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6526. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6527. tmp &= ~(0xF << 28);
  6528. tmp |= (4 << 28);
  6529. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6530. }
  6531. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6532. * Programming" based on the parameters passed:
  6533. * - Sequence to enable CLKOUT_DP
  6534. * - Sequence to enable CLKOUT_DP without spread
  6535. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6536. */
  6537. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6538. bool with_spread, bool with_fdi)
  6539. {
  6540. uint32_t reg, tmp;
  6541. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6542. with_spread = true;
  6543. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6544. with_fdi, "LP PCH doesn't have FDI\n"))
  6545. with_fdi = false;
  6546. mutex_lock(&dev_priv->sb_lock);
  6547. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6548. tmp &= ~SBI_SSCCTL_DISABLE;
  6549. tmp |= SBI_SSCCTL_PATHALT;
  6550. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6551. udelay(24);
  6552. if (with_spread) {
  6553. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6554. tmp &= ~SBI_SSCCTL_PATHALT;
  6555. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6556. if (with_fdi) {
  6557. lpt_reset_fdi_mphy(dev_priv);
  6558. lpt_program_fdi_mphy(dev_priv);
  6559. }
  6560. }
  6561. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6562. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6563. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6564. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6565. mutex_unlock(&dev_priv->sb_lock);
  6566. }
  6567. /* Sequence to disable CLKOUT_DP */
  6568. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6569. {
  6570. uint32_t reg, tmp;
  6571. mutex_lock(&dev_priv->sb_lock);
  6572. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6573. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6574. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6575. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6576. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6577. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6578. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6579. tmp |= SBI_SSCCTL_PATHALT;
  6580. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6581. udelay(32);
  6582. }
  6583. tmp |= SBI_SSCCTL_DISABLE;
  6584. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6585. }
  6586. mutex_unlock(&dev_priv->sb_lock);
  6587. }
  6588. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6589. static const uint16_t sscdivintphase[] = {
  6590. [BEND_IDX( 50)] = 0x3B23,
  6591. [BEND_IDX( 45)] = 0x3B23,
  6592. [BEND_IDX( 40)] = 0x3C23,
  6593. [BEND_IDX( 35)] = 0x3C23,
  6594. [BEND_IDX( 30)] = 0x3D23,
  6595. [BEND_IDX( 25)] = 0x3D23,
  6596. [BEND_IDX( 20)] = 0x3E23,
  6597. [BEND_IDX( 15)] = 0x3E23,
  6598. [BEND_IDX( 10)] = 0x3F23,
  6599. [BEND_IDX( 5)] = 0x3F23,
  6600. [BEND_IDX( 0)] = 0x0025,
  6601. [BEND_IDX( -5)] = 0x0025,
  6602. [BEND_IDX(-10)] = 0x0125,
  6603. [BEND_IDX(-15)] = 0x0125,
  6604. [BEND_IDX(-20)] = 0x0225,
  6605. [BEND_IDX(-25)] = 0x0225,
  6606. [BEND_IDX(-30)] = 0x0325,
  6607. [BEND_IDX(-35)] = 0x0325,
  6608. [BEND_IDX(-40)] = 0x0425,
  6609. [BEND_IDX(-45)] = 0x0425,
  6610. [BEND_IDX(-50)] = 0x0525,
  6611. };
  6612. /*
  6613. * Bend CLKOUT_DP
  6614. * steps -50 to 50 inclusive, in steps of 5
  6615. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6616. * change in clock period = -(steps / 10) * 5.787 ps
  6617. */
  6618. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6619. {
  6620. uint32_t tmp;
  6621. int idx = BEND_IDX(steps);
  6622. if (WARN_ON(steps % 5 != 0))
  6623. return;
  6624. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6625. return;
  6626. mutex_lock(&dev_priv->sb_lock);
  6627. if (steps % 10 != 0)
  6628. tmp = 0xAAAAAAAB;
  6629. else
  6630. tmp = 0x00000000;
  6631. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6632. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6633. tmp &= 0xffff0000;
  6634. tmp |= sscdivintphase[idx];
  6635. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6636. mutex_unlock(&dev_priv->sb_lock);
  6637. }
  6638. #undef BEND_IDX
  6639. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6640. {
  6641. struct intel_encoder *encoder;
  6642. bool has_vga = false;
  6643. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6644. switch (encoder->type) {
  6645. case INTEL_OUTPUT_ANALOG:
  6646. has_vga = true;
  6647. break;
  6648. default:
  6649. break;
  6650. }
  6651. }
  6652. if (has_vga) {
  6653. lpt_bend_clkout_dp(dev_priv, 0);
  6654. lpt_enable_clkout_dp(dev_priv, true, true);
  6655. } else {
  6656. lpt_disable_clkout_dp(dev_priv);
  6657. }
  6658. }
  6659. /*
  6660. * Initialize reference clocks when the driver loads
  6661. */
  6662. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6663. {
  6664. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6665. ironlake_init_pch_refclk(dev_priv);
  6666. else if (HAS_PCH_LPT(dev_priv))
  6667. lpt_init_pch_refclk(dev_priv);
  6668. }
  6669. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6670. {
  6671. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6672. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6673. int pipe = intel_crtc->pipe;
  6674. uint32_t val;
  6675. val = 0;
  6676. switch (intel_crtc->config->pipe_bpp) {
  6677. case 18:
  6678. val |= PIPECONF_6BPC;
  6679. break;
  6680. case 24:
  6681. val |= PIPECONF_8BPC;
  6682. break;
  6683. case 30:
  6684. val |= PIPECONF_10BPC;
  6685. break;
  6686. case 36:
  6687. val |= PIPECONF_12BPC;
  6688. break;
  6689. default:
  6690. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6691. BUG();
  6692. }
  6693. if (intel_crtc->config->dither)
  6694. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6695. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6696. val |= PIPECONF_INTERLACED_ILK;
  6697. else
  6698. val |= PIPECONF_PROGRESSIVE;
  6699. if (intel_crtc->config->limited_color_range)
  6700. val |= PIPECONF_COLOR_RANGE_SELECT;
  6701. I915_WRITE(PIPECONF(pipe), val);
  6702. POSTING_READ(PIPECONF(pipe));
  6703. }
  6704. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6705. {
  6706. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6708. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6709. u32 val = 0;
  6710. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6711. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6712. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6713. val |= PIPECONF_INTERLACED_ILK;
  6714. else
  6715. val |= PIPECONF_PROGRESSIVE;
  6716. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6717. POSTING_READ(PIPECONF(cpu_transcoder));
  6718. }
  6719. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6720. {
  6721. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6722. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6723. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6724. u32 val = 0;
  6725. switch (intel_crtc->config->pipe_bpp) {
  6726. case 18:
  6727. val |= PIPEMISC_DITHER_6_BPC;
  6728. break;
  6729. case 24:
  6730. val |= PIPEMISC_DITHER_8_BPC;
  6731. break;
  6732. case 30:
  6733. val |= PIPEMISC_DITHER_10_BPC;
  6734. break;
  6735. case 36:
  6736. val |= PIPEMISC_DITHER_12_BPC;
  6737. break;
  6738. default:
  6739. /* Case prevented by pipe_config_set_bpp. */
  6740. BUG();
  6741. }
  6742. if (intel_crtc->config->dither)
  6743. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6744. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6745. }
  6746. }
  6747. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6748. {
  6749. /*
  6750. * Account for spread spectrum to avoid
  6751. * oversubscribing the link. Max center spread
  6752. * is 2.5%; use 5% for safety's sake.
  6753. */
  6754. u32 bps = target_clock * bpp * 21 / 20;
  6755. return DIV_ROUND_UP(bps, link_bw * 8);
  6756. }
  6757. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6758. {
  6759. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6760. }
  6761. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6762. struct intel_crtc_state *crtc_state,
  6763. struct dpll *reduced_clock)
  6764. {
  6765. struct drm_crtc *crtc = &intel_crtc->base;
  6766. struct drm_device *dev = crtc->dev;
  6767. struct drm_i915_private *dev_priv = to_i915(dev);
  6768. u32 dpll, fp, fp2;
  6769. int factor;
  6770. /* Enable autotuning of the PLL clock (if permissible) */
  6771. factor = 21;
  6772. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6773. if ((intel_panel_use_ssc(dev_priv) &&
  6774. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6775. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6776. factor = 25;
  6777. } else if (crtc_state->sdvo_tv_clock)
  6778. factor = 20;
  6779. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6780. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6781. fp |= FP_CB_TUNE;
  6782. if (reduced_clock) {
  6783. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6784. if (reduced_clock->m < factor * reduced_clock->n)
  6785. fp2 |= FP_CB_TUNE;
  6786. } else {
  6787. fp2 = fp;
  6788. }
  6789. dpll = 0;
  6790. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6791. dpll |= DPLLB_MODE_LVDS;
  6792. else
  6793. dpll |= DPLLB_MODE_DAC_SERIAL;
  6794. dpll |= (crtc_state->pixel_multiplier - 1)
  6795. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6796. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6797. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6798. dpll |= DPLL_SDVO_HIGH_SPEED;
  6799. if (intel_crtc_has_dp_encoder(crtc_state))
  6800. dpll |= DPLL_SDVO_HIGH_SPEED;
  6801. /*
  6802. * The high speed IO clock is only really required for
  6803. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6804. * possible to share the DPLL between CRT and HDMI. Enabling
  6805. * the clock needlessly does no real harm, except use up a
  6806. * bit of power potentially.
  6807. *
  6808. * We'll limit this to IVB with 3 pipes, since it has only two
  6809. * DPLLs and so DPLL sharing is the only way to get three pipes
  6810. * driving PCH ports at the same time. On SNB we could do this,
  6811. * and potentially avoid enabling the second DPLL, but it's not
  6812. * clear if it''s a win or loss power wise. No point in doing
  6813. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6814. */
  6815. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6816. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6817. dpll |= DPLL_SDVO_HIGH_SPEED;
  6818. /* compute bitmask from p1 value */
  6819. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6820. /* also FPA1 */
  6821. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6822. switch (crtc_state->dpll.p2) {
  6823. case 5:
  6824. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6825. break;
  6826. case 7:
  6827. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6828. break;
  6829. case 10:
  6830. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6831. break;
  6832. case 14:
  6833. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6834. break;
  6835. }
  6836. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6837. intel_panel_use_ssc(dev_priv))
  6838. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6839. else
  6840. dpll |= PLL_REF_INPUT_DREFCLK;
  6841. dpll |= DPLL_VCO_ENABLE;
  6842. crtc_state->dpll_hw_state.dpll = dpll;
  6843. crtc_state->dpll_hw_state.fp0 = fp;
  6844. crtc_state->dpll_hw_state.fp1 = fp2;
  6845. }
  6846. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6847. struct intel_crtc_state *crtc_state)
  6848. {
  6849. struct drm_device *dev = crtc->base.dev;
  6850. struct drm_i915_private *dev_priv = to_i915(dev);
  6851. struct dpll reduced_clock;
  6852. bool has_reduced_clock = false;
  6853. struct intel_shared_dpll *pll;
  6854. const struct intel_limit *limit;
  6855. int refclk = 120000;
  6856. memset(&crtc_state->dpll_hw_state, 0,
  6857. sizeof(crtc_state->dpll_hw_state));
  6858. crtc->lowfreq_avail = false;
  6859. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6860. if (!crtc_state->has_pch_encoder)
  6861. return 0;
  6862. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6863. if (intel_panel_use_ssc(dev_priv)) {
  6864. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6865. dev_priv->vbt.lvds_ssc_freq);
  6866. refclk = dev_priv->vbt.lvds_ssc_freq;
  6867. }
  6868. if (intel_is_dual_link_lvds(dev)) {
  6869. if (refclk == 100000)
  6870. limit = &intel_limits_ironlake_dual_lvds_100m;
  6871. else
  6872. limit = &intel_limits_ironlake_dual_lvds;
  6873. } else {
  6874. if (refclk == 100000)
  6875. limit = &intel_limits_ironlake_single_lvds_100m;
  6876. else
  6877. limit = &intel_limits_ironlake_single_lvds;
  6878. }
  6879. } else {
  6880. limit = &intel_limits_ironlake_dac;
  6881. }
  6882. if (!crtc_state->clock_set &&
  6883. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6884. refclk, NULL, &crtc_state->dpll)) {
  6885. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6886. return -EINVAL;
  6887. }
  6888. ironlake_compute_dpll(crtc, crtc_state,
  6889. has_reduced_clock ? &reduced_clock : NULL);
  6890. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  6891. if (pll == NULL) {
  6892. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6893. pipe_name(crtc->pipe));
  6894. return -EINVAL;
  6895. }
  6896. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6897. has_reduced_clock)
  6898. crtc->lowfreq_avail = true;
  6899. return 0;
  6900. }
  6901. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6902. struct intel_link_m_n *m_n)
  6903. {
  6904. struct drm_device *dev = crtc->base.dev;
  6905. struct drm_i915_private *dev_priv = to_i915(dev);
  6906. enum pipe pipe = crtc->pipe;
  6907. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6908. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6909. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6910. & ~TU_SIZE_MASK;
  6911. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6912. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6913. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6914. }
  6915. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6916. enum transcoder transcoder,
  6917. struct intel_link_m_n *m_n,
  6918. struct intel_link_m_n *m2_n2)
  6919. {
  6920. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6921. enum pipe pipe = crtc->pipe;
  6922. if (INTEL_GEN(dev_priv) >= 5) {
  6923. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6924. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6925. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6926. & ~TU_SIZE_MASK;
  6927. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6928. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6929. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6930. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6931. * gen < 8) and if DRRS is supported (to make sure the
  6932. * registers are not unnecessarily read).
  6933. */
  6934. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6935. crtc->config->has_drrs) {
  6936. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6937. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6938. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6939. & ~TU_SIZE_MASK;
  6940. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6941. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6942. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6943. }
  6944. } else {
  6945. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6946. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6947. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6948. & ~TU_SIZE_MASK;
  6949. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6950. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6951. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6952. }
  6953. }
  6954. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6955. struct intel_crtc_state *pipe_config)
  6956. {
  6957. if (pipe_config->has_pch_encoder)
  6958. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6959. else
  6960. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6961. &pipe_config->dp_m_n,
  6962. &pipe_config->dp_m2_n2);
  6963. }
  6964. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6965. struct intel_crtc_state *pipe_config)
  6966. {
  6967. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6968. &pipe_config->fdi_m_n, NULL);
  6969. }
  6970. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6971. struct intel_crtc_state *pipe_config)
  6972. {
  6973. struct drm_device *dev = crtc->base.dev;
  6974. struct drm_i915_private *dev_priv = to_i915(dev);
  6975. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6976. uint32_t ps_ctrl = 0;
  6977. int id = -1;
  6978. int i;
  6979. /* find scaler attached to this pipe */
  6980. for (i = 0; i < crtc->num_scalers; i++) {
  6981. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6982. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6983. id = i;
  6984. pipe_config->pch_pfit.enabled = true;
  6985. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6986. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  6987. break;
  6988. }
  6989. }
  6990. scaler_state->scaler_id = id;
  6991. if (id >= 0) {
  6992. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  6993. } else {
  6994. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  6995. }
  6996. }
  6997. static void
  6998. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6999. struct intel_initial_plane_config *plane_config)
  7000. {
  7001. struct drm_device *dev = crtc->base.dev;
  7002. struct drm_i915_private *dev_priv = to_i915(dev);
  7003. u32 val, base, offset, stride_mult, tiling;
  7004. int pipe = crtc->pipe;
  7005. int fourcc, pixel_format;
  7006. unsigned int aligned_height;
  7007. struct drm_framebuffer *fb;
  7008. struct intel_framebuffer *intel_fb;
  7009. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7010. if (!intel_fb) {
  7011. DRM_DEBUG_KMS("failed to alloc fb\n");
  7012. return;
  7013. }
  7014. fb = &intel_fb->base;
  7015. fb->dev = dev;
  7016. val = I915_READ(PLANE_CTL(pipe, 0));
  7017. if (!(val & PLANE_CTL_ENABLE))
  7018. goto error;
  7019. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7020. fourcc = skl_format_to_fourcc(pixel_format,
  7021. val & PLANE_CTL_ORDER_RGBX,
  7022. val & PLANE_CTL_ALPHA_MASK);
  7023. fb->format = drm_format_info(fourcc);
  7024. tiling = val & PLANE_CTL_TILED_MASK;
  7025. switch (tiling) {
  7026. case PLANE_CTL_TILED_LINEAR:
  7027. fb->modifier = DRM_FORMAT_MOD_LINEAR;
  7028. break;
  7029. case PLANE_CTL_TILED_X:
  7030. plane_config->tiling = I915_TILING_X;
  7031. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7032. break;
  7033. case PLANE_CTL_TILED_Y:
  7034. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7035. break;
  7036. case PLANE_CTL_TILED_YF:
  7037. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7038. break;
  7039. default:
  7040. MISSING_CASE(tiling);
  7041. goto error;
  7042. }
  7043. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7044. plane_config->base = base;
  7045. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7046. val = I915_READ(PLANE_SIZE(pipe, 0));
  7047. fb->height = ((val >> 16) & 0xfff) + 1;
  7048. fb->width = ((val >> 0) & 0x1fff) + 1;
  7049. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7050. stride_mult = intel_fb_stride_alignment(fb, 0);
  7051. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7052. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7053. plane_config->size = fb->pitches[0] * aligned_height;
  7054. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7055. pipe_name(pipe), fb->width, fb->height,
  7056. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7057. plane_config->size);
  7058. plane_config->fb = intel_fb;
  7059. return;
  7060. error:
  7061. kfree(intel_fb);
  7062. }
  7063. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7064. struct intel_crtc_state *pipe_config)
  7065. {
  7066. struct drm_device *dev = crtc->base.dev;
  7067. struct drm_i915_private *dev_priv = to_i915(dev);
  7068. uint32_t tmp;
  7069. tmp = I915_READ(PF_CTL(crtc->pipe));
  7070. if (tmp & PF_ENABLE) {
  7071. pipe_config->pch_pfit.enabled = true;
  7072. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7073. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7074. /* We currently do not free assignements of panel fitters on
  7075. * ivb/hsw (since we don't use the higher upscaling modes which
  7076. * differentiates them) so just WARN about this case for now. */
  7077. if (IS_GEN7(dev_priv)) {
  7078. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7079. PF_PIPE_SEL_IVB(crtc->pipe));
  7080. }
  7081. }
  7082. }
  7083. static void
  7084. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7085. struct intel_initial_plane_config *plane_config)
  7086. {
  7087. struct drm_device *dev = crtc->base.dev;
  7088. struct drm_i915_private *dev_priv = to_i915(dev);
  7089. u32 val, base, offset;
  7090. int pipe = crtc->pipe;
  7091. int fourcc, pixel_format;
  7092. unsigned int aligned_height;
  7093. struct drm_framebuffer *fb;
  7094. struct intel_framebuffer *intel_fb;
  7095. val = I915_READ(DSPCNTR(pipe));
  7096. if (!(val & DISPLAY_PLANE_ENABLE))
  7097. return;
  7098. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7099. if (!intel_fb) {
  7100. DRM_DEBUG_KMS("failed to alloc fb\n");
  7101. return;
  7102. }
  7103. fb = &intel_fb->base;
  7104. fb->dev = dev;
  7105. if (INTEL_GEN(dev_priv) >= 4) {
  7106. if (val & DISPPLANE_TILED) {
  7107. plane_config->tiling = I915_TILING_X;
  7108. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7109. }
  7110. }
  7111. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7112. fourcc = i9xx_format_to_fourcc(pixel_format);
  7113. fb->format = drm_format_info(fourcc);
  7114. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7115. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7116. offset = I915_READ(DSPOFFSET(pipe));
  7117. } else {
  7118. if (plane_config->tiling)
  7119. offset = I915_READ(DSPTILEOFF(pipe));
  7120. else
  7121. offset = I915_READ(DSPLINOFF(pipe));
  7122. }
  7123. plane_config->base = base;
  7124. val = I915_READ(PIPESRC(pipe));
  7125. fb->width = ((val >> 16) & 0xfff) + 1;
  7126. fb->height = ((val >> 0) & 0xfff) + 1;
  7127. val = I915_READ(DSPSTRIDE(pipe));
  7128. fb->pitches[0] = val & 0xffffffc0;
  7129. aligned_height = intel_fb_align_height(fb, 0, fb->height);
  7130. plane_config->size = fb->pitches[0] * aligned_height;
  7131. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7132. pipe_name(pipe), fb->width, fb->height,
  7133. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7134. plane_config->size);
  7135. plane_config->fb = intel_fb;
  7136. }
  7137. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7138. struct intel_crtc_state *pipe_config)
  7139. {
  7140. struct drm_device *dev = crtc->base.dev;
  7141. struct drm_i915_private *dev_priv = to_i915(dev);
  7142. enum intel_display_power_domain power_domain;
  7143. uint32_t tmp;
  7144. bool ret;
  7145. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7146. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7147. return false;
  7148. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7149. pipe_config->shared_dpll = NULL;
  7150. ret = false;
  7151. tmp = I915_READ(PIPECONF(crtc->pipe));
  7152. if (!(tmp & PIPECONF_ENABLE))
  7153. goto out;
  7154. switch (tmp & PIPECONF_BPC_MASK) {
  7155. case PIPECONF_6BPC:
  7156. pipe_config->pipe_bpp = 18;
  7157. break;
  7158. case PIPECONF_8BPC:
  7159. pipe_config->pipe_bpp = 24;
  7160. break;
  7161. case PIPECONF_10BPC:
  7162. pipe_config->pipe_bpp = 30;
  7163. break;
  7164. case PIPECONF_12BPC:
  7165. pipe_config->pipe_bpp = 36;
  7166. break;
  7167. default:
  7168. break;
  7169. }
  7170. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7171. pipe_config->limited_color_range = true;
  7172. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7173. struct intel_shared_dpll *pll;
  7174. enum intel_dpll_id pll_id;
  7175. pipe_config->has_pch_encoder = true;
  7176. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7177. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7178. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7179. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7180. if (HAS_PCH_IBX(dev_priv)) {
  7181. /*
  7182. * The pipe->pch transcoder and pch transcoder->pll
  7183. * mapping is fixed.
  7184. */
  7185. pll_id = (enum intel_dpll_id) crtc->pipe;
  7186. } else {
  7187. tmp = I915_READ(PCH_DPLL_SEL);
  7188. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7189. pll_id = DPLL_ID_PCH_PLL_B;
  7190. else
  7191. pll_id= DPLL_ID_PCH_PLL_A;
  7192. }
  7193. pipe_config->shared_dpll =
  7194. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7195. pll = pipe_config->shared_dpll;
  7196. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7197. &pipe_config->dpll_hw_state));
  7198. tmp = pipe_config->dpll_hw_state.dpll;
  7199. pipe_config->pixel_multiplier =
  7200. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7201. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7202. ironlake_pch_clock_get(crtc, pipe_config);
  7203. } else {
  7204. pipe_config->pixel_multiplier = 1;
  7205. }
  7206. intel_get_pipe_timings(crtc, pipe_config);
  7207. intel_get_pipe_src_size(crtc, pipe_config);
  7208. ironlake_get_pfit_config(crtc, pipe_config);
  7209. ret = true;
  7210. out:
  7211. intel_display_power_put(dev_priv, power_domain);
  7212. return ret;
  7213. }
  7214. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7215. {
  7216. struct drm_device *dev = &dev_priv->drm;
  7217. struct intel_crtc *crtc;
  7218. for_each_intel_crtc(dev, crtc)
  7219. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7220. pipe_name(crtc->pipe));
  7221. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7222. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7223. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7224. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7225. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7226. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7227. "CPU PWM1 enabled\n");
  7228. if (IS_HASWELL(dev_priv))
  7229. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7230. "CPU PWM2 enabled\n");
  7231. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7232. "PCH PWM1 enabled\n");
  7233. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7234. "Utility pin enabled\n");
  7235. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7236. /*
  7237. * In theory we can still leave IRQs enabled, as long as only the HPD
  7238. * interrupts remain enabled. We used to check for that, but since it's
  7239. * gen-specific and since we only disable LCPLL after we fully disable
  7240. * the interrupts, the check below should be enough.
  7241. */
  7242. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7243. }
  7244. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7245. {
  7246. if (IS_HASWELL(dev_priv))
  7247. return I915_READ(D_COMP_HSW);
  7248. else
  7249. return I915_READ(D_COMP_BDW);
  7250. }
  7251. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7252. {
  7253. if (IS_HASWELL(dev_priv)) {
  7254. mutex_lock(&dev_priv->rps.hw_lock);
  7255. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7256. val))
  7257. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7258. mutex_unlock(&dev_priv->rps.hw_lock);
  7259. } else {
  7260. I915_WRITE(D_COMP_BDW, val);
  7261. POSTING_READ(D_COMP_BDW);
  7262. }
  7263. }
  7264. /*
  7265. * This function implements pieces of two sequences from BSpec:
  7266. * - Sequence for display software to disable LCPLL
  7267. * - Sequence for display software to allow package C8+
  7268. * The steps implemented here are just the steps that actually touch the LCPLL
  7269. * register. Callers should take care of disabling all the display engine
  7270. * functions, doing the mode unset, fixing interrupts, etc.
  7271. */
  7272. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7273. bool switch_to_fclk, bool allow_power_down)
  7274. {
  7275. uint32_t val;
  7276. assert_can_disable_lcpll(dev_priv);
  7277. val = I915_READ(LCPLL_CTL);
  7278. if (switch_to_fclk) {
  7279. val |= LCPLL_CD_SOURCE_FCLK;
  7280. I915_WRITE(LCPLL_CTL, val);
  7281. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7282. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7283. DRM_ERROR("Switching to FCLK failed\n");
  7284. val = I915_READ(LCPLL_CTL);
  7285. }
  7286. val |= LCPLL_PLL_DISABLE;
  7287. I915_WRITE(LCPLL_CTL, val);
  7288. POSTING_READ(LCPLL_CTL);
  7289. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7290. DRM_ERROR("LCPLL still locked\n");
  7291. val = hsw_read_dcomp(dev_priv);
  7292. val |= D_COMP_COMP_DISABLE;
  7293. hsw_write_dcomp(dev_priv, val);
  7294. ndelay(100);
  7295. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7296. 1))
  7297. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7298. if (allow_power_down) {
  7299. val = I915_READ(LCPLL_CTL);
  7300. val |= LCPLL_POWER_DOWN_ALLOW;
  7301. I915_WRITE(LCPLL_CTL, val);
  7302. POSTING_READ(LCPLL_CTL);
  7303. }
  7304. }
  7305. /*
  7306. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7307. * source.
  7308. */
  7309. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7310. {
  7311. uint32_t val;
  7312. val = I915_READ(LCPLL_CTL);
  7313. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7314. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7315. return;
  7316. /*
  7317. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7318. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7319. */
  7320. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7321. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7322. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7323. I915_WRITE(LCPLL_CTL, val);
  7324. POSTING_READ(LCPLL_CTL);
  7325. }
  7326. val = hsw_read_dcomp(dev_priv);
  7327. val |= D_COMP_COMP_FORCE;
  7328. val &= ~D_COMP_COMP_DISABLE;
  7329. hsw_write_dcomp(dev_priv, val);
  7330. val = I915_READ(LCPLL_CTL);
  7331. val &= ~LCPLL_PLL_DISABLE;
  7332. I915_WRITE(LCPLL_CTL, val);
  7333. if (intel_wait_for_register(dev_priv,
  7334. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7335. 5))
  7336. DRM_ERROR("LCPLL not locked yet\n");
  7337. if (val & LCPLL_CD_SOURCE_FCLK) {
  7338. val = I915_READ(LCPLL_CTL);
  7339. val &= ~LCPLL_CD_SOURCE_FCLK;
  7340. I915_WRITE(LCPLL_CTL, val);
  7341. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7342. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7343. DRM_ERROR("Switching back to LCPLL failed\n");
  7344. }
  7345. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7346. intel_update_cdclk(dev_priv);
  7347. }
  7348. /*
  7349. * Package states C8 and deeper are really deep PC states that can only be
  7350. * reached when all the devices on the system allow it, so even if the graphics
  7351. * device allows PC8+, it doesn't mean the system will actually get to these
  7352. * states. Our driver only allows PC8+ when going into runtime PM.
  7353. *
  7354. * The requirements for PC8+ are that all the outputs are disabled, the power
  7355. * well is disabled and most interrupts are disabled, and these are also
  7356. * requirements for runtime PM. When these conditions are met, we manually do
  7357. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7358. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7359. * hang the machine.
  7360. *
  7361. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7362. * the state of some registers, so when we come back from PC8+ we need to
  7363. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7364. * need to take care of the registers kept by RC6. Notice that this happens even
  7365. * if we don't put the device in PCI D3 state (which is what currently happens
  7366. * because of the runtime PM support).
  7367. *
  7368. * For more, read "Display Sequences for Package C8" on the hardware
  7369. * documentation.
  7370. */
  7371. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7372. {
  7373. uint32_t val;
  7374. DRM_DEBUG_KMS("Enabling package C8+\n");
  7375. if (HAS_PCH_LPT_LP(dev_priv)) {
  7376. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7377. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7378. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7379. }
  7380. lpt_disable_clkout_dp(dev_priv);
  7381. hsw_disable_lcpll(dev_priv, true, true);
  7382. }
  7383. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7384. {
  7385. uint32_t val;
  7386. DRM_DEBUG_KMS("Disabling package C8+\n");
  7387. hsw_restore_lcpll(dev_priv);
  7388. lpt_init_pch_refclk(dev_priv);
  7389. if (HAS_PCH_LPT_LP(dev_priv)) {
  7390. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7391. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7392. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7393. }
  7394. }
  7395. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7396. struct intel_crtc_state *crtc_state)
  7397. {
  7398. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7399. struct intel_encoder *encoder =
  7400. intel_ddi_get_crtc_new_encoder(crtc_state);
  7401. if (!intel_get_shared_dpll(crtc, crtc_state, encoder)) {
  7402. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  7403. pipe_name(crtc->pipe));
  7404. return -EINVAL;
  7405. }
  7406. }
  7407. crtc->lowfreq_avail = false;
  7408. return 0;
  7409. }
  7410. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7411. enum port port,
  7412. struct intel_crtc_state *pipe_config)
  7413. {
  7414. enum intel_dpll_id id;
  7415. switch (port) {
  7416. case PORT_A:
  7417. id = DPLL_ID_SKL_DPLL0;
  7418. break;
  7419. case PORT_B:
  7420. id = DPLL_ID_SKL_DPLL1;
  7421. break;
  7422. case PORT_C:
  7423. id = DPLL_ID_SKL_DPLL2;
  7424. break;
  7425. default:
  7426. DRM_ERROR("Incorrect port type\n");
  7427. return;
  7428. }
  7429. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7430. }
  7431. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7432. enum port port,
  7433. struct intel_crtc_state *pipe_config)
  7434. {
  7435. enum intel_dpll_id id;
  7436. u32 temp;
  7437. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7438. id = temp >> (port * 3 + 1);
  7439. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7440. return;
  7441. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7442. }
  7443. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7444. enum port port,
  7445. struct intel_crtc_state *pipe_config)
  7446. {
  7447. enum intel_dpll_id id;
  7448. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7449. switch (ddi_pll_sel) {
  7450. case PORT_CLK_SEL_WRPLL1:
  7451. id = DPLL_ID_WRPLL1;
  7452. break;
  7453. case PORT_CLK_SEL_WRPLL2:
  7454. id = DPLL_ID_WRPLL2;
  7455. break;
  7456. case PORT_CLK_SEL_SPLL:
  7457. id = DPLL_ID_SPLL;
  7458. break;
  7459. case PORT_CLK_SEL_LCPLL_810:
  7460. id = DPLL_ID_LCPLL_810;
  7461. break;
  7462. case PORT_CLK_SEL_LCPLL_1350:
  7463. id = DPLL_ID_LCPLL_1350;
  7464. break;
  7465. case PORT_CLK_SEL_LCPLL_2700:
  7466. id = DPLL_ID_LCPLL_2700;
  7467. break;
  7468. default:
  7469. MISSING_CASE(ddi_pll_sel);
  7470. /* fall through */
  7471. case PORT_CLK_SEL_NONE:
  7472. return;
  7473. }
  7474. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7475. }
  7476. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7477. struct intel_crtc_state *pipe_config,
  7478. u64 *power_domain_mask)
  7479. {
  7480. struct drm_device *dev = crtc->base.dev;
  7481. struct drm_i915_private *dev_priv = to_i915(dev);
  7482. enum intel_display_power_domain power_domain;
  7483. u32 tmp;
  7484. /*
  7485. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7486. * transcoder handled below.
  7487. */
  7488. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7489. /*
  7490. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7491. * consistency and less surprising code; it's in always on power).
  7492. */
  7493. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7494. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7495. enum pipe trans_edp_pipe;
  7496. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7497. default:
  7498. WARN(1, "unknown pipe linked to edp transcoder\n");
  7499. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7500. case TRANS_DDI_EDP_INPUT_A_ON:
  7501. trans_edp_pipe = PIPE_A;
  7502. break;
  7503. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7504. trans_edp_pipe = PIPE_B;
  7505. break;
  7506. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7507. trans_edp_pipe = PIPE_C;
  7508. break;
  7509. }
  7510. if (trans_edp_pipe == crtc->pipe)
  7511. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7512. }
  7513. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7514. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7515. return false;
  7516. *power_domain_mask |= BIT_ULL(power_domain);
  7517. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7518. return tmp & PIPECONF_ENABLE;
  7519. }
  7520. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7521. struct intel_crtc_state *pipe_config,
  7522. u64 *power_domain_mask)
  7523. {
  7524. struct drm_device *dev = crtc->base.dev;
  7525. struct drm_i915_private *dev_priv = to_i915(dev);
  7526. enum intel_display_power_domain power_domain;
  7527. enum port port;
  7528. enum transcoder cpu_transcoder;
  7529. u32 tmp;
  7530. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7531. if (port == PORT_A)
  7532. cpu_transcoder = TRANSCODER_DSI_A;
  7533. else
  7534. cpu_transcoder = TRANSCODER_DSI_C;
  7535. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7536. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7537. continue;
  7538. *power_domain_mask |= BIT_ULL(power_domain);
  7539. /*
  7540. * The PLL needs to be enabled with a valid divider
  7541. * configuration, otherwise accessing DSI registers will hang
  7542. * the machine. See BSpec North Display Engine
  7543. * registers/MIPI[BXT]. We can break out here early, since we
  7544. * need the same DSI PLL to be enabled for both DSI ports.
  7545. */
  7546. if (!intel_dsi_pll_is_enabled(dev_priv))
  7547. break;
  7548. /* XXX: this works for video mode only */
  7549. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7550. if (!(tmp & DPI_ENABLE))
  7551. continue;
  7552. tmp = I915_READ(MIPI_CTRL(port));
  7553. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7554. continue;
  7555. pipe_config->cpu_transcoder = cpu_transcoder;
  7556. break;
  7557. }
  7558. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7559. }
  7560. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7561. struct intel_crtc_state *pipe_config)
  7562. {
  7563. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7564. struct intel_shared_dpll *pll;
  7565. enum port port;
  7566. uint32_t tmp;
  7567. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7568. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7569. if (IS_GEN9_BC(dev_priv))
  7570. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7571. else if (IS_GEN9_LP(dev_priv))
  7572. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7573. else
  7574. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7575. pll = pipe_config->shared_dpll;
  7576. if (pll) {
  7577. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7578. &pipe_config->dpll_hw_state));
  7579. }
  7580. /*
  7581. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7582. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7583. * the PCH transcoder is on.
  7584. */
  7585. if (INTEL_GEN(dev_priv) < 9 &&
  7586. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7587. pipe_config->has_pch_encoder = true;
  7588. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7589. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7590. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7591. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7592. }
  7593. }
  7594. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7595. struct intel_crtc_state *pipe_config)
  7596. {
  7597. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7598. enum intel_display_power_domain power_domain;
  7599. u64 power_domain_mask;
  7600. bool active;
  7601. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7602. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7603. return false;
  7604. power_domain_mask = BIT_ULL(power_domain);
  7605. pipe_config->shared_dpll = NULL;
  7606. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7607. if (IS_GEN9_LP(dev_priv) &&
  7608. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7609. WARN_ON(active);
  7610. active = true;
  7611. }
  7612. if (!active)
  7613. goto out;
  7614. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7615. haswell_get_ddi_port_state(crtc, pipe_config);
  7616. intel_get_pipe_timings(crtc, pipe_config);
  7617. }
  7618. intel_get_pipe_src_size(crtc, pipe_config);
  7619. pipe_config->gamma_mode =
  7620. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7621. if (INTEL_GEN(dev_priv) >= 9) {
  7622. intel_crtc_init_scalers(crtc, pipe_config);
  7623. pipe_config->scaler_state.scaler_id = -1;
  7624. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7625. }
  7626. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7627. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7628. power_domain_mask |= BIT_ULL(power_domain);
  7629. if (INTEL_GEN(dev_priv) >= 9)
  7630. skylake_get_pfit_config(crtc, pipe_config);
  7631. else
  7632. ironlake_get_pfit_config(crtc, pipe_config);
  7633. }
  7634. if (IS_HASWELL(dev_priv))
  7635. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7636. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7637. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7638. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7639. pipe_config->pixel_multiplier =
  7640. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7641. } else {
  7642. pipe_config->pixel_multiplier = 1;
  7643. }
  7644. out:
  7645. for_each_power_domain(power_domain, power_domain_mask)
  7646. intel_display_power_put(dev_priv, power_domain);
  7647. return active;
  7648. }
  7649. static u32 i845_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7650. const struct intel_plane_state *plane_state)
  7651. {
  7652. unsigned int width = plane_state->base.crtc_w;
  7653. unsigned int stride = roundup_pow_of_two(width) * 4;
  7654. switch (stride) {
  7655. default:
  7656. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7657. width, stride);
  7658. stride = 256;
  7659. /* fallthrough */
  7660. case 256:
  7661. case 512:
  7662. case 1024:
  7663. case 2048:
  7664. break;
  7665. }
  7666. return CURSOR_ENABLE |
  7667. CURSOR_GAMMA_ENABLE |
  7668. CURSOR_FORMAT_ARGB |
  7669. CURSOR_STRIDE(stride);
  7670. }
  7671. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  7672. const struct intel_plane_state *plane_state)
  7673. {
  7674. struct drm_device *dev = crtc->dev;
  7675. struct drm_i915_private *dev_priv = to_i915(dev);
  7676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7677. uint32_t cntl = 0, size = 0;
  7678. if (plane_state && plane_state->base.visible) {
  7679. unsigned int width = plane_state->base.crtc_w;
  7680. unsigned int height = plane_state->base.crtc_h;
  7681. cntl = plane_state->ctl;
  7682. size = (height << 12) | width;
  7683. }
  7684. if (intel_crtc->cursor_cntl != 0 &&
  7685. (intel_crtc->cursor_base != base ||
  7686. intel_crtc->cursor_size != size ||
  7687. intel_crtc->cursor_cntl != cntl)) {
  7688. /* On these chipsets we can only modify the base/size/stride
  7689. * whilst the cursor is disabled.
  7690. */
  7691. I915_WRITE_FW(CURCNTR(PIPE_A), 0);
  7692. POSTING_READ_FW(CURCNTR(PIPE_A));
  7693. intel_crtc->cursor_cntl = 0;
  7694. }
  7695. if (intel_crtc->cursor_base != base) {
  7696. I915_WRITE_FW(CURBASE(PIPE_A), base);
  7697. intel_crtc->cursor_base = base;
  7698. }
  7699. if (intel_crtc->cursor_size != size) {
  7700. I915_WRITE_FW(CURSIZE, size);
  7701. intel_crtc->cursor_size = size;
  7702. }
  7703. if (intel_crtc->cursor_cntl != cntl) {
  7704. I915_WRITE_FW(CURCNTR(PIPE_A), cntl);
  7705. POSTING_READ_FW(CURCNTR(PIPE_A));
  7706. intel_crtc->cursor_cntl = cntl;
  7707. }
  7708. }
  7709. static u32 i9xx_cursor_ctl(const struct intel_crtc_state *crtc_state,
  7710. const struct intel_plane_state *plane_state)
  7711. {
  7712. struct drm_i915_private *dev_priv =
  7713. to_i915(plane_state->base.plane->dev);
  7714. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  7715. enum pipe pipe = crtc->pipe;
  7716. u32 cntl;
  7717. cntl = MCURSOR_GAMMA_ENABLE;
  7718. if (HAS_DDI(dev_priv))
  7719. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7720. cntl |= pipe << 28; /* Connect to correct pipe */
  7721. switch (plane_state->base.crtc_w) {
  7722. case 64:
  7723. cntl |= CURSOR_MODE_64_ARGB_AX;
  7724. break;
  7725. case 128:
  7726. cntl |= CURSOR_MODE_128_ARGB_AX;
  7727. break;
  7728. case 256:
  7729. cntl |= CURSOR_MODE_256_ARGB_AX;
  7730. break;
  7731. default:
  7732. MISSING_CASE(plane_state->base.crtc_w);
  7733. return 0;
  7734. }
  7735. if (plane_state->base.rotation & DRM_ROTATE_180)
  7736. cntl |= CURSOR_ROTATE_180;
  7737. return cntl;
  7738. }
  7739. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  7740. const struct intel_plane_state *plane_state)
  7741. {
  7742. struct drm_device *dev = crtc->dev;
  7743. struct drm_i915_private *dev_priv = to_i915(dev);
  7744. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7745. int pipe = intel_crtc->pipe;
  7746. uint32_t cntl = 0;
  7747. if (plane_state && plane_state->base.visible)
  7748. cntl = plane_state->ctl;
  7749. if (intel_crtc->cursor_cntl != cntl) {
  7750. I915_WRITE_FW(CURCNTR(pipe), cntl);
  7751. POSTING_READ_FW(CURCNTR(pipe));
  7752. intel_crtc->cursor_cntl = cntl;
  7753. }
  7754. /* and commit changes on next vblank */
  7755. I915_WRITE_FW(CURBASE(pipe), base);
  7756. POSTING_READ_FW(CURBASE(pipe));
  7757. intel_crtc->cursor_base = base;
  7758. }
  7759. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7760. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7761. const struct intel_plane_state *plane_state)
  7762. {
  7763. struct drm_device *dev = crtc->dev;
  7764. struct drm_i915_private *dev_priv = to_i915(dev);
  7765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7766. int pipe = intel_crtc->pipe;
  7767. u32 base = intel_crtc->cursor_addr;
  7768. unsigned long irqflags;
  7769. u32 pos = 0;
  7770. if (plane_state) {
  7771. int x = plane_state->base.crtc_x;
  7772. int y = plane_state->base.crtc_y;
  7773. if (x < 0) {
  7774. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7775. x = -x;
  7776. }
  7777. pos |= x << CURSOR_X_SHIFT;
  7778. if (y < 0) {
  7779. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7780. y = -y;
  7781. }
  7782. pos |= y << CURSOR_Y_SHIFT;
  7783. /* ILK+ do this automagically */
  7784. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7785. plane_state->base.rotation & DRM_ROTATE_180) {
  7786. base += (plane_state->base.crtc_h *
  7787. plane_state->base.crtc_w - 1) * 4;
  7788. }
  7789. }
  7790. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  7791. I915_WRITE_FW(CURPOS(pipe), pos);
  7792. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  7793. i845_update_cursor(crtc, base, plane_state);
  7794. else
  7795. i9xx_update_cursor(crtc, base, plane_state);
  7796. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  7797. }
  7798. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  7799. uint32_t width, uint32_t height)
  7800. {
  7801. if (width == 0 || height == 0)
  7802. return false;
  7803. /*
  7804. * 845g/865g are special in that they are only limited by
  7805. * the width of their cursors, the height is arbitrary up to
  7806. * the precision of the register. Everything else requires
  7807. * square cursors, limited to a few power-of-two sizes.
  7808. */
  7809. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  7810. if ((width & 63) != 0)
  7811. return false;
  7812. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  7813. return false;
  7814. if (height > 1023)
  7815. return false;
  7816. } else {
  7817. switch (width | height) {
  7818. case 256:
  7819. case 128:
  7820. if (IS_GEN2(dev_priv))
  7821. return false;
  7822. case 64:
  7823. break;
  7824. default:
  7825. return false;
  7826. }
  7827. }
  7828. return true;
  7829. }
  7830. /* VESA 640x480x72Hz mode to set on the pipe */
  7831. static struct drm_display_mode load_detect_mode = {
  7832. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7833. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7834. };
  7835. struct drm_framebuffer *
  7836. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7837. struct drm_mode_fb_cmd2 *mode_cmd)
  7838. {
  7839. struct intel_framebuffer *intel_fb;
  7840. int ret;
  7841. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7842. if (!intel_fb)
  7843. return ERR_PTR(-ENOMEM);
  7844. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7845. if (ret)
  7846. goto err;
  7847. return &intel_fb->base;
  7848. err:
  7849. kfree(intel_fb);
  7850. return ERR_PTR(ret);
  7851. }
  7852. static u32
  7853. intel_framebuffer_pitch_for_width(int width, int bpp)
  7854. {
  7855. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7856. return ALIGN(pitch, 64);
  7857. }
  7858. static u32
  7859. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7860. {
  7861. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7862. return PAGE_ALIGN(pitch * mode->vdisplay);
  7863. }
  7864. static struct drm_framebuffer *
  7865. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7866. struct drm_display_mode *mode,
  7867. int depth, int bpp)
  7868. {
  7869. struct drm_framebuffer *fb;
  7870. struct drm_i915_gem_object *obj;
  7871. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7872. obj = i915_gem_object_create(to_i915(dev),
  7873. intel_framebuffer_size_for_mode(mode, bpp));
  7874. if (IS_ERR(obj))
  7875. return ERR_CAST(obj);
  7876. mode_cmd.width = mode->hdisplay;
  7877. mode_cmd.height = mode->vdisplay;
  7878. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7879. bpp);
  7880. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7881. fb = intel_framebuffer_create(obj, &mode_cmd);
  7882. if (IS_ERR(fb))
  7883. i915_gem_object_put(obj);
  7884. return fb;
  7885. }
  7886. static struct drm_framebuffer *
  7887. mode_fits_in_fbdev(struct drm_device *dev,
  7888. struct drm_display_mode *mode)
  7889. {
  7890. #ifdef CONFIG_DRM_FBDEV_EMULATION
  7891. struct drm_i915_private *dev_priv = to_i915(dev);
  7892. struct drm_i915_gem_object *obj;
  7893. struct drm_framebuffer *fb;
  7894. if (!dev_priv->fbdev)
  7895. return NULL;
  7896. if (!dev_priv->fbdev->fb)
  7897. return NULL;
  7898. obj = dev_priv->fbdev->fb->obj;
  7899. BUG_ON(!obj);
  7900. fb = &dev_priv->fbdev->fb->base;
  7901. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7902. fb->format->cpp[0] * 8))
  7903. return NULL;
  7904. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7905. return NULL;
  7906. drm_framebuffer_reference(fb);
  7907. return fb;
  7908. #else
  7909. return NULL;
  7910. #endif
  7911. }
  7912. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  7913. struct drm_crtc *crtc,
  7914. struct drm_display_mode *mode,
  7915. struct drm_framebuffer *fb,
  7916. int x, int y)
  7917. {
  7918. struct drm_plane_state *plane_state;
  7919. int hdisplay, vdisplay;
  7920. int ret;
  7921. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  7922. if (IS_ERR(plane_state))
  7923. return PTR_ERR(plane_state);
  7924. if (mode)
  7925. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  7926. else
  7927. hdisplay = vdisplay = 0;
  7928. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  7929. if (ret)
  7930. return ret;
  7931. drm_atomic_set_fb_for_plane(plane_state, fb);
  7932. plane_state->crtc_x = 0;
  7933. plane_state->crtc_y = 0;
  7934. plane_state->crtc_w = hdisplay;
  7935. plane_state->crtc_h = vdisplay;
  7936. plane_state->src_x = x << 16;
  7937. plane_state->src_y = y << 16;
  7938. plane_state->src_w = hdisplay << 16;
  7939. plane_state->src_h = vdisplay << 16;
  7940. return 0;
  7941. }
  7942. int intel_get_load_detect_pipe(struct drm_connector *connector,
  7943. struct drm_display_mode *mode,
  7944. struct intel_load_detect_pipe *old,
  7945. struct drm_modeset_acquire_ctx *ctx)
  7946. {
  7947. struct intel_crtc *intel_crtc;
  7948. struct intel_encoder *intel_encoder =
  7949. intel_attached_encoder(connector);
  7950. struct drm_crtc *possible_crtc;
  7951. struct drm_encoder *encoder = &intel_encoder->base;
  7952. struct drm_crtc *crtc = NULL;
  7953. struct drm_device *dev = encoder->dev;
  7954. struct drm_i915_private *dev_priv = to_i915(dev);
  7955. struct drm_framebuffer *fb;
  7956. struct drm_mode_config *config = &dev->mode_config;
  7957. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  7958. struct drm_connector_state *connector_state;
  7959. struct intel_crtc_state *crtc_state;
  7960. int ret, i = -1;
  7961. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7962. connector->base.id, connector->name,
  7963. encoder->base.id, encoder->name);
  7964. old->restore_state = NULL;
  7965. WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  7966. /*
  7967. * Algorithm gets a little messy:
  7968. *
  7969. * - if the connector already has an assigned crtc, use it (but make
  7970. * sure it's on first)
  7971. *
  7972. * - try to find the first unused crtc that can drive this connector,
  7973. * and use that if we find one
  7974. */
  7975. /* See if we already have a CRTC for this connector */
  7976. if (connector->state->crtc) {
  7977. crtc = connector->state->crtc;
  7978. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7979. if (ret)
  7980. goto fail;
  7981. /* Make sure the crtc and connector are running */
  7982. goto found;
  7983. }
  7984. /* Find an unused one (if possible) */
  7985. for_each_crtc(dev, possible_crtc) {
  7986. i++;
  7987. if (!(encoder->possible_crtcs & (1 << i)))
  7988. continue;
  7989. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  7990. if (ret)
  7991. goto fail;
  7992. if (possible_crtc->state->enable) {
  7993. drm_modeset_unlock(&possible_crtc->mutex);
  7994. continue;
  7995. }
  7996. crtc = possible_crtc;
  7997. break;
  7998. }
  7999. /*
  8000. * If we didn't find an unused CRTC, don't use any.
  8001. */
  8002. if (!crtc) {
  8003. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  8004. goto fail;
  8005. }
  8006. found:
  8007. intel_crtc = to_intel_crtc(crtc);
  8008. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  8009. if (ret)
  8010. goto fail;
  8011. state = drm_atomic_state_alloc(dev);
  8012. restore_state = drm_atomic_state_alloc(dev);
  8013. if (!state || !restore_state) {
  8014. ret = -ENOMEM;
  8015. goto fail;
  8016. }
  8017. state->acquire_ctx = ctx;
  8018. restore_state->acquire_ctx = ctx;
  8019. connector_state = drm_atomic_get_connector_state(state, connector);
  8020. if (IS_ERR(connector_state)) {
  8021. ret = PTR_ERR(connector_state);
  8022. goto fail;
  8023. }
  8024. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  8025. if (ret)
  8026. goto fail;
  8027. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8028. if (IS_ERR(crtc_state)) {
  8029. ret = PTR_ERR(crtc_state);
  8030. goto fail;
  8031. }
  8032. crtc_state->base.active = crtc_state->base.enable = true;
  8033. if (!mode)
  8034. mode = &load_detect_mode;
  8035. /* We need a framebuffer large enough to accommodate all accesses
  8036. * that the plane may generate whilst we perform load detection.
  8037. * We can not rely on the fbcon either being present (we get called
  8038. * during its initialisation to detect all boot displays, or it may
  8039. * not even exist) or that it is large enough to satisfy the
  8040. * requested mode.
  8041. */
  8042. fb = mode_fits_in_fbdev(dev, mode);
  8043. if (fb == NULL) {
  8044. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8045. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8046. } else
  8047. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8048. if (IS_ERR(fb)) {
  8049. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8050. goto fail;
  8051. }
  8052. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8053. if (ret)
  8054. goto fail;
  8055. drm_framebuffer_unreference(fb);
  8056. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8057. if (ret)
  8058. goto fail;
  8059. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8060. if (!ret)
  8061. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8062. if (!ret)
  8063. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8064. if (ret) {
  8065. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8066. goto fail;
  8067. }
  8068. ret = drm_atomic_commit(state);
  8069. if (ret) {
  8070. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8071. goto fail;
  8072. }
  8073. old->restore_state = restore_state;
  8074. drm_atomic_state_put(state);
  8075. /* let the connector get through one full cycle before testing */
  8076. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8077. return true;
  8078. fail:
  8079. if (state) {
  8080. drm_atomic_state_put(state);
  8081. state = NULL;
  8082. }
  8083. if (restore_state) {
  8084. drm_atomic_state_put(restore_state);
  8085. restore_state = NULL;
  8086. }
  8087. if (ret == -EDEADLK)
  8088. return ret;
  8089. return false;
  8090. }
  8091. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8092. struct intel_load_detect_pipe *old,
  8093. struct drm_modeset_acquire_ctx *ctx)
  8094. {
  8095. struct intel_encoder *intel_encoder =
  8096. intel_attached_encoder(connector);
  8097. struct drm_encoder *encoder = &intel_encoder->base;
  8098. struct drm_atomic_state *state = old->restore_state;
  8099. int ret;
  8100. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8101. connector->base.id, connector->name,
  8102. encoder->base.id, encoder->name);
  8103. if (!state)
  8104. return;
  8105. ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
  8106. if (ret)
  8107. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8108. drm_atomic_state_put(state);
  8109. }
  8110. static int i9xx_pll_refclk(struct drm_device *dev,
  8111. const struct intel_crtc_state *pipe_config)
  8112. {
  8113. struct drm_i915_private *dev_priv = to_i915(dev);
  8114. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8115. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8116. return dev_priv->vbt.lvds_ssc_freq;
  8117. else if (HAS_PCH_SPLIT(dev_priv))
  8118. return 120000;
  8119. else if (!IS_GEN2(dev_priv))
  8120. return 96000;
  8121. else
  8122. return 48000;
  8123. }
  8124. /* Returns the clock of the currently programmed mode of the given pipe. */
  8125. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8126. struct intel_crtc_state *pipe_config)
  8127. {
  8128. struct drm_device *dev = crtc->base.dev;
  8129. struct drm_i915_private *dev_priv = to_i915(dev);
  8130. int pipe = pipe_config->cpu_transcoder;
  8131. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8132. u32 fp;
  8133. struct dpll clock;
  8134. int port_clock;
  8135. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8136. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8137. fp = pipe_config->dpll_hw_state.fp0;
  8138. else
  8139. fp = pipe_config->dpll_hw_state.fp1;
  8140. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8141. if (IS_PINEVIEW(dev_priv)) {
  8142. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8143. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8144. } else {
  8145. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8146. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8147. }
  8148. if (!IS_GEN2(dev_priv)) {
  8149. if (IS_PINEVIEW(dev_priv))
  8150. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8151. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8152. else
  8153. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8154. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8155. switch (dpll & DPLL_MODE_MASK) {
  8156. case DPLLB_MODE_DAC_SERIAL:
  8157. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8158. 5 : 10;
  8159. break;
  8160. case DPLLB_MODE_LVDS:
  8161. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8162. 7 : 14;
  8163. break;
  8164. default:
  8165. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8166. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8167. return;
  8168. }
  8169. if (IS_PINEVIEW(dev_priv))
  8170. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8171. else
  8172. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8173. } else {
  8174. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8175. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8176. if (is_lvds) {
  8177. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8178. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8179. if (lvds & LVDS_CLKB_POWER_UP)
  8180. clock.p2 = 7;
  8181. else
  8182. clock.p2 = 14;
  8183. } else {
  8184. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8185. clock.p1 = 2;
  8186. else {
  8187. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8188. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8189. }
  8190. if (dpll & PLL_P2_DIVIDE_BY_4)
  8191. clock.p2 = 4;
  8192. else
  8193. clock.p2 = 2;
  8194. }
  8195. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8196. }
  8197. /*
  8198. * This value includes pixel_multiplier. We will use
  8199. * port_clock to compute adjusted_mode.crtc_clock in the
  8200. * encoder's get_config() function.
  8201. */
  8202. pipe_config->port_clock = port_clock;
  8203. }
  8204. int intel_dotclock_calculate(int link_freq,
  8205. const struct intel_link_m_n *m_n)
  8206. {
  8207. /*
  8208. * The calculation for the data clock is:
  8209. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8210. * But we want to avoid losing precison if possible, so:
  8211. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8212. *
  8213. * and the link clock is simpler:
  8214. * link_clock = (m * link_clock) / n
  8215. */
  8216. if (!m_n->link_n)
  8217. return 0;
  8218. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8219. }
  8220. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8221. struct intel_crtc_state *pipe_config)
  8222. {
  8223. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8224. /* read out port_clock from the DPLL */
  8225. i9xx_crtc_clock_get(crtc, pipe_config);
  8226. /*
  8227. * In case there is an active pipe without active ports,
  8228. * we may need some idea for the dotclock anyway.
  8229. * Calculate one based on the FDI configuration.
  8230. */
  8231. pipe_config->base.adjusted_mode.crtc_clock =
  8232. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8233. &pipe_config->fdi_m_n);
  8234. }
  8235. /** Returns the currently programmed mode of the given pipe. */
  8236. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8237. struct drm_crtc *crtc)
  8238. {
  8239. struct drm_i915_private *dev_priv = to_i915(dev);
  8240. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8241. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8242. struct drm_display_mode *mode;
  8243. struct intel_crtc_state *pipe_config;
  8244. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8245. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8246. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8247. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8248. enum pipe pipe = intel_crtc->pipe;
  8249. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8250. if (!mode)
  8251. return NULL;
  8252. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8253. if (!pipe_config) {
  8254. kfree(mode);
  8255. return NULL;
  8256. }
  8257. /*
  8258. * Construct a pipe_config sufficient for getting the clock info
  8259. * back out of crtc_clock_get.
  8260. *
  8261. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8262. * to use a real value here instead.
  8263. */
  8264. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8265. pipe_config->pixel_multiplier = 1;
  8266. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8267. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8268. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8269. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8270. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8271. mode->hdisplay = (htot & 0xffff) + 1;
  8272. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8273. mode->hsync_start = (hsync & 0xffff) + 1;
  8274. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8275. mode->vdisplay = (vtot & 0xffff) + 1;
  8276. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8277. mode->vsync_start = (vsync & 0xffff) + 1;
  8278. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8279. drm_mode_set_name(mode);
  8280. kfree(pipe_config);
  8281. return mode;
  8282. }
  8283. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8284. {
  8285. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8286. struct drm_device *dev = crtc->dev;
  8287. struct intel_flip_work *work;
  8288. spin_lock_irq(&dev->event_lock);
  8289. work = intel_crtc->flip_work;
  8290. intel_crtc->flip_work = NULL;
  8291. spin_unlock_irq(&dev->event_lock);
  8292. if (work) {
  8293. cancel_work_sync(&work->mmio_work);
  8294. cancel_work_sync(&work->unpin_work);
  8295. kfree(work);
  8296. }
  8297. drm_crtc_cleanup(crtc);
  8298. kfree(intel_crtc);
  8299. }
  8300. static void intel_unpin_work_fn(struct work_struct *__work)
  8301. {
  8302. struct intel_flip_work *work =
  8303. container_of(__work, struct intel_flip_work, unpin_work);
  8304. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8305. struct drm_device *dev = crtc->base.dev;
  8306. struct drm_plane *primary = crtc->base.primary;
  8307. if (is_mmio_work(work))
  8308. flush_work(&work->mmio_work);
  8309. mutex_lock(&dev->struct_mutex);
  8310. intel_unpin_fb_vma(work->old_vma);
  8311. i915_gem_object_put(work->pending_flip_obj);
  8312. mutex_unlock(&dev->struct_mutex);
  8313. i915_gem_request_put(work->flip_queued_req);
  8314. intel_frontbuffer_flip_complete(to_i915(dev),
  8315. to_intel_plane(primary)->frontbuffer_bit);
  8316. intel_fbc_post_update(crtc);
  8317. drm_framebuffer_unreference(work->old_fb);
  8318. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8319. atomic_dec(&crtc->unpin_work_count);
  8320. kfree(work);
  8321. }
  8322. /* Is 'a' after or equal to 'b'? */
  8323. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8324. {
  8325. return !((a - b) & 0x80000000);
  8326. }
  8327. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8328. struct intel_flip_work *work)
  8329. {
  8330. struct drm_device *dev = crtc->base.dev;
  8331. struct drm_i915_private *dev_priv = to_i915(dev);
  8332. if (abort_flip_on_reset(crtc))
  8333. return true;
  8334. /*
  8335. * The relevant registers doen't exist on pre-ctg.
  8336. * As the flip done interrupt doesn't trigger for mmio
  8337. * flips on gmch platforms, a flip count check isn't
  8338. * really needed there. But since ctg has the registers,
  8339. * include it in the check anyway.
  8340. */
  8341. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8342. return true;
  8343. /*
  8344. * BDW signals flip done immediately if the plane
  8345. * is disabled, even if the plane enable is already
  8346. * armed to occur at the next vblank :(
  8347. */
  8348. /*
  8349. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8350. * used the same base address. In that case the mmio flip might
  8351. * have completed, but the CS hasn't even executed the flip yet.
  8352. *
  8353. * A flip count check isn't enough as the CS might have updated
  8354. * the base address just after start of vblank, but before we
  8355. * managed to process the interrupt. This means we'd complete the
  8356. * CS flip too soon.
  8357. *
  8358. * Combining both checks should get us a good enough result. It may
  8359. * still happen that the CS flip has been executed, but has not
  8360. * yet actually completed. But in case the base address is the same
  8361. * anyway, we don't really care.
  8362. */
  8363. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8364. crtc->flip_work->gtt_offset &&
  8365. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8366. crtc->flip_work->flip_count);
  8367. }
  8368. static bool
  8369. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8370. struct intel_flip_work *work)
  8371. {
  8372. /*
  8373. * MMIO work completes when vblank is different from
  8374. * flip_queued_vblank.
  8375. *
  8376. * Reset counter value doesn't matter, this is handled by
  8377. * i915_wait_request finishing early, so no need to handle
  8378. * reset here.
  8379. */
  8380. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8381. }
  8382. static bool pageflip_finished(struct intel_crtc *crtc,
  8383. struct intel_flip_work *work)
  8384. {
  8385. if (!atomic_read(&work->pending))
  8386. return false;
  8387. smp_rmb();
  8388. if (is_mmio_work(work))
  8389. return __pageflip_finished_mmio(crtc, work);
  8390. else
  8391. return __pageflip_finished_cs(crtc, work);
  8392. }
  8393. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8394. {
  8395. struct drm_device *dev = &dev_priv->drm;
  8396. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8397. struct intel_flip_work *work;
  8398. unsigned long flags;
  8399. /* Ignore early vblank irqs */
  8400. if (!crtc)
  8401. return;
  8402. /*
  8403. * This is called both by irq handlers and the reset code (to complete
  8404. * lost pageflips) so needs the full irqsave spinlocks.
  8405. */
  8406. spin_lock_irqsave(&dev->event_lock, flags);
  8407. work = crtc->flip_work;
  8408. if (work != NULL &&
  8409. !is_mmio_work(work) &&
  8410. pageflip_finished(crtc, work))
  8411. page_flip_completed(crtc);
  8412. spin_unlock_irqrestore(&dev->event_lock, flags);
  8413. }
  8414. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8415. {
  8416. struct drm_device *dev = &dev_priv->drm;
  8417. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8418. struct intel_flip_work *work;
  8419. unsigned long flags;
  8420. /* Ignore early vblank irqs */
  8421. if (!crtc)
  8422. return;
  8423. /*
  8424. * This is called both by irq handlers and the reset code (to complete
  8425. * lost pageflips) so needs the full irqsave spinlocks.
  8426. */
  8427. spin_lock_irqsave(&dev->event_lock, flags);
  8428. work = crtc->flip_work;
  8429. if (work != NULL &&
  8430. is_mmio_work(work) &&
  8431. pageflip_finished(crtc, work))
  8432. page_flip_completed(crtc);
  8433. spin_unlock_irqrestore(&dev->event_lock, flags);
  8434. }
  8435. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8436. struct intel_flip_work *work)
  8437. {
  8438. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8439. /* Ensure that the work item is consistent when activating it ... */
  8440. smp_mb__before_atomic();
  8441. atomic_set(&work->pending, 1);
  8442. }
  8443. static int intel_gen2_queue_flip(struct drm_device *dev,
  8444. struct drm_crtc *crtc,
  8445. struct drm_framebuffer *fb,
  8446. struct drm_i915_gem_object *obj,
  8447. struct drm_i915_gem_request *req,
  8448. uint32_t flags)
  8449. {
  8450. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8451. u32 flip_mask, *cs;
  8452. cs = intel_ring_begin(req, 6);
  8453. if (IS_ERR(cs))
  8454. return PTR_ERR(cs);
  8455. /* Can't queue multiple flips, so wait for the previous
  8456. * one to finish before executing the next.
  8457. */
  8458. if (intel_crtc->plane)
  8459. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8460. else
  8461. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8462. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8463. *cs++ = MI_NOOP;
  8464. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8465. *cs++ = fb->pitches[0];
  8466. *cs++ = intel_crtc->flip_work->gtt_offset;
  8467. *cs++ = 0; /* aux display base address, unused */
  8468. return 0;
  8469. }
  8470. static int intel_gen3_queue_flip(struct drm_device *dev,
  8471. struct drm_crtc *crtc,
  8472. struct drm_framebuffer *fb,
  8473. struct drm_i915_gem_object *obj,
  8474. struct drm_i915_gem_request *req,
  8475. uint32_t flags)
  8476. {
  8477. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8478. u32 flip_mask, *cs;
  8479. cs = intel_ring_begin(req, 6);
  8480. if (IS_ERR(cs))
  8481. return PTR_ERR(cs);
  8482. if (intel_crtc->plane)
  8483. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8484. else
  8485. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8486. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8487. *cs++ = MI_NOOP;
  8488. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8489. *cs++ = fb->pitches[0];
  8490. *cs++ = intel_crtc->flip_work->gtt_offset;
  8491. *cs++ = MI_NOOP;
  8492. return 0;
  8493. }
  8494. static int intel_gen4_queue_flip(struct drm_device *dev,
  8495. struct drm_crtc *crtc,
  8496. struct drm_framebuffer *fb,
  8497. struct drm_i915_gem_object *obj,
  8498. struct drm_i915_gem_request *req,
  8499. uint32_t flags)
  8500. {
  8501. struct drm_i915_private *dev_priv = to_i915(dev);
  8502. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8503. u32 pf, pipesrc, *cs;
  8504. cs = intel_ring_begin(req, 4);
  8505. if (IS_ERR(cs))
  8506. return PTR_ERR(cs);
  8507. /* i965+ uses the linear or tiled offsets from the
  8508. * Display Registers (which do not change across a page-flip)
  8509. * so we need only reprogram the base address.
  8510. */
  8511. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8512. *cs++ = fb->pitches[0];
  8513. *cs++ = intel_crtc->flip_work->gtt_offset |
  8514. intel_fb_modifier_to_tiling(fb->modifier);
  8515. /* XXX Enabling the panel-fitter across page-flip is so far
  8516. * untested on non-native modes, so ignore it for now.
  8517. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8518. */
  8519. pf = 0;
  8520. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8521. *cs++ = pf | pipesrc;
  8522. return 0;
  8523. }
  8524. static int intel_gen6_queue_flip(struct drm_device *dev,
  8525. struct drm_crtc *crtc,
  8526. struct drm_framebuffer *fb,
  8527. struct drm_i915_gem_object *obj,
  8528. struct drm_i915_gem_request *req,
  8529. uint32_t flags)
  8530. {
  8531. struct drm_i915_private *dev_priv = to_i915(dev);
  8532. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8533. u32 pf, pipesrc, *cs;
  8534. cs = intel_ring_begin(req, 4);
  8535. if (IS_ERR(cs))
  8536. return PTR_ERR(cs);
  8537. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8538. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8539. *cs++ = intel_crtc->flip_work->gtt_offset;
  8540. /* Contrary to the suggestions in the documentation,
  8541. * "Enable Panel Fitter" does not seem to be required when page
  8542. * flipping with a non-native mode, and worse causes a normal
  8543. * modeset to fail.
  8544. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8545. */
  8546. pf = 0;
  8547. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8548. *cs++ = pf | pipesrc;
  8549. return 0;
  8550. }
  8551. static int intel_gen7_queue_flip(struct drm_device *dev,
  8552. struct drm_crtc *crtc,
  8553. struct drm_framebuffer *fb,
  8554. struct drm_i915_gem_object *obj,
  8555. struct drm_i915_gem_request *req,
  8556. uint32_t flags)
  8557. {
  8558. struct drm_i915_private *dev_priv = to_i915(dev);
  8559. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8560. u32 *cs, plane_bit = 0;
  8561. int len, ret;
  8562. switch (intel_crtc->plane) {
  8563. case PLANE_A:
  8564. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8565. break;
  8566. case PLANE_B:
  8567. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8568. break;
  8569. case PLANE_C:
  8570. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8571. break;
  8572. default:
  8573. WARN_ONCE(1, "unknown plane in flip command\n");
  8574. return -ENODEV;
  8575. }
  8576. len = 4;
  8577. if (req->engine->id == RCS) {
  8578. len += 6;
  8579. /*
  8580. * On Gen 8, SRM is now taking an extra dword to accommodate
  8581. * 48bits addresses, and we need a NOOP for the batch size to
  8582. * stay even.
  8583. */
  8584. if (IS_GEN8(dev_priv))
  8585. len += 2;
  8586. }
  8587. /*
  8588. * BSpec MI_DISPLAY_FLIP for IVB:
  8589. * "The full packet must be contained within the same cache line."
  8590. *
  8591. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8592. * cacheline, if we ever start emitting more commands before
  8593. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8594. * then do the cacheline alignment, and finally emit the
  8595. * MI_DISPLAY_FLIP.
  8596. */
  8597. ret = intel_ring_cacheline_align(req);
  8598. if (ret)
  8599. return ret;
  8600. cs = intel_ring_begin(req, len);
  8601. if (IS_ERR(cs))
  8602. return PTR_ERR(cs);
  8603. /* Unmask the flip-done completion message. Note that the bspec says that
  8604. * we should do this for both the BCS and RCS, and that we must not unmask
  8605. * more than one flip event at any time (or ensure that one flip message
  8606. * can be sent by waiting for flip-done prior to queueing new flips).
  8607. * Experimentation says that BCS works despite DERRMR masking all
  8608. * flip-done completion events and that unmasking all planes at once
  8609. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8610. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8611. */
  8612. if (req->engine->id == RCS) {
  8613. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8614. *cs++ = i915_mmio_reg_offset(DERRMR);
  8615. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8616. DERRMR_PIPEB_PRI_FLIP_DONE |
  8617. DERRMR_PIPEC_PRI_FLIP_DONE);
  8618. if (IS_GEN8(dev_priv))
  8619. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8620. MI_SRM_LRM_GLOBAL_GTT;
  8621. else
  8622. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8623. *cs++ = i915_mmio_reg_offset(DERRMR);
  8624. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8625. if (IS_GEN8(dev_priv)) {
  8626. *cs++ = 0;
  8627. *cs++ = MI_NOOP;
  8628. }
  8629. }
  8630. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8631. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8632. *cs++ = intel_crtc->flip_work->gtt_offset;
  8633. *cs++ = MI_NOOP;
  8634. return 0;
  8635. }
  8636. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8637. struct drm_i915_gem_object *obj)
  8638. {
  8639. /*
  8640. * This is not being used for older platforms, because
  8641. * non-availability of flip done interrupt forces us to use
  8642. * CS flips. Older platforms derive flip done using some clever
  8643. * tricks involving the flip_pending status bits and vblank irqs.
  8644. * So using MMIO flips there would disrupt this mechanism.
  8645. */
  8646. if (engine == NULL)
  8647. return true;
  8648. if (INTEL_GEN(engine->i915) < 5)
  8649. return false;
  8650. if (i915.use_mmio_flip < 0)
  8651. return false;
  8652. else if (i915.use_mmio_flip > 0)
  8653. return true;
  8654. else if (i915.enable_execlists)
  8655. return true;
  8656. return engine != i915_gem_object_last_write_engine(obj);
  8657. }
  8658. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8659. unsigned int rotation,
  8660. struct intel_flip_work *work)
  8661. {
  8662. struct drm_device *dev = intel_crtc->base.dev;
  8663. struct drm_i915_private *dev_priv = to_i915(dev);
  8664. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8665. const enum pipe pipe = intel_crtc->pipe;
  8666. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8667. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8668. ctl &= ~PLANE_CTL_TILED_MASK;
  8669. switch (fb->modifier) {
  8670. case DRM_FORMAT_MOD_LINEAR:
  8671. break;
  8672. case I915_FORMAT_MOD_X_TILED:
  8673. ctl |= PLANE_CTL_TILED_X;
  8674. break;
  8675. case I915_FORMAT_MOD_Y_TILED:
  8676. ctl |= PLANE_CTL_TILED_Y;
  8677. break;
  8678. case I915_FORMAT_MOD_Yf_TILED:
  8679. ctl |= PLANE_CTL_TILED_YF;
  8680. break;
  8681. default:
  8682. MISSING_CASE(fb->modifier);
  8683. }
  8684. /*
  8685. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8686. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8687. */
  8688. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8689. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8690. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8691. POSTING_READ(PLANE_SURF(pipe, 0));
  8692. }
  8693. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8694. struct intel_flip_work *work)
  8695. {
  8696. struct drm_device *dev = intel_crtc->base.dev;
  8697. struct drm_i915_private *dev_priv = to_i915(dev);
  8698. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8699. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8700. u32 dspcntr;
  8701. dspcntr = I915_READ(reg);
  8702. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8703. dspcntr |= DISPPLANE_TILED;
  8704. else
  8705. dspcntr &= ~DISPPLANE_TILED;
  8706. I915_WRITE(reg, dspcntr);
  8707. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8708. POSTING_READ(DSPSURF(intel_crtc->plane));
  8709. }
  8710. static void intel_mmio_flip_work_func(struct work_struct *w)
  8711. {
  8712. struct intel_flip_work *work =
  8713. container_of(w, struct intel_flip_work, mmio_work);
  8714. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8715. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8716. struct intel_framebuffer *intel_fb =
  8717. to_intel_framebuffer(crtc->base.primary->fb);
  8718. struct drm_i915_gem_object *obj = intel_fb->obj;
  8719. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8720. intel_pipe_update_start(crtc);
  8721. if (INTEL_GEN(dev_priv) >= 9)
  8722. skl_do_mmio_flip(crtc, work->rotation, work);
  8723. else
  8724. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8725. ilk_do_mmio_flip(crtc, work);
  8726. intel_pipe_update_end(crtc, work);
  8727. }
  8728. static int intel_default_queue_flip(struct drm_device *dev,
  8729. struct drm_crtc *crtc,
  8730. struct drm_framebuffer *fb,
  8731. struct drm_i915_gem_object *obj,
  8732. struct drm_i915_gem_request *req,
  8733. uint32_t flags)
  8734. {
  8735. return -ENODEV;
  8736. }
  8737. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8738. struct intel_crtc *intel_crtc,
  8739. struct intel_flip_work *work)
  8740. {
  8741. u32 addr, vblank;
  8742. if (!atomic_read(&work->pending))
  8743. return false;
  8744. smp_rmb();
  8745. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8746. if (work->flip_ready_vblank == 0) {
  8747. if (work->flip_queued_req &&
  8748. !i915_gem_request_completed(work->flip_queued_req))
  8749. return false;
  8750. work->flip_ready_vblank = vblank;
  8751. }
  8752. if (vblank - work->flip_ready_vblank < 3)
  8753. return false;
  8754. /* Potential stall - if we see that the flip has happened,
  8755. * assume a missed interrupt. */
  8756. if (INTEL_GEN(dev_priv) >= 4)
  8757. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8758. else
  8759. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8760. /* There is a potential issue here with a false positive after a flip
  8761. * to the same address. We could address this by checking for a
  8762. * non-incrementing frame counter.
  8763. */
  8764. return addr == work->gtt_offset;
  8765. }
  8766. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8767. {
  8768. struct drm_device *dev = &dev_priv->drm;
  8769. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8770. struct intel_flip_work *work;
  8771. WARN_ON(!in_interrupt());
  8772. if (crtc == NULL)
  8773. return;
  8774. spin_lock(&dev->event_lock);
  8775. work = crtc->flip_work;
  8776. if (work != NULL && !is_mmio_work(work) &&
  8777. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8778. WARN_ONCE(1,
  8779. "Kicking stuck page flip: queued at %d, now %d\n",
  8780. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8781. page_flip_completed(crtc);
  8782. work = NULL;
  8783. }
  8784. if (work != NULL && !is_mmio_work(work) &&
  8785. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8786. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8787. spin_unlock(&dev->event_lock);
  8788. }
  8789. __maybe_unused
  8790. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8791. struct drm_framebuffer *fb,
  8792. struct drm_pending_vblank_event *event,
  8793. uint32_t page_flip_flags)
  8794. {
  8795. struct drm_device *dev = crtc->dev;
  8796. struct drm_i915_private *dev_priv = to_i915(dev);
  8797. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8798. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8799. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8800. struct drm_plane *primary = crtc->primary;
  8801. enum pipe pipe = intel_crtc->pipe;
  8802. struct intel_flip_work *work;
  8803. struct intel_engine_cs *engine;
  8804. bool mmio_flip;
  8805. struct drm_i915_gem_request *request;
  8806. struct i915_vma *vma;
  8807. int ret;
  8808. /*
  8809. * drm_mode_page_flip_ioctl() should already catch this, but double
  8810. * check to be safe. In the future we may enable pageflipping from
  8811. * a disabled primary plane.
  8812. */
  8813. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8814. return -EBUSY;
  8815. /* Can't change pixel format via MI display flips. */
  8816. if (fb->format != crtc->primary->fb->format)
  8817. return -EINVAL;
  8818. /*
  8819. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8820. * Note that pitch changes could also affect these register.
  8821. */
  8822. if (INTEL_GEN(dev_priv) > 3 &&
  8823. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8824. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8825. return -EINVAL;
  8826. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8827. goto out_hang;
  8828. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8829. if (work == NULL)
  8830. return -ENOMEM;
  8831. work->event = event;
  8832. work->crtc = crtc;
  8833. work->old_fb = old_fb;
  8834. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8835. ret = drm_crtc_vblank_get(crtc);
  8836. if (ret)
  8837. goto free_work;
  8838. /* We borrow the event spin lock for protecting flip_work */
  8839. spin_lock_irq(&dev->event_lock);
  8840. if (intel_crtc->flip_work) {
  8841. /* Before declaring the flip queue wedged, check if
  8842. * the hardware completed the operation behind our backs.
  8843. */
  8844. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8845. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8846. page_flip_completed(intel_crtc);
  8847. } else {
  8848. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8849. spin_unlock_irq(&dev->event_lock);
  8850. drm_crtc_vblank_put(crtc);
  8851. kfree(work);
  8852. return -EBUSY;
  8853. }
  8854. }
  8855. intel_crtc->flip_work = work;
  8856. spin_unlock_irq(&dev->event_lock);
  8857. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8858. flush_workqueue(dev_priv->wq);
  8859. /* Reference the objects for the scheduled work. */
  8860. drm_framebuffer_reference(work->old_fb);
  8861. crtc->primary->fb = fb;
  8862. update_state_fb(crtc->primary);
  8863. work->pending_flip_obj = i915_gem_object_get(obj);
  8864. ret = i915_mutex_lock_interruptible(dev);
  8865. if (ret)
  8866. goto cleanup;
  8867. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  8868. if (i915_reset_backoff_or_wedged(&dev_priv->gpu_error)) {
  8869. ret = -EIO;
  8870. goto unlock;
  8871. }
  8872. atomic_inc(&intel_crtc->unpin_work_count);
  8873. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8874. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  8875. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8876. engine = dev_priv->engine[BCS];
  8877. if (fb->modifier != old_fb->modifier)
  8878. /* vlv: DISPLAY_FLIP fails to change tiling */
  8879. engine = NULL;
  8880. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  8881. engine = dev_priv->engine[BCS];
  8882. } else if (INTEL_GEN(dev_priv) >= 7) {
  8883. engine = i915_gem_object_last_write_engine(obj);
  8884. if (engine == NULL || engine->id != RCS)
  8885. engine = dev_priv->engine[BCS];
  8886. } else {
  8887. engine = dev_priv->engine[RCS];
  8888. }
  8889. mmio_flip = use_mmio_flip(engine, obj);
  8890. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  8891. if (IS_ERR(vma)) {
  8892. ret = PTR_ERR(vma);
  8893. goto cleanup_pending;
  8894. }
  8895. work->old_vma = to_intel_plane_state(primary->state)->vma;
  8896. to_intel_plane_state(primary->state)->vma = vma;
  8897. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  8898. work->rotation = crtc->primary->state->rotation;
  8899. /*
  8900. * There's the potential that the next frame will not be compatible with
  8901. * FBC, so we want to call pre_update() before the actual page flip.
  8902. * The problem is that pre_update() caches some information about the fb
  8903. * object, so we want to do this only after the object is pinned. Let's
  8904. * be on the safe side and do this immediately before scheduling the
  8905. * flip.
  8906. */
  8907. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  8908. to_intel_plane_state(primary->state));
  8909. if (mmio_flip) {
  8910. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  8911. queue_work(system_unbound_wq, &work->mmio_work);
  8912. } else {
  8913. request = i915_gem_request_alloc(engine,
  8914. dev_priv->kernel_context);
  8915. if (IS_ERR(request)) {
  8916. ret = PTR_ERR(request);
  8917. goto cleanup_unpin;
  8918. }
  8919. ret = i915_gem_request_await_object(request, obj, false);
  8920. if (ret)
  8921. goto cleanup_request;
  8922. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  8923. page_flip_flags);
  8924. if (ret)
  8925. goto cleanup_request;
  8926. intel_mark_page_flip_active(intel_crtc, work);
  8927. work->flip_queued_req = i915_gem_request_get(request);
  8928. i915_add_request(request);
  8929. }
  8930. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  8931. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  8932. to_intel_plane(primary)->frontbuffer_bit);
  8933. mutex_unlock(&dev->struct_mutex);
  8934. intel_frontbuffer_flip_prepare(to_i915(dev),
  8935. to_intel_plane(primary)->frontbuffer_bit);
  8936. trace_i915_flip_request(intel_crtc->plane, obj);
  8937. return 0;
  8938. cleanup_request:
  8939. i915_add_request(request);
  8940. cleanup_unpin:
  8941. to_intel_plane_state(primary->state)->vma = work->old_vma;
  8942. intel_unpin_fb_vma(vma);
  8943. cleanup_pending:
  8944. atomic_dec(&intel_crtc->unpin_work_count);
  8945. unlock:
  8946. mutex_unlock(&dev->struct_mutex);
  8947. cleanup:
  8948. crtc->primary->fb = old_fb;
  8949. update_state_fb(crtc->primary);
  8950. i915_gem_object_put(obj);
  8951. drm_framebuffer_unreference(work->old_fb);
  8952. spin_lock_irq(&dev->event_lock);
  8953. intel_crtc->flip_work = NULL;
  8954. spin_unlock_irq(&dev->event_lock);
  8955. drm_crtc_vblank_put(crtc);
  8956. free_work:
  8957. kfree(work);
  8958. if (ret == -EIO) {
  8959. struct drm_atomic_state *state;
  8960. struct drm_plane_state *plane_state;
  8961. out_hang:
  8962. state = drm_atomic_state_alloc(dev);
  8963. if (!state)
  8964. return -ENOMEM;
  8965. state->acquire_ctx = dev->mode_config.acquire_ctx;
  8966. retry:
  8967. plane_state = drm_atomic_get_plane_state(state, primary);
  8968. ret = PTR_ERR_OR_ZERO(plane_state);
  8969. if (!ret) {
  8970. drm_atomic_set_fb_for_plane(plane_state, fb);
  8971. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  8972. if (!ret)
  8973. ret = drm_atomic_commit(state);
  8974. }
  8975. if (ret == -EDEADLK) {
  8976. drm_modeset_backoff(state->acquire_ctx);
  8977. drm_atomic_state_clear(state);
  8978. goto retry;
  8979. }
  8980. drm_atomic_state_put(state);
  8981. if (ret == 0 && event) {
  8982. spin_lock_irq(&dev->event_lock);
  8983. drm_crtc_send_vblank_event(crtc, event);
  8984. spin_unlock_irq(&dev->event_lock);
  8985. }
  8986. }
  8987. return ret;
  8988. }
  8989. /**
  8990. * intel_wm_need_update - Check whether watermarks need updating
  8991. * @plane: drm plane
  8992. * @state: new plane state
  8993. *
  8994. * Check current plane state versus the new one to determine whether
  8995. * watermarks need to be recalculated.
  8996. *
  8997. * Returns true or false.
  8998. */
  8999. static bool intel_wm_need_update(struct drm_plane *plane,
  9000. struct drm_plane_state *state)
  9001. {
  9002. struct intel_plane_state *new = to_intel_plane_state(state);
  9003. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  9004. /* Update watermarks on tiling or size changes. */
  9005. if (new->base.visible != cur->base.visible)
  9006. return true;
  9007. if (!cur->base.fb || !new->base.fb)
  9008. return false;
  9009. if (cur->base.fb->modifier != new->base.fb->modifier ||
  9010. cur->base.rotation != new->base.rotation ||
  9011. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  9012. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  9013. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  9014. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  9015. return true;
  9016. return false;
  9017. }
  9018. static bool needs_scaling(struct intel_plane_state *state)
  9019. {
  9020. int src_w = drm_rect_width(&state->base.src) >> 16;
  9021. int src_h = drm_rect_height(&state->base.src) >> 16;
  9022. int dst_w = drm_rect_width(&state->base.dst);
  9023. int dst_h = drm_rect_height(&state->base.dst);
  9024. return (src_w != dst_w || src_h != dst_h);
  9025. }
  9026. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9027. struct drm_plane_state *plane_state)
  9028. {
  9029. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9030. struct drm_crtc *crtc = crtc_state->crtc;
  9031. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9032. struct intel_plane *plane = to_intel_plane(plane_state->plane);
  9033. struct drm_device *dev = crtc->dev;
  9034. struct drm_i915_private *dev_priv = to_i915(dev);
  9035. struct intel_plane_state *old_plane_state =
  9036. to_intel_plane_state(plane->base.state);
  9037. bool mode_changed = needs_modeset(crtc_state);
  9038. bool was_crtc_enabled = crtc->state->active;
  9039. bool is_crtc_enabled = crtc_state->active;
  9040. bool turn_off, turn_on, visible, was_visible;
  9041. struct drm_framebuffer *fb = plane_state->fb;
  9042. int ret;
  9043. if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
  9044. ret = skl_update_scaler_plane(
  9045. to_intel_crtc_state(crtc_state),
  9046. to_intel_plane_state(plane_state));
  9047. if (ret)
  9048. return ret;
  9049. }
  9050. was_visible = old_plane_state->base.visible;
  9051. visible = plane_state->visible;
  9052. if (!was_crtc_enabled && WARN_ON(was_visible))
  9053. was_visible = false;
  9054. /*
  9055. * Visibility is calculated as if the crtc was on, but
  9056. * after scaler setup everything depends on it being off
  9057. * when the crtc isn't active.
  9058. *
  9059. * FIXME this is wrong for watermarks. Watermarks should also
  9060. * be computed as if the pipe would be active. Perhaps move
  9061. * per-plane wm computation to the .check_plane() hook, and
  9062. * only combine the results from all planes in the current place?
  9063. */
  9064. if (!is_crtc_enabled) {
  9065. plane_state->visible = visible = false;
  9066. to_intel_crtc_state(crtc_state)->active_planes &= ~BIT(plane->id);
  9067. }
  9068. if (!was_visible && !visible)
  9069. return 0;
  9070. if (fb != old_plane_state->base.fb)
  9071. pipe_config->fb_changed = true;
  9072. turn_off = was_visible && (!visible || mode_changed);
  9073. turn_on = visible && (!was_visible || mode_changed);
  9074. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9075. intel_crtc->base.base.id, intel_crtc->base.name,
  9076. plane->base.base.id, plane->base.name,
  9077. fb ? fb->base.id : -1);
  9078. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9079. plane->base.base.id, plane->base.name,
  9080. was_visible, visible,
  9081. turn_off, turn_on, mode_changed);
  9082. if (turn_on) {
  9083. if (INTEL_GEN(dev_priv) < 5)
  9084. pipe_config->update_wm_pre = true;
  9085. /* must disable cxsr around plane enable/disable */
  9086. if (plane->id != PLANE_CURSOR)
  9087. pipe_config->disable_cxsr = true;
  9088. } else if (turn_off) {
  9089. if (INTEL_GEN(dev_priv) < 5)
  9090. pipe_config->update_wm_post = true;
  9091. /* must disable cxsr around plane enable/disable */
  9092. if (plane->id != PLANE_CURSOR)
  9093. pipe_config->disable_cxsr = true;
  9094. } else if (intel_wm_need_update(&plane->base, plane_state)) {
  9095. if (INTEL_GEN(dev_priv) < 5) {
  9096. /* FIXME bollocks */
  9097. pipe_config->update_wm_pre = true;
  9098. pipe_config->update_wm_post = true;
  9099. }
  9100. }
  9101. if (visible || was_visible)
  9102. pipe_config->fb_bits |= plane->frontbuffer_bit;
  9103. /*
  9104. * WaCxSRDisabledForSpriteScaling:ivb
  9105. *
  9106. * cstate->update_wm was already set above, so this flag will
  9107. * take effect when we commit and program watermarks.
  9108. */
  9109. if (plane->id == PLANE_SPRITE0 && IS_IVYBRIDGE(dev_priv) &&
  9110. needs_scaling(to_intel_plane_state(plane_state)) &&
  9111. !needs_scaling(old_plane_state))
  9112. pipe_config->disable_lp_wm = true;
  9113. return 0;
  9114. }
  9115. static bool encoders_cloneable(const struct intel_encoder *a,
  9116. const struct intel_encoder *b)
  9117. {
  9118. /* masks could be asymmetric, so check both ways */
  9119. return a == b || (a->cloneable & (1 << b->type) &&
  9120. b->cloneable & (1 << a->type));
  9121. }
  9122. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9123. struct intel_crtc *crtc,
  9124. struct intel_encoder *encoder)
  9125. {
  9126. struct intel_encoder *source_encoder;
  9127. struct drm_connector *connector;
  9128. struct drm_connector_state *connector_state;
  9129. int i;
  9130. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9131. if (connector_state->crtc != &crtc->base)
  9132. continue;
  9133. source_encoder =
  9134. to_intel_encoder(connector_state->best_encoder);
  9135. if (!encoders_cloneable(encoder, source_encoder))
  9136. return false;
  9137. }
  9138. return true;
  9139. }
  9140. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9141. struct drm_crtc_state *crtc_state)
  9142. {
  9143. struct drm_device *dev = crtc->dev;
  9144. struct drm_i915_private *dev_priv = to_i915(dev);
  9145. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9146. struct intel_crtc_state *pipe_config =
  9147. to_intel_crtc_state(crtc_state);
  9148. struct drm_atomic_state *state = crtc_state->state;
  9149. int ret;
  9150. bool mode_changed = needs_modeset(crtc_state);
  9151. if (mode_changed && !crtc_state->active)
  9152. pipe_config->update_wm_post = true;
  9153. if (mode_changed && crtc_state->enable &&
  9154. dev_priv->display.crtc_compute_clock &&
  9155. !WARN_ON(pipe_config->shared_dpll)) {
  9156. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9157. pipe_config);
  9158. if (ret)
  9159. return ret;
  9160. }
  9161. if (crtc_state->color_mgmt_changed) {
  9162. ret = intel_color_check(crtc, crtc_state);
  9163. if (ret)
  9164. return ret;
  9165. /*
  9166. * Changing color management on Intel hardware is
  9167. * handled as part of planes update.
  9168. */
  9169. crtc_state->planes_changed = true;
  9170. }
  9171. ret = 0;
  9172. if (dev_priv->display.compute_pipe_wm) {
  9173. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9174. if (ret) {
  9175. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9176. return ret;
  9177. }
  9178. }
  9179. if (dev_priv->display.compute_intermediate_wm &&
  9180. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9181. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9182. return 0;
  9183. /*
  9184. * Calculate 'intermediate' watermarks that satisfy both the
  9185. * old state and the new state. We can program these
  9186. * immediately.
  9187. */
  9188. ret = dev_priv->display.compute_intermediate_wm(dev,
  9189. intel_crtc,
  9190. pipe_config);
  9191. if (ret) {
  9192. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9193. return ret;
  9194. }
  9195. } else if (dev_priv->display.compute_intermediate_wm) {
  9196. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9197. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9198. }
  9199. if (INTEL_GEN(dev_priv) >= 9) {
  9200. if (mode_changed)
  9201. ret = skl_update_scaler_crtc(pipe_config);
  9202. if (!ret)
  9203. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9204. pipe_config);
  9205. }
  9206. return ret;
  9207. }
  9208. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9209. .atomic_begin = intel_begin_crtc_commit,
  9210. .atomic_flush = intel_finish_crtc_commit,
  9211. .atomic_check = intel_crtc_atomic_check,
  9212. };
  9213. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9214. {
  9215. struct intel_connector *connector;
  9216. struct drm_connector_list_iter conn_iter;
  9217. drm_connector_list_iter_begin(dev, &conn_iter);
  9218. for_each_intel_connector_iter(connector, &conn_iter) {
  9219. if (connector->base.state->crtc)
  9220. drm_connector_unreference(&connector->base);
  9221. if (connector->base.encoder) {
  9222. connector->base.state->best_encoder =
  9223. connector->base.encoder;
  9224. connector->base.state->crtc =
  9225. connector->base.encoder->crtc;
  9226. drm_connector_reference(&connector->base);
  9227. } else {
  9228. connector->base.state->best_encoder = NULL;
  9229. connector->base.state->crtc = NULL;
  9230. }
  9231. }
  9232. drm_connector_list_iter_end(&conn_iter);
  9233. }
  9234. static void
  9235. connected_sink_compute_bpp(struct intel_connector *connector,
  9236. struct intel_crtc_state *pipe_config)
  9237. {
  9238. const struct drm_display_info *info = &connector->base.display_info;
  9239. int bpp = pipe_config->pipe_bpp;
  9240. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9241. connector->base.base.id,
  9242. connector->base.name);
  9243. /* Don't use an invalid EDID bpc value */
  9244. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9245. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9246. bpp, info->bpc * 3);
  9247. pipe_config->pipe_bpp = info->bpc * 3;
  9248. }
  9249. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9250. if (info->bpc == 0 && bpp > 24) {
  9251. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9252. bpp);
  9253. pipe_config->pipe_bpp = 24;
  9254. }
  9255. }
  9256. static int
  9257. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9258. struct intel_crtc_state *pipe_config)
  9259. {
  9260. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9261. struct drm_atomic_state *state;
  9262. struct drm_connector *connector;
  9263. struct drm_connector_state *connector_state;
  9264. int bpp, i;
  9265. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9266. IS_CHERRYVIEW(dev_priv)))
  9267. bpp = 10*3;
  9268. else if (INTEL_GEN(dev_priv) >= 5)
  9269. bpp = 12*3;
  9270. else
  9271. bpp = 8*3;
  9272. pipe_config->pipe_bpp = bpp;
  9273. state = pipe_config->base.state;
  9274. /* Clamp display bpp to EDID value */
  9275. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9276. if (connector_state->crtc != &crtc->base)
  9277. continue;
  9278. connected_sink_compute_bpp(to_intel_connector(connector),
  9279. pipe_config);
  9280. }
  9281. return bpp;
  9282. }
  9283. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9284. {
  9285. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9286. "type: 0x%x flags: 0x%x\n",
  9287. mode->crtc_clock,
  9288. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9289. mode->crtc_hsync_end, mode->crtc_htotal,
  9290. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9291. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9292. }
  9293. static inline void
  9294. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9295. unsigned int lane_count, struct intel_link_m_n *m_n)
  9296. {
  9297. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9298. id, lane_count,
  9299. m_n->gmch_m, m_n->gmch_n,
  9300. m_n->link_m, m_n->link_n, m_n->tu);
  9301. }
  9302. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9303. struct intel_crtc_state *pipe_config,
  9304. const char *context)
  9305. {
  9306. struct drm_device *dev = crtc->base.dev;
  9307. struct drm_i915_private *dev_priv = to_i915(dev);
  9308. struct drm_plane *plane;
  9309. struct intel_plane *intel_plane;
  9310. struct intel_plane_state *state;
  9311. struct drm_framebuffer *fb;
  9312. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9313. crtc->base.base.id, crtc->base.name, context);
  9314. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9315. transcoder_name(pipe_config->cpu_transcoder),
  9316. pipe_config->pipe_bpp, pipe_config->dither);
  9317. if (pipe_config->has_pch_encoder)
  9318. intel_dump_m_n_config(pipe_config, "fdi",
  9319. pipe_config->fdi_lanes,
  9320. &pipe_config->fdi_m_n);
  9321. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9322. intel_dump_m_n_config(pipe_config, "dp m_n",
  9323. pipe_config->lane_count, &pipe_config->dp_m_n);
  9324. if (pipe_config->has_drrs)
  9325. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9326. pipe_config->lane_count,
  9327. &pipe_config->dp_m2_n2);
  9328. }
  9329. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9330. pipe_config->has_audio, pipe_config->has_infoframe);
  9331. DRM_DEBUG_KMS("requested mode:\n");
  9332. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9333. DRM_DEBUG_KMS("adjusted mode:\n");
  9334. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9335. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9336. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9337. pipe_config->port_clock,
  9338. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9339. pipe_config->pixel_rate);
  9340. if (INTEL_GEN(dev_priv) >= 9)
  9341. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9342. crtc->num_scalers,
  9343. pipe_config->scaler_state.scaler_users,
  9344. pipe_config->scaler_state.scaler_id);
  9345. if (HAS_GMCH_DISPLAY(dev_priv))
  9346. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9347. pipe_config->gmch_pfit.control,
  9348. pipe_config->gmch_pfit.pgm_ratios,
  9349. pipe_config->gmch_pfit.lvds_border_bits);
  9350. else
  9351. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9352. pipe_config->pch_pfit.pos,
  9353. pipe_config->pch_pfit.size,
  9354. enableddisabled(pipe_config->pch_pfit.enabled));
  9355. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9356. pipe_config->ips_enabled, pipe_config->double_wide);
  9357. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9358. DRM_DEBUG_KMS("planes on this crtc\n");
  9359. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9360. struct drm_format_name_buf format_name;
  9361. intel_plane = to_intel_plane(plane);
  9362. if (intel_plane->pipe != crtc->pipe)
  9363. continue;
  9364. state = to_intel_plane_state(plane->state);
  9365. fb = state->base.fb;
  9366. if (!fb) {
  9367. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9368. plane->base.id, plane->name, state->scaler_id);
  9369. continue;
  9370. }
  9371. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9372. plane->base.id, plane->name,
  9373. fb->base.id, fb->width, fb->height,
  9374. drm_get_format_name(fb->format->format, &format_name));
  9375. if (INTEL_GEN(dev_priv) >= 9)
  9376. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9377. state->scaler_id,
  9378. state->base.src.x1 >> 16,
  9379. state->base.src.y1 >> 16,
  9380. drm_rect_width(&state->base.src) >> 16,
  9381. drm_rect_height(&state->base.src) >> 16,
  9382. state->base.dst.x1, state->base.dst.y1,
  9383. drm_rect_width(&state->base.dst),
  9384. drm_rect_height(&state->base.dst));
  9385. }
  9386. }
  9387. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9388. {
  9389. struct drm_device *dev = state->dev;
  9390. struct drm_connector *connector;
  9391. unsigned int used_ports = 0;
  9392. unsigned int used_mst_ports = 0;
  9393. /*
  9394. * Walk the connector list instead of the encoder
  9395. * list to detect the problem on ddi platforms
  9396. * where there's just one encoder per digital port.
  9397. */
  9398. drm_for_each_connector(connector, dev) {
  9399. struct drm_connector_state *connector_state;
  9400. struct intel_encoder *encoder;
  9401. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9402. if (!connector_state)
  9403. connector_state = connector->state;
  9404. if (!connector_state->best_encoder)
  9405. continue;
  9406. encoder = to_intel_encoder(connector_state->best_encoder);
  9407. WARN_ON(!connector_state->crtc);
  9408. switch (encoder->type) {
  9409. unsigned int port_mask;
  9410. case INTEL_OUTPUT_UNKNOWN:
  9411. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9412. break;
  9413. case INTEL_OUTPUT_DP:
  9414. case INTEL_OUTPUT_HDMI:
  9415. case INTEL_OUTPUT_EDP:
  9416. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9417. /* the same port mustn't appear more than once */
  9418. if (used_ports & port_mask)
  9419. return false;
  9420. used_ports |= port_mask;
  9421. break;
  9422. case INTEL_OUTPUT_DP_MST:
  9423. used_mst_ports |=
  9424. 1 << enc_to_mst(&encoder->base)->primary->port;
  9425. break;
  9426. default:
  9427. break;
  9428. }
  9429. }
  9430. /* can't mix MST and SST/HDMI on the same port */
  9431. if (used_ports & used_mst_ports)
  9432. return false;
  9433. return true;
  9434. }
  9435. static void
  9436. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9437. {
  9438. struct drm_i915_private *dev_priv =
  9439. to_i915(crtc_state->base.crtc->dev);
  9440. struct intel_crtc_scaler_state scaler_state;
  9441. struct intel_dpll_hw_state dpll_hw_state;
  9442. struct intel_shared_dpll *shared_dpll;
  9443. struct intel_crtc_wm_state wm_state;
  9444. bool force_thru;
  9445. /* FIXME: before the switch to atomic started, a new pipe_config was
  9446. * kzalloc'd. Code that depends on any field being zero should be
  9447. * fixed, so that the crtc_state can be safely duplicated. For now,
  9448. * only fields that are know to not cause problems are preserved. */
  9449. scaler_state = crtc_state->scaler_state;
  9450. shared_dpll = crtc_state->shared_dpll;
  9451. dpll_hw_state = crtc_state->dpll_hw_state;
  9452. force_thru = crtc_state->pch_pfit.force_thru;
  9453. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9454. wm_state = crtc_state->wm;
  9455. /* Keep base drm_crtc_state intact, only clear our extended struct */
  9456. BUILD_BUG_ON(offsetof(struct intel_crtc_state, base));
  9457. memset(&crtc_state->base + 1, 0,
  9458. sizeof(*crtc_state) - sizeof(crtc_state->base));
  9459. crtc_state->scaler_state = scaler_state;
  9460. crtc_state->shared_dpll = shared_dpll;
  9461. crtc_state->dpll_hw_state = dpll_hw_state;
  9462. crtc_state->pch_pfit.force_thru = force_thru;
  9463. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9464. crtc_state->wm = wm_state;
  9465. }
  9466. static int
  9467. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9468. struct intel_crtc_state *pipe_config)
  9469. {
  9470. struct drm_atomic_state *state = pipe_config->base.state;
  9471. struct intel_encoder *encoder;
  9472. struct drm_connector *connector;
  9473. struct drm_connector_state *connector_state;
  9474. int base_bpp, ret = -EINVAL;
  9475. int i;
  9476. bool retry = true;
  9477. clear_intel_crtc_state(pipe_config);
  9478. pipe_config->cpu_transcoder =
  9479. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9480. /*
  9481. * Sanitize sync polarity flags based on requested ones. If neither
  9482. * positive or negative polarity is requested, treat this as meaning
  9483. * negative polarity.
  9484. */
  9485. if (!(pipe_config->base.adjusted_mode.flags &
  9486. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9487. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9488. if (!(pipe_config->base.adjusted_mode.flags &
  9489. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9490. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9491. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9492. pipe_config);
  9493. if (base_bpp < 0)
  9494. goto fail;
  9495. /*
  9496. * Determine the real pipe dimensions. Note that stereo modes can
  9497. * increase the actual pipe size due to the frame doubling and
  9498. * insertion of additional space for blanks between the frame. This
  9499. * is stored in the crtc timings. We use the requested mode to do this
  9500. * computation to clearly distinguish it from the adjusted mode, which
  9501. * can be changed by the connectors in the below retry loop.
  9502. */
  9503. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9504. &pipe_config->pipe_src_w,
  9505. &pipe_config->pipe_src_h);
  9506. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9507. if (connector_state->crtc != crtc)
  9508. continue;
  9509. encoder = to_intel_encoder(connector_state->best_encoder);
  9510. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9511. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9512. goto fail;
  9513. }
  9514. /*
  9515. * Determine output_types before calling the .compute_config()
  9516. * hooks so that the hooks can use this information safely.
  9517. */
  9518. pipe_config->output_types |= 1 << encoder->type;
  9519. }
  9520. encoder_retry:
  9521. /* Ensure the port clock defaults are reset when retrying. */
  9522. pipe_config->port_clock = 0;
  9523. pipe_config->pixel_multiplier = 1;
  9524. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9525. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9526. CRTC_STEREO_DOUBLE);
  9527. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9528. * adjust it according to limitations or connector properties, and also
  9529. * a chance to reject the mode entirely.
  9530. */
  9531. for_each_new_connector_in_state(state, connector, connector_state, i) {
  9532. if (connector_state->crtc != crtc)
  9533. continue;
  9534. encoder = to_intel_encoder(connector_state->best_encoder);
  9535. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9536. DRM_DEBUG_KMS("Encoder config failure\n");
  9537. goto fail;
  9538. }
  9539. }
  9540. /* Set default port clock if not overwritten by the encoder. Needs to be
  9541. * done afterwards in case the encoder adjusts the mode. */
  9542. if (!pipe_config->port_clock)
  9543. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9544. * pipe_config->pixel_multiplier;
  9545. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9546. if (ret < 0) {
  9547. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9548. goto fail;
  9549. }
  9550. if (ret == RETRY) {
  9551. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9552. ret = -EINVAL;
  9553. goto fail;
  9554. }
  9555. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9556. retry = false;
  9557. goto encoder_retry;
  9558. }
  9559. /* Dithering seems to not pass-through bits correctly when it should, so
  9560. * only enable it on 6bpc panels and when its not a compliance
  9561. * test requesting 6bpc video pattern.
  9562. */
  9563. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9564. !pipe_config->dither_force_disable;
  9565. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9566. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9567. fail:
  9568. return ret;
  9569. }
  9570. static void
  9571. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9572. {
  9573. struct drm_crtc *crtc;
  9574. struct drm_crtc_state *new_crtc_state;
  9575. int i;
  9576. /* Double check state. */
  9577. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  9578. to_intel_crtc(crtc)->config = to_intel_crtc_state(new_crtc_state);
  9579. /* Update hwmode for vblank functions */
  9580. if (new_crtc_state->active)
  9581. crtc->hwmode = new_crtc_state->adjusted_mode;
  9582. else
  9583. crtc->hwmode.crtc_clock = 0;
  9584. /*
  9585. * Update legacy state to satisfy fbc code. This can
  9586. * be removed when fbc uses the atomic state.
  9587. */
  9588. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9589. struct drm_plane_state *plane_state = crtc->primary->state;
  9590. crtc->primary->fb = plane_state->fb;
  9591. crtc->x = plane_state->src_x >> 16;
  9592. crtc->y = plane_state->src_y >> 16;
  9593. }
  9594. }
  9595. }
  9596. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9597. {
  9598. int diff;
  9599. if (clock1 == clock2)
  9600. return true;
  9601. if (!clock1 || !clock2)
  9602. return false;
  9603. diff = abs(clock1 - clock2);
  9604. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9605. return true;
  9606. return false;
  9607. }
  9608. static bool
  9609. intel_compare_m_n(unsigned int m, unsigned int n,
  9610. unsigned int m2, unsigned int n2,
  9611. bool exact)
  9612. {
  9613. if (m == m2 && n == n2)
  9614. return true;
  9615. if (exact || !m || !n || !m2 || !n2)
  9616. return false;
  9617. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9618. if (n > n2) {
  9619. while (n > n2) {
  9620. m2 <<= 1;
  9621. n2 <<= 1;
  9622. }
  9623. } else if (n < n2) {
  9624. while (n < n2) {
  9625. m <<= 1;
  9626. n <<= 1;
  9627. }
  9628. }
  9629. if (n != n2)
  9630. return false;
  9631. return intel_fuzzy_clock_check(m, m2);
  9632. }
  9633. static bool
  9634. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9635. struct intel_link_m_n *m2_n2,
  9636. bool adjust)
  9637. {
  9638. if (m_n->tu == m2_n2->tu &&
  9639. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9640. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9641. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9642. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9643. if (adjust)
  9644. *m2_n2 = *m_n;
  9645. return true;
  9646. }
  9647. return false;
  9648. }
  9649. static void __printf(3, 4)
  9650. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9651. {
  9652. char *level;
  9653. unsigned int category;
  9654. struct va_format vaf;
  9655. va_list args;
  9656. if (adjust) {
  9657. level = KERN_DEBUG;
  9658. category = DRM_UT_KMS;
  9659. } else {
  9660. level = KERN_ERR;
  9661. category = DRM_UT_NONE;
  9662. }
  9663. va_start(args, format);
  9664. vaf.fmt = format;
  9665. vaf.va = &args;
  9666. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9667. va_end(args);
  9668. }
  9669. static bool
  9670. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9671. struct intel_crtc_state *current_config,
  9672. struct intel_crtc_state *pipe_config,
  9673. bool adjust)
  9674. {
  9675. bool ret = true;
  9676. #define PIPE_CONF_CHECK_X(name) \
  9677. if (current_config->name != pipe_config->name) { \
  9678. pipe_config_err(adjust, __stringify(name), \
  9679. "(expected 0x%08x, found 0x%08x)\n", \
  9680. current_config->name, \
  9681. pipe_config->name); \
  9682. ret = false; \
  9683. }
  9684. #define PIPE_CONF_CHECK_I(name) \
  9685. if (current_config->name != pipe_config->name) { \
  9686. pipe_config_err(adjust, __stringify(name), \
  9687. "(expected %i, found %i)\n", \
  9688. current_config->name, \
  9689. pipe_config->name); \
  9690. ret = false; \
  9691. }
  9692. #define PIPE_CONF_CHECK_P(name) \
  9693. if (current_config->name != pipe_config->name) { \
  9694. pipe_config_err(adjust, __stringify(name), \
  9695. "(expected %p, found %p)\n", \
  9696. current_config->name, \
  9697. pipe_config->name); \
  9698. ret = false; \
  9699. }
  9700. #define PIPE_CONF_CHECK_M_N(name) \
  9701. if (!intel_compare_link_m_n(&current_config->name, \
  9702. &pipe_config->name,\
  9703. adjust)) { \
  9704. pipe_config_err(adjust, __stringify(name), \
  9705. "(expected tu %i gmch %i/%i link %i/%i, " \
  9706. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9707. current_config->name.tu, \
  9708. current_config->name.gmch_m, \
  9709. current_config->name.gmch_n, \
  9710. current_config->name.link_m, \
  9711. current_config->name.link_n, \
  9712. pipe_config->name.tu, \
  9713. pipe_config->name.gmch_m, \
  9714. pipe_config->name.gmch_n, \
  9715. pipe_config->name.link_m, \
  9716. pipe_config->name.link_n); \
  9717. ret = false; \
  9718. }
  9719. /* This is required for BDW+ where there is only one set of registers for
  9720. * switching between high and low RR.
  9721. * This macro can be used whenever a comparison has to be made between one
  9722. * hw state and multiple sw state variables.
  9723. */
  9724. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9725. if (!intel_compare_link_m_n(&current_config->name, \
  9726. &pipe_config->name, adjust) && \
  9727. !intel_compare_link_m_n(&current_config->alt_name, \
  9728. &pipe_config->name, adjust)) { \
  9729. pipe_config_err(adjust, __stringify(name), \
  9730. "(expected tu %i gmch %i/%i link %i/%i, " \
  9731. "or tu %i gmch %i/%i link %i/%i, " \
  9732. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9733. current_config->name.tu, \
  9734. current_config->name.gmch_m, \
  9735. current_config->name.gmch_n, \
  9736. current_config->name.link_m, \
  9737. current_config->name.link_n, \
  9738. current_config->alt_name.tu, \
  9739. current_config->alt_name.gmch_m, \
  9740. current_config->alt_name.gmch_n, \
  9741. current_config->alt_name.link_m, \
  9742. current_config->alt_name.link_n, \
  9743. pipe_config->name.tu, \
  9744. pipe_config->name.gmch_m, \
  9745. pipe_config->name.gmch_n, \
  9746. pipe_config->name.link_m, \
  9747. pipe_config->name.link_n); \
  9748. ret = false; \
  9749. }
  9750. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9751. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9752. pipe_config_err(adjust, __stringify(name), \
  9753. "(%x) (expected %i, found %i)\n", \
  9754. (mask), \
  9755. current_config->name & (mask), \
  9756. pipe_config->name & (mask)); \
  9757. ret = false; \
  9758. }
  9759. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9760. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9761. pipe_config_err(adjust, __stringify(name), \
  9762. "(expected %i, found %i)\n", \
  9763. current_config->name, \
  9764. pipe_config->name); \
  9765. ret = false; \
  9766. }
  9767. #define PIPE_CONF_QUIRK(quirk) \
  9768. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9769. PIPE_CONF_CHECK_I(cpu_transcoder);
  9770. PIPE_CONF_CHECK_I(has_pch_encoder);
  9771. PIPE_CONF_CHECK_I(fdi_lanes);
  9772. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9773. PIPE_CONF_CHECK_I(lane_count);
  9774. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9775. if (INTEL_GEN(dev_priv) < 8) {
  9776. PIPE_CONF_CHECK_M_N(dp_m_n);
  9777. if (current_config->has_drrs)
  9778. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9779. } else
  9780. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9781. PIPE_CONF_CHECK_X(output_types);
  9782. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9783. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9784. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9785. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9786. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9787. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9788. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9789. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9790. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9791. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9792. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9793. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9794. PIPE_CONF_CHECK_I(pixel_multiplier);
  9795. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9796. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9797. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9798. PIPE_CONF_CHECK_I(limited_color_range);
  9799. PIPE_CONF_CHECK_I(hdmi_scrambling);
  9800. PIPE_CONF_CHECK_I(hdmi_high_tmds_clock_ratio);
  9801. PIPE_CONF_CHECK_I(has_infoframe);
  9802. PIPE_CONF_CHECK_I(has_audio);
  9803. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9804. DRM_MODE_FLAG_INTERLACE);
  9805. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9806. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9807. DRM_MODE_FLAG_PHSYNC);
  9808. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9809. DRM_MODE_FLAG_NHSYNC);
  9810. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9811. DRM_MODE_FLAG_PVSYNC);
  9812. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9813. DRM_MODE_FLAG_NVSYNC);
  9814. }
  9815. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9816. /* pfit ratios are autocomputed by the hw on gen4+ */
  9817. if (INTEL_GEN(dev_priv) < 4)
  9818. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9819. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9820. if (!adjust) {
  9821. PIPE_CONF_CHECK_I(pipe_src_w);
  9822. PIPE_CONF_CHECK_I(pipe_src_h);
  9823. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9824. if (current_config->pch_pfit.enabled) {
  9825. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9826. PIPE_CONF_CHECK_X(pch_pfit.size);
  9827. }
  9828. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9829. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9830. }
  9831. /* BDW+ don't expose a synchronous way to read the state */
  9832. if (IS_HASWELL(dev_priv))
  9833. PIPE_CONF_CHECK_I(ips_enabled);
  9834. PIPE_CONF_CHECK_I(double_wide);
  9835. PIPE_CONF_CHECK_P(shared_dpll);
  9836. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9837. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9838. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9839. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9840. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9841. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9842. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9843. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9844. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9845. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9846. PIPE_CONF_CHECK_X(dsi_pll.div);
  9847. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9848. PIPE_CONF_CHECK_I(pipe_bpp);
  9849. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9850. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9851. #undef PIPE_CONF_CHECK_X
  9852. #undef PIPE_CONF_CHECK_I
  9853. #undef PIPE_CONF_CHECK_P
  9854. #undef PIPE_CONF_CHECK_FLAGS
  9855. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9856. #undef PIPE_CONF_QUIRK
  9857. return ret;
  9858. }
  9859. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9860. const struct intel_crtc_state *pipe_config)
  9861. {
  9862. if (pipe_config->has_pch_encoder) {
  9863. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9864. &pipe_config->fdi_m_n);
  9865. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9866. /*
  9867. * FDI already provided one idea for the dotclock.
  9868. * Yell if the encoder disagrees.
  9869. */
  9870. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9871. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9872. fdi_dotclock, dotclock);
  9873. }
  9874. }
  9875. static void verify_wm_state(struct drm_crtc *crtc,
  9876. struct drm_crtc_state *new_state)
  9877. {
  9878. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9879. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9880. struct skl_pipe_wm hw_wm, *sw_wm;
  9881. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9882. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9884. const enum pipe pipe = intel_crtc->pipe;
  9885. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9886. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9887. return;
  9888. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9889. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9890. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9891. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9892. /* planes */
  9893. for_each_universal_plane(dev_priv, pipe, plane) {
  9894. hw_plane_wm = &hw_wm.planes[plane];
  9895. sw_plane_wm = &sw_wm->planes[plane];
  9896. /* Watermarks */
  9897. for (level = 0; level <= max_level; level++) {
  9898. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9899. &sw_plane_wm->wm[level]))
  9900. continue;
  9901. 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",
  9902. pipe_name(pipe), plane + 1, level,
  9903. sw_plane_wm->wm[level].plane_en,
  9904. sw_plane_wm->wm[level].plane_res_b,
  9905. sw_plane_wm->wm[level].plane_res_l,
  9906. hw_plane_wm->wm[level].plane_en,
  9907. hw_plane_wm->wm[level].plane_res_b,
  9908. hw_plane_wm->wm[level].plane_res_l);
  9909. }
  9910. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9911. &sw_plane_wm->trans_wm)) {
  9912. 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",
  9913. pipe_name(pipe), plane + 1,
  9914. sw_plane_wm->trans_wm.plane_en,
  9915. sw_plane_wm->trans_wm.plane_res_b,
  9916. sw_plane_wm->trans_wm.plane_res_l,
  9917. hw_plane_wm->trans_wm.plane_en,
  9918. hw_plane_wm->trans_wm.plane_res_b,
  9919. hw_plane_wm->trans_wm.plane_res_l);
  9920. }
  9921. /* DDB */
  9922. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9923. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9924. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9925. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9926. pipe_name(pipe), plane + 1,
  9927. sw_ddb_entry->start, sw_ddb_entry->end,
  9928. hw_ddb_entry->start, hw_ddb_entry->end);
  9929. }
  9930. }
  9931. /*
  9932. * cursor
  9933. * If the cursor plane isn't active, we may not have updated it's ddb
  9934. * allocation. In that case since the ddb allocation will be updated
  9935. * once the plane becomes visible, we can skip this check
  9936. */
  9937. if (intel_crtc->cursor_addr) {
  9938. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9939. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9940. /* Watermarks */
  9941. for (level = 0; level <= max_level; level++) {
  9942. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9943. &sw_plane_wm->wm[level]))
  9944. continue;
  9945. 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",
  9946. pipe_name(pipe), level,
  9947. sw_plane_wm->wm[level].plane_en,
  9948. sw_plane_wm->wm[level].plane_res_b,
  9949. sw_plane_wm->wm[level].plane_res_l,
  9950. hw_plane_wm->wm[level].plane_en,
  9951. hw_plane_wm->wm[level].plane_res_b,
  9952. hw_plane_wm->wm[level].plane_res_l);
  9953. }
  9954. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9955. &sw_plane_wm->trans_wm)) {
  9956. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9957. pipe_name(pipe),
  9958. sw_plane_wm->trans_wm.plane_en,
  9959. sw_plane_wm->trans_wm.plane_res_b,
  9960. sw_plane_wm->trans_wm.plane_res_l,
  9961. hw_plane_wm->trans_wm.plane_en,
  9962. hw_plane_wm->trans_wm.plane_res_b,
  9963. hw_plane_wm->trans_wm.plane_res_l);
  9964. }
  9965. /* DDB */
  9966. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9967. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9968. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9969. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9970. pipe_name(pipe),
  9971. sw_ddb_entry->start, sw_ddb_entry->end,
  9972. hw_ddb_entry->start, hw_ddb_entry->end);
  9973. }
  9974. }
  9975. }
  9976. static void
  9977. verify_connector_state(struct drm_device *dev,
  9978. struct drm_atomic_state *state,
  9979. struct drm_crtc *crtc)
  9980. {
  9981. struct drm_connector *connector;
  9982. struct drm_connector_state *new_conn_state;
  9983. int i;
  9984. for_each_new_connector_in_state(state, connector, new_conn_state, i) {
  9985. struct drm_encoder *encoder = connector->encoder;
  9986. if (new_conn_state->crtc != crtc)
  9987. continue;
  9988. intel_connector_verify_state(to_intel_connector(connector));
  9989. I915_STATE_WARN(new_conn_state->best_encoder != encoder,
  9990. "connector's atomic encoder doesn't match legacy encoder\n");
  9991. }
  9992. }
  9993. static void
  9994. verify_encoder_state(struct drm_device *dev, struct drm_atomic_state *state)
  9995. {
  9996. struct intel_encoder *encoder;
  9997. struct drm_connector *connector;
  9998. struct drm_connector_state *old_conn_state, *new_conn_state;
  9999. int i;
  10000. for_each_intel_encoder(dev, encoder) {
  10001. bool enabled = false, found = false;
  10002. enum pipe pipe;
  10003. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  10004. encoder->base.base.id,
  10005. encoder->base.name);
  10006. for_each_oldnew_connector_in_state(state, connector, old_conn_state,
  10007. new_conn_state, i) {
  10008. if (old_conn_state->best_encoder == &encoder->base)
  10009. found = true;
  10010. if (new_conn_state->best_encoder != &encoder->base)
  10011. continue;
  10012. found = enabled = true;
  10013. I915_STATE_WARN(new_conn_state->crtc !=
  10014. encoder->base.crtc,
  10015. "connector's crtc doesn't match encoder crtc\n");
  10016. }
  10017. if (!found)
  10018. continue;
  10019. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  10020. "encoder's enabled state mismatch "
  10021. "(expected %i, found %i)\n",
  10022. !!encoder->base.crtc, enabled);
  10023. if (!encoder->base.crtc) {
  10024. bool active;
  10025. active = encoder->get_hw_state(encoder, &pipe);
  10026. I915_STATE_WARN(active,
  10027. "encoder detached but still enabled on pipe %c.\n",
  10028. pipe_name(pipe));
  10029. }
  10030. }
  10031. }
  10032. static void
  10033. verify_crtc_state(struct drm_crtc *crtc,
  10034. struct drm_crtc_state *old_crtc_state,
  10035. struct drm_crtc_state *new_crtc_state)
  10036. {
  10037. struct drm_device *dev = crtc->dev;
  10038. struct drm_i915_private *dev_priv = to_i915(dev);
  10039. struct intel_encoder *encoder;
  10040. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10041. struct intel_crtc_state *pipe_config, *sw_config;
  10042. struct drm_atomic_state *old_state;
  10043. bool active;
  10044. old_state = old_crtc_state->state;
  10045. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10046. pipe_config = to_intel_crtc_state(old_crtc_state);
  10047. memset(pipe_config, 0, sizeof(*pipe_config));
  10048. pipe_config->base.crtc = crtc;
  10049. pipe_config->base.state = old_state;
  10050. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10051. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10052. /* hw state is inconsistent with the pipe quirk */
  10053. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10054. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10055. active = new_crtc_state->active;
  10056. I915_STATE_WARN(new_crtc_state->active != active,
  10057. "crtc active state doesn't match with hw state "
  10058. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10059. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10060. "transitional active state does not match atomic hw state "
  10061. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10062. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10063. enum pipe pipe;
  10064. active = encoder->get_hw_state(encoder, &pipe);
  10065. I915_STATE_WARN(active != new_crtc_state->active,
  10066. "[ENCODER:%i] active %i with crtc active %i\n",
  10067. encoder->base.base.id, active, new_crtc_state->active);
  10068. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10069. "Encoder connected to wrong pipe %c\n",
  10070. pipe_name(pipe));
  10071. if (active) {
  10072. pipe_config->output_types |= 1 << encoder->type;
  10073. encoder->get_config(encoder, pipe_config);
  10074. }
  10075. }
  10076. intel_crtc_compute_pixel_rate(pipe_config);
  10077. if (!new_crtc_state->active)
  10078. return;
  10079. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10080. sw_config = to_intel_crtc_state(crtc->state);
  10081. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10082. pipe_config, false)) {
  10083. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10084. intel_dump_pipe_config(intel_crtc, pipe_config,
  10085. "[hw state]");
  10086. intel_dump_pipe_config(intel_crtc, sw_config,
  10087. "[sw state]");
  10088. }
  10089. }
  10090. static void
  10091. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10092. struct intel_shared_dpll *pll,
  10093. struct drm_crtc *crtc,
  10094. struct drm_crtc_state *new_state)
  10095. {
  10096. struct intel_dpll_hw_state dpll_hw_state;
  10097. unsigned crtc_mask;
  10098. bool active;
  10099. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10100. DRM_DEBUG_KMS("%s\n", pll->name);
  10101. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10102. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10103. I915_STATE_WARN(!pll->on && pll->active_mask,
  10104. "pll in active use but not on in sw tracking\n");
  10105. I915_STATE_WARN(pll->on && !pll->active_mask,
  10106. "pll is on but not used by any active crtc\n");
  10107. I915_STATE_WARN(pll->on != active,
  10108. "pll on state mismatch (expected %i, found %i)\n",
  10109. pll->on, active);
  10110. }
  10111. if (!crtc) {
  10112. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10113. "more active pll users than references: %x vs %x\n",
  10114. pll->active_mask, pll->state.crtc_mask);
  10115. return;
  10116. }
  10117. crtc_mask = 1 << drm_crtc_index(crtc);
  10118. if (new_state->active)
  10119. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10120. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10121. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10122. else
  10123. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10124. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10125. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10126. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10127. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10128. crtc_mask, pll->state.crtc_mask);
  10129. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10130. &dpll_hw_state,
  10131. sizeof(dpll_hw_state)),
  10132. "pll hw state mismatch\n");
  10133. }
  10134. static void
  10135. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10136. struct drm_crtc_state *old_crtc_state,
  10137. struct drm_crtc_state *new_crtc_state)
  10138. {
  10139. struct drm_i915_private *dev_priv = to_i915(dev);
  10140. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10141. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10142. if (new_state->shared_dpll)
  10143. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10144. if (old_state->shared_dpll &&
  10145. old_state->shared_dpll != new_state->shared_dpll) {
  10146. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10147. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10148. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10149. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10150. pipe_name(drm_crtc_index(crtc)));
  10151. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10152. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10153. pipe_name(drm_crtc_index(crtc)));
  10154. }
  10155. }
  10156. static void
  10157. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10158. struct drm_atomic_state *state,
  10159. struct drm_crtc_state *old_state,
  10160. struct drm_crtc_state *new_state)
  10161. {
  10162. if (!needs_modeset(new_state) &&
  10163. !to_intel_crtc_state(new_state)->update_pipe)
  10164. return;
  10165. verify_wm_state(crtc, new_state);
  10166. verify_connector_state(crtc->dev, state, crtc);
  10167. verify_crtc_state(crtc, old_state, new_state);
  10168. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10169. }
  10170. static void
  10171. verify_disabled_dpll_state(struct drm_device *dev)
  10172. {
  10173. struct drm_i915_private *dev_priv = to_i915(dev);
  10174. int i;
  10175. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10176. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10177. }
  10178. static void
  10179. intel_modeset_verify_disabled(struct drm_device *dev,
  10180. struct drm_atomic_state *state)
  10181. {
  10182. verify_encoder_state(dev, state);
  10183. verify_connector_state(dev, state, NULL);
  10184. verify_disabled_dpll_state(dev);
  10185. }
  10186. static void update_scanline_offset(struct intel_crtc *crtc)
  10187. {
  10188. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10189. /*
  10190. * The scanline counter increments at the leading edge of hsync.
  10191. *
  10192. * On most platforms it starts counting from vtotal-1 on the
  10193. * first active line. That means the scanline counter value is
  10194. * always one less than what we would expect. Ie. just after
  10195. * start of vblank, which also occurs at start of hsync (on the
  10196. * last active line), the scanline counter will read vblank_start-1.
  10197. *
  10198. * On gen2 the scanline counter starts counting from 1 instead
  10199. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10200. * to keep the value positive), instead of adding one.
  10201. *
  10202. * On HSW+ the behaviour of the scanline counter depends on the output
  10203. * type. For DP ports it behaves like most other platforms, but on HDMI
  10204. * there's an extra 1 line difference. So we need to add two instead of
  10205. * one to the value.
  10206. */
  10207. if (IS_GEN2(dev_priv)) {
  10208. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10209. int vtotal;
  10210. vtotal = adjusted_mode->crtc_vtotal;
  10211. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10212. vtotal /= 2;
  10213. crtc->scanline_offset = vtotal - 1;
  10214. } else if (HAS_DDI(dev_priv) &&
  10215. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10216. crtc->scanline_offset = 2;
  10217. } else
  10218. crtc->scanline_offset = 1;
  10219. }
  10220. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10221. {
  10222. struct drm_device *dev = state->dev;
  10223. struct drm_i915_private *dev_priv = to_i915(dev);
  10224. struct drm_crtc *crtc;
  10225. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10226. int i;
  10227. if (!dev_priv->display.crtc_compute_clock)
  10228. return;
  10229. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10230. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10231. struct intel_shared_dpll *old_dpll =
  10232. to_intel_crtc_state(old_crtc_state)->shared_dpll;
  10233. if (!needs_modeset(new_crtc_state))
  10234. continue;
  10235. to_intel_crtc_state(new_crtc_state)->shared_dpll = NULL;
  10236. if (!old_dpll)
  10237. continue;
  10238. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10239. }
  10240. }
  10241. /*
  10242. * This implements the workaround described in the "notes" section of the mode
  10243. * set sequence documentation. When going from no pipes or single pipe to
  10244. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10245. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10246. */
  10247. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10248. {
  10249. struct drm_crtc_state *crtc_state;
  10250. struct intel_crtc *intel_crtc;
  10251. struct drm_crtc *crtc;
  10252. struct intel_crtc_state *first_crtc_state = NULL;
  10253. struct intel_crtc_state *other_crtc_state = NULL;
  10254. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10255. int i;
  10256. /* look at all crtc's that are going to be enabled in during modeset */
  10257. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10258. intel_crtc = to_intel_crtc(crtc);
  10259. if (!crtc_state->active || !needs_modeset(crtc_state))
  10260. continue;
  10261. if (first_crtc_state) {
  10262. other_crtc_state = to_intel_crtc_state(crtc_state);
  10263. break;
  10264. } else {
  10265. first_crtc_state = to_intel_crtc_state(crtc_state);
  10266. first_pipe = intel_crtc->pipe;
  10267. }
  10268. }
  10269. /* No workaround needed? */
  10270. if (!first_crtc_state)
  10271. return 0;
  10272. /* w/a possibly needed, check how many crtc's are already enabled. */
  10273. for_each_intel_crtc(state->dev, intel_crtc) {
  10274. struct intel_crtc_state *pipe_config;
  10275. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10276. if (IS_ERR(pipe_config))
  10277. return PTR_ERR(pipe_config);
  10278. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10279. if (!pipe_config->base.active ||
  10280. needs_modeset(&pipe_config->base))
  10281. continue;
  10282. /* 2 or more enabled crtcs means no need for w/a */
  10283. if (enabled_pipe != INVALID_PIPE)
  10284. return 0;
  10285. enabled_pipe = intel_crtc->pipe;
  10286. }
  10287. if (enabled_pipe != INVALID_PIPE)
  10288. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10289. else if (other_crtc_state)
  10290. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10291. return 0;
  10292. }
  10293. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10294. {
  10295. struct drm_crtc *crtc;
  10296. /* Add all pipes to the state */
  10297. for_each_crtc(state->dev, crtc) {
  10298. struct drm_crtc_state *crtc_state;
  10299. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10300. if (IS_ERR(crtc_state))
  10301. return PTR_ERR(crtc_state);
  10302. }
  10303. return 0;
  10304. }
  10305. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10306. {
  10307. struct drm_crtc *crtc;
  10308. /*
  10309. * Add all pipes to the state, and force
  10310. * a modeset on all the active ones.
  10311. */
  10312. for_each_crtc(state->dev, crtc) {
  10313. struct drm_crtc_state *crtc_state;
  10314. int ret;
  10315. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10316. if (IS_ERR(crtc_state))
  10317. return PTR_ERR(crtc_state);
  10318. if (!crtc_state->active || needs_modeset(crtc_state))
  10319. continue;
  10320. crtc_state->mode_changed = true;
  10321. ret = drm_atomic_add_affected_connectors(state, crtc);
  10322. if (ret)
  10323. return ret;
  10324. ret = drm_atomic_add_affected_planes(state, crtc);
  10325. if (ret)
  10326. return ret;
  10327. }
  10328. return 0;
  10329. }
  10330. static int intel_modeset_checks(struct drm_atomic_state *state)
  10331. {
  10332. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10333. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10334. struct drm_crtc *crtc;
  10335. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10336. int ret = 0, i;
  10337. if (!check_digital_port_conflicts(state)) {
  10338. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10339. return -EINVAL;
  10340. }
  10341. intel_state->modeset = true;
  10342. intel_state->active_crtcs = dev_priv->active_crtcs;
  10343. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10344. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10345. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10346. if (new_crtc_state->active)
  10347. intel_state->active_crtcs |= 1 << i;
  10348. else
  10349. intel_state->active_crtcs &= ~(1 << i);
  10350. if (old_crtc_state->active != new_crtc_state->active)
  10351. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10352. }
  10353. /*
  10354. * See if the config requires any additional preparation, e.g.
  10355. * to adjust global state with pipes off. We need to do this
  10356. * here so we can get the modeset_pipe updated config for the new
  10357. * mode set on this crtc. For other crtcs we need to use the
  10358. * adjusted_mode bits in the crtc directly.
  10359. */
  10360. if (dev_priv->display.modeset_calc_cdclk) {
  10361. ret = dev_priv->display.modeset_calc_cdclk(state);
  10362. if (ret < 0)
  10363. return ret;
  10364. /*
  10365. * Writes to dev_priv->cdclk.logical must protected by
  10366. * holding all the crtc locks, even if we don't end up
  10367. * touching the hardware
  10368. */
  10369. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10370. &intel_state->cdclk.logical)) {
  10371. ret = intel_lock_all_pipes(state);
  10372. if (ret < 0)
  10373. return ret;
  10374. }
  10375. /* All pipes must be switched off while we change the cdclk. */
  10376. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10377. &intel_state->cdclk.actual)) {
  10378. ret = intel_modeset_all_pipes(state);
  10379. if (ret < 0)
  10380. return ret;
  10381. }
  10382. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10383. intel_state->cdclk.logical.cdclk,
  10384. intel_state->cdclk.actual.cdclk);
  10385. } else {
  10386. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10387. }
  10388. intel_modeset_clear_plls(state);
  10389. if (IS_HASWELL(dev_priv))
  10390. return haswell_mode_set_planes_workaround(state);
  10391. return 0;
  10392. }
  10393. /*
  10394. * Handle calculation of various watermark data at the end of the atomic check
  10395. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10396. * handlers to ensure that all derived state has been updated.
  10397. */
  10398. static int calc_watermark_data(struct drm_atomic_state *state)
  10399. {
  10400. struct drm_device *dev = state->dev;
  10401. struct drm_i915_private *dev_priv = to_i915(dev);
  10402. /* Is there platform-specific watermark information to calculate? */
  10403. if (dev_priv->display.compute_global_watermarks)
  10404. return dev_priv->display.compute_global_watermarks(state);
  10405. return 0;
  10406. }
  10407. /**
  10408. * intel_atomic_check - validate state object
  10409. * @dev: drm device
  10410. * @state: state to validate
  10411. */
  10412. static int intel_atomic_check(struct drm_device *dev,
  10413. struct drm_atomic_state *state)
  10414. {
  10415. struct drm_i915_private *dev_priv = to_i915(dev);
  10416. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10417. struct drm_crtc *crtc;
  10418. struct drm_crtc_state *old_crtc_state, *crtc_state;
  10419. int ret, i;
  10420. bool any_ms = false;
  10421. ret = drm_atomic_helper_check_modeset(dev, state);
  10422. if (ret)
  10423. return ret;
  10424. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, crtc_state, i) {
  10425. struct intel_crtc_state *pipe_config =
  10426. to_intel_crtc_state(crtc_state);
  10427. /* Catch I915_MODE_FLAG_INHERITED */
  10428. if (crtc_state->mode.private_flags != old_crtc_state->mode.private_flags)
  10429. crtc_state->mode_changed = true;
  10430. if (!needs_modeset(crtc_state))
  10431. continue;
  10432. if (!crtc_state->enable) {
  10433. any_ms = true;
  10434. continue;
  10435. }
  10436. /* FIXME: For only active_changed we shouldn't need to do any
  10437. * state recomputation at all. */
  10438. ret = drm_atomic_add_affected_connectors(state, crtc);
  10439. if (ret)
  10440. return ret;
  10441. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10442. if (ret) {
  10443. intel_dump_pipe_config(to_intel_crtc(crtc),
  10444. pipe_config, "[failed]");
  10445. return ret;
  10446. }
  10447. if (i915.fastboot &&
  10448. intel_pipe_config_compare(dev_priv,
  10449. to_intel_crtc_state(old_crtc_state),
  10450. pipe_config, true)) {
  10451. crtc_state->mode_changed = false;
  10452. pipe_config->update_pipe = true;
  10453. }
  10454. if (needs_modeset(crtc_state))
  10455. any_ms = true;
  10456. ret = drm_atomic_add_affected_planes(state, crtc);
  10457. if (ret)
  10458. return ret;
  10459. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10460. needs_modeset(crtc_state) ?
  10461. "[modeset]" : "[fastset]");
  10462. }
  10463. if (any_ms) {
  10464. ret = intel_modeset_checks(state);
  10465. if (ret)
  10466. return ret;
  10467. } else {
  10468. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10469. }
  10470. ret = drm_atomic_helper_check_planes(dev, state);
  10471. if (ret)
  10472. return ret;
  10473. intel_fbc_choose_crtc(dev_priv, state);
  10474. return calc_watermark_data(state);
  10475. }
  10476. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10477. struct drm_atomic_state *state)
  10478. {
  10479. struct drm_i915_private *dev_priv = to_i915(dev);
  10480. struct drm_crtc_state *crtc_state;
  10481. struct drm_crtc *crtc;
  10482. int i, ret;
  10483. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  10484. if (state->legacy_cursor_update)
  10485. continue;
  10486. ret = intel_crtc_wait_for_pending_flips(crtc);
  10487. if (ret)
  10488. return ret;
  10489. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10490. flush_workqueue(dev_priv->wq);
  10491. }
  10492. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10493. if (ret)
  10494. return ret;
  10495. ret = drm_atomic_helper_prepare_planes(dev, state);
  10496. mutex_unlock(&dev->struct_mutex);
  10497. return ret;
  10498. }
  10499. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10500. {
  10501. struct drm_device *dev = crtc->base.dev;
  10502. if (!dev->max_vblank_count)
  10503. return drm_accurate_vblank_count(&crtc->base);
  10504. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10505. }
  10506. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10507. struct drm_i915_private *dev_priv,
  10508. unsigned crtc_mask)
  10509. {
  10510. unsigned last_vblank_count[I915_MAX_PIPES];
  10511. enum pipe pipe;
  10512. int ret;
  10513. if (!crtc_mask)
  10514. return;
  10515. for_each_pipe(dev_priv, pipe) {
  10516. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10517. pipe);
  10518. if (!((1 << pipe) & crtc_mask))
  10519. continue;
  10520. ret = drm_crtc_vblank_get(&crtc->base);
  10521. if (WARN_ON(ret != 0)) {
  10522. crtc_mask &= ~(1 << pipe);
  10523. continue;
  10524. }
  10525. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10526. }
  10527. for_each_pipe(dev_priv, pipe) {
  10528. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10529. pipe);
  10530. long lret;
  10531. if (!((1 << pipe) & crtc_mask))
  10532. continue;
  10533. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10534. last_vblank_count[pipe] !=
  10535. drm_crtc_vblank_count(&crtc->base),
  10536. msecs_to_jiffies(50));
  10537. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10538. drm_crtc_vblank_put(&crtc->base);
  10539. }
  10540. }
  10541. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10542. {
  10543. /* fb updated, need to unpin old fb */
  10544. if (crtc_state->fb_changed)
  10545. return true;
  10546. /* wm changes, need vblank before final wm's */
  10547. if (crtc_state->update_wm_post)
  10548. return true;
  10549. if (crtc_state->wm.need_postvbl_update)
  10550. return true;
  10551. return false;
  10552. }
  10553. static void intel_update_crtc(struct drm_crtc *crtc,
  10554. struct drm_atomic_state *state,
  10555. struct drm_crtc_state *old_crtc_state,
  10556. struct drm_crtc_state *new_crtc_state,
  10557. unsigned int *crtc_vblank_mask)
  10558. {
  10559. struct drm_device *dev = crtc->dev;
  10560. struct drm_i915_private *dev_priv = to_i915(dev);
  10561. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10562. struct intel_crtc_state *pipe_config = to_intel_crtc_state(new_crtc_state);
  10563. bool modeset = needs_modeset(new_crtc_state);
  10564. if (modeset) {
  10565. update_scanline_offset(intel_crtc);
  10566. dev_priv->display.crtc_enable(pipe_config, state);
  10567. } else {
  10568. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10569. pipe_config);
  10570. }
  10571. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10572. intel_fbc_enable(
  10573. intel_crtc, pipe_config,
  10574. to_intel_plane_state(crtc->primary->state));
  10575. }
  10576. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10577. if (needs_vblank_wait(pipe_config))
  10578. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10579. }
  10580. static void intel_update_crtcs(struct drm_atomic_state *state,
  10581. unsigned int *crtc_vblank_mask)
  10582. {
  10583. struct drm_crtc *crtc;
  10584. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10585. int i;
  10586. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10587. if (!new_crtc_state->active)
  10588. continue;
  10589. intel_update_crtc(crtc, state, old_crtc_state,
  10590. new_crtc_state, crtc_vblank_mask);
  10591. }
  10592. }
  10593. static void skl_update_crtcs(struct drm_atomic_state *state,
  10594. unsigned int *crtc_vblank_mask)
  10595. {
  10596. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10597. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10598. struct drm_crtc *crtc;
  10599. struct intel_crtc *intel_crtc;
  10600. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10601. struct intel_crtc_state *cstate;
  10602. unsigned int updated = 0;
  10603. bool progress;
  10604. enum pipe pipe;
  10605. int i;
  10606. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10607. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i)
  10608. /* ignore allocations for crtc's that have been turned off. */
  10609. if (new_crtc_state->active)
  10610. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10611. /*
  10612. * Whenever the number of active pipes changes, we need to make sure we
  10613. * update the pipes in the right order so that their ddb allocations
  10614. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10615. * cause pipe underruns and other bad stuff.
  10616. */
  10617. do {
  10618. progress = false;
  10619. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10620. bool vbl_wait = false;
  10621. unsigned int cmask = drm_crtc_mask(crtc);
  10622. intel_crtc = to_intel_crtc(crtc);
  10623. cstate = to_intel_crtc_state(crtc->state);
  10624. pipe = intel_crtc->pipe;
  10625. if (updated & cmask || !cstate->base.active)
  10626. continue;
  10627. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10628. continue;
  10629. updated |= cmask;
  10630. entries[i] = &cstate->wm.skl.ddb;
  10631. /*
  10632. * If this is an already active pipe, it's DDB changed,
  10633. * and this isn't the last pipe that needs updating
  10634. * then we need to wait for a vblank to pass for the
  10635. * new ddb allocation to take effect.
  10636. */
  10637. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10638. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10639. !new_crtc_state->active_changed &&
  10640. intel_state->wm_results.dirty_pipes != updated)
  10641. vbl_wait = true;
  10642. intel_update_crtc(crtc, state, old_crtc_state,
  10643. new_crtc_state, crtc_vblank_mask);
  10644. if (vbl_wait)
  10645. intel_wait_for_vblank(dev_priv, pipe);
  10646. progress = true;
  10647. }
  10648. } while (progress);
  10649. }
  10650. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10651. {
  10652. struct intel_atomic_state *state, *next;
  10653. struct llist_node *freed;
  10654. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10655. llist_for_each_entry_safe(state, next, freed, freed)
  10656. drm_atomic_state_put(&state->base);
  10657. }
  10658. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10659. {
  10660. struct drm_i915_private *dev_priv =
  10661. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10662. intel_atomic_helper_free_state(dev_priv);
  10663. }
  10664. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10665. {
  10666. struct drm_device *dev = state->dev;
  10667. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10668. struct drm_i915_private *dev_priv = to_i915(dev);
  10669. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  10670. struct drm_crtc *crtc;
  10671. struct intel_crtc_state *intel_cstate;
  10672. bool hw_check = intel_state->modeset;
  10673. u64 put_domains[I915_MAX_PIPES] = {};
  10674. unsigned crtc_vblank_mask = 0;
  10675. int i;
  10676. drm_atomic_helper_wait_for_dependencies(state);
  10677. if (intel_state->modeset)
  10678. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10679. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10680. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10681. if (needs_modeset(new_crtc_state) ||
  10682. to_intel_crtc_state(new_crtc_state)->update_pipe) {
  10683. hw_check = true;
  10684. put_domains[to_intel_crtc(crtc)->pipe] =
  10685. modeset_get_crtc_power_domains(crtc,
  10686. to_intel_crtc_state(new_crtc_state));
  10687. }
  10688. if (!needs_modeset(new_crtc_state))
  10689. continue;
  10690. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state),
  10691. to_intel_crtc_state(new_crtc_state));
  10692. if (old_crtc_state->active) {
  10693. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10694. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10695. intel_crtc->active = false;
  10696. intel_fbc_disable(intel_crtc);
  10697. intel_disable_shared_dpll(intel_crtc);
  10698. /*
  10699. * Underruns don't always raise
  10700. * interrupts, so check manually.
  10701. */
  10702. intel_check_cpu_fifo_underruns(dev_priv);
  10703. intel_check_pch_fifo_underruns(dev_priv);
  10704. if (!crtc->state->active) {
  10705. /*
  10706. * Make sure we don't call initial_watermarks
  10707. * for ILK-style watermark updates.
  10708. *
  10709. * No clue what this is supposed to achieve.
  10710. */
  10711. if (INTEL_GEN(dev_priv) >= 9)
  10712. dev_priv->display.initial_watermarks(intel_state,
  10713. to_intel_crtc_state(crtc->state));
  10714. }
  10715. }
  10716. }
  10717. /* Only after disabling all output pipelines that will be changed can we
  10718. * update the the output configuration. */
  10719. intel_modeset_update_crtc_state(state);
  10720. if (intel_state->modeset) {
  10721. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10722. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10723. /*
  10724. * SKL workaround: bspec recommends we disable the SAGV when we
  10725. * have more then one pipe enabled
  10726. */
  10727. if (!intel_can_enable_sagv(state))
  10728. intel_disable_sagv(dev_priv);
  10729. intel_modeset_verify_disabled(dev, state);
  10730. }
  10731. /* Complete the events for pipes that have now been disabled */
  10732. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10733. bool modeset = needs_modeset(new_crtc_state);
  10734. /* Complete events for now disable pipes here. */
  10735. if (modeset && !new_crtc_state->active && new_crtc_state->event) {
  10736. spin_lock_irq(&dev->event_lock);
  10737. drm_crtc_send_vblank_event(crtc, new_crtc_state->event);
  10738. spin_unlock_irq(&dev->event_lock);
  10739. new_crtc_state->event = NULL;
  10740. }
  10741. }
  10742. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10743. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10744. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10745. * already, but still need the state for the delayed optimization. To
  10746. * fix this:
  10747. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10748. * - schedule that vblank worker _before_ calling hw_done
  10749. * - at the start of commit_tail, cancel it _synchrously
  10750. * - switch over to the vblank wait helper in the core after that since
  10751. * we don't need out special handling any more.
  10752. */
  10753. if (!state->legacy_cursor_update)
  10754. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10755. /*
  10756. * Now that the vblank has passed, we can go ahead and program the
  10757. * optimal watermarks on platforms that need two-step watermark
  10758. * programming.
  10759. *
  10760. * TODO: Move this (and other cleanup) to an async worker eventually.
  10761. */
  10762. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  10763. intel_cstate = to_intel_crtc_state(new_crtc_state);
  10764. if (dev_priv->display.optimize_watermarks)
  10765. dev_priv->display.optimize_watermarks(intel_state,
  10766. intel_cstate);
  10767. }
  10768. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  10769. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10770. if (put_domains[i])
  10771. modeset_put_power_domains(dev_priv, put_domains[i]);
  10772. intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
  10773. }
  10774. if (intel_state->modeset && intel_can_enable_sagv(state))
  10775. intel_enable_sagv(dev_priv);
  10776. drm_atomic_helper_commit_hw_done(state);
  10777. if (intel_state->modeset)
  10778. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10779. mutex_lock(&dev->struct_mutex);
  10780. drm_atomic_helper_cleanup_planes(dev, state);
  10781. mutex_unlock(&dev->struct_mutex);
  10782. drm_atomic_helper_commit_cleanup_done(state);
  10783. drm_atomic_state_put(state);
  10784. /* As one of the primary mmio accessors, KMS has a high likelihood
  10785. * of triggering bugs in unclaimed access. After we finish
  10786. * modesetting, see if an error has been flagged, and if so
  10787. * enable debugging for the next modeset - and hope we catch
  10788. * the culprit.
  10789. *
  10790. * XXX note that we assume display power is on at this point.
  10791. * This might hold true now but we need to add pm helper to check
  10792. * unclaimed only when the hardware is on, as atomic commits
  10793. * can happen also when the device is completely off.
  10794. */
  10795. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10796. intel_atomic_helper_free_state(dev_priv);
  10797. }
  10798. static void intel_atomic_commit_work(struct work_struct *work)
  10799. {
  10800. struct drm_atomic_state *state =
  10801. container_of(work, struct drm_atomic_state, commit_work);
  10802. intel_atomic_commit_tail(state);
  10803. }
  10804. static int __i915_sw_fence_call
  10805. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10806. enum i915_sw_fence_notify notify)
  10807. {
  10808. struct intel_atomic_state *state =
  10809. container_of(fence, struct intel_atomic_state, commit_ready);
  10810. switch (notify) {
  10811. case FENCE_COMPLETE:
  10812. if (state->base.commit_work.func)
  10813. queue_work(system_unbound_wq, &state->base.commit_work);
  10814. break;
  10815. case FENCE_FREE:
  10816. {
  10817. struct intel_atomic_helper *helper =
  10818. &to_i915(state->base.dev)->atomic_helper;
  10819. if (llist_add(&state->freed, &helper->free_list))
  10820. schedule_work(&helper->free_work);
  10821. break;
  10822. }
  10823. }
  10824. return NOTIFY_DONE;
  10825. }
  10826. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10827. {
  10828. struct drm_plane_state *old_plane_state, *new_plane_state;
  10829. struct drm_plane *plane;
  10830. int i;
  10831. for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i)
  10832. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10833. intel_fb_obj(new_plane_state->fb),
  10834. to_intel_plane(plane)->frontbuffer_bit);
  10835. }
  10836. /**
  10837. * intel_atomic_commit - commit validated state object
  10838. * @dev: DRM device
  10839. * @state: the top-level driver state object
  10840. * @nonblock: nonblocking commit
  10841. *
  10842. * This function commits a top-level state object that has been validated
  10843. * with drm_atomic_helper_check().
  10844. *
  10845. * RETURNS
  10846. * Zero for success or -errno.
  10847. */
  10848. static int intel_atomic_commit(struct drm_device *dev,
  10849. struct drm_atomic_state *state,
  10850. bool nonblock)
  10851. {
  10852. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10853. struct drm_i915_private *dev_priv = to_i915(dev);
  10854. int ret = 0;
  10855. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10856. if (ret)
  10857. return ret;
  10858. drm_atomic_state_get(state);
  10859. i915_sw_fence_init(&intel_state->commit_ready,
  10860. intel_atomic_commit_ready);
  10861. ret = intel_atomic_prepare_commit(dev, state);
  10862. if (ret) {
  10863. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10864. i915_sw_fence_commit(&intel_state->commit_ready);
  10865. return ret;
  10866. }
  10867. /*
  10868. * The intel_legacy_cursor_update() fast path takes care
  10869. * of avoiding the vblank waits for simple cursor
  10870. * movement and flips. For cursor on/off and size changes,
  10871. * we want to perform the vblank waits so that watermark
  10872. * updates happen during the correct frames. Gen9+ have
  10873. * double buffered watermarks and so shouldn't need this.
  10874. *
  10875. * Do this after drm_atomic_helper_setup_commit() and
  10876. * intel_atomic_prepare_commit() because we still want
  10877. * to skip the flip and fb cleanup waits. Although that
  10878. * does risk yanking the mapping from under the display
  10879. * engine.
  10880. *
  10881. * FIXME doing watermarks and fb cleanup from a vblank worker
  10882. * (assuming we had any) would solve these problems.
  10883. */
  10884. if (INTEL_GEN(dev_priv) < 9)
  10885. state->legacy_cursor_update = false;
  10886. drm_atomic_helper_swap_state(state, true);
  10887. dev_priv->wm.distrust_bios_wm = false;
  10888. intel_shared_dpll_swap_state(state);
  10889. intel_atomic_track_fbs(state);
  10890. if (intel_state->modeset) {
  10891. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10892. sizeof(intel_state->min_pixclk));
  10893. dev_priv->active_crtcs = intel_state->active_crtcs;
  10894. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10895. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10896. }
  10897. drm_atomic_state_get(state);
  10898. INIT_WORK(&state->commit_work,
  10899. nonblock ? intel_atomic_commit_work : NULL);
  10900. i915_sw_fence_commit(&intel_state->commit_ready);
  10901. if (!nonblock) {
  10902. i915_sw_fence_wait(&intel_state->commit_ready);
  10903. intel_atomic_commit_tail(state);
  10904. }
  10905. return 0;
  10906. }
  10907. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10908. {
  10909. struct drm_device *dev = crtc->dev;
  10910. struct drm_atomic_state *state;
  10911. struct drm_crtc_state *crtc_state;
  10912. int ret;
  10913. state = drm_atomic_state_alloc(dev);
  10914. if (!state) {
  10915. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  10916. crtc->base.id, crtc->name);
  10917. return;
  10918. }
  10919. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  10920. retry:
  10921. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10922. ret = PTR_ERR_OR_ZERO(crtc_state);
  10923. if (!ret) {
  10924. if (!crtc_state->active)
  10925. goto out;
  10926. crtc_state->mode_changed = true;
  10927. ret = drm_atomic_commit(state);
  10928. }
  10929. if (ret == -EDEADLK) {
  10930. drm_atomic_state_clear(state);
  10931. drm_modeset_backoff(state->acquire_ctx);
  10932. goto retry;
  10933. }
  10934. out:
  10935. drm_atomic_state_put(state);
  10936. }
  10937. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10938. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  10939. .set_config = drm_atomic_helper_set_config,
  10940. .set_property = drm_atomic_helper_crtc_set_property,
  10941. .destroy = intel_crtc_destroy,
  10942. .page_flip = drm_atomic_helper_page_flip,
  10943. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10944. .atomic_destroy_state = intel_crtc_destroy_state,
  10945. .set_crc_source = intel_crtc_set_crc_source,
  10946. };
  10947. /**
  10948. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10949. * @plane: drm plane to prepare for
  10950. * @fb: framebuffer to prepare for presentation
  10951. *
  10952. * Prepares a framebuffer for usage on a display plane. Generally this
  10953. * involves pinning the underlying object and updating the frontbuffer tracking
  10954. * bits. Some older platforms need special physical address handling for
  10955. * cursor planes.
  10956. *
  10957. * Must be called with struct_mutex held.
  10958. *
  10959. * Returns 0 on success, negative error code on failure.
  10960. */
  10961. int
  10962. intel_prepare_plane_fb(struct drm_plane *plane,
  10963. struct drm_plane_state *new_state)
  10964. {
  10965. struct intel_atomic_state *intel_state =
  10966. to_intel_atomic_state(new_state->state);
  10967. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10968. struct drm_framebuffer *fb = new_state->fb;
  10969. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10970. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10971. int ret;
  10972. if (obj) {
  10973. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10974. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10975. const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  10976. ret = i915_gem_object_attach_phys(obj, align);
  10977. if (ret) {
  10978. DRM_DEBUG_KMS("failed to attach phys object\n");
  10979. return ret;
  10980. }
  10981. } else {
  10982. struct i915_vma *vma;
  10983. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10984. if (IS_ERR(vma)) {
  10985. DRM_DEBUG_KMS("failed to pin object\n");
  10986. return PTR_ERR(vma);
  10987. }
  10988. to_intel_plane_state(new_state)->vma = vma;
  10989. }
  10990. }
  10991. if (!obj && !old_obj)
  10992. return 0;
  10993. if (old_obj) {
  10994. struct drm_crtc_state *crtc_state =
  10995. drm_atomic_get_existing_crtc_state(new_state->state,
  10996. plane->state->crtc);
  10997. /* Big Hammer, we also need to ensure that any pending
  10998. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10999. * current scanout is retired before unpinning the old
  11000. * framebuffer. Note that we rely on userspace rendering
  11001. * into the buffer attached to the pipe they are waiting
  11002. * on. If not, userspace generates a GPU hang with IPEHR
  11003. * point to the MI_WAIT_FOR_EVENT.
  11004. *
  11005. * This should only fail upon a hung GPU, in which case we
  11006. * can safely continue.
  11007. */
  11008. if (needs_modeset(crtc_state)) {
  11009. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11010. old_obj->resv, NULL,
  11011. false, 0,
  11012. GFP_KERNEL);
  11013. if (ret < 0)
  11014. return ret;
  11015. }
  11016. }
  11017. if (new_state->fence) { /* explicit fencing */
  11018. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11019. new_state->fence,
  11020. I915_FENCE_TIMEOUT,
  11021. GFP_KERNEL);
  11022. if (ret < 0)
  11023. return ret;
  11024. }
  11025. if (!obj)
  11026. return 0;
  11027. if (!new_state->fence) { /* implicit fencing */
  11028. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11029. obj->resv, NULL,
  11030. false, I915_FENCE_TIMEOUT,
  11031. GFP_KERNEL);
  11032. if (ret < 0)
  11033. return ret;
  11034. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11035. }
  11036. return 0;
  11037. }
  11038. /**
  11039. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11040. * @plane: drm plane to clean up for
  11041. * @fb: old framebuffer that was on plane
  11042. *
  11043. * Cleans up a framebuffer that has just been removed from a plane.
  11044. *
  11045. * Must be called with struct_mutex held.
  11046. */
  11047. void
  11048. intel_cleanup_plane_fb(struct drm_plane *plane,
  11049. struct drm_plane_state *old_state)
  11050. {
  11051. struct i915_vma *vma;
  11052. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11053. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11054. if (vma)
  11055. intel_unpin_fb_vma(vma);
  11056. }
  11057. int
  11058. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11059. {
  11060. struct drm_i915_private *dev_priv;
  11061. int max_scale;
  11062. int crtc_clock, max_dotclk;
  11063. if (!intel_crtc || !crtc_state->base.enable)
  11064. return DRM_PLANE_HELPER_NO_SCALING;
  11065. dev_priv = to_i915(intel_crtc->base.dev);
  11066. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11067. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11068. if (IS_GEMINILAKE(dev_priv))
  11069. max_dotclk *= 2;
  11070. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11071. return DRM_PLANE_HELPER_NO_SCALING;
  11072. /*
  11073. * skl max scale is lower of:
  11074. * close to 3 but not 3, -1 is for that purpose
  11075. * or
  11076. * cdclk/crtc_clock
  11077. */
  11078. max_scale = min((1 << 16) * 3 - 1,
  11079. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  11080. return max_scale;
  11081. }
  11082. static int
  11083. intel_check_primary_plane(struct drm_plane *plane,
  11084. struct intel_crtc_state *crtc_state,
  11085. struct intel_plane_state *state)
  11086. {
  11087. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11088. struct drm_crtc *crtc = state->base.crtc;
  11089. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11090. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11091. bool can_position = false;
  11092. int ret;
  11093. if (INTEL_GEN(dev_priv) >= 9) {
  11094. /* use scaler when colorkey is not required */
  11095. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11096. min_scale = 1;
  11097. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11098. }
  11099. can_position = true;
  11100. }
  11101. ret = drm_plane_helper_check_state(&state->base,
  11102. &state->clip,
  11103. min_scale, max_scale,
  11104. can_position, true);
  11105. if (ret)
  11106. return ret;
  11107. if (!state->base.fb)
  11108. return 0;
  11109. if (INTEL_GEN(dev_priv) >= 9) {
  11110. ret = skl_check_plane_surface(state);
  11111. if (ret)
  11112. return ret;
  11113. state->ctl = skl_plane_ctl(crtc_state, state);
  11114. } else {
  11115. ret = i9xx_check_plane_surface(state);
  11116. if (ret)
  11117. return ret;
  11118. state->ctl = i9xx_plane_ctl(crtc_state, state);
  11119. }
  11120. return 0;
  11121. }
  11122. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11123. struct drm_crtc_state *old_crtc_state)
  11124. {
  11125. struct drm_device *dev = crtc->dev;
  11126. struct drm_i915_private *dev_priv = to_i915(dev);
  11127. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11128. struct intel_crtc_state *intel_cstate =
  11129. to_intel_crtc_state(crtc->state);
  11130. struct intel_crtc_state *old_intel_cstate =
  11131. to_intel_crtc_state(old_crtc_state);
  11132. struct intel_atomic_state *old_intel_state =
  11133. to_intel_atomic_state(old_crtc_state->state);
  11134. bool modeset = needs_modeset(crtc->state);
  11135. if (!modeset &&
  11136. (intel_cstate->base.color_mgmt_changed ||
  11137. intel_cstate->update_pipe)) {
  11138. intel_color_set_csc(crtc->state);
  11139. intel_color_load_luts(crtc->state);
  11140. }
  11141. /* Perform vblank evasion around commit operation */
  11142. intel_pipe_update_start(intel_crtc);
  11143. if (modeset)
  11144. goto out;
  11145. if (intel_cstate->update_pipe)
  11146. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11147. else if (INTEL_GEN(dev_priv) >= 9)
  11148. skl_detach_scalers(intel_crtc);
  11149. out:
  11150. if (dev_priv->display.atomic_update_watermarks)
  11151. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11152. intel_cstate);
  11153. }
  11154. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11155. struct drm_crtc_state *old_crtc_state)
  11156. {
  11157. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11158. intel_pipe_update_end(intel_crtc, NULL);
  11159. }
  11160. /**
  11161. * intel_plane_destroy - destroy a plane
  11162. * @plane: plane to destroy
  11163. *
  11164. * Common destruction function for all types of planes (primary, cursor,
  11165. * sprite).
  11166. */
  11167. void intel_plane_destroy(struct drm_plane *plane)
  11168. {
  11169. drm_plane_cleanup(plane);
  11170. kfree(to_intel_plane(plane));
  11171. }
  11172. const struct drm_plane_funcs intel_plane_funcs = {
  11173. .update_plane = drm_atomic_helper_update_plane,
  11174. .disable_plane = drm_atomic_helper_disable_plane,
  11175. .destroy = intel_plane_destroy,
  11176. .set_property = drm_atomic_helper_plane_set_property,
  11177. .atomic_get_property = intel_plane_atomic_get_property,
  11178. .atomic_set_property = intel_plane_atomic_set_property,
  11179. .atomic_duplicate_state = intel_plane_duplicate_state,
  11180. .atomic_destroy_state = intel_plane_destroy_state,
  11181. };
  11182. static int
  11183. intel_legacy_cursor_update(struct drm_plane *plane,
  11184. struct drm_crtc *crtc,
  11185. struct drm_framebuffer *fb,
  11186. int crtc_x, int crtc_y,
  11187. unsigned int crtc_w, unsigned int crtc_h,
  11188. uint32_t src_x, uint32_t src_y,
  11189. uint32_t src_w, uint32_t src_h,
  11190. struct drm_modeset_acquire_ctx *ctx)
  11191. {
  11192. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11193. int ret;
  11194. struct drm_plane_state *old_plane_state, *new_plane_state;
  11195. struct intel_plane *intel_plane = to_intel_plane(plane);
  11196. struct drm_framebuffer *old_fb;
  11197. struct drm_crtc_state *crtc_state = crtc->state;
  11198. struct i915_vma *old_vma;
  11199. /*
  11200. * When crtc is inactive or there is a modeset pending,
  11201. * wait for it to complete in the slowpath
  11202. */
  11203. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11204. to_intel_crtc_state(crtc_state)->update_pipe)
  11205. goto slow;
  11206. old_plane_state = plane->state;
  11207. /*
  11208. * If any parameters change that may affect watermarks,
  11209. * take the slowpath. Only changing fb or position should be
  11210. * in the fastpath.
  11211. */
  11212. if (old_plane_state->crtc != crtc ||
  11213. old_plane_state->src_w != src_w ||
  11214. old_plane_state->src_h != src_h ||
  11215. old_plane_state->crtc_w != crtc_w ||
  11216. old_plane_state->crtc_h != crtc_h ||
  11217. !old_plane_state->fb != !fb)
  11218. goto slow;
  11219. new_plane_state = intel_plane_duplicate_state(plane);
  11220. if (!new_plane_state)
  11221. return -ENOMEM;
  11222. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11223. new_plane_state->src_x = src_x;
  11224. new_plane_state->src_y = src_y;
  11225. new_plane_state->src_w = src_w;
  11226. new_plane_state->src_h = src_h;
  11227. new_plane_state->crtc_x = crtc_x;
  11228. new_plane_state->crtc_y = crtc_y;
  11229. new_plane_state->crtc_w = crtc_w;
  11230. new_plane_state->crtc_h = crtc_h;
  11231. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11232. to_intel_plane_state(new_plane_state));
  11233. if (ret)
  11234. goto out_free;
  11235. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11236. if (ret)
  11237. goto out_free;
  11238. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11239. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11240. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11241. if (ret) {
  11242. DRM_DEBUG_KMS("failed to attach phys object\n");
  11243. goto out_unlock;
  11244. }
  11245. } else {
  11246. struct i915_vma *vma;
  11247. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11248. if (IS_ERR(vma)) {
  11249. DRM_DEBUG_KMS("failed to pin object\n");
  11250. ret = PTR_ERR(vma);
  11251. goto out_unlock;
  11252. }
  11253. to_intel_plane_state(new_plane_state)->vma = vma;
  11254. }
  11255. old_fb = old_plane_state->fb;
  11256. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11257. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11258. intel_plane->frontbuffer_bit);
  11259. /* Swap plane state */
  11260. new_plane_state->fence = old_plane_state->fence;
  11261. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11262. new_plane_state->fence = NULL;
  11263. new_plane_state->fb = old_fb;
  11264. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11265. if (plane->state->visible) {
  11266. trace_intel_update_plane(plane, to_intel_crtc(crtc));
  11267. intel_plane->update_plane(plane,
  11268. to_intel_crtc_state(crtc->state),
  11269. to_intel_plane_state(plane->state));
  11270. } else {
  11271. trace_intel_disable_plane(plane, to_intel_crtc(crtc));
  11272. intel_plane->disable_plane(plane, crtc);
  11273. }
  11274. intel_cleanup_plane_fb(plane, new_plane_state);
  11275. out_unlock:
  11276. mutex_unlock(&dev_priv->drm.struct_mutex);
  11277. out_free:
  11278. intel_plane_destroy_state(plane, new_plane_state);
  11279. return ret;
  11280. slow:
  11281. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11282. crtc_x, crtc_y, crtc_w, crtc_h,
  11283. src_x, src_y, src_w, src_h, ctx);
  11284. }
  11285. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11286. .update_plane = intel_legacy_cursor_update,
  11287. .disable_plane = drm_atomic_helper_disable_plane,
  11288. .destroy = intel_plane_destroy,
  11289. .set_property = drm_atomic_helper_plane_set_property,
  11290. .atomic_get_property = intel_plane_atomic_get_property,
  11291. .atomic_set_property = intel_plane_atomic_set_property,
  11292. .atomic_duplicate_state = intel_plane_duplicate_state,
  11293. .atomic_destroy_state = intel_plane_destroy_state,
  11294. };
  11295. static struct intel_plane *
  11296. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11297. {
  11298. struct intel_plane *primary = NULL;
  11299. struct intel_plane_state *state = NULL;
  11300. const uint32_t *intel_primary_formats;
  11301. unsigned int supported_rotations;
  11302. unsigned int num_formats;
  11303. int ret;
  11304. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11305. if (!primary) {
  11306. ret = -ENOMEM;
  11307. goto fail;
  11308. }
  11309. state = intel_create_plane_state(&primary->base);
  11310. if (!state) {
  11311. ret = -ENOMEM;
  11312. goto fail;
  11313. }
  11314. primary->base.state = &state->base;
  11315. primary->can_scale = false;
  11316. primary->max_downscale = 1;
  11317. if (INTEL_GEN(dev_priv) >= 9) {
  11318. primary->can_scale = true;
  11319. state->scaler_id = -1;
  11320. }
  11321. primary->pipe = pipe;
  11322. /*
  11323. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11324. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11325. */
  11326. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11327. primary->plane = (enum plane) !pipe;
  11328. else
  11329. primary->plane = (enum plane) pipe;
  11330. primary->id = PLANE_PRIMARY;
  11331. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11332. primary->check_plane = intel_check_primary_plane;
  11333. if (INTEL_GEN(dev_priv) >= 9) {
  11334. intel_primary_formats = skl_primary_formats;
  11335. num_formats = ARRAY_SIZE(skl_primary_formats);
  11336. primary->update_plane = skylake_update_primary_plane;
  11337. primary->disable_plane = skylake_disable_primary_plane;
  11338. } else if (INTEL_GEN(dev_priv) >= 4) {
  11339. intel_primary_formats = i965_primary_formats;
  11340. num_formats = ARRAY_SIZE(i965_primary_formats);
  11341. primary->update_plane = i9xx_update_primary_plane;
  11342. primary->disable_plane = i9xx_disable_primary_plane;
  11343. } else {
  11344. intel_primary_formats = i8xx_primary_formats;
  11345. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11346. primary->update_plane = i9xx_update_primary_plane;
  11347. primary->disable_plane = i9xx_disable_primary_plane;
  11348. }
  11349. if (INTEL_GEN(dev_priv) >= 9)
  11350. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11351. 0, &intel_plane_funcs,
  11352. intel_primary_formats, num_formats,
  11353. DRM_PLANE_TYPE_PRIMARY,
  11354. "plane 1%c", pipe_name(pipe));
  11355. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11356. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11357. 0, &intel_plane_funcs,
  11358. intel_primary_formats, num_formats,
  11359. DRM_PLANE_TYPE_PRIMARY,
  11360. "primary %c", pipe_name(pipe));
  11361. else
  11362. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11363. 0, &intel_plane_funcs,
  11364. intel_primary_formats, num_formats,
  11365. DRM_PLANE_TYPE_PRIMARY,
  11366. "plane %c", plane_name(primary->plane));
  11367. if (ret)
  11368. goto fail;
  11369. if (INTEL_GEN(dev_priv) >= 9) {
  11370. supported_rotations =
  11371. DRM_ROTATE_0 | DRM_ROTATE_90 |
  11372. DRM_ROTATE_180 | DRM_ROTATE_270;
  11373. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11374. supported_rotations =
  11375. DRM_ROTATE_0 | DRM_ROTATE_180 |
  11376. DRM_REFLECT_X;
  11377. } else if (INTEL_GEN(dev_priv) >= 4) {
  11378. supported_rotations =
  11379. DRM_ROTATE_0 | DRM_ROTATE_180;
  11380. } else {
  11381. supported_rotations = DRM_ROTATE_0;
  11382. }
  11383. if (INTEL_GEN(dev_priv) >= 4)
  11384. drm_plane_create_rotation_property(&primary->base,
  11385. DRM_ROTATE_0,
  11386. supported_rotations);
  11387. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11388. return primary;
  11389. fail:
  11390. kfree(state);
  11391. kfree(primary);
  11392. return ERR_PTR(ret);
  11393. }
  11394. static int
  11395. intel_check_cursor_plane(struct drm_plane *plane,
  11396. struct intel_crtc_state *crtc_state,
  11397. struct intel_plane_state *state)
  11398. {
  11399. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11400. struct drm_framebuffer *fb = state->base.fb;
  11401. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11402. enum pipe pipe = to_intel_plane(plane)->pipe;
  11403. unsigned stride;
  11404. int ret;
  11405. ret = drm_plane_helper_check_state(&state->base,
  11406. &state->clip,
  11407. DRM_PLANE_HELPER_NO_SCALING,
  11408. DRM_PLANE_HELPER_NO_SCALING,
  11409. true, true);
  11410. if (ret)
  11411. return ret;
  11412. /* if we want to turn off the cursor ignore width and height */
  11413. if (!obj)
  11414. return 0;
  11415. /* Check for which cursor types we support */
  11416. if (!cursor_size_ok(dev_priv, state->base.crtc_w,
  11417. state->base.crtc_h)) {
  11418. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11419. state->base.crtc_w, state->base.crtc_h);
  11420. return -EINVAL;
  11421. }
  11422. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11423. if (obj->base.size < stride * state->base.crtc_h) {
  11424. DRM_DEBUG_KMS("buffer is too small\n");
  11425. return -ENOMEM;
  11426. }
  11427. if (fb->modifier != DRM_FORMAT_MOD_LINEAR) {
  11428. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11429. return -EINVAL;
  11430. }
  11431. /*
  11432. * There's something wrong with the cursor on CHV pipe C.
  11433. * If it straddles the left edge of the screen then
  11434. * moving it away from the edge or disabling it often
  11435. * results in a pipe underrun, and often that can lead to
  11436. * dead pipe (constant underrun reported, and it scans
  11437. * out just a solid color). To recover from that, the
  11438. * display power well must be turned off and on again.
  11439. * Refuse the put the cursor into that compromised position.
  11440. */
  11441. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_C &&
  11442. state->base.visible && state->base.crtc_x < 0) {
  11443. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11444. return -EINVAL;
  11445. }
  11446. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  11447. state->ctl = i845_cursor_ctl(crtc_state, state);
  11448. else
  11449. state->ctl = i9xx_cursor_ctl(crtc_state, state);
  11450. return 0;
  11451. }
  11452. static void
  11453. intel_disable_cursor_plane(struct drm_plane *plane,
  11454. struct drm_crtc *crtc)
  11455. {
  11456. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11457. intel_crtc->cursor_addr = 0;
  11458. intel_crtc_update_cursor(crtc, NULL);
  11459. }
  11460. static void
  11461. intel_update_cursor_plane(struct drm_plane *plane,
  11462. const struct intel_crtc_state *crtc_state,
  11463. const struct intel_plane_state *state)
  11464. {
  11465. struct drm_crtc *crtc = crtc_state->base.crtc;
  11466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11467. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11468. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11469. uint32_t addr;
  11470. if (!obj)
  11471. addr = 0;
  11472. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  11473. addr = intel_plane_ggtt_offset(state);
  11474. else
  11475. addr = obj->phys_handle->busaddr;
  11476. intel_crtc->cursor_addr = addr;
  11477. intel_crtc_update_cursor(crtc, state);
  11478. }
  11479. static struct intel_plane *
  11480. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11481. {
  11482. struct intel_plane *cursor = NULL;
  11483. struct intel_plane_state *state = NULL;
  11484. int ret;
  11485. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11486. if (!cursor) {
  11487. ret = -ENOMEM;
  11488. goto fail;
  11489. }
  11490. state = intel_create_plane_state(&cursor->base);
  11491. if (!state) {
  11492. ret = -ENOMEM;
  11493. goto fail;
  11494. }
  11495. cursor->base.state = &state->base;
  11496. cursor->can_scale = false;
  11497. cursor->max_downscale = 1;
  11498. cursor->pipe = pipe;
  11499. cursor->plane = pipe;
  11500. cursor->id = PLANE_CURSOR;
  11501. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11502. cursor->check_plane = intel_check_cursor_plane;
  11503. cursor->update_plane = intel_update_cursor_plane;
  11504. cursor->disable_plane = intel_disable_cursor_plane;
  11505. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11506. 0, &intel_cursor_plane_funcs,
  11507. intel_cursor_formats,
  11508. ARRAY_SIZE(intel_cursor_formats),
  11509. DRM_PLANE_TYPE_CURSOR,
  11510. "cursor %c", pipe_name(pipe));
  11511. if (ret)
  11512. goto fail;
  11513. if (INTEL_GEN(dev_priv) >= 4)
  11514. drm_plane_create_rotation_property(&cursor->base,
  11515. DRM_ROTATE_0,
  11516. DRM_ROTATE_0 |
  11517. DRM_ROTATE_180);
  11518. if (INTEL_GEN(dev_priv) >= 9)
  11519. state->scaler_id = -1;
  11520. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11521. return cursor;
  11522. fail:
  11523. kfree(state);
  11524. kfree(cursor);
  11525. return ERR_PTR(ret);
  11526. }
  11527. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11528. struct intel_crtc_state *crtc_state)
  11529. {
  11530. struct intel_crtc_scaler_state *scaler_state =
  11531. &crtc_state->scaler_state;
  11532. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11533. int i;
  11534. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11535. if (!crtc->num_scalers)
  11536. return;
  11537. for (i = 0; i < crtc->num_scalers; i++) {
  11538. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11539. scaler->in_use = 0;
  11540. scaler->mode = PS_SCALER_MODE_DYN;
  11541. }
  11542. scaler_state->scaler_id = -1;
  11543. }
  11544. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11545. {
  11546. struct intel_crtc *intel_crtc;
  11547. struct intel_crtc_state *crtc_state = NULL;
  11548. struct intel_plane *primary = NULL;
  11549. struct intel_plane *cursor = NULL;
  11550. int sprite, ret;
  11551. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11552. if (!intel_crtc)
  11553. return -ENOMEM;
  11554. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11555. if (!crtc_state) {
  11556. ret = -ENOMEM;
  11557. goto fail;
  11558. }
  11559. intel_crtc->config = crtc_state;
  11560. intel_crtc->base.state = &crtc_state->base;
  11561. crtc_state->base.crtc = &intel_crtc->base;
  11562. primary = intel_primary_plane_create(dev_priv, pipe);
  11563. if (IS_ERR(primary)) {
  11564. ret = PTR_ERR(primary);
  11565. goto fail;
  11566. }
  11567. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11568. for_each_sprite(dev_priv, pipe, sprite) {
  11569. struct intel_plane *plane;
  11570. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11571. if (IS_ERR(plane)) {
  11572. ret = PTR_ERR(plane);
  11573. goto fail;
  11574. }
  11575. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11576. }
  11577. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11578. if (IS_ERR(cursor)) {
  11579. ret = PTR_ERR(cursor);
  11580. goto fail;
  11581. }
  11582. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11583. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11584. &primary->base, &cursor->base,
  11585. &intel_crtc_funcs,
  11586. "pipe %c", pipe_name(pipe));
  11587. if (ret)
  11588. goto fail;
  11589. intel_crtc->pipe = pipe;
  11590. intel_crtc->plane = primary->plane;
  11591. intel_crtc->cursor_base = ~0;
  11592. intel_crtc->cursor_cntl = ~0;
  11593. intel_crtc->cursor_size = ~0;
  11594. /* initialize shared scalers */
  11595. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11596. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11597. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11598. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11599. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11600. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11601. intel_color_init(&intel_crtc->base);
  11602. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11603. return 0;
  11604. fail:
  11605. /*
  11606. * drm_mode_config_cleanup() will free up any
  11607. * crtcs/planes already initialized.
  11608. */
  11609. kfree(crtc_state);
  11610. kfree(intel_crtc);
  11611. return ret;
  11612. }
  11613. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11614. {
  11615. struct drm_device *dev = connector->base.dev;
  11616. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11617. if (!connector->base.state->crtc)
  11618. return INVALID_PIPE;
  11619. return to_intel_crtc(connector->base.state->crtc)->pipe;
  11620. }
  11621. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11622. struct drm_file *file)
  11623. {
  11624. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11625. struct drm_crtc *drmmode_crtc;
  11626. struct intel_crtc *crtc;
  11627. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11628. if (!drmmode_crtc)
  11629. return -ENOENT;
  11630. crtc = to_intel_crtc(drmmode_crtc);
  11631. pipe_from_crtc_id->pipe = crtc->pipe;
  11632. return 0;
  11633. }
  11634. static int intel_encoder_clones(struct intel_encoder *encoder)
  11635. {
  11636. struct drm_device *dev = encoder->base.dev;
  11637. struct intel_encoder *source_encoder;
  11638. int index_mask = 0;
  11639. int entry = 0;
  11640. for_each_intel_encoder(dev, source_encoder) {
  11641. if (encoders_cloneable(encoder, source_encoder))
  11642. index_mask |= (1 << entry);
  11643. entry++;
  11644. }
  11645. return index_mask;
  11646. }
  11647. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11648. {
  11649. if (!IS_MOBILE(dev_priv))
  11650. return false;
  11651. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11652. return false;
  11653. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11654. return false;
  11655. return true;
  11656. }
  11657. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11658. {
  11659. if (INTEL_GEN(dev_priv) >= 9)
  11660. return false;
  11661. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11662. return false;
  11663. if (IS_CHERRYVIEW(dev_priv))
  11664. return false;
  11665. if (HAS_PCH_LPT_H(dev_priv) &&
  11666. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11667. return false;
  11668. /* DDI E can't be used if DDI A requires 4 lanes */
  11669. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11670. return false;
  11671. if (!dev_priv->vbt.int_crt_support)
  11672. return false;
  11673. return true;
  11674. }
  11675. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11676. {
  11677. int pps_num;
  11678. int pps_idx;
  11679. if (HAS_DDI(dev_priv))
  11680. return;
  11681. /*
  11682. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11683. * everywhere where registers can be write protected.
  11684. */
  11685. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11686. pps_num = 2;
  11687. else
  11688. pps_num = 1;
  11689. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11690. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11691. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11692. I915_WRITE(PP_CONTROL(pps_idx), val);
  11693. }
  11694. }
  11695. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11696. {
  11697. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11698. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11699. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11700. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11701. else
  11702. dev_priv->pps_mmio_base = PPS_BASE;
  11703. intel_pps_unlock_regs_wa(dev_priv);
  11704. }
  11705. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11706. {
  11707. struct intel_encoder *encoder;
  11708. bool dpd_is_edp = false;
  11709. intel_pps_init(dev_priv);
  11710. /*
  11711. * intel_edp_init_connector() depends on this completing first, to
  11712. * prevent the registeration of both eDP and LVDS and the incorrect
  11713. * sharing of the PPS.
  11714. */
  11715. intel_lvds_init(dev_priv);
  11716. if (intel_crt_present(dev_priv))
  11717. intel_crt_init(dev_priv);
  11718. if (IS_GEN9_LP(dev_priv)) {
  11719. /*
  11720. * FIXME: Broxton doesn't support port detection via the
  11721. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11722. * detect the ports.
  11723. */
  11724. intel_ddi_init(dev_priv, PORT_A);
  11725. intel_ddi_init(dev_priv, PORT_B);
  11726. intel_ddi_init(dev_priv, PORT_C);
  11727. intel_dsi_init(dev_priv);
  11728. } else if (HAS_DDI(dev_priv)) {
  11729. int found;
  11730. /*
  11731. * Haswell uses DDI functions to detect digital outputs.
  11732. * On SKL pre-D0 the strap isn't connected, so we assume
  11733. * it's there.
  11734. */
  11735. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11736. /* WaIgnoreDDIAStrap: skl */
  11737. if (found || IS_GEN9_BC(dev_priv))
  11738. intel_ddi_init(dev_priv, PORT_A);
  11739. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11740. * register */
  11741. found = I915_READ(SFUSE_STRAP);
  11742. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11743. intel_ddi_init(dev_priv, PORT_B);
  11744. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11745. intel_ddi_init(dev_priv, PORT_C);
  11746. if (found & SFUSE_STRAP_DDID_DETECTED)
  11747. intel_ddi_init(dev_priv, PORT_D);
  11748. /*
  11749. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11750. */
  11751. if (IS_GEN9_BC(dev_priv) &&
  11752. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11753. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11754. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11755. intel_ddi_init(dev_priv, PORT_E);
  11756. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11757. int found;
  11758. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11759. if (has_edp_a(dev_priv))
  11760. intel_dp_init(dev_priv, DP_A, PORT_A);
  11761. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11762. /* PCH SDVOB multiplex with HDMIB */
  11763. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11764. if (!found)
  11765. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11766. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11767. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11768. }
  11769. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11770. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11771. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11772. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11773. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11774. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11775. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11776. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11777. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11778. bool has_edp, has_port;
  11779. /*
  11780. * The DP_DETECTED bit is the latched state of the DDC
  11781. * SDA pin at boot. However since eDP doesn't require DDC
  11782. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11783. * eDP ports may have been muxed to an alternate function.
  11784. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11785. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11786. * detect eDP ports.
  11787. *
  11788. * Sadly the straps seem to be missing sometimes even for HDMI
  11789. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11790. * and VBT for the presence of the port. Additionally we can't
  11791. * trust the port type the VBT declares as we've seen at least
  11792. * HDMI ports that the VBT claim are DP or eDP.
  11793. */
  11794. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11795. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11796. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11797. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11798. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11799. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11800. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11801. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11802. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11803. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11804. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11805. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11806. if (IS_CHERRYVIEW(dev_priv)) {
  11807. /*
  11808. * eDP not supported on port D,
  11809. * so no need to worry about it
  11810. */
  11811. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11812. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11813. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11814. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11815. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11816. }
  11817. intel_dsi_init(dev_priv);
  11818. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11819. bool found = false;
  11820. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11821. DRM_DEBUG_KMS("probing SDVOB\n");
  11822. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11823. if (!found && IS_G4X(dev_priv)) {
  11824. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11825. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11826. }
  11827. if (!found && IS_G4X(dev_priv))
  11828. intel_dp_init(dev_priv, DP_B, PORT_B);
  11829. }
  11830. /* Before G4X SDVOC doesn't have its own detect register */
  11831. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11832. DRM_DEBUG_KMS("probing SDVOC\n");
  11833. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11834. }
  11835. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11836. if (IS_G4X(dev_priv)) {
  11837. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11838. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11839. }
  11840. if (IS_G4X(dev_priv))
  11841. intel_dp_init(dev_priv, DP_C, PORT_C);
  11842. }
  11843. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11844. intel_dp_init(dev_priv, DP_D, PORT_D);
  11845. } else if (IS_GEN2(dev_priv))
  11846. intel_dvo_init(dev_priv);
  11847. if (SUPPORTS_TV(dev_priv))
  11848. intel_tv_init(dev_priv);
  11849. intel_psr_init(dev_priv);
  11850. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11851. encoder->base.possible_crtcs = encoder->crtc_mask;
  11852. encoder->base.possible_clones =
  11853. intel_encoder_clones(encoder);
  11854. }
  11855. intel_init_pch_refclk(dev_priv);
  11856. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11857. }
  11858. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11859. {
  11860. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11861. drm_framebuffer_cleanup(fb);
  11862. i915_gem_object_lock(intel_fb->obj);
  11863. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11864. i915_gem_object_unlock(intel_fb->obj);
  11865. i915_gem_object_put(intel_fb->obj);
  11866. kfree(intel_fb);
  11867. }
  11868. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11869. struct drm_file *file,
  11870. unsigned int *handle)
  11871. {
  11872. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11873. struct drm_i915_gem_object *obj = intel_fb->obj;
  11874. if (obj->userptr.mm) {
  11875. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11876. return -EINVAL;
  11877. }
  11878. return drm_gem_handle_create(file, &obj->base, handle);
  11879. }
  11880. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11881. struct drm_file *file,
  11882. unsigned flags, unsigned color,
  11883. struct drm_clip_rect *clips,
  11884. unsigned num_clips)
  11885. {
  11886. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11887. i915_gem_object_flush_if_display(obj);
  11888. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11889. return 0;
  11890. }
  11891. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11892. .destroy = intel_user_framebuffer_destroy,
  11893. .create_handle = intel_user_framebuffer_create_handle,
  11894. .dirty = intel_user_framebuffer_dirty,
  11895. };
  11896. static
  11897. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11898. uint64_t fb_modifier, uint32_t pixel_format)
  11899. {
  11900. u32 gen = INTEL_GEN(dev_priv);
  11901. if (gen >= 9) {
  11902. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11903. /* "The stride in bytes must not exceed the of the size of 8K
  11904. * pixels and 32K bytes."
  11905. */
  11906. return min(8192 * cpp, 32768);
  11907. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11908. return 32*1024;
  11909. } else if (gen >= 4) {
  11910. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11911. return 16*1024;
  11912. else
  11913. return 32*1024;
  11914. } else if (gen >= 3) {
  11915. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11916. return 8*1024;
  11917. else
  11918. return 16*1024;
  11919. } else {
  11920. /* XXX DSPC is limited to 4k tiled */
  11921. return 8*1024;
  11922. }
  11923. }
  11924. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11925. struct drm_i915_gem_object *obj,
  11926. struct drm_mode_fb_cmd2 *mode_cmd)
  11927. {
  11928. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11929. struct drm_format_name_buf format_name;
  11930. u32 pitch_limit, stride_alignment;
  11931. unsigned int tiling, stride;
  11932. int ret = -EINVAL;
  11933. i915_gem_object_lock(obj);
  11934. obj->framebuffer_references++;
  11935. tiling = i915_gem_object_get_tiling(obj);
  11936. stride = i915_gem_object_get_stride(obj);
  11937. i915_gem_object_unlock(obj);
  11938. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11939. /*
  11940. * If there's a fence, enforce that
  11941. * the fb modifier and tiling mode match.
  11942. */
  11943. if (tiling != I915_TILING_NONE &&
  11944. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11945. DRM_DEBUG_KMS("tiling_mode doesn't match fb modifier\n");
  11946. goto err;
  11947. }
  11948. } else {
  11949. if (tiling == I915_TILING_X) {
  11950. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11951. } else if (tiling == I915_TILING_Y) {
  11952. DRM_DEBUG_KMS("No Y tiling for legacy addfb\n");
  11953. goto err;
  11954. }
  11955. }
  11956. /* Passed in modifier sanity checking. */
  11957. switch (mode_cmd->modifier[0]) {
  11958. case I915_FORMAT_MOD_Y_TILED:
  11959. case I915_FORMAT_MOD_Yf_TILED:
  11960. if (INTEL_GEN(dev_priv) < 9) {
  11961. DRM_DEBUG_KMS("Unsupported tiling 0x%llx!\n",
  11962. mode_cmd->modifier[0]);
  11963. goto err;
  11964. }
  11965. case DRM_FORMAT_MOD_LINEAR:
  11966. case I915_FORMAT_MOD_X_TILED:
  11967. break;
  11968. default:
  11969. DRM_DEBUG_KMS("Unsupported fb modifier 0x%llx!\n",
  11970. mode_cmd->modifier[0]);
  11971. goto err;
  11972. }
  11973. /*
  11974. * gen2/3 display engine uses the fence if present,
  11975. * so the tiling mode must match the fb modifier exactly.
  11976. */
  11977. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11978. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11979. DRM_DEBUG_KMS("tiling_mode must match fb modifier exactly on gen2/3\n");
  11980. goto err;
  11981. }
  11982. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11983. mode_cmd->pixel_format);
  11984. if (mode_cmd->pitches[0] > pitch_limit) {
  11985. DRM_DEBUG_KMS("%s pitch (%u) must be at most %d\n",
  11986. mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
  11987. "tiled" : "linear",
  11988. mode_cmd->pitches[0], pitch_limit);
  11989. goto err;
  11990. }
  11991. /*
  11992. * If there's a fence, enforce that
  11993. * the fb pitch and fence stride match.
  11994. */
  11995. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11996. DRM_DEBUG_KMS("pitch (%d) must match tiling stride (%d)\n",
  11997. mode_cmd->pitches[0], stride);
  11998. goto err;
  11999. }
  12000. /* Reject formats not supported by any plane early. */
  12001. switch (mode_cmd->pixel_format) {
  12002. case DRM_FORMAT_C8:
  12003. case DRM_FORMAT_RGB565:
  12004. case DRM_FORMAT_XRGB8888:
  12005. case DRM_FORMAT_ARGB8888:
  12006. break;
  12007. case DRM_FORMAT_XRGB1555:
  12008. if (INTEL_GEN(dev_priv) > 3) {
  12009. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12010. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12011. goto err;
  12012. }
  12013. break;
  12014. case DRM_FORMAT_ABGR8888:
  12015. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  12016. INTEL_GEN(dev_priv) < 9) {
  12017. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12018. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12019. goto err;
  12020. }
  12021. break;
  12022. case DRM_FORMAT_XBGR8888:
  12023. case DRM_FORMAT_XRGB2101010:
  12024. case DRM_FORMAT_XBGR2101010:
  12025. if (INTEL_GEN(dev_priv) < 4) {
  12026. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12027. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12028. goto err;
  12029. }
  12030. break;
  12031. case DRM_FORMAT_ABGR2101010:
  12032. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12033. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12034. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12035. goto err;
  12036. }
  12037. break;
  12038. case DRM_FORMAT_YUYV:
  12039. case DRM_FORMAT_UYVY:
  12040. case DRM_FORMAT_YVYU:
  12041. case DRM_FORMAT_VYUY:
  12042. if (INTEL_GEN(dev_priv) < 5) {
  12043. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12044. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12045. goto err;
  12046. }
  12047. break;
  12048. default:
  12049. DRM_DEBUG_KMS("unsupported pixel format: %s\n",
  12050. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12051. goto err;
  12052. }
  12053. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12054. if (mode_cmd->offsets[0] != 0)
  12055. goto err;
  12056. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12057. &intel_fb->base, mode_cmd);
  12058. stride_alignment = intel_fb_stride_alignment(&intel_fb->base, 0);
  12059. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  12060. DRM_DEBUG_KMS("pitch (%d) must be at least %u byte aligned\n",
  12061. mode_cmd->pitches[0], stride_alignment);
  12062. goto err;
  12063. }
  12064. intel_fb->obj = obj;
  12065. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12066. if (ret)
  12067. goto err;
  12068. ret = drm_framebuffer_init(obj->base.dev,
  12069. &intel_fb->base,
  12070. &intel_fb_funcs);
  12071. if (ret) {
  12072. DRM_ERROR("framebuffer init failed %d\n", ret);
  12073. goto err;
  12074. }
  12075. return 0;
  12076. err:
  12077. i915_gem_object_lock(obj);
  12078. obj->framebuffer_references--;
  12079. i915_gem_object_unlock(obj);
  12080. return ret;
  12081. }
  12082. static struct drm_framebuffer *
  12083. intel_user_framebuffer_create(struct drm_device *dev,
  12084. struct drm_file *filp,
  12085. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12086. {
  12087. struct drm_framebuffer *fb;
  12088. struct drm_i915_gem_object *obj;
  12089. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12090. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12091. if (!obj)
  12092. return ERR_PTR(-ENOENT);
  12093. fb = intel_framebuffer_create(obj, &mode_cmd);
  12094. if (IS_ERR(fb))
  12095. i915_gem_object_put(obj);
  12096. return fb;
  12097. }
  12098. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12099. {
  12100. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12101. drm_atomic_state_default_release(state);
  12102. i915_sw_fence_fini(&intel_state->commit_ready);
  12103. kfree(state);
  12104. }
  12105. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12106. .fb_create = intel_user_framebuffer_create,
  12107. .output_poll_changed = intel_fbdev_output_poll_changed,
  12108. .atomic_check = intel_atomic_check,
  12109. .atomic_commit = intel_atomic_commit,
  12110. .atomic_state_alloc = intel_atomic_state_alloc,
  12111. .atomic_state_clear = intel_atomic_state_clear,
  12112. .atomic_state_free = intel_atomic_state_free,
  12113. };
  12114. /**
  12115. * intel_init_display_hooks - initialize the display modesetting hooks
  12116. * @dev_priv: device private
  12117. */
  12118. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12119. {
  12120. intel_init_cdclk_hooks(dev_priv);
  12121. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12122. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12123. dev_priv->display.get_initial_plane_config =
  12124. skylake_get_initial_plane_config;
  12125. dev_priv->display.crtc_compute_clock =
  12126. haswell_crtc_compute_clock;
  12127. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12128. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12129. } else if (HAS_DDI(dev_priv)) {
  12130. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12131. dev_priv->display.get_initial_plane_config =
  12132. ironlake_get_initial_plane_config;
  12133. dev_priv->display.crtc_compute_clock =
  12134. haswell_crtc_compute_clock;
  12135. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12136. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12137. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12138. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12139. dev_priv->display.get_initial_plane_config =
  12140. ironlake_get_initial_plane_config;
  12141. dev_priv->display.crtc_compute_clock =
  12142. ironlake_crtc_compute_clock;
  12143. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12144. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12145. } else if (IS_CHERRYVIEW(dev_priv)) {
  12146. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12147. dev_priv->display.get_initial_plane_config =
  12148. i9xx_get_initial_plane_config;
  12149. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12150. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12151. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12152. } else if (IS_VALLEYVIEW(dev_priv)) {
  12153. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12154. dev_priv->display.get_initial_plane_config =
  12155. i9xx_get_initial_plane_config;
  12156. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12157. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12158. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12159. } else if (IS_G4X(dev_priv)) {
  12160. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12161. dev_priv->display.get_initial_plane_config =
  12162. i9xx_get_initial_plane_config;
  12163. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12164. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12165. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12166. } else if (IS_PINEVIEW(dev_priv)) {
  12167. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12168. dev_priv->display.get_initial_plane_config =
  12169. i9xx_get_initial_plane_config;
  12170. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12171. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12172. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12173. } else if (!IS_GEN2(dev_priv)) {
  12174. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12175. dev_priv->display.get_initial_plane_config =
  12176. i9xx_get_initial_plane_config;
  12177. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12178. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12179. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12180. } else {
  12181. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12182. dev_priv->display.get_initial_plane_config =
  12183. i9xx_get_initial_plane_config;
  12184. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12185. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12186. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12187. }
  12188. if (IS_GEN5(dev_priv)) {
  12189. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12190. } else if (IS_GEN6(dev_priv)) {
  12191. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12192. } else if (IS_IVYBRIDGE(dev_priv)) {
  12193. /* FIXME: detect B0+ stepping and use auto training */
  12194. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12195. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12196. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12197. }
  12198. if (dev_priv->info.gen >= 9)
  12199. dev_priv->display.update_crtcs = skl_update_crtcs;
  12200. else
  12201. dev_priv->display.update_crtcs = intel_update_crtcs;
  12202. switch (INTEL_INFO(dev_priv)->gen) {
  12203. case 2:
  12204. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12205. break;
  12206. case 3:
  12207. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12208. break;
  12209. case 4:
  12210. case 5:
  12211. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12212. break;
  12213. case 6:
  12214. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12215. break;
  12216. case 7:
  12217. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12218. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12219. break;
  12220. case 9:
  12221. /* Drop through - unsupported since execlist only. */
  12222. default:
  12223. /* Default just returns -ENODEV to indicate unsupported */
  12224. dev_priv->display.queue_flip = intel_default_queue_flip;
  12225. }
  12226. }
  12227. /*
  12228. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12229. * resume, or other times. This quirk makes sure that's the case for
  12230. * affected systems.
  12231. */
  12232. static void quirk_pipea_force(struct drm_device *dev)
  12233. {
  12234. struct drm_i915_private *dev_priv = to_i915(dev);
  12235. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12236. DRM_INFO("applying pipe a force quirk\n");
  12237. }
  12238. static void quirk_pipeb_force(struct drm_device *dev)
  12239. {
  12240. struct drm_i915_private *dev_priv = to_i915(dev);
  12241. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12242. DRM_INFO("applying pipe b force quirk\n");
  12243. }
  12244. /*
  12245. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12246. */
  12247. static void quirk_ssc_force_disable(struct drm_device *dev)
  12248. {
  12249. struct drm_i915_private *dev_priv = to_i915(dev);
  12250. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12251. DRM_INFO("applying lvds SSC disable quirk\n");
  12252. }
  12253. /*
  12254. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12255. * brightness value
  12256. */
  12257. static void quirk_invert_brightness(struct drm_device *dev)
  12258. {
  12259. struct drm_i915_private *dev_priv = to_i915(dev);
  12260. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12261. DRM_INFO("applying inverted panel brightness quirk\n");
  12262. }
  12263. /* Some VBT's incorrectly indicate no backlight is present */
  12264. static void quirk_backlight_present(struct drm_device *dev)
  12265. {
  12266. struct drm_i915_private *dev_priv = to_i915(dev);
  12267. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12268. DRM_INFO("applying backlight present quirk\n");
  12269. }
  12270. struct intel_quirk {
  12271. int device;
  12272. int subsystem_vendor;
  12273. int subsystem_device;
  12274. void (*hook)(struct drm_device *dev);
  12275. };
  12276. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12277. struct intel_dmi_quirk {
  12278. void (*hook)(struct drm_device *dev);
  12279. const struct dmi_system_id (*dmi_id_list)[];
  12280. };
  12281. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12282. {
  12283. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12284. return 1;
  12285. }
  12286. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12287. {
  12288. .dmi_id_list = &(const struct dmi_system_id[]) {
  12289. {
  12290. .callback = intel_dmi_reverse_brightness,
  12291. .ident = "NCR Corporation",
  12292. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12293. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12294. },
  12295. },
  12296. { } /* terminating entry */
  12297. },
  12298. .hook = quirk_invert_brightness,
  12299. },
  12300. };
  12301. static struct intel_quirk intel_quirks[] = {
  12302. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12303. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12304. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12305. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12306. /* 830 needs to leave pipe A & dpll A up */
  12307. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12308. /* 830 needs to leave pipe B & dpll B up */
  12309. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12310. /* Lenovo U160 cannot use SSC on LVDS */
  12311. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12312. /* Sony Vaio Y cannot use SSC on LVDS */
  12313. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12314. /* Acer Aspire 5734Z must invert backlight brightness */
  12315. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12316. /* Acer/eMachines G725 */
  12317. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12318. /* Acer/eMachines e725 */
  12319. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12320. /* Acer/Packard Bell NCL20 */
  12321. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12322. /* Acer Aspire 4736Z */
  12323. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12324. /* Acer Aspire 5336 */
  12325. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12326. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12327. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12328. /* Acer C720 Chromebook (Core i3 4005U) */
  12329. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12330. /* Apple Macbook 2,1 (Core 2 T7400) */
  12331. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12332. /* Apple Macbook 4,1 */
  12333. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12334. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12335. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12336. /* HP Chromebook 14 (Celeron 2955U) */
  12337. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12338. /* Dell Chromebook 11 */
  12339. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12340. /* Dell Chromebook 11 (2015 version) */
  12341. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12342. };
  12343. static void intel_init_quirks(struct drm_device *dev)
  12344. {
  12345. struct pci_dev *d = dev->pdev;
  12346. int i;
  12347. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12348. struct intel_quirk *q = &intel_quirks[i];
  12349. if (d->device == q->device &&
  12350. (d->subsystem_vendor == q->subsystem_vendor ||
  12351. q->subsystem_vendor == PCI_ANY_ID) &&
  12352. (d->subsystem_device == q->subsystem_device ||
  12353. q->subsystem_device == PCI_ANY_ID))
  12354. q->hook(dev);
  12355. }
  12356. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12357. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12358. intel_dmi_quirks[i].hook(dev);
  12359. }
  12360. }
  12361. /* Disable the VGA plane that we never use */
  12362. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12363. {
  12364. struct pci_dev *pdev = dev_priv->drm.pdev;
  12365. u8 sr1;
  12366. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12367. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12368. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12369. outb(SR01, VGA_SR_INDEX);
  12370. sr1 = inb(VGA_SR_DATA);
  12371. outb(sr1 | 1<<5, VGA_SR_DATA);
  12372. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12373. udelay(300);
  12374. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12375. POSTING_READ(vga_reg);
  12376. }
  12377. void intel_modeset_init_hw(struct drm_device *dev)
  12378. {
  12379. struct drm_i915_private *dev_priv = to_i915(dev);
  12380. intel_update_cdclk(dev_priv);
  12381. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12382. intel_init_clock_gating(dev_priv);
  12383. }
  12384. /*
  12385. * Calculate what we think the watermarks should be for the state we've read
  12386. * out of the hardware and then immediately program those watermarks so that
  12387. * we ensure the hardware settings match our internal state.
  12388. *
  12389. * We can calculate what we think WM's should be by creating a duplicate of the
  12390. * current state (which was constructed during hardware readout) and running it
  12391. * through the atomic check code to calculate new watermark values in the
  12392. * state object.
  12393. */
  12394. static void sanitize_watermarks(struct drm_device *dev)
  12395. {
  12396. struct drm_i915_private *dev_priv = to_i915(dev);
  12397. struct drm_atomic_state *state;
  12398. struct intel_atomic_state *intel_state;
  12399. struct drm_crtc *crtc;
  12400. struct drm_crtc_state *cstate;
  12401. struct drm_modeset_acquire_ctx ctx;
  12402. int ret;
  12403. int i;
  12404. /* Only supported on platforms that use atomic watermark design */
  12405. if (!dev_priv->display.optimize_watermarks)
  12406. return;
  12407. /*
  12408. * We need to hold connection_mutex before calling duplicate_state so
  12409. * that the connector loop is protected.
  12410. */
  12411. drm_modeset_acquire_init(&ctx, 0);
  12412. retry:
  12413. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12414. if (ret == -EDEADLK) {
  12415. drm_modeset_backoff(&ctx);
  12416. goto retry;
  12417. } else if (WARN_ON(ret)) {
  12418. goto fail;
  12419. }
  12420. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12421. if (WARN_ON(IS_ERR(state)))
  12422. goto fail;
  12423. intel_state = to_intel_atomic_state(state);
  12424. /*
  12425. * Hardware readout is the only time we don't want to calculate
  12426. * intermediate watermarks (since we don't trust the current
  12427. * watermarks).
  12428. */
  12429. if (!HAS_GMCH_DISPLAY(dev_priv))
  12430. intel_state->skip_intermediate_wm = true;
  12431. ret = intel_atomic_check(dev, state);
  12432. if (ret) {
  12433. /*
  12434. * If we fail here, it means that the hardware appears to be
  12435. * programmed in a way that shouldn't be possible, given our
  12436. * understanding of watermark requirements. This might mean a
  12437. * mistake in the hardware readout code or a mistake in the
  12438. * watermark calculations for a given platform. Raise a WARN
  12439. * so that this is noticeable.
  12440. *
  12441. * If this actually happens, we'll have to just leave the
  12442. * BIOS-programmed watermarks untouched and hope for the best.
  12443. */
  12444. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12445. goto put_state;
  12446. }
  12447. /* Write calculated watermark values back */
  12448. for_each_new_crtc_in_state(state, crtc, cstate, i) {
  12449. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12450. cs->wm.need_postvbl_update = true;
  12451. dev_priv->display.optimize_watermarks(intel_state, cs);
  12452. }
  12453. put_state:
  12454. drm_atomic_state_put(state);
  12455. fail:
  12456. drm_modeset_drop_locks(&ctx);
  12457. drm_modeset_acquire_fini(&ctx);
  12458. }
  12459. int intel_modeset_init(struct drm_device *dev)
  12460. {
  12461. struct drm_i915_private *dev_priv = to_i915(dev);
  12462. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12463. enum pipe pipe;
  12464. struct intel_crtc *crtc;
  12465. drm_mode_config_init(dev);
  12466. dev->mode_config.min_width = 0;
  12467. dev->mode_config.min_height = 0;
  12468. dev->mode_config.preferred_depth = 24;
  12469. dev->mode_config.prefer_shadow = 1;
  12470. dev->mode_config.allow_fb_modifiers = true;
  12471. dev->mode_config.funcs = &intel_mode_funcs;
  12472. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12473. intel_atomic_helper_free_state_worker);
  12474. intel_init_quirks(dev);
  12475. intel_init_pm(dev_priv);
  12476. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12477. return 0;
  12478. /*
  12479. * There may be no VBT; and if the BIOS enabled SSC we can
  12480. * just keep using it to avoid unnecessary flicker. Whereas if the
  12481. * BIOS isn't using it, don't assume it will work even if the VBT
  12482. * indicates as much.
  12483. */
  12484. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12485. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12486. DREF_SSC1_ENABLE);
  12487. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12488. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12489. bios_lvds_use_ssc ? "en" : "dis",
  12490. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12491. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12492. }
  12493. }
  12494. if (IS_GEN2(dev_priv)) {
  12495. dev->mode_config.max_width = 2048;
  12496. dev->mode_config.max_height = 2048;
  12497. } else if (IS_GEN3(dev_priv)) {
  12498. dev->mode_config.max_width = 4096;
  12499. dev->mode_config.max_height = 4096;
  12500. } else {
  12501. dev->mode_config.max_width = 8192;
  12502. dev->mode_config.max_height = 8192;
  12503. }
  12504. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12505. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12506. dev->mode_config.cursor_height = 1023;
  12507. } else if (IS_GEN2(dev_priv)) {
  12508. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12509. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12510. } else {
  12511. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12512. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12513. }
  12514. dev->mode_config.fb_base = ggtt->mappable_base;
  12515. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12516. INTEL_INFO(dev_priv)->num_pipes,
  12517. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12518. for_each_pipe(dev_priv, pipe) {
  12519. int ret;
  12520. ret = intel_crtc_init(dev_priv, pipe);
  12521. if (ret) {
  12522. drm_mode_config_cleanup(dev);
  12523. return ret;
  12524. }
  12525. }
  12526. intel_shared_dpll_init(dev);
  12527. intel_update_czclk(dev_priv);
  12528. intel_modeset_init_hw(dev);
  12529. if (dev_priv->max_cdclk_freq == 0)
  12530. intel_update_max_cdclk(dev_priv);
  12531. /* Just disable it once at startup */
  12532. i915_disable_vga(dev_priv);
  12533. intel_setup_outputs(dev_priv);
  12534. drm_modeset_lock_all(dev);
  12535. intel_modeset_setup_hw_state(dev);
  12536. drm_modeset_unlock_all(dev);
  12537. for_each_intel_crtc(dev, crtc) {
  12538. struct intel_initial_plane_config plane_config = {};
  12539. if (!crtc->active)
  12540. continue;
  12541. /*
  12542. * Note that reserving the BIOS fb up front prevents us
  12543. * from stuffing other stolen allocations like the ring
  12544. * on top. This prevents some ugliness at boot time, and
  12545. * can even allow for smooth boot transitions if the BIOS
  12546. * fb is large enough for the active pipe configuration.
  12547. */
  12548. dev_priv->display.get_initial_plane_config(crtc,
  12549. &plane_config);
  12550. /*
  12551. * If the fb is shared between multiple heads, we'll
  12552. * just get the first one.
  12553. */
  12554. intel_find_initial_plane_obj(crtc, &plane_config);
  12555. }
  12556. /*
  12557. * Make sure hardware watermarks really match the state we read out.
  12558. * Note that we need to do this after reconstructing the BIOS fb's
  12559. * since the watermark calculation done here will use pstate->fb.
  12560. */
  12561. if (!HAS_GMCH_DISPLAY(dev_priv))
  12562. sanitize_watermarks(dev);
  12563. return 0;
  12564. }
  12565. static void intel_enable_pipe_a(struct drm_device *dev)
  12566. {
  12567. struct intel_connector *connector;
  12568. struct drm_connector_list_iter conn_iter;
  12569. struct drm_connector *crt = NULL;
  12570. struct intel_load_detect_pipe load_detect_temp;
  12571. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12572. int ret;
  12573. /* We can't just switch on the pipe A, we need to set things up with a
  12574. * proper mode and output configuration. As a gross hack, enable pipe A
  12575. * by enabling the load detect pipe once. */
  12576. drm_connector_list_iter_begin(dev, &conn_iter);
  12577. for_each_intel_connector_iter(connector, &conn_iter) {
  12578. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12579. crt = &connector->base;
  12580. break;
  12581. }
  12582. }
  12583. drm_connector_list_iter_end(&conn_iter);
  12584. if (!crt)
  12585. return;
  12586. ret = intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx);
  12587. WARN(ret < 0, "All modeset mutexes are locked, but intel_get_load_detect_pipe failed\n");
  12588. if (ret > 0)
  12589. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12590. }
  12591. static bool
  12592. intel_check_plane_mapping(struct intel_crtc *crtc)
  12593. {
  12594. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12595. u32 val;
  12596. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12597. return true;
  12598. val = I915_READ(DSPCNTR(!crtc->plane));
  12599. if ((val & DISPLAY_PLANE_ENABLE) &&
  12600. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12601. return false;
  12602. return true;
  12603. }
  12604. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12605. {
  12606. struct drm_device *dev = crtc->base.dev;
  12607. struct intel_encoder *encoder;
  12608. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12609. return true;
  12610. return false;
  12611. }
  12612. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12613. {
  12614. struct drm_device *dev = encoder->base.dev;
  12615. struct intel_connector *connector;
  12616. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12617. return connector;
  12618. return NULL;
  12619. }
  12620. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12621. enum transcoder pch_transcoder)
  12622. {
  12623. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12624. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12625. }
  12626. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12627. {
  12628. struct drm_device *dev = crtc->base.dev;
  12629. struct drm_i915_private *dev_priv = to_i915(dev);
  12630. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12631. /* Clear any frame start delays used for debugging left by the BIOS */
  12632. if (!transcoder_is_dsi(cpu_transcoder)) {
  12633. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12634. I915_WRITE(reg,
  12635. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12636. }
  12637. /* restore vblank interrupts to correct state */
  12638. drm_crtc_vblank_reset(&crtc->base);
  12639. if (crtc->active) {
  12640. struct intel_plane *plane;
  12641. drm_crtc_vblank_on(&crtc->base);
  12642. /* Disable everything but the primary plane */
  12643. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12644. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12645. continue;
  12646. trace_intel_disable_plane(&plane->base, crtc);
  12647. plane->disable_plane(&plane->base, &crtc->base);
  12648. }
  12649. }
  12650. /* We need to sanitize the plane -> pipe mapping first because this will
  12651. * disable the crtc (and hence change the state) if it is wrong. Note
  12652. * that gen4+ has a fixed plane -> pipe mapping. */
  12653. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12654. bool plane;
  12655. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12656. crtc->base.base.id, crtc->base.name);
  12657. /* Pipe has the wrong plane attached and the plane is active.
  12658. * Temporarily change the plane mapping and disable everything
  12659. * ... */
  12660. plane = crtc->plane;
  12661. crtc->base.primary->state->visible = true;
  12662. crtc->plane = !plane;
  12663. intel_crtc_disable_noatomic(&crtc->base);
  12664. crtc->plane = plane;
  12665. }
  12666. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12667. crtc->pipe == PIPE_A && !crtc->active) {
  12668. /* BIOS forgot to enable pipe A, this mostly happens after
  12669. * resume. Force-enable the pipe to fix this, the update_dpms
  12670. * call below we restore the pipe to the right state, but leave
  12671. * the required bits on. */
  12672. intel_enable_pipe_a(dev);
  12673. }
  12674. /* Adjust the state of the output pipe according to whether we
  12675. * have active connectors/encoders. */
  12676. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12677. intel_crtc_disable_noatomic(&crtc->base);
  12678. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12679. /*
  12680. * We start out with underrun reporting disabled to avoid races.
  12681. * For correct bookkeeping mark this on active crtcs.
  12682. *
  12683. * Also on gmch platforms we dont have any hardware bits to
  12684. * disable the underrun reporting. Which means we need to start
  12685. * out with underrun reporting disabled also on inactive pipes,
  12686. * since otherwise we'll complain about the garbage we read when
  12687. * e.g. coming up after runtime pm.
  12688. *
  12689. * No protection against concurrent access is required - at
  12690. * worst a fifo underrun happens which also sets this to false.
  12691. */
  12692. crtc->cpu_fifo_underrun_disabled = true;
  12693. /*
  12694. * We track the PCH trancoder underrun reporting state
  12695. * within the crtc. With crtc for pipe A housing the underrun
  12696. * reporting state for PCH transcoder A, crtc for pipe B housing
  12697. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12698. * and marking underrun reporting as disabled for the non-existing
  12699. * PCH transcoders B and C would prevent enabling the south
  12700. * error interrupt (see cpt_can_enable_serr_int()).
  12701. */
  12702. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12703. crtc->pch_fifo_underrun_disabled = true;
  12704. }
  12705. }
  12706. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12707. {
  12708. struct intel_connector *connector;
  12709. /* We need to check both for a crtc link (meaning that the
  12710. * encoder is active and trying to read from a pipe) and the
  12711. * pipe itself being active. */
  12712. bool has_active_crtc = encoder->base.crtc &&
  12713. to_intel_crtc(encoder->base.crtc)->active;
  12714. connector = intel_encoder_find_connector(encoder);
  12715. if (connector && !has_active_crtc) {
  12716. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12717. encoder->base.base.id,
  12718. encoder->base.name);
  12719. /* Connector is active, but has no active pipe. This is
  12720. * fallout from our resume register restoring. Disable
  12721. * the encoder manually again. */
  12722. if (encoder->base.crtc) {
  12723. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12724. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12725. encoder->base.base.id,
  12726. encoder->base.name);
  12727. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12728. if (encoder->post_disable)
  12729. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12730. }
  12731. encoder->base.crtc = NULL;
  12732. /* Inconsistent output/port/pipe state happens presumably due to
  12733. * a bug in one of the get_hw_state functions. Or someplace else
  12734. * in our code, like the register restore mess on resume. Clamp
  12735. * things to off as a safer default. */
  12736. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12737. connector->base.encoder = NULL;
  12738. }
  12739. /* Enabled encoders without active connectors will be fixed in
  12740. * the crtc fixup. */
  12741. }
  12742. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12743. {
  12744. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12745. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12746. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12747. i915_disable_vga(dev_priv);
  12748. }
  12749. }
  12750. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12751. {
  12752. /* This function can be called both from intel_modeset_setup_hw_state or
  12753. * at a very early point in our resume sequence, where the power well
  12754. * structures are not yet restored. Since this function is at a very
  12755. * paranoid "someone might have enabled VGA while we were not looking"
  12756. * level, just check if the power well is enabled instead of trying to
  12757. * follow the "don't touch the power well if we don't need it" policy
  12758. * the rest of the driver uses. */
  12759. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12760. return;
  12761. i915_redisable_vga_power_on(dev_priv);
  12762. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12763. }
  12764. static bool primary_get_hw_state(struct intel_plane *plane)
  12765. {
  12766. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12767. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12768. }
  12769. /* FIXME read out full plane state for all planes */
  12770. static void readout_plane_state(struct intel_crtc *crtc)
  12771. {
  12772. struct intel_plane *primary = to_intel_plane(crtc->base.primary);
  12773. bool visible;
  12774. visible = crtc->active && primary_get_hw_state(primary);
  12775. intel_set_plane_visible(to_intel_crtc_state(crtc->base.state),
  12776. to_intel_plane_state(primary->base.state),
  12777. visible);
  12778. }
  12779. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12780. {
  12781. struct drm_i915_private *dev_priv = to_i915(dev);
  12782. enum pipe pipe;
  12783. struct intel_crtc *crtc;
  12784. struct intel_encoder *encoder;
  12785. struct intel_connector *connector;
  12786. struct drm_connector_list_iter conn_iter;
  12787. int i;
  12788. dev_priv->active_crtcs = 0;
  12789. for_each_intel_crtc(dev, crtc) {
  12790. struct intel_crtc_state *crtc_state =
  12791. to_intel_crtc_state(crtc->base.state);
  12792. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12793. memset(crtc_state, 0, sizeof(*crtc_state));
  12794. crtc_state->base.crtc = &crtc->base;
  12795. crtc_state->base.active = crtc_state->base.enable =
  12796. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12797. crtc->base.enabled = crtc_state->base.enable;
  12798. crtc->active = crtc_state->base.active;
  12799. if (crtc_state->base.active)
  12800. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12801. readout_plane_state(crtc);
  12802. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12803. crtc->base.base.id, crtc->base.name,
  12804. enableddisabled(crtc_state->base.active));
  12805. }
  12806. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12807. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12808. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12809. &pll->state.hw_state);
  12810. pll->state.crtc_mask = 0;
  12811. for_each_intel_crtc(dev, crtc) {
  12812. struct intel_crtc_state *crtc_state =
  12813. to_intel_crtc_state(crtc->base.state);
  12814. if (crtc_state->base.active &&
  12815. crtc_state->shared_dpll == pll)
  12816. pll->state.crtc_mask |= 1 << crtc->pipe;
  12817. }
  12818. pll->active_mask = pll->state.crtc_mask;
  12819. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12820. pll->name, pll->state.crtc_mask, pll->on);
  12821. }
  12822. for_each_intel_encoder(dev, encoder) {
  12823. pipe = 0;
  12824. if (encoder->get_hw_state(encoder, &pipe)) {
  12825. struct intel_crtc_state *crtc_state;
  12826. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12827. crtc_state = to_intel_crtc_state(crtc->base.state);
  12828. encoder->base.crtc = &crtc->base;
  12829. crtc_state->output_types |= 1 << encoder->type;
  12830. encoder->get_config(encoder, crtc_state);
  12831. } else {
  12832. encoder->base.crtc = NULL;
  12833. }
  12834. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12835. encoder->base.base.id, encoder->base.name,
  12836. enableddisabled(encoder->base.crtc),
  12837. pipe_name(pipe));
  12838. }
  12839. drm_connector_list_iter_begin(dev, &conn_iter);
  12840. for_each_intel_connector_iter(connector, &conn_iter) {
  12841. if (connector->get_hw_state(connector)) {
  12842. connector->base.dpms = DRM_MODE_DPMS_ON;
  12843. encoder = connector->encoder;
  12844. connector->base.encoder = &encoder->base;
  12845. if (encoder->base.crtc &&
  12846. encoder->base.crtc->state->active) {
  12847. /*
  12848. * This has to be done during hardware readout
  12849. * because anything calling .crtc_disable may
  12850. * rely on the connector_mask being accurate.
  12851. */
  12852. encoder->base.crtc->state->connector_mask |=
  12853. 1 << drm_connector_index(&connector->base);
  12854. encoder->base.crtc->state->encoder_mask |=
  12855. 1 << drm_encoder_index(&encoder->base);
  12856. }
  12857. } else {
  12858. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12859. connector->base.encoder = NULL;
  12860. }
  12861. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12862. connector->base.base.id, connector->base.name,
  12863. enableddisabled(connector->base.encoder));
  12864. }
  12865. drm_connector_list_iter_end(&conn_iter);
  12866. for_each_intel_crtc(dev, crtc) {
  12867. struct intel_crtc_state *crtc_state =
  12868. to_intel_crtc_state(crtc->base.state);
  12869. int pixclk = 0;
  12870. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  12871. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12872. if (crtc_state->base.active) {
  12873. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12874. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12875. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12876. /*
  12877. * The initial mode needs to be set in order to keep
  12878. * the atomic core happy. It wants a valid mode if the
  12879. * crtc's enabled, so we do the above call.
  12880. *
  12881. * But we don't set all the derived state fully, hence
  12882. * set a flag to indicate that a full recalculation is
  12883. * needed on the next commit.
  12884. */
  12885. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12886. intel_crtc_compute_pixel_rate(crtc_state);
  12887. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12888. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12889. pixclk = crtc_state->pixel_rate;
  12890. else
  12891. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12892. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12893. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12894. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12895. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12896. update_scanline_offset(crtc);
  12897. }
  12898. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12899. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12900. }
  12901. }
  12902. static void
  12903. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12904. {
  12905. struct intel_encoder *encoder;
  12906. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12907. u64 get_domains;
  12908. enum intel_display_power_domain domain;
  12909. if (!encoder->get_power_domains)
  12910. continue;
  12911. get_domains = encoder->get_power_domains(encoder);
  12912. for_each_power_domain(domain, get_domains)
  12913. intel_display_power_get(dev_priv, domain);
  12914. }
  12915. }
  12916. /* Scan out the current hw modeset state,
  12917. * and sanitizes it to the current state
  12918. */
  12919. static void
  12920. intel_modeset_setup_hw_state(struct drm_device *dev)
  12921. {
  12922. struct drm_i915_private *dev_priv = to_i915(dev);
  12923. enum pipe pipe;
  12924. struct intel_crtc *crtc;
  12925. struct intel_encoder *encoder;
  12926. int i;
  12927. intel_modeset_readout_hw_state(dev);
  12928. /* HW state is read out, now we need to sanitize this mess. */
  12929. get_encoder_power_domains(dev_priv);
  12930. for_each_intel_encoder(dev, encoder) {
  12931. intel_sanitize_encoder(encoder);
  12932. }
  12933. for_each_pipe(dev_priv, pipe) {
  12934. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12935. intel_sanitize_crtc(crtc);
  12936. intel_dump_pipe_config(crtc, crtc->config,
  12937. "[setup_hw_state]");
  12938. }
  12939. intel_modeset_update_connector_atomic_state(dev);
  12940. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12941. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12942. if (!pll->on || pll->active_mask)
  12943. continue;
  12944. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12945. pll->funcs.disable(dev_priv, pll);
  12946. pll->on = false;
  12947. }
  12948. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12949. vlv_wm_get_hw_state(dev);
  12950. vlv_wm_sanitize(dev_priv);
  12951. } else if (IS_GEN9(dev_priv)) {
  12952. skl_wm_get_hw_state(dev);
  12953. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12954. ilk_wm_get_hw_state(dev);
  12955. }
  12956. for_each_intel_crtc(dev, crtc) {
  12957. u64 put_domains;
  12958. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12959. if (WARN_ON(put_domains))
  12960. modeset_put_power_domains(dev_priv, put_domains);
  12961. }
  12962. intel_display_set_init_power(dev_priv, false);
  12963. intel_power_domains_verify_state(dev_priv);
  12964. intel_fbc_init_pipe_state(dev_priv);
  12965. }
  12966. void intel_display_resume(struct drm_device *dev)
  12967. {
  12968. struct drm_i915_private *dev_priv = to_i915(dev);
  12969. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12970. struct drm_modeset_acquire_ctx ctx;
  12971. int ret;
  12972. dev_priv->modeset_restore_state = NULL;
  12973. if (state)
  12974. state->acquire_ctx = &ctx;
  12975. /*
  12976. * This is a cludge because with real atomic modeset mode_config.mutex
  12977. * won't be taken. Unfortunately some probed state like
  12978. * audio_codec_enable is still protected by mode_config.mutex, so lock
  12979. * it here for now.
  12980. */
  12981. mutex_lock(&dev->mode_config.mutex);
  12982. drm_modeset_acquire_init(&ctx, 0);
  12983. while (1) {
  12984. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12985. if (ret != -EDEADLK)
  12986. break;
  12987. drm_modeset_backoff(&ctx);
  12988. }
  12989. if (!ret)
  12990. ret = __intel_display_resume(dev, state, &ctx);
  12991. drm_modeset_drop_locks(&ctx);
  12992. drm_modeset_acquire_fini(&ctx);
  12993. mutex_unlock(&dev->mode_config.mutex);
  12994. if (ret)
  12995. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12996. if (state)
  12997. drm_atomic_state_put(state);
  12998. }
  12999. void intel_modeset_gem_init(struct drm_device *dev)
  13000. {
  13001. struct drm_i915_private *dev_priv = to_i915(dev);
  13002. intel_init_gt_powersave(dev_priv);
  13003. intel_setup_overlay(dev_priv);
  13004. }
  13005. int intel_connector_register(struct drm_connector *connector)
  13006. {
  13007. struct intel_connector *intel_connector = to_intel_connector(connector);
  13008. int ret;
  13009. ret = intel_backlight_device_register(intel_connector);
  13010. if (ret)
  13011. goto err;
  13012. return 0;
  13013. err:
  13014. return ret;
  13015. }
  13016. void intel_connector_unregister(struct drm_connector *connector)
  13017. {
  13018. struct intel_connector *intel_connector = to_intel_connector(connector);
  13019. intel_backlight_device_unregister(intel_connector);
  13020. intel_panel_destroy_backlight(connector);
  13021. }
  13022. void intel_modeset_cleanup(struct drm_device *dev)
  13023. {
  13024. struct drm_i915_private *dev_priv = to_i915(dev);
  13025. flush_work(&dev_priv->atomic_helper.free_work);
  13026. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  13027. intel_disable_gt_powersave(dev_priv);
  13028. /*
  13029. * Interrupts and polling as the first thing to avoid creating havoc.
  13030. * Too much stuff here (turning of connectors, ...) would
  13031. * experience fancy races otherwise.
  13032. */
  13033. intel_irq_uninstall(dev_priv);
  13034. /*
  13035. * Due to the hpd irq storm handling the hotplug work can re-arm the
  13036. * poll handlers. Hence disable polling after hpd handling is shut down.
  13037. */
  13038. drm_kms_helper_poll_fini(dev);
  13039. intel_unregister_dsm_handler();
  13040. intel_fbc_global_disable(dev_priv);
  13041. /* flush any delayed tasks or pending work */
  13042. flush_scheduled_work();
  13043. drm_mode_config_cleanup(dev);
  13044. intel_cleanup_overlay(dev_priv);
  13045. intel_cleanup_gt_powersave(dev_priv);
  13046. intel_teardown_gmbus(dev_priv);
  13047. }
  13048. void intel_connector_attach_encoder(struct intel_connector *connector,
  13049. struct intel_encoder *encoder)
  13050. {
  13051. connector->encoder = encoder;
  13052. drm_mode_connector_attach_encoder(&connector->base,
  13053. &encoder->base);
  13054. }
  13055. /*
  13056. * set vga decode state - true == enable VGA decode
  13057. */
  13058. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13059. {
  13060. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13061. u16 gmch_ctrl;
  13062. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13063. DRM_ERROR("failed to read control word\n");
  13064. return -EIO;
  13065. }
  13066. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13067. return 0;
  13068. if (state)
  13069. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13070. else
  13071. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13072. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13073. DRM_ERROR("failed to write control word\n");
  13074. return -EIO;
  13075. }
  13076. return 0;
  13077. }
  13078. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13079. struct intel_display_error_state {
  13080. u32 power_well_driver;
  13081. int num_transcoders;
  13082. struct intel_cursor_error_state {
  13083. u32 control;
  13084. u32 position;
  13085. u32 base;
  13086. u32 size;
  13087. } cursor[I915_MAX_PIPES];
  13088. struct intel_pipe_error_state {
  13089. bool power_domain_on;
  13090. u32 source;
  13091. u32 stat;
  13092. } pipe[I915_MAX_PIPES];
  13093. struct intel_plane_error_state {
  13094. u32 control;
  13095. u32 stride;
  13096. u32 size;
  13097. u32 pos;
  13098. u32 addr;
  13099. u32 surface;
  13100. u32 tile_offset;
  13101. } plane[I915_MAX_PIPES];
  13102. struct intel_transcoder_error_state {
  13103. bool power_domain_on;
  13104. enum transcoder cpu_transcoder;
  13105. u32 conf;
  13106. u32 htotal;
  13107. u32 hblank;
  13108. u32 hsync;
  13109. u32 vtotal;
  13110. u32 vblank;
  13111. u32 vsync;
  13112. } transcoder[4];
  13113. };
  13114. struct intel_display_error_state *
  13115. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13116. {
  13117. struct intel_display_error_state *error;
  13118. int transcoders[] = {
  13119. TRANSCODER_A,
  13120. TRANSCODER_B,
  13121. TRANSCODER_C,
  13122. TRANSCODER_EDP,
  13123. };
  13124. int i;
  13125. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13126. return NULL;
  13127. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13128. if (error == NULL)
  13129. return NULL;
  13130. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13131. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13132. for_each_pipe(dev_priv, i) {
  13133. error->pipe[i].power_domain_on =
  13134. __intel_display_power_is_enabled(dev_priv,
  13135. POWER_DOMAIN_PIPE(i));
  13136. if (!error->pipe[i].power_domain_on)
  13137. continue;
  13138. error->cursor[i].control = I915_READ(CURCNTR(i));
  13139. error->cursor[i].position = I915_READ(CURPOS(i));
  13140. error->cursor[i].base = I915_READ(CURBASE(i));
  13141. error->plane[i].control = I915_READ(DSPCNTR(i));
  13142. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13143. if (INTEL_GEN(dev_priv) <= 3) {
  13144. error->plane[i].size = I915_READ(DSPSIZE(i));
  13145. error->plane[i].pos = I915_READ(DSPPOS(i));
  13146. }
  13147. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13148. error->plane[i].addr = I915_READ(DSPADDR(i));
  13149. if (INTEL_GEN(dev_priv) >= 4) {
  13150. error->plane[i].surface = I915_READ(DSPSURF(i));
  13151. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13152. }
  13153. error->pipe[i].source = I915_READ(PIPESRC(i));
  13154. if (HAS_GMCH_DISPLAY(dev_priv))
  13155. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13156. }
  13157. /* Note: this does not include DSI transcoders. */
  13158. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13159. if (HAS_DDI(dev_priv))
  13160. error->num_transcoders++; /* Account for eDP. */
  13161. for (i = 0; i < error->num_transcoders; i++) {
  13162. enum transcoder cpu_transcoder = transcoders[i];
  13163. error->transcoder[i].power_domain_on =
  13164. __intel_display_power_is_enabled(dev_priv,
  13165. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13166. if (!error->transcoder[i].power_domain_on)
  13167. continue;
  13168. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13169. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13170. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13171. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13172. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13173. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13174. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13175. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13176. }
  13177. return error;
  13178. }
  13179. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13180. void
  13181. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13182. struct intel_display_error_state *error)
  13183. {
  13184. struct drm_i915_private *dev_priv = m->i915;
  13185. int i;
  13186. if (!error)
  13187. return;
  13188. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13189. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13190. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13191. error->power_well_driver);
  13192. for_each_pipe(dev_priv, i) {
  13193. err_printf(m, "Pipe [%d]:\n", i);
  13194. err_printf(m, " Power: %s\n",
  13195. onoff(error->pipe[i].power_domain_on));
  13196. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13197. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13198. err_printf(m, "Plane [%d]:\n", i);
  13199. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13200. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13201. if (INTEL_GEN(dev_priv) <= 3) {
  13202. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13203. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13204. }
  13205. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13206. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13207. if (INTEL_GEN(dev_priv) >= 4) {
  13208. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13209. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13210. }
  13211. err_printf(m, "Cursor [%d]:\n", i);
  13212. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13213. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13214. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13215. }
  13216. for (i = 0; i < error->num_transcoders; i++) {
  13217. err_printf(m, "CPU transcoder: %s\n",
  13218. transcoder_name(error->transcoder[i].cpu_transcoder));
  13219. err_printf(m, " Power: %s\n",
  13220. onoff(error->transcoder[i].power_domain_on));
  13221. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13222. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13223. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13224. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13225. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13226. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13227. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13228. }
  13229. }
  13230. #endif